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
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf#ifndef APU_LDAP_CACHE_H
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf#define APU_LDAP_CACHE_H
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh/**
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh * @file util_ldap_cache.h
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh * @brief This switches LDAP support on or off.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* this whole thing disappears if LDAP is not enabled */
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf#if APR_HAS_LDAP
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * LDAP Cache Manager
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
5c0419d51818eb02045cf923a9fe456127a44c60wrowe#include "util_ldap.h"
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholestypedef struct util_cache_node_t {
11e076839c8d5a82d55e710194d0daac51390dbdsf void *payload; /* Pointer to the payload */
11e076839c8d5a82d55e710194d0daac51390dbdsf 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;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstruct util_ald_cache {
11e076839c8d5a82d55e710194d0daac51390dbdsf unsigned long size; /* Size of cache array */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes 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 */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_time_t marktime; /* Time that the cache became 3/4 full */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes unsigned long (*hash)(void *); /* Func to hash the payload */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int (*compare)(void *, void *); /* Func to compare two payloads */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes void * (*copy)(util_ald_cache_t *cache, void *); /* Func to alloc mem and copy payload to new mem */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes void (*free)(util_ald_cache_t *cache, void *); /* Func to free mem used by the payload */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes void (*display)(request_rec *r, util_ald_cache_t *cache, void *); /* Func to display the payload contents */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes util_cache_node_t **nodes;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
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 */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes unsigned long npurged; /* Number of elements purged in last purge. This is not
742318b93e89c311f66b55f426c4d9cf2c14628bjim obvious: it won't be 3/4 the size of the cache if
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes there were a lot of expired entries. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes unsigned long fetches; /* Number of fetches */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes unsigned long hits; /* Number of cache hits */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes unsigned long inserts; /* Number of inserts */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes unsigned long removes; /* Number of removes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if APR_HAS_SHARED_MEMORY
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_shm_t *shm_addr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes 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 )
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#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;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes util_ald_cache_t *compare_cache;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes util_ald_cache_t *dn_compare_cache;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes} util_url_node_t;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj/*
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj * When a group is found, subgroups are stored in the group's cache entry.
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj */
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpjtypedef struct util_compare_subgroup_t {
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj const char **subgroupDNs;
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj int len;
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj} util_compare_subgroup_t;
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
742318b93e89c311f66b55f426c4d9cf2c14628bjim * We cache every successful search and bind operation, using the username
742318b93e89c311f66b55f426c4d9cf2c14628bjim * as the key. Each node in the cache contains the returned DN, plus the
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * password used to bind.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholestypedef struct util_search_node_t {
11e076839c8d5a82d55e710194d0daac51390dbdsf const char *username; /* Cache key */
11e076839c8d5a82d55e710194d0daac51390dbdsf const char *dn; /* DN returned from search */
11e076839c8d5a82d55e710194d0daac51390dbdsf const char *bindpw; /* The most recently used bind password;
11e076839c8d5a82d55e710194d0daac51390dbdsf NULL if the bind failed */
11e076839c8d5a82d55e710194d0daac51390dbdsf apr_time_t lastbind; /* Time of last successful bind */
11e076839c8d5a82d55e710194d0daac51390dbdsf const char **vals; /* Values of queried attributes */
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes 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
742318b93e89c311f66b55f426c4d9cf2c14628bjim * value as the key.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholestypedef struct util_compare_node_t {
11e076839c8d5a82d55e710194d0daac51390dbdsf const char *dn; /* DN, attrib and value combine to be the key */
742318b93e89c311f66b55f426c4d9cf2c14628bjim const char *attrib;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *value;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_time_t lastcompare;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int result;
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj 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. */
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj 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 {
11e076839c8d5a82d55e710194d0daac51390dbdsf const char *reqdn; /* The DN in the require dn statement */
11e076839c8d5a82d55e710194d0daac51390dbdsf const char *dn; /* The DN found in the search */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes} util_dn_compare_node_t;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Function prototypes for LDAP cache
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* util_ldap_cache.c */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesunsigned long util_ldap_url_node_hash(void *n);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesint util_ldap_url_node_compare(void *a, void *b);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesvoid *util_ldap_url_node_copy(util_ald_cache_t *cache, void *c);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesvoid 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);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesvoid util_ldap_search_node_display(request_rec *r, util_ald_cache_t *cache, void *n);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesunsigned 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);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesvoid *util_ald_alloc(util_ald_cache_t *cache, unsigned long size);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesconst char *util_ald_strdup(util_ald_cache_t *cache, const char *s);
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpjutil_compare_subgroup_t *util_ald_sgl_dup(util_ald_cache_t *cache, util_compare_subgroup_t *sgl);
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpjvoid util_ald_sgl_free(util_ald_cache_t *cache, util_compare_subgroup_t **sgl);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* 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,
c76a31675e52fe9308b147f9edd9a7024b4c8ce7bnicholes long cache_size,
742318b93e89c311f66b55f426c4d9cf2c14628bjim unsigned long (*hashfunc)(void *),
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int (*comparefunc)(void *, void *),
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes void * (*copyfunc)(util_ald_cache_t *cache, void *),
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes void (*freefunc)(util_ald_cache_t *cache, void *),
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes void (*displayfunc)(request_rec *r, util_ald_cache_t *cache, void *));
742318b93e89c311f66b55f426c4d9cf2c14628bjim
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
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf#endif /* APR_HAS_LDAP */
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf#endif /* APU_LDAP_CACHE_H */