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 /* Set LDAP version. This will always succeed: If the client
245 * bound with a particular version, then so can we.
247 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
248 (const void *)&vers );
250 /* FIXME: configurable? */
251 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
253 if ( ( li->flags & LDAP_BACK_F_USE_TLS )
254 && !ldap_is_ldaps_url( li->url )
255 && ( rs->sr_err = ldap_start_tls_s( ld, NULL, NULL ) ) != LDAP_SUCCESS )
257 /* if StartTLS is requested, only attempt it if the URL
258 * is not "ldaps://"; this may occur not only in case
259 * of misconfiguration, but also when used in the chain
260 * overlay, where the "uri" can be parsed out of a referral */
261 if ( rs->sr_err == LDAP_SERVER_DOWN
262 || ( li->flags & LDAP_BACK_F_TLS_CRITICAL ) )
264 ldap_unbind_ext_s( ld, NULL, NULL );
269 if ( *lcp == NULL ) {
270 *lcp = (struct ldapconn *)ch_malloc( sizeof( struct ldapconn ) );
271 memset( *lcp, 0, sizeof( struct ldapconn ) );
276 if ( rs->sr_err != LDAP_SUCCESS ) {
277 rs->sr_err = slap_map_api2result( rs );
278 if ( sendok & LDAP_BACK_SENDERR ) {
279 if ( rs->sr_text == NULL ) {
280 rs->sr_text = "ldap_initialize() failed";
282 send_ldap_result( op, rs );
291 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
293 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
294 struct ldapconn *lc, lc_curr;
297 /* Searches for a ldapconn in the avl tree */
299 /* Explicit binds must not be shared */
300 if ( op->o_tag == LDAP_REQ_BIND
302 && op->o_conn->c_authz_backend
303 && op->o_bd->be_private == op->o_conn->c_authz_backend->be_private ) )
305 lc_curr.lc_conn = op->o_conn;
308 lc_curr.lc_conn = NULL;
311 /* Internal searches are privileged and shared. So is root. */
312 if ( op->o_do_not_cache || be_isroot( op ) ) {
313 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
314 lc_curr.lc_conn = NULL;
318 lc_curr.lc_local_ndn = op->o_ndn;
321 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
322 lc = (struct ldapconn *)avl_find( li->conntree,
323 (caddr_t)&lc_curr, ldap_back_conn_cmp );
324 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
326 /* Looks like we didn't get a bind. Open a new session... */
328 /* lc here must be NULL */
329 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
333 lc->lc_conn = lc_curr.lc_conn;
334 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
336 ldap_pvt_thread_mutex_init( &lc->lc_mutex );
339 ber_dupbv( &lc->lc_cred, &li->acl_passwd );
340 ber_dupbv( &lc->lc_bound_ndn, &li->acl_authcDN );
343 BER_BVZERO( &lc->lc_cred );
344 BER_BVZERO( &lc->lc_bound_ndn );
345 if ( op->o_conn && !BER_BVISEMPTY( &op->o_ndn )
346 && op->o_bd == op->o_conn->c_authz_backend )
348 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
354 /* Inserts the newly created ldapconn in the avl tree */
355 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
356 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
357 ldap_back_conn_cmp, ldap_back_conn_dup );
359 #if PRINT_CONNTREE > 0
360 myprint( li->conntree );
361 #endif /* PRINT_CONNTREE */
363 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
365 Debug( LDAP_DEBUG_TRACE,
366 "=>ldap_back_getconn: conn %p inserted\n", (void *) lc, 0, 0 );
368 /* Err could be -1 in case a duplicate ldapconn is inserted */
369 if ( rs->sr_err != 0 ) {
370 ldap_back_conn_free( lc );
371 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
372 send_ldap_error( op, rs, LDAP_OTHER,
373 "internal server error" );
378 Debug( LDAP_DEBUG_TRACE,
379 "=>ldap_back_getconn: conn %p fetched\n", (void *) lc, 0, 0 );
388 * Note: as the check for the value of lc->lc_bound was already here, I removed
389 * it from all the callers, and I made the function return the flag, so
390 * it can be used to simplify the check.
393 ldap_back_dobind_int(
397 ldap_back_send_t sendok,
403 assert( retries >= 0 );
405 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
406 if ( !lc->lc_bound ) {
408 * FIXME: we need to let clients use proxyAuthz
409 * otherwise we cannot do symmetric pools of servers;
410 * we have to live with the fact that a user can
411 * authorize itself as any ID that is allowed
412 * by the authzTo directive of the "proxyauthzdn".
415 * NOTE: current Proxy Authorization specification
416 * and implementation do not allow proxy authorization
417 * control to be provided with Bind requests
420 * if no bind took place yet, but the connection is bound
421 * and the "idassert-authcDN" (or other ID) is set,
422 * then bind as the asserting ideintity and explicitly
423 * add the proxyAuthz control to every operation with the
424 * dn bound to the connection as control value.
426 if ( op->o_conn != NULL && BER_BVISNULL( &lc->lc_bound_ndn ) ) {
427 (void)ldap_back_proxy_authz_bind( lc, op, rs );
432 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
433 lc->lc_bound_ndn.bv_val,
434 LDAP_SASL_SIMPLE, &lc->lc_cred,
435 NULL, NULL, &msgid );
437 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
439 ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
442 /* lc here must be the regular lc, reset and ready for init */
443 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
451 ldap_back_freeconn( op, lc );
452 rs->sr_err = slap_map_api2result( rs );
457 rc = ldap_back_op_result( lc, op, rs, msgid, sendok );
458 if ( rc == LDAP_SUCCESS ) {
465 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
470 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
472 return ldap_back_dobind_int( lc, op, rs, sendok, 1 );
478 * This is a callback used for chasing referrals using the same
479 * credentials as the original user on this session.
482 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
483 ber_int_t msgid, void *params )
485 struct ldapconn *lc = (struct ldapconn *)params;
487 /* FIXME: add checks on the URL/identity? */
489 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
490 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
499 ldap_back_send_t sendok )
502 LDAPMessage *res = NULL;
505 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
508 rs->sr_matched = NULL;
510 /* if the error recorded in the reply corresponds
511 * to a successful state, get the error from the
512 * remote server response */
513 if ( ERR_OK( rs->sr_err ) ) {
515 struct timeval tv = { 0, 0 };
518 /* if result parsing fails, note the failure reason */
519 switch ( ldap_result( lc->lc_ld, msgid, 1, &tv, &res ) ) {
522 tv.tv_usec = 100000; /* 0.1 s */
523 ldap_pvt_thread_yield();
527 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
532 /* otherwise get the result; if it is not
533 * LDAP_SUCCESS, record it in the reply
534 * structure (this includes
535 * LDAP_COMPARE_{TRUE|FALSE}) */
537 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
538 &match, &text, NULL, NULL, 1 );
540 if ( rc != LDAP_SUCCESS ) {
546 /* if the error in the reply structure is not
547 * LDAP_SUCCESS, try to map it from client
549 if ( !ERR_OK( rs->sr_err ) ) {
550 rs->sr_err = slap_map_api2result( rs );
552 /* internal ops ( op->o_conn == NULL )
553 * must not reply to client */
554 if ( op->o_conn && !op->o_do_not_cache && match ) {
556 /* record the (massaged) matched
557 * DN into the reply structure */
558 rs->sr_matched = match;
562 ( ( sendok & LDAP_BACK_SENDOK )
563 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
565 send_ldap_result( op, rs );
568 if ( rs->sr_matched != match ) {
569 free( (char *)rs->sr_matched );
571 rs->sr_matched = NULL;
572 ldap_memfree( match );
575 ldap_memfree( text );
578 return( ERR_OK( rs->sr_err ) ? 0 : -1 );
581 /* return true if bound, false if failed */
583 ldap_back_retry( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
585 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
586 ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
590 /* lc here must be the regular lc, reset and ready for init */
591 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
595 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
596 return ldap_back_dobind_int( lc, op, rs, sendok, 0 );
600 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs )
602 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
603 struct berval binddn = slap_empty_bv;
604 struct berval bindcred = slap_empty_bv;
610 * FIXME: we need to let clients use proxyAuthz
611 * otherwise we cannot do symmetric pools of servers;
612 * we have to live with the fact that a user can
613 * authorize itself as any ID that is allowed
614 * by the authzTo directive of the "proxyauthzdn".
617 * NOTE: current Proxy Authorization specification
618 * and implementation do not allow proxy authorization
619 * control to be provided with Bind requests
622 * if no bind took place yet, but the connection is bound
623 * and the "proxyauthzdn" is set, then bind as
624 * "proxyauthzdn" and explicitly add the proxyAuthz
625 * control to every operation with the dn bound
626 * to the connection as control value.
629 /* bind as proxyauthzdn only if no idassert mode
630 * is requested, or if the client's identity
632 switch ( li->idassert_mode ) {
633 case LDAP_BACK_IDASSERT_LEGACY:
634 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) && !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
635 if ( !BER_BVISNULL( &li->idassert_authcDN ) && !BER_BVISEMPTY( &li->idassert_authcDN ) )
637 binddn = li->idassert_authcDN;
638 bindcred = li->idassert_passwd;
645 if ( li->idassert_authz ) {
646 struct berval authcDN;
648 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
649 authcDN = slap_empty_bv;
651 authcDN = op->o_conn->c_ndn;
653 rs->sr_err = slap_sasl_matches( op, li->idassert_authz,
654 &authcDN, &authcDN );
655 if ( rs->sr_err != LDAP_SUCCESS ) {
656 send_ldap_result( op, rs );
662 binddn = li->idassert_authcDN;
663 bindcred = li->idassert_passwd;
668 if ( dobind && li->idassert_authmethod == LDAP_AUTH_SASL ) {
669 #ifdef HAVE_CYRUS_SASL
670 void *defaults = NULL;
671 struct berval authzID = BER_BVNULL;
674 /* if SASL supports native authz, prepare for it */
675 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
676 ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
678 switch ( li->idassert_mode ) {
679 case LDAP_BACK_IDASSERT_OTHERID:
680 case LDAP_BACK_IDASSERT_OTHERDN:
681 authzID = li->idassert_authzID;
684 case LDAP_BACK_IDASSERT_ANONYMOUS:
685 BER_BVSTR( &authzID, "dn:" );
688 case LDAP_BACK_IDASSERT_SELF:
689 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
690 /* connection is not authc'd, so don't idassert */
691 BER_BVSTR( &authzID, "dn:" );
694 authzID.bv_len = STRLENOF( "dn:" ) + op->o_conn->c_ndn.bv_len;
695 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
696 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
697 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
698 op->o_conn->c_ndn.bv_val, op->o_conn->c_ndn.bv_len + 1 );
707 #if 0 /* will deal with this later... */
708 if ( sasl_secprops != NULL ) {
709 rs->sr_err = ldap_set_option( lc->lc_ld, LDAP_OPT_X_SASL_SECPROPS,
710 (void *) sasl_secprops );
712 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
713 send_ldap_result( op, rs );
720 defaults = lutil_sasl_defaults( lc->lc_ld,
721 li->idassert_sasl_mech.bv_val,
722 li->idassert_sasl_realm.bv_val,
723 li->idassert_authcID.bv_val,
724 li->idassert_passwd.bv_val,
727 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
728 li->idassert_sasl_mech.bv_val, NULL, NULL,
729 li->idassert_sasl_flags, lutil_sasl_interact,
732 lutil_sasl_freedefs( defaults );
734 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
737 rs->sr_err = slap_map_api2result( rs );
738 if ( rs->sr_err != LDAP_SUCCESS ) {
740 send_ldap_result( op, rs );
746 #endif /* HAVE_CYRUS_SASL */
749 switch ( li->idassert_authmethod ) {
750 case LDAP_AUTH_SIMPLE:
751 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
752 binddn.bv_val, LDAP_SASL_SIMPLE,
753 &bindcred, NULL, NULL, &msgid );
763 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
764 send_ldap_result( op, rs );
768 rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_SENDERR );
769 if ( rc == LDAP_SUCCESS ) {
777 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
778 * to existing server-side controls if required; if not,
779 * the existing server-side controls are placed in *pctrls.
780 * The caller, after using the controls in client API
781 * operations, if ( *pctrls != op->o_ctrls ), should
782 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
783 * The function returns success if the control could
784 * be added if required, or if it did nothing; in the future,
785 * it might return some error if it failed.
787 * if no bind took place yet, but the connection is bound
788 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
789 * and explicitly add proxyAuthz the control to every operation
790 * with the dn bound to the connection as control value.
792 * If no server-side controls are defined for the operation,
793 * simply add the proxyAuthz control; otherwise, if the
794 * proxyAuthz control is not already set, add it as
795 * the first one (FIXME: is controls order significant
799 ldap_back_proxy_authz_ctrl(
803 LDAPControl ***pctrls )
805 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
806 LDAPControl **ctrls = NULL;
809 struct berval assertedID;
813 rs->sr_err = LDAP_SUCCESS;
815 if ( ( BER_BVISNULL( &li->idassert_authcID ) || BER_BVISEMPTY( &li->idassert_authcID ) )
816 && ( BER_BVISNULL( &li->idassert_authcDN ) || BER_BVISEMPTY( &li->idassert_authcDN ) ) ) {
824 if ( li->idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
825 if ( op->o_proxy_authz ) {
827 * FIXME: we do not want to perform proxyAuthz
828 * on behalf of the client, because this would
829 * be performed with "proxyauthzdn" privileges.
831 * This might actually be too strict, since
832 * the "proxyauthzdn" authzTo, and each entry's
833 * authzFrom attributes may be crafted
834 * to avoid unwanted proxyAuthz to take place.
837 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
838 rs->sr_text = "proxyAuthz not allowed within namingContext";
843 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
847 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
851 if ( BER_BVISNULL( &li->idassert_authcDN ) ) {
855 } else if ( li->idassert_authmethod == LDAP_AUTH_SASL ) {
856 if ( ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
857 /* && ( !BER_BVISNULL( &op->o_conn->c_ndn ) || lc->lc_bound ) */ )
859 /* already asserted in SASL via native authz */
860 /* NOTE: the test on lc->lc_bound is used to trap
861 * native authorization of anonymous users,
862 * since in that case op->o_conn->c_ndn is NULL */
866 } else if ( li->idassert_authz ) {
868 struct berval authcDN;
870 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
871 authcDN = slap_empty_bv;
873 authcDN = op->o_conn->c_ndn;
875 rc = slap_sasl_matches( op, li->idassert_authz,
876 &authcDN, & authcDN );
877 if ( rc != LDAP_SUCCESS ) {
878 /* op->o_conn->c_ndn is not authorized
884 if ( op->o_proxy_authz ) {
887 * 1) ignore the already set proxyAuthz control
888 * 2) leave it in place, and don't set ours
890 * 4) reject the operation
892 * option (4) is very drastic
893 * option (3) will make the remote server reject
894 * the operation, thus being equivalent to (4)
895 * option (2) will likely break the idassert
896 * assumptions, so we cannot accept it;
897 * option (1) means that we are contradicting
898 * the client's reques.
900 * I think (4) is the only correct choice.
902 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
903 rs->sr_text = "proxyAuthz not allowed within namingContext";
906 if ( op->o_do_not_cache && op->o_is_auth_check ) {
907 mode = LDAP_BACK_IDASSERT_NOASSERT;
910 mode = li->idassert_mode;
914 case LDAP_BACK_IDASSERT_LEGACY:
915 case LDAP_BACK_IDASSERT_SELF:
916 /* original behavior:
917 * assert the client's identity */
918 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
919 assertedID = slap_empty_bv;
921 assertedID = op->o_conn->c_ndn;
925 case LDAP_BACK_IDASSERT_ANONYMOUS:
926 /* assert "anonymous" */
927 assertedID = slap_empty_bv;
930 case LDAP_BACK_IDASSERT_NOASSERT:
931 /* don't assert; bind as proxyauthzdn */
934 case LDAP_BACK_IDASSERT_OTHERID:
935 case LDAP_BACK_IDASSERT_OTHERDN:
936 /* assert idassert DN */
937 assertedID = li->idassert_authzID;
944 if ( BER_BVISNULL( &assertedID ) ) {
945 assertedID = slap_empty_bv;
949 for ( i = 0; op->o_ctrls[ i ]; i++ )
950 /* just count ctrls */ ;
953 ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
954 ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
956 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
957 ctrls[ 0 ]->ldctl_iscritical = 1;
959 switch ( li->idassert_mode ) {
960 /* already in u:ID or dn:DN form */
961 case LDAP_BACK_IDASSERT_OTHERID:
962 case LDAP_BACK_IDASSERT_OTHERDN:
963 ber_dupbv( &ctrls[ 0 ]->ldctl_value, &assertedID );
966 /* needs the dn: prefix */
968 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
969 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
970 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
971 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val + STRLENOF( "dn:" ),
972 assertedID.bv_val, assertedID.bv_len + 1 );
977 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
978 ctrls[ i + 1 ] = op->o_ctrls[ i ];
981 ctrls[ i + 1 ] = NULL;
984 if ( ctrls == NULL ) {
994 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
996 LDAPControl **ctrls = *pctrls;
998 /* we assume that the first control is the proxyAuthz
999 * added by back-ldap, so it's the only one we explicitly
1001 if ( ctrls && ctrls != op->o_ctrls ) {
1002 assert( ctrls[ 0 ] );
1004 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1005 free( ctrls[ 0 ]->ldctl_value.bv_val );