![]() | ||
SMTP Authentication with Postfix using files or MySQLThere are times when you need to have users authenticate their SMTP sessions. Perhaps you have roaming users and you don't want to be an open relay, but you cannot predict where these users are. You need a way for them to say to your SMTP server "hey I belong here, let me send email". One way to do is is using SMTP Authentication. The user's username and password are sent to the SMTP server. The server then checks the pair is correct and lets the user then send mail (or not if they are incorrect). SMTP Authentication is defined in RFC2554. Postfix has a method of authentication, but it is tied up with SASL. For file-based authentication you just create a special password database. However for other types you cannot simply make a LDAP or MySQL table and be done with it. You can either use SASL natively or do it the way I have implemented it here where Postfix uses SASL which uses PAM which uses MySQL; a round-about way but it does work. There is some sporadic documentation about this around The Internet, but I wrote this up in the hope you find it useful and so I don't have to remember it or relearn it all over again. You might also be able to adapt this method to use other sorts of PAM authentication. For example I'm pretty sure this method with a little adaption would also work for LDAP authentication. Obviously you could use other databases other than MySQL, its just what I was using here. Required PackagesThe following Debian packages are required to get this all working. I'm using Debian Sarge here but for the most part it should work for other versions and dists with some small changes. Some other packages will be needed, but will be pulled in as dependencies.
You have to make sure that either one or both of the authentication modules packages are installed. If you don't and you setup Postfix to use SASL (see below) then the stupid process will be throttled. For older distributions you may need the libsasl (no 2) packages. Postfix and MySQL socket problemPostfix runs the smtpd daemon in a chrooted environment, usually something like /var/spool/postfix. That means that as far as the smtpd process is concerned you have nothing above that point. MySQL has a socket sitting in another directory, something like /var/run/mysql/mysqld.sock. The problem is that the socket sits in an area that smtpd believes doesn't exist and cannot get to anyway because of the chroot. To get around this problem, you have 3 options:
Stopping smtpd from being in a chrootThis had me going for a long, long time. To change this, edit /etc/postfix/master.cf and change the following line: smtp inet n - n - - smtpd The second 'n' means it is not chrooted. There may be a way of running smtpd in a chroot with the SASL and MySQL authentication but I'm not sure how. Postfix ChangesThe following lines are added to /etc/postfix/main.cf
smtpd_sasl_auth_enable = yes SASL Files SetupSo far the postfix server knows it has to use SASL if it gets an authentication request. The default way for SASL to work out if you are authenticated is for it to examine a Berkley DB file called /etc/sasldb2. You can add and change users using the saslpasswd2 program. The problem here is if you run smtpd in a chroot environment then it will not find the sasldb file. If you try to authenticate postfix will give an error "warning: SASL authentication problem: unable to open Berkeley db /etc/sasldb2: No such file or directory". The problem here is that you have a /etc/sasldb2 file, but postfix is looking for a /var/spool/postfix/etc/sasldb2 file. The two solutions for this problem are to either not run postfix in a chroot environment (see a previous section on how to stop it) or get that sasldb2 file into the correct directory. You can put it right by copying it. You will also need to make sure the user that smtpd runs as can read the file. Debian users can automatically get this file updated by editing /etc/init.d/postfix. Around line 43 there is a list of files that are copied from their real directories into the chroot. Change the line so it looks like: FILES="etc/localtime etc/services etc/resolv.conf etc/hosts etc/nsswitch.conf etc/sasldb2" Now when postfix is restarted you have the new sasldb2 ready to go. If you are doing file-based authentication then you are done, drop down to the Testing section. MySQL SASL SetupFor MySQL authentication, the next step is to get SASL to ask PAM to authenticate the user. There's some confusion because the location of this file has moved around. On my system with the versions of the packages given above, it is found at /etc/postfix/sasl/smtpd.conf but it also has been found in /usr/local/lib/sasl/smtpd.conf and /usr/lib/sasl/smtp.conf. The file is real simple one-liner: pwcheck_method: pam That's it for SASL, it will then use standard PAM as we all know and love for authenticating. PAM SetupThe PAM setup is pretty standard. All you need to know is the PAM service is called smtp, so you need to create a file /etc/pam.d/smtp. SASL only uses the authentication management group. It might be useful to test how things are going so far. To do this, and only for testing, you can use the pam_permit module. This module permits anything you send, so its useful for testing or for some strange circumstances, but shouldn't be used in a production environment. The file /etc/pam.d/smtp would then look like: auth required pam_permit.so If you are going to run it with MySQL, use a configuration similar to that shown below. The configuration is similar to a user doing the following:
server$ mysql -u postfix -psecret postfixdb auth required pam_mysql.so user=postfix passwd=secret db=postfixdb table=users usercolumn=id passwdcolumn=password crypt=0 The table users has two columns. The first is called id and has the username, the second is password it has the unencrypted password in it. A select is made checking both username and password. If there is a single row returned, authentication is successful. TestingI use the plain authentication method for testing. To do this you need to convert the username and password into a base64 encoded string. For example, if you have username user and password pass, you would type:
server$ printf 'user\0user\0pass' | mimencode So the string is the username and password joined together with \0 between them. The username is needed twice. To test it, telnet to the SMTP port of your server and type the auth commands.
server$ telnet mail.my.server 25 I've used a EHLO instead of the normal HELO as this is an extended hello, so the server gives you a list of things it can do. Notice that there are two AUTH lines, this is due to the broken_sasl_auth_clients line in /etc/postfix/main.cf. You may have different authentication modules, it depends on what packages you have installed. The important thing is the server's response to your commands is 235 Authentication successful. This means that it recognizes the username and password. If it doesn't, it returns a 535 Error: authentication failed. If you get a failed message, check the mail logs. The logs should tell you why the authentication failed. Instead of using the plain authentication, you might want to use the LOGIN method. Once again mimencode is used to get the base64 encoding:
server$ printf 'user' | mimencode You now have the two base64 encoded strings, to test this method is very similar to the PLAIN method.
server$ telnet 10.1.2.3 25 You might wonder what that strange text is after the 334 numbers. Once again mimencode can help. It's a base64 encoding of the response from the mail server.
server$ printf 'VXNlcm5hbWU6' | mimencode -u ; echo So the mail server is asking for a username and password, in base64. I don't know why they bother to do this as it doesn't make it that much more secure but at least you now know what it is. Client ConfigurationOK, so you have you server setup that can do authentication, but now you want your laptop that is running Postfix to relay all email through your server. This section describes the client setup. Postfix SetupSetting up Postfix is pretty simple. Tell Postfix to send all email to your mail server and enable SASL. The file /etc/postfix/main.cf requires the following lines:
relayhost = mail.example.net The configuration is telling postfix to send all email to mail.example.net, use SASL authentication and that the passwords are found in a particular file. Remember for outgoing mail Postfix uses smtp while incoming uses smtpd. As the client sends email the configuration lines have the "d less" smtp_ keywords. Client Password fileThe format of the client password file is simple, especially if you have written hash tables for Postfix before. The key is the remote server and the value is the username and password to use for that server separated by a colon. mail.example.net myuser:secpasswd | ||