]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/search.c
honor "chase-referrals no" (ITS#4447)
[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
432                                 /*
433                                  * When no candidates are left,
434                                  * the outer cycle finishes
435                                  */
436                                 candidates[ i ].sr_msgid = -1;
437                                 --ncandidates;
438                                 rs->sr_err = candidates[ i ].sr_err = LDAP_OTHER;
439                                 rs->sr_text = "remote server unavailable";
440
441                         } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
442                                 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
443                                         /* don't retry any more... */
444                                         candidates[ i ].sr_type = REP_RESULT;
445                                 }
446
447                                 is_ok++;
448
449                                 e = ldap_first_entry( msc->msc_ld, res );
450                                 savepriv = op->o_private;
451                                 op->o_private = (void *)i;
452                                 rs->sr_err = meta_send_entry( op, rs, mc, i, e );
453                                 ldap_msgfree( res );
454                                 res = NULL;
455
456                                 switch ( rs->sr_err ) {
457                                 case LDAP_SIZELIMIT_EXCEEDED:
458                                         savepriv = op->o_private;
459                                         op->o_private = (void *)i;
460                                         send_ldap_result( op, rs );
461                                         op->o_private = savepriv;
462                                         rs->sr_err = LDAP_SUCCESS;
463                                         goto finish;
464
465                                 case LDAP_UNAVAILABLE:
466                                         rs->sr_err = LDAP_OTHER;
467                                         goto finish;
468                                 }
469                                 op->o_private = savepriv;
470
471                                 gotit = 1;
472
473 #if 0
474                                 /*
475                                  * If scope is BASE, we need to jump out
476                                  * as soon as one entry is found; if
477                                  * the target pool is properly crafted,
478                                  * this should correspond to the sole
479                                  * entry that has the base DN
480                                  */
481                                 /* FIXME: this defeats the purpose of
482                                  * doing a search with scope == base and
483                                  * sizelimit = 1 to determine if a
484                                  * candidate is actually unique */
485                                 if ( op->ors_scope == LDAP_SCOPE_BASE
486                                                 && rs->sr_nentries > 0 )
487                                 {
488                                         doabandon = 1;
489                                         ncandidates = 0;
490                                         sres = LDAP_SUCCESS;
491                                         break;
492                                 }
493 #endif
494
495                         } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
496                                 char            **references = NULL;
497                                 int             cnt;
498
499                                 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
500                                         /* don't retry any more... */
501                                         candidates[ i ].sr_type = REP_RESULT;
502                                 }
503
504                                 is_ok++;
505
506                                 rc = ldap_parse_reference( msc->msc_ld, res,
507                                                 &references, &rs->sr_ctrls, 1 );
508                                 res = NULL;
509
510                                 if ( rc != LDAP_SUCCESS ) {
511                                         continue;
512                                 }
513
514                                 if ( references == NULL ) {
515                                         continue;
516                                 }
517
518 #ifdef ENABLE_REWRITE
519                                 dc.ctx = "referralDN";
520 #else /* ! ENABLE_REWRITE */
521                                 dc.tofrom = 0;
522                                 dc.normalized = 0;
523 #endif /* ! ENABLE_REWRITE */
524
525                                 /* FIXME: merge all and return at the end */
526
527                                 for ( cnt = 0; references[ cnt ]; cnt++ )
528                                         ;
529
530                                 rs->sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
531
532                                 for ( cnt = 0; references[ cnt ]; cnt++ ) {
533                                         ber_str2bv( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ] );
534                                 }
535                                 BER_BVZERO( &rs->sr_ref[ cnt ] );
536
537                                 ( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref );
538
539                                 if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
540                                         /* ignore return value by now */
541                                         savepriv = op->o_private;
542                                         op->o_private = (void *)i;
543                                         ( void )send_search_reference( op, rs );
544                                         op->o_private = savepriv;
545
546                                         ber_bvarray_free( rs->sr_ref );
547                                         rs->sr_ref = NULL;
548                                 }
549
550                                 /* cleanup */
551                                 if ( references ) {
552                                         ber_memvfree( (void **)references );
553                                 }
554
555                                 if ( rs->sr_ctrls ) {
556                                         ldap_controls_free( rs->sr_ctrls );
557                                         rs->sr_ctrls = NULL;
558                                 }
559
560                         } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
561                                 char            buf[ SLAP_TEXT_BUFLEN ];
562                                 char            **references = NULL;
563
564                                 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
565                                         /* don't retry any more... */
566                                         candidates[ i ].sr_type = REP_RESULT;
567                                 }
568
569                                 /* NOTE: ignores response controls
570                                  * (and intermediate response controls
571                                  * as well, except for those with search
572                                  * references); this may not be correct,
573                                  * but if they're not ignored then
574                                  * back-meta would need to merge them
575                                  * consistently (think of pagedResults...)
576                                  */
577                                 if ( ldap_parse_result( msc->msc_ld,
578                                                         res,
579                                                         &candidates[ i ].sr_err,
580                                                         (char **)&candidates[ i ].sr_matched,
581                                                         NULL /* (char **)&candidates[ i ].sr_text */ ,
582                                                         &references,
583                                                         NULL /* &candidates[ i ].sr_ctrls (unused) */ ,
584                                                         1 ) != LDAP_SUCCESS )
585                                 {
586                                         res = NULL;
587                                         ldap_get_option( msc->msc_ld,
588                                                         LDAP_OPT_ERROR_NUMBER,
589                                                         &rs->sr_err );
590                                         sres = slap_map_api2result( rs );
591                                         candidates[ i ].sr_type = REP_RESULT;
592                                         goto really_bad;
593                                 }
594
595                                 rs->sr_err = candidates[ i ].sr_err;
596                                 sres = slap_map_api2result( rs );
597                                 res = NULL;
598
599                                 /* massage matchedDN if need be */
600                                 if ( candidates[ i ].sr_matched != NULL ) {
601                                         {
602                                                 struct berval   match, mmatch;
603
604                                                 ber_str2bv( candidates[ i ].sr_matched,
605                                                         0, 0, &match );
606                                                 candidates[ i ].sr_matched = NULL;
607
608                                                 dc.ctx = "matchedDN";
609                                                 dc.target = &mi->mi_targets[ i ];
610                                                 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
611                                                         if ( mmatch.bv_val == match.bv_val ) {
612                                                                 candidates[ i ].sr_matched = ch_strdup( mmatch.bv_val );
613
614                                                         } else {
615                                                                 candidates[ i ].sr_matched = mmatch.bv_val;
616                                                         }
617
618                                                         candidate_match++;
619                                                 } 
620                                                 ldap_memfree( match.bv_val );
621                                         }
622                                 }
623
624
625                                 /* add references to array */
626                                 if ( references ) {
627                                         BerVarray       sr_ref;
628                                         int             cnt;
629
630                                         for ( cnt = 0; references[ cnt ]; cnt++ )
631                                                 ;
632
633                                         sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
634
635                                         for ( cnt = 0; references[ cnt ]; cnt++ ) {
636                                                 ber_str2bv( references[ cnt ], 0, 1, &sr_ref[ cnt ] );
637                                         }
638                                         BER_BVZERO( &sr_ref[ cnt ] );
639
640                                         ( void )ldap_back_referral_result_rewrite( &dc, sr_ref );
641                                 
642                                         /* cleanup */
643                                         ber_memvfree( (void **)references );
644
645                                         if ( rs->sr_v2ref == NULL ) {
646                                                 rs->sr_v2ref = sr_ref;
647
648                                         } else {
649                                                 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
650                                                         ber_bvarray_add( &rs->sr_v2ref, &sr_ref[ cnt ] );
651                                                 }
652                                                 ber_memfree( sr_ref );
653                                         }
654                                 }
655
656                                 rs->sr_err = candidates[ i ].sr_err;
657                                 sres = slap_map_api2result( rs );
658
659                                 snprintf( buf, sizeof( buf ),
660                                         "%s meta_back_search[%ld] "
661                                         "match=\"%s\" err=%ld\n",
662                                         op->o_log_prefix, i,
663                                         candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
664                                         (long) candidates[ i ].sr_err );
665                                 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
666
667                                 switch ( sres ) {
668                                 case LDAP_NO_SUCH_OBJECT:
669                                         /* is_ok is touched any time a valid
670                                          * (even intermediate) result is
671                                          * returned; as a consequence, if
672                                          * a candidate returns noSuchObject
673                                          * it is ignored and the candidate
674                                          * is simply demoted. */
675                                         if ( is_ok ) {
676                                                 sres = LDAP_SUCCESS;
677                                         }
678                                         break;
679
680                                 case LDAP_SUCCESS:
681                                 case LDAP_REFERRAL:
682                                         is_ok++;
683                                         break;
684
685                                 default:
686                                         if ( META_BACK_ONERR_STOP( mi ) ) {
687                                                 savepriv = op->o_private;
688                                                 op->o_private = (void *)i;
689                                                 send_ldap_result( op, rs );
690                                                 op->o_private = savepriv;
691                                                 goto finish;
692                                         }
693                                         break;
694                                 }
695
696                                 last = i;
697                                 rc = 0;
698
699                                 /*
700                                  * When no candidates are left,
701                                  * the outer cycle finishes
702                                  */
703                                 candidates[ i ].sr_msgid = -1;
704                                 --ncandidates;
705
706                         } else {
707                                 assert( 0 );
708                                 goto really_bad;
709                         }
710                 }
711
712                 /* check for abandon */
713                 if ( op->o_abandon || doabandon ) {
714                         for ( i = 0; i < mi->mi_ntargets; i++ ) {
715                                 metasingleconn_t        *msc = &mc->mc_conns[ i ];
716
717                                 if ( candidates[ i ].sr_msgid != -1 ) {
718                                         ldap_abandon_ext( msc->msc_ld,
719                                                 candidates[ i ].sr_msgid,
720                                                 NULL, NULL );
721                                         candidates[ i ].sr_msgid = -1;
722                                 }
723                         }
724
725                         if ( op->o_abandon ) {
726                                 rc = SLAPD_ABANDON;
727                                 goto finish;
728                         }
729                 }
730
731                 if ( gotit == 0 ) {
732                         LDAP_BACK_TV_SET( &tv );
733                         ldap_pvt_thread_yield();
734
735                 } else {
736                         tv.tv_sec = 0;
737                         tv.tv_usec = 0;
738                 }
739         }
740
741         if ( rc == -1 ) {
742                 /*
743                  * FIXME: need a better strategy to handle errors
744                  */
745                 rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE );
746                 goto finish;
747         }
748
749         /*
750          * Rewrite the matched portion of the search base, if required
751          * 
752          * FIXME: only the last one gets caught!
753          */
754         savepriv = op->o_private;
755         op->o_private = (void *)(long)mi->mi_ntargets;
756         if ( candidate_match > 0 ) {
757                 struct berval   pmatched = BER_BVNULL;
758
759                 /* we use the first one */
760                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
761                         if ( candidates[ i ].sr_tag == META_CANDIDATE
762                                         && candidates[ i ].sr_matched != NULL )
763                         {
764                                 struct berval   bv, pbv;
765                                 int             rc;
766
767                                 /* if we got success, and this target
768                                  * returned noSuchObject, and its suffix
769                                  * is a superior of the searchBase,
770                                  * ignore the matchedDN */
771                                 if ( sres == LDAP_SUCCESS
772                                         && candidates[ i ].sr_err == LDAP_NO_SUCH_OBJECT
773                                         && op->o_req_ndn.bv_len > mi->mi_targets[ i ].mt_nsuffix.bv_len )
774                                 {
775                                         free( (char *)candidates[ i ].sr_matched );
776                                         candidates[ i ].sr_matched = NULL;
777                                         continue;
778                                 }
779
780                                 ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
781                                 rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
782
783                                 if ( rc == LDAP_SUCCESS ) {
784
785                                         /* NOTE: if they all are superiors
786                                          * of the baseDN, the shorter is also 
787                                          * superior of the longer... */
788                                         if ( pbv.bv_len > pmatched.bv_len ) {
789                                                 if ( !BER_BVISNULL( &pmatched ) ) {
790                                                         op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
791                                                 }
792                                                 pmatched = pbv;
793                                                 op->o_private = (void *)i;
794
795                                         } else {
796                                                 op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
797                                         }
798                                 }
799
800                                 if ( candidates[ i ].sr_matched != NULL ) {
801                                         free( (char *)candidates[ i ].sr_matched );
802                                         candidates[ i ].sr_matched = NULL;
803                                 }
804                         }
805                 }
806
807                 if ( !BER_BVISNULL( &pmatched ) ) {
808                         matched = pmatched.bv_val;
809                 }
810
811         } else if ( sres == LDAP_NO_SUCH_OBJECT ) {
812                 matched = op->o_bd->be_suffix[ 0 ].bv_val;
813         }
814
815 #if 0
816         {
817                 char    buf[BUFSIZ];
818                 char    cnd[BUFSIZ];
819                 int     i;
820
821                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
822                         if ( candidates[ i ].sr_tag == META_CANDIDATE ) {
823                                 cnd[ i ] = '*';
824                         } else {
825                                 cnd[ i ] = ' ';
826                         }
827                 }
828                 cnd[ i ] = '\0';
829
830                 snprintf( buf, sizeof( buf ), "%s meta_back_search: is_scope=%d is_ok=%d cnd=\"%s\"\n",
831                         op->o_log_prefix, initial_candidates, is_ok, cnd );
832
833                 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
834         }
835 #endif
836
837         /*
838          * In case we returned at least one entry, we return LDAP_SUCCESS
839          * otherwise, the latter error code we got
840          *
841          * FIXME: we should handle error codes and return the more 
842          * important/reasonable
843          */
844
845         if ( sres == LDAP_SUCCESS && rs->sr_v2ref ) {
846                 sres = LDAP_REFERRAL;
847         }
848         rs->sr_err = sres;
849         rs->sr_matched = matched;
850         rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
851         send_ldap_result( op, rs );
852         op->o_private = savepriv;
853         rs->sr_matched = NULL;
854         rs->sr_ref = NULL;
855
856 finish:;
857         if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
858                 op->o_tmpfree( matched, op->o_tmpmemctx );
859         }
860
861         if ( rs->sr_v2ref ) {
862                 ber_bvarray_free( rs->sr_v2ref );
863         }
864
865         for ( i = 0; i < mi->mi_ntargets; i++ ) {
866                 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
867                         continue;
868                 }
869
870                 if ( candidates[ i ].sr_matched ) {
871                         free( (char *)candidates[ i ].sr_matched );
872                         candidates[ i ].sr_matched = NULL;
873                 }
874
875                 if ( candidates[ i ].sr_text ) {
876                         ldap_memfree( (char *)candidates[ i ].sr_text );
877                         candidates[ i ].sr_text = NULL;
878                 }
879
880                 if ( candidates[ i ].sr_ref ) {
881                         ber_bvarray_free( candidates[ i ].sr_ref );
882                         candidates[ i ].sr_ref = NULL;
883                 }
884
885                 if ( candidates[ i ].sr_ctrls ) {
886                         ldap_controls_free( candidates[ i ].sr_ctrls );
887                         candidates[ i ].sr_ctrls = NULL;
888                 }
889         }
890
891         meta_back_release_conn( op, mc );
892
893         return rs->sr_err;
894 }
895
896 static int
897 meta_send_entry(
898         Operation       *op,
899         SlapReply       *rs,
900         metaconn_t      *mc,
901         int             target,
902         LDAPMessage     *e )
903 {
904         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
905         struct berval           a, mapped;
906         Entry                   ent = { 0 };
907         BerElement              ber = *e->lm_ber;
908         Attribute               *attr, **attrp;
909         struct berval           bdn,
910                                 dn = BER_BVNULL;
911         const char              *text;
912         dncookie                dc;
913         int                     rc;
914
915         if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
916                 return LDAP_DECODING_ERROR;
917         }
918
919         /*
920          * Rewrite the dn of the result, if needed
921          */
922         dc.target = &mi->mi_targets[ target ];
923         dc.conn = op->o_conn;
924         dc.rs = rs;
925         dc.ctx = "searchResult";
926
927         rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
928         if ( rs->sr_err != LDAP_SUCCESS) {
929                 return rs->sr_err;
930         }
931
932         /*
933          * Note: this may fail if the target host(s) schema differs
934          * from the one known to the meta, and a DN with unknown
935          * attributes is returned.
936          * 
937          * FIXME: should we log anything, or delegate to dnNormalize?
938          */
939         rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
940                 op->o_tmpmemctx );
941         if ( dn.bv_val != bdn.bv_val ) {
942                 free( dn.bv_val );
943         }
944         BER_BVZERO( &dn );
945
946         if ( rc != LDAP_SUCCESS ) {
947                 return LDAP_INVALID_DN_SYNTAX;
948         }
949
950         /*
951          * cache dn
952          */
953         if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
954                 ( void )meta_dncache_update_entry( &mi->mi_cache,
955                                 &ent.e_nname, target );
956         }
957
958         attrp = &ent.e_attrs;
959
960         dc.ctx = "searchAttrDN";
961         while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
962                 int                             last = 0;
963                 slap_syntax_validate_func       *validate;
964                 slap_syntax_transform_func      *pretty;
965
966                 ldap_back_map( &mi->mi_targets[ target ].mt_rwmap.rwm_at, 
967                                 &a, &mapped, BACKLDAP_REMAP );
968                 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
969                         ( void )ber_scanf( &ber, "x" /* [W] */ );
970                         continue;
971                 }
972                 attr = ( Attribute * )ch_calloc( 1, sizeof( Attribute ) );
973                 if ( attr == NULL ) {
974                         continue;
975                 }
976                 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
977                                 != LDAP_SUCCESS) {
978                         if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
979                                 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
980                         {
981                                 char    buf[ SLAP_TEXT_BUFLEN ];
982
983                                 snprintf( buf, sizeof( buf ),
984                                         "%s meta_send_entry(\"%s\"): "
985                                         "slap_bv2undef_ad(%s): %s\n",
986                                         op->o_log_prefix, ent.e_name.bv_val,
987                                         mapped.bv_val, text );
988
989                                 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
990                                 ch_free( attr );
991                                 continue;
992                         }
993                 }
994
995                 /* no subschemaSubentry */
996                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
997                         || attr->a_desc == slap_schema.si_ad_entryDN )
998                 {
999
1000                         /* 
1001                          * We eat target's subschemaSubentry because
1002                          * a search for this value is likely not
1003                          * to resolve to the appropriate backend;
1004                          * later, the local subschemaSubentry is
1005                          * added.
1006                          *
1007                          * We also eat entryDN because the frontend
1008                          * will reattach it without checking if already
1009                          * present...
1010                          */
1011                         ( void )ber_scanf( &ber, "x" /* [W] */ );
1012
1013                         ch_free(attr);
1014                         continue;
1015                 }
1016
1017                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR 
1018                                 || attr->a_vals == NULL )
1019                 {
1020                         attr->a_vals = (struct berval *)&slap_dummy_bv;
1021
1022                 } else {
1023                         for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
1024                                 ;
1025                 }
1026
1027                 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
1028                 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
1029
1030                 if ( !validate && !pretty ) {
1031                         attr_free( attr );
1032                         goto next_attr;
1033                 }
1034
1035                 if ( attr->a_desc == slap_schema.si_ad_objectClass
1036                                 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
1037                 {
1038                         struct berval   *bv;
1039
1040                         for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1041                                 ldap_back_map( &mi->mi_targets[ target ].mt_rwmap.rwm_oc,
1042                                                 bv, &mapped, BACKLDAP_REMAP );
1043                                 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
1044                                         free( bv->bv_val );
1045                                         BER_BVZERO( bv );
1046                                         if ( --last < 0 ) {
1047                                                 break;
1048                                         }
1049                                         *bv = attr->a_vals[ last ];
1050                                         BER_BVZERO( &attr->a_vals[ last ] );
1051                                         bv--;
1052
1053                                 } else if ( mapped.bv_val != bv->bv_val ) {
1054                                         free( bv->bv_val );
1055                                         ber_dupbv( bv, &mapped );
1056                                 }
1057                         }
1058                 /*
1059                  * It is necessary to try to rewrite attributes with
1060                  * dn syntax because they might be used in ACLs as
1061                  * members of groups; since ACLs are applied to the
1062                  * rewritten stuff, no dn-based subecj clause could
1063                  * be used at the ldap backend side (see
1064                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
1065                  * The problem can be overcome by moving the dn-based
1066                  * ACLs to the target directory server, and letting
1067                  * everything pass thru the ldap backend.
1068                  */
1069                 } else {
1070                         int     i;
1071
1072                         if ( attr->a_desc->ad_type->sat_syntax ==
1073                                 slap_schema.si_syn_distinguishedName )
1074                         {
1075                                 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
1076
1077                         } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
1078                                 ldap_back_referral_result_rewrite( &dc, attr->a_vals );
1079
1080                         }
1081
1082                         for ( i = 0; i < last; i++ ) {
1083                                 struct berval   pval;
1084                                 int             rc;
1085
1086                                 if ( pretty ) {
1087                                         rc = pretty( attr->a_desc->ad_type->sat_syntax,
1088                                                 &attr->a_vals[i], &pval, NULL );
1089
1090                                 } else {
1091                                         rc = validate( attr->a_desc->ad_type->sat_syntax,
1092                                                 &attr->a_vals[i] );
1093                                 }
1094
1095                                 if ( rc ) {
1096                                         LBER_FREE( attr->a_vals[i].bv_val );
1097                                         if ( --last == i ) {
1098                                                 BER_BVZERO( &attr->a_vals[ i ] );
1099                                                 break;
1100                                         }
1101                                         attr->a_vals[i] = attr->a_vals[last];
1102                                         BER_BVZERO( &attr->a_vals[last] );
1103                                         i--;
1104                                         continue;
1105                                 }
1106
1107                                 if ( pretty ) {
1108                                         LBER_FREE( attr->a_vals[i].bv_val );
1109                                         attr->a_vals[i] = pval;
1110                                 }
1111                         }
1112
1113                         if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
1114                                 attr_free( attr );
1115                                 goto next_attr;
1116                         }
1117                 }
1118
1119                 if ( last && attr->a_desc->ad_type->sat_equality &&
1120                         attr->a_desc->ad_type->sat_equality->smr_normalize )
1121                 {
1122                         int i;
1123
1124                         attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
1125                         for ( i = 0; i<last; i++ ) {
1126                                 attr->a_desc->ad_type->sat_equality->smr_normalize(
1127                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1128                                         attr->a_desc->ad_type->sat_syntax,
1129                                         attr->a_desc->ad_type->sat_equality,
1130                                         &attr->a_vals[i], &attr->a_nvals[i],
1131                                         NULL );
1132                         }
1133                         BER_BVZERO( &attr->a_nvals[i] );
1134
1135                 } else {
1136                         attr->a_nvals = attr->a_vals;
1137                 }
1138
1139                 *attrp = attr;
1140                 attrp = &attr->a_next;
1141 next_attr:;
1142         }
1143         rs->sr_entry = &ent;
1144         rs->sr_attrs = op->ors_attrs;
1145         rs->sr_flags = 0;
1146         rc = send_search_entry( op, rs );
1147         switch ( rc ) {
1148         case LDAP_UNAVAILABLE:
1149                 rc = LDAP_OTHER;
1150                 break;
1151         }
1152         rs->sr_entry = NULL;
1153         rs->sr_attrs = NULL;
1154         
1155         if ( !BER_BVISNULL( &ent.e_name ) ) {
1156                 free( ent.e_name.bv_val );
1157                 BER_BVZERO( &ent.e_name );
1158         }
1159         if ( !BER_BVISNULL( &ent.e_nname ) ) {
1160                 free( ent.e_nname.bv_val );
1161                 BER_BVZERO( &ent.e_nname );
1162         }
1163         entry_clean( &ent );
1164
1165         return rc;
1166 }
1167