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 unsigned long answerable_cnt; /* how many times it was answerable */
88 int refcnt; /* references since last refresh */
89 ldap_pvt_thread_mutex_t answerable_cnt_mutex;
90 struct cached_query_s *next; /* next query in the template */
91 struct cached_query_s *prev; /* previous query in the template */
92 struct cached_query_s *lru_up; /* previous query in the LRU list */
93 struct cached_query_s *lru_down; /* next query in the LRU list */
94 ldap_pvt_thread_rdwr_t rwlock;
100 * ldap:///<base>??<scope>?<filter>?x-uuid=<uid>,x-template=<template>,x-attrset=<attrset>,x-expiry=<expiry>,x-refresh=<refresh>
102 * <base> ::= CachedQuery.qbase->base
103 * <scope> ::= CachedQuery.scope
104 * <filter> ::= filter2bv(CachedQuery.filter)
105 * <uuid> ::= CachedQuery.q_uuid
106 * <attrset> ::= CachedQuery.qtemp->attr_set_index
107 * <expiry> ::= CachedQuery.expiry_time
108 * <refresh> ::= CachedQuery.refresh_time
110 * quick hack: parse URI, call add_query() and then fix
111 * CachedQuery.expiry_time and CachedQuery.q_uuid
113 * NOTE: if the <attrset> changes, all stored URLs will be invalidated.
117 * Represents a set of projected attributes.
121 struct query_template_s *templates;
122 AttributeName* attrs; /* specifies the set */
124 #define PC_CONFIGURED (0x1)
125 #define PC_REFERENCED (0x2)
126 #define PC_GOT_OC (0x4)
127 int count; /* number of attributes */
130 /* struct representing a query template
131 * e.g. template string = &(cn=)(mail=)
133 typedef struct query_template_s {
134 struct query_template_s *qtnext;
135 struct query_template_s *qmnext;
138 CachedQuery* query; /* most recent query cached for the template */
139 CachedQuery* query_last; /* oldest query cached for the template */
140 ldap_pvt_thread_rdwr_t t_rwlock; /* Rd/wr lock for accessing queries in the template */
141 struct berval querystr; /* Filter string corresponding to the QT */
142 struct berval bindbase; /* base DN for Bind request */
143 struct berval bindfilterstr; /* Filter string for Bind request */
144 struct berval bindftemp; /* bind filter template */
146 AttributeDescription **bindfattrs; /* attrs to substitute in ftemp */
148 int bindnattrs; /* number of bindfattrs */
150 int attr_set_index; /* determines the projected attributes */
151 int no_of_queries; /* Total number of queries in the template */
152 time_t ttl; /* TTL for the queries of this template */
153 time_t negttl; /* TTL for negative results */
154 time_t limitttl; /* TTL for sizelimit exceeding results */
155 time_t ttr; /* time to refresh */
156 time_t bindttr; /* TTR for cached binds */
157 struct attr_set t_attrs; /* filter attrs + attr_set */
165 } pc_caching_reason_t;
167 static const char *pc_caching_reason_str[] = {
176 struct query_manager_s;
178 /* prototypes for functions for 1) query containment
179 * 2) query addition, 3) cache replacement
181 typedef CachedQuery *(QCfunc)(Operation *op, struct query_manager_s*,
182 Query*, QueryTemplate*);
183 typedef CachedQuery *(AddQueryfunc)(Operation *op, struct query_manager_s*,
184 Query*, QueryTemplate*, pc_caching_reason_t, int wlock);
185 typedef void (CRfunc)(struct query_manager_s*, struct berval*);
187 /* LDAP query cache */
188 typedef struct query_manager_s {
189 struct attr_set* attr_sets; /* possible sets of projected attributes */
190 QueryTemplate* templates; /* cacheable templates */
192 CachedQuery* lru_top; /* top and bottom of LRU list */
193 CachedQuery* lru_bottom;
195 ldap_pvt_thread_mutex_t lru_mutex; /* mutex for accessing LRU list */
197 /* Query cache methods */
198 QCfunc *qcfunc; /* Query containment*/
199 CRfunc *crfunc; /* cache replacement */
200 AddQueryfunc *addfunc; /* add query */
203 /* LDAP query cache manager */
204 typedef struct cache_manager_s {
205 BackendDB db; /* underlying database */
206 unsigned long num_cached_queries; /* total number of cached queries */
207 unsigned long max_queries; /* upper bound on # of cached queries */
208 int save_queries; /* save cached queries across restarts */
209 int check_cacheability; /* check whether a query is cacheable */
210 int numattrsets; /* number of attribute sets */
211 int cur_entries; /* current number of entries cached */
212 int max_entries; /* max number of entries cached */
213 int num_entries_limit; /* max # of entries in a cacheable query */
215 char response_cb; /* install the response callback
216 * at the tail of the callback list */
217 #define PCACHE_RESPONSE_CB_HEAD 0
218 #define PCACHE_RESPONSE_CB_TAIL 1
219 char defer_db_open; /* defer open for online add */
220 char cache_binds; /* cache binds or just passthru */
222 time_t cc_period; /* interval between successive consistency checks (sec) */
223 #define PCACHE_CC_PAUSED 1
224 #define PCACHE_CC_OFFLINE 2
228 ldap_pvt_thread_mutex_t cache_mutex;
230 query_manager* qm; /* query cache managed by the cache manager */
232 #ifdef PCACHE_MONITOR
234 struct berval monitor_ndn;
235 #endif /* PCACHE_MONITOR */
238 #ifdef PCACHE_MONITOR
239 static int pcache_monitor_db_init( BackendDB *be );
240 static int pcache_monitor_db_open( BackendDB *be );
241 static int pcache_monitor_db_close( BackendDB *be );
242 static int pcache_monitor_db_destroy( BackendDB *be );
243 #endif /* PCACHE_MONITOR */
245 static int pcache_debug;
247 #ifdef PCACHE_CONTROL_PRIVDB
248 static int privDB_cid;
249 #endif /* PCACHE_CONTROL_PRIVDB */
251 static AttributeDescription *ad_queryId, *ad_cachedQueryURL;
253 #ifdef PCACHE_MONITOR
254 static AttributeDescription *ad_numQueries, *ad_numEntries;
255 static ObjectClass *oc_olmPCache;
256 #endif /* PCACHE_MONITOR */
262 { "PCacheOID", "1.3.6.1.4.1.4203.666.11.9.1" },
263 { "PCacheAttributes", "PCacheOID:1" },
264 { "PCacheObjectClasses", "PCacheOID:2" },
271 AttributeDescription **adp;
273 { "( PCacheAttributes:1 "
274 "NAME 'pcacheQueryID' "
275 "DESC 'ID of query the entry belongs to, formatted as a UUID' "
276 "EQUALITY octetStringMatch "
277 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40{64} "
278 "NO-USER-MODIFICATION "
279 "USAGE directoryOperation )",
281 { "( PCacheAttributes:2 "
282 "NAME 'pcacheQueryURL' "
283 "DESC 'URI describing a cached query' "
284 "EQUALITY caseExactMatch "
285 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 "
286 "NO-USER-MODIFICATION "
287 "USAGE directoryOperation )",
288 &ad_cachedQueryURL },
289 #ifdef PCACHE_MONITOR
290 { "( PCacheAttributes:3 "
291 "NAME 'pcacheNumQueries' "
292 "DESC 'Number of cached queries' "
293 "EQUALITY integerMatch "
294 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
295 "NO-USER-MODIFICATION "
296 "USAGE directoryOperation )",
298 { "( PCacheAttributes:4 "
299 "NAME 'pcacheNumEntries' "
300 "DESC 'Number of cached entries' "
301 "EQUALITY integerMatch "
302 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
303 "NO-USER-MODIFICATION "
304 "USAGE directoryOperation )",
306 #endif /* PCACHE_MONITOR */
315 #ifdef PCACHE_MONITOR
316 /* augments an existing object, so it must be AUXILIARY */
317 { "( PCacheObjectClasses:1 "
318 "NAME ( 'olmPCache' ) "
322 "$ pcacheNumQueries "
323 "$ pcacheNumEntries "
326 #endif /* PCACHE_MONITOR */
335 struct berval *fstr );
342 QueryTemplate *templ,
343 pc_caching_reason_t why,
349 struct berval *query_uuid );
352 * Turn a cached query into its URL representation
355 query2url( Operation *op, CachedQuery *q, struct berval *urlbv, int dolock )
357 struct berval bv_scope,
359 char attrset_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
360 expiry_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
361 refresh_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
362 answerable_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
364 ber_len_t attrset_len,
370 ldap_pvt_thread_rdwr_rlock( &q->rwlock );
373 ldap_pvt_scope2bv( q->scope, &bv_scope );
374 filter2bv_x( op, q->filter, &bv_filter );
375 attrset_len = sprintf( attrset_buf,
376 "%lu", (unsigned long)q->qtemp->attr_set_index );
377 expiry_len = sprintf( expiry_buf,
378 "%lu", (unsigned long)q->expiry_time );
379 answerable_len = snprintf( answerable_buf, sizeof( answerable_buf ),
380 "%lu", q->answerable_cnt );
381 if ( q->refresh_time )
382 refresh_len = sprintf( refresh_buf,
383 "%lu", (unsigned long)q->refresh_time );
387 urlbv->bv_len = STRLENOF( "ldap:///" )
388 + q->qbase->base.bv_len
393 + STRLENOF( "?x-uuid=" )
395 + STRLENOF( ",x-attrset=" )
397 + STRLENOF( ",x-expiry=" )
399 + STRLENOF( ",x-answerable=" )
402 urlbv->bv_len += STRLENOF( ",x-refresh=" )
405 ptr = urlbv->bv_val = ber_memalloc_x( urlbv->bv_len + 1, op->o_tmpmemctx );
406 ptr = lutil_strcopy( ptr, "ldap:///" );
407 ptr = lutil_strcopy( ptr, q->qbase->base.bv_val );
408 ptr = lutil_strcopy( ptr, "??" );
409 ptr = lutil_strcopy( ptr, bv_scope.bv_val );
410 ptr = lutil_strcopy( ptr, "?" );
411 ptr = lutil_strcopy( ptr, bv_filter.bv_val );
412 ptr = lutil_strcopy( ptr, "?x-uuid=" );
413 ptr = lutil_strcopy( ptr, q->q_uuid.bv_val );
414 ptr = lutil_strcopy( ptr, ",x-attrset=" );
415 ptr = lutil_strcopy( ptr, attrset_buf );
416 ptr = lutil_strcopy( ptr, ",x-expiry=" );
417 ptr = lutil_strcopy( ptr, expiry_buf );
418 ptr = lutil_strcopy( ptr, ",x-answerable=" );
419 ptr = lutil_strcopy( ptr, answerable_buf );
421 ptr = lutil_strcopy( ptr, ",x-refresh=" );
422 ptr = lutil_strcopy( ptr, refresh_buf );
425 ber_memfree_x( bv_filter.bv_val, op->o_tmpmemctx );
428 ldap_pvt_thread_rdwr_runlock( &q->rwlock );
434 /* Find and record the empty filter clauses */
437 ftemp_attrs( struct berval *ftemp, struct berval *template,
438 AttributeDescription ***ret, const char **text )
444 AttributeDescription *ad;
445 AttributeDescription **descs = NULL;
448 temp2 = ch_malloc( ftemp->bv_len );
455 while ( *p1 == '(' || *p1 == '&' || *p1 == '|' || *p1 == ')' )
458 p2 = strchr( p1, '=' );
462 AC_MEMCPY( t1, p1, i );
466 if ( p2[-1] == '<' || p2[-1] == '>' ) p2--;
470 i = slap_bv2ad( &bv, &ad, text );
475 if ( *p2 == '<' || *p2 == '>' ) p2++;
476 if ( p2[1] != ')' ) {
478 while ( *p2 != ')' ) p2++;
483 descs = (AttributeDescription **)ch_realloc(descs,
484 (attr_cnt + 2)*sizeof(AttributeDescription *));
486 descs[attr_cnt++] = ad;
491 descs[attr_cnt] = NULL;
493 template->bv_val = temp2;
494 template->bv_len = t1 - temp2;
499 template_attrs( char *template, struct attr_set *set, AttributeName **ret,
510 AttributeDescription *ad;
511 AttributeName *attrs;
517 attrs = ch_calloc( set->count + 1, sizeof(AttributeName) );
518 for ( i=0; i < set->count; i++ )
519 attrs[i] = set->attrs[i];
521 alluser = an_find( attrs, slap_bv_all_user_attrs );
522 allop = an_find( attrs, slap_bv_all_operational_attrs );
525 while ( *p1 == '(' || *p1 == '&' || *p1 == '|' || *p1 == ')' ) p1++;
526 p2 = strchr( p1, '=' );
529 if ( p2[-1] == '<' || p2[-1] == '>' ) p2--;
533 i = slap_bv2ad( &bv, &ad, text );
540 if ( ad == slap_schema.si_ad_objectClass )
543 if ( is_at_operational(ad->ad_type)) {
547 } else if ( alluser ) {
550 if ( !ad_inlist( ad, attrs )) {
551 attrs = (AttributeName *)ch_realloc(attrs,
552 (attr_cnt + 2)*sizeof(AttributeName));
554 attrs[attr_cnt].an_desc = ad;
555 attrs[attr_cnt].an_name = ad->ad_cname;
556 attrs[attr_cnt].an_oc = NULL;
557 attrs[attr_cnt].an_flags = 0;
558 BER_BVZERO( &attrs[attr_cnt+1].an_name );
566 *text = "couldn't parse template";
569 if ( !got_oc && !( set->flags & PC_GOT_OC )) {
570 attrs = (AttributeName *)ch_realloc(attrs,
571 (attr_cnt + 2)*sizeof(AttributeName));
573 ad = slap_schema.si_ad_objectClass;
574 attrs[attr_cnt].an_desc = ad;
575 attrs[attr_cnt].an_name = ad->ad_cname;
576 attrs[attr_cnt].an_oc = NULL;
577 attrs[attr_cnt].an_flags = 0;
578 BER_BVZERO( &attrs[attr_cnt+1].an_name );
586 * Turn an URL representing a formerly cached query into a cached query,
587 * and try to cache it
598 LDAPURLDesc *lud = NULL;
600 tempstr = BER_BVNULL,
605 unsigned long answerable_cnt;
608 #define GOT_UUID 0x1U
609 #define GOT_ATTRSET 0x2U
610 #define GOT_EXPIRY 0x4U
611 #define GOT_ANSWERABLE 0x8U
612 #define GOT_REFRESH 0x10U
613 #define GOT_ALL (GOT_UUID|GOT_ATTRSET|GOT_EXPIRY|GOT_ANSWERABLE)
616 rc = ldap_url_parse( url, &lud );
617 if ( rc != LDAP_URL_SUCCESS ) {
621 /* non-allowed fields */
622 if ( lud->lud_host != NULL ) {
627 if ( lud->lud_attrs != NULL ) {
633 if ( strcmp( lud->lud_scheme, "ldap" ) != 0 ) {
638 /* required fields */
639 if ( lud->lud_dn == NULL || lud->lud_dn[ 0 ] == '\0' ) {
644 switch ( lud->lud_scope ) {
645 case LDAP_SCOPE_BASE:
646 case LDAP_SCOPE_ONELEVEL:
647 case LDAP_SCOPE_SUBTREE:
648 case LDAP_SCOPE_SUBORDINATE:
656 if ( lud->lud_filter == NULL || lud->lud_filter[ 0 ] == '\0' ) {
661 if ( lud->lud_exts == NULL ) {
666 for ( i = 0; lud->lud_exts[ i ] != NULL; i++ ) {
667 if ( strncmp( lud->lud_exts[ i ], "x-uuid=", STRLENOF( "x-uuid=" ) ) == 0 ) {
668 struct berval tmpUUID;
669 Syntax *syn_UUID = slap_schema.si_ad_entryUUID->ad_type->sat_syntax;
671 if ( got & GOT_UUID ) {
676 ber_str2bv( &lud->lud_exts[ i ][ STRLENOF( "x-uuid=" ) ], 0, 0, &tmpUUID );
677 if ( !BER_BVISEMPTY( &tmpUUID ) ) {
678 rc = syn_UUID->ssyn_pretty( syn_UUID, &tmpUUID, &uuid, NULL );
679 if ( rc != LDAP_SUCCESS ) {
685 } else if ( strncmp( lud->lud_exts[ i ], "x-attrset=", STRLENOF( "x-attrset=" ) ) == 0 ) {
686 if ( got & GOT_ATTRSET ) {
691 rc = lutil_atoi( &attrset, &lud->lud_exts[ i ][ STRLENOF( "x-attrset=" ) ] );
697 } else if ( strncmp( lud->lud_exts[ i ], "x-expiry=", STRLENOF( "x-expiry=" ) ) == 0 ) {
700 if ( got & GOT_EXPIRY ) {
705 rc = lutil_atoul( &l, &lud->lud_exts[ i ][ STRLENOF( "x-expiry=" ) ] );
709 expiry_time = (time_t)l;
712 } else if ( strncmp( lud->lud_exts[ i ], "x-answerable=", STRLENOF( "x-answerable=" ) ) == 0 ) {
713 if ( got & GOT_ANSWERABLE ) {
718 rc = lutil_atoul( &answerable_cnt, &lud->lud_exts[ i ][ STRLENOF( "x-answerable=" ) ] );
722 got |= GOT_ANSWERABLE;
724 } else if ( strncmp( lud->lud_exts[ i ], "x-refresh=", STRLENOF( "x-refresh=" ) ) == 0 ) {
727 if ( got & GOT_REFRESH ) {
732 rc = lutil_atoul( &l, &lud->lud_exts[ i ][ STRLENOF( "x-refresh=" ) ] );
736 refresh_time = (time_t)l;
745 if ( got != GOT_ALL ) {
750 if ( !(got & GOT_REFRESH ))
753 /* ignore expired queries */
754 if ( expiry_time <= slap_get_time()) {
757 memset( &op2.oq_search, 0, sizeof( op2.oq_search ) );
759 (void)remove_query_data( &op2, &uuid );
764 ber_str2bv( lud->lud_dn, 0, 0, &base );
765 rc = dnNormalize( 0, NULL, NULL, &base, &query.base, NULL );
766 if ( rc != LDAP_SUCCESS ) {
769 query.scope = lud->lud_scope;
770 query.filter = str2filter( lud->lud_filter );
771 if ( query.filter == NULL ) {
776 tempstr.bv_val = ch_malloc( strlen( lud->lud_filter ) + 1 );
778 if ( filter2template( op, query.filter, &tempstr ) ) {
779 ch_free( tempstr.bv_val );
784 /* check for query containment */
785 qt = qm->attr_sets[attrset].templates;
786 for ( ; qt; qt = qt->qtnext ) {
787 /* find if template i can potentially answer tempstr */
788 if ( bvmatch( &qt->querystr, &tempstr ) ) {
798 cq = add_query( op, qm, &query, qt, PC_POSITIVE, 0 );
800 cq->expiry_time = expiry_time;
801 cq->refresh_time = refresh_time;
803 cq->answerable_cnt = answerable_cnt;
806 /* it's now into cq->filter */
816 if ( query.filter != NULL ) filter_free( query.filter );
817 if ( !BER_BVISNULL( &tempstr ) ) ch_free( tempstr.bv_val );
818 if ( !BER_BVISNULL( &query.base ) ) ch_free( query.base.bv_val );
819 if ( !BER_BVISNULL( &uuid ) ) ch_free( uuid.bv_val );
820 if ( lud != NULL ) ldap_free_urldesc( lud );
825 /* Return 1 for an added entry, else 0 */
831 struct berval* query_uuid )
834 Modifications* modlist = NULL;
835 const char* text = NULL;
837 char textbuf[SLAP_TEXT_BUFLEN];
838 size_t textlen = sizeof(textbuf);
840 SlapReply sreply = {REP_RESULT};
842 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
849 /* add queryId attribute */
850 attr_merge_one( e, ad_queryId, query_uuid, NULL );
852 /* append the attribute list from the fetched entry */
853 e->e_attrs->a_next = attr;
855 op->o_tag = LDAP_REQ_ADD;
856 op->o_protocol = LDAP_VERSION3;
857 op->o_callback = &cb;
858 op->o_time = slap_get_time();
859 op->o_do_not_cache = 1;
862 op->o_req_dn = e->e_name;
863 op->o_req_ndn = e->e_nname;
864 rc = op->o_bd->be_add( op, &sreply );
866 if ( rc != LDAP_SUCCESS ) {
867 if ( rc == LDAP_ALREADY_EXISTS ) {
868 rs_reinit( &sreply, REP_RESULT );
869 slap_entry2mods( e, &modlist, &text, textbuf, textlen );
870 modlist->sml_op = LDAP_MOD_ADD;
871 op->o_tag = LDAP_REQ_MODIFY;
872 op->orm_modlist = modlist;
873 op->o_managedsait = SLAP_CONTROL_CRITICAL;
874 op->o_bd->be_modify( op, &sreply );
875 slap_mods_free( modlist, 1 );
876 } else if ( rc == LDAP_REFERRAL ||
877 rc == LDAP_NO_SUCH_OBJECT ) {
878 syncrepl_add_glue( op, e );
887 if ( op->ora_e == e )
895 /* Length-ordered sort on normalized DNs */
896 static int pcache_dn_cmp( const void *v1, const void *v2 )
898 const Qbase *q1 = v1, *q2 = v2;
900 int rc = q1->base.bv_len - q2->base.bv_len;
902 rc = strncmp( q1->base.bv_val, q2->base.bv_val, q1->base.bv_len );
906 static int lex_bvcmp( struct berval *bv1, struct berval *bv2 )
909 dif = bv1->bv_len - bv2->bv_len;
911 if ( dif > 0 ) len -= dif;
912 len = memcmp( bv1->bv_val, bv2->bv_val, len );
918 /* compare the current value in each filter */
919 static int pcache_filter_cmp( Filter *f1, Filter *f2 )
921 int rc, weight1, weight2;
923 switch( f1->f_choice ) {
924 case LDAP_FILTER_PRESENT:
927 case LDAP_FILTER_EQUALITY:
935 switch( f2->f_choice ) {
936 case LDAP_FILTER_PRESENT:
939 case LDAP_FILTER_EQUALITY:
947 rc = weight1 - weight2;
953 rc = lex_bvcmp( &f1->f_av_value, &f2->f_av_value );
956 if ( f1->f_choice == LDAP_FILTER_SUBSTRINGS ) {
958 if ( !BER_BVISNULL( &f1->f_sub_initial )) {
959 if ( !BER_BVISNULL( &f2->f_sub_initial )) {
960 rc = lex_bvcmp( &f1->f_sub_initial,
961 &f2->f_sub_initial );
965 } else if ( !BER_BVISNULL( &f2->f_sub_initial )) {
969 if ( f1->f_sub_any ) {
970 if ( f2->f_sub_any ) {
971 rc = lex_bvcmp( f1->f_sub_any,
976 } else if ( f2->f_sub_any ) {
980 if ( !BER_BVISNULL( &f1->f_sub_final )) {
981 if ( !BER_BVISNULL( &f2->f_sub_final )) {
982 rc = lex_bvcmp( &f1->f_sub_final,
987 } else if ( !BER_BVISNULL( &f2->f_sub_final )) {
991 rc = lex_bvcmp( &f1->f_mr_value,
1005 while ( f1->f_choice == LDAP_FILTER_AND || f1->f_choice == LDAP_FILTER_OR )
1007 while ( f2->f_choice == LDAP_FILTER_AND || f2->f_choice == LDAP_FILTER_OR )
1009 rc = pcache_filter_cmp( f1, f2 );
1017 /* compare filters in each query */
1018 static int pcache_query_cmp( const void *v1, const void *v2 )
1020 const CachedQuery *q1 = v1, *q2 =v2;
1021 return pcache_filter_cmp( q1->first, q2->first );
1024 /* add query on top of LRU list */
1026 add_query_on_top (query_manager* qm, CachedQuery* qc)
1028 CachedQuery* top = qm->lru_top;
1035 qm->lru_bottom = qc;
1039 Debug( pcache_debug, "Base of added query = %s\n",
1040 qc->qbase->base.bv_val, 0, 0 );
1043 /* remove_query from LRU list */
1046 remove_query (query_manager* qm, CachedQuery* qc)
1055 down = qc->lru_down;
1061 qm->lru_bottom = up;
1067 up->lru_down = down;
1069 qc->lru_up = qc->lru_down = NULL;
1072 /* find and remove string2 from string1
1073 * from start if position = 1,
1074 * from end if position = 3,
1075 * from anywhere if position = 2
1076 * string1 is overwritten if position = 2.
1080 find_and_remove(struct berval* ber1, struct berval* ber2, int position)
1084 if ( !ber2->bv_val )
1086 if ( !ber1->bv_val )
1089 switch( position ) {
1091 if ( ber1->bv_len >= ber2->bv_len && !memcmp( ber1->bv_val,
1092 ber2->bv_val, ber2->bv_len )) {
1094 ber1->bv_val += ber2->bv_len;
1095 ber1->bv_len -= ber2->bv_len;
1100 ber1->bv_val[ber1->bv_len] = '\0';
1101 temp = strstr( ber1->bv_val, ber2->bv_val );
1103 strcpy( temp, temp+ber2->bv_len );
1104 ber1->bv_len -= ber2->bv_len;
1110 if ( ber1->bv_len >= ber2->bv_len &&
1111 !memcmp( ber1->bv_val+ber1->bv_len-ber2->bv_len, ber2->bv_val,
1114 ber1->bv_len -= ber2->bv_len;
1122 static struct berval*
1123 merge_init_final(Operation *op, struct berval* init, struct berval* any,
1124 struct berval* final)
1126 struct berval* merged, *temp;
1127 int i, any_count, count;
1129 for (any_count=0; any && any[any_count].bv_val; any_count++)
1139 merged = (struct berval*)op->o_tmpalloc( (count+1)*sizeof(struct berval),
1144 ber_dupbv_x( temp, init, op->o_tmpmemctx );
1148 for (i=0; i<any_count; i++) {
1149 ber_dupbv_x( temp, any, op->o_tmpmemctx );
1154 ber_dupbv_x( temp, final, op->o_tmpmemctx );
1161 /* Each element in stored must be found in incoming. Incoming is overwritten.
1164 strings_containment(struct berval* stored, struct berval* incoming)
1166 struct berval* element;
1170 for ( element=stored; element->bv_val != NULL; element++ ) {
1171 for (j = k; incoming[j].bv_val != NULL; j++) {
1172 if (find_and_remove(&(incoming[j]), element, 2)) {
1189 substr_containment_substr(Operation *op, Filter* stored, Filter* incoming)
1193 struct berval init_incoming;
1194 struct berval final_incoming;
1195 struct berval *remaining_incoming = NULL;
1197 if ((!(incoming->f_sub_initial.bv_val) && (stored->f_sub_initial.bv_val))
1198 || (!(incoming->f_sub_final.bv_val) && (stored->f_sub_final.bv_val)))
1201 init_incoming = incoming->f_sub_initial;
1202 final_incoming = incoming->f_sub_final;
1204 if (find_and_remove(&init_incoming,
1205 &(stored->f_sub_initial), 1) && find_and_remove(&final_incoming,
1206 &(stored->f_sub_final), 3))
1208 if (stored->f_sub_any == NULL) {
1212 remaining_incoming = merge_init_final(op, &init_incoming,
1213 incoming->f_sub_any, &final_incoming);
1214 rc = strings_containment(stored->f_sub_any, remaining_incoming);
1215 ber_bvarray_free_x( remaining_incoming, op->o_tmpmemctx );
1222 substr_containment_equality(Operation *op, Filter* stored, Filter* incoming)
1224 struct berval incoming_val[2];
1227 incoming_val[1] = incoming->f_av_value;
1229 if (find_and_remove(incoming_val+1,
1230 &(stored->f_sub_initial), 1) && find_and_remove(incoming_val+1,
1231 &(stored->f_sub_final), 3)) {
1232 if (stored->f_sub_any == NULL){
1236 ber_dupbv_x( incoming_val, incoming_val+1, op->o_tmpmemctx );
1237 BER_BVZERO( incoming_val+1 );
1238 rc = strings_containment(stored->f_sub_any, incoming_val);
1239 op->o_tmpfree( incoming_val[0].bv_val, op->o_tmpmemctx );
1246 filter_first( Filter *f )
1248 while ( f->f_choice == LDAP_FILTER_OR || f->f_choice == LDAP_FILTER_AND )
1254 static CachedQuery *
1255 find_filter( Operation *op, Avlnode *root, Filter *inputf, Filter *first )
1259 MatchingRule* mrule = NULL;
1260 int res=0, eqpass= 0;
1263 CachedQuery cq, *qc;
1268 /* substring matches sort to the end, and we just have to
1269 * walk the entire list.
1271 if ( first->f_choice == LDAP_FILTER_SUBSTRINGS ) {
1272 ptr = tavl_end( root, 1 );
1273 dir = TAVL_DIR_LEFT;
1275 ptr = tavl_find3( root, &cq, pcache_query_cmp, &ret );
1276 dir = (first->f_choice == LDAP_FILTER_GE) ? TAVL_DIR_LEFT :
1285 /* an incoming substr query can only be satisfied by a cached
1288 if ( first->f_choice == LDAP_FILTER_SUBSTRINGS &&
1289 qc->first->f_choice != LDAP_FILTER_SUBSTRINGS )
1292 /* an incoming eq query can be satisfied by a cached eq or substr
1295 if ( first->f_choice == LDAP_FILTER_EQUALITY ) {
1296 if ( eqpass == 0 ) {
1297 if ( qc->first->f_choice != LDAP_FILTER_EQUALITY ) {
1298 nextpass: eqpass = 1;
1299 ptr = tavl_end( root, 1 );
1300 dir = TAVL_DIR_LEFT;
1304 if ( qc->first->f_choice != LDAP_FILTER_SUBSTRINGS )
1310 switch (fs->f_choice) {
1311 case LDAP_FILTER_EQUALITY:
1312 if (fi->f_choice == LDAP_FILTER_EQUALITY)
1313 mrule = fs->f_ava->aa_desc->ad_type->sat_equality;
1317 case LDAP_FILTER_GE:
1318 case LDAP_FILTER_LE:
1319 mrule = fs->f_ava->aa_desc->ad_type->sat_ordering;
1326 rc = value_match(&ret, fs->f_ava->aa_desc, mrule,
1327 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
1328 &(fi->f_ava->aa_value),
1329 &(fs->f_ava->aa_value), &text);
1330 if (rc != LDAP_SUCCESS) {
1333 if ( fi==first && fi->f_choice==LDAP_FILTER_EQUALITY && ret )
1336 switch (fs->f_choice) {
1337 case LDAP_FILTER_OR:
1338 case LDAP_FILTER_AND:
1343 case LDAP_FILTER_SUBSTRINGS:
1344 /* check if the equality query can be
1345 * answered with cached substring query */
1346 if ((fi->f_choice == LDAP_FILTER_EQUALITY)
1347 && substr_containment_equality( op,
1350 /* check if the substring query can be
1351 * answered with cached substring query */
1352 if ((fi->f_choice ==LDAP_FILTER_SUBSTRINGS
1353 ) && substr_containment_substr( op,
1359 case LDAP_FILTER_PRESENT:
1364 case LDAP_FILTER_EQUALITY:
1370 case LDAP_FILTER_GE:
1371 if (mrule && ret >= 0)
1376 case LDAP_FILTER_LE:
1377 if (mrule && ret <= 0)
1382 case LDAP_FILTER_NOT:
1388 } while((res) && (fi != NULL) && (fs != NULL));
1392 ptr = tavl_next( ptr, dir );
1397 /* check whether query is contained in any of
1398 * the cached queries in template
1400 static CachedQuery *
1401 query_containment(Operation *op, query_manager *qm,
1403 QueryTemplate *templa)
1406 int depth = 0, tscope;
1407 Qbase qbase, *qbptr = NULL;
1410 if (query->filter != NULL) {
1413 Debug( pcache_debug, "Lock QC index = %p\n",
1414 (void *) templa, 0, 0 );
1415 qbase.base = query->base;
1417 first = filter_first( query->filter );
1419 ldap_pvt_thread_rdwr_rlock(&templa->t_rwlock);
1422 qbptr = avl_find( templa->qbase, &qbase, pcache_dn_cmp );
1424 tscope = query->scope;
1425 /* Find a matching scope:
1426 * match at depth 0 OK
1429 * subord at depth > 0 OK
1430 * subtree at any depth OK
1432 * subtree or subord at any depth OK
1434 * subtree or subord at any depth OK
1436 * subord at depth > 0 OK
1437 * subtree at any depth OK
1439 for ( tscope = 0 ; tscope <= LDAP_SCOPE_CHILDREN; tscope++ ) {
1440 switch ( query->scope ) {
1441 case LDAP_SCOPE_BASE:
1442 if ( tscope == LDAP_SCOPE_BASE && depth ) continue;
1443 if ( tscope == LDAP_SCOPE_ONE && depth != 1) continue;
1444 if ( tscope == LDAP_SCOPE_CHILDREN && !depth ) continue;
1446 case LDAP_SCOPE_ONE:
1447 if ( tscope == LDAP_SCOPE_BASE )
1448 tscope = LDAP_SCOPE_ONE;
1449 if ( tscope == LDAP_SCOPE_ONE && depth ) continue;
1450 if ( !depth ) break;
1451 if ( tscope < LDAP_SCOPE_SUBTREE )
1452 tscope = LDAP_SCOPE_SUBTREE;
1454 case LDAP_SCOPE_SUBTREE:
1455 if ( tscope < LDAP_SCOPE_SUBTREE )
1456 tscope = LDAP_SCOPE_SUBTREE;
1457 if ( tscope == LDAP_SCOPE_CHILDREN && !depth ) continue;
1459 case LDAP_SCOPE_CHILDREN:
1460 if ( tscope < LDAP_SCOPE_SUBTREE )
1461 tscope = LDAP_SCOPE_SUBTREE;
1464 if ( !qbptr->scopes[tscope] ) continue;
1467 qc = find_filter( op, qbptr->scopes[tscope],
1468 query->filter, first );
1470 if ( qc->q_sizelimit ) {
1471 ldap_pvt_thread_rdwr_runlock(&templa->t_rwlock);
1474 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1475 if (qm->lru_top != qc) {
1476 remove_query(qm, qc);
1477 add_query_on_top(qm, qc);
1479 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1484 if ( be_issuffix( op->o_bd, &qbase.base ))
1487 dnParent( &qbase.base, &pdn );
1492 Debug( pcache_debug,
1493 "Not answerable: Unlock QC index=%p\n",
1494 (void *) templa, 0, 0 );
1495 ldap_pvt_thread_rdwr_runlock(&templa->t_rwlock);
1501 free_query (CachedQuery* qc)
1503 free(qc->q_uuid.bv_val);
1504 filter_free(qc->filter);
1505 ldap_pvt_thread_mutex_destroy(&qc->answerable_cnt_mutex);
1506 ldap_pvt_thread_rdwr_destroy( &qc->rwlock );
1507 memset(qc, 0, sizeof(*qc));
1512 /* Add query to query cache, the returned Query is locked for writing */
1513 static CachedQuery *
1518 QueryTemplate *templ,
1519 pc_caching_reason_t why,
1522 CachedQuery* new_cached_query = (CachedQuery*) ch_malloc(sizeof(CachedQuery));
1526 time_t ttl = 0, ttr = 0;
1529 new_cached_query->qtemp = templ;
1530 BER_BVZERO( &new_cached_query->q_uuid );
1531 new_cached_query->q_sizelimit = 0;
1533 now = slap_get_time();
1538 ttr = now + templ->ttr;
1542 ttl = templ->negttl;
1546 ttl = templ->limitttl;
1553 new_cached_query->expiry_time = now + ttl;
1554 new_cached_query->refresh_time = ttr;
1556 new_cached_query->answerable_cnt = 0;
1557 new_cached_query->refcnt = 1;
1558 ldap_pvt_thread_mutex_init(&new_cached_query->answerable_cnt_mutex);
1560 new_cached_query->lru_up = NULL;
1561 new_cached_query->lru_down = NULL;
1562 Debug( pcache_debug, "Added query expires at %ld (%s)\n",
1563 (long) new_cached_query->expiry_time,
1564 pc_caching_reason_str[ why ], 0 );
1566 new_cached_query->scope = query->scope;
1567 new_cached_query->filter = query->filter;
1568 new_cached_query->first = first = filter_first( query->filter );
1570 ldap_pvt_thread_rdwr_init(&new_cached_query->rwlock);
1572 ldap_pvt_thread_rdwr_wlock(&new_cached_query->rwlock);
1574 qb.base = query->base;
1576 /* Adding a query */
1577 Debug( pcache_debug, "Lock AQ index = %p\n",
1578 (void *) templ, 0, 0 );
1579 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
1580 qbase = avl_find( templ->qbase, &qb, pcache_dn_cmp );
1582 qbase = ch_calloc( 1, sizeof(Qbase) + qb.base.bv_len + 1 );
1583 qbase->base.bv_len = qb.base.bv_len;
1584 qbase->base.bv_val = (char *)(qbase+1);
1585 memcpy( qbase->base.bv_val, qb.base.bv_val, qb.base.bv_len );
1586 qbase->base.bv_val[qbase->base.bv_len] = '\0';
1587 avl_insert( &templ->qbase, qbase, pcache_dn_cmp, avl_dup_error );
1589 new_cached_query->next = templ->query;
1590 new_cached_query->prev = NULL;
1591 new_cached_query->qbase = qbase;
1592 rc = tavl_insert( &qbase->scopes[query->scope], new_cached_query,
1593 pcache_query_cmp, avl_dup_error );
1596 if (templ->query == NULL)
1597 templ->query_last = new_cached_query;
1599 templ->query->prev = new_cached_query;
1600 templ->query = new_cached_query;
1601 templ->no_of_queries++;
1603 ldap_pvt_thread_mutex_destroy(&new_cached_query->answerable_cnt_mutex);
1604 ldap_pvt_thread_rdwr_destroy( &new_cached_query->rwlock );
1605 ch_free( new_cached_query );
1606 new_cached_query = find_filter( op, qbase->scopes[query->scope],
1607 query->filter, first );
1608 filter_free( query->filter );
1609 query->filter = NULL;
1611 Debug( pcache_debug, "TEMPLATE %p QUERIES++ %d\n",
1612 (void *) templ, templ->no_of_queries, 0 );
1614 Debug( pcache_debug, "Unlock AQ index = %p \n",
1615 (void *) templ, 0, 0 );
1616 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
1618 /* Adding on top of LRU list */
1620 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1621 add_query_on_top(qm, new_cached_query);
1622 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1624 return rc == 0 ? new_cached_query : NULL;
1628 remove_from_template (CachedQuery* qc, QueryTemplate* template)
1630 if (!qc->prev && !qc->next) {
1631 template->query_last = template->query = NULL;
1632 } else if (qc->prev == NULL) {
1633 qc->next->prev = NULL;
1634 template->query = qc->next;
1635 } else if (qc->next == NULL) {
1636 qc->prev->next = NULL;
1637 template->query_last = qc->prev;
1639 qc->next->prev = qc->prev;
1640 qc->prev->next = qc->next;
1642 tavl_delete( &qc->qbase->scopes[qc->scope], qc, pcache_query_cmp );
1643 qc->qbase->queries--;
1644 if ( qc->qbase->queries == 0 ) {
1645 avl_delete( &template->qbase, qc->qbase, pcache_dn_cmp );
1646 ch_free( qc->qbase );
1650 template->no_of_queries--;
1653 /* remove bottom query of LRU list from the query cache */
1655 * NOTE: slight change in functionality.
1657 * - if result->bv_val is NULL, the query at the bottom of the LRU
1659 * - otherwise, the query whose UUID is *result is removed
1660 * - if not found, result->bv_val is zeroed
1663 cache_replacement(query_manager* qm, struct berval *result)
1665 CachedQuery* bottom;
1666 QueryTemplate *temp;
1668 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1669 if ( BER_BVISNULL( result ) ) {
1670 bottom = qm->lru_bottom;
1673 Debug ( pcache_debug,
1674 "Cache replacement invoked without "
1675 "any query in LRU list\n", 0, 0, 0 );
1676 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1681 for ( bottom = qm->lru_bottom;
1683 bottom = bottom->lru_up )
1685 if ( bvmatch( result, &bottom->q_uuid ) ) {
1691 Debug ( pcache_debug,
1692 "Could not find query with uuid=\"%s\""
1693 "in LRU list\n", result->bv_val, 0, 0 );
1694 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1695 BER_BVZERO( result );
1700 temp = bottom->qtemp;
1701 remove_query(qm, bottom);
1702 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1704 *result = bottom->q_uuid;
1705 BER_BVZERO( &bottom->q_uuid );
1707 Debug( pcache_debug, "Lock CR index = %p\n", (void *) temp, 0, 0 );
1708 ldap_pvt_thread_rdwr_wlock(&temp->t_rwlock);
1709 remove_from_template(bottom, temp);
1710 Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
1711 (void *) temp, temp->no_of_queries, 0 );
1712 Debug( pcache_debug, "Unlock CR index = %p\n", (void *) temp, 0, 0 );
1713 ldap_pvt_thread_rdwr_wunlock(&temp->t_rwlock);
1718 struct query_info *next;
1730 struct query_info *qi;
1733 if ( rs->sr_type != REP_SEARCH ) return 0;
1735 attr = attr_find( rs->sr_entry->e_attrs, ad_queryId );
1736 if ( attr == NULL ) return 0;
1738 count = attr->a_numvals;
1739 assert( count > 0 );
1740 qi = op->o_tmpalloc( sizeof( struct query_info ), op->o_tmpmemctx );
1741 qi->next = op->o_callback->sc_private;
1742 op->o_callback->sc_private = qi;
1743 ber_dupbv_x( &qi->xdn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1744 qi->del = ( count == 1 );
1752 struct berval *query_uuid )
1754 struct query_info *qi, *qnext;
1755 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(pcacheQueryID=)" ) ];
1756 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
1757 Filter filter = {LDAP_FILTER_EQUALITY};
1758 SlapReply sreply = {REP_RESULT};
1759 slap_callback cb = { NULL, remove_func, NULL, NULL };
1762 op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
1763 "(%s=%s)", ad_queryId->ad_cname.bv_val, query_uuid->bv_val);
1764 filter.f_ava = &ava;
1765 filter.f_av_desc = ad_queryId;
1766 filter.f_av_value = *query_uuid;
1768 op->o_tag = LDAP_REQ_SEARCH;
1769 op->o_protocol = LDAP_VERSION3;
1770 op->o_callback = &cb;
1771 op->o_time = slap_get_time();
1772 op->o_do_not_cache = 1;
1774 op->o_req_dn = op->o_bd->be_suffix[0];
1775 op->o_req_ndn = op->o_bd->be_nsuffix[0];
1776 op->ors_scope = LDAP_SCOPE_SUBTREE;
1777 op->ors_deref = LDAP_DEREF_NEVER;
1778 op->ors_slimit = SLAP_NO_LIMIT;
1779 op->ors_tlimit = SLAP_NO_LIMIT;
1780 op->ors_limit = NULL;
1781 op->ors_filter = &filter;
1782 op->ors_filterstr.bv_val = filter_str;
1783 op->ors_filterstr.bv_len = strlen(filter_str);
1784 op->ors_attrs = NULL;
1785 op->ors_attrsonly = 0;
1787 op->o_bd->be_search( op, &sreply );
1789 for ( qi=cb.sc_private; qi; qi=qnext ) {
1792 op->o_req_dn = qi->xdn;
1793 op->o_req_ndn = qi->xdn;
1794 rs_reinit( &sreply, REP_RESULT );
1797 Debug( pcache_debug, "DELETING ENTRY TEMPLATE=%s\n",
1798 query_uuid->bv_val, 0, 0 );
1800 op->o_tag = LDAP_REQ_DELETE;
1802 if (op->o_bd->be_delete(op, &sreply) == LDAP_SUCCESS) {
1808 struct berval vals[2];
1810 vals[0] = *query_uuid;
1811 vals[1].bv_val = NULL;
1813 mod.sml_op = LDAP_MOD_DELETE;
1815 mod.sml_desc = ad_queryId;
1816 mod.sml_type = ad_queryId->ad_cname;
1817 mod.sml_values = vals;
1818 mod.sml_nvalues = NULL;
1819 mod.sml_numvals = 1;
1820 mod.sml_next = NULL;
1821 Debug( pcache_debug,
1822 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
1823 query_uuid->bv_val, 0, 0 );
1825 op->orm_modlist = &mod;
1827 op->o_bd->be_modify( op, &sreply );
1829 op->o_tmpfree( qi->xdn.bv_val, op->o_tmpmemctx );
1830 op->o_tmpfree( qi, op->o_tmpmemctx );
1837 AttributeName* attrs,
1846 struct berval *fstr )
1848 AttributeDescription *ad;
1851 switch ( f->f_choice ) {
1852 case LDAP_FILTER_EQUALITY:
1854 len = STRLENOF( "(=)" ) + ad->ad_cname.bv_len;
1855 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=)", ad->ad_cname.bv_val );
1856 assert( ret == len );
1857 fstr->bv_len += len;
1860 case LDAP_FILTER_GE:
1862 len = STRLENOF( "(>=)" ) + ad->ad_cname.bv_len;
1863 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s>=)", ad->ad_cname.bv_val);
1864 assert( ret == len );
1865 fstr->bv_len += len;
1868 case LDAP_FILTER_LE:
1870 len = STRLENOF( "(<=)" ) + ad->ad_cname.bv_len;
1871 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s<=)", ad->ad_cname.bv_val);
1872 assert( ret == len );
1873 fstr->bv_len += len;
1876 case LDAP_FILTER_APPROX:
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_SUBSTRINGS:
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_PRESENT:
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_AND:
1901 case LDAP_FILTER_OR:
1902 case LDAP_FILTER_NOT: {
1904 fstr->bv_val[fstr->bv_len++] = '(';
1905 switch ( f->f_choice ) {
1906 case LDAP_FILTER_AND:
1907 fstr->bv_val[fstr->bv_len] = '&';
1909 case LDAP_FILTER_OR:
1910 fstr->bv_val[fstr->bv_len] = '|';
1912 case LDAP_FILTER_NOT:
1913 fstr->bv_val[fstr->bv_len] = '!';
1918 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1919 rc = filter2template( op, f, fstr );
1922 fstr->bv_val[fstr->bv_len++] = ')';
1923 fstr->bv_val[fstr->bv_len] = '\0';
1929 /* a filter should at least have room for "()",
1930 * an "=" and for a 1-char attr */
1931 strcpy( fstr->bv_val, "(?=)" );
1932 fstr->bv_len += STRLENOF("(?=)");
1939 #define BI_HASHED 0x01
1940 #define BI_DIDCB 0x02
1941 #define BI_LOOKUP 0x04
1945 typedef struct bindinfo {
1946 cache_manager *bi_cm;
1948 QueryTemplate *bi_templ;
1949 struct search_info *bi_si;
1951 slap_callback bi_cb;
1954 struct search_info {
1957 QueryTemplate *qtemp;
1958 AttributeName* save_attrs; /* original attributes, saved for response */
1959 int swap_saved_attrs;
1964 int slimit_exceeded;
1965 pc_caching_reason_t caching_reason;
1971 remove_query_and_data(
1974 struct berval *uuid )
1976 query_manager* qm = cm->qm;
1978 qm->crfunc( qm, uuid );
1979 if ( !BER_BVISNULL( uuid ) ) {
1982 Debug( pcache_debug,
1983 "Removing query UUID %s\n",
1984 uuid->bv_val, 0, 0 );
1985 return_val = remove_query_data( op, uuid );
1986 Debug( pcache_debug,
1987 "QUERY REMOVED, SIZE=%d\n",
1989 ldap_pvt_thread_mutex_lock( &cm->cache_mutex );
1990 cm->cur_entries -= return_val;
1991 cm->num_cached_queries--;
1992 Debug( pcache_debug,
1993 "STORED QUERIES = %lu\n",
1994 cm->num_cached_queries, 0, 0 );
1995 ldap_pvt_thread_mutex_unlock( &cm->cache_mutex );
1996 Debug( pcache_debug,
1997 "QUERY REMOVED, CACHE ="
1999 cm->cur_entries, 0, 0 );
2004 * Callback used to fetch queryId values based on entryUUID;
2005 * used by pcache_remove_entries_from_cache()
2008 fetch_queryId_cb( Operation *op, SlapReply *rs )
2012 /* only care about searchEntry responses */
2013 if ( rs->sr_type != REP_SEARCH ) {
2017 /* allow only one response per entryUUID */
2018 if ( op->o_callback->sc_private != NULL ) {
2024 /* copy all queryId values into callback's private data */
2025 a = attr_find( rs->sr_entry->e_attrs, ad_queryId );
2027 BerVarray vals = NULL;
2029 ber_bvarray_dup_x( &vals, a->a_nvals, op->o_tmpmemctx );
2030 op->o_callback->sc_private = (void *)vals;
2034 /* clear entry if required */
2035 rs_flush_entry( op, rs, (slap_overinst *) op->o_bd->bd_info );
2041 * Call that allows to remove a set of entries from the cache,
2042 * by forcing the removal of all the related queries.
2045 pcache_remove_entries_from_cache(
2048 BerVarray entryUUIDs )
2050 Connection conn = { 0 };
2051 OperationBuffer opbuf;
2053 slap_callback sc = { 0 };
2055 char filtbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(entryUUID=)" ) ];
2056 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2057 AttributeName attrs[ 2 ] = {{{ 0 }}};
2061 void *thrctx = ldap_pvt_thread_pool_context();
2063 connection_fake_init( &conn, &opbuf, thrctx );
2071 memset( &op->oq_search, 0, sizeof( op->oq_search ) );
2072 op->ors_scope = LDAP_SCOPE_SUBTREE;
2073 op->ors_deref = LDAP_DEREF_NEVER;
2074 f.f_choice = LDAP_FILTER_EQUALITY;
2076 ava.aa_desc = slap_schema.si_ad_entryUUID;
2077 op->ors_filter = &f;
2079 op->ors_tlimit = SLAP_NO_LIMIT;
2080 op->ors_limit = NULL;
2081 attrs[ 0 ].an_desc = ad_queryId;
2082 attrs[ 0 ].an_name = ad_queryId->ad_cname;
2083 op->ors_attrs = attrs;
2084 op->ors_attrsonly = 0;
2086 op->o_req_dn = cm->db.be_suffix[ 0 ];
2087 op->o_req_ndn = cm->db.be_nsuffix[ 0 ];
2089 op->o_tag = LDAP_REQ_SEARCH;
2090 op->o_protocol = LDAP_VERSION3;
2091 op->o_managedsait = SLAP_CONTROL_CRITICAL;
2093 op->o_dn = op->o_bd->be_rootdn;
2094 op->o_ndn = op->o_bd->be_rootndn;
2095 sc.sc_response = fetch_queryId_cb;
2096 op->o_callback = ≻
2098 for ( s = 0; !BER_BVISNULL( &entryUUIDs[ s ] ); s++ ) {
2099 BerVarray vals = NULL;
2100 SlapReply rs = { REP_RESULT };
2102 op->ors_filterstr.bv_len = snprintf( filtbuf, sizeof( filtbuf ),
2103 "(entryUUID=%s)", entryUUIDs[ s ].bv_val );
2104 op->ors_filterstr.bv_val = filtbuf;
2105 ava.aa_value = entryUUIDs[ s ];
2107 rc = op->o_bd->be_search( op, &rs );
2108 if ( rc != LDAP_SUCCESS ) {
2112 vals = (BerVarray)op->o_callback->sc_private;
2113 if ( vals != NULL ) {
2116 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
2117 struct berval val = vals[ i ];
2119 remove_query_and_data( op, cm, &val );
2121 if ( !BER_BVISNULL( &val ) && val.bv_val != vals[ i ].bv_val ) {
2122 ch_free( val.bv_val );
2126 ber_bvarray_free_x( vals, op->o_tmpmemctx );
2127 op->o_callback->sc_private = NULL;
2135 * Call that allows to remove a query from the cache.
2138 pcache_remove_query_from_cache(
2141 struct berval *queryid )
2143 Operation op2 = *op;
2147 /* remove the selected query */
2148 remove_query_and_data( &op2, cm, queryid );
2150 return LDAP_SUCCESS;
2154 * Call that allows to remove a set of queries related to an entry
2155 * from the cache; if queryid is not null, the entry must belong to
2156 * the query indicated by queryid.
2159 pcache_remove_entry_queries_from_cache(
2163 struct berval *queryid )
2165 Connection conn = { 0 };
2166 OperationBuffer opbuf;
2168 slap_callback sc = { 0 };
2169 SlapReply rs = { REP_RESULT };
2171 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(pcacheQueryID=)" ) ];
2172 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2173 AttributeName attrs[ 2 ] = {{{ 0 }}};
2176 BerVarray vals = NULL;
2179 void *thrctx = ldap_pvt_thread_pool_context();
2181 connection_fake_init( &conn, &opbuf, thrctx );
2189 memset( &op->oq_search, 0, sizeof( op->oq_search ) );
2190 op->ors_scope = LDAP_SCOPE_BASE;
2191 op->ors_deref = LDAP_DEREF_NEVER;
2192 if ( queryid == NULL || BER_BVISNULL( queryid ) ) {
2193 BER_BVSTR( &op->ors_filterstr, "(objectClass=*)" );
2194 f.f_choice = LDAP_FILTER_PRESENT;
2195 f.f_desc = slap_schema.si_ad_objectClass;
2198 op->ors_filterstr.bv_len = snprintf( filter_str,
2199 sizeof( filter_str ), "(%s=%s)",
2200 ad_queryId->ad_cname.bv_val, queryid->bv_val );
2201 f.f_choice = LDAP_FILTER_EQUALITY;
2203 f.f_av_desc = ad_queryId;
2204 f.f_av_value = *queryid;
2206 op->ors_filter = &f;
2208 op->ors_tlimit = SLAP_NO_LIMIT;
2209 op->ors_limit = NULL;
2210 attrs[ 0 ].an_desc = ad_queryId;
2211 attrs[ 0 ].an_name = ad_queryId->ad_cname;
2212 op->ors_attrs = attrs;
2213 op->ors_attrsonly = 0;
2215 op->o_req_dn = *ndn;
2216 op->o_req_ndn = *ndn;
2218 op->o_tag = LDAP_REQ_SEARCH;
2219 op->o_protocol = LDAP_VERSION3;
2220 op->o_managedsait = SLAP_CONTROL_CRITICAL;
2222 op->o_dn = op->o_bd->be_rootdn;
2223 op->o_ndn = op->o_bd->be_rootndn;
2224 sc.sc_response = fetch_queryId_cb;
2225 op->o_callback = ≻
2227 rc = op->o_bd->be_search( op, &rs );
2228 if ( rc != LDAP_SUCCESS ) {
2232 vals = (BerVarray)op->o_callback->sc_private;
2233 if ( vals != NULL ) {
2236 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
2237 struct berval val = vals[ i ];
2239 remove_query_and_data( op, cm, &val );
2241 if ( !BER_BVISNULL( &val ) && val.bv_val != vals[ i ].bv_val ) {
2242 ch_free( val.bv_val );
2246 ber_bvarray_free_x( vals, op->o_tmpmemctx );
2249 return LDAP_SUCCESS;
2255 struct berval *query_uuid )
2257 struct search_info *si = op->o_callback->sc_private;
2258 slap_overinst *on = si->on;
2259 cache_manager *cm = on->on_bi.bi_private;
2262 struct berval crp_uuid;
2263 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
2265 Connection conn = {0};
2266 OperationBuffer opbuf;
2267 void *thrctx = ldap_pvt_thread_pool_context();
2269 query_uuid->bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf));
2270 ber_str2bv(uuidbuf, query_uuid->bv_len, 1, query_uuid);
2272 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
2273 op_tmp = &opbuf.ob_op;
2274 op_tmp->o_bd = &cm->db;
2275 op_tmp->o_dn = cm->db.be_rootdn;
2276 op_tmp->o_ndn = cm->db.be_rootndn;
2278 Debug( pcache_debug, "UUID for query being added = %s\n",
2281 for ( e=si->head; e; e=si->head ) {
2282 si->head = e->e_private;
2283 e->e_private = NULL;
2284 while ( cm->cur_entries > (cm->max_entries) ) {
2285 BER_BVZERO( &crp_uuid );
2286 remove_query_and_data( op_tmp, cm, &crp_uuid );
2289 return_val = merge_entry(op_tmp, e, 0, query_uuid);
2290 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2291 cm->cur_entries += return_val;
2292 Debug( pcache_debug,
2293 "ENTRY ADDED/MERGED, CACHED ENTRIES=%d\n",
2294 cm->cur_entries, 0, 0 );
2296 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2303 pcache_op_cleanup( Operation *op, SlapReply *rs ) {
2304 slap_callback *cb = op->o_callback;
2305 struct search_info *si = cb->sc_private;
2306 slap_overinst *on = si->on;
2307 cache_manager *cm = on->on_bi.bi_private;
2308 query_manager* qm = cm->qm;
2310 if ( rs->sr_type == REP_SEARCH ) {
2313 /* don't return more entries than requested by the client */
2314 if ( si->slimit > 0 && rs->sr_nentries >= si->slimit ) {
2315 si->slimit_exceeded = 1;
2318 /* If we haven't exceeded the limit for this query,
2319 * build a chain of answers to store. If we hit the
2320 * limit, empty the chain and ignore the rest.
2323 /* check if the entry contains undefined
2324 * attributes/objectClasses (ITS#5680) */
2325 if ( cm->check_cacheability && test_filter( op, rs->sr_entry, si->query.filter ) != LDAP_COMPARE_TRUE ) {
2326 Debug( pcache_debug, "%s: query not cacheable because of schema issues in DN \"%s\"\n",
2327 op->o_log_prefix, rs->sr_entry->e_name.bv_val, 0 );
2331 /* check for malformed entries: attrs with no values */
2333 Attribute *a = rs->sr_entry->e_attrs;
2334 for (; a; a=a->a_next) {
2335 if ( !a->a_numvals ) {
2336 Debug( pcache_debug, "%s: query not cacheable because of attrs without values in DN \"%s\" (%s)\n",
2337 op->o_log_prefix, rs->sr_entry->e_name.bv_val,
2338 a->a_desc->ad_cname.bv_val );
2344 if ( si->count < si->max ) {
2346 e = entry_dup( rs->sr_entry );
2347 if ( !si->head ) si->head = e;
2348 if ( si->tail ) si->tail->e_private = e;
2355 for (;si->head; si->head=e) {
2356 e = si->head->e_private;
2357 si->head->e_private = NULL;
2358 entry_free(si->head);
2366 if ( rs->sr_type == REP_RESULT ||
2367 op->o_abandon || rs->sr_err == SLAPD_ABANDON )
2369 if ( si->swap_saved_attrs ) {
2370 rs->sr_attrs = si->save_attrs;
2371 op->ors_attrs = si->save_attrs;
2373 if ( (op->o_abandon || rs->sr_err == SLAPD_ABANDON) &&
2374 si->caching_reason == PC_IGNORE )
2376 filter_free( si->query.filter );
2378 /* duplicate query, free it */
2380 for (;si->head; si->head=e) {
2381 e = si->head->e_private;
2382 si->head->e_private = NULL;
2383 entry_free(si->head);
2387 } else if ( si->caching_reason != PC_IGNORE ) {
2388 CachedQuery *qc = qm->addfunc(op, qm, &si->query,
2389 si->qtemp, si->caching_reason, 1 );
2392 switch ( si->caching_reason ) {
2394 cache_entries( op, &qc->q_uuid );
2396 si->pbi->bi_cq = qc;
2400 qc->q_sizelimit = rs->sr_nentries;
2410 ldap_pvt_thread_rdwr_wunlock(&qc->rwlock);
2411 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2412 cm->num_cached_queries++;
2413 Debug( pcache_debug, "STORED QUERIES = %lu\n",
2414 cm->num_cached_queries, 0, 0 );
2415 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2417 /* If the consistency checker suspended itself,
2420 if ( cm->cc_paused == PCACHE_CC_PAUSED ) {
2421 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2422 if ( cm->cc_paused == PCACHE_CC_PAUSED ) {
2424 ldap_pvt_runqueue_resched( &slapd_rq, cm->cc_arg, 0 );
2426 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2429 } else if ( si->count ) {
2430 /* duplicate query, free it */
2432 for (;si->head; si->head=e) {
2433 e = si->head->e_private;
2434 si->head->e_private = NULL;
2435 entry_free(si->head);
2440 filter_free( si->query.filter );
2443 op->o_callback = op->o_callback->sc_next;
2444 op->o_tmpfree( cb, op->o_tmpmemctx );
2447 return SLAP_CB_CONTINUE;
2455 struct search_info *si = op->o_callback->sc_private;
2457 if ( si->swap_saved_attrs ) {
2458 rs->sr_attrs = si->save_attrs;
2459 op->ors_attrs = si->save_attrs;
2462 if ( rs->sr_type == REP_SEARCH ) {
2463 /* don't return more entries than requested by the client */
2464 if ( si->slimit_exceeded ) {
2468 } else if ( rs->sr_type == REP_RESULT ) {
2471 if ( rs->sr_err == LDAP_SUCCESS ) {
2472 si->caching_reason = PC_POSITIVE;
2474 } else if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED
2475 && si->qtemp->limitttl )
2479 si->caching_reason = PC_SIZELIMIT;
2480 for (;si->head; si->head=e) {
2481 e = si->head->e_private;
2482 si->head->e_private = NULL;
2483 entry_free(si->head);
2487 } else if ( si->qtemp->negttl && !si->count && !si->over &&
2488 rs->sr_err == LDAP_SUCCESS )
2490 si->caching_reason = PC_NEGATIVE;
2494 if ( si->slimit_exceeded ) {
2495 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
2499 return SLAP_CB_CONTINUE;
2502 /* NOTE: this is a quick workaround to let pcache minimally interact
2503 * with pagedResults. A more articulated solutions would be to
2504 * perform the remote query without control and cache all results,
2505 * performing the pagedResults search only within the client
2506 * and the proxy. This requires pcache to understand pagedResults. */
2508 pcache_chk_controls(
2512 const char *non = "";
2513 const char *stripped = "";
2515 switch( op->o_pagedresults ) {
2516 case SLAP_CONTROL_NONCRITICAL:
2518 stripped = "; stripped";
2521 case SLAP_CONTROL_CRITICAL:
2522 Debug( pcache_debug, "%s: "
2523 "%scritical pagedResults control "
2524 "disabled with proxy cache%s.\n",
2525 op->o_log_prefix, non, stripped );
2527 slap_remove_control( op, rs, slap_cids.sc_pagedResults, NULL );
2531 rs->sr_err = SLAP_CB_CONTINUE;
2539 pc_setpw( Operation *op, struct berval *pwd, cache_manager *cm )
2541 struct berval vals[2];
2544 const char *text = NULL;
2545 BER_BVZERO( &vals[0] );
2546 slap_passwd_hash( pwd, &vals[0], &text );
2547 if ( BER_BVISEMPTY( &vals[0] )) {
2548 Debug( pcache_debug, "pc_setpw: hash failed %s\n",
2554 BER_BVZERO( &vals[1] );
2558 SlapReply sr = { REP_RESULT };
2559 slap_callback cb = { 0, slap_null_cb, 0, 0 };
2562 mod.sml_op = LDAP_MOD_REPLACE;
2564 mod.sml_desc = slap_schema.si_ad_userPassword;
2565 mod.sml_type = mod.sml_desc->ad_cname;
2566 mod.sml_values = vals;
2567 mod.sml_nvalues = NULL;
2568 mod.sml_numvals = 1;
2569 mod.sml_next = NULL;
2571 op->o_tag = LDAP_REQ_MODIFY;
2572 op->orm_modlist = &mod;
2574 op->o_dn = op->o_bd->be_rootdn;
2575 op->o_ndn = op->o_bd->be_rootndn;
2576 op->o_callback = &cb;
2577 Debug( pcache_debug, "pc_setpw: CACHING BIND for %s\n",
2578 op->o_req_dn.bv_val, 0, 0 );
2579 rc = op->o_bd->be_modify( op, &sr );
2580 ch_free( vals[0].bv_val );
2585 typedef struct bindcacheinfo {
2591 pc_bind_save( Operation *op, SlapReply *rs )
2593 if ( rs->sr_err == LDAP_SUCCESS ) {
2594 bindcacheinfo *bci = op->o_callback->sc_private;
2595 slap_overinst *on = bci->on;
2596 cache_manager *cm = on->on_bi.bi_private;
2598 Operation op2 = *op;
2599 if ( pc_setpw( &op2, &op->orb_cred, cm ) == LDAP_SUCCESS )
2600 bci->qc->bindref_time = op->o_time + bci->qc->qtemp->bindttr;
2602 return SLAP_CB_CONTINUE;
2606 pc_bind_attrs( Operation *op, Entry *e, QueryTemplate *temp,
2607 struct berval *fbv )
2610 struct berval *vals, pres = BER_BVC("*");
2614 vals = op->o_tmpalloc( temp->bindnattrs * sizeof( struct berval ),
2617 for ( i=0; i<temp->bindnattrs; i++ ) {
2618 a = attr_find( e->e_attrs, temp->bindfattrs[i] );
2619 if ( a && a->a_vals ) {
2620 vals[i] = a->a_vals[0];
2621 len += a->a_vals[0].bv_len;
2626 fbv->bv_len = len + temp->bindftemp.bv_len;
2627 fbv->bv_val = op->o_tmpalloc( fbv->bv_len + 1, op->o_tmpmemctx );
2629 p1 = temp->bindftemp.bv_val;
2634 if ( p1[0] == '=' && p1[1] == ')' ) {
2635 AC_MEMCPY( p2, vals[i].bv_val, vals[i].bv_len );
2636 p2 += vals[i].bv_len;
2642 op->o_tmpfree( vals, op->o_tmpmemctx );
2644 /* FIXME: are we sure str2filter_x can't fail?
2645 * caller needs to check */
2647 Filter *f = str2filter_x( op, fbv->bv_val );
2648 assert( f != NULL );
2653 /* Check if the requested entry is from the cache and has a valid
2654 * ttr and password hash
2657 pc_bind_search( Operation *op, SlapReply *rs )
2659 if ( rs->sr_type == REP_SEARCH ) {
2660 bindinfo *pbi = op->o_callback->sc_private;
2662 /* We only care if this is an already cached result and we're
2663 * below the refresh time, or we're offline.
2666 if (( pbi->bi_cm->cc_paused & PCACHE_CC_OFFLINE ) ||
2667 op->o_time < pbi->bi_cq->bindref_time ) {
2670 /* See if a recognized password is hashed here */
2671 a = attr_find( rs->sr_entry->e_attrs,
2672 slap_schema.si_ad_userPassword );
2673 if ( a && a->a_vals[0].bv_val[0] == '{' &&
2674 lutil_passwd_scheme( a->a_vals[0].bv_val ))
2675 pbi->bi_flags |= BI_HASHED;
2677 Debug( pcache_debug, "pc_bind_search: cache is stale, "
2678 "reftime: %ld, current time: %ld\n",
2679 pbi->bi_cq->bindref_time, op->o_time, 0 );
2681 } else if ( pbi->bi_si ) {
2682 /* This search result is going into the cache */
2686 filter_free( pbi->bi_si->query.filter );
2687 f = pc_bind_attrs( op, rs->sr_entry, pbi->bi_templ, &fbv );
2688 op->o_tmpfree( fbv.bv_val, op->o_tmpmemctx );
2689 pbi->bi_si->query.filter = filter_dup( f, NULL );
2690 filter_free_x( op, f, 1 );
2696 /* We always want pc_bind_search to run after the search handlers */
2698 pc_bind_resp( Operation *op, SlapReply *rs )
2700 bindinfo *pbi = op->o_callback->sc_private;
2701 if ( !( pbi->bi_flags & BI_DIDCB )) {
2702 slap_callback *sc = op->o_callback;
2703 while ( sc && sc->sc_response != pcache_response )
2706 sc = op->o_callback;
2707 pbi->bi_cb.sc_next = sc->sc_next;
2708 sc->sc_next = &pbi->bi_cb;
2709 pbi->bi_flags |= BI_DIDCB;
2711 return SLAP_CB_CONTINUE;
2714 #ifdef PCACHE_CONTROL_PRIVDB
2720 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2721 cache_manager *cm = on->on_bi.bi_private;
2722 slap_callback *save_cb;
2725 /* skip if control is unset */
2726 if ( op->o_ctrlflag[ privDB_cid ] != SLAP_CONTROL_CRITICAL ) {
2727 return SLAP_CB_CONTINUE;
2730 /* The cache DB isn't open yet */
2731 if ( cm->defer_db_open ) {
2732 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
2733 "pcachePrivDB: cacheDB not available" );
2737 /* FIXME: might be a little bit exaggerated... */
2738 if ( !be_isroot( op ) ) {
2739 save_cb = op->o_callback;
2740 op->o_callback = NULL;
2741 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
2742 "pcachePrivDB: operation not allowed" );
2743 op->o_callback = save_cb;
2748 /* map tag to operation */
2749 type = slap_req2op( op->o_tag );
2750 if ( type != SLAP_OP_LAST ) {
2754 /* execute, if possible */
2755 func = &cm->db.be_bind;
2756 if ( func[ type ] != NULL ) {
2757 Operation op2 = *op;
2761 rc = func[ type ]( &op2, rs );
2762 if ( type == SLAP_OP_BIND && rc == LDAP_SUCCESS ) {
2763 op->o_conn->c_authz_cookie = cm->db.be_private;
2770 /* otherwise fall back to error */
2771 save_cb = op->o_callback;
2772 op->o_callback = NULL;
2773 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
2774 "operation not supported with pcachePrivDB control" );
2775 op->o_callback = save_cb;
2779 #endif /* PCACHE_CONTROL_PRIVDB */
2786 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2787 cache_manager *cm = on->on_bi.bi_private;
2788 QueryTemplate *temp;
2790 slap_callback cb = { 0 }, *sc;
2796 #ifdef PCACHE_CONTROL_PRIVDB
2797 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL )
2798 return pcache_op_privdb( op, rs );
2799 #endif /* PCACHE_CONTROL_PRIVDB */
2801 /* Skip if we're not configured for Binds, or cache DB isn't open yet */
2802 if ( !cm->cache_binds || cm->defer_db_open )
2803 return SLAP_CB_CONTINUE;
2805 /* First find a matching template with Bind info */
2806 for ( temp=cm->qm->templates; temp; temp=temp->qmnext ) {
2807 if ( temp->bindttr && dnIsSuffix( &op->o_req_ndn, &temp->bindbase ))
2810 /* Didn't find a suitable template, just passthru */
2812 return SLAP_CB_CONTINUE;
2814 /* See if the entry is already locally cached. If so, we can
2815 * populate the query filter to retrieve the cached query. We
2816 * need to check the bindrefresh time in the query.
2819 op2.o_dn = op->o_bd->be_rootdn;
2820 op2.o_ndn = op->o_bd->be_rootndn;
2825 rc = be_entry_get_rw( &op2, &op->o_req_ndn, NULL, NULL, 0, &e );
2826 if ( rc == LDAP_SUCCESS && e ) {
2827 bi.bi_flags |= BI_LOOKUP;
2828 op2.ors_filter = pc_bind_attrs( op, e, temp, &op2.ors_filterstr );
2829 be_entry_release_r( &op2, e );
2831 op2.ors_filter = temp->bindfilter;
2832 op2.ors_filterstr = temp->bindfilterstr;
2835 op2.o_bd = op->o_bd;
2836 op2.o_tag = LDAP_REQ_SEARCH;
2837 op2.ors_scope = LDAP_SCOPE_BASE;
2838 op2.ors_deref = LDAP_DEREF_NEVER;
2840 op2.ors_tlimit = SLAP_NO_LIMIT;
2841 op2.ors_limit = NULL;
2842 op2.ors_attrs = cm->qm->attr_sets[temp->attr_set_index].attrs;
2843 op2.ors_attrsonly = 0;
2845 /* We want to invoke search at the same level of the stack
2846 * as we're already at...
2853 bi.bi_cb.sc_response = pc_bind_search;
2854 bi.bi_cb.sc_cleanup = NULL;
2855 bi.bi_cb.sc_private = &bi;
2856 cb.sc_private = &bi;
2857 cb.sc_response = pc_bind_resp;
2858 op2.o_callback = &cb;
2859 overlay_op_walk( &op2, rs, op_search, on->on_info, on );
2861 /* OK, just bind locally */
2862 if ( bi.bi_flags & BI_HASHED ) {
2863 BackendDB *be = op->o_bd;
2866 Debug( pcache_debug, "pcache_op_bind: CACHED BIND for %s\n",
2867 op->o_req_dn.bv_val, 0, 0 );
2869 if ( op->o_bd->be_bind( op, rs ) == LDAP_SUCCESS ) {
2870 op->o_conn->c_authz_cookie = cm->db.be_private;
2876 /* We have a cached query to work with */
2878 sc = op->o_tmpalloc( sizeof(slap_callback) + sizeof(bindcacheinfo),
2880 sc->sc_response = pc_bind_save;
2881 sc->sc_cleanup = NULL;
2882 sc->sc_private = sc+1;
2883 bci = sc->sc_private;
2884 sc->sc_next = op->o_callback;
2885 op->o_callback = sc;
2889 return SLAP_CB_CONTINUE;
2897 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2898 cache_manager *cm = on->on_bi.bi_private;
2899 query_manager* qm = cm->qm;
2904 QueryTemplate *qtemp = NULL;
2905 bindinfo *pbi = NULL;
2908 CachedQuery *answerable = NULL;
2911 struct berval tempstr;
2913 #ifdef PCACHE_CONTROL_PRIVDB
2914 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL ) {
2915 return pcache_op_privdb( op, rs );
2917 #endif /* PCACHE_CONTROL_PRIVDB */
2919 /* The cache DB isn't open yet */
2920 if ( cm->defer_db_open ) {
2921 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
2922 "pcachePrivDB: cacheDB not available" );
2926 /* pickup runtime ACL changes */
2927 cm->db.be_acl = op->o_bd->be_acl;
2930 /* See if we're processing a Bind request */
2931 slap_callback *cb = op->o_callback;
2933 for ( ; cb; cb=cb->sc_next ) {
2934 if ( cb->sc_response == pc_bind_resp ) {
2935 pbi = cb->sc_private;
2941 /* FIXME: cannot cache/answer requests with pagedResults control */
2943 query.filter = op->ors_filter;
2946 query.base = pbi->bi_templ->bindbase;
2947 query.scope = pbi->bi_templ->bindscope;
2948 attr_set = pbi->bi_templ->attr_set_index;
2950 qtemp = pbi->bi_templ;
2951 if ( pbi->bi_flags & BI_LOOKUP )
2952 answerable = qm->qcfunc(op, qm, &query, qtemp);
2955 tempstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len+1,
2958 if ( filter2template( op, op->ors_filter, &tempstr ))
2960 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
2961 return SLAP_CB_CONTINUE;
2964 Debug( pcache_debug, "query template of incoming query = %s\n",
2965 tempstr.bv_val, 0, 0 );
2968 attr_set = get_attr_set(op->ors_attrs, qm, cm->numattrsets);
2970 query.base = op->o_req_ndn;
2971 query.scope = op->ors_scope;
2973 /* check for query containment */
2974 if (attr_set > -1) {
2975 QueryTemplate *qt = qm->attr_sets[attr_set].templates;
2976 for (; qt; qt = qt->qtnext ) {
2977 /* find if template i can potentially answer tempstr */
2978 if ( ber_bvstrcasecmp( &qt->querystr, &tempstr ) != 0 )
2982 Debug( pcache_debug, "Entering QC, querystr = %s\n",
2983 op->ors_filterstr.bv_val, 0, 0 );
2984 answerable = qm->qcfunc(op, qm, &query, qt);
2986 /* if != NULL, rlocks qtemp->t_rwlock */
2991 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
2995 BackendDB *save_bd = op->o_bd;
2997 ldap_pvt_thread_mutex_lock( &answerable->answerable_cnt_mutex );
2998 answerable->answerable_cnt++;
2999 /* we only care about refcnts if we're refreshing */
3000 if ( answerable->refresh_time )
3001 answerable->refcnt++;
3002 Debug( pcache_debug, "QUERY ANSWERABLE (answered %lu times)\n",
3003 answerable->answerable_cnt, 0, 0 );
3004 ldap_pvt_thread_mutex_unlock( &answerable->answerable_cnt_mutex );
3006 ldap_pvt_thread_rdwr_rlock(&answerable->rwlock);
3007 if ( BER_BVISNULL( &answerable->q_uuid )) {
3008 /* No entries cached, just an empty result set */
3010 send_ldap_result( op, rs );
3012 /* Let Bind know we used a cached query */
3014 pbi->bi_cq = answerable;
3017 if ( cm->response_cb == PCACHE_RESPONSE_CB_TAIL ) {
3019 /* The cached entry was already processed by any
3020 * other overlays, so don't let it get processed again.
3022 * This loop removes over_back_response from the stack.
3024 if ( overlay_callback_after_backover( op, &cb, 0) == 0 ) {
3025 slap_callback **scp;
3026 for ( scp = &op->o_callback; *scp != NULL;
3027 scp = &(*scp)->sc_next ) {
3028 if ( (*scp)->sc_next == &cb ) {
3035 i = cm->db.bd_info->bi_op_search( op, rs );
3037 ldap_pvt_thread_rdwr_runlock(&answerable->rwlock);
3038 /* locked by qtemp->qcfunc (query_containment) */
3039 ldap_pvt_thread_rdwr_runlock(&qtemp->t_rwlock);
3044 Debug( pcache_debug, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
3046 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
3047 if (cm->num_cached_queries >= cm->max_queries) {
3050 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
3052 if (op->ors_attrsonly)
3057 struct search_info *si;
3059 Debug( pcache_debug, "QUERY CACHEABLE\n", 0, 0, 0 );
3060 query.filter = filter_dup(op->ors_filter, NULL);
3062 cb = op->o_tmpalloc( sizeof(*cb) + sizeof(*si), op->o_tmpmemctx );
3063 cb->sc_response = pcache_response;
3064 cb->sc_cleanup = pcache_op_cleanup;
3065 cb->sc_private = (cb+1);
3066 si = cb->sc_private;
3070 si->max = cm->num_entries_limit ;
3074 si->slimit_exceeded = 0;
3075 si->caching_reason = PC_IGNORE;
3076 if ( op->ors_slimit > 0 && op->ors_slimit < cm->num_entries_limit ) {
3077 si->slimit = op->ors_slimit;
3078 op->ors_slimit = cm->num_entries_limit;
3082 si->swap_saved_attrs = 1;
3083 si->save_attrs = op->ors_attrs;
3088 op->ors_attrs = qtemp->t_attrs.attrs;
3090 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
3091 cb->sc_next = op->o_callback;
3092 op->o_callback = cb;
3095 slap_callback **pcb;
3097 /* need to move the callback at the end, in case other
3098 * overlays are present, so that the final entry is
3099 * actually cached */
3101 for ( pcb = &op->o_callback; *pcb; pcb = &(*pcb)->sc_next );
3106 Debug( pcache_debug, "QUERY NOT CACHEABLE\n",
3110 return SLAP_CB_CONTINUE;
3115 AttributeName* attrs,
3123 for ( ; attrs[i].an_name.bv_val; i++ ) {
3124 /* only count valid attribute names
3125 * (searches ignore others, this overlay does the same) */
3126 if ( attrs[i].an_desc ) {
3132 /* recognize default or explicit single "*" */
3134 ( i == 1 && bvmatch( &attrs[0].an_name, slap_bv_all_user_attrs ) ) )
3137 attrs = slap_anlist_all_user_attributes;
3139 /* recognize implicit (no valid attributes) or explicit single "1.1" */
3140 } else if ( count == 0 ||
3141 ( i == 1 && bvmatch( &attrs[0].an_name, slap_bv_no_attrs ) ) )
3147 for ( i = 0; i < num; i++ ) {
3151 if ( count > qm->attr_sets[i].count ) {
3156 if ( !qm->attr_sets[i].count ) {
3162 for ( a2 = attrs; a2->an_name.bv_val; a2++ ) {
3163 if ( !a2->an_desc && !bvmatch( &a2->an_name, slap_bv_all_user_attrs ) ) continue;
3165 if ( !an_find( qm->attr_sets[i].attrs, &a2->an_name ) ) {
3183 /* Refresh a cached query:
3184 * 1: Replay the query on the remote DB and merge each entry into
3185 * the local DB. Remember the DNs of each remote entry.
3186 * 2: Search the local DB for all entries matching this queryID.
3187 * Delete any entry whose DN is not in the list from (1).
3189 typedef struct dnlist {
3190 struct dnlist *next;
3195 typedef struct refresh_info {
3203 static dnlist *dnl_alloc( Operation *op, struct berval *bvdn )
3205 dnlist *dn = op->o_tmpalloc( sizeof(dnlist) + bvdn->bv_len + 1,
3207 dn->dn.bv_len = bvdn->bv_len;
3208 dn->dn.bv_val = (char *)(dn+1);
3209 AC_MEMCPY( dn->dn.bv_val, bvdn->bv_val, dn->dn.bv_len );
3210 dn->dn.bv_val[dn->dn.bv_len] = '\0';
3215 refresh_merge( Operation *op, SlapReply *rs )
3217 if ( rs->sr_type == REP_SEARCH ) {
3218 refresh_info *ri = op->o_callback->sc_private;
3224 ocb = op->o_callback;
3225 /* Find local entry, merge */
3226 op->o_bd = ri->ri_be;
3227 rc = be_entry_get_rw( op, &rs->sr_entry->e_nname, NULL, NULL, 0, &e );
3228 if ( rc != LDAP_SUCCESS || e == NULL ) {
3229 /* No local entry, just add it. FIXME: we are not checking
3230 * the cache entry limit here
3232 merge_entry( op, rs->sr_entry, 1, &ri->ri_q->q_uuid );
3234 /* Entry exists, update it */
3237 Modifications *modlist, *mods = NULL;
3238 const char* text = NULL;
3239 char textbuf[SLAP_TEXT_BUFLEN];
3240 size_t textlen = sizeof(textbuf);
3241 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
3245 /* Get a copy of only the attrs we requested */
3246 for ( a=e->e_attrs; a; a=a->a_next ) {
3247 if ( ad_inlist( a->a_desc, rs->sr_attrs )) {
3248 *b = attr_alloc( a->a_desc );
3250 /* The actual values still belong to e */
3251 (*b)->a_flags |= SLAP_ATTR_DONT_FREE_VALS |
3252 SLAP_ATTR_DONT_FREE_DATA;
3253 b = &((*b)->a_next);
3257 slap_entry2mods( rs->sr_entry, &modlist, &text, textbuf, textlen );
3258 syncrepl_diff_entry( op, ne.e_attrs, rs->sr_entry->e_attrs,
3259 &mods, &modlist, 0 );
3260 be_entry_release_r( op, e );
3261 attrs_free( ne.e_attrs );
3262 slap_mods_free( modlist, 1 );
3263 /* mods is NULL if there are no changes */
3265 SlapReply rs2 = { REP_RESULT };
3266 struct berval dn = op->o_req_dn;
3267 struct berval ndn = op->o_req_ndn;
3268 op->o_tag = LDAP_REQ_MODIFY;
3269 op->orm_modlist = mods;
3270 op->o_req_dn = rs->sr_entry->e_name;
3271 op->o_req_ndn = rs->sr_entry->e_nname;
3272 op->o_callback = &cb;
3273 op->o_bd->be_modify( op, &rs2 );
3274 rs->sr_err = rs2.sr_err;
3275 rs_assert_done( &rs2 );
3276 slap_mods_free( mods, 1 );
3278 op->o_req_ndn = ndn;
3282 /* Add DN to list */
3283 dnl = dnl_alloc( op, &rs->sr_entry->e_nname );
3285 if ( ri->ri_tail ) {
3286 ri->ri_tail->next = dnl;
3291 op->o_callback = ocb;
3297 refresh_purge( Operation *op, SlapReply *rs )
3299 if ( rs->sr_type == REP_SEARCH ) {
3300 refresh_info *ri = op->o_callback->sc_private;
3304 /* Did the entry exist on the remote? */
3305 for ( dn=&ri->ri_dns; *dn; dn = &(*dn)->next ) {
3306 if ( dn_match( &(*dn)->dn, &rs->sr_entry->e_nname )) {
3307 dnlist *dnext = (*dn)->next;
3308 op->o_tmpfree( *dn, op->o_tmpmemctx );
3314 /* No, so put it on the list to delete */
3317 dnlist *dnl = dnl_alloc( op, &rs->sr_entry->e_nname );
3318 dnl->next = ri->ri_dels;
3320 a = attr_find( rs->sr_entry->e_attrs, ad_queryId );
3321 /* If ours is the only queryId, delete entry */
3322 dnl->delete = ( a->a_numvals == 1 );
3329 refresh_query( Operation *op, CachedQuery *query, slap_overinst *on )
3331 SlapReply rs = {REP_RESULT};
3332 slap_callback cb = { 0 };
3333 refresh_info ri = { 0 };
3334 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(pcacheQueryID=)" ) ];
3335 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
3336 Filter filter = {LDAP_FILTER_EQUALITY};
3337 AttributeName attrs[ 2 ] = {{{ 0 }}};
3341 ldap_pvt_thread_mutex_lock( &query->answerable_cnt_mutex );
3343 ldap_pvt_thread_mutex_unlock( &query->answerable_cnt_mutex );
3345 cb.sc_response = refresh_merge;
3346 cb.sc_private = &ri;
3349 ri.ri_be = op->o_bd;
3352 op->o_tag = LDAP_REQ_SEARCH;
3353 op->o_protocol = LDAP_VERSION3;
3354 op->o_callback = &cb;
3355 op->o_do_not_cache = 1;
3357 op->o_req_dn = query->qbase->base;
3358 op->o_req_ndn = query->qbase->base;
3359 op->ors_scope = query->scope;
3360 op->ors_slimit = SLAP_NO_LIMIT;
3361 op->ors_tlimit = SLAP_NO_LIMIT;
3362 op->ors_limit = NULL;
3363 op->ors_filter = query->filter;
3364 filter2bv_x( op, query->filter, &op->ors_filterstr );
3365 op->ors_attrs = query->qtemp->t_attrs.attrs;
3366 op->ors_attrsonly = 0;
3368 op->o_bd = on->on_info->oi_origdb;
3369 rc = op->o_bd->be_search( op, &rs );
3371 op->o_bd = ri.ri_be;
3375 /* Get the DNs of all entries matching this query */
3376 cb.sc_response = refresh_purge;
3378 op->o_bd = ri.ri_be;
3379 op->o_req_dn = op->o_bd->be_suffix[0];
3380 op->o_req_ndn = op->o_bd->be_nsuffix[0];
3381 op->ors_scope = LDAP_SCOPE_SUBTREE;
3382 op->ors_deref = LDAP_DEREF_NEVER;
3383 op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
3384 "(%s=%s)", ad_queryId->ad_cname.bv_val, query->q_uuid.bv_val);
3385 filter.f_ava = &ava;
3386 filter.f_av_desc = ad_queryId;
3387 filter.f_av_value = query->q_uuid;
3388 attrs[ 0 ].an_desc = ad_queryId;
3389 attrs[ 0 ].an_name = ad_queryId->ad_cname;
3390 op->ors_attrs = attrs;
3391 op->ors_attrsonly = 0;
3392 rs_reinit( &rs, REP_RESULT );
3393 rc = op->o_bd->be_search( op, &rs );
3394 if ( rc ) goto leave;
3396 while (( dn = ri.ri_dels )) {
3397 op->o_req_dn = dn->dn;
3398 op->o_req_ndn = dn->dn;
3399 rs_reinit( &rs, REP_RESULT );
3401 op->o_tag = LDAP_REQ_DELETE;
3402 op->o_bd->be_delete( op, &rs );
3405 struct berval vals[2];
3407 vals[0] = query->q_uuid;
3408 BER_BVZERO( &vals[1] );
3409 mod.sml_op = LDAP_MOD_DELETE;
3411 mod.sml_desc = ad_queryId;
3412 mod.sml_type = ad_queryId->ad_cname;
3413 mod.sml_values = vals;
3414 mod.sml_nvalues = NULL;
3415 mod.sml_numvals = 1;
3416 mod.sml_next = NULL;
3418 op->o_tag = LDAP_REQ_MODIFY;
3419 op->orm_modlist = &mod;
3420 op->o_bd->be_modify( op, &rs );
3422 ri.ri_dels = dn->next;
3423 op->o_tmpfree( dn, op->o_tmpmemctx );
3427 /* reset our local heap, we're done with it */
3428 slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, op->o_threadctx, 1 );
3437 struct re_s *rtask = arg;
3438 slap_overinst *on = rtask->arg;
3439 cache_manager *cm = on->on_bi.bi_private;
3440 query_manager *qm = cm->qm;
3441 Connection conn = {0};
3442 OperationBuffer opbuf;
3445 CachedQuery *query, *qprev;
3446 int return_val, pause = PCACHE_CC_PAUSED;
3447 QueryTemplate *templ;
3449 /* Don't expire anything when we're offline */
3450 if ( cm->cc_paused & PCACHE_CC_OFFLINE ) {
3451 pause = PCACHE_CC_OFFLINE;
3455 connection_fake_init( &conn, &opbuf, ctx );
3459 op->o_dn = cm->db.be_rootdn;
3460 op->o_ndn = cm->db.be_rootndn;
3464 for (templ = qm->templates; templ; templ=templ->qmnext) {
3466 if ( !templ->query_last ) continue;
3468 op->o_time = slap_get_time();
3469 if ( !templ->ttr ) {
3471 if ( templ->negttl && templ->negttl < ttl )
3472 ttl = templ->negttl;
3473 if ( templ->limitttl && templ->limitttl < ttl )
3474 ttl = templ->limitttl;
3475 /* The oldest timestamp that needs expiration checking */
3479 for ( query=templ->query_last; query; query=qprev ) {
3480 qprev = query->prev;
3481 if ( query->refresh_time && query->refresh_time < op->o_time ) {
3482 /* A refresh will extend the expiry if the query has been
3483 * referenced, but not if it's unreferenced. If the
3484 * expiration has been hit, then skip the refresh since
3485 * we're just going to discard the result anyway.
3487 if ( query->refcnt )
3488 query->expiry_time = op->o_time + templ->ttl;
3489 if ( query->expiry_time > op->o_time ) {
3490 refresh_query( op, query, on );
3495 if (query->expiry_time < op->o_time) {
3497 Debug( pcache_debug, "Lock CR index = %p\n",
3498 (void *) templ, 0, 0 );
3499 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
3500 if ( query == templ->query_last ) {
3502 remove_from_template(query, templ);
3503 Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
3504 (void *) templ, templ->no_of_queries, 0 );
3505 Debug( pcache_debug, "Unlock CR index = %p\n",
3506 (void *) templ, 0, 0 );
3508 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
3512 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
3513 remove_query(qm, query);
3514 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
3515 if ( BER_BVISNULL( &query->q_uuid ))
3518 return_val = remove_query_data(op, &query->q_uuid);
3519 Debug( pcache_debug, "STALE QUERY REMOVED, SIZE=%d\n",
3521 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
3522 cm->cur_entries -= return_val;
3523 cm->num_cached_queries--;
3524 Debug( pcache_debug, "STORED QUERIES = %lu\n",
3525 cm->num_cached_queries, 0, 0 );
3526 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
3527 Debug( pcache_debug,
3528 "STALE QUERY REMOVED, CACHE ="
3530 cm->cur_entries, 0, 0 );
3532 } else if ( !templ->ttr && query->expiry_time > ttl ) {
3533 /* We don't need to check for refreshes, and this
3534 * query's expiry is too new, and all subsequent queries
3535 * will be newer yet. So stop looking.
3537 * If we have refreshes, then we always have to walk the
3538 * entire query list.
3546 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3547 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
3548 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
3550 /* If there were no queries, defer processing for a while */
3551 if ( cm->cc_paused != pause )
3552 cm->cc_paused = pause;
3553 ldap_pvt_runqueue_resched( &slapd_rq, rtask, pause );
3555 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3560 #define MAX_ATTR_SETS 500
3573 static ConfigDriver pc_cf_gen;
3574 static ConfigLDAPadd pc_ldadd;
3575 static ConfigCfAdd pc_cfadd;
3577 static ConfigTable pccfg[] = {
3578 { "pcache", "backend> <max_entries> <numattrsets> <entry limit> "
3580 6, 6, 0, ARG_MAGIC|ARG_NO_DELETE|PC_MAIN, pc_cf_gen,
3581 "( OLcfgOvAt:2.1 NAME ( 'olcPcache' 'olcProxyCache' ) "
3582 "DESC 'Proxy Cache basic parameters' "
3583 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
3584 { "pcacheAttrset", "index> <attributes...",
3585 2, 0, 0, ARG_MAGIC|PC_ATTR, pc_cf_gen,
3586 "( OLcfgOvAt:2.2 NAME ( 'olcPcacheAttrset' 'olcProxyAttrset' ) "
3587 "DESC 'A set of attributes to cache' "
3588 "SYNTAX OMsDirectoryString )", NULL, NULL },
3589 { "pcacheTemplate", "filter> <attrset-index> <TTL> <negTTL> "
3591 4, 7, 0, ARG_MAGIC|PC_TEMP, pc_cf_gen,
3592 "( OLcfgOvAt:2.3 NAME ( 'olcPcacheTemplate' 'olcProxyCacheTemplate' ) "
3593 "DESC 'Filter template, attrset, cache TTL, "
3594 "optional negative TTL, optional sizelimit TTL, "
3596 "SYNTAX OMsDirectoryString )", NULL, NULL },
3597 { "pcachePosition", "head|tail(default)",
3598 2, 2, 0, ARG_MAGIC|PC_RESP, pc_cf_gen,
3599 "( OLcfgOvAt:2.4 NAME 'olcPcachePosition' "
3600 "DESC 'Response callback position in overlay stack' "
3601 "SYNTAX OMsDirectoryString )", NULL, NULL },
3602 { "pcacheMaxQueries", "queries",
3603 2, 2, 0, ARG_INT|ARG_MAGIC|PC_QUERIES, pc_cf_gen,
3604 "( OLcfgOvAt:2.5 NAME ( 'olcPcacheMaxQueries' 'olcProxyCacheQueries' ) "
3605 "DESC 'Maximum number of queries to cache' "
3606 "SYNTAX OMsInteger )", NULL, NULL },
3607 { "pcachePersist", "TRUE|FALSE",
3608 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, save_queries),
3609 "( OLcfgOvAt:2.6 NAME ( 'olcPcachePersist' 'olcProxySaveQueries' ) "
3610 "DESC 'Save cached queries for hot restart' "
3611 "SYNTAX OMsBoolean )", NULL, NULL },
3612 { "pcacheValidate", "TRUE|FALSE",
3613 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, check_cacheability),
3614 "( OLcfgOvAt:2.7 NAME ( 'olcPcacheValidate' 'olcProxyCheckCacheability' ) "
3615 "DESC 'Check whether the results of a query are cacheable, e.g. for schema issues' "
3616 "SYNTAX OMsBoolean )", NULL, NULL },
3617 { "pcacheOffline", "TRUE|FALSE",
3618 2, 2, 0, ARG_ON_OFF|ARG_MAGIC|PC_OFFLINE, pc_cf_gen,
3619 "( OLcfgOvAt:2.8 NAME 'olcPcacheOffline' "
3620 "DESC 'Set cache to offline mode and disable expiration' "
3621 "SYNTAX OMsBoolean )", NULL, NULL },
3622 { "pcacheBind", "filter> <attrset-index> <TTR> <scope> <base",
3623 6, 6, 0, ARG_MAGIC|PC_BIND, pc_cf_gen,
3624 "( OLcfgOvAt:2.9 NAME 'olcPcacheBind' "
3625 "DESC 'Parameters for caching Binds' "
3626 "SYNTAX OMsDirectoryString )", NULL, NULL },
3627 { "pcache-", "private database args",
3628 1, 0, STRLENOF("pcache-"), ARG_MAGIC|PC_PRIVATE_DB, pc_cf_gen,
3631 /* Legacy keywords */
3632 { "proxycache", "backend> <max_entries> <numattrsets> <entry limit> "
3634 6, 6, 0, ARG_MAGIC|ARG_NO_DELETE|PC_MAIN, pc_cf_gen,
3636 { "proxyattrset", "index> <attributes...",
3637 2, 0, 0, ARG_MAGIC|PC_ATTR, pc_cf_gen,
3639 { "proxytemplate", "filter> <attrset-index> <TTL> <negTTL",
3640 4, 7, 0, ARG_MAGIC|PC_TEMP, pc_cf_gen,
3642 { "response-callback", "head|tail(default)",
3643 2, 2, 0, ARG_MAGIC|PC_RESP, pc_cf_gen,
3645 { "proxyCacheQueries", "queries",
3646 2, 2, 0, ARG_INT|ARG_MAGIC|PC_QUERIES, pc_cf_gen,
3648 { "proxySaveQueries", "TRUE|FALSE",
3649 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, save_queries),
3651 { "proxyCheckCacheability", "TRUE|FALSE",
3652 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, check_cacheability),
3655 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
3658 static ConfigOCs pcocs[] = {
3659 { "( OLcfgOvOc:2.1 "
3660 "NAME 'olcPcacheConfig' "
3661 "DESC 'ProxyCache configuration' "
3662 "SUP olcOverlayConfig "
3663 "MUST ( olcPcache $ olcPcacheAttrset $ olcPcacheTemplate ) "
3664 "MAY ( olcPcachePosition $ olcPcacheMaxQueries $ olcPcachePersist $ "
3665 "olcPcacheValidate $ olcPcacheOffline $ olcPcacheBind ) )",
3666 Cft_Overlay, pccfg, NULL, pc_cfadd },
3667 { "( OLcfgOvOc:2.2 "
3668 "NAME 'olcPcacheDatabase' "
3669 "DESC 'Cache database configuration' "
3670 "AUXILIARY )", Cft_Misc, olcDatabaseDummy, pc_ldadd },
3674 static int pcache_db_open2( slap_overinst *on, ConfigReply *cr );
3677 pc_ldadd_cleanup( ConfigArgs *c )
3679 slap_overinst *on = c->ca_private;
3680 return pcache_db_open2( on, &c->reply );
3684 pc_ldadd( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
3689 if ( p->ce_type != Cft_Overlay || !p->ce_bi ||
3690 p->ce_bi->bi_cf_ocs != pcocs )
3691 return LDAP_CONSTRAINT_VIOLATION;
3693 on = (slap_overinst *)p->ce_bi;
3694 cm = on->on_bi.bi_private;
3696 /* Defer open if this is an LDAPadd */
3697 if ( CONFIG_ONLINE_ADD( ca ))
3698 ca->cleanup = pc_ldadd_cleanup;
3700 cm->defer_db_open = 0;
3701 ca->ca_private = on;
3702 return LDAP_SUCCESS;
3706 pc_cfadd( Operation *op, SlapReply *rs, Entry *p, ConfigArgs *ca )
3708 CfEntryInfo *pe = p->e_private;
3709 slap_overinst *on = (slap_overinst *)pe->ce_bi;
3710 cache_manager *cm = on->on_bi.bi_private;
3713 /* FIXME: should not hardcode "olcDatabase" here */
3714 bv.bv_len = snprintf( ca->cr_msg, sizeof( ca->cr_msg ),
3715 "olcDatabase=" SLAP_X_ORDERED_FMT "%s",
3716 0, cm->db.bd_info->bi_type );
3717 if ( bv.bv_len >= sizeof( ca->cr_msg ) ) {
3720 bv.bv_val = ca->cr_msg;
3722 cm->defer_db_open = 0;
3724 /* We can only create this entry if the database is table-driven
3726 if ( cm->db.bd_info->bi_cf_ocs )
3727 config_build_entry( op, rs, pe, ca, &bv, cm->db.bd_info->bi_cf_ocs,
3734 pc_cf_gen( ConfigArgs *c )
3736 slap_overinst *on = (slap_overinst *)c->bi;
3737 cache_manager* cm = on->on_bi.bi_private;
3738 query_manager* qm = cm->qm;
3739 QueryTemplate* temp;
3740 AttributeName* attr_name;
3741 AttributeName* attrarray;
3742 const char* text=NULL;
3747 if ( c->op == SLAP_CONFIG_EMIT ) {
3751 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s %d %d %d %ld",
3752 cm->db.bd_info->bi_type, cm->max_entries, cm->numattrsets,
3753 cm->num_entries_limit, cm->cc_period );
3754 bv.bv_val = c->cr_msg;
3755 value_add_one( &c->rvalue_vals, &bv );
3758 for (i=0; i<cm->numattrsets; i++) {
3759 if ( !qm->attr_sets[i].count ) continue;
3761 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ), "%d", i );
3763 /* count the attr length */
3764 for ( attr_name = qm->attr_sets[i].attrs;
3765 attr_name->an_name.bv_val; attr_name++ )
3766 bv.bv_len += attr_name->an_name.bv_len + 1;
3768 bv.bv_val = ch_malloc( bv.bv_len+1 );
3769 ptr = lutil_strcopy( bv.bv_val, c->cr_msg );
3770 for ( attr_name = qm->attr_sets[i].attrs;
3771 attr_name->an_name.bv_val; attr_name++ ) {
3773 ptr = lutil_strcopy( ptr, attr_name->an_name.bv_val );
3775 ber_bvarray_add( &c->rvalue_vals, &bv );
3777 if ( !c->rvalue_vals )
3781 for (temp=qm->templates; temp; temp=temp->qmnext) {
3782 /* HEADS-UP: always print all;
3783 * if optional == 0, ignore */
3784 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ),
3785 " %d %ld %ld %ld %ld",
3786 temp->attr_set_index,
3791 bv.bv_len += temp->querystr.bv_len + 2;
3792 bv.bv_val = ch_malloc( bv.bv_len+1 );
3795 ptr = lutil_strcopy( ptr, temp->querystr.bv_val );
3797 strcpy( ptr, c->cr_msg );
3798 ber_bvarray_add( &c->rvalue_vals, &bv );
3800 if ( !c->rvalue_vals )
3804 for (temp=qm->templates; temp; temp=temp->qmnext) {
3805 if ( !temp->bindttr ) continue;
3806 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ),
3808 temp->attr_set_index,
3810 ldap_pvt_scope2str( temp->bindscope ));
3811 bv.bv_len += temp->bindbase.bv_len + temp->bindftemp.bv_len + 4;
3812 bv.bv_val = ch_malloc( bv.bv_len + 1 );
3815 ptr = lutil_strcopy( ptr, temp->bindftemp.bv_val );
3817 ptr = lutil_strcopy( ptr, c->cr_msg );
3819 ptr = lutil_strcopy( ptr, temp->bindbase.bv_val );
3822 ber_bvarray_add( &c->rvalue_vals, &bv );
3824 if ( !c->rvalue_vals )
3828 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
3829 BER_BVSTR( &bv, "head" );
3831 BER_BVSTR( &bv, "tail" );
3833 value_add_one( &c->rvalue_vals, &bv );
3836 c->value_int = cm->max_queries;
3839 c->value_int = (cm->cc_paused & PCACHE_CC_OFFLINE) != 0;
3843 } else if ( c->op == LDAP_MOD_DELETE ) {
3846 case PC_ATTR: /* FIXME */
3851 cm->cc_paused &= ~PCACHE_CC_OFFLINE;
3852 /* If there were cached queries when we went offline,
3853 * restart the checker now.
3855 if ( cm->num_cached_queries ) {
3856 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3858 ldap_pvt_runqueue_resched( &slapd_rq, cm->cc_arg, 0 );
3859 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3869 if ( cm->numattrsets > 0 ) {
3870 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"pcache\" directive already provided" );
3871 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3875 if ( lutil_atoi( &cm->numattrsets, c->argv[3] ) != 0 ) {
3876 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse num attrsets=\"%s\" (arg #3)",
3878 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3881 if ( cm->numattrsets <= 0 ) {
3882 snprintf( c->cr_msg, sizeof( c->cr_msg ), "numattrsets (arg #3) must be positive" );
3883 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3886 if ( cm->numattrsets > MAX_ATTR_SETS ) {
3887 snprintf( c->cr_msg, sizeof( c->cr_msg ), "numattrsets (arg #3) must be <= %d", MAX_ATTR_SETS );
3888 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3892 if ( !backend_db_init( c->argv[1], &cm->db, -1, NULL )) {
3893 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unknown backend type (arg #1)" );
3894 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3898 if ( lutil_atoi( &cm->max_entries, c->argv[2] ) != 0 ) {
3899 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse max entries=\"%s\" (arg #2)",
3901 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3904 if ( cm->max_entries <= 0 ) {
3905 snprintf( c->cr_msg, sizeof( c->cr_msg ), "max entries (arg #2) must be positive.\n" );
3906 Debug( LDAP_DEBUG_CONFIG, "%s: %s\n", c->log, c->cr_msg, 0 );
3910 if ( lutil_atoi( &cm->num_entries_limit, c->argv[4] ) != 0 ) {
3911 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse entry limit=\"%s\" (arg #4)",
3913 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3916 if ( cm->num_entries_limit <= 0 ) {
3917 snprintf( c->cr_msg, sizeof( c->cr_msg ), "entry limit (arg #4) must be positive" );
3918 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3921 if ( cm->num_entries_limit > cm->max_entries ) {
3922 snprintf( c->cr_msg, sizeof( c->cr_msg ), "entry limit (arg #4) must be less than max entries %d (arg #2)", cm->max_entries );
3923 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3927 if ( lutil_parse_time( c->argv[5], &t ) != 0 ) {
3928 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse period=\"%s\" (arg #5)",
3930 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3934 cm->cc_period = (time_t)t;
3935 Debug( pcache_debug,
3936 "Total # of attribute sets to be cached = %d.\n",
3937 cm->numattrsets, 0, 0 );
3938 qm->attr_sets = ( struct attr_set * )ch_calloc( cm->numattrsets,
3939 sizeof( struct attr_set ) );
3942 if ( cm->numattrsets == 0 ) {
3943 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"pcache\" directive not provided yet" );
3944 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3947 if ( lutil_atoi( &num, c->argv[1] ) != 0 ) {
3948 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse attrset #=\"%s\"",
3950 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3954 if ( num < 0 || num >= cm->numattrsets ) {
3955 snprintf( c->cr_msg, sizeof( c->cr_msg ), "attrset index %d out of bounds (must be %s%d)",
3956 num, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
3957 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3960 qm->attr_sets[num].flags |= PC_CONFIGURED;
3961 if ( c->argc == 2 ) {
3963 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3964 "need an explicit attr in attrlist; use \"*\" to indicate all attrs" );
3965 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3968 } else if ( c->argc == 3 ) {
3969 if ( strcmp( c->argv[2], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) {
3970 qm->attr_sets[num].count = 1;
3971 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 2,
3972 sizeof( AttributeName ) );
3973 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_USER_ATTRIBUTES );
3976 } else if ( strcmp( c->argv[2], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 ) {
3977 qm->attr_sets[num].count = 1;
3978 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 2,
3979 sizeof( AttributeName ) );
3980 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
3983 } else if ( strcmp( c->argv[2], LDAP_NO_ATTRS ) == 0 ) {
3986 /* else: fallthru */
3988 } else if ( c->argc == 4 ) {
3989 if ( ( strcmp( c->argv[2], LDAP_ALL_USER_ATTRIBUTES ) == 0 && strcmp( c->argv[3], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 )
3990 || ( strcmp( c->argv[2], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 && strcmp( c->argv[3], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) )
3992 qm->attr_sets[num].count = 2;
3993 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 3,
3994 sizeof( AttributeName ) );
3995 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_USER_ATTRIBUTES );
3996 BER_BVSTR( &qm->attr_sets[num].attrs[1].an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
3999 /* else: fallthru */
4002 if ( c->argc > 2 ) {
4003 int all_user = 0, all_op = 0;
4005 qm->attr_sets[num].count = c->argc - 2;
4006 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( c->argc - 1,
4007 sizeof( AttributeName ) );
4008 attr_name = qm->attr_sets[num].attrs;
4009 for ( i = 2; i < c->argc; i++ ) {
4010 attr_name->an_desc = NULL;
4011 if ( strcmp( c->argv[i], LDAP_NO_ATTRS ) == 0 ) {
4012 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4013 "invalid attr #%d \"%s\" in attrlist",
4014 i - 2, c->argv[i] );
4015 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4016 ch_free( qm->attr_sets[num].attrs );
4017 qm->attr_sets[num].attrs = NULL;
4018 qm->attr_sets[num].count = 0;
4021 if ( strcmp( c->argv[i], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) {
4023 BER_BVSTR( &attr_name->an_name, LDAP_ALL_USER_ATTRIBUTES );
4024 } else if ( strcmp( c->argv[i], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 ) {
4026 BER_BVSTR( &attr_name->an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
4028 if ( slap_str2ad( c->argv[i], &attr_name->an_desc, &text ) ) {
4029 strcpy( c->cr_msg, text );
4030 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4031 ch_free( qm->attr_sets[num].attrs );
4032 qm->attr_sets[num].attrs = NULL;
4033 qm->attr_sets[num].count = 0;
4036 attr_name->an_name = attr_name->an_desc->ad_cname;
4038 attr_name->an_oc = NULL;
4039 attr_name->an_flags = 0;
4040 if ( attr_name->an_desc == slap_schema.si_ad_objectClass )
4041 qm->attr_sets[num].flags |= PC_GOT_OC;
4043 BER_BVZERO( &attr_name->an_name );
4046 /* warn if list contains both "*" and "+" */
4047 if ( i > 4 && all_user && all_op ) {
4048 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4049 "warning: attribute list contains \"*\" and \"+\"" );
4050 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4055 if ( cm->numattrsets == 0 ) {
4056 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"pcache\" directive not provided yet" );
4057 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4060 if ( lutil_atoi( &i, c->argv[2] ) != 0 ) {
4061 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse template #=\"%s\"",
4063 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4067 if ( i < 0 || i >= cm->numattrsets ||
4068 !(qm->attr_sets[i].flags & PC_CONFIGURED )) {
4069 snprintf( c->cr_msg, sizeof( c->cr_msg ), "template index %d invalid (%s%d)",
4070 i, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
4071 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4075 AttributeName *attrs;
4077 cnt = template_attrs( c->argv[1], &qm->attr_sets[i], &attrs, &text );
4079 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse template: %s",
4081 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4084 temp = ch_calloc( 1, sizeof( QueryTemplate ));
4085 temp->qmnext = qm->templates;
4086 qm->templates = temp;
4087 temp->t_attrs.attrs = attrs;
4088 temp->t_attrs.count = cnt;
4090 ldap_pvt_thread_rdwr_init( &temp->t_rwlock );
4091 temp->query = temp->query_last = NULL;
4092 if ( lutil_parse_time( c->argv[3], &t ) != 0 ) {
4093 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4094 "unable to parse template ttl=\"%s\"",
4096 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4098 ch_free( temp->t_attrs.attrs );
4102 temp->ttl = (time_t)t;
4103 temp->negttl = (time_t)0;
4104 temp->limitttl = (time_t)0;
4105 temp->ttr = (time_t)0;
4106 switch ( c->argc ) {
4108 if ( lutil_parse_time( c->argv[6], &t ) != 0 ) {
4109 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4110 "unable to parse template ttr=\"%s\"",
4112 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4115 temp->ttr = (time_t)t;
4119 if ( lutil_parse_time( c->argv[5], &t ) != 0 ) {
4120 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4121 "unable to parse template sizelimit ttl=\"%s\"",
4123 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4126 temp->limitttl = (time_t)t;
4130 if ( lutil_parse_time( c->argv[4], &t ) != 0 ) {
4131 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4132 "unable to parse template negative ttl=\"%s\"",
4134 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4137 temp->negttl = (time_t)t;
4141 temp->no_of_queries = 0;
4143 ber_str2bv( c->argv[1], 0, 1, &temp->querystr );
4144 Debug( pcache_debug, "Template:\n", 0, 0, 0 );
4145 Debug( pcache_debug, " query template: %s\n",
4146 temp->querystr.bv_val, 0, 0 );
4147 temp->attr_set_index = i;
4148 qm->attr_sets[i].flags |= PC_REFERENCED;
4149 temp->qtnext = qm->attr_sets[i].templates;
4150 qm->attr_sets[i].templates = temp;
4151 Debug( pcache_debug, " attributes: \n", 0, 0, 0 );
4152 if ( ( attrarray = qm->attr_sets[i].attrs ) != NULL ) {
4153 for ( i=0; attrarray[i].an_name.bv_val; i++ )
4154 Debug( pcache_debug, "\t%s\n",
4155 attrarray[i].an_name.bv_val, 0, 0 );
4159 if ( !qm->templates ) {
4160 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"pcacheTemplate\" directive not provided yet" );
4161 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4164 if ( lutil_atoi( &i, c->argv[2] ) != 0 ) {
4165 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse Bind index #=\"%s\"",
4167 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4171 if ( i < 0 || i >= cm->numattrsets ||
4172 !(qm->attr_sets[i].flags & PC_CONFIGURED )) {
4173 snprintf( c->cr_msg, sizeof( c->cr_msg ), "Bind index %d invalid (%s%d)",
4174 i, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
4175 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4178 { struct berval bv, tempbv;
4179 AttributeDescription **descs;
4181 ber_str2bv( c->argv[1], 0, 0, &bv );
4182 ndescs = ftemp_attrs( &bv, &tempbv, &descs, &text );
4184 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse template: %s",
4186 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4189 for ( temp = qm->templates; temp; temp=temp->qmnext ) {
4190 if ( temp->attr_set_index == i && bvmatch( &tempbv,
4194 ch_free( tempbv.bv_val );
4197 snprintf( c->cr_msg, sizeof( c->cr_msg ), "Bind template %s %d invalid",
4199 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4202 ber_dupbv( &temp->bindftemp, &bv );
4203 temp->bindfattrs = descs;
4204 temp->bindnattrs = ndescs;
4206 if ( lutil_parse_time( c->argv[3], &t ) != 0 ) {
4207 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4208 "unable to parse bind ttr=\"%s\"",
4210 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4212 ch_free( temp->bindfattrs );
4213 temp->bindfattrs = NULL;
4214 ch_free( temp->bindftemp.bv_val );
4215 BER_BVZERO( &temp->bindftemp );
4218 num = ldap_pvt_str2scope( c->argv[4] );
4220 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4221 "unable to parse bind scope=\"%s\"",
4223 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4227 struct berval dn, ndn;
4228 ber_str2bv( c->argv[5], 0, 0, &dn );
4229 rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
4231 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4232 "invalid bind baseDN=\"%s\"",
4234 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4237 if ( temp->bindbase.bv_val )
4238 ch_free( temp->bindbase.bv_val );
4239 temp->bindbase = ndn;
4242 /* convert the template into dummy filter */
4244 char *eq = temp->bindftemp.bv_val, *e2;
4247 while ((eq = strchr(eq, '=' ))) {
4252 bv.bv_len = temp->bindftemp.bv_len + i;
4253 bv.bv_val = ch_malloc( bv.bv_len + 1 );
4254 for ( e2 = bv.bv_val, eq = temp->bindftemp.bv_val;
4265 f = str2filter( bv.bv_val );
4267 ch_free( bv.bv_val );
4268 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4269 "unable to parse bindfilter=\"%s\"", bv.bv_val );
4270 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4271 ch_free( temp->bindbase.bv_val );
4272 BER_BVZERO( &temp->bindbase );
4275 if ( temp->bindfilter )
4276 filter_free( temp->bindfilter );
4277 if ( temp->bindfilterstr.bv_val )
4278 ch_free( temp->bindfilterstr.bv_val );
4279 temp->bindfilterstr = bv;
4280 temp->bindfilter = f;
4282 temp->bindttr = (time_t)t;
4283 temp->bindscope = num;
4284 cm->cache_binds = 1;
4288 if ( strcasecmp( c->argv[1], "head" ) == 0 ) {
4289 cm->response_cb = PCACHE_RESPONSE_CB_HEAD;
4291 } else if ( strcasecmp( c->argv[1], "tail" ) == 0 ) {
4292 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
4295 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unknown specifier" );
4296 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4301 if ( c->value_int <= 0 ) {
4302 snprintf( c->cr_msg, sizeof( c->cr_msg ), "max queries must be positive" );
4303 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4306 cm->max_queries = c->value_int;
4310 cm->cc_paused |= PCACHE_CC_OFFLINE;
4312 cm->cc_paused &= ~PCACHE_CC_OFFLINE;
4315 if ( cm->db.be_private == NULL ) {
4316 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4317 "private database must be defined before setting database specific options" );
4318 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4322 if ( cm->db.bd_info->bi_cf_ocs ) {
4325 char *argv0 = c->argv[ 0 ];
4327 c->argv[ 0 ] = &argv0[ STRLENOF( "pcache-" ) ];
4329 ct = config_find_keyword( cm->db.bd_info->bi_cf_ocs->co_table, c );
4331 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4332 "private database does not recognize specific option '%s'",
4334 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4338 c->table = cm->db.bd_info->bi_cf_ocs->co_type;
4340 c->bi = c->be->bd_info;
4342 rc = config_add_vals( ct, c );
4346 c->table = c2.table;
4349 c->argv[ 0 ] = argv0;
4351 } else if ( cm->db.be_config != NULL ) {
4352 char *argv0 = c->argv[ 0 ];
4354 c->argv[ 0 ] = &argv0[ STRLENOF( "pcache-" ) ];
4355 rc = cm->db.be_config( &cm->db, c->fname, c->lineno, c->argc, c->argv );
4356 c->argv[ 0 ] = argv0;
4359 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4360 "no means to set private database specific options" );
4361 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4366 rc = SLAP_CONF_UNKNOWN;
4382 slap_overinst *on = (slap_overinst *)be->bd_info;
4383 cache_manager* cm = on->on_bi.bi_private;
4385 /* Something for the cache database? */
4386 if ( cm->db.bd_info && cm->db.bd_info->bi_db_config )
4387 return cm->db.bd_info->bi_db_config( &cm->db, fname, lineno,
4389 return SLAP_CONF_UNKNOWN;
4397 slap_overinst *on = (slap_overinst *)be->bd_info;
4401 cm = (cache_manager *)ch_malloc(sizeof(cache_manager));
4402 on->on_bi.bi_private = cm;
4404 qm = (query_manager*)ch_malloc(sizeof(query_manager));
4407 SLAP_DBFLAGS(&cm->db) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
4408 cm->db.be_private = NULL;
4409 cm->db.bd_self = &cm->db;
4411 cm->numattrsets = 0;
4412 cm->num_entries_limit = 5;
4413 cm->num_cached_queries = 0;
4414 cm->max_entries = 0;
4415 cm->cur_entries = 0;
4416 cm->max_queries = 10000;
4417 cm->save_queries = 0;
4418 cm->check_cacheability = 0;
4419 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
4420 cm->defer_db_open = 1;
4421 cm->cache_binds = 0;
4422 cm->cc_period = 1000;
4425 #ifdef PCACHE_MONITOR
4426 cm->monitor_cb = NULL;
4427 #endif /* PCACHE_MONITOR */
4429 qm->attr_sets = NULL;
4430 qm->templates = NULL;
4432 qm->lru_bottom = NULL;
4434 qm->qcfunc = query_containment;
4435 qm->crfunc = cache_replacement;
4436 qm->addfunc = add_query;
4437 ldap_pvt_thread_mutex_init(&qm->lru_mutex);
4439 ldap_pvt_thread_mutex_init(&cm->cache_mutex);
4441 #ifndef PCACHE_MONITOR
4443 #else /* PCACHE_MONITOR */
4444 return pcache_monitor_db_init( be );
4445 #endif /* PCACHE_MONITOR */
4449 pcache_cachedquery_open_cb( Operation *op, SlapReply *rs )
4451 assert( op->o_tag == LDAP_REQ_SEARCH );
4453 if ( rs->sr_type == REP_SEARCH ) {
4456 a = attr_find( rs->sr_entry->e_attrs, ad_cachedQueryURL );
4460 assert( a->a_nvals != NULL );
4462 valsp = op->o_callback->sc_private;
4463 assert( *valsp == NULL );
4465 ber_bvarray_dup_x( valsp, a->a_nvals, op->o_tmpmemctx );
4473 pcache_cachedquery_count_cb( Operation *op, SlapReply *rs )
4475 assert( op->o_tag == LDAP_REQ_SEARCH );
4477 if ( rs->sr_type == REP_SEARCH ) {
4478 int *countp = (int *)op->o_callback->sc_private;
4491 cache_manager *cm = on->on_bi.bi_private;
4492 query_manager* qm = cm->qm;
4495 rc = backend_startup_one( &cm->db, cr );
4497 cm->defer_db_open = 0;
4500 /* There is no runqueue in TOOL mode */
4501 if (( slapMode & SLAP_SERVER_MODE ) && rc == 0 ) {
4502 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4503 ldap_pvt_runqueue_insert( &slapd_rq, cm->cc_period,
4504 consistency_check, on,
4505 "pcache_consistency", cm->db.be_suffix[0].bv_val );
4506 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4508 /* Cached database must have the rootdn */
4509 if ( BER_BVISNULL( &cm->db.be_rootndn )
4510 || BER_BVISEMPTY( &cm->db.be_rootndn ) )
4512 Debug( LDAP_DEBUG_ANY, "pcache_db_open(): "
4513 "underlying database of type \"%s\"\n"
4514 " serving naming context \"%s\"\n"
4515 " has no \"rootdn\", required by \"pcache\".\n",
4516 on->on_info->oi_orig->bi_type,
4517 cm->db.be_suffix[0].bv_val, 0 );
4521 if ( cm->save_queries ) {
4522 void *thrctx = ldap_pvt_thread_pool_context();
4523 Connection conn = { 0 };
4524 OperationBuffer opbuf;
4526 slap_callback cb = { 0 };
4527 SlapReply rs = { REP_RESULT };
4528 BerVarray vals = NULL;
4529 Filter f = { 0 }, f2 = { 0 };
4530 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
4531 AttributeName attrs[ 2 ] = {{{ 0 }}};
4533 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
4538 op->o_tag = LDAP_REQ_SEARCH;
4539 op->o_protocol = LDAP_VERSION3;
4540 cb.sc_response = pcache_cachedquery_open_cb;
4541 cb.sc_private = &vals;
4542 op->o_callback = &cb;
4543 op->o_time = slap_get_time();
4544 op->o_do_not_cache = 1;
4545 op->o_managedsait = SLAP_CONTROL_CRITICAL;
4547 op->o_dn = cm->db.be_rootdn;
4548 op->o_ndn = cm->db.be_rootndn;
4549 op->o_req_dn = cm->db.be_suffix[ 0 ];
4550 op->o_req_ndn = cm->db.be_nsuffix[ 0 ];
4552 op->ors_scope = LDAP_SCOPE_BASE;
4553 op->ors_deref = LDAP_DEREF_NEVER;
4555 op->ors_tlimit = SLAP_NO_LIMIT;
4556 op->ors_limit = NULL;
4557 ber_str2bv( "(pcacheQueryURL=*)", 0, 0, &op->ors_filterstr );
4558 f.f_choice = LDAP_FILTER_PRESENT;
4559 f.f_desc = ad_cachedQueryURL;
4560 op->ors_filter = &f;
4561 attrs[ 0 ].an_desc = ad_cachedQueryURL;
4562 attrs[ 0 ].an_name = ad_cachedQueryURL->ad_cname;
4563 op->ors_attrs = attrs;
4564 op->ors_attrsonly = 0;
4566 rc = op->o_bd->be_search( op, &rs );
4567 if ( rc == LDAP_SUCCESS && vals != NULL ) {
4570 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
4571 if ( url2query( vals[ i ].bv_val, op, qm ) == 0 ) {
4572 cm->num_cached_queries++;
4576 ber_bvarray_free_x( vals, op->o_tmpmemctx );
4579 /* count cached entries */
4580 f.f_choice = LDAP_FILTER_NOT;
4582 f2.f_choice = LDAP_FILTER_EQUALITY;
4584 f2.f_av_desc = slap_schema.si_ad_objectClass;
4585 BER_BVSTR( &f2.f_av_value, "glue" );
4586 ber_str2bv( "(!(objectClass=glue))", 0, 0, &op->ors_filterstr );
4588 op->ors_slimit = SLAP_NO_LIMIT;
4589 op->ors_scope = LDAP_SCOPE_SUBTREE;
4590 op->ors_attrs = slap_anlist_no_attrs;
4592 rs_reinit( &rs, REP_RESULT );
4593 op->o_callback->sc_response = pcache_cachedquery_count_cb;
4594 op->o_callback->sc_private = &rs.sr_nentries;
4596 rc = op->o_bd->be_search( op, &rs );
4598 cm->cur_entries = rs.sr_nentries;
4612 slap_overinst *on = (slap_overinst *)be->bd_info;
4613 cache_manager *cm = on->on_bi.bi_private;
4614 query_manager* qm = cm->qm;
4615 int i, ncf = 0, rf = 0, nrf = 0, rc = 0;
4617 /* check attr sets */
4618 for ( i = 0; i < cm->numattrsets; i++) {
4619 if ( !( qm->attr_sets[i].flags & PC_CONFIGURED ) ) {
4620 if ( qm->attr_sets[i].flags & PC_REFERENCED ) {
4621 Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d not configured but referenced.\n", i, 0, 0 );
4625 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, attr set #%d not configured.\n", i, 0, 0 );
4629 } else if ( !( qm->attr_sets[i].flags & PC_REFERENCED ) ) {
4630 Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d configured but not referenced.\n", i, 0, 0 );
4635 if ( ncf || rf || nrf ) {
4636 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets configured but not referenced.\n", nrf, 0, 0 );
4637 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets not configured.\n", ncf, 0, 0 );
4638 Debug( LDAP_DEBUG_CONFIG, "pcache: %d attr sets not configured but referenced.\n", rf, 0, 0 );
4645 /* need to inherit something from the original database... */
4646 cm->db.be_def_limit = be->be_def_limit;
4647 cm->db.be_limits = be->be_limits;
4648 cm->db.be_acl = be->be_acl;
4649 cm->db.be_dfltaccess = be->be_dfltaccess;
4651 if ( SLAP_DBMONITORING( be ) ) {
4652 SLAP_DBFLAGS( &cm->db ) |= SLAP_DBFLAG_MONITORING;
4655 SLAP_DBFLAGS( &cm->db ) &= ~SLAP_DBFLAG_MONITORING;
4658 if ( !cm->defer_db_open ) {
4659 rc = pcache_db_open2( on, cr );
4662 #ifdef PCACHE_MONITOR
4663 if ( rc == LDAP_SUCCESS ) {
4664 rc = pcache_monitor_db_open( be );
4666 #endif /* PCACHE_MONITOR */
4672 pcache_free_qbase( void *v )
4678 tavl_free( qb->scopes[i], NULL );
4688 slap_overinst *on = (slap_overinst *)be->bd_info;
4689 cache_manager *cm = on->on_bi.bi_private;
4690 query_manager *qm = cm->qm;
4694 /* stop the thread ... */
4696 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4697 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, cm->cc_arg ) ) {
4698 ldap_pvt_runqueue_stoptask( &slapd_rq, cm->cc_arg );
4700 ldap_pvt_runqueue_remove( &slapd_rq, cm->cc_arg );
4701 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4704 if ( cm->save_queries ) {
4706 BerVarray vals = NULL;
4709 Connection conn = { 0 };
4710 OperationBuffer opbuf;
4712 slap_callback cb = { 0 };
4714 SlapReply rs = { REP_RESULT };
4715 Modifications mod = {{ 0 }};
4717 thrctx = ldap_pvt_thread_pool_context();
4719 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
4722 mod.sml_numvals = 0;
4723 if ( qm->templates != NULL ) {
4724 for ( tm = qm->templates; tm != NULL; tm = tm->qmnext ) {
4725 for ( qc = tm->query; qc; qc = qc->next ) {
4728 if ( query2url( op, qc, &bv, 0 ) == 0 ) {
4729 ber_bvarray_add_x( &vals, &bv, op->o_tmpmemctx );
4737 op->o_dn = cm->db.be_rootdn;
4738 op->o_ndn = cm->db.be_rootndn;
4740 op->o_tag = LDAP_REQ_MODIFY;
4741 op->o_protocol = LDAP_VERSION3;
4742 cb.sc_response = slap_null_cb;
4743 op->o_callback = &cb;
4744 op->o_time = slap_get_time();
4745 op->o_do_not_cache = 1;
4746 op->o_managedsait = SLAP_CONTROL_CRITICAL;
4748 op->o_req_dn = op->o_bd->be_suffix[0];
4749 op->o_req_ndn = op->o_bd->be_nsuffix[0];
4751 mod.sml_op = LDAP_MOD_REPLACE;
4753 mod.sml_desc = ad_cachedQueryURL;
4754 mod.sml_type = ad_cachedQueryURL->ad_cname;
4755 mod.sml_values = vals;
4756 mod.sml_nvalues = NULL;
4757 mod.sml_next = NULL;
4758 Debug( pcache_debug,
4759 "%sSETTING CACHED QUERY URLS\n",
4760 vals == NULL ? "RE" : "", 0, 0 );
4762 op->orm_modlist = &mod;
4764 op->o_bd->be_modify( op, &rs );
4766 ber_bvarray_free_x( vals, op->o_tmpmemctx );
4769 /* cleanup stuff inherited from the original database... */
4770 cm->db.be_limits = NULL;
4771 cm->db.be_acl = NULL;
4774 if ( cm->db.bd_info->bi_db_close ) {
4775 rc = cm->db.bd_info->bi_db_close( &cm->db, NULL );
4777 while ( (tm = qm->templates) != NULL ) {
4778 CachedQuery *qc, *qn;
4779 qm->templates = tm->qmnext;
4780 for ( qc = tm->query; qc; qc = qn ) {
4784 avl_free( tm->qbase, pcache_free_qbase );
4785 free( tm->querystr.bv_val );
4786 free( tm->bindfattrs );
4787 free( tm->bindftemp.bv_val );
4788 free( tm->bindfilterstr.bv_val );
4789 free( tm->bindbase.bv_val );
4790 filter_free( tm->bindfilter );
4791 ldap_pvt_thread_rdwr_destroy( &tm->t_rwlock );
4792 free( tm->t_attrs.attrs );
4796 for ( i=0; i<cm->numattrsets; i++ ) {
4797 free( qm->attr_sets[i].attrs );
4799 free( qm->attr_sets );
4800 qm->attr_sets = NULL;
4802 #ifdef PCACHE_MONITOR
4803 if ( rc == LDAP_SUCCESS ) {
4804 rc = pcache_monitor_db_close( be );
4806 #endif /* PCACHE_MONITOR */
4817 slap_overinst *on = (slap_overinst *)be->bd_info;
4818 cache_manager *cm = on->on_bi.bi_private;
4819 query_manager *qm = cm->qm;
4821 if ( cm->db.be_private != NULL ) {
4822 backend_stopdown_one( &cm->db );
4825 ldap_pvt_thread_mutex_destroy( &qm->lru_mutex );
4826 ldap_pvt_thread_mutex_destroy( &cm->cache_mutex );
4830 #ifdef PCACHE_MONITOR
4831 pcache_monitor_db_destroy( be );
4832 #endif /* PCACHE_MONITOR */
4837 #ifdef PCACHE_CONTROL_PRIVDB
4839 Control ::= SEQUENCE {
4840 controlType LDAPOID,
4841 criticality BOOLEAN DEFAULT FALSE,
4842 controlValue OCTET STRING OPTIONAL }
4844 controlType ::= 1.3.6.1.4.1.4203.666.11.9.5.1
4846 * criticality must be TRUE; controlValue must be absent.
4854 if ( op->o_ctrlflag[ privDB_cid ] != SLAP_CONTROL_NONE ) {
4855 rs->sr_text = "privateDB control specified multiple times";
4856 return LDAP_PROTOCOL_ERROR;
4859 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
4860 rs->sr_text = "privateDB control value not absent";
4861 return LDAP_PROTOCOL_ERROR;
4864 if ( !ctrl->ldctl_iscritical ) {
4865 rs->sr_text = "privateDB control criticality required";
4866 return LDAP_PROTOCOL_ERROR;
4869 op->o_ctrlflag[ privDB_cid ] = SLAP_CONTROL_CRITICAL;
4871 return LDAP_SUCCESS;
4874 static char *extops[] = {
4875 LDAP_EXOP_MODIFY_PASSWD,
4878 #endif /* PCACHE_CONTROL_PRIVDB */
4880 static struct berval pcache_exop_MODIFY_PASSWD = BER_BVC( LDAP_EXOP_MODIFY_PASSWD );
4881 #ifdef PCACHE_EXOP_QUERY_DELETE
4882 static struct berval pcache_exop_QUERY_DELETE = BER_BVC( PCACHE_EXOP_QUERY_DELETE );
4884 #define LDAP_TAG_EXOP_QUERY_DELETE_BASE ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 0)
4885 #define LDAP_TAG_EXOP_QUERY_DELETE_DN ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 1)
4886 #define LDAP_TAG_EXOP_QUERY_DELETE_UUID ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 2)
4889 ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
4890 requestName [0] LDAPOID,
4891 requestValue [1] OCTET STRING OPTIONAL }
4893 requestName ::= 1.3.6.1.4.1.4203.666.11.9.6.1
4895 requestValue ::= SEQUENCE { CHOICE {
4897 entryDN [1] LDAPDN },
4898 queryID [2] OCTET STRING (SIZE(16))
4899 -- constrained to UUID }
4901 * Either baseDN or entryDN must be present, to allow database selection.
4903 * 1. if baseDN and queryID are present, then the query corresponding
4904 * to queryID is deleted;
4905 * 2. if baseDN is present and queryID is absent, then all queries
4907 * 3. if entryDN is present and queryID is absent, then all queries
4908 * corresponding to the queryID values present in entryDN are deleted;
4909 * 4. if entryDN and queryID are present, then all queries
4910 * corresponding to the queryID values present in entryDN are deleted,
4911 * but only if the value of queryID is contained in the entry;
4913 * Currently, only 1, 3 and 4 are implemented. 2 can be obtained by either
4914 * recursively deleting the database (ldapdelete -r) with PRIVDB control,
4915 * or by removing the database files.
4917 ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
4918 COMPONENTS OF LDAPResult,
4919 responseName [10] LDAPOID OPTIONAL,
4920 responseValue [11] OCTET STRING OPTIONAL }
4922 * responseName and responseValue must be absent.
4926 * - on success, *tagp is either LDAP_TAG_EXOP_QUERY_DELETE_BASE
4927 * or LDAP_TAG_EXOP_QUERY_DELETE_DN.
4928 * - if ndn != NULL, it is set to the normalized DN in the request
4929 * corresponding to either the baseDN or the entryDN, according
4930 * to *tagp; memory is malloc'ed on the Operation's slab, and must
4931 * be freed by the caller.
4932 * - if uuid != NULL, it is set to point to the normalized UUID;
4933 * memory is malloc'ed on the Operation's slab, and must
4934 * be freed by the caller.
4937 pcache_parse_query_delete(
4941 struct berval *uuid,
4945 int rc = LDAP_SUCCESS;
4948 BerElementBuffer berbuf;
4949 BerElement *ber = (BerElement *)&berbuf;
4950 struct berval reqdata = BER_BVNULL;
4962 if ( in == NULL || in->bv_len == 0 ) {
4963 *text = "empty request data field in queryDelete exop";
4964 return LDAP_PROTOCOL_ERROR;
4967 ber_dupbv_x( &reqdata, in, ctx );
4969 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
4970 ber_init2( ber, &reqdata, 0 );
4972 tag = ber_scanf( ber, "{" /*}*/ );
4974 if ( tag == LBER_ERROR ) {
4975 Debug( LDAP_DEBUG_TRACE,
4976 "pcache_parse_query_delete: decoding error.\n",
4978 goto decoding_error;
4981 tag = ber_peek_tag( ber, &len );
4982 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_BASE
4983 || tag == LDAP_TAG_EXOP_QUERY_DELETE_DN )
4987 if ( ndn != NULL ) {
4990 tag = ber_scanf( ber, "m", &dn );
4991 if ( tag == LBER_ERROR ) {
4992 Debug( LDAP_DEBUG_TRACE,
4993 "pcache_parse_query_delete: DN parse failed.\n",
4995 goto decoding_error;
4998 rc = dnNormalize( 0, NULL, NULL, &dn, ndn, ctx );
4999 if ( rc != LDAP_SUCCESS ) {
5000 *text = "invalid DN in queryDelete exop request data";
5005 tag = ber_scanf( ber, "x" /* "m" */ );
5006 if ( tag == LBER_DEFAULT ) {
5007 goto decoding_error;
5011 tag = ber_peek_tag( ber, &len );
5014 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_UUID ) {
5015 if ( uuid != NULL ) {
5017 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
5019 tag = ber_scanf( ber, "m", &bv );
5020 if ( tag == LBER_ERROR ) {
5021 Debug( LDAP_DEBUG_TRACE,
5022 "pcache_parse_query_delete: UUID parse failed.\n",
5024 goto decoding_error;
5027 if ( bv.bv_len != 16 ) {
5028 Debug( LDAP_DEBUG_TRACE,
5029 "pcache_parse_query_delete: invalid UUID length %lu.\n",
5030 (unsigned long)bv.bv_len, 0, 0 );
5031 goto decoding_error;
5034 rc = lutil_uuidstr_from_normalized(
5035 bv.bv_val, bv.bv_len,
5036 uuidbuf, sizeof( uuidbuf ) );
5038 goto decoding_error;
5040 ber_str2bv( uuidbuf, rc, 1, uuid );
5044 tag = ber_skip_tag( ber, &len );
5045 if ( tag == LBER_DEFAULT ) {
5046 goto decoding_error;
5050 Debug( LDAP_DEBUG_TRACE,
5051 "pcache_parse_query_delete: invalid UUID length %lu.\n",
5052 (unsigned long)len, 0, 0 );
5053 goto decoding_error;
5057 tag = ber_peek_tag( ber, &len );
5060 if ( tag != LBER_DEFAULT || len != 0 ) {
5062 Debug( LDAP_DEBUG_TRACE,
5063 "pcache_parse_query_delete: decoding error\n",
5065 rc = LDAP_PROTOCOL_ERROR;
5066 *text = "queryDelete data decoding error";
5069 if ( ndn && !BER_BVISNULL( ndn ) ) {
5070 slap_sl_free( ndn->bv_val, ctx );
5074 if ( uuid && !BER_BVISNULL( uuid ) ) {
5075 slap_sl_free( uuid->bv_val, ctx );
5080 if ( !BER_BVISNULL( &reqdata ) ) {
5081 ber_memfree_x( reqdata.bv_val, ctx );
5088 pcache_exop_query_delete(
5092 BackendDB *bd = op->o_bd;
5094 struct berval uuid = BER_BVNULL,
5096 char buf[ SLAP_TEXT_BUFLEN ];
5098 ber_tag_t tag = LBER_DEFAULT;
5100 if ( LogTest( LDAP_DEBUG_STATS ) ) {
5104 rs->sr_err = pcache_parse_query_delete( op->ore_reqdata,
5105 &tag, &op->o_req_ndn, uuidp,
5106 &rs->sr_text, op->o_tmpmemctx );
5107 if ( rs->sr_err != LDAP_SUCCESS ) {
5111 if ( LogTest( LDAP_DEBUG_STATS ) ) {
5112 assert( !BER_BVISNULL( &op->o_req_ndn ) );
5113 len = snprintf( buf, sizeof( buf ), " dn=\"%s\"", op->o_req_ndn.bv_val );
5115 if ( !BER_BVISNULL( &uuid ) && len < sizeof( buf ) ) {
5116 snprintf( &buf[ len ], sizeof( buf ) - len, " pcacheQueryId=\"%s\"", uuid.bv_val );
5119 Debug( LDAP_DEBUG_STATS, "%s QUERY DELETE%s\n",
5120 op->o_log_prefix, buf, 0 );
5122 op->o_req_dn = op->o_req_ndn;
5124 op->o_bd = select_backend( &op->o_req_ndn, 0 );
5125 if ( op->o_bd == NULL ) {
5126 send_ldap_error( op, rs, LDAP_NO_SUCH_OBJECT,
5127 "no global superior knowledge" );
5129 rs->sr_err = backend_check_restrictions( op, rs,
5130 (struct berval *)&pcache_exop_QUERY_DELETE );
5131 if ( rs->sr_err != LDAP_SUCCESS ) {
5135 if ( op->o_bd->be_extended == NULL ) {
5136 send_ldap_error( op, rs, LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
5137 "backend does not support extended operations" );
5141 op->o_bd->be_extended( op, rs );
5144 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
5145 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
5146 BER_BVZERO( &op->o_req_ndn );
5147 BER_BVZERO( &op->o_req_dn );
5150 if ( !BER_BVISNULL( &uuid ) ) {
5151 op->o_tmpfree( uuid.bv_val, op->o_tmpmemctx );
5158 #endif /* PCACHE_EXOP_QUERY_DELETE */
5161 pcache_op_extended( Operation *op, SlapReply *rs )
5163 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
5164 cache_manager *cm = on->on_bi.bi_private;
5166 #ifdef PCACHE_CONTROL_PRIVDB
5167 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL ) {
5168 return pcache_op_privdb( op, rs );
5170 #endif /* PCACHE_CONTROL_PRIVDB */
5172 #ifdef PCACHE_EXOP_QUERY_DELETE
5173 if ( bvmatch( &op->ore_reqoid, &pcache_exop_QUERY_DELETE ) ) {
5174 struct berval uuid = BER_BVNULL;
5175 ber_tag_t tag = LBER_DEFAULT;
5177 rs->sr_err = pcache_parse_query_delete( op->ore_reqdata,
5178 &tag, NULL, &uuid, &rs->sr_text, op->o_tmpmemctx );
5179 assert( rs->sr_err == LDAP_SUCCESS );
5181 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_DN ) {
5182 /* remove all queries related to the selected entry */
5183 rs->sr_err = pcache_remove_entry_queries_from_cache( op,
5184 cm, &op->o_req_ndn, &uuid );
5186 } else if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_BASE ) {
5187 if ( !BER_BVISNULL( &uuid ) ) {
5188 /* remove the selected query */
5189 rs->sr_err = pcache_remove_query_from_cache( op,
5193 /* TODO: remove all queries */
5194 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5195 rs->sr_text = "deletion of all queries not implemented";
5199 op->o_tmpfree( uuid.bv_val, op->o_tmpmemctx );
5202 #endif /* PCACHE_EXOP_QUERY_DELETE */
5204 /* We only care if we're configured for Bind caching */
5205 if ( bvmatch( &op->ore_reqoid, &pcache_exop_MODIFY_PASSWD ) &&
5207 /* See if the local entry exists and has a password.
5208 * It's too much work to find the matching query, so
5209 * we just see if there's a hashed password to update.
5211 Operation op2 = *op;
5217 op2.o_dn = op->o_bd->be_rootdn;
5218 op2.o_ndn = op->o_bd->be_rootndn;
5219 rc = be_entry_get_rw( &op2, &op->o_req_ndn, NULL,
5220 slap_schema.si_ad_userPassword, 0, &e );
5221 if ( rc == LDAP_SUCCESS && e ) {
5222 /* See if a recognized password is hashed here */
5223 Attribute *a = attr_find( e->e_attrs,
5224 slap_schema.si_ad_userPassword );
5225 if ( a && a->a_vals[0].bv_val[0] == '{' &&
5226 lutil_passwd_scheme( a->a_vals[0].bv_val )) {
5229 be_entry_release_r( &op2, e );
5233 rc = overlay_op_walk( op, rs, op_extended, on->on_info,
5235 if ( rc == LDAP_SUCCESS ) {
5236 req_pwdexop_s *qpw = &op->oq_pwdexop;
5238 /* We don't care if it succeeds or not */
5239 pc_setpw( &op2, &qpw->rs_new, cm );
5244 return SLAP_CB_CONTINUE;
5248 pcache_entry_release( Operation *op, Entry *e, int rw )
5250 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
5251 cache_manager *cm = on->on_bi.bi_private;
5252 BackendDB *db = op->o_bd;
5256 rc = be_entry_release_rw( op, e, rw );
5261 #ifdef PCACHE_MONITOR
5264 pcache_monitor_update(
5270 cache_manager *cm = (cache_manager *) priv;
5271 query_manager *qm = cm->qm;
5274 BerVarray vals = NULL;
5276 attr_delete( &e->e_attrs, ad_cachedQueryURL );
5277 if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || ad_inlist( ad_cachedQueryURL, rs->sr_attrs ) )
5278 && qm->templates != NULL )
5282 for ( tm = qm->templates; tm != NULL; tm = tm->qmnext ) {
5283 for ( qc = tm->query; qc; qc = qc->next ) {
5286 if ( query2url( op, qc, &bv, 1 ) == 0 ) {
5287 ber_bvarray_add_x( &vals, &bv, op->o_tmpmemctx );
5293 if ( vals != NULL ) {
5294 attr_merge_normalize( e, ad_cachedQueryURL, vals, NULL );
5295 ber_bvarray_free_x( vals, op->o_tmpmemctx );
5301 char buf[ SLAP_TEXT_BUFLEN ];
5304 /* number of cached queries */
5305 a = attr_find( e->e_attrs, ad_numQueries );
5306 assert( a != NULL );
5309 bv.bv_len = snprintf( buf, sizeof( buf ), "%lu", cm->num_cached_queries );
5311 if ( a->a_nvals != a->a_vals ) {
5312 ber_bvreplace( &a->a_nvals[ 0 ], &bv );
5314 ber_bvreplace( &a->a_vals[ 0 ], &bv );
5316 /* number of cached entries */
5317 a = attr_find( e->e_attrs, ad_numEntries );
5318 assert( a != NULL );
5321 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", cm->cur_entries );
5323 if ( a->a_nvals != a->a_vals ) {
5324 ber_bvreplace( &a->a_nvals[ 0 ], &bv );
5326 ber_bvreplace( &a->a_vals[ 0 ], &bv );
5329 return SLAP_CB_CONTINUE;
5333 pcache_monitor_free(
5337 struct berval values[ 2 ];
5338 Modification mod = { 0 };
5341 char textbuf[ SLAP_TEXT_BUFLEN ];
5345 /* NOTE: if slap_shutdown != 0, priv might have already been freed */
5348 /* Remove objectClass */
5349 mod.sm_op = LDAP_MOD_DELETE;
5350 mod.sm_desc = slap_schema.si_ad_objectClass;
5351 mod.sm_values = values;
5353 values[ 0 ] = oc_olmPCache->soc_cname;
5354 BER_BVZERO( &values[ 1 ] );
5356 rc = modify_delete_values( e, &mod, 1, &text,
5357 textbuf, sizeof( textbuf ) );
5358 /* don't care too much about return code... */
5361 mod.sm_values = NULL;
5362 mod.sm_desc = ad_cachedQueryURL;
5364 rc = modify_delete_values( e, &mod, 1, &text,
5365 textbuf, sizeof( textbuf ) );
5366 /* don't care too much about return code... */
5369 mod.sm_values = NULL;
5370 mod.sm_desc = ad_numQueries;
5372 rc = modify_delete_values( e, &mod, 1, &text,
5373 textbuf, sizeof( textbuf ) );
5374 /* don't care too much about return code... */
5377 mod.sm_values = NULL;
5378 mod.sm_desc = ad_numEntries;
5380 rc = modify_delete_values( e, &mod, 1, &text,
5381 textbuf, sizeof( textbuf ) );
5382 /* don't care too much about return code... */
5384 return SLAP_CB_CONTINUE;
5388 * call from within pcache_initialize()
5391 pcache_monitor_initialize( void )
5393 static int pcache_monitor_initialized = 0;
5395 if ( backend_info( "monitor" ) == NULL ) {
5399 if ( pcache_monitor_initialized++ ) {
5407 pcache_monitor_db_init( BackendDB *be )
5409 if ( pcache_monitor_initialize() == LDAP_SUCCESS ) {
5410 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING;
5417 pcache_monitor_db_open( BackendDB *be )
5419 slap_overinst *on = (slap_overinst *)be->bd_info;
5420 cache_manager *cm = on->on_bi.bi_private;
5421 Attribute *a, *next;
5422 monitor_callback_t *cb = NULL;
5425 monitor_extra_t *mbe;
5426 struct berval dummy = BER_BVC( "" );
5428 if ( !SLAP_DBMONITORING( be ) ) {
5432 mi = backend_info( "monitor" );
5433 if ( !mi || !mi->bi_extra ) {
5434 SLAP_DBFLAGS( be ) ^= SLAP_DBFLAG_MONITORING;
5439 /* don't bother if monitor is not configured */
5440 if ( !mbe->is_configured() ) {
5441 static int warning = 0;
5443 if ( warning++ == 0 ) {
5444 Debug( LDAP_DEBUG_ANY, "pcache_monitor_db_open: "
5445 "monitoring disabled; "
5446 "configure monitor database to enable\n",
5453 /* alloc as many as required (plus 1 for objectClass) */
5454 a = attrs_alloc( 1 + 2 );
5460 a->a_desc = slap_schema.si_ad_objectClass;
5461 attr_valadd( a, &oc_olmPCache->soc_cname, NULL, 1 );
5465 struct berval bv = BER_BVC( "0" );
5467 next->a_desc = ad_numQueries;
5468 attr_valadd( next, &bv, NULL, 1 );
5469 next = next->a_next;
5471 next->a_desc = ad_numEntries;
5472 attr_valadd( next, &bv, NULL, 1 );
5473 next = next->a_next;
5476 cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
5477 cb->mc_update = pcache_monitor_update;
5478 cb->mc_free = pcache_monitor_free;
5479 cb->mc_private = (void *)cm;
5481 /* make sure the database is registered; then add monitor attributes */
5482 BER_BVZERO( &cm->monitor_ndn );
5483 rc = mbe->register_overlay( be, on, &cm->monitor_ndn );
5485 rc = mbe->register_entry_attrs( &cm->monitor_ndn, a, cb,
5486 &dummy, -1, &dummy);
5502 /* store for cleanup */
5503 cm->monitor_cb = (void *)cb;
5505 /* we don't need to keep track of the attributes, because
5506 * bdb_monitor_free() takes care of everything */
5515 pcache_monitor_db_close( BackendDB *be )
5517 slap_overinst *on = (slap_overinst *)be->bd_info;
5518 cache_manager *cm = on->on_bi.bi_private;
5520 if ( cm->monitor_cb != NULL ) {
5521 BackendInfo *mi = backend_info( "monitor" );
5522 monitor_extra_t *mbe;
5524 if ( mi && &mi->bi_extra ) {
5526 mbe->unregister_entry_callback( &cm->monitor_ndn,
5527 (monitor_callback_t *)cm->monitor_cb,
5536 pcache_monitor_db_destroy( BackendDB *be )
5541 #endif /* PCACHE_MONITOR */
5543 static slap_overinst pcache;
5545 static char *obsolete_names[] = {
5550 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
5552 #endif /* SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC */
5557 struct berval debugbv = BER_BVC("pcache");
5561 code = slap_loglevel_get( &debugbv, &pcache_debug );
5566 #ifdef PCACHE_CONTROL_PRIVDB
5567 code = register_supported_control( PCACHE_CONTROL_PRIVDB,
5568 SLAP_CTRL_BIND|SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE, extops,
5569 parse_privdb_ctrl, &privDB_cid );
5570 if ( code != LDAP_SUCCESS ) {
5571 Debug( LDAP_DEBUG_ANY,
5572 "pcache_initialize: failed to register control %s (%d)\n",
5573 PCACHE_CONTROL_PRIVDB, code, 0 );
5576 #endif /* PCACHE_CONTROL_PRIVDB */
5578 #ifdef PCACHE_EXOP_QUERY_DELETE
5579 code = load_extop2( (struct berval *)&pcache_exop_QUERY_DELETE,
5580 SLAP_EXOP_WRITES|SLAP_EXOP_HIDE, pcache_exop_query_delete,
5582 if ( code != LDAP_SUCCESS ) {
5583 Debug( LDAP_DEBUG_ANY,
5584 "pcache_initialize: unable to register queryDelete exop: %d.\n",
5588 #endif /* PCACHE_EXOP_QUERY_DELETE */
5590 argv[ 0 ] = "back-bdb/back-hdb monitor";
5595 for ( i = 0; s_oid[ i ].name; i++ ) {
5597 argv[ 1 ] = s_oid[ i ].name;
5598 argv[ 2 ] = s_oid[ i ].oid;
5600 if ( parse_oidm( &c, 0, NULL ) != 0 ) {
5601 Debug( LDAP_DEBUG_ANY, "pcache_initialize: "
5602 "unable to add objectIdentifier \"%s=%s\"\n",
5603 s_oid[ i ].name, s_oid[ i ].oid, 0 );
5608 for ( i = 0; s_ad[i].desc != NULL; i++ ) {
5609 code = register_at( s_ad[i].desc, s_ad[i].adp, 0 );
5611 Debug( LDAP_DEBUG_ANY,
5612 "pcache_initialize: register_at #%d failed\n", i, 0, 0 );
5615 (*s_ad[i].adp)->ad_type->sat_flags |= SLAP_AT_HIDE;
5618 for ( i = 0; s_oc[i].desc != NULL; i++ ) {
5619 code = register_oc( s_oc[i].desc, s_oc[i].ocp, 0 );
5621 Debug( LDAP_DEBUG_ANY,
5622 "pcache_initialize: register_oc #%d failed\n", i, 0, 0 );
5625 (*s_oc[i].ocp)->soc_flags |= SLAP_OC_HIDE;
5628 pcache.on_bi.bi_type = "pcache";
5629 pcache.on_bi.bi_obsolete_names = obsolete_names;
5630 pcache.on_bi.bi_db_init = pcache_db_init;
5631 pcache.on_bi.bi_db_config = pcache_db_config;
5632 pcache.on_bi.bi_db_open = pcache_db_open;
5633 pcache.on_bi.bi_db_close = pcache_db_close;
5634 pcache.on_bi.bi_db_destroy = pcache_db_destroy;
5636 pcache.on_bi.bi_op_search = pcache_op_search;
5637 pcache.on_bi.bi_op_bind = pcache_op_bind;
5638 #ifdef PCACHE_CONTROL_PRIVDB
5639 pcache.on_bi.bi_op_compare = pcache_op_privdb;
5640 pcache.on_bi.bi_op_modrdn = pcache_op_privdb;
5641 pcache.on_bi.bi_op_modify = pcache_op_privdb;
5642 pcache.on_bi.bi_op_add = pcache_op_privdb;
5643 pcache.on_bi.bi_op_delete = pcache_op_privdb;
5644 #endif /* PCACHE_CONTROL_PRIVDB */
5645 pcache.on_bi.bi_extended = pcache_op_extended;
5647 pcache.on_bi.bi_entry_release_rw = pcache_entry_release;
5648 pcache.on_bi.bi_chk_controls = pcache_chk_controls;
5650 pcache.on_bi.bi_cf_ocs = pcocs;
5652 code = config_register_schema( pccfg, pcocs );
5653 if ( code ) return code;
5655 return overlay_register( &pcache );
5658 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
5659 int init_module(int argc, char *argv[]) {
5660 return pcache_initialize();
5664 #endif /* defined(SLAPD_OVER_PROXYCACHE) */