]> gitweb.michael.orlitzky.com - mailshears.git/blob - bin/mailshears
Update TODO.
[mailshears.git] / bin / mailshears
1 #!/usr/bin/ruby -wKU
2 #
3 # mailshears, to prune unused mail directories.
4 #
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.
10 #
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.
14 #
15
16 # Define a usage string using the program name.
17 exe = File.basename($PROGRAM_NAME)
18 usage = "#{exe} [prune | rm <target> | mv <src> <dst>]"
19
20 # Defaults
21 mode_name = 'prune'
22 mode = :prune
23
24 # Now, if a mode was supplied, it should be in ARGV[0].
25 if ARGV.length() > 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()
30 end
31 end
32
33 if mode_name == 'rm' then
34 mode = :rm
35 elsif mode_name == 'mv' then
36 mode = :mv
37 end
38
39 # Need this before referencing ExitCodes.
40 require 'mailshears'
41
42 # Since we removed both the executable name and the mode name (if it
43 # existed) from ARGV, what remains should be the required
44 # arguments.
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)
51 end
52
53 cfg = Configuration.new()
54
55 # Load each of the plugins that we'll need.
56 cfg.plugins.each do |plugin_file|
57 require "#{mode_name}/plugins/#{plugin_file}"
58 end
59
60 # And the runners.
61 require "#{mode_name}/#{mode_name}_runner"
62 require "#{mode_name}/#{mode_name}_dummy_runner"
63
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)
68 header = "#{exe}, "
69
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
74 else
75 # Fall back to whatever this looks like.
76 header += current_time.to_s
77 end
78
79 header += ' (Plugin: ' + plugin_name + ")\n"
80 header += '-' * header.size # Underline the header.
81
82 return header
83 end
84
85
86 plugin_class = nil
87 runner_class = nil
88 dummy_runner_class = nil
89
90 if mode == :rm then
91 plugin_class = RmPlugin
92 runner_class = RmRunner
93 dummy_runner_class = RmDummyRunner
94 elsif mode == :mv then
95 plugin_class = MvPlugin
96 runner_class = MvRunner
97 dummy_runner_class = MvDummyRunner
98 else
99 # Safe, catch-all default
100 plugin_class = PrunePlugin
101 runner_class = PruneRunner
102 dummy_runner_class = PruneDummyRunner
103 end
104
105 puts make_header(plugin_class.to_s())
106
107 plugin_class.includers.each do |plugin_class_includer|
108 plugin = plugin_class_includer.new()
109
110 if cfg.i_mean_business then
111 runner = runner_class.new()
112 else
113 runner = dummy_runner_class.new()
114 end
115
116 # The splat passes the correct (we hope) number of arguments to the
117 # appropriate runner. The Rm(Dummy)Runner have splats on their
118 # *target arguments as well, to turn ARGV back into an array.
119 runner.run(plugin, *ARGV)
120
121 puts ""
122 end
123
124
125
126 Kernel.exit(0)
127
128
129 begin
130 # Get a list of domains from the Postfixadmin database.
131 db_domains = pgadb.get_domains_from_db()
132 rescue DatabaseError => e
133 puts "There was an error connecting to the database: #{e.to_s}"
134 Kernel.exit(ExitCodes::DATABASE_ERROR)
135 end
136
137 begin
138 # And the accounts.
139 db_accounts = pgadb.get_accounts_from_db()
140 rescue DatabaseError => e
141 puts "There was an error connecting to the database: #{e.to_s}"
142 Kernel.exit(ExitCodes::DATABASE_ERROR)
143 end
144
145
146 Plugin.includers.each do |plugin_class_includer|
147 plugin = plugin_class_includer.new()
148
149 begin
150 leftover_domains = plugin.get_leftover_domains(db_domains)
151 rescue StandardError => e
152 puts "There was an error retrieving domains from the filesystem: #{e.to_s}"
153 Kernel.exit(ExitCodes::FILESYSTEM_ERROR)
154 end
155
156 begin
157 leftover_accounts = plugin.get_leftover_accounts(db_accounts)
158 rescue StandardError => e
159 puts "There was an error retrieving accounts from the filesystem: #{e.to_s}"
160 Kernel.exit(ExitCodes::FILESYSTEM_ERROR)
161 end
162
163 if leftover_domains.size > 0 or leftover_accounts.size > 0
164 puts make_header(plugin_class.to_s())
165
166 leftover_domains.each do |domain|
167 puts "Found: #{domain} (#{plugin.describe_domain(domain)})"
168 end
169
170 leftover_accounts.each do |account|
171 puts "Found: #{account} (#{plugin.describe_account(account)})"
172 end
173
174 if cfg.i_mean_business
175 # We have to delete the accounts before the domain,
176 # otherwise they'd already be gone.
177 leftover_accounts.each do |account|
178 # Get the description before we delete the domain.
179 # This can still fail if the account's domain is gone.
180 account_description = plugin.describe_account(account)
181 plugin.delete_account(account)
182 puts "Removed: #{account} (#{account_description})"
183 end
184
185 leftover_domains.each do |domain|
186 # Get the description before we delete the domain.
187 domain_description = plugin.describe_domain(domain)
188 plugin.delete_domain(domain)
189 puts "Removed: #{domain} (#{domain_description})"
190 end
191 end
192
193 puts ""
194 end
195
196 end