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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * util_ldap_cache_mgr.c: LDAP cache manager things
20 * Original code from auth_ldap module for Apache v1.3:
21 * Copyright 1998, 1999 Enbridge Pipelines Inc.
22 * Copyright 1999-2001 Dave Carrigan
26 #include "util_ldap.h"
27 #include "util_ldap_cache.h"
28 #include <apr_strings.h>
32 /* only here until strdup is gone */
35 /* here till malloc is gone */
38 static const unsigned long primes[] =
77 void util_ald_free(util_ald_cache_t *cache, const void *ptr)
79 #if APR_HAS_SHARED_MEMORY
80 if (cache->rmm_addr) {
82 /* Free in shared memory */
83 apr_rmm_free(cache->rmm_addr, apr_rmm_offset_get(cache->rmm_addr, (void *)ptr));
87 /* Cache shm is not used */
96 void *util_ald_alloc(util_ald_cache_t *cache, unsigned long size)
100 #if APR_HAS_SHARED_MEMORY
101 if (cache->rmm_addr) {
102 /* allocate from shared memory */
103 apr_rmm_off_t block = apr_rmm_calloc(cache->rmm_addr, size);
104 return block ? (void *)apr_rmm_addr_get(cache->rmm_addr, block) : NULL;
107 /* Cache shm is not used */
108 return (void *)calloc(sizeof(char), size);
111 return (void *)calloc(sizeof(char), size);
115 const char *util_ald_strdup(util_ald_cache_t *cache, const char *s)
117 #if APR_HAS_SHARED_MEMORY
118 if (cache->rmm_addr) {
119 /* allocate from shared memory */
120 apr_rmm_off_t block = apr_rmm_calloc(cache->rmm_addr, strlen(s)+1);
121 char *buf = block ? (char *)apr_rmm_addr_get(cache->rmm_addr, block) : NULL;
130 /* Cache shm is not used */
140 * Computes the hash on a set of strings. The first argument is the number
141 * of strings to hash, the rest of the args are strings.
142 * Algorithm taken from glibc.
144 unsigned long util_ald_hash_string(int nstr, ...)
148 unsigned long h=0, g;
151 va_start(args, nstr);
152 for (i=0; i < nstr; ++i) {
153 str = va_arg(args, char *);
154 for (p = str; *p; ++p) {
156 if ( ( g = h & 0xf0000000 ) ) {
169 Purges a cache that has gotten full. We keep track of the time that we
170 added the entry that made the cache 3/4 full, then delete all entries
171 that were added before that time. It's pretty simplistic, but time to
172 purge is only O(n), which is more important.
174 void util_ald_cache_purge(util_ald_cache_t *cache)
177 util_cache_node_t *p, *q, **pp;
183 cache->last_purge = apr_time_now();
187 for (i=0; i < cache->size; ++i) {
188 pp = cache->nodes + i;
191 if (p->add_time < cache->marktime) {
193 (*cache->free)(cache, p->payload);
194 util_ald_free(cache, p);
207 cache->avg_purgetime =
208 ((t - cache->last_purge) + (cache->avg_purgetime * (cache->numpurges-1))) /
216 util_url_node_t *util_ald_create_caches(util_ldap_state_t *st, const char *url)
218 util_url_node_t curl, *newcurl = NULL;
219 util_ald_cache_t *search_cache;
220 util_ald_cache_t *compare_cache;
221 util_ald_cache_t *dn_compare_cache;
223 /* create the three caches */
224 search_cache = util_ald_create_cache(st,
225 util_ldap_search_node_hash,
226 util_ldap_search_node_compare,
227 util_ldap_search_node_copy,
228 util_ldap_search_node_free,
229 util_ldap_search_node_display);
230 compare_cache = util_ald_create_cache(st,
231 util_ldap_compare_node_hash,
232 util_ldap_compare_node_compare,
233 util_ldap_compare_node_copy,
234 util_ldap_compare_node_free,
235 util_ldap_compare_node_display);
236 dn_compare_cache = util_ald_create_cache(st,
237 util_ldap_dn_compare_node_hash,
238 util_ldap_dn_compare_node_compare,
239 util_ldap_dn_compare_node_copy,
240 util_ldap_dn_compare_node_free,
241 util_ldap_dn_compare_node_display);
243 /* check that all the caches initialised successfully */
244 if (search_cache && compare_cache && dn_compare_cache) {
246 /* The contents of this structure will be duplicated in shared
247 memory during the insert. So use stack memory rather than
248 pool memory to avoid a memory leak. */
249 memset (&curl, 0, sizeof(util_url_node_t));
251 curl.search_cache = search_cache;
252 curl.compare_cache = compare_cache;
253 curl.dn_compare_cache = dn_compare_cache;
255 newcurl = util_ald_cache_insert(st->util_ldap_cache, &curl);
263 util_ald_cache_t *util_ald_create_cache(util_ldap_state_t *st,
264 unsigned long (*hashfunc)(void *),
265 int (*comparefunc)(void *, void *),
266 void * (*copyfunc)(util_ald_cache_t *cache, void *),
267 void (*freefunc)(util_ald_cache_t *cache, void *),
268 void (*displayfunc)(request_rec *r, util_ald_cache_t *cache, void *))
270 util_ald_cache_t *cache;
273 if (st->search_cache_size <= 0)
276 #if APR_HAS_SHARED_MEMORY
277 if (!st->cache_rmm) {
281 apr_rmm_off_t block = apr_rmm_calloc(st->cache_rmm, sizeof(util_ald_cache_t));
282 cache = block ? (util_ald_cache_t *)apr_rmm_addr_get(st->cache_rmm, block) : NULL;
285 cache = (util_ald_cache_t *)calloc(sizeof(util_ald_cache_t), 1);
290 #if APR_HAS_SHARED_MEMORY
291 cache->rmm_addr = st->cache_rmm;
292 cache->shm_addr = st->cache_shm;
294 cache->maxentries = st->search_cache_size;
295 cache->numentries = 0;
296 cache->size = st->search_cache_size / 3;
297 if (cache->size < 64) cache->size = 64;
298 for (i = 0; primes[i] && primes[i] < cache->size; ++i) ;
299 cache->size = primes[i]? primes[i] : primes[i-1];
301 cache->nodes = (util_cache_node_t **)util_ald_alloc(cache, cache->size * sizeof(util_cache_node_t *));
303 util_ald_free(cache, cache);
307 for (i=0; i < cache->size; ++i)
308 cache->nodes[i] = NULL;
310 cache->hash = hashfunc;
311 cache->compare = comparefunc;
312 cache->copy = copyfunc;
313 cache->free = freefunc;
314 cache->display = displayfunc;
316 cache->fullmark = cache->maxentries / 4 * 3;
318 cache->avg_purgetime = 0.0;
319 cache->numpurges = 0;
320 cache->last_purge = 0;
331 void util_ald_destroy_cache(util_ald_cache_t *cache)
334 util_cache_node_t *p, *q;
339 for (i = 0; i < cache->size; ++i) {
344 (*cache->free)(cache, p->payload);
345 util_ald_free(cache, p);
349 util_ald_free(cache, cache->nodes);
350 util_ald_free(cache, cache);
353 void *util_ald_cache_fetch(util_ald_cache_t *cache, void *payload)
356 util_cache_node_t *p;
363 hashval = (*cache->hash)(payload) % cache->size;
364 for (p = cache->nodes[hashval];
365 p && !(*cache->compare)(p->payload, payload);
378 * Insert an item into the cache.
379 * *** Does not catch duplicates!!! ***
381 void *util_ald_cache_insert(util_ald_cache_t *cache, void *payload)
384 util_cache_node_t *node;
387 if (cache == NULL || payload == NULL) {
391 /* check if we are full - if so, try purge */
392 if (cache->numentries >= cache->maxentries) {
393 util_ald_cache_purge(cache);
394 if (cache->numentries >= cache->maxentries) {
395 /* if the purge was not effective, we leave now to avoid an overflow */
400 /* should be safe to add an entry */
401 if ((node = (util_cache_node_t *)util_ald_alloc(cache, sizeof(util_cache_node_t))) == NULL) {
405 /* Take a copy of the payload before proceeeding. */
406 payload = (*cache->copy)(cache, payload);
408 util_ald_free(cache, node);
412 /* populate the entry */
414 hashval = (*cache->hash)(payload) % cache->size;
415 node->add_time = apr_time_now();
416 node->payload = payload;
417 node->next = cache->nodes[hashval];
418 cache->nodes[hashval] = node;
420 /* if we reach the full mark, note the time we did so
421 * for the benefit of the purge function
423 if (++cache->numentries == cache->fullmark) {
424 cache->marktime=apr_time_now();
427 return node->payload;
430 void util_ald_cache_remove(util_ald_cache_t *cache, void *payload)
433 util_cache_node_t *p, *q;
439 hashval = (*cache->hash)(payload) % cache->size;
440 for (p = cache->nodes[hashval], q=NULL;
441 p && !(*cache->compare)(p->payload, payload);
446 /* If p is null, it means that we couldn't find the node, so just return */
451 /* We found the node, and it's the first in the list */
452 cache->nodes[hashval] = p->next;
455 /* We found the node and it's not the first in the list */
458 (*cache->free)(cache, p->payload);
459 util_ald_free(cache, p);
463 char *util_ald_cache_display_stats(request_rec *r, util_ald_cache_t *cache, char *name, char *id)
469 util_cache_node_t *n;
471 apr_pool_t *p = r->pool;
477 for (i=0; i < cache->size; ++i) {
478 if (cache->nodes[i] != NULL) {
480 for (n = cache->nodes[i];
481 n != NULL && n != n->next;
487 chainlen = nchains? (double)totchainlen / (double)nchains : 0;
490 buf2 = apr_psprintf(p,
491 "<a href=\"%s?%s\">%s</a>",
500 buf = apr_psprintf(p,
503 "<td align='right' nowrap>%lu (%.0f%% full)</td>"
504 "<td align='right'>%.1f</td>"
505 "<td align='right'>%lu/%lu</td>"
506 "<td align='right'>%.0f%%</td>"
507 "<td align='right'>%lu/%lu</td>",
510 (double)cache->numentries / (double)cache->maxentries * 100.0,
514 (cache->fetches > 0 ? (double)(cache->hits) / (double)(cache->fetches) * 100.0 : 100.0),
518 if (cache->numpurges) {
519 char str_ctime[APR_CTIME_LEN];
521 apr_ctime(str_ctime, cache->last_purge);
522 buf = apr_psprintf(p,
524 "<td align='right'>%lu</td>\n"
525 "<td align='right' nowrap>%s</td>\n",
531 buf = apr_psprintf(p,
532 "%s<td colspan='2' align='center'>(none)</td>\n",
536 buf = apr_psprintf(p, "%s<td align='right'>%.2g</td>\n</tr>", buf, cache->avg_purgetime);
541 char *util_ald_cache_display(request_rec *r, util_ldap_state_t *st)
544 char *buf, *t1, *t2, *t3;
545 char *id1, *id2, *id3;
546 char *argfmt = "cache=%s&id=%d&off=%d";
547 char *scanfmt = "cache=%4s&id=%u&off=%u%1s";
548 apr_pool_t *pool = r->pool;
549 util_cache_node_t *p = NULL;
550 util_url_node_t *n = NULL;
552 util_ald_cache_t *util_ldap_cache = st->util_ldap_cache;
555 if (!util_ldap_cache) {
556 return "<tr valign='top'><td nowrap colspan=7>Cache has not been enabled/initialised.</td></tr>";
559 if (r->args && strlen(r->args)) {
560 char cachetype[5], lint[2];
561 unsigned int id, off;
562 char date_str[APR_CTIME_LEN+1];
564 if ((3 == sscanf(r->args, scanfmt, cachetype, &id, &off, lint)) &&
565 (id < util_ldap_cache->size)) {
567 if ((p = util_ldap_cache->nodes[id]) != NULL) {
568 n = (util_url_node_t *)p->payload;
575 ap_rputs(apr_psprintf(r->pool,
577 "<table border='0'>\n"
579 "<td bgcolor='#000000'><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Cache Name:</b></font></td>"
580 "<td bgcolor='#ffffff'><font size='-1' face='Arial,Helvetica' color='#000000'><b>%s (%s)</b></font></td>"
584 cachetype[0] == 'm'? "Main" :
585 (cachetype[0] == 's' ? "Search" :
586 (cachetype[0] == 'c' ? "Compares" : "DNCompares"))), r);
588 switch (cachetype[0]) {
590 if (util_ldap_cache->marktime) {
591 apr_ctime(date_str, util_ldap_cache->marktime);
596 ap_rputs(apr_psprintf(r->pool,
598 "<table border='0'>\n"
600 "<td bgcolor='#000000'><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Size:</b></font></td>"
601 "<td bgcolor='#ffffff'><font size='-1' face='Arial,Helvetica' color='#000000'><b>%ld</b></font></td>"
604 "<td bgcolor='#000000'><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Max Entries:</b></font></td>"
605 "<td bgcolor='#ffffff'><font size='-1' face='Arial,Helvetica' color='#000000'><b>%ld</b></font></td>"
608 "<td bgcolor='#000000'><font size='-1' face='Arial,Helvetica' color='#ffffff'><b># Entries:</b></font></td>"
609 "<td bgcolor='#ffffff'><font size='-1' face='Arial,Helvetica' color='#000000'><b>%ld</b></font></td>"
612 "<td bgcolor='#000000'><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Full Mark:</b></font></td>"
613 "<td bgcolor='#ffffff'><font size='-1' face='Arial,Helvetica' color='#000000'><b>%ld</b></font></td>"
616 "<td bgcolor='#000000'><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Full Mark Time:</b></font></td>"
617 "<td bgcolor='#ffffff'><font size='-1' face='Arial,Helvetica' color='#000000'><b>%s</b></font></td>"
620 util_ldap_cache->size,
621 util_ldap_cache->maxentries,
622 util_ldap_cache->numentries,
623 util_ldap_cache->fullmark,
627 "<table border='0'>\n"
628 "<tr bgcolor='#000000'>\n"
629 "<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>LDAP URL</b></font></td>"
630 "<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Size</b></font></td>"
631 "<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Max Entries</b></font></td>"
632 "<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b># Entries</b></font></td>"
633 "<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Full Mark</b></font></td>"
634 "<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Full Mark Time</b></font></td>"
637 for (i=0; i < util_ldap_cache->size; ++i) {
638 for (p = util_ldap_cache->nodes[i]; p != NULL; p = p->next) {
640 (*util_ldap_cache->display)(r, util_ldap_cache, p->payload);
643 ap_rputs("</table>\n</p>\n", r);
649 "<table border='0'>\n"
650 "<tr bgcolor='#000000'>\n"
651 "<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>LDAP Filter</b></font></td>"
652 "<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>User Name</b></font></td>"
653 "<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Last Bind</b></font></td>"
657 for (i=0; i < n->search_cache->size; ++i) {
658 for (p = n->search_cache->nodes[i]; p != NULL; p = p->next) {
660 (*n->search_cache->display)(r, n->search_cache, p->payload);
664 ap_rputs("</table>\n</p>\n", r);
668 "<table border='0'>\n"
669 "<tr bgcolor='#000000'>\n"
670 "<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>DN</b></font></td>"
671 "<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Attribute</b></font></td>"
672 "<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Value</b></font></td>"
673 "<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Last Compare</b></font></td>"
674 "<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Result</b></font></td>"
678 for (i=0; i < n->compare_cache->size; ++i) {
679 for (p = n->compare_cache->nodes[i]; p != NULL; p = p->next) {
681 (*n->compare_cache->display)(r, n->compare_cache, p->payload);
685 ap_rputs("</table>\n</p>\n", r);
689 "<table border='0'>\n"
690 "<tr bgcolor='#000000'>\n"
691 "<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Require DN</b></font></td>"
692 "<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Actual DN</b></font></td>"
696 for (i=0; i < n->dn_compare_cache->size; ++i) {
697 for (p = n->dn_compare_cache->nodes[i]; p != NULL; p = p->next) {
699 (*n->dn_compare_cache->display)(r, n->dn_compare_cache, p->payload);
703 ap_rputs("</table>\n</p>\n", r);
716 "<table border='0'>\n"
717 "<tr bgcolor='#000000'>\n"
718 "<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Cache Name</b></font></td>"
719 "<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Entries</b></font></td>"
720 "<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Avg. Chain Len.</b></font></td>"
721 "<td colspan='2'><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Hits</b></font></td>"
722 "<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Ins/Rem</b></font></td>"
723 "<td colspan='2'><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Purges</b></font></td>"
724 "<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Avg Purge Time</b></font></td>"
729 id1 = apr_psprintf(pool, argfmt, "main", 0, 0);
730 buf = util_ald_cache_display_stats(r, st->util_ldap_cache, "LDAP URL Cache", id1);
732 for (i=0; i < util_ldap_cache->size; ++i) {
733 for (p = util_ldap_cache->nodes[i],j=0; p != NULL; p = p->next,j++) {
735 n = (util_url_node_t *)p->payload;
737 t1 = apr_psprintf(pool, "%s (Searches)", n->url);
738 t2 = apr_psprintf(pool, "%s (Compares)", n->url);
739 t3 = apr_psprintf(pool, "%s (DNCompares)", n->url);
740 id1 = apr_psprintf(pool, argfmt, "srch", i, j);
741 id2 = apr_psprintf(pool, argfmt, "cmpr", i, j);
742 id3 = apr_psprintf(pool, argfmt, "dncp", i, j);
744 buf = apr_psprintf(pool, "%s\n\n"
749 util_ald_cache_display_stats(r, n->search_cache, t1, id1),
750 util_ald_cache_display_stats(r, n->compare_cache, t2, id2),
751 util_ald_cache_display_stats(r, n->dn_compare_cache, t3, id3)
756 ap_rputs("</table>\n</p>\n", r);
762 #endif /* APU_HAS_LDAP */