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 time_t ttr; /* time to refresh */
136 struct attr_set t_attrs; /* filter attrs + attr_set */
144 } pc_caching_reason_t;
146 static const char *pc_caching_reason_str[] = {
155 struct query_manager_s;
157 /* prototypes for functions for 1) query containment
158 * 2) query addition, 3) cache replacement
160 typedef CachedQuery *(QCfunc)(Operation *op, struct query_manager_s*,
161 Query*, QueryTemplate*);
162 typedef CachedQuery *(AddQueryfunc)(Operation *op, struct query_manager_s*,
163 Query*, QueryTemplate*, pc_caching_reason_t, int wlock);
164 typedef void (CRfunc)(struct query_manager_s*, struct berval*);
166 /* LDAP query cache */
167 typedef struct query_manager_s {
168 struct attr_set* attr_sets; /* possible sets of projected attributes */
169 QueryTemplate* templates; /* cacheable templates */
171 CachedQuery* lru_top; /* top and bottom of LRU list */
172 CachedQuery* lru_bottom;
174 ldap_pvt_thread_mutex_t lru_mutex; /* mutex for accessing LRU list */
176 /* Query cache methods */
177 QCfunc *qcfunc; /* Query containment*/
178 CRfunc *crfunc; /* cache replacement */
179 AddQueryfunc *addfunc; /* add query */
182 /* LDAP query cache manager */
183 typedef struct cache_manager_s {
184 BackendDB db; /* underlying database */
185 unsigned long num_cached_queries; /* total number of cached queries */
186 unsigned long max_queries; /* upper bound on # of cached queries */
187 int save_queries; /* save cached queries across restarts */
188 int check_cacheability; /* check whether a query is cacheable */
189 int numattrsets; /* number of attribute sets */
190 int cur_entries; /* current number of entries cached */
191 int max_entries; /* max number of entries cached */
192 int num_entries_limit; /* max # of entries in a cacheable query */
194 char response_cb; /* install the response callback
195 * at the tail of the callback list */
196 #define PCACHE_RESPONSE_CB_HEAD 0
197 #define PCACHE_RESPONSE_CB_TAIL 1
198 char defer_db_open; /* defer open for online add */
200 time_t cc_period; /* interval between successive consistency checks (sec) */
201 #define PCACHE_CC_PAUSED 1
202 #define PCACHE_CC_OFFLINE 2
206 ldap_pvt_thread_mutex_t cache_mutex;
208 query_manager* qm; /* query cache managed by the cache manager */
211 static int pcache_debug;
213 #ifdef PCACHE_CONTROL_PRIVDB
214 static int privDB_cid;
215 #endif /* PCACHE_CONTROL_PRIVDB */
217 static AttributeDescription *ad_queryId, *ad_cachedQueryURL;
220 AttributeDescription **adp;
222 { "( 1.3.6.1.4.1.4203.666.11.9.1.1 "
224 "DESC 'ID of query the entry belongs to, formatted as a UUID' "
225 "EQUALITY octetStringMatch "
226 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40{64} "
227 "NO-USER-MODIFICATION "
228 "USAGE directoryOperation )",
230 { "( 1.3.6.1.4.1.4203.666.11.9.1.2 "
231 "NAME 'cachedQueryURL' "
232 "DESC 'URI describing a cached query' "
233 "EQUALITY caseExactMatch "
234 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 "
235 "NO-USER-MODIFICATION "
236 "USAGE directoryOperation )",
237 &ad_cachedQueryURL },
246 AttributeName** filter_attrs,
248 int* filter_got_oc );
255 QueryTemplate *templ,
256 pc_caching_reason_t why,
263 struct berval *query_uuid );
266 * Turn a cached query into its URL representation
269 query2url( Operation *op, CachedQuery *q, struct berval *urlbv, int dolock )
271 struct berval bv_scope,
273 char attrset_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
274 expiry_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
275 answerable_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
277 ber_len_t attrset_len,
282 ldap_pvt_thread_rdwr_rlock( &q->rwlock );
285 ldap_pvt_scope2bv( q->scope, &bv_scope );
286 filter2bv_x( op, q->filter, &bv_filter );
287 attrset_len = sprintf( attrset_buf,
288 "%lu", (unsigned long)q->qtemp->attr_set_index );
289 expiry_len = sprintf( expiry_buf,
290 "%lu", (unsigned long)q->expiry_time );
291 answerable_len = snprintf( answerable_buf, sizeof( answerable_buf ),
292 "%lu", q->answerable_cnt );
294 urlbv->bv_len = STRLENOF( "ldap:///" )
295 + q->qbase->base.bv_len
300 + STRLENOF( "?x-uuid=" )
302 + STRLENOF( ",x-attrset=" )
304 + STRLENOF( ",x-expiry=" )
306 + STRLENOF( ",x-answerable=" )
308 ptr = urlbv->bv_val = ber_memalloc_x( urlbv->bv_len + 1, op->o_tmpmemctx );
309 ptr = lutil_strcopy( ptr, "ldap:///" );
310 ptr = lutil_strcopy( ptr, q->qbase->base.bv_val );
311 ptr = lutil_strcopy( ptr, "??" );
312 ptr = lutil_strcopy( ptr, bv_scope.bv_val );
313 ptr = lutil_strcopy( ptr, "?" );
314 ptr = lutil_strcopy( ptr, bv_filter.bv_val );
315 ptr = lutil_strcopy( ptr, "?x-uuid=" );
316 ptr = lutil_strcopy( ptr, q->q_uuid.bv_val );
317 ptr = lutil_strcopy( ptr, ",x-attrset=" );
318 ptr = lutil_strcopy( ptr, attrset_buf );
319 ptr = lutil_strcopy( ptr, ",x-expiry=" );
320 ptr = lutil_strcopy( ptr, expiry_buf );
321 ptr = lutil_strcopy( ptr, ",x-answerable=" );
322 ptr = lutil_strcopy( ptr, answerable_buf );
324 ber_memfree_x( bv_filter.bv_val, op->o_tmpmemctx );
327 ldap_pvt_thread_rdwr_runlock( &q->rwlock );
334 * Turn an URL representing a formerly cached query into a cached query,
335 * and try to cache it
346 LDAPURLDesc *lud = NULL;
348 tempstr = BER_BVNULL,
352 unsigned long answerable_cnt;
355 #define GOT_UUID 0x1U
356 #define GOT_ATTRSET 0x2U
357 #define GOT_EXPIRY 0x4U
358 #define GOT_ANSWERABLE 0x8U
359 #define GOT_ALL (GOT_UUID|GOT_ATTRSET|GOT_EXPIRY|GOT_ANSWERABLE)
362 rc = ldap_url_parse( url, &lud );
363 if ( rc != LDAP_URL_SUCCESS ) {
367 /* non-allowed fields */
368 if ( lud->lud_host != NULL ) {
373 if ( lud->lud_attrs != NULL ) {
379 if ( strcmp( lud->lud_scheme, "ldap" ) != 0 ) {
384 /* required fields */
385 if ( lud->lud_dn == NULL || lud->lud_dn[ 0 ] == '\0' ) {
390 switch ( lud->lud_scope ) {
391 case LDAP_SCOPE_BASE:
392 case LDAP_SCOPE_ONELEVEL:
393 case LDAP_SCOPE_SUBTREE:
394 case LDAP_SCOPE_SUBORDINATE:
402 if ( lud->lud_filter == NULL || lud->lud_filter[ 0 ] == '\0' ) {
407 if ( lud->lud_exts == NULL ) {
412 for ( i = 0; lud->lud_exts[ i ] != NULL; i++ ) {
413 if ( strncmp( lud->lud_exts[ i ], "x-uuid=", STRLENOF( "x-uuid=" ) ) == 0 ) {
414 struct berval tmpUUID;
415 Syntax *syn_UUID = slap_schema.si_ad_entryUUID->ad_type->sat_syntax;
417 if ( got & GOT_UUID ) {
422 ber_str2bv( &lud->lud_exts[ i ][ STRLENOF( "x-uuid=" ) ], 0, 0, &tmpUUID );
423 rc = syn_UUID->ssyn_pretty( syn_UUID, &tmpUUID, &uuid, NULL );
424 if ( rc != LDAP_SUCCESS ) {
429 } else if ( strncmp( lud->lud_exts[ i ], "x-attrset=", STRLENOF( "x-attrset=" ) ) == 0 ) {
430 if ( got & GOT_ATTRSET ) {
435 rc = lutil_atoi( &attrset, &lud->lud_exts[ i ][ STRLENOF( "x-attrset=" ) ] );
441 } else if ( strncmp( lud->lud_exts[ i ], "x-expiry=", STRLENOF( "x-expiry=" ) ) == 0 ) {
444 if ( got & GOT_EXPIRY ) {
449 rc = lutil_atoul( &l, &lud->lud_exts[ i ][ STRLENOF( "x-expiry=" ) ] );
453 expiry_time = (time_t)l;
456 } else if ( strncmp( lud->lud_exts[ i ], "x-answerable=", STRLENOF( "x-answerable=" ) ) == 0 ) {
457 if ( got & GOT_ANSWERABLE ) {
462 rc = lutil_atoul( &answerable_cnt, &lud->lud_exts[ i ][ STRLENOF( "x-answerable=" ) ] );
466 got |= GOT_ANSWERABLE;
474 if ( got != GOT_ALL ) {
479 /* ignore expired queries */
480 if ( expiry_time <= slap_get_time()) {
482 SlapReply rs2 = { 0 };
484 memset( &op2.oq_search, 0, sizeof( op2.oq_search ) );
486 (void)remove_query_data( &op2, &rs2, &uuid );
491 ber_str2bv( lud->lud_dn, 0, 0, &base );
492 rc = dnNormalize( 0, NULL, NULL, &base, &query.base, NULL );
493 if ( rc != LDAP_SUCCESS ) {
496 query.scope = lud->lud_scope;
497 query.filter = str2filter( lud->lud_filter );
499 tempstr.bv_val = ch_malloc( strlen( lud->lud_filter ) + 1 );
501 if ( filter2template( op, query.filter, &tempstr, NULL, NULL, NULL ) ) {
502 ch_free( tempstr.bv_val );
507 /* check for query containment */
508 qt = qm->attr_sets[attrset].templates;
509 for ( ; qt; qt = qt->qtnext ) {
510 /* find if template i can potentially answer tempstr */
511 if ( bvmatch( &qt->querystr, &tempstr ) ) {
521 cq = add_query( op, qm, &query, qt, PC_POSITIVE, 0 );
523 cq->expiry_time = expiry_time;
525 cq->answerable_cnt = answerable_cnt;
527 /* it's now into cq->filter */
537 if ( query.filter != NULL ) filter_free( query.filter );
538 if ( !BER_BVISNULL( &tempstr ) ) ch_free( tempstr.bv_val );
539 if ( !BER_BVISNULL( &query.base ) ) ch_free( query.base.bv_val );
540 if ( !BER_BVISNULL( &uuid ) ) ch_free( uuid.bv_val );
541 if ( lud != NULL ) ldap_free_urldesc( lud );
546 /* Return 1 for an added entry, else 0 */
551 struct berval* query_uuid )
554 Modifications* modlist = NULL;
555 const char* text = NULL;
557 char textbuf[SLAP_TEXT_BUFLEN];
558 size_t textlen = sizeof(textbuf);
560 SlapReply sreply = {REP_RESULT};
562 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
567 /* add queryId attribute */
568 attr_merge_one( e, ad_queryId, query_uuid, NULL );
570 /* append the attribute list from the fetched entry */
571 e->e_attrs->a_next = attr;
573 op->o_tag = LDAP_REQ_ADD;
574 op->o_protocol = LDAP_VERSION3;
575 op->o_callback = &cb;
576 op->o_time = slap_get_time();
577 op->o_do_not_cache = 1;
580 op->o_req_dn = e->e_name;
581 op->o_req_ndn = e->e_nname;
582 rc = op->o_bd->be_add( op, &sreply );
584 if ( rc != LDAP_SUCCESS ) {
585 if ( rc == LDAP_ALREADY_EXISTS ) {
586 slap_entry2mods( e, &modlist, &text, textbuf, textlen );
587 modlist->sml_op = LDAP_MOD_ADD;
588 op->o_tag = LDAP_REQ_MODIFY;
589 op->orm_modlist = modlist;
590 op->o_bd->be_modify( op, &sreply );
591 slap_mods_free( modlist, 1 );
592 } else if ( rc == LDAP_REFERRAL ||
593 rc == LDAP_NO_SUCH_OBJECT ) {
594 syncrepl_add_glue( op, e );
603 if ( op->ora_e == e )
604 be_entry_release_w( op, e );
611 /* Length-ordered sort on normalized DNs */
612 static int pcache_dn_cmp( const void *v1, const void *v2 )
614 const Qbase *q1 = v1, *q2 = v2;
616 int rc = q1->base.bv_len - q2->base.bv_len;
618 rc = strncmp( q1->base.bv_val, q2->base.bv_val, q1->base.bv_len );
622 static int lex_bvcmp( struct berval *bv1, struct berval *bv2 )
625 dif = bv1->bv_len - bv2->bv_len;
627 if ( dif > 0 ) len -= dif;
628 len = memcmp( bv1->bv_val, bv2->bv_val, len );
634 /* compare the current value in each filter */
635 static int pcache_filter_cmp( Filter *f1, Filter *f2 )
637 int rc, weight1, weight2;
639 switch( f1->f_choice ) {
640 case LDAP_FILTER_PRESENT:
643 case LDAP_FILTER_EQUALITY:
651 switch( f2->f_choice ) {
652 case LDAP_FILTER_PRESENT:
655 case LDAP_FILTER_EQUALITY:
663 rc = weight1 - weight2;
669 rc = lex_bvcmp( &f1->f_av_value, &f2->f_av_value );
672 if ( f1->f_choice == LDAP_FILTER_SUBSTRINGS ) {
674 if ( !BER_BVISNULL( &f1->f_sub_initial )) {
675 if ( !BER_BVISNULL( &f2->f_sub_initial )) {
676 rc = lex_bvcmp( &f1->f_sub_initial,
677 &f2->f_sub_initial );
681 } else if ( !BER_BVISNULL( &f2->f_sub_initial )) {
685 if ( f1->f_sub_any ) {
686 if ( f2->f_sub_any ) {
687 rc = lex_bvcmp( f1->f_sub_any,
692 } else if ( f2->f_sub_any ) {
696 if ( !BER_BVISNULL( &f1->f_sub_final )) {
697 if ( !BER_BVISNULL( &f2->f_sub_final )) {
698 rc = lex_bvcmp( &f1->f_sub_final,
703 } else if ( !BER_BVISNULL( &f2->f_sub_final )) {
707 rc = lex_bvcmp( &f1->f_mr_value,
721 while ( f1->f_choice == LDAP_FILTER_AND || f1->f_choice == LDAP_FILTER_OR )
723 while ( f2->f_choice == LDAP_FILTER_AND || f2->f_choice == LDAP_FILTER_OR )
725 rc = pcache_filter_cmp( f1, f2 );
733 /* compare filters in each query */
734 static int pcache_query_cmp( const void *v1, const void *v2 )
736 const CachedQuery *q1 = v1, *q2 =v2;
737 return pcache_filter_cmp( q1->first, q2->first );
740 /* add query on top of LRU list */
742 add_query_on_top (query_manager* qm, CachedQuery* qc)
744 CachedQuery* top = qm->lru_top;
755 Debug( pcache_debug, "Base of added query = %s\n",
756 qc->qbase->base.bv_val, 0, 0 );
759 /* remove_query from LRU list */
762 remove_query (query_manager* qm, CachedQuery* qc)
785 qc->lru_up = qc->lru_down = NULL;
788 /* find and remove string2 from string1
789 * from start if position = 1,
790 * from end if position = 3,
791 * from anywhere if position = 2
792 * string1 is overwritten if position = 2.
796 find_and_remove(struct berval* ber1, struct berval* ber2, int position)
807 if ( ber1->bv_len >= ber2->bv_len && !memcmp( ber1->bv_val,
808 ber2->bv_val, ber2->bv_len )) {
810 ber1->bv_val += ber2->bv_len;
811 ber1->bv_len -= ber2->bv_len;
816 ber1->bv_val[ber1->bv_len] = '\0';
817 temp = strstr( ber1->bv_val, ber2->bv_val );
819 strcpy( temp, temp+ber2->bv_len );
820 ber1->bv_len -= ber2->bv_len;
826 if ( ber1->bv_len >= ber2->bv_len &&
827 !memcmp( ber1->bv_val+ber1->bv_len-ber2->bv_len, ber2->bv_val,
830 ber1->bv_len -= ber2->bv_len;
838 static struct berval*
839 merge_init_final(Operation *op, struct berval* init, struct berval* any,
840 struct berval* final)
842 struct berval* merged, *temp;
843 int i, any_count, count;
845 for (any_count=0; any && any[any_count].bv_val; any_count++)
855 merged = (struct berval*)op->o_tmpalloc( (count+1)*sizeof(struct berval),
860 ber_dupbv_x( temp, init, op->o_tmpmemctx );
864 for (i=0; i<any_count; i++) {
865 ber_dupbv_x( temp, any, op->o_tmpmemctx );
870 ber_dupbv_x( temp, final, op->o_tmpmemctx );
877 /* Each element in stored must be found in incoming. Incoming is overwritten.
880 strings_containment(struct berval* stored, struct berval* incoming)
882 struct berval* element;
886 for ( element=stored; element->bv_val != NULL; element++ ) {
887 for (j = k; incoming[j].bv_val != NULL; j++) {
888 if (find_and_remove(&(incoming[j]), element, 2)) {
905 substr_containment_substr(Operation *op, Filter* stored, Filter* incoming)
909 struct berval init_incoming;
910 struct berval final_incoming;
911 struct berval *remaining_incoming = NULL;
913 if ((!(incoming->f_sub_initial.bv_val) && (stored->f_sub_initial.bv_val))
914 || (!(incoming->f_sub_final.bv_val) && (stored->f_sub_final.bv_val)))
917 init_incoming = incoming->f_sub_initial;
918 final_incoming = incoming->f_sub_final;
920 if (find_and_remove(&init_incoming,
921 &(stored->f_sub_initial), 1) && find_and_remove(&final_incoming,
922 &(stored->f_sub_final), 3))
924 if (stored->f_sub_any == NULL) {
928 remaining_incoming = merge_init_final(op, &init_incoming,
929 incoming->f_sub_any, &final_incoming);
930 rc = strings_containment(stored->f_sub_any, remaining_incoming);
931 ber_bvarray_free_x( remaining_incoming, op->o_tmpmemctx );
938 substr_containment_equality(Operation *op, Filter* stored, Filter* incoming)
940 struct berval incoming_val[2];
943 incoming_val[1] = incoming->f_av_value;
945 if (find_and_remove(incoming_val+1,
946 &(stored->f_sub_initial), 1) && find_and_remove(incoming_val+1,
947 &(stored->f_sub_final), 3)) {
948 if (stored->f_sub_any == NULL){
952 ber_dupbv_x( incoming_val, incoming_val+1, op->o_tmpmemctx );
953 BER_BVZERO( incoming_val+1 );
954 rc = strings_containment(stored->f_sub_any, incoming_val);
955 op->o_tmpfree( incoming_val[0].bv_val, op->o_tmpmemctx );
962 filter_first( Filter *f )
964 while ( f->f_choice == LDAP_FILTER_OR || f->f_choice == LDAP_FILTER_AND )
971 find_filter( Operation *op, Avlnode *root, Filter *inputf, Filter *first )
975 MatchingRule* mrule = NULL;
976 int res=0, eqpass= 0;
984 /* substring matches sort to the end, and we just have to
985 * walk the entire list.
987 if ( first->f_choice == LDAP_FILTER_SUBSTRINGS ) {
988 ptr = tavl_end( root, 1 );
991 ptr = tavl_find3( root, &cq, pcache_query_cmp, &ret );
992 dir = (first->f_choice == LDAP_FILTER_GE) ? TAVL_DIR_LEFT :
1001 /* an incoming substr query can only be satisfied by a cached
1004 if ( first->f_choice == LDAP_FILTER_SUBSTRINGS &&
1005 qc->first->f_choice != LDAP_FILTER_SUBSTRINGS )
1008 /* an incoming eq query can be satisfied by a cached eq or substr
1011 if ( first->f_choice == LDAP_FILTER_EQUALITY ) {
1012 if ( eqpass == 0 ) {
1013 if ( qc->first->f_choice != LDAP_FILTER_EQUALITY ) {
1014 nextpass: eqpass = 1;
1015 ptr = tavl_end( root, 1 );
1016 dir = TAVL_DIR_LEFT;
1020 if ( qc->first->f_choice != LDAP_FILTER_SUBSTRINGS )
1026 switch (fs->f_choice) {
1027 case LDAP_FILTER_EQUALITY:
1028 if (fi->f_choice == LDAP_FILTER_EQUALITY)
1029 mrule = fs->f_ava->aa_desc->ad_type->sat_equality;
1033 case LDAP_FILTER_GE:
1034 case LDAP_FILTER_LE:
1035 mrule = fs->f_ava->aa_desc->ad_type->sat_ordering;
1042 rc = value_match(&ret, fs->f_ava->aa_desc, mrule,
1043 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
1044 &(fi->f_ava->aa_value),
1045 &(fs->f_ava->aa_value), &text);
1046 if (rc != LDAP_SUCCESS) {
1049 if ( fi==first && fi->f_choice==LDAP_FILTER_EQUALITY && ret )
1052 switch (fs->f_choice) {
1053 case LDAP_FILTER_OR:
1054 case LDAP_FILTER_AND:
1059 case LDAP_FILTER_SUBSTRINGS:
1060 /* check if the equality query can be
1061 * answered with cached substring query */
1062 if ((fi->f_choice == LDAP_FILTER_EQUALITY)
1063 && substr_containment_equality( op,
1066 /* check if the substring query can be
1067 * answered with cached substring query */
1068 if ((fi->f_choice ==LDAP_FILTER_SUBSTRINGS
1069 ) && substr_containment_substr( op,
1075 case LDAP_FILTER_PRESENT:
1080 case LDAP_FILTER_EQUALITY:
1086 case LDAP_FILTER_GE:
1087 if (mrule && ret >= 0)
1092 case LDAP_FILTER_LE:
1093 if (mrule && ret <= 0)
1098 case LDAP_FILTER_NOT:
1104 } while((res) && (fi != NULL) && (fs != NULL));
1108 ptr = tavl_next( ptr, dir );
1113 /* check whether query is contained in any of
1114 * the cached queries in template
1116 static CachedQuery *
1117 query_containment(Operation *op, query_manager *qm,
1119 QueryTemplate *templa)
1122 int depth = 0, tscope;
1123 Qbase qbase, *qbptr = NULL;
1126 if (query->filter != NULL) {
1129 Debug( pcache_debug, "Lock QC index = %p\n",
1130 (void *) templa, 0, 0 );
1131 qbase.base = query->base;
1133 first = filter_first( query->filter );
1135 ldap_pvt_thread_rdwr_rlock(&templa->t_rwlock);
1138 qbptr = avl_find( templa->qbase, &qbase, pcache_dn_cmp );
1140 tscope = query->scope;
1141 /* Find a matching scope:
1142 * match at depth 0 OK
1145 * subord at depth > 0 OK
1146 * subtree at any depth OK
1148 * subtree or subord at any depth OK
1150 * subtree or subord at any depth OK
1152 * subord at depth > 0 OK
1153 * subtree at any depth OK
1155 for ( tscope = 0 ; tscope <= LDAP_SCOPE_CHILDREN; tscope++ ) {
1156 switch ( query->scope ) {
1157 case LDAP_SCOPE_BASE:
1158 if ( tscope == LDAP_SCOPE_BASE && depth ) continue;
1159 if ( tscope == LDAP_SCOPE_ONE && depth != 1) continue;
1160 if ( tscope == LDAP_SCOPE_CHILDREN && !depth ) continue;
1162 case LDAP_SCOPE_ONE:
1163 if ( tscope == LDAP_SCOPE_BASE )
1164 tscope = LDAP_SCOPE_ONE;
1165 if ( tscope == LDAP_SCOPE_ONE && depth ) continue;
1166 if ( !depth ) break;
1167 if ( tscope < LDAP_SCOPE_SUBTREE )
1168 tscope = LDAP_SCOPE_SUBTREE;
1170 case LDAP_SCOPE_SUBTREE:
1171 if ( tscope < LDAP_SCOPE_SUBTREE )
1172 tscope = LDAP_SCOPE_SUBTREE;
1173 if ( tscope == LDAP_SCOPE_CHILDREN && !depth ) continue;
1175 case LDAP_SCOPE_CHILDREN:
1176 if ( tscope < LDAP_SCOPE_SUBTREE )
1177 tscope = LDAP_SCOPE_SUBTREE;
1180 if ( !qbptr->scopes[tscope] ) continue;
1183 qc = find_filter( op, qbptr->scopes[tscope],
1184 query->filter, first );
1186 if ( qc->q_sizelimit ) {
1187 ldap_pvt_thread_rdwr_runlock(&templa->t_rwlock);
1190 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1191 if (qm->lru_top != qc) {
1192 remove_query(qm, qc);
1193 add_query_on_top(qm, qc);
1195 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1200 if ( be_issuffix( op->o_bd, &qbase.base ))
1203 dnParent( &qbase.base, &pdn );
1208 Debug( pcache_debug,
1209 "Not answerable: Unlock QC index=%p\n",
1210 (void *) templa, 0, 0 );
1211 ldap_pvt_thread_rdwr_runlock(&templa->t_rwlock);
1217 free_query (CachedQuery* qc)
1219 free(qc->q_uuid.bv_val);
1220 filter_free(qc->filter);
1221 ldap_pvt_thread_mutex_destroy(&qc->answerable_cnt_mutex);
1222 ldap_pvt_thread_rdwr_destroy( &qc->rwlock );
1223 memset(qc, 0, sizeof(*qc));
1228 /* Add query to query cache, the returned Query is locked for writing */
1229 static CachedQuery *
1234 QueryTemplate *templ,
1235 pc_caching_reason_t why,
1238 CachedQuery* new_cached_query = (CachedQuery*) ch_malloc(sizeof(CachedQuery));
1244 new_cached_query->qtemp = templ;
1245 BER_BVZERO( &new_cached_query->q_uuid );
1246 new_cached_query->q_sizelimit = 0;
1254 ttl = templ->negttl;
1258 ttl = templ->limitttl;
1265 new_cached_query->expiry_time = slap_get_time() + ttl;
1267 new_cached_query->answerable_cnt = 0;
1268 ldap_pvt_thread_mutex_init(&new_cached_query->answerable_cnt_mutex);
1270 new_cached_query->lru_up = NULL;
1271 new_cached_query->lru_down = NULL;
1272 Debug( pcache_debug, "Added query expires at %ld (%s)\n",
1273 (long) new_cached_query->expiry_time,
1274 pc_caching_reason_str[ why ], 0 );
1276 new_cached_query->scope = query->scope;
1277 new_cached_query->filter = query->filter;
1278 new_cached_query->first = first = filter_first( query->filter );
1280 ldap_pvt_thread_rdwr_init(&new_cached_query->rwlock);
1282 ldap_pvt_thread_rdwr_wlock(&new_cached_query->rwlock);
1284 qb.base = query->base;
1286 /* Adding a query */
1287 Debug( pcache_debug, "Lock AQ index = %p\n",
1288 (void *) templ, 0, 0 );
1289 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
1290 qbase = avl_find( templ->qbase, &qb, pcache_dn_cmp );
1292 qbase = ch_calloc( 1, sizeof(Qbase) + qb.base.bv_len + 1 );
1293 qbase->base.bv_len = qb.base.bv_len;
1294 qbase->base.bv_val = (char *)(qbase+1);
1295 memcpy( qbase->base.bv_val, qb.base.bv_val, qb.base.bv_len );
1296 qbase->base.bv_val[qbase->base.bv_len] = '\0';
1297 avl_insert( &templ->qbase, qbase, pcache_dn_cmp, avl_dup_error );
1299 new_cached_query->next = templ->query;
1300 new_cached_query->prev = NULL;
1301 new_cached_query->qbase = qbase;
1302 rc = tavl_insert( &qbase->scopes[query->scope], new_cached_query,
1303 pcache_query_cmp, avl_dup_error );
1306 if (templ->query == NULL)
1307 templ->query_last = new_cached_query;
1309 templ->query->prev = new_cached_query;
1310 templ->query = new_cached_query;
1311 templ->no_of_queries++;
1313 ch_free( new_cached_query );
1314 new_cached_query = find_filter( op, qbase->scopes[query->scope],
1315 query->filter, first );
1316 filter_free( query->filter );
1317 query->filter = NULL;
1319 Debug( pcache_debug, "TEMPLATE %p QUERIES++ %d\n",
1320 (void *) templ, templ->no_of_queries, 0 );
1322 Debug( pcache_debug, "Unlock AQ index = %p \n",
1323 (void *) templ, 0, 0 );
1324 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
1326 /* Adding on top of LRU list */
1328 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1329 add_query_on_top(qm, new_cached_query);
1330 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1332 return rc == 0 ? new_cached_query : NULL;
1336 remove_from_template (CachedQuery* qc, QueryTemplate* template)
1338 if (!qc->prev && !qc->next) {
1339 template->query_last = template->query = NULL;
1340 } else if (qc->prev == NULL) {
1341 qc->next->prev = NULL;
1342 template->query = qc->next;
1343 } else if (qc->next == NULL) {
1344 qc->prev->next = NULL;
1345 template->query_last = qc->prev;
1347 qc->next->prev = qc->prev;
1348 qc->prev->next = qc->next;
1350 tavl_delete( &qc->qbase->scopes[qc->scope], qc, pcache_query_cmp );
1351 qc->qbase->queries--;
1352 if ( qc->qbase->queries == 0 ) {
1353 avl_delete( &template->qbase, qc->qbase, pcache_dn_cmp );
1354 ch_free( qc->qbase );
1358 template->no_of_queries--;
1361 /* remove bottom query of LRU list from the query cache */
1363 * NOTE: slight change in functionality.
1365 * - if result->bv_val is NULL, the query at the bottom of the LRU
1367 * - otherwise, the query whose UUID is *result is removed
1368 * - if not found, result->bv_val is zeroed
1371 cache_replacement(query_manager* qm, struct berval *result)
1373 CachedQuery* bottom;
1374 QueryTemplate *temp;
1376 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1377 if ( BER_BVISNULL( result ) ) {
1378 bottom = qm->lru_bottom;
1381 Debug ( pcache_debug,
1382 "Cache replacement invoked without "
1383 "any query in LRU list\n", 0, 0, 0 );
1384 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1389 for ( bottom = qm->lru_bottom;
1391 bottom = bottom->lru_up )
1393 if ( bvmatch( result, &bottom->q_uuid ) ) {
1399 Debug ( pcache_debug,
1400 "Could not find query with uuid=\"%s\""
1401 "in LRU list\n", result->bv_val, 0, 0 );
1402 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1403 BER_BVZERO( result );
1408 temp = bottom->qtemp;
1409 remove_query(qm, bottom);
1410 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1412 *result = bottom->q_uuid;
1413 BER_BVZERO( &bottom->q_uuid );
1415 Debug( pcache_debug, "Lock CR index = %p\n", (void *) temp, 0, 0 );
1416 ldap_pvt_thread_rdwr_wlock(&temp->t_rwlock);
1417 remove_from_template(bottom, temp);
1418 Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
1419 (void *) temp, temp->no_of_queries, 0 );
1420 Debug( pcache_debug, "Unlock CR index = %p\n", (void *) temp, 0, 0 );
1421 ldap_pvt_thread_rdwr_wunlock(&temp->t_rwlock);
1426 struct query_info *next;
1438 struct query_info *qi;
1441 if ( rs->sr_type != REP_SEARCH ) return 0;
1443 attr = attr_find( rs->sr_entry->e_attrs, ad_queryId );
1444 if ( attr == NULL ) return 0;
1446 count = attr->a_numvals;
1447 assert( count > 0 );
1448 qi = op->o_tmpalloc( sizeof( struct query_info ), op->o_tmpmemctx );
1449 qi->next = op->o_callback->sc_private;
1450 op->o_callback->sc_private = qi;
1451 ber_dupbv_x( &qi->xdn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1452 qi->del = ( count == 1 );
1461 struct berval *query_uuid )
1463 struct query_info *qi, *qnext;
1464 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(queryId=)" ) ];
1465 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
1466 Filter filter = {LDAP_FILTER_EQUALITY};
1467 SlapReply sreply = {REP_RESULT};
1468 slap_callback cb = { NULL, remove_func, NULL, NULL };
1471 sreply.sr_entry = NULL;
1472 sreply.sr_nentries = 0;
1473 op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
1474 "(%s=%s)", ad_queryId->ad_cname.bv_val, query_uuid->bv_val);
1475 filter.f_ava = &ava;
1476 filter.f_av_desc = ad_queryId;
1477 filter.f_av_value = *query_uuid;
1479 op->o_tag = LDAP_REQ_SEARCH;
1480 op->o_protocol = LDAP_VERSION3;
1481 op->o_callback = &cb;
1482 op->o_time = slap_get_time();
1483 op->o_do_not_cache = 1;
1485 op->o_req_dn = op->o_bd->be_suffix[0];
1486 op->o_req_ndn = op->o_bd->be_nsuffix[0];
1487 op->ors_scope = LDAP_SCOPE_SUBTREE;
1488 op->ors_deref = LDAP_DEREF_NEVER;
1489 op->ors_slimit = SLAP_NO_LIMIT;
1490 op->ors_tlimit = SLAP_NO_LIMIT;
1491 op->ors_filter = &filter;
1492 op->ors_filterstr.bv_val = filter_str;
1493 op->ors_filterstr.bv_len = strlen(filter_str);
1494 op->ors_attrs = NULL;
1495 op->ors_attrsonly = 0;
1497 op->o_bd->be_search( op, &sreply );
1499 for ( qi=cb.sc_private; qi; qi=qnext ) {
1502 op->o_req_dn = qi->xdn;
1503 op->o_req_ndn = qi->xdn;
1506 Debug( pcache_debug, "DELETING ENTRY TEMPLATE=%s\n",
1507 query_uuid->bv_val, 0, 0 );
1509 op->o_tag = LDAP_REQ_DELETE;
1511 if (op->o_bd->be_delete(op, &sreply) == LDAP_SUCCESS) {
1517 struct berval vals[2];
1519 vals[0] = *query_uuid;
1520 vals[1].bv_val = NULL;
1522 mod.sml_op = LDAP_MOD_DELETE;
1524 mod.sml_desc = ad_queryId;
1525 mod.sml_type = ad_queryId->ad_cname;
1526 mod.sml_values = vals;
1527 mod.sml_nvalues = NULL;
1528 mod.sml_numvals = 1;
1529 mod.sml_next = NULL;
1530 Debug( pcache_debug,
1531 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
1532 query_uuid->bv_val, 0, 0 );
1534 op->orm_modlist = &mod;
1536 op->o_bd->be_modify( op, &sreply );
1538 op->o_tmpfree( qi->xdn.bv_val, op->o_tmpmemctx );
1539 op->o_tmpfree( qi, op->o_tmpmemctx );
1546 AttributeName* attrs,
1555 struct berval *fstr,
1556 AttributeName** filter_attrs,
1558 int* filter_got_oc )
1560 AttributeDescription *ad;
1563 switch ( f->f_choice ) {
1564 case LDAP_FILTER_EQUALITY:
1566 len = STRLENOF( "(=)" ) + ad->ad_cname.bv_len;
1567 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=)", ad->ad_cname.bv_val );
1568 assert( ret == len );
1569 fstr->bv_len += len;
1572 case LDAP_FILTER_GE:
1574 len = STRLENOF( "(>=)" ) + ad->ad_cname.bv_len;
1575 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s>=)", ad->ad_cname.bv_val);
1576 assert( ret == len );
1577 fstr->bv_len += len;
1580 case LDAP_FILTER_LE:
1582 len = STRLENOF( "(<=)" ) + ad->ad_cname.bv_len;
1583 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s<=)", ad->ad_cname.bv_val);
1584 assert( ret == len );
1585 fstr->bv_len += len;
1588 case LDAP_FILTER_APPROX:
1590 len = STRLENOF( "(~=)" ) + ad->ad_cname.bv_len;
1591 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s~=)", ad->ad_cname.bv_val);
1592 assert( ret == len );
1593 fstr->bv_len += len;
1596 case LDAP_FILTER_SUBSTRINGS:
1598 len = STRLENOF( "(=)" ) + ad->ad_cname.bv_len;
1599 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=)", ad->ad_cname.bv_val );
1600 assert( ret == len );
1601 fstr->bv_len += len;
1604 case LDAP_FILTER_PRESENT:
1606 len = STRLENOF( "(=*)" ) + ad->ad_cname.bv_len;
1607 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=*)", ad->ad_cname.bv_val );
1608 assert( ret == len );
1609 fstr->bv_len += len;
1612 case LDAP_FILTER_AND:
1613 case LDAP_FILTER_OR:
1614 case LDAP_FILTER_NOT: {
1616 fstr->bv_val[fstr->bv_len++] = '(';
1617 switch ( f->f_choice ) {
1618 case LDAP_FILTER_AND:
1619 fstr->bv_val[fstr->bv_len] = '&';
1621 case LDAP_FILTER_OR:
1622 fstr->bv_val[fstr->bv_len] = '|';
1624 case LDAP_FILTER_NOT:
1625 fstr->bv_val[fstr->bv_len] = '!';
1630 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1631 rc = filter2template( op, f, fstr, filter_attrs, filter_cnt,
1635 fstr->bv_val[fstr->bv_len++] = ')';
1636 fstr->bv_val[fstr->bv_len] = '\0';
1642 /* a filter should at least have room for "()",
1643 * an "=" and for a 1-char attr */
1644 strcpy( fstr->bv_val, "(?=)" );
1645 fstr->bv_len += STRLENOF("(?=)");
1649 if ( filter_attrs != NULL ) {
1650 *filter_attrs = (AttributeName *)op->o_tmprealloc(*filter_attrs,
1651 (*filter_cnt + 2)*sizeof(AttributeName), op->o_tmpmemctx);
1653 (*filter_attrs)[*filter_cnt].an_desc = ad;
1654 (*filter_attrs)[*filter_cnt].an_name = ad->ad_cname;
1655 (*filter_attrs)[*filter_cnt].an_oc = NULL;
1656 (*filter_attrs)[*filter_cnt].an_flags = 0;
1657 BER_BVZERO( &(*filter_attrs)[*filter_cnt+1].an_name );
1659 if ( ad == slap_schema.si_ad_objectClass )
1666 struct search_info {
1669 QueryTemplate *qtemp;
1670 AttributeName* save_attrs; /* original attributes, saved for response */
1671 int swap_saved_attrs;
1676 int slimit_exceeded;
1677 pc_caching_reason_t caching_reason;
1682 remove_query_and_data(
1686 struct berval *uuid )
1688 query_manager* qm = cm->qm;
1690 qm->crfunc( qm, uuid );
1691 if ( !BER_BVISNULL( uuid ) ) {
1694 Debug( pcache_debug,
1695 "Removing query UUID %s\n",
1696 uuid->bv_val, 0, 0 );
1697 return_val = remove_query_data( op, rs, uuid );
1698 Debug( pcache_debug,
1699 "QUERY REMOVED, SIZE=%d\n",
1701 ldap_pvt_thread_mutex_lock( &cm->cache_mutex );
1702 cm->cur_entries -= return_val;
1703 cm->num_cached_queries--;
1704 Debug( pcache_debug,
1705 "STORED QUERIES = %lu\n",
1706 cm->num_cached_queries, 0, 0 );
1707 ldap_pvt_thread_mutex_unlock( &cm->cache_mutex );
1708 Debug( pcache_debug,
1709 "QUERY REMOVED, CACHE ="
1711 cm->cur_entries, 0, 0 );
1716 * Callback used to fetch queryId values based on entryUUID;
1717 * used by pcache_remove_entries_from_cache()
1720 fetch_queryId_cb( Operation *op, SlapReply *rs )
1724 /* only care about searchEntry responses */
1725 if ( rs->sr_type != REP_SEARCH ) {
1729 /* allow only one response per entryUUID */
1730 if ( op->o_callback->sc_private != NULL ) {
1736 /* copy all queryId values into callback's private data */
1737 a = attr_find( rs->sr_entry->e_attrs, ad_queryId );
1739 BerVarray vals = NULL;
1741 ber_bvarray_dup_x( &vals, a->a_nvals, op->o_tmpmemctx );
1742 op->o_callback->sc_private = (void *)vals;
1746 /* clear entry if required */
1747 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
1748 entry_free( rs->sr_entry );
1749 rs->sr_entry = NULL;
1750 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
1757 * Call that allows to remove a set of entries from the cache,
1758 * by forcing the removal of all the related queries.
1761 pcache_remove_entries_from_cache(
1764 BerVarray entryUUIDs )
1766 Connection conn = { 0 };
1767 OperationBuffer opbuf;
1769 slap_callback sc = { 0 };
1770 SlapReply rs = { REP_RESULT };
1772 char filtbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(entryUUID=)" ) ];
1773 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
1774 AttributeName attrs[ 2 ] = {{{ 0 }}};
1778 void *thrctx = ldap_pvt_thread_pool_context();
1780 connection_fake_init( &conn, &opbuf, thrctx );
1788 memset( &op->oq_search, 0, sizeof( op->oq_search ) );
1789 op->ors_scope = LDAP_SCOPE_SUBTREE;
1790 op->ors_deref = LDAP_DEREF_NEVER;
1791 f.f_choice = LDAP_FILTER_EQUALITY;
1793 ava.aa_desc = slap_schema.si_ad_entryUUID;
1794 op->ors_filter = &f;
1796 op->ors_tlimit = SLAP_NO_LIMIT;
1797 attrs[ 0 ].an_desc = ad_queryId;
1798 attrs[ 0 ].an_name = ad_queryId->ad_cname;
1799 op->ors_attrs = attrs;
1800 op->ors_attrsonly = 0;
1802 op->o_req_dn = cm->db.be_suffix[ 0 ];
1803 op->o_req_ndn = cm->db.be_nsuffix[ 0 ];
1805 op->o_tag = LDAP_REQ_SEARCH;
1806 op->o_protocol = LDAP_VERSION3;
1807 op->o_managedsait = SLAP_CONTROL_CRITICAL;
1809 op->o_dn = op->o_bd->be_rootdn;
1810 op->o_ndn = op->o_bd->be_rootndn;
1811 sc.sc_response = fetch_queryId_cb;
1812 op->o_callback = ≻
1814 for ( s = 0; !BER_BVISNULL( &entryUUIDs[ s ] ); s++ ) {
1815 BerVarray vals = NULL;
1817 op->ors_filterstr.bv_len = snprintf( filtbuf, sizeof( filtbuf ),
1818 "(entryUUID=%s)", entryUUIDs[ s ].bv_val );
1819 op->ors_filterstr.bv_val = filtbuf;
1820 ava.aa_value = entryUUIDs[ s ];
1822 rc = op->o_bd->be_search( op, &rs );
1823 if ( rc != LDAP_SUCCESS ) {
1827 vals = (BerVarray)op->o_callback->sc_private;
1828 if ( vals != NULL ) {
1831 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1832 struct berval val = vals[ i ];
1834 remove_query_and_data( op, &rs, cm, &val );
1836 if ( !BER_BVISNULL( &val ) && val.bv_val != vals[ i ].bv_val ) {
1837 ch_free( val.bv_val );
1841 ber_bvarray_free_x( vals, op->o_tmpmemctx );
1842 op->o_callback->sc_private = NULL;
1850 * Call that allows to remove a query from the cache.
1853 pcache_remove_query_from_cache(
1856 struct berval *queryid )
1858 Operation op2 = *op;
1859 SlapReply rs2 = { 0 };
1863 /* remove the selected query */
1864 remove_query_and_data( &op2, &rs2, cm, queryid );
1866 return LDAP_SUCCESS;
1870 * Call that allows to remove a set of queries related to an entry
1871 * from the cache; if queryid is not null, the entry must belong to
1872 * the query indicated by queryid.
1875 pcache_remove_entry_queries_from_cache(
1879 struct berval *queryid )
1881 Connection conn = { 0 };
1882 OperationBuffer opbuf;
1884 slap_callback sc = { 0 };
1885 SlapReply rs = { REP_RESULT };
1887 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(queryId=)" ) ];
1888 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
1889 AttributeName attrs[ 2 ] = {{{ 0 }}};
1892 BerVarray vals = NULL;
1895 void *thrctx = ldap_pvt_thread_pool_context();
1897 connection_fake_init( &conn, &opbuf, thrctx );
1905 memset( &op->oq_search, 0, sizeof( op->oq_search ) );
1906 op->ors_scope = LDAP_SCOPE_BASE;
1907 op->ors_deref = LDAP_DEREF_NEVER;
1908 if ( queryid == NULL || BER_BVISNULL( queryid ) ) {
1909 BER_BVSTR( &op->ors_filterstr, "(objectClass=*)" );
1910 f.f_choice = LDAP_FILTER_PRESENT;
1911 f.f_desc = slap_schema.si_ad_objectClass;
1914 op->ors_filterstr.bv_len = snprintf( filter_str,
1915 sizeof( filter_str ), "(%s=%s)",
1916 ad_queryId->ad_cname.bv_val, queryid->bv_val );
1917 f.f_choice = LDAP_FILTER_EQUALITY;
1919 f.f_av_desc = ad_queryId;
1920 f.f_av_value = *queryid;
1922 op->ors_filter = &f;
1924 op->ors_tlimit = SLAP_NO_LIMIT;
1925 attrs[ 0 ].an_desc = ad_queryId;
1926 attrs[ 0 ].an_name = ad_queryId->ad_cname;
1927 op->ors_attrs = attrs;
1928 op->ors_attrsonly = 0;
1930 op->o_req_dn = *ndn;
1931 op->o_req_ndn = *ndn;
1933 op->o_tag = LDAP_REQ_SEARCH;
1934 op->o_protocol = LDAP_VERSION3;
1935 op->o_managedsait = SLAP_CONTROL_CRITICAL;
1937 op->o_dn = op->o_bd->be_rootdn;
1938 op->o_ndn = op->o_bd->be_rootndn;
1939 sc.sc_response = fetch_queryId_cb;
1940 op->o_callback = ≻
1942 rc = op->o_bd->be_search( op, &rs );
1943 if ( rc != LDAP_SUCCESS ) {
1947 vals = (BerVarray)op->o_callback->sc_private;
1948 if ( vals != NULL ) {
1951 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1952 struct berval val = vals[ i ];
1954 remove_query_and_data( op, &rs, cm, &val );
1956 if ( !BER_BVISNULL( &val ) && val.bv_val != vals[ i ].bv_val ) {
1957 ch_free( val.bv_val );
1961 ber_bvarray_free_x( vals, op->o_tmpmemctx );
1964 return LDAP_SUCCESS;
1971 struct berval *query_uuid )
1973 struct search_info *si = op->o_callback->sc_private;
1974 slap_overinst *on = si->on;
1975 cache_manager *cm = on->on_bi.bi_private;
1978 struct berval crp_uuid;
1979 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1981 Connection conn = {0};
1982 OperationBuffer opbuf;
1983 void *thrctx = ldap_pvt_thread_pool_context();
1985 query_uuid->bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf));
1986 ber_str2bv(uuidbuf, query_uuid->bv_len, 1, query_uuid);
1988 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
1989 op_tmp = &opbuf.ob_op;
1990 op_tmp->o_bd = &cm->db;
1991 op_tmp->o_dn = cm->db.be_rootdn;
1992 op_tmp->o_ndn = cm->db.be_rootndn;
1994 Debug( pcache_debug, "UUID for query being added = %s\n",
1997 for ( e=si->head; e; e=si->head ) {
1998 si->head = e->e_private;
1999 e->e_private = NULL;
2000 while ( cm->cur_entries > (cm->max_entries) ) {
2001 BER_BVZERO( &crp_uuid );
2002 remove_query_and_data( op_tmp, rs, cm, &crp_uuid );
2005 return_val = merge_entry(op_tmp, e, query_uuid);
2006 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2007 cm->cur_entries += return_val;
2008 Debug( pcache_debug,
2009 "ENTRY ADDED/MERGED, CACHED ENTRIES=%d\n",
2010 cm->cur_entries, 0, 0 );
2012 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2019 pcache_op_cleanup( Operation *op, SlapReply *rs ) {
2020 slap_callback *cb = op->o_callback;
2021 struct search_info *si = cb->sc_private;
2022 slap_overinst *on = si->on;
2023 cache_manager *cm = on->on_bi.bi_private;
2024 query_manager* qm = cm->qm;
2026 if ( rs->sr_type == REP_SEARCH ) {
2029 /* don't return more entries than requested by the client */
2030 if ( si->slimit > 0 && rs->sr_nentries >= si->slimit ) {
2031 si->slimit_exceeded = 1;
2034 /* If we haven't exceeded the limit for this query,
2035 * build a chain of answers to store. If we hit the
2036 * limit, empty the chain and ignore the rest.
2039 /* check if the entry contains undefined
2040 * attributes/objectClasses (ITS#5680) */
2041 if ( cm->check_cacheability && test_filter( op, rs->sr_entry, si->query.filter ) != LDAP_COMPARE_TRUE ) {
2042 Debug( pcache_debug, "%s: query not cacheable because of schema issues in DN \"%s\"\n",
2043 op->o_log_prefix, rs->sr_entry->e_name.bv_val, 0 );
2047 /* check for malformed entries: attrs with no values */
2049 Attribute *a = rs->sr_entry->e_attrs;
2050 for (; a; a=a->a_next) {
2051 if ( !a->a_numvals ) {
2052 Debug( pcache_debug, "%s: query not cacheable because of attrs without values in DN \"%s\" (%s)\n",
2053 op->o_log_prefix, rs->sr_entry->e_name.bv_val,
2054 a->a_desc->ad_cname.bv_val );
2060 if ( si->count < si->max ) {
2062 e = entry_dup( rs->sr_entry );
2063 if ( !si->head ) si->head = e;
2064 if ( si->tail ) si->tail->e_private = e;
2071 for (;si->head; si->head=e) {
2072 e = si->head->e_private;
2073 si->head->e_private = NULL;
2074 entry_free(si->head);
2082 if ( rs->sr_type == REP_RESULT ||
2083 op->o_abandon || rs->sr_err == SLAPD_ABANDON )
2085 if ( si->swap_saved_attrs ) {
2086 rs->sr_attrs = si->save_attrs;
2087 op->ors_attrs = si->save_attrs;
2089 if ( (op->o_abandon || rs->sr_err == SLAPD_ABANDON) &&
2090 si->caching_reason == PC_IGNORE )
2092 filter_free( si->query.filter );
2094 /* duplicate query, free it */
2096 for (;si->head; si->head=e) {
2097 e = si->head->e_private;
2098 si->head->e_private = NULL;
2099 entry_free(si->head);
2102 op->o_callback = op->o_callback->sc_next;
2103 op->o_tmpfree( cb, op->o_tmpmemctx );
2104 } else if ( si->caching_reason != PC_IGNORE ) {
2105 CachedQuery *qc = qm->addfunc(op, qm, &si->query,
2106 si->qtemp, si->caching_reason, 1 );
2109 switch ( si->caching_reason ) {
2111 cache_entries( op, rs, &qc->q_uuid );
2115 qc->q_sizelimit = rs->sr_nentries;
2125 ldap_pvt_thread_rdwr_wunlock(&qc->rwlock);
2126 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2127 cm->num_cached_queries++;
2128 Debug( pcache_debug, "STORED QUERIES = %lu\n",
2129 cm->num_cached_queries, 0, 0 );
2130 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2132 /* If the consistency checker suspended itself,
2135 if ( cm->cc_paused == PCACHE_CC_PAUSED ) {
2136 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2137 if ( cm->cc_paused == PCACHE_CC_PAUSED ) {
2139 ldap_pvt_runqueue_resched( &slapd_rq, cm->cc_arg, 0 );
2141 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2144 } else if ( si->count ) {
2145 /* duplicate query, free it */
2147 for (;si->head; si->head=e) {
2148 e = si->head->e_private;
2149 si->head->e_private = NULL;
2150 entry_free(si->head);
2155 filter_free( si->query.filter );
2159 return SLAP_CB_CONTINUE;
2167 struct search_info *si = op->o_callback->sc_private;
2169 if ( si->swap_saved_attrs ) {
2170 rs->sr_attrs = si->save_attrs;
2171 op->ors_attrs = si->save_attrs;
2174 if ( rs->sr_type == REP_SEARCH ) {
2175 /* don't return more entries than requested by the client */
2176 if ( si->slimit_exceeded ) {
2180 } else if ( rs->sr_type == REP_RESULT ) {
2183 if ( rs->sr_err == LDAP_SUCCESS ) {
2184 si->caching_reason = PC_POSITIVE;
2186 } else if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED
2187 && si->qtemp->limitttl )
2189 si->caching_reason = PC_SIZELIMIT;
2192 } else if ( si->qtemp->negttl && !si->count && !si->over &&
2193 rs->sr_err == LDAP_SUCCESS )
2195 si->caching_reason = PC_NEGATIVE;
2199 if ( si->slimit_exceeded ) {
2200 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
2204 return SLAP_CB_CONTINUE;
2210 AttributeName** new_attrs,
2211 struct attr_set *attrs,
2212 AttributeName* filter_attrs,
2222 /* duplicate attrs */
2223 count = attrs->count + fattr_cnt;
2224 if ( !fattr_got_oc && !(attrs->flags & PC_GOT_OC)) {
2229 *new_attrs = (AttributeName*)ch_calloc( count + 1,
2230 sizeof(AttributeName) );
2231 for (i=0; i<attrs->count; i++) {
2232 (*new_attrs)[i].an_name = attrs->attrs[i].an_name;
2233 (*new_attrs)[i].an_desc = attrs->attrs[i].an_desc;
2235 BER_BVZERO( &(*new_attrs)[i].an_name );
2236 alluser = an_find( *new_attrs, slap_bv_all_user_attrs );
2237 allop = an_find( *new_attrs, slap_bv_all_operational_attrs );
2240 for ( i=0; i<fattr_cnt; i++ ) {
2241 if ( an_find(*new_attrs, &filter_attrs[i].an_name ) ) {
2244 if ( is_at_operational(filter_attrs[i].an_desc->ad_type) ) {
2248 } else if ( alluser ) {
2251 (*new_attrs)[j].an_name = filter_attrs[i].an_name;
2252 (*new_attrs)[j].an_desc = filter_attrs[i].an_desc;
2253 (*new_attrs)[j].an_oc = NULL;
2254 (*new_attrs)[j].an_flags = 0;
2258 (*new_attrs)[j].an_name = slap_schema.si_ad_objectClass->ad_cname;
2259 (*new_attrs)[j].an_desc = slap_schema.si_ad_objectClass;
2260 (*new_attrs)[j].an_oc = NULL;
2261 (*new_attrs)[j].an_flags = 0;
2264 BER_BVZERO( &(*new_attrs)[j].an_name );
2269 /* NOTE: this is a quick workaround to let pcache minimally interact
2270 * with pagedResults. A more articulated solutions would be to
2271 * perform the remote query without control and cache all results,
2272 * performing the pagedResults search only within the client
2273 * and the proxy. This requires pcache to understand pagedResults. */
2275 pcache_chk_controls(
2279 const char *non = "";
2280 const char *stripped = "";
2282 switch( op->o_pagedresults ) {
2283 case SLAP_CONTROL_NONCRITICAL:
2285 stripped = "; stripped";
2288 case SLAP_CONTROL_CRITICAL:
2289 Debug( pcache_debug, "%s: "
2290 "%scritical pagedResults control "
2291 "disabled with proxy cache%s.\n",
2292 op->o_log_prefix, non, stripped );
2294 slap_remove_control( op, rs, slap_cids.sc_pagedResults, NULL );
2298 rs->sr_err = SLAP_CB_CONTINUE;
2305 #ifdef PCACHE_CONTROL_PRIVDB
2311 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2312 cache_manager *cm = on->on_bi.bi_private;
2313 slap_callback *save_cb;
2316 /* skip if control is unset */
2317 if ( op->o_ctrlflag[ privDB_cid ] != SLAP_CONTROL_CRITICAL ) {
2318 return SLAP_CB_CONTINUE;
2321 /* The cache DB isn't open yet */
2322 if ( cm->defer_db_open ) {
2323 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
2324 "pcachePrivDB: cacheDB not available" );
2328 /* FIXME: might be a little bit exaggerated... */
2329 if ( !be_isroot( op ) ) {
2330 save_cb = op->o_callback;
2331 op->o_callback = NULL;
2332 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
2333 "pcachePrivDB: operation not allowed" );
2334 op->o_callback = save_cb;
2339 /* map tag to operation */
2340 type = slap_req2op( op->o_tag );
2341 if ( type != SLAP_OP_LAST ) {
2345 /* execute, if possible */
2346 func = &cm->db.be_bind;
2347 if ( func[ type ] != NULL ) {
2348 Operation op2 = *op;
2352 rc = func[ type ]( &op2, rs );
2353 if ( type == SLAP_OP_BIND && rc == LDAP_SUCCESS ) {
2354 op->o_conn->c_authz_cookie = cm->db.be_private;
2359 /* otherwise fall back to error */
2360 save_cb = op->o_callback;
2361 op->o_callback = NULL;
2362 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
2363 "operation not supported with pcachePrivDB control" );
2364 op->o_callback = save_cb;
2368 #endif /* PCACHE_CONTROL_PRIVDB */
2375 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2376 cache_manager *cm = on->on_bi.bi_private;
2377 query_manager* qm = cm->qm;
2381 AttributeName *filter_attrs = NULL;
2384 QueryTemplate *qtemp = NULL;
2387 CachedQuery *answerable = NULL;
2390 int fattr_got_oc = 0;
2392 struct berval tempstr;
2394 #ifdef PCACHE_CONTROL_PRIVDB
2395 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL ) {
2396 return pcache_op_privdb( op, rs );
2398 #endif /* PCACHE_CONTROL_PRIVDB */
2400 /* The cache DB isn't open yet */
2401 if ( cm->defer_db_open ) {
2402 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
2403 "pcachePrivDB: cacheDB not available" );
2407 /* pickup runtime ACL changes */
2408 cm->db.be_acl = op->o_bd->be_acl;
2410 tempstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len+1, op->o_tmpmemctx );
2412 if ( filter2template( op, op->ors_filter, &tempstr, &filter_attrs,
2413 &fattr_cnt, &fattr_got_oc ))
2415 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
2416 return SLAP_CB_CONTINUE;
2419 Debug( pcache_debug, "query template of incoming query = %s\n",
2420 tempstr.bv_val, 0, 0 );
2422 /* FIXME: cannot cache/answer requests with pagedResults control */
2425 attr_set = get_attr_set(op->ors_attrs, qm, cm->numattrsets);
2427 query.filter = op->ors_filter;
2428 query.base = op->o_req_ndn;
2429 query.scope = op->ors_scope;
2431 /* check for query containment */
2432 if (attr_set > -1) {
2433 QueryTemplate *qt = qm->attr_sets[attr_set].templates;
2434 for (; qt; qt = qt->qtnext ) {
2435 /* find if template i can potentially answer tempstr */
2436 if ( ber_bvstrcasecmp( &qt->querystr, &tempstr ) != 0 )
2440 Debug( pcache_debug, "Entering QC, querystr = %s\n",
2441 op->ors_filterstr.bv_val, 0, 0 );
2442 answerable = qm->qcfunc(op, qm, &query, qt);
2444 /* if != NULL, rlocks qtemp->t_rwlock */
2449 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
2452 BackendDB *save_bd = op->o_bd;
2453 slap_callback *save_cb = op->o_callback;
2455 ldap_pvt_thread_mutex_lock( &answerable->answerable_cnt_mutex );
2456 answerable->answerable_cnt++;
2457 Debug( pcache_debug, "QUERY ANSWERABLE (answered %lu times)\n",
2458 answerable->answerable_cnt, 0, 0 );
2459 ldap_pvt_thread_mutex_unlock( &answerable->answerable_cnt_mutex );
2461 op->o_tmpfree( filter_attrs, op->o_tmpmemctx );
2462 ldap_pvt_thread_rdwr_rlock(&answerable->rwlock);
2463 if ( BER_BVISNULL( &answerable->q_uuid )) {
2464 /* No entries cached, just an empty result set */
2466 send_ldap_result( op, rs );
2469 if ( cm->response_cb == PCACHE_RESPONSE_CB_TAIL ) {
2470 /* The cached entry was already processed by any
2471 * other overlays, so don't let it get processed again.
2473 op->o_callback = NULL;
2475 i = cm->db.bd_info->bi_op_search( op, rs );
2477 ldap_pvt_thread_rdwr_runlock(&answerable->rwlock);
2478 /* locked by qtemp->qcfunc (query_containment) */
2479 ldap_pvt_thread_rdwr_runlock(&qtemp->t_rwlock);
2481 op->o_callback = save_cb;
2485 Debug( pcache_debug, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
2487 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2488 if (cm->num_cached_queries >= cm->max_queries) {
2491 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2493 if (op->ors_attrsonly)
2498 struct search_info *si;
2500 Debug( pcache_debug, "QUERY CACHEABLE\n", 0, 0, 0 );
2501 query.filter = filter_dup(op->ors_filter, NULL);
2502 ldap_pvt_thread_rdwr_wlock(&qtemp->t_rwlock);
2503 if ( !qtemp->t_attrs.count ) {
2504 qtemp->t_attrs.count = add_filter_attrs(op,
2505 &qtemp->t_attrs.attrs,
2506 &qm->attr_sets[attr_set],
2507 filter_attrs, fattr_cnt, fattr_got_oc);
2509 ldap_pvt_thread_rdwr_wunlock(&qtemp->t_rwlock);
2511 cb = op->o_tmpalloc( sizeof(*cb) + sizeof(*si), op->o_tmpmemctx );
2512 cb->sc_response = pcache_response;
2513 cb->sc_cleanup = pcache_op_cleanup;
2514 cb->sc_private = (cb+1);
2515 si = cb->sc_private;
2519 si->max = cm->num_entries_limit ;
2523 si->slimit_exceeded = 0;
2524 si->caching_reason = PC_IGNORE;
2525 if ( op->ors_slimit > 0 && op->ors_slimit < cm->num_entries_limit ) {
2526 si->slimit = op->ors_slimit;
2527 op->ors_slimit = cm->num_entries_limit;
2531 si->swap_saved_attrs = 1;
2532 si->save_attrs = op->ors_attrs;
2534 op->ors_attrs = qtemp->t_attrs.attrs;
2536 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
2537 cb->sc_next = op->o_callback;
2538 op->o_callback = cb;
2541 slap_callback **pcb;
2543 /* need to move the callback at the end, in case other
2544 * overlays are present, so that the final entry is
2545 * actually cached */
2547 for ( pcb = &op->o_callback; *pcb; pcb = &(*pcb)->sc_next );
2552 Debug( pcache_debug, "QUERY NOT CACHEABLE\n",
2556 op->o_tmpfree( filter_attrs, op->o_tmpmemctx );
2558 return SLAP_CB_CONTINUE;
2563 AttributeName* attrs,
2571 for ( ; attrs[count].an_name.bv_val; count++ );
2574 /* recognize a single "*" or a "1.1" */
2577 attrs = slap_anlist_all_user_attributes;
2579 } else if ( count == 1 && bvmatch( &attrs[0].an_name, slap_bv_no_attrs ) ) {
2584 for ( i = 0; i < num; i++ ) {
2588 if ( count > qm->attr_sets[i].count ) {
2593 if ( !qm->attr_sets[i].count ) {
2599 for ( a2 = attrs; a2->an_name.bv_val; a2++ ) {
2600 if ( !an_find( qm->attr_sets[i].attrs, &a2->an_name ) ) {
2623 struct re_s *rtask = arg;
2624 slap_overinst *on = rtask->arg;
2625 cache_manager *cm = on->on_bi.bi_private;
2626 query_manager *qm = cm->qm;
2627 Connection conn = {0};
2628 OperationBuffer opbuf;
2631 SlapReply rs = {REP_RESULT};
2633 int return_val, pause = PCACHE_CC_PAUSED;
2634 QueryTemplate* templ;
2636 /* Don't expire anything when we're offline */
2637 if ( cm->cc_paused & PCACHE_CC_OFFLINE ) {
2638 pause = PCACHE_CC_OFFLINE;
2642 connection_fake_init( &conn, &opbuf, ctx );
2646 op->o_dn = cm->db.be_rootdn;
2647 op->o_ndn = cm->db.be_rootndn;
2651 for (templ = qm->templates; templ; templ=templ->qmnext) {
2652 query = templ->query_last;
2653 if ( query ) pause = 0;
2654 op->o_time = slap_get_time();
2655 while (query && (query->expiry_time < op->o_time)) {
2657 Debug( pcache_debug, "Lock CR index = %p\n",
2658 (void *) templ, 0, 0 );
2659 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
2660 if ( query == templ->query_last ) {
2662 remove_from_template(query, templ);
2663 Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
2664 (void *) templ, templ->no_of_queries, 0 );
2665 Debug( pcache_debug, "Unlock CR index = %p\n",
2666 (void *) templ, 0, 0 );
2668 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
2670 query = templ->query_last;
2673 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
2674 remove_query(qm, query);
2675 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
2676 if ( BER_BVISNULL( &query->q_uuid ))
2679 return_val = remove_query_data(op, &rs, &query->q_uuid);
2680 Debug( pcache_debug, "STALE QUERY REMOVED, SIZE=%d\n",
2682 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2683 cm->cur_entries -= return_val;
2684 cm->num_cached_queries--;
2685 Debug( pcache_debug, "STORED QUERIES = %lu\n",
2686 cm->num_cached_queries, 0, 0 );
2687 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2688 Debug( pcache_debug,
2689 "STALE QUERY REMOVED, CACHE ="
2691 cm->cur_entries, 0, 0 );
2693 query = templ->query_last;
2698 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2699 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
2700 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
2702 /* If there were no queries, defer processing for a while */
2703 if ( cm->cc_paused != pause )
2704 cm->cc_paused = pause;
2705 ldap_pvt_runqueue_resched( &slapd_rq, rtask, pause );
2707 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2712 #define MAX_ATTR_SETS 500
2724 static ConfigDriver pc_cf_gen;
2725 static ConfigLDAPadd pc_ldadd;
2726 static ConfigCfAdd pc_cfadd;
2728 static ConfigTable pccfg[] = {
2729 { "pcache", "backend> <max_entries> <numattrsets> <entry limit> "
2731 6, 6, 0, ARG_MAGIC|ARG_NO_DELETE|PC_MAIN, pc_cf_gen,
2732 "( OLcfgOvAt:2.1 NAME ( 'olcPcache' 'olcProxyCache' ) "
2733 "DESC 'Proxy Cache basic parameters' "
2734 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
2735 { "pcacheAttrset", "index> <attributes...",
2736 2, 0, 0, ARG_MAGIC|PC_ATTR, pc_cf_gen,
2737 "( OLcfgOvAt:2.2 NAME ( 'olcPcacheAttrset' 'olcProxyAttrset' ) "
2738 "DESC 'A set of attributes to cache' "
2739 "SYNTAX OMsDirectoryString )", NULL, NULL },
2740 { "pcacheTemplate", "filter> <attrset-index> <TTL> <negTTL> "
2742 4, 7, 0, ARG_MAGIC|PC_TEMP, pc_cf_gen,
2743 "( OLcfgOvAt:2.3 NAME ( 'olcPcacheTemplate' 'olcProxyCacheTemplate' ) "
2744 "DESC 'Filter template, attrset, cache TTL, "
2745 "optional negative TTL, optional sizelimit TTL, "
2747 "SYNTAX OMsDirectoryString )", NULL, NULL },
2748 { "pcachePosition", "head|tail(default)",
2749 2, 2, 0, ARG_MAGIC|PC_RESP, pc_cf_gen,
2750 "( OLcfgOvAt:2.4 NAME 'olcPcachePosition' "
2751 "DESC 'Response callback position in overlay stack' "
2752 "SYNTAX OMsDirectoryString )", NULL, NULL },
2753 { "pcacheMaxQueries", "queries",
2754 2, 2, 0, ARG_INT|ARG_MAGIC|PC_QUERIES, pc_cf_gen,
2755 "( OLcfgOvAt:2.5 NAME ( 'olcPcacheMaxQueries' 'olcProxyCacheQueries' ) "
2756 "DESC 'Maximum number of queries to cache' "
2757 "SYNTAX OMsInteger )", NULL, NULL },
2758 { "pcachePersist", "TRUE|FALSE",
2759 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, save_queries),
2760 "( OLcfgOvAt:2.6 NAME ( 'olcPcachePersist' 'olcProxySaveQueries' ) "
2761 "DESC 'Save cached queries for hot restart' "
2762 "SYNTAX OMsBoolean )", NULL, NULL },
2763 { "pcacheValidate", "TRUE|FALSE",
2764 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, check_cacheability),
2765 "( OLcfgOvAt:2.7 NAME ( 'olcPcacheValidate' 'olcProxyCheckCacheability' ) "
2766 "DESC 'Check whether the results of a query are cacheable, e.g. for schema issues' "
2767 "SYNTAX OMsBoolean )", NULL, NULL },
2768 { "pcacheOffline", "TRUE|FALSE",
2769 2, 2, 0, ARG_ON_OFF|ARG_MAGIC|PC_OFFLINE, pc_cf_gen,
2770 "( OLcfgOvAt:2.8 NAME 'olcPcacheOffline' "
2771 "DESC 'Set cache to offline mode and disable expiration' "
2772 "SYNTAX OMsBoolean )", NULL, NULL },
2773 { "pcache-", "private database args",
2774 1, 0, STRLENOF("pcache-"), ARG_MAGIC|PC_PRIVATE_DB, pc_cf_gen,
2777 /* Legacy keywords */
2778 { "proxycache", "backend> <max_entries> <numattrsets> <entry limit> "
2780 6, 6, 0, ARG_MAGIC|ARG_NO_DELETE|PC_MAIN, pc_cf_gen,
2782 { "proxyattrset", "index> <attributes...",
2783 2, 0, 0, ARG_MAGIC|PC_ATTR, pc_cf_gen,
2785 { "proxytemplate", "filter> <attrset-index> <TTL> <negTTL",
2786 4, 6, 0, ARG_MAGIC|PC_TEMP, pc_cf_gen,
2788 { "response-callback", "head|tail(default)",
2789 2, 2, 0, ARG_MAGIC|PC_RESP, pc_cf_gen,
2791 { "proxyCacheQueries", "queries",
2792 2, 2, 0, ARG_INT|ARG_MAGIC|PC_QUERIES, pc_cf_gen,
2794 { "proxySaveQueries", "TRUE|FALSE",
2795 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, save_queries),
2797 { "proxyCheckCacheability", "TRUE|FALSE",
2798 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, check_cacheability),
2801 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
2804 static ConfigOCs pcocs[] = {
2805 { "( OLcfgOvOc:2.1 "
2806 "NAME 'olcPcacheConfig' "
2807 "DESC 'ProxyCache configuration' "
2808 "SUP olcOverlayConfig "
2809 "MUST ( olcPcache $ olcPcacheAttrset $ olcPcacheTemplate ) "
2810 "MAY ( olcPcachePosition $ olcPcacheMaxQueries $ olcPcachePersist $ "
2811 "olcPcacheValidate $ olcPcacheOffline ) )",
2812 Cft_Overlay, pccfg, NULL, pc_cfadd },
2813 { "( OLcfgOvOc:2.2 "
2814 "NAME 'olcPcacheDatabase' "
2815 "DESC 'Cache database configuration' "
2816 "AUXILIARY )", Cft_Misc, olcDatabaseDummy, pc_ldadd },
2820 static int pcache_db_open2( slap_overinst *on, ConfigReply *cr );
2823 pc_ldadd_cleanup( ConfigArgs *c )
2825 slap_overinst *on = c->ca_private;
2826 return pcache_db_open2( on, &c->reply );
2830 pc_ldadd( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
2835 if ( p->ce_type != Cft_Overlay || !p->ce_bi ||
2836 p->ce_bi->bi_cf_ocs != pcocs )
2837 return LDAP_CONSTRAINT_VIOLATION;
2839 on = (slap_overinst *)p->ce_bi;
2840 cm = on->on_bi.bi_private;
2842 /* Defer open if this is an LDAPadd */
2843 if ( CONFIG_ONLINE_ADD( ca ))
2844 ca->cleanup = pc_ldadd_cleanup;
2846 cm->defer_db_open = 0;
2847 ca->ca_private = on;
2848 return LDAP_SUCCESS;
2852 pc_cfadd( Operation *op, SlapReply *rs, Entry *p, ConfigArgs *ca )
2854 CfEntryInfo *pe = p->e_private;
2855 slap_overinst *on = (slap_overinst *)pe->ce_bi;
2856 cache_manager *cm = on->on_bi.bi_private;
2859 /* FIXME: should not hardcode "olcDatabase" here */
2860 bv.bv_len = snprintf( ca->cr_msg, sizeof( ca->cr_msg ),
2861 "olcDatabase=" SLAP_X_ORDERED_FMT "%s",
2862 0, cm->db.bd_info->bi_type );
2863 if ( bv.bv_len >= sizeof( ca->cr_msg ) ) {
2866 bv.bv_val = ca->cr_msg;
2868 cm->defer_db_open = 0;
2870 /* We can only create this entry if the database is table-driven
2872 if ( cm->db.bd_info->bi_cf_ocs )
2873 config_build_entry( op, rs, pe, ca, &bv, cm->db.bd_info->bi_cf_ocs,
2880 pc_cf_gen( ConfigArgs *c )
2882 slap_overinst *on = (slap_overinst *)c->bi;
2883 cache_manager* cm = on->on_bi.bi_private;
2884 query_manager* qm = cm->qm;
2885 QueryTemplate* temp;
2886 AttributeName* attr_name;
2887 AttributeName* attrarray;
2888 const char* text=NULL;
2893 if ( c->op == SLAP_CONFIG_EMIT ) {
2897 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s %d %d %d %ld",
2898 cm->db.bd_info->bi_type, cm->max_entries, cm->numattrsets,
2899 cm->num_entries_limit, cm->cc_period );
2900 bv.bv_val = c->cr_msg;
2901 value_add_one( &c->rvalue_vals, &bv );
2904 for (i=0; i<cm->numattrsets; i++) {
2905 if ( !qm->attr_sets[i].count ) continue;
2907 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ), "%d", i );
2909 /* count the attr length */
2910 for ( attr_name = qm->attr_sets[i].attrs;
2911 attr_name->an_name.bv_val; attr_name++ )
2912 bv.bv_len += attr_name->an_name.bv_len + 1;
2914 bv.bv_val = ch_malloc( bv.bv_len+1 );
2915 ptr = lutil_strcopy( bv.bv_val, c->cr_msg );
2916 for ( attr_name = qm->attr_sets[i].attrs;
2917 attr_name->an_name.bv_val; attr_name++ ) {
2919 ptr = lutil_strcopy( ptr, attr_name->an_name.bv_val );
2921 ber_bvarray_add( &c->rvalue_vals, &bv );
2923 if ( !c->rvalue_vals )
2927 for (temp=qm->templates; temp; temp=temp->qmnext) {
2928 /* HEADS-UP: always print all;
2929 * if optional == 0, ignore */
2930 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ),
2931 " %d %ld %ld %ld %ld",
2932 temp->attr_set_index,
2937 bv.bv_len += temp->querystr.bv_len + 2;
2938 bv.bv_val = ch_malloc( bv.bv_len+1 );
2941 ptr = lutil_strcopy( ptr, temp->querystr.bv_val );
2943 strcpy( ptr, c->cr_msg );
2944 ber_bvarray_add( &c->rvalue_vals, &bv );
2946 if ( !c->rvalue_vals )
2950 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
2951 BER_BVSTR( &bv, "head" );
2953 BER_BVSTR( &bv, "tail" );
2955 value_add_one( &c->rvalue_vals, &bv );
2958 c->value_int = cm->max_queries;
2961 c->value_int = (cm->cc_paused & PCACHE_CC_OFFLINE) != 0;
2965 } else if ( c->op == LDAP_MOD_DELETE ) {
2968 case PC_ATTR: /* FIXME */
2972 cm->cc_paused &= ~PCACHE_CC_OFFLINE;
2981 if ( cm->numattrsets > 0 ) {
2982 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"proxycache\" directive already provided" );
2983 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2987 if ( lutil_atoi( &cm->numattrsets, c->argv[3] ) != 0 ) {
2988 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse num attrsets=\"%s\" (arg #3)",
2990 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2993 if ( cm->numattrsets <= 0 ) {
2994 snprintf( c->cr_msg, sizeof( c->cr_msg ), "numattrsets (arg #3) must be positive" );
2995 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
2998 if ( cm->numattrsets > MAX_ATTR_SETS ) {
2999 snprintf( c->cr_msg, sizeof( c->cr_msg ), "numattrsets (arg #3) must be <= %d", MAX_ATTR_SETS );
3000 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3004 if ( !backend_db_init( c->argv[1], &cm->db, -1, NULL )) {
3005 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unknown backend type (arg #1)" );
3006 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3010 if ( lutil_atoi( &cm->max_entries, c->argv[2] ) != 0 ) {
3011 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse max entries=\"%s\" (arg #2)",
3013 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3016 if ( cm->max_entries <= 0 ) {
3017 snprintf( c->cr_msg, sizeof( c->cr_msg ), "max entries (arg #2) must be positive.\n" );
3018 Debug( LDAP_DEBUG_CONFIG, "%s: %s\n", c->log, c->cr_msg, 0 );
3022 if ( lutil_atoi( &cm->num_entries_limit, c->argv[4] ) != 0 ) {
3023 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse entry limit=\"%s\" (arg #4)",
3025 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3028 if ( cm->num_entries_limit <= 0 ) {
3029 snprintf( c->cr_msg, sizeof( c->cr_msg ), "entry limit (arg #4) must be positive" );
3030 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3033 if ( cm->num_entries_limit > cm->max_entries ) {
3034 snprintf( c->cr_msg, sizeof( c->cr_msg ), "entry limit (arg #4) must be less than max entries %d (arg #2)", cm->max_entries );
3035 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3039 if ( lutil_parse_time( c->argv[5], &t ) != 0 ) {
3040 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse period=\"%s\" (arg #5)",
3042 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3046 cm->cc_period = (time_t)t;
3047 Debug( pcache_debug,
3048 "Total # of attribute sets to be cached = %d.\n",
3049 cm->numattrsets, 0, 0 );
3050 qm->attr_sets = ( struct attr_set * )ch_calloc( cm->numattrsets,
3051 sizeof( struct attr_set ) );
3054 if ( cm->numattrsets == 0 ) {
3055 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"proxycache\" directive not provided yet" );
3056 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3059 if ( lutil_atoi( &num, c->argv[1] ) != 0 ) {
3060 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse attrset #=\"%s\"",
3062 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3066 if ( num < 0 || num >= cm->numattrsets ) {
3067 snprintf( c->cr_msg, sizeof( c->cr_msg ), "attrset index %d out of bounds (must be %s%d)",
3068 num, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
3069 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3072 qm->attr_sets[num].flags |= PC_CONFIGURED;
3073 if ( c->argc == 2 ) {
3075 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3076 "need an explicit attr in attrlist; use \"*\" to indicate all attrs" );
3077 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3080 } else if ( c->argc == 3 ) {
3081 if ( strcmp( c->argv[2], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) {
3082 qm->attr_sets[num].count = 1;
3083 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 2,
3084 sizeof( AttributeName ) );
3085 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_USER_ATTRIBUTES );
3088 } else if ( strcmp( c->argv[2], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 ) {
3089 qm->attr_sets[num].count = 1;
3090 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 2,
3091 sizeof( AttributeName ) );
3092 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
3095 } else if ( strcmp( c->argv[2], LDAP_NO_ATTRS ) == 0 ) {
3098 /* else: fallthru */
3100 } else if ( c->argc == 4 ) {
3101 if ( ( strcmp( c->argv[2], LDAP_ALL_USER_ATTRIBUTES ) == 0 && strcmp( c->argv[3], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 )
3102 || ( strcmp( c->argv[2], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 && strcmp( c->argv[3], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) )
3104 qm->attr_sets[num].count = 2;
3105 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 3,
3106 sizeof( AttributeName ) );
3107 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_USER_ATTRIBUTES );
3108 BER_BVSTR( &qm->attr_sets[num].attrs[1].an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
3111 /* else: fallthru */
3114 if ( c->argc > 2 ) {
3115 int all_user = 0, all_op = 0;
3117 qm->attr_sets[num].count = c->argc - 2;
3118 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( c->argc - 1,
3119 sizeof( AttributeName ) );
3120 attr_name = qm->attr_sets[num].attrs;
3121 for ( i = 2; i < c->argc; i++ ) {
3122 attr_name->an_desc = NULL;
3123 if ( strcmp( c->argv[i], LDAP_NO_ATTRS ) == 0 ) {
3124 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3125 "invalid attr #%d \"%s\" in attrlist",
3126 i - 2, c->argv[i] );
3127 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3128 ch_free( qm->attr_sets[num].attrs );
3129 qm->attr_sets[num].attrs = NULL;
3130 qm->attr_sets[num].count = 0;
3133 if ( strcmp( c->argv[i], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) {
3135 BER_BVSTR( &attr_name->an_name, LDAP_ALL_USER_ATTRIBUTES );
3136 } else if ( strcmp( c->argv[i], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 ) {
3138 BER_BVSTR( &attr_name->an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
3140 if ( slap_str2ad( c->argv[i], &attr_name->an_desc, &text ) ) {
3141 strcpy( c->cr_msg, text );
3142 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3143 ch_free( qm->attr_sets[num].attrs );
3144 qm->attr_sets[num].attrs = NULL;
3145 qm->attr_sets[num].count = 0;
3148 attr_name->an_name = attr_name->an_desc->ad_cname;
3150 attr_name->an_oc = NULL;
3151 attr_name->an_flags = 0;
3152 if ( attr_name->an_desc == slap_schema.si_ad_objectClass )
3153 qm->attr_sets[num].flags |= PC_GOT_OC;
3155 BER_BVZERO( &attr_name->an_name );
3158 /* warn if list contains both "*" and "+" */
3159 if ( i > 4 && all_user && all_op ) {
3160 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3161 "warning: attribute list contains \"*\" and \"+\"" );
3162 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3167 if ( cm->numattrsets == 0 ) {
3168 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"proxycache\" directive not provided yet" );
3169 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3172 if ( lutil_atoi( &i, c->argv[2] ) != 0 ) {
3173 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse template #=\"%s\"",
3175 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3179 if ( i < 0 || i >= cm->numattrsets ||
3180 !(qm->attr_sets[i].flags & PC_CONFIGURED )) {
3181 snprintf( c->cr_msg, sizeof( c->cr_msg ), "template index %d invalid (%s%d)",
3182 i, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
3183 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3186 temp = ch_calloc( 1, sizeof( QueryTemplate ));
3187 temp->qmnext = qm->templates;
3188 qm->templates = temp;
3189 ldap_pvt_thread_rdwr_init( &temp->t_rwlock );
3190 temp->query = temp->query_last = NULL;
3191 if ( lutil_parse_time( c->argv[3], &t ) != 0 ) {
3192 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3193 "unable to parse template ttl=\"%s\"",
3195 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3198 temp->ttl = (time_t)t;
3199 temp->negttl = (time_t)0;
3200 temp->limitttl = (time_t)0;
3201 temp->ttr = (time_t)0;
3202 switch ( c->argc ) {
3204 if ( lutil_parse_time( c->argv[6], &t ) != 0 ) {
3205 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3206 "unable to parse template ttr=\"%s\"",
3208 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3211 temp->ttr = (time_t)t;
3215 if ( lutil_parse_time( c->argv[5], &t ) != 0 ) {
3216 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3217 "unable to parse template sizelimit ttl=\"%s\"",
3219 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3222 temp->limitttl = (time_t)t;
3226 if ( lutil_parse_time( c->argv[4], &t ) != 0 ) {
3227 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3228 "unable to parse template negative ttl=\"%s\"",
3230 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3233 temp->negttl = (time_t)t;
3237 temp->no_of_queries = 0;
3239 ber_str2bv( c->argv[1], 0, 1, &temp->querystr );
3240 Debug( pcache_debug, "Template:\n", 0, 0, 0 );
3241 Debug( pcache_debug, " query template: %s\n",
3242 temp->querystr.bv_val, 0, 0 );
3243 temp->attr_set_index = i;
3244 qm->attr_sets[i].flags |= PC_REFERENCED;
3245 temp->qtnext = qm->attr_sets[i].templates;
3246 qm->attr_sets[i].templates = temp;
3247 Debug( pcache_debug, " attributes: \n", 0, 0, 0 );
3248 if ( ( attrarray = qm->attr_sets[i].attrs ) != NULL ) {
3249 for ( i=0; attrarray[i].an_name.bv_val; i++ )
3250 Debug( pcache_debug, "\t%s\n",
3251 attrarray[i].an_name.bv_val, 0, 0 );
3255 if ( strcasecmp( c->argv[1], "head" ) == 0 ) {
3256 cm->response_cb = PCACHE_RESPONSE_CB_HEAD;
3258 } else if ( strcasecmp( c->argv[1], "tail" ) == 0 ) {
3259 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
3262 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unknown specifier" );
3263 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3268 if ( c->value_int <= 0 ) {
3269 snprintf( c->cr_msg, sizeof( c->cr_msg ), "max queries must be positive" );
3270 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3273 cm->max_queries = c->value_int;
3277 cm->cc_paused |= PCACHE_CC_OFFLINE;
3279 cm->cc_paused &= ~PCACHE_CC_OFFLINE;
3282 if ( cm->db.be_private == NULL ) {
3283 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3284 "private database must be defined before setting database specific options" );
3285 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3289 if ( cm->db.bd_info->bi_cf_ocs ) {
3292 char *argv0 = c->argv[ 0 ];
3294 c->argv[ 0 ] = &argv0[ STRLENOF( "proxycache-" ) ];
3296 ct = config_find_keyword( cm->db.bd_info->bi_cf_ocs->co_table, c );
3298 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3299 "private database does not recognize specific option '%s'",
3301 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3305 c->table = cm->db.bd_info->bi_cf_ocs->co_type;
3307 c->bi = c->be->bd_info;
3309 rc = config_add_vals( ct, c );
3313 c->table = c2.table;
3316 c->argv[ 0 ] = argv0;
3318 } else if ( cm->db.be_config != NULL ) {
3319 char *argv0 = c->argv[ 0 ];
3321 c->argv[ 0 ] = &argv0[ STRLENOF( "proxycache-" ) ];
3322 rc = cm->db.be_config( &cm->db, c->fname, c->lineno, c->argc, c->argv );
3323 c->argv[ 0 ] = argv0;
3326 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3327 "no means to set private database specific options" );
3328 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3333 rc = SLAP_CONF_UNKNOWN;
3349 slap_overinst *on = (slap_overinst *)be->bd_info;
3350 cache_manager* cm = on->on_bi.bi_private;
3352 /* Something for the cache database? */
3353 if ( cm->db.bd_info && cm->db.bd_info->bi_db_config )
3354 return cm->db.bd_info->bi_db_config( &cm->db, fname, lineno,
3356 return SLAP_CONF_UNKNOWN;
3364 slap_overinst *on = (slap_overinst *)be->bd_info;
3368 cm = (cache_manager *)ch_malloc(sizeof(cache_manager));
3369 on->on_bi.bi_private = cm;
3371 qm = (query_manager*)ch_malloc(sizeof(query_manager));
3374 SLAP_DBFLAGS(&cm->db) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3375 cm->db.be_private = NULL;
3376 cm->db.bd_self = &cm->db;
3378 cm->numattrsets = 0;
3379 cm->num_entries_limit = 5;
3380 cm->num_cached_queries = 0;
3381 cm->max_entries = 0;
3382 cm->cur_entries = 0;
3383 cm->max_queries = 10000;
3384 cm->save_queries = 0;
3385 cm->check_cacheability = 0;
3386 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
3387 cm->defer_db_open = 1;
3388 cm->cc_period = 1000;
3392 qm->attr_sets = NULL;
3393 qm->templates = NULL;
3395 qm->lru_bottom = NULL;
3397 qm->qcfunc = query_containment;
3398 qm->crfunc = cache_replacement;
3399 qm->addfunc = add_query;
3400 ldap_pvt_thread_mutex_init(&qm->lru_mutex);
3402 ldap_pvt_thread_mutex_init(&cm->cache_mutex);
3407 pcache_cachedquery_open_cb( Operation *op, SlapReply *rs )
3409 assert( op->o_tag == LDAP_REQ_SEARCH );
3411 if ( rs->sr_type == REP_SEARCH ) {
3414 a = attr_find( rs->sr_entry->e_attrs, ad_cachedQueryURL );
3418 assert( a->a_nvals != NULL );
3420 valsp = op->o_callback->sc_private;
3421 assert( *valsp == NULL );
3423 ber_bvarray_dup_x( valsp, a->a_nvals, op->o_tmpmemctx );
3431 pcache_cachedquery_count_cb( Operation *op, SlapReply *rs )
3433 assert( op->o_tag == LDAP_REQ_SEARCH );
3435 if ( rs->sr_type == REP_SEARCH ) {
3436 int *countp = (int *)op->o_callback->sc_private;
3449 cache_manager *cm = on->on_bi.bi_private;
3450 query_manager* qm = cm->qm;
3453 rc = backend_startup_one( &cm->db, cr );
3455 cm->defer_db_open = 0;
3458 /* There is no runqueue in TOOL mode */
3459 if (( slapMode & SLAP_SERVER_MODE ) && rc == 0 ) {
3460 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3461 ldap_pvt_runqueue_insert( &slapd_rq, cm->cc_period,
3462 consistency_check, on,
3463 "pcache_consistency", cm->db.be_suffix[0].bv_val );
3464 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3466 /* Cached database must have the rootdn */
3467 if ( BER_BVISNULL( &cm->db.be_rootndn )
3468 || BER_BVISEMPTY( &cm->db.be_rootndn ) )
3470 Debug( LDAP_DEBUG_ANY, "pcache_db_open(): "
3471 "underlying database of type \"%s\"\n"
3472 " serving naming context \"%s\"\n"
3473 " has no \"rootdn\", required by \"proxycache\".\n",
3474 on->on_info->oi_orig->bi_type,
3475 cm->db.be_suffix[0].bv_val, 0 );
3479 if ( cm->save_queries ) {
3480 void *thrctx = ldap_pvt_thread_pool_context();
3481 Connection conn = { 0 };
3482 OperationBuffer opbuf;
3484 slap_callback cb = { 0 };
3485 SlapReply rs = { 0 };
3486 BerVarray vals = NULL;
3487 Filter f = { 0 }, f2 = { 0 };
3488 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
3489 AttributeName attrs[ 2 ] = {{{ 0 }}};
3491 connection_fake_init( &conn, &opbuf, thrctx );
3496 op->o_tag = LDAP_REQ_SEARCH;
3497 op->o_protocol = LDAP_VERSION3;
3498 cb.sc_response = pcache_cachedquery_open_cb;
3499 cb.sc_private = &vals;
3500 op->o_callback = &cb;
3501 op->o_time = slap_get_time();
3502 op->o_do_not_cache = 1;
3503 op->o_managedsait = SLAP_CONTROL_CRITICAL;
3505 op->o_dn = cm->db.be_rootdn;
3506 op->o_ndn = cm->db.be_rootndn;
3507 op->o_req_dn = cm->db.be_suffix[ 0 ];
3508 op->o_req_ndn = cm->db.be_nsuffix[ 0 ];
3510 op->ors_scope = LDAP_SCOPE_BASE;
3511 op->ors_deref = LDAP_DEREF_NEVER;
3513 op->ors_tlimit = SLAP_NO_LIMIT;
3514 ber_str2bv( "(cachedQueryURL=*)", 0, 0, &op->ors_filterstr );
3515 f.f_choice = LDAP_FILTER_PRESENT;
3516 f.f_desc = ad_cachedQueryURL;
3517 op->ors_filter = &f;
3518 attrs[ 0 ].an_desc = ad_cachedQueryURL;
3519 attrs[ 0 ].an_name = ad_cachedQueryURL->ad_cname;
3520 op->ors_attrs = attrs;
3521 op->ors_attrsonly = 0;
3523 rc = op->o_bd->be_search( op, &rs );
3524 if ( rc == LDAP_SUCCESS && vals != NULL ) {
3527 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
3528 if ( url2query( vals[ i ].bv_val, op, qm ) == 0 ) {
3529 cm->num_cached_queries++;
3533 ber_bvarray_free_x( vals, op->o_tmpmemctx );
3536 /* count cached entries */
3537 f.f_choice = LDAP_FILTER_NOT;
3539 f2.f_choice = LDAP_FILTER_EQUALITY;
3541 f2.f_av_desc = slap_schema.si_ad_objectClass;
3542 BER_BVSTR( &f2.f_av_value, "glue" );
3543 ber_str2bv( "(!(objectClass=glue))", 0, 0, &op->ors_filterstr );
3545 op->ors_slimit = SLAP_NO_LIMIT;
3546 op->ors_scope = LDAP_SCOPE_SUBTREE;
3547 op->ors_attrs = slap_anlist_no_attrs;
3549 op->o_callback->sc_response = pcache_cachedquery_count_cb;
3551 op->o_callback->sc_private = &rs.sr_nentries;
3553 rc = op->o_bd->be_search( op, &rs );
3555 cm->cur_entries = rs.sr_nentries;
3569 slap_overinst *on = (slap_overinst *)be->bd_info;
3570 cache_manager *cm = on->on_bi.bi_private;
3571 query_manager* qm = cm->qm;
3572 int i, ncf = 0, rf = 0, nrf = 0, rc = 0;
3574 /* check attr sets */
3575 for ( i = 0; i < cm->numattrsets; i++) {
3576 if ( !( qm->attr_sets[i].flags & PC_CONFIGURED ) ) {
3577 if ( qm->attr_sets[i].flags & PC_REFERENCED ) {
3578 Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d not configured but referenced.\n", i, 0, 0 );
3582 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, attr set #%d not configured.\n", i, 0, 0 );
3586 } else if ( !( qm->attr_sets[i].flags & PC_REFERENCED ) ) {
3587 Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d configured but not referenced.\n", i, 0, 0 );
3592 if ( ncf || rf || nrf ) {
3593 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets configured but not referenced.\n", nrf, 0, 0 );
3594 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets not configured.\n", ncf, 0, 0 );
3595 Debug( LDAP_DEBUG_CONFIG, "pcache: %d attr sets not configured but referenced.\n", rf, 0, 0 );
3602 /* need to inherit something from the original database... */
3603 cm->db.be_def_limit = be->be_def_limit;
3604 cm->db.be_limits = be->be_limits;
3605 cm->db.be_acl = be->be_acl;
3606 cm->db.be_dfltaccess = be->be_dfltaccess;
3608 if ( SLAP_DBMONITORING( be ) ) {
3609 SLAP_DBFLAGS( &cm->db ) |= SLAP_DBFLAG_MONITORING;
3612 SLAP_DBFLAGS( &cm->db ) &= ~SLAP_DBFLAG_MONITORING;
3615 if ( !cm->defer_db_open )
3616 rc = pcache_db_open2( on, cr );
3622 pcache_free_qbase( void *v )
3628 tavl_free( qb->scopes[i], NULL );
3638 slap_overinst *on = (slap_overinst *)be->bd_info;
3639 cache_manager *cm = on->on_bi.bi_private;
3640 query_manager *qm = cm->qm;
3644 if ( cm->save_queries ) {
3646 BerVarray vals = NULL;
3649 Connection conn = { 0 };
3650 OperationBuffer opbuf;
3652 slap_callback cb = { 0 };
3654 SlapReply rs = { REP_RESULT };
3655 Modifications mod = {{ 0 }};
3657 thrctx = ldap_pvt_thread_pool_context();
3659 connection_fake_init( &conn, &opbuf, thrctx );
3662 if ( qm->templates != NULL ) {
3663 for ( tm = qm->templates; tm != NULL; tm = tm->qmnext ) {
3664 for ( qc = tm->query; qc; qc = qc->next ) {
3667 if ( query2url( op, qc, &bv, 0 ) == 0 ) {
3668 ber_bvarray_add_x( &vals, &bv, op->o_tmpmemctx );
3675 op->o_dn = cm->db.be_rootdn;
3676 op->o_ndn = cm->db.be_rootndn;
3678 op->o_tag = LDAP_REQ_MODIFY;
3679 op->o_protocol = LDAP_VERSION3;
3680 cb.sc_response = slap_null_cb;
3681 op->o_callback = &cb;
3682 op->o_time = slap_get_time();
3683 op->o_do_not_cache = 1;
3684 op->o_managedsait = SLAP_CONTROL_CRITICAL;
3686 op->o_req_dn = op->o_bd->be_suffix[0];
3687 op->o_req_ndn = op->o_bd->be_nsuffix[0];
3689 mod.sml_op = LDAP_MOD_REPLACE;
3691 mod.sml_desc = ad_cachedQueryURL;
3692 mod.sml_type = ad_cachedQueryURL->ad_cname;
3693 mod.sml_values = vals;
3694 mod.sml_nvalues = NULL;
3695 mod.sml_numvals = 1;
3696 mod.sml_next = NULL;
3697 Debug( pcache_debug,
3698 "%sSETTING CACHED QUERY URLS\n",
3699 vals == NULL ? "RE" : "", 0, 0 );
3701 op->orm_modlist = &mod;
3703 op->o_bd->be_modify( op, &rs );
3705 ber_bvarray_free_x( vals, op->o_tmpmemctx );
3708 /* cleanup stuff inherited from the original database... */
3709 cm->db.be_limits = NULL;
3710 cm->db.be_acl = NULL;
3712 /* stop the thread ... */
3714 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3715 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, cm->cc_arg ) ) {
3716 ldap_pvt_runqueue_stoptask( &slapd_rq, cm->cc_arg );
3718 ldap_pvt_runqueue_remove( &slapd_rq, cm->cc_arg );
3719 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3722 if ( cm->db.bd_info->bi_db_close ) {
3723 rc = cm->db.bd_info->bi_db_close( &cm->db, NULL );
3725 while ( (tm = qm->templates) != NULL ) {
3726 CachedQuery *qc, *qn;
3727 qm->templates = tm->qmnext;
3728 for ( qc = tm->query; qc; qc = qn ) {
3732 avl_free( tm->qbase, pcache_free_qbase );
3733 free( tm->querystr.bv_val );
3734 ldap_pvt_thread_rdwr_destroy( &tm->t_rwlock );
3735 free( tm->t_attrs.attrs );
3739 for ( i=0; i<cm->numattrsets; i++ ) {
3740 free( qm->attr_sets[i].attrs );
3742 free( qm->attr_sets );
3743 qm->attr_sets = NULL;
3754 slap_overinst *on = (slap_overinst *)be->bd_info;
3755 cache_manager *cm = on->on_bi.bi_private;
3756 query_manager *qm = cm->qm;
3758 if ( cm->db.be_private != NULL ) {
3759 backend_stopdown_one( &cm->db );
3762 ldap_pvt_thread_mutex_destroy( &qm->lru_mutex );
3763 ldap_pvt_thread_mutex_destroy( &cm->cache_mutex );
3770 #ifdef PCACHE_CONTROL_PRIVDB
3772 Control ::= SEQUENCE {
3773 controlType LDAPOID,
3774 criticality BOOLEAN DEFAULT FALSE,
3775 controlValue OCTET STRING OPTIONAL }
3777 controlType ::= 1.3.6.1.4.1.4203.666.11.9.5.1
3779 * criticality must be TRUE; controlValue must be absent.
3787 if ( op->o_ctrlflag[ privDB_cid ] != SLAP_CONTROL_NONE ) {
3788 rs->sr_text = "privateDB control specified multiple times";
3789 return LDAP_PROTOCOL_ERROR;
3792 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
3793 rs->sr_text = "privateDB control value not absent";
3794 return LDAP_PROTOCOL_ERROR;
3797 if ( !ctrl->ldctl_iscritical ) {
3798 rs->sr_text = "privateDB control criticality required";
3799 return LDAP_PROTOCOL_ERROR;
3802 op->o_ctrlflag[ privDB_cid ] = SLAP_CONTROL_CRITICAL;
3804 return LDAP_SUCCESS;
3807 static char *extops[] = {
3808 LDAP_EXOP_MODIFY_PASSWD,
3811 #endif /* PCACHE_CONTROL_PRIVDB */
3813 #ifdef PCACHE_EXOP_QUERY_DELETE
3814 static struct berval pcache_exop_QUERY_DELETE = BER_BVC( PCACHE_EXOP_QUERY_DELETE );
3816 #define LDAP_TAG_EXOP_QUERY_DELETE_BASE ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 0)
3817 #define LDAP_TAG_EXOP_QUERY_DELETE_DN ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 1)
3818 #define LDAP_TAG_EXOP_QUERY_DELETE_UUID ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 2)
3821 ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
3822 requestName [0] LDAPOID,
3823 requestValue [1] OCTET STRING OPTIONAL }
3825 requestName ::= 1.3.6.1.4.1.4203.666.11.9.6.1
3827 requestValue ::= SEQUENCE { CHOICE {
3829 entryDN [1] LDAPDN },
3830 queryID [2] OCTET STRING (SIZE(16))
3831 -- constrained to UUID }
3833 * Either baseDN or entryDN must be present, to allow database selection.
3835 * 1. if baseDN and queryID are present, then the query corresponding
3836 * to queryID is deleted;
3837 * 2. if baseDN is present and queryID is absent, then all queries
3839 * 3. if entryDN is present and queryID is absent, then all queries
3840 * corresponding to the queryID values present in entryDN are deleted;
3841 * 4. if entryDN and queryID are present, then all queries
3842 * corresponding to the queryID values present in entryDN are deleted,
3843 * but only if the value of queryID is contained in the entry;
3845 * Currently, only 1, 3 and 4 are implemented. 2 can be obtained by either
3846 * recursively deleting the database (ldapdelete -r) with PRIVDB control,
3847 * or by removing the database files.
3849 ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
3850 COMPONENTS OF LDAPResult,
3851 responseName [10] LDAPOID OPTIONAL,
3852 responseValue [11] OCTET STRING OPTIONAL }
3854 * responseName and responseValue must be absent.
3858 * - on success, *tagp is either LDAP_TAG_EXOP_QUERY_DELETE_BASE
3859 * or LDAP_TAG_EXOP_QUERY_DELETE_DN.
3860 * - if ndn != NULL, it is set to the normalized DN in the request
3861 * corresponding to either the baseDN or the entryDN, according
3862 * to *tagp; memory is malloc'ed on the Operation's slab, and must
3863 * be freed by the caller.
3864 * - if uuid != NULL, it is set to point to the normalized UUID;
3865 * memory is malloc'ed on the Operation's slab, and must
3866 * be freed by the caller.
3869 pcache_parse_query_delete(
3873 struct berval *uuid,
3877 int rc = LDAP_SUCCESS;
3880 BerElementBuffer berbuf;
3881 BerElement *ber = (BerElement *)&berbuf;
3882 struct berval reqdata = BER_BVNULL;
3894 if ( in == NULL || in->bv_len == 0 ) {
3895 *text = "empty request data field in queryDelete exop";
3896 return LDAP_PROTOCOL_ERROR;
3899 ber_dupbv_x( &reqdata, in, ctx );
3901 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
3902 ber_init2( ber, &reqdata, 0 );
3904 tag = ber_scanf( ber, "{" /*}*/ );
3906 if ( tag == LBER_ERROR ) {
3907 Debug( LDAP_DEBUG_TRACE,
3908 "pcache_parse_query_delete: decoding error.\n",
3910 goto decoding_error;
3913 tag = ber_peek_tag( ber, &len );
3914 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_BASE
3915 || tag == LDAP_TAG_EXOP_QUERY_DELETE_DN )
3919 if ( ndn != NULL ) {
3922 tag = ber_scanf( ber, "m", &dn );
3923 if ( tag == LBER_ERROR ) {
3924 Debug( LDAP_DEBUG_TRACE,
3925 "pcache_parse_query_delete: DN parse failed.\n",
3927 goto decoding_error;
3930 rc = dnNormalize( 0, NULL, NULL, &dn, ndn, ctx );
3931 if ( rc != LDAP_SUCCESS ) {
3932 *text = "invalid DN in queryDelete exop request data";
3937 tag = ber_scanf( ber, "x" /* "m" */ );
3938 if ( tag == LBER_DEFAULT ) {
3939 goto decoding_error;
3943 tag = ber_peek_tag( ber, &len );
3946 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_UUID ) {
3947 if ( uuid != NULL ) {
3949 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
3951 tag = ber_scanf( ber, "m", &bv );
3952 if ( tag == LBER_ERROR ) {
3953 Debug( LDAP_DEBUG_TRACE,
3954 "pcache_parse_query_delete: UUID parse failed.\n",
3956 goto decoding_error;
3959 if ( bv.bv_len != 16 ) {
3960 Debug( LDAP_DEBUG_TRACE,
3961 "pcache_parse_query_delete: invalid UUID length %lu.\n",
3962 (unsigned long)bv.bv_len, 0, 0 );
3963 goto decoding_error;
3966 rc = lutil_uuidstr_from_normalized(
3967 bv.bv_val, bv.bv_len,
3968 uuidbuf, sizeof( uuidbuf ) );
3970 goto decoding_error;
3972 ber_str2bv( uuidbuf, rc, 1, uuid );
3976 tag = ber_skip_tag( ber, &len );
3977 if ( tag == LBER_DEFAULT ) {
3978 goto decoding_error;
3982 Debug( LDAP_DEBUG_TRACE,
3983 "pcache_parse_query_delete: invalid UUID length %lu.\n",
3984 (unsigned long)len, 0, 0 );
3985 goto decoding_error;
3989 tag = ber_peek_tag( ber, &len );
3992 if ( tag != LBER_DEFAULT || len != 0 ) {
3994 Debug( LDAP_DEBUG_TRACE,
3995 "pcache_parse_query_delete: decoding error\n",
3997 rc = LDAP_PROTOCOL_ERROR;
3998 *text = "queryDelete data decoding error";
4001 if ( ndn && !BER_BVISNULL( ndn ) ) {
4002 slap_sl_free( ndn->bv_val, ctx );
4006 if ( uuid && !BER_BVISNULL( uuid ) ) {
4007 slap_sl_free( uuid->bv_val, ctx );
4012 if ( !BER_BVISNULL( &reqdata ) ) {
4013 ber_memfree_x( reqdata.bv_val, ctx );
4020 pcache_exop_query_delete(
4024 BackendDB *bd = op->o_bd;
4026 struct berval uuid = BER_BVNULL,
4028 char buf[ SLAP_TEXT_BUFLEN ];
4030 ber_tag_t tag = LBER_DEFAULT;
4032 if ( LogTest( LDAP_DEBUG_STATS ) ) {
4036 rs->sr_err = pcache_parse_query_delete( op->ore_reqdata,
4037 &tag, &op->o_req_ndn, uuidp,
4038 &rs->sr_text, op->o_tmpmemctx );
4039 if ( rs->sr_err != LDAP_SUCCESS ) {
4043 if ( LogTest( LDAP_DEBUG_STATS ) ) {
4044 assert( !BER_BVISNULL( &op->o_req_ndn ) );
4045 len = snprintf( buf, sizeof( buf ), " dn=\"%s\"", op->o_req_ndn.bv_val );
4047 if ( !BER_BVISNULL( &uuid ) && len < sizeof( buf ) ) {
4048 snprintf( &buf[ len ], sizeof( buf ) - len, " queryId=\"%s\"", uuid.bv_val );
4051 Debug( LDAP_DEBUG_STATS, "%s QUERY DELETE%s\n",
4052 op->o_log_prefix, buf, 0 );
4054 op->o_req_dn = op->o_req_ndn;
4056 op->o_bd = select_backend( &op->o_req_ndn, 0 );
4057 rs->sr_err = backend_check_restrictions( op, rs,
4058 (struct berval *)&pcache_exop_QUERY_DELETE );
4059 if ( rs->sr_err != LDAP_SUCCESS ) {
4063 if ( op->o_bd->be_extended == NULL ) {
4064 send_ldap_error( op, rs, LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
4065 "backend does not support extended operations" );
4069 op->o_bd->be_extended( op, rs );
4072 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
4073 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
4074 BER_BVZERO( &op->o_req_ndn );
4075 BER_BVZERO( &op->o_req_dn );
4078 if ( !BER_BVISNULL( &uuid ) ) {
4079 op->o_tmpfree( uuid.bv_val, op->o_tmpmemctx );
4088 pcache_op_extended( Operation *op, SlapReply *rs )
4090 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
4091 cache_manager *cm = on->on_bi.bi_private;
4093 #ifdef PCACHE_CONTROL_PRIVDB
4094 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL ) {
4095 return pcache_op_privdb( op, rs );
4097 #endif /* PCACHE_CONTROL_PRIVDB */
4099 if ( bvmatch( &op->ore_reqoid, &pcache_exop_QUERY_DELETE ) ) {
4100 struct berval uuid = BER_BVNULL;
4101 ber_tag_t tag = LBER_DEFAULT;
4103 rs->sr_err = pcache_parse_query_delete( op->ore_reqdata,
4104 &tag, NULL, &uuid, &rs->sr_text, op->o_tmpmemctx );
4105 assert( rs->sr_err == LDAP_SUCCESS );
4107 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_DN ) {
4108 /* remove all queries related to the selected entry */
4109 rs->sr_err = pcache_remove_entry_queries_from_cache( op,
4110 cm, &op->o_req_ndn, &uuid );
4112 } else if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_BASE ) {
4113 if ( !BER_BVISNULL( &uuid ) ) {
4114 /* remove the selected query */
4115 rs->sr_err = pcache_remove_query_from_cache( op,
4119 /* TODO: remove all queries */
4120 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
4121 rs->sr_text = "deletion of all queries not implemented";
4125 op->o_tmpfree( uuid.bv_val, op->o_tmpmemctx );
4130 #endif /* PCACHE_EXOP_QUERY_DELETE */
4132 static slap_overinst pcache;
4134 static char *obsolete_names[] = {
4139 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
4141 #endif /* SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC */
4146 struct berval debugbv = BER_BVC("pcache");
4148 code = slap_loglevel_get( &debugbv, &pcache_debug );
4153 #ifdef PCACHE_CONTROL_PRIVDB
4154 code = register_supported_control( PCACHE_CONTROL_PRIVDB,
4155 SLAP_CTRL_BIND|SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE, extops,
4156 parse_privdb_ctrl, &privDB_cid );
4157 if ( code != LDAP_SUCCESS ) {
4158 Debug( LDAP_DEBUG_ANY,
4159 "pcache_initialize: failed to register control %s (%d)\n",
4160 PCACHE_CONTROL_PRIVDB, code, 0 );
4163 #endif /* PCACHE_CONTROL_PRIVDB */
4165 #ifdef PCACHE_EXOP_QUERY_DELETE
4166 code = load_extop2( (struct berval *)&pcache_exop_QUERY_DELETE,
4167 SLAP_EXOP_WRITES|SLAP_EXOP_HIDE, pcache_exop_query_delete,
4169 if ( code != LDAP_SUCCESS ) {
4170 Debug( LDAP_DEBUG_ANY,
4171 "pcache_initialize: unable to register queryDelete exop: %d.\n",
4175 #endif /* PCACHE_EXOP_QUERY_DELETE */
4177 for ( i = 0; as[i].desc != NULL; i++ ) {
4178 code = register_at( as[i].desc, as[i].adp, 0 );
4180 Debug( LDAP_DEBUG_ANY,
4181 "pcache_initialize: register_at #%d failed\n", i, 0, 0 );
4184 (*as[i].adp)->ad_type->sat_flags |= SLAP_AT_HIDE;
4187 pcache.on_bi.bi_type = "pcache";
4188 pcache.on_bi.bi_obsolete_names = obsolete_names;
4189 pcache.on_bi.bi_db_init = pcache_db_init;
4190 pcache.on_bi.bi_db_config = pcache_db_config;
4191 pcache.on_bi.bi_db_open = pcache_db_open;
4192 pcache.on_bi.bi_db_close = pcache_db_close;
4193 pcache.on_bi.bi_db_destroy = pcache_db_destroy;
4195 pcache.on_bi.bi_op_search = pcache_op_search;
4196 #ifdef PCACHE_CONTROL_PRIVDB
4197 pcache.on_bi.bi_op_bind = pcache_op_privdb;
4198 pcache.on_bi.bi_op_compare = pcache_op_privdb;
4199 pcache.on_bi.bi_op_modrdn = pcache_op_privdb;
4200 pcache.on_bi.bi_op_modify = pcache_op_privdb;
4201 pcache.on_bi.bi_op_add = pcache_op_privdb;
4202 pcache.on_bi.bi_op_delete = pcache_op_privdb;
4203 #endif /* PCACHE_CONTROL_PRIVDB */
4204 #ifdef PCACHE_EXOP_QUERY_DELETE
4205 pcache.on_bi.bi_extended = pcache_op_extended;
4206 #elif defined( PCACHE_CONTROL_PRIVDB )
4207 pcache.on_bi.bi_extended = pcache_op_privdb;
4210 pcache.on_bi.bi_chk_controls = pcache_chk_controls;
4212 pcache.on_bi.bi_cf_ocs = pcocs;
4214 code = config_register_schema( pccfg, pcocs );
4215 if ( code ) return code;
4217 return overlay_register( &pcache );
4220 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
4221 int init_module(int argc, char *argv[]) {
4222 return pcache_initialize();
4226 #endif /* defined(SLAPD_OVER_PROXYCACHE) */