Load the default configuration file in lib/mailshears.rb.
[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 require 'mailshears'
17
18 pgadb = PostfixadminDb.new(Configuration::DBHOST,
19 Configuration::DBPORT,
20 Configuration::DBOPTS,
21 Configuration::DBTTY,
22 Configuration::DBNAME,
23 Configuration::DBUSER,
24 Configuration::DBPASS)
25
26
27 begin
28 # Get a list of domains from the Postfixadmin database.
29 db_domains = pgadb.get_domains_from_db()
30 rescue DatabaseError => e
31 puts "There was an error connecting to the database: #{e.to_s}"
32 Kernel.exit(ExitCodes::DATABASE_ERROR)
33 end
34
35 begin
36 # And the accounts.
37 db_accounts = pgadb.get_accounts_from_db()
38 rescue DatabaseError => e
39 puts "There was an error connecting to the database: #{e.to_s}"
40 Kernel.exit(ExitCodes::DATABASE_ERROR)
41 end
42
43
44 Plugin.includers.each do |plugin_class|
45 plugin = plugin_class.new()
46
47 begin
48 leftover_domains = plugin.get_leftover_domains(db_domains)
49 rescue StandardError => e
50 puts "There was an error retrieving domains from the filesystem: #{e.to_s}"
51 Kernel.exit(ExitCodes::FILESYSTEM_ERROR)
52 end
53
54 begin
55 leftover_accounts = plugin.get_leftover_accounts(db_accounts)
56 rescue StandardError => e
57 puts "There was an error retrieving accounts from the filesystem: #{e.to_s}"
58 Kernel.exit(ExitCodes::FILESYSTEM_ERROR)
59 end
60
61 if leftover_domains.size > 0 or leftover_accounts.size > 0
62 # The header that we output before the list of domains/accounts.
63 # Just the path of this script, the current time, and the plugin name.
64 header = "#{$0}, "
65
66 current_time = Time.now()
67 if current_time.respond_to?(:iso8601)
68 # Somehow this method is missing on some machines.
69 header += current_time.iso8601.to_s
70 else
71 # Fall back to whatever this looks like.
72 header += current_time.to_s + "\n"
73 end
74
75 header += 'Plugin: ' + plugin_class.to_s + "\n"
76 puts header
77 puts '-' * header.size # Underline the header.
78
79 leftover_domains.each do |domain|
80 puts "Found: #{domain} (#{plugin.describe_domain(domain)})"
81 end
82
83 leftover_accounts.each do |account|
84 puts "Found: #{account} (#{plugin.describe_account(account)})"
85 end
86
87 if Configuration::I_MEAN_BUSINESS
88 # We have to delete the accounts before the domain,
89 # otherwise they'd already be gone.
90 leftover_accounts.each do |account|
91 # Get the description before we delete the domain.
92 # This can still fail if the account's domain is gone.
93 account_description = plugin.describe_account(account)
94 plugin.delete_account(account)
95 puts "Removed: #{account} (#{account_description})"
96 end
97
98 leftover_domains.each do |domain|
99 # Get the description before we delete the domain.
100 domain_description = plugin.describe_domain(domain)
101 plugin.delete_domain(domain)
102 puts "Removed: #{domain} (#{domain_description})"
103 end
104 end
105
106 puts ""
107 end
108
109 end