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