]>
gitweb.michael.orlitzky.com - mailshears.git/blob - mailshears
d181d0834f70a84492530a779a6b022c9ee2c674
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 cfg
= Configuration
.new()
19 pgadb
= PostfixadminDb
.new(cfg
.dbhost
,
29 # Get a list of domains from the Postfixadmin database.
30 db_domains
= pgadb
.get_domains_from_db()
31 rescue DatabaseError
=> e
32 puts
"There was an error connecting to the database: #{e.to_s}"
33 Kernel
.exit(ExitCodes
::DATABASE_ERROR)
38 db_accounts
= pgadb
.get_accounts_from_db()
39 rescue DatabaseError
=> e
40 puts
"There was an error connecting to the database: #{e.to_s}"
41 Kernel
.exit(ExitCodes
::DATABASE_ERROR)
45 Plugin
.includers
.each
do |plugin_class
|
46 plugin
= plugin_class
.new()
49 leftover_domains
= plugin
.get_leftover_domains(db_domains
)
50 rescue StandardError
=> e
51 puts
"There was an error retrieving domains from the filesystem: #{e.to_s}"
52 Kernel
.exit(ExitCodes
::FILESYSTEM_ERROR)
56 leftover_accounts
= plugin
.get_leftover_accounts(db_accounts
)
57 rescue StandardError
=> e
58 puts
"There was an error retrieving accounts from the filesystem: #{e.to_s}"
59 Kernel
.exit(ExitCodes
::FILESYSTEM_ERROR)
62 if leftover_domains
.size
> 0 or leftover_accounts
.size
> 0
63 # The header that we output before the list of domains/accounts.
64 # Just the path of this script, the current time, and the plugin name.
67 current_time
= Time
.now()
68 if current_time
.respond_to
?(:iso8601)
69 # Somehow this method is missing on some machines.
70 header +
= current_time
.iso8601
.to_s
72 # Fall back to whatever this looks like.
73 header +
= current_time
.to_s +
"\n"
76 header +
= 'Plugin: ' + plugin_class
.to_s +
"\n"
78 puts
'-' * header
.size
# Underline the header.
80 leftover_domains
.each
do |domain
|
81 puts
"Found: #{domain} (#{plugin.describe_domain(domain)})"
84 leftover_accounts
.each
do |account
|
85 puts
"Found: #{account} (#{plugin.describe_account(account)})"
88 if cfg
.i_mean_business
89 # We have to delete the accounts before the domain,
90 # otherwise they'd already be gone.
91 leftover_accounts
.each
do |account
|
92 # Get the description before we delete the domain.
93 # This can still fail if the account's domain is gone.
94 account_description
= plugin
.describe_account(account
)
95 plugin
.delete_account(account
)
96 puts
"Removed: #{account} (#{account_description})"
99 leftover_domains
.each
do |domain
|
100 # Get the description before we delete the domain.
101 domain_description
= plugin
.describe_domain(domain
)
102 plugin
.delete_domain(domain
)
103 puts
"Removed: #{domain} (#{domain_description})"