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 /* wait for all other ops to release the connection */
108 while ( lc->lc_refcnt > 1 ) {
109 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
110 ldap_pvt_thread_yield();
111 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
113 assert( lc->lc_refcnt == 1 );
114 lc = avl_delete( &li->conntree, (caddr_t)lc,
115 ldap_back_conn_cmp );
116 assert( lc != NULL );
118 if ( !BER_BVISNULL( &lc->lc_local_ndn ) ) {
119 ch_free( lc->lc_local_ndn.bv_val );
121 ber_dupbv( &lc->lc_local_ndn, &op->o_req_ndn );
122 lerr = avl_insert( &li->conntree, (caddr_t)lc,
123 ldap_back_conn_cmp, ldap_back_conn_dup );
124 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
126 /* handle this! (e.g. wait until refcnt goes to 1...) */
127 ldap_back_conn_free( lc );
133 ldap_back_release_conn( op, rs, lc );
142 * compares two struct ldapconn based on the value of the conn pointer;
146 ldap_back_conn_cmp( const void *c1, const void *c2 )
148 const struct ldapconn *lc1 = (const struct ldapconn *)c1;
149 const struct ldapconn *lc2 = (const struct ldapconn *)c2;
152 /* If local DNs don't match, it is definitely not a match */
153 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
158 /* For shared sessions, conn is NULL. Only explicitly
159 * bound sessions will have non-NULL conn.
161 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
167 * returns -1 in case a duplicate struct ldapconn has been inserted;
171 ldap_back_conn_dup( void *c1, void *c2 )
173 struct ldapconn *lc1 = (struct ldapconn *)c1;
174 struct ldapconn *lc2 = (struct ldapconn *)c2;
176 /* Cannot have more than one shared session with same DN */
177 if ( dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) &&
178 lc1->lc_conn == lc2->lc_conn )
186 #if PRINT_CONNTREE > 0
188 ravl_print( Avlnode *root, int depth )
197 ravl_print( root->avl_right, depth+1 );
199 for ( i = 0; i < depth; i++ ) {
204 printf( "lc(%lx) local(%s) conn(%lx) %d\n",
205 lc, lc->lc_local_ndn.bv_val, lc->lc_conn, root->avl_bf );
207 ravl_print( root->avl_left, depth+1 );
211 myprint( Avlnode *root )
213 printf( "********\n" );
216 printf( "\tNULL\n" );
219 ravl_print( root, 0 );
222 printf( "********\n" );
224 #endif /* PRINT_CONNTREE */
227 ldap_back_freeconn( Operation *op, struct ldapconn *lc )
229 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
232 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
233 assert( lc->lc_refcnt > 0 );
234 if ( --lc->lc_refcnt == 0 ) {
235 lc = avl_delete( &li->conntree, (caddr_t)lc,
236 ldap_back_conn_cmp );
237 assert( lc != NULL );
239 ldap_back_conn_free( (void *)lc );
241 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
247 ldap_back_prepare_conn( struct ldapconn **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
249 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
250 int vers = op->o_protocol;
253 assert( lcp != NULL );
255 rs->sr_err = ldap_initialize( &ld, li->url );
256 if ( rs->sr_err != LDAP_SUCCESS ) {
260 /* Set LDAP version. This will always succeed: If the client
261 * bound with a particular version, then so can we.
263 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
265 /* automatically chase referrals ("[dont-]chase-referrals" statement) */
266 if ( LDAP_BACK_CHASE_REFERRALS( li ) ) {
267 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
271 /* start TLS ("tls-[try-]{start,propagate}" statements) */
272 if ( ( LDAP_BACK_USE_TLS( li ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( li ) ) )
273 && !ldap_is_ldaps_url( li->url ) )
275 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
277 * use asynchronous StartTLS
278 * in case, chase referral (not implemented yet)
282 rs->sr_err = ldap_start_tls( ld, NULL, NULL, &msgid );
283 if ( rs->sr_err == LDAP_SUCCESS ) {
284 LDAPMessage *res = NULL;
286 struct timeval tv = { 0, 0 };
289 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
291 rs->sr_err = LDAP_OTHER;
293 } else if ( rc == 0 ) {
300 rs->sr_err = LDAP_OTHER;
302 } else if ( rc == LDAP_RES_EXTENDED ) {
303 struct berval *data = NULL;
305 rs->sr_err = ldap_parse_extended_result( ld, res,
307 if ( rs->sr_err == LDAP_SUCCESS ) {
308 rs->sr_err = ldap_result2error( ld, res, 1 );
311 /* FIXME: in case a referral
312 * is returned, should we try
313 * using it instead of the
315 if ( rs->sr_err == LDAP_SUCCESS ) {
316 ldap_install_tls( ld );
318 } else if ( rs->sr_err == LDAP_REFERRAL ) {
319 rs->sr_err = LDAP_OTHER;
320 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
324 if ( data->bv_val ) {
325 ber_memfree( data->bv_val );
332 rs->sr_err = LDAP_OTHER;
339 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
341 * use synchronous StartTLS
343 rs->sr_err = ldap_start_tls_s( ld, NULL, NULL );
344 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
346 /* if StartTLS is requested, only attempt it if the URL
347 * is not "ldaps://"; this may occur not only in case
348 * of misconfiguration, but also when used in the chain
349 * overlay, where the "uri" can be parsed out of a referral */
350 if ( rs->sr_err == LDAP_SERVER_DOWN
351 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( li ) ) )
353 ldap_unbind_ext_s( ld, NULL, NULL );
357 /* in case Start TLS is not critical */
358 rs->sr_err = LDAP_SUCCESS;
360 #endif /* HAVE_TLS */
362 if ( *lcp == NULL ) {
363 *lcp = (struct ldapconn *)ch_malloc( sizeof( struct ldapconn ) );
364 memset( *lcp, 0, sizeof( struct ldapconn ) );
367 (*lcp)->lc_refcnt = 1;
370 if ( rs->sr_err != LDAP_SUCCESS ) {
371 rs->sr_err = slap_map_api2result( rs );
372 if ( sendok & LDAP_BACK_SENDERR ) {
373 if ( rs->sr_text == NULL ) {
374 rs->sr_text = "ldap_initialize() failed";
376 send_ldap_result( op, rs );
385 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
387 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
388 struct ldapconn *lc, lc_curr = { 0 };
390 /* Searches for a ldapconn in the avl tree */
392 /* Explicit binds must not be shared */
393 if ( op->o_tag == LDAP_REQ_BIND
395 && op->o_conn->c_authz_backend
396 && op->o_bd->be_private == op->o_conn->c_authz_backend->be_private ) )
398 lc_curr.lc_conn = op->o_conn;
401 lc_curr.lc_conn = NULL;
404 /* Internal searches are privileged and shared. So is root. */
405 if ( op->o_do_not_cache || be_isroot( op ) ) {
406 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
407 lc_curr.lc_conn = NULL;
408 lc_curr.lc_ispriv = 1;
411 lc_curr.lc_local_ndn = op->o_ndn;
414 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
415 lc = (struct ldapconn *)avl_find( li->conntree,
416 (caddr_t)&lc_curr, ldap_back_conn_cmp );
420 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
422 /* Looks like we didn't get a bind. Open a new session... */
424 /* lc here must be NULL */
425 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
429 lc->lc_conn = lc_curr.lc_conn;
430 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
432 ldap_pvt_thread_mutex_init( &lc->lc_mutex );
434 if ( lc_curr.lc_ispriv ) {
435 ber_dupbv( &lc->lc_cred, &li->acl_passwd );
436 ber_dupbv( &lc->lc_bound_ndn, &li->acl_authcDN );
437 lc->lc_ispriv = lc_curr.lc_ispriv;
440 BER_BVZERO( &lc->lc_cred );
441 BER_BVZERO( &lc->lc_bound_ndn );
442 if ( op->o_conn && !BER_BVISEMPTY( &op->o_ndn )
443 && op->o_bd == op->o_conn->c_authz_backend )
445 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
451 /* Inserts the newly created ldapconn in the avl tree */
452 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
453 assert( lc->lc_refcnt == 1 );
454 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
455 ldap_back_conn_cmp, ldap_back_conn_dup );
457 #if PRINT_CONNTREE > 0
458 myprint( li->conntree );
459 #endif /* PRINT_CONNTREE */
461 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
463 Debug( LDAP_DEBUG_TRACE,
464 "=>ldap_back_getconn: conn %p inserted (refcnt=%u)\n",
465 (void *)lc, lc->lc_refcnt, 0 );
467 /* Err could be -1 in case a duplicate ldapconn is inserted */
468 if ( rs->sr_err != 0 ) {
469 ldap_back_conn_free( lc );
470 rs->sr_err = LDAP_OTHER;
471 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
472 send_ldap_error( op, rs, LDAP_OTHER,
473 "internal server error" );
478 Debug( LDAP_DEBUG_TRACE,
479 "=>ldap_back_getconn: conn %p fetched (refcnt=%u)\n",
480 (void *)lc, lc->lc_refcnt, 0 );
487 ldap_back_release_conn(
490 struct ldapconn *lc )
492 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
494 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
495 assert( lc->lc_refcnt > 0 );
497 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
503 * Note: as the check for the value of lc->lc_bound was already here, I removed
504 * it from all the callers, and I made the function return the flag, so
505 * it can be used to simplify the check.
508 ldap_back_dobind_int(
512 ldap_back_send_t sendok,
518 assert( retries >= 0 );
520 if ( !lc->lc_bound ) {
521 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
524 * FIXME: we need to let clients use proxyAuthz
525 * otherwise we cannot do symmetric pools of servers;
526 * we have to live with the fact that a user can
527 * authorize itself as any ID that is allowed
528 * by the authzTo directive of the "proxyauthzdn".
531 * NOTE: current Proxy Authorization specification
532 * and implementation do not allow proxy authorization
533 * control to be provided with Bind requests
536 * if no bind took place yet, but the connection is bound
537 * and the "idassert-authcDN" (or other ID) is set,
538 * then bind as the asserting identity and explicitly
539 * add the proxyAuthz control to every operation with the
540 * dn bound to the connection as control value.
541 * This is done also if this is the authrizing backend,
542 * but the "override" flag is given to idassert.
543 * It allows to use SASL bind and yet proxyAuthz users
545 if ( op->o_conn != NULL &&
546 !op->o_do_not_cache &&
548 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
549 ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
551 (void)ldap_back_proxy_authz_bind( lc, op, rs );
555 #ifdef HAVE_CYRUS_SASL
556 if ( lc->lc_ispriv && li->acl_authmethod == LDAP_AUTH_SASL ) {
557 void *defaults = NULL;
559 #if 1 /* will deal with this later... */
560 if ( li->acl_secprops != NULL ) {
561 rc = ldap_set_option( lc->lc_ld,
562 LDAP_OPT_X_SASL_SECPROPS, li->acl_secprops);
564 if( rc != LDAP_OPT_SUCCESS ) {
565 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
566 "(%s,SECPROPS,\"%s\") failed!\n",
567 li->url, li->acl_secprops, 0 );
573 defaults = lutil_sasl_defaults( lc->lc_ld,
574 li->acl_sasl_mech.bv_val,
575 li->acl_sasl_realm.bv_val,
576 li->acl_authcID.bv_val,
577 li->acl_passwd.bv_val,
580 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
581 li->acl_authcDN.bv_val,
582 li->acl_sasl_mech.bv_val, NULL, NULL,
583 LDAP_SASL_QUIET, lutil_sasl_interact,
586 lutil_sasl_freedefs( defaults );
588 rs->sr_err = slap_map_api2result( rs );
589 if ( rs->sr_err != LDAP_SUCCESS ) {
591 send_ldap_result( op, rs );
598 #endif /* HAVE_CYRUS_SASL */
601 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
602 lc->lc_bound_ndn.bv_val,
603 LDAP_SASL_SIMPLE, &lc->lc_cred,
604 NULL, NULL, &msgid );
606 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
608 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
609 assert( lc->lc_refcnt > 0 );
610 if ( lc->lc_refcnt == 1 ) {
611 ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
614 /* lc here must be the regular lc, reset and ready for init */
615 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
617 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
618 if ( rs->sr_err == LDAP_SUCCESS ) {
624 ldap_back_freeconn( op, lc );
625 rs->sr_err = slap_map_api2result( rs );
630 rc = ldap_back_op_result( lc, op, rs, msgid, sendok );
631 if ( rc == LDAP_SUCCESS ) {
642 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
646 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
647 rc = ldap_back_dobind_int( lc, op, rs, sendok, 1 );
648 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
656 * This is a callback used for chasing referrals using the same
657 * credentials as the original user on this session.
660 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
661 ber_int_t msgid, void *params )
663 struct ldapconn *lc = (struct ldapconn *)params;
665 /* FIXME: add checks on the URL/identity? */
667 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
668 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
677 ldap_back_send_t sendok )
680 LDAPMessage *res = NULL;
683 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
686 rs->sr_matched = NULL;
688 /* if the error recorded in the reply corresponds
689 * to a successful state, get the error from the
690 * remote server response */
691 if ( ERR_OK( rs->sr_err ) ) {
693 struct timeval tv = { 0, 0 };
696 /* if result parsing fails, note the failure reason */
697 switch ( ldap_result( lc->lc_ld, msgid, 1, &tv, &res ) ) {
700 tv.tv_usec = 100000; /* 0.1 s */
701 ldap_pvt_thread_yield();
705 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
710 /* otherwise get the result; if it is not
711 * LDAP_SUCCESS, record it in the reply
712 * structure (this includes
713 * LDAP_COMPARE_{TRUE|FALSE}) */
715 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
716 &match, &text, NULL, NULL, 1 );
718 if ( rc != LDAP_SUCCESS ) {
724 /* if the error in the reply structure is not
725 * LDAP_SUCCESS, try to map it from client
727 if ( !ERR_OK( rs->sr_err ) ) {
728 rs->sr_err = slap_map_api2result( rs );
730 /* internal ops ( op->o_conn == NULL )
731 * must not reply to client */
732 if ( op->o_conn && !op->o_do_not_cache && match ) {
734 /* record the (massaged) matched
735 * DN into the reply structure */
736 rs->sr_matched = match;
740 ( ( sendok & LDAP_BACK_SENDOK )
741 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
743 send_ldap_result( op, rs );
746 if ( rs->sr_matched != match ) {
747 free( (char *)rs->sr_matched );
749 rs->sr_matched = NULL;
750 ldap_memfree( match );
753 ldap_memfree( text );
756 return( ERR_OK( rs->sr_err ) ? 0 : -1 );
759 /* return true if bound, false if failed */
761 ldap_back_retry( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
764 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
766 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
768 if ( lc->lc_refcnt == 1 ) {
769 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
770 ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
774 /* lc here must be the regular lc, reset and ready for init */
775 rc = ldap_back_prepare_conn( &lc, op, rs, sendok );
776 if ( rc == LDAP_SUCCESS ) {
777 rc = ldap_back_dobind_int( lc, op, rs, sendok, 0 );
779 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
782 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
788 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs )
790 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
791 struct berval binddn = slap_empty_bv;
792 struct berval bindcred = slap_empty_bv;
798 * FIXME: we need to let clients use proxyAuthz
799 * otherwise we cannot do symmetric pools of servers;
800 * we have to live with the fact that a user can
801 * authorize itself as any ID that is allowed
802 * by the authzTo directive of the "proxyauthzdn".
805 * NOTE: current Proxy Authorization specification
806 * and implementation do not allow proxy authorization
807 * control to be provided with Bind requests
810 * if no bind took place yet, but the connection is bound
811 * and the "proxyauthzdn" is set, then bind as
812 * "proxyauthzdn" and explicitly add the proxyAuthz
813 * control to every operation with the dn bound
814 * to the connection as control value.
817 /* bind as proxyauthzdn only if no idassert mode
818 * is requested, or if the client's identity
820 switch ( li->idassert_mode ) {
821 case LDAP_BACK_IDASSERT_LEGACY:
822 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) && !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
823 if ( !BER_BVISNULL( &li->idassert_authcDN ) && !BER_BVISEMPTY( &li->idassert_authcDN ) )
825 binddn = li->idassert_authcDN;
826 bindcred = li->idassert_passwd;
833 if ( li->idassert_authz ) {
834 struct berval authcDN;
836 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
837 authcDN = slap_empty_bv;
839 authcDN = op->o_conn->c_ndn;
841 rs->sr_err = slap_sasl_matches( op, li->idassert_authz,
842 &authcDN, &authcDN );
843 if ( rs->sr_err != LDAP_SUCCESS ) {
844 send_ldap_result( op, rs );
850 binddn = li->idassert_authcDN;
851 bindcred = li->idassert_passwd;
856 if ( dobind && li->idassert_authmethod == LDAP_AUTH_SASL ) {
857 #ifdef HAVE_CYRUS_SASL
858 void *defaults = NULL;
859 struct berval authzID = BER_BVNULL;
862 /* if SASL supports native authz, prepare for it */
863 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
864 ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
866 switch ( li->idassert_mode ) {
867 case LDAP_BACK_IDASSERT_OTHERID:
868 case LDAP_BACK_IDASSERT_OTHERDN:
869 authzID = li->idassert_authzID;
872 case LDAP_BACK_IDASSERT_ANONYMOUS:
873 BER_BVSTR( &authzID, "dn:" );
876 case LDAP_BACK_IDASSERT_SELF:
877 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
878 /* connection is not authc'd, so don't idassert */
879 BER_BVSTR( &authzID, "dn:" );
882 authzID.bv_len = STRLENOF( "dn:" ) + op->o_conn->c_ndn.bv_len;
883 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
884 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
885 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
886 op->o_conn->c_ndn.bv_val, op->o_conn->c_ndn.bv_len + 1 );
895 #if 0 /* will deal with this later... */
896 if ( sasl_secprops != NULL ) {
897 rs->sr_err = ldap_set_option( lc->lc_ld, LDAP_OPT_X_SASL_SECPROPS,
898 (void *) sasl_secprops );
900 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
901 send_ldap_result( op, rs );
908 defaults = lutil_sasl_defaults( lc->lc_ld,
909 li->idassert_sasl_mech.bv_val,
910 li->idassert_sasl_realm.bv_val,
911 li->idassert_authcID.bv_val,
912 li->idassert_passwd.bv_val,
915 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
916 li->idassert_sasl_mech.bv_val, NULL, NULL,
917 LDAP_SASL_QUIET, lutil_sasl_interact,
920 lutil_sasl_freedefs( defaults );
922 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
925 rs->sr_err = slap_map_api2result( rs );
926 if ( rs->sr_err != LDAP_SUCCESS ) {
928 send_ldap_result( op, rs );
934 #endif /* HAVE_CYRUS_SASL */
937 switch ( li->idassert_authmethod ) {
938 case LDAP_AUTH_SIMPLE:
939 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
940 binddn.bv_val, LDAP_SASL_SIMPLE,
941 &bindcred, NULL, NULL, &msgid );
951 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
952 send_ldap_result( op, rs );
956 rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_SENDERR );
957 if ( rc == LDAP_SUCCESS ) {
965 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
966 * to existing server-side controls if required; if not,
967 * the existing server-side controls are placed in *pctrls.
968 * The caller, after using the controls in client API
969 * operations, if ( *pctrls != op->o_ctrls ), should
970 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
971 * The function returns success if the control could
972 * be added if required, or if it did nothing; in the future,
973 * it might return some error if it failed.
975 * if no bind took place yet, but the connection is bound
976 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
977 * and explicitly add proxyAuthz the control to every operation
978 * with the dn bound to the connection as control value.
980 * If no server-side controls are defined for the operation,
981 * simply add the proxyAuthz control; otherwise, if the
982 * proxyAuthz control is not already set, add it as
985 * FIXME: is controls order significant for security?
986 * ANSWER: controls ordering and interoperability
987 * must be indicated by the specs of each control; if none
988 * is specified, the order is irrelevant.
991 ldap_back_proxy_authz_ctrl(
995 LDAPControl ***pctrls )
997 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
998 LDAPControl **ctrls = NULL;
1001 struct berval assertedID;
1005 rs->sr_err = LDAP_SUCCESS;
1007 if ( ( BER_BVISNULL( &li->idassert_authcID ) || BER_BVISEMPTY( &li->idassert_authcID ) )
1008 && ( BER_BVISNULL( &li->idassert_authcDN ) || BER_BVISEMPTY( &li->idassert_authcDN ) ) ) {
1012 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1016 if ( li->idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1017 if ( op->o_proxy_authz ) {
1019 * FIXME: we do not want to perform proxyAuthz
1020 * on behalf of the client, because this would
1021 * be performed with "proxyauthzdn" privileges.
1023 * This might actually be too strict, since
1024 * the "proxyauthzdn" authzTo, and each entry's
1025 * authzFrom attributes may be crafted
1026 * to avoid unwanted proxyAuthz to take place.
1029 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1030 rs->sr_text = "proxyAuthz not allowed within namingContext";
1035 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1039 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1043 if ( BER_BVISNULL( &li->idassert_authcDN ) ) {
1047 } else if ( li->idassert_authmethod == LDAP_AUTH_SASL ) {
1048 if ( ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1049 /* && ( !BER_BVISNULL( &op->o_conn->c_ndn ) || lc->lc_bound ) */ )
1051 /* already asserted in SASL via native authz */
1052 /* NOTE: the test on lc->lc_bound is used to trap
1053 * native authorization of anonymous users,
1054 * since in that case op->o_conn->c_ndn is NULL */
1058 } else if ( li->idassert_authz ) {
1060 struct berval authcDN;
1062 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1063 authcDN = slap_empty_bv;
1065 authcDN = op->o_conn->c_ndn;
1067 rc = slap_sasl_matches( op, li->idassert_authz,
1068 &authcDN, & authcDN );
1069 if ( rc != LDAP_SUCCESS ) {
1070 /* op->o_conn->c_ndn is not authorized
1071 * to use idassert */
1076 if ( op->o_proxy_authz ) {
1079 * 1) ignore the already set proxyAuthz control
1080 * 2) leave it in place, and don't set ours
1082 * 4) reject the operation
1084 * option (4) is very drastic
1085 * option (3) will make the remote server reject
1086 * the operation, thus being equivalent to (4)
1087 * option (2) will likely break the idassert
1088 * assumptions, so we cannot accept it;
1089 * option (1) means that we are contradicting
1090 * the client's reques.
1092 * I think (4) is the only correct choice.
1094 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1095 rs->sr_text = "proxyAuthz not allowed within namingContext";
1098 if ( op->o_do_not_cache && op->o_is_auth_check ) {
1099 mode = LDAP_BACK_IDASSERT_NOASSERT;
1102 mode = li->idassert_mode;
1106 case LDAP_BACK_IDASSERT_LEGACY:
1107 case LDAP_BACK_IDASSERT_SELF:
1108 /* original behavior:
1109 * assert the client's identity */
1110 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1111 assertedID = slap_empty_bv;
1113 assertedID = op->o_conn->c_ndn;
1117 case LDAP_BACK_IDASSERT_ANONYMOUS:
1118 /* assert "anonymous" */
1119 assertedID = slap_empty_bv;
1122 case LDAP_BACK_IDASSERT_NOASSERT:
1123 /* don't assert; bind as proxyauthzdn */
1126 case LDAP_BACK_IDASSERT_OTHERID:
1127 case LDAP_BACK_IDASSERT_OTHERDN:
1128 /* assert idassert DN */
1129 assertedID = li->idassert_authzID;
1136 if ( BER_BVISNULL( &assertedID ) ) {
1137 assertedID = slap_empty_bv;
1140 if ( op->o_ctrls ) {
1141 for ( i = 0; op->o_ctrls[ i ]; i++ )
1142 /* just count ctrls */ ;
1145 ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
1146 ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
1148 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1149 ctrls[ 0 ]->ldctl_iscritical = 1;
1151 switch ( li->idassert_mode ) {
1152 /* already in u:ID or dn:DN form */
1153 case LDAP_BACK_IDASSERT_OTHERID:
1154 case LDAP_BACK_IDASSERT_OTHERDN:
1155 ber_dupbv( &ctrls[ 0 ]->ldctl_value, &assertedID );
1158 /* needs the dn: prefix */
1160 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1161 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
1162 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1163 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val + STRLENOF( "dn:" ),
1164 assertedID.bv_val, assertedID.bv_len + 1 );
1168 if ( op->o_ctrls ) {
1169 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1170 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1173 ctrls[ i + 1 ] = NULL;
1176 if ( ctrls == NULL ) {
1177 ctrls = op->o_ctrls;
1186 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1188 LDAPControl **ctrls = *pctrls;
1190 /* we assume that the first control is the proxyAuthz
1191 * added by back-ldap, so it's the only one we explicitly
1193 if ( ctrls && ctrls != op->o_ctrls ) {
1194 assert( ctrls[ 0 ] );
1196 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1197 free( ctrls[ 0 ]->ldctl_value.bv_val );