]>
gitweb.michael.orlitzky.com - mailshears.git/blob - bin/mailshears
96a3c303777269d9a1a32c8110f6821478fbae15
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 # Load our config file.
19 require 'configuration'
21 pgadb
= PostfixadminDb
.new(Configuration
::DBHOST,
22 Configuration
::DBPORT,
23 Configuration
::DBOPTS,
25 Configuration
::DBNAME,
26 Configuration
::DBUSER,
27 Configuration
::DBPASS)
31 # Get a list of domains from the Postfixadmin database.
32 db_domains
= pgadb
.get_domains_from_db()
33 rescue DatabaseError
=> e
34 puts
"There was an error connecting to the database: #{e.to_s}"
35 Kernel
.exit(ExitCodes
::DATABASE_ERROR)
40 db_accounts
= pgadb
.get_accounts_from_db()
41 rescue DatabaseError
=> e
42 puts
"There was an error connecting to the database: #{e.to_s}"
43 Kernel
.exit(ExitCodes
::DATABASE_ERROR)
47 Plugin
.includers
.each
do |plugin_class
|
48 plugin
= plugin_class
.new()
51 leftover_domains
= plugin
.get_leftover_domains(db_domains
)
52 rescue StandardError
=> e
53 puts
"There was an error retrieving domains from the filesystem: #{e.to_s}"
54 Kernel
.exit(ExitCodes
::FILESYSTEM_ERROR)
58 leftover_accounts
= plugin
.get_leftover_accounts(db_accounts
)
59 rescue StandardError
=> e
60 puts
"There was an error retrieving accounts from the filesystem: #{e.to_s}"
61 Kernel
.exit(ExitCodes
::FILESYSTEM_ERROR)
64 if leftover_domains
.size
> 0 or leftover_accounts
.size
> 0
65 # The header that we output before the list of domains/accounts.
66 # Just the path of this script, the current time, and the plugin name.
69 current_time
= Time
.now()
70 if current_time
.respond_to
?(:iso8601)
71 # Somehow this method is missing on some machines.
72 header +
= current_time
.iso8601
.to_s
74 # Fall back to whatever this looks like.
75 header +
= current_time
.to_s +
"\n"
78 header +
= 'Plugin: ' + plugin_class
.to_s +
"\n"
80 puts
'-' * header
.size
# Underline the header.
82 leftover_domains
.each
do |domain
|
83 puts
"Found: #{domain} (#{plugin.describe_domain(domain)})"
86 leftover_accounts
.each
do |account
|
87 puts
"Found: #{account} (#{plugin.describe_account(account)})"
90 if Configuration
::I_MEAN_BUSINESS
91 # We have to delete the accounts before the domain,
92 # otherwise they'd already be gone.
93 leftover_accounts
.each
do |account
|
94 # Get the description before we delete the domain.
95 # This can still fail if the account's domain is gone.
96 account_description
= plugin
.describe_account(account
)
97 plugin
.delete_account(account
)
98 puts
"Removed: #{account} (#{account_description})"
101 leftover_domains
.each
do |domain
|
102 # Get the description before we delete the domain.
103 domain_description
= plugin
.describe_domain(domain
)
104 plugin
.delete_domain(domain
)
105 puts
"Removed: #{domain} (#{domain_description})"