Add the GPL3 'LICENSE' file.
[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 # Needed for rm_rf.
17 require 'fileutils'
18
19 # Load our config file.
20 require 'configuration'
21
22 # And the necessary classes.
23 require 'errors.rb'
24 require 'exit_codes.rb'
25 require 'postfixadmin_db'
26
27 Configuration::PLUGINS.each do |plugin_file|
28 require "plugins/#{plugin_file}"
29 end
30
31 pgadb = PostfixadminDb.new(Configuration::DBHOST,
32 Configuration::DBPORT,
33 Configuration::DBOPTS,
34 Configuration::DBTTY,
35 Configuration::DBNAME,
36 Configuration::DBUSER,
37 Configuration::DBPASS)
38
39
40 begin
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)
46 end
47
48 begin
49 # And the accounts.
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)
54 end
55
56
57 Plugin.includers.each do |plugin_class|
58 plugin = plugin_class.new()
59
60 begin
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)
65 end
66
67 begin
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)
72 end
73
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.
77 header = "#{$0}, "
78
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
83 else
84 # Fall back to whatever this looks like.
85 header += current_time.to_s + "\n"
86 end
87
88 header += 'Plugin: ' + plugin_class.to_s + "\n"
89 puts header
90 puts '-' * header.size # Underline the header.
91
92 leftover_domains.each do |domain|
93 puts "Found: #{domain} (#{plugin.describe_domain(domain)})"
94 end
95
96 leftover_accounts.each do |account|
97 puts "Found: #{account} (#{plugin.describe_account(account)})"
98 end
99
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})"
109 end
110
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})"
116 end
117 end
118
119 puts ""
120 end
121
122 end