]>
gitweb.michael.orlitzky.com - mailshears.git/blob - bin/mailshears
3 # mailshears, to prune unused mail directories.
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.
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.
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.
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
) +
'/../'
28 # Load our config file.
29 require 'bin/configuration'
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'
37 dms
= DovecotMailstore
.new(Configuration
::MAIL_ROOT)
39 pgadb
= PostfixadminDb
.new(Configuration
::DBHOST,
40 Configuration
::DBPORT,
41 Configuration
::DBOPTS,
43 Configuration
::DBNAME,
44 Configuration
::DBUSER,
45 Configuration
::DBPASS)
48 # First, we find out if any domains have been removed from the
49 # database but not from the filesystem.
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)
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)
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.
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)
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)
87 # The list of domains on the filesystem that aren't in the DB.
88 dom_difference
= fs_domains
- db_domains
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
94 # Don't output any unnecessary junk. Cron might mail it to someone.
95 if dom_difference
.size
> 0 or acct_difference
.size
> 0
97 # The header that we output before the list of accounts.
98 # Just the path of this script, and the current time.
101 current_time
= Time
.now()
102 if current_time
.respond_to
?(:iso8601)
103 # Somehow this method is missing on some machines.
104 header +
= current_time
.iso8601
.to_s
106 # Fall back to whatever this looks like.
107 header +
= current_time
.to_s
111 puts
'-' * header
.size
# Underline the header.