2 * Copyright (c) 1993 The Regents of the University of Michigan.
5 * cache.c - local caching support for LDAP
14 static char copyright[] = "@(#) Copyright (c) 1993 The Regents of the University of Michigan.\nAll rights reserved.\n";
18 #include <ac/string.h>
25 #if defined( DOS ) || defined( _WIN32 )
35 #include <sys/types.h>
36 #include <sys/socket.h>
44 static int cache_hash LDAP_P(( BerElement *ber ));
45 static LDAPMessage *msg_dup LDAP_P(( LDAPMessage *msg ));
46 static int request_cmp LDAP_P(( BerElement *req1, BerElement *req2 ));
47 static int chain_contains_dn LDAP_P(( LDAPMessage *msg, char *dn ));
48 static long msg_size LDAP_P(( LDAPMessage *msg ));
49 static void check_cache_memused LDAP_P(( LDAPCache *lc ));
50 static void uncache_entry_or_req LDAP_P(( LDAP *ld, char *dn, int msgid ));
53 ldap_enable_cache( LDAP *ld, long timeout, long maxmem )
55 if ( ld->ld_cache == NULLLDCACHE ) {
56 if (( ld->ld_cache = (LDAPCache *)malloc( sizeof( LDAPCache )))
58 ld->ld_errno = LDAP_NO_MEMORY;
61 (void) memset( ld->ld_cache, 0, sizeof( LDAPCache ));
62 ld->ld_cache->lc_memused = sizeof( LDAPCache );
65 ld->ld_cache->lc_timeout = timeout;
66 ld->ld_cache->lc_maxmem = maxmem;
67 check_cache_memused( ld->ld_cache );
68 ld->ld_cache->lc_enabled = 1;
74 ldap_disable_cache( LDAP *ld )
76 if ( ld->ld_cache != NULLLDCACHE ) {
77 ld->ld_cache->lc_enabled = 0;
84 ldap_set_cache_options( LDAP *ld, unsigned long opts )
86 if ( ld->ld_cache != NULLLDCACHE ) {
87 ld->ld_cache->lc_options = opts;
93 ldap_destroy_cache( LDAP *ld )
95 if ( ld->ld_cache != NULLLDCACHE ) {
96 ldap_flush_cache( ld );
97 free( (char *)ld->ld_cache );
98 ld->ld_cache = NULLLDCACHE;
104 ldap_flush_cache( LDAP *ld )
107 LDAPMessage *m, *next;
109 Debug( LDAP_DEBUG_TRACE, "ldap_flush_cache\n", 0, 0, 0 );
111 if ( ld->ld_cache != NULLLDCACHE ) {
112 /* delete all requests in the queue */
113 for ( m = ld->ld_cache->lc_requests; m != NULLMSG; m = next ) {
117 ld->ld_cache->lc_requests = NULLMSG;
119 /* delete all messages in the cache */
120 for ( i = 0; i < LDAP_CACHE_BUCKETS; ++i ) {
121 for ( m = ld->ld_cache->lc_buckets[ i ];
122 m != NULLMSG; m = next ) {
126 ld->ld_cache->lc_buckets[ i ] = NULLMSG;
128 ld->ld_cache->lc_memused = sizeof( LDAPCache );
134 ldap_uncache_request( LDAP *ld, int msgid )
136 Debug( LDAP_DEBUG_TRACE, "ldap_uncache_request %d ld_cache %lx\n",
137 msgid, (long) ld->ld_cache, 0 );
139 uncache_entry_or_req( ld, NULL, msgid );
144 ldap_uncache_entry( LDAP *ld, char *dn )
146 Debug( LDAP_DEBUG_TRACE, "ldap_uncache_entry %s ld_cache %lx\n",
147 dn, (long) ld->ld_cache, 0 );
149 uncache_entry_or_req( ld, dn, 0 );
154 uncache_entry_or_req( LDAP *ld,
155 char *dn, /* if non-NULL, uncache entry */
156 int msgid ) /* request to uncache (if dn == NULL) */
159 LDAPMessage *m, *prev, *next;
161 Debug( LDAP_DEBUG_TRACE,
162 "ldap_uncache_entry_or_req dn %s msgid %d ld_cache %lx\n",
163 dn, msgid, (long) ld->ld_cache );
165 if ( ld->ld_cache == NULLLDCACHE ) {
169 /* first check the request queue */
171 for ( m = ld->ld_cache->lc_requests; m != NULLMSG; m = next ) {
173 if (( dn != NULL && chain_contains_dn( m, dn )) ||
174 ( dn == NULL && m->lm_msgid == msgid )) {
175 if ( prev == NULLMSG ) {
176 ld->ld_cache->lc_requests = next;
178 prev->lm_next = next;
180 ld->ld_cache->lc_memused -= msg_size( m );
187 /* now check the rest of the cache */
188 for ( i = 0; i < LDAP_CACHE_BUCKETS; ++i ) {
190 for ( m = ld->ld_cache->lc_buckets[ i ]; m != NULLMSG;
193 if (( dn != NULL && chain_contains_dn( m, dn )) ||
194 ( dn == NULL && m->lm_msgid == msgid )) {
195 if ( prev == NULLMSG ) {
196 ld->ld_cache->lc_buckets[ i ] = next;
198 prev->lm_next = next;
200 ld->ld_cache->lc_memused -= msg_size( m );
211 ldap_add_request_to_cache( LDAP *ld, unsigned long msgtype, BerElement *request )
216 Debug( LDAP_DEBUG_TRACE, "ldap_add_request_to_cache\n", 0, 0, 0 );
218 ld->ld_errno = LDAP_SUCCESS;
219 if ( ld->ld_cache == NULLLDCACHE ||
220 ( ld->ld_cache->lc_enabled == 0 )) {
224 if (( new = (LDAPMessage *) calloc( 1, sizeof(LDAPMessage) ))
226 if (( new->lm_ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
230 len = request->ber_ptr - request->ber_buf;
231 if (( new->lm_ber->ber_buf = (char *) malloc( (size_t)len ))
233 ber_free( new->lm_ber, 0 );
235 ld->ld_errno = LDAP_NO_MEMORY;
238 SAFEMEMCPY( new->lm_ber->ber_buf, request->ber_buf,
240 new->lm_ber->ber_ptr = new->lm_ber->ber_buf;
241 new->lm_ber->ber_end = new->lm_ber->ber_buf + len;
242 new->lm_msgid = ld->ld_msgid;
243 new->lm_msgtype = msgtype;;
244 new->lm_next = ld->ld_cache->lc_requests;
245 ld->ld_cache->lc_requests = new;
247 ld->ld_errno = LDAP_NO_MEMORY;
253 ldap_add_result_to_cache( LDAP *ld, LDAPMessage *result )
255 LDAPMessage *m, **mp, *req, *new, *prev;
258 Debug( LDAP_DEBUG_TRACE, "ldap_add_result_to_cache: id %d, type %d\n",
259 result->lm_msgid, result->lm_msgtype, 0 );
261 if ( ld->ld_cache == NULLLDCACHE ||
262 ( ld->ld_cache->lc_enabled == 0 )) {
263 Debug( LDAP_DEBUG_TRACE, "artc: cache disabled\n", 0, 0, 0 );
267 if ( result->lm_msgtype != LDAP_RES_SEARCH_ENTRY &&
268 result->lm_msgtype != LDAP_RES_SEARCH_RESULT &&
269 result->lm_msgtype != LDAP_RES_COMPARE ) {
271 * only cache search and compare operations
273 Debug( LDAP_DEBUG_TRACE,
274 "artc: only caching search & compare operations\n", 0, 0, 0 );
279 * if corresponding request is in the lc_requests list, add this
280 * result to it. if this result completes the results for the
281 * request, add the request/result chain to the cache proper.
284 for ( m = ld->ld_cache->lc_requests; m != NULL; m = m->lm_next ) {
285 if ( m->lm_msgid == result->lm_msgid ) {
291 if ( m != NULLMSG ) { /* found request; add to end of chain */
293 for ( ; m->lm_chain != NULLMSG; m = m->lm_chain )
295 if (( new = msg_dup( result )) != NULLMSG ) {
296 new->lm_chain = NULLMSG;
298 Debug( LDAP_DEBUG_TRACE,
299 "artc: result added to cache request chain\n",
302 if ( result->lm_msgtype == LDAP_RES_SEARCH_RESULT ||
303 result->lm_msgtype == LDAP_RES_COMPARE ) {
305 * this result completes the chain of results
306 * add to cache proper if appropriate
308 keep = 0; /* pessimistic */
309 err = ldap_result2error( ld, result, 0 );
310 if ( err == LDAP_SUCCESS ||
311 ( result->lm_msgtype == LDAP_RES_COMPARE &&
312 ( err == LDAP_COMPARE_FALSE ||
313 err == LDAP_COMPARE_TRUE ||
314 err == LDAP_NO_SUCH_ATTRIBUTE ))) {
318 if ( ld->ld_cache->lc_options == 0 ) {
319 if ( err == LDAP_SIZELIMIT_EXCEEDED ) {
322 } else if (( ld->ld_cache->lc_options &
323 LDAP_CACHE_OPT_CACHEALLERRS ) != 0 ) {
327 if ( prev == NULLMSG ) {
328 ld->ld_cache->lc_requests = req->lm_next;
330 prev->lm_next = req->lm_next;
334 Debug( LDAP_DEBUG_TRACE,
335 "artc: not caching result with error %d\n",
339 mp = &ld->ld_cache->lc_buckets[
340 cache_hash( req->lm_ber ) ];
343 req->lm_time = (long) time( NULL );
344 ld->ld_cache->lc_memused += msg_size( req );
345 check_cache_memused( ld->ld_cache );
346 Debug( LDAP_DEBUG_TRACE,
347 "artc: cached result with error %d\n",
352 Debug( LDAP_DEBUG_TRACE, "artc: msgid not in request list\n",
359 * look in the cache for this request
360 * return 0 if found, -1 if not
361 * if found, the corresponding result messages are added to the incoming
362 * queue with the correct (new) msgid so that subsequent ldap_result calls
366 ldap_check_cache( LDAP *ld, unsigned long msgtype, BerElement *request )
368 LDAPMessage *m, *new, *prev, *next;
371 unsigned long validtime;
373 Debug( LDAP_DEBUG_TRACE, "ldap_check_cache\n", 0, 0, 0 );
375 if ( ld->ld_cache == NULLLDCACHE ||
376 ( ld->ld_cache->lc_enabled == 0 )) {
380 reqber.ber_buf = reqber.ber_ptr = request->ber_buf;
381 reqber.ber_end = request->ber_ptr;
383 validtime = (long)time( NULL ) - ld->ld_cache->lc_timeout;
386 hash = cache_hash( &reqber );
387 for ( m = ld->ld_cache->lc_buckets[ hash ]; m != NULLMSG; m = next ) {
388 Debug( LDAP_DEBUG_TRACE,"cc: examining id %d,type %d\n",
389 m->lm_msgid, m->lm_msgtype, 0 );
390 if ( m->lm_time < validtime ) {
391 /* delete expired message */
393 if ( prev == NULL ) {
394 ld->ld_cache->lc_buckets[ hash ] = next;
396 prev->lm_next = next;
398 Debug( LDAP_DEBUG_TRACE, "cc: expired id %d\n",
400 ld->ld_cache->lc_memused -= msg_size( m );
403 if ( m->lm_msgtype == (int)msgtype &&
404 request_cmp( m->lm_ber, &reqber ) == 0 ) {
412 if ( m == NULLMSG ) {
417 * add duplicates of responses to incoming queue
420 for ( m = m->lm_chain; m != NULLMSG; m = m->lm_chain ) {
421 if (( new = msg_dup( m )) == NULLMSG ) {
425 new->lm_msgid = ld->ld_msgid;
426 new->lm_chain = NULLMSG;
428 new->lm_next = ld->ld_responses;
429 ld->ld_responses = new;
432 prev->lm_chain = new;
435 Debug( LDAP_DEBUG_TRACE, "cc: added type %d\n",
436 new->lm_msgtype, 0, 0 );
439 Debug( LDAP_DEBUG_TRACE, "cc: result returned from cache\n", 0, 0, 0 );
445 cache_hash( BerElement *ber )
451 * just take the length of the packet and mod with # of buckets
454 if ( ber_skip_tag( &bercpy, &len ) == LBER_ERROR
455 || ber_scanf( &bercpy, "x" ) == LBER_ERROR ) {
456 len = 0; /* punt: just return zero */
458 len = bercpy.ber_end - bercpy.ber_ptr;
461 Debug( LDAP_DEBUG_TRACE, "cache_hash: len is %ld, returning %ld\n",
462 len, len % LDAP_CACHE_BUCKETS, 0 );
463 return( (int) ( len % LDAP_CACHE_BUCKETS ));
468 msg_dup( LDAPMessage *msg )
473 if (( new = (LDAPMessage *)malloc( sizeof(LDAPMessage))) != NULL ) {
474 *new = *msg; /* struct copy */
475 if (( new->lm_ber = ber_dup( msg->lm_ber )) == NULLBER ) {
479 len = msg->lm_ber->ber_end - msg->lm_ber->ber_buf;
480 if (( new->lm_ber->ber_buf = (char *) malloc(
481 (size_t)len )) == NULL ) {
482 ber_free( new->lm_ber, 0 );
486 SAFEMEMCPY( new->lm_ber->ber_buf, msg->lm_ber->ber_buf,
488 new->lm_ber->ber_ptr = new->lm_ber->ber_buf +
489 ( msg->lm_ber->ber_ptr - msg->lm_ber->ber_buf );
490 new->lm_ber->ber_end = new->lm_ber->ber_buf + len;
498 request_cmp( BerElement *req1, BerElement *req2 )
503 r1 = *req1; /* struct copies */
507 * skip the enclosing tags (sequence markers) and the msg ids
509 if ( ber_skip_tag( &r1, &len ) == LBER_ERROR || ber_scanf( &r1, "x" )
513 if ( ber_skip_tag( &r2, &len ) == LBER_ERROR || ber_scanf( &r2, "x" )
519 * check remaining length and bytes if necessary
521 if (( len = r1.ber_end - r1.ber_ptr ) != r2.ber_end - r2.ber_ptr ) {
522 return( -1 ); /* different lengths */
524 return( memcmp( r1.ber_ptr, r2.ber_ptr, (size_t)len ));
529 chain_contains_dn( LDAPMessage *msg, char *dn )
539 * first check the base or dn of the request
541 ber = *msg->lm_ber; /* struct copy */
542 if ( ber_scanf( &ber, "{i{a", &msgid, &s ) != LBER_ERROR ) {
543 rc = ( strcasecmp( dn, s ) == 0 ) ? 1 : 0;
550 if ( msg->lm_msgtype == LDAP_REQ_COMPARE ) {
555 * now check the dn of each search result
558 for ( m = msg->lm_chain; m != NULLMSG && rc == 0 ; m = m->lm_chain ) {
559 if ( m->lm_msgtype != LDAP_RES_SEARCH_ENTRY ) {
562 ber = *m->lm_ber; /* struct copy */
563 if ( ber_scanf( &ber, "{a", &s ) != LBER_ERROR ) {
564 rc = ( strcasecmp( dn, s ) == 0 ) ? 1 : 0;
574 msg_size( LDAPMessage *msg )
580 for ( m = msg; m != NULLMSG; m = m->lm_chain ) {
581 size += ( sizeof( LDAPMessage ) + m->lm_ber->ber_end -
582 m->lm_ber->ber_buf );
589 #define THRESHOLD_FACTOR 3 / 4
590 #define SIZE_FACTOR 2 / 3
593 check_cache_memused( LDAPCache *lc )
596 * this routine is called to check if the cache is too big (lc_maxmem >
597 * minimum cache size and lc_memused > lc_maxmem). If too big, it reduces
598 * the cache size to < SIZE_FACTOR * lc_maxmem. The algorithm is as follows:
599 * remove_threshold = lc_timeout seconds;
601 * remove everything older than remove_threshold seconds;
602 * remove_threshold = remove_threshold * THRESHOLD_FACTOR;
603 * } while ( cache size is > SIZE_FACTOR * lc_maxmem )
606 unsigned long remove_threshold, validtime;
607 LDAPMessage *m, *prev, *next;
609 Debug( LDAP_DEBUG_TRACE, "check_cache_memused: %ld bytes in use (%ld max)\n",
610 lc->lc_memused, lc->lc_maxmem, 0 );
612 if ( lc->lc_maxmem <= sizeof( LDAPCache )
613 || lc->lc_memused <= lc->lc_maxmem * SIZE_FACTOR ) {
617 remove_threshold = lc->lc_timeout;
618 while ( lc->lc_memused > lc->lc_maxmem * SIZE_FACTOR ) {
619 validtime = (long)time( NULL ) - remove_threshold;
620 for ( i = 0; i < LDAP_CACHE_BUCKETS; ++i ) {
622 for ( m = lc->lc_buckets[ i ]; m != NULLMSG;
625 if ( m->lm_time < validtime ) {
626 if ( prev == NULLMSG ) {
627 lc->lc_buckets[ i ] = next;
629 prev->lm_next = next;
631 lc->lc_memused -= msg_size( m );
632 Debug( LDAP_DEBUG_TRACE,
641 remove_threshold *= THRESHOLD_FACTOR;
644 Debug( LDAP_DEBUG_TRACE, "ccm: reduced usage to %ld bytes\n",
645 lc->lc_memused, 0, 0 );
648 #endif /* !NO_CACHE */