]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/search.c
seems to fix the equivalent of ITS#4429 in back-meta; had to give up per-target conn...
[openldap] / servers / slapd / back-meta / search.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2006 The OpenLDAP Foundation.
5  * Portions Copyright 2001-2003 Pierangelo Masarati.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by the Howard Chu for inclusion
19  * in OpenLDAP Software and subsequently enhanced by Pierangelo
20  * Masarati.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/socket.h>
28 #include <ac/string.h>
29 #include <ac/time.h>
30
31 #include "slap.h"
32 #include "../back-ldap/back-ldap.h"
33 #include "back-meta.h"
34 #undef ldap_debug       /* silence a warning in ldap-int.h */
35 #include "ldap_log.h"
36 #include "../../../libraries/libldap/ldap-int.h"
37
38 static int
39 meta_send_entry(
40         Operation       *op,
41         SlapReply       *rs,
42         metaconn_t      *mc,
43         int             i,
44         LDAPMessage     *e );
45
46 typedef enum meta_search_candidate_t {
47         META_SEARCH_ERR = -1,
48         META_SEARCH_NOT_CANDIDATE,
49         META_SEARCH_CANDIDATE
50 } meta_search_candidate_t;
51
52 static meta_search_candidate_t
53 meta_back_search_start(
54         Operation               *op,
55         SlapReply               *rs,
56         dncookie                *dc,
57         metasingleconn_t        *msc,
58         int                     candidate,
59         SlapReply               *candidates
60 )
61 {
62         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
63         struct berval   realbase = op->o_req_dn;
64         int             realscope = op->ors_scope;
65         ber_len_t       suffixlen = 0;
66         struct berval   mbase = BER_BVNULL; 
67         struct berval   mfilter = BER_BVNULL;
68         char            **mapped_attrs = NULL;
69         int             rc;
70         meta_search_candidate_t retcode;
71         struct timeval  tv, *tvp = NULL;
72
73         /* should we check return values? */
74         if ( op->ors_deref != -1 ) {
75                 ldap_set_option( msc->msc_ld, LDAP_OPT_DEREF,
76                                 ( void * )&op->ors_deref );
77         }
78
79         if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
80                 tv.tv_sec = op->ors_tlimit > 0 ? op->ors_tlimit : 1;
81                 tvp = &tv;
82         }
83
84         dc->target = &mi->mi_targets[ candidate ];
85
86         /*
87          * modifies the base according to the scope, if required
88          */
89         suffixlen = mi->mi_targets[ candidate ].mt_nsuffix.bv_len;
90         if ( suffixlen > op->o_req_ndn.bv_len ) {
91                 switch ( op->ors_scope ) {
92                 case LDAP_SCOPE_SUBTREE:
93                         /*
94                          * make the target suffix the new base
95                          * FIXME: this is very forgiving, because
96                          * "illegal" searchBases may be turned
97                          * into the suffix of the target; however,
98                          * the requested searchBase already passed
99                          * thru the candidate analyzer...
100                          */
101                         if ( dnIsSuffix( &mi->mi_targets[ candidate ].mt_nsuffix,
102                                         &op->o_req_ndn ) )
103                         {
104                                 realbase = mi->mi_targets[ candidate ].mt_nsuffix;
105                                 if ( mi->mi_targets[ candidate ].mt_scope == LDAP_SCOPE_SUBORDINATE ) {
106                                         realscope = LDAP_SCOPE_SUBORDINATE;
107                                 }
108
109                         } else {
110                                 /*
111                                  * this target is no longer candidate
112                                  */
113                                 return META_SEARCH_NOT_CANDIDATE;
114                         }
115                         break;
116
117                 case LDAP_SCOPE_SUBORDINATE:
118                 case LDAP_SCOPE_ONELEVEL:
119                 {
120                         struct berval   rdn = mi->mi_targets[ candidate ].mt_nsuffix;
121                         rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
122                         if ( dnIsOneLevelRDN( &rdn )
123                                         && dnIsSuffix( &mi->mi_targets[ candidate ].mt_nsuffix, &op->o_req_ndn ) )
124                         {
125                                 /*
126                                  * if there is exactly one level,
127                                  * make the target suffix the new
128                                  * base, and make scope "base"
129                                  */
130                                 realbase = mi->mi_targets[ candidate ].mt_nsuffix;
131                                 if ( op->ors_scope == LDAP_SCOPE_SUBORDINATE ) {
132                                         if ( mi->mi_targets[ candidate ].mt_scope == LDAP_SCOPE_SUBORDINATE ) {
133                                                 realscope = LDAP_SCOPE_SUBORDINATE;
134                                         } else {
135                                                 realscope = LDAP_SCOPE_SUBTREE;
136                                         }
137                                 } else {
138                                         realscope = LDAP_SCOPE_BASE;
139                                 }
140                                 break;
141                         } /* else continue with the next case */
142                 }
143
144                 case LDAP_SCOPE_BASE:
145                         /*
146                          * this target is no longer candidate
147                          */
148                         return META_SEARCH_NOT_CANDIDATE;
149                 }
150         }
151
152         /*
153          * Rewrite the search base, if required
154          */
155         dc->ctx = "searchBase";
156         switch ( ldap_back_dn_massage( dc, &realbase, &mbase ) ) {
157         default:
158                 break;
159
160         case REWRITE_REGEXEC_UNWILLING:
161                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
162                 rs->sr_text = "Operation not allowed";
163                 send_ldap_result( op, rs );
164                 return META_SEARCH_ERR;
165
166         case REWRITE_REGEXEC_ERR:
167
168                 /*
169                  * this target is no longer candidate
170                  */
171                 return META_SEARCH_NOT_CANDIDATE;
172         }
173
174         /*
175          * Maps filter
176          */
177         rc = ldap_back_filter_map_rewrite( dc, op->ors_filter,
178                         &mfilter, BACKLDAP_MAP );
179         switch ( rc ) {
180         case LDAP_SUCCESS:
181                 break;
182
183         case LDAP_COMPARE_FALSE:
184         default:
185                 /*
186                  * this target is no longer candidate
187                  */
188                 retcode = META_SEARCH_NOT_CANDIDATE;
189                 goto done;
190         }
191
192         /*
193          * Maps required attributes
194          */
195         rc = ldap_back_map_attrs( &mi->mi_targets[ candidate ].mt_rwmap.rwm_at,
196                         op->ors_attrs, BACKLDAP_MAP, &mapped_attrs );
197         if ( rc != LDAP_SUCCESS ) {
198                 /*
199                  * this target is no longer candidate
200                  */
201                 retcode = META_SEARCH_NOT_CANDIDATE;
202                 goto done;
203         }
204
205         /*
206          * Starts the search
207          */
208         rc = ldap_search_ext( msc->msc_ld,
209                         mbase.bv_val, realscope, mfilter.bv_val,
210                         mapped_attrs, op->ors_attrsonly,
211                         op->o_ctrls, NULL, tvp, op->ors_slimit,
212                         &candidates[ candidate ].sr_msgid ); 
213         if ( rc == LDAP_SUCCESS ) {
214                 retcode = META_SEARCH_CANDIDATE;
215
216         } else {
217                 candidates[ candidate ].sr_msgid = -1;
218                 retcode = META_SEARCH_NOT_CANDIDATE;
219         }
220
221 done:;
222         if ( mapped_attrs ) {
223                 free( mapped_attrs );
224         }
225         if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
226                 free( mfilter.bv_val );
227         }
228         if ( mbase.bv_val != realbase.bv_val ) {
229                 free( mbase.bv_val );
230         }
231
232         return retcode;
233 }
234
235 int
236 meta_back_search( Operation *op, SlapReply *rs )
237 {
238         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
239         metaconn_t      *mc;
240         struct timeval  tv = { 0, 0 };
241         time_t          stoptime = (time_t)-1;
242         LDAPMessage     *res = NULL, *e;
243         int             rc = 0, sres = LDAP_SUCCESS;
244         char            *matched = NULL;
245         int             last = 0, ncandidates = 0,
246                         initial_candidates = 0, candidate_match = 0;
247         long            i;
248         dncookie        dc;
249         int             is_ok = 0;
250         void            *savepriv;
251         SlapReply       *candidates = meta_back_candidates_get( op );
252
253         /*
254          * controls are set in ldap_back_dobind()
255          * 
256          * FIXME: in case of values return filter, we might want
257          * to map attrs and maybe rewrite value
258          */
259         mc = meta_back_getconn( op, rs, NULL, LDAP_BACK_SENDERR );
260         if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
261                 return rs->sr_err;
262         }
263
264         dc.conn = op->o_conn;
265         dc.rs = rs;
266
267         /*
268          * Inits searches
269          */
270         for ( i = 0; i < mi->mi_ntargets; i++ ) {
271                 metasingleconn_t        *msc = &mc->mc_conns[ i ];
272
273                 candidates[ i ].sr_msgid = -1;
274                 candidates[ i ].sr_matched = NULL;
275                 candidates[ i ].sr_text = NULL;
276                 candidates[ i ].sr_ref = NULL;
277                 candidates[ i ].sr_ctrls = NULL;
278
279                 if ( candidates[ i ].sr_tag != META_CANDIDATE
280                         || candidates[ i ].sr_err != LDAP_SUCCESS )
281                 {
282                         continue;
283                 }
284
285                 switch ( meta_back_search_start( op, rs, &dc, msc, i, candidates ) )
286                 {
287                 case META_SEARCH_NOT_CANDIDATE:
288                         break;
289
290                 case META_SEARCH_CANDIDATE:
291                         candidates[ i ].sr_type = REP_INTERMEDIATE;
292                         ++ncandidates;
293                         break;
294
295                 case META_SEARCH_ERR:
296                         rc = -1;
297                         goto finish;
298                 }
299         }
300
301         initial_candidates = ncandidates;
302
303 #if 0
304         {
305                 char    cnd[BUFSIZ];
306                 int     i;
307
308                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
309                         if ( candidates[ i ].sr_tag == META_CANDIDATE ) {
310                                 cnd[ i ] = '*';
311                         } else {
312                                 cnd[ i ] = ' ';
313                         }
314                 }
315                 cnd[ i ] = '\0';
316
317                 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: ncandidates=%d "
318                         "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
319         }
320 #endif
321
322         if ( initial_candidates == 0 ) {
323                 /* NOTE: here we are not sending any matchedDN;
324                  * this is intended, because if the back-meta
325                  * is serving this search request, but no valid
326                  * candidate could be looked up, it means that
327                  * there is a hole in the mapping of the targets
328                  * and thus no knowledge of any remote superior
329                  * is available */
330                 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: "
331                         "base=\"%s\" scope=%d: "
332                         "no candidate could be selected\n",
333                         op->o_log_prefix, op->o_req_dn.bv_val,
334                         op->ors_scope );
335
336                 /* FIXME: we're sending the first error we encounter;
337                  * maybe we should pick the worst... */
338                 rc = LDAP_NO_SUCH_OBJECT;
339                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
340                         if ( candidates[ i ].sr_tag == META_CANDIDATE
341                                 && candidates[ i ].sr_err != LDAP_SUCCESS )
342                         {
343                                 rc = candidates[ i ].sr_err;
344                                 break;
345                         }
346                 }
347
348                 send_ldap_error( op, rs, rc, NULL );
349
350                 goto finish;
351         }
352
353         /* We pull apart the ber result, stuff it into a slapd entry, and
354          * let send_search_entry stuff it back into ber format. Slow & ugly,
355          * but this is necessary for version matching, and for ACL processing.
356          */
357
358         if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
359                 stoptime = op->o_time + op->ors_tlimit;
360         }
361
362         /*
363          * In case there are no candidates, no cycle takes place...
364          *
365          * FIXME: we might use a queue, to better balance the load 
366          * among the candidates
367          */
368         for ( rc = 0; ncandidates > 0; ) {
369                 int     gotit = 0, doabandon = 0;
370
371                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
372                         metasingleconn_t        *msc = &mc->mc_conns[ i ];
373
374                         if ( candidates[ i ].sr_msgid == -1 ) {
375                                 continue;
376                         }
377
378                         /* check for abandon */
379                         if ( op->o_abandon ) {
380                                 break;
381                         }
382                         
383                         /*
384                          * FIXME: handle time limit as well?
385                          * Note that target servers are likely 
386                          * to handle it, so at some time we'll
387                          * get a LDAP_TIMELIMIT_EXCEEDED from
388                          * one of them ...
389                          */
390 get_result:;
391                         rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
392                                         LDAP_MSG_ONE, &tv, &res );
393
394                         if ( rc == 0 ) {
395                                 /* FIXME: res should not need to be freed */
396                                 assert( res == NULL );
397
398                                 /* check time limit */
399                                 if ( op->ors_tlimit != SLAP_NO_LIMIT
400                                                 && slap_get_time() > stoptime )
401                                 {
402                                         doabandon = 1;
403                                         rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
404                                         savepriv = op->o_private;
405                                         op->o_private = (void *)i;
406                                         send_ldap_result( op, rs );
407                                         op->o_private = savepriv;
408                                         goto finish;
409                                 }
410
411                                 continue;
412
413                         } else if ( rc == -1 ) {
414 really_bad:;
415                                 /* something REALLY bad happened! */
416                                 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
417                                         candidates[ i ].sr_type = REP_RESULT;
418
419                                         if ( meta_back_retry( op, rs, &mc, i, LDAP_BACK_DONTSEND ) ) {
420                                                 switch ( meta_back_search_start( op, rs, &dc, msc, i, candidates ) )
421                                                 {
422                                                 case META_SEARCH_CANDIDATE:
423                                                         goto get_result;
424
425                                                 default:
426                                                         rc = rs->sr_err = LDAP_OTHER;
427                                                         goto finish;
428                                                 }
429                                         }
430
431                                         savepriv = op->o_private;
432                                         op->o_private = (void *)i;
433                                         send_ldap_result( op, rs );
434                                         op->o_private = savepriv;
435                                         goto finish;
436                                 }
437
438                                 /*
439                                  * When no candidates are left,
440                                  * the outer cycle finishes
441                                  */
442                                 candidates[ i ].sr_msgid = -1;
443                                 --ncandidates;
444                                 rs->sr_err = candidates[ i ].sr_err = LDAP_OTHER;
445                                 rs->sr_text = "remote server unavailable";
446
447                         } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
448                                 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
449                                         /* don't retry any more... */
450                                         candidates[ i ].sr_type = REP_RESULT;
451                                 }
452
453                                 is_ok++;
454
455                                 e = ldap_first_entry( msc->msc_ld, res );
456                                 savepriv = op->o_private;
457                                 op->o_private = (void *)i;
458                                 rs->sr_err = meta_send_entry( op, rs, mc, i, e );
459                                 ldap_msgfree( res );
460                                 res = NULL;
461
462                                 switch ( rs->sr_err ) {
463                                 case LDAP_SIZELIMIT_EXCEEDED:
464                                         savepriv = op->o_private;
465                                         op->o_private = (void *)i;
466                                         send_ldap_result( op, rs );
467                                         op->o_private = savepriv;
468                                         rs->sr_err = LDAP_SUCCESS;
469                                         goto finish;
470
471                                 case LDAP_UNAVAILABLE:
472                                         rs->sr_err = LDAP_OTHER;
473                                         goto finish;
474                                 }
475                                 op->o_private = savepriv;
476
477                                 gotit = 1;
478
479 #if 0
480                                 /*
481                                  * If scope is BASE, we need to jump out
482                                  * as soon as one entry is found; if
483                                  * the target pool is properly crafted,
484                                  * this should correspond to the sole
485                                  * entry that has the base DN
486                                  */
487                                 /* FIXME: this defeats the purpose of
488                                  * doing a search with scope == base and
489                                  * sizelimit = 1 to determine if a
490                                  * candidate is actually unique */
491                                 if ( op->ors_scope == LDAP_SCOPE_BASE
492                                                 && rs->sr_nentries > 0 )
493                                 {
494                                         doabandon = 1;
495                                         ncandidates = 0;
496                                         sres = LDAP_SUCCESS;
497                                         break;
498                                 }
499 #endif
500
501                         } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
502                                 char            **references = NULL;
503                                 int             cnt;
504
505                                 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
506                                         /* don't retry any more... */
507                                         candidates[ i ].sr_type = REP_RESULT;
508                                 }
509
510                                 is_ok++;
511
512                                 rc = ldap_parse_reference( msc->msc_ld, res,
513                                                 &references, &rs->sr_ctrls, 1 );
514                                 res = NULL;
515
516                                 if ( rc != LDAP_SUCCESS ) {
517                                         continue;
518                                 }
519
520                                 if ( references == NULL ) {
521                                         continue;
522                                 }
523
524 #ifdef ENABLE_REWRITE
525                                 dc.ctx = "referralDN";
526 #else /* ! ENABLE_REWRITE */
527                                 dc.tofrom = 0;
528                                 dc.normalized = 0;
529 #endif /* ! ENABLE_REWRITE */
530
531                                 /* FIXME: merge all and return at the end */
532
533                                 for ( cnt = 0; references[ cnt ]; cnt++ )
534                                         ;
535
536                                 rs->sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
537
538                                 for ( cnt = 0; references[ cnt ]; cnt++ ) {
539                                         ber_str2bv( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ] );
540                                 }
541                                 BER_BVZERO( &rs->sr_ref[ cnt ] );
542
543                                 ( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref );
544
545                                 if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
546                                         /* ignore return value by now */
547                                         savepriv = op->o_private;
548                                         op->o_private = (void *)i;
549                                         ( void )send_search_reference( op, rs );
550                                         op->o_private = savepriv;
551
552                                         ber_bvarray_free( rs->sr_ref );
553                                         rs->sr_ref = NULL;
554                                 }
555
556                                 /* cleanup */
557                                 if ( references ) {
558                                         ber_memvfree( (void **)references );
559                                 }
560
561                                 if ( rs->sr_ctrls ) {
562                                         ldap_controls_free( rs->sr_ctrls );
563                                         rs->sr_ctrls = NULL;
564                                 }
565
566                         } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
567                                 char            buf[ SLAP_TEXT_BUFLEN ];
568                                 char            **references = NULL;
569
570                                 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
571                                         /* don't retry any more... */
572                                         candidates[ i ].sr_type = REP_RESULT;
573                                 }
574
575                                 /* NOTE: ignores response controls
576                                  * (and intermediate response controls
577                                  * as well, except for those with search
578                                  * references); this may not be correct,
579                                  * but if they're not ignored then
580                                  * back-meta would need to merge them
581                                  * consistently (think of pagedResults...)
582                                  */
583                                 if ( ldap_parse_result( msc->msc_ld,
584                                                         res,
585                                                         &candidates[ i ].sr_err,
586                                                         (char **)&candidates[ i ].sr_matched,
587                                                         NULL /* (char **)&candidates[ i ].sr_text */ ,
588                                                         &references,
589                                                         NULL /* &candidates[ i ].sr_ctrls (unused) */ ,
590                                                         1 ) != LDAP_SUCCESS )
591                                 {
592                                         res = NULL;
593                                         ldap_get_option( msc->msc_ld,
594                                                         LDAP_OPT_ERROR_NUMBER,
595                                                         &rs->sr_err );
596                                         sres = slap_map_api2result( rs );
597                                         candidates[ i ].sr_type = REP_RESULT;
598                                         goto really_bad;
599                                 }
600
601                                 rs->sr_err = candidates[ i ].sr_err;
602                                 sres = slap_map_api2result( rs );
603                                 res = NULL;
604
605                                 /* massage matchedDN if need be */
606                                 if ( candidates[ i ].sr_matched != NULL ) {
607                                         {
608                                                 struct berval   match, mmatch;
609
610                                                 ber_str2bv( candidates[ i ].sr_matched,
611                                                         0, 0, &match );
612                                                 candidates[ i ].sr_matched = NULL;
613
614                                                 dc.ctx = "matchedDN";
615                                                 dc.target = &mi->mi_targets[ i ];
616                                                 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
617                                                         if ( mmatch.bv_val == match.bv_val ) {
618                                                                 candidates[ i ].sr_matched = ch_strdup( mmatch.bv_val );
619
620                                                         } else {
621                                                                 candidates[ i ].sr_matched = mmatch.bv_val;
622                                                         }
623
624                                                         candidate_match++;
625                                                 } 
626                                                 ldap_memfree( match.bv_val );
627                                         }
628                                 }
629
630
631                                 /* add references to array */
632                                 if ( references ) {
633                                         BerVarray       sr_ref;
634                                         int             cnt;
635
636                                         for ( cnt = 0; references[ cnt ]; cnt++ )
637                                                 ;
638
639                                         sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
640
641                                         for ( cnt = 0; references[ cnt ]; cnt++ ) {
642                                                 ber_str2bv( references[ cnt ], 0, 1, &sr_ref[ cnt ] );
643                                         }
644                                         BER_BVZERO( &sr_ref[ cnt ] );
645
646                                         ( void )ldap_back_referral_result_rewrite( &dc, sr_ref );
647                                 
648                                         /* cleanup */
649                                         ber_memvfree( (void **)references );
650
651                                         if ( rs->sr_v2ref == NULL ) {
652                                                 rs->sr_v2ref = sr_ref;
653
654                                         } else {
655                                                 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
656                                                         ber_bvarray_add( &rs->sr_v2ref, &sr_ref[ cnt ] );
657                                                 }
658                                                 ber_memfree( sr_ref );
659                                         }
660                                 }
661
662                                 rs->sr_err = candidates[ i ].sr_err;
663                                 sres = slap_map_api2result( rs );
664
665                                 snprintf( buf, sizeof( buf ),
666                                         "%s meta_back_search[%ld] "
667                                         "match=\"%s\" err=%ld\n",
668                                         op->o_log_prefix, i,
669                                         candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
670                                         (long) candidates[ i ].sr_err );
671                                 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
672
673                                 switch ( sres ) {
674                                 case LDAP_NO_SUCH_OBJECT:
675                                         /* is_ok is touched any time a valid
676                                          * (even intermediate) result is
677                                          * returned; as a consequence, if
678                                          * a candidate returns noSuchObject
679                                          * it is ignored and the candidate
680                                          * is simply demoted. */
681                                         if ( is_ok ) {
682                                                 sres = LDAP_SUCCESS;
683                                         }
684                                         break;
685
686                                 case LDAP_SUCCESS:
687                                 case LDAP_REFERRAL:
688                                         is_ok++;
689                                         break;
690
691                                 default:
692                                         if ( META_BACK_ONERR_STOP( mi ) ) {
693                                                 savepriv = op->o_private;
694                                                 op->o_private = (void *)i;
695                                                 send_ldap_result( op, rs );
696                                                 op->o_private = savepriv;
697                                                 goto finish;
698                                         }
699                                         break;
700                                 }
701
702                                 last = i;
703                                 rc = 0;
704
705                                 /*
706                                  * When no candidates are left,
707                                  * the outer cycle finishes
708                                  */
709                                 candidates[ i ].sr_msgid = -1;
710                                 --ncandidates;
711
712                         } else {
713                                 assert( 0 );
714                                 goto really_bad;
715                         }
716                 }
717
718                 /* check for abandon */
719                 if ( op->o_abandon || doabandon ) {
720                         for ( i = 0; i < mi->mi_ntargets; i++ ) {
721                                 metasingleconn_t        *msc = &mc->mc_conns[ i ];
722
723                                 if ( candidates[ i ].sr_msgid != -1 ) {
724                                         ldap_abandon_ext( msc->msc_ld,
725                                                 candidates[ i ].sr_msgid,
726                                                 NULL, NULL );
727                                         candidates[ i ].sr_msgid = -1;
728                                 }
729                         }
730
731                         if ( op->o_abandon ) {
732                                 rc = SLAPD_ABANDON;
733                                 goto finish;
734                         }
735                 }
736
737                 if ( gotit == 0 ) {
738                         LDAP_BACK_TV_SET( &tv );
739                         ldap_pvt_thread_yield();
740
741                 } else {
742                         tv.tv_sec = 0;
743                         tv.tv_usec = 0;
744                 }
745         }
746
747         if ( rc == -1 ) {
748                 /*
749                  * FIXME: need a better strategy to handle errors
750                  */
751                 rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE );
752                 goto finish;
753         }
754
755         /*
756          * Rewrite the matched portion of the search base, if required
757          * 
758          * FIXME: only the last one gets caught!
759          */
760         savepriv = op->o_private;
761         op->o_private = (void *)(long)mi->mi_ntargets;
762         if ( candidate_match > 0 ) {
763                 struct berval   pmatched = BER_BVNULL;
764
765                 /* we use the first one */
766                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
767                         if ( candidates[ i ].sr_tag == META_CANDIDATE
768                                         && candidates[ i ].sr_matched != NULL )
769                         {
770                                 struct berval   bv, pbv;
771                                 int             rc;
772
773                                 /* if we got success, and this target
774                                  * returned noSuchObject, and its suffix
775                                  * is a superior of the searchBase,
776                                  * ignore the matchedDN */
777                                 if ( sres == LDAP_SUCCESS
778                                         && candidates[ i ].sr_err == LDAP_NO_SUCH_OBJECT
779                                         && op->o_req_ndn.bv_len > mi->mi_targets[ i ].mt_nsuffix.bv_len )
780                                 {
781                                         free( (char *)candidates[ i ].sr_matched );
782                                         candidates[ i ].sr_matched = NULL;
783                                         continue;
784                                 }
785
786                                 ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
787                                 rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
788
789                                 if ( rc == LDAP_SUCCESS ) {
790
791                                         /* NOTE: if they all are superiors
792                                          * of the baseDN, the shorter is also 
793                                          * superior of the longer... */
794                                         if ( pbv.bv_len > pmatched.bv_len ) {
795                                                 if ( !BER_BVISNULL( &pmatched ) ) {
796                                                         op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
797                                                 }
798                                                 pmatched = pbv;
799                                                 op->o_private = (void *)i;
800
801                                         } else {
802                                                 op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
803                                         }
804                                 }
805
806                                 if ( candidates[ i ].sr_matched != NULL ) {
807                                         free( (char *)candidates[ i ].sr_matched );
808                                         candidates[ i ].sr_matched = NULL;
809                                 }
810                         }
811                 }
812
813                 if ( !BER_BVISNULL( &pmatched ) ) {
814                         matched = pmatched.bv_val;
815                 }
816
817         } else if ( sres == LDAP_NO_SUCH_OBJECT ) {
818                 matched = op->o_bd->be_suffix[ 0 ].bv_val;
819         }
820
821 #if 0
822         {
823                 char    buf[BUFSIZ];
824                 char    cnd[BUFSIZ];
825                 int     i;
826
827                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
828                         if ( candidates[ i ].sr_tag == META_CANDIDATE ) {
829                                 cnd[ i ] = '*';
830                         } else {
831                                 cnd[ i ] = ' ';
832                         }
833                 }
834                 cnd[ i ] = '\0';
835
836                 snprintf( buf, sizeof( buf ), "%s meta_back_search: is_scope=%d is_ok=%d cnd=\"%s\"\n",
837                         op->o_log_prefix, initial_candidates, is_ok, cnd );
838
839                 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
840         }
841 #endif
842
843         /*
844          * In case we returned at least one entry, we return LDAP_SUCCESS
845          * otherwise, the latter error code we got
846          *
847          * FIXME: we should handle error codes and return the more 
848          * important/reasonable
849          */
850
851         if ( sres == LDAP_SUCCESS && rs->sr_v2ref ) {
852                 sres = LDAP_REFERRAL;
853         }
854         rs->sr_err = sres;
855         rs->sr_matched = matched;
856         rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
857         send_ldap_result( op, rs );
858         op->o_private = savepriv;
859         rs->sr_matched = NULL;
860         rs->sr_ref = NULL;
861
862 finish:;
863         if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
864                 op->o_tmpfree( matched, op->o_tmpmemctx );
865         }
866
867         if ( rs->sr_v2ref ) {
868                 ber_bvarray_free( rs->sr_v2ref );
869         }
870
871         for ( i = 0; i < mi->mi_ntargets; i++ ) {
872                 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
873                         continue;
874                 }
875
876                 if ( candidates[ i ].sr_matched ) {
877                         free( (char *)candidates[ i ].sr_matched );
878                         candidates[ i ].sr_matched = NULL;
879                 }
880
881                 if ( candidates[ i ].sr_text ) {
882                         ldap_memfree( (char *)candidates[ i ].sr_text );
883                         candidates[ i ].sr_text = NULL;
884                 }
885
886                 if ( candidates[ i ].sr_ref ) {
887                         ber_bvarray_free( candidates[ i ].sr_ref );
888                         candidates[ i ].sr_ref = NULL;
889                 }
890
891                 if ( candidates[ i ].sr_ctrls ) {
892                         ldap_controls_free( candidates[ i ].sr_ctrls );
893                         candidates[ i ].sr_ctrls = NULL;
894                 }
895         }
896
897         if ( mc ) {
898                 meta_back_release_conn( op, mc );
899         }
900
901         return rs->sr_err;
902 }
903
904 static int
905 meta_send_entry(
906         Operation       *op,
907         SlapReply       *rs,
908         metaconn_t      *mc,
909         int             target,
910         LDAPMessage     *e )
911 {
912         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
913         struct berval           a, mapped;
914         Entry                   ent = { 0 };
915         BerElement              ber = *e->lm_ber;
916         Attribute               *attr, **attrp;
917         struct berval           bdn,
918                                 dn = BER_BVNULL;
919         const char              *text;
920         dncookie                dc;
921         int                     rc;
922
923         if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
924                 return LDAP_DECODING_ERROR;
925         }
926
927         /*
928          * Rewrite the dn of the result, if needed
929          */
930         dc.target = &mi->mi_targets[ target ];
931         dc.conn = op->o_conn;
932         dc.rs = rs;
933         dc.ctx = "searchResult";
934
935         rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
936         if ( rs->sr_err != LDAP_SUCCESS) {
937                 return rs->sr_err;
938         }
939
940         /*
941          * Note: this may fail if the target host(s) schema differs
942          * from the one known to the meta, and a DN with unknown
943          * attributes is returned.
944          * 
945          * FIXME: should we log anything, or delegate to dnNormalize?
946          */
947         rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
948                 op->o_tmpmemctx );
949         if ( dn.bv_val != bdn.bv_val ) {
950                 free( dn.bv_val );
951         }
952         BER_BVZERO( &dn );
953
954         if ( rc != LDAP_SUCCESS ) {
955                 return LDAP_INVALID_DN_SYNTAX;
956         }
957
958         /*
959          * cache dn
960          */
961         if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
962                 ( void )meta_dncache_update_entry( &mi->mi_cache,
963                                 &ent.e_nname, target );
964         }
965
966         attrp = &ent.e_attrs;
967
968         dc.ctx = "searchAttrDN";
969         while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
970                 int                             last = 0;
971                 slap_syntax_validate_func       *validate;
972                 slap_syntax_transform_func      *pretty;
973
974                 ldap_back_map( &mi->mi_targets[ target ].mt_rwmap.rwm_at, 
975                                 &a, &mapped, BACKLDAP_REMAP );
976                 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
977                         ( void )ber_scanf( &ber, "x" /* [W] */ );
978                         continue;
979                 }
980                 attr = ( Attribute * )ch_calloc( 1, sizeof( Attribute ) );
981                 if ( attr == NULL ) {
982                         continue;
983                 }
984                 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
985                                 != LDAP_SUCCESS) {
986                         if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
987                                 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
988                         {
989                                 char    buf[ SLAP_TEXT_BUFLEN ];
990
991                                 snprintf( buf, sizeof( buf ),
992                                         "%s meta_send_entry(\"%s\"): "
993                                         "slap_bv2undef_ad(%s): %s\n",
994                                         op->o_log_prefix, ent.e_name.bv_val,
995                                         mapped.bv_val, text );
996
997                                 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
998                                 ch_free( attr );
999                                 continue;
1000                         }
1001                 }
1002
1003                 /* no subschemaSubentry */
1004                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
1005                         || attr->a_desc == slap_schema.si_ad_entryDN )
1006                 {
1007
1008                         /* 
1009                          * We eat target's subschemaSubentry because
1010                          * a search for this value is likely not
1011                          * to resolve to the appropriate backend;
1012                          * later, the local subschemaSubentry is
1013                          * added.
1014                          *
1015                          * We also eat entryDN because the frontend
1016                          * will reattach it without checking if already
1017                          * present...
1018                          */
1019                         ( void )ber_scanf( &ber, "x" /* [W] */ );
1020
1021                         ch_free(attr);
1022                         continue;
1023                 }
1024
1025                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR 
1026                                 || attr->a_vals == NULL )
1027                 {
1028                         attr->a_vals = (struct berval *)&slap_dummy_bv;
1029
1030                 } else {
1031                         for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
1032                                 ;
1033                 }
1034
1035                 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
1036                 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
1037
1038                 if ( !validate && !pretty ) {
1039                         attr_free( attr );
1040                         goto next_attr;
1041                 }
1042
1043                 if ( attr->a_desc == slap_schema.si_ad_objectClass
1044                                 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
1045                 {
1046                         struct berval   *bv;
1047
1048                         for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1049                                 ldap_back_map( &mi->mi_targets[ target ].mt_rwmap.rwm_oc,
1050                                                 bv, &mapped, BACKLDAP_REMAP );
1051                                 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
1052                                         free( bv->bv_val );
1053                                         BER_BVZERO( bv );
1054                                         if ( --last < 0 ) {
1055                                                 break;
1056                                         }
1057                                         *bv = attr->a_vals[ last ];
1058                                         BER_BVZERO( &attr->a_vals[ last ] );
1059                                         bv--;
1060
1061                                 } else if ( mapped.bv_val != bv->bv_val ) {
1062                                         free( bv->bv_val );
1063                                         ber_dupbv( bv, &mapped );
1064                                 }
1065                         }
1066                 /*
1067                  * It is necessary to try to rewrite attributes with
1068                  * dn syntax because they might be used in ACLs as
1069                  * members of groups; since ACLs are applied to the
1070                  * rewritten stuff, no dn-based subecj clause could
1071                  * be used at the ldap backend side (see
1072                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
1073                  * The problem can be overcome by moving the dn-based
1074                  * ACLs to the target directory server, and letting
1075                  * everything pass thru the ldap backend.
1076                  */
1077                 } else {
1078                         int     i;
1079
1080                         if ( attr->a_desc->ad_type->sat_syntax ==
1081                                 slap_schema.si_syn_distinguishedName )
1082                         {
1083                                 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
1084
1085                         } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
1086                                 ldap_back_referral_result_rewrite( &dc, attr->a_vals );
1087
1088                         }
1089
1090                         for ( i = 0; i < last; i++ ) {
1091                                 struct berval   pval;
1092                                 int             rc;
1093
1094                                 if ( pretty ) {
1095                                         rc = pretty( attr->a_desc->ad_type->sat_syntax,
1096                                                 &attr->a_vals[i], &pval, NULL );
1097
1098                                 } else {
1099                                         rc = validate( attr->a_desc->ad_type->sat_syntax,
1100                                                 &attr->a_vals[i] );
1101                                 }
1102
1103                                 if ( rc ) {
1104                                         LBER_FREE( attr->a_vals[i].bv_val );
1105                                         if ( --last == i ) {
1106                                                 BER_BVZERO( &attr->a_vals[ i ] );
1107                                                 break;
1108                                         }
1109                                         attr->a_vals[i] = attr->a_vals[last];
1110                                         BER_BVZERO( &attr->a_vals[last] );
1111                                         i--;
1112                                         continue;
1113                                 }
1114
1115                                 if ( pretty ) {
1116                                         LBER_FREE( attr->a_vals[i].bv_val );
1117                                         attr->a_vals[i] = pval;
1118                                 }
1119                         }
1120
1121                         if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
1122                                 attr_free( attr );
1123                                 goto next_attr;
1124                         }
1125                 }
1126
1127                 if ( last && attr->a_desc->ad_type->sat_equality &&
1128                         attr->a_desc->ad_type->sat_equality->smr_normalize )
1129                 {
1130                         int i;
1131
1132                         attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
1133                         for ( i = 0; i<last; i++ ) {
1134                                 attr->a_desc->ad_type->sat_equality->smr_normalize(
1135                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1136                                         attr->a_desc->ad_type->sat_syntax,
1137                                         attr->a_desc->ad_type->sat_equality,
1138                                         &attr->a_vals[i], &attr->a_nvals[i],
1139                                         NULL );
1140                         }
1141                         BER_BVZERO( &attr->a_nvals[i] );
1142
1143                 } else {
1144                         attr->a_nvals = attr->a_vals;
1145                 }
1146
1147                 *attrp = attr;
1148                 attrp = &attr->a_next;
1149 next_attr:;
1150         }
1151         rs->sr_entry = &ent;
1152         rs->sr_attrs = op->ors_attrs;
1153         rs->sr_flags = 0;
1154         rc = send_search_entry( op, rs );
1155         switch ( rc ) {
1156         case LDAP_UNAVAILABLE:
1157                 rc = LDAP_OTHER;
1158                 break;
1159         }
1160         rs->sr_entry = NULL;
1161         rs->sr_attrs = NULL;
1162         
1163         if ( !BER_BVISNULL( &ent.e_name ) ) {
1164                 free( ent.e_name.bv_val );
1165                 BER_BVZERO( &ent.e_name );
1166         }
1167         if ( !BER_BVISNULL( &ent.e_nname ) ) {
1168                 free( ent.e_nname.bv_val );
1169                 BER_BVZERO( &ent.e_nname );
1170         }
1171         entry_clean( &ent );
1172
1173         return rc;
1174 }
1175