The auth_ldap Module for Apache

This is an authentication module for Apache that allows Apache to authenticate HTTP clients using user entries in an LDAP directory. The current stable version is 1.6.1. The latest version and the change log is always available at http://www.rudedog.org/auth_ldap/.

auth_ldap supports the following features:

Contents

Building the Module

The module is compatible with Apache 1.3.x. To use the SSL extensions, you must use the Netscape SDK. To use TLS, you must use the OpenLDAP 2.x SDK.

Under Unix, auth_ldap can be built as a DSO (Dynamic Shared Object) using Apache's apxs program. You should be able to build it using the old-style Configure method, but I don't personally use that method, so I can't guarantee support for it. The directions in this section apply to building auth_ldap as a DSO.

Note that a separate makefile is provided for building under Windows NT with Visual C++ 5.0.

  1. Run the configure script to generate the Makefile. configure accepts the following options.
    Option Comments
    --with-apxs=/path/to/apxs Specifies the path to Apache's apxs command. If not provided, configure will search your path.
    --with-ldap-sdk=sdk Specifies the LDAP SDK that you are using. For OpenLDAP, use openldap. For Netscape, use netscape. If you're using a different SDK, specify other. If not provided, the default is openldap.
    --with-sdk-headers=/path/to/headers Specifies the directory that contains the LDAP SDK include files.
    --with-sdk-libs=/path/to/libs Specifies the directory that contains the LDAP SDK libraries.
    --with-ssl Build auth_ldap with SSL support.
    --with-shared-cache Include support for the shared LDAP cache. The default is to have the shared cache enabled. To turn it off, use the option --without-shared-cache. Note that the shared cache requires that Apache was built with MM support. If Apache was not built with MM support, then this option will do nothing, even if enabled. It is safe to ignore this option.
    --with-activate Include commands during the installation to activate auth_ldap. Your Apache's httpd.conf file will be modified during installation if this option is on.
    --with-frontpage Include support for the FrontPage hack.
  2. Run make; make install to install the module with the rest of your Apache modules.
  3. To load the module, add an appropriate LoadModule directive to your httpd.conf file, such as
    # For Unix
    LoadModule   auth_ldap_module    libexec/auth_ldap.so
    
    # For Windows NT
    LoadModule   auth_ldap_module    modules/AuthLDAP.dll

Note that this module has not been extensively tested under Windows NT, as I do not have easy access to an NT box. Because Apache on NT is threaded, there are a lot of concurrency issues. I think that I have addressed most of the concurrency issues, although the locks are pretty coarse grained. If you have any experiences, good or bad, with the module under Windows NT, please let me know, so I can improve these instructions and the module. I'm especially open to receiving code improvements for NT.

Operation

There are two phases in granting access to a user. The first phase is authentication, in which auth_ldap verifies that the user's credentials are valid. This also called the search/bind phase. The second phase is authorization, in which auth_ldap determines if the authenticated user is allowed access to the resource in question. This is also known as the compare phase.

The Authentication Phase

During the authentication phase, auth_ldap searches for an entry in the directory that matches the username that the HTTP client passes. If a single unique match is found, then auth_ldap attempts to bind to the directory server using the DN of the entry plus the password provided by the HTTP client. Because it does a search, then a bind, it is often referred to as the search/bind phase. Here are the steps taken during the search/bind phase.

  1. Generate a search filter by combining the attribute and filter provided in the AuthLDAPURL directive with the username passed by the HTTP client.
  2. Search the directory using the generated filter. If the search does not return exactly one entry, deny or decline access.
  3. Fetch the distinguished name of the entry retrieved from the search and attempt to bind to the LDAP server using the DN and the password passed by the HTTP client. If the bind is unsuccessful, deny or decline access.

The following directives are used during the search/bind phase

AuthLDAPURL Specifies the LDAP server, the base DN, the attribute to use in the search, as well as the extra search filter to use.
AuthLDAPBindDN An optional DN to bind with during the search phase.
AuthLDAPBindPassword An optional password to bind with during the search phase.

The Authorization Phase

During the authorization phase, auth_ldap attempts to determine if the user is authorized to access the resource. Many of these checks require auth_ldap to do a compare operation on the LDAP server. This is why this phase is often referred to as the compare phase. auth_ldap accepts the following require directives to determine if the credentials are acceptable:

auth_ldap uses the following directives during the compare phase:

