]>
gitweb.michael.orlitzky.com - mailshears.git/blob - bin/mailshears
06534d887c9125dabde3ab43e6371253f0bda122
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 # Define a usage string using the program name.
17 exe
= File
.basename($PROGRAM_NAME)
18 usage
= "#{exe} [prune | rm <target> | mv <src> <dst>]"
24 # Now, if a mode was supplied, it should be in ARGV[0].
26 mode_names
= ['prune', 'rm', 'mv']
27 if mode_names
.include?(ARGV.first().downcase()) then
28 # Peel the mode name off the head of the list.
29 mode_name
= ARGV.shift()
33 if mode_name
== 'rm' then
35 elsif mode_name
== 'mv' then
39 # Need this before referencing ExitCodes.
42 # Since we removed both the executable name and the mode name (if it
43 # existed) from ARGV, what remains should be the required
45 if (mode
== :prune and (ARGV.length() !
= 0)) or
46 (mode
== :rm and (ARGV.length() !
= 1)) or
47 (mode
== :mv and (ARGV.length() !
= 2)) then
48 puts
"ERROR: missing (or extra) command-line arguments."
49 puts
"Usage: #{usage}"
50 Kernel
.exit(ExitCodes
::BAD_COMMAND_LINE)
53 cfg
= Configuration
.new()
55 # Load each of the plugins that we'll need.
56 cfg
.plugins
.each
do |plugin_file
|
57 require "#{mode_name}/plugins/#{plugin_file}"
61 require "#{mode_name}/#{mode_name}_runner"
62 require "#{mode_name}/#{mode_name}_dummy_runner"
64 def make_header(plugin_name
)
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.
67 exe
= File
.basename($PROGRAM_NAME)
70 current_time
= Time
.now()
71 if current_time
.respond_to
?(:iso8601)
72 # Somehow this method is missing on some machines.
73 header +
= current_time
.iso8601
.to_s
75 # Fall back to whatever this looks like.
76 header +
= current_time
.to_s
79 header +
= ' (Plugin: ' + plugin_name +
")\n"
80 header +
= '-' * header
.size
# Underline the header.
87 RmPlugin
.includers
.each
do |plugin_class
|
88 plugin
= plugin_class
.new()
89 puts
make_header(plugin_class
.to_s())
91 if cfg
.i_mean_business
then
92 runner
= RmRunner
.new()
94 runner
= RmDummyRunner
.new()
97 runner
.run(plugin
, ARGV)
106 pgadb
= PostfixadminDb
.new(cfg
.dbhost
,
118 # Get a list of domains from the Postfixadmin database.
119 db_domains
= pgadb
.get_domains_from_db()
120 rescue DatabaseError
=> e
121 puts
"There was an error connecting to the database: #{e.to_s}"
122 Kernel
.exit(ExitCodes
::DATABASE_ERROR)
127 db_accounts
= pgadb
.get_accounts_from_db()
128 rescue DatabaseError
=> e
129 puts
"There was an error connecting to the database: #{e.to_s}"
130 Kernel
.exit(ExitCodes
::DATABASE_ERROR)
134 Plugin
.includers
.each
do |plugin_class
|
135 plugin
= plugin_class
.new()
138 leftover_domains
= plugin
.get_leftover_domains(db_domains
)
139 rescue StandardError
=> e
140 puts
"There was an error retrieving domains from the filesystem: #{e.to_s}"
141 Kernel
.exit(ExitCodes
::FILESYSTEM_ERROR)
145 leftover_accounts
= plugin
.get_leftover_accounts(db_accounts
)
146 rescue StandardError
=> e
147 puts
"There was an error retrieving accounts from the filesystem: #{e.to_s}"
148 Kernel
.exit(ExitCodes
::FILESYSTEM_ERROR)
151 if leftover_domains
.size
> 0 or leftover_accounts
.size
> 0
152 puts
make_header(plugin_class
.to_s())
154 leftover_domains
.each
do |domain
|
155 puts
"Found: #{domain} (#{plugin.describe_domain(domain)})"
158 leftover_accounts
.each
do |account
|
159 puts
"Found: #{account} (#{plugin.describe_account(account)})"
162 if cfg
.i_mean_business
163 # We have to delete the accounts before the domain,
164 # otherwise they'd already be gone.
165 leftover_accounts
.each
do |account
|
166 # Get the description before we delete the domain.
167 # This can still fail if the account's domain is gone.
168 account_description
= plugin
.describe_account(account
)
169 plugin
.delete_account(account
)
170 puts
"Removed: #{account} (#{account_description})"
173 leftover_domains
.each
do |domain
|
174 # Get the description before we delete the domain.
175 domain_description
= plugin
.describe_domain(domain
)
176 plugin
.delete_domain(domain
)
177 puts
"Removed: #{domain} (#{domain_description})"