]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/bind.c
move req2op selection into a helper
[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 #include "lutil_ldap.h"
38
39 static int
40 meta_back_proxy_authz_bind(
41         metaconn_t              *mc,
42         int                     candidate,
43         Operation               *op,
44         SlapReply               *rs,
45         ldap_back_send_t        sendok );
46
47 static int
48 meta_back_single_bind(
49         Operation               *op,
50         SlapReply               *rs,
51         metaconn_t              *mc,
52         int                     candidate );
53
54 int
55 meta_back_bind( Operation *op, SlapReply *rs )
56 {
57         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
58         metaconn_t      *mc = NULL;
59
60         int             rc = LDAP_OTHER,
61                         i,
62                         gotit = 0,
63                         isroot = 0;
64
65         SlapReply       *candidates = meta_back_candidates_get( op );
66
67         rs->sr_err = LDAP_SUCCESS;
68
69         Debug( LDAP_DEBUG_ARGS, "%s meta_back_bind: dn=\"%s\".\n",
70                 op->o_log_prefix, op->o_req_dn.bv_val, 0 );
71
72         /* the test on the bind method should be superfluous */
73         if ( op->orb_method == LDAP_AUTH_SIMPLE
74                 && be_isroot_dn( op->o_bd, &op->o_req_ndn ) )
75         {
76                 if ( !be_isroot_pw( op ) ) {
77                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
78                         rs->sr_text = NULL;
79                         send_ldap_result( op, rs );
80                         return rs->sr_err;
81                 }
82
83                 if ( META_BACK_DEFER_ROOTDN_BIND( mi ) ) {
84                         rs->sr_err = LDAP_SUCCESS;
85                         rs->sr_text = NULL;
86                         /* frontend will return success */
87                         return rs->sr_err;
88                 }
89
90                 isroot = 1;
91         }
92
93         /* we need meta_back_getconn() not send result even on error,
94          * because we want to intercept the error and make it
95          * invalidCredentials */
96         mc = meta_back_getconn( op, rs, NULL, LDAP_BACK_BIND_DONTSEND );
97         if ( !mc ) {
98                 if ( LogTest( LDAP_DEBUG_ANY ) ) {
99                         char    buf[ SLAP_TEXT_BUFLEN ];
100
101                         snprintf( buf, sizeof( buf ),
102                                 "meta_back_bind: no target "
103                                 "for dn \"%s\" (%d%s%s).",
104                                 op->o_req_dn.bv_val, rs->sr_err,
105                                 rs->sr_text ? ". " : "",
106                                 rs->sr_text ? rs->sr_text : "" );
107                         Debug( LDAP_DEBUG_ANY,
108                                 "%s %s\n",
109                                 op->o_log_prefix, buf, 0 );
110                 }
111
112                 /* FIXME: there might be cases where we don't want
113                  * to map the error onto invalidCredentials */
114                 switch ( rs->sr_err ) {
115                 case LDAP_NO_SUCH_OBJECT:
116                 case LDAP_UNWILLING_TO_PERFORM:
117                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
118                         rs->sr_text = NULL;
119                         break;
120                 }
121                 send_ldap_result( op, rs );
122                 return rs->sr_err;
123         }
124
125         /*
126          * Each target is scanned ...
127          */
128         mc->mc_authz_target = META_BOUND_NONE;
129         for ( i = 0; i < mi->mi_ntargets; i++ ) {
130                 metatarget_t    *mt = mi->mi_targets[ i ];
131                 int             lerr;
132
133                 /*
134                  * Skip non-candidates
135                  */
136                 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
137                         continue;
138                 }
139
140                 if ( gotit == 0 ) {
141                         /* set rc to LDAP_SUCCESS only if at least
142                          * one candidate has been tried */
143                         rc = LDAP_SUCCESS;
144                         gotit = 1;
145
146                 } else if ( isroot == 0 ) {
147                         /*
148                          * A bind operation is expected to have
149                          * ONE CANDIDATE ONLY!
150                          */
151                         Debug( LDAP_DEBUG_ANY,
152                                 "### %s meta_back_bind: more than one"
153                                 " candidate selected...\n",
154                                 op->o_log_prefix, 0, 0 );
155                 }
156
157                 if ( isroot ) {
158                         if ( mt->mt_idassert_authmethod == LDAP_AUTH_NONE
159                                 || BER_BVISNULL( &mt->mt_idassert_authcDN ) )
160                         {
161                                 metasingleconn_t        *msc = &mc->mc_conns[ i ];
162
163                                 /* skip the target if no pseudorootdn is provided */
164                                 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
165                                         ch_free( msc->msc_bound_ndn.bv_val );
166                                         BER_BVZERO( &msc->msc_bound_ndn );
167                                 }
168
169                                 if ( LDAP_BACK_SAVECRED( mi ) &&
170                                         !BER_BVISNULL( &msc->msc_cred ) )
171                                 {
172                                         /* destroy sensitive data */
173                                         memset( msc->msc_cred.bv_val, 0,
174                                                 msc->msc_cred.bv_len );
175                                         ch_free( msc->msc_cred.bv_val );
176                                         BER_BVZERO( &msc->msc_cred );
177                                 }
178
179                                 continue;
180                         }
181
182                         
183                         (void)meta_back_proxy_authz_bind( mc, i, op, rs, LDAP_BACK_DONTSEND );
184                         lerr = rs->sr_err;
185
186                 } else {
187                         lerr = meta_back_single_bind( op, rs, mc, i );
188                 }
189
190                 if ( lerr != LDAP_SUCCESS ) {
191                         rc = rs->sr_err = lerr;
192                         /* FIXME: in some cases (e.g. unavailable)
193                          * do not assume it's not candidate; rather
194                          * mark this as an error to be eventually
195                          * reported to client */
196                         META_CANDIDATE_CLEAR( &candidates[ i ] );
197                         break;
198                 }
199         }
200
201         /* must re-insert if local DN changed as result of bind */
202         if ( rc == LDAP_SUCCESS ) {
203                 if ( isroot ) {
204                         mc->mc_authz_target = META_BOUND_ALL;
205                         ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ) );
206                 }
207
208                 if ( !dn_match( &op->o_req_ndn, &mc->mc_local_ndn ) ) {
209                         metaconn_t      *tmpmc;
210                         int             lerr;
211
212                         /* wait for all other ops to release the connection */
213 retry_lock:;
214                         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
215                         if ( mc->mc_refcnt > 1 ) {
216                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
217                                 ldap_pvt_thread_yield();
218                                 goto retry_lock;
219                         }
220
221                         assert( mc->mc_refcnt == 1 );
222                         tmpmc = avl_delete( &mi->mi_conninfo.lai_tree, (caddr_t)mc,
223                                 meta_back_conndn_cmp );
224                         assert( tmpmc == mc );
225
226                         /* delete all cached connections with the current connection */
227                         if ( LDAP_BACK_SINGLECONN( mi ) ) {
228                                 while ( ( tmpmc = avl_delete( &mi->mi_conninfo.lai_tree, (caddr_t)mc, meta_back_conn_cmp ) ) != NULL )
229                                 {
230                                         Debug( LDAP_DEBUG_TRACE,
231                                                 "=>meta_back_bind: destroying conn %ld (refcnt=%u)\n",
232                                                 LDAP_BACK_PCONN_ID( mc->mc_conn ), mc->mc_refcnt, 0 );
233
234                                         if ( tmpmc->mc_refcnt != 0 ) {
235                                                 /* taint it */
236                                                 LDAP_BACK_CONN_TAINTED_SET( tmpmc );
237
238                                         } else {
239                                                 /*
240                                                  * Needs a test because the handler may be corrupted,
241                                                  * and calling ldap_unbind on a corrupted header results
242                                                  * in a segmentation fault
243                                                  */
244                                                 meta_back_conn_free( tmpmc );
245                                         }
246                                 }
247                         }
248
249                         ber_bvreplace( &mc->mc_local_ndn, &op->o_req_ndn );
250                         if ( isroot ) {
251                                 mc->mc_conn = LDAP_BACK_PCONN_SET( op );
252                         }
253                         lerr = avl_insert( &mi->mi_conninfo.lai_tree, (caddr_t)mc,
254                                 meta_back_conndn_cmp, meta_back_conndn_dup );
255                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
256                         if ( lerr == -1 ) {
257                                 meta_clear_candidates( op, mc );
258
259                                 /* we can do this because mc_refcnt == 1 */
260                                 mc->mc_refcnt = 0;
261                                 meta_back_conn_free( mc );
262                                 mc = NULL;
263                         }
264                 }
265         }
266
267         if ( mc != NULL ) {
268                 meta_back_release_conn( op, mc );
269         }
270
271         /*
272          * rc is LDAP_SUCCESS if at least one bind succeeded,
273          * err is the last error that occurred during a bind;
274          * if at least (and at most?) one bind succeeds, fine.
275          */
276         if ( rc != LDAP_SUCCESS ) {
277                 
278                 /*
279                  * deal with bind failure ...
280                  */
281
282                 /*
283                  * no target was found within the naming context, 
284                  * so bind must fail with invalid credentials
285                  */
286                 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
287                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
288                 } else {
289                         rs->sr_err = slap_map_api2result( rs );
290                 }
291                 send_ldap_result( op, rs );
292                 return rs->sr_err;
293
294         }
295
296         return LDAP_SUCCESS;
297 }
298
299 static int
300 meta_back_bind_op_result(
301         Operation               *op,
302         SlapReply               *rs,
303         metaconn_t              *mc,
304         int                     candidate,
305         int                     msgid,
306         ldap_back_send_t        sendok )
307 {
308         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
309         metatarget_t            *mt = mi->mi_targets[ candidate ];
310         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
311         LDAPMessage             *res;
312         struct timeval          tv;
313         int                     rc;
314         int                     nretries = mt->mt_nretries;
315         char                    buf[ SLAP_TEXT_BUFLEN ];
316
317         Debug( LDAP_DEBUG_TRACE,
318                 ">>> %s meta_back_bind_op_result[%d]\n",
319                 op->o_log_prefix, candidate, 0 );
320
321         if ( rs->sr_err == LDAP_SUCCESS ) {
322                 LDAP_BACK_TV_SET( &tv );
323
324                 /*
325                  * handle response!!!
326                  */
327 retry:;
328                 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
329                 switch ( rc ) {
330                 case 0:
331                         Debug( LDAP_DEBUG_ANY,
332                                 "%s meta_back_bind_op_result[%d]: ldap_result=0 nretries=%d.\n",
333                                 op->o_log_prefix, candidate, nretries );
334
335                         if ( nretries != META_RETRY_NEVER ) {
336                                 ldap_pvt_thread_yield();
337                                 if ( nretries > 0 ) {
338                                         nretries--;
339                                 }
340                                 tv = mt->mt_bind_timeout;
341                                 goto retry;
342                         }
343
344                         /* FIXME: binds cannot be abandoned */
345                         /* don't let anyone else use this handler,
346                          * because there's a pending bind that will not
347                          * be acknowledged */
348                         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
349                         ldap_unbind_ext( msc->msc_ld, NULL, NULL );
350                         msc->msc_ld = NULL;
351                         rs->sr_err = LDAP_BUSY;
352                         LDAP_BACK_CONN_BINDING_CLEAR( msc );
353                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
354                         break;
355
356                 case -1:
357                         ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
358                                 &rs->sr_err );
359
360                         snprintf( buf, sizeof( buf ),
361                                 "err=%d (%s) nretries=%d",
362                                 rs->sr_err, ldap_err2string( rs->sr_err ), nretries );
363                         Debug( LDAP_DEBUG_ANY,
364                                 "### %s meta_back_bind_op_result[%d]: %s.\n",
365                                 op->o_log_prefix, candidate, buf );
366                         break;
367
368                 default:
369                         /* only touch when activity actually took place... */
370                         if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
371                                 msc->msc_time = op->o_time;
372                         }
373
374                         /* FIXME: matched? referrals? response controls? */
375                         rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
376                                         NULL, NULL, NULL, NULL, 1 );
377                         if ( rc != LDAP_SUCCESS ) {
378                                 rs->sr_err = rc;
379                         }
380                         break;
381                 }
382         }
383
384         rs->sr_err = slap_map_api2result( rs );
385
386         Debug( LDAP_DEBUG_TRACE,
387                 "<<< %s meta_back_bind_op_result[%d] err=%d\n",
388                 op->o_log_prefix, candidate, rs->sr_err );
389
390         return rs->sr_err;
391 }
392
393 /*
394  * meta_back_single_bind
395  *
396  * attempts to perform a bind with creds
397  */
398 static int
399 meta_back_single_bind(
400         Operation               *op,
401         SlapReply               *rs,
402         metaconn_t              *mc,
403         int                     candidate )
404 {
405         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
406         metatarget_t            *mt = mi->mi_targets[ candidate ];
407         struct berval           mdn = BER_BVNULL;
408         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
409         int                     msgid;
410         dncookie                dc;
411         
412         if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
413                 ch_free( msc->msc_bound_ndn.bv_val );
414                 BER_BVZERO( &msc->msc_bound_ndn );
415         }
416
417         if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &msc->msc_cred ) ) {
418                 /* destroy sensitive data */
419                 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
420                 ch_free( msc->msc_cred.bv_val );
421                 BER_BVZERO( &msc->msc_cred );
422         }
423
424         /*
425          * Rewrite the bind dn if needed
426          */
427         dc.target = mt;
428         dc.conn = op->o_conn;
429         dc.rs = rs;
430         dc.ctx = "bindDN";
431
432         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
433                 rs->sr_text = "DN rewrite error";
434                 rs->sr_err = LDAP_OTHER;
435                 return rs->sr_err;
436         }
437
438         /* FIXME: this fixes the bind problem right now; we need
439          * to use the asynchronous version to get the "matched"
440          * and more in case of failure ... */
441         /* FIXME: should we check if at least some of the op->o_ctrls
442          * can/should be passed? */
443         rs->sr_err = ldap_sasl_bind( msc->msc_ld, mdn.bv_val,
444                         LDAP_SASL_SIMPLE, &op->orb_cred,
445                         op->o_ctrls, NULL, &msgid );
446         meta_back_bind_op_result( op, rs, mc, candidate, msgid, LDAP_BACK_DONTSEND );
447         if ( rs->sr_err != LDAP_SUCCESS ) {
448                 goto return_results;
449         }
450
451         /* If defined, proxyAuthz will be used also when
452          * back-ldap is the authorizing backend; for this
453          * purpose, a successful bind is followed by a
454          * bind with the configured identity assertion */
455         /* NOTE: use with care */
456         if ( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
457                 meta_back_proxy_authz_bind( mc, candidate, op, rs, LDAP_BACK_SENDERR );
458                 if ( !LDAP_BACK_CONN_ISBOUND( msc ) ) {
459                         goto return_results;
460                 }
461                 goto cache_refresh;
462         }
463
464         ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_ndn );
465         LDAP_BACK_CONN_ISBOUND_SET( msc );
466         mc->mc_authz_target = candidate;
467
468         if ( LDAP_BACK_SAVECRED( mi ) ) {
469                 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
470                 ldap_set_rebind_proc( msc->msc_ld, mt->mt_rebind_f, msc );
471         }
472
473 cache_refresh:;
474         if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
475                         && !BER_BVISEMPTY( &op->o_req_ndn ) )
476         {
477                 ( void )meta_dncache_update_entry( &mi->mi_cache,
478                                 &op->o_req_ndn, candidate );
479         }
480
481 return_results:;
482         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
483                 free( mdn.bv_val );
484         }
485
486         if ( META_BACK_TGT_QUARANTINE( mt ) ) {
487                 meta_back_quarantine( op, rs, candidate );
488         }
489
490         return rs->sr_err;
491 }
492
493 /*
494  * meta_back_single_dobind
495  */
496 int
497 meta_back_single_dobind(
498         Operation               *op,
499         SlapReply               *rs,
500         metaconn_t              **mcp,
501         int                     candidate,
502         ldap_back_send_t        sendok,
503         int                     nretries,
504         int                     dolock )
505 {
506         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
507         metatarget_t            *mt = mi->mi_targets[ candidate ];
508         metaconn_t              *mc = *mcp;
509         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
510         int                     rc;
511         static struct berval    cred = BER_BVC( "" );
512         int                     msgid;
513
514         assert( !LDAP_BACK_CONN_ISBOUND( msc ) );
515
516         /* NOTE: this obsoletes pseudorootdn */
517         if ( op->o_conn != NULL &&
518                 !op->o_do_not_cache &&
519                 ( BER_BVISNULL( &msc->msc_bound_ndn ) ||
520                         BER_BVISEMPTY( &msc->msc_bound_ndn ) ||
521                         ( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
522         {
523                 (void)meta_back_proxy_authz_bind( mc, candidate, op, rs, sendok );
524                 rc = rs->sr_err;
525                 goto done;
526         }
527
528         /* FIXME: should we check if at least some of the op->o_ctrls
529          * can/should be passed? */
530         rs->sr_err = ldap_sasl_bind( msc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
531                         NULL, NULL, &msgid );
532         rc = meta_back_bind_op_result( op, rs, mc, candidate, msgid, sendok );
533
534 done:;
535         rs->sr_err = rc;
536         if ( rc != LDAP_SUCCESS ) {
537                 if ( dolock ) {
538                         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
539                 }
540                 LDAP_BACK_CONN_BINDING_CLEAR( msc );
541                 if ( META_BACK_ONERR_STOP( mi ) ) {
542                         LDAP_BACK_CONN_TAINTED_SET( mc );
543                         meta_back_release_conn_lock( op, mc, 0 );
544                         *mcp = NULL;
545                 }
546                 if ( dolock ) {
547                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
548                 }
549
550                 if ( META_BACK_ONERR_STOP( mi ) && ( sendok & LDAP_BACK_SENDERR ) ) {
551                         send_ldap_result( op, rs );
552                 }
553         }
554
555         if ( META_BACK_TGT_QUARANTINE( mt ) ) {
556                 meta_back_quarantine( op, rs, candidate );
557         }
558
559         return rc;
560 }
561
562 /*
563  * meta_back_dobind
564  */
565 int
566 meta_back_dobind(
567         Operation               *op,
568         SlapReply               *rs,
569         metaconn_t              *mc,
570         ldap_back_send_t        sendok )
571 {
572         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
573
574         int                     bound = 0,
575                                 i,
576                                 isroot = 0;
577
578         SlapReply               *candidates = meta_back_candidates_get( op );
579
580         if ( be_isroot( op ) ) {
581                 isroot = 1;
582         }
583
584         Debug( LDAP_DEBUG_TRACE,
585                 "%s meta_back_dobind: conn=%ld%s\n",
586                 op->o_log_prefix,
587                 LDAP_BACK_PCONN_ID( mc->mc_conn ),
588                 isroot ? " (isroot)" : "" );
589
590         /*
591          * all the targets are bound as pseudoroot
592          */
593         if ( mc->mc_authz_target == META_BOUND_ALL ) {
594                 bound = 1;
595                 goto done;
596         }
597
598         for ( i = 0; i < mi->mi_ntargets; i++ ) {
599                 metatarget_t            *mt = mi->mi_targets[ i ];
600                 metasingleconn_t        *msc = &mc->mc_conns[ i ];
601                 int                     rc;
602
603                 /*
604                  * Not a candidate
605                  */
606                 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
607                         continue;
608                 }
609
610                 assert( msc->msc_ld != NULL );
611
612                 /*
613                  * If the target is already bound it is skipped
614                  */
615
616 retry_binding:;
617                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
618                 if ( LDAP_BACK_CONN_ISBOUND( msc )
619                         || ( LDAP_BACK_CONN_ISANON( msc )
620                                 && mt->mt_idassert_authmethod == LDAP_AUTH_NONE ) )
621                 {
622                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
623                         ++bound;
624                         continue;
625
626                 } else if ( LDAP_BACK_CONN_BINDING( msc ) ) {
627                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
628                         ldap_pvt_thread_yield();
629                         goto retry_binding;
630
631                 }
632
633                 LDAP_BACK_CONN_BINDING_SET( msc );
634                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
635
636                 rc = meta_back_single_dobind( op, rs, &mc, i,
637                         LDAP_BACK_DONTSEND, mt->mt_nretries, 1 );
638                 /*
639                  * NOTE: meta_back_single_dobind() already retries;
640                  * in case of failure, it resets mc...
641                  */
642                 if ( rc != LDAP_SUCCESS ) {
643                         char            buf[ SLAP_TEXT_BUFLEN ];
644
645                         if ( mc == NULL ) {
646                                 /* meta_back_single_dobind() already sent 
647                                  * response and released connection */
648                                 goto send_err;
649                         }
650
651
652                         if ( rc == LDAP_UNAVAILABLE ) {
653                                 /* FIXME: meta_back_retry() already calls
654                                  * meta_back_single_dobind() */
655                                 if ( meta_back_retry( op, rs, &mc, i, sendok ) ) {
656                                         goto retry_ok;
657                                 }
658
659                                 if ( mc != NULL ) {
660                                         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
661                                         LDAP_BACK_CONN_BINDING_CLEAR( msc );
662                                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
663                                 }
664
665                                 return 0;
666                         }
667
668                         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
669                         LDAP_BACK_CONN_BINDING_CLEAR( msc );
670                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
671
672                         snprintf( buf, sizeof( buf ),
673                                 "meta_back_dobind[%d]: (%s) err=%d (%s).",
674                                 i, isroot ? op->o_bd->be_rootdn.bv_val : "anonymous",
675                                 rc, ldap_err2string( rc ) );
676                         Debug( LDAP_DEBUG_ANY,
677                                 "%s %s\n",
678                                 op->o_log_prefix, buf, 0 );
679
680                         /*
681                          * null cred bind should always succeed
682                          * as anonymous, so a failure means
683                          * the target is no longer candidate possibly
684                          * due to technical reasons (remote host down?)
685                          * so better clear the handle
686                          */
687                         /* leave the target candidate, but record the error for later use */
688                         candidates[ i ].sr_err = rc;
689                         if ( META_BACK_ONERR_STOP( mi ) ) {
690                                 bound = 0;
691                                 goto done;
692                         }
693
694                         continue;
695                 } /* else */
696
697 retry_ok:;
698                 Debug( LDAP_DEBUG_TRACE,
699                         "%s meta_back_dobind[%d]: "
700                         "(%s)\n",
701                         op->o_log_prefix, i,
702                         isroot ? op->o_bd->be_rootdn.bv_val : "anonymous" );
703
704                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
705                 LDAP_BACK_CONN_BINDING_CLEAR( msc );
706                 if ( isroot ) {
707                         LDAP_BACK_CONN_ISBOUND_SET( msc );
708                 } else {
709                         LDAP_BACK_CONN_ISANON_SET( msc );
710                 }
711                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
712                 ++bound;
713         }
714
715 done:;
716         Debug( LDAP_DEBUG_TRACE,
717                 "%s meta_back_dobind: conn=%ld bound=%d\n",
718                 op->o_log_prefix, LDAP_BACK_PCONN_ID( mc->mc_conn ), bound );
719
720         if ( bound == 0 ) {
721                 meta_back_release_conn( op, mc );
722
723 send_err:;
724                 if ( sendok & LDAP_BACK_SENDERR ) {
725                         if ( rs->sr_err == LDAP_SUCCESS ) {
726                                 rs->sr_err = LDAP_BUSY;
727                         }
728                         send_ldap_result( op, rs );
729                 }
730
731                 return 0;
732         }
733
734         return ( bound > 0 );
735 }
736
737 /*
738  * meta_back_default_rebind
739  *
740  * This is a callback used for chasing referrals using the same
741  * credentials as the original user on this session.
742  */
743 int 
744 meta_back_default_rebind(
745         LDAP                    *ld,
746         LDAP_CONST char         *url,
747         ber_tag_t               request,
748         ber_int_t               msgid,
749         void                    *params )
750 {
751         metasingleconn_t        *msc = ( metasingleconn_t * )params;
752
753         return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
754                         LDAP_SASL_SIMPLE, &msc->msc_cred,
755                         NULL, NULL, NULL );
756 }
757
758 /*
759  * meta_back_default_urllist
760  *
761  * This is a callback used for mucking with the urllist
762  */
763 int 
764 meta_back_default_urllist(
765         LDAP            *ld,
766         LDAPURLDesc     **urllist,
767         LDAPURLDesc     **url,
768         void            *params )
769 {
770         metatarget_t    *mt = (metatarget_t *)params;
771         LDAPURLDesc     **urltail;
772
773         if ( urllist == url ) {
774                 return LDAP_SUCCESS;
775         }
776
777         for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
778                 /* count */ ;
779
780         *urltail = *urllist;
781         *urllist = *url;
782         *url = NULL;
783
784         ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
785         if ( mt->mt_uri ) {
786                 ch_free( mt->mt_uri );
787         }
788
789         ldap_get_option( ld, LDAP_OPT_URI, (void *)&mt->mt_uri );
790         ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
791
792         return LDAP_SUCCESS;
793 }
794
795 int
796 meta_back_cancel(
797         metaconn_t              *mc,
798         Operation               *op,
799         SlapReply               *rs,
800         ber_int_t               msgid,
801         int                     candidate,
802         ldap_back_send_t        sendok )
803 {
804         metainfo_t              *mi = (metainfo_t *)op->o_bd->be_private;
805
806         metatarget_t            *mt = mi->mi_targets[ candidate ];
807         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
808
809         int                     rc = LDAP_OTHER;
810
811         Debug( LDAP_DEBUG_TRACE, ">>> %s meta_back_cancel[%d] msgid=%d\n",
812                 op->o_log_prefix, candidate, msgid );
813
814         /* default behavior */
815         if ( META_BACK_TGT_ABANDON( mt ) ) {
816                 rc = ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
817
818         } else if ( META_BACK_TGT_IGNORE( mt ) ) {
819                 rc = LDAP_SUCCESS;
820
821         } else if ( META_BACK_TGT_CANCEL( mt ) ) {
822                 rc = ldap_cancel_s( msc->msc_ld, msgid, NULL, NULL );
823
824         } else {
825                 assert( 0 );
826         }
827
828         Debug( LDAP_DEBUG_TRACE, "<<< %s meta_back_cancel[%d] err=%d\n",
829                 op->o_log_prefix, candidate, rc );
830
831         return rc;
832 }
833
834
835
836 /*
837  * FIXME: error return must be handled in a cleaner way ...
838  */
839 int
840 meta_back_op_result(
841         metaconn_t              *mc,
842         Operation               *op,
843         SlapReply               *rs,
844         int                     candidate,
845         ber_int_t               msgid,
846         time_t                  timeout,
847         ldap_back_send_t        sendok )
848 {
849         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
850
851         const char      *save_text = rs->sr_text,
852                         *save_matched = rs->sr_matched;
853         BerVarray       save_ref = rs->sr_ref;
854         LDAPControl     **save_ctrls = rs->sr_ctrls;
855         void            *matched_ctx = NULL;
856
857         char            *matched = NULL;
858         char            *text = NULL;
859         char            **refs = NULL;
860         LDAPControl     **ctrls = NULL;
861
862         assert( mc != NULL );
863
864         rs->sr_text = NULL;
865         rs->sr_matched = NULL;
866         rs->sr_ref = NULL;
867         rs->sr_ctrls = NULL;
868
869         if ( candidate != META_TARGET_NONE ) {
870                 metatarget_t            *mt = mi->mi_targets[ candidate ];
871                 metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
872
873 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
874
875                 if ( ERR_OK( rs->sr_err ) ) {
876                         int             rc;
877                         struct timeval  tv;
878                         LDAPMessage     *res = NULL;
879
880                         if ( timeout ) {
881                                 tv.tv_sec = timeout;
882                                 tv.tv_usec = 0;
883
884                         } else {
885                                 LDAP_BACK_TV_SET( &tv );
886                         }
887
888 retry:;
889                         rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
890                         switch ( rc ) {
891                         case 0:
892                                 if ( timeout ) {
893                                         (void)meta_back_cancel( mc, op, rs, msgid, candidate, sendok );
894                                         rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
895                                                 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
896                                         rs->sr_text = "Operation timed out";
897                                         break;
898                                 }
899
900                                 LDAP_BACK_TV_SET( &tv );
901                                 ldap_pvt_thread_yield();
902                                 goto retry;
903
904                         case -1:
905                                 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
906                                                 &rs->sr_err );
907                                 break;
908
909
910                         /* otherwise get the result; if it is not
911                          * LDAP_SUCCESS, record it in the reply
912                          * structure (this includes 
913                          * LDAP_COMPARE_{TRUE|FALSE}) */
914                         default:
915                                 /* only touch when activity actually took place... */
916                                 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
917                                         msc->msc_time = op->o_time;
918                                 }
919
920                                 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
921                                                 &matched, &text, &refs, &ctrls, 1 );
922                                 res = NULL;
923                                 rs->sr_text = text;
924                                 if ( rc != LDAP_SUCCESS ) {
925                                         rs->sr_err = rc;
926                                 }
927                                 if ( refs != NULL ) {
928                                         int     i;
929         
930                                         for ( i = 0; refs[ i ] != NULL; i++ )
931                                                 /* count */ ;
932                                         rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
933                                                 op->o_tmpmemctx );
934                                         for ( i = 0; refs[ i ] != NULL; i++ ) {
935                                                 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
936                                         }
937                                         BER_BVZERO( &rs->sr_ref[ i ] );
938                                 }
939                                 if ( ctrls != NULL ) {
940                                         rs->sr_ctrls = ctrls;
941                                 }
942                         }
943
944                         assert( res == NULL );
945                 }
946
947                 /* if the error in the reply structure is not
948                  * LDAP_SUCCESS, try to map it from client 
949                  * to server error */
950                 if ( !ERR_OK( rs->sr_err ) ) {
951                         rs->sr_err = slap_map_api2result( rs );
952
953                         /* internal ops ( op->o_conn == NULL ) 
954                          * must not reply to client */
955                         if ( op->o_conn && !op->o_do_not_cache && matched ) {
956
957                                 /* record the (massaged) matched
958                                  * DN into the reply structure */
959                                 rs->sr_matched = matched;
960                         }
961                 }
962
963                 if ( META_BACK_TGT_QUARANTINE( mt ) ) {
964                         meta_back_quarantine( op, rs, candidate );
965                 }
966
967         } else {
968                 int     i,
969                         err = rs->sr_err;
970
971                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
972                         metasingleconn_t        *msc = &mc->mc_conns[ i ];
973                         char                    *xtext = NULL;
974                         char                    *xmatched = NULL;
975
976                         rs->sr_err = LDAP_SUCCESS;
977
978                         ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
979                         if ( rs->sr_err != LDAP_SUCCESS ) {
980                                 /*
981                                  * better check the type of error. In some cases
982                                  * (search ?) it might be better to return a
983                                  * success if at least one of the targets gave
984                                  * positive result ...
985                                  */
986                                 ldap_get_option( msc->msc_ld,
987                                                 LDAP_OPT_ERROR_STRING, &xtext );
988                                 if ( xtext != NULL && xtext [ 0 ] == '\0' ) {
989                                         ldap_memfree( xtext );
990                                         xtext = NULL;
991                                 }
992
993                                 ldap_get_option( msc->msc_ld,
994                                                 LDAP_OPT_MATCHED_DN, &xmatched );
995                                 if ( xmatched != NULL && xmatched[ 0 ] == '\0' ) {
996                                         ldap_memfree( xmatched );
997                                         xmatched = NULL;
998                                 }
999
1000                                 rs->sr_err = slap_map_api2result( rs );
1001         
1002                                 if ( LogTest( LDAP_DEBUG_ANY ) ) {
1003                                         char    buf[ SLAP_TEXT_BUFLEN ];
1004
1005                                         snprintf( buf, sizeof( buf ),
1006                                                 "meta_back_op_result[%d] "
1007                                                 "err=%d text=\"%s\" matched=\"%s\"", 
1008                                                 i, rs->sr_err,
1009                                                 ( xtext ? xtext : "" ),
1010                                                 ( xmatched ? xmatched : "" ) );
1011                                         Debug( LDAP_DEBUG_ANY, "%s %s.\n",
1012                                                 op->o_log_prefix, buf, 0 );
1013                                 }
1014
1015                                 /*
1016                                  * FIXME: need to rewrite "match" (need rwinfo)
1017                                  */
1018                                 switch ( rs->sr_err ) {
1019                                 default:
1020                                         err = rs->sr_err;
1021                                         if ( xtext != NULL ) {
1022                                                 if ( text ) {
1023                                                         ldap_memfree( text );
1024                                                 }
1025                                                 text = xtext;
1026                                                 xtext = NULL;
1027                                         }
1028                                         if ( xmatched != NULL ) {
1029                                                 if ( matched ) {
1030                                                         ldap_memfree( matched );
1031                                                 }
1032                                                 matched = xmatched;
1033                                                 xmatched = NULL;
1034                                         }
1035                                         break;
1036                                 }
1037
1038                                 if ( xtext ) {
1039                                         ldap_memfree( xtext );
1040                                 }
1041         
1042                                 if ( xmatched ) {
1043                                         ldap_memfree( xmatched );
1044                                 }
1045                         }
1046
1047                         if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ i ] ) ) {
1048                                 meta_back_quarantine( op, rs, i );
1049                         }
1050                 }
1051
1052                 if ( err != LDAP_SUCCESS ) {
1053                         rs->sr_err = err;
1054                 }
1055         }
1056
1057         if ( matched != NULL ) {
1058                 struct berval   dn, pdn;
1059
1060                 ber_str2bv( matched, 0, 0, &dn );
1061                 if ( dnPretty( NULL, &dn, &pdn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
1062                         ldap_memfree( matched );
1063                         matched_ctx = op->o_tmpmemctx;
1064                         matched = pdn.bv_val;
1065                 }
1066                 rs->sr_matched = matched;
1067         }
1068
1069         if ( op->o_conn &&
1070                 ( ( sendok & LDAP_BACK_SENDOK ) 
1071                         || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1072         {
1073                 send_ldap_result( op, rs );
1074         }
1075         if ( matched ) {
1076                 op->o_tmpfree( (char *)rs->sr_matched, matched_ctx );
1077         }
1078         if ( text ) {
1079                 ldap_memfree( text );
1080         }
1081         if ( rs->sr_ref ) {
1082                 assert( refs != NULL );
1083                 ber_memvfree( (void **)refs );
1084                 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1085         }
1086         if ( ctrls ) {
1087                 assert( rs->sr_ctrls != NULL );
1088                 ldap_controls_free( ctrls );
1089         }
1090
1091         rs->sr_text = save_text;
1092         rs->sr_matched = save_matched;
1093         rs->sr_ref = save_ref;
1094         rs->sr_ctrls = save_ctrls;
1095
1096         return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1097 }
1098
1099 /*
1100  * meta_back_proxy_authz_cred()
1101  *
1102  * prepares credentials & method for meta_back_proxy_authz_bind();
1103  * or, if method is SASL, performs the SASL bind directly.
1104  */
1105 int
1106 meta_back_proxy_authz_cred(
1107         metaconn_t              *mc,
1108         int                     candidate,
1109         Operation               *op,
1110         SlapReply               *rs,
1111         ldap_back_send_t        sendok,
1112         struct berval           *binddn,
1113         struct berval           *bindcred,
1114         int                     *method )
1115 {
1116         metainfo_t              *mi = (metainfo_t *)op->o_bd->be_private;
1117         metatarget_t            *mt = mi->mi_targets[ candidate ];
1118         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
1119         struct berval           ndn;
1120         int                     dobind = 0;
1121
1122         /* don't proxyAuthz if protocol is not LDAPv3 */
1123         switch ( mt->mt_version ) {
1124         case LDAP_VERSION3:
1125                 break;
1126
1127         case 0:
1128                 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1129                         break;
1130                 }
1131                 /* fall thru */
1132
1133         default:
1134                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1135                 if ( sendok & LDAP_BACK_SENDERR ) {
1136                         send_ldap_result( op, rs );
1137                 }
1138                 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1139                 goto done;
1140         }
1141
1142         if ( op->o_tag == LDAP_REQ_BIND ) {
1143                 ndn = op->o_req_ndn;
1144
1145         } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1146                 ndn = op->o_conn->c_ndn;
1147
1148         } else {
1149                 ndn = op->o_ndn;
1150         }
1151
1152         /*
1153          * FIXME: we need to let clients use proxyAuthz
1154          * otherwise we cannot do symmetric pools of servers;
1155          * we have to live with the fact that a user can
1156          * authorize itself as any ID that is allowed
1157          * by the authzTo directive of the "proxyauthzdn".
1158          */
1159         /*
1160          * NOTE: current Proxy Authorization specification
1161          * and implementation do not allow proxy authorization
1162          * control to be provided with Bind requests
1163          */
1164         /*
1165          * if no bind took place yet, but the connection is bound
1166          * and the "proxyauthzdn" is set, then bind as 
1167          * "proxyauthzdn" and explicitly add the proxyAuthz 
1168          * control to every operation with the dn bound 
1169          * to the connection as control value.
1170          */
1171
1172         /* bind as proxyauthzdn only if no idassert mode
1173          * is requested, or if the client's identity
1174          * is authorized */
1175         switch ( mt->mt_idassert_mode ) {
1176         case LDAP_BACK_IDASSERT_LEGACY:
1177                 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1178                         if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) && !BER_BVISEMPTY( &mt->mt_idassert_authcDN ) )
1179                         {
1180                                 *binddn = mt->mt_idassert_authcDN;
1181                                 *bindcred = mt->mt_idassert_passwd;
1182                                 dobind = 1;
1183                         }
1184                 }
1185                 break;
1186
1187         default:
1188                 /* NOTE: rootdn can always idassert */
1189                 if ( BER_BVISNULL( &ndn ) && mt->mt_idassert_authz == NULL ) {
1190                         if ( mt->mt_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1191                                 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1192                                 if ( sendok & LDAP_BACK_SENDERR ) {
1193                                         send_ldap_result( op, rs );
1194                                 }
1195                                 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1196
1197                         } else {
1198                                 rs->sr_err = LDAP_SUCCESS;
1199                                 *binddn = slap_empty_bv;
1200                                 *bindcred = slap_empty_bv;
1201                                 break;
1202                         }
1203
1204                         goto done;
1205
1206                 } else if ( mt->mt_idassert_authz && !be_isroot( op ) ) {
1207                         struct berval authcDN;
1208
1209                         if ( BER_BVISNULL( &ndn ) ) {
1210                                 authcDN = slap_empty_bv;
1211
1212                         } else {
1213                                 authcDN = ndn;
1214                         }       
1215                         rs->sr_err = slap_sasl_matches( op, mt->mt_idassert_authz,
1216                                         &authcDN, &authcDN );
1217                         if ( rs->sr_err != LDAP_SUCCESS ) {
1218                                 if ( mt->mt_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1219                                         if ( sendok & LDAP_BACK_SENDERR ) {
1220                                                 send_ldap_result( op, rs );
1221                                         }
1222                                         LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1223
1224                                 } else {
1225                                         rs->sr_err = LDAP_SUCCESS;
1226                                         *binddn = slap_empty_bv;
1227                                         *bindcred = slap_empty_bv;
1228                                         break;
1229                                 }
1230
1231                                 goto done;
1232                         }
1233                 }
1234
1235                 *binddn = mt->mt_idassert_authcDN;
1236                 *bindcred = mt->mt_idassert_passwd;
1237                 dobind = 1;
1238                 break;
1239         }
1240
1241         if ( dobind && mt->mt_idassert_authmethod == LDAP_AUTH_SASL ) {
1242 #ifdef HAVE_CYRUS_SASL
1243                 void            *defaults = NULL;
1244                 struct berval   authzID = BER_BVNULL;
1245                 int             freeauthz = 0;
1246
1247                 /* if SASL supports native authz, prepare for it */
1248                 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1249                                 ( mt->mt_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1250                 {
1251                         switch ( mt->mt_idassert_mode ) {
1252                         case LDAP_BACK_IDASSERT_OTHERID:
1253                         case LDAP_BACK_IDASSERT_OTHERDN:
1254                                 authzID = mt->mt_idassert_authzID;
1255                                 break;
1256
1257                         case LDAP_BACK_IDASSERT_ANONYMOUS:
1258                                 BER_BVSTR( &authzID, "dn:" );
1259                                 break;
1260
1261                         case LDAP_BACK_IDASSERT_SELF:
1262                                 if ( BER_BVISNULL( &ndn ) ) {
1263                                         /* connection is not authc'd, so don't idassert */
1264                                         BER_BVSTR( &authzID, "dn:" );
1265                                         break;
1266                                 }
1267                                 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1268                                 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1269                                 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1270                                 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1271                                                 ndn.bv_val, ndn.bv_len + 1 );
1272                                 freeauthz = 1;
1273                                 break;
1274
1275                         default:
1276                                 break;
1277                         }
1278                 }
1279
1280                 if ( mt->mt_idassert_secprops != NULL ) {
1281                         rs->sr_err = ldap_set_option( msc->msc_ld,
1282                                 LDAP_OPT_X_SASL_SECPROPS,
1283                                 (void *)mt->mt_idassert_secprops );
1284
1285                         if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1286                                 rs->sr_err = LDAP_OTHER;
1287                                 if ( sendok & LDAP_BACK_SENDERR ) {
1288                                         send_ldap_result( op, rs );
1289                                 }
1290                                 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1291                                 goto done;
1292                         }
1293                 }
1294
1295                 defaults = lutil_sasl_defaults( msc->msc_ld,
1296                                 mt->mt_idassert_sasl_mech.bv_val,
1297                                 mt->mt_idassert_sasl_realm.bv_val,
1298                                 mt->mt_idassert_authcID.bv_val,
1299                                 mt->mt_idassert_passwd.bv_val,
1300                                 authzID.bv_val );
1301
1302                 rs->sr_err = ldap_sasl_interactive_bind_s( msc->msc_ld, binddn->bv_val,
1303                                 mt->mt_idassert_sasl_mech.bv_val, NULL, NULL,
1304                                 LDAP_SASL_QUIET, lutil_sasl_interact,
1305                                 defaults );
1306
1307                 rs->sr_err = slap_map_api2result( rs );
1308                 if ( rs->sr_err != LDAP_SUCCESS ) {
1309                         LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1310                         if ( sendok & LDAP_BACK_SENDERR ) {
1311                                 send_ldap_result( op, rs );
1312                         }
1313
1314                 } else {
1315                         LDAP_BACK_CONN_ISBOUND_SET( msc );
1316                 }
1317
1318                 lutil_sasl_freedefs( defaults );
1319                 if ( freeauthz ) {
1320                         slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1321                 }
1322
1323                 goto done;
1324 #endif /* HAVE_CYRUS_SASL */
1325         }
1326
1327         *method = mt->mt_idassert_authmethod;
1328         switch ( mt->mt_idassert_authmethod ) {
1329         case LDAP_AUTH_NONE:
1330                 BER_BVSTR( binddn, "" );
1331                 BER_BVSTR( bindcred, "" );
1332                 /* fallthru */
1333
1334         case LDAP_AUTH_SIMPLE:
1335                 break;
1336
1337         default:
1338                 /* unsupported! */
1339                 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1340                 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1341                 if ( sendok & LDAP_BACK_SENDERR ) {
1342                         send_ldap_result( op, rs );
1343                 }
1344                 break;
1345         }
1346
1347 done:;
1348         return rs->sr_err;
1349 }
1350
1351 static int
1352 meta_back_proxy_authz_bind( metaconn_t *mc, int candidate, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1353 {
1354         metainfo_t              *mi = (metainfo_t *)op->o_bd->be_private;
1355         metatarget_t            *mt = mi->mi_targets[ candidate ];
1356         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
1357         struct berval           binddn = BER_BVC( "" ),
1358                                 cred = BER_BVC( "" );
1359         int                     method = LDAP_AUTH_NONE,
1360                                 rc;
1361
1362         rc = meta_back_proxy_authz_cred( mc, candidate, op, rs, sendok, &binddn, &cred, &method );
1363         if ( rc == LDAP_SUCCESS && !LDAP_BACK_CONN_ISBOUND( msc ) ) {
1364                 int     msgid;
1365
1366                 switch ( method ) {
1367                 case LDAP_AUTH_NONE:
1368                 case LDAP_AUTH_SIMPLE:
1369                         rs->sr_err = ldap_sasl_bind( msc->msc_ld,
1370                                         binddn.bv_val, LDAP_SASL_SIMPLE,
1371                                         &cred, NULL, NULL, &msgid );
1372                         rc = meta_back_bind_op_result( op, rs, mc, candidate, msgid, sendok );
1373                         if ( rc == LDAP_SUCCESS ) {
1374                                 /* set rebind stuff in case of successful proxyAuthz bind,
1375                                  * so that referral chasing is attempted using the right
1376                                  * identity */
1377                                 LDAP_BACK_CONN_ISBOUND_SET( msc );
1378                                 ber_bvreplace( &msc->msc_bound_ndn, &binddn );
1379
1380                                 if ( LDAP_BACK_SAVECRED( mi ) ) {
1381                                         if ( !BER_BVISNULL( &msc->msc_cred ) ) {
1382                                                 memset( msc->msc_cred.bv_val, 0,
1383                                                         msc->msc_cred.bv_len );
1384                                         }
1385                                         ber_bvreplace( &msc->msc_cred, &cred );
1386                                         ldap_set_rebind_proc( msc->msc_ld, mt->mt_rebind_f, msc );
1387                                 }
1388                         }
1389                         break;
1390
1391                 default:
1392                         assert( 0 );
1393                         break;
1394                 }
1395         }
1396
1397         return LDAP_BACK_CONN_ISBOUND( msc );
1398 }