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 refcnt=%d\n",
233 (void *)lc, lc->lc_local_ndn.bv_val, (void *)lc->lc_conn,
234 avl_bf2str( root->avl_bf ), lc->lc_refcnt );
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 ("chase-referrals [{yes|no}" statement) */
451 ldap_set_option( ld, LDAP_OPT_REFERRALS,
452 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
455 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
456 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
457 if ( rs->sr_err != LDAP_SUCCESS ) {
458 ldap_unbind_ext( ld, NULL, NULL );
461 #endif /* HAVE_TLS */
463 if ( *lcp == NULL ) {
464 *lcp = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
465 (*lcp)->lc_flags= li->li_flags;
468 (*lcp)->lc_refcnt = 1;
469 (*lcp)->lc_binding = 1;
472 LDAP_BACK_CONN_ISTLS_SET( *lcp );
474 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
476 #endif /* HAVE_TLS */
479 if ( rs->sr_err != LDAP_SUCCESS ) {
480 rs->sr_err = slap_map_api2result( rs );
481 if ( sendok & LDAP_BACK_SENDERR ) {
482 if ( rs->sr_text == NULL ) {
483 rs->sr_text = "ldap_initialize() failed";
485 send_ldap_result( op, rs );
490 if ( li->li_conn_ttl > 0 ) {
491 (*lcp)->lc_create_time = op->o_time;
499 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
501 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
502 ldapconn_t *lc = NULL,
504 int refcnt = 1, binding = 1;
506 /* Internal searches are privileged and shared. So is root. */
507 if ( op->o_do_not_cache || be_isroot( op ) ) {
508 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
509 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
510 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
513 lc_curr.lc_local_ndn = op->o_ndn;
514 /* Explicit binds must not be shared */
515 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
516 lc_curr.lc_conn = op->o_conn;
519 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
523 /* Explicit Bind requests always get their own conn */
524 if ( !( sendok & LDAP_BACK_BINDING ) ) {
525 /* Searches for a ldapconn in the avl tree */
527 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
529 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
530 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
532 /* Don't reuse connections while they're still binding */
533 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
534 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
535 ldap_pvt_thread_yield();
538 refcnt = ++lc->lc_refcnt;
539 binding = ++lc->lc_binding;
541 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
544 /* Looks like we didn't get a bind. Open a new session... */
546 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
549 if ( sendok & LDAP_BACK_BINDING ) {
550 LDAP_BACK_CONN_BINDING_SET( lc );
552 lc->lc_conn = lc_curr.lc_conn;
553 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
555 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
556 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
557 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
558 LDAP_BACK_CONN_ISPRIV_SET( lc );
561 BER_BVZERO( &lc->lc_cred );
562 BER_BVZERO( &lc->lc_bound_ndn );
564 /* FIXME: if we set lc_bound_ndn = o_ndn
565 * we end up with a bind with DN but no password! */
566 if ( !BER_BVISEMPTY( &op->o_ndn )
567 && SLAP_IS_AUTHZ_BACKEND( op ) )
569 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
575 /* if start TLS failed but it was not mandatory,
576 * check if the non-TLS connection was already
577 * in cache; in case, destroy the newly created
578 * connection and use the existing one */
579 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
580 && !ldap_tls_inplace( lc->lc_ld ) )
584 lc_curr.lc_conn = LDAP_BACK_PCONN;
585 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
586 tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
587 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
588 if ( tmplc != NULL ) {
589 refcnt = ++tmplc->lc_refcnt;
590 binding = ++tmplc->lc_binding;
591 ldap_back_conn_free( lc );
594 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
596 if ( tmplc != NULL ) {
600 #endif /* HAVE_TLS */
602 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
604 /* Inserts the newly created ldapconn in the avl tree */
605 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
607 assert( lc->lc_refcnt == 1 );
608 assert( lc->lc_binding == 1 );
609 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
610 ldap_back_conndn_cmp, ldap_back_conndn_dup );
612 #if PRINT_CONNTREE > 0
613 myprint( li->li_conninfo.lai_tree );
614 #endif /* PRINT_CONNTREE */
616 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
618 Debug( LDAP_DEBUG_TRACE,
619 "=>ldap_back_getconn: conn %p inserted refcnt=%u binding=%u\n",
620 (void *)lc, refcnt, binding );
622 /* Err could be -1 in case a duplicate ldapconn is inserted */
623 if ( rs->sr_err != 0 ) {
624 ldap_back_conn_free( lc );
625 rs->sr_err = LDAP_OTHER;
626 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
627 send_ldap_error( op, rs, LDAP_OTHER,
628 "internal server error" );
634 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
635 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
637 /* in case of failure, it frees/taints lc and sets it to NULL */
638 if ( !ldap_back_retry( &lc, op, rs, sendok ) ) {
644 Debug( LDAP_DEBUG_TRACE,
645 "=>ldap_back_getconn: conn %p fetched refcnt=%u binding=%u\n",
646 (void *)lc, refcnt, binding );
650 if ( li->li_idle_timeout && lc ) {
651 lc->lc_time = op->o_time;
659 ldap_back_release_conn_lock(
665 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
668 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
670 assert( lc->lc_refcnt > 0 );
672 LDAP_BACK_CONN_BINDING_CLEAR( lc );
674 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
681 * Note: as the check for the value of lc->lc_bound was already here, I removed
682 * it from all the callers, and I made the function return the flag, so
683 * it can be used to simplify the check.
685 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
688 ldap_back_dobind_int(
692 ldap_back_send_t sendok,
696 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
701 assert( retries >= 0 );
705 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
708 if ( binding == 0 ) {
709 /* check if already bound */
710 rc = LDAP_BACK_CONN_ISBOUND( lc );
714 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
719 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
720 /* if someone else is about to bind it, give up and retry */
722 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
724 ldap_pvt_thread_yield();
728 /* otherwise this thread will bind it */
729 LDAP_BACK_CONN_BINDING_SET( lc );
734 /* wait for pending operations to finish */
735 /* FIXME: may become a bottleneck! */
736 if ( lc->lc_refcnt != lc->lc_binding ) {
738 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
740 ldap_pvt_thread_yield();
745 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
749 while ( lc->lc_refcnt > 1 ) {
750 ldap_pvt_thread_yield();
751 rc = LDAP_BACK_CONN_ISBOUND( lc );
758 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
760 LDAP_BACK_CONN_BINDING_SET( lc );
762 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
767 * FIXME: we need to let clients use proxyAuthz
768 * otherwise we cannot do symmetric pools of servers;
769 * we have to live with the fact that a user can
770 * authorize itself as any ID that is allowed
771 * by the authzTo directive of the "proxyauthzdn".
774 * NOTE: current Proxy Authorization specification
775 * and implementation do not allow proxy authorization
776 * control to be provided with Bind requests
779 * if no bind took place yet, but the connection is bound
780 * and the "idassert-authcDN" (or other ID) is set,
781 * then bind as the asserting identity and explicitly
782 * add the proxyAuthz control to every operation with the
783 * dn bound to the connection as control value.
784 * This is done also if this is the authrizing backend,
785 * but the "override" flag is given to idassert.
786 * It allows to use SASL bind and yet proxyAuthz users
788 if ( op->o_conn != NULL &&
789 !op->o_do_not_cache &&
790 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
791 ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
793 (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok );
797 #ifdef HAVE_CYRUS_SASL
798 if ( LDAP_BACK_CONN_ISPRIV( lc )
799 && li->li_acl_authmethod == LDAP_AUTH_SASL )
801 void *defaults = NULL;
803 if ( li->li_acl_secprops != NULL ) {
804 rc = ldap_set_option( lc->lc_ld,
805 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops);
807 if ( rc != LDAP_OPT_SUCCESS ) {
808 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
809 "(%s,SECPROPS,\"%s\") failed!\n",
810 li->li_uri, li->li_acl_secprops, 0 );
815 defaults = lutil_sasl_defaults( lc->lc_ld,
816 li->li_acl_sasl_mech.bv_val,
817 li->li_acl_sasl_realm.bv_val,
818 li->li_acl_authcID.bv_val,
819 li->li_acl_passwd.bv_val,
822 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
823 li->li_acl_authcDN.bv_val,
824 li->li_acl_sasl_mech.bv_val, NULL, NULL,
825 LDAP_SASL_QUIET, lutil_sasl_interact,
828 lutil_sasl_freedefs( defaults );
830 rs->sr_err = slap_map_api2result( rs );
831 if ( rs->sr_err != LDAP_SUCCESS ) {
832 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
833 send_ldap_result( op, rs );
836 LDAP_BACK_CONN_ISBOUND_SET( lc );
840 #endif /* HAVE_CYRUS_SASL */
843 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
844 lc->lc_bound_ndn.bv_val,
845 LDAP_SASL_SIMPLE, &lc->lc_cred,
846 NULL, NULL, &msgid );
848 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
849 if ( retries != LDAP_BACK_RETRY_NEVER ) {
851 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
854 assert( lc->lc_refcnt > 0 );
855 if ( lc->lc_refcnt == 1 ) {
856 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
859 /* lc here must be the regular lc, reset and ready for init */
860 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
863 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
865 if ( rs->sr_err == LDAP_SUCCESS ) {
874 ldap_back_freeconn( op, lc, dolock );
875 rs->sr_err = slap_map_api2result( rs );
880 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
881 if ( rc == LDAP_SUCCESS ) {
882 LDAP_BACK_CONN_ISBOUND_SET( lc );
887 LDAP_BACK_CONN_BINDING_CLEAR( lc );
888 rc = LDAP_BACK_CONN_ISBOUND( lc );
890 ldap_back_release_conn_lock( op, rs, lc, dolock );
897 ldap_back_dobind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
899 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
901 return ldap_back_dobind_int( lc, op, rs, sendok, li->li_nretries, 1 );
905 * ldap_back_default_rebind
907 * This is a callback used for chasing referrals using the same
908 * credentials as the original user on this session.
911 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
912 ber_int_t msgid, void *params )
914 ldapconn_t *lc = (ldapconn_t *)params;
917 /* ... otherwise we couldn't get here */
918 assert( lc != NULL );
920 if ( !ldap_tls_inplace( ld ) ) {
921 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
923 const char *text = NULL;
925 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
926 LDAP_BACK_RETRY_DEFAULT, &text );
927 if ( rc != LDAP_SUCCESS ) {
931 #endif /* HAVE_TLS */
933 /* FIXME: add checks on the URL/identity? */
935 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
936 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
946 ldap_back_send_t sendok )
949 LDAPMessage *res = NULL;
952 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
955 rs->sr_matched = NULL;
957 /* if the error recorded in the reply corresponds
958 * to a successful state, get the error from the
959 * remote server response */
960 if ( ERR_OK( rs->sr_err ) ) {
969 LDAP_BACK_TV_SET( &tv );
973 /* if result parsing fails, note the failure reason */
974 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
978 (void)ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
979 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
980 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
981 rs->sr_text = "Operation timed out";
985 LDAP_BACK_TV_SET( &tv );
986 ldap_pvt_thread_yield();
990 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
995 /* otherwise get the result; if it is not
996 * LDAP_SUCCESS, record it in the reply
997 * structure (this includes
998 * LDAP_COMPARE_{TRUE|FALSE}) */
1000 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1001 &match, &text, NULL, NULL, 1 );
1003 if ( rc != LDAP_SUCCESS ) {
1009 /* if the error in the reply structure is not
1010 * LDAP_SUCCESS, try to map it from client
1011 * to server error */
1012 if ( !ERR_OK( rs->sr_err ) ) {
1013 rs->sr_err = slap_map_api2result( rs );
1015 /* internal ops ( op->o_conn == NULL )
1016 * must not reply to client */
1017 if ( op->o_conn && !op->o_do_not_cache && match ) {
1019 /* record the (massaged) matched
1020 * DN into the reply structure */
1021 rs->sr_matched = match;
1025 ( ( sendok & LDAP_BACK_SENDOK )
1026 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1028 send_ldap_result( op, rs );
1031 if ( rs->sr_matched != match ) {
1032 free( (char *)rs->sr_matched );
1034 rs->sr_matched = NULL;
1035 ldap_memfree( match );
1038 ldap_memfree( text );
1041 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1044 /* return true if bound, false if failed */
1046 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1049 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1051 assert( lcp != NULL );
1052 assert( *lcp != NULL );
1054 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1056 if ( (*lcp)->lc_refcnt == 1 ) {
1057 Debug( LDAP_DEBUG_ANY,
1058 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1059 op->o_log_prefix, li->li_uri,
1060 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1061 "" : (*lcp)->lc_bound_ndn.bv_val );
1063 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1064 (*lcp)->lc_ld = NULL;
1065 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1067 /* lc here must be the regular lc, reset and ready for init */
1068 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1069 if ( rc != LDAP_SUCCESS ) {
1074 rc = ldap_back_dobind_int( *lcp, op, rs, sendok, 0, 0 );
1081 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1087 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1089 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1090 struct berval binddn = slap_empty_bv;
1091 struct berval bindcred = slap_empty_bv;
1097 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1098 ndn = op->o_conn->c_ndn;
1105 * FIXME: we need to let clients use proxyAuthz
1106 * otherwise we cannot do symmetric pools of servers;
1107 * we have to live with the fact that a user can
1108 * authorize itself as any ID that is allowed
1109 * by the authzTo directive of the "proxyauthzdn".
1112 * NOTE: current Proxy Authorization specification
1113 * and implementation do not allow proxy authorization
1114 * control to be provided with Bind requests
1117 * if no bind took place yet, but the connection is bound
1118 * and the "proxyauthzdn" is set, then bind as
1119 * "proxyauthzdn" and explicitly add the proxyAuthz
1120 * control to every operation with the dn bound
1121 * to the connection as control value.
1124 /* bind as proxyauthzdn only if no idassert mode
1125 * is requested, or if the client's identity
1127 switch ( li->li_idassert_mode ) {
1128 case LDAP_BACK_IDASSERT_LEGACY:
1129 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1130 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1132 binddn = li->li_idassert_authcDN;
1133 bindcred = li->li_idassert_passwd;
1140 /* NOTE: rootdn can always idassert */
1141 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1142 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1143 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1144 if ( sendok & LDAP_BACK_SENDERR ) {
1145 send_ldap_result( op, rs );
1147 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1150 rs->sr_err = LDAP_SUCCESS;
1151 binddn = slap_empty_bv;
1152 bindcred = slap_empty_bv;
1158 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1159 struct berval authcDN;
1161 if ( BER_BVISNULL( &ndn ) ) {
1162 authcDN = slap_empty_bv;
1167 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1168 &authcDN, &authcDN );
1169 if ( rs->sr_err != LDAP_SUCCESS ) {
1170 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1171 if ( sendok & LDAP_BACK_SENDERR ) {
1172 send_ldap_result( op, rs );
1174 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1177 rs->sr_err = LDAP_SUCCESS;
1178 binddn = slap_empty_bv;
1179 bindcred = slap_empty_bv;
1187 binddn = li->li_idassert_authcDN;
1188 bindcred = li->li_idassert_passwd;
1193 if ( dobind && li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1194 #ifdef HAVE_CYRUS_SASL
1195 void *defaults = NULL;
1196 struct berval authzID = BER_BVNULL;
1199 /* if SASL supports native authz, prepare for it */
1200 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1201 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1203 switch ( li->li_idassert_mode ) {
1204 case LDAP_BACK_IDASSERT_OTHERID:
1205 case LDAP_BACK_IDASSERT_OTHERDN:
1206 authzID = li->li_idassert_authzID;
1209 case LDAP_BACK_IDASSERT_ANONYMOUS:
1210 BER_BVSTR( &authzID, "dn:" );
1213 case LDAP_BACK_IDASSERT_SELF:
1214 if ( BER_BVISNULL( &ndn ) ) {
1215 /* connection is not authc'd, so don't idassert */
1216 BER_BVSTR( &authzID, "dn:" );
1219 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1220 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1221 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1222 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1223 ndn.bv_val, ndn.bv_len + 1 );
1232 if ( li->li_idassert_secprops != NULL ) {
1233 rs->sr_err = ldap_set_option( lc->lc_ld,
1234 LDAP_OPT_X_SASL_SECPROPS,
1235 (void *)li->li_idassert_secprops );
1237 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1238 rs->sr_err = LDAP_OTHER;
1239 if ( sendok & LDAP_BACK_SENDERR ) {
1240 send_ldap_result( op, rs );
1242 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1247 defaults = lutil_sasl_defaults( lc->lc_ld,
1248 li->li_idassert_sasl_mech.bv_val,
1249 li->li_idassert_sasl_realm.bv_val,
1250 li->li_idassert_authcID.bv_val,
1251 li->li_idassert_passwd.bv_val,
1254 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1255 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1256 LDAP_SASL_QUIET, lutil_sasl_interact,
1259 rs->sr_err = slap_map_api2result( rs );
1260 if ( rs->sr_err != LDAP_SUCCESS ) {
1261 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1262 if ( sendok & LDAP_BACK_SENDERR ) {
1263 send_ldap_result( op, rs );
1267 LDAP_BACK_CONN_ISBOUND_SET( lc );
1270 lutil_sasl_freedefs( defaults );
1272 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1276 #endif /* HAVE_CYRUS_SASL */
1279 switch ( li->li_idassert_authmethod ) {
1280 case LDAP_AUTH_NONE:
1281 LDAP_BACK_CONN_ISBOUND_SET( lc );
1284 case LDAP_AUTH_SIMPLE:
1285 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1286 binddn.bv_val, LDAP_SASL_SIMPLE,
1287 &bindcred, NULL, NULL, &msgid );
1292 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1293 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1294 if ( sendok & LDAP_BACK_SENDERR ) {
1295 send_ldap_result( op, rs );
1300 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1301 if ( rc == LDAP_SUCCESS ) {
1302 LDAP_BACK_CONN_ISBOUND_SET( lc );
1305 return LDAP_BACK_CONN_ISBOUND( lc );
1309 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1310 * to existing server-side controls if required; if not,
1311 * the existing server-side controls are placed in *pctrls.
1312 * The caller, after using the controls in client API
1313 * operations, if ( *pctrls != op->o_ctrls ), should
1314 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1315 * The function returns success if the control could
1316 * be added if required, or if it did nothing; in the future,
1317 * it might return some error if it failed.
1319 * if no bind took place yet, but the connection is bound
1320 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1321 * and explicitly add proxyAuthz the control to every operation
1322 * with the dn bound to the connection as control value.
1324 * If no server-side controls are defined for the operation,
1325 * simply add the proxyAuthz control; otherwise, if the
1326 * proxyAuthz control is not already set, add it as
1329 * FIXME: is controls order significant for security?
1330 * ANSWER: controls ordering and interoperability
1331 * must be indicated by the specs of each control; if none
1332 * is specified, the order is irrelevant.
1335 ldap_back_proxy_authz_ctrl(
1339 LDAPControl ***pctrls )
1341 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
1342 LDAPControl **ctrls = NULL;
1345 struct berval assertedID,
1350 rs->sr_err = LDAP_SUCCESS;
1352 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1353 * but if it is not set this test fails. We need a different
1354 * means to detect if idassert is enabled */
1355 if ( ( BER_BVISNULL( &li->li_idassert_authcID ) || BER_BVISEMPTY( &li->li_idassert_authcID ) )
1356 && ( BER_BVISNULL( &li->li_idassert_authcDN ) || BER_BVISEMPTY( &li->li_idassert_authcDN ) ) )
1361 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1365 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1366 ndn = op->o_conn->c_ndn;
1372 if ( li->li_idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1373 if ( op->o_proxy_authz ) {
1375 * FIXME: we do not want to perform proxyAuthz
1376 * on behalf of the client, because this would
1377 * be performed with "proxyauthzdn" privileges.
1379 * This might actually be too strict, since
1380 * the "proxyauthzdn" authzTo, and each entry's
1381 * authzFrom attributes may be crafted
1382 * to avoid unwanted proxyAuthz to take place.
1385 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1386 rs->sr_text = "proxyAuthz not allowed within namingContext";
1391 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1395 if ( BER_BVISNULL( &ndn ) ) {
1399 if ( BER_BVISNULL( &li->li_idassert_authcDN ) ) {
1403 } else if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1404 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1405 /* && ( !BER_BVISNULL( &ndn )
1406 || LDAP_BACK_CONN_ISBOUND( lc ) ) */ )
1408 /* already asserted in SASL via native authz */
1409 /* NOTE: the test on lc->lc_bound is used to trap
1410 * native authorization of anonymous users,
1411 * since in that case ndn is NULL */
1415 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1417 struct berval authcDN;
1419 if ( BER_BVISNULL( &ndn ) ) {
1420 authcDN = slap_empty_bv;
1424 rc = slap_sasl_matches( op, li->li_idassert_authz,
1425 &authcDN, & authcDN );
1426 if ( rc != LDAP_SUCCESS ) {
1427 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1429 /* ndn is not authorized
1430 * to use idassert */
1437 if ( op->o_proxy_authz ) {
1440 * 1) ignore the already set proxyAuthz control
1441 * 2) leave it in place, and don't set ours
1443 * 4) reject the operation
1445 * option (4) is very drastic
1446 * option (3) will make the remote server reject
1447 * the operation, thus being equivalent to (4)
1448 * option (2) will likely break the idassert
1449 * assumptions, so we cannot accept it;
1450 * option (1) means that we are contradicting
1451 * the client's reques.
1453 * I think (4) is the only correct choice.
1455 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1456 rs->sr_text = "proxyAuthz not allowed within namingContext";
1459 if ( op->o_is_auth_check ) {
1460 mode = LDAP_BACK_IDASSERT_NOASSERT;
1463 mode = li->li_idassert_mode;
1467 case LDAP_BACK_IDASSERT_SELF:
1468 if ( BER_BVISNULL( &ndn ) ) {
1474 case LDAP_BACK_IDASSERT_LEGACY:
1475 /* original behavior:
1476 * assert the client's identity */
1477 if ( BER_BVISNULL( &ndn ) ) {
1478 assertedID = slap_empty_bv;
1484 case LDAP_BACK_IDASSERT_ANONYMOUS:
1485 /* assert "anonymous" */
1486 assertedID = slap_empty_bv;
1489 case LDAP_BACK_IDASSERT_NOASSERT:
1490 /* don't assert; bind as proxyauthzdn */
1493 case LDAP_BACK_IDASSERT_OTHERID:
1494 case LDAP_BACK_IDASSERT_OTHERDN:
1495 /* assert idassert DN */
1496 assertedID = li->li_idassert_authzID;
1503 if ( BER_BVISNULL( &assertedID ) ) {
1504 assertedID = slap_empty_bv;
1507 if ( op->o_ctrls ) {
1508 for ( i = 0; op->o_ctrls[ i ]; i++ )
1509 /* just count ctrls */ ;
1512 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
1514 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
1516 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1517 ctrls[ 0 ]->ldctl_iscritical = 1;
1519 switch ( li->li_idassert_mode ) {
1520 /* already in u:ID or dn:DN form */
1521 case LDAP_BACK_IDASSERT_OTHERID:
1522 case LDAP_BACK_IDASSERT_OTHERDN:
1523 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
1526 /* needs the dn: prefix */
1528 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1529 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
1531 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1532 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1533 assertedID.bv_val, assertedID.bv_len + 1 );
1537 if ( op->o_ctrls ) {
1538 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1539 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1542 ctrls[ i + 1 ] = NULL;
1545 if ( ctrls == NULL ) {
1546 ctrls = op->o_ctrls;
1555 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1557 LDAPControl **ctrls = *pctrls;
1559 /* we assume that the first control is the proxyAuthz
1560 * added by back-ldap, so it's the only one we explicitly
1562 if ( ctrls && ctrls != op->o_ctrls ) {
1563 assert( ctrls[ 0 ] != NULL );
1565 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1566 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1569 op->o_tmpfree( ctrls, op->o_tmpmemctx );