Actually remove directories when I_MEAN_BUSINESS == true.
[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/dovecot_mailstore'
38 require 'src/postfixadmin_db'
39
40 dms = DovecotMailstore.new(Configuration::MAIL_ROOT)
41
42 pgadb = PostfixadminDb.new(Configuration::DBHOST,
43 Configuration::DBPORT,
44 Configuration::DBOPTS,
45 Configuration::DBTTY,
46 Configuration::DBNAME,
47 Configuration::DBUSER,
48 Configuration::DBPASS)
49
50
51 # First, we find out if any domains have been removed from the
52 # database but not from the filesystem.
53 begin
54 # Get the list of domains according to the filesystem.
55 fs_domains = dms.get_domains_from_filesystem()
56 rescue StandardError => e
57 puts "There was an error retrieving domains from the filesystem: #{e.to_s}"
58 Kernel.exit(ExitCodes::FILESYSTEM_ERROR)
59 end
60
61 begin
62 # ...and according to the Postfixadmin database.
63 db_domains = pgadb.get_domains_from_db()
64 rescue DatabaseError => e
65 puts "There was an error connecting to the database: #{e.to_s}"
66 Kernel.exit(ExitCodes::DATABASE_ERROR)
67 end
68
69
70 # Then, we get the list of accounts that have been removed. We did
71 # the domains first so that, if a domain was removed, we can avoid
72 # reporting each of its accounts individually.
73 begin
74 # Get the list of accounts according to the filesystem.
75 fs_accts = dms.get_accounts_from_filesystem(db_domains)
76 rescue StandardError => e
77 puts "There was an error retrieving accounts from the filesystem: #{e.to_s}"
78 Kernel.exit(ExitCodes::FILESYSTEM_ERROR)
79 end
80
81 begin
82 # ...and according to the Postfixadmin database.
83 db_accts = pgadb.get_accounts_from_db()
84 rescue DatabaseError => e
85 puts "There was an error connecting to the database: #{e.to_s}"
86 Kernel.exit(ExitCodes::DATABASE_ERROR)
87 end
88
89
90 # The list of domains on the filesystem that aren't in the DB.
91 dom_difference = fs_domains - db_domains
92
93 # And accounts on the filesystem that aren't in the DB and don't
94 # belong to a domain that was removed.
95 acct_difference = fs_accts - db_accts
96
97 # Don't output any unnecessary junk. Cron might mail it to someone.
98 if dom_difference.size > 0 or acct_difference.size > 0
99 # The header that we output before the list of accounts.
100 # Just the path of this script, and the current time.
101 header = "#{$0}, "
102
103 current_time = Time.now()
104 if current_time.respond_to?(:iso8601)
105 # Somehow this method is missing on some machines.
106 header += current_time.iso8601.to_s
107 else
108 # Fall back to whatever this looks like.
109 header += current_time.to_s
110 end
111
112 puts header
113 puts '-' * header.size # Underline the header.
114
115 dom_difference.each do |domain|
116 puts "Found: #{domain} (#{dms.get_domain_path(domain)})"
117 end
118
119 acct_difference.each do |account|
120 puts "Found: #{account} (#{dms.get_account_path(account)})"
121 end
122
123 if Configuration::I_MEAN_BUSINESS
124 dom_difference.each do |domain|
125 domain_path = dms.get_domain_path(domain)
126 FileUtils.rm_rf(domain_path)
127 puts "Removed: #{domain_path}"
128 end
129
130 acct_difference.each do |account|
131 account_path = dms.get_account_path(account)
132 FileUtils.rm_rf(account_path)
133 puts "Removed: #{account_path}"
134 end
135 end
136
137 puts ""
138 end