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_SENDRESULT );
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 ( li->flags & LDAP_BACK_F_SAVECRED ) {
91 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
92 memset( lc->lc_cred.bv_val, 0,
94 ch_free( lc->lc_cred.bv_val );
96 ber_dupbv( &lc->lc_cred, &op->orb_cred );
97 ldap_set_rebind_proc( lc->lc_ld, ldap_back_rebind, lc );
102 /* must re-insert if local DN changed as result of bind */
103 if ( lc->lc_bound && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) ) {
106 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
107 lc = avl_delete( &li->conntree, (caddr_t)lc,
108 ldap_back_conn_cmp );
109 if ( !BER_BVISNULL( &lc->lc_local_ndn ) ) {
110 ch_free( lc->lc_local_ndn.bv_val );
112 ber_dupbv( &lc->lc_local_ndn, &op->o_req_ndn );
113 lerr = avl_insert( &li->conntree, (caddr_t)lc,
114 ldap_back_conn_cmp, ldap_back_conn_dup );
115 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
117 ldap_back_conn_free( lc );
127 * compares two struct ldapconn based on the value of the conn pointer;
131 ldap_back_conn_cmp( const void *c1, const void *c2 )
133 const struct ldapconn *lc1 = (const struct ldapconn *)c1;
134 const struct ldapconn *lc2 = (const struct ldapconn *)c2;
137 /* If local DNs don't match, it is definitely not a match */
138 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
143 /* For shared sessions, conn is NULL. Only explicitly
144 * bound sessions will have non-NULL conn.
146 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
152 * returns -1 in case a duplicate struct ldapconn has been inserted;
156 ldap_back_conn_dup( void *c1, void *c2 )
158 struct ldapconn *lc1 = (struct ldapconn *)c1;
159 struct ldapconn *lc2 = (struct ldapconn *)c2;
161 /* Cannot have more than one shared session with same DN */
162 if ( dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) &&
163 lc1->lc_conn == lc2->lc_conn )
171 #if PRINT_CONNTREE > 0
173 ravl_print( Avlnode *root, int depth )
182 ravl_print( root->avl_right, depth+1 );
184 for ( i = 0; i < depth; i++ ) {
189 printf( "lc(%lx) local(%s) conn(%lx) %d\n",
190 lc, lc->lc_local_ndn.bv_val, lc->lc_conn, root->avl_bf );
192 ravl_print( root->avl_left, depth+1 );
196 myprint( Avlnode *root )
198 printf( "********\n" );
201 printf( "\tNULL\n" );
204 ravl_print( root, 0 );
207 printf( "********\n" );
209 #endif /* PRINT_CONNTREE */
212 ldap_back_freeconn( Operation *op, struct ldapconn *lc )
214 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
216 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
217 lc = avl_delete( &li->conntree, (caddr_t)lc,
218 ldap_back_conn_cmp );
219 ldap_back_conn_free( (void *)lc );
220 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
226 ldap_back_prepare_conn( struct ldapconn **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
228 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
229 int vers = op->o_protocol;
232 assert( lcp != NULL );
234 rs->sr_err = ldap_initialize( &ld, li->url );
235 if ( rs->sr_err != LDAP_SUCCESS ) {
239 /* Set LDAP version. This will always succeed: If the client
240 * bound with a particular version, then so can we.
242 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
244 /* automatically chase referrals ("chase-referrals"/"dont-chase-referrals" statement) */
245 if ( li->flags & LDAP_BACK_F_CHASE_REFERRALS ) {
246 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
249 /* start TLS ("start-tls"/"try-start-tls" statements) */
250 if ( ( li->flags & LDAP_BACK_F_USE_TLS )
251 && !ldap_is_ldaps_url( li->url )
252 && ( rs->sr_err = ldap_start_tls_s( ld, NULL, NULL ) ) != LDAP_SUCCESS )
254 /* if StartTLS is requested, only attempt it if the URL
255 * is not "ldaps://"; this may occur not only in case
256 * of misconfiguration, but also when used in the chain
257 * overlay, where the "uri" can be parsed out of a referral */
258 if ( rs->sr_err == LDAP_SERVER_DOWN
259 || ( li->flags & LDAP_BACK_F_TLS_CRITICAL ) )
261 ldap_unbind_ext_s( ld, NULL, NULL );
266 if ( *lcp == NULL ) {
267 *lcp = (struct ldapconn *)ch_malloc( sizeof( struct ldapconn ) );
268 memset( *lcp, 0, sizeof( struct ldapconn ) );
273 if ( rs->sr_err != LDAP_SUCCESS ) {
274 rs->sr_err = slap_map_api2result( rs );
275 if ( sendok & LDAP_BACK_SENDERR ) {
276 if ( rs->sr_text == NULL ) {
277 rs->sr_text = "ldap_initialize() failed";
279 send_ldap_result( op, rs );
288 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
290 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
291 struct ldapconn *lc, lc_curr;
294 /* Searches for a ldapconn in the avl tree */
296 /* Explicit binds must not be shared */
297 if ( op->o_tag == LDAP_REQ_BIND
299 && op->o_conn->c_authz_backend
300 && op->o_bd->be_private == op->o_conn->c_authz_backend->be_private ) )
302 lc_curr.lc_conn = op->o_conn;
305 lc_curr.lc_conn = NULL;
308 /* Internal searches are privileged and shared. So is root. */
309 if ( op->o_do_not_cache || be_isroot( op ) ) {
310 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
311 lc_curr.lc_conn = NULL;
315 lc_curr.lc_local_ndn = op->o_ndn;
318 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
319 lc = (struct ldapconn *)avl_find( li->conntree,
320 (caddr_t)&lc_curr, ldap_back_conn_cmp );
321 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
323 /* Looks like we didn't get a bind. Open a new session... */
325 /* lc here must be NULL */
326 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
330 lc->lc_conn = lc_curr.lc_conn;
331 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
333 ldap_pvt_thread_mutex_init( &lc->lc_mutex );
336 ber_dupbv( &lc->lc_cred, &li->acl_passwd );
337 ber_dupbv( &lc->lc_bound_ndn, &li->acl_authcDN );
340 BER_BVZERO( &lc->lc_cred );
341 BER_BVZERO( &lc->lc_bound_ndn );
342 if ( op->o_conn && !BER_BVISEMPTY( &op->o_ndn )
343 && op->o_bd == op->o_conn->c_authz_backend )
345 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
351 /* Inserts the newly created ldapconn in the avl tree */
352 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
353 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
354 ldap_back_conn_cmp, ldap_back_conn_dup );
356 #if PRINT_CONNTREE > 0
357 myprint( li->conntree );
358 #endif /* PRINT_CONNTREE */
360 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
362 Debug( LDAP_DEBUG_TRACE,
363 "=>ldap_back_getconn: conn %p inserted\n", (void *) lc, 0, 0 );
365 /* Err could be -1 in case a duplicate ldapconn is inserted */
366 if ( rs->sr_err != 0 ) {
367 ldap_back_conn_free( lc );
368 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
369 send_ldap_error( op, rs, LDAP_OTHER,
370 "internal server error" );
375 Debug( LDAP_DEBUG_TRACE,
376 "=>ldap_back_getconn: conn %p fetched\n", (void *) lc, 0, 0 );
385 * Note: as the check for the value of lc->lc_bound was already here, I removed
386 * it from all the callers, and I made the function return the flag, so
387 * it can be used to simplify the check.
390 ldap_back_dobind_int(
394 ldap_back_send_t sendok,
400 assert( retries >= 0 );
402 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
403 if ( !lc->lc_bound ) {
404 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
407 * FIXME: we need to let clients use proxyAuthz
408 * otherwise we cannot do symmetric pools of servers;
409 * we have to live with the fact that a user can
410 * authorize itself as any ID that is allowed
411 * by the authzTo directive of the "proxyauthzdn".
414 * NOTE: current Proxy Authorization specification
415 * and implementation do not allow proxy authorization
416 * control to be provided with Bind requests
419 * if no bind took place yet, but the connection is bound
420 * and the "idassert-authcDN" (or other ID) is set,
421 * then bind as the asserting identity and explicitly
422 * add the proxyAuthz control to every operation with the
423 * dn bound to the connection as control value.
424 * This is done also if this is the authrizing backend,
425 * but the "override" flag is given to idassert.
426 * It allows to use SASL bind and yet proxyAuthz users
428 if ( op->o_conn != NULL &&
429 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
430 ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
432 (void)ldap_back_proxy_authz_bind( lc, op, rs );
437 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
438 lc->lc_bound_ndn.bv_val,
439 LDAP_SASL_SIMPLE, &lc->lc_cred,
440 NULL, NULL, &msgid );
442 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
444 ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
447 /* lc here must be the regular lc, reset and ready for init */
448 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
456 ldap_back_freeconn( op, lc );
457 rs->sr_err = slap_map_api2result( rs );
462 rc = ldap_back_op_result( lc, op, rs, msgid, sendok );
463 if ( rc == LDAP_SUCCESS ) {
470 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
475 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
477 return ldap_back_dobind_int( lc, op, rs, sendok, 1 );
483 * This is a callback used for chasing referrals using the same
484 * credentials as the original user on this session.
487 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
488 ber_int_t msgid, void *params )
490 struct ldapconn *lc = (struct ldapconn *)params;
492 /* FIXME: add checks on the URL/identity? */
494 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
495 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
504 ldap_back_send_t sendok )
507 LDAPMessage *res = NULL;
510 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
513 rs->sr_matched = NULL;
515 /* if the error recorded in the reply corresponds
516 * to a successful state, get the error from the
517 * remote server response */
518 if ( ERR_OK( rs->sr_err ) ) {
520 struct timeval tv = { 0, 0 };
523 /* if result parsing fails, note the failure reason */
524 switch ( ldap_result( lc->lc_ld, msgid, 1, &tv, &res ) ) {
527 tv.tv_usec = 100000; /* 0.1 s */
528 ldap_pvt_thread_yield();
532 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
537 /* otherwise get the result; if it is not
538 * LDAP_SUCCESS, record it in the reply
539 * structure (this includes
540 * LDAP_COMPARE_{TRUE|FALSE}) */
542 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
543 &match, &text, NULL, NULL, 1 );
545 if ( rc != LDAP_SUCCESS ) {
551 /* if the error in the reply structure is not
552 * LDAP_SUCCESS, try to map it from client
554 if ( !ERR_OK( rs->sr_err ) ) {
555 rs->sr_err = slap_map_api2result( rs );
557 /* internal ops ( op->o_conn == NULL )
558 * must not reply to client */
559 if ( op->o_conn && !op->o_do_not_cache && match ) {
561 /* record the (massaged) matched
562 * DN into the reply structure */
563 rs->sr_matched = match;
567 ( ( sendok & LDAP_BACK_SENDOK )
568 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
570 send_ldap_result( op, rs );
573 if ( rs->sr_matched != match ) {
574 free( (char *)rs->sr_matched );
576 rs->sr_matched = NULL;
577 ldap_memfree( match );
580 ldap_memfree( text );
583 return( ERR_OK( rs->sr_err ) ? 0 : -1 );
586 /* return true if bound, false if failed */
588 ldap_back_retry( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
590 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
591 ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
595 /* lc here must be the regular lc, reset and ready for init */
596 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
600 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
601 return ldap_back_dobind_int( lc, op, rs, sendok, 0 );
605 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs )
607 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
608 struct berval binddn = slap_empty_bv;
609 struct berval bindcred = slap_empty_bv;
615 * FIXME: we need to let clients use proxyAuthz
616 * otherwise we cannot do symmetric pools of servers;
617 * we have to live with the fact that a user can
618 * authorize itself as any ID that is allowed
619 * by the authzTo directive of the "proxyauthzdn".
622 * NOTE: current Proxy Authorization specification
623 * and implementation do not allow proxy authorization
624 * control to be provided with Bind requests
627 * if no bind took place yet, but the connection is bound
628 * and the "proxyauthzdn" is set, then bind as
629 * "proxyauthzdn" and explicitly add the proxyAuthz
630 * control to every operation with the dn bound
631 * to the connection as control value.
634 /* bind as proxyauthzdn only if no idassert mode
635 * is requested, or if the client's identity
637 switch ( li->idassert_mode ) {
638 case LDAP_BACK_IDASSERT_LEGACY:
639 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) && !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
640 if ( !BER_BVISNULL( &li->idassert_authcDN ) && !BER_BVISEMPTY( &li->idassert_authcDN ) )
642 binddn = li->idassert_authcDN;
643 bindcred = li->idassert_passwd;
650 if ( li->idassert_authz ) {
651 struct berval authcDN;
653 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
654 authcDN = slap_empty_bv;
656 authcDN = op->o_conn->c_ndn;
658 rs->sr_err = slap_sasl_matches( op, li->idassert_authz,
659 &authcDN, &authcDN );
660 if ( rs->sr_err != LDAP_SUCCESS ) {
661 send_ldap_result( op, rs );
667 binddn = li->idassert_authcDN;
668 bindcred = li->idassert_passwd;
673 if ( dobind && li->idassert_authmethod == LDAP_AUTH_SASL ) {
674 #ifdef HAVE_CYRUS_SASL
675 void *defaults = NULL;
676 struct berval authzID = BER_BVNULL;
679 /* if SASL supports native authz, prepare for it */
680 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
681 ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
683 switch ( li->idassert_mode ) {
684 case LDAP_BACK_IDASSERT_OTHERID:
685 case LDAP_BACK_IDASSERT_OTHERDN:
686 authzID = li->idassert_authzID;
689 case LDAP_BACK_IDASSERT_ANONYMOUS:
690 BER_BVSTR( &authzID, "dn:" );
693 case LDAP_BACK_IDASSERT_SELF:
694 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
695 /* connection is not authc'd, so don't idassert */
696 BER_BVSTR( &authzID, "dn:" );
699 authzID.bv_len = STRLENOF( "dn:" ) + op->o_conn->c_ndn.bv_len;
700 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
701 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
702 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
703 op->o_conn->c_ndn.bv_val, op->o_conn->c_ndn.bv_len + 1 );
712 #if 0 /* will deal with this later... */
713 if ( sasl_secprops != NULL ) {
714 rs->sr_err = ldap_set_option( lc->lc_ld, LDAP_OPT_X_SASL_SECPROPS,
715 (void *) sasl_secprops );
717 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
718 send_ldap_result( op, rs );
725 defaults = lutil_sasl_defaults( lc->lc_ld,
726 li->idassert_sasl_mech.bv_val,
727 li->idassert_sasl_realm.bv_val,
728 li->idassert_authcID.bv_val,
729 li->idassert_passwd.bv_val,
732 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
733 li->idassert_sasl_mech.bv_val, NULL, NULL,
734 li->idassert_sasl_flags, lutil_sasl_interact,
737 lutil_sasl_freedefs( defaults );
739 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
742 rs->sr_err = slap_map_api2result( rs );
743 if ( rs->sr_err != LDAP_SUCCESS ) {
745 send_ldap_result( op, rs );
751 #endif /* HAVE_CYRUS_SASL */
754 switch ( li->idassert_authmethod ) {
755 case LDAP_AUTH_SIMPLE:
756 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
757 binddn.bv_val, LDAP_SASL_SIMPLE,
758 &bindcred, NULL, NULL, &msgid );
768 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
769 send_ldap_result( op, rs );
773 rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_SENDERR );
774 if ( rc == LDAP_SUCCESS ) {
782 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
783 * to existing server-side controls if required; if not,
784 * the existing server-side controls are placed in *pctrls.
785 * The caller, after using the controls in client API
786 * operations, if ( *pctrls != op->o_ctrls ), should
787 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
788 * The function returns success if the control could
789 * be added if required, or if it did nothing; in the future,
790 * it might return some error if it failed.
792 * if no bind took place yet, but the connection is bound
793 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
794 * and explicitly add proxyAuthz the control to every operation
795 * with the dn bound to the connection as control value.
797 * If no server-side controls are defined for the operation,
798 * simply add the proxyAuthz control; otherwise, if the
799 * proxyAuthz control is not already set, add it as
800 * the first one (FIXME: is controls order significant
804 ldap_back_proxy_authz_ctrl(
808 LDAPControl ***pctrls )
810 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
811 LDAPControl **ctrls = NULL;
814 struct berval assertedID;
818 rs->sr_err = LDAP_SUCCESS;
820 if ( ( BER_BVISNULL( &li->idassert_authcID ) || BER_BVISEMPTY( &li->idassert_authcID ) )
821 && ( BER_BVISNULL( &li->idassert_authcDN ) || BER_BVISEMPTY( &li->idassert_authcDN ) ) ) {
829 if ( li->idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
830 if ( op->o_proxy_authz ) {
832 * FIXME: we do not want to perform proxyAuthz
833 * on behalf of the client, because this would
834 * be performed with "proxyauthzdn" privileges.
836 * This might actually be too strict, since
837 * the "proxyauthzdn" authzTo, and each entry's
838 * authzFrom attributes may be crafted
839 * to avoid unwanted proxyAuthz to take place.
842 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
843 rs->sr_text = "proxyAuthz not allowed within namingContext";
848 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
852 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
856 if ( BER_BVISNULL( &li->idassert_authcDN ) ) {
860 } else if ( li->idassert_authmethod == LDAP_AUTH_SASL ) {
861 if ( ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
862 /* && ( !BER_BVISNULL( &op->o_conn->c_ndn ) || lc->lc_bound ) */ )
864 /* already asserted in SASL via native authz */
865 /* NOTE: the test on lc->lc_bound is used to trap
866 * native authorization of anonymous users,
867 * since in that case op->o_conn->c_ndn is NULL */
871 } else if ( li->idassert_authz ) {
873 struct berval authcDN;
875 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
876 authcDN = slap_empty_bv;
878 authcDN = op->o_conn->c_ndn;
880 rc = slap_sasl_matches( op, li->idassert_authz,
881 &authcDN, & authcDN );
882 if ( rc != LDAP_SUCCESS ) {
883 /* op->o_conn->c_ndn is not authorized
889 if ( op->o_proxy_authz ) {
892 * 1) ignore the already set proxyAuthz control
893 * 2) leave it in place, and don't set ours
895 * 4) reject the operation
897 * option (4) is very drastic
898 * option (3) will make the remote server reject
899 * the operation, thus being equivalent to (4)
900 * option (2) will likely break the idassert
901 * assumptions, so we cannot accept it;
902 * option (1) means that we are contradicting
903 * the client's reques.
905 * I think (4) is the only correct choice.
907 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
908 rs->sr_text = "proxyAuthz not allowed within namingContext";
911 if ( op->o_do_not_cache && op->o_is_auth_check ) {
912 mode = LDAP_BACK_IDASSERT_NOASSERT;
915 mode = li->idassert_mode;
919 case LDAP_BACK_IDASSERT_LEGACY:
920 case LDAP_BACK_IDASSERT_SELF:
921 /* original behavior:
922 * assert the client's identity */
923 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
924 assertedID = slap_empty_bv;
926 assertedID = op->o_conn->c_ndn;
930 case LDAP_BACK_IDASSERT_ANONYMOUS:
931 /* assert "anonymous" */
932 assertedID = slap_empty_bv;
935 case LDAP_BACK_IDASSERT_NOASSERT:
936 /* don't assert; bind as proxyauthzdn */
939 case LDAP_BACK_IDASSERT_OTHERID:
940 case LDAP_BACK_IDASSERT_OTHERDN:
941 /* assert idassert DN */
942 assertedID = li->idassert_authzID;
949 if ( BER_BVISNULL( &assertedID ) ) {
950 assertedID = slap_empty_bv;
954 for ( i = 0; op->o_ctrls[ i ]; i++ )
955 /* just count ctrls */ ;
958 ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
959 ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
961 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
962 ctrls[ 0 ]->ldctl_iscritical = 1;
964 switch ( li->idassert_mode ) {
965 /* already in u:ID or dn:DN form */
966 case LDAP_BACK_IDASSERT_OTHERID:
967 case LDAP_BACK_IDASSERT_OTHERDN:
968 ber_dupbv( &ctrls[ 0 ]->ldctl_value, &assertedID );
971 /* needs the dn: prefix */
973 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
974 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
975 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
976 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val + STRLENOF( "dn:" ),
977 assertedID.bv_val, assertedID.bv_len + 1 );
982 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
983 ctrls[ i + 1 ] = op->o_ctrls[ i ];
986 ctrls[ i + 1 ] = NULL;
989 if ( ctrls == NULL ) {
999 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1001 LDAPControl **ctrls = *pctrls;
1003 /* we assume that the first control is the proxyAuthz
1004 * added by back-ldap, so it's the only one we explicitly
1006 if ( ctrls && ctrls != op->o_ctrls ) {
1007 assert( ctrls[ 0 ] );
1009 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1010 free( ctrls[ 0 ]->ldctl_value.bv_val );