AuthLDAPURL The attribute specified in the URL is used in compare operations for the require user operation.
AuthLDAPCompareDNOnServer Determines the behavior of the require dn directive.
AuthLDAPGroupAttribute Determines the attribute to use for comparisons in the require group directive.
AuthLDAPGroupAttributeIsDN Specifies whether to use the user DN or the username when doing comparisons for the require group directive.

Auth_ldap Directives

This section contains the complete list of directives that are used by the auth_ldap module.

AuthLDAPAuthoritative

Syntax: AuthLDAPAuthoritative < on(default) | off >
Context: directory, .htaccess
Override: AuthConfig
Status: Extension
Module: auth_ldap

Set to off if this module should let other authentication modules attempt to authenticate the user, should authentication with this module fail. Control is only passed on to lower modules if there is no DN or rule that matches the supplied user name (as passed by the client).


AuthLDAPBindDN

Syntax: AuthLDAPBindDN distinguished-name
Context: directory, .htaccess
Override: AuthConfig
Status: Extension
Module: auth_ldap

An optional DN used to bind to the server when searching for entries. If not provided, auth_ldap will use an anonymous bind.


AuthLDAPBindPassword

Syntax: AuthLDAPBindPassword password
Context: directory, .htaccess
Override: AuthConfig
Status: Extension
Module: auth_ldap

A bind password to use in conjunction with the bind DN. Note that the bind password is probably sensitive data, and should be properly protected. You should only use the AuthLDAPBindDN and AuthLDAPBindPassword if you absolutely need them to search the directory.


AuthLDAPVersion

Syntax: AuthLDAPVersion integer
Context: server config
Override: Not Applicable
Status: Extension
Module: auth_ldap

Specifies the LDAP protocol version to use against the LDAP server. Zero means to use the default. Typical values are 2 or 3. Newer OpenLDAP servers typically want version 3.


AuthLDAPCacheSize

Syntax: AuthLDAPCacheSize size
Context: server config
Override: Not Applicable
Status: Extension
Module: auth_ldap

Specifies the maximum size of the primary LDAP cache. This cache contains successful search/binds. Set it to 0 to turn of search/bind caching. The default size is 1024 cached searches. See the section below on caching for complete information on caching LDAP operations in auth_ldap.


AuthLDAPCacheTTL

Syntax: AuthLDAPCacheTTL time
Context: server config
Override: Not Applicable
Status: Extension
Module: auth_ldap

Specifies the time (in seconds) that an item in the search/bind cache remains valid. The default is 600 seconds (10 minutes). A value of 0 means that items in the cache never go stale.


AuthLDAPCertDBPath

Syntax: AuthLDAPCertDBPath /path/to/cert7.db/directory
Context: server config
Override: Not Applicable
Status: Extension
Module: auth_ldap

Specifies in which directory auth_ldap should look for the certificate authorities database. There should be a file named cert7.db in that directory.


AuthLDAPCompareDNOnServer

Syntax: AuthLDAPCompareDNOnServer < on(default) | off >
Context: directory, .htaccess
Override: AuthConfig
Status: Extension
Module: auth_ldap

When set, auth_ldap will use the LDAP server to compare the DNs. This is the only foolproof way to compare DNs. auth_ldap will search the directory for the DN specified with the require dn directive, then, retrieve the DN and compare it with the DN retrieved from the user entry. If this directive is not set, auth_ldap simply does a string comparison. It is possible to get false negatives with this approach, but it is much faster. Note the auth_ldap cache can speed up DN comparison in most situations.


AuthLDAPDereferenceAliases

Syntax: AuthLDAPDereferenceAliases never | searching | finding | always
Context: directory, .htaccess
Override: AuthConfig
Status: Extension
Module: auth_ldap

This directive specifies when auth_ldap will de-reference aliases during LDAP operations. The default is always.


AuthLDAPEnabled

Syntax: AuthLDAPEnabled < on(default) | off >
Context: directory, .htaccess
Override: AuthConfig
Status: Extension
Module: auth_ldap

Set to off to disable auth_ldap in certain directories. This is useful if you have auth_ldap enabled at or near the top of your tree, but want to disable it completely in certain locations.


AuthLDAPFrontPageHack

Syntax: AuthLDAPFrontPageHack < on | off(default) >
Context: directory, .htaccess
Override: AuthConfig
Status: Extension
Module: auth_ldap

See the section on using Microsoft FrontPage with auth_ldap.


