]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/search.c
don't risk leaving around client library error codes; try to return additional inform...
[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-2010 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 "lutil.h"
32 #include "slap.h"
33 #include "../back-ldap/back-ldap.h"
34 #include "back-meta.h"
35 #undef ldap_debug       /* silence a warning in ldap-int.h */
36 #include "ldap_log.h"
37 #include "../../../libraries/libldap/ldap-int.h"
38
39 /* IGNORE means that target does not (no longer) participate
40  * in the search;
41  * NOTREADY means the search on that target has not been initialized yet
42  */
43 #define META_MSGID_IGNORE       (-1)
44 #define META_MSGID_NEED_BIND    (-2)
45 #define META_MSGID_CONNECTING   (-3)
46
47 static int
48 meta_send_entry(
49         Operation       *op,
50         SlapReply       *rs,
51         metaconn_t      *mc,
52         int             i,
53         LDAPMessage     *e );
54
55 typedef enum meta_search_candidate_t {
56         META_SEARCH_UNDEFINED = -2,
57         META_SEARCH_ERR = -1,
58         META_SEARCH_NOT_CANDIDATE,
59         META_SEARCH_CANDIDATE,
60         META_SEARCH_BINDING,
61         META_SEARCH_NEED_BIND,
62         META_SEARCH_CONNECTING
63 } meta_search_candidate_t;
64
65 /*
66  * meta_search_dobind_init()
67  *
68  * initiates bind for a candidate target of a search.
69  */
70 static meta_search_candidate_t
71 meta_search_dobind_init(
72         Operation               *op,
73         SlapReply               *rs,
74         metaconn_t              **mcp,
75         int                     candidate,
76         SlapReply               *candidates )
77 {
78         metaconn_t              *mc = *mcp;
79         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
80         metatarget_t            *mt = mi->mi_targets[ candidate ];
81         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
82
83         struct berval           binddn = msc->msc_bound_ndn,
84                                 cred = msc->msc_cred;
85         int                     method;
86
87         int                     rc;
88
89         meta_search_candidate_t retcode;
90
91         Debug( LDAP_DEBUG_TRACE, "%s >>> meta_search_dobind_init[%d]\n",
92                 op->o_log_prefix, candidate, 0 );
93
94         /*
95          * all the targets are already bound as pseudoroot
96          */
97         if ( mc->mc_authz_target == META_BOUND_ALL ) {
98                 return META_SEARCH_CANDIDATE;
99         }
100
101         retcode = META_SEARCH_BINDING;
102         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
103         if ( LDAP_BACK_CONN_ISBOUND( msc ) || LDAP_BACK_CONN_ISANON( msc ) ) {
104                 /* already bound (or anonymous) */
105
106 #ifdef DEBUG_205
107                 char    buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
108                 int     bound = 0;
109
110                 if ( LDAP_BACK_CONN_ISBOUND( msc ) ) {
111                         bound = 1;
112                 }
113
114                 snprintf( buf, sizeof( buf ), " mc=%p ld=%p%s DN=\"%s\"",
115                         (void *)mc, (void *)msc->msc_ld,
116                         bound ? " bound" : " anonymous",
117                         bound == 0 ? "" : msc->msc_bound_ndn.bv_val );
118                 Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
119                         op->o_log_prefix, candidate, buf );
120 #endif /* DEBUG_205 */
121
122                 retcode = META_SEARCH_CANDIDATE;
123
124         } else if ( META_BACK_CONN_CREATING( msc ) || LDAP_BACK_CONN_BINDING( msc ) ) {
125                 /* another thread is binding the target for this conn; wait */
126
127 #ifdef DEBUG_205
128                 char    buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
129
130                 snprintf( buf, sizeof( buf ), " mc=%p ld=%p needbind",
131                         (void *)mc, (void *)msc->msc_ld );
132                 Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
133                         op->o_log_prefix, candidate, buf );
134 #endif /* DEBUG_205 */
135
136                 candidates[ candidate ].sr_msgid = META_MSGID_NEED_BIND;
137                 retcode = META_SEARCH_NEED_BIND;
138
139         } else {
140                 /* we'll need to bind the target for this conn */
141
142 #ifdef DEBUG_205
143                 char buf[ SLAP_TEXT_BUFLEN ];
144
145                 snprintf( buf, sizeof( buf ), " mc=%p ld=%p binding",
146                         (void *)mc, (void *)msc->msc_ld );
147                 Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
148                         op->o_log_prefix, candidate, buf );
149 #endif /* DEBUG_205 */
150
151                 if ( msc->msc_ld == NULL ) {
152                         /* for some reason (e.g. because formerly in "binding"
153                          * state, with eventual connection expiration or invalidation)
154                          * it was not initialized as expected */
155
156                         Debug( LDAP_DEBUG_ANY, "%s meta_search_dobind_init[%d] mc=%p ld=NULL\n",
157                                 op->o_log_prefix, candidate, (void *)mc );
158
159                         rc = meta_back_init_one_conn( op, rs, *mcp, candidate,
160                                 LDAP_BACK_CONN_ISPRIV( *mcp ), LDAP_BACK_DONTSEND, 0 );
161                         switch ( rc ) {
162                         case LDAP_SUCCESS:
163                                 assert( msc->msc_ld != NULL );
164                                 break;
165
166                         case LDAP_SERVER_DOWN:
167                         case LDAP_UNAVAILABLE:
168                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
169                                 goto down;
170         
171                         default:
172                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
173                                 goto other;
174                         }
175                 }
176
177                 LDAP_BACK_CONN_BINDING_SET( msc );
178         }
179
180         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
181
182         if ( retcode != META_SEARCH_BINDING ) {
183                 return retcode;
184         }
185
186         /* NOTE: this obsoletes pseudorootdn */
187         if ( op->o_conn != NULL &&
188                 !op->o_do_not_cache &&
189                 ( BER_BVISNULL( &msc->msc_bound_ndn ) ||
190                         BER_BVISEMPTY( &msc->msc_bound_ndn ) ||
191                         ( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
192         {
193                 rc = meta_back_proxy_authz_cred( mc, candidate, op, rs, LDAP_BACK_DONTSEND, &binddn, &cred, &method );
194                 if ( rc != LDAP_SUCCESS ) {
195                         goto down;
196                 }
197
198                 /* NOTE: we copy things here, even if bind didn't succeed yet,
199                  * because the connection is not shared until bind is over */
200                 if ( !BER_BVISNULL( &binddn ) ) {
201                         ber_bvreplace( &msc->msc_bound_ndn, &binddn );
202                         if ( META_BACK_TGT_SAVECRED( mt ) && !BER_BVISNULL( &cred ) ) {
203                                 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
204                                         memset( msc->msc_cred.bv_val, 0,
205                                                 msc->msc_cred.bv_len );
206                                 }
207                                 ber_bvreplace( &msc->msc_cred, &cred );
208                         }
209                 }
210
211                 if ( LDAP_BACK_CONN_ISBOUND( msc ) ) {
212                         /* apparently, idassert was configured with SASL bind,
213                          * so bind occurred inside meta_back_proxy_authz_cred() */
214                         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
215                         LDAP_BACK_CONN_BINDING_CLEAR( msc );
216                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
217                         return META_SEARCH_CANDIDATE;
218                 }
219
220                 /* paranoid */
221                 switch ( method ) {
222                 case LDAP_AUTH_NONE:
223                 case LDAP_AUTH_SIMPLE:
224                         /* do a simple bind with binddn, cred */
225                         break;
226
227                 default:
228                         assert( 0 );
229                         break;
230                 }
231         }
232
233         assert( msc->msc_ld != NULL );
234
235         if ( !BER_BVISEMPTY( &binddn ) && BER_BVISEMPTY( &cred ) ) {
236                 /* bind anonymously? */
237                 Debug( LDAP_DEBUG_ANY, "%s meta_search_dobind_init[%d] mc=%p: "
238                         "non-empty dn with empty cred; binding anonymously\n",
239                         op->o_log_prefix, candidate, (void *)mc );
240                 cred = slap_empty_bv;
241                 
242         } else if ( BER_BVISEMPTY( &binddn ) && !BER_BVISEMPTY( &cred ) ) {
243                 /* error */
244                 Debug( LDAP_DEBUG_ANY, "%s meta_search_dobind_init[%d] mc=%p: "
245                         "empty dn with non-empty cred: error\n",
246                         op->o_log_prefix, candidate, (void *)mc );
247                 goto other;
248         }
249
250         /* connect must be async only the first time... */
251         ldap_set_option( msc->msc_ld, LDAP_OPT_CONNECT_ASYNC, LDAP_OPT_ON );
252
253 retry:;
254         rc = ldap_sasl_bind( msc->msc_ld, binddn.bv_val, LDAP_SASL_SIMPLE, &cred,
255                         NULL, NULL, &candidates[ candidate ].sr_msgid );
256
257 #ifdef DEBUG_205
258         {
259                 char buf[ SLAP_TEXT_BUFLEN ];
260
261                 snprintf( buf, sizeof( buf ), "meta_search_dobind_init[%d] mc=%p ld=%p rc=%d",
262                         candidate, (void *)mc, (void *)mc->mc_conns[ candidate ].msc_ld, rc );
263                 Debug( LDAP_DEBUG_ANY, "### %s %s\n",
264                         op->o_log_prefix, buf, 0 );
265         }
266 #endif /* DEBUG_205 */
267
268         switch ( rc ) {
269         case LDAP_SUCCESS:
270                 assert( candidates[ candidate ].sr_msgid >= 0 );
271                 META_BINDING_SET( &candidates[ candidate ] );
272                 return META_SEARCH_BINDING;
273
274         case LDAP_X_CONNECTING:
275                 /* must retry, same conn */
276                 candidates[ candidate ].sr_msgid = META_MSGID_CONNECTING;
277                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
278                 LDAP_BACK_CONN_BINDING_CLEAR( msc );
279                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
280                 return META_SEARCH_CONNECTING;
281
282         case LDAP_SERVER_DOWN:
283 down:;
284                 /* This is the worst thing that could happen:
285                  * the search will wait until the retry is over. */
286                 if ( !META_IS_RETRYING( &candidates[ candidate ] ) ) {
287                         META_RETRYING_SET( &candidates[ candidate ] );
288
289                         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
290
291                         assert( mc->mc_refcnt > 0 );
292                         if ( LogTest( LDAP_DEBUG_ANY ) ) {
293                                 char    buf[ SLAP_TEXT_BUFLEN ];
294
295                                 /* this lock is required; however,
296                                  * it's invoked only when logging is on */
297                                 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
298                                 snprintf( buf, sizeof( buf ),
299                                         "retrying URI=\"%s\" DN=\"%s\"",
300                                         mt->mt_uri,
301                                         BER_BVISNULL( &msc->msc_bound_ndn ) ?
302                                                 "" : msc->msc_bound_ndn.bv_val );
303                                 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
304
305                                 Debug( LDAP_DEBUG_ANY,
306                                         "%s meta_search_dobind_init[%d]: %s.\n",
307                                         op->o_log_prefix, candidate, buf );
308                         }
309
310                         meta_clear_one_candidate( op, mc, candidate );
311                         LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
312
313                         ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
314
315                         /* mc here must be the regular mc, reset and ready for init */
316                         rc = meta_back_init_one_conn( op, rs, mc, candidate,
317                                 LDAP_BACK_CONN_ISPRIV( mc ), LDAP_BACK_DONTSEND, 0 );
318
319                         if ( rc == LDAP_SUCCESS ) {
320                                 LDAP_BACK_CONN_BINDING_SET( msc );
321                         }
322
323                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
324
325                         if ( rc == LDAP_SUCCESS ) {
326                                 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
327                                 goto retry;
328                         }
329                 }
330
331                 if ( *mcp == NULL ) {
332                         retcode = META_SEARCH_ERR;
333                         rs->sr_err = LDAP_UNAVAILABLE;
334                         candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
335                         break;
336                 }
337                 /* fall thru */
338
339         default:
340 other:;
341                 rs->sr_err = rc;
342                 rc = slap_map_api2result( rs );
343
344                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
345                 meta_clear_one_candidate( op, mc, candidate );
346                 candidates[ candidate ].sr_err = rc;
347                 if ( META_BACK_ONERR_STOP( mi ) ) {
348                         LDAP_BACK_CONN_TAINTED_SET( mc );
349                         meta_back_release_conn_lock( mi, mc, 0 );
350                         *mcp = NULL;
351                         rs->sr_err = rc;
352
353                         retcode = META_SEARCH_ERR;
354
355                 } else {
356                         retcode = META_SEARCH_NOT_CANDIDATE;
357                 }
358                 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
359                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
360                 break;
361         }
362
363         return retcode;
364 }
365
366 static meta_search_candidate_t
367 meta_search_dobind_result(
368         Operation               *op,
369         SlapReply               *rs,
370         metaconn_t              **mcp,
371         int                     candidate,
372         SlapReply               *candidates,
373         LDAPMessage             *res )
374 {
375         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
376         metaconn_t              *mc = *mcp;
377         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
378
379         meta_search_candidate_t retcode = META_SEARCH_NOT_CANDIDATE;
380         int                     rc;
381
382         assert( msc->msc_ld != NULL );
383
384         /* FIXME: matched? referrals? response controls? */
385         rc = ldap_parse_result( msc->msc_ld, res,
386                 &candidates[ candidate ].sr_err,
387                 NULL, NULL, NULL, NULL, 0 );
388         if ( rc != LDAP_SUCCESS ) {
389                 candidates[ candidate ].sr_err = rc;
390         }
391         rc = slap_map_api2result( &candidates[ candidate ] );
392
393         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
394         LDAP_BACK_CONN_BINDING_CLEAR( msc );
395         if ( rc != LDAP_SUCCESS ) {
396                 meta_clear_one_candidate( op, mc, candidate );
397                 candidates[ candidate ].sr_err = rc;
398                 if ( META_BACK_ONERR_STOP( mi ) ) {
399                         LDAP_BACK_CONN_TAINTED_SET( mc );
400                         meta_back_release_conn_lock( mi, mc, 0 );
401                         *mcp = NULL;
402                         retcode = META_SEARCH_ERR;
403                         rs->sr_err = rc;
404                 }
405
406         } else {
407                 /* FIXME: check if bound as idassert authcDN! */
408                 if ( BER_BVISNULL( &msc->msc_bound_ndn )
409                         || BER_BVISEMPTY( &msc->msc_bound_ndn ) )
410                 {
411                         LDAP_BACK_CONN_ISANON_SET( msc );
412
413                 } else {
414                         LDAP_BACK_CONN_ISBOUND_SET( msc );
415                 }
416                 retcode = META_SEARCH_CANDIDATE;
417
418                 /* connect must be async */
419                 ldap_set_option( msc->msc_ld, LDAP_OPT_CONNECT_ASYNC, LDAP_OPT_OFF );
420         }
421
422         candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
423         META_BINDING_CLEAR( &candidates[ candidate ] );
424
425         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
426
427         return retcode;
428 }
429
430 static meta_search_candidate_t
431 meta_back_search_start(
432         Operation               *op,
433         SlapReply               *rs,
434         dncookie                *dc,
435         metaconn_t              **mcp,
436         int                     candidate,
437         SlapReply               *candidates )
438 {
439         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
440         metatarget_t            *mt = mi->mi_targets[ candidate ];
441         metasingleconn_t        *msc = &(*mcp)->mc_conns[ candidate ];
442         struct berval           realbase = op->o_req_dn;
443         int                     realscope = op->ors_scope;
444         struct berval           mbase = BER_BVNULL; 
445         struct berval           mfilter = BER_BVNULL;
446         char                    **mapped_attrs = NULL;
447         int                     rc;
448         meta_search_candidate_t retcode;
449         struct timeval          tv, *tvp = NULL;
450         int                     nretries = 1;
451         LDAPControl             **ctrls = NULL;
452
453         /* this should not happen; just in case... */
454         if ( msc->msc_ld == NULL ) {
455                 Debug( LDAP_DEBUG_ANY,
456                         "%s: meta_back_search_start candidate=%d ld=NULL%s.\n",
457                         op->o_log_prefix, candidate,
458                         META_BACK_ONERR_STOP( mi ) ? "" : " (ignored)" );
459                 candidates[ candidate ].sr_err = LDAP_OTHER;
460                 if ( META_BACK_ONERR_STOP( mi ) ) {
461                         return META_SEARCH_ERR;
462                 }
463                 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
464                 return META_SEARCH_NOT_CANDIDATE;
465         }
466
467         Debug( LDAP_DEBUG_TRACE, "%s >>> meta_back_search_start[%d]\n", op->o_log_prefix, candidate, 0 );
468
469         /*
470          * modifies the base according to the scope, if required
471          */
472         if ( mt->mt_nsuffix.bv_len > op->o_req_ndn.bv_len ) {
473                 switch ( op->ors_scope ) {
474                 case LDAP_SCOPE_SUBTREE:
475                         /*
476                          * make the target suffix the new base
477                          * FIXME: this is very forgiving, because
478                          * "illegal" searchBases may be turned
479                          * into the suffix of the target; however,
480                          * the requested searchBase already passed
481                          * thru the candidate analyzer...
482                          */
483                         if ( dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) ) {
484                                 realbase = mt->mt_nsuffix;
485                                 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
486                                         realscope = LDAP_SCOPE_SUBORDINATE;
487                                 }
488
489                         } else {
490                                 /*
491                                  * this target is no longer candidate
492                                  */
493                                 retcode = META_SEARCH_NOT_CANDIDATE;
494                                 goto doreturn;
495                         }
496                         break;
497
498                 case LDAP_SCOPE_SUBORDINATE:
499                 case LDAP_SCOPE_ONELEVEL:
500                 {
501                         struct berval   rdn = mt->mt_nsuffix;
502                         rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
503                         if ( dnIsOneLevelRDN( &rdn )
504                                         && dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) )
505                         {
506                                 /*
507                                  * if there is exactly one level,
508                                  * make the target suffix the new
509                                  * base, and make scope "base"
510                                  */
511                                 realbase = mt->mt_nsuffix;
512                                 if ( op->ors_scope == LDAP_SCOPE_SUBORDINATE ) {
513                                         if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
514                                                 realscope = LDAP_SCOPE_SUBORDINATE;
515                                         } else {
516                                                 realscope = LDAP_SCOPE_SUBTREE;
517                                         }
518                                 } else {
519                                         realscope = LDAP_SCOPE_BASE;
520                                 }
521                                 break;
522                         } /* else continue with the next case */
523                 }
524
525                 case LDAP_SCOPE_BASE:
526                         /*
527                          * this target is no longer candidate
528                          */
529                         retcode = META_SEARCH_NOT_CANDIDATE;
530                         goto doreturn;
531                 }
532         }
533
534         /* initiate dobind */
535         retcode = meta_search_dobind_init( op, rs, mcp, candidate, candidates );
536
537         Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%d]=%d\n", op->o_log_prefix, candidate, retcode );
538
539         if ( retcode != META_SEARCH_CANDIDATE ) {
540                 goto doreturn;
541         }
542
543         /*
544          * Rewrite the search base, if required
545          */
546         dc->target = mt;
547         dc->ctx = "searchBase";
548         switch ( ldap_back_dn_massage( dc, &realbase, &mbase ) ) {
549         case LDAP_SUCCESS:
550                 break;
551
552         case LDAP_UNWILLING_TO_PERFORM:
553                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
554                 rs->sr_text = "Operation not allowed";
555                 send_ldap_result( op, rs );
556                 retcode = META_SEARCH_ERR;
557                 goto doreturn;
558
559         default:
560
561                 /*
562                  * this target is no longer candidate
563                  */
564                 retcode = META_SEARCH_NOT_CANDIDATE;
565                 goto doreturn;
566         }
567
568         /*
569          * Maps filter
570          */
571         rc = ldap_back_filter_map_rewrite( dc, op->ors_filter,
572                         &mfilter, BACKLDAP_MAP, op->o_tmpmemctx );
573         switch ( rc ) {
574         case LDAP_SUCCESS:
575                 break;
576
577         case LDAP_COMPARE_FALSE:
578         default:
579                 /*
580                  * this target is no longer candidate
581                  */
582                 retcode = META_SEARCH_NOT_CANDIDATE;
583                 goto done;
584         }
585
586         /*
587          * Maps required attributes
588          */
589         rc = ldap_back_map_attrs( op, &mt->mt_rwmap.rwm_at,
590                         op->ors_attrs, BACKLDAP_MAP, &mapped_attrs );
591         if ( rc != LDAP_SUCCESS ) {
592                 /*
593                  * this target is no longer candidate
594                  */
595                 retcode = META_SEARCH_NOT_CANDIDATE;
596                 goto done;
597         }
598
599         if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
600                 tv.tv_sec = op->ors_tlimit > 0 ? op->ors_tlimit : 1;
601                 tv.tv_usec = 0;
602                 tvp = &tv;
603         }
604
605 retry:;
606         ctrls = op->o_ctrls;
607         if ( meta_back_controls_add( op, rs, *mcp, candidate, &ctrls )
608                 != LDAP_SUCCESS )
609         {
610                 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
611                 retcode = META_SEARCH_NOT_CANDIDATE;
612                 goto done;
613         }
614
615         /*
616          * Starts the search
617          */
618         assert( msc->msc_ld != NULL );
619         rc = ldap_pvt_search( msc->msc_ld,
620                         mbase.bv_val, realscope, mfilter.bv_val,
621                         mapped_attrs, op->ors_attrsonly,
622                         ctrls, NULL, tvp, op->ors_slimit, op->ors_deref,
623                         &candidates[ candidate ].sr_msgid ); 
624         switch ( rc ) {
625         case LDAP_SUCCESS:
626                 retcode = META_SEARCH_CANDIDATE;
627                 break;
628         
629         case LDAP_SERVER_DOWN:
630                 if ( nretries && meta_back_retry( op, rs, mcp, candidate, LDAP_BACK_DONTSEND ) ) {
631                         nretries = 0;
632                         /* if the identity changed, there might be need to re-authz */
633                         (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
634                         goto retry;
635                 }
636
637                 if ( *mcp == NULL ) {
638                         retcode = META_SEARCH_ERR;
639                         candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
640                         break;
641                 }
642                 /* fall thru */
643
644         default:
645                 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
646                 retcode = META_SEARCH_NOT_CANDIDATE;
647         }
648
649 done:;
650         (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
651
652         if ( mapped_attrs ) {
653                 ber_memfree_x( mapped_attrs, op->o_tmpmemctx );
654         }
655         if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
656                 ber_memfree_x( mfilter.bv_val, op->o_tmpmemctx );
657         }
658         if ( mbase.bv_val != realbase.bv_val ) {
659                 free( mbase.bv_val );
660         }
661
662 doreturn:;
663         Debug( LDAP_DEBUG_TRACE, "%s <<< meta_back_search_start[%d]=%d\n", op->o_log_prefix, candidate, retcode );
664
665         return retcode;
666 }
667
668 int
669 meta_back_search( Operation *op, SlapReply *rs )
670 {
671         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
672         metaconn_t      *mc;
673         struct timeval  save_tv = { 0, 0 },
674                         tv;
675         time_t          stoptime = (time_t)(-1),
676                         lastres_time = slap_get_time(),
677                         timeout = 0;
678         int             rc = 0, sres = LDAP_SUCCESS;
679         char            *matched = NULL;
680         int             last = 0, ncandidates = 0,
681                         initial_candidates = 0, candidate_match = 0,
682                         needbind = 0;
683         ldap_back_send_t        sendok = LDAP_BACK_SENDERR;
684         long            i;
685         dncookie        dc;
686         int             is_ok = 0;
687         void            *savepriv;
688         SlapReply       *candidates = NULL;
689         int             do_taint = 0;
690
691         /*
692          * controls are set in ldap_back_dobind()
693          * 
694          * FIXME: in case of values return filter, we might want
695          * to map attrs and maybe rewrite value
696          */
697 getconn:;
698         mc = meta_back_getconn( op, rs, NULL, sendok );
699         if ( !mc ) {
700                 return rs->sr_err;
701         }
702
703         dc.conn = op->o_conn;
704         dc.rs = rs;
705
706         if ( candidates == NULL ) candidates = meta_back_candidates_get( op );
707         /*
708          * Inits searches
709          */
710         for ( i = 0; i < mi->mi_ntargets; i++ ) {
711                 /* reset sr_msgid; it is used in most loops
712                  * to check if that target is still to be considered */
713                 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
714
715                 /* a target is marked as candidate by meta_back_getconn();
716                  * if for any reason (an error, it's over or so) it is
717                  * no longer active, sr_msgid is set to META_MSGID_IGNORE
718                  * but it remains candidate, which means it has been active
719                  * at some point during the operation.  This allows to 
720                  * use its response code and more to compute the final
721                  * response */
722                 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
723                         continue;
724                 }
725
726                 candidates[ i ].sr_matched = NULL;
727                 candidates[ i ].sr_text = NULL;
728                 candidates[ i ].sr_ref = NULL;
729                 candidates[ i ].sr_ctrls = NULL;
730
731                 /* get largest timeout among candidates */
732                 if ( mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ]
733                         && mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ] > timeout )
734                 {
735                         timeout = mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ];
736                 }
737         }
738
739         for ( i = 0; i < mi->mi_ntargets; i++ ) {
740                 if ( !META_IS_CANDIDATE( &candidates[ i ] )
741                         || candidates[ i ].sr_err != LDAP_SUCCESS )
742                 {
743                         continue;
744                 }
745
746                 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
747                 {
748                 case META_SEARCH_NOT_CANDIDATE:
749                         candidates[ i ].sr_msgid = META_MSGID_IGNORE;
750                         break;
751
752                 case META_SEARCH_NEED_BIND:
753                         ++needbind;
754                         /* fallthru */
755
756                 case META_SEARCH_CONNECTING:
757                 case META_SEARCH_CANDIDATE:
758                 case META_SEARCH_BINDING:
759                         candidates[ i ].sr_type = REP_INTERMEDIATE;
760                         ++ncandidates;
761                         break;
762
763                 case META_SEARCH_ERR:
764                         savepriv = op->o_private;
765                         op->o_private = (void *)i;
766                         send_ldap_result( op, rs );
767                         op->o_private = savepriv;
768                         rc = -1;
769                         goto finish;
770
771                 default:
772                         assert( 0 );
773                         break;
774                 }
775         }
776
777         if ( ncandidates > 0 && needbind == ncandidates ) {
778                 /*
779                  * give up the second time...
780                  *
781                  * NOTE: this should not occur the second time, since a fresh
782                  * connection has ben created; however, targets may also
783                  * need bind because the bind timed out or so.
784                  */
785                 if ( sendok & LDAP_BACK_BINDING ) {
786                         Debug( LDAP_DEBUG_ANY,
787                                 "%s meta_back_search: unable to initialize conn\n",
788                                 op->o_log_prefix, 0, 0 );
789                         rs->sr_err = LDAP_UNAVAILABLE;
790                         rs->sr_text = "unable to initialize connection to remote targets";
791                         send_ldap_result( op, rs );
792                         rc = -1;
793                         goto finish;
794                 }
795
796                 /* FIXME: better create a separate connection? */
797                 sendok |= LDAP_BACK_BINDING;
798
799 #ifdef DEBUG_205
800                 Debug( LDAP_DEBUG_ANY, "*** %s drop mc=%p create new connection\n",
801                         op->o_log_prefix, (void *)mc, 0 );
802 #endif /* DEBUG_205 */
803
804                 meta_back_release_conn( mi, mc );
805                 mc = NULL;
806
807                 needbind = 0;
808                 ncandidates = 0;
809
810                 goto getconn;
811         }
812
813         initial_candidates = ncandidates;
814
815         if ( LogTest( LDAP_DEBUG_TRACE ) ) {
816                 char    cnd[ SLAP_TEXT_BUFLEN ];
817                 int     c;
818
819                 for ( c = 0; c < mi->mi_ntargets; c++ ) {
820                         if ( META_IS_CANDIDATE( &candidates[ c ] ) ) {
821                                 cnd[ c ] = '*';
822                         } else {
823                                 cnd[ c ] = ' ';
824                         }
825                 }
826                 cnd[ c ] = '\0';
827
828                 Debug( LDAP_DEBUG_TRACE, "%s meta_back_search: ncandidates=%d "
829                         "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
830         }
831
832         if ( initial_candidates == 0 ) {
833                 /* NOTE: here we are not sending any matchedDN;
834                  * this is intended, because if the back-meta
835                  * is serving this search request, but no valid
836                  * candidate could be looked up, it means that
837                  * there is a hole in the mapping of the targets
838                  * and thus no knowledge of any remote superior
839                  * is available */
840                 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: "
841                         "base=\"%s\" scope=%d: "
842                         "no candidate could be selected\n",
843                         op->o_log_prefix, op->o_req_dn.bv_val,
844                         op->ors_scope );
845
846                 /* FIXME: we're sending the first error we encounter;
847                  * maybe we should pick the worst... */
848                 rc = LDAP_NO_SUCH_OBJECT;
849                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
850                         if ( META_IS_CANDIDATE( &candidates[ i ] )
851                                 && candidates[ i ].sr_err != LDAP_SUCCESS )
852                         {
853                                 rc = candidates[ i ].sr_err;
854                                 break;
855                         }
856                 }
857
858                 send_ldap_error( op, rs, rc, NULL );
859
860                 goto finish;
861         }
862
863         /* We pull apart the ber result, stuff it into a slapd entry, and
864          * let send_search_entry stuff it back into ber format. Slow & ugly,
865          * but this is necessary for version matching, and for ACL processing.
866          */
867
868         if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
869                 stoptime = op->o_time + op->ors_tlimit;
870         }
871
872         /*
873          * In case there are no candidates, no cycle takes place...
874          *
875          * FIXME: we might use a queue, to better balance the load 
876          * among the candidates
877          */
878         for ( rc = 0; ncandidates > 0; ) {
879                 int     gotit = 0,
880                         doabandon = 0,
881                         alreadybound = ncandidates;
882
883                 /* check timeout */
884                 if ( timeout && lastres_time > 0
885                         && ( slap_get_time() - lastres_time ) > timeout )
886                 {
887                         doabandon = 1;
888                         rs->sr_text = "Operation timed out";
889                         rc = rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
890                                 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
891                         savepriv = op->o_private;
892                         op->o_private = (void *)i;
893                         send_ldap_result( op, rs );
894                         op->o_private = savepriv;
895                         goto finish;
896                 }
897
898                 /* check time limit */
899                 if ( op->ors_tlimit != SLAP_NO_LIMIT
900                                 && slap_get_time() > stoptime )
901                 {
902                         doabandon = 1;
903                         rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
904                         savepriv = op->o_private;
905                         op->o_private = (void *)i;
906                         send_ldap_result( op, rs );
907                         op->o_private = savepriv;
908                         goto finish;
909                 }
910
911                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
912                         meta_search_candidate_t retcode = META_SEARCH_UNDEFINED;
913                         metasingleconn_t        *msc = &mc->mc_conns[ i ];
914                         LDAPMessage             *res = NULL, *msg;
915
916                         /* if msgid is invalid, don't ldap_result() */
917                         if ( candidates[ i ].sr_msgid == META_MSGID_IGNORE ) {
918                                 continue;
919                         }
920
921                         /* if target still needs bind, retry */
922                         if ( candidates[ i ].sr_msgid == META_MSGID_NEED_BIND
923                                 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
924                         {
925                                 /* initiate dobind */
926                                 retcode = meta_search_dobind_init( op, rs, &mc, i, candidates );
927
928                                 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%ld]=%d\n",
929                                         op->o_log_prefix, i, retcode );
930
931                                 switch ( retcode ) {
932                                 case META_SEARCH_NEED_BIND:
933                                         alreadybound--;
934                                         /* fallthru */
935
936                                 case META_SEARCH_CONNECTING:
937                                 case META_SEARCH_BINDING:
938                                         break;
939
940                                 case META_SEARCH_ERR:
941                                         candidates[ i ].sr_err = rs->sr_err;
942                                         if ( META_BACK_ONERR_STOP( mi ) ) {
943                                                 savepriv = op->o_private;
944                                                 op->o_private = (void *)i;
945                                                 send_ldap_result( op, rs );
946                                                 op->o_private = savepriv;
947                                                 goto finish;
948                                         }
949                                         /* fallthru */
950
951                                 case META_SEARCH_NOT_CANDIDATE:
952                                         /*
953                                          * When no candidates are left,
954                                          * the outer cycle finishes
955                                          */
956                                         candidates[ i ].sr_msgid = META_MSGID_IGNORE;
957                                         assert( ncandidates > 0 );
958                                         --ncandidates;
959                                         break;
960
961                                 case META_SEARCH_CANDIDATE:
962                                         candidates[ i ].sr_msgid = META_MSGID_IGNORE;
963                                         switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
964                                         {
965                                         case META_SEARCH_CANDIDATE:
966                                                 assert( candidates[ i ].sr_msgid >= 0 );
967                                                 break;
968
969                                         case META_SEARCH_ERR:
970                                                 candidates[ i ].sr_err = rs->sr_err;
971                                                 if ( META_BACK_ONERR_STOP( mi ) ) {
972                                                         savepriv = op->o_private;
973                                                         op->o_private = (void *)i;
974                                                         send_ldap_result( op, rs );
975                                                         op->o_private = savepriv;
976                                                         goto finish;
977                                                 }
978                                                 /* fallthru */
979
980                                         case META_SEARCH_NOT_CANDIDATE:
981                                                 /* means that meta_back_search_start()
982                                                  * failed but onerr == continue */
983                                                 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
984                                                 assert( ncandidates > 0 );
985                                                 --ncandidates;
986                                                 break;
987
988                                         default:
989                                                 /* impossible */
990                                                 assert( 0 );
991                                                 break;
992                                         }
993                                         break;
994
995                                 default:
996                                         /* impossible */
997                                         assert( 0 );
998                                         break;
999                                 }
1000                                 continue;
1001                         }
1002
1003                         /* check for abandon */
1004                         if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( mc ) ) {
1005                                 break;
1006                         }
1007
1008 #ifdef DEBUG_205
1009                         if ( msc->msc_ld == NULL ) {
1010                                 char    buf[ SLAP_TEXT_BUFLEN ];
1011
1012                                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1013                                 snprintf( buf, sizeof( buf ),
1014                                         "%s meta_back_search[%ld] mc=%p msgid=%d%s%s%s\n",
1015                                         op->o_log_prefix, (long)i, (void *)mc,
1016                                         candidates[ i ].sr_msgid,
1017                                         META_IS_BINDING( &candidates[ i ] ) ? " binding" : "",
1018                                         LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ? " connbinding" : "",
1019                                         META_BACK_CONN_CREATING( &mc->mc_conns[ i ] ) ? " conncreating" : "" );
1020                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1021                                         
1022                                 Debug( LDAP_DEBUG_ANY, "!!! %s\n", buf, 0, 0 );
1023                         }
1024 #endif /* DEBUG_205 */
1025                         
1026                         /*
1027                          * FIXME: handle time limit as well?
1028                          * Note that target servers are likely 
1029                          * to handle it, so at some time we'll
1030                          * get a LDAP_TIMELIMIT_EXCEEDED from
1031                          * one of them ...
1032                          */
1033                         tv = save_tv;
1034                         rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
1035                                         LDAP_MSG_RECEIVED, &tv, &res );
1036                         switch ( rc ) {
1037                         case 0:
1038                                 /* FIXME: res should not need to be freed */
1039                                 assert( res == NULL );
1040                                 continue;
1041
1042                         case -1:
1043 really_bad:;
1044                                 /* something REALLY bad happened! */
1045                                 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1046                                         candidates[ i ].sr_type = REP_RESULT;
1047
1048                                         if ( meta_back_retry( op, rs, &mc, i, LDAP_BACK_DONTSEND ) ) {
1049                                                 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1050                                                 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
1051                                                 {
1052                                                         /* means that failed but onerr == continue */
1053                                                 case META_SEARCH_NOT_CANDIDATE:
1054                                                         candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1055
1056                                                         assert( ncandidates > 0 );
1057                                                         --ncandidates;
1058
1059                                                         candidates[ i ].sr_err = rs->sr_err;
1060                                                         if ( META_BACK_ONERR_STOP( mi ) ) {
1061                                                                 savepriv = op->o_private;
1062                                                                 op->o_private = (void *)i;
1063                                                                 send_ldap_result( op, rs );
1064                                                                 op->o_private = savepriv;
1065                                                                 goto finish;
1066                                                         }
1067                                                         /* fall thru */
1068
1069                                                 case META_SEARCH_CANDIDATE:
1070                                                         /* get back into business... */
1071                                                         continue;
1072
1073                                                 case META_SEARCH_BINDING:
1074                                                 case META_SEARCH_CONNECTING:
1075                                                 case META_SEARCH_NEED_BIND:
1076                                                 case META_SEARCH_UNDEFINED:
1077                                                         assert( 0 );
1078
1079                                                 default:
1080                                                         /* unrecoverable error */
1081                                                         candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1082                                                         rc = rs->sr_err = LDAP_OTHER;
1083                                                         goto finish;
1084                                                 }
1085                                         }
1086
1087                                         candidates[ i ].sr_err = rs->sr_err;
1088                                         if ( META_BACK_ONERR_STOP( mi ) ) {
1089                                                 savepriv = op->o_private;
1090                                                 op->o_private = (void *)i;
1091                                                 send_ldap_result( op, rs );
1092                                                 op->o_private = savepriv;
1093                                                 goto finish;
1094                                         }
1095                                 }
1096
1097                                 /*
1098                                  * When no candidates are left,
1099                                  * the outer cycle finishes
1100                                  */
1101                                 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1102                                 assert( ncandidates > 0 );
1103                                 --ncandidates;
1104                                 rs->sr_err = candidates[ i ].sr_err;
1105                                 continue;
1106
1107                         default:
1108                                 lastres_time = slap_get_time();
1109
1110                                 /* only touch when activity actually took place... */
1111                                 if ( mi->mi_idle_timeout != 0 && msc->msc_time < lastres_time ) {
1112                                         msc->msc_time = lastres_time;
1113                                 }
1114                                 break;
1115                         }
1116
1117                         for ( msg = ldap_first_message( msc->msc_ld, res );
1118                                 msg != NULL;
1119                                 msg = ldap_next_message( msc->msc_ld, msg ) )
1120                         {
1121                                 rc = ldap_msgtype( msg );
1122                                 if ( rc == LDAP_RES_SEARCH_ENTRY ) {
1123                                         LDAPMessage     *e;
1124
1125                                         if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1126                                                 /* don't retry any more... */
1127                                                 candidates[ i ].sr_type = REP_RESULT;
1128                                         }
1129
1130                                         is_ok++;
1131
1132                                         e = ldap_first_entry( msc->msc_ld, msg );
1133                                         savepriv = op->o_private;
1134                                         op->o_private = (void *)i;
1135                                         rs->sr_err = meta_send_entry( op, rs, mc, i, e );
1136
1137                                         switch ( rs->sr_err ) {
1138                                         case LDAP_SIZELIMIT_EXCEEDED:
1139                                                 savepriv = op->o_private;
1140                                                 op->o_private = (void *)i;
1141                                                 send_ldap_result( op, rs );
1142                                                 op->o_private = savepriv;
1143                                                 rs->sr_err = LDAP_SUCCESS;
1144                                                 ldap_msgfree( res );
1145                                                 res = NULL;
1146                                                 goto finish;
1147
1148                                         case LDAP_UNAVAILABLE:
1149                                                 rs->sr_err = LDAP_OTHER;
1150                                                 ldap_msgfree( res );
1151                                                 res = NULL;
1152                                                 goto finish;
1153                                         }
1154                                         op->o_private = savepriv;
1155
1156                                         /* don't wait any longer... */
1157                                         gotit = 1;
1158                                         save_tv.tv_sec = 0;
1159                                         save_tv.tv_usec = 0;
1160
1161                                 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
1162                                         char            **references = NULL;
1163                                         int             cnt;
1164
1165                                         if ( META_BACK_TGT_NOREFS( mi->mi_targets[ i ] ) ) {
1166                                                 continue;
1167                                         }
1168
1169                                         if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1170                                                 /* don't retry any more... */
1171                                                 candidates[ i ].sr_type = REP_RESULT;
1172                                         }
1173         
1174                                         is_ok++;
1175         
1176                                         rc = ldap_parse_reference( msc->msc_ld, msg,
1177                                                         &references, &rs->sr_ctrls, 0 );
1178         
1179                                         if ( rc != LDAP_SUCCESS ) {
1180                                                 continue;
1181                                         }
1182         
1183                                         if ( references == NULL ) {
1184                                                 continue;
1185                                         }
1186
1187 #ifdef ENABLE_REWRITE
1188                                         dc.ctx = "referralDN";
1189 #else /* ! ENABLE_REWRITE */
1190                                         dc.tofrom = 0;
1191                                         dc.normalized = 0;
1192 #endif /* ! ENABLE_REWRITE */
1193
1194                                         /* FIXME: merge all and return at the end */
1195         
1196                                         for ( cnt = 0; references[ cnt ]; cnt++ )
1197                                                 ;
1198         
1199                                         rs->sr_ref = ber_memalloc_x( sizeof( struct berval ) * ( cnt + 1 ),
1200                                                 op->o_tmpmemctx );
1201         
1202                                         for ( cnt = 0; references[ cnt ]; cnt++ ) {
1203                                                 ber_str2bv_x( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ],
1204                                                 op->o_tmpmemctx );
1205                                         }
1206                                         BER_BVZERO( &rs->sr_ref[ cnt ] );
1207         
1208                                         ( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref,
1209                                                 op->o_tmpmemctx );
1210
1211                                         if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
1212                                                 /* ignore return value by now */
1213                                                 savepriv = op->o_private;
1214                                                 op->o_private = (void *)i;
1215                                                 ( void )send_search_reference( op, rs );
1216                                                 op->o_private = savepriv;
1217         
1218                                                 ber_bvarray_free_x( rs->sr_ref, op->o_tmpmemctx );
1219                                                 rs->sr_ref = NULL;
1220                                         }
1221
1222                                         /* cleanup */
1223                                         if ( references ) {
1224                                                 ber_memvfree( (void **)references );
1225                                         }
1226
1227                                         if ( rs->sr_ctrls ) {
1228                                                 ldap_controls_free( rs->sr_ctrls );
1229                                                 rs->sr_ctrls = NULL;
1230                                         }
1231
1232                                 } else if ( rc == LDAP_RES_INTERMEDIATE ) {
1233                                         if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1234                                                 /* don't retry any more... */
1235                                                 candidates[ i ].sr_type = REP_RESULT;
1236                                         }
1237         
1238                                         /* FIXME: response controls
1239                                          * are passed without checks */
1240                                         rs->sr_err = ldap_parse_intermediate( msc->msc_ld,
1241                                                 msg,
1242                                                 (char **)&rs->sr_rspoid,
1243                                                 &rs->sr_rspdata,
1244                                                 &rs->sr_ctrls,
1245                                                 0 );
1246                                         if ( rs->sr_err != LDAP_SUCCESS ) {
1247                                                 candidates[ i ].sr_type = REP_RESULT;
1248                                                 ldap_msgfree( res );
1249                                                 res = NULL;
1250                                                 goto really_bad;
1251                                         }
1252
1253                                         slap_send_ldap_intermediate( op, rs );
1254
1255                                         if ( rs->sr_rspoid != NULL ) {
1256                                                 ber_memfree( (char *)rs->sr_rspoid );
1257                                                 rs->sr_rspoid = NULL;
1258                                         }
1259
1260                                         if ( rs->sr_rspdata != NULL ) {
1261                                                 ber_bvfree( rs->sr_rspdata );
1262                                                 rs->sr_rspdata = NULL;
1263                                         }
1264
1265                                         if ( rs->sr_ctrls != NULL ) {
1266                                                 ldap_controls_free( rs->sr_ctrls );
1267                                                 rs->sr_ctrls = NULL;
1268                                         }
1269
1270                                 } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
1271                                         char            buf[ SLAP_TEXT_BUFLEN ];
1272                                         char            **references = NULL;
1273
1274                                         if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1275                                                 /* don't retry any more... */
1276                                                 candidates[ i ].sr_type = REP_RESULT;
1277                                         }
1278         
1279                                         candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1280
1281                                         /* NOTE: ignores response controls
1282                                          * (and intermediate response controls
1283                                          * as well, except for those with search
1284                                          * references); this may not be correct,
1285                                          * but if they're not ignored then
1286                                          * back-meta would need to merge them
1287                                          * consistently (think of pagedResults...)
1288                                          */
1289                                         /* FIXME: response controls? */
1290                                         rs->sr_err = ldap_parse_result( msc->msc_ld,
1291                                                 msg,
1292                                                 &candidates[ i ].sr_err,
1293                                                 (char **)&candidates[ i ].sr_matched,
1294                                                 (char **)&candidates[ i ].sr_text,
1295                                                 &references,
1296                                                 NULL /* &candidates[ i ].sr_ctrls (unused) */ ,
1297                                                 0 );
1298                                         if ( rs->sr_err != LDAP_SUCCESS ) {
1299                                                 candidates[ i ].sr_err = rs->sr_err;
1300                                                 sres = slap_map_api2result( &candidates[ i ] );
1301                                                 candidates[ i ].sr_type = REP_RESULT;
1302                                                 ldap_msgfree( res );
1303                                                 res = NULL;
1304                                                 goto really_bad;
1305                                         }
1306
1307                                         rs->sr_err = candidates[ i ].sr_err;
1308
1309                                         /* massage matchedDN if need be */
1310                                         if ( candidates[ i ].sr_matched != NULL ) {
1311                                                 struct berval   match, mmatch;
1312
1313                                                 ber_str2bv( candidates[ i ].sr_matched,
1314                                                         0, 0, &match );
1315                                                 candidates[ i ].sr_matched = NULL;
1316
1317                                                 dc.ctx = "matchedDN";
1318                                                 dc.target = mi->mi_targets[ i ];
1319                                                 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
1320                                                         if ( mmatch.bv_val == match.bv_val ) {
1321                                                                 candidates[ i ].sr_matched
1322                                                                         = ch_strdup( mmatch.bv_val );
1323
1324                                                         } else {
1325                                                                 candidates[ i ].sr_matched = mmatch.bv_val;
1326                                                         }
1327
1328                                                         candidate_match++;
1329                                                 } 
1330                                                 ldap_memfree( match.bv_val );
1331                                         }
1332
1333                                         /* add references to array */
1334                                         /* RFC 4511: referrals can only appear
1335                                          * if result code is LDAP_REFERRAL */
1336                                         if ( references != NULL
1337                                                 && references[ 0 ] != NULL
1338                                                 && references[ 0 ][ 0 ] != '\0' )
1339                                         {
1340                                                 if ( rs->sr_err != LDAP_REFERRAL ) {
1341                                                         Debug( LDAP_DEBUG_ANY,
1342                                                                 "%s meta_back_search[%ld]: "
1343                                                                 "got referrals with err=%d\n",
1344                                                                 op->o_log_prefix,
1345                                                                 i, rs->sr_err );
1346
1347                                                 } else {
1348                                                         BerVarray       sr_ref;
1349                                                         int             cnt;
1350         
1351                                                         for ( cnt = 0; references[ cnt ]; cnt++ )
1352                                                                 ;
1353         
1354                                                         sr_ref = ber_memalloc_x( sizeof( struct berval ) * ( cnt + 1 ),
1355                                                                 op->o_tmpmemctx );
1356         
1357                                                         for ( cnt = 0; references[ cnt ]; cnt++ ) {
1358                                                                 ber_str2bv_x( references[ cnt ], 0, 1, &sr_ref[ cnt ],
1359                                                                         op->o_tmpmemctx );
1360                                                         }
1361                                                         BER_BVZERO( &sr_ref[ cnt ] );
1362         
1363                                                         ( void )ldap_back_referral_result_rewrite( &dc, sr_ref,
1364                                                                 op->o_tmpmemctx );
1365                                         
1366                                                         if ( rs->sr_v2ref == NULL ) {
1367                                                                 rs->sr_v2ref = sr_ref;
1368
1369                                                         } else {
1370                                                                 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
1371                                                                         ber_bvarray_add_x( &rs->sr_v2ref, &sr_ref[ cnt ],
1372                                                                                 op->o_tmpmemctx );
1373                                                                 }
1374                                                                 ber_memfree_x( sr_ref, op->o_tmpmemctx );
1375                                                         }
1376                                                 }
1377
1378                                         } else if ( rs->sr_err == LDAP_REFERRAL ) {
1379                                                 Debug( LDAP_DEBUG_ANY,
1380                                                         "%s meta_back_search[%ld]: "
1381                                                         "got err=%d with null "
1382                                                         "or empty referrals\n",
1383                                                         op->o_log_prefix,
1384                                                         i, rs->sr_err );
1385
1386                                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1387                                         }
1388
1389                                         /* cleanup */
1390                                         ber_memvfree( (void **)references );
1391         
1392                                         sres = slap_map_api2result( rs );
1393         
1394                                         if ( LogTest( LDAP_DEBUG_TRACE | LDAP_DEBUG_ANY ) ) {
1395                                                 snprintf( buf, sizeof( buf ),
1396                                                         "%s meta_back_search[%ld] "
1397                                                         "match=\"%s\" err=%ld",
1398                                                         op->o_log_prefix, i,
1399                                                         candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
1400                                                         (long) candidates[ i ].sr_err );
1401                                                 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1402                                                         Debug( LDAP_DEBUG_TRACE, "%s.\n", buf, 0, 0 );
1403         
1404                                                 } else {
1405                                                         Debug( LDAP_DEBUG_ANY, "%s (%s).\n",
1406                                                                 buf, ldap_err2string( candidates[ i ].sr_err ), 0 );
1407                                                 }
1408                                         }
1409         
1410                                         switch ( sres ) {
1411                                         case LDAP_NO_SUCH_OBJECT:
1412                                                 /* is_ok is touched any time a valid
1413                                                  * (even intermediate) result is
1414                                                  * returned; as a consequence, if
1415                                                  * a candidate returns noSuchObject
1416                                                  * it is ignored and the candidate
1417                                                  * is simply demoted. */
1418                                                 if ( is_ok ) {
1419                                                         sres = LDAP_SUCCESS;
1420                                                 }
1421                                                 break;
1422         
1423                                         case LDAP_SUCCESS:
1424                                         case LDAP_REFERRAL:
1425                                                 is_ok++;
1426                                                 break;
1427         
1428                                         case LDAP_SIZELIMIT_EXCEEDED:
1429                                                 /* if a target returned sizelimitExceeded
1430                                                  * and the entry count is equal to the
1431                                                  * proxy's limit, the target would have
1432                                                  * returned more, and the error must be
1433                                                  * propagated to the client; otherwise,
1434                                                  * the target enforced a limit lower
1435                                                  * than what requested by the proxy;
1436                                                  * ignore it */
1437                                                 candidates[ i ].sr_err = rs->sr_err;
1438                                                 if ( rs->sr_nentries == op->ors_slimit
1439                                                         || META_BACK_ONERR_STOP( mi ) )
1440                                                 {
1441                                                         char *save_text = rs->sr_text;
1442                                                         savepriv = op->o_private;
1443                                                         op->o_private = (void *)i;
1444                                                         rs->sr_text = candidates[ i ].sr_text;
1445                                                         send_ldap_result( op, rs );
1446                                                         rs->sr_text = save_text;
1447                                                         op->o_private = savepriv;
1448                                                         ldap_msgfree( res );
1449                                                         res = NULL;
1450                                                         goto finish;
1451                                                 }
1452                                                 break;
1453         
1454                                         default:
1455                                                 candidates[ i ].sr_err = rs->sr_err;
1456                                                 if ( META_BACK_ONERR_STOP( mi ) ) {
1457                                                         char *save_text = rs->sr_text;
1458                                                         savepriv = op->o_private;
1459                                                         op->o_private = (void *)i;
1460                                                         rs->sr_text = candidates[ i ].sr_text;
1461                                                         send_ldap_result( op, rs );
1462                                                         rs->sr_text = save_text;
1463                                                         op->o_private = savepriv;
1464                                                         ldap_msgfree( res );
1465                                                         res = NULL;
1466                                                         goto finish;
1467                                                 }
1468                                                 break;
1469                                         }
1470         
1471                                         last = i;
1472                                         rc = 0;
1473         
1474                                         /*
1475                                          * When no candidates are left,
1476                                          * the outer cycle finishes
1477                                          */
1478                                         assert( ncandidates > 0 );
1479                                         --ncandidates;
1480
1481                                 } else if ( rc == LDAP_RES_BIND ) {
1482                                         meta_search_candidate_t retcode;
1483         
1484                                         retcode = meta_search_dobind_result( op, rs, &mc, i, candidates, msg );
1485                                         if ( retcode == META_SEARCH_CANDIDATE ) {
1486                                                 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1487                                                 retcode = meta_back_search_start( op, rs, &dc, &mc, i, candidates );
1488                                         }
1489         
1490                                         switch ( retcode ) {
1491                                         case META_SEARCH_CANDIDATE:
1492                                                 break;
1493         
1494                                                 /* means that failed but onerr == continue */
1495                                         case META_SEARCH_NOT_CANDIDATE:
1496                                         case META_SEARCH_ERR:
1497                                                 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1498                                                 assert( ncandidates > 0 );
1499                                                 --ncandidates;
1500         
1501                                                 candidates[ i ].sr_err = rs->sr_err;
1502                                                 if ( META_BACK_ONERR_STOP( mi ) ) {
1503                                                         savepriv = op->o_private;
1504                                                         op->o_private = (void *)i;
1505                                                         send_ldap_result( op, rs );
1506                                                         op->o_private = savepriv;
1507                                                         ldap_msgfree( res );
1508                                                         res = NULL;
1509                                                         goto finish;
1510                                                 }
1511                                                 goto free_message;
1512         
1513                                         default:
1514                                                 assert( 0 );
1515                                                 break;
1516                                         }
1517         
1518                                 } else {
1519                                         Debug( LDAP_DEBUG_ANY,
1520                                                 "%s meta_back_search[%ld]: "
1521                                                 "unrecognized response message tag=%d\n",
1522                                                 op->o_log_prefix,
1523                                                 i, rc );
1524                                 
1525                                         ldap_msgfree( res );
1526                                         res = NULL;
1527                                         goto really_bad;
1528                                 }
1529                         }
1530
1531 free_message:;
1532                         ldap_msgfree( res );
1533                         res = NULL;
1534                 }
1535
1536                 /* check for abandon */
1537                 if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( mc ) ) {
1538                         for ( i = 0; i < mi->mi_ntargets; i++ ) {
1539                                 if ( candidates[ i ].sr_msgid >= 0
1540                                         || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1541                                 {
1542                                         if ( META_IS_BINDING( &candidates[ i ] )
1543                                                 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1544                                         {
1545                                                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1546                                                 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] )
1547                                                         || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1548                                                 {
1549                                                         /* if still binding, destroy */
1550
1551 #ifdef DEBUG_205
1552                                                         char buf[ SLAP_TEXT_BUFLEN ];
1553
1554                                                         snprintf( buf, sizeof( buf), "%s meta_back_search(abandon) "
1555                                                                 "ldap_unbind_ext[%ld] mc=%p ld=%p",
1556                                                                 op->o_log_prefix, i, (void *)mc,
1557                                                                 (void *)mc->mc_conns[i].msc_ld );
1558
1559                                                         Debug( LDAP_DEBUG_ANY, "### %s\n", buf, 0, 0 );
1560 #endif /* DEBUG_205 */
1561
1562                                                         meta_clear_one_candidate( op, mc, i );
1563                                                 }
1564                                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1565                                                 META_BINDING_CLEAR( &candidates[ i ] );
1566                                                 
1567                                         } else {
1568                                                 (void)meta_back_cancel( mc, op, rs,
1569                                                         candidates[ i ].sr_msgid, i,
1570                                                         LDAP_BACK_DONTSEND );
1571                                         }
1572
1573                                         candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1574                                         assert( ncandidates > 0 );
1575                                         --ncandidates;
1576                                 }
1577                         }
1578
1579                         if ( op->o_abandon ) {
1580                                 rc = SLAPD_ABANDON;
1581                         }
1582
1583                         /* let send_ldap_result play cleanup handlers (ITS#4645) */
1584                         break;
1585                 }
1586
1587                 /* if no entry was found during this loop,
1588                  * set a minimal timeout */
1589                 if ( ncandidates > 0 && gotit == 0 ) {
1590                         if ( save_tv.tv_sec == 0 && save_tv.tv_usec == 0 ) {
1591                                 save_tv.tv_usec = LDAP_BACK_RESULT_UTIMEOUT/initial_candidates;
1592
1593                                 /* arbitrarily limit to something between 1 and 2 minutes */
1594                         } else if ( ( stoptime == -1 && save_tv.tv_sec < 60 )
1595                                 || save_tv.tv_sec < ( stoptime - slap_get_time() ) / ( 2 * ncandidates ) )
1596                         {
1597                                 /* double the timeout */
1598                                 lutil_timermul( &save_tv, 2, &save_tv );
1599                         }
1600
1601                         if ( alreadybound == 0 ) {
1602                                 tv = save_tv;
1603                                 (void)select( 0, NULL, NULL, NULL, &tv );
1604
1605                         } else {
1606                                 ldap_pvt_thread_yield();
1607                         }
1608                 }
1609         }
1610
1611         if ( rc == -1 ) {
1612                 /*
1613                  * FIXME: need a better strategy to handle errors
1614                  */
1615                 if ( mc ) {
1616                         rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE,
1617                                 -1, stoptime != -1 ? (stoptime - slap_get_time()) : 0,
1618                                 LDAP_BACK_SENDERR );
1619                 } else {
1620                         rc = rs->sr_err;
1621                 }
1622                 goto finish;
1623         }
1624
1625         /*
1626          * Rewrite the matched portion of the search base, if required
1627          * 
1628          * FIXME: only the last one gets caught!
1629          */
1630         savepriv = op->o_private;
1631         op->o_private = (void *)(long)mi->mi_ntargets;
1632         if ( candidate_match > 0 ) {
1633                 struct berval   pmatched = BER_BVNULL;
1634
1635                 /* we use the first one */
1636                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1637                         if ( META_IS_CANDIDATE( &candidates[ i ] )
1638                                         && candidates[ i ].sr_matched != NULL )
1639                         {
1640                                 struct berval   bv, pbv;
1641                                 int             rc;
1642
1643                                 /* if we got success, and this target
1644                                  * returned noSuchObject, and its suffix
1645                                  * is a superior of the searchBase,
1646                                  * ignore the matchedDN */
1647                                 if ( sres == LDAP_SUCCESS
1648                                         && candidates[ i ].sr_err == LDAP_NO_SUCH_OBJECT
1649                                         && op->o_req_ndn.bv_len > mi->mi_targets[ i ]->mt_nsuffix.bv_len )
1650                                 {
1651                                         free( (char *)candidates[ i ].sr_matched );
1652                                         candidates[ i ].sr_matched = NULL;
1653                                         continue;
1654                                 }
1655
1656                                 ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
1657                                 rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
1658
1659                                 if ( rc == LDAP_SUCCESS ) {
1660
1661                                         /* NOTE: if they all are superiors
1662                                          * of the baseDN, the shorter is also 
1663                                          * superior of the longer... */
1664                                         if ( pbv.bv_len > pmatched.bv_len ) {
1665                                                 if ( !BER_BVISNULL( &pmatched ) ) {
1666                                                         op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
1667                                                 }
1668                                                 pmatched = pbv;
1669                                                 op->o_private = (void *)i;
1670
1671                                         } else {
1672                                                 op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
1673                                         }
1674                                 }
1675
1676                                 if ( candidates[ i ].sr_matched != NULL ) {
1677                                         free( (char *)candidates[ i ].sr_matched );
1678                                         candidates[ i ].sr_matched = NULL;
1679                                 }
1680                         }
1681                 }
1682
1683                 if ( !BER_BVISNULL( &pmatched ) ) {
1684                         matched = pmatched.bv_val;
1685                 }
1686
1687         } else if ( sres == LDAP_NO_SUCH_OBJECT ) {
1688                 matched = op->o_bd->be_suffix[ 0 ].bv_val;
1689         }
1690
1691         /*
1692          * In case we returned at least one entry, we return LDAP_SUCCESS
1693          * otherwise, the latter error code we got
1694          */
1695
1696         if ( sres == LDAP_SUCCESS ) {
1697                 if ( rs->sr_v2ref ) {
1698                         sres = LDAP_REFERRAL;
1699                 }
1700
1701                 if ( META_BACK_ONERR_REPORT( mi ) ) {
1702                         /*
1703                          * Report errors, if any
1704                          *
1705                          * FIXME: we should handle error codes and return the more 
1706                          * important/reasonable
1707                          */
1708                         for ( i = 0; i < mi->mi_ntargets; i++ ) {
1709                                 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1710                                         continue;
1711                                 }
1712
1713                                 if ( candidates[ i ].sr_err != LDAP_SUCCESS
1714                                         && candidates[ i ].sr_err != LDAP_NO_SUCH_OBJECT )
1715                                 {
1716                                         sres = candidates[ i ].sr_err;
1717                                         break;
1718                                 }
1719                         }
1720                 }
1721         }
1722
1723         rs->sr_err = sres;
1724         rs->sr_matched = matched;
1725         rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
1726         send_ldap_result( op, rs );
1727         op->o_private = savepriv;
1728         rs->sr_matched = NULL;
1729         rs->sr_ref = NULL;
1730
1731 finish:;
1732         if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
1733                 op->o_tmpfree( matched, op->o_tmpmemctx );
1734         }
1735
1736         if ( rs->sr_v2ref ) {
1737                 ber_bvarray_free_x( rs->sr_v2ref, op->o_tmpmemctx );
1738         }
1739
1740         for ( i = 0; i < mi->mi_ntargets; i++ ) {
1741                 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1742                         continue;
1743                 }
1744
1745                 if ( mc ) {
1746                         if ( META_IS_BINDING( &candidates[ i ] )
1747                                 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1748                         {
1749                                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1750                                 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] )
1751                                         || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1752                                 {
1753                                         assert( candidates[ i ].sr_msgid >= 0
1754                                                 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING );
1755                                         assert( mc->mc_conns[ i ].msc_ld != NULL );
1756
1757 #ifdef DEBUG_205
1758                                         Debug( LDAP_DEBUG_ANY, "### %s meta_back_search(cleanup) "
1759                                                 "ldap_unbind_ext[%ld] ld=%p\n",
1760                                                 op->o_log_prefix, i, (void *)mc->mc_conns[i].msc_ld );
1761 #endif /* DEBUG_205 */
1762
1763                                         /* if still binding, destroy */
1764                                         meta_clear_one_candidate( op, mc, i );
1765                                 }
1766                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1767                                 META_BINDING_CLEAR( &candidates[ i ] );
1768
1769                         } else if ( candidates[ i ].sr_msgid >= 0 ) {
1770                                 (void)meta_back_cancel( mc, op, rs,
1771                                         candidates[ i ].sr_msgid, i,
1772                                         LDAP_BACK_DONTSEND );
1773                         }
1774                 }
1775
1776                 if ( candidates[ i ].sr_matched ) {
1777                         free( (char *)candidates[ i ].sr_matched );
1778                         candidates[ i ].sr_matched = NULL;
1779                 }
1780
1781                 if ( candidates[ i ].sr_text ) {
1782                         ldap_memfree( (char *)candidates[ i ].sr_text );
1783                         candidates[ i ].sr_text = NULL;
1784                 }
1785
1786                 if ( candidates[ i ].sr_ref ) {
1787                         ber_bvarray_free( candidates[ i ].sr_ref );
1788                         candidates[ i ].sr_ref = NULL;
1789                 }
1790
1791                 if ( candidates[ i ].sr_ctrls ) {
1792                         ldap_controls_free( candidates[ i ].sr_ctrls );
1793                         candidates[ i ].sr_ctrls = NULL;
1794                 }
1795
1796                 if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ i ] ) ) {
1797                         meta_back_quarantine( op, &candidates[ i ], i );
1798                 }
1799
1800                 /* only in case of timelimit exceeded, if the timelimit exceeded because
1801                  * one contacted target never responded, invalidate the connection
1802                  * NOTE: should we quarantine the target as well?  right now, the connection
1803                  * is invalidated; the next time it will be recreated and the target
1804                  * will be quarantined if it cannot be contacted */
1805                 if ( mi->mi_idle_timeout != 0
1806                         && rs->sr_err == LDAP_TIMELIMIT_EXCEEDED
1807                         && op->o_time > mc->mc_conns[ i ].msc_time )
1808                 {
1809                         /* don't let anyone else use this expired connection */
1810                         do_taint++;
1811                 }
1812         }
1813
1814         if ( mc ) {
1815                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1816                 if ( do_taint ) {
1817                         LDAP_BACK_CONN_TAINTED_SET( mc );
1818                 }
1819                 meta_back_release_conn_lock( mi, mc, 0 );
1820                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1821         }
1822
1823         return rs->sr_err;
1824 }
1825
1826 static int
1827 meta_send_entry(
1828         Operation       *op,
1829         SlapReply       *rs,
1830         metaconn_t      *mc,
1831         int             target,
1832         LDAPMessage     *e )
1833 {
1834         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
1835         struct berval           a, mapped;
1836         int                     check_duplicate_attrs = 0;
1837         int                     check_sorted_attrs = 0;
1838         Entry                   ent = { 0 };
1839         BerElement              ber = *e->lm_ber;
1840         Attribute               *attr, **attrp;
1841         struct berval           bdn,
1842                                 dn = BER_BVNULL;
1843         const char              *text;
1844         dncookie                dc;
1845         ber_len_t               len;
1846         ber_tag_t               tag;
1847         int                     rc;
1848
1849         if ( ber_scanf( &ber, "l{", &len ) == LBER_ERROR ) {
1850                 return LDAP_DECODING_ERROR;
1851         }
1852
1853         if ( ber_set_option( &ber, LBER_OPT_REMAINING_BYTES, &len ) != LBER_OPT_SUCCESS ) {
1854                 return LDAP_OTHER;
1855         }
1856
1857         if ( ber_scanf( &ber, "m{", &bdn ) == LBER_ERROR ) {
1858                 return LDAP_DECODING_ERROR;
1859         }
1860
1861         /*
1862          * Rewrite the dn of the result, if needed
1863          */
1864         dc.target = mi->mi_targets[ target ];
1865         dc.conn = op->o_conn;
1866         dc.rs = rs;
1867         dc.ctx = "searchResult";
1868
1869         rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
1870         if ( rs->sr_err != LDAP_SUCCESS) {
1871                 return rs->sr_err;
1872         }
1873
1874         /*
1875          * Note: this may fail if the target host(s) schema differs
1876          * from the one known to the meta, and a DN with unknown
1877          * attributes is returned.
1878          * 
1879          * FIXME: should we log anything, or delegate to dnNormalize?
1880          */
1881         rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
1882                 op->o_tmpmemctx );
1883         if ( dn.bv_val != bdn.bv_val ) {
1884                 free( dn.bv_val );
1885         }
1886         BER_BVZERO( &dn );
1887
1888         if ( rc != LDAP_SUCCESS ) {
1889                 Debug( LDAP_DEBUG_ANY,
1890                         "%s meta_send_entry(\"%s\"): "
1891                         "invalid DN syntax\n",
1892                         op->o_log_prefix, ent.e_name.bv_val, 0 );
1893                 rc = LDAP_INVALID_DN_SYNTAX;
1894                 goto done;
1895         }
1896
1897         /*
1898          * cache dn
1899          */
1900         if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1901                 ( void )meta_dncache_update_entry( &mi->mi_cache,
1902                                 &ent.e_nname, target );
1903         }
1904
1905         attrp = &ent.e_attrs;
1906
1907         dc.ctx = "searchAttrDN";
1908         while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
1909                 int                             last = 0;
1910                 slap_syntax_validate_func       *validate;
1911                 slap_syntax_transform_func      *pretty;
1912
1913                 if ( ber_pvt_ber_remaining( &ber ) < 0 ) {
1914                         Debug( LDAP_DEBUG_ANY,
1915                                 "%s meta_send_entry(\"%s\"): "
1916                                 "unable to parse attr \"%s\".\n",
1917                                 op->o_log_prefix, ent.e_name.bv_val, a.bv_val );
1918                                 
1919                         rc = LDAP_OTHER;
1920                         goto done;
1921                 }
1922
1923                 if ( ber_pvt_ber_remaining( &ber ) == 0 ) {
1924                         break;
1925                 }
1926
1927                 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_at, 
1928                                 &a, &mapped, BACKLDAP_REMAP );
1929                 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
1930                         ( void )ber_scanf( &ber, "x" /* [W] */ );
1931                         continue;
1932                 }
1933                 if ( mapped.bv_val != a.bv_val ) {
1934                         /* will need to check for duplicate attrs */
1935                         check_duplicate_attrs++;
1936                 }
1937                 attr = attr_alloc( NULL );
1938                 if ( attr == NULL ) {
1939                         rc = LDAP_OTHER;
1940                         goto done;
1941                 }
1942                 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
1943                                 != LDAP_SUCCESS) {
1944                         if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
1945                                 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
1946                         {
1947                                 char    buf[ SLAP_TEXT_BUFLEN ];
1948
1949                                 snprintf( buf, sizeof( buf ),
1950                                         "%s meta_send_entry(\"%s\"): "
1951                                         "slap_bv2undef_ad(%s): %s\n",
1952                                         op->o_log_prefix, ent.e_name.bv_val,
1953                                         mapped.bv_val, text );
1954
1955                                 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
1956                                 ( void )ber_scanf( &ber, "x" /* [W] */ );
1957                                 attr_free( attr );
1958                                 continue;
1959                         }
1960                 }
1961
1962                 if ( attr->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL )
1963                         check_sorted_attrs = 1;
1964
1965                 /* no subschemaSubentry */
1966                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
1967                         || attr->a_desc == slap_schema.si_ad_entryDN )
1968                 {
1969
1970                         /* 
1971                          * We eat target's subschemaSubentry because
1972                          * a search for this value is likely not
1973                          * to resolve to the appropriate backend;
1974                          * later, the local subschemaSubentry is
1975                          * added.
1976                          *
1977                          * We also eat entryDN because the frontend
1978                          * will reattach it without checking if already
1979                          * present...
1980                          */
1981                         ( void )ber_scanf( &ber, "x" /* [W] */ );
1982                         attr_free(attr);
1983                         continue;
1984                 }
1985
1986                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR 
1987                                 || attr->a_vals == NULL )
1988                 {
1989                         attr->a_vals = (struct berval *)&slap_dummy_bv;
1990
1991                 } else {
1992                         for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
1993                                 ;
1994                 }
1995                 attr->a_numvals = last;
1996
1997                 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
1998                 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
1999
2000                 if ( !validate && !pretty ) {
2001                         attr_free( attr );
2002                         goto next_attr;
2003                 }
2004
2005                 if ( attr->a_desc == slap_schema.si_ad_objectClass
2006                                 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
2007                 {
2008                         struct berval   *bv;
2009
2010                         for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
2011                                 ObjectClass *oc;
2012
2013                                 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_oc,
2014                                                 bv, &mapped, BACKLDAP_REMAP );
2015                                 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
2016 remove_oc:;
2017                                         free( bv->bv_val );
2018                                         BER_BVZERO( bv );
2019                                         if ( --last < 0 ) {
2020                                                 break;
2021                                         }
2022                                         *bv = attr->a_vals[ last ];
2023                                         BER_BVZERO( &attr->a_vals[ last ] );
2024                                         bv--;
2025
2026                                 } else if ( mapped.bv_val != bv->bv_val ) {
2027                                         int     i;
2028
2029                                         for ( i = 0; !BER_BVISNULL( &attr->a_vals[ i ] ); i++ ) {
2030                                                 if ( &attr->a_vals[ i ] == bv ) {
2031                                                         continue;
2032                                                 }
2033
2034                                                 if ( ber_bvstrcasecmp( &mapped, &attr->a_vals[ i ] ) == 0 ) {
2035                                                         break;
2036                                                 }
2037                                         }
2038
2039                                         if ( !BER_BVISNULL( &attr->a_vals[ i ] ) ) {
2040                                                 goto remove_oc;
2041                                         }
2042
2043                                         ber_bvreplace( bv, &mapped );
2044
2045                                 } else if ( ( oc = oc_bvfind_undef( bv ) ) == NULL ) {
2046                                         goto remove_oc;
2047
2048                                 } else {
2049                                         ber_bvreplace( bv, &oc->soc_cname );
2050                                 }
2051                         }
2052                 /*
2053                  * It is necessary to try to rewrite attributes with
2054                  * dn syntax because they might be used in ACLs as
2055                  * members of groups; since ACLs are applied to the
2056                  * rewritten stuff, no dn-based subecj clause could
2057                  * be used at the ldap backend side (see
2058                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
2059                  * The problem can be overcome by moving the dn-based
2060                  * ACLs to the target directory server, and letting
2061                  * everything pass thru the ldap backend.
2062                  */
2063                 } else {
2064                         int     i;
2065
2066                         if ( attr->a_desc->ad_type->sat_syntax ==
2067                                 slap_schema.si_syn_distinguishedName )
2068                         {
2069                                 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
2070
2071                         } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
2072                                 ldap_back_referral_result_rewrite( &dc, attr->a_vals, NULL );
2073
2074                         }
2075
2076                         for ( i = 0; i < last; i++ ) {
2077                                 struct berval   pval;
2078                                 int             rc;
2079
2080                                 if ( pretty ) {
2081                                         rc = ordered_value_pretty( attr->a_desc,
2082                                                 &attr->a_vals[i], &pval, NULL );
2083
2084                                 } else {
2085                                         rc = ordered_value_validate( attr->a_desc,
2086                                                 &attr->a_vals[i], 0 );
2087                                 }
2088
2089                                 if ( rc ) {
2090                                         LBER_FREE( attr->a_vals[i].bv_val );
2091                                         if ( --last == i ) {
2092                                                 BER_BVZERO( &attr->a_vals[ i ] );
2093                                                 break;
2094                                         }
2095                                         attr->a_vals[i] = attr->a_vals[last];
2096                                         BER_BVZERO( &attr->a_vals[last] );
2097                                         i--;
2098                                         continue;
2099                                 }
2100
2101                                 if ( pretty ) {
2102                                         LBER_FREE( attr->a_vals[i].bv_val );
2103                                         attr->a_vals[i] = pval;
2104                                 }
2105                         }
2106
2107                         if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
2108                                 attr_free( attr );
2109                                 goto next_attr;
2110                         }
2111                 }
2112
2113                 if ( last && attr->a_desc->ad_type->sat_equality &&
2114                         attr->a_desc->ad_type->sat_equality->smr_normalize )
2115                 {
2116                         int i;
2117
2118                         attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
2119                         for ( i = 0; i<last; i++ ) {
2120                                 /* if normalizer fails, drop this value */
2121                                 if ( ordered_value_normalize(
2122                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2123                                         attr->a_desc,
2124                                         attr->a_desc->ad_type->sat_equality,
2125                                         &attr->a_vals[i], &attr->a_nvals[i],
2126                                         NULL )) {
2127                                         LBER_FREE( attr->a_vals[i].bv_val );
2128                                         if ( --last == i ) {
2129                                                 BER_BVZERO( &attr->a_vals[ i ] );
2130                                                 break;
2131                                         }
2132                                         attr->a_vals[i] = attr->a_vals[last];
2133                                         BER_BVZERO( &attr->a_vals[last] );
2134                                         i--;
2135                                 }
2136                         }
2137                         BER_BVZERO( &attr->a_nvals[i] );
2138                         if ( last == 0 ) {
2139                                 attr_free( attr );
2140                                 goto next_attr;
2141                         }
2142
2143                 } else {
2144                         attr->a_nvals = attr->a_vals;
2145                 }
2146
2147                 attr->a_numvals = last;
2148                 *attrp = attr;
2149                 attrp = &attr->a_next;
2150 next_attr:;
2151         }
2152
2153         /* only check if some mapping occurred */
2154         if ( check_duplicate_attrs ) {
2155                 Attribute       **ap;
2156
2157                 for ( ap = &ent.e_attrs; *ap != NULL; ap = &(*ap)->a_next ) {
2158                         Attribute       **tap;
2159
2160                         for ( tap = &(*ap)->a_next; *tap != NULL; ) {
2161                                 if ( (*tap)->a_desc == (*ap)->a_desc ) {
2162                                         Entry           e = { 0 };
2163                                         Modification    mod = { 0 };
2164                                         const char      *text = NULL;
2165                                         char            textbuf[ SLAP_TEXT_BUFLEN ];
2166                                         Attribute       *next = (*tap)->a_next;
2167
2168                                         BER_BVSTR( &e.e_name, "" );
2169                                         BER_BVSTR( &e.e_nname, "" );
2170                                         e.e_attrs = *ap;
2171                                         mod.sm_op = LDAP_MOD_ADD;
2172                                         mod.sm_desc = (*ap)->a_desc;
2173                                         mod.sm_type = mod.sm_desc->ad_cname;
2174                                         mod.sm_numvals = (*ap)->a_numvals;
2175                                         mod.sm_values = (*tap)->a_vals;
2176                                         if ( (*tap)->a_nvals != (*tap)->a_vals ) {
2177                                                 mod.sm_nvalues = (*tap)->a_nvals;
2178                                         }
2179
2180                                         (void)modify_add_values( &e, &mod,
2181                                                 /* permissive */ 1,
2182                                                 &text, textbuf, sizeof( textbuf ) );
2183
2184                                         /* should not insert new attrs! */
2185                                         assert( e.e_attrs == *ap );
2186
2187                                         attr_free( *tap );
2188                                         *tap = next;
2189
2190                                 } else {
2191                                         tap = &(*tap)->a_next;
2192                                 }
2193                         }
2194                 }
2195         }
2196
2197         /* Check for sorted attributes */
2198         if ( check_sorted_attrs ) {
2199                 for ( attr = ent.e_attrs; attr; attr = attr->a_next ) {
2200                         if ( attr->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
2201                                 while ( attr->a_numvals > 1 ) {
2202                                         int i;
2203                                         int rc = slap_sort_vals( (Modifications *)attr, &text, &i, op->o_tmpmemctx );
2204                                         if ( rc != LDAP_TYPE_OR_VALUE_EXISTS )
2205                                                 break;
2206
2207                                         /* Strip duplicate values */
2208                                         if ( attr->a_nvals != attr->a_vals )
2209                                                 LBER_FREE( attr->a_nvals[i].bv_val );
2210                                         LBER_FREE( attr->a_vals[i].bv_val );
2211                                         attr->a_numvals--;
2212                                         if ( i < attr->a_numvals ) {
2213                                                 attr->a_vals[i] = attr->a_vals[attr->a_numvals];
2214                                                 if ( attr->a_nvals != attr->a_vals )
2215                                                         attr->a_nvals[i] = attr->a_nvals[attr->a_numvals];
2216                                         }
2217                                         BER_BVZERO(&attr->a_vals[attr->a_numvals]);
2218                                         if ( attr->a_nvals != attr->a_vals )
2219                                                 BER_BVZERO(&attr->a_nvals[attr->a_numvals]);
2220                                 }
2221                                 attr->a_flags |= SLAP_ATTR_SORTED_VALS;
2222                         }
2223                 }
2224         }
2225
2226         ldap_get_entry_controls( mc->mc_conns[target].msc_ld,
2227                 e, &rs->sr_ctrls );
2228         rs->sr_entry = &ent;
2229         rs->sr_attrs = op->ors_attrs;
2230         rs->sr_operational_attrs = NULL;
2231         rs->sr_flags = mi->mi_targets[ target ]->mt_rep_flags;
2232         rs->sr_err = LDAP_SUCCESS;
2233         rc = send_search_entry( op, rs );
2234         switch ( rc ) {
2235         case LDAP_UNAVAILABLE:
2236                 rc = LDAP_OTHER;
2237                 break;
2238         }
2239
2240 done:;
2241         rs->sr_entry = NULL;
2242         rs->sr_attrs = NULL;
2243         if ( rs->sr_ctrls != NULL ) {
2244                 ldap_controls_free( rs->sr_ctrls );
2245                 rs->sr_ctrls = NULL;
2246         }
2247         if ( !BER_BVISNULL( &ent.e_name ) ) {
2248                 free( ent.e_name.bv_val );
2249                 BER_BVZERO( &ent.e_name );
2250         }
2251         if ( !BER_BVISNULL( &ent.e_nname ) ) {
2252                 free( ent.e_nname.bv_val );
2253                 BER_BVZERO( &ent.e_nname );
2254         }
2255         entry_clean( &ent );
2256
2257         return rc;
2258 }
2259