Replacing Sendmail with Postfix

Q I am running Sendmail with SendmailAnalyzer and a custom web interface, and I would like to replace the whole lot with Postfix and add ons. Can you provide me with at least a direction to follow?

A I can do one better: we will be setting up Postfix to use a database to store user credentials, in such a way that it works with Postfix Admin (http://high5.net/postfixadmin) out of the box. As you may know already, the start of the magic happens in main.cf. Let's make the app even more efficient in its lookups, and also open the door to having POP3/IMAP daemons look up password information in the database. A lot of config files use hash: argument to specify files where we have name /value pairs. We should change all the relevant entries to include a new type - mysql:. mysql: is a highly configurable driver that allows us to set up a database and its tables however you want. You can then configure it to fetch the info you need from the structure you created. It will all become clearer after we go through the first example. For now, just think of it as a pointer to a location that says, "Look up info from a MySQL database". The first line we should change in the main.cf file is the virtual_mailbox_domains line. It shouldnow read:

virtual_mailbox_domains = mysql:/etc/postfix/mysql-virtual-mailbox-domains

Now, mysql-virtual-mailbox-domains has only a suggested path; it can be wherever you want it to be. In the file mysql-virtual-mailbox-domains, we get to tell the mysql: argument where and how to get the info it needs, rather than the file itself being the repository of the info. Ah! Things are getting more interesting! The file mysql-virtual-mailbox-domains should contain at least:

hosts = unix:/var/lib/mysql/mysql.sock, 127.0.0.1
user = postfixadmin
password = p0stf1xadm1n
dbname = postfix
table = domain
select_field = description
where_field = domain

These parameters should be in any and all files that configure the mysql: argument.The hosts directive allows us to specify which database server we want to use. Here, we chose to list the Unix socket and the IP address. mysql: will try the locations we set from left to right until it is successful. The user is the database user mysql:, and should connect to the database; password is the password,; dbname is the database name; table is the database table. The other two directives require a little bit more explanation. In an attempt to do just that, let me show you how mysql: actually uses these directives to query the MySQL database. The exact query constructed by mysql: is:

SELECT select_field FROM table WHERE where_field = 'magicValue'

which translates to:

SELECT description FROM domain WHERE domain = 'magicValue'

And this query is submitted to database postfix on a database server listening on Unix socket /var/lib/mysql/mysql.sock. If that fails it will be submitted on 127.0.0.1, with user postfixadmin and password p0stf1xadm1n. description, domain and domain are values from the file above. However, in this case magicValue is the domain name from the recipient's email address of the email message that the parsing is being applied to. Let's check another directive from main.cf.

virtual_mailbox_maps = mysql:/etc/postfix/mysql-virtual-mailbox-maps

and the file mysql-virtual-mailbox-maps:

hosts = unix:/var/lib/mysql/mysql.
sock, 127.0.0.1
user = postfixadmin
password = p0stf1xadm1n
dbname = postfix
table = mailbox
select_field = maildir
where_field = username

The query in this instance would be to the same database server with the same username/password, but constructed differently, as per the file configuration:

SELECT maildir FROM mailbox WHERE username = 'magicValue'

magicValue in this instance would be the full recipient address in the email being parsed. From last month's configuration, if magicValue is account1@example com then the returned value would be example.com-dir/account1/. Don't worry if you don't understand this fully: I am replicating a setup that will work with Postfix Admin. Once that is set up, you will be able to use that interface to easily add example domains, then real domains, then check the database for how the data is laid out to make sense of it all. This is the missing part from the Postfix Admin documentation, ie INSTALL.TXT http://high5.net/postfixadmin/index.php?file=INSTALL.TXT. The rest of the main.cf entries that should be changed are

virtual_alias_maps = mysql:/etc/postfix/mysql-virtual-alias-maps

And the respective file contents in mysql-virtual-alias-maps:

hosts = unix:/var/lib/mysql/mysql.sock, 127.0.0.1
user = postfixadmin
password = p0stf1xadm1n
dbname = postfix
table = alias
select_field = goto
where_field = address

And that's it! The unchanged entries to main.cf are:

virtual_mailbox_base = /path/to/mail/root
virtual_minimum_uid = 5000
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000

One caveat though. If you get the following error once you have set everything up: 'fatal: unsupported dictionary type: mysql' then your package lacks MySQL support. On an RPM-based distribution you can edit the spec file and change

%define MYSQL 0

to:

%define MYSQL 1

then rebuild the source RPM and install. Your Postfix setup should now be compatible with Postfix Admin. I suggest that you install it, play around with it and check the database to see where all the info is going and everything will be crystal clear!

Follow us on Identi.ca or Twitter

Username:   Password:
Create Account | About TuxRadar