]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/bind.c
only wait if no entry was returned
[openldap] / servers / slapd / back-meta / bind.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2006 The OpenLDAP Foundation.
5  * Portions Copyright 2001-2003 Pierangelo Masarati.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by the Howard Chu for inclusion
19  * in OpenLDAP Software and subsequently enhanced by Pierangelo
20  * Masarati.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/errno.h>
28 #include <ac/socket.h>
29 #include <ac/string.h>
30
31
32 #define AVL_INTERNAL
33 #include "slap.h"
34 #include "../back-ldap/back-ldap.h"
35 #include "back-meta.h"
36
37 static LDAP_REBIND_PROC meta_back_default_rebind;
38
39 /*
40  * a module could register a replacement for this function
41  */
42 LDAP_REBIND_PROC        *meta_back_rebind_f = meta_back_default_rebind;
43
44 int
45 meta_back_bind( Operation *op, SlapReply *rs )
46 {
47         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
48         metaconn_t      *mc = NULL;
49
50         int             rc = LDAP_OTHER,
51                         i,
52                         gotit = 0,
53                         isroot = 0;
54
55         SlapReply       *candidates = meta_back_candidates_get( op );
56
57         rs->sr_err = LDAP_SUCCESS;
58
59         Debug( LDAP_DEBUG_ARGS, "%s meta_back_bind: dn=\"%s\".\n",
60                 op->o_log_prefix, op->o_req_dn.bv_val, 0 );
61
62         /* the test on the bind method should be superfluous */
63         if ( op->orb_method == LDAP_AUTH_SIMPLE
64                 && be_isroot_dn( op->o_bd, &op->o_req_ndn ) )
65         {
66                 if ( !be_isroot_pw( op ) ) {
67                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
68                         rs->sr_text = NULL;
69                         send_ldap_result( op, rs );
70                         return rs->sr_err;
71                 }
72
73                 if ( META_BACK_DEFER_ROOTDN_BIND( mi ) ) {
74                         rs->sr_err = LDAP_SUCCESS;
75                         rs->sr_text = NULL;
76                         /* frontend will return success */
77                         return rs->sr_err;
78                 }
79
80                 isroot = 1;
81         }
82
83         /* we need meta_back_getconn() not send result even on error,
84          * because we want to intercept the error and make it
85          * invalidCredentials */
86         mc = meta_back_getconn( op, rs, NULL, LDAP_BACK_BIND_DONTSEND );
87         if ( !mc ) {
88                 char    buf[ SLAP_TEXT_BUFLEN ];
89
90                 snprintf( buf, sizeof( buf ),
91                         "meta_back_bind: no target "
92                         "for dn \"%s\" (%d%s%s).",
93                         op->o_req_dn.bv_val, rs->sr_err,
94                         rs->sr_text ? ". " : "",
95                         rs->sr_text ? rs->sr_text : "" );
96                 Debug( LDAP_DEBUG_ANY,
97                         "%s %s\n",
98                         op->o_log_prefix, buf, 0 );
99
100                 /* FIXME: there might be cases where we don't want
101                  * to map the error onto invalidCredentials */
102                 switch ( rs->sr_err ) {
103                 case LDAP_NO_SUCH_OBJECT:
104                 case LDAP_UNWILLING_TO_PERFORM:
105                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
106                         rs->sr_text = NULL;
107                         break;
108                 }
109                 send_ldap_result( op, rs );
110                 return rs->sr_err;
111         }
112
113         /*
114          * Each target is scanned ...
115          */
116         mc->mc_authz_target = META_BOUND_NONE;
117         for ( i = 0; i < mi->mi_ntargets; i++ ) {
118                 int             lerr;
119                 Operation       op2 = *op;
120                 int             massage = 1;
121
122                 /*
123                  * Skip non-candidates
124                  */
125                 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
126                         continue;
127                 }
128
129                 if ( gotit == 0 ) {
130                         /* set rc to LDAP_SUCCESS only if at least
131                          * one candidate has been tried */
132                         rc = LDAP_SUCCESS;
133                         gotit = 1;
134
135                 } else if ( isroot == 0 ) {
136                         /*
137                          * A bind operation is expected to have
138                          * ONE CANDIDATE ONLY!
139                          */
140                         Debug( LDAP_DEBUG_ANY,
141                                 "### %s meta_back_bind: more than one"
142                                 " candidate selected...\n",
143                                 op->o_log_prefix, 0, 0 );
144                 }
145
146                 if ( isroot ) {
147                         if ( BER_BVISNULL( &mi->mi_targets[ i ].mt_pseudorootdn ) )
148                         {
149                                 metasingleconn_t        *msc = &mc->mc_conns[ i ];
150
151                                 /* skip the target if no pseudorootdn is provided */
152                                 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
153                                         ch_free( msc->msc_bound_ndn.bv_val );
154                                         BER_BVZERO( &msc->msc_bound_ndn );
155                                 }
156
157                                 if ( LDAP_BACK_SAVECRED( mi ) &&
158                                         !BER_BVISNULL( &msc->msc_cred ) )
159                                 {
160                                         /* destroy sensitive data */
161                                         memset( msc->msc_cred.bv_val, 0,
162                                                 msc->msc_cred.bv_len );
163                                         ch_free( msc->msc_cred.bv_val );
164                                         BER_BVZERO( &msc->msc_cred );
165                                 }
166
167                                 continue;
168                         }
169
170                         op2.o_req_dn = mi->mi_targets[ i ].mt_pseudorootdn;
171                         op2.o_req_ndn = mi->mi_targets[ i ].mt_pseudorootdn;
172                         op2.orb_cred = mi->mi_targets[ i ].mt_pseudorootpw;
173                         op2.orb_method = LDAP_AUTH_SIMPLE;
174
175                         massage = 0;
176                 }
177                 
178                 lerr = meta_back_single_bind( &op2, rs, mc, i, massage );
179
180                 if ( lerr != LDAP_SUCCESS ) {
181                         rc = rs->sr_err = lerr;
182                         /* FIXME: in some cases (e.g. unavailable)
183                          * do not assume it's not candidate; rather
184                          * mark this as an error to be eventually
185                          * reported to client */
186                         candidates[ i ].sr_tag = META_NOT_CANDIDATE;
187                         break;
188                 }
189         }
190
191         /* must re-insert if local DN changed as result of bind */
192         if ( rc == LDAP_SUCCESS ) {
193                 if ( isroot ) {
194                         mc->mc_authz_target = META_BOUND_ALL;
195                         ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ) );
196                 }
197
198                 if ( !dn_match( &op->o_req_ndn, &mc->mc_local_ndn ) ) {
199                         metaconn_t      *tmpmc;
200                         int             lerr;
201
202                         /* wait for all other ops to release the connection */
203 retry_lock:;
204                         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
205                         if ( mc->mc_refcnt > 1 ) {
206                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
207                                 ldap_pvt_thread_yield();
208                                 goto retry_lock;
209                         }
210
211                         assert( mc->mc_refcnt == 1 );
212                         tmpmc = avl_delete( &mi->mi_conninfo.lai_tree, (caddr_t)mc,
213                                 meta_back_conndn_cmp );
214                         assert( tmpmc == mc );
215
216                         ber_bvreplace( &mc->mc_local_ndn, &op->o_req_ndn );
217                         lerr = avl_insert( &mi->mi_conninfo.lai_tree, (caddr_t)mc,
218                                 meta_back_conndn_cmp, meta_back_conndn_dup );
219                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
220                         if ( lerr == -1 ) {
221                                 meta_clear_candidates( op, mc );
222
223                                 /* we can do this because mc_refcnt == 1 */
224                                 mc->mc_refcnt = 0;
225                                 meta_back_conn_free( mc );
226                                 mc = NULL;
227                         }
228                 }
229         }
230
231         if ( mc != NULL ) {
232                 meta_back_release_conn( op, mc );
233         }
234
235         /*
236          * rc is LDAP_SUCCESS if at least one bind succeeded,
237          * err is the last error that occurred during a bind;
238          * if at least (and at most?) one bind succeeds, fine.
239          */
240         if ( rc != LDAP_SUCCESS ) {
241                 
242                 /*
243                  * deal with bind failure ...
244                  */
245
246                 /*
247                  * no target was found within the naming context, 
248                  * so bind must fail with invalid credentials
249                  */
250                 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
251                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
252                 } else {
253                         rs->sr_err = slap_map_api2result( rs );
254                 }
255                 send_ldap_result( op, rs );
256                 return rs->sr_err;
257
258         }
259
260         return LDAP_SUCCESS;
261 }
262
263 /*
264  * meta_back_single_bind
265  *
266  * attempts to perform a bind with creds
267  */
268 int
269 meta_back_single_bind(
270         Operation               *op,
271         SlapReply               *rs,
272         metaconn_t              *mc,
273         int                     candidate,
274         int                     massage )
275 {
276         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
277         metatarget_t            *mt = &mi->mi_targets[ candidate ];
278         struct berval           mdn = BER_BVNULL;
279         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
280         int                     msgid,
281                                 rebinding = 0;
282
283         
284         if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
285                 ch_free( msc->msc_bound_ndn.bv_val );
286                 BER_BVZERO( &msc->msc_bound_ndn );
287         }
288
289         if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &msc->msc_cred ) ) {
290                 /* destroy sensitive data */
291                 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
292                 ch_free( msc->msc_cred.bv_val );
293                 BER_BVZERO( &msc->msc_cred );
294         }
295
296         /*
297          * Rewrite the bind dn if needed
298          */
299         if ( massage ) {
300                 dncookie                dc;
301
302                 dc.target = mt;
303                 dc.conn = op->o_conn;
304                 dc.rs = rs;
305                 dc.ctx = "bindDN";
306
307                 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
308                         rs->sr_text = "DN rewrite error";
309                         rs->sr_err = LDAP_OTHER;
310                         return rs->sr_err;
311                 }
312
313         } else {
314                 mdn = op->o_req_dn;
315         }
316
317         /* FIXME: this fixes the bind problem right now; we need
318          * to use the asynchronous version to get the "matched"
319          * and more in case of failure ... */
320         /* FIXME: should we check if at least some of the op->o_ctrls
321          * can/should be passed? */
322 rebind:;
323         rs->sr_err = ldap_sasl_bind( msc->msc_ld, mdn.bv_val,
324                         LDAP_SASL_SIMPLE, &op->orb_cred,
325                         op->o_ctrls, NULL, &msgid );
326         if ( rs->sr_err == LDAP_SUCCESS ) {
327                 LDAPMessage     *res;
328                 struct timeval  tv;
329                 int             rc;
330                 int             nretries = mt->mt_nretries;
331                 char            buf[ SLAP_TEXT_BUFLEN ];
332
333                 LDAP_BACK_TV_SET( &tv );
334
335                 /*
336                  * handle response!!!
337                  */
338 retry:;
339                 switch ( ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
340                 case 0:
341                         snprintf( buf, sizeof( buf ),
342                                 "ldap_result=0 nretries=%d%s",
343                                 nretries, rebinding ? " rebinding" : "" );
344                         Debug( LDAP_DEBUG_ANY,
345                                 "%s meta_back_single_bind[%d]: %s.\n",
346                                 op->o_log_prefix, candidate, buf );
347
348                         if ( nretries != META_RETRY_NEVER ) {
349                                 ldap_pvt_thread_yield();
350                                 if ( nretries > 0 ) {
351                                         nretries--;
352                                 }
353                                 tv = mt->mt_bind_timeout;
354                                 goto retry;
355                         }
356
357                         rs->sr_err = LDAP_BUSY;
358                         if ( rebinding ) {
359                                 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
360                                 break;
361                         }
362
363                         /* FIXME: some times the request times out
364                          * while the other party is not willing to
365                          * send a response any more.  Give it a second
366                          * chance with a freshly bound connection */
367                         rebinding = 1;
368                         nretries = mt->mt_nretries;
369                         /* fallthru */
370
371                 case -1:
372                         ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
373                                 &rs->sr_err );
374
375                         if ( rebinding ) {
376                                 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
377                         }
378
379                         snprintf( buf, sizeof( buf ),
380                                 "err=%d (%s) nretries=%d",
381                                 rs->sr_err, ldap_err2string( rs->sr_err ), nretries );
382                         Debug( LDAP_DEBUG_ANY,
383                                 "### %s meta_back_single_bind[%d]: %s.\n",
384                                 op->o_log_prefix, candidate, buf );
385
386                         rc = slap_map_api2result( rs );
387                         if ( rs->sr_err == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
388                                 rc = meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_DONTSEND );
389                                 if ( rc ) {
390                                         if ( nretries > 0 ) {
391                                                 nretries--;
392                                         }
393                                         ldap_pvt_thread_yield();
394                                         goto rebind;
395                                 }
396                                 goto return_results;
397                         }
398                         break;
399
400                 default:
401                         rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
402                                         NULL, NULL, NULL, NULL, 1 );
403                         if ( rc != LDAP_SUCCESS ) {
404                                 rs->sr_err = rc;
405                         }
406                         break;
407                 }
408         }
409
410         if ( rs->sr_err != LDAP_SUCCESS ) {
411                 rs->sr_err = slap_map_api2result( rs );
412                 goto return_results;
413         }
414
415         ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_dn );
416         LDAP_BACK_CONN_ISBOUND_SET( msc );
417         mc->mc_authz_target = candidate;
418
419         if ( LDAP_BACK_SAVECRED( mi ) ) {
420                 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
421                 ldap_set_rebind_proc( msc->msc_ld, meta_back_rebind_f, msc );
422         }
423
424         if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
425                         && op->o_req_ndn.bv_len != 0 )
426         {
427                 ( void )meta_dncache_update_entry( &mi->mi_cache,
428                                 &op->o_req_ndn, candidate );
429         }
430
431 return_results:;
432         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
433                 free( mdn.bv_val );
434         }
435
436         return rs->sr_err;
437 }
438
439 /*
440  * meta_back_single_dobind
441  */
442 int
443 meta_back_single_dobind(
444         Operation               *op,
445         SlapReply               *rs,
446         metaconn_t              **mcp,
447         int                     candidate,
448         ldap_back_send_t        sendok,
449         int                     nretries,
450         int                     dolock )
451 {
452         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
453         metatarget_t            *mt = &mi->mi_targets[ candidate ];
454         metaconn_t              *mc = *mcp;
455         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
456         int                     rc;
457         static struct berval    cred = BER_BVC( "" );
458         int                     msgid,
459                                 rebinding = 0,
460                                 save_nretries = nretries;
461
462         assert( !LDAP_BACK_CONN_ISBOUND( msc ) );
463
464         /*
465          * meta_back_single_dobind() calls meta_back_single_bind()
466          * if required.
467          */
468         if ( be_isroot( op ) && !BER_BVISNULL( &mi->mi_targets[ candidate ].mt_pseudorootdn ) )
469         {
470                 Operation       op2 = *op;
471
472                 op2.o_tag = LDAP_REQ_BIND;
473                 op2.o_req_dn = mi->mi_targets[ candidate ].mt_pseudorootdn;
474                 op2.o_req_ndn = mi->mi_targets[ candidate ].mt_pseudorootdn;
475                 op2.orb_cred = mi->mi_targets[ candidate ].mt_pseudorootpw;
476                 op2.orb_method = LDAP_AUTH_SIMPLE;
477
478                 rc = meta_back_single_bind( &op2, rs, *mcp, candidate, 0 );
479                 goto done;
480         }
481
482         /*
483          * Otherwise an anonymous bind is performed
484          * (note: if the target was already bound, the anonymous
485          * bind clears the previous bind).
486          */
487         if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
488                 ber_memfree( msc->msc_bound_ndn.bv_val );
489                 BER_BVZERO( &msc->msc_bound_ndn );
490         }
491                 
492         if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &msc->msc_cred ) ) {
493                 /* destroy sensitive data */
494                 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
495                 ber_memfree( msc->msc_cred.bv_val );
496                 BER_BVZERO( &msc->msc_cred );
497         }
498
499         /* FIXME: should we check if at least some of the op->o_ctrls
500          * can/should be passed? */
501 rebind:;
502         rc = ldap_sasl_bind( msc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
503                         NULL, NULL, &msgid );
504         if ( rc == LDAP_SUCCESS ) {
505                 LDAPMessage     *res;
506                 struct timeval  tv;
507                 char            buf[ SLAP_TEXT_BUFLEN ];
508
509                 LDAP_BACK_TV_SET( &tv );
510
511                 /*
512                  * handle response!!!
513                  */
514 retry:;
515                 switch ( ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
516                 case 0:
517                         snprintf( buf, sizeof( buf ),
518                                 "ldap_result=0 nretries=%d%s",
519                                 nretries, rebinding ? " rebinding" : "" );
520                         Debug( LDAP_DEBUG_ANY,
521                                 "%s meta_back_single_dobind[%d]: %s.\n",
522                                 op->o_log_prefix, candidate, buf );
523
524                         if ( nretries != META_RETRY_NEVER ) {
525                                 ldap_pvt_thread_yield();
526                                 if ( nretries > 0 ) {
527                                         nretries--;
528                                 }
529                                 tv = mt->mt_bind_timeout;
530                                 goto retry;
531                         }
532
533                         rc = LDAP_BUSY;
534                         if ( rebinding ) {
535                                 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
536                                 break;
537                         }
538
539                         /* FIXME: some times the request times out
540                          * while the other party is not willing to
541                          * send a response any more.  Give it a second
542                          * chance with a freshly bound connection */
543                         rebinding = 1;
544                         nretries = save_nretries;
545                         /* fallthru */
546
547                 case -1:
548                         ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
549                                 &rs->sr_err );
550
551                         if ( rebinding ) {
552                                 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
553                         }
554
555                         snprintf( buf, sizeof( buf ),
556                                 "err=%d (%s) nretries=%d",
557                                 rs->sr_err, ldap_err2string( rs->sr_err ), nretries );
558                         Debug( LDAP_DEBUG_ANY,
559                                 "### %s meta_back_single_dobind[%d]: %s.\n",
560                                 op->o_log_prefix, candidate, buf );
561
562                         rc = slap_map_api2result( rs );
563                         if ( rc == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
564                                 if ( dolock ) {
565                                         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
566                                 }
567
568                                 if ( mc->mc_refcnt == 1 ) {
569                                         meta_clear_one_candidate( msc );
570                                         LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
571
572                                         ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
573
574                                         /* mc here must be the regular mc,
575                                          * reset and ready for init */
576                                         rc = meta_back_init_one_conn( op, rs,
577                                                 mt, mc, candidate,
578                                                 LDAP_BACK_CONN_ISPRIV( mc ),
579                                                 LDAP_BACK_DONTSEND );
580
581                                 } else {
582                                         /* can't do anything about it */
583                                         rc = LDAP_UNAVAILABLE;
584                                 }
585
586                                 if ( dolock ) {
587                                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
588                                 }
589
590                                 if ( rc == LDAP_SUCCESS ) {
591                                         ldap_pvt_thread_yield();
592                                         if ( nretries > 0 ) {
593                                                 nretries--;
594                                         }
595                                         goto rebind;
596                                 }
597                         }
598                         break;
599
600                 default:
601                         rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
602                                         NULL, NULL, NULL, NULL, 1 );
603                         if ( rc == LDAP_SUCCESS ) {
604                                 rc = slap_map_api2result( rs );
605                         }
606                         break;
607                 }
608
609         } else {
610                 rs->sr_err = rc;
611                 rc = slap_map_api2result( rs );
612         }
613
614 done:;
615         rs->sr_err = rc;
616         if ( rc != LDAP_SUCCESS && META_BACK_ONERR_STOP( mi ) ) {
617                 meta_back_release_conn_lock( op, mc, 1, dolock );
618                 *mcp = NULL;
619
620                 if ( sendok & LDAP_BACK_SENDERR ) {
621                         send_ldap_result( op, rs );
622                 }
623         }
624
625         return rc;
626 }
627
628 /*
629  * meta_back_dobind
630  */
631 int
632 meta_back_dobind(
633         Operation               *op,
634         SlapReply               *rs,
635         metaconn_t              *mc,
636         ldap_back_send_t        sendok )
637 {
638         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
639
640         int                     bound = 0,
641                                 i,
642                                 isroot = 0;
643
644         SlapReply               *candidates = meta_back_candidates_get( op );
645
646         if ( be_isroot( op ) ) {
647                 isroot = 1;
648         }
649
650         Debug( LDAP_DEBUG_TRACE,
651                 "%s meta_back_dobind: conn=%ld%s\n",
652                 op->o_log_prefix,
653                 LDAP_BACK_PCONN_ID( mc->mc_conn ),
654                 isroot ? " (isroot)" : "" );
655
656         /*
657          * all the targets are bound as pseudoroot
658          */
659         if ( mc->mc_authz_target == META_BOUND_ALL ) {
660                 bound = 1;
661                 goto done;
662         }
663
664         for ( i = 0; i < mi->mi_ntargets; i++ ) {
665                 metatarget_t            *mt = &mi->mi_targets[ i ];
666                 metasingleconn_t        *msc = &mc->mc_conns[ i ];
667                 int                     rc, do_retry = 1;
668                 char                    *rootdn = NULL;
669
670                 /*
671                  * Not a candidate
672                  */
673                 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
674                         continue;
675                 }
676
677                 assert( msc->msc_ld != NULL );
678
679                 /*
680                  * If the target is already bound it is skipped
681                  */
682                 if ( LDAP_BACK_CONN_ISBOUND( msc ) || LDAP_BACK_CONN_ISANON( msc ) ) {
683                         ++bound;
684                         continue;
685                 }
686
687 retry:;
688                 rc = meta_back_single_dobind( op, rs, &mc, i,
689                         LDAP_BACK_DONTSEND, mt->mt_nretries, 1 );
690                 /*
691                  * NOTE: meta_back_single_dobind() already retries;
692                  * in case of failure, it resets mc...
693                  */
694                 if ( rc != LDAP_SUCCESS ) {
695                         char            buf[ SLAP_TEXT_BUFLEN ];
696
697                         if ( mc == NULL ) {
698                                 /* meta_back_single_dobind() already sent 
699                                  * response and released connection */
700                                 goto send_err;
701                         }
702
703                         if ( rc == LDAP_UNAVAILABLE && do_retry ) {
704                                 do_retry = 0;
705                                 if ( meta_back_retry( op, rs, &mc, i, sendok ) ) {
706                                         goto retry;
707                                 }
708                                 return 0;
709                         }
710
711                         snprintf( buf, sizeof( buf ),
712                                 "meta_back_dobind[%d]: (%s) err=%d (%s).",
713                                 i, rootdn ? rootdn : "anonymous",
714                                 rc, ldap_err2string( rc ) );
715                         Debug( LDAP_DEBUG_ANY,
716                                 "%s %s\n",
717                                 op->o_log_prefix, buf, 0 );
718
719                         /*
720                          * null cred bind should always succeed
721                          * as anonymous, so a failure means
722                          * the target is no longer candidate possibly
723                          * due to technical reasons (remote host down?)
724                          * so better clear the handle
725                          */
726                         /* leave the target candidate, but record the error for later use */
727                         candidates[ i ].sr_err = rc;
728                         if ( META_BACK_ONERR_STOP( mi ) ) {
729                                 bound = 0;
730                                 goto done;
731                         }
732
733                         continue;
734                 } /* else */
735                 
736                 Debug( LDAP_DEBUG_TRACE,
737                         "%s meta_back_dobind[%d]: "
738                         "(%s)\n",
739                         op->o_log_prefix, i,
740                         rootdn ? rootdn : "anonymous" );
741
742                 if ( rootdn ) {
743                         LDAP_BACK_CONN_ISBOUND_SET( msc );
744                 } else {
745                         LDAP_BACK_CONN_ISANON_SET( msc );
746                 }
747                 ++bound;
748         }
749
750 done:;
751         Debug( LDAP_DEBUG_TRACE,
752                 "%s meta_back_dobind: conn=%ld bound=%d\n",
753                 op->o_log_prefix, LDAP_BACK_PCONN_ID( mc->mc_conn ), bound );
754
755         if ( bound == 0 ) {
756                 meta_back_release_conn( op, mc );
757
758 send_err:;
759                 if ( sendok & LDAP_BACK_SENDERR ) {
760                         if ( rs->sr_err == LDAP_SUCCESS ) {
761                                 rs->sr_err = LDAP_BUSY;
762                         }
763                         send_ldap_result( op, rs );
764                 }
765
766                 return 0;
767         }
768
769         return ( bound > 0 );
770 }
771
772 /*
773  * meta_back_default_rebind
774  *
775  * This is a callback used for chasing referrals using the same
776  * credentials as the original user on this session.
777  */
778 static int 
779 meta_back_default_rebind(
780         LDAP                    *ld,
781         LDAP_CONST char         *url,
782         ber_tag_t               request,
783         ber_int_t               msgid,
784         void                    *params )
785 {
786         metasingleconn_t        *msc = ( metasingleconn_t * )params;
787
788         return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
789                         LDAP_SASL_SIMPLE, &msc->msc_cred,
790                         NULL, NULL, NULL );
791 }
792
793 /*
794  * FIXME: error return must be handled in a cleaner way ...
795  */
796 int
797 meta_back_op_result(
798         metaconn_t      *mc,
799         Operation       *op,
800         SlapReply       *rs,
801         int             candidate )
802 {
803         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
804
805         int                     i,
806                                 rerr = LDAP_SUCCESS;
807         char                    *rmsg = NULL,
808                                 *rmatch = NULL;
809         const char              *save_rmsg = NULL,
810                                 *save_rmatch = NULL;
811         void                    *rmatch_ctx = NULL;
812
813         if ( candidate != META_TARGET_NONE ) {
814                 metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
815
816                 rs->sr_err = LDAP_SUCCESS;
817
818                 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
819                 if ( rs->sr_err != LDAP_SUCCESS ) {
820                         /*
821                          * better check the type of error. In some cases
822                          * (search ?) it might be better to return a
823                          * success if at least one of the targets gave
824                          * positive result ...
825                          */
826                         ldap_get_option( msc->msc_ld,
827                                         LDAP_OPT_ERROR_STRING, &rmsg );
828                         if ( rmsg != NULL && rmsg[ 0 ] == '\0' ) {
829                                 ldap_memfree( rmsg );
830                                 rmsg = NULL;
831                         }
832
833                         ldap_get_option( msc->msc_ld,
834                                         LDAP_OPT_MATCHED_DN, &rmatch );
835                         if ( rmatch != NULL && rmatch[ 0 ] == '\0' ) {
836                                 ldap_memfree( rmatch );
837                                 rmatch = NULL;
838                         }
839
840                         rerr = rs->sr_err = slap_map_api2result( rs );
841
842                         Debug(LDAP_DEBUG_ANY,
843                                         "==> meta_back_op_result: target"
844                                         " <%d> sending msg \"%s\""
845                                         " (matched \"%s\")\n", 
846                                         candidate, ( rmsg ? rmsg : "" ),
847                                         ( rmatch ? rmatch : "" ) );
848                 }
849
850         } else {
851                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
852                         metasingleconn_t        *msc = &mc->mc_conns[ i ];
853                         char                    *msg = NULL;
854                         char                    *match = NULL;
855
856                         rs->sr_err = LDAP_SUCCESS;
857
858                         ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
859                         if ( rs->sr_err != LDAP_SUCCESS ) {
860                                 /*
861                                  * better check the type of error. In some cases
862                                  * (search ?) it might be better to return a
863                                  * success if at least one of the targets gave
864                                  * positive result ...
865                                  */
866                                 ldap_get_option( msc->msc_ld,
867                                                 LDAP_OPT_ERROR_STRING, &msg );
868                                 if ( msg != NULL && msg[ 0 ] == '\0' ) {
869                                         ldap_memfree( msg );
870                                         msg = NULL;
871                                 }
872
873                                 ldap_get_option( msc->msc_ld,
874                                                 LDAP_OPT_MATCHED_DN, &match );
875                                 if ( match != NULL && match[ 0 ] == '\0' ) {
876                                         ldap_memfree( match );
877                                         match = NULL;
878                                 }
879
880                                 rs->sr_err = slap_map_api2result( rs );
881         
882                                 Debug(LDAP_DEBUG_ANY,
883                                                 "==> meta_back_op_result: target"
884                                                 " <%d> sending msg \"%s\""
885                                                 " (matched \"%s\")\n", 
886                                                 i, ( msg ? msg : "" ),
887                                                 ( match ? match : "" ) );
888         
889                                 /*
890                                  * FIXME: need to rewrite "match" (need rwinfo)
891                                  */
892                                 switch ( rs->sr_err ) {
893                                 default:
894                                         rerr = rs->sr_err;
895                                         if ( msg != NULL ) {
896                                                 if ( rmsg ) {
897                                                         ldap_memfree( rmsg );
898                                                 }
899                                                 rmsg = msg;
900                                                 msg = NULL;
901                                         }
902                                         if ( match != NULL ) {
903                                                 if ( rmatch ) {
904                                                         ldap_memfree( rmatch );
905                                                 }
906                                                 rmatch = match;
907                                                 match = NULL;
908                                         }
909                                         break;
910                                 }
911
912                                 if ( msg ) {
913                                         ldap_memfree( msg );
914                                 }
915         
916                                 if ( match ) {
917                                         ldap_memfree( match );
918                                 }
919                         }
920                 }
921         }
922         
923         rs->sr_err = rerr;
924         if ( rmsg != NULL ) {
925                 save_rmsg = rs->sr_text;
926                 rs->sr_text = rmsg;
927         }
928         if ( rmatch != NULL ) {
929                 struct berval   dn, pdn;
930
931                 ber_str2bv( rmatch, 0, 0, &dn );
932                 if ( dnPretty( NULL, &dn, &pdn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
933                         ldap_memfree( rmatch );
934                         rmatch_ctx = op->o_tmpmemctx;
935                         rmatch = pdn.bv_val;
936                 }
937                 save_rmatch = rs->sr_matched;
938                 rs->sr_matched = rmatch;
939         }
940         send_ldap_result( op, rs );
941         if ( rmsg != NULL ) {
942                 ber_memfree( rmsg );
943                 rs->sr_text = save_rmsg;
944         }
945         if ( rmatch != NULL ) {
946                 ber_memfree_x( rmatch, rmatch_ctx );
947                 rs->sr_matched = save_rmatch;
948         }
949
950         return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );
951 }
952