]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/pcache.c
9db2ea4a080f2f91b4e6252c22b120bf5b512358
[openldap] / servers / slapd / overlays / pcache.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 2003-2006 The OpenLDAP Foundation.
5  * Portions Copyright 2003 IBM Corporation.
6  * Portions Copyright 2003 Symas Corporation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
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>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Apurva Kumar for inclusion
19  * in OpenLDAP Software and subsequently rewritten by Howard Chu.
20  */
21
22 #include "portable.h"
23
24 #ifdef SLAPD_OVER_PROXYCACHE
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/time.h>
30
31 #include "slap.h"
32 #include "lutil.h"
33 #include "ldap_rq.h"
34
35 #include "config.h"
36
37 /* query cache structs */
38 /* query */
39
40 typedef struct Query_s {
41         Filter*         filter;         /* Search Filter */
42         AttributeName*  attrs;          /* Projected attributes */
43         AttributeName*  save_attrs;     /* original attributes, saved for response */
44         struct berval   base;           /* Search Base */
45         int             scope;          /* Search scope */
46 } Query;
47
48 struct query_template_s;
49
50 /* struct representing a cached query */
51 typedef struct cached_query_s {
52         Query                           query;          /* LDAP query */
53         struct berval                   q_uuid;         /* query identifier */
54         struct query_template_s *qtemp; /* template of the query */
55         time_t                          expiry_time;    /* time till the query is considered valid */
56         struct cached_query_s           *next;          /* next query in the template */
57         struct cached_query_s           *prev;          /* previous query in the template */
58         struct cached_query_s           *lru_up;        /* previous query in the LRU list */
59         struct cached_query_s           *lru_down;      /* next query in the LRU list */
60 } CachedQuery;
61
62 /*
63  * Represents a set of projected attributes.
64  */
65
66 struct attr_set {
67         struct query_template_s *templates;
68         AttributeName*  attrs;          /* specifies the set */
69         unsigned        flags;
70 #define PC_CONFIGURED   (0x1)
71 #define PC_REFERENCED   (0x2)
72 #define PC_GOT_OC               (0x4)
73         int             count;          /* number of attributes */
74 };
75
76 /* struct representing a query template
77  * e.g. template string = &(cn=)(mail=)
78  */
79 typedef struct query_template_s {
80         struct query_template_s *qtnext;
81         struct query_template_s *qmnext;
82
83         CachedQuery*    query;          /* most recent query cached for the template */
84         CachedQuery*    query_last;     /* oldest query cached for the template */
85         ldap_pvt_thread_rdwr_t *t_rwlock; /* Rd/wr lock for accessing queries in the template */
86         struct berval   querystr;       /* Filter string corresponding to the QT */
87
88         int             attr_set_index; /* determines the projected attributes */
89         int             no_of_queries;  /* Total number of queries in the template */
90         time_t          ttl;            /* TTL for the queries of this template */
91         time_t          negttl;         /* TTL for negative results */
92         struct attr_set t_attrs;        /* filter attrs + attr_set */
93 } QueryTemplate;
94
95 struct query_manager_s;
96
97 /* prototypes for functions for 1) query containment
98  * 2) query addition, 3) cache replacement
99  */
100 typedef CachedQuery *   (QCfunc)(Operation *op, struct query_manager_s*, Query*, QueryTemplate*);
101 typedef void    (AddQueryfunc)(struct query_manager_s*, Query*, QueryTemplate*, struct berval*);
102 typedef void    (CRfunc)(struct query_manager_s*, struct berval * );
103
104 /* LDAP query cache */
105 typedef struct query_manager_s {
106         struct attr_set*        attr_sets;              /* possible sets of projected attributes */
107         QueryTemplate*          templates;              /* cacheable templates */
108
109         CachedQuery*            lru_top;                /* top and bottom of LRU list */
110         CachedQuery*            lru_bottom;
111
112         ldap_pvt_thread_mutex_t         lru_mutex;      /* mutex for accessing LRU list */
113
114         /* Query cache methods */
115         QCfunc                  *qcfunc;                        /* Query containment*/
116         CRfunc                  *crfunc;                        /* cache replacement */
117         AddQueryfunc    *addfunc;                       /* add query */
118 } query_manager;
119
120 /* LDAP query cache manager */
121 typedef struct cache_manager_s {
122         BackendDB       db;     /* underlying database */
123         unsigned long   num_cached_queries;             /* total number of cached queries */
124         unsigned long   max_queries;                    /* upper bound on # of cached queries */
125         int     numattrsets;                    /* number of attribute sets */
126         int     cur_entries;                    /* current number of entries cached */
127         int     max_entries;                    /* max number of entries cached */
128         int     num_entries_limit;              /* max # of entries in a cacheable query */
129
130         char    response_cb;                    /* install the response callback
131                                                  * at the tail of the callback list */
132 #define PCACHE_RESPONSE_CB_HEAD 0
133 #define PCACHE_RESPONSE_CB_TAIL 1
134
135         time_t  cc_period;              /* interval between successive consistency checks (sec) */
136         int     cc_paused;
137         void    *cc_arg;
138
139         ldap_pvt_thread_mutex_t         cache_mutex;
140
141         query_manager*   qm;    /* query cache managed by the cache manager */
142 } cache_manager;
143
144 static int pcache_debug;
145
146 static AttributeDescription *ad_queryid;
147 static char *queryid_schema = "( 1.3.6.1.4.1.4203.666.1.12 NAME 'queryid' "
148                         "DESC 'list of queries the entry belongs to' "
149                         "EQUALITY octetStringMatch "
150                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40{64} "
151                         "NO-USER-MODIFICATION USAGE directoryOperation )";
152
153 /* Return 1 for an added entry, else 0 */
154 static int
155 merge_entry(
156         Operation               *op,
157         Entry                   *e,
158         struct berval*          query_uuid )
159 {
160         int             rc;
161         Modifications* modlist = NULL;
162         const char*     text = NULL;
163         Attribute               *attr;
164         char                    textbuf[SLAP_TEXT_BUFLEN];
165         size_t                  textlen = sizeof(textbuf);
166
167         SlapReply sreply = {REP_RESULT};
168
169         slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
170
171         attr = e->e_attrs;
172         e->e_attrs = NULL;
173
174         /* add queryid attribute */
175         attr_merge_one( e, ad_queryid, query_uuid, NULL );
176
177         /* append the attribute list from the fetched entry */
178         e->e_attrs->a_next = attr;
179
180         op->o_tag = LDAP_REQ_ADD;
181         op->o_protocol = LDAP_VERSION3;
182         op->o_callback = &cb;
183         op->o_time = slap_get_time();
184         op->o_do_not_cache = 1;
185
186         op->ora_e = e;
187         op->o_req_dn = e->e_name;
188         op->o_req_ndn = e->e_nname;
189         rc = op->o_bd->be_add( op, &sreply );
190
191         if ( rc != LDAP_SUCCESS ) {
192                 if ( rc == LDAP_ALREADY_EXISTS ) {
193                         slap_entry2mods( e, &modlist, &text, textbuf, textlen );
194                         modlist->sml_op = LDAP_MOD_ADD;
195                         op->o_tag = LDAP_REQ_MODIFY;
196                         op->orm_modlist = modlist;
197                         op->o_bd->be_modify( op, &sreply );
198                         slap_mods_free( modlist, 1 );
199                 } else if ( rc == LDAP_REFERRAL ||
200                                         rc == LDAP_NO_SUCH_OBJECT ) {
201                         syncrepl_add_glue( op, e );
202                         e = NULL;
203                         rc = 1;
204                 }
205                 if ( e ) {
206                         entry_free( e );
207                         rc = 0;
208                 }
209         } else {
210                 be_entry_release_w( op, e );
211                 rc = 1;
212         }
213
214         return rc;
215 }
216
217 /* compare base and scope of incoming and cached queries */
218 static int base_scope_compare(
219         struct berval* ndn_stored,
220         struct berval* ndn_incoming,
221         int scope_stored,
222         int scope_incoming      )
223 {
224         struct berval pdn_incoming = BER_BVNULL;
225
226         if (scope_stored < scope_incoming)
227                 return 0;
228
229         if ( !dnIsSuffix(ndn_incoming, ndn_stored))
230                 return 0;
231
232         switch(scope_stored) {
233         case LDAP_SCOPE_BASE:
234                 return (ndn_incoming->bv_len == ndn_stored->bv_len);
235
236         case LDAP_SCOPE_ONELEVEL:
237                 switch(scope_incoming){
238                 case LDAP_SCOPE_BASE:
239                         dnParent(ndn_incoming, &pdn_incoming);
240                         return (pdn_incoming.bv_len == ndn_stored->bv_len);
241
242                 case LDAP_SCOPE_ONELEVEL:
243                         return (ndn_incoming->bv_len == ndn_stored->bv_len);
244
245                 default:
246                         return 0;
247                 }
248         case LDAP_SCOPE_SUBTREE:
249                 return 1;
250                 break;
251         default:
252                 return 0;
253                 break;
254     }
255 }
256
257 /* add query on top of LRU list */
258 static void
259 add_query_on_top (query_manager* qm, CachedQuery* qc)
260 {
261         CachedQuery* top = qm->lru_top;
262         Query* q = (Query*)qc;
263
264         qm->lru_top = qc;
265
266         if (top)
267                 top->lru_up = qc;
268         else
269                 qm->lru_bottom = qc;
270
271         qc->lru_down = top;
272         qc->lru_up = NULL;
273         Debug( pcache_debug, "Base of added query = %s\n",
274                         q->base.bv_val, 0, 0 );
275 }
276
277 /* remove_query from LRU list */
278
279 static void
280 remove_query (query_manager* qm, CachedQuery* qc)
281 {
282         CachedQuery* up;
283         CachedQuery* down;
284
285         if (!qc)
286                 return;
287
288         up = qc->lru_up;
289         down = qc->lru_down;
290
291         if (!up)
292                 qm->lru_top = down;
293
294         if (!down)
295                 qm->lru_bottom = up;
296
297         if (down)
298                 down->lru_up = up;
299
300         if (up)
301                 up->lru_down = down;
302
303         qc->lru_up = qc->lru_down = NULL;
304 }
305
306 /* find and remove string2 from string1
307  * from start if position = 1,
308  * from end if position = 3,
309  * from anywhere if position = 2
310  * string1 is overwritten if position = 2.
311  */
312
313 static int
314 find_and_remove(struct berval* ber1, struct berval* ber2, int position)
315 {
316         int ret=0;
317
318         if ( !ber2->bv_val )
319                 return 1;
320         if ( !ber1->bv_val )
321                 return 0;
322
323         switch( position ) {
324         case 1:
325                 if ( ber1->bv_len >= ber2->bv_len && !memcmp( ber1->bv_val,
326                         ber2->bv_val, ber2->bv_len )) {
327                         ret = 1;
328                         ber1->bv_val += ber2->bv_len;
329                         ber1->bv_len -= ber2->bv_len;
330                 }
331                 break;
332         case 2: {
333                 char *temp;
334                 ber1->bv_val[ber1->bv_len] = '\0';
335                 temp = strstr( ber1->bv_val, ber2->bv_val );
336                 if ( temp ) {
337                         strcpy( temp, temp+ber2->bv_len );
338                         ber1->bv_len -= ber2->bv_len;
339                         ret = 1;
340                 }
341                 break;
342                 }
343         case 3:
344                 if ( ber1->bv_len >= ber2->bv_len &&
345                         !memcmp( ber1->bv_val+ber1->bv_len-ber2->bv_len, ber2->bv_val,
346                                 ber2->bv_len )) {
347                         ret = 1;
348                         ber1->bv_len -= ber2->bv_len;
349                 }
350                 break;
351         }
352         return ret;
353 }
354
355
356 static struct berval*
357 merge_init_final(Operation *op, struct berval* init, struct berval* any,
358         struct berval* final)
359 {
360         struct berval* merged, *temp;
361         int i, any_count, count;
362
363         for (any_count=0; any && any[any_count].bv_val; any_count++)
364                 ;
365
366         count = any_count;
367
368         if (init->bv_val)
369                 count++;
370         if (final->bv_val)
371                 count++;
372
373         merged = (struct berval*)op->o_tmpalloc( (count+1)*sizeof(struct berval),
374                 op->o_tmpmemctx );
375         temp = merged;
376
377         if (init->bv_val) {
378                 ber_dupbv_x( temp, init, op->o_tmpmemctx );
379                 temp++;
380         }
381
382         for (i=0; i<any_count; i++) {
383                 ber_dupbv_x( temp, any, op->o_tmpmemctx );
384                 temp++; any++;
385         }
386
387         if (final->bv_val){
388                 ber_dupbv_x( temp, final, op->o_tmpmemctx );
389                 temp++;
390         }
391         BER_BVZERO( temp );
392         return merged;
393 }
394
395 /* Each element in stored must be found in incoming. Incoming is overwritten.
396  */
397 static int
398 strings_containment(struct berval* stored, struct berval* incoming)
399 {
400         struct berval* element;
401         int k=0;
402         int j, rc = 0;
403
404         for ( element=stored; element->bv_val != NULL; element++ ) {
405                 for (j = k; incoming[j].bv_val != NULL; j++) {
406                         if (find_and_remove(&(incoming[j]), element, 2)) {
407                                 k = j;
408                                 rc = 1;
409                                 break;
410                         }
411                         rc = 0;
412                 }
413                 if ( rc ) {
414                         continue;
415                 } else {
416                         return 0;
417                 }
418         }
419         return 1;
420 }
421
422 static int
423 substr_containment_substr(Operation *op, Filter* stored, Filter* incoming)
424 {
425         int rc = 0;
426
427         struct berval init_incoming;
428         struct berval final_incoming;
429         struct berval *remaining_incoming = NULL;
430
431         if ((!(incoming->f_sub_initial.bv_val) && (stored->f_sub_initial.bv_val))
432            || (!(incoming->f_sub_final.bv_val) && (stored->f_sub_final.bv_val)))
433                 return 0;
434
435         init_incoming = incoming->f_sub_initial;
436         final_incoming =  incoming->f_sub_final;
437
438         if (find_and_remove(&init_incoming,
439                         &(stored->f_sub_initial), 1) && find_and_remove(&final_incoming,
440                         &(stored->f_sub_final), 3))
441         {
442                 if (stored->f_sub_any == NULL) {
443                         rc = 1;
444                         goto final;
445                 }
446                 remaining_incoming = merge_init_final(op, &init_incoming,
447                                                 incoming->f_sub_any, &final_incoming);
448                 rc = strings_containment(stored->f_sub_any, remaining_incoming);
449                 ber_bvarray_free_x( remaining_incoming, op->o_tmpmemctx );
450         }
451 final:
452         return rc;
453 }
454
455 static int
456 substr_containment_equality(Operation *op, Filter* stored, Filter* incoming)
457 {
458         struct berval incoming_val[2];
459         int rc = 0;
460
461         incoming_val[1] = incoming->f_av_value;
462
463         if (find_and_remove(incoming_val+1,
464                         &(stored->f_sub_initial), 1) && find_and_remove(incoming_val+1,
465                         &(stored->f_sub_final), 3)) {
466                 if (stored->f_sub_any == NULL){
467                         rc = 1;
468                         goto final;
469                 }
470                 ber_dupbv_x( incoming_val, incoming_val+1, op->o_tmpmemctx );
471                 BER_BVZERO( incoming_val+1 );
472                 rc = strings_containment(stored->f_sub_any, incoming_val);
473                 op->o_tmpfree( incoming_val[0].bv_val, op->o_tmpmemctx );
474         }
475 final:
476         return rc;
477 }
478
479 /* check whether query is contained in any of
480  * the cached queries in template
481  */
482 static CachedQuery *
483 query_containment(Operation *op, query_manager *qm,
484                   Query *query,
485                   QueryTemplate *templa)
486 {
487         CachedQuery* qc;
488         Query* q;
489         Filter* inputf = query->filter;
490         struct berval* base = &(query->base);
491         int scope = query->scope;
492         int res=0;
493         Filter* fs;
494         Filter* fi;
495         int ret, rc;
496         const char* text;
497
498         MatchingRule* mrule = NULL;
499         if (inputf != NULL) {
500                 Debug( pcache_debug, "Lock QC index = %p\n",
501                                 templa, 0, 0 );
502                 ldap_pvt_thread_rdwr_rlock(templa->t_rwlock);
503                 for(qc=templa->query; qc != NULL; qc= qc->next) {
504                         q = (Query*)qc;
505                         if(base_scope_compare(&(q->base), base, q->scope, scope)) {
506                                 fi = inputf;
507                                 fs = q->filter;
508                                 do {
509                                         res=0;
510                                         switch (fs->f_choice) {
511                                         case LDAP_FILTER_EQUALITY:
512                                                 if (fi->f_choice == LDAP_FILTER_EQUALITY)
513                                                         mrule = fs->f_ava->aa_desc->ad_type->sat_equality;
514                                                 else
515                                                         ret = 1;
516                                                 break;
517                                         case LDAP_FILTER_GE:
518                                         case LDAP_FILTER_LE:
519                                                 mrule = fs->f_ava->aa_desc->ad_type->sat_ordering;
520                                                 break;
521                                         default:
522                                                 mrule = NULL; 
523                                         }
524                                         if (mrule) {
525                                                 rc = value_match(&ret, fs->f_ava->aa_desc, mrule,
526                                                         SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
527                                                         &(fi->f_ava->aa_value),
528                                                         &(fs->f_ava->aa_value), &text);
529                                                 if (rc != LDAP_SUCCESS) {
530                                                         ldap_pvt_thread_rdwr_runlock(templa->t_rwlock);
531                                                         Debug( pcache_debug,
532                                                         "Unlock: Exiting QC index=%p\n",
533                                                         templa, 0, 0 );
534                                                         return NULL;
535                                                 }
536                                         }
537                                         switch (fs->f_choice) {
538                                         case LDAP_FILTER_OR:
539                                         case LDAP_FILTER_AND:
540                                                 fs = fs->f_and;
541                                                 fi = fi->f_and;
542                                                 res=1;
543                                                 break;
544                                         case LDAP_FILTER_SUBSTRINGS:
545                                                 /* check if the equality query can be
546                                                 * answered with cached substring query */
547                                                 if ((fi->f_choice == LDAP_FILTER_EQUALITY)
548                                                         && substr_containment_equality( op,
549                                                         fs, fi))
550                                                         res=1;
551                                                 /* check if the substring query can be
552                                                 * answered with cached substring query */
553                                                 if ((fi->f_choice ==LDAP_FILTER_SUBSTRINGS
554                                                         ) && substr_containment_substr( op,
555                                                         fs, fi))
556                                                         res= 1;
557                                                 fs=fs->f_next;
558                                                 fi=fi->f_next;
559                                                 break;
560                                         case LDAP_FILTER_PRESENT:
561                                                 res=1;
562                                                 fs=fs->f_next;
563                                                 fi=fi->f_next;
564                                                 break;
565                                         case LDAP_FILTER_EQUALITY:
566                                                 if (ret == 0)
567                                                         res = 1;
568                                                 fs=fs->f_next;
569                                                 fi=fi->f_next;
570                                                 break;
571                                         case LDAP_FILTER_GE:
572                                                 if (mrule && ret >= 0)
573                                                         res = 1;
574                                                 fs=fs->f_next;
575                                                 fi=fi->f_next;
576                                                 break;
577                                         case LDAP_FILTER_LE:
578                                                 if (mrule && ret <= 0)
579                                                         res = 1;
580                                                 fs=fs->f_next;
581                                                 fi=fi->f_next;
582                                                 break;
583                                         case LDAP_FILTER_NOT:
584                                                 res=0;
585                                                 break;
586                                         default:
587                                                 break;
588                                         }
589                                 } while((res) && (fi != NULL) && (fs != NULL));
590
591                                 if(res) {
592                                         ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
593                                         if (qm->lru_top != qc) {
594                                                 remove_query(qm, qc);
595                                                 add_query_on_top(qm, qc);
596                                         }
597                                         ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
598                                         return qc;
599                                 }
600                         }
601                 }
602                 Debug( pcache_debug,
603                         "Not answerable: Unlock QC index=%p\n",
604                         templa, 0, 0 );
605                 ldap_pvt_thread_rdwr_runlock(templa->t_rwlock);
606         }
607         return NULL;
608 }
609
610 static void
611 free_query (CachedQuery* qc)
612 {
613         Query* q = (Query*)qc;
614
615         free(qc->q_uuid.bv_val);
616         filter_free(q->filter);
617         free (q->base.bv_val);
618         free(q->attrs);
619         free(qc);
620 }
621
622
623 /* Add query to query cache */
624 static void add_query(
625         query_manager* qm,
626         Query* query,
627         QueryTemplate *templ,
628         struct berval* uuid)
629 {
630         CachedQuery* new_cached_query = (CachedQuery*) ch_malloc(sizeof(CachedQuery));
631         Query* new_query;
632         new_cached_query->qtemp = templ;
633         if ( uuid ) {
634                 new_cached_query->q_uuid = *uuid;
635                 new_cached_query->expiry_time = slap_get_time() + templ->ttl;
636         } else {
637                 BER_BVZERO( &new_cached_query->q_uuid );
638                 new_cached_query->expiry_time = slap_get_time() + templ->negttl;
639         }
640         new_cached_query->lru_up = NULL;
641         new_cached_query->lru_down = NULL;
642         Debug( pcache_debug, "Added query expires at %ld\n",
643                         (long) new_cached_query->expiry_time, 0, 0 );
644         new_query = (Query*)new_cached_query;
645
646         ber_dupbv(&new_query->base, &query->base);
647         new_query->scope = query->scope;
648         new_query->filter = query->filter;
649         new_query->attrs = query->attrs;
650
651         /* Adding a query    */
652         Debug( pcache_debug, "Lock AQ index = %p\n",
653                         templ, 0, 0 );
654         ldap_pvt_thread_rdwr_wlock(templ->t_rwlock);
655         if (templ->query == NULL)
656                 templ->query_last = new_cached_query;
657         else
658                 templ->query->prev = new_cached_query;
659         new_cached_query->next = templ->query;
660         new_cached_query->prev = NULL;
661         templ->query = new_cached_query;
662         templ->no_of_queries++;
663         Debug( pcache_debug, "TEMPLATE %p QUERIES++ %d\n",
664                         templ, templ->no_of_queries, 0 );
665
666         Debug( pcache_debug, "Unlock AQ index = %p \n",
667                         templ, 0, 0 );
668         ldap_pvt_thread_rdwr_wunlock(templ->t_rwlock);
669
670         /* Adding on top of LRU list  */
671         ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
672         add_query_on_top(qm, new_cached_query);
673         ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
674 }
675
676 static void
677 remove_from_template (CachedQuery* qc, QueryTemplate* template)
678 {
679         if (!qc->prev && !qc->next) {
680                 template->query_last = template->query = NULL;
681         } else if (qc->prev == NULL) {
682                 qc->next->prev = NULL;
683                 template->query = qc->next;
684         } else if (qc->next == NULL) {
685                 qc->prev->next = NULL;
686                 template->query_last = qc->prev;
687         } else {
688                 qc->next->prev = qc->prev;
689                 qc->prev->next = qc->next;
690         }
691
692         template->no_of_queries--;
693 }
694
695 /* remove bottom query of LRU list from the query cache */
696 static void cache_replacement(query_manager* qm, struct berval *result)
697 {
698         CachedQuery* bottom;
699         QueryTemplate *temp;
700
701         ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
702         bottom = qm->lru_bottom;
703
704         result->bv_val = NULL;
705         result->bv_len = 0;
706
707         if (!bottom) {
708                 Debug ( pcache_debug,
709                         "Cache replacement invoked without "
710                         "any query in LRU list\n", 0, 0, 0 );
711                 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
712                 return;
713         }
714
715         temp = bottom->qtemp;
716         remove_query(qm, bottom);
717         ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
718
719         *result = bottom->q_uuid;
720         bottom->q_uuid.bv_val = NULL;
721
722         Debug( pcache_debug, "Lock CR index = %p\n", temp, 0, 0 );
723         ldap_pvt_thread_rdwr_wlock(temp->t_rwlock);
724         remove_from_template(bottom, temp);
725         Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
726                 temp, temp->no_of_queries, 0 );
727         Debug( pcache_debug, "Unlock CR index = %p\n", temp, 0, 0 );
728         ldap_pvt_thread_rdwr_wunlock(temp->t_rwlock);
729         free_query(bottom);
730 }
731
732 struct query_info {
733         struct query_info *next;
734         struct berval xdn;
735         int del;
736 };
737
738 static int
739 remove_func (
740         Operation       *op,
741         SlapReply       *rs
742 )
743 {
744         Attribute *attr;
745         struct query_info *qi;
746         int count = 0;
747
748         if ( rs->sr_type != REP_SEARCH ) return 0;
749
750         for (attr = rs->sr_entry->e_attrs; attr!= NULL; attr = attr->a_next) {
751                 if (attr->a_desc == ad_queryid) {
752                         for (count=0; attr->a_vals[count].bv_val; count++)
753                                 ;
754                         break;
755                 }
756         }
757         if ( count == 0 ) return 0;
758         qi = op->o_tmpalloc( sizeof( struct query_info ), op->o_tmpmemctx );
759         qi->next = op->o_callback->sc_private;
760         op->o_callback->sc_private = qi;
761         ber_dupbv_x( &qi->xdn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
762         qi->del = ( count == 1 );
763
764         return 0;
765 }
766
767 static int
768 remove_query_data (
769         Operation       *op,
770         SlapReply       *rs,
771         struct berval* query_uuid)
772 {
773         struct query_info       *qi, *qnext;
774         char                    filter_str[64];
775 #ifdef LDAP_COMP_MATCH
776         AttributeAssertion      ava = { NULL, BER_BVNULL, NULL };
777 #else
778         AttributeAssertion      ava = { NULL, BER_BVNULL };
779 #endif
780         Filter                  filter = {LDAP_FILTER_EQUALITY};
781         SlapReply               sreply = {REP_RESULT};
782         slap_callback cb = { NULL, remove_func, NULL, NULL };
783         int deleted = 0;
784
785         sreply.sr_entry = NULL;
786         sreply.sr_nentries = 0;
787         op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
788                 "(%s=%s)", ad_queryid->ad_cname.bv_val, query_uuid->bv_val);
789         filter.f_ava = &ava;
790         filter.f_av_desc = ad_queryid;
791         filter.f_av_value = *query_uuid;
792
793         op->o_tag = LDAP_REQ_SEARCH;
794         op->o_protocol = LDAP_VERSION3;
795         op->o_callback = &cb;
796         op->o_time = slap_get_time();
797         op->o_do_not_cache = 1;
798
799         op->o_req_dn = op->o_bd->be_suffix[0];
800         op->o_req_ndn = op->o_bd->be_nsuffix[0];
801         op->ors_scope = LDAP_SCOPE_SUBTREE;
802         op->ors_deref = LDAP_DEREF_NEVER;
803         op->ors_slimit = SLAP_NO_LIMIT;
804         op->ors_tlimit = SLAP_NO_LIMIT;
805         op->ors_filter = &filter;
806         op->ors_filterstr.bv_val = filter_str;
807         op->ors_filterstr.bv_len = strlen(filter_str);
808         op->ors_attrs = NULL;
809         op->ors_attrsonly = 0;
810
811         op->o_bd->be_search( op, &sreply );
812
813         for ( qi=cb.sc_private; qi; qi=qnext ) {
814                 qnext = qi->next;
815
816                 op->o_req_dn = qi->xdn;
817                 op->o_req_ndn = qi->xdn;
818
819                 if ( qi->del) {
820                         Debug( pcache_debug, "DELETING ENTRY TEMPLATE=%s\n",
821                                 query_uuid->bv_val, 0, 0 );
822
823                         op->o_tag = LDAP_REQ_DELETE;
824
825                         if (op->o_bd->be_delete(op, &sreply) == LDAP_SUCCESS) {
826                                 deleted++;
827                         }
828                 } else {
829                         Modifications mod;
830                         struct berval vals[2];
831
832                         vals[0] = *query_uuid;
833                         vals[1].bv_val = NULL;
834                         vals[1].bv_len = 0;
835                         mod.sml_op = LDAP_MOD_DELETE;
836                         mod.sml_flags = 0;
837                         mod.sml_desc = ad_queryid;
838                         mod.sml_type = ad_queryid->ad_cname;
839                         mod.sml_values = vals;
840                         mod.sml_nvalues = NULL;
841                         mod.sml_next = NULL;
842                         Debug( pcache_debug,
843                                 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
844                                 query_uuid->bv_val, 0, 0 );
845
846                         op->orm_modlist = &mod;
847
848                         op->o_bd->be_modify( op, &sreply );
849                 }
850                 op->o_tmpfree( qi->xdn.bv_val, op->o_tmpmemctx );
851                 op->o_tmpfree( qi, op->o_tmpmemctx );
852         }
853         return deleted;
854 }
855
856 static int
857 get_attr_set(
858         AttributeName* attrs,
859         query_manager* qm,
860         int num
861 );
862
863 static int
864 filter2template(
865         Operation               *op,
866         Filter                  *f,
867         struct                  berval *fstr,
868         AttributeName**         filter_attrs,
869         int*                    filter_cnt,
870         int*                    filter_got_oc )
871 {
872         AttributeDescription *ad;
873
874         switch ( f->f_choice ) {
875         case LDAP_FILTER_EQUALITY:
876                 ad = f->f_av_desc;
877                 sprintf( fstr->bv_val+fstr->bv_len, "(%s=)", ad->ad_cname.bv_val );
878                 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=)") - 1 );
879                 break;
880
881         case LDAP_FILTER_GE:
882                 ad = f->f_av_desc;
883                 sprintf( fstr->bv_val+fstr->bv_len, "(%s>=)", ad->ad_cname.bv_val);
884                 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(>=)") - 1 );
885                 break;
886
887         case LDAP_FILTER_LE:
888                 ad = f->f_av_desc;
889                 sprintf( fstr->bv_val+fstr->bv_len, "(%s<=)", ad->ad_cname.bv_val);
890                 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(<=)") - 1 );
891                 break;
892
893         case LDAP_FILTER_APPROX:
894                 ad = f->f_av_desc;
895                 sprintf( fstr->bv_val+fstr->bv_len, "(%s~=)", ad->ad_cname.bv_val);
896                 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(~=)") - 1 );
897                 break;
898
899         case LDAP_FILTER_SUBSTRINGS:
900                 ad = f->f_sub_desc;
901                 sprintf( fstr->bv_val+fstr->bv_len, "(%s=)", ad->ad_cname.bv_val );
902                 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=)") - 1 );
903                 break;
904
905         case LDAP_FILTER_PRESENT:
906                 ad = f->f_desc;
907                 sprintf( fstr->bv_val+fstr->bv_len, "(%s=*)", ad->ad_cname.bv_val );
908                 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=*)") - 1 );
909                 break;
910
911         case LDAP_FILTER_AND:
912         case LDAP_FILTER_OR:
913         case LDAP_FILTER_NOT: {
914                 int rc = 0;
915                 sprintf( fstr->bv_val+fstr->bv_len, "(%c",
916                         f->f_choice == LDAP_FILTER_AND ? '&' :
917                         f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
918                 fstr->bv_len += sizeof("(%") - 1;
919
920                 for ( f = f->f_list; f != NULL; f = f->f_next ) {
921                         rc = filter2template( op, f, fstr, filter_attrs, filter_cnt,
922                                 filter_got_oc );
923                         if ( rc ) break;
924                 }
925                 sprintf( fstr->bv_val+fstr->bv_len, ")" );
926                 fstr->bv_len += sizeof(")") - 1;
927
928                 return rc;
929                 }
930
931         default:
932                 strcpy( fstr->bv_val, "(?=?)" );
933                 fstr->bv_len += sizeof("(?=?)")-1;
934                 return -1;
935         }
936
937         *filter_attrs = (AttributeName *)op->o_tmprealloc(*filter_attrs,
938                                 (*filter_cnt + 2)*sizeof(AttributeName), op->o_tmpmemctx);
939
940         (*filter_attrs)[*filter_cnt].an_desc = ad;
941         (*filter_attrs)[*filter_cnt].an_name = ad->ad_cname;
942         (*filter_attrs)[*filter_cnt].an_oc = NULL;
943         (*filter_attrs)[*filter_cnt].an_oc_exclude = 0;
944         BER_BVZERO( &(*filter_attrs)[*filter_cnt+1].an_name );
945         (*filter_cnt)++;
946         if ( ad == slap_schema.si_ad_objectClass )
947                 *filter_got_oc = 1;
948         return 0;
949 }
950
951 struct search_info {
952         slap_overinst *on;
953         Query query;
954         QueryTemplate *qtemp;
955         int max;
956         int over;
957         int count;
958         Entry *head, *tail;
959 };
960
961 static int
962 cache_entries(
963         Operation       *op,
964         SlapReply       *rs,
965         struct berval *query_uuid)
966 {
967         struct search_info *si = op->o_callback->sc_private;
968         slap_overinst *on = si->on;
969         cache_manager *cm = on->on_bi.bi_private;
970         query_manager*          qm = cm->qm;
971         int             return_val = 0;
972         Entry           *e;
973         struct berval   crp_uuid;
974         char            uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
975         Operation op_tmp = *op;
976
977         query_uuid->bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf));
978         ber_str2bv(uuidbuf, query_uuid->bv_len, 1, query_uuid);
979
980         op_tmp.o_bd = &cm->db;
981         op_tmp.o_dn = cm->db.be_rootdn;
982         op_tmp.o_ndn = cm->db.be_rootndn;
983
984         Debug( pcache_debug, "UUID for query being added = %s\n",
985                         uuidbuf, 0, 0 );
986
987         for ( e=si->head; e; e=si->head ) {
988                 si->head = e->e_private;
989                 e->e_private = NULL;
990                 while ( cm->cur_entries > (cm->max_entries) ) {
991                                 qm->crfunc(qm, &crp_uuid);
992                                 if (crp_uuid.bv_val) {
993                                         Debug( pcache_debug,
994                                                 "Removing query UUID %s\n",
995                                                 crp_uuid.bv_val, 0, 0 );
996                                         return_val = remove_query_data(&op_tmp, rs, &crp_uuid);
997                                         Debug( pcache_debug,
998                                                 "QUERY REMOVED, SIZE=%d\n",
999                                                 return_val, 0, 0);
1000                                         ldap_pvt_thread_mutex_lock(
1001                                                         &cm->cache_mutex );
1002                                         cm->cur_entries -= return_val;
1003                                         cm->num_cached_queries--;
1004                                         Debug( pcache_debug,
1005                                                 "STORED QUERIES = %lu\n",
1006                                                 cm->num_cached_queries, 0, 0 );
1007                                         ldap_pvt_thread_mutex_unlock(
1008                                                         &cm->cache_mutex );
1009                                         Debug( pcache_debug,
1010                                                 "QUERY REMOVED, CACHE ="
1011                                                 "%d entries\n",
1012                                                 cm->cur_entries, 0, 0 );
1013                                 }
1014                 }
1015
1016                 return_val = merge_entry(&op_tmp, e, query_uuid);
1017                 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1018                 cm->cur_entries += return_val;
1019                 Debug( pcache_debug,
1020                         "ENTRY ADDED/MERGED, CACHED ENTRIES=%d\n",
1021                         cm->cur_entries, 0, 0 );
1022                 return_val = 0;
1023                 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1024         }
1025
1026         return return_val;
1027 }
1028
1029 static int
1030 pcache_response(
1031         Operation       *op,
1032         SlapReply       *rs )
1033 {
1034         struct search_info *si = op->o_callback->sc_private;
1035         slap_overinst *on = si->on;
1036         cache_manager *cm = on->on_bi.bi_private;
1037         query_manager*          qm = cm->qm;
1038         struct berval uuid;
1039
1040         if ( si->query.save_attrs != NULL ) {
1041                 rs->sr_attrs = si->query.save_attrs;
1042                 op->ors_attrs = si->query.save_attrs;
1043                 si->query.save_attrs = NULL;
1044         }
1045
1046         if ( rs->sr_type == REP_SEARCH ) {
1047                 Entry *e;
1048                 /* If we haven't exceeded the limit for this query,
1049                  * build a chain of answers to store. If we hit the
1050                  * limit, empty the chain and ignore the rest.
1051                  */
1052                 if ( !si->over ) {
1053                         if ( si->count < si->max ) {
1054                                 si->count++;
1055                                 e = entry_dup( rs->sr_entry );
1056                                 if ( !si->head ) si->head = e;
1057                                 if ( si->tail ) si->tail->e_private = e;
1058                                 si->tail = e;
1059                         } else {
1060                                 si->over = 1;
1061                                 si->count = 0;
1062                                 for (;si->head; si->head=e) {
1063                                         e = si->head->e_private;
1064                                         si->head->e_private = NULL;
1065                                         entry_free(si->head);
1066                                 }
1067                                 si->tail = NULL;
1068                         }
1069                 }
1070
1071         } else if ( rs->sr_type == REP_RESULT ) {
1072                 QueryTemplate* templ = si->qtemp;
1073                 if (( si->count && cache_entries( op, rs, &uuid ) == 0 ) ||
1074                         ( templ->negttl && !si->count && !si->over &&
1075                                 rs->sr_err == LDAP_SUCCESS )) {
1076                         qm->addfunc(qm, &si->query, si->qtemp,
1077                                 si->count ? &uuid : NULL);
1078
1079                         ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1080                         cm->num_cached_queries++;
1081                         Debug( pcache_debug, "STORED QUERIES = %lu\n",
1082                                         cm->num_cached_queries, 0, 0 );
1083                         ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1084
1085                         /* If the consistency checker suspended itself,
1086                          * wake it back up
1087                          */
1088                         if ( cm->cc_paused ) {
1089                                 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1090                                 if ( cm->cc_paused ) {
1091                                         cm->cc_paused = 0;
1092                                         ldap_pvt_runqueue_resched( &slapd_rq, cm->cc_arg, 0 );
1093                                 }
1094                                 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1095                         }
1096                 } else {
1097                         free( si->query.attrs );
1098                         filter_free( si->query.filter );
1099                 }
1100
1101                 /* free self */
1102                 op->o_callback->sc_cleanup = slap_freeself_cb;
1103         }
1104         return SLAP_CB_CONTINUE;
1105 }
1106
1107 static void
1108 add_filter_attrs(
1109         Operation *op,
1110         AttributeName** new_attrs,
1111         struct attr_set *attrs,
1112         AttributeName* filter_attrs,
1113         int fattr_cnt,
1114         int fattr_got_oc)
1115 {
1116         int alluser = 0;
1117         int allop = 0;
1118         int i, j;
1119         int count;
1120         int addoc = 0;
1121
1122         /* duplicate attrs */
1123         count = attrs->count + fattr_cnt;
1124         if ( !fattr_got_oc && !(attrs->flags & PC_GOT_OC)) {
1125                 addoc = 1;
1126                 count++;
1127         }
1128
1129         *new_attrs = (AttributeName*)ch_malloc((count+1)*
1130                 sizeof(AttributeName));
1131         for (i=0; i<attrs->count; i++) {
1132                 (*new_attrs)[i].an_name = attrs->attrs[i].an_name;
1133                 (*new_attrs)[i].an_desc = attrs->attrs[i].an_desc;
1134         }
1135         BER_BVZERO( &(*new_attrs)[i].an_name );
1136         alluser = an_find(*new_attrs, &AllUser);
1137         allop = an_find(*new_attrs, &AllOper);
1138
1139         j = i;
1140         for ( i=0; i<fattr_cnt; i++ ) {
1141                 if ( an_find(*new_attrs, &filter_attrs[i].an_name ))
1142                         continue;
1143                 if ( is_at_operational(filter_attrs[i].an_desc->ad_type) ) {
1144                         if (allop)
1145                                 continue;
1146                 } else if (alluser)
1147                         continue;
1148                 (*new_attrs)[j].an_name = filter_attrs[i].an_name;
1149                 (*new_attrs)[j].an_desc = filter_attrs[i].an_desc;
1150                 (*new_attrs)[j].an_oc = NULL;
1151                 (*new_attrs)[j].an_oc_exclude = 0;
1152                 j++;
1153         }
1154         if ( addoc ) {
1155                 (*new_attrs)[j].an_name = slap_schema.si_ad_objectClass->ad_cname;
1156                 (*new_attrs)[j].an_desc = slap_schema.si_ad_objectClass;
1157                 (*new_attrs)[j].an_oc = NULL;
1158                 (*new_attrs)[j].an_oc_exclude = 0;
1159                 j++;
1160         }
1161         BER_BVZERO( &(*new_attrs)[j].an_name );
1162 }
1163
1164 /* NOTE: this is a quick workaround to let pcache minimally interact
1165  * with pagedResults.  A more articulated solutions would be to
1166  * perform the remote query without control and cache all results,
1167  * performing the pagedResults search only within the client
1168  * and the proxy.  This requires pcache to understand pagedResults. */
1169 static int
1170 pcache_chk_controls(
1171         Operation       *op,
1172         SlapReply       *rs )
1173 {
1174         const char      *non = "";
1175         const char      *stripped = "";
1176
1177         switch( op->o_pagedresults ) {
1178         case SLAP_CONTROL_NONCRITICAL:
1179                 non = "non-";
1180                 stripped = "; stripped";
1181                 /* fallthru */
1182
1183         case SLAP_CONTROL_CRITICAL:
1184                 Debug( pcache_debug, "%s: "
1185                         "%scritical pagedResults control "
1186                         "disabled with proxy cache%s.\n",
1187                         op->o_log_prefix, non, stripped );
1188                 
1189                 slap_remove_control( op, rs, slap_cids.sc_pagedResults, NULL );
1190                 break;
1191
1192         default:
1193                 rs->sr_err = SLAP_CB_CONTINUE;
1194                 break;
1195         }
1196
1197         return rs->sr_err;
1198 }
1199
1200 static int
1201 pcache_op_search(
1202         Operation       *op,
1203         SlapReply       *rs )
1204 {
1205         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1206         cache_manager *cm = on->on_bi.bi_private;
1207         query_manager*          qm = cm->qm;
1208
1209         int i = -1;
1210
1211         AttributeName   *filter_attrs = NULL;
1212
1213         Query           query;
1214         QueryTemplate *qtemp = NULL;
1215
1216         int             attr_set = -1;
1217         CachedQuery     *answerable = NULL;
1218         int             cacheable = 0;
1219         int             fattr_cnt=0;
1220         int             fattr_got_oc = 0;
1221
1222         struct berval tempstr;
1223
1224         tempstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len+1, op->o_tmpmemctx );
1225         tempstr.bv_len = 0;
1226         if ( filter2template( op, op->ors_filter, &tempstr, &filter_attrs,
1227                 &fattr_cnt, &fattr_got_oc )) {
1228                 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
1229                 return SLAP_CB_CONTINUE;
1230         }
1231
1232         Debug( pcache_debug, "query template of incoming query = %s\n",
1233                                         tempstr.bv_val, 0, 0 );
1234
1235         /* FIXME: cannot cache/answer requests with pagedResults control */
1236         
1237
1238         /* find attr set */
1239         attr_set = get_attr_set(op->ors_attrs, qm, cm->numattrsets);
1240
1241         query.filter = op->ors_filter;
1242         query.attrs = op->ors_attrs;
1243         query.save_attrs = NULL;
1244         query.base = op->o_req_ndn;
1245         query.scope = op->ors_scope;
1246
1247         /* check for query containment */
1248         if (attr_set > -1) {
1249                 QueryTemplate *qt = qm->attr_sets[attr_set].templates;
1250                 for (; qt; qt = qt->qtnext ) {
1251                         /* find if template i can potentially answer tempstr */
1252                         if (qt->querystr.bv_len != tempstr.bv_len ||
1253                                 strcasecmp( qt->querystr.bv_val, tempstr.bv_val ))
1254                                 continue;
1255                         cacheable = 1;
1256                         qtemp = qt;
1257                         Debug( LDAP_DEBUG_NONE, "Entering QC, querystr = %s\n",
1258                                         op->ors_filterstr.bv_val, 0, 0 );
1259                         answerable = (*(qm->qcfunc))(op, qm, &query, qt);
1260
1261                         if (answerable)
1262                                 break;
1263                 }
1264         }
1265         op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
1266
1267         query.save_attrs = op->ors_attrs;
1268         query.attrs = NULL;
1269
1270         if (answerable) {
1271                 /* Need to clear the callbacks of the original operation,
1272                  * in case there are other overlays */
1273                 BackendDB       *save_bd = op->o_bd;
1274                 slap_callback   *save_cb = op->o_callback;
1275
1276                 Debug( pcache_debug, "QUERY ANSWERABLE\n", 0, 0, 0 );
1277                 op->o_tmpfree( filter_attrs, op->o_tmpmemctx );
1278                 ldap_pvt_thread_rdwr_runlock(qtemp->t_rwlock);
1279                 if ( BER_BVISNULL( &answerable->q_uuid )) {
1280                         /* No entries cached, just an empty result set */
1281                         i = rs->sr_err = 0;
1282                         send_ldap_result( op, rs );
1283                 } else {
1284                         op->o_bd = &cm->db;
1285                         op->o_callback = NULL;
1286                         i = cm->db.bd_info->bi_op_search( op, rs );
1287                 }
1288                 op->o_bd = save_bd;
1289                 op->o_callback = save_cb;
1290                 return i;
1291         }
1292
1293         Debug( pcache_debug, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
1294
1295         ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1296         if (cm->num_cached_queries >= cm->max_queries) {
1297                 cacheable = 0;
1298         }
1299         ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1300
1301         if (op->ors_attrsonly)
1302                 cacheable = 0;
1303
1304         if (cacheable) {
1305                 slap_callback           *cb;
1306                 struct search_info      *si;
1307
1308                 Debug( pcache_debug, "QUERY CACHEABLE\n", 0, 0, 0 );
1309                 query.filter = filter_dup(op->ors_filter, NULL);
1310                 add_filter_attrs(op, &query.attrs, &qm->attr_sets[attr_set],
1311                         filter_attrs, fattr_cnt, fattr_got_oc);
1312
1313                 op->ors_attrs = query.attrs;
1314
1315                 cb = op->o_tmpalloc( sizeof(*cb) + sizeof(*si), op->o_tmpmemctx);
1316                 cb->sc_response = pcache_response;
1317                 cb->sc_cleanup = NULL;
1318                 cb->sc_private = (cb+1);
1319                 si = cb->sc_private;
1320                 si->on = on;
1321                 si->query = query;
1322                 si->qtemp = qtemp;
1323                 si->max = cm->num_entries_limit ;
1324                 si->over = 0;
1325                 si->count = 0;
1326                 si->head = NULL;
1327                 si->tail = NULL;
1328
1329                 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
1330                         cb->sc_next = op->o_callback;
1331                         op->o_callback = cb;
1332
1333                 } else {
1334                         slap_callback           **pcb;
1335
1336                         /* need to move the callback at the end, in case other
1337                          * overlays are present, so that the final entry is
1338                          * actually cached */
1339                         cb->sc_next = NULL;
1340                         for ( pcb = &op->o_callback; *pcb; pcb = &(*pcb)->sc_next );
1341                         *pcb = cb;
1342                 }
1343
1344         } else {
1345                 Debug( pcache_debug, "QUERY NOT CACHEABLE\n",
1346                                         0, 0, 0);
1347         }
1348
1349         op->o_tmpfree( filter_attrs, op->o_tmpmemctx );
1350
1351         return SLAP_CB_CONTINUE;
1352 }
1353
1354 static int
1355 get_attr_set(
1356         AttributeName* attrs,
1357         query_manager* qm,
1358         int num )
1359 {
1360         int i;
1361         int count = 0;
1362
1363         if ( attrs ) {
1364                 for ( ; attrs[count].an_name.bv_val; count++ );
1365         }
1366
1367         for (i=0; i<num; i++) {
1368                 AttributeName *a2;
1369                 int found = 1;
1370
1371                 if ( count > qm->attr_sets[i].count )
1372                         continue;
1373                 if ( !count ) {
1374                         if ( !qm->attr_sets[i].count )
1375                                 break;
1376                         continue;
1377                 }
1378                 for ( a2 = attrs; a2->an_name.bv_val; a2++ ) {
1379                         if ( !an_find( qm->attr_sets[i].attrs, &a2->an_name )) {
1380                                 found = 0;
1381                                 break;
1382                         }
1383                 }
1384                 if ( found )
1385                         break;
1386         }
1387         if ( i == num )
1388                 i = -1;
1389         return i;
1390 }
1391
1392 static void*
1393 consistency_check(
1394         void *ctx,
1395         void *arg )
1396 {
1397         struct re_s *rtask = arg;
1398         slap_overinst *on = rtask->arg;
1399         cache_manager *cm = on->on_bi.bi_private;
1400         query_manager *qm = cm->qm;
1401         Connection conn = {0};
1402         OperationBuffer opbuf;
1403         Operation *op;
1404
1405         SlapReply rs = {REP_RESULT};
1406         CachedQuery* query, *query_prev;
1407         int i, return_val, pause = 1;
1408         QueryTemplate* templ;
1409
1410         op = (Operation *) &opbuf;
1411         connection_fake_init( &conn, op, ctx );
1412
1413         op->o_bd = &cm->db;
1414         op->o_dn = cm->db.be_rootdn;
1415         op->o_ndn = cm->db.be_rootndn;
1416
1417         cm->cc_arg = arg;
1418
1419         for (templ = qm->templates; templ; templ=templ->qmnext) {
1420                 query = templ->query_last;
1421                 if ( query ) pause = 0;
1422                 op->o_time = slap_get_time();
1423                 while (query && (query->expiry_time < op->o_time)) {
1424                         Debug( pcache_debug, "Lock CR index = %d\n",
1425                                         i, 0, 0 );
1426                         ldap_pvt_thread_rdwr_wlock(templ->t_rwlock);
1427                         remove_from_template(query, templ);
1428                         Debug( pcache_debug, "TEMPLATE %d QUERIES-- %d\n",
1429                                         i, templ->no_of_queries, 0 );
1430                         Debug( pcache_debug, "Unlock CR index = %d\n",
1431                                         i, 0, 0 );
1432                         ldap_pvt_thread_rdwr_wunlock(templ->t_rwlock);
1433                         ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1434                         remove_query(qm, query);
1435                         ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1436                         if ( BER_BVISNULL( &query->q_uuid ))
1437                                 return_val = 0;
1438                         else
1439                                 return_val = remove_query_data(op, &rs, &query->q_uuid);
1440                         Debug( pcache_debug, "STALE QUERY REMOVED, SIZE=%d\n",
1441                                                 return_val, 0, 0 );
1442                         ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1443                         cm->cur_entries -= return_val;
1444                         cm->num_cached_queries--;
1445                         Debug( pcache_debug, "STORED QUERIES = %lu\n",
1446                                         cm->num_cached_queries, 0, 0 );
1447                         ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1448                         Debug( pcache_debug,
1449                                 "STALE QUERY REMOVED, CACHE ="
1450                                 "%d entries\n",
1451                                 cm->cur_entries, 0, 0 );
1452                         query_prev = query;
1453                         query = query->prev;
1454                         free_query(query_prev);
1455                 }
1456         }
1457         ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1458         if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1459                 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1460         }
1461         /* If there were no queries, defer processing for a while */
1462         cm->cc_paused = pause;
1463         ldap_pvt_runqueue_resched( &slapd_rq, rtask, pause );
1464
1465         ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1466         return NULL;
1467 }
1468
1469
1470 #define MAX_ATTR_SETS 500
1471
1472 enum {
1473         PC_MAIN = 1,
1474         PC_ATTR,
1475         PC_TEMP,
1476         PC_RESP,
1477         PC_QUERIES
1478 };
1479
1480 static ConfigDriver pc_cf_gen;
1481 static ConfigLDAPadd pc_ldadd;
1482 static ConfigCfAdd pc_cfadd;
1483
1484 static ConfigTable pccfg[] = {
1485         { "proxycache", "backend> <max_entries> <numattrsets> <entry limit> "
1486                                 "<cycle_time",
1487                 6, 6, 0, ARG_MAGIC|ARG_NO_DELETE|PC_MAIN, pc_cf_gen,
1488                 "( OLcfgOvAt:2.1 NAME 'olcProxyCache' "
1489                         "DESC 'ProxyCache basic parameters' "
1490                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
1491         { "proxyattrset", "index> <attributes...",
1492                 2, 0, 0, ARG_MAGIC|PC_ATTR, pc_cf_gen,
1493                 "( OLcfgOvAt:2.2 NAME 'olcProxyAttrset' "
1494                         "DESC 'A set of attributes to cache' "
1495                         "SYNTAX OMsDirectoryString )", NULL, NULL },
1496         { "proxytemplate", "filter> <attrset-index> <TTL> <negTTL",
1497                 4, 5, 0, ARG_MAGIC|PC_TEMP, pc_cf_gen,
1498                 "( OLcfgOvAt:2.3 NAME 'olcProxyTemplate' "
1499                         "DESC 'Filter template, attrset, cache TTL, optional negative TTL' "
1500                         "SYNTAX OMsDirectoryString )", NULL, NULL },
1501         { "response-callback", "head|tail(default)",
1502                 2, 2, 0, ARG_MAGIC|PC_RESP, pc_cf_gen,
1503                 "( OLcfgOvAt:2.4 NAME 'olcProxyResponseCB' "
1504                         "DESC 'Response callback position in overlay stack' "
1505                         "SYNTAX OMsDirectoryString )", NULL, NULL },
1506         { "proxyCacheQueries", "queries",
1507                 2, 2, 0, ARG_INT|ARG_MAGIC|PC_QUERIES, pc_cf_gen,
1508                 "( OLcfgOvAt:2.5 NAME 'olcProxyCacheQueries' "
1509                         "DESC 'Maximum number of queries to cache' "
1510                         "SYNTAX OMsInteger )", NULL, NULL },
1511
1512         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1513 };
1514
1515 static ConfigOCs pcocs[] = {
1516         { "( OLcfgOvOc:2.1 "
1517                 "NAME 'olcPcacheConfig' "
1518                 "DESC 'ProxyCache configuration' "
1519                 "SUP olcOverlayConfig "
1520                 "MUST ( olcProxyCache $ olcProxyAttrset $ olcProxyTemplate ) "
1521                 "MAY ( olcProxyResponseCB $ olcProxyCacheQueries ) )", Cft_Overlay, pccfg, NULL, pc_cfadd },
1522         { "( OLcfgOvOc:2.2 "
1523                 "NAME 'olcPcacheDatabase' "
1524                 "DESC 'Cache database configuration' "
1525                 "AUXILIARY )", Cft_Misc, pccfg, pc_ldadd },
1526         { NULL, 0, NULL }
1527 };
1528
1529 static int
1530 pc_ldadd( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
1531 {
1532         slap_overinst *on;
1533         cache_manager *cm;
1534
1535         if ( p->ce_type != Cft_Overlay || !p->ce_bi ||
1536                 p->ce_bi->bi_cf_ocs != pcocs )
1537                 return LDAP_CONSTRAINT_VIOLATION;
1538
1539         on = (slap_overinst *)p->ce_bi;
1540         cm = on->on_bi.bi_private;
1541         ca->be = &cm->db;
1542         return LDAP_SUCCESS;
1543 }
1544
1545 static int
1546 pc_cfadd( Operation *op, SlapReply *rs, Entry *p, ConfigArgs *ca )
1547 {
1548         CfEntryInfo *pe = p->e_private;
1549         slap_overinst *on = (slap_overinst *)pe->ce_bi;
1550         cache_manager *cm = on->on_bi.bi_private;
1551         struct berval bv;
1552
1553         /* FIXME: should not hardcode "olcDatabase" here */
1554         bv.bv_len = sprintf( ca->msg, "olcDatabase=%s", cm->db.bd_info->bi_type );
1555         bv.bv_val = ca->msg;
1556         ca->be = &cm->db;
1557
1558         /* We can only create this entry if the database is table-driven
1559          */
1560         if ( cm->db.bd_info->bi_cf_ocs )
1561                 config_build_entry( op, rs, pe, ca, &bv, cm->db.bd_info->bi_cf_ocs,
1562                         &pcocs[1] );
1563
1564         return 0;
1565 }
1566
1567 static int
1568 pc_cf_gen( ConfigArgs *c )
1569 {
1570         slap_overinst   *on = (slap_overinst *)c->bi;
1571         cache_manager*  cm = on->on_bi.bi_private;
1572         query_manager*  qm = cm->qm;
1573         QueryTemplate*  temp;
1574         AttributeName*  attr_name;
1575         AttributeName*  attrarray;
1576         const char*     text=NULL;
1577         int             i, num, rc = 0;
1578         char            *ptr;
1579         unsigned long   t;
1580
1581         if ( c->op == SLAP_CONFIG_EMIT ) {
1582                 struct berval bv;
1583                 switch( c->type ) {
1584                 case PC_MAIN:
1585                         bv.bv_len = snprintf( c->msg, sizeof( c->msg ), "%s %d %d %d %ld",
1586                                 cm->db.bd_info->bi_type, cm->max_entries, cm->numattrsets,
1587                                 cm->num_entries_limit, cm->cc_period );
1588                         bv.bv_val = c->msg;
1589                         value_add_one( &c->rvalue_vals, &bv );
1590                         break;
1591                 case PC_ATTR:
1592                         for (i=0; i<cm->numattrsets; i++) {
1593                                 if ( !qm->attr_sets[i].count ) continue;
1594
1595                                 bv.bv_len = snprintf( c->msg, sizeof( c->msg ), "%d", i );
1596
1597                                 /* count the attr length */
1598                                 for ( attr_name = qm->attr_sets[i].attrs;
1599                                         attr_name->an_name.bv_val; attr_name++ )
1600                                         bv.bv_len += attr_name->an_name.bv_len + 1;
1601
1602                                 bv.bv_val = ch_malloc( bv.bv_len+1 );
1603                                 ptr = lutil_strcopy( bv.bv_val, c->msg );
1604                                 for ( attr_name = qm->attr_sets[i].attrs;
1605                                         attr_name->an_name.bv_val; attr_name++ ) {
1606                                         *ptr++ = ' ';
1607                                         ptr = lutil_strcopy( ptr, attr_name->an_name.bv_val );
1608                                 }
1609                                 ber_bvarray_add( &c->rvalue_vals, &bv );
1610                         }
1611                         if ( !c->rvalue_vals )
1612                                 rc = 1;
1613                         break;
1614                 case PC_TEMP:
1615                         for (temp=qm->templates; temp; temp=temp->qmnext) {
1616                                 if ( temp->negttl ) {
1617                                         bv.bv_len = snprintf( c->msg, sizeof( c->msg ),
1618                                                 " %d %ld %ld",
1619                                                 temp->attr_set_index,
1620                                                 temp->ttl,
1621                                                 temp->negttl );
1622                                 } else {
1623                                         bv.bv_len = snprintf( c->msg, sizeof( c->msg ), " %d %ld",
1624                                                 temp->attr_set_index,
1625                                                 temp->ttl );
1626                                 }
1627                                 bv.bv_len += temp->querystr.bv_len + 2;
1628                                 bv.bv_val = ch_malloc( bv.bv_len+1 );
1629                                 ptr = bv.bv_val;
1630                                 *ptr++ = '"';
1631                                 ptr = lutil_strcopy( ptr, temp->querystr.bv_val );
1632                                 *ptr++ = '"';
1633                                 strcpy( ptr, c->msg );
1634                                 ber_bvarray_add( &c->rvalue_vals, &bv );
1635                         }
1636                         if ( !c->rvalue_vals )
1637                                 rc = 1;
1638                         break;
1639                 case PC_RESP:
1640                         if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
1641                                 BER_BVSTR( &bv, "head" );
1642                         } else {
1643                                 BER_BVSTR( &bv, "tail" );
1644                         }
1645                         value_add_one( &c->rvalue_vals, &bv );
1646                         break;
1647                 case PC_QUERIES:
1648                         c->value_int = cm->max_queries;
1649                         break;
1650                 }
1651                 return rc;
1652         } else if ( c->op == LDAP_MOD_DELETE ) {
1653                 return 1;       /* FIXME */
1654 #if 0
1655                 switch( c->type ) {
1656                 case PC_ATTR:
1657                 case PC_TEMP:
1658                 }
1659                 return rc;
1660 #endif
1661         }
1662
1663         switch( c->type ) {
1664         case PC_MAIN:
1665                 if ( cm->numattrsets > 0 ) {
1666                         snprintf( c->msg, sizeof( c->msg ), "\"proxycache\" directive already provided" );
1667                         Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1668                         return( 1 );
1669                 }
1670
1671                 if ( lutil_atoi( &cm->numattrsets, c->argv[3] ) != 0 ) {
1672                         snprintf( c->msg, sizeof( c->msg ), "unable to parse num attrsets=\"%s\" (arg #3)",
1673                                 c->argv[3] );
1674                         Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1675                         return( 1 );
1676                 }
1677                 if ( cm->numattrsets <= 0 ) {
1678                         snprintf( c->msg, sizeof( c->msg ), "numattrsets (arg #3) must be positive" );
1679                         Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1680                         return( 1 );
1681                 }
1682                 if ( cm->numattrsets > MAX_ATTR_SETS ) {
1683                         snprintf( c->msg, sizeof( c->msg ), "numattrsets (arg #3) must be <= %d", MAX_ATTR_SETS );
1684                         Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1685                         return( 1 );
1686                 }
1687
1688                 if ( !backend_db_init( c->argv[1], &cm->db )) {
1689                         snprintf( c->msg, sizeof( c->msg ), "unknown backend type (arg #1)" );
1690                         Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1691                         return( 1 );
1692                 }
1693
1694                 if ( lutil_atoi( &cm->max_entries, c->argv[2] ) != 0 ) {
1695                         snprintf( c->msg, sizeof( c->msg ), "unable to parse max entries=\"%s\" (arg #2)",
1696                                 c->argv[2] );
1697                         Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1698                         return( 1 );
1699                 }
1700                 if ( cm->max_entries <= 0 ) {
1701                         snprintf( c->msg, sizeof( c->msg ), "max entries (arg #2) must be positive.\n" );
1702                         Debug( LDAP_DEBUG_CONFIG, "%s: %s\n", c->log, c->msg, 0 );
1703                         return( 1 );
1704                 }
1705
1706                 if ( lutil_atoi( &cm->num_entries_limit, c->argv[4] ) != 0 ) {
1707                         snprintf( c->msg, sizeof( c->msg ), "unable to parse entry limit=\"%s\" (arg #4)",
1708                                 c->argv[4] );
1709                         Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1710                         return( 1 );
1711                 }
1712                 if ( cm->num_entries_limit <= 0 ) {
1713                         snprintf( c->msg, sizeof( c->msg ), "entry limit (arg #4) must be positive" );
1714                         Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1715                         return( 1 );
1716                 }
1717                 if ( cm->num_entries_limit > cm->max_entries ) {
1718                         snprintf( c->msg, sizeof( c->msg ), "entry limit (arg #4) must be less than max entries %d (arg #2)", cm->max_entries );
1719                         Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1720                         return( 1 );
1721                 }
1722
1723                 if ( lutil_parse_time( c->argv[5], &t ) != 0 ) {
1724                         snprintf( c->msg, sizeof( c->msg ), "unable to parse period=\"%s\" (arg #5)",
1725                                 c->argv[5] );
1726                         Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1727                         return( 1 );
1728                 }
1729                 cm->cc_period = (time_t)t;
1730                 Debug( pcache_debug,
1731                                 "Total # of attribute sets to be cached = %d.\n",
1732                                 cm->numattrsets, 0, 0 );
1733                 qm->attr_sets = ( struct attr_set * )ch_calloc( cm->numattrsets,
1734                                                 sizeof( struct attr_set ) );
1735                 break;
1736         case PC_ATTR:
1737                 if ( cm->numattrsets == 0 ) {
1738                         snprintf( c->msg, sizeof( c->msg ), "\"proxycache\" directive not provided yet" );
1739                         Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1740                         return( 1 );
1741                 }
1742                 if ( lutil_atoi( &num, c->argv[1] ) != 0 ) {
1743                         snprintf( c->msg, sizeof( c->msg ), "unable to parse attrset #=\"%s\"",
1744                                 c->argv[1] );
1745                         Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1746                         return( 1 );
1747                 }
1748
1749                 if ( num < 0 || num >= cm->numattrsets ) {
1750                         snprintf( c->msg, sizeof( c->msg ), "attrset index %d out of bounds (must be %s%d)",
1751                                 num, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
1752                         Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1753                         return 1;
1754                 }
1755                 qm->attr_sets[num].flags |= PC_CONFIGURED;
1756                 if ( c->argc > 2 && strcmp( c->argv[2], "*" ) ) {
1757                         qm->attr_sets[num].count = c->argc - 2;
1758                         qm->attr_sets[num].attrs = (AttributeName*)ch_malloc(
1759                                                 (c->argc-1) * sizeof( AttributeName ));
1760                         attr_name = qm->attr_sets[num].attrs;
1761                         for ( i = 2; i < c->argc; i++ ) {
1762                                 attr_name->an_desc = NULL;
1763                                 if ( slap_str2ad( c->argv[i], 
1764                                                 &attr_name->an_desc, &text ) )
1765                                 {
1766                                         strcpy( c->msg, text );
1767                                         Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1768                                         ch_free( qm->attr_sets[num].attrs );
1769                                         qm->attr_sets[num].attrs = NULL;
1770                                         qm->attr_sets[num].count = 0;
1771                                         return 1;
1772                                 }
1773                                 attr_name->an_name = attr_name->an_desc->ad_cname;
1774                                 attr_name->an_oc = NULL;
1775                                 attr_name->an_oc_exclude = 0;
1776                                 if ( attr_name->an_desc == slap_schema.si_ad_objectClass )
1777                                         qm->attr_sets[num].flags |= PC_GOT_OC;
1778                                 attr_name++;
1779                                 BER_BVZERO( &attr_name->an_name );
1780                         }
1781                 }
1782                 break;
1783         case PC_TEMP:
1784                 if ( cm->numattrsets == 0 ) {
1785                         snprintf( c->msg, sizeof( c->msg ), "\"proxycache\" directive not provided yet" );
1786                         Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1787                         return( 1 );
1788                 }
1789                 if ( lutil_atoi( &i, c->argv[2] ) != 0 ) {
1790                         snprintf( c->msg, sizeof( c->msg ), "unable to parse template #=\"%s\"",
1791                                 c->argv[2] );
1792                         Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1793                         return( 1 );
1794                 }
1795
1796                 if ( i < 0 || i >= cm->numattrsets || 
1797                         !(qm->attr_sets[i].flags & PC_CONFIGURED )) {
1798                         snprintf( c->msg, sizeof( c->msg ), "template index %d invalid (%s%d)",
1799                                 i, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
1800                         Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1801                         return 1;
1802                 }
1803                 temp = ch_calloc( 1, sizeof( QueryTemplate ));
1804                 temp->qmnext = qm->templates;
1805                 qm->templates = temp;
1806                 temp->t_rwlock = ch_malloc( sizeof( ldap_pvt_thread_rdwr_t ) );
1807                 ldap_pvt_thread_rdwr_init( temp->t_rwlock );
1808                 temp->query = temp->query_last = NULL;
1809                 if ( lutil_parse_time( c->argv[3], &t ) != 0 ) {
1810                         snprintf( c->msg, sizeof( c->msg ), "unable to parse template ttl=\"%s\"",
1811                                 c->argv[3] );
1812                         Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1813                         return( 1 );
1814                 }
1815                 temp->ttl = (time_t)t;
1816                 if ( c->argc == 5 ) {
1817                         if ( lutil_parse_time( c->argv[4], &t ) != 0 ) {
1818                                 snprintf( c->msg, sizeof( c->msg ),
1819                                         "unable to parse template negttl=\"%s\"",
1820                                         c->argv[4] );
1821                                 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1822                                         return( 1 );
1823                         }
1824                         temp->negttl = (time_t)t;
1825                 } else {
1826                         temp->negttl = 0;
1827                 }
1828
1829                 temp->no_of_queries = 0;
1830
1831                 ber_str2bv( c->argv[1], 0, 1, &temp->querystr );
1832                 Debug( pcache_debug, "Template:\n", 0, 0, 0 );
1833                 Debug( pcache_debug, "  query template: %s\n",
1834                                 temp->querystr.bv_val, 0, 0 );
1835                 temp->attr_set_index = i;
1836                 qm->attr_sets[i].flags |= PC_REFERENCED;
1837                 temp->qtnext = qm->attr_sets[i].templates;
1838                 qm->attr_sets[i].templates = temp;
1839                 Debug( pcache_debug, "  attributes: \n", 0, 0, 0 );
1840                 if ( ( attrarray = qm->attr_sets[i].attrs ) != NULL ) {
1841                         for ( i=0; attrarray[i].an_name.bv_val; i++ )
1842                                 Debug( pcache_debug, "\t%s\n",
1843                                         attrarray[i].an_name.bv_val, 0, 0 );
1844                 }
1845                 temp++; 
1846                 temp->querystr.bv_val = NULL;
1847                 break;
1848         case PC_RESP:
1849                 if ( strcasecmp( c->argv[1], "head" ) == 0 ) {
1850                         cm->response_cb = PCACHE_RESPONSE_CB_HEAD;
1851
1852                 } else if ( strcasecmp( c->argv[1], "tail" ) == 0 ) {
1853                         cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
1854
1855                 } else {
1856                         snprintf( c->msg, sizeof( c->msg ), "unknown specifier" );
1857                         Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1858                         return 1;
1859                 }
1860                 break;
1861         case PC_QUERIES:
1862                 if ( c->value_int <= 0 ) {
1863                         snprintf( c->msg, sizeof( c->msg ), "max queries must be positive" );
1864                         Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1865                         return( 1 );
1866                 }
1867                 cm->max_queries = c->value_int;
1868                 break;
1869         }
1870         return rc;
1871 }
1872
1873 static int
1874 pcache_db_config(
1875         BackendDB       *be,
1876         const char      *fname,
1877         int             lineno,
1878         int             argc,
1879         char            **argv
1880 )
1881 {
1882         slap_overinst   *on = (slap_overinst *)be->bd_info;
1883         cache_manager*  cm = on->on_bi.bi_private;
1884
1885         /* Something for the cache database? */
1886         if ( cm->db.bd_info && cm->db.bd_info->bi_db_config )
1887                 return cm->db.bd_info->bi_db_config( &cm->db, fname, lineno,
1888                         argc, argv );
1889         return SLAP_CONF_UNKNOWN;
1890 }
1891
1892 static int
1893 pcache_db_init(
1894         BackendDB *be
1895 )
1896 {
1897         slap_overinst *on = (slap_overinst *)be->bd_info;
1898         cache_manager *cm;
1899         query_manager *qm;
1900
1901         cm = (cache_manager *)ch_malloc(sizeof(cache_manager));
1902         on->on_bi.bi_private = cm;
1903
1904         qm = (query_manager*)ch_malloc(sizeof(query_manager));
1905
1906         cm->db = *be;
1907         SLAP_DBFLAGS(&cm->db) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
1908         cm->db.be_private = NULL;
1909         cm->db.be_pcl_mutexp = &cm->db.be_pcl_mutex;
1910         cm->qm = qm;
1911         cm->numattrsets = 0;
1912         cm->num_entries_limit = 5;
1913         cm->num_cached_queries = 0;
1914         cm->max_entries = 0;
1915         cm->cur_entries = 0;
1916         cm->max_queries = 10000;
1917         cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
1918         cm->cc_period = 1000;
1919         cm->cc_paused = 0;
1920
1921         qm->attr_sets = NULL;
1922         qm->templates = NULL;
1923         qm->lru_top = NULL;
1924         qm->lru_bottom = NULL;
1925
1926         qm->qcfunc = query_containment;
1927         qm->crfunc = cache_replacement;
1928         qm->addfunc = add_query;
1929         ldap_pvt_thread_mutex_init(&qm->lru_mutex);
1930
1931         ldap_pvt_thread_mutex_init(&cm->cache_mutex);
1932         return 0;
1933 }
1934
1935 static int
1936 pcache_db_open(
1937         BackendDB *be
1938 )
1939 {
1940         slap_overinst   *on = (slap_overinst *)be->bd_info;
1941         cache_manager   *cm = on->on_bi.bi_private;
1942         query_manager*  qm = cm->qm;
1943         int             i, ncf = 0, rf = 0, nrf = 0, rc = 0;
1944
1945         /* check attr sets */
1946         for ( i = 0; i < cm->numattrsets; i++) {
1947                 if ( !( qm->attr_sets[i].flags & PC_CONFIGURED ) ) {
1948                         if ( qm->attr_sets[i].flags & PC_REFERENCED ) {
1949                                 Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d not configured but referenced.\n", i, 0, 0 );
1950                                 rf++;
1951
1952                         } else {
1953                                 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, attr set #%d not configured.\n", i, 0, 0 );
1954                         }
1955                         ncf++;
1956
1957                 } else if ( !( qm->attr_sets[i].flags & PC_REFERENCED ) ) {
1958                         Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d configured but not referenced.\n", i, 0, 0 );
1959                         nrf++;
1960                 }
1961         }
1962
1963         if ( ncf || rf || nrf ) {
1964                 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets configured but not referenced.\n", nrf, 0, 0 );
1965                 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets not configured.\n", ncf, 0, 0 );
1966                 Debug( LDAP_DEBUG_CONFIG, "pcache: %d attr sets not configured but referenced.\n", rf, 0, 0 );
1967
1968                 if ( rf > 0 ) {
1969                         return 1;
1970                 }
1971         }
1972
1973         /* need to inherit something from the original database... */
1974         cm->db.be_def_limit = be->be_def_limit;
1975         cm->db.be_limits = be->be_limits;
1976         cm->db.be_acl = be->be_acl;
1977         cm->db.be_dfltaccess = be->be_dfltaccess;
1978
1979         rc = backend_startup_one( &cm->db );
1980
1981         /* There is no runqueue in TOOL mode */
1982         if ( slapMode & SLAP_SERVER_MODE ) {
1983                 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1984                 ldap_pvt_runqueue_insert( &slapd_rq, cm->cc_period,
1985                         consistency_check, on,
1986                         "pcache_consistency", be->be_suffix[0].bv_val );
1987                 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1988
1989                 /* Cached database must have the rootdn */
1990                 if ( BER_BVISNULL( &cm->db.be_rootndn )
1991                                 || BER_BVISEMPTY( &cm->db.be_rootndn ) )
1992                 {
1993                         Debug( LDAP_DEBUG_ANY, "pcache_db_open(): "
1994                                 "underlying database of type \"%s\"\n"
1995                                 "    serving naming context \"%s\"\n"
1996                                 "    has no \"rootdn\", required by \"proxycache\".\n",
1997                                 on->on_info->oi_orig->bi_type,
1998                                 cm->db.be_suffix[0].bv_val, 0 );
1999                         return 1;
2000                 }
2001         }
2002
2003         return rc;
2004 }
2005
2006 static int
2007 pcache_db_close(
2008         BackendDB *be
2009 )
2010 {
2011         slap_overinst *on = (slap_overinst *)be->bd_info;
2012         cache_manager *cm = on->on_bi.bi_private;
2013         query_manager *qm = cm->qm;
2014         QueryTemplate *tm = qm->templates;
2015         int i, rc = 0;
2016
2017         /* cleanup stuff inherited from the original database... */
2018         cm->db.be_limits = NULL;
2019         cm->db.be_acl = NULL;
2020
2021         if ( cm->db.bd_info->bi_db_close ) {
2022                 rc = cm->db.bd_info->bi_db_close( &cm->db );
2023         }
2024         for ( ; tm; tm=tm->qmnext ) {
2025                 CachedQuery *qc, *qn;
2026                 for ( qc = tm->query; qc; qc = qn ) {
2027                         qn = qc->next;
2028                         free_query( qc );
2029                 }
2030                 free( tm->querystr.bv_val );
2031                 ldap_pvt_thread_rdwr_destroy( tm->t_rwlock );
2032                 ch_free( tm->t_rwlock );
2033         }
2034         free( qm->templates );
2035         qm->templates = NULL;
2036
2037         for ( i=0; i<cm->numattrsets; i++ ) {
2038                 free( qm->attr_sets[i].attrs );
2039         }
2040         free( qm->attr_sets );
2041         qm->attr_sets = NULL;
2042
2043         return rc;
2044 }
2045
2046 static int
2047 pcache_db_destroy(
2048         BackendDB *be
2049 )
2050 {
2051         slap_overinst *on = (slap_overinst *)be->bd_info;
2052         cache_manager *cm = on->on_bi.bi_private;
2053         query_manager *qm = cm->qm;
2054
2055         /* cleanup stuff inherited from the original database... */
2056         cm->db.be_suffix = NULL;
2057         cm->db.be_nsuffix = NULL;
2058         BER_BVZERO( &cm->db.be_rootdn );
2059         BER_BVZERO( &cm->db.be_rootndn );
2060         BER_BVZERO( &cm->db.be_rootpw );
2061         /* FIXME: there might be more... */
2062
2063         if ( cm->db.be_private != NULL ) {
2064                 backend_destroy_one( &cm->db, 0 );
2065         }
2066
2067         ldap_pvt_thread_mutex_destroy( &qm->lru_mutex );
2068         ldap_pvt_thread_mutex_destroy( &cm->cache_mutex );
2069         free( qm );
2070         free( cm );
2071
2072         return 0;
2073 }
2074
2075 static slap_overinst pcache;
2076
2077 static char *obsolete_names[] = {
2078         "proxycache",
2079         NULL
2080 };
2081
2082 int pcache_initialize()
2083 {
2084         LDAPAttributeType *at;
2085         int code;
2086         const char *err;
2087         struct berval debugbv = BER_BVC("pcache");
2088
2089         if (( code = slap_loglevel_get( &debugbv, &pcache_debug )))
2090                 return code;
2091
2092         at = ldap_str2attributetype( queryid_schema, &code, &err,
2093                 LDAP_SCHEMA_ALLOW_ALL );
2094         if ( !at ) {
2095                 Debug( LDAP_DEBUG_ANY,
2096                         "pcache_initialize: ldap_str2attributetype failed %s %s\n",
2097                         ldap_scherr2str(code), err, 0 );
2098                 return code;
2099         }
2100         code = at_add( at, 0, NULL, &err );
2101         if ( !code ) {
2102                 slap_str2ad( at->at_names[0], &ad_queryid, &err );
2103         }
2104         ldap_memfree( at );
2105         if ( code ) {
2106                 Debug( LDAP_DEBUG_ANY,
2107                         "pcache_initialize: at_add failed %s %s\n",
2108                         scherr2str(code), err, 0 );
2109                 return code;
2110         }
2111
2112         pcache.on_bi.bi_type = "pcache";
2113         pcache.on_bi.bi_obsolete_names = obsolete_names;
2114         pcache.on_bi.bi_db_init = pcache_db_init;
2115         pcache.on_bi.bi_db_config = pcache_db_config;
2116         pcache.on_bi.bi_db_open = pcache_db_open;
2117         pcache.on_bi.bi_db_close = pcache_db_close;
2118         pcache.on_bi.bi_db_destroy = pcache_db_destroy;
2119
2120         pcache.on_bi.bi_op_search = pcache_op_search;
2121
2122         pcache.on_bi.bi_chk_controls = pcache_chk_controls;
2123
2124         pcache.on_bi.bi_cf_ocs = pcocs;
2125
2126         code = config_register_schema( pccfg, pcocs );
2127         if ( code ) return code;
2128
2129         return overlay_register( &pcache );
2130 }
2131
2132 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
2133 int init_module(int argc, char *argv[]) {
2134         return pcache_initialize();
2135 }
2136 #endif
2137
2138 #endif  /* defined(SLAPD_OVER_PROXYCACHE) */