Accueil > HowTo Mail > POSTFIX, authentification SASL et MySQL

jeudi 11 janvier 2007, par
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_README.html.
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 saslauthdon 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.
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-SMTP-AUTH-4-DUMMIES.html
adapté pour la debian 4.0 par tonio
On peut utiliser une autre methode, tout aussi fiable et peut etre un peu plus aisée à mettre en oeuvre.
Depuis peu SASL est capable d’utiliser comme pwcheck_method le authdaemond de courier.
Il faut donc bien evidemment avoir courier installé et fonctionnel avec mysql. (comme indiqué dans le tuto du site)
On installe les paquets :
aptitude install libsasl2-2 libsasl2-modules libsasl2-modules-sql sasl2-bin
on crée le fichier /etc/postfix/sasl/smtp.conf
vi /etc/postfix/sasl/smtpd.conf
et on colle :
les mecanismes PLAIN et LOGIN se refere au pass encrypté dans la base. Mais le password sera transmis en clair sur le reseau. C’est pour cela que nous imposons le TLS pour le SASL dans le tuto.
Ici on ajoute le CRAM-MD5 qui permet de crypter le password sasl sur le reseau. Il se refere au champs clear dans la base.
Celui ci n’existe pas dans la structure du tuto sur postfix.
On peut le creer si on veut pouvoir renseigner un mot de passe en clair dans la base.
la transmission par sasl sera elle cryptée, c’est l’avantage de cette methode.
mais l’inconvenient est de stocké le password en clair dans la base.
Dans cette methodde, le demon saslauthd est inutile, il ne faudra donc pas le demarrer.
modifer le /etc/default/saslauthd et changer le START=YES en START=NO.
On cree le lien vers socket de courier-auth pour que postfix puisse communiquer avec :
mkdir -p /var/spool/postfix/var/run/courier/authdaemon
ln /var/run/courier/authdaemon/socket /var/spool/postfix/var/run/courier/authdaemon/socket
chown -R daemon:daemon /var/spool/postfix/var/run/courier
chmod 775 /var/run/courier/authdaemon On edite le fichier /etc/init.d/postfix
et juste apres : # Make sure that the chroot environment is set up correctly.
on colle :
Voila il reste juste a parametrer le main.cf de postfix comme indiqué dans la methode 1
puis on relance postfix :
/etc/init.d/postfix/restart
Cet outil permet de réunirles informations nécessaires pour aider à résoudre les problemes liés a SASL.
wget http://postfix.state-of-mind.de/patrick.koetter/saslfinger/saslfinger-1.0.5.tar.gz
tar xvzf saslfinger-1.0.5.tar.gz
cd saslfinger-1.0.5
./install.shPour tester la partie serveur :
saslfinger -s
pour la partie cliente :
saslfinger -c
pour tester l’authentification au niveau du demon saslauthd (donc sans impliquer postfix) :
testsaslauthd -u user@starbridge.org -p secret -f /var/spool/postfix/var/run/saslauthd/mux -s smtp
(le user est donc user@starbridge.org et son password est "secret", comme dans le tuto du site)
S’authentifier en telnet : (donc cette fois on teste par postfix)
générer un password à soumettre :
perl -MMIME::Base64 -e 'print encode_base64("user\@starbridge\.org\0user\@starbridge\.org\0secret");'cela donne
puis
telnet 127.0.0.1 25
et on entre :
le serveur renvoit des informations puis on entre :
On doit voir
235 2.0.0 Authentication successful
Pour info voici la séquence complète :
Voir en ligne : http://www.nervous.it/txt/Postfix-S...