util_ldap_cache_mgr.c revision 92f8e96490cb38f58db704ec6bf0041069f067f2
/* Copyright 2001-2005 The Apache Software Foundation or its licensors, as
* applicable.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* util_ldap_cache_mgr.c: LDAP cache manager things
*
* Original code from auth_ldap module for Apache v1.3:
* Copyright 1998, 1999 Enbridge Pipelines Inc.
* Copyright 1999-2001 Dave Carrigan
*/
#include "httpd.h"
#include "util_ldap.h"
#include "util_ldap_cache.h"
#include <apr_strings.h>
#if APR_HAS_LDAP
/* only here until strdup is gone */
#include <string.h>
/* here till malloc is gone */
#include <stdlib.h>
static const unsigned long primes[] =
{
11,
19,
37,
73,
109,
163,
251,
367,
557,
823,
1237,
1861,
2777,
4177,
6247,
9371,
14057,
21089,
31627,
47431,
71143,
106721,
160073,
240101,
360163,
540217,
810343,
1215497,
1823231,
2734867,
4102283,
6153409,
9230113,
13845163,
0
};
{
if (ptr)
/* Free in shared memory */
}
else {
if (ptr)
/* Cache shm is not used */
}
#else
if (ptr)
#endif
}
{
if (0 == size)
return NULL;
/* allocate from shared memory */
}
else {
/* Cache shm is not used */
}
#else
#endif
}
{
/* allocate from shared memory */
if (buf) {
return buf;
}
else {
return NULL;
}
} else {
/* Cache shm is not used */
return strdup(s);
}
#else
return strdup(s);
#endif
}
/*
* Computes the hash on a set of strings. The first argument is the number
* of strings to hash, the rest of the args are strings.
* Algorithm taken from glibc.
*/
unsigned long util_ald_hash_string(int nstr, ...)
{
int i;
unsigned long h=0, g;
char *str, *p;
for (i=0; i < nstr; ++i) {
for (p = str; *p; ++p) {
h = ( h << 4 ) + *p;
if ( ( g = h & 0xf0000000 ) ) {
h = h ^ (g >> 24);
h = h ^ g;
}
}
}
return h;
}
/*
Purges a cache that has gotten full. We keep track of the time that we
added the entry that made the cache 3/4 full, then delete all entries
that were added before that time. It's pretty simplistic, but time to
purge is only O(n), which is more important.
*/
{
unsigned long i;
util_cache_node_t *p, *q, **pp;
apr_time_t t;
if (!cache)
return;
p = *pp;
while (p != NULL) {
q = p->next;
util_ald_free(cache, p);
cache->numentries--;
p = *pp = q;
}
else {
p = *pp;
}
}
}
t = apr_time_now();
}
/*
* create caches
*/
{
/* create the three caches */
/* check that all the caches initialised successfully */
/* The contents of this structure will be duplicated in shared
memory during the insert. So use stack memory rather than
pool memory to avoid a memory leak. */
}
return newcurl;
}
long cache_size,
unsigned long (*hashfunc)(void *),
int (*comparefunc)(void *, void *),
{
unsigned long i;
if (cache_size <= 0)
return NULL;
return NULL;
}
else {
}
#else
#endif
if (!cache)
return NULL;
#endif
cache->numentries = 0;
cache->nodes = (util_cache_node_t **)util_ald_alloc(cache, cache->size * sizeof(util_cache_node_t *));
return NULL;
}
cache->last_purge = 0;
return cache;
}
{
unsigned long i;
util_cache_node_t *p, *q;
return;
q = NULL;
while (p != NULL) {
q = p->next;
util_ald_free(cache, p);
p = q;
}
}
}
{
unsigned long hashval;
return NULL;
p = p->next) ;
if (p != NULL) {
return p->payload;
}
else {
return NULL;
}
}
/*
* Insert an item into the cache.
* *** Does not catch duplicates!!! ***
*/
{
unsigned long hashval;
/* sanity check */
return NULL;
}
/* check if we are full - if so, try purge */
/* if the purge was not effective, we leave now to avoid an overflow */
return NULL;
}
}
/* should be safe to add an entry */
return NULL;
}
/* Take a copy of the payload before proceeeding. */
if (!payload) {
return NULL;
}
/* populate the entry */
/* if we reach the full mark, note the time we did so
* for the benefit of the purge function
*/
}
}
{
unsigned long hashval;
util_cache_node_t *p, *q;
return;
p = p->next) {
q = p;
}
/* If p is null, it means that we couldn't find the node, so just return */
if (p == NULL)
return;
if (q == NULL) {
/* We found the node, and it's the first in the list */
}
else {
/* We found the node and it's not the first in the list */
}
util_ald_free(cache, p);
cache->numentries--;
}
{
unsigned long i;
int totchainlen = 0;
int nchains = 0;
double chainlen;
apr_pool_t *p = r->pool;
return "";
}
nchains++;
n = n->next) {
totchainlen++;
}
}
}
if (id) {
buf2 = apr_psprintf(p,
"<a href=\"%s?%s\">%s</a>",
r->uri,
id,
name);
}
else {
}
buf = apr_psprintf(p,
"<tr valign='top'>"
"<td nowrap>%s</td>"
"<td align='right' nowrap>%lu (%.0f%% full)</td>"
"<td align='right'>%.1f</td>"
"<td align='right'>%lu/%lu</td>"
"<td align='right'>%.0f%%</td>"
"<td align='right'>%lu/%lu</td>",
buf2,
char str_ctime[APR_CTIME_LEN];
buf = apr_psprintf(p,
"%s"
"<td align='right'>%lu</td>\n"
"<td align='right' nowrap>%s</td>\n",
buf,
}
else {
buf = apr_psprintf(p,
"%s<td colspan='2' align='center'>(none)</td>\n",
buf);
}
return buf;
}
{
unsigned long i,j;
char *argfmt = "cache=%s&id=%d&off=%d";
char *scanfmt = "cache=%4s&id=%u&off=%u%1s";
util_cache_node_t *p = NULL;
util_url_node_t *n = NULL;
if (!util_ldap_cache) {
return "<tr valign='top'><td nowrap colspan=7>Cache has not been enabled/initialised.</td></tr>";
}
n = (util_url_node_t *)p->payload;
}
else {
buf = "";
}
"<p>\n"
"<table border='0'>\n"
"<tr>\n"
"<td bgcolor='#000000'><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Cache Name:</b></font></td>"
"<td bgcolor='#ffffff'><font size='-1' face='Arial,Helvetica' color='#000000'><b>%s (%s)</b></font></td>"
"</tr>\n"
"</table>\n</p>\n",
buf,
switch (cachetype[0]) {
case 'm':
if (util_ldap_cache->marktime) {
}
else
date_str[0] = 0;
"<p>\n"
"<table border='0'>\n"
"<tr>\n"
"<td bgcolor='#000000'><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Size:</b></font></td>"
"<td bgcolor='#ffffff'><font size='-1' face='Arial,Helvetica' color='#000000'><b>%ld</b></font></td>"
"</tr>\n"
"<tr>\n"
"<td bgcolor='#000000'><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Max Entries:</b></font></td>"
"<td bgcolor='#ffffff'><font size='-1' face='Arial,Helvetica' color='#000000'><b>%ld</b></font></td>"
"</tr>\n"
"<tr>\n"
"<td bgcolor='#000000'><font size='-1' face='Arial,Helvetica' color='#ffffff'><b># Entries:</b></font></td>"
"<td bgcolor='#ffffff'><font size='-1' face='Arial,Helvetica' color='#000000'><b>%ld</b></font></td>"
"</tr>\n"
"<tr>\n"
"<td bgcolor='#000000'><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Full Mark:</b></font></td>"
"<td bgcolor='#ffffff'><font size='-1' face='Arial,Helvetica' color='#000000'><b>%ld</b></font></td>"
"</tr>\n"
"<tr>\n"
"<td bgcolor='#000000'><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Full Mark Time:</b></font></td>"
"<td bgcolor='#ffffff'><font size='-1' face='Arial,Helvetica' color='#000000'><b>%s</b></font></td>"
"</tr>\n"
"</table>\n</p>\n",
date_str), r);
ap_rputs("<p>\n"
"<table border='0'>\n"
"<tr bgcolor='#000000'>\n"
"<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>LDAP URL</b></font></td>"
"<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Size</b></font></td>"
"<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Max Entries</b></font></td>"
"<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b># Entries</b></font></td>"
"<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Full Mark</b></font></td>"
"<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Full Mark Time</b></font></td>"
"</tr>\n", r
);
for (i=0; i < util_ldap_cache->size; ++i) {
}
}
ap_rputs("</table>\n</p>\n", r);
break;
case 's':
ap_rputs("<p>\n"
"<table border='0'>\n"
"<tr bgcolor='#000000'>\n"
"<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>LDAP Filter</b></font></td>"
"<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>User Name</b></font></td>"
"<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Last Bind</b></font></td>"
"</tr>\n", r
);
if (n) {
for (i=0; i < n->search_cache->size; ++i) {
}
}
}
ap_rputs("</table>\n</p>\n", r);
break;
case 'c':
ap_rputs("<p>\n"
"<table border='0'>\n"
"<tr bgcolor='#000000'>\n"
"<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>DN</b></font></td>"
"<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Attribute</b></font></td>"
"<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Value</b></font></td>"
"<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Last Compare</b></font></td>"
"<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Result</b></font></td>"
"</tr>\n", r
);
if (n) {
for (i=0; i < n->compare_cache->size; ++i) {
}
}
}
ap_rputs("</table>\n</p>\n", r);
break;
case 'd':
ap_rputs("<p>\n"
"<table border='0'>\n"
"<tr bgcolor='#000000'>\n"
"<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Require DN</b></font></td>"
"<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Actual DN</b></font></td>"
"</tr>\n", r
);
if (n) {
for (i=0; i < n->dn_compare_cache->size; ++i) {
}
}
}
ap_rputs("</table>\n</p>\n", r);
break;
default:
break;
}
}
else {
buf = "";
}
}
else {
ap_rputs("<p>\n"
"<table border='0'>\n"
"<tr bgcolor='#000000'>\n"
"<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Cache Name</b></font></td>"
"<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Entries</b></font></td>"
"<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Avg. Chain Len.</b></font></td>"
"<td colspan='2'><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Hits</b></font></td>"
"<td colspan='2'><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Purges</b></font></td>"
"<td><font size='-1' face='Arial,Helvetica' color='#ffffff'><b>Avg Purge Time</b></font></td>"
"</tr>\n", r
);
for (i=0; i < util_ldap_cache->size; ++i) {
n = (util_url_node_t *)p->payload;
"%s\n\n"
"%s\n\n"
"%s\n\n",
buf,
);
}
}
ap_rputs("</table>\n</p>\n", r);
}
return buf;
}
#endif /* APR_HAS_LDAP */