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 unsigned long answerable_cnt; /* how many times it was answerable */
79 ldap_pvt_thread_mutex_t answerable_cnt_mutex;
80 struct cached_query_s *next; /* next query in the template */
81 struct cached_query_s *prev; /* previous query in the template */
82 struct cached_query_s *lru_up; /* previous query in the LRU list */
83 struct cached_query_s *lru_down; /* next query in the LRU list */
84 ldap_pvt_thread_rdwr_t rwlock;
90 * ldap:///<base>??<scope>?<filter>?x-uuid=<uid>,x-template=<template>,x-attrset=<attrset>,x-expiry=<expiry>
92 * <base> ::= CachedQuery.qbase->base
93 * <scope> ::= CachedQuery.scope
94 * <filter> ::= filter2bv(CachedQuery.filter)
95 * <uuid> ::= CachedQuery.q_uuid
96 * <attrset> ::= CachedQuery.qtemp->attr_set_index
97 * <expiry> ::= CachedQuery.expiry_time
99 * quick hack: parse URI, call add_query() and then fix
100 * CachedQuery.expiry_time and CachedQuery.q_uuid
104 * Represents a set of projected attributes.
108 struct query_template_s *templates;
109 AttributeName* attrs; /* specifies the set */
111 #define PC_CONFIGURED (0x1)
112 #define PC_REFERENCED (0x2)
113 #define PC_GOT_OC (0x4)
114 int count; /* number of attributes */
117 /* struct representing a query template
118 * e.g. template string = &(cn=)(mail=)
120 typedef struct query_template_s {
121 struct query_template_s *qtnext;
122 struct query_template_s *qmnext;
125 CachedQuery* query; /* most recent query cached for the template */
126 CachedQuery* query_last; /* oldest query cached for the template */
127 ldap_pvt_thread_rdwr_t t_rwlock; /* Rd/wr lock for accessing queries in the template */
128 struct berval querystr; /* Filter string corresponding to the QT */
130 int attr_set_index; /* determines the projected attributes */
131 int no_of_queries; /* Total number of queries in the template */
132 time_t ttl; /* TTL for the queries of this template */
133 time_t negttl; /* TTL for negative results */
134 time_t limitttl; /* TTL for sizelimit exceeding results */
135 struct attr_set t_attrs; /* filter attrs + attr_set */
143 } pc_caching_reason_t;
145 static const char *pc_caching_reason_str[] = {
154 struct query_manager_s;
156 /* prototypes for functions for 1) query containment
157 * 2) query addition, 3) cache replacement
159 typedef CachedQuery *(QCfunc)(Operation *op, struct query_manager_s*,
160 Query*, QueryTemplate*);
161 typedef CachedQuery *(AddQueryfunc)(Operation *op, struct query_manager_s*,
162 Query*, QueryTemplate*, pc_caching_reason_t, int wlock);
163 typedef void (CRfunc)(struct query_manager_s*, struct berval*);
165 /* LDAP query cache */
166 typedef struct query_manager_s {
167 struct attr_set* attr_sets; /* possible sets of projected attributes */
168 QueryTemplate* templates; /* cacheable templates */
170 CachedQuery* lru_top; /* top and bottom of LRU list */
171 CachedQuery* lru_bottom;
173 ldap_pvt_thread_mutex_t lru_mutex; /* mutex for accessing LRU list */
175 /* Query cache methods */
176 QCfunc *qcfunc; /* Query containment*/
177 CRfunc *crfunc; /* cache replacement */
178 AddQueryfunc *addfunc; /* add query */
181 /* LDAP query cache manager */
182 typedef struct cache_manager_s {
183 BackendDB db; /* underlying database */
184 unsigned long num_cached_queries; /* total number of cached queries */
185 unsigned long max_queries; /* upper bound on # of cached queries */
186 int save_queries; /* save cached queries across restarts */
187 int check_cacheability; /* check whether a query is cacheable */
188 int numattrsets; /* number of attribute sets */
189 int cur_entries; /* current number of entries cached */
190 int max_entries; /* max number of entries cached */
191 int num_entries_limit; /* max # of entries in a cacheable query */
193 char response_cb; /* install the response callback
194 * at the tail of the callback list */
195 #define PCACHE_RESPONSE_CB_HEAD 0
196 #define PCACHE_RESPONSE_CB_TAIL 1
197 char defer_db_open; /* defer open for online add */
199 time_t cc_period; /* interval between successive consistency checks (sec) */
200 #define PCACHE_CC_PAUSED 1
201 #define PCACHE_CC_OFFLINE 2
205 ldap_pvt_thread_mutex_t cache_mutex;
207 query_manager* qm; /* query cache managed by the cache manager */
210 static int pcache_debug;
212 #ifdef PCACHE_CONTROL_PRIVDB
213 static int privDB_cid;
214 #endif /* PCACHE_CONTROL_PRIVDB */
216 static AttributeDescription *ad_queryId, *ad_cachedQueryURL;
219 AttributeDescription **adp;
221 { "( 1.3.6.1.4.1.4203.666.11.9.1.1 "
223 "DESC 'ID of query the entry belongs to, formatted as a UUID' "
224 "EQUALITY octetStringMatch "
225 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40{64} "
226 "NO-USER-MODIFICATION "
227 "USAGE directoryOperation )",
229 { "( 1.3.6.1.4.1.4203.666.11.9.1.2 "
230 "NAME 'cachedQueryURL' "
231 "DESC 'URI describing a cached query' "
232 "EQUALITY caseExactMatch "
233 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 "
234 "NO-USER-MODIFICATION "
235 "USAGE directoryOperation )",
236 &ad_cachedQueryURL },
245 AttributeName** filter_attrs,
247 int* filter_got_oc );
254 QueryTemplate *templ,
255 pc_caching_reason_t why,
262 struct berval *query_uuid );
265 * Turn a cached query into its URL representation
268 query2url( Operation *op, CachedQuery *q, struct berval *urlbv, int dolock )
270 struct berval bv_scope,
272 char attrset_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
273 expiry_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
274 answerable_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
276 ber_len_t attrset_len,
281 ldap_pvt_thread_rdwr_rlock( &q->rwlock );
284 ldap_pvt_scope2bv( q->scope, &bv_scope );
285 filter2bv_x( op, q->filter, &bv_filter );
286 attrset_len = sprintf( attrset_buf,
287 "%lu", (unsigned long)q->qtemp->attr_set_index );
288 expiry_len = sprintf( expiry_buf,
289 "%lu", (unsigned long)q->expiry_time );
290 answerable_len = snprintf( answerable_buf, sizeof( answerable_buf ),
291 "%lu", q->answerable_cnt );
293 urlbv->bv_len = STRLENOF( "ldap:///" )
294 + q->qbase->base.bv_len
299 + STRLENOF( "?x-uuid=" )
301 + STRLENOF( ",x-attrset=" )
303 + STRLENOF( ",x-expiry=" )
305 + STRLENOF( ",x-answerable=" )
307 ptr = urlbv->bv_val = ber_memalloc_x( urlbv->bv_len + 1, op->o_tmpmemctx );
308 ptr = lutil_strcopy( ptr, "ldap:///" );
309 ptr = lutil_strcopy( ptr, q->qbase->base.bv_val );
310 ptr = lutil_strcopy( ptr, "??" );
311 ptr = lutil_strcopy( ptr, bv_scope.bv_val );
312 ptr = lutil_strcopy( ptr, "?" );
313 ptr = lutil_strcopy( ptr, bv_filter.bv_val );
314 ptr = lutil_strcopy( ptr, "?x-uuid=" );
315 ptr = lutil_strcopy( ptr, q->q_uuid.bv_val );
316 ptr = lutil_strcopy( ptr, ",x-attrset=" );
317 ptr = lutil_strcopy( ptr, attrset_buf );
318 ptr = lutil_strcopy( ptr, ",x-expiry=" );
319 ptr = lutil_strcopy( ptr, expiry_buf );
320 ptr = lutil_strcopy( ptr, ",x-answerable=" );
321 ptr = lutil_strcopy( ptr, answerable_buf );
323 ber_memfree_x( bv_filter.bv_val, op->o_tmpmemctx );
326 ldap_pvt_thread_rdwr_runlock( &q->rwlock );
333 * Turn an URL representing a formerly cached query into a cached query,
334 * and try to cache it
345 LDAPURLDesc *lud = NULL;
347 tempstr = BER_BVNULL,
351 unsigned long answerable_cnt;
354 #define GOT_UUID 0x1U
355 #define GOT_ATTRSET 0x2U
356 #define GOT_EXPIRY 0x4U
357 #define GOT_ANSWERABLE 0x8U
358 #define GOT_ALL (GOT_UUID|GOT_ATTRSET|GOT_EXPIRY|GOT_ANSWERABLE)
361 rc = ldap_url_parse( url, &lud );
362 if ( rc != LDAP_URL_SUCCESS ) {
366 /* non-allowed fields */
367 if ( lud->lud_host != NULL ) {
372 if ( lud->lud_attrs != NULL ) {
378 if ( strcmp( lud->lud_scheme, "ldap" ) != 0 ) {
383 /* required fields */
384 if ( lud->lud_dn == NULL || lud->lud_dn[ 0 ] == '\0' ) {
389 switch ( lud->lud_scope ) {
390 case LDAP_SCOPE_BASE:
391 case LDAP_SCOPE_ONELEVEL:
392 case LDAP_SCOPE_SUBTREE:
393 case LDAP_SCOPE_SUBORDINATE:
401 if ( lud->lud_filter == NULL || lud->lud_filter[ 0 ] == '\0' ) {
406 if ( lud->lud_exts == NULL ) {
411 for ( i = 0; lud->lud_exts[ i ] != NULL; i++ ) {
412 if ( strncmp( lud->lud_exts[ i ], "x-uuid=", STRLENOF( "x-uuid=" ) ) == 0 ) {
413 struct berval tmpUUID;
414 Syntax *syn_UUID = slap_schema.si_ad_entryUUID->ad_type->sat_syntax;
416 if ( got & GOT_UUID ) {
421 ber_str2bv( &lud->lud_exts[ i ][ STRLENOF( "x-uuid=" ) ], 0, 0, &tmpUUID );
422 rc = syn_UUID->ssyn_pretty( syn_UUID, &tmpUUID, &uuid, NULL );
423 if ( rc != LDAP_SUCCESS ) {
428 } else if ( strncmp( lud->lud_exts[ i ], "x-attrset=", STRLENOF( "x-attrset=" ) ) == 0 ) {
429 if ( got & GOT_ATTRSET ) {
434 rc = lutil_atoi( &attrset, &lud->lud_exts[ i ][ STRLENOF( "x-attrset=" ) ] );
440 } else if ( strncmp( lud->lud_exts[ i ], "x-expiry=", STRLENOF( "x-expiry=" ) ) == 0 ) {
443 if ( got & GOT_EXPIRY ) {
448 rc = lutil_atoul( &l, &lud->lud_exts[ i ][ STRLENOF( "x-expiry=" ) ] );
452 expiry_time = (time_t)l;
455 } else if ( strncmp( lud->lud_exts[ i ], "x-answerable=", STRLENOF( "x-answerable=" ) ) == 0 ) {
456 if ( got & GOT_ANSWERABLE ) {
461 rc = lutil_atoul( &answerable_cnt, &lud->lud_exts[ i ][ STRLENOF( "x-answerable=" ) ] );
465 got |= GOT_ANSWERABLE;
473 if ( got != GOT_ALL ) {
478 /* ignore expired queries */
479 if ( expiry_time <= slap_get_time()) {
481 SlapReply rs2 = { 0 };
483 memset( &op2.oq_search, 0, sizeof( op2.oq_search ) );
485 (void)remove_query_data( &op2, &rs2, &uuid );
490 ber_str2bv( lud->lud_dn, 0, 0, &base );
491 rc = dnNormalize( 0, NULL, NULL, &base, &query.base, NULL );
492 if ( rc != LDAP_SUCCESS ) {
495 query.scope = lud->lud_scope;
496 query.filter = str2filter( lud->lud_filter );
498 tempstr.bv_val = ch_malloc( strlen( lud->lud_filter ) + 1 );
500 if ( filter2template( op, query.filter, &tempstr, NULL, NULL, NULL ) ) {
501 ch_free( tempstr.bv_val );
506 /* check for query containment */
507 qt = qm->attr_sets[attrset].templates;
508 for ( ; qt; qt = qt->qtnext ) {
509 /* find if template i can potentially answer tempstr */
510 if ( bvmatch( &qt->querystr, &tempstr ) ) {
520 cq = add_query( op, qm, &query, qt, PC_POSITIVE, 0 );
522 cq->expiry_time = expiry_time;
524 cq->answerable_cnt = answerable_cnt;
526 /* it's now into cq->filter */
536 if ( query.filter != NULL ) filter_free( query.filter );
537 if ( !BER_BVISNULL( &tempstr ) ) ch_free( tempstr.bv_val );
538 if ( !BER_BVISNULL( &query.base ) ) ch_free( query.base.bv_val );
539 if ( !BER_BVISNULL( &uuid ) ) ch_free( uuid.bv_val );
540 if ( lud != NULL ) ldap_free_urldesc( lud );
545 /* Return 1 for an added entry, else 0 */
550 struct berval* query_uuid )
553 Modifications* modlist = NULL;
554 const char* text = NULL;
556 char textbuf[SLAP_TEXT_BUFLEN];
557 size_t textlen = sizeof(textbuf);
559 SlapReply sreply = {REP_RESULT};
561 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
566 /* add queryId attribute */
567 attr_merge_one( e, ad_queryId, query_uuid, NULL );
569 /* append the attribute list from the fetched entry */
570 e->e_attrs->a_next = attr;
572 op->o_tag = LDAP_REQ_ADD;
573 op->o_protocol = LDAP_VERSION3;
574 op->o_callback = &cb;
575 op->o_time = slap_get_time();
576 op->o_do_not_cache = 1;
579 op->o_req_dn = e->e_name;
580 op->o_req_ndn = e->e_nname;
581 rc = op->o_bd->be_add( op, &sreply );
583 if ( rc != LDAP_SUCCESS ) {
584 if ( rc == LDAP_ALREADY_EXISTS ) {
585 slap_entry2mods( e, &modlist, &text, textbuf, textlen );
586 modlist->sml_op = LDAP_MOD_ADD;
587 op->o_tag = LDAP_REQ_MODIFY;
588 op->orm_modlist = modlist;
589 op->o_bd->be_modify( op, &sreply );
590 slap_mods_free( modlist, 1 );
591 } else if ( rc == LDAP_REFERRAL ||
592 rc == LDAP_NO_SUCH_OBJECT ) {
593 syncrepl_add_glue( op, e );
602 if ( op->ora_e == e )
603 be_entry_release_w( op, e );
610 /* Length-ordered sort on normalized DNs */
611 static int pcache_dn_cmp( const void *v1, const void *v2 )
613 const Qbase *q1 = v1, *q2 = v2;
615 int rc = q1->base.bv_len - q2->base.bv_len;
617 rc = strncmp( q1->base.bv_val, q2->base.bv_val, q1->base.bv_len );
621 static int lex_bvcmp( struct berval *bv1, struct berval *bv2 )
624 dif = bv1->bv_len - bv2->bv_len;
626 if ( dif > 0 ) len -= dif;
627 len = memcmp( bv1->bv_val, bv2->bv_val, len );
633 /* compare the current value in each filter */
634 static int pcache_filter_cmp( Filter *f1, Filter *f2 )
636 int rc, weight1, weight2;
638 switch( f1->f_choice ) {
639 case LDAP_FILTER_PRESENT:
642 case LDAP_FILTER_EQUALITY:
650 switch( f2->f_choice ) {
651 case LDAP_FILTER_PRESENT:
654 case LDAP_FILTER_EQUALITY:
662 rc = weight1 - weight2;
668 rc = lex_bvcmp( &f1->f_av_value, &f2->f_av_value );
671 if ( f1->f_choice == LDAP_FILTER_SUBSTRINGS ) {
673 if ( !BER_BVISNULL( &f1->f_sub_initial )) {
674 if ( !BER_BVISNULL( &f2->f_sub_initial )) {
675 rc = lex_bvcmp( &f1->f_sub_initial,
676 &f2->f_sub_initial );
680 } else if ( !BER_BVISNULL( &f2->f_sub_initial )) {
684 if ( f1->f_sub_any ) {
685 if ( f2->f_sub_any ) {
686 rc = lex_bvcmp( f1->f_sub_any,
691 } else if ( f2->f_sub_any ) {
695 if ( !BER_BVISNULL( &f1->f_sub_final )) {
696 if ( !BER_BVISNULL( &f2->f_sub_final )) {
697 rc = lex_bvcmp( &f1->f_sub_final,
702 } else if ( !BER_BVISNULL( &f2->f_sub_final )) {
706 rc = lex_bvcmp( &f1->f_mr_value,
720 while ( f1->f_choice == LDAP_FILTER_AND || f1->f_choice == LDAP_FILTER_OR )
722 while ( f2->f_choice == LDAP_FILTER_AND || f2->f_choice == LDAP_FILTER_OR )
724 rc = pcache_filter_cmp( f1, f2 );
732 /* compare filters in each query */
733 static int pcache_query_cmp( const void *v1, const void *v2 )
735 const CachedQuery *q1 = v1, *q2 =v2;
736 return pcache_filter_cmp( q1->first, q2->first );
739 /* add query on top of LRU list */
741 add_query_on_top (query_manager* qm, CachedQuery* qc)
743 CachedQuery* top = qm->lru_top;
754 Debug( pcache_debug, "Base of added query = %s\n",
755 qc->qbase->base.bv_val, 0, 0 );
758 /* remove_query from LRU list */
761 remove_query (query_manager* qm, CachedQuery* qc)
784 qc->lru_up = qc->lru_down = NULL;
787 /* find and remove string2 from string1
788 * from start if position = 1,
789 * from end if position = 3,
790 * from anywhere if position = 2
791 * string1 is overwritten if position = 2.
795 find_and_remove(struct berval* ber1, struct berval* ber2, int position)
806 if ( ber1->bv_len >= ber2->bv_len && !memcmp( ber1->bv_val,
807 ber2->bv_val, ber2->bv_len )) {
809 ber1->bv_val += ber2->bv_len;
810 ber1->bv_len -= ber2->bv_len;
815 ber1->bv_val[ber1->bv_len] = '\0';
816 temp = strstr( ber1->bv_val, ber2->bv_val );
818 strcpy( temp, temp+ber2->bv_len );
819 ber1->bv_len -= ber2->bv_len;
825 if ( ber1->bv_len >= ber2->bv_len &&
826 !memcmp( ber1->bv_val+ber1->bv_len-ber2->bv_len, ber2->bv_val,
829 ber1->bv_len -= ber2->bv_len;
837 static struct berval*
838 merge_init_final(Operation *op, struct berval* init, struct berval* any,
839 struct berval* final)
841 struct berval* merged, *temp;
842 int i, any_count, count;
844 for (any_count=0; any && any[any_count].bv_val; any_count++)
854 merged = (struct berval*)op->o_tmpalloc( (count+1)*sizeof(struct berval),
859 ber_dupbv_x( temp, init, op->o_tmpmemctx );
863 for (i=0; i<any_count; i++) {
864 ber_dupbv_x( temp, any, op->o_tmpmemctx );
869 ber_dupbv_x( temp, final, op->o_tmpmemctx );
876 /* Each element in stored must be found in incoming. Incoming is overwritten.
879 strings_containment(struct berval* stored, struct berval* incoming)
881 struct berval* element;
885 for ( element=stored; element->bv_val != NULL; element++ ) {
886 for (j = k; incoming[j].bv_val != NULL; j++) {
887 if (find_and_remove(&(incoming[j]), element, 2)) {
904 substr_containment_substr(Operation *op, Filter* stored, Filter* incoming)
908 struct berval init_incoming;
909 struct berval final_incoming;
910 struct berval *remaining_incoming = NULL;
912 if ((!(incoming->f_sub_initial.bv_val) && (stored->f_sub_initial.bv_val))
913 || (!(incoming->f_sub_final.bv_val) && (stored->f_sub_final.bv_val)))
916 init_incoming = incoming->f_sub_initial;
917 final_incoming = incoming->f_sub_final;
919 if (find_and_remove(&init_incoming,
920 &(stored->f_sub_initial), 1) && find_and_remove(&final_incoming,
921 &(stored->f_sub_final), 3))
923 if (stored->f_sub_any == NULL) {
927 remaining_incoming = merge_init_final(op, &init_incoming,
928 incoming->f_sub_any, &final_incoming);
929 rc = strings_containment(stored->f_sub_any, remaining_incoming);
930 ber_bvarray_free_x( remaining_incoming, op->o_tmpmemctx );
937 substr_containment_equality(Operation *op, Filter* stored, Filter* incoming)
939 struct berval incoming_val[2];
942 incoming_val[1] = incoming->f_av_value;
944 if (find_and_remove(incoming_val+1,
945 &(stored->f_sub_initial), 1) && find_and_remove(incoming_val+1,
946 &(stored->f_sub_final), 3)) {
947 if (stored->f_sub_any == NULL){
951 ber_dupbv_x( incoming_val, incoming_val+1, op->o_tmpmemctx );
952 BER_BVZERO( incoming_val+1 );
953 rc = strings_containment(stored->f_sub_any, incoming_val);
954 op->o_tmpfree( incoming_val[0].bv_val, op->o_tmpmemctx );
961 filter_first( Filter *f )
963 while ( f->f_choice == LDAP_FILTER_OR || f->f_choice == LDAP_FILTER_AND )
970 find_filter( Operation *op, Avlnode *root, Filter *inputf, Filter *first )
974 MatchingRule* mrule = NULL;
975 int res=0, eqpass= 0;
983 /* substring matches sort to the end, and we just have to
984 * walk the entire list.
986 if ( first->f_choice == LDAP_FILTER_SUBSTRINGS ) {
987 ptr = tavl_end( root, 1 );
990 ptr = tavl_find3( root, &cq, pcache_query_cmp, &ret );
991 dir = (first->f_choice == LDAP_FILTER_GE) ? TAVL_DIR_LEFT :
1000 /* an incoming substr query can only be satisfied by a cached
1003 if ( first->f_choice == LDAP_FILTER_SUBSTRINGS &&
1004 qc->first->f_choice != LDAP_FILTER_SUBSTRINGS )
1007 /* an incoming eq query can be satisfied by a cached eq or substr
1010 if ( first->f_choice == LDAP_FILTER_EQUALITY ) {
1011 if ( eqpass == 0 ) {
1012 if ( qc->first->f_choice != LDAP_FILTER_EQUALITY ) {
1013 nextpass: eqpass = 1;
1014 ptr = tavl_end( root, 1 );
1015 dir = TAVL_DIR_LEFT;
1019 if ( qc->first->f_choice != LDAP_FILTER_SUBSTRINGS )
1025 switch (fs->f_choice) {
1026 case LDAP_FILTER_EQUALITY:
1027 if (fi->f_choice == LDAP_FILTER_EQUALITY)
1028 mrule = fs->f_ava->aa_desc->ad_type->sat_equality;
1032 case LDAP_FILTER_GE:
1033 case LDAP_FILTER_LE:
1034 mrule = fs->f_ava->aa_desc->ad_type->sat_ordering;
1041 rc = value_match(&ret, fs->f_ava->aa_desc, mrule,
1042 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
1043 &(fi->f_ava->aa_value),
1044 &(fs->f_ava->aa_value), &text);
1045 if (rc != LDAP_SUCCESS) {
1048 if ( fi==first && fi->f_choice==LDAP_FILTER_EQUALITY && ret )
1051 switch (fs->f_choice) {
1052 case LDAP_FILTER_OR:
1053 case LDAP_FILTER_AND:
1058 case LDAP_FILTER_SUBSTRINGS:
1059 /* check if the equality query can be
1060 * answered with cached substring query */
1061 if ((fi->f_choice == LDAP_FILTER_EQUALITY)
1062 && substr_containment_equality( op,
1065 /* check if the substring query can be
1066 * answered with cached substring query */
1067 if ((fi->f_choice ==LDAP_FILTER_SUBSTRINGS
1068 ) && substr_containment_substr( op,
1074 case LDAP_FILTER_PRESENT:
1079 case LDAP_FILTER_EQUALITY:
1085 case LDAP_FILTER_GE:
1086 if (mrule && ret >= 0)
1091 case LDAP_FILTER_LE:
1092 if (mrule && ret <= 0)
1097 case LDAP_FILTER_NOT:
1103 } while((res) && (fi != NULL) && (fs != NULL));
1107 ptr = tavl_next( ptr, dir );
1112 /* check whether query is contained in any of
1113 * the cached queries in template
1115 static CachedQuery *
1116 query_containment(Operation *op, query_manager *qm,
1118 QueryTemplate *templa)
1121 int depth = 0, tscope;
1122 Qbase qbase, *qbptr = NULL;
1125 if (query->filter != NULL) {
1128 Debug( pcache_debug, "Lock QC index = %p\n",
1129 (void *) templa, 0, 0 );
1130 qbase.base = query->base;
1132 first = filter_first( query->filter );
1134 ldap_pvt_thread_rdwr_rlock(&templa->t_rwlock);
1137 qbptr = avl_find( templa->qbase, &qbase, pcache_dn_cmp );
1139 tscope = query->scope;
1140 /* Find a matching scope:
1141 * match at depth 0 OK
1144 * subord at depth > 0 OK
1145 * subtree at any depth OK
1147 * subtree or subord at any depth OK
1149 * subtree or subord at any depth OK
1151 * subord at depth > 0 OK
1152 * subtree at any depth OK
1154 for ( tscope = 0 ; tscope <= LDAP_SCOPE_CHILDREN; tscope++ ) {
1155 switch ( query->scope ) {
1156 case LDAP_SCOPE_BASE:
1157 if ( tscope == LDAP_SCOPE_BASE && depth ) continue;
1158 if ( tscope == LDAP_SCOPE_ONE && depth != 1) continue;
1159 if ( tscope == LDAP_SCOPE_CHILDREN && !depth ) continue;
1161 case LDAP_SCOPE_ONE:
1162 if ( tscope == LDAP_SCOPE_BASE )
1163 tscope = LDAP_SCOPE_ONE;
1164 if ( tscope == LDAP_SCOPE_ONE && depth ) continue;
1165 if ( !depth ) break;
1166 if ( tscope < LDAP_SCOPE_SUBTREE )
1167 tscope = LDAP_SCOPE_SUBTREE;
1169 case LDAP_SCOPE_SUBTREE:
1170 if ( tscope < LDAP_SCOPE_SUBTREE )
1171 tscope = LDAP_SCOPE_SUBTREE;
1172 if ( tscope == LDAP_SCOPE_CHILDREN && !depth ) continue;
1174 case LDAP_SCOPE_CHILDREN:
1175 if ( tscope < LDAP_SCOPE_SUBTREE )
1176 tscope = LDAP_SCOPE_SUBTREE;
1179 if ( !qbptr->scopes[tscope] ) continue;
1182 qc = find_filter( op, qbptr->scopes[tscope],
1183 query->filter, first );
1185 if ( qc->q_sizelimit ) {
1186 ldap_pvt_thread_rdwr_runlock(&templa->t_rwlock);
1189 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1190 if (qm->lru_top != qc) {
1191 remove_query(qm, qc);
1192 add_query_on_top(qm, qc);
1194 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1199 if ( be_issuffix( op->o_bd, &qbase.base ))
1202 dnParent( &qbase.base, &pdn );
1207 Debug( pcache_debug,
1208 "Not answerable: Unlock QC index=%p\n",
1209 (void *) templa, 0, 0 );
1210 ldap_pvt_thread_rdwr_runlock(&templa->t_rwlock);
1216 free_query (CachedQuery* qc)
1218 free(qc->q_uuid.bv_val);
1219 filter_free(qc->filter);
1220 ldap_pvt_thread_mutex_destroy(&qc->answerable_cnt_mutex);
1221 ldap_pvt_thread_rdwr_destroy( &qc->rwlock );
1222 memset(qc, 0, sizeof(*qc));
1227 /* Add query to query cache, the returned Query is locked for writing */
1228 static CachedQuery *
1233 QueryTemplate *templ,
1234 pc_caching_reason_t why,
1237 CachedQuery* new_cached_query = (CachedQuery*) ch_malloc(sizeof(CachedQuery));
1243 new_cached_query->qtemp = templ;
1244 BER_BVZERO( &new_cached_query->q_uuid );
1245 new_cached_query->q_sizelimit = 0;
1253 ttl = templ->negttl;
1257 ttl = templ->limitttl;
1264 new_cached_query->expiry_time = slap_get_time() + ttl;
1266 new_cached_query->answerable_cnt = 0;
1267 ldap_pvt_thread_mutex_init(&new_cached_query->answerable_cnt_mutex);
1269 new_cached_query->lru_up = NULL;
1270 new_cached_query->lru_down = NULL;
1271 Debug( pcache_debug, "Added query expires at %ld (%s)\n",
1272 (long) new_cached_query->expiry_time,
1273 pc_caching_reason_str[ why ], 0 );
1275 new_cached_query->scope = query->scope;
1276 new_cached_query->filter = query->filter;
1277 new_cached_query->first = first = filter_first( query->filter );
1279 ldap_pvt_thread_rdwr_init(&new_cached_query->rwlock);
1281 ldap_pvt_thread_rdwr_wlock(&new_cached_query->rwlock);
1283 qb.base = query->base;
1285 /* Adding a query */
1286 Debug( pcache_debug, "Lock AQ index = %p\n",
1287 (void *) templ, 0, 0 );
1288 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
1289 qbase = avl_find( templ->qbase, &qb, pcache_dn_cmp );
1291 qbase = ch_calloc( 1, sizeof(Qbase) + qb.base.bv_len + 1 );
1292 qbase->base.bv_len = qb.base.bv_len;
1293 qbase->base.bv_val = (char *)(qbase+1);
1294 memcpy( qbase->base.bv_val, qb.base.bv_val, qb.base.bv_len );
1295 qbase->base.bv_val[qbase->base.bv_len] = '\0';
1296 avl_insert( &templ->qbase, qbase, pcache_dn_cmp, avl_dup_error );
1298 new_cached_query->next = templ->query;
1299 new_cached_query->prev = NULL;
1300 new_cached_query->qbase = qbase;
1301 rc = tavl_insert( &qbase->scopes[query->scope], new_cached_query,
1302 pcache_query_cmp, avl_dup_error );
1305 if (templ->query == NULL)
1306 templ->query_last = new_cached_query;
1308 templ->query->prev = new_cached_query;
1309 templ->query = new_cached_query;
1310 templ->no_of_queries++;
1312 ch_free( new_cached_query );
1313 new_cached_query = find_filter( op, qbase->scopes[query->scope],
1314 query->filter, first );
1315 filter_free( query->filter );
1316 query->filter = NULL;
1318 Debug( pcache_debug, "TEMPLATE %p QUERIES++ %d\n",
1319 (void *) templ, templ->no_of_queries, 0 );
1321 Debug( pcache_debug, "Unlock AQ index = %p \n",
1322 (void *) templ, 0, 0 );
1323 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
1325 /* Adding on top of LRU list */
1327 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1328 add_query_on_top(qm, new_cached_query);
1329 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1331 return rc == 0 ? new_cached_query : NULL;
1335 remove_from_template (CachedQuery* qc, QueryTemplate* template)
1337 if (!qc->prev && !qc->next) {
1338 template->query_last = template->query = NULL;
1339 } else if (qc->prev == NULL) {
1340 qc->next->prev = NULL;
1341 template->query = qc->next;
1342 } else if (qc->next == NULL) {
1343 qc->prev->next = NULL;
1344 template->query_last = qc->prev;
1346 qc->next->prev = qc->prev;
1347 qc->prev->next = qc->next;
1349 tavl_delete( &qc->qbase->scopes[qc->scope], qc, pcache_query_cmp );
1350 qc->qbase->queries--;
1351 if ( qc->qbase->queries == 0 ) {
1352 avl_delete( &template->qbase, qc->qbase, pcache_dn_cmp );
1353 ch_free( qc->qbase );
1357 template->no_of_queries--;
1360 /* remove bottom query of LRU list from the query cache */
1362 * NOTE: slight change in functionality.
1364 * - if result->bv_val is NULL, the query at the bottom of the LRU
1366 * - otherwise, the query whose UUID is *result is removed
1367 * - if not found, result->bv_val is zeroed
1370 cache_replacement(query_manager* qm, struct berval *result)
1372 CachedQuery* bottom;
1373 QueryTemplate *temp;
1375 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1376 if ( BER_BVISNULL( result ) ) {
1377 bottom = qm->lru_bottom;
1380 Debug ( pcache_debug,
1381 "Cache replacement invoked without "
1382 "any query in LRU list\n", 0, 0, 0 );
1383 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1388 for ( bottom = qm->lru_bottom;
1390 bottom = bottom->lru_up )
1392 if ( bvmatch( result, &bottom->q_uuid ) ) {
1398 Debug ( pcache_debug,
1399 "Could not find query with uuid=\"%s\""
1400 "in LRU list\n", result->bv_val, 0, 0 );
1401 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1402 BER_BVZERO( result );
1407 temp = bottom->qtemp;
1408 remove_query(qm, bottom);
1409 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1411 *result = bottom->q_uuid;
1412 BER_BVZERO( &bottom->q_uuid );
1414 Debug( pcache_debug, "Lock CR index = %p\n", (void *) temp, 0, 0 );
1415 ldap_pvt_thread_rdwr_wlock(&temp->t_rwlock);
1416 remove_from_template(bottom, temp);
1417 Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
1418 (void *) temp, temp->no_of_queries, 0 );
1419 Debug( pcache_debug, "Unlock CR index = %p\n", (void *) temp, 0, 0 );
1420 ldap_pvt_thread_rdwr_wunlock(&temp->t_rwlock);
1425 struct query_info *next;
1437 struct query_info *qi;
1440 if ( rs->sr_type != REP_SEARCH ) return 0;
1442 attr = attr_find( rs->sr_entry->e_attrs, ad_queryId );
1443 if ( attr == NULL ) return 0;
1445 count = attr->a_numvals;
1446 assert( count > 0 );
1447 qi = op->o_tmpalloc( sizeof( struct query_info ), op->o_tmpmemctx );
1448 qi->next = op->o_callback->sc_private;
1449 op->o_callback->sc_private = qi;
1450 ber_dupbv_x( &qi->xdn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1451 qi->del = ( count == 1 );
1460 struct berval *query_uuid )
1462 struct query_info *qi, *qnext;
1463 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(queryId=)" ) ];
1464 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
1465 Filter filter = {LDAP_FILTER_EQUALITY};
1466 SlapReply sreply = {REP_RESULT};
1467 slap_callback cb = { NULL, remove_func, NULL, NULL };
1470 sreply.sr_entry = NULL;
1471 sreply.sr_nentries = 0;
1472 op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
1473 "(%s=%s)", ad_queryId->ad_cname.bv_val, query_uuid->bv_val);
1474 filter.f_ava = &ava;
1475 filter.f_av_desc = ad_queryId;
1476 filter.f_av_value = *query_uuid;
1478 op->o_tag = LDAP_REQ_SEARCH;
1479 op->o_protocol = LDAP_VERSION3;
1480 op->o_callback = &cb;
1481 op->o_time = slap_get_time();
1482 op->o_do_not_cache = 1;
1484 op->o_req_dn = op->o_bd->be_suffix[0];
1485 op->o_req_ndn = op->o_bd->be_nsuffix[0];
1486 op->ors_scope = LDAP_SCOPE_SUBTREE;
1487 op->ors_deref = LDAP_DEREF_NEVER;
1488 op->ors_slimit = SLAP_NO_LIMIT;
1489 op->ors_tlimit = SLAP_NO_LIMIT;
1490 op->ors_filter = &filter;
1491 op->ors_filterstr.bv_val = filter_str;
1492 op->ors_filterstr.bv_len = strlen(filter_str);
1493 op->ors_attrs = NULL;
1494 op->ors_attrsonly = 0;
1496 op->o_bd->be_search( op, &sreply );
1498 for ( qi=cb.sc_private; qi; qi=qnext ) {
1501 op->o_req_dn = qi->xdn;
1502 op->o_req_ndn = qi->xdn;
1505 Debug( pcache_debug, "DELETING ENTRY TEMPLATE=%s\n",
1506 query_uuid->bv_val, 0, 0 );
1508 op->o_tag = LDAP_REQ_DELETE;
1510 if (op->o_bd->be_delete(op, &sreply) == LDAP_SUCCESS) {
1516 struct berval vals[2];
1518 vals[0] = *query_uuid;
1519 vals[1].bv_val = NULL;
1521 mod.sml_op = LDAP_MOD_DELETE;
1523 mod.sml_desc = ad_queryId;
1524 mod.sml_type = ad_queryId->ad_cname;
1525 mod.sml_values = vals;
1526 mod.sml_nvalues = NULL;
1527 mod.sml_numvals = 1;
1528 mod.sml_next = NULL;
1529 Debug( pcache_debug,
1530 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
1531 query_uuid->bv_val, 0, 0 );
1533 op->orm_modlist = &mod;
1535 op->o_bd->be_modify( op, &sreply );
1537 op->o_tmpfree( qi->xdn.bv_val, op->o_tmpmemctx );
1538 op->o_tmpfree( qi, op->o_tmpmemctx );
1545 AttributeName* attrs,
1554 struct berval *fstr,
1555 AttributeName** filter_attrs,
1557 int* filter_got_oc )
1559 AttributeDescription *ad;
1562 switch ( f->f_choice ) {
1563 case LDAP_FILTER_EQUALITY:
1565 len = STRLENOF( "(=)" ) + ad->ad_cname.bv_len;
1566 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=)", ad->ad_cname.bv_val );
1567 assert( ret == len );
1568 fstr->bv_len += len;
1571 case LDAP_FILTER_GE:
1573 len = STRLENOF( "(>=)" ) + ad->ad_cname.bv_len;
1574 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s>=)", ad->ad_cname.bv_val);
1575 assert( ret == len );
1576 fstr->bv_len += len;
1579 case LDAP_FILTER_LE:
1581 len = STRLENOF( "(<=)" ) + ad->ad_cname.bv_len;
1582 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s<=)", ad->ad_cname.bv_val);
1583 assert( ret == len );
1584 fstr->bv_len += len;
1587 case LDAP_FILTER_APPROX:
1589 len = STRLENOF( "(~=)" ) + ad->ad_cname.bv_len;
1590 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s~=)", ad->ad_cname.bv_val);
1591 assert( ret == len );
1592 fstr->bv_len += len;
1595 case LDAP_FILTER_SUBSTRINGS:
1597 len = STRLENOF( "(=)" ) + ad->ad_cname.bv_len;
1598 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=)", ad->ad_cname.bv_val );
1599 assert( ret == len );
1600 fstr->bv_len += len;
1603 case LDAP_FILTER_PRESENT:
1605 len = STRLENOF( "(=*)" ) + ad->ad_cname.bv_len;
1606 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=*)", ad->ad_cname.bv_val );
1607 assert( ret == len );
1608 fstr->bv_len += len;
1611 case LDAP_FILTER_AND:
1612 case LDAP_FILTER_OR:
1613 case LDAP_FILTER_NOT: {
1615 fstr->bv_val[fstr->bv_len++] = '(';
1616 switch ( f->f_choice ) {
1617 case LDAP_FILTER_AND:
1618 fstr->bv_val[fstr->bv_len] = '&';
1620 case LDAP_FILTER_OR:
1621 fstr->bv_val[fstr->bv_len] = '|';
1623 case LDAP_FILTER_NOT:
1624 fstr->bv_val[fstr->bv_len] = '!';
1629 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1630 rc = filter2template( op, f, fstr, filter_attrs, filter_cnt,
1634 fstr->bv_val[fstr->bv_len++] = ')';
1635 fstr->bv_val[fstr->bv_len] = '\0';
1641 /* a filter should at least have room for "()",
1642 * an "=" and for a 1-char attr */
1643 strcpy( fstr->bv_val, "(?=)" );
1644 fstr->bv_len += STRLENOF("(?=)");
1648 if ( filter_attrs != NULL ) {
1649 *filter_attrs = (AttributeName *)op->o_tmprealloc(*filter_attrs,
1650 (*filter_cnt + 2)*sizeof(AttributeName), op->o_tmpmemctx);
1652 (*filter_attrs)[*filter_cnt].an_desc = ad;
1653 (*filter_attrs)[*filter_cnt].an_name = ad->ad_cname;
1654 (*filter_attrs)[*filter_cnt].an_oc = NULL;
1655 (*filter_attrs)[*filter_cnt].an_flags = 0;
1656 BER_BVZERO( &(*filter_attrs)[*filter_cnt+1].an_name );
1658 if ( ad == slap_schema.si_ad_objectClass )
1665 struct search_info {
1668 QueryTemplate *qtemp;
1669 AttributeName* save_attrs; /* original attributes, saved for response */
1670 int swap_saved_attrs;
1675 int slimit_exceeded;
1676 pc_caching_reason_t caching_reason;
1681 remove_query_and_data(
1685 struct berval *uuid )
1687 query_manager* qm = cm->qm;
1689 qm->crfunc( qm, uuid );
1690 if ( !BER_BVISNULL( uuid ) ) {
1693 Debug( pcache_debug,
1694 "Removing query UUID %s\n",
1695 uuid->bv_val, 0, 0 );
1696 return_val = remove_query_data( op, rs, uuid );
1697 Debug( pcache_debug,
1698 "QUERY REMOVED, SIZE=%d\n",
1700 ldap_pvt_thread_mutex_lock( &cm->cache_mutex );
1701 cm->cur_entries -= return_val;
1702 cm->num_cached_queries--;
1703 Debug( pcache_debug,
1704 "STORED QUERIES = %lu\n",
1705 cm->num_cached_queries, 0, 0 );
1706 ldap_pvt_thread_mutex_unlock( &cm->cache_mutex );
1707 Debug( pcache_debug,
1708 "QUERY REMOVED, CACHE ="
1710 cm->cur_entries, 0, 0 );
1715 * Callback used to fetch queryId values based on entryUUID;
1716 * used by pcache_remove_entries_from_cache()
1719 fetch_queryId_cb( Operation *op, SlapReply *rs )
1723 /* only care about searchEntry responses */
1724 if ( rs->sr_type != REP_SEARCH ) {
1728 /* allow only one response per entryUUID */
1729 if ( op->o_callback->sc_private != NULL ) {
1735 /* copy all queryId values into callback's private data */
1736 a = attr_find( rs->sr_entry->e_attrs, ad_queryId );
1738 BerVarray vals = NULL;
1740 ber_bvarray_dup_x( &vals, a->a_nvals, op->o_tmpmemctx );
1741 op->o_callback->sc_private = (void *)vals;
1745 /* clear entry if required */
1746 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
1747 entry_free( rs->sr_entry );
1748 rs->sr_entry = NULL;
1749 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
1756 * Call that allows to remove a set of entries from the cache,
1757 * by forcing the removal of all the related queries.
1760 pcache_remove_entries_from_cache(
1763 BerVarray entryUUIDs )
1765 Connection conn = { 0 };
1766 OperationBuffer opbuf;
1768 slap_callback sc = { 0 };
1769 SlapReply rs = { REP_RESULT };
1771 char filtbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(entryUUID=)" ) ];
1772 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
1773 AttributeName attrs[ 2 ] = {{{ 0 }}};
1777 void *thrctx = ldap_pvt_thread_pool_context();
1779 connection_fake_init( &conn, &opbuf, thrctx );
1787 memset( &op->oq_search, 0, sizeof( op->oq_search ) );
1788 op->ors_scope = LDAP_SCOPE_SUBTREE;
1789 op->ors_deref = LDAP_DEREF_NEVER;
1790 f.f_choice = LDAP_FILTER_EQUALITY;
1792 ava.aa_desc = slap_schema.si_ad_entryUUID;
1793 op->ors_filter = &f;
1795 op->ors_tlimit = SLAP_NO_LIMIT;
1796 attrs[ 0 ].an_desc = ad_queryId;
1797 attrs[ 0 ].an_name = ad_queryId->ad_cname;
1798 op->ors_attrs = attrs;
1799 op->ors_attrsonly = 0;
1801 op->o_req_dn = cm->db.be_suffix[ 0 ];
1802 op->o_req_ndn = cm->db.be_nsuffix[ 0 ];
1804 op->o_tag = LDAP_REQ_SEARCH;
1805 op->o_protocol = LDAP_VERSION3;
1806 op->o_managedsait = SLAP_CONTROL_CRITICAL;
1808 op->o_dn = op->o_bd->be_rootdn;
1809 op->o_ndn = op->o_bd->be_rootndn;
1810 sc.sc_response = fetch_queryId_cb;
1811 op->o_callback = ≻
1813 for ( s = 0; !BER_BVISNULL( &entryUUIDs[ s ] ); s++ ) {
1814 BerVarray vals = NULL;
1816 op->ors_filterstr.bv_len = snprintf( filtbuf, sizeof( filtbuf ),
1817 "(entryUUID=%s)", entryUUIDs[ s ].bv_val );
1818 op->ors_filterstr.bv_val = filtbuf;
1819 ava.aa_value = entryUUIDs[ s ];
1821 rc = op->o_bd->be_search( op, &rs );
1822 if ( rc != LDAP_SUCCESS ) {
1826 vals = (BerVarray)op->o_callback->sc_private;
1827 if ( vals != NULL ) {
1830 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1831 struct berval val = vals[ i ];
1833 remove_query_and_data( op, &rs, cm, &val );
1835 if ( !BER_BVISNULL( &val ) && val.bv_val != vals[ i ].bv_val ) {
1836 ch_free( val.bv_val );
1840 ber_bvarray_free_x( vals, op->o_tmpmemctx );
1841 op->o_callback->sc_private = NULL;
1849 * Call that allows to remove a query from the cache.
1852 pcache_remove_query_from_cache(
1855 struct berval *queryid )
1857 Operation op2 = *op;
1858 SlapReply rs2 = { 0 };
1862 /* remove the selected query */
1863 remove_query_and_data( &op2, &rs2, cm, queryid );
1865 return LDAP_SUCCESS;
1869 * Call that allows to remove a set of queries related to an entry
1870 * from the cache; if queryid is not null, the entry must belong to
1871 * the query indicated by queryid.
1874 pcache_remove_entry_queries_from_cache(
1878 struct berval *queryid )
1880 Connection conn = { 0 };
1881 OperationBuffer opbuf;
1883 slap_callback sc = { 0 };
1884 SlapReply rs = { REP_RESULT };
1886 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(queryId=)" ) ];
1887 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
1888 AttributeName attrs[ 2 ] = {{{ 0 }}};
1891 BerVarray vals = NULL;
1894 void *thrctx = ldap_pvt_thread_pool_context();
1896 connection_fake_init( &conn, &opbuf, thrctx );
1904 memset( &op->oq_search, 0, sizeof( op->oq_search ) );
1905 op->ors_scope = LDAP_SCOPE_BASE;
1906 op->ors_deref = LDAP_DEREF_NEVER;
1907 if ( queryid == NULL || BER_BVISNULL( queryid ) ) {
1908 BER_BVSTR( &op->ors_filterstr, "(objectClass=*)" );
1909 f.f_choice = LDAP_FILTER_PRESENT;
1910 f.f_desc = slap_schema.si_ad_objectClass;
1913 op->ors_filterstr.bv_len = snprintf( filter_str,
1914 sizeof( filter_str ), "(%s=%s)",
1915 ad_queryId->ad_cname.bv_val, queryid->bv_val );
1916 f.f_choice = LDAP_FILTER_EQUALITY;
1918 f.f_av_desc = ad_queryId;
1919 f.f_av_value = *queryid;
1921 op->ors_filter = &f;
1923 op->ors_tlimit = SLAP_NO_LIMIT;
1924 attrs[ 0 ].an_desc = ad_queryId;
1925 attrs[ 0 ].an_name = ad_queryId->ad_cname;
1926 op->ors_attrs = attrs;
1927 op->ors_attrsonly = 0;
1929 op->o_req_dn = *ndn;
1930 op->o_req_ndn = *ndn;
1932 op->o_tag = LDAP_REQ_SEARCH;
1933 op->o_protocol = LDAP_VERSION3;
1934 op->o_managedsait = SLAP_CONTROL_CRITICAL;
1936 op->o_dn = op->o_bd->be_rootdn;
1937 op->o_ndn = op->o_bd->be_rootndn;
1938 sc.sc_response = fetch_queryId_cb;
1939 op->o_callback = ≻
1941 rc = op->o_bd->be_search( op, &rs );
1942 if ( rc != LDAP_SUCCESS ) {
1946 vals = (BerVarray)op->o_callback->sc_private;
1947 if ( vals != NULL ) {
1950 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1951 struct berval val = vals[ i ];
1953 remove_query_and_data( op, &rs, cm, &val );
1955 if ( !BER_BVISNULL( &val ) && val.bv_val != vals[ i ].bv_val ) {
1956 ch_free( val.bv_val );
1960 ber_bvarray_free_x( vals, op->o_tmpmemctx );
1963 return LDAP_SUCCESS;
1970 struct berval *query_uuid )
1972 struct search_info *si = op->o_callback->sc_private;
1973 slap_overinst *on = si->on;
1974 cache_manager *cm = on->on_bi.bi_private;
1977 struct berval crp_uuid;
1978 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1980 Connection conn = {0};
1981 OperationBuffer opbuf;
1982 void *thrctx = ldap_pvt_thread_pool_context();
1984 query_uuid->bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf));
1985 ber_str2bv(uuidbuf, query_uuid->bv_len, 1, query_uuid);
1987 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
1988 op_tmp = &opbuf.ob_op;
1989 op_tmp->o_bd = &cm->db;
1990 op_tmp->o_dn = cm->db.be_rootdn;
1991 op_tmp->o_ndn = cm->db.be_rootndn;
1993 Debug( pcache_debug, "UUID for query being added = %s\n",
1996 for ( e=si->head; e; e=si->head ) {
1997 si->head = e->e_private;
1998 e->e_private = NULL;
1999 while ( cm->cur_entries > (cm->max_entries) ) {
2000 BER_BVZERO( &crp_uuid );
2001 remove_query_and_data( op_tmp, rs, cm, &crp_uuid );
2004 return_val = merge_entry(op_tmp, e, query_uuid);
2005 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2006 cm->cur_entries += return_val;
2007 Debug( pcache_debug,
2008 "ENTRY ADDED/MERGED, CACHED ENTRIES=%d\n",
2009 cm->cur_entries, 0, 0 );
2011 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2018 pcache_op_cleanup( Operation *op, SlapReply *rs ) {
2019 slap_callback *cb = op->o_callback;
2020 struct search_info *si = cb->sc_private;
2021 slap_overinst *on = si->on;
2022 cache_manager *cm = on->on_bi.bi_private;
2023 query_manager* qm = cm->qm;
2025 if ( rs->sr_type == REP_SEARCH ) {
2028 /* don't return more entries than requested by the client */
2029 if ( si->slimit > 0 && rs->sr_nentries >= si->slimit ) {
2030 si->slimit_exceeded = 1;
2033 /* If we haven't exceeded the limit for this query,
2034 * build a chain of answers to store. If we hit the
2035 * limit, empty the chain and ignore the rest.
2038 /* check if the entry contains undefined
2039 * attributes/objectClasses (ITS#5680) */
2040 if ( cm->check_cacheability && test_filter( op, rs->sr_entry, si->query.filter ) != LDAP_COMPARE_TRUE ) {
2041 Debug( pcache_debug, "%s: query not cacheable because of schema issues in DN \"%s\"\n",
2042 op->o_log_prefix, rs->sr_entry->e_name.bv_val, 0 );
2046 /* check for malformed entries: attrs with no values */
2048 Attribute *a = rs->sr_entry->e_attrs;
2049 for (; a; a=a->a_next) {
2050 if ( !a->a_numvals ) {
2051 Debug( pcache_debug, "%s: query not cacheable because of attrs without values in DN \"%s\" (%s)\n",
2052 op->o_log_prefix, rs->sr_entry->e_name.bv_val,
2053 a->a_desc->ad_cname.bv_val );
2059 if ( si->count < si->max ) {
2061 e = entry_dup( rs->sr_entry );
2062 if ( !si->head ) si->head = e;
2063 if ( si->tail ) si->tail->e_private = e;
2070 for (;si->head; si->head=e) {
2071 e = si->head->e_private;
2072 si->head->e_private = NULL;
2073 entry_free(si->head);
2081 if ( rs->sr_type == REP_RESULT ||
2082 op->o_abandon || rs->sr_err == SLAPD_ABANDON )
2084 if ( si->swap_saved_attrs ) {
2085 rs->sr_attrs = si->save_attrs;
2086 op->ors_attrs = si->save_attrs;
2088 if ( (op->o_abandon || rs->sr_err == SLAPD_ABANDON) &&
2089 si->caching_reason == PC_IGNORE )
2091 filter_free( si->query.filter );
2093 /* duplicate query, free it */
2095 for (;si->head; si->head=e) {
2096 e = si->head->e_private;
2097 si->head->e_private = NULL;
2098 entry_free(si->head);
2101 op->o_callback = op->o_callback->sc_next;
2102 op->o_tmpfree( cb, op->o_tmpmemctx );
2103 } else if ( si->caching_reason != PC_IGNORE ) {
2104 CachedQuery *qc = qm->addfunc(op, qm, &si->query,
2105 si->qtemp, si->caching_reason, 1 );
2108 switch ( si->caching_reason ) {
2110 cache_entries( op, rs, &qc->q_uuid );
2114 qc->q_sizelimit = rs->sr_nentries;
2124 ldap_pvt_thread_rdwr_wunlock(&qc->rwlock);
2125 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2126 cm->num_cached_queries++;
2127 Debug( pcache_debug, "STORED QUERIES = %lu\n",
2128 cm->num_cached_queries, 0, 0 );
2129 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2131 /* If the consistency checker suspended itself,
2134 if ( cm->cc_paused == PCACHE_CC_PAUSED ) {
2135 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2136 if ( cm->cc_paused == PCACHE_CC_PAUSED ) {
2138 ldap_pvt_runqueue_resched( &slapd_rq, cm->cc_arg, 0 );
2140 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2143 } else if ( si->count ) {
2144 /* duplicate query, free it */
2146 for (;si->head; si->head=e) {
2147 e = si->head->e_private;
2148 si->head->e_private = NULL;
2149 entry_free(si->head);
2154 filter_free( si->query.filter );
2158 return SLAP_CB_CONTINUE;
2166 struct search_info *si = op->o_callback->sc_private;
2168 if ( si->swap_saved_attrs ) {
2169 rs->sr_attrs = si->save_attrs;
2170 op->ors_attrs = si->save_attrs;
2173 if ( rs->sr_type == REP_SEARCH ) {
2174 /* don't return more entries than requested by the client */
2175 if ( si->slimit_exceeded ) {
2179 } else if ( rs->sr_type == REP_RESULT ) {
2182 if ( rs->sr_err == LDAP_SUCCESS ) {
2183 si->caching_reason = PC_POSITIVE;
2185 } else if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED
2186 && si->qtemp->limitttl )
2188 si->caching_reason = PC_SIZELIMIT;
2191 } else if ( si->qtemp->negttl && !si->count && !si->over &&
2192 rs->sr_err == LDAP_SUCCESS )
2194 si->caching_reason = PC_NEGATIVE;
2198 if ( si->slimit_exceeded ) {
2199 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
2203 return SLAP_CB_CONTINUE;
2209 AttributeName** new_attrs,
2210 struct attr_set *attrs,
2211 AttributeName* filter_attrs,
2221 /* duplicate attrs */
2222 count = attrs->count + fattr_cnt;
2223 if ( !fattr_got_oc && !(attrs->flags & PC_GOT_OC)) {
2228 *new_attrs = (AttributeName*)ch_calloc( count + 1,
2229 sizeof(AttributeName) );
2230 for (i=0; i<attrs->count; i++) {
2231 (*new_attrs)[i].an_name = attrs->attrs[i].an_name;
2232 (*new_attrs)[i].an_desc = attrs->attrs[i].an_desc;
2234 BER_BVZERO( &(*new_attrs)[i].an_name );
2235 alluser = an_find( *new_attrs, slap_bv_all_user_attrs );
2236 allop = an_find( *new_attrs, slap_bv_all_operational_attrs );
2239 for ( i=0; i<fattr_cnt; i++ ) {
2240 if ( an_find(*new_attrs, &filter_attrs[i].an_name ) ) {
2243 if ( is_at_operational(filter_attrs[i].an_desc->ad_type) ) {
2247 } else if ( alluser ) {
2250 (*new_attrs)[j].an_name = filter_attrs[i].an_name;
2251 (*new_attrs)[j].an_desc = filter_attrs[i].an_desc;
2252 (*new_attrs)[j].an_oc = NULL;
2253 (*new_attrs)[j].an_flags = 0;
2257 (*new_attrs)[j].an_name = slap_schema.si_ad_objectClass->ad_cname;
2258 (*new_attrs)[j].an_desc = slap_schema.si_ad_objectClass;
2259 (*new_attrs)[j].an_oc = NULL;
2260 (*new_attrs)[j].an_flags = 0;
2263 BER_BVZERO( &(*new_attrs)[j].an_name );
2268 /* NOTE: this is a quick workaround to let pcache minimally interact
2269 * with pagedResults. A more articulated solutions would be to
2270 * perform the remote query without control and cache all results,
2271 * performing the pagedResults search only within the client
2272 * and the proxy. This requires pcache to understand pagedResults. */
2274 pcache_chk_controls(
2278 const char *non = "";
2279 const char *stripped = "";
2281 switch( op->o_pagedresults ) {
2282 case SLAP_CONTROL_NONCRITICAL:
2284 stripped = "; stripped";
2287 case SLAP_CONTROL_CRITICAL:
2288 Debug( pcache_debug, "%s: "
2289 "%scritical pagedResults control "
2290 "disabled with proxy cache%s.\n",
2291 op->o_log_prefix, non, stripped );
2293 slap_remove_control( op, rs, slap_cids.sc_pagedResults, NULL );
2297 rs->sr_err = SLAP_CB_CONTINUE;
2304 #ifdef PCACHE_CONTROL_PRIVDB
2310 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2311 cache_manager *cm = on->on_bi.bi_private;
2312 slap_callback *save_cb;
2315 /* skip if control is unset */
2316 if ( op->o_ctrlflag[ privDB_cid ] != SLAP_CONTROL_CRITICAL ) {
2317 return SLAP_CB_CONTINUE;
2320 /* The cache DB isn't open yet */
2321 if ( cm->defer_db_open ) {
2322 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
2323 "pcachePrivDB: cacheDB not available" );
2327 /* FIXME: might be a little bit exaggerated... */
2328 if ( !be_isroot( op ) ) {
2329 save_cb = op->o_callback;
2330 op->o_callback = NULL;
2331 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
2332 "pcachePrivDB: operation not allowed" );
2333 op->o_callback = save_cb;
2338 /* map tag to operation */
2339 type = slap_req2op( op->o_tag );
2340 if ( type != SLAP_OP_LAST ) {
2344 /* execute, if possible */
2345 func = &cm->db.be_bind;
2346 if ( func[ type ] != NULL ) {
2347 Operation op2 = *op;
2351 rc = func[ type ]( &op2, rs );
2352 if ( type == SLAP_OP_BIND && rc == LDAP_SUCCESS ) {
2353 op->o_conn->c_authz_cookie = cm->db.be_private;
2358 /* otherwise fall back to error */
2359 save_cb = op->o_callback;
2360 op->o_callback = NULL;
2361 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
2362 "operation not supported with pcachePrivDB control" );
2363 op->o_callback = save_cb;
2367 #endif /* PCACHE_CONTROL_PRIVDB */
2374 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2375 cache_manager *cm = on->on_bi.bi_private;
2376 query_manager* qm = cm->qm;
2380 AttributeName *filter_attrs = NULL;
2383 QueryTemplate *qtemp = NULL;
2386 CachedQuery *answerable = NULL;
2389 int fattr_got_oc = 0;
2391 struct berval tempstr;
2393 #ifdef PCACHE_CONTROL_PRIVDB
2394 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL ) {
2395 return pcache_op_privdb( op, rs );
2397 #endif /* PCACHE_CONTROL_PRIVDB */
2399 /* The cache DB isn't open yet */
2400 if ( cm->defer_db_open ) {
2401 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
2402 "pcachePrivDB: cacheDB not available" );
2406 /* pickup runtime ACL changes */
2407 cm->db.be_acl = op->o_bd->be_acl;
2409 tempstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len+1, op->o_tmpmemctx );
2411 if ( filter2template( op, op->ors_filter, &tempstr, &filter_attrs,
2412 &fattr_cnt, &fattr_got_oc ))
2414 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
2415 return SLAP_CB_CONTINUE;
2418 Debug( pcache_debug, "query template of incoming query = %s\n",
2419 tempstr.bv_val, 0, 0 );
2421 /* FIXME: cannot cache/answer requests with pagedResults control */
2424 attr_set = get_attr_set(op->ors_attrs, qm, cm->numattrsets);
2426 query.filter = op->ors_filter;
2427 query.base = op->o_req_ndn;
2428 query.scope = op->ors_scope;
2430 /* check for query containment */
2431 if (attr_set > -1) {
2432 QueryTemplate *qt = qm->attr_sets[attr_set].templates;
2433 for (; qt; qt = qt->qtnext ) {
2434 /* find if template i can potentially answer tempstr */
2435 if ( ber_bvstrcasecmp( &qt->querystr, &tempstr ) != 0 )
2439 Debug( pcache_debug, "Entering QC, querystr = %s\n",
2440 op->ors_filterstr.bv_val, 0, 0 );
2441 answerable = qm->qcfunc(op, qm, &query, qt);
2443 /* if != NULL, rlocks qtemp->t_rwlock */
2448 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
2451 BackendDB *save_bd = op->o_bd;
2452 slap_callback *save_cb = op->o_callback;
2454 ldap_pvt_thread_mutex_lock( &answerable->answerable_cnt_mutex );
2455 answerable->answerable_cnt++;
2456 Debug( pcache_debug, "QUERY ANSWERABLE (answered %lu times)\n",
2457 answerable->answerable_cnt, 0, 0 );
2458 ldap_pvt_thread_mutex_unlock( &answerable->answerable_cnt_mutex );
2460 op->o_tmpfree( filter_attrs, op->o_tmpmemctx );
2461 ldap_pvt_thread_rdwr_rlock(&answerable->rwlock);
2462 if ( BER_BVISNULL( &answerable->q_uuid )) {
2463 /* No entries cached, just an empty result set */
2465 send_ldap_result( op, rs );
2468 if ( cm->response_cb == PCACHE_RESPONSE_CB_TAIL ) {
2469 /* The cached entry was already processed by any
2470 * other overlays, so don't let it get processed again.
2472 op->o_callback = NULL;
2474 i = cm->db.bd_info->bi_op_search( op, rs );
2476 ldap_pvt_thread_rdwr_runlock(&answerable->rwlock);
2477 /* locked by qtemp->qcfunc (query_containment) */
2478 ldap_pvt_thread_rdwr_runlock(&qtemp->t_rwlock);
2480 op->o_callback = save_cb;
2484 Debug( pcache_debug, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
2486 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2487 if (cm->num_cached_queries >= cm->max_queries) {
2490 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2492 if (op->ors_attrsonly)
2497 struct search_info *si;
2499 Debug( pcache_debug, "QUERY CACHEABLE\n", 0, 0, 0 );
2500 query.filter = filter_dup(op->ors_filter, NULL);
2501 ldap_pvt_thread_rdwr_wlock(&qtemp->t_rwlock);
2502 if ( !qtemp->t_attrs.count ) {
2503 qtemp->t_attrs.count = add_filter_attrs(op,
2504 &qtemp->t_attrs.attrs,
2505 &qm->attr_sets[attr_set],
2506 filter_attrs, fattr_cnt, fattr_got_oc);
2508 ldap_pvt_thread_rdwr_wunlock(&qtemp->t_rwlock);
2510 cb = op->o_tmpalloc( sizeof(*cb) + sizeof(*si), op->o_tmpmemctx );
2511 cb->sc_response = pcache_response;
2512 cb->sc_cleanup = pcache_op_cleanup;
2513 cb->sc_private = (cb+1);
2514 si = cb->sc_private;
2518 si->max = cm->num_entries_limit ;
2522 si->slimit_exceeded = 0;
2523 si->caching_reason = PC_IGNORE;
2524 if ( op->ors_slimit > 0 && op->ors_slimit < cm->num_entries_limit ) {
2525 si->slimit = op->ors_slimit;
2526 op->ors_slimit = cm->num_entries_limit;
2530 si->swap_saved_attrs = 1;
2531 si->save_attrs = op->ors_attrs;
2533 op->ors_attrs = qtemp->t_attrs.attrs;
2535 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
2536 cb->sc_next = op->o_callback;
2537 op->o_callback = cb;
2540 slap_callback **pcb;
2542 /* need to move the callback at the end, in case other
2543 * overlays are present, so that the final entry is
2544 * actually cached */
2546 for ( pcb = &op->o_callback; *pcb; pcb = &(*pcb)->sc_next );
2551 Debug( pcache_debug, "QUERY NOT CACHEABLE\n",
2555 op->o_tmpfree( filter_attrs, op->o_tmpmemctx );
2557 return SLAP_CB_CONTINUE;
2562 AttributeName* attrs,
2570 for ( ; attrs[count].an_name.bv_val; count++ );
2573 /* recognize a single "*" or a "1.1" */
2576 attrs = slap_anlist_all_user_attributes;
2578 } else if ( count == 1 && bvmatch( &attrs[0].an_name, slap_bv_no_attrs ) ) {
2583 for ( i = 0; i < num; i++ ) {
2587 if ( count > qm->attr_sets[i].count ) {
2592 if ( !qm->attr_sets[i].count ) {
2598 for ( a2 = attrs; a2->an_name.bv_val; a2++ ) {
2599 if ( !an_find( qm->attr_sets[i].attrs, &a2->an_name ) ) {
2622 struct re_s *rtask = arg;
2623 slap_overinst *on = rtask->arg;
2624 cache_manager *cm = on->on_bi.bi_private;
2625 query_manager *qm = cm->qm;
2626 Connection conn = {0};
2627 OperationBuffer opbuf;
2630 SlapReply rs = {REP_RESULT};
2632 int return_val, pause = PCACHE_CC_PAUSED;
2633 QueryTemplate* templ;
2635 /* Don't expire anything when we're offline */
2636 if ( cm->cc_paused & PCACHE_CC_OFFLINE ) {
2637 pause = PCACHE_CC_OFFLINE;
2641 connection_fake_init( &conn, &opbuf, ctx );
2645 op->o_dn = cm->db.be_rootdn;
2646 op->o_ndn = cm->db.be_rootndn;
2650 for (templ = qm->templates; templ; templ=templ->qmnext) {
2651 query = templ->query_last;
2652 if ( query ) pause = 0;
2653 op->o_time = slap_get_time();
2654 while (query && (query->expiry_time < op->o_time)) {
2656 Debug( pcache_debug, "Lock CR index = %p\n",
2657 (void *) templ, 0, 0 );
2658 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
2659 if ( query == templ->query_last ) {
2661 remove_from_template(query, templ);
2662 Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
2663 (void *) templ, templ->no_of_queries, 0 );
2664 Debug( pcache_debug, "Unlock CR index = %p\n",
2665 (void *) templ, 0, 0 );
2667 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
2669 query = templ->query_last;
2672 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
2673 remove_query(qm, query);
2674 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
2675 if ( BER_BVISNULL( &query->q_uuid ))
2678 return_val = remove_query_data(op, &rs, &query->q_uuid);
2679 Debug( pcache_debug, "STALE QUERY REMOVED, SIZE=%d\n",
2681 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2682 cm->cur_entries -= return_val;
2683 cm->num_cached_queries--;
2684 Debug( pcache_debug, "STORED QUERIES = %lu\n",
2685 cm->num_cached_queries, 0, 0 );
2686 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2687 Debug( pcache_debug,
2688 "STALE QUERY REMOVED, CACHE ="
2690 cm->cur_entries, 0, 0 );
2692 query = templ->query_last;
2697 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2698 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
2699 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
2701 /* If there were no queries, defer processing for a while */
2702 if ( cm->cc_paused != pause )
2703 cm->cc_paused = pause;
2704 ldap_pvt_runqueue_resched( &slapd_rq, rtask, pause );
2706 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2711 #define MAX_ATTR_SETS 500
2723 static ConfigDriver pc_cf_gen;
2724 static ConfigLDAPadd pc_ldadd;
2725 static ConfigCfAdd pc_cfadd;
2727 static ConfigTable pccfg[] = {
2728 { "proxycache", "backend> <max_entries> <numattrsets> <entry limit> "
2730 6, 6, 0, ARG_MAGIC|ARG_NO_DELETE|PC_MAIN, pc_cf_gen,
2731 "( OLcfgOvAt:2.1 NAME 'olcProxyCache' "
2732 "DESC 'ProxyCache basic parameters' "
2733 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
2734 { "proxyattrset", "index> <attributes...",
2735 2, 0, 0, ARG_MAGIC|PC_ATTR, pc_cf_gen,
2736 "( OLcfgOvAt:2.2 NAME 'olcProxyAttrset' "
2737 "DESC 'A set of attributes to cache' "
2738 "SYNTAX OMsDirectoryString )", NULL, NULL },
2739 { "proxytemplate", "filter> <attrset-index> <TTL> <negTTL",
2740 4, 6, 0, ARG_MAGIC|PC_TEMP, pc_cf_gen,
2741 "( OLcfgOvAt:2.3 NAME 'olcProxyTemplate' "
2742 "DESC 'Filter template, attrset, cache TTL, "
2743 "optional negative TTL, optional sizelimit TTL' "
2744 "SYNTAX OMsDirectoryString )", NULL, NULL },
2745 { "response-callback", "head|tail(default)",
2746 2, 2, 0, ARG_MAGIC|PC_RESP, pc_cf_gen,
2747 "( OLcfgOvAt:2.4 NAME 'olcProxyResponseCB' "
2748 "DESC 'Response callback position in overlay stack' "
2749 "SYNTAX OMsDirectoryString )", NULL, NULL },
2750 { "proxyCacheQueries", "queries",
2751 2, 2, 0, ARG_INT|ARG_MAGIC|PC_QUERIES, pc_cf_gen,
2752 "( OLcfgOvAt:2.5 NAME 'olcProxyCacheQueries' "
2753 "DESC 'Maximum number of queries to cache' "
2754 "SYNTAX OMsInteger )", NULL, NULL },
2755 { "proxySaveQueries", "TRUE|FALSE",
2756 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, save_queries),
2757 "( OLcfgOvAt:2.6 NAME 'olcProxySaveQueries' "
2758 "DESC 'Save cached queries for hot restart' "
2759 "SYNTAX OMsBoolean )", NULL, NULL },
2760 { "proxyCheckCacheability", "TRUE|FALSE",
2761 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, check_cacheability),
2762 "( OLcfgOvAt:2.7 NAME 'olcProxyCheckCacheability' "
2763 "DESC 'Check whether the results of a query are cacheable, e.g. for schema issues' "
2764 "SYNTAX OMsBoolean )", NULL, NULL },
2765 { "proxyCacheOffline", "TRUE|FALSE",
2766 2, 2, 0, ARG_ON_OFF|ARG_MAGIC|PC_OFFLINE, pc_cf_gen,
2767 "( OLcfgOvAt:2.8 NAME 'olcProxyCacheOffline' "
2768 "DESC 'Set cache to offline mode and disable expiration' "
2769 "SYNTAX OMsBoolean )", NULL, NULL },
2770 { "proxycache-", "private database args",
2771 1, 0, STRLENOF("proxycache-"), ARG_MAGIC|PC_PRIVATE_DB, pc_cf_gen,
2774 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
2777 static ConfigOCs pcocs[] = {
2778 { "( OLcfgOvOc:2.1 "
2779 "NAME 'olcPcacheConfig' "
2780 "DESC 'ProxyCache configuration' "
2781 "SUP olcOverlayConfig "
2782 "MUST ( olcProxyCache $ olcProxyAttrset $ olcProxyTemplate ) "
2783 "MAY ( olcProxyResponseCB $ olcProxyCacheQueries $ olcProxySaveQueries $ olcProxyCheckCacheability $ olcProxyCacheOffline ) )",
2784 Cft_Overlay, pccfg, NULL, pc_cfadd },
2785 { "( OLcfgOvOc:2.2 "
2786 "NAME 'olcPcacheDatabase' "
2787 "DESC 'Cache database configuration' "
2788 "AUXILIARY )", Cft_Misc, olcDatabaseDummy, pc_ldadd },
2792 static int pcache_db_open2( slap_overinst *on, ConfigReply *cr );
2795 pc_ldadd_cleanup( ConfigArgs *c )
2797 slap_overinst *on = c->ca_private;
2798 return pcache_db_open2( on, &c->reply );
2802 pc_ldadd( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
2807 if ( p->ce_type != Cft_Overlay || !p->ce_bi ||
2808 p->ce_bi->bi_cf_ocs != pcocs )
2809 return LDAP_CONSTRAINT_VIOLATION;
2811 on = (slap_overinst *)p->ce_bi;
2812 cm = on->on_bi.bi_private;
2814 /* Defer open if this is an LDAPadd */
2815 if ( CONFIG_ONLINE_ADD( ca ))
2816 ca->cleanup = pc_ldadd_cleanup;
2818 cm->defer_db_open = 0;
2819 ca->ca_private = on;
2820 return LDAP_SUCCESS;
2824 pc_cfadd( Operation *op, SlapReply *rs, Entry *p, ConfigArgs *ca )
2826 CfEntryInfo *pe = p->e_private;
2827 slap_overinst *on = (slap_overinst *)pe->ce_bi;
2828 cache_manager *cm = on->on_bi.bi_private;
2831 /* FIXME: should not hardcode "olcDatabase" here */
2832 bv.bv_len = snprintf( ca->cr_msg, sizeof( ca->cr_msg ),
2833 "olcDatabase=" SLAP_X_ORDERED_FMT "%s",
2834 0, cm->db.bd_info->bi_type );
2835 if ( bv.bv_len >= sizeof( ca->cr_msg ) ) {
2838 bv.bv_val = ca->cr_msg;
2840 cm->defer_db_open = 0;
2842 /* We can only create this entry if the database is table-driven
2844 if ( cm->db.bd_info->bi_cf_ocs )
2845 config_build_entry( op, rs, pe, ca, &bv, cm->db.bd_info->bi_cf_ocs,
2852 pc_cf_gen( ConfigArgs *c )
2854 slap_overinst *on = (slap_overinst *)c->bi;
2855 cache_manager* cm = on->on_bi.bi_private;
2856 query_manager* qm = cm->qm;
2857 QueryTemplate* temp;
2858 AttributeName* attr_name;
2859 AttributeName* attrarray;
2860 const char* text=NULL;
2865 if ( c->op == SLAP_CONFIG_EMIT ) {
2869 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s %d %d %d %ld",
2870 cm->db.bd_info->bi_type, cm->max_entries, cm->numattrsets,
2871 cm->num_entries_limit, cm->cc_period );
2872 bv.bv_val = c->cr_msg;
2873 value_add_one( &c->rvalue_vals, &bv );
2876 for (i=0; i<cm->numattrsets; i++) {
2877 if ( !qm->attr_sets[i].count ) continue;
2879 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ), "%d", i );
2881 /* count the attr length */
2882 for ( attr_name = qm->attr_sets[i].attrs;
2883 attr_name->an_name.bv_val; attr_name++ )
2884 bv.bv_len += attr_name->an_name.bv_len + 1;
2886 bv.bv_val = ch_malloc( bv.bv_len+1 );
2887 ptr = lutil_strcopy( bv.bv_val, c->cr_msg );
2888 for ( attr_name = qm->attr_sets[i].attrs;
2889 attr_name->an_name.bv_val; attr_name++ ) {
2891 ptr = lutil_strcopy( ptr, attr_name->an_name.bv_val );
2893 ber_bvarray_add( &c->rvalue_vals, &bv );
2895 if ( !c->rvalue_vals )
2899 for (temp=qm->templates; temp; temp=temp->qmnext) {
2900 /* HEADS-UP: always print all;
2901 * if optional == 0, ignore */
2902 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ),
2904 temp->attr_set_index,
2908 bv.bv_len += temp->querystr.bv_len + 2;
2909 bv.bv_val = ch_malloc( bv.bv_len+1 );
2912 ptr = lutil_strcopy( ptr, temp->querystr.bv_val );
2914 strcpy( ptr, c->cr_msg );
2915 ber_bvarray_add( &c->rvalue_vals, &bv );
2917 if ( !c->rvalue_vals )
2921 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
2922 BER_BVSTR( &bv, "head" );
2924 BER_BVSTR( &bv, "tail" );
2926 value_add_one( &c->rvalue_vals, &bv );
2929 c->value_int = cm->max_queries;
2932 c->value_int = (cm->cc_paused & PCACHE_CC_OFFLINE) != 0;
2936 } else if ( c->op == LDAP_MOD_DELETE ) {
2939 case PC_ATTR: /* FIXME */
2943 cm->cc_paused &= ~PCACHE_CC_OFFLINE;
2952 if ( cm->numattrsets > 0 ) {
2953 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"proxycache\" directive already provided" );
2954 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2958 if ( lutil_atoi( &cm->numattrsets, c->argv[3] ) != 0 ) {
2959 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse num attrsets=\"%s\" (arg #3)",
2961 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2964 if ( cm->numattrsets <= 0 ) {
2965 snprintf( c->cr_msg, sizeof( c->cr_msg ), "numattrsets (arg #3) must be positive" );
2966 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2969 if ( cm->numattrsets > MAX_ATTR_SETS ) {
2970 snprintf( c->cr_msg, sizeof( c->cr_msg ), "numattrsets (arg #3) must be <= %d", MAX_ATTR_SETS );
2971 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2975 if ( !backend_db_init( c->argv[1], &cm->db, -1, NULL )) {
2976 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unknown backend type (arg #1)" );
2977 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2981 if ( lutil_atoi( &cm->max_entries, c->argv[2] ) != 0 ) {
2982 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse max entries=\"%s\" (arg #2)",
2984 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2987 if ( cm->max_entries <= 0 ) {
2988 snprintf( c->cr_msg, sizeof( c->cr_msg ), "max entries (arg #2) must be positive.\n" );
2989 Debug( LDAP_DEBUG_CONFIG, "%s: %s\n", c->log, c->cr_msg, 0 );
2993 if ( lutil_atoi( &cm->num_entries_limit, c->argv[4] ) != 0 ) {
2994 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse entry limit=\"%s\" (arg #4)",
2996 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2999 if ( cm->num_entries_limit <= 0 ) {
3000 snprintf( c->cr_msg, sizeof( c->cr_msg ), "entry limit (arg #4) must be positive" );
3001 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3004 if ( cm->num_entries_limit > cm->max_entries ) {
3005 snprintf( c->cr_msg, sizeof( c->cr_msg ), "entry limit (arg #4) must be less than max entries %d (arg #2)", cm->max_entries );
3006 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3010 if ( lutil_parse_time( c->argv[5], &t ) != 0 ) {
3011 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse period=\"%s\" (arg #5)",
3013 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3017 cm->cc_period = (time_t)t;
3018 Debug( pcache_debug,
3019 "Total # of attribute sets to be cached = %d.\n",
3020 cm->numattrsets, 0, 0 );
3021 qm->attr_sets = ( struct attr_set * )ch_calloc( cm->numattrsets,
3022 sizeof( struct attr_set ) );
3025 if ( cm->numattrsets == 0 ) {
3026 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"proxycache\" directive not provided yet" );
3027 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3030 if ( lutil_atoi( &num, c->argv[1] ) != 0 ) {
3031 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse attrset #=\"%s\"",
3033 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3037 if ( num < 0 || num >= cm->numattrsets ) {
3038 snprintf( c->cr_msg, sizeof( c->cr_msg ), "attrset index %d out of bounds (must be %s%d)",
3039 num, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
3040 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3043 qm->attr_sets[num].flags |= PC_CONFIGURED;
3044 if ( c->argc == 2 ) {
3046 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3047 "need an explicit attr in attrlist; use \"*\" to indicate all attrs" );
3048 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3051 } else if ( c->argc == 3 ) {
3052 if ( strcmp( c->argv[2], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) {
3053 qm->attr_sets[num].count = 1;
3054 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 2,
3055 sizeof( AttributeName ) );
3056 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_USER_ATTRIBUTES );
3059 } else if ( strcmp( c->argv[2], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 ) {
3060 qm->attr_sets[num].count = 1;
3061 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 2,
3062 sizeof( AttributeName ) );
3063 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
3066 } else if ( strcmp( c->argv[2], LDAP_NO_ATTRS ) == 0 ) {
3069 /* else: fallthru */
3071 } else if ( c->argc == 4 ) {
3072 if ( ( strcmp( c->argv[2], LDAP_ALL_USER_ATTRIBUTES ) == 0 && strcmp( c->argv[3], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 )
3073 || ( strcmp( c->argv[2], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 && strcmp( c->argv[3], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) )
3075 qm->attr_sets[num].count = 2;
3076 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 3,
3077 sizeof( AttributeName ) );
3078 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_USER_ATTRIBUTES );
3079 BER_BVSTR( &qm->attr_sets[num].attrs[1].an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
3082 /* else: fallthru */
3085 if ( c->argc > 2 ) {
3086 int all_user = 0, all_op = 0;
3088 qm->attr_sets[num].count = c->argc - 2;
3089 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( c->argc - 1,
3090 sizeof( AttributeName ) );
3091 attr_name = qm->attr_sets[num].attrs;
3092 for ( i = 2; i < c->argc; i++ ) {
3093 attr_name->an_desc = NULL;
3094 if ( strcmp( c->argv[i], LDAP_NO_ATTRS ) == 0 ) {
3095 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3096 "invalid attr #%d \"%s\" in attrlist",
3097 i - 2, c->argv[i] );
3098 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3099 ch_free( qm->attr_sets[num].attrs );
3100 qm->attr_sets[num].attrs = NULL;
3101 qm->attr_sets[num].count = 0;
3104 if ( strcmp( c->argv[i], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) {
3106 BER_BVSTR( &attr_name->an_name, LDAP_ALL_USER_ATTRIBUTES );
3107 } else if ( strcmp( c->argv[i], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 ) {
3109 BER_BVSTR( &attr_name->an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
3111 if ( slap_str2ad( c->argv[i], &attr_name->an_desc, &text ) ) {
3112 strcpy( c->cr_msg, text );
3113 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3114 ch_free( qm->attr_sets[num].attrs );
3115 qm->attr_sets[num].attrs = NULL;
3116 qm->attr_sets[num].count = 0;
3119 attr_name->an_name = attr_name->an_desc->ad_cname;
3121 attr_name->an_oc = NULL;
3122 attr_name->an_flags = 0;
3123 if ( attr_name->an_desc == slap_schema.si_ad_objectClass )
3124 qm->attr_sets[num].flags |= PC_GOT_OC;
3126 BER_BVZERO( &attr_name->an_name );
3129 /* warn if list contains both "*" and "+" */
3130 if ( i > 4 && all_user && all_op ) {
3131 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3132 "warning: attribute list contains \"*\" and \"+\"" );
3133 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3138 if ( cm->numattrsets == 0 ) {
3139 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"proxycache\" directive not provided yet" );
3140 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3143 if ( lutil_atoi( &i, c->argv[2] ) != 0 ) {
3144 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse template #=\"%s\"",
3146 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3150 if ( i < 0 || i >= cm->numattrsets ||
3151 !(qm->attr_sets[i].flags & PC_CONFIGURED )) {
3152 snprintf( c->cr_msg, sizeof( c->cr_msg ), "template index %d invalid (%s%d)",
3153 i, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
3154 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3157 temp = ch_calloc( 1, sizeof( QueryTemplate ));
3158 temp->qmnext = qm->templates;
3159 qm->templates = temp;
3160 ldap_pvt_thread_rdwr_init( &temp->t_rwlock );
3161 temp->query = temp->query_last = NULL;
3162 if ( lutil_parse_time( c->argv[3], &t ) != 0 ) {
3163 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3164 "unable to parse template ttl=\"%s\"",
3166 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3169 temp->ttl = (time_t)t;
3170 temp->negttl = (time_t)0;
3171 temp->limitttl = (time_t)0;
3172 switch ( c->argc ) {
3174 if ( lutil_parse_time( c->argv[5], &t ) != 0 ) {
3175 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3176 "unable to parse template sizelimit ttl=\"%s\"",
3178 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3181 temp->limitttl = (time_t)t;
3185 if ( lutil_parse_time( c->argv[4], &t ) != 0 ) {
3186 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3187 "unable to parse template negative ttl=\"%s\"",
3189 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3192 temp->negttl = (time_t)t;
3196 temp->no_of_queries = 0;
3198 ber_str2bv( c->argv[1], 0, 1, &temp->querystr );
3199 Debug( pcache_debug, "Template:\n", 0, 0, 0 );
3200 Debug( pcache_debug, " query template: %s\n",
3201 temp->querystr.bv_val, 0, 0 );
3202 temp->attr_set_index = i;
3203 qm->attr_sets[i].flags |= PC_REFERENCED;
3204 temp->qtnext = qm->attr_sets[i].templates;
3205 qm->attr_sets[i].templates = temp;
3206 Debug( pcache_debug, " attributes: \n", 0, 0, 0 );
3207 if ( ( attrarray = qm->attr_sets[i].attrs ) != NULL ) {
3208 for ( i=0; attrarray[i].an_name.bv_val; i++ )
3209 Debug( pcache_debug, "\t%s\n",
3210 attrarray[i].an_name.bv_val, 0, 0 );
3214 if ( strcasecmp( c->argv[1], "head" ) == 0 ) {
3215 cm->response_cb = PCACHE_RESPONSE_CB_HEAD;
3217 } else if ( strcasecmp( c->argv[1], "tail" ) == 0 ) {
3218 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
3221 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unknown specifier" );
3222 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3227 if ( c->value_int <= 0 ) {
3228 snprintf( c->cr_msg, sizeof( c->cr_msg ), "max queries must be positive" );
3229 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3232 cm->max_queries = c->value_int;
3236 cm->cc_paused |= PCACHE_CC_OFFLINE;
3238 cm->cc_paused &= ~PCACHE_CC_OFFLINE;
3241 if ( cm->db.be_private == NULL ) {
3242 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3243 "private database must be defined before setting database specific options" );
3244 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3248 if ( cm->db.be_config != NULL ) {
3249 char *argv0 = c->argv[ 0 ];
3251 c->argv[ 0 ] = &argv0[ STRLENOF( "proxycache-" ) ];
3252 rc = cm->db.be_config( &cm->db, c->fname, c->lineno, c->argc, c->argv );
3253 c->argv[ 0 ] = argv0;
3256 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3257 "no means to set private database specific options" );
3258 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3263 rc = SLAP_CONF_UNKNOWN;
3279 slap_overinst *on = (slap_overinst *)be->bd_info;
3280 cache_manager* cm = on->on_bi.bi_private;
3282 /* Something for the cache database? */
3283 if ( cm->db.bd_info && cm->db.bd_info->bi_db_config )
3284 return cm->db.bd_info->bi_db_config( &cm->db, fname, lineno,
3286 return SLAP_CONF_UNKNOWN;
3294 slap_overinst *on = (slap_overinst *)be->bd_info;
3298 cm = (cache_manager *)ch_malloc(sizeof(cache_manager));
3299 on->on_bi.bi_private = cm;
3301 qm = (query_manager*)ch_malloc(sizeof(query_manager));
3304 SLAP_DBFLAGS(&cm->db) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3305 cm->db.be_private = NULL;
3306 cm->db.bd_self = &cm->db;
3308 cm->numattrsets = 0;
3309 cm->num_entries_limit = 5;
3310 cm->num_cached_queries = 0;
3311 cm->max_entries = 0;
3312 cm->cur_entries = 0;
3313 cm->max_queries = 10000;
3314 cm->save_queries = 0;
3315 cm->check_cacheability = 0;
3316 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
3317 cm->defer_db_open = 1;
3318 cm->cc_period = 1000;
3322 qm->attr_sets = NULL;
3323 qm->templates = NULL;
3325 qm->lru_bottom = NULL;
3327 qm->qcfunc = query_containment;
3328 qm->crfunc = cache_replacement;
3329 qm->addfunc = add_query;
3330 ldap_pvt_thread_mutex_init(&qm->lru_mutex);
3332 ldap_pvt_thread_mutex_init(&cm->cache_mutex);
3337 pcache_cachedquery_open_cb( Operation *op, SlapReply *rs )
3339 assert( op->o_tag == LDAP_REQ_SEARCH );
3341 if ( rs->sr_type == REP_SEARCH ) {
3344 a = attr_find( rs->sr_entry->e_attrs, ad_cachedQueryURL );
3348 assert( a->a_nvals != NULL );
3350 valsp = op->o_callback->sc_private;
3351 assert( *valsp == NULL );
3353 ber_bvarray_dup_x( valsp, a->a_nvals, op->o_tmpmemctx );
3361 pcache_cachedquery_count_cb( Operation *op, SlapReply *rs )
3363 assert( op->o_tag == LDAP_REQ_SEARCH );
3365 if ( rs->sr_type == REP_SEARCH ) {
3366 int *countp = (int *)op->o_callback->sc_private;
3379 cache_manager *cm = on->on_bi.bi_private;
3380 query_manager* qm = cm->qm;
3383 rc = backend_startup_one( &cm->db, cr );
3385 cm->defer_db_open = 0;
3388 /* There is no runqueue in TOOL mode */
3389 if (( slapMode & SLAP_SERVER_MODE ) && rc == 0 ) {
3390 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3391 ldap_pvt_runqueue_insert( &slapd_rq, cm->cc_period,
3392 consistency_check, on,
3393 "pcache_consistency", cm->db.be_suffix[0].bv_val );
3394 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3396 /* Cached database must have the rootdn */
3397 if ( BER_BVISNULL( &cm->db.be_rootndn )
3398 || BER_BVISEMPTY( &cm->db.be_rootndn ) )
3400 Debug( LDAP_DEBUG_ANY, "pcache_db_open(): "
3401 "underlying database of type \"%s\"\n"
3402 " serving naming context \"%s\"\n"
3403 " has no \"rootdn\", required by \"proxycache\".\n",
3404 on->on_info->oi_orig->bi_type,
3405 cm->db.be_suffix[0].bv_val, 0 );
3409 if ( cm->save_queries ) {
3410 void *thrctx = ldap_pvt_thread_pool_context();
3411 Connection conn = { 0 };
3412 OperationBuffer opbuf;
3414 slap_callback cb = { 0 };
3415 SlapReply rs = { 0 };
3416 BerVarray vals = NULL;
3417 Filter f = { 0 }, f2 = { 0 };
3418 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
3419 AttributeName attrs[ 2 ] = {{{ 0 }}};
3421 connection_fake_init( &conn, &opbuf, thrctx );
3426 op->o_tag = LDAP_REQ_SEARCH;
3427 op->o_protocol = LDAP_VERSION3;
3428 cb.sc_response = pcache_cachedquery_open_cb;
3429 cb.sc_private = &vals;
3430 op->o_callback = &cb;
3431 op->o_time = slap_get_time();
3432 op->o_do_not_cache = 1;
3433 op->o_managedsait = SLAP_CONTROL_CRITICAL;
3435 op->o_dn = cm->db.be_rootdn;
3436 op->o_ndn = cm->db.be_rootndn;
3437 op->o_req_dn = cm->db.be_suffix[ 0 ];
3438 op->o_req_ndn = cm->db.be_nsuffix[ 0 ];
3440 op->ors_scope = LDAP_SCOPE_BASE;
3441 op->ors_deref = LDAP_DEREF_NEVER;
3443 op->ors_tlimit = SLAP_NO_LIMIT;
3444 ber_str2bv( "(cachedQueryURL=*)", 0, 0, &op->ors_filterstr );
3445 f.f_choice = LDAP_FILTER_PRESENT;
3446 f.f_desc = ad_cachedQueryURL;
3447 op->ors_filter = &f;
3448 attrs[ 0 ].an_desc = ad_cachedQueryURL;
3449 attrs[ 0 ].an_name = ad_cachedQueryURL->ad_cname;
3450 op->ors_attrs = attrs;
3451 op->ors_attrsonly = 0;
3453 rc = op->o_bd->be_search( op, &rs );
3454 if ( rc == LDAP_SUCCESS && vals != NULL ) {
3457 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
3458 if ( url2query( vals[ i ].bv_val, op, qm ) == 0 ) {
3459 cm->num_cached_queries++;
3463 ber_bvarray_free_x( vals, op->o_tmpmemctx );
3466 /* count cached entries */
3467 f.f_choice = LDAP_FILTER_NOT;
3469 f2.f_choice = LDAP_FILTER_EQUALITY;
3471 f2.f_av_desc = slap_schema.si_ad_objectClass;
3472 BER_BVSTR( &f2.f_av_value, "glue" );
3473 ber_str2bv( "(!(objectClass=glue))", 0, 0, &op->ors_filterstr );
3475 op->ors_slimit = SLAP_NO_LIMIT;
3476 op->ors_scope = LDAP_SCOPE_SUBTREE;
3477 op->ors_attrs = slap_anlist_no_attrs;
3479 op->o_callback->sc_response = pcache_cachedquery_count_cb;
3481 op->o_callback->sc_private = &rs.sr_nentries;
3483 rc = op->o_bd->be_search( op, &rs );
3485 cm->cur_entries = rs.sr_nentries;
3499 slap_overinst *on = (slap_overinst *)be->bd_info;
3500 cache_manager *cm = on->on_bi.bi_private;
3501 query_manager* qm = cm->qm;
3502 int i, ncf = 0, rf = 0, nrf = 0, rc = 0;
3504 /* check attr sets */
3505 for ( i = 0; i < cm->numattrsets; i++) {
3506 if ( !( qm->attr_sets[i].flags & PC_CONFIGURED ) ) {
3507 if ( qm->attr_sets[i].flags & PC_REFERENCED ) {
3508 Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d not configured but referenced.\n", i, 0, 0 );
3512 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, attr set #%d not configured.\n", i, 0, 0 );
3516 } else if ( !( qm->attr_sets[i].flags & PC_REFERENCED ) ) {
3517 Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d configured but not referenced.\n", i, 0, 0 );
3522 if ( ncf || rf || nrf ) {
3523 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets configured but not referenced.\n", nrf, 0, 0 );
3524 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets not configured.\n", ncf, 0, 0 );
3525 Debug( LDAP_DEBUG_CONFIG, "pcache: %d attr sets not configured but referenced.\n", rf, 0, 0 );
3532 /* need to inherit something from the original database... */
3533 cm->db.be_def_limit = be->be_def_limit;
3534 cm->db.be_limits = be->be_limits;
3535 cm->db.be_acl = be->be_acl;
3536 cm->db.be_dfltaccess = be->be_dfltaccess;
3538 if ( SLAP_DBMONITORING( be ) ) {
3539 SLAP_DBFLAGS( &cm->db ) |= SLAP_DBFLAG_MONITORING;
3542 SLAP_DBFLAGS( &cm->db ) &= ~SLAP_DBFLAG_MONITORING;
3545 if ( !cm->defer_db_open )
3546 rc = pcache_db_open2( on, cr );
3552 pcache_free_qbase( void *v )
3558 tavl_free( qb->scopes[i], NULL );
3568 slap_overinst *on = (slap_overinst *)be->bd_info;
3569 cache_manager *cm = on->on_bi.bi_private;
3570 query_manager *qm = cm->qm;
3574 if ( cm->save_queries ) {
3576 BerVarray vals = NULL;
3579 Connection conn = { 0 };
3580 OperationBuffer opbuf;
3582 slap_callback cb = { 0 };
3584 SlapReply rs = { REP_RESULT };
3585 Modifications mod = {{ 0 }};
3587 thrctx = ldap_pvt_thread_pool_context();
3589 connection_fake_init( &conn, &opbuf, thrctx );
3592 if ( qm->templates != NULL ) {
3593 for ( tm = qm->templates; tm != NULL; tm = tm->qmnext ) {
3594 for ( qc = tm->query; qc; qc = qc->next ) {
3597 if ( query2url( op, qc, &bv, 0 ) == 0 ) {
3598 ber_bvarray_add_x( &vals, &bv, op->o_tmpmemctx );
3605 op->o_dn = cm->db.be_rootdn;
3606 op->o_ndn = cm->db.be_rootndn;
3608 op->o_tag = LDAP_REQ_MODIFY;
3609 op->o_protocol = LDAP_VERSION3;
3610 cb.sc_response = slap_null_cb;
3611 op->o_callback = &cb;
3612 op->o_time = slap_get_time();
3613 op->o_do_not_cache = 1;
3614 op->o_managedsait = SLAP_CONTROL_CRITICAL;
3616 op->o_req_dn = op->o_bd->be_suffix[0];
3617 op->o_req_ndn = op->o_bd->be_nsuffix[0];
3619 mod.sml_op = LDAP_MOD_REPLACE;
3621 mod.sml_desc = ad_cachedQueryURL;
3622 mod.sml_type = ad_cachedQueryURL->ad_cname;
3623 mod.sml_values = vals;
3624 mod.sml_nvalues = NULL;
3625 mod.sml_numvals = 1;
3626 mod.sml_next = NULL;
3627 Debug( pcache_debug,
3628 "%sSETTING CACHED QUERY URLS\n",
3629 vals == NULL ? "RE" : "", 0, 0 );
3631 op->orm_modlist = &mod;
3633 op->o_bd->be_modify( op, &rs );
3635 ber_bvarray_free_x( vals, op->o_tmpmemctx );
3638 /* cleanup stuff inherited from the original database... */
3639 cm->db.be_limits = NULL;
3640 cm->db.be_acl = NULL;
3642 /* stop the thread ... */
3644 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3645 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, cm->cc_arg ) ) {
3646 ldap_pvt_runqueue_stoptask( &slapd_rq, cm->cc_arg );
3648 ldap_pvt_runqueue_remove( &slapd_rq, cm->cc_arg );
3649 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3652 if ( cm->db.bd_info->bi_db_close ) {
3653 rc = cm->db.bd_info->bi_db_close( &cm->db, NULL );
3655 while ( (tm = qm->templates) != NULL ) {
3656 CachedQuery *qc, *qn;
3657 qm->templates = tm->qmnext;
3658 for ( qc = tm->query; qc; qc = qn ) {
3662 avl_free( tm->qbase, pcache_free_qbase );
3663 free( tm->querystr.bv_val );
3664 ldap_pvt_thread_rdwr_destroy( &tm->t_rwlock );
3665 free( tm->t_attrs.attrs );
3669 for ( i=0; i<cm->numattrsets; i++ ) {
3670 free( qm->attr_sets[i].attrs );
3672 free( qm->attr_sets );
3673 qm->attr_sets = NULL;
3684 slap_overinst *on = (slap_overinst *)be->bd_info;
3685 cache_manager *cm = on->on_bi.bi_private;
3686 query_manager *qm = cm->qm;
3688 if ( cm->db.be_private != NULL ) {
3689 backend_stopdown_one( &cm->db );
3692 ldap_pvt_thread_mutex_destroy( &qm->lru_mutex );
3693 ldap_pvt_thread_mutex_destroy( &cm->cache_mutex );
3700 #ifdef PCACHE_CONTROL_PRIVDB
3702 Control ::= SEQUENCE {
3703 controlType LDAPOID,
3704 criticality BOOLEAN DEFAULT FALSE,
3705 controlValue OCTET STRING OPTIONAL }
3707 controlType ::= 1.3.6.1.4.1.4203.666.11.9.5.1
3709 * criticality must be TRUE; controlValue must be absent.
3717 if ( op->o_ctrlflag[ privDB_cid ] != SLAP_CONTROL_NONE ) {
3718 rs->sr_text = "privateDB control specified multiple times";
3719 return LDAP_PROTOCOL_ERROR;
3722 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
3723 rs->sr_text = "privateDB control value not absent";
3724 return LDAP_PROTOCOL_ERROR;
3727 if ( !ctrl->ldctl_iscritical ) {
3728 rs->sr_text = "privateDB control criticality required";
3729 return LDAP_PROTOCOL_ERROR;
3732 op->o_ctrlflag[ privDB_cid ] = SLAP_CONTROL_CRITICAL;
3734 return LDAP_SUCCESS;
3737 static char *extops[] = {
3738 LDAP_EXOP_MODIFY_PASSWD,
3741 #endif /* PCACHE_CONTROL_PRIVDB */
3743 #ifdef PCACHE_EXOP_QUERY_DELETE
3744 static struct berval pcache_exop_QUERY_DELETE = BER_BVC( PCACHE_EXOP_QUERY_DELETE );
3746 #define LDAP_TAG_EXOP_QUERY_DELETE_BASE ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 0)
3747 #define LDAP_TAG_EXOP_QUERY_DELETE_DN ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 1)
3748 #define LDAP_TAG_EXOP_QUERY_DELETE_UUID ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 2)
3751 ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
3752 requestName [0] LDAPOID,
3753 requestValue [1] OCTET STRING OPTIONAL }
3755 requestName ::= 1.3.6.1.4.1.4203.666.11.9.6.1
3757 requestValue ::= SEQUENCE { CHOICE {
3759 entryDN [1] LDAPDN },
3760 queryID [2] OCTET STRING (SIZE(16))
3761 -- constrained to UUID }
3763 * Either baseDN or entryDN must be present, to allow database selection.
3765 * 1. if baseDN and queryID are present, then the query corresponding
3766 * to queryID is deleted;
3767 * 2. if baseDN is present and queryID is absent, then all queries
3769 * 3. if entryDN is present and queryID is absent, then all queries
3770 * corresponding to the queryID values present in entryDN are deleted;
3771 * 4. if entryDN and queryID are present, then all queries
3772 * corresponding to the queryID values present in entryDN are deleted,
3773 * but only if the value of queryID is contained in the entry;
3775 * Currently, only 1, 3 and 4 are implemented. 2 can be obtained by either
3776 * recursively deleting the database (ldapdelete -r) with PRIVDB control,
3777 * or by removing the database files.
3779 ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
3780 COMPONENTS OF LDAPResult,
3781 responseName [10] LDAPOID OPTIONAL,
3782 responseValue [11] OCTET STRING OPTIONAL }
3784 * responseName and responseValue must be absent.
3788 * - on success, *tagp is either LDAP_TAG_EXOP_QUERY_DELETE_BASE
3789 * or LDAP_TAG_EXOP_QUERY_DELETE_DN.
3790 * - if ndn != NULL, it is set to the normalized DN in the request
3791 * corresponding to either the baseDN or the entryDN, according
3792 * to *tagp; memory is malloc'ed on the Operation's slab, and must
3793 * be freed by the caller.
3794 * - if uuid != NULL, it is set to point to the normalized UUID;
3795 * memory is malloc'ed on the Operation's slab, and must
3796 * be freed by the caller.
3799 pcache_parse_query_delete(
3803 struct berval *uuid,
3807 int rc = LDAP_SUCCESS;
3810 BerElementBuffer berbuf;
3811 BerElement *ber = (BerElement *)&berbuf;
3812 struct berval reqdata = BER_BVNULL;
3824 if ( in == NULL || in->bv_len == 0 ) {
3825 *text = "empty request data field in queryDelete exop";
3826 return LDAP_PROTOCOL_ERROR;
3829 ber_dupbv_x( &reqdata, in, ctx );
3831 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
3832 ber_init2( ber, &reqdata, 0 );
3834 tag = ber_scanf( ber, "{" /*}*/ );
3836 if ( tag == LBER_ERROR ) {
3837 Debug( LDAP_DEBUG_TRACE,
3838 "pcache_parse_query_delete: decoding error.\n",
3840 goto decoding_error;
3843 tag = ber_peek_tag( ber, &len );
3844 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_BASE
3845 || tag == LDAP_TAG_EXOP_QUERY_DELETE_DN )
3849 if ( ndn != NULL ) {
3852 tag = ber_scanf( ber, "m", &dn );
3853 if ( tag == LBER_ERROR ) {
3854 Debug( LDAP_DEBUG_TRACE,
3855 "pcache_parse_query_delete: DN parse failed.\n",
3857 goto decoding_error;
3860 rc = dnNormalize( 0, NULL, NULL, &dn, ndn, ctx );
3861 if ( rc != LDAP_SUCCESS ) {
3862 *text = "invalid DN in queryDelete exop request data";
3867 tag = ber_scanf( ber, "x" /* "m" */ );
3868 if ( tag == LBER_DEFAULT ) {
3869 goto decoding_error;
3873 tag = ber_peek_tag( ber, &len );
3876 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_UUID ) {
3877 if ( uuid != NULL ) {
3879 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
3881 tag = ber_scanf( ber, "m", &bv );
3882 if ( tag == LBER_ERROR ) {
3883 Debug( LDAP_DEBUG_TRACE,
3884 "pcache_parse_query_delete: UUID parse failed.\n",
3886 goto decoding_error;
3889 if ( bv.bv_len != 16 ) {
3890 Debug( LDAP_DEBUG_TRACE,
3891 "pcache_parse_query_delete: invalid UUID length %lu.\n",
3892 (unsigned long)bv.bv_len, 0, 0 );
3893 goto decoding_error;
3896 rc = lutil_uuidstr_from_normalized(
3897 bv.bv_val, bv.bv_len,
3898 uuidbuf, sizeof( uuidbuf ) );
3900 goto decoding_error;
3902 ber_str2bv( uuidbuf, rc, 1, uuid );
3906 tag = ber_skip_tag( ber, &len );
3907 if ( tag == LBER_DEFAULT ) {
3908 goto decoding_error;
3912 Debug( LDAP_DEBUG_TRACE,
3913 "pcache_parse_query_delete: invalid UUID length %lu.\n",
3914 (unsigned long)len, 0, 0 );
3915 goto decoding_error;
3919 tag = ber_peek_tag( ber, &len );
3922 if ( tag != LBER_DEFAULT || len != 0 ) {
3924 Debug( LDAP_DEBUG_TRACE,
3925 "pcache_parse_query_delete: decoding error\n",
3927 rc = LDAP_PROTOCOL_ERROR;
3928 *text = "queryDelete data decoding error";
3931 if ( ndn && !BER_BVISNULL( ndn ) ) {
3932 slap_sl_free( ndn->bv_val, ctx );
3936 if ( uuid && !BER_BVISNULL( uuid ) ) {
3937 slap_sl_free( uuid->bv_val, ctx );
3942 if ( !BER_BVISNULL( &reqdata ) ) {
3943 ber_memfree_x( reqdata.bv_val, ctx );
3950 pcache_exop_query_delete(
3954 BackendDB *bd = op->o_bd;
3956 struct berval uuid = BER_BVNULL,
3958 char buf[ SLAP_TEXT_BUFLEN ];
3960 ber_tag_t tag = LBER_DEFAULT;
3962 if ( LogTest( LDAP_DEBUG_STATS ) ) {
3966 rs->sr_err = pcache_parse_query_delete( op->ore_reqdata,
3967 &tag, &op->o_req_ndn, uuidp,
3968 &rs->sr_text, op->o_tmpmemctx );
3969 if ( rs->sr_err != LDAP_SUCCESS ) {
3973 if ( LogTest( LDAP_DEBUG_STATS ) ) {
3974 assert( !BER_BVISNULL( &op->o_req_ndn ) );
3975 len = snprintf( buf, sizeof( buf ), " dn=\"%s\"", op->o_req_ndn.bv_val );
3977 if ( !BER_BVISNULL( &uuid ) && len < sizeof( buf ) ) {
3978 snprintf( &buf[ len ], sizeof( buf ) - len, " queryId=\"%s\"", uuid.bv_val );
3981 Debug( LDAP_DEBUG_STATS, "%s QUERY DELETE%s\n",
3982 op->o_log_prefix, buf, 0 );
3984 op->o_req_dn = op->o_req_ndn;
3986 op->o_bd = select_backend( &op->o_req_ndn, 0 );
3987 rs->sr_err = backend_check_restrictions( op, rs,
3988 (struct berval *)&pcache_exop_QUERY_DELETE );
3989 if ( rs->sr_err != LDAP_SUCCESS ) {
3993 if ( op->o_bd->be_extended == NULL ) {
3994 send_ldap_error( op, rs, LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
3995 "backend does not support extended operations" );
3999 op->o_bd->be_extended( op, rs );
4002 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
4003 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
4004 BER_BVZERO( &op->o_req_ndn );
4005 BER_BVZERO( &op->o_req_dn );
4008 if ( !BER_BVISNULL( &uuid ) ) {
4009 op->o_tmpfree( uuid.bv_val, op->o_tmpmemctx );
4018 pcache_op_extended( Operation *op, SlapReply *rs )
4020 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
4021 cache_manager *cm = on->on_bi.bi_private;
4023 #ifdef PCACHE_CONTROL_PRIVDB
4024 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL ) {
4025 return pcache_op_privdb( op, rs );
4027 #endif /* PCACHE_CONTROL_PRIVDB */
4029 if ( bvmatch( &op->ore_reqoid, &pcache_exop_QUERY_DELETE ) ) {
4030 struct berval uuid = BER_BVNULL;
4031 ber_tag_t tag = LBER_DEFAULT;
4033 rs->sr_err = pcache_parse_query_delete( op->ore_reqdata,
4034 &tag, NULL, &uuid, &rs->sr_text, op->o_tmpmemctx );
4035 assert( rs->sr_err == LDAP_SUCCESS );
4037 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_DN ) {
4038 /* remove all queries related to the selected entry */
4039 rs->sr_err = pcache_remove_entry_queries_from_cache( op,
4040 cm, &op->o_req_ndn, &uuid );
4042 } else if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_BASE ) {
4043 if ( !BER_BVISNULL( &uuid ) ) {
4044 /* remove the selected query */
4045 rs->sr_err = pcache_remove_query_from_cache( op,
4049 /* TODO: remove all queries */
4050 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
4051 rs->sr_text = "deletion of all queries not implemented";
4055 op->o_tmpfree( uuid.bv_val, op->o_tmpmemctx );
4060 #endif /* PCACHE_EXOP_QUERY_DELETE */
4062 static slap_overinst pcache;
4064 static char *obsolete_names[] = {
4069 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
4071 #endif /* SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC */
4076 struct berval debugbv = BER_BVC("pcache");
4078 code = slap_loglevel_get( &debugbv, &pcache_debug );
4083 #ifdef PCACHE_CONTROL_PRIVDB
4084 code = register_supported_control( PCACHE_CONTROL_PRIVDB,
4085 SLAP_CTRL_BIND|SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE, extops,
4086 parse_privdb_ctrl, &privDB_cid );
4087 if ( code != LDAP_SUCCESS ) {
4088 Debug( LDAP_DEBUG_ANY,
4089 "pcache_initialize: failed to register control %s (%d)\n",
4090 PCACHE_CONTROL_PRIVDB, code, 0 );
4093 #endif /* PCACHE_CONTROL_PRIVDB */
4095 #ifdef PCACHE_EXOP_QUERY_DELETE
4096 code = load_extop2( (struct berval *)&pcache_exop_QUERY_DELETE,
4097 SLAP_EXOP_WRITES|SLAP_EXOP_HIDE, pcache_exop_query_delete,
4099 if ( code != LDAP_SUCCESS ) {
4100 Debug( LDAP_DEBUG_ANY,
4101 "pcache_initialize: unable to register queryDelete exop: %d.\n",
4105 #endif /* PCACHE_EXOP_QUERY_DELETE */
4107 for ( i = 0; as[i].desc != NULL; i++ ) {
4108 code = register_at( as[i].desc, as[i].adp, 0 );
4110 Debug( LDAP_DEBUG_ANY,
4111 "pcache_initialize: register_at #%d failed\n", i, 0, 0 );
4114 (*as[i].adp)->ad_type->sat_flags |= SLAP_AT_HIDE;
4117 pcache.on_bi.bi_type = "pcache";
4118 pcache.on_bi.bi_obsolete_names = obsolete_names;
4119 pcache.on_bi.bi_db_init = pcache_db_init;
4120 pcache.on_bi.bi_db_config = pcache_db_config;
4121 pcache.on_bi.bi_db_open = pcache_db_open;
4122 pcache.on_bi.bi_db_close = pcache_db_close;
4123 pcache.on_bi.bi_db_destroy = pcache_db_destroy;
4125 pcache.on_bi.bi_op_search = pcache_op_search;
4126 #ifdef PCACHE_CONTROL_PRIVDB
4127 pcache.on_bi.bi_op_bind = pcache_op_privdb;
4128 pcache.on_bi.bi_op_compare = pcache_op_privdb;
4129 pcache.on_bi.bi_op_modrdn = pcache_op_privdb;
4130 pcache.on_bi.bi_op_modify = pcache_op_privdb;
4131 pcache.on_bi.bi_op_add = pcache_op_privdb;
4132 pcache.on_bi.bi_op_delete = pcache_op_privdb;
4133 #endif /* PCACHE_CONTROL_PRIVDB */
4134 #ifdef PCACHE_EXOP_QUERY_DELETE
4135 pcache.on_bi.bi_extended = pcache_op_extended;
4136 #elif defined( PCACHE_CONTROL_PRIVDB )
4137 pcache.on_bi.bi_extended = pcache_op_privdb;
4140 pcache.on_bi.bi_chk_controls = pcache_chk_controls;
4142 pcache.on_bi.bi_cf_ocs = pcocs;
4144 code = config_register_schema( pccfg, pcocs );
4145 if ( code ) return code;
4147 return overlay_register( &pcache );
4150 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
4151 int init_module(int argc, char *argv[]) {
4152 return pcache_initialize();
4156 #endif /* defined(SLAPD_OVER_PROXYCACHE) */