util_ldap.c revision 7a55c294da84865fe13262ed66ffd0c5841a9da5
e609c337f729875bc20e01096c7e610f45356f54nilgun/* Licensed to the Apache Software Foundation (ASF) under one or more
e609c337f729875bc20e01096c7e610f45356f54nilgun * contributor license agreements. See the NOTICE file distributed with
e609c337f729875bc20e01096c7e610f45356f54nilgun * this work for additional information regarding copyright ownership.
e609c337f729875bc20e01096c7e610f45356f54nilgun * The ASF licenses this file to You under the Apache License, Version 2.0
e609c337f729875bc20e01096c7e610f45356f54nilgun * (the "License"); you may not use this file except in compliance with
e609c337f729875bc20e01096c7e610f45356f54nilgun * the License. You may obtain a copy of the License at
e609c337f729875bc20e01096c7e610f45356f54nilgun * Unless required by applicable law or agreed to in writing, software
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * distributed under the License is distributed on an "AS IS" BASIS,
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * See the License for the specific language governing permissions and
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * limitations under the License.
3f08db06526d6901aa08c110b5bc7dde6bc39905nd * util_ldap.c: LDAP things
e609c337f729875bc20e01096c7e610f45356f54nilgun * Original code from auth_ldap module for Apache v1.3:
e609c337f729875bc20e01096c7e610f45356f54nilgun * Copyright 1998, 1999 Enbridge Pipelines Inc.
3f08db06526d6901aa08c110b5bc7dde6bc39905nd * Copyright 1999-2001 Dave Carrigan
e609c337f729875bc20e01096c7e610f45356f54nilgun#error mod_ldap requires APR-util to have LDAP support built in
e609c337f729875bc20e01096c7e610f45356f54nilgun/* Default define for ldap functions that need a SIZELIMIT but
e609c337f729875bc20e01096c7e610f45356f54nilgun * do not have the define
e609c337f729875bc20e01096c7e610f45356f54nilgun * XXX This should be removed once a supporting #define is
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun * released through APR-Util.
e609c337f729875bc20e01096c7e610f45356f54nilgunstatic const char *ldap_cache_mutex_type = "ldap-cache";
e609c337f729875bc20e01096c7e610f45356f54nilgunstatic apr_status_t uldap_connection_unbind(void *param);
e609c337f729875bc20e01096c7e610f45356f54nilgun#define LDAP_CACHE_LOCK() do { \
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun#define LDAP_CACHE_UNLOCK() do { \
e609c337f729875bc20e01096c7e610f45356f54nilgunstatic void util_ldap_strdup (char **str, const char *newstr)
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun * Status Handler
e609c337f729875bc20e01096c7e610f45356f54nilgun * --------------
e609c337f729875bc20e01096c7e610f45356f54nilgun * This handler generates a status page about the current performance of
e609c337f729875bc20e01096c7e610f45356f54nilgun * the LDAP cache. It is enabled as follows:
e609c337f729875bc20e01096c7e610f45356f54nilgun * <Location /ldap-status>
e609c337f729875bc20e01096c7e610f45356f54nilgun * SetHandler ldap-status
e609c337f729875bc20e01096c7e610f45356f54nilgun * </Location>
e609c337f729875bc20e01096c7e610f45356f54nilgun st = (util_ldap_state_t *) ap_get_module_config(r->server->module_config,
e609c337f729875bc20e01096c7e610f45356f54nilgun ap_set_content_type(r, "text/html; charset=ISO-8859-1");
e609c337f729875bc20e01096c7e610f45356f54nilgun "<html><head><title>LDAP Cache Information</title></head>\n", r);
e609c337f729875bc20e01096c7e610f45356f54nilgun ap_rputs("<body bgcolor='#ffffff'><h1 align=center>LDAP Cache Information"
e609c337f729875bc20e01096c7e610f45356f54nilgun "</h1>\n", r);
e609c337f729875bc20e01096c7e610f45356f54nilgun/* ------------------------------------------------------------------ */
e609c337f729875bc20e01096c7e610f45356f54nilgun * Closes an LDAP connection by unlocking it. The next time
e609c337f729875bc20e01096c7e610f45356f54nilgun * uldap_connection_find() is called this connection will be
e609c337f729875bc20e01096c7e610f45356f54nilgun * available for reuse.
e609c337f729875bc20e01096c7e610f45356f54nilgunstatic void uldap_connection_close(util_ldap_connection_t *ldc)
e609c337f729875bc20e01096c7e610f45356f54nilgun /* We leave bound LDAP connections floating around in our pool,
e609c337f729875bc20e01096c7e610f45356f54nilgun * but always check/fix the binddn/bindpw when we take them out
e609c337f729875bc20e01096c7e610f45356f54nilgun * of the pool
e609c337f729875bc20e01096c7e610f45356f54nilgun /* mark our connection as available for reuse */
e609c337f729875bc20e01096c7e610f45356f54nilgun * Destroys an LDAP connection by unbinding and closing the connection to
e609c337f729875bc20e01096c7e610f45356f54nilgun * the LDAP server. It is used to bring the connection back to a known
e609c337f729875bc20e01096c7e610f45356f54nilgun * state after an error.
e609c337f729875bc20e01096c7e610f45356f54nilgunstatic apr_status_t uldap_connection_unbind(void *param)
e609c337f729875bc20e01096c7e610f45356f54nilgun /* forget the rebind info for this conn */
e609c337f729875bc20e01096c7e610f45356f54nilgun if (ldc->ChaseReferrals == AP_LDAP_CHASEREFERRALS_ON) {
e609c337f729875bc20e01096c7e610f45356f54nilgun/* not presently used, not part of the API */
e609c337f729875bc20e01096c7e610f45356f54nilgun * util_ldap_connection_remove frees all storage associated with the LDAP
e609c337f729875bc20e01096c7e610f45356f54nilgun * connection and removes it completely from the per-virtualhost list of
e609c337f729875bc20e01096c7e610f45356f54nilgun * connections
e609c337f729875bc20e01096c7e610f45356f54nilgun * The caller should hold the lock for this connection
91f378b5a10f2d83820902ed10ba7967a3920c18nilgunstatic apr_status_t util_ldap_connection_remove (void *param) {
91f378b5a10f2d83820902ed10ba7967a3920c18nilgun util_ldap_connection_t *ldc = param, *l = NULL, *prev = NULL;
e609c337f729875bc20e01096c7e610f45356f54nilgun /* Remove ldc from the list */
e609c337f729875bc20e01096c7e610f45356f54nilgun if (l == ldc) {
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun /* Destory the pool associated with this connection */
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun struct timeval connectionTimeout = {10,0}; /* 10 second connection timeout */
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun /* Since the host will include a port if the default port is not used,
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun * always specify the default ports for the port parameter. This will
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun * allow a host string that contains multiple hosts the ability to mix
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun * some hosts with ports and some without. All hosts which do not
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun * specify a port will use the default port.
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun APR_LDAP_SSL == ldc->secure ? LDAPS_PORT : LDAP_PORT,
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun /* something really bad happened */
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun if (ldc->ChaseReferrals == AP_LDAP_CHASEREFERRALS_ON) {
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun /* Now that we have an ldap struct, add it to the referral list for rebinds. */
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun rc = apr_ldap_rebind_add(ldc->rebind_pool, ldc->ldap, ldc->binddn, ldc->bindpw);
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun "LDAP: Unable to add rebind cross reference entry. Out of memory?");
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun ldc->reason = "LDAP: Unable to add rebind cross reference entry.";
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun /* always default to LDAP V3 */
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun ldap_set_option(ldc->ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun /* set client certificates */
b9bf3918f6eaf7747bcbfbd02792bcbe4a052784nilgun apr_ldap_set_option(r->pool, ldc->ldap, APR_LDAP_OPT_TLS_CERT,
e609c337f729875bc20e01096c7e610f45356f54nilgun /* switch on SSL/TLS */
727872d18412fc021f03969b8641810d8896820bhumbedooh /* Set the alias dereferencing option */
727872d18412fc021f03969b8641810d8896820bhumbedooh ldap_set_option(ldc->ldap, LDAP_OPT_DEREF, &ldap_option);
cc7e1025de9ac63bd4db6fe7f71c158b2cf09fe4humbedooh /* Set options for rebind and referrals. */
727872d18412fc021f03969b8641810d8896820bhumbedooh ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
0d0ba3a410038e179b695446bb149cce6264e0abnd "LDAP: Setting referrals to %s.",
0d0ba3a410038e179b695446bb149cce6264e0abnd ((ldc->ChaseReferrals == AP_LDAP_CHASEREFERRALS_ON) ? "On" : "Off"));
0d0ba3a410038e179b695446bb149cce6264e0abnd (void *)((ldc->ChaseReferrals == AP_LDAP_CHASEREFERRALS_ON) ?
0d0ba3a410038e179b695446bb149cce6264e0abnd "Unable to set LDAP_OPT_REFERRALS option to %s: %d.",
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh ((ldc->ChaseReferrals == AP_LDAP_CHASEREFERRALS_ON) ? "On" : "Off"),
d229f940abfb2490dee17979e9a5ff31b7012eb5rbowen result->reason = "Unable to set LDAP_OPT_REFERRALS.";
if ((ldc->ReferralHopLimit != AP_LDAP_HOPLIMIT_UNSET) && ldc->ChaseReferrals == AP_LDAP_CHASEREFERRALS_ON) {
&(result));
#ifdef APR_LDAP_OPT_VERIFY_CERT
#if defined(LDAPSSL_VERIFY_SERVER)
int i = LDAP_OPT_X_TLS_DEMAND;
int i = LDAP_OPT_X_TLS_NEVER;
#ifdef LDAP_OPT_NETWORK_TIMEOUT
#ifdef LDAP_OPT_TIMEOUT
return(rc);
int rc;
return LDAP_OTHER;
else if (rc == 0) {
return rc;
int rc = 0;
int failures = 0;
int new_connection = 0;
if (!ldc) {
return LDAP_SUCCESS;
return rc;
&ldap_module);
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
#if APR_HAS_THREADS
#if APR_HAS_THREADS
return NULL;
#if APR_HAS_THREADS
l->bound = 0;
#if APR_HAS_THREADS
return NULL;
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;
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;
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;
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) {
dn);
dn);
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;
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)
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;
void *dummy,
const char *val)
long timeout;
char *endptr;
&ldap_module);
return err;
if (timeout < 0) {
if (timeout) {
timeout);
#ifndef LDAP_OPT_TIMEOUT
return NULL;
void *dummy,
const char *val)
&ldap_module);
if (timeout < 0) {
return NULL;
#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);
int rc;
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}