1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef APU_LDAP_CACHE_H
18#define APU_LDAP_CACHE_H
19
20/**
21 * @file  util_ldap_cache.h
22 * @brief This switches LDAP support on or off.
23 */
24
25/* this whole thing disappears if LDAP is not enabled */
26#if APR_HAS_LDAP
27
28
29/*
30 * LDAP Cache Manager
31 */
32
33#include "util_ldap.h"
34
35typedef struct util_cache_node_t {
36    void *payload;              /* Pointer to the payload */
37    apr_time_t add_time;        /* Time node was added to cache */
38    struct util_cache_node_t *next;
39} util_cache_node_t;
40
41typedef struct util_ald_cache util_ald_cache_t;
42
43struct util_ald_cache {
44    unsigned long size;                 /* Size of cache array */
45    unsigned long maxentries;           /* Maximum number of cache entries */
46    unsigned long numentries;           /* Current number of cache entries */
47    unsigned long fullmark;             /* Used to keep track of when cache becomes 3/4 full */
48    apr_time_t marktime;                /* Time that the cache became 3/4 full */
49    unsigned long (*hash)(void *);      /* Func to hash the payload */
50    int (*compare)(void *, void *);     /* Func to compare two payloads */
51    void * (*copy)(util_ald_cache_t *cache, void *); /* Func to alloc mem and copy payload to new mem */
52    void (*free)(util_ald_cache_t *cache, void *); /* Func to free mem used by the payload */
53    void (*display)(request_rec *r, util_ald_cache_t *cache, void *); /* Func to display the payload contents */
54    util_cache_node_t **nodes;
55
56    unsigned long numpurges;    /* No. of times the cache has been purged */
57    double avg_purgetime;       /* Average time to purge the cache */
58    apr_time_t last_purge;      /* Time of the last purge */
59    unsigned long npurged;      /* Number of elements purged in last purge. This is not
60                                   obvious: it won't be 3/4 the size of the cache if
61                                   there were a lot of expired entries. */
62
63    unsigned long fetches;      /* Number of fetches */
64    unsigned long hits;         /* Number of cache hits */
65    unsigned long inserts;      /* Number of inserts */
66    unsigned long removes;      /* Number of removes */
67
68#if APR_HAS_SHARED_MEMORY
69    apr_shm_t *shm_addr;
70    apr_rmm_t *rmm_addr;
71#endif
72
73};
74
75#ifndef WIN32
76#define ALD_MM_FILE_MODE ( S_IRUSR|S_IWUSR )
77#else
78#define ALD_MM_FILE_MODE ( _S_IREAD|_S_IWRITE )
79#endif
80
81
82/*
83 * LDAP Cache
84 */
85
86/*
87 * Maintain a cache of LDAP URLs that the server handles. Each node in
88 * the cache contains the search cache for that URL, and a compare cache
89 * for the URL. The compare cash is populated when doing require group
90 * compares.
91 */
92typedef struct util_url_node_t {
93    const char *url;
94    util_ald_cache_t *search_cache;
95    util_ald_cache_t *compare_cache;
96    util_ald_cache_t *dn_compare_cache;
97} util_url_node_t;
98
99/*
100 * When a group is found, subgroups are stored in the group's cache entry.
101 */
102typedef struct util_compare_subgroup_t {
103    const char **subgroupDNs;
104    int len;
105} util_compare_subgroup_t;
106
107/*
108 * We cache every successful search and bind operation, using the username
109 * as the key. Each node in the cache contains the returned DN, plus the
110 * password used to bind.
111 */
112typedef struct util_search_node_t {
113    const char *username;               /* Cache key */
114    const char *dn;                     /* DN returned from search */
115    const char *bindpw;                 /* The most recently used bind password;
116                                           NULL if the bind failed */
117    apr_time_t lastbind;                /* Time of last successful bind */
118    const char **vals;                  /* Values of queried attributes */
119    int        numvals;         /* Number of queried attributes */
120} util_search_node_t;
121
122/*
123 * We cache every successful compare operation, using the DN, attrib, and
124 * value as the key.
125 */
126typedef struct util_compare_node_t {
127    const char *dn;                     /* DN, attrib and value combine to be the key */
128    const char *attrib;
129    const char *value;
130    apr_time_t lastcompare;
131    int result;
132    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. */
133    struct util_compare_subgroup_t *subgroupList;
134} util_compare_node_t;
135
136/*
137 * We cache every successful compare dn operation, using the dn in the require
138 * statement and the dn fetched based on the client-provided username.
139 */
140typedef struct util_dn_compare_node_t {
141    const char *reqdn;          /* The DN in the require dn statement */
142    const char *dn;                     /* The DN found in the search */
143} util_dn_compare_node_t;
144
145
146/*
147 * Function prototypes for LDAP cache
148 */
149
150/* util_ldap_cache.c */
151unsigned long util_ldap_url_node_hash(void *n);
152int util_ldap_url_node_compare(void *a, void *b);
153void *util_ldap_url_node_copy(util_ald_cache_t *cache, void *c);
154void util_ldap_url_node_free(util_ald_cache_t *cache, void *n);
155void util_ldap_url_node_display(request_rec *r, util_ald_cache_t *cache, void *n);
156
157unsigned long util_ldap_search_node_hash(void *n);
158int util_ldap_search_node_compare(void *a, void *b);
159void *util_ldap_search_node_copy(util_ald_cache_t *cache, void *c);
160void util_ldap_search_node_free(util_ald_cache_t *cache, void *n);
161void util_ldap_search_node_display(request_rec *r, util_ald_cache_t *cache, void *n);
162
163unsigned long util_ldap_compare_node_hash(void *n);
164int util_ldap_compare_node_compare(void *a, void *b);
165void *util_ldap_compare_node_copy(util_ald_cache_t *cache, void *c);
166void util_ldap_compare_node_free(util_ald_cache_t *cache, void *n);
167void util_ldap_compare_node_display(request_rec *r, util_ald_cache_t *cache, void *n);
168
169unsigned long util_ldap_dn_compare_node_hash(void *n);
170int util_ldap_dn_compare_node_compare(void *a, void *b);
171void *util_ldap_dn_compare_node_copy(util_ald_cache_t *cache, void *c);
172void util_ldap_dn_compare_node_free(util_ald_cache_t *cache, void *n);
173void util_ldap_dn_compare_node_display(request_rec *r, util_ald_cache_t *cache, void *n);
174
175
176/* util_ldap_cache_mgr.c */
177
178/* Cache alloc and free function, dealing or not with shm */
179void util_ald_free(util_ald_cache_t *cache, const void *ptr);
180void *util_ald_alloc(util_ald_cache_t *cache, unsigned long size);
181const char *util_ald_strdup(util_ald_cache_t *cache, const char *s);
182util_compare_subgroup_t *util_ald_sgl_dup(util_ald_cache_t *cache, util_compare_subgroup_t *sgl);
183void util_ald_sgl_free(util_ald_cache_t *cache, util_compare_subgroup_t **sgl);
184
185/* Cache managing function */
186unsigned long util_ald_hash_string(int nstr, ...);
187void util_ald_cache_purge(util_ald_cache_t *cache);
188util_url_node_t *util_ald_create_caches(util_ldap_state_t *s, const char *url);
189util_ald_cache_t *util_ald_create_cache(util_ldap_state_t *st,
190                                long cache_size,
191                                unsigned long (*hashfunc)(void *),
192                                int (*comparefunc)(void *, void *),
193                                void * (*copyfunc)(util_ald_cache_t *cache, void *),
194                                void (*freefunc)(util_ald_cache_t *cache, void *),
195                                void (*displayfunc)(request_rec *r, util_ald_cache_t *cache, void *));
196
197void util_ald_destroy_cache(util_ald_cache_t *cache);
198void *util_ald_cache_fetch(util_ald_cache_t *cache, void *payload);
199void *util_ald_cache_insert(util_ald_cache_t *cache, void *payload);
200void util_ald_cache_remove(util_ald_cache_t *cache, void *payload);
201char *util_ald_cache_display_stats(request_rec *r, util_ald_cache_t *cache, char *name, char *id);
202
203#endif /* APR_HAS_LDAP */
204#endif /* APU_LDAP_CACHE_H */
205