]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/cache-search.c
Proxy caching update : op->o_caching_on flag removed
[openldap] / servers / slapd / back-meta / cache-search.c
1 /*
2  * Copyright (c) 2003 by International Business Machines, Inc.
3  *
4  * International Business Machines, Inc. (hereinafter called IBM) grants
5  * permission under its copyrights to use, copy, modify, and distribute this
6  * Software with or without fee, provided that the above copyright notice and
7  * all paragraphs of this notice appear in all copies, and that the name of IBM
8  * not be used in connection with the marketing of any product incorporating
9  * the Software or modifications thereof, without specific, written prior
10  * permission.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
13  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
14  * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
15  * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
17  * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
18  *
19  * 
20  * This software is based on the backends back-ldap and back-meta, implemented
21  * by Howard Chu <hyc@highlandsun.com>, Mark Valence
22  * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
23  * contributors. 
24  *
25  * The original copyright statements follow. 
26  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
27  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
28  *
29  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
30  *
31  * This work has been developed to fulfill the requirements
32  * of SysNet s.n.c. <http:www.sys-net.it> and it has been donated
33  * to the OpenLDAP Foundation in the hope that it may be useful
34  * to the Open Source community, but WITHOUT ANY WARRANTY.
35  *
36  * Permission is granted to anyone to use this software for any purpose
37  * on any computer system, and to alter it and redistribute it, subject
38  * to the following restrictions:
39  *
40  * 1. The author and SysNet s.n.c. are not responsible for the consequences
41  *    of use of this software, no matter how awful, even if they arise from 
42  *    flaws in it.
43  *
44  * 2. The origin of this software must not be misrepresented, either by
45  *    explicit claim or by omission.  Since few users ever read sources,
46  *    credits should appear in the documentation.
47  *
48  * 3. Altered versions must be plainly marked as such, and must not be
49  *    misrepresented as being the original software.  Since few users
50  *    ever read sources, credits should appear in the documentation.
51  *    SysNet s.n.c. cannot be responsible for the consequences of the
52  *    alterations.
53  *
54  * 4. This notice may not be removed or altered.
55  *
56  *
57  * This software is based on the backend back-ldap, implemented
58  * by Howard Chu <hyc@highlandsun.com>, and modified by Mark Valence
59  * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
60  * contributors. The contribution of the original software to the present
61  * implementation is acknowledged in this copyright statement.
62  *
63  * A special acknowledgement goes to Howard for the overall architecture
64  * (and for borrowing large pieces of code), and to Mark, who implemented
65  * from scratch the attribute/objectclass mapping.
66  *
67  * The original copyright statement follows.
68  *
69  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
70  *
71  * Permission is granted to anyone to use this software for any purpose
72  * on any computer system, and to alter it and redistribute it, subject
73  * to the following restrictions:
74  *
75  * 1. The author is not responsible for the consequences of use of this
76  *    software, no matter how awful, even if they arise from flaws in it.
77  *
78  * 2. The origin of this software must not be misrepresented, either by
79  *    explicit claim or by omission.  Since few users ever read sources,
80  *    credits should appear in the documentation.
81  *
82  * 3. Altered versions must be plainly marked as such, and must not be
83  *    misrepresented as being the original software.  Since few users
84  *    ever read sources, credits should appear in the
85  *    documentation.
86  *
87  * 4. This notice may not be removed or altered.
88  *                
89  */
90 #include "portable.h"
91
92 #include <stdio.h>
93
94 #include <ac/socket.h>
95 #include <ac/string.h>
96 #include <ac/time.h>
97
98 #include "ldap_pvt.h"
99 #include "lutil.h"
100 #include "slap.h"
101 #include "../back-ldap/back-ldap.h"
102 #include "back-meta.h"
103 #undef ldap_debug       /* silence a warning in ldap-int.h */
104 #include "ldap_log.h"
105 #include "../../../libraries/libldap/ldap-int.h"
106
107 #ifdef LDAP_CACHING 
108 static Entry* 
109 meta_create_entry(
110         Backend         *be,
111         struct metaconn *lc,
112         int             target,
113         LDAPMessage     *e,
114         struct exception* result
115 ); 
116
117 static int
118 is_one_level_rdn(
119         const char      *rdn,
120         int             from
121 );
122
123 static struct metaconn*  
124 metaConnect(
125         Operation               *op, 
126         SlapReply               *rs,
127         int                     op_type, 
128         struct berval           *nbase, 
129         struct exception        *result 
130 );
131
132 static void
133 add_filter_attrs(
134         AttributeName** newattrs, 
135         AttributeName* attrs, 
136         AttributeName* filter_attrs
137 );
138
139 static int 
140 handleLdapResult(
141         struct metaconn* lc, 
142         Operation* op, 
143         SlapReply *rs,
144         int* msgid, Backend* be, 
145         AttributeName* attrs, 
146         int attrsonly, 
147         int candidates, 
148         int cacheable, 
149         Entry*** entry_array, 
150         int curr_limit, 
151         int slimit,
152         struct exception* result
153 );
154
155 static Entry* 
156 get_result_entry(
157         Backend* be,
158         struct metaconn* lc, 
159         struct metasingleconn* lsc, 
160         int* msgid,
161         int i, 
162         struct timeval* tv, 
163         struct exception* result
164 ); 
165
166 static void
167 rewriteSession(
168         struct rewrite_info* info, 
169         const char* rewriteContext, 
170         const char* string, 
171         const void* cookie,  
172         char** base, 
173         struct exception* result
174 );
175
176 static int 
177 get_attr_set(
178         AttributeName* attrs, 
179         query_manager* qm, 
180         int num
181 );
182
183 static int 
184 attrscmp(
185         AttributeName* attrs_in, 
186         AttributeName* attrs
187 );
188
189 static char* 
190 cache_entries(
191         Operation       *op,
192         SlapReply       *rs,
193         Entry** entry_array, 
194         cache_manager* cm, 
195         struct exception* result
196 ); 
197
198 static int 
199 is_temp_answerable(
200         int attr_set, 
201         struct berval* tempstr, 
202         query_manager* qm, 
203         int template_id
204 );
205
206 static void*
207 consistency_check(
208         void    *op
209 ); 
210
211 static int
212 cache_back_sentry(
213         Operation* op, 
214         SlapReply *rs
215 );
216
217
218 int
219 meta_back_cache_search(
220         Operation       *op,
221         SlapReply       *rs )
222         /*
223         Backend         *be,
224         Connection      *conn,
225         Operation       *op,
226         struct berval   *base,
227         struct berval   *nbase,
228         int             scope,
229         int             deref,
230         int             slimit,
231         int             tlimit,
232         Filter          *filt,
233         struct berval   *filterstr,
234         AttributeName   *attributes,
235         int             attrsonly
236 ) */
237 {
238         struct metainfo         *li = ( struct metainfo * )op->o_bd->be_private;
239         struct metaconn         *lc;
240         struct metasingleconn   *lsc;
241         cache_manager*          cm = li->cm; 
242         query_manager*          qm = cm->qm; 
243
244         Operation               *oper;
245
246         time_t                  curr_time; 
247
248         int count, rc = 0, *msgid = NULL; 
249         char *mbase = NULL;
250         char *cbase = NULL; 
251         char *uuid; 
252             
253         int i = -1, last = 0, candidates = 0, op_type;
254
255         struct berval   mfilter;
256         struct berval   cachebase = { 0L, NULL };  
257         struct berval   ncachebase = { 0L, NULL };  
258         struct berval   cache_suffix; 
259         struct berval   tempstr = { 0L, NULL }; 
260
261         AttributeName   *filter_attrs = NULL; 
262         AttributeName   *new_attrs = NULL; 
263         AttributeName   *attrs = NULL; 
264
265         Entry           *e;
266         Entry           **entry_array = NULL;
267
268         Query           query; 
269
270         int             attr_set = -1; 
271         int             template_id = -1; 
272         int             answerable = 0; 
273         int             cacheable = 0; 
274         int             num_entries = 0;
275         int             curr_limit;
276         int             fattr_cnt=0; 
277         int             oc_attr_absent = 1;
278
279         struct exception result[1]; 
280
281         Filter* filter = str2filter(op->ors_filterstr.bv_val); 
282         slap_callback cb = {cache_back_sentry, NULL}; 
283
284         cb.sc_private = op->o_bd; 
285
286         if (op->ors_attrs) {
287                 for ( count=0; op->ors_attrs[ count ].an_name.bv_val; count++ ) {
288                         if ( op->ors_attrs[count].an_desc == slap_schema.si_ad_objectClass )
289                                 oc_attr_absent = 0;
290                 }
291                 attrs = (AttributeName*)malloc( ( count + 1 + oc_attr_absent )
292                                                                 *sizeof(AttributeName));
293                 for ( count=0; op->ors_attrs[ count ].an_name.bv_val; count++ ) {
294                         ber_dupbv(&attrs[ count ].an_name,
295                                                 &op->ors_attrs[ count ].an_name);
296                         attrs[count].an_desc = op->ors_attrs[count].an_desc; 
297                 }
298                 attrs[ count ].an_name.bv_val = NULL;
299                 attrs[ count ].an_name.bv_len = 0;
300         }
301
302         result->type = SUCCESS; 
303         result->rc = 0; 
304         ldap_pvt_thread_mutex_lock(&cm->cache_mutex); 
305         cm->threads++; 
306 #ifdef NEW_LOGGING
307         LDAP_LOG( BACK_META, DETAIL1, "Threads++ = %d\n", cm->threads, 0, 0 );
308 #else /* !NEW_LOGGING */
309         Debug( LDAP_DEBUG_ANY, "Threads++ = %d\n", cm->threads, 0, 0 );
310 #endif /* !NEW_LOGGING */
311         ldap_pvt_thread_mutex_unlock(&cm->cache_mutex); 
312         
313         ldap_pvt_thread_mutex_lock(&cm->cc_mutex); 
314         if (!cm->cc_thread_started) {
315                 oper = (Operation*)malloc(sizeof(Operation)); 
316                 *oper = *op; 
317                 cm->cc_thread_started = 1; 
318                 ldap_pvt_thread_create(&(cm->cc_thread), 1, consistency_check, (void*)oper); 
319         }       
320         ldap_pvt_thread_mutex_unlock(&cm->cc_mutex); 
321
322         filter2template(filter, &tempstr, &filter_attrs, &fattr_cnt, result);  
323         if (result->type != SUCCESS) 
324                 goto Catch; 
325
326 #ifdef NEW_LOGGING
327         LDAP_LOG( BACK_META, DETAIL1, "query template of incoming query = %s\n",
328                                         tempstr.bv_val, 0, 0 );
329 #else /* !NEW_LOGGING */
330         Debug( LDAP_DEBUG_ANY, "query template of incoming query = %s\n",
331                                         tempstr.bv_val, 0, 0 );
332 #endif /* !NEW_LOGGING */
333         curr_limit = cm->num_entries_limit ;
334
335         /* find attr set */     
336         attr_set = get_attr_set(attrs, qm, cm->numattrsets); 
337     
338         query.filter = filter; 
339         query.attrs = attrs; 
340         query.base = op->o_req_dn; 
341         query.scope = op->ors_scope; 
342
343         /* check for query containment */
344         if (attr_set > -1) {
345                 for (i=0; i<cm->numtemplates; i++) {
346                         /* find if template i can potentially answer tempstr */
347                         if (!is_temp_answerable(attr_set, &tempstr, qm, i)) 
348                                 continue; 
349                         if (attr_set == qm->templates[i].attr_set_index) {
350                                 cacheable = 1; 
351                                 template_id = i; 
352                         }
353 #ifdef NEW_LOGGING
354                         LDAP_LOG( BACK_META, DETAIL2,
355                                         "Entering QC, querystr = %s\n",
356                                         op->ors_filterstr.bv_val, 0, 0 );
357 #else /* !NEW_LOGGING */
358                         Debug( LDAP_DEBUG_NONE, "Entering QC, querystr = %s\n",
359                                         op->ors_filterstr.bv_val, 0, 0 );
360 #endif /* !NEW_LOGGING */
361                         answerable = (*(qm->qcfunc))(qm, &query, i);
362
363                         if (answerable)
364                                 break;
365                 }
366         }
367
368         if ( attrs && oc_attr_absent ) {
369                 for ( count = 0; attrs[count].an_name.bv_val; count++) ;
370                 attrs[ count ].an_name.bv_val = "objectClass";
371                 attrs[ count ].an_name.bv_len = strlen( "objectClass" );
372                 attrs[ count ].an_desc = slap_schema.si_ad_objectClass;
373                 attrs[ count + 1 ].an_name.bv_val = NULL;
374                 attrs[ count + 1 ].an_name.bv_len = 0;
375         }
376
377         if (answerable) {
378                 Operation       op_tmp;
379
380 #ifdef NEW_LOGGING
381                 LDAP_LOG( BACK_META, DETAIL1, "QUERY ANSWERABLE\n", 0, 0, 0 );
382 #else /* !NEW_LOGGING */
383                 Debug( LDAP_DEBUG_ANY, "QUERY ANSWERABLE\n", 0, 0, 0 );
384 #endif /* !NEW_LOGGING */
385                 rewriteSession(li->rwinfo, "cacheBase", op->o_req_dn.bv_val,
386                                         op->o_conn, &cbase, result); 
387                 if (result->type != SUCCESS) { 
388                         ldap_pvt_thread_rdwr_runlock(&qm->templates[i].t_rwlock); 
389                         goto Catch; 
390                 }
391                 if ( cbase == NULL ) {
392                         cachebase = op->o_req_dn;
393                 } else {
394                         cachebase.bv_val = cbase;
395                         cachebase.bv_len = strlen(cbase);
396                 }
397                 dnNormalize(0, NULL, NULL, &cachebase, &ncachebase,
398                                 op->o_tmpmemctx); 
399
400                 /* FIXME: safe default? */
401                 op_tmp = *op;
402
403                 op_tmp.o_bd = li->glue_be;
404                 op_tmp.o_req_dn = cachebase;
405                 op_tmp.o_req_ndn = ncachebase;
406
407                 op_tmp.o_callback = &cb; 
408
409                 li->glue_be->be_search(&op_tmp, rs);
410                 free( ncachebase.bv_val );
411                 if ( cachebase.bv_val != op->o_req_dn.bv_val ) {
412                         /* free only if rewritten */
413                         free( cachebase.bv_val );
414                 }
415
416                 ldap_pvt_thread_rdwr_runlock(&qm->templates[i].t_rwlock); 
417         } else {
418                 Operation       op_tmp;
419                 op_tmp = *op;
420 #ifdef NEW_LOGGING
421                 LDAP_LOG( BACK_META, DETAIL1, "QUERY NOT ANSWERABLE\n",
422                                         0, 0, 0 );
423 #else /* !NEW_LOGGING */
424                 Debug( LDAP_DEBUG_ANY, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
425 #endif /* !NEW_LOGGING */
426
427                 if ( op->ors_scope == LDAP_SCOPE_BASE ) {
428                         op_type = META_OP_REQUIRE_SINGLE;
429                 } else {
430                         op_type = META_OP_ALLOW_MULTIPLE;
431                 }
432
433                 lc = metaConnect(&op_tmp, rs, op_type,
434                                 &op->o_req_ndn, result);
435
436                 if (result->type != SUCCESS) 
437                         goto Catch; 
438
439                 ldap_pvt_thread_mutex_lock(&cm->cache_mutex); 
440                 if (cm->num_cached_queries >= cm->max_queries) {
441                         cacheable = 0; 
442                 }
443                 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex); 
444                 
445                 if (cacheable) {
446                         add_filter_attrs(&new_attrs, attrs, filter_attrs);
447                 } else {
448                         new_attrs = attrs; 
449                 }
450
451                 free(filter_attrs); 
452         
453                 /*
454                  * Array of message id of each target
455                  */
456                 msgid = ch_calloc( sizeof( int ), li->ntargets );
457                 if ( msgid == NULL ) {
458                         result->type = CONN_ERR; 
459                         goto Catch; 
460                 }
461
462                 /*
463                 if (slimit > 0 &&  (slimit <= cm->num_entries_limit))  
464                         slimit = cm->num_entries_limit; 
465                 */
466
467                 /*
468                  * Inits searches
469                  */
470
471                 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
472                         char    *realbase = ( char * )op->o_req_dn.bv_val;
473                         int     realscope = op->ors_scope;
474                         ber_len_t suffixlen;
475                         char    *mapped_filter, **mapped_attrs;
476
477                         /* FIXME: Check for more than one targets */
478                         if ( meta_back_is_candidate(
479                                         &li->targets[i]->suffix,
480                                         &op->o_req_ndn ))
481                                 lsc->candidate = META_CANDIDATE; 
482
483                         if ( lsc->candidate != META_CANDIDATE ) 
484                                 continue;
485
486                         if ( op->ors_deref != -1 ) {
487                                 ldap_set_option( lsc->ld, LDAP_OPT_DEREF,
488                                                 ( void * )&op->ors_deref);
489                         }
490                         if ( op->ors_tlimit != -1 ) {
491                                 ldap_set_option( lsc->ld, LDAP_OPT_TIMELIMIT,
492                                                 ( void * )&op->ors_tlimit);
493                         }
494                         if ( op->ors_slimit != -1 ) {
495                                 ldap_set_option( lsc->ld, LDAP_OPT_SIZELIMIT,
496                                                 ( void * )&op->ors_slimit);
497                         }
498
499                         /*
500                          * modifies the base according to the scope, if required
501                          */
502                         suffixlen = li->targets[ i ]->suffix.bv_len;
503                         if ( suffixlen > op->o_req_ndn.bv_len ) {
504                                 switch ( op->ors_scope ) {
505                                 case LDAP_SCOPE_SUBTREE:
506                                         /*
507                                          * make the target suffix the new base
508                                          * FIXME: this is very forgiving,
509                                          * because illegal bases may be turned
510                                          * into the suffix of the target.
511                                          */
512                                         if ( dnIsSuffix(
513                                                 &li->targets[ i ]->suffix,
514                                                 &op->o_req_ndn ) ) {
515                                                 realbase =
516                                                 li->targets[i]->suffix.bv_val;
517                                         } else {
518                                                 /*
519                                                  * this target is no longer
520                                                  * candidate
521                                                  */
522                                                 lsc->candidate =
523                                                         META_NOT_CANDIDATE;
524                                                 continue;
525                                         }
526                                         break;
527
528                                 case LDAP_SCOPE_ONELEVEL:
529                                         if ( is_one_level_rdn(
530                                                 li->targets[ i ]->suffix.bv_val,
531                                                 suffixlen - op->o_req_ndn.bv_len - 1 )
532                                                 && dnIsSuffix(
533                                                 &li->targets[ i ]->suffix,
534                                                 &op->o_req_ndn ) ) {
535                                                 /*
536                                                  * if there is exactly one
537                                                  * level, make the target suffix
538                                                  * the new base, and make scope
539                                                  * "base"
540                                                  */
541                                                 realbase =
542                                                 li->targets[i]->suffix.bv_val;
543                                                 realscope = LDAP_SCOPE_BASE;
544                                                 break;
545                                         } /* else continue with the next case */
546
547                                 case LDAP_SCOPE_BASE:
548                                         /*
549                                          * this target is no longer candidate
550                                          */
551                                         lsc->candidate = META_NOT_CANDIDATE;
552                                         continue;
553                                 }
554                         }
555
556                         /*
557                          * Rewrite the search base, if required
558                          */
559
560                         rewriteSession(li->targets[i]->rwmap.rwm_rw,
561                                         "searchBase",
562                                         realbase, op->o_conn, &mbase, result); 
563
564                         if (result->type != SUCCESS)
565                                 goto Catch; 
566
567                         if ( mbase == NULL ) {
568                                 mbase = realbase;
569                         }
570
571                         /*
572                          * Rewrite the search filter, if required
573                          */
574                         rewriteSession( li->targets[i]->rwmap.rwm_rw,
575                                         "searchFilter",
576                                         op->ors_filterstr.bv_val, op->o_conn,
577                                         &mfilter.bv_val, result);
578                         if (result->type != SUCCESS) 
579                                 goto Catch; 
580
581                         if ( mfilter.bv_val != NULL && mfilter.bv_val[ 0 ]
582                                                                 != '\0') {
583                                 mfilter.bv_len = strlen( mfilter.bv_val );
584                         } else {
585                                 if ( mfilter.bv_val != NULL ) {
586                                         free( mfilter.bv_val );
587                                 }
588                                 mfilter = op->ors_filterstr;
589                         }
590
591 #if 0
592                         /*
593                          * Maps attributes in filter
594                          */
595                         mapped_filter = ldap_back_map_filter(
596                                         &li->targets[i]->rwmap.rwm_at,
597                                         &li->targets[i]->rwmap.rwm_oc,
598                                         &mfilter, 0 );
599                         if ( mapped_filter == NULL ) {
600                                 mapped_filter = ( char * )mfilter.bv_val;
601                         } else {
602                                 if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
603                                         free( mfilter.bv_val );
604                                 }
605                         }
606                         mfilter.bv_val = NULL;
607                         mfilter.bv_len = 0;
608 #else
609                         mapped_filter = (char *) mfilter.bv_val;
610 #endif
611
612                         /*
613                          * Maps required attributes
614                          */
615                         if ( ldap_back_map_attrs(
616                                         &li->targets[ i ]->rwmap.rwm_at,
617                                         new_attrs, 0, &mapped_attrs ) ) {
618                                 goto Catch;
619                         }
620
621                         /*
622                          * Starts the search
623                          */
624                         msgid[ i ] = ldap_search( lsc->ld, mbase, realscope,
625                                                 mapped_filter, mapped_attrs,
626                                                 op->ors_attrsonly );
627
628                         if ( msgid[ i ] == -1 ) {
629                                 result->type = CONN_ERR; 
630                                 goto Catch; 
631                                 /*
632                                 lsc->candidate = META_NOT_CANDIDATE;
633                                 continue;
634                                 */
635                         }
636
637                         if ( mapped_attrs ) {
638                                 free( mapped_attrs );
639                                 mapped_attrs = NULL;
640                         }
641
642                         if ( mapped_filter != op->ors_filterstr.bv_val ) {
643                                 free( mapped_filter );
644                                 mapped_filter = NULL;
645                         }
646
647                         if ( mbase != realbase ) {
648                                 free( mbase );
649                                 mbase = NULL;
650                         }
651
652                         ++candidates;
653                 }
654
655                 num_entries = handleLdapResult(lc, &op_tmp, rs, msgid,
656                                 op->o_bd, attrs,
657                                 op->ors_attrsonly, candidates, 
658                                 cacheable, &entry_array,
659                                 curr_limit, op->ors_slimit, result); 
660
661                 if (result->type != SUCCESS) 
662                         goto Catch; 
663                 if (cacheable && (num_entries <= curr_limit)) {
664
665 #ifdef NEW_LOGGING
666                         LDAP_LOG( BACK_META, DETAIL1,
667                                         "QUERY CACHEABLE\n", 0, 0, 0 );
668 #else /* !NEW_LOGGING */
669                         Debug( LDAP_DEBUG_ANY, "QUERY CACHEABLE\n", 0, 0, 0 );
670 #endif /* !NEW_LOGGING */
671                         op_tmp.o_bd = li->glue_be;
672                         uuid = cache_entries(&op_tmp, rs, entry_array, cm, result); 
673 #ifdef NEW_LOGGING
674                         LDAP_LOG( BACK_META, DETAIL1,
675                                         "Added query %s UUID %s ENTRIES %d\n",
676                                         op->ors_filterstr.bv_val,
677                                         uuid, num_entries );
678 #else /* !NEW_LOGGING */
679                         Debug( LDAP_DEBUG_ANY,
680                                         "Added query %s UUID %s ENTRIES %d\n",
681                                         op->ors_filterstr.bv_val,
682                                         uuid, num_entries );
683 #endif /* !NEW_LOGGING */
684             
685                         if (result->type != SUCCESS) 
686                                 goto Catch; 
687                         (*(qm->addfunc))(qm, &query, template_id, uuid, result); 
688                         if (result->type != SUCCESS) 
689                                 goto Catch; 
690                         filter = 0; 
691                         attrs = 0; 
692
693                         /* FIXME : launch do_syncrepl() threads around here
694                          *
695                          * entryUUID and entryCSN need also to be requested by :
696                          */
697                         /*
698                         msgid[ i ] = ldap_search( lsc->ld, mbase, realscope,
699                                                 mapped_filter, mapped_attrs, op->ors_attrsonly );
700                         */
701                         /* Also, mbase, realscope, mapped_filter, mapped_attrs need
702                          * be managed as arrays. Each element needs to be retained by this point.
703                          */
704
705                 } else {
706 #ifdef NEW_LOGGING
707                         LDAP_LOG( BACK_META, DETAIL1,
708                                         "QUERY NOT CACHEABLE no\n",
709                                         0, 0, 0);
710 #else /* !NEW_LOGGING */
711                         Debug( LDAP_DEBUG_ANY, "QUERY NOT CACHEABLE no\n",
712                                         0, 0, 0);
713 #endif /* !NEW_LOGGING */
714                 }
715         }
716
717 Catch:;
718         switch (result->type) {
719                 case SUCCESS: 
720                         rc = 0; 
721                         break;
722
723                 case FILTER_ERR: 
724 #ifdef NEW_LOGGING
725                         LDAP_LOG( BACK_META, DETAIL1,
726                                         "Invalid template error\n", 0, 0, 0 );
727 #else /* !NEW_LOGGING */
728                         Debug( LDAP_DEBUG_ANY, "Invalid template error\n",
729                                         0, 0, 0 );
730 #endif /* !NEW_LOGGING */
731                         break; 
732
733                 case CONN_ERR: 
734                         rc = -1; 
735 #ifdef NEW_LOGGING
736                         LDAP_LOG( BACK_META, DETAIL1,
737                                 "Could not connect to a remote server\n",
738                                 0, 0, 0 );
739 #else /* !NEW_LOGGING */
740                         Debug( LDAP_DEBUG_ANY,
741                                 "Could not connect to a remote server\n",
742                                 0, 0, 0 );
743 #endif /* !NEW_LOGGING */
744                         send_ldap_error(op, rs, LDAP_OTHER,
745                                         "Connection error" );
746                         break;
747                         
748                 case RESULT_ERR: 
749                         rc = -1; 
750 #ifdef NEW_LOGGING
751                         LDAP_LOG( BACK_META, DETAIL1,
752                                 "Error in handling ldap_result\n", 0, 0, 0 );
753 #else /* !NEW_LOGGING */
754                         Debug( LDAP_DEBUG_ANY,
755                                 "Error in handling ldap_result\n", 0, 0, 0 );
756 #endif /* !NEW_LOGGING */
757                         break; 
758
759                 case REWRITING_ERR: 
760                         rc = -1; 
761                         if (result->rc == REWRITE_REGEXEC_UNWILLING) {
762                                 send_ldap_error( op, rs,
763                                                 LDAP_UNWILLING_TO_PERFORM,
764                                                 "Unwilling to perform" );
765                         } else {
766                                 send_ldap_error( op, rs, LDAP_OTHER,
767                                                 "Rewrite error" );
768                         }
769                         break;
770
771                 case MERGE_ERR: 
772                         rc = -1; 
773 #ifdef NEW_LOGGING
774                         LDAP_LOG( BACK_META, DETAIL1,
775                                 "Error in merging entry \n", 0, 0, 0 );
776 #else /* !NEW_LOGGING */
777                         Debug( LDAP_DEBUG_ANY,
778                                 "Error in merging entry \n", 0, 0, 0 );
779 #endif /* !NEW_LOGGING */
780                         break;
781
782                 case REMOVE_ERR: 
783                         rc = -1; 
784 #ifdef NEW_LOGGING
785                         LDAP_LOG( BACK_META, DETAIL1,
786                                         "Error in removing query \n",
787                                         0, 0, 0 );
788 #else /* !NEW_LOGGING */
789                         Debug( LDAP_DEBUG_ANY, "Error in removing query \n",
790                                         0, 0, 0 );
791 #endif /* !NEW_LOGGING */
792                         break;
793
794                 default:
795                         /* assert(0); */
796                         break;
797         }
798
799
800         if ( msgid ) {
801                 ch_free( msgid );
802         }
803         if (entry_array)  {
804                 for (i=0; (e = entry_array[i]); i++) {
805                         entry_free(e); 
806                 }
807                 free(entry_array);
808         }
809         if (filter) 
810                 filter_free(filter);
811
812         if (new_attrs) {
813                 if (new_attrs != attrs) 
814                         free(new_attrs); 
815         }
816
817         if (attrs)
818                 free(attrs); 
819
820         if (tempstr.bv_val ) {
821                 free(tempstr.bv_val);
822         }
823         ldap_pvt_thread_mutex_lock(&cm->cache_mutex); 
824         cm->threads--; 
825 #ifdef NEW_LOGGING
826         LDAP_LOG( BACK_META, DETAIL1, "Threads-- = %d\n", cm->threads, 0, 0 ); 
827 #else /* !NEW_LOGGING */
828         Debug( LDAP_DEBUG_ANY, "Threads-- = %d\n", cm->threads, 0, 0 ); 
829 #endif /* !NEW_LOGGING */
830         ldap_pvt_thread_mutex_unlock(&cm->cache_mutex); 
831         return rc;
832 }
833
834
835 static Entry* 
836 meta_create_entry (
837         Backend                 *be,
838         struct metaconn         *lc,
839         int                     target,
840         LDAPMessage             *e,
841         struct exception*       result
842 )
843 {
844         struct metainfo         *li = ( struct metainfo * )be->be_private;
845         struct berval           a, mapped;
846         Entry*                  ent;
847         BerElement              ber = *e->lm_ber;
848         Attribute               *attr, *soc_attr, **attrp;
849         struct berval   dummy = { 0, NULL };
850         struct berval   *bv, bdn;
851         const char              *text;
852         char*                   ename = NULL; 
853         struct berval   sc;
854         char*                   textbuf;
855         size_t                  textlen;
856
857         if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
858                 result->type = CREATE_ENTRY_ERR;        
859                 return NULL; 
860         }
861         ent = (Entry*)malloc(sizeof(Entry)); 
862
863         /*
864          * Rewrite the dn of the result, if needed
865          */
866         rewriteSession( li->targets[ target ]->rwmap.rwm_rw, "searchResult",
867                         bdn.bv_val, lc->conn, &ent->e_name.bv_val, result );  
868
869         if (result->type != SUCCESS) {
870                 return NULL; 
871         }
872         if ( ent->e_name.bv_val == NULL ) {
873                 ber_dupbv(&(ent->e_name), &bdn);
874         } else {
875 #ifdef NEW_LOGGING
876                 LDAP_LOG( BACK_META, DETAIL1,
877                         "[rw] searchResult[%d]: \"%s\" -> \"%s\"\n",
878                         target, bdn.bv_val, ent->e_name.bv_val );
879 #else /* !NEW_LOGGING */
880                 Debug( LDAP_DEBUG_ARGS, "rw> searchResult[%d]: \"%s\""
881                         " -> \"%s\"\n",
882                         target, bdn.bv_val, ent->e_name.bv_val );
883 #endif /* !NEW_LOGGING */
884                 ent->e_name.bv_len = strlen( ent->e_name.bv_val );
885         }
886                 
887         /*
888          * Note: this may fail if the target host(s) schema differs
889          * from the one known to the meta, and a DN with unknown
890          * attributes is returned.
891          * 
892          * FIXME: should we log anything, or delegate to dnNormalize?
893          */
894         dnNormalize( 0, NULL, NULL, &ent->e_name, &ent->e_nname, NULL ); 
895
896         /*
897         if ( dnNormalize( 0, NULL, NULL, &ent->e_name, &ent->e_nname )
898                 != LDAP_SUCCESS )
899         {
900                 return LDAP_INVALID_DN_SYNTAX;
901         }
902         */
903
904         /*
905          * cache dn
906          */
907         if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
908                 meta_dncache_update_entry( &li->cache, &ent->e_nname, target );
909         }
910
911         ent->e_id = 0;
912         ent->e_attrs = 0;
913         ent->e_private = 0;
914         ent->e_bv.bv_val = 0; 
915
916         attrp = &ent->e_attrs;
917
918         while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
919                 ldap_back_map( &li->targets[ target ]->rwmap.rwm_at, 
920                                 &a, &mapped, 1 );
921                 if ( mapped.bv_val == NULL ) {
922                         continue;
923                 }
924                 attr = ( Attribute * )ch_malloc( sizeof( Attribute ) );
925                 if ( attr == NULL ) {
926                         continue;
927                 }
928                 attr->a_flags = 0;
929                 attr->a_next = 0;
930                 attr->a_desc = NULL;
931                 attr->a_nvals = NULL;
932                 if ( slap_bv2ad( &mapped, &attr->a_desc, &text ) != LDAP_SUCCESS) {
933                         if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text ) 
934                                         != LDAP_SUCCESS) {
935 #ifdef NEW_LOGGING
936                                 LDAP_LOG( BACK_META, DETAIL1,
937                                         "slap_bv2undef_ad(%s): %s\n",
938                                         mapped.bv_val, text, 0 );
939 #else /* !NEW_LOGGING */
940                                 Debug( LDAP_DEBUG_ANY,
941                                         "slap_bv2undef_ad(%s): "
942                                         "%s\n%s", mapped.bv_val, text, "" );
943 #endif /* !NEW_LOGGING */
944                                 ch_free( attr );
945                                 continue;
946                         }
947                 }
948
949                 /* no subschemaSubentry */
950                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
951                         ch_free(attr);
952                         continue;
953                 }
954
955                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR 
956                                 || attr->a_vals == NULL ) {
957                         attr->a_vals = &dummy;
958 #if 0
959                 } else if ( attr->a_desc == slap_schema.si_ad_objectClass ||
960                                 attr->a_desc ==
961                                 slap_schema.si_ad_structuralObjectClass) {
962 #else
963                 } else if ( attr->a_desc == slap_schema.si_ad_objectClass ) {
964 #endif
965                         int i, last;
966                         for ( last = 0; attr->a_vals[ last ].bv_val; ++last )
967                                 ;
968                         for ( i = 0, bv = attr->a_vals; bv->bv_val; bv++,i++ ) {
969                                 ldap_back_map( &li->targets[ target]->rwmap.rwm_oc,
970                                                 bv, &mapped, 1 );
971                                 if ( mapped.bv_val == NULL ) {
972                                         free( bv->bv_val );
973                                         bv->bv_val = NULL;
974                                         if ( --last < 0 ) {
975                                                 break;
976                                         }
977                                         *bv = attr->a_vals[ last ];
978                                         attr->a_vals[ last ].bv_val = NULL;
979                                         i--;
980                                 } else if ( mapped.bv_val != bv->bv_val ) {
981                                         free( bv->bv_val );
982                                         ber_dupbv( bv, &mapped );
983                                 }
984                         }
985
986                         structural_class( attr->a_vals, &sc, NULL, &text, textbuf, textlen );
987                         soc_attr = (Attribute*) ch_malloc( sizeof( Attribute ));
988                         soc_attr->a_desc = slap_schema.si_ad_structuralObjectClass;
989                         soc_attr->a_vals = (BerVarray) ch_malloc( 2* sizeof( BerValue ));
990                         ber_dupbv( &soc_attr->a_vals[0], &sc );
991                         soc_attr->a_vals[1].bv_len = 0;
992                         soc_attr->a_vals[1].bv_val = NULL;
993                         soc_attr->a_nvals = (BerVarray) ch_malloc( 2* sizeof( BerValue ));
994                         ber_dupbv( &soc_attr->a_nvals[0], &sc );
995                         soc_attr->a_nvals[1].bv_len = 0;
996                         soc_attr->a_nvals[1].bv_val = NULL;
997
998                         *attrp = soc_attr;
999                         attrp = &soc_attr->a_next;
1000
1001                 /*
1002                  * It is necessary to try to rewrite attributes with
1003                  * dn syntax because they might be used in ACLs as
1004                  * members of groups; since ACLs are applied to the
1005                  * rewritten stuff, no dn-based subecj clause could
1006                  * be used at the ldap backend side (see
1007                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
1008                  * The problem can be overcome by moving the dn-based
1009                  * ACLs to the target directory server, and letting
1010                  * everything pass thru the ldap backend.
1011                  */
1012                 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
1013                                 SLAPD_DN_SYNTAX ) == 0 ) {
1014                         int i;
1015                         for ( i = 0, bv = attr->a_vals; bv->bv_val; bv++,i++ ) {
1016                                 char *newval;
1017                                 rewriteSession(li->targets[ target ]->rwmap.rwm_rw,
1018                                                 "searchResult", bv->bv_val,
1019                                                 lc->conn, &newval, result); 
1020                                 if (result->type != SUCCESS) {
1021                                         /* FIXME : Handle error */
1022                                         result->type = SUCCESS; 
1023                                 } else {
1024                                         /* left as is */
1025                                         if ( newval == NULL ) {
1026                                                 break;
1027                                         }
1028 #ifdef NEW_LOGGING
1029                                         LDAP_LOG( BACK_META, DETAIL1,
1030                                                 "[rw] searchResult on "
1031                                                 "attr=%s: \"%s\" -> \"%s\"\n",
1032                                                 attr->a_desc->ad_type->
1033                                                 sat_cname.bv_val,
1034                                                 bv->bv_val, newval );
1035 #else /* !NEW_LOGGING */
1036                                         Debug( LDAP_DEBUG_ARGS,
1037                                                 "rw> searchResult on attr=%s:"
1038                                                 " \"%s\" -> \"%s\"\n",
1039                                                 attr->a_desc->ad_type->
1040                                                 sat_cname.bv_val,
1041                                                 bv->bv_val, newval );
1042 #endif /* !NEW_LOGGING */
1043                                         free( bv->bv_val );
1044                                         bv->bv_val = newval;
1045                                         bv->bv_len = strlen( newval );
1046                                 }
1047                         }
1048                 }
1049                 *attrp = attr;
1050                 attrp = &attr->a_next;
1051         }
1052
1053         return ent; 
1054 }
1055
1056 static int
1057 is_one_level_rdn(
1058         const char      *rdn,
1059         int             from
1060 )
1061 {
1062         for ( ; from--; ) {
1063                 if ( DN_SEPARATOR( rdn[ from ] ) ) {
1064                         return 0;
1065                 }
1066         }
1067         return 1;
1068 }
1069
1070 static struct metaconn*  
1071 metaConnect(
1072         Operation*              op, 
1073         SlapReply               *rs,
1074         int                     op_type, 
1075         struct berval           *nbase, 
1076         struct exception        *result)
1077 {
1078         struct metaconn         *lc; 
1079
1080         result->type = SUCCESS; 
1081         lc = meta_back_getconn( op, rs, op_type, nbase, NULL );
1082         if (!lc) {
1083                 result->type = CONN_ERR; 
1084                 return 0; 
1085         }
1086         return lc; 
1087 }
1088
1089 static void
1090 add_filter_attrs(
1091         AttributeName** new_attrs, 
1092         AttributeName* attrs, 
1093         AttributeName* filter_attrs )
1094 {
1095         struct berval all_user = { sizeof(LDAP_ALL_USER_ATTRIBUTES) -1,
1096                                    LDAP_ALL_USER_ATTRIBUTES };
1097
1098         struct berval all_op = { sizeof(LDAP_ALL_OPERATIONAL_ATTRIBUTES) -1,
1099                                         LDAP_ALL_OPERATIONAL_ATTRIBUTES}; 
1100
1101         int alluser = 0; 
1102         int allop = 0; 
1103         int i; 
1104         int count; 
1105
1106         /* duplicate attrs */
1107         if (attrs == NULL) {
1108                 count = 1; 
1109         } else { 
1110                 for (count=0; attrs[count].an_name.bv_val; count++) 
1111                         ;
1112         }
1113         *new_attrs = (AttributeName*)(malloc((count+1)*sizeof(AttributeName))); 
1114         if (attrs == NULL) { 
1115                 (*new_attrs)[0].an_name.bv_val = "*"; 
1116                 (*new_attrs)[0].an_name.bv_len = 1; 
1117                 (*new_attrs)[1].an_name.bv_val = NULL;
1118                 (*new_attrs)[1].an_name.bv_len = 0; 
1119                 alluser = 1; 
1120                 allop = 0; 
1121         } else {  
1122                 for (i=0; i<count; i++) {
1123                         (*new_attrs)[i].an_name = attrs[i].an_name; 
1124                         (*new_attrs)[i].an_desc = attrs[i].an_desc;  
1125                 }
1126                 (*new_attrs)[count].an_name.bv_val = NULL; 
1127                 (*new_attrs)[count].an_name.bv_len = 0; 
1128                 alluser = an_find(*new_attrs, &all_user); 
1129                 allop = an_find(*new_attrs, &all_op); 
1130         }
1131
1132         for ( i=0; filter_attrs[i].an_name.bv_val; i++ ) {
1133                 if ( an_find(*new_attrs, &filter_attrs[i].an_name ))
1134                         continue; 
1135                 if ( is_at_operational(filter_attrs[i].an_desc->ad_type) ) {
1136                         if (allop) 
1137                                 continue; 
1138                 } else if (alluser) 
1139                         continue; 
1140                 *new_attrs = (AttributeName*)(realloc(*new_attrs,
1141                                         (count+2)*sizeof(AttributeName))); 
1142                 (*new_attrs)[count].an_name.bv_val =
1143                                 filter_attrs[i].an_name.bv_val; 
1144                 (*new_attrs)[count].an_name.bv_len =
1145                                 filter_attrs[i].an_name.bv_len; 
1146                 (*new_attrs)[count].an_desc = filter_attrs[i].an_desc; 
1147                 (*new_attrs)[count+1].an_name.bv_val = NULL; 
1148                 (*new_attrs)[count+1].an_name.bv_len = 0; 
1149                 count++; 
1150         }
1151 }
1152
1153 static int 
1154 handleLdapResult(
1155         struct metaconn* lc,
1156         Operation* op, 
1157         SlapReply *rs,
1158         int* msgid, Backend* be, 
1159         AttributeName* attrs, 
1160         int attrsonly, 
1161         int candidates, 
1162         int cacheable, 
1163         Entry*** entry_array, 
1164         int curr_limit, 
1165         int slimit,
1166         struct exception* result)
1167 {
1168         Entry  *entry;
1169         char *match = NULL, *err = NULL, *cache_ename = NULL;
1170         int sres; 
1171         int mres = LDAP_SUCCESS; 
1172         int num_entries = 0, count, i, rc;     
1173         struct timeval tv = {0, 0}; 
1174         struct metasingleconn* lsc; 
1175         struct metainfo         *li = ( struct metainfo * )be->be_private;
1176         result->rc = 0; 
1177         result->type = SUCCESS; 
1178
1179         for ( count = 0, rc = 0; candidates > 0; ) {
1180                 int ab, gotit = 0;
1181
1182                 /* check for abandon */
1183                 ab = op->o_abandon;
1184
1185                 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); lsc++, i++ ) {
1186                         if ( lsc->candidate != META_CANDIDATE ) {
1187                                 continue;
1188                         }
1189
1190                         if ( ab ) {
1191                                 ldap_abandon( lsc->ld, msgid[ i ] );
1192                                 result->type = ABANDON_ERR;
1193                                 break; 
1194                         }
1195
1196                         if ( slimit > 0 && num_entries == slimit ) {
1197                                 result->type = SLIMIT_ERR; 
1198                                 break; 
1199                         }
1200
1201                         if ((entry = get_result_entry(be, lc, lsc,
1202                                                 msgid, i, &tv, result))) {
1203                                 rs->sr_entry = entry;
1204                                 rs->sr_attrs = op->ors_attrs; 
1205                                 send_search_entry( op, rs );
1206                                 rs->sr_entry = NULL;
1207                                 rs->sr_attrs = NULL; 
1208                                 if ((cacheable) &&
1209                                                 (num_entries < curr_limit))  {
1210                                         rewriteSession( li->rwinfo,
1211                                                         "cacheResult",
1212                                                         entry->e_name.bv_val,
1213                                                         lc->conn,
1214                                                         &cache_ename, result );  
1215                                         free(entry->e_name.bv_val); 
1216                                         if (result->type != SUCCESS) {
1217                                                 return 0; 
1218                                         }
1219                                         ber_str2bv(cache_ename,
1220                                                 strlen(cache_ename),
1221                                                 0, &entry->e_name); 
1222                                         ber_dupbv(&entry->e_nname,
1223                                                 &entry->e_name); 
1224                                         *entry_array = (Entry**)realloc(
1225                                                         *entry_array,
1226                                                         (( num_entries+2 ) *
1227                                                          sizeof( Entry* )));
1228                                         (*entry_array)[num_entries] = entry;    
1229                                         (*entry_array)[num_entries+1] = NULL;
1230                                 }
1231                                 num_entries++; 
1232                                 gotit = 1; 
1233                         } else if (result->type == REWRITING_ERR) {
1234                                 return 0; 
1235                         } else if (result->type == TIMEOUT_ERR) {
1236                                 result->type = SUCCESS; 
1237                                 continue;  
1238                         } else if (result->type == CREATE_ENTRY_ERR) {
1239                                 break; 
1240                         } else if (result->rc == -1) {
1241                                 break; 
1242                         } else {
1243                                 rs->sr_err = result->rc;
1244                                 sres = ldap_back_map_result(rs);
1245                                 if (mres == LDAP_SUCCESS &&
1246                                                 sres != LDAP_SUCCESS) {
1247                                         mres = sres; 
1248                                         ldap_get_option(lsc->ld,
1249                                                 LDAP_OPT_ERROR_STRING, &err);
1250                                         ldap_get_option(lsc->ld,
1251                                                 LDAP_OPT_MATCHED_DN, &match);
1252                                 }
1253                                 lsc->candidate = META_NOT_CANDIDATE; 
1254                                 candidates--; 
1255                                 result->type = SUCCESS; 
1256                         }
1257                 }
1258                 switch (result->type) {
1259                 case RESULT_ERR: 
1260 #ifdef NEW_LOGGING
1261                         LDAP_LOG( BACK_META, DETAIL1,
1262                                         "ldap_result error, rc = -1\n",
1263                                         0, 0, 0 );
1264 #else /* !NEW_LOGGING */
1265                         Debug( LDAP_DEBUG_ANY, "ldap_result error, rc = -1\n",
1266                                         0, 0, 0 );
1267 #endif /* !NEW_LOGGING */
1268                         rs->sr_err = LDAP_OTHER;
1269                         send_ldap_result( op, rs );
1270                         return 0; 
1271
1272                 case CREATE_ENTRY_ERR: 
1273 #ifdef NEW_LOGGING
1274                         LDAP_LOG( BACK_META, DETAIL1,
1275                                         "Error in parsing result \n",
1276                                         0, 0, 0 );
1277 #else /* !NEW_LOGGING */
1278                         Debug( LDAP_DEBUG_ANY, "Error in parsing result \n",
1279                                         0, 0, 0 );
1280 #endif /* !NEW_LOGGING */
1281                         rs->sr_err = LDAP_OTHER;
1282                         send_ldap_result( op, rs );
1283                         result->type = RESULT_ERR; 
1284                         return 0; 
1285
1286                 case SLIMIT_ERR: 
1287 #ifdef NEW_LOGGING
1288                         LDAP_LOG( BACK_META, DETAIL1, "Size limit exceeded \n",
1289                                         0, 0, 0 );
1290 #else /* !NEW_LOGGING */
1291                         Debug( LDAP_DEBUG_ANY, "Size limit exceeded \n",
1292                                         0, 0, 0 );
1293 #endif /* !NEW_LOGGING */
1294                         rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
1295                         send_ldap_result( op, rs );
1296                         result->type = RESULT_ERR; 
1297                         return 0;
1298
1299                 case ABANDON_ERR: 
1300 #ifdef NEW_LOGGING
1301                         LDAP_LOG( BACK_META, DETAIL1,
1302                                         "search operation abandoned \n",
1303                                         0, 0, 0 );
1304 #else /* !NEW_LOGGING */
1305                         Debug( LDAP_DEBUG_ANY, "search operation abandoned \n",
1306                                         0, 0, 0 );
1307 #endif /* !NEW_LOGGING */
1308                         result->type = RESULT_ERR; 
1309                         return 0; 
1310
1311                 default:
1312                         /* assert( 0 ); */
1313                         break;
1314                 }
1315                 if ( gotit == 0 ) {
1316                         tv.tv_sec = 0;
1317                         tv.tv_usec = 100000;
1318                         ldap_pvt_thread_yield();
1319                 } else {
1320                         tv.tv_sec = 0;
1321                         tv.tv_usec = 0;
1322                 }
1323         }
1324
1325         rs->sr_err = mres;
1326         rs->sr_text = err;
1327         rs->sr_matched = match;
1328
1329         send_ldap_result( op, rs );
1330
1331         rs->sr_text = NULL;
1332         rs->sr_matched = NULL;
1333
1334         if (err) 
1335                 free(err); 
1336
1337         if (match) 
1338                 free(match); 
1339     
1340         result->type = (mres == LDAP_SUCCESS) ? SUCCESS : RESULT_ERR; 
1341         return num_entries; 
1342 }
1343
1344 static Entry* 
1345 get_result_entry(
1346         Backend* be, 
1347         struct metaconn* lc, 
1348         struct metasingleconn* lsc, 
1349         int* msgid,
1350         int i, 
1351         struct timeval* tv, 
1352         struct exception* result)
1353 {
1354         Entry* entry; 
1355         LDAPMessage     *res, *e; 
1356         int rc; 
1357         int sres = LDAP_SUCCESS; 
1358
1359         rc = ldap_result( lsc->ld, msgid[ i ],
1360                         0, tv, &res );
1361
1362         if ( rc == 0 ) {
1363                 result->type = TIMEOUT_ERR; 
1364                 return NULL; 
1365         } else if ( rc == -1 ) {
1366                 result->rc = -1; 
1367                 result->type = RESULT_ERR; 
1368                 return NULL; 
1369         } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
1370                 e = ldap_first_entry( lsc->ld, res );
1371                 entry = meta_create_entry(be, lc, i, e, result);  
1372                 if (!entry) {
1373                         return NULL; 
1374                 }    
1375                 ldap_msgfree( res );
1376                 result->type = SUCCESS; 
1377                 return entry; 
1378         } else {
1379                 sres = ldap_result2error( lsc->ld,
1380                                 res, 1 );
1381                 result->rc = sres; 
1382                 result->type = RESULT_ERR; 
1383                 return NULL; 
1384         }
1385 }       
1386
1387 static void
1388 rewriteSession(
1389         struct rewrite_info* info, 
1390         const char* rewriteContext, 
1391         const char* string, 
1392         const void* cookie,  
1393         char** base, 
1394         struct exception* result)
1395 {
1396         int rc = rewrite_session(info, rewriteContext, string, cookie, base); 
1397         if (rc != REWRITE_REGEXEC_OK) {
1398                 result->rc = rc; 
1399                 result->type = REWRITING_ERR; 
1400
1401                 if (strcmp(rewriteContext, "searchBase") == 0) 
1402 #ifdef NEW_LOGGING
1403                         LDAP_LOG( BACK_META, DETAIL1,
1404                                 "Problem in rewriting search base\n", 0, 0, 0 );
1405 #else /* !NEW_LOGGING */
1406                         Debug( LDAP_DEBUG_ANY,
1407                                 "Problem in rewriting search base\n", 0, 0, 0 );
1408 #endif /* !NEW_LOGGING */
1409                 if (strcmp(rewriteContext, "searchFilter") == 0) 
1410 #ifdef NEW_LOGGING
1411                         LDAP_LOG( BACK_META, DETAIL1,
1412                                 "Problem in rewriting search filter\n",
1413                                 0, 0, 0 );
1414 #else /* !NEW_LOGGING */
1415                         Debug( LDAP_DEBUG_ANY,
1416                                 "Problem in rewriting search filter\n",
1417                                 0, 0, 0 );
1418 #endif /* !NEW_LOGGING */
1419                 if (strcmp(rewriteContext, "searchResult") == 0) 
1420 #ifdef NEW_LOGGING
1421                         LDAP_LOG( BACK_META, DETAIL1,
1422                                 "Problem in rewriting DN, or DN syntax "
1423                                 "attributes of search result\n", 0, 0, 0 );
1424 #else /* !NEW_LOGGING */
1425                         Debug( LDAP_DEBUG_ANY,
1426                                 "Problem in rewriting DN, or DN syntax "
1427                                 "attributes of search result\n", 0, 0, 0 );
1428 #endif /* !NEW_LOGGING */
1429                 if (strcmp(rewriteContext, "cacheBase") == 0) 
1430 #ifdef NEW_LOGGING
1431                         LDAP_LOG( BACK_META, DETAIL1,
1432                                 "Problem in rewriting search base with "
1433                                 "cache base\n", 0, 0, 0 );
1434 #else /* !NEW_LOGGING */
1435                         Debug( LDAP_DEBUG_ANY,
1436                                 "Problem in rewriting search base with "
1437                                 "cache base\n", 0, 0, 0 );
1438 #endif /* !NEW_LOGGING */
1439                 if (strcmp(rewriteContext, "cacheResult") == 0) 
1440 #ifdef NEW_LOGGING
1441                         LDAP_LOG( BACK_META, DETAIL1,
1442                                 "Problem in rewriting DN for cached entries\n",
1443                                 0, 0, 0 );
1444 #else /* !NEW_LOGGING */
1445                         Debug( LDAP_DEBUG_ANY,
1446                                 "Problem in rewriting DN for cached entries\n",
1447                                 0, 0, 0 );
1448 #endif /* !NEW_LOGGING */
1449                 if (strcmp(rewriteContext, "cacheReturn") == 0) 
1450 #ifdef NEW_LOGGING
1451                         LDAP_LOG( BACK_META, DETAIL1,
1452                                 "Problem in rewriting DN for answerable "
1453                                 "entries\n", 0, 0, 0 );
1454 #else /* !NEW_LOGGING */
1455                         Debug( LDAP_DEBUG_ANY,
1456                                 "Problem in rewriting DN for answerable "
1457                                 "entries\n", 0, 0, 0 );
1458 #endif /* !NEW_LOGGING */
1459         } else {
1460                 result->type = SUCCESS;
1461         }
1462 }
1463
1464 static int 
1465 get_attr_set(
1466         AttributeName* attrs, 
1467         query_manager* qm, 
1468         int num )
1469 {
1470         int i; 
1471         for (i=0; i<num; i++) {
1472                 if (attrscmp(attrs, qm->attr_sets[i].attrs)) 
1473                         return i;
1474         }
1475         return -1; 
1476 }
1477
1478 static int 
1479 attrscmp(
1480         AttributeName* attrs_in, 
1481         AttributeName* attrs)
1482 {
1483         int i, count1, count2; 
1484         if ( attrs_in == NULL ) {
1485                 return (attrs ? 0 : 1); 
1486         } 
1487         if ( attrs == NULL ) 
1488                 return 0; 
1489         
1490         for ( count1=0;
1491               attrs_in && attrs_in[count1].an_name.bv_val != NULL;
1492               count1++ )
1493                 ;
1494         for ( count2=0;
1495               attrs && attrs[count2].an_name.bv_val != NULL;
1496               count2++) 
1497                 ;
1498         if ( count1 != count2 )
1499                 return 0; 
1500
1501         for ( i=0; i<count1; i++ ) {
1502                 if ( !an_find(attrs, &attrs_in[i].an_name ))
1503                         return 0; 
1504         }
1505         return 1; 
1506 }
1507
1508 static char* 
1509 cache_entries(
1510         Operation       *op,
1511         SlapReply       *rs,
1512         Entry** entry_array, 
1513         cache_manager* cm, 
1514         struct exception* result)
1515 {
1516         int             i; 
1517         int             return_val; 
1518         int             cache_size; 
1519         Entry           *e; 
1520         struct berval   query_uuid; 
1521         struct berval   crp_uuid; 
1522         char            uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ], *crpid; 
1523         char            crpuuid[40]; 
1524         query_manager   *qm = cm->qm;
1525     
1526         result->type = SUCCESS; 
1527         query_uuid.bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf)); 
1528         query_uuid.bv_val = ch_strdup(uuidbuf);
1529
1530 #ifdef NEW_LOGGING
1531         LDAP_LOG( BACK_META, DETAIL1, "UUID for query being added = %s\n",
1532                         uuidbuf, 0, 0 );
1533 #else /* !NEW_LOGGING */
1534         Debug( LDAP_DEBUG_ANY, "UUID for query being added = %s\n",
1535                         uuidbuf, 0, 0 );
1536 #endif /* !NEW_LOGGING */
1537         
1538         for ( i=0; ( entry_array && (e=entry_array[i]) ); i++ ) {
1539 #ifdef NEW_LOGGING
1540                 LDAP_LOG( BACK_META, DETAIL2, "LOCKING REMOVE MUTEX\n",
1541                                 0, 0, 0 );
1542 #else /* !NEW_LOGGING */
1543                 Debug( LDAP_DEBUG_NONE, "LOCKING REMOVE MUTEX\n", 0, 0, 0 );
1544 #endif /* !NEW_LOGGING */
1545                 ldap_pvt_thread_mutex_lock(&cm->remove_mutex); 
1546 #ifdef NEW_LOGGING
1547                 LDAP_LOG( BACK_META, DETAIL2, "LOCKED REMOVE MUTEX\n", 0, 0, 0);
1548 #else /* !NEW_LOGGING */
1549                 Debug( LDAP_DEBUG_NONE, "LOCKED REMOVE MUTEX\n", 0, 0, 0);
1550 #endif /* !NEW_LOGGING */
1551                 if ( cm->cache_size > (cm->thresh_hi) ) {
1552                         while(cm->cache_size > (cm->thresh_lo)) {
1553                                 crpid = cache_replacement(qm);
1554                                 if (crpid == NULL) {
1555                                         result->type = REMOVE_ERR; 
1556                                 } else {
1557                                         strcpy(crpuuid, crpid); 
1558                                         crp_uuid.bv_val = crpuuid; 
1559                                         crp_uuid.bv_len = strlen(crpuuid); 
1560 #ifdef NEW_LOGGING
1561                                         LDAP_LOG( BACK_META, DETAIL1,
1562                                                 "Removing query UUID %s\n",
1563                                                 crpuuid, 0, 0 );
1564 #else /* !NEW_LOGGING */
1565                                         Debug( LDAP_DEBUG_ANY,
1566                                                 "Removing query UUID %s\n",
1567                                                 crpuuid, 0, 0 );
1568 #endif /* !NEW_LOGGING */
1569                                         return_val = remove_query_data(op, rs,
1570                                                         &crp_uuid, result); 
1571 #ifdef NEW_LOGGING
1572                                         LDAP_LOG( BACK_META, DETAIL1,
1573                                                 "QUERY REMOVED, SIZE=%d\n",
1574                                                 return_val, 0, 0);
1575 #else /* !NEW_LOGGING */
1576                                         Debug( LDAP_DEBUG_ANY,
1577                                                 "QUERY REMOVED, SIZE=%d\n",
1578                                                 return_val, 0, 0);
1579 #endif /* !NEW_LOGGING */
1580                                         ldap_pvt_thread_mutex_lock(
1581                                                         &cm->cache_mutex ); 
1582                                         cm->total_entries -= result->rc; 
1583                                         cm->num_cached_queries--; 
1584 #ifdef NEW_LOGGING
1585                                         LDAP_LOG( BACK_META, DETAIL1,
1586                                                 "STORED QUERIES = %lu\n",
1587                                                 cm->num_cached_queries, 0, 0 );
1588 #else /* !NEW_LOGGING */
1589                                         Debug( LDAP_DEBUG_ANY,
1590                                                 "STORED QUERIES = %lu\n",
1591                                                 cm->num_cached_queries, 0, 0 );
1592 #endif /* !NEW_LOGGING */
1593                                         ldap_pvt_thread_mutex_unlock(
1594                                                         &cm->cache_mutex );
1595                                         cm->cache_size = (return_val >
1596                                                 cm->cache_size) ?
1597                                                 0 : (cm->cache_size-return_val);
1598 #ifdef NEW_LOGGING
1599                                         LDAP_LOG( BACK_META, DETAIL1,
1600                                                 "QUERY REMOVED, CACHE SIZE="
1601                                                 "%lu bytes %d entries\n",
1602                                                 cm->cache_size,
1603                                                 cm->total_entries, 0 );
1604 #else /* !NEW_LOGGING */
1605                                         Debug( LDAP_DEBUG_ANY,
1606                                                 "QUERY REMOVED, CACHE SIZE="
1607                                                 "%lu bytes %d entries\n",
1608                                                 cm->cache_size,
1609                                                 cm->total_entries, 0 );
1610 #endif /* !NEW_LOGGING */
1611                                 }
1612                         }
1613                 }
1614
1615                 rs->sr_entry = e;
1616                 return_val = merge_entry(op, rs, &query_uuid, result);
1617                 rs->sr_entry = NULL;
1618                 cm->cache_size += return_val;
1619 #ifdef NEW_LOGGING
1620                 LDAP_LOG( BACK_META, DETAIL1,
1621                         "ENTRY ADDED/MERGED, CACHE SIZE=%lu bytes\n",
1622                         cm->cache_size, 0, 0 );
1623 #else /* !NEW_LOGGING */
1624                 Debug( LDAP_DEBUG_ANY,
1625                         "ENTRY ADDED/MERGED, CACHE SIZE=%lu bytes\n",
1626                         cm->cache_size, 0, 0 );
1627 #endif /* !NEW_LOGGING */
1628 #ifdef NEW_LOGGING
1629                 LDAP_LOG( BACK_META, DETAIL2, "UNLOCKING REMOVE MUTEX\n",
1630                                 0, 0, 0 );
1631 #else /* !NEW_LOGGING */
1632                 Debug( LDAP_DEBUG_NONE, "UNLOCKING REMOVE MUTEX\n", 0, 0, 0 );
1633 #endif /* !NEW_LOGGING */
1634                 ldap_pvt_thread_mutex_unlock(&cm->remove_mutex); 
1635 #ifdef NEW_LOGGING
1636                 LDAP_LOG( BACK_META, DETAIL2, "UNLOCKED REMOVE MUTEX\n",
1637                                 0, 0, 0 );
1638 #else /* !NEW_LOGGING */
1639                 Debug( LDAP_DEBUG_NONE, "UNLOCKED REMOVE MUTEX\n", 0, 0, 0 );
1640 #endif /* !NEW_LOGGING */
1641                 if (result->type != SUCCESS) 
1642                         return 0; 
1643                 ldap_pvt_thread_mutex_lock(&cm->cache_mutex); 
1644                 cm->total_entries += result->rc; 
1645 #ifdef NEW_LOGGING
1646                 LDAP_LOG( BACK_META, DETAIL1,
1647                         "ENTRY ADDED/MERGED, SIZE=%d, CACHED ENTRIES=%d\n",
1648                         return_val, cm->total_entries, 0 );
1649 #else /* !NEW_LOGGING */
1650                 Debug( LDAP_DEBUG_ANY,
1651                         "ENTRY ADDED/MERGED, SIZE=%d, CACHED ENTRIES=%d\n",
1652                         return_val, cm->total_entries, 0 );
1653 #endif /* !NEW_LOGGING */
1654                 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex); 
1655         }
1656         ldap_pvt_thread_mutex_lock(&cm->cache_mutex); 
1657         cm->num_cached_queries++; 
1658 #ifdef NEW_LOGGING
1659         LDAP_LOG( BACK_META, DETAIL1, "STORED QUERIES = %lu\n",
1660                         cm->num_cached_queries, 0, 0 );
1661 #else /* !NEW_LOGGING */
1662         Debug( LDAP_DEBUG_ANY, "STORED QUERIES = %lu\n",
1663                         cm->num_cached_queries, 0, 0 );
1664 #endif /* !NEW_LOGGING */
1665         ldap_pvt_thread_mutex_unlock(&cm->cache_mutex); 
1666
1667         return query_uuid.bv_val; 
1668 }
1669
1670 static int 
1671 is_temp_answerable(
1672         int attr_set, 
1673         struct berval* tempstr, 
1674         query_manager* qm, 
1675         int template_id )
1676 {
1677         int i; 
1678         int* id_array; 
1679         char* str;
1680         int result = 0; 
1681         i = qm->templates[template_id].attr_set_index; 
1682         str = qm->templates[template_id].querystr; 
1683
1684         if (attr_set == i) {
1685                 result = 1; 
1686         } else { 
1687                 id_array = qm->attr_sets[attr_set].ID_array;    
1688
1689                 while (*id_array != -1) {
1690                         if (*id_array == i) 
1691                                 result = 1; 
1692                         id_array++; 
1693                 }
1694         }
1695         if (!result) 
1696                 return 0; 
1697         if (strcasecmp(str, tempstr->bv_val) == 0)  
1698                 return 1; 
1699         return 0; 
1700 }
1701
1702 static void* 
1703 consistency_check(void* operation)
1704 {
1705         Operation* op = (Operation*)operation; 
1706
1707         SlapReply rs = {REP_RESULT}; 
1708
1709         struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
1710         cache_manager*  cm = li->cm; 
1711         query_manager* qm = cm->qm; 
1712         CachedQuery* query, *query_prev; 
1713         time_t curr_time; 
1714         struct berval uuid;  
1715         struct exception result; 
1716         int i, return_val; 
1717         QueryTemplate* templ;
1718
1719
1720         op->o_bd = li->glue_be;
1721       
1722         for(;;) {
1723                 ldap_pvt_thread_sleep(cm->cc_period);     
1724                 for (i=0; qm->templates[i].querystr; i++) {
1725                         templ = qm->templates + i; 
1726                         query = templ->query_last; 
1727                         curr_time = slap_get_time(); 
1728                         ldap_pvt_thread_mutex_lock(&cm->remove_mutex); 
1729                         while (query && (query->expiry_time < curr_time)) {
1730                                 ldap_pvt_thread_mutex_lock(&qm->lru_mutex); 
1731                                 remove_query(qm, query); 
1732                                 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex); 
1733 #ifdef NEW_LOGGING
1734                                 LDAP_LOG( BACK_META, DETAIL1, "Lock CR index = %d\n",
1735                                                 i, 0, 0 );
1736 #else /* !NEW_LOGGING */
1737                                 Debug( LDAP_DEBUG_ANY, "Lock CR index = %d\n",
1738                                                 i, 0, 0 );
1739 #endif /* !NEW_LOGGING */
1740                                 ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);  
1741                                 remove_from_template(query, templ); 
1742 #ifdef NEW_LOGGING
1743                                 LDAP_LOG( BACK_META, DETAIL1,
1744                                                 "TEMPLATE %d QUERIES-- %d\n",
1745                                                 i, templ->no_of_queries, 0 );
1746 #else /* !NEW_LOGGING */
1747                                 Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES-- %d\n",
1748                                                 i, templ->no_of_queries, 0 );
1749 #endif /* !NEW_LOGGING */
1750 #ifdef NEW_LOGGING
1751                                 LDAP_LOG( BACK_META, DETAIL1, "Unlock CR index = %d\n",
1752                                                 i, 0, 0 );
1753 #else /* !NEW_LOGGING */
1754                                 Debug( LDAP_DEBUG_ANY, "Unlock CR index = %d\n",
1755                                                 i, 0, 0 );
1756 #endif /* !NEW_LOGGING */
1757                                 ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);  
1758                                 uuid.bv_val = query->q_uuid; 
1759                                 uuid.bv_len = strlen(query->q_uuid); 
1760                                 return_val = remove_query_data(op, &rs, &uuid, &result); 
1761 #ifdef NEW_LOGGING
1762                                 LDAP_LOG( BACK_META, DETAIL1,
1763                                                 "STALE QUERY REMOVED, SIZE=%d\n",
1764                                                 return_val, 0, 0 );
1765 #else /* !NEW_LOGGING */
1766                                 Debug( LDAP_DEBUG_ANY, "STALE QUERY REMOVED, SIZE=%d\n",
1767                                                         return_val, 0, 0 );
1768 #endif /* !NEW_LOGGING */
1769                                 ldap_pvt_thread_mutex_lock(&cm->cache_mutex); 
1770                                 cm->total_entries -= result.rc; 
1771                                 cm->num_cached_queries--; 
1772 #ifdef NEW_LOGGING
1773                                 LDAP_LOG( BACK_META, DETAIL1, "STORED QUERIES = %lu\n",
1774                                                 cm->num_cached_queries, 0, 0 );
1775 #else /* !NEW_LOGGING */
1776                                 Debug( LDAP_DEBUG_ANY, "STORED QUERIES = %lu\n",
1777                                                 cm->num_cached_queries, 0, 0 );
1778 #endif /* !NEW_LOGGING */
1779                                 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex); 
1780                                 cm->cache_size = (return_val > cm->cache_size) ?
1781                                                         0: (cm->cache_size-return_val);
1782 #ifdef NEW_LOGGING
1783                                 LDAP_LOG( BACK_META, DETAIL1,
1784                                         "STALE QUERY REMOVED, CACHE SIZE=%lu bytes %d "
1785                                         "entries\n", cm->cache_size,
1786                                         cm->total_entries, 0 );
1787 #else /* !NEW_LOGGING */
1788                                 Debug( LDAP_DEBUG_ANY,
1789                                         "STALE QUERY REMOVED, CACHE SIZE=%lu bytes %d "
1790                                         "entries\n", cm->cache_size,
1791                                         cm->total_entries, 0 );
1792 #endif /* !NEW_LOGGING */
1793                                 query_prev = query; 
1794                                 query = query->prev; 
1795                                 free_query(query_prev); 
1796                         }
1797                         ldap_pvt_thread_mutex_unlock(&cm->remove_mutex); 
1798                 }
1799         }
1800 }
1801
1802 static int
1803 cache_back_sentry(
1804         Operation* op, 
1805         SlapReply *rs )
1806
1807         slap_callback           *cb = op->o_callback; 
1808         /*struct metainfo       *li = ( struct metainfo * )op->o_bd->be_private;*/
1809         Backend* be = (Backend*)(cb->sc_private);
1810         struct metainfo         *li = ( struct metainfo * )be->be_private;
1811  
1812         char                    *ename = NULL;
1813         struct exception        result;
1814         struct berval           dn;
1815         struct berval           ndn;
1816  
1817         if (rs->sr_type == REP_SEARCH) {
1818                 dn = rs->sr_entry->e_name; 
1819                 ndn = rs->sr_entry->e_nname; 
1820
1821                 rewriteSession( li->rwinfo, "cacheReturn",
1822                                 rs->sr_entry->e_name.bv_val, op->o_conn,
1823                                 &ename, &result );  
1824                 ber_str2bv(ename, strlen(ename), 0, &rs->sr_entry->e_name); 
1825                 /* FIXME: should we normalize this? */
1826                 ber_dupbv(&rs->sr_entry->e_nname, &rs->sr_entry->e_name); 
1827
1828                 op->o_callback = NULL; 
1829
1830                 send_search_entry( op, rs );
1831          
1832                 rs->sr_entry->e_name = dn; 
1833                 rs->sr_entry->e_nname = ndn; 
1834
1835                 op->o_callback = cb; 
1836                 return 0; 
1837         } else if (rs->sr_type == REP_RESULT) { 
1838                 op->o_callback = NULL; 
1839                 send_ldap_result( op, rs ); 
1840                 return 0; 
1841         }
1842 }
1843 #endif