POSTFIX, authentification SASL et MySQL
POSTFIX, authentification SASL et MySQL
enabling SMTP-AUTH on Postfix using the latest Debian packages.
Scenario
You are running Debian etch 4.0 or Lenny.
Your mail server is hosting multiple domains as described in http://www.postfix.org/VIRTUAL_READ....
You are using MySQL as a backend for user authentication as described in http://www.postfix.org/MYSQL_README.html.
Your users can authenticate on your pop3/imap server as :
user test@test.com
pass test123 You want to allow them to authenticate with SMTP-AUTH using the very same credentials.
The passwords of your users’ pop3/imap accounts are stored in the database in encrypted form (md5 in this example). You want to authenticate on a secure channel (TLS).
You want to run Postfix’s "smtp" service chroot’ed, i.e. you have a line like this in /etc/postfix/master.cf :
smtp inet n - - - - smtpd What you need :
Install the following packages :
apt-get install postfix-tls sasl2-bin libsasl2 libsasl2-modules libpam-mysql opensslnote :
postfix-tls doesnt exist anymore in debian 4.0 and Testing. Package Postifx is sufficient.
libsasl2 is now libsasl2-2
How to set up the whole thing :
Create the file /etc/pam.d/smtp with the following content :
Change "yourpass" to match your grant table. Create /etc/postfix/sasl/smtpd.conf with the following content :
Edit the file /etc/default/saslauthd like this :
Now we’ll have to make the directory we just added in the previous step, chown it so Postfix can use it, and add the Postfix user to the sasl group.
mkdir /var/spool/postfix/var/
mkdir /var/spool/postfix/var/run/
mkdir /var/spool/postfix/var/run/saslauthd
chown -R root:sasl /var/spool/postfix/var/
chmod 710 /var/spool/postfix/var/run/saslauthd
adduser postfix saslcreate a link to keep everybody happy :
# ln -s /var/spool/postfix/var/run/saslauthd /var/run/saslauthdAdd the following lines to /etc/postfix/main.cf :
Also remember to add "permit_sasl_authenticated" under "smtpd_recipient_restrictions" as follow :
.....
permit_mynetworks,
permit_sasl_authenticated,
reject_unauth_destination,
......Open /etc/init.d/postfix, search for the FILES variable and add etc/postfix/sasl/smtpd.conf to the list :
FILES="etc/localtime etc/services etc/resolv.conf etc/hosts \ etc/nsswitch.conf etc/nss_mdns.config etc/postfix/sasl/smtpd.conf"Restart Postfix and start saslauthd :
# /etc/init.d/postfix restart
# /etc/init.d/saslauthd startverifier que les parametres sont bien passés au demon saslauthd :
# ps waux | grep saslauthdEnable TLS
on prendra comme exemple le domaine starbridge.org
Pour un serveur en production, il serait préférable d’utiliser un véritable certificat fourni et signé par une autorité de certification de confiance. (payant).
On édite la configuration de ssl pour pouvoir signer des certificats sur 10 ans, au lieu d’1 an par défaut :
vi /etc/ssl/openssl.cnf
on change la ligne default_days en
On crée le Certificat Racine :
cd ~
/usr/lib/ssl/misc/CA.pl -newcaon entre les parametres requis, on choisis un pass phrase de son choix et on laisse "challenge password" vide.
Ce certificat racine sert à signer les certificats. Il est localisé dans le répertoire /demoCA.
On crée maintenant une clé privée pour le serveur ainsi qu’un certificat public non signé.
mkdir ~/CERT
cd ~/CERT
openssl req -new -nodes -keyout starbridge-key.pem -out starbridge-req.pem -days 3650et on entre les parametres comme ci dessous :
Note : le paramètre le plus important est le Common Name qui doit être le meme que le nom avec lequel se connecte les clients sur le serveur. Ici j’ai pris le nom de domaine du serveur de mail : starbridge.org mais cela peut etre le FQDN : spike.starbridge.org.
On signe maintenant ce certificat public avec le certificat racine :
cd ~
openssl ca -out CERT/starbridge-cert.pem -infiles CERT/starbridge-req.pemVoici la sortie de la signature :
On copie maintenant le certificat et la clé dans postfix :
mkdir /etc/postfix/tls
cp demoCA/cacert.pem CERT/starbridge-key.pem CERT/starbridge-cert.pem /etc/postfix/tls/
chmod 644 /etc/postfix/tls/starbridge-cert.pem /etc/postfix/tls/cacert.pem
chmod 400 /etc/postfix/tls/starbridge-key.pem
chmod 400 ~/CERT/*On ajoute ceci au /etc/postfix/main.cf :
On redémarre Postfix :
/etc/init.d/postfix restartOn vérifie le fonctionnement depuis un client mail configuré pour l’authentification SASL sur un chiffrement TLS avec les mêmes identifiants que pour la connexion IMAP (ne pas oublier le @starbridge.org).
Pour le type d’authentication, il faut sélectionner "en clair" (le terme dépend du client mail).
C’est le chiffrage de la connexion par le TLS qui sécurisera le transfert du password.
C’est pour cela qu’il ne faut pas dissocier TLS et authentification.
Random thoughts
Q. : Can Postfix query the MySQL db directly ? A. : No.
Q. : Why do you use libpam-mysql ? saslauthd natively supports SQL. A. : Because saslauthd only supports unencrypted password if you use a sql db as an authentication backend. That’s the reason for interfacing saslauthd with PAM. PAM, in turn, can use just anything.
Q. : My friend told me that /etc/postfix/sasl/smtpd.conf should contain
"pwcheck_method : pam" A. : That was true for SASL < 2.x. Now you have to use saslauthd.
Q. : Why do you run saslauthd with the -r flag ? A. : Because my users authenticate as "user@domain", not "user". If you are in trouble check /var/log/auth.log .
Q. : Why did you move saslauthd’s socket to
/var/spool/postfix/var/run/saslauthd ?
A. : Because the smtp service runs chroot’ed.
Q. : Why did you add etc/postfix/sasl/smtpd.conf to the FILES variable ? A. : Because Postfix needs to access that file from inside the chroot. The init.d script copies the latest copy of that file inside the chroot at every restart.
Q. : How does the authentication chain work ? A. : Postfix connects to saslauthd via socket, which in turn asks PAM to authenticate the user which in turn queries the relevant MySQL table.
Q. : Are there any alternatives to libpam-mysql ? A. : Perhaps it’s possible to use authdaemon from the Courier package.
Q. : Why do you use 127.0.0.1 instead of localhost ?
A. : In order to use a TCP socket instead of a unix socket. This way we don’t have to put MySQL’s unix socket inside Postfix’s chroot.
article original : http://www.nervous.it/txt/Postfix-S... adapté pour la debian 4.0 par tonio


