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
29 #include <ac/socket.h>
30 #include <ac/string.h>
34 #include "back-ldap.h"
36 #include <lutil_ldap.h>
38 #define PRINT_CONNTREE 0
40 static LDAP_REBIND_PROC ldap_back_rebind;
43 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs );
46 ldap_back_prepare_conn( struct ldapconn **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
49 ldap_back_bind( Operation *op, SlapReply *rs )
51 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
57 lc = ldap_back_getconn( op, rs, LDAP_BACK_SENDERR );
62 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
63 ch_free( lc->lc_bound_ndn.bv_val );
64 BER_BVZERO( &lc->lc_bound_ndn );
68 /* method is always LDAP_AUTH_SIMPLE if we got here */
69 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
71 &op->orb_cred, op->o_ctrls, NULL, &msgid );
72 rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_SENDERR );
74 if ( rc == LDAP_SUCCESS ) {
75 /* If defined, proxyAuthz will be used also when
76 * back-ldap is the authorizing backend; for this
77 * purpose, a successful bind is followed by a
78 * bind with the configured identity assertion */
79 /* NOTE: use with care */
80 if ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
81 ldap_back_proxy_authz_bind( lc, op, rs );
82 if ( lc->lc_bound == 0 ) {
89 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
91 if ( LDAP_BACK_SAVECRED( li ) ) {
92 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
93 memset( lc->lc_cred.bv_val, 0,
96 ber_bvreplace( &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 /* wait for all other ops to release the connection */
108 switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
109 case LDAP_PVT_THREAD_EBUSY:
111 ldap_pvt_thread_yield();
115 if ( lc->lc_refcnt > 1 ) {
116 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
117 ldap_pvt_thread_yield();
123 assert( lc->lc_refcnt == 1 );
124 lc = avl_delete( &li->conntree, (caddr_t)lc,
125 ldap_back_conn_cmp );
126 assert( lc != NULL );
128 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
129 lerr = avl_insert( &li->conntree, (caddr_t)lc,
130 ldap_back_conn_cmp, ldap_back_conn_dup );
131 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
133 /* we can do this because lc_refcnt == 1 */
134 ldap_back_conn_free( lc );
140 ldap_back_release_conn( op, rs, lc );
149 * compares two struct ldapconn based on the value of the conn pointer;
153 ldap_back_conn_cmp( const void *c1, const void *c2 )
155 const struct ldapconn *lc1 = (const struct ldapconn *)c1;
156 const struct ldapconn *lc2 = (const struct ldapconn *)c2;
159 /* If local DNs don't match, it is definitely not a match */
160 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
165 /* For shared sessions, conn is NULL. Only explicitly
166 * bound sessions will have non-NULL conn.
168 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
174 * returns -1 in case a duplicate struct ldapconn has been inserted;
178 ldap_back_conn_dup( void *c1, void *c2 )
180 struct ldapconn *lc1 = (struct ldapconn *)c1;
181 struct ldapconn *lc2 = (struct ldapconn *)c2;
183 /* Cannot have more than one shared session with same DN */
184 if ( dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) &&
185 lc1->lc_conn == lc2->lc_conn )
193 #if PRINT_CONNTREE > 0
195 ravl_print( Avlnode *root, int depth )
204 ravl_print( root->avl_right, depth+1 );
206 for ( i = 0; i < depth; i++ ) {
211 printf( "lc(%lx) local(%s) conn(%lx) %d\n",
212 lc, lc->lc_local_ndn.bv_val, lc->lc_conn, root->avl_bf );
214 ravl_print( root->avl_left, depth+1 );
218 myprint( Avlnode *root )
220 printf( "********\n" );
223 printf( "\tNULL\n" );
226 ravl_print( root, 0 );
229 printf( "********\n" );
231 #endif /* PRINT_CONNTREE */
234 ldap_back_freeconn( Operation *op, struct ldapconn *lc )
236 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
239 switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
240 case LDAP_PVT_THREAD_EBUSY:
242 ldap_pvt_thread_yield();
249 assert( lc->lc_refcnt > 0 );
250 if ( --lc->lc_refcnt == 0 ) {
251 lc = avl_delete( &li->conntree, (caddr_t)lc,
252 ldap_back_conn_cmp );
253 assert( lc != NULL );
255 ldap_back_conn_free( (void *)lc );
258 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
264 ldap_back_prepare_conn( struct ldapconn **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
266 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
267 int vers = op->o_protocol;
270 assert( lcp != NULL );
272 rs->sr_err = ldap_initialize( &ld, li->url );
273 if ( rs->sr_err != LDAP_SUCCESS ) {
277 /* Set LDAP version. This will always succeed: If the client
278 * bound with a particular version, then so can we.
280 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
282 /* automatically chase referrals ("[dont-]chase-referrals" statement) */
283 if ( LDAP_BACK_CHASE_REFERRALS( li ) ) {
284 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
288 /* start TLS ("tls-[try-]{start,propagate}" statements) */
289 if ( ( LDAP_BACK_USE_TLS( li ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( li ) ) )
290 && !ldap_is_ldaps_url( li->url ) )
292 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
294 * use asynchronous StartTLS
295 * in case, chase referral (not implemented yet)
299 rs->sr_err = ldap_start_tls( ld, NULL, NULL, &msgid );
300 if ( rs->sr_err == LDAP_SUCCESS ) {
301 LDAPMessage *res = NULL;
303 struct timeval tv = { 0, 0 };
306 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
308 rs->sr_err = LDAP_OTHER;
310 } else if ( rc == 0 ) {
317 rs->sr_err = LDAP_OTHER;
319 } else if ( rc == LDAP_RES_EXTENDED ) {
320 struct berval *data = NULL;
322 rs->sr_err = ldap_parse_extended_result( ld, res,
324 if ( rs->sr_err == LDAP_SUCCESS ) {
325 rs->sr_err = ldap_result2error( ld, res, 1 );
328 /* FIXME: in case a referral
329 * is returned, should we try
330 * using it instead of the
332 if ( rs->sr_err == LDAP_SUCCESS ) {
333 ldap_install_tls( ld );
335 } else if ( rs->sr_err == LDAP_REFERRAL ) {
336 rs->sr_err = LDAP_OTHER;
337 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
341 if ( data->bv_val ) {
342 ber_memfree( data->bv_val );
349 rs->sr_err = LDAP_OTHER;
356 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
358 * use synchronous StartTLS
360 rs->sr_err = ldap_start_tls_s( ld, NULL, NULL );
361 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
363 /* if StartTLS is requested, only attempt it if the URL
364 * is not "ldaps://"; this may occur not only in case
365 * of misconfiguration, but also when used in the chain
366 * overlay, where the "uri" can be parsed out of a referral */
367 if ( rs->sr_err == LDAP_SERVER_DOWN
368 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( li ) ) )
370 ldap_unbind_ext( ld, NULL, NULL );
374 /* in case Start TLS is not critical */
375 rs->sr_err = LDAP_SUCCESS;
377 #endif /* HAVE_TLS */
379 if ( *lcp == NULL ) {
380 *lcp = (struct ldapconn *)ch_calloc( 1, sizeof( struct ldapconn ) );
383 (*lcp)->lc_refcnt = 1;
386 if ( rs->sr_err != LDAP_SUCCESS ) {
387 rs->sr_err = slap_map_api2result( rs );
388 if ( sendok & LDAP_BACK_SENDERR ) {
389 if ( rs->sr_text == NULL ) {
390 rs->sr_text = "ldap_initialize() failed";
392 send_ldap_result( op, rs );
401 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
403 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
408 /* Searches for a ldapconn in the avl tree */
410 /* Explicit binds must not be shared */
411 if ( op->o_tag == LDAP_REQ_BIND
413 && op->o_conn->c_authz_backend
414 && op->o_bd->be_private == op->o_conn->c_authz_backend->be_private ) )
416 lc_curr.lc_conn = op->o_conn;
419 lc_curr.lc_conn = NULL;
422 /* Internal searches are privileged and shared. So is root. */
423 /* FIXME: there seem to be concurrency issues */
424 if ( op->o_do_not_cache || be_isroot( op ) ) {
425 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
426 lc_curr.lc_conn = NULL;
427 lc_curr.lc_ispriv = 1;
430 lc_curr.lc_local_ndn = op->o_ndn;
434 switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
435 case LDAP_PVT_THREAD_EBUSY:
437 ldap_pvt_thread_yield();
444 lc = (struct ldapconn *)avl_find( li->conntree,
445 (caddr_t)&lc_curr, ldap_back_conn_cmp );
447 refcnt = ++lc->lc_refcnt;
449 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
451 /* Looks like we didn't get a bind. Open a new session... */
453 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
457 lc->lc_conn = lc_curr.lc_conn;
458 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
460 if ( lc_curr.lc_ispriv ) {
461 ber_dupbv( &lc->lc_cred, &li->acl_passwd );
462 ber_dupbv( &lc->lc_bound_ndn, &li->acl_authcDN );
463 lc->lc_ispriv = lc_curr.lc_ispriv;
466 BER_BVZERO( &lc->lc_cred );
467 BER_BVZERO( &lc->lc_bound_ndn );
468 if ( op->o_conn && !BER_BVISEMPTY( &op->o_ndn )
469 && op->o_bd->be_private == op->o_conn->c_authz_backend->be_private )
471 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
477 /* Inserts the newly created ldapconn in the avl tree */
479 switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
480 case LDAP_PVT_THREAD_EBUSY:
482 ldap_pvt_thread_yield();
489 assert( lc->lc_refcnt == 1 );
490 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
491 ldap_back_conn_cmp, ldap_back_conn_dup );
493 #if PRINT_CONNTREE > 0
494 myprint( li->conntree );
495 #endif /* PRINT_CONNTREE */
497 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
499 Debug( LDAP_DEBUG_TRACE,
500 "=>ldap_back_getconn: conn %p inserted (refcnt=%u)\n",
501 (void *)lc, refcnt, 0 );
503 /* Err could be -1 in case a duplicate ldapconn is inserted */
504 if ( rs->sr_err != 0 ) {
505 ldap_back_conn_free( lc );
506 rs->sr_err = LDAP_OTHER;
507 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
508 send_ldap_error( op, rs, LDAP_OTHER,
509 "internal server error" );
515 Debug( LDAP_DEBUG_TRACE,
516 "=>ldap_back_getconn: conn %p fetched (refcnt=%u)\n",
517 (void *)lc, refcnt, 0 );
524 ldap_back_release_conn(
527 struct ldapconn *lc )
529 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
532 switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
533 case LDAP_PVT_THREAD_EBUSY:
535 ldap_pvt_thread_yield();
542 assert( lc->lc_refcnt > 0 );
544 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
550 * Note: as the check for the value of lc->lc_bound was already here, I removed
551 * it from all the callers, and I made the function return the flag, so
552 * it can be used to simplify the check.
554 * Note: dolock indicates whether li->conn_mutex must be locked or not
557 ldap_back_dobind_int(
561 ldap_back_send_t sendok,
568 assert( retries >= 0 );
570 if ( !lc->lc_bound ) {
571 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
574 * FIXME: we need to let clients use proxyAuthz
575 * otherwise we cannot do symmetric pools of servers;
576 * we have to live with the fact that a user can
577 * authorize itself as any ID that is allowed
578 * by the authzTo directive of the "proxyauthzdn".
581 * NOTE: current Proxy Authorization specification
582 * and implementation do not allow proxy authorization
583 * control to be provided with Bind requests
586 * if no bind took place yet, but the connection is bound
587 * and the "idassert-authcDN" (or other ID) is set,
588 * then bind as the asserting identity and explicitly
589 * add the proxyAuthz control to every operation with the
590 * dn bound to the connection as control value.
591 * This is done also if this is the authrizing backend,
592 * but the "override" flag is given to idassert.
593 * It allows to use SASL bind and yet proxyAuthz users
595 if ( op->o_conn != NULL &&
596 !op->o_do_not_cache &&
597 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
598 ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
600 (void)ldap_back_proxy_authz_bind( lc, op, rs );
604 #ifdef HAVE_CYRUS_SASL
605 if ( lc->lc_ispriv && li->acl_authmethod == LDAP_AUTH_SASL ) {
606 void *defaults = NULL;
608 #if 1 /* will deal with this later... */
609 if ( li->acl_secprops != NULL ) {
610 rc = ldap_set_option( lc->lc_ld,
611 LDAP_OPT_X_SASL_SECPROPS, li->acl_secprops);
613 if( rc != LDAP_OPT_SUCCESS ) {
614 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
615 "(%s,SECPROPS,\"%s\") failed!\n",
616 li->url, li->acl_secprops, 0 );
622 defaults = lutil_sasl_defaults( lc->lc_ld,
623 li->acl_sasl_mech.bv_val,
624 li->acl_sasl_realm.bv_val,
625 li->acl_authcID.bv_val,
626 li->acl_passwd.bv_val,
629 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
630 li->acl_authcDN.bv_val,
631 li->acl_sasl_mech.bv_val, NULL, NULL,
632 LDAP_SASL_QUIET, lutil_sasl_interact,
635 lutil_sasl_freedefs( defaults );
637 rs->sr_err = slap_map_api2result( rs );
638 if ( rs->sr_err != LDAP_SUCCESS ) {
640 send_ldap_result( op, rs );
647 #endif /* HAVE_CYRUS_SASL */
650 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
651 lc->lc_bound_ndn.bv_val,
652 LDAP_SASL_SIMPLE, &lc->lc_cred,
653 NULL, NULL, &msgid );
655 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
659 switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
660 case LDAP_PVT_THREAD_EBUSY:
662 ldap_pvt_thread_yield();
670 assert( lc->lc_refcnt > 0 );
671 if ( lc->lc_refcnt == 1 ) {
672 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
675 /* lc here must be the regular lc, reset and ready for init */
676 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
679 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
681 if ( rs->sr_err == LDAP_SUCCESS ) {
687 ldap_back_freeconn( op, lc );
688 rs->sr_err = slap_map_api2result( rs );
693 rc = ldap_back_op_result( lc, op, rs, msgid, sendok );
694 if ( rc == LDAP_SUCCESS ) {
705 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
707 return ldap_back_dobind_int( lc, op, rs, sendok, 1, 1 );
713 * This is a callback used for chasing referrals using the same
714 * credentials as the original user on this session.
717 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
718 ber_int_t msgid, void *params )
720 struct ldapconn *lc = (struct ldapconn *)params;
722 /* FIXME: add checks on the URL/identity? */
724 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
725 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
734 ldap_back_send_t sendok )
737 LDAPMessage *res = NULL;
740 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
743 rs->sr_matched = NULL;
745 /* if the error recorded in the reply corresponds
746 * to a successful state, get the error from the
747 * remote server response */
748 if ( ERR_OK( rs->sr_err ) ) {
750 struct timeval tv = { 0, 0 };
753 /* if result parsing fails, note the failure reason */
754 switch ( ldap_result( lc->lc_ld, msgid, 1, &tv, &res ) ) {
757 tv.tv_usec = 100000; /* 0.1 s */
758 ldap_pvt_thread_yield();
762 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
767 /* otherwise get the result; if it is not
768 * LDAP_SUCCESS, record it in the reply
769 * structure (this includes
770 * LDAP_COMPARE_{TRUE|FALSE}) */
772 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
773 &match, &text, NULL, NULL, 1 );
775 if ( rc != LDAP_SUCCESS ) {
781 /* if the error in the reply structure is not
782 * LDAP_SUCCESS, try to map it from client
784 if ( !ERR_OK( rs->sr_err ) ) {
785 rs->sr_err = slap_map_api2result( rs );
787 /* internal ops ( op->o_conn == NULL )
788 * must not reply to client */
789 if ( op->o_conn && !op->o_do_not_cache && match ) {
791 /* record the (massaged) matched
792 * DN into the reply structure */
793 rs->sr_matched = match;
797 ( ( sendok & LDAP_BACK_SENDOK )
798 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
800 send_ldap_result( op, rs );
803 if ( rs->sr_matched != match ) {
804 free( (char *)rs->sr_matched );
806 rs->sr_matched = NULL;
807 ldap_memfree( match );
810 ldap_memfree( text );
813 return( ERR_OK( rs->sr_err ) ? 0 : -1 );
816 /* return true if bound, false if failed */
818 ldap_back_retry( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
821 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
824 switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
825 case LDAP_PVT_THREAD_EBUSY:
827 ldap_pvt_thread_yield();
834 if ( lc->lc_refcnt == 1 ) {
835 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
839 /* lc here must be the regular lc, reset and ready for init */
840 rc = ldap_back_prepare_conn( &lc, op, rs, sendok );
841 if ( rc == LDAP_SUCCESS ) {
842 rc = ldap_back_dobind_int( lc, op, rs, sendok, 0, 0 );
846 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
852 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs )
854 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
855 struct berval binddn = slap_empty_bv;
856 struct berval bindcred = slap_empty_bv;
862 * FIXME: we need to let clients use proxyAuthz
863 * otherwise we cannot do symmetric pools of servers;
864 * we have to live with the fact that a user can
865 * authorize itself as any ID that is allowed
866 * by the authzTo directive of the "proxyauthzdn".
869 * NOTE: current Proxy Authorization specification
870 * and implementation do not allow proxy authorization
871 * control to be provided with Bind requests
874 * if no bind took place yet, but the connection is bound
875 * and the "proxyauthzdn" is set, then bind as
876 * "proxyauthzdn" and explicitly add the proxyAuthz
877 * control to every operation with the dn bound
878 * to the connection as control value.
881 /* bind as proxyauthzdn only if no idassert mode
882 * is requested, or if the client's identity
884 switch ( li->idassert_mode ) {
885 case LDAP_BACK_IDASSERT_LEGACY:
886 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) && !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
887 if ( !BER_BVISNULL( &li->idassert_authcDN ) && !BER_BVISEMPTY( &li->idassert_authcDN ) )
889 binddn = li->idassert_authcDN;
890 bindcred = li->idassert_passwd;
897 /* NOTE: rootdn can always idassert */
898 if ( li->idassert_authz && !be_isroot( op ) ) {
899 struct berval authcDN;
901 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
902 authcDN = slap_empty_bv;
905 authcDN = op->o_conn->c_ndn;
907 rs->sr_err = slap_sasl_matches( op, li->idassert_authz,
908 &authcDN, &authcDN );
909 if ( rs->sr_err != LDAP_SUCCESS ) {
910 if ( li->idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
911 send_ldap_result( op, rs );
915 rs->sr_err = LDAP_SUCCESS;
916 binddn = slap_empty_bv;
917 bindcred = slap_empty_bv;
925 binddn = li->idassert_authcDN;
926 bindcred = li->idassert_passwd;
931 if ( dobind && li->idassert_authmethod == LDAP_AUTH_SASL ) {
932 #ifdef HAVE_CYRUS_SASL
933 void *defaults = NULL;
934 struct berval authzID = BER_BVNULL;
937 /* if SASL supports native authz, prepare for it */
938 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
939 ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
941 switch ( li->idassert_mode ) {
942 case LDAP_BACK_IDASSERT_OTHERID:
943 case LDAP_BACK_IDASSERT_OTHERDN:
944 authzID = li->idassert_authzID;
947 case LDAP_BACK_IDASSERT_ANONYMOUS:
948 BER_BVSTR( &authzID, "dn:" );
951 case LDAP_BACK_IDASSERT_SELF:
952 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
953 /* connection is not authc'd, so don't idassert */
954 BER_BVSTR( &authzID, "dn:" );
957 authzID.bv_len = STRLENOF( "dn:" ) + op->o_conn->c_ndn.bv_len;
958 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
959 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
960 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
961 op->o_conn->c_ndn.bv_val, op->o_conn->c_ndn.bv_len + 1 );
970 #if 0 /* will deal with this later... */
971 if ( sasl_secprops != NULL ) {
972 rs->sr_err = ldap_set_option( lc->lc_ld, LDAP_OPT_X_SASL_SECPROPS,
973 (void *) sasl_secprops );
975 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
976 send_ldap_result( op, rs );
983 defaults = lutil_sasl_defaults( lc->lc_ld,
984 li->idassert_sasl_mech.bv_val,
985 li->idassert_sasl_realm.bv_val,
986 li->idassert_authcID.bv_val,
987 li->idassert_passwd.bv_val,
990 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
991 li->idassert_sasl_mech.bv_val, NULL, NULL,
992 LDAP_SASL_QUIET, lutil_sasl_interact,
995 lutil_sasl_freedefs( defaults );
997 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1000 rs->sr_err = slap_map_api2result( rs );
1001 if ( rs->sr_err != LDAP_SUCCESS ) {
1003 send_ldap_result( op, rs );
1009 #endif /* HAVE_CYRUS_SASL */
1012 switch ( li->idassert_authmethod ) {
1013 case LDAP_AUTH_SIMPLE:
1014 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1015 binddn.bv_val, LDAP_SASL_SIMPLE,
1016 &bindcred, NULL, NULL, &msgid );
1019 case LDAP_AUTH_NONE:
1026 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1027 send_ldap_result( op, rs );
1031 rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_SENDERR );
1032 if ( rc == LDAP_SUCCESS ) {
1036 return lc->lc_bound;
1040 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1041 * to existing server-side controls if required; if not,
1042 * the existing server-side controls are placed in *pctrls.
1043 * The caller, after using the controls in client API
1044 * operations, if ( *pctrls != op->o_ctrls ), should
1045 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1046 * The function returns success if the control could
1047 * be added if required, or if it did nothing; in the future,
1048 * it might return some error if it failed.
1050 * if no bind took place yet, but the connection is bound
1051 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1052 * and explicitly add proxyAuthz the control to every operation
1053 * with the dn bound to the connection as control value.
1055 * If no server-side controls are defined for the operation,
1056 * simply add the proxyAuthz control; otherwise, if the
1057 * proxyAuthz control is not already set, add it as
1060 * FIXME: is controls order significant for security?
1061 * ANSWER: controls ordering and interoperability
1062 * must be indicated by the specs of each control; if none
1063 * is specified, the order is irrelevant.
1066 ldap_back_proxy_authz_ctrl(
1067 struct ldapconn *lc,
1070 LDAPControl ***pctrls )
1072 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
1073 LDAPControl **ctrls = NULL;
1076 struct berval assertedID;
1080 rs->sr_err = LDAP_SUCCESS;
1082 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1083 * but if it is not set this test fails. We need a different
1084 * means to detect if idassert is enabled */
1085 if ( ( BER_BVISNULL( &li->idassert_authcID ) || BER_BVISEMPTY( &li->idassert_authcID ) )
1086 && ( BER_BVISNULL( &li->idassert_authcDN ) || BER_BVISEMPTY( &li->idassert_authcDN ) ) )
1091 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1095 if ( li->idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1096 if ( op->o_proxy_authz ) {
1098 * FIXME: we do not want to perform proxyAuthz
1099 * on behalf of the client, because this would
1100 * be performed with "proxyauthzdn" privileges.
1102 * This might actually be too strict, since
1103 * the "proxyauthzdn" authzTo, and each entry's
1104 * authzFrom attributes may be crafted
1105 * to avoid unwanted proxyAuthz to take place.
1108 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1109 rs->sr_text = "proxyAuthz not allowed within namingContext";
1114 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1118 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1122 if ( BER_BVISNULL( &li->idassert_authcDN ) ) {
1126 } else if ( li->idassert_authmethod == LDAP_AUTH_SASL ) {
1127 if ( ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1128 /* && ( !BER_BVISNULL( &op->o_conn->c_ndn ) || lc->lc_bound ) */ )
1130 /* already asserted in SASL via native authz */
1131 /* NOTE: the test on lc->lc_bound is used to trap
1132 * native authorization of anonymous users,
1133 * since in that case op->o_conn->c_ndn is NULL */
1137 } else if ( li->idassert_authz && !be_isroot( op ) ) {
1139 struct berval authcDN;
1141 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1142 authcDN = slap_empty_bv;
1144 authcDN = op->o_conn->c_ndn;
1146 rc = slap_sasl_matches( op, li->idassert_authz,
1147 &authcDN, & authcDN );
1148 if ( rc != LDAP_SUCCESS ) {
1149 if ( li->idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1151 /* op->o_conn->c_ndn is not authorized
1152 * to use idassert */
1159 if ( op->o_proxy_authz ) {
1162 * 1) ignore the already set proxyAuthz control
1163 * 2) leave it in place, and don't set ours
1165 * 4) reject the operation
1167 * option (4) is very drastic
1168 * option (3) will make the remote server reject
1169 * the operation, thus being equivalent to (4)
1170 * option (2) will likely break the idassert
1171 * assumptions, so we cannot accept it;
1172 * option (1) means that we are contradicting
1173 * the client's reques.
1175 * I think (4) is the only correct choice.
1177 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1178 rs->sr_text = "proxyAuthz not allowed within namingContext";
1181 if ( op->o_do_not_cache && op->o_is_auth_check ) {
1182 mode = LDAP_BACK_IDASSERT_NOASSERT;
1185 mode = li->idassert_mode;
1189 case LDAP_BACK_IDASSERT_LEGACY:
1190 case LDAP_BACK_IDASSERT_SELF:
1191 /* original behavior:
1192 * assert the client's identity */
1193 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1194 assertedID = slap_empty_bv;
1196 assertedID = op->o_conn->c_ndn;
1200 case LDAP_BACK_IDASSERT_ANONYMOUS:
1201 /* assert "anonymous" */
1202 assertedID = slap_empty_bv;
1205 case LDAP_BACK_IDASSERT_NOASSERT:
1206 /* don't assert; bind as proxyauthzdn */
1209 case LDAP_BACK_IDASSERT_OTHERID:
1210 case LDAP_BACK_IDASSERT_OTHERDN:
1211 /* assert idassert DN */
1212 assertedID = li->idassert_authzID;
1219 if ( BER_BVISNULL( &assertedID ) ) {
1220 assertedID = slap_empty_bv;
1223 if ( op->o_ctrls ) {
1224 for ( i = 0; op->o_ctrls[ i ]; i++ )
1225 /* just count ctrls */ ;
1228 ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
1229 ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
1231 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1232 ctrls[ 0 ]->ldctl_iscritical = 1;
1234 switch ( li->idassert_mode ) {
1235 /* already in u:ID or dn:DN form */
1236 case LDAP_BACK_IDASSERT_OTHERID:
1237 case LDAP_BACK_IDASSERT_OTHERDN:
1238 ber_dupbv( &ctrls[ 0 ]->ldctl_value, &assertedID );
1241 /* needs the dn: prefix */
1243 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1244 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
1245 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1246 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1247 assertedID.bv_val, assertedID.bv_len + 1 );
1251 if ( op->o_ctrls ) {
1252 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1253 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1256 ctrls[ i + 1 ] = NULL;
1259 if ( ctrls == NULL ) {
1260 ctrls = op->o_ctrls;
1269 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1271 LDAPControl **ctrls = *pctrls;
1273 /* we assume that the first control is the proxyAuthz
1274 * added by back-ldap, so it's the only one we explicitly
1276 if ( ctrls && ctrls != op->o_ctrls ) {
1277 assert( ctrls[ 0 ] != NULL );
1279 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1280 free( ctrls[ 0 ]->ldctl_value.bv_val );