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 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>
40 * Control that allows to access the private DB
41 * instead of the public one
43 #define PCACHE_CONTROL_PRIVDB "1.3.6.1.4.1.4203.666.11.9.5.1"
46 * Extended Operation that allows to remove a query from the cache
48 #define PCACHE_EXOP_QUERY_DELETE "1.3.6.1.4.1.4203.666.11.9.6.1"
51 /* query cache structs */
54 typedef struct Query_s {
55 Filter* filter; /* Search Filter */
56 struct berval base; /* Search Base */
57 int scope; /* Search scope */
60 struct query_template_s;
62 typedef struct Qbase_s {
63 Avlnode *scopes[4]; /* threaded AVL trees of cached queries */
68 /* struct representing a cached query */
69 typedef struct cached_query_s {
74 struct berval q_uuid; /* query identifier */
76 struct query_template_s *qtemp; /* template of the query */
77 time_t expiry_time; /* time till the query is considered valid */
78 struct cached_query_s *next; /* next query in the template */
79 struct cached_query_s *prev; /* previous query in the template */
80 struct cached_query_s *lru_up; /* previous query in the LRU list */
81 struct cached_query_s *lru_down; /* next query in the LRU list */
82 ldap_pvt_thread_rdwr_t rwlock;
88 * ldap:///<base>??<scope>?<filter>?x-uuid=<uid>,x-template=<template>,x-attrset=<attrset>,x-expiry=<expiry>
90 * <base> ::= CachedQuery.qbase->base
91 * <scope> ::= CachedQuery.scope
92 * <filter> ::= filter2bv(CachedQuery.filter)
93 * <uuid> ::= CachedQuery.q_uuid
94 * <attrset> ::= CachedQuery.qtemp->attr_set_index
95 * <expiry> ::= CachedQuery.expiry_time
97 * quick hack: parse URI, call add_query() and then fix
98 * CachedQuery.expiry_time and CachedQuery.q_uuid
102 * Represents a set of projected attributes.
106 struct query_template_s *templates;
107 AttributeName* attrs; /* specifies the set */
109 #define PC_CONFIGURED (0x1)
110 #define PC_REFERENCED (0x2)
111 #define PC_GOT_OC (0x4)
112 int count; /* number of attributes */
115 /* struct representing a query template
116 * e.g. template string = &(cn=)(mail=)
118 typedef struct query_template_s {
119 struct query_template_s *qtnext;
120 struct query_template_s *qmnext;
123 CachedQuery* query; /* most recent query cached for the template */
124 CachedQuery* query_last; /* oldest query cached for the template */
125 ldap_pvt_thread_rdwr_t t_rwlock; /* Rd/wr lock for accessing queries in the template */
126 struct berval querystr; /* Filter string corresponding to the QT */
128 int attr_set_index; /* determines the projected attributes */
129 int no_of_queries; /* Total number of queries in the template */
130 time_t ttl; /* TTL for the queries of this template */
131 time_t negttl; /* TTL for negative results */
132 time_t limitttl; /* TTL for sizelimit exceeding results */
133 struct attr_set t_attrs; /* filter attrs + attr_set */
141 } pc_caching_reason_t;
143 static const char *pc_caching_reason_str[] = {
152 struct query_manager_s;
154 /* prototypes for functions for 1) query containment
155 * 2) query addition, 3) cache replacement
157 typedef CachedQuery *(QCfunc)(Operation *op, struct query_manager_s*,
158 Query*, QueryTemplate*);
159 typedef CachedQuery *(AddQueryfunc)(Operation *op, struct query_manager_s*,
160 Query*, QueryTemplate*, pc_caching_reason_t, int wlock);
161 typedef void (CRfunc)(struct query_manager_s*, struct berval*);
163 /* LDAP query cache */
164 typedef struct query_manager_s {
165 struct attr_set* attr_sets; /* possible sets of projected attributes */
166 QueryTemplate* templates; /* cacheable templates */
168 CachedQuery* lru_top; /* top and bottom of LRU list */
169 CachedQuery* lru_bottom;
171 ldap_pvt_thread_mutex_t lru_mutex; /* mutex for accessing LRU list */
173 /* Query cache methods */
174 QCfunc *qcfunc; /* Query containment*/
175 CRfunc *crfunc; /* cache replacement */
176 AddQueryfunc *addfunc; /* add query */
179 /* LDAP query cache manager */
180 typedef struct cache_manager_s {
181 BackendDB db; /* underlying database */
182 unsigned long num_cached_queries; /* total number of cached queries */
183 unsigned long max_queries; /* upper bound on # of cached queries */
184 int save_queries; /* save cached queries across restarts */
185 int check_cacheability; /* check whether a query is cacheable */
186 int numattrsets; /* number of attribute sets */
187 int cur_entries; /* current number of entries cached */
188 int max_entries; /* max number of entries cached */
189 int num_entries_limit; /* max # of entries in a cacheable query */
191 char response_cb; /* install the response callback
192 * at the tail of the callback list */
193 #define PCACHE_RESPONSE_CB_HEAD 0
194 #define PCACHE_RESPONSE_CB_TAIL 1
195 char defer_db_open; /* defer open for online add */
197 time_t cc_period; /* interval between successive consistency checks (sec) */
201 ldap_pvt_thread_mutex_t cache_mutex;
203 query_manager* qm; /* query cache managed by the cache manager */
206 static int pcache_debug;
208 #ifdef PCACHE_CONTROL_PRIVDB
209 static int privDB_cid;
210 #endif /* PCACHE_CONTROL_PRIVDB */
212 static AttributeDescription *ad_queryId, *ad_cachedQueryURL;
215 AttributeDescription **adp;
217 { "( 1.3.6.1.4.1.4203.666.11.9.1.1 "
219 "DESC 'ID of query the entry belongs to, formatted as a UUID' "
220 "EQUALITY octetStringMatch "
221 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40{64} "
222 "NO-USER-MODIFICATION "
223 "USAGE directoryOperation )",
225 { "( 1.3.6.1.4.1.4203.666.11.9.1.2 "
226 "NAME 'cachedQueryURL' "
227 "DESC 'URI describing a cached query' "
228 "EQUALITY caseExactMatch "
229 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 "
230 "NO-USER-MODIFICATION "
231 "USAGE directoryOperation )",
232 &ad_cachedQueryURL },
241 AttributeName** filter_attrs,
243 int* filter_got_oc );
250 QueryTemplate *templ,
251 pc_caching_reason_t why,
258 struct berval *query_uuid );
261 * Turn a cached query into its URL representation
264 query2url( Operation *op, CachedQuery *q, struct berval *urlbv )
266 struct berval bv_scope,
268 char attrset_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
269 expiry_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
271 ber_len_t attrset_len,
274 ldap_pvt_scope2bv( q->scope, &bv_scope );
275 filter2bv_x( op, q->filter, &bv_filter );
276 attrset_len = sprintf( attrset_buf,
277 "%lu", (unsigned long)q->qtemp->attr_set_index );
278 expiry_len = sprintf( expiry_buf,
279 "%lu", (unsigned long)q->expiry_time );
281 urlbv->bv_len = STRLENOF( "ldap:///" )
282 + q->qbase->base.bv_len
287 + STRLENOF( "?x-uuid=" )
289 + STRLENOF( ",x-attrset=" )
291 + STRLENOF( ",x-expiry=" )
293 ptr = urlbv->bv_val = ber_memalloc_x( urlbv->bv_len + 1, op->o_tmpmemctx );
294 ptr = lutil_strcopy( ptr, "ldap:///" );
295 ptr = lutil_strcopy( ptr, q->qbase->base.bv_val );
296 ptr = lutil_strcopy( ptr, "??" );
297 ptr = lutil_strcopy( ptr, bv_scope.bv_val );
298 ptr = lutil_strcopy( ptr, "?" );
299 ptr = lutil_strcopy( ptr, bv_filter.bv_val );
300 ptr = lutil_strcopy( ptr, "?x-uuid=" );
301 ptr = lutil_strcopy( ptr, q->q_uuid.bv_val );
302 ptr = lutil_strcopy( ptr, ",x-attrset=" );
303 ptr = lutil_strcopy( ptr, attrset_buf );
304 ptr = lutil_strcopy( ptr, ",x-expiry=" );
305 ptr = lutil_strcopy( ptr, expiry_buf );
307 ber_memfree_x( bv_filter.bv_val, op->o_tmpmemctx );
313 * Turn an URL representing a formerly cached query into a cached query,
314 * and try to cache it
325 LDAPURLDesc *lud = NULL;
327 tempstr = BER_BVNULL,
337 rc = ldap_url_parse( url, &lud );
338 if ( rc != LDAP_URL_SUCCESS ) {
342 /* non-allowed fields */
343 if ( lud->lud_host != NULL ) {
348 if ( lud->lud_attrs != NULL ) {
354 if ( strcmp( lud->lud_scheme, "ldap" ) != 0 ) {
359 /* required fields */
360 if ( lud->lud_dn == NULL || lud->lud_dn[ 0 ] == '\0' ) {
365 switch ( lud->lud_scope ) {
366 case LDAP_SCOPE_BASE:
367 case LDAP_SCOPE_ONELEVEL:
368 case LDAP_SCOPE_SUBTREE:
369 case LDAP_SCOPE_SUBORDINATE:
377 if ( lud->lud_filter == NULL || lud->lud_filter[ 0 ] == '\0' ) {
382 if ( lud->lud_exts == NULL ) {
387 for ( i = 0; lud->lud_exts[ i ] != NULL; i++ ) {
388 if ( strncmp( lud->lud_exts[ i ], "x-uuid=", STRLENOF( "x-uuid=" ) ) == 0 ) {
389 struct berval tmpUUID;
390 Syntax *syn_UUID = slap_schema.si_ad_entryUUID->ad_type->sat_syntax;
392 ber_str2bv( &lud->lud_exts[ i ][ STRLENOF( "x-uuid=" ) ], 0, 0, &tmpUUID );
393 rc = syn_UUID->ssyn_pretty( syn_UUID, &tmpUUID, &uuid, NULL );
394 if ( rc != LDAP_SUCCESS ) {
399 } else if ( strncmp( lud->lud_exts[ i ], "x-attrset=", STRLENOF( "x-attrset=" ) ) == 0 ) {
400 rc = lutil_atoi( &attrset, &lud->lud_exts[ i ][ STRLENOF( "x-attrset=" ) ] );
406 } else if ( strncmp( lud->lud_exts[ i ], "x-expiry=", STRLENOF( "x-expiry=" ) ) == 0 ) {
409 rc = lutil_atoul( &l, &lud->lud_exts[ i ][ STRLENOF( "x-expiry=" ) ] );
413 expiry_time = (time_t)l;
427 if ( !got_attrset ) {
437 /* ignore expired queries */
438 if ( expiry_time <= slap_get_time()) {
440 SlapReply rs2 = { 0 };
442 memset( &op2.oq_search, 0, sizeof( op2.oq_search ) );
444 (void)remove_query_data( &op2, &rs2, &uuid );
449 ber_str2bv( lud->lud_dn, 0, 0, &base );
450 rc = dnNormalize( 0, NULL, NULL, &base, &query.base, NULL );
451 if ( rc != LDAP_SUCCESS ) {
454 query.scope = lud->lud_scope;
455 query.filter = str2filter( lud->lud_filter );
457 tempstr.bv_val = ch_malloc( strlen( lud->lud_filter ) + 1 );
459 if ( filter2template( op, query.filter, &tempstr, NULL, NULL, NULL ) ) {
460 ch_free( tempstr.bv_val );
465 /* check for query containment */
466 qt = qm->attr_sets[attrset].templates;
467 for ( ; qt; qt = qt->qtnext ) {
468 /* find if template i can potentially answer tempstr */
469 if ( bvmatch( &qt->querystr, &tempstr ) ) {
479 cq = add_query( op, qm, &query, qt, PC_POSITIVE, 0 );
481 cq->expiry_time = expiry_time;
484 /* it's now into cq->filter */
494 if ( query.filter != NULL ) filter_free( query.filter );
495 if ( !BER_BVISNULL( &tempstr ) ) ch_free( tempstr.bv_val );
496 if ( !BER_BVISNULL( &query.base ) ) ch_free( query.base.bv_val );
497 if ( !BER_BVISNULL( &uuid ) ) ch_free( uuid.bv_val );
498 if ( lud != NULL ) ldap_free_urldesc( lud );
503 /* Return 1 for an added entry, else 0 */
508 struct berval* query_uuid )
511 Modifications* modlist = NULL;
512 const char* text = NULL;
514 char textbuf[SLAP_TEXT_BUFLEN];
515 size_t textlen = sizeof(textbuf);
517 SlapReply sreply = {REP_RESULT};
519 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
524 /* add queryId attribute */
525 attr_merge_one( e, ad_queryId, query_uuid, NULL );
527 /* append the attribute list from the fetched entry */
528 e->e_attrs->a_next = attr;
530 op->o_tag = LDAP_REQ_ADD;
531 op->o_protocol = LDAP_VERSION3;
532 op->o_callback = &cb;
533 op->o_time = slap_get_time();
534 op->o_do_not_cache = 1;
537 op->o_req_dn = e->e_name;
538 op->o_req_ndn = e->e_nname;
539 rc = op->o_bd->be_add( op, &sreply );
541 if ( rc != LDAP_SUCCESS ) {
542 if ( rc == LDAP_ALREADY_EXISTS ) {
543 slap_entry2mods( e, &modlist, &text, textbuf, textlen );
544 modlist->sml_op = LDAP_MOD_ADD;
545 op->o_tag = LDAP_REQ_MODIFY;
546 op->orm_modlist = modlist;
547 op->o_bd->be_modify( op, &sreply );
548 slap_mods_free( modlist, 1 );
549 } else if ( rc == LDAP_REFERRAL ||
550 rc == LDAP_NO_SUCH_OBJECT ) {
551 syncrepl_add_glue( op, e );
560 if ( op->ora_e == e )
561 be_entry_release_w( op, e );
568 /* Length-ordered sort on normalized DNs */
569 static int pcache_dn_cmp( const void *v1, const void *v2 )
571 const Qbase *q1 = v1, *q2 = v2;
573 int rc = q1->base.bv_len - q2->base.bv_len;
575 rc = strncmp( q1->base.bv_val, q2->base.bv_val, q1->base.bv_len );
579 static int lex_bvcmp( struct berval *bv1, struct berval *bv2 )
582 dif = bv1->bv_len - bv2->bv_len;
584 if ( dif > 0 ) len -= dif;
585 len = memcmp( bv1->bv_val, bv2->bv_val, len );
591 /* compare the first value in each filter */
592 static int pcache_filter_cmp( const void *v1, const void *v2 )
594 const CachedQuery *q1 = v1, *q2 =v2;
595 int rc, weight1, weight2;
597 switch( q1->first->f_choice ) {
598 case LDAP_FILTER_PRESENT:
601 case LDAP_FILTER_EQUALITY:
609 switch( q2->first->f_choice ) {
610 case LDAP_FILTER_PRESENT:
613 case LDAP_FILTER_EQUALITY:
621 rc = weight1 - weight2;
626 rc = lex_bvcmp( &q1->first->f_av_value, &q2->first->f_av_value );
629 if ( q1->first->f_choice == LDAP_FILTER_SUBSTRINGS ) {
631 if ( !BER_BVISNULL( &q1->first->f_sub_initial )) {
632 if ( !BER_BVISNULL( &q2->first->f_sub_initial )) {
633 rc = lex_bvcmp( &q1->first->f_sub_initial,
634 &q2->first->f_sub_initial );
638 } else if ( !BER_BVISNULL( &q2->first->f_sub_initial )) {
642 if ( q1->first->f_sub_any ) {
643 if ( q2->first->f_sub_any ) {
644 rc = lex_bvcmp( q1->first->f_sub_any,
645 q2->first->f_sub_any );
649 } else if ( q2->first->f_sub_any ) {
653 if ( !BER_BVISNULL( &q1->first->f_sub_final )) {
654 if ( !BER_BVISNULL( &q2->first->f_sub_final )) {
655 rc = lex_bvcmp( &q1->first->f_sub_final,
656 &q2->first->f_sub_final );
660 } else if ( !BER_BVISNULL( &q2->first->f_sub_final )) {
664 rc = lex_bvcmp( &q1->first->f_mr_value,
665 &q2->first->f_mr_value );
674 /* add query on top of LRU list */
676 add_query_on_top (query_manager* qm, CachedQuery* qc)
678 CachedQuery* top = qm->lru_top;
689 Debug( pcache_debug, "Base of added query = %s\n",
690 qc->qbase->base.bv_val, 0, 0 );
693 /* remove_query from LRU list */
696 remove_query (query_manager* qm, CachedQuery* qc)
719 qc->lru_up = qc->lru_down = NULL;
722 /* find and remove string2 from string1
723 * from start if position = 1,
724 * from end if position = 3,
725 * from anywhere if position = 2
726 * string1 is overwritten if position = 2.
730 find_and_remove(struct berval* ber1, struct berval* ber2, int position)
741 if ( ber1->bv_len >= ber2->bv_len && !memcmp( ber1->bv_val,
742 ber2->bv_val, ber2->bv_len )) {
744 ber1->bv_val += ber2->bv_len;
745 ber1->bv_len -= ber2->bv_len;
750 ber1->bv_val[ber1->bv_len] = '\0';
751 temp = strstr( ber1->bv_val, ber2->bv_val );
753 strcpy( temp, temp+ber2->bv_len );
754 ber1->bv_len -= ber2->bv_len;
760 if ( ber1->bv_len >= ber2->bv_len &&
761 !memcmp( ber1->bv_val+ber1->bv_len-ber2->bv_len, ber2->bv_val,
764 ber1->bv_len -= ber2->bv_len;
772 static struct berval*
773 merge_init_final(Operation *op, struct berval* init, struct berval* any,
774 struct berval* final)
776 struct berval* merged, *temp;
777 int i, any_count, count;
779 for (any_count=0; any && any[any_count].bv_val; any_count++)
789 merged = (struct berval*)op->o_tmpalloc( (count+1)*sizeof(struct berval),
794 ber_dupbv_x( temp, init, op->o_tmpmemctx );
798 for (i=0; i<any_count; i++) {
799 ber_dupbv_x( temp, any, op->o_tmpmemctx );
804 ber_dupbv_x( temp, final, op->o_tmpmemctx );
811 /* Each element in stored must be found in incoming. Incoming is overwritten.
814 strings_containment(struct berval* stored, struct berval* incoming)
816 struct berval* element;
820 for ( element=stored; element->bv_val != NULL; element++ ) {
821 for (j = k; incoming[j].bv_val != NULL; j++) {
822 if (find_and_remove(&(incoming[j]), element, 2)) {
839 substr_containment_substr(Operation *op, Filter* stored, Filter* incoming)
843 struct berval init_incoming;
844 struct berval final_incoming;
845 struct berval *remaining_incoming = NULL;
847 if ((!(incoming->f_sub_initial.bv_val) && (stored->f_sub_initial.bv_val))
848 || (!(incoming->f_sub_final.bv_val) && (stored->f_sub_final.bv_val)))
851 init_incoming = incoming->f_sub_initial;
852 final_incoming = incoming->f_sub_final;
854 if (find_and_remove(&init_incoming,
855 &(stored->f_sub_initial), 1) && find_and_remove(&final_incoming,
856 &(stored->f_sub_final), 3))
858 if (stored->f_sub_any == NULL) {
862 remaining_incoming = merge_init_final(op, &init_incoming,
863 incoming->f_sub_any, &final_incoming);
864 rc = strings_containment(stored->f_sub_any, remaining_incoming);
865 ber_bvarray_free_x( remaining_incoming, op->o_tmpmemctx );
872 substr_containment_equality(Operation *op, Filter* stored, Filter* incoming)
874 struct berval incoming_val[2];
877 incoming_val[1] = incoming->f_av_value;
879 if (find_and_remove(incoming_val+1,
880 &(stored->f_sub_initial), 1) && find_and_remove(incoming_val+1,
881 &(stored->f_sub_final), 3)) {
882 if (stored->f_sub_any == NULL){
886 ber_dupbv_x( incoming_val, incoming_val+1, op->o_tmpmemctx );
887 BER_BVZERO( incoming_val+1 );
888 rc = strings_containment(stored->f_sub_any, incoming_val);
889 op->o_tmpfree( incoming_val[0].bv_val, op->o_tmpmemctx );
896 filter_first( Filter *f )
898 while ( f->f_choice == LDAP_FILTER_OR || f->f_choice == LDAP_FILTER_AND )
905 find_filter( Operation *op, Avlnode *root, Filter *inputf, Filter *first )
909 MatchingRule* mrule = NULL;
910 int res=0, eqpass= 0;
918 /* substring matches sort to the end, and we just have to
919 * walk the entire list.
921 if ( first->f_choice == LDAP_FILTER_SUBSTRINGS ) {
922 ptr = tavl_end( root, 1 );
925 ptr = tavl_find3( root, &cq, pcache_filter_cmp, &ret );
926 dir = (first->f_choice == LDAP_FILTER_GE) ? TAVL_DIR_LEFT :
935 /* an incoming substr query can only be satisfied by a cached
938 if ( first->f_choice == LDAP_FILTER_SUBSTRINGS &&
939 qc->first->f_choice != LDAP_FILTER_SUBSTRINGS )
942 /* an incoming eq query can be satisfied by a cached eq or substr
945 if ( first->f_choice == LDAP_FILTER_EQUALITY ) {
947 if ( qc->first->f_choice != LDAP_FILTER_EQUALITY ) {
948 nextpass: eqpass = 1;
949 ptr = tavl_end( root, 1 );
954 if ( qc->first->f_choice != LDAP_FILTER_SUBSTRINGS )
960 switch (fs->f_choice) {
961 case LDAP_FILTER_EQUALITY:
962 if (fi->f_choice == LDAP_FILTER_EQUALITY)
963 mrule = fs->f_ava->aa_desc->ad_type->sat_equality;
969 mrule = fs->f_ava->aa_desc->ad_type->sat_ordering;
976 rc = value_match(&ret, fs->f_ava->aa_desc, mrule,
977 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
978 &(fi->f_ava->aa_value),
979 &(fs->f_ava->aa_value), &text);
980 if (rc != LDAP_SUCCESS) {
983 if ( fi==first && fi->f_choice==LDAP_FILTER_EQUALITY && ret )
986 switch (fs->f_choice) {
988 case LDAP_FILTER_AND:
993 case LDAP_FILTER_SUBSTRINGS:
994 /* check if the equality query can be
995 * answered with cached substring query */
996 if ((fi->f_choice == LDAP_FILTER_EQUALITY)
997 && substr_containment_equality( op,
1000 /* check if the substring query can be
1001 * answered with cached substring query */
1002 if ((fi->f_choice ==LDAP_FILTER_SUBSTRINGS
1003 ) && substr_containment_substr( op,
1009 case LDAP_FILTER_PRESENT:
1014 case LDAP_FILTER_EQUALITY:
1020 case LDAP_FILTER_GE:
1021 if (mrule && ret >= 0)
1026 case LDAP_FILTER_LE:
1027 if (mrule && ret <= 0)
1032 case LDAP_FILTER_NOT:
1038 } while((res) && (fi != NULL) && (fs != NULL));
1042 ptr = tavl_next( ptr, dir );
1047 /* check whether query is contained in any of
1048 * the cached queries in template
1050 static CachedQuery *
1051 query_containment(Operation *op, query_manager *qm,
1053 QueryTemplate *templa)
1056 int depth = 0, tscope;
1057 Qbase qbase, *qbptr = NULL;
1060 if (query->filter != NULL) {
1063 Debug( pcache_debug, "Lock QC index = %p\n",
1064 (void *) templa, 0, 0 );
1065 qbase.base = query->base;
1067 first = filter_first( query->filter );
1069 ldap_pvt_thread_rdwr_rlock(&templa->t_rwlock);
1072 qbptr = avl_find( templa->qbase, &qbase, pcache_dn_cmp );
1074 tscope = query->scope;
1075 /* Find a matching scope:
1076 * match at depth 0 OK
1079 * subord at depth > 0 OK
1080 * subtree at any depth OK
1082 * subtree or subord at any depth OK
1084 * subtree or subord at any depth OK
1086 * subord at depth > 0 OK
1087 * subtree at any depth OK
1089 for ( tscope = 0 ; tscope <= LDAP_SCOPE_CHILDREN; tscope++ ) {
1090 switch ( query->scope ) {
1091 case LDAP_SCOPE_BASE:
1092 if ( tscope == LDAP_SCOPE_BASE && depth ) continue;
1093 if ( tscope == LDAP_SCOPE_ONE && depth != 1) continue;
1094 if ( tscope == LDAP_SCOPE_CHILDREN && !depth ) continue;
1096 case LDAP_SCOPE_ONE:
1097 if ( tscope == LDAP_SCOPE_BASE )
1098 tscope = LDAP_SCOPE_ONE;
1099 if ( tscope == LDAP_SCOPE_ONE && depth ) continue;
1100 if ( !depth ) break;
1101 if ( tscope < LDAP_SCOPE_SUBTREE )
1102 tscope = LDAP_SCOPE_SUBTREE;
1104 case LDAP_SCOPE_SUBTREE:
1105 if ( tscope < LDAP_SCOPE_SUBTREE )
1106 tscope = LDAP_SCOPE_SUBTREE;
1107 if ( tscope == LDAP_SCOPE_CHILDREN && !depth ) continue;
1109 case LDAP_SCOPE_CHILDREN:
1110 if ( tscope < LDAP_SCOPE_SUBTREE )
1111 tscope = LDAP_SCOPE_SUBTREE;
1114 if ( !qbptr->scopes[tscope] ) continue;
1117 qc = find_filter( op, qbptr->scopes[tscope],
1118 query->filter, first );
1120 if ( qc->q_sizelimit ) {
1121 ldap_pvt_thread_rdwr_runlock(&templa->t_rwlock);
1124 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1125 if (qm->lru_top != qc) {
1126 remove_query(qm, qc);
1127 add_query_on_top(qm, qc);
1129 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1134 if ( be_issuffix( op->o_bd, &qbase.base ))
1137 dnParent( &qbase.base, &pdn );
1142 Debug( pcache_debug,
1143 "Not answerable: Unlock QC index=%p\n",
1144 (void *) templa, 0, 0 );
1145 ldap_pvt_thread_rdwr_runlock(&templa->t_rwlock);
1151 free_query (CachedQuery* qc)
1153 free(qc->q_uuid.bv_val);
1154 filter_free(qc->filter);
1159 /* Add query to query cache, the returned Query is locked for writing */
1160 static CachedQuery *
1165 QueryTemplate *templ,
1166 pc_caching_reason_t why,
1169 CachedQuery* new_cached_query = (CachedQuery*) ch_malloc(sizeof(CachedQuery));
1175 new_cached_query->qtemp = templ;
1176 BER_BVZERO( &new_cached_query->q_uuid );
1177 new_cached_query->q_sizelimit = 0;
1185 ttl = templ->negttl;
1189 ttl = templ->limitttl;
1196 new_cached_query->expiry_time = slap_get_time() + ttl;
1197 new_cached_query->lru_up = NULL;
1198 new_cached_query->lru_down = NULL;
1199 Debug( pcache_debug, "Added query expires at %ld (%s)\n",
1200 (long) new_cached_query->expiry_time,
1201 pc_caching_reason_str[ why ], 0 );
1203 new_cached_query->scope = query->scope;
1204 new_cached_query->filter = query->filter;
1205 new_cached_query->first = first = filter_first( query->filter );
1207 ldap_pvt_thread_rdwr_init(&new_cached_query->rwlock);
1209 ldap_pvt_thread_rdwr_wlock(&new_cached_query->rwlock);
1211 qb.base = query->base;
1213 /* Adding a query */
1214 Debug( pcache_debug, "Lock AQ index = %p\n",
1215 (void *) templ, 0, 0 );
1216 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
1217 qbase = avl_find( templ->qbase, &qb, pcache_dn_cmp );
1219 qbase = ch_calloc( 1, sizeof(Qbase) + qb.base.bv_len + 1 );
1220 qbase->base.bv_len = qb.base.bv_len;
1221 qbase->base.bv_val = (char *)(qbase+1);
1222 memcpy( qbase->base.bv_val, qb.base.bv_val, qb.base.bv_len );
1223 qbase->base.bv_val[qbase->base.bv_len] = '\0';
1224 avl_insert( &templ->qbase, qbase, pcache_dn_cmp, avl_dup_error );
1226 new_cached_query->next = templ->query;
1227 new_cached_query->prev = NULL;
1228 new_cached_query->qbase = qbase;
1229 rc = tavl_insert( &qbase->scopes[query->scope], new_cached_query,
1230 pcache_filter_cmp, avl_dup_error );
1233 if (templ->query == NULL)
1234 templ->query_last = new_cached_query;
1236 templ->query->prev = new_cached_query;
1237 templ->query = new_cached_query;
1238 templ->no_of_queries++;
1240 ch_free( new_cached_query );
1241 new_cached_query = find_filter( op, qbase->scopes[query->scope],
1242 query->filter, first );
1243 filter_free( query->filter );
1245 Debug( pcache_debug, "TEMPLATE %p QUERIES++ %d\n",
1246 (void *) templ, templ->no_of_queries, 0 );
1248 Debug( pcache_debug, "Unlock AQ index = %p \n",
1249 (void *) templ, 0, 0 );
1250 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
1252 /* Adding on top of LRU list */
1254 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1255 add_query_on_top(qm, new_cached_query);
1256 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1258 return rc == 0 ? new_cached_query : NULL;
1262 remove_from_template (CachedQuery* qc, QueryTemplate* template)
1264 if (!qc->prev && !qc->next) {
1265 template->query_last = template->query = NULL;
1266 } else if (qc->prev == NULL) {
1267 qc->next->prev = NULL;
1268 template->query = qc->next;
1269 } else if (qc->next == NULL) {
1270 qc->prev->next = NULL;
1271 template->query_last = qc->prev;
1273 qc->next->prev = qc->prev;
1274 qc->prev->next = qc->next;
1276 tavl_delete( &qc->qbase->scopes[qc->scope], qc, pcache_filter_cmp );
1277 qc->qbase->queries--;
1278 if ( qc->qbase->queries == 0 ) {
1279 avl_delete( &template->qbase, qc->qbase, pcache_dn_cmp );
1280 ch_free( qc->qbase );
1284 template->no_of_queries--;
1287 /* remove bottom query of LRU list from the query cache */
1289 * NOTE: slight change in functionality.
1291 * - if result->bv_val is NULL, the query at the bottom of the LRU
1293 * - otherwise, the query whose UUID is *result is removed
1294 * - if not found, result->bv_val is zeroed
1297 cache_replacement(query_manager* qm, struct berval *result)
1299 CachedQuery* bottom;
1300 QueryTemplate *temp;
1302 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1303 if ( BER_BVISNULL( result ) ) {
1304 bottom = qm->lru_bottom;
1307 Debug ( pcache_debug,
1308 "Cache replacement invoked without "
1309 "any query in LRU list\n", 0, 0, 0 );
1310 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1315 for ( bottom = qm->lru_bottom;
1317 bottom = bottom->lru_up )
1319 if ( bvmatch( result, &bottom->q_uuid ) ) {
1325 Debug ( pcache_debug,
1326 "Could not find query with uuid=\"%s\""
1327 "in LRU list\n", result->bv_val, 0, 0 );
1328 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1329 BER_BVZERO( result );
1334 temp = bottom->qtemp;
1335 remove_query(qm, bottom);
1336 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1338 *result = bottom->q_uuid;
1339 BER_BVZERO( &bottom->q_uuid );
1341 Debug( pcache_debug, "Lock CR index = %p\n", (void *) temp, 0, 0 );
1342 ldap_pvt_thread_rdwr_wlock(&temp->t_rwlock);
1343 remove_from_template(bottom, temp);
1344 Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
1345 (void *) temp, temp->no_of_queries, 0 );
1346 Debug( pcache_debug, "Unlock CR index = %p\n", (void *) temp, 0, 0 );
1347 ldap_pvt_thread_rdwr_wunlock(&temp->t_rwlock);
1352 struct query_info *next;
1364 struct query_info *qi;
1367 if ( rs->sr_type != REP_SEARCH ) return 0;
1369 attr = attr_find( rs->sr_entry->e_attrs, ad_queryId );
1370 if ( attr == NULL ) return 0;
1372 count = attr->a_numvals;
1373 assert( count > 0 );
1374 qi = op->o_tmpalloc( sizeof( struct query_info ), op->o_tmpmemctx );
1375 qi->next = op->o_callback->sc_private;
1376 op->o_callback->sc_private = qi;
1377 ber_dupbv_x( &qi->xdn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1378 qi->del = ( count == 1 );
1387 struct berval *query_uuid )
1389 struct query_info *qi, *qnext;
1390 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(queryId=)" ) ];
1391 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
1392 Filter filter = {LDAP_FILTER_EQUALITY};
1393 SlapReply sreply = {REP_RESULT};
1394 slap_callback cb = { NULL, remove_func, NULL, NULL };
1397 sreply.sr_entry = NULL;
1398 sreply.sr_nentries = 0;
1399 op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
1400 "(%s=%s)", ad_queryId->ad_cname.bv_val, query_uuid->bv_val);
1401 filter.f_ava = &ava;
1402 filter.f_av_desc = ad_queryId;
1403 filter.f_av_value = *query_uuid;
1405 op->o_tag = LDAP_REQ_SEARCH;
1406 op->o_protocol = LDAP_VERSION3;
1407 op->o_callback = &cb;
1408 op->o_time = slap_get_time();
1409 op->o_do_not_cache = 1;
1411 op->o_req_dn = op->o_bd->be_suffix[0];
1412 op->o_req_ndn = op->o_bd->be_nsuffix[0];
1413 op->ors_scope = LDAP_SCOPE_SUBTREE;
1414 op->ors_deref = LDAP_DEREF_NEVER;
1415 op->ors_slimit = SLAP_NO_LIMIT;
1416 op->ors_tlimit = SLAP_NO_LIMIT;
1417 op->ors_filter = &filter;
1418 op->ors_filterstr.bv_val = filter_str;
1419 op->ors_filterstr.bv_len = strlen(filter_str);
1420 op->ors_attrs = NULL;
1421 op->ors_attrsonly = 0;
1423 op->o_bd->be_search( op, &sreply );
1425 for ( qi=cb.sc_private; qi; qi=qnext ) {
1428 op->o_req_dn = qi->xdn;
1429 op->o_req_ndn = qi->xdn;
1432 Debug( pcache_debug, "DELETING ENTRY TEMPLATE=%s\n",
1433 query_uuid->bv_val, 0, 0 );
1435 op->o_tag = LDAP_REQ_DELETE;
1437 if (op->o_bd->be_delete(op, &sreply) == LDAP_SUCCESS) {
1443 struct berval vals[2];
1445 vals[0] = *query_uuid;
1446 vals[1].bv_val = NULL;
1448 mod.sml_op = LDAP_MOD_DELETE;
1450 mod.sml_desc = ad_queryId;
1451 mod.sml_type = ad_queryId->ad_cname;
1452 mod.sml_values = vals;
1453 mod.sml_nvalues = NULL;
1454 mod.sml_numvals = 1;
1455 mod.sml_next = NULL;
1456 Debug( pcache_debug,
1457 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
1458 query_uuid->bv_val, 0, 0 );
1460 op->orm_modlist = &mod;
1462 op->o_bd->be_modify( op, &sreply );
1464 op->o_tmpfree( qi->xdn.bv_val, op->o_tmpmemctx );
1465 op->o_tmpfree( qi, op->o_tmpmemctx );
1472 AttributeName* attrs,
1481 struct berval *fstr,
1482 AttributeName** filter_attrs,
1484 int* filter_got_oc )
1486 AttributeDescription *ad;
1489 switch ( f->f_choice ) {
1490 case LDAP_FILTER_EQUALITY:
1492 len = STRLENOF( "(=)" ) + ad->ad_cname.bv_len;
1493 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=)", ad->ad_cname.bv_val );
1494 assert( ret == len );
1495 fstr->bv_len += len;
1498 case LDAP_FILTER_GE:
1500 len = STRLENOF( "(>=)" ) + ad->ad_cname.bv_len;
1501 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s>=)", ad->ad_cname.bv_val);
1502 assert( ret == len );
1503 fstr->bv_len += len;
1506 case LDAP_FILTER_LE:
1508 len = STRLENOF( "(<=)" ) + ad->ad_cname.bv_len;
1509 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s<=)", ad->ad_cname.bv_val);
1510 assert( ret == len );
1511 fstr->bv_len += len;
1514 case LDAP_FILTER_APPROX:
1516 len = STRLENOF( "(~=)" ) + ad->ad_cname.bv_len;
1517 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s~=)", ad->ad_cname.bv_val);
1518 assert( ret == len );
1519 fstr->bv_len += len;
1522 case LDAP_FILTER_SUBSTRINGS:
1524 len = STRLENOF( "(=)" ) + ad->ad_cname.bv_len;
1525 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=)", ad->ad_cname.bv_val );
1526 assert( ret == len );
1527 fstr->bv_len += len;
1530 case LDAP_FILTER_PRESENT:
1532 len = STRLENOF( "(=*)" ) + ad->ad_cname.bv_len;
1533 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=*)", ad->ad_cname.bv_val );
1534 assert( ret == len );
1535 fstr->bv_len += len;
1538 case LDAP_FILTER_AND:
1539 case LDAP_FILTER_OR:
1540 case LDAP_FILTER_NOT: {
1542 fstr->bv_val[fstr->bv_len++] = '(';
1543 switch ( f->f_choice ) {
1544 case LDAP_FILTER_AND:
1545 fstr->bv_val[fstr->bv_len] = '&';
1547 case LDAP_FILTER_OR:
1548 fstr->bv_val[fstr->bv_len] = '|';
1550 case LDAP_FILTER_NOT:
1551 fstr->bv_val[fstr->bv_len] = '!';
1556 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1557 rc = filter2template( op, f, fstr, filter_attrs, filter_cnt,
1561 fstr->bv_val[fstr->bv_len++] = ')';
1562 fstr->bv_val[fstr->bv_len] = '\0';
1568 /* a filter should at least have room for "()",
1569 * an "=" and for a 1-char attr */
1570 strcpy( fstr->bv_val, "(?=)" );
1571 fstr->bv_len += STRLENOF("(?=)");
1575 if ( filter_attrs != NULL ) {
1576 *filter_attrs = (AttributeName *)op->o_tmprealloc(*filter_attrs,
1577 (*filter_cnt + 2)*sizeof(AttributeName), op->o_tmpmemctx);
1579 (*filter_attrs)[*filter_cnt].an_desc = ad;
1580 (*filter_attrs)[*filter_cnt].an_name = ad->ad_cname;
1581 (*filter_attrs)[*filter_cnt].an_oc = NULL;
1582 (*filter_attrs)[*filter_cnt].an_oc_exclude = 0;
1583 BER_BVZERO( &(*filter_attrs)[*filter_cnt+1].an_name );
1585 if ( ad == slap_schema.si_ad_objectClass )
1592 struct search_info {
1595 QueryTemplate *qtemp;
1596 AttributeName* save_attrs; /* original attributes, saved for response */
1597 int swap_saved_attrs;
1602 int slimit_exceeded;
1603 pc_caching_reason_t caching_reason;
1608 remove_query_and_data(
1612 struct berval *uuid )
1614 query_manager* qm = cm->qm;
1616 qm->crfunc( qm, uuid );
1617 if ( !BER_BVISNULL( uuid ) ) {
1620 Debug( pcache_debug,
1621 "Removing query UUID %s\n",
1622 uuid->bv_val, 0, 0 );
1623 return_val = remove_query_data( op, rs, uuid );
1624 Debug( pcache_debug,
1625 "QUERY REMOVED, SIZE=%d\n",
1627 ldap_pvt_thread_mutex_lock( &cm->cache_mutex );
1628 cm->cur_entries -= return_val;
1629 cm->num_cached_queries--;
1630 Debug( pcache_debug,
1631 "STORED QUERIES = %lu\n",
1632 cm->num_cached_queries, 0, 0 );
1633 ldap_pvt_thread_mutex_unlock( &cm->cache_mutex );
1634 Debug( pcache_debug,
1635 "QUERY REMOVED, CACHE ="
1637 cm->cur_entries, 0, 0 );
1642 * Callback used to fetch queryId values based on entryUUID;
1643 * used by pcache_remove_entries_from_cache()
1646 fetch_queryId_cb( Operation *op, SlapReply *rs )
1650 /* only care about searchEntry responses */
1651 if ( rs->sr_type != REP_SEARCH ) {
1655 /* allow only one response per entryUUID */
1656 if ( op->o_callback->sc_private != NULL ) {
1662 /* copy all queryId values into callback's private data */
1663 a = attr_find( rs->sr_entry->e_attrs, ad_queryId );
1665 BerVarray vals = NULL;
1667 ber_bvarray_dup_x( &vals, a->a_nvals, op->o_tmpmemctx );
1668 op->o_callback->sc_private = (void *)vals;
1672 /* clear entry if required */
1673 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
1674 entry_free( rs->sr_entry );
1675 rs->sr_entry = NULL;
1676 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
1683 * Call that allows to remove a set of entries from the cache,
1684 * by forcing the removal of all the related queries.
1687 pcache_remove_entries_from_cache(
1690 BerVarray entryUUIDs )
1692 Connection conn = { 0 };
1693 OperationBuffer opbuf;
1695 slap_callback sc = { 0 };
1696 SlapReply rs = { REP_RESULT };
1698 char filtbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(entryUUID=)" ) ];
1699 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
1700 AttributeName attrs[ 2 ] = {{{ 0 }}};
1704 void *thrctx = ldap_pvt_thread_pool_context();
1706 connection_fake_init( &conn, &opbuf, thrctx );
1714 memset( &op->oq_search, 0, sizeof( op->oq_search ) );
1715 op->ors_scope = LDAP_SCOPE_SUBTREE;
1716 op->ors_deref = LDAP_DEREF_NEVER;
1717 f.f_choice = LDAP_FILTER_EQUALITY;
1719 ava.aa_desc = slap_schema.si_ad_entryUUID;
1720 op->ors_filter = &f;
1722 op->ors_tlimit = SLAP_NO_LIMIT;
1723 attrs[ 0 ].an_desc = ad_queryId;
1724 attrs[ 0 ].an_name = ad_queryId->ad_cname;
1725 op->ors_attrs = attrs;
1726 op->ors_attrsonly = 0;
1728 op->o_req_dn = cm->db.be_suffix[ 0 ];
1729 op->o_req_ndn = cm->db.be_nsuffix[ 0 ];
1731 op->o_tag = LDAP_REQ_SEARCH;
1732 op->o_protocol = LDAP_VERSION3;
1733 op->o_managedsait = SLAP_CONTROL_CRITICAL;
1735 op->o_dn = op->o_bd->be_rootdn;
1736 op->o_ndn = op->o_bd->be_rootndn;
1737 sc.sc_response = fetch_queryId_cb;
1738 op->o_callback = ≻
1740 for ( s = 0; !BER_BVISNULL( &entryUUIDs[ s ] ); s++ ) {
1741 BerVarray vals = NULL;
1743 op->ors_filterstr.bv_len = snprintf( filtbuf, sizeof( filtbuf ),
1744 "(entryUUID=%s)", entryUUIDs[ s ].bv_val );
1745 op->ors_filterstr.bv_val = filtbuf;
1746 ava.aa_value = entryUUIDs[ s ];
1748 rc = op->o_bd->be_search( op, &rs );
1749 if ( rc != LDAP_SUCCESS ) {
1753 vals = (BerVarray)op->o_callback->sc_private;
1754 if ( vals != NULL ) {
1757 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1758 struct berval val = vals[ i ];
1760 remove_query_and_data( op, &rs, cm, &val );
1762 if ( !BER_BVISNULL( &val ) && val.bv_val != vals[ i ].bv_val ) {
1763 ch_free( val.bv_val );
1767 ber_bvarray_free_x( vals, op->o_tmpmemctx );
1768 op->o_callback->sc_private = NULL;
1776 * Call that allows to remove a query from the cache.
1779 pcache_remove_query_from_cache(
1782 struct berval *queryid )
1784 Operation op2 = *op;
1785 SlapReply rs2 = { 0 };
1789 /* remove the selected query */
1790 remove_query_and_data( &op2, &rs2, cm, queryid );
1792 return LDAP_SUCCESS;
1796 * Call that allows to remove a set of queries related to an entry
1797 * from the cache; if queryid is not null, the entry must belong to
1798 * the query indicated by queryid.
1801 pcache_remove_entry_queries_from_cache(
1805 struct berval *queryid )
1807 Connection conn = { 0 };
1808 OperationBuffer opbuf;
1810 slap_callback sc = { 0 };
1811 SlapReply rs = { REP_RESULT };
1813 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(queryId=)" ) ];
1814 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
1815 AttributeName attrs[ 2 ] = {{{ 0 }}};
1818 BerVarray vals = NULL;
1821 void *thrctx = ldap_pvt_thread_pool_context();
1823 connection_fake_init( &conn, &opbuf, thrctx );
1831 memset( &op->oq_search, 0, sizeof( op->oq_search ) );
1832 op->ors_scope = LDAP_SCOPE_BASE;
1833 op->ors_deref = LDAP_DEREF_NEVER;
1834 if ( queryid == NULL || BER_BVISNULL( queryid ) ) {
1835 BER_BVSTR( &op->ors_filterstr, "(objectClass=*)" );
1836 f.f_choice = LDAP_FILTER_PRESENT;
1837 f.f_desc = slap_schema.si_ad_objectClass;
1840 op->ors_filterstr.bv_len = snprintf( filter_str,
1841 sizeof( filter_str ), "(%s=%s)",
1842 ad_queryId->ad_cname.bv_val, queryid->bv_val );
1843 f.f_choice = LDAP_FILTER_EQUALITY;
1845 f.f_av_desc = ad_queryId;
1846 f.f_av_value = *queryid;
1848 op->ors_filter = &f;
1850 op->ors_tlimit = SLAP_NO_LIMIT;
1851 attrs[ 0 ].an_desc = ad_queryId;
1852 attrs[ 0 ].an_name = ad_queryId->ad_cname;
1853 op->ors_attrs = attrs;
1854 op->ors_attrsonly = 0;
1856 op->o_req_dn = *ndn;
1857 op->o_req_ndn = *ndn;
1859 op->o_tag = LDAP_REQ_SEARCH;
1860 op->o_protocol = LDAP_VERSION3;
1861 op->o_managedsait = SLAP_CONTROL_CRITICAL;
1863 op->o_dn = op->o_bd->be_rootdn;
1864 op->o_ndn = op->o_bd->be_rootndn;
1865 sc.sc_response = fetch_queryId_cb;
1866 op->o_callback = ≻
1868 rc = op->o_bd->be_search( op, &rs );
1869 if ( rc != LDAP_SUCCESS ) {
1873 vals = (BerVarray)op->o_callback->sc_private;
1874 if ( vals != NULL ) {
1877 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1878 struct berval val = vals[ i ];
1880 remove_query_and_data( op, &rs, cm, &val );
1882 if ( !BER_BVISNULL( &val ) && val.bv_val != vals[ i ].bv_val ) {
1883 ch_free( val.bv_val );
1887 ber_bvarray_free_x( vals, op->o_tmpmemctx );
1890 return LDAP_SUCCESS;
1897 struct berval *query_uuid )
1899 struct search_info *si = op->o_callback->sc_private;
1900 slap_overinst *on = si->on;
1901 cache_manager *cm = on->on_bi.bi_private;
1904 struct berval crp_uuid;
1905 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1907 Connection conn = {0};
1908 OperationBuffer opbuf;
1909 void *thrctx = ldap_pvt_thread_pool_context();
1911 query_uuid->bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf));
1912 ber_str2bv(uuidbuf, query_uuid->bv_len, 1, query_uuid);
1914 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
1915 op_tmp = &opbuf.ob_op;
1916 op_tmp->o_bd = &cm->db;
1917 op_tmp->o_dn = cm->db.be_rootdn;
1918 op_tmp->o_ndn = cm->db.be_rootndn;
1920 Debug( pcache_debug, "UUID for query being added = %s\n",
1923 for ( e=si->head; e; e=si->head ) {
1924 si->head = e->e_private;
1925 e->e_private = NULL;
1926 while ( cm->cur_entries > (cm->max_entries) ) {
1927 BER_BVZERO( &crp_uuid );
1928 remove_query_and_data( op_tmp, rs, cm, &crp_uuid );
1931 return_val = merge_entry(op_tmp, e, query_uuid);
1932 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1933 cm->cur_entries += return_val;
1934 Debug( pcache_debug,
1935 "ENTRY ADDED/MERGED, CACHED ENTRIES=%d\n",
1936 cm->cur_entries, 0, 0 );
1938 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1945 pcache_op_cleanup( Operation *op, SlapReply *rs ) {
1946 slap_callback *cb = op->o_callback;
1947 struct search_info *si = cb->sc_private;
1948 slap_overinst *on = si->on;
1949 cache_manager *cm = on->on_bi.bi_private;
1950 query_manager* qm = cm->qm;
1952 if ( rs->sr_type == REP_SEARCH ) {
1955 /* don't return more entries than requested by the client */
1956 if ( si->slimit && rs->sr_nentries >= si->slimit ) {
1957 si->slimit_exceeded = 1;
1960 /* If we haven't exceeded the limit for this query,
1961 * build a chain of answers to store. If we hit the
1962 * limit, empty the chain and ignore the rest.
1965 /* check if the entry contains undefined
1966 * attributes/objectClasses (ITS#5680) */
1967 if ( cm->check_cacheability && test_filter( op, rs->sr_entry, si->query.filter ) != LDAP_COMPARE_TRUE ) {
1968 Debug( pcache_debug, "%s: query not cacheable because of schema issues in DN \"%s\"\n",
1969 op->o_log_prefix, rs->sr_entry->e_name.bv_val, 0 );
1973 if ( si->count < si->max ) {
1975 e = entry_dup( rs->sr_entry );
1976 if ( !si->head ) si->head = e;
1977 if ( si->tail ) si->tail->e_private = e;
1984 for (;si->head; si->head=e) {
1985 e = si->head->e_private;
1986 si->head->e_private = NULL;
1987 entry_free(si->head);
1995 if ( rs->sr_type == REP_RESULT ||
1996 op->o_abandon || rs->sr_err == SLAPD_ABANDON )
1998 if ( si->swap_saved_attrs ) {
1999 rs->sr_attrs = si->save_attrs;
2000 op->ors_attrs = si->save_attrs;
2002 if ( (op->o_abandon || rs->sr_err == SLAPD_ABANDON) &&
2003 si->caching_reason == PC_IGNORE ) {
2004 filter_free( si->query.filter );
2006 /* duplicate query, free it */
2008 for (;si->head; si->head=e) {
2009 e = si->head->e_private;
2010 si->head->e_private = NULL;
2011 entry_free(si->head);
2014 op->o_callback = op->o_callback->sc_next;
2015 op->o_tmpfree( cb, op->o_tmpmemctx );
2016 } else if ( si->caching_reason != PC_IGNORE ) {
2017 CachedQuery *qc = qm->addfunc(op, qm, &si->query,
2018 si->qtemp, si->caching_reason, 1 );
2021 switch ( si->caching_reason ) {
2023 cache_entries( op, rs, &qc->q_uuid );
2027 qc->q_sizelimit = rs->sr_nentries;
2037 ldap_pvt_thread_rdwr_wunlock(&qc->rwlock);
2038 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2039 cm->num_cached_queries++;
2040 Debug( pcache_debug, "STORED QUERIES = %lu\n",
2041 cm->num_cached_queries, 0, 0 );
2042 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2044 /* If the consistency checker suspended itself,
2047 if ( cm->cc_paused ) {
2048 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2049 if ( cm->cc_paused ) {
2051 ldap_pvt_runqueue_resched( &slapd_rq, cm->cc_arg, 0 );
2053 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2056 } else if ( si->count ) {
2057 /* duplicate query, free it */
2059 for (;si->head; si->head=e) {
2060 e = si->head->e_private;
2061 si->head->e_private = NULL;
2062 entry_free(si->head);
2067 filter_free( si->query.filter );
2071 return SLAP_CB_CONTINUE;
2079 struct search_info *si = op->o_callback->sc_private;
2081 if ( si->swap_saved_attrs ) {
2082 rs->sr_attrs = si->save_attrs;
2083 op->ors_attrs = si->save_attrs;
2086 if ( rs->sr_type == REP_SEARCH ) {
2087 /* don't return more entries than requested by the client */
2088 if ( si->slimit_exceeded ) {
2092 } else if ( rs->sr_type == REP_RESULT ) {
2095 if ( rs->sr_err == LDAP_SUCCESS ) {
2096 si->caching_reason = PC_POSITIVE;
2098 } else if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED
2099 && si->qtemp->limitttl )
2101 si->caching_reason = PC_SIZELIMIT;
2104 } else if ( si->qtemp->negttl && !si->count && !si->over &&
2105 rs->sr_err == LDAP_SUCCESS )
2107 si->caching_reason = PC_NEGATIVE;
2111 if ( si->slimit_exceeded ) {
2112 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
2116 return SLAP_CB_CONTINUE;
2122 AttributeName** new_attrs,
2123 struct attr_set *attrs,
2124 AttributeName* filter_attrs,
2134 /* duplicate attrs */
2135 count = attrs->count + fattr_cnt;
2136 if ( !fattr_got_oc && !(attrs->flags & PC_GOT_OC)) {
2141 *new_attrs = (AttributeName*)ch_calloc( count + 1,
2142 sizeof(AttributeName) );
2143 for (i=0; i<attrs->count; i++) {
2144 (*new_attrs)[i].an_name = attrs->attrs[i].an_name;
2145 (*new_attrs)[i].an_desc = attrs->attrs[i].an_desc;
2147 BER_BVZERO( &(*new_attrs)[i].an_name );
2148 alluser = an_find(*new_attrs, &AllUser);
2149 allop = an_find(*new_attrs, &AllOper);
2152 for ( i=0; i<fattr_cnt; i++ ) {
2153 if ( an_find(*new_attrs, &filter_attrs[i].an_name ) ) {
2156 if ( is_at_operational(filter_attrs[i].an_desc->ad_type) ) {
2160 } else if ( alluser ) {
2163 (*new_attrs)[j].an_name = filter_attrs[i].an_name;
2164 (*new_attrs)[j].an_desc = filter_attrs[i].an_desc;
2165 (*new_attrs)[j].an_oc = NULL;
2166 (*new_attrs)[j].an_oc_exclude = 0;
2170 (*new_attrs)[j].an_name = slap_schema.si_ad_objectClass->ad_cname;
2171 (*new_attrs)[j].an_desc = slap_schema.si_ad_objectClass;
2172 (*new_attrs)[j].an_oc = NULL;
2173 (*new_attrs)[j].an_oc_exclude = 0;
2176 BER_BVZERO( &(*new_attrs)[j].an_name );
2181 /* NOTE: this is a quick workaround to let pcache minimally interact
2182 * with pagedResults. A more articulated solutions would be to
2183 * perform the remote query without control and cache all results,
2184 * performing the pagedResults search only within the client
2185 * and the proxy. This requires pcache to understand pagedResults. */
2187 pcache_chk_controls(
2191 const char *non = "";
2192 const char *stripped = "";
2194 switch( op->o_pagedresults ) {
2195 case SLAP_CONTROL_NONCRITICAL:
2197 stripped = "; stripped";
2200 case SLAP_CONTROL_CRITICAL:
2201 Debug( pcache_debug, "%s: "
2202 "%scritical pagedResults control "
2203 "disabled with proxy cache%s.\n",
2204 op->o_log_prefix, non, stripped );
2206 slap_remove_control( op, rs, slap_cids.sc_pagedResults, NULL );
2210 rs->sr_err = SLAP_CB_CONTINUE;
2217 #ifdef PCACHE_CONTROL_PRIVDB
2223 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2224 cache_manager *cm = on->on_bi.bi_private;
2225 slap_callback *save_cb;
2228 /* skip if control is unset */
2229 if ( op->o_ctrlflag[ privDB_cid ] != SLAP_CONTROL_CRITICAL ) {
2230 return SLAP_CB_CONTINUE;
2233 /* The cache DB isn't open yet */
2234 if ( cm->defer_db_open ) {
2235 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
2236 "pcachePrivDB: cacheDB not available" );
2240 /* FIXME: might be a little bit exaggerated... */
2241 if ( !be_isroot( op ) ) {
2242 save_cb = op->o_callback;
2243 op->o_callback = NULL;
2244 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
2245 "pcachePrivDB: operation not allowed" );
2246 op->o_callback = save_cb;
2251 /* map tag to operation */
2252 type = slap_req2op( op->o_tag );
2253 if ( type != SLAP_OP_LAST ) {
2257 /* execute, if possible */
2258 func = &cm->db.be_bind;
2259 if ( func[ type ] != NULL ) {
2260 Operation op2 = *op;
2264 rc = func[ type ]( &op2, rs );
2265 if ( type == SLAP_OP_BIND && rc == LDAP_SUCCESS ) {
2266 op->o_conn->c_authz_cookie = cm->db.be_private;
2271 /* otherwise fall back to error */
2272 save_cb = op->o_callback;
2273 op->o_callback = NULL;
2274 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
2275 "operation not supported with pcachePrivDB control" );
2276 op->o_callback = save_cb;
2280 #endif /* PCACHE_CONTROL_PRIVDB */
2287 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2288 cache_manager *cm = on->on_bi.bi_private;
2289 query_manager* qm = cm->qm;
2293 AttributeName *filter_attrs = NULL;
2296 QueryTemplate *qtemp = NULL;
2299 CachedQuery *answerable = NULL;
2302 int fattr_got_oc = 0;
2304 struct berval tempstr;
2306 #ifdef PCACHE_CONTROL_PRIVDB
2307 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL ) {
2308 return pcache_op_privdb( op, rs );
2310 #endif /* PCACHE_CONTROL_PRIVDB */
2312 /* The cache DB isn't open yet */
2313 if ( cm->defer_db_open ) {
2314 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
2315 "pcachePrivDB: cacheDB not available" );
2319 /* pickup runtime ACL changes */
2320 cm->db.be_acl = op->o_bd->be_acl;
2322 tempstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len+1, op->o_tmpmemctx );
2324 if ( filter2template( op, op->ors_filter, &tempstr, &filter_attrs,
2325 &fattr_cnt, &fattr_got_oc )) {
2326 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
2327 return SLAP_CB_CONTINUE;
2330 Debug( pcache_debug, "query template of incoming query = %s\n",
2331 tempstr.bv_val, 0, 0 );
2333 /* FIXME: cannot cache/answer requests with pagedResults control */
2336 attr_set = get_attr_set(op->ors_attrs, qm, cm->numattrsets);
2338 query.filter = op->ors_filter;
2339 query.base = op->o_req_ndn;
2340 query.scope = op->ors_scope;
2342 /* check for query containment */
2343 if (attr_set > -1) {
2344 QueryTemplate *qt = qm->attr_sets[attr_set].templates;
2345 for (; qt; qt = qt->qtnext ) {
2346 /* find if template i can potentially answer tempstr */
2347 if (qt->querystr.bv_len != tempstr.bv_len ||
2348 strcasecmp( qt->querystr.bv_val, tempstr.bv_val ))
2352 Debug( pcache_debug, "Entering QC, querystr = %s\n",
2353 op->ors_filterstr.bv_val, 0, 0 );
2354 answerable = (*(qm->qcfunc))(op, qm, &query, qt);
2360 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
2363 /* Need to clear the callbacks of the original operation,
2364 * in case there are other overlays */
2365 BackendDB *save_bd = op->o_bd;
2366 slap_callback *save_cb = op->o_callback;
2368 Debug( pcache_debug, "QUERY ANSWERABLE\n", 0, 0, 0 );
2369 op->o_tmpfree( filter_attrs, op->o_tmpmemctx );
2370 ldap_pvt_thread_rdwr_rlock(&answerable->rwlock);
2371 if ( BER_BVISNULL( &answerable->q_uuid )) {
2372 /* No entries cached, just an empty result set */
2374 send_ldap_result( op, rs );
2377 op->o_callback = NULL;
2378 i = cm->db.bd_info->bi_op_search( op, rs );
2380 ldap_pvt_thread_rdwr_runlock(&answerable->rwlock);
2381 ldap_pvt_thread_rdwr_runlock(&qtemp->t_rwlock);
2383 op->o_callback = save_cb;
2387 Debug( pcache_debug, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
2389 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2390 if (cm->num_cached_queries >= cm->max_queries) {
2393 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2395 if (op->ors_attrsonly)
2400 struct search_info *si;
2402 Debug( pcache_debug, "QUERY CACHEABLE\n", 0, 0, 0 );
2403 query.filter = filter_dup(op->ors_filter, NULL);
2404 ldap_pvt_thread_rdwr_wlock(&qtemp->t_rwlock);
2405 if ( !qtemp->t_attrs.count ) {
2406 qtemp->t_attrs.count = add_filter_attrs(op,
2407 &qtemp->t_attrs.attrs,
2408 &qm->attr_sets[attr_set],
2409 filter_attrs, fattr_cnt, fattr_got_oc);
2411 ldap_pvt_thread_rdwr_wunlock(&qtemp->t_rwlock);
2413 cb = op->o_tmpalloc( sizeof(*cb) + sizeof(*si), op->o_tmpmemctx );
2414 cb->sc_response = pcache_response;
2415 cb->sc_cleanup = pcache_op_cleanup;
2416 cb->sc_private = (cb+1);
2417 si = cb->sc_private;
2421 si->max = cm->num_entries_limit ;
2425 si->slimit_exceeded = 0;
2426 si->caching_reason = PC_IGNORE;
2427 if ( op->ors_slimit && op->ors_slimit < cm->num_entries_limit ) {
2428 si->slimit = op->ors_slimit;
2429 op->ors_slimit = cm->num_entries_limit;
2433 si->swap_saved_attrs = 1;
2434 si->save_attrs = op->ors_attrs;
2436 op->ors_attrs = qtemp->t_attrs.attrs;
2438 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
2439 cb->sc_next = op->o_callback;
2440 op->o_callback = cb;
2443 slap_callback **pcb;
2445 /* need to move the callback at the end, in case other
2446 * overlays are present, so that the final entry is
2447 * actually cached */
2449 for ( pcb = &op->o_callback; *pcb; pcb = &(*pcb)->sc_next );
2454 Debug( pcache_debug, "QUERY NOT CACHEABLE\n",
2458 op->o_tmpfree( filter_attrs, op->o_tmpmemctx );
2460 return SLAP_CB_CONTINUE;
2465 AttributeName* attrs,
2473 for ( ; attrs[count].an_name.bv_val; count++ );
2476 /* recognize a single "*" or a "1.1" */
2479 attrs = slap_anlist_all_user_attributes;
2481 } else if ( count == 1 && strcmp( attrs[0].an_name.bv_val, LDAP_NO_ATTRS ) == 0 ) {
2486 for ( i = 0; i < num; i++ ) {
2490 if ( count > qm->attr_sets[i].count ) {
2495 if ( !qm->attr_sets[i].count ) {
2501 for ( a2 = attrs; a2->an_name.bv_val; a2++ ) {
2502 if ( !an_find( qm->attr_sets[i].attrs, &a2->an_name ) ) {
2525 struct re_s *rtask = arg;
2526 slap_overinst *on = rtask->arg;
2527 cache_manager *cm = on->on_bi.bi_private;
2528 query_manager *qm = cm->qm;
2529 Connection conn = {0};
2530 OperationBuffer opbuf;
2533 SlapReply rs = {REP_RESULT};
2535 int return_val, pause = 1;
2536 QueryTemplate* templ;
2538 connection_fake_init( &conn, &opbuf, ctx );
2542 op->o_dn = cm->db.be_rootdn;
2543 op->o_ndn = cm->db.be_rootndn;
2547 for (templ = qm->templates; templ; templ=templ->qmnext) {
2548 query = templ->query_last;
2549 if ( query ) pause = 0;
2550 op->o_time = slap_get_time();
2551 while (query && (query->expiry_time < op->o_time)) {
2553 Debug( pcache_debug, "Lock CR index = %p\n",
2554 (void *) templ, 0, 0 );
2555 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
2556 if ( query == templ->query_last ) {
2558 remove_from_template(query, templ);
2559 Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
2560 (void *) templ, templ->no_of_queries, 0 );
2561 Debug( pcache_debug, "Unlock CR index = %p\n",
2562 (void *) templ, 0, 0 );
2564 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
2566 query = templ->query_last;
2569 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
2570 remove_query(qm, query);
2571 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
2572 if ( BER_BVISNULL( &query->q_uuid ))
2575 return_val = remove_query_data(op, &rs, &query->q_uuid);
2576 Debug( pcache_debug, "STALE QUERY REMOVED, SIZE=%d\n",
2578 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2579 cm->cur_entries -= return_val;
2580 cm->num_cached_queries--;
2581 Debug( pcache_debug, "STORED QUERIES = %lu\n",
2582 cm->num_cached_queries, 0, 0 );
2583 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2584 Debug( pcache_debug,
2585 "STALE QUERY REMOVED, CACHE ="
2587 cm->cur_entries, 0, 0 );
2589 query = templ->query_last;
2592 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2593 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
2594 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
2596 /* If there were no queries, defer processing for a while */
2597 cm->cc_paused = pause;
2598 ldap_pvt_runqueue_resched( &slapd_rq, rtask, pause );
2600 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2605 #define MAX_ATTR_SETS 500
2615 static ConfigDriver pc_cf_gen;
2616 static ConfigLDAPadd pc_ldadd;
2617 static ConfigCfAdd pc_cfadd;
2619 static ConfigTable pccfg[] = {
2620 { "proxycache", "backend> <max_entries> <numattrsets> <entry limit> "
2622 6, 6, 0, ARG_MAGIC|ARG_NO_DELETE|PC_MAIN, pc_cf_gen,
2623 "( OLcfgOvAt:2.1 NAME 'olcProxyCache' "
2624 "DESC 'ProxyCache basic parameters' "
2625 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
2626 { "proxyattrset", "index> <attributes...",
2627 2, 0, 0, ARG_MAGIC|PC_ATTR, pc_cf_gen,
2628 "( OLcfgOvAt:2.2 NAME 'olcProxyAttrset' "
2629 "DESC 'A set of attributes to cache' "
2630 "SYNTAX OMsDirectoryString )", NULL, NULL },
2631 { "proxytemplate", "filter> <attrset-index> <TTL> <negTTL",
2632 4, 6, 0, ARG_MAGIC|PC_TEMP, pc_cf_gen,
2633 "( OLcfgOvAt:2.3 NAME 'olcProxyTemplate' "
2634 "DESC 'Filter template, attrset, cache TTL, "
2635 "optional negative TTL, optional sizelimit TTL' "
2636 "SYNTAX OMsDirectoryString )", NULL, NULL },
2637 { "response-callback", "head|tail(default)",
2638 2, 2, 0, ARG_MAGIC|PC_RESP, pc_cf_gen,
2639 "( OLcfgOvAt:2.4 NAME 'olcProxyResponseCB' "
2640 "DESC 'Response callback position in overlay stack' "
2641 "SYNTAX OMsDirectoryString )", NULL, NULL },
2642 { "proxyCacheQueries", "queries",
2643 2, 2, 0, ARG_INT|ARG_MAGIC|PC_QUERIES, pc_cf_gen,
2644 "( OLcfgOvAt:2.5 NAME 'olcProxyCacheQueries' "
2645 "DESC 'Maximum number of queries to cache' "
2646 "SYNTAX OMsInteger )", NULL, NULL },
2647 { "proxySaveQueries", "TRUE|FALSE",
2648 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, save_queries),
2649 "( OLcfgOvAt:2.6 NAME 'olcProxySaveQueries' "
2650 "DESC 'Save cached queries for hot restart' "
2651 "SYNTAX OMsBoolean )", NULL, NULL },
2652 { "proxyCheckCacheability", "TRUE|FALSE",
2653 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, check_cacheability),
2654 "( OLcfgOvAt:2.7 NAME 'olcProxyCheckCacheability' "
2655 "DESC 'Check whether the results of a query are cacheable, e.g. for schema issues' "
2656 "SYNTAX OMsBoolean )", NULL, NULL },
2658 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
2661 static ConfigOCs pcocs[] = {
2662 { "( OLcfgOvOc:2.1 "
2663 "NAME 'olcPcacheConfig' "
2664 "DESC 'ProxyCache configuration' "
2665 "SUP olcOverlayConfig "
2666 "MUST ( olcProxyCache $ olcProxyAttrset $ olcProxyTemplate ) "
2667 "MAY ( olcProxyResponseCB $ olcProxyCacheQueries $ olcProxySaveQueries $ olcProxyCheckCacheability ) )",
2668 Cft_Overlay, pccfg, NULL, pc_cfadd },
2669 { "( OLcfgOvOc:2.2 "
2670 "NAME 'olcPcacheDatabase' "
2671 "DESC 'Cache database configuration' "
2672 "AUXILIARY )", Cft_Misc, olcDatabaseDummy, pc_ldadd },
2676 static int pcache_db_open2( slap_overinst *on, ConfigReply *cr );
2679 pc_ldadd_cleanup( ConfigArgs *c )
2681 slap_overinst *on = c->ca_private;
2682 return pcache_db_open2( on, &c->reply );
2686 pc_ldadd( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
2691 if ( p->ce_type != Cft_Overlay || !p->ce_bi ||
2692 p->ce_bi->bi_cf_ocs != pcocs )
2693 return LDAP_CONSTRAINT_VIOLATION;
2695 on = (slap_overinst *)p->ce_bi;
2696 cm = on->on_bi.bi_private;
2698 /* Defer open if this is an LDAPadd */
2699 if ( CONFIG_ONLINE_ADD( ca ))
2700 ca->cleanup = pc_ldadd_cleanup;
2702 cm->defer_db_open = 0;
2703 ca->ca_private = on;
2704 return LDAP_SUCCESS;
2708 pc_cfadd( Operation *op, SlapReply *rs, Entry *p, ConfigArgs *ca )
2710 CfEntryInfo *pe = p->e_private;
2711 slap_overinst *on = (slap_overinst *)pe->ce_bi;
2712 cache_manager *cm = on->on_bi.bi_private;
2715 /* FIXME: should not hardcode "olcDatabase" here */
2716 bv.bv_len = snprintf( ca->cr_msg, sizeof( ca->cr_msg ),
2717 "olcDatabase=%s", cm->db.bd_info->bi_type );
2718 if ( bv.bv_len >= sizeof( ca->cr_msg ) ) {
2721 bv.bv_val = ca->cr_msg;
2723 cm->defer_db_open = 0;
2725 /* We can only create this entry if the database is table-driven
2727 if ( cm->db.bd_info->bi_cf_ocs )
2728 config_build_entry( op, rs, pe, ca, &bv, cm->db.bd_info->bi_cf_ocs,
2735 pc_cf_gen( ConfigArgs *c )
2737 slap_overinst *on = (slap_overinst *)c->bi;
2738 cache_manager* cm = on->on_bi.bi_private;
2739 query_manager* qm = cm->qm;
2740 QueryTemplate* temp;
2741 AttributeName* attr_name;
2742 AttributeName* attrarray;
2743 const char* text=NULL;
2748 if ( c->op == SLAP_CONFIG_EMIT ) {
2752 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s %d %d %d %ld",
2753 cm->db.bd_info->bi_type, cm->max_entries, cm->numattrsets,
2754 cm->num_entries_limit, cm->cc_period );
2755 bv.bv_val = c->cr_msg;
2756 value_add_one( &c->rvalue_vals, &bv );
2759 for (i=0; i<cm->numattrsets; i++) {
2760 if ( !qm->attr_sets[i].count ) continue;
2762 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ), "%d", i );
2764 /* count the attr length */
2765 for ( attr_name = qm->attr_sets[i].attrs;
2766 attr_name->an_name.bv_val; attr_name++ )
2767 bv.bv_len += attr_name->an_name.bv_len + 1;
2769 bv.bv_val = ch_malloc( bv.bv_len+1 );
2770 ptr = lutil_strcopy( bv.bv_val, c->cr_msg );
2771 for ( attr_name = qm->attr_sets[i].attrs;
2772 attr_name->an_name.bv_val; attr_name++ ) {
2774 ptr = lutil_strcopy( ptr, attr_name->an_name.bv_val );
2776 ber_bvarray_add( &c->rvalue_vals, &bv );
2778 if ( !c->rvalue_vals )
2782 for (temp=qm->templates; temp; temp=temp->qmnext) {
2783 /* HEADS-UP: always print all;
2784 * if optional == 0, ignore */
2785 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ),
2787 temp->attr_set_index,
2791 bv.bv_len += temp->querystr.bv_len + 2;
2792 bv.bv_val = ch_malloc( bv.bv_len+1 );
2795 ptr = lutil_strcopy( ptr, temp->querystr.bv_val );
2797 strcpy( ptr, c->cr_msg );
2798 ber_bvarray_add( &c->rvalue_vals, &bv );
2800 if ( !c->rvalue_vals )
2804 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
2805 BER_BVSTR( &bv, "head" );
2807 BER_BVSTR( &bv, "tail" );
2809 value_add_one( &c->rvalue_vals, &bv );
2812 c->value_int = cm->max_queries;
2816 } else if ( c->op == LDAP_MOD_DELETE ) {
2817 return 1; /* FIXME */
2829 if ( cm->numattrsets > 0 ) {
2830 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"proxycache\" directive already provided" );
2831 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2835 if ( lutil_atoi( &cm->numattrsets, c->argv[3] ) != 0 ) {
2836 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse num attrsets=\"%s\" (arg #3)",
2838 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2841 if ( cm->numattrsets <= 0 ) {
2842 snprintf( c->cr_msg, sizeof( c->cr_msg ), "numattrsets (arg #3) must be positive" );
2843 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2846 if ( cm->numattrsets > MAX_ATTR_SETS ) {
2847 snprintf( c->cr_msg, sizeof( c->cr_msg ), "numattrsets (arg #3) must be <= %d", MAX_ATTR_SETS );
2848 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2852 if ( !backend_db_init( c->argv[1], &cm->db, -1, NULL )) {
2853 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unknown backend type (arg #1)" );
2854 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2858 if ( lutil_atoi( &cm->max_entries, c->argv[2] ) != 0 ) {
2859 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse max entries=\"%s\" (arg #2)",
2861 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2864 if ( cm->max_entries <= 0 ) {
2865 snprintf( c->cr_msg, sizeof( c->cr_msg ), "max entries (arg #2) must be positive.\n" );
2866 Debug( LDAP_DEBUG_CONFIG, "%s: %s\n", c->log, c->cr_msg, 0 );
2870 if ( lutil_atoi( &cm->num_entries_limit, c->argv[4] ) != 0 ) {
2871 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse entry limit=\"%s\" (arg #4)",
2873 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2876 if ( cm->num_entries_limit <= 0 ) {
2877 snprintf( c->cr_msg, sizeof( c->cr_msg ), "entry limit (arg #4) must be positive" );
2878 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2881 if ( cm->num_entries_limit > cm->max_entries ) {
2882 snprintf( c->cr_msg, sizeof( c->cr_msg ), "entry limit (arg #4) must be less than max entries %d (arg #2)", cm->max_entries );
2883 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2887 if ( lutil_parse_time( c->argv[5], &t ) != 0 ) {
2888 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse period=\"%s\" (arg #5)",
2890 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2894 cm->cc_period = (time_t)t;
2895 Debug( pcache_debug,
2896 "Total # of attribute sets to be cached = %d.\n",
2897 cm->numattrsets, 0, 0 );
2898 qm->attr_sets = ( struct attr_set * )ch_calloc( cm->numattrsets,
2899 sizeof( struct attr_set ) );
2902 if ( cm->numattrsets == 0 ) {
2903 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"proxycache\" directive not provided yet" );
2904 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2907 if ( lutil_atoi( &num, c->argv[1] ) != 0 ) {
2908 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse attrset #=\"%s\"",
2910 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2914 if ( num < 0 || num >= cm->numattrsets ) {
2915 snprintf( c->cr_msg, sizeof( c->cr_msg ), "attrset index %d out of bounds (must be %s%d)",
2916 num, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
2917 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2920 qm->attr_sets[num].flags |= PC_CONFIGURED;
2921 if ( c->argc == 2 ) {
2923 snprintf( c->cr_msg, sizeof( c->cr_msg ),
2924 "need an explicit attr in attrlist; use \"*\" to indicate all attrs" );
2925 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2928 } else if ( c->argc == 3 ) {
2929 if ( strcmp( c->argv[2], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) {
2930 qm->attr_sets[num].count = 1;
2931 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 2,
2932 sizeof( AttributeName ) );
2933 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_USER_ATTRIBUTES );
2936 } else if ( strcmp( c->argv[2], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 ) {
2937 qm->attr_sets[num].count = 1;
2938 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 2,
2939 sizeof( AttributeName ) );
2940 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
2943 } else if ( strcmp( c->argv[2], LDAP_NO_ATTRS ) == 0 ) {
2946 /* else: fallthru */
2948 } else if ( c->argc == 4 ) {
2949 if ( ( strcmp( c->argv[2], LDAP_ALL_USER_ATTRIBUTES ) == 0 && strcmp( c->argv[3], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 )
2950 || ( strcmp( c->argv[2], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 && strcmp( c->argv[3], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) )
2952 qm->attr_sets[num].count = 2;
2953 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 3,
2954 sizeof( AttributeName ) );
2955 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_USER_ATTRIBUTES );
2956 BER_BVSTR( &qm->attr_sets[num].attrs[1].an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
2959 /* else: fallthru */
2962 if ( c->argc > 2 ) {
2963 int all_user = 0, all_op = 0;
2965 qm->attr_sets[num].count = c->argc - 2;
2966 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( c->argc - 1,
2967 sizeof( AttributeName ) );
2968 attr_name = qm->attr_sets[num].attrs;
2969 for ( i = 2; i < c->argc; i++ ) {
2970 attr_name->an_desc = NULL;
2971 if ( strcmp( c->argv[i], LDAP_NO_ATTRS ) == 0 ) {
2972 snprintf( c->cr_msg, sizeof( c->cr_msg ),
2973 "invalid attr #%d \"%s\" in attrlist",
2974 i - 2, c->argv[i] );
2975 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2976 ch_free( qm->attr_sets[num].attrs );
2977 qm->attr_sets[num].attrs = NULL;
2978 qm->attr_sets[num].count = 0;
2981 if ( strcmp( c->argv[i], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) {
2983 BER_BVSTR( &attr_name->an_name, LDAP_ALL_USER_ATTRIBUTES );
2984 } else if ( strcmp( c->argv[i], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 ) {
2986 BER_BVSTR( &attr_name->an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
2988 if ( slap_str2ad( c->argv[i], &attr_name->an_desc, &text ) ) {
2989 strcpy( c->cr_msg, text );
2990 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2991 ch_free( qm->attr_sets[num].attrs );
2992 qm->attr_sets[num].attrs = NULL;
2993 qm->attr_sets[num].count = 0;
2996 attr_name->an_name = attr_name->an_desc->ad_cname;
2998 attr_name->an_oc = NULL;
2999 attr_name->an_oc_exclude = 0;
3000 if ( attr_name->an_desc == slap_schema.si_ad_objectClass )
3001 qm->attr_sets[num].flags |= PC_GOT_OC;
3003 BER_BVZERO( &attr_name->an_name );
3006 /* warn if list contains both "*" and "+" */
3007 if ( i > 4 && all_user && all_op ) {
3008 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3009 "warning: attribute list contains \"*\" and \"+\"" );
3010 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3015 if ( cm->numattrsets == 0 ) {
3016 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"proxycache\" directive not provided yet" );
3017 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3020 if ( lutil_atoi( &i, c->argv[2] ) != 0 ) {
3021 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse template #=\"%s\"",
3023 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3027 if ( i < 0 || i >= cm->numattrsets ||
3028 !(qm->attr_sets[i].flags & PC_CONFIGURED )) {
3029 snprintf( c->cr_msg, sizeof( c->cr_msg ), "template index %d invalid (%s%d)",
3030 i, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
3031 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3034 temp = ch_calloc( 1, sizeof( QueryTemplate ));
3035 temp->qmnext = qm->templates;
3036 qm->templates = temp;
3037 ldap_pvt_thread_rdwr_init( &temp->t_rwlock );
3038 temp->query = temp->query_last = NULL;
3039 if ( lutil_parse_time( c->argv[3], &t ) != 0 ) {
3040 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3041 "unable to parse template ttl=\"%s\"",
3043 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3046 temp->ttl = (time_t)t;
3047 temp->negttl = (time_t)0;
3048 temp->limitttl = (time_t)0;
3049 switch ( c->argc ) {
3051 if ( lutil_parse_time( c->argv[5], &t ) != 0 ) {
3052 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3053 "unable to parse template sizelimit ttl=\"%s\"",
3055 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3058 temp->limitttl = (time_t)t;
3062 if ( lutil_parse_time( c->argv[4], &t ) != 0 ) {
3063 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3064 "unable to parse template negative ttl=\"%s\"",
3066 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3069 temp->negttl = (time_t)t;
3073 temp->no_of_queries = 0;
3075 ber_str2bv( c->argv[1], 0, 1, &temp->querystr );
3076 Debug( pcache_debug, "Template:\n", 0, 0, 0 );
3077 Debug( pcache_debug, " query template: %s\n",
3078 temp->querystr.bv_val, 0, 0 );
3079 temp->attr_set_index = i;
3080 qm->attr_sets[i].flags |= PC_REFERENCED;
3081 temp->qtnext = qm->attr_sets[i].templates;
3082 qm->attr_sets[i].templates = temp;
3083 Debug( pcache_debug, " attributes: \n", 0, 0, 0 );
3084 if ( ( attrarray = qm->attr_sets[i].attrs ) != NULL ) {
3085 for ( i=0; attrarray[i].an_name.bv_val; i++ )
3086 Debug( pcache_debug, "\t%s\n",
3087 attrarray[i].an_name.bv_val, 0, 0 );
3091 if ( strcasecmp( c->argv[1], "head" ) == 0 ) {
3092 cm->response_cb = PCACHE_RESPONSE_CB_HEAD;
3094 } else if ( strcasecmp( c->argv[1], "tail" ) == 0 ) {
3095 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
3098 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unknown specifier" );
3099 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3104 if ( c->value_int <= 0 ) {
3105 snprintf( c->cr_msg, sizeof( c->cr_msg ), "max queries must be positive" );
3106 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3109 cm->max_queries = c->value_int;
3124 slap_overinst *on = (slap_overinst *)be->bd_info;
3125 cache_manager* cm = on->on_bi.bi_private;
3127 /* Something for the cache database? */
3128 if ( cm->db.bd_info && cm->db.bd_info->bi_db_config )
3129 return cm->db.bd_info->bi_db_config( &cm->db, fname, lineno,
3131 return SLAP_CONF_UNKNOWN;
3139 slap_overinst *on = (slap_overinst *)be->bd_info;
3143 cm = (cache_manager *)ch_malloc(sizeof(cache_manager));
3144 on->on_bi.bi_private = cm;
3146 qm = (query_manager*)ch_malloc(sizeof(query_manager));
3149 SLAP_DBFLAGS(&cm->db) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3150 cm->db.be_private = NULL;
3151 cm->db.bd_self = &cm->db;
3153 cm->numattrsets = 0;
3154 cm->num_entries_limit = 5;
3155 cm->num_cached_queries = 0;
3156 cm->max_entries = 0;
3157 cm->cur_entries = 0;
3158 cm->max_queries = 10000;
3159 cm->save_queries = 0;
3160 cm->check_cacheability = 0;
3161 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
3162 cm->defer_db_open = 1;
3163 cm->cc_period = 1000;
3167 qm->attr_sets = NULL;
3168 qm->templates = NULL;
3170 qm->lru_bottom = NULL;
3172 qm->qcfunc = query_containment;
3173 qm->crfunc = cache_replacement;
3174 qm->addfunc = add_query;
3175 ldap_pvt_thread_mutex_init(&qm->lru_mutex);
3177 ldap_pvt_thread_mutex_init(&cm->cache_mutex);
3182 pcache_cachedquery_open_cb( Operation *op, SlapReply *rs )
3184 assert( op->o_tag == LDAP_REQ_SEARCH );
3186 if ( rs->sr_type == REP_SEARCH ) {
3189 a = attr_find( rs->sr_entry->e_attrs, ad_cachedQueryURL );
3193 assert( a->a_nvals != NULL );
3195 valsp = op->o_callback->sc_private;
3196 assert( *valsp == NULL );
3198 ber_bvarray_dup_x( valsp, a->a_nvals, op->o_tmpmemctx );
3206 pcache_cachedquery_count_cb( Operation *op, SlapReply *rs )
3208 assert( op->o_tag == LDAP_REQ_SEARCH );
3210 if ( rs->sr_type == REP_SEARCH ) {
3211 int *countp = (int *)op->o_callback->sc_private;
3224 cache_manager *cm = on->on_bi.bi_private;
3225 query_manager* qm = cm->qm;
3228 rc = backend_startup_one( &cm->db, cr );
3230 cm->defer_db_open = 0;
3233 /* There is no runqueue in TOOL mode */
3234 if (( slapMode & SLAP_SERVER_MODE ) && rc == 0 ) {
3235 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3236 ldap_pvt_runqueue_insert( &slapd_rq, cm->cc_period,
3237 consistency_check, on,
3238 "pcache_consistency", cm->db.be_suffix[0].bv_val );
3239 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3241 /* Cached database must have the rootdn */
3242 if ( BER_BVISNULL( &cm->db.be_rootndn )
3243 || BER_BVISEMPTY( &cm->db.be_rootndn ) )
3245 Debug( LDAP_DEBUG_ANY, "pcache_db_open(): "
3246 "underlying database of type \"%s\"\n"
3247 " serving naming context \"%s\"\n"
3248 " has no \"rootdn\", required by \"proxycache\".\n",
3249 on->on_info->oi_orig->bi_type,
3250 cm->db.be_suffix[0].bv_val, 0 );
3254 if ( cm->save_queries ) {
3255 void *thrctx = ldap_pvt_thread_pool_context();
3256 Connection conn = { 0 };
3257 OperationBuffer opbuf;
3259 slap_callback cb = { 0 };
3260 SlapReply rs = { 0 };
3261 BerVarray vals = NULL;
3262 Filter f = { 0 }, f2 = { 0 };
3263 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
3264 AttributeName attrs[ 2 ] = {{{ 0 }}};
3266 connection_fake_init( &conn, &opbuf, thrctx );
3271 op->o_tag = LDAP_REQ_SEARCH;
3272 op->o_protocol = LDAP_VERSION3;
3273 cb.sc_response = pcache_cachedquery_open_cb;
3274 cb.sc_private = &vals;
3275 op->o_callback = &cb;
3276 op->o_time = slap_get_time();
3277 op->o_do_not_cache = 1;
3278 op->o_managedsait = SLAP_CONTROL_CRITICAL;
3280 op->o_dn = cm->db.be_rootdn;
3281 op->o_ndn = cm->db.be_rootndn;
3282 op->o_req_dn = cm->db.be_suffix[ 0 ];
3283 op->o_req_ndn = cm->db.be_nsuffix[ 0 ];
3285 op->ors_scope = LDAP_SCOPE_BASE;
3286 op->ors_deref = LDAP_DEREF_NEVER;
3288 op->ors_tlimit = SLAP_NO_LIMIT;
3289 ber_str2bv( "(cachedQueryURL=*)", 0, 0, &op->ors_filterstr );
3290 f.f_choice = LDAP_FILTER_PRESENT;
3291 f.f_desc = ad_cachedQueryURL;
3292 op->ors_filter = &f;
3293 attrs[ 0 ].an_desc = ad_cachedQueryURL;
3294 attrs[ 0 ].an_name = ad_cachedQueryURL->ad_cname;
3295 op->ors_attrs = attrs;
3296 op->ors_attrsonly = 0;
3298 rc = op->o_bd->be_search( op, &rs );
3299 if ( rc == LDAP_SUCCESS && vals != NULL ) {
3302 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
3303 if ( url2query( vals[ i ].bv_val, op, qm ) == 0 ) {
3304 cm->num_cached_queries++;
3308 ber_bvarray_free_x( vals, op->o_tmpmemctx );
3311 /* count cached entries */
3312 f.f_choice = LDAP_FILTER_NOT;
3314 f2.f_choice = LDAP_FILTER_EQUALITY;
3316 f2.f_av_desc = slap_schema.si_ad_objectClass;
3317 BER_BVSTR( &f2.f_av_value, "glue" );
3318 ber_str2bv( "(!(objectClass=glue))", 0, 0, &op->ors_filterstr );
3320 op->ors_slimit = SLAP_NO_LIMIT;
3321 op->ors_scope = LDAP_SCOPE_SUBTREE;
3322 op->ors_attrs = slap_anlist_no_attrs;
3324 op->o_callback->sc_response = pcache_cachedquery_count_cb;
3326 op->o_callback->sc_private = &rs.sr_nentries;
3328 rc = op->o_bd->be_search( op, &rs );
3330 cm->cur_entries = rs.sr_nentries;
3344 slap_overinst *on = (slap_overinst *)be->bd_info;
3345 cache_manager *cm = on->on_bi.bi_private;
3346 query_manager* qm = cm->qm;
3347 int i, ncf = 0, rf = 0, nrf = 0, rc = 0;
3349 /* check attr sets */
3350 for ( i = 0; i < cm->numattrsets; i++) {
3351 if ( !( qm->attr_sets[i].flags & PC_CONFIGURED ) ) {
3352 if ( qm->attr_sets[i].flags & PC_REFERENCED ) {
3353 Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d not configured but referenced.\n", i, 0, 0 );
3357 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, attr set #%d not configured.\n", i, 0, 0 );
3361 } else if ( !( qm->attr_sets[i].flags & PC_REFERENCED ) ) {
3362 Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d configured but not referenced.\n", i, 0, 0 );
3367 if ( ncf || rf || nrf ) {
3368 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets configured but not referenced.\n", nrf, 0, 0 );
3369 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets not configured.\n", ncf, 0, 0 );
3370 Debug( LDAP_DEBUG_CONFIG, "pcache: %d attr sets not configured but referenced.\n", rf, 0, 0 );
3377 /* need to inherit something from the original database... */
3378 cm->db.be_def_limit = be->be_def_limit;
3379 cm->db.be_limits = be->be_limits;
3380 cm->db.be_acl = be->be_acl;
3381 cm->db.be_dfltaccess = be->be_dfltaccess;
3383 if ( SLAP_DBMONITORING( be ) ) {
3384 SLAP_DBFLAGS( &cm->db ) |= SLAP_DBFLAG_MONITORING;
3387 SLAP_DBFLAGS( &cm->db ) &= ~SLAP_DBFLAG_MONITORING;
3390 if ( !cm->defer_db_open )
3391 rc = pcache_db_open2( on, cr );
3397 pcache_free_qbase( void *v )
3403 tavl_free( qb->scopes[i], NULL );
3413 slap_overinst *on = (slap_overinst *)be->bd_info;
3414 cache_manager *cm = on->on_bi.bi_private;
3415 query_manager *qm = cm->qm;
3419 if ( cm->save_queries ) {
3421 BerVarray vals = NULL;
3424 Connection conn = { 0 };
3425 OperationBuffer opbuf;
3427 slap_callback cb = { 0 };
3429 SlapReply rs = { REP_RESULT };
3430 Modifications mod = {{ 0 }};
3432 thrctx = ldap_pvt_thread_pool_context();
3434 connection_fake_init( &conn, &opbuf, thrctx );
3437 if ( qm->templates != NULL ) {
3438 for ( tm = qm->templates; tm != NULL; tm = tm->qmnext ) {
3439 for ( qc = tm->query; qc; qc = qc->next ) {
3442 if ( query2url( op, qc, &bv ) == 0 ) {
3443 ber_bvarray_add_x( &vals, &bv, op->o_tmpmemctx );
3450 op->o_dn = cm->db.be_rootdn;
3451 op->o_ndn = cm->db.be_rootndn;
3453 op->o_tag = LDAP_REQ_MODIFY;
3454 op->o_protocol = LDAP_VERSION3;
3455 cb.sc_response = slap_null_cb;
3456 op->o_callback = &cb;
3457 op->o_time = slap_get_time();
3458 op->o_do_not_cache = 1;
3459 op->o_managedsait = SLAP_CONTROL_CRITICAL;
3461 op->o_req_dn = op->o_bd->be_suffix[0];
3462 op->o_req_ndn = op->o_bd->be_nsuffix[0];
3464 mod.sml_op = LDAP_MOD_REPLACE;
3466 mod.sml_desc = ad_cachedQueryURL;
3467 mod.sml_type = ad_cachedQueryURL->ad_cname;
3468 mod.sml_values = vals;
3469 mod.sml_nvalues = NULL;
3470 mod.sml_numvals = 1;
3471 mod.sml_next = NULL;
3472 Debug( pcache_debug,
3473 "%sSETTING CACHED QUERY URLS\n",
3474 vals == NULL ? "RE" : "", 0, 0 );
3476 op->orm_modlist = &mod;
3478 op->o_bd->be_modify( op, &rs );
3480 ber_bvarray_free_x( vals, op->o_tmpmemctx );
3483 /* cleanup stuff inherited from the original database... */
3484 cm->db.be_limits = NULL;
3485 cm->db.be_acl = NULL;
3487 /* stop the thread ... */
3489 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3490 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, cm->cc_arg ) ) {
3491 ldap_pvt_runqueue_stoptask( &slapd_rq, cm->cc_arg );
3493 ldap_pvt_runqueue_remove( &slapd_rq, cm->cc_arg );
3494 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3497 if ( cm->db.bd_info->bi_db_close ) {
3498 rc = cm->db.bd_info->bi_db_close( &cm->db, NULL );
3500 while ( (tm = qm->templates) != NULL ) {
3501 CachedQuery *qc, *qn;
3502 qm->templates = tm->qmnext;
3503 for ( qc = tm->query; qc; qc = qn ) {
3507 avl_free( tm->qbase, pcache_free_qbase );
3508 free( tm->querystr.bv_val );
3509 ldap_pvt_thread_rdwr_destroy( &tm->t_rwlock );
3510 free( tm->t_attrs.attrs );
3514 for ( i=0; i<cm->numattrsets; i++ ) {
3515 free( qm->attr_sets[i].attrs );
3517 free( qm->attr_sets );
3518 qm->attr_sets = NULL;
3529 slap_overinst *on = (slap_overinst *)be->bd_info;
3530 cache_manager *cm = on->on_bi.bi_private;
3531 query_manager *qm = cm->qm;
3533 if ( cm->db.be_private != NULL ) {
3534 backend_stopdown_one( &cm->db );
3537 ldap_pvt_thread_mutex_destroy( &qm->lru_mutex );
3538 ldap_pvt_thread_mutex_destroy( &cm->cache_mutex );
3545 #ifdef PCACHE_CONTROL_PRIVDB
3547 Control ::= SEQUENCE {
3548 controlType LDAPOID,
3549 criticality BOOLEAN DEFAULT FALSE,
3550 controlValue OCTET STRING OPTIONAL }
3552 controlType ::= 1.3.6.1.4.1.4203.666.11.9.5.1
3554 * criticality must be TRUE; controlValue must be absent.
3562 if ( op->o_ctrlflag[ privDB_cid ] != SLAP_CONTROL_NONE ) {
3563 rs->sr_text = "privateDB control specified multiple times";
3564 return LDAP_PROTOCOL_ERROR;
3567 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
3568 rs->sr_text = "privateDB control value not absent";
3569 return LDAP_PROTOCOL_ERROR;
3572 if ( !ctrl->ldctl_iscritical ) {
3573 rs->sr_text = "privateDB control criticality required";
3574 return LDAP_PROTOCOL_ERROR;
3577 op->o_ctrlflag[ privDB_cid ] = SLAP_CONTROL_CRITICAL;
3579 return LDAP_SUCCESS;
3582 static char *extops[] = {
3583 LDAP_EXOP_MODIFY_PASSWD,
3586 #endif /* PCACHE_CONTROL_PRIVDB */
3588 #ifdef PCACHE_EXOP_QUERY_DELETE
3589 static struct berval pcache_exop_QUERY_DELETE = BER_BVC( PCACHE_EXOP_QUERY_DELETE );
3591 #define LDAP_TAG_EXOP_QUERY_DELETE_BASE ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 0)
3592 #define LDAP_TAG_EXOP_QUERY_DELETE_DN ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 1)
3593 #define LDAP_TAG_EXOP_QUERY_DELETE_UUID ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 2)
3596 ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
3597 requestName [0] LDAPOID,
3598 requestValue [1] OCTET STRING OPTIONAL }
3600 requestName ::= 1.3.6.1.4.1.4203.666.11.9.6.1
3602 requestValue ::= SEQUENCE { CHOICE {
3604 entryDN [1] LDAPDN },
3605 queryID [2] OCTET STRING (SIZE(16))
3606 -- constrained to UUID }
3608 * Either baseDN or entryDN must be present, to allow database selection.
3610 * 1. if baseDN and queryID are present, then the query corresponding
3611 * to queryID is deleted;
3612 * 2. if baseDN is present and queryID is absent, then all queries
3614 * 3. if entryDN is present and queryID is absent, then all queries
3615 * corresponding to the queryID values present in entryDN are deleted;
3616 * 4. if entryDN and queryID are present, then all queries
3617 * corresponding to the queryID values present in entryDN are deleted,
3618 * but only if the value of queryID is contained in the entry;
3620 * Currently, only 1, 3 and 4 are implemented. 2 can be obtained by either
3621 * recursively deleting the database (ldapdelete -r) with PRIVDB control,
3622 * or by removing the database files.
3624 ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
3625 COMPONENTS OF LDAPResult,
3626 responseName [10] LDAPOID OPTIONAL,
3627 responseValue [11] OCTET STRING OPTIONAL }
3629 * responseName and responseValue must be absent.
3633 * - on success, *tagp is either LDAP_TAG_EXOP_QUERY_DELETE_BASE
3634 * or LDAP_TAG_EXOP_QUERY_DELETE_DN.
3635 * - if ndn != NULL, it is set to the normalized DN in the request
3636 * corresponding to either the baseDN or the entryDN, according
3637 * to *tagp; memory is malloc'ed on the Operation's slab, and must
3638 * be freed by the caller.
3639 * - if uuid != NULL, it is set to point to the normalized UUID;
3640 * memory is malloc'ed on the Operation's slab, and must
3641 * be freed by the caller.
3644 pcache_parse_query_delete(
3648 struct berval *uuid,
3652 int rc = LDAP_SUCCESS;
3655 BerElementBuffer berbuf;
3656 BerElement *ber = (BerElement *)&berbuf;
3657 struct berval reqdata = BER_BVNULL;
3669 if ( in == NULL || in->bv_len == 0 ) {
3670 *text = "empty request data field in queryDelete exop";
3671 return LDAP_PROTOCOL_ERROR;
3674 ber_dupbv_x( &reqdata, in, ctx );
3676 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
3677 ber_init2( ber, &reqdata, 0 );
3679 tag = ber_scanf( ber, "{" /*}*/ );
3681 if ( tag == LBER_ERROR ) {
3682 Debug( LDAP_DEBUG_TRACE,
3683 "pcache_parse_query_delete: decoding error.\n",
3685 goto decoding_error;
3688 tag = ber_peek_tag( ber, &len );
3689 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_BASE
3690 || tag == LDAP_TAG_EXOP_QUERY_DELETE_DN )
3694 if ( ndn != NULL ) {
3697 tag = ber_scanf( ber, "m", &dn );
3698 if ( tag == LBER_ERROR ) {
3699 Debug( LDAP_DEBUG_TRACE,
3700 "pcache_parse_query_delete: DN parse failed.\n",
3702 goto decoding_error;
3705 rc = dnNormalize( 0, NULL, NULL, &dn, ndn, ctx );
3706 if ( rc != LDAP_SUCCESS ) {
3707 *text = "invalid DN in queryDelete exop request data";
3712 tag = ber_scanf( ber, "x" /* "m" */ );
3713 if ( tag == LBER_DEFAULT ) {
3714 goto decoding_error;
3718 tag = ber_peek_tag( ber, &len );
3721 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_UUID ) {
3722 if ( uuid != NULL ) {
3724 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
3726 tag = ber_scanf( ber, "m", &bv );
3727 if ( tag == LBER_ERROR ) {
3728 Debug( LDAP_DEBUG_TRACE,
3729 "pcache_parse_query_delete: UUID parse failed.\n",
3731 goto decoding_error;
3734 if ( bv.bv_len != 16 ) {
3735 Debug( LDAP_DEBUG_TRACE,
3736 "pcache_parse_query_delete: invalid UUID length %lu.\n",
3737 (unsigned long)bv.bv_len, 0, 0 );
3738 goto decoding_error;
3741 rc = lutil_uuidstr_from_normalized(
3742 bv.bv_val, bv.bv_len,
3743 uuidbuf, sizeof( uuidbuf ) );
3745 goto decoding_error;
3747 ber_str2bv( uuidbuf, rc, 1, uuid );
3751 tag = ber_skip_tag( ber, &len );
3752 if ( tag == LBER_DEFAULT ) {
3753 goto decoding_error;
3757 Debug( LDAP_DEBUG_TRACE,
3758 "pcache_parse_query_delete: invalid UUID length %lu.\n",
3759 (unsigned long)len, 0, 0 );
3760 goto decoding_error;
3764 tag = ber_peek_tag( ber, &len );
3767 if ( tag != LBER_DEFAULT || len != 0 ) {
3769 Debug( LDAP_DEBUG_TRACE,
3770 "pcache_parse_query_delete: decoding error\n",
3772 rc = LDAP_PROTOCOL_ERROR;
3773 *text = "queryDelete data decoding error";
3776 if ( ndn && !BER_BVISNULL( ndn ) ) {
3777 slap_sl_free( ndn->bv_val, ctx );
3781 if ( uuid && !BER_BVISNULL( uuid ) ) {
3782 slap_sl_free( uuid->bv_val, ctx );
3787 if ( !BER_BVISNULL( &reqdata ) ) {
3788 ber_memfree_x( reqdata.bv_val, ctx );
3795 pcache_exop_query_delete(
3799 BackendDB *bd = op->o_bd;
3801 struct berval uuid = BER_BVNULL,
3803 char buf[ SLAP_TEXT_BUFLEN ];
3805 ber_tag_t tag = LBER_DEFAULT;
3807 if ( LogTest( LDAP_DEBUG_STATS ) ) {
3811 rs->sr_err = pcache_parse_query_delete( op->ore_reqdata,
3812 &tag, &op->o_req_ndn, uuidp,
3813 &rs->sr_text, op->o_tmpmemctx );
3814 if ( rs->sr_err != LDAP_SUCCESS ) {
3818 if ( LogTest( LDAP_DEBUG_STATS ) ) {
3819 assert( !BER_BVISNULL( &op->o_req_ndn ) );
3820 len = snprintf( buf, sizeof( buf ), " dn=\"%s\"", op->o_req_ndn.bv_val );
3822 if ( !BER_BVISNULL( &uuid ) && len < sizeof( buf ) ) {
3823 snprintf( &buf[ len ], sizeof( buf ) - len, " queryId=\"%s\"", uuid.bv_val );
3826 Debug( LDAP_DEBUG_STATS, "%s QUERY DELETE%s\n",
3827 op->o_log_prefix, buf, 0 );
3829 op->o_req_dn = op->o_req_ndn;
3831 op->o_bd = select_backend( &op->o_req_ndn, 0 );
3832 rs->sr_err = backend_check_restrictions( op, rs,
3833 (struct berval *)&pcache_exop_QUERY_DELETE );
3834 if ( rs->sr_err != LDAP_SUCCESS ) {
3838 if ( op->o_bd->be_extended == NULL ) {
3839 send_ldap_error( op, rs, LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
3840 "backend does not support extended operations" );
3844 op->o_bd->be_extended( op, rs );
3847 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
3848 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
3849 BER_BVZERO( &op->o_req_ndn );
3850 BER_BVZERO( &op->o_req_dn );
3853 if ( !BER_BVISNULL( &uuid ) ) {
3854 op->o_tmpfree( uuid.bv_val, op->o_tmpmemctx );
3863 pcache_op_extended( Operation *op, SlapReply *rs )
3865 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
3866 cache_manager *cm = on->on_bi.bi_private;
3868 #ifdef PCACHE_CONTROL_PRIVDB
3869 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL ) {
3870 return pcache_op_privdb( op, rs );
3872 #endif /* PCACHE_CONTROL_PRIVDB */
3874 if ( bvmatch( &op->ore_reqoid, &pcache_exop_QUERY_DELETE ) ) {
3875 struct berval uuid = BER_BVNULL;
3876 ber_tag_t tag = LBER_DEFAULT;
3878 rs->sr_err = pcache_parse_query_delete( op->ore_reqdata,
3879 &tag, NULL, &uuid, &rs->sr_text, op->o_tmpmemctx );
3880 assert( rs->sr_err == LDAP_SUCCESS );
3882 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_DN ) {
3883 /* remove all queries related to the selected entry */
3884 rs->sr_err = pcache_remove_entry_queries_from_cache( op,
3885 cm, &op->o_req_ndn, &uuid );
3887 } else if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_BASE ) {
3888 if ( !BER_BVISNULL( &uuid ) ) {
3889 /* remove the selected query */
3890 rs->sr_err = pcache_remove_query_from_cache( op,
3894 /* TODO: remove all queries */
3895 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
3896 rs->sr_text = "deletion of all queries not implemented";
3900 op->o_tmpfree( uuid.bv_val, op->o_tmpmemctx );
3905 #endif /* PCACHE_EXOP_QUERY_DELETE */
3907 static slap_overinst pcache;
3909 static char *obsolete_names[] = {
3914 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
3916 #endif /* SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC */
3921 struct berval debugbv = BER_BVC("pcache");
3923 code = slap_loglevel_get( &debugbv, &pcache_debug );
3928 #ifdef PCACHE_CONTROL_PRIVDB
3929 code = register_supported_control( PCACHE_CONTROL_PRIVDB,
3930 SLAP_CTRL_BIND|SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE, extops,
3931 parse_privdb_ctrl, &privDB_cid );
3932 if ( code != LDAP_SUCCESS ) {
3933 Debug( LDAP_DEBUG_ANY,
3934 "pcache_initialize: failed to register control %s (%d)\n",
3935 PCACHE_CONTROL_PRIVDB, code, 0 );
3938 #endif /* PCACHE_CONTROL_PRIVDB */
3940 #ifdef PCACHE_EXOP_QUERY_DELETE
3941 code = load_extop2( (struct berval *)&pcache_exop_QUERY_DELETE,
3942 SLAP_EXOP_WRITES|SLAP_EXOP_HIDE, pcache_exop_query_delete,
3944 if ( code != LDAP_SUCCESS ) {
3945 Debug( LDAP_DEBUG_ANY,
3946 "pcache_initialize: unable to register queryDelete exop: %d.\n",
3950 #endif /* PCACHE_EXOP_QUERY_DELETE */
3952 for ( i = 0; as[i].desc != NULL; i++ ) {
3953 code = register_at( as[i].desc, as[i].adp, 0 );
3955 Debug( LDAP_DEBUG_ANY,
3956 "pcache_initialize: register_at #%d failed\n", i, 0, 0 );
3959 (*as[i].adp)->ad_type->sat_flags |= SLAP_AT_HIDE;
3962 pcache.on_bi.bi_type = "pcache";
3963 pcache.on_bi.bi_obsolete_names = obsolete_names;
3964 pcache.on_bi.bi_db_init = pcache_db_init;
3965 pcache.on_bi.bi_db_config = pcache_db_config;
3966 pcache.on_bi.bi_db_open = pcache_db_open;
3967 pcache.on_bi.bi_db_close = pcache_db_close;
3968 pcache.on_bi.bi_db_destroy = pcache_db_destroy;
3970 pcache.on_bi.bi_op_search = pcache_op_search;
3971 #ifdef PCACHE_CONTROL_PRIVDB
3972 pcache.on_bi.bi_op_bind = pcache_op_privdb;
3973 pcache.on_bi.bi_op_compare = pcache_op_privdb;
3974 pcache.on_bi.bi_op_modrdn = pcache_op_privdb;
3975 pcache.on_bi.bi_op_modify = pcache_op_privdb;
3976 pcache.on_bi.bi_op_add = pcache_op_privdb;
3977 pcache.on_bi.bi_op_delete = pcache_op_privdb;
3978 #endif /* PCACHE_CONTROL_PRIVDB */
3979 #ifdef PCACHE_EXOP_QUERY_DELETE
3980 pcache.on_bi.bi_extended = pcache_op_extended;
3981 #elif defined( PCACHE_CONTROL_PRIVDB )
3982 pcache.on_bi.bi_extended = pcache_op_privdb;
3985 pcache.on_bi.bi_chk_controls = pcache_chk_controls;
3987 pcache.on_bi.bi_cf_ocs = pcocs;
3989 code = config_register_schema( pccfg, pcocs );
3990 if ( code ) return code;
3992 return overlay_register( &pcache );
3995 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
3996 int init_module(int argc, char *argv[]) {
3997 return pcache_initialize();
4001 #endif /* defined(SLAPD_OVER_PROXYCACHE) */