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
41 * FIXME: temporarily disable pooled connections, as there seem to be
42 * some concurrency problem
44 /* #undef LDAP_BACK_POOLED_CONNS */
46 static LDAP_REBIND_PROC ldap_back_rebind;
49 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs );
52 ldap_back_prepare_conn( struct ldapconn **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
55 ldap_back_bind( Operation *op, SlapReply *rs )
57 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
63 lc = ldap_back_getconn( op, rs, LDAP_BACK_SENDERR );
68 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
69 ch_free( lc->lc_bound_ndn.bv_val );
70 BER_BVZERO( &lc->lc_bound_ndn );
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, 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->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
87 ldap_back_proxy_authz_bind( lc, op, rs );
88 if ( lc->lc_bound == 0 ) {
95 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
97 if ( LDAP_BACK_SAVECRED( li ) ) {
98 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
99 memset( lc->lc_cred.bv_val, 0,
100 lc->lc_cred.bv_len );
102 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
103 ldap_set_rebind_proc( lc->lc_ld, ldap_back_rebind, lc );
108 /* must re-insert if local DN changed as result of bind */
109 if ( lc->lc_bound && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) ) {
112 /* wait for all other ops to release the connection */
114 switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
115 case LDAP_PVT_THREAD_EBUSY:
117 ldap_pvt_thread_yield();
121 if ( lc->lc_refcnt > 1 ) {
122 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
123 ldap_pvt_thread_yield();
129 assert( lc->lc_refcnt == 1 );
130 lc = avl_delete( &li->conntree, (caddr_t)lc,
131 ldap_back_conn_cmp );
132 assert( lc != NULL );
134 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
135 lerr = avl_insert( &li->conntree, (caddr_t)lc,
136 ldap_back_conn_cmp, ldap_back_conn_dup );
137 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
139 /* we can do this because lc_refcnt == 1 */
140 ldap_back_conn_free( lc );
146 ldap_back_release_conn( op, rs, lc );
155 * compares two struct ldapconn based on the value of the conn pointer;
159 ldap_back_conn_cmp( const void *c1, const void *c2 )
161 const struct ldapconn *lc1 = (const struct ldapconn *)c1;
162 const struct ldapconn *lc2 = (const struct ldapconn *)c2;
165 /* If local DNs don't match, it is definitely not a match */
166 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
171 /* For shared sessions, conn is NULL. Only explicitly
172 * bound sessions will have non-NULL conn.
174 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
180 * returns -1 in case a duplicate struct ldapconn has been inserted;
184 ldap_back_conn_dup( void *c1, void *c2 )
186 struct ldapconn *lc1 = (struct ldapconn *)c1;
187 struct ldapconn *lc2 = (struct ldapconn *)c2;
189 /* Cannot have more than one shared session with same DN */
190 if ( dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) &&
191 lc1->lc_conn == lc2->lc_conn )
199 #if PRINT_CONNTREE > 0
201 ravl_print( Avlnode *root, int depth )
210 ravl_print( root->avl_right, depth+1 );
212 for ( i = 0; i < depth; i++ ) {
217 printf( "lc(%lx) local(%s) conn(%lx) %d\n",
218 lc, lc->lc_local_ndn.bv_val, lc->lc_conn, root->avl_bf );
220 ravl_print( root->avl_left, depth+1 );
224 myprint( Avlnode *root )
226 printf( "********\n" );
229 printf( "\tNULL\n" );
232 ravl_print( root, 0 );
235 printf( "********\n" );
237 #endif /* PRINT_CONNTREE */
240 ldap_back_freeconn( Operation *op, struct ldapconn *lc )
242 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
245 switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
246 case LDAP_PVT_THREAD_EBUSY:
248 ldap_pvt_thread_yield();
255 assert( lc->lc_refcnt > 0 );
256 if ( --lc->lc_refcnt == 0 ) {
257 lc = avl_delete( &li->conntree, (caddr_t)lc,
258 ldap_back_conn_cmp );
259 assert( lc != NULL );
261 ldap_back_conn_free( (void *)lc );
264 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
270 ldap_back_prepare_conn( struct ldapconn **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
272 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
273 int vers = op->o_protocol;
276 assert( lcp != NULL );
278 rs->sr_err = ldap_initialize( &ld, li->url );
279 if ( rs->sr_err != LDAP_SUCCESS ) {
283 /* Set LDAP version. This will always succeed: If the client
284 * bound with a particular version, then so can we.
286 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
288 /* automatically chase referrals ("[dont-]chase-referrals" statement) */
289 if ( LDAP_BACK_CHASE_REFERRALS( li ) ) {
290 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
294 /* start TLS ("tls-[try-]{start,propagate}" statements) */
295 if ( ( LDAP_BACK_USE_TLS( li ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( li ) ) )
296 && !ldap_is_ldaps_url( li->url ) )
298 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
300 * use asynchronous StartTLS
301 * in case, chase referral (not implemented yet)
305 rs->sr_err = ldap_start_tls( ld, NULL, NULL, &msgid );
306 if ( rs->sr_err == LDAP_SUCCESS ) {
307 LDAPMessage *res = NULL;
309 struct timeval tv = { 0, 0 };
312 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
314 rs->sr_err = LDAP_OTHER;
316 } else if ( rc == 0 ) {
323 rs->sr_err = LDAP_OTHER;
325 } else if ( rc == LDAP_RES_EXTENDED ) {
326 struct berval *data = NULL;
328 rs->sr_err = ldap_parse_extended_result( ld, res,
330 if ( rs->sr_err == LDAP_SUCCESS ) {
331 rs->sr_err = ldap_result2error( ld, res, 1 );
334 /* FIXME: in case a referral
335 * is returned, should we try
336 * using it instead of the
338 if ( rs->sr_err == LDAP_SUCCESS ) {
339 ldap_install_tls( ld );
341 } else if ( rs->sr_err == LDAP_REFERRAL ) {
342 rs->sr_err = LDAP_OTHER;
343 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
347 if ( data->bv_val ) {
348 ber_memfree( data->bv_val );
355 rs->sr_err = LDAP_OTHER;
362 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
364 * use synchronous StartTLS
366 rs->sr_err = ldap_start_tls_s( ld, NULL, NULL );
367 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
369 /* if StartTLS is requested, only attempt it if the URL
370 * is not "ldaps://"; this may occur not only in case
371 * of misconfiguration, but also when used in the chain
372 * overlay, where the "uri" can be parsed out of a referral */
373 if ( rs->sr_err == LDAP_SERVER_DOWN
374 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( li ) ) )
376 ldap_unbind_ext_s( ld, NULL, NULL );
380 /* in case Start TLS is not critical */
381 rs->sr_err = LDAP_SUCCESS;
383 #endif /* HAVE_TLS */
385 if ( *lcp == NULL ) {
386 *lcp = (struct ldapconn *)ch_calloc( 1, sizeof( struct ldapconn ) );
389 (*lcp)->lc_refcnt = 1;
392 if ( rs->sr_err != LDAP_SUCCESS ) {
393 rs->sr_err = slap_map_api2result( rs );
394 if ( sendok & LDAP_BACK_SENDERR ) {
395 if ( rs->sr_text == NULL ) {
396 rs->sr_text = "ldap_initialize() failed";
398 send_ldap_result( op, rs );
407 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
409 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
410 struct ldapconn *lc, lc_curr = { 0 };
413 /* Searches for a ldapconn in the avl tree */
415 /* Explicit binds must not be shared */
416 if ( op->o_tag == LDAP_REQ_BIND
418 && op->o_conn->c_authz_backend
419 && op->o_bd->be_private == op->o_conn->c_authz_backend->be_private ) )
421 lc_curr.lc_conn = op->o_conn;
424 lc_curr.lc_conn = NULL;
427 /* Internal searches are privileged and shared. So is root. */
428 #ifdef LDAP_BACK_POOLED_CONNS
429 /* FIXME: there seem to be concurrency issues */
430 if ( op->o_do_not_cache || be_isroot( op ) ) {
431 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
432 lc_curr.lc_conn = NULL;
433 lc_curr.lc_ispriv = 1;
436 #endif /* LDAP_BACK_POOLED_CONNS */
438 lc_curr.lc_local_ndn = op->o_ndn;
442 switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
443 case LDAP_PVT_THREAD_EBUSY:
445 ldap_pvt_thread_yield();
452 lc = (struct ldapconn *)avl_find( li->conntree,
453 (caddr_t)&lc_curr, ldap_back_conn_cmp );
455 refcnt = ++lc->lc_refcnt;
457 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
459 /* Looks like we didn't get a bind. Open a new session... */
461 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
465 lc->lc_conn = lc_curr.lc_conn;
466 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
468 ldap_pvt_thread_mutex_init( &lc->lc_mutex );
470 if ( lc_curr.lc_ispriv ) {
471 ber_dupbv( &lc->lc_cred, &li->acl_passwd );
472 ber_dupbv( &lc->lc_bound_ndn, &li->acl_authcDN );
473 lc->lc_ispriv = lc_curr.lc_ispriv;
476 BER_BVZERO( &lc->lc_cred );
477 BER_BVZERO( &lc->lc_bound_ndn );
478 if ( op->o_conn && !BER_BVISEMPTY( &op->o_ndn )
479 && op->o_bd->be_private == op->o_conn->c_authz_backend->be_private )
481 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
487 /* Inserts the newly created ldapconn in the avl tree */
489 switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
490 case LDAP_PVT_THREAD_EBUSY:
492 ldap_pvt_thread_yield();
499 assert( lc->lc_refcnt == 1 );
500 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
501 ldap_back_conn_cmp, ldap_back_conn_dup );
503 #if PRINT_CONNTREE > 0
504 myprint( li->conntree );
505 #endif /* PRINT_CONNTREE */
507 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
509 Debug( LDAP_DEBUG_TRACE,
510 "=>ldap_back_getconn: conn %p inserted (refcnt=%u)\n",
511 (void *)lc, refcnt, 0 );
513 /* Err could be -1 in case a duplicate ldapconn is inserted */
514 if ( rs->sr_err != 0 ) {
515 ldap_back_conn_free( lc );
516 rs->sr_err = LDAP_OTHER;
517 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
518 send_ldap_error( op, rs, LDAP_OTHER,
519 "internal server error" );
525 Debug( LDAP_DEBUG_TRACE,
526 "=>ldap_back_getconn: conn %p fetched (refcnt=%u)\n",
527 (void *)lc, refcnt, 0 );
534 ldap_back_release_conn(
537 struct ldapconn *lc )
539 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
542 switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
543 case LDAP_PVT_THREAD_EBUSY:
545 ldap_pvt_thread_yield();
552 assert( lc->lc_refcnt > 0 );
554 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
560 * Note: as the check for the value of lc->lc_bound was already here, I removed
561 * it from all the callers, and I made the function return the flag, so
562 * it can be used to simplify the check.
564 * Note: lc_mutex is locked; dolock indicates whether li->conn_mutex
565 * must be locked or not
568 ldap_back_dobind_int(
572 ldap_back_send_t sendok,
579 assert( retries >= 0 );
581 if ( !lc->lc_bound ) {
582 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
585 * FIXME: we need to let clients use proxyAuthz
586 * otherwise we cannot do symmetric pools of servers;
587 * we have to live with the fact that a user can
588 * authorize itself as any ID that is allowed
589 * by the authzTo directive of the "proxyauthzdn".
592 * NOTE: current Proxy Authorization specification
593 * and implementation do not allow proxy authorization
594 * control to be provided with Bind requests
597 * if no bind took place yet, but the connection is bound
598 * and the "idassert-authcDN" (or other ID) is set,
599 * then bind as the asserting identity and explicitly
600 * add the proxyAuthz control to every operation with the
601 * dn bound to the connection as control value.
602 * This is done also if this is the authrizing backend,
603 * but the "override" flag is given to idassert.
604 * It allows to use SASL bind and yet proxyAuthz users
606 if ( op->o_conn != NULL &&
607 !op->o_do_not_cache &&
608 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
609 ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
611 (void)ldap_back_proxy_authz_bind( lc, op, rs );
615 #ifdef HAVE_CYRUS_SASL
616 if ( lc->lc_ispriv && li->acl_authmethod == LDAP_AUTH_SASL ) {
617 void *defaults = NULL;
619 #if 1 /* will deal with this later... */
620 if ( li->acl_secprops != NULL ) {
621 rc = ldap_set_option( lc->lc_ld,
622 LDAP_OPT_X_SASL_SECPROPS, li->acl_secprops);
624 if( rc != LDAP_OPT_SUCCESS ) {
625 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
626 "(%s,SECPROPS,\"%s\") failed!\n",
627 li->url, li->acl_secprops, 0 );
633 defaults = lutil_sasl_defaults( lc->lc_ld,
634 li->acl_sasl_mech.bv_val,
635 li->acl_sasl_realm.bv_val,
636 li->acl_authcID.bv_val,
637 li->acl_passwd.bv_val,
640 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
641 li->acl_authcDN.bv_val,
642 li->acl_sasl_mech.bv_val, NULL, NULL,
643 LDAP_SASL_QUIET, lutil_sasl_interact,
646 lutil_sasl_freedefs( defaults );
648 rs->sr_err = slap_map_api2result( rs );
649 if ( rs->sr_err != LDAP_SUCCESS ) {
651 send_ldap_result( op, rs );
658 #endif /* HAVE_CYRUS_SASL */
661 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
662 lc->lc_bound_ndn.bv_val,
663 LDAP_SASL_SIMPLE, &lc->lc_cred,
664 NULL, NULL, &msgid );
666 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
670 switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
671 case LDAP_PVT_THREAD_EBUSY:
673 ldap_pvt_thread_yield();
681 assert( lc->lc_refcnt > 0 );
682 if ( lc->lc_refcnt == 1 ) {
683 ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
686 /* lc here must be the regular lc, reset and ready for init */
687 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
690 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
692 if ( rs->sr_err == LDAP_SUCCESS ) {
698 ldap_back_freeconn( op, lc );
699 rs->sr_err = slap_map_api2result( rs );
704 rc = ldap_back_op_result( lc, op, rs, msgid, sendok );
705 if ( rc == LDAP_SUCCESS ) {
716 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
720 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
721 rc = ldap_back_dobind_int( lc, op, rs, sendok, 1, 1 );
722 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
730 * This is a callback used for chasing referrals using the same
731 * credentials as the original user on this session.
734 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
735 ber_int_t msgid, void *params )
737 struct ldapconn *lc = (struct ldapconn *)params;
739 /* FIXME: add checks on the URL/identity? */
741 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
742 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
751 ldap_back_send_t sendok )
754 LDAPMessage *res = NULL;
757 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
760 rs->sr_matched = NULL;
762 /* if the error recorded in the reply corresponds
763 * to a successful state, get the error from the
764 * remote server response */
765 if ( ERR_OK( rs->sr_err ) ) {
767 struct timeval tv = { 0, 0 };
770 /* if result parsing fails, note the failure reason */
771 switch ( ldap_result( lc->lc_ld, msgid, 1, &tv, &res ) ) {
774 tv.tv_usec = 100000; /* 0.1 s */
775 ldap_pvt_thread_yield();
779 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
784 /* otherwise get the result; if it is not
785 * LDAP_SUCCESS, record it in the reply
786 * structure (this includes
787 * LDAP_COMPARE_{TRUE|FALSE}) */
789 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
790 &match, &text, NULL, NULL, 1 );
792 if ( rc != LDAP_SUCCESS ) {
798 /* if the error in the reply structure is not
799 * LDAP_SUCCESS, try to map it from client
801 if ( !ERR_OK( rs->sr_err ) ) {
802 rs->sr_err = slap_map_api2result( rs );
804 /* internal ops ( op->o_conn == NULL )
805 * must not reply to client */
806 if ( op->o_conn && !op->o_do_not_cache && match ) {
808 /* record the (massaged) matched
809 * DN into the reply structure */
810 rs->sr_matched = match;
814 ( ( sendok & LDAP_BACK_SENDOK )
815 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
817 send_ldap_result( op, rs );
820 if ( rs->sr_matched != match ) {
821 free( (char *)rs->sr_matched );
823 rs->sr_matched = NULL;
824 ldap_memfree( match );
827 ldap_memfree( text );
830 return( ERR_OK( rs->sr_err ) ? 0 : -1 );
833 /* return true if bound, false if failed */
835 ldap_back_retry( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
838 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
841 switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
842 case LDAP_PVT_THREAD_EBUSY:
844 ldap_pvt_thread_yield();
851 if ( lc->lc_refcnt == 1 ) {
852 ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
856 /* lc here must be the regular lc, reset and ready for init */
857 rc = ldap_back_prepare_conn( &lc, op, rs, sendok );
858 if ( rc == LDAP_SUCCESS ) {
859 rc = ldap_back_dobind_int( lc, op, rs, sendok, 0, 0 );
863 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
869 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs )
871 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
872 struct berval binddn = slap_empty_bv;
873 struct berval bindcred = slap_empty_bv;
879 * FIXME: we need to let clients use proxyAuthz
880 * otherwise we cannot do symmetric pools of servers;
881 * we have to live with the fact that a user can
882 * authorize itself as any ID that is allowed
883 * by the authzTo directive of the "proxyauthzdn".
886 * NOTE: current Proxy Authorization specification
887 * and implementation do not allow proxy authorization
888 * control to be provided with Bind requests
891 * if no bind took place yet, but the connection is bound
892 * and the "proxyauthzdn" is set, then bind as
893 * "proxyauthzdn" and explicitly add the proxyAuthz
894 * control to every operation with the dn bound
895 * to the connection as control value.
898 /* bind as proxyauthzdn only if no idassert mode
899 * is requested, or if the client's identity
901 switch ( li->idassert_mode ) {
902 case LDAP_BACK_IDASSERT_LEGACY:
903 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) && !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
904 if ( !BER_BVISNULL( &li->idassert_authcDN ) && !BER_BVISEMPTY( &li->idassert_authcDN ) )
906 binddn = li->idassert_authcDN;
907 bindcred = li->idassert_passwd;
914 /* NOTE: rootdn can always idassert */
915 if ( li->idassert_authz && !be_isroot( op ) ) {
916 struct berval authcDN;
918 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
919 authcDN = slap_empty_bv;
922 authcDN = op->o_conn->c_ndn;
924 rs->sr_err = slap_sasl_matches( op, li->idassert_authz,
925 &authcDN, &authcDN );
926 if ( rs->sr_err != LDAP_SUCCESS ) {
927 if ( li->idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
928 send_ldap_result( op, rs );
932 rs->sr_err = LDAP_SUCCESS;
933 binddn = slap_empty_bv;
934 bindcred = slap_empty_bv;
942 binddn = li->idassert_authcDN;
943 bindcred = li->idassert_passwd;
948 if ( dobind && li->idassert_authmethod == LDAP_AUTH_SASL ) {
949 #ifdef HAVE_CYRUS_SASL
950 void *defaults = NULL;
951 struct berval authzID = BER_BVNULL;
954 /* if SASL supports native authz, prepare for it */
955 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
956 ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
958 switch ( li->idassert_mode ) {
959 case LDAP_BACK_IDASSERT_OTHERID:
960 case LDAP_BACK_IDASSERT_OTHERDN:
961 authzID = li->idassert_authzID;
964 case LDAP_BACK_IDASSERT_ANONYMOUS:
965 BER_BVSTR( &authzID, "dn:" );
968 case LDAP_BACK_IDASSERT_SELF:
969 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
970 /* connection is not authc'd, so don't idassert */
971 BER_BVSTR( &authzID, "dn:" );
974 authzID.bv_len = STRLENOF( "dn:" ) + op->o_conn->c_ndn.bv_len;
975 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
976 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
977 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
978 op->o_conn->c_ndn.bv_val, op->o_conn->c_ndn.bv_len + 1 );
987 #if 0 /* will deal with this later... */
988 if ( sasl_secprops != NULL ) {
989 rs->sr_err = ldap_set_option( lc->lc_ld, LDAP_OPT_X_SASL_SECPROPS,
990 (void *) sasl_secprops );
992 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
993 send_ldap_result( op, rs );
1000 defaults = lutil_sasl_defaults( lc->lc_ld,
1001 li->idassert_sasl_mech.bv_val,
1002 li->idassert_sasl_realm.bv_val,
1003 li->idassert_authcID.bv_val,
1004 li->idassert_passwd.bv_val,
1007 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1008 li->idassert_sasl_mech.bv_val, NULL, NULL,
1009 LDAP_SASL_QUIET, lutil_sasl_interact,
1012 lutil_sasl_freedefs( defaults );
1014 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1017 rs->sr_err = slap_map_api2result( rs );
1018 if ( rs->sr_err != LDAP_SUCCESS ) {
1020 send_ldap_result( op, rs );
1026 #endif /* HAVE_CYRUS_SASL */
1029 switch ( li->idassert_authmethod ) {
1030 case LDAP_AUTH_SIMPLE:
1031 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1032 binddn.bv_val, LDAP_SASL_SIMPLE,
1033 &bindcred, NULL, NULL, &msgid );
1036 case LDAP_AUTH_NONE:
1043 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1044 send_ldap_result( op, rs );
1048 rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_SENDERR );
1049 if ( rc == LDAP_SUCCESS ) {
1053 return lc->lc_bound;
1057 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1058 * to existing server-side controls if required; if not,
1059 * the existing server-side controls are placed in *pctrls.
1060 * The caller, after using the controls in client API
1061 * operations, if ( *pctrls != op->o_ctrls ), should
1062 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1063 * The function returns success if the control could
1064 * be added if required, or if it did nothing; in the future,
1065 * it might return some error if it failed.
1067 * if no bind took place yet, but the connection is bound
1068 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1069 * and explicitly add proxyAuthz the control to every operation
1070 * with the dn bound to the connection as control value.
1072 * If no server-side controls are defined for the operation,
1073 * simply add the proxyAuthz control; otherwise, if the
1074 * proxyAuthz control is not already set, add it as
1077 * FIXME: is controls order significant for security?
1078 * ANSWER: controls ordering and interoperability
1079 * must be indicated by the specs of each control; if none
1080 * is specified, the order is irrelevant.
1083 ldap_back_proxy_authz_ctrl(
1084 struct ldapconn *lc,
1087 LDAPControl ***pctrls )
1089 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
1090 LDAPControl **ctrls = NULL;
1093 struct berval assertedID;
1097 rs->sr_err = LDAP_SUCCESS;
1099 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1100 * but if it is not set this test fails. We need a different
1101 * means to detect if idassert is enabled */
1102 if ( ( BER_BVISNULL( &li->idassert_authcID ) || BER_BVISEMPTY( &li->idassert_authcID ) )
1103 && ( BER_BVISNULL( &li->idassert_authcDN ) || BER_BVISEMPTY( &li->idassert_authcDN ) ) )
1108 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1112 if ( li->idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1113 if ( op->o_proxy_authz ) {
1115 * FIXME: we do not want to perform proxyAuthz
1116 * on behalf of the client, because this would
1117 * be performed with "proxyauthzdn" privileges.
1119 * This might actually be too strict, since
1120 * the "proxyauthzdn" authzTo, and each entry's
1121 * authzFrom attributes may be crafted
1122 * to avoid unwanted proxyAuthz to take place.
1125 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1126 rs->sr_text = "proxyAuthz not allowed within namingContext";
1131 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1135 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1139 if ( BER_BVISNULL( &li->idassert_authcDN ) ) {
1143 } else if ( li->idassert_authmethod == LDAP_AUTH_SASL ) {
1144 if ( ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1145 /* && ( !BER_BVISNULL( &op->o_conn->c_ndn ) || lc->lc_bound ) */ )
1147 /* already asserted in SASL via native authz */
1148 /* NOTE: the test on lc->lc_bound is used to trap
1149 * native authorization of anonymous users,
1150 * since in that case op->o_conn->c_ndn is NULL */
1154 } else if ( li->idassert_authz && !be_isroot( op ) ) {
1156 struct berval authcDN;
1158 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1159 authcDN = slap_empty_bv;
1161 authcDN = op->o_conn->c_ndn;
1163 rc = slap_sasl_matches( op, li->idassert_authz,
1164 &authcDN, & authcDN );
1165 if ( rc != LDAP_SUCCESS ) {
1166 if ( li->idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1168 /* op->o_conn->c_ndn is not authorized
1169 * to use idassert */
1176 if ( op->o_proxy_authz ) {
1179 * 1) ignore the already set proxyAuthz control
1180 * 2) leave it in place, and don't set ours
1182 * 4) reject the operation
1184 * option (4) is very drastic
1185 * option (3) will make the remote server reject
1186 * the operation, thus being equivalent to (4)
1187 * option (2) will likely break the idassert
1188 * assumptions, so we cannot accept it;
1189 * option (1) means that we are contradicting
1190 * the client's reques.
1192 * I think (4) is the only correct choice.
1194 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1195 rs->sr_text = "proxyAuthz not allowed within namingContext";
1198 if ( op->o_do_not_cache && op->o_is_auth_check ) {
1199 mode = LDAP_BACK_IDASSERT_NOASSERT;
1202 mode = li->idassert_mode;
1206 case LDAP_BACK_IDASSERT_LEGACY:
1207 case LDAP_BACK_IDASSERT_SELF:
1208 /* original behavior:
1209 * assert the client's identity */
1210 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1211 assertedID = slap_empty_bv;
1213 assertedID = op->o_conn->c_ndn;
1217 case LDAP_BACK_IDASSERT_ANONYMOUS:
1218 /* assert "anonymous" */
1219 assertedID = slap_empty_bv;
1222 case LDAP_BACK_IDASSERT_NOASSERT:
1223 /* don't assert; bind as proxyauthzdn */
1226 case LDAP_BACK_IDASSERT_OTHERID:
1227 case LDAP_BACK_IDASSERT_OTHERDN:
1228 /* assert idassert DN */
1229 assertedID = li->idassert_authzID;
1236 if ( BER_BVISNULL( &assertedID ) ) {
1237 assertedID = slap_empty_bv;
1240 if ( op->o_ctrls ) {
1241 for ( i = 0; op->o_ctrls[ i ]; i++ )
1242 /* just count ctrls */ ;
1245 ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
1246 ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
1248 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1249 ctrls[ 0 ]->ldctl_iscritical = 1;
1251 switch ( li->idassert_mode ) {
1252 /* already in u:ID or dn:DN form */
1253 case LDAP_BACK_IDASSERT_OTHERID:
1254 case LDAP_BACK_IDASSERT_OTHERDN:
1255 ber_dupbv( &ctrls[ 0 ]->ldctl_value, &assertedID );
1258 /* needs the dn: prefix */
1260 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1261 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
1262 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1263 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1264 assertedID.bv_val, assertedID.bv_len + 1 );
1268 if ( op->o_ctrls ) {
1269 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1270 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1273 ctrls[ i + 1 ] = NULL;
1276 if ( ctrls == NULL ) {
1277 ctrls = op->o_ctrls;
1286 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1288 LDAPControl **ctrls = *pctrls;
1290 /* we assume that the first control is the proxyAuthz
1291 * added by back-ldap, so it's the only one we explicitly
1293 if ( ctrls && ctrls != op->o_ctrls ) {
1294 assert( ctrls[ 0 ] != NULL );
1296 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1297 free( ctrls[ 0 ]->ldctl_value.bv_val );