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 ) ) {
104 struct ldapconn *tmplc;
107 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
108 tmplc = avl_delete( &li->conntree, (caddr_t)lc,
109 ldap_back_conn_cmp );
110 if ( tmplc != NULL ) {
111 if ( !BER_BVISNULL( &lc->lc_local_ndn ) ) {
112 ch_free( lc->lc_local_ndn.bv_val );
114 ber_dupbv( &lc->lc_local_ndn, &op->o_req_ndn );
115 lerr = avl_insert( &li->conntree, (caddr_t)lc,
116 ldap_back_conn_cmp, ldap_back_conn_dup );
119 /* something BAD happened */
123 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
125 ldap_back_conn_free( lc );
135 * compares two struct ldapconn based on the value of the conn pointer;
139 ldap_back_conn_cmp( const void *c1, const void *c2 )
141 const struct ldapconn *lc1 = (const struct ldapconn *)c1;
142 const struct ldapconn *lc2 = (const struct ldapconn *)c2;
145 /* If local DNs don't match, it is definitely not a match */
146 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
151 /* For shared sessions, conn is NULL. Only explicitly
152 * bound sessions will have non-NULL conn.
154 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
160 * returns -1 in case a duplicate struct ldapconn has been inserted;
164 ldap_back_conn_dup( void *c1, void *c2 )
166 struct ldapconn *lc1 = (struct ldapconn *)c1;
167 struct ldapconn *lc2 = (struct ldapconn *)c2;
169 /* Cannot have more than one shared session with same DN */
170 if ( dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) &&
171 lc1->lc_conn == lc2->lc_conn )
179 #if PRINT_CONNTREE > 0
181 ravl_print( Avlnode *root, int depth )
190 ravl_print( root->avl_right, depth+1 );
192 for ( i = 0; i < depth; i++ ) {
197 printf( "lc(%lx) local(%s) conn(%lx) %d\n",
198 lc, lc->lc_local_ndn.bv_val, lc->lc_conn, root->avl_bf );
200 ravl_print( root->avl_left, depth+1 );
204 myprint( Avlnode *root )
206 printf( "********\n" );
209 printf( "\tNULL\n" );
212 ravl_print( root, 0 );
215 printf( "********\n" );
217 #endif /* PRINT_CONNTREE */
220 ldap_back_freeconn( Operation *op, struct ldapconn *lc )
222 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
225 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
226 lc = avl_delete( &li->conntree, (caddr_t)lc,
227 ldap_back_conn_cmp );
229 /* something BAD happened */
233 ldap_back_conn_free( (void *)lc );
235 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
241 ldap_back_prepare_conn( struct ldapconn **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
243 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
244 int vers = op->o_protocol;
247 assert( lcp != NULL );
249 rs->sr_err = ldap_initialize( &ld, li->url );
250 if ( rs->sr_err != LDAP_SUCCESS ) {
254 /* Set LDAP version. This will always succeed: If the client
255 * bound with a particular version, then so can we.
257 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
259 /* automatically chase referrals ("[dont-]chase-referrals" statement) */
260 if ( LDAP_BACK_CHASE_REFERRALS( li ) ) {
261 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
265 /* start TLS ("tls-[try-]{start,propagate}" statements) */
266 if ( ( LDAP_BACK_USE_TLS( li ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( li ) ) )
267 && !ldap_is_ldaps_url( li->url ) )
269 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
271 * use asynchronous StartTLS
272 * in case, chase referral (not implemented yet)
276 rs->sr_err = ldap_start_tls( ld, NULL, NULL, &msgid );
277 if ( rs->sr_err == LDAP_SUCCESS ) {
278 LDAPMessage *res = NULL;
280 struct timeval tv = { 0, 0 };
283 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
285 rs->sr_err = LDAP_OTHER;
287 } else if ( rc == 0 ) {
294 rs->sr_err = LDAP_OTHER;
296 } else if ( rc == LDAP_RES_EXTENDED ) {
297 struct berval *data = NULL;
299 rs->sr_err = ldap_parse_extended_result( ld, res,
301 if ( rs->sr_err == LDAP_SUCCESS ) {
302 rs->sr_err = ldap_result2error( ld, res, 1 );
305 /* FIXME: in case a referral
306 * is returned, should we try
307 * using it instead of the
309 if ( rs->sr_err == LDAP_SUCCESS ) {
310 ldap_install_tls( ld );
312 } else if ( rs->sr_err == LDAP_REFERRAL ) {
313 rs->sr_err = LDAP_OTHER;
314 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
318 if ( data->bv_val ) {
319 ber_memfree( data->bv_val );
326 rs->sr_err = LDAP_OTHER;
333 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
335 * use synchronous StartTLS
337 rs->sr_err = ldap_start_tls_s( ld, NULL, NULL );
338 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
340 /* if StartTLS is requested, only attempt it if the URL
341 * is not "ldaps://"; this may occur not only in case
342 * of misconfiguration, but also when used in the chain
343 * overlay, where the "uri" can be parsed out of a referral */
344 if ( rs->sr_err == LDAP_SERVER_DOWN
345 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( li ) ) )
347 ldap_unbind_ext_s( ld, NULL, NULL );
351 #endif /* HAVE_TLS */
353 if ( *lcp == NULL ) {
354 *lcp = (struct ldapconn *)ch_malloc( sizeof( struct ldapconn ) );
355 memset( *lcp, 0, sizeof( struct ldapconn ) );
360 if ( rs->sr_err != LDAP_SUCCESS ) {
361 rs->sr_err = slap_map_api2result( rs );
362 if ( sendok & LDAP_BACK_SENDERR ) {
363 if ( rs->sr_text == NULL ) {
364 rs->sr_text = "ldap_initialize() failed";
366 send_ldap_result( op, rs );
375 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
377 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
378 struct ldapconn *lc, lc_curr = { 0 };
380 /* Searches for a ldapconn in the avl tree */
382 /* Explicit binds must not be shared */
383 if ( op->o_tag == LDAP_REQ_BIND
385 && op->o_conn->c_authz_backend
386 && op->o_bd->be_private == op->o_conn->c_authz_backend->be_private ) )
388 lc_curr.lc_conn = op->o_conn;
391 lc_curr.lc_conn = NULL;
394 /* Internal searches are privileged and shared. So is root. */
395 if ( op->o_do_not_cache || be_isroot( op ) ) {
396 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
397 lc_curr.lc_conn = NULL;
398 lc_curr.lc_ispriv = 1;
401 lc_curr.lc_local_ndn = op->o_ndn;
404 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
405 lc = (struct ldapconn *)avl_find( li->conntree,
406 (caddr_t)&lc_curr, ldap_back_conn_cmp );
407 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
409 /* Looks like we didn't get a bind. Open a new session... */
411 /* lc here must be NULL */
412 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
416 lc->lc_conn = lc_curr.lc_conn;
417 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
419 ldap_pvt_thread_mutex_init( &lc->lc_mutex );
421 if ( lc_curr.lc_ispriv ) {
422 ber_dupbv( &lc->lc_cred, &li->acl_passwd );
423 ber_dupbv( &lc->lc_bound_ndn, &li->acl_authcDN );
424 lc->lc_ispriv = lc_curr.lc_ispriv;
427 BER_BVZERO( &lc->lc_cred );
428 BER_BVZERO( &lc->lc_bound_ndn );
429 if ( op->o_conn && !BER_BVISEMPTY( &op->o_ndn )
430 && op->o_bd == op->o_conn->c_authz_backend )
432 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
438 /* Inserts the newly created ldapconn in the avl tree */
439 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
440 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
441 ldap_back_conn_cmp, ldap_back_conn_dup );
443 #if PRINT_CONNTREE > 0
444 myprint( li->conntree );
445 #endif /* PRINT_CONNTREE */
447 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
449 Debug( LDAP_DEBUG_TRACE,
450 "=>ldap_back_getconn: conn %p inserted\n", (void *) lc, 0, 0 );
452 /* Err could be -1 in case a duplicate ldapconn is inserted */
453 if ( rs->sr_err != 0 ) {
454 ldap_back_conn_free( lc );
455 rs->sr_err = LDAP_OTHER;
456 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
457 send_ldap_error( op, rs, LDAP_OTHER,
458 "internal server error" );
463 Debug( LDAP_DEBUG_TRACE,
464 "=>ldap_back_getconn: conn %p fetched\n", (void *) lc, 0, 0 );
473 * Note: as the check for the value of lc->lc_bound was already here, I removed
474 * it from all the callers, and I made the function return the flag, so
475 * it can be used to simplify the check.
478 ldap_back_dobind_int(
482 ldap_back_send_t sendok,
488 assert( retries >= 0 );
490 if ( !lc->lc_bound ) {
491 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
494 * FIXME: we need to let clients use proxyAuthz
495 * otherwise we cannot do symmetric pools of servers;
496 * we have to live with the fact that a user can
497 * authorize itself as any ID that is allowed
498 * by the authzTo directive of the "proxyauthzdn".
501 * NOTE: current Proxy Authorization specification
502 * and implementation do not allow proxy authorization
503 * control to be provided with Bind requests
506 * if no bind took place yet, but the connection is bound
507 * and the "idassert-authcDN" (or other ID) is set,
508 * then bind as the asserting identity and explicitly
509 * add the proxyAuthz control to every operation with the
510 * dn bound to the connection as control value.
511 * This is done also if this is the authrizing backend,
512 * but the "override" flag is given to idassert.
513 * It allows to use SASL bind and yet proxyAuthz users
515 if ( op->o_conn != NULL &&
516 !op->o_do_not_cache &&
518 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
519 ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
521 (void)ldap_back_proxy_authz_bind( lc, op, rs );
525 #ifdef HAVE_CYRUS_SASL
526 if ( lc->lc_ispriv && li->acl_authmethod == LDAP_AUTH_SASL ) {
527 void *defaults = NULL;
529 #if 1 /* will deal with this later... */
530 if ( li->acl_secprops != NULL ) {
531 rc = ldap_set_option( lc->lc_ld,
532 LDAP_OPT_X_SASL_SECPROPS, li->acl_secprops);
534 if( rc != LDAP_OPT_SUCCESS ) {
535 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
536 "(%s,SECPROPS,\"%s\") failed!\n",
537 li->url, li->acl_secprops, 0 );
543 defaults = lutil_sasl_defaults( lc->lc_ld,
544 li->acl_sasl_mech.bv_val,
545 li->acl_sasl_realm.bv_val,
546 li->acl_authcID.bv_val,
547 li->acl_passwd.bv_val,
550 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
551 li->acl_authcDN.bv_val,
552 li->acl_sasl_mech.bv_val, NULL, NULL,
553 LDAP_SASL_QUIET, lutil_sasl_interact,
556 lutil_sasl_freedefs( defaults );
558 rs->sr_err = slap_map_api2result( rs );
559 if ( rs->sr_err != LDAP_SUCCESS ) {
561 send_ldap_result( op, rs );
568 #endif /* HAVE_CYRUS_SASL */
571 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
572 lc->lc_bound_ndn.bv_val,
573 LDAP_SASL_SIMPLE, &lc->lc_cred,
574 NULL, NULL, &msgid );
576 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
578 ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
581 /* lc here must be the regular lc, reset and ready for init */
582 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
590 ldap_back_freeconn( op, lc );
591 rs->sr_err = slap_map_api2result( rs );
596 rc = ldap_back_op_result( lc, op, rs, msgid, sendok );
597 if ( rc == LDAP_SUCCESS ) {
608 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
612 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
613 rc = ldap_back_dobind_int( lc, op, rs, sendok, 1 );
614 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
622 * This is a callback used for chasing referrals using the same
623 * credentials as the original user on this session.
626 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
627 ber_int_t msgid, void *params )
629 struct ldapconn *lc = (struct ldapconn *)params;
631 /* FIXME: add checks on the URL/identity? */
633 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
634 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
643 ldap_back_send_t sendok )
646 LDAPMessage *res = NULL;
649 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
652 rs->sr_matched = NULL;
654 /* if the error recorded in the reply corresponds
655 * to a successful state, get the error from the
656 * remote server response */
657 if ( ERR_OK( rs->sr_err ) ) {
659 struct timeval tv = { 0, 0 };
662 /* if result parsing fails, note the failure reason */
663 switch ( ldap_result( lc->lc_ld, msgid, 1, &tv, &res ) ) {
666 tv.tv_usec = 100000; /* 0.1 s */
667 ldap_pvt_thread_yield();
671 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
676 /* otherwise get the result; if it is not
677 * LDAP_SUCCESS, record it in the reply
678 * structure (this includes
679 * LDAP_COMPARE_{TRUE|FALSE}) */
681 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
682 &match, &text, NULL, NULL, 1 );
684 if ( rc != LDAP_SUCCESS ) {
690 /* if the error in the reply structure is not
691 * LDAP_SUCCESS, try to map it from client
693 if ( !ERR_OK( rs->sr_err ) ) {
694 rs->sr_err = slap_map_api2result( rs );
696 /* internal ops ( op->o_conn == NULL )
697 * must not reply to client */
698 if ( op->o_conn && !op->o_do_not_cache && match ) {
700 /* record the (massaged) matched
701 * DN into the reply structure */
702 rs->sr_matched = match;
706 ( ( sendok & LDAP_BACK_SENDOK )
707 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
709 send_ldap_result( op, rs );
712 if ( rs->sr_matched != match ) {
713 free( (char *)rs->sr_matched );
715 rs->sr_matched = NULL;
716 ldap_memfree( match );
719 ldap_memfree( text );
722 return( ERR_OK( rs->sr_err ) ? 0 : -1 );
725 /* return true if bound, false if failed */
727 ldap_back_retry( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
731 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
732 ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
736 /* lc here must be the regular lc, reset and ready for init */
737 rc = ldap_back_prepare_conn( &lc, op, rs, sendok );
738 if ( rc == LDAP_SUCCESS ) {
739 rc = ldap_back_dobind_int( lc, op, rs, sendok, 0 );
741 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
747 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs )
749 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
750 struct berval binddn = slap_empty_bv;
751 struct berval bindcred = slap_empty_bv;
757 * FIXME: we need to let clients use proxyAuthz
758 * otherwise we cannot do symmetric pools of servers;
759 * we have to live with the fact that a user can
760 * authorize itself as any ID that is allowed
761 * by the authzTo directive of the "proxyauthzdn".
764 * NOTE: current Proxy Authorization specification
765 * and implementation do not allow proxy authorization
766 * control to be provided with Bind requests
769 * if no bind took place yet, but the connection is bound
770 * and the "proxyauthzdn" is set, then bind as
771 * "proxyauthzdn" and explicitly add the proxyAuthz
772 * control to every operation with the dn bound
773 * to the connection as control value.
776 /* bind as proxyauthzdn only if no idassert mode
777 * is requested, or if the client's identity
779 switch ( li->idassert_mode ) {
780 case LDAP_BACK_IDASSERT_LEGACY:
781 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) && !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
782 if ( !BER_BVISNULL( &li->idassert_authcDN ) && !BER_BVISEMPTY( &li->idassert_authcDN ) )
784 binddn = li->idassert_authcDN;
785 bindcred = li->idassert_passwd;
792 if ( li->idassert_authz ) {
793 struct berval authcDN;
795 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
796 authcDN = slap_empty_bv;
798 authcDN = op->o_conn->c_ndn;
800 rs->sr_err = slap_sasl_matches( op, li->idassert_authz,
801 &authcDN, &authcDN );
802 if ( rs->sr_err != LDAP_SUCCESS ) {
803 send_ldap_result( op, rs );
809 binddn = li->idassert_authcDN;
810 bindcred = li->idassert_passwd;
815 if ( dobind && li->idassert_authmethod == LDAP_AUTH_SASL ) {
816 #ifdef HAVE_CYRUS_SASL
817 void *defaults = NULL;
818 struct berval authzID = BER_BVNULL;
821 /* if SASL supports native authz, prepare for it */
822 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
823 ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
825 switch ( li->idassert_mode ) {
826 case LDAP_BACK_IDASSERT_OTHERID:
827 case LDAP_BACK_IDASSERT_OTHERDN:
828 authzID = li->idassert_authzID;
831 case LDAP_BACK_IDASSERT_ANONYMOUS:
832 BER_BVSTR( &authzID, "dn:" );
835 case LDAP_BACK_IDASSERT_SELF:
836 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
837 /* connection is not authc'd, so don't idassert */
838 BER_BVSTR( &authzID, "dn:" );
841 authzID.bv_len = STRLENOF( "dn:" ) + op->o_conn->c_ndn.bv_len;
842 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
843 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
844 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
845 op->o_conn->c_ndn.bv_val, op->o_conn->c_ndn.bv_len + 1 );
854 #if 0 /* will deal with this later... */
855 if ( sasl_secprops != NULL ) {
856 rs->sr_err = ldap_set_option( lc->lc_ld, LDAP_OPT_X_SASL_SECPROPS,
857 (void *) sasl_secprops );
859 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
860 send_ldap_result( op, rs );
867 defaults = lutil_sasl_defaults( lc->lc_ld,
868 li->idassert_sasl_mech.bv_val,
869 li->idassert_sasl_realm.bv_val,
870 li->idassert_authcID.bv_val,
871 li->idassert_passwd.bv_val,
874 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
875 li->idassert_sasl_mech.bv_val, NULL, NULL,
876 LDAP_SASL_QUIET, lutil_sasl_interact,
879 lutil_sasl_freedefs( defaults );
881 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
884 rs->sr_err = slap_map_api2result( rs );
885 if ( rs->sr_err != LDAP_SUCCESS ) {
887 send_ldap_result( op, rs );
893 #endif /* HAVE_CYRUS_SASL */
896 switch ( li->idassert_authmethod ) {
897 case LDAP_AUTH_SIMPLE:
898 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
899 binddn.bv_val, LDAP_SASL_SIMPLE,
900 &bindcred, NULL, NULL, &msgid );
910 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
911 send_ldap_result( op, rs );
915 rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_SENDERR );
916 if ( rc == LDAP_SUCCESS ) {
924 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
925 * to existing server-side controls if required; if not,
926 * the existing server-side controls are placed in *pctrls.
927 * The caller, after using the controls in client API
928 * operations, if ( *pctrls != op->o_ctrls ), should
929 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
930 * The function returns success if the control could
931 * be added if required, or if it did nothing; in the future,
932 * it might return some error if it failed.
934 * if no bind took place yet, but the connection is bound
935 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
936 * and explicitly add proxyAuthz the control to every operation
937 * with the dn bound to the connection as control value.
939 * If no server-side controls are defined for the operation,
940 * simply add the proxyAuthz control; otherwise, if the
941 * proxyAuthz control is not already set, add it as
944 * FIXME: is controls order significant for security?
945 * ANSWER: controls ordering and interoperability
946 * must be indicated by the specs of each control; if none
947 * is specified, the order is irrelevant.
950 ldap_back_proxy_authz_ctrl(
954 LDAPControl ***pctrls )
956 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
957 LDAPControl **ctrls = NULL;
960 struct berval assertedID;
964 rs->sr_err = LDAP_SUCCESS;
966 if ( ( BER_BVISNULL( &li->idassert_authcID ) || BER_BVISEMPTY( &li->idassert_authcID ) )
967 && ( BER_BVISNULL( &li->idassert_authcDN ) || BER_BVISEMPTY( &li->idassert_authcDN ) ) ) {
971 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
975 if ( li->idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
976 if ( op->o_proxy_authz ) {
978 * FIXME: we do not want to perform proxyAuthz
979 * on behalf of the client, because this would
980 * be performed with "proxyauthzdn" privileges.
982 * This might actually be too strict, since
983 * the "proxyauthzdn" authzTo, and each entry's
984 * authzFrom attributes may be crafted
985 * to avoid unwanted proxyAuthz to take place.
988 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
989 rs->sr_text = "proxyAuthz not allowed within namingContext";
994 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
998 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1002 if ( BER_BVISNULL( &li->idassert_authcDN ) ) {
1006 } else if ( li->idassert_authmethod == LDAP_AUTH_SASL ) {
1007 if ( ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1008 /* && ( !BER_BVISNULL( &op->o_conn->c_ndn ) || lc->lc_bound ) */ )
1010 /* already asserted in SASL via native authz */
1011 /* NOTE: the test on lc->lc_bound is used to trap
1012 * native authorization of anonymous users,
1013 * since in that case op->o_conn->c_ndn is NULL */
1017 } else if ( li->idassert_authz ) {
1019 struct berval authcDN;
1021 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1022 authcDN = slap_empty_bv;
1024 authcDN = op->o_conn->c_ndn;
1026 rc = slap_sasl_matches( op, li->idassert_authz,
1027 &authcDN, & authcDN );
1028 if ( rc != LDAP_SUCCESS ) {
1029 /* op->o_conn->c_ndn is not authorized
1030 * to use idassert */
1035 if ( op->o_proxy_authz ) {
1038 * 1) ignore the already set proxyAuthz control
1039 * 2) leave it in place, and don't set ours
1041 * 4) reject the operation
1043 * option (4) is very drastic
1044 * option (3) will make the remote server reject
1045 * the operation, thus being equivalent to (4)
1046 * option (2) will likely break the idassert
1047 * assumptions, so we cannot accept it;
1048 * option (1) means that we are contradicting
1049 * the client's reques.
1051 * I think (4) is the only correct choice.
1053 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1054 rs->sr_text = "proxyAuthz not allowed within namingContext";
1057 if ( op->o_do_not_cache && op->o_is_auth_check ) {
1058 mode = LDAP_BACK_IDASSERT_NOASSERT;
1061 mode = li->idassert_mode;
1065 case LDAP_BACK_IDASSERT_LEGACY:
1066 case LDAP_BACK_IDASSERT_SELF:
1067 /* original behavior:
1068 * assert the client's identity */
1069 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1070 assertedID = slap_empty_bv;
1072 assertedID = op->o_conn->c_ndn;
1076 case LDAP_BACK_IDASSERT_ANONYMOUS:
1077 /* assert "anonymous" */
1078 assertedID = slap_empty_bv;
1081 case LDAP_BACK_IDASSERT_NOASSERT:
1082 /* don't assert; bind as proxyauthzdn */
1085 case LDAP_BACK_IDASSERT_OTHERID:
1086 case LDAP_BACK_IDASSERT_OTHERDN:
1087 /* assert idassert DN */
1088 assertedID = li->idassert_authzID;
1095 if ( BER_BVISNULL( &assertedID ) ) {
1096 assertedID = slap_empty_bv;
1099 if ( op->o_ctrls ) {
1100 for ( i = 0; op->o_ctrls[ i ]; i++ )
1101 /* just count ctrls */ ;
1104 ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
1105 ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
1107 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1108 ctrls[ 0 ]->ldctl_iscritical = 1;
1110 switch ( li->idassert_mode ) {
1111 /* already in u:ID or dn:DN form */
1112 case LDAP_BACK_IDASSERT_OTHERID:
1113 case LDAP_BACK_IDASSERT_OTHERDN:
1114 ber_dupbv( &ctrls[ 0 ]->ldctl_value, &assertedID );
1117 /* needs the dn: prefix */
1119 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1120 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
1121 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1122 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val + STRLENOF( "dn:" ),
1123 assertedID.bv_val, assertedID.bv_len + 1 );
1127 if ( op->o_ctrls ) {
1128 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1129 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1132 ctrls[ i + 1 ] = NULL;
1135 if ( ctrls == NULL ) {
1136 ctrls = op->o_ctrls;
1145 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1147 LDAPControl **ctrls = *pctrls;
1149 /* we assume that the first control is the proxyAuthz
1150 * added by back-ldap, so it's the only one we explicitly
1152 if ( ctrls && ctrls != op->o_ctrls ) {
1153 assert( ctrls[ 0 ] );
1155 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1156 free( ctrls[ 0 ]->ldctl_value.bv_val );