]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/search.c
For ITS#5421: Remove ldap-int.h dependencies.
[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         /*
704          * controls are set in ldap_back_dobind()
705          * 
706          * FIXME: in case of values return filter, we might want
707          * to map attrs and maybe rewrite value
708          */
709 getconn:;
710         mc = meta_back_getconn( op, rs, NULL, sendok );
711         if ( !mc ) {
712                 return rs->sr_err;
713         }
714
715         dc.conn = op->o_conn;
716         dc.rs = rs;
717
718         if ( candidates == NULL ) candidates = meta_back_candidates_get( op );
719         /*
720          * Inits searches
721          */
722         for ( i = 0; i < mi->mi_ntargets; i++ ) {
723                 /* reset sr_msgid; it is used in most loops
724                  * to check if that target is still to be considered */
725                 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
726
727                 /* a target is marked as candidate by meta_back_getconn();
728                  * if for any reason (an error, it's over or so) it is
729                  * no longer active, sr_msgid is set to META_MSGID_IGNORE
730                  * but it remains candidate, which means it has been active
731                  * at some point during the operation.  This allows to 
732                  * use its response code and more to compute the final
733                  * response */
734                 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
735                         continue;
736                 }
737
738                 candidates[ i ].sr_matched = NULL;
739                 candidates[ i ].sr_text = NULL;
740                 candidates[ i ].sr_ref = NULL;
741                 candidates[ i ].sr_ctrls = NULL;
742
743                 /* get largest timeout among candidates */
744                 if ( mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ]
745                         && mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ] > timeout )
746                 {
747                         timeout = mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ];
748                 }
749         }
750
751         for ( i = 0; i < mi->mi_ntargets; i++ ) {
752                 if ( !META_IS_CANDIDATE( &candidates[ i ] )
753                         || candidates[ i ].sr_err != LDAP_SUCCESS )
754                 {
755                         continue;
756                 }
757
758                 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
759                 {
760                 case META_SEARCH_NOT_CANDIDATE:
761                         candidates[ i ].sr_msgid = META_MSGID_IGNORE;
762                         break;
763
764                 case META_SEARCH_NEED_BIND:
765                         ++needbind;
766                         /* fallthru */
767
768                 case META_SEARCH_CONNECTING:
769                 case META_SEARCH_CANDIDATE:
770                 case META_SEARCH_BINDING:
771                         candidates[ i ].sr_type = REP_INTERMEDIATE;
772                         ++ncandidates;
773                         break;
774
775                 case META_SEARCH_ERR:
776                         savepriv = op->o_private;
777                         op->o_private = (void *)i;
778                         send_ldap_result( op, rs );
779                         op->o_private = savepriv;
780                         rc = -1;
781                         goto finish;
782
783                 default:
784                         assert( 0 );
785                         break;
786                 }
787         }
788
789         if ( ncandidates > 0 && needbind == ncandidates ) {
790                 /*
791                  * give up the second time...
792                  *
793                  * NOTE: this should not occur the second time, since a fresh
794                  * connection has ben created; however, targets may also
795                  * need bind because the bind timed out or so.
796                  */
797                 if ( sendok & LDAP_BACK_BINDING ) {
798                         Debug( LDAP_DEBUG_ANY,
799                                 "%s meta_back_search: unable to initialize conn\n",
800                                 op->o_log_prefix, 0, 0 );
801                         rs->sr_err = LDAP_UNAVAILABLE;
802                         rs->sr_text = "unable to initialize connection to remote targets";
803                         send_ldap_result( op, rs );
804                         rc = -1;
805                         goto finish;
806                 }
807
808                 /* FIXME: better create a separate connection? */
809                 sendok |= LDAP_BACK_BINDING;
810
811 #ifdef DEBUG_205
812                 Debug( LDAP_DEBUG_ANY, "*** %s drop mc=%p create new connection\n",
813                         op->o_log_prefix, (void *)mc, 0 );
814 #endif /* DEBUG_205 */
815
816                 meta_back_release_conn( mi, mc );
817                 mc = NULL;
818
819                 needbind = 0;
820                 ncandidates = 0;
821
822                 goto getconn;
823         }
824
825         initial_candidates = ncandidates;
826
827         if ( LogTest( LDAP_DEBUG_TRACE ) ) {
828                 char    cnd[ SLAP_TEXT_BUFLEN ];
829                 int     c;
830
831                 for ( c = 0; c < mi->mi_ntargets; c++ ) {
832                         if ( META_IS_CANDIDATE( &candidates[ c ] ) ) {
833                                 cnd[ c ] = '*';
834                         } else {
835                                 cnd[ c ] = ' ';
836                         }
837                 }
838                 cnd[ c ] = '\0';
839
840                 Debug( LDAP_DEBUG_TRACE, "%s meta_back_search: ncandidates=%d "
841                         "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
842         }
843
844         if ( initial_candidates == 0 ) {
845                 /* NOTE: here we are not sending any matchedDN;
846                  * this is intended, because if the back-meta
847                  * is serving this search request, but no valid
848                  * candidate could be looked up, it means that
849                  * there is a hole in the mapping of the targets
850                  * and thus no knowledge of any remote superior
851                  * is available */
852                 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: "
853                         "base=\"%s\" scope=%d: "
854                         "no candidate could be selected\n",
855                         op->o_log_prefix, op->o_req_dn.bv_val,
856                         op->ors_scope );
857
858                 /* FIXME: we're sending the first error we encounter;
859                  * maybe we should pick the worst... */
860                 rc = LDAP_NO_SUCH_OBJECT;
861                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
862                         if ( META_IS_CANDIDATE( &candidates[ i ] )
863                                 && candidates[ i ].sr_err != LDAP_SUCCESS )
864                         {
865                                 rc = candidates[ i ].sr_err;
866                                 break;
867                         }
868                 }
869
870                 send_ldap_error( op, rs, rc, NULL );
871
872                 goto finish;
873         }
874
875         /* We pull apart the ber result, stuff it into a slapd entry, and
876          * let send_search_entry stuff it back into ber format. Slow & ugly,
877          * but this is necessary for version matching, and for ACL processing.
878          */
879
880         if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
881                 stoptime = op->o_time + op->ors_tlimit;
882         }
883
884         /*
885          * In case there are no candidates, no cycle takes place...
886          *
887          * FIXME: we might use a queue, to better balance the load 
888          * among the candidates
889          */
890         for ( rc = 0; ncandidates > 0; ) {
891                 int     gotit = 0,
892                         doabandon = 0,
893                         alreadybound = ncandidates;
894
895                 /* check timeout */
896                 if ( timeout && lastres_time > 0
897                         && ( slap_get_time() - lastres_time ) > timeout )
898                 {
899                         doabandon = 1;
900                         rs->sr_text = "Operation timed out";
901                         rc = rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
902                                 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
903                         savepriv = op->o_private;
904                         op->o_private = (void *)i;
905                         send_ldap_result( op, rs );
906                         op->o_private = savepriv;
907                         goto finish;
908                 }
909
910                 /* check time limit */
911                 if ( op->ors_tlimit != SLAP_NO_LIMIT
912                                 && slap_get_time() > stoptime )
913                 {
914                         doabandon = 1;
915                         rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
916                         savepriv = op->o_private;
917                         op->o_private = (void *)i;
918                         send_ldap_result( op, rs );
919                         op->o_private = savepriv;
920                         goto finish;
921                 }
922
923                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
924                         meta_search_candidate_t retcode = META_SEARCH_UNDEFINED;
925                         metasingleconn_t        *msc = &mc->mc_conns[ i ];
926                         LDAPMessage             *res = NULL, *msg;
927
928                         /* if msgid is invalid, don't ldap_result() */
929                         if ( candidates[ i ].sr_msgid == META_MSGID_IGNORE ) {
930                                 continue;
931                         }
932
933                         /* if target still needs bind, retry */
934                         if ( candidates[ i ].sr_msgid == META_MSGID_NEED_BIND
935                                 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
936                         {
937                                 /* initiate dobind */
938                                 retcode = meta_search_dobind_init( op, rs, &mc, i, candidates );
939
940                                 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%ld]=%d\n",
941                                         op->o_log_prefix, i, retcode );
942
943                                 switch ( retcode ) {
944                                 case META_SEARCH_NEED_BIND:
945                                         alreadybound--;
946                                         /* fallthru */
947
948                                 case META_SEARCH_CONNECTING:
949                                 case META_SEARCH_BINDING:
950                                         break;
951
952                                 case META_SEARCH_ERR:
953                                         candidates[ i ].sr_err = rs->sr_err;
954                                         if ( META_BACK_ONERR_STOP( mi ) ) {
955                                                 savepriv = op->o_private;
956                                                 op->o_private = (void *)i;
957                                                 send_ldap_result( op, rs );
958                                                 op->o_private = savepriv;
959                                                 goto finish;
960                                         }
961                                         /* fallthru */
962
963                                 case META_SEARCH_NOT_CANDIDATE:
964                                         /*
965                                          * When no candidates are left,
966                                          * the outer cycle finishes
967                                          */
968                                         candidates[ i ].sr_msgid = META_MSGID_IGNORE;
969                                         assert( ncandidates > 0 );
970                                         --ncandidates;
971                                         break;
972
973                                 case META_SEARCH_CANDIDATE:
974                                         candidates[ i ].sr_msgid = META_MSGID_IGNORE;
975                                         switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
976                                         {
977                                         case META_SEARCH_CANDIDATE:
978                                                 assert( candidates[ i ].sr_msgid >= 0 );
979                                                 break;
980
981                                         case META_SEARCH_ERR:
982                                                 candidates[ i ].sr_err = rs->sr_err;
983                                                 if ( META_BACK_ONERR_STOP( mi ) ) {
984                                                         savepriv = op->o_private;
985                                                         op->o_private = (void *)i;
986                                                         send_ldap_result( op, rs );
987                                                         op->o_private = savepriv;
988                                                         goto finish;
989                                                 }
990                                                 /* fallthru */
991
992                                         case META_SEARCH_NOT_CANDIDATE:
993                                                 /* means that meta_back_search_start()
994                                                  * failed but onerr == continue */
995                                                 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
996                                                 assert( ncandidates > 0 );
997                                                 --ncandidates;
998                                                 break;
999
1000                                         default:
1001                                                 /* impossible */
1002                                                 assert( 0 );
1003                                                 break;
1004                                         }
1005                                         break;
1006
1007                                 default:
1008                                         /* impossible */
1009                                         assert( 0 );
1010                                         break;
1011                                 }
1012                                 continue;
1013                         }
1014
1015                         /* check for abandon */
1016                         if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( mc ) ) {
1017                                 break;
1018                         }
1019
1020 #ifdef DEBUG_205
1021                         if ( msc->msc_ld == NULL ) {
1022                                 char    buf[ SLAP_TEXT_BUFLEN ];
1023
1024                                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1025                                 snprintf( buf, sizeof( buf ),
1026                                         "%s meta_back_search[%ld] mc=%p msgid=%d%s%s%s\n",
1027                                         op->o_log_prefix, (long)i, (void *)mc,
1028                                         candidates[ i ].sr_msgid,
1029                                         META_IS_BINDING( &candidates[ i ] ) ? " binding" : "",
1030                                         LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ? " connbinding" : "",
1031                                         META_BACK_CONN_CREATING( &mc->mc_conns[ i ] ) ? " conncreating" : "" );
1032                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1033                                         
1034                                 Debug( LDAP_DEBUG_ANY, "!!! %s\n", buf, 0, 0 );
1035                         }
1036 #endif /* DEBUG_205 */
1037                         
1038                         /*
1039                          * FIXME: handle time limit as well?
1040                          * Note that target servers are likely 
1041                          * to handle it, so at some time we'll
1042                          * get a LDAP_TIMELIMIT_EXCEEDED from
1043                          * one of them ...
1044                          */
1045                         tv = save_tv;
1046                         rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
1047                                         LDAP_MSG_RECEIVED, &tv, &res );
1048                         switch ( rc ) {
1049                         case 0:
1050                                 /* FIXME: res should not need to be freed */
1051                                 assert( res == NULL );
1052                                 continue;
1053
1054                         case -1:
1055 really_bad:;
1056                                 /* something REALLY bad happened! */
1057                                 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1058                                         candidates[ i ].sr_type = REP_RESULT;
1059
1060                                         if ( meta_back_retry( op, rs, &mc, i, LDAP_BACK_DONTSEND ) ) {
1061                                                 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1062                                                 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
1063                                                 {
1064                                                         /* means that failed but onerr == continue */
1065                                                 case META_SEARCH_NOT_CANDIDATE:
1066                                                         candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1067
1068                                                         assert( ncandidates > 0 );
1069                                                         --ncandidates;
1070
1071                                                         candidates[ i ].sr_err = rs->sr_err;
1072                                                         if ( META_BACK_ONERR_STOP( mi ) ) {
1073                                                                 savepriv = op->o_private;
1074                                                                 op->o_private = (void *)i;
1075                                                                 send_ldap_result( op, rs );
1076                                                                 op->o_private = savepriv;
1077                                                                 goto finish;
1078                                                         }
1079                                                         /* fall thru */
1080
1081                                                 case META_SEARCH_CANDIDATE:
1082                                                         /* get back into business... */
1083                                                         continue;
1084
1085                                                 case META_SEARCH_BINDING:
1086                                                 case META_SEARCH_CONNECTING:
1087                                                 case META_SEARCH_NEED_BIND:
1088                                                 case META_SEARCH_UNDEFINED:
1089                                                         assert( 0 );
1090
1091                                                 default:
1092                                                         /* unrecoverable error */
1093                                                         candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1094                                                         rc = rs->sr_err = LDAP_OTHER;
1095                                                         goto finish;
1096                                                 }
1097                                         }
1098
1099                                         candidates[ i ].sr_err = rs->sr_err;
1100                                         if ( META_BACK_ONERR_STOP( mi ) ) {
1101                                                 savepriv = op->o_private;
1102                                                 op->o_private = (void *)i;
1103                                                 send_ldap_result( op, rs );
1104                                                 op->o_private = savepriv;
1105                                                 goto finish;
1106                                         }
1107                                 }
1108
1109                                 /*
1110                                  * When no candidates are left,
1111                                  * the outer cycle finishes
1112                                  */
1113                                 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1114                                 assert( ncandidates > 0 );
1115                                 --ncandidates;
1116                                 rs->sr_err = candidates[ i ].sr_err;
1117                                 continue;
1118
1119                         default:
1120                                 lastres_time = slap_get_time();
1121
1122                                 /* only touch when activity actually took place... */
1123                                 if ( mi->mi_idle_timeout != 0 && msc->msc_time < lastres_time ) {
1124                                         msc->msc_time = lastres_time;
1125                                 }
1126                                 break;
1127                         }
1128
1129                         for ( msg = ldap_first_message( msc->msc_ld, res );
1130                                 msg != NULL;
1131                                 msg = ldap_next_message( msc->msc_ld, msg ) )
1132                         {
1133                                 rc = ldap_msgtype( msg );
1134                                 if ( rc == LDAP_RES_SEARCH_ENTRY ) {
1135                                         LDAPMessage     *e;
1136
1137                                         if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1138                                                 /* don't retry any more... */
1139                                                 candidates[ i ].sr_type = REP_RESULT;
1140                                         }
1141
1142                                         is_ok++;
1143
1144                                         e = ldap_first_entry( msc->msc_ld, msg );
1145                                         savepriv = op->o_private;
1146                                         op->o_private = (void *)i;
1147                                         rs->sr_err = meta_send_entry( op, rs, mc, i, e );
1148
1149                                         switch ( rs->sr_err ) {
1150                                         case LDAP_SIZELIMIT_EXCEEDED:
1151                                                 savepriv = op->o_private;
1152                                                 op->o_private = (void *)i;
1153                                                 send_ldap_result( op, rs );
1154                                                 op->o_private = savepriv;
1155                                                 rs->sr_err = LDAP_SUCCESS;
1156                                                 ldap_msgfree( res );
1157                                                 res = NULL;
1158                                                 goto finish;
1159
1160                                         case LDAP_UNAVAILABLE:
1161                                                 rs->sr_err = LDAP_OTHER;
1162                                                 ldap_msgfree( res );
1163                                                 res = NULL;
1164                                                 goto finish;
1165                                         }
1166                                         op->o_private = savepriv;
1167
1168                                         /* don't wait any longer... */
1169                                         gotit = 1;
1170                                         save_tv.tv_sec = 0;
1171                                         save_tv.tv_usec = 0;
1172
1173                                 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
1174                                         char            **references = NULL;
1175                                         int             cnt;
1176
1177                                         if ( META_BACK_TGT_NOREFS( mi->mi_targets[ i ] ) ) {
1178                                                 continue;
1179                                         }
1180
1181                                         if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1182                                                 /* don't retry any more... */
1183                                                 candidates[ i ].sr_type = REP_RESULT;
1184                                         }
1185         
1186                                         is_ok++;
1187         
1188                                         rc = ldap_parse_reference( msc->msc_ld, msg,
1189                                                         &references, &rs->sr_ctrls, 0 );
1190         
1191                                         if ( rc != LDAP_SUCCESS ) {
1192                                                 continue;
1193                                         }
1194         
1195                                         if ( references == NULL ) {
1196                                                 continue;
1197                                         }
1198
1199 #ifdef ENABLE_REWRITE
1200                                         dc.ctx = "referralDN";
1201 #else /* ! ENABLE_REWRITE */
1202                                         dc.tofrom = 0;
1203                                         dc.normalized = 0;
1204 #endif /* ! ENABLE_REWRITE */
1205
1206                                         /* FIXME: merge all and return at the end */
1207         
1208                                         for ( cnt = 0; references[ cnt ]; cnt++ )
1209                                                 ;
1210         
1211                                         rs->sr_ref = ber_memalloc_x( sizeof( struct berval ) * ( cnt + 1 ),
1212                                                 op->o_tmpmemctx );
1213         
1214                                         for ( cnt = 0; references[ cnt ]; cnt++ ) {
1215                                                 ber_str2bv_x( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ],
1216                                                 op->o_tmpmemctx );
1217                                         }
1218                                         BER_BVZERO( &rs->sr_ref[ cnt ] );
1219         
1220                                         ( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref,
1221                                                 op->o_tmpmemctx );
1222
1223                                         if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
1224                                                 /* ignore return value by now */
1225                                                 savepriv = op->o_private;
1226                                                 op->o_private = (void *)i;
1227                                                 ( void )send_search_reference( op, rs );
1228                                                 op->o_private = savepriv;
1229         
1230                                                 ber_bvarray_free_x( rs->sr_ref, op->o_tmpmemctx );
1231                                                 rs->sr_ref = NULL;
1232                                         }
1233
1234                                         /* cleanup */
1235                                         if ( references ) {
1236                                                 ber_memvfree( (void **)references );
1237                                         }
1238
1239                                         if ( rs->sr_ctrls ) {
1240                                                 ldap_controls_free( rs->sr_ctrls );
1241                                                 rs->sr_ctrls = NULL;
1242                                         }
1243
1244                                 } else if ( rc == LDAP_RES_INTERMEDIATE ) {
1245                                         if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1246                                                 /* don't retry any more... */
1247                                                 candidates[ i ].sr_type = REP_RESULT;
1248                                         }
1249         
1250                                         /* FIXME: response controls
1251                                          * are passed without checks */
1252                                         rs->sr_err = ldap_parse_intermediate( msc->msc_ld,
1253                                                 msg,
1254                                                 (char **)&rs->sr_rspoid,
1255                                                 &rs->sr_rspdata,
1256                                                 &rs->sr_ctrls,
1257                                                 0 );
1258                                         if ( rs->sr_err != LDAP_SUCCESS ) {
1259                                                 candidates[ i ].sr_type = REP_RESULT;
1260                                                 ldap_msgfree( res );
1261                                                 res = NULL;
1262                                                 goto really_bad;
1263                                         }
1264
1265                                         slap_send_ldap_intermediate( op, rs );
1266
1267                                         if ( rs->sr_rspoid != NULL ) {
1268                                                 ber_memfree( (char *)rs->sr_rspoid );
1269                                                 rs->sr_rspoid = NULL;
1270                                         }
1271
1272                                         if ( rs->sr_rspdata != NULL ) {
1273                                                 ber_bvfree( rs->sr_rspdata );
1274                                                 rs->sr_rspdata = NULL;
1275                                         }
1276
1277                                         if ( rs->sr_ctrls != NULL ) {
1278                                                 ldap_controls_free( rs->sr_ctrls );
1279                                                 rs->sr_ctrls = NULL;
1280                                         }
1281
1282                                 } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
1283                                         char            buf[ SLAP_TEXT_BUFLEN ];
1284                                         char            **references = NULL;
1285
1286                                         if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1287                                                 /* don't retry any more... */
1288                                                 candidates[ i ].sr_type = REP_RESULT;
1289                                         }
1290         
1291                                         candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1292
1293                                         /* NOTE: ignores response controls
1294                                          * (and intermediate response controls
1295                                          * as well, except for those with search
1296                                          * references); this may not be correct,
1297                                          * but if they're not ignored then
1298                                          * back-meta would need to merge them
1299                                          * consistently (think of pagedResults...)
1300                                          */
1301                                         /* FIXME: response controls? */
1302                                         rs->sr_err = ldap_parse_result( msc->msc_ld,
1303                                                 msg,
1304                                                 &candidates[ i ].sr_err,
1305                                                 (char **)&candidates[ i ].sr_matched,
1306                                                 (char **)&candidates[ i ].sr_text,
1307                                                 &references,
1308                                                 NULL /* &candidates[ i ].sr_ctrls (unused) */ ,
1309                                                 0 );
1310                                         if ( rs->sr_err != LDAP_SUCCESS ) {
1311                                                 candidates[ i ].sr_err = rs->sr_err;
1312                                                 sres = slap_map_api2result( &candidates[ i ] );
1313                                                 candidates[ i ].sr_type = REP_RESULT;
1314                                                 ldap_msgfree( res );
1315                                                 res = NULL;
1316                                                 goto really_bad;
1317                                         }
1318
1319                                         rs->sr_err = candidates[ i ].sr_err;
1320
1321                                         /* massage matchedDN if need be */
1322                                         if ( candidates[ i ].sr_matched != NULL ) {
1323                                                 struct berval   match, mmatch;
1324
1325                                                 ber_str2bv( candidates[ i ].sr_matched,
1326                                                         0, 0, &match );
1327                                                 candidates[ i ].sr_matched = NULL;
1328
1329                                                 dc.ctx = "matchedDN";
1330                                                 dc.target = mi->mi_targets[ i ];
1331                                                 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
1332                                                         if ( mmatch.bv_val == match.bv_val ) {
1333                                                                 candidates[ i ].sr_matched
1334                                                                         = ch_strdup( mmatch.bv_val );
1335
1336                                                         } else {
1337                                                                 candidates[ i ].sr_matched = mmatch.bv_val;
1338                                                         }
1339
1340                                                         candidate_match++;
1341                                                 } 
1342                                                 ldap_memfree( match.bv_val );
1343                                         }
1344
1345                                         /* add references to array */
1346                                         /* RFC 4511: referrals can only appear
1347                                          * if result code is LDAP_REFERRAL */
1348                                         if ( references != NULL
1349                                                 && references[ 0 ] != NULL
1350                                                 && references[ 0 ][ 0 ] != '\0' )
1351                                         {
1352                                                 if ( rs->sr_err != LDAP_REFERRAL ) {
1353                                                         Debug( LDAP_DEBUG_ANY,
1354                                                                 "%s meta_back_search[%ld]: "
1355                                                                 "got referrals with err=%d\n",
1356                                                                 op->o_log_prefix,
1357                                                                 i, rs->sr_err );
1358
1359                                                 } else {
1360                                                         BerVarray       sr_ref;
1361                                                         int             cnt;
1362         
1363                                                         for ( cnt = 0; references[ cnt ]; cnt++ )
1364                                                                 ;
1365         
1366                                                         sr_ref = ber_memalloc_x( sizeof( struct berval ) * ( cnt + 1 ),
1367                                                                 op->o_tmpmemctx );
1368         
1369                                                         for ( cnt = 0; references[ cnt ]; cnt++ ) {
1370                                                                 ber_str2bv_x( references[ cnt ], 0, 1, &sr_ref[ cnt ],
1371                                                                         op->o_tmpmemctx );
1372                                                         }
1373                                                         BER_BVZERO( &sr_ref[ cnt ] );
1374         
1375                                                         ( void )ldap_back_referral_result_rewrite( &dc, sr_ref,
1376                                                                 op->o_tmpmemctx );
1377                                         
1378                                                         if ( rs->sr_v2ref == NULL ) {
1379                                                                 rs->sr_v2ref = sr_ref;
1380
1381                                                         } else {
1382                                                                 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
1383                                                                         ber_bvarray_add_x( &rs->sr_v2ref, &sr_ref[ cnt ],
1384                                                                                 op->o_tmpmemctx );
1385                                                                 }
1386                                                                 ber_memfree_x( sr_ref, op->o_tmpmemctx );
1387                                                         }
1388                                                 }
1389
1390                                         } else if ( rs->sr_err == LDAP_REFERRAL ) {
1391                                                 Debug( LDAP_DEBUG_ANY,
1392                                                         "%s meta_back_search[%ld]: "
1393                                                         "got err=%d with null "
1394                                                         "or empty referrals\n",
1395                                                         op->o_log_prefix,
1396                                                         i, rs->sr_err );
1397
1398                                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1399                                         }
1400
1401                                         /* cleanup */
1402                                         ber_memvfree( (void **)references );
1403         
1404                                         sres = slap_map_api2result( rs );
1405         
1406                                         if ( LogTest( LDAP_DEBUG_TRACE | LDAP_DEBUG_ANY ) ) {
1407                                                 snprintf( buf, sizeof( buf ),
1408                                                         "%s meta_back_search[%ld] "
1409                                                         "match=\"%s\" err=%ld",
1410                                                         op->o_log_prefix, i,
1411                                                         candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
1412                                                         (long) candidates[ i ].sr_err );
1413                                                 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1414                                                         Debug( LDAP_DEBUG_TRACE, "%s.\n", buf, 0, 0 );
1415         
1416                                                 } else {
1417                                                         Debug( LDAP_DEBUG_ANY, "%s (%s).\n",
1418                                                                 buf, ldap_err2string( candidates[ i ].sr_err ), 0 );
1419                                                 }
1420                                         }
1421         
1422                                         switch ( sres ) {
1423                                         case LDAP_NO_SUCH_OBJECT:
1424                                                 /* is_ok is touched any time a valid
1425                                                  * (even intermediate) result is
1426                                                  * returned; as a consequence, if
1427                                                  * a candidate returns noSuchObject
1428                                                  * it is ignored and the candidate
1429                                                  * is simply demoted. */
1430                                                 if ( is_ok ) {
1431                                                         sres = LDAP_SUCCESS;
1432                                                 }
1433                                                 break;
1434         
1435                                         case LDAP_SUCCESS:
1436                                         case LDAP_REFERRAL:
1437                                                 is_ok++;
1438                                                 break;
1439         
1440                                         case LDAP_SIZELIMIT_EXCEEDED:
1441                                                 /* if a target returned sizelimitExceeded
1442                                                  * and the entry count is equal to the
1443                                                  * proxy's limit, the target would have
1444                                                  * returned more, and the error must be
1445                                                  * propagated to the client; otherwise,
1446                                                  * the target enforced a limit lower
1447                                                  * than what requested by the proxy;
1448                                                  * ignore it */
1449                                                 candidates[ i ].sr_err = rs->sr_err;
1450                                                 if ( rs->sr_nentries == op->ors_slimit
1451                                                         || META_BACK_ONERR_STOP( mi ) )
1452                                                 {
1453                                                         const char *save_text = rs->sr_text;
1454                                                         savepriv = op->o_private;
1455                                                         op->o_private = (void *)i;
1456                                                         rs->sr_text = candidates[ i ].sr_text;
1457                                                         send_ldap_result( op, rs );
1458                                                         rs->sr_text = save_text;
1459                                                         op->o_private = savepriv;
1460                                                         ldap_msgfree( res );
1461                                                         res = NULL;
1462                                                         goto finish;
1463                                                 }
1464                                                 break;
1465         
1466                                         default:
1467                                                 candidates[ i ].sr_err = rs->sr_err;
1468                                                 if ( META_BACK_ONERR_STOP( mi ) ) {
1469                                                         const char *save_text = rs->sr_text;
1470                                                         savepriv = op->o_private;
1471                                                         op->o_private = (void *)i;
1472                                                         rs->sr_text = candidates[ i ].sr_text;
1473                                                         send_ldap_result( op, rs );
1474                                                         rs->sr_text = save_text;
1475                                                         op->o_private = savepriv;
1476                                                         ldap_msgfree( res );
1477                                                         res = NULL;
1478                                                         goto finish;
1479                                                 }
1480                                                 break;
1481                                         }
1482         
1483                                         last = i;
1484                                         rc = 0;
1485         
1486                                         /*
1487                                          * When no candidates are left,
1488                                          * the outer cycle finishes
1489                                          */
1490                                         assert( ncandidates > 0 );
1491                                         --ncandidates;
1492
1493                                 } else if ( rc == LDAP_RES_BIND ) {
1494                                         meta_search_candidate_t retcode;
1495         
1496                                         retcode = meta_search_dobind_result( op, rs, &mc, i, candidates, msg );
1497                                         if ( retcode == META_SEARCH_CANDIDATE ) {
1498                                                 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1499                                                 retcode = meta_back_search_start( op, rs, &dc, &mc, i, candidates );
1500                                         }
1501         
1502                                         switch ( retcode ) {
1503                                         case META_SEARCH_CANDIDATE:
1504                                                 break;
1505         
1506                                                 /* means that failed but onerr == continue */
1507                                         case META_SEARCH_NOT_CANDIDATE:
1508                                         case META_SEARCH_ERR:
1509                                                 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1510                                                 assert( ncandidates > 0 );
1511                                                 --ncandidates;
1512         
1513                                                 candidates[ i ].sr_err = rs->sr_err;
1514                                                 if ( META_BACK_ONERR_STOP( mi ) ) {
1515                                                         savepriv = op->o_private;
1516                                                         op->o_private = (void *)i;
1517                                                         send_ldap_result( op, rs );
1518                                                         op->o_private = savepriv;
1519                                                         ldap_msgfree( res );
1520                                                         res = NULL;
1521                                                         goto finish;
1522                                                 }
1523                                                 goto free_message;
1524         
1525                                         default:
1526                                                 assert( 0 );
1527                                                 break;
1528                                         }
1529         
1530                                 } else {
1531                                         Debug( LDAP_DEBUG_ANY,
1532                                                 "%s meta_back_search[%ld]: "
1533                                                 "unrecognized response message tag=%d\n",
1534                                                 op->o_log_prefix,
1535                                                 i, rc );
1536                                 
1537                                         ldap_msgfree( res );
1538                                         res = NULL;
1539                                         goto really_bad;
1540                                 }
1541                         }
1542
1543 free_message:;
1544                         ldap_msgfree( res );
1545                         res = NULL;
1546                 }
1547
1548                 /* check for abandon */
1549                 if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( mc ) ) {
1550                         for ( i = 0; i < mi->mi_ntargets; i++ ) {
1551                                 if ( candidates[ i ].sr_msgid >= 0
1552                                         || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1553                                 {
1554                                         if ( META_IS_BINDING( &candidates[ i ] )
1555                                                 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1556                                         {
1557                                                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1558                                                 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] )
1559                                                         || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1560                                                 {
1561                                                         /* if still binding, destroy */
1562
1563 #ifdef DEBUG_205
1564                                                         char buf[ SLAP_TEXT_BUFLEN ];
1565
1566                                                         snprintf( buf, sizeof( buf), "%s meta_back_search(abandon) "
1567                                                                 "ldap_unbind_ext[%ld] mc=%p ld=%p",
1568                                                                 op->o_log_prefix, i, (void *)mc,
1569                                                                 (void *)mc->mc_conns[i].msc_ld );
1570
1571                                                         Debug( LDAP_DEBUG_ANY, "### %s\n", buf, 0, 0 );
1572 #endif /* DEBUG_205 */
1573
1574                                                         meta_clear_one_candidate( op, mc, i );
1575                                                 }
1576                                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1577                                                 META_BINDING_CLEAR( &candidates[ i ] );
1578                                                 
1579                                         } else {
1580                                                 (void)meta_back_cancel( mc, op, rs,
1581                                                         candidates[ i ].sr_msgid, i,
1582                                                         LDAP_BACK_DONTSEND );
1583                                         }
1584
1585                                         candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1586                                         assert( ncandidates > 0 );
1587                                         --ncandidates;
1588                                 }
1589                         }
1590
1591                         if ( op->o_abandon ) {
1592                                 rc = SLAPD_ABANDON;
1593                         }
1594
1595                         /* let send_ldap_result play cleanup handlers (ITS#4645) */
1596                         break;
1597                 }
1598
1599                 /* if no entry was found during this loop,
1600                  * set a minimal timeout */
1601                 if ( ncandidates > 0 && gotit == 0 ) {
1602                         if ( save_tv.tv_sec == 0 && save_tv.tv_usec == 0 ) {
1603                                 save_tv.tv_usec = LDAP_BACK_RESULT_UTIMEOUT/initial_candidates;
1604
1605                                 /* arbitrarily limit to something between 1 and 2 minutes */
1606                         } else if ( ( stoptime == -1 && save_tv.tv_sec < 60 )
1607                                 || save_tv.tv_sec < ( stoptime - slap_get_time() ) / ( 2 * ncandidates ) )
1608                         {
1609                                 /* double the timeout */
1610                                 lutil_timermul( &save_tv, 2, &save_tv );
1611                         }
1612
1613                         if ( alreadybound == 0 ) {
1614                                 tv = save_tv;
1615                                 (void)select( 0, NULL, NULL, NULL, &tv );
1616
1617                         } else {
1618                                 ldap_pvt_thread_yield();
1619                         }
1620                 }
1621         }
1622
1623         if ( rc == -1 ) {
1624                 /*
1625                  * FIXME: need a better strategy to handle errors
1626                  */
1627                 if ( mc ) {
1628                         rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE,
1629                                 -1, stoptime != -1 ? (stoptime - slap_get_time()) : 0,
1630                                 LDAP_BACK_SENDERR );
1631                 } else {
1632                         rc = rs->sr_err;
1633                 }
1634                 goto finish;
1635         }
1636
1637         /*
1638          * Rewrite the matched portion of the search base, if required
1639          * 
1640          * FIXME: only the last one gets caught!
1641          */
1642         savepriv = op->o_private;
1643         op->o_private = (void *)(long)mi->mi_ntargets;
1644         if ( candidate_match > 0 ) {
1645                 struct berval   pmatched = BER_BVNULL;
1646
1647                 /* we use the first one */
1648                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1649                         if ( META_IS_CANDIDATE( &candidates[ i ] )
1650                                         && candidates[ i ].sr_matched != NULL )
1651                         {
1652                                 struct berval   bv, pbv;
1653                                 int             rc;
1654
1655                                 /* if we got success, and this target
1656                                  * returned noSuchObject, and its suffix
1657                                  * is a superior of the searchBase,
1658                                  * ignore the matchedDN */
1659                                 if ( sres == LDAP_SUCCESS
1660                                         && candidates[ i ].sr_err == LDAP_NO_SUCH_OBJECT
1661                                         && op->o_req_ndn.bv_len > mi->mi_targets[ i ]->mt_nsuffix.bv_len )
1662                                 {
1663                                         free( (char *)candidates[ i ].sr_matched );
1664                                         candidates[ i ].sr_matched = NULL;
1665                                         continue;
1666                                 }
1667
1668                                 ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
1669                                 rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
1670
1671                                 if ( rc == LDAP_SUCCESS ) {
1672
1673                                         /* NOTE: if they all are superiors
1674                                          * of the baseDN, the shorter is also 
1675                                          * superior of the longer... */
1676                                         if ( pbv.bv_len > pmatched.bv_len ) {
1677                                                 if ( !BER_BVISNULL( &pmatched ) ) {
1678                                                         op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
1679                                                 }
1680                                                 pmatched = pbv;
1681                                                 op->o_private = (void *)i;
1682
1683                                         } else {
1684                                                 op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
1685                                         }
1686                                 }
1687
1688                                 if ( candidates[ i ].sr_matched != NULL ) {
1689                                         free( (char *)candidates[ i ].sr_matched );
1690                                         candidates[ i ].sr_matched = NULL;
1691                                 }
1692                         }
1693                 }
1694
1695                 if ( !BER_BVISNULL( &pmatched ) ) {
1696                         matched = pmatched.bv_val;
1697                 }
1698
1699         } else if ( sres == LDAP_NO_SUCH_OBJECT ) {
1700                 matched = op->o_bd->be_suffix[ 0 ].bv_val;
1701         }
1702
1703         /*
1704          * In case we returned at least one entry, we return LDAP_SUCCESS
1705          * otherwise, the latter error code we got
1706          */
1707
1708         if ( sres == LDAP_SUCCESS ) {
1709                 if ( rs->sr_v2ref ) {
1710                         sres = LDAP_REFERRAL;
1711                 }
1712
1713                 if ( META_BACK_ONERR_REPORT( mi ) ) {
1714                         /*
1715                          * Report errors, if any
1716                          *
1717                          * FIXME: we should handle error codes and return the more 
1718                          * important/reasonable
1719                          */
1720                         for ( i = 0; i < mi->mi_ntargets; i++ ) {
1721                                 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1722                                         continue;
1723                                 }
1724
1725                                 if ( candidates[ i ].sr_err != LDAP_SUCCESS
1726                                         && candidates[ i ].sr_err != LDAP_NO_SUCH_OBJECT )
1727                                 {
1728                                         sres = candidates[ i ].sr_err;
1729                                         break;
1730                                 }
1731                         }
1732                 }
1733         }
1734
1735         rs->sr_err = sres;
1736         rs->sr_matched = matched;
1737         rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
1738         send_ldap_result( op, rs );
1739         op->o_private = savepriv;
1740         rs->sr_matched = NULL;
1741         rs->sr_ref = NULL;
1742
1743 finish:;
1744         if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
1745                 op->o_tmpfree( matched, op->o_tmpmemctx );
1746         }
1747
1748         if ( rs->sr_v2ref ) {
1749                 ber_bvarray_free_x( rs->sr_v2ref, op->o_tmpmemctx );
1750         }
1751
1752         for ( i = 0; i < mi->mi_ntargets; i++ ) {
1753                 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1754                         continue;
1755                 }
1756
1757                 if ( mc ) {
1758                         if ( META_IS_BINDING( &candidates[ i ] )
1759                                 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1760                         {
1761                                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1762                                 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] )
1763                                         || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1764                                 {
1765                                         assert( candidates[ i ].sr_msgid >= 0
1766                                                 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING );
1767                                         assert( mc->mc_conns[ i ].msc_ld != NULL );
1768
1769 #ifdef DEBUG_205
1770                                         Debug( LDAP_DEBUG_ANY, "### %s meta_back_search(cleanup) "
1771                                                 "ldap_unbind_ext[%ld] ld=%p\n",
1772                                                 op->o_log_prefix, i, (void *)mc->mc_conns[i].msc_ld );
1773 #endif /* DEBUG_205 */
1774
1775                                         /* if still binding, destroy */
1776                                         meta_clear_one_candidate( op, mc, i );
1777                                 }
1778                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1779                                 META_BINDING_CLEAR( &candidates[ i ] );
1780
1781                         } else if ( candidates[ i ].sr_msgid >= 0 ) {
1782                                 (void)meta_back_cancel( mc, op, rs,
1783                                         candidates[ i ].sr_msgid, i,
1784                                         LDAP_BACK_DONTSEND );
1785                         }
1786                 }
1787
1788                 if ( candidates[ i ].sr_matched ) {
1789                         free( (char *)candidates[ i ].sr_matched );
1790                         candidates[ i ].sr_matched = NULL;
1791                 }
1792
1793                 if ( candidates[ i ].sr_text ) {
1794                         ldap_memfree( (char *)candidates[ i ].sr_text );
1795                         candidates[ i ].sr_text = NULL;
1796                 }
1797
1798                 if ( candidates[ i ].sr_ref ) {
1799                         ber_bvarray_free( candidates[ i ].sr_ref );
1800                         candidates[ i ].sr_ref = NULL;
1801                 }
1802
1803                 if ( candidates[ i ].sr_ctrls ) {
1804                         ldap_controls_free( candidates[ i ].sr_ctrls );
1805                         candidates[ i ].sr_ctrls = NULL;
1806                 }
1807
1808                 if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ i ] ) ) {
1809                         meta_back_quarantine( op, &candidates[ i ], i );
1810                 }
1811
1812                 /* only in case of timelimit exceeded, if the timelimit exceeded because
1813                  * one contacted target never responded, invalidate the connection
1814                  * NOTE: should we quarantine the target as well?  right now, the connection
1815                  * is invalidated; the next time it will be recreated and the target
1816                  * will be quarantined if it cannot be contacted */
1817                 if ( mi->mi_idle_timeout != 0
1818                         && rs->sr_err == LDAP_TIMELIMIT_EXCEEDED
1819                         && op->o_time > mc->mc_conns[ i ].msc_time )
1820                 {
1821                         /* don't let anyone else use this expired connection */
1822                         do_taint++;
1823                 }
1824         }
1825
1826         if ( mc ) {
1827                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1828                 if ( do_taint ) {
1829                         LDAP_BACK_CONN_TAINTED_SET( mc );
1830                 }
1831                 meta_back_release_conn_lock( mi, mc, 0 );
1832                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1833         }
1834
1835         return rs->sr_err;
1836 }
1837
1838 static int
1839 meta_send_entry(
1840         Operation       *op,
1841         SlapReply       *rs,
1842         metaconn_t      *mc,
1843         int             target,
1844         LDAPMessage     *e )
1845 {
1846         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
1847         struct berval           a, mapped;
1848         int                     check_duplicate_attrs = 0;
1849         int                     check_sorted_attrs = 0;
1850         Entry                   ent = { 0 };
1851         BerElement              ber = *ldap_get_message_ber( e );
1852         Attribute               *attr, **attrp;
1853         struct berval           bdn,
1854                                 dn = BER_BVNULL;
1855         const char              *text;
1856         dncookie                dc;
1857         ber_len_t               len;
1858         int                     rc;
1859
1860         if ( ber_scanf( &ber, "l{", &len ) == LBER_ERROR ) {
1861                 return LDAP_DECODING_ERROR;
1862         }
1863
1864         if ( ber_set_option( &ber, LBER_OPT_REMAINING_BYTES, &len ) != LBER_OPT_SUCCESS ) {
1865                 return LDAP_OTHER;
1866         }
1867
1868         if ( ber_scanf( &ber, "m{", &bdn ) == LBER_ERROR ) {
1869                 return LDAP_DECODING_ERROR;
1870         }
1871
1872         /*
1873          * Rewrite the dn of the result, if needed
1874          */
1875         dc.target = mi->mi_targets[ target ];
1876         dc.conn = op->o_conn;
1877         dc.rs = rs;
1878         dc.ctx = "searchResult";
1879
1880         rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
1881         if ( rs->sr_err != LDAP_SUCCESS) {
1882                 return rs->sr_err;
1883         }
1884
1885         /*
1886          * Note: this may fail if the target host(s) schema differs
1887          * from the one known to the meta, and a DN with unknown
1888          * attributes is returned.
1889          * 
1890          * FIXME: should we log anything, or delegate to dnNormalize?
1891          */
1892         rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
1893                 op->o_tmpmemctx );
1894         if ( dn.bv_val != bdn.bv_val ) {
1895                 free( dn.bv_val );
1896         }
1897         BER_BVZERO( &dn );
1898
1899         if ( rc != LDAP_SUCCESS ) {
1900                 Debug( LDAP_DEBUG_ANY,
1901                         "%s meta_send_entry(\"%s\"): "
1902                         "invalid DN syntax\n",
1903                         op->o_log_prefix, ent.e_name.bv_val, 0 );
1904                 rc = LDAP_INVALID_DN_SYNTAX;
1905                 goto done;
1906         }
1907
1908         /*
1909          * cache dn
1910          */
1911         if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1912                 ( void )meta_dncache_update_entry( &mi->mi_cache,
1913                                 &ent.e_nname, target );
1914         }
1915
1916         attrp = &ent.e_attrs;
1917
1918         dc.ctx = "searchAttrDN";
1919         while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
1920                 int                             last = 0;
1921                 slap_syntax_validate_func       *validate;
1922                 slap_syntax_transform_func      *pretty;
1923
1924                 if ( ber_pvt_ber_remaining( &ber ) < 0 ) {
1925                         Debug( LDAP_DEBUG_ANY,
1926                                 "%s meta_send_entry(\"%s\"): "
1927                                 "unable to parse attr \"%s\".\n",
1928                                 op->o_log_prefix, ent.e_name.bv_val, a.bv_val );
1929                                 
1930                         rc = LDAP_OTHER;
1931                         goto done;
1932                 }
1933
1934                 if ( ber_pvt_ber_remaining( &ber ) == 0 ) {
1935                         break;
1936                 }
1937
1938                 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_at, 
1939                                 &a, &mapped, BACKLDAP_REMAP );
1940                 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
1941                         ( void )ber_scanf( &ber, "x" /* [W] */ );
1942                         continue;
1943                 }
1944                 if ( mapped.bv_val != a.bv_val ) {
1945                         /* will need to check for duplicate attrs */
1946                         check_duplicate_attrs++;
1947                 }
1948                 attr = attr_alloc( NULL );
1949                 if ( attr == NULL ) {
1950                         rc = LDAP_OTHER;
1951                         goto done;
1952                 }
1953                 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
1954                                 != LDAP_SUCCESS) {
1955                         if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
1956                                 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
1957                         {
1958                                 char    buf[ SLAP_TEXT_BUFLEN ];
1959
1960                                 snprintf( buf, sizeof( buf ),
1961                                         "%s meta_send_entry(\"%s\"): "
1962                                         "slap_bv2undef_ad(%s): %s\n",
1963                                         op->o_log_prefix, ent.e_name.bv_val,
1964                                         mapped.bv_val, text );
1965
1966                                 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
1967                                 ( void )ber_scanf( &ber, "x" /* [W] */ );
1968                                 attr_free( attr );
1969                                 continue;
1970                         }
1971                 }
1972
1973                 if ( attr->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL )
1974                         check_sorted_attrs = 1;
1975
1976                 /* no subschemaSubentry */
1977                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
1978                         || attr->a_desc == slap_schema.si_ad_entryDN )
1979                 {
1980
1981                         /* 
1982                          * We eat target's subschemaSubentry because
1983                          * a search for this value is likely not
1984                          * to resolve to the appropriate backend;
1985                          * later, the local subschemaSubentry is
1986                          * added.
1987                          *
1988                          * We also eat entryDN because the frontend
1989                          * will reattach it without checking if already
1990                          * present...
1991                          */
1992                         ( void )ber_scanf( &ber, "x" /* [W] */ );
1993                         attr_free(attr);
1994                         continue;
1995                 }
1996
1997                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR 
1998                                 || attr->a_vals == NULL )
1999                 {
2000                         attr->a_vals = (struct berval *)&slap_dummy_bv;
2001
2002                 } else {
2003                         for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
2004                                 ;
2005                 }
2006                 attr->a_numvals = last;
2007
2008                 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
2009                 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
2010
2011                 if ( !validate && !pretty ) {
2012                         attr_free( attr );
2013                         goto next_attr;
2014                 }
2015
2016                 if ( attr->a_desc == slap_schema.si_ad_objectClass
2017                                 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
2018                 {
2019                         struct berval   *bv;
2020
2021                         for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
2022                                 ObjectClass *oc;
2023
2024                                 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_oc,
2025                                                 bv, &mapped, BACKLDAP_REMAP );
2026                                 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
2027 remove_oc:;
2028                                         free( bv->bv_val );
2029                                         BER_BVZERO( bv );
2030                                         if ( --last < 0 ) {
2031                                                 break;
2032                                         }
2033                                         *bv = attr->a_vals[ last ];
2034                                         BER_BVZERO( &attr->a_vals[ last ] );
2035                                         bv--;
2036
2037                                 } else if ( mapped.bv_val != bv->bv_val ) {
2038                                         int     i;
2039
2040                                         for ( i = 0; !BER_BVISNULL( &attr->a_vals[ i ] ); i++ ) {
2041                                                 if ( &attr->a_vals[ i ] == bv ) {
2042                                                         continue;
2043                                                 }
2044
2045                                                 if ( ber_bvstrcasecmp( &mapped, &attr->a_vals[ i ] ) == 0 ) {
2046                                                         break;
2047                                                 }
2048                                         }
2049
2050                                         if ( !BER_BVISNULL( &attr->a_vals[ i ] ) ) {
2051                                                 goto remove_oc;
2052                                         }
2053
2054                                         ber_bvreplace( bv, &mapped );
2055
2056                                 } else if ( ( oc = oc_bvfind_undef( bv ) ) == NULL ) {
2057                                         goto remove_oc;
2058
2059                                 } else {
2060                                         ber_bvreplace( bv, &oc->soc_cname );
2061                                 }
2062                         }
2063                 /*
2064                  * It is necessary to try to rewrite attributes with
2065                  * dn syntax because they might be used in ACLs as
2066                  * members of groups; since ACLs are applied to the
2067                  * rewritten stuff, no dn-based subecj clause could
2068                  * be used at the ldap backend side (see
2069                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
2070                  * The problem can be overcome by moving the dn-based
2071                  * ACLs to the target directory server, and letting
2072                  * everything pass thru the ldap backend.
2073                  */
2074                 } else {
2075                         int     i;
2076
2077                         if ( attr->a_desc->ad_type->sat_syntax ==
2078                                 slap_schema.si_syn_distinguishedName )
2079                         {
2080                                 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
2081
2082                         } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
2083                                 ldap_back_referral_result_rewrite( &dc, attr->a_vals, NULL );
2084
2085                         }
2086
2087                         for ( i = 0; i < last; i++ ) {
2088                                 struct berval   pval;
2089                                 int             rc;
2090
2091                                 if ( pretty ) {
2092                                         rc = ordered_value_pretty( attr->a_desc,
2093                                                 &attr->a_vals[i], &pval, NULL );
2094
2095                                 } else {
2096                                         rc = ordered_value_validate( attr->a_desc,
2097                                                 &attr->a_vals[i], 0 );
2098                                 }
2099
2100                                 if ( rc ) {
2101                                         LBER_FREE( attr->a_vals[i].bv_val );
2102                                         if ( --last == i ) {
2103                                                 BER_BVZERO( &attr->a_vals[ i ] );
2104                                                 break;
2105                                         }
2106                                         attr->a_vals[i] = attr->a_vals[last];
2107                                         BER_BVZERO( &attr->a_vals[last] );
2108                                         i--;
2109                                         continue;
2110                                 }
2111
2112                                 if ( pretty ) {
2113                                         LBER_FREE( attr->a_vals[i].bv_val );
2114                                         attr->a_vals[i] = pval;
2115                                 }
2116                         }
2117
2118                         if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
2119                                 attr_free( attr );
2120                                 goto next_attr;
2121                         }
2122                 }
2123
2124                 if ( last && attr->a_desc->ad_type->sat_equality &&
2125                         attr->a_desc->ad_type->sat_equality->smr_normalize )
2126                 {
2127                         int i;
2128
2129                         attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
2130                         for ( i = 0; i<last; i++ ) {
2131                                 /* if normalizer fails, drop this value */
2132                                 if ( ordered_value_normalize(
2133                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2134                                         attr->a_desc,
2135                                         attr->a_desc->ad_type->sat_equality,
2136                                         &attr->a_vals[i], &attr->a_nvals[i],
2137                                         NULL )) {
2138                                         LBER_FREE( attr->a_vals[i].bv_val );
2139                                         if ( --last == i ) {
2140                                                 BER_BVZERO( &attr->a_vals[ i ] );
2141                                                 break;
2142                                         }
2143                                         attr->a_vals[i] = attr->a_vals[last];
2144                                         BER_BVZERO( &attr->a_vals[last] );
2145                                         i--;
2146                                 }
2147                         }
2148                         BER_BVZERO( &attr->a_nvals[i] );
2149                         if ( last == 0 ) {
2150                                 attr_free( attr );
2151                                 goto next_attr;
2152                         }
2153
2154                 } else {
2155                         attr->a_nvals = attr->a_vals;
2156                 }
2157
2158                 attr->a_numvals = last;
2159                 *attrp = attr;
2160                 attrp = &attr->a_next;
2161 next_attr:;
2162         }
2163
2164         /* only check if some mapping occurred */
2165         if ( check_duplicate_attrs ) {
2166                 Attribute       **ap;
2167
2168                 for ( ap = &ent.e_attrs; *ap != NULL; ap = &(*ap)->a_next ) {
2169                         Attribute       **tap;
2170
2171                         for ( tap = &(*ap)->a_next; *tap != NULL; ) {
2172                                 if ( (*tap)->a_desc == (*ap)->a_desc ) {
2173                                         Entry           e = { 0 };
2174                                         Modification    mod = { 0 };
2175                                         const char      *text = NULL;
2176                                         char            textbuf[ SLAP_TEXT_BUFLEN ];
2177                                         Attribute       *next = (*tap)->a_next;
2178
2179                                         BER_BVSTR( &e.e_name, "" );
2180                                         BER_BVSTR( &e.e_nname, "" );
2181                                         e.e_attrs = *ap;
2182                                         mod.sm_op = LDAP_MOD_ADD;
2183                                         mod.sm_desc = (*ap)->a_desc;
2184                                         mod.sm_type = mod.sm_desc->ad_cname;
2185                                         mod.sm_numvals = (*ap)->a_numvals;
2186                                         mod.sm_values = (*tap)->a_vals;
2187                                         if ( (*tap)->a_nvals != (*tap)->a_vals ) {
2188                                                 mod.sm_nvalues = (*tap)->a_nvals;
2189                                         }
2190
2191                                         (void)modify_add_values( &e, &mod,
2192                                                 /* permissive */ 1,
2193                                                 &text, textbuf, sizeof( textbuf ) );
2194
2195                                         /* should not insert new attrs! */
2196                                         assert( e.e_attrs == *ap );
2197
2198                                         attr_free( *tap );
2199                                         *tap = next;
2200
2201                                 } else {
2202                                         tap = &(*tap)->a_next;
2203                                 }
2204                         }
2205                 }
2206         }
2207
2208         /* Check for sorted attributes */
2209         if ( check_sorted_attrs ) {
2210                 for ( attr = ent.e_attrs; attr; attr = attr->a_next ) {
2211                         if ( attr->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
2212                                 while ( attr->a_numvals > 1 ) {
2213                                         int i;
2214                                         int rc = slap_sort_vals( (Modifications *)attr, &text, &i, op->o_tmpmemctx );
2215                                         if ( rc != LDAP_TYPE_OR_VALUE_EXISTS )
2216                                                 break;
2217
2218                                         /* Strip duplicate values */
2219                                         if ( attr->a_nvals != attr->a_vals )
2220                                                 LBER_FREE( attr->a_nvals[i].bv_val );
2221                                         LBER_FREE( attr->a_vals[i].bv_val );
2222                                         attr->a_numvals--;
2223                                         if ( (unsigned)i < attr->a_numvals ) {
2224                                                 attr->a_vals[i] = attr->a_vals[attr->a_numvals];
2225                                                 if ( attr->a_nvals != attr->a_vals )
2226                                                         attr->a_nvals[i] = attr->a_nvals[attr->a_numvals];
2227                                         }
2228                                         BER_BVZERO(&attr->a_vals[attr->a_numvals]);
2229                                         if ( attr->a_nvals != attr->a_vals )
2230                                                 BER_BVZERO(&attr->a_nvals[attr->a_numvals]);
2231                                 }
2232                                 attr->a_flags |= SLAP_ATTR_SORTED_VALS;
2233                         }
2234                 }
2235         }
2236
2237         ldap_get_entry_controls( mc->mc_conns[target].msc_ld,
2238                 e, &rs->sr_ctrls );
2239         rs->sr_entry = &ent;
2240         rs->sr_attrs = op->ors_attrs;
2241         rs->sr_operational_attrs = NULL;
2242         rs->sr_flags = mi->mi_targets[ target ]->mt_rep_flags;
2243         rs->sr_err = LDAP_SUCCESS;
2244         rc = send_search_entry( op, rs );
2245         switch ( rc ) {
2246         case LDAP_UNAVAILABLE:
2247                 rc = LDAP_OTHER;
2248                 break;
2249         }
2250
2251 done:;
2252         rs->sr_entry = NULL;
2253         rs->sr_attrs = NULL;
2254         if ( rs->sr_ctrls != NULL ) {
2255                 ldap_controls_free( rs->sr_ctrls );
2256                 rs->sr_ctrls = NULL;
2257         }
2258         if ( !BER_BVISNULL( &ent.e_name ) ) {
2259                 free( ent.e_name.bv_val );
2260                 BER_BVZERO( &ent.e_name );
2261         }
2262         if ( !BER_BVISNULL( &ent.e_nname ) ) {
2263                 free( ent.e_nname.bv_val );
2264                 BER_BVZERO( &ent.e_nname );
2265         }
2266         entry_clean( &ent );
2267
2268         return rc;
2269 }
2270