util_ldap.c revision f2386b627177c7a80d38fed6ec0aed3c086909c1
124N/A/* Licensed to the Apache Software Foundation (ASF) under one or more 124N/A * contributor license agreements. See the NOTICE file distributed with 124N/A * this work for additional information regarding copyright ownership. 124N/A * The ASF licenses this file to You under the Apache License, Version 2.0 124N/A * (the "License"); you may not use this file except in compliance with 124N/A * the License. You may obtain a copy of the License at 124N/A * Unless required by applicable law or agreed to in writing, software 124N/A * distributed under the License is distributed on an "AS IS" BASIS, 124N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124N/A * See the License for the specific language governing permissions and 124N/A * limitations under the License. 124N/A * Original code from auth_ldap module for Apache v1.3: 669N/A * Copyright 1998, 1999 Enbridge Pipelines Inc. 124N/A * Copyright 1999-2001 Dave Carrigan 124N/A/* Default define for ldap functions that need a SIZELIMIT but 124N/A * do not have the define 124N/A * XXX This should be removed once a supporting #define is 765N/A * released through APR-Util. * This handler generates a status page about the current performance of * the LDAP cache. It is enabled as follows: * <Location /ldap-status> "<html><head><title>LDAP Cache Information</title></head>\n", r);
ap_rputs(
"<body bgcolor='#ffffff'><h1 align=center>LDAP Cache Information" /* ------------------------------------------------------------------ */ * Closes an LDAP connection by unlocking it. The next time * uldap_connection_find() is called this connection will be * Is it safe leaving bound connections floating around between the * different modules? Keeping the user bound is a performance boost, * but it is also a potential security problem - maybe. * For now we unbind the user when we finish with a connection, but /* mark our connection as available for reuse */ * Destroys an LDAP connection by unbinding and closing the connection to * the LDAP server. It is used to bring the connection back to a known * Clean up an LDAP connection by unbinding and unlocking the connection. * This cleanup does not remove the util_ldap_connection_t from the * per-virtualhost list of connections, does not remove the storage * for the util_ldap_connection_t or it's data, and is NOT run automatically. /* Release the rebind info for this connection. No more referral rebinds required. */ /* unbind and disconnect from the LDAP server */ /* free the username and password */ /* ldc->reason is allocated from r->pool */ * util_ldap_connection_remove frees all storage associated with the LDAP * connection and removes it completely from the per-virtualhost list of * The caller should hold the lock for this connection /* Remove ldc from the list */ /* Some unfortunate duplication between this method * and uldap_connection_cleanup() /* Destory the pool associated with this connection */ /* Since the host will include a port if the default port is not used, * always specify the default ports for the port parameter. This will * allow a host string that contains multiple hosts the ability to mix * some hosts with ports and some without. All hosts which do not * specify a port will use the default port. /* something really bad happened */ ldc->
reason =
"LDAP: ldap initialization failed";
ldc->
reason =
"LDAP: ldap initialization failed";
/* Now that we have an ldap struct, add it to the referral list for rebinds. */ "LDAP: Unable to add rebind cross reference entry. Out of memory?");
ldc->
reason =
"LDAP: Unable to add rebind cross reference entry.";
/* always default to LDAP V3 */ /* set client certificates */ /* Set the alias dereferencing option */ /* Set options for rebind and referrals. */ "LDAP: Setting referrals to %s.",
"Unable to set LDAP_OPT_REFERRALS option to %s: %d.",
/* Referral hop limit - only if referrals are enabled and a hop limit is explicitly requested */ "Setting referral hop limit to %d.",
"Unable to set LDAP_OPT_REFHOPLIMIT option to %d: %d.",
/*XXX All of the #ifdef's need to be removed once apr-util 1.2 is released */ /* This is not a per-connection setting so just pass NULL for the Ldap connection handle */ "LDAP: Could not set the connection timeout");
* LDAP_OPT_TIMEOUT is not portable, but it influences all synchronous ldap * function calls and not just ldap_search_ext_s(), which accepts a timeout * XXX: It would be possible to simulate LDAP_OPT_TIMEOUT by replacing all * XXX: synchronous ldap function calls with asynchronous calls and using * XXX: ldap_result() with a timeout. "LDAP: Could not set LDAP_OPT_TIMEOUT");
* Replacement function for ldap_simple_bind_s() with a timeout. * To do this in a portable way, we have to use ldap_simple_bind() and * Returns LDAP_SUCCESS on success; and an error code on failure ldc->
reason =
"LDAP: ldap_simple_bind() failed";
/* -1 is LDAP_SERVER_DOWN in openldap, use something else */ ldc->
reason =
"LDAP: ldap_simple_bind() result retrieval failed";
/* -1 is LDAP_SERVER_DOWN in openldap, use something else */ ldc->
reason =
"LDAP: ldap_simple_bind() timed out";
ldc->
reason =
"LDAP: ldap_simple_bind() parse result failed";
* Connect to the LDAP server and binds. Does not connect if already * connected (i.e. ldc->ldap is non-NULL.) Does not bind if already bound. * Returns LDAP_SUCCESS on success; and an error code on failure /* sanity check for NULL */ /* If the connection is already bound, return ldc->
reason =
"LDAP: connection open successful (already bound)";
/* create the ldap session handle /* loop trying to bind up to 10 times if LDAP_SERVER_DOWN error is * returned. If LDAP_TIMEOUT is returned on the first try, maybe the * connection was idle for a long time and has been dropped by a firewall. * In this case close the connection immediately and try again. * On Success or any other error, break out of the loop. * NOTE: Looping is probably not a great idea. If the server isn't * responding the chances it will respond after a few tries are poor. * However, the original code looped and it only happens on "ldap_simple_bind() timed out on reused " "connection, dropped by firewall?");
/* attempt to init the connection once again */ /* free the handle if there was an error ldc->
reason =
"LDAP: ldap_simple_bind() failed";
ldc->
reason =
"LDAP: connection open successful";
* Compare client certificate arrays. * Returns 1 on compare failure, 0 otherwise. /* arrays both NULL? if so, then equal */ /* arrays different length or either NULL? If so, then not equal */ /* run an actual comparison */ /* One is passwordless? If so, then not equal */ /* if we got here, the cert arrays were identical */ * Find an existing ldap connection struct that matches the * provided ldap connection parameters. * If not found in the cache, a new ldc structure will be allocated * from st->pool and returned to the caller. If found in the cache, * a pointer to the existing ldc structure will be returned. /* mutex lock this function */ /* Search for an exact connection match in the list that is not /* If this connection didn't match the criteria, then we * need to unlock the mutex so it is available to be reused. /* If nothing found, search again, but we don't care about the * binddn and bindpw this time. /* the bind credentials have changed */ /* If this connection didn't match the criteria, then we * need to unlock the mutex so it is available to be reused. /* artificially disable cache */ /* If no connection was found after the second search, we "util_ldap: Failed to create memory pool");
* Add the new connection entry to the linked list. Note that we * don't actually establish an LDAP connection yet; that happens * the first time authentication is requested. /* create the details of this connection in the new pool */ /* The security mode after parsing the URL will always be either * APR_LDAP_NONE (ldap://) or APR_LDAP_SSL (ldaps://). * If the security setting is NONE, override it to the security * setting optionally supplied by the admin using LDAPTrustedMode /* save away a copy of the client cert list that is presently valid */ /* ------------------------------------------------------------------ */ * Compares two DNs to see if they're equal. The only way to do this correctly * is to search for the dn and then do ldap_get_dn() on the result. This should * match the initial dn, since it would have been also retrieved with * ldap_get_dn(). This is expensive, so if the configuration value * compare_dn_on_server is false, just does an ordinary strcmp. * The lock for the ldap cache should already be acquired. const char *
url,
const char *
dn,
/* get cache entry (or create one) */ /* unlock this read lock */ ldc->
reason =
"DN Comparison FALSE (direct strcmp())";
ldc->
reason =
"DN Comparison TRUE (direct strcmp())";
/* no - it's a server side compare */ /* is it in the compare cache? */ /* If it's in the cache, it's good */ /* unlock this read lock */ /* unlock this read lock */ /* make a server connection */ /* connect to server failed */ "(objectclass=*)",
NULL,
1,
ldc->
reason =
"DN Comparison ldap_search_ext_s() " "failed with server down";
* we are reusing a connection that doesn't seem to be active anymore * (firewall state drop?), let's try a new connection. ldc->
reason =
"DN Comparison ldap_search_ext_s() " /* search for reqdn failed - no match */ ldc->
reason =
"DN Comparison ldap_search_ext_s() failed";
/* compare unsuccessful */ ldc->
reason =
"DN Comparison FALSE (checked on server)";
/* compare successful - add to the compare cache */ ldc->
reason =
"DN Comparison TRUE (checked on server)";
* Does an generic ldap_compare operation. It accepts a cache that it will use * to lookup the compare in the cache. We cache two kinds of compares * (require group compares) and (require user compares). Each compare has a * different cache node: require group includes the DN; require user does not * because the require user cache is owned by the const char *
url,
const char *
dn,
/* get cache entry (or create one) */ /* make a comparison to the cache */ /* ...but it is too old */ ldc->
reason =
"Comparison no such attribute (cached)";
ldc->
reason =
"Comparison undefined (cached)";
/* record the result code to return with the reason... */ /* and unlock this read lock */ /* unlock this read lock */ /* connection failed - try again */ ldc->
reason =
"ldap_compare_s() failed with server down";
* we are reusing a connection that doesn't seem to be active anymore * (firewall state drop?), let's try a new connection. ldc->
reason =
"ldap_compare_s() failed with timeout";
/* compare completed; caching result */ /* If the node doesn't exist then insert it, otherwise just update * it with the last results " insertion failure.",
getpid());
ldc->
reason =
"Comparison true (adding to cache)";
ldc->
reason =
"Comparison false (adding to cache)";
ldc->
reason =
"Comparison no such attribute (adding to cache)";
* 3.B. The cache didn't have any subgrouplist yet. Go check for subgroups. /* try to do the search */ ldc->
reason =
"ldap_search_ext_s() for subgroups failed with server" * we are reusing a connection that doesn't seem to be active anymore * (firewall state drop?), let's try a new connection. ldc->
reason =
"ldap_search_ext_s() for subgroups failed with timeout";
/* if there is an error (including LDAP_NO_SUCH_OBJECT) return now */ ldc->
reason =
"ldap_search_ext_s() for subgroups failed";
* Get values for the provided sub-group attributes. /* Get *all* matching "member" values from this group. */ * Now we are going to pare the subgroup members of this group * to *just* the subgroups, add them to the compare_nodep, and * then proceed to check the new level of subgroups. /* Check if this entry really is a group. */ /* It's a group, so add it to the array. */ /* We need to fill in tmp_local_subgroups using the data from LDAP */ * Does a recursive lookup operation to try to find a user within (cached) * nested groups. It accepts a cache that it will use to lookup previous * compare attempts. We cache two kinds of compares (require group compares) * and (require user compares). Each compare has a different cache node: * require group includes the DN; require user does not because the require * user cache is owned by the * DON'T CALL THIS UNLESS YOU CALLED uldap_cache_compare FIRST!!!!! * 1. Call uldap_cache_compare for each subgroupclass value to check the * generic, user-agnostic, cached group entry. This will create a new generic * wasn't one. If nothing returns LDAP_COMPARE_TRUE skip to step 5 since we * 2. Lock The cache and get the generic cache entry. * 3. Check if there is already a subgrouplist in this generic group's cache * A. If there is, go to step 4. * i) Use ldap_search to get the full list * of subgroup "members" (which may include non-group "members"). * ii) Use uldap_cache_compare to strip the list down to just groups. * iii) Lock and add this stripped down list to the cache of the generic * 4. Loop through the sgl and call uldap_cache_compare (using the user info) * subgroup to see if the subgroup contains the user and to get the subgroups * cache (with user-afinity, if they aren't already there). * A. If the user is in the subgroup, then we'll be returning * B. if the user isn't in the subgroup (LDAP_COMPARE_FALSE via * uldap_cache_compare) then recursively call this function to get the * 5. Cleanup local allocations. * 6. Return the final result. const char *
url,
const char *
dn,
* Stop looking at deeper levels of nested groups if we have reached the * max. Since we already checked the top-level group in uldap_cache_compare, * we don't need to check it again here - so if max_subgroup_depth is set * to 0, we won't check it (i.e. that is why we check < rather than <=). * We'll be calling uldap_cache_compare from here to check if the user is * in the next level before we recurse into that next level looking for * 1. Check the "groupiness" of the specified basedn. Stopping at the first ldc->
reason =
"DN failed group verification.";
* 2. Find previously created cache entry and check if there is already a /* make a comparison to the cache */ * Found the generic group entry... but the user isn't in this * group or we wouldn't be here. /* Make a local copy of the subgroup list */ " Making local copy of SGL for " "group (%s)(objectClass=%s) ",
/* No Cached SGL, retrieve from LDAP */ " retrieving from LDAP" ,
getpid(),
dn);
/* No SGL aailable via LDAP either */ " util_ldap: no subgroups for %s" ,
getpid(),
dn);
* Find the generic group cache entry and add the sgl we just retrieved. * The group entry we want to attach our SGL to doesn't exist. * We only got here if we verified this DN was actually a group * based on the objectClass, but we can't call the compare function * while we already hold the cache lock -- only the insert. "retrieve group entry for %s from cache",
* We have a valid cache entry and a locally generated SGL. * Attach the SGL to the cache entry /* We looked up an SGL for a group and found it to be empty */ "Copying local SGL of len %d for group %s into cache",
"Copy of SGL failed to obtain shared memory, " "couldn't update cache");
* tmp_local_sgl has either been created, or copied out of the cache * If tmp_local_sgl is NULL, there are no subgroups to process and we'll * 4. Now loop through the subgroupList and call uldap_cache_compare * 4.A. We found the user in the subgroup. Return " util_ldap: Found user %s in a subgroup (%s) at" * 4.B. We didn't find the user in this subgroup, so recurse into " util_ldap: user %s not found in subgroup (%s) at" /* Get the cache node for this url */ /* found entry in search cache... */ * Remove this item from the cache if its expired. If the sent * password doesn't match the storepassword, the entry will * be removed and readded later if the credentials pass /* ...but entry is too old */ /* ...and entry is valid */ ldc->
reason =
"Authentication successful (cached)";
/* unlock this read lock */ * At this point, there is no valid cached search, so lets do the search. * If LDAP operation fails due to LDAP_SERVER_DOWN, control returns here. ldc->
reason =
"ldap_search_ext_s() for user failed with server down";
/* if there is an error (including LDAP_NO_SUCH_OBJECT) return now */ ldc->
reason =
"ldap_search_ext_s() for user failed";
* We should have found exactly one entry; to find a different ldc->
reason =
"User is not unique (search found two " /* Grab the dn, copy it into the pool, and free it again */ * A bind to the server with an empty password always succeeds, so * we check to ensure that the password is not empty. This implies * that users who actually do have empty passwords will never be * able to authenticate with this module. I don't see this as a big * Attempt to bind with the retrieved dn and the password. If the bind * fails, it means that the password is wrong (the dn obviously * exists, since we just retrieved it) ldc->
reason =
"ldap_simple_bind() to check user credentials " "failed with server down";
ldc->
reason =
"ldap_simple_bind() to check user credentials " /* failure? if so - return */ ldc->
reason =
"ldap_simple_bind() to check user credentials failed";
* We have just bound the connection to a different user and password * combination, which might be reused unintentionally next time this * connection is used from the connection pool. To ensure no confusion, * we mark the connection as unbound. * Get values for the provided attributes. * Add the new username to the search cache. /* Search again to make sure that another thread didn't ready insert * this node into the cache before we got here. If it does exist then /* Nothing in cache, insert new entry */ /* Entry in cache is invalid, remove it and insert new one */ /* Cache entry is valid, update lastbind */ * This function will return the DN of the entry matching userid. * It is used to get the DN in case some other module than mod_auth_ldap * has authenticated the user. * The function is basically a copy of uldap_cache_checkuserid * with password checking removed. /* Get the cache node for this url */ /* found entry in search cache... */ * Remove this item from the cache if its expired. /* ...but entry is too old */ /* ...and entry is valid */ /* unlock this read lock */ * At this point, there is no valid cached search, so lets do the search. * If LDAP operation fails due to LDAP_SERVER_DOWN, control returns here. ldc->
reason =
"ldap_search_ext_s() for user failed with server down";
/* if there is an error (including LDAP_NO_SUCH_OBJECT) return now */ ldc->
reason =
"ldap_search_ext_s() for user failed";
* We should have found exactly one entry; to find a different ldc->
reason =
"User is not unique (search found two " /* Grab the dn, copy it into the pool, and free it again */ * Get values for the provided attributes. * Add the new username to the search cache. /* Search again to make sure that another thread didn't ready insert * this node into the cache before we got here. If it does exist then /* Nothing in cache, insert new entry */ * Don't update lastbind on entries with bindpw because * we haven't verified that password. It's OK to update * the entry if there is no password in it. /* Cache entry is valid, update lastbind */ * Reports if ssl support is enabled * 1 = enabled, 0 = not enabled /* ---------------------------------------- */ "LDAP cache: Setting shared memory cache file to %s bytes.",
* Parse the certificate type. * The type can be one of the following: * CA_DER, CA_BASE64, CA_CERT7_DB, CA_SECMOD, CERT_DER, CERT_BASE64, * CERT_KEY3_DB, CERT_NICKNAME, KEY_DER, KEY_BASE64 * If no matches are found, APR_LDAP_CA_TYPE_UNKNOWN is returned. /* Authority file in binary DER format */ /* Authority file in Base64 format */ /* Client cert file in DER format */ /* Client cert file in Base64 format */ /* Client cert file in PKCS#12 format */ /* Netscape client cert nickname */ /* Client cert key file in DER format */ /* Client cert key file in Base64 format */ /* Client cert key file in PKCS#12 format */ * Set LDAPTrustedGlobalCert. * This directive takes either two or three arguments: * - certificate file / directory / nickname * - certificate password (optional) * This directive may only be used globally. /* handle the certificate type */ "not recognised. It should be one " "of CA_DER, CA_BASE64, CA_CERT7_DB, " "CA_SECMOD, CERT_DER, CERT_BASE64, " "CERT_KEY3_DB, CERT_NICKNAME, " "KEY_DER, KEY_BASE64",
type);
return "Certificate type was not specified.";
"LDAP: SSL trusted global cert - %s (type %s)",
/* add the certificate to the global array */ /* if file is a file or path, fix the path */ "LDAP: Could not open SSL trusted certificate " return "Invalid global certificate file path";
* Set LDAPTrustedClientCert. * This directive takes either two or three arguments: * - certificate file / directory / nickname * - certificate password (optional) /* handle the certificate type */ "not recognised. It should be one " "CERT_DER, CERT_BASE64, " "CERT_NICKNAME, CERT_PFX, " "KEY_DER, KEY_BASE64, KEY_PFX",
"LDAPTrustedGlobalCert directive. " "Only CA_DER, CA_BASE64, " "CERT_DER, CERT_BASE64, " "CERT_NICKNAME, KEY_DER, and " "KEY_BASE64 may be used.",
type);
return "Certificate type was not specified.";
"LDAP: SSL trusted client cert - %s (type %s)",
/* add the certificate to the client array */ /* if file is a file or path, fix the path */ "LDAP: Could not open SSL client certificate " return "Invalid client certificate file path";
* This directive sets what encryption mode to use on a connection: * - STARTTLS (TLS encryption) "LDAP: SSL trusted mode - %s",
return "Invalid LDAPTrustedMode setting: must be one of NONE, " "LDAP: SSL verify server certificate - %s",
"LDAP: Connection timeout option not supported by the " "LDAP: Setting referral chasing %s",
return "This directive is not supported with the currently linked LDAP library";
return "LDAPReferralHopLimit must be greater than zero (Use 'LDAPReferrals Off' to disable referral chasing)";
"LDAP: Limit chased referrals to maximum of %d hops.",
/* defaults are AP_LDAP_CHASEREFERRALS_ON and AP_LDAP_DEFAULT_HOPLIMIT */ return "Timeout not numerical";
return "Timeout must be non-negative";
"LDAP: LDAP_OPT_TIMEOUT option not supported by the " "LDAP library in use. Using LDAPTimeout value as search " /* Create a per vhost pool for mod_ldap to use, serialized with * st->mutex (also one per vhost). both are replicated by fork(), * no shared memory managed by either. /* cache-related settings are not merged here, but in the post_config hook, * since the cache has not yet sprung to life /* The cache settings can not be modified in a virtual host since all server use the same /* These LDAP connection settings can not be overwritten in a virtual host. Once set in the base server, they must remain the same. None of the LDAP SDKs seem to be able to handle setting the verify_svr_cert flag on a per-connection basis. The OpenLDAP client appears to be able to handle the connection timeout per-connection but the Novell SDK cannot. Allowing the timeout to be set by each vhost is of little value so rather than trying to make special expections for one LDAP SDK, GLOBAL_ONLY is being enforced on this setting as well. */ /* util_ldap_post_config() will be called twice. Don't bother * going through all of the initialization on the first call * because it will just be thrown away.*/ /* If the cache file already exists then delete it. Otherwise we are * going to run into problems creating the shared memory. */ /* initializing cache if shared memory size is not zero and we already "LDAP cache: could not create shared memory segment");
/* merge config in all vhost */ "LDAP merging Shared Cache conf: shm=0x%pp rmm=0x%pp " "LDAP cache: LDAPSharedCacheSize is zero, disabling " * Initialize SSL support, and log the result for the benefit of the admin. * If SSL is not supported it is not necessarily an error, as the * application may not want to use it. "LDAP: SSL support available" );
"LDAP: SSL support unavailable%s%s",
/* Initialize the rebind callback's cross reference list. */ "LDAP: Could not set the LDAP library debug level to %d:(%d) %s",
"Failed to initialise global mutex %s in child process %" "Set the size of the shared memory cache (in bytes). Use " "0 to disable the shared memory cache. (default: 100000)"),
"Set the file name for the shared memory cache."),
"Set the maximum number of entries that are possible in the " "LDAP search cache. Use 0 or -1 to disable the search cache " "Set the maximum time (in seconds) that an item can be " "cached in the LDAP search cache. Use 0 for no limit. " "Set the maximum number of entries that are possible " "in the LDAP compare cache. Use 0 or -1 to disable the compare cache " "Set the maximum time (in seconds) that an item is cached " "in the LDAP operation cache. Use 0 for no limit. " "Takes three arguments; the first argument is the cert " "type of the second argument, one of CA_DER, CA_BASE64, " "CA_CERT7_DB, CA_SECMOD, CERT_DER, CERT_BASE64, CERT_KEY3_DB, " "CERT_NICKNAME, KEY_DER, or KEY_BASE64. The second argument " "specifes the file and/or directory containing the trusted CA " "certificates (and global client certs for Netware) used to " "validate the LDAP server. The third argument is an optional " "passphrase if applicable."),
"Takes three arguments: the first argument is the certificate " "type of the second argument, one of CA_DER, CA_BASE64, " "CA_CERT7_DB, CA_SECMOD, CERT_DER, CERT_BASE64, CERT_KEY3_DB, " "CERT_NICKNAME, KEY_DER, or KEY_BASE64. The second argument " "specifies the file and/or directory containing the client " "certificate, or certificate ID used to validate this LDAP " "client. The third argument is an optional passphrase if " "Specify the type of security that should be applied to " "an LDAP connection. One of; NONE, SSL or STARTTLS."),
"Set to 'ON' requires that the server certificate be verified" " before a secure LDAP connection can be establish. Default" "Specify the LDAP socket connection timeout in seconds " "Choose whether referrals are chased ['ON'|'OFF']. Default 'ON'"),
"Limit the number of referral hops that LDAP can follow. " "(Integer value, Consult LDAP SDK documentation for applicability and defaults"),
"Enable debugging in LDAP SDK (Default: off, values: SDK specific"),
"Specify the LDAP bind/search timeout in seconds " "(0 = no limit). Default: 60"),
NULL,
/* merge dir config */