1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2006 The OpenLDAP Foundation.
6 * Portions Copyright 2000-2003 Pierangelo Masarati.
7 * Portions Copyright 1999-2003 Howard Chu.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
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>.
19 * This work was initially developed by Howard Chu for inclusion
20 * in OpenLDAP Software and subsequently enhanced by Pierangelo
29 #include <ac/socket.h>
30 #include <ac/string.h>
34 #include "back-ldap.h"
36 #include <lutil_ldap.h>
38 #ifndef PRINT_CONNTREE
39 #define PRINT_CONNTREE 0
40 #endif /* !PRINT_CONNTREE */
42 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
44 static LDAP_REBIND_PROC ldap_back_default_rebind;
46 LDAP_REBIND_PROC *ldap_back_rebind_f = ldap_back_default_rebind;
49 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
52 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
55 ldap_back_bind( Operation *op, SlapReply *rs )
57 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
63 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR );
68 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
69 ch_free( lc->lc_bound_ndn.bv_val );
70 BER_BVZERO( &lc->lc_bound_ndn );
72 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
74 /* method is always LDAP_AUTH_SIMPLE if we got here */
75 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
77 &op->orb_cred, op->o_ctrls, NULL, &msgid );
78 rc = ldap_back_op_result( lc, op, rs, msgid, 0, LDAP_BACK_SENDERR );
80 if ( rc == LDAP_SUCCESS ) {
81 /* If defined, proxyAuthz will be used also when
82 * back-ldap is the authorizing backend; for this
83 * purpose, a successful bind is followed by a
84 * bind with the configured identity assertion */
85 /* NOTE: use with care */
86 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
87 ldap_back_proxy_authz_bind( lc, op, rs, LDAP_BACK_SENDERR );
88 if ( !LDAP_BACK_CONN_ISBOUND( lc ) ) {
94 LDAP_BACK_CONN_ISBOUND_SET( lc );
95 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
97 if ( LDAP_BACK_SAVECRED( li ) ) {
98 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
99 memset( lc->lc_cred.bv_val, 0,
100 lc->lc_cred.bv_len );
102 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
103 ldap_set_rebind_proc( lc->lc_ld, ldap_back_rebind_f, lc );
108 assert( lc->lc_binding == 1 );
111 /* must re-insert if local DN changed as result of bind */
112 if ( !LDAP_BACK_CONN_ISBOUND( lc )
113 || ( LDAP_BACK_CONN_ISBOUND( lc )
114 && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) ) )
118 /* wait for all other ops to release the connection */
120 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
121 if ( lc->lc_refcnt > 1 ) {
122 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
123 ldap_pvt_thread_yield();
127 assert( lc->lc_refcnt == 1 );
128 lc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
129 ldap_back_conndn_cmp );
130 assert( lc != NULL );
132 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
133 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
134 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
135 ldap_back_conndn_cmp, ldap_back_conndn_dup );
138 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
140 /* we can do this because lc_refcnt == 1 */
141 ldap_back_conn_free( lc );
147 ldap_back_release_conn( op, rs, lc );
154 * ldap_back_conndn_cmp
156 * compares two ldapconn_t based on the value of the conn pointer
157 * and of the local DN; used by avl stuff for insert, lookup
161 ldap_back_conndn_cmp( const void *c1, const void *c2 )
163 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
164 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
167 /* If local DNs don't match, it is definitely not a match */
168 /* For shared sessions, conn is NULL. Only explicitly
169 * bound sessions will have non-NULL conn.
171 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
173 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
182 * compares two ldapconn_t based on the value of the conn pointer;
183 * used by avl stuff for delete of all conns with the same connid
186 ldap_back_conn_cmp( const void *c1, const void *c2 )
188 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
189 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
191 /* For shared sessions, conn is NULL. Only explicitly
192 * bound sessions will have non-NULL conn.
194 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
198 * ldap_back_conndn_dup
200 * returns -1 in case a duplicate ldapconn_t has been inserted;
204 ldap_back_conndn_dup( void *c1, void *c2 )
206 ldapconn_t *lc1 = (ldapconn_t *)c1;
207 ldapconn_t *lc2 = (ldapconn_t *)c2;
209 /* Cannot have more than one shared session with same DN */
210 if ( lc1->lc_conn == lc2->lc_conn &&
211 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
219 #if PRINT_CONNTREE > 0
221 ravl_print( Avlnode *root, int depth )
230 ravl_print( root->avl_right, depth+1 );
232 for ( i = 0; i < depth; i++ ) {
233 fprintf( stderr, "-" );
237 fprintf( stderr, "lc=%p local=\"%s\" conn=%p %s refcnt=%d\n",
238 (void *)lc, lc->lc_local_ndn.bv_val, (void *)lc->lc_conn,
239 avl_bf2str( root->avl_bf ), lc->lc_refcnt );
241 ravl_print( root->avl_left, depth+1 );
245 myprint( Avlnode *root )
247 fprintf( stderr, "========>\n" );
250 fprintf( stderr, "\tNULL\n" );
253 ravl_print( root, 0 );
256 fprintf( stderr, "<========\n" );
258 #endif /* PRINT_CONNTREE */
261 ldap_back_freeconn( Operation *op, ldapconn_t *lc, int dolock )
263 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
266 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
269 assert( lc->lc_refcnt > 0 );
270 if ( --lc->lc_refcnt == 0 ) {
271 lc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
272 ldap_back_conndn_cmp );
273 assert( lc != NULL );
275 ldap_back_conn_free( (void *)lc );
279 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
296 int rc = LDAP_SUCCESS;
299 /* this is ridiculous... */
300 dummy.li_flags = flags;
302 /* start TLS ("tls-[try-]{start,propagate}" statements) */
303 if ( ( LDAP_BACK_USE_TLS( &dummy ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS( &dummy ) ) )
304 && !ldap_is_ldaps_url( url ) )
306 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
308 * use asynchronous StartTLS
309 * in case, chase referral (not implemented yet)
313 if ( protocol == 0 ) {
314 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
318 if ( protocol < LDAP_VERSION3 ) {
319 protocol = LDAP_VERSION3;
320 /* Set LDAP version */
321 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
322 (const void *)&protocol );
325 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
326 if ( rc == LDAP_SUCCESS ) {
327 LDAPMessage *res = NULL;
330 LDAP_BACK_TV_SET( &tv );
333 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
335 rc = LDAP_UNAVAILABLE;
337 } else if ( rc == 0 ) {
338 if ( retries != LDAP_BACK_RETRY_NEVER ) {
339 ldap_pvt_thread_yield();
343 LDAP_BACK_TV_SET( &tv );
346 rc = LDAP_UNAVAILABLE;
348 } else if ( rc == LDAP_RES_EXTENDED ) {
349 struct berval *data = NULL;
351 rc = ldap_parse_extended_result( ld, res,
353 if ( rc == LDAP_SUCCESS ) {
355 rc = ldap_parse_result( ld, res, &err,
356 NULL, NULL, NULL, NULL, 1 );
357 if ( rc == LDAP_SUCCESS ) {
362 /* FIXME: in case a referral
363 * is returned, should we try
364 * using it instead of the
366 if ( rc == LDAP_SUCCESS ) {
367 rc = ldap_install_tls( ld );
369 } else if ( rc == LDAP_REFERRAL ) {
370 rc = LDAP_UNWILLING_TO_PERFORM;
371 *text = "unwilling to chase referral returned by Start TLS exop";
375 if ( data->bv_val ) {
376 ber_memfree( data->bv_val );
390 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
392 * use synchronous StartTLS
394 rc = ldap_start_tls_s( ld, NULL, NULL );
395 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
397 /* if StartTLS is requested, only attempt it if the URL
398 * is not "ldaps://"; this may occur not only in case
399 * of misconfiguration, but also when used in the chain
400 * overlay, where the "uri" can be parsed out of a referral */
406 case LDAP_SERVER_DOWN:
410 if ( LDAP_BACK_TLS_CRITICAL( &dummy ) ) {
411 *text = "could not start TLS";
415 /* in case Start TLS is not critical */
427 #endif /* HAVE_TLS */
430 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
432 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
433 int vers = op->o_protocol;
436 int is_tls = op->o_conn->c_is_tls;
437 #endif /* HAVE_TLS */
439 assert( lcp != NULL );
441 rs->sr_err = ldap_initialize( &ld, li->li_uri );
442 if ( rs->sr_err != LDAP_SUCCESS ) {
446 /* Set LDAP version. This will always succeed: If the client
447 * bound with a particular version, then so can we.
450 /* assume it's an internal op; set to LDAPv3 */
451 vers = LDAP_VERSION3;
453 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
455 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
456 ldap_set_option( ld, LDAP_OPT_REFERRALS,
457 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
460 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
461 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
462 if ( rs->sr_err != LDAP_SUCCESS ) {
463 ldap_unbind_ext( ld, NULL, NULL );
466 #endif /* HAVE_TLS */
468 if ( *lcp == NULL ) {
469 *lcp = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
470 (*lcp)->lc_flags= li->li_flags;
473 (*lcp)->lc_refcnt = 1;
474 (*lcp)->lc_binding = 1;
477 LDAP_BACK_CONN_ISTLS_SET( *lcp );
479 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
481 #endif /* HAVE_TLS */
484 if ( rs->sr_err != LDAP_SUCCESS ) {
485 rs->sr_err = slap_map_api2result( rs );
486 if ( sendok & LDAP_BACK_SENDERR ) {
487 if ( rs->sr_text == NULL ) {
488 rs->sr_text = "ldap_initialize() failed";
490 send_ldap_result( op, rs );
495 if ( li->li_conn_ttl > 0 ) {
496 (*lcp)->lc_create_time = op->o_time;
504 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
506 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
507 ldapconn_t *lc = NULL,
509 int refcnt = 1, binding = 1;
511 /* Internal searches are privileged and shared. So is root. */
512 if ( op->o_do_not_cache || be_isroot( op ) ) {
513 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
514 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
515 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
518 lc_curr.lc_local_ndn = op->o_ndn;
519 /* Explicit binds must not be shared */
520 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
521 lc_curr.lc_conn = op->o_conn;
524 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
528 /* Explicit Bind requests always get their own conn */
529 if ( !( sendok & LDAP_BACK_BINDING ) ) {
530 /* Searches for a ldapconn in the avl tree */
532 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
534 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
535 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
537 /* Don't reuse connections while they're still binding */
538 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
539 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
540 ldap_pvt_thread_yield();
543 refcnt = ++lc->lc_refcnt;
544 binding = ++lc->lc_binding;
546 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
549 /* Looks like we didn't get a bind. Open a new session... */
551 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
554 if ( sendok & LDAP_BACK_BINDING ) {
555 LDAP_BACK_CONN_BINDING_SET( lc );
557 lc->lc_conn = lc_curr.lc_conn;
558 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
560 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
561 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
562 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
563 LDAP_BACK_CONN_ISPRIV_SET( lc );
566 BER_BVZERO( &lc->lc_cred );
567 BER_BVZERO( &lc->lc_bound_ndn );
569 /* FIXME: if we set lc_bound_ndn = o_ndn
570 * we end up with a bind with DN but no password! */
571 if ( !BER_BVISEMPTY( &op->o_ndn )
572 && SLAP_IS_AUTHZ_BACKEND( op ) )
574 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
580 /* if start TLS failed but it was not mandatory,
581 * check if the non-TLS connection was already
582 * in cache; in case, destroy the newly created
583 * connection and use the existing one */
584 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
585 && !ldap_tls_inplace( lc->lc_ld ) )
589 lc_curr.lc_conn = LDAP_BACK_PCONN;
590 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
591 tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
592 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
593 if ( tmplc != NULL ) {
594 refcnt = ++tmplc->lc_refcnt;
595 binding = ++tmplc->lc_binding;
596 ldap_back_conn_free( lc );
599 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
601 if ( tmplc != NULL ) {
605 #endif /* HAVE_TLS */
607 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
609 /* Inserts the newly created ldapconn in the avl tree */
610 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
612 assert( lc->lc_refcnt == 1 );
613 assert( lc->lc_binding == 1 );
614 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
615 ldap_back_conndn_cmp, ldap_back_conndn_dup );
617 #if PRINT_CONNTREE > 0
618 myprint( li->li_conninfo.lai_tree );
619 #endif /* PRINT_CONNTREE */
621 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
623 Debug( LDAP_DEBUG_TRACE,
624 "=>ldap_back_getconn: conn %p inserted refcnt=%u binding=%u\n",
625 (void *)lc, refcnt, binding );
627 /* Err could be -1 in case a duplicate ldapconn is inserted */
628 if ( rs->sr_err != 0 ) {
629 ldap_back_conn_free( lc );
630 rs->sr_err = LDAP_OTHER;
631 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
632 send_ldap_error( op, rs, LDAP_OTHER,
633 "internal server error" );
639 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
640 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
642 /* in case of failure, it frees/taints lc and sets it to NULL */
643 if ( !ldap_back_retry( &lc, op, rs, sendok ) ) {
649 Debug( LDAP_DEBUG_TRACE,
650 "=>ldap_back_getconn: conn %p fetched refcnt=%u binding=%u\n",
651 (void *)lc, refcnt, binding );
655 if ( li->li_idle_timeout && lc ) {
656 lc->lc_time = op->o_time;
664 ldap_back_release_conn_lock(
670 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
673 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
675 assert( lc->lc_refcnt > 0 );
677 LDAP_BACK_CONN_BINDING_CLEAR( lc );
679 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
686 * Note: as the check for the value of lc->lc_bound was already here, I removed
687 * it from all the callers, and I made the function return the flag, so
688 * it can be used to simplify the check.
690 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
693 ldap_back_dobind_int(
697 ldap_back_send_t sendok,
701 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
706 assert( retries >= 0 );
710 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
713 if ( binding == 0 ) {
714 /* check if already bound */
715 rc = LDAP_BACK_CONN_ISBOUND( lc );
719 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
724 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
725 /* if someone else is about to bind it, give up and retry */
727 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
729 ldap_pvt_thread_yield();
733 /* otherwise this thread will bind it */
734 LDAP_BACK_CONN_BINDING_SET( lc );
739 /* wait for pending operations to finish */
740 /* FIXME: may become a bottleneck! */
741 if ( lc->lc_refcnt != lc->lc_binding ) {
743 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
745 ldap_pvt_thread_yield();
750 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
754 while ( lc->lc_refcnt > 1 ) {
755 ldap_pvt_thread_yield();
756 rc = LDAP_BACK_CONN_ISBOUND( lc );
763 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
765 LDAP_BACK_CONN_BINDING_SET( lc );
767 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
772 * FIXME: we need to let clients use proxyAuthz
773 * otherwise we cannot do symmetric pools of servers;
774 * we have to live with the fact that a user can
775 * authorize itself as any ID that is allowed
776 * by the authzTo directive of the "proxyauthzdn".
779 * NOTE: current Proxy Authorization specification
780 * and implementation do not allow proxy authorization
781 * control to be provided with Bind requests
784 * if no bind took place yet, but the connection is bound
785 * and the "idassert-authcDN" (or other ID) is set,
786 * then bind as the asserting identity and explicitly
787 * add the proxyAuthz control to every operation with the
788 * dn bound to the connection as control value.
789 * This is done also if this is the authrizing backend,
790 * but the "override" flag is given to idassert.
791 * It allows to use SASL bind and yet proxyAuthz users
793 if ( op->o_conn != NULL &&
794 !op->o_do_not_cache &&
795 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
796 ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
798 (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok );
802 #ifdef HAVE_CYRUS_SASL
803 if ( LDAP_BACK_CONN_ISPRIV( lc )
804 && li->li_acl_authmethod == LDAP_AUTH_SASL )
806 void *defaults = NULL;
808 if ( li->li_acl_secprops != NULL ) {
809 rc = ldap_set_option( lc->lc_ld,
810 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops);
812 if ( rc != LDAP_OPT_SUCCESS ) {
813 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
814 "(%s,SECPROPS,\"%s\") failed!\n",
815 li->li_uri, li->li_acl_secprops, 0 );
820 defaults = lutil_sasl_defaults( lc->lc_ld,
821 li->li_acl_sasl_mech.bv_val,
822 li->li_acl_sasl_realm.bv_val,
823 li->li_acl_authcID.bv_val,
824 li->li_acl_passwd.bv_val,
827 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
828 li->li_acl_authcDN.bv_val,
829 li->li_acl_sasl_mech.bv_val, NULL, NULL,
830 LDAP_SASL_QUIET, lutil_sasl_interact,
833 lutil_sasl_freedefs( defaults );
835 rs->sr_err = slap_map_api2result( rs );
836 if ( rs->sr_err != LDAP_SUCCESS ) {
837 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
838 send_ldap_result( op, rs );
841 LDAP_BACK_CONN_ISBOUND_SET( lc );
845 #endif /* HAVE_CYRUS_SASL */
848 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
849 lc->lc_bound_ndn.bv_val,
850 LDAP_SASL_SIMPLE, &lc->lc_cred,
851 NULL, NULL, &msgid );
853 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
854 if ( retries != LDAP_BACK_RETRY_NEVER ) {
856 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
859 assert( lc->lc_refcnt > 0 );
860 if ( lc->lc_refcnt == 1 ) {
861 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
864 /* lc here must be the regular lc, reset and ready for init */
865 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
868 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
870 if ( rs->sr_err == LDAP_SUCCESS ) {
879 ldap_back_freeconn( op, lc, dolock );
880 rs->sr_err = slap_map_api2result( rs );
885 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
886 if ( rc == LDAP_SUCCESS ) {
887 LDAP_BACK_CONN_ISBOUND_SET( lc );
892 LDAP_BACK_CONN_BINDING_CLEAR( lc );
893 rc = LDAP_BACK_CONN_ISBOUND( lc );
895 ldap_back_release_conn_lock( op, rs, lc, dolock );
902 ldap_back_dobind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
904 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
906 return ldap_back_dobind_int( lc, op, rs, sendok, li->li_nretries, 1 );
910 * ldap_back_default_rebind
912 * This is a callback used for chasing referrals using the same
913 * credentials as the original user on this session.
916 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
917 ber_int_t msgid, void *params )
919 ldapconn_t *lc = (ldapconn_t *)params;
922 /* ... otherwise we couldn't get here */
923 assert( lc != NULL );
925 if ( !ldap_tls_inplace( ld ) ) {
926 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
928 const char *text = NULL;
930 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
931 LDAP_BACK_RETRY_DEFAULT, &text );
932 if ( rc != LDAP_SUCCESS ) {
936 #endif /* HAVE_TLS */
938 /* FIXME: add checks on the URL/identity? */
940 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
941 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
951 ldap_back_send_t sendok )
954 LDAPMessage *res = NULL;
957 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
960 rs->sr_matched = NULL;
962 /* if the error recorded in the reply corresponds
963 * to a successful state, get the error from the
964 * remote server response */
965 if ( ERR_OK( rs->sr_err ) ) {
974 LDAP_BACK_TV_SET( &tv );
978 /* if result parsing fails, note the failure reason */
979 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
983 (void)ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
984 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
985 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
986 rs->sr_text = "Operation timed out";
990 LDAP_BACK_TV_SET( &tv );
991 ldap_pvt_thread_yield();
995 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1000 /* otherwise get the result; if it is not
1001 * LDAP_SUCCESS, record it in the reply
1002 * structure (this includes
1003 * LDAP_COMPARE_{TRUE|FALSE}) */
1005 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1006 &match, &text, NULL, NULL, 1 );
1008 if ( rc != LDAP_SUCCESS ) {
1014 /* if the error in the reply structure is not
1015 * LDAP_SUCCESS, try to map it from client
1016 * to server error */
1017 if ( !ERR_OK( rs->sr_err ) ) {
1018 rs->sr_err = slap_map_api2result( rs );
1020 /* internal ops ( op->o_conn == NULL )
1021 * must not reply to client */
1022 if ( op->o_conn && !op->o_do_not_cache && match ) {
1024 /* record the (massaged) matched
1025 * DN into the reply structure */
1026 rs->sr_matched = match;
1030 ( ( sendok & LDAP_BACK_SENDOK )
1031 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1033 send_ldap_result( op, rs );
1036 if ( rs->sr_matched != match ) {
1037 free( (char *)rs->sr_matched );
1039 rs->sr_matched = NULL;
1040 ldap_memfree( match );
1043 ldap_memfree( text );
1046 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1049 /* return true if bound, false if failed */
1051 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1054 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1056 assert( lcp != NULL );
1057 assert( *lcp != NULL );
1059 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1061 if ( (*lcp)->lc_refcnt == 1 ) {
1062 Debug( LDAP_DEBUG_ANY,
1063 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1064 op->o_log_prefix, li->li_uri,
1065 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1066 "" : (*lcp)->lc_bound_ndn.bv_val );
1068 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1069 (*lcp)->lc_ld = NULL;
1070 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1072 /* lc here must be the regular lc, reset and ready for init */
1073 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1074 if ( rc != LDAP_SUCCESS ) {
1079 rc = ldap_back_dobind_int( *lcp, op, rs, sendok, 0, 0 );
1086 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1092 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1094 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1095 struct berval binddn = slap_empty_bv;
1096 struct berval bindcred = slap_empty_bv;
1102 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1103 ndn = op->o_conn->c_ndn;
1110 * FIXME: we need to let clients use proxyAuthz
1111 * otherwise we cannot do symmetric pools of servers;
1112 * we have to live with the fact that a user can
1113 * authorize itself as any ID that is allowed
1114 * by the authzTo directive of the "proxyauthzdn".
1117 * NOTE: current Proxy Authorization specification
1118 * and implementation do not allow proxy authorization
1119 * control to be provided with Bind requests
1122 * if no bind took place yet, but the connection is bound
1123 * and the "proxyauthzdn" is set, then bind as
1124 * "proxyauthzdn" and explicitly add the proxyAuthz
1125 * control to every operation with the dn bound
1126 * to the connection as control value.
1129 /* bind as proxyauthzdn only if no idassert mode
1130 * is requested, or if the client's identity
1132 switch ( li->li_idassert_mode ) {
1133 case LDAP_BACK_IDASSERT_LEGACY:
1134 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1135 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1137 binddn = li->li_idassert_authcDN;
1138 bindcred = li->li_idassert_passwd;
1145 /* NOTE: rootdn can always idassert */
1146 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1147 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1148 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1149 if ( sendok & LDAP_BACK_SENDERR ) {
1150 send_ldap_result( op, rs );
1152 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1155 rs->sr_err = LDAP_SUCCESS;
1156 binddn = slap_empty_bv;
1157 bindcred = slap_empty_bv;
1163 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1164 struct berval authcDN;
1166 if ( BER_BVISNULL( &ndn ) ) {
1167 authcDN = slap_empty_bv;
1172 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1173 &authcDN, &authcDN );
1174 if ( rs->sr_err != LDAP_SUCCESS ) {
1175 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1176 if ( sendok & LDAP_BACK_SENDERR ) {
1177 send_ldap_result( op, rs );
1179 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1182 rs->sr_err = LDAP_SUCCESS;
1183 binddn = slap_empty_bv;
1184 bindcred = slap_empty_bv;
1192 binddn = li->li_idassert_authcDN;
1193 bindcred = li->li_idassert_passwd;
1198 if ( dobind && li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1199 #ifdef HAVE_CYRUS_SASL
1200 void *defaults = NULL;
1201 struct berval authzID = BER_BVNULL;
1204 /* if SASL supports native authz, prepare for it */
1205 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1206 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1208 switch ( li->li_idassert_mode ) {
1209 case LDAP_BACK_IDASSERT_OTHERID:
1210 case LDAP_BACK_IDASSERT_OTHERDN:
1211 authzID = li->li_idassert_authzID;
1214 case LDAP_BACK_IDASSERT_ANONYMOUS:
1215 BER_BVSTR( &authzID, "dn:" );
1218 case LDAP_BACK_IDASSERT_SELF:
1219 if ( BER_BVISNULL( &ndn ) ) {
1220 /* connection is not authc'd, so don't idassert */
1221 BER_BVSTR( &authzID, "dn:" );
1224 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1225 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1226 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1227 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1228 ndn.bv_val, ndn.bv_len + 1 );
1237 if ( li->li_idassert_secprops != NULL ) {
1238 rs->sr_err = ldap_set_option( lc->lc_ld,
1239 LDAP_OPT_X_SASL_SECPROPS,
1240 (void *)li->li_idassert_secprops );
1242 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1243 rs->sr_err = LDAP_OTHER;
1244 if ( sendok & LDAP_BACK_SENDERR ) {
1245 send_ldap_result( op, rs );
1247 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1252 defaults = lutil_sasl_defaults( lc->lc_ld,
1253 li->li_idassert_sasl_mech.bv_val,
1254 li->li_idassert_sasl_realm.bv_val,
1255 li->li_idassert_authcID.bv_val,
1256 li->li_idassert_passwd.bv_val,
1259 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1260 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1261 LDAP_SASL_QUIET, lutil_sasl_interact,
1264 rs->sr_err = slap_map_api2result( rs );
1265 if ( rs->sr_err != LDAP_SUCCESS ) {
1266 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1267 if ( sendok & LDAP_BACK_SENDERR ) {
1268 send_ldap_result( op, rs );
1272 LDAP_BACK_CONN_ISBOUND_SET( lc );
1275 lutil_sasl_freedefs( defaults );
1277 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1281 #endif /* HAVE_CYRUS_SASL */
1284 switch ( li->li_idassert_authmethod ) {
1285 case LDAP_AUTH_NONE:
1286 LDAP_BACK_CONN_ISBOUND_SET( lc );
1289 case LDAP_AUTH_SIMPLE:
1290 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1291 binddn.bv_val, LDAP_SASL_SIMPLE,
1292 &bindcred, NULL, NULL, &msgid );
1297 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1298 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1299 if ( sendok & LDAP_BACK_SENDERR ) {
1300 send_ldap_result( op, rs );
1305 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1306 if ( rc == LDAP_SUCCESS ) {
1307 LDAP_BACK_CONN_ISBOUND_SET( lc );
1310 return LDAP_BACK_CONN_ISBOUND( lc );
1314 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1315 * to existing server-side controls if required; if not,
1316 * the existing server-side controls are placed in *pctrls.
1317 * The caller, after using the controls in client API
1318 * operations, if ( *pctrls != op->o_ctrls ), should
1319 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1320 * The function returns success if the control could
1321 * be added if required, or if it did nothing; in the future,
1322 * it might return some error if it failed.
1324 * if no bind took place yet, but the connection is bound
1325 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1326 * and explicitly add proxyAuthz the control to every operation
1327 * with the dn bound to the connection as control value.
1329 * If no server-side controls are defined for the operation,
1330 * simply add the proxyAuthz control; otherwise, if the
1331 * proxyAuthz control is not already set, add it as
1334 * FIXME: is controls order significant for security?
1335 * ANSWER: controls ordering and interoperability
1336 * must be indicated by the specs of each control; if none
1337 * is specified, the order is irrelevant.
1340 ldap_back_proxy_authz_ctrl(
1344 LDAPControl ***pctrls )
1346 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
1347 LDAPControl **ctrls = NULL;
1350 struct berval assertedID,
1355 rs->sr_err = LDAP_SUCCESS;
1357 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1358 * but if it is not set this test fails. We need a different
1359 * means to detect if idassert is enabled */
1360 if ( ( BER_BVISNULL( &li->li_idassert_authcID ) || BER_BVISEMPTY( &li->li_idassert_authcID ) )
1361 && ( BER_BVISNULL( &li->li_idassert_authcDN ) || BER_BVISEMPTY( &li->li_idassert_authcDN ) ) )
1366 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1370 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1371 ndn = op->o_conn->c_ndn;
1377 if ( li->li_idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1378 if ( op->o_proxy_authz ) {
1380 * FIXME: we do not want to perform proxyAuthz
1381 * on behalf of the client, because this would
1382 * be performed with "proxyauthzdn" privileges.
1384 * This might actually be too strict, since
1385 * the "proxyauthzdn" authzTo, and each entry's
1386 * authzFrom attributes may be crafted
1387 * to avoid unwanted proxyAuthz to take place.
1390 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1391 rs->sr_text = "proxyAuthz not allowed within namingContext";
1396 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1400 if ( BER_BVISNULL( &ndn ) ) {
1404 if ( BER_BVISNULL( &li->li_idassert_authcDN ) ) {
1408 } else if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1409 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1410 /* && ( !BER_BVISNULL( &ndn )
1411 || LDAP_BACK_CONN_ISBOUND( lc ) ) */ )
1413 /* already asserted in SASL via native authz */
1414 /* NOTE: the test on lc->lc_bound is used to trap
1415 * native authorization of anonymous users,
1416 * since in that case ndn is NULL */
1420 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1422 struct berval authcDN;
1424 if ( BER_BVISNULL( &ndn ) ) {
1425 authcDN = slap_empty_bv;
1429 rc = slap_sasl_matches( op, li->li_idassert_authz,
1430 &authcDN, & authcDN );
1431 if ( rc != LDAP_SUCCESS ) {
1432 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1434 /* ndn is not authorized
1435 * to use idassert */
1442 if ( op->o_proxy_authz ) {
1445 * 1) ignore the already set proxyAuthz control
1446 * 2) leave it in place, and don't set ours
1448 * 4) reject the operation
1450 * option (4) is very drastic
1451 * option (3) will make the remote server reject
1452 * the operation, thus being equivalent to (4)
1453 * option (2) will likely break the idassert
1454 * assumptions, so we cannot accept it;
1455 * option (1) means that we are contradicting
1456 * the client's reques.
1458 * I think (4) is the only correct choice.
1460 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1461 rs->sr_text = "proxyAuthz not allowed within namingContext";
1464 if ( op->o_is_auth_check ) {
1465 mode = LDAP_BACK_IDASSERT_NOASSERT;
1468 mode = li->li_idassert_mode;
1472 case LDAP_BACK_IDASSERT_SELF:
1473 if ( BER_BVISNULL( &ndn ) ) {
1479 case LDAP_BACK_IDASSERT_LEGACY:
1480 /* original behavior:
1481 * assert the client's identity */
1482 if ( BER_BVISNULL( &ndn ) ) {
1483 assertedID = slap_empty_bv;
1489 case LDAP_BACK_IDASSERT_ANONYMOUS:
1490 /* assert "anonymous" */
1491 assertedID = slap_empty_bv;
1494 case LDAP_BACK_IDASSERT_NOASSERT:
1495 /* don't assert; bind as proxyauthzdn */
1498 case LDAP_BACK_IDASSERT_OTHERID:
1499 case LDAP_BACK_IDASSERT_OTHERDN:
1500 /* assert idassert DN */
1501 assertedID = li->li_idassert_authzID;
1508 if ( BER_BVISNULL( &assertedID ) ) {
1509 assertedID = slap_empty_bv;
1512 if ( op->o_ctrls ) {
1513 for ( i = 0; op->o_ctrls[ i ]; i++ )
1514 /* just count ctrls */ ;
1517 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
1519 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
1521 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1522 ctrls[ 0 ]->ldctl_iscritical = 1;
1524 switch ( li->li_idassert_mode ) {
1525 /* already in u:ID or dn:DN form */
1526 case LDAP_BACK_IDASSERT_OTHERID:
1527 case LDAP_BACK_IDASSERT_OTHERDN:
1528 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
1531 /* needs the dn: prefix */
1533 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1534 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
1536 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1537 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1538 assertedID.bv_val, assertedID.bv_len + 1 );
1542 if ( op->o_ctrls ) {
1543 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1544 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1547 ctrls[ i + 1 ] = NULL;
1550 if ( ctrls == NULL ) {
1551 ctrls = op->o_ctrls;
1560 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1562 LDAPControl **ctrls = *pctrls;
1564 /* we assume that the first control is the proxyAuthz
1565 * added by back-ldap, so it's the only one we explicitly
1567 if ( ctrls && ctrls != op->o_ctrls ) {
1568 assert( ctrls[ 0 ] != NULL );
1570 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1571 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1574 op->o_tmpfree( ctrls, op->o_tmpmemctx );