I needed to authenticate a website for a school against an Active Directory server today. I found the job surprisingly easy.

My first attempt was using a pam smb module, and an apache pam module. This worked well, but had a couple of flaws:

  • You could only have one /etc/pam.d/ file for apache, so if you wanted different styles of authentication you are out of luck. doh.
  • the pam smb module doesn't support groups, so I couldn't have an area just for Teachers only.

Oh well, scrapped that idea.

I looked at the smb modules for apache. This was a port of the pam_smb module to the apache api, didn't really gain me much, except it removed the limitation on one /etc/pam.d/ file for apache. Not that this really was much of a problem if you didn't have group support.

my final approach to the problem was an ldap authentication module for apache. This hit the nail on the head.

The major stumbling block I had was trying to find out the BaseDN. If you bind anonymously you can't search or anything useful. To bind as someone useful, you have to know their dn, including the BaseDN. Turns out the BaseDN was the name of the 'domain' with dc1?'s inserted. so if your domain is '', your baseDN is dc=example,dc=com. I'm not sure if this can be configured to be something else.

I had to learn how to write ldap url's, but it was dead basic, the configuration I used was

<Directory /var/www/staff>

AuthLDAPURL ldap://,OU=Teachers,DC=example,DC=com?sAMAccountName?sub?(objectClass=user) AuthLDAPBindDN cn=user,cn=Users,dc=example,dc=com AuthLDAPBindPassword password-here

AuthType? Basic

AuthName? "Mumble School Intranet"

require valid-user


/var/www/staff should be the path that you want to secure. should be the hostname of your ads server, I suspect you can use something like here, but I didn't experiment, comments anyone?

user should be some user which has read privilege to the directory (which is likely to be a full name with a space, so you may need to put double quotes around the entire string following AuthLDAPBindDN)

password-here should be users password

and voila! It worked.

you can test the ldap stuff from active directory by using
ldapsearch -x -b basedn -D cn=user,cn=Users,dc=example,dc=com -W
use simple auth. Never got the non-simple auth to ever work
use this base DN
use this as the dn to bind as
prompt for the binddn's password
the URL is
ldap://hostname:389/search basedn?username attribute?search type?search filter


is the hostname of the ldap server
search basedn
is the root of the search you're going to do.
username attribute
is the name of the attribute for the username
search type
is 'sub' for subtree
search filter
is a filter to apply, we only want to return user's (since we don't want people doing something silly like authenticating as a printer or a domain name)

the sAMAccountName is the ldap attribute ActiveDirectory uses for storing the username.


Note that Windows doesn't provide any really useful ways of seeing the LDAP structure, and thus knowing how to construct your LDAP queries can be tricky. There is a very helpful tool called "Ldp.exe" in the Windows Support Tools package (see;en-us;301423 - and note that there is an article for win2k3 also). Using this tool can be fairly intimidating. See:;EN-US;224543 for an overview.

1?: Domain Component

Round Two!

Going back to this, we found a problem. This appeared in apache's error log
36:07 2003? [error? [client Too many failures connecting to LDAP server

This is caused by mod_ldap trying to use LDAP v2. This appears to cause some (unspecified) problems with Active Directory. So I found a patch by Jeff Costlow (j.costlow at (may whatever deity he worships provide him with many years of good health and fortune) which allows you to force LDAP version 3.

This prevents the error above from occuring, and now only authorised people can login.

There is a nasty security flaw in mod_ldap that while that error is appearing in your logs you can login as any user (even if that user doesn't exist). This is because when that error occurs mod_ldap fails and returns 0. However 0 turns out to be "success" instead of "failure". Ooops!

Changing passwords

The following was posted to the NT BugTraq list (don't ask), and I thought it may be appropriate.

Here is the code I use to change a password in Active Directory. It uses the perl-ldap module from

Ther perl-ldap FAQ provides examples of Active Directory password resets, and normal LDAP password resets: (or perldoc Net::LDAP::FAQ )

It is part of a bigger program that we use to synchronize our LDAP to Active Directory. (one way synchronization from LDAP to AD, except for passwords which are two-way) I have reduced it down to a simple command line program that reads name & password from stdin. It does a very simplistic unicode conversion that will fail for non-ascii characters. Feel free to use it for whatever you like.

% jim supersecret
  1. /usr/local/bin/perl -w

use strict; use Net::LDAPS;

my($Ad, $mesg, $uid, $pass, $npass, $dn, $rtn);

($uid, $pass) = split(" ",<STDIN>);

if (($uid eq "") or ($pass eq "")) {

print "Uid and/or password missing in input\n"; exit 1;


print "Trying to set $uid to password $pass\n";

  1. Bind to the AD server

$Ad = Net::LDAPS->new("", version =3)

or print "Unable to connect to AD server\n", exit 2;

$Ad->bind(dn ="CN=ad,OU=Admin,DC=test,DC=uva,DC=nl", password => "gandalf")

or print "Unable to bind to AD server\n", exit 2;

  1. Do a AD lookup to get the dn for this user
  2. then change their password.

$mesg = $Ad->search(base "DC=s-res,DC=uva,DC=nl", filter ="cn=$uid"); if($mesg->count ! 1) {

print "AD lookup failed for user $uid\n"; exit 3;


  1. Add quotes and uniCode

map { $npass .= "$_\000" } split(//, "\"$pass\"");

  1. Now change it

$dn = $mesg->entry(0)->dn;

$rtn = $Ad->modify($dn, replace { "unicodePwd" =$npass }); if($rtn->{'resultCode'} ! 0) {

print "User $uid, setting password failed\n"; exit 2;


print "Password for $uid changed in AD\n"; exit 0;

Another good method for authentication with apache is to use one of the webISO's (web initial sign on) see . In particular the webISO provided by is flexible and can be used with IIS aswell. It can either authenticate against LDAP, kerberos, unix password file (/etc/shadow), or pam modules. It give single sign on to web apps.


The following authors of this page have not agreed to the WlugWikiLicense. As such copyright to all content on this page is retained by the original authors.
  • BrianCain
  • CalRacey
The following authors of this page have agreed to the WlugWikiLicense.

lib/plugin/WlugLicense.php:99: Warning: Invalid argument supplied for foreach()

lib/plugin/WlugLicense.php:111: Notice: Undefined variable: ignore_authors

lib/plugin/WlugLicense.php:111: Notice: Undefined variable: ignore_authors

lib/plugin/WlugLicense.php:111: Notice: Undefined variable: ignore_authors