]>
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.
16 # We need Pathname to get the real filesystem path
17 # of this script (and not, for example, the path of
18 # a symlink which points to it).
21 # This bit of magic adds the parent directory (the
22 # project root) to the list of ruby load paths.
23 # Thus, our require statements will work regardless of
24 # how or from where the script was run.
25 executable
= Pathname
.new(__FILE__
).realpath
.to_s
26 $
: << File
.dirname(executable
) +
'/../'
31 # Load our config file.
32 require 'bin/configuration'
34 # And the necessary classes.
35 require 'src/errors.rb'
36 require 'src/exit_codes.rb'
37 require 'src/postfixadmin_db'
39 Configuration
::PLUGINS.each
do |plugin_file
|
40 require "src/plugins/#{plugin_file}"
43 pgadb
= PostfixadminDb
.new(Configuration
::DBHOST,
44 Configuration
::DBPORT,
45 Configuration
::DBOPTS,
47 Configuration
::DBNAME,
48 Configuration
::DBUSER,
49 Configuration
::DBPASS)
53 # Get a list of domains from the Postfixadmin database.
54 db_domains
= pgadb
.get_domains_from_db()
55 rescue DatabaseError
=> e
56 puts
"There was an error connecting to the database: #{e.to_s}"
57 Kernel
.exit(ExitCodes
::DATABASE_ERROR)
62 db_accounts
= pgadb
.get_accounts_from_db()
63 rescue DatabaseError
=> e
64 puts
"There was an error connecting to the database: #{e.to_s}"
65 Kernel
.exit(ExitCodes
::DATABASE_ERROR)
69 Plugin
.includers
.each
do |plugin_class
|
70 plugin
= plugin_class
.new()
73 leftover_domains
= plugin
.get_leftover_domains(db_domains
)
74 rescue StandardError
=> e
75 puts
"There was an error retrieving domains from the filesystem: #{e.to_s}"
76 Kernel
.exit(ExitCodes
::FILESYSTEM_ERROR)
80 leftover_accounts
= plugin
.get_leftover_accounts(db_accounts
)
81 rescue StandardError
=> e
82 puts
"There was an error retrieving accounts from the filesystem: #{e.to_s}"
83 Kernel
.exit(ExitCodes
::FILESYSTEM_ERROR)
86 if leftover_domains
.size
> 0 or leftover_accounts
.size
> 0
87 # The header that we output before the list of domains/accounts.
88 # Just the path of this script, the current time, and the plugin name.
91 current_time
= Time
.now()
92 if current_time
.respond_to
?(:iso8601)
93 # Somehow this method is missing on some machines.
94 header +
= current_time
.iso8601
.to_s
96 # Fall back to whatever this looks like.
97 header +
= current_time
.to_s +
"\n"
100 header +
= 'Plugin: ' + plugin_class
.to_s +
"\n"
102 puts
'-' * header
.size
# Underline the header.
104 leftover_domains
.each
do |domain
|
105 puts
"Found: #{domain} (#{plugin.describe_domain(domain)})"
108 leftover_accounts
.each
do |account
|
109 puts
"Found: #{account} (#{plugin.describe_account(account)})"
112 if Configuration
::I_MEAN_BUSINESS
113 leftover_domains
.each
do |domain
|
114 plugin
.delete_domain(domain
)
115 puts
"Removed: #{domain} (#{plugin.describe_domain(domain)})"
118 leftover_accounts
.each
do |account
|
120 plugin
.delete_account(account
)
121 puts
"Removed: #{account} (#{plugin.describe_account(account)})"
122 rescue NonexistentAccountError
=> e
123 # This usually happens after an account's domain is deleted.
124 # When we try to delete the account, it's already gone.
125 puts
"Gone: #{account} (#{plugin.describe_account(account)})"