2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2003-2009 The OpenLDAP Foundation.
5 * Portions Copyright 2003 IBM Corporation.
6 * Portions Copyright 2003-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,
350 struct berval *query_uuid );
353 * Turn a cached query into its URL representation
356 query2url( Operation *op, CachedQuery *q, struct berval *urlbv, int dolock )
358 struct berval bv_scope,
360 char attrset_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
361 expiry_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
362 refresh_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
363 answerable_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
365 ber_len_t attrset_len,
371 ldap_pvt_thread_rdwr_rlock( &q->rwlock );
374 ldap_pvt_scope2bv( q->scope, &bv_scope );
375 filter2bv_x( op, q->filter, &bv_filter );
376 attrset_len = sprintf( attrset_buf,
377 "%lu", (unsigned long)q->qtemp->attr_set_index );
378 expiry_len = sprintf( expiry_buf,
379 "%lu", (unsigned long)q->expiry_time );
380 answerable_len = snprintf( answerable_buf, sizeof( answerable_buf ),
381 "%lu", q->answerable_cnt );
382 if ( q->refresh_time )
383 refresh_len = sprintf( refresh_buf,
384 "%lu", (unsigned long)q->refresh_time );
388 urlbv->bv_len = STRLENOF( "ldap:///" )
389 + q->qbase->base.bv_len
394 + STRLENOF( "?x-uuid=" )
396 + STRLENOF( ",x-attrset=" )
398 + STRLENOF( ",x-expiry=" )
400 + STRLENOF( ",x-answerable=" )
403 urlbv->bv_len += STRLENOF( ",x-refresh=" )
406 ptr = urlbv->bv_val = ber_memalloc_x( urlbv->bv_len + 1, op->o_tmpmemctx );
407 ptr = lutil_strcopy( ptr, "ldap:///" );
408 ptr = lutil_strcopy( ptr, q->qbase->base.bv_val );
409 ptr = lutil_strcopy( ptr, "??" );
410 ptr = lutil_strcopy( ptr, bv_scope.bv_val );
411 ptr = lutil_strcopy( ptr, "?" );
412 ptr = lutil_strcopy( ptr, bv_filter.bv_val );
413 ptr = lutil_strcopy( ptr, "?x-uuid=" );
414 ptr = lutil_strcopy( ptr, q->q_uuid.bv_val );
415 ptr = lutil_strcopy( ptr, ",x-attrset=" );
416 ptr = lutil_strcopy( ptr, attrset_buf );
417 ptr = lutil_strcopy( ptr, ",x-expiry=" );
418 ptr = lutil_strcopy( ptr, expiry_buf );
419 ptr = lutil_strcopy( ptr, ",x-answerable=" );
420 ptr = lutil_strcopy( ptr, answerable_buf );
422 ptr = lutil_strcopy( ptr, ",x-refresh=" );
423 ptr = lutil_strcopy( ptr, refresh_buf );
426 ber_memfree_x( bv_filter.bv_val, op->o_tmpmemctx );
429 ldap_pvt_thread_rdwr_runlock( &q->rwlock );
435 /* Find and record the empty filter clauses */
438 ftemp_attrs( struct berval *ftemp, struct berval *template,
439 AttributeDescription ***ret, const char **text )
445 AttributeDescription *ad;
446 AttributeDescription **descs = NULL;
449 temp2 = ch_malloc( ftemp->bv_len );
456 while ( *p1 == '(' || *p1 == '&' || *p1 == '|' || *p1 == ')' )
459 p2 = strchr( p1, '=' );
463 AC_MEMCPY( t1, p1, i );
467 if ( p2[-1] == '<' || p2[-1] == '>' ) p2--;
471 i = slap_bv2ad( &bv, &ad, text );
476 if ( *p2 == '<' || *p2 == '>' ) p2++;
477 if ( p2[1] != ')' ) {
479 while ( *p2 != ')' ) p2++;
484 descs = (AttributeDescription **)ch_realloc(descs,
485 (attr_cnt + 2)*sizeof(AttributeDescription *));
487 descs[attr_cnt++] = ad;
492 descs[attr_cnt] = NULL;
494 template->bv_val = temp2;
495 template->bv_len = t1 - temp2;
500 template_attrs( char *template, struct attr_set *set, AttributeName **ret,
511 AttributeDescription *ad;
512 AttributeName *attrs;
518 attrs = ch_calloc( set->count + 1, sizeof(AttributeName) );
519 for ( i=0; i < set->count; i++ )
520 attrs[i] = set->attrs[i];
522 alluser = an_find( attrs, slap_bv_all_user_attrs );
523 allop = an_find( attrs, slap_bv_all_operational_attrs );
526 while ( *p1 == '(' || *p1 == '&' || *p1 == '|' || *p1 == ')' ) p1++;
527 p2 = strchr( p1, '=' );
530 if ( p2[-1] == '<' || p2[-1] == '>' ) p2--;
534 i = slap_bv2ad( &bv, &ad, text );
541 if ( ad == slap_schema.si_ad_objectClass )
544 if ( is_at_operational(ad->ad_type)) {
548 } else if ( alluser ) {
551 if ( !ad_inlist( ad, attrs )) {
552 attrs = (AttributeName *)ch_realloc(attrs,
553 (attr_cnt + 2)*sizeof(AttributeName));
555 attrs[attr_cnt].an_desc = ad;
556 attrs[attr_cnt].an_name = ad->ad_cname;
557 attrs[attr_cnt].an_oc = NULL;
558 attrs[attr_cnt].an_flags = 0;
559 BER_BVZERO( &attrs[attr_cnt+1].an_name );
567 *text = "couldn't parse template";
570 if ( !got_oc && !( set->flags & PC_GOT_OC )) {
571 attrs = (AttributeName *)ch_realloc(attrs,
572 (attr_cnt + 2)*sizeof(AttributeName));
574 ad = slap_schema.si_ad_objectClass;
575 attrs[attr_cnt].an_desc = ad;
576 attrs[attr_cnt].an_name = ad->ad_cname;
577 attrs[attr_cnt].an_oc = NULL;
578 attrs[attr_cnt].an_flags = 0;
579 BER_BVZERO( &attrs[attr_cnt+1].an_name );
587 * Turn an URL representing a formerly cached query into a cached query,
588 * and try to cache it
599 LDAPURLDesc *lud = NULL;
601 tempstr = BER_BVNULL,
606 unsigned long answerable_cnt;
609 #define GOT_UUID 0x1U
610 #define GOT_ATTRSET 0x2U
611 #define GOT_EXPIRY 0x4U
612 #define GOT_ANSWERABLE 0x8U
613 #define GOT_REFRESH 0x10U
614 #define GOT_ALL (GOT_UUID|GOT_ATTRSET|GOT_EXPIRY|GOT_ANSWERABLE)
617 rc = ldap_url_parse( url, &lud );
618 if ( rc != LDAP_URL_SUCCESS ) {
622 /* non-allowed fields */
623 if ( lud->lud_host != NULL ) {
628 if ( lud->lud_attrs != NULL ) {
634 if ( strcmp( lud->lud_scheme, "ldap" ) != 0 ) {
639 /* required fields */
640 if ( lud->lud_dn == NULL || lud->lud_dn[ 0 ] == '\0' ) {
645 switch ( lud->lud_scope ) {
646 case LDAP_SCOPE_BASE:
647 case LDAP_SCOPE_ONELEVEL:
648 case LDAP_SCOPE_SUBTREE:
649 case LDAP_SCOPE_SUBORDINATE:
657 if ( lud->lud_filter == NULL || lud->lud_filter[ 0 ] == '\0' ) {
662 if ( lud->lud_exts == NULL ) {
667 for ( i = 0; lud->lud_exts[ i ] != NULL; i++ ) {
668 if ( strncmp( lud->lud_exts[ i ], "x-uuid=", STRLENOF( "x-uuid=" ) ) == 0 ) {
669 struct berval tmpUUID;
670 Syntax *syn_UUID = slap_schema.si_ad_entryUUID->ad_type->sat_syntax;
672 if ( got & GOT_UUID ) {
677 ber_str2bv( &lud->lud_exts[ i ][ STRLENOF( "x-uuid=" ) ], 0, 0, &tmpUUID );
678 rc = syn_UUID->ssyn_pretty( syn_UUID, &tmpUUID, &uuid, NULL );
679 if ( rc != LDAP_SUCCESS ) {
684 } else if ( strncmp( lud->lud_exts[ i ], "x-attrset=", STRLENOF( "x-attrset=" ) ) == 0 ) {
685 if ( got & GOT_ATTRSET ) {
690 rc = lutil_atoi( &attrset, &lud->lud_exts[ i ][ STRLENOF( "x-attrset=" ) ] );
696 } else if ( strncmp( lud->lud_exts[ i ], "x-expiry=", STRLENOF( "x-expiry=" ) ) == 0 ) {
699 if ( got & GOT_EXPIRY ) {
704 rc = lutil_atoul( &l, &lud->lud_exts[ i ][ STRLENOF( "x-expiry=" ) ] );
708 expiry_time = (time_t)l;
711 } else if ( strncmp( lud->lud_exts[ i ], "x-answerable=", STRLENOF( "x-answerable=" ) ) == 0 ) {
712 if ( got & GOT_ANSWERABLE ) {
717 rc = lutil_atoul( &answerable_cnt, &lud->lud_exts[ i ][ STRLENOF( "x-answerable=" ) ] );
721 got |= GOT_ANSWERABLE;
723 } else if ( strncmp( lud->lud_exts[ i ], "x-refresh=", STRLENOF( "x-refresh=" ) ) == 0 ) {
726 if ( got & GOT_REFRESH ) {
731 rc = lutil_atoul( &l, &lud->lud_exts[ i ][ STRLENOF( "x-refresh=" ) ] );
735 refresh_time = (time_t)l;
744 if ( got != GOT_ALL ) {
749 if ( !(got & GOT_REFRESH ))
752 /* ignore expired queries */
753 if ( expiry_time <= slap_get_time()) {
755 SlapReply rs2 = { 0 };
757 memset( &op2.oq_search, 0, sizeof( op2.oq_search ) );
759 (void)remove_query_data( &op2, &rs2, &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 slap_entry2mods( e, &modlist, &text, textbuf, textlen );
869 modlist->sml_op = LDAP_MOD_ADD;
870 op->o_tag = LDAP_REQ_MODIFY;
871 op->orm_modlist = modlist;
872 op->o_managedsait = SLAP_CONTROL_CRITICAL;
873 op->o_bd->be_modify( op, &sreply );
874 slap_mods_free( modlist, 1 );
875 } else if ( rc == LDAP_REFERRAL ||
876 rc == LDAP_NO_SUCH_OBJECT ) {
877 syncrepl_add_glue( op, e );
886 if ( op->ora_e == e )
894 /* Length-ordered sort on normalized DNs */
895 static int pcache_dn_cmp( const void *v1, const void *v2 )
897 const Qbase *q1 = v1, *q2 = v2;
899 int rc = q1->base.bv_len - q2->base.bv_len;
901 rc = strncmp( q1->base.bv_val, q2->base.bv_val, q1->base.bv_len );
905 static int lex_bvcmp( struct berval *bv1, struct berval *bv2 )
908 dif = bv1->bv_len - bv2->bv_len;
910 if ( dif > 0 ) len -= dif;
911 len = memcmp( bv1->bv_val, bv2->bv_val, len );
917 /* compare the current value in each filter */
918 static int pcache_filter_cmp( Filter *f1, Filter *f2 )
920 int rc, weight1, weight2;
922 switch( f1->f_choice ) {
923 case LDAP_FILTER_PRESENT:
926 case LDAP_FILTER_EQUALITY:
934 switch( f2->f_choice ) {
935 case LDAP_FILTER_PRESENT:
938 case LDAP_FILTER_EQUALITY:
946 rc = weight1 - weight2;
952 rc = lex_bvcmp( &f1->f_av_value, &f2->f_av_value );
955 if ( f1->f_choice == LDAP_FILTER_SUBSTRINGS ) {
957 if ( !BER_BVISNULL( &f1->f_sub_initial )) {
958 if ( !BER_BVISNULL( &f2->f_sub_initial )) {
959 rc = lex_bvcmp( &f1->f_sub_initial,
960 &f2->f_sub_initial );
964 } else if ( !BER_BVISNULL( &f2->f_sub_initial )) {
968 if ( f1->f_sub_any ) {
969 if ( f2->f_sub_any ) {
970 rc = lex_bvcmp( f1->f_sub_any,
975 } else if ( f2->f_sub_any ) {
979 if ( !BER_BVISNULL( &f1->f_sub_final )) {
980 if ( !BER_BVISNULL( &f2->f_sub_final )) {
981 rc = lex_bvcmp( &f1->f_sub_final,
986 } else if ( !BER_BVISNULL( &f2->f_sub_final )) {
990 rc = lex_bvcmp( &f1->f_mr_value,
1004 while ( f1->f_choice == LDAP_FILTER_AND || f1->f_choice == LDAP_FILTER_OR )
1006 while ( f2->f_choice == LDAP_FILTER_AND || f2->f_choice == LDAP_FILTER_OR )
1008 rc = pcache_filter_cmp( f1, f2 );
1016 /* compare filters in each query */
1017 static int pcache_query_cmp( const void *v1, const void *v2 )
1019 const CachedQuery *q1 = v1, *q2 =v2;
1020 return pcache_filter_cmp( q1->first, q2->first );
1023 /* add query on top of LRU list */
1025 add_query_on_top (query_manager* qm, CachedQuery* qc)
1027 CachedQuery* top = qm->lru_top;
1034 qm->lru_bottom = qc;
1038 Debug( pcache_debug, "Base of added query = %s\n",
1039 qc->qbase->base.bv_val, 0, 0 );
1042 /* remove_query from LRU list */
1045 remove_query (query_manager* qm, CachedQuery* qc)
1054 down = qc->lru_down;
1060 qm->lru_bottom = up;
1066 up->lru_down = down;
1068 qc->lru_up = qc->lru_down = NULL;
1071 /* find and remove string2 from string1
1072 * from start if position = 1,
1073 * from end if position = 3,
1074 * from anywhere if position = 2
1075 * string1 is overwritten if position = 2.
1079 find_and_remove(struct berval* ber1, struct berval* ber2, int position)
1083 if ( !ber2->bv_val )
1085 if ( !ber1->bv_val )
1088 switch( position ) {
1090 if ( ber1->bv_len >= ber2->bv_len && !memcmp( ber1->bv_val,
1091 ber2->bv_val, ber2->bv_len )) {
1093 ber1->bv_val += ber2->bv_len;
1094 ber1->bv_len -= ber2->bv_len;
1099 ber1->bv_val[ber1->bv_len] = '\0';
1100 temp = strstr( ber1->bv_val, ber2->bv_val );
1102 strcpy( temp, temp+ber2->bv_len );
1103 ber1->bv_len -= ber2->bv_len;
1109 if ( ber1->bv_len >= ber2->bv_len &&
1110 !memcmp( ber1->bv_val+ber1->bv_len-ber2->bv_len, ber2->bv_val,
1113 ber1->bv_len -= ber2->bv_len;
1121 static struct berval*
1122 merge_init_final(Operation *op, struct berval* init, struct berval* any,
1123 struct berval* final)
1125 struct berval* merged, *temp;
1126 int i, any_count, count;
1128 for (any_count=0; any && any[any_count].bv_val; any_count++)
1138 merged = (struct berval*)op->o_tmpalloc( (count+1)*sizeof(struct berval),
1143 ber_dupbv_x( temp, init, op->o_tmpmemctx );
1147 for (i=0; i<any_count; i++) {
1148 ber_dupbv_x( temp, any, op->o_tmpmemctx );
1153 ber_dupbv_x( temp, final, op->o_tmpmemctx );
1160 /* Each element in stored must be found in incoming. Incoming is overwritten.
1163 strings_containment(struct berval* stored, struct berval* incoming)
1165 struct berval* element;
1169 for ( element=stored; element->bv_val != NULL; element++ ) {
1170 for (j = k; incoming[j].bv_val != NULL; j++) {
1171 if (find_and_remove(&(incoming[j]), element, 2)) {
1188 substr_containment_substr(Operation *op, Filter* stored, Filter* incoming)
1192 struct berval init_incoming;
1193 struct berval final_incoming;
1194 struct berval *remaining_incoming = NULL;
1196 if ((!(incoming->f_sub_initial.bv_val) && (stored->f_sub_initial.bv_val))
1197 || (!(incoming->f_sub_final.bv_val) && (stored->f_sub_final.bv_val)))
1200 init_incoming = incoming->f_sub_initial;
1201 final_incoming = incoming->f_sub_final;
1203 if (find_and_remove(&init_incoming,
1204 &(stored->f_sub_initial), 1) && find_and_remove(&final_incoming,
1205 &(stored->f_sub_final), 3))
1207 if (stored->f_sub_any == NULL) {
1211 remaining_incoming = merge_init_final(op, &init_incoming,
1212 incoming->f_sub_any, &final_incoming);
1213 rc = strings_containment(stored->f_sub_any, remaining_incoming);
1214 ber_bvarray_free_x( remaining_incoming, op->o_tmpmemctx );
1221 substr_containment_equality(Operation *op, Filter* stored, Filter* incoming)
1223 struct berval incoming_val[2];
1226 incoming_val[1] = incoming->f_av_value;
1228 if (find_and_remove(incoming_val+1,
1229 &(stored->f_sub_initial), 1) && find_and_remove(incoming_val+1,
1230 &(stored->f_sub_final), 3)) {
1231 if (stored->f_sub_any == NULL){
1235 ber_dupbv_x( incoming_val, incoming_val+1, op->o_tmpmemctx );
1236 BER_BVZERO( incoming_val+1 );
1237 rc = strings_containment(stored->f_sub_any, incoming_val);
1238 op->o_tmpfree( incoming_val[0].bv_val, op->o_tmpmemctx );
1245 filter_first( Filter *f )
1247 while ( f->f_choice == LDAP_FILTER_OR || f->f_choice == LDAP_FILTER_AND )
1253 static CachedQuery *
1254 find_filter( Operation *op, Avlnode *root, Filter *inputf, Filter *first )
1258 MatchingRule* mrule = NULL;
1259 int res=0, eqpass= 0;
1262 CachedQuery cq, *qc;
1267 /* substring matches sort to the end, and we just have to
1268 * walk the entire list.
1270 if ( first->f_choice == LDAP_FILTER_SUBSTRINGS ) {
1271 ptr = tavl_end( root, 1 );
1272 dir = TAVL_DIR_LEFT;
1274 ptr = tavl_find3( root, &cq, pcache_query_cmp, &ret );
1275 dir = (first->f_choice == LDAP_FILTER_GE) ? TAVL_DIR_LEFT :
1284 /* an incoming substr query can only be satisfied by a cached
1287 if ( first->f_choice == LDAP_FILTER_SUBSTRINGS &&
1288 qc->first->f_choice != LDAP_FILTER_SUBSTRINGS )
1291 /* an incoming eq query can be satisfied by a cached eq or substr
1294 if ( first->f_choice == LDAP_FILTER_EQUALITY ) {
1295 if ( eqpass == 0 ) {
1296 if ( qc->first->f_choice != LDAP_FILTER_EQUALITY ) {
1297 nextpass: eqpass = 1;
1298 ptr = tavl_end( root, 1 );
1299 dir = TAVL_DIR_LEFT;
1303 if ( qc->first->f_choice != LDAP_FILTER_SUBSTRINGS )
1309 switch (fs->f_choice) {
1310 case LDAP_FILTER_EQUALITY:
1311 if (fi->f_choice == LDAP_FILTER_EQUALITY)
1312 mrule = fs->f_ava->aa_desc->ad_type->sat_equality;
1316 case LDAP_FILTER_GE:
1317 case LDAP_FILTER_LE:
1318 mrule = fs->f_ava->aa_desc->ad_type->sat_ordering;
1325 rc = value_match(&ret, fs->f_ava->aa_desc, mrule,
1326 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
1327 &(fi->f_ava->aa_value),
1328 &(fs->f_ava->aa_value), &text);
1329 if (rc != LDAP_SUCCESS) {
1332 if ( fi==first && fi->f_choice==LDAP_FILTER_EQUALITY && ret )
1335 switch (fs->f_choice) {
1336 case LDAP_FILTER_OR:
1337 case LDAP_FILTER_AND:
1342 case LDAP_FILTER_SUBSTRINGS:
1343 /* check if the equality query can be
1344 * answered with cached substring query */
1345 if ((fi->f_choice == LDAP_FILTER_EQUALITY)
1346 && substr_containment_equality( op,
1349 /* check if the substring query can be
1350 * answered with cached substring query */
1351 if ((fi->f_choice ==LDAP_FILTER_SUBSTRINGS
1352 ) && substr_containment_substr( op,
1358 case LDAP_FILTER_PRESENT:
1363 case LDAP_FILTER_EQUALITY:
1369 case LDAP_FILTER_GE:
1370 if (mrule && ret >= 0)
1375 case LDAP_FILTER_LE:
1376 if (mrule && ret <= 0)
1381 case LDAP_FILTER_NOT:
1387 } while((res) && (fi != NULL) && (fs != NULL));
1391 ptr = tavl_next( ptr, dir );
1396 /* check whether query is contained in any of
1397 * the cached queries in template
1399 static CachedQuery *
1400 query_containment(Operation *op, query_manager *qm,
1402 QueryTemplate *templa)
1405 int depth = 0, tscope;
1406 Qbase qbase, *qbptr = NULL;
1409 if (query->filter != NULL) {
1412 Debug( pcache_debug, "Lock QC index = %p\n",
1413 (void *) templa, 0, 0 );
1414 qbase.base = query->base;
1416 first = filter_first( query->filter );
1418 ldap_pvt_thread_rdwr_rlock(&templa->t_rwlock);
1421 qbptr = avl_find( templa->qbase, &qbase, pcache_dn_cmp );
1423 tscope = query->scope;
1424 /* Find a matching scope:
1425 * match at depth 0 OK
1428 * subord at depth > 0 OK
1429 * subtree at any depth OK
1431 * subtree or subord at any depth OK
1433 * subtree or subord at any depth OK
1435 * subord at depth > 0 OK
1436 * subtree at any depth OK
1438 for ( tscope = 0 ; tscope <= LDAP_SCOPE_CHILDREN; tscope++ ) {
1439 switch ( query->scope ) {
1440 case LDAP_SCOPE_BASE:
1441 if ( tscope == LDAP_SCOPE_BASE && depth ) continue;
1442 if ( tscope == LDAP_SCOPE_ONE && depth != 1) continue;
1443 if ( tscope == LDAP_SCOPE_CHILDREN && !depth ) continue;
1445 case LDAP_SCOPE_ONE:
1446 if ( tscope == LDAP_SCOPE_BASE )
1447 tscope = LDAP_SCOPE_ONE;
1448 if ( tscope == LDAP_SCOPE_ONE && depth ) continue;
1449 if ( !depth ) break;
1450 if ( tscope < LDAP_SCOPE_SUBTREE )
1451 tscope = LDAP_SCOPE_SUBTREE;
1453 case LDAP_SCOPE_SUBTREE:
1454 if ( tscope < LDAP_SCOPE_SUBTREE )
1455 tscope = LDAP_SCOPE_SUBTREE;
1456 if ( tscope == LDAP_SCOPE_CHILDREN && !depth ) continue;
1458 case LDAP_SCOPE_CHILDREN:
1459 if ( tscope < LDAP_SCOPE_SUBTREE )
1460 tscope = LDAP_SCOPE_SUBTREE;
1463 if ( !qbptr->scopes[tscope] ) continue;
1466 qc = find_filter( op, qbptr->scopes[tscope],
1467 query->filter, first );
1469 if ( qc->q_sizelimit ) {
1470 ldap_pvt_thread_rdwr_runlock(&templa->t_rwlock);
1473 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1474 if (qm->lru_top != qc) {
1475 remove_query(qm, qc);
1476 add_query_on_top(qm, qc);
1478 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1483 if ( be_issuffix( op->o_bd, &qbase.base ))
1486 dnParent( &qbase.base, &pdn );
1491 Debug( pcache_debug,
1492 "Not answerable: Unlock QC index=%p\n",
1493 (void *) templa, 0, 0 );
1494 ldap_pvt_thread_rdwr_runlock(&templa->t_rwlock);
1500 free_query (CachedQuery* qc)
1502 free(qc->q_uuid.bv_val);
1503 filter_free(qc->filter);
1504 ldap_pvt_thread_mutex_destroy(&qc->answerable_cnt_mutex);
1505 ldap_pvt_thread_rdwr_destroy( &qc->rwlock );
1506 memset(qc, 0, sizeof(*qc));
1511 /* Add query to query cache, the returned Query is locked for writing */
1512 static CachedQuery *
1517 QueryTemplate *templ,
1518 pc_caching_reason_t why,
1521 CachedQuery* new_cached_query = (CachedQuery*) ch_malloc(sizeof(CachedQuery));
1525 time_t ttl = 0, ttr = 0;
1528 new_cached_query->qtemp = templ;
1529 BER_BVZERO( &new_cached_query->q_uuid );
1530 new_cached_query->q_sizelimit = 0;
1532 now = slap_get_time();
1537 ttr = now + templ->ttr;
1541 ttl = templ->negttl;
1545 ttl = templ->limitttl;
1552 new_cached_query->expiry_time = now + ttl;
1553 new_cached_query->refresh_time = ttr;
1555 new_cached_query->answerable_cnt = 0;
1556 new_cached_query->refcnt = 1;
1557 ldap_pvt_thread_mutex_init(&new_cached_query->answerable_cnt_mutex);
1559 new_cached_query->lru_up = NULL;
1560 new_cached_query->lru_down = NULL;
1561 Debug( pcache_debug, "Added query expires at %ld (%s)\n",
1562 (long) new_cached_query->expiry_time,
1563 pc_caching_reason_str[ why ], 0 );
1565 new_cached_query->scope = query->scope;
1566 new_cached_query->filter = query->filter;
1567 new_cached_query->first = first = filter_first( query->filter );
1569 ldap_pvt_thread_rdwr_init(&new_cached_query->rwlock);
1571 ldap_pvt_thread_rdwr_wlock(&new_cached_query->rwlock);
1573 qb.base = query->base;
1575 /* Adding a query */
1576 Debug( pcache_debug, "Lock AQ index = %p\n",
1577 (void *) templ, 0, 0 );
1578 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
1579 qbase = avl_find( templ->qbase, &qb, pcache_dn_cmp );
1581 qbase = ch_calloc( 1, sizeof(Qbase) + qb.base.bv_len + 1 );
1582 qbase->base.bv_len = qb.base.bv_len;
1583 qbase->base.bv_val = (char *)(qbase+1);
1584 memcpy( qbase->base.bv_val, qb.base.bv_val, qb.base.bv_len );
1585 qbase->base.bv_val[qbase->base.bv_len] = '\0';
1586 avl_insert( &templ->qbase, qbase, pcache_dn_cmp, avl_dup_error );
1588 new_cached_query->next = templ->query;
1589 new_cached_query->prev = NULL;
1590 new_cached_query->qbase = qbase;
1591 rc = tavl_insert( &qbase->scopes[query->scope], new_cached_query,
1592 pcache_query_cmp, avl_dup_error );
1595 if (templ->query == NULL)
1596 templ->query_last = new_cached_query;
1598 templ->query->prev = new_cached_query;
1599 templ->query = new_cached_query;
1600 templ->no_of_queries++;
1602 ch_free( new_cached_query );
1603 new_cached_query = find_filter( op, qbase->scopes[query->scope],
1604 query->filter, first );
1605 filter_free( query->filter );
1606 query->filter = NULL;
1608 Debug( pcache_debug, "TEMPLATE %p QUERIES++ %d\n",
1609 (void *) templ, templ->no_of_queries, 0 );
1611 Debug( pcache_debug, "Unlock AQ index = %p \n",
1612 (void *) templ, 0, 0 );
1613 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
1615 /* Adding on top of LRU list */
1617 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1618 add_query_on_top(qm, new_cached_query);
1619 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1621 return rc == 0 ? new_cached_query : NULL;
1625 remove_from_template (CachedQuery* qc, QueryTemplate* template)
1627 if (!qc->prev && !qc->next) {
1628 template->query_last = template->query = NULL;
1629 } else if (qc->prev == NULL) {
1630 qc->next->prev = NULL;
1631 template->query = qc->next;
1632 } else if (qc->next == NULL) {
1633 qc->prev->next = NULL;
1634 template->query_last = qc->prev;
1636 qc->next->prev = qc->prev;
1637 qc->prev->next = qc->next;
1639 tavl_delete( &qc->qbase->scopes[qc->scope], qc, pcache_query_cmp );
1640 qc->qbase->queries--;
1641 if ( qc->qbase->queries == 0 ) {
1642 avl_delete( &template->qbase, qc->qbase, pcache_dn_cmp );
1643 ch_free( qc->qbase );
1647 template->no_of_queries--;
1650 /* remove bottom query of LRU list from the query cache */
1652 * NOTE: slight change in functionality.
1654 * - if result->bv_val is NULL, the query at the bottom of the LRU
1656 * - otherwise, the query whose UUID is *result is removed
1657 * - if not found, result->bv_val is zeroed
1660 cache_replacement(query_manager* qm, struct berval *result)
1662 CachedQuery* bottom;
1663 QueryTemplate *temp;
1665 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1666 if ( BER_BVISNULL( result ) ) {
1667 bottom = qm->lru_bottom;
1670 Debug ( pcache_debug,
1671 "Cache replacement invoked without "
1672 "any query in LRU list\n", 0, 0, 0 );
1673 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1678 for ( bottom = qm->lru_bottom;
1680 bottom = bottom->lru_up )
1682 if ( bvmatch( result, &bottom->q_uuid ) ) {
1688 Debug ( pcache_debug,
1689 "Could not find query with uuid=\"%s\""
1690 "in LRU list\n", result->bv_val, 0, 0 );
1691 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1692 BER_BVZERO( result );
1697 temp = bottom->qtemp;
1698 remove_query(qm, bottom);
1699 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1701 *result = bottom->q_uuid;
1702 BER_BVZERO( &bottom->q_uuid );
1704 Debug( pcache_debug, "Lock CR index = %p\n", (void *) temp, 0, 0 );
1705 ldap_pvt_thread_rdwr_wlock(&temp->t_rwlock);
1706 remove_from_template(bottom, temp);
1707 Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
1708 (void *) temp, temp->no_of_queries, 0 );
1709 Debug( pcache_debug, "Unlock CR index = %p\n", (void *) temp, 0, 0 );
1710 ldap_pvt_thread_rdwr_wunlock(&temp->t_rwlock);
1715 struct query_info *next;
1727 struct query_info *qi;
1730 if ( rs->sr_type != REP_SEARCH ) return 0;
1732 attr = attr_find( rs->sr_entry->e_attrs, ad_queryId );
1733 if ( attr == NULL ) return 0;
1735 count = attr->a_numvals;
1736 assert( count > 0 );
1737 qi = op->o_tmpalloc( sizeof( struct query_info ), op->o_tmpmemctx );
1738 qi->next = op->o_callback->sc_private;
1739 op->o_callback->sc_private = qi;
1740 ber_dupbv_x( &qi->xdn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1741 qi->del = ( count == 1 );
1750 struct berval *query_uuid )
1752 struct query_info *qi, *qnext;
1753 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(pcacheQueryID=)" ) ];
1754 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
1755 Filter filter = {LDAP_FILTER_EQUALITY};
1756 SlapReply sreply = {REP_RESULT};
1757 slap_callback cb = { NULL, remove_func, NULL, NULL };
1760 sreply.sr_entry = NULL;
1761 sreply.sr_nentries = 0;
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;
1796 Debug( pcache_debug, "DELETING ENTRY TEMPLATE=%s\n",
1797 query_uuid->bv_val, 0, 0 );
1799 op->o_tag = LDAP_REQ_DELETE;
1801 if (op->o_bd->be_delete(op, &sreply) == LDAP_SUCCESS) {
1807 struct berval vals[2];
1809 vals[0] = *query_uuid;
1810 vals[1].bv_val = NULL;
1812 mod.sml_op = LDAP_MOD_DELETE;
1814 mod.sml_desc = ad_queryId;
1815 mod.sml_type = ad_queryId->ad_cname;
1816 mod.sml_values = vals;
1817 mod.sml_nvalues = NULL;
1818 mod.sml_numvals = 1;
1819 mod.sml_next = NULL;
1820 Debug( pcache_debug,
1821 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
1822 query_uuid->bv_val, 0, 0 );
1824 op->orm_modlist = &mod;
1826 op->o_bd->be_modify( op, &sreply );
1828 op->o_tmpfree( qi->xdn.bv_val, op->o_tmpmemctx );
1829 op->o_tmpfree( qi, op->o_tmpmemctx );
1836 AttributeName* attrs,
1845 struct berval *fstr )
1847 AttributeDescription *ad;
1850 switch ( f->f_choice ) {
1851 case LDAP_FILTER_EQUALITY:
1853 len = STRLENOF( "(=)" ) + ad->ad_cname.bv_len;
1854 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=)", ad->ad_cname.bv_val );
1855 assert( ret == len );
1856 fstr->bv_len += len;
1859 case LDAP_FILTER_GE:
1861 len = STRLENOF( "(>=)" ) + ad->ad_cname.bv_len;
1862 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s>=)", ad->ad_cname.bv_val);
1863 assert( ret == len );
1864 fstr->bv_len += len;
1867 case LDAP_FILTER_LE:
1869 len = STRLENOF( "(<=)" ) + ad->ad_cname.bv_len;
1870 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s<=)", ad->ad_cname.bv_val);
1871 assert( ret == len );
1872 fstr->bv_len += len;
1875 case LDAP_FILTER_APPROX:
1877 len = STRLENOF( "(~=)" ) + ad->ad_cname.bv_len;
1878 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s~=)", ad->ad_cname.bv_val);
1879 assert( ret == len );
1880 fstr->bv_len += len;
1883 case LDAP_FILTER_SUBSTRINGS:
1885 len = STRLENOF( "(=)" ) + ad->ad_cname.bv_len;
1886 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=)", ad->ad_cname.bv_val );
1887 assert( ret == len );
1888 fstr->bv_len += len;
1891 case LDAP_FILTER_PRESENT:
1893 len = STRLENOF( "(=*)" ) + ad->ad_cname.bv_len;
1894 ret = snprintf( fstr->bv_val+fstr->bv_len, len + 1, "(%s=*)", ad->ad_cname.bv_val );
1895 assert( ret == len );
1896 fstr->bv_len += len;
1899 case LDAP_FILTER_AND:
1900 case LDAP_FILTER_OR:
1901 case LDAP_FILTER_NOT: {
1903 fstr->bv_val[fstr->bv_len++] = '(';
1904 switch ( f->f_choice ) {
1905 case LDAP_FILTER_AND:
1906 fstr->bv_val[fstr->bv_len] = '&';
1908 case LDAP_FILTER_OR:
1909 fstr->bv_val[fstr->bv_len] = '|';
1911 case LDAP_FILTER_NOT:
1912 fstr->bv_val[fstr->bv_len] = '!';
1917 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1918 rc = filter2template( op, f, fstr );
1921 fstr->bv_val[fstr->bv_len++] = ')';
1922 fstr->bv_val[fstr->bv_len] = '\0';
1928 /* a filter should at least have room for "()",
1929 * an "=" and for a 1-char attr */
1930 strcpy( fstr->bv_val, "(?=)" );
1931 fstr->bv_len += STRLENOF("(?=)");
1938 #define BI_HASHED 0x01
1939 #define BI_DIDCB 0x02
1940 #define BI_LOOKUP 0x04
1944 typedef struct bindinfo {
1945 cache_manager *bi_cm;
1947 QueryTemplate *bi_templ;
1948 struct search_info *bi_si;
1950 slap_callback bi_cb;
1953 struct search_info {
1956 QueryTemplate *qtemp;
1957 AttributeName* save_attrs; /* original attributes, saved for response */
1958 int swap_saved_attrs;
1963 int slimit_exceeded;
1964 pc_caching_reason_t caching_reason;
1970 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, rs, 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 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
2036 entry_free( rs->sr_entry );
2037 rs->sr_entry = NULL;
2038 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
2045 * Call that allows to remove a set of entries from the cache,
2046 * by forcing the removal of all the related queries.
2049 pcache_remove_entries_from_cache(
2052 BerVarray entryUUIDs )
2054 Connection conn = { 0 };
2055 OperationBuffer opbuf;
2057 slap_callback sc = { 0 };
2058 SlapReply rs = { REP_RESULT };
2060 char filtbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(entryUUID=)" ) ];
2061 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2062 AttributeName attrs[ 2 ] = {{{ 0 }}};
2066 void *thrctx = ldap_pvt_thread_pool_context();
2068 connection_fake_init( &conn, &opbuf, thrctx );
2076 memset( &op->oq_search, 0, sizeof( op->oq_search ) );
2077 op->ors_scope = LDAP_SCOPE_SUBTREE;
2078 op->ors_deref = LDAP_DEREF_NEVER;
2079 f.f_choice = LDAP_FILTER_EQUALITY;
2081 ava.aa_desc = slap_schema.si_ad_entryUUID;
2082 op->ors_filter = &f;
2084 op->ors_tlimit = SLAP_NO_LIMIT;
2085 op->ors_limit = NULL;
2086 attrs[ 0 ].an_desc = ad_queryId;
2087 attrs[ 0 ].an_name = ad_queryId->ad_cname;
2088 op->ors_attrs = attrs;
2089 op->ors_attrsonly = 0;
2091 op->o_req_dn = cm->db.be_suffix[ 0 ];
2092 op->o_req_ndn = cm->db.be_nsuffix[ 0 ];
2094 op->o_tag = LDAP_REQ_SEARCH;
2095 op->o_protocol = LDAP_VERSION3;
2096 op->o_managedsait = SLAP_CONTROL_CRITICAL;
2098 op->o_dn = op->o_bd->be_rootdn;
2099 op->o_ndn = op->o_bd->be_rootndn;
2100 sc.sc_response = fetch_queryId_cb;
2101 op->o_callback = ≻
2103 for ( s = 0; !BER_BVISNULL( &entryUUIDs[ s ] ); s++ ) {
2104 BerVarray vals = NULL;
2106 op->ors_filterstr.bv_len = snprintf( filtbuf, sizeof( filtbuf ),
2107 "(entryUUID=%s)", entryUUIDs[ s ].bv_val );
2108 op->ors_filterstr.bv_val = filtbuf;
2109 ava.aa_value = entryUUIDs[ s ];
2111 rc = op->o_bd->be_search( op, &rs );
2112 if ( rc != LDAP_SUCCESS ) {
2116 vals = (BerVarray)op->o_callback->sc_private;
2117 if ( vals != NULL ) {
2120 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
2121 struct berval val = vals[ i ];
2123 remove_query_and_data( op, &rs, cm, &val );
2125 if ( !BER_BVISNULL( &val ) && val.bv_val != vals[ i ].bv_val ) {
2126 ch_free( val.bv_val );
2130 ber_bvarray_free_x( vals, op->o_tmpmemctx );
2131 op->o_callback->sc_private = NULL;
2139 * Call that allows to remove a query from the cache.
2142 pcache_remove_query_from_cache(
2145 struct berval *queryid )
2147 Operation op2 = *op;
2148 SlapReply rs2 = { 0 };
2152 /* remove the selected query */
2153 remove_query_and_data( &op2, &rs2, cm, queryid );
2155 return LDAP_SUCCESS;
2159 * Call that allows to remove a set of queries related to an entry
2160 * from the cache; if queryid is not null, the entry must belong to
2161 * the query indicated by queryid.
2164 pcache_remove_entry_queries_from_cache(
2168 struct berval *queryid )
2170 Connection conn = { 0 };
2171 OperationBuffer opbuf;
2173 slap_callback sc = { 0 };
2174 SlapReply rs = { REP_RESULT };
2176 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(pcacheQueryID=)" ) ];
2177 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2178 AttributeName attrs[ 2 ] = {{{ 0 }}};
2181 BerVarray vals = NULL;
2184 void *thrctx = ldap_pvt_thread_pool_context();
2186 connection_fake_init( &conn, &opbuf, thrctx );
2194 memset( &op->oq_search, 0, sizeof( op->oq_search ) );
2195 op->ors_scope = LDAP_SCOPE_BASE;
2196 op->ors_deref = LDAP_DEREF_NEVER;
2197 if ( queryid == NULL || BER_BVISNULL( queryid ) ) {
2198 BER_BVSTR( &op->ors_filterstr, "(objectClass=*)" );
2199 f.f_choice = LDAP_FILTER_PRESENT;
2200 f.f_desc = slap_schema.si_ad_objectClass;
2203 op->ors_filterstr.bv_len = snprintf( filter_str,
2204 sizeof( filter_str ), "(%s=%s)",
2205 ad_queryId->ad_cname.bv_val, queryid->bv_val );
2206 f.f_choice = LDAP_FILTER_EQUALITY;
2208 f.f_av_desc = ad_queryId;
2209 f.f_av_value = *queryid;
2211 op->ors_filter = &f;
2213 op->ors_tlimit = SLAP_NO_LIMIT;
2214 op->ors_limit = NULL;
2215 attrs[ 0 ].an_desc = ad_queryId;
2216 attrs[ 0 ].an_name = ad_queryId->ad_cname;
2217 op->ors_attrs = attrs;
2218 op->ors_attrsonly = 0;
2220 op->o_req_dn = *ndn;
2221 op->o_req_ndn = *ndn;
2223 op->o_tag = LDAP_REQ_SEARCH;
2224 op->o_protocol = LDAP_VERSION3;
2225 op->o_managedsait = SLAP_CONTROL_CRITICAL;
2227 op->o_dn = op->o_bd->be_rootdn;
2228 op->o_ndn = op->o_bd->be_rootndn;
2229 sc.sc_response = fetch_queryId_cb;
2230 op->o_callback = ≻
2232 rc = op->o_bd->be_search( op, &rs );
2233 if ( rc != LDAP_SUCCESS ) {
2237 vals = (BerVarray)op->o_callback->sc_private;
2238 if ( vals != NULL ) {
2241 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
2242 struct berval val = vals[ i ];
2244 remove_query_and_data( op, &rs, cm, &val );
2246 if ( !BER_BVISNULL( &val ) && val.bv_val != vals[ i ].bv_val ) {
2247 ch_free( val.bv_val );
2251 ber_bvarray_free_x( vals, op->o_tmpmemctx );
2254 return LDAP_SUCCESS;
2261 struct berval *query_uuid )
2263 struct search_info *si = op->o_callback->sc_private;
2264 slap_overinst *on = si->on;
2265 cache_manager *cm = on->on_bi.bi_private;
2268 struct berval crp_uuid;
2269 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
2271 Connection conn = {0};
2272 OperationBuffer opbuf;
2273 void *thrctx = ldap_pvt_thread_pool_context();
2275 query_uuid->bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf));
2276 ber_str2bv(uuidbuf, query_uuid->bv_len, 1, query_uuid);
2278 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
2279 op_tmp = &opbuf.ob_op;
2280 op_tmp->o_bd = &cm->db;
2281 op_tmp->o_dn = cm->db.be_rootdn;
2282 op_tmp->o_ndn = cm->db.be_rootndn;
2284 Debug( pcache_debug, "UUID for query being added = %s\n",
2287 for ( e=si->head; e; e=si->head ) {
2288 si->head = e->e_private;
2289 e->e_private = NULL;
2290 while ( cm->cur_entries > (cm->max_entries) ) {
2291 BER_BVZERO( &crp_uuid );
2292 remove_query_and_data( op_tmp, rs, cm, &crp_uuid );
2295 return_val = merge_entry(op_tmp, e, 0, query_uuid);
2296 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2297 cm->cur_entries += return_val;
2298 Debug( pcache_debug,
2299 "ENTRY ADDED/MERGED, CACHED ENTRIES=%d\n",
2300 cm->cur_entries, 0, 0 );
2302 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2309 pcache_op_cleanup( Operation *op, SlapReply *rs ) {
2310 slap_callback *cb = op->o_callback;
2311 struct search_info *si = cb->sc_private;
2312 slap_overinst *on = si->on;
2313 cache_manager *cm = on->on_bi.bi_private;
2314 query_manager* qm = cm->qm;
2316 if ( rs->sr_type == REP_SEARCH ) {
2319 /* don't return more entries than requested by the client */
2320 if ( si->slimit > 0 && rs->sr_nentries >= si->slimit ) {
2321 si->slimit_exceeded = 1;
2324 /* If we haven't exceeded the limit for this query,
2325 * build a chain of answers to store. If we hit the
2326 * limit, empty the chain and ignore the rest.
2329 /* check if the entry contains undefined
2330 * attributes/objectClasses (ITS#5680) */
2331 if ( cm->check_cacheability && test_filter( op, rs->sr_entry, si->query.filter ) != LDAP_COMPARE_TRUE ) {
2332 Debug( pcache_debug, "%s: query not cacheable because of schema issues in DN \"%s\"\n",
2333 op->o_log_prefix, rs->sr_entry->e_name.bv_val, 0 );
2337 /* check for malformed entries: attrs with no values */
2339 Attribute *a = rs->sr_entry->e_attrs;
2340 for (; a; a=a->a_next) {
2341 if ( !a->a_numvals ) {
2342 Debug( pcache_debug, "%s: query not cacheable because of attrs without values in DN \"%s\" (%s)\n",
2343 op->o_log_prefix, rs->sr_entry->e_name.bv_val,
2344 a->a_desc->ad_cname.bv_val );
2350 if ( si->count < si->max ) {
2352 e = entry_dup( rs->sr_entry );
2353 if ( !si->head ) si->head = e;
2354 if ( si->tail ) si->tail->e_private = e;
2361 for (;si->head; si->head=e) {
2362 e = si->head->e_private;
2363 si->head->e_private = NULL;
2364 entry_free(si->head);
2372 if ( rs->sr_type == REP_RESULT ||
2373 op->o_abandon || rs->sr_err == SLAPD_ABANDON )
2375 if ( si->swap_saved_attrs ) {
2376 rs->sr_attrs = si->save_attrs;
2377 op->ors_attrs = si->save_attrs;
2379 if ( (op->o_abandon || rs->sr_err == SLAPD_ABANDON) &&
2380 si->caching_reason == PC_IGNORE )
2382 filter_free( si->query.filter );
2384 /* duplicate query, free it */
2386 for (;si->head; si->head=e) {
2387 e = si->head->e_private;
2388 si->head->e_private = NULL;
2389 entry_free(si->head);
2392 op->o_callback = op->o_callback->sc_next;
2393 op->o_tmpfree( cb, op->o_tmpmemctx );
2394 } else if ( si->caching_reason != PC_IGNORE ) {
2395 CachedQuery *qc = qm->addfunc(op, qm, &si->query,
2396 si->qtemp, si->caching_reason, 1 );
2399 switch ( si->caching_reason ) {
2401 cache_entries( op, rs, &qc->q_uuid );
2403 si->pbi->bi_cq = qc;
2407 qc->q_sizelimit = rs->sr_nentries;
2417 ldap_pvt_thread_rdwr_wunlock(&qc->rwlock);
2418 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
2419 cm->num_cached_queries++;
2420 Debug( pcache_debug, "STORED QUERIES = %lu\n",
2421 cm->num_cached_queries, 0, 0 );
2422 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
2424 /* If the consistency checker suspended itself,
2427 if ( cm->cc_paused == PCACHE_CC_PAUSED ) {
2428 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2429 if ( cm->cc_paused == PCACHE_CC_PAUSED ) {
2431 ldap_pvt_runqueue_resched( &slapd_rq, cm->cc_arg, 0 );
2433 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2436 } else if ( si->count ) {
2437 /* duplicate query, free it */
2439 for (;si->head; si->head=e) {
2440 e = si->head->e_private;
2441 si->head->e_private = NULL;
2442 entry_free(si->head);
2447 filter_free( si->query.filter );
2451 return SLAP_CB_CONTINUE;
2459 struct search_info *si = op->o_callback->sc_private;
2461 if ( si->swap_saved_attrs ) {
2462 rs->sr_attrs = si->save_attrs;
2463 op->ors_attrs = si->save_attrs;
2466 if ( rs->sr_type == REP_SEARCH ) {
2467 /* don't return more entries than requested by the client */
2468 if ( si->slimit_exceeded ) {
2472 } else if ( rs->sr_type == REP_RESULT ) {
2475 if ( rs->sr_err == LDAP_SUCCESS ) {
2476 si->caching_reason = PC_POSITIVE;
2478 } else if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED
2479 && si->qtemp->limitttl )
2481 si->caching_reason = PC_SIZELIMIT;
2483 for (;si->head; si->head=e) {
2484 e = si->head->e_private;
2485 si->head->e_private = NULL;
2486 entry_free(si->head);
2490 } else if ( si->qtemp->negttl && !si->count && !si->over &&
2491 rs->sr_err == LDAP_SUCCESS )
2493 si->caching_reason = PC_NEGATIVE;
2497 if ( si->slimit_exceeded ) {
2498 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
2502 return SLAP_CB_CONTINUE;
2505 /* NOTE: this is a quick workaround to let pcache minimally interact
2506 * with pagedResults. A more articulated solutions would be to
2507 * perform the remote query without control and cache all results,
2508 * performing the pagedResults search only within the client
2509 * and the proxy. This requires pcache to understand pagedResults. */
2511 pcache_chk_controls(
2515 const char *non = "";
2516 const char *stripped = "";
2518 switch( op->o_pagedresults ) {
2519 case SLAP_CONTROL_NONCRITICAL:
2521 stripped = "; stripped";
2524 case SLAP_CONTROL_CRITICAL:
2525 Debug( pcache_debug, "%s: "
2526 "%scritical pagedResults control "
2527 "disabled with proxy cache%s.\n",
2528 op->o_log_prefix, non, stripped );
2530 slap_remove_control( op, rs, slap_cids.sc_pagedResults, NULL );
2534 rs->sr_err = SLAP_CB_CONTINUE;
2542 pc_setpw( Operation *op, struct berval *pwd, cache_manager *cm )
2544 struct berval vals[2];
2547 const char *text = NULL;
2548 BER_BVZERO( &vals[0] );
2549 slap_passwd_hash( pwd, &vals[0], &text );
2550 if ( BER_BVISEMPTY( &vals[0] )) {
2551 Debug( pcache_debug, "pc_setpw: hash failed %s\n",
2557 BER_BVZERO( &vals[1] );
2561 SlapReply sr = { REP_RESULT };
2562 slap_callback cb = { 0, slap_null_cb, 0, 0 };
2565 mod.sml_op = LDAP_MOD_REPLACE;
2567 mod.sml_desc = slap_schema.si_ad_userPassword;
2568 mod.sml_type = mod.sml_desc->ad_cname;
2569 mod.sml_values = vals;
2570 mod.sml_nvalues = NULL;
2571 mod.sml_numvals = 1;
2572 mod.sml_next = NULL;
2574 op->o_tag = LDAP_REQ_MODIFY;
2575 op->orm_modlist = &mod;
2577 op->o_dn = op->o_bd->be_rootdn;
2578 op->o_ndn = op->o_bd->be_rootndn;
2579 op->o_callback = &cb;
2580 Debug( pcache_debug, "pc_setpw: CACHING BIND for %s\n",
2581 op->o_req_dn.bv_val, 0, 0 );
2582 rc = op->o_bd->be_modify( op, &sr );
2583 ch_free( vals[0].bv_val );
2588 typedef struct bindcacheinfo {
2594 pc_bind_save( Operation *op, SlapReply *rs )
2596 if ( rs->sr_err == LDAP_SUCCESS ) {
2597 bindcacheinfo *bci = op->o_callback->sc_private;
2598 slap_overinst *on = bci->on;
2599 cache_manager *cm = on->on_bi.bi_private;
2601 Operation op2 = *op;
2602 if ( pc_setpw( &op2, &op->orb_cred, cm ) == LDAP_SUCCESS )
2603 bci->qc->bindref_time = op->o_time + bci->qc->qtemp->bindttr;
2605 return SLAP_CB_CONTINUE;
2609 pc_bind_attrs( Operation *op, Entry *e, QueryTemplate *temp,
2610 struct berval *fbv )
2613 struct berval *vals, pres = BER_BVC("*");
2617 vals = op->o_tmpalloc( temp->bindnattrs * sizeof( struct berval ),
2620 for ( i=0; i<temp->bindnattrs; i++ ) {
2621 a = attr_find( e->e_attrs, temp->bindfattrs[i] );
2622 if ( a && a->a_vals ) {
2623 vals[i] = a->a_vals[0];
2624 len += a->a_vals[0].bv_len;
2629 fbv->bv_len = len + temp->bindftemp.bv_len;
2630 fbv->bv_val = op->o_tmpalloc( fbv->bv_len + 1, op->o_tmpmemctx );
2632 p1 = temp->bindftemp.bv_val;
2637 if ( p1[0] == '=' && p1[1] == ')' ) {
2638 AC_MEMCPY( p2, vals[i].bv_val, vals[i].bv_len );
2639 p2 += vals[i].bv_len;
2645 op->o_tmpfree( vals, op->o_tmpmemctx );
2647 /* FIXME: are we sure str2filter_x can't fail?
2648 * caller needs to check */
2650 Filter *f = str2filter_x( op, fbv->bv_val );
2651 assert( f != NULL );
2656 /* Check if the requested entry is from the cache and has a valid
2657 * ttr and password hash
2660 pc_bind_search( Operation *op, SlapReply *rs )
2662 if ( rs->sr_type == REP_SEARCH ) {
2663 bindinfo *pbi = op->o_callback->sc_private;
2665 /* We only care if this is an already cached result and we're
2666 * below the refresh time, or we're offline.
2669 if (( pbi->bi_cm->cc_paused & PCACHE_CC_OFFLINE ) ||
2670 op->o_time < pbi->bi_cq->bindref_time ) {
2673 /* See if a recognized password is hashed here */
2674 a = attr_find( rs->sr_entry->e_attrs,
2675 slap_schema.si_ad_userPassword );
2676 if ( a && a->a_vals[0].bv_val[0] == '{' &&
2677 lutil_passwd_scheme( a->a_vals[0].bv_val ))
2678 pbi->bi_flags |= BI_HASHED;
2680 Debug( pcache_debug, "pc_bind_search: cache is stale, "
2681 "reftime: %ld, current time: %ld\n",
2682 pbi->bi_cq->bindref_time, op->o_time, 0 );
2684 } else if ( pbi->bi_si ) {
2685 /* This search result is going into the cache */
2689 filter_free( pbi->bi_si->query.filter );
2690 f = pc_bind_attrs( op, rs->sr_entry, pbi->bi_templ, &fbv );
2691 op->o_tmpfree( fbv.bv_val, op->o_tmpmemctx );
2692 pbi->bi_si->query.filter = filter_dup( f, NULL );
2693 filter_free_x( op, f, 1 );
2699 /* We always want pc_bind_search to run after the search handlers */
2701 pc_bind_resp( Operation *op, SlapReply *rs )
2703 bindinfo *pbi = op->o_callback->sc_private;
2704 if ( !( pbi->bi_flags & BI_DIDCB )) {
2705 slap_callback *sc = op->o_callback;
2706 while ( sc && sc->sc_response != pcache_response )
2709 sc = op->o_callback;
2710 pbi->bi_cb.sc_next = sc->sc_next;
2711 sc->sc_next = &pbi->bi_cb;
2712 pbi->bi_flags |= BI_DIDCB;
2714 return SLAP_CB_CONTINUE;
2717 #ifdef PCACHE_CONTROL_PRIVDB
2723 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2724 cache_manager *cm = on->on_bi.bi_private;
2725 slap_callback *save_cb;
2728 /* skip if control is unset */
2729 if ( op->o_ctrlflag[ privDB_cid ] != SLAP_CONTROL_CRITICAL ) {
2730 return SLAP_CB_CONTINUE;
2733 /* The cache DB isn't open yet */
2734 if ( cm->defer_db_open ) {
2735 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
2736 "pcachePrivDB: cacheDB not available" );
2740 /* FIXME: might be a little bit exaggerated... */
2741 if ( !be_isroot( op ) ) {
2742 save_cb = op->o_callback;
2743 op->o_callback = NULL;
2744 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
2745 "pcachePrivDB: operation not allowed" );
2746 op->o_callback = save_cb;
2751 /* map tag to operation */
2752 type = slap_req2op( op->o_tag );
2753 if ( type != SLAP_OP_LAST ) {
2757 /* execute, if possible */
2758 func = &cm->db.be_bind;
2759 if ( func[ type ] != NULL ) {
2760 Operation op2 = *op;
2764 rc = func[ type ]( &op2, rs );
2765 if ( type == SLAP_OP_BIND && rc == LDAP_SUCCESS ) {
2766 op->o_conn->c_authz_cookie = cm->db.be_private;
2773 /* otherwise fall back to error */
2774 save_cb = op->o_callback;
2775 op->o_callback = NULL;
2776 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
2777 "operation not supported with pcachePrivDB control" );
2778 op->o_callback = save_cb;
2782 #endif /* PCACHE_CONTROL_PRIVDB */
2789 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2790 cache_manager *cm = on->on_bi.bi_private;
2791 QueryTemplate *temp;
2793 slap_callback cb = { 0 }, *sc;
2799 #ifdef PCACHE_CONTROL_PRIVDB
2800 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL )
2801 return pcache_op_privdb( op, rs );
2802 #endif /* PCACHE_CONTROL_PRIVDB */
2804 /* Skip if we're not configured for Binds, or cache DB isn't open yet */
2805 if ( !cm->cache_binds || cm->defer_db_open )
2806 return SLAP_CB_CONTINUE;
2808 /* First find a matching template with Bind info */
2809 for ( temp=cm->qm->templates; temp; temp=temp->qmnext ) {
2810 if ( temp->bindttr && dnIsSuffix( &op->o_req_ndn, &temp->bindbase ))
2813 /* Didn't find a suitable template, just passthru */
2815 return SLAP_CB_CONTINUE;
2817 /* See if the entry is already locally cached. If so, we can
2818 * populate the query filter to retrieve the cached query. We
2819 * need to check the bindrefresh time in the query.
2822 op2.o_dn = op->o_bd->be_rootdn;
2823 op2.o_ndn = op->o_bd->be_rootndn;
2828 rc = be_entry_get_rw( &op2, &op->o_req_ndn, NULL, NULL, 0, &e );
2829 if ( rc == LDAP_SUCCESS && e ) {
2830 bi.bi_flags |= BI_LOOKUP;
2831 op2.ors_filter = pc_bind_attrs( op, e, temp, &op2.ors_filterstr );
2832 be_entry_release_r( &op2, e );
2834 op2.ors_filter = temp->bindfilter;
2835 op2.ors_filterstr = temp->bindfilterstr;
2838 op2.o_bd = op->o_bd;
2839 op2.o_tag = LDAP_REQ_SEARCH;
2840 op2.ors_scope = LDAP_SCOPE_BASE;
2841 op2.ors_deref = LDAP_DEREF_NEVER;
2843 op2.ors_tlimit = SLAP_NO_LIMIT;
2844 op2.ors_limit = NULL;
2845 op2.ors_attrs = cm->qm->attr_sets[temp->attr_set_index].attrs;
2846 op2.ors_attrsonly = 0;
2848 /* We want to invoke search at the same level of the stack
2849 * as we're already at...
2856 bi.bi_cb.sc_response = pc_bind_search;
2857 bi.bi_cb.sc_cleanup = NULL;
2858 bi.bi_cb.sc_private = &bi;
2859 cb.sc_private = &bi;
2860 cb.sc_response = pc_bind_resp;
2861 op2.o_callback = &cb;
2862 overlay_op_walk( &op2, rs, op_search, on->on_info, on );
2864 /* OK, just bind locally */
2865 if ( bi.bi_flags & BI_HASHED ) {
2866 BackendDB *be = op->o_bd;
2869 Debug( pcache_debug, "pcache_op_bind: CACHED BIND for %s\n",
2870 op->o_req_dn.bv_val, 0, 0 );
2872 if ( op->o_bd->be_bind( op, rs ) == LDAP_SUCCESS ) {
2873 op->o_conn->c_authz_cookie = cm->db.be_private;
2879 /* We have a cached query to work with */
2881 sc = op->o_tmpalloc( sizeof(slap_callback) + sizeof(bindcacheinfo),
2883 sc->sc_response = pc_bind_save;
2884 sc->sc_cleanup = NULL;
2885 sc->sc_private = sc+1;
2886 bci = sc->sc_private;
2887 sc->sc_next = op->o_callback;
2888 op->o_callback = sc;
2892 return SLAP_CB_CONTINUE;
2900 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2901 cache_manager *cm = on->on_bi.bi_private;
2902 query_manager* qm = cm->qm;
2907 QueryTemplate *qtemp = NULL;
2908 bindinfo *pbi = NULL;
2911 CachedQuery *answerable = NULL;
2914 struct berval tempstr;
2916 #ifdef PCACHE_CONTROL_PRIVDB
2917 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL ) {
2918 return pcache_op_privdb( op, rs );
2920 #endif /* PCACHE_CONTROL_PRIVDB */
2922 /* The cache DB isn't open yet */
2923 if ( cm->defer_db_open ) {
2924 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
2925 "pcachePrivDB: cacheDB not available" );
2929 /* pickup runtime ACL changes */
2930 cm->db.be_acl = op->o_bd->be_acl;
2933 /* See if we're processing a Bind request */
2934 slap_callback *cb = op->o_callback;
2936 for ( ; cb; cb=cb->sc_next ) {
2937 if ( cb->sc_response == pc_bind_resp ) {
2938 pbi = cb->sc_private;
2944 /* FIXME: cannot cache/answer requests with pagedResults control */
2946 query.filter = op->ors_filter;
2949 query.base = pbi->bi_templ->bindbase;
2950 query.scope = pbi->bi_templ->bindscope;
2951 attr_set = pbi->bi_templ->attr_set_index;
2953 qtemp = pbi->bi_templ;
2954 if ( pbi->bi_flags & BI_LOOKUP )
2955 answerable = qm->qcfunc(op, qm, &query, qtemp);
2958 tempstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len+1,
2961 if ( filter2template( op, op->ors_filter, &tempstr ))
2963 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
2964 return SLAP_CB_CONTINUE;
2967 Debug( pcache_debug, "query template of incoming query = %s\n",
2968 tempstr.bv_val, 0, 0 );
2971 attr_set = get_attr_set(op->ors_attrs, qm, cm->numattrsets);
2973 query.base = op->o_req_ndn;
2974 query.scope = op->ors_scope;
2976 /* check for query containment */
2977 if (attr_set > -1) {
2978 QueryTemplate *qt = qm->attr_sets[attr_set].templates;
2979 for (; qt; qt = qt->qtnext ) {
2980 /* find if template i can potentially answer tempstr */
2981 if ( ber_bvstrcasecmp( &qt->querystr, &tempstr ) != 0 )
2985 Debug( pcache_debug, "Entering QC, querystr = %s\n",
2986 op->ors_filterstr.bv_val, 0, 0 );
2987 answerable = qm->qcfunc(op, qm, &query, qt);
2989 /* if != NULL, rlocks qtemp->t_rwlock */
2994 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
2998 BackendDB *save_bd = op->o_bd;
3000 ldap_pvt_thread_mutex_lock( &answerable->answerable_cnt_mutex );
3001 answerable->answerable_cnt++;
3002 /* we only care about refcnts if we're refreshing */
3003 if ( answerable->refresh_time )
3004 answerable->refcnt++;
3005 Debug( pcache_debug, "QUERY ANSWERABLE (answered %lu times)\n",
3006 answerable->answerable_cnt, 0, 0 );
3007 ldap_pvt_thread_mutex_unlock( &answerable->answerable_cnt_mutex );
3009 ldap_pvt_thread_rdwr_rlock(&answerable->rwlock);
3010 if ( BER_BVISNULL( &answerable->q_uuid )) {
3011 /* No entries cached, just an empty result set */
3013 send_ldap_result( op, rs );
3015 /* Let Bind know we used a cached query */
3017 pbi->bi_cq = answerable;
3021 if ( cm->response_cb == PCACHE_RESPONSE_CB_TAIL ) {
3022 /* The cached entry was already processed by any
3023 * other overlays, so don't let it get processed again.
3025 op->o_callback = NULL;
3028 i = cm->db.bd_info->bi_op_search( op, rs );
3030 ldap_pvt_thread_rdwr_runlock(&answerable->rwlock);
3031 /* locked by qtemp->qcfunc (query_containment) */
3032 ldap_pvt_thread_rdwr_runlock(&qtemp->t_rwlock);
3037 Debug( pcache_debug, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
3039 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
3040 if (cm->num_cached_queries >= cm->max_queries) {
3043 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
3045 if (op->ors_attrsonly)
3050 struct search_info *si;
3052 Debug( pcache_debug, "QUERY CACHEABLE\n", 0, 0, 0 );
3053 query.filter = filter_dup(op->ors_filter, NULL);
3055 cb = op->o_tmpalloc( sizeof(*cb) + sizeof(*si), op->o_tmpmemctx );
3056 cb->sc_response = pcache_response;
3057 cb->sc_cleanup = pcache_op_cleanup;
3058 cb->sc_private = (cb+1);
3059 si = cb->sc_private;
3063 si->max = cm->num_entries_limit ;
3067 si->slimit_exceeded = 0;
3068 si->caching_reason = PC_IGNORE;
3069 if ( op->ors_slimit > 0 && op->ors_slimit < cm->num_entries_limit ) {
3070 si->slimit = op->ors_slimit;
3071 op->ors_slimit = cm->num_entries_limit;
3075 si->swap_saved_attrs = 1;
3076 si->save_attrs = op->ors_attrs;
3081 op->ors_attrs = qtemp->t_attrs.attrs;
3083 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
3084 cb->sc_next = op->o_callback;
3085 op->o_callback = cb;
3088 slap_callback **pcb;
3090 /* need to move the callback at the end, in case other
3091 * overlays are present, so that the final entry is
3092 * actually cached */
3094 for ( pcb = &op->o_callback; *pcb; pcb = &(*pcb)->sc_next );
3099 Debug( pcache_debug, "QUERY NOT CACHEABLE\n",
3103 return SLAP_CB_CONTINUE;
3108 AttributeName* attrs,
3116 for ( ; attrs[count].an_name.bv_val; count++ );
3119 /* recognize a single "*" or a "1.1" */
3122 attrs = slap_anlist_all_user_attributes;
3124 } else if ( count == 1 && bvmatch( &attrs[0].an_name, slap_bv_no_attrs ) ) {
3129 for ( i = 0; i < num; i++ ) {
3133 if ( count > qm->attr_sets[i].count ) {
3138 if ( !qm->attr_sets[i].count ) {
3144 for ( a2 = attrs; a2->an_name.bv_val; a2++ ) {
3145 if ( !an_find( qm->attr_sets[i].attrs, &a2->an_name ) ) {
3163 /* Refresh a cached query:
3164 * 1: Replay the query on the remote DB and merge each entry into
3165 * the local DB. Remember the DNs of each remote entry.
3166 * 2: Search the local DB for all entries matching this queryID.
3167 * Delete any entry whose DN is not in the list from (1).
3169 typedef struct dnlist {
3170 struct dnlist *next;
3175 typedef struct refresh_info {
3183 static dnlist *dnl_alloc( Operation *op, struct berval *bvdn )
3185 dnlist *dn = op->o_tmpalloc( sizeof(dnlist) + bvdn->bv_len + 1,
3187 dn->dn.bv_len = bvdn->bv_len;
3188 dn->dn.bv_val = (char *)(dn+1);
3189 AC_MEMCPY( dn->dn.bv_val, bvdn->bv_val, dn->dn.bv_len );
3190 dn->dn.bv_val[dn->dn.bv_len] = '\0';
3195 refresh_merge( Operation *op, SlapReply *rs )
3197 if ( rs->sr_type == REP_SEARCH ) {
3198 refresh_info *ri = op->o_callback->sc_private;
3199 BackendDB *be = op->o_bd;
3205 ocb = op->o_callback;
3206 /* Find local entry, merge */
3207 op->o_bd = ri->ri_be;
3208 rc = be_entry_get_rw( op, &rs->sr_entry->e_nname, NULL, NULL, 0, &e );
3209 if ( rc != LDAP_SUCCESS || e == NULL ) {
3210 /* No local entry, just add it. FIXME: we are not checking
3211 * the cache entry limit here
3213 merge_entry( op, rs->sr_entry, 1, &ri->ri_q->q_uuid );
3215 /* Entry exists, update it */
3218 Modifications *modlist, *mods = NULL;
3219 const char* text = NULL;
3220 char textbuf[SLAP_TEXT_BUFLEN];
3221 size_t textlen = sizeof(textbuf);
3222 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
3226 /* Get a copy of only the attrs we requested */
3227 for ( a=e->e_attrs; a; a=a->a_next ) {
3228 if ( ad_inlist( a->a_desc, rs->sr_attrs )) {
3229 *b = attr_alloc( a->a_desc );
3231 /* The actual values still belong to e */
3232 (*b)->a_flags |= SLAP_ATTR_DONT_FREE_VALS |
3233 SLAP_ATTR_DONT_FREE_DATA;
3234 b = &((*b)->a_next);
3238 slap_entry2mods( rs->sr_entry, &modlist, &text, textbuf, textlen );
3239 syncrepl_diff_entry( op, ne.e_attrs, rs->sr_entry->e_attrs,
3240 &mods, &modlist, 0 );
3241 be_entry_release_r( op, e );
3242 attrs_free( ne.e_attrs );
3243 slap_mods_free( modlist, 1 );
3244 /* mods is NULL if there are no changes */
3246 struct berval dn = op->o_req_dn;
3247 struct berval ndn = op->o_req_ndn;
3248 op->o_tag = LDAP_REQ_MODIFY;
3249 op->orm_modlist = mods;
3250 op->o_req_dn = rs->sr_entry->e_name;
3251 op->o_req_ndn = rs->sr_entry->e_nname;
3252 op->o_callback = &cb;
3253 op->o_bd->be_modify( op, rs );
3254 slap_mods_free( mods, 1 );
3256 op->o_req_ndn = ndn;
3260 /* Add DN to list */
3261 dnl = dnl_alloc( op, &rs->sr_entry->e_nname );
3263 if ( ri->ri_tail ) {
3264 ri->ri_tail->next = dnl;
3269 op->o_callback = ocb;
3275 refresh_purge( Operation *op, SlapReply *rs )
3277 if ( rs->sr_type == REP_SEARCH ) {
3278 refresh_info *ri = op->o_callback->sc_private;
3282 /* Did the entry exist on the remote? */
3283 for ( dn=&ri->ri_dns; *dn; dn = &(*dn)->next ) {
3284 if ( dn_match( &(*dn)->dn, &rs->sr_entry->e_nname )) {
3285 dnlist *dnext = (*dn)->next;
3286 op->o_tmpfree( *dn, op->o_tmpmemctx );
3292 /* No, so put it on the list to delete */
3295 dnlist *dnl = dnl_alloc( op, &rs->sr_entry->e_nname );
3296 dnl->next = ri->ri_dels;
3298 a = attr_find( rs->sr_entry->e_attrs, ad_queryId );
3299 /* If ours is the only queryId, delete entry */
3300 dnl->delete = ( a->a_numvals == 1 );
3307 refresh_query( Operation *op, SlapReply *rs, CachedQuery *query,
3310 slap_callback cb = { 0 };
3311 refresh_info ri = { 0 };
3312 char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(pcacheQueryID=)" ) ];
3313 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
3314 Filter filter = {LDAP_FILTER_EQUALITY};
3315 AttributeName attrs[ 2 ] = {{{ 0 }}};
3319 ldap_pvt_thread_mutex_lock( &query->answerable_cnt_mutex );
3321 ldap_pvt_thread_mutex_unlock( &query->answerable_cnt_mutex );
3323 cb.sc_response = refresh_merge;
3324 cb.sc_private = &ri;
3327 ri.ri_be = op->o_bd;
3330 op->o_tag = LDAP_REQ_SEARCH;
3331 op->o_protocol = LDAP_VERSION3;
3332 op->o_callback = &cb;
3333 op->o_do_not_cache = 1;
3335 op->o_req_dn = query->qbase->base;
3336 op->o_req_ndn = query->qbase->base;
3337 op->ors_scope = query->scope;
3338 op->ors_slimit = SLAP_NO_LIMIT;
3339 op->ors_tlimit = SLAP_NO_LIMIT;
3340 op->ors_limit = NULL;
3341 op->ors_filter = query->filter;
3342 filter2bv_x( op, query->filter, &op->ors_filterstr );
3343 op->ors_attrs = query->qtemp->t_attrs.attrs;
3344 op->ors_attrsonly = 0;
3346 op->o_bd = on->on_info->oi_origdb;
3347 rc = op->o_bd->be_search( op, rs );
3349 op->o_bd = ri.ri_be;
3353 /* Get the DNs of all entries matching this query */
3354 cb.sc_response = refresh_purge;
3356 op->o_bd = ri.ri_be;
3357 op->o_req_dn = op->o_bd->be_suffix[0];
3358 op->o_req_ndn = op->o_bd->be_nsuffix[0];
3359 op->ors_scope = LDAP_SCOPE_SUBTREE;
3360 op->ors_deref = LDAP_DEREF_NEVER;
3361 op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
3362 "(%s=%s)", ad_queryId->ad_cname.bv_val, query->q_uuid.bv_val);
3363 filter.f_ava = &ava;
3364 filter.f_av_desc = ad_queryId;
3365 filter.f_av_value = query->q_uuid;
3366 attrs[ 0 ].an_desc = ad_queryId;
3367 attrs[ 0 ].an_name = ad_queryId->ad_cname;
3368 op->ors_attrs = attrs;
3369 op->ors_attrsonly = 0;
3370 rs->sr_entry = NULL;
3371 rs->sr_nentries = 0;
3372 rc = op->o_bd->be_search( op, rs );
3373 if ( rc ) goto leave;
3375 while (( dn = ri.ri_dels )) {
3376 op->o_req_dn = dn->dn;
3377 op->o_req_ndn = dn->dn;
3379 op->o_tag = LDAP_REQ_DELETE;
3380 op->o_bd->be_delete( op, rs );
3383 struct berval vals[2];
3385 vals[0] = query->q_uuid;
3386 BER_BVZERO( &vals[1] );
3387 mod.sml_op = LDAP_MOD_DELETE;
3389 mod.sml_desc = ad_queryId;
3390 mod.sml_type = ad_queryId->ad_cname;
3391 mod.sml_values = vals;
3392 mod.sml_nvalues = NULL;
3393 mod.sml_numvals = 1;
3394 mod.sml_next = NULL;
3396 op->o_tag = LDAP_REQ_MODIFY;
3397 op->orm_modlist = &mod;
3398 op->o_bd->be_modify( op, rs );
3400 ri.ri_dels = dn->next;
3401 op->o_tmpfree( dn, op->o_tmpmemctx );
3405 /* reset our local heap, we're done with it */
3406 slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, op->o_threadctx, 1 );
3415 struct re_s *rtask = arg;
3416 slap_overinst *on = rtask->arg;
3417 cache_manager *cm = on->on_bi.bi_private;
3418 query_manager *qm = cm->qm;
3419 Connection conn = {0};
3420 OperationBuffer opbuf;
3423 SlapReply rs = {REP_RESULT};
3424 CachedQuery *query, *qprev;
3425 int return_val, pause = PCACHE_CC_PAUSED;
3426 QueryTemplate *templ;
3428 /* Don't expire anything when we're offline */
3429 if ( cm->cc_paused & PCACHE_CC_OFFLINE ) {
3430 pause = PCACHE_CC_OFFLINE;
3434 connection_fake_init( &conn, &opbuf, ctx );
3438 op->o_dn = cm->db.be_rootdn;
3439 op->o_ndn = cm->db.be_rootndn;
3443 for (templ = qm->templates; templ; templ=templ->qmnext) {
3445 if ( !templ->query_last ) continue;
3447 op->o_time = slap_get_time();
3448 if ( !templ->ttr ) {
3450 if ( templ->negttl && templ->negttl < ttl )
3451 ttl = templ->negttl;
3452 if ( templ->limitttl && templ->limitttl < ttl )
3453 ttl = templ->limitttl;
3454 /* The oldest timestamp that needs expiration checking */
3458 for ( query=templ->query_last; query; query=qprev ) {
3459 qprev = query->prev;
3460 if ( query->refresh_time && query->refresh_time < op->o_time ) {
3461 /* A refresh will extend the expiry if the query has been
3462 * referenced, but not if it's unreferenced. If the
3463 * expiration has been hit, then skip the refresh since
3464 * we're just going to discard the result anyway.
3466 if ( query->refcnt )
3467 query->expiry_time = op->o_time + templ->ttl;
3468 if ( query->expiry_time > op->o_time ) {
3469 refresh_query( op, &rs, query, on );
3474 if (query->expiry_time < op->o_time) {
3476 Debug( pcache_debug, "Lock CR index = %p\n",
3477 (void *) templ, 0, 0 );
3478 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
3479 if ( query == templ->query_last ) {
3481 remove_from_template(query, templ);
3482 Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
3483 (void *) templ, templ->no_of_queries, 0 );
3484 Debug( pcache_debug, "Unlock CR index = %p\n",
3485 (void *) templ, 0, 0 );
3487 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
3491 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
3492 remove_query(qm, query);
3493 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
3494 if ( BER_BVISNULL( &query->q_uuid ))
3497 return_val = remove_query_data(op, &rs, &query->q_uuid);
3498 Debug( pcache_debug, "STALE QUERY REMOVED, SIZE=%d\n",
3500 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
3501 cm->cur_entries -= return_val;
3502 cm->num_cached_queries--;
3503 Debug( pcache_debug, "STORED QUERIES = %lu\n",
3504 cm->num_cached_queries, 0, 0 );
3505 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
3506 Debug( pcache_debug,
3507 "STALE QUERY REMOVED, CACHE ="
3509 cm->cur_entries, 0, 0 );
3511 } else if ( !templ->ttr && query->expiry_time > ttl ) {
3512 /* We don't need to check for refreshes, and this
3513 * query's expiry is too new, and all subsequent queries
3514 * will be newer yet. So stop looking.
3516 * If we have refreshes, then we always have to walk the
3517 * entire query list.
3525 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3526 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
3527 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
3529 /* If there were no queries, defer processing for a while */
3530 if ( cm->cc_paused != pause )
3531 cm->cc_paused = pause;
3532 ldap_pvt_runqueue_resched( &slapd_rq, rtask, pause );
3534 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3539 #define MAX_ATTR_SETS 500
3552 static ConfigDriver pc_cf_gen;
3553 static ConfigLDAPadd pc_ldadd;
3554 static ConfigCfAdd pc_cfadd;
3556 static ConfigTable pccfg[] = {
3557 { "pcache", "backend> <max_entries> <numattrsets> <entry limit> "
3559 6, 6, 0, ARG_MAGIC|ARG_NO_DELETE|PC_MAIN, pc_cf_gen,
3560 "( OLcfgOvAt:2.1 NAME ( 'olcPcache' 'olcProxyCache' ) "
3561 "DESC 'Proxy Cache basic parameters' "
3562 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
3563 { "pcacheAttrset", "index> <attributes...",
3564 2, 0, 0, ARG_MAGIC|PC_ATTR, pc_cf_gen,
3565 "( OLcfgOvAt:2.2 NAME ( 'olcPcacheAttrset' 'olcProxyAttrset' ) "
3566 "DESC 'A set of attributes to cache' "
3567 "SYNTAX OMsDirectoryString )", NULL, NULL },
3568 { "pcacheTemplate", "filter> <attrset-index> <TTL> <negTTL> "
3570 4, 7, 0, ARG_MAGIC|PC_TEMP, pc_cf_gen,
3571 "( OLcfgOvAt:2.3 NAME ( 'olcPcacheTemplate' 'olcProxyCacheTemplate' ) "
3572 "DESC 'Filter template, attrset, cache TTL, "
3573 "optional negative TTL, optional sizelimit TTL, "
3575 "SYNTAX OMsDirectoryString )", NULL, NULL },
3576 { "pcachePosition", "head|tail(default)",
3577 2, 2, 0, ARG_MAGIC|PC_RESP, pc_cf_gen,
3578 "( OLcfgOvAt:2.4 NAME 'olcPcachePosition' "
3579 "DESC 'Response callback position in overlay stack' "
3580 "SYNTAX OMsDirectoryString )", NULL, NULL },
3581 { "pcacheMaxQueries", "queries",
3582 2, 2, 0, ARG_INT|ARG_MAGIC|PC_QUERIES, pc_cf_gen,
3583 "( OLcfgOvAt:2.5 NAME ( 'olcPcacheMaxQueries' 'olcProxyCacheQueries' ) "
3584 "DESC 'Maximum number of queries to cache' "
3585 "SYNTAX OMsInteger )", NULL, NULL },
3586 { "pcachePersist", "TRUE|FALSE",
3587 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, save_queries),
3588 "( OLcfgOvAt:2.6 NAME ( 'olcPcachePersist' 'olcProxySaveQueries' ) "
3589 "DESC 'Save cached queries for hot restart' "
3590 "SYNTAX OMsBoolean )", NULL, NULL },
3591 { "pcacheValidate", "TRUE|FALSE",
3592 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, check_cacheability),
3593 "( OLcfgOvAt:2.7 NAME ( 'olcPcacheValidate' 'olcProxyCheckCacheability' ) "
3594 "DESC 'Check whether the results of a query are cacheable, e.g. for schema issues' "
3595 "SYNTAX OMsBoolean )", NULL, NULL },
3596 { "pcacheOffline", "TRUE|FALSE",
3597 2, 2, 0, ARG_ON_OFF|ARG_MAGIC|PC_OFFLINE, pc_cf_gen,
3598 "( OLcfgOvAt:2.8 NAME 'olcPcacheOffline' "
3599 "DESC 'Set cache to offline mode and disable expiration' "
3600 "SYNTAX OMsBoolean )", NULL, NULL },
3601 { "pcacheBind", "filter> <attrset-index> <TTR> <scope> <base",
3602 6, 6, 0, ARG_MAGIC|PC_BIND, pc_cf_gen,
3603 "( OLcfgOvAt:2.9 NAME 'olcPcacheBind' "
3604 "DESC 'Parameters for caching Binds' "
3605 "SYNTAX OMsDirectoryString )", NULL, NULL },
3606 { "pcache-", "private database args",
3607 1, 0, STRLENOF("pcache-"), ARG_MAGIC|PC_PRIVATE_DB, pc_cf_gen,
3610 /* Legacy keywords */
3611 { "proxycache", "backend> <max_entries> <numattrsets> <entry limit> "
3613 6, 6, 0, ARG_MAGIC|ARG_NO_DELETE|PC_MAIN, pc_cf_gen,
3615 { "proxyattrset", "index> <attributes...",
3616 2, 0, 0, ARG_MAGIC|PC_ATTR, pc_cf_gen,
3618 { "proxytemplate", "filter> <attrset-index> <TTL> <negTTL",
3619 4, 7, 0, ARG_MAGIC|PC_TEMP, pc_cf_gen,
3621 { "response-callback", "head|tail(default)",
3622 2, 2, 0, ARG_MAGIC|PC_RESP, pc_cf_gen,
3624 { "proxyCacheQueries", "queries",
3625 2, 2, 0, ARG_INT|ARG_MAGIC|PC_QUERIES, pc_cf_gen,
3627 { "proxySaveQueries", "TRUE|FALSE",
3628 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, save_queries),
3630 { "proxyCheckCacheability", "TRUE|FALSE",
3631 2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, check_cacheability),
3634 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
3637 static ConfigOCs pcocs[] = {
3638 { "( OLcfgOvOc:2.1 "
3639 "NAME 'olcPcacheConfig' "
3640 "DESC 'ProxyCache configuration' "
3641 "SUP olcOverlayConfig "
3642 "MUST ( olcPcache $ olcPcacheAttrset $ olcPcacheTemplate ) "
3643 "MAY ( olcPcachePosition $ olcPcacheMaxQueries $ olcPcachePersist $ "
3644 "olcPcacheValidate $ olcPcacheOffline $ olcPcacheBind ) )",
3645 Cft_Overlay, pccfg, NULL, pc_cfadd },
3646 { "( OLcfgOvOc:2.2 "
3647 "NAME 'olcPcacheDatabase' "
3648 "DESC 'Cache database configuration' "
3649 "AUXILIARY )", Cft_Misc, olcDatabaseDummy, pc_ldadd },
3653 static int pcache_db_open2( slap_overinst *on, ConfigReply *cr );
3656 pc_ldadd_cleanup( ConfigArgs *c )
3658 slap_overinst *on = c->ca_private;
3659 return pcache_db_open2( on, &c->reply );
3663 pc_ldadd( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
3668 if ( p->ce_type != Cft_Overlay || !p->ce_bi ||
3669 p->ce_bi->bi_cf_ocs != pcocs )
3670 return LDAP_CONSTRAINT_VIOLATION;
3672 on = (slap_overinst *)p->ce_bi;
3673 cm = on->on_bi.bi_private;
3675 /* Defer open if this is an LDAPadd */
3676 if ( CONFIG_ONLINE_ADD( ca ))
3677 ca->cleanup = pc_ldadd_cleanup;
3679 cm->defer_db_open = 0;
3680 ca->ca_private = on;
3681 return LDAP_SUCCESS;
3685 pc_cfadd( Operation *op, SlapReply *rs, Entry *p, ConfigArgs *ca )
3687 CfEntryInfo *pe = p->e_private;
3688 slap_overinst *on = (slap_overinst *)pe->ce_bi;
3689 cache_manager *cm = on->on_bi.bi_private;
3692 /* FIXME: should not hardcode "olcDatabase" here */
3693 bv.bv_len = snprintf( ca->cr_msg, sizeof( ca->cr_msg ),
3694 "olcDatabase=" SLAP_X_ORDERED_FMT "%s",
3695 0, cm->db.bd_info->bi_type );
3696 if ( bv.bv_len >= sizeof( ca->cr_msg ) ) {
3699 bv.bv_val = ca->cr_msg;
3701 cm->defer_db_open = 0;
3703 /* We can only create this entry if the database is table-driven
3705 if ( cm->db.bd_info->bi_cf_ocs )
3706 config_build_entry( op, rs, pe, ca, &bv, cm->db.bd_info->bi_cf_ocs,
3713 pc_cf_gen( ConfigArgs *c )
3715 slap_overinst *on = (slap_overinst *)c->bi;
3716 cache_manager* cm = on->on_bi.bi_private;
3717 query_manager* qm = cm->qm;
3718 QueryTemplate* temp;
3719 AttributeName* attr_name;
3720 AttributeName* attrarray;
3721 const char* text=NULL;
3726 if ( c->op == SLAP_CONFIG_EMIT ) {
3730 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s %d %d %d %ld",
3731 cm->db.bd_info->bi_type, cm->max_entries, cm->numattrsets,
3732 cm->num_entries_limit, cm->cc_period );
3733 bv.bv_val = c->cr_msg;
3734 value_add_one( &c->rvalue_vals, &bv );
3737 for (i=0; i<cm->numattrsets; i++) {
3738 if ( !qm->attr_sets[i].count ) continue;
3740 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ), "%d", i );
3742 /* count the attr length */
3743 for ( attr_name = qm->attr_sets[i].attrs;
3744 attr_name->an_name.bv_val; attr_name++ )
3745 bv.bv_len += attr_name->an_name.bv_len + 1;
3747 bv.bv_val = ch_malloc( bv.bv_len+1 );
3748 ptr = lutil_strcopy( bv.bv_val, c->cr_msg );
3749 for ( attr_name = qm->attr_sets[i].attrs;
3750 attr_name->an_name.bv_val; attr_name++ ) {
3752 ptr = lutil_strcopy( ptr, attr_name->an_name.bv_val );
3754 ber_bvarray_add( &c->rvalue_vals, &bv );
3756 if ( !c->rvalue_vals )
3760 for (temp=qm->templates; temp; temp=temp->qmnext) {
3761 /* HEADS-UP: always print all;
3762 * if optional == 0, ignore */
3763 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ),
3764 " %d %ld %ld %ld %ld",
3765 temp->attr_set_index,
3770 bv.bv_len += temp->querystr.bv_len + 2;
3771 bv.bv_val = ch_malloc( bv.bv_len+1 );
3774 ptr = lutil_strcopy( ptr, temp->querystr.bv_val );
3776 strcpy( ptr, c->cr_msg );
3777 ber_bvarray_add( &c->rvalue_vals, &bv );
3779 if ( !c->rvalue_vals )
3783 for (temp=qm->templates; temp; temp=temp->qmnext) {
3784 if ( !temp->bindttr ) continue;
3785 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ),
3787 temp->attr_set_index,
3789 ldap_pvt_scope2str( temp->bindscope ));
3790 bv.bv_len += temp->bindbase.bv_len + temp->bindftemp.bv_len + 4;
3791 bv.bv_val = ch_malloc( bv.bv_len + 1 );
3794 ptr = lutil_strcopy( ptr, temp->bindftemp.bv_val );
3796 ptr = lutil_strcopy( ptr, c->cr_msg );
3798 ptr = lutil_strcopy( ptr, temp->bindbase.bv_val );
3801 ber_bvarray_add( &c->rvalue_vals, &bv );
3803 if ( !c->rvalue_vals )
3807 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
3808 BER_BVSTR( &bv, "head" );
3810 BER_BVSTR( &bv, "tail" );
3812 value_add_one( &c->rvalue_vals, &bv );
3815 c->value_int = cm->max_queries;
3818 c->value_int = (cm->cc_paused & PCACHE_CC_OFFLINE) != 0;
3822 } else if ( c->op == LDAP_MOD_DELETE ) {
3825 case PC_ATTR: /* FIXME */
3830 cm->cc_paused &= ~PCACHE_CC_OFFLINE;
3831 /* If there were cached queries when we went offline,
3832 * restart the checker now.
3834 if ( cm->num_cached_queries ) {
3835 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3837 ldap_pvt_runqueue_resched( &slapd_rq, cm->cc_arg, 0 );
3838 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3848 if ( cm->numattrsets > 0 ) {
3849 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"pcache\" directive already provided" );
3850 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3854 if ( lutil_atoi( &cm->numattrsets, c->argv[3] ) != 0 ) {
3855 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse num attrsets=\"%s\" (arg #3)",
3857 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3860 if ( cm->numattrsets <= 0 ) {
3861 snprintf( c->cr_msg, sizeof( c->cr_msg ), "numattrsets (arg #3) must be positive" );
3862 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3865 if ( cm->numattrsets > MAX_ATTR_SETS ) {
3866 snprintf( c->cr_msg, sizeof( c->cr_msg ), "numattrsets (arg #3) must be <= %d", MAX_ATTR_SETS );
3867 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3871 if ( !backend_db_init( c->argv[1], &cm->db, -1, NULL )) {
3872 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unknown backend type (arg #1)" );
3873 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3877 if ( lutil_atoi( &cm->max_entries, c->argv[2] ) != 0 ) {
3878 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse max entries=\"%s\" (arg #2)",
3880 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3883 if ( cm->max_entries <= 0 ) {
3884 snprintf( c->cr_msg, sizeof( c->cr_msg ), "max entries (arg #2) must be positive.\n" );
3885 Debug( LDAP_DEBUG_CONFIG, "%s: %s\n", c->log, c->cr_msg, 0 );
3889 if ( lutil_atoi( &cm->num_entries_limit, c->argv[4] ) != 0 ) {
3890 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse entry limit=\"%s\" (arg #4)",
3892 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3895 if ( cm->num_entries_limit <= 0 ) {
3896 snprintf( c->cr_msg, sizeof( c->cr_msg ), "entry limit (arg #4) must be positive" );
3897 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3900 if ( cm->num_entries_limit > cm->max_entries ) {
3901 snprintf( c->cr_msg, sizeof( c->cr_msg ), "entry limit (arg #4) must be less than max entries %d (arg #2)", cm->max_entries );
3902 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3906 if ( lutil_parse_time( c->argv[5], &t ) != 0 ) {
3907 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse period=\"%s\" (arg #5)",
3909 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3913 cm->cc_period = (time_t)t;
3914 Debug( pcache_debug,
3915 "Total # of attribute sets to be cached = %d.\n",
3916 cm->numattrsets, 0, 0 );
3917 qm->attr_sets = ( struct attr_set * )ch_calloc( cm->numattrsets,
3918 sizeof( struct attr_set ) );
3921 if ( cm->numattrsets == 0 ) {
3922 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"pcache\" directive not provided yet" );
3923 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3926 if ( lutil_atoi( &num, c->argv[1] ) != 0 ) {
3927 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse attrset #=\"%s\"",
3929 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3933 if ( num < 0 || num >= cm->numattrsets ) {
3934 snprintf( c->cr_msg, sizeof( c->cr_msg ), "attrset index %d out of bounds (must be %s%d)",
3935 num, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
3936 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3939 qm->attr_sets[num].flags |= PC_CONFIGURED;
3940 if ( c->argc == 2 ) {
3942 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3943 "need an explicit attr in attrlist; use \"*\" to indicate all attrs" );
3944 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3947 } else if ( c->argc == 3 ) {
3948 if ( strcmp( c->argv[2], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) {
3949 qm->attr_sets[num].count = 1;
3950 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 2,
3951 sizeof( AttributeName ) );
3952 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_USER_ATTRIBUTES );
3955 } else if ( strcmp( c->argv[2], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 ) {
3956 qm->attr_sets[num].count = 1;
3957 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 2,
3958 sizeof( AttributeName ) );
3959 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
3962 } else if ( strcmp( c->argv[2], LDAP_NO_ATTRS ) == 0 ) {
3965 /* else: fallthru */
3967 } else if ( c->argc == 4 ) {
3968 if ( ( strcmp( c->argv[2], LDAP_ALL_USER_ATTRIBUTES ) == 0 && strcmp( c->argv[3], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 )
3969 || ( strcmp( c->argv[2], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 && strcmp( c->argv[3], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) )
3971 qm->attr_sets[num].count = 2;
3972 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( 3,
3973 sizeof( AttributeName ) );
3974 BER_BVSTR( &qm->attr_sets[num].attrs[0].an_name, LDAP_ALL_USER_ATTRIBUTES );
3975 BER_BVSTR( &qm->attr_sets[num].attrs[1].an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
3978 /* else: fallthru */
3981 if ( c->argc > 2 ) {
3982 int all_user = 0, all_op = 0;
3984 qm->attr_sets[num].count = c->argc - 2;
3985 qm->attr_sets[num].attrs = (AttributeName*)ch_calloc( c->argc - 1,
3986 sizeof( AttributeName ) );
3987 attr_name = qm->attr_sets[num].attrs;
3988 for ( i = 2; i < c->argc; i++ ) {
3989 attr_name->an_desc = NULL;
3990 if ( strcmp( c->argv[i], LDAP_NO_ATTRS ) == 0 ) {
3991 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3992 "invalid attr #%d \"%s\" in attrlist",
3993 i - 2, c->argv[i] );
3994 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
3995 ch_free( qm->attr_sets[num].attrs );
3996 qm->attr_sets[num].attrs = NULL;
3997 qm->attr_sets[num].count = 0;
4000 if ( strcmp( c->argv[i], LDAP_ALL_USER_ATTRIBUTES ) == 0 ) {
4002 BER_BVSTR( &attr_name->an_name, LDAP_ALL_USER_ATTRIBUTES );
4003 } else if ( strcmp( c->argv[i], LDAP_ALL_OPERATIONAL_ATTRIBUTES ) == 0 ) {
4005 BER_BVSTR( &attr_name->an_name, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
4007 if ( slap_str2ad( c->argv[i], &attr_name->an_desc, &text ) ) {
4008 strcpy( c->cr_msg, text );
4009 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4010 ch_free( qm->attr_sets[num].attrs );
4011 qm->attr_sets[num].attrs = NULL;
4012 qm->attr_sets[num].count = 0;
4015 attr_name->an_name = attr_name->an_desc->ad_cname;
4017 attr_name->an_oc = NULL;
4018 attr_name->an_flags = 0;
4019 if ( attr_name->an_desc == slap_schema.si_ad_objectClass )
4020 qm->attr_sets[num].flags |= PC_GOT_OC;
4022 BER_BVZERO( &attr_name->an_name );
4025 /* warn if list contains both "*" and "+" */
4026 if ( i > 4 && all_user && all_op ) {
4027 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4028 "warning: attribute list contains \"*\" and \"+\"" );
4029 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4034 if ( cm->numattrsets == 0 ) {
4035 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"pcache\" directive not provided yet" );
4036 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4039 if ( lutil_atoi( &i, c->argv[2] ) != 0 ) {
4040 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse template #=\"%s\"",
4042 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4046 if ( i < 0 || i >= cm->numattrsets ||
4047 !(qm->attr_sets[i].flags & PC_CONFIGURED )) {
4048 snprintf( c->cr_msg, sizeof( c->cr_msg ), "template index %d invalid (%s%d)",
4049 i, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
4050 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4054 AttributeName *attrs;
4056 cnt = template_attrs( c->argv[1], &qm->attr_sets[i], &attrs, &text );
4058 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse template: %s",
4060 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4063 temp = ch_calloc( 1, sizeof( QueryTemplate ));
4064 temp->qmnext = qm->templates;
4065 qm->templates = temp;
4066 temp->t_attrs.attrs = attrs;
4067 temp->t_attrs.count = cnt;
4069 ldap_pvt_thread_rdwr_init( &temp->t_rwlock );
4070 temp->query = temp->query_last = NULL;
4071 if ( lutil_parse_time( c->argv[3], &t ) != 0 ) {
4072 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4073 "unable to parse template ttl=\"%s\"",
4075 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4077 ch_free( temp->t_attrs.attrs );
4081 temp->ttl = (time_t)t;
4082 temp->negttl = (time_t)0;
4083 temp->limitttl = (time_t)0;
4084 temp->ttr = (time_t)0;
4085 switch ( c->argc ) {
4087 if ( lutil_parse_time( c->argv[6], &t ) != 0 ) {
4088 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4089 "unable to parse template ttr=\"%s\"",
4091 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4094 temp->ttr = (time_t)t;
4098 if ( lutil_parse_time( c->argv[5], &t ) != 0 ) {
4099 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4100 "unable to parse template sizelimit ttl=\"%s\"",
4102 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4105 temp->limitttl = (time_t)t;
4109 if ( lutil_parse_time( c->argv[4], &t ) != 0 ) {
4110 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4111 "unable to parse template negative ttl=\"%s\"",
4113 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4116 temp->negttl = (time_t)t;
4120 temp->no_of_queries = 0;
4122 ber_str2bv( c->argv[1], 0, 1, &temp->querystr );
4123 Debug( pcache_debug, "Template:\n", 0, 0, 0 );
4124 Debug( pcache_debug, " query template: %s\n",
4125 temp->querystr.bv_val, 0, 0 );
4126 temp->attr_set_index = i;
4127 qm->attr_sets[i].flags |= PC_REFERENCED;
4128 temp->qtnext = qm->attr_sets[i].templates;
4129 qm->attr_sets[i].templates = temp;
4130 Debug( pcache_debug, " attributes: \n", 0, 0, 0 );
4131 if ( ( attrarray = qm->attr_sets[i].attrs ) != NULL ) {
4132 for ( i=0; attrarray[i].an_name.bv_val; i++ )
4133 Debug( pcache_debug, "\t%s\n",
4134 attrarray[i].an_name.bv_val, 0, 0 );
4138 if ( !qm->templates ) {
4139 snprintf( c->cr_msg, sizeof( c->cr_msg ), "\"pcacheTemplate\" directive not provided yet" );
4140 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4143 if ( lutil_atoi( &i, c->argv[2] ) != 0 ) {
4144 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse Bind index #=\"%s\"",
4146 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4150 if ( i < 0 || i >= cm->numattrsets ||
4151 !(qm->attr_sets[i].flags & PC_CONFIGURED )) {
4152 snprintf( c->cr_msg, sizeof( c->cr_msg ), "Bind index %d invalid (%s%d)",
4153 i, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
4154 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4157 { struct berval bv, tempbv;
4158 AttributeDescription **descs;
4160 ber_str2bv( c->argv[1], 0, 0, &bv );
4161 ndescs = ftemp_attrs( &bv, &tempbv, &descs, &text );
4163 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unable to parse template: %s",
4165 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4168 for ( temp = qm->templates; temp; temp=temp->qmnext ) {
4169 if ( temp->attr_set_index == i && bvmatch( &tempbv,
4173 ch_free( tempbv.bv_val );
4176 snprintf( c->cr_msg, sizeof( c->cr_msg ), "Bind template %s %d invalid",
4178 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4181 ber_dupbv( &temp->bindftemp, &bv );
4182 temp->bindfattrs = descs;
4183 temp->bindnattrs = ndescs;
4185 if ( lutil_parse_time( c->argv[3], &t ) != 0 ) {
4186 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4187 "unable to parse bind ttr=\"%s\"",
4189 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4191 ch_free( temp->bindfattrs );
4192 temp->bindfattrs = NULL;
4193 ch_free( temp->bindftemp.bv_val );
4194 BER_BVZERO( &temp->bindftemp );
4197 num = ldap_pvt_str2scope( c->argv[4] );
4199 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4200 "unable to parse bind scope=\"%s\"",
4202 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4206 struct berval dn, ndn;
4207 ber_str2bv( c->argv[5], 0, 0, &dn );
4208 rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
4210 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4211 "invalid bind baseDN=\"%s\"",
4213 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4216 if ( temp->bindbase.bv_val )
4217 ch_free( temp->bindbase.bv_val );
4218 temp->bindbase = ndn;
4221 /* convert the template into dummy filter */
4223 char *eq = temp->bindftemp.bv_val, *e2;
4226 while ((eq = strchr(eq, '=' ))) {
4231 bv.bv_len = temp->bindftemp.bv_len + i;
4232 bv.bv_val = ch_malloc( bv.bv_len + 1 );
4233 for ( e2 = bv.bv_val, eq = temp->bindftemp.bv_val;
4244 f = str2filter( bv.bv_val );
4246 ch_free( bv.bv_val );
4247 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4248 "unable to parse bindfilter=\"%s\"", bv.bv_val );
4249 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4250 ch_free( temp->bindbase.bv_val );
4251 BER_BVZERO( &temp->bindbase );
4254 if ( temp->bindfilter )
4255 filter_free( temp->bindfilter );
4256 if ( temp->bindfilterstr.bv_val )
4257 ch_free( temp->bindfilterstr.bv_val );
4258 temp->bindfilterstr = bv;
4259 temp->bindfilter = f;
4261 temp->bindttr = (time_t)t;
4262 temp->bindscope = num;
4263 cm->cache_binds = 1;
4267 if ( strcasecmp( c->argv[1], "head" ) == 0 ) {
4268 cm->response_cb = PCACHE_RESPONSE_CB_HEAD;
4270 } else if ( strcasecmp( c->argv[1], "tail" ) == 0 ) {
4271 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
4274 snprintf( c->cr_msg, sizeof( c->cr_msg ), "unknown specifier" );
4275 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4280 if ( c->value_int <= 0 ) {
4281 snprintf( c->cr_msg, sizeof( c->cr_msg ), "max queries must be positive" );
4282 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4285 cm->max_queries = c->value_int;
4289 cm->cc_paused |= PCACHE_CC_OFFLINE;
4291 cm->cc_paused &= ~PCACHE_CC_OFFLINE;
4294 if ( cm->db.be_private == NULL ) {
4295 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4296 "private database must be defined before setting database specific options" );
4297 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4301 if ( cm->db.bd_info->bi_cf_ocs ) {
4304 char *argv0 = c->argv[ 0 ];
4306 c->argv[ 0 ] = &argv0[ STRLENOF( "pcache-" ) ];
4308 ct = config_find_keyword( cm->db.bd_info->bi_cf_ocs->co_table, c );
4310 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4311 "private database does not recognize specific option '%s'",
4313 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4317 c->table = cm->db.bd_info->bi_cf_ocs->co_type;
4319 c->bi = c->be->bd_info;
4321 rc = config_add_vals( ct, c );
4325 c->table = c2.table;
4328 c->argv[ 0 ] = argv0;
4330 } else if ( cm->db.be_config != NULL ) {
4331 char *argv0 = c->argv[ 0 ];
4333 c->argv[ 0 ] = &argv0[ STRLENOF( "pcache-" ) ];
4334 rc = cm->db.be_config( &cm->db, c->fname, c->lineno, c->argc, c->argv );
4335 c->argv[ 0 ] = argv0;
4338 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4339 "no means to set private database specific options" );
4340 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->cr_msg, 0 );
4345 rc = SLAP_CONF_UNKNOWN;
4361 slap_overinst *on = (slap_overinst *)be->bd_info;
4362 cache_manager* cm = on->on_bi.bi_private;
4364 /* Something for the cache database? */
4365 if ( cm->db.bd_info && cm->db.bd_info->bi_db_config )
4366 return cm->db.bd_info->bi_db_config( &cm->db, fname, lineno,
4368 return SLAP_CONF_UNKNOWN;
4376 slap_overinst *on = (slap_overinst *)be->bd_info;
4380 cm = (cache_manager *)ch_malloc(sizeof(cache_manager));
4381 on->on_bi.bi_private = cm;
4383 qm = (query_manager*)ch_malloc(sizeof(query_manager));
4386 SLAP_DBFLAGS(&cm->db) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
4387 cm->db.be_private = NULL;
4388 cm->db.bd_self = &cm->db;
4390 cm->numattrsets = 0;
4391 cm->num_entries_limit = 5;
4392 cm->num_cached_queries = 0;
4393 cm->max_entries = 0;
4394 cm->cur_entries = 0;
4395 cm->max_queries = 10000;
4396 cm->save_queries = 0;
4397 cm->check_cacheability = 0;
4398 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
4399 cm->defer_db_open = 1;
4400 cm->cache_binds = 0;
4401 cm->cc_period = 1000;
4404 #ifdef PCACHE_MONITOR
4405 cm->monitor_cb = NULL;
4406 #endif /* PCACHE_MONITOR */
4408 qm->attr_sets = NULL;
4409 qm->templates = NULL;
4411 qm->lru_bottom = NULL;
4413 qm->qcfunc = query_containment;
4414 qm->crfunc = cache_replacement;
4415 qm->addfunc = add_query;
4416 ldap_pvt_thread_mutex_init(&qm->lru_mutex);
4418 ldap_pvt_thread_mutex_init(&cm->cache_mutex);
4420 #ifndef PCACHE_MONITOR
4422 #else /* PCACHE_MONITOR */
4423 return pcache_monitor_db_init( be );
4424 #endif /* PCACHE_MONITOR */
4428 pcache_cachedquery_open_cb( Operation *op, SlapReply *rs )
4430 assert( op->o_tag == LDAP_REQ_SEARCH );
4432 if ( rs->sr_type == REP_SEARCH ) {
4435 a = attr_find( rs->sr_entry->e_attrs, ad_cachedQueryURL );
4439 assert( a->a_nvals != NULL );
4441 valsp = op->o_callback->sc_private;
4442 assert( *valsp == NULL );
4444 ber_bvarray_dup_x( valsp, a->a_nvals, op->o_tmpmemctx );
4452 pcache_cachedquery_count_cb( Operation *op, SlapReply *rs )
4454 assert( op->o_tag == LDAP_REQ_SEARCH );
4456 if ( rs->sr_type == REP_SEARCH ) {
4457 int *countp = (int *)op->o_callback->sc_private;
4470 cache_manager *cm = on->on_bi.bi_private;
4471 query_manager* qm = cm->qm;
4474 rc = backend_startup_one( &cm->db, cr );
4476 cm->defer_db_open = 0;
4479 /* There is no runqueue in TOOL mode */
4480 if (( slapMode & SLAP_SERVER_MODE ) && rc == 0 ) {
4481 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4482 ldap_pvt_runqueue_insert( &slapd_rq, cm->cc_period,
4483 consistency_check, on,
4484 "pcache_consistency", cm->db.be_suffix[0].bv_val );
4485 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4487 /* Cached database must have the rootdn */
4488 if ( BER_BVISNULL( &cm->db.be_rootndn )
4489 || BER_BVISEMPTY( &cm->db.be_rootndn ) )
4491 Debug( LDAP_DEBUG_ANY, "pcache_db_open(): "
4492 "underlying database of type \"%s\"\n"
4493 " serving naming context \"%s\"\n"
4494 " has no \"rootdn\", required by \"pcache\".\n",
4495 on->on_info->oi_orig->bi_type,
4496 cm->db.be_suffix[0].bv_val, 0 );
4500 if ( cm->save_queries ) {
4501 void *thrctx = ldap_pvt_thread_pool_context();
4502 Connection conn = { 0 };
4503 OperationBuffer opbuf;
4505 slap_callback cb = { 0 };
4506 SlapReply rs = { 0 };
4507 BerVarray vals = NULL;
4508 Filter f = { 0 }, f2 = { 0 };
4509 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
4510 AttributeName attrs[ 2 ] = {{{ 0 }}};
4512 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
4517 op->o_tag = LDAP_REQ_SEARCH;
4518 op->o_protocol = LDAP_VERSION3;
4519 cb.sc_response = pcache_cachedquery_open_cb;
4520 cb.sc_private = &vals;
4521 op->o_callback = &cb;
4522 op->o_time = slap_get_time();
4523 op->o_do_not_cache = 1;
4524 op->o_managedsait = SLAP_CONTROL_CRITICAL;
4526 op->o_dn = cm->db.be_rootdn;
4527 op->o_ndn = cm->db.be_rootndn;
4528 op->o_req_dn = cm->db.be_suffix[ 0 ];
4529 op->o_req_ndn = cm->db.be_nsuffix[ 0 ];
4531 op->ors_scope = LDAP_SCOPE_BASE;
4532 op->ors_deref = LDAP_DEREF_NEVER;
4534 op->ors_tlimit = SLAP_NO_LIMIT;
4535 op->ors_limit = NULL;
4536 ber_str2bv( "(pcacheQueryURL=*)", 0, 0, &op->ors_filterstr );
4537 f.f_choice = LDAP_FILTER_PRESENT;
4538 f.f_desc = ad_cachedQueryURL;
4539 op->ors_filter = &f;
4540 attrs[ 0 ].an_desc = ad_cachedQueryURL;
4541 attrs[ 0 ].an_name = ad_cachedQueryURL->ad_cname;
4542 op->ors_attrs = attrs;
4543 op->ors_attrsonly = 0;
4545 rc = op->o_bd->be_search( op, &rs );
4546 if ( rc == LDAP_SUCCESS && vals != NULL ) {
4549 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
4550 if ( url2query( vals[ i ].bv_val, op, qm ) == 0 ) {
4551 cm->num_cached_queries++;
4555 ber_bvarray_free_x( vals, op->o_tmpmemctx );
4558 /* count cached entries */
4559 f.f_choice = LDAP_FILTER_NOT;
4561 f2.f_choice = LDAP_FILTER_EQUALITY;
4563 f2.f_av_desc = slap_schema.si_ad_objectClass;
4564 BER_BVSTR( &f2.f_av_value, "glue" );
4565 ber_str2bv( "(!(objectClass=glue))", 0, 0, &op->ors_filterstr );
4567 op->ors_slimit = SLAP_NO_LIMIT;
4568 op->ors_scope = LDAP_SCOPE_SUBTREE;
4569 op->ors_attrs = slap_anlist_no_attrs;
4571 op->o_callback->sc_response = pcache_cachedquery_count_cb;
4573 op->o_callback->sc_private = &rs.sr_nentries;
4575 rc = op->o_bd->be_search( op, &rs );
4577 cm->cur_entries = rs.sr_nentries;
4591 slap_overinst *on = (slap_overinst *)be->bd_info;
4592 cache_manager *cm = on->on_bi.bi_private;
4593 query_manager* qm = cm->qm;
4594 int i, ncf = 0, rf = 0, nrf = 0, rc = 0;
4596 /* check attr sets */
4597 for ( i = 0; i < cm->numattrsets; i++) {
4598 if ( !( qm->attr_sets[i].flags & PC_CONFIGURED ) ) {
4599 if ( qm->attr_sets[i].flags & PC_REFERENCED ) {
4600 Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d not configured but referenced.\n", i, 0, 0 );
4604 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, attr set #%d not configured.\n", i, 0, 0 );
4608 } else if ( !( qm->attr_sets[i].flags & PC_REFERENCED ) ) {
4609 Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d configured but not referenced.\n", i, 0, 0 );
4614 if ( ncf || rf || nrf ) {
4615 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets configured but not referenced.\n", nrf, 0, 0 );
4616 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets not configured.\n", ncf, 0, 0 );
4617 Debug( LDAP_DEBUG_CONFIG, "pcache: %d attr sets not configured but referenced.\n", rf, 0, 0 );
4624 /* need to inherit something from the original database... */
4625 cm->db.be_def_limit = be->be_def_limit;
4626 cm->db.be_limits = be->be_limits;
4627 cm->db.be_acl = be->be_acl;
4628 cm->db.be_dfltaccess = be->be_dfltaccess;
4630 if ( SLAP_DBMONITORING( be ) ) {
4631 SLAP_DBFLAGS( &cm->db ) |= SLAP_DBFLAG_MONITORING;
4634 SLAP_DBFLAGS( &cm->db ) &= ~SLAP_DBFLAG_MONITORING;
4637 if ( !cm->defer_db_open ) {
4638 rc = pcache_db_open2( on, cr );
4641 #ifdef PCACHE_MONITOR
4642 if ( rc == LDAP_SUCCESS ) {
4643 rc = pcache_monitor_db_open( be );
4645 #endif /* PCACHE_MONITOR */
4651 pcache_free_qbase( void *v )
4657 tavl_free( qb->scopes[i], NULL );
4667 slap_overinst *on = (slap_overinst *)be->bd_info;
4668 cache_manager *cm = on->on_bi.bi_private;
4669 query_manager *qm = cm->qm;
4673 /* stop the thread ... */
4675 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4676 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, cm->cc_arg ) ) {
4677 ldap_pvt_runqueue_stoptask( &slapd_rq, cm->cc_arg );
4679 ldap_pvt_runqueue_remove( &slapd_rq, cm->cc_arg );
4680 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4683 if ( cm->save_queries ) {
4685 BerVarray vals = NULL;
4688 Connection conn = { 0 };
4689 OperationBuffer opbuf;
4691 slap_callback cb = { 0 };
4693 SlapReply rs = { REP_RESULT };
4694 Modifications mod = {{ 0 }};
4696 thrctx = ldap_pvt_thread_pool_context();
4698 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
4701 if ( qm->templates != NULL ) {
4702 for ( tm = qm->templates; tm != NULL; tm = tm->qmnext ) {
4703 for ( qc = tm->query; qc; qc = qc->next ) {
4706 if ( query2url( op, qc, &bv, 0 ) == 0 ) {
4707 ber_bvarray_add_x( &vals, &bv, op->o_tmpmemctx );
4714 op->o_dn = cm->db.be_rootdn;
4715 op->o_ndn = cm->db.be_rootndn;
4717 op->o_tag = LDAP_REQ_MODIFY;
4718 op->o_protocol = LDAP_VERSION3;
4719 cb.sc_response = slap_null_cb;
4720 op->o_callback = &cb;
4721 op->o_time = slap_get_time();
4722 op->o_do_not_cache = 1;
4723 op->o_managedsait = SLAP_CONTROL_CRITICAL;
4725 op->o_req_dn = op->o_bd->be_suffix[0];
4726 op->o_req_ndn = op->o_bd->be_nsuffix[0];
4728 mod.sml_op = LDAP_MOD_REPLACE;
4730 mod.sml_desc = ad_cachedQueryURL;
4731 mod.sml_type = ad_cachedQueryURL->ad_cname;
4732 mod.sml_values = vals;
4733 mod.sml_nvalues = NULL;
4734 mod.sml_numvals = 1;
4735 mod.sml_next = NULL;
4736 Debug( pcache_debug,
4737 "%sSETTING CACHED QUERY URLS\n",
4738 vals == NULL ? "RE" : "", 0, 0 );
4740 op->orm_modlist = &mod;
4742 op->o_bd->be_modify( op, &rs );
4744 ber_bvarray_free_x( vals, op->o_tmpmemctx );
4747 /* cleanup stuff inherited from the original database... */
4748 cm->db.be_limits = NULL;
4749 cm->db.be_acl = NULL;
4752 if ( cm->db.bd_info->bi_db_close ) {
4753 rc = cm->db.bd_info->bi_db_close( &cm->db, NULL );
4755 while ( (tm = qm->templates) != NULL ) {
4756 CachedQuery *qc, *qn;
4757 qm->templates = tm->qmnext;
4758 for ( qc = tm->query; qc; qc = qn ) {
4762 avl_free( tm->qbase, pcache_free_qbase );
4763 free( tm->querystr.bv_val );
4764 free( tm->bindfattrs );
4765 free( tm->bindftemp.bv_val );
4766 free( tm->bindfilterstr.bv_val );
4767 free( tm->bindbase.bv_val );
4768 filter_free( tm->bindfilter );
4769 ldap_pvt_thread_rdwr_destroy( &tm->t_rwlock );
4770 free( tm->t_attrs.attrs );
4774 for ( i=0; i<cm->numattrsets; i++ ) {
4775 free( qm->attr_sets[i].attrs );
4777 free( qm->attr_sets );
4778 qm->attr_sets = NULL;
4780 #ifdef PCACHE_MONITOR
4781 if ( rc == LDAP_SUCCESS ) {
4782 rc = pcache_monitor_db_close( be );
4784 #endif /* PCACHE_MONITOR */
4795 slap_overinst *on = (slap_overinst *)be->bd_info;
4796 cache_manager *cm = on->on_bi.bi_private;
4797 query_manager *qm = cm->qm;
4799 if ( cm->db.be_private != NULL ) {
4800 backend_stopdown_one( &cm->db );
4803 ldap_pvt_thread_mutex_destroy( &qm->lru_mutex );
4804 ldap_pvt_thread_mutex_destroy( &cm->cache_mutex );
4808 #ifdef PCACHE_MONITOR
4809 pcache_monitor_db_destroy( be );
4810 #endif /* PCACHE_MONITOR */
4815 #ifdef PCACHE_CONTROL_PRIVDB
4817 Control ::= SEQUENCE {
4818 controlType LDAPOID,
4819 criticality BOOLEAN DEFAULT FALSE,
4820 controlValue OCTET STRING OPTIONAL }
4822 controlType ::= 1.3.6.1.4.1.4203.666.11.9.5.1
4824 * criticality must be TRUE; controlValue must be absent.
4832 if ( op->o_ctrlflag[ privDB_cid ] != SLAP_CONTROL_NONE ) {
4833 rs->sr_text = "privateDB control specified multiple times";
4834 return LDAP_PROTOCOL_ERROR;
4837 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
4838 rs->sr_text = "privateDB control value not absent";
4839 return LDAP_PROTOCOL_ERROR;
4842 if ( !ctrl->ldctl_iscritical ) {
4843 rs->sr_text = "privateDB control criticality required";
4844 return LDAP_PROTOCOL_ERROR;
4847 op->o_ctrlflag[ privDB_cid ] = SLAP_CONTROL_CRITICAL;
4849 return LDAP_SUCCESS;
4852 static char *extops[] = {
4853 LDAP_EXOP_MODIFY_PASSWD,
4856 #endif /* PCACHE_CONTROL_PRIVDB */
4858 static struct berval pcache_exop_MODIFY_PASSWD = BER_BVC( LDAP_EXOP_MODIFY_PASSWD );
4859 #ifdef PCACHE_EXOP_QUERY_DELETE
4860 static struct berval pcache_exop_QUERY_DELETE = BER_BVC( PCACHE_EXOP_QUERY_DELETE );
4862 #define LDAP_TAG_EXOP_QUERY_DELETE_BASE ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 0)
4863 #define LDAP_TAG_EXOP_QUERY_DELETE_DN ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 1)
4864 #define LDAP_TAG_EXOP_QUERY_DELETE_UUID ((LBER_CLASS_CONTEXT|LBER_CONSTRUCTED) + 2)
4867 ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
4868 requestName [0] LDAPOID,
4869 requestValue [1] OCTET STRING OPTIONAL }
4871 requestName ::= 1.3.6.1.4.1.4203.666.11.9.6.1
4873 requestValue ::= SEQUENCE { CHOICE {
4875 entryDN [1] LDAPDN },
4876 queryID [2] OCTET STRING (SIZE(16))
4877 -- constrained to UUID }
4879 * Either baseDN or entryDN must be present, to allow database selection.
4881 * 1. if baseDN and queryID are present, then the query corresponding
4882 * to queryID is deleted;
4883 * 2. if baseDN is present and queryID is absent, then all queries
4885 * 3. if entryDN is present and queryID is absent, then all queries
4886 * corresponding to the queryID values present in entryDN are deleted;
4887 * 4. if entryDN and queryID are present, then all queries
4888 * corresponding to the queryID values present in entryDN are deleted,
4889 * but only if the value of queryID is contained in the entry;
4891 * Currently, only 1, 3 and 4 are implemented. 2 can be obtained by either
4892 * recursively deleting the database (ldapdelete -r) with PRIVDB control,
4893 * or by removing the database files.
4895 ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
4896 COMPONENTS OF LDAPResult,
4897 responseName [10] LDAPOID OPTIONAL,
4898 responseValue [11] OCTET STRING OPTIONAL }
4900 * responseName and responseValue must be absent.
4904 * - on success, *tagp is either LDAP_TAG_EXOP_QUERY_DELETE_BASE
4905 * or LDAP_TAG_EXOP_QUERY_DELETE_DN.
4906 * - if ndn != NULL, it is set to the normalized DN in the request
4907 * corresponding to either the baseDN or the entryDN, according
4908 * to *tagp; memory is malloc'ed on the Operation's slab, and must
4909 * be freed by the caller.
4910 * - if uuid != NULL, it is set to point to the normalized UUID;
4911 * memory is malloc'ed on the Operation's slab, and must
4912 * be freed by the caller.
4915 pcache_parse_query_delete(
4919 struct berval *uuid,
4923 int rc = LDAP_SUCCESS;
4926 BerElementBuffer berbuf;
4927 BerElement *ber = (BerElement *)&berbuf;
4928 struct berval reqdata = BER_BVNULL;
4940 if ( in == NULL || in->bv_len == 0 ) {
4941 *text = "empty request data field in queryDelete exop";
4942 return LDAP_PROTOCOL_ERROR;
4945 ber_dupbv_x( &reqdata, in, ctx );
4947 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
4948 ber_init2( ber, &reqdata, 0 );
4950 tag = ber_scanf( ber, "{" /*}*/ );
4952 if ( tag == LBER_ERROR ) {
4953 Debug( LDAP_DEBUG_TRACE,
4954 "pcache_parse_query_delete: decoding error.\n",
4956 goto decoding_error;
4959 tag = ber_peek_tag( ber, &len );
4960 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_BASE
4961 || tag == LDAP_TAG_EXOP_QUERY_DELETE_DN )
4965 if ( ndn != NULL ) {
4968 tag = ber_scanf( ber, "m", &dn );
4969 if ( tag == LBER_ERROR ) {
4970 Debug( LDAP_DEBUG_TRACE,
4971 "pcache_parse_query_delete: DN parse failed.\n",
4973 goto decoding_error;
4976 rc = dnNormalize( 0, NULL, NULL, &dn, ndn, ctx );
4977 if ( rc != LDAP_SUCCESS ) {
4978 *text = "invalid DN in queryDelete exop request data";
4983 tag = ber_scanf( ber, "x" /* "m" */ );
4984 if ( tag == LBER_DEFAULT ) {
4985 goto decoding_error;
4989 tag = ber_peek_tag( ber, &len );
4992 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_UUID ) {
4993 if ( uuid != NULL ) {
4995 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
4997 tag = ber_scanf( ber, "m", &bv );
4998 if ( tag == LBER_ERROR ) {
4999 Debug( LDAP_DEBUG_TRACE,
5000 "pcache_parse_query_delete: UUID parse failed.\n",
5002 goto decoding_error;
5005 if ( bv.bv_len != 16 ) {
5006 Debug( LDAP_DEBUG_TRACE,
5007 "pcache_parse_query_delete: invalid UUID length %lu.\n",
5008 (unsigned long)bv.bv_len, 0, 0 );
5009 goto decoding_error;
5012 rc = lutil_uuidstr_from_normalized(
5013 bv.bv_val, bv.bv_len,
5014 uuidbuf, sizeof( uuidbuf ) );
5016 goto decoding_error;
5018 ber_str2bv( uuidbuf, rc, 1, uuid );
5022 tag = ber_skip_tag( ber, &len );
5023 if ( tag == LBER_DEFAULT ) {
5024 goto decoding_error;
5028 Debug( LDAP_DEBUG_TRACE,
5029 "pcache_parse_query_delete: invalid UUID length %lu.\n",
5030 (unsigned long)len, 0, 0 );
5031 goto decoding_error;
5035 tag = ber_peek_tag( ber, &len );
5038 if ( tag != LBER_DEFAULT || len != 0 ) {
5040 Debug( LDAP_DEBUG_TRACE,
5041 "pcache_parse_query_delete: decoding error\n",
5043 rc = LDAP_PROTOCOL_ERROR;
5044 *text = "queryDelete data decoding error";
5047 if ( ndn && !BER_BVISNULL( ndn ) ) {
5048 slap_sl_free( ndn->bv_val, ctx );
5052 if ( uuid && !BER_BVISNULL( uuid ) ) {
5053 slap_sl_free( uuid->bv_val, ctx );
5058 if ( !BER_BVISNULL( &reqdata ) ) {
5059 ber_memfree_x( reqdata.bv_val, ctx );
5066 pcache_exop_query_delete(
5070 BackendDB *bd = op->o_bd;
5072 struct berval uuid = BER_BVNULL,
5074 char buf[ SLAP_TEXT_BUFLEN ];
5076 ber_tag_t tag = LBER_DEFAULT;
5078 if ( LogTest( LDAP_DEBUG_STATS ) ) {
5082 rs->sr_err = pcache_parse_query_delete( op->ore_reqdata,
5083 &tag, &op->o_req_ndn, uuidp,
5084 &rs->sr_text, op->o_tmpmemctx );
5085 if ( rs->sr_err != LDAP_SUCCESS ) {
5089 if ( LogTest( LDAP_DEBUG_STATS ) ) {
5090 assert( !BER_BVISNULL( &op->o_req_ndn ) );
5091 len = snprintf( buf, sizeof( buf ), " dn=\"%s\"", op->o_req_ndn.bv_val );
5093 if ( !BER_BVISNULL( &uuid ) && len < sizeof( buf ) ) {
5094 snprintf( &buf[ len ], sizeof( buf ) - len, " pcacheQueryId=\"%s\"", uuid.bv_val );
5097 Debug( LDAP_DEBUG_STATS, "%s QUERY DELETE%s\n",
5098 op->o_log_prefix, buf, 0 );
5100 op->o_req_dn = op->o_req_ndn;
5102 op->o_bd = select_backend( &op->o_req_ndn, 0 );
5103 rs->sr_err = backend_check_restrictions( op, rs,
5104 (struct berval *)&pcache_exop_QUERY_DELETE );
5105 if ( rs->sr_err != LDAP_SUCCESS ) {
5109 if ( op->o_bd->be_extended == NULL ) {
5110 send_ldap_error( op, rs, LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
5111 "backend does not support extended operations" );
5115 op->o_bd->be_extended( op, rs );
5118 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
5119 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
5120 BER_BVZERO( &op->o_req_ndn );
5121 BER_BVZERO( &op->o_req_dn );
5124 if ( !BER_BVISNULL( &uuid ) ) {
5125 op->o_tmpfree( uuid.bv_val, op->o_tmpmemctx );
5132 #endif /* PCACHE_EXOP_QUERY_DELETE */
5135 pcache_op_extended( Operation *op, SlapReply *rs )
5137 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
5138 cache_manager *cm = on->on_bi.bi_private;
5140 #ifdef PCACHE_CONTROL_PRIVDB
5141 if ( op->o_ctrlflag[ privDB_cid ] == SLAP_CONTROL_CRITICAL ) {
5142 return pcache_op_privdb( op, rs );
5144 #endif /* PCACHE_CONTROL_PRIVDB */
5146 #ifdef PCACHE_EXOP_QUERY_DELETE
5147 if ( bvmatch( &op->ore_reqoid, &pcache_exop_QUERY_DELETE ) ) {
5148 struct berval uuid = BER_BVNULL;
5149 ber_tag_t tag = LBER_DEFAULT;
5151 rs->sr_err = pcache_parse_query_delete( op->ore_reqdata,
5152 &tag, NULL, &uuid, &rs->sr_text, op->o_tmpmemctx );
5153 assert( rs->sr_err == LDAP_SUCCESS );
5155 if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_DN ) {
5156 /* remove all queries related to the selected entry */
5157 rs->sr_err = pcache_remove_entry_queries_from_cache( op,
5158 cm, &op->o_req_ndn, &uuid );
5160 } else if ( tag == LDAP_TAG_EXOP_QUERY_DELETE_BASE ) {
5161 if ( !BER_BVISNULL( &uuid ) ) {
5162 /* remove the selected query */
5163 rs->sr_err = pcache_remove_query_from_cache( op,
5167 /* TODO: remove all queries */
5168 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5169 rs->sr_text = "deletion of all queries not implemented";
5173 op->o_tmpfree( uuid.bv_val, op->o_tmpmemctx );
5176 #endif /* PCACHE_EXOP_QUERY_DELETE */
5178 /* We only care if we're configured for Bind caching */
5179 if ( bvmatch( &op->ore_reqoid, &pcache_exop_MODIFY_PASSWD ) &&
5181 /* See if the local entry exists and has a password.
5182 * It's too much work to find the matching query, so
5183 * we just see if there's a hashed password to update.
5185 Operation op2 = *op;
5191 op2.o_dn = op->o_bd->be_rootdn;
5192 op2.o_ndn = op->o_bd->be_rootndn;
5193 rc = be_entry_get_rw( &op2, &op->o_req_ndn, NULL,
5194 slap_schema.si_ad_userPassword, 0, &e );
5195 if ( rc == LDAP_SUCCESS && e ) {
5196 /* See if a recognized password is hashed here */
5197 Attribute *a = attr_find( e->e_attrs,
5198 slap_schema.si_ad_userPassword );
5199 if ( a && a->a_vals[0].bv_val[0] == '{' &&
5200 lutil_passwd_scheme( a->a_vals[0].bv_val )) {
5203 be_entry_release_r( &op2, e );
5207 rc = overlay_op_walk( op, rs, op_extended, on->on_info,
5209 if ( rc == LDAP_SUCCESS ) {
5210 req_pwdexop_s *qpw = &op->oq_pwdexop;
5212 /* We don't care if it succeeds or not */
5213 pc_setpw( &op2, &qpw->rs_new, cm );
5218 return SLAP_CB_CONTINUE;
5221 #ifdef PCACHE_MONITOR
5224 pcache_monitor_update(
5230 cache_manager *cm = (cache_manager *) priv;
5231 query_manager *qm = cm->qm;
5234 BerVarray vals = NULL;
5236 attr_delete( &e->e_attrs, ad_cachedQueryURL );
5237 if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || ad_inlist( ad_cachedQueryURL, rs->sr_attrs ) )
5238 && qm->templates != NULL )
5242 for ( tm = qm->templates; tm != NULL; tm = tm->qmnext ) {
5243 for ( qc = tm->query; qc; qc = qc->next ) {
5246 if ( query2url( op, qc, &bv, 1 ) == 0 ) {
5247 ber_bvarray_add_x( &vals, &bv, op->o_tmpmemctx );
5253 if ( vals != NULL ) {
5254 attr_merge_normalize( e, ad_cachedQueryURL, vals, NULL );
5255 ber_bvarray_free_x( vals, op->o_tmpmemctx );
5261 char buf[ SLAP_TEXT_BUFLEN ];
5264 /* number of cached queries */
5265 a = attr_find( e->e_attrs, ad_numQueries );
5266 assert( a != NULL );
5269 bv.bv_len = snprintf( buf, sizeof( buf ), "%lu", cm->num_cached_queries );
5271 if ( a->a_nvals != a->a_vals ) {
5272 ber_bvreplace( &a->a_nvals[ 0 ], &bv );
5274 ber_bvreplace( &a->a_vals[ 0 ], &bv );
5276 /* number of cached entries */
5277 a = attr_find( e->e_attrs, ad_numEntries );
5278 assert( a != NULL );
5281 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", cm->cur_entries );
5283 if ( a->a_nvals != a->a_vals ) {
5284 ber_bvreplace( &a->a_nvals[ 0 ], &bv );
5286 ber_bvreplace( &a->a_vals[ 0 ], &bv );
5289 return SLAP_CB_CONTINUE;
5293 pcache_monitor_free(
5297 struct berval values[ 2 ];
5298 Modification mod = { 0 };
5301 char textbuf[ SLAP_TEXT_BUFLEN ];
5305 /* NOTE: if slap_shutdown != 0, priv might have already been freed */
5308 /* Remove objectClass */
5309 mod.sm_op = LDAP_MOD_DELETE;
5310 mod.sm_desc = slap_schema.si_ad_objectClass;
5311 mod.sm_values = values;
5313 values[ 0 ] = oc_olmPCache->soc_cname;
5314 BER_BVZERO( &values[ 1 ] );
5316 rc = modify_delete_values( e, &mod, 1, &text,
5317 textbuf, sizeof( textbuf ) );
5318 /* don't care too much about return code... */
5321 mod.sm_values = NULL;
5322 mod.sm_desc = ad_cachedQueryURL;
5324 rc = modify_delete_values( e, &mod, 1, &text,
5325 textbuf, sizeof( textbuf ) );
5326 /* don't care too much about return code... */
5329 mod.sm_values = NULL;
5330 mod.sm_desc = ad_numQueries;
5332 rc = modify_delete_values( e, &mod, 1, &text,
5333 textbuf, sizeof( textbuf ) );
5334 /* don't care too much about return code... */
5337 mod.sm_values = NULL;
5338 mod.sm_desc = ad_numEntries;
5340 rc = modify_delete_values( e, &mod, 1, &text,
5341 textbuf, sizeof( textbuf ) );
5342 /* don't care too much about return code... */
5344 return SLAP_CB_CONTINUE;
5348 * call from within pcache_initialize()
5351 pcache_monitor_initialize( void )
5353 static int pcache_monitor_initialized = 0;
5355 if ( backend_info( "monitor" ) == NULL ) {
5359 if ( pcache_monitor_initialized++ ) {
5367 pcache_monitor_db_init( BackendDB *be )
5369 if ( pcache_monitor_initialize() == LDAP_SUCCESS ) {
5370 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING;
5377 pcache_monitor_db_open( BackendDB *be )
5379 slap_overinst *on = (slap_overinst *)be->bd_info;
5380 cache_manager *cm = on->on_bi.bi_private;
5381 Attribute *a, *next;
5382 monitor_callback_t *cb = NULL;
5385 monitor_extra_t *mbe;
5386 struct berval dummy = BER_BVC( "" );
5388 if ( !SLAP_DBMONITORING( be ) ) {
5392 mi = backend_info( "monitor" );
5393 if ( !mi || !mi->bi_extra ) {
5394 SLAP_DBFLAGS( be ) ^= SLAP_DBFLAG_MONITORING;
5399 /* don't bother if monitor is not configured */
5400 if ( !mbe->is_configured() ) {
5401 static int warning = 0;
5403 if ( warning++ == 0 ) {
5404 Debug( LDAP_DEBUG_ANY, "pcache_monitor_db_open: "
5405 "monitoring disabled; "
5406 "configure monitor database to enable\n",
5413 /* alloc as many as required (plus 1 for objectClass) */
5414 a = attrs_alloc( 1 + 2 );
5420 a->a_desc = slap_schema.si_ad_objectClass;
5421 attr_valadd( a, &oc_olmPCache->soc_cname, NULL, 1 );
5425 struct berval bv = BER_BVC( "0" );
5427 next->a_desc = ad_numQueries;
5428 attr_valadd( next, &bv, NULL, 1 );
5429 next = next->a_next;
5431 next->a_desc = ad_numEntries;
5432 attr_valadd( next, &bv, NULL, 1 );
5433 next = next->a_next;
5436 cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
5437 cb->mc_update = pcache_monitor_update;
5438 cb->mc_free = pcache_monitor_free;
5439 cb->mc_private = (void *)cm;
5441 /* make sure the database is registered; then add monitor attributes */
5442 BER_BVZERO( &cm->monitor_ndn );
5443 rc = mbe->register_overlay( be, on, &cm->monitor_ndn );
5445 rc = mbe->register_entry_attrs( &cm->monitor_ndn, a, cb,
5446 &dummy, -1, &dummy);
5462 /* store for cleanup */
5463 cm->monitor_cb = (void *)cb;
5465 /* we don't need to keep track of the attributes, because
5466 * bdb_monitor_free() takes care of everything */
5475 pcache_monitor_db_close( BackendDB *be )
5477 slap_overinst *on = (slap_overinst *)be->bd_info;
5478 cache_manager *cm = on->on_bi.bi_private;
5480 if ( cm->monitor_cb != NULL ) {
5481 BackendInfo *mi = backend_info( "monitor" );
5482 monitor_extra_t *mbe;
5484 if ( mi && &mi->bi_extra ) {
5486 mbe->unregister_entry_callback( NULL,
5487 (monitor_callback_t *)cm->monitor_cb,
5496 pcache_monitor_db_destroy( BackendDB *be )
5501 #endif /* PCACHE_MONITOR */
5503 static slap_overinst pcache;
5505 static char *obsolete_names[] = {
5510 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
5512 #endif /* SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC */
5517 struct berval debugbv = BER_BVC("pcache");
5521 code = slap_loglevel_get( &debugbv, &pcache_debug );
5526 #ifdef PCACHE_CONTROL_PRIVDB
5527 code = register_supported_control( PCACHE_CONTROL_PRIVDB,
5528 SLAP_CTRL_BIND|SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE, extops,
5529 parse_privdb_ctrl, &privDB_cid );
5530 if ( code != LDAP_SUCCESS ) {
5531 Debug( LDAP_DEBUG_ANY,
5532 "pcache_initialize: failed to register control %s (%d)\n",
5533 PCACHE_CONTROL_PRIVDB, code, 0 );
5536 #endif /* PCACHE_CONTROL_PRIVDB */
5538 #ifdef PCACHE_EXOP_QUERY_DELETE
5539 code = load_extop2( (struct berval *)&pcache_exop_QUERY_DELETE,
5540 SLAP_EXOP_WRITES|SLAP_EXOP_HIDE, pcache_exop_query_delete,
5542 if ( code != LDAP_SUCCESS ) {
5543 Debug( LDAP_DEBUG_ANY,
5544 "pcache_initialize: unable to register queryDelete exop: %d.\n",
5548 #endif /* PCACHE_EXOP_QUERY_DELETE */
5550 argv[ 0 ] = "back-bdb/back-hdb monitor";
5555 for ( i = 0; s_oid[ i ].name; i++ ) {
5557 argv[ 1 ] = s_oid[ i ].name;
5558 argv[ 2 ] = s_oid[ i ].oid;
5560 if ( parse_oidm( &c, 0, NULL ) != 0 ) {
5561 Debug( LDAP_DEBUG_ANY, "pcache_initialize: "
5562 "unable to add objectIdentifier \"%s=%s\"\n",
5563 s_oid[ i ].name, s_oid[ i ].oid, 0 );
5568 for ( i = 0; s_ad[i].desc != NULL; i++ ) {
5569 code = register_at( s_ad[i].desc, s_ad[i].adp, 0 );
5571 Debug( LDAP_DEBUG_ANY,
5572 "pcache_initialize: register_at #%d failed\n", i, 0, 0 );
5575 (*s_ad[i].adp)->ad_type->sat_flags |= SLAP_AT_HIDE;
5578 for ( i = 0; s_oc[i].desc != NULL; i++ ) {
5579 code = register_oc( s_oc[i].desc, s_oc[i].ocp, 0 );
5581 Debug( LDAP_DEBUG_ANY,
5582 "pcache_initialize: register_oc #%d failed\n", i, 0, 0 );
5585 (*s_oc[i].ocp)->soc_flags |= SLAP_OC_HIDE;
5588 pcache.on_bi.bi_type = "pcache";
5589 pcache.on_bi.bi_obsolete_names = obsolete_names;
5590 pcache.on_bi.bi_db_init = pcache_db_init;
5591 pcache.on_bi.bi_db_config = pcache_db_config;
5592 pcache.on_bi.bi_db_open = pcache_db_open;
5593 pcache.on_bi.bi_db_close = pcache_db_close;
5594 pcache.on_bi.bi_db_destroy = pcache_db_destroy;
5596 pcache.on_bi.bi_op_search = pcache_op_search;
5597 pcache.on_bi.bi_op_bind = pcache_op_bind;
5598 #ifdef PCACHE_CONTROL_PRIVDB
5599 pcache.on_bi.bi_op_compare = pcache_op_privdb;
5600 pcache.on_bi.bi_op_modrdn = pcache_op_privdb;
5601 pcache.on_bi.bi_op_modify = pcache_op_privdb;
5602 pcache.on_bi.bi_op_add = pcache_op_privdb;
5603 pcache.on_bi.bi_op_delete = pcache_op_privdb;
5604 #endif /* PCACHE_CONTROL_PRIVDB */
5605 pcache.on_bi.bi_extended = pcache_op_extended;
5607 pcache.on_bi.bi_chk_controls = pcache_chk_controls;
5609 pcache.on_bi.bi_cf_ocs = pcocs;
5611 code = config_register_schema( pccfg, pcocs );
5612 if ( code ) return code;
5614 return overlay_register( &pcache );
5617 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
5618 int init_module(int argc, char *argv[]) {
5619 return pcache_initialize();
5623 #endif /* defined(SLAPD_OVER_PROXYCACHE) */