Debian Codegraph @

servers/slapd/back-asyncmeta/bind.c

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