Blame modules/ldap/util_ldap_cache.h

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