AuthLDAPGroupAttribute

Syntax: AuthLDAPGroupAttribute attribute
Context: directory, .htaccess
Override: AuthConfig
Status: Extension
Module: auth_ldap

This directive specifies which LDAP attributes are used to check for group membership. Up to 10 attributes can be used by specifying this directive multiple times. If not specified, then auth_ldap uses the member and uniquemember attributes.


AuthLDAPGroupAttributeIsDN

Syntax: AuthLDAPGroupAttributeIsDN < on(default) | off >
Context: directory, .htaccess
Override: AuthConfig
Status: Extension
Module: auth_ldap

When set, this directive says to use the distinguished name of the client username when checking for group membership. Otherwise, the username will be used. For example, assume that the client sent the username bjenson, which corresponds to the LDAP DN cn=Babs Jenson, o=Airius. If this directive is set, auth_ldap will check if the group has cn=Babs Jenson, o=Airius as a member. If this directive is not set, then auth_ldap will check if the group has bjenson as a member.


AuthLDAPOpCacheSize

Syntax: AuthLDAPOpCacheSize size
Context: server config
Override: Not Applicable
Status: Extension
Module: auth_ldap

This specifies the size of the cache auth_ldap uses to cache LDAP compare operations. The default is 1024 entries. Setting it to 0 disables operation caching.


AuthLDAPOpCacheTTL

Syntax: AuthLDAPOpCacheTTL time
Context: server config
Override: Not Applicable
Status: Extension
Module: auth_ldap

Specifies the time (in seconds) that entries in the operation cache remain valid. The default is 600 seconds.


AuthLDAPRemoteUserIsDN

Syntax: AuthLDAPRemoteUserIsDN < on | off(default) >
Context: directory, .htaccess
Override: AuthConfig
Status: Extension
Module: auth_ldap

If this directive is set to on, the value of the REMOTE_USER environment variable will be set to the full distinguished name of the authenticated user, rather than just the username that was passed by the client. It is turned off by default, which is consistent with the behavior of previous auth_ldap releases.


AuthLDAPStartTLS

Syntax: AuthLDAPStartTLS < on | off(default) >
Context: directory, .htaccess
Override: AuthConfig
Status: Extension
Module: auth_ldap

If this directive is set to on, auth_ldap will start a secure TLS session after connecting to the LDAP server. This requires your LDAP server to support TLS.


AuthLDAPUrl

Syntax: AuthLDAPUrl url
Context: directory, .htaccess
Override: AuthConfig
Status: Extension
Module: auth_ldap

An RFC 2255 URL which specifies the LDAP search parameters to use. The syntax of the URL is

ldap://host:port/basedn?attribute?scope?filter

ldap For regular ldap, use the string ldap. For secure LDAP, use ldaps instead. Secure LDAP is only available if auth_ldap was compiled with SSL support.
host:port

The name/port of the ldap server (defaults to localhost:389 for ldap, and localhost:636 for ldaps). To specify multiple, redundant LDAP servers, just list all servers, separated by spaces. auth_ldap will try connecting to each server in turn, until it makes a successful connection.

Once a connection has been made to a server, that connection remains active for the life of the httpd process, or until the LDAP server goes down.

If the LDAP server goes down and breaks an existing connection, auth_ldap will attempt to re-connect, starting with the primary server, and trying each redundant server in turn. Note that this is different than a true round-robin search.

basedn The DN of the branch of the directory where all searches should start from. At the very least, this must be the top of your directory tree, but could also specify a subtree in the directory.
attribute The attribute to search for. Although RFC 2255 allows a comma-separated list of attributes, only the first attribute will be used, no matter how many are provided. If no attributes are provided, the default is to use uid. It's a good idea to choose an attribute that will be unique across all entries in the subtree you will be using.
scope The scope of the search. Can be either one or sub. Note that a scope of base is also supported by RFC 2255, but is not supported by this module. If the scope is not provided, or if base scope is specified, the default is to use a scope of sub.
filter A valid LDAP search filter. If not provided, defaults to (objectClass=*), which will search for all objects in the tree. Filters are limited to approximately 8000 characters (the definition of MAX_STRING_LEN in the Apache source code). This should be than sufficient for any application.

When doing searches, the attribute, filter and username passed by the HTTP client are combined to create a search filter that looks like (&(filter)(attribute=username)).

