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 current value in each filter */
592 static int pcache_filter_cmp( Filter *f1, Filter *f2 )
594 int rc, weight1, weight2;
596 switch( f1->f_choice ) {
597 case LDAP_FILTER_PRESENT:
600 case LDAP_FILTER_EQUALITY:
608 switch( f2->f_choice ) {
609 case LDAP_FILTER_PRESENT:
612 case LDAP_FILTER_EQUALITY:
620 rc = weight1 - weight2;
626 rc = lex_bvcmp( &f1->f_av_value, &f2->f_av_value );
629 if ( f1->f_choice == LDAP_FILTER_SUBSTRINGS ) {
631 if ( !BER_BVISNULL( &f1->f_sub_initial )) {
632 if ( !BER_BVISNULL( &f2->f_sub_initial )) {
633 rc = lex_bvcmp( &f1->f_sub_initial,
634 &f2->f_sub_initial );
638 } else if ( !BER_BVISNULL( &f2->f_sub_initial )) {
642 if ( f1->f_sub_any ) {
643 if ( f2->f_sub_any ) {
644 rc = lex_bvcmp( f1->f_sub_any,
649 } else if ( f2->f_sub_any ) {
653 if ( !BER_BVISNULL( &f1->f_sub_final )) {
654 if ( !BER_BVISNULL( &f2->f_sub_final )) {
655 rc = lex_bvcmp( &f1->f_sub_final,
660 } else if ( !BER_BVISNULL( &f2->f_sub_final )) {
664 rc = lex_bvcmp( &f1->f_mr_value,
678 while ( f1->f_choice == LDAP_FILTER_AND || f1->f_choice == LDAP_FILTER_OR )
680 while ( f2->f_choice == LDAP_FILTER_AND || f2->f_choice == LDAP_FILTER_OR )
682 rc = pcache_filter_cmp( f1, f2 );
690 /* compare filters in each query */
691 static int pcache_query_cmp( const void *v1, const void *v2 )
693 const CachedQuery *q1 = v1, *q2 =v2;
694 return pcache_filter_cmp( q1->first, q2->first );
697 /* add query on top of LRU list */
699 add_query_on_top (query_manager* qm, CachedQuery* qc)
701 CachedQuery* top = qm->lru_top;
712 Debug( pcache_debug, "Base of added query = %s\n",
713 qc->qbase->base.bv_val, 0, 0 );
716 /* remove_query from LRU list */
719 remove_query (query_manager* qm, CachedQuery* qc)
742 qc->lru_up = qc->lru_down = NULL;
745 /* find and remove string2 from string1
746 * from start if position = 1,
747 * from end if position = 3,
748 * from anywhere if position = 2
749 * string1 is overwritten if position = 2.
753 find_and_remove(struct berval* ber1, struct berval* ber2, int position)
764 if ( ber1->bv_len >= ber2->bv_len && !memcmp( ber1->bv_val,
765 ber2->bv_val, ber2->bv_len )) {
767 ber1->bv_val += ber2->bv_len;
768 ber1->bv_len -= ber2->bv_len;
773 ber1->bv_val[ber1->bv_len] = '\0';
774 temp = strstr( ber1->bv_val, ber2->bv_val );
776 strcpy( temp, temp+ber2->bv_len );
777 ber1->bv_len -= ber2->bv_len;
783 if ( ber1->bv_len >= ber2->bv_len &&
784 !memcmp( ber1->bv_val+ber1->bv_len-ber2->bv_len, ber2->bv_val,
787 ber1->bv_len -= ber2->bv_len;
795 static struct berval*
796 merge_init_final(Operation *op, struct berval* init, struct berval* any,
797 struct berval* final)
799 struct berval* merged, *temp;
800 int i, any_count, count;
802 for (any_count=0; any && any[any_count].bv_val; any_count++)
812 merged = (struct berval*)op->o_tmpalloc( (count+1)*sizeof(struct berval),
817 ber_dupbv_x( temp, init, op->o_tmpmemctx );
821 for (i=0; i<any_count; i++) {
822 ber_dupbv_x( temp, any, op->o_tmpmemctx );
827 ber_dupbv_x( temp, final, op->o_tmpmemctx );
834 /* Each element in stored must be found in incoming. Incoming is overwritten.
837 strings_containment(struct berval* stored, struct berval* incoming)
839 struct berval* element;
843 for ( element=stored; element->bv_val != NULL; element++ ) {
844 for (j = k; incoming[j].bv_val != NULL; j++) {
845 if (find_and_remove(&(incoming[j]), element, 2)) {
862 substr_containment_substr(Operation *op, Filter* stored, Filter* incoming)
866 struct berval init_incoming;
867 struct berval final_incoming;
868 struct berval *remaining_incoming = NULL;
870 if ((!(incoming->f_sub_initial.bv_val) && (stored->f_sub_initial.bv_val))
871 || (!(incoming->f_sub_final.bv_val) && (stored->f_sub_final.bv_val)))
874 init_incoming = incoming->f_sub_initial;
875 final_incoming = incoming->f_sub_final;
877 if (find_and_remove(&init_incoming,
878 &(stored->f_sub_initial), 1) && find_and_remove(&final_incoming,
879 &(stored->f_sub_final), 3))
881 if (stored->f_sub_any == NULL) {
885 remaining_incoming = merge_init_final(op, &init_incoming,
886 incoming->f_sub_any, &final_incoming);
887 rc = strings_containment(stored->f_sub_any, remaining_incoming);
888 ber_bvarray_free_x( remaining_incoming, op->o_tmpmemctx );
895 substr_containment_equality(Operation *op, Filter* stored, Filter* incoming)
897 struct berval incoming_val[2];
900 incoming_val[1] = incoming->f_av_value;
902 if (find_and_remove(incoming_val+1,
903 &(stored->f_sub_initial), 1) && find_and_remove(incoming_val+1,
904 &(stored->f_sub_final), 3)) {
905 if (stored->f_sub_any == NULL){
909 ber_dupbv_x( incoming_val, incoming_val+1, op->o_tmpmemctx );
910 BER_BVZERO( incoming_val+1 );
911 rc = strings_containment(stored->f_sub_any, incoming_val);
912 op->o_tmpfree( incoming_val[0].bv_val, op->o_tmpmemctx );
919 filter_first( Filter *f )
921 while ( f->f_choice == LDAP_FILTER_OR || f->f_choice == LDAP_FILTER_AND )
928 find_filter( Operation *op, Avlnode *root, Filter *inputf, Filter *first )
932 MatchingRule* mrule = NULL;
933 int res=0, eqpass= 0;
941 /* substring matches sort to the end, and we just have to
942 * walk the entire list.
944 if ( first->f_choice == LDAP_FILTER_SUBSTRINGS ) {
945 ptr = tavl_end( root, 1 );
948 ptr = tavl_find3( root, &cq, pcache_query_cmp, &ret );
949 dir = (first->f_choice == LDAP_FILTER_GE) ? TAVL_DIR_LEFT :
958 /* an incoming substr query can only be satisfied by a cached
961 if ( first->f_choice == LDAP_FILTER_SUBSTRINGS &&
962 qc->first->f_choice != LDAP_FILTER_SUBSTRINGS )
965 /* an incoming eq query can be satisfied by a cached eq or substr
968 if ( first->f_choice == LDAP_FILTER_EQUALITY ) {
970 if ( qc->first->f_choice != LDAP_FILTER_EQUALITY ) {
971 nextpass: eqpass = 1;
972 ptr = tavl_end( root, 1 );
977 if ( qc->first->f_choice != LDAP_FILTER_SUBSTRINGS )
983 switch (fs->f_choice) {
984 case LDAP_FILTER_EQUALITY:
985 if (fi->f_choice == LDAP_FILTER_EQUALITY)
986 mrule = fs->f_ava->aa_desc->ad_type->sat_equality;
992 mrule = fs->f_ava->aa_desc->ad_type->sat_ordering;
999 rc = value_match(&ret, fs->f_ava->aa_desc, mrule,
1000 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
1001 &(fi->f_ava->aa_value),
1002 &(fs->f_ava->aa_value), &text);
1003 if (rc != LDAP_SUCCESS) {
1006 if ( fi==first && fi->f_choice==LDAP_FILTER_EQUALITY && ret )
1009 switch (fs->f_choice) {
1010 case LDAP_FILTER_OR:
1011 case LDAP_FILTER_AND:
1016 case LDAP_FILTER_SUBSTRINGS:
1017 /* check if the equality query can be
1018 * answered with cached substring query */
1019 if ((fi->f_choice == LDAP_FILTER_EQUALITY)
1020 && substr_containment_equality( op,
1023 /* check if the substring query can be
1024 * answered with cached substring query */
1025 if ((fi->f_choice ==LDAP_FILTER_SUBSTRINGS
1026 ) && substr_containment_substr( op,
1032 case LDAP_FILTER_PRESENT:
1037 case LDAP_FILTER_EQUALITY:
1043 case LDAP_FILTER_GE:
1044 if (mrule && ret >= 0)
1049 case LDAP_FILTER_LE:
1050 if (mrule && ret <= 0)
1055 case LDAP_FILTER_NOT:
1061 } while((res) && (fi != NULL) && (fs != NULL));
1065 ptr = tavl_next( ptr, dir );
1070 /* check whether query is contained in any of
1071 * the cached queries in template
1073 static CachedQuery *
1074 query_containment(Operation *op, query_manager *qm,
1076 QueryTemplate *templa)
1079 int depth = 0, tscope;
1080 Qbase qbase, *qbptr = NULL;
1083 if (query->filter != NULL) {
1086 Debug( pcache_debug, "Lock QC index = %p\n",
1087 (void *) templa, 0, 0 );
1088 qbase.base = query->base;
1090 first = filter_first( query->filter );
1092 ldap_pvt_thread_rdwr_rlock(&templa->t_rwlock);
1095 qbptr = avl_find( templa->qbase, &qbase, pcache_dn_cmp );
1097 tscope = query->scope;
1098 /* Find a matching scope:
1099 * match at depth 0 OK
1102 * subord at depth > 0 OK
1103 * subtree at any depth OK
1105 * subtree or subord at any depth OK
1107 * subtree or subord at any depth OK
1109 * subord at depth > 0 OK
1110 * subtree at any depth OK
1112 for ( tscope = 0 ; tscope <= LDAP_SCOPE_CHILDREN; tscope++ ) {
1113 switch ( query->scope ) {
1114 case LDAP_SCOPE_BASE:
1115 if ( tscope == LDAP_SCOPE_BASE && depth ) continue;
1116 if ( tscope == LDAP_SCOPE_ONE && depth != 1) continue;
1117 if ( tscope == LDAP_SCOPE_CHILDREN && !depth ) continue;
1119 case LDAP_SCOPE_ONE:
1120 if ( tscope == LDAP_SCOPE_BASE )
1121 tscope = LDAP_SCOPE_ONE;
1122 if ( tscope == LDAP_SCOPE_ONE && depth ) continue;
1123 if ( !depth ) break;
1124 if ( tscope < LDAP_SCOPE_SUBTREE )
1125 tscope = LDAP_SCOPE_SUBTREE;
1127 case LDAP_SCOPE_SUBTREE:
1128 if ( tscope < LDAP_SCOPE_SUBTREE )
1129 tscope = LDAP_SCOPE_SUBTREE;
1130 if ( tscope == LDAP_SCOPE_CHILDREN && !depth ) continue;
1132 case LDAP_SCOPE_CHILDREN:
1133 if ( tscope < LDAP_SCOPE_SUBTREE )
1134 tscope = LDAP_SCOPE_SUBTREE;
1137 if ( !qbptr->scopes[tscope] ) continue;
1140 qc = find_filter( op, qbptr->scopes[tscope],
1141 query->filter, first );
1143 if ( qc->q_sizelimit ) {
1144 ldap_pvt_thread_rdwr_runlock(&templa->t_rwlock);
1147 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1148 if (qm->lru_top != qc) {
1149 remove_query(qm, qc);
1150 add_query_on_top(qm, qc);
1152 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1157 if ( be_issuffix( op->o_bd, &qbase.base ))
1160 dnParent( &qbase.base, &pdn );
1165 Debug( pcache_debug,
1166 "Not answerable: Unlock QC index=%p\n",
1167 (void *) templa, 0, 0 );
1168 ldap_pvt_thread_rdwr_runlock(&templa->t_rwlock);
1174 free_query (CachedQuery* qc)
1176 free(qc->q_uuid.bv_val);
1177 filter_free(qc->filter);
1178 ldap_pvt_thread_rdwr_destroy( &qc->rwlock );
1179 memset(qc, 0, sizeof(*qc));
1184 /* Add query to query cache, the returned Query is locked for writing */
1185 static CachedQuery *
1190 QueryTemplate *templ,
1191 pc_caching_reason_t why,
1194 CachedQuery* new_cached_query = (CachedQuery*) ch_malloc(sizeof(CachedQuery));
1200 new_cached_query->qtemp = templ;
1201 BER_BVZERO( &new_cached_query->q_uuid );
1202 new_cached_query->q_sizelimit = 0;
1210 ttl = templ->negttl;
1214 ttl = templ->limitttl;
1221 new_cached_query->expiry_time = slap_get_time() + ttl;
1222 new_cached_query->lru_up = NULL;
1223 new_cached_query->lru_down = NULL;
1224 Debug( pcache_debug, "Added query expires at %ld (%s)\n",
1225 (long) new_cached_query->expiry_time,
1226 pc_caching_reason_str[ why ], 0 );
1228 new_cached_query->scope = query->scope;
1229 new_cached_query->filter = query->filter;
1230 new_cached_query->first = first = filter_first( query->filter );
1232 ldap_pvt_thread_rdwr_init(&new_cached_query->rwlock);
1234 ldap_pvt_thread_rdwr_wlock(&new_cached_query->rwlock);
1236 qb.base = query->base;
1238 /* Adding a query */
1239 Debug( pcache_debug, "Lock AQ index = %p\n",
1240 (void *) templ, 0, 0 );
1241 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
1242 qbase = avl_find( templ->qbase, &qb, pcache_dn_cmp );
1244 qbase = ch_calloc( 1, sizeof(Qbase) + qb.base.bv_len + 1 );
1245 qbase->base.bv_len = qb.base.bv_len;
1246 qbase->base.bv_val = (char *)(qbase+1);
1247 memcpy( qbase->base.bv_val, qb.base.bv_val, qb.base.bv_len );
1248 qbase->base.bv_val[qbase->base.bv_len] = '\0';
1249 avl_insert( &templ->qbase, qbase, pcache_dn_cmp, avl_dup_error );
1251 new_cached_query->next = templ->query;
1252 new_cached_query->prev = NULL;
1253 new_cached_query->qbase = qbase;
1254 rc = tavl_insert( &qbase->scopes[query->scope], new_cached_query,
1255 pcache_query_cmp, avl_dup_error );
1258 if (templ->query == NULL)
1259 templ->query_last = new_cached_query;
1261 templ->query->prev = new_cached_query;
1262 templ->query = new_cached_query;
1263 templ->no_of_queries++;
1265 ch_free( new_cached_query );
1266 new_cached_query = find_filter( op, qbase->scopes[query->scope],
1267 query->filter, first );
1268 filter_free( query->filter );
1269 query->filter = NULL;
1271 Debug( pcache_debug, "TEMPLATE %p QUERIES++ %d\n",
1272 (void *) templ, templ->no_of_queries, 0 );
1274 Debug( pcache_debug, "Unlock AQ index = %p \n",
1275 (void *) templ, 0, 0 );
1276 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
1278 /* Adding on top of LRU list */
1280 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1281 add_query_on_top(qm, new_cached_query);
1282 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1284 return rc == 0 ? new_cached_query : NULL;
1288 remove_from_template (CachedQuery* qc, QueryTemplate* template)
1290 if (!qc->prev && !qc->next) {
1291 template->query_last = template->query = NULL;
1292 } else if (qc->prev == NULL) {
1293 qc->next->prev = NULL;
1294 template->query = qc->next;
1295 } else if (qc->next == NULL) {
1296 qc->prev->next = NULL;
1297 template->query_last = qc->prev;
1299 qc->next->prev = qc->prev;
1300 qc->prev->next = qc->next;
1302 tavl_delete( &qc->qbase->scopes[qc->scope], qc, pcache_query_cmp );
1303 qc->qbase->queries--;
1304 if ( qc->qbase->queries == 0 ) {
1305 avl_delete( &template->qbase, qc->qbase, pcache_dn_cmp );
1306 ch_free( qc->qbase );
1310 template->no_of_queries--;
1313 /* remove bottom query of LRU list from the query cache */
1315 * NOTE: slight change in functionality.
1317 * - if result->bv_val is NULL, the query at the bottom of the LRU
1319 * - otherwise, the query whose UUID is *result is removed
1320 * - if not found, result->bv_val is zeroed
1323 cache_replacement(query_manager* qm, struct berval *result)
1325 CachedQuery* bottom;
1326 QueryTemplate *temp;
1328 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1329 if ( BER_BVISNULL( result ) ) {
1330 bottom = qm->lru_bottom;
1333 Debug ( pcache_debug,
1334 "Cache replacement invoked without "
1335 "any query in LRU list\n", 0, 0, 0 );
1336 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1341 for ( bottom = qm->lru_bottom;
1343 bottom = bottom->lru_up )
1345 if ( bvmatch( result, &bottom->q_uuid ) ) {
1351 Debug ( pcache_debug,
1352 "Could not find query with uuid=\"%s\""
1353 "in LRU list\n", result->bv_val, 0, 0 );
1354 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1355 BER_BVZERO( result );
1360 temp = bottom->qtemp;
1361 remove_query(qm, bottom);
1362 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1364 *result = bottom->q_uuid;
1365 BER_BVZERO( &bottom->q_uuid );
1367 Debug( pcache_debug, "Lock CR index = %p\n", (void *) temp, 0, 0 );
1368 ldap_pvt_thread_rdwr_wlock(&temp->t_rwlock);
1369 remove_from_template(bottom, temp);
1370 Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
1371 (void *) temp, temp->no_of_queries, 0 );
1372 Debug( pcache_debug, "Unlock CR index = %p\n", (void *) temp, 0, 0 );
1373 ldap_pvt_thread_rdwr_wunlock(&temp->t_rwlock);
1378 struct query_info *next;
1390 struct query_info *qi;
1393 if ( rs->sr_type != REP_SEARCH ) return 0;
1395 attr = attr_find( rs->sr_entry->e_attrs, ad_queryId );
1396 if ( attr == NULL ) return 0;
1398 count = attr->a_numvals;
1399 assert( count > 0 );
1400 qi = op->o_tmpalloc( sizeof( struct query_info ), op->o_tmpmemctx );
1401 qi->next = op->o_callback->sc_private;
1402 op->o_callback->sc_private = qi;
1403 ber_dupbv_x( &qi->xdn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1404 qi->del = ( count == 1 );
1413 struct berval *query_uuid )
1415 struct query_info *qi, *qnext;
1416 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(queryId=)" ) ];
1417 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
1418 Filter filter = {LDAP_FILTER_EQUALITY};
1419 SlapReply sreply = {REP_RESULT};
1420 slap_callback cb = { NULL, remove_func, NULL, NULL };
1423 sreply.sr_entry = NULL;
1424 sreply.sr_nentries = 0;
1425 op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
1426 "(%s=%s)", ad_queryId->ad_cname.bv_val, query_uuid->bv_val);
1427 filter.f_ava = &ava;
1428 filter.f_av_desc = ad_queryId;
1429 filter.f_av_value = *query_uuid;
1431 op->o_tag = LDAP_REQ_SEARCH;
1432 op->o_protocol = LDAP_VERSION3;
1433 op->o_callback = &cb;
1434 op->o_time = slap_get_time();
1435 op->o_do_not_cache = 1;
1437 op->o_req_dn = op->o_bd->be_suffix[0];
1438 op->o_req_ndn = op->o_bd->be_nsuffix[0];
1439 op->ors_scope = LDAP_SCOPE_SUBTREE;
1440 op->ors_deref = LDAP_DEREF_NEVER;
1441 op->ors_slimit = SLAP_NO_LIMIT;
1442 op->ors_tlimit = SLAP_NO_LIMIT;
1443 op->ors_filter = &filter;
1444 op->ors_filterstr.bv_val = filter_str;
1445 op->ors_filterstr.bv_len = strlen(filter_str);
1446 op->ors_attrs = NULL;
1447 op->ors_attrsonly = 0;
1449 op->o_bd->be_search( op, &sreply );
1451 for ( qi=cb.sc_private; qi; qi=qnext ) {
1454 op->o_req_dn = qi->xdn;
1455 op->o_req_ndn = qi->xdn;
1458 Debug( pcache_debug, "DELETING ENTRY TEMPLATE=%s\n",
1459 query_uuid->bv_val, 0, 0 );
1461 op->o_tag = LDAP_REQ_DELETE;
1463 if (op->o_bd->be_delete(op, &sreply) == LDAP_SUCCESS) {
1469 struct berval vals[2];
1471 vals[0] = *query_uuid;
1472 vals[1].bv_val = NULL;
1474 mod.sml_op = LDAP_MOD_DELETE;
1476 mod.sml_desc = ad_queryId;
1477 mod.sml_type = ad_queryId->ad_cname;
1478 mod.sml_values = vals;
1479 mod.sml_nvalues = NULL;
1480 mod.sml_numvals = 1;
1481 mod.sml_next = NULL;
1482 Debug( pcache_debug,
1483 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
1484 query_uuid->bv_val, 0, 0 );
1486 op->orm_modlist = &mod;
1488 op->o_bd->be_modify( op, &sreply );
1490 op->o_tmpfree( qi->xdn.bv_val, op->o_tmpmemctx );
1491 op->o_tmpfree( qi, op->o_tmpmemctx );
1498 AttributeName* attrs,
1507 struct berval *fstr,
1508 AttributeName** filter_attrs,
1510 int* filter_got_oc )
1512 AttributeDescription *ad;
1515 switch ( f->f_choice ) {
1516 case LDAP_FILTER_EQUALITY:
1518 len = STRLENOF( "(=)" ) + ad->ad_cname.bv_len;
1519 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=)", ad->ad_cname.bv_val );
1520 assert( ret == len );
1521 fstr->bv_len += len;
1524 case LDAP_FILTER_GE:
1526 len = STRLENOF( "(>=)" ) + ad->ad_cname.bv_len;
1527 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s>=)", ad->ad_cname.bv_val);
1528 assert( ret == len );
1529 fstr->bv_len += len;
1532 case LDAP_FILTER_LE:
1534 len = STRLENOF( "(<=)" ) + ad->ad_cname.bv_len;
1535 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s<=)", ad->ad_cname.bv_val);
1536 assert( ret == len );
1537 fstr->bv_len += len;
1540 case LDAP_FILTER_APPROX:
1542 len = STRLENOF( "(~=)" ) + ad->ad_cname.bv_len;
1543 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s~=)", ad->ad_cname.bv_val);
1544 assert( ret == len );
1545 fstr->bv_len += len;
1548 case LDAP_FILTER_SUBSTRINGS:
1550 len = STRLENOF( "(=)" ) + ad->ad_cname.bv_len;
1551 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=)", ad->ad_cname.bv_val );
1552 assert( ret == len );
1553 fstr->bv_len += len;
1556 case LDAP_FILTER_PRESENT:
1558 len = STRLENOF( "(=*)" ) + ad->ad_cname.bv_len;
1559 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=*)", ad->ad_cname.bv_val );
1560 assert( ret == len );
1561 fstr->bv_len += len;
1564 case LDAP_FILTER_AND:
1565 case LDAP_FILTER_OR:
1566 case LDAP_FILTER_NOT: {
1568 fstr->bv_val[fstr->bv_len++] = '(';
1569 switch ( f->f_choice ) {
1570 case LDAP_FILTER_AND:
1571 fstr->bv_val[fstr->bv_len] = '&';
1573 case LDAP_FILTER_OR:
1574 fstr->bv_val[fstr->bv_len] = '|';
1576 case LDAP_FILTER_NOT:
1577 fstr->bv_val[fstr->bv_len] = '!';
1582 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1583 rc = filter2template( op, f, fstr, filter_attrs, filter_cnt,
1587 fstr->bv_val[fstr->bv_len++] = ')';
1588 fstr->bv_val[fstr->bv_len] = '\0';
1594 /* a filter should at least have room for "()",
1595 * an "=" and for a 1-char attr */
1596 strcpy( fstr->bv_val, "(?=)" );
1597 fstr->bv_len += STRLENOF("(?=)");
1601 if ( filter_attrs != NULL ) {
1602 *filter_attrs = (AttributeName *)op->o_tmprealloc(*filter_attrs,
1603 (*filter_cnt + 2)*sizeof(AttributeName), op->o_tmpmemctx);
1605 (*filter_attrs)[*filter_cnt].an_desc = ad;
1606 (*filter_attrs)[*filter_cnt].an_name = ad->ad_cname;
1607 (*filter_attrs)[*filter_cnt].an_oc = NULL;
1608 (*filter_attrs)[*filter_cnt].an_oc_exclude = 0;
1609 BER_BVZERO( &(*filter_attrs)[*filter_cnt+1].an_name );
1611 if ( ad == slap_schema.si_ad_objectClass )
1618 struct search_info {
1621 QueryTemplate *qtemp;
1622 AttributeName* save_attrs; /* original attributes, saved for response */
1623 int swap_saved_attrs;
1628 int slimit_exceeded;
1629 pc_caching_reason_t caching_reason;
1634 remove_query_and_data(
1638 struct berval *uuid )
1640 query_manager* qm = cm->qm;
1642 qm->crfunc( qm, uuid );
1643 if ( !BER_BVISNULL( uuid ) ) {
1646 Debug( pcache_debug,
1647 "Removing query UUID %s\n",
1648 uuid->bv_val, 0, 0 );
1649 return_val = remove_query_data( op, rs, uuid );
1650 Debug( pcache_debug,
1651 "QUERY REMOVED, SIZE=%d\n",
1653 ldap_pvt_thread_mutex_lock( &cm->cache_mutex );
1654 cm->cur_entries -= return_val;
1655 cm->num_cached_queries--;
1656 Debug( pcache_debug,
1657 "STORED QUERIES = %lu\n",
1658 cm->num_cached_queries, 0, 0 );
1659 ldap_pvt_thread_mutex_unlock( &cm->cache_mutex );
1660 Debug( pcache_debug,
1661 "QUERY REMOVED, CACHE ="
1663 cm->cur_entries, 0, 0 );
1668 * Callback used to fetch queryId values based on entryUUID;
1669 * used by pcache_remove_entries_from_cache()
1672 fetch_queryId_cb( Operation *op, SlapReply *rs )
1676 /* only care about searchEntry responses */
1677 if ( rs->sr_type != REP_SEARCH ) {
1681 /* allow only one response per entryUUID */
1682 if ( op->o_callback->sc_private != NULL ) {
1688 /* copy all queryId values into callback's private data */
1689 a = attr_find( rs->sr_entry->e_attrs, ad_queryId );
1691 BerVarray vals = NULL;
1693 ber_bvarray_dup_x( &vals, a->a_nvals, op->o_tmpmemctx );
1694 op->o_callback->sc_private = (void *)vals;
1698 /* clear entry if required */
1699 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
1700 entry_free( rs->sr_entry );
1701 rs->sr_entry = NULL;
1702 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
1709 * Call that allows to remove a set of entries from the cache,
1710 * by forcing the removal of all the related queries.
1713 pcache_remove_entries_from_cache(
1716 BerVarray entryUUIDs )
1718 Connection conn = { 0 };
1719 OperationBuffer opbuf;
1721 slap_callback sc = { 0 };
1722 SlapReply rs = { REP_RESULT };
1724 char filtbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(entryUUID=)" ) ];
1725 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
1726 AttributeName attrs[ 2 ] = {{{ 0 }}};
1730 void *thrctx = ldap_pvt_thread_pool_context();
1732 connection_fake_init( &conn, &opbuf, thrctx );
1740 memset( &op->oq_search, 0, sizeof( op->oq_search ) );
1741 op->ors_scope = LDAP_SCOPE_SUBTREE;
1742 op->ors_deref = LDAP_DEREF_NEVER;
1743 f.f_choice = LDAP_FILTER_EQUALITY;
1745 ava.aa_desc = slap_schema.si_ad_entryUUID;
1746 op->ors_filter = &f;
1748 op->ors_tlimit = SLAP_NO_LIMIT;
1749 attrs[ 0 ].an_desc = ad_queryId;
1750 attrs[ 0 ].an_name = ad_queryId->ad_cname;
1751 op->ors_attrs = attrs;
1752 op->ors_attrsonly = 0;
1754 op->o_req_dn = cm->db.be_suffix[ 0 ];
1755 op->o_req_ndn = cm->db.be_nsuffix[ 0 ];
1757 op->o_tag = LDAP_REQ_SEARCH;
1758 op->o_protocol = LDAP_VERSION3;
1759 op->o_managedsait = SLAP_CONTROL_CRITICAL;
1761 op->o_dn = op->o_bd->be_rootdn;
1762 op->o_ndn = op->o_bd->be_rootndn;
1763 sc.sc_response = fetch_queryId_cb;
1764 op->o_callback = ≻
1766 for ( s = 0; !BER_BVISNULL( &entryUUIDs[ s ] ); s++ ) {
1767 BerVarray vals = NULL;
1769 op->ors_filterstr.bv_len = snprintf( filtbuf, sizeof( filtbuf ),
1770 "(entryUUID=%s)", entryUUIDs[ s ].bv_val );
1771 op->ors_filterstr.bv_val = filtbuf;
1772 ava.aa_value = entryUUIDs[ s ];
1774 rc = op->o_bd->be_search( op, &rs );
1775 if ( rc != LDAP_SUCCESS ) {
1779 vals = (BerVarray)op->o_callback->sc_private;
1780 if ( vals != NULL ) {
1783 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1784 struct berval val = vals[ i ];
1786 remove_query_and_data( op, &rs, cm, &val );
1788 if ( !BER_BVISNULL( &val ) && val.bv_val != vals[ i ].bv_val ) {
1789 ch_free( val.bv_val );
1793 ber_bvarray_free_x( vals, op->o_tmpmemctx );
1794 op->o_callback->sc_private = NULL;
1802 * Call that allows to remove a query from the cache.
1805 pcache_remove_query_from_cache(
1808 struct berval *queryid )
1810 Operation op2 = *op;
1811 SlapReply rs2 = { 0 };
1815 /* remove the selected query */
1816 remove_query_and_data( &op2, &rs2, cm, queryid );
1818 return LDAP_SUCCESS;
1822 * Call that allows to remove a set of queries related to an entry
1823 * from the cache; if queryid is not null, the entry must belong to
1824 * the query indicated by queryid.
1827 pcache_remove_entry_queries_from_cache(
1831 struct berval *queryid )
1833 Connection conn = { 0 };
1834 OperationBuffer opbuf;
1836 slap_callback sc = { 0 };
1837 SlapReply rs = { REP_RESULT };
1839 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(queryId=)" ) ];
1840 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
1841 AttributeName attrs[ 2 ] = {{{ 0 }}};
1844 BerVarray vals = NULL;
1847 void *thrctx = ldap_pvt_thread_pool_context();
1849 connection_fake_init( &conn, &opbuf, thrctx );
1857 memset( &op->oq_search, 0, sizeof( op->oq_search ) );
1858 op->ors_scope = LDAP_SCOPE_BASE;
1859 op->ors_deref = LDAP_DEREF_NEVER;
1860 if ( queryid == NULL || BER_BVISNULL( queryid ) ) {
1861 BER_BVSTR( &op->ors_filterstr, "(objectClass=*)" );
1862 f.f_choice = LDAP_FILTER_PRESENT;
1863 f.f_desc = slap_schema.si_ad_objectClass;
1866 op->ors_filterstr.bv_len = snprintf( filter_str,
1867 sizeof( filter_str ), "(%s=%s)",
1868 ad_queryId->ad_cname.bv_val, queryid->bv_val );
1869 f.f_choice = LDAP_FILTER_EQUALITY;
1871 f.f_av_desc = ad_queryId;
1872 f.f_av_value = *queryid;
1874 op->ors_filter = &f;
1876 op->ors_tlimit = SLAP_NO_LIMIT;
1877 attrs[ 0 ].an_desc = ad_queryId;
1878 attrs[ 0 ].an_name = ad_queryId->ad_cname;
1879 op->ors_attrs = attrs;
1880 op->ors_attrsonly = 0;
1882 op->o_req_dn = *ndn;
1883 op->o_req_ndn = *ndn;
1885 op->o_tag = LDAP_REQ_SEARCH;
1886 op->o_protocol = LDAP_VERSION3;
1887 op->o_managedsait = SLAP_CONTROL_CRITICAL;
1889 op->o_dn = op->o_bd->be_rootdn;
1890 op->o_ndn = op->o_bd->be_rootndn;
1891 sc.sc_response = fetch_queryId_cb;
1892 op->o_callback = ≻
1894 rc = op->o_bd->be_search( op, &rs );
1895 if ( rc != LDAP_SUCCESS ) {
1899 vals = (BerVarray)op->o_callback->sc_private;
1900 if ( vals != NULL ) {
1903 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1904 struct berval val = vals[ i ];
1906 remove_query_and_data( op, &rs, cm, &val );
1908 if ( !BER_BVISNULL( &val ) && val.bv_val != vals[ i ].bv_val ) {
1909 ch_free( val.bv_val );
1913 ber_bvarray_free_x( vals, op->o_tmpmemctx );
1916 return LDAP_SUCCESS;
1923 struct berval *query_uuid )
1925 struct search_info *si = op->o_callback->sc_private;
1926 slap_overinst *on = si->on;
1927 cache_manager *cm = on->on_bi.bi_private;
1930 struct berval crp_uuid;
1931 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1933 Connection conn = {0};
1934 OperationBuffer opbuf;
1935 void *thrctx = ldap_pvt_thread_pool_context();
1937 query_uuid->bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf));
1938 ber_str2bv(uuidbuf, query_uuid->bv_len, 1, query_uuid);
1940 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
1941 op_tmp = &opbuf.ob_op;
1942 op_tmp->o_bd = &cm->db;
1943 op_tmp->o_dn = cm->db.be_rootdn;
1944 op_tmp->o_ndn = cm->db.be_rootndn;
1946 Debug( pcache_debug, "UUID for query being added = %s\n",
1949 for ( e=si->head; e; e=si->head ) {
1950 si->head = e->e_private;
1951 e->e_private = NULL;
1952 while ( cm->cur_entries > (cm->max_entries) ) {
1953 BER_BVZERO( &crp_uuid );
1954 remove_query_and_data( op_tmp, rs, cm, &crp_uuid );
1957 return_val = merge_entry(op_tmp, e, query_uuid);
1958 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1959 cm->cur_entries += return_val;
1960 Debug( pcache_debug,
1961 "ENTRY ADDED/MERGED, CACHED ENTRIES=%d\n",
1962 cm->cur_entries, 0, 0 );
1964 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1971 pcache_op_cleanup( Operation *op, SlapReply *rs ) {
1972 slap_callback *cb = op->o_callback;
1973 struct search_info *si = cb->sc_private;
1974 slap_overinst *on = si->on;
1975 cache_manager *cm = on->on_bi.bi_private;
1976 query_manager* qm = cm->qm;
1978 if ( rs->sr_type == REP_SEARCH ) {
1981 /* don't return more entries than requested by the client */
1982 if ( si->slimit > 0 && rs->sr_nentries >= si->slimit ) {
1983 si->slimit_exceeded = 1;
1986 /* If we haven't exceeded the limit for this query,
1987 * build a chain of answers to store. If we hit the
1988 * limit, empty the chain and ignore the rest.
1991 /* check if the entry contains undefined
1992 * attributes/objectClasses (ITS#5680) */
1993 if ( cm->check_cacheability && test_filter( op, rs->sr_entry, si->query.filter ) != LDAP_COMPARE_TRUE ) {
1994 Debug( pcache_debug, "%s: query not cacheable because of schema issues in DN \"%s\"\n",
1995 op->o_log_prefix, rs->sr_entry->e_name.bv_val, 0 );
1999 /* check for malformed entries: attrs with no values */
2001 Attribute *a = rs->sr_entry->e_attrs;
2002 for (; a; a=a->a_next) {
2003 if ( !a->a_numvals ) {
2004 Debug( pcache_debug, "%s: query not cacheable because of attrs without values in DN \"%s\" (%s)\n",
2005 op->o_log_prefix, rs->sr_entry->e_name.bv_val,
2006 a->a_desc->ad_cname.bv_val );
2012 if ( si->count < si->max ) {
2014 e = entry_dup( rs->sr_entry );
2015 if ( !si->head ) si->head = e;
2016 if ( si->tail ) si->tail->e_private = e;
2023 for (;si->head; si->head=e) {
2024 e = si->head->e_private;
2025 si->head->e_private = NULL;
2026 entry_free(si->head);
2034 if ( rs->sr_type == REP_RESULT ||
2035 op->o_abandon || rs->sr_err == SLAPD_ABANDON )
2037 if ( si->swap_saved_attrs ) {
2038 rs->sr_attrs = si->save_attrs;
2039 op->ors_attrs = si->save_attrs;
2041 if ( (op->o_abandon || rs->sr_err == SLAPD_ABANDON) &&
2042 si->caching_reason == PC_IGNORE ) {
2043 filter_free( si->query.filter );
2045 /* duplicate query, free it */
2047 for (;si->head; si->head=e) {
2048 e = si->head->e_private;
2049 si->head->e_private = NULL;
2050 entry_free(si->head);
2053 op->o_callback = op->o_callback->sc_next;
2054 op->o_tmpfree( cb, op->o_tmpmemctx );
2055 } else if ( si->caching_reason != PC_IGNORE ) {
2056 CachedQuery *qc = qm->addfunc(op, qm, &si->query,
2057 si->qtemp, si->caching_reason, 1 );
2060 switch ( si->caching_reason ) {
2062 cache_entries( op, rs, &qc->q_uuid );
2066 qc->q_sizelimit = rs->sr_nentries;
2076 ldap_pvt_thread_rdwr_wunlock(&qc->rwlock);
2077 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2078 cm->num_cached_queries++;
2079 Debug( pcache_debug, "STORED QUERIES = %lu\n",
2080 cm->num_cached_queries, 0, 0 );
2081 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2083 /* If the consistency checker suspended itself,
2086 if ( cm->cc_paused ) {
2087 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2088 if ( cm->cc_paused ) {
2090 ldap_pvt_runqueue_resched( &slapd_rq, cm->cc_arg, 0 );
2092 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2095 } else if ( si->count ) {
2096 /* duplicate query, free it */
2098 for (;si->head; si->head=e) {
2099 e = si->head->e_private;
2100 si->head->e_private = NULL;
2101 entry_free(si->head);
2106 filter_free( si->query.filter );
2110 return SLAP_CB_CONTINUE;
2118 struct search_info *si = op->o_callback->sc_private;
2120 if ( si->swap_saved_attrs ) {
2121 rs->sr_attrs = si->save_attrs;
2122 op->ors_attrs = si->save_attrs;
2125 if ( rs->sr_type == REP_SEARCH ) {
2126 /* don't return more entries than requested by the client */
2127 if ( si->slimit_exceeded ) {
2131 } else if ( rs->sr_type == REP_RESULT ) {
2134 if ( rs->sr_err == LDAP_SUCCESS ) {
2135 si->caching_reason = PC_POSITIVE;
2137 } else if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED
2138 && si->qtemp->limitttl )
2140 si->caching_reason = PC_SIZELIMIT;
2143 } else if ( si->qtemp->negttl && !si->count && !si->over &&
2144 rs->sr_err == LDAP_SUCCESS )
2146 si->caching_reason = PC_NEGATIVE;
2150 if ( si->slimit_exceeded ) {
2151 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
2155 return SLAP_CB_CONTINUE;
2161 AttributeName** new_attrs,
2162 struct attr_set *attrs,
2163 AttributeName* filter_attrs,
2173 /* duplicate attrs */
2174 count = attrs->count + fattr_cnt;
2175 if ( !fattr_got_oc && !(attrs->flags & PC_GOT_OC)) {
2180 *new_attrs = (AttributeName*)ch_calloc( count + 1,
2181 sizeof(AttributeName) );
2182 for (i=0; i<attrs->count; i++) {
2183 (*new_attrs)[i].an_name = attrs->attrs[i].an_name;
2184 (*new_attrs)[i].an_desc = attrs->attrs[i].an_desc;
2186 BER_BVZERO( &(*new_attrs)[i].an_name );
2187 alluser = an_find( *new_attrs, slap_bv_all_user_attrs );
2188 allop = an_find( *new_attrs, slap_bv_all_operational_attrs );
2191 for ( i=0; i<fattr_cnt; i++ ) {
2192 if ( an_find(*new_attrs, &filter_attrs[i].an_name ) ) {
2195 if ( is_at_operational(filter_attrs[i].an_desc->ad_type) ) {
2199 } else if ( alluser ) {
2202 (*new_attrs)[j].an_name = filter_attrs[i].an_name;
2203 (*new_attrs)[j].an_desc = filter_attrs[i].an_desc;
2204 (*new_attrs)[j].an_oc = NULL;
2205 (*new_attrs)[j].an_oc_exclude = 0;
2209 (*new_attrs)[j].an_name = slap_schema.si_ad_objectClass->ad_cname;
2210 (*new_attrs)[j].an_desc = slap_schema.si_ad_objectClass;
2211 (*new_attrs)[j].an_oc = NULL;
2212 (*new_attrs)[j].an_oc_exclude = 0;
2215 BER_BVZERO( &(*new_attrs)[j].an_name );
2220 /* NOTE: this is a quick workaround to let pcache minimally interact
2221 * with pagedResults. A more articulated solutions would be to
2222 * perform the remote query without control and cache all results,
2223 * performing the pagedResults search only within the client
2224 * and the proxy. This requires pcache to understand pagedResults. */
2226 pcache_chk_controls(
2230 const char *non = "";
2231 const char *stripped = "";
2233 switch( op->o_pagedresults ) {
2234 case SLAP_CONTROL_NONCRITICAL:
2236 stripped = "; stripped";
2239 case SLAP_CONTROL_CRITICAL:
2240 Debug( pcache_debug, "%s: "
2241 "%scritical pagedResults control "
2242 "disabled with proxy cache%s.\n",
2243 op->o_log_prefix, non, stripped );
2245 slap_remove_control( op, rs, slap_cids.sc_pagedResults, NULL );
2249 rs->sr_err = SLAP_CB_CONTINUE;
2256 #ifdef PCACHE_CONTROL_PRIVDB
2262 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2263 cache_manager *cm = on->on_bi.bi_private;
2264 slap_callback *save_cb;
2267 /* skip if control is unset */
2268 if ( op->o_ctrlflag[ privDB_cid ] != SLAP_CONTROL_CRITICAL ) {
2269 return SLAP_CB_CONTINUE;
2272 /* The cache DB isn't open yet */
2273 if ( cm->defer_db_open ) {
2274 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
2275 "pcachePrivDB: cacheDB not available" );
2279 /* FIXME: might be a little bit exaggerated... */
2280 if ( !be_isroot( op ) ) {
2281 save_cb = op->o_callback;
2282 op->o_callback = NULL;
2283 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
2284 "pcachePrivDB: operation not allowed" );
2285 op->o_callback = save_cb;
2290 /* map tag to operation */
2291 type = slap_req2op( op->o_tag );
2292 if ( type != SLAP_OP_LAST ) {
2296 /* execute, if possible */
2297 func = &cm->db.be_bind;
2298 if ( func[ type ] != NULL ) {
2299 Operation op2 = *op;
2303 rc = func[ type ]( &op2, rs );
2304 if ( type == SLAP_OP_BIND && rc == LDAP_SUCCESS ) {
2305 op->o_conn->c_authz_cookie = cm->db.be_private;
2310 /* otherwise fall back to error */
2311 save_cb = op->o_callback;
2312 op->o_callback = NULL;
2313 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
2314 "operation not supported with pcachePrivDB control" );
2315 op->o_callback = save_cb;
2319 #endif /* PCACHE_CONTROL_PRIVDB */
2326 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2327 cache_manager *cm = on->on_bi.bi_private;
2328 query_manager* qm = cm->qm;
2332 AttributeName *filter_attrs = NULL;
2335 QueryTemplate *qtemp = NULL;
2338 CachedQuery *answerable = NULL;
2341 int fattr_got_oc = 0;
2343 struct berval tempstr;
2345 #ifdef PCACHE_CONTROL_PRIVDB
2346 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL ) {
2347 return pcache_op_privdb( op, rs );
2349 #endif /* PCACHE_CONTROL_PRIVDB */
2351 /* The cache DB isn't open yet */
2352 if ( cm->defer_db_open ) {
2353 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
2354 "pcachePrivDB: cacheDB not available" );
2358 /* pickup runtime ACL changes */
2359 cm->db.be_acl = op->o_bd->be_acl;
2361 tempstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len+1, op->o_tmpmemctx );
2363 if ( filter2template( op, op->ors_filter, &tempstr, &filter_attrs,
2364 &fattr_cnt, &fattr_got_oc )) {
2365 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
2366 return SLAP_CB_CONTINUE;
2369 Debug( pcache_debug, "query template of incoming query = %s\n",
2370 tempstr.bv_val, 0, 0 );
2372 /* FIXME: cannot cache/answer requests with pagedResults control */
2375 attr_set = get_attr_set(op->ors_attrs, qm, cm->numattrsets);
2377 query.filter = op->ors_filter;
2378 query.base = op->o_req_ndn;
2379 query.scope = op->ors_scope;
2381 /* check for query containment */
2382 if (attr_set > -1) {
2383 QueryTemplate *qt = qm->attr_sets[attr_set].templates;
2384 for (; qt; qt = qt->qtnext ) {
2385 /* find if template i can potentially answer tempstr */
2386 if (qt->querystr.bv_len != tempstr.bv_len ||
2387 strcasecmp( qt->querystr.bv_val, tempstr.bv_val ))
2391 Debug( pcache_debug, "Entering QC, querystr = %s\n",
2392 op->ors_filterstr.bv_val, 0, 0 );
2393 answerable = (*(qm->qcfunc))(op, qm, &query, qt);
2399 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
2402 BackendDB *save_bd = op->o_bd;
2403 slap_callback *save_cb = op->o_callback;
2405 Debug( pcache_debug, "QUERY ANSWERABLE\n", 0, 0, 0 );
2406 op->o_tmpfree( filter_attrs, op->o_tmpmemctx );
2407 ldap_pvt_thread_rdwr_rlock(&answerable->rwlock);
2408 if ( BER_BVISNULL( &answerable->q_uuid )) {
2409 /* No entries cached, just an empty result set */
2411 send_ldap_result( op, rs );
2414 i = cm->db.bd_info->bi_op_search( op, rs );
2416 ldap_pvt_thread_rdwr_runlock(&answerable->rwlock);
2417 ldap_pvt_thread_rdwr_runlock(&qtemp->t_rwlock);
2419 op->o_callback = save_cb;
2423 Debug( pcache_debug, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
2425 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2426 if (cm->num_cached_queries >= cm->max_queries) {
2429 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2431 if (op->ors_attrsonly)
2436 struct search_info *si;
2438 Debug( pcache_debug, "QUERY CACHEABLE\n", 0, 0, 0 );
2439 query.filter = filter_dup(op->ors_filter, NULL);
2440 ldap_pvt_thread_rdwr_wlock(&qtemp->t_rwlock);
2441 if ( !qtemp->t_attrs.count ) {
2442 qtemp->t_attrs.count = add_filter_attrs(op,
2443 &qtemp->t_attrs.attrs,
2444 &qm->attr_sets[attr_set],
2445 filter_attrs, fattr_cnt, fattr_got_oc);
2447 ldap_pvt_thread_rdwr_wunlock(&qtemp->t_rwlock);
2449 cb = op->o_tmpalloc( sizeof(*cb) + sizeof(*si), op->o_tmpmemctx );
2450 cb->sc_response = pcache_response;
2451 cb->sc_cleanup = pcache_op_cleanup;
2452 cb->sc_private = (cb+1);
2453 si = cb->sc_private;
2457 si->max = cm->num_entries_limit ;
2461 si->slimit_exceeded = 0;
2462 si->caching_reason = PC_IGNORE;
2463 if ( op->ors_slimit > 0 && op->ors_slimit < cm->num_entries_limit ) {
2464 si->slimit = op->ors_slimit;
2465 op->ors_slimit = cm->num_entries_limit;
2469 si->swap_saved_attrs = 1;
2470 si->save_attrs = op->ors_attrs;
2472 op->ors_attrs = qtemp->t_attrs.attrs;
2474 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
2475 cb->sc_next = op->o_callback;
2476 op->o_callback = cb;
2479 slap_callback **pcb;
2481 /* need to move the callback at the end, in case other
2482 * overlays are present, so that the final entry is
2483 * actually cached */
2485 for ( pcb = &op->o_callback; *pcb; pcb = &(*pcb)->sc_next );
2490 Debug( pcache_debug, "QUERY NOT CACHEABLE\n",
2494 op->o_tmpfree( filter_attrs, op->o_tmpmemctx );
2496 return SLAP_CB_CONTINUE;
2501 AttributeName* attrs,
2509 for ( ; attrs[count].an_name.bv_val; count++ );
2512 /* recognize a single "*" or a "1.1" */
2515 attrs = slap_anlist_all_user_attributes;
2517 } else if ( count == 1 && strcmp( attrs[0].an_name.bv_val, LDAP_NO_ATTRS ) == 0 ) {
2522 for ( i = 0; i < num; i++ ) {
2526 if ( count > qm->attr_sets[i].count ) {
2531 if ( !qm->attr_sets[i].count ) {
2537 for ( a2 = attrs; a2->an_name.bv_val; a2++ ) {
2538 if ( !an_find( qm->attr_sets[i].attrs, &a2->an_name ) ) {
2561 struct re_s *rtask = arg;
2562 slap_overinst *on = rtask->arg;
2563 cache_manager *cm = on->on_bi.bi_private;
2564 query_manager *qm = cm->qm;
2565 Connection conn = {0};
2566 OperationBuffer opbuf;
2569 SlapReply rs = {REP_RESULT};
2571 int return_val, pause = 1;
2572 QueryTemplate* templ;
2574 connection_fake_init( &conn, &opbuf, ctx );
2578 op->o_dn = cm->db.be_rootdn;
2579 op->o_ndn = cm->db.be_rootndn;
2583 for (templ = qm->templates; templ; templ=templ->qmnext) {
2584 query = templ->query_last;
2585 if ( query ) pause = 0;
2586 op->o_time = slap_get_time();
2587 while (query && (query->expiry_time < op->o_time)) {
2589 Debug( pcache_debug, "Lock CR index = %p\n",
2590 (void *) templ, 0, 0 );
2591 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
2592 if ( query == templ->query_last ) {
2594 remove_from_template(query, templ);
2595 Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
2596 (void *) templ, templ->no_of_queries, 0 );
2597 Debug( pcache_debug, "Unlock CR index = %p\n",
2598 (void *) templ, 0, 0 );
2600 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
2602 query = templ->query_last;
2605 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
2606 remove_query(qm, query);
2607 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
2608 if ( BER_BVISNULL( &query->q_uuid ))
2611 return_val = remove_query_data(op, &rs, &query->q_uuid);
2612 Debug( pcache_debug, "STALE QUERY REMOVED, SIZE=%d\n",
2614 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2615 cm->cur_entries -= return_val;
2616 cm->num_cached_queries--;
2617 Debug( pcache_debug, "STORED QUERIES = %lu\n",
2618 cm->num_cached_queries, 0, 0 );
2619 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2620 Debug( pcache_debug,
2621 "STALE QUERY REMOVED, CACHE ="
2623 cm->cur_entries, 0, 0 );
2625 query = templ->query_last;
2628 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2629 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
2630 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
2632 /* If there were no queries, defer processing for a while */
2633 cm->cc_paused = pause;
2634 ldap_pvt_runqueue_resched( &slapd_rq, rtask, pause );
2636 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2641 #define MAX_ATTR_SETS 500
2651 static ConfigDriver pc_cf_gen;
2652 static ConfigLDAPadd pc_ldadd;
2653 static ConfigCfAdd pc_cfadd;
2655 static ConfigTable pccfg[] = {
2656 { "proxycache", "backend> <max_entries> <numattrsets> <entry limit> "
2658 6, 6, 0, ARG_MAGIC|ARG_NO_DELETE|PC_MAIN, pc_cf_gen,
2659 "( OLcfgOvAt:2.1 NAME 'olcProxyCache' "
2660 "DESC 'ProxyCache basic parameters' "
2661 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
2662 { "proxyattrset", "index> <attributes...",
2663 2, 0, 0, ARG_MAGIC|PC_ATTR, pc_cf_gen,
2664 "( OLcfgOvAt:2.2 NAME 'olcProxyAttrset' "
2665 "DESC 'A set of attributes to cache' "
2666 "SYNTAX OMsDirectoryString )", NULL, NULL },
2667 { "proxytemplate", "filter> <attrset-index> <TTL> <negTTL",
2668 4, 6, 0, ARG_MAGIC|PC_TEMP, pc_cf_gen,
2669 "( OLcfgOvAt:2.3 NAME 'olcProxyTemplate' "
2670 "DESC 'Filter template, attrset, cache TTL, "
2671 "optional negative TTL, optional sizelimit TTL' "
2672 "SYNTAX OMsDirectoryString )", NULL, NULL },
2673 { "response-callback", "head|tail(default)",
2674 2, 2, 0, ARG_MAGIC|PC_RESP, pc_cf_gen,
2675 "( OLcfgOvAt:2.4 NAME 'olcProxyResponseCB' "
2676 "DESC 'Response callback position in overlay stack' "
2677 "SYNTAX OMsDirectoryString )", NULL, NULL },
2678 { "proxyCacheQueries", "queries",
2679 2, 2, 0, ARG_INT|ARG_MAGIC|PC_QUERIES, pc_cf_gen,
2680 "( OLcfgOvAt:2.5 NAME 'olcProxyCacheQueries' "
2681 "DESC 'Maximum number of queries to cache' "
2682 "SYNTAX OMsInteger )", NULL, NULL },
2683 { "proxySaveQueries", "TRUE|FALSE",
2684 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, save_queries),
2685 "( OLcfgOvAt:2.6 NAME 'olcProxySaveQueries' "
2686 "DESC 'Save cached queries for hot restart' "
2687 "SYNTAX OMsBoolean )", NULL, NULL },
2688 { "proxyCheckCacheability", "TRUE|FALSE",
2689 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, check_cacheability),
2690 "( OLcfgOvAt:2.7 NAME 'olcProxyCheckCacheability' "
2691 "DESC 'Check whether the results of a query are cacheable, e.g. for schema issues' "
2692 "SYNTAX OMsBoolean )", NULL, NULL },
2694 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
2697 static ConfigOCs pcocs[] = {
2698 { "( OLcfgOvOc:2.1 "
2699 "NAME 'olcPcacheConfig' "
2700 "DESC 'ProxyCache configuration' "
2701 "SUP olcOverlayConfig "
2702 "MUST ( olcProxyCache $ olcProxyAttrset $ olcProxyTemplate ) "
2703 "MAY ( olcProxyResponseCB $ olcProxyCacheQueries $ olcProxySaveQueries $ olcProxyCheckCacheability ) )",
2704 Cft_Overlay, pccfg, NULL, pc_cfadd },
2705 { "( OLcfgOvOc:2.2 "
2706 "NAME 'olcPcacheDatabase' "
2707 "DESC 'Cache database configuration' "
2708 "AUXILIARY )", Cft_Misc, olcDatabaseDummy, pc_ldadd },
2712 static int pcache_db_open2( slap_overinst *on, ConfigReply *cr );
2715 pc_ldadd_cleanup( ConfigArgs *c )
2717 slap_overinst *on = c->ca_private;
2718 return pcache_db_open2( on, &c->reply );
2722 pc_ldadd( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
2727 if ( p->ce_type != Cft_Overlay || !p->ce_bi ||
2728 p->ce_bi->bi_cf_ocs != pcocs )
2729 return LDAP_CONSTRAINT_VIOLATION;
2731 on = (slap_overinst *)p->ce_bi;
2732 cm = on->on_bi.bi_private;
2734 /* Defer open if this is an LDAPadd */
2735 if ( CONFIG_ONLINE_ADD( ca ))
2736 ca->cleanup = pc_ldadd_cleanup;
2738 cm->defer_db_open = 0;
2739 ca->ca_private = on;
2740 return LDAP_SUCCESS;
2744 pc_cfadd( Operation *op, SlapReply *rs, Entry *p, ConfigArgs *ca )
2746 CfEntryInfo *pe = p->e_private;
2747 slap_overinst *on = (slap_overinst *)pe->ce_bi;
2748 cache_manager *cm = on->on_bi.bi_private;
2751 /* FIXME: should not hardcode "olcDatabase" here */
2752 bv.bv_len = snprintf( ca->cr_msg, sizeof( ca->cr_msg ),
2753 "olcDatabase=" SLAP_X_ORDERED_FMT "%s",
2754 0, cm->db.bd_info->bi_type );
2755 if ( bv.bv_len >= sizeof( ca->cr_msg ) ) {
2758 bv.bv_val = ca->cr_msg;
2760 cm->defer_db_open = 0;
2762 /* We can only create this entry if the database is table-driven
2764 if ( cm->db.bd_info->bi_cf_ocs )
2765 config_build_entry( op, rs, pe, ca, &bv, cm->db.bd_info->bi_cf_ocs,
2772 pc_cf_gen( ConfigArgs *c )
2774 slap_overinst *on = (slap_overinst *)c->bi;
2775 cache_manager* cm = on->on_bi.bi_private;
2776 query_manager* qm = cm->qm;
2777 QueryTemplate* temp;
2778 AttributeName* attr_name;
2779 AttributeName* attrarray;
2780 const char* text=NULL;
2785 if ( c->op == SLAP_CONFIG_EMIT ) {
2789 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s %d %d %d %ld",
2790 cm->db.bd_info->bi_type, cm->max_entries, cm->numattrsets,
2791 cm->num_entries_limit, cm->cc_period );
2792 bv.bv_val = c->cr_msg;
2793 value_add_one( &c->rvalue_vals, &bv );
2796 for (i=0; i<cm->numattrsets; i++) {
2797 if ( !qm->attr_sets[i].count ) continue;
2799 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ), "%d", i );
2801 /* count the attr length */
2802 for ( attr_name = qm->attr_sets[i].attrs;
2803 attr_name->an_name.bv_val; attr_name++ )
2804 bv.bv_len += attr_name->an_name.bv_len + 1;
2806 bv.bv_val = ch_malloc( bv.bv_len+1 );
2807 ptr = lutil_strcopy( bv.bv_val, c->cr_msg );
2808 for ( attr_name = qm->attr_sets[i].attrs;
2809 attr_name->an_name.bv_val; attr_name++ ) {
2811 ptr = lutil_strcopy( ptr, attr_name->an_name.bv_val );
2813 ber_bvarray_add( &c->rvalue_vals, &bv );
2815 if ( !c->rvalue_vals )
2819 for (temp=qm->templates; temp; temp=temp->qmnext) {
2820 /* HEADS-UP: always print all;
2821 * if optional == 0, ignore */
2822 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ),
2824 temp->attr_set_index,
2828 bv.bv_len += temp->querystr.bv_len + 2;
2829 bv.bv_val = ch_malloc( bv.bv_len+1 );
2832 ptr = lutil_strcopy( ptr, temp->querystr.bv_val );
2834 strcpy( ptr, c->cr_msg );
2835 ber_bvarray_add( &c->rvalue_vals, &bv );
2837 if ( !c->rvalue_vals )
2841 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
2842 BER_BVSTR( &bv, "head" );
2844 BER_BVSTR( &bv, "tail" );
2846 value_add_one( &c->rvalue_vals, &bv );
2849 c->value_int = cm->max_queries;
2853 } else if ( c->op == LDAP_MOD_DELETE ) {
2854 return 1; /* FIXME */
2866 if ( cm->numattrsets > 0 ) {
2867 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"proxycache\" directive already provided" );
2868 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2872 if ( lutil_atoi( &cm->numattrsets, c->argv[3] ) != 0 ) {
2873 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse num attrsets=\"%s\" (arg #3)",
2875 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2878 if ( cm->numattrsets <= 0 ) {
2879 snprintf( c->cr_msg, sizeof( c->cr_msg ), "numattrsets (arg #3) must be positive" );
2880 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2883 if ( cm->numattrsets > MAX_ATTR_SETS ) {
2884 snprintf( c->cr_msg, sizeof( c->cr_msg ), "numattrsets (arg #3) must be <= %d", MAX_ATTR_SETS );
2885 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2889 if ( !backend_db_init( c->argv[1], &cm->db, -1, NULL )) {
2890 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unknown backend type (arg #1)" );
2891 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2895 if ( lutil_atoi( &cm->max_entries, c->argv[2] ) != 0 ) {
2896 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse max entries=\"%s\" (arg #2)",
2898 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2901 if ( cm->max_entries <= 0 ) {
2902 snprintf( c->cr_msg, sizeof( c->cr_msg ), "max entries (arg #2) must be positive.\n" );
2903 Debug( LDAP_DEBUG_CONFIG, "%s: %s\n", c->log, c->cr_msg, 0 );
2907 if ( lutil_atoi( &cm->num_entries_limit, c->argv[4] ) != 0 ) {
2908 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse entry limit=\"%s\" (arg #4)",
2910 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2913 if ( cm->num_entries_limit <= 0 ) {
2914 snprintf( c->cr_msg, sizeof( c->cr_msg ), "entry limit (arg #4) must be positive" );
2915 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2918 if ( cm->num_entries_limit > cm->max_entries ) {
2919 snprintf( c->cr_msg, sizeof( c->cr_msg ), "entry limit (arg #4) must be less than max entries %d (arg #2)", cm->max_entries );
2920 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2924 if ( lutil_parse_time( c->argv[5], &t ) != 0 ) {
2925 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse period=\"%s\" (arg #5)",
2927 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2931 cm->cc_period = (time_t)t;
2932 Debug( pcache_debug,
2933 "Total # of attribute sets to be cached = %d.\n",
2934 cm->numattrsets, 0, 0 );
2935 qm->attr_sets = ( struct attr_set * )ch_calloc( cm->numattrsets,
2936 sizeof( struct attr_set ) );
2939 if ( cm->numattrsets == 0 ) {
2940 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"proxycache\" directive not provided yet" );
2941 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2944 if ( lutil_atoi( &num, c->argv[1] ) != 0 ) {
2945 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse attrset #=\"%s\"",
2947 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2951 if ( num < 0 || num >= cm->numattrsets ) {
2952 snprintf( c->cr_msg, sizeof( c->cr_msg ), "attrset index %d out of bounds (must be %s%d)",
2953 num, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
2954 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2957 qm->attr_sets[num].flags |= PC_CONFIGURED;
2958 if ( c->argc == 2 ) {
2960 snprintf( c->cr_msg, sizeof( c->cr_msg ),
2961 "need an explicit attr in attrlist; use \"*\" to indicate all attrs" );
2962 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2965 } else if ( c->argc == 3 ) {
2966 if ( strcmp( c->argv[2], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) {
2967 qm->attr_sets[num].count = 1;
2968 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 2,
2969 sizeof( AttributeName ) );
2970 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_USER_ATTRIBUTES );
2973 } else if ( strcmp( c->argv[2], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 ) {
2974 qm->attr_sets[num].count = 1;
2975 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 2,
2976 sizeof( AttributeName ) );
2977 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
2980 } else if ( strcmp( c->argv[2], LDAP_NO_ATTRS ) == 0 ) {
2983 /* else: fallthru */
2985 } else if ( c->argc == 4 ) {
2986 if ( ( strcmp( c->argv[2], LDAP_ALL_USER_ATTRIBUTES ) == 0 && strcmp( c->argv[3], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 )
2987 || ( strcmp( c->argv[2], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 && strcmp( c->argv[3], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) )
2989 qm->attr_sets[num].count = 2;
2990 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 3,
2991 sizeof( AttributeName ) );
2992 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_USER_ATTRIBUTES );
2993 BER_BVSTR( &qm->attr_sets[num].attrs[1].an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
2996 /* else: fallthru */
2999 if ( c->argc > 2 ) {
3000 int all_user = 0, all_op = 0;
3002 qm->attr_sets[num].count = c->argc - 2;
3003 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( c->argc - 1,
3004 sizeof( AttributeName ) );
3005 attr_name = qm->attr_sets[num].attrs;
3006 for ( i = 2; i < c->argc; i++ ) {
3007 attr_name->an_desc = NULL;
3008 if ( strcmp( c->argv[i], LDAP_NO_ATTRS ) == 0 ) {
3009 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3010 "invalid attr #%d \"%s\" in attrlist",
3011 i - 2, c->argv[i] );
3012 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3013 ch_free( qm->attr_sets[num].attrs );
3014 qm->attr_sets[num].attrs = NULL;
3015 qm->attr_sets[num].count = 0;
3018 if ( strcmp( c->argv[i], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) {
3020 BER_BVSTR( &attr_name->an_name, LDAP_ALL_USER_ATTRIBUTES );
3021 } else if ( strcmp( c->argv[i], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 ) {
3023 BER_BVSTR( &attr_name->an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
3025 if ( slap_str2ad( c->argv[i], &attr_name->an_desc, &text ) ) {
3026 strcpy( c->cr_msg, text );
3027 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3028 ch_free( qm->attr_sets[num].attrs );
3029 qm->attr_sets[num].attrs = NULL;
3030 qm->attr_sets[num].count = 0;
3033 attr_name->an_name = attr_name->an_desc->ad_cname;
3035 attr_name->an_oc = NULL;
3036 attr_name->an_oc_exclude = 0;
3037 if ( attr_name->an_desc == slap_schema.si_ad_objectClass )
3038 qm->attr_sets[num].flags |= PC_GOT_OC;
3040 BER_BVZERO( &attr_name->an_name );
3043 /* warn if list contains both "*" and "+" */
3044 if ( i > 4 && all_user && all_op ) {
3045 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3046 "warning: attribute list contains \"*\" and \"+\"" );
3047 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3052 if ( cm->numattrsets == 0 ) {
3053 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"proxycache\" directive not provided yet" );
3054 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3057 if ( lutil_atoi( &i, c->argv[2] ) != 0 ) {
3058 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse template #=\"%s\"",
3060 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3064 if ( i < 0 || i >= cm->numattrsets ||
3065 !(qm->attr_sets[i].flags & PC_CONFIGURED )) {
3066 snprintf( c->cr_msg, sizeof( c->cr_msg ), "template index %d invalid (%s%d)",
3067 i, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
3068 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3071 temp = ch_calloc( 1, sizeof( QueryTemplate ));
3072 temp->qmnext = qm->templates;
3073 qm->templates = temp;
3074 ldap_pvt_thread_rdwr_init( &temp->t_rwlock );
3075 temp->query = temp->query_last = NULL;
3076 if ( lutil_parse_time( c->argv[3], &t ) != 0 ) {
3077 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3078 "unable to parse template ttl=\"%s\"",
3080 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3083 temp->ttl = (time_t)t;
3084 temp->negttl = (time_t)0;
3085 temp->limitttl = (time_t)0;
3086 switch ( c->argc ) {
3088 if ( lutil_parse_time( c->argv[5], &t ) != 0 ) {
3089 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3090 "unable to parse template sizelimit ttl=\"%s\"",
3092 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3095 temp->limitttl = (time_t)t;
3099 if ( lutil_parse_time( c->argv[4], &t ) != 0 ) {
3100 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3101 "unable to parse template negative ttl=\"%s\"",
3103 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3106 temp->negttl = (time_t)t;
3110 temp->no_of_queries = 0;
3112 ber_str2bv( c->argv[1], 0, 1, &temp->querystr );
3113 Debug( pcache_debug, "Template:\n", 0, 0, 0 );
3114 Debug( pcache_debug, " query template: %s\n",
3115 temp->querystr.bv_val, 0, 0 );
3116 temp->attr_set_index = i;
3117 qm->attr_sets[i].flags |= PC_REFERENCED;
3118 temp->qtnext = qm->attr_sets[i].templates;
3119 qm->attr_sets[i].templates = temp;
3120 Debug( pcache_debug, " attributes: \n", 0, 0, 0 );
3121 if ( ( attrarray = qm->attr_sets[i].attrs ) != NULL ) {
3122 for ( i=0; attrarray[i].an_name.bv_val; i++ )
3123 Debug( pcache_debug, "\t%s\n",
3124 attrarray[i].an_name.bv_val, 0, 0 );
3128 if ( strcasecmp( c->argv[1], "head" ) == 0 ) {
3129 cm->response_cb = PCACHE_RESPONSE_CB_HEAD;
3131 } else if ( strcasecmp( c->argv[1], "tail" ) == 0 ) {
3132 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
3135 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unknown specifier" );
3136 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3141 if ( c->value_int <= 0 ) {
3142 snprintf( c->cr_msg, sizeof( c->cr_msg ), "max queries must be positive" );
3143 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3146 cm->max_queries = c->value_int;
3161 slap_overinst *on = (slap_overinst *)be->bd_info;
3162 cache_manager* cm = on->on_bi.bi_private;
3164 /* Something for the cache database? */
3165 if ( cm->db.bd_info && cm->db.bd_info->bi_db_config )
3166 return cm->db.bd_info->bi_db_config( &cm->db, fname, lineno,
3168 return SLAP_CONF_UNKNOWN;
3176 slap_overinst *on = (slap_overinst *)be->bd_info;
3180 cm = (cache_manager *)ch_malloc(sizeof(cache_manager));
3181 on->on_bi.bi_private = cm;
3183 qm = (query_manager*)ch_malloc(sizeof(query_manager));
3186 SLAP_DBFLAGS(&cm->db) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3187 cm->db.be_private = NULL;
3188 cm->db.bd_self = &cm->db;
3190 cm->numattrsets = 0;
3191 cm->num_entries_limit = 5;
3192 cm->num_cached_queries = 0;
3193 cm->max_entries = 0;
3194 cm->cur_entries = 0;
3195 cm->max_queries = 10000;
3196 cm->save_queries = 0;
3197 cm->check_cacheability = 0;
3198 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
3199 cm->defer_db_open = 1;
3200 cm->cc_period = 1000;
3204 qm->attr_sets = NULL;
3205 qm->templates = NULL;
3207 qm->lru_bottom = NULL;
3209 qm->qcfunc = query_containment;
3210 qm->crfunc = cache_replacement;
3211 qm->addfunc = add_query;
3212 ldap_pvt_thread_mutex_init(&qm->lru_mutex);
3214 ldap_pvt_thread_mutex_init(&cm->cache_mutex);
3219 pcache_cachedquery_open_cb( Operation *op, SlapReply *rs )
3221 assert( op->o_tag == LDAP_REQ_SEARCH );
3223 if ( rs->sr_type == REP_SEARCH ) {
3226 a = attr_find( rs->sr_entry->e_attrs, ad_cachedQueryURL );
3230 assert( a->a_nvals != NULL );
3232 valsp = op->o_callback->sc_private;
3233 assert( *valsp == NULL );
3235 ber_bvarray_dup_x( valsp, a->a_nvals, op->o_tmpmemctx );
3243 pcache_cachedquery_count_cb( Operation *op, SlapReply *rs )
3245 assert( op->o_tag == LDAP_REQ_SEARCH );
3247 if ( rs->sr_type == REP_SEARCH ) {
3248 int *countp = (int *)op->o_callback->sc_private;
3261 cache_manager *cm = on->on_bi.bi_private;
3262 query_manager* qm = cm->qm;
3265 rc = backend_startup_one( &cm->db, cr );
3267 cm->defer_db_open = 0;
3270 /* There is no runqueue in TOOL mode */
3271 if (( slapMode & SLAP_SERVER_MODE ) && rc == 0 ) {
3272 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3273 ldap_pvt_runqueue_insert( &slapd_rq, cm->cc_period,
3274 consistency_check, on,
3275 "pcache_consistency", cm->db.be_suffix[0].bv_val );
3276 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3278 /* Cached database must have the rootdn */
3279 if ( BER_BVISNULL( &cm->db.be_rootndn )
3280 || BER_BVISEMPTY( &cm->db.be_rootndn ) )
3282 Debug( LDAP_DEBUG_ANY, "pcache_db_open(): "
3283 "underlying database of type \"%s\"\n"
3284 " serving naming context \"%s\"\n"
3285 " has no \"rootdn\", required by \"proxycache\".\n",
3286 on->on_info->oi_orig->bi_type,
3287 cm->db.be_suffix[0].bv_val, 0 );
3291 if ( cm->save_queries ) {
3292 void *thrctx = ldap_pvt_thread_pool_context();
3293 Connection conn = { 0 };
3294 OperationBuffer opbuf;
3296 slap_callback cb = { 0 };
3297 SlapReply rs = { 0 };
3298 BerVarray vals = NULL;
3299 Filter f = { 0 }, f2 = { 0 };
3300 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
3301 AttributeName attrs[ 2 ] = {{{ 0 }}};
3303 connection_fake_init( &conn, &opbuf, thrctx );
3308 op->o_tag = LDAP_REQ_SEARCH;
3309 op->o_protocol = LDAP_VERSION3;
3310 cb.sc_response = pcache_cachedquery_open_cb;
3311 cb.sc_private = &vals;
3312 op->o_callback = &cb;
3313 op->o_time = slap_get_time();
3314 op->o_do_not_cache = 1;
3315 op->o_managedsait = SLAP_CONTROL_CRITICAL;
3317 op->o_dn = cm->db.be_rootdn;
3318 op->o_ndn = cm->db.be_rootndn;
3319 op->o_req_dn = cm->db.be_suffix[ 0 ];
3320 op->o_req_ndn = cm->db.be_nsuffix[ 0 ];
3322 op->ors_scope = LDAP_SCOPE_BASE;
3323 op->ors_deref = LDAP_DEREF_NEVER;
3325 op->ors_tlimit = SLAP_NO_LIMIT;
3326 ber_str2bv( "(cachedQueryURL=*)", 0, 0, &op->ors_filterstr );
3327 f.f_choice = LDAP_FILTER_PRESENT;
3328 f.f_desc = ad_cachedQueryURL;
3329 op->ors_filter = &f;
3330 attrs[ 0 ].an_desc = ad_cachedQueryURL;
3331 attrs[ 0 ].an_name = ad_cachedQueryURL->ad_cname;
3332 op->ors_attrs = attrs;
3333 op->ors_attrsonly = 0;
3335 rc = op->o_bd->be_search( op, &rs );
3336 if ( rc == LDAP_SUCCESS && vals != NULL ) {
3339 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
3340 if ( url2query( vals[ i ].bv_val, op, qm ) == 0 ) {
3341 cm->num_cached_queries++;
3345 ber_bvarray_free_x( vals, op->o_tmpmemctx );
3348 /* count cached entries */
3349 f.f_choice = LDAP_FILTER_NOT;
3351 f2.f_choice = LDAP_FILTER_EQUALITY;
3353 f2.f_av_desc = slap_schema.si_ad_objectClass;
3354 BER_BVSTR( &f2.f_av_value, "glue" );
3355 ber_str2bv( "(!(objectClass=glue))", 0, 0, &op->ors_filterstr );
3357 op->ors_slimit = SLAP_NO_LIMIT;
3358 op->ors_scope = LDAP_SCOPE_SUBTREE;
3359 op->ors_attrs = slap_anlist_no_attrs;
3361 op->o_callback->sc_response = pcache_cachedquery_count_cb;
3363 op->o_callback->sc_private = &rs.sr_nentries;
3365 rc = op->o_bd->be_search( op, &rs );
3367 cm->cur_entries = rs.sr_nentries;
3381 slap_overinst *on = (slap_overinst *)be->bd_info;
3382 cache_manager *cm = on->on_bi.bi_private;
3383 query_manager* qm = cm->qm;
3384 int i, ncf = 0, rf = 0, nrf = 0, rc = 0;
3386 /* check attr sets */
3387 for ( i = 0; i < cm->numattrsets; i++) {
3388 if ( !( qm->attr_sets[i].flags & PC_CONFIGURED ) ) {
3389 if ( qm->attr_sets[i].flags & PC_REFERENCED ) {
3390 Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d not configured but referenced.\n", i, 0, 0 );
3394 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, attr set #%d not configured.\n", i, 0, 0 );
3398 } else if ( !( qm->attr_sets[i].flags & PC_REFERENCED ) ) {
3399 Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d configured but not referenced.\n", i, 0, 0 );
3404 if ( ncf || rf || nrf ) {
3405 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets configured but not referenced.\n", nrf, 0, 0 );
3406 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets not configured.\n", ncf, 0, 0 );
3407 Debug( LDAP_DEBUG_CONFIG, "pcache: %d attr sets not configured but referenced.\n", rf, 0, 0 );
3414 /* need to inherit something from the original database... */
3415 cm->db.be_def_limit = be->be_def_limit;
3416 cm->db.be_limits = be->be_limits;
3417 cm->db.be_acl = be->be_acl;
3418 cm->db.be_dfltaccess = be->be_dfltaccess;
3420 if ( SLAP_DBMONITORING( be ) ) {
3421 SLAP_DBFLAGS( &cm->db ) |= SLAP_DBFLAG_MONITORING;
3424 SLAP_DBFLAGS( &cm->db ) &= ~SLAP_DBFLAG_MONITORING;
3427 if ( !cm->defer_db_open )
3428 rc = pcache_db_open2( on, cr );
3434 pcache_free_qbase( void *v )
3440 tavl_free( qb->scopes[i], NULL );
3450 slap_overinst *on = (slap_overinst *)be->bd_info;
3451 cache_manager *cm = on->on_bi.bi_private;
3452 query_manager *qm = cm->qm;
3456 if ( cm->save_queries ) {
3458 BerVarray vals = NULL;
3461 Connection conn = { 0 };
3462 OperationBuffer opbuf;
3464 slap_callback cb = { 0 };
3466 SlapReply rs = { REP_RESULT };
3467 Modifications mod = {{ 0 }};
3469 thrctx = ldap_pvt_thread_pool_context();
3471 connection_fake_init( &conn, &opbuf, thrctx );
3474 if ( qm->templates != NULL ) {
3475 for ( tm = qm->templates; tm != NULL; tm = tm->qmnext ) {
3476 for ( qc = tm->query; qc; qc = qc->next ) {
3479 if ( query2url( op, qc, &bv ) == 0 ) {
3480 ber_bvarray_add_x( &vals, &bv, op->o_tmpmemctx );
3487 op->o_dn = cm->db.be_rootdn;
3488 op->o_ndn = cm->db.be_rootndn;
3490 op->o_tag = LDAP_REQ_MODIFY;
3491 op->o_protocol = LDAP_VERSION3;
3492 cb.sc_response = slap_null_cb;
3493 op->o_callback = &cb;
3494 op->o_time = slap_get_time();
3495 op->o_do_not_cache = 1;
3496 op->o_managedsait = SLAP_CONTROL_CRITICAL;
3498 op->o_req_dn = op->o_bd->be_suffix[0];
3499 op->o_req_ndn = op->o_bd->be_nsuffix[0];
3501 mod.sml_op = LDAP_MOD_REPLACE;
3503 mod.sml_desc = ad_cachedQueryURL;
3504 mod.sml_type = ad_cachedQueryURL->ad_cname;
3505 mod.sml_values = vals;
3506 mod.sml_nvalues = NULL;
3507 mod.sml_numvals = 1;
3508 mod.sml_next = NULL;
3509 Debug( pcache_debug,
3510 "%sSETTING CACHED QUERY URLS\n",
3511 vals == NULL ? "RE" : "", 0, 0 );
3513 op->orm_modlist = &mod;
3515 op->o_bd->be_modify( op, &rs );
3517 ber_bvarray_free_x( vals, op->o_tmpmemctx );
3520 /* cleanup stuff inherited from the original database... */
3521 cm->db.be_limits = NULL;
3522 cm->db.be_acl = NULL;
3524 /* stop the thread ... */
3526 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3527 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, cm->cc_arg ) ) {
3528 ldap_pvt_runqueue_stoptask( &slapd_rq, cm->cc_arg );
3530 ldap_pvt_runqueue_remove( &slapd_rq, cm->cc_arg );
3531 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3534 if ( cm->db.bd_info->bi_db_close ) {
3535 rc = cm->db.bd_info->bi_db_close( &cm->db, NULL );
3537 while ( (tm = qm->templates) != NULL ) {
3538 CachedQuery *qc, *qn;
3539 qm->templates = tm->qmnext;
3540 for ( qc = tm->query; qc; qc = qn ) {
3544 avl_free( tm->qbase, pcache_free_qbase );
3545 free( tm->querystr.bv_val );
3546 ldap_pvt_thread_rdwr_destroy( &tm->t_rwlock );
3547 free( tm->t_attrs.attrs );
3551 for ( i=0; i<cm->numattrsets; i++ ) {
3552 free( qm->attr_sets[i].attrs );
3554 free( qm->attr_sets );
3555 qm->attr_sets = NULL;
3566 slap_overinst *on = (slap_overinst *)be->bd_info;
3567 cache_manager *cm = on->on_bi.bi_private;
3568 query_manager *qm = cm->qm;
3570 if ( cm->db.be_private != NULL ) {
3571 backend_stopdown_one( &cm->db );
3574 ldap_pvt_thread_mutex_destroy( &qm->lru_mutex );
3575 ldap_pvt_thread_mutex_destroy( &cm->cache_mutex );
3582 #ifdef PCACHE_CONTROL_PRIVDB
3584 Control ::= SEQUENCE {
3585 controlType LDAPOID,
3586 criticality BOOLEAN DEFAULT FALSE,
3587 controlValue OCTET STRING OPTIONAL }
3589 controlType ::= 1.3.6.1.4.1.4203.666.11.9.5.1
3591 * criticality must be TRUE; controlValue must be absent.
3599 if ( op->o_ctrlflag[ privDB_cid ] != SLAP_CONTROL_NONE ) {
3600 rs->sr_text = "privateDB control specified multiple times";
3601 return LDAP_PROTOCOL_ERROR;
3604 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
3605 rs->sr_text = "privateDB control value not absent";
3606 return LDAP_PROTOCOL_ERROR;
3609 if ( !ctrl->ldctl_iscritical ) {
3610 rs->sr_text = "privateDB control criticality required";
3611 return LDAP_PROTOCOL_ERROR;
3614 op->o_ctrlflag[ privDB_cid ] = SLAP_CONTROL_CRITICAL;
3616 return LDAP_SUCCESS;
3619 static char *extops[] = {
3620 LDAP_EXOP_MODIFY_PASSWD,
3623 #endif /* PCACHE_CONTROL_PRIVDB */
3625 #ifdef PCACHE_EXOP_QUERY_DELETE
3626 static struct berval pcache_exop_QUERY_DELETE = BER_BVC( PCACHE_EXOP_QUERY_DELETE );
3628 #define LDAP_TAG_EXOP_QUERY_DELETE_BASE ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 0)
3629 #define LDAP_TAG_EXOP_QUERY_DELETE_DN ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 1)
3630 #define LDAP_TAG_EXOP_QUERY_DELETE_UUID ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 2)
3633 ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
3634 requestName [0] LDAPOID,
3635 requestValue [1] OCTET STRING OPTIONAL }
3637 requestName ::= 1.3.6.1.4.1.4203.666.11.9.6.1
3639 requestValue ::= SEQUENCE { CHOICE {
3641 entryDN [1] LDAPDN },
3642 queryID [2] OCTET STRING (SIZE(16))
3643 -- constrained to UUID }
3645 * Either baseDN or entryDN must be present, to allow database selection.
3647 * 1. if baseDN and queryID are present, then the query corresponding
3648 * to queryID is deleted;
3649 * 2. if baseDN is present and queryID is absent, then all queries
3651 * 3. if entryDN is present and queryID is absent, then all queries
3652 * corresponding to the queryID values present in entryDN are deleted;
3653 * 4. if entryDN and queryID are present, then all queries
3654 * corresponding to the queryID values present in entryDN are deleted,
3655 * but only if the value of queryID is contained in the entry;
3657 * Currently, only 1, 3 and 4 are implemented. 2 can be obtained by either
3658 * recursively deleting the database (ldapdelete -r) with PRIVDB control,
3659 * or by removing the database files.
3661 ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
3662 COMPONENTS OF LDAPResult,
3663 responseName [10] LDAPOID OPTIONAL,
3664 responseValue [11] OCTET STRING OPTIONAL }
3666 * responseName and responseValue must be absent.
3670 * - on success, *tagp is either LDAP_TAG_EXOP_QUERY_DELETE_BASE
3671 * or LDAP_TAG_EXOP_QUERY_DELETE_DN.
3672 * - if ndn != NULL, it is set to the normalized DN in the request
3673 * corresponding to either the baseDN or the entryDN, according
3674 * to *tagp; memory is malloc'ed on the Operation's slab, and must
3675 * be freed by the caller.
3676 * - if uuid != NULL, it is set to point to the normalized UUID;
3677 * memory is malloc'ed on the Operation's slab, and must
3678 * be freed by the caller.
3681 pcache_parse_query_delete(
3685 struct berval *uuid,
3689 int rc = LDAP_SUCCESS;
3692 BerElementBuffer berbuf;
3693 BerElement *ber = (BerElement *)&berbuf;
3694 struct berval reqdata = BER_BVNULL;
3706 if ( in == NULL || in->bv_len == 0 ) {
3707 *text = "empty request data field in queryDelete exop";
3708 return LDAP_PROTOCOL_ERROR;
3711 ber_dupbv_x( &reqdata, in, ctx );
3713 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
3714 ber_init2( ber, &reqdata, 0 );
3716 tag = ber_scanf( ber, "{" /*}*/ );
3718 if ( tag == LBER_ERROR ) {
3719 Debug( LDAP_DEBUG_TRACE,
3720 "pcache_parse_query_delete: decoding error.\n",
3722 goto decoding_error;
3725 tag = ber_peek_tag( ber, &len );
3726 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_BASE
3727 || tag == LDAP_TAG_EXOP_QUERY_DELETE_DN )
3731 if ( ndn != NULL ) {
3734 tag = ber_scanf( ber, "m", &dn );
3735 if ( tag == LBER_ERROR ) {
3736 Debug( LDAP_DEBUG_TRACE,
3737 "pcache_parse_query_delete: DN parse failed.\n",
3739 goto decoding_error;
3742 rc = dnNormalize( 0, NULL, NULL, &dn, ndn, ctx );
3743 if ( rc != LDAP_SUCCESS ) {
3744 *text = "invalid DN in queryDelete exop request data";
3749 tag = ber_scanf( ber, "x" /* "m" */ );
3750 if ( tag == LBER_DEFAULT ) {
3751 goto decoding_error;
3755 tag = ber_peek_tag( ber, &len );
3758 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_UUID ) {
3759 if ( uuid != NULL ) {
3761 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
3763 tag = ber_scanf( ber, "m", &bv );
3764 if ( tag == LBER_ERROR ) {
3765 Debug( LDAP_DEBUG_TRACE,
3766 "pcache_parse_query_delete: UUID parse failed.\n",
3768 goto decoding_error;
3771 if ( bv.bv_len != 16 ) {
3772 Debug( LDAP_DEBUG_TRACE,
3773 "pcache_parse_query_delete: invalid UUID length %lu.\n",
3774 (unsigned long)bv.bv_len, 0, 0 );
3775 goto decoding_error;
3778 rc = lutil_uuidstr_from_normalized(
3779 bv.bv_val, bv.bv_len,
3780 uuidbuf, sizeof( uuidbuf ) );
3782 goto decoding_error;
3784 ber_str2bv( uuidbuf, rc, 1, uuid );
3788 tag = ber_skip_tag( ber, &len );
3789 if ( tag == LBER_DEFAULT ) {
3790 goto decoding_error;
3794 Debug( LDAP_DEBUG_TRACE,
3795 "pcache_parse_query_delete: invalid UUID length %lu.\n",
3796 (unsigned long)len, 0, 0 );
3797 goto decoding_error;
3801 tag = ber_peek_tag( ber, &len );
3804 if ( tag != LBER_DEFAULT || len != 0 ) {
3806 Debug( LDAP_DEBUG_TRACE,
3807 "pcache_parse_query_delete: decoding error\n",
3809 rc = LDAP_PROTOCOL_ERROR;
3810 *text = "queryDelete data decoding error";
3813 if ( ndn && !BER_BVISNULL( ndn ) ) {
3814 slap_sl_free( ndn->bv_val, ctx );
3818 if ( uuid && !BER_BVISNULL( uuid ) ) {
3819 slap_sl_free( uuid->bv_val, ctx );
3824 if ( !BER_BVISNULL( &reqdata ) ) {
3825 ber_memfree_x( reqdata.bv_val, ctx );
3832 pcache_exop_query_delete(
3836 BackendDB *bd = op->o_bd;
3838 struct berval uuid = BER_BVNULL,
3840 char buf[ SLAP_TEXT_BUFLEN ];
3842 ber_tag_t tag = LBER_DEFAULT;
3844 if ( LogTest( LDAP_DEBUG_STATS ) ) {
3848 rs->sr_err = pcache_parse_query_delete( op->ore_reqdata,
3849 &tag, &op->o_req_ndn, uuidp,
3850 &rs->sr_text, op->o_tmpmemctx );
3851 if ( rs->sr_err != LDAP_SUCCESS ) {
3855 if ( LogTest( LDAP_DEBUG_STATS ) ) {
3856 assert( !BER_BVISNULL( &op->o_req_ndn ) );
3857 len = snprintf( buf, sizeof( buf ), " dn=\"%s\"", op->o_req_ndn.bv_val );
3859 if ( !BER_BVISNULL( &uuid ) && len < sizeof( buf ) ) {
3860 snprintf( &buf[ len ], sizeof( buf ) - len, " queryId=\"%s\"", uuid.bv_val );
3863 Debug( LDAP_DEBUG_STATS, "%s QUERY DELETE%s\n",
3864 op->o_log_prefix, buf, 0 );
3866 op->o_req_dn = op->o_req_ndn;
3868 op->o_bd = select_backend( &op->o_req_ndn, 0 );
3869 rs->sr_err = backend_check_restrictions( op, rs,
3870 (struct berval *)&pcache_exop_QUERY_DELETE );
3871 if ( rs->sr_err != LDAP_SUCCESS ) {
3875 if ( op->o_bd->be_extended == NULL ) {
3876 send_ldap_error( op, rs, LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
3877 "backend does not support extended operations" );
3881 op->o_bd->be_extended( op, rs );
3884 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
3885 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
3886 BER_BVZERO( &op->o_req_ndn );
3887 BER_BVZERO( &op->o_req_dn );
3890 if ( !BER_BVISNULL( &uuid ) ) {
3891 op->o_tmpfree( uuid.bv_val, op->o_tmpmemctx );
3900 pcache_op_extended( Operation *op, SlapReply *rs )
3902 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
3903 cache_manager *cm = on->on_bi.bi_private;
3905 #ifdef PCACHE_CONTROL_PRIVDB
3906 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL ) {
3907 return pcache_op_privdb( op, rs );
3909 #endif /* PCACHE_CONTROL_PRIVDB */
3911 if ( bvmatch( &op->ore_reqoid, &pcache_exop_QUERY_DELETE ) ) {
3912 struct berval uuid = BER_BVNULL;
3913 ber_tag_t tag = LBER_DEFAULT;
3915 rs->sr_err = pcache_parse_query_delete( op->ore_reqdata,
3916 &tag, NULL, &uuid, &rs->sr_text, op->o_tmpmemctx );
3917 assert( rs->sr_err == LDAP_SUCCESS );
3919 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_DN ) {
3920 /* remove all queries related to the selected entry */
3921 rs->sr_err = pcache_remove_entry_queries_from_cache( op,
3922 cm, &op->o_req_ndn, &uuid );
3924 } else if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_BASE ) {
3925 if ( !BER_BVISNULL( &uuid ) ) {
3926 /* remove the selected query */
3927 rs->sr_err = pcache_remove_query_from_cache( op,
3931 /* TODO: remove all queries */
3932 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
3933 rs->sr_text = "deletion of all queries not implemented";
3937 op->o_tmpfree( uuid.bv_val, op->o_tmpmemctx );
3942 #endif /* PCACHE_EXOP_QUERY_DELETE */
3944 static slap_overinst pcache;
3946 static char *obsolete_names[] = {
3951 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
3953 #endif /* SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC */
3958 struct berval debugbv = BER_BVC("pcache");
3960 code = slap_loglevel_get( &debugbv, &pcache_debug );
3965 #ifdef PCACHE_CONTROL_PRIVDB
3966 code = register_supported_control( PCACHE_CONTROL_PRIVDB,
3967 SLAP_CTRL_BIND|SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE, extops,
3968 parse_privdb_ctrl, &privDB_cid );
3969 if ( code != LDAP_SUCCESS ) {
3970 Debug( LDAP_DEBUG_ANY,
3971 "pcache_initialize: failed to register control %s (%d)\n",
3972 PCACHE_CONTROL_PRIVDB, code, 0 );
3975 #endif /* PCACHE_CONTROL_PRIVDB */
3977 #ifdef PCACHE_EXOP_QUERY_DELETE
3978 code = load_extop2( (struct berval *)&pcache_exop_QUERY_DELETE,
3979 SLAP_EXOP_WRITES|SLAP_EXOP_HIDE, pcache_exop_query_delete,
3981 if ( code != LDAP_SUCCESS ) {
3982 Debug( LDAP_DEBUG_ANY,
3983 "pcache_initialize: unable to register queryDelete exop: %d.\n",
3987 #endif /* PCACHE_EXOP_QUERY_DELETE */
3989 for ( i = 0; as[i].desc != NULL; i++ ) {
3990 code = register_at( as[i].desc, as[i].adp, 0 );
3992 Debug( LDAP_DEBUG_ANY,
3993 "pcache_initialize: register_at #%d failed\n", i, 0, 0 );
3996 (*as[i].adp)->ad_type->sat_flags |= SLAP_AT_HIDE;
3999 pcache.on_bi.bi_type = "pcache";
4000 pcache.on_bi.bi_obsolete_names = obsolete_names;
4001 pcache.on_bi.bi_db_init = pcache_db_init;
4002 pcache.on_bi.bi_db_config = pcache_db_config;
4003 pcache.on_bi.bi_db_open = pcache_db_open;
4004 pcache.on_bi.bi_db_close = pcache_db_close;
4005 pcache.on_bi.bi_db_destroy = pcache_db_destroy;
4007 pcache.on_bi.bi_op_search = pcache_op_search;
4008 #ifdef PCACHE_CONTROL_PRIVDB
4009 pcache.on_bi.bi_op_bind = pcache_op_privdb;
4010 pcache.on_bi.bi_op_compare = pcache_op_privdb;
4011 pcache.on_bi.bi_op_modrdn = pcache_op_privdb;
4012 pcache.on_bi.bi_op_modify = pcache_op_privdb;
4013 pcache.on_bi.bi_op_add = pcache_op_privdb;
4014 pcache.on_bi.bi_op_delete = pcache_op_privdb;
4015 #endif /* PCACHE_CONTROL_PRIVDB */
4016 #ifdef PCACHE_EXOP_QUERY_DELETE
4017 pcache.on_bi.bi_extended = pcache_op_extended;
4018 #elif defined( PCACHE_CONTROL_PRIVDB )
4019 pcache.on_bi.bi_extended = pcache_op_privdb;
4022 pcache.on_bi.bi_chk_controls = pcache_chk_controls;
4024 pcache.on_bi.bi_cf_ocs = pcocs;
4026 code = config_register_schema( pccfg, pcocs );
4027 if ( code ) return code;
4029 return overlay_register( &pcache );
4032 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
4033 int init_module(int argc, char *argv[]) {
4034 return pcache_initialize();
4038 #endif /* defined(SLAPD_OVER_PROXYCACHE) */