captainarkdotnet/content/postfixadmin.md

5.0 KiB

Title: Postfix Admin Date: 2016-03-06 Category: Tutorial

As I explained in this previous tutorial, I've been running my own mail server without any issue for some time now.

However, every time I've wanted to add a domain, create a new mailbox or change a user's password, I've had to do it manually from a SQL shell. As fun as it may be, it does get old very fast, so I've decided to install a web frontend to manage this database.

After a bit a googling, I've settled on Postfix Admin.

The latest stable version of Postfix Admin was released in 2009. Version 3.0 has been in the works for some time now and the project can be cloned from their Github repo.

I've also tried ViMbAdmin, but it felt a little heavy considering what I was going to use it for.

You'll need a web server with PHP support to run Postfix Admin. I personnaly run nginx with php5-fpm, but I won't explain how to configure it here. I'll simply explain how to migrate your current database to one managed with Postfix Admin with as little downtime as possible.

Creating a new database

Since the database managed by Postfix Admin does not use the same schema as the one we've created in my previous tutorial, we'll have to create a new one. We will give all privileges on that database to the same user as before, 'mail'@'localhost'.

CREATE DATABASE mailnew;
GRANT SELECT ON mailnew.* TO 'mail'@'localhost';
FLUSH PRIVILEGES;

At this point, you can clone the Postfix Admin project from Github and go through the installation process.

While editing the config.inc.php file (or config.local.php file if you've decided to copy it), make sure that the database_name option is set to use the mailnew database we've just created.

Also, make sure that the encrypt option is set to dovecot:SHA512-CRYPT.

The installation process will create all the necessary tables in the database.

At this point, you'll have to recreate all domains, mailboxes and aliases that you have configured in your current mail database using the Postfix Admin interface.

Postfix configuration

Once you're done with Postfix Admin, it's time to configure Postfix to use its schema.

First thing first, let's backup our current configuration :

mkdir /etc/postfix/mysql-backup
cp -a /etc/postfix/mysql-virtual* /etc/postfix/mysql-backup/

Next, we have to edit the 3 files we've just backuped. The only line that actually changes is the one beginning with query.

The first file is /etc/postfix/mysql-virtual-mailbox-domains.cf :

user = mail
password = mailpassword
hosts = 127.0.0.1
dbname = mail
query = SELECT 1 FROM domain WHERE domain='%s' AND active='1'

The second one is /etc/postfix/mysql-virtual-mailbox-maps.cf :

user = mail
password = mailpassword
hosts = 127.0.0.1
dbname = mail
query = SELECT 1 FROM mailbox WHERE username='%s' AND active='1'

And the last one is /etc/postfix/mysql-virtual-alias-maps.cf :

user = mail
password = mailpassword
hosts = 127.0.0.1
dbname = mail
query = SELECT goto FROM alias WHERE address='%s' AND active='1'

Dovecot configuration

Same as with Postfix, we now need to configure Dovecot to use the Postfix Admin schema.

First, let's backup our current configuration :

cp -a /etc/dovecot/sql.conf /etc/dovecot/sql.conf.bak

Next, we have to edit the /etc/dovecot/sql.conf file. The only line that changes is the one beginning with password_query.

driver = mysql
connect = host=localhost dbname=mail user=mail password=mailpassword
default_pass_scheme = SHA512-CRYPT
password_query = SELECT username as user, password FROM mailbox WHERE username='%u' AND active='1';

Migrating to the new schema

We're done with the configuration part. Time to migrate to the new schema.

First, let's create a backup of our current mail database :

mysqldump mail | bzip2 > /home/user/mail.sql.bz2

Next, in a SQL shell, we're going to drop and recreate the mail database :

DROP DATABASE mail;
CREATE DATABASE mail;

We now have to dump the contents of the mailnew database into the newly created mail database :

mysqldump mailnew | mysql mail

The last thing we have to do is to restart Postfix and Dovecot so that it starts using the new database schema :

systemctl restart postfix
systemctl restart dovecot

At this point, Postfix and Dovecot are using the Postfix Admin schema in the mail database.

The last thing we have to do is to edit Postfix Admin's config.inc.php file to use the mail database as well instead of the mailnew database that it should be currently using.

Cleanup

Once you've confirmed that everything is working properly, you can delete the backup files we've created :

rm -rf /etc/postfix/mysql-backup
rm /etc/dovecot/sql.conf.bak 

You can drop the mailnew database as well :

DROP DATABASE mailnew;

Conclusion

That's all ! As always, please do leave a comment if this article has been of any use to you !