Debian Codegraph @

servers/slapd/back-meta/bind.c

raw
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 *
4 * Copyright 1999-2024 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 int dolock );
47
48 static int
49 meta_back_single_bind(
50 Operation *op,
51 SlapReply *rs,
52 metaconn_t *mc,
53 int candidate );
54
55 int
56 meta_back_bind( Operation *op, SlapReply *rs )
57 {
58 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
59 metaconn_t *mc = NULL;
60
61 int rc = LDAP_OTHER,
62 i,
63 gotit = 0,
64 isroot = 0;
65
66 SlapReply *candidates;
67
68 rs->sr_err = LDAP_SUCCESS;
69
70 Debug( LDAP_DEBUG_ARGS, "%s meta_back_bind: dn=\"%s\".\n",
71 op->o_log_prefix, op->o_req_dn.bv_val );
72
73 /* the test on the bind method should be superfluous */
74 switch ( be_rootdn_bind( op, rs ) ) {
75 case LDAP_SUCCESS:
76 if ( META_BACK_DEFER_ROOTDN_BIND( mi ) ) {
77 /* frontend will return success */
78 return rs->sr_err;
79 }
80
81 isroot = 1;
82 /* fallthru */
83
84 case SLAP_CB_CONTINUE:
85 break;
86
87 default:
88 /* be_rootdn_bind() sent result */
89 return rs->sr_err;
90 }
91
92 candidates = meta_back_candidates_get( op );
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, candidates );
97 if ( !mc ) {
98 Debug(LDAP_DEBUG_ANY,
99 "%s meta_back_bind: no target " "for dn \"%s\" (%d%s%s).\n",
100 op->o_log_prefix, op->o_req_dn.bv_val,
101 rs->sr_err, rs->sr_text ? ". " : "",
102 rs->sr_text ? rs->sr_text : "" );
103
104 /* FIXME: there might be cases where we don't want
105 * to map the error onto invalidCredentials */
106 switch ( rs->sr_err ) {
107 case LDAP_NO_SUCH_OBJECT:
108 case LDAP_UNWILLING_TO_PERFORM:
109 rs->sr_err = LDAP_INVALID_CREDENTIALS;
110 rs->sr_text = NULL;
111 break;
112 }
113 send_ldap_result( op, rs );
114 op->o_tmpfree( candidates, op->o_tmpmemctx );
115 return rs->sr_err;
116 }
117
118 /*
119 * Each target is scanned ...
120 */
121 mc->mc_authz_target = META_BOUND_NONE;
122 for ( i = 0; i < mi->mi_ntargets; i++ ) {
123 metatarget_t *mt = mi->mi_targets[ i ];
124 int lerr;
125
126 /*
127 * Skip non-candidates
128 */
129 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
130 continue;
131 }
132
133 if ( gotit == 0 ) {
134 /* set rc to LDAP_SUCCESS only if at least
135 * one candidate has been tried */
136 rc = LDAP_SUCCESS;
137 gotit = 1;
138
139 } else if ( !isroot ) {
140 /*
141 * A bind operation is expected to have
142 * ONE CANDIDATE ONLY!
143 */
144 Debug( LDAP_DEBUG_ANY,
145 "### %s meta_back_bind: more than one"
146 " candidate selected...\n",
147 op->o_log_prefix );
148 }
149
150 if ( isroot ) {
151 if ( mt->mt_idassert_authmethod == LDAP_AUTH_NONE
152 || BER_BVISNULL( &mt->mt_idassert_authcDN ) )
153 {
154 metasingleconn_t *msc = &mc->mc_conns[ i ];
155
156 /* skip the target if no pseudorootdn is provided */
157 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
158 ch_free( msc->msc_bound_ndn.bv_val );
159 BER_BVZERO( &msc->msc_bound_ndn );
160 }
161
162 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
163 /* destroy sensitive data */
164 memset( msc->msc_cred.bv_val, 0,
165 msc->msc_cred.bv_len );
166 ch_free( msc->msc_cred.bv_val );
167 BER_BVZERO( &msc->msc_cred );
168 }
169
170 continue;
171 }
172
173
174 (void)meta_back_proxy_authz_bind( mc, i, op, rs, LDAP_BACK_DONTSEND, 1 );
175 lerr = rs->sr_err;
176
177 } else {
178 lerr = meta_back_single_bind( op, rs, mc, i );
179 }
180
181 if ( lerr != LDAP_SUCCESS ) {
182 rc = rs->sr_err = lerr;
183
184 /* FIXME: in some cases (e.g. unavailable)
185 * do not assume it's not candidate; rather
186 * mark this as an error to be eventually
187 * reported to client */
188 META_CANDIDATE_CLEAR( &candidates[ i ] );
189 break;
190 }
191 }
192
193 /* must re-insert if local DN changed as result of bind */
194 if ( rc == LDAP_SUCCESS ) {
195 if ( isroot ) {
196 mc->mc_authz_target = META_BOUND_ALL;
197 }
198
199 if ( !LDAP_BACK_PCONN_ISPRIV( mc )
200 && !dn_match( &op->o_req_ndn, &mc->mc_local_ndn ) )
201 {
202 int lerr;
203
204 /* wait for all other ops to release the connection */
205 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
206 assert( mc->mc_refcnt == 1 );
207 #if META_BACK_PRINT_CONNTREE > 0
208 meta_back_print_conntree( mi, ">>> meta_back_bind" );
209 #endif /* META_BACK_PRINT_CONNTREE */
210
211 /* delete all cached connections with the current connection */
212 if ( LDAP_BACK_SINGLECONN( mi ) ) {
213 metaconn_t *tmpmc;
214
215 while ( ( tmpmc = ldap_tavl_delete( &mi->mi_conninfo.lai_tree, (caddr_t)mc, meta_back_conn_cmp ) ) != NULL )
216 {
217 assert( !LDAP_BACK_PCONN_ISPRIV( mc ) );
218 Debug( LDAP_DEBUG_TRACE,
219 "=>meta_back_bind: destroying conn %lu (refcnt=%u)\n",
220 mc->mc_conn->c_connid, mc->mc_refcnt );
221
222 if ( tmpmc->mc_refcnt != 0 ) {
223 /* taint it */
224 LDAP_BACK_CONN_TAINTED_SET( tmpmc );
225
226 } else {
227 /*
228 * Needs a test because the handler may be corrupted,
229 * and calling ldap_unbind on a corrupted header results
230 * in a segmentation fault
231 */
232 meta_back_conn_free( tmpmc );
233 }
234 }
235 }
236
237 ber_bvreplace( &mc->mc_local_ndn, &op->o_req_ndn );
238 lerr = ldap_tavl_insert( &mi->mi_conninfo.lai_tree, (caddr_t)mc,
239 meta_back_conndn_cmp, meta_back_conndn_dup );
240 #if META_BACK_PRINT_CONNTREE > 0
241 meta_back_print_conntree( mi, "<<< meta_back_bind" );
242 #endif /* META_BACK_PRINT_CONNTREE */
243 if ( lerr == 0 ) {
244 #if 0
245 /* NOTE: a connection cannot be privileged
246 * and be in the avl tree at the same time
247 */
248 if ( isroot ) {
249 LDAP_BACK_CONN_ISPRIV_SET( mc );
250 LDAP_BACK_PCONN_SET( mc, op );
251 }
252 #endif
253 LDAP_BACK_CONN_CACHED_SET( mc );
254
255 } else {
256 LDAP_BACK_CONN_CACHED_CLEAR( mc );
257 }
258 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
259 }
260 }
261
262 if ( mc != NULL ) {
263 meta_back_release_conn( mi, mc );
264 }
265
266 /*
267 * rc is LDAP_SUCCESS if at least one bind succeeded,
268 * err is the last error that occurred during a bind;
269 * if at least (and at most?) one bind succeeds, fine.
270 */
271 if ( rc != LDAP_SUCCESS ) {
272
273 /*
274 * deal with bind failure ...
275 */
276
277 /*
278 * no target was found within the naming context,
279 * so bind must fail with invalid credentials
280 */
281 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
282 rs->sr_err = LDAP_INVALID_CREDENTIALS;
283 } else {
284 rs->sr_err = slap_map_api2result( rs );
285 }
286 send_ldap_result( op, rs );
287 op->o_tmpfree( candidates, op->o_tmpmemctx );
288 return rs->sr_err;
289
290 }
291
292 op->o_tmpfree( candidates, op->o_tmpmemctx );
293 return LDAP_SUCCESS;
294 }
295
296 static int
297 meta_back_bind_op_result(
298 Operation *op,
299 SlapReply *rs,
300 metaconn_t *mc,
301 int candidate,
302 int msgid,
303 ldap_back_send_t sendok,
304 int dolock )
305 {
306 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
307 metatarget_t *mt = mi->mi_targets[ candidate ];
308 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
309 LDAPMessage *res;
310 struct timeval tv;
311 int rc;
312 int nretries = mt->mt_nretries;
313
314 Debug( LDAP_DEBUG_TRACE,
315 ">>> %s meta_back_bind_op_result[%d]\n",
316 op->o_log_prefix, candidate );
317
318 /* make sure this is clean */
319 assert( rs->sr_ctrls == NULL );
320
321 if ( rs->sr_err == LDAP_SUCCESS ) {
322 time_t stoptime = (time_t)(-1),
323 timeout;
324 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
325 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
326 const char *timeout_text = "Operation timed out";
327 slap_op_t opidx = slap_req2op( op->o_tag );
328
329 /* since timeout is not specified, compute and use
330 * the one specific to the ongoing operation */
331 if ( opidx == LDAP_REQ_SEARCH ) {
332 if ( op->ors_tlimit <= 0 ) {
333 timeout = 0;
334
335 } else {
336 timeout = op->ors_tlimit;
337 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
338 timeout_text = NULL;
339 }
340
341 } else {
342 timeout = mt->mt_timeout[ opidx ];
343 }
344
345 /* better than nothing :) */
346 if ( timeout == 0 ) {
347 if ( mi->mi_idle_timeout ) {
348 timeout = mi->mi_idle_timeout;
349
350 } else if ( mi->mi_conn_ttl ) {
351 timeout = mi->mi_conn_ttl;
352 }
353 }
354
355 if ( timeout ) {
356 stoptime = op->o_time + timeout;
357 }
358
359 LDAP_BACK_TV_SET( &tv );
360
361 /*
362 * handle response!!!
363 */
364 retry:;
365 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
366 switch ( rc ) {
367 case 0:
368 if ( nretries != META_RETRY_NEVER
369 || ( timeout && slap_get_time() <= stoptime ) )
370 {
371 ldap_pvt_thread_yield();
372 if ( nretries > 0 ) {
373 nretries--;
374 }
375 tv = mt->mt_bind_timeout;
376 goto retry;
377 }
378
379 /* don't let anyone else use this handler,
380 * because there's a pending bind that will not
381 * be acknowledged */
382 if ( dolock) {
383 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
384 }
385 assert( LDAP_BACK_CONN_BINDING( msc ) );
386
387 #ifdef DEBUG_205
388 Debug( LDAP_DEBUG_ANY, "### %s meta_back_bind_op_result ldap_unbind_ext[%d] ld=%p\n",
389 op->o_log_prefix, candidate, (void *)msc->msc_ld );
390 #endif /* DEBUG_205 */
391
392 meta_clear_one_candidate( op, mc, candidate );
393 if ( dolock ) {
394 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
395 }
396
397 rs->sr_err = timeout_err;
398 rs->sr_text = timeout_text;
399 break;
400
401 case -1:
402 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
403 &rs->sr_err );
404
405 Debug(LDAP_DEBUG_ANY,
406 "### %s meta_back_bind_op_result[%d]: err=%d (%s) nretries=%d.\n",
407 op->o_log_prefix, candidate, rs->sr_err,
408 ldap_err2string(rs->sr_err), nretries );
409 break;
410
411 default:
412 /* only touch when activity actually took place... */
413 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
414 msc->msc_time = op->o_time;
415 }
416
417 /* FIXME: matched? referrals? response controls? */
418 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
419 NULL, NULL, NULL, NULL, 1 );
420 if ( rc != LDAP_SUCCESS ) {
421 rs->sr_err = rc;
422 }
423 rs->sr_err = slap_map_api2result( rs );
424 break;
425 }
426 }
427
428 rs->sr_err = slap_map_api2result( rs );
429
430 Debug( LDAP_DEBUG_TRACE,
431 "<<< %s meta_back_bind_op_result[%d] err=%d\n",
432 op->o_log_prefix, candidate, rs->sr_err );
433
434 return rs->sr_err;
435 }
436
437 /*
438 * meta_back_single_bind
439 *
440 * attempts to perform a bind with creds
441 */
442 static int
443 meta_back_single_bind(
444 Operation *op,
445 SlapReply *rs,
446 metaconn_t *mc,
447 int candidate )
448 {
449 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
450 metatarget_t *mt = mi->mi_targets[ candidate ];
451 struct berval mdn = BER_BVNULL;
452 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
453 int msgid;
454 dncookie dc;
455 struct berval save_o_dn;
456 int save_o_do_not_cache;
457 LDAPControl **ctrls = NULL;
458
459 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
460 ch_free( msc->msc_bound_ndn.bv_val );
461 BER_BVZERO( &msc->msc_bound_ndn );
462 }
463
464 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
465 /* destroy sensitive data */
466 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
467 ch_free( msc->msc_cred.bv_val );
468 BER_BVZERO( &msc->msc_cred );
469 }
470
471 /*
472 * Rewrite the bind dn if needed
473 */
474 dc.target = mt;
475 dc.conn = op->o_conn;
476 dc.rs = rs;
477 dc.ctx = "bindDN";
478
479 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
480 rs->sr_text = "DN rewrite error";
481 rs->sr_err = LDAP_OTHER;
482 return rs->sr_err;
483 }
484
485 /* don't add proxyAuthz; set the bindDN */
486 save_o_dn = op->o_dn;
487 save_o_do_not_cache = op->o_do_not_cache;
488 op->o_do_not_cache = 1;
489 op->o_dn = op->o_req_dn;
490
491 ctrls = op->o_ctrls;
492 rs->sr_err = meta_back_controls_add( op, rs, mc, candidate, &ctrls );
493 op->o_dn = save_o_dn;
494 op->o_do_not_cache = save_o_do_not_cache;
495 if ( rs->sr_err != LDAP_SUCCESS ) {
496 goto return_results;
497 }
498
499 /* FIXME: this fixes the bind problem right now; we need
500 * to use the asynchronous version to get the "matched"
501 * and more in case of failure ... */
502 /* FIXME: should we check if at least some of the op->o_ctrls
503 * can/should be passed? */
504 for (;;) {
505 rs->sr_err = ldap_sasl_bind( msc->msc_ld, mdn.bv_val,
506 LDAP_SASL_SIMPLE, &op->orb_cred,
507 ctrls, NULL, &msgid );
508 if ( rs->sr_err != LDAP_X_CONNECTING ) {
509 break;
510 }
511 ldap_pvt_thread_yield();
512 }
513
514 mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
515
516 meta_back_bind_op_result( op, rs, mc, candidate, msgid, LDAP_BACK_DONTSEND, 1 );
517 if ( rs->sr_err != LDAP_SUCCESS ) {
518 goto return_results;
519 }
520
521 /* If defined, proxyAuthz will be used also when
522 * back-ldap is the authorizing backend; for this
523 * purpose, a successful bind is followed by a
524 * bind with the configured identity assertion */
525 /* NOTE: use with care */
526 if ( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
527 meta_back_proxy_authz_bind( mc, candidate, op, rs, LDAP_BACK_SENDERR, 1 );
528 if ( !LDAP_BACK_CONN_ISBOUND( msc ) ) {
529 goto return_results;
530 }
531 goto cache_refresh;
532 }
533
534 ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_ndn );
535 LDAP_BACK_CONN_ISBOUND_SET( msc );
536 mc->mc_authz_target = candidate;
537
538 if ( META_BACK_TGT_SAVECRED( mt ) ) {
539 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
540 memset( msc->msc_cred.bv_val, 0,
541 msc->msc_cred.bv_len );
542 }
543 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
544 ldap_set_rebind_proc( msc->msc_ld, mt->mt_rebind_f, msc );
545 }
546
547 cache_refresh:;
548 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
549 && !BER_BVISEMPTY( &op->o_req_ndn ) )
550 {
551 ( void )meta_dncache_update_entry( &mi->mi_cache,
552 &op->o_req_ndn, candidate );
553 }
554
555 return_results:;
556 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
557 free( mdn.bv_val );
558 }
559
560 if ( META_BACK_TGT_QUARANTINE( mt ) ) {
561 meta_back_quarantine( op, rs, candidate );
562 }
563
564 return rs->sr_err;
565 }
566
567 /*
568 * meta_back_single_dobind
569 */
570 int
571 meta_back_single_dobind(
572 Operation *op,
573 SlapReply *rs,
574 metaconn_t **mcp,
575 int candidate,
576 ldap_back_send_t sendok,
577 int nretries,
578 int dolock )
579 {
580 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
581 metatarget_t *mt = mi->mi_targets[ candidate ];
582 metaconn_t *mc = *mcp;
583 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
584 int msgid;
585
586 assert( !LDAP_BACK_CONN_ISBOUND( msc ) );
587
588 /* NOTE: this obsoletes pseudorootdn */
589 if ( op->o_conn != NULL &&
590 !op->o_do_not_cache &&
591 ( BER_BVISNULL( &msc->msc_bound_ndn ) ||
592 BER_BVISEMPTY( &msc->msc_bound_ndn ) ||
593 ( LDAP_BACK_CONN_ISPRIV( mc ) && dn_match( &msc->msc_bound_ndn, &mt->mt_idassert_authcDN ) ) ||
594 ( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
595 {
596 (void)meta_back_proxy_authz_bind( mc, candidate, op, rs, sendok, dolock );
597
598 } else {
599 char *binddn = "";
600 struct berval cred = BER_BVC( "" );
601
602 /* use credentials if available */
603 if ( !BER_BVISNULL( &msc->msc_bound_ndn )
604 && !BER_BVISNULL( &msc->msc_cred ) )
605 {
606 binddn = msc->msc_bound_ndn.bv_val;
607 cred = msc->msc_cred;
608 }
609
610 /* FIXME: should we check if at least some of the op->o_ctrls
611 * can/should be passed? */
612 if(!dolock) {
613 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
614 }
615
616 for (;;) {
617 rs->sr_err = ldap_sasl_bind( msc->msc_ld,
618 binddn, LDAP_SASL_SIMPLE, &cred,
619 NULL, NULL, &msgid );
620 if ( rs->sr_err != LDAP_X_CONNECTING ) {
621 break;
622 }
623 ldap_pvt_thread_yield();
624 }
625
626 if(!dolock) {
627 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
628 }
629
630 rs->sr_err = meta_back_bind_op_result( op, rs, mc, candidate, msgid, sendok, dolock );
631
632 /* if bind succeeded, but anonymous, clear msc_bound_ndn */
633 if ( rs->sr_err != LDAP_SUCCESS || binddn[0] == '\0' ) {
634 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
635 ber_memfree( msc->msc_bound_ndn.bv_val );
636 BER_BVZERO( &msc->msc_bound_ndn );
637 }
638
639 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
640 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
641 ber_memfree( msc->msc_cred.bv_val );
642 BER_BVZERO( &msc->msc_cred );
643 }
644 }
645 }
646
647 if ( rs->sr_err != LDAP_SUCCESS ) {
648 if ( dolock ) {
649 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
650 }
651 LDAP_BACK_CONN_BINDING_CLEAR( msc );
652 if ( META_BACK_ONERR_STOP( mi ) ) {
653 LDAP_BACK_CONN_TAINTED_SET( mc );
654 meta_back_release_conn_lock( mi, mc, 0 );
655 *mcp = NULL;
656 }
657 if ( dolock ) {
658 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
659 }
660 }
661
662 if ( META_BACK_TGT_QUARANTINE( mt ) ) {
663 meta_back_quarantine( op, rs, candidate );
664 }
665
666 return rs->sr_err;
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 SlapReply *candidates )
679 {
680 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
681
682 int bound = 0,
683 i,
684 isroot = 0;
685
686 if ( be_isroot( op ) ) {
687 isroot = 1;
688 }
689
690 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
691 char buf[STRLENOF("4294967295U") + 1] = { 0 };
692 mi->mi_ldap_extra->connid2str( &mc->mc_base, buf, sizeof(buf) );
693
694 Debug( LDAP_DEBUG_TRACE,
695 "%s meta_back_dobind: conn=%s%s\n",
696 op->o_log_prefix, buf,
697 isroot ? " (isroot)" : "" );
698 }
699
700 /*
701 * all the targets are bound as pseudoroot
702 */
703 if ( mc->mc_authz_target == META_BOUND_ALL ) {
704 bound = 1;
705 goto done;
706 }
707
708 for ( i = 0; i < mi->mi_ntargets; i++ ) {
709 metatarget_t *mt = mi->mi_targets[ i ];
710 metasingleconn_t *msc = &mc->mc_conns[ i ];
711 int rc;
712
713 /*
714 * Not a candidate
715 */
716 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
717 continue;
718 }
719
720 assert( msc->msc_ld != NULL );
721
722 /*
723 * If the target is already bound it is skipped
724 */
725
726 retry_binding:;
727 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
728 if ( LDAP_BACK_CONN_ISBOUND( msc )
729 || ( LDAP_BACK_CONN_ISANON( msc )
730 && mt->mt_idassert_authmethod == LDAP_AUTH_NONE ) )
731 {
732 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
733 ++bound;
734 continue;
735
736 } else if ( META_BACK_CONN_CREATING( msc ) || LDAP_BACK_CONN_BINDING( msc ) )
737 {
738 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
739 ldap_pvt_thread_yield();
740 goto retry_binding;
741
742 }
743
744 LDAP_BACK_CONN_BINDING_SET( msc );
745 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
746
747 rc = meta_back_single_dobind( op, rs, &mc, i,
748 LDAP_BACK_DONTSEND, mt->mt_nretries, 1 );
749 /*
750 * NOTE: meta_back_single_dobind() already retries;
751 * in case of failure, it resets mc...
752 */
753 if ( rc != LDAP_SUCCESS ) {
754 if ( mc == NULL ) {
755 /* meta_back_single_dobind() already sent
756 * response and released connection */
757 goto send_err;
758 }
759
760 if ( rc == LDAP_UNAVAILABLE ) {
761 /* FIXME: meta_back_retry() already re-calls
762 * meta_back_single_dobind() */
763 if ( meta_back_retry( op, rs, &mc, i, sendok, candidates ) ) {
764 goto retry_ok;
765 }
766
767 if ( mc != NULL ) {
768 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
769 LDAP_BACK_CONN_BINDING_CLEAR( msc );
770 meta_back_release_conn_lock( mi, mc, 0 );
771 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
772 }
773
774 return 0;
775 }
776
777 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
778 LDAP_BACK_CONN_BINDING_CLEAR( msc );
779 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
780
781 Debug(LDAP_DEBUG_ANY,
782 "%s meta_back_dobind[%d]: (%s) err=%d (%s).\n",
783 op->o_log_prefix, i,
784 isroot ? op->o_bd->be_rootdn.bv_val : "anonymous",
785 rc, ldap_err2string(rc) );
786
787 /*
788 * null cred bind should always succeed
789 * as anonymous, so a failure means
790 * the target is no longer candidate possibly
791 * due to technical reasons (remote host down?)
792 * so better clear the handle
793 */
794 /* leave the target candidate, but record the error for later use */
795 candidates[ i ].sr_err = rc;
796 if ( META_BACK_ONERR_STOP( mi ) ) {
797 bound = 0;
798 goto done;
799 }
800
801 continue;
802 } /* else */
803
804 retry_ok:;
805 Debug( LDAP_DEBUG_TRACE,
806 "%s meta_back_dobind[%d]: "
807 "(%s)\n",
808 op->o_log_prefix, i,
809 isroot ? op->o_bd->be_rootdn.bv_val : "anonymous" );
810
811 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
812 LDAP_BACK_CONN_BINDING_CLEAR( msc );
813 if ( isroot ) {
814 LDAP_BACK_CONN_ISBOUND_SET( msc );
815 } else {
816 LDAP_BACK_CONN_ISANON_SET( msc );
817 }
818 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
819 ++bound;
820 }
821
822 done:;
823 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
824 char buf[STRLENOF("4294967295U") + 1] = { 0 };
825 mi->mi_ldap_extra->connid2str( &mc->mc_base, buf, sizeof(buf) );
826
827 Debug( LDAP_DEBUG_TRACE,
828 "%s meta_back_dobind: conn=%s bound=%d\n",
829 op->o_log_prefix, buf, bound );
830 }
831
832 if ( bound == 0 ) {
833 meta_back_release_conn( mi, mc );
834
835 send_err:;
836 if ( sendok & LDAP_BACK_SENDERR ) {
837 if ( rs->sr_err == LDAP_SUCCESS ) {
838 rs->sr_err = LDAP_BUSY;
839 }
840 send_ldap_result( op, rs );
841 }
842
843 return 0;
844 }
845
846 return ( bound > 0 );
847 }
848
849 /*
850 * meta_back_default_rebind
851 *
852 * This is a callback used for chasing referrals using the same
853 * credentials as the original user on this session.
854 */
855 int
856 meta_back_default_rebind(
857 LDAP *ld,
858 LDAP_CONST char *url,
859 ber_tag_t request,
860 ber_int_t msgid,
861 void *params )
862 {
863 metasingleconn_t *msc = ( metasingleconn_t * )params;
864
865 return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
866 LDAP_SASL_SIMPLE, &msc->msc_cred,
867 NULL, NULL, NULL );
868 }
869
870 /*
871 * meta_back_default_urllist
872 *
873 * This is a callback used for mucking with the urllist
874 */
875 int
876 meta_back_default_urllist(
877 LDAP *ld,
878 LDAPURLDesc **urllist,
879 LDAPURLDesc **url,
880 void *params )
881 {
882 metatarget_t *mt = (metatarget_t *)params;
883 LDAPURLDesc **urltail;
884
885 if ( urllist == url ) {
886 return LDAP_SUCCESS;
887 }
888
889 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
890 /* count */ ;
891
892 *urltail = *urllist;
893 *urllist = *url;
894 *url = NULL;
895
896 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
897 if ( mt->mt_uri ) {
898 ch_free( mt->mt_uri );
899 }
900
901 ldap_get_option( ld, LDAP_OPT_URI, (void *)&mt->mt_uri );
902 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
903
904 return LDAP_SUCCESS;
905 }
906
907 int
908 meta_back_cancel(
909 metaconn_t *mc,
910 Operation *op,
911 SlapReply *rs,
912 ber_int_t msgid,
913 int candidate,
914 ldap_back_send_t sendok )
915 {
916 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
917
918 metatarget_t *mt = mi->mi_targets[ candidate ];
919 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
920
921 int rc = LDAP_OTHER;
922
923 Debug( LDAP_DEBUG_TRACE, ">>> %s meta_back_cancel[%d] msgid=%d\n",
924 op->o_log_prefix, candidate, msgid );
925
926 /* default behavior */
927 if ( META_BACK_TGT_ABANDON( mt ) ) {
928 rc = ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
929
930 } else if ( META_BACK_TGT_IGNORE( mt ) ) {
931 rc = ldap_pvt_discard( msc->msc_ld, msgid );
932
933 } else if ( META_BACK_TGT_CANCEL( mt ) ) {
934 rc = ldap_cancel_s( msc->msc_ld, msgid, NULL, NULL );
935
936 } else {
937 assert( 0 );
938 }
939
940 Debug( LDAP_DEBUG_TRACE, "<<< %s meta_back_cancel[%d] err=%d\n",
941 op->o_log_prefix, candidate, rc );
942
943 return rc;
944 }
945
946
947
948 /*
949 * FIXME: error return must be handled in a cleaner way ...
950 */
951 int
952 meta_back_op_result(
953 metaconn_t *mc,
954 Operation *op,
955 SlapReply *rs,
956 int candidate,
957 ber_int_t msgid,
958 time_t timeout,
959 ldap_back_send_t sendok )
960 {
961 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
962
963 const char *save_text = rs->sr_text,
964 *save_matched = rs->sr_matched;
965 BerVarray save_ref = rs->sr_ref;
966 LDAPControl **save_ctrls = rs->sr_ctrls;
967 void *matched_ctx = NULL;
968
969 char *matched = NULL;
970 char *text = NULL;
971 char **refs = NULL;
972 LDAPControl **ctrls = NULL;
973
974 assert( mc != NULL );
975
976 rs->sr_text = NULL;
977 rs->sr_matched = NULL;
978 rs->sr_ref = NULL;
979 rs->sr_ctrls = NULL;
980
981 if ( candidate != META_TARGET_NONE ) {
982 metatarget_t *mt = mi->mi_targets[ candidate ];
983 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
984
985 if ( LDAP_ERR_OK( rs->sr_err ) ) {
986 int rc;
987 struct timeval tv;
988 LDAPMessage *res = NULL;
989 time_t stoptime = (time_t)(-1);
990 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
991 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
992 const char *timeout_text = "Operation timed out";
993
994 /* if timeout is not specified, compute and use
995 * the one specific to the ongoing operation */
996 if ( timeout == (time_t)(-1) ) {
997 slap_op_t opidx = slap_req2op( op->o_tag );
998
999 if ( opidx == SLAP_OP_SEARCH ) {
1000 if ( op->ors_tlimit <= 0 ) {
1001 timeout = 0;
1002
1003 } else {
1004 timeout = op->ors_tlimit;
1005 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
1006 timeout_text = NULL;
1007 }
1008
1009 } else {
1010 timeout = mt->mt_timeout[ opidx ];
1011 }
1012 }
1013
1014 /* better than nothing :) */
1015 if ( timeout == 0 ) {
1016 if ( mi->mi_idle_timeout ) {
1017 timeout = mi->mi_idle_timeout;
1018
1019 } else if ( mi->mi_conn_ttl ) {
1020 timeout = mi->mi_conn_ttl;
1021 }
1022 }
1023
1024 if ( timeout ) {
1025 stoptime = op->o_time + timeout;
1026 }
1027
1028 LDAP_BACK_TV_SET( &tv );
1029
1030 retry:;
1031 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1032 switch ( rc ) {
1033 case 0:
1034 if ( timeout && slap_get_time() > stoptime ) {
1035 (void)meta_back_cancel( mc, op, rs, msgid, candidate, sendok );
1036 rs->sr_err = timeout_err;
1037 rs->sr_text = timeout_text;
1038 break;
1039 }
1040
1041 LDAP_BACK_TV_SET( &tv );
1042 ldap_pvt_thread_yield();
1043 goto retry;
1044
1045 case -1:
1046 ldap_get_option( msc->msc_ld, LDAP_OPT_RESULT_CODE,
1047 &rs->sr_err );
1048 break;
1049
1050
1051 /* otherwise get the result; if it is not
1052 * LDAP_SUCCESS, record it in the reply
1053 * structure (this includes
1054 * LDAP_COMPARE_{TRUE|FALSE}) */
1055 default:
1056 /* only touch when activity actually took place... */
1057 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
1058 msc->msc_time = op->o_time;
1059 }
1060
1061 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
1062 &matched, &text, &refs, &ctrls, 1 );
1063 res = NULL;
1064 if ( rc == LDAP_SUCCESS ) {
1065 rs->sr_text = text;
1066 } else {
1067 rs->sr_err = rc;
1068 }
1069 rs->sr_err = slap_map_api2result( rs );
1070
1071 /* RFC 4511: referrals can only appear
1072 * if result code is LDAP_REFERRAL */
1073 if ( refs != NULL
1074 && refs[ 0 ] != NULL
1075 && refs[ 0 ][ 0 ] != '\0' )
1076 {
1077 if ( rs->sr_err != LDAP_REFERRAL ) {
1078 Debug( LDAP_DEBUG_ANY,
1079 "%s meta_back_op_result[%d]: "
1080 "got referrals with err=%d\n",
1081 op->o_log_prefix,
1082 candidate, rs->sr_err );
1083
1084 } else {
1085 int i;
1086
1087 for ( i = 0; refs[ i ] != NULL; i++ )
1088 /* count */ ;
1089 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1090 op->o_tmpmemctx );
1091 for ( i = 0; refs[ i ] != NULL; i++ ) {
1092 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1093 }
1094 BER_BVZERO( &rs->sr_ref[ i ] );
1095 }
1096
1097 } else if ( rs->sr_err == LDAP_REFERRAL ) {
1098 Debug( LDAP_DEBUG_ANY,
1099 "%s meta_back_op_result[%d]: "
1100 "got err=%d with null "
1101 "or empty referrals\n",
1102 op->o_log_prefix,
1103 candidate, rs->sr_err );
1104
1105 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1106 }
1107
1108 if ( ctrls != NULL ) {
1109 rs->sr_ctrls = ctrls;
1110 }
1111 }
1112
1113 assert( res == NULL );
1114 }
1115
1116 /* if the error in the reply structure is not
1117 * LDAP_SUCCESS, try to map it from client
1118 * to server error */
1119 if ( !LDAP_ERR_OK( rs->sr_err ) ) {
1120 rs->sr_err = slap_map_api2result( rs );
1121
1122 /* internal ops ( op->o_conn == NULL )
1123 * must not reply to client */
1124 if ( op->o_conn && !op->o_do_not_cache && matched ) {
1125
1126 /* record the (massaged) matched
1127 * DN into the reply structure */
1128 rs->sr_matched = matched;
1129 }
1130 }
1131
1132 if ( META_BACK_TGT_QUARANTINE( mt ) ) {
1133 meta_back_quarantine( op, rs, candidate );
1134 }
1135
1136 } else {
1137 int i,
1138 err = rs->sr_err;
1139
1140 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1141 metasingleconn_t *msc = &mc->mc_conns[ i ];
1142 char *xtext = NULL;
1143 char *xmatched = NULL;
1144
1145 if ( msc->msc_ld == NULL ) {
1146 continue;
1147 }
1148
1149 rs->sr_err = LDAP_SUCCESS;
1150
1151 ldap_get_option( msc->msc_ld, LDAP_OPT_RESULT_CODE, &rs->sr_err );
1152 if ( rs->sr_err != LDAP_SUCCESS ) {
1153 /*
1154 * better check the type of error. In some cases
1155 * (search ?) it might be better to return a
1156 * success if at least one of the targets gave
1157 * positive result ...
1158 */
1159 ldap_get_option( msc->msc_ld,
1160 LDAP_OPT_DIAGNOSTIC_MESSAGE, &xtext );
1161 if ( xtext != NULL && xtext [ 0 ] == '\0' ) {
1162 ldap_memfree( xtext );
1163 xtext = NULL;
1164 }
1165
1166 ldap_get_option( msc->msc_ld,
1167 LDAP_OPT_MATCHED_DN, &xmatched );
1168 if ( xmatched != NULL && xmatched[ 0 ] == '\0' ) {
1169 ldap_memfree( xmatched );
1170 xmatched = NULL;
1171 }
1172
1173 rs->sr_err = slap_map_api2result( rs );
1174
1175 Debug(LDAP_DEBUG_ANY,
1176 "%s meta_back_op_result[%d] " "err=%d text=\"%s\" matched=\"%s\".\n",
1177 op->o_log_prefix, i, rs->sr_err,
1178 (xtext ? xtext : ""),
1179 (xmatched ? xmatched : "") );
1180
1181 /*
1182 * FIXME: need to rewrite "match" (need rwinfo)
1183 */
1184 switch ( rs->sr_err ) {
1185 default:
1186 err = rs->sr_err;
1187 if ( xtext != NULL ) {
1188 if ( text ) {
1189 ldap_memfree( text );
1190 }
1191 text = xtext;
1192 xtext = NULL;
1193 }
1194 if ( xmatched != NULL ) {
1195 if ( matched ) {
1196 ldap_memfree( matched );
1197 }
1198 matched = xmatched;
1199 xmatched = NULL;
1200 }
1201 break;
1202 }
1203
1204 if ( xtext ) {
1205 ldap_memfree( xtext );
1206 }
1207
1208 if ( xmatched ) {
1209 ldap_memfree( xmatched );
1210 }
1211 }
1212
1213 if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ i ] ) ) {
1214 meta_back_quarantine( op, rs, i );
1215 }
1216 }
1217
1218 if ( err != LDAP_SUCCESS ) {
1219 rs->sr_err = err;
1220 }
1221 }
1222
1223 if ( matched != NULL ) {
1224 struct berval dn, pdn;
1225
1226 ber_str2bv( matched, 0, 0, &dn );
1227 if ( dnPretty( NULL, &dn, &pdn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
1228 ldap_memfree( matched );
1229 matched_ctx = op->o_tmpmemctx;
1230 matched = pdn.bv_val;
1231 }
1232 rs->sr_matched = matched;
1233 }
1234
1235 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1236 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1237 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1238 if ( rs->sr_text == NULL ) rs->sr_text = "Proxy operation retry failed";
1239 send_ldap_result( op, rs );
1240 }
1241 }
1242
1243 } else if ( op->o_conn &&
1244 ( ( ( sendok & LDAP_BACK_SENDOK ) && LDAP_ERR_OK( rs->sr_err ) )
1245 || ( ( sendok & LDAP_BACK_SENDERR ) && !LDAP_ERR_OK( rs->sr_err ) ) ) )
1246 {
1247 send_ldap_result( op, rs );
1248 }
1249 if ( matched ) {
1250 op->o_tmpfree( (char *)rs->sr_matched, matched_ctx );
1251 }
1252 if ( text ) {
1253 ldap_memfree( text );
1254 }
1255 if ( rs->sr_ref ) {
1256 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1257 rs->sr_ref = NULL;
1258 }
1259 if ( refs ) {
1260 ber_memvfree( (void **)refs );
1261 }
1262 if ( ctrls ) {
1263 assert( rs->sr_ctrls != NULL );
1264 ldap_controls_free( ctrls );
1265 }
1266
1267 rs->sr_text = save_text;
1268 rs->sr_matched = save_matched;
1269 rs->sr_ref = save_ref;
1270 rs->sr_ctrls = save_ctrls;
1271
1272 return( LDAP_ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1273 }
1274
1275 /*
1276 * meta_back_proxy_authz_cred()
1277 *
1278 * prepares credentials & method for meta_back_proxy_authz_bind();
1279 * or, if method is SASL, performs the SASL bind directly.
1280 */
1281 int
1282 meta_back_proxy_authz_cred(
1283 metaconn_t *mc,
1284 int candidate,
1285 Operation *op,
1286 SlapReply *rs,
1287 ldap_back_send_t sendok,
1288 struct berval *binddn,
1289 struct berval *bindcred,
1290 int *method )
1291 {
1292 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1293 metatarget_t *mt = mi->mi_targets[ candidate ];
1294 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
1295 struct berval ndn;
1296 int dobind = 0;
1297
1298 /* don't proxyAuthz if protocol is not LDAPv3 */
1299 switch ( mt->mt_version ) {
1300 case LDAP_VERSION3:
1301 break;
1302
1303 case 0:
1304 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1305 break;
1306 }
1307 /* fall thru */
1308
1309 default:
1310 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1311 if ( sendok & LDAP_BACK_SENDERR ) {
1312 send_ldap_result( op, rs );
1313 }
1314 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1315 goto done;
1316 }
1317
1318 if ( op->o_tag == LDAP_REQ_BIND ) {
1319 ndn = op->o_req_ndn;
1320
1321 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1322 ndn = op->o_conn->c_ndn;
1323
1324 } else {
1325 ndn = op->o_ndn;
1326 }
1327 rs->sr_err = LDAP_SUCCESS;
1328
1329 /*
1330 * FIXME: we need to let clients use proxyAuthz
1331 * otherwise we cannot do symmetric pools of servers;
1332 * we have to live with the fact that a user can
1333 * authorize itself as any ID that is allowed
1334 * by the authzTo directive of the "proxyauthzdn".
1335 */
1336 /*
1337 * NOTE: current Proxy Authorization specification
1338 * and implementation do not allow proxy authorization
1339 * control to be provided with Bind requests
1340 */
1341 /*
1342 * if no bind took place yet, but the connection is bound
1343 * and the "proxyauthzdn" is set, then bind as
1344 * "proxyauthzdn" and explicitly add the proxyAuthz
1345 * control to every operation with the dn bound
1346 * to the connection as control value.
1347 */
1348
1349 /* bind as proxyauthzdn only if no idassert mode
1350 * is requested, or if the client's identity
1351 * is authorized */
1352 switch ( mt->mt_idassert_mode ) {
1353 case LDAP_BACK_IDASSERT_LEGACY:
1354 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1355 if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) && !BER_BVISEMPTY( &mt->mt_idassert_authcDN ) )
1356 {
1357 *binddn = mt->mt_idassert_authcDN;
1358 *bindcred = mt->mt_idassert_passwd;
1359 dobind = 1;
1360 }
1361 }
1362 break;
1363
1364 default:
1365 /* NOTE: rootdn can always idassert */
1366 if ( BER_BVISNULL( &ndn )
1367 && mt->mt_idassert_authz == NULL
1368 && !( mt->mt_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL ) )
1369 {
1370 if ( mt->mt_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1371 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1372 if ( sendok & LDAP_BACK_SENDERR ) {
1373 send_ldap_result( op, rs );
1374 }
1375 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1376 goto done;
1377
1378 }
1379
1380 rs->sr_err = LDAP_SUCCESS;
1381 *binddn = slap_empty_bv;
1382 *bindcred = slap_empty_bv;
1383 break;
1384
1385 } else if ( mt->mt_idassert_authz && !be_isroot( op ) ) {
1386 struct berval authcDN;
1387
1388 if ( BER_BVISNULL( &ndn ) ) {
1389 authcDN = slap_empty_bv;
1390
1391 } else {
1392 authcDN = ndn;
1393 }
1394 rs->sr_err = slap_sasl_matches( op, mt->mt_idassert_authz,
1395 &authcDN, &authcDN );
1396 if ( rs->sr_err != LDAP_SUCCESS ) {
1397 if ( mt->mt_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1398 if ( sendok & LDAP_BACK_SENDERR ) {
1399 send_ldap_result( op, rs );
1400 }
1401 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1402 goto done;
1403 }
1404
1405 rs->sr_err = LDAP_SUCCESS;
1406 *binddn = slap_empty_bv;
1407 *bindcred = slap_empty_bv;
1408 break;
1409 }
1410 }
1411
1412 *binddn = mt->mt_idassert_authcDN;
1413 *bindcred = mt->mt_idassert_passwd;
1414 dobind = 1;
1415 break;
1416 }
1417
1418 if ( dobind && mt->mt_idassert_authmethod == LDAP_AUTH_SASL ) {
1419 #ifdef HAVE_CYRUS_SASL
1420 void *defaults = NULL;
1421 struct berval authzID = BER_BVNULL;
1422 int freeauthz = 0;
1423
1424 /* if SASL supports native authz, prepare for it */
1425 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1426 ( mt->mt_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1427 {
1428 switch ( mt->mt_idassert_mode ) {
1429 case LDAP_BACK_IDASSERT_OTHERID:
1430 case LDAP_BACK_IDASSERT_OTHERDN:
1431 authzID = mt->mt_idassert_authzID;
1432 break;
1433
1434 case LDAP_BACK_IDASSERT_ANONYMOUS:
1435 BER_BVSTR( &authzID, "dn:" );
1436 break;
1437
1438 case LDAP_BACK_IDASSERT_SELF:
1439 if ( BER_BVISNULL( &ndn ) ) {
1440 /* connection is not authc'd, so don't idassert */
1441 BER_BVSTR( &authzID, "dn:" );
1442 break;
1443 }
1444 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1445 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1446 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1447 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1448 ndn.bv_val, ndn.bv_len + 1 );
1449 freeauthz = 1;
1450 break;
1451
1452 default:
1453 break;
1454 }
1455 }
1456
1457 if ( mt->mt_idassert_secprops != NULL ) {
1458 rs->sr_err = ldap_set_option( msc->msc_ld,
1459 LDAP_OPT_X_SASL_SECPROPS,
1460 (void *)mt->mt_idassert_secprops );
1461
1462 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1463 rs->sr_err = LDAP_OTHER;
1464 if ( sendok & LDAP_BACK_SENDERR ) {
1465 send_ldap_result( op, rs );
1466 }
1467 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1468 goto done;
1469 }
1470 }
1471
1472 defaults = lutil_sasl_defaults( msc->msc_ld,
1473 mt->mt_idassert_sasl_mech.bv_val,
1474 mt->mt_idassert_sasl_realm.bv_val,
1475 mt->mt_idassert_authcID.bv_val,
1476 mt->mt_idassert_passwd.bv_val,
1477 authzID.bv_val );
1478 if ( defaults == NULL ) {
1479 rs->sr_err = LDAP_OTHER;
1480 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1481 if ( sendok & LDAP_BACK_SENDERR ) {
1482 send_ldap_result( op, rs );
1483 }
1484 goto done;
1485 }
1486
1487 rs->sr_err = ldap_sasl_interactive_bind_s( msc->msc_ld, binddn->bv_val,
1488 mt->mt_idassert_sasl_mech.bv_val, NULL, NULL,
1489 LDAP_SASL_QUIET, lutil_sasl_interact,
1490 defaults );
1491
1492 rs->sr_err = slap_map_api2result( rs );
1493 if ( rs->sr_err != LDAP_SUCCESS ) {
1494 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1495 if ( sendok & LDAP_BACK_SENDERR ) {
1496 send_ldap_result( op, rs );
1497 }
1498
1499 } else {
1500 LDAP_BACK_CONN_ISBOUND_SET( msc );
1501 }
1502
1503 lutil_sasl_freedefs( defaults );
1504 if ( freeauthz ) {
1505 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1506 }
1507
1508 goto done;
1509 #endif /* HAVE_CYRUS_SASL */
1510 }
1511
1512 *method = mt->mt_idassert_authmethod;
1513 switch ( mt->mt_idassert_authmethod ) {
1514 case LDAP_AUTH_NONE:
1515 BER_BVSTR( binddn, "" );
1516 BER_BVSTR( bindcred, "" );
1517 /* fallthru */
1518
1519 case LDAP_AUTH_SIMPLE:
1520 break;
1521
1522 default:
1523 /* unsupported! */
1524 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1525 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1526 if ( sendok & LDAP_BACK_SENDERR ) {
1527 send_ldap_result( op, rs );
1528 }
1529 break;
1530 }
1531
1532 done:;
1533
1534 if ( !BER_BVISEMPTY( binddn ) ) {
1535 LDAP_BACK_CONN_ISIDASSERT_SET( msc );
1536 }
1537
1538 return rs->sr_err;
1539 }
1540
1541 static int
1542 meta_back_proxy_authz_bind(
1543 metaconn_t *mc,
1544 int candidate,
1545 Operation *op,
1546 SlapReply *rs,
1547 ldap_back_send_t sendok,
1548 int dolock )
1549 {
1550 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1551 metatarget_t *mt = mi->mi_targets[ candidate ];
1552 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
1553 struct berval binddn = BER_BVC( "" ),
1554 cred = BER_BVC( "" );
1555 int method = LDAP_AUTH_NONE,
1556 rc;
1557
1558 rc = meta_back_proxy_authz_cred( mc, candidate, op, rs, sendok, &binddn, &cred, &method );
1559 if ( rc == LDAP_SUCCESS && !LDAP_BACK_CONN_ISBOUND( msc ) ) {
1560 int msgid;
1561
1562 switch ( method ) {
1563 case LDAP_AUTH_NONE:
1564 case LDAP_AUTH_SIMPLE:
1565
1566 if(!dolock) {
1567 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1568 }
1569
1570 for (;;) {
1571 rs->sr_err = ldap_sasl_bind( msc->msc_ld,
1572 binddn.bv_val, LDAP_SASL_SIMPLE,
1573 &cred, NULL, NULL, &msgid );
1574 if ( rs->sr_err != LDAP_X_CONNECTING ) {
1575 break;
1576 }
1577 ldap_pvt_thread_yield();
1578 }
1579
1580 if(!dolock) {
1581 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1582 }
1583
1584 rc = meta_back_bind_op_result( op, rs, mc, candidate, msgid, sendok, dolock );
1585 if ( rc == LDAP_SUCCESS ) {
1586 /* set rebind stuff in case of successful proxyAuthz bind,
1587 * so that referral chasing is attempted using the right
1588 * identity */
1589 LDAP_BACK_CONN_ISBOUND_SET( msc );
1590 ber_bvreplace( &msc->msc_bound_ndn, &binddn );
1591
1592 if ( META_BACK_TGT_SAVECRED( mt ) ) {
1593 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
1594 memset( msc->msc_cred.bv_val, 0,
1595 msc->msc_cred.bv_len );
1596 }
1597 ber_bvreplace( &msc->msc_cred, &cred );
1598 ldap_set_rebind_proc( msc->msc_ld, mt->mt_rebind_f, msc );
1599 }
1600 }
1601 break;
1602
1603 default:
1604 assert( 0 );
1605 break;
1606 }
1607 }
1608
1609 return LDAP_BACK_CONN_ISBOUND( msc );
1610 }
1611
1612 /*
1613 * Add controls;
1614 *
1615 * if any needs to be added, it is prepended to existing ones,
1616 * in a newly allocated array. The companion function
1617 * mi->mi_ldap_extra->controls_free() must be used to restore the original
1618 * status of op->o_ctrls.
1619 */
1620 int
1621 meta_back_controls_add(
1622 Operation *op,
1623 SlapReply *rs,
1624 metaconn_t *mc,
1625 int candidate,
1626 LDAPControl ***pctrls )
1627 {
1628 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1629 metatarget_t *mt = mi->mi_targets[ candidate ];
1630 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
1631
1632 LDAPControl **ctrls = NULL;
1633 /* set to the maximum number of controls this backend can add */
1634 LDAPControl c[ 2 ] = {{ 0 }};
1635 int n = 0, i, j1 = 0, j2 = 0, skipped = 0;
1636
1637 *pctrls = NULL;
1638
1639 rs->sr_err = LDAP_SUCCESS;
1640
1641 /* don't add controls if protocol is not LDAPv3 */
1642 switch ( mt->mt_version ) {
1643 case LDAP_VERSION3:
1644 break;
1645
1646 case 0:
1647 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1648 break;
1649 }
1650 /* fall thru */
1651
1652 default:
1653 goto done;
1654 }
1655
1656 /* put controls that go __before__ existing ones here */
1657
1658 /* proxyAuthz for identity assertion */
1659 switch ( mi->mi_ldap_extra->proxy_authz_ctrl( op, rs, &msc->msc_bound_ndn,
1660 mt->mt_version, &mt->mt_idassert, &c[ j1 ] ) )
1661 {
1662 case SLAP_CB_CONTINUE:
1663 break;
1664
1665 case LDAP_SUCCESS:
1666 j1++;
1667 break;
1668
1669 default:
1670 goto done;
1671 }
1672
1673 /* put controls that go __after__ existing ones here */
1674
1675 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
1676 /* session tracking */
1677 if ( META_BACK_TGT_ST_REQUEST( mt ) ) {
1678 switch ( slap_ctrl_session_tracking_request_add( op, rs, &c[ j1 + j2 ] ) ) {
1679 case SLAP_CB_CONTINUE:
1680 break;
1681
1682 case LDAP_SUCCESS:
1683 j2++;
1684 break;
1685
1686 default:
1687 goto done;
1688 }
1689 }
1690 #endif /* SLAP_CONTROL_X_SESSION_TRACKING */
1691
1692 if ( rs->sr_err == SLAP_CB_CONTINUE ) {
1693 rs->sr_err = LDAP_SUCCESS;
1694 }
1695
1696 /* if nothing to do, just bail out */
1697 if ( j1 == 0 && j2 == 0 ) {
1698 goto done;
1699 }
1700
1701 assert( j1 + j2 <= (int) (sizeof( c )/sizeof( c[0] )) );
1702
1703 if ( op->o_ctrls ) {
1704 for ( n = 0; op->o_ctrls[ n ]; n++ )
1705 /* just count ctrls */ ;
1706 }
1707
1708 ctrls = op->o_tmpalloc( (n + j1 + j2 + 1) * sizeof( LDAPControl * ) + ( j1 + j2 ) * sizeof( LDAPControl ),
1709 op->o_tmpmemctx );
1710 if ( j1 ) {
1711 ctrls[ 0 ] = (LDAPControl *)&ctrls[ n + j1 + j2 + 1 ];
1712 *ctrls[ 0 ] = c[ 0 ];
1713 for ( i = 1; i < j1; i++ ) {
1714 ctrls[ i ] = &ctrls[ 0 ][ i ];
1715 *ctrls[ i ] = c[ i ];
1716 }
1717 }
1718
1719 i = 0;
1720 if ( op->o_ctrls ) {
1721 LDAPControl *proxyauthz = ldap_control_find(
1722 LDAP_CONTROL_PROXY_AUTHZ, op->o_ctrls, NULL );
1723
1724 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1725 /* Only replace it if we generated one */
1726 if ( j1 && proxyauthz && proxyauthz == op->o_ctrls[ i ] ) {
1727 /* Frontend has already checked only one is present */
1728 assert( skipped == 0 );
1729 skipped++;
1730 continue;
1731 }
1732 ctrls[ i + j1 - skipped ] = op->o_ctrls[ i ];
1733 }
1734 }
1735
1736 n += j1 - skipped;
1737 if ( j2 ) {
1738 ctrls[ n ] = (LDAPControl *)&ctrls[ n + j2 + 1 ] + j1;
1739 *ctrls[ n ] = c[ j1 ];
1740 for ( i = 1; i < j2; i++ ) {
1741 ctrls[ n + i ] = &ctrls[ n ][ i ];
1742 *ctrls[ n + i ] = c[ i ];
1743 }
1744 }
1745
1746 ctrls[ n + j2 ] = NULL;
1747
1748 done:;
1749 if ( ctrls == NULL ) {
1750 ctrls = op->o_ctrls;
1751 }
1752
1753 *pctrls = ctrls;
1754
1755 return rs->sr_err;
1756 }
1757