2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2003-2009 The OpenLDAP Foundation.
5 * Portions Copyright 2003 IBM Corporation.
6 * Portions Copyright 2003-2009 Symas Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by Apurva Kumar for inclusion
19 * in OpenLDAP Software and subsequently rewritten by Howard Chu.
24 #ifdef SLAPD_OVER_PROXYCACHE
28 #include <ac/string.h>
36 #include "../back-monitor/back-monitor.h"
42 * Control that allows to access the private DB
43 * instead of the public one
45 #define PCACHE_CONTROL_PRIVDB "1.3.6.1.4.1.4203.666.11.9.5.1"
48 * Extended Operation that allows to remove a query from the cache
50 #define PCACHE_EXOP_QUERY_DELETE "1.3.6.1.4.1.4203.666.11.9.6.1"
55 #define PCACHE_MONITOR
58 /* query cache structs */
61 typedef struct Query_s {
62 Filter* filter; /* Search Filter */
63 struct berval base; /* Search Base */
64 int scope; /* Search scope */
67 struct query_template_s;
69 typedef struct Qbase_s {
70 Avlnode *scopes[4]; /* threaded AVL trees of cached queries */
75 /* struct representing a cached query */
76 typedef struct cached_query_s {
81 struct berval q_uuid; /* query identifier */
83 struct query_template_s *qtemp; /* template of the query */
84 time_t expiry_time; /* time till the query is considered invalid */
85 time_t refresh_time; /* time till the query is refreshed */
86 time_t bindref_time; /* time till the bind is refreshed */
87 unsigned long answerable_cnt; /* how many times it was answerable */
88 int refcnt; /* references since last refresh */
89 ldap_pvt_thread_mutex_t answerable_cnt_mutex;
90 struct cached_query_s *next; /* next query in the template */
91 struct cached_query_s *prev; /* previous query in the template */
92 struct cached_query_s *lru_up; /* previous query in the LRU list */
93 struct cached_query_s *lru_down; /* next query in the LRU list */
94 ldap_pvt_thread_rdwr_t rwlock;
100 * ldap:///<base>??<scope>?<filter>?x-uuid=<uid>,x-template=<template>,x-attrset=<attrset>,x-expiry=<expiry>,x-refresh=<refresh>
102 * <base> ::= CachedQuery.qbase->base
103 * <scope> ::= CachedQuery.scope
104 * <filter> ::= filter2bv(CachedQuery.filter)
105 * <uuid> ::= CachedQuery.q_uuid
106 * <attrset> ::= CachedQuery.qtemp->attr_set_index
107 * <expiry> ::= CachedQuery.expiry_time
108 * <refresh> ::= CachedQuery.refresh_time
110 * quick hack: parse URI, call add_query() and then fix
111 * CachedQuery.expiry_time and CachedQuery.q_uuid
113 * NOTE: if the <attrset> changes, all stored URLs will be invalidated.
117 * Represents a set of projected attributes.
121 struct query_template_s *templates;
122 AttributeName* attrs; /* specifies the set */
124 #define PC_CONFIGURED (0x1)
125 #define PC_REFERENCED (0x2)
126 #define PC_GOT_OC (0x4)
127 int count; /* number of attributes */
130 /* struct representing a query template
131 * e.g. template string = &(cn=)(mail=)
133 typedef struct query_template_s {
134 struct query_template_s *qtnext;
135 struct query_template_s *qmnext;
138 CachedQuery* query; /* most recent query cached for the template */
139 CachedQuery* query_last; /* oldest query cached for the template */
140 ldap_pvt_thread_rdwr_t t_rwlock; /* Rd/wr lock for accessing queries in the template */
141 struct berval querystr; /* Filter string corresponding to the QT */
142 struct berval bindbase; /* base DN for Bind request */
143 struct berval bindfilterstr; /* Filter string for Bind request */
144 struct berval bindftemp; /* bind filter template */
146 AttributeDescription **bindfattrs; /* attrs to substitute in ftemp */
148 int bindnattrs; /* number of bindfattrs */
150 int attr_set_index; /* determines the projected attributes */
151 int no_of_queries; /* Total number of queries in the template */
152 time_t ttl; /* TTL for the queries of this template */
153 time_t negttl; /* TTL for negative results */
154 time_t limitttl; /* TTL for sizelimit exceeding results */
155 time_t ttr; /* time to refresh */
156 time_t bindttr; /* TTR for cached binds */
157 struct attr_set t_attrs; /* filter attrs + attr_set */
165 } pc_caching_reason_t;
167 static const char *pc_caching_reason_str[] = {
176 struct query_manager_s;
178 /* prototypes for functions for 1) query containment
179 * 2) query addition, 3) cache replacement
181 typedef CachedQuery *(QCfunc)(Operation *op, struct query_manager_s*,
182 Query*, QueryTemplate*);
183 typedef CachedQuery *(AddQueryfunc)(Operation *op, struct query_manager_s*,
184 Query*, QueryTemplate*, pc_caching_reason_t, int wlock);
185 typedef void (CRfunc)(struct query_manager_s*, struct berval*);
187 /* LDAP query cache */
188 typedef struct query_manager_s {
189 struct attr_set* attr_sets; /* possible sets of projected attributes */
190 QueryTemplate* templates; /* cacheable templates */
192 CachedQuery* lru_top; /* top and bottom of LRU list */
193 CachedQuery* lru_bottom;
195 ldap_pvt_thread_mutex_t lru_mutex; /* mutex for accessing LRU list */
197 /* Query cache methods */
198 QCfunc *qcfunc; /* Query containment*/
199 CRfunc *crfunc; /* cache replacement */
200 AddQueryfunc *addfunc; /* add query */
203 /* LDAP query cache manager */
204 typedef struct cache_manager_s {
205 BackendDB db; /* underlying database */
206 unsigned long num_cached_queries; /* total number of cached queries */
207 unsigned long max_queries; /* upper bound on # of cached queries */
208 int save_queries; /* save cached queries across restarts */
209 int check_cacheability; /* check whether a query is cacheable */
210 int numattrsets; /* number of attribute sets */
211 int cur_entries; /* current number of entries cached */
212 int max_entries; /* max number of entries cached */
213 int num_entries_limit; /* max # of entries in a cacheable query */
215 char response_cb; /* install the response callback
216 * at the tail of the callback list */
217 #define PCACHE_RESPONSE_CB_HEAD 0
218 #define PCACHE_RESPONSE_CB_TAIL 1
219 char defer_db_open; /* defer open for online add */
220 char cache_binds; /* cache binds or just passthru */
222 time_t cc_period; /* interval between successive consistency checks (sec) */
223 #define PCACHE_CC_PAUSED 1
224 #define PCACHE_CC_OFFLINE 2
228 ldap_pvt_thread_mutex_t cache_mutex;
230 query_manager* qm; /* query cache managed by the cache manager */
232 #ifdef PCACHE_MONITOR
234 struct berval monitor_ndn;
235 #endif /* PCACHE_MONITOR */
238 #ifdef PCACHE_MONITOR
239 static int pcache_monitor_db_init( BackendDB *be );
240 static int pcache_monitor_db_open( BackendDB *be );
241 static int pcache_monitor_db_close( BackendDB *be );
242 static int pcache_monitor_db_destroy( BackendDB *be );
243 #endif /* PCACHE_MONITOR */
245 static int pcache_debug;
247 #ifdef PCACHE_CONTROL_PRIVDB
248 static int privDB_cid;
249 #endif /* PCACHE_CONTROL_PRIVDB */
251 static AttributeDescription *ad_queryId, *ad_cachedQueryURL;
253 #ifdef PCACHE_MONITOR
254 static AttributeDescription *ad_numQueries, *ad_numEntries;
255 static ObjectClass *oc_olmPCache;
256 #endif /* PCACHE_MONITOR */
262 { "PCacheOID", "1.3.6.1.4.1.4203.666.11.9.1" },
263 { "PCacheAttributes", "PCacheOID:1" },
264 { "PCacheObjectClasses", "PCacheOID:2" },
271 AttributeDescription **adp;
273 { "( PCacheAttributes:1 "
274 "NAME 'pcacheQueryID' "
275 "DESC 'ID of query the entry belongs to, formatted as a UUID' "
276 "EQUALITY octetStringMatch "
277 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40{64} "
278 "NO-USER-MODIFICATION "
279 "USAGE directoryOperation )",
281 { "( PCacheAttributes:2 "
282 "NAME 'pcacheQueryURL' "
283 "DESC 'URI describing a cached query' "
284 "EQUALITY caseExactMatch "
285 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 "
286 "NO-USER-MODIFICATION "
287 "USAGE directoryOperation )",
288 &ad_cachedQueryURL },
289 #ifdef PCACHE_MONITOR
290 { "( PCacheAttributes:3 "
291 "NAME 'pcacheNumQueries' "
292 "DESC 'Number of cached queries' "
293 "EQUALITY integerMatch "
294 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
295 "NO-USER-MODIFICATION "
296 "USAGE directoryOperation )",
298 { "( PCacheAttributes:4 "
299 "NAME 'pcacheNumEntries' "
300 "DESC 'Number of cached entries' "
301 "EQUALITY integerMatch "
302 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
303 "NO-USER-MODIFICATION "
304 "USAGE directoryOperation )",
306 #endif /* PCACHE_MONITOR */
315 #ifdef PCACHE_MONITOR
316 /* augments an existing object, so it must be AUXILIARY */
317 { "( PCacheObjectClasses:1 "
318 "NAME ( 'olmPCache' ) "
322 "$ pcacheNumQueries "
323 "$ pcacheNumEntries "
326 #endif /* PCACHE_MONITOR */
335 struct berval *fstr );
342 QueryTemplate *templ,
343 pc_caching_reason_t why,
350 struct berval *query_uuid );
353 * Turn a cached query into its URL representation
356 query2url( Operation *op, CachedQuery *q, struct berval *urlbv, int dolock )
358 struct berval bv_scope,
360 char attrset_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
361 expiry_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
362 refresh_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
363 answerable_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
365 ber_len_t attrset_len,
371 ldap_pvt_thread_rdwr_rlock( &q->rwlock );
374 ldap_pvt_scope2bv( q->scope, &bv_scope );
375 filter2bv_x( op, q->filter, &bv_filter );
376 attrset_len = sprintf( attrset_buf,
377 "%lu", (unsigned long)q->qtemp->attr_set_index );
378 expiry_len = sprintf( expiry_buf,
379 "%lu", (unsigned long)q->expiry_time );
380 answerable_len = snprintf( answerable_buf, sizeof( answerable_buf ),
381 "%lu", q->answerable_cnt );
382 if ( q->refresh_time )
383 refresh_len = sprintf( refresh_buf,
384 "%lu", (unsigned long)q->refresh_time );
388 urlbv->bv_len = STRLENOF( "ldap:///" )
389 + q->qbase->base.bv_len
394 + STRLENOF( "?x-uuid=" )
396 + STRLENOF( ",x-attrset=" )
398 + STRLENOF( ",x-expiry=" )
400 + STRLENOF( ",x-answerable=" )
403 urlbv->bv_len += STRLENOF( ",x-refresh=" )
406 ptr = urlbv->bv_val = ber_memalloc_x( urlbv->bv_len + 1, op->o_tmpmemctx );
407 ptr = lutil_strcopy( ptr, "ldap:///" );
408 ptr = lutil_strcopy( ptr, q->qbase->base.bv_val );
409 ptr = lutil_strcopy( ptr, "??" );
410 ptr = lutil_strcopy( ptr, bv_scope.bv_val );
411 ptr = lutil_strcopy( ptr, "?" );
412 ptr = lutil_strcopy( ptr, bv_filter.bv_val );
413 ptr = lutil_strcopy( ptr, "?x-uuid=" );
414 ptr = lutil_strcopy( ptr, q->q_uuid.bv_val );
415 ptr = lutil_strcopy( ptr, ",x-attrset=" );
416 ptr = lutil_strcopy( ptr, attrset_buf );
417 ptr = lutil_strcopy( ptr, ",x-expiry=" );
418 ptr = lutil_strcopy( ptr, expiry_buf );
419 ptr = lutil_strcopy( ptr, ",x-answerable=" );
420 ptr = lutil_strcopy( ptr, answerable_buf );
422 ptr = lutil_strcopy( ptr, ",x-refresh=" );
423 ptr = lutil_strcopy( ptr, refresh_buf );
426 ber_memfree_x( bv_filter.bv_val, op->o_tmpmemctx );
429 ldap_pvt_thread_rdwr_runlock( &q->rwlock );
435 /* Find and record the empty filter clauses */
438 ftemp_attrs( struct berval *ftemp, struct berval *template,
439 AttributeDescription ***ret, const char **text )
445 AttributeDescription *ad;
446 AttributeDescription **descs = NULL;
449 temp2 = ch_malloc( ftemp->bv_len );
456 while ( *p1 == '(' || *p1 == '&' || *p1 == '|' || *p1 == ')' )
459 p2 = strchr( p1, '=' );
463 AC_MEMCPY( t1, p1, i );
467 if ( p2[-1] == '<' || p2[-1] == '>' ) p2--;
471 i = slap_bv2ad( &bv, &ad, text );
476 if ( *p2 == '<' || *p2 == '>' ) p2++;
477 if ( p2[1] != ')' ) {
479 while ( *p2 != ')' ) p2++;
484 descs = (AttributeDescription **)ch_realloc(descs,
485 (attr_cnt + 2)*sizeof(AttributeDescription *));
487 descs[attr_cnt++] = ad;
492 descs[attr_cnt] = NULL;
494 template->bv_val = temp2;
495 template->bv_len = t1 - temp2;
500 template_attrs( char *template, struct attr_set *set, AttributeName **ret,
511 AttributeDescription *ad;
512 AttributeName *attrs;
518 attrs = ch_malloc(( set->count+1 )*sizeof(AttributeName));
519 for ( i=0; i < set->count; i++ )
520 attrs[i] = set->attrs[i];
522 alluser = an_find( attrs, slap_bv_all_user_attrs );
523 allop = an_find( attrs, slap_bv_all_operational_attrs );
526 while ( *p1 == '(' || *p1 == '&' || *p1 == '|' || *p1 == ')' ) p1++;
527 p2 = strchr( p1, '=' );
530 if ( p2[-1] == '<' || p2[-1] == '>' ) p2--;
534 i = slap_bv2ad( &bv, &ad, text );
541 if ( ad == slap_schema.si_ad_objectClass )
544 if ( is_at_operational(ad->ad_type)) {
548 } else if ( alluser ) {
551 if ( !ad_inlist( ad, attrs )) {
552 attrs = (AttributeName *)ch_realloc(attrs,
553 (attr_cnt + 2)*sizeof(AttributeName));
555 attrs[attr_cnt].an_desc = ad;
556 attrs[attr_cnt].an_name = ad->ad_cname;
557 attrs[attr_cnt].an_oc = NULL;
558 attrs[attr_cnt].an_flags = 0;
559 BER_BVZERO( &attrs[attr_cnt+1].an_name );
567 *text = "couldn't parse template";
570 if ( !got_oc && !( set->flags & PC_GOT_OC )) {
571 attrs = (AttributeName *)ch_realloc(attrs,
572 (attr_cnt + 2)*sizeof(AttributeName));
574 ad = slap_schema.si_ad_objectClass;
575 attrs[attr_cnt].an_desc = ad;
576 attrs[attr_cnt].an_name = ad->ad_cname;
577 attrs[attr_cnt].an_oc = NULL;
578 attrs[attr_cnt].an_flags = 0;
579 BER_BVZERO( &attrs[attr_cnt+1].an_name );
587 * Turn an URL representing a formerly cached query into a cached query,
588 * and try to cache it
599 LDAPURLDesc *lud = NULL;
601 tempstr = BER_BVNULL,
606 unsigned long answerable_cnt;
609 #define GOT_UUID 0x1U
610 #define GOT_ATTRSET 0x2U
611 #define GOT_EXPIRY 0x4U
612 #define GOT_ANSWERABLE 0x8U
613 #define GOT_REFRESH 0x10U
614 #define GOT_ALL (GOT_UUID|GOT_ATTRSET|GOT_EXPIRY|GOT_ANSWERABLE)
617 rc = ldap_url_parse( url, &lud );
618 if ( rc != LDAP_URL_SUCCESS ) {
622 /* non-allowed fields */
623 if ( lud->lud_host != NULL ) {
628 if ( lud->lud_attrs != NULL ) {
634 if ( strcmp( lud->lud_scheme, "ldap" ) != 0 ) {
639 /* required fields */
640 if ( lud->lud_dn == NULL || lud->lud_dn[ 0 ] == '\0' ) {
645 switch ( lud->lud_scope ) {
646 case LDAP_SCOPE_BASE:
647 case LDAP_SCOPE_ONELEVEL:
648 case LDAP_SCOPE_SUBTREE:
649 case LDAP_SCOPE_SUBORDINATE:
657 if ( lud->lud_filter == NULL || lud->lud_filter[ 0 ] == '\0' ) {
662 if ( lud->lud_exts == NULL ) {
667 for ( i = 0; lud->lud_exts[ i ] != NULL; i++ ) {
668 if ( strncmp( lud->lud_exts[ i ], "x-uuid=", STRLENOF( "x-uuid=" ) ) == 0 ) {
669 struct berval tmpUUID;
670 Syntax *syn_UUID = slap_schema.si_ad_entryUUID->ad_type->sat_syntax;
672 if ( got & GOT_UUID ) {
677 ber_str2bv( &lud->lud_exts[ i ][ STRLENOF( "x-uuid=" ) ], 0, 0, &tmpUUID );
678 rc = syn_UUID->ssyn_pretty( syn_UUID, &tmpUUID, &uuid, NULL );
679 if ( rc != LDAP_SUCCESS ) {
684 } else if ( strncmp( lud->lud_exts[ i ], "x-attrset=", STRLENOF( "x-attrset=" ) ) == 0 ) {
685 if ( got & GOT_ATTRSET ) {
690 rc = lutil_atoi( &attrset, &lud->lud_exts[ i ][ STRLENOF( "x-attrset=" ) ] );
696 } else if ( strncmp( lud->lud_exts[ i ], "x-expiry=", STRLENOF( "x-expiry=" ) ) == 0 ) {
699 if ( got & GOT_EXPIRY ) {
704 rc = lutil_atoul( &l, &lud->lud_exts[ i ][ STRLENOF( "x-expiry=" ) ] );
708 expiry_time = (time_t)l;
711 } else if ( strncmp( lud->lud_exts[ i ], "x-answerable=", STRLENOF( "x-answerable=" ) ) == 0 ) {
712 if ( got & GOT_ANSWERABLE ) {
717 rc = lutil_atoul( &answerable_cnt, &lud->lud_exts[ i ][ STRLENOF( "x-answerable=" ) ] );
721 got |= GOT_ANSWERABLE;
723 } else if ( strncmp( lud->lud_exts[ i ], "x-refresh=", STRLENOF( "x-refresh=" ) ) == 0 ) {
726 if ( got & GOT_REFRESH ) {
731 rc = lutil_atoul( &l, &lud->lud_exts[ i ][ STRLENOF( "x-refresh=" ) ] );
735 refresh_time = (time_t)l;
744 if ( got != GOT_ALL ) {
749 if ( !(got & GOT_REFRESH ))
752 /* ignore expired queries */
753 if ( expiry_time <= slap_get_time()) {
755 SlapReply rs2 = { 0 };
757 memset( &op2.oq_search, 0, sizeof( op2.oq_search ) );
759 (void)remove_query_data( &op2, &rs2, &uuid );
764 ber_str2bv( lud->lud_dn, 0, 0, &base );
765 rc = dnNormalize( 0, NULL, NULL, &base, &query.base, NULL );
766 if ( rc != LDAP_SUCCESS ) {
769 query.scope = lud->lud_scope;
770 query.filter = str2filter( lud->lud_filter );
772 tempstr.bv_val = ch_malloc( strlen( lud->lud_filter ) + 1 );
774 if ( filter2template( op, query.filter, &tempstr ) ) {
775 ch_free( tempstr.bv_val );
780 /* check for query containment */
781 qt = qm->attr_sets[attrset].templates;
782 for ( ; qt; qt = qt->qtnext ) {
783 /* find if template i can potentially answer tempstr */
784 if ( bvmatch( &qt->querystr, &tempstr ) ) {
794 cq = add_query( op, qm, &query, qt, PC_POSITIVE, 0 );
796 cq->expiry_time = expiry_time;
797 cq->refresh_time = refresh_time;
799 cq->answerable_cnt = answerable_cnt;
802 /* it's now into cq->filter */
812 if ( query.filter != NULL ) filter_free( query.filter );
813 if ( !BER_BVISNULL( &tempstr ) ) ch_free( tempstr.bv_val );
814 if ( !BER_BVISNULL( &query.base ) ) ch_free( query.base.bv_val );
815 if ( !BER_BVISNULL( &uuid ) ) ch_free( uuid.bv_val );
816 if ( lud != NULL ) ldap_free_urldesc( lud );
821 /* Return 1 for an added entry, else 0 */
826 struct berval* query_uuid )
829 Modifications* modlist = NULL;
830 const char* text = NULL;
832 char textbuf[SLAP_TEXT_BUFLEN];
833 size_t textlen = sizeof(textbuf);
835 SlapReply sreply = {REP_RESULT};
837 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
842 /* add queryId attribute */
843 attr_merge_one( e, ad_queryId, query_uuid, NULL );
845 /* append the attribute list from the fetched entry */
846 e->e_attrs->a_next = attr;
848 op->o_tag = LDAP_REQ_ADD;
849 op->o_protocol = LDAP_VERSION3;
850 op->o_callback = &cb;
851 op->o_time = slap_get_time();
852 op->o_do_not_cache = 1;
855 op->o_req_dn = e->e_name;
856 op->o_req_ndn = e->e_nname;
857 rc = op->o_bd->be_add( op, &sreply );
859 if ( rc != LDAP_SUCCESS ) {
860 if ( rc == LDAP_ALREADY_EXISTS ) {
861 slap_entry2mods( e, &modlist, &text, textbuf, textlen );
862 modlist->sml_op = LDAP_MOD_ADD;
863 op->o_tag = LDAP_REQ_MODIFY;
864 op->orm_modlist = modlist;
865 op->o_bd->be_modify( op, &sreply );
866 slap_mods_free( modlist, 1 );
867 } else if ( rc == LDAP_REFERRAL ||
868 rc == LDAP_NO_SUCH_OBJECT ) {
869 syncrepl_add_glue( op, e );
878 if ( op->ora_e == e )
879 be_entry_release_w( op, e );
886 /* Length-ordered sort on normalized DNs */
887 static int pcache_dn_cmp( const void *v1, const void *v2 )
889 const Qbase *q1 = v1, *q2 = v2;
891 int rc = q1->base.bv_len - q2->base.bv_len;
893 rc = strncmp( q1->base.bv_val, q2->base.bv_val, q1->base.bv_len );
897 static int lex_bvcmp( struct berval *bv1, struct berval *bv2 )
900 dif = bv1->bv_len - bv2->bv_len;
902 if ( dif > 0 ) len -= dif;
903 len = memcmp( bv1->bv_val, bv2->bv_val, len );
909 /* compare the current value in each filter */
910 static int pcache_filter_cmp( Filter *f1, Filter *f2 )
912 int rc, weight1, weight2;
914 switch( f1->f_choice ) {
915 case LDAP_FILTER_PRESENT:
918 case LDAP_FILTER_EQUALITY:
926 switch( f2->f_choice ) {
927 case LDAP_FILTER_PRESENT:
930 case LDAP_FILTER_EQUALITY:
938 rc = weight1 - weight2;
944 rc = lex_bvcmp( &f1->f_av_value, &f2->f_av_value );
947 if ( f1->f_choice == LDAP_FILTER_SUBSTRINGS ) {
949 if ( !BER_BVISNULL( &f1->f_sub_initial )) {
950 if ( !BER_BVISNULL( &f2->f_sub_initial )) {
951 rc = lex_bvcmp( &f1->f_sub_initial,
952 &f2->f_sub_initial );
956 } else if ( !BER_BVISNULL( &f2->f_sub_initial )) {
960 if ( f1->f_sub_any ) {
961 if ( f2->f_sub_any ) {
962 rc = lex_bvcmp( f1->f_sub_any,
967 } else if ( f2->f_sub_any ) {
971 if ( !BER_BVISNULL( &f1->f_sub_final )) {
972 if ( !BER_BVISNULL( &f2->f_sub_final )) {
973 rc = lex_bvcmp( &f1->f_sub_final,
978 } else if ( !BER_BVISNULL( &f2->f_sub_final )) {
982 rc = lex_bvcmp( &f1->f_mr_value,
996 while ( f1->f_choice == LDAP_FILTER_AND || f1->f_choice == LDAP_FILTER_OR )
998 while ( f2->f_choice == LDAP_FILTER_AND || f2->f_choice == LDAP_FILTER_OR )
1000 rc = pcache_filter_cmp( f1, f2 );
1008 /* compare filters in each query */
1009 static int pcache_query_cmp( const void *v1, const void *v2 )
1011 const CachedQuery *q1 = v1, *q2 =v2;
1012 return pcache_filter_cmp( q1->first, q2->first );
1015 /* add query on top of LRU list */
1017 add_query_on_top (query_manager* qm, CachedQuery* qc)
1019 CachedQuery* top = qm->lru_top;
1026 qm->lru_bottom = qc;
1030 Debug( pcache_debug, "Base of added query = %s\n",
1031 qc->qbase->base.bv_val, 0, 0 );
1034 /* remove_query from LRU list */
1037 remove_query (query_manager* qm, CachedQuery* qc)
1046 down = qc->lru_down;
1052 qm->lru_bottom = up;
1058 up->lru_down = down;
1060 qc->lru_up = qc->lru_down = NULL;
1063 /* find and remove string2 from string1
1064 * from start if position = 1,
1065 * from end if position = 3,
1066 * from anywhere if position = 2
1067 * string1 is overwritten if position = 2.
1071 find_and_remove(struct berval* ber1, struct berval* ber2, int position)
1075 if ( !ber2->bv_val )
1077 if ( !ber1->bv_val )
1080 switch( position ) {
1082 if ( ber1->bv_len >= ber2->bv_len && !memcmp( ber1->bv_val,
1083 ber2->bv_val, ber2->bv_len )) {
1085 ber1->bv_val += ber2->bv_len;
1086 ber1->bv_len -= ber2->bv_len;
1091 ber1->bv_val[ber1->bv_len] = '\0';
1092 temp = strstr( ber1->bv_val, ber2->bv_val );
1094 strcpy( temp, temp+ber2->bv_len );
1095 ber1->bv_len -= ber2->bv_len;
1101 if ( ber1->bv_len >= ber2->bv_len &&
1102 !memcmp( ber1->bv_val+ber1->bv_len-ber2->bv_len, ber2->bv_val,
1105 ber1->bv_len -= ber2->bv_len;
1113 static struct berval*
1114 merge_init_final(Operation *op, struct berval* init, struct berval* any,
1115 struct berval* final)
1117 struct berval* merged, *temp;
1118 int i, any_count, count;
1120 for (any_count=0; any && any[any_count].bv_val; any_count++)
1130 merged = (struct berval*)op->o_tmpalloc( (count+1)*sizeof(struct berval),
1135 ber_dupbv_x( temp, init, op->o_tmpmemctx );
1139 for (i=0; i<any_count; i++) {
1140 ber_dupbv_x( temp, any, op->o_tmpmemctx );
1145 ber_dupbv_x( temp, final, op->o_tmpmemctx );
1152 /* Each element in stored must be found in incoming. Incoming is overwritten.
1155 strings_containment(struct berval* stored, struct berval* incoming)
1157 struct berval* element;
1161 for ( element=stored; element->bv_val != NULL; element++ ) {
1162 for (j = k; incoming[j].bv_val != NULL; j++) {
1163 if (find_and_remove(&(incoming[j]), element, 2)) {
1180 substr_containment_substr(Operation *op, Filter* stored, Filter* incoming)
1184 struct berval init_incoming;
1185 struct berval final_incoming;
1186 struct berval *remaining_incoming = NULL;
1188 if ((!(incoming->f_sub_initial.bv_val) && (stored->f_sub_initial.bv_val))
1189 || (!(incoming->f_sub_final.bv_val) && (stored->f_sub_final.bv_val)))
1192 init_incoming = incoming->f_sub_initial;
1193 final_incoming = incoming->f_sub_final;
1195 if (find_and_remove(&init_incoming,
1196 &(stored->f_sub_initial), 1) && find_and_remove(&final_incoming,
1197 &(stored->f_sub_final), 3))
1199 if (stored->f_sub_any == NULL) {
1203 remaining_incoming = merge_init_final(op, &init_incoming,
1204 incoming->f_sub_any, &final_incoming);
1205 rc = strings_containment(stored->f_sub_any, remaining_incoming);
1206 ber_bvarray_free_x( remaining_incoming, op->o_tmpmemctx );
1213 substr_containment_equality(Operation *op, Filter* stored, Filter* incoming)
1215 struct berval incoming_val[2];
1218 incoming_val[1] = incoming->f_av_value;
1220 if (find_and_remove(incoming_val+1,
1221 &(stored->f_sub_initial), 1) && find_and_remove(incoming_val+1,
1222 &(stored->f_sub_final), 3)) {
1223 if (stored->f_sub_any == NULL){
1227 ber_dupbv_x( incoming_val, incoming_val+1, op->o_tmpmemctx );
1228 BER_BVZERO( incoming_val+1 );
1229 rc = strings_containment(stored->f_sub_any, incoming_val);
1230 op->o_tmpfree( incoming_val[0].bv_val, op->o_tmpmemctx );
1237 filter_first( Filter *f )
1239 while ( f->f_choice == LDAP_FILTER_OR || f->f_choice == LDAP_FILTER_AND )
1245 static CachedQuery *
1246 find_filter( Operation *op, Avlnode *root, Filter *inputf, Filter *first )
1250 MatchingRule* mrule = NULL;
1251 int res=0, eqpass= 0;
1254 CachedQuery cq, *qc;
1259 /* substring matches sort to the end, and we just have to
1260 * walk the entire list.
1262 if ( first->f_choice == LDAP_FILTER_SUBSTRINGS ) {
1263 ptr = tavl_end( root, 1 );
1264 dir = TAVL_DIR_LEFT;
1266 ptr = tavl_find3( root, &cq, pcache_query_cmp, &ret );
1267 dir = (first->f_choice == LDAP_FILTER_GE) ? TAVL_DIR_LEFT :
1276 /* an incoming substr query can only be satisfied by a cached
1279 if ( first->f_choice == LDAP_FILTER_SUBSTRINGS &&
1280 qc->first->f_choice != LDAP_FILTER_SUBSTRINGS )
1283 /* an incoming eq query can be satisfied by a cached eq or substr
1286 if ( first->f_choice == LDAP_FILTER_EQUALITY ) {
1287 if ( eqpass == 0 ) {
1288 if ( qc->first->f_choice != LDAP_FILTER_EQUALITY ) {
1289 nextpass: eqpass = 1;
1290 ptr = tavl_end( root, 1 );
1291 dir = TAVL_DIR_LEFT;
1295 if ( qc->first->f_choice != LDAP_FILTER_SUBSTRINGS )
1301 switch (fs->f_choice) {
1302 case LDAP_FILTER_EQUALITY:
1303 if (fi->f_choice == LDAP_FILTER_EQUALITY)
1304 mrule = fs->f_ava->aa_desc->ad_type->sat_equality;
1308 case LDAP_FILTER_GE:
1309 case LDAP_FILTER_LE:
1310 mrule = fs->f_ava->aa_desc->ad_type->sat_ordering;
1317 rc = value_match(&ret, fs->f_ava->aa_desc, mrule,
1318 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
1319 &(fi->f_ava->aa_value),
1320 &(fs->f_ava->aa_value), &text);
1321 if (rc != LDAP_SUCCESS) {
1324 if ( fi==first && fi->f_choice==LDAP_FILTER_EQUALITY && ret )
1327 switch (fs->f_choice) {
1328 case LDAP_FILTER_OR:
1329 case LDAP_FILTER_AND:
1334 case LDAP_FILTER_SUBSTRINGS:
1335 /* check if the equality query can be
1336 * answered with cached substring query */
1337 if ((fi->f_choice == LDAP_FILTER_EQUALITY)
1338 && substr_containment_equality( op,
1341 /* check if the substring query can be
1342 * answered with cached substring query */
1343 if ((fi->f_choice ==LDAP_FILTER_SUBSTRINGS
1344 ) && substr_containment_substr( op,
1350 case LDAP_FILTER_PRESENT:
1355 case LDAP_FILTER_EQUALITY:
1361 case LDAP_FILTER_GE:
1362 if (mrule && ret >= 0)
1367 case LDAP_FILTER_LE:
1368 if (mrule && ret <= 0)
1373 case LDAP_FILTER_NOT:
1379 } while((res) && (fi != NULL) && (fs != NULL));
1383 ptr = tavl_next( ptr, dir );
1388 /* check whether query is contained in any of
1389 * the cached queries in template
1391 static CachedQuery *
1392 query_containment(Operation *op, query_manager *qm,
1394 QueryTemplate *templa)
1397 int depth = 0, tscope;
1398 Qbase qbase, *qbptr = NULL;
1401 if (query->filter != NULL) {
1404 Debug( pcache_debug, "Lock QC index = %p\n",
1405 (void *) templa, 0, 0 );
1406 qbase.base = query->base;
1408 first = filter_first( query->filter );
1410 ldap_pvt_thread_rdwr_rlock(&templa->t_rwlock);
1413 qbptr = avl_find( templa->qbase, &qbase, pcache_dn_cmp );
1415 tscope = query->scope;
1416 /* Find a matching scope:
1417 * match at depth 0 OK
1420 * subord at depth > 0 OK
1421 * subtree at any depth OK
1423 * subtree or subord at any depth OK
1425 * subtree or subord at any depth OK
1427 * subord at depth > 0 OK
1428 * subtree at any depth OK
1430 for ( tscope = 0 ; tscope <= LDAP_SCOPE_CHILDREN; tscope++ ) {
1431 switch ( query->scope ) {
1432 case LDAP_SCOPE_BASE:
1433 if ( tscope == LDAP_SCOPE_BASE && depth ) continue;
1434 if ( tscope == LDAP_SCOPE_ONE && depth != 1) continue;
1435 if ( tscope == LDAP_SCOPE_CHILDREN && !depth ) continue;
1437 case LDAP_SCOPE_ONE:
1438 if ( tscope == LDAP_SCOPE_BASE )
1439 tscope = LDAP_SCOPE_ONE;
1440 if ( tscope == LDAP_SCOPE_ONE && depth ) continue;
1441 if ( !depth ) break;
1442 if ( tscope < LDAP_SCOPE_SUBTREE )
1443 tscope = LDAP_SCOPE_SUBTREE;
1445 case LDAP_SCOPE_SUBTREE:
1446 if ( tscope < LDAP_SCOPE_SUBTREE )
1447 tscope = LDAP_SCOPE_SUBTREE;
1448 if ( tscope == LDAP_SCOPE_CHILDREN && !depth ) continue;
1450 case LDAP_SCOPE_CHILDREN:
1451 if ( tscope < LDAP_SCOPE_SUBTREE )
1452 tscope = LDAP_SCOPE_SUBTREE;
1455 if ( !qbptr->scopes[tscope] ) continue;
1458 qc = find_filter( op, qbptr->scopes[tscope],
1459 query->filter, first );
1461 if ( qc->q_sizelimit ) {
1462 ldap_pvt_thread_rdwr_runlock(&templa->t_rwlock);
1465 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1466 if (qm->lru_top != qc) {
1467 remove_query(qm, qc);
1468 add_query_on_top(qm, qc);
1470 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1475 if ( be_issuffix( op->o_bd, &qbase.base ))
1478 dnParent( &qbase.base, &pdn );
1483 Debug( pcache_debug,
1484 "Not answerable: Unlock QC index=%p\n",
1485 (void *) templa, 0, 0 );
1486 ldap_pvt_thread_rdwr_runlock(&templa->t_rwlock);
1492 free_query (CachedQuery* qc)
1494 free(qc->q_uuid.bv_val);
1495 filter_free(qc->filter);
1496 ldap_pvt_thread_mutex_destroy(&qc->answerable_cnt_mutex);
1497 ldap_pvt_thread_rdwr_destroy( &qc->rwlock );
1498 memset(qc, 0, sizeof(*qc));
1503 /* Add query to query cache, the returned Query is locked for writing */
1504 static CachedQuery *
1509 QueryTemplate *templ,
1510 pc_caching_reason_t why,
1513 CachedQuery* new_cached_query = (CachedQuery*) ch_malloc(sizeof(CachedQuery));
1517 time_t ttl = 0, ttr = 0;
1520 new_cached_query->qtemp = templ;
1521 BER_BVZERO( &new_cached_query->q_uuid );
1522 new_cached_query->q_sizelimit = 0;
1524 now = slap_get_time();
1529 ttr = now + templ->ttr;
1533 ttl = templ->negttl;
1537 ttl = templ->limitttl;
1544 new_cached_query->expiry_time = now + ttl;
1545 new_cached_query->refresh_time = ttr;
1547 new_cached_query->answerable_cnt = 0;
1548 new_cached_query->refcnt = 1;
1549 ldap_pvt_thread_mutex_init(&new_cached_query->answerable_cnt_mutex);
1551 new_cached_query->lru_up = NULL;
1552 new_cached_query->lru_down = NULL;
1553 Debug( pcache_debug, "Added query expires at %ld (%s)\n",
1554 (long) new_cached_query->expiry_time,
1555 pc_caching_reason_str[ why ], 0 );
1557 new_cached_query->scope = query->scope;
1558 new_cached_query->filter = query->filter;
1559 new_cached_query->first = first = filter_first( query->filter );
1561 ldap_pvt_thread_rdwr_init(&new_cached_query->rwlock);
1563 ldap_pvt_thread_rdwr_wlock(&new_cached_query->rwlock);
1565 qb.base = query->base;
1567 /* Adding a query */
1568 Debug( pcache_debug, "Lock AQ index = %p\n",
1569 (void *) templ, 0, 0 );
1570 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
1571 qbase = avl_find( templ->qbase, &qb, pcache_dn_cmp );
1573 qbase = ch_calloc( 1, sizeof(Qbase) + qb.base.bv_len + 1 );
1574 qbase->base.bv_len = qb.base.bv_len;
1575 qbase->base.bv_val = (char *)(qbase+1);
1576 memcpy( qbase->base.bv_val, qb.base.bv_val, qb.base.bv_len );
1577 qbase->base.bv_val[qbase->base.bv_len] = '\0';
1578 avl_insert( &templ->qbase, qbase, pcache_dn_cmp, avl_dup_error );
1580 new_cached_query->next = templ->query;
1581 new_cached_query->prev = NULL;
1582 new_cached_query->qbase = qbase;
1583 rc = tavl_insert( &qbase->scopes[query->scope], new_cached_query,
1584 pcache_query_cmp, avl_dup_error );
1587 if (templ->query == NULL)
1588 templ->query_last = new_cached_query;
1590 templ->query->prev = new_cached_query;
1591 templ->query = new_cached_query;
1592 templ->no_of_queries++;
1594 ch_free( new_cached_query );
1595 new_cached_query = find_filter( op, qbase->scopes[query->scope],
1596 query->filter, first );
1597 filter_free( query->filter );
1598 query->filter = NULL;
1600 Debug( pcache_debug, "TEMPLATE %p QUERIES++ %d\n",
1601 (void *) templ, templ->no_of_queries, 0 );
1603 Debug( pcache_debug, "Unlock AQ index = %p \n",
1604 (void *) templ, 0, 0 );
1605 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
1607 /* Adding on top of LRU list */
1609 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1610 add_query_on_top(qm, new_cached_query);
1611 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1613 return rc == 0 ? new_cached_query : NULL;
1617 remove_from_template (CachedQuery* qc, QueryTemplate* template)
1619 if (!qc->prev && !qc->next) {
1620 template->query_last = template->query = NULL;
1621 } else if (qc->prev == NULL) {
1622 qc->next->prev = NULL;
1623 template->query = qc->next;
1624 } else if (qc->next == NULL) {
1625 qc->prev->next = NULL;
1626 template->query_last = qc->prev;
1628 qc->next->prev = qc->prev;
1629 qc->prev->next = qc->next;
1631 tavl_delete( &qc->qbase->scopes[qc->scope], qc, pcache_query_cmp );
1632 qc->qbase->queries--;
1633 if ( qc->qbase->queries == 0 ) {
1634 avl_delete( &template->qbase, qc->qbase, pcache_dn_cmp );
1635 ch_free( qc->qbase );
1639 template->no_of_queries--;
1642 /* remove bottom query of LRU list from the query cache */
1644 * NOTE: slight change in functionality.
1646 * - if result->bv_val is NULL, the query at the bottom of the LRU
1648 * - otherwise, the query whose UUID is *result is removed
1649 * - if not found, result->bv_val is zeroed
1652 cache_replacement(query_manager* qm, struct berval *result)
1654 CachedQuery* bottom;
1655 QueryTemplate *temp;
1657 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1658 if ( BER_BVISNULL( result ) ) {
1659 bottom = qm->lru_bottom;
1662 Debug ( pcache_debug,
1663 "Cache replacement invoked without "
1664 "any query in LRU list\n", 0, 0, 0 );
1665 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1670 for ( bottom = qm->lru_bottom;
1672 bottom = bottom->lru_up )
1674 if ( bvmatch( result, &bottom->q_uuid ) ) {
1680 Debug ( pcache_debug,
1681 "Could not find query with uuid=\"%s\""
1682 "in LRU list\n", result->bv_val, 0, 0 );
1683 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1684 BER_BVZERO( result );
1689 temp = bottom->qtemp;
1690 remove_query(qm, bottom);
1691 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1693 *result = bottom->q_uuid;
1694 BER_BVZERO( &bottom->q_uuid );
1696 Debug( pcache_debug, "Lock CR index = %p\n", (void *) temp, 0, 0 );
1697 ldap_pvt_thread_rdwr_wlock(&temp->t_rwlock);
1698 remove_from_template(bottom, temp);
1699 Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
1700 (void *) temp, temp->no_of_queries, 0 );
1701 Debug( pcache_debug, "Unlock CR index = %p\n", (void *) temp, 0, 0 );
1702 ldap_pvt_thread_rdwr_wunlock(&temp->t_rwlock);
1707 struct query_info *next;
1719 struct query_info *qi;
1722 if ( rs->sr_type != REP_SEARCH ) return 0;
1724 attr = attr_find( rs->sr_entry->e_attrs, ad_queryId );
1725 if ( attr == NULL ) return 0;
1727 count = attr->a_numvals;
1728 assert( count > 0 );
1729 qi = op->o_tmpalloc( sizeof( struct query_info ), op->o_tmpmemctx );
1730 qi->next = op->o_callback->sc_private;
1731 op->o_callback->sc_private = qi;
1732 ber_dupbv_x( &qi->xdn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1733 qi->del = ( count == 1 );
1742 struct berval *query_uuid )
1744 struct query_info *qi, *qnext;
1745 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(pcacheQueryID=)" ) ];
1746 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
1747 Filter filter = {LDAP_FILTER_EQUALITY};
1748 SlapReply sreply = {REP_RESULT};
1749 slap_callback cb = { NULL, remove_func, NULL, NULL };
1752 sreply.sr_entry = NULL;
1753 sreply.sr_nentries = 0;
1754 op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
1755 "(%s=%s)", ad_queryId->ad_cname.bv_val, query_uuid->bv_val);
1756 filter.f_ava = &ava;
1757 filter.f_av_desc = ad_queryId;
1758 filter.f_av_value = *query_uuid;
1760 op->o_tag = LDAP_REQ_SEARCH;
1761 op->o_protocol = LDAP_VERSION3;
1762 op->o_callback = &cb;
1763 op->o_time = slap_get_time();
1764 op->o_do_not_cache = 1;
1766 op->o_req_dn = op->o_bd->be_suffix[0];
1767 op->o_req_ndn = op->o_bd->be_nsuffix[0];
1768 op->ors_scope = LDAP_SCOPE_SUBTREE;
1769 op->ors_deref = LDAP_DEREF_NEVER;
1770 op->ors_slimit = SLAP_NO_LIMIT;
1771 op->ors_tlimit = SLAP_NO_LIMIT;
1772 op->ors_limit = NULL;
1773 op->ors_filter = &filter;
1774 op->ors_filterstr.bv_val = filter_str;
1775 op->ors_filterstr.bv_len = strlen(filter_str);
1776 op->ors_attrs = NULL;
1777 op->ors_attrsonly = 0;
1779 op->o_bd->be_search( op, &sreply );
1781 for ( qi=cb.sc_private; qi; qi=qnext ) {
1784 op->o_req_dn = qi->xdn;
1785 op->o_req_ndn = qi->xdn;
1788 Debug( pcache_debug, "DELETING ENTRY TEMPLATE=%s\n",
1789 query_uuid->bv_val, 0, 0 );
1791 op->o_tag = LDAP_REQ_DELETE;
1793 if (op->o_bd->be_delete(op, &sreply) == LDAP_SUCCESS) {
1799 struct berval vals[2];
1801 vals[0] = *query_uuid;
1802 vals[1].bv_val = NULL;
1804 mod.sml_op = LDAP_MOD_DELETE;
1806 mod.sml_desc = ad_queryId;
1807 mod.sml_type = ad_queryId->ad_cname;
1808 mod.sml_values = vals;
1809 mod.sml_nvalues = NULL;
1810 mod.sml_numvals = 1;
1811 mod.sml_next = NULL;
1812 Debug( pcache_debug,
1813 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
1814 query_uuid->bv_val, 0, 0 );
1816 op->orm_modlist = &mod;
1818 op->o_bd->be_modify( op, &sreply );
1820 op->o_tmpfree( qi->xdn.bv_val, op->o_tmpmemctx );
1821 op->o_tmpfree( qi, op->o_tmpmemctx );
1828 AttributeName* attrs,
1837 struct berval *fstr )
1839 AttributeDescription *ad;
1842 switch ( f->f_choice ) {
1843 case LDAP_FILTER_EQUALITY:
1845 len = STRLENOF( "(=)" ) + ad->ad_cname.bv_len;
1846 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=)", ad->ad_cname.bv_val );
1847 assert( ret == len );
1848 fstr->bv_len += len;
1851 case LDAP_FILTER_GE:
1853 len = STRLENOF( "(>=)" ) + ad->ad_cname.bv_len;
1854 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s>=)", ad->ad_cname.bv_val);
1855 assert( ret == len );
1856 fstr->bv_len += len;
1859 case LDAP_FILTER_LE:
1861 len = STRLENOF( "(<=)" ) + ad->ad_cname.bv_len;
1862 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s<=)", ad->ad_cname.bv_val);
1863 assert( ret == len );
1864 fstr->bv_len += len;
1867 case LDAP_FILTER_APPROX:
1869 len = STRLENOF( "(~=)" ) + ad->ad_cname.bv_len;
1870 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s~=)", ad->ad_cname.bv_val);
1871 assert( ret == len );
1872 fstr->bv_len += len;
1875 case LDAP_FILTER_SUBSTRINGS:
1877 len = STRLENOF( "(=)" ) + ad->ad_cname.bv_len;
1878 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=)", ad->ad_cname.bv_val );
1879 assert( ret == len );
1880 fstr->bv_len += len;
1883 case LDAP_FILTER_PRESENT:
1885 len = STRLENOF( "(=*)" ) + ad->ad_cname.bv_len;
1886 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=*)", ad->ad_cname.bv_val );
1887 assert( ret == len );
1888 fstr->bv_len += len;
1891 case LDAP_FILTER_AND:
1892 case LDAP_FILTER_OR:
1893 case LDAP_FILTER_NOT: {
1895 fstr->bv_val[fstr->bv_len++] = '(';
1896 switch ( f->f_choice ) {
1897 case LDAP_FILTER_AND:
1898 fstr->bv_val[fstr->bv_len] = '&';
1900 case LDAP_FILTER_OR:
1901 fstr->bv_val[fstr->bv_len] = '|';
1903 case LDAP_FILTER_NOT:
1904 fstr->bv_val[fstr->bv_len] = '!';
1909 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1910 rc = filter2template( op, f, fstr );
1913 fstr->bv_val[fstr->bv_len++] = ')';
1914 fstr->bv_val[fstr->bv_len] = '\0';
1920 /* a filter should at least have room for "()",
1921 * an "=" and for a 1-char attr */
1922 strcpy( fstr->bv_val, "(?=)" );
1923 fstr->bv_len += STRLENOF("(?=)");
1930 #define BI_HASHED 0x01
1931 #define BI_DIDCB 0x02
1932 #define BI_LOOKUP 0x04
1936 typedef struct bindinfo {
1937 cache_manager *bi_cm;
1939 QueryTemplate *bi_templ;
1940 struct search_info *bi_si;
1942 slap_callback bi_cb;
1945 struct search_info {
1948 QueryTemplate *qtemp;
1949 AttributeName* save_attrs; /* original attributes, saved for response */
1950 int swap_saved_attrs;
1955 int slimit_exceeded;
1956 pc_caching_reason_t caching_reason;
1962 remove_query_and_data(
1966 struct berval *uuid )
1968 query_manager* qm = cm->qm;
1970 qm->crfunc( qm, uuid );
1971 if ( !BER_BVISNULL( uuid ) ) {
1974 Debug( pcache_debug,
1975 "Removing query UUID %s\n",
1976 uuid->bv_val, 0, 0 );
1977 return_val = remove_query_data( op, rs, uuid );
1978 Debug( pcache_debug,
1979 "QUERY REMOVED, SIZE=%d\n",
1981 ldap_pvt_thread_mutex_lock( &cm->cache_mutex );
1982 cm->cur_entries -= return_val;
1983 cm->num_cached_queries--;
1984 Debug( pcache_debug,
1985 "STORED QUERIES = %lu\n",
1986 cm->num_cached_queries, 0, 0 );
1987 ldap_pvt_thread_mutex_unlock( &cm->cache_mutex );
1988 Debug( pcache_debug,
1989 "QUERY REMOVED, CACHE ="
1991 cm->cur_entries, 0, 0 );
1996 * Callback used to fetch queryId values based on entryUUID;
1997 * used by pcache_remove_entries_from_cache()
2000 fetch_queryId_cb( Operation *op, SlapReply *rs )
2004 /* only care about searchEntry responses */
2005 if ( rs->sr_type != REP_SEARCH ) {
2009 /* allow only one response per entryUUID */
2010 if ( op->o_callback->sc_private != NULL ) {
2016 /* copy all queryId values into callback's private data */
2017 a = attr_find( rs->sr_entry->e_attrs, ad_queryId );
2019 BerVarray vals = NULL;
2021 ber_bvarray_dup_x( &vals, a->a_nvals, op->o_tmpmemctx );
2022 op->o_callback->sc_private = (void *)vals;
2026 /* clear entry if required */
2027 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
2028 entry_free( rs->sr_entry );
2029 rs->sr_entry = NULL;
2030 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
2037 * Call that allows to remove a set of entries from the cache,
2038 * by forcing the removal of all the related queries.
2041 pcache_remove_entries_from_cache(
2044 BerVarray entryUUIDs )
2046 Connection conn = { 0 };
2047 OperationBuffer opbuf;
2049 slap_callback sc = { 0 };
2050 SlapReply rs = { REP_RESULT };
2052 char filtbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(entryUUID=)" ) ];
2053 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2054 AttributeName attrs[ 2 ] = {{{ 0 }}};
2058 void *thrctx = ldap_pvt_thread_pool_context();
2060 connection_fake_init( &conn, &opbuf, thrctx );
2068 memset( &op->oq_search, 0, sizeof( op->oq_search ) );
2069 op->ors_scope = LDAP_SCOPE_SUBTREE;
2070 op->ors_deref = LDAP_DEREF_NEVER;
2071 f.f_choice = LDAP_FILTER_EQUALITY;
2073 ava.aa_desc = slap_schema.si_ad_entryUUID;
2074 op->ors_filter = &f;
2076 op->ors_tlimit = SLAP_NO_LIMIT;
2077 op->ors_limit = NULL;
2078 attrs[ 0 ].an_desc = ad_queryId;
2079 attrs[ 0 ].an_name = ad_queryId->ad_cname;
2080 op->ors_attrs = attrs;
2081 op->ors_attrsonly = 0;
2083 op->o_req_dn = cm->db.be_suffix[ 0 ];
2084 op->o_req_ndn = cm->db.be_nsuffix[ 0 ];
2086 op->o_tag = LDAP_REQ_SEARCH;
2087 op->o_protocol = LDAP_VERSION3;
2088 op->o_managedsait = SLAP_CONTROL_CRITICAL;
2090 op->o_dn = op->o_bd->be_rootdn;
2091 op->o_ndn = op->o_bd->be_rootndn;
2092 sc.sc_response = fetch_queryId_cb;
2093 op->o_callback = ≻
2095 for ( s = 0; !BER_BVISNULL( &entryUUIDs[ s ] ); s++ ) {
2096 BerVarray vals = NULL;
2098 op->ors_filterstr.bv_len = snprintf( filtbuf, sizeof( filtbuf ),
2099 "(entryUUID=%s)", entryUUIDs[ s ].bv_val );
2100 op->ors_filterstr.bv_val = filtbuf;
2101 ava.aa_value = entryUUIDs[ s ];
2103 rc = op->o_bd->be_search( op, &rs );
2104 if ( rc != LDAP_SUCCESS ) {
2108 vals = (BerVarray)op->o_callback->sc_private;
2109 if ( vals != NULL ) {
2112 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
2113 struct berval val = vals[ i ];
2115 remove_query_and_data( op, &rs, cm, &val );
2117 if ( !BER_BVISNULL( &val ) && val.bv_val != vals[ i ].bv_val ) {
2118 ch_free( val.bv_val );
2122 ber_bvarray_free_x( vals, op->o_tmpmemctx );
2123 op->o_callback->sc_private = NULL;
2131 * Call that allows to remove a query from the cache.
2134 pcache_remove_query_from_cache(
2137 struct berval *queryid )
2139 Operation op2 = *op;
2140 SlapReply rs2 = { 0 };
2144 /* remove the selected query */
2145 remove_query_and_data( &op2, &rs2, cm, queryid );
2147 return LDAP_SUCCESS;
2151 * Call that allows to remove a set of queries related to an entry
2152 * from the cache; if queryid is not null, the entry must belong to
2153 * the query indicated by queryid.
2156 pcache_remove_entry_queries_from_cache(
2160 struct berval *queryid )
2162 Connection conn = { 0 };
2163 OperationBuffer opbuf;
2165 slap_callback sc = { 0 };
2166 SlapReply rs = { REP_RESULT };
2168 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(pcacheQueryID=)" ) ];
2169 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2170 AttributeName attrs[ 2 ] = {{{ 0 }}};
2173 BerVarray vals = NULL;
2176 void *thrctx = ldap_pvt_thread_pool_context();
2178 connection_fake_init( &conn, &opbuf, thrctx );
2186 memset( &op->oq_search, 0, sizeof( op->oq_search ) );
2187 op->ors_scope = LDAP_SCOPE_BASE;
2188 op->ors_deref = LDAP_DEREF_NEVER;
2189 if ( queryid == NULL || BER_BVISNULL( queryid ) ) {
2190 BER_BVSTR( &op->ors_filterstr, "(objectClass=*)" );
2191 f.f_choice = LDAP_FILTER_PRESENT;
2192 f.f_desc = slap_schema.si_ad_objectClass;
2195 op->ors_filterstr.bv_len = snprintf( filter_str,
2196 sizeof( filter_str ), "(%s=%s)",
2197 ad_queryId->ad_cname.bv_val, queryid->bv_val );
2198 f.f_choice = LDAP_FILTER_EQUALITY;
2200 f.f_av_desc = ad_queryId;
2201 f.f_av_value = *queryid;
2203 op->ors_filter = &f;
2205 op->ors_tlimit = SLAP_NO_LIMIT;
2206 op->ors_limit = NULL;
2207 attrs[ 0 ].an_desc = ad_queryId;
2208 attrs[ 0 ].an_name = ad_queryId->ad_cname;
2209 op->ors_attrs = attrs;
2210 op->ors_attrsonly = 0;
2212 op->o_req_dn = *ndn;
2213 op->o_req_ndn = *ndn;
2215 op->o_tag = LDAP_REQ_SEARCH;
2216 op->o_protocol = LDAP_VERSION3;
2217 op->o_managedsait = SLAP_CONTROL_CRITICAL;
2219 op->o_dn = op->o_bd->be_rootdn;
2220 op->o_ndn = op->o_bd->be_rootndn;
2221 sc.sc_response = fetch_queryId_cb;
2222 op->o_callback = ≻
2224 rc = op->o_bd->be_search( op, &rs );
2225 if ( rc != LDAP_SUCCESS ) {
2229 vals = (BerVarray)op->o_callback->sc_private;
2230 if ( vals != NULL ) {
2233 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
2234 struct berval val = vals[ i ];
2236 remove_query_and_data( op, &rs, cm, &val );
2238 if ( !BER_BVISNULL( &val ) && val.bv_val != vals[ i ].bv_val ) {
2239 ch_free( val.bv_val );
2243 ber_bvarray_free_x( vals, op->o_tmpmemctx );
2246 return LDAP_SUCCESS;
2253 struct berval *query_uuid )
2255 struct search_info *si = op->o_callback->sc_private;
2256 slap_overinst *on = si->on;
2257 cache_manager *cm = on->on_bi.bi_private;
2260 struct berval crp_uuid;
2261 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
2263 Connection conn = {0};
2264 OperationBuffer opbuf;
2265 void *thrctx = ldap_pvt_thread_pool_context();
2267 query_uuid->bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf));
2268 ber_str2bv(uuidbuf, query_uuid->bv_len, 1, query_uuid);
2270 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
2271 op_tmp = &opbuf.ob_op;
2272 op_tmp->o_bd = &cm->db;
2273 op_tmp->o_dn = cm->db.be_rootdn;
2274 op_tmp->o_ndn = cm->db.be_rootndn;
2276 Debug( pcache_debug, "UUID for query being added = %s\n",
2279 for ( e=si->head; e; e=si->head ) {
2280 si->head = e->e_private;
2281 e->e_private = NULL;
2282 while ( cm->cur_entries > (cm->max_entries) ) {
2283 BER_BVZERO( &crp_uuid );
2284 remove_query_and_data( op_tmp, rs, cm, &crp_uuid );
2287 return_val = merge_entry(op_tmp, e, query_uuid);
2288 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2289 cm->cur_entries += return_val;
2290 Debug( pcache_debug,
2291 "ENTRY ADDED/MERGED, CACHED ENTRIES=%d\n",
2292 cm->cur_entries, 0, 0 );
2294 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2301 pcache_op_cleanup( Operation *op, SlapReply *rs ) {
2302 slap_callback *cb = op->o_callback;
2303 struct search_info *si = cb->sc_private;
2304 slap_overinst *on = si->on;
2305 cache_manager *cm = on->on_bi.bi_private;
2306 query_manager* qm = cm->qm;
2308 if ( rs->sr_type == REP_SEARCH ) {
2311 /* don't return more entries than requested by the client */
2312 if ( si->slimit > 0 && rs->sr_nentries >= si->slimit ) {
2313 si->slimit_exceeded = 1;
2316 /* If we haven't exceeded the limit for this query,
2317 * build a chain of answers to store. If we hit the
2318 * limit, empty the chain and ignore the rest.
2321 /* check if the entry contains undefined
2322 * attributes/objectClasses (ITS#5680) */
2323 if ( cm->check_cacheability && test_filter( op, rs->sr_entry, si->query.filter ) != LDAP_COMPARE_TRUE ) {
2324 Debug( pcache_debug, "%s: query not cacheable because of schema issues in DN \"%s\"\n",
2325 op->o_log_prefix, rs->sr_entry->e_name.bv_val, 0 );
2329 /* check for malformed entries: attrs with no values */
2331 Attribute *a = rs->sr_entry->e_attrs;
2332 for (; a; a=a->a_next) {
2333 if ( !a->a_numvals ) {
2334 Debug( pcache_debug, "%s: query not cacheable because of attrs without values in DN \"%s\" (%s)\n",
2335 op->o_log_prefix, rs->sr_entry->e_name.bv_val,
2336 a->a_desc->ad_cname.bv_val );
2342 if ( si->count < si->max ) {
2344 e = entry_dup( rs->sr_entry );
2345 if ( !si->head ) si->head = e;
2346 if ( si->tail ) si->tail->e_private = e;
2353 for (;si->head; si->head=e) {
2354 e = si->head->e_private;
2355 si->head->e_private = NULL;
2356 entry_free(si->head);
2364 if ( rs->sr_type == REP_RESULT ||
2365 op->o_abandon || rs->sr_err == SLAPD_ABANDON )
2367 if ( si->swap_saved_attrs ) {
2368 rs->sr_attrs = si->save_attrs;
2369 op->ors_attrs = si->save_attrs;
2371 if ( (op->o_abandon || rs->sr_err == SLAPD_ABANDON) &&
2372 si->caching_reason == PC_IGNORE )
2374 filter_free( si->query.filter );
2376 /* duplicate query, free it */
2378 for (;si->head; si->head=e) {
2379 e = si->head->e_private;
2380 si->head->e_private = NULL;
2381 entry_free(si->head);
2384 op->o_callback = op->o_callback->sc_next;
2385 op->o_tmpfree( cb, op->o_tmpmemctx );
2386 } else if ( si->caching_reason != PC_IGNORE ) {
2387 CachedQuery *qc = qm->addfunc(op, qm, &si->query,
2388 si->qtemp, si->caching_reason, 1 );
2391 switch ( si->caching_reason ) {
2393 cache_entries( op, rs, &qc->q_uuid );
2395 si->pbi->bi_cq = qc;
2399 qc->q_sizelimit = rs->sr_nentries;
2409 ldap_pvt_thread_rdwr_wunlock(&qc->rwlock);
2410 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2411 cm->num_cached_queries++;
2412 Debug( pcache_debug, "STORED QUERIES = %lu\n",
2413 cm->num_cached_queries, 0, 0 );
2414 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2416 /* If the consistency checker suspended itself,
2419 if ( cm->cc_paused == PCACHE_CC_PAUSED ) {
2420 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2421 if ( cm->cc_paused == PCACHE_CC_PAUSED ) {
2423 ldap_pvt_runqueue_resched( &slapd_rq, cm->cc_arg, 0 );
2425 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2428 } else if ( si->count ) {
2429 /* duplicate query, free it */
2431 for (;si->head; si->head=e) {
2432 e = si->head->e_private;
2433 si->head->e_private = NULL;
2434 entry_free(si->head);
2439 filter_free( si->query.filter );
2443 return SLAP_CB_CONTINUE;
2451 struct search_info *si = op->o_callback->sc_private;
2453 if ( si->swap_saved_attrs ) {
2454 rs->sr_attrs = si->save_attrs;
2455 op->ors_attrs = si->save_attrs;
2458 if ( rs->sr_type == REP_SEARCH ) {
2459 /* don't return more entries than requested by the client */
2460 if ( si->slimit_exceeded ) {
2464 } else if ( rs->sr_type == REP_RESULT ) {
2467 if ( rs->sr_err == LDAP_SUCCESS ) {
2468 si->caching_reason = PC_POSITIVE;
2470 } else if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED
2471 && si->qtemp->limitttl )
2473 si->caching_reason = PC_SIZELIMIT;
2476 } else if ( si->qtemp->negttl && !si->count && !si->over &&
2477 rs->sr_err == LDAP_SUCCESS )
2479 si->caching_reason = PC_NEGATIVE;
2483 if ( si->slimit_exceeded ) {
2484 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
2488 return SLAP_CB_CONTINUE;
2491 /* NOTE: this is a quick workaround to let pcache minimally interact
2492 * with pagedResults. A more articulated solutions would be to
2493 * perform the remote query without control and cache all results,
2494 * performing the pagedResults search only within the client
2495 * and the proxy. This requires pcache to understand pagedResults. */
2497 pcache_chk_controls(
2501 const char *non = "";
2502 const char *stripped = "";
2504 switch( op->o_pagedresults ) {
2505 case SLAP_CONTROL_NONCRITICAL:
2507 stripped = "; stripped";
2510 case SLAP_CONTROL_CRITICAL:
2511 Debug( pcache_debug, "%s: "
2512 "%scritical pagedResults control "
2513 "disabled with proxy cache%s.\n",
2514 op->o_log_prefix, non, stripped );
2516 slap_remove_control( op, rs, slap_cids.sc_pagedResults, NULL );
2520 rs->sr_err = SLAP_CB_CONTINUE;
2527 typedef struct bindcacheinfo {
2533 pc_bind_save( Operation *op, SlapReply *rs )
2535 if ( rs->sr_err == LDAP_SUCCESS ) {
2536 bindcacheinfo *bci = op->o_callback->sc_private;
2537 slap_overinst *on = bci->on;
2538 cache_manager *cm = on->on_bi.bi_private;
2539 struct berval vals[2];
2540 const char *text = NULL;
2542 BER_BVZERO( &vals[0] );
2543 BER_BVZERO( &vals[1] );
2544 slap_passwd_hash( &op->orb_cred, &vals[0], &text );
2545 if ( BER_BVISEMPTY( &vals[0] )) {
2546 Debug( pcache_debug, "pcache_bind_save: hash failed %s\n",
2549 Operation op2 = *op;
2551 SlapReply sr = { REP_RESULT };
2552 slap_callback cb = { 0, slap_null_cb, 0, 0 };
2554 mod.sml_op = LDAP_MOD_REPLACE;
2556 mod.sml_desc = slap_schema.si_ad_userPassword;
2557 mod.sml_type = mod.sml_desc->ad_cname;
2558 mod.sml_values = vals;
2559 mod.sml_nvalues = NULL;
2560 mod.sml_numvals = 1;
2561 mod.sml_next = NULL;
2563 op2.o_tag = LDAP_REQ_MODIFY;
2564 op2.orm_modlist = &mod;
2566 op2.o_dn = op2.o_bd->be_rootdn;
2567 op2.o_ndn = op2.o_bd->be_rootndn;
2568 op2.o_callback = &cb;
2569 Debug( pcache_debug, "pcache_bind_save: caching bind for %s\n",
2570 op->o_req_dn.bv_val, 0, 0 );
2571 if ( op2.o_bd->be_modify( &op2, &sr ) == LDAP_SUCCESS )
2572 bci->qc->bindref_time = op->o_time + bci->qc->qtemp->bindttr;
2573 ch_free( vals[0].bv_val );
2576 return SLAP_CB_CONTINUE;
2580 pc_bind_attrs( Operation *op, Entry *e, QueryTemplate *temp,
2581 struct berval *fbv )
2584 struct berval *vals, pres = BER_BVC("*");
2588 vals = op->o_tmpalloc( temp->bindnattrs * sizeof( struct berval ),
2591 for ( i=0; i<temp->bindnattrs; i++ ) {
2592 a = attr_find( e->e_attrs, temp->bindfattrs[i] );
2593 if ( a && a->a_vals ) {
2594 vals[i] = a->a_vals[0];
2595 len += a->a_vals[0].bv_len;
2600 fbv->bv_len = len + temp->bindftemp.bv_len;
2601 fbv->bv_val = op->o_tmpalloc( fbv->bv_len + 1, op->o_tmpmemctx );
2603 p1 = temp->bindftemp.bv_val;
2608 if ( p1[0] == '=' && p1[1] == ')' ) {
2609 AC_MEMCPY( p2, vals[i].bv_val, vals[i].bv_len );
2610 p2 += vals[i].bv_len;
2616 op->o_tmpfree( vals, op->o_tmpmemctx );
2617 return str2filter_x( op, fbv->bv_val );
2620 /* Check if the requested entry is from the cache and has a valid
2621 * ttr and password hash
2624 pc_bind_search( Operation *op, SlapReply *rs )
2626 if ( rs->sr_type == REP_SEARCH ) {
2627 bindinfo *pbi = op->o_callback->sc_private;
2629 /* We only care if this is an already cached result and we're
2630 * below the refresh time, or we're offline.
2633 if (( pbi->bi_cm->cc_paused & PCACHE_CC_OFFLINE ) ||
2634 op->o_time < pbi->bi_cq->bindref_time ) {
2637 /* See if a recognized password is hashed here */
2638 a = attr_find( rs->sr_entry->e_attrs,
2639 slap_schema.si_ad_userPassword );
2640 if ( a && a->a_vals[0].bv_val[0] == '{' &&
2641 lutil_passwd_scheme( a->a_vals[0].bv_val ))
2642 pbi->bi_flags |= BI_HASHED;
2644 Debug( pcache_debug, "pc_bind_search: cache is stale, "
2645 "reftime: %ld, current time: %ld\n",
2646 pbi->bi_cq->bindref_time, op->o_time, 0 );
2648 } else if ( pbi->bi_si ) {
2649 /* This search result is going into the cache */
2653 filter_free( pbi->bi_si->query.filter );
2654 f = pc_bind_attrs( op, rs->sr_entry, pbi->bi_templ, &fbv );
2655 op->o_tmpfree( fbv.bv_val, op->o_tmpmemctx );
2656 pbi->bi_si->query.filter = filter_dup( f, NULL );
2657 filter_free_x( op, f, 1 );
2663 /* We always want pc_bind_search to run after the search handlers */
2665 pc_bind_resp( Operation *op, SlapReply *rs )
2667 bindinfo *pbi = op->o_callback->sc_private;
2668 if ( !( pbi->bi_flags & BI_DIDCB )) {
2669 slap_callback *sc = op->o_callback;
2670 while ( sc && sc->sc_response != pcache_response )
2673 sc = op->o_callback;
2674 pbi->bi_cb.sc_next = sc->sc_next;
2675 sc->sc_next = &pbi->bi_cb;
2676 pbi->bi_flags |= BI_DIDCB;
2678 return SLAP_CB_CONTINUE;
2686 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2687 cache_manager *cm = on->on_bi.bi_private;
2688 QueryTemplate *temp;
2690 slap_callback cb = { 0 }, *sc;
2694 int is_priv = 0, rc;
2696 #ifdef PCACHE_CONTROL_PRIVDB
2697 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL )
2701 /* Skip if we're not configured for Binds, and no priv control */
2702 if ( !cm->cache_binds && !is_priv )
2703 return SLAP_CB_CONTINUE;
2705 /* The cache DB isn't open yet */
2706 if ( cm->defer_db_open ) {
2708 /* Just passthru for now */
2709 return SLAP_CB_CONTINUE;
2711 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
2712 "pcachePrivDB: cacheDB not available" );
2719 slap_callback *save_cb;
2721 /* FIXME: might be a little bit exaggerated... */
2722 if ( !be_isroot( op ) ) {
2723 save_cb = op->o_callback;
2724 op->o_callback = NULL;
2725 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
2726 "pcachePrivDB: operation not allowed" );
2727 op->o_callback = save_cb;
2732 /* execute, if possible */
2733 if ( cm->db.be_bind != NULL ) {
2737 rc = op2.o_bd->be_bind( &op2, rs );
2738 if ( rc == LDAP_SUCCESS ) {
2739 op->o_conn->c_authz_cookie = cm->db.be_private;
2742 /* otherwise fall back to error */
2743 save_cb = op->o_callback;
2744 op->o_callback = NULL;
2745 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
2746 "operation not supported with pcachePrivDB control" );
2747 op->o_callback = save_cb;
2753 /* First find a matching template with Bind info */
2754 for ( temp=cm->qm->templates; temp; temp=temp->qmnext ) {
2755 if ( temp->bindttr && dnIsSuffix( &op->o_req_ndn, &temp->bindbase ))
2758 /* Didn't find a suitable template, just passthru */
2760 return SLAP_CB_CONTINUE;
2762 /* See if the entry is already locally cached. If so, we can
2763 * populate the query filter to retrieve the cached query. We
2764 * need to check the bindrefresh time in the query.
2767 op2.o_dn = op->o_bd->be_rootdn;
2768 op2.o_ndn = op->o_bd->be_rootndn;
2773 rc = be_entry_get_rw( &op2, &op->o_req_ndn, NULL, NULL, 0, &e );
2774 if ( rc == LDAP_SUCCESS && e ) {
2778 bi.bi_flags |= BI_LOOKUP;
2779 op2.ors_filter = pc_bind_attrs( op, e, temp, &op2.ors_filterstr );
2780 be_entry_release_r( &op2, e );
2782 op2.ors_filter = temp->bindfilter;
2783 op2.ors_filterstr = temp->bindfilterstr;
2786 op2.o_bd = op->o_bd;
2787 op2.o_tag = LDAP_REQ_SEARCH;
2788 op2.ors_scope = LDAP_SCOPE_BASE;
2789 op2.ors_deref = LDAP_DEREF_NEVER;
2791 op2.ors_tlimit = SLAP_NO_LIMIT;
2792 op2.ors_limit = NULL;
2793 op2.ors_attrs = cm->qm->attr_sets[temp->attr_set_index].attrs;
2794 op2.ors_attrsonly = 0;
2796 /* We want to invoke search at the same level of the stack
2797 * as we're already at...
2804 bi.bi_cb.sc_response = pc_bind_search;
2805 bi.bi_cb.sc_cleanup = NULL;
2806 bi.bi_cb.sc_private = &bi;
2807 cb.sc_private = &bi;
2808 cb.sc_response = pc_bind_resp;
2809 op2.o_callback = &cb;
2810 overlay_op_walk( &op2, rs, op_search, on->on_info, on );
2812 /* OK, just bind locally */
2813 if ( bi.bi_flags & BI_HASHED ) {
2814 BackendDB *be = op->o_bd;
2817 if ( op->o_bd->be_bind( op, rs ) == LDAP_SUCCESS ) {
2818 op->o_conn->c_authz_cookie = cm->db.be_private;
2824 /* We have a cached query to work with */
2826 sc = op->o_tmpalloc( sizeof(slap_callback) + sizeof(bindcacheinfo),
2828 sc->sc_response = pc_bind_save;
2829 sc->sc_cleanup = NULL;
2830 sc->sc_private = sc+1;
2831 bci = sc->sc_private;
2832 sc->sc_next = op->o_callback;
2833 op->o_callback = sc;
2837 return SLAP_CB_CONTINUE;
2840 #ifdef PCACHE_CONTROL_PRIVDB
2846 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2847 cache_manager *cm = on->on_bi.bi_private;
2848 slap_callback *save_cb;
2851 /* skip if control is unset */
2852 if ( op->o_ctrlflag[ privDB_cid ] != SLAP_CONTROL_CRITICAL ) {
2853 return SLAP_CB_CONTINUE;
2856 /* The cache DB isn't open yet */
2857 if ( cm->defer_db_open ) {
2858 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
2859 "pcachePrivDB: cacheDB not available" );
2863 /* FIXME: might be a little bit exaggerated... */
2864 if ( !be_isroot( op ) ) {
2865 save_cb = op->o_callback;
2866 op->o_callback = NULL;
2867 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
2868 "pcachePrivDB: operation not allowed" );
2869 op->o_callback = save_cb;
2874 /* map tag to operation */
2875 type = slap_req2op( op->o_tag );
2876 if ( type != SLAP_OP_LAST ) {
2880 /* execute, if possible */
2881 func = &cm->db.be_bind;
2882 if ( func[ type ] != NULL ) {
2883 Operation op2 = *op;
2887 rc = func[ type ]( &op2, rs );
2888 if ( type == SLAP_OP_BIND && rc == LDAP_SUCCESS ) {
2889 op->o_conn->c_authz_cookie = cm->db.be_private;
2896 /* otherwise fall back to error */
2897 save_cb = op->o_callback;
2898 op->o_callback = NULL;
2899 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
2900 "operation not supported with pcachePrivDB control" );
2901 op->o_callback = save_cb;
2905 #endif /* PCACHE_CONTROL_PRIVDB */
2912 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2913 cache_manager *cm = on->on_bi.bi_private;
2914 query_manager* qm = cm->qm;
2919 QueryTemplate *qtemp = NULL;
2920 bindinfo *pbi = NULL;
2923 CachedQuery *answerable = NULL;
2926 struct berval tempstr;
2928 #ifdef PCACHE_CONTROL_PRIVDB
2929 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL ) {
2930 return pcache_op_privdb( op, rs );
2932 #endif /* PCACHE_CONTROL_PRIVDB */
2934 /* The cache DB isn't open yet */
2935 if ( cm->defer_db_open ) {
2936 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
2937 "pcachePrivDB: cacheDB not available" );
2941 /* pickup runtime ACL changes */
2942 cm->db.be_acl = op->o_bd->be_acl;
2945 /* See if we're processing a Bind request */
2946 slap_callback *cb = op->o_callback;
2948 for ( ; cb; cb=cb->sc_next ) {
2949 if ( cb->sc_response == pc_bind_resp ) {
2950 pbi = cb->sc_private;
2956 /* FIXME: cannot cache/answer requests with pagedResults control */
2958 query.filter = op->ors_filter;
2961 query.base = pbi->bi_templ->bindbase;
2962 query.scope = pbi->bi_templ->bindscope;
2963 attr_set = pbi->bi_templ->attr_set_index;
2965 qtemp = pbi->bi_templ;
2966 if ( pbi->bi_flags & BI_LOOKUP )
2967 answerable = qm->qcfunc(op, qm, &query, qtemp);
2970 tempstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len+1,
2973 if ( filter2template( op, op->ors_filter, &tempstr ))
2975 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
2976 return SLAP_CB_CONTINUE;
2979 Debug( pcache_debug, "query template of incoming query = %s\n",
2980 tempstr.bv_val, 0, 0 );
2983 attr_set = get_attr_set(op->ors_attrs, qm, cm->numattrsets);
2985 query.base = op->o_req_ndn;
2986 query.scope = op->ors_scope;
2988 /* check for query containment */
2989 if (attr_set > -1) {
2990 QueryTemplate *qt = qm->attr_sets[attr_set].templates;
2991 for (; qt; qt = qt->qtnext ) {
2992 /* find if template i can potentially answer tempstr */
2993 if ( ber_bvstrcasecmp( &qt->querystr, &tempstr ) != 0 )
2997 Debug( pcache_debug, "Entering QC, querystr = %s\n",
2998 op->ors_filterstr.bv_val, 0, 0 );
2999 answerable = qm->qcfunc(op, qm, &query, qt);
3001 /* if != NULL, rlocks qtemp->t_rwlock */
3006 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
3010 BackendDB *save_bd = op->o_bd;
3012 ldap_pvt_thread_mutex_lock( &answerable->answerable_cnt_mutex );
3013 answerable->answerable_cnt++;
3014 /* we only care about refcnts if we're refreshing */
3015 if ( answerable->refresh_time )
3016 answerable->refcnt++;
3017 Debug( pcache_debug, "QUERY ANSWERABLE (answered %lu times)\n",
3018 answerable->answerable_cnt, 0, 0 );
3019 ldap_pvt_thread_mutex_unlock( &answerable->answerable_cnt_mutex );
3021 ldap_pvt_thread_rdwr_rlock(&answerable->rwlock);
3022 if ( BER_BVISNULL( &answerable->q_uuid )) {
3023 /* No entries cached, just an empty result set */
3025 send_ldap_result( op, rs );
3027 /* Let Bind know we used a cached query */
3029 pbi->bi_cq = answerable;
3033 if ( cm->response_cb == PCACHE_RESPONSE_CB_TAIL ) {
3034 /* The cached entry was already processed by any
3035 * other overlays, so don't let it get processed again.
3037 op->o_callback = NULL;
3040 i = cm->db.bd_info->bi_op_search( op, rs );
3042 ldap_pvt_thread_rdwr_runlock(&answerable->rwlock);
3043 /* locked by qtemp->qcfunc (query_containment) */
3044 ldap_pvt_thread_rdwr_runlock(&qtemp->t_rwlock);
3049 Debug( pcache_debug, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
3051 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
3052 if (cm->num_cached_queries >= cm->max_queries) {
3055 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
3057 if (op->ors_attrsonly)
3062 struct search_info *si;
3064 Debug( pcache_debug, "QUERY CACHEABLE\n", 0, 0, 0 );
3065 query.filter = filter_dup(op->ors_filter, NULL);
3067 cb = op->o_tmpalloc( sizeof(*cb) + sizeof(*si), op->o_tmpmemctx );
3068 cb->sc_response = pcache_response;
3069 cb->sc_cleanup = pcache_op_cleanup;
3070 cb->sc_private = (cb+1);
3071 si = cb->sc_private;
3075 si->max = cm->num_entries_limit ;
3079 si->slimit_exceeded = 0;
3080 si->caching_reason = PC_IGNORE;
3081 if ( op->ors_slimit > 0 && op->ors_slimit < cm->num_entries_limit ) {
3082 si->slimit = op->ors_slimit;
3083 op->ors_slimit = cm->num_entries_limit;
3087 si->swap_saved_attrs = 1;
3088 si->save_attrs = op->ors_attrs;
3093 op->ors_attrs = qtemp->t_attrs.attrs;
3095 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
3096 cb->sc_next = op->o_callback;
3097 op->o_callback = cb;
3100 slap_callback **pcb;
3102 /* need to move the callback at the end, in case other
3103 * overlays are present, so that the final entry is
3104 * actually cached */
3106 for ( pcb = &op->o_callback; *pcb; pcb = &(*pcb)->sc_next );
3111 Debug( pcache_debug, "QUERY NOT CACHEABLE\n",
3115 return SLAP_CB_CONTINUE;
3120 AttributeName* attrs,
3128 for ( ; attrs[count].an_name.bv_val; count++ );
3131 /* recognize a single "*" or a "1.1" */
3134 attrs = slap_anlist_all_user_attributes;
3136 } else if ( count == 1 && bvmatch( &attrs[0].an_name, slap_bv_no_attrs ) ) {
3141 for ( i = 0; i < num; i++ ) {
3145 if ( count > qm->attr_sets[i].count ) {
3150 if ( !qm->attr_sets[i].count ) {
3156 for ( a2 = attrs; a2->an_name.bv_val; a2++ ) {
3157 if ( !an_find( qm->attr_sets[i].attrs, &a2->an_name ) ) {
3175 /* Refresh a cached query:
3176 * 1: Replay the query on the remote DB and merge each entry into
3177 * the local DB. Remember the DNs of each remote entry.
3178 * 2: Search the local DB for all entries matching this queryID.
3179 * Delete any entry whose DN is not in the list from (1).
3181 typedef struct dnlist {
3182 struct dnlist *next;
3187 typedef struct refresh_info {
3195 static dnlist *dnl_alloc( Operation *op, struct berval *bvdn )
3197 dnlist *dn = op->o_tmpalloc( sizeof(dnlist) + bvdn->bv_len + 1,
3199 dn->dn.bv_len = bvdn->bv_len;
3200 dn->dn.bv_val = (char *)(dn+1);
3201 AC_MEMCPY( dn->dn.bv_val, bvdn->bv_val, dn->dn.bv_len );
3202 dn->dn.bv_val[dn->dn.bv_len] = '\0';
3207 refresh_merge( Operation *op, SlapReply *rs )
3209 if ( rs->sr_type == REP_SEARCH ) {
3210 refresh_info *ri = op->o_callback->sc_private;
3211 BackendDB *be = op->o_bd;
3217 ocb = op->o_callback;
3218 /* Find local entry, merge */
3219 op->o_bd = ri->ri_be;
3220 rc = be_entry_get_rw( op, &rs->sr_entry->e_nname, NULL, NULL, 0, &e );
3221 if ( rc != LDAP_SUCCESS || e == NULL ) {
3222 /* No local entry, just add it. FIXME: we are not checking
3223 * the cache entry limit here
3225 merge_entry( op, rs->sr_entry, &ri->ri_q->q_uuid );
3227 /* Entry exists, update it */
3230 Modifications *modlist, *mods = NULL;
3231 const char* text = NULL;
3232 char textbuf[SLAP_TEXT_BUFLEN];
3233 size_t textlen = sizeof(textbuf);
3234 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
3238 /* Get a copy of only the attrs we requested */
3239 for ( a=e->e_attrs; a; a=a->a_next ) {
3240 if ( ad_inlist( a->a_desc, rs->sr_attrs )) {
3241 *b = attr_alloc( a->a_desc );
3243 /* The actual values still belong to e */
3244 (*b)->a_flags |= SLAP_ATTR_DONT_FREE_VALS |
3245 SLAP_ATTR_DONT_FREE_DATA;
3246 b = &((*b)->a_next);
3250 slap_entry2mods( rs->sr_entry, &modlist, &text, textbuf, textlen );
3251 syncrepl_diff_entry( op, ne.e_attrs, rs->sr_entry->e_attrs,
3252 &mods, &modlist, 0 );
3253 be_entry_release_r( op, e );
3254 attrs_free( ne.e_attrs );
3255 slap_mods_free( modlist, 1 );
3256 /* mods is NULL if there are no changes */
3258 struct berval dn = op->o_req_dn;
3259 struct berval ndn = op->o_req_ndn;
3260 op->o_tag = LDAP_REQ_MODIFY;
3261 op->orm_modlist = mods;
3262 op->o_req_dn = rs->sr_entry->e_name;
3263 op->o_req_ndn = rs->sr_entry->e_nname;
3264 op->o_callback = &cb;
3265 op->o_bd->be_modify( op, rs );
3266 slap_mods_free( mods, 1 );
3268 op->o_req_ndn = ndn;
3272 /* Add DN to list */
3273 dnl = dnl_alloc( op, &rs->sr_entry->e_nname );
3275 if ( ri->ri_tail ) {
3276 ri->ri_tail->next = dnl;
3281 op->o_callback = ocb;
3287 refresh_purge( Operation *op, SlapReply *rs )
3289 if ( rs->sr_type == REP_SEARCH ) {
3290 refresh_info *ri = op->o_callback->sc_private;
3294 /* Did the entry exist on the remote? */
3295 for ( dn=&ri->ri_dns; *dn; dn = &(*dn)->next ) {
3296 if ( dn_match( &(*dn)->dn, &rs->sr_entry->e_nname )) {
3297 dnlist *dnext = (*dn)->next;
3298 op->o_tmpfree( *dn, op->o_tmpmemctx );
3304 /* No, so put it on the list to delete */
3307 dnlist *dnl = dnl_alloc( op, &rs->sr_entry->e_nname );
3308 dnl->next = ri->ri_dels;
3310 a = attr_find( rs->sr_entry->e_attrs, ad_queryId );
3311 /* If ours is the only queryId, delete entry */
3312 dnl->delete = ( a->a_numvals == 1 );
3319 refresh_query( Operation *op, SlapReply *rs, CachedQuery *query,
3322 slap_callback cb = { 0 };
3323 refresh_info ri = { 0 };
3324 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(pcacheQueryID=)" ) ];
3325 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
3326 Filter filter = {LDAP_FILTER_EQUALITY};
3327 AttributeName attrs[ 2 ] = {{{ 0 }}};
3331 ldap_pvt_thread_mutex_lock( &query->answerable_cnt_mutex );
3333 ldap_pvt_thread_mutex_unlock( &query->answerable_cnt_mutex );
3335 cb.sc_response = refresh_merge;
3336 cb.sc_private = &ri;
3339 ri.ri_be = op->o_bd;
3342 op->o_tag = LDAP_REQ_SEARCH;
3343 op->o_protocol = LDAP_VERSION3;
3344 op->o_callback = &cb;
3345 op->o_do_not_cache = 1;
3347 op->o_req_dn = query->qbase->base;
3348 op->o_req_ndn = query->qbase->base;
3349 op->ors_scope = query->scope;
3350 op->ors_slimit = SLAP_NO_LIMIT;
3351 op->ors_tlimit = SLAP_NO_LIMIT;
3352 op->ors_limit = NULL;
3353 op->ors_filter = query->filter;
3354 filter2bv_x( op, query->filter, &op->ors_filterstr );
3355 op->ors_attrs = query->qtemp->t_attrs.attrs;
3356 op->ors_attrsonly = 0;
3358 op->o_bd = on->on_info->oi_origdb;
3359 rc = op->o_bd->be_search( op, rs );
3361 op->o_bd = ri.ri_be;
3365 /* Get the DNs of all entries matching this query */
3366 cb.sc_response = refresh_purge;
3368 op->o_bd = ri.ri_be;
3369 op->o_req_dn = op->o_bd->be_suffix[0];
3370 op->o_req_ndn = op->o_bd->be_nsuffix[0];
3371 op->ors_scope = LDAP_SCOPE_SUBTREE;
3372 op->ors_deref = LDAP_DEREF_NEVER;
3373 op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
3374 "(%s=%s)", ad_queryId->ad_cname.bv_val, query->q_uuid.bv_val);
3375 filter.f_ava = &ava;
3376 filter.f_av_desc = ad_queryId;
3377 filter.f_av_value = query->q_uuid;
3378 attrs[ 0 ].an_desc = ad_queryId;
3379 attrs[ 0 ].an_name = ad_queryId->ad_cname;
3380 op->ors_attrs = attrs;
3381 op->ors_attrsonly = 0;
3382 rs->sr_entry = NULL;
3383 rs->sr_nentries = 0;
3384 rc = op->o_bd->be_search( op, rs );
3385 if ( rc ) goto leave;
3387 while (( dn = ri.ri_dels )) {
3388 op->o_req_dn = dn->dn;
3389 op->o_req_ndn = dn->dn;
3391 op->o_tag = LDAP_REQ_DELETE;
3392 op->o_bd->be_delete( op, rs );
3395 struct berval vals[2];
3397 vals[0] = query->q_uuid;
3398 BER_BVZERO( &vals[1] );
3399 mod.sml_op = LDAP_MOD_DELETE;
3401 mod.sml_desc = ad_queryId;
3402 mod.sml_type = ad_queryId->ad_cname;
3403 mod.sml_values = vals;
3404 mod.sml_nvalues = NULL;
3405 mod.sml_numvals = 1;
3406 mod.sml_next = NULL;
3408 op->o_tag = LDAP_REQ_MODIFY;
3409 op->orm_modlist = &mod;
3410 op->o_bd->be_modify( op, rs );
3412 ri.ri_dels = dn->next;
3413 op->o_tmpfree( dn, op->o_tmpmemctx );
3417 /* reset our local heap, we're done with it */
3418 slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, op->o_threadctx, 1 );
3427 struct re_s *rtask = arg;
3428 slap_overinst *on = rtask->arg;
3429 cache_manager *cm = on->on_bi.bi_private;
3430 query_manager *qm = cm->qm;
3431 Connection conn = {0};
3432 OperationBuffer opbuf;
3435 SlapReply rs = {REP_RESULT};
3436 CachedQuery *query, *qprev;
3437 int return_val, pause = PCACHE_CC_PAUSED;
3438 QueryTemplate *templ;
3440 /* Don't expire anything when we're offline */
3441 if ( cm->cc_paused & PCACHE_CC_OFFLINE ) {
3442 pause = PCACHE_CC_OFFLINE;
3446 connection_fake_init( &conn, &opbuf, ctx );
3450 op->o_dn = cm->db.be_rootdn;
3451 op->o_ndn = cm->db.be_rootndn;
3455 for (templ = qm->templates; templ; templ=templ->qmnext) {
3457 if ( !templ->query_last ) continue;
3459 op->o_time = slap_get_time();
3460 if ( !templ->ttr ) {
3462 if ( templ->negttl && templ->negttl < ttl )
3463 ttl = templ->negttl;
3464 if ( templ->limitttl && templ->limitttl < ttl )
3465 ttl = templ->limitttl;
3466 /* The oldest timestamp that needs expiration checking */
3470 for ( query=templ->query_last; query; query=qprev ) {
3471 qprev = query->prev;
3472 if ( query->refresh_time && query->refresh_time < op->o_time ) {
3473 /* A refresh will extend the expiry if the query has been
3474 * referenced, but not if it's unreferenced. If the
3475 * expiration has been hit, then skip the refresh since
3476 * we're just going to discard the result anyway.
3478 if ( query->refcnt )
3479 query->expiry_time = op->o_time + templ->ttl;
3480 if ( query->expiry_time > op->o_time ) {
3481 refresh_query( op, &rs, query, on );
3486 if (query->expiry_time < op->o_time) {
3488 Debug( pcache_debug, "Lock CR index = %p\n",
3489 (void *) templ, 0, 0 );
3490 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
3491 if ( query == templ->query_last ) {
3493 remove_from_template(query, templ);
3494 Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
3495 (void *) templ, templ->no_of_queries, 0 );
3496 Debug( pcache_debug, "Unlock CR index = %p\n",
3497 (void *) templ, 0, 0 );
3499 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
3503 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
3504 remove_query(qm, query);
3505 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
3506 if ( BER_BVISNULL( &query->q_uuid ))
3509 return_val = remove_query_data(op, &rs, &query->q_uuid);
3510 Debug( pcache_debug, "STALE QUERY REMOVED, SIZE=%d\n",
3512 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
3513 cm->cur_entries -= return_val;
3514 cm->num_cached_queries--;
3515 Debug( pcache_debug, "STORED QUERIES = %lu\n",
3516 cm->num_cached_queries, 0, 0 );
3517 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
3518 Debug( pcache_debug,
3519 "STALE QUERY REMOVED, CACHE ="
3521 cm->cur_entries, 0, 0 );
3523 } else if ( !templ->ttr && query->expiry_time > ttl ) {
3524 /* We don't need to check for refreshes, and this
3525 * query's expiry is too new, and all subsequent queries
3526 * will be newer yet. So stop looking.
3528 * If we have refreshes, then we always have to walk the
3529 * entire query list.
3537 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3538 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
3539 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
3541 /* If there were no queries, defer processing for a while */
3542 if ( cm->cc_paused != pause )
3543 cm->cc_paused = pause;
3544 ldap_pvt_runqueue_resched( &slapd_rq, rtask, pause );
3546 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3551 #define MAX_ATTR_SETS 500
3564 static ConfigDriver pc_cf_gen;
3565 static ConfigLDAPadd pc_ldadd;
3566 static ConfigCfAdd pc_cfadd;
3568 static ConfigTable pccfg[] = {
3569 { "pcache", "backend> <max_entries> <numattrsets> <entry limit> "
3571 6, 6, 0, ARG_MAGIC|ARG_NO_DELETE|PC_MAIN, pc_cf_gen,
3572 "( OLcfgOvAt:2.1 NAME ( 'olcPcache' 'olcProxyCache' ) "
3573 "DESC 'Proxy Cache basic parameters' "
3574 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
3575 { "pcacheAttrset", "index> <attributes...",
3576 2, 0, 0, ARG_MAGIC|PC_ATTR, pc_cf_gen,
3577 "( OLcfgOvAt:2.2 NAME ( 'olcPcacheAttrset' 'olcProxyAttrset' ) "
3578 "DESC 'A set of attributes to cache' "
3579 "SYNTAX OMsDirectoryString )", NULL, NULL },
3580 { "pcacheTemplate", "filter> <attrset-index> <TTL> <negTTL> "
3582 4, 7, 0, ARG_MAGIC|PC_TEMP, pc_cf_gen,
3583 "( OLcfgOvAt:2.3 NAME ( 'olcPcacheTemplate' 'olcProxyCacheTemplate' ) "
3584 "DESC 'Filter template, attrset, cache TTL, "
3585 "optional negative TTL, optional sizelimit TTL, "
3587 "SYNTAX OMsDirectoryString )", NULL, NULL },
3588 { "pcachePosition", "head|tail(default)",
3589 2, 2, 0, ARG_MAGIC|PC_RESP, pc_cf_gen,
3590 "( OLcfgOvAt:2.4 NAME 'olcPcachePosition' "
3591 "DESC 'Response callback position in overlay stack' "
3592 "SYNTAX OMsDirectoryString )", NULL, NULL },
3593 { "pcacheMaxQueries", "queries",
3594 2, 2, 0, ARG_INT|ARG_MAGIC|PC_QUERIES, pc_cf_gen,
3595 "( OLcfgOvAt:2.5 NAME ( 'olcPcacheMaxQueries' 'olcProxyCacheQueries' ) "
3596 "DESC 'Maximum number of queries to cache' "
3597 "SYNTAX OMsInteger )", NULL, NULL },
3598 { "pcachePersist", "TRUE|FALSE",
3599 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, save_queries),
3600 "( OLcfgOvAt:2.6 NAME ( 'olcPcachePersist' 'olcProxySaveQueries' ) "
3601 "DESC 'Save cached queries for hot restart' "
3602 "SYNTAX OMsBoolean )", NULL, NULL },
3603 { "pcacheValidate", "TRUE|FALSE",
3604 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, check_cacheability),
3605 "( OLcfgOvAt:2.7 NAME ( 'olcPcacheValidate' 'olcProxyCheckCacheability' ) "
3606 "DESC 'Check whether the results of a query are cacheable, e.g. for schema issues' "
3607 "SYNTAX OMsBoolean )", NULL, NULL },
3608 { "pcacheOffline", "TRUE|FALSE",
3609 2, 2, 0, ARG_ON_OFF|ARG_MAGIC|PC_OFFLINE, pc_cf_gen,
3610 "( OLcfgOvAt:2.8 NAME 'olcPcacheOffline' "
3611 "DESC 'Set cache to offline mode and disable expiration' "
3612 "SYNTAX OMsBoolean )", NULL, NULL },
3613 { "pcacheBind", "filter> <attrset-index> <TTR> <scope> <base",
3614 6, 6, 0, ARG_MAGIC|PC_BIND, pc_cf_gen,
3615 "( OLcfgOvAt:2.9 NAME 'olcPcacheBind' "
3616 "DESC 'Parameters for caching Binds' "
3617 "SYNTAX OMsDirectoryString )", NULL, NULL },
3618 { "pcache-", "private database args",
3619 1, 0, STRLENOF("pcache-"), ARG_MAGIC|PC_PRIVATE_DB, pc_cf_gen,
3622 /* Legacy keywords */
3623 { "proxycache", "backend> <max_entries> <numattrsets> <entry limit> "
3625 6, 6, 0, ARG_MAGIC|ARG_NO_DELETE|PC_MAIN, pc_cf_gen,
3627 { "proxyattrset", "index> <attributes...",
3628 2, 0, 0, ARG_MAGIC|PC_ATTR, pc_cf_gen,
3630 { "proxytemplate", "filter> <attrset-index> <TTL> <negTTL",
3631 4, 7, 0, ARG_MAGIC|PC_TEMP, pc_cf_gen,
3633 { "response-callback", "head|tail(default)",
3634 2, 2, 0, ARG_MAGIC|PC_RESP, pc_cf_gen,
3636 { "proxyCacheQueries", "queries",
3637 2, 2, 0, ARG_INT|ARG_MAGIC|PC_QUERIES, pc_cf_gen,
3639 { "proxySaveQueries", "TRUE|FALSE",
3640 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, save_queries),
3642 { "proxyCheckCacheability", "TRUE|FALSE",
3643 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, check_cacheability),
3646 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
3649 static ConfigOCs pcocs[] = {
3650 { "( OLcfgOvOc:2.1 "
3651 "NAME 'olcPcacheConfig' "
3652 "DESC 'ProxyCache configuration' "
3653 "SUP olcOverlayConfig "
3654 "MUST ( olcPcache $ olcPcacheAttrset $ olcPcacheTemplate ) "
3655 "MAY ( olcPcachePosition $ olcPcacheMaxQueries $ olcPcachePersist $ "
3656 "olcPcacheValidate $ olcPcacheOffline $ olcPcacheBind ) )",
3657 Cft_Overlay, pccfg, NULL, pc_cfadd },
3658 { "( OLcfgOvOc:2.2 "
3659 "NAME 'olcPcacheDatabase' "
3660 "DESC 'Cache database configuration' "
3661 "AUXILIARY )", Cft_Misc, olcDatabaseDummy, pc_ldadd },
3665 static int pcache_db_open2( slap_overinst *on, ConfigReply *cr );
3668 pc_ldadd_cleanup( ConfigArgs *c )
3670 slap_overinst *on = c->ca_private;
3671 return pcache_db_open2( on, &c->reply );
3675 pc_ldadd( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
3680 if ( p->ce_type != Cft_Overlay || !p->ce_bi ||
3681 p->ce_bi->bi_cf_ocs != pcocs )
3682 return LDAP_CONSTRAINT_VIOLATION;
3684 on = (slap_overinst *)p->ce_bi;
3685 cm = on->on_bi.bi_private;
3687 /* Defer open if this is an LDAPadd */
3688 if ( CONFIG_ONLINE_ADD( ca ))
3689 ca->cleanup = pc_ldadd_cleanup;
3691 cm->defer_db_open = 0;
3692 ca->ca_private = on;
3693 return LDAP_SUCCESS;
3697 pc_cfadd( Operation *op, SlapReply *rs, Entry *p, ConfigArgs *ca )
3699 CfEntryInfo *pe = p->e_private;
3700 slap_overinst *on = (slap_overinst *)pe->ce_bi;
3701 cache_manager *cm = on->on_bi.bi_private;
3704 /* FIXME: should not hardcode "olcDatabase" here */
3705 bv.bv_len = snprintf( ca->cr_msg, sizeof( ca->cr_msg ),
3706 "olcDatabase=" SLAP_X_ORDERED_FMT "%s",
3707 0, cm->db.bd_info->bi_type );
3708 if ( bv.bv_len >= sizeof( ca->cr_msg ) ) {
3711 bv.bv_val = ca->cr_msg;
3713 cm->defer_db_open = 0;
3715 /* We can only create this entry if the database is table-driven
3717 if ( cm->db.bd_info->bi_cf_ocs )
3718 config_build_entry( op, rs, pe, ca, &bv, cm->db.bd_info->bi_cf_ocs,
3725 pc_cf_gen( ConfigArgs *c )
3727 slap_overinst *on = (slap_overinst *)c->bi;
3728 cache_manager* cm = on->on_bi.bi_private;
3729 query_manager* qm = cm->qm;
3730 QueryTemplate* temp;
3731 AttributeName* attr_name;
3732 AttributeName* attrarray;
3733 const char* text=NULL;
3738 if ( c->op == SLAP_CONFIG_EMIT ) {
3742 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s %d %d %d %ld",
3743 cm->db.bd_info->bi_type, cm->max_entries, cm->numattrsets,
3744 cm->num_entries_limit, cm->cc_period );
3745 bv.bv_val = c->cr_msg;
3746 value_add_one( &c->rvalue_vals, &bv );
3749 for (i=0; i<cm->numattrsets; i++) {
3750 if ( !qm->attr_sets[i].count ) continue;
3752 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ), "%d", i );
3754 /* count the attr length */
3755 for ( attr_name = qm->attr_sets[i].attrs;
3756 attr_name->an_name.bv_val; attr_name++ )
3757 bv.bv_len += attr_name->an_name.bv_len + 1;
3759 bv.bv_val = ch_malloc( bv.bv_len+1 );
3760 ptr = lutil_strcopy( bv.bv_val, c->cr_msg );
3761 for ( attr_name = qm->attr_sets[i].attrs;
3762 attr_name->an_name.bv_val; attr_name++ ) {
3764 ptr = lutil_strcopy( ptr, attr_name->an_name.bv_val );
3766 ber_bvarray_add( &c->rvalue_vals, &bv );
3768 if ( !c->rvalue_vals )
3772 for (temp=qm->templates; temp; temp=temp->qmnext) {
3773 /* HEADS-UP: always print all;
3774 * if optional == 0, ignore */
3775 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ),
3776 " %d %ld %ld %ld %ld",
3777 temp->attr_set_index,
3782 bv.bv_len += temp->querystr.bv_len + 2;
3783 bv.bv_val = ch_malloc( bv.bv_len+1 );
3786 ptr = lutil_strcopy( ptr, temp->querystr.bv_val );
3788 strcpy( ptr, c->cr_msg );
3789 ber_bvarray_add( &c->rvalue_vals, &bv );
3791 if ( !c->rvalue_vals )
3795 for (temp=qm->templates; temp; temp=temp->qmnext) {
3796 if ( !temp->bindttr ) continue;
3797 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ),
3799 temp->attr_set_index,
3801 ldap_pvt_scope2str( temp->bindscope ));
3802 bv.bv_len += temp->bindbase.bv_len + temp->bindftemp.bv_len + 3;
3803 bv.bv_val = ch_malloc( bv.bv_len+1 );
3806 ptr = lutil_strcopy( ptr, temp->bindftemp.bv_val );
3808 ptr = lutil_strcopy( ptr, c->cr_msg );
3810 ptr = lutil_strcopy( ptr, temp->bindbase.bv_val );
3813 ber_bvarray_add( &c->rvalue_vals, &bv );
3815 if ( !c->rvalue_vals )
3819 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
3820 BER_BVSTR( &bv, "head" );
3822 BER_BVSTR( &bv, "tail" );
3824 value_add_one( &c->rvalue_vals, &bv );
3827 c->value_int = cm->max_queries;
3830 c->value_int = (cm->cc_paused & PCACHE_CC_OFFLINE) != 0;
3834 } else if ( c->op == LDAP_MOD_DELETE ) {
3837 case PC_ATTR: /* FIXME */
3842 cm->cc_paused &= ~PCACHE_CC_OFFLINE;
3843 /* If there were cached queries when we went offline,
3844 * restart the checker now.
3846 if ( cm->num_cached_queries ) {
3847 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3849 ldap_pvt_runqueue_resched( &slapd_rq, cm->cc_arg, 0 );
3850 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3860 if ( cm->numattrsets > 0 ) {
3861 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"pcache\" directive already provided" );
3862 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3866 if ( lutil_atoi( &cm->numattrsets, c->argv[3] ) != 0 ) {
3867 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse num attrsets=\"%s\" (arg #3)",
3869 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3872 if ( cm->numattrsets <= 0 ) {
3873 snprintf( c->cr_msg, sizeof( c->cr_msg ), "numattrsets (arg #3) must be positive" );
3874 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3877 if ( cm->numattrsets > MAX_ATTR_SETS ) {
3878 snprintf( c->cr_msg, sizeof( c->cr_msg ), "numattrsets (arg #3) must be <= %d", MAX_ATTR_SETS );
3879 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3883 if ( !backend_db_init( c->argv[1], &cm->db, -1, NULL )) {
3884 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unknown backend type (arg #1)" );
3885 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3889 if ( lutil_atoi( &cm->max_entries, c->argv[2] ) != 0 ) {
3890 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse max entries=\"%s\" (arg #2)",
3892 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3895 if ( cm->max_entries <= 0 ) {
3896 snprintf( c->cr_msg, sizeof( c->cr_msg ), "max entries (arg #2) must be positive.\n" );
3897 Debug( LDAP_DEBUG_CONFIG, "%s: %s\n", c->log, c->cr_msg, 0 );
3901 if ( lutil_atoi( &cm->num_entries_limit, c->argv[4] ) != 0 ) {
3902 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse entry limit=\"%s\" (arg #4)",
3904 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3907 if ( cm->num_entries_limit <= 0 ) {
3908 snprintf( c->cr_msg, sizeof( c->cr_msg ), "entry limit (arg #4) must be positive" );
3909 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3912 if ( cm->num_entries_limit > cm->max_entries ) {
3913 snprintf( c->cr_msg, sizeof( c->cr_msg ), "entry limit (arg #4) must be less than max entries %d (arg #2)", cm->max_entries );
3914 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3918 if ( lutil_parse_time( c->argv[5], &t ) != 0 ) {
3919 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse period=\"%s\" (arg #5)",
3921 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3925 cm->cc_period = (time_t)t;
3926 Debug( pcache_debug,
3927 "Total # of attribute sets to be cached = %d.\n",
3928 cm->numattrsets, 0, 0 );
3929 qm->attr_sets = ( struct attr_set * )ch_calloc( cm->numattrsets,
3930 sizeof( struct attr_set ) );
3933 if ( cm->numattrsets == 0 ) {
3934 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"pcache\" directive not provided yet" );
3935 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3938 if ( lutil_atoi( &num, c->argv[1] ) != 0 ) {
3939 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse attrset #=\"%s\"",
3941 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3945 if ( num < 0 || num >= cm->numattrsets ) {
3946 snprintf( c->cr_msg, sizeof( c->cr_msg ), "attrset index %d out of bounds (must be %s%d)",
3947 num, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
3948 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3951 qm->attr_sets[num].flags |= PC_CONFIGURED;
3952 if ( c->argc == 2 ) {
3954 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3955 "need an explicit attr in attrlist; use \"*\" to indicate all attrs" );
3956 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3959 } else if ( c->argc == 3 ) {
3960 if ( strcmp( c->argv[2], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) {
3961 qm->attr_sets[num].count = 1;
3962 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 2,
3963 sizeof( AttributeName ) );
3964 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_USER_ATTRIBUTES );
3967 } else if ( strcmp( c->argv[2], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 ) {
3968 qm->attr_sets[num].count = 1;
3969 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 2,
3970 sizeof( AttributeName ) );
3971 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
3974 } else if ( strcmp( c->argv[2], LDAP_NO_ATTRS ) == 0 ) {
3977 /* else: fallthru */
3979 } else if ( c->argc == 4 ) {
3980 if ( ( strcmp( c->argv[2], LDAP_ALL_USER_ATTRIBUTES ) == 0 && strcmp( c->argv[3], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 )
3981 || ( strcmp( c->argv[2], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 && strcmp( c->argv[3], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) )
3983 qm->attr_sets[num].count = 2;
3984 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 3,
3985 sizeof( AttributeName ) );
3986 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_USER_ATTRIBUTES );
3987 BER_BVSTR( &qm->attr_sets[num].attrs[1].an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
3990 /* else: fallthru */
3993 if ( c->argc > 2 ) {
3994 int all_user = 0, all_op = 0;
3996 qm->attr_sets[num].count = c->argc - 2;
3997 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( c->argc - 1,
3998 sizeof( AttributeName ) );
3999 attr_name = qm->attr_sets[num].attrs;
4000 for ( i = 2; i < c->argc; i++ ) {
4001 attr_name->an_desc = NULL;
4002 if ( strcmp( c->argv[i], LDAP_NO_ATTRS ) == 0 ) {
4003 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4004 "invalid attr #%d \"%s\" in attrlist",
4005 i - 2, c->argv[i] );
4006 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4007 ch_free( qm->attr_sets[num].attrs );
4008 qm->attr_sets[num].attrs = NULL;
4009 qm->attr_sets[num].count = 0;
4012 if ( strcmp( c->argv[i], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) {
4014 BER_BVSTR( &attr_name->an_name, LDAP_ALL_USER_ATTRIBUTES );
4015 } else if ( strcmp( c->argv[i], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 ) {
4017 BER_BVSTR( &attr_name->an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
4019 if ( slap_str2ad( c->argv[i], &attr_name->an_desc, &text ) ) {
4020 strcpy( c->cr_msg, text );
4021 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4022 ch_free( qm->attr_sets[num].attrs );
4023 qm->attr_sets[num].attrs = NULL;
4024 qm->attr_sets[num].count = 0;
4027 attr_name->an_name = attr_name->an_desc->ad_cname;
4029 attr_name->an_oc = NULL;
4030 attr_name->an_flags = 0;
4031 if ( attr_name->an_desc == slap_schema.si_ad_objectClass )
4032 qm->attr_sets[num].flags |= PC_GOT_OC;
4034 BER_BVZERO( &attr_name->an_name );
4037 /* warn if list contains both "*" and "+" */
4038 if ( i > 4 && all_user && all_op ) {
4039 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4040 "warning: attribute list contains \"*\" and \"+\"" );
4041 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4046 if ( cm->numattrsets == 0 ) {
4047 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"pcache\" directive not provided yet" );
4048 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4051 if ( lutil_atoi( &i, c->argv[2] ) != 0 ) {
4052 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse template #=\"%s\"",
4054 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4058 if ( i < 0 || i >= cm->numattrsets ||
4059 !(qm->attr_sets[i].flags & PC_CONFIGURED )) {
4060 snprintf( c->cr_msg, sizeof( c->cr_msg ), "template index %d invalid (%s%d)",
4061 i, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
4062 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4066 AttributeName *attrs;
4068 cnt = template_attrs( c->argv[1], &qm->attr_sets[i], &attrs, &text );
4070 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse template: %s",
4072 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4075 temp = ch_calloc( 1, sizeof( QueryTemplate ));
4076 temp->qmnext = qm->templates;
4077 qm->templates = temp;
4078 temp->t_attrs.attrs = attrs;
4079 temp->t_attrs.count = cnt;
4081 ldap_pvt_thread_rdwr_init( &temp->t_rwlock );
4082 temp->query = temp->query_last = NULL;
4083 if ( lutil_parse_time( c->argv[3], &t ) != 0 ) {
4084 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4085 "unable to parse template ttl=\"%s\"",
4087 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4089 ch_free( temp->t_attrs.attrs );
4093 temp->ttl = (time_t)t;
4094 temp->negttl = (time_t)0;
4095 temp->limitttl = (time_t)0;
4096 temp->ttr = (time_t)0;
4097 switch ( c->argc ) {
4099 if ( lutil_parse_time( c->argv[6], &t ) != 0 ) {
4100 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4101 "unable to parse template ttr=\"%s\"",
4103 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4106 temp->ttr = (time_t)t;
4110 if ( lutil_parse_time( c->argv[5], &t ) != 0 ) {
4111 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4112 "unable to parse template sizelimit ttl=\"%s\"",
4114 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4117 temp->limitttl = (time_t)t;
4121 if ( lutil_parse_time( c->argv[4], &t ) != 0 ) {
4122 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4123 "unable to parse template negative ttl=\"%s\"",
4125 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4128 temp->negttl = (time_t)t;
4132 temp->no_of_queries = 0;
4134 ber_str2bv( c->argv[1], 0, 1, &temp->querystr );
4135 Debug( pcache_debug, "Template:\n", 0, 0, 0 );
4136 Debug( pcache_debug, " query template: %s\n",
4137 temp->querystr.bv_val, 0, 0 );
4138 temp->attr_set_index = i;
4139 qm->attr_sets[i].flags |= PC_REFERENCED;
4140 temp->qtnext = qm->attr_sets[i].templates;
4141 qm->attr_sets[i].templates = temp;
4142 Debug( pcache_debug, " attributes: \n", 0, 0, 0 );
4143 if ( ( attrarray = qm->attr_sets[i].attrs ) != NULL ) {
4144 for ( i=0; attrarray[i].an_name.bv_val; i++ )
4145 Debug( pcache_debug, "\t%s\n",
4146 attrarray[i].an_name.bv_val, 0, 0 );
4150 if ( !qm->templates ) {
4151 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"pcacheTemplate\" directive not provided yet" );
4152 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4155 if ( lutil_atoi( &i, c->argv[2] ) != 0 ) {
4156 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse Bind index #=\"%s\"",
4158 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4162 if ( i < 0 || i >= cm->numattrsets ||
4163 !(qm->attr_sets[i].flags & PC_CONFIGURED )) {
4164 snprintf( c->cr_msg, sizeof( c->cr_msg ), "Bind index %d invalid (%s%d)",
4165 i, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
4166 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4169 { struct berval bv, tempbv;
4170 AttributeDescription **descs;
4172 ber_str2bv( c->argv[1], 0, 0, &bv );
4173 ndescs = ftemp_attrs( &bv, &tempbv, &descs, &text );
4175 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse template: %s",
4177 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4180 for ( temp = qm->templates; temp; temp=temp->qmnext ) {
4181 if ( temp->attr_set_index == i && bvmatch( &tempbv,
4185 ch_free( tempbv.bv_val );
4188 snprintf( c->cr_msg, sizeof( c->cr_msg ), "Bind template %s %d invalid",
4190 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4193 ber_dupbv( &temp->bindftemp, &bv );
4194 temp->bindfattrs = descs;
4195 temp->bindnattrs = ndescs;
4197 if ( lutil_parse_time( c->argv[3], &t ) != 0 ) {
4198 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4199 "unable to parse bind ttr=\"%s\"",
4201 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4203 ch_free( temp->bindfattrs );
4204 temp->bindfattrs = NULL;
4205 ch_free( temp->bindftemp.bv_val );
4206 BER_BVZERO( &temp->bindftemp );
4209 num = ldap_pvt_str2scope( c->argv[4] );
4211 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4212 "unable to parse bind scope=\"%s\"",
4214 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4218 struct berval dn, ndn;
4219 ber_str2bv( c->argv[5], 0, 0, &dn );
4220 rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
4222 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4223 "invalid bind baseDN=\"%s\"",
4225 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4228 if ( temp->bindbase.bv_val )
4229 ch_free( temp->bindbase.bv_val );
4230 temp->bindbase = ndn;
4233 /* convert the template into dummy filter */
4235 char *eq = temp->bindftemp.bv_val, *e2;
4238 while ((eq = strchr(eq, '=' ))) {
4243 bv.bv_len = temp->bindftemp.bv_len + i;
4244 bv.bv_val = ch_malloc( bv.bv_len + 1 );
4245 for ( e2 = bv.bv_val, eq = temp->bindftemp.bv_val;
4256 f = str2filter( bv.bv_val );
4258 ch_free( bv.bv_val );
4259 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4260 "unable to parse bindfilter=\"%s\"", bv.bv_val );
4261 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4262 ch_free( temp->bindbase.bv_val );
4263 BER_BVZERO( &temp->bindbase );
4266 if ( temp->bindfilter )
4267 filter_free( temp->bindfilter );
4268 if ( temp->bindfilterstr.bv_val )
4269 ch_free( temp->bindfilterstr.bv_val );
4270 temp->bindfilterstr = bv;
4271 temp->bindfilter = f;
4273 temp->bindttr = (time_t)t;
4274 temp->bindscope = num;
4275 cm->cache_binds = 1;
4279 if ( strcasecmp( c->argv[1], "head" ) == 0 ) {
4280 cm->response_cb = PCACHE_RESPONSE_CB_HEAD;
4282 } else if ( strcasecmp( c->argv[1], "tail" ) == 0 ) {
4283 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
4286 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unknown specifier" );
4287 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4292 if ( c->value_int <= 0 ) {
4293 snprintf( c->cr_msg, sizeof( c->cr_msg ), "max queries must be positive" );
4294 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4297 cm->max_queries = c->value_int;
4301 cm->cc_paused |= PCACHE_CC_OFFLINE;
4303 cm->cc_paused &= ~PCACHE_CC_OFFLINE;
4306 if ( cm->db.be_private == NULL ) {
4307 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4308 "private database must be defined before setting database specific options" );
4309 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4313 if ( cm->db.bd_info->bi_cf_ocs ) {
4316 char *argv0 = c->argv[ 0 ];
4318 c->argv[ 0 ] = &argv0[ STRLENOF( "pcache-" ) ];
4320 ct = config_find_keyword( cm->db.bd_info->bi_cf_ocs->co_table, c );
4322 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4323 "private database does not recognize specific option '%s'",
4325 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4329 c->table = cm->db.bd_info->bi_cf_ocs->co_type;
4331 c->bi = c->be->bd_info;
4333 rc = config_add_vals( ct, c );
4337 c->table = c2.table;
4340 c->argv[ 0 ] = argv0;
4342 } else if ( cm->db.be_config != NULL ) {
4343 char *argv0 = c->argv[ 0 ];
4345 c->argv[ 0 ] = &argv0[ STRLENOF( "pcache-" ) ];
4346 rc = cm->db.be_config( &cm->db, c->fname, c->lineno, c->argc, c->argv );
4347 c->argv[ 0 ] = argv0;
4350 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4351 "no means to set private database specific options" );
4352 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4357 rc = SLAP_CONF_UNKNOWN;
4373 slap_overinst *on = (slap_overinst *)be->bd_info;
4374 cache_manager* cm = on->on_bi.bi_private;
4376 /* Something for the cache database? */
4377 if ( cm->db.bd_info && cm->db.bd_info->bi_db_config )
4378 return cm->db.bd_info->bi_db_config( &cm->db, fname, lineno,
4380 return SLAP_CONF_UNKNOWN;
4388 slap_overinst *on = (slap_overinst *)be->bd_info;
4392 cm = (cache_manager *)ch_malloc(sizeof(cache_manager));
4393 on->on_bi.bi_private = cm;
4395 qm = (query_manager*)ch_malloc(sizeof(query_manager));
4398 SLAP_DBFLAGS(&cm->db) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
4399 cm->db.be_private = NULL;
4400 cm->db.bd_self = &cm->db;
4402 cm->numattrsets = 0;
4403 cm->num_entries_limit = 5;
4404 cm->num_cached_queries = 0;
4405 cm->max_entries = 0;
4406 cm->cur_entries = 0;
4407 cm->max_queries = 10000;
4408 cm->save_queries = 0;
4409 cm->check_cacheability = 0;
4410 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
4411 cm->defer_db_open = 1;
4412 cm->cc_period = 1000;
4416 qm->attr_sets = NULL;
4417 qm->templates = NULL;
4419 qm->lru_bottom = NULL;
4421 qm->qcfunc = query_containment;
4422 qm->crfunc = cache_replacement;
4423 qm->addfunc = add_query;
4424 ldap_pvt_thread_mutex_init(&qm->lru_mutex);
4426 ldap_pvt_thread_mutex_init(&cm->cache_mutex);
4428 #ifndef PCACHE_MONITOR
4430 #else /* PCACHE_MONITOR */
4431 return pcache_monitor_db_init( be );
4432 #endif /* PCACHE_MONITOR */
4436 pcache_cachedquery_open_cb( Operation *op, SlapReply *rs )
4438 assert( op->o_tag == LDAP_REQ_SEARCH );
4440 if ( rs->sr_type == REP_SEARCH ) {
4443 a = attr_find( rs->sr_entry->e_attrs, ad_cachedQueryURL );
4447 assert( a->a_nvals != NULL );
4449 valsp = op->o_callback->sc_private;
4450 assert( *valsp == NULL );
4452 ber_bvarray_dup_x( valsp, a->a_nvals, op->o_tmpmemctx );
4460 pcache_cachedquery_count_cb( Operation *op, SlapReply *rs )
4462 assert( op->o_tag == LDAP_REQ_SEARCH );
4464 if ( rs->sr_type == REP_SEARCH ) {
4465 int *countp = (int *)op->o_callback->sc_private;
4478 cache_manager *cm = on->on_bi.bi_private;
4479 query_manager* qm = cm->qm;
4482 rc = backend_startup_one( &cm->db, cr );
4484 cm->defer_db_open = 0;
4487 /* There is no runqueue in TOOL mode */
4488 if (( slapMode & SLAP_SERVER_MODE ) && rc == 0 ) {
4489 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4490 ldap_pvt_runqueue_insert( &slapd_rq, cm->cc_period,
4491 consistency_check, on,
4492 "pcache_consistency", cm->db.be_suffix[0].bv_val );
4493 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4495 /* Cached database must have the rootdn */
4496 if ( BER_BVISNULL( &cm->db.be_rootndn )
4497 || BER_BVISEMPTY( &cm->db.be_rootndn ) )
4499 Debug( LDAP_DEBUG_ANY, "pcache_db_open(): "
4500 "underlying database of type \"%s\"\n"
4501 " serving naming context \"%s\"\n"
4502 " has no \"rootdn\", required by \"pcache\".\n",
4503 on->on_info->oi_orig->bi_type,
4504 cm->db.be_suffix[0].bv_val, 0 );
4508 if ( cm->save_queries ) {
4509 void *thrctx = ldap_pvt_thread_pool_context();
4510 Connection conn = { 0 };
4511 OperationBuffer opbuf;
4513 slap_callback cb = { 0 };
4514 SlapReply rs = { 0 };
4515 BerVarray vals = NULL;
4516 Filter f = { 0 }, f2 = { 0 };
4517 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
4518 AttributeName attrs[ 2 ] = {{{ 0 }}};
4520 connection_fake_init( &conn, &opbuf, thrctx );
4525 op->o_tag = LDAP_REQ_SEARCH;
4526 op->o_protocol = LDAP_VERSION3;
4527 cb.sc_response = pcache_cachedquery_open_cb;
4528 cb.sc_private = &vals;
4529 op->o_callback = &cb;
4530 op->o_time = slap_get_time();
4531 op->o_do_not_cache = 1;
4532 op->o_managedsait = SLAP_CONTROL_CRITICAL;
4534 op->o_dn = cm->db.be_rootdn;
4535 op->o_ndn = cm->db.be_rootndn;
4536 op->o_req_dn = cm->db.be_suffix[ 0 ];
4537 op->o_req_ndn = cm->db.be_nsuffix[ 0 ];
4539 op->ors_scope = LDAP_SCOPE_BASE;
4540 op->ors_deref = LDAP_DEREF_NEVER;
4542 op->ors_tlimit = SLAP_NO_LIMIT;
4543 op->ors_limit = NULL;
4544 ber_str2bv( "(cachedQueryURL=*)", 0, 0, &op->ors_filterstr );
4545 f.f_choice = LDAP_FILTER_PRESENT;
4546 f.f_desc = ad_cachedQueryURL;
4547 op->ors_filter = &f;
4548 attrs[ 0 ].an_desc = ad_cachedQueryURL;
4549 attrs[ 0 ].an_name = ad_cachedQueryURL->ad_cname;
4550 op->ors_attrs = attrs;
4551 op->ors_attrsonly = 0;
4553 rc = op->o_bd->be_search( op, &rs );
4554 if ( rc == LDAP_SUCCESS && vals != NULL ) {
4557 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
4558 if ( url2query( vals[ i ].bv_val, op, qm ) == 0 ) {
4559 cm->num_cached_queries++;
4563 ber_bvarray_free_x( vals, op->o_tmpmemctx );
4566 /* count cached entries */
4567 f.f_choice = LDAP_FILTER_NOT;
4569 f2.f_choice = LDAP_FILTER_EQUALITY;
4571 f2.f_av_desc = slap_schema.si_ad_objectClass;
4572 BER_BVSTR( &f2.f_av_value, "glue" );
4573 ber_str2bv( "(!(objectClass=glue))", 0, 0, &op->ors_filterstr );
4575 op->ors_slimit = SLAP_NO_LIMIT;
4576 op->ors_scope = LDAP_SCOPE_SUBTREE;
4577 op->ors_attrs = slap_anlist_no_attrs;
4579 op->o_callback->sc_response = pcache_cachedquery_count_cb;
4581 op->o_callback->sc_private = &rs.sr_nentries;
4583 rc = op->o_bd->be_search( op, &rs );
4585 cm->cur_entries = rs.sr_nentries;
4599 slap_overinst *on = (slap_overinst *)be->bd_info;
4600 cache_manager *cm = on->on_bi.bi_private;
4601 query_manager* qm = cm->qm;
4602 int i, ncf = 0, rf = 0, nrf = 0, rc = 0;
4604 /* check attr sets */
4605 for ( i = 0; i < cm->numattrsets; i++) {
4606 if ( !( qm->attr_sets[i].flags & PC_CONFIGURED ) ) {
4607 if ( qm->attr_sets[i].flags & PC_REFERENCED ) {
4608 Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d not configured but referenced.\n", i, 0, 0 );
4612 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, attr set #%d not configured.\n", i, 0, 0 );
4616 } else if ( !( qm->attr_sets[i].flags & PC_REFERENCED ) ) {
4617 Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d configured but not referenced.\n", i, 0, 0 );
4622 if ( ncf || rf || nrf ) {
4623 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets configured but not referenced.\n", nrf, 0, 0 );
4624 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets not configured.\n", ncf, 0, 0 );
4625 Debug( LDAP_DEBUG_CONFIG, "pcache: %d attr sets not configured but referenced.\n", rf, 0, 0 );
4632 /* need to inherit something from the original database... */
4633 cm->db.be_def_limit = be->be_def_limit;
4634 cm->db.be_limits = be->be_limits;
4635 cm->db.be_acl = be->be_acl;
4636 cm->db.be_dfltaccess = be->be_dfltaccess;
4638 if ( SLAP_DBMONITORING( be ) ) {
4639 SLAP_DBFLAGS( &cm->db ) |= SLAP_DBFLAG_MONITORING;
4642 SLAP_DBFLAGS( &cm->db ) &= ~SLAP_DBFLAG_MONITORING;
4645 if ( !cm->defer_db_open ) {
4646 rc = pcache_db_open2( on, cr );
4649 #ifdef PCACHE_MONITOR
4650 if ( rc == LDAP_SUCCESS ) {
4651 rc = pcache_monitor_db_open( be );
4653 #endif /* PCACHE_MONITOR */
4659 pcache_free_qbase( void *v )
4665 tavl_free( qb->scopes[i], NULL );
4675 slap_overinst *on = (slap_overinst *)be->bd_info;
4676 cache_manager *cm = on->on_bi.bi_private;
4677 query_manager *qm = cm->qm;
4681 /* stop the thread ... */
4683 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4684 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, cm->cc_arg ) ) {
4685 ldap_pvt_runqueue_stoptask( &slapd_rq, cm->cc_arg );
4687 ldap_pvt_runqueue_remove( &slapd_rq, cm->cc_arg );
4688 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4691 if ( cm->save_queries ) {
4693 BerVarray vals = NULL;
4696 Connection conn = { 0 };
4697 OperationBuffer opbuf;
4699 slap_callback cb = { 0 };
4701 SlapReply rs = { REP_RESULT };
4702 Modifications mod = {{ 0 }};
4704 thrctx = ldap_pvt_thread_pool_context();
4706 connection_fake_init( &conn, &opbuf, thrctx );
4709 if ( qm->templates != NULL ) {
4710 for ( tm = qm->templates; tm != NULL; tm = tm->qmnext ) {
4711 for ( qc = tm->query; qc; qc = qc->next ) {
4714 if ( query2url( op, qc, &bv, 0 ) == 0 ) {
4715 ber_bvarray_add_x( &vals, &bv, op->o_tmpmemctx );
4722 op->o_dn = cm->db.be_rootdn;
4723 op->o_ndn = cm->db.be_rootndn;
4725 op->o_tag = LDAP_REQ_MODIFY;
4726 op->o_protocol = LDAP_VERSION3;
4727 cb.sc_response = slap_null_cb;
4728 op->o_callback = &cb;
4729 op->o_time = slap_get_time();
4730 op->o_do_not_cache = 1;
4731 op->o_managedsait = SLAP_CONTROL_CRITICAL;
4733 op->o_req_dn = op->o_bd->be_suffix[0];
4734 op->o_req_ndn = op->o_bd->be_nsuffix[0];
4736 mod.sml_op = LDAP_MOD_REPLACE;
4738 mod.sml_desc = ad_cachedQueryURL;
4739 mod.sml_type = ad_cachedQueryURL->ad_cname;
4740 mod.sml_values = vals;
4741 mod.sml_nvalues = NULL;
4742 mod.sml_numvals = 1;
4743 mod.sml_next = NULL;
4744 Debug( pcache_debug,
4745 "%sSETTING CACHED QUERY URLS\n",
4746 vals == NULL ? "RE" : "", 0, 0 );
4748 op->orm_modlist = &mod;
4750 op->o_bd->be_modify( op, &rs );
4752 ber_bvarray_free_x( vals, op->o_tmpmemctx );
4755 /* cleanup stuff inherited from the original database... */
4756 cm->db.be_limits = NULL;
4757 cm->db.be_acl = NULL;
4760 if ( cm->db.bd_info->bi_db_close ) {
4761 rc = cm->db.bd_info->bi_db_close( &cm->db, NULL );
4763 while ( (tm = qm->templates) != NULL ) {
4764 CachedQuery *qc, *qn;
4765 qm->templates = tm->qmnext;
4766 for ( qc = tm->query; qc; qc = qn ) {
4770 avl_free( tm->qbase, pcache_free_qbase );
4771 free( tm->querystr.bv_val );
4772 free( tm->bindfattrs );
4773 free( tm->bindftemp.bv_val );
4774 free( tm->bindfilterstr.bv_val );
4775 free( tm->bindbase.bv_val );
4776 filter_free( tm->bindfilter );
4777 ldap_pvt_thread_rdwr_destroy( &tm->t_rwlock );
4778 free( tm->t_attrs.attrs );
4782 for ( i=0; i<cm->numattrsets; i++ ) {
4783 free( qm->attr_sets[i].attrs );
4785 free( qm->attr_sets );
4786 qm->attr_sets = NULL;
4788 #ifdef PCACHE_MONITOR
4789 if ( rc == LDAP_SUCCESS ) {
4790 rc = pcache_monitor_db_close( be );
4792 #endif /* PCACHE_MONITOR */
4803 slap_overinst *on = (slap_overinst *)be->bd_info;
4804 cache_manager *cm = on->on_bi.bi_private;
4805 query_manager *qm = cm->qm;
4807 if ( cm->db.be_private != NULL ) {
4808 backend_stopdown_one( &cm->db );
4811 ldap_pvt_thread_mutex_destroy( &qm->lru_mutex );
4812 ldap_pvt_thread_mutex_destroy( &cm->cache_mutex );
4816 #ifdef PCACHE_MONITOR
4817 pcache_monitor_db_destroy( be );
4818 #endif /* PCACHE_MONITOR */
4823 #ifdef PCACHE_CONTROL_PRIVDB
4825 Control ::= SEQUENCE {
4826 controlType LDAPOID,
4827 criticality BOOLEAN DEFAULT FALSE,
4828 controlValue OCTET STRING OPTIONAL }
4830 controlType ::= 1.3.6.1.4.1.4203.666.11.9.5.1
4832 * criticality must be TRUE; controlValue must be absent.
4840 if ( op->o_ctrlflag[ privDB_cid ] != SLAP_CONTROL_NONE ) {
4841 rs->sr_text = "privateDB control specified multiple times";
4842 return LDAP_PROTOCOL_ERROR;
4845 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
4846 rs->sr_text = "privateDB control value not absent";
4847 return LDAP_PROTOCOL_ERROR;
4850 if ( !ctrl->ldctl_iscritical ) {
4851 rs->sr_text = "privateDB control criticality required";
4852 return LDAP_PROTOCOL_ERROR;
4855 op->o_ctrlflag[ privDB_cid ] = SLAP_CONTROL_CRITICAL;
4857 return LDAP_SUCCESS;
4860 static char *extops[] = {
4861 LDAP_EXOP_MODIFY_PASSWD,
4864 #endif /* PCACHE_CONTROL_PRIVDB */
4866 #ifdef PCACHE_EXOP_QUERY_DELETE
4867 static struct berval pcache_exop_QUERY_DELETE = BER_BVC( PCACHE_EXOP_QUERY_DELETE );
4869 #define LDAP_TAG_EXOP_QUERY_DELETE_BASE ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 0)
4870 #define LDAP_TAG_EXOP_QUERY_DELETE_DN ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 1)
4871 #define LDAP_TAG_EXOP_QUERY_DELETE_UUID ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 2)
4874 ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
4875 requestName [0] LDAPOID,
4876 requestValue [1] OCTET STRING OPTIONAL }
4878 requestName ::= 1.3.6.1.4.1.4203.666.11.9.6.1
4880 requestValue ::= SEQUENCE { CHOICE {
4882 entryDN [1] LDAPDN },
4883 queryID [2] OCTET STRING (SIZE(16))
4884 -- constrained to UUID }
4886 * Either baseDN or entryDN must be present, to allow database selection.
4888 * 1. if baseDN and queryID are present, then the query corresponding
4889 * to queryID is deleted;
4890 * 2. if baseDN is present and queryID is absent, then all queries
4892 * 3. if entryDN is present and queryID is absent, then all queries
4893 * corresponding to the queryID values present in entryDN are deleted;
4894 * 4. if entryDN and queryID are present, then all queries
4895 * corresponding to the queryID values present in entryDN are deleted,
4896 * but only if the value of queryID is contained in the entry;
4898 * Currently, only 1, 3 and 4 are implemented. 2 can be obtained by either
4899 * recursively deleting the database (ldapdelete -r) with PRIVDB control,
4900 * or by removing the database files.
4902 ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
4903 COMPONENTS OF LDAPResult,
4904 responseName [10] LDAPOID OPTIONAL,
4905 responseValue [11] OCTET STRING OPTIONAL }
4907 * responseName and responseValue must be absent.
4911 * - on success, *tagp is either LDAP_TAG_EXOP_QUERY_DELETE_BASE
4912 * or LDAP_TAG_EXOP_QUERY_DELETE_DN.
4913 * - if ndn != NULL, it is set to the normalized DN in the request
4914 * corresponding to either the baseDN or the entryDN, according
4915 * to *tagp; memory is malloc'ed on the Operation's slab, and must
4916 * be freed by the caller.
4917 * - if uuid != NULL, it is set to point to the normalized UUID;
4918 * memory is malloc'ed on the Operation's slab, and must
4919 * be freed by the caller.
4922 pcache_parse_query_delete(
4926 struct berval *uuid,
4930 int rc = LDAP_SUCCESS;
4933 BerElementBuffer berbuf;
4934 BerElement *ber = (BerElement *)&berbuf;
4935 struct berval reqdata = BER_BVNULL;
4947 if ( in == NULL || in->bv_len == 0 ) {
4948 *text = "empty request data field in queryDelete exop";
4949 return LDAP_PROTOCOL_ERROR;
4952 ber_dupbv_x( &reqdata, in, ctx );
4954 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
4955 ber_init2( ber, &reqdata, 0 );
4957 tag = ber_scanf( ber, "{" /*}*/ );
4959 if ( tag == LBER_ERROR ) {
4960 Debug( LDAP_DEBUG_TRACE,
4961 "pcache_parse_query_delete: decoding error.\n",
4963 goto decoding_error;
4966 tag = ber_peek_tag( ber, &len );
4967 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_BASE
4968 || tag == LDAP_TAG_EXOP_QUERY_DELETE_DN )
4972 if ( ndn != NULL ) {
4975 tag = ber_scanf( ber, "m", &dn );
4976 if ( tag == LBER_ERROR ) {
4977 Debug( LDAP_DEBUG_TRACE,
4978 "pcache_parse_query_delete: DN parse failed.\n",
4980 goto decoding_error;
4983 rc = dnNormalize( 0, NULL, NULL, &dn, ndn, ctx );
4984 if ( rc != LDAP_SUCCESS ) {
4985 *text = "invalid DN in queryDelete exop request data";
4990 tag = ber_scanf( ber, "x" /* "m" */ );
4991 if ( tag == LBER_DEFAULT ) {
4992 goto decoding_error;
4996 tag = ber_peek_tag( ber, &len );
4999 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_UUID ) {
5000 if ( uuid != NULL ) {
5002 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
5004 tag = ber_scanf( ber, "m", &bv );
5005 if ( tag == LBER_ERROR ) {
5006 Debug( LDAP_DEBUG_TRACE,
5007 "pcache_parse_query_delete: UUID parse failed.\n",
5009 goto decoding_error;
5012 if ( bv.bv_len != 16 ) {
5013 Debug( LDAP_DEBUG_TRACE,
5014 "pcache_parse_query_delete: invalid UUID length %lu.\n",
5015 (unsigned long)bv.bv_len, 0, 0 );
5016 goto decoding_error;
5019 rc = lutil_uuidstr_from_normalized(
5020 bv.bv_val, bv.bv_len,
5021 uuidbuf, sizeof( uuidbuf ) );
5023 goto decoding_error;
5025 ber_str2bv( uuidbuf, rc, 1, uuid );
5029 tag = ber_skip_tag( ber, &len );
5030 if ( tag == LBER_DEFAULT ) {
5031 goto decoding_error;
5035 Debug( LDAP_DEBUG_TRACE,
5036 "pcache_parse_query_delete: invalid UUID length %lu.\n",
5037 (unsigned long)len, 0, 0 );
5038 goto decoding_error;
5042 tag = ber_peek_tag( ber, &len );
5045 if ( tag != LBER_DEFAULT || len != 0 ) {
5047 Debug( LDAP_DEBUG_TRACE,
5048 "pcache_parse_query_delete: decoding error\n",
5050 rc = LDAP_PROTOCOL_ERROR;
5051 *text = "queryDelete data decoding error";
5054 if ( ndn && !BER_BVISNULL( ndn ) ) {
5055 slap_sl_free( ndn->bv_val, ctx );
5059 if ( uuid && !BER_BVISNULL( uuid ) ) {
5060 slap_sl_free( uuid->bv_val, ctx );
5065 if ( !BER_BVISNULL( &reqdata ) ) {
5066 ber_memfree_x( reqdata.bv_val, ctx );
5073 pcache_exop_query_delete(
5077 BackendDB *bd = op->o_bd;
5079 struct berval uuid = BER_BVNULL,
5081 char buf[ SLAP_TEXT_BUFLEN ];
5083 ber_tag_t tag = LBER_DEFAULT;
5085 if ( LogTest( LDAP_DEBUG_STATS ) ) {
5089 rs->sr_err = pcache_parse_query_delete( op->ore_reqdata,
5090 &tag, &op->o_req_ndn, uuidp,
5091 &rs->sr_text, op->o_tmpmemctx );
5092 if ( rs->sr_err != LDAP_SUCCESS ) {
5096 if ( LogTest( LDAP_DEBUG_STATS ) ) {
5097 assert( !BER_BVISNULL( &op->o_req_ndn ) );
5098 len = snprintf( buf, sizeof( buf ), " dn=\"%s\"", op->o_req_ndn.bv_val );
5100 if ( !BER_BVISNULL( &uuid ) && len < sizeof( buf ) ) {
5101 snprintf( &buf[ len ], sizeof( buf ) - len, " pcacheQueryId=\"%s\"", uuid.bv_val );
5104 Debug( LDAP_DEBUG_STATS, "%s QUERY DELETE%s\n",
5105 op->o_log_prefix, buf, 0 );
5107 op->o_req_dn = op->o_req_ndn;
5109 op->o_bd = select_backend( &op->o_req_ndn, 0 );
5110 rs->sr_err = backend_check_restrictions( op, rs,
5111 (struct berval *)&pcache_exop_QUERY_DELETE );
5112 if ( rs->sr_err != LDAP_SUCCESS ) {
5116 if ( op->o_bd->be_extended == NULL ) {
5117 send_ldap_error( op, rs, LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
5118 "backend does not support extended operations" );
5122 op->o_bd->be_extended( op, rs );
5125 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
5126 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
5127 BER_BVZERO( &op->o_req_ndn );
5128 BER_BVZERO( &op->o_req_dn );
5131 if ( !BER_BVISNULL( &uuid ) ) {
5132 op->o_tmpfree( uuid.bv_val, op->o_tmpmemctx );
5141 pcache_op_extended( Operation *op, SlapReply *rs )
5143 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
5144 cache_manager *cm = on->on_bi.bi_private;
5146 #ifdef PCACHE_CONTROL_PRIVDB
5147 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL ) {
5148 return pcache_op_privdb( op, rs );
5150 #endif /* PCACHE_CONTROL_PRIVDB */
5152 if ( bvmatch( &op->ore_reqoid, &pcache_exop_QUERY_DELETE ) ) {
5153 struct berval uuid = BER_BVNULL;
5154 ber_tag_t tag = LBER_DEFAULT;
5156 rs->sr_err = pcache_parse_query_delete( op->ore_reqdata,
5157 &tag, NULL, &uuid, &rs->sr_text, op->o_tmpmemctx );
5158 assert( rs->sr_err == LDAP_SUCCESS );
5160 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_DN ) {
5161 /* remove all queries related to the selected entry */
5162 rs->sr_err = pcache_remove_entry_queries_from_cache( op,
5163 cm, &op->o_req_ndn, &uuid );
5165 } else if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_BASE ) {
5166 if ( !BER_BVISNULL( &uuid ) ) {
5167 /* remove the selected query */
5168 rs->sr_err = pcache_remove_query_from_cache( op,
5172 /* TODO: remove all queries */
5173 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5174 rs->sr_text = "deletion of all queries not implemented";
5178 op->o_tmpfree( uuid.bv_val, op->o_tmpmemctx );
5183 #endif /* PCACHE_EXOP_QUERY_DELETE */
5185 #ifdef PCACHE_MONITOR
5188 pcache_monitor_update(
5194 cache_manager *cm = (cache_manager *) priv;
5195 query_manager *qm = cm->qm;
5198 BerVarray vals = NULL;
5200 attr_delete( &e->e_attrs, ad_cachedQueryURL );
5201 if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || ad_inlist( ad_cachedQueryURL, rs->sr_attrs ) )
5202 && qm->templates != NULL )
5206 for ( tm = qm->templates; tm != NULL; tm = tm->qmnext ) {
5207 for ( qc = tm->query; qc; qc = qc->next ) {
5210 if ( query2url( op, qc, &bv, 1 ) == 0 ) {
5211 ber_bvarray_add_x( &vals, &bv, op->o_tmpmemctx );
5217 if ( vals != NULL ) {
5218 attr_merge_normalize( e, ad_cachedQueryURL, vals, NULL );
5219 ber_bvarray_free_x( vals, op->o_tmpmemctx );
5225 char *buf[ SLAP_TEXT_BUFLEN ];
5228 /* number of cached queries */
5229 a = attr_find( e->e_attrs, ad_numQueries );
5230 assert( a != NULL );
5233 bv.bv_len = snprintf( buf, sizeof( buf ), "%lu", cm->num_cached_queries );
5235 if ( a->a_nvals != a->a_vals ) {
5236 ber_bvreplace( &a->a_nvals[ 0 ], &bv );
5238 ber_bvreplace( &a->a_vals[ 0 ], &bv );
5240 /* number of cached entries */
5241 a = attr_find( e->e_attrs, ad_numEntries );
5242 assert( a != NULL );
5245 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", cm->cur_entries );
5247 if ( a->a_nvals != a->a_vals ) {
5248 ber_bvreplace( &a->a_nvals[ 0 ], &bv );
5250 ber_bvreplace( &a->a_vals[ 0 ], &bv );
5253 return SLAP_CB_CONTINUE;
5257 pcache_monitor_free(
5261 struct berval values[ 2 ];
5262 Modification mod = { 0 };
5265 char textbuf[ SLAP_TEXT_BUFLEN ];
5269 /* NOTE: if slap_shutdown != 0, priv might have already been freed */
5272 /* Remove objectClass */
5273 mod.sm_op = LDAP_MOD_DELETE;
5274 mod.sm_desc = slap_schema.si_ad_objectClass;
5275 mod.sm_values = values;
5277 values[ 0 ] = oc_olmPCache->soc_cname;
5278 BER_BVZERO( &values[ 1 ] );
5280 rc = modify_delete_values( e, &mod, 1, &text,
5281 textbuf, sizeof( textbuf ) );
5282 /* don't care too much about return code... */
5285 mod.sm_values = NULL;
5286 mod.sm_desc = ad_cachedQueryURL;
5288 rc = modify_delete_values( e, &mod, 1, &text,
5289 textbuf, sizeof( textbuf ) );
5290 /* don't care too much about return code... */
5293 mod.sm_values = NULL;
5294 mod.sm_desc = ad_numQueries;
5296 rc = modify_delete_values( e, &mod, 1, &text,
5297 textbuf, sizeof( textbuf ) );
5298 /* don't care too much about return code... */
5301 mod.sm_values = NULL;
5302 mod.sm_desc = ad_numEntries;
5304 rc = modify_delete_values( e, &mod, 1, &text,
5305 textbuf, sizeof( textbuf ) );
5306 /* don't care too much about return code... */
5308 return SLAP_CB_CONTINUE;
5312 * call from within pcache_initialize()
5315 pcache_monitor_initialize( void )
5317 static int pcache_monitor_initialized = 0;
5319 if ( backend_info( "monitor" ) == NULL ) {
5323 if ( pcache_monitor_initialized++ ) {
5331 pcache_monitor_db_init( BackendDB *be )
5333 if ( pcache_monitor_initialize() == LDAP_SUCCESS ) {
5334 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING;
5341 pcache_monitor_db_open( BackendDB *be )
5343 slap_overinst *on = (slap_overinst *)be->bd_info;
5344 cache_manager *cm = on->on_bi.bi_private;
5345 Attribute *a, *next;
5346 monitor_callback_t *cb = NULL;
5349 monitor_extra_t *mbe;
5350 struct berval dummy = BER_BVC( "" );
5352 if ( !SLAP_DBMONITORING( be ) ) {
5356 mi = backend_info( "monitor" );
5357 if ( !mi || !mi->bi_extra ) {
5358 SLAP_DBFLAGS( be ) ^= SLAP_DBFLAG_MONITORING;
5363 /* don't bother if monitor is not configured */
5364 if ( !mbe->is_configured() ) {
5365 static int warning = 0;
5367 if ( warning++ == 0 ) {
5368 Debug( LDAP_DEBUG_ANY, "pcache_monitor_db_open: "
5369 "monitoring disabled; "
5370 "configure monitor database to enable\n",
5377 /* alloc as many as required (plus 1 for objectClass) */
5378 a = attrs_alloc( 1 + 2 );
5384 a->a_desc = slap_schema.si_ad_objectClass;
5385 attr_valadd( a, &oc_olmPCache->soc_cname, NULL, 1 );
5389 struct berval bv = BER_BVC( "0" );
5391 next->a_desc = ad_numQueries;
5392 attr_valadd( next, &bv, NULL, 1 );
5393 next = next->a_next;
5395 next->a_desc = ad_numEntries;
5396 attr_valadd( next, &bv, NULL, 1 );
5397 next = next->a_next;
5400 cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
5401 cb->mc_update = pcache_monitor_update;
5402 cb->mc_free = pcache_monitor_free;
5403 cb->mc_private = (void *)cm;
5405 /* make sure the database is registered; then add monitor attributes */
5406 BER_BVZERO( &cm->monitor_ndn );
5407 rc = mbe->register_overlay( be, on, &cm->monitor_ndn );
5409 rc = mbe->register_entry_attrs( &cm->monitor_ndn, a, cb,
5410 &dummy, -1, &dummy);
5426 /* store for cleanup */
5427 cm->monitor_cb = (void *)cb;
5429 /* we don't need to keep track of the attributes, because
5430 * bdb_monitor_free() takes care of everything */
5439 pcache_monitor_db_close( BackendDB *be )
5441 slap_overinst *on = (slap_overinst *)be->bd_info;
5442 cache_manager *cm = on->on_bi.bi_private;
5444 if ( cm->monitor_cb != NULL ) {
5445 BackendInfo *mi = backend_info( "monitor" );
5446 monitor_extra_t *mbe;
5448 if ( mi && &mi->bi_extra ) {
5450 mbe->unregister_entry_callback( NULL,
5451 (monitor_callback_t *)cm->monitor_cb,
5460 pcache_monitor_db_destroy( BackendDB *be )
5465 #endif /* PCACHE_MONITOR */
5467 static slap_overinst pcache;
5469 static char *obsolete_names[] = {
5474 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
5476 #endif /* SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC */
5481 struct berval debugbv = BER_BVC("pcache");
5485 code = slap_loglevel_get( &debugbv, &pcache_debug );
5490 #ifdef PCACHE_CONTROL_PRIVDB
5491 code = register_supported_control( PCACHE_CONTROL_PRIVDB,
5492 SLAP_CTRL_BIND|SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE, extops,
5493 parse_privdb_ctrl, &privDB_cid );
5494 if ( code != LDAP_SUCCESS ) {
5495 Debug( LDAP_DEBUG_ANY,
5496 "pcache_initialize: failed to register control %s (%d)\n",
5497 PCACHE_CONTROL_PRIVDB, code, 0 );
5500 #endif /* PCACHE_CONTROL_PRIVDB */
5502 #ifdef PCACHE_EXOP_QUERY_DELETE
5503 code = load_extop2( (struct berval *)&pcache_exop_QUERY_DELETE,
5504 SLAP_EXOP_WRITES|SLAP_EXOP_HIDE, pcache_exop_query_delete,
5506 if ( code != LDAP_SUCCESS ) {
5507 Debug( LDAP_DEBUG_ANY,
5508 "pcache_initialize: unable to register queryDelete exop: %d.\n",
5512 #endif /* PCACHE_EXOP_QUERY_DELETE */
5514 argv[ 0 ] = "back-bdb/back-hdb monitor";
5519 for ( i = 0; s_oid[ i ].name; i++ ) {
5521 argv[ 1 ] = s_oid[ i ].name;
5522 argv[ 2 ] = s_oid[ i ].oid;
5524 if ( parse_oidm( &c, 0, NULL ) != 0 ) {
5525 Debug( LDAP_DEBUG_ANY, "pcache_initialize: "
5526 "unable to add objectIdentifier \"%s=%s\"\n",
5527 s_oid[ i ].name, s_oid[ i ].oid, 0 );
5532 for ( i = 0; s_ad[i].desc != NULL; i++ ) {
5533 code = register_at( s_ad[i].desc, s_ad[i].adp, 0 );
5535 Debug( LDAP_DEBUG_ANY,
5536 "pcache_initialize: register_at #%d failed\n", i, 0, 0 );
5539 (*s_ad[i].adp)->ad_type->sat_flags |= SLAP_AT_HIDE;
5542 for ( i = 0; s_oc[i].desc != NULL; i++ ) {
5543 code = register_oc( s_oc[i].desc, s_oc[i].ocp, 0 );
5545 Debug( LDAP_DEBUG_ANY,
5546 "pcache_initialize: register_oc #%d failed\n", i, 0, 0 );
5549 (*s_oc[i].ocp)->soc_flags |= SLAP_OC_HIDE;
5552 pcache.on_bi.bi_type = "pcache";
5553 pcache.on_bi.bi_obsolete_names = obsolete_names;
5554 pcache.on_bi.bi_db_init = pcache_db_init;
5555 pcache.on_bi.bi_db_config = pcache_db_config;
5556 pcache.on_bi.bi_db_open = pcache_db_open;
5557 pcache.on_bi.bi_db_close = pcache_db_close;
5558 pcache.on_bi.bi_db_destroy = pcache_db_destroy;
5560 pcache.on_bi.bi_op_search = pcache_op_search;
5561 pcache.on_bi.bi_op_bind = pcache_op_bind;
5562 #ifdef PCACHE_CONTROL_PRIVDB
5563 pcache.on_bi.bi_op_compare = pcache_op_privdb;
5564 pcache.on_bi.bi_op_modrdn = pcache_op_privdb;
5565 pcache.on_bi.bi_op_modify = pcache_op_privdb;
5566 pcache.on_bi.bi_op_add = pcache_op_privdb;
5567 pcache.on_bi.bi_op_delete = pcache_op_privdb;
5568 #endif /* PCACHE_CONTROL_PRIVDB */
5569 #ifdef PCACHE_EXOP_QUERY_DELETE
5570 pcache.on_bi.bi_extended = pcache_op_extended;
5571 #elif defined( PCACHE_CONTROL_PRIVDB )
5572 pcache.on_bi.bi_extended = pcache_op_privdb;
5575 pcache.on_bi.bi_chk_controls = pcache_chk_controls;
5577 pcache.on_bi.bi_cf_ocs = pcocs;
5579 code = config_register_schema( pccfg, pcocs );
5580 if ( code ) return code;
5582 return overlay_register( &pcache );
5585 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
5586 int init_module(int argc, char *argv[]) {
5587 return pcache_initialize();
5591 #endif /* defined(SLAPD_OVER_PROXYCACHE) */