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.c: LDAP cache 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 /* ------------------------------------------------------------------ */
34 unsigned long util_ldap_url_node_hash(void *n)
36 util_url_node_t *node = n;
37 return util_ald_hash_string(1, node->url);
40 int util_ldap_url_node_compare(void *a, void *b)
42 util_url_node_t *na = a;
43 util_url_node_t *nb = b;
45 return (strcmp(na->url, nb->url) == 0);
48 void *util_ldap_url_node_copy(util_ald_cache_t *cache, void *c)
50 util_url_node_t *n = c;
51 util_url_node_t *node = util_ald_alloc(cache, sizeof *node);
54 if (!(node->url = util_ald_strdup(cache, n->url))) {
55 util_ald_free(cache, node->url);
58 node->search_cache = n->search_cache;
59 node->compare_cache = n->compare_cache;
60 node->dn_compare_cache = n->dn_compare_cache;
68 void util_ldap_url_node_free(util_ald_cache_t *cache, void *n)
70 util_url_node_t *node = n;
72 util_ald_free(cache, node->url);
73 util_ald_destroy_cache(node->search_cache);
74 util_ald_destroy_cache(node->compare_cache);
75 util_ald_destroy_cache(node->dn_compare_cache);
76 util_ald_free(cache, node);
79 void util_ldap_url_node_display(request_rec *r, util_ald_cache_t *cache, void *n)
81 util_url_node_t *node = n;
82 char date_str[APR_CTIME_LEN];
84 util_ald_cache_t *cache_node;
90 cache_node = node->search_cache;
91 type_str = "Searches";
94 cache_node = node->compare_cache;
95 type_str = "Compares";
99 cache_node = node->dn_compare_cache;
100 type_str = "DN Compares";
104 if (cache_node->marktime) {
105 apr_ctime(date_str, cache_node->marktime);
112 "<td nowrap>%s (%s)</td>"
113 "<td nowrap>%ld</td>"
114 "<td nowrap>%ld</td>"
115 "<td nowrap>%ld</td>"
116 "<td nowrap>%ld</td>"
122 cache_node->maxentries,
123 cache_node->numentries,
124 cache_node->fullmark,
130 /* ------------------------------------------------------------------ */
132 /* Cache functions for search nodes */
133 unsigned long util_ldap_search_node_hash(void *n)
135 util_search_node_t *node = n;
136 return util_ald_hash_string(1, node->username);
139 int util_ldap_search_node_compare(void *a, void *b)
141 util_search_node_t *na = a;
142 util_search_node_t *nb = b;
144 return (strcmp(na->username, nb->username) == 0);
147 void *util_ldap_search_node_copy(util_ald_cache_t *cache, void *c)
149 util_search_node_t *node = c;
150 util_search_node_t *newnode = util_ald_alloc(cache, sizeof *newnode);
157 int k = node->numvals;
159 if (!(newnode->vals = util_ald_alloc(cache, sizeof(char *) * (k+1)))) {
160 util_ldap_search_node_free(cache, newnode);
163 newnode->numvals = node->numvals;
166 if (!(newnode->vals[i] = util_ald_strdup(cache, node->vals[i]))) {
167 util_ldap_search_node_free(cache, newnode);
172 newnode->vals[i] = NULL;
177 newnode->vals = NULL;
179 if (!(newnode->username = util_ald_strdup(cache, node->username)) ||
180 !(newnode->dn = util_ald_strdup(cache, node->dn)) ) {
181 util_ldap_search_node_free(cache, newnode);
185 if(!(newnode->bindpw = util_ald_strdup(cache, node->bindpw))) {
186 util_ldap_search_node_free(cache, newnode);
190 newnode->bindpw = NULL;
192 newnode->lastbind = node->lastbind;
195 return (void *)newnode;
198 void util_ldap_search_node_free(util_ald_cache_t *cache, void *n)
201 util_search_node_t *node = n;
202 int k = node->numvals;
207 util_ald_free(cache, node->vals[i]);
210 util_ald_free(cache, node->vals);
212 util_ald_free(cache, node->username);
213 util_ald_free(cache, node->dn);
214 util_ald_free(cache, node->bindpw);
215 util_ald_free(cache, node);
218 void util_ldap_search_node_display(request_rec *r, util_ald_cache_t *cache, void *n)
220 util_search_node_t *node = n;
221 char date_str[APR_CTIME_LEN];
223 apr_ctime(date_str, node->lastbind);
236 /* ------------------------------------------------------------------ */
238 unsigned long util_ldap_compare_node_hash(void *n)
240 util_compare_node_t *node = n;
241 return util_ald_hash_string(3, node->dn, node->attrib, node->value);
244 int util_ldap_compare_node_compare(void *a, void *b)
246 util_compare_node_t *na = a;
247 util_compare_node_t *nb = b;
249 return (strcmp(na->dn, nb->dn) == 0 &&
250 strcmp(na->attrib, nb->attrib) == 0 &&
251 strcmp(na->value, nb->value) == 0);
254 void *util_ldap_compare_node_copy(util_ald_cache_t *cache, void *c)
256 util_compare_node_t *n = c;
257 util_compare_node_t *node = util_ald_alloc(cache, sizeof *node);
260 if (!(node->dn = util_ald_strdup(cache, n->dn)) ||
261 !(node->attrib = util_ald_strdup(cache, n->attrib)) ||
262 !(node->value = util_ald_strdup(cache, n->value)) ||
263 ((n->subgroupList) && !(node->subgroupList = util_ald_sgl_dup(cache, n->subgroupList)))) {
264 util_ldap_compare_node_free(cache, node);
267 node->lastcompare = n->lastcompare;
268 node->result = n->result;
269 node->sgl_processed = n->sgl_processed;
277 void util_ldap_compare_node_free(util_ald_cache_t *cache, void *n)
279 util_compare_node_t *node = n;
281 util_ald_sgl_free(cache, &(node->subgroupList));
282 util_ald_free(cache, node->dn);
283 util_ald_free(cache, node->attrib);
284 util_ald_free(cache, node->value);
285 util_ald_free(cache, node);
288 void util_ldap_compare_node_display(request_rec *r, util_ald_cache_t *cache, void *n)
290 util_compare_node_t *node = n;
291 char date_str[APR_CTIME_LEN];
293 char *sub_groups_val;
294 char *sub_groups_checked;
296 apr_ctime(date_str, node->lastcompare);
298 if (node->result == LDAP_COMPARE_TRUE) {
299 cmp_result = "LDAP_COMPARE_TRUE";
301 else if (node->result == LDAP_COMPARE_FALSE) {
302 cmp_result = "LDAP_COMPARE_FALSE";
305 cmp_result = apr_itoa(r->pool, node->result);
308 if(node->subgroupList) {
309 sub_groups_val = "Yes";
312 sub_groups_val = "No";
315 if(node->sgl_processed) {
316 sub_groups_checked = "Yes";
319 sub_groups_checked = "No";
341 /* ------------------------------------------------------------------ */
343 unsigned long util_ldap_dn_compare_node_hash(void *n)
345 util_dn_compare_node_t *node = n;
346 return util_ald_hash_string(1, node->reqdn);
349 int util_ldap_dn_compare_node_compare(void *a, void *b)
351 util_dn_compare_node_t *na = a;
352 util_dn_compare_node_t *nb = b;
354 return (strcmp(na->reqdn, nb->reqdn) == 0);
357 void *util_ldap_dn_compare_node_copy(util_ald_cache_t *cache, void *c)
359 util_dn_compare_node_t *n = c;
360 util_dn_compare_node_t *node = util_ald_alloc(cache, sizeof *node);
363 if (!(node->reqdn = util_ald_strdup(cache, n->reqdn)) ||
364 !(node->dn = util_ald_strdup(cache, n->dn))) {
365 util_ldap_dn_compare_node_free(cache, node);
375 void util_ldap_dn_compare_node_free(util_ald_cache_t *cache, void *n)
377 util_dn_compare_node_t *node = n;
378 util_ald_free(cache, node->reqdn);
379 util_ald_free(cache, node->dn);
380 util_ald_free(cache, node);
383 void util_ldap_dn_compare_node_display(request_rec *r, util_ald_cache_t *cache, void *n)
385 util_dn_compare_node_t *node = n;
397 /* ------------------------------------------------------------------ */
398 static apr_status_t util_ldap_cache_module_kill(void *data)
400 util_ldap_state_t *st = data;
402 util_ald_destroy_cache(st->util_ldap_cache);
403 #if APR_HAS_SHARED_MEMORY
404 if (st->cache_rmm != NULL) {
405 apr_rmm_destroy (st->cache_rmm);
406 st->cache_rmm = NULL;
408 if (st->cache_shm != NULL) {
409 apr_status_t result = apr_shm_destroy(st->cache_shm);
410 st->cache_shm = NULL;
417 apr_status_t util_ldap_cache_init(apr_pool_t *pool, util_ldap_state_t *st)
419 #if APR_HAS_SHARED_MEMORY
423 if (st->cache_file) {
424 /* Remove any existing shm segment with this name. */
425 apr_shm_remove(st->cache_file, st->pool);
428 size = APR_ALIGN_DEFAULT(st->cache_bytes);
430 result = apr_shm_create(&st->cache_shm, size, st->cache_file, st->pool);
431 if (result != APR_SUCCESS) {
435 /* Determine the usable size of the shm segment. */
436 size = apr_shm_size_get(st->cache_shm);
438 /* This will create a rmm "handler" to get into the shared memory area */
439 result = apr_rmm_init(&st->cache_rmm, NULL,
440 apr_shm_baseaddr_get(st->cache_shm), size,
442 if (result != APR_SUCCESS) {
448 apr_pool_cleanup_register(st->pool, st , util_ldap_cache_module_kill, apr_pool_cleanup_null);
450 st->util_ldap_cache =
451 util_ald_create_cache(st,
452 st->search_cache_size,
453 util_ldap_url_node_hash,
454 util_ldap_url_node_compare,
455 util_ldap_url_node_copy,
456 util_ldap_url_node_free,
457 util_ldap_url_node_display);
462 #endif /* APR_HAS_LDAP */