Apache HTTPD
util_ldap_cache.c
Go to the documentation of this file.
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/*
18 * util_ldap_cache.c: LDAP cache things
19 *
20 * Original code from auth_ldap module for Apache v1.3:
21 * Copyright 1998, 1999 Enbridge Pipelines Inc.
22 * Copyright 1999-2001 Dave Carrigan
23 */
24
25#include "httpd.h"
26#include "util_ldap.h"
27#include "util_ldap_cache.h"
28#include <apr_strings.h>
29
30#if APR_HAS_LDAP
31
32/* ------------------------------------------------------------------ */
33
34unsigned long util_ldap_url_node_hash(void *n)
35{
36 util_url_node_t *node = n;
37 return util_ald_hash_string(1, node->url);
38}
39
40int util_ldap_url_node_compare(void *a, void *b)
41{
44
45 return (strcmp(na->url, nb->url) == 0);
46}
47
49{
51 util_url_node_t *node = util_ald_alloc(cache, sizeof *node);
52
53 if (node) {
54 if (!(node->url = util_ald_strdup(cache, n->url))) {
55 util_ald_free(cache, node);
56 return NULL;
57 }
58 node->search_cache = n->search_cache;
59 node->compare_cache = n->compare_cache;
60 node->dn_compare_cache = n->dn_compare_cache;
61 return node;
62 }
63 else {
64 return NULL;
65 }
66}
67
69{
70 util_url_node_t *node = n;
71
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);
77}
78
80{
81 util_url_node_t *node = n;
83 const char *type_str;
85 int x;
86
87 for (x=0;x<3;x++) {
88 switch (x) {
89 case 0:
90 cache_node = node->search_cache;
91 type_str = "Searches";
92 break;
93 case 1:
94 cache_node = node->compare_cache;
95 type_str = "Compares";
96 break;
97 case 2:
98 default:
99 cache_node = node->dn_compare_cache;
100 type_str = "DN Compares";
101 break;
102 }
103
104 if (cache_node->marktime) {
105 apr_ctime(date_str, cache_node->marktime);
106 }
107 else
108 date_str[0] = 0;
109
111 "<tr valign='top'>"
112 "<td nowrap>%s (%s)</td>"
113 "<td nowrap>%ld</td>"
114 "<td nowrap>%ld</td>"
115 "<td nowrap>%ld</td>"
116 "<td nowrap>%" APR_TIME_T_FMT "</td>"
117 "<td nowrap>%ld</td>"
118 "<td nowrap>%s</td>"
119 "</tr>",
120 node->url,
121 type_str,
122 cache_node->size,
123 cache_node->maxentries,
124 cache_node->numentries,
126 cache_node->fullmark,
127 date_str);
128 }
129
130}
131
132/* ------------------------------------------------------------------ */
133
134/* Cache functions for search nodes */
135unsigned long util_ldap_search_node_hash(void *n)
136{
137 util_search_node_t *node = n;
138 return util_ald_hash_string(1, node->username);
139}
140
141int util_ldap_search_node_compare(void *a, void *b)
142{
145
146 return (strcmp(na->username, nb->username) == 0);
147}
148
150{
151 util_search_node_t *node = c;
153
154 /* safety check */
155 if (newnode) {
156
157 /* copy vals */
158 if (node->vals) {
159 int k = node->numvals;
160 int i = 0;
161 if (!(newnode->vals = util_ald_alloc(cache, sizeof(char *) * (k+1)))) {
163 return NULL;
164 }
165 newnode->numvals = node->numvals;
166 for (;k;k--) {
167 if (node->vals[i]) {
168 if (!(newnode->vals[i] = util_ald_strdup(cache, node->vals[i]))) {
170 return NULL;
171 }
172 }
173 else
174 newnode->vals[i] = NULL;
175 i++;
176 }
177 }
178 else {
179 newnode->vals = NULL;
180 }
181 if (!(newnode->username = util_ald_strdup(cache, node->username)) ||
182 !(newnode->dn = util_ald_strdup(cache, node->dn)) ) {
184 return NULL;
185 }
186 if (node->bindpw) {
187 if (!(newnode->bindpw = util_ald_strdup(cache, node->bindpw))) {
189 return NULL;
190 }
191 } else {
192 newnode->bindpw = NULL;
193 }
194 newnode->lastbind = node->lastbind;
195
196 }
197 return (void *)newnode;
198}
199
201{
202 int i = 0;
203 util_search_node_t *node = n;
204 int k = node->numvals;
205
206 if (node->vals) {
207 for (;k;k--,i++) {
208 if (node->vals[i]) {
209 util_ald_free(cache, node->vals[i]);
210 }
211 }
212 util_ald_free(cache, node->vals);
213 }
214 util_ald_free(cache, node->username);
215 util_ald_free(cache, node->dn);
216 util_ald_free(cache, node->bindpw);
217 util_ald_free(cache, node);
218}
219
221{
222 util_search_node_t *node = n;
224
225 apr_ctime(date_str, node->lastbind);
226
228 "<tr valign='top'>"
229 "<td nowrap>%s</td>"
230 "<td nowrap>%s</td>"
231 "<td nowrap>%s</td>"
232 "</tr>",
233 ap_escape_html(r->pool, node->username),
234 ap_escape_html(r->pool, node->dn),
235 date_str);
236}
237
238/* ------------------------------------------------------------------ */
239
240unsigned long util_ldap_compare_node_hash(void *n)
241{
242 util_compare_node_t *node = n;
243 return util_ald_hash_string(3, node->dn, node->attrib, node->value);
244}
245
246int util_ldap_compare_node_compare(void *a, void *b)
247{
250
251 return (strcmp(na->dn, nb->dn) == 0 &&
252 strcmp(na->attrib, nb->attrib) == 0 &&
253 strcmp(na->value, nb->value) == 0);
254}
255
257{
259 util_compare_node_t *node = util_ald_alloc(cache, sizeof *node);
260
261 if (node) {
262 if (!(node->dn = util_ald_strdup(cache, n->dn)) ||
263 !(node->attrib = util_ald_strdup(cache, n->attrib)) ||
264 !(node->value = util_ald_strdup(cache, n->value)) ||
265 ((n->subgroupList) && !(node->subgroupList = util_ald_sgl_dup(cache, n->subgroupList)))) {
267 return NULL;
268 }
269 node->lastcompare = n->lastcompare;
270 node->result = n->result;
271 node->sgl_processed = n->sgl_processed;
272 return node;
273 }
274 else {
275 return NULL;
276 }
277}
278
280{
281 util_compare_node_t *node = n;
282
283 util_ald_sgl_free(cache, &(node->subgroupList));
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);
288}
289
291{
292 util_compare_node_t *node = n;
294 char *cmp_result;
295 char *sub_groups_val;
296 char *sub_groups_checked;
297
298 apr_ctime(date_str, node->lastcompare);
299
300 if (node->result == LDAP_COMPARE_TRUE) {
301 cmp_result = "LDAP_COMPARE_TRUE";
302 }
303 else if (node->result == LDAP_COMPARE_FALSE) {
304 cmp_result = "LDAP_COMPARE_FALSE";
305 }
306 else {
307 cmp_result = apr_itoa(r->pool, node->result);
308 }
309
310 if (node->subgroupList) {
311 sub_groups_val = "Yes";
312 }
313 else {
314 sub_groups_val = "No";
315 }
316
317 if (node->sgl_processed) {
318 sub_groups_checked = "Yes";
319 }
320 else {
321 sub_groups_checked = "No";
322 }
323
325 "<tr valign='top'>"
326 "<td nowrap>%s</td>"
327 "<td nowrap>%s</td>"
328 "<td nowrap>%s</td>"
329 "<td nowrap>%s</td>"
330 "<td nowrap>%s</td>"
331 "<td nowrap>%s</td>"
332 "<td nowrap>%s</td>"
333 "</tr>",
334 ap_escape_html(r->pool, node->dn),
335 ap_escape_html(r->pool, node->attrib),
336 ap_escape_html(r->pool, node->value),
337 date_str,
341}
342
343/* ------------------------------------------------------------------ */
344
345unsigned long util_ldap_dn_compare_node_hash(void *n)
346{
348 return util_ald_hash_string(1, node->reqdn);
349}
350
351int util_ldap_dn_compare_node_compare(void *a, void *b)
352{
355
356 return (strcmp(na->reqdn, nb->reqdn) == 0);
357}
358
360{
362 util_dn_compare_node_t *node = util_ald_alloc(cache, sizeof *node);
363
364 if (node) {
365 if (!(node->reqdn = util_ald_strdup(cache, n->reqdn)) ||
366 !(node->dn = util_ald_strdup(cache, n->dn))) {
368 return NULL;
369 }
370 return node;
371 }
372 else {
373 return NULL;
374 }
375}
376
378{
380 util_ald_free(cache, node->reqdn);
381 util_ald_free(cache, node->dn);
382 util_ald_free(cache, node);
383}
384
386{
388
390 "<tr valign='top'>"
391 "<td nowrap>%s</td>"
392 "<td nowrap>%s</td>"
393 "</tr>",
394 ap_escape_html(r->pool, node->reqdn),
395 ap_escape_html(r->pool, node->dn));
396}
397
398
399/* ------------------------------------------------------------------ */
401{
403
404 util_ald_destroy_cache(st->util_ldap_cache);
405#if APR_HAS_SHARED_MEMORY
406 if (st->cache_rmm != NULL) {
407 apr_rmm_destroy (st->cache_rmm);
408 st->cache_rmm = NULL;
409 }
410 if (st->cache_shm != NULL) {
412 st->cache_shm = NULL;
413 return result;
414 }
415#endif
416 return APR_SUCCESS;
417}
418
420{
421#if APR_HAS_SHARED_MEMORY
424
425 if (st->cache_bytes > 0) {
426 if (st->cache_file) {
427 /* Remove any existing shm segment with this name. */
428 apr_shm_remove(st->cache_file, st->pool);
429 }
430
431 size = APR_ALIGN_DEFAULT(st->cache_bytes);
432
433 result = apr_shm_create(&st->cache_shm, size, st->cache_file, st->pool);
434 if (result != APR_SUCCESS) {
435 return result;
436 }
437
438 /* Determine the usable size of the shm segment. */
439 size = apr_shm_size_get(st->cache_shm);
440
441 /* This will create a rmm "handler" to get into the shared memory area */
442 result = apr_rmm_init(&st->cache_rmm, NULL,
443 apr_shm_baseaddr_get(st->cache_shm), size,
444 st->pool);
445 if (result != APR_SUCCESS) {
446 return result;
447 }
448 }
449
450#endif
451
453
454 st->util_ldap_cache =
456 st->search_cache_size,
457 st->search_cache_ttl,
463 return APR_SUCCESS;
464}
465
466
467#endif /* APR_HAS_LDAP */
int n
Definition ap_regex.h:278
APR Strings library.
request_rec * r
int ap_rprintf(request_rec *r, const char *fmt,...) __attribute__((format(printf
apr_bucket apr_bucket_brigade * a
#define ap_escape_html(p, s)
Definition httpd.h:1860
apr_size_t size
const char int apr_pool_t * pool
Definition apr_cstr.h:84
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
void * data
apr_array_header_t ** result
#define APR_ALIGN_DEFAULT(size)
apr_vformatter_buff_t * c
Definition apr_lib.h:175
apr_pool_t * b
Definition apr_pools.h:529
#define APR_CTIME_LEN
Definition apr_time.h:198
#define APR_TIME_T_FMT
Definition apr_time.h:52
#define apr_time_sec(time)
Definition apr_time.h:63
HTTP Daemon routines.
return NULL
Definition mod_so.c:359
int i
Definition mod_so.c:347
A structure that represents the current request.
Definition httpd.h:845
apr_pool_t * pool
Definition httpd.h:847
apr_status_t apr_ctime(char *date_str, apr_time_t t)
Definition timestr.c:90
Apache LDAP library.
This switches LDAP support on or off.