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