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 assert( lc->lc_binding == 1 );
96 LDAP_BACK_CONN_ISBOUND_SET( lc );
97 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
99 if ( LDAP_BACK_SAVECRED( li ) ) {
100 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
101 memset( lc->lc_cred.bv_val, 0,
102 lc->lc_cred.bv_len );
104 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
105 ldap_set_rebind_proc( lc->lc_ld, ldap_back_rebind_f, lc );
110 /* must re-insert if local DN changed as result of bind */
111 if ( LDAP_BACK_CONN_ISBOUND( lc )
112 && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) )
116 /* wait for all other ops to release the connection */
118 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
119 if ( lc->lc_refcnt > 1 ) {
120 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
121 ldap_pvt_thread_yield();
125 assert( lc->lc_refcnt == 1 );
126 lc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
127 ldap_back_conndn_cmp );
128 assert( lc != NULL );
130 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
131 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
132 ldap_back_conndn_cmp, ldap_back_conndn_dup );
133 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
135 /* we can do this because lc_refcnt == 1 */
136 ldap_back_conn_free( lc );
142 ldap_back_release_conn( op, rs, lc );
149 * ldap_back_conndn_cmp
151 * compares two ldapconn_t based on the value of the conn pointer
152 * and of the local DN; used by avl stuff for insert, lookup
156 ldap_back_conndn_cmp( const void *c1, const void *c2 )
158 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
159 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
162 /* If local DNs don't match, it is definitely not a match */
163 /* For shared sessions, conn is NULL. Only explicitly
164 * bound sessions will have non-NULL conn.
166 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
168 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
177 * compares two ldapconn_t based on the value of the conn pointer;
178 * used by avl stuff for delete of all conns with the same connid
181 ldap_back_conn_cmp( const void *c1, const void *c2 )
183 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
184 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
186 /* For shared sessions, conn is NULL. Only explicitly
187 * bound sessions will have non-NULL conn.
189 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
193 * ldap_back_conndn_dup
195 * returns -1 in case a duplicate ldapconn_t has been inserted;
199 ldap_back_conndn_dup( void *c1, void *c2 )
201 ldapconn_t *lc1 = (ldapconn_t *)c1;
202 ldapconn_t *lc2 = (ldapconn_t *)c2;
204 /* Cannot have more than one shared session with same DN */
205 if ( lc1->lc_conn == lc2->lc_conn &&
206 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
214 #if PRINT_CONNTREE > 0
216 ravl_print( Avlnode *root, int depth )
225 ravl_print( root->avl_right, depth+1 );
227 for ( i = 0; i < depth; i++ ) {
228 fprintf( stderr, "-" );
232 fprintf( stderr, "lc=%p local=\"%s\" conn=%p %s\n",
233 (void *)lc, lc->lc_local_ndn.bv_val, (void *)lc->lc_conn,
234 avl_bf2str( root->avl_bf) );
236 ravl_print( root->avl_left, depth+1 );
240 myprint( Avlnode *root )
242 fprintf( stderr, "========>\n" );
245 fprintf( stderr, "\tNULL\n" );
248 ravl_print( root, 0 );
251 fprintf( stderr, "<========\n" );
253 #endif /* PRINT_CONNTREE */
256 ldap_back_freeconn( Operation *op, ldapconn_t *lc, int dolock )
258 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
261 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
264 assert( lc->lc_refcnt > 0 );
265 if ( --lc->lc_refcnt == 0 ) {
266 lc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
267 ldap_back_conndn_cmp );
268 assert( lc != NULL );
270 ldap_back_conn_free( (void *)lc );
274 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
291 int rc = LDAP_SUCCESS;
294 /* this is ridiculous... */
295 dummy.li_flags = flags;
297 /* start TLS ("tls-[try-]{start,propagate}" statements) */
298 if ( ( LDAP_BACK_USE_TLS( &dummy ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS( &dummy ) ) )
299 && !ldap_is_ldaps_url( url ) )
301 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
303 * use asynchronous StartTLS
304 * in case, chase referral (not implemented yet)
308 if ( protocol == 0 ) {
309 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
313 if ( protocol < LDAP_VERSION3 ) {
314 protocol = LDAP_VERSION3;
315 /* Set LDAP version */
316 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
317 (const void *)&protocol );
320 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
321 if ( rc == LDAP_SUCCESS ) {
322 LDAPMessage *res = NULL;
325 LDAP_BACK_TV_SET( &tv );
328 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
330 rc = LDAP_UNAVAILABLE;
332 } else if ( rc == 0 ) {
333 if ( retries != LDAP_BACK_RETRY_NEVER ) {
334 ldap_pvt_thread_yield();
338 LDAP_BACK_TV_SET( &tv );
341 rc = LDAP_UNAVAILABLE;
343 } else if ( rc == LDAP_RES_EXTENDED ) {
344 struct berval *data = NULL;
346 rc = ldap_parse_extended_result( ld, res,
348 if ( rc == LDAP_SUCCESS ) {
350 rc = ldap_parse_result( ld, res, &err,
351 NULL, NULL, NULL, NULL, 1 );
352 if ( rc == LDAP_SUCCESS ) {
357 /* FIXME: in case a referral
358 * is returned, should we try
359 * using it instead of the
361 if ( rc == LDAP_SUCCESS ) {
362 rc = ldap_install_tls( ld );
364 } else if ( rc == LDAP_REFERRAL ) {
365 rc = LDAP_UNWILLING_TO_PERFORM;
366 *text = "unwilling to chase referral returned by Start TLS exop";
370 if ( data->bv_val ) {
371 ber_memfree( data->bv_val );
385 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
387 * use synchronous StartTLS
389 rc = ldap_start_tls_s( ld, NULL, NULL );
390 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
392 /* if StartTLS is requested, only attempt it if the URL
393 * is not "ldaps://"; this may occur not only in case
394 * of misconfiguration, but also when used in the chain
395 * overlay, where the "uri" can be parsed out of a referral */
401 case LDAP_SERVER_DOWN:
405 if ( LDAP_BACK_TLS_CRITICAL( &dummy ) ) {
406 *text = "could not start TLS";
410 /* in case Start TLS is not critical */
422 #endif /* HAVE_TLS */
425 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
427 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
428 int vers = op->o_protocol;
431 int is_tls = op->o_conn->c_is_tls;
432 #endif /* HAVE_TLS */
434 assert( lcp != NULL );
436 rs->sr_err = ldap_initialize( &ld, li->li_uri );
437 if ( rs->sr_err != LDAP_SUCCESS ) {
441 /* Set LDAP version. This will always succeed: If the client
442 * bound with a particular version, then so can we.
445 /* assume it's an internal op; set to LDAPv3 */
446 vers = LDAP_VERSION3;
448 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
450 /* automatically chase referrals ("[dont-]chase-referrals" statement) */
451 if ( LDAP_BACK_CHASE_REFERRALS( li ) ) {
452 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
456 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
457 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
458 if ( rs->sr_err != LDAP_SUCCESS ) {
459 ldap_unbind_ext( ld, NULL, NULL );
462 #endif /* HAVE_TLS */
464 if ( *lcp == NULL ) {
465 *lcp = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
466 (*lcp)->lc_flags= li->li_flags;
469 (*lcp)->lc_refcnt = 1;
470 (*lcp)->lc_binding = 1;
473 LDAP_BACK_CONN_ISTLS_SET( *lcp );
475 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
477 #endif /* HAVE_TLS */
480 if ( rs->sr_err != LDAP_SUCCESS ) {
481 rs->sr_err = slap_map_api2result( rs );
482 if ( sendok & LDAP_BACK_SENDERR ) {
483 if ( rs->sr_text == NULL ) {
484 rs->sr_text = "ldap_initialize() failed";
486 send_ldap_result( op, rs );
491 if ( li->li_conn_ttl > 0 ) {
492 (*lcp)->lc_create_time = op->o_time;
500 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
502 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
503 ldapconn_t *lc = NULL,
505 int refcnt = 1, binding = 1;
507 /* Internal searches are privileged and shared. So is root. */
508 if ( op->o_do_not_cache || be_isroot( op ) ) {
509 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
510 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
511 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
514 lc_curr.lc_local_ndn = op->o_ndn;
515 /* Explicit binds must not be shared */
516 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
517 lc_curr.lc_conn = op->o_conn;
520 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
524 /* Explicit Bind requests always get their own conn */
525 if ( !( sendok & LDAP_BACK_BINDING ) ) {
526 /* Searches for a ldapconn in the avl tree */
528 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
530 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
531 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
533 /* Don't reuse connections while they're still binding */
534 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
535 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
536 ldap_pvt_thread_yield();
539 refcnt = ++lc->lc_refcnt;
540 binding = ++lc->lc_binding;
542 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
545 /* Looks like we didn't get a bind. Open a new session... */
547 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
550 if ( sendok & LDAP_BACK_BINDING ) {
551 LDAP_BACK_CONN_BINDING_SET( lc );
553 lc->lc_conn = lc_curr.lc_conn;
554 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
556 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
557 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
558 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
559 LDAP_BACK_CONN_ISPRIV_SET( lc );
562 BER_BVZERO( &lc->lc_cred );
563 BER_BVZERO( &lc->lc_bound_ndn );
565 /* FIXME: if we set lc_bound_ndn = o_ndn
566 * we end up with a bind with DN but no password! */
567 if ( !BER_BVISEMPTY( &op->o_ndn )
568 && SLAP_IS_AUTHZ_BACKEND( op ) )
570 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
576 /* if start TLS failed but it was not mandatory,
577 * check if the non-TLS connection was already
578 * in cache; in case, destroy the newly created
579 * connection and use the existing one */
580 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
581 && !ldap_tls_inplace( lc->lc_ld ) )
585 lc_curr.lc_conn = LDAP_BACK_PCONN;
586 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
587 tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
588 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
589 if ( tmplc != NULL ) {
590 refcnt = ++tmplc->lc_refcnt;
591 binding = ++tmplc->lc_binding;
592 ldap_back_conn_free( lc );
595 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
597 if ( tmplc != NULL ) {
601 #endif /* HAVE_TLS */
603 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
605 /* Inserts the newly created ldapconn in the avl tree */
606 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
608 assert( lc->lc_refcnt == 1 );
609 assert( lc->lc_binding == 1 );
610 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
611 ldap_back_conndn_cmp, ldap_back_conndn_dup );
613 #if PRINT_CONNTREE > 0
614 myprint( li->li_conninfo.lai_tree );
615 #endif /* PRINT_CONNTREE */
617 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
619 Debug( LDAP_DEBUG_TRACE,
620 "=>ldap_back_getconn: conn %p inserted refcnt=%u binding=%u\n",
621 (void *)lc, refcnt, binding );
623 /* Err could be -1 in case a duplicate ldapconn is inserted */
624 if ( rs->sr_err != 0 ) {
625 ldap_back_conn_free( lc );
626 rs->sr_err = LDAP_OTHER;
627 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
628 send_ldap_error( op, rs, LDAP_OTHER,
629 "internal server error" );
635 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
636 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
638 /* in case of failure, it frees/taints lc and sets it to NULL */
639 if ( !ldap_back_retry( &lc, op, rs, sendok ) ) {
645 Debug( LDAP_DEBUG_TRACE,
646 "=>ldap_back_getconn: conn %p fetched refcnt=%u binding=%u\n",
647 (void *)lc, refcnt, binding );
651 if ( li->li_idle_timeout && lc ) {
652 lc->lc_time = op->o_time;
660 ldap_back_release_conn_lock(
666 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
669 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
671 assert( lc->lc_refcnt > 0 );
673 LDAP_BACK_CONN_BINDING_CLEAR( lc );
675 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
682 * Note: as the check for the value of lc->lc_bound was already here, I removed
683 * it from all the callers, and I made the function return the flag, so
684 * it can be used to simplify the check.
686 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
689 ldap_back_dobind_int(
693 ldap_back_send_t sendok,
697 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
702 assert( retries >= 0 );
706 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
709 if ( binding == 0 ) {
710 /* check if already bound */
711 rc = LDAP_BACK_CONN_ISBOUND( lc );
715 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
720 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
721 /* if someone else is about to bind it, give up and retry */
723 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
725 ldap_pvt_thread_yield();
729 /* otherwise this thread will bind it */
730 LDAP_BACK_CONN_BINDING_SET( lc );
735 /* wait for pending operations to finish */
736 /* FIXME: may become a bottleneck! */
737 if ( lc->lc_refcnt != lc->lc_binding ) {
739 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
741 ldap_pvt_thread_yield();
746 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
750 while ( lc->lc_refcnt > 1 ) {
751 ldap_pvt_thread_yield();
752 rc = LDAP_BACK_CONN_ISBOUND( lc );
759 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
761 LDAP_BACK_CONN_BINDING_SET( lc );
763 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
768 * FIXME: we need to let clients use proxyAuthz
769 * otherwise we cannot do symmetric pools of servers;
770 * we have to live with the fact that a user can
771 * authorize itself as any ID that is allowed
772 * by the authzTo directive of the "proxyauthzdn".
775 * NOTE: current Proxy Authorization specification
776 * and implementation do not allow proxy authorization
777 * control to be provided with Bind requests
780 * if no bind took place yet, but the connection is bound
781 * and the "idassert-authcDN" (or other ID) is set,
782 * then bind as the asserting identity and explicitly
783 * add the proxyAuthz control to every operation with the
784 * dn bound to the connection as control value.
785 * This is done also if this is the authrizing backend,
786 * but the "override" flag is given to idassert.
787 * It allows to use SASL bind and yet proxyAuthz users
789 if ( op->o_conn != NULL &&
790 !op->o_do_not_cache &&
791 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
792 ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
794 (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok );
798 #ifdef HAVE_CYRUS_SASL
799 if ( LDAP_BACK_CONN_ISPRIV( lc )
800 && li->li_acl_authmethod == LDAP_AUTH_SASL )
802 void *defaults = NULL;
804 if ( li->li_acl_secprops != NULL ) {
805 rc = ldap_set_option( lc->lc_ld,
806 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops);
808 if ( rc != LDAP_OPT_SUCCESS ) {
809 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
810 "(%s,SECPROPS,\"%s\") failed!\n",
811 li->li_uri, li->li_acl_secprops, 0 );
816 defaults = lutil_sasl_defaults( lc->lc_ld,
817 li->li_acl_sasl_mech.bv_val,
818 li->li_acl_sasl_realm.bv_val,
819 li->li_acl_authcID.bv_val,
820 li->li_acl_passwd.bv_val,
823 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
824 li->li_acl_authcDN.bv_val,
825 li->li_acl_sasl_mech.bv_val, NULL, NULL,
826 LDAP_SASL_QUIET, lutil_sasl_interact,
829 lutil_sasl_freedefs( defaults );
831 rs->sr_err = slap_map_api2result( rs );
832 if ( rs->sr_err != LDAP_SUCCESS ) {
833 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
834 send_ldap_result( op, rs );
837 LDAP_BACK_CONN_ISBOUND_SET( lc );
841 #endif /* HAVE_CYRUS_SASL */
844 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
845 lc->lc_bound_ndn.bv_val,
846 LDAP_SASL_SIMPLE, &lc->lc_cred,
847 NULL, NULL, &msgid );
849 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
850 if ( retries != LDAP_BACK_RETRY_NEVER ) {
852 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
855 assert( lc->lc_refcnt > 0 );
856 if ( lc->lc_refcnt == 1 ) {
857 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
860 /* lc here must be the regular lc, reset and ready for init */
861 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
864 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
866 if ( rs->sr_err == LDAP_SUCCESS ) {
875 ldap_back_freeconn( op, lc, dolock );
876 rs->sr_err = slap_map_api2result( rs );
881 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
882 if ( rc == LDAP_SUCCESS ) {
883 LDAP_BACK_CONN_ISBOUND_SET( lc );
888 LDAP_BACK_CONN_BINDING_CLEAR( lc );
889 rc = LDAP_BACK_CONN_ISBOUND( lc );
891 ldap_back_release_conn_lock( op, rs, lc, dolock );
898 ldap_back_dobind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
900 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
902 return ldap_back_dobind_int( lc, op, rs, sendok, li->li_nretries, 1 );
906 * ldap_back_default_rebind
908 * This is a callback used for chasing referrals using the same
909 * credentials as the original user on this session.
912 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
913 ber_int_t msgid, void *params )
915 ldapconn_t *lc = (ldapconn_t *)params;
918 /* ... otherwise we couldn't get here */
919 assert( lc != NULL );
921 if ( !ldap_tls_inplace( ld ) ) {
922 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
924 const char *text = NULL;
926 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
927 LDAP_BACK_RETRY_DEFAULT, &text );
928 if ( rc != LDAP_SUCCESS ) {
932 #endif /* HAVE_TLS */
934 /* FIXME: add checks on the URL/identity? */
936 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
937 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
947 ldap_back_send_t sendok )
950 LDAPMessage *res = NULL;
953 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
956 rs->sr_matched = NULL;
958 /* if the error recorded in the reply corresponds
959 * to a successful state, get the error from the
960 * remote server response */
961 if ( ERR_OK( rs->sr_err ) ) {
970 LDAP_BACK_TV_SET( &tv );
974 /* if result parsing fails, note the failure reason */
975 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
979 (void)ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
980 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
981 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
982 rs->sr_text = "Operation timed out";
986 LDAP_BACK_TV_SET( &tv );
987 ldap_pvt_thread_yield();
991 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
996 /* otherwise get the result; if it is not
997 * LDAP_SUCCESS, record it in the reply
998 * structure (this includes
999 * LDAP_COMPARE_{TRUE|FALSE}) */
1001 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1002 &match, &text, NULL, NULL, 1 );
1004 if ( rc != LDAP_SUCCESS ) {
1010 /* if the error in the reply structure is not
1011 * LDAP_SUCCESS, try to map it from client
1012 * to server error */
1013 if ( !ERR_OK( rs->sr_err ) ) {
1014 rs->sr_err = slap_map_api2result( rs );
1016 /* internal ops ( op->o_conn == NULL )
1017 * must not reply to client */
1018 if ( op->o_conn && !op->o_do_not_cache && match ) {
1020 /* record the (massaged) matched
1021 * DN into the reply structure */
1022 rs->sr_matched = match;
1026 ( ( sendok & LDAP_BACK_SENDOK )
1027 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1029 send_ldap_result( op, rs );
1032 if ( rs->sr_matched != match ) {
1033 free( (char *)rs->sr_matched );
1035 rs->sr_matched = NULL;
1036 ldap_memfree( match );
1039 ldap_memfree( text );
1042 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1045 /* return true if bound, false if failed */
1047 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1050 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1052 assert( lcp != NULL );
1053 assert( *lcp != NULL );
1055 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1057 if ( (*lcp)->lc_refcnt == 1 ) {
1058 Debug( LDAP_DEBUG_ANY,
1059 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1060 op->o_log_prefix, li->li_uri,
1061 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1062 "" : (*lcp)->lc_bound_ndn.bv_val );
1064 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1065 (*lcp)->lc_ld = NULL;
1066 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1068 /* lc here must be the regular lc, reset and ready for init */
1069 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1070 if ( rc != LDAP_SUCCESS ) {
1075 rc = ldap_back_dobind_int( *lcp, op, rs, sendok, 0, 0 );
1082 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1088 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1090 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1091 struct berval binddn = slap_empty_bv;
1092 struct berval bindcred = slap_empty_bv;
1098 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1099 ndn = op->o_conn->c_ndn;
1106 * FIXME: we need to let clients use proxyAuthz
1107 * otherwise we cannot do symmetric pools of servers;
1108 * we have to live with the fact that a user can
1109 * authorize itself as any ID that is allowed
1110 * by the authzTo directive of the "proxyauthzdn".
1113 * NOTE: current Proxy Authorization specification
1114 * and implementation do not allow proxy authorization
1115 * control to be provided with Bind requests
1118 * if no bind took place yet, but the connection is bound
1119 * and the "proxyauthzdn" is set, then bind as
1120 * "proxyauthzdn" and explicitly add the proxyAuthz
1121 * control to every operation with the dn bound
1122 * to the connection as control value.
1125 /* bind as proxyauthzdn only if no idassert mode
1126 * is requested, or if the client's identity
1128 switch ( li->li_idassert_mode ) {
1129 case LDAP_BACK_IDASSERT_LEGACY:
1130 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1131 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1133 binddn = li->li_idassert_authcDN;
1134 bindcred = li->li_idassert_passwd;
1141 /* NOTE: rootdn can always idassert */
1142 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1143 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1144 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1145 if ( sendok & LDAP_BACK_SENDERR ) {
1146 send_ldap_result( op, rs );
1148 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1151 rs->sr_err = LDAP_SUCCESS;
1152 binddn = slap_empty_bv;
1153 bindcred = slap_empty_bv;
1159 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1160 struct berval authcDN;
1162 if ( BER_BVISNULL( &ndn ) ) {
1163 authcDN = slap_empty_bv;
1168 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1169 &authcDN, &authcDN );
1170 if ( rs->sr_err != LDAP_SUCCESS ) {
1171 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1172 if ( sendok & LDAP_BACK_SENDERR ) {
1173 send_ldap_result( op, rs );
1175 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1178 rs->sr_err = LDAP_SUCCESS;
1179 binddn = slap_empty_bv;
1180 bindcred = slap_empty_bv;
1188 binddn = li->li_idassert_authcDN;
1189 bindcred = li->li_idassert_passwd;
1194 if ( dobind && li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1195 #ifdef HAVE_CYRUS_SASL
1196 void *defaults = NULL;
1197 struct berval authzID = BER_BVNULL;
1200 /* if SASL supports native authz, prepare for it */
1201 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1202 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1204 switch ( li->li_idassert_mode ) {
1205 case LDAP_BACK_IDASSERT_OTHERID:
1206 case LDAP_BACK_IDASSERT_OTHERDN:
1207 authzID = li->li_idassert_authzID;
1210 case LDAP_BACK_IDASSERT_ANONYMOUS:
1211 BER_BVSTR( &authzID, "dn:" );
1214 case LDAP_BACK_IDASSERT_SELF:
1215 if ( BER_BVISNULL( &ndn ) ) {
1216 /* connection is not authc'd, so don't idassert */
1217 BER_BVSTR( &authzID, "dn:" );
1220 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1221 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1222 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1223 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1224 ndn.bv_val, ndn.bv_len + 1 );
1233 if ( li->li_idassert_secprops != NULL ) {
1234 rs->sr_err = ldap_set_option( lc->lc_ld,
1235 LDAP_OPT_X_SASL_SECPROPS,
1236 (void *)li->li_idassert_secprops );
1238 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1239 rs->sr_err = LDAP_OTHER;
1240 if ( sendok & LDAP_BACK_SENDERR ) {
1241 send_ldap_result( op, rs );
1243 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1248 defaults = lutil_sasl_defaults( lc->lc_ld,
1249 li->li_idassert_sasl_mech.bv_val,
1250 li->li_idassert_sasl_realm.bv_val,
1251 li->li_idassert_authcID.bv_val,
1252 li->li_idassert_passwd.bv_val,
1255 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1256 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1257 LDAP_SASL_QUIET, lutil_sasl_interact,
1260 rs->sr_err = slap_map_api2result( rs );
1261 if ( rs->sr_err != LDAP_SUCCESS ) {
1262 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1263 if ( sendok & LDAP_BACK_SENDERR ) {
1264 send_ldap_result( op, rs );
1268 LDAP_BACK_CONN_ISBOUND_SET( lc );
1271 lutil_sasl_freedefs( defaults );
1273 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1277 #endif /* HAVE_CYRUS_SASL */
1280 switch ( li->li_idassert_authmethod ) {
1281 case LDAP_AUTH_NONE:
1282 LDAP_BACK_CONN_ISBOUND_SET( lc );
1285 case LDAP_AUTH_SIMPLE:
1286 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1287 binddn.bv_val, LDAP_SASL_SIMPLE,
1288 &bindcred, NULL, NULL, &msgid );
1293 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1294 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1295 if ( sendok & LDAP_BACK_SENDERR ) {
1296 send_ldap_result( op, rs );
1301 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1302 if ( rc == LDAP_SUCCESS ) {
1303 LDAP_BACK_CONN_ISBOUND_SET( lc );
1306 return LDAP_BACK_CONN_ISBOUND( lc );
1310 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1311 * to existing server-side controls if required; if not,
1312 * the existing server-side controls are placed in *pctrls.
1313 * The caller, after using the controls in client API
1314 * operations, if ( *pctrls != op->o_ctrls ), should
1315 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1316 * The function returns success if the control could
1317 * be added if required, or if it did nothing; in the future,
1318 * it might return some error if it failed.
1320 * if no bind took place yet, but the connection is bound
1321 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1322 * and explicitly add proxyAuthz the control to every operation
1323 * with the dn bound to the connection as control value.
1325 * If no server-side controls are defined for the operation,
1326 * simply add the proxyAuthz control; otherwise, if the
1327 * proxyAuthz control is not already set, add it as
1330 * FIXME: is controls order significant for security?
1331 * ANSWER: controls ordering and interoperability
1332 * must be indicated by the specs of each control; if none
1333 * is specified, the order is irrelevant.
1336 ldap_back_proxy_authz_ctrl(
1340 LDAPControl ***pctrls )
1342 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
1343 LDAPControl **ctrls = NULL;
1346 struct berval assertedID,
1351 rs->sr_err = LDAP_SUCCESS;
1353 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1354 * but if it is not set this test fails. We need a different
1355 * means to detect if idassert is enabled */
1356 if ( ( BER_BVISNULL( &li->li_idassert_authcID ) || BER_BVISEMPTY( &li->li_idassert_authcID ) )
1357 && ( BER_BVISNULL( &li->li_idassert_authcDN ) || BER_BVISEMPTY( &li->li_idassert_authcDN ) ) )
1362 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1366 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1367 ndn = op->o_conn->c_ndn;
1373 if ( li->li_idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1374 if ( op->o_proxy_authz ) {
1376 * FIXME: we do not want to perform proxyAuthz
1377 * on behalf of the client, because this would
1378 * be performed with "proxyauthzdn" privileges.
1380 * This might actually be too strict, since
1381 * the "proxyauthzdn" authzTo, and each entry's
1382 * authzFrom attributes may be crafted
1383 * to avoid unwanted proxyAuthz to take place.
1386 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1387 rs->sr_text = "proxyAuthz not allowed within namingContext";
1392 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1396 if ( BER_BVISNULL( &ndn ) ) {
1400 if ( BER_BVISNULL( &li->li_idassert_authcDN ) ) {
1404 } else if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1405 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1406 /* && ( !BER_BVISNULL( &ndn )
1407 || LDAP_BACK_CONN_ISBOUND( lc ) ) */ )
1409 /* already asserted in SASL via native authz */
1410 /* NOTE: the test on lc->lc_bound is used to trap
1411 * native authorization of anonymous users,
1412 * since in that case ndn is NULL */
1416 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1418 struct berval authcDN;
1420 if ( BER_BVISNULL( &ndn ) ) {
1421 authcDN = slap_empty_bv;
1425 rc = slap_sasl_matches( op, li->li_idassert_authz,
1426 &authcDN, & authcDN );
1427 if ( rc != LDAP_SUCCESS ) {
1428 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1430 /* ndn is not authorized
1431 * to use idassert */
1438 if ( op->o_proxy_authz ) {
1441 * 1) ignore the already set proxyAuthz control
1442 * 2) leave it in place, and don't set ours
1444 * 4) reject the operation
1446 * option (4) is very drastic
1447 * option (3) will make the remote server reject
1448 * the operation, thus being equivalent to (4)
1449 * option (2) will likely break the idassert
1450 * assumptions, so we cannot accept it;
1451 * option (1) means that we are contradicting
1452 * the client's reques.
1454 * I think (4) is the only correct choice.
1456 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1457 rs->sr_text = "proxyAuthz not allowed within namingContext";
1460 if ( op->o_is_auth_check ) {
1461 mode = LDAP_BACK_IDASSERT_NOASSERT;
1464 mode = li->li_idassert_mode;
1468 case LDAP_BACK_IDASSERT_SELF:
1469 if ( BER_BVISNULL( &ndn ) ) {
1475 case LDAP_BACK_IDASSERT_LEGACY:
1476 /* original behavior:
1477 * assert the client's identity */
1478 if ( BER_BVISNULL( &ndn ) ) {
1479 assertedID = slap_empty_bv;
1485 case LDAP_BACK_IDASSERT_ANONYMOUS:
1486 /* assert "anonymous" */
1487 assertedID = slap_empty_bv;
1490 case LDAP_BACK_IDASSERT_NOASSERT:
1491 /* don't assert; bind as proxyauthzdn */
1494 case LDAP_BACK_IDASSERT_OTHERID:
1495 case LDAP_BACK_IDASSERT_OTHERDN:
1496 /* assert idassert DN */
1497 assertedID = li->li_idassert_authzID;
1504 if ( BER_BVISNULL( &assertedID ) ) {
1505 assertedID = slap_empty_bv;
1508 if ( op->o_ctrls ) {
1509 for ( i = 0; op->o_ctrls[ i ]; i++ )
1510 /* just count ctrls */ ;
1513 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
1515 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
1517 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1518 ctrls[ 0 ]->ldctl_iscritical = 1;
1520 switch ( li->li_idassert_mode ) {
1521 /* already in u:ID or dn:DN form */
1522 case LDAP_BACK_IDASSERT_OTHERID:
1523 case LDAP_BACK_IDASSERT_OTHERDN:
1524 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
1527 /* needs the dn: prefix */
1529 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1530 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
1532 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1533 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1534 assertedID.bv_val, assertedID.bv_len + 1 );
1538 if ( op->o_ctrls ) {
1539 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1540 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1543 ctrls[ i + 1 ] = NULL;
1546 if ( ctrls == NULL ) {
1547 ctrls = op->o_ctrls;
1556 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1558 LDAPControl **ctrls = *pctrls;
1560 /* we assume that the first control is the proxyAuthz
1561 * added by back-ldap, so it's the only one we explicitly
1563 if ( ctrls && ctrls != op->o_ctrls ) {
1564 assert( ctrls[ 0 ] != NULL );
1566 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1567 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1570 op->o_tmpfree( ctrls, op->o_tmpmemctx );