util_ldap.c revision 57e892abdf632bb9224c6ae6a715d863242a00f3
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe/* Licensed to the Apache Software Foundation (ASF) under one or more
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * contributor license agreements. See the NOTICE file distributed with
fd9abdda70912b99b24e3bf1a38f26fde908a74cnd * this work for additional information regarding copyright ownership.
fd9abdda70912b99b24e3bf1a38f26fde908a74cnd * The ASF licenses this file to You under the Apache License, Version 2.0
fd9abdda70912b99b24e3bf1a38f26fde908a74cnd * (the "License"); you may not use this file except in compliance with
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * the License. You may obtain a copy of the License at
04bab87733a0e93a926e82311c85cd8ac06a032fnd * Unless required by applicable law or agreed to in writing, software
96ad5d81ee4a2cc66a4ae19893efc8aa6d06fae7jailletc * distributed under the License is distributed on an "AS IS" BASIS,
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * See the License for the specific language governing permissions and
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * limitations under the License.
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * util_ldap.c: LDAP things
af33a4994ae2ff15bc67d19ff1a7feb906745bf8rbowen * Original code from auth_ldap module for Apache v1.3:
3f08db06526d6901aa08c110b5bc7dde6bc39905nd * Copyright 1998, 1999 Enbridge Pipelines Inc.
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * Copyright 1999-2001 Dave Carrigan
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe#error mod_ldap requires APR-util to have LDAP support built in
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe/* Default define for ldap functions that need a SIZELIMIT but
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * do not have the define
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * XXX This should be removed once a supporting #define is
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh * released through APR-Util.
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowestatic const char *ldap_cache_mutex_type = "ldap-cache";
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe#define LDAP_CACHE_LOCK() do { \
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe#define LDAP_CACHE_UNLOCK() do { \
04bab87733a0e93a926e82311c85cd8ac06a032fnd} while (0)
35fe7da687551c1d6e0f62e192f10e39f6a55208rbowenstatic apr_status_t util_ldap_connection_remove (void *param);
04bab87733a0e93a926e82311c85cd8ac06a032fndstatic void util_ldap_strdup (char **str, const char *newstr)
04bab87733a0e93a926e82311c85cd8ac06a032fnd * Status Handler
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * --------------
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * This handler generates a status page about the current performance of
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * the LDAP cache. It is enabled as follows:
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * <Location /ldap-status>
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * SetHandler ldap-status
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * </Location>
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe ap_set_content_type(r, "text/html; charset=ISO-8859-1");
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe "<html><head><title>LDAP Cache Information</title></head>\n", r);
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe ap_rputs("<body bgcolor='#ffffff'><h1 align=center>LDAP Cache Information"
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe "</h1>\n", r);
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe/* ------------------------------------------------------------------ */
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * Closes an LDAP connection by unlocking it. The next time
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * uldap_connection_find() is called this connection will be
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * available for reuse.
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowestatic void uldap_connection_close(util_ldap_connection_t *ldc)
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * QUESTION:
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * Is it safe leaving bound connections floating around between the
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * different modules? Keeping the user bound is a performance boost,
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * but it is also a potential security problem - maybe.
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * For now we unbind the user when we finish with a connection, but
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * we don't have to...
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe /* mark our connection as available for reuse */
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * Destroys an LDAP connection by unbinding and closing the connection to
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * the LDAP server. It is used to bring the connection back to a known
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * state after an error.
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowestatic apr_status_t uldap_connection_unbind(void *param)
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * Clean up an LDAP connection by unbinding and unlocking the connection.
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * This cleanup does not remove the util_ldap_connection_t from the
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * per-virtualhost list of connections, does not remove the storage
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * for the util_ldap_connection_t or it's data, and is NOT run automatically.
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowestatic apr_status_t uldap_connection_cleanup(void *param)
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe /* Release the rebind info for this connection. No more referral rebinds required. */
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe /* unbind and disconnect from the LDAP server */
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe /* free the username and password */
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe /* ldc->reason is allocated from r->pool */
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe /* unlock this entry */
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * util_ldap_connection_remove frees all storage associated with the LDAP
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * connection and removes it completely from the per-virtualhost list of
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * connections
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * The caller should hold the lock for this connection
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowestatic apr_status_t util_ldap_connection_remove (void *param) {
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe util_ldap_connection_t *ldc = param, *l = NULL, *prev = NULL;
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe /* Remove ldc from the list */
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe if (l == ldc) {
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe /* Some unfortunate duplication between this method
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * and uldap_connection_cleanup()
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe /* Destory the pool associated with this connection */
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe struct timeval timeOut = {10,0}; /* 10 second connection timeout */
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe /* Since the host will include a port if the default port is not used,
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * always specify the default ports for the port parameter. This will
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * allow a host string that contains multiple hosts the ability to mix
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * some hosts with ports and some without. All hosts which do not
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * specify a port will use the default port.
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe /* something really bad happened */
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe /* Now that we have an ldap struct, add it to the referral list for rebinds. */
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe rc = apr_ldap_rebind_add(ldc->pool, ldc->ldap, ldc->binddn, ldc->bindpw);
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe "LDAP: Unable to add rebind cross reference entry. Out of memory?");
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe ldc->reason = "LDAP: Unable to add rebind cross reference entry.";
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe /* always default to LDAP V3 */
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe ldap_set_option(ldc->ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe /* set client certificates */
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe apr_ldap_set_option(r->pool, ldc->ldap, APR_LDAP_OPT_TLS_CERT,
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe /* switch on SSL/TLS */
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe /* Set the alias dereferencing option */
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe ldap_set_option(ldc->ldap, LDAP_OPT_DEREF, &ldap_option);
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe /* Set options for rebind and referrals. */
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe "LDAP: Setting referrals to %s.",
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe ((ldc->ChaseReferrals == AP_LDAP_CHASEREFERRALS_ON) ? "On" : "Off"));
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe (void *)((ldc->ChaseReferrals == AP_LDAP_CHASEREFERRALS_ON) ?
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe "Unable to set LDAP_OPT_REFERRALS option to %s: %d.",
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe ((ldc->ChaseReferrals == AP_LDAP_CHASEREFERRALS_ON) ? "On" : "Off"),
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe if ((ldc->ReferralHopLimit != AP_LDAP_HOPLIMIT_UNSET) && ldc->ChaseReferrals == AP_LDAP_CHASEREFERRALS_ON) {
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe /* Referral hop limit - only if referrals are enabled and a hop limit is explicitly requested */
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe "Setting referral hop limit to %d.",
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe "Unable to set LDAP_OPT_REFHOPLIMIT option to %d: %d.",
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe result->reason = "Unable to set LDAP_OPT_REFHOPLIMIT.";
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe/*XXX All of the #ifdef's need to be removed once apr-util 1.2 is released */
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe apr_ldap_set_option(r->pool, ldc->ldap, APR_LDAP_OPT_VERIFY_CERT,
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe result->rc = ldapssl_set_verify_mode(LDAPSSL_VERIFY_SERVER);
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe result->rc = ldapssl_set_verify_mode(LDAPSSL_VERIFY_NONE);
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe /* This is not a per-connection setting so just pass NULL for the
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe Ldap connection handle */
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe result->rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &i);
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe result->rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &i);
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe rc = apr_ldap_set_option(r->pool, ldc->ldap, LDAP_OPT_NETWORK_TIMEOUT,
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe "LDAP: Could not set the connection timeout");
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * Connect to the LDAP server and binds. Does not connect if already
43b5796243b481db6ca8718f9bd5c5084bd61888lgentis * connected (i.e. ldc->ldap is non-NULL.) Does not bind if already bound.
d5d58e8346ef3553dfd7d5f51078a9b268729b3frbowen * Returns LDAP_SUCCESS on success; and an error code on failure
43b5796243b481db6ca8718f9bd5c5084bd61888lgentis /* sanity check for NULL */
43b5796243b481db6ca8718f9bd5c5084bd61888lgentis /* If the connection is already bound, return
43b5796243b481db6ca8718f9bd5c5084bd61888lgentis ldc->reason = "LDAP: connection open successful (already bound)";
43b5796243b481db6ca8718f9bd5c5084bd61888lgentis /* create the ldap session handle
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe /* loop trying to bind up to 10 times if LDAP_SERVER_DOWN error is
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * returned. Break out of the loop on Success or any other error.
fc32fe886201ffcf1ca557bdf7d34a7468ab3374wrowe * NOTE: Looping is probably not a great idea. If the server isn't
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung * responding the chances it will respond after a few tries are poor.
727872d18412fc021f03969b8641810d8896820bhumbedooh * However, the original code looped and it only happens on
0d0ba3a410038e179b695446bb149cce6264e0abnd * the error condition.
0d0ba3a410038e179b695446bb149cce6264e0abnd /* attempt to init the connection once again */
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd /* free the handle if there was an error
return(rc);
static util_ldap_connection_t *
&ldap_module);
#if APR_HAS_THREADS
#if APR_HAS_THREADS
#if APR_HAS_THREADS
#if APR_HAS_THREADS
l->bound = 0;
#if APR_HAS_THREADS
#if APR_HAS_THREADS
return NULL;
#if APR_HAS_THREADS
l->bound = 0;
p->next = l;
#if APR_HAS_THREADS
int result = 0;
int failures = 0;
char *searchdn;
&ldap_module);
if (!compare_dn_on_server) {
return LDAP_COMPARE_FALSE;
return LDAP_COMPARE_TRUE;
if (curl) {
return LDAP_COMPARE_TRUE;
return result;
return result;
goto start_over;
return result;
if (curl) {
return result;
int result = 0;
int failures = 0;
&ldap_module);
if (curl) {
return result;
return result;
return result;
(char *)dn,
(char *)attrib,
(char *)value);
goto start_over;
if (curl) {
void *junk;
return LDAP_COMPARE_TRUE;
return LDAP_COMPARE_FALSE;
return LDAP_NO_SUCH_ATTRIBUTE;
return result;
const char *url,
const char *dn,
char **subgroupAttrs,
int failures = 0;
if (!subgroupAttrs) {
return res;
return res;
return res;
goto start_over;
return res;
if (subgroupAttrs) {
char **values;
int val_index = 0;
if (values) {
val_index = 0;
tmp_sgcIndex = 0;
tmp_sgcIndex++;
val_index++;
indx++;
int sgindex;
char **group;
return res;
char **subgroupAttrs,
int cur_subgroup_depth,
int max_subgroup_depth)
&ldap_module);
return LDAP_COMPARE_FALSE;
return result;
sizeof(util_compare_subgroup_t));
if (!tmp_local_sgl) {
if (!tmp_local_sgl) {
if (sgl_copy) {
if (!tmp_local_sgl) {
return result;
sgindex++;
return result;
const char ***retvals)
int numvals = 0;
int result = 0;
char *dn;
int count;
int failures = 0;
&ldap_module);
&curnode);
if (curl) {
if (attrs) {
return LDAP_SUCCESS;
return result;
return result;
goto start_over;
return result;
if (count == 0 )
return LDAP_NO_SUCH_OBJECT;
return LDAP_INVALID_CREDENTIALS;
(char *)*binddn,
(char *)bindpw);
goto start_over;
return result;
if (attrs) {
while (attrs[k++]);
numvals = k;
while (attrs[i]) {
char **values;
if (curl) {
return LDAP_SUCCESS;
int numvals = 0;
int result = 0;
char *dn;
int count;
int failures = 0;
&ldap_module);
&curnode);
if (curl) {
if (attrs) {
return LDAP_SUCCESS;
return result;
return result;
goto start_over;
return result;
if (count == 0 )
return LDAP_NO_SUCH_OBJECT;
if (attrs) {
while (attrs[k++]);
numvals = k;
while (attrs[i]) {
char **values;
if (curl) {
return LDAP_SUCCESS;
const char *bytes)
&ldap_module);
return err;
return NULL;
const char *file)
&ldap_module);
return err;
if (file) {
return NULL;
const char *ttl)
&ldap_module);
return err;
return NULL;
const char *size)
&ldap_module);
return err;
return NULL;
const char *ttl)
&ldap_module);
return err;
return NULL;
const char *size)
&ldap_module);
return err;
return NULL;
return APR_LDAP_CA_TYPE_DER;
return APR_LDAP_CA_TYPE_BASE64;
return APR_LDAP_CA_TYPE_CERT7_DB;
return APR_LDAP_CA_TYPE_SECMOD;
return APR_LDAP_CERT_TYPE_DER;
return APR_LDAP_CERT_TYPE_BASE64;
return APR_LDAP_CERT_TYPE_PFX;
return APR_LDAP_CERT_TYPE_KEY3_DB;
return APR_LDAP_CERT_TYPE_NICKNAME;
return APR_LDAP_KEY_TYPE_DER;
return APR_LDAP_KEY_TYPE_BASE64;
return APR_LDAP_KEY_TYPE_PFX;
return APR_LDAP_CA_TYPE_UNKNOWN;
void *dummy,
const char *type,
const char *file,
const char *password)
&ldap_module);
int cert_type = 0;
return err;
if (type) {
!= APR_SUCCESS))
return(NULL);
void *config,
const char *type,
const char *file,
const char *password)
&ldap_module);
int cert_type = 0;
if (type) {
type);
!= APR_SUCCESS))
return(NULL);
const char *mode)
&ldap_module);
mode);
return(NULL);
void *dummy,
int mode)
&ldap_module);
return err;
return(NULL);
void *dummy,
const char *ttl)
#ifdef LDAP_OPT_NETWORK_TIMEOUT
&ldap_module);
return err;
#ifdef LDAP_OPT_NETWORK_TIMEOUT
return NULL;
void *config,
int mode)
return(NULL);
void *config,
const char *arg) {
&ldap_module);
return err;
#ifndef AP_LDAP_OPT_DEBUG
return NULL;
void *config,
const char *hop_limit)
return "LDAPReferralHopLimit must be greater than zero (Use 'LDAPReferrals Off' to disable referral chasing)";
return NULL;
return dc;
#if APR_HAS_THREADS
return st;
void *overridesv)
#if APR_HAS_THREADS
return st;
return APR_SUCCESS;
APR_LOCK_DEFAULT, 0);
return result;
return OK;
&ldap_module);
void *data;
int rc;
if (!data) {
NULL);
return OK;
return DONE;
return result;
while (s_vhost) {
&ldap_module);
NULL,
&(result_err));
apr_ldap_rebind_init (p);
#ifdef AP_LDAP_OPT_DEBUG
return(OK);
&ldap_module);
{NULL}