util_ldap.c revision 783874b86bfe13d7a4fe0922f344a3779cdccea3
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd/* Licensed to the Apache Software Foundation (ASF) under one or more
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * contributor license agreements. See the NOTICE file distributed with
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * this work for additional information regarding copyright ownership.
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * The ASF licenses this file to You under the Apache License, Version 2.0
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * (the "License"); you may not use this file except in compliance with
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * the License. You may obtain a copy of the License at
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * Unless required by applicable law or agreed to in writing, software
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * distributed under the License is distributed on an "AS IS" BASIS,
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * See the License for the specific language governing permissions and
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * limitations under the License.
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * util_ldap.c: LDAP things
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * Original code from auth_ldap module for Apache v1.3:
9472e4d3c410be3b3f1addbf3b1db1769f64e765nd * Copyright 1998, 1999 Enbridge Pipelines Inc.
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * Copyright 1999-2001 Dave Carrigan
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd#error mod_ldap requires APR-util to have LDAP support built in
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd/* Default define for ldap functions that need a SIZELIMIT but
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * do not have the define
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * XXX This should be removed once a supporting #define is
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * released through APR-Util.
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd#if !defined(LDAP_OPT_NETWORK_TIMEOUT) && defined(LDAP_OPT_CONNECT_TIMEOUT)
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd#define LDAP_CACHE_LOCK() do { \
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd} while (0)
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd#define LDAP_CACHE_UNLOCK() do { \
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd} while (0)
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34ndstatic void util_ldap_strdup (char **str, const char *newstr)
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * Status Handler
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * --------------
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * This handler generates a status page about the current performance of
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * the LDAP cache. It is enabled as follows:
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * <Location /ldap-status>
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * SetHandler ldap-status
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * </Location>
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd st = (util_ldap_state_t *) ap_get_module_config(r->server->module_config,
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd "<html><head><title>LDAP Cache Information</title></head>\n", r);
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd ap_rputs("<body bgcolor='#ffffff'><h1 align=center>LDAP Cache Information"
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd "</h1>\n", r);
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd/* ------------------------------------------------------------------ */
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * Closes an LDAP connection by unlocking it. The next time
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * uldap_connection_find() is called this connection will be
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * available for reuse.
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34ndstatic void uldap_connection_close(util_ldap_connection_t *ldc)
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd /* We leave bound LDAP connections floating around in our pool,
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * but always check/fix the binddn/bindpw when we take them out
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * of the pool
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd /* mark our connection as available for reuse */
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * Destroys an LDAP connection by unbinding and closing the connection to
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * the LDAP server. It is used to bring the connection back to a known
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * state after an error.
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd /* forget the rebind info for this conn */
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd/* not presently used, not part of the API */
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * util_ldap_connection_remove frees all storage associated with the LDAP
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * connection and removes it completely from the per-virtualhost list of
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * connections
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * The caller should hold the lock for this connection
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34ndstatic apr_status_t util_ldap_connection_remove (void *param) {
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd util_ldap_connection_t *ldc = param, *l = NULL, *prev = NULL;
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd /* Remove ldc from the list */
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd if (l == ldc) {
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd /* Destory the pool associated with this connection */
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd struct timeval connectionTimeout = {10,0}; /* 10 second connection timeout */
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
fac8c35bfb158112226ab43ddf84d59daca5dc30nd /* Since the host will include a port if the default port is not used,
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung * always specify the default ports for the port parameter. This will
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * allow a host string that contains multiple hosts the ability to mix
9c1260efa52c82c2a58e5b5f20cd6902563d95f5rbowen * some hosts with ports and some without. All hosts which do not
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * specify a port will use the default port.
&(result));
return(APR_EGENERAL);
return(rc);
&(result));
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 ldaprc;
#ifdef LDAP_OPT_ERROR_NUMBER
#ifdef LDAP_OPT_RESULT_CODE
return LDAP_OTHER;
int rc;
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);
failures++;
return(rc);
static util_ldap_connection_t *
&ldap_module);
#if APR_HAS_THREADS
#if APR_HAS_THREADS
/* Go ahead (by falling through) and use it, so we don't create more just to unbind some other old ones */
#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;
failures++;
goto start_over;
failures++;
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);
failures++;
goto start_over;
failures++;
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;
&ldap_module);
if (!subgroupAttrs) {
return res;
return res;
return res;
failures++;
goto start_over;
failures++;
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;
failures++;
goto start_over;
return result;
if (count == 0 )
return LDAP_NO_SUCH_OBJECT;
return LDAP_INVALID_CREDENTIALS;
failures++;
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;
failures++;
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) {
#ifdef AP_LDAP_OPT_DEBUG
&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;
void *dummy,
const char *val)
&ldap_module);
return err;
if (timeout < 0) {
return NULL;
void *dummy,
const char *val)
&ldap_module);
return err;
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}