e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher/* Licensed to the Apache Software Foundation (ASF) under one or more
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * contributor license agreements. See the NOTICE file distributed with
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * this work for additional information regarding copyright ownership.
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * The ASF licenses this file to You under the Apache License, Version 2.0
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * (the "License"); you may not use this file except in compliance with
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * the License. You may obtain a copy of the License at
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * http://www.apache.org/licenses/LICENSE-2.0
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * Unless required by applicable law or agreed to in writing, software
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * distributed under the License is distributed on an "AS IS" BASIS,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * See the License for the specific language governing permissions and
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * limitations under the License.
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * util_ldap_cache.c: LDAP cache things
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * Original code from auth_ldap module for Apache v1.3:
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * Copyright 1998, 1999 Enbridge Pipelines Inc.
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * Copyright 1999-2001 Dave Carrigan
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher/* ------------------------------------------------------------------ */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherunsigned long util_ldap_url_node_hash(void *n)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return util_ald_hash_string(1, node->url);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherint util_ldap_url_node_compare(void *a, void *b)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallaghervoid *util_ldap_url_node_copy(util_ald_cache_t *cache, void *c)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher util_url_node_t *node = util_ald_alloc(cache, sizeof *node);
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov if (!(node->url = util_ald_strdup(cache, n->url))) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher node->dn_compare_cache = n->dn_compare_cache;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallaghervoid util_ldap_url_node_free(util_ald_cache_t *cache, void *n)
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov util_ald_destroy_cache(node->search_cache);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher util_ald_destroy_cache(node->compare_cache);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher util_ald_destroy_cache(node->dn_compare_cache);
d115f40c7a3999e3cbe705a2ff9cf0fd493f80fbMichal Zidekvoid util_ldap_url_node_display(request_rec *r, util_ald_cache_t *cache, void *n)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher for (x=0;x<3;x++) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_ctime(date_str, cache_node->marktime);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "<tr valign='top'>"
e0404de84c31d2387bb244d018a5cac8d01f8b19Simo Sorce "<td nowrap>%s (%s)</td>"
21d485184df986e1a123f70c689517386e51a5ceMichal Zidek "<td nowrap>%ld</td>"
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "<td nowrap>%ld</td>"
21d485184df986e1a123f70c689517386e51a5ceMichal Zidek "<td nowrap>%ld</td>"
21d485184df986e1a123f70c689517386e51a5ceMichal Zidek "<td nowrap>%ld</td>"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "<td nowrap>%s</td>"
e0404de84c31d2387bb244d018a5cac8d01f8b19Simo Sorce/* ------------------------------------------------------------------ */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher/* Cache functions for search nodes */
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashovunsigned long util_ldap_search_node_hash(void *n)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return util_ald_hash_string(1, node->username);
e0404de84c31d2387bb244d018a5cac8d01f8b19Simo Sorceint util_ldap_search_node_compare(void *a, void *b)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return (strcmp(na->username, nb->username) == 0);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallaghervoid *util_ldap_search_node_copy(util_ald_cache_t *cache, void *c)
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov util_search_node_t *newnode = util_ald_alloc(cache, sizeof *newnode);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* safety check */
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov /* copy vals */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (!(newnode->vals = util_ald_alloc(cache, sizeof(char *) * (k+1)))) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher util_ldap_search_node_free(cache, newnode);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher for (;k;k--) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (!(newnode->vals[i] = util_ald_strdup(cache, node->vals[i]))) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher util_ldap_search_node_free(cache, newnode);
3d8a87081a6cd197acbd355b5a39111669ec2aa6Jakub Hrozek if (!(newnode->username = util_ald_strdup(cache, node->username)) ||
3d8a87081a6cd197acbd355b5a39111669ec2aa6Jakub Hrozek !(newnode->dn = util_ald_strdup(cache, node->dn)) ) {
3d8a87081a6cd197acbd355b5a39111669ec2aa6Jakub Hrozek if(!(newnode->bindpw = util_ald_strdup(cache, node->bindpw))) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return (void *)newnode;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallaghervoid util_ldap_search_node_free(util_ald_cache_t *cache, void *n)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher for (;k;k--,i++) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallaghervoid util_ldap_search_node_display(request_rec *r, util_ald_cache_t *cache, void *n)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "<tr valign='top'>"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "<td nowrap>%s</td>"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "<td nowrap>%s</td>"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "<td nowrap>%s</td>"
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov/* ------------------------------------------------------------------ */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherunsigned long util_ldap_compare_node_hash(void *n)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return util_ald_hash_string(3, node->dn, node->attrib, node->value);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherint util_ldap_compare_node_compare(void *a, void *b)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallaghervoid *util_ldap_compare_node_copy(util_ald_cache_t *cache, void *c)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher util_compare_node_t *node = util_ald_alloc(cache, sizeof *node);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (!(node->dn = util_ald_strdup(cache, n->dn)) ||
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher !(node->attrib = util_ald_strdup(cache, n->attrib)) ||
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher !(node->value = util_ald_strdup(cache, n->value)) ||
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ((n->subgroupList) && !(node->subgroupList = util_ald_sgl_dup(cache, n->subgroupList)))) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallaghervoid util_ldap_compare_node_free(util_ald_cache_t *cache, void *n)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher util_ald_sgl_free(cache, &(node->subgroupList));
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallaghervoid util_ldap_compare_node_display(request_rec *r, util_ald_cache_t *cache, void *n)
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov else if (node->result == LDAP_COMPARE_FALSE) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher cmp_result = apr_itoa(r->pool, node->result);
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov "<tr valign='top'>"
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "<td nowrap>%s</td>"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "<td nowrap>%s</td>"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "<td nowrap>%s</td>"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "<td nowrap>%s</td>"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "<td nowrap>%s</td>"
dea636af4d1902a081ee891f1b19ee2f8729d759Pavel Březina "<td nowrap>%s</td>"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "<td nowrap>%s</td>"
6fb75e297bf7fc83e3db1f5ae8560624656ef319Jan Zeleny/* ------------------------------------------------------------------ */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherunsigned long util_ldap_dn_compare_node_hash(void *n)
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashovint util_ldap_dn_compare_node_compare(void *a, void *b)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return (strcmp(na->reqdn, nb->reqdn) == 0);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallaghervoid *util_ldap_dn_compare_node_copy(util_ald_cache_t *cache, void *c)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher util_dn_compare_node_t *node = util_ald_alloc(cache, sizeof *node);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (!(node->reqdn = util_ald_strdup(cache, n->reqdn)) ||
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher !(node->dn = util_ald_strdup(cache, n->dn))) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher util_ldap_dn_compare_node_free(cache, node);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallaghervoid util_ldap_dn_compare_node_free(util_ald_cache_t *cache, void *n)
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashovvoid util_ldap_dn_compare_node_display(request_rec *r, util_ald_cache_t *cache, void *n)
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "<tr valign='top'>"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "<td nowrap>%s</td>"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "<td nowrap>%s</td>"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher/* ------------------------------------------------------------------ */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherstatic apr_status_t util_ldap_cache_module_kill(void *data)
f5e22261a2ff95f2a61f4f199fffb8de79668110Stephen Gallagher apr_status_t result = apr_shm_destroy(st->cache_shm);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherapr_status_t util_ldap_cache_init(apr_pool_t *pool, util_ldap_state_t *st)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* Remove any existing shm segment with this name. */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher size = APR_ALIGN_DEFAULT(st->cache_bytes);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher result = apr_shm_create(&st->cache_shm, size, st->cache_file, st->pool);
1ea2e8bd370e0dc2f2c3fa09232cf67082ef748dStephen Gallagher /* Determine the usable size of the shm segment. */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* This will create a rmm "handler" to get into the shared memory area */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher result = apr_rmm_init(&st->cache_rmm, NULL,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_shm_baseaddr_get(st->cache_shm), size,
94a66f84bd3c28fcabffeb84c682dccf89d89c2bSumit Bose apr_pool_cleanup_register(st->pool, st , util_ldap_cache_module_kill, apr_pool_cleanup_null);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#endif /* APR_HAS_LDAP */