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 <apr_strings.h>
27 #include "util_ldap.h"
28 #include "util_ldap_cache.h"
32 #if APR_HAS_SHARED_MEMORY
33 #define MODLDAP_SHMEM_CACHE "/tmp/mod_ldap_cache"
36 /* ------------------------------------------------------------------ */
38 unsigned long util_ldap_url_node_hash(void *n)
40 util_url_node_t *node = (util_url_node_t *)n;
41 return util_ald_hash_string(1, node->url);
44 int util_ldap_url_node_compare(void *a, void *b)
46 util_url_node_t *na = (util_url_node_t *)a;
47 util_url_node_t *nb = (util_url_node_t *)b;
49 return(strcmp(na->url, nb->url) == 0);
52 void *util_ldap_url_node_copy(util_ald_cache_t *cache, void *c)
54 util_url_node_t *n = (util_url_node_t *)c;
55 util_url_node_t *node = (util_url_node_t *)util_ald_alloc(cache, sizeof(util_url_node_t));
58 if (!(node->url = util_ald_strdup(cache, n->url))) {
59 util_ald_free(cache, node->url);
62 node->search_cache = n->search_cache;
63 node->compare_cache = n->compare_cache;
64 node->dn_compare_cache = n->dn_compare_cache;
72 void util_ldap_url_node_free(util_ald_cache_t *cache, void *n)
74 util_url_node_t *node = (util_url_node_t *)n;
76 util_ald_free(cache, node->url);
77 util_ald_destroy_cache(node->search_cache);
78 util_ald_destroy_cache(node->compare_cache);
79 util_ald_destroy_cache(node->dn_compare_cache);
80 util_ald_free(cache, node);
83 void util_ldap_url_node_display(request_rec *r, util_ald_cache_t *cache, void *n)
85 util_url_node_t *node = (util_url_node_t *)n;
86 char date_str[APR_CTIME_LEN+1];
89 util_ald_cache_t *cache_node;
95 cache_node = node->search_cache;
96 type_str = "Searches";
99 cache_node = node->compare_cache;
100 type_str = "Compares";
103 cache_node = node->dn_compare_cache;
104 type_str = "DN Compares";
108 if (cache_node->marktime) {
109 apr_ctime(date_str, cache_node->marktime);
114 buf = apr_psprintf(r->pool,
116 "<td nowrap>%s (%s)</td>"
117 "<td nowrap>%ld</td>"
118 "<td nowrap>%ld</td>"
119 "<td nowrap>%ld</td>"
120 "<td nowrap>%ld</td>"
126 cache_node->maxentries,
127 cache_node->numentries,
128 cache_node->fullmark,
136 /* ------------------------------------------------------------------ */
138 /* Cache functions for search nodes */
139 unsigned long util_ldap_search_node_hash(void *n)
141 util_search_node_t *node = (util_search_node_t *)n;
142 return util_ald_hash_string(1, ((util_search_node_t *)(node))->username);
145 int util_ldap_search_node_compare(void *a, void *b)
147 return(strcmp(((util_search_node_t *)a)->username,
148 ((util_search_node_t *)b)->username) == 0);
151 void *util_ldap_search_node_copy(util_ald_cache_t *cache, void *c)
153 util_search_node_t *node = (util_search_node_t *)c;
154 util_search_node_t *newnode = util_ald_alloc(cache, sizeof(util_search_node_t));
161 int k = node->numvals;
163 if (!(newnode->vals = util_ald_alloc(cache, sizeof(char *) * (k+1)))) {
164 util_ldap_search_node_free(cache, newnode);
167 newnode->numvals = node->numvals;
170 if (!(newnode->vals[i] = util_ald_strdup(cache, node->vals[i]))) {
171 util_ldap_search_node_free(cache, newnode);
176 newnode->vals[i] = NULL;
181 newnode->vals = NULL;
183 if (!(newnode->username = util_ald_strdup(cache, node->username)) ||
184 !(newnode->dn = util_ald_strdup(cache, node->dn)) ) {
185 util_ldap_search_node_free(cache, newnode);
189 if(!(newnode->bindpw = util_ald_strdup(cache, node->bindpw))) {
190 util_ldap_search_node_free(cache, newnode);
194 newnode->bindpw = NULL;
196 newnode->lastbind = node->lastbind;
199 return (void *)newnode;
202 void util_ldap_search_node_free(util_ald_cache_t *cache, void *n)
205 util_search_node_t *node = (util_search_node_t *)n;
206 int k = node->numvals;
211 util_ald_free(cache, node->vals[i]);
214 util_ald_free(cache, node->vals);
216 util_ald_free(cache, node->username);
217 util_ald_free(cache, node->dn);
218 util_ald_free(cache, node->bindpw);
219 util_ald_free(cache, node);
222 void util_ldap_search_node_display(request_rec *r, util_ald_cache_t *cache, void *n)
224 util_search_node_t *node = (util_search_node_t *)n;
225 char date_str[APR_CTIME_LEN+1];
228 apr_ctime(date_str, node->lastbind);
230 buf = apr_psprintf(r->pool,
243 /* ------------------------------------------------------------------ */
245 unsigned long util_ldap_compare_node_hash(void *n)
247 util_compare_node_t *node = (util_compare_node_t *)n;
248 return util_ald_hash_string(3, node->dn, node->attrib, node->value);
251 int util_ldap_compare_node_compare(void *a, void *b)
253 util_compare_node_t *na = (util_compare_node_t *)a;
254 util_compare_node_t *nb = (util_compare_node_t *)b;
255 return (strcmp(na->dn, nb->dn) == 0 &&
256 strcmp(na->attrib, nb->attrib) == 0 &&
257 strcmp(na->value, nb->value) == 0);
260 void *util_ldap_compare_node_copy(util_ald_cache_t *cache, void *c)
262 util_compare_node_t *n = (util_compare_node_t *)c;
263 util_compare_node_t *node = (util_compare_node_t *)util_ald_alloc(cache, sizeof(util_compare_node_t));
266 if (!(node->dn = util_ald_strdup(cache, n->dn)) ||
267 !(node->attrib = util_ald_strdup(cache, n->attrib)) ||
268 !(node->value = util_ald_strdup(cache, n->value))) {
269 util_ldap_compare_node_free(cache, node);
272 node->lastcompare = n->lastcompare;
273 node->result = n->result;
281 void util_ldap_compare_node_free(util_ald_cache_t *cache, void *n)
283 util_compare_node_t *node = (util_compare_node_t *)n;
284 util_ald_free(cache, node->dn);
285 util_ald_free(cache, node->attrib);
286 util_ald_free(cache, node->value);
287 util_ald_free(cache, node);
290 void util_ldap_compare_node_display(request_rec *r, util_ald_cache_t *cache, void *n)
292 util_compare_node_t *node = (util_compare_node_t *)n;
293 char date_str[APR_CTIME_LEN+1];
294 char *buf, *cmp_result;
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 buf = apr_psprintf(r->pool,
325 /* ------------------------------------------------------------------ */
327 unsigned long util_ldap_dn_compare_node_hash(void *n)
329 return util_ald_hash_string(1, ((util_dn_compare_node_t *)n)->reqdn);
332 int util_ldap_dn_compare_node_compare(void *a, void *b)
334 return (strcmp(((util_dn_compare_node_t *)a)->reqdn,
335 ((util_dn_compare_node_t *)b)->reqdn) == 0);
338 void *util_ldap_dn_compare_node_copy(util_ald_cache_t *cache, void *c)
340 util_dn_compare_node_t *n = (util_dn_compare_node_t *)c;
341 util_dn_compare_node_t *node = (util_dn_compare_node_t *)util_ald_alloc(cache, sizeof(util_dn_compare_node_t));
343 if (!(node->reqdn = util_ald_strdup(cache, n->reqdn)) ||
344 !(node->dn = util_ald_strdup(cache, n->dn))) {
345 util_ldap_dn_compare_node_free(cache, node);
355 void util_ldap_dn_compare_node_free(util_ald_cache_t *cache, void *n)
357 util_dn_compare_node_t *node = (util_dn_compare_node_t *)n;
358 util_ald_free(cache, node->reqdn);
359 util_ald_free(cache, node->dn);
360 util_ald_free(cache, node);
363 void util_ldap_dn_compare_node_display(request_rec *r, util_ald_cache_t *cache, void *n)
365 util_dn_compare_node_t *node = (util_dn_compare_node_t *)n;
368 buf = apr_psprintf(r->pool,
380 /* ------------------------------------------------------------------ */
381 apr_status_t util_ldap_cache_child_kill(void *data);
382 apr_status_t util_ldap_cache_module_kill(void *data);
384 apr_status_t util_ldap_cache_module_kill(void *data)
386 util_ldap_state_t *st = (util_ldap_state_t *)data;
388 util_ald_destroy_cache(st->util_ldap_cache);
389 #if APR_HAS_SHARED_MEMORY
390 if (st->cache_rmm != NULL) {
391 apr_rmm_destroy (st->cache_rmm);
392 st->cache_rmm = NULL;
394 if (st->cache_shm != NULL) {
395 apr_status_t result = apr_shm_destroy(st->cache_shm);
396 st->cache_shm = NULL;
397 apr_file_remove(st->cache_file, st->pool);
404 apr_status_t util_ldap_cache_init(apr_pool_t *pool, util_ldap_state_t *st)
406 #if APR_HAS_SHARED_MEMORY
410 size = APR_ALIGN_DEFAULT(st->cache_bytes);
412 result = apr_shm_create(&st->cache_shm, size, st->cache_file, st->pool);
413 if (result == APR_EEXIST) {
415 * The cache could have already been created (i.e. we may be a child process). See
416 * if we can attach to the existing shared memory
418 result = apr_shm_attach(&st->cache_shm, st->cache_file, st->pool);
420 if (result != APR_SUCCESS) {
424 /* Determine the usable size of the shm segment. */
425 size = apr_shm_size_get(st->cache_shm);
427 /* This will create a rmm "handler" to get into the shared memory area */
428 result = apr_rmm_init(&st->cache_rmm, NULL,
429 apr_shm_baseaddr_get(st->cache_shm), size,
431 if (result != APR_SUCCESS) {
437 apr_pool_cleanup_register(st->pool, st , util_ldap_cache_module_kill, apr_pool_cleanup_null);
439 st->util_ldap_cache =
440 util_ald_create_cache(st,
441 util_ldap_url_node_hash,
442 util_ldap_url_node_compare,
443 util_ldap_url_node_copy,
444 util_ldap_url_node_free,
445 util_ldap_url_node_display);
450 #endif /* APU_HAS_LDAP */