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