1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2005 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
28 #include <ac/socket.h>
29 #include <ac/string.h>
33 #include "back-ldap.h"
35 #include <lutil_ldap.h>
37 #define PRINT_CONNTREE 0
39 static LDAP_REBIND_PROC ldap_back_rebind;
42 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs );
45 ldap_back_prepare_conn( struct ldapconn **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
48 ldap_back_bind( Operation *op, SlapReply *rs )
50 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
56 lc = ldap_back_getconn( op, rs, LDAP_BACK_SENDERR );
61 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
62 ch_free( lc->lc_bound_ndn.bv_val );
63 BER_BVZERO( &lc->lc_bound_ndn );
67 /* method is always LDAP_AUTH_SIMPLE if we got here */
68 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
70 &op->orb_cred, op->o_ctrls, NULL, &msgid );
71 rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_SENDERR );
73 if ( rc == LDAP_SUCCESS ) {
74 /* If defined, proxyAuthz will be used also when
75 * back-ldap is the authorizing backend; for this
76 * purpose, a successful bind is followed by a
77 * bind with the configured identity assertion */
78 /* NOTE: use with care */
79 if ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
80 ldap_back_proxy_authz_bind( lc, op, rs );
81 if ( lc->lc_bound == 0 ) {
88 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
90 if ( LDAP_BACK_SAVECRED( li ) ) {
91 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
92 memset( lc->lc_cred.bv_val, 0,
94 ch_free( lc->lc_cred.bv_val );
96 ber_dupbv( &lc->lc_cred, &op->orb_cred );
97 ldap_set_rebind_proc( lc->lc_ld, ldap_back_rebind, lc );
102 /* must re-insert if local DN changed as result of bind */
103 if ( lc->lc_bound && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) ) {
106 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
107 lc = avl_delete( &li->conntree, (caddr_t)lc,
108 ldap_back_conn_cmp );
109 if ( !BER_BVISNULL( &lc->lc_local_ndn ) ) {
110 ch_free( lc->lc_local_ndn.bv_val );
112 ber_dupbv( &lc->lc_local_ndn, &op->o_req_ndn );
113 lerr = avl_insert( &li->conntree, (caddr_t)lc,
114 ldap_back_conn_cmp, ldap_back_conn_dup );
115 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
117 ldap_back_conn_free( lc );
127 * compares two struct ldapconn based on the value of the conn pointer;
131 ldap_back_conn_cmp( const void *c1, const void *c2 )
133 const struct ldapconn *lc1 = (const struct ldapconn *)c1;
134 const struct ldapconn *lc2 = (const struct ldapconn *)c2;
137 /* If local DNs don't match, it is definitely not a match */
138 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
143 /* For shared sessions, conn is NULL. Only explicitly
144 * bound sessions will have non-NULL conn.
146 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
152 * returns -1 in case a duplicate struct ldapconn has been inserted;
156 ldap_back_conn_dup( void *c1, void *c2 )
158 struct ldapconn *lc1 = (struct ldapconn *)c1;
159 struct ldapconn *lc2 = (struct ldapconn *)c2;
161 /* Cannot have more than one shared session with same DN */
162 if ( dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) &&
163 lc1->lc_conn == lc2->lc_conn )
171 #if PRINT_CONNTREE > 0
173 ravl_print( Avlnode *root, int depth )
182 ravl_print( root->avl_right, depth+1 );
184 for ( i = 0; i < depth; i++ ) {
189 printf( "lc(%lx) local(%s) conn(%lx) %d\n",
190 lc, lc->lc_local_ndn.bv_val, lc->lc_conn, root->avl_bf );
192 ravl_print( root->avl_left, depth+1 );
196 myprint( Avlnode *root )
198 printf( "********\n" );
201 printf( "\tNULL\n" );
204 ravl_print( root, 0 );
207 printf( "********\n" );
209 #endif /* PRINT_CONNTREE */
212 ldap_back_freeconn( Operation *op, struct ldapconn *lc )
214 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
216 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
217 lc = avl_delete( &li->conntree, (caddr_t)lc,
218 ldap_back_conn_cmp );
219 ldap_back_conn_free( (void *)lc );
220 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
226 ldap_back_prepare_conn( struct ldapconn **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
228 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
229 int vers = op->o_protocol;
232 assert( lcp != NULL );
234 rs->sr_err = ldap_initialize( &ld, li->url );
235 if ( rs->sr_err != LDAP_SUCCESS ) {
239 /* Set LDAP version. This will always succeed: If the client
240 * bound with a particular version, then so can we.
242 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
244 /* automatically chase referrals ("[dont-]chase-referrals" statement) */
245 if ( LDAP_BACK_CHASE_REFERRALS( li ) ) {
246 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
250 /* start TLS ("tls-[try-]{start,propagate}" statements) */
251 if ( ( LDAP_BACK_USE_TLS( li ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( li ) ) )
252 && !ldap_is_ldaps_url( li->url ) )
256 * use asynchronous StartTLS
257 * in case, chase referral (not implemented yet)
261 rs->sr_err = ldap_start_tls( ld, NULL, NULL, &msgid );
262 if ( rs->sr_err == LDAP_SUCCESS ) {
263 LDAPMessage *res = NULL;
265 struct timeval tv = { 0, 0 };
268 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
270 rs->sr_err = LDAP_OTHER;
272 } else if ( rc == 0 ) {
279 rs->sr_err = LDAP_OTHER;
281 } else if ( rc == LDAP_RES_EXTENDED ) {
282 struct berval *data = NULL;
284 rs->sr_err = ldap_parse_extended_result( ld, res,
286 if ( rs->sr_err == LDAP_SUCCESS ) {
287 rs->sr_err = ldap_result2error( ld, res, 1 );
290 /* FIXME: in case a referral
291 * is returned, should we try
292 * using it instead of the
294 if ( rs->sr_err == LDAP_SUCCESS ) {
295 ldap_install_tls( ld );
297 } else if ( rs->sr_err == LDAP_REFERRAL ) {
298 rs->sr_err = LDAP_OTHER;
299 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
303 if ( data->bv_val ) {
304 ber_memfree( data->bv_val );
311 rs->sr_err = LDAP_OTHER;
320 * use synchronous StartTLS
322 rs->sr_err = ldap_start_tls_s( ld, NULL, NULL );
325 /* if StartTLS is requested, only attempt it if the URL
326 * is not "ldaps://"; this may occur not only in case
327 * of misconfiguration, but also when used in the chain
328 * overlay, where the "uri" can be parsed out of a referral */
329 if ( rs->sr_err == LDAP_SERVER_DOWN
330 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( li ) ) )
332 ldap_unbind_ext_s( ld, NULL, NULL );
336 #endif /* HAVE_TLS */
338 if ( *lcp == NULL ) {
339 *lcp = (struct ldapconn *)ch_malloc( sizeof( struct ldapconn ) );
340 memset( *lcp, 0, sizeof( struct ldapconn ) );
345 if ( rs->sr_err != LDAP_SUCCESS ) {
346 rs->sr_err = slap_map_api2result( rs );
347 if ( sendok & LDAP_BACK_SENDERR ) {
348 if ( rs->sr_text == NULL ) {
349 rs->sr_text = "ldap_initialize() failed";
351 send_ldap_result( op, rs );
360 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
362 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
363 struct ldapconn *lc, lc_curr = { 0 };
365 /* Searches for a ldapconn in the avl tree */
367 /* Explicit binds must not be shared */
368 if ( op->o_tag == LDAP_REQ_BIND
370 && op->o_conn->c_authz_backend
371 && op->o_bd->be_private == op->o_conn->c_authz_backend->be_private ) )
373 lc_curr.lc_conn = op->o_conn;
376 lc_curr.lc_conn = NULL;
379 /* Internal searches are privileged and shared. So is root. */
380 if ( op->o_do_not_cache || be_isroot( op ) ) {
381 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
382 lc_curr.lc_conn = NULL;
383 lc_curr.lc_ispriv = 1;
386 lc_curr.lc_local_ndn = op->o_ndn;
389 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
390 lc = (struct ldapconn *)avl_find( li->conntree,
391 (caddr_t)&lc_curr, ldap_back_conn_cmp );
392 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
394 /* Looks like we didn't get a bind. Open a new session... */
396 /* lc here must be NULL */
397 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
401 lc->lc_conn = lc_curr.lc_conn;
402 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
404 ldap_pvt_thread_mutex_init( &lc->lc_mutex );
406 if ( lc_curr.lc_ispriv ) {
407 ber_dupbv( &lc->lc_cred, &li->acl_passwd );
408 ber_dupbv( &lc->lc_bound_ndn, &li->acl_authcDN );
409 lc->lc_ispriv = lc_curr.lc_ispriv;
412 BER_BVZERO( &lc->lc_cred );
413 BER_BVZERO( &lc->lc_bound_ndn );
414 if ( op->o_conn && !BER_BVISEMPTY( &op->o_ndn )
415 && op->o_bd == op->o_conn->c_authz_backend )
417 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
423 /* Inserts the newly created ldapconn in the avl tree */
424 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
425 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
426 ldap_back_conn_cmp, ldap_back_conn_dup );
428 #if PRINT_CONNTREE > 0
429 myprint( li->conntree );
430 #endif /* PRINT_CONNTREE */
432 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
434 Debug( LDAP_DEBUG_TRACE,
435 "=>ldap_back_getconn: conn %p inserted\n", (void *) lc, 0, 0 );
437 /* Err could be -1 in case a duplicate ldapconn is inserted */
438 if ( rs->sr_err != 0 ) {
439 ldap_back_conn_free( lc );
440 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
441 send_ldap_error( op, rs, LDAP_OTHER,
442 "internal server error" );
447 Debug( LDAP_DEBUG_TRACE,
448 "=>ldap_back_getconn: conn %p fetched\n", (void *) lc, 0, 0 );
457 * Note: as the check for the value of lc->lc_bound was already here, I removed
458 * it from all the callers, and I made the function return the flag, so
459 * it can be used to simplify the check.
462 ldap_back_dobind_int(
466 ldap_back_send_t sendok,
472 assert( retries >= 0 );
474 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
475 if ( !lc->lc_bound ) {
476 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
479 * FIXME: we need to let clients use proxyAuthz
480 * otherwise we cannot do symmetric pools of servers;
481 * we have to live with the fact that a user can
482 * authorize itself as any ID that is allowed
483 * by the authzTo directive of the "proxyauthzdn".
486 * NOTE: current Proxy Authorization specification
487 * and implementation do not allow proxy authorization
488 * control to be provided with Bind requests
491 * if no bind took place yet, but the connection is bound
492 * and the "idassert-authcDN" (or other ID) is set,
493 * then bind as the asserting identity and explicitly
494 * add the proxyAuthz control to every operation with the
495 * dn bound to the connection as control value.
496 * This is done also if this is the authrizing backend,
497 * but the "override" flag is given to idassert.
498 * It allows to use SASL bind and yet proxyAuthz users
500 if ( op->o_conn != NULL &&
501 !op->o_do_not_cache &&
503 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
504 ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
506 (void)ldap_back_proxy_authz_bind( lc, op, rs );
510 #ifdef HAVE_CYRUS_SASL
511 if ( lc->lc_ispriv && li->acl_authmethod == LDAP_AUTH_SASL ) {
512 void *defaults = NULL;
514 #if 0 /* will deal with this later... */
515 if ( sasl_secprops != NULL ) {
516 rs->sr_err = ldap_set_option( lc->lc_ld, LDAP_OPT_X_SASL_SECPROPS,
517 (void *) sasl_secprops );
519 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
520 send_ldap_result( op, rs );
527 defaults = lutil_sasl_defaults( lc->lc_ld,
528 li->acl_sasl_mech.bv_val,
529 li->acl_sasl_realm.bv_val,
530 li->acl_authcID.bv_val,
531 li->acl_passwd.bv_val,
534 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
535 li->acl_authcDN.bv_val,
536 li->acl_sasl_mech.bv_val, NULL, NULL,
537 LDAP_SASL_QUIET, lutil_sasl_interact,
540 lutil_sasl_freedefs( defaults );
542 rs->sr_err = slap_map_api2result( rs );
543 if ( rs->sr_err != LDAP_SUCCESS ) {
545 send_ldap_result( op, rs );
552 #endif /* HAVE_CYRUS_SASL */
555 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
556 lc->lc_bound_ndn.bv_val,
557 LDAP_SASL_SIMPLE, &lc->lc_cred,
558 NULL, NULL, &msgid );
560 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
562 ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
565 /* lc here must be the regular lc, reset and ready for init */
566 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
574 ldap_back_freeconn( op, lc );
575 rs->sr_err = slap_map_api2result( rs );
580 rc = ldap_back_op_result( lc, op, rs, msgid, sendok );
581 if ( rc == LDAP_SUCCESS ) {
588 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
593 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
595 return ldap_back_dobind_int( lc, op, rs, sendok, 1 );
601 * This is a callback used for chasing referrals using the same
602 * credentials as the original user on this session.
605 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
606 ber_int_t msgid, void *params )
608 struct ldapconn *lc = (struct ldapconn *)params;
610 /* FIXME: add checks on the URL/identity? */
612 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
613 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
622 ldap_back_send_t sendok )
625 LDAPMessage *res = NULL;
628 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
631 rs->sr_matched = NULL;
633 /* if the error recorded in the reply corresponds
634 * to a successful state, get the error from the
635 * remote server response */
636 if ( ERR_OK( rs->sr_err ) ) {
638 struct timeval tv = { 0, 0 };
641 /* if result parsing fails, note the failure reason */
642 switch ( ldap_result( lc->lc_ld, msgid, 1, &tv, &res ) ) {
645 tv.tv_usec = 100000; /* 0.1 s */
646 ldap_pvt_thread_yield();
650 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
655 /* otherwise get the result; if it is not
656 * LDAP_SUCCESS, record it in the reply
657 * structure (this includes
658 * LDAP_COMPARE_{TRUE|FALSE}) */
660 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
661 &match, &text, NULL, NULL, 1 );
663 if ( rc != LDAP_SUCCESS ) {
669 /* if the error in the reply structure is not
670 * LDAP_SUCCESS, try to map it from client
672 if ( !ERR_OK( rs->sr_err ) ) {
673 rs->sr_err = slap_map_api2result( rs );
675 /* internal ops ( op->o_conn == NULL )
676 * must not reply to client */
677 if ( op->o_conn && !op->o_do_not_cache && match ) {
679 /* record the (massaged) matched
680 * DN into the reply structure */
681 rs->sr_matched = match;
685 ( ( sendok & LDAP_BACK_SENDOK )
686 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
688 send_ldap_result( op, rs );
691 if ( rs->sr_matched != match ) {
692 free( (char *)rs->sr_matched );
694 rs->sr_matched = NULL;
695 ldap_memfree( match );
698 ldap_memfree( text );
701 return( ERR_OK( rs->sr_err ) ? 0 : -1 );
704 /* return true if bound, false if failed */
706 ldap_back_retry( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
708 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
709 ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
713 /* lc here must be the regular lc, reset and ready for init */
714 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
718 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
719 return ldap_back_dobind_int( lc, op, rs, sendok, 0 );
723 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs )
725 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
726 struct berval binddn = slap_empty_bv;
727 struct berval bindcred = slap_empty_bv;
733 * FIXME: we need to let clients use proxyAuthz
734 * otherwise we cannot do symmetric pools of servers;
735 * we have to live with the fact that a user can
736 * authorize itself as any ID that is allowed
737 * by the authzTo directive of the "proxyauthzdn".
740 * NOTE: current Proxy Authorization specification
741 * and implementation do not allow proxy authorization
742 * control to be provided with Bind requests
745 * if no bind took place yet, but the connection is bound
746 * and the "proxyauthzdn" is set, then bind as
747 * "proxyauthzdn" and explicitly add the proxyAuthz
748 * control to every operation with the dn bound
749 * to the connection as control value.
752 /* bind as proxyauthzdn only if no idassert mode
753 * is requested, or if the client's identity
755 switch ( li->idassert_mode ) {
756 case LDAP_BACK_IDASSERT_LEGACY:
757 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) && !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
758 if ( !BER_BVISNULL( &li->idassert_authcDN ) && !BER_BVISEMPTY( &li->idassert_authcDN ) )
760 binddn = li->idassert_authcDN;
761 bindcred = li->idassert_passwd;
768 if ( li->idassert_authz ) {
769 struct berval authcDN;
771 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
772 authcDN = slap_empty_bv;
774 authcDN = op->o_conn->c_ndn;
776 rs->sr_err = slap_sasl_matches( op, li->idassert_authz,
777 &authcDN, &authcDN );
778 if ( rs->sr_err != LDAP_SUCCESS ) {
779 send_ldap_result( op, rs );
785 binddn = li->idassert_authcDN;
786 bindcred = li->idassert_passwd;
791 if ( dobind && li->idassert_authmethod == LDAP_AUTH_SASL ) {
792 #ifdef HAVE_CYRUS_SASL
793 void *defaults = NULL;
794 struct berval authzID = BER_BVNULL;
797 /* if SASL supports native authz, prepare for it */
798 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
799 ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
801 switch ( li->idassert_mode ) {
802 case LDAP_BACK_IDASSERT_OTHERID:
803 case LDAP_BACK_IDASSERT_OTHERDN:
804 authzID = li->idassert_authzID;
807 case LDAP_BACK_IDASSERT_ANONYMOUS:
808 BER_BVSTR( &authzID, "dn:" );
811 case LDAP_BACK_IDASSERT_SELF:
812 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
813 /* connection is not authc'd, so don't idassert */
814 BER_BVSTR( &authzID, "dn:" );
817 authzID.bv_len = STRLENOF( "dn:" ) + op->o_conn->c_ndn.bv_len;
818 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
819 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
820 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
821 op->o_conn->c_ndn.bv_val, op->o_conn->c_ndn.bv_len + 1 );
830 #if 0 /* will deal with this later... */
831 if ( sasl_secprops != NULL ) {
832 rs->sr_err = ldap_set_option( lc->lc_ld, LDAP_OPT_X_SASL_SECPROPS,
833 (void *) sasl_secprops );
835 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
836 send_ldap_result( op, rs );
843 defaults = lutil_sasl_defaults( lc->lc_ld,
844 li->idassert_sasl_mech.bv_val,
845 li->idassert_sasl_realm.bv_val,
846 li->idassert_authcID.bv_val,
847 li->idassert_passwd.bv_val,
850 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
851 li->idassert_sasl_mech.bv_val, NULL, NULL,
852 LDAP_SASL_QUIET, lutil_sasl_interact,
855 lutil_sasl_freedefs( defaults );
857 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
860 rs->sr_err = slap_map_api2result( rs );
861 if ( rs->sr_err != LDAP_SUCCESS ) {
863 send_ldap_result( op, rs );
869 #endif /* HAVE_CYRUS_SASL */
872 switch ( li->idassert_authmethod ) {
873 case LDAP_AUTH_SIMPLE:
874 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
875 binddn.bv_val, LDAP_SASL_SIMPLE,
876 &bindcred, NULL, NULL, &msgid );
886 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
887 send_ldap_result( op, rs );
891 rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_SENDERR );
892 if ( rc == LDAP_SUCCESS ) {
900 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
901 * to existing server-side controls if required; if not,
902 * the existing server-side controls are placed in *pctrls.
903 * The caller, after using the controls in client API
904 * operations, if ( *pctrls != op->o_ctrls ), should
905 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
906 * The function returns success if the control could
907 * be added if required, or if it did nothing; in the future,
908 * it might return some error if it failed.
910 * if no bind took place yet, but the connection is bound
911 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
912 * and explicitly add proxyAuthz the control to every operation
913 * with the dn bound to the connection as control value.
915 * If no server-side controls are defined for the operation,
916 * simply add the proxyAuthz control; otherwise, if the
917 * proxyAuthz control is not already set, add it as
920 * FIXME: is controls order significant for security?
921 * ANSWER: controls ordering and interoperability
922 * must be indicated by the specs of each control; if none
923 * is specified, the order is irrelevant.
926 ldap_back_proxy_authz_ctrl(
930 LDAPControl ***pctrls )
932 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
933 LDAPControl **ctrls = NULL;
936 struct berval assertedID;
940 rs->sr_err = LDAP_SUCCESS;
942 if ( ( BER_BVISNULL( &li->idassert_authcID ) || BER_BVISEMPTY( &li->idassert_authcID ) )
943 && ( BER_BVISNULL( &li->idassert_authcDN ) || BER_BVISEMPTY( &li->idassert_authcDN ) ) ) {
947 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
951 if ( li->idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
952 if ( op->o_proxy_authz ) {
954 * FIXME: we do not want to perform proxyAuthz
955 * on behalf of the client, because this would
956 * be performed with "proxyauthzdn" privileges.
958 * This might actually be too strict, since
959 * the "proxyauthzdn" authzTo, and each entry's
960 * authzFrom attributes may be crafted
961 * to avoid unwanted proxyAuthz to take place.
964 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
965 rs->sr_text = "proxyAuthz not allowed within namingContext";
970 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
974 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
978 if ( BER_BVISNULL( &li->idassert_authcDN ) ) {
982 } else if ( li->idassert_authmethod == LDAP_AUTH_SASL ) {
983 if ( ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
984 /* && ( !BER_BVISNULL( &op->o_conn->c_ndn ) || lc->lc_bound ) */ )
986 /* already asserted in SASL via native authz */
987 /* NOTE: the test on lc->lc_bound is used to trap
988 * native authorization of anonymous users,
989 * since in that case op->o_conn->c_ndn is NULL */
993 } else if ( li->idassert_authz ) {
995 struct berval authcDN;
997 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
998 authcDN = slap_empty_bv;
1000 authcDN = op->o_conn->c_ndn;
1002 rc = slap_sasl_matches( op, li->idassert_authz,
1003 &authcDN, & authcDN );
1004 if ( rc != LDAP_SUCCESS ) {
1005 /* op->o_conn->c_ndn is not authorized
1006 * to use idassert */
1011 if ( op->o_proxy_authz ) {
1014 * 1) ignore the already set proxyAuthz control
1015 * 2) leave it in place, and don't set ours
1017 * 4) reject the operation
1019 * option (4) is very drastic
1020 * option (3) will make the remote server reject
1021 * the operation, thus being equivalent to (4)
1022 * option (2) will likely break the idassert
1023 * assumptions, so we cannot accept it;
1024 * option (1) means that we are contradicting
1025 * the client's reques.
1027 * I think (4) is the only correct choice.
1029 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1030 rs->sr_text = "proxyAuthz not allowed within namingContext";
1033 if ( op->o_do_not_cache && op->o_is_auth_check ) {
1034 mode = LDAP_BACK_IDASSERT_NOASSERT;
1037 mode = li->idassert_mode;
1041 case LDAP_BACK_IDASSERT_LEGACY:
1042 case LDAP_BACK_IDASSERT_SELF:
1043 /* original behavior:
1044 * assert the client's identity */
1045 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1046 assertedID = slap_empty_bv;
1048 assertedID = op->o_conn->c_ndn;
1052 case LDAP_BACK_IDASSERT_ANONYMOUS:
1053 /* assert "anonymous" */
1054 assertedID = slap_empty_bv;
1057 case LDAP_BACK_IDASSERT_NOASSERT:
1058 /* don't assert; bind as proxyauthzdn */
1061 case LDAP_BACK_IDASSERT_OTHERID:
1062 case LDAP_BACK_IDASSERT_OTHERDN:
1063 /* assert idassert DN */
1064 assertedID = li->idassert_authzID;
1071 if ( BER_BVISNULL( &assertedID ) ) {
1072 assertedID = slap_empty_bv;
1075 if ( op->o_ctrls ) {
1076 for ( i = 0; op->o_ctrls[ i ]; i++ )
1077 /* just count ctrls */ ;
1080 ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
1081 ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
1083 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1084 ctrls[ 0 ]->ldctl_iscritical = 1;
1086 switch ( li->idassert_mode ) {
1087 /* already in u:ID or dn:DN form */
1088 case LDAP_BACK_IDASSERT_OTHERID:
1089 case LDAP_BACK_IDASSERT_OTHERDN:
1090 ber_dupbv( &ctrls[ 0 ]->ldctl_value, &assertedID );
1093 /* needs the dn: prefix */
1095 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1096 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
1097 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1098 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val + STRLENOF( "dn:" ),
1099 assertedID.bv_val, assertedID.bv_len + 1 );
1103 if ( op->o_ctrls ) {
1104 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1105 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1108 ctrls[ i + 1 ] = NULL;
1111 if ( ctrls == NULL ) {
1112 ctrls = op->o_ctrls;
1121 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1123 LDAPControl **ctrls = *pctrls;
1125 /* we assume that the first control is the proxyAuthz
1126 * added by back-ldap, so it's the only one we explicitly
1128 if ( ctrls && ctrls != op->o_ctrls ) {
1129 assert( ctrls[ 0 ] );
1131 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1132 free( ctrls[ 0 ]->ldctl_value.bv_val );