]> gitweb.michael.orlitzky.com - mailshears.git/blob - bin/mailshears
Handle the case where an account's domain has already been deleted (and thus the...
[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 # 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).
19 require 'pathname'
20
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) + '/../'
27
28 # Needed for rm_rf.
29 require 'fileutils'
30
31 # Load our config file.
32 require 'bin/configuration'
33
34 # And the necessary classes.
35 require 'src/errors.rb'
36 require 'src/exit_codes.rb'
37 require 'src/postfixadmin_db'
38
39 Configuration::PLUGINS.each do |plugin_file|
40 require "src/plugins/#{plugin_file}"
41 end
42
43 pgadb = PostfixadminDb.new(Configuration::DBHOST,
44 Configuration::DBPORT,
45 Configuration::DBOPTS,
46 Configuration::DBTTY,
47 Configuration::DBNAME,
48 Configuration::DBUSER,
49 Configuration::DBPASS)
50
51
52 begin
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)
58 end
59
60 begin
61 # And the accounts.
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)
66 end
67
68
69 Plugin.includers.each do |plugin_class|
70 plugin = plugin_class.new()
71
72 begin
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)
77 end
78
79 begin
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)
84 end
85
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.
89 header = "#{$0}, "
90
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
95 else
96 # Fall back to whatever this looks like.
97 header += current_time.to_s + "\n"
98 end
99
100 header += 'Plugin: ' + plugin_class.to_s + "\n"
101 puts header
102 puts '-' * header.size # Underline the header.
103
104 leftover_domains.each do |domain|
105 puts "Found: #{domain} (#{plugin.describe_domain(domain)})"
106 end
107
108 leftover_accounts.each do |account|
109 puts "Found: #{account} (#{plugin.describe_account(account)})"
110 end
111
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)})"
116 end
117
118 leftover_accounts.each do |account|
119 begin
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)})"
126 end
127 end
128 end
129
130 puts ""
131 end
132
133 end