]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/cache-search.c
group rewrite/map stuff in one structure and optimize more function calls
[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         struct metainfo* li, 
126         Connection* conn, 
127         Operation* op, 
128         int op_type, 
129         struct berval* nbase, 
130         struct exception* result 
131 );
132
133 static void
134 add_filter_attrs(
135         AttributeName** newattrs, 
136         AttributeName* attrs, 
137         AttributeName* filter_attrs
138 );
139
140 static int 
141 handleLdapResult(
142         struct metaconn* lc, 
143         Operation* op, 
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         Entry** entry_array, 
192         cache_manager* cm, 
193         Backend* be, 
194         Connection* conn, 
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         Backend* be,
209         Backend* glue_be, 
210         Connection* conn
211 ); 
212
213 static int
214 cache_back_sentry(
215         Backend* be, 
216         Connection* conn,
217         Operation* op, 
218         Entry* e, 
219         AttributeName* attrs, 
220         int attrsonly, 
221         LDAPControl** ctrls
222 );
223
224 int
225 meta_back_cache_search(
226         Backend         *be,
227         Connection      *conn,
228         Operation       *op,
229         struct berval   *base,
230         struct berval   *nbase,
231         int             scope,
232         int             deref,
233         int             slimit,
234         int             tlimit,
235         Filter          *filt,
236         struct berval   *filterstr,
237         AttributeName   *attributes,
238         int             attrsonly
239 )
240 {
241         struct metainfo         *li = ( struct metainfo * )be->be_private;
242         struct metaconn         *lc;
243         struct metasingleconn   *lsc;
244         cache_manager*          cm = li->cm; 
245         query_manager*          qm = cm->qm; 
246
247         time_t                  curr_time; 
248
249         int count, rc = 0, *msgid = NULL; 
250         char *mbase = NULL;
251         char *cbase = NULL; 
252         char *uuid; 
253             
254         int i = -1, last = 0, candidates = 0, op_type;
255
256         struct berval   mfilter;
257         struct berval   cachebase = { 0L, NULL };  
258         struct berval   ncachebase = { 0L, NULL };  
259         struct berval   cache_suffix; 
260         struct berval   tempstr = { 0L, NULL }; 
261
262         AttributeName   *filter_attrs = NULL; 
263         AttributeName   *new_attrs = NULL; 
264         AttributeName   *attrs = NULL; 
265
266         Entry           *e;
267         Entry           **entry_array = NULL;
268
269         Query           query; 
270
271         int             attr_set = -1; 
272         int             template_id = -1; 
273         int             answerable = 0; 
274         int             cacheable = 0; 
275         int             num_entries = 0;
276         int             curr_limit;
277         int             fattr_cnt=0; 
278
279    
280         struct exception result[1]; 
281
282         Filter* filter = str2filter(filterstr->bv_val); 
283         slap_callback cb = {NULL, NULL, cache_back_sentry, NULL}; 
284
285         cb.sc_private = be; 
286
287         if (attributes) {
288                 for ( count=0; attributes[ count ].an_name.bv_val; count++ )
289                         ;
290                 attrs = (AttributeName*)malloc( ( count + 1 ) *
291                                                 sizeof(AttributeName));
292                 for ( count=0; attributes[ count ].an_name.bv_val; count++ ) {
293                         ber_dupbv(&attrs[ count ].an_name,
294                                                 &attributes[ count ].an_name);
295                         attrs[count].an_desc = attributes[count].an_desc; 
296                 }
297                 attrs[ count ].an_name.bv_val = NULL;
298                 attrs[ count ].an_name.bv_len = 0;
299         }
300
301         result->type = SUCCESS; 
302         result->rc = 0; 
303         ldap_pvt_thread_mutex_lock(&cm->cache_mutex); 
304         cm->threads++; 
305 #ifdef NEW_LOGGING
306         LDAP_LOG( BACK_META, DETAIL1, "Threads++ = %d\n", cm->threads, 0, 0 );
307 #else /* !NEW_LOGGING */
308         Debug( LDAP_DEBUG_ANY, "Threads++ = %d\n", cm->threads, 0, 0 );
309 #endif /* !NEW_LOGGING */
310         ldap_pvt_thread_mutex_unlock(&cm->cache_mutex); 
311     
312         filter2template(filter, &tempstr, &filter_attrs, &fattr_cnt, result);  
313         if (result->type != SUCCESS) 
314                 goto Catch; 
315
316 #ifdef NEW_LOGGING
317         LDAP_LOG( BACK_META, DETAIL1, "query template of incoming query = %s\n",
318                                         tempstr.bv_val, 0, 0 );
319 #else /* !NEW_LOGGING */
320         Debug( LDAP_DEBUG_ANY, "query template of incoming query = %s\n",
321                                         tempstr.bv_val, 0, 0 );
322 #endif /* !NEW_LOGGING */
323         curr_limit = cm->num_entries_limit ;
324
325         /* find attr set */     
326         attr_set = get_attr_set(attrs, qm, cm->numattrsets); 
327     
328         query.filter = filter; 
329         query.attrs = attrs; 
330         query.base = *base; 
331         query.scope = scope; 
332
333         /* check for query containment */
334         if (attr_set > -1) {
335                 for (i=0; i<cm->numtemplates; i++) {
336                         /* find if template i can potentially answer tempstr */
337                         if (!is_temp_answerable(attr_set, &tempstr, qm, i)) 
338                                 continue; 
339                         if (attr_set == qm->templates[i].attr_set_index) {
340                                 cacheable = 1; 
341                                 template_id = i; 
342                         }
343 #ifdef NEW_LOGGING
344                         LDAP_LOG( BACK_META, DETAIL2,
345                                         "Entering QC, querystr = %s\n",
346                                         filterstr->bv_val, 0, 0 );
347 #else /* !NEW_LOGGING */
348                         Debug( LDAP_DEBUG_NONE, "Entering QC, querystr = %s\n",
349                                                 filterstr->bv_val, 0, 0 );
350 #endif /* !NEW_LOGGING */
351                         answerable = (*(qm->qcfunc))(qm, &query, i);
352
353                         if (answerable)
354                                 break;
355                 }
356         }
357
358         if (answerable) {
359 #ifdef NEW_LOGGING
360                 LDAP_LOG( BACK_META, DETAIL1, "QUERY ANSWERABLE\n", 0, 0, 0 );
361 #else /* !NEW_LOGGING */
362                 Debug( LDAP_DEBUG_ANY, "QUERY ANSWERABLE\n", 0, 0, 0 );
363 #endif /* !NEW_LOGGING */
364                 rewriteSession(li->rwinfo, "cacheBase", base->bv_val,
365                                         conn, &cbase, result); 
366                 if (result->type != SUCCESS) { 
367                         ldap_pvt_thread_rdwr_runlock(&qm->templates[i].t_rwlock); 
368                         goto Catch; 
369                 }
370                 if ( cbase == NULL ) {
371                         cachebase = *base;
372                 } else {
373                         cachebase.bv_val = cbase;
374                         cachebase.bv_len = strlen(cbase);
375                 }
376                 dnNormalize(NULL, &cachebase, &ncachebase); 
377         
378                 op->o_caching_on = 1;   
379                 op->o_callback = &cb; 
380                 li->glue_be->be_search(li->glue_be, conn, op, &cachebase,
381                                 &ncachebase, scope, deref, slimit, tlimit,
382                                 filter, filterstr, attrs, attrsonly);
383                 ber_memfree( ncachebase.bv_val );
384                 if ( cachebase.bv_val != base->bv_val ) {
385                         /* free only if rewritten */
386                         free( cachebase.bv_val );
387                 }
388
389                 ldap_pvt_thread_rdwr_runlock(&qm->templates[i].t_rwlock); 
390         } else {
391 #ifdef NEW_LOGGING
392                 LDAP_LOG( BACK_META, DETAIL1, "QUERY NOT ANSWERABLE\n",
393                                         0, 0, 0 );
394 #else /* !NEW_LOGGING */
395                 Debug( LDAP_DEBUG_ANY, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
396 #endif /* !NEW_LOGGING */
397
398                 if ( scope == LDAP_SCOPE_BASE ) {
399                         op_type = META_OP_REQUIRE_SINGLE;
400                 } else {
401                         op_type = META_OP_ALLOW_MULTIPLE;
402                 }
403
404                 lc = metaConnect(li, conn, op, op_type, nbase, result);
405
406                 if (result->type != SUCCESS) 
407                         goto Catch; 
408
409                 ldap_pvt_thread_mutex_lock(&cm->cache_mutex); 
410                 if (cm->num_cached_queries >= cm->max_queries) {
411                         cacheable = 0; 
412                 }
413                 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex); 
414                 
415                 if (cacheable) {
416 #ifdef NEW_LOGGING
417                         LDAP_LOG( BACK_META, DETAIL1,
418                                         "QUERY TEMPLATE CACHEABLE\n",
419                                         0, 0, 0);
420 #else /* !NEW_LOGGING */
421                         Debug( LDAP_DEBUG_ANY, "QUERY TEMPLATE CACHEABLE\n",
422                                         0, 0, 0);
423 #endif /* !NEW_LOGGING */
424                         add_filter_attrs(&new_attrs, attrs, filter_attrs);
425                 } else {
426                         new_attrs = attrs; 
427                 }
428
429                 free(filter_attrs); 
430         
431                 /*
432                  * Array of message id of each target
433                  */
434                 msgid = ch_calloc( sizeof( int ), li->ntargets );
435                 if ( msgid == NULL ) {
436                         result->type = CONN_ERR; 
437                         goto Catch; 
438                 }
439
440                 /*
441                 if (slimit > 0 &&  (slimit <= cm->num_entries_limit))  
442                         slimit = cm->num_entries_limit; 
443                 */
444
445                 /*
446                  * Inits searches
447                  */
448
449                 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
450                         char    *realbase = ( char * )base->bv_val;
451                         int     realscope = scope;
452                         ber_len_t suffixlen;
453                         char    *mapped_filter, **mapped_attrs;
454
455                         /* FIXME: Check for more than one targets */
456                         if ( meta_back_is_candidate(
457                                         &li->targets[i]->suffix, nbase ))
458                                 lsc->candidate = META_CANDIDATE; 
459
460                         if ( lsc->candidate != META_CANDIDATE ) 
461                                 continue;
462
463                         if ( deref != -1 ) {
464                                 ldap_set_option( lsc->ld, LDAP_OPT_DEREF,
465                                                 ( void * )&deref);
466                         }
467                         if ( tlimit != -1 ) {
468                                 ldap_set_option( lsc->ld, LDAP_OPT_TIMELIMIT,
469                                                 ( void * )&tlimit);
470                         }
471                         if ( slimit != -1 ) {
472                                 ldap_set_option( lsc->ld, LDAP_OPT_SIZELIMIT,
473                                                 ( void * )&slimit);
474                         }
475
476                         /*
477                          * modifies the base according to the scope, if required
478                          */
479                         suffixlen = li->targets[ i ]->suffix.bv_len;
480                         if ( suffixlen > nbase->bv_len ) {
481                                 switch ( scope ) {
482                                 case LDAP_SCOPE_SUBTREE:
483                                         /*
484                                          * make the target suffix the new base
485                                          * FIXME: this is very forgiving,
486                                          * because illegal bases may be turned
487                                          * into the suffix of the target.
488                                          */
489                                         if ( dnIsSuffix(
490                                                 &li->targets[ i ]->suffix,
491                                                 nbase ) ) {
492                                                 realbase =
493                                                 li->targets[i]->suffix.bv_val;
494                                         } else {
495                                                 /*
496                                                  * this target is no longer
497                                                  * candidate
498                                                  */
499                                                 lsc->candidate =
500                                                         META_NOT_CANDIDATE;
501                                                 continue;
502                                         }
503                                         break;
504
505                                 case LDAP_SCOPE_ONELEVEL:
506                                         if ( is_one_level_rdn(
507                                                 li->targets[ i ]->suffix.bv_val,
508                                                 suffixlen - nbase->bv_len - 1 )
509                                                 && dnIsSuffix(
510                                                 &li->targets[ i ]->suffix,
511                                                 nbase ) ) {
512                                                 /*
513                                                  * if there is exactly one
514                                                  * level, make the target suffix
515                                                  * the new base, and make scope
516                                                  * "base"
517                                                  */
518                                                 realbase =
519                                                 li->targets[i]->suffix.bv_val;
520                                                 realscope = LDAP_SCOPE_BASE;
521                                                 break;
522                                         } /* else continue with the next case */
523                                 case LDAP_SCOPE_BASE:
524                                         /*
525                                          * this target is no longer candidate
526                                          */
527                                         lsc->candidate = META_NOT_CANDIDATE;
528                                         continue;
529                                 }
530                         }
531
532                         /*
533                          * Rewrite the search base, if required
534                          */
535
536                         rewriteSession(li->targets[i]->rwinfo, "searchBase",
537                                         realbase, conn, &mbase, result); 
538
539                         if (result->type != SUCCESS)
540                                 goto Catch; 
541
542                         if ( mbase == NULL ) {
543                                 mbase = realbase;
544                         }
545
546                         /*
547                          * Rewrite the search filter, if required
548                          */
549                         rewriteSession( li->targets[i]->rwinfo, "searchFilter",
550                                         filterstr->bv_val, conn,
551                                         &mfilter.bv_val, result);
552                         if (result->type != SUCCESS) 
553                                 goto Catch; 
554
555                         if ( mfilter.bv_val != NULL && mfilter.bv_val[ 0 ]
556                                                                 != '\0') {
557                                 mfilter.bv_len = strlen( mfilter.bv_val );
558                         } else {
559                                 if ( mfilter.bv_val != NULL ) {
560                                         free( mfilter.bv_val );
561                                 }
562                                 mfilter = *filterstr;
563                         }
564
565                         /*
566                          * Maps attributes in filter
567                          */
568                         mapped_filter = ldap_back_map_filter(
569                                         &li->targets[i]->at_map,
570                                         &li->targets[i]->oc_map, &mfilter, 0 );
571                         if ( mapped_filter == NULL ) {
572                                 mapped_filter = ( char * )mfilter.bv_val;
573                         } else {
574                                 if ( mfilter.bv_val != filterstr->bv_val ) {
575                                         free( mfilter.bv_val );
576                                 }
577                         }
578                         mfilter.bv_val = NULL;
579                         mfilter.bv_len = 0;
580
581                         /*
582                          * Maps required attributes
583                          */
584                         mapped_attrs = ldap_back_map_attrs(
585                                         &li->targets[ i ]->at_map,
586                                         new_attrs, 0 );
587                         if ( mapped_attrs == NULL && new_attrs) {
588                                 for ( count=0;
589                                       new_attrs[ count ].an_name.bv_val;
590                                       count++)
591                                         ;
592                                 mapped_attrs = ch_malloc( ( count + 1 ) *
593                                                         sizeof(char *));
594                                 for ( count=0;
595                                       new_attrs[ count ].an_name.bv_val;
596                                       count++ ) {
597                                         mapped_attrs[ count ] =
598                                                 new_attrs[count].an_name.bv_val;
599                                 }
600                                 mapped_attrs[ count ] = NULL;
601                         }
602
603                         /*
604                          * Starts the search
605                          */
606                         msgid[ i ] = ldap_search( lsc->ld, mbase, realscope,
607                                                 mapped_filter, mapped_attrs,
608                                                 attrsonly ); 
609                         if ( msgid[ i ] == -1 ) {
610                                 lsc->candidate = META_NOT_CANDIDATE;
611                                 continue;
612                         }
613
614                         if ( mapped_attrs ) {
615                                 free( mapped_attrs );
616                                 mapped_attrs = NULL;
617                         }
618
619                         if ( mapped_filter != filterstr->bv_val ) {
620                                 free( mapped_filter );
621                                 mapped_filter = NULL;
622                         }
623
624                         if ( mbase != realbase ) {
625                                 free( mbase );
626                                 mbase = NULL;
627                         }
628
629                         ++candidates;
630                 }
631
632                 num_entries = handleLdapResult(lc, op, msgid, be, attrs,
633                                 attrsonly, candidates, cacheable, &entry_array,
634                                 curr_limit, slimit, result); 
635
636                 if (result->type != SUCCESS) 
637                         goto Catch; 
638                 if (cacheable && (num_entries <= curr_limit)) {
639 #ifdef NEW_LOGGING
640                         LDAP_LOG( BACK_META, DETAIL1,
641                                         "QUERY CACHEABLE\n", 0, 0, 0 );
642 #else /* !NEW_LOGGING */
643                         Debug( LDAP_DEBUG_ANY, "QUERY CACHEABLE\n", 0, 0, 0 );
644 #endif /* !NEW_LOGGING */
645                         uuid = cache_entries(entry_array, cm, li->glue_be,
646                                                         conn, result); 
647 #ifdef NEW_LOGGING
648                         LDAP_LOG( BACK_META, DETAIL1,
649                                         "Added query %s UUID %s ENTRIES %d\n",
650                                         filterstr->bv_val, uuid, num_entries );
651 #else /* !NEW_LOGGING */
652                         Debug( LDAP_DEBUG_ANY,
653                                         "Added query %s UUID %s ENTRIES %d\n",
654                                         filterstr->bv_val, uuid, num_entries );
655 #endif /* !NEW_LOGGING */
656             
657                         if (result->type != SUCCESS) 
658                                 goto Catch; 
659                         (*(qm->addfunc))(qm, &query, template_id, uuid, result); 
660                         if (result->type != SUCCESS) 
661                                 goto Catch; 
662                         filter = 0; 
663                         attrs = 0; 
664                 }
665         }
666
667 Catch:;
668         switch (result->type) {
669                 case SUCCESS: 
670                         rc=0; 
671                         break; 
672                 case FILTER_ERR: 
673 #ifdef NEW_LOGGING
674                         LDAP_LOG( BACK_META, DETAIL1,
675                                         "Invalid template error\n", 0, 0, 0 );
676 #else /* !NEW_LOGGING */
677                         Debug( LDAP_DEBUG_ANY, "Invalid template error\n",
678                                         0, 0, 0 );
679 #endif /* !NEW_LOGGING */
680                         break; 
681                 case CONN_ERR: 
682                         rc = -1; 
683 #ifdef NEW_LOGGING
684                         LDAP_LOG( BACK_META, DETAIL1,
685                                 "Could not connect to a remote server\n",
686                                 0, 0, 0 );
687 #else /* !NEW_LOGGING */
688                         Debug( LDAP_DEBUG_ANY,
689                                 "Could not connect to a remote server\n",
690                                 0, 0, 0 );
691 #endif /* !NEW_LOGGING */
692                         send_ldap_result(conn, op, LDAP_OTHER,  
693                                         NULL, "Connection error",
694                                         NULL, NULL );
695                         break; 
696                 case RESULT_ERR: 
697                         rc = -1; 
698 #ifdef NEW_LOGGING
699                         LDAP_LOG( BACK_META, DETAIL1,
700                                 "Error in handling ldap_result\n", 0, 0, 0 );
701 #else /* !NEW_LOGGING */
702                         Debug( LDAP_DEBUG_ANY,
703                                 "Error in handling ldap_result\n", 0, 0, 0 );
704 #endif /* !NEW_LOGGING */
705                         break; 
706                 case REWRITING_ERR: 
707                         rc = -1; 
708                         if (result->rc == REWRITE_REGEXEC_UNWILLING) {
709                                 send_ldap_result( conn, op,
710                                                 LDAP_UNWILLING_TO_PERFORM,
711                                                 NULL, "Unwilling to perform",
712                                                 NULL, NULL );
713                         } else {
714                                 send_ldap_result( conn, op, LDAP_OTHER,
715                                                 NULL, "Rewrite error",
716                                                 NULL, NULL );
717                         }
718                         break; 
719                 case MERGE_ERR: 
720                         rc = -1; 
721 #ifdef NEW_LOGGING
722                         LDAP_LOG( BACK_META, DETAIL1,
723                                 "Error in merging entry \n", 0, 0, 0 );
724 #else /* !NEW_LOGGING */
725                         Debug( LDAP_DEBUG_ANY,
726                                 "Error in merging entry \n", 0, 0, 0 );
727 #endif /* !NEW_LOGGING */
728                         break; 
729                 case REMOVE_ERR: 
730                         rc = -1; 
731 #ifdef NEW_LOGGING
732                         LDAP_LOG( BACK_META, DETAIL1,
733                                         "Error in removing query \n",
734                                         0, 0, 0 );
735 #else /* !NEW_LOGGING */
736                         Debug( LDAP_DEBUG_ANY, "Error in removing query \n",
737                                         0, 0, 0 );
738 #endif /* !NEW_LOGGING */
739                         break; 
740                 default:
741                         /* assert(0); */
742                         break;
743         }
744
745         ldap_pvt_thread_mutex_lock(&cm->consistency_mutex); 
746         curr_time = slap_get_time(); 
747         if (curr_time - cm->consistency_time > cm->consistency_cycle_time) {
748                 cm->consistency_time = curr_time; 
749                 consistency_check(be, li->glue_be, conn); 
750         }       
751         ldap_pvt_thread_mutex_unlock(&cm->consistency_mutex); 
752
753         if ( msgid ) {
754                 ch_free( msgid );
755         }
756         if (entry_array)  {
757                 for (i=0; (e = entry_array[i]); i++) {
758                         entry_free(e); 
759                 }
760                 free(entry_array);
761         }
762         if (filter) 
763                 filter_free(filter);
764
765         if (new_attrs) {
766                 if (new_attrs != attrs) 
767                         free(new_attrs); 
768         }
769
770         if (attrs)
771                 free(attrs); 
772
773         if (tempstr.bv_val ) {
774                 free(tempstr.bv_val);
775         }
776         ldap_pvt_thread_mutex_lock(&cm->cache_mutex); 
777         cm->threads--; 
778 #ifdef NEW_LOGGING
779         LDAP_LOG( BACK_META, DETAIL1, "Threads-- = %d\n", cm->threads, 0, 0 ); 
780 #else /* !NEW_LOGGING */
781         Debug( LDAP_DEBUG_ANY, "Threads-- = %d\n", cm->threads, 0, 0 ); 
782 #endif /* !NEW_LOGGING */
783         ldap_pvt_thread_mutex_unlock(&cm->cache_mutex); 
784         return rc;
785 }
786
787
788 static Entry* 
789 meta_create_entry (
790         Backend                 *be,
791         struct metaconn         *lc,
792         int                     target,
793         LDAPMessage             *e,
794         struct exception*       result
795 )
796 {
797         struct metainfo         *li = ( struct metainfo * )be->be_private;
798         struct berval           a, mapped;
799         Entry*                  ent;
800         BerElement              ber = *e->lm_ber;
801         Attribute               *attr, **attrp;
802         struct berval           dummy = { 0, NULL };
803         struct berval           *bv, bdn;
804         const char              *text;
805         char*                   ename = NULL; 
806         if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
807                 result->type = CREATE_ENTRY_ERR;        
808                 return NULL; 
809         }
810         ent = (Entry*)malloc(sizeof(Entry)); 
811
812         /*
813          * Rewrite the dn of the result, if needed
814          */
815         rewriteSession( li->targets[ target ]->rwinfo, "searchResult",
816                         bdn.bv_val, lc->conn, &ent->e_name.bv_val, result );  
817
818         if (result->type != SUCCESS) {
819                 return NULL; 
820         }
821         if ( ent->e_name.bv_val == NULL ) {
822                 ber_dupbv(&(ent->e_name), &bdn);
823         } else {
824 #ifdef NEW_LOGGING
825                 LDAP_LOG( BACK_META, DETAIL1,
826                         "[rw] searchResult[%d]: \"%s\" -> \"%s\"\n",
827                         target, bdn.bv_val, ent->e_name.bv_val );
828 #else /* !NEW_LOGGING */
829                 Debug( LDAP_DEBUG_ARGS, "rw> searchResult[%d]: \"%s\""
830                         " -> \"%s\"\n",
831                         target, bdn.bv_val, ent->e_name.bv_val );
832 #endif /* !NEW_LOGGING */
833                 ent->e_name.bv_len = strlen( ent->e_name.bv_val );
834         }
835                 
836         /*
837          * Note: this may fail if the target host(s) schema differs
838          * from the one known to the meta, and a DN with unknown
839          * attributes is returned.
840          * 
841          * FIXME: should we log anything, or delegate to dnNormalize2?
842          */
843         dnNormalize2( NULL, &ent->e_name, &ent->e_nname ); 
844
845         /*
846         if ( dnNormalize2( NULL, &ent->e_name, &ent->e_nname ) != LDAP_SUCCESS ) {
847                 return LDAP_INVALID_DN_SYNTAX;
848         }
849         */
850
851         /*
852          * cache dn
853          */
854         if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
855                 meta_dncache_update_entry( &li->cache, &ent->e_nname, target );
856         }
857
858         ent->e_id = 0;
859         ent->e_attrs = 0;
860         ent->e_private = 0;
861         ent->e_bv.bv_val = 0; 
862
863         attrp = &ent->e_attrs;
864
865         while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
866                 ldap_back_map( &li->targets[ target ]->at_map, &a, &mapped, 1 );
867                 if ( mapped.bv_val == NULL ) {
868                         continue;
869                 }
870                 attr = ( Attribute * )ch_malloc( sizeof( Attribute ) );
871                 if ( attr == NULL ) {
872                         continue;
873                 }
874                 attr->a_flags = 0;
875                 attr->a_next = 0;
876                 attr->a_desc = NULL;
877                 if ( slap_bv2ad( &mapped, &attr->a_desc, &text ) != LDAP_SUCCESS) {
878                         if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text ) 
879                                         != LDAP_SUCCESS) {
880 #ifdef NEW_LOGGING
881                                 LDAP_LOG( BACK_META, DETAIL1,
882                                         "slap_bv2undef_ad(%s): %s\n",
883                                         mapped.bv_val, text, 0 );
884 #else /* !NEW_LOGGING */
885                                 Debug( LDAP_DEBUG_ANY,
886                                         "slap_bv2undef_ad(%s): "
887                                         "%s\n%s", mapped.bv_val, text, "" );
888 #endif /* !NEW_LOGGING */
889                                 ch_free( attr );
890                                 continue;
891                         }
892                 }
893
894                 /* no subschemaSubentry */
895                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
896                         ch_free(attr);
897                         continue;
898                 }
899
900                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR 
901                                 || attr->a_vals == NULL ) {
902                         attr->a_vals = &dummy;
903                 } else if ( attr->a_desc == slap_schema.si_ad_objectClass ||
904                                 attr->a_desc ==
905                                 slap_schema.si_ad_structuralObjectClass) {
906                         int i, last;
907                         for ( last = 0; attr->a_vals[ last ].bv_val; ++last )
908                                 ;
909                         for ( i = 0, bv = attr->a_vals; bv->bv_val; bv++,i++ ) {
910                                 ldap_back_map( &li->targets[ target]->oc_map,
911                                                 bv, &mapped, 1 );
912                                 if ( mapped.bv_val == NULL ) {
913                                         free( bv->bv_val );
914                                         bv->bv_val = NULL;
915                                         if ( --last < 0 ) {
916                                                 break;
917                                         }
918                                         *bv = attr->a_vals[ last ];
919                                         attr->a_vals[ last ].bv_val = NULL;
920                                         i--;
921                                 } else if ( mapped.bv_val != bv->bv_val ) {
922                                         free( bv->bv_val );
923                                         ber_dupbv( bv, &mapped );
924                                 }
925                         }
926                 /*
927                  * It is necessary to try to rewrite attributes with
928                  * dn syntax because they might be used in ACLs as
929                  * members of groups; since ACLs are applied to the
930                  * rewritten stuff, no dn-based subecj clause could
931                  * be used at the ldap backend side (see
932                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
933                  * The problem can be overcome by moving the dn-based
934                  * ACLs to the target directory server, and letting
935                  * everything pass thru the ldap backend.
936                  */
937                 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
938                                 SLAPD_DN_SYNTAX ) == 0 ) {
939                         int i;
940                         for ( i = 0, bv = attr->a_vals; bv->bv_val; bv++,i++ ) {
941                                 char *newval;
942                                 rewriteSession(li->targets[ target ]->rwinfo,
943                                                 "searchResult", bv->bv_val,
944                                                 lc->conn, &newval, result); 
945                                 if (result->type != SUCCESS) {
946                                         /* FIXME : Handle error */
947                                         result->type = SUCCESS; 
948                                 } else {
949                                         /* left as is */
950                                         if ( newval == NULL ) {
951                                                 break;
952                                         }
953 #ifdef NEW_LOGGING
954                                         LDAP_LOG( BACK_META, DETAIL1,
955                                                 "[rw] searchResult on "
956                                                 "attr=%s: \"%s\" -> \"%s\"\n",
957                                                 attr->a_desc->ad_type->
958                                                 sat_cname.bv_val,
959                                                 bv->bv_val, newval );
960 #else /* !NEW_LOGGING */
961                                         Debug( LDAP_DEBUG_ARGS,
962                                                 "rw> searchResult on attr=%s:"
963                                                 " \"%s\" -> \"%s\"\n",
964                                                 attr->a_desc->ad_type->
965                                                 sat_cname.bv_val,
966                                                 bv->bv_val, newval );
967 #endif /* !NEW_LOGGING */
968                                         free( bv->bv_val );
969                                         bv->bv_val = newval;
970                                         bv->bv_len = strlen( newval );
971                                 }
972                         }
973                 }
974                 *attrp = attr;
975                 attrp = &attr->a_next;
976         }
977         return ent; 
978 }
979
980 static int
981 is_one_level_rdn(
982         const char      *rdn,
983         int             from
984 )
985 {
986         for ( ; from--; ) {
987                 if ( DN_SEPARATOR( rdn[ from ] ) ) {
988                         return 0;
989                 }
990         }
991         return 1;
992 }
993
994 static struct metaconn*  
995 metaConnect(
996         struct metainfo* li, 
997         Connection* conn, 
998         Operation* op, 
999         int op_type, 
1000         struct berval* nbase, 
1001         struct exception* result)
1002 {
1003         struct metaconn* lc; 
1004         result->type = SUCCESS; 
1005         lc = meta_back_getconn( conn, op, op_type, nbase, NULL );
1006         if (!lc) {
1007                 result->type = CONN_ERR; 
1008                 return 0; 
1009         }
1010         return lc; 
1011 }
1012
1013 static void
1014 add_filter_attrs(
1015         AttributeName** new_attrs, 
1016         AttributeName* attrs, 
1017         AttributeName* filter_attrs )
1018 {
1019         struct berval all_user = { sizeof(LDAP_ALL_USER_ATTRIBUTES) -1,
1020                                    LDAP_ALL_USER_ATTRIBUTES };
1021
1022         struct berval all_op = { sizeof(LDAP_ALL_OPERATIONAL_ATTRIBUTES) -1,
1023                                         LDAP_ALL_OPERATIONAL_ATTRIBUTES}; 
1024
1025         int alluser = 0; 
1026         int allop = 0; 
1027         int i; 
1028         int count; 
1029
1030         /* duplicate attrs */
1031         for (count=0; attrs[count].an_name.bv_val; count++) 
1032                 ;
1033         *new_attrs =  (AttributeName*)(malloc((count+1)*sizeof(AttributeName))); 
1034         for (i=0; i<count; i++) {
1035                 /*
1036                 ber_dupbv(&((*new_attrs)[i].an_name), &(attrs[i].an_name)); 
1037                 */
1038                 (*new_attrs)[i].an_name = attrs[i].an_name; 
1039                 (*new_attrs)[i].an_desc = attrs[i].an_desc;  
1040         }
1041         (*new_attrs)[count].an_name.bv_val = NULL; 
1042         (*new_attrs)[count].an_name.bv_len = 0; 
1043
1044
1045         if ((*new_attrs)[0].an_name.bv_val == NULL) {
1046                 *new_attrs = (AttributeName*)(malloc(2*sizeof(AttributeName))); 
1047                 (*new_attrs)[0].an_name.bv_val = "*"; 
1048                 (*new_attrs)[0].an_name.bv_len = 1; 
1049                 (*new_attrs)[1].an_name.bv_val = NULL; 
1050                 (*new_attrs)[1].an_name.bv_len = 0; 
1051                 alluser = 1; 
1052                 count = 1; 
1053         } else {
1054                 alluser = an_find(*new_attrs, &all_user); 
1055                 allop = an_find(*new_attrs, &all_op); 
1056         }
1057
1058         for ( i=0; filter_attrs[i].an_name.bv_val; i++ ) {
1059                 if ( an_find(*new_attrs, &filter_attrs[i].an_name ))
1060                         continue; 
1061                 if ( is_at_operational(filter_attrs[i].an_desc->ad_type)
1062                                                 && allop )
1063                         continue; 
1064                 else if (alluser) 
1065                         continue; 
1066                 *new_attrs = (AttributeName*)(realloc(*new_attrs,
1067                                         (count+2)*sizeof(AttributeName))); 
1068                 (*new_attrs)[count].an_name.bv_val =
1069                                 filter_attrs[i].an_name.bv_val; 
1070                 (*new_attrs)[count].an_name.bv_len =
1071                                 filter_attrs[i].an_name.bv_len; 
1072                 (*new_attrs)[count].an_desc = filter_attrs[i].an_desc; 
1073                 (*new_attrs)[count+1].an_name.bv_val = NULL; 
1074                 (*new_attrs)[count+1].an_name.bv_len = 0; 
1075                 count++; 
1076         }
1077 }
1078
1079 static int 
1080 handleLdapResult(
1081         struct metaconn* lc,
1082         Operation* op, 
1083         int* msgid, Backend* be, 
1084         AttributeName* attrs, 
1085         int attrsonly, 
1086         int candidates, 
1087         int cacheable, 
1088         Entry*** entry_array, 
1089         int curr_limit, 
1090         int slimit,
1091         struct exception* result)
1092 {
1093         Entry  *entry;
1094         char *match = NULL, *err = NULL, *cache_ename = NULL;
1095         int sres; 
1096         int mres = LDAP_SUCCESS; 
1097         int num_entries = 0, count, i, rc;     
1098         struct timeval tv = {0, 0}; 
1099         struct metasingleconn* lsc; 
1100         struct metainfo         *li = ( struct metainfo * )be->be_private;
1101         result->rc = 0; 
1102         result->type = SUCCESS; 
1103
1104         for ( count = 0, rc = 0; candidates > 0; ) {
1105                 int ab, gotit = 0;
1106
1107                 /* check for abandon */
1108                 ab = op->o_abandon;
1109
1110                 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); lsc++, i++ ) {
1111                         if ( lsc->candidate != META_CANDIDATE ) {
1112                                 continue;
1113                         }
1114
1115                         if ( ab ) {
1116                                 ldap_abandon( lsc->ld, msgid[ i ] );
1117                                 result->type = ABANDON_ERR;
1118                                 break; 
1119                         }
1120
1121                         if ( slimit > 0 && num_entries == slimit ) {
1122                                 result->type = SLIMIT_ERR; 
1123                                 break; 
1124                         }
1125
1126                         if ((entry = get_result_entry(be, lc, lsc,
1127                                                 msgid, i, &tv, result))) { 
1128                                 send_search_entry( be, lc->conn, op, entry,
1129                                                 attrs, attrsonly, NULL );
1130                                 if ((cacheable) &&
1131                                                 (num_entries < curr_limit))  {
1132                                         rewriteSession( li->rwinfo,
1133                                                         "cacheResult",
1134                                                         entry->e_name.bv_val,
1135                                                         lc->conn,
1136                                                         &cache_ename, result );  
1137                                         free(entry->e_name.bv_val); 
1138                                         if (result->type != SUCCESS) {
1139                                                 return 0; 
1140                                         }
1141                                         ber_str2bv(cache_ename,
1142                                                 strlen(cache_ename),
1143                                                 0, &entry->e_name); 
1144                                         ber_dupbv(&entry->e_nname,
1145                                                 &entry->e_name); 
1146                                         *entry_array = (Entry**)realloc(
1147                                                         *entry_array,
1148                                                         (( num_entries+2 ) *
1149                                                          sizeof( Entry* )));
1150                                         (*entry_array)[num_entries] = entry;    
1151                                         (*entry_array)[num_entries+1] = NULL;
1152                                 }
1153                                 num_entries++; 
1154                                 gotit = 1; 
1155                         } else if (result->type == REWRITING_ERR) {
1156                                 return 0; 
1157                         } else if (result->type == TIMEOUT_ERR) {
1158                                 result->type = SUCCESS; 
1159                                 continue;  
1160                         } else if (result->type == CREATE_ENTRY_ERR) {
1161                                 break; 
1162                         } else if (result->rc == -1) {
1163                                 break; 
1164                         } else {
1165                                 sres = ldap_back_map_result(result->rc);
1166                                 if (mres == LDAP_SUCCESS &&
1167                                                 sres != LDAP_SUCCESS) {
1168                                         mres = sres; 
1169                                         ldap_get_option(lsc->ld,
1170                                                 LDAP_OPT_ERROR_STRING, &err);
1171                                         ldap_get_option(lsc->ld,
1172                                                 LDAP_OPT_MATCHED_DN, &match);
1173                                 }
1174                                 lsc->candidate = META_NOT_CANDIDATE; 
1175                                 candidates--; 
1176                                 result->type = SUCCESS; 
1177                         }
1178                 }
1179                 switch (result->type) {
1180                 case RESULT_ERR: 
1181 #ifdef NEW_LOGGING
1182                         LDAP_LOG( BACK_META, DETAIL1,
1183                                         "ldap_result error, rc = -1\n",
1184                                         0, 0, 0 );
1185 #else /* !NEW_LOGGING */
1186                         Debug( LDAP_DEBUG_ANY, "ldap_result error, rc = -1\n",
1187                                         0, 0, 0 );
1188 #endif /* !NEW_LOGGING */
1189                         send_search_result( lc->conn, op, LDAP_OTHER, NULL,
1190                                         NULL, NULL, NULL, num_entries );
1191                         return 0; 
1192                 case CREATE_ENTRY_ERR: 
1193 #ifdef NEW_LOGGING
1194                         LDAP_LOG( BACK_META, DETAIL1,
1195                                         "Error in parsing result \n",
1196                                         0, 0, 0 );
1197 #else /* !NEW_LOGGING */
1198                         Debug( LDAP_DEBUG_ANY, "Error in parsing result \n",
1199                                         0, 0, 0 );
1200 #endif /* !NEW_LOGGING */
1201                         send_search_result( lc->conn, op, LDAP_OTHER, NULL,
1202                                         NULL, NULL, NULL, num_entries );
1203                         result->type = RESULT_ERR; 
1204                         return 0; 
1205                 case SLIMIT_ERR: 
1206 #ifdef NEW_LOGGING
1207                         LDAP_LOG( BACK_META, DETAIL1, "Size limit exceeded \n",
1208                                         0, 0, 0 );
1209 #else /* !NEW_LOGGING */
1210                         Debug( LDAP_DEBUG_ANY, "Size limit exceeded \n",
1211                                         0, 0, 0 );
1212 #endif /* !NEW_LOGGING */
1213                         send_search_result( lc->conn, op,
1214                                         LDAP_SIZELIMIT_EXCEEDED,
1215                                         NULL, NULL, NULL, NULL, num_entries );
1216                         result->type = RESULT_ERR; 
1217                         return 0; 
1218                 case ABANDON_ERR: 
1219 #ifdef NEW_LOGGING
1220                         LDAP_LOG( BACK_META, DETAIL1,
1221                                         "search operation abandoned \n",
1222                                         0, 0, 0 );
1223 #else /* !NEW_LOGGING */
1224                         Debug( LDAP_DEBUG_ANY, "search operation abandoned \n",
1225                                         0, 0, 0 );
1226 #endif /* !NEW_LOGGING */
1227                         result->type = RESULT_ERR; 
1228                         return 0; 
1229                 default:
1230                         /* assert( 0 ); */
1231                         break;
1232                 }
1233                 if ( gotit == 0 ) {
1234                         tv.tv_sec = 0;
1235                         tv.tv_usec = 100000;
1236                         ldap_pvt_thread_yield();
1237                 } else {
1238                         tv.tv_sec = 0;
1239                         tv.tv_usec = 0;
1240                 }
1241         }
1242
1243         send_search_result( lc->conn, op, mres, match, err,
1244                                 NULL, NULL, num_entries );
1245
1246         if (err) 
1247                 free(err); 
1248
1249         if (match) 
1250                 free(match); 
1251     
1252         result->type = (mres == LDAP_SUCCESS) ? SUCCESS : RESULT_ERR; 
1253         return num_entries; 
1254 }
1255
1256 static Entry* 
1257 get_result_entry(
1258         Backend* be, 
1259         struct metaconn* lc, 
1260         struct metasingleconn* lsc, 
1261         int* msgid,
1262         int i, 
1263         struct timeval* tv, 
1264         struct exception* result)
1265 {
1266         Entry* entry; 
1267         LDAPMessage     *res, *e; 
1268         int rc; 
1269         int sres = LDAP_SUCCESS; 
1270
1271         rc = ldap_result( lsc->ld, msgid[ i ],
1272                         0, tv, &res );
1273
1274         if ( rc == 0 ) {
1275                 result->type = TIMEOUT_ERR; 
1276                 return NULL; 
1277         } else if ( rc == -1 ) {
1278                 result->rc = -1; 
1279                 result->type = RESULT_ERR; 
1280                 return NULL; 
1281         } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
1282                 e = ldap_first_entry( lsc->ld, res );
1283                 entry = meta_create_entry(be, lc, i, e, result);  
1284                 if (!entry) {
1285                         return NULL; 
1286                 }    
1287                 ldap_msgfree( res );
1288                 result->type = SUCCESS; 
1289                 return entry; 
1290         } else {
1291                 sres = ldap_result2error( lsc->ld,
1292                                 res, 1 );
1293                 result->rc = sres; 
1294                 result->type = RESULT_ERR; 
1295                 return NULL; 
1296         }
1297 }       
1298
1299 static void
1300 rewriteSession(
1301         struct rewrite_info* info, 
1302         const char* rewriteContext, 
1303         const char* string, 
1304         const void* cookie,  
1305         char** base, 
1306         struct exception* result)
1307 {
1308         int rc = rewrite_session(info, rewriteContext, string, cookie, base); 
1309         if (rc != REWRITE_REGEXEC_OK) {
1310                 result->rc = rc; 
1311                 result->type = REWRITING_ERR; 
1312
1313                 if (strcmp(rewriteContext, "searchBase") == 0) 
1314 #ifdef NEW_LOGGING
1315                         LDAP_LOG( BACK_META, DETAIL1,
1316                                 "Problem in rewriting search base\n", 0, 0, 0 );
1317 #else /* !NEW_LOGGING */
1318                         Debug( LDAP_DEBUG_ANY,
1319                                 "Problem in rewriting search base\n", 0, 0, 0 );
1320 #endif /* !NEW_LOGGING */
1321                 if (strcmp(rewriteContext, "searchFilter") == 0) 
1322 #ifdef NEW_LOGGING
1323                         LDAP_LOG( BACK_META, DETAIL1,
1324                                 "Problem in rewriting search filter\n",
1325                                 0, 0, 0 );
1326 #else /* !NEW_LOGGING */
1327                         Debug( LDAP_DEBUG_ANY,
1328                                 "Problem in rewriting search filter\n",
1329                                 0, 0, 0 );
1330 #endif /* !NEW_LOGGING */
1331                 if (strcmp(rewriteContext, "searchResult") == 0) 
1332 #ifdef NEW_LOGGING
1333                         LDAP_LOG( BACK_META, DETAIL1,
1334                                 "Problem in rewriting DN, or DN syntax "
1335                                 "attributes of search result\n", 0, 0, 0 );
1336 #else /* !NEW_LOGGING */
1337                         Debug( LDAP_DEBUG_ANY,
1338                                 "Problem in rewriting DN, or DN syntax "
1339                                 "attributes of search result\n", 0, 0, 0 );
1340 #endif /* !NEW_LOGGING */
1341                 if (strcmp(rewriteContext, "cacheBase") == 0) 
1342 #ifdef NEW_LOGGING
1343                         LDAP_LOG( BACK_META, DETAIL1,
1344                                 "Problem in rewriting search base with "
1345                                 "cache base\n", 0, 0, 0 );
1346 #else /* !NEW_LOGGING */
1347                         Debug( LDAP_DEBUG_ANY,
1348                                 "Problem in rewriting search base with "
1349                                 "cache base\n", 0, 0, 0 );
1350 #endif /* !NEW_LOGGING */
1351                 if (strcmp(rewriteContext, "cacheResult") == 0) 
1352 #ifdef NEW_LOGGING
1353                         LDAP_LOG( BACK_META, DETAIL1,
1354                                 "Problem in rewriting DN for cached entries\n",
1355                                 0, 0, 0 );
1356 #else /* !NEW_LOGGING */
1357                         Debug( LDAP_DEBUG_ANY,
1358                                 "Problem in rewriting DN for cached entries\n",
1359                                 0, 0, 0 );
1360 #endif /* !NEW_LOGGING */
1361                 if (strcmp(rewriteContext, "cacheReturn") == 0) 
1362 #ifdef NEW_LOGGING
1363                         LDAP_LOG( BACK_META, DETAIL1,
1364                                 "Problem in rewriting DN for answerable "
1365                                 "entries\n", 0, 0, 0 );
1366 #else /* !NEW_LOGGING */
1367                         Debug( LDAP_DEBUG_ANY,
1368                                 "Problem in rewriting DN for answerable "
1369                                 "entries\n", 0, 0, 0 );
1370 #endif /* !NEW_LOGGING */
1371         } else {
1372                 result->type = SUCCESS;
1373         }
1374 }
1375
1376 static int 
1377 get_attr_set(
1378         AttributeName* attrs, 
1379         query_manager* qm, 
1380         int num )
1381 {
1382         int i; 
1383         for (i=0; i<num; i++) {
1384                 if (attrscmp(attrs, qm->attr_sets[i].attrs)) 
1385                         return i;
1386         }
1387         return -1; 
1388 }
1389
1390 static int 
1391 attrscmp(
1392         AttributeName* attrs_in, 
1393         AttributeName* attrs)
1394 {
1395         int i, count1, count2; 
1396         if ((attrs_in==NULL) || (attrs==NULL)) 
1397                 return 1; 
1398         for ( count1=0;
1399               attrs_in && attrs_in[count1].an_name.bv_val != NULL;
1400               count1++ )
1401                 ;
1402         for ( count2=0;
1403               attrs && attrs[count2].an_name.bv_val != NULL;
1404               count2++) 
1405                 ;
1406         if ( count1 != count2 )
1407                 return 0; 
1408
1409         for ( i=0; i<count1; i++ ) {
1410                 if ( !an_find(attrs, &attrs_in[i].an_name ))
1411                         return 0; 
1412         }
1413         return 1; 
1414 }
1415
1416 static char* 
1417 cache_entries(
1418         Entry** entry_array, 
1419         cache_manager* cm, 
1420         Backend* be, 
1421         Connection* conn, 
1422         struct exception* result)
1423 {
1424         int i; 
1425         int return_val; 
1426         int cache_size; 
1427         Entry* e; 
1428         struct berval query_uuid; 
1429         struct berval crp_uuid; 
1430         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ], *crpid; 
1431         char crpuuid[40]; 
1432         query_manager* qm = cm->qm;
1433     
1434         result->type = SUCCESS; 
1435         query_uuid.bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf)); 
1436         query_uuid.bv_val = ch_strdup(uuidbuf);
1437
1438 #ifdef NEW_LOGGING
1439         LDAP_LOG( BACK_META, DETAIL1, "UUID for query being added = %s\n",
1440                         uuidbuf, 0, 0 );
1441 #else /* !NEW_LOGGING */
1442         Debug( LDAP_DEBUG_ANY, "UUID for query being added = %s\n",
1443                         uuidbuf, 0, 0 );
1444 #endif /* !NEW_LOGGING */
1445         
1446         for ( i=0; ( entry_array && (e=entry_array[i]) ); i++ ) {
1447 #ifdef NEW_LOGGING
1448                 LDAP_LOG( BACK_META, DETAIL2, "LOCKING REMOVE MUTEX\n",
1449                                 0, 0, 0 );
1450 #else /* !NEW_LOGGING */
1451                 Debug( LDAP_DEBUG_NONE, "LOCKING REMOVE MUTEX\n", 0, 0, 0 );
1452 #endif /* !NEW_LOGGING */
1453                 ldap_pvt_thread_mutex_lock(&cm->remove_mutex); 
1454 #ifdef NEW_LOGGING
1455                 LDAP_LOG( BACK_META, DETAIL2, "LOCKED REMOVE MUTEX\n", 0, 0, 0);
1456 #else /* !NEW_LOGGING */
1457                 Debug( LDAP_DEBUG_NONE, "LOCKED REMOVE MUTEX\n", 0, 0, 0);
1458 #endif /* !NEW_LOGGING */
1459                 if ( cm->cache_size > (cm->thresh_hi) ) {
1460                         while(cm->cache_size > (cm->thresh_lo)) {
1461                                 crpid = cache_replacement(qm);
1462                                 if (crpid == NULL) {
1463                                         result->type = REMOVE_ERR; 
1464                                 } else {
1465                                         strcpy(crpuuid, crpid); 
1466                                         crp_uuid.bv_val = crpuuid; 
1467                                         crp_uuid.bv_len = strlen(crpuuid); 
1468 #ifdef NEW_LOGGING
1469                                         LDAP_LOG( BACK_META, DETAIL1,
1470                                                 "Removing query UUID %s\n",
1471                                                 crpuuid, 0, 0 );
1472 #else /* !NEW_LOGGING */
1473                                         Debug( LDAP_DEBUG_ANY,
1474                                                 "Removing query UUID %s\n",
1475                                                 crpuuid, 0, 0 );
1476 #endif /* !NEW_LOGGING */
1477                                         return_val = remove_query_data(be, conn,
1478                                                         &crp_uuid, result); 
1479 #ifdef NEW_LOGGING
1480                                         LDAP_LOG( BACK_META, DETAIL1,
1481                                                 "QUERY REMOVED, SIZE=%d\n",
1482                                                 return_val, 0, 0);
1483 #else /* !NEW_LOGGING */
1484                                         Debug( LDAP_DEBUG_ANY,
1485                                                 "QUERY REMOVED, SIZE=%d\n",
1486                                                 return_val, 0, 0);
1487 #endif /* !NEW_LOGGING */
1488                                         ldap_pvt_thread_mutex_lock(
1489                                                         &cm->cache_mutex ); 
1490                                         cm->total_entries -= result->rc; 
1491                                         cm->num_cached_queries--; 
1492 #ifdef NEW_LOGGING
1493                                         LDAP_LOG( BACK_META, DETAIL1,
1494                                                 "STORED QUERIES = %d\n",
1495                                                 cm->num_cached_queries, 0, 0 );
1496 #else /* !NEW_LOGGING */
1497                                         Debug( LDAP_DEBUG_ANY,
1498                                                 "STORED QUERIES = %d\n",
1499                                                 cm->num_cached_queries, 0, 0 );
1500 #endif /* !NEW_LOGGING */
1501                                         ldap_pvt_thread_mutex_unlock(
1502                                                         &cm->cache_mutex );
1503                                         cm->cache_size = (return_val >
1504                                                 cm->cache_size) ?
1505                                                 0 : (cm->cache_size-return_val);
1506 #ifdef NEW_LOGGING
1507                                         LDAP_LOG( BACK_META, DETAIL1,
1508                                                 "QUERY REMOVED, CACHE SIZE="
1509                                                 "%d bytes %d entries\n",
1510                                                 cm->cache_size,
1511                                                 cm->total_entries, 0 );
1512 #else /* !NEW_LOGGING */
1513                                         Debug( LDAP_DEBUG_ANY,
1514                                                 "QUERY REMOVED, CACHE SIZE="
1515                                                 "%d bytes %d entries\n",
1516                                                 cm->cache_size,
1517                                                 cm->total_entries, 0 );
1518 #endif /* !NEW_LOGGING */
1519                                 }
1520                         }
1521                 }
1522                 return_val = merge_entry(be, conn, e, &query_uuid, result);
1523                 cm->cache_size += return_val;
1524 #ifdef NEW_LOGGING
1525                 LDAP_LOG( BACK_META, DETAIL1,
1526                         "ENTRY ADDED/MERGED, CACHE SIZE=%d bytes\n",
1527                         cm->cache_size, 0, 0 );
1528 #else /* !NEW_LOGGING */
1529                 Debug( LDAP_DEBUG_ANY,
1530                         "ENTRY ADDED/MERGED, CACHE SIZE=%d bytes\n",
1531                         cm->cache_size, 0, 0 );
1532 #endif /* !NEW_LOGGING */
1533 #ifdef NEW_LOGGING
1534                 LDAP_LOG( BACK_META, DETAIL2, "UNLOCKING REMOVE MUTEX\n",
1535                                 0, 0, 0 );
1536 #else /* !NEW_LOGGING */
1537                 Debug( LDAP_DEBUG_NONE, "UNLOCKING REMOVE MUTEX\n", 0, 0, 0 );
1538 #endif /* !NEW_LOGGING */
1539                 ldap_pvt_thread_mutex_unlock(&cm->remove_mutex); 
1540 #ifdef NEW_LOGGING
1541                 LDAP_LOG( BACK_META, DETAIL2, "UNLOCKED REMOVE MUTEX\n",
1542                                 0, 0, 0 );
1543 #else /* !NEW_LOGGING */
1544                 Debug( LDAP_DEBUG_NONE, "UNLOCKED REMOVE MUTEX\n", 0, 0, 0 );
1545 #endif /* !NEW_LOGGING */
1546                 if (result->type != SUCCESS) 
1547                         return 0; 
1548                 ldap_pvt_thread_mutex_lock(&cm->cache_mutex); 
1549                 cm->total_entries += result->rc; 
1550 #ifdef NEW_LOGGING
1551                 LDAP_LOG( BACK_META, DETAIL1,
1552                         "ENTRY ADDED/MERGED, SIZE=%d, CACHED ENTRIES=%d\n",
1553                         return_val, cm->total_entries, 0 );
1554 #else /* !NEW_LOGGING */
1555                 Debug( LDAP_DEBUG_ANY,
1556                         "ENTRY ADDED/MERGED, SIZE=%d, CACHED ENTRIES=%d\n",
1557                         return_val, cm->total_entries, 0 );
1558 #endif /* !NEW_LOGGING */
1559                 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex); 
1560         }
1561         ldap_pvt_thread_mutex_lock(&cm->cache_mutex); 
1562         cm->num_cached_queries++; 
1563 #ifdef NEW_LOGGING
1564         LDAP_LOG( BACK_META, DETAIL1, "STORED QUERIES = %d\n",
1565                         cm->num_cached_queries, 0, 0 );
1566 #else /* !NEW_LOGGING */
1567         Debug( LDAP_DEBUG_ANY, "STORED QUERIES = %d\n",
1568                         cm->num_cached_queries, 0, 0 );
1569 #endif /* !NEW_LOGGING */
1570         ldap_pvt_thread_mutex_unlock(&cm->cache_mutex); 
1571
1572         return query_uuid.bv_val; 
1573 }
1574
1575 static int 
1576 is_temp_answerable(
1577         int attr_set, 
1578         struct berval* tempstr, 
1579         query_manager* qm, 
1580         int template_id )
1581 {
1582         int i; 
1583         int* id_array; 
1584         char* str;
1585         int result = 0; 
1586         i = qm->templates[template_id].attr_set_index; 
1587         str = qm->templates[template_id].querystr; 
1588
1589         if (attr_set == i) {
1590                 result = 1; 
1591         } else { 
1592                 id_array = qm->attr_sets[attr_set].ID_array;    
1593
1594                 while (*id_array != -1) {
1595                         if (*id_array == i) 
1596                                 result = 1; 
1597                         id_array++; 
1598                 }
1599         }
1600         if (!result) 
1601                 return 0; 
1602         if (strcasecmp(str, tempstr->bv_val) == 0)  
1603                 return 1; 
1604         return 0; 
1605 }
1606
1607 static void 
1608 consistency_check(Backend* be, Backend* glue_be, Connection* conn)
1609 {
1610         struct metainfo *li = ( struct metainfo * )be->be_private;
1611         cache_manager*  cm = li->cm; 
1612         query_manager* qm = cm->qm; 
1613         CachedQuery* query, *query_prev; 
1614         time_t curr_time; 
1615         struct berval uuid;  
1616         struct exception result; 
1617         int i, return_val; 
1618         QueryTemplate* templ; 
1619     
1620       
1621         for (i=0; qm->templates[i].querystr; i++) {
1622                 templ = qm->templates + i; 
1623                 query = templ->query_last; 
1624                 curr_time = slap_get_time(); 
1625                 ldap_pvt_thread_mutex_lock(&cm->remove_mutex); 
1626                 while (query && (query->expiry_time < curr_time)) {
1627                         ldap_pvt_thread_mutex_lock(&qm->lru_mutex); 
1628                         remove_query(qm, query); 
1629                         ldap_pvt_thread_mutex_unlock(&qm->lru_mutex); 
1630 #ifdef NEW_LOGGING
1631                         LDAP_LOG( BACK_META, DETAIL1, "Lock CR index = %d\n",
1632                                         i, 0, 0 );
1633 #else /* !NEW_LOGGING */
1634                         Debug( LDAP_DEBUG_ANY, "Lock CR index = %d\n",
1635                                         i, 0, 0 );
1636 #endif /* !NEW_LOGGING */
1637                         ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);  
1638                         remove_from_template(query, templ); 
1639 #ifdef NEW_LOGGING
1640                         LDAP_LOG( BACK_META, DETAIL1,
1641                                         "TEMPLATE %d QUERIES-- %d\n",
1642                                         i, templ->no_of_queries, 0 );
1643 #else /* !NEW_LOGGING */
1644                         Debug( LDAP_DEBUG_ANY, "TEMPLATE %d QUERIES-- %d\n",
1645                                         i, templ->no_of_queries, 0 );
1646 #endif /* !NEW_LOGGING */
1647 #ifdef NEW_LOGGING
1648                         LDAP_LOG( BACK_META, DETAIL1, "Unlock CR index = %d\n",
1649                                         i, 0, 0 );
1650 #else /* !NEW_LOGGING */
1651                         Debug( LDAP_DEBUG_ANY, "Unlock CR index = %d\n",
1652                                         i, 0, 0 );
1653 #endif /* !NEW_LOGGING */
1654                         ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);  
1655                         uuid.bv_val = query->q_uuid; 
1656                         uuid.bv_len = strlen(query->q_uuid); 
1657                         return_val = remove_query_data(glue_be, conn,
1658                                                 &uuid, &result); 
1659 #ifdef NEW_LOGGING
1660                         LDAP_LOG( BACK_META, DETAIL1,
1661                                         "STALE QUERY REMOVED, SIZE=%d\n",
1662                                         return_val, 0, 0 );
1663 #else /* !NEW_LOGGING */
1664                         Debug( LDAP_DEBUG_ANY, "STALE QUERY REMOVED, SIZE=%d\n",
1665                                                 return_val, 0, 0 );
1666 #endif /* !NEW_LOGGING */
1667                         ldap_pvt_thread_mutex_lock(&cm->cache_mutex); 
1668                         cm->total_entries -= result.rc; 
1669                         cm->num_cached_queries--; 
1670 #ifdef NEW_LOGGING
1671                         LDAP_LOG( BACK_META, DETAIL1, "STORED QUERIES = %d\n",
1672                                         cm->num_cached_queries, 0, 0 );
1673 #else /* !NEW_LOGGING */
1674                         Debug( LDAP_DEBUG_ANY, "STORED QUERIES = %d\n",
1675                                         cm->num_cached_queries, 0, 0 );
1676 #endif /* !NEW_LOGGING */
1677                         ldap_pvt_thread_mutex_unlock(&cm->cache_mutex); 
1678                         cm->cache_size = (return_val > cm->cache_size) ?
1679                                                 0: (cm->cache_size-return_val);
1680 #ifdef NEW_LOGGING
1681                         LDAP_LOG( BACK_META, DETAIL1,
1682                                 "STALE QUERY REMOVED, CACHE SIZE=%d bytes %d "
1683                                 "entries\n", cm->cache_size,
1684                                 cm->total_entries, 0 );
1685 #else /* !NEW_LOGGING */
1686                         Debug( LDAP_DEBUG_ANY,
1687                                 "STALE QUERY REMOVED, CACHE SIZE=%d bytes %d "
1688                                 "entries\n", cm->cache_size,
1689                                 cm->total_entries, 0 );
1690 #endif /* !NEW_LOGGING */
1691                         query_prev = query; 
1692                         query = query->prev; 
1693                         free_query(query_prev); 
1694                 }
1695                 ldap_pvt_thread_mutex_unlock(&cm->remove_mutex); 
1696         }
1697 }
1698
1699 static int
1700 cache_back_sentry(
1701         Backend* glue_be, 
1702         Connection* conn,
1703         Operation* op, 
1704         Entry* e, 
1705         AttributeName* attrs, 
1706         int attrsonly, 
1707         LDAPControl** ctrls )
1708
1709         slap_callback* cb = op->o_callback; 
1710         Backend* be = (Backend*)(cb->sc_private); 
1711         struct metainfo *li = ( struct metainfo * )be->be_private;
1712  
1713         char* ename = NULL; 
1714         struct exception result; 
1715         struct berval dn; 
1716         struct berval ndn; 
1717
1718         dn = e->e_name; 
1719         ndn = e->e_nname; 
1720
1721         rewriteSession( li->rwinfo,
1722                 "cacheReturn", e->e_name.bv_val, conn, &ename, &result );  
1723         ber_str2bv(ename, strlen(ename), 0, &e->e_name); 
1724         ber_dupbv(&e->e_nname, &e->e_name); 
1725
1726         op->o_callback = NULL; 
1727         send_search_entry(be, conn, op, e, attrs, attrsonly, ctrls); 
1728  
1729         e->e_name = dn; 
1730         e->e_nname = ndn; 
1731
1732         op->o_callback = cb; 
1733         return 0;  
1734 }
1735 #endif