]>
gitweb.michael.orlitzky.com - mailshears.git/blob - mailshears
b1a5e00e1d66885f7ceed478f6031912b540429f
3 # mailshears, to prune unused mail directories.
5 # Mail accounts for virtual hosts are stored in SQL, and managed by
6 # Postfixadmin. However, the physical directories are handled by
7 # Postfix/Dovecot and are left untouched by Postfixadmin. This is good
8 # for security, but comes at a cost: Postfixadmin can't remove a
9 # user's mail directory when his or her account is deleted.
11 # This program compares the list of filesystem accounts with the ones
12 # in the database. It outputs any accounts that exist in the
13 # filesystem, but not the database.
18 pgadb
= PostfixadminDb
.new(Configuration
::dbhost,
19 Configuration
::dbport,
20 Configuration
::dbopts,
22 Configuration
::dbname,
23 Configuration
::dbuser,
24 Configuration
::dbpass)
28 # Get a list of domains from the Postfixadmin database.
29 db_domains
= pgadb
.get_domains_from_db()
30 rescue DatabaseError
=> e
31 puts
"There was an error connecting to the database: #{e.to_s}"
32 Kernel
.exit(ExitCodes
::DATABASE_ERROR)
37 db_accounts
= pgadb
.get_accounts_from_db()
38 rescue DatabaseError
=> e
39 puts
"There was an error connecting to the database: #{e.to_s}"
40 Kernel
.exit(ExitCodes
::DATABASE_ERROR)
44 Plugin
.includers
.each
do |plugin_class
|
45 plugin
= plugin_class
.new()
48 leftover_domains
= plugin
.get_leftover_domains(db_domains
)
49 rescue StandardError
=> e
50 puts
"There was an error retrieving domains from the filesystem: #{e.to_s}"
51 Kernel
.exit(ExitCodes
::FILESYSTEM_ERROR)
55 leftover_accounts
= plugin
.get_leftover_accounts(db_accounts
)
56 rescue StandardError
=> e
57 puts
"There was an error retrieving accounts from the filesystem: #{e.to_s}"
58 Kernel
.exit(ExitCodes
::FILESYSTEM_ERROR)
61 if leftover_domains
.size
> 0 or leftover_accounts
.size
> 0
62 # The header that we output before the list of domains/accounts.
63 # Just the path of this script, the current time, and the plugin name.
66 current_time
= Time
.now()
67 if current_time
.respond_to
?(:iso8601)
68 # Somehow this method is missing on some machines.
69 header +
= current_time
.iso8601
.to_s
71 # Fall back to whatever this looks like.
72 header +
= current_time
.to_s +
"\n"
75 header +
= 'Plugin: ' + plugin_class
.to_s +
"\n"
77 puts
'-' * header
.size
# Underline the header.
79 leftover_domains
.each
do |domain
|
80 puts
"Found: #{domain} (#{plugin.describe_domain(domain)})"
83 leftover_accounts
.each
do |account
|
84 puts
"Found: #{account} (#{plugin.describe_account(account)})"
87 if Configuration
::i_mean_business
88 # We have to delete the accounts before the domain,
89 # otherwise they'd already be gone.
90 leftover_accounts
.each
do |account
|
91 # Get the description before we delete the domain.
92 # This can still fail if the account's domain is gone.
93 account_description
= plugin
.describe_account(account
)
94 plugin
.delete_account(account
)
95 puts
"Removed: #{account} (#{account_description})"
98 leftover_domains
.each
do |domain
|
99 # Get the description before we delete the domain.
100 domain_description
= plugin
.describe_domain(domain
)
101 plugin
.delete_domain(domain
)
102 puts
"Removed: #{domain} (#{domain_description})"