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 )
1254 typedef struct fstack {
1255 struct fstack *fs_next;
1260 static CachedQuery *
1261 find_filter( Operation *op, Avlnode *root, Filter *inputf, Filter *first )
1265 MatchingRule* mrule = NULL;
1266 int res=0, eqpass= 0;
1269 CachedQuery cq, *qc;
1270 fstack *stack = NULL, *fsp;
1275 /* substring matches sort to the end, and we just have to
1276 * walk the entire list.
1278 if ( first->f_choice == LDAP_FILTER_SUBSTRINGS ) {
1279 ptr = tavl_end( root, 1 );
1280 dir = TAVL_DIR_LEFT;
1282 ptr = tavl_find3( root, &cq, pcache_query_cmp, &ret );
1283 dir = (first->f_choice == LDAP_FILTER_GE) ? TAVL_DIR_LEFT :
1292 /* an incoming substr query can only be satisfied by a cached
1295 if ( first->f_choice == LDAP_FILTER_SUBSTRINGS &&
1296 qc->first->f_choice != LDAP_FILTER_SUBSTRINGS )
1299 /* an incoming eq query can be satisfied by a cached eq or substr
1302 if ( first->f_choice == LDAP_FILTER_EQUALITY ) {
1303 if ( eqpass == 0 ) {
1304 if ( qc->first->f_choice != LDAP_FILTER_EQUALITY ) {
1305 nextpass: eqpass = 1;
1306 ptr = tavl_end( root, 1 );
1307 dir = TAVL_DIR_LEFT;
1311 if ( qc->first->f_choice != LDAP_FILTER_SUBSTRINGS )
1317 switch (fs->f_choice) {
1318 case LDAP_FILTER_EQUALITY:
1319 if (fi->f_choice == LDAP_FILTER_EQUALITY)
1320 mrule = fs->f_ava->aa_desc->ad_type->sat_equality;
1324 case LDAP_FILTER_GE:
1325 case LDAP_FILTER_LE:
1326 mrule = fs->f_ava->aa_desc->ad_type->sat_ordering;
1333 rc = value_match(&ret, fs->f_ava->aa_desc, mrule,
1334 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
1335 &(fi->f_ava->aa_value),
1336 &(fs->f_ava->aa_value), &text);
1337 if (rc != LDAP_SUCCESS) {
1340 if ( fi==first && fi->f_choice==LDAP_FILTER_EQUALITY && ret )
1343 switch (fs->f_choice) {
1344 case LDAP_FILTER_OR:
1345 case LDAP_FILTER_AND:
1348 /* save our stack position */
1349 fsp = op->o_tmpalloc(sizeof(fstack), op->o_tmpmemctx);
1350 fsp->fs_next = stack;
1351 fsp->fs_fs = fs->f_next;
1352 fsp->fs_fi = fi->f_next;
1356 case LDAP_FILTER_SUBSTRINGS:
1357 /* check if the equality query can be
1358 * answered with cached substring query */
1359 if ((fi->f_choice == LDAP_FILTER_EQUALITY)
1360 && substr_containment_equality( op,
1363 /* check if the substring query can be
1364 * answered with cached substring query */
1365 if ((fi->f_choice ==LDAP_FILTER_SUBSTRINGS
1366 ) && substr_containment_substr( op,
1372 case LDAP_FILTER_PRESENT:
1377 case LDAP_FILTER_EQUALITY:
1383 case LDAP_FILTER_GE:
1384 if (mrule && ret >= 0)
1389 case LDAP_FILTER_LE:
1390 if (mrule && ret <= 0)
1395 case LDAP_FILTER_NOT:
1401 if (!fs && !fi && stack) {
1403 stack = fsp->fs_next;
1406 op->o_tmpfree(fsp, op->o_tmpmemctx);
1408 } while((res) && (fi != NULL) && (fs != NULL));
1412 ptr = tavl_next( ptr, dir );
1417 /* check whether query is contained in any of
1418 * the cached queries in template
1420 static CachedQuery *
1421 query_containment(Operation *op, query_manager *qm,
1423 QueryTemplate *templa)
1426 int depth = 0, tscope;
1427 Qbase qbase, *qbptr = NULL;
1430 if (query->filter != NULL) {
1433 Debug( pcache_debug, "Lock QC index = %p\n",
1434 (void *) templa, 0, 0 );
1435 qbase.base = query->base;
1437 first = filter_first( query->filter );
1439 ldap_pvt_thread_rdwr_rlock(&templa->t_rwlock);
1442 qbptr = avl_find( templa->qbase, &qbase, pcache_dn_cmp );
1444 tscope = query->scope;
1445 /* Find a matching scope:
1446 * match at depth 0 OK
1449 * subord at depth > 0 OK
1450 * subtree at any depth OK
1452 * subtree or subord at any depth OK
1454 * subtree or subord at any depth OK
1456 * subord at depth > 0 OK
1457 * subtree at any depth OK
1459 for ( tscope = 0 ; tscope <= LDAP_SCOPE_CHILDREN; tscope++ ) {
1460 switch ( query->scope ) {
1461 case LDAP_SCOPE_BASE:
1462 if ( tscope == LDAP_SCOPE_BASE && depth ) continue;
1463 if ( tscope == LDAP_SCOPE_ONE && depth != 1) continue;
1464 if ( tscope == LDAP_SCOPE_CHILDREN && !depth ) continue;
1466 case LDAP_SCOPE_ONE:
1467 if ( tscope == LDAP_SCOPE_BASE )
1468 tscope = LDAP_SCOPE_ONE;
1469 if ( tscope == LDAP_SCOPE_ONE && depth ) continue;
1470 if ( !depth ) break;
1471 if ( tscope < LDAP_SCOPE_SUBTREE )
1472 tscope = LDAP_SCOPE_SUBTREE;
1474 case LDAP_SCOPE_SUBTREE:
1475 if ( tscope < LDAP_SCOPE_SUBTREE )
1476 tscope = LDAP_SCOPE_SUBTREE;
1477 if ( tscope == LDAP_SCOPE_CHILDREN && !depth ) continue;
1479 case LDAP_SCOPE_CHILDREN:
1480 if ( tscope < LDAP_SCOPE_SUBTREE )
1481 tscope = LDAP_SCOPE_SUBTREE;
1484 if ( !qbptr->scopes[tscope] ) continue;
1487 qc = find_filter( op, qbptr->scopes[tscope],
1488 query->filter, first );
1490 if ( qc->q_sizelimit ) {
1491 ldap_pvt_thread_rdwr_runlock(&templa->t_rwlock);
1494 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1495 if (qm->lru_top != qc) {
1496 remove_query(qm, qc);
1497 add_query_on_top(qm, qc);
1499 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1504 if ( be_issuffix( op->o_bd, &qbase.base ))
1507 dnParent( &qbase.base, &pdn );
1512 Debug( pcache_debug,
1513 "Not answerable: Unlock QC index=%p\n",
1514 (void *) templa, 0, 0 );
1515 ldap_pvt_thread_rdwr_runlock(&templa->t_rwlock);
1521 free_query (CachedQuery* qc)
1523 free(qc->q_uuid.bv_val);
1524 filter_free(qc->filter);
1525 ldap_pvt_thread_mutex_destroy(&qc->answerable_cnt_mutex);
1526 ldap_pvt_thread_rdwr_destroy( &qc->rwlock );
1527 memset(qc, 0, sizeof(*qc));
1532 /* Add query to query cache, the returned Query is locked for writing */
1533 static CachedQuery *
1538 QueryTemplate *templ,
1539 pc_caching_reason_t why,
1542 CachedQuery* new_cached_query = (CachedQuery*) ch_malloc(sizeof(CachedQuery));
1546 time_t ttl = 0, ttr = 0;
1549 new_cached_query->qtemp = templ;
1550 BER_BVZERO( &new_cached_query->q_uuid );
1551 new_cached_query->q_sizelimit = 0;
1553 now = slap_get_time();
1558 ttr = now + templ->ttr;
1562 ttl = templ->negttl;
1566 ttl = templ->limitttl;
1573 new_cached_query->expiry_time = now + ttl;
1574 new_cached_query->refresh_time = ttr;
1575 new_cached_query->bindref_time = 0;
1577 new_cached_query->bind_refcnt = 0;
1578 new_cached_query->answerable_cnt = 0;
1579 new_cached_query->refcnt = 1;
1580 ldap_pvt_thread_mutex_init(&new_cached_query->answerable_cnt_mutex);
1582 new_cached_query->lru_up = NULL;
1583 new_cached_query->lru_down = NULL;
1584 Debug( pcache_debug, "Added query expires at %ld (%s)\n",
1585 (long) new_cached_query->expiry_time,
1586 pc_caching_reason_str[ why ], 0 );
1588 new_cached_query->scope = query->scope;
1589 new_cached_query->filter = query->filter;
1590 new_cached_query->first = first = filter_first( query->filter );
1592 ldap_pvt_thread_rdwr_init(&new_cached_query->rwlock);
1594 ldap_pvt_thread_rdwr_wlock(&new_cached_query->rwlock);
1596 qb.base = query->base;
1598 /* Adding a query */
1599 Debug( pcache_debug, "Lock AQ index = %p\n",
1600 (void *) templ, 0, 0 );
1601 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
1602 qbase = avl_find( templ->qbase, &qb, pcache_dn_cmp );
1604 qbase = ch_calloc( 1, sizeof(Qbase) + qb.base.bv_len + 1 );
1605 qbase->base.bv_len = qb.base.bv_len;
1606 qbase->base.bv_val = (char *)(qbase+1);
1607 memcpy( qbase->base.bv_val, qb.base.bv_val, qb.base.bv_len );
1608 qbase->base.bv_val[qbase->base.bv_len] = '\0';
1609 avl_insert( &templ->qbase, qbase, pcache_dn_cmp, avl_dup_error );
1611 new_cached_query->next = templ->query;
1612 new_cached_query->prev = NULL;
1613 new_cached_query->qbase = qbase;
1614 rc = tavl_insert( &qbase->scopes[query->scope], new_cached_query,
1615 pcache_query_cmp, avl_dup_error );
1618 if (templ->query == NULL)
1619 templ->query_last = new_cached_query;
1621 templ->query->prev = new_cached_query;
1622 templ->query = new_cached_query;
1623 templ->no_of_queries++;
1625 ldap_pvt_thread_mutex_destroy(&new_cached_query->answerable_cnt_mutex);
1627 ldap_pvt_thread_rdwr_wunlock(&new_cached_query->rwlock);
1628 ldap_pvt_thread_rdwr_destroy( &new_cached_query->rwlock );
1629 ch_free( new_cached_query );
1630 new_cached_query = find_filter( op, qbase->scopes[query->scope],
1631 query->filter, first );
1632 filter_free( query->filter );
1633 query->filter = NULL;
1635 Debug( pcache_debug, "TEMPLATE %p QUERIES++ %d\n",
1636 (void *) templ, templ->no_of_queries, 0 );
1638 /* Adding on top of LRU list */
1640 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1641 add_query_on_top(qm, new_cached_query);
1642 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1644 Debug( pcache_debug, "Unlock AQ index = %p \n",
1645 (void *) templ, 0, 0 );
1646 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
1648 return rc == 0 ? new_cached_query : NULL;
1652 remove_from_template (CachedQuery* qc, QueryTemplate* template)
1654 if (!qc->prev && !qc->next) {
1655 template->query_last = template->query = NULL;
1656 } else if (qc->prev == NULL) {
1657 qc->next->prev = NULL;
1658 template->query = qc->next;
1659 } else if (qc->next == NULL) {
1660 qc->prev->next = NULL;
1661 template->query_last = qc->prev;
1663 qc->next->prev = qc->prev;
1664 qc->prev->next = qc->next;
1666 tavl_delete( &qc->qbase->scopes[qc->scope], qc, pcache_query_cmp );
1667 qc->qbase->queries--;
1668 if ( qc->qbase->queries == 0 ) {
1669 avl_delete( &template->qbase, qc->qbase, pcache_dn_cmp );
1670 ch_free( qc->qbase );
1674 template->no_of_queries--;
1677 /* remove bottom query of LRU list from the query cache */
1679 * NOTE: slight change in functionality.
1681 * - if result->bv_val is NULL, the query at the bottom of the LRU
1683 * - otherwise, the query whose UUID is *result is removed
1684 * - if not found, result->bv_val is zeroed
1687 cache_replacement(query_manager* qm, struct berval *result)
1689 CachedQuery* bottom;
1690 QueryTemplate *temp;
1692 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1693 if ( BER_BVISNULL( result ) ) {
1694 bottom = qm->lru_bottom;
1697 Debug ( pcache_debug,
1698 "Cache replacement invoked without "
1699 "any query in LRU list\n", 0, 0, 0 );
1700 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1705 for ( bottom = qm->lru_bottom;
1707 bottom = bottom->lru_up )
1709 if ( bvmatch( result, &bottom->q_uuid ) ) {
1715 Debug ( pcache_debug,
1716 "Could not find query with uuid=\"%s\""
1717 "in LRU list\n", result->bv_val, 0, 0 );
1718 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1719 BER_BVZERO( result );
1724 temp = bottom->qtemp;
1725 remove_query(qm, bottom);
1726 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1728 *result = bottom->q_uuid;
1729 BER_BVZERO( &bottom->q_uuid );
1731 Debug( pcache_debug, "Lock CR index = %p\n", (void *) temp, 0, 0 );
1732 ldap_pvt_thread_rdwr_wlock(&temp->t_rwlock);
1733 remove_from_template(bottom, temp);
1734 Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
1735 (void *) temp, temp->no_of_queries, 0 );
1736 Debug( pcache_debug, "Unlock CR index = %p\n", (void *) temp, 0, 0 );
1737 ldap_pvt_thread_rdwr_wunlock(&temp->t_rwlock);
1742 struct query_info *next;
1754 struct query_info *qi;
1757 if ( rs->sr_type != REP_SEARCH ) return 0;
1759 attr = attr_find( rs->sr_entry->e_attrs, ad_queryId );
1760 if ( attr == NULL ) return 0;
1762 count = attr->a_numvals;
1763 assert( count > 0 );
1764 qi = op->o_tmpalloc( sizeof( struct query_info ), op->o_tmpmemctx );
1765 qi->next = op->o_callback->sc_private;
1766 op->o_callback->sc_private = qi;
1767 ber_dupbv_x( &qi->xdn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1768 qi->del = ( count == 1 );
1776 struct berval *query_uuid )
1778 struct query_info *qi, *qnext;
1779 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(pcacheQueryID=)" ) ];
1780 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
1781 Filter filter = {LDAP_FILTER_EQUALITY};
1782 SlapReply sreply = {REP_RESULT};
1783 slap_callback cb = { NULL, remove_func, NULL, NULL };
1786 op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
1787 "(%s=%s)", ad_queryId->ad_cname.bv_val, query_uuid->bv_val);
1788 filter.f_ava = &ava;
1789 filter.f_av_desc = ad_queryId;
1790 filter.f_av_value = *query_uuid;
1792 op->o_tag = LDAP_REQ_SEARCH;
1793 op->o_protocol = LDAP_VERSION3;
1794 op->o_callback = &cb;
1795 op->o_time = slap_get_time();
1796 op->o_do_not_cache = 1;
1798 op->o_req_dn = op->o_bd->be_suffix[0];
1799 op->o_req_ndn = op->o_bd->be_nsuffix[0];
1800 op->ors_scope = LDAP_SCOPE_SUBTREE;
1801 op->ors_deref = LDAP_DEREF_NEVER;
1802 op->ors_slimit = SLAP_NO_LIMIT;
1803 op->ors_tlimit = SLAP_NO_LIMIT;
1804 op->ors_limit = NULL;
1805 op->ors_filter = &filter;
1806 op->ors_filterstr.bv_val = filter_str;
1807 op->ors_filterstr.bv_len = strlen(filter_str);
1808 op->ors_attrs = NULL;
1809 op->ors_attrsonly = 0;
1811 op->o_bd->be_search( op, &sreply );
1813 for ( qi=cb.sc_private; qi; qi=qnext ) {
1816 op->o_req_dn = qi->xdn;
1817 op->o_req_ndn = qi->xdn;
1818 rs_reinit( &sreply, REP_RESULT );
1821 Debug( pcache_debug, "DELETING ENTRY TEMPLATE=%s\n",
1822 query_uuid->bv_val, 0, 0 );
1824 op->o_tag = LDAP_REQ_DELETE;
1826 if (op->o_bd->be_delete(op, &sreply) == LDAP_SUCCESS) {
1832 struct berval vals[2];
1834 vals[0] = *query_uuid;
1835 vals[1].bv_val = NULL;
1837 mod.sml_op = LDAP_MOD_DELETE;
1839 mod.sml_desc = ad_queryId;
1840 mod.sml_type = ad_queryId->ad_cname;
1841 mod.sml_values = vals;
1842 mod.sml_nvalues = NULL;
1843 mod.sml_numvals = 1;
1844 mod.sml_next = NULL;
1845 Debug( pcache_debug,
1846 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
1847 query_uuid->bv_val, 0, 0 );
1849 op->orm_modlist = &mod;
1851 op->o_bd->be_modify( op, &sreply );
1853 op->o_tmpfree( qi->xdn.bv_val, op->o_tmpmemctx );
1854 op->o_tmpfree( qi, op->o_tmpmemctx );
1861 AttributeName* attrs,
1870 struct berval *fstr )
1872 AttributeDescription *ad;
1875 switch ( f->f_choice ) {
1876 case LDAP_FILTER_EQUALITY:
1878 len = STRLENOF( "(=)" ) + ad->ad_cname.bv_len;
1879 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=)", ad->ad_cname.bv_val );
1880 assert( ret == len );
1881 fstr->bv_len += len;
1884 case LDAP_FILTER_GE:
1886 len = STRLENOF( "(>=)" ) + ad->ad_cname.bv_len;
1887 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s>=)", ad->ad_cname.bv_val);
1888 assert( ret == len );
1889 fstr->bv_len += len;
1892 case LDAP_FILTER_LE:
1894 len = STRLENOF( "(<=)" ) + ad->ad_cname.bv_len;
1895 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s<=)", ad->ad_cname.bv_val);
1896 assert( ret == len );
1897 fstr->bv_len += len;
1900 case LDAP_FILTER_APPROX:
1902 len = STRLENOF( "(~=)" ) + ad->ad_cname.bv_len;
1903 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s~=)", ad->ad_cname.bv_val);
1904 assert( ret == len );
1905 fstr->bv_len += len;
1908 case LDAP_FILTER_SUBSTRINGS:
1910 len = STRLENOF( "(=)" ) + ad->ad_cname.bv_len;
1911 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=)", ad->ad_cname.bv_val );
1912 assert( ret == len );
1913 fstr->bv_len += len;
1916 case LDAP_FILTER_PRESENT:
1918 len = STRLENOF( "(=*)" ) + ad->ad_cname.bv_len;
1919 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=*)", ad->ad_cname.bv_val );
1920 assert( ret == len );
1921 fstr->bv_len += len;
1924 case LDAP_FILTER_AND:
1925 case LDAP_FILTER_OR:
1926 case LDAP_FILTER_NOT: {
1928 fstr->bv_val[fstr->bv_len++] = '(';
1929 switch ( f->f_choice ) {
1930 case LDAP_FILTER_AND:
1931 fstr->bv_val[fstr->bv_len] = '&';
1933 case LDAP_FILTER_OR:
1934 fstr->bv_val[fstr->bv_len] = '|';
1936 case LDAP_FILTER_NOT:
1937 fstr->bv_val[fstr->bv_len] = '!';
1942 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1943 rc = filter2template( op, f, fstr );
1946 fstr->bv_val[fstr->bv_len++] = ')';
1947 fstr->bv_val[fstr->bv_len] = '\0';
1953 /* a filter should at least have room for "()",
1954 * an "=" and for a 1-char attr */
1955 strcpy( fstr->bv_val, "(?=)" );
1956 fstr->bv_len += STRLENOF("(?=)");
1963 #define BI_HASHED 0x01
1964 #define BI_DIDCB 0x02
1965 #define BI_LOOKUP 0x04
1969 typedef struct bindinfo {
1970 cache_manager *bi_cm;
1972 QueryTemplate *bi_templ;
1973 struct search_info *bi_si;
1975 slap_callback bi_cb;
1978 struct search_info {
1981 QueryTemplate *qtemp;
1982 AttributeName* save_attrs; /* original attributes, saved for response */
1983 int swap_saved_attrs;
1988 int slimit_exceeded;
1989 pc_caching_reason_t caching_reason;
1995 remove_query_and_data(
1998 struct berval *uuid )
2000 query_manager* qm = cm->qm;
2002 qm->crfunc( qm, uuid );
2003 if ( !BER_BVISNULL( uuid ) ) {
2006 Debug( pcache_debug,
2007 "Removing query UUID %s\n",
2008 uuid->bv_val, 0, 0 );
2009 return_val = remove_query_data( op, uuid );
2010 Debug( pcache_debug,
2011 "QUERY REMOVED, SIZE=%d\n",
2013 ldap_pvt_thread_mutex_lock( &cm->cache_mutex );
2014 cm->cur_entries -= return_val;
2015 cm->num_cached_queries--;
2016 Debug( pcache_debug,
2017 "STORED QUERIES = %lu\n",
2018 cm->num_cached_queries, 0, 0 );
2019 ldap_pvt_thread_mutex_unlock( &cm->cache_mutex );
2020 Debug( pcache_debug,
2021 "QUERY REMOVED, CACHE ="
2023 cm->cur_entries, 0, 0 );
2028 * Callback used to fetch queryId values based on entryUUID;
2029 * used by pcache_remove_entries_from_cache()
2032 fetch_queryId_cb( Operation *op, SlapReply *rs )
2036 /* only care about searchEntry responses */
2037 if ( rs->sr_type != REP_SEARCH ) {
2041 /* allow only one response per entryUUID */
2042 if ( op->o_callback->sc_private != NULL ) {
2048 /* copy all queryId values into callback's private data */
2049 a = attr_find( rs->sr_entry->e_attrs, ad_queryId );
2051 BerVarray vals = NULL;
2053 ber_bvarray_dup_x( &vals, a->a_nvals, op->o_tmpmemctx );
2054 op->o_callback->sc_private = (void *)vals;
2058 /* clear entry if required */
2059 rs_flush_entry( op, rs, (slap_overinst *) op->o_bd->bd_info );
2065 * Call that allows to remove a set of entries from the cache,
2066 * by forcing the removal of all the related queries.
2069 pcache_remove_entries_from_cache(
2072 BerVarray entryUUIDs )
2074 Connection conn = { 0 };
2075 OperationBuffer opbuf;
2077 slap_callback sc = { 0 };
2079 char filtbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(entryUUID=)" ) ];
2080 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2081 AttributeName attrs[ 2 ] = {{{ 0 }}};
2085 void *thrctx = ldap_pvt_thread_pool_context();
2087 connection_fake_init( &conn, &opbuf, thrctx );
2095 memset( &op->oq_search, 0, sizeof( op->oq_search ) );
2096 op->ors_scope = LDAP_SCOPE_SUBTREE;
2097 op->ors_deref = LDAP_DEREF_NEVER;
2098 f.f_choice = LDAP_FILTER_EQUALITY;
2100 ava.aa_desc = slap_schema.si_ad_entryUUID;
2101 op->ors_filter = &f;
2103 op->ors_tlimit = SLAP_NO_LIMIT;
2104 op->ors_limit = NULL;
2105 attrs[ 0 ].an_desc = ad_queryId;
2106 attrs[ 0 ].an_name = ad_queryId->ad_cname;
2107 op->ors_attrs = attrs;
2108 op->ors_attrsonly = 0;
2110 op->o_req_dn = cm->db.be_suffix[ 0 ];
2111 op->o_req_ndn = cm->db.be_nsuffix[ 0 ];
2113 op->o_tag = LDAP_REQ_SEARCH;
2114 op->o_protocol = LDAP_VERSION3;
2115 op->o_managedsait = SLAP_CONTROL_CRITICAL;
2117 op->o_dn = op->o_bd->be_rootdn;
2118 op->o_ndn = op->o_bd->be_rootndn;
2119 sc.sc_response = fetch_queryId_cb;
2120 op->o_callback = ≻
2122 for ( s = 0; !BER_BVISNULL( &entryUUIDs[ s ] ); s++ ) {
2123 BerVarray vals = NULL;
2124 SlapReply rs = { REP_RESULT };
2126 op->ors_filterstr.bv_len = snprintf( filtbuf, sizeof( filtbuf ),
2127 "(entryUUID=%s)", entryUUIDs[ s ].bv_val );
2128 op->ors_filterstr.bv_val = filtbuf;
2129 ava.aa_value = entryUUIDs[ s ];
2131 rc = op->o_bd->be_search( op, &rs );
2132 if ( rc != LDAP_SUCCESS ) {
2136 vals = (BerVarray)op->o_callback->sc_private;
2137 if ( vals != NULL ) {
2140 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
2141 struct berval val = vals[ i ];
2143 remove_query_and_data( op, cm, &val );
2145 if ( !BER_BVISNULL( &val ) && val.bv_val != vals[ i ].bv_val ) {
2146 ch_free( val.bv_val );
2150 ber_bvarray_free_x( vals, op->o_tmpmemctx );
2151 op->o_callback->sc_private = NULL;
2159 * Call that allows to remove a query from the cache.
2162 pcache_remove_query_from_cache(
2165 struct berval *queryid )
2167 Operation op2 = *op;
2171 /* remove the selected query */
2172 remove_query_and_data( &op2, cm, queryid );
2174 return LDAP_SUCCESS;
2178 * Call that allows to remove a set of queries related to an entry
2179 * from the cache; if queryid is not null, the entry must belong to
2180 * the query indicated by queryid.
2183 pcache_remove_entry_queries_from_cache(
2187 struct berval *queryid )
2189 Connection conn = { 0 };
2190 OperationBuffer opbuf;
2192 slap_callback sc = { 0 };
2193 SlapReply rs = { REP_RESULT };
2195 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(pcacheQueryID=)" ) ];
2196 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2197 AttributeName attrs[ 2 ] = {{{ 0 }}};
2200 BerVarray vals = NULL;
2203 void *thrctx = ldap_pvt_thread_pool_context();
2205 connection_fake_init( &conn, &opbuf, thrctx );
2213 memset( &op->oq_search, 0, sizeof( op->oq_search ) );
2214 op->ors_scope = LDAP_SCOPE_BASE;
2215 op->ors_deref = LDAP_DEREF_NEVER;
2216 if ( queryid == NULL || BER_BVISNULL( queryid ) ) {
2217 BER_BVSTR( &op->ors_filterstr, "(objectClass=*)" );
2218 f.f_choice = LDAP_FILTER_PRESENT;
2219 f.f_desc = slap_schema.si_ad_objectClass;
2222 op->ors_filterstr.bv_len = snprintf( filter_str,
2223 sizeof( filter_str ), "(%s=%s)",
2224 ad_queryId->ad_cname.bv_val, queryid->bv_val );
2225 f.f_choice = LDAP_FILTER_EQUALITY;
2227 f.f_av_desc = ad_queryId;
2228 f.f_av_value = *queryid;
2230 op->ors_filter = &f;
2232 op->ors_tlimit = SLAP_NO_LIMIT;
2233 op->ors_limit = NULL;
2234 attrs[ 0 ].an_desc = ad_queryId;
2235 attrs[ 0 ].an_name = ad_queryId->ad_cname;
2236 op->ors_attrs = attrs;
2237 op->ors_attrsonly = 0;
2239 op->o_req_dn = *ndn;
2240 op->o_req_ndn = *ndn;
2242 op->o_tag = LDAP_REQ_SEARCH;
2243 op->o_protocol = LDAP_VERSION3;
2244 op->o_managedsait = SLAP_CONTROL_CRITICAL;
2246 op->o_dn = op->o_bd->be_rootdn;
2247 op->o_ndn = op->o_bd->be_rootndn;
2248 sc.sc_response = fetch_queryId_cb;
2249 op->o_callback = ≻
2251 rc = op->o_bd->be_search( op, &rs );
2252 if ( rc != LDAP_SUCCESS ) {
2256 vals = (BerVarray)op->o_callback->sc_private;
2257 if ( vals != NULL ) {
2260 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
2261 struct berval val = vals[ i ];
2263 remove_query_and_data( op, cm, &val );
2265 if ( !BER_BVISNULL( &val ) && val.bv_val != vals[ i ].bv_val ) {
2266 ch_free( val.bv_val );
2270 ber_bvarray_free_x( vals, op->o_tmpmemctx );
2273 return LDAP_SUCCESS;
2279 struct berval *query_uuid )
2281 struct search_info *si = op->o_callback->sc_private;
2282 slap_overinst *on = si->on;
2283 cache_manager *cm = on->on_bi.bi_private;
2286 struct berval crp_uuid;
2287 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
2289 Connection conn = {0};
2290 OperationBuffer opbuf;
2291 void *thrctx = ldap_pvt_thread_pool_context();
2293 query_uuid->bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf));
2294 ber_str2bv(uuidbuf, query_uuid->bv_len, 1, query_uuid);
2296 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
2297 op_tmp = &opbuf.ob_op;
2298 op_tmp->o_bd = &cm->db;
2299 op_tmp->o_dn = cm->db.be_rootdn;
2300 op_tmp->o_ndn = cm->db.be_rootndn;
2302 Debug( pcache_debug, "UUID for query being added = %s\n",
2305 for ( e=si->head; e; e=si->head ) {
2306 si->head = e->e_private;
2307 e->e_private = NULL;
2308 while ( cm->cur_entries > (cm->max_entries) ) {
2309 BER_BVZERO( &crp_uuid );
2310 remove_query_and_data( op_tmp, cm, &crp_uuid );
2313 return_val = merge_entry(op_tmp, e, 0, query_uuid);
2314 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2315 cm->cur_entries += return_val;
2316 Debug( pcache_debug,
2317 "ENTRY ADDED/MERGED, CACHED ENTRIES=%d\n",
2318 cm->cur_entries, 0, 0 );
2320 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2327 pcache_op_cleanup( Operation *op, SlapReply *rs ) {
2328 slap_callback *cb = op->o_callback;
2329 struct search_info *si = cb->sc_private;
2330 slap_overinst *on = si->on;
2331 cache_manager *cm = on->on_bi.bi_private;
2332 query_manager* qm = cm->qm;
2334 if ( rs->sr_type == REP_SEARCH ) {
2337 /* don't return more entries than requested by the client */
2338 if ( si->slimit > 0 && rs->sr_nentries >= si->slimit ) {
2339 si->slimit_exceeded = 1;
2342 /* If we haven't exceeded the limit for this query,
2343 * build a chain of answers to store. If we hit the
2344 * limit, empty the chain and ignore the rest.
2347 /* check if the entry contains undefined
2348 * attributes/objectClasses (ITS#5680) */
2349 if ( cm->check_cacheability && test_filter( op, rs->sr_entry, si->query.filter ) != LDAP_COMPARE_TRUE ) {
2350 Debug( pcache_debug, "%s: query not cacheable because of schema issues in DN \"%s\"\n",
2351 op->o_log_prefix, rs->sr_entry->e_name.bv_val, 0 );
2355 /* check for malformed entries: attrs with no values */
2357 Attribute *a = rs->sr_entry->e_attrs;
2358 for (; a; a=a->a_next) {
2359 if ( !a->a_numvals ) {
2360 Debug( pcache_debug, "%s: query not cacheable because of attrs without values in DN \"%s\" (%s)\n",
2361 op->o_log_prefix, rs->sr_entry->e_name.bv_val,
2362 a->a_desc->ad_cname.bv_val );
2368 if ( si->count < si->max ) {
2370 e = entry_dup( rs->sr_entry );
2371 if ( !si->head ) si->head = e;
2372 if ( si->tail ) si->tail->e_private = e;
2379 for (;si->head; si->head=e) {
2380 e = si->head->e_private;
2381 si->head->e_private = NULL;
2382 entry_free(si->head);
2390 if ( rs->sr_type == REP_RESULT ||
2391 op->o_abandon || rs->sr_err == SLAPD_ABANDON )
2393 if ( si->swap_saved_attrs ) {
2394 rs->sr_attrs = si->save_attrs;
2395 op->ors_attrs = si->save_attrs;
2397 if ( (op->o_abandon || rs->sr_err == SLAPD_ABANDON) &&
2398 si->caching_reason == PC_IGNORE )
2400 filter_free( si->query.filter );
2402 /* duplicate query, free it */
2404 for (;si->head; si->head=e) {
2405 e = si->head->e_private;
2406 si->head->e_private = NULL;
2407 entry_free(si->head);
2411 } else if ( si->caching_reason != PC_IGNORE ) {
2412 CachedQuery *qc = qm->addfunc(op, qm, &si->query,
2413 si->qtemp, si->caching_reason, 1 );
2416 switch ( si->caching_reason ) {
2418 cache_entries( op, &qc->q_uuid );
2421 si->pbi->bi_cq = qc;
2426 qc->q_sizelimit = rs->sr_nentries;
2436 ldap_pvt_thread_rdwr_wunlock(&qc->rwlock);
2437 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2438 cm->num_cached_queries++;
2439 Debug( pcache_debug, "STORED QUERIES = %lu\n",
2440 cm->num_cached_queries, 0, 0 );
2441 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2443 /* If the consistency checker suspended itself,
2446 if ( cm->cc_paused == PCACHE_CC_PAUSED ) {
2447 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2448 if ( cm->cc_paused == PCACHE_CC_PAUSED ) {
2450 ldap_pvt_runqueue_resched( &slapd_rq, cm->cc_arg, 0 );
2452 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2455 } else if ( si->count ) {
2456 /* duplicate query, free it */
2458 for (;si->head; si->head=e) {
2459 e = si->head->e_private;
2460 si->head->e_private = NULL;
2461 entry_free(si->head);
2466 filter_free( si->query.filter );
2469 op->o_callback = op->o_callback->sc_next;
2470 op->o_tmpfree( cb, op->o_tmpmemctx );
2473 return SLAP_CB_CONTINUE;
2481 struct search_info *si = op->o_callback->sc_private;
2483 if ( si->swap_saved_attrs ) {
2484 rs->sr_attrs = si->save_attrs;
2485 op->ors_attrs = si->save_attrs;
2488 if ( rs->sr_type == REP_SEARCH ) {
2489 /* don't return more entries than requested by the client */
2490 if ( si->slimit_exceeded ) {
2494 } else if ( rs->sr_type == REP_RESULT ) {
2497 if ( rs->sr_err == LDAP_SUCCESS ) {
2498 si->caching_reason = PC_POSITIVE;
2500 } else if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED
2501 && si->qtemp->limitttl )
2505 si->caching_reason = PC_SIZELIMIT;
2506 for (;si->head; si->head=e) {
2507 e = si->head->e_private;
2508 si->head->e_private = NULL;
2509 entry_free(si->head);
2513 } else if ( si->qtemp->negttl && !si->count && !si->over &&
2514 rs->sr_err == LDAP_SUCCESS )
2516 si->caching_reason = PC_NEGATIVE;
2520 if ( si->slimit_exceeded ) {
2521 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
2525 return SLAP_CB_CONTINUE;
2528 /* NOTE: this is a quick workaround to let pcache minimally interact
2529 * with pagedResults. A more articulated solutions would be to
2530 * perform the remote query without control and cache all results,
2531 * performing the pagedResults search only within the client
2532 * and the proxy. This requires pcache to understand pagedResults. */
2534 pcache_chk_controls(
2538 const char *non = "";
2539 const char *stripped = "";
2541 switch( op->o_pagedresults ) {
2542 case SLAP_CONTROL_NONCRITICAL:
2544 stripped = "; stripped";
2547 case SLAP_CONTROL_CRITICAL:
2548 Debug( pcache_debug, "%s: "
2549 "%scritical pagedResults control "
2550 "disabled with proxy cache%s.\n",
2551 op->o_log_prefix, non, stripped );
2553 slap_remove_control( op, rs, slap_cids.sc_pagedResults, NULL );
2557 rs->sr_err = SLAP_CB_CONTINUE;
2565 pc_setpw( Operation *op, struct berval *pwd, cache_manager *cm )
2567 struct berval vals[2];
2570 const char *text = NULL;
2571 BER_BVZERO( &vals[0] );
2572 slap_passwd_hash( pwd, &vals[0], &text );
2573 if ( BER_BVISEMPTY( &vals[0] )) {
2574 Debug( pcache_debug, "pc_setpw: hash failed %s\n",
2580 BER_BVZERO( &vals[1] );
2584 SlapReply sr = { REP_RESULT };
2585 slap_callback cb = { 0, slap_null_cb, 0, 0 };
2588 mod.sml_op = LDAP_MOD_REPLACE;
2590 mod.sml_desc = slap_schema.si_ad_userPassword;
2591 mod.sml_type = mod.sml_desc->ad_cname;
2592 mod.sml_values = vals;
2593 mod.sml_nvalues = NULL;
2594 mod.sml_numvals = 1;
2595 mod.sml_next = NULL;
2597 op->o_tag = LDAP_REQ_MODIFY;
2598 op->orm_modlist = &mod;
2600 op->o_dn = op->o_bd->be_rootdn;
2601 op->o_ndn = op->o_bd->be_rootndn;
2602 op->o_callback = &cb;
2603 Debug( pcache_debug, "pc_setpw: CACHING BIND for %s\n",
2604 op->o_req_dn.bv_val, 0, 0 );
2605 rc = op->o_bd->be_modify( op, &sr );
2606 ch_free( vals[0].bv_val );
2611 typedef struct bindcacheinfo {
2617 pc_bind_save( Operation *op, SlapReply *rs )
2619 if ( rs->sr_err == LDAP_SUCCESS ) {
2620 bindcacheinfo *bci = op->o_callback->sc_private;
2621 slap_overinst *on = bci->on;
2622 cache_manager *cm = on->on_bi.bi_private;
2623 CachedQuery *qc = bci->qc;
2626 ldap_pvt_thread_rdwr_wlock( &qc->rwlock );
2627 if ( qc->bind_refcnt-- ) {
2628 Operation op2 = *op;
2629 if ( pc_setpw( &op2, &op->orb_cred, cm ) == LDAP_SUCCESS )
2630 bci->qc->bindref_time = op->o_time + bci->qc->qtemp->bindttr;
2635 ldap_pvt_thread_rdwr_wunlock( &qc->rwlock );
2636 if ( delete ) free_query(qc);
2638 return SLAP_CB_CONTINUE;
2642 pc_bind_attrs( Operation *op, Entry *e, QueryTemplate *temp,
2643 struct berval *fbv )
2646 struct berval *vals, pres = BER_BVC("*");
2650 vals = op->o_tmpalloc( temp->bindnattrs * sizeof( struct berval ),
2653 for ( i=0; i<temp->bindnattrs; i++ ) {
2654 a = attr_find( e->e_attrs, temp->bindfattrs[i] );
2655 if ( a && a->a_vals ) {
2656 vals[i] = a->a_vals[0];
2657 len += a->a_vals[0].bv_len;
2662 fbv->bv_len = len + temp->bindftemp.bv_len;
2663 fbv->bv_val = op->o_tmpalloc( fbv->bv_len + 1, op->o_tmpmemctx );
2665 p1 = temp->bindftemp.bv_val;
2670 if ( p1[0] == '=' && p1[1] == ')' ) {
2671 AC_MEMCPY( p2, vals[i].bv_val, vals[i].bv_len );
2672 p2 += vals[i].bv_len;
2678 op->o_tmpfree( vals, op->o_tmpmemctx );
2680 /* FIXME: are we sure str2filter_x can't fail?
2681 * caller needs to check */
2683 Filter *f = str2filter_x( op, fbv->bv_val );
2684 assert( f != NULL );
2689 /* Check if the requested entry is from the cache and has a valid
2690 * ttr and password hash
2693 pc_bind_search( Operation *op, SlapReply *rs )
2695 if ( rs->sr_type == REP_SEARCH ) {
2696 bindinfo *pbi = op->o_callback->sc_private;
2698 /* We only care if this is an already cached result and we're
2699 * below the refresh time, or we're offline.
2702 if (( pbi->bi_cm->cc_paused & PCACHE_CC_OFFLINE ) ||
2703 op->o_time < pbi->bi_cq->bindref_time ) {
2706 /* See if a recognized password is hashed here */
2707 a = attr_find( rs->sr_entry->e_attrs,
2708 slap_schema.si_ad_userPassword );
2709 if ( a && a->a_vals[0].bv_val[0] == '{' &&
2710 lutil_passwd_scheme( a->a_vals[0].bv_val ))
2711 pbi->bi_flags |= BI_HASHED;
2713 Debug( pcache_debug, "pc_bind_search: cache is stale, "
2714 "reftime: %ld, current time: %ld\n",
2715 pbi->bi_cq->bindref_time, op->o_time, 0 );
2717 } else if ( pbi->bi_si ) {
2718 /* This search result is going into the cache */
2722 filter_free( pbi->bi_si->query.filter );
2723 f = pc_bind_attrs( op, rs->sr_entry, pbi->bi_templ, &fbv );
2724 op->o_tmpfree( fbv.bv_val, op->o_tmpmemctx );
2725 pbi->bi_si->query.filter = filter_dup( f, NULL );
2726 filter_free_x( op, f, 1 );
2732 /* We always want pc_bind_search to run after the search handlers */
2734 pc_bind_resp( Operation *op, SlapReply *rs )
2736 bindinfo *pbi = op->o_callback->sc_private;
2737 if ( !( pbi->bi_flags & BI_DIDCB )) {
2738 slap_callback *sc = op->o_callback;
2739 while ( sc && sc->sc_response != pcache_response )
2742 sc = op->o_callback;
2743 pbi->bi_cb.sc_next = sc->sc_next;
2744 sc->sc_next = &pbi->bi_cb;
2745 pbi->bi_flags |= BI_DIDCB;
2747 return SLAP_CB_CONTINUE;
2750 #ifdef PCACHE_CONTROL_PRIVDB
2756 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2757 cache_manager *cm = on->on_bi.bi_private;
2758 slap_callback *save_cb;
2761 /* skip if control is unset */
2762 if ( op->o_ctrlflag[ privDB_cid ] != SLAP_CONTROL_CRITICAL ) {
2763 return SLAP_CB_CONTINUE;
2766 /* The cache DB isn't open yet */
2767 if ( cm->defer_db_open ) {
2768 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
2769 "pcachePrivDB: cacheDB not available" );
2773 /* FIXME: might be a little bit exaggerated... */
2774 if ( !be_isroot( op ) ) {
2775 save_cb = op->o_callback;
2776 op->o_callback = NULL;
2777 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
2778 "pcachePrivDB: operation not allowed" );
2779 op->o_callback = save_cb;
2784 /* map tag to operation */
2785 type = slap_req2op( op->o_tag );
2786 if ( type != SLAP_OP_LAST ) {
2790 /* execute, if possible */
2791 func = &cm->db.be_bind;
2792 if ( func[ type ] != NULL ) {
2793 Operation op2 = *op;
2797 rc = func[ type ]( &op2, rs );
2798 if ( type == SLAP_OP_BIND && rc == LDAP_SUCCESS ) {
2799 op->o_conn->c_authz_cookie = cm->db.be_private;
2806 /* otherwise fall back to error */
2807 save_cb = op->o_callback;
2808 op->o_callback = NULL;
2809 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
2810 "operation not supported with pcachePrivDB control" );
2811 op->o_callback = save_cb;
2815 #endif /* PCACHE_CONTROL_PRIVDB */
2822 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2823 cache_manager *cm = on->on_bi.bi_private;
2824 QueryTemplate *temp;
2826 slap_callback cb = { 0 }, *sc;
2832 #ifdef PCACHE_CONTROL_PRIVDB
2833 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL )
2834 return pcache_op_privdb( op, rs );
2835 #endif /* PCACHE_CONTROL_PRIVDB */
2837 /* Skip if we're not configured for Binds, or cache DB isn't open yet */
2838 if ( !cm->cache_binds || cm->defer_db_open )
2839 return SLAP_CB_CONTINUE;
2841 /* First find a matching template with Bind info */
2842 for ( temp=cm->qm->templates; temp; temp=temp->qmnext ) {
2843 if ( temp->bindttr && dnIsSuffix( &op->o_req_ndn, &temp->bindbase ))
2846 /* Didn't find a suitable template, just passthru */
2848 return SLAP_CB_CONTINUE;
2850 /* See if the entry is already locally cached. If so, we can
2851 * populate the query filter to retrieve the cached query. We
2852 * need to check the bindrefresh time in the query.
2855 op2.o_dn = op->o_bd->be_rootdn;
2856 op2.o_ndn = op->o_bd->be_rootndn;
2861 rc = be_entry_get_rw( &op2, &op->o_req_ndn, NULL, NULL, 0, &e );
2862 if ( rc == LDAP_SUCCESS && e ) {
2863 bi.bi_flags |= BI_LOOKUP;
2864 op2.ors_filter = pc_bind_attrs( op, e, temp, &op2.ors_filterstr );
2865 be_entry_release_r( &op2, e );
2867 op2.ors_filter = temp->bindfilter;
2868 op2.ors_filterstr = temp->bindfilterstr;
2871 op2.o_bd = op->o_bd;
2872 op2.o_tag = LDAP_REQ_SEARCH;
2873 op2.ors_scope = LDAP_SCOPE_BASE;
2874 op2.ors_deref = LDAP_DEREF_NEVER;
2876 op2.ors_tlimit = SLAP_NO_LIMIT;
2877 op2.ors_limit = NULL;
2878 op2.ors_attrs = cm->qm->attr_sets[temp->attr_set_index].attrs;
2879 op2.ors_attrsonly = 0;
2881 /* We want to invoke search at the same level of the stack
2882 * as we're already at...
2889 bi.bi_cb.sc_response = pc_bind_search;
2890 bi.bi_cb.sc_cleanup = NULL;
2891 bi.bi_cb.sc_private = &bi;
2892 cb.sc_private = &bi;
2893 cb.sc_response = pc_bind_resp;
2894 op2.o_callback = &cb;
2895 overlay_op_walk( &op2, rs, op_search, on->on_info, on );
2897 /* OK, just bind locally */
2898 if ( bi.bi_flags & BI_HASHED ) {
2899 BackendDB *be = op->o_bd;
2903 Debug( pcache_debug, "pcache_op_bind: CACHED BIND for %s\n",
2904 op->o_req_dn.bv_val, 0, 0 );
2906 if ( op->o_bd->be_bind( op, rs ) == LDAP_SUCCESS ) {
2907 op->o_conn->c_authz_cookie = cm->db.be_private;
2910 ldap_pvt_thread_rdwr_wlock( &bi.bi_cq->rwlock );
2911 if ( !bi.bi_cq->bind_refcnt-- ) {
2914 ldap_pvt_thread_rdwr_wunlock( &bi.bi_cq->rwlock );
2915 if ( delete ) free_query( bi.bi_cq );
2919 /* We have a cached query to work with */
2921 sc = op->o_tmpalloc( sizeof(slap_callback) + sizeof(bindcacheinfo),
2923 sc->sc_response = pc_bind_save;
2924 sc->sc_cleanup = NULL;
2925 sc->sc_private = sc+1;
2926 bci = sc->sc_private;
2927 sc->sc_next = op->o_callback;
2928 op->o_callback = sc;
2932 return SLAP_CB_CONTINUE;
2935 static slap_response refresh_merge;
2942 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2943 cache_manager *cm = on->on_bi.bi_private;
2944 query_manager* qm = cm->qm;
2949 QueryTemplate *qtemp = NULL;
2950 bindinfo *pbi = NULL;
2953 CachedQuery *answerable = NULL;
2956 struct berval tempstr;
2958 #ifdef PCACHE_CONTROL_PRIVDB
2959 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL ) {
2960 return pcache_op_privdb( op, rs );
2962 #endif /* PCACHE_CONTROL_PRIVDB */
2964 /* The cache DB isn't open yet */
2965 if ( cm->defer_db_open ) {
2966 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
2967 "pcachePrivDB: cacheDB not available" );
2971 /* pickup runtime ACL changes */
2972 cm->db.be_acl = op->o_bd->be_acl;
2975 /* See if we're processing a Bind request
2976 * or a cache refresh */
2977 slap_callback *cb = op->o_callback;
2979 for ( ; cb; cb=cb->sc_next ) {
2980 if ( cb->sc_response == pc_bind_resp ) {
2981 pbi = cb->sc_private;
2984 if ( cb->sc_response == refresh_merge ) {
2985 /* This is a refresh, do not search the cache */
2986 return SLAP_CB_CONTINUE;
2991 /* FIXME: cannot cache/answer requests with pagedResults control */
2993 query.filter = op->ors_filter;
2996 query.base = pbi->bi_templ->bindbase;
2997 query.scope = pbi->bi_templ->bindscope;
2998 attr_set = pbi->bi_templ->attr_set_index;
3000 qtemp = pbi->bi_templ;
3001 if ( pbi->bi_flags & BI_LOOKUP )
3002 answerable = qm->qcfunc(op, qm, &query, qtemp);
3005 tempstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len+1,
3008 if ( filter2template( op, op->ors_filter, &tempstr ))
3010 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
3011 return SLAP_CB_CONTINUE;
3014 Debug( pcache_debug, "query template of incoming query = %s\n",
3015 tempstr.bv_val, 0, 0 );
3018 attr_set = get_attr_set(op->ors_attrs, qm, cm->numattrsets);
3020 query.base = op->o_req_ndn;
3021 query.scope = op->ors_scope;
3023 /* check for query containment */
3024 if (attr_set > -1) {
3025 QueryTemplate *qt = qm->attr_sets[attr_set].templates;
3026 for (; qt; qt = qt->qtnext ) {
3027 /* find if template i can potentially answer tempstr */
3028 if ( ber_bvstrcasecmp( &qt->querystr, &tempstr ) != 0 )
3032 Debug( pcache_debug, "Entering QC, querystr = %s\n",
3033 op->ors_filterstr.bv_val, 0, 0 );
3034 answerable = qm->qcfunc(op, qm, &query, qt);
3036 /* if != NULL, rlocks qtemp->t_rwlock */
3041 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
3045 BackendDB *save_bd = op->o_bd;
3047 ldap_pvt_thread_mutex_lock( &answerable->answerable_cnt_mutex );
3048 answerable->answerable_cnt++;
3049 /* we only care about refcnts if we're refreshing */
3050 if ( answerable->refresh_time )
3051 answerable->refcnt++;
3052 Debug( pcache_debug, "QUERY ANSWERABLE (answered %lu times)\n",
3053 answerable->answerable_cnt, 0, 0 );
3054 ldap_pvt_thread_mutex_unlock( &answerable->answerable_cnt_mutex );
3056 ldap_pvt_thread_rdwr_wlock(&answerable->rwlock);
3057 if ( BER_BVISNULL( &answerable->q_uuid )) {
3058 /* No entries cached, just an empty result set */
3060 send_ldap_result( op, rs );
3062 /* Let Bind know we used a cached query */
3064 answerable->bind_refcnt++;
3065 pbi->bi_cq = answerable;
3069 if ( cm->response_cb == PCACHE_RESPONSE_CB_TAIL ) {
3071 /* The cached entry was already processed by any
3072 * other overlays, so don't let it get processed again.
3074 * This loop removes over_back_response from the stack.
3076 if ( overlay_callback_after_backover( op, &cb, 0) == 0 ) {
3077 slap_callback **scp;
3078 for ( scp = &op->o_callback; *scp != NULL;
3079 scp = &(*scp)->sc_next ) {
3080 if ( (*scp)->sc_next == &cb ) {
3087 i = cm->db.bd_info->bi_op_search( op, rs );
3089 ldap_pvt_thread_rdwr_wunlock(&answerable->rwlock);
3090 /* locked by qtemp->qcfunc (query_containment) */
3091 ldap_pvt_thread_rdwr_runlock(&qtemp->t_rwlock);
3096 Debug( pcache_debug, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
3098 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
3099 if (cm->num_cached_queries >= cm->max_queries) {
3102 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
3104 if (op->ors_attrsonly)
3109 struct search_info *si;
3111 Debug( pcache_debug, "QUERY CACHEABLE\n", 0, 0, 0 );
3112 query.filter = filter_dup(op->ors_filter, NULL);
3114 cb = op->o_tmpalloc( sizeof(*cb) + sizeof(*si), op->o_tmpmemctx );
3115 cb->sc_response = pcache_response;
3116 cb->sc_cleanup = pcache_op_cleanup;
3117 cb->sc_private = (cb+1);
3118 si = cb->sc_private;
3122 si->max = cm->num_entries_limit ;
3126 si->slimit_exceeded = 0;
3127 si->caching_reason = PC_IGNORE;
3128 if ( op->ors_slimit > 0 && op->ors_slimit < cm->num_entries_limit ) {
3129 si->slimit = op->ors_slimit;
3130 op->ors_slimit = cm->num_entries_limit;
3134 si->swap_saved_attrs = 1;
3135 si->save_attrs = op->ors_attrs;
3140 op->ors_attrs = qtemp->t_attrs.attrs;
3142 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
3143 cb->sc_next = op->o_callback;
3144 op->o_callback = cb;
3147 slap_callback **pcb;
3149 /* need to move the callback at the end, in case other
3150 * overlays are present, so that the final entry is
3151 * actually cached */
3153 for ( pcb = &op->o_callback; *pcb; pcb = &(*pcb)->sc_next );
3158 Debug( pcache_debug, "QUERY NOT CACHEABLE\n",
3162 return SLAP_CB_CONTINUE;
3167 AttributeName* attrs,
3175 for ( ; attrs[i].an_name.bv_val; i++ ) {
3176 /* only count valid attribute names
3177 * (searches ignore others, this overlay does the same) */
3178 if ( attrs[i].an_desc ) {
3184 /* recognize default or explicit single "*" */
3186 ( i == 1 && bvmatch( &attrs[0].an_name, slap_bv_all_user_attrs ) ) )
3189 attrs = slap_anlist_all_user_attributes;
3191 /* recognize implicit (no valid attributes) or explicit single "1.1" */
3192 } else if ( count == 0 ||
3193 ( i == 1 && bvmatch( &attrs[0].an_name, slap_bv_no_attrs ) ) )
3199 for ( i = 0; i < num; i++ ) {
3203 if ( count > qm->attr_sets[i].count ) {
3208 if ( !qm->attr_sets[i].count ) {
3214 for ( a2 = attrs; a2->an_name.bv_val; a2++ ) {
3215 if ( !a2->an_desc && !bvmatch( &a2->an_name, slap_bv_all_user_attrs ) ) continue;
3217 if ( !an_find( qm->attr_sets[i].attrs, &a2->an_name ) ) {
3235 /* Refresh a cached query:
3236 * 1: Replay the query on the remote DB and merge each entry into
3237 * the local DB. Remember the DNs of each remote entry.
3238 * 2: Search the local DB for all entries matching this queryID.
3239 * Delete any entry whose DN is not in the list from (1).
3241 typedef struct dnlist {
3242 struct dnlist *next;
3247 typedef struct refresh_info {
3255 static dnlist *dnl_alloc( Operation *op, struct berval *bvdn )
3257 dnlist *dn = op->o_tmpalloc( sizeof(dnlist) + bvdn->bv_len + 1,
3259 dn->dn.bv_len = bvdn->bv_len;
3260 dn->dn.bv_val = (char *)(dn+1);
3261 AC_MEMCPY( dn->dn.bv_val, bvdn->bv_val, dn->dn.bv_len );
3262 dn->dn.bv_val[dn->dn.bv_len] = '\0';
3267 refresh_merge( Operation *op, SlapReply *rs )
3269 if ( rs->sr_type == REP_SEARCH ) {
3270 refresh_info *ri = op->o_callback->sc_private;
3276 ocb = op->o_callback;
3277 /* Find local entry, merge */
3278 op->o_bd = ri->ri_be;
3279 rc = be_entry_get_rw( op, &rs->sr_entry->e_nname, NULL, NULL, 0, &e );
3280 if ( rc != LDAP_SUCCESS || e == NULL ) {
3281 /* No local entry, just add it. FIXME: we are not checking
3282 * the cache entry limit here
3284 merge_entry( op, rs->sr_entry, 1, &ri->ri_q->q_uuid );
3286 /* Entry exists, update it */
3289 Modifications *modlist, *mods = NULL;
3290 const char* text = NULL;
3291 char textbuf[SLAP_TEXT_BUFLEN];
3292 size_t textlen = sizeof(textbuf);
3293 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
3297 /* Get a copy of only the attrs we requested */
3298 for ( a=e->e_attrs; a; a=a->a_next ) {
3299 if ( ad_inlist( a->a_desc, rs->sr_attrs )) {
3300 *b = attr_alloc( a->a_desc );
3302 /* The actual values still belong to e */
3303 (*b)->a_flags |= SLAP_ATTR_DONT_FREE_VALS |
3304 SLAP_ATTR_DONT_FREE_DATA;
3305 b = &((*b)->a_next);
3309 slap_entry2mods( rs->sr_entry, &modlist, &text, textbuf, textlen );
3310 syncrepl_diff_entry( op, ne.e_attrs, rs->sr_entry->e_attrs,
3311 &mods, &modlist, 0 );
3312 be_entry_release_r( op, e );
3313 attrs_free( ne.e_attrs );
3314 slap_mods_free( modlist, 1 );
3315 /* mods is NULL if there are no changes */
3317 SlapReply rs2 = { REP_RESULT };
3318 struct berval dn = op->o_req_dn;
3319 struct berval ndn = op->o_req_ndn;
3320 op->o_tag = LDAP_REQ_MODIFY;
3321 op->orm_modlist = mods;
3322 op->o_req_dn = rs->sr_entry->e_name;
3323 op->o_req_ndn = rs->sr_entry->e_nname;
3324 op->o_callback = &cb;
3325 op->o_bd->be_modify( op, &rs2 );
3326 rs->sr_err = rs2.sr_err;
3327 rs_assert_done( &rs2 );
3328 slap_mods_free( mods, 1 );
3330 op->o_req_ndn = ndn;
3334 /* Add DN to list */
3335 dnl = dnl_alloc( op, &rs->sr_entry->e_nname );
3337 if ( ri->ri_tail ) {
3338 ri->ri_tail->next = dnl;
3343 op->o_callback = ocb;
3349 refresh_purge( Operation *op, SlapReply *rs )
3351 if ( rs->sr_type == REP_SEARCH ) {
3352 refresh_info *ri = op->o_callback->sc_private;
3356 /* Did the entry exist on the remote? */
3357 for ( dn=&ri->ri_dns; *dn; dn = &(*dn)->next ) {
3358 if ( dn_match( &(*dn)->dn, &rs->sr_entry->e_nname )) {
3359 dnlist *dnext = (*dn)->next;
3360 op->o_tmpfree( *dn, op->o_tmpmemctx );
3366 /* No, so put it on the list to delete */
3369 dnlist *dnl = dnl_alloc( op, &rs->sr_entry->e_nname );
3370 dnl->next = ri->ri_dels;
3372 a = attr_find( rs->sr_entry->e_attrs, ad_queryId );
3373 /* If ours is the only queryId, delete entry */
3374 dnl->delete = ( a->a_numvals == 1 );
3381 refresh_query( Operation *op, CachedQuery *query, slap_overinst *on )
3383 SlapReply rs = {REP_RESULT};
3384 slap_callback cb = { 0 };
3385 refresh_info ri = { 0 };
3386 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(pcacheQueryID=)" ) ];
3387 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
3388 Filter filter = {LDAP_FILTER_EQUALITY};
3389 AttributeName attrs[ 2 ] = {{{ 0 }}};
3393 ldap_pvt_thread_mutex_lock( &query->answerable_cnt_mutex );
3395 ldap_pvt_thread_mutex_unlock( &query->answerable_cnt_mutex );
3397 cb.sc_response = refresh_merge;
3398 cb.sc_private = &ri;
3401 ri.ri_be = op->o_bd;
3404 op->o_tag = LDAP_REQ_SEARCH;
3405 op->o_protocol = LDAP_VERSION3;
3406 op->o_callback = &cb;
3407 op->o_do_not_cache = 1;
3409 op->o_req_dn = query->qbase->base;
3410 op->o_req_ndn = query->qbase->base;
3411 op->ors_scope = query->scope;
3412 op->ors_slimit = SLAP_NO_LIMIT;
3413 op->ors_tlimit = SLAP_NO_LIMIT;
3414 op->ors_limit = NULL;
3415 op->ors_filter = query->filter;
3416 filter2bv_x( op, query->filter, &op->ors_filterstr );
3417 op->ors_attrs = query->qtemp->t_attrs.attrs;
3418 op->ors_attrsonly = 0;
3420 op->o_bd = on->on_info->oi_origdb;
3421 rc = op->o_bd->be_search( op, &rs );
3423 op->o_bd = ri.ri_be;
3427 /* Get the DNs of all entries matching this query */
3428 cb.sc_response = refresh_purge;
3430 op->o_bd = ri.ri_be;
3431 op->o_req_dn = op->o_bd->be_suffix[0];
3432 op->o_req_ndn = op->o_bd->be_nsuffix[0];
3433 op->ors_scope = LDAP_SCOPE_SUBTREE;
3434 op->ors_deref = LDAP_DEREF_NEVER;
3435 op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
3436 "(%s=%s)", ad_queryId->ad_cname.bv_val, query->q_uuid.bv_val);
3437 filter.f_ava = &ava;
3438 filter.f_av_desc = ad_queryId;
3439 filter.f_av_value = query->q_uuid;
3440 attrs[ 0 ].an_desc = ad_queryId;
3441 attrs[ 0 ].an_name = ad_queryId->ad_cname;
3442 op->ors_attrs = attrs;
3443 op->ors_attrsonly = 0;
3444 rs_reinit( &rs, REP_RESULT );
3445 rc = op->o_bd->be_search( op, &rs );
3446 if ( rc ) goto leave;
3448 while (( dn = ri.ri_dels )) {
3449 op->o_req_dn = dn->dn;
3450 op->o_req_ndn = dn->dn;
3451 rs_reinit( &rs, REP_RESULT );
3453 op->o_tag = LDAP_REQ_DELETE;
3454 op->o_bd->be_delete( op, &rs );
3457 struct berval vals[2];
3459 vals[0] = query->q_uuid;
3460 BER_BVZERO( &vals[1] );
3461 mod.sml_op = LDAP_MOD_DELETE;
3463 mod.sml_desc = ad_queryId;
3464 mod.sml_type = ad_queryId->ad_cname;
3465 mod.sml_values = vals;
3466 mod.sml_nvalues = NULL;
3467 mod.sml_numvals = 1;
3468 mod.sml_next = NULL;
3470 op->o_tag = LDAP_REQ_MODIFY;
3471 op->orm_modlist = &mod;
3472 op->o_bd->be_modify( op, &rs );
3474 ri.ri_dels = dn->next;
3475 op->o_tmpfree( dn, op->o_tmpmemctx );
3479 /* reset our local heap, we're done with it */
3480 slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, op->o_threadctx, 1 );
3489 struct re_s *rtask = arg;
3490 slap_overinst *on = rtask->arg;
3491 cache_manager *cm = on->on_bi.bi_private;
3492 query_manager *qm = cm->qm;
3493 Connection conn = {0};
3494 OperationBuffer opbuf;
3497 CachedQuery *query, *qprev;
3498 int return_val, pause = PCACHE_CC_PAUSED;
3499 QueryTemplate *templ;
3501 /* Don't expire anything when we're offline */
3502 if ( cm->cc_paused & PCACHE_CC_OFFLINE ) {
3503 pause = PCACHE_CC_OFFLINE;
3507 connection_fake_init( &conn, &opbuf, ctx );
3511 op->o_dn = cm->db.be_rootdn;
3512 op->o_ndn = cm->db.be_rootndn;
3516 for (templ = qm->templates; templ; templ=templ->qmnext) {
3518 if ( !templ->query_last ) continue;
3520 op->o_time = slap_get_time();
3521 if ( !templ->ttr ) {
3523 if ( templ->negttl && templ->negttl < ttl )
3524 ttl = templ->negttl;
3525 if ( templ->limitttl && templ->limitttl < ttl )
3526 ttl = templ->limitttl;
3527 /* The oldest timestamp that needs expiration checking */
3531 for ( query=templ->query_last; query; query=qprev ) {
3532 qprev = query->prev;
3533 if ( query->refresh_time && query->refresh_time < op->o_time ) {
3534 /* A refresh will extend the expiry if the query has been
3535 * referenced, but not if it's unreferenced. If the
3536 * expiration has been hit, then skip the refresh since
3537 * we're just going to discard the result anyway.
3539 if ( query->refcnt )
3540 query->expiry_time = op->o_time + templ->ttl;
3541 if ( query->expiry_time > op->o_time ) {
3542 refresh_query( op, query, on );
3547 if (query->expiry_time < op->o_time) {
3549 Debug( pcache_debug, "Lock CR index = %p\n",
3550 (void *) templ, 0, 0 );
3551 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
3552 if ( query == templ->query_last ) {
3554 remove_from_template(query, templ);
3555 Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
3556 (void *) templ, templ->no_of_queries, 0 );
3557 Debug( pcache_debug, "Unlock CR index = %p\n",
3558 (void *) templ, 0, 0 );
3561 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
3564 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
3565 remove_query(qm, query);
3566 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
3567 if ( BER_BVISNULL( &query->q_uuid ))
3570 return_val = remove_query_data(op, &query->q_uuid);
3571 Debug( pcache_debug, "STALE QUERY REMOVED, SIZE=%d\n",
3573 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
3574 cm->cur_entries -= return_val;
3575 cm->num_cached_queries--;
3576 Debug( pcache_debug, "STORED QUERIES = %lu\n",
3577 cm->num_cached_queries, 0, 0 );
3578 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
3579 Debug( pcache_debug,
3580 "STALE QUERY REMOVED, CACHE ="
3582 cm->cur_entries, 0, 0 );
3583 ldap_pvt_thread_rdwr_wlock( &query->rwlock );
3584 if ( query->bind_refcnt-- ) {
3589 ldap_pvt_thread_rdwr_wunlock( &query->rwlock );
3590 if ( rem ) free_query(query);
3591 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
3592 } else if ( !templ->ttr && query->expiry_time > ttl ) {
3593 /* We don't need to check for refreshes, and this
3594 * query's expiry is too new, and all subsequent queries
3595 * will be newer yet. So stop looking.
3597 * If we have refreshes, then we always have to walk the
3598 * entire query list.
3606 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3607 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
3608 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
3610 /* If there were no queries, defer processing for a while */
3611 if ( cm->cc_paused != pause )
3612 cm->cc_paused = pause;
3613 ldap_pvt_runqueue_resched( &slapd_rq, rtask, pause );
3615 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3620 #define MAX_ATTR_SETS 500
3633 static ConfigDriver pc_cf_gen;
3634 static ConfigLDAPadd pc_ldadd;
3635 static ConfigCfAdd pc_cfadd;
3637 static ConfigTable pccfg[] = {
3638 { "pcache", "backend> <max_entries> <numattrsets> <entry limit> "
3640 6, 6, 0, ARG_MAGIC|ARG_NO_DELETE|PC_MAIN, pc_cf_gen,
3641 "( OLcfgOvAt:2.1 NAME ( 'olcPcache' 'olcProxyCache' ) "
3642 "DESC 'Proxy Cache basic parameters' "
3643 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
3644 { "pcacheAttrset", "index> <attributes...",
3645 2, 0, 0, ARG_MAGIC|PC_ATTR, pc_cf_gen,
3646 "( OLcfgOvAt:2.2 NAME ( 'olcPcacheAttrset' 'olcProxyAttrset' ) "
3647 "DESC 'A set of attributes to cache' "
3648 "SYNTAX OMsDirectoryString )", NULL, NULL },
3649 { "pcacheTemplate", "filter> <attrset-index> <TTL> <negTTL> "
3651 4, 7, 0, ARG_MAGIC|PC_TEMP, pc_cf_gen,
3652 "( OLcfgOvAt:2.3 NAME ( 'olcPcacheTemplate' 'olcProxyCacheTemplate' ) "
3653 "DESC 'Filter template, attrset, cache TTL, "
3654 "optional negative TTL, optional sizelimit TTL, "
3656 "SYNTAX OMsDirectoryString )", NULL, NULL },
3657 { "pcachePosition", "head|tail(default)",
3658 2, 2, 0, ARG_MAGIC|PC_RESP, pc_cf_gen,
3659 "( OLcfgOvAt:2.4 NAME 'olcPcachePosition' "
3660 "DESC 'Response callback position in overlay stack' "
3661 "SYNTAX OMsDirectoryString )", NULL, NULL },
3662 { "pcacheMaxQueries", "queries",
3663 2, 2, 0, ARG_INT|ARG_MAGIC|PC_QUERIES, pc_cf_gen,
3664 "( OLcfgOvAt:2.5 NAME ( 'olcPcacheMaxQueries' 'olcProxyCacheQueries' ) "
3665 "DESC 'Maximum number of queries to cache' "
3666 "SYNTAX OMsInteger )", NULL, NULL },
3667 { "pcachePersist", "TRUE|FALSE",
3668 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, save_queries),
3669 "( OLcfgOvAt:2.6 NAME ( 'olcPcachePersist' 'olcProxySaveQueries' ) "
3670 "DESC 'Save cached queries for hot restart' "
3671 "SYNTAX OMsBoolean )", NULL, NULL },
3672 { "pcacheValidate", "TRUE|FALSE",
3673 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, check_cacheability),
3674 "( OLcfgOvAt:2.7 NAME ( 'olcPcacheValidate' 'olcProxyCheckCacheability' ) "
3675 "DESC 'Check whether the results of a query are cacheable, e.g. for schema issues' "
3676 "SYNTAX OMsBoolean )", NULL, NULL },
3677 { "pcacheOffline", "TRUE|FALSE",
3678 2, 2, 0, ARG_ON_OFF|ARG_MAGIC|PC_OFFLINE, pc_cf_gen,
3679 "( OLcfgOvAt:2.8 NAME 'olcPcacheOffline' "
3680 "DESC 'Set cache to offline mode and disable expiration' "
3681 "SYNTAX OMsBoolean )", NULL, NULL },
3682 { "pcacheBind", "filter> <attrset-index> <TTR> <scope> <base",
3683 6, 6, 0, ARG_MAGIC|PC_BIND, pc_cf_gen,
3684 "( OLcfgOvAt:2.9 NAME 'olcPcacheBind' "
3685 "DESC 'Parameters for caching Binds' "
3686 "SYNTAX OMsDirectoryString )", NULL, NULL },
3687 { "pcache-", "private database args",
3688 1, 0, STRLENOF("pcache-"), ARG_MAGIC|PC_PRIVATE_DB, pc_cf_gen,
3691 /* Legacy keywords */
3692 { "proxycache", "backend> <max_entries> <numattrsets> <entry limit> "
3694 6, 6, 0, ARG_MAGIC|ARG_NO_DELETE|PC_MAIN, pc_cf_gen,
3696 { "proxyattrset", "index> <attributes...",
3697 2, 0, 0, ARG_MAGIC|PC_ATTR, pc_cf_gen,
3699 { "proxytemplate", "filter> <attrset-index> <TTL> <negTTL",
3700 4, 7, 0, ARG_MAGIC|PC_TEMP, pc_cf_gen,
3702 { "response-callback", "head|tail(default)",
3703 2, 2, 0, ARG_MAGIC|PC_RESP, pc_cf_gen,
3705 { "proxyCacheQueries", "queries",
3706 2, 2, 0, ARG_INT|ARG_MAGIC|PC_QUERIES, pc_cf_gen,
3708 { "proxySaveQueries", "TRUE|FALSE",
3709 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, save_queries),
3711 { "proxyCheckCacheability", "TRUE|FALSE",
3712 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, check_cacheability),
3715 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
3718 static ConfigOCs pcocs[] = {
3719 { "( OLcfgOvOc:2.1 "
3720 "NAME 'olcPcacheConfig' "
3721 "DESC 'ProxyCache configuration' "
3722 "SUP olcOverlayConfig "
3723 "MUST ( olcPcache $ olcPcacheAttrset $ olcPcacheTemplate ) "
3724 "MAY ( olcPcachePosition $ olcPcacheMaxQueries $ olcPcachePersist $ "
3725 "olcPcacheValidate $ olcPcacheOffline $ olcPcacheBind ) )",
3726 Cft_Overlay, pccfg, NULL, pc_cfadd },
3727 { "( OLcfgOvOc:2.2 "
3728 "NAME 'olcPcacheDatabase' "
3729 "DESC 'Cache database configuration' "
3730 "AUXILIARY )", Cft_Misc, olcDatabaseDummy, pc_ldadd },
3734 static int pcache_db_open2( slap_overinst *on, ConfigReply *cr );
3737 pc_ldadd_cleanup( ConfigArgs *c )
3739 slap_overinst *on = c->ca_private;
3740 return pcache_db_open2( on, &c->reply );
3744 pc_ldadd( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
3749 if ( p->ce_type != Cft_Overlay || !p->ce_bi ||
3750 p->ce_bi->bi_cf_ocs != pcocs )
3751 return LDAP_CONSTRAINT_VIOLATION;
3753 on = (slap_overinst *)p->ce_bi;
3754 cm = on->on_bi.bi_private;
3756 /* Defer open if this is an LDAPadd */
3757 if ( CONFIG_ONLINE_ADD( ca ))
3758 ca->cleanup = pc_ldadd_cleanup;
3760 cm->defer_db_open = 0;
3761 ca->ca_private = on;
3762 return LDAP_SUCCESS;
3766 pc_cfadd( Operation *op, SlapReply *rs, Entry *p, ConfigArgs *ca )
3768 CfEntryInfo *pe = p->e_private;
3769 slap_overinst *on = (slap_overinst *)pe->ce_bi;
3770 cache_manager *cm = on->on_bi.bi_private;
3773 /* FIXME: should not hardcode "olcDatabase" here */
3774 bv.bv_len = snprintf( ca->cr_msg, sizeof( ca->cr_msg ),
3775 "olcDatabase=" SLAP_X_ORDERED_FMT "%s",
3776 0, cm->db.bd_info->bi_type );
3777 if ( bv.bv_len >= sizeof( ca->cr_msg ) ) {
3780 bv.bv_val = ca->cr_msg;
3782 cm->defer_db_open = 0;
3784 /* We can only create this entry if the database is table-driven
3786 if ( cm->db.bd_info->bi_cf_ocs )
3787 config_build_entry( op, rs, pe, ca, &bv, cm->db.bd_info->bi_cf_ocs,
3794 pc_cf_gen( ConfigArgs *c )
3796 slap_overinst *on = (slap_overinst *)c->bi;
3797 cache_manager* cm = on->on_bi.bi_private;
3798 query_manager* qm = cm->qm;
3799 QueryTemplate* temp;
3800 AttributeName* attr_name;
3801 AttributeName* attrarray;
3802 const char* text=NULL;
3807 if ( c->op == SLAP_CONFIG_EMIT ) {
3811 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s %d %d %d %ld",
3812 cm->db.bd_info->bi_type, cm->max_entries, cm->numattrsets,
3813 cm->num_entries_limit, cm->cc_period );
3814 bv.bv_val = c->cr_msg;
3815 value_add_one( &c->rvalue_vals, &bv );
3818 for (i=0; i<cm->numattrsets; i++) {
3819 if ( !qm->attr_sets[i].count ) continue;
3821 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ), "%d", i );
3823 /* count the attr length */
3824 for ( attr_name = qm->attr_sets[i].attrs;
3825 attr_name->an_name.bv_val; attr_name++ )
3827 bv.bv_len += attr_name->an_name.bv_len + 1;
3828 if ( attr_name->an_desc &&
3829 ( attr_name->an_desc->ad_flags & SLAP_DESC_TEMPORARY ) ) {
3830 bv.bv_len += STRLENOF("undef:");
3834 bv.bv_val = ch_malloc( bv.bv_len+1 );
3835 ptr = lutil_strcopy( bv.bv_val, c->cr_msg );
3836 for ( attr_name = qm->attr_sets[i].attrs;
3837 attr_name->an_name.bv_val; attr_name++ ) {
3839 if ( attr_name->an_desc &&
3840 ( attr_name->an_desc->ad_flags & SLAP_DESC_TEMPORARY ) ) {
3841 ptr = lutil_strcopy( ptr, "undef:" );
3843 ptr = lutil_strcopy( ptr, attr_name->an_name.bv_val );
3845 ber_bvarray_add( &c->rvalue_vals, &bv );
3847 if ( !c->rvalue_vals )
3851 for (temp=qm->templates; temp; temp=temp->qmnext) {
3852 /* HEADS-UP: always print all;
3853 * if optional == 0, ignore */
3854 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ),
3855 " %d %ld %ld %ld %ld",
3856 temp->attr_set_index,
3861 bv.bv_len += temp->querystr.bv_len + 2;
3862 bv.bv_val = ch_malloc( bv.bv_len+1 );
3865 ptr = lutil_strcopy( ptr, temp->querystr.bv_val );
3867 strcpy( ptr, c->cr_msg );
3868 ber_bvarray_add( &c->rvalue_vals, &bv );
3870 if ( !c->rvalue_vals )
3874 for (temp=qm->templates; temp; temp=temp->qmnext) {
3875 if ( !temp->bindttr ) continue;
3876 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ),
3878 temp->attr_set_index,
3880 ldap_pvt_scope2str( temp->bindscope ));
3881 bv.bv_len += temp->bindbase.bv_len + temp->bindftemp.bv_len + 4;
3882 bv.bv_val = ch_malloc( bv.bv_len + 1 );
3885 ptr = lutil_strcopy( ptr, temp->bindftemp.bv_val );
3887 ptr = lutil_strcopy( ptr, c->cr_msg );
3889 ptr = lutil_strcopy( ptr, temp->bindbase.bv_val );
3892 ber_bvarray_add( &c->rvalue_vals, &bv );
3894 if ( !c->rvalue_vals )
3898 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
3899 BER_BVSTR( &bv, "head" );
3901 BER_BVSTR( &bv, "tail" );
3903 value_add_one( &c->rvalue_vals, &bv );
3906 c->value_int = cm->max_queries;
3909 c->value_int = (cm->cc_paused & PCACHE_CC_OFFLINE) != 0;
3913 } else if ( c->op == LDAP_MOD_DELETE ) {
3916 case PC_ATTR: /* FIXME */
3921 cm->cc_paused &= ~PCACHE_CC_OFFLINE;
3922 /* If there were cached queries when we went offline,
3923 * restart the checker now.
3925 if ( cm->num_cached_queries ) {
3926 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3928 ldap_pvt_runqueue_resched( &slapd_rq, cm->cc_arg, 0 );
3929 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3939 if ( cm->numattrsets > 0 ) {
3940 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"pcache\" directive already provided" );
3941 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3945 if ( lutil_atoi( &cm->numattrsets, c->argv[3] ) != 0 ) {
3946 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse num attrsets=\"%s\" (arg #3)",
3948 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3951 if ( cm->numattrsets <= 0 ) {
3952 snprintf( c->cr_msg, sizeof( c->cr_msg ), "numattrsets (arg #3) must be positive" );
3953 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3956 if ( cm->numattrsets > MAX_ATTR_SETS ) {
3957 snprintf( c->cr_msg, sizeof( c->cr_msg ), "numattrsets (arg #3) must be <= %d", MAX_ATTR_SETS );
3958 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3962 if ( !backend_db_init( c->argv[1], &cm->db, -1, NULL )) {
3963 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unknown backend type (arg #1)" );
3964 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3968 if ( lutil_atoi( &cm->max_entries, c->argv[2] ) != 0 ) {
3969 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse max entries=\"%s\" (arg #2)",
3971 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3974 if ( cm->max_entries <= 0 ) {
3975 snprintf( c->cr_msg, sizeof( c->cr_msg ), "max entries (arg #2) must be positive.\n" );
3976 Debug( LDAP_DEBUG_CONFIG, "%s: %s\n", c->log, c->cr_msg, 0 );
3980 if ( lutil_atoi( &cm->num_entries_limit, c->argv[4] ) != 0 ) {
3981 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse entry limit=\"%s\" (arg #4)",
3983 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3986 if ( cm->num_entries_limit <= 0 ) {
3987 snprintf( c->cr_msg, sizeof( c->cr_msg ), "entry limit (arg #4) must be positive" );
3988 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3991 if ( cm->num_entries_limit > cm->max_entries ) {
3992 snprintf( c->cr_msg, sizeof( c->cr_msg ), "entry limit (arg #4) must be less than max entries %d (arg #2)", cm->max_entries );
3993 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3997 if ( lutil_parse_time( c->argv[5], &t ) != 0 ) {
3998 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse period=\"%s\" (arg #5)",
4000 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4004 cm->cc_period = (time_t)t;
4005 Debug( pcache_debug,
4006 "Total # of attribute sets to be cached = %d.\n",
4007 cm->numattrsets, 0, 0 );
4008 qm->attr_sets = ( struct attr_set * )ch_calloc( cm->numattrsets,
4009 sizeof( struct attr_set ) );
4012 if ( cm->numattrsets == 0 ) {
4013 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"pcache\" directive not provided yet" );
4014 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4017 if ( lutil_atoi( &num, c->argv[1] ) != 0 ) {
4018 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse attrset #=\"%s\"",
4020 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4024 if ( num < 0 || num >= cm->numattrsets ) {
4025 snprintf( c->cr_msg, sizeof( c->cr_msg ), "attrset index %d out of bounds (must be %s%d)",
4026 num, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
4027 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4030 qm->attr_sets[num].flags |= PC_CONFIGURED;
4031 if ( c->argc == 2 ) {
4033 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4034 "need an explicit attr in attrlist; use \"*\" to indicate all attrs" );
4035 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4038 } else if ( c->argc == 3 ) {
4039 if ( strcmp( c->argv[2], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) {
4040 qm->attr_sets[num].count = 1;
4041 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 2,
4042 sizeof( AttributeName ) );
4043 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_USER_ATTRIBUTES );
4046 } else if ( strcmp( c->argv[2], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 ) {
4047 qm->attr_sets[num].count = 1;
4048 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 2,
4049 sizeof( AttributeName ) );
4050 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
4053 } else if ( strcmp( c->argv[2], LDAP_NO_ATTRS ) == 0 ) {
4056 /* else: fallthru */
4058 } else if ( c->argc == 4 ) {
4059 if ( ( strcmp( c->argv[2], LDAP_ALL_USER_ATTRIBUTES ) == 0 && strcmp( c->argv[3], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 )
4060 || ( strcmp( c->argv[2], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 && strcmp( c->argv[3], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) )
4062 qm->attr_sets[num].count = 2;
4063 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 3,
4064 sizeof( AttributeName ) );
4065 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_USER_ATTRIBUTES );
4066 BER_BVSTR( &qm->attr_sets[num].attrs[1].an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
4069 /* else: fallthru */
4072 if ( c->argc > 2 ) {
4073 int all_user = 0, all_op = 0;
4075 qm->attr_sets[num].count = c->argc - 2;
4076 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( c->argc - 1,
4077 sizeof( AttributeName ) );
4078 attr_name = qm->attr_sets[num].attrs;
4079 for ( i = 2; i < c->argc; i++ ) {
4080 attr_name->an_desc = NULL;
4081 if ( strcmp( c->argv[i], LDAP_NO_ATTRS ) == 0 ) {
4082 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4083 "invalid attr #%d \"%s\" in attrlist",
4084 i - 2, c->argv[i] );
4085 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4086 ch_free( qm->attr_sets[num].attrs );
4087 qm->attr_sets[num].attrs = NULL;
4088 qm->attr_sets[num].count = 0;
4091 if ( strcmp( c->argv[i], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) {
4093 BER_BVSTR( &attr_name->an_name, LDAP_ALL_USER_ATTRIBUTES );
4094 } else if ( strcmp( c->argv[i], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 ) {
4096 BER_BVSTR( &attr_name->an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
4098 if ( strncasecmp( c->argv[i], "undef:", STRLENOF("undef:") ) == 0 ) {
4100 ber_str2bv( c->argv[i] + STRLENOF("undef:"), 0, 0, &bv );
4101 attr_name->an_desc = slap_bv2tmp_ad( &bv, NULL );
4103 } else if ( slap_str2ad( c->argv[i], &attr_name->an_desc, &text ) ) {
4104 strcpy( c->cr_msg, text );
4105 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4106 ch_free( qm->attr_sets[num].attrs );
4107 qm->attr_sets[num].attrs = NULL;
4108 qm->attr_sets[num].count = 0;
4111 attr_name->an_name = attr_name->an_desc->ad_cname;
4113 attr_name->an_oc = NULL;
4114 attr_name->an_flags = 0;
4115 if ( attr_name->an_desc == slap_schema.si_ad_objectClass )
4116 qm->attr_sets[num].flags |= PC_GOT_OC;
4118 BER_BVZERO( &attr_name->an_name );
4121 /* warn if list contains both "*" and "+" */
4122 if ( i > 4 && all_user && all_op ) {
4123 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4124 "warning: attribute list contains \"*\" and \"+\"" );
4125 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4130 if ( cm->numattrsets == 0 ) {
4131 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"pcache\" directive not provided yet" );
4132 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4135 if ( lutil_atoi( &i, c->argv[2] ) != 0 ) {
4136 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse template #=\"%s\"",
4138 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4142 if ( i < 0 || i >= cm->numattrsets ||
4143 !(qm->attr_sets[i].flags & PC_CONFIGURED )) {
4144 snprintf( c->cr_msg, sizeof( c->cr_msg ), "template index %d invalid (%s%d)",
4145 i, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
4146 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4150 AttributeName *attrs;
4152 cnt = template_attrs( c->argv[1], &qm->attr_sets[i], &attrs, &text );
4154 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse template: %s",
4156 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4159 temp = ch_calloc( 1, sizeof( QueryTemplate ));
4160 temp->qmnext = qm->templates;
4161 qm->templates = temp;
4162 temp->t_attrs.attrs = attrs;
4163 temp->t_attrs.count = cnt;
4165 ldap_pvt_thread_rdwr_init( &temp->t_rwlock );
4166 temp->query = temp->query_last = NULL;
4167 if ( lutil_parse_time( c->argv[3], &t ) != 0 ) {
4168 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4169 "unable to parse template ttl=\"%s\"",
4171 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4173 ch_free( temp->t_attrs.attrs );
4177 temp->ttl = (time_t)t;
4178 temp->negttl = (time_t)0;
4179 temp->limitttl = (time_t)0;
4180 temp->ttr = (time_t)0;
4181 switch ( c->argc ) {
4183 if ( lutil_parse_time( c->argv[6], &t ) != 0 ) {
4184 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4185 "unable to parse template ttr=\"%s\"",
4187 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4190 temp->ttr = (time_t)t;
4194 if ( lutil_parse_time( c->argv[5], &t ) != 0 ) {
4195 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4196 "unable to parse template sizelimit ttl=\"%s\"",
4198 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4201 temp->limitttl = (time_t)t;
4205 if ( lutil_parse_time( c->argv[4], &t ) != 0 ) {
4206 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4207 "unable to parse template negative ttl=\"%s\"",
4209 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4212 temp->negttl = (time_t)t;
4216 temp->no_of_queries = 0;
4218 ber_str2bv( c->argv[1], 0, 1, &temp->querystr );
4219 Debug( pcache_debug, "Template:\n", 0, 0, 0 );
4220 Debug( pcache_debug, " query template: %s\n",
4221 temp->querystr.bv_val, 0, 0 );
4222 temp->attr_set_index = i;
4223 qm->attr_sets[i].flags |= PC_REFERENCED;
4224 temp->qtnext = qm->attr_sets[i].templates;
4225 qm->attr_sets[i].templates = temp;
4226 Debug( pcache_debug, " attributes: \n", 0, 0, 0 );
4227 if ( ( attrarray = qm->attr_sets[i].attrs ) != NULL ) {
4228 for ( i=0; attrarray[i].an_name.bv_val; i++ )
4229 Debug( pcache_debug, "\t%s\n",
4230 attrarray[i].an_name.bv_val, 0, 0 );
4234 if ( !qm->templates ) {
4235 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"pcacheTemplate\" directive not provided yet" );
4236 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4239 if ( lutil_atoi( &i, c->argv[2] ) != 0 ) {
4240 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse Bind index #=\"%s\"",
4242 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4246 if ( i < 0 || i >= cm->numattrsets ||
4247 !(qm->attr_sets[i].flags & PC_CONFIGURED )) {
4248 snprintf( c->cr_msg, sizeof( c->cr_msg ), "Bind index %d invalid (%s%d)",
4249 i, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
4250 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4253 { struct berval bv, tempbv;
4254 AttributeDescription **descs;
4256 ber_str2bv( c->argv[1], 0, 0, &bv );
4257 ndescs = ftemp_attrs( &bv, &tempbv, &descs, &text );
4259 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse template: %s",
4261 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4264 for ( temp = qm->templates; temp; temp=temp->qmnext ) {
4265 if ( temp->attr_set_index == i && bvmatch( &tempbv,
4269 ch_free( tempbv.bv_val );
4272 snprintf( c->cr_msg, sizeof( c->cr_msg ), "Bind template %s %d invalid",
4274 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4277 ber_dupbv( &temp->bindftemp, &bv );
4278 temp->bindfattrs = descs;
4279 temp->bindnattrs = ndescs;
4281 if ( lutil_parse_time( c->argv[3], &t ) != 0 ) {
4282 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4283 "unable to parse bind ttr=\"%s\"",
4285 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4287 ch_free( temp->bindfattrs );
4288 temp->bindfattrs = NULL;
4289 ch_free( temp->bindftemp.bv_val );
4290 BER_BVZERO( &temp->bindftemp );
4293 num = ldap_pvt_str2scope( c->argv[4] );
4295 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4296 "unable to parse bind scope=\"%s\"",
4298 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4302 struct berval dn, ndn;
4303 ber_str2bv( c->argv[5], 0, 0, &dn );
4304 rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
4306 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4307 "invalid bind baseDN=\"%s\"",
4309 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4312 if ( temp->bindbase.bv_val )
4313 ch_free( temp->bindbase.bv_val );
4314 temp->bindbase = ndn;
4317 /* convert the template into dummy filter */
4319 char *eq = temp->bindftemp.bv_val, *e2;
4322 while ((eq = strchr(eq, '=' ))) {
4327 bv.bv_len = temp->bindftemp.bv_len + i;
4328 bv.bv_val = ch_malloc( bv.bv_len + 1 );
4329 for ( e2 = bv.bv_val, eq = temp->bindftemp.bv_val;
4340 f = str2filter( bv.bv_val );
4342 ch_free( bv.bv_val );
4343 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4344 "unable to parse bindfilter=\"%s\"", bv.bv_val );
4345 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4346 ch_free( temp->bindbase.bv_val );
4347 BER_BVZERO( &temp->bindbase );
4350 if ( temp->bindfilter )
4351 filter_free( temp->bindfilter );
4352 if ( temp->bindfilterstr.bv_val )
4353 ch_free( temp->bindfilterstr.bv_val );
4354 temp->bindfilterstr = bv;
4355 temp->bindfilter = f;
4357 temp->bindttr = (time_t)t;
4358 temp->bindscope = num;
4359 cm->cache_binds = 1;
4363 if ( strcasecmp( c->argv[1], "head" ) == 0 ) {
4364 cm->response_cb = PCACHE_RESPONSE_CB_HEAD;
4366 } else if ( strcasecmp( c->argv[1], "tail" ) == 0 ) {
4367 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
4370 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unknown specifier" );
4371 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4376 if ( c->value_int <= 0 ) {
4377 snprintf( c->cr_msg, sizeof( c->cr_msg ), "max queries must be positive" );
4378 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4381 cm->max_queries = c->value_int;
4385 cm->cc_paused |= PCACHE_CC_OFFLINE;
4387 cm->cc_paused &= ~PCACHE_CC_OFFLINE;
4390 if ( cm->db.be_private == NULL ) {
4391 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4392 "private database must be defined before setting database specific options" );
4393 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4397 if ( cm->db.bd_info->bi_cf_ocs ) {
4400 char *argv0 = c->argv[ 0 ];
4402 c->argv[ 0 ] = &argv0[ STRLENOF( "pcache-" ) ];
4404 ct = config_find_keyword( cm->db.bd_info->bi_cf_ocs->co_table, c );
4406 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4407 "private database does not recognize specific option '%s'",
4409 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4413 c->table = cm->db.bd_info->bi_cf_ocs->co_type;
4415 c->bi = c->be->bd_info;
4417 rc = config_add_vals( ct, c );
4421 c->table = c2.table;
4424 c->argv[ 0 ] = argv0;
4426 } else if ( cm->db.be_config != NULL ) {
4427 char *argv0 = c->argv[ 0 ];
4429 c->argv[ 0 ] = &argv0[ STRLENOF( "pcache-" ) ];
4430 rc = cm->db.be_config( &cm->db, c->fname, c->lineno, c->argc, c->argv );
4431 c->argv[ 0 ] = argv0;
4434 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4435 "no means to set private database specific options" );
4436 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4441 rc = SLAP_CONF_UNKNOWN;
4457 slap_overinst *on = (slap_overinst *)be->bd_info;
4458 cache_manager* cm = on->on_bi.bi_private;
4460 /* Something for the cache database? */
4461 if ( cm->db.bd_info && cm->db.bd_info->bi_db_config )
4462 return cm->db.bd_info->bi_db_config( &cm->db, fname, lineno,
4464 return SLAP_CONF_UNKNOWN;
4472 slap_overinst *on = (slap_overinst *)be->bd_info;
4476 cm = (cache_manager *)ch_malloc(sizeof(cache_manager));
4477 on->on_bi.bi_private = cm;
4479 qm = (query_manager*)ch_malloc(sizeof(query_manager));
4482 SLAP_DBFLAGS(&cm->db) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
4483 cm->db.be_private = NULL;
4484 cm->db.bd_self = &cm->db;
4486 cm->numattrsets = 0;
4487 cm->num_entries_limit = 5;
4488 cm->num_cached_queries = 0;
4489 cm->max_entries = 0;
4490 cm->cur_entries = 0;
4491 cm->max_queries = 10000;
4492 cm->save_queries = 0;
4493 cm->check_cacheability = 0;
4494 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
4495 cm->defer_db_open = 1;
4496 cm->cache_binds = 0;
4497 cm->cc_period = 1000;
4500 #ifdef PCACHE_MONITOR
4501 cm->monitor_cb = NULL;
4502 #endif /* PCACHE_MONITOR */
4504 qm->attr_sets = NULL;
4505 qm->templates = NULL;
4507 qm->lru_bottom = NULL;
4509 qm->qcfunc = query_containment;
4510 qm->crfunc = cache_replacement;
4511 qm->addfunc = add_query;
4512 ldap_pvt_thread_mutex_init(&qm->lru_mutex);
4514 ldap_pvt_thread_mutex_init(&cm->cache_mutex);
4516 #ifndef PCACHE_MONITOR
4518 #else /* PCACHE_MONITOR */
4519 return pcache_monitor_db_init( be );
4520 #endif /* PCACHE_MONITOR */
4524 pcache_cachedquery_open_cb( Operation *op, SlapReply *rs )
4526 assert( op->o_tag == LDAP_REQ_SEARCH );
4528 if ( rs->sr_type == REP_SEARCH ) {
4531 a = attr_find( rs->sr_entry->e_attrs, ad_cachedQueryURL );
4535 assert( a->a_nvals != NULL );
4537 valsp = op->o_callback->sc_private;
4538 assert( *valsp == NULL );
4540 ber_bvarray_dup_x( valsp, a->a_nvals, op->o_tmpmemctx );
4548 pcache_cachedquery_count_cb( Operation *op, SlapReply *rs )
4550 assert( op->o_tag == LDAP_REQ_SEARCH );
4552 if ( rs->sr_type == REP_SEARCH ) {
4553 int *countp = (int *)op->o_callback->sc_private;
4566 cache_manager *cm = on->on_bi.bi_private;
4567 query_manager* qm = cm->qm;
4570 rc = backend_startup_one( &cm->db, cr );
4572 cm->defer_db_open = 0;
4575 /* There is no runqueue in TOOL mode */
4576 if (( slapMode & SLAP_SERVER_MODE ) && rc == 0 ) {
4577 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4578 ldap_pvt_runqueue_insert( &slapd_rq, cm->cc_period,
4579 consistency_check, on,
4580 "pcache_consistency", cm->db.be_suffix[0].bv_val );
4581 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4583 /* Cached database must have the rootdn */
4584 if ( BER_BVISNULL( &cm->db.be_rootndn )
4585 || BER_BVISEMPTY( &cm->db.be_rootndn ) )
4587 Debug( LDAP_DEBUG_ANY, "pcache_db_open(): "
4588 "underlying database of type \"%s\"\n"
4589 " serving naming context \"%s\"\n"
4590 " has no \"rootdn\", required by \"pcache\".\n",
4591 on->on_info->oi_orig->bi_type,
4592 cm->db.be_suffix[0].bv_val, 0 );
4596 if ( cm->save_queries ) {
4597 void *thrctx = ldap_pvt_thread_pool_context();
4598 Connection conn = { 0 };
4599 OperationBuffer opbuf;
4601 slap_callback cb = { 0 };
4602 SlapReply rs = { REP_RESULT };
4603 BerVarray vals = NULL;
4604 Filter f = { 0 }, f2 = { 0 };
4605 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
4606 AttributeName attrs[ 2 ] = {{{ 0 }}};
4608 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
4613 op->o_tag = LDAP_REQ_SEARCH;
4614 op->o_protocol = LDAP_VERSION3;
4615 cb.sc_response = pcache_cachedquery_open_cb;
4616 cb.sc_private = &vals;
4617 op->o_callback = &cb;
4618 op->o_time = slap_get_time();
4619 op->o_do_not_cache = 1;
4620 op->o_managedsait = SLAP_CONTROL_CRITICAL;
4622 op->o_dn = cm->db.be_rootdn;
4623 op->o_ndn = cm->db.be_rootndn;
4624 op->o_req_dn = cm->db.be_suffix[ 0 ];
4625 op->o_req_ndn = cm->db.be_nsuffix[ 0 ];
4627 op->ors_scope = LDAP_SCOPE_BASE;
4628 op->ors_deref = LDAP_DEREF_NEVER;
4630 op->ors_tlimit = SLAP_NO_LIMIT;
4631 op->ors_limit = NULL;
4632 ber_str2bv( "(pcacheQueryURL=*)", 0, 0, &op->ors_filterstr );
4633 f.f_choice = LDAP_FILTER_PRESENT;
4634 f.f_desc = ad_cachedQueryURL;
4635 op->ors_filter = &f;
4636 attrs[ 0 ].an_desc = ad_cachedQueryURL;
4637 attrs[ 0 ].an_name = ad_cachedQueryURL->ad_cname;
4638 op->ors_attrs = attrs;
4639 op->ors_attrsonly = 0;
4641 rc = op->o_bd->be_search( op, &rs );
4642 if ( rc == LDAP_SUCCESS && vals != NULL ) {
4645 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
4646 if ( url2query( vals[ i ].bv_val, op, qm ) == 0 ) {
4647 cm->num_cached_queries++;
4651 ber_bvarray_free_x( vals, op->o_tmpmemctx );
4654 /* count cached entries */
4655 f.f_choice = LDAP_FILTER_NOT;
4657 f2.f_choice = LDAP_FILTER_EQUALITY;
4659 f2.f_av_desc = slap_schema.si_ad_objectClass;
4660 BER_BVSTR( &f2.f_av_value, "glue" );
4661 ber_str2bv( "(!(objectClass=glue))", 0, 0, &op->ors_filterstr );
4663 op->ors_slimit = SLAP_NO_LIMIT;
4664 op->ors_scope = LDAP_SCOPE_SUBTREE;
4665 op->ors_attrs = slap_anlist_no_attrs;
4667 rs_reinit( &rs, REP_RESULT );
4668 op->o_callback->sc_response = pcache_cachedquery_count_cb;
4669 op->o_callback->sc_private = &rs.sr_nentries;
4671 rc = op->o_bd->be_search( op, &rs );
4673 cm->cur_entries = rs.sr_nentries;
4687 slap_overinst *on = (slap_overinst *)be->bd_info;
4688 cache_manager *cm = on->on_bi.bi_private;
4689 query_manager* qm = cm->qm;
4690 int i, ncf = 0, rf = 0, nrf = 0, rc = 0;
4692 /* check attr sets */
4693 for ( i = 0; i < cm->numattrsets; i++) {
4694 if ( !( qm->attr_sets[i].flags & PC_CONFIGURED ) ) {
4695 if ( qm->attr_sets[i].flags & PC_REFERENCED ) {
4696 Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d not configured but referenced.\n", i, 0, 0 );
4700 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, attr set #%d not configured.\n", i, 0, 0 );
4704 } else if ( !( qm->attr_sets[i].flags & PC_REFERENCED ) ) {
4705 Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d configured but not referenced.\n", i, 0, 0 );
4710 if ( ncf || rf || nrf ) {
4711 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets configured but not referenced.\n", nrf, 0, 0 );
4712 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets not configured.\n", ncf, 0, 0 );
4713 Debug( LDAP_DEBUG_CONFIG, "pcache: %d attr sets not configured but referenced.\n", rf, 0, 0 );
4720 /* need to inherit something from the original database... */
4721 cm->db.be_def_limit = be->be_def_limit;
4722 cm->db.be_limits = be->be_limits;
4723 cm->db.be_acl = be->be_acl;
4724 cm->db.be_dfltaccess = be->be_dfltaccess;
4726 if ( SLAP_DBMONITORING( be ) ) {
4727 SLAP_DBFLAGS( &cm->db ) |= SLAP_DBFLAG_MONITORING;
4730 SLAP_DBFLAGS( &cm->db ) &= ~SLAP_DBFLAG_MONITORING;
4733 if ( !cm->defer_db_open ) {
4734 rc = pcache_db_open2( on, cr );
4737 #ifdef PCACHE_MONITOR
4738 if ( rc == LDAP_SUCCESS ) {
4739 rc = pcache_monitor_db_open( be );
4741 #endif /* PCACHE_MONITOR */
4747 pcache_free_qbase( void *v )
4753 tavl_free( qb->scopes[i], NULL );
4763 slap_overinst *on = (slap_overinst *)be->bd_info;
4764 cache_manager *cm = on->on_bi.bi_private;
4765 query_manager *qm = cm->qm;
4769 /* stop the thread ... */
4771 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4772 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, cm->cc_arg ) ) {
4773 ldap_pvt_runqueue_stoptask( &slapd_rq, cm->cc_arg );
4775 ldap_pvt_runqueue_remove( &slapd_rq, cm->cc_arg );
4776 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4779 if ( cm->save_queries ) {
4781 BerVarray vals = NULL;
4784 Connection conn = { 0 };
4785 OperationBuffer opbuf;
4787 slap_callback cb = { 0 };
4789 SlapReply rs = { REP_RESULT };
4790 Modifications mod = {{ 0 }};
4792 thrctx = ldap_pvt_thread_pool_context();
4794 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
4797 mod.sml_numvals = 0;
4798 if ( qm->templates != NULL ) {
4799 for ( tm = qm->templates; tm != NULL; tm = tm->qmnext ) {
4800 for ( qc = tm->query; qc; qc = qc->next ) {
4803 if ( query2url( op, qc, &bv, 0 ) == 0 ) {
4804 ber_bvarray_add_x( &vals, &bv, op->o_tmpmemctx );
4812 op->o_dn = cm->db.be_rootdn;
4813 op->o_ndn = cm->db.be_rootndn;
4815 op->o_tag = LDAP_REQ_MODIFY;
4816 op->o_protocol = LDAP_VERSION3;
4817 cb.sc_response = slap_null_cb;
4818 op->o_callback = &cb;
4819 op->o_time = slap_get_time();
4820 op->o_do_not_cache = 1;
4821 op->o_managedsait = SLAP_CONTROL_CRITICAL;
4823 op->o_req_dn = op->o_bd->be_suffix[0];
4824 op->o_req_ndn = op->o_bd->be_nsuffix[0];
4826 mod.sml_op = LDAP_MOD_REPLACE;
4828 mod.sml_desc = ad_cachedQueryURL;
4829 mod.sml_type = ad_cachedQueryURL->ad_cname;
4830 mod.sml_values = vals;
4831 mod.sml_nvalues = NULL;
4832 mod.sml_next = NULL;
4833 Debug( pcache_debug,
4834 "%sSETTING CACHED QUERY URLS\n",
4835 vals == NULL ? "RE" : "", 0, 0 );
4837 op->orm_modlist = &mod;
4839 op->o_bd->be_modify( op, &rs );
4841 ber_bvarray_free_x( vals, op->o_tmpmemctx );
4844 /* cleanup stuff inherited from the original database... */
4845 cm->db.be_limits = NULL;
4846 cm->db.be_acl = NULL;
4849 if ( cm->db.bd_info->bi_db_close ) {
4850 rc = cm->db.bd_info->bi_db_close( &cm->db, NULL );
4852 while ( (tm = qm->templates) != NULL ) {
4853 CachedQuery *qc, *qn;
4854 qm->templates = tm->qmnext;
4855 for ( qc = tm->query; qc; qc = qn ) {
4859 avl_free( tm->qbase, pcache_free_qbase );
4860 free( tm->querystr.bv_val );
4861 free( tm->bindfattrs );
4862 free( tm->bindftemp.bv_val );
4863 free( tm->bindfilterstr.bv_val );
4864 free( tm->bindbase.bv_val );
4865 filter_free( tm->bindfilter );
4866 ldap_pvt_thread_rdwr_destroy( &tm->t_rwlock );
4867 free( tm->t_attrs.attrs );
4871 for ( i = 0; i < cm->numattrsets; i++ ) {
4874 /* Account of LDAP_NO_ATTRS */
4875 if ( !qm->attr_sets[i].count ) continue;
4877 for ( j = 0; !BER_BVISNULL( &qm->attr_sets[i].attrs[j].an_name ); j++ ) {
4878 if ( qm->attr_sets[i].attrs[j].an_desc &&
4879 ( qm->attr_sets[i].attrs[j].an_desc->ad_flags &
4880 SLAP_DESC_TEMPORARY ) ) {
4881 slap_sl_mfuncs.bmf_free( qm->attr_sets[i].attrs[j].an_desc, NULL );
4884 free( qm->attr_sets[i].attrs );
4886 free( qm->attr_sets );
4887 qm->attr_sets = NULL;
4889 #ifdef PCACHE_MONITOR
4890 if ( rc == LDAP_SUCCESS ) {
4891 rc = pcache_monitor_db_close( be );
4893 #endif /* PCACHE_MONITOR */
4904 slap_overinst *on = (slap_overinst *)be->bd_info;
4905 cache_manager *cm = on->on_bi.bi_private;
4906 query_manager *qm = cm->qm;
4908 if ( cm->db.be_private != NULL ) {
4909 backend_stopdown_one( &cm->db );
4912 ldap_pvt_thread_mutex_destroy( &qm->lru_mutex );
4913 ldap_pvt_thread_mutex_destroy( &cm->cache_mutex );
4917 #ifdef PCACHE_MONITOR
4918 pcache_monitor_db_destroy( be );
4919 #endif /* PCACHE_MONITOR */
4924 #ifdef PCACHE_CONTROL_PRIVDB
4926 Control ::= SEQUENCE {
4927 controlType LDAPOID,
4928 criticality BOOLEAN DEFAULT FALSE,
4929 controlValue OCTET STRING OPTIONAL }
4931 controlType ::= 1.3.6.1.4.1.4203.666.11.9.5.1
4933 * criticality must be TRUE; controlValue must be absent.
4941 if ( op->o_ctrlflag[ privDB_cid ] != SLAP_CONTROL_NONE ) {
4942 rs->sr_text = "privateDB control specified multiple times";
4943 return LDAP_PROTOCOL_ERROR;
4946 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
4947 rs->sr_text = "privateDB control value not absent";
4948 return LDAP_PROTOCOL_ERROR;
4951 if ( !ctrl->ldctl_iscritical ) {
4952 rs->sr_text = "privateDB control criticality required";
4953 return LDAP_PROTOCOL_ERROR;
4956 op->o_ctrlflag[ privDB_cid ] = SLAP_CONTROL_CRITICAL;
4958 return LDAP_SUCCESS;
4961 static char *extops[] = {
4962 LDAP_EXOP_MODIFY_PASSWD,
4965 #endif /* PCACHE_CONTROL_PRIVDB */
4967 static struct berval pcache_exop_MODIFY_PASSWD = BER_BVC( LDAP_EXOP_MODIFY_PASSWD );
4968 #ifdef PCACHE_EXOP_QUERY_DELETE
4969 static struct berval pcache_exop_QUERY_DELETE = BER_BVC( PCACHE_EXOP_QUERY_DELETE );
4971 #define LDAP_TAG_EXOP_QUERY_DELETE_BASE ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 0)
4972 #define LDAP_TAG_EXOP_QUERY_DELETE_DN ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 1)
4973 #define LDAP_TAG_EXOP_QUERY_DELETE_UUID ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 2)
4976 ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
4977 requestName [0] LDAPOID,
4978 requestValue [1] OCTET STRING OPTIONAL }
4980 requestName ::= 1.3.6.1.4.1.4203.666.11.9.6.1
4982 requestValue ::= SEQUENCE { CHOICE {
4984 entryDN [1] LDAPDN },
4985 queryID [2] OCTET STRING (SIZE(16))
4986 -- constrained to UUID }
4988 * Either baseDN or entryDN must be present, to allow database selection.
4990 * 1. if baseDN and queryID are present, then the query corresponding
4991 * to queryID is deleted;
4992 * 2. if baseDN is present and queryID is absent, then all queries
4994 * 3. if entryDN is present and queryID is absent, then all queries
4995 * corresponding to the queryID values present in entryDN are deleted;
4996 * 4. if entryDN and queryID are present, then all queries
4997 * corresponding to the queryID values present in entryDN are deleted,
4998 * but only if the value of queryID is contained in the entry;
5000 * Currently, only 1, 3 and 4 are implemented. 2 can be obtained by either
5001 * recursively deleting the database (ldapdelete -r) with PRIVDB control,
5002 * or by removing the database files.
5004 ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
5005 COMPONENTS OF LDAPResult,
5006 responseName [10] LDAPOID OPTIONAL,
5007 responseValue [11] OCTET STRING OPTIONAL }
5009 * responseName and responseValue must be absent.
5013 * - on success, *tagp is either LDAP_TAG_EXOP_QUERY_DELETE_BASE
5014 * or LDAP_TAG_EXOP_QUERY_DELETE_DN.
5015 * - if ndn != NULL, it is set to the normalized DN in the request
5016 * corresponding to either the baseDN or the entryDN, according
5017 * to *tagp; memory is malloc'ed on the Operation's slab, and must
5018 * be freed by the caller.
5019 * - if uuid != NULL, it is set to point to the normalized UUID;
5020 * memory is malloc'ed on the Operation's slab, and must
5021 * be freed by the caller.
5024 pcache_parse_query_delete(
5028 struct berval *uuid,
5032 int rc = LDAP_SUCCESS;
5035 BerElementBuffer berbuf;
5036 BerElement *ber = (BerElement *)&berbuf;
5037 struct berval reqdata = BER_BVNULL;
5049 if ( in == NULL || in->bv_len == 0 ) {
5050 *text = "empty request data field in queryDelete exop";
5051 return LDAP_PROTOCOL_ERROR;
5054 ber_dupbv_x( &reqdata, in, ctx );
5056 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
5057 ber_init2( ber, &reqdata, 0 );
5059 tag = ber_scanf( ber, "{" /*}*/ );
5061 if ( tag == LBER_ERROR ) {
5062 Debug( LDAP_DEBUG_TRACE,
5063 "pcache_parse_query_delete: decoding error.\n",
5065 goto decoding_error;
5068 tag = ber_peek_tag( ber, &len );
5069 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_BASE
5070 || tag == LDAP_TAG_EXOP_QUERY_DELETE_DN )
5074 if ( ndn != NULL ) {
5077 tag = ber_scanf( ber, "m", &dn );
5078 if ( tag == LBER_ERROR ) {
5079 Debug( LDAP_DEBUG_TRACE,
5080 "pcache_parse_query_delete: DN parse failed.\n",
5082 goto decoding_error;
5085 rc = dnNormalize( 0, NULL, NULL, &dn, ndn, ctx );
5086 if ( rc != LDAP_SUCCESS ) {
5087 *text = "invalid DN in queryDelete exop request data";
5092 tag = ber_scanf( ber, "x" /* "m" */ );
5093 if ( tag == LBER_DEFAULT ) {
5094 goto decoding_error;
5098 tag = ber_peek_tag( ber, &len );
5101 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_UUID ) {
5102 if ( uuid != NULL ) {
5104 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
5106 tag = ber_scanf( ber, "m", &bv );
5107 if ( tag == LBER_ERROR ) {
5108 Debug( LDAP_DEBUG_TRACE,
5109 "pcache_parse_query_delete: UUID parse failed.\n",
5111 goto decoding_error;
5114 if ( bv.bv_len != 16 ) {
5115 Debug( LDAP_DEBUG_TRACE,
5116 "pcache_parse_query_delete: invalid UUID length %lu.\n",
5117 (unsigned long)bv.bv_len, 0, 0 );
5118 goto decoding_error;
5121 rc = lutil_uuidstr_from_normalized(
5122 bv.bv_val, bv.bv_len,
5123 uuidbuf, sizeof( uuidbuf ) );
5125 goto decoding_error;
5127 ber_str2bv( uuidbuf, rc, 1, uuid );
5131 tag = ber_skip_tag( ber, &len );
5132 if ( tag == LBER_DEFAULT ) {
5133 goto decoding_error;
5137 Debug( LDAP_DEBUG_TRACE,
5138 "pcache_parse_query_delete: invalid UUID length %lu.\n",
5139 (unsigned long)len, 0, 0 );
5140 goto decoding_error;
5144 tag = ber_peek_tag( ber, &len );
5147 if ( tag != LBER_DEFAULT || len != 0 ) {
5149 Debug( LDAP_DEBUG_TRACE,
5150 "pcache_parse_query_delete: decoding error\n",
5152 rc = LDAP_PROTOCOL_ERROR;
5153 *text = "queryDelete data decoding error";
5156 if ( ndn && !BER_BVISNULL( ndn ) ) {
5157 slap_sl_free( ndn->bv_val, ctx );
5161 if ( uuid && !BER_BVISNULL( uuid ) ) {
5162 slap_sl_free( uuid->bv_val, ctx );
5167 if ( !BER_BVISNULL( &reqdata ) ) {
5168 ber_memfree_x( reqdata.bv_val, ctx );
5175 pcache_exop_query_delete(
5179 BackendDB *bd = op->o_bd;
5181 struct berval uuid = BER_BVNULL,
5183 char buf[ SLAP_TEXT_BUFLEN ];
5185 ber_tag_t tag = LBER_DEFAULT;
5187 if ( LogTest( LDAP_DEBUG_STATS ) ) {
5191 rs->sr_err = pcache_parse_query_delete( op->ore_reqdata,
5192 &tag, &op->o_req_ndn, uuidp,
5193 &rs->sr_text, op->o_tmpmemctx );
5194 if ( rs->sr_err != LDAP_SUCCESS ) {
5198 if ( LogTest( LDAP_DEBUG_STATS ) ) {
5199 assert( !BER_BVISNULL( &op->o_req_ndn ) );
5200 len = snprintf( buf, sizeof( buf ), " dn=\"%s\"", op->o_req_ndn.bv_val );
5202 if ( !BER_BVISNULL( &uuid ) && len < sizeof( buf ) ) {
5203 snprintf( &buf[ len ], sizeof( buf ) - len, " pcacheQueryId=\"%s\"", uuid.bv_val );
5206 Debug( LDAP_DEBUG_STATS, "%s QUERY DELETE%s\n",
5207 op->o_log_prefix, buf, 0 );
5209 op->o_req_dn = op->o_req_ndn;
5211 op->o_bd = select_backend( &op->o_req_ndn, 0 );
5212 if ( op->o_bd == NULL ) {
5213 send_ldap_error( op, rs, LDAP_NO_SUCH_OBJECT,
5214 "no global superior knowledge" );
5216 rs->sr_err = backend_check_restrictions( op, rs,
5217 (struct berval *)&pcache_exop_QUERY_DELETE );
5218 if ( rs->sr_err != LDAP_SUCCESS ) {
5222 if ( op->o_bd->be_extended == NULL ) {
5223 send_ldap_error( op, rs, LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
5224 "backend does not support extended operations" );
5228 op->o_bd->be_extended( op, rs );
5231 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
5232 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
5233 BER_BVZERO( &op->o_req_ndn );
5234 BER_BVZERO( &op->o_req_dn );
5237 if ( !BER_BVISNULL( &uuid ) ) {
5238 op->o_tmpfree( uuid.bv_val, op->o_tmpmemctx );
5245 #endif /* PCACHE_EXOP_QUERY_DELETE */
5248 pcache_op_extended( Operation *op, SlapReply *rs )
5250 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
5251 cache_manager *cm = on->on_bi.bi_private;
5253 #ifdef PCACHE_CONTROL_PRIVDB
5254 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL ) {
5255 return pcache_op_privdb( op, rs );
5257 #endif /* PCACHE_CONTROL_PRIVDB */
5259 #ifdef PCACHE_EXOP_QUERY_DELETE
5260 if ( bvmatch( &op->ore_reqoid, &pcache_exop_QUERY_DELETE ) ) {
5261 struct berval uuid = BER_BVNULL;
5262 ber_tag_t tag = LBER_DEFAULT;
5264 rs->sr_err = pcache_parse_query_delete( op->ore_reqdata,
5265 &tag, NULL, &uuid, &rs->sr_text, op->o_tmpmemctx );
5266 assert( rs->sr_err == LDAP_SUCCESS );
5268 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_DN ) {
5269 /* remove all queries related to the selected entry */
5270 rs->sr_err = pcache_remove_entry_queries_from_cache( op,
5271 cm, &op->o_req_ndn, &uuid );
5273 } else if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_BASE ) {
5274 if ( !BER_BVISNULL( &uuid ) ) {
5275 /* remove the selected query */
5276 rs->sr_err = pcache_remove_query_from_cache( op,
5280 /* TODO: remove all queries */
5281 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5282 rs->sr_text = "deletion of all queries not implemented";
5286 op->o_tmpfree( uuid.bv_val, op->o_tmpmemctx );
5289 #endif /* PCACHE_EXOP_QUERY_DELETE */
5291 /* We only care if we're configured for Bind caching */
5292 if ( bvmatch( &op->ore_reqoid, &pcache_exop_MODIFY_PASSWD ) &&
5294 /* See if the local entry exists and has a password.
5295 * It's too much work to find the matching query, so
5296 * we just see if there's a hashed password to update.
5298 Operation op2 = *op;
5304 op2.o_dn = op->o_bd->be_rootdn;
5305 op2.o_ndn = op->o_bd->be_rootndn;
5306 rc = be_entry_get_rw( &op2, &op->o_req_ndn, NULL,
5307 slap_schema.si_ad_userPassword, 0, &e );
5308 if ( rc == LDAP_SUCCESS && e ) {
5309 /* See if a recognized password is hashed here */
5310 Attribute *a = attr_find( e->e_attrs,
5311 slap_schema.si_ad_userPassword );
5312 if ( a && a->a_vals[0].bv_val[0] == '{' &&
5313 lutil_passwd_scheme( a->a_vals[0].bv_val )) {
5316 be_entry_release_r( &op2, e );
5320 rc = overlay_op_walk( op, rs, op_extended, on->on_info,
5322 if ( rc == LDAP_SUCCESS ) {
5323 req_pwdexop_s *qpw = &op->oq_pwdexop;
5325 /* We don't care if it succeeds or not */
5326 pc_setpw( &op2, &qpw->rs_new, cm );
5331 return SLAP_CB_CONTINUE;
5335 pcache_entry_release( Operation *op, Entry *e, int rw )
5337 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
5338 cache_manager *cm = on->on_bi.bi_private;
5339 BackendDB *db = op->o_bd;
5343 rc = be_entry_release_rw( op, e, rw );
5348 #ifdef PCACHE_MONITOR
5351 pcache_monitor_update(
5357 cache_manager *cm = (cache_manager *) priv;
5358 query_manager *qm = cm->qm;
5361 BerVarray vals = NULL;
5363 attr_delete( &e->e_attrs, ad_cachedQueryURL );
5364 if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || ad_inlist( ad_cachedQueryURL, rs->sr_attrs ) )
5365 && qm->templates != NULL )
5369 for ( tm = qm->templates; tm != NULL; tm = tm->qmnext ) {
5370 for ( qc = tm->query; qc; qc = qc->next ) {
5373 if ( query2url( op, qc, &bv, 1 ) == 0 ) {
5374 ber_bvarray_add_x( &vals, &bv, op->o_tmpmemctx );
5380 if ( vals != NULL ) {
5381 attr_merge_normalize( e, ad_cachedQueryURL, vals, NULL );
5382 ber_bvarray_free_x( vals, op->o_tmpmemctx );
5388 char buf[ SLAP_TEXT_BUFLEN ];
5391 /* number of cached queries */
5392 a = attr_find( e->e_attrs, ad_numQueries );
5393 assert( a != NULL );
5396 bv.bv_len = snprintf( buf, sizeof( buf ), "%lu", cm->num_cached_queries );
5398 if ( a->a_nvals != a->a_vals ) {
5399 ber_bvreplace( &a->a_nvals[ 0 ], &bv );
5401 ber_bvreplace( &a->a_vals[ 0 ], &bv );
5403 /* number of cached entries */
5404 a = attr_find( e->e_attrs, ad_numEntries );
5405 assert( a != NULL );
5408 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", cm->cur_entries );
5410 if ( a->a_nvals != a->a_vals ) {
5411 ber_bvreplace( &a->a_nvals[ 0 ], &bv );
5413 ber_bvreplace( &a->a_vals[ 0 ], &bv );
5416 return SLAP_CB_CONTINUE;
5420 pcache_monitor_free(
5424 struct berval values[ 2 ];
5425 Modification mod = { 0 };
5428 char textbuf[ SLAP_TEXT_BUFLEN ];
5432 /* NOTE: if slap_shutdown != 0, priv might have already been freed */
5435 /* Remove objectClass */
5436 mod.sm_op = LDAP_MOD_DELETE;
5437 mod.sm_desc = slap_schema.si_ad_objectClass;
5438 mod.sm_values = values;
5440 values[ 0 ] = oc_olmPCache->soc_cname;
5441 BER_BVZERO( &values[ 1 ] );
5443 rc = modify_delete_values( e, &mod, 1, &text,
5444 textbuf, sizeof( textbuf ) );
5445 /* don't care too much about return code... */
5448 mod.sm_values = NULL;
5449 mod.sm_desc = ad_cachedQueryURL;
5451 rc = modify_delete_values( e, &mod, 1, &text,
5452 textbuf, sizeof( textbuf ) );
5453 /* don't care too much about return code... */
5456 mod.sm_values = NULL;
5457 mod.sm_desc = ad_numQueries;
5459 rc = modify_delete_values( e, &mod, 1, &text,
5460 textbuf, sizeof( textbuf ) );
5461 /* don't care too much about return code... */
5464 mod.sm_values = NULL;
5465 mod.sm_desc = ad_numEntries;
5467 rc = modify_delete_values( e, &mod, 1, &text,
5468 textbuf, sizeof( textbuf ) );
5469 /* don't care too much about return code... */
5471 return SLAP_CB_CONTINUE;
5475 * call from within pcache_initialize()
5478 pcache_monitor_initialize( void )
5480 static int pcache_monitor_initialized = 0;
5482 if ( backend_info( "monitor" ) == NULL ) {
5486 if ( pcache_monitor_initialized++ ) {
5494 pcache_monitor_db_init( BackendDB *be )
5496 if ( pcache_monitor_initialize() == LDAP_SUCCESS ) {
5497 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING;
5504 pcache_monitor_db_open( BackendDB *be )
5506 slap_overinst *on = (slap_overinst *)be->bd_info;
5507 cache_manager *cm = on->on_bi.bi_private;
5508 Attribute *a, *next;
5509 monitor_callback_t *cb = NULL;
5512 monitor_extra_t *mbe;
5513 struct berval dummy = BER_BVC( "" );
5515 if ( !SLAP_DBMONITORING( be ) ) {
5519 mi = backend_info( "monitor" );
5520 if ( !mi || !mi->bi_extra ) {
5521 SLAP_DBFLAGS( be ) ^= SLAP_DBFLAG_MONITORING;
5526 /* don't bother if monitor is not configured */
5527 if ( !mbe->is_configured() ) {
5528 static int warning = 0;
5530 if ( warning++ == 0 ) {
5531 Debug( LDAP_DEBUG_ANY, "pcache_monitor_db_open: "
5532 "monitoring disabled; "
5533 "configure monitor database to enable\n",
5540 /* alloc as many as required (plus 1 for objectClass) */
5541 a = attrs_alloc( 1 + 2 );
5547 a->a_desc = slap_schema.si_ad_objectClass;
5548 attr_valadd( a, &oc_olmPCache->soc_cname, NULL, 1 );
5552 struct berval bv = BER_BVC( "0" );
5554 next->a_desc = ad_numQueries;
5555 attr_valadd( next, &bv, NULL, 1 );
5556 next = next->a_next;
5558 next->a_desc = ad_numEntries;
5559 attr_valadd( next, &bv, NULL, 1 );
5560 next = next->a_next;
5563 cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
5564 cb->mc_update = pcache_monitor_update;
5565 cb->mc_free = pcache_monitor_free;
5566 cb->mc_private = (void *)cm;
5568 /* make sure the database is registered; then add monitor attributes */
5569 BER_BVZERO( &cm->monitor_ndn );
5570 rc = mbe->register_overlay( be, on, &cm->monitor_ndn );
5572 rc = mbe->register_entry_attrs( &cm->monitor_ndn, a, cb,
5573 &dummy, -1, &dummy);
5589 /* store for cleanup */
5590 cm->monitor_cb = (void *)cb;
5592 /* we don't need to keep track of the attributes, because
5593 * bdb_monitor_free() takes care of everything */
5602 pcache_monitor_db_close( BackendDB *be )
5604 slap_overinst *on = (slap_overinst *)be->bd_info;
5605 cache_manager *cm = on->on_bi.bi_private;
5607 if ( cm->monitor_cb != NULL ) {
5608 BackendInfo *mi = backend_info( "monitor" );
5609 monitor_extra_t *mbe;
5611 if ( mi && &mi->bi_extra ) {
5613 mbe->unregister_entry_callback( &cm->monitor_ndn,
5614 (monitor_callback_t *)cm->monitor_cb,
5623 pcache_monitor_db_destroy( BackendDB *be )
5628 #endif /* PCACHE_MONITOR */
5630 static slap_overinst pcache;
5632 static char *obsolete_names[] = {
5637 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
5639 #endif /* SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC */
5644 struct berval debugbv = BER_BVC("pcache");
5648 code = slap_loglevel_get( &debugbv, &pcache_debug );
5653 #ifdef PCACHE_CONTROL_PRIVDB
5654 code = register_supported_control( PCACHE_CONTROL_PRIVDB,
5655 SLAP_CTRL_BIND|SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE, extops,
5656 parse_privdb_ctrl, &privDB_cid );
5657 if ( code != LDAP_SUCCESS ) {
5658 Debug( LDAP_DEBUG_ANY,
5659 "pcache_initialize: failed to register control %s (%d)\n",
5660 PCACHE_CONTROL_PRIVDB, code, 0 );
5663 #endif /* PCACHE_CONTROL_PRIVDB */
5665 #ifdef PCACHE_EXOP_QUERY_DELETE
5666 code = load_extop2( (struct berval *)&pcache_exop_QUERY_DELETE,
5667 SLAP_EXOP_WRITES|SLAP_EXOP_HIDE, pcache_exop_query_delete,
5669 if ( code != LDAP_SUCCESS ) {
5670 Debug( LDAP_DEBUG_ANY,
5671 "pcache_initialize: unable to register queryDelete exop: %d.\n",
5675 #endif /* PCACHE_EXOP_QUERY_DELETE */
5677 argv[ 0 ] = "back-bdb/back-hdb monitor";
5682 for ( i = 0; s_oid[ i ].name; i++ ) {
5684 argv[ 1 ] = s_oid[ i ].name;
5685 argv[ 2 ] = s_oid[ i ].oid;
5687 if ( parse_oidm( &c, 0, NULL ) != 0 ) {
5688 Debug( LDAP_DEBUG_ANY, "pcache_initialize: "
5689 "unable to add objectIdentifier \"%s=%s\"\n",
5690 s_oid[ i ].name, s_oid[ i ].oid, 0 );
5695 for ( i = 0; s_ad[i].desc != NULL; i++ ) {
5696 code = register_at( s_ad[i].desc, s_ad[i].adp, 0 );
5698 Debug( LDAP_DEBUG_ANY,
5699 "pcache_initialize: register_at #%d failed\n", i, 0, 0 );
5702 (*s_ad[i].adp)->ad_type->sat_flags |= SLAP_AT_HIDE;
5705 for ( i = 0; s_oc[i].desc != NULL; i++ ) {
5706 code = register_oc( s_oc[i].desc, s_oc[i].ocp, 0 );
5708 Debug( LDAP_DEBUG_ANY,
5709 "pcache_initialize: register_oc #%d failed\n", i, 0, 0 );
5712 (*s_oc[i].ocp)->soc_flags |= SLAP_OC_HIDE;
5715 pcache.on_bi.bi_type = "pcache";
5716 pcache.on_bi.bi_obsolete_names = obsolete_names;
5717 pcache.on_bi.bi_db_init = pcache_db_init;
5718 pcache.on_bi.bi_db_config = pcache_db_config;
5719 pcache.on_bi.bi_db_open = pcache_db_open;
5720 pcache.on_bi.bi_db_close = pcache_db_close;
5721 pcache.on_bi.bi_db_destroy = pcache_db_destroy;
5723 pcache.on_bi.bi_op_search = pcache_op_search;
5724 pcache.on_bi.bi_op_bind = pcache_op_bind;
5725 #ifdef PCACHE_CONTROL_PRIVDB
5726 pcache.on_bi.bi_op_compare = pcache_op_privdb;
5727 pcache.on_bi.bi_op_modrdn = pcache_op_privdb;
5728 pcache.on_bi.bi_op_modify = pcache_op_privdb;
5729 pcache.on_bi.bi_op_add = pcache_op_privdb;
5730 pcache.on_bi.bi_op_delete = pcache_op_privdb;
5731 #endif /* PCACHE_CONTROL_PRIVDB */
5732 pcache.on_bi.bi_extended = pcache_op_extended;
5734 pcache.on_bi.bi_entry_release_rw = pcache_entry_release;
5735 pcache.on_bi.bi_chk_controls = pcache_chk_controls;
5737 pcache.on_bi.bi_cf_ocs = pcocs;
5739 code = config_register_schema( pccfg, pcocs );
5740 if ( code ) return code;
5742 return overlay_register( &pcache );
5745 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
5746 int init_module(int argc, char *argv[]) {
5747 return pcache_initialize();
5751 #endif /* defined(SLAPD_OVER_PROXYCACHE) */