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+1];
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+1];
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
util_ldap_compare_node_free(cache, node);
266
node->lastcompare = n->lastcompare;
267
node->result = n->result;
275
void util_ldap_compare_node_free(util_ald_cache_t *cache, void *n)
277
util_compare_node_t *node = n;
278
util_ald_free(cache, node->dn);
279
util_ald_free(cache, node->attrib);
280
util_ald_free(cache, node->value);
281
util_ald_free(cache, node);
284
void util_ldap_compare_node_display(request_rec *r, util_ald_cache_t *cache, void *n)
286
util_compare_node_t *node = n;
287
char date_str[APR_CTIME_LEN+1];
290
apr_ctime(date_str, node->lastcompare);
292
if (node->result == LDAP_COMPARE_TRUE) {
293
cmp_result = "LDAP_COMPARE_TRUE";
295
else if (node->result == LDAP_COMPARE_FALSE) {
296
cmp_result = "LDAP_COMPARE_FALSE";
299
cmp_result = apr_itoa(r->pool, node->result);
317
/* ------------------------------------------------------------------ */
319
unsigned long util_ldap_dn_compare_node_hash(void *n)
321
util_dn_compare_node_t *node = n;
322
return util_ald_hash_string(1, node->reqdn);
325
int util_ldap_dn_compare_node_compare(void *a, void *b)
327
util_dn_compare_node_t *na = a;
328
util_dn_compare_node_t *nb = b;
330
return (strcmp(na->reqdn, nb->reqdn) == 0);
333
void *util_ldap_dn_compare_node_copy(util_ald_cache_t *cache, void *c)
335
util_dn_compare_node_t *n = c;
336
util_dn_compare_node_t *node = util_ald_alloc(cache, sizeof *node);
339
if (!(node->reqdn = util_ald_strdup(cache, n->reqdn)) ||
340
!(node->dn = util_ald_strdup(cache, n->dn))) {
341
util_ldap_dn_compare_node_free(cache, node);
351
void util_ldap_dn_compare_node_free(util_ald_cache_t *cache, void *n)
353
util_dn_compare_node_t *node = n;
354
util_ald_free(cache, node->reqdn);
355
util_ald_free(cache, node->dn);
356
util_ald_free(cache, node);
359
void util_ldap_dn_compare_node_display(request_rec *r, util_ald_cache_t *cache, void *n)
361
util_dn_compare_node_t *node = n;
373
/* ------------------------------------------------------------------ */
374
static apr_status_t util_ldap_cache_module_kill(void *data)
376
util_ldap_state_t *st = data;
378
util_ald_destroy_cache(st->util_ldap_cache);
379
#if APR_HAS_SHARED_MEMORY
380
if (st->cache_rmm != NULL) {
381
apr_rmm_destroy (st->cache_rmm);
382
st->cache_rmm = NULL;
384
if (st->cache_shm != NULL) {
385
apr_status_t result = apr_shm_destroy(st->cache_shm);
386
st->cache_shm = NULL;
393
apr_status_t util_ldap_cache_init(apr_pool_t *pool, util_ldap_state_t *st)
395
#if APR_HAS_SHARED_MEMORY
399
if (st->cache_file) {
400
/* Remove any existing shm segment with this name. */
401
apr_shm_remove(st->cache_file, st->pool);
404
size = APR_ALIGN_DEFAULT(st->cache_bytes);
406
result = apr_shm_create(&st->cache_shm, size, st->cache_file, st->pool);
407
if (result != APR_SUCCESS) {
411
/* Determine the usable size of the shm segment. */
412
size = apr_shm_size_get(st->cache_shm);
414
/* This will create a rmm "handler" to get into the shared memory area */
415
result = apr_rmm_init(&st->cache_rmm, NULL,
416
apr_shm_baseaddr_get(st->cache_shm), size,
418
if (result != APR_SUCCESS) {
424
apr_pool_cleanup_register(st->pool, st , util_ldap_cache_module_kill, apr_pool_cleanup_null);
426
st->util_ldap_cache =
427
util_ald_create_cache(st,
428
st->search_cache_size,
429
util_ldap_url_node_hash,
430
util_ldap_url_node_compare,
431
util_ldap_url_node_copy,
432
util_ldap_url_node_free,
433
util_ldap_url_node_display);
438
#endif /* APR_HAS_LDAP */