]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/bind.c
9f5c0962dae51e0b22c5d3090b58d26a9cbfd318
[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                         candidates[ i ].sr_tag = META_NOT_CANDIDATE;
183                         break;
184                 }
185         }
186
187         /* must re-insert if local DN changed as result of bind */
188         if ( rc == LDAP_SUCCESS ) {
189                 if ( isroot ) {
190                         mc->mc_authz_target = META_BOUND_ALL;
191                         ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ) );
192                 }
193
194                 if ( !dn_match( &op->o_req_ndn, &mc->mc_local_ndn ) ) {
195                         int             lerr;
196
197                         /* wait for all other ops to release the connection */
198 retry_lock:;
199                         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
200                         if ( mc->mc_refcnt > 1 ) {
201                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
202                                 ldap_pvt_thread_yield();
203                                 goto retry_lock;
204                         }
205
206                         assert( mc->mc_refcnt == 1 );
207                         mc = avl_delete( &mi->mi_conninfo.lai_tree, (caddr_t)mc,
208                                 meta_back_dnconn_cmp );
209                         assert( mc != NULL );
210
211                         ber_bvreplace( &mc->mc_local_ndn, &op->o_req_ndn );
212                         lerr = avl_insert( &mi->mi_conninfo.lai_tree, (caddr_t)mc,
213                                 meta_back_dnconn_cmp, meta_back_dnconn_dup );
214                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
215                         if ( lerr == -1 ) {
216                                 for ( i = 0; i < mi->mi_ntargets; ++i ) {
217                                         if ( mc->mc_conns[ i ].msc_ld != NULL ) {
218                                                 meta_clear_one_candidate( &mc->mc_conns[ i ] );
219                                         }
220                                 }
221
222                                 /* we can do this because mc_refcnt == 1 */
223                                 mc->mc_refcnt = 0;
224                                 meta_back_conn_free( mc );
225                                 mc = NULL;
226                         }
227                 }
228         }
229
230         if ( mc != NULL ) {
231                 meta_back_release_conn( op, mc );
232         }
233
234         /*
235          * rc is LDAP_SUCCESS if at least one bind succeeded,
236          * err is the last error that occurred during a bind;
237          * if at least (and at most?) one bind succeeds, fine.
238          */
239         if ( rc != LDAP_SUCCESS ) {
240                 
241                 /*
242                  * deal with bind failure ...
243                  */
244
245                 /*
246                  * no target was found within the naming context, 
247                  * so bind must fail with invalid credentials
248                  */
249                 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
250                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
251                 } else {
252                         rs->sr_err = slap_map_api2result( rs );
253                 }
254                 send_ldap_result( op, rs );
255                 return rs->sr_err;
256
257         }
258
259         return LDAP_SUCCESS;
260 }
261
262 /*
263  * meta_back_single_bind
264  *
265  * attempts to perform a bind with creds
266  */
267 int
268 meta_back_single_bind(
269         Operation               *op,
270         SlapReply               *rs,
271         metaconn_t              *mc,
272         int                     candidate,
273         int                     massage )
274 {
275         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
276         metatarget_t            *mt = &mi->mi_targets[ candidate ];
277         struct berval           mdn = BER_BVNULL;
278         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
279         int                     msgid,
280                                 rebinding = 0;
281
282         
283         if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
284                 ch_free( msc->msc_bound_ndn.bv_val );
285                 BER_BVZERO( &msc->msc_bound_ndn );
286         }
287
288         if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &msc->msc_cred ) ) {
289                 /* destroy sensitive data */
290                 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
291                 ch_free( msc->msc_cred.bv_val );
292                 BER_BVZERO( &msc->msc_cred );
293         }
294
295         /*
296          * Rewrite the bind dn if needed
297          */
298         if ( massage ) {
299                 dncookie                dc;
300
301                 dc.target = mt;
302                 dc.conn = op->o_conn;
303                 dc.rs = rs;
304                 dc.ctx = "bindDN";
305
306                 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
307                         send_ldap_result( op, rs );
308                         return -1;
309                 }
310
311         } else {
312                 mdn = op->o_req_dn;
313         }
314
315         /* FIXME: this fixes the bind problem right now; we need
316          * to use the asynchronous version to get the "matched"
317          * and more in case of failure ... */
318         /* FIXME: should we check if at least some of the op->o_ctrls
319          * can/should be passed? */
320 rebind:;
321         rs->sr_err = ldap_sasl_bind( msc->msc_ld, mdn.bv_val,
322                         LDAP_SASL_SIMPLE, &op->orb_cred,
323                         op->o_ctrls, NULL, &msgid );
324         if ( rs->sr_err == LDAP_SUCCESS ) {
325                 LDAPMessage     *res;
326                 struct timeval  tv;
327                 int             rc;
328                 int             nretries = mt->mt_nretries;
329                 char            buf[ SLAP_TEXT_BUFLEN ];
330
331                 LDAP_BACK_TV_SET( &tv );
332
333                 /*
334                  * handle response!!!
335                  */
336 retry:;
337                 switch ( ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
338                 case 0:
339                         snprintf( buf, sizeof( buf ),
340                                 "ldap_result=0 nretries=%d%s",
341                                 nretries, rebinding ? " rebinding" : "" );
342                         Debug( LDAP_DEBUG_ANY,
343                                 "%s meta_back_single_bind[%d]: %s.\n",
344                                 op->o_log_prefix, candidate, buf );
345
346                         if ( nretries != META_RETRY_NEVER ) {
347                                 ldap_pvt_thread_yield();
348                                 if ( nretries > 0 ) {
349                                         nretries--;
350                                 }
351                                 tv = mt->mt_bind_timeout;
352                                 goto retry;
353                         }
354
355                         rs->sr_err = LDAP_BUSY;
356                         if ( rebinding ) {
357                                 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
358                                 break;
359                         }
360
361                         /* FIXME: some times the request times out
362                          * while the other party is not willing to
363                          * send a response any more.  Give it a second
364                          * chance with a freshly bound connection */
365                         rebinding = 1;
366                         nretries = mt->mt_nretries;
367                         /* fallthru */
368
369                 case -1:
370                         ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
371                                 &rs->sr_err );
372
373                         if ( rebinding ) {
374                                 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
375                         }
376
377                         snprintf( buf, sizeof( buf ),
378                                 "err=%d nretries=%d",
379                                 rs->sr_err, nretries );
380                         Debug( LDAP_DEBUG_ANY,
381                                 "### %s meta_back_single_bind[%d]: %s.\n",
382                                 op->o_log_prefix, candidate, buf );
383
384                         rc = slap_map_api2result( rs );
385                         if ( rs->sr_err == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
386                                 rc = meta_back_retry( op, rs, mc, candidate, LDAP_BACK_DONTSEND );
387                                 if ( rc ) {
388                                         if ( nretries > 0 ) {
389                                                 nretries--;
390                                         }
391                                         ldap_pvt_thread_yield();
392                                         goto rebind;
393                                 }
394                         }
395                         break;
396
397                 default:
398                         rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
399                                         NULL, NULL, NULL, NULL, 1 );
400                         if ( rc != LDAP_SUCCESS ) {
401                                 rs->sr_err = rc;
402                         }
403                         break;
404                 }
405         }
406
407         if ( rs->sr_err != LDAP_SUCCESS ) {
408                 rs->sr_err = slap_map_api2result( rs );
409                 goto return_results;
410         }
411
412         ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_dn );
413         LDAP_BACK_CONN_ISBOUND_SET( msc );
414         mc->mc_authz_target = candidate;
415
416         if ( LDAP_BACK_SAVECRED( mi ) ) {
417                 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
418                 ldap_set_rebind_proc( msc->msc_ld, meta_back_rebind_f, msc );
419         }
420
421         if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
422                         && op->o_req_ndn.bv_len != 0 )
423         {
424                 ( void )meta_dncache_update_entry( &mi->mi_cache,
425                                 &op->o_req_ndn, candidate );
426         }
427
428 return_results:;
429         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
430                 free( mdn.bv_val );
431         }
432
433         return rs->sr_err;
434 }
435
436 /*
437  * meta_back_single_dobind
438  */
439 int
440 meta_back_single_dobind(
441         Operation               *op,
442         SlapReply               *rs,
443         metaconn_t              *mc,
444         int                     candidate,
445         ldap_back_send_t        sendok,
446         int                     nretries,
447         int                     dolock )
448 {
449         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
450         metatarget_t            *mt = &mi->mi_targets[ candidate ];
451         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
452         int                     rc;
453         static struct berval    cred = BER_BVC( "" );
454         int                     msgid,
455                                 rebinding = 0,
456                                 save_nretries = nretries;
457
458         assert( !LDAP_BACK_CONN_ISBOUND( msc ) );
459
460         /*
461          * Otherwise an anonymous bind is performed
462          * (note: if the target was already bound, the anonymous
463          * bind clears the previous bind).
464          */
465         if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
466                 ber_memfree( msc->msc_bound_ndn.bv_val );
467                 BER_BVZERO( &msc->msc_bound_ndn );
468         }
469                 
470         if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &msc->msc_cred ) ) {
471                 /* destroy sensitive data */
472                 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
473                 ber_memfree( msc->msc_cred.bv_val );
474                 BER_BVZERO( &msc->msc_cred );
475         }
476
477         /* FIXME: should we check if at least some of the op->o_ctrls
478          * can/should be passed? */
479 rebind:;
480         rc = ldap_sasl_bind( msc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
481                         NULL, NULL, &msgid );
482         if ( rc == LDAP_SUCCESS ) {
483                 LDAPMessage     *res;
484                 struct timeval  tv;
485                 char            buf[ SLAP_TEXT_BUFLEN ];
486
487                 LDAP_BACK_TV_SET( &tv );
488
489                 /*
490                  * handle response!!!
491                  */
492 retry:;
493                 switch ( ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
494                 case 0:
495                         snprintf( buf, sizeof( buf ),
496                                 "ldap_result=0 nretries=%d%s",
497                                 nretries, rebinding ? " rebinding" : "" );
498                         Debug( LDAP_DEBUG_ANY,
499                                 "%s meta_back_single_dobind[%d]: %s.\n",
500                                 op->o_log_prefix, candidate, buf );
501
502                         if ( nretries != META_RETRY_NEVER ) {
503                                 ldap_pvt_thread_yield();
504                                 if ( nretries > 0 ) {
505                                         nretries--;
506                                 }
507                                 tv = mt->mt_bind_timeout;
508                                 goto retry;
509                         }
510
511                         rc = LDAP_BUSY;
512                         if ( rebinding ) {
513                                 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
514                                 break;
515                         }
516
517                         /* FIXME: some times the request times out
518                          * while the other party is not willing to
519                          * send a response any more.  Give it a second
520                          * chance with a freshly bound connection */
521                         rebinding = 1;
522                         nretries = save_nretries;
523                         /* fallthru */
524
525                 case -1:
526                         ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
527                                 &rs->sr_err );
528
529                         if ( rebinding ) {
530                                 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
531                         }
532
533                         snprintf( buf, sizeof( buf ),
534                                 "err=%d nretries=%d",
535                                 rs->sr_err, nretries );
536                         Debug( LDAP_DEBUG_ANY,
537                                 "### %s meta_back_single_dobind[%d]: %s.\n",
538                                 op->o_log_prefix, candidate, buf );
539
540                         rc = slap_map_api2result( rs );
541                         if ( rc == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
542                                 /* NOTE: we do not use meta_back_retry() here
543                                  * to avoid circular loops; mc_mutex is set
544                                  * by the caller */
545                                 if ( dolock ) {
546                                         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
547                                 }
548
549                                 if ( mc->mc_refcnt == 1 ) {
550                                         meta_clear_one_candidate( msc );
551                                         LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
552
553                                         ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
554
555                                         /* mc here must be the regular mc,
556                                          * reset and ready for init */
557                                         rc = meta_back_init_one_conn( op, rs,
558                                                 mt, mc, candidate,
559                                                 LDAP_BACK_CONN_ISPRIV( mc ),
560                                                 LDAP_BACK_DONTSEND );
561
562                                 } else {
563                                         /* can't do anything about it */
564                                         rc = LDAP_UNAVAILABLE;
565                                 }
566
567                                 if ( dolock ) {
568                                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
569                                 }
570
571                                 if ( rc == LDAP_SUCCESS ) {
572                                         ldap_pvt_thread_yield();
573                                         if ( nretries > 0 ) {
574                                                 nretries--;
575                                         }
576                                         goto rebind;
577                                 }
578                         }
579                         break;
580
581                 default:
582                         rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
583                                         NULL, NULL, NULL, NULL, 1 );
584                         if ( rc == LDAP_SUCCESS ) {
585                                 rc = slap_map_api2result( rs );
586                         }
587                         break;
588                 }
589
590         } else {
591                 rs->sr_err = rc;
592                 rc = slap_map_api2result( rs );
593         }
594
595         rs->sr_err = rc;
596         if ( rc != LDAP_SUCCESS && ( sendok & LDAP_BACK_SENDERR ) ) {
597                 send_ldap_result( op, rs );
598         }
599
600         return rc;
601 }
602
603 /*
604  * meta_back_dobind
605  */
606 int
607 meta_back_dobind(
608         Operation               *op,
609         SlapReply               *rs,
610         metaconn_t              *mc,
611         ldap_back_send_t        sendok )
612 {
613         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
614
615         int                     bound = 0,
616                                 i,
617                                 isroot = 0;
618
619         SlapReply               *candidates = meta_back_candidates_get( op );
620
621         if ( be_isroot( op ) ) {
622                 isroot = 1;
623         }
624
625         Debug( LDAP_DEBUG_TRACE,
626                 "%s meta_back_dobind: conn=%ld%s\n",
627                 op->o_log_prefix,
628                 LDAP_BACK_PCONN_ID( mc->mc_conn ),
629                 isroot ? " (isroot)" : "" );
630
631         ldap_pvt_thread_mutex_lock( &mc->mc_mutex );
632
633         /*
634          * all the targets are bound as pseudoroot
635          */
636         if ( mc->mc_authz_target == META_BOUND_ALL ) {
637                 bound = 1;
638                 goto done;
639         }
640
641         for ( i = 0; i < mi->mi_ntargets; i++ ) {
642                 metatarget_t            *mt = &mi->mi_targets[ i ];
643                 metasingleconn_t        *msc = &mc->mc_conns[ i ];
644                 int                     rc, do_retry = 1;
645                 char                    *rootdn = NULL;
646
647                 /*
648                  * Not a candidate
649                  */
650                 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
651                         continue;
652                 }
653
654                 assert( msc->msc_ld != NULL );
655
656                 /*
657                  * If the target is already bound it is skipped
658                  */
659                 if ( LDAP_BACK_CONN_ISBOUND( msc ) || LDAP_BACK_CONN_ISANON( msc ) ) {
660                         ++bound;
661                         continue;
662                 }
663
664 retry:;
665                 if ( isroot && !BER_BVISNULL( &mi->mi_targets[ i ].mt_pseudorootdn ) )
666                 {
667                         Operation       op2 = *op;
668
669                         op2.o_tag = LDAP_REQ_BIND;
670                         op2.o_req_dn = mi->mi_targets[ i ].mt_pseudorootdn;
671                         op2.o_req_ndn = mi->mi_targets[ i ].mt_pseudorootdn;
672                         op2.orb_cred = mi->mi_targets[ i ].mt_pseudorootpw;
673                         op2.orb_method = LDAP_AUTH_SIMPLE;
674
675                         rootdn = mi->mi_targets[ i ].mt_pseudorootdn.bv_val;
676
677                         rc = meta_back_single_bind( &op2, rs, mc, i, 0 );
678
679                 } else {
680                         rc = meta_back_single_dobind( op, rs, mc, i,
681                                 LDAP_BACK_DONTSEND, mt->mt_nretries, 1 );
682                 }
683
684                 if ( rc != LDAP_SUCCESS ) {
685                         char            buf[ SLAP_TEXT_BUFLEN ];
686
687                         if ( rc == LDAP_UNAVAILABLE && do_retry ) {
688                                 do_retry = 0;
689                                 if ( meta_back_retry_lock( op, rs, mc, i, LDAP_BACK_DONTSEND, 0 ) ) {
690                                         goto retry;
691                                 }
692                         }
693
694                         snprintf( buf, sizeof( buf ),
695                                 "meta_back_dobind[%d]: (%s) err=%d.",
696                                 i, rootdn ? rootdn : "anonymous", rc );
697                         Debug( LDAP_DEBUG_ANY,
698                                 "%s %s\n",
699                                 op->o_log_prefix, buf, 0 );
700
701                         /*
702                          * null cred bind should always succeed
703                          * as anonymous, so a failure means
704                          * the target is no longer candidate possibly
705                          * due to technical reasons (remote host down?)
706                          * so better clear the handle
707                          */
708                         /* leave the target candidate, but record the error for later use */
709                         candidates[ i ].sr_err = rc;
710                         if ( META_BACK_ONERR_STOP( mi ) ) {
711                                 bound = 0;
712                                 goto done;
713                         }
714                         continue;
715                 } /* else */
716                 
717                 Debug( LDAP_DEBUG_TRACE,
718                         "%s meta_back_dobind[%d]: "
719                         "(%s)\n",
720                         op->o_log_prefix, i,
721                         rootdn ? rootdn : "anonymous" );
722
723                 if ( rootdn ) {
724                         LDAP_BACK_CONN_ISBOUND_SET( msc );
725                 } else {
726                         LDAP_BACK_CONN_ISANON_SET( msc );
727                 }
728                 ++bound;
729         }
730
731 done:;
732         ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
733
734         Debug( LDAP_DEBUG_TRACE,
735                 "%s meta_back_dobind: conn=%ld bound=%d\n",
736                 op->o_log_prefix, LDAP_BACK_PCONN_ID( mc->mc_conn ), bound );
737
738         if ( bound == 0 ) {
739                 meta_back_release_conn( op, mc );
740
741                 if ( sendok & LDAP_BACK_SENDERR ) {
742                         if ( rs->sr_err == LDAP_SUCCESS ) {
743                                 rs->sr_err = LDAP_BUSY;
744                         }
745                         send_ldap_result( op, rs );
746                 }
747         }
748
749         return( bound > 0 );
750 }
751
752 /*
753  * meta_back_default_rebind
754  *
755  * This is a callback used for chasing referrals using the same
756  * credentials as the original user on this session.
757  */
758 static int 
759 meta_back_default_rebind(
760         LDAP                    *ld,
761         LDAP_CONST char         *url,
762         ber_tag_t               request,
763         ber_int_t               msgid,
764         void                    *params )
765 {
766         metasingleconn_t        *msc = ( metasingleconn_t * )params;
767
768         return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
769                         LDAP_SASL_SIMPLE, &msc->msc_cred,
770                         NULL, NULL, NULL );
771 }
772
773 /*
774  * FIXME: error return must be handled in a cleaner way ...
775  */
776 int
777 meta_back_op_result(
778         metaconn_t      *mc,
779         Operation       *op,
780         SlapReply       *rs,
781         int             candidate )
782 {
783         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
784
785         int                     i,
786                                 rerr = LDAP_SUCCESS;
787         char                    *rmsg = NULL,
788                                 *rmatch = NULL;
789         const char              *save_rmsg = NULL,
790                                 *save_rmatch = NULL;
791         void                    *rmatch_ctx = NULL;
792
793         if ( candidate != META_TARGET_NONE ) {
794                 metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
795
796                 rs->sr_err = LDAP_SUCCESS;
797
798                 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
799                 if ( rs->sr_err != LDAP_SUCCESS ) {
800                         /*
801                          * better check the type of error. In some cases
802                          * (search ?) it might be better to return a
803                          * success if at least one of the targets gave
804                          * positive result ...
805                          */
806                         ldap_get_option( msc->msc_ld,
807                                         LDAP_OPT_ERROR_STRING, &rmsg );
808                         if ( rmsg != NULL && rmsg[ 0 ] == '\0' ) {
809                                 ldap_memfree( rmsg );
810                                 rmsg = NULL;
811                         }
812
813                         ldap_get_option( msc->msc_ld,
814                                         LDAP_OPT_MATCHED_DN, &rmatch );
815                         if ( rmatch != NULL && rmatch[ 0 ] == '\0' ) {
816                                 ldap_memfree( rmatch );
817                                 rmatch = NULL;
818                         }
819
820                         rerr = rs->sr_err = slap_map_api2result( rs );
821
822                         Debug(LDAP_DEBUG_ANY,
823                                         "==> meta_back_op_result: target"
824                                         " <%d> sending msg \"%s\""
825                                         " (matched \"%s\")\n", 
826                                         candidate, ( rmsg ? rmsg : "" ),
827                                         ( rmatch ? rmatch : "" ) );
828                 }
829
830         } else {
831                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
832                         metasingleconn_t        *msc = &mc->mc_conns[ i ];
833                         char                    *msg = NULL;
834                         char                    *match = NULL;
835
836                         rs->sr_err = LDAP_SUCCESS;
837
838                         ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
839                         if ( rs->sr_err != LDAP_SUCCESS ) {
840                                 /*
841                                  * better check the type of error. In some cases
842                                  * (search ?) it might be better to return a
843                                  * success if at least one of the targets gave
844                                  * positive result ...
845                                  */
846                                 ldap_get_option( msc->msc_ld,
847                                                 LDAP_OPT_ERROR_STRING, &msg );
848                                 if ( msg != NULL && msg[ 0 ] == '\0' ) {
849                                         ldap_memfree( msg );
850                                         msg = NULL;
851                                 }
852
853                                 ldap_get_option( msc->msc_ld,
854                                                 LDAP_OPT_MATCHED_DN, &match );
855                                 if ( match != NULL && match[ 0 ] == '\0' ) {
856                                         ldap_memfree( match );
857                                         match = NULL;
858                                 }
859
860                                 rs->sr_err = slap_map_api2result( rs );
861         
862                                 Debug(LDAP_DEBUG_ANY,
863                                                 "==> meta_back_op_result: target"
864                                                 " <%d> sending msg \"%s\""
865                                                 " (matched \"%s\")\n", 
866                                                 i, ( msg ? msg : "" ),
867                                                 ( match ? match : "" ) );
868         
869                                 /*
870                                  * FIXME: need to rewrite "match" (need rwinfo)
871                                  */
872                                 switch ( rs->sr_err ) {
873                                 default:
874                                         rerr = rs->sr_err;
875                                         if ( msg != NULL ) {
876                                                 if ( rmsg ) {
877                                                         ldap_memfree( rmsg );
878                                                 }
879                                                 rmsg = msg;
880                                                 msg = NULL;
881                                         }
882                                         if ( match != NULL ) {
883                                                 if ( rmatch ) {
884                                                         ldap_memfree( rmatch );
885                                                 }
886                                                 rmatch = match;
887                                                 match = NULL;
888                                         }
889                                         break;
890                                 }
891
892                                 if ( msg ) {
893                                         ldap_memfree( msg );
894                                 }
895         
896                                 if ( match ) {
897                                         ldap_memfree( match );
898                                 }
899                         }
900                 }
901         }
902         
903         rs->sr_err = rerr;
904         if ( rmsg != NULL ) {
905                 save_rmsg = rs->sr_text;
906                 rs->sr_text = rmsg;
907         }
908         if ( rmatch != NULL ) {
909                 struct berval   dn, pdn;
910
911                 ber_str2bv( rmatch, 0, 0, &dn );
912                 if ( dnPretty( NULL, &dn, &pdn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
913                         ldap_memfree( rmatch );
914                         rmatch_ctx = op->o_tmpmemctx;
915                         rmatch = pdn.bv_val;
916                 }
917                 save_rmatch = rs->sr_matched;
918                 rs->sr_matched = rmatch;
919         }
920         send_ldap_result( op, rs );
921         if ( rmsg != NULL ) {
922                 ber_memfree( rmsg );
923                 rs->sr_text = save_rmsg;
924         }
925         if ( rmatch != NULL ) {
926                 ber_memfree_x( rmatch, rmatch_ctx );
927                 rs->sr_matched = save_rmatch;
928         }
929
930         return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );
931 }
932