]>
gitweb.michael.orlitzky.com - mailshears.git/blob - bin/mailshears
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.
19 # Load our config file.
20 require 'configuration'
22 # And the necessary classes.
24 require 'exit_codes.rb'
25 require 'postfixadmin_db'
27 Configuration
::PLUGINS.each
do |plugin_file
|
28 require "plugins/#{plugin_file}"
31 pgadb
= PostfixadminDb
.new(Configuration
::DBHOST,
32 Configuration
::DBPORT,
33 Configuration
::DBOPTS,
35 Configuration
::DBNAME,
36 Configuration
::DBUSER,
37 Configuration
::DBPASS)
41 # Get a list of domains from the Postfixadmin database.
42 db_domains
= pgadb
.get_domains_from_db()
43 rescue DatabaseError
=> e
44 puts
"There was an error connecting to the database: #{e.to_s}"
45 Kernel
.exit(ExitCodes
::DATABASE_ERROR)
50 db_accounts
= pgadb
.get_accounts_from_db()
51 rescue DatabaseError
=> e
52 puts
"There was an error connecting to the database: #{e.to_s}"
53 Kernel
.exit(ExitCodes
::DATABASE_ERROR)
57 Plugin
.includers
.each
do |plugin_class
|
58 plugin
= plugin_class
.new()
61 leftover_domains
= plugin
.get_leftover_domains(db_domains
)
62 rescue StandardError
=> e
63 puts
"There was an error retrieving domains from the filesystem: #{e.to_s}"
64 Kernel
.exit(ExitCodes
::FILESYSTEM_ERROR)
68 leftover_accounts
= plugin
.get_leftover_accounts(db_accounts
)
69 rescue StandardError
=> e
70 puts
"There was an error retrieving accounts from the filesystem: #{e.to_s}"
71 Kernel
.exit(ExitCodes
::FILESYSTEM_ERROR)
74 if leftover_domains
.size
> 0 or leftover_accounts
.size
> 0
75 # The header that we output before the list of domains/accounts.
76 # Just the path of this script, the current time, and the plugin name.
79 current_time
= Time
.now()
80 if current_time
.respond_to
?(:iso8601)
81 # Somehow this method is missing on some machines.
82 header +
= current_time
.iso8601
.to_s
84 # Fall back to whatever this looks like.
85 header +
= current_time
.to_s +
"\n"
88 header +
= 'Plugin: ' + plugin_class
.to_s +
"\n"
90 puts
'-' * header
.size
# Underline the header.
92 leftover_domains
.each
do |domain
|
93 puts
"Found: #{domain} (#{plugin.describe_domain(domain)})"
96 leftover_accounts
.each
do |account
|
97 puts
"Found: #{account} (#{plugin.describe_account(account)})"
100 if Configuration
::I_MEAN_BUSINESS
101 # We have to delete the accounts before the domain,
102 # otherwise they'd already be gone.
103 leftover_accounts
.each
do |account
|
104 # Get the description before we delete the domain.
105 # This can still fail if the account's domain is gone.
106 account_description
= plugin
.describe_account(account
)
107 plugin
.delete_account(account
)
108 puts
"Removed: #{account} (#{account_description})"
111 leftover_domains
.each
do |domain
|
112 # Get the description before we delete the domain.
113 domain_description
= plugin
.describe_domain(domain
)
114 plugin
.delete_domain(domain
)
115 puts
"Removed: #{domain} (#{domain_description})"