2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2003-2011 The OpenLDAP Foundation.
5 * Portions Copyright 2003 IBM Corporation.
6 * Portions Copyright 2003-2009 Symas Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by Apurva Kumar for inclusion
19 * in OpenLDAP Software and subsequently rewritten by Howard Chu.
24 #ifdef SLAPD_OVER_PROXYCACHE
28 #include <ac/string.h>
36 #include "../back-monitor/back-monitor.h"
42 * Control that allows to access the private DB
43 * instead of the public one
45 #define PCACHE_CONTROL_PRIVDB "1.3.6.1.4.1.4203.666.11.9.5.1"
48 * Extended Operation that allows to remove a query from the cache
50 #define PCACHE_EXOP_QUERY_DELETE "1.3.6.1.4.1.4203.666.11.9.6.1"
55 #define PCACHE_MONITOR
58 /* query cache structs */
61 typedef struct Query_s {
62 Filter* filter; /* Search Filter */
63 struct berval base; /* Search Base */
64 int scope; /* Search scope */
67 struct query_template_s;
69 typedef struct Qbase_s {
70 Avlnode *scopes[4]; /* threaded AVL trees of cached queries */
75 /* struct representing a cached query */
76 typedef struct cached_query_s {
81 struct berval q_uuid; /* query identifier */
83 struct query_template_s *qtemp; /* template of the query */
84 time_t expiry_time; /* time till the query is considered invalid */
85 time_t refresh_time; /* time till the query is refreshed */
86 time_t bindref_time; /* time till the bind is refreshed */
87 int bind_refcnt; /* number of bind operation referencing this query */
88 unsigned long answerable_cnt; /* how many times it was answerable */
89 int refcnt; /* references since last refresh */
90 ldap_pvt_thread_mutex_t answerable_cnt_mutex;
91 struct cached_query_s *next; /* next query in the template */
92 struct cached_query_s *prev; /* previous query in the template */
93 struct cached_query_s *lru_up; /* previous query in the LRU list */
94 struct cached_query_s *lru_down; /* next query in the LRU list */
95 ldap_pvt_thread_rdwr_t rwlock;
101 * ldap:///<base>??<scope>?<filter>?x-uuid=<uid>,x-template=<template>,x-attrset=<attrset>,x-expiry=<expiry>,x-refresh=<refresh>
103 * <base> ::= CachedQuery.qbase->base
104 * <scope> ::= CachedQuery.scope
105 * <filter> ::= filter2bv(CachedQuery.filter)
106 * <uuid> ::= CachedQuery.q_uuid
107 * <attrset> ::= CachedQuery.qtemp->attr_set_index
108 * <expiry> ::= CachedQuery.expiry_time
109 * <refresh> ::= CachedQuery.refresh_time
111 * quick hack: parse URI, call add_query() and then fix
112 * CachedQuery.expiry_time and CachedQuery.q_uuid
114 * NOTE: if the <attrset> changes, all stored URLs will be invalidated.
118 * Represents a set of projected attributes.
122 struct query_template_s *templates;
123 AttributeName* attrs; /* specifies the set */
125 #define PC_CONFIGURED (0x1)
126 #define PC_REFERENCED (0x2)
127 #define PC_GOT_OC (0x4)
128 int count; /* number of attributes */
131 /* struct representing a query template
132 * e.g. template string = &(cn=)(mail=)
134 typedef struct query_template_s {
135 struct query_template_s *qtnext;
136 struct query_template_s *qmnext;
139 CachedQuery* query; /* most recent query cached for the template */
140 CachedQuery* query_last; /* oldest query cached for the template */
141 ldap_pvt_thread_rdwr_t t_rwlock; /* Rd/wr lock for accessing queries in the template */
142 struct berval querystr; /* Filter string corresponding to the QT */
143 struct berval bindbase; /* base DN for Bind request */
144 struct berval bindfilterstr; /* Filter string for Bind request */
145 struct berval bindftemp; /* bind filter template */
147 AttributeDescription **bindfattrs; /* attrs to substitute in ftemp */
149 int bindnattrs; /* number of bindfattrs */
151 int attr_set_index; /* determines the projected attributes */
152 int no_of_queries; /* Total number of queries in the template */
153 time_t ttl; /* TTL for the queries of this template */
154 time_t negttl; /* TTL for negative results */
155 time_t limitttl; /* TTL for sizelimit exceeding results */
156 time_t ttr; /* time to refresh */
157 time_t bindttr; /* TTR for cached binds */
158 struct attr_set t_attrs; /* filter attrs + attr_set */
166 } pc_caching_reason_t;
168 static const char *pc_caching_reason_str[] = {
177 struct query_manager_s;
179 /* prototypes for functions for 1) query containment
180 * 2) query addition, 3) cache replacement
182 typedef CachedQuery *(QCfunc)(Operation *op, struct query_manager_s*,
183 Query*, QueryTemplate*);
184 typedef CachedQuery *(AddQueryfunc)(Operation *op, struct query_manager_s*,
185 Query*, QueryTemplate*, pc_caching_reason_t, int wlock);
186 typedef void (CRfunc)(struct query_manager_s*, struct berval*);
188 /* LDAP query cache */
189 typedef struct query_manager_s {
190 struct attr_set* attr_sets; /* possible sets of projected attributes */
191 QueryTemplate* templates; /* cacheable templates */
193 CachedQuery* lru_top; /* top and bottom of LRU list */
194 CachedQuery* lru_bottom;
196 ldap_pvt_thread_mutex_t lru_mutex; /* mutex for accessing LRU list */
198 /* Query cache methods */
199 QCfunc *qcfunc; /* Query containment*/
200 CRfunc *crfunc; /* cache replacement */
201 AddQueryfunc *addfunc; /* add query */
204 /* LDAP query cache manager */
205 typedef struct cache_manager_s {
206 BackendDB db; /* underlying database */
207 unsigned long num_cached_queries; /* total number of cached queries */
208 unsigned long max_queries; /* upper bound on # of cached queries */
209 int save_queries; /* save cached queries across restarts */
210 int check_cacheability; /* check whether a query is cacheable */
211 int numattrsets; /* number of attribute sets */
212 int cur_entries; /* current number of entries cached */
213 int max_entries; /* max number of entries cached */
214 int num_entries_limit; /* max # of entries in a cacheable query */
216 char response_cb; /* install the response callback
217 * at the tail of the callback list */
218 #define PCACHE_RESPONSE_CB_HEAD 0
219 #define PCACHE_RESPONSE_CB_TAIL 1
220 char defer_db_open; /* defer open for online add */
221 char cache_binds; /* cache binds or just passthru */
223 time_t cc_period; /* interval between successive consistency checks (sec) */
224 #define PCACHE_CC_PAUSED 1
225 #define PCACHE_CC_OFFLINE 2
229 ldap_pvt_thread_mutex_t cache_mutex;
231 query_manager* qm; /* query cache managed by the cache manager */
233 #ifdef PCACHE_MONITOR
235 struct berval monitor_ndn;
236 #endif /* PCACHE_MONITOR */
239 #ifdef PCACHE_MONITOR
240 static int pcache_monitor_db_init( BackendDB *be );
241 static int pcache_monitor_db_open( BackendDB *be );
242 static int pcache_monitor_db_close( BackendDB *be );
243 static int pcache_monitor_db_destroy( BackendDB *be );
244 #endif /* PCACHE_MONITOR */
246 static int pcache_debug;
248 #ifdef PCACHE_CONTROL_PRIVDB
249 static int privDB_cid;
250 #endif /* PCACHE_CONTROL_PRIVDB */
252 static AttributeDescription *ad_queryId, *ad_cachedQueryURL;
254 #ifdef PCACHE_MONITOR
255 static AttributeDescription *ad_numQueries, *ad_numEntries;
256 static ObjectClass *oc_olmPCache;
257 #endif /* PCACHE_MONITOR */
263 { "PCacheOID", "1.3.6.1.4.1.4203.666.11.9.1" },
264 { "PCacheAttributes", "PCacheOID:1" },
265 { "PCacheObjectClasses", "PCacheOID:2" },
272 AttributeDescription **adp;
274 { "( PCacheAttributes:1 "
275 "NAME 'pcacheQueryID' "
276 "DESC 'ID of query the entry belongs to, formatted as a UUID' "
277 "EQUALITY octetStringMatch "
278 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40{64} "
279 "NO-USER-MODIFICATION "
280 "USAGE directoryOperation )",
282 { "( PCacheAttributes:2 "
283 "NAME 'pcacheQueryURL' "
284 "DESC 'URI describing a cached query' "
285 "EQUALITY caseExactMatch "
286 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 "
287 "NO-USER-MODIFICATION "
288 "USAGE directoryOperation )",
289 &ad_cachedQueryURL },
290 #ifdef PCACHE_MONITOR
291 { "( PCacheAttributes:3 "
292 "NAME 'pcacheNumQueries' "
293 "DESC 'Number of cached queries' "
294 "EQUALITY integerMatch "
295 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
296 "NO-USER-MODIFICATION "
297 "USAGE directoryOperation )",
299 { "( PCacheAttributes:4 "
300 "NAME 'pcacheNumEntries' "
301 "DESC 'Number of cached entries' "
302 "EQUALITY integerMatch "
303 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
304 "NO-USER-MODIFICATION "
305 "USAGE directoryOperation )",
307 #endif /* PCACHE_MONITOR */
316 #ifdef PCACHE_MONITOR
317 /* augments an existing object, so it must be AUXILIARY */
318 { "( PCacheObjectClasses:1 "
319 "NAME ( 'olmPCache' ) "
323 "$ pcacheNumQueries "
324 "$ pcacheNumEntries "
327 #endif /* PCACHE_MONITOR */
336 struct berval *fstr );
343 QueryTemplate *templ,
344 pc_caching_reason_t why,
350 struct berval *query_uuid );
353 * Turn a cached query into its URL representation
356 query2url( Operation *op, CachedQuery *q, struct berval *urlbv, int dolock )
358 struct berval bv_scope,
360 char attrset_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
361 expiry_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
362 refresh_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
363 answerable_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
365 ber_len_t attrset_len,
371 ldap_pvt_thread_rdwr_rlock( &q->rwlock );
374 ldap_pvt_scope2bv( q->scope, &bv_scope );
375 filter2bv_x( op, q->filter, &bv_filter );
376 attrset_len = sprintf( attrset_buf,
377 "%lu", (unsigned long)q->qtemp->attr_set_index );
378 expiry_len = sprintf( expiry_buf,
379 "%lu", (unsigned long)q->expiry_time );
380 answerable_len = snprintf( answerable_buf, sizeof( answerable_buf ),
381 "%lu", q->answerable_cnt );
382 if ( q->refresh_time )
383 refresh_len = sprintf( refresh_buf,
384 "%lu", (unsigned long)q->refresh_time );
388 urlbv->bv_len = STRLENOF( "ldap:///" )
389 + q->qbase->base.bv_len
394 + STRLENOF( "?x-uuid=" )
396 + STRLENOF( ",x-attrset=" )
398 + STRLENOF( ",x-expiry=" )
400 + STRLENOF( ",x-answerable=" )
403 urlbv->bv_len += STRLENOF( ",x-refresh=" )
406 ptr = urlbv->bv_val = ber_memalloc_x( urlbv->bv_len + 1, op->o_tmpmemctx );
407 ptr = lutil_strcopy( ptr, "ldap:///" );
408 ptr = lutil_strcopy( ptr, q->qbase->base.bv_val );
409 ptr = lutil_strcopy( ptr, "??" );
410 ptr = lutil_strcopy( ptr, bv_scope.bv_val );
411 ptr = lutil_strcopy( ptr, "?" );
412 ptr = lutil_strcopy( ptr, bv_filter.bv_val );
413 ptr = lutil_strcopy( ptr, "?x-uuid=" );
414 ptr = lutil_strcopy( ptr, q->q_uuid.bv_val );
415 ptr = lutil_strcopy( ptr, ",x-attrset=" );
416 ptr = lutil_strcopy( ptr, attrset_buf );
417 ptr = lutil_strcopy( ptr, ",x-expiry=" );
418 ptr = lutil_strcopy( ptr, expiry_buf );
419 ptr = lutil_strcopy( ptr, ",x-answerable=" );
420 ptr = lutil_strcopy( ptr, answerable_buf );
422 ptr = lutil_strcopy( ptr, ",x-refresh=" );
423 ptr = lutil_strcopy( ptr, refresh_buf );
426 ber_memfree_x( bv_filter.bv_val, op->o_tmpmemctx );
429 ldap_pvt_thread_rdwr_runlock( &q->rwlock );
435 /* Find and record the empty filter clauses */
438 ftemp_attrs( struct berval *ftemp, struct berval *template,
439 AttributeDescription ***ret, const char **text )
445 AttributeDescription *ad;
446 AttributeDescription **descs = NULL;
449 temp2 = ch_malloc( ftemp->bv_len + 1 );
456 while ( *p1 == '(' || *p1 == '&' || *p1 == '|' || *p1 == ')' )
459 p2 = strchr( p1, '=' );
463 AC_MEMCPY( t1, p1, i );
467 if ( p2[-1] == '<' || p2[-1] == '>' ) p2--;
471 i = slap_bv2ad( &bv, &ad, text );
476 if ( *p2 == '<' || *p2 == '>' ) p2++;
477 if ( p2[1] != ')' ) {
479 while ( *p2 != ')' ) p2++;
484 descs = (AttributeDescription **)ch_realloc(descs,
485 (attr_cnt + 2)*sizeof(AttributeDescription *));
487 descs[attr_cnt++] = ad;
492 descs[attr_cnt] = NULL;
494 template->bv_val = temp2;
495 template->bv_len = t1 - temp2;
500 template_attrs( char *template, struct attr_set *set, AttributeName **ret,
511 AttributeDescription *ad;
512 AttributeName *attrs;
518 attrs = ch_calloc( set->count + 1, sizeof(AttributeName) );
519 for ( i=0; i < set->count; i++ )
520 attrs[i] = set->attrs[i];
522 alluser = an_find( attrs, slap_bv_all_user_attrs );
523 allop = an_find( attrs, slap_bv_all_operational_attrs );
526 while ( *p1 == '(' || *p1 == '&' || *p1 == '|' || *p1 == ')' ) p1++;
527 p2 = strchr( p1, '=' );
530 if ( p2[-1] == '<' || p2[-1] == '>' ) p2--;
534 i = slap_bv2ad( &bv, &ad, text );
541 if ( ad == slap_schema.si_ad_objectClass )
544 if ( is_at_operational(ad->ad_type)) {
548 } else if ( alluser ) {
551 if ( !ad_inlist( ad, attrs )) {
552 attrs = (AttributeName *)ch_realloc(attrs,
553 (attr_cnt + 2)*sizeof(AttributeName));
555 attrs[attr_cnt].an_desc = ad;
556 attrs[attr_cnt].an_name = ad->ad_cname;
557 attrs[attr_cnt].an_oc = NULL;
558 attrs[attr_cnt].an_flags = 0;
559 BER_BVZERO( &attrs[attr_cnt+1].an_name );
567 *text = "couldn't parse template";
570 if ( !got_oc && !( set->flags & PC_GOT_OC )) {
571 attrs = (AttributeName *)ch_realloc(attrs,
572 (attr_cnt + 2)*sizeof(AttributeName));
574 ad = slap_schema.si_ad_objectClass;
575 attrs[attr_cnt].an_desc = ad;
576 attrs[attr_cnt].an_name = ad->ad_cname;
577 attrs[attr_cnt].an_oc = NULL;
578 attrs[attr_cnt].an_flags = 0;
579 BER_BVZERO( &attrs[attr_cnt+1].an_name );
587 * Turn an URL representing a formerly cached query into a cached query,
588 * and try to cache it
599 LDAPURLDesc *lud = NULL;
601 tempstr = BER_BVNULL,
606 unsigned long answerable_cnt;
609 #define GOT_UUID 0x1U
610 #define GOT_ATTRSET 0x2U
611 #define GOT_EXPIRY 0x4U
612 #define GOT_ANSWERABLE 0x8U
613 #define GOT_REFRESH 0x10U
614 #define GOT_ALL (GOT_UUID|GOT_ATTRSET|GOT_EXPIRY|GOT_ANSWERABLE)
617 rc = ldap_url_parse( url, &lud );
618 if ( rc != LDAP_URL_SUCCESS ) {
622 /* non-allowed fields */
623 if ( lud->lud_host != NULL ) {
628 if ( lud->lud_attrs != NULL ) {
634 if ( strcmp( lud->lud_scheme, "ldap" ) != 0 ) {
639 /* required fields */
640 if ( lud->lud_dn == NULL || lud->lud_dn[ 0 ] == '\0' ) {
645 switch ( lud->lud_scope ) {
646 case LDAP_SCOPE_BASE:
647 case LDAP_SCOPE_ONELEVEL:
648 case LDAP_SCOPE_SUBTREE:
649 case LDAP_SCOPE_SUBORDINATE:
657 if ( lud->lud_filter == NULL || lud->lud_filter[ 0 ] == '\0' ) {
662 if ( lud->lud_exts == NULL ) {
667 for ( i = 0; lud->lud_exts[ i ] != NULL; i++ ) {
668 if ( strncmp( lud->lud_exts[ i ], "x-uuid=", STRLENOF( "x-uuid=" ) ) == 0 ) {
669 struct berval tmpUUID;
670 Syntax *syn_UUID = slap_schema.si_ad_entryUUID->ad_type->sat_syntax;
672 if ( got & GOT_UUID ) {
677 ber_str2bv( &lud->lud_exts[ i ][ STRLENOF( "x-uuid=" ) ], 0, 0, &tmpUUID );
678 if ( !BER_BVISEMPTY( &tmpUUID ) ) {
679 rc = syn_UUID->ssyn_pretty( syn_UUID, &tmpUUID, &uuid, NULL );
680 if ( rc != LDAP_SUCCESS ) {
686 } else if ( strncmp( lud->lud_exts[ i ], "x-attrset=", STRLENOF( "x-attrset=" ) ) == 0 ) {
687 if ( got & GOT_ATTRSET ) {
692 rc = lutil_atoi( &attrset, &lud->lud_exts[ i ][ STRLENOF( "x-attrset=" ) ] );
698 } else if ( strncmp( lud->lud_exts[ i ], "x-expiry=", STRLENOF( "x-expiry=" ) ) == 0 ) {
701 if ( got & GOT_EXPIRY ) {
706 rc = lutil_atoul( &l, &lud->lud_exts[ i ][ STRLENOF( "x-expiry=" ) ] );
710 expiry_time = (time_t)l;
713 } else if ( strncmp( lud->lud_exts[ i ], "x-answerable=", STRLENOF( "x-answerable=" ) ) == 0 ) {
714 if ( got & GOT_ANSWERABLE ) {
719 rc = lutil_atoul( &answerable_cnt, &lud->lud_exts[ i ][ STRLENOF( "x-answerable=" ) ] );
723 got |= GOT_ANSWERABLE;
725 } else if ( strncmp( lud->lud_exts[ i ], "x-refresh=", STRLENOF( "x-refresh=" ) ) == 0 ) {
728 if ( got & GOT_REFRESH ) {
733 rc = lutil_atoul( &l, &lud->lud_exts[ i ][ STRLENOF( "x-refresh=" ) ] );
737 refresh_time = (time_t)l;
746 if ( got != GOT_ALL ) {
751 if ( !(got & GOT_REFRESH ))
754 /* ignore expired queries */
755 if ( expiry_time <= slap_get_time()) {
758 memset( &op2.oq_search, 0, sizeof( op2.oq_search ) );
760 (void)remove_query_data( &op2, &uuid );
765 ber_str2bv( lud->lud_dn, 0, 0, &base );
766 rc = dnNormalize( 0, NULL, NULL, &base, &query.base, NULL );
767 if ( rc != LDAP_SUCCESS ) {
770 query.scope = lud->lud_scope;
771 query.filter = str2filter( lud->lud_filter );
772 if ( query.filter == NULL ) {
777 tempstr.bv_val = ch_malloc( strlen( lud->lud_filter ) + 1 );
779 if ( filter2template( op, query.filter, &tempstr ) ) {
780 ch_free( tempstr.bv_val );
785 /* check for query containment */
786 qt = qm->attr_sets[attrset].templates;
787 for ( ; qt; qt = qt->qtnext ) {
788 /* find if template i can potentially answer tempstr */
789 if ( bvmatch( &qt->querystr, &tempstr ) ) {
799 cq = add_query( op, qm, &query, qt, PC_POSITIVE, 0 );
801 cq->expiry_time = expiry_time;
802 cq->refresh_time = refresh_time;
804 cq->answerable_cnt = answerable_cnt;
807 /* it's now into cq->filter */
817 if ( query.filter != NULL ) filter_free( query.filter );
818 if ( !BER_BVISNULL( &tempstr ) ) ch_free( tempstr.bv_val );
819 if ( !BER_BVISNULL( &query.base ) ) ch_free( query.base.bv_val );
820 if ( !BER_BVISNULL( &uuid ) ) ch_free( uuid.bv_val );
821 if ( lud != NULL ) ldap_free_urldesc( lud );
826 /* Return 1 for an added entry, else 0 */
832 struct berval* query_uuid )
835 Modifications* modlist = NULL;
836 const char* text = NULL;
838 char textbuf[SLAP_TEXT_BUFLEN];
839 size_t textlen = sizeof(textbuf);
841 SlapReply sreply = {REP_RESULT};
843 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
850 /* add queryId attribute */
851 attr_merge_one( e, ad_queryId, query_uuid, NULL );
853 /* append the attribute list from the fetched entry */
854 e->e_attrs->a_next = attr;
856 op->o_tag = LDAP_REQ_ADD;
857 op->o_protocol = LDAP_VERSION3;
858 op->o_callback = &cb;
859 op->o_time = slap_get_time();
860 op->o_do_not_cache = 1;
863 op->o_req_dn = e->e_name;
864 op->o_req_ndn = e->e_nname;
865 rc = op->o_bd->be_add( op, &sreply );
867 if ( rc != LDAP_SUCCESS ) {
868 if ( rc == LDAP_ALREADY_EXISTS ) {
869 rs_reinit( &sreply, REP_RESULT );
870 slap_entry2mods( e, &modlist, &text, textbuf, textlen );
871 modlist->sml_op = LDAP_MOD_ADD;
872 op->o_tag = LDAP_REQ_MODIFY;
873 op->orm_modlist = modlist;
874 op->o_managedsait = SLAP_CONTROL_CRITICAL;
875 op->o_bd->be_modify( op, &sreply );
876 slap_mods_free( modlist, 1 );
877 } else if ( rc == LDAP_REFERRAL ||
878 rc == LDAP_NO_SUCH_OBJECT ) {
879 syncrepl_add_glue( op, e );
888 if ( op->ora_e == e )
896 /* Length-ordered sort on normalized DNs */
897 static int pcache_dn_cmp( const void *v1, const void *v2 )
899 const Qbase *q1 = v1, *q2 = v2;
901 int rc = q1->base.bv_len - q2->base.bv_len;
903 rc = strncmp( q1->base.bv_val, q2->base.bv_val, q1->base.bv_len );
907 static int lex_bvcmp( struct berval *bv1, struct berval *bv2 )
910 dif = bv1->bv_len - bv2->bv_len;
912 if ( dif > 0 ) len -= dif;
913 len = memcmp( bv1->bv_val, bv2->bv_val, len );
919 /* compare the current value in each filter */
920 static int pcache_filter_cmp( Filter *f1, Filter *f2 )
922 int rc, weight1, weight2;
924 switch( f1->f_choice ) {
925 case LDAP_FILTER_PRESENT:
928 case LDAP_FILTER_EQUALITY:
936 switch( f2->f_choice ) {
937 case LDAP_FILTER_PRESENT:
940 case LDAP_FILTER_EQUALITY:
948 rc = weight1 - weight2;
954 rc = lex_bvcmp( &f1->f_av_value, &f2->f_av_value );
957 if ( f1->f_choice == LDAP_FILTER_SUBSTRINGS ) {
959 if ( !BER_BVISNULL( &f1->f_sub_initial )) {
960 if ( !BER_BVISNULL( &f2->f_sub_initial )) {
961 rc = lex_bvcmp( &f1->f_sub_initial,
962 &f2->f_sub_initial );
966 } else if ( !BER_BVISNULL( &f2->f_sub_initial )) {
970 if ( f1->f_sub_any ) {
971 if ( f2->f_sub_any ) {
972 rc = lex_bvcmp( f1->f_sub_any,
977 } else if ( f2->f_sub_any ) {
981 if ( !BER_BVISNULL( &f1->f_sub_final )) {
982 if ( !BER_BVISNULL( &f2->f_sub_final )) {
983 rc = lex_bvcmp( &f1->f_sub_final,
988 } else if ( !BER_BVISNULL( &f2->f_sub_final )) {
992 rc = lex_bvcmp( &f1->f_mr_value,
1006 while ( f1->f_choice == LDAP_FILTER_AND || f1->f_choice == LDAP_FILTER_OR )
1008 while ( f2->f_choice == LDAP_FILTER_AND || f2->f_choice == LDAP_FILTER_OR )
1010 rc = pcache_filter_cmp( f1, f2 );
1018 /* compare filters in each query */
1019 static int pcache_query_cmp( const void *v1, const void *v2 )
1021 const CachedQuery *q1 = v1, *q2 =v2;
1022 return pcache_filter_cmp( q1->first, q2->first );
1025 /* add query on top of LRU list */
1027 add_query_on_top (query_manager* qm, CachedQuery* qc)
1029 CachedQuery* top = qm->lru_top;
1036 qm->lru_bottom = qc;
1040 Debug( pcache_debug, "Base of added query = %s\n",
1041 qc->qbase->base.bv_val, 0, 0 );
1044 /* remove_query from LRU list */
1047 remove_query (query_manager* qm, CachedQuery* qc)
1056 down = qc->lru_down;
1062 qm->lru_bottom = up;
1068 up->lru_down = down;
1070 qc->lru_up = qc->lru_down = NULL;
1073 /* find and remove string2 from string1
1074 * from start if position = 1,
1075 * from end if position = 3,
1076 * from anywhere if position = 2
1077 * string1 is overwritten if position = 2.
1081 find_and_remove(struct berval* ber1, struct berval* ber2, int position)
1085 if ( !ber2->bv_val )
1087 if ( !ber1->bv_val )
1090 switch( position ) {
1092 if ( ber1->bv_len >= ber2->bv_len && !memcmp( ber1->bv_val,
1093 ber2->bv_val, ber2->bv_len )) {
1095 ber1->bv_val += ber2->bv_len;
1096 ber1->bv_len -= ber2->bv_len;
1101 ber1->bv_val[ber1->bv_len] = '\0';
1102 temp = strstr( ber1->bv_val, ber2->bv_val );
1104 strcpy( temp, temp+ber2->bv_len );
1105 ber1->bv_len -= ber2->bv_len;
1111 if ( ber1->bv_len >= ber2->bv_len &&
1112 !memcmp( ber1->bv_val+ber1->bv_len-ber2->bv_len, ber2->bv_val,
1115 ber1->bv_len -= ber2->bv_len;
1123 static struct berval*
1124 merge_init_final(Operation *op, struct berval* init, struct berval* any,
1125 struct berval* final)
1127 struct berval* merged, *temp;
1128 int i, any_count, count;
1130 for (any_count=0; any && any[any_count].bv_val; any_count++)
1140 merged = (struct berval*)op->o_tmpalloc( (count+1)*sizeof(struct berval),
1145 ber_dupbv_x( temp, init, op->o_tmpmemctx );
1149 for (i=0; i<any_count; i++) {
1150 ber_dupbv_x( temp, any, op->o_tmpmemctx );
1155 ber_dupbv_x( temp, final, op->o_tmpmemctx );
1162 /* Each element in stored must be found in incoming. Incoming is overwritten.
1165 strings_containment(struct berval* stored, struct berval* incoming)
1167 struct berval* element;
1171 for ( element=stored; element->bv_val != NULL; element++ ) {
1172 for (j = k; incoming[j].bv_val != NULL; j++) {
1173 if (find_and_remove(&(incoming[j]), element, 2)) {
1190 substr_containment_substr(Operation *op, Filter* stored, Filter* incoming)
1194 struct berval init_incoming;
1195 struct berval final_incoming;
1196 struct berval *remaining_incoming = NULL;
1198 if ((!(incoming->f_sub_initial.bv_val) && (stored->f_sub_initial.bv_val))
1199 || (!(incoming->f_sub_final.bv_val) && (stored->f_sub_final.bv_val)))
1202 init_incoming = incoming->f_sub_initial;
1203 final_incoming = incoming->f_sub_final;
1205 if (find_and_remove(&init_incoming,
1206 &(stored->f_sub_initial), 1) && find_and_remove(&final_incoming,
1207 &(stored->f_sub_final), 3))
1209 if (stored->f_sub_any == NULL) {
1213 remaining_incoming = merge_init_final(op, &init_incoming,
1214 incoming->f_sub_any, &final_incoming);
1215 rc = strings_containment(stored->f_sub_any, remaining_incoming);
1216 ber_bvarray_free_x( remaining_incoming, op->o_tmpmemctx );
1223 substr_containment_equality(Operation *op, Filter* stored, Filter* incoming)
1225 struct berval incoming_val[2];
1228 incoming_val[1] = incoming->f_av_value;
1230 if (find_and_remove(incoming_val+1,
1231 &(stored->f_sub_initial), 1) && find_and_remove(incoming_val+1,
1232 &(stored->f_sub_final), 3)) {
1233 if (stored->f_sub_any == NULL){
1237 ber_dupbv_x( incoming_val, incoming_val+1, op->o_tmpmemctx );
1238 BER_BVZERO( incoming_val+1 );
1239 rc = strings_containment(stored->f_sub_any, incoming_val);
1240 op->o_tmpfree( incoming_val[0].bv_val, op->o_tmpmemctx );
1247 filter_first( Filter *f )
1249 while ( f->f_choice == LDAP_FILTER_OR || f->f_choice == LDAP_FILTER_AND )
1255 static CachedQuery *
1256 find_filter( Operation *op, Avlnode *root, Filter *inputf, Filter *first )
1260 MatchingRule* mrule = NULL;
1261 int res=0, eqpass= 0;
1264 CachedQuery cq, *qc;
1269 /* substring matches sort to the end, and we just have to
1270 * walk the entire list.
1272 if ( first->f_choice == LDAP_FILTER_SUBSTRINGS ) {
1273 ptr = tavl_end( root, 1 );
1274 dir = TAVL_DIR_LEFT;
1276 ptr = tavl_find3( root, &cq, pcache_query_cmp, &ret );
1277 dir = (first->f_choice == LDAP_FILTER_GE) ? TAVL_DIR_LEFT :
1286 /* an incoming substr query can only be satisfied by a cached
1289 if ( first->f_choice == LDAP_FILTER_SUBSTRINGS &&
1290 qc->first->f_choice != LDAP_FILTER_SUBSTRINGS )
1293 /* an incoming eq query can be satisfied by a cached eq or substr
1296 if ( first->f_choice == LDAP_FILTER_EQUALITY ) {
1297 if ( eqpass == 0 ) {
1298 if ( qc->first->f_choice != LDAP_FILTER_EQUALITY ) {
1299 nextpass: eqpass = 1;
1300 ptr = tavl_end( root, 1 );
1301 dir = TAVL_DIR_LEFT;
1305 if ( qc->first->f_choice != LDAP_FILTER_SUBSTRINGS )
1311 switch (fs->f_choice) {
1312 case LDAP_FILTER_EQUALITY:
1313 if (fi->f_choice == LDAP_FILTER_EQUALITY)
1314 mrule = fs->f_ava->aa_desc->ad_type->sat_equality;
1318 case LDAP_FILTER_GE:
1319 case LDAP_FILTER_LE:
1320 mrule = fs->f_ava->aa_desc->ad_type->sat_ordering;
1327 rc = value_match(&ret, fs->f_ava->aa_desc, mrule,
1328 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
1329 &(fi->f_ava->aa_value),
1330 &(fs->f_ava->aa_value), &text);
1331 if (rc != LDAP_SUCCESS) {
1334 if ( fi==first && fi->f_choice==LDAP_FILTER_EQUALITY && ret )
1337 switch (fs->f_choice) {
1338 case LDAP_FILTER_OR:
1339 case LDAP_FILTER_AND:
1344 case LDAP_FILTER_SUBSTRINGS:
1345 /* check if the equality query can be
1346 * answered with cached substring query */
1347 if ((fi->f_choice == LDAP_FILTER_EQUALITY)
1348 && substr_containment_equality( op,
1351 /* check if the substring query can be
1352 * answered with cached substring query */
1353 if ((fi->f_choice ==LDAP_FILTER_SUBSTRINGS
1354 ) && substr_containment_substr( op,
1360 case LDAP_FILTER_PRESENT:
1365 case LDAP_FILTER_EQUALITY:
1371 case LDAP_FILTER_GE:
1372 if (mrule && ret >= 0)
1377 case LDAP_FILTER_LE:
1378 if (mrule && ret <= 0)
1383 case LDAP_FILTER_NOT:
1389 } while((res) && (fi != NULL) && (fs != NULL));
1393 ptr = tavl_next( ptr, dir );
1398 /* check whether query is contained in any of
1399 * the cached queries in template
1401 static CachedQuery *
1402 query_containment(Operation *op, query_manager *qm,
1404 QueryTemplate *templa)
1407 int depth = 0, tscope;
1408 Qbase qbase, *qbptr = NULL;
1411 if (query->filter != NULL) {
1414 Debug( pcache_debug, "Lock QC index = %p\n",
1415 (void *) templa, 0, 0 );
1416 qbase.base = query->base;
1418 first = filter_first( query->filter );
1420 ldap_pvt_thread_rdwr_rlock(&templa->t_rwlock);
1423 qbptr = avl_find( templa->qbase, &qbase, pcache_dn_cmp );
1425 tscope = query->scope;
1426 /* Find a matching scope:
1427 * match at depth 0 OK
1430 * subord at depth > 0 OK
1431 * subtree at any depth OK
1433 * subtree or subord at any depth OK
1435 * subtree or subord at any depth OK
1437 * subord at depth > 0 OK
1438 * subtree at any depth OK
1440 for ( tscope = 0 ; tscope <= LDAP_SCOPE_CHILDREN; tscope++ ) {
1441 switch ( query->scope ) {
1442 case LDAP_SCOPE_BASE:
1443 if ( tscope == LDAP_SCOPE_BASE && depth ) continue;
1444 if ( tscope == LDAP_SCOPE_ONE && depth != 1) continue;
1445 if ( tscope == LDAP_SCOPE_CHILDREN && !depth ) continue;
1447 case LDAP_SCOPE_ONE:
1448 if ( tscope == LDAP_SCOPE_BASE )
1449 tscope = LDAP_SCOPE_ONE;
1450 if ( tscope == LDAP_SCOPE_ONE && depth ) continue;
1451 if ( !depth ) break;
1452 if ( tscope < LDAP_SCOPE_SUBTREE )
1453 tscope = LDAP_SCOPE_SUBTREE;
1455 case LDAP_SCOPE_SUBTREE:
1456 if ( tscope < LDAP_SCOPE_SUBTREE )
1457 tscope = LDAP_SCOPE_SUBTREE;
1458 if ( tscope == LDAP_SCOPE_CHILDREN && !depth ) continue;
1460 case LDAP_SCOPE_CHILDREN:
1461 if ( tscope < LDAP_SCOPE_SUBTREE )
1462 tscope = LDAP_SCOPE_SUBTREE;
1465 if ( !qbptr->scopes[tscope] ) continue;
1468 qc = find_filter( op, qbptr->scopes[tscope],
1469 query->filter, first );
1471 if ( qc->q_sizelimit ) {
1472 ldap_pvt_thread_rdwr_runlock(&templa->t_rwlock);
1475 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1476 if (qm->lru_top != qc) {
1477 remove_query(qm, qc);
1478 add_query_on_top(qm, qc);
1480 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1485 if ( be_issuffix( op->o_bd, &qbase.base ))
1488 dnParent( &qbase.base, &pdn );
1493 Debug( pcache_debug,
1494 "Not answerable: Unlock QC index=%p\n",
1495 (void *) templa, 0, 0 );
1496 ldap_pvt_thread_rdwr_runlock(&templa->t_rwlock);
1502 free_query (CachedQuery* qc)
1504 free(qc->q_uuid.bv_val);
1505 filter_free(qc->filter);
1506 ldap_pvt_thread_mutex_destroy(&qc->answerable_cnt_mutex);
1507 ldap_pvt_thread_rdwr_destroy( &qc->rwlock );
1508 memset(qc, 0, sizeof(*qc));
1513 /* Add query to query cache, the returned Query is locked for writing */
1514 static CachedQuery *
1519 QueryTemplate *templ,
1520 pc_caching_reason_t why,
1523 CachedQuery* new_cached_query = (CachedQuery*) ch_malloc(sizeof(CachedQuery));
1527 time_t ttl = 0, ttr = 0;
1530 new_cached_query->qtemp = templ;
1531 BER_BVZERO( &new_cached_query->q_uuid );
1532 new_cached_query->q_sizelimit = 0;
1534 now = slap_get_time();
1539 ttr = now + templ->ttr;
1543 ttl = templ->negttl;
1547 ttl = templ->limitttl;
1554 new_cached_query->expiry_time = now + ttl;
1555 new_cached_query->refresh_time = ttr;
1556 new_cached_query->bindref_time = 0;
1558 new_cached_query->bind_refcnt = 0;
1559 new_cached_query->answerable_cnt = 0;
1560 new_cached_query->refcnt = 1;
1561 ldap_pvt_thread_mutex_init(&new_cached_query->answerable_cnt_mutex);
1563 new_cached_query->lru_up = NULL;
1564 new_cached_query->lru_down = NULL;
1565 Debug( pcache_debug, "Added query expires at %ld (%s)\n",
1566 (long) new_cached_query->expiry_time,
1567 pc_caching_reason_str[ why ], 0 );
1569 new_cached_query->scope = query->scope;
1570 new_cached_query->filter = query->filter;
1571 new_cached_query->first = first = filter_first( query->filter );
1573 ldap_pvt_thread_rdwr_init(&new_cached_query->rwlock);
1575 ldap_pvt_thread_rdwr_wlock(&new_cached_query->rwlock);
1577 qb.base = query->base;
1579 /* Adding a query */
1580 Debug( pcache_debug, "Lock AQ index = %p\n",
1581 (void *) templ, 0, 0 );
1582 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
1583 qbase = avl_find( templ->qbase, &qb, pcache_dn_cmp );
1585 qbase = ch_calloc( 1, sizeof(Qbase) + qb.base.bv_len + 1 );
1586 qbase->base.bv_len = qb.base.bv_len;
1587 qbase->base.bv_val = (char *)(qbase+1);
1588 memcpy( qbase->base.bv_val, qb.base.bv_val, qb.base.bv_len );
1589 qbase->base.bv_val[qbase->base.bv_len] = '\0';
1590 avl_insert( &templ->qbase, qbase, pcache_dn_cmp, avl_dup_error );
1592 new_cached_query->next = templ->query;
1593 new_cached_query->prev = NULL;
1594 new_cached_query->qbase = qbase;
1595 rc = tavl_insert( &qbase->scopes[query->scope], new_cached_query,
1596 pcache_query_cmp, avl_dup_error );
1599 if (templ->query == NULL)
1600 templ->query_last = new_cached_query;
1602 templ->query->prev = new_cached_query;
1603 templ->query = new_cached_query;
1604 templ->no_of_queries++;
1606 ldap_pvt_thread_mutex_destroy(&new_cached_query->answerable_cnt_mutex);
1608 ldap_pvt_thread_rdwr_wunlock(&new_cached_query->rwlock);
1609 ldap_pvt_thread_rdwr_destroy( &new_cached_query->rwlock );
1610 ch_free( new_cached_query );
1611 new_cached_query = find_filter( op, qbase->scopes[query->scope],
1612 query->filter, first );
1613 filter_free( query->filter );
1614 query->filter = NULL;
1616 Debug( pcache_debug, "TEMPLATE %p QUERIES++ %d\n",
1617 (void *) templ, templ->no_of_queries, 0 );
1619 /* Adding on top of LRU list */
1621 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1622 add_query_on_top(qm, new_cached_query);
1623 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1625 Debug( pcache_debug, "Unlock AQ index = %p \n",
1626 (void *) templ, 0, 0 );
1627 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
1629 return rc == 0 ? new_cached_query : NULL;
1633 remove_from_template (CachedQuery* qc, QueryTemplate* template)
1635 if (!qc->prev && !qc->next) {
1636 template->query_last = template->query = NULL;
1637 } else if (qc->prev == NULL) {
1638 qc->next->prev = NULL;
1639 template->query = qc->next;
1640 } else if (qc->next == NULL) {
1641 qc->prev->next = NULL;
1642 template->query_last = qc->prev;
1644 qc->next->prev = qc->prev;
1645 qc->prev->next = qc->next;
1647 tavl_delete( &qc->qbase->scopes[qc->scope], qc, pcache_query_cmp );
1648 qc->qbase->queries--;
1649 if ( qc->qbase->queries == 0 ) {
1650 avl_delete( &template->qbase, qc->qbase, pcache_dn_cmp );
1651 ch_free( qc->qbase );
1655 template->no_of_queries--;
1658 /* remove bottom query of LRU list from the query cache */
1660 * NOTE: slight change in functionality.
1662 * - if result->bv_val is NULL, the query at the bottom of the LRU
1664 * - otherwise, the query whose UUID is *result is removed
1665 * - if not found, result->bv_val is zeroed
1668 cache_replacement(query_manager* qm, struct berval *result)
1670 CachedQuery* bottom;
1671 QueryTemplate *temp;
1673 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1674 if ( BER_BVISNULL( result ) ) {
1675 bottom = qm->lru_bottom;
1678 Debug ( pcache_debug,
1679 "Cache replacement invoked without "
1680 "any query in LRU list\n", 0, 0, 0 );
1681 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1686 for ( bottom = qm->lru_bottom;
1688 bottom = bottom->lru_up )
1690 if ( bvmatch( result, &bottom->q_uuid ) ) {
1696 Debug ( pcache_debug,
1697 "Could not find query with uuid=\"%s\""
1698 "in LRU list\n", result->bv_val, 0, 0 );
1699 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1700 BER_BVZERO( result );
1705 temp = bottom->qtemp;
1706 remove_query(qm, bottom);
1707 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1709 *result = bottom->q_uuid;
1710 BER_BVZERO( &bottom->q_uuid );
1712 Debug( pcache_debug, "Lock CR index = %p\n", (void *) temp, 0, 0 );
1713 ldap_pvt_thread_rdwr_wlock(&temp->t_rwlock);
1714 remove_from_template(bottom, temp);
1715 Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
1716 (void *) temp, temp->no_of_queries, 0 );
1717 Debug( pcache_debug, "Unlock CR index = %p\n", (void *) temp, 0, 0 );
1718 ldap_pvt_thread_rdwr_wunlock(&temp->t_rwlock);
1723 struct query_info *next;
1735 struct query_info *qi;
1738 if ( rs->sr_type != REP_SEARCH ) return 0;
1740 attr = attr_find( rs->sr_entry->e_attrs, ad_queryId );
1741 if ( attr == NULL ) return 0;
1743 count = attr->a_numvals;
1744 assert( count > 0 );
1745 qi = op->o_tmpalloc( sizeof( struct query_info ), op->o_tmpmemctx );
1746 qi->next = op->o_callback->sc_private;
1747 op->o_callback->sc_private = qi;
1748 ber_dupbv_x( &qi->xdn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1749 qi->del = ( count == 1 );
1757 struct berval *query_uuid )
1759 struct query_info *qi, *qnext;
1760 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(pcacheQueryID=)" ) ];
1761 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
1762 Filter filter = {LDAP_FILTER_EQUALITY};
1763 SlapReply sreply = {REP_RESULT};
1764 slap_callback cb = { NULL, remove_func, NULL, NULL };
1767 op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
1768 "(%s=%s)", ad_queryId->ad_cname.bv_val, query_uuid->bv_val);
1769 filter.f_ava = &ava;
1770 filter.f_av_desc = ad_queryId;
1771 filter.f_av_value = *query_uuid;
1773 op->o_tag = LDAP_REQ_SEARCH;
1774 op->o_protocol = LDAP_VERSION3;
1775 op->o_callback = &cb;
1776 op->o_time = slap_get_time();
1777 op->o_do_not_cache = 1;
1779 op->o_req_dn = op->o_bd->be_suffix[0];
1780 op->o_req_ndn = op->o_bd->be_nsuffix[0];
1781 op->ors_scope = LDAP_SCOPE_SUBTREE;
1782 op->ors_deref = LDAP_DEREF_NEVER;
1783 op->ors_slimit = SLAP_NO_LIMIT;
1784 op->ors_tlimit = SLAP_NO_LIMIT;
1785 op->ors_limit = NULL;
1786 op->ors_filter = &filter;
1787 op->ors_filterstr.bv_val = filter_str;
1788 op->ors_filterstr.bv_len = strlen(filter_str);
1789 op->ors_attrs = NULL;
1790 op->ors_attrsonly = 0;
1792 op->o_bd->be_search( op, &sreply );
1794 for ( qi=cb.sc_private; qi; qi=qnext ) {
1797 op->o_req_dn = qi->xdn;
1798 op->o_req_ndn = qi->xdn;
1799 rs_reinit( &sreply, REP_RESULT );
1802 Debug( pcache_debug, "DELETING ENTRY TEMPLATE=%s\n",
1803 query_uuid->bv_val, 0, 0 );
1805 op->o_tag = LDAP_REQ_DELETE;
1807 if (op->o_bd->be_delete(op, &sreply) == LDAP_SUCCESS) {
1813 struct berval vals[2];
1815 vals[0] = *query_uuid;
1816 vals[1].bv_val = NULL;
1818 mod.sml_op = LDAP_MOD_DELETE;
1820 mod.sml_desc = ad_queryId;
1821 mod.sml_type = ad_queryId->ad_cname;
1822 mod.sml_values = vals;
1823 mod.sml_nvalues = NULL;
1824 mod.sml_numvals = 1;
1825 mod.sml_next = NULL;
1826 Debug( pcache_debug,
1827 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
1828 query_uuid->bv_val, 0, 0 );
1830 op->orm_modlist = &mod;
1832 op->o_bd->be_modify( op, &sreply );
1834 op->o_tmpfree( qi->xdn.bv_val, op->o_tmpmemctx );
1835 op->o_tmpfree( qi, op->o_tmpmemctx );
1842 AttributeName* attrs,
1851 struct berval *fstr )
1853 AttributeDescription *ad;
1856 switch ( f->f_choice ) {
1857 case LDAP_FILTER_EQUALITY:
1859 len = STRLENOF( "(=)" ) + ad->ad_cname.bv_len;
1860 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=)", ad->ad_cname.bv_val );
1861 assert( ret == len );
1862 fstr->bv_len += len;
1865 case LDAP_FILTER_GE:
1867 len = STRLENOF( "(>=)" ) + ad->ad_cname.bv_len;
1868 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s>=)", ad->ad_cname.bv_val);
1869 assert( ret == len );
1870 fstr->bv_len += len;
1873 case LDAP_FILTER_LE:
1875 len = STRLENOF( "(<=)" ) + ad->ad_cname.bv_len;
1876 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s<=)", ad->ad_cname.bv_val);
1877 assert( ret == len );
1878 fstr->bv_len += len;
1881 case LDAP_FILTER_APPROX:
1883 len = STRLENOF( "(~=)" ) + ad->ad_cname.bv_len;
1884 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s~=)", ad->ad_cname.bv_val);
1885 assert( ret == len );
1886 fstr->bv_len += len;
1889 case LDAP_FILTER_SUBSTRINGS:
1891 len = STRLENOF( "(=)" ) + ad->ad_cname.bv_len;
1892 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=)", ad->ad_cname.bv_val );
1893 assert( ret == len );
1894 fstr->bv_len += len;
1897 case LDAP_FILTER_PRESENT:
1899 len = STRLENOF( "(=*)" ) + ad->ad_cname.bv_len;
1900 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=*)", ad->ad_cname.bv_val );
1901 assert( ret == len );
1902 fstr->bv_len += len;
1905 case LDAP_FILTER_AND:
1906 case LDAP_FILTER_OR:
1907 case LDAP_FILTER_NOT: {
1909 fstr->bv_val[fstr->bv_len++] = '(';
1910 switch ( f->f_choice ) {
1911 case LDAP_FILTER_AND:
1912 fstr->bv_val[fstr->bv_len] = '&';
1914 case LDAP_FILTER_OR:
1915 fstr->bv_val[fstr->bv_len] = '|';
1917 case LDAP_FILTER_NOT:
1918 fstr->bv_val[fstr->bv_len] = '!';
1923 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1924 rc = filter2template( op, f, fstr );
1927 fstr->bv_val[fstr->bv_len++] = ')';
1928 fstr->bv_val[fstr->bv_len] = '\0';
1934 /* a filter should at least have room for "()",
1935 * an "=" and for a 1-char attr */
1936 strcpy( fstr->bv_val, "(?=)" );
1937 fstr->bv_len += STRLENOF("(?=)");
1944 #define BI_HASHED 0x01
1945 #define BI_DIDCB 0x02
1946 #define BI_LOOKUP 0x04
1950 typedef struct bindinfo {
1951 cache_manager *bi_cm;
1953 QueryTemplate *bi_templ;
1954 struct search_info *bi_si;
1956 slap_callback bi_cb;
1959 struct search_info {
1962 QueryTemplate *qtemp;
1963 AttributeName* save_attrs; /* original attributes, saved for response */
1964 int swap_saved_attrs;
1969 int slimit_exceeded;
1970 pc_caching_reason_t caching_reason;
1976 remove_query_and_data(
1979 struct berval *uuid )
1981 query_manager* qm = cm->qm;
1983 qm->crfunc( qm, uuid );
1984 if ( !BER_BVISNULL( uuid ) ) {
1987 Debug( pcache_debug,
1988 "Removing query UUID %s\n",
1989 uuid->bv_val, 0, 0 );
1990 return_val = remove_query_data( op, uuid );
1991 Debug( pcache_debug,
1992 "QUERY REMOVED, SIZE=%d\n",
1994 ldap_pvt_thread_mutex_lock( &cm->cache_mutex );
1995 cm->cur_entries -= return_val;
1996 cm->num_cached_queries--;
1997 Debug( pcache_debug,
1998 "STORED QUERIES = %lu\n",
1999 cm->num_cached_queries, 0, 0 );
2000 ldap_pvt_thread_mutex_unlock( &cm->cache_mutex );
2001 Debug( pcache_debug,
2002 "QUERY REMOVED, CACHE ="
2004 cm->cur_entries, 0, 0 );
2009 * Callback used to fetch queryId values based on entryUUID;
2010 * used by pcache_remove_entries_from_cache()
2013 fetch_queryId_cb( Operation *op, SlapReply *rs )
2017 /* only care about searchEntry responses */
2018 if ( rs->sr_type != REP_SEARCH ) {
2022 /* allow only one response per entryUUID */
2023 if ( op->o_callback->sc_private != NULL ) {
2029 /* copy all queryId values into callback's private data */
2030 a = attr_find( rs->sr_entry->e_attrs, ad_queryId );
2032 BerVarray vals = NULL;
2034 ber_bvarray_dup_x( &vals, a->a_nvals, op->o_tmpmemctx );
2035 op->o_callback->sc_private = (void *)vals;
2039 /* clear entry if required */
2040 rs_flush_entry( op, rs, (slap_overinst *) op->o_bd->bd_info );
2046 * Call that allows to remove a set of entries from the cache,
2047 * by forcing the removal of all the related queries.
2050 pcache_remove_entries_from_cache(
2053 BerVarray entryUUIDs )
2055 Connection conn = { 0 };
2056 OperationBuffer opbuf;
2058 slap_callback sc = { 0 };
2060 char filtbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(entryUUID=)" ) ];
2061 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2062 AttributeName attrs[ 2 ] = {{{ 0 }}};
2066 void *thrctx = ldap_pvt_thread_pool_context();
2068 connection_fake_init( &conn, &opbuf, thrctx );
2076 memset( &op->oq_search, 0, sizeof( op->oq_search ) );
2077 op->ors_scope = LDAP_SCOPE_SUBTREE;
2078 op->ors_deref = LDAP_DEREF_NEVER;
2079 f.f_choice = LDAP_FILTER_EQUALITY;
2081 ava.aa_desc = slap_schema.si_ad_entryUUID;
2082 op->ors_filter = &f;
2084 op->ors_tlimit = SLAP_NO_LIMIT;
2085 op->ors_limit = NULL;
2086 attrs[ 0 ].an_desc = ad_queryId;
2087 attrs[ 0 ].an_name = ad_queryId->ad_cname;
2088 op->ors_attrs = attrs;
2089 op->ors_attrsonly = 0;
2091 op->o_req_dn = cm->db.be_suffix[ 0 ];
2092 op->o_req_ndn = cm->db.be_nsuffix[ 0 ];
2094 op->o_tag = LDAP_REQ_SEARCH;
2095 op->o_protocol = LDAP_VERSION3;
2096 op->o_managedsait = SLAP_CONTROL_CRITICAL;
2098 op->o_dn = op->o_bd->be_rootdn;
2099 op->o_ndn = op->o_bd->be_rootndn;
2100 sc.sc_response = fetch_queryId_cb;
2101 op->o_callback = ≻
2103 for ( s = 0; !BER_BVISNULL( &entryUUIDs[ s ] ); s++ ) {
2104 BerVarray vals = NULL;
2105 SlapReply rs = { REP_RESULT };
2107 op->ors_filterstr.bv_len = snprintf( filtbuf, sizeof( filtbuf ),
2108 "(entryUUID=%s)", entryUUIDs[ s ].bv_val );
2109 op->ors_filterstr.bv_val = filtbuf;
2110 ava.aa_value = entryUUIDs[ s ];
2112 rc = op->o_bd->be_search( op, &rs );
2113 if ( rc != LDAP_SUCCESS ) {
2117 vals = (BerVarray)op->o_callback->sc_private;
2118 if ( vals != NULL ) {
2121 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
2122 struct berval val = vals[ i ];
2124 remove_query_and_data( op, cm, &val );
2126 if ( !BER_BVISNULL( &val ) && val.bv_val != vals[ i ].bv_val ) {
2127 ch_free( val.bv_val );
2131 ber_bvarray_free_x( vals, op->o_tmpmemctx );
2132 op->o_callback->sc_private = NULL;
2140 * Call that allows to remove a query from the cache.
2143 pcache_remove_query_from_cache(
2146 struct berval *queryid )
2148 Operation op2 = *op;
2152 /* remove the selected query */
2153 remove_query_and_data( &op2, cm, queryid );
2155 return LDAP_SUCCESS;
2159 * Call that allows to remove a set of queries related to an entry
2160 * from the cache; if queryid is not null, the entry must belong to
2161 * the query indicated by queryid.
2164 pcache_remove_entry_queries_from_cache(
2168 struct berval *queryid )
2170 Connection conn = { 0 };
2171 OperationBuffer opbuf;
2173 slap_callback sc = { 0 };
2174 SlapReply rs = { REP_RESULT };
2176 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(pcacheQueryID=)" ) ];
2177 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2178 AttributeName attrs[ 2 ] = {{{ 0 }}};
2181 BerVarray vals = NULL;
2184 void *thrctx = ldap_pvt_thread_pool_context();
2186 connection_fake_init( &conn, &opbuf, thrctx );
2194 memset( &op->oq_search, 0, sizeof( op->oq_search ) );
2195 op->ors_scope = LDAP_SCOPE_BASE;
2196 op->ors_deref = LDAP_DEREF_NEVER;
2197 if ( queryid == NULL || BER_BVISNULL( queryid ) ) {
2198 BER_BVSTR( &op->ors_filterstr, "(objectClass=*)" );
2199 f.f_choice = LDAP_FILTER_PRESENT;
2200 f.f_desc = slap_schema.si_ad_objectClass;
2203 op->ors_filterstr.bv_len = snprintf( filter_str,
2204 sizeof( filter_str ), "(%s=%s)",
2205 ad_queryId->ad_cname.bv_val, queryid->bv_val );
2206 f.f_choice = LDAP_FILTER_EQUALITY;
2208 f.f_av_desc = ad_queryId;
2209 f.f_av_value = *queryid;
2211 op->ors_filter = &f;
2213 op->ors_tlimit = SLAP_NO_LIMIT;
2214 op->ors_limit = NULL;
2215 attrs[ 0 ].an_desc = ad_queryId;
2216 attrs[ 0 ].an_name = ad_queryId->ad_cname;
2217 op->ors_attrs = attrs;
2218 op->ors_attrsonly = 0;
2220 op->o_req_dn = *ndn;
2221 op->o_req_ndn = *ndn;
2223 op->o_tag = LDAP_REQ_SEARCH;
2224 op->o_protocol = LDAP_VERSION3;
2225 op->o_managedsait = SLAP_CONTROL_CRITICAL;
2227 op->o_dn = op->o_bd->be_rootdn;
2228 op->o_ndn = op->o_bd->be_rootndn;
2229 sc.sc_response = fetch_queryId_cb;
2230 op->o_callback = ≻
2232 rc = op->o_bd->be_search( op, &rs );
2233 if ( rc != LDAP_SUCCESS ) {
2237 vals = (BerVarray)op->o_callback->sc_private;
2238 if ( vals != NULL ) {
2241 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
2242 struct berval val = vals[ i ];
2244 remove_query_and_data( op, cm, &val );
2246 if ( !BER_BVISNULL( &val ) && val.bv_val != vals[ i ].bv_val ) {
2247 ch_free( val.bv_val );
2251 ber_bvarray_free_x( vals, op->o_tmpmemctx );
2254 return LDAP_SUCCESS;
2260 struct berval *query_uuid )
2262 struct search_info *si = op->o_callback->sc_private;
2263 slap_overinst *on = si->on;
2264 cache_manager *cm = on->on_bi.bi_private;
2267 struct berval crp_uuid;
2268 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
2270 Connection conn = {0};
2271 OperationBuffer opbuf;
2272 void *thrctx = ldap_pvt_thread_pool_context();
2274 query_uuid->bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf));
2275 ber_str2bv(uuidbuf, query_uuid->bv_len, 1, query_uuid);
2277 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
2278 op_tmp = &opbuf.ob_op;
2279 op_tmp->o_bd = &cm->db;
2280 op_tmp->o_dn = cm->db.be_rootdn;
2281 op_tmp->o_ndn = cm->db.be_rootndn;
2283 Debug( pcache_debug, "UUID for query being added = %s\n",
2286 for ( e=si->head; e; e=si->head ) {
2287 si->head = e->e_private;
2288 e->e_private = NULL;
2289 while ( cm->cur_entries > (cm->max_entries) ) {
2290 BER_BVZERO( &crp_uuid );
2291 remove_query_and_data( op_tmp, cm, &crp_uuid );
2294 return_val = merge_entry(op_tmp, e, 0, query_uuid);
2295 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2296 cm->cur_entries += return_val;
2297 Debug( pcache_debug,
2298 "ENTRY ADDED/MERGED, CACHED ENTRIES=%d\n",
2299 cm->cur_entries, 0, 0 );
2301 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2308 pcache_op_cleanup( Operation *op, SlapReply *rs ) {
2309 slap_callback *cb = op->o_callback;
2310 struct search_info *si = cb->sc_private;
2311 slap_overinst *on = si->on;
2312 cache_manager *cm = on->on_bi.bi_private;
2313 query_manager* qm = cm->qm;
2315 if ( rs->sr_type == REP_SEARCH ) {
2318 /* don't return more entries than requested by the client */
2319 if ( si->slimit > 0 && rs->sr_nentries >= si->slimit ) {
2320 si->slimit_exceeded = 1;
2323 /* If we haven't exceeded the limit for this query,
2324 * build a chain of answers to store. If we hit the
2325 * limit, empty the chain and ignore the rest.
2328 /* check if the entry contains undefined
2329 * attributes/objectClasses (ITS#5680) */
2330 if ( cm->check_cacheability && test_filter( op, rs->sr_entry, si->query.filter ) != LDAP_COMPARE_TRUE ) {
2331 Debug( pcache_debug, "%s: query not cacheable because of schema issues in DN \"%s\"\n",
2332 op->o_log_prefix, rs->sr_entry->e_name.bv_val, 0 );
2336 /* check for malformed entries: attrs with no values */
2338 Attribute *a = rs->sr_entry->e_attrs;
2339 for (; a; a=a->a_next) {
2340 if ( !a->a_numvals ) {
2341 Debug( pcache_debug, "%s: query not cacheable because of attrs without values in DN \"%s\" (%s)\n",
2342 op->o_log_prefix, rs->sr_entry->e_name.bv_val,
2343 a->a_desc->ad_cname.bv_val );
2349 if ( si->count < si->max ) {
2351 e = entry_dup( rs->sr_entry );
2352 if ( !si->head ) si->head = e;
2353 if ( si->tail ) si->tail->e_private = e;
2360 for (;si->head; si->head=e) {
2361 e = si->head->e_private;
2362 si->head->e_private = NULL;
2363 entry_free(si->head);
2371 if ( rs->sr_type == REP_RESULT ||
2372 op->o_abandon || rs->sr_err == SLAPD_ABANDON )
2374 if ( si->swap_saved_attrs ) {
2375 rs->sr_attrs = si->save_attrs;
2376 op->ors_attrs = si->save_attrs;
2378 if ( (op->o_abandon || rs->sr_err == SLAPD_ABANDON) &&
2379 si->caching_reason == PC_IGNORE )
2381 filter_free( si->query.filter );
2383 /* duplicate query, free it */
2385 for (;si->head; si->head=e) {
2386 e = si->head->e_private;
2387 si->head->e_private = NULL;
2388 entry_free(si->head);
2392 } else if ( si->caching_reason != PC_IGNORE ) {
2393 CachedQuery *qc = qm->addfunc(op, qm, &si->query,
2394 si->qtemp, si->caching_reason, 1 );
2397 switch ( si->caching_reason ) {
2399 cache_entries( op, &qc->q_uuid );
2402 si->pbi->bi_cq = qc;
2407 qc->q_sizelimit = rs->sr_nentries;
2417 ldap_pvt_thread_rdwr_wunlock(&qc->rwlock);
2418 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2419 cm->num_cached_queries++;
2420 Debug( pcache_debug, "STORED QUERIES = %lu\n",
2421 cm->num_cached_queries, 0, 0 );
2422 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2424 /* If the consistency checker suspended itself,
2427 if ( cm->cc_paused == PCACHE_CC_PAUSED ) {
2428 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2429 if ( cm->cc_paused == PCACHE_CC_PAUSED ) {
2431 ldap_pvt_runqueue_resched( &slapd_rq, cm->cc_arg, 0 );
2433 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2436 } else if ( si->count ) {
2437 /* duplicate query, free it */
2439 for (;si->head; si->head=e) {
2440 e = si->head->e_private;
2441 si->head->e_private = NULL;
2442 entry_free(si->head);
2447 filter_free( si->query.filter );
2450 op->o_callback = op->o_callback->sc_next;
2451 op->o_tmpfree( cb, op->o_tmpmemctx );
2454 return SLAP_CB_CONTINUE;
2462 struct search_info *si = op->o_callback->sc_private;
2464 if ( si->swap_saved_attrs ) {
2465 rs->sr_attrs = si->save_attrs;
2466 op->ors_attrs = si->save_attrs;
2469 if ( rs->sr_type == REP_SEARCH ) {
2470 /* don't return more entries than requested by the client */
2471 if ( si->slimit_exceeded ) {
2475 } else if ( rs->sr_type == REP_RESULT ) {
2478 if ( rs->sr_err == LDAP_SUCCESS ) {
2479 si->caching_reason = PC_POSITIVE;
2481 } else if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED
2482 && si->qtemp->limitttl )
2486 si->caching_reason = PC_SIZELIMIT;
2487 for (;si->head; si->head=e) {
2488 e = si->head->e_private;
2489 si->head->e_private = NULL;
2490 entry_free(si->head);
2494 } else if ( si->qtemp->negttl && !si->count && !si->over &&
2495 rs->sr_err == LDAP_SUCCESS )
2497 si->caching_reason = PC_NEGATIVE;
2501 if ( si->slimit_exceeded ) {
2502 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
2506 return SLAP_CB_CONTINUE;
2509 /* NOTE: this is a quick workaround to let pcache minimally interact
2510 * with pagedResults. A more articulated solutions would be to
2511 * perform the remote query without control and cache all results,
2512 * performing the pagedResults search only within the client
2513 * and the proxy. This requires pcache to understand pagedResults. */
2515 pcache_chk_controls(
2519 const char *non = "";
2520 const char *stripped = "";
2522 switch( op->o_pagedresults ) {
2523 case SLAP_CONTROL_NONCRITICAL:
2525 stripped = "; stripped";
2528 case SLAP_CONTROL_CRITICAL:
2529 Debug( pcache_debug, "%s: "
2530 "%scritical pagedResults control "
2531 "disabled with proxy cache%s.\n",
2532 op->o_log_prefix, non, stripped );
2534 slap_remove_control( op, rs, slap_cids.sc_pagedResults, NULL );
2538 rs->sr_err = SLAP_CB_CONTINUE;
2546 pc_setpw( Operation *op, struct berval *pwd, cache_manager *cm )
2548 struct berval vals[2];
2551 const char *text = NULL;
2552 BER_BVZERO( &vals[0] );
2553 slap_passwd_hash( pwd, &vals[0], &text );
2554 if ( BER_BVISEMPTY( &vals[0] )) {
2555 Debug( pcache_debug, "pc_setpw: hash failed %s\n",
2561 BER_BVZERO( &vals[1] );
2565 SlapReply sr = { REP_RESULT };
2566 slap_callback cb = { 0, slap_null_cb, 0, 0 };
2569 mod.sml_op = LDAP_MOD_REPLACE;
2571 mod.sml_desc = slap_schema.si_ad_userPassword;
2572 mod.sml_type = mod.sml_desc->ad_cname;
2573 mod.sml_values = vals;
2574 mod.sml_nvalues = NULL;
2575 mod.sml_numvals = 1;
2576 mod.sml_next = NULL;
2578 op->o_tag = LDAP_REQ_MODIFY;
2579 op->orm_modlist = &mod;
2581 op->o_dn = op->o_bd->be_rootdn;
2582 op->o_ndn = op->o_bd->be_rootndn;
2583 op->o_callback = &cb;
2584 Debug( pcache_debug, "pc_setpw: CACHING BIND for %s\n",
2585 op->o_req_dn.bv_val, 0, 0 );
2586 rc = op->o_bd->be_modify( op, &sr );
2587 ch_free( vals[0].bv_val );
2592 typedef struct bindcacheinfo {
2598 pc_bind_save( Operation *op, SlapReply *rs )
2600 if ( rs->sr_err == LDAP_SUCCESS ) {
2601 bindcacheinfo *bci = op->o_callback->sc_private;
2602 slap_overinst *on = bci->on;
2603 cache_manager *cm = on->on_bi.bi_private;
2604 CachedQuery *qc = bci->qc;
2607 ldap_pvt_thread_rdwr_wlock( &qc->rwlock );
2608 if ( qc->bind_refcnt-- ) {
2609 Operation op2 = *op;
2610 if ( pc_setpw( &op2, &op->orb_cred, cm ) == LDAP_SUCCESS )
2611 bci->qc->bindref_time = op->o_time + bci->qc->qtemp->bindttr;
2616 ldap_pvt_thread_rdwr_wunlock( &qc->rwlock );
2617 if ( delete ) free_query(qc);
2619 return SLAP_CB_CONTINUE;
2623 pc_bind_attrs( Operation *op, Entry *e, QueryTemplate *temp,
2624 struct berval *fbv )
2627 struct berval *vals, pres = BER_BVC("*");
2631 vals = op->o_tmpalloc( temp->bindnattrs * sizeof( struct berval ),
2634 for ( i=0; i<temp->bindnattrs; i++ ) {
2635 a = attr_find( e->e_attrs, temp->bindfattrs[i] );
2636 if ( a && a->a_vals ) {
2637 vals[i] = a->a_vals[0];
2638 len += a->a_vals[0].bv_len;
2643 fbv->bv_len = len + temp->bindftemp.bv_len;
2644 fbv->bv_val = op->o_tmpalloc( fbv->bv_len + 1, op->o_tmpmemctx );
2646 p1 = temp->bindftemp.bv_val;
2651 if ( p1[0] == '=' && p1[1] == ')' ) {
2652 AC_MEMCPY( p2, vals[i].bv_val, vals[i].bv_len );
2653 p2 += vals[i].bv_len;
2659 op->o_tmpfree( vals, op->o_tmpmemctx );
2661 /* FIXME: are we sure str2filter_x can't fail?
2662 * caller needs to check */
2664 Filter *f = str2filter_x( op, fbv->bv_val );
2665 assert( f != NULL );
2670 /* Check if the requested entry is from the cache and has a valid
2671 * ttr and password hash
2674 pc_bind_search( Operation *op, SlapReply *rs )
2676 if ( rs->sr_type == REP_SEARCH ) {
2677 bindinfo *pbi = op->o_callback->sc_private;
2679 /* We only care if this is an already cached result and we're
2680 * below the refresh time, or we're offline.
2683 if (( pbi->bi_cm->cc_paused & PCACHE_CC_OFFLINE ) ||
2684 op->o_time < pbi->bi_cq->bindref_time ) {
2687 /* See if a recognized password is hashed here */
2688 a = attr_find( rs->sr_entry->e_attrs,
2689 slap_schema.si_ad_userPassword );
2690 if ( a && a->a_vals[0].bv_val[0] == '{' &&
2691 lutil_passwd_scheme( a->a_vals[0].bv_val ))
2692 pbi->bi_flags |= BI_HASHED;
2694 Debug( pcache_debug, "pc_bind_search: cache is stale, "
2695 "reftime: %ld, current time: %ld\n",
2696 pbi->bi_cq->bindref_time, op->o_time, 0 );
2698 } else if ( pbi->bi_si ) {
2699 /* This search result is going into the cache */
2703 filter_free( pbi->bi_si->query.filter );
2704 f = pc_bind_attrs( op, rs->sr_entry, pbi->bi_templ, &fbv );
2705 op->o_tmpfree( fbv.bv_val, op->o_tmpmemctx );
2706 pbi->bi_si->query.filter = filter_dup( f, NULL );
2707 filter_free_x( op, f, 1 );
2713 /* We always want pc_bind_search to run after the search handlers */
2715 pc_bind_resp( Operation *op, SlapReply *rs )
2717 bindinfo *pbi = op->o_callback->sc_private;
2718 if ( !( pbi->bi_flags & BI_DIDCB )) {
2719 slap_callback *sc = op->o_callback;
2720 while ( sc && sc->sc_response != pcache_response )
2723 sc = op->o_callback;
2724 pbi->bi_cb.sc_next = sc->sc_next;
2725 sc->sc_next = &pbi->bi_cb;
2726 pbi->bi_flags |= BI_DIDCB;
2728 return SLAP_CB_CONTINUE;
2731 #ifdef PCACHE_CONTROL_PRIVDB
2737 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2738 cache_manager *cm = on->on_bi.bi_private;
2739 slap_callback *save_cb;
2742 /* skip if control is unset */
2743 if ( op->o_ctrlflag[ privDB_cid ] != SLAP_CONTROL_CRITICAL ) {
2744 return SLAP_CB_CONTINUE;
2747 /* The cache DB isn't open yet */
2748 if ( cm->defer_db_open ) {
2749 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
2750 "pcachePrivDB: cacheDB not available" );
2754 /* FIXME: might be a little bit exaggerated... */
2755 if ( !be_isroot( op ) ) {
2756 save_cb = op->o_callback;
2757 op->o_callback = NULL;
2758 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
2759 "pcachePrivDB: operation not allowed" );
2760 op->o_callback = save_cb;
2765 /* map tag to operation */
2766 type = slap_req2op( op->o_tag );
2767 if ( type != SLAP_OP_LAST ) {
2771 /* execute, if possible */
2772 func = &cm->db.be_bind;
2773 if ( func[ type ] != NULL ) {
2774 Operation op2 = *op;
2778 rc = func[ type ]( &op2, rs );
2779 if ( type == SLAP_OP_BIND && rc == LDAP_SUCCESS ) {
2780 op->o_conn->c_authz_cookie = cm->db.be_private;
2787 /* otherwise fall back to error */
2788 save_cb = op->o_callback;
2789 op->o_callback = NULL;
2790 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
2791 "operation not supported with pcachePrivDB control" );
2792 op->o_callback = save_cb;
2796 #endif /* PCACHE_CONTROL_PRIVDB */
2803 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2804 cache_manager *cm = on->on_bi.bi_private;
2805 QueryTemplate *temp;
2807 slap_callback cb = { 0 }, *sc;
2813 #ifdef PCACHE_CONTROL_PRIVDB
2814 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL )
2815 return pcache_op_privdb( op, rs );
2816 #endif /* PCACHE_CONTROL_PRIVDB */
2818 /* Skip if we're not configured for Binds, or cache DB isn't open yet */
2819 if ( !cm->cache_binds || cm->defer_db_open )
2820 return SLAP_CB_CONTINUE;
2822 /* First find a matching template with Bind info */
2823 for ( temp=cm->qm->templates; temp; temp=temp->qmnext ) {
2824 if ( temp->bindttr && dnIsSuffix( &op->o_req_ndn, &temp->bindbase ))
2827 /* Didn't find a suitable template, just passthru */
2829 return SLAP_CB_CONTINUE;
2831 /* See if the entry is already locally cached. If so, we can
2832 * populate the query filter to retrieve the cached query. We
2833 * need to check the bindrefresh time in the query.
2836 op2.o_dn = op->o_bd->be_rootdn;
2837 op2.o_ndn = op->o_bd->be_rootndn;
2842 rc = be_entry_get_rw( &op2, &op->o_req_ndn, NULL, NULL, 0, &e );
2843 if ( rc == LDAP_SUCCESS && e ) {
2844 bi.bi_flags |= BI_LOOKUP;
2845 op2.ors_filter = pc_bind_attrs( op, e, temp, &op2.ors_filterstr );
2846 be_entry_release_r( &op2, e );
2848 op2.ors_filter = temp->bindfilter;
2849 op2.ors_filterstr = temp->bindfilterstr;
2852 op2.o_bd = op->o_bd;
2853 op2.o_tag = LDAP_REQ_SEARCH;
2854 op2.ors_scope = LDAP_SCOPE_BASE;
2855 op2.ors_deref = LDAP_DEREF_NEVER;
2857 op2.ors_tlimit = SLAP_NO_LIMIT;
2858 op2.ors_limit = NULL;
2859 op2.ors_attrs = cm->qm->attr_sets[temp->attr_set_index].attrs;
2860 op2.ors_attrsonly = 0;
2862 /* We want to invoke search at the same level of the stack
2863 * as we're already at...
2870 bi.bi_cb.sc_response = pc_bind_search;
2871 bi.bi_cb.sc_cleanup = NULL;
2872 bi.bi_cb.sc_private = &bi;
2873 cb.sc_private = &bi;
2874 cb.sc_response = pc_bind_resp;
2875 op2.o_callback = &cb;
2876 overlay_op_walk( &op2, rs, op_search, on->on_info, on );
2878 /* OK, just bind locally */
2879 if ( bi.bi_flags & BI_HASHED ) {
2880 BackendDB *be = op->o_bd;
2884 Debug( pcache_debug, "pcache_op_bind: CACHED BIND for %s\n",
2885 op->o_req_dn.bv_val, 0, 0 );
2887 if ( op->o_bd->be_bind( op, rs ) == LDAP_SUCCESS ) {
2888 op->o_conn->c_authz_cookie = cm->db.be_private;
2891 ldap_pvt_thread_rdwr_wlock( &bi.bi_cq->rwlock );
2892 if ( !bi.bi_cq->bind_refcnt-- ) {
2895 ldap_pvt_thread_rdwr_wunlock( &bi.bi_cq->rwlock );
2896 if ( delete ) free_query( bi.bi_cq );
2900 /* We have a cached query to work with */
2902 sc = op->o_tmpalloc( sizeof(slap_callback) + sizeof(bindcacheinfo),
2904 sc->sc_response = pc_bind_save;
2905 sc->sc_cleanup = NULL;
2906 sc->sc_private = sc+1;
2907 bci = sc->sc_private;
2908 sc->sc_next = op->o_callback;
2909 op->o_callback = sc;
2913 return SLAP_CB_CONTINUE;
2916 static slap_response refresh_merge;
2923 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2924 cache_manager *cm = on->on_bi.bi_private;
2925 query_manager* qm = cm->qm;
2930 QueryTemplate *qtemp = NULL;
2931 bindinfo *pbi = NULL;
2934 CachedQuery *answerable = NULL;
2937 struct berval tempstr;
2939 #ifdef PCACHE_CONTROL_PRIVDB
2940 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL ) {
2941 return pcache_op_privdb( op, rs );
2943 #endif /* PCACHE_CONTROL_PRIVDB */
2945 /* The cache DB isn't open yet */
2946 if ( cm->defer_db_open ) {
2947 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
2948 "pcachePrivDB: cacheDB not available" );
2952 /* pickup runtime ACL changes */
2953 cm->db.be_acl = op->o_bd->be_acl;
2956 /* See if we're processing a Bind request
2957 * or a cache refresh */
2958 slap_callback *cb = op->o_callback;
2960 for ( ; cb; cb=cb->sc_next ) {
2961 if ( cb->sc_response == pc_bind_resp ) {
2962 pbi = cb->sc_private;
2965 if ( cb->sc_response == refresh_merge ) {
2966 /* This is a refresh, do not search the cache */
2967 return SLAP_CB_CONTINUE;
2972 /* FIXME: cannot cache/answer requests with pagedResults control */
2974 query.filter = op->ors_filter;
2977 query.base = pbi->bi_templ->bindbase;
2978 query.scope = pbi->bi_templ->bindscope;
2979 attr_set = pbi->bi_templ->attr_set_index;
2981 qtemp = pbi->bi_templ;
2982 if ( pbi->bi_flags & BI_LOOKUP )
2983 answerable = qm->qcfunc(op, qm, &query, qtemp);
2986 tempstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len+1,
2989 if ( filter2template( op, op->ors_filter, &tempstr ))
2991 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
2992 return SLAP_CB_CONTINUE;
2995 Debug( pcache_debug, "query template of incoming query = %s\n",
2996 tempstr.bv_val, 0, 0 );
2999 attr_set = get_attr_set(op->ors_attrs, qm, cm->numattrsets);
3001 query.base = op->o_req_ndn;
3002 query.scope = op->ors_scope;
3004 /* check for query containment */
3005 if (attr_set > -1) {
3006 QueryTemplate *qt = qm->attr_sets[attr_set].templates;
3007 for (; qt; qt = qt->qtnext ) {
3008 /* find if template i can potentially answer tempstr */
3009 if ( ber_bvstrcasecmp( &qt->querystr, &tempstr ) != 0 )
3013 Debug( pcache_debug, "Entering QC, querystr = %s\n",
3014 op->ors_filterstr.bv_val, 0, 0 );
3015 answerable = qm->qcfunc(op, qm, &query, qt);
3017 /* if != NULL, rlocks qtemp->t_rwlock */
3022 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
3026 BackendDB *save_bd = op->o_bd;
3028 ldap_pvt_thread_mutex_lock( &answerable->answerable_cnt_mutex );
3029 answerable->answerable_cnt++;
3030 /* we only care about refcnts if we're refreshing */
3031 if ( answerable->refresh_time )
3032 answerable->refcnt++;
3033 Debug( pcache_debug, "QUERY ANSWERABLE (answered %lu times)\n",
3034 answerable->answerable_cnt, 0, 0 );
3035 ldap_pvt_thread_mutex_unlock( &answerable->answerable_cnt_mutex );
3037 ldap_pvt_thread_rdwr_wlock(&answerable->rwlock);
3038 if ( BER_BVISNULL( &answerable->q_uuid )) {
3039 /* No entries cached, just an empty result set */
3041 send_ldap_result( op, rs );
3043 /* Let Bind know we used a cached query */
3045 answerable->bind_refcnt++;
3046 pbi->bi_cq = answerable;
3050 if ( cm->response_cb == PCACHE_RESPONSE_CB_TAIL ) {
3052 /* The cached entry was already processed by any
3053 * other overlays, so don't let it get processed again.
3055 * This loop removes over_back_response from the stack.
3057 if ( overlay_callback_after_backover( op, &cb, 0) == 0 ) {
3058 slap_callback **scp;
3059 for ( scp = &op->o_callback; *scp != NULL;
3060 scp = &(*scp)->sc_next ) {
3061 if ( (*scp)->sc_next == &cb ) {
3068 i = cm->db.bd_info->bi_op_search( op, rs );
3070 ldap_pvt_thread_rdwr_wunlock(&answerable->rwlock);
3071 /* locked by qtemp->qcfunc (query_containment) */
3072 ldap_pvt_thread_rdwr_runlock(&qtemp->t_rwlock);
3077 Debug( pcache_debug, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
3079 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
3080 if (cm->num_cached_queries >= cm->max_queries) {
3083 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
3085 if (op->ors_attrsonly)
3090 struct search_info *si;
3092 Debug( pcache_debug, "QUERY CACHEABLE\n", 0, 0, 0 );
3093 query.filter = filter_dup(op->ors_filter, NULL);
3095 cb = op->o_tmpalloc( sizeof(*cb) + sizeof(*si), op->o_tmpmemctx );
3096 cb->sc_response = pcache_response;
3097 cb->sc_cleanup = pcache_op_cleanup;
3098 cb->sc_private = (cb+1);
3099 si = cb->sc_private;
3103 si->max = cm->num_entries_limit ;
3107 si->slimit_exceeded = 0;
3108 si->caching_reason = PC_IGNORE;
3109 if ( op->ors_slimit > 0 && op->ors_slimit < cm->num_entries_limit ) {
3110 si->slimit = op->ors_slimit;
3111 op->ors_slimit = cm->num_entries_limit;
3115 si->swap_saved_attrs = 1;
3116 si->save_attrs = op->ors_attrs;
3121 op->ors_attrs = qtemp->t_attrs.attrs;
3123 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
3124 cb->sc_next = op->o_callback;
3125 op->o_callback = cb;
3128 slap_callback **pcb;
3130 /* need to move the callback at the end, in case other
3131 * overlays are present, so that the final entry is
3132 * actually cached */
3134 for ( pcb = &op->o_callback; *pcb; pcb = &(*pcb)->sc_next );
3139 Debug( pcache_debug, "QUERY NOT CACHEABLE\n",
3143 return SLAP_CB_CONTINUE;
3148 AttributeName* attrs,
3156 for ( ; attrs[i].an_name.bv_val; i++ ) {
3157 /* only count valid attribute names
3158 * (searches ignore others, this overlay does the same) */
3159 if ( attrs[i].an_desc ) {
3165 /* recognize default or explicit single "*" */
3167 ( i == 1 && bvmatch( &attrs[0].an_name, slap_bv_all_user_attrs ) ) )
3170 attrs = slap_anlist_all_user_attributes;
3172 /* recognize implicit (no valid attributes) or explicit single "1.1" */
3173 } else if ( count == 0 ||
3174 ( i == 1 && bvmatch( &attrs[0].an_name, slap_bv_no_attrs ) ) )
3180 for ( i = 0; i < num; i++ ) {
3184 if ( count > qm->attr_sets[i].count ) {
3189 if ( !qm->attr_sets[i].count ) {
3195 for ( a2 = attrs; a2->an_name.bv_val; a2++ ) {
3196 if ( !a2->an_desc && !bvmatch( &a2->an_name, slap_bv_all_user_attrs ) ) continue;
3198 if ( !an_find( qm->attr_sets[i].attrs, &a2->an_name ) ) {
3216 /* Refresh a cached query:
3217 * 1: Replay the query on the remote DB and merge each entry into
3218 * the local DB. Remember the DNs of each remote entry.
3219 * 2: Search the local DB for all entries matching this queryID.
3220 * Delete any entry whose DN is not in the list from (1).
3222 typedef struct dnlist {
3223 struct dnlist *next;
3228 typedef struct refresh_info {
3236 static dnlist *dnl_alloc( Operation *op, struct berval *bvdn )
3238 dnlist *dn = op->o_tmpalloc( sizeof(dnlist) + bvdn->bv_len + 1,
3240 dn->dn.bv_len = bvdn->bv_len;
3241 dn->dn.bv_val = (char *)(dn+1);
3242 AC_MEMCPY( dn->dn.bv_val, bvdn->bv_val, dn->dn.bv_len );
3243 dn->dn.bv_val[dn->dn.bv_len] = '\0';
3248 refresh_merge( Operation *op, SlapReply *rs )
3250 if ( rs->sr_type == REP_SEARCH ) {
3251 refresh_info *ri = op->o_callback->sc_private;
3257 ocb = op->o_callback;
3258 /* Find local entry, merge */
3259 op->o_bd = ri->ri_be;
3260 rc = be_entry_get_rw( op, &rs->sr_entry->e_nname, NULL, NULL, 0, &e );
3261 if ( rc != LDAP_SUCCESS || e == NULL ) {
3262 /* No local entry, just add it. FIXME: we are not checking
3263 * the cache entry limit here
3265 merge_entry( op, rs->sr_entry, 1, &ri->ri_q->q_uuid );
3267 /* Entry exists, update it */
3270 Modifications *modlist, *mods = NULL;
3271 const char* text = NULL;
3272 char textbuf[SLAP_TEXT_BUFLEN];
3273 size_t textlen = sizeof(textbuf);
3274 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
3278 /* Get a copy of only the attrs we requested */
3279 for ( a=e->e_attrs; a; a=a->a_next ) {
3280 if ( ad_inlist( a->a_desc, rs->sr_attrs )) {
3281 *b = attr_alloc( a->a_desc );
3283 /* The actual values still belong to e */
3284 (*b)->a_flags |= SLAP_ATTR_DONT_FREE_VALS |
3285 SLAP_ATTR_DONT_FREE_DATA;
3286 b = &((*b)->a_next);
3290 slap_entry2mods( rs->sr_entry, &modlist, &text, textbuf, textlen );
3291 syncrepl_diff_entry( op, ne.e_attrs, rs->sr_entry->e_attrs,
3292 &mods, &modlist, 0 );
3293 be_entry_release_r( op, e );
3294 attrs_free( ne.e_attrs );
3295 slap_mods_free( modlist, 1 );
3296 /* mods is NULL if there are no changes */
3298 SlapReply rs2 = { REP_RESULT };
3299 struct berval dn = op->o_req_dn;
3300 struct berval ndn = op->o_req_ndn;
3301 op->o_tag = LDAP_REQ_MODIFY;
3302 op->orm_modlist = mods;
3303 op->o_req_dn = rs->sr_entry->e_name;
3304 op->o_req_ndn = rs->sr_entry->e_nname;
3305 op->o_callback = &cb;
3306 op->o_bd->be_modify( op, &rs2 );
3307 rs->sr_err = rs2.sr_err;
3308 rs_assert_done( &rs2 );
3309 slap_mods_free( mods, 1 );
3311 op->o_req_ndn = ndn;
3315 /* Add DN to list */
3316 dnl = dnl_alloc( op, &rs->sr_entry->e_nname );
3318 if ( ri->ri_tail ) {
3319 ri->ri_tail->next = dnl;
3324 op->o_callback = ocb;
3330 refresh_purge( Operation *op, SlapReply *rs )
3332 if ( rs->sr_type == REP_SEARCH ) {
3333 refresh_info *ri = op->o_callback->sc_private;
3337 /* Did the entry exist on the remote? */
3338 for ( dn=&ri->ri_dns; *dn; dn = &(*dn)->next ) {
3339 if ( dn_match( &(*dn)->dn, &rs->sr_entry->e_nname )) {
3340 dnlist *dnext = (*dn)->next;
3341 op->o_tmpfree( *dn, op->o_tmpmemctx );
3347 /* No, so put it on the list to delete */
3350 dnlist *dnl = dnl_alloc( op, &rs->sr_entry->e_nname );
3351 dnl->next = ri->ri_dels;
3353 a = attr_find( rs->sr_entry->e_attrs, ad_queryId );
3354 /* If ours is the only queryId, delete entry */
3355 dnl->delete = ( a->a_numvals == 1 );
3362 refresh_query( Operation *op, CachedQuery *query, slap_overinst *on )
3364 SlapReply rs = {REP_RESULT};
3365 slap_callback cb = { 0 };
3366 refresh_info ri = { 0 };
3367 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(pcacheQueryID=)" ) ];
3368 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
3369 Filter filter = {LDAP_FILTER_EQUALITY};
3370 AttributeName attrs[ 2 ] = {{{ 0 }}};
3374 ldap_pvt_thread_mutex_lock( &query->answerable_cnt_mutex );
3376 ldap_pvt_thread_mutex_unlock( &query->answerable_cnt_mutex );
3378 cb.sc_response = refresh_merge;
3379 cb.sc_private = &ri;
3382 ri.ri_be = op->o_bd;
3385 op->o_tag = LDAP_REQ_SEARCH;
3386 op->o_protocol = LDAP_VERSION3;
3387 op->o_callback = &cb;
3388 op->o_do_not_cache = 1;
3390 op->o_req_dn = query->qbase->base;
3391 op->o_req_ndn = query->qbase->base;
3392 op->ors_scope = query->scope;
3393 op->ors_slimit = SLAP_NO_LIMIT;
3394 op->ors_tlimit = SLAP_NO_LIMIT;
3395 op->ors_limit = NULL;
3396 op->ors_filter = query->filter;
3397 filter2bv_x( op, query->filter, &op->ors_filterstr );
3398 op->ors_attrs = query->qtemp->t_attrs.attrs;
3399 op->ors_attrsonly = 0;
3401 op->o_bd = on->on_info->oi_origdb;
3402 rc = op->o_bd->be_search( op, &rs );
3404 op->o_bd = ri.ri_be;
3408 /* Get the DNs of all entries matching this query */
3409 cb.sc_response = refresh_purge;
3411 op->o_bd = ri.ri_be;
3412 op->o_req_dn = op->o_bd->be_suffix[0];
3413 op->o_req_ndn = op->o_bd->be_nsuffix[0];
3414 op->ors_scope = LDAP_SCOPE_SUBTREE;
3415 op->ors_deref = LDAP_DEREF_NEVER;
3416 op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
3417 "(%s=%s)", ad_queryId->ad_cname.bv_val, query->q_uuid.bv_val);
3418 filter.f_ava = &ava;
3419 filter.f_av_desc = ad_queryId;
3420 filter.f_av_value = query->q_uuid;
3421 attrs[ 0 ].an_desc = ad_queryId;
3422 attrs[ 0 ].an_name = ad_queryId->ad_cname;
3423 op->ors_attrs = attrs;
3424 op->ors_attrsonly = 0;
3425 rs_reinit( &rs, REP_RESULT );
3426 rc = op->o_bd->be_search( op, &rs );
3427 if ( rc ) goto leave;
3429 while (( dn = ri.ri_dels )) {
3430 op->o_req_dn = dn->dn;
3431 op->o_req_ndn = dn->dn;
3432 rs_reinit( &rs, REP_RESULT );
3434 op->o_tag = LDAP_REQ_DELETE;
3435 op->o_bd->be_delete( op, &rs );
3438 struct berval vals[2];
3440 vals[0] = query->q_uuid;
3441 BER_BVZERO( &vals[1] );
3442 mod.sml_op = LDAP_MOD_DELETE;
3444 mod.sml_desc = ad_queryId;
3445 mod.sml_type = ad_queryId->ad_cname;
3446 mod.sml_values = vals;
3447 mod.sml_nvalues = NULL;
3448 mod.sml_numvals = 1;
3449 mod.sml_next = NULL;
3451 op->o_tag = LDAP_REQ_MODIFY;
3452 op->orm_modlist = &mod;
3453 op->o_bd->be_modify( op, &rs );
3455 ri.ri_dels = dn->next;
3456 op->o_tmpfree( dn, op->o_tmpmemctx );
3460 /* reset our local heap, we're done with it */
3461 slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, op->o_threadctx, 1 );
3470 struct re_s *rtask = arg;
3471 slap_overinst *on = rtask->arg;
3472 cache_manager *cm = on->on_bi.bi_private;
3473 query_manager *qm = cm->qm;
3474 Connection conn = {0};
3475 OperationBuffer opbuf;
3478 CachedQuery *query, *qprev;
3479 int return_val, pause = PCACHE_CC_PAUSED;
3480 QueryTemplate *templ;
3482 /* Don't expire anything when we're offline */
3483 if ( cm->cc_paused & PCACHE_CC_OFFLINE ) {
3484 pause = PCACHE_CC_OFFLINE;
3488 connection_fake_init( &conn, &opbuf, ctx );
3492 op->o_dn = cm->db.be_rootdn;
3493 op->o_ndn = cm->db.be_rootndn;
3497 for (templ = qm->templates; templ; templ=templ->qmnext) {
3499 if ( !templ->query_last ) continue;
3501 op->o_time = slap_get_time();
3502 if ( !templ->ttr ) {
3504 if ( templ->negttl && templ->negttl < ttl )
3505 ttl = templ->negttl;
3506 if ( templ->limitttl && templ->limitttl < ttl )
3507 ttl = templ->limitttl;
3508 /* The oldest timestamp that needs expiration checking */
3512 for ( query=templ->query_last; query; query=qprev ) {
3513 qprev = query->prev;
3514 if ( query->refresh_time && query->refresh_time < op->o_time ) {
3515 /* A refresh will extend the expiry if the query has been
3516 * referenced, but not if it's unreferenced. If the
3517 * expiration has been hit, then skip the refresh since
3518 * we're just going to discard the result anyway.
3520 if ( query->refcnt )
3521 query->expiry_time = op->o_time + templ->ttl;
3522 if ( query->expiry_time > op->o_time ) {
3523 refresh_query( op, query, on );
3528 if (query->expiry_time < op->o_time) {
3530 Debug( pcache_debug, "Lock CR index = %p\n",
3531 (void *) templ, 0, 0 );
3532 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
3533 if ( query == templ->query_last ) {
3535 remove_from_template(query, templ);
3536 Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
3537 (void *) templ, templ->no_of_queries, 0 );
3538 Debug( pcache_debug, "Unlock CR index = %p\n",
3539 (void *) templ, 0, 0 );
3542 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
3545 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
3546 remove_query(qm, query);
3547 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
3548 if ( BER_BVISNULL( &query->q_uuid ))
3551 return_val = remove_query_data(op, &query->q_uuid);
3552 Debug( pcache_debug, "STALE QUERY REMOVED, SIZE=%d\n",
3554 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
3555 cm->cur_entries -= return_val;
3556 cm->num_cached_queries--;
3557 Debug( pcache_debug, "STORED QUERIES = %lu\n",
3558 cm->num_cached_queries, 0, 0 );
3559 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
3560 Debug( pcache_debug,
3561 "STALE QUERY REMOVED, CACHE ="
3563 cm->cur_entries, 0, 0 );
3564 ldap_pvt_thread_rdwr_wlock( &query->rwlock );
3565 if ( query->bind_refcnt-- ) {
3570 ldap_pvt_thread_rdwr_wunlock( &query->rwlock );
3571 if ( rem ) free_query(query);
3572 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
3573 } else if ( !templ->ttr && query->expiry_time > ttl ) {
3574 /* We don't need to check for refreshes, and this
3575 * query's expiry is too new, and all subsequent queries
3576 * will be newer yet. So stop looking.
3578 * If we have refreshes, then we always have to walk the
3579 * entire query list.
3587 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3588 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
3589 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
3591 /* If there were no queries, defer processing for a while */
3592 if ( cm->cc_paused != pause )
3593 cm->cc_paused = pause;
3594 ldap_pvt_runqueue_resched( &slapd_rq, rtask, pause );
3596 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3601 #define MAX_ATTR_SETS 500
3614 static ConfigDriver pc_cf_gen;
3615 static ConfigLDAPadd pc_ldadd;
3616 static ConfigCfAdd pc_cfadd;
3618 static ConfigTable pccfg[] = {
3619 { "pcache", "backend> <max_entries> <numattrsets> <entry limit> "
3621 6, 6, 0, ARG_MAGIC|ARG_NO_DELETE|PC_MAIN, pc_cf_gen,
3622 "( OLcfgOvAt:2.1 NAME ( 'olcPcache' 'olcProxyCache' ) "
3623 "DESC 'Proxy Cache basic parameters' "
3624 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
3625 { "pcacheAttrset", "index> <attributes...",
3626 2, 0, 0, ARG_MAGIC|PC_ATTR, pc_cf_gen,
3627 "( OLcfgOvAt:2.2 NAME ( 'olcPcacheAttrset' 'olcProxyAttrset' ) "
3628 "DESC 'A set of attributes to cache' "
3629 "SYNTAX OMsDirectoryString )", NULL, NULL },
3630 { "pcacheTemplate", "filter> <attrset-index> <TTL> <negTTL> "
3632 4, 7, 0, ARG_MAGIC|PC_TEMP, pc_cf_gen,
3633 "( OLcfgOvAt:2.3 NAME ( 'olcPcacheTemplate' 'olcProxyCacheTemplate' ) "
3634 "DESC 'Filter template, attrset, cache TTL, "
3635 "optional negative TTL, optional sizelimit TTL, "
3637 "SYNTAX OMsDirectoryString )", NULL, NULL },
3638 { "pcachePosition", "head|tail(default)",
3639 2, 2, 0, ARG_MAGIC|PC_RESP, pc_cf_gen,
3640 "( OLcfgOvAt:2.4 NAME 'olcPcachePosition' "
3641 "DESC 'Response callback position in overlay stack' "
3642 "SYNTAX OMsDirectoryString )", NULL, NULL },
3643 { "pcacheMaxQueries", "queries",
3644 2, 2, 0, ARG_INT|ARG_MAGIC|PC_QUERIES, pc_cf_gen,
3645 "( OLcfgOvAt:2.5 NAME ( 'olcPcacheMaxQueries' 'olcProxyCacheQueries' ) "
3646 "DESC 'Maximum number of queries to cache' "
3647 "SYNTAX OMsInteger )", NULL, NULL },
3648 { "pcachePersist", "TRUE|FALSE",
3649 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, save_queries),
3650 "( OLcfgOvAt:2.6 NAME ( 'olcPcachePersist' 'olcProxySaveQueries' ) "
3651 "DESC 'Save cached queries for hot restart' "
3652 "SYNTAX OMsBoolean )", NULL, NULL },
3653 { "pcacheValidate", "TRUE|FALSE",
3654 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, check_cacheability),
3655 "( OLcfgOvAt:2.7 NAME ( 'olcPcacheValidate' 'olcProxyCheckCacheability' ) "
3656 "DESC 'Check whether the results of a query are cacheable, e.g. for schema issues' "
3657 "SYNTAX OMsBoolean )", NULL, NULL },
3658 { "pcacheOffline", "TRUE|FALSE",
3659 2, 2, 0, ARG_ON_OFF|ARG_MAGIC|PC_OFFLINE, pc_cf_gen,
3660 "( OLcfgOvAt:2.8 NAME 'olcPcacheOffline' "
3661 "DESC 'Set cache to offline mode and disable expiration' "
3662 "SYNTAX OMsBoolean )", NULL, NULL },
3663 { "pcacheBind", "filter> <attrset-index> <TTR> <scope> <base",
3664 6, 6, 0, ARG_MAGIC|PC_BIND, pc_cf_gen,
3665 "( OLcfgOvAt:2.9 NAME 'olcPcacheBind' "
3666 "DESC 'Parameters for caching Binds' "
3667 "SYNTAX OMsDirectoryString )", NULL, NULL },
3668 { "pcache-", "private database args",
3669 1, 0, STRLENOF("pcache-"), ARG_MAGIC|PC_PRIVATE_DB, pc_cf_gen,
3672 /* Legacy keywords */
3673 { "proxycache", "backend> <max_entries> <numattrsets> <entry limit> "
3675 6, 6, 0, ARG_MAGIC|ARG_NO_DELETE|PC_MAIN, pc_cf_gen,
3677 { "proxyattrset", "index> <attributes...",
3678 2, 0, 0, ARG_MAGIC|PC_ATTR, pc_cf_gen,
3680 { "proxytemplate", "filter> <attrset-index> <TTL> <negTTL",
3681 4, 7, 0, ARG_MAGIC|PC_TEMP, pc_cf_gen,
3683 { "response-callback", "head|tail(default)",
3684 2, 2, 0, ARG_MAGIC|PC_RESP, pc_cf_gen,
3686 { "proxyCacheQueries", "queries",
3687 2, 2, 0, ARG_INT|ARG_MAGIC|PC_QUERIES, pc_cf_gen,
3689 { "proxySaveQueries", "TRUE|FALSE",
3690 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, save_queries),
3692 { "proxyCheckCacheability", "TRUE|FALSE",
3693 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, check_cacheability),
3696 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
3699 static ConfigOCs pcocs[] = {
3700 { "( OLcfgOvOc:2.1 "
3701 "NAME 'olcPcacheConfig' "
3702 "DESC 'ProxyCache configuration' "
3703 "SUP olcOverlayConfig "
3704 "MUST ( olcPcache $ olcPcacheAttrset $ olcPcacheTemplate ) "
3705 "MAY ( olcPcachePosition $ olcPcacheMaxQueries $ olcPcachePersist $ "
3706 "olcPcacheValidate $ olcPcacheOffline $ olcPcacheBind ) )",
3707 Cft_Overlay, pccfg, NULL, pc_cfadd },
3708 { "( OLcfgOvOc:2.2 "
3709 "NAME 'olcPcacheDatabase' "
3710 "DESC 'Cache database configuration' "
3711 "AUXILIARY )", Cft_Misc, olcDatabaseDummy, pc_ldadd },
3715 static int pcache_db_open2( slap_overinst *on, ConfigReply *cr );
3718 pc_ldadd_cleanup( ConfigArgs *c )
3720 slap_overinst *on = c->ca_private;
3721 return pcache_db_open2( on, &c->reply );
3725 pc_ldadd( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
3730 if ( p->ce_type != Cft_Overlay || !p->ce_bi ||
3731 p->ce_bi->bi_cf_ocs != pcocs )
3732 return LDAP_CONSTRAINT_VIOLATION;
3734 on = (slap_overinst *)p->ce_bi;
3735 cm = on->on_bi.bi_private;
3737 /* Defer open if this is an LDAPadd */
3738 if ( CONFIG_ONLINE_ADD( ca ))
3739 ca->cleanup = pc_ldadd_cleanup;
3741 cm->defer_db_open = 0;
3742 ca->ca_private = on;
3743 return LDAP_SUCCESS;
3747 pc_cfadd( Operation *op, SlapReply *rs, Entry *p, ConfigArgs *ca )
3749 CfEntryInfo *pe = p->e_private;
3750 slap_overinst *on = (slap_overinst *)pe->ce_bi;
3751 cache_manager *cm = on->on_bi.bi_private;
3754 /* FIXME: should not hardcode "olcDatabase" here */
3755 bv.bv_len = snprintf( ca->cr_msg, sizeof( ca->cr_msg ),
3756 "olcDatabase=" SLAP_X_ORDERED_FMT "%s",
3757 0, cm->db.bd_info->bi_type );
3758 if ( bv.bv_len >= sizeof( ca->cr_msg ) ) {
3761 bv.bv_val = ca->cr_msg;
3763 cm->defer_db_open = 0;
3765 /* We can only create this entry if the database is table-driven
3767 if ( cm->db.bd_info->bi_cf_ocs )
3768 config_build_entry( op, rs, pe, ca, &bv, cm->db.bd_info->bi_cf_ocs,
3775 pc_cf_gen( ConfigArgs *c )
3777 slap_overinst *on = (slap_overinst *)c->bi;
3778 cache_manager* cm = on->on_bi.bi_private;
3779 query_manager* qm = cm->qm;
3780 QueryTemplate* temp;
3781 AttributeName* attr_name;
3782 AttributeName* attrarray;
3783 const char* text=NULL;
3788 if ( c->op == SLAP_CONFIG_EMIT ) {
3792 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s %d %d %d %ld",
3793 cm->db.bd_info->bi_type, cm->max_entries, cm->numattrsets,
3794 cm->num_entries_limit, cm->cc_period );
3795 bv.bv_val = c->cr_msg;
3796 value_add_one( &c->rvalue_vals, &bv );
3799 for (i=0; i<cm->numattrsets; i++) {
3800 if ( !qm->attr_sets[i].count ) continue;
3802 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ), "%d", i );
3804 /* count the attr length */
3805 for ( attr_name = qm->attr_sets[i].attrs;
3806 attr_name->an_name.bv_val; attr_name++ )
3808 bv.bv_len += attr_name->an_name.bv_len + 1;
3809 if ( attr_name->an_desc &&
3810 ( attr_name->an_desc->ad_flags & SLAP_DESC_TEMPORARY ) ) {
3811 bv.bv_len += STRLENOF("undef:");
3815 bv.bv_val = ch_malloc( bv.bv_len+1 );
3816 ptr = lutil_strcopy( bv.bv_val, c->cr_msg );
3817 for ( attr_name = qm->attr_sets[i].attrs;
3818 attr_name->an_name.bv_val; attr_name++ ) {
3820 if ( attr_name->an_desc &&
3821 ( attr_name->an_desc->ad_flags & SLAP_DESC_TEMPORARY ) ) {
3822 ptr = lutil_strcopy( ptr, "undef:" );
3824 ptr = lutil_strcopy( ptr, attr_name->an_name.bv_val );
3826 ber_bvarray_add( &c->rvalue_vals, &bv );
3828 if ( !c->rvalue_vals )
3832 for (temp=qm->templates; temp; temp=temp->qmnext) {
3833 /* HEADS-UP: always print all;
3834 * if optional == 0, ignore */
3835 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ),
3836 " %d %ld %ld %ld %ld",
3837 temp->attr_set_index,
3842 bv.bv_len += temp->querystr.bv_len + 2;
3843 bv.bv_val = ch_malloc( bv.bv_len+1 );
3846 ptr = lutil_strcopy( ptr, temp->querystr.bv_val );
3848 strcpy( ptr, c->cr_msg );
3849 ber_bvarray_add( &c->rvalue_vals, &bv );
3851 if ( !c->rvalue_vals )
3855 for (temp=qm->templates; temp; temp=temp->qmnext) {
3856 if ( !temp->bindttr ) continue;
3857 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ),
3859 temp->attr_set_index,
3861 ldap_pvt_scope2str( temp->bindscope ));
3862 bv.bv_len += temp->bindbase.bv_len + temp->bindftemp.bv_len + 4;
3863 bv.bv_val = ch_malloc( bv.bv_len + 1 );
3866 ptr = lutil_strcopy( ptr, temp->bindftemp.bv_val );
3868 ptr = lutil_strcopy( ptr, c->cr_msg );
3870 ptr = lutil_strcopy( ptr, temp->bindbase.bv_val );
3873 ber_bvarray_add( &c->rvalue_vals, &bv );
3875 if ( !c->rvalue_vals )
3879 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
3880 BER_BVSTR( &bv, "head" );
3882 BER_BVSTR( &bv, "tail" );
3884 value_add_one( &c->rvalue_vals, &bv );
3887 c->value_int = cm->max_queries;
3890 c->value_int = (cm->cc_paused & PCACHE_CC_OFFLINE) != 0;
3894 } else if ( c->op == LDAP_MOD_DELETE ) {
3897 case PC_ATTR: /* FIXME */
3902 cm->cc_paused &= ~PCACHE_CC_OFFLINE;
3903 /* If there were cached queries when we went offline,
3904 * restart the checker now.
3906 if ( cm->num_cached_queries ) {
3907 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3909 ldap_pvt_runqueue_resched( &slapd_rq, cm->cc_arg, 0 );
3910 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3920 if ( cm->numattrsets > 0 ) {
3921 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"pcache\" directive already provided" );
3922 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3926 if ( lutil_atoi( &cm->numattrsets, c->argv[3] ) != 0 ) {
3927 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse num attrsets=\"%s\" (arg #3)",
3929 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3932 if ( cm->numattrsets <= 0 ) {
3933 snprintf( c->cr_msg, sizeof( c->cr_msg ), "numattrsets (arg #3) must be positive" );
3934 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3937 if ( cm->numattrsets > MAX_ATTR_SETS ) {
3938 snprintf( c->cr_msg, sizeof( c->cr_msg ), "numattrsets (arg #3) must be <= %d", MAX_ATTR_SETS );
3939 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3943 if ( !backend_db_init( c->argv[1], &cm->db, -1, NULL )) {
3944 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unknown backend type (arg #1)" );
3945 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3949 if ( lutil_atoi( &cm->max_entries, c->argv[2] ) != 0 ) {
3950 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse max entries=\"%s\" (arg #2)",
3952 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3955 if ( cm->max_entries <= 0 ) {
3956 snprintf( c->cr_msg, sizeof( c->cr_msg ), "max entries (arg #2) must be positive.\n" );
3957 Debug( LDAP_DEBUG_CONFIG, "%s: %s\n", c->log, c->cr_msg, 0 );
3961 if ( lutil_atoi( &cm->num_entries_limit, c->argv[4] ) != 0 ) {
3962 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse entry limit=\"%s\" (arg #4)",
3964 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3967 if ( cm->num_entries_limit <= 0 ) {
3968 snprintf( c->cr_msg, sizeof( c->cr_msg ), "entry limit (arg #4) must be positive" );
3969 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3972 if ( cm->num_entries_limit > cm->max_entries ) {
3973 snprintf( c->cr_msg, sizeof( c->cr_msg ), "entry limit (arg #4) must be less than max entries %d (arg #2)", cm->max_entries );
3974 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3978 if ( lutil_parse_time( c->argv[5], &t ) != 0 ) {
3979 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse period=\"%s\" (arg #5)",
3981 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3985 cm->cc_period = (time_t)t;
3986 Debug( pcache_debug,
3987 "Total # of attribute sets to be cached = %d.\n",
3988 cm->numattrsets, 0, 0 );
3989 qm->attr_sets = ( struct attr_set * )ch_calloc( cm->numattrsets,
3990 sizeof( struct attr_set ) );
3993 if ( cm->numattrsets == 0 ) {
3994 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"pcache\" directive not provided yet" );
3995 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3998 if ( lutil_atoi( &num, c->argv[1] ) != 0 ) {
3999 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse attrset #=\"%s\"",
4001 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4005 if ( num < 0 || num >= cm->numattrsets ) {
4006 snprintf( c->cr_msg, sizeof( c->cr_msg ), "attrset index %d out of bounds (must be %s%d)",
4007 num, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
4008 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4011 qm->attr_sets[num].flags |= PC_CONFIGURED;
4012 if ( c->argc == 2 ) {
4014 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4015 "need an explicit attr in attrlist; use \"*\" to indicate all attrs" );
4016 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4019 } else if ( c->argc == 3 ) {
4020 if ( strcmp( c->argv[2], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) {
4021 qm->attr_sets[num].count = 1;
4022 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 2,
4023 sizeof( AttributeName ) );
4024 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_USER_ATTRIBUTES );
4027 } else if ( strcmp( c->argv[2], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 ) {
4028 qm->attr_sets[num].count = 1;
4029 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 2,
4030 sizeof( AttributeName ) );
4031 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
4034 } else if ( strcmp( c->argv[2], LDAP_NO_ATTRS ) == 0 ) {
4037 /* else: fallthru */
4039 } else if ( c->argc == 4 ) {
4040 if ( ( strcmp( c->argv[2], LDAP_ALL_USER_ATTRIBUTES ) == 0 && strcmp( c->argv[3], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 )
4041 || ( strcmp( c->argv[2], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 && strcmp( c->argv[3], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) )
4043 qm->attr_sets[num].count = 2;
4044 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 3,
4045 sizeof( AttributeName ) );
4046 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_USER_ATTRIBUTES );
4047 BER_BVSTR( &qm->attr_sets[num].attrs[1].an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
4050 /* else: fallthru */
4053 if ( c->argc > 2 ) {
4054 int all_user = 0, all_op = 0;
4056 qm->attr_sets[num].count = c->argc - 2;
4057 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( c->argc - 1,
4058 sizeof( AttributeName ) );
4059 attr_name = qm->attr_sets[num].attrs;
4060 for ( i = 2; i < c->argc; i++ ) {
4061 attr_name->an_desc = NULL;
4062 if ( strcmp( c->argv[i], LDAP_NO_ATTRS ) == 0 ) {
4063 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4064 "invalid attr #%d \"%s\" in attrlist",
4065 i - 2, c->argv[i] );
4066 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4067 ch_free( qm->attr_sets[num].attrs );
4068 qm->attr_sets[num].attrs = NULL;
4069 qm->attr_sets[num].count = 0;
4072 if ( strcmp( c->argv[i], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) {
4074 BER_BVSTR( &attr_name->an_name, LDAP_ALL_USER_ATTRIBUTES );
4075 } else if ( strcmp( c->argv[i], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 ) {
4077 BER_BVSTR( &attr_name->an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
4079 if ( strncasecmp( c->argv[i], "undef:", STRLENOF("undef:") ) == 0 ) {
4081 ber_str2bv( c->argv[i] + STRLENOF("undef:"), 0, 0, &bv );
4082 attr_name->an_desc = slap_bv2tmp_ad( &bv, NULL );
4084 } else if ( slap_str2ad( c->argv[i], &attr_name->an_desc, &text ) ) {
4085 strcpy( c->cr_msg, text );
4086 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4087 ch_free( qm->attr_sets[num].attrs );
4088 qm->attr_sets[num].attrs = NULL;
4089 qm->attr_sets[num].count = 0;
4092 attr_name->an_name = attr_name->an_desc->ad_cname;
4094 attr_name->an_oc = NULL;
4095 attr_name->an_flags = 0;
4096 if ( attr_name->an_desc == slap_schema.si_ad_objectClass )
4097 qm->attr_sets[num].flags |= PC_GOT_OC;
4099 BER_BVZERO( &attr_name->an_name );
4102 /* warn if list contains both "*" and "+" */
4103 if ( i > 4 && all_user && all_op ) {
4104 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4105 "warning: attribute list contains \"*\" and \"+\"" );
4106 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4111 if ( cm->numattrsets == 0 ) {
4112 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"pcache\" directive not provided yet" );
4113 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4116 if ( lutil_atoi( &i, c->argv[2] ) != 0 ) {
4117 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse template #=\"%s\"",
4119 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4123 if ( i < 0 || i >= cm->numattrsets ||
4124 !(qm->attr_sets[i].flags & PC_CONFIGURED )) {
4125 snprintf( c->cr_msg, sizeof( c->cr_msg ), "template index %d invalid (%s%d)",
4126 i, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
4127 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4131 AttributeName *attrs;
4133 cnt = template_attrs( c->argv[1], &qm->attr_sets[i], &attrs, &text );
4135 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse template: %s",
4137 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4140 temp = ch_calloc( 1, sizeof( QueryTemplate ));
4141 temp->qmnext = qm->templates;
4142 qm->templates = temp;
4143 temp->t_attrs.attrs = attrs;
4144 temp->t_attrs.count = cnt;
4146 ldap_pvt_thread_rdwr_init( &temp->t_rwlock );
4147 temp->query = temp->query_last = NULL;
4148 if ( lutil_parse_time( c->argv[3], &t ) != 0 ) {
4149 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4150 "unable to parse template ttl=\"%s\"",
4152 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4154 ch_free( temp->t_attrs.attrs );
4158 temp->ttl = (time_t)t;
4159 temp->negttl = (time_t)0;
4160 temp->limitttl = (time_t)0;
4161 temp->ttr = (time_t)0;
4162 switch ( c->argc ) {
4164 if ( lutil_parse_time( c->argv[6], &t ) != 0 ) {
4165 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4166 "unable to parse template ttr=\"%s\"",
4168 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4171 temp->ttr = (time_t)t;
4175 if ( lutil_parse_time( c->argv[5], &t ) != 0 ) {
4176 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4177 "unable to parse template sizelimit ttl=\"%s\"",
4179 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4182 temp->limitttl = (time_t)t;
4186 if ( lutil_parse_time( c->argv[4], &t ) != 0 ) {
4187 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4188 "unable to parse template negative ttl=\"%s\"",
4190 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4193 temp->negttl = (time_t)t;
4197 temp->no_of_queries = 0;
4199 ber_str2bv( c->argv[1], 0, 1, &temp->querystr );
4200 Debug( pcache_debug, "Template:\n", 0, 0, 0 );
4201 Debug( pcache_debug, " query template: %s\n",
4202 temp->querystr.bv_val, 0, 0 );
4203 temp->attr_set_index = i;
4204 qm->attr_sets[i].flags |= PC_REFERENCED;
4205 temp->qtnext = qm->attr_sets[i].templates;
4206 qm->attr_sets[i].templates = temp;
4207 Debug( pcache_debug, " attributes: \n", 0, 0, 0 );
4208 if ( ( attrarray = qm->attr_sets[i].attrs ) != NULL ) {
4209 for ( i=0; attrarray[i].an_name.bv_val; i++ )
4210 Debug( pcache_debug, "\t%s\n",
4211 attrarray[i].an_name.bv_val, 0, 0 );
4215 if ( !qm->templates ) {
4216 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"pcacheTemplate\" directive not provided yet" );
4217 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4220 if ( lutil_atoi( &i, c->argv[2] ) != 0 ) {
4221 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse Bind index #=\"%s\"",
4223 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4227 if ( i < 0 || i >= cm->numattrsets ||
4228 !(qm->attr_sets[i].flags & PC_CONFIGURED )) {
4229 snprintf( c->cr_msg, sizeof( c->cr_msg ), "Bind index %d invalid (%s%d)",
4230 i, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
4231 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4234 { struct berval bv, tempbv;
4235 AttributeDescription **descs;
4237 ber_str2bv( c->argv[1], 0, 0, &bv );
4238 ndescs = ftemp_attrs( &bv, &tempbv, &descs, &text );
4240 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse template: %s",
4242 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4245 for ( temp = qm->templates; temp; temp=temp->qmnext ) {
4246 if ( temp->attr_set_index == i && bvmatch( &tempbv,
4250 ch_free( tempbv.bv_val );
4253 snprintf( c->cr_msg, sizeof( c->cr_msg ), "Bind template %s %d invalid",
4255 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4258 ber_dupbv( &temp->bindftemp, &bv );
4259 temp->bindfattrs = descs;
4260 temp->bindnattrs = ndescs;
4262 if ( lutil_parse_time( c->argv[3], &t ) != 0 ) {
4263 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4264 "unable to parse bind ttr=\"%s\"",
4266 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4268 ch_free( temp->bindfattrs );
4269 temp->bindfattrs = NULL;
4270 ch_free( temp->bindftemp.bv_val );
4271 BER_BVZERO( &temp->bindftemp );
4274 num = ldap_pvt_str2scope( c->argv[4] );
4276 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4277 "unable to parse bind scope=\"%s\"",
4279 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4283 struct berval dn, ndn;
4284 ber_str2bv( c->argv[5], 0, 0, &dn );
4285 rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
4287 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4288 "invalid bind baseDN=\"%s\"",
4290 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4293 if ( temp->bindbase.bv_val )
4294 ch_free( temp->bindbase.bv_val );
4295 temp->bindbase = ndn;
4298 /* convert the template into dummy filter */
4300 char *eq = temp->bindftemp.bv_val, *e2;
4303 while ((eq = strchr(eq, '=' ))) {
4308 bv.bv_len = temp->bindftemp.bv_len + i;
4309 bv.bv_val = ch_malloc( bv.bv_len + 1 );
4310 for ( e2 = bv.bv_val, eq = temp->bindftemp.bv_val;
4321 f = str2filter( bv.bv_val );
4323 ch_free( bv.bv_val );
4324 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4325 "unable to parse bindfilter=\"%s\"", bv.bv_val );
4326 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4327 ch_free( temp->bindbase.bv_val );
4328 BER_BVZERO( &temp->bindbase );
4331 if ( temp->bindfilter )
4332 filter_free( temp->bindfilter );
4333 if ( temp->bindfilterstr.bv_val )
4334 ch_free( temp->bindfilterstr.bv_val );
4335 temp->bindfilterstr = bv;
4336 temp->bindfilter = f;
4338 temp->bindttr = (time_t)t;
4339 temp->bindscope = num;
4340 cm->cache_binds = 1;
4344 if ( strcasecmp( c->argv[1], "head" ) == 0 ) {
4345 cm->response_cb = PCACHE_RESPONSE_CB_HEAD;
4347 } else if ( strcasecmp( c->argv[1], "tail" ) == 0 ) {
4348 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
4351 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unknown specifier" );
4352 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4357 if ( c->value_int <= 0 ) {
4358 snprintf( c->cr_msg, sizeof( c->cr_msg ), "max queries must be positive" );
4359 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4362 cm->max_queries = c->value_int;
4366 cm->cc_paused |= PCACHE_CC_OFFLINE;
4368 cm->cc_paused &= ~PCACHE_CC_OFFLINE;
4371 if ( cm->db.be_private == NULL ) {
4372 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4373 "private database must be defined before setting database specific options" );
4374 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4378 if ( cm->db.bd_info->bi_cf_ocs ) {
4381 char *argv0 = c->argv[ 0 ];
4383 c->argv[ 0 ] = &argv0[ STRLENOF( "pcache-" ) ];
4385 ct = config_find_keyword( cm->db.bd_info->bi_cf_ocs->co_table, c );
4387 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4388 "private database does not recognize specific option '%s'",
4390 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4394 c->table = cm->db.bd_info->bi_cf_ocs->co_type;
4396 c->bi = c->be->bd_info;
4398 rc = config_add_vals( ct, c );
4402 c->table = c2.table;
4405 c->argv[ 0 ] = argv0;
4407 } else if ( cm->db.be_config != NULL ) {
4408 char *argv0 = c->argv[ 0 ];
4410 c->argv[ 0 ] = &argv0[ STRLENOF( "pcache-" ) ];
4411 rc = cm->db.be_config( &cm->db, c->fname, c->lineno, c->argc, c->argv );
4412 c->argv[ 0 ] = argv0;
4415 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4416 "no means to set private database specific options" );
4417 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4422 rc = SLAP_CONF_UNKNOWN;
4438 slap_overinst *on = (slap_overinst *)be->bd_info;
4439 cache_manager* cm = on->on_bi.bi_private;
4441 /* Something for the cache database? */
4442 if ( cm->db.bd_info && cm->db.bd_info->bi_db_config )
4443 return cm->db.bd_info->bi_db_config( &cm->db, fname, lineno,
4445 return SLAP_CONF_UNKNOWN;
4453 slap_overinst *on = (slap_overinst *)be->bd_info;
4457 cm = (cache_manager *)ch_malloc(sizeof(cache_manager));
4458 on->on_bi.bi_private = cm;
4460 qm = (query_manager*)ch_malloc(sizeof(query_manager));
4463 SLAP_DBFLAGS(&cm->db) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
4464 cm->db.be_private = NULL;
4465 cm->db.bd_self = &cm->db;
4467 cm->numattrsets = 0;
4468 cm->num_entries_limit = 5;
4469 cm->num_cached_queries = 0;
4470 cm->max_entries = 0;
4471 cm->cur_entries = 0;
4472 cm->max_queries = 10000;
4473 cm->save_queries = 0;
4474 cm->check_cacheability = 0;
4475 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
4476 cm->defer_db_open = 1;
4477 cm->cache_binds = 0;
4478 cm->cc_period = 1000;
4481 #ifdef PCACHE_MONITOR
4482 cm->monitor_cb = NULL;
4483 #endif /* PCACHE_MONITOR */
4485 qm->attr_sets = NULL;
4486 qm->templates = NULL;
4488 qm->lru_bottom = NULL;
4490 qm->qcfunc = query_containment;
4491 qm->crfunc = cache_replacement;
4492 qm->addfunc = add_query;
4493 ldap_pvt_thread_mutex_init(&qm->lru_mutex);
4495 ldap_pvt_thread_mutex_init(&cm->cache_mutex);
4497 #ifndef PCACHE_MONITOR
4499 #else /* PCACHE_MONITOR */
4500 return pcache_monitor_db_init( be );
4501 #endif /* PCACHE_MONITOR */
4505 pcache_cachedquery_open_cb( Operation *op, SlapReply *rs )
4507 assert( op->o_tag == LDAP_REQ_SEARCH );
4509 if ( rs->sr_type == REP_SEARCH ) {
4512 a = attr_find( rs->sr_entry->e_attrs, ad_cachedQueryURL );
4516 assert( a->a_nvals != NULL );
4518 valsp = op->o_callback->sc_private;
4519 assert( *valsp == NULL );
4521 ber_bvarray_dup_x( valsp, a->a_nvals, op->o_tmpmemctx );
4529 pcache_cachedquery_count_cb( Operation *op, SlapReply *rs )
4531 assert( op->o_tag == LDAP_REQ_SEARCH );
4533 if ( rs->sr_type == REP_SEARCH ) {
4534 int *countp = (int *)op->o_callback->sc_private;
4547 cache_manager *cm = on->on_bi.bi_private;
4548 query_manager* qm = cm->qm;
4551 rc = backend_startup_one( &cm->db, cr );
4553 cm->defer_db_open = 0;
4556 /* There is no runqueue in TOOL mode */
4557 if (( slapMode & SLAP_SERVER_MODE ) && rc == 0 ) {
4558 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4559 ldap_pvt_runqueue_insert( &slapd_rq, cm->cc_period,
4560 consistency_check, on,
4561 "pcache_consistency", cm->db.be_suffix[0].bv_val );
4562 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4564 /* Cached database must have the rootdn */
4565 if ( BER_BVISNULL( &cm->db.be_rootndn )
4566 || BER_BVISEMPTY( &cm->db.be_rootndn ) )
4568 Debug( LDAP_DEBUG_ANY, "pcache_db_open(): "
4569 "underlying database of type \"%s\"\n"
4570 " serving naming context \"%s\"\n"
4571 " has no \"rootdn\", required by \"pcache\".\n",
4572 on->on_info->oi_orig->bi_type,
4573 cm->db.be_suffix[0].bv_val, 0 );
4577 if ( cm->save_queries ) {
4578 void *thrctx = ldap_pvt_thread_pool_context();
4579 Connection conn = { 0 };
4580 OperationBuffer opbuf;
4582 slap_callback cb = { 0 };
4583 SlapReply rs = { REP_RESULT };
4584 BerVarray vals = NULL;
4585 Filter f = { 0 }, f2 = { 0 };
4586 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
4587 AttributeName attrs[ 2 ] = {{{ 0 }}};
4589 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
4594 op->o_tag = LDAP_REQ_SEARCH;
4595 op->o_protocol = LDAP_VERSION3;
4596 cb.sc_response = pcache_cachedquery_open_cb;
4597 cb.sc_private = &vals;
4598 op->o_callback = &cb;
4599 op->o_time = slap_get_time();
4600 op->o_do_not_cache = 1;
4601 op->o_managedsait = SLAP_CONTROL_CRITICAL;
4603 op->o_dn = cm->db.be_rootdn;
4604 op->o_ndn = cm->db.be_rootndn;
4605 op->o_req_dn = cm->db.be_suffix[ 0 ];
4606 op->o_req_ndn = cm->db.be_nsuffix[ 0 ];
4608 op->ors_scope = LDAP_SCOPE_BASE;
4609 op->ors_deref = LDAP_DEREF_NEVER;
4611 op->ors_tlimit = SLAP_NO_LIMIT;
4612 op->ors_limit = NULL;
4613 ber_str2bv( "(pcacheQueryURL=*)", 0, 0, &op->ors_filterstr );
4614 f.f_choice = LDAP_FILTER_PRESENT;
4615 f.f_desc = ad_cachedQueryURL;
4616 op->ors_filter = &f;
4617 attrs[ 0 ].an_desc = ad_cachedQueryURL;
4618 attrs[ 0 ].an_name = ad_cachedQueryURL->ad_cname;
4619 op->ors_attrs = attrs;
4620 op->ors_attrsonly = 0;
4622 rc = op->o_bd->be_search( op, &rs );
4623 if ( rc == LDAP_SUCCESS && vals != NULL ) {
4626 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
4627 if ( url2query( vals[ i ].bv_val, op, qm ) == 0 ) {
4628 cm->num_cached_queries++;
4632 ber_bvarray_free_x( vals, op->o_tmpmemctx );
4635 /* count cached entries */
4636 f.f_choice = LDAP_FILTER_NOT;
4638 f2.f_choice = LDAP_FILTER_EQUALITY;
4640 f2.f_av_desc = slap_schema.si_ad_objectClass;
4641 BER_BVSTR( &f2.f_av_value, "glue" );
4642 ber_str2bv( "(!(objectClass=glue))", 0, 0, &op->ors_filterstr );
4644 op->ors_slimit = SLAP_NO_LIMIT;
4645 op->ors_scope = LDAP_SCOPE_SUBTREE;
4646 op->ors_attrs = slap_anlist_no_attrs;
4648 rs_reinit( &rs, REP_RESULT );
4649 op->o_callback->sc_response = pcache_cachedquery_count_cb;
4650 op->o_callback->sc_private = &rs.sr_nentries;
4652 rc = op->o_bd->be_search( op, &rs );
4654 cm->cur_entries = rs.sr_nentries;
4668 slap_overinst *on = (slap_overinst *)be->bd_info;
4669 cache_manager *cm = on->on_bi.bi_private;
4670 query_manager* qm = cm->qm;
4671 int i, ncf = 0, rf = 0, nrf = 0, rc = 0;
4673 /* check attr sets */
4674 for ( i = 0; i < cm->numattrsets; i++) {
4675 if ( !( qm->attr_sets[i].flags & PC_CONFIGURED ) ) {
4676 if ( qm->attr_sets[i].flags & PC_REFERENCED ) {
4677 Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d not configured but referenced.\n", i, 0, 0 );
4681 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, attr set #%d not configured.\n", i, 0, 0 );
4685 } else if ( !( qm->attr_sets[i].flags & PC_REFERENCED ) ) {
4686 Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d configured but not referenced.\n", i, 0, 0 );
4691 if ( ncf || rf || nrf ) {
4692 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets configured but not referenced.\n", nrf, 0, 0 );
4693 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets not configured.\n", ncf, 0, 0 );
4694 Debug( LDAP_DEBUG_CONFIG, "pcache: %d attr sets not configured but referenced.\n", rf, 0, 0 );
4701 /* need to inherit something from the original database... */
4702 cm->db.be_def_limit = be->be_def_limit;
4703 cm->db.be_limits = be->be_limits;
4704 cm->db.be_acl = be->be_acl;
4705 cm->db.be_dfltaccess = be->be_dfltaccess;
4707 if ( SLAP_DBMONITORING( be ) ) {
4708 SLAP_DBFLAGS( &cm->db ) |= SLAP_DBFLAG_MONITORING;
4711 SLAP_DBFLAGS( &cm->db ) &= ~SLAP_DBFLAG_MONITORING;
4714 if ( !cm->defer_db_open ) {
4715 rc = pcache_db_open2( on, cr );
4718 #ifdef PCACHE_MONITOR
4719 if ( rc == LDAP_SUCCESS ) {
4720 rc = pcache_monitor_db_open( be );
4722 #endif /* PCACHE_MONITOR */
4728 pcache_free_qbase( void *v )
4734 tavl_free( qb->scopes[i], NULL );
4744 slap_overinst *on = (slap_overinst *)be->bd_info;
4745 cache_manager *cm = on->on_bi.bi_private;
4746 query_manager *qm = cm->qm;
4750 /* stop the thread ... */
4752 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4753 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, cm->cc_arg ) ) {
4754 ldap_pvt_runqueue_stoptask( &slapd_rq, cm->cc_arg );
4756 ldap_pvt_runqueue_remove( &slapd_rq, cm->cc_arg );
4757 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4760 if ( cm->save_queries ) {
4762 BerVarray vals = NULL;
4765 Connection conn = { 0 };
4766 OperationBuffer opbuf;
4768 slap_callback cb = { 0 };
4770 SlapReply rs = { REP_RESULT };
4771 Modifications mod = {{ 0 }};
4773 thrctx = ldap_pvt_thread_pool_context();
4775 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
4778 mod.sml_numvals = 0;
4779 if ( qm->templates != NULL ) {
4780 for ( tm = qm->templates; tm != NULL; tm = tm->qmnext ) {
4781 for ( qc = tm->query; qc; qc = qc->next ) {
4784 if ( query2url( op, qc, &bv, 0 ) == 0 ) {
4785 ber_bvarray_add_x( &vals, &bv, op->o_tmpmemctx );
4793 op->o_dn = cm->db.be_rootdn;
4794 op->o_ndn = cm->db.be_rootndn;
4796 op->o_tag = LDAP_REQ_MODIFY;
4797 op->o_protocol = LDAP_VERSION3;
4798 cb.sc_response = slap_null_cb;
4799 op->o_callback = &cb;
4800 op->o_time = slap_get_time();
4801 op->o_do_not_cache = 1;
4802 op->o_managedsait = SLAP_CONTROL_CRITICAL;
4804 op->o_req_dn = op->o_bd->be_suffix[0];
4805 op->o_req_ndn = op->o_bd->be_nsuffix[0];
4807 mod.sml_op = LDAP_MOD_REPLACE;
4809 mod.sml_desc = ad_cachedQueryURL;
4810 mod.sml_type = ad_cachedQueryURL->ad_cname;
4811 mod.sml_values = vals;
4812 mod.sml_nvalues = NULL;
4813 mod.sml_next = NULL;
4814 Debug( pcache_debug,
4815 "%sSETTING CACHED QUERY URLS\n",
4816 vals == NULL ? "RE" : "", 0, 0 );
4818 op->orm_modlist = &mod;
4820 op->o_bd->be_modify( op, &rs );
4822 ber_bvarray_free_x( vals, op->o_tmpmemctx );
4825 /* cleanup stuff inherited from the original database... */
4826 cm->db.be_limits = NULL;
4827 cm->db.be_acl = NULL;
4830 if ( cm->db.bd_info->bi_db_close ) {
4831 rc = cm->db.bd_info->bi_db_close( &cm->db, NULL );
4833 while ( (tm = qm->templates) != NULL ) {
4834 CachedQuery *qc, *qn;
4835 qm->templates = tm->qmnext;
4836 for ( qc = tm->query; qc; qc = qn ) {
4840 avl_free( tm->qbase, pcache_free_qbase );
4841 free( tm->querystr.bv_val );
4842 free( tm->bindfattrs );
4843 free( tm->bindftemp.bv_val );
4844 free( tm->bindfilterstr.bv_val );
4845 free( tm->bindbase.bv_val );
4846 filter_free( tm->bindfilter );
4847 ldap_pvt_thread_rdwr_destroy( &tm->t_rwlock );
4848 free( tm->t_attrs.attrs );
4852 for ( i = 0; i < cm->numattrsets; i++ ) {
4855 /* Account of LDAP_NO_ATTRS */
4856 if ( !qm->attr_sets[i].count ) continue;
4858 for ( j = 0; !BER_BVISNULL( &qm->attr_sets[i].attrs[j].an_name ); j++ ) {
4859 if ( qm->attr_sets[i].attrs[j].an_desc &&
4860 ( qm->attr_sets[i].attrs[j].an_desc->ad_flags &
4861 SLAP_DESC_TEMPORARY ) ) {
4862 slap_sl_mfuncs.bmf_free( qm->attr_sets[i].attrs[j].an_desc, NULL );
4865 free( qm->attr_sets[i].attrs );
4867 free( qm->attr_sets );
4868 qm->attr_sets = NULL;
4870 #ifdef PCACHE_MONITOR
4871 if ( rc == LDAP_SUCCESS ) {
4872 rc = pcache_monitor_db_close( be );
4874 #endif /* PCACHE_MONITOR */
4885 slap_overinst *on = (slap_overinst *)be->bd_info;
4886 cache_manager *cm = on->on_bi.bi_private;
4887 query_manager *qm = cm->qm;
4889 if ( cm->db.be_private != NULL ) {
4890 backend_stopdown_one( &cm->db );
4893 ldap_pvt_thread_mutex_destroy( &qm->lru_mutex );
4894 ldap_pvt_thread_mutex_destroy( &cm->cache_mutex );
4898 #ifdef PCACHE_MONITOR
4899 pcache_monitor_db_destroy( be );
4900 #endif /* PCACHE_MONITOR */
4905 #ifdef PCACHE_CONTROL_PRIVDB
4907 Control ::= SEQUENCE {
4908 controlType LDAPOID,
4909 criticality BOOLEAN DEFAULT FALSE,
4910 controlValue OCTET STRING OPTIONAL }
4912 controlType ::= 1.3.6.1.4.1.4203.666.11.9.5.1
4914 * criticality must be TRUE; controlValue must be absent.
4922 if ( op->o_ctrlflag[ privDB_cid ] != SLAP_CONTROL_NONE ) {
4923 rs->sr_text = "privateDB control specified multiple times";
4924 return LDAP_PROTOCOL_ERROR;
4927 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
4928 rs->sr_text = "privateDB control value not absent";
4929 return LDAP_PROTOCOL_ERROR;
4932 if ( !ctrl->ldctl_iscritical ) {
4933 rs->sr_text = "privateDB control criticality required";
4934 return LDAP_PROTOCOL_ERROR;
4937 op->o_ctrlflag[ privDB_cid ] = SLAP_CONTROL_CRITICAL;
4939 return LDAP_SUCCESS;
4942 static char *extops[] = {
4943 LDAP_EXOP_MODIFY_PASSWD,
4946 #endif /* PCACHE_CONTROL_PRIVDB */
4948 static struct berval pcache_exop_MODIFY_PASSWD = BER_BVC( LDAP_EXOP_MODIFY_PASSWD );
4949 #ifdef PCACHE_EXOP_QUERY_DELETE
4950 static struct berval pcache_exop_QUERY_DELETE = BER_BVC( PCACHE_EXOP_QUERY_DELETE );
4952 #define LDAP_TAG_EXOP_QUERY_DELETE_BASE ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 0)
4953 #define LDAP_TAG_EXOP_QUERY_DELETE_DN ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 1)
4954 #define LDAP_TAG_EXOP_QUERY_DELETE_UUID ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 2)
4957 ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
4958 requestName [0] LDAPOID,
4959 requestValue [1] OCTET STRING OPTIONAL }
4961 requestName ::= 1.3.6.1.4.1.4203.666.11.9.6.1
4963 requestValue ::= SEQUENCE { CHOICE {
4965 entryDN [1] LDAPDN },
4966 queryID [2] OCTET STRING (SIZE(16))
4967 -- constrained to UUID }
4969 * Either baseDN or entryDN must be present, to allow database selection.
4971 * 1. if baseDN and queryID are present, then the query corresponding
4972 * to queryID is deleted;
4973 * 2. if baseDN is present and queryID is absent, then all queries
4975 * 3. if entryDN is present and queryID is absent, then all queries
4976 * corresponding to the queryID values present in entryDN are deleted;
4977 * 4. if entryDN and queryID are present, then all queries
4978 * corresponding to the queryID values present in entryDN are deleted,
4979 * but only if the value of queryID is contained in the entry;
4981 * Currently, only 1, 3 and 4 are implemented. 2 can be obtained by either
4982 * recursively deleting the database (ldapdelete -r) with PRIVDB control,
4983 * or by removing the database files.
4985 ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
4986 COMPONENTS OF LDAPResult,
4987 responseName [10] LDAPOID OPTIONAL,
4988 responseValue [11] OCTET STRING OPTIONAL }
4990 * responseName and responseValue must be absent.
4994 * - on success, *tagp is either LDAP_TAG_EXOP_QUERY_DELETE_BASE
4995 * or LDAP_TAG_EXOP_QUERY_DELETE_DN.
4996 * - if ndn != NULL, it is set to the normalized DN in the request
4997 * corresponding to either the baseDN or the entryDN, according
4998 * to *tagp; memory is malloc'ed on the Operation's slab, and must
4999 * be freed by the caller.
5000 * - if uuid != NULL, it is set to point to the normalized UUID;
5001 * memory is malloc'ed on the Operation's slab, and must
5002 * be freed by the caller.
5005 pcache_parse_query_delete(
5009 struct berval *uuid,
5013 int rc = LDAP_SUCCESS;
5016 BerElementBuffer berbuf;
5017 BerElement *ber = (BerElement *)&berbuf;
5018 struct berval reqdata = BER_BVNULL;
5030 if ( in == NULL || in->bv_len == 0 ) {
5031 *text = "empty request data field in queryDelete exop";
5032 return LDAP_PROTOCOL_ERROR;
5035 ber_dupbv_x( &reqdata, in, ctx );
5037 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
5038 ber_init2( ber, &reqdata, 0 );
5040 tag = ber_scanf( ber, "{" /*}*/ );
5042 if ( tag == LBER_ERROR ) {
5043 Debug( LDAP_DEBUG_TRACE,
5044 "pcache_parse_query_delete: decoding error.\n",
5046 goto decoding_error;
5049 tag = ber_peek_tag( ber, &len );
5050 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_BASE
5051 || tag == LDAP_TAG_EXOP_QUERY_DELETE_DN )
5055 if ( ndn != NULL ) {
5058 tag = ber_scanf( ber, "m", &dn );
5059 if ( tag == LBER_ERROR ) {
5060 Debug( LDAP_DEBUG_TRACE,
5061 "pcache_parse_query_delete: DN parse failed.\n",
5063 goto decoding_error;
5066 rc = dnNormalize( 0, NULL, NULL, &dn, ndn, ctx );
5067 if ( rc != LDAP_SUCCESS ) {
5068 *text = "invalid DN in queryDelete exop request data";
5073 tag = ber_scanf( ber, "x" /* "m" */ );
5074 if ( tag == LBER_DEFAULT ) {
5075 goto decoding_error;
5079 tag = ber_peek_tag( ber, &len );
5082 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_UUID ) {
5083 if ( uuid != NULL ) {
5085 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
5087 tag = ber_scanf( ber, "m", &bv );
5088 if ( tag == LBER_ERROR ) {
5089 Debug( LDAP_DEBUG_TRACE,
5090 "pcache_parse_query_delete: UUID parse failed.\n",
5092 goto decoding_error;
5095 if ( bv.bv_len != 16 ) {
5096 Debug( LDAP_DEBUG_TRACE,
5097 "pcache_parse_query_delete: invalid UUID length %lu.\n",
5098 (unsigned long)bv.bv_len, 0, 0 );
5099 goto decoding_error;
5102 rc = lutil_uuidstr_from_normalized(
5103 bv.bv_val, bv.bv_len,
5104 uuidbuf, sizeof( uuidbuf ) );
5106 goto decoding_error;
5108 ber_str2bv( uuidbuf, rc, 1, uuid );
5112 tag = ber_skip_tag( ber, &len );
5113 if ( tag == LBER_DEFAULT ) {
5114 goto decoding_error;
5118 Debug( LDAP_DEBUG_TRACE,
5119 "pcache_parse_query_delete: invalid UUID length %lu.\n",
5120 (unsigned long)len, 0, 0 );
5121 goto decoding_error;
5125 tag = ber_peek_tag( ber, &len );
5128 if ( tag != LBER_DEFAULT || len != 0 ) {
5130 Debug( LDAP_DEBUG_TRACE,
5131 "pcache_parse_query_delete: decoding error\n",
5133 rc = LDAP_PROTOCOL_ERROR;
5134 *text = "queryDelete data decoding error";
5137 if ( ndn && !BER_BVISNULL( ndn ) ) {
5138 slap_sl_free( ndn->bv_val, ctx );
5142 if ( uuid && !BER_BVISNULL( uuid ) ) {
5143 slap_sl_free( uuid->bv_val, ctx );
5148 if ( !BER_BVISNULL( &reqdata ) ) {
5149 ber_memfree_x( reqdata.bv_val, ctx );
5156 pcache_exop_query_delete(
5160 BackendDB *bd = op->o_bd;
5162 struct berval uuid = BER_BVNULL,
5164 char buf[ SLAP_TEXT_BUFLEN ];
5166 ber_tag_t tag = LBER_DEFAULT;
5168 if ( LogTest( LDAP_DEBUG_STATS ) ) {
5172 rs->sr_err = pcache_parse_query_delete( op->ore_reqdata,
5173 &tag, &op->o_req_ndn, uuidp,
5174 &rs->sr_text, op->o_tmpmemctx );
5175 if ( rs->sr_err != LDAP_SUCCESS ) {
5179 if ( LogTest( LDAP_DEBUG_STATS ) ) {
5180 assert( !BER_BVISNULL( &op->o_req_ndn ) );
5181 len = snprintf( buf, sizeof( buf ), " dn=\"%s\"", op->o_req_ndn.bv_val );
5183 if ( !BER_BVISNULL( &uuid ) && len < sizeof( buf ) ) {
5184 snprintf( &buf[ len ], sizeof( buf ) - len, " pcacheQueryId=\"%s\"", uuid.bv_val );
5187 Debug( LDAP_DEBUG_STATS, "%s QUERY DELETE%s\n",
5188 op->o_log_prefix, buf, 0 );
5190 op->o_req_dn = op->o_req_ndn;
5192 op->o_bd = select_backend( &op->o_req_ndn, 0 );
5193 if ( op->o_bd == NULL ) {
5194 send_ldap_error( op, rs, LDAP_NO_SUCH_OBJECT,
5195 "no global superior knowledge" );
5197 rs->sr_err = backend_check_restrictions( op, rs,
5198 (struct berval *)&pcache_exop_QUERY_DELETE );
5199 if ( rs->sr_err != LDAP_SUCCESS ) {
5203 if ( op->o_bd->be_extended == NULL ) {
5204 send_ldap_error( op, rs, LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
5205 "backend does not support extended operations" );
5209 op->o_bd->be_extended( op, rs );
5212 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
5213 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
5214 BER_BVZERO( &op->o_req_ndn );
5215 BER_BVZERO( &op->o_req_dn );
5218 if ( !BER_BVISNULL( &uuid ) ) {
5219 op->o_tmpfree( uuid.bv_val, op->o_tmpmemctx );
5226 #endif /* PCACHE_EXOP_QUERY_DELETE */
5229 pcache_op_extended( Operation *op, SlapReply *rs )
5231 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
5232 cache_manager *cm = on->on_bi.bi_private;
5234 #ifdef PCACHE_CONTROL_PRIVDB
5235 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL ) {
5236 return pcache_op_privdb( op, rs );
5238 #endif /* PCACHE_CONTROL_PRIVDB */
5240 #ifdef PCACHE_EXOP_QUERY_DELETE
5241 if ( bvmatch( &op->ore_reqoid, &pcache_exop_QUERY_DELETE ) ) {
5242 struct berval uuid = BER_BVNULL;
5243 ber_tag_t tag = LBER_DEFAULT;
5245 rs->sr_err = pcache_parse_query_delete( op->ore_reqdata,
5246 &tag, NULL, &uuid, &rs->sr_text, op->o_tmpmemctx );
5247 assert( rs->sr_err == LDAP_SUCCESS );
5249 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_DN ) {
5250 /* remove all queries related to the selected entry */
5251 rs->sr_err = pcache_remove_entry_queries_from_cache( op,
5252 cm, &op->o_req_ndn, &uuid );
5254 } else if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_BASE ) {
5255 if ( !BER_BVISNULL( &uuid ) ) {
5256 /* remove the selected query */
5257 rs->sr_err = pcache_remove_query_from_cache( op,
5261 /* TODO: remove all queries */
5262 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5263 rs->sr_text = "deletion of all queries not implemented";
5267 op->o_tmpfree( uuid.bv_val, op->o_tmpmemctx );
5270 #endif /* PCACHE_EXOP_QUERY_DELETE */
5272 /* We only care if we're configured for Bind caching */
5273 if ( bvmatch( &op->ore_reqoid, &pcache_exop_MODIFY_PASSWD ) &&
5275 /* See if the local entry exists and has a password.
5276 * It's too much work to find the matching query, so
5277 * we just see if there's a hashed password to update.
5279 Operation op2 = *op;
5285 op2.o_dn = op->o_bd->be_rootdn;
5286 op2.o_ndn = op->o_bd->be_rootndn;
5287 rc = be_entry_get_rw( &op2, &op->o_req_ndn, NULL,
5288 slap_schema.si_ad_userPassword, 0, &e );
5289 if ( rc == LDAP_SUCCESS && e ) {
5290 /* See if a recognized password is hashed here */
5291 Attribute *a = attr_find( e->e_attrs,
5292 slap_schema.si_ad_userPassword );
5293 if ( a && a->a_vals[0].bv_val[0] == '{' &&
5294 lutil_passwd_scheme( a->a_vals[0].bv_val )) {
5297 be_entry_release_r( &op2, e );
5301 rc = overlay_op_walk( op, rs, op_extended, on->on_info,
5303 if ( rc == LDAP_SUCCESS ) {
5304 req_pwdexop_s *qpw = &op->oq_pwdexop;
5306 /* We don't care if it succeeds or not */
5307 pc_setpw( &op2, &qpw->rs_new, cm );
5312 return SLAP_CB_CONTINUE;
5316 pcache_entry_release( Operation *op, Entry *e, int rw )
5318 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
5319 cache_manager *cm = on->on_bi.bi_private;
5320 BackendDB *db = op->o_bd;
5324 rc = be_entry_release_rw( op, e, rw );
5329 #ifdef PCACHE_MONITOR
5332 pcache_monitor_update(
5338 cache_manager *cm = (cache_manager *) priv;
5339 query_manager *qm = cm->qm;
5342 BerVarray vals = NULL;
5344 attr_delete( &e->e_attrs, ad_cachedQueryURL );
5345 if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || ad_inlist( ad_cachedQueryURL, rs->sr_attrs ) )
5346 && qm->templates != NULL )
5350 for ( tm = qm->templates; tm != NULL; tm = tm->qmnext ) {
5351 for ( qc = tm->query; qc; qc = qc->next ) {
5354 if ( query2url( op, qc, &bv, 1 ) == 0 ) {
5355 ber_bvarray_add_x( &vals, &bv, op->o_tmpmemctx );
5361 if ( vals != NULL ) {
5362 attr_merge_normalize( e, ad_cachedQueryURL, vals, NULL );
5363 ber_bvarray_free_x( vals, op->o_tmpmemctx );
5369 char buf[ SLAP_TEXT_BUFLEN ];
5372 /* number of cached queries */
5373 a = attr_find( e->e_attrs, ad_numQueries );
5374 assert( a != NULL );
5377 bv.bv_len = snprintf( buf, sizeof( buf ), "%lu", cm->num_cached_queries );
5379 if ( a->a_nvals != a->a_vals ) {
5380 ber_bvreplace( &a->a_nvals[ 0 ], &bv );
5382 ber_bvreplace( &a->a_vals[ 0 ], &bv );
5384 /* number of cached entries */
5385 a = attr_find( e->e_attrs, ad_numEntries );
5386 assert( a != NULL );
5389 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", cm->cur_entries );
5391 if ( a->a_nvals != a->a_vals ) {
5392 ber_bvreplace( &a->a_nvals[ 0 ], &bv );
5394 ber_bvreplace( &a->a_vals[ 0 ], &bv );
5397 return SLAP_CB_CONTINUE;
5401 pcache_monitor_free(
5405 struct berval values[ 2 ];
5406 Modification mod = { 0 };
5409 char textbuf[ SLAP_TEXT_BUFLEN ];
5413 /* NOTE: if slap_shutdown != 0, priv might have already been freed */
5416 /* Remove objectClass */
5417 mod.sm_op = LDAP_MOD_DELETE;
5418 mod.sm_desc = slap_schema.si_ad_objectClass;
5419 mod.sm_values = values;
5421 values[ 0 ] = oc_olmPCache->soc_cname;
5422 BER_BVZERO( &values[ 1 ] );
5424 rc = modify_delete_values( e, &mod, 1, &text,
5425 textbuf, sizeof( textbuf ) );
5426 /* don't care too much about return code... */
5429 mod.sm_values = NULL;
5430 mod.sm_desc = ad_cachedQueryURL;
5432 rc = modify_delete_values( e, &mod, 1, &text,
5433 textbuf, sizeof( textbuf ) );
5434 /* don't care too much about return code... */
5437 mod.sm_values = NULL;
5438 mod.sm_desc = ad_numQueries;
5440 rc = modify_delete_values( e, &mod, 1, &text,
5441 textbuf, sizeof( textbuf ) );
5442 /* don't care too much about return code... */
5445 mod.sm_values = NULL;
5446 mod.sm_desc = ad_numEntries;
5448 rc = modify_delete_values( e, &mod, 1, &text,
5449 textbuf, sizeof( textbuf ) );
5450 /* don't care too much about return code... */
5452 return SLAP_CB_CONTINUE;
5456 * call from within pcache_initialize()
5459 pcache_monitor_initialize( void )
5461 static int pcache_monitor_initialized = 0;
5463 if ( backend_info( "monitor" ) == NULL ) {
5467 if ( pcache_monitor_initialized++ ) {
5475 pcache_monitor_db_init( BackendDB *be )
5477 if ( pcache_monitor_initialize() == LDAP_SUCCESS ) {
5478 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING;
5485 pcache_monitor_db_open( BackendDB *be )
5487 slap_overinst *on = (slap_overinst *)be->bd_info;
5488 cache_manager *cm = on->on_bi.bi_private;
5489 Attribute *a, *next;
5490 monitor_callback_t *cb = NULL;
5493 monitor_extra_t *mbe;
5494 struct berval dummy = BER_BVC( "" );
5496 if ( !SLAP_DBMONITORING( be ) ) {
5500 mi = backend_info( "monitor" );
5501 if ( !mi || !mi->bi_extra ) {
5502 SLAP_DBFLAGS( be ) ^= SLAP_DBFLAG_MONITORING;
5507 /* don't bother if monitor is not configured */
5508 if ( !mbe->is_configured() ) {
5509 static int warning = 0;
5511 if ( warning++ == 0 ) {
5512 Debug( LDAP_DEBUG_ANY, "pcache_monitor_db_open: "
5513 "monitoring disabled; "
5514 "configure monitor database to enable\n",
5521 /* alloc as many as required (plus 1 for objectClass) */
5522 a = attrs_alloc( 1 + 2 );
5528 a->a_desc = slap_schema.si_ad_objectClass;
5529 attr_valadd( a, &oc_olmPCache->soc_cname, NULL, 1 );
5533 struct berval bv = BER_BVC( "0" );
5535 next->a_desc = ad_numQueries;
5536 attr_valadd( next, &bv, NULL, 1 );
5537 next = next->a_next;
5539 next->a_desc = ad_numEntries;
5540 attr_valadd( next, &bv, NULL, 1 );
5541 next = next->a_next;
5544 cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
5545 cb->mc_update = pcache_monitor_update;
5546 cb->mc_free = pcache_monitor_free;
5547 cb->mc_private = (void *)cm;
5549 /* make sure the database is registered; then add monitor attributes */
5550 BER_BVZERO( &cm->monitor_ndn );
5551 rc = mbe->register_overlay( be, on, &cm->monitor_ndn );
5553 rc = mbe->register_entry_attrs( &cm->monitor_ndn, a, cb,
5554 &dummy, -1, &dummy);
5570 /* store for cleanup */
5571 cm->monitor_cb = (void *)cb;
5573 /* we don't need to keep track of the attributes, because
5574 * bdb_monitor_free() takes care of everything */
5583 pcache_monitor_db_close( BackendDB *be )
5585 slap_overinst *on = (slap_overinst *)be->bd_info;
5586 cache_manager *cm = on->on_bi.bi_private;
5588 if ( cm->monitor_cb != NULL ) {
5589 BackendInfo *mi = backend_info( "monitor" );
5590 monitor_extra_t *mbe;
5592 if ( mi && &mi->bi_extra ) {
5594 mbe->unregister_entry_callback( &cm->monitor_ndn,
5595 (monitor_callback_t *)cm->monitor_cb,
5604 pcache_monitor_db_destroy( BackendDB *be )
5609 #endif /* PCACHE_MONITOR */
5611 static slap_overinst pcache;
5613 static char *obsolete_names[] = {
5618 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
5620 #endif /* SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC */
5625 struct berval debugbv = BER_BVC("pcache");
5629 code = slap_loglevel_get( &debugbv, &pcache_debug );
5634 #ifdef PCACHE_CONTROL_PRIVDB
5635 code = register_supported_control( PCACHE_CONTROL_PRIVDB,
5636 SLAP_CTRL_BIND|SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE, extops,
5637 parse_privdb_ctrl, &privDB_cid );
5638 if ( code != LDAP_SUCCESS ) {
5639 Debug( LDAP_DEBUG_ANY,
5640 "pcache_initialize: failed to register control %s (%d)\n",
5641 PCACHE_CONTROL_PRIVDB, code, 0 );
5644 #endif /* PCACHE_CONTROL_PRIVDB */
5646 #ifdef PCACHE_EXOP_QUERY_DELETE
5647 code = load_extop2( (struct berval *)&pcache_exop_QUERY_DELETE,
5648 SLAP_EXOP_WRITES|SLAP_EXOP_HIDE, pcache_exop_query_delete,
5650 if ( code != LDAP_SUCCESS ) {
5651 Debug( LDAP_DEBUG_ANY,
5652 "pcache_initialize: unable to register queryDelete exop: %d.\n",
5656 #endif /* PCACHE_EXOP_QUERY_DELETE */
5658 argv[ 0 ] = "back-bdb/back-hdb monitor";
5663 for ( i = 0; s_oid[ i ].name; i++ ) {
5665 argv[ 1 ] = s_oid[ i ].name;
5666 argv[ 2 ] = s_oid[ i ].oid;
5668 if ( parse_oidm( &c, 0, NULL ) != 0 ) {
5669 Debug( LDAP_DEBUG_ANY, "pcache_initialize: "
5670 "unable to add objectIdentifier \"%s=%s\"\n",
5671 s_oid[ i ].name, s_oid[ i ].oid, 0 );
5676 for ( i = 0; s_ad[i].desc != NULL; i++ ) {
5677 code = register_at( s_ad[i].desc, s_ad[i].adp, 0 );
5679 Debug( LDAP_DEBUG_ANY,
5680 "pcache_initialize: register_at #%d failed\n", i, 0, 0 );
5683 (*s_ad[i].adp)->ad_type->sat_flags |= SLAP_AT_HIDE;
5686 for ( i = 0; s_oc[i].desc != NULL; i++ ) {
5687 code = register_oc( s_oc[i].desc, s_oc[i].ocp, 0 );
5689 Debug( LDAP_DEBUG_ANY,
5690 "pcache_initialize: register_oc #%d failed\n", i, 0, 0 );
5693 (*s_oc[i].ocp)->soc_flags |= SLAP_OC_HIDE;
5696 pcache.on_bi.bi_type = "pcache";
5697 pcache.on_bi.bi_obsolete_names = obsolete_names;
5698 pcache.on_bi.bi_db_init = pcache_db_init;
5699 pcache.on_bi.bi_db_config = pcache_db_config;
5700 pcache.on_bi.bi_db_open = pcache_db_open;
5701 pcache.on_bi.bi_db_close = pcache_db_close;
5702 pcache.on_bi.bi_db_destroy = pcache_db_destroy;
5704 pcache.on_bi.bi_op_search = pcache_op_search;
5705 pcache.on_bi.bi_op_bind = pcache_op_bind;
5706 #ifdef PCACHE_CONTROL_PRIVDB
5707 pcache.on_bi.bi_op_compare = pcache_op_privdb;
5708 pcache.on_bi.bi_op_modrdn = pcache_op_privdb;
5709 pcache.on_bi.bi_op_modify = pcache_op_privdb;
5710 pcache.on_bi.bi_op_add = pcache_op_privdb;
5711 pcache.on_bi.bi_op_delete = pcache_op_privdb;
5712 #endif /* PCACHE_CONTROL_PRIVDB */
5713 pcache.on_bi.bi_extended = pcache_op_extended;
5715 pcache.on_bi.bi_entry_release_rw = pcache_entry_release;
5716 pcache.on_bi.bi_chk_controls = pcache_chk_controls;
5718 pcache.on_bi.bi_cf_ocs = pcocs;
5720 code = config_register_schema( pccfg, pcocs );
5721 if ( code ) return code;
5723 return overlay_register( &pcache );
5726 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
5727 int init_module(int argc, char *argv[]) {
5728 return pcache_initialize();
5732 #endif /* defined(SLAPD_OVER_PROXYCACHE) */