PAM/OTP authentication via Apache

Translation of the original in Catalan. Read it in: CatalàCastellano

Apache authentication known as AuthBasic despite its insecurity is one of the most used, since it allows us to protect a directory or simply a file in a simple and quick way. The protection of this type of authentication is very relative because the password travels in the clear across the network, moreover, travelling in the HTTP headers or even in the page's own URL it can end up being indexed by a browser.

Following the line of the previous articles:

Now what it is time for is to add this type of authentication to Apache so that we can give access credentials to a directory/file to someone but these will expire after some time (about 3 minutes) and search engines or other agents with more bad intentions will not be able to access the resource after that time. As always the idea is to take advantage of PAM/OTP.

In the first instance to achieve that I have tried with the libapache2-mod-auth-pam module but I could not make sense of it and I have been unable to complete the configuration. So then, what I have done is try with the libapache2-mod-authnz-external module both available in Ubuntu 8.04 (Hardy). This second module I have combined it with checkpassword-pam.

So then, the idea is quite simple, we configure Apache so that it uses libapache2-mod-authnz-external, in essence what this module does is rely on external agents to do the authentication. This external agent is the checkpassword-pam I mentioned and which as its name says what it does is validate the password against PAM, so then, although it is not a very efficient solution at the resource level since it has to load a second program to validate each user I consider that it is a sufficiently good solution for my case.

The configuration

We install the apache module and we enable it:

apt-get install libapache2-mod-authnz-external
a2enmod authnz_external
/etc/init.d/apache2 restart

Installation of checkpassword-pam

cd /var/tmp
wget "http://downloads.sourceforge.net/project/checkpasswd-pam/checkpasswd-pam/0.99/checkpassword-pam-0.99.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fcheckpasswd-pam%2F&ts=1284649515&use_mirror=ignum"
tar xvfz checkpassword-pam-0.99.tar.gz
cd checkpassword-pam-0.99
./configure
make
mkdir -p /opt/checkpassword-pam
cp checkpassword-pam /opt/checkpassword-pam

Configuration file for the apache2 service of PAM, /etc/pam.d/apache2:

auth    sufficient      pam_script.so onerr=fail dir=/etc/pam-script.d/
account  required       pam_script.so onerr=fail dir=/etc/pam-script.d/

If you look closely this file is the same as the one I used to do the authentication in the PHP and PAM/OTP article.

Now we go to one of the Apache configuration files for a virtualhost and we add the following code to protect a directory served by this virtualhost:

<Directory /var/www/virtualhost/directori_a_protegir>
  AuthType Basic
  AuthName "Restricted area for My Server"
  require user nom_usuari
  AuthBasicProvider external
  AuthExternal autenticador
</Directory>
AddExternalAuth autenticador "/opt/checkpassword-pam/checkpassword-pam -H --noenv -s php -- /bin/true"
SetExternalAuthMethod autenticador checkpassword

Important things to highlight in this configuration, note that I need to add 'require user nom_usuari', that is because I do not have any user file where Apache can validate which are my valid users and the 'require valid-users' directive would not work, so then, we have to specify which users will be able to login through this command, or else, add another directive that allows Apache to find a list of users somewhere.

Another important detail are the parameters I have to use in checkpasswords-pam so that it works well:

  • -H it does not try to do a 'chdir-home' since perhaps the user does not have this 'home directory'.

  • --noenv checkpassword-pam looks for certain environment variables in order to work, with this parameter it does not look for them and takes what it needs from stdin.

  • -s apache2 it specifies the name of the service that it will look for inside /etc/pam.d/apache2

  • -- /bin/true it simply serves so that checkpassword-pam does nothing after validating a user, since we could run some program if we wanted to after doing the validation.

References