util_ldap_cache.h revision 769fec4a3ee740c172676c243aeb02171d80872e
b66914021bd429f41311d2909a7e9289866da7fdnd/* ====================================================================
b51bf223f42d43ca6b1b33c95124edcfa5a871a4nd * The Apache Software License, Version 1.1
9963f91528694fb21e93da8584c31f226c6de97akess *
b66914021bd429f41311d2909a7e9289866da7fdnd * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
b66914021bd429f41311d2909a7e9289866da7fdnd * reserved.
08cb74ca432a8c24e39f17dedce527e6a47b8001jerenkrantz *
08cb74ca432a8c24e39f17dedce527e6a47b8001jerenkrantz * Redistribution and use in source and binary forms, with or without
b66914021bd429f41311d2909a7e9289866da7fdnd * modification, are permitted provided that the following conditions
b66914021bd429f41311d2909a7e9289866da7fdnd * are met:
b66914021bd429f41311d2909a7e9289866da7fdnd *
b66914021bd429f41311d2909a7e9289866da7fdnd * 1. Redistributions of source code must retain the above copyright
b66914021bd429f41311d2909a7e9289866da7fdnd * notice, this list of conditions and the following disclaimer.
b66914021bd429f41311d2909a7e9289866da7fdnd *
b66914021bd429f41311d2909a7e9289866da7fdnd * 2. Redistributions in binary form must reproduce the above copyright
b66914021bd429f41311d2909a7e9289866da7fdnd * notice, this list of conditions and the following disclaimer in
b66914021bd429f41311d2909a7e9289866da7fdnd * the documentation and/or other materials provided with the
b66914021bd429f41311d2909a7e9289866da7fdnd * distribution.
b66914021bd429f41311d2909a7e9289866da7fdnd *
b66914021bd429f41311d2909a7e9289866da7fdnd * 3. The end-user documentation included with the redistribution,
b66914021bd429f41311d2909a7e9289866da7fdnd * if any, must include the following acknowledgment:
b66914021bd429f41311d2909a7e9289866da7fdnd * "This product includes software developed by the
eed2a23d9b5986937f1e2b1c120be97744508a72nd * Apache Software Foundation (http://www.apache.org/)."
eed2a23d9b5986937f1e2b1c120be97744508a72nd * Alternately, this acknowledgment may appear in the software itself,
eed2a23d9b5986937f1e2b1c120be97744508a72nd * if and wherever such third-party acknowledgments normally appear.
eed2a23d9b5986937f1e2b1c120be97744508a72nd *
eed2a23d9b5986937f1e2b1c120be97744508a72nd * 4. The names "Apache" and "Apache Software Foundation" must
eed2a23d9b5986937f1e2b1c120be97744508a72nd * not be used to endorse or promote products derived from this
eed2a23d9b5986937f1e2b1c120be97744508a72nd * software without prior written permission. For written
eed2a23d9b5986937f1e2b1c120be97744508a72nd * permission, please contact apache@apache.org.
eed2a23d9b5986937f1e2b1c120be97744508a72nd *
eed2a23d9b5986937f1e2b1c120be97744508a72nd * 5. Products derived from this software may not be called "Apache",
eed2a23d9b5986937f1e2b1c120be97744508a72nd * nor may "Apache" appear in their name, without prior written
eed2a23d9b5986937f1e2b1c120be97744508a72nd * permission of the Apache Software Foundation.
eed2a23d9b5986937f1e2b1c120be97744508a72nd *
eed2a23d9b5986937f1e2b1c120be97744508a72nd * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
eed2a23d9b5986937f1e2b1c120be97744508a72nd * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
eed2a23d9b5986937f1e2b1c120be97744508a72nd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
eed2a23d9b5986937f1e2b1c120be97744508a72nd * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
eed2a23d9b5986937f1e2b1c120be97744508a72nd * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
eed2a23d9b5986937f1e2b1c120be97744508a72nd * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
eed2a23d9b5986937f1e2b1c120be97744508a72nd * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
eed2a23d9b5986937f1e2b1c120be97744508a72nd * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
eed2a23d9b5986937f1e2b1c120be97744508a72nd * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
eed2a23d9b5986937f1e2b1c120be97744508a72nd * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
eed2a23d9b5986937f1e2b1c120be97744508a72nd * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
eed2a23d9b5986937f1e2b1c120be97744508a72nd * SUCH DAMAGE.
eed2a23d9b5986937f1e2b1c120be97744508a72nd * ====================================================================
eed2a23d9b5986937f1e2b1c120be97744508a72nd *
eed2a23d9b5986937f1e2b1c120be97744508a72nd * This software consists of voluntary contributions made by many
eed2a23d9b5986937f1e2b1c120be97744508a72nd * individuals on behalf of the Apache Software Foundation. For more
eed2a23d9b5986937f1e2b1c120be97744508a72nd * information on the Apache Software Foundation, please see
eed2a23d9b5986937f1e2b1c120be97744508a72nd * <http://www.apache.org/>.
eed2a23d9b5986937f1e2b1c120be97744508a72nd */
eed2a23d9b5986937f1e2b1c120be97744508a72nd
eed2a23d9b5986937f1e2b1c120be97744508a72nd
eed2a23d9b5986937f1e2b1c120be97744508a72nd#ifndef APU_LDAP_CACHE_H
eed2a23d9b5986937f1e2b1c120be97744508a72nd#define APU_LDAP_CACHE_H
eed2a23d9b5986937f1e2b1c120be97744508a72nd
eed2a23d9b5986937f1e2b1c120be97744508a72nd/*
eed2a23d9b5986937f1e2b1c120be97744508a72nd * This switches LDAP support on or off.
eed2a23d9b5986937f1e2b1c120be97744508a72nd */
eed2a23d9b5986937f1e2b1c120be97744508a72nd
eed2a23d9b5986937f1e2b1c120be97744508a72nd/* this whole thing disappears if LDAP is not enabled */
eed2a23d9b5986937f1e2b1c120be97744508a72nd#ifdef APU_HAS_LDAP
eed2a23d9b5986937f1e2b1c120be97744508a72nd
eed2a23d9b5986937f1e2b1c120be97744508a72nd
eed2a23d9b5986937f1e2b1c120be97744508a72nd/*
eed2a23d9b5986937f1e2b1c120be97744508a72nd * LDAP Cache Manager
eed2a23d9b5986937f1e2b1c120be97744508a72nd */
eed2a23d9b5986937f1e2b1c120be97744508a72nd
eed2a23d9b5986937f1e2b1c120be97744508a72nd#include <apr_shm.h>
eed2a23d9b5986937f1e2b1c120be97744508a72nd#include <apr_rmm.h> /* EDD */
bcf004854091600aa279525d6772e1827114d39dnd
eed2a23d9b5986937f1e2b1c120be97744508a72ndtypedef struct util_cache_node_t {
eed2a23d9b5986937f1e2b1c120be97744508a72nd void *payload; /* Pointer to the payload */
eed2a23d9b5986937f1e2b1c120be97744508a72nd apr_time_t add_time; /* Time node was added to cache */
eed2a23d9b5986937f1e2b1c120be97744508a72nd struct util_cache_node_t *next;
eed2a23d9b5986937f1e2b1c120be97744508a72nd} util_cache_node_t;
eed2a23d9b5986937f1e2b1c120be97744508a72nd
eed2a23d9b5986937f1e2b1c120be97744508a72ndtypedef struct util_ald_cache util_ald_cache_t;
eed2a23d9b5986937f1e2b1c120be97744508a72nd
eed2a23d9b5986937f1e2b1c120be97744508a72ndstruct util_ald_cache {
eed2a23d9b5986937f1e2b1c120be97744508a72nd unsigned long size; /* Size of cache array */
eed2a23d9b5986937f1e2b1c120be97744508a72nd unsigned long maxentries; /* Maximum number of cache entries */
eed2a23d9b5986937f1e2b1c120be97744508a72nd unsigned long numentries; /* Current number of cache entries */
eed2a23d9b5986937f1e2b1c120be97744508a72nd unsigned long fullmark; /* Used to keep track of when cache becomes 3/4 full */
eed2a23d9b5986937f1e2b1c120be97744508a72nd apr_time_t marktime; /* Time that the cache became 3/4 full */
eed2a23d9b5986937f1e2b1c120be97744508a72nd unsigned long (*hash)(void *); /* Func to hash the payload */
eed2a23d9b5986937f1e2b1c120be97744508a72nd int (*compare)(void *, void *); /* Func to compare two payloads */
eed2a23d9b5986937f1e2b1c120be97744508a72nd void * (*copy)(util_ald_cache_t *cache, void *); /* Func to alloc mem and copy payload to new mem */
eed2a23d9b5986937f1e2b1c120be97744508a72nd void (*free)(util_ald_cache_t *cache, void *); /* Func to free mem used by the payload */
eed2a23d9b5986937f1e2b1c120be97744508a72nd util_cache_node_t **nodes;
eed2a23d9b5986937f1e2b1c120be97744508a72nd
eed2a23d9b5986937f1e2b1c120be97744508a72nd unsigned long numpurges; /* No. of times the cache has been purged */
eed2a23d9b5986937f1e2b1c120be97744508a72nd double avg_purgetime; /* Average time to purge the cache */
9963f91528694fb21e93da8584c31f226c6de97akess apr_time_t last_purge; /* Time of the last purge */
bcf004854091600aa279525d6772e1827114d39dnd unsigned long npurged; /* Number of elements purged in last purge. This is not
9963f91528694fb21e93da8584c31f226c6de97akess obvious: it won't be 3/4 the size of the cache if
eed2a23d9b5986937f1e2b1c120be97744508a72nd there were a lot of expired entries. */
eed2a23d9b5986937f1e2b1c120be97744508a72nd
b51bf223f42d43ca6b1b33c95124edcfa5a871a4nd unsigned long fetches; /* Number of fetches */
b51bf223f42d43ca6b1b33c95124edcfa5a871a4nd unsigned long hits; /* Number of cache hits */
eed2a23d9b5986937f1e2b1c120be97744508a72nd unsigned long inserts; /* Number of inserts */
eed2a23d9b5986937f1e2b1c120be97744508a72nd unsigned long removes; /* Number of removes */
eed2a23d9b5986937f1e2b1c120be97744508a72nd
eed2a23d9b5986937f1e2b1c120be97744508a72nd#if APR_HAS_SHARED_MEMORY
eed2a23d9b5986937f1e2b1c120be97744508a72nd apr_shm_t *shm_addr;
eed2a23d9b5986937f1e2b1c120be97744508a72nd apr_rmm_t *rmm_addr;
eed2a23d9b5986937f1e2b1c120be97744508a72nd#endif
eed2a23d9b5986937f1e2b1c120be97744508a72nd
eed2a23d9b5986937f1e2b1c120be97744508a72nd};
b51bf223f42d43ca6b1b33c95124edcfa5a871a4nd
eed2a23d9b5986937f1e2b1c120be97744508a72nd#if APR_HAS_THREADS
eed2a23d9b5986937f1e2b1c120be97744508a72nd#define LDAP_CACHE_LOCK_CREATE(p) \
eed2a23d9b5986937f1e2b1c120be97744508a72nd if (!st->util_ldap_cache_lock) apr_thread_rwlock_create(&st->util_ldap_cache_lock, st->pool)
eed2a23d9b5986937f1e2b1c120be97744508a72nd#define LDAP_CACHE_WRLOCK() \
eed2a23d9b5986937f1e2b1c120be97744508a72nd apr_thread_rwlock_wrlock(st->util_ldap_cache_lock)
eed2a23d9b5986937f1e2b1c120be97744508a72nd#define LDAP_CACHE_UNLOCK() \
eed2a23d9b5986937f1e2b1c120be97744508a72nd apr_thread_rwlock_unlock(st->util_ldap_cache_lock)
eed2a23d9b5986937f1e2b1c120be97744508a72nd#define LDAP_CACHE_RDLOCK() \
eed2a23d9b5986937f1e2b1c120be97744508a72nd apr_thread_rwlock_rdlock(st->util_ldap_cache_lock)
eed2a23d9b5986937f1e2b1c120be97744508a72nd#else
eed2a23d9b5986937f1e2b1c120be97744508a72nd#define LDAP_CACHE_LOCK_CREATE(p)
eed2a23d9b5986937f1e2b1c120be97744508a72nd#define LDAP_CACHE_WRLOCK()
eed2a23d9b5986937f1e2b1c120be97744508a72nd#define LDAP_CACHE_UNLOCK()
eed2a23d9b5986937f1e2b1c120be97744508a72nd#define LDAP_CACHE_RDLOCK()
eed2a23d9b5986937f1e2b1c120be97744508a72nd#endif
eed2a23d9b5986937f1e2b1c120be97744508a72nd
eed2a23d9b5986937f1e2b1c120be97744508a72nd#ifndef WIN32
eed2a23d9b5986937f1e2b1c120be97744508a72nd#define ALD_MM_FILE_MODE ( S_IRUSR|S_IWUSR )
#else
#define ALD_MM_FILE_MODE ( _S_IREAD|_S_IWRITE )
#endif
/*
* LDAP Cache
*/
/*
* Maintain a cache of LDAP URLs that the server handles. Each node in
* the cache contains the search cache for that URL, and a compare cache
* for the URL. The compare cash is populated when doing require group
* compares.
*/
typedef struct util_url_node_t {
const char *url;
util_ald_cache_t *search_cache;
util_ald_cache_t *compare_cache;
util_ald_cache_t *dn_compare_cache;
} util_url_node_t;
/*
* We cache every successful search and bind operation, using the username
* as the key. Each node in the cache contains the returned DN, plus the
* password used to bind.
*/
typedef struct util_search_node_t {
const char *username; /* Cache key */
const char *dn; /* DN returned from search */
const char *bindpw; /* The most recently used bind password;
NULL if the bind failed */
apr_time_t lastbind; /* Time of last successful bind */
const char **vals; /* Values of queried attributes */
} util_search_node_t;
/*
* We cache every successful compare operation, using the DN, attrib, and
* value as the key.
*/
typedef struct util_compare_node_t {
const char *dn; /* DN, attrib and value combine to be the key */
const char *attrib;
const char *value;
apr_time_t lastcompare;
int result;
} util_compare_node_t;
/*
* We cache every successful compare dn operation, using the dn in the require
* statement and the dn fetched based on the client-provided username.
*/
typedef struct util_dn_compare_node_t {
const char *reqdn; /* The DN in the require dn statement */
const char *dn; /* The DN found in the search */
} util_dn_compare_node_t;
/*
* Function prototypes for LDAP cache
*/
/* util_ldap_cache.c */
unsigned long util_ldap_url_node_hash(void *n);
int util_ldap_url_node_compare(void *a, void *b);
void *util_ldap_url_node_copy(util_ald_cache_t *cache, void *c);
void util_ldap_url_node_free(util_ald_cache_t *cache, void *n);
unsigned long util_ldap_search_node_hash(void *n);
int util_ldap_search_node_compare(void *a, void *b);
void *util_ldap_search_node_copy(util_ald_cache_t *cache, void *c);
void util_ldap_search_node_free(util_ald_cache_t *cache, void *n);
unsigned long util_ldap_compare_node_hash(void *n);
int util_ldap_compare_node_compare(void *a, void *b);
void *util_ldap_compare_node_copy(util_ald_cache_t *cache, void *c);
void util_ldap_compare_node_free(util_ald_cache_t *cache, void *n);
unsigned long util_ldap_dn_compare_node_hash(void *n);
int util_ldap_dn_compare_node_compare(void *a, void *b);
void *util_ldap_dn_compare_node_copy(util_ald_cache_t *cache, void *c);
void util_ldap_dn_compare_node_free(util_ald_cache_t *cache, void *n);
/* util_ldap_cache_mgr.c */
/* Cache alloc and free function, dealing or not with shm */
void util_ald_free(apr_rmm_t *rmm_addr, const void *ptr);
void *util_ald_alloc(apr_rmm_t *rmm_addr, unsigned long size);
const char *util_ald_strdup(apr_rmm_t *rmm_addr, const char *s);
/* Cache managing function */
unsigned long util_ald_hash_string(int nstr, ...);
void util_ald_cache_purge(util_ald_cache_t *cache);
util_url_node_t *util_ald_create_caches(util_ldap_state_t *s, const char *url);
util_ald_cache_t *util_ald_create_cache(util_ldap_state_t *st,
unsigned long (*hashfunc)(void *),
int (*comparefunc)(void *, void *),
void * (*copyfunc)(util_ald_cache_t *cache, void *),
void (*freefunc)(util_ald_cache_t *cache, void *));
void util_ald_destroy_cache(util_ald_cache_t *cache);
void *util_ald_cache_fetch(util_ald_cache_t *cache, void *payload);
void util_ald_cache_insert(util_ald_cache_t *cache, void *payload);
void util_ald_cache_remove(util_ald_cache_t *cache, void *payload);
char *util_ald_cache_display_stats(apr_pool_t *p, util_ald_cache_t *cache,
char *name);
#endif /* APU_HAS_LDAP */
#endif /* APU_LDAP_CACHE_H */