util_ldap_cache.h revision 3f5585f7f4a7d74f2f94ec729ea8c1879d419e35
842ae4bd224140319ae7feec1872b93dfd491143fielding/* Licensed to the Apache Software Foundation (ASF) under one or more
842ae4bd224140319ae7feec1872b93dfd491143fielding * contributor license agreements. See the NOTICE file distributed with
842ae4bd224140319ae7feec1872b93dfd491143fielding * this work for additional information regarding copyright ownership.
842ae4bd224140319ae7feec1872b93dfd491143fielding * The ASF licenses this file to You under the Apache License, Version 2.0
842ae4bd224140319ae7feec1872b93dfd491143fielding * (the "License"); you may not use this file except in compliance with
842ae4bd224140319ae7feec1872b93dfd491143fielding * the License. You may obtain a copy of the License at
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * http://www.apache.org/licenses/LICENSE-2.0
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Unless required by applicable law or agreed to in writing, software
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * distributed under the License is distributed on an "AS IS" BASIS,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * See the License for the specific language governing permissions and
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * limitations under the License.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#ifndef APU_LDAP_CACHE_H
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#define APU_LDAP_CACHE_H
e8f95a682820a599fe41b22977010636be5c2717jim
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/**
e8f95a682820a599fe41b22977010636be5c2717jim * @file util_ldap_cache.h
1747d30b98aa1bdbc43994c02cd46ab4cb9319e4fielding * @brief This switches LDAP support on or off.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* this whole thing disappears if LDAP is not enabled */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if APR_HAS_LDAP
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * LDAP Cache Manager
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#include "util_ldap.h"
5c0419d51818eb02045cf923a9fe456127a44c60wrowe
5c0419d51818eb02045cf923a9fe456127a44c60wrowetypedef struct util_cache_node_t {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes void *payload; /* Pointer to the payload */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_time_t add_time; /* Time node was added to cache */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes struct util_cache_node_t *next;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes} util_cache_node_t;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholestypedef struct util_ald_cache util_ald_cache_t;
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton
d266c3777146d36a4c23c17aad6f153aebea1bf4jortonstruct util_ald_cache {
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton unsigned long size; /* Size of cache array */
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton unsigned long maxentries; /* Maximum number of cache entries */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes unsigned long numentries; /* Current number of cache entries */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes unsigned long fullmark; /* Used to keep track of when cache becomes 3/4 full */
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes apr_time_t marktime; /* Time that the cache became 3/4 full */
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes unsigned long (*hash)(void *); /* Func to hash the payload */
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes int (*compare)(void *, void *); /* Func to compare two payloads */
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes void * (*copy)(util_ald_cache_t *cache, void *); /* Func to alloc mem and copy payload to new mem */
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes void (*free)(util_ald_cache_t *cache, void *); /* Func to free mem used by the payload */
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes void (*display)(request_rec *r, util_ald_cache_t *cache, void *); /* Func to display the payload contents */
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes util_cache_node_t **nodes;
cd3bbd6d2df78d6c75e5d159a81ef8bdd5f70df9trawick
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes unsigned long numpurges; /* No. of times the cache has been purged */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes double avg_purgetime; /* Average time to purge the cache */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_time_t last_purge; /* Time of the last purge */
0568280364eb026393be492ebc732795c4934643jorton unsigned long npurged; /* Number of elements purged in last purge. This is not
0568280364eb026393be492ebc732795c4934643jorton obvious: it won't be 3/4 the size of the cache if
0568280364eb026393be492ebc732795c4934643jorton there were a lot of expired entries. */
0568280364eb026393be492ebc732795c4934643jorton
0568280364eb026393be492ebc732795c4934643jorton unsigned long fetches; /* Number of fetches */
0568280364eb026393be492ebc732795c4934643jorton unsigned long hits; /* Number of cache hits */
0568280364eb026393be492ebc732795c4934643jorton unsigned long inserts; /* Number of inserts */
0568280364eb026393be492ebc732795c4934643jorton unsigned long removes; /* Number of removes */
0568280364eb026393be492ebc732795c4934643jorton
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if APR_HAS_SHARED_MEMORY
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener apr_shm_t *shm_addr;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener apr_rmm_t *rmm_addr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes};
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#ifndef WIN32
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#define ALD_MM_FILE_MODE ( S_IRUSR|S_IWUSR )
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#else
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#define ALD_MM_FILE_MODE ( _S_IREAD|_S_IWRITE )
796e4a7141265d8ed7036e4628161c6eafb2a789jorton#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * LDAP Cache
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Maintain a cache of LDAP URLs that the server handles. Each node in
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * the cache contains the search cache for that URL, and a compare cache
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * for the URL. The compare cash is populated when doing require group
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * compares.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholestypedef struct util_url_node_t {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *url;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes util_ald_cache_t *search_cache;
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe util_ald_cache_t *compare_cache;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes util_ald_cache_t *dn_compare_cache;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe} util_url_node_t;
e8f95a682820a599fe41b22977010636be5c2717jim
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * When a group is found, subgroups are stored in the group's cache entry.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholestypedef struct util_compare_subgroup_t {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char **subgroupDNs;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int len;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes} util_compare_subgroup_t;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * We cache every successful search and bind operation, using the username
a1790fb35c4b352dab721370985c623a9f8f5062rpluem * as the key. Each node in the cache contains the returned DN, plus the
713a2b68bac4aeb1e9c48785006c0732451039depquerna * password used to bind.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholestypedef struct util_search_node_t {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *username; /* Cache key */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *dn; /* DN returned from search */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *bindpw; /* The most recently used bind password;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe NULL if the bind failed */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe apr_time_t lastbind; /* Time of last successful bind */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char **vals; /* Values of queried attributes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int numvals; /* Number of queried attributes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes} util_search_node_t;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * We cache every successful compare operation, using the DN, attrib, and
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * value as the key.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovenertypedef struct util_compare_node_t {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *dn; /* DN, attrib and value combine to be the key */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *attrib;
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes const char *value;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_time_t lastcompare;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int result;
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe int sgl_processed; /* 0 if no sgl processing yet. 1 if sgl has been processed (even if SGL is NULL). Saves repeat work on leaves. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes struct util_compare_subgroup_t *subgroupList;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes} util_compare_node_t;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * We cache every successful compare dn operation, using the dn in the require
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * statement and the dn fetched based on the client-provided username.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholestypedef struct util_dn_compare_node_t {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *reqdn; /* The DN in the require dn statement */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *dn; /* The DN found in the search */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes} util_dn_compare_node_t;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener/*
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener * Function prototypes for LDAP cache
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener/* util_ldap_cache.c */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesunsigned long util_ldap_url_node_hash(void *n);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovenerint util_ldap_url_node_compare(void *a, void *b);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesvoid *util_ldap_url_node_copy(util_ald_cache_t *cache, void *c);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovenervoid util_ldap_url_node_free(util_ald_cache_t *cache, void *n);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesvoid util_ldap_url_node_display(request_rec *r, util_ald_cache_t *cache, void *n);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesunsigned long util_ldap_search_node_hash(void *n);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesint util_ldap_search_node_compare(void *a, void *b);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesvoid *util_ldap_search_node_copy(util_ald_cache_t *cache, void *c);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesvoid util_ldap_search_node_free(util_ald_cache_t *cache, void *n);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovenervoid util_ldap_search_node_display(request_rec *r, util_ald_cache_t *cache, void *n);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
8113dac419143273351446c3ad653f3fe5ba5cfdwroweunsigned long util_ldap_compare_node_hash(void *n);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesint util_ldap_compare_node_compare(void *a, void *b);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesvoid *util_ldap_compare_node_copy(util_ald_cache_t *cache, void *c);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesvoid util_ldap_compare_node_free(util_ald_cache_t *cache, void *n);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesvoid util_ldap_compare_node_display(request_rec *r, util_ald_cache_t *cache, void *n);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesunsigned long util_ldap_dn_compare_node_hash(void *n);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesint util_ldap_dn_compare_node_compare(void *a, void *b);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesvoid *util_ldap_dn_compare_node_copy(util_ald_cache_t *cache, void *c);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesvoid util_ldap_dn_compare_node_free(util_ald_cache_t *cache, void *n);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesvoid util_ldap_dn_compare_node_display(request_rec *r, util_ald_cache_t *cache, void *n);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* util_ldap_cache_mgr.c */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Cache alloc and free function, dealing or not with shm */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesvoid util_ald_free(util_ald_cache_t *cache, const void *ptr);
1f299703465bd9975d94e9f229f76af807442de2covenervoid *util_ald_alloc(util_ald_cache_t *cache, unsigned long size);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovenerconst char *util_ald_strdup(util_ald_cache_t *cache, const char *s);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovenerutil_compare_subgroup_t *util_ald_sgl_dup(util_ald_cache_t *cache, util_compare_subgroup_t *sgl);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovenervoid util_ald_sgl_free(util_ald_cache_t *cache, util_compare_subgroup_t **sgl);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe/* Cache managing function */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesunsigned long util_ald_hash_string(int nstr, ...);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesvoid util_ald_cache_purge(util_ald_cache_t *cache);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesutil_url_node_t *util_ald_create_caches(util_ldap_state_t *s, const char *url);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesutil_ald_cache_t *util_ald_create_cache(util_ldap_state_t *st,
9ad7b260be233be7d7b5576979825cac72e15498rederpj long cache_size,
9ad7b260be233be7d7b5576979825cac72e15498rederpj unsigned long (*hashfunc)(void *),
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int (*comparefunc)(void *, void *),
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes void * (*copyfunc)(util_ald_cache_t *cache, void *),
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes void (*freefunc)(util_ald_cache_t *cache, void *),
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes void (*displayfunc)(request_rec *r, util_ald_cache_t *cache, void *));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesvoid util_ald_destroy_cache(util_ald_cache_t *cache);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesvoid *util_ald_cache_fetch(util_ald_cache_t *cache, void *payload);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesvoid *util_ald_cache_insert(util_ald_cache_t *cache, void *payload);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesvoid util_ald_cache_remove(util_ald_cache_t *cache, void *payload);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholeschar *util_ald_cache_display_stats(request_rec *r, util_ald_cache_t *cache, char *name, char *id);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
3e6d7277b90d3011db832139afc20efb5f17e203rederpj#endif /* APR_HAS_LDAP */
3e6d7277b90d3011db832139afc20efb5f17e203rederpj#endif /* APU_LDAP_CACHE_H */
3e6d7277b90d3011db832139afc20efb5f17e203rederpj