For example, consider an URL of ldap://ldap.airius.com/o=Airius?cn?sub?(posixid=*). When a client attempts to connect using a username of Babs Jenson, the resulting search filter will be (&(posixid=*)(cn=Babs Jenson)).

See below for examples of AuthLDAPURL URLs.


The require Directives

Apache's require directives are used during the authorization phase to ensure that a user is allowed to access a resource.

require valid-user

If this directive exists, auth_ldap grants access to any user that has successfully authenticated during the search/bind phase.

require user

The require user directive specifies what usernames can access the resource. Once auth_ldap has retrieved a unique DN from the directory, it does an LDAP compare operation using the username specified in the require user to see if that username is part of the just-fetched LDAP entry. Multiple users can be granted access by putting multiple usernames on the line, separated with spaces. If a username has a space in it, then it must be the only user on the line. In this case, multiple users can be granted access by using multiple require user directives, with one user per line. For example, with a AuthLDAPURL of ldap://ldap/o=Airius?cn (i.e., cn is used for searches), the following require directives could be used to restrict access:

require user Barbara Jenson
require user Fred User
require user Joe Manager

Because of the way that auth_ldap handles this directive, Barbara Jenson could sign on as Barbara Jenson, Babs Jenson or any other cn that she has in her LDAP entry. Only the single require user line is needed to support all values of the attribute in the user's entry.

If the uid attribute was used instead of the cn attribute in the URL above, the above three lines could be condensed to

require user bjenson fuser jmanager

require group

This directive specifies an LDAP group whose members are allowed access. It takes the distinguished name of the LDAP group. For example, assume that the following entry existed in the LDAP directory:

dn: cn=Administrators, o=Airius
objectClass: groupOfUniqueNames
uniqueMember: cn=Barbara Jenson, o=Airius
uniqueMember: cn=Fred User, o=Airius

The following directive would grant access to both Fred and Barbara:

require group cn=Administrators, o=Airius

Behavior of this directive is modified by the AuthLDAPGroupAttribute and AuthLDAPGroupAttributeIsDN directives.

require dn

The require dn directive allows the administrator to grant access based on distinguished names. It specifies a DN that must match for access to be granted. If the distinguished name that was retrieved from the directory server matches the distinguished name in the require dn, then authorization is granted.

The following directive would grant access to a specific DN:

require dn cn=Barbara Jenson, o=Airius

Behavior of this directive is modified by the AuthLDAPCompareDNOnServer directive.

Examples

Caching in auth_ldap

For improved performance, auth_ldap uses an aggressive caching strategy to minimize the number of times that the LDAP server must be contacted. Caching can easily double or triple the throughput of Apache when it is serving auth_ldap-protected pages. In addition, the load on the LDAP server will be significantly decreased.

auth_ldap supports two types of LDAP caching during the search/bind phase with a search/bind cache and during the compare phase with two operation caches. Each LDAP URL that is used by the server has its own set of these three caches.

The Search/Bind Cache

The process of doing a search and then a bind is the most time-consuming aspect of LDAP operation, especially if the directory is large. The search/bind cache is used to cache all searches that resulted in successful binds. Negative results (i.e., unsuccessful searches, or searches that did not result in a successful bind) are not cached. The rationale behind this decision is that connections with invalid credentials are only a tiny percentage of the total number of connections, so by not caching invalid credentials, the size of the cache is reduced.

auth_ldap stores the username, the DN retrieved, the password used to bind, and the time of the bind in the cache. Whenever a new connection is initiated with the same username, auth_ldap compares the password of the new connection with the password in the cache. If the passwords match, and if the cached entry is not too old, auth_ldap bypasses the search/bind phase.

The search and bind cache is controlled with the AuthLDAPCacheSize and AuthLDAPCacheTTL directives.

Operation Caches

During the compare phase, auth_ldap uses two operation caches to cache the compare operations that it does. The first compare cache is used to cache the results of compares done for the require user and require group directives. The second compare cache is used to cache the results of compares done for the require dn directive.

The behavior of both of these caches is controlled with the AuthLDAPOpCacheSize and AuthLDAPOpCacheTTL directives.

Monitoring the Cache

auth_ldap has a content handler that allows administrators to monitor the cache performance. The name of the content handler is auth-ldap-info, so the following directives could be used to access the auth_ldap cache information:

<Location /server/cache-info >
 SetHandler auth-ldap-info
</Location>
By fetching the URL http://servername/cache-info, the administrator can get a status report of every cache that is used by auth_ldap cache. Note that if Apache does not support MM-style shared memory, then each httpd instance has its own cache, so reloading the URL will result in different information each time, depending on which httpd instance processes the request.

Benchmarks

These benchmarks compare the performance for different cache configurations. They list the time to complete the benchmark, and the number of LDAP operations actually performed the LDAP server. All benchmarks were performed with Apache 1.3.4. Apache, the WWW client and the directory server all resided on the same system.

Note! These benchmarks were performed using auth_ldap 1.4, which used a much different caching strategy, the meaning of some of the benchmarks will not be apparent. The latest version of auth_ldap uses a strategy that should be much more efficient in most situations, so the benchmarks are mostly of use only for historical reasons.

The first benchmark did 10,000 retrievals of the same page. The page was protected by a <Location> specification in access.conf that contained a single require user directive.

LDAP Operations
Cache Level Real Time Connect Bind Search Compare Total
None 6:04 6 60,006 30,000 30,000 120,012
Search 4:53 6 60,006 6 30,000 90,018
Search, bind 2:20 6 18 6 30,000 30,030
Search, bind, compare 1:46 6 18 6 6 36

The second benchmark did 10,000 retrievals of the same page, but chose a random username from a pool of 1000 names for each connection. The page was protected by a <Location> specification in access.conf that contained a require valid-user directive. Since this doesn't require a LDAP compare operation, there is no benchmark for the third level of caching (search, bind and compare).

LDAP Operations
Cache Level Real Time Connect Bind Search Total
None 5:47 7 60,002 30,001 90,009
Search 5:00 7 60,002 9,970 69,979
Search, bind 2:51 7 9,672 9,968 19,647

These benchmarks are only included to give an idea about how caching can improve performance. They do not really simulate real world conditions, since both servers and the client were in a sandbox.

Using TLS

To use TLS, simply set the AuthLDAPStartTLS to on. Nothing else needs to be done (other than ensure that your LDAP server is configured for TLS).

Using SSL

auth_ldap will not talk to any SSL server unless that server has a certificate signed by a known Certificate Authority. Once you've built auth_ldap with SSL support, auth_ldap still needs to be told where it can find a database containing the known CAs. This database is in the same format as Netscape Communicator's cert7.db database. The easiest way to get this file is to start up a fresh copy of Netscape, and grab the resulting $HOME/.netscape/cert7.db file.

To specify a secure LDAP server, use ldaps:// in the AuthLDAPURL directive, instead of ldap://.

Using Microsoft FrontPage with auth_ldap

Normally, FrontPage uses FrontPage-web-specific user/group files (i.e., the mod_auth module) to handle all authentication. Unfortunately, it is not possible to just change to LDAP authentication by adding the proper directives, because it will break the Permissions forms in the FrontPage client, which attempt to modify the standard text-based authorization files.

To use FrontPage with auth_ldap, ensure that auth_ldap is compiled with the -DAUTH_LDAP_FRONTPAGE_HACK. See the Makefile for more information.

Once a FrontPage web has been created, adding LDAP authentication to it is a matter of adding the following directives to every .htaccess file that gets created in the web

AuthLDAPURL            the url
AuthLDAPAuthoritative  off
AuthLDAPFrontPageHack  on

AuthLDAPAuthoritative must be off to allow auth_ldap to decline group authentication so that Apache will fall back to file authentication for checking group membership. This allows the FrontPage-managed group file to be used.

How It Works

FrontPage restricts access to a web by adding the require valid-user directive to the .htaccess files. If AuthLDAPFrontPageHack is not on, the require valid-user directive will succeed for any user who is valid as far as LDAP is concerned. This means that anybody who has an entry in the LDAP directory is considered a valid user, whereas FrontPage considers only those people in the local user file to be valid. The purpose of the hack is to force Apache to consult the local user file (which is managed by FrontPage) - instead of LDAP - when handling the require valid-user directive.

Once directives have been added as specified above, FrontPage users will be able to perform all management operations from the FrontPage client.

Caveats

Acknowledgements

Thanks go to

Copyright

Copyright © 1998, 1999, Enbridge Pipelines Inc.
Copyright © 1999-2006, Dave Carrigan
All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Apache itself. This module is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. The copyright holder of this module can not be held liable for any general, special, incidental or consequential damages arising out of the use of the module.


<dave@rudedog.org>