1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2006 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 #ifndef PRINT_CONNTREE
39 #define PRINT_CONNTREE 0
40 #endif /* !PRINT_CONNTREE */
42 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
45 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
48 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
51 ldap_back_conndnlc_cmp( const void *c1, const void *c2 );
54 ldap_back_bind( Operation *op, SlapReply *rs )
56 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
62 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR );
67 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
68 ch_free( lc->lc_bound_ndn.bv_val );
69 BER_BVZERO( &lc->lc_bound_ndn );
71 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
73 /* method is always LDAP_AUTH_SIMPLE if we got here */
74 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
76 &op->orb_cred, op->o_ctrls, NULL, &msgid );
77 rc = ldap_back_op_result( lc, op, rs, msgid, 0, LDAP_BACK_SENDERR );
79 if ( rc == LDAP_SUCCESS ) {
80 /* If defined, proxyAuthz will be used also when
81 * back-ldap is the authorizing backend; for this
82 * purpose, a successful bind is followed by a
83 * bind with the configured identity assertion */
84 /* NOTE: use with care */
85 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
86 ldap_back_proxy_authz_bind( lc, op, rs, LDAP_BACK_SENDERR );
87 if ( !LDAP_BACK_CONN_ISBOUND( lc ) ) {
93 LDAP_BACK_CONN_ISBOUND_SET( lc );
94 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
96 if ( LDAP_BACK_SAVECRED( li ) ) {
97 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
98 memset( lc->lc_cred.bv_val, 0,
101 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
102 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
107 assert( lc->lc_binding == 1 );
110 /* must re-insert if local DN changed as result of bind */
111 if ( !LDAP_BACK_CONN_ISBOUND( lc )
112 || ( LDAP_BACK_CONN_ISBOUND( lc )
113 && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) ) )
118 /* wait for all other ops to release the connection */
120 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
121 if ( lc->lc_refcnt > 1 ) {
122 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
123 ldap_pvt_thread_yield();
127 assert( lc->lc_refcnt == 1 );
128 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
129 ldap_back_conndnlc_cmp );
130 assert( tmplc == NULL || lc == tmplc );
132 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
133 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
134 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
135 ldap_back_conndn_cmp, ldap_back_conndn_dup );
138 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
144 /* duplicate; someone else successfully bound
145 * on the same connection with the same identity;
146 * we can do this because lc_refcnt == 1 */
147 ldap_back_conn_free( lc );
153 ldap_back_release_conn( op, rs, lc );
160 * ldap_back_conndn_cmp
162 * compares two ldapconn_t based on the value of the conn pointer
163 * and of the local DN; used by avl stuff for insert, lookup
167 ldap_back_conndn_cmp( const void *c1, const void *c2 )
169 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
170 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
173 /* If local DNs don't match, it is definitely not a match */
174 /* For shared sessions, conn is NULL. Only explicitly
175 * bound sessions will have non-NULL conn.
177 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
179 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
186 * ldap_back_conndnlc_cmp
188 * compares two ldapconn_t based on the value of the conn pointer,
189 * the local DN and the lc pointer; used by avl stuff for insert, lookup
193 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
195 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
196 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
199 /* If local DNs don't match, it is definitely not a match */
200 /* For shared sessions, conn is NULL. Only explicitly
201 * bound sessions will have non-NULL conn.
203 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
205 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
207 rc = SLAP_PTRCMP( lc1, lc2 );
217 * compares two ldapconn_t based on the value of the conn pointer;
218 * used by avl stuff for delete of all conns with the same connid
221 ldap_back_conn_cmp( const void *c1, const void *c2 )
223 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
224 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
226 /* For shared sessions, conn is NULL. Only explicitly
227 * bound sessions will have non-NULL conn.
229 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
233 * ldap_back_conndn_dup
235 * returns -1 in case a duplicate ldapconn_t has been inserted;
239 ldap_back_conndn_dup( void *c1, void *c2 )
241 ldapconn_t *lc1 = (ldapconn_t *)c1;
242 ldapconn_t *lc2 = (ldapconn_t *)c2;
244 /* Cannot have more than one shared session with same DN */
245 if ( lc1->lc_conn == lc2->lc_conn &&
246 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
254 #if PRINT_CONNTREE > 0
256 ravl_print( Avlnode *root, int depth )
265 ravl_print( root->avl_right, depth+1 );
267 for ( i = 0; i < depth; i++ ) {
268 fprintf( stderr, "-" );
272 fprintf( stderr, "lc=%p local=\"%s\" conn=%p %s refcnt=%d\n",
273 (void *)lc, lc->lc_local_ndn.bv_val, (void *)lc->lc_conn,
274 avl_bf2str( root->avl_bf ), lc->lc_refcnt );
276 ravl_print( root->avl_left, depth+1 );
280 myprint( Avlnode *root )
282 fprintf( stderr, "========>\n" );
285 fprintf( stderr, "\tNULL\n" );
288 ravl_print( root, 0 );
291 fprintf( stderr, "<========\n" );
293 #endif /* PRINT_CONNTREE */
296 ldap_back_freeconn( Operation *op, ldapconn_t *lc, int dolock )
298 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
302 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
305 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
306 ldap_back_conndnlc_cmp );
307 assert( LDAP_BACK_CONN_TAINTED( lc ) || tmplc == lc );
308 if ( lc->lc_refcnt == 0 ) {
309 ldap_back_conn_free( (void *)lc );
313 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
330 int rc = LDAP_SUCCESS;
333 /* this is ridiculous... */
334 dummy.li_flags = flags;
336 /* start TLS ("tls-[try-]{start,propagate}" statements) */
337 if ( ( LDAP_BACK_USE_TLS( &dummy ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS( &dummy ) ) )
338 && !ldap_is_ldaps_url( url ) )
340 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
342 * use asynchronous StartTLS
343 * in case, chase referral (not implemented yet)
347 if ( protocol == 0 ) {
348 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
352 if ( protocol < LDAP_VERSION3 ) {
353 /* we should rather bail out... */
354 rc = LDAP_UNWILLING_TO_PERFORM;
355 *text = "invalid protocol version";
358 if ( rc == LDAP_SUCCESS ) {
359 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
362 if ( rc == LDAP_SUCCESS ) {
363 LDAPMessage *res = NULL;
366 LDAP_BACK_TV_SET( &tv );
369 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
371 rc = LDAP_UNAVAILABLE;
373 } else if ( rc == 0 ) {
374 if ( retries != LDAP_BACK_RETRY_NEVER ) {
375 ldap_pvt_thread_yield();
379 LDAP_BACK_TV_SET( &tv );
382 rc = LDAP_UNAVAILABLE;
384 } else if ( rc == LDAP_RES_EXTENDED ) {
385 struct berval *data = NULL;
387 rc = ldap_parse_extended_result( ld, res,
389 if ( rc == LDAP_SUCCESS ) {
391 rc = ldap_parse_result( ld, res, &err,
392 NULL, NULL, NULL, NULL, 1 );
393 if ( rc == LDAP_SUCCESS ) {
398 /* FIXME: in case a referral
399 * is returned, should we try
400 * using it instead of the
402 if ( rc == LDAP_SUCCESS ) {
403 rc = ldap_install_tls( ld );
405 } else if ( rc == LDAP_REFERRAL ) {
406 rc = LDAP_UNWILLING_TO_PERFORM;
407 *text = "unwilling to chase referral returned by Start TLS exop";
411 if ( data->bv_val ) {
412 ber_memfree( data->bv_val );
426 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
428 * use synchronous StartTLS
430 rc = ldap_start_tls_s( ld, NULL, NULL );
431 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
433 /* if StartTLS is requested, only attempt it if the URL
434 * is not "ldaps://"; this may occur not only in case
435 * of misconfiguration, but also when used in the chain
436 * overlay, where the "uri" can be parsed out of a referral */
442 case LDAP_SERVER_DOWN:
446 if ( LDAP_BACK_TLS_CRITICAL( &dummy ) ) {
447 *text = "could not start TLS";
451 /* in case Start TLS is not critical */
463 #endif /* HAVE_TLS */
466 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
468 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
472 int is_tls = op->o_conn->c_is_tls;
473 #endif /* HAVE_TLS */
475 assert( lcp != NULL );
477 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
478 rs->sr_err = ldap_initialize( &ld, li->li_uri );
479 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
480 if ( rs->sr_err != LDAP_SUCCESS ) {
484 if ( li->li_urllist_f ) {
485 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
488 /* Set LDAP version. This will always succeed: If the client
489 * bound with a particular version, then so can we.
491 if ( li->li_version != 0 ) {
492 version = li->li_version;
494 } else if ( op->o_protocol != 0 ) {
495 version = op->o_protocol;
498 /* assume it's an internal op; set to LDAPv3 */
499 version = LDAP_VERSION3;
501 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
503 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
504 ldap_set_option( ld, LDAP_OPT_REFERRALS,
505 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
507 if ( li->li_network_timeout > 0 ) {
510 tv.tv_sec = li->li_network_timeout;
512 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
516 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
517 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
518 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
519 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
520 if ( rs->sr_err != LDAP_SUCCESS ) {
521 ldap_unbind_ext( ld, NULL, NULL );
524 #endif /* HAVE_TLS */
526 if ( *lcp == NULL ) {
527 *lcp = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
528 (*lcp)->lc_flags= li->li_flags;
531 (*lcp)->lc_refcnt = 1;
532 (*lcp)->lc_binding = 1;
535 LDAP_BACK_CONN_ISTLS_SET( *lcp );
537 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
539 #endif /* HAVE_TLS */
542 if ( rs->sr_err != LDAP_SUCCESS ) {
543 rs->sr_err = slap_map_api2result( rs );
544 if ( sendok & LDAP_BACK_SENDERR ) {
545 if ( rs->sr_text == NULL ) {
546 rs->sr_text = "ldap_initialize() failed";
548 send_ldap_result( op, rs );
553 if ( li->li_conn_ttl > 0 ) {
554 (*lcp)->lc_create_time = op->o_time;
562 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
564 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
565 ldapconn_t *lc = NULL,
567 int refcnt = 1, binding = 1;
569 /* Internal searches are privileged and shared. So is root. */
570 if ( op->o_do_not_cache || be_isroot( op ) ) {
571 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
572 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
573 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
576 lc_curr.lc_local_ndn = op->o_ndn;
577 /* Explicit binds must not be shared */
578 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
579 lc_curr.lc_conn = op->o_conn;
582 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
586 /* Explicit Bind requests always get their own conn */
587 if ( !( sendok & LDAP_BACK_BINDING ) ) {
588 /* Searches for a ldapconn in the avl tree */
590 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
592 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
593 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
595 /* Don't reuse connections while they're still binding */
596 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
597 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
598 ldap_pvt_thread_yield();
601 refcnt = ++lc->lc_refcnt;
602 binding = ++lc->lc_binding;
604 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
607 /* Looks like we didn't get a bind. Open a new session... */
609 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
612 if ( sendok & LDAP_BACK_BINDING ) {
613 LDAP_BACK_CONN_BINDING_SET( lc );
615 lc->lc_conn = lc_curr.lc_conn;
616 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
618 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
619 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
620 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
621 LDAP_BACK_CONN_ISPRIV_SET( lc );
624 BER_BVZERO( &lc->lc_cred );
625 BER_BVZERO( &lc->lc_bound_ndn );
627 /* FIXME: if we set lc_bound_ndn = o_ndn
628 * we end up with a bind with DN but no password! */
629 if ( !BER_BVISEMPTY( &op->o_ndn )
630 && SLAP_IS_AUTHZ_BACKEND( op ) )
632 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
638 /* if start TLS failed but it was not mandatory,
639 * check if the non-TLS connection was already
640 * in cache; in case, destroy the newly created
641 * connection and use the existing one */
642 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
643 && !ldap_tls_inplace( lc->lc_ld ) )
647 lc_curr.lc_conn = LDAP_BACK_PCONN;
648 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
649 tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
650 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
651 if ( tmplc != NULL ) {
652 refcnt = ++tmplc->lc_refcnt;
653 binding = ++tmplc->lc_binding;
654 ldap_back_conn_free( lc );
657 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
659 if ( tmplc != NULL ) {
663 #endif /* HAVE_TLS */
665 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
667 /* Inserts the newly created ldapconn in the avl tree */
668 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
670 assert( lc->lc_refcnt == 1 );
671 assert( lc->lc_binding == 1 );
672 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
673 ldap_back_conndn_cmp, ldap_back_conndn_dup );
675 #if PRINT_CONNTREE > 0
676 myprint( li->li_conninfo.lai_tree );
677 #endif /* PRINT_CONNTREE */
679 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
681 Debug( LDAP_DEBUG_TRACE,
682 "=>ldap_back_getconn: conn %p inserted refcnt=%u binding=%u\n",
683 (void *)lc, refcnt, binding );
685 /* Err could be -1 in case a duplicate ldapconn is inserted */
686 switch ( rs->sr_err ) {
691 if ( !( sendok & LDAP_BACK_BINDING ) ) {
692 /* duplicate: free and try to get the newly created one */
695 /* taint connection, so that it'll be freed when released */
696 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
697 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
698 ldap_back_conndnlc_cmp );
699 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
700 LDAP_BACK_CONN_TAINTED_SET( lc );
704 ldap_back_conn_free( lc );
705 rs->sr_err = LDAP_OTHER;
706 rs->sr_text = "proxy bind collision";
707 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
708 send_ldap_result( op, rs );
715 char buf[ SLAP_TEXT_BUFLEN ];
718 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
719 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
723 /* let it be used, but taint/delete it so that
724 * no-one else can look it up any further */
725 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
726 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
727 ldap_back_conndnlc_cmp );
728 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
729 LDAP_BACK_CONN_TAINTED_SET( lc );
732 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
733 snprintf( buf, sizeof( buf ),
734 "conn %p fetched refcnt=%u binding=%u%s",
735 (void *)lc, refcnt, binding, expiring ? " expiring" : "" );
736 Debug( LDAP_DEBUG_TRACE,
737 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
743 #endif /* HAVE_TLS */
744 if ( li->li_idle_timeout && lc ) {
745 lc->lc_time = op->o_time;
752 ldap_back_release_conn_lock(
758 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
761 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
763 assert( lc->lc_refcnt > 0 );
764 LDAP_BACK_CONN_BINDING_CLEAR( lc );
766 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
767 ldap_back_freeconn( op, lc, 0 );
770 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
777 * Note: as the check for the value of lc->lc_bound was already here, I removed
778 * it from all the callers, and I made the function return the flag, so
779 * it can be used to simplify the check.
781 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
784 ldap_back_dobind_int(
788 ldap_back_send_t sendok,
792 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
797 assert( retries >= 0 );
801 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
804 if ( binding == 0 ) {
805 /* check if already bound */
806 rc = LDAP_BACK_CONN_ISBOUND( lc );
810 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
815 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
816 /* if someone else is about to bind it, give up and retry */
818 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
820 ldap_pvt_thread_yield();
824 /* otherwise this thread will bind it */
825 LDAP_BACK_CONN_BINDING_SET( lc );
830 /* wait for pending operations to finish */
831 /* FIXME: may become a bottleneck! */
832 if ( lc->lc_refcnt != lc->lc_binding ) {
834 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
836 ldap_pvt_thread_yield();
841 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
845 while ( lc->lc_refcnt > 1 ) {
846 ldap_pvt_thread_yield();
847 rc = LDAP_BACK_CONN_ISBOUND( lc );
854 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
856 LDAP_BACK_CONN_BINDING_SET( lc );
858 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
863 * FIXME: we need to let clients use proxyAuthz
864 * otherwise we cannot do symmetric pools of servers;
865 * we have to live with the fact that a user can
866 * authorize itself as any ID that is allowed
867 * by the authzTo directive of the "proxyauthzdn".
870 * NOTE: current Proxy Authorization specification
871 * and implementation do not allow proxy authorization
872 * control to be provided with Bind requests
875 * if no bind took place yet, but the connection is bound
876 * and the "idassert-authcDN" (or other ID) is set,
877 * then bind as the asserting identity and explicitly
878 * add the proxyAuthz control to every operation with the
879 * dn bound to the connection as control value.
880 * This is done also if this is the authrizing backend,
881 * but the "override" flag is given to idassert.
882 * It allows to use SASL bind and yet proxyAuthz users
884 if ( op->o_conn != NULL &&
885 !op->o_do_not_cache &&
886 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
887 ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
889 (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok );
893 #ifdef HAVE_CYRUS_SASL
894 if ( LDAP_BACK_CONN_ISPRIV( lc )
895 && li->li_acl_authmethod == LDAP_AUTH_SASL )
897 void *defaults = NULL;
899 if ( li->li_acl_secprops != NULL ) {
900 rc = ldap_set_option( lc->lc_ld,
901 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops);
903 if ( rc != LDAP_OPT_SUCCESS ) {
904 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
905 "(SECPROPS,\"%s\") failed!\n",
906 li->li_acl_secprops, 0, 0 );
911 defaults = lutil_sasl_defaults( lc->lc_ld,
912 li->li_acl_sasl_mech.bv_val,
913 li->li_acl_sasl_realm.bv_val,
914 li->li_acl_authcID.bv_val,
915 li->li_acl_passwd.bv_val,
918 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
919 li->li_acl_authcDN.bv_val,
920 li->li_acl_sasl_mech.bv_val, NULL, NULL,
921 LDAP_SASL_QUIET, lutil_sasl_interact,
924 lutil_sasl_freedefs( defaults );
926 rs->sr_err = slap_map_api2result( rs );
927 if ( rs->sr_err != LDAP_SUCCESS ) {
928 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
929 send_ldap_result( op, rs );
932 LDAP_BACK_CONN_ISBOUND_SET( lc );
936 #endif /* HAVE_CYRUS_SASL */
939 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
940 lc->lc_bound_ndn.bv_val,
941 LDAP_SASL_SIMPLE, &lc->lc_cred,
942 NULL, NULL, &msgid );
944 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
945 if ( retries != LDAP_BACK_RETRY_NEVER ) {
947 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
950 assert( lc->lc_refcnt > 0 );
951 if ( lc->lc_refcnt == 1 ) {
952 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
955 /* lc here must be the regular lc, reset and ready for init */
956 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
957 if ( rs->sr_err != LDAP_SUCCESS ) {
964 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
967 if ( rs->sr_err == LDAP_SUCCESS ) {
976 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
980 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
984 ldap_back_freeconn( op, lc, dolock );
985 rs->sr_err = slap_map_api2result( rs );
990 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
991 if ( rc == LDAP_SUCCESS ) {
992 LDAP_BACK_CONN_ISBOUND_SET( lc );
997 LDAP_BACK_CONN_BINDING_CLEAR( lc );
998 rc = LDAP_BACK_CONN_ISBOUND( lc );
1000 ldap_back_release_conn_lock( op, rs, lc, dolock );
1007 ldap_back_dobind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1009 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1011 return ldap_back_dobind_int( lc, op, rs, sendok, li->li_nretries, 1 );
1015 * ldap_back_default_rebind
1017 * This is a callback used for chasing referrals using the same
1018 * credentials as the original user on this session.
1021 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1022 ber_int_t msgid, void *params )
1024 ldapconn_t *lc = (ldapconn_t *)params;
1027 /* ... otherwise we couldn't get here */
1028 assert( lc != NULL );
1030 if ( !ldap_tls_inplace( ld ) ) {
1031 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1033 const char *text = NULL;
1035 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1036 LDAP_BACK_RETRY_DEFAULT, &text );
1037 if ( rc != LDAP_SUCCESS ) {
1041 #endif /* HAVE_TLS */
1043 /* FIXME: add checks on the URL/identity? */
1045 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
1046 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1050 * ldap_back_default_urllist
1053 ldap_back_default_urllist(
1055 LDAPURLDesc **urllist,
1059 ldapinfo_t *li = (ldapinfo_t *)params;
1060 LDAPURLDesc **urltail;
1062 if ( urllist == url ) {
1063 return LDAP_SUCCESS;
1066 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1069 *urltail = *urllist;
1073 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1075 ch_free( li->li_uri );
1078 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1079 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1081 return LDAP_SUCCESS;
1085 ldap_back_op_result(
1091 ldap_back_send_t sendok )
1094 LDAPMessage *res = NULL;
1097 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1100 rs->sr_matched = NULL;
1102 /* if the error recorded in the reply corresponds
1103 * to a successful state, get the error from the
1104 * remote server response */
1105 if ( ERR_OK( rs->sr_err ) ) {
1110 tv.tv_sec = timeout;
1114 LDAP_BACK_TV_SET( &tv );
1118 /* if result parsing fails, note the failure reason */
1119 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1123 (void)ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1124 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
1125 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
1126 rs->sr_text = "Operation timed out";
1130 LDAP_BACK_TV_SET( &tv );
1131 ldap_pvt_thread_yield();
1135 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1140 /* otherwise get the result; if it is not
1141 * LDAP_SUCCESS, record it in the reply
1142 * structure (this includes
1143 * LDAP_COMPARE_{TRUE|FALSE}) */
1145 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1146 &match, &text, NULL, NULL, 1 );
1148 if ( rc != LDAP_SUCCESS ) {
1154 /* if the error in the reply structure is not
1155 * LDAP_SUCCESS, try to map it from client
1156 * to server error */
1157 if ( !ERR_OK( rs->sr_err ) ) {
1158 rs->sr_err = slap_map_api2result( rs );
1160 /* internal ops ( op->o_conn == NULL )
1161 * must not reply to client */
1162 if ( op->o_conn && !op->o_do_not_cache && match ) {
1164 /* record the (massaged) matched
1165 * DN into the reply structure */
1166 rs->sr_matched = match;
1170 ( ( sendok & LDAP_BACK_SENDOK )
1171 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1173 send_ldap_result( op, rs );
1176 if ( rs->sr_matched != match ) {
1177 free( (char *)rs->sr_matched );
1179 rs->sr_matched = NULL;
1180 ldap_memfree( match );
1183 ldap_memfree( text );
1186 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1189 /* return true if bound, false if failed */
1191 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1194 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1196 assert( lcp != NULL );
1197 assert( *lcp != NULL );
1199 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1201 if ( (*lcp)->lc_refcnt == 1 ) {
1202 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1203 Debug( LDAP_DEBUG_ANY,
1204 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1205 op->o_log_prefix, li->li_uri,
1206 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1207 "" : (*lcp)->lc_bound_ndn.bv_val );
1208 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1210 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1211 (*lcp)->lc_ld = NULL;
1212 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1214 /* lc here must be the regular lc, reset and ready for init */
1215 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1216 if ( rc != LDAP_SUCCESS ) {
1221 rc = ldap_back_dobind_int( *lcp, op, rs, sendok, 0, 0 );
1228 Debug( LDAP_DEBUG_TRACE,
1229 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1230 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1232 ldap_back_release_conn_lock( op, rs, *lcp, 0 );
1236 rs->sr_err = LDAP_UNAVAILABLE;
1237 rs->sr_text = "unable to retry";
1238 send_ldap_result( op, rs );
1242 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1248 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1250 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1251 struct berval binddn = slap_empty_bv;
1252 struct berval bindcred = slap_empty_bv;
1258 /* don't proxyAuthz if protocol is not LDAPv3 */
1259 switch ( li->li_version ) {
1264 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1273 if ( op->o_tag == LDAP_REQ_BIND ) {
1274 ndn = op->o_req_ndn;
1276 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1277 ndn = op->o_conn->c_ndn;
1284 * FIXME: we need to let clients use proxyAuthz
1285 * otherwise we cannot do symmetric pools of servers;
1286 * we have to live with the fact that a user can
1287 * authorize itself as any ID that is allowed
1288 * by the authzTo directive of the "proxyauthzdn".
1291 * NOTE: current Proxy Authorization specification
1292 * and implementation do not allow proxy authorization
1293 * control to be provided with Bind requests
1296 * if no bind took place yet, but the connection is bound
1297 * and the "proxyauthzdn" is set, then bind as
1298 * "proxyauthzdn" and explicitly add the proxyAuthz
1299 * control to every operation with the dn bound
1300 * to the connection as control value.
1303 /* bind as proxyauthzdn only if no idassert mode
1304 * is requested, or if the client's identity
1306 switch ( li->li_idassert_mode ) {
1307 case LDAP_BACK_IDASSERT_LEGACY:
1308 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1309 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1311 binddn = li->li_idassert_authcDN;
1312 bindcred = li->li_idassert_passwd;
1319 /* NOTE: rootdn can always idassert */
1320 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1321 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1322 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1323 if ( sendok & LDAP_BACK_SENDERR ) {
1324 send_ldap_result( op, rs );
1326 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1329 rs->sr_err = LDAP_SUCCESS;
1330 binddn = slap_empty_bv;
1331 bindcred = slap_empty_bv;
1337 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1338 struct berval authcDN;
1340 if ( BER_BVISNULL( &ndn ) ) {
1341 authcDN = slap_empty_bv;
1346 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1347 &authcDN, &authcDN );
1348 if ( rs->sr_err != LDAP_SUCCESS ) {
1349 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1350 if ( sendok & LDAP_BACK_SENDERR ) {
1351 send_ldap_result( op, rs );
1353 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1356 rs->sr_err = LDAP_SUCCESS;
1357 binddn = slap_empty_bv;
1358 bindcred = slap_empty_bv;
1366 binddn = li->li_idassert_authcDN;
1367 bindcred = li->li_idassert_passwd;
1372 if ( dobind && li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1373 #ifdef HAVE_CYRUS_SASL
1374 void *defaults = NULL;
1375 struct berval authzID = BER_BVNULL;
1378 /* if SASL supports native authz, prepare for it */
1379 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1380 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1382 switch ( li->li_idassert_mode ) {
1383 case LDAP_BACK_IDASSERT_OTHERID:
1384 case LDAP_BACK_IDASSERT_OTHERDN:
1385 authzID = li->li_idassert_authzID;
1388 case LDAP_BACK_IDASSERT_ANONYMOUS:
1389 BER_BVSTR( &authzID, "dn:" );
1392 case LDAP_BACK_IDASSERT_SELF:
1393 if ( BER_BVISNULL( &ndn ) ) {
1394 /* connection is not authc'd, so don't idassert */
1395 BER_BVSTR( &authzID, "dn:" );
1398 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1399 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1400 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1401 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1402 ndn.bv_val, ndn.bv_len + 1 );
1411 if ( li->li_idassert_secprops != NULL ) {
1412 rs->sr_err = ldap_set_option( lc->lc_ld,
1413 LDAP_OPT_X_SASL_SECPROPS,
1414 (void *)li->li_idassert_secprops );
1416 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1417 rs->sr_err = LDAP_OTHER;
1418 if ( sendok & LDAP_BACK_SENDERR ) {
1419 send_ldap_result( op, rs );
1421 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1426 defaults = lutil_sasl_defaults( lc->lc_ld,
1427 li->li_idassert_sasl_mech.bv_val,
1428 li->li_idassert_sasl_realm.bv_val,
1429 li->li_idassert_authcID.bv_val,
1430 li->li_idassert_passwd.bv_val,
1433 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1434 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1435 LDAP_SASL_QUIET, lutil_sasl_interact,
1438 rs->sr_err = slap_map_api2result( rs );
1439 if ( rs->sr_err != LDAP_SUCCESS ) {
1440 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1441 if ( sendok & LDAP_BACK_SENDERR ) {
1442 send_ldap_result( op, rs );
1446 LDAP_BACK_CONN_ISBOUND_SET( lc );
1449 lutil_sasl_freedefs( defaults );
1451 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1455 #endif /* HAVE_CYRUS_SASL */
1458 switch ( li->li_idassert_authmethod ) {
1459 case LDAP_AUTH_NONE:
1460 LDAP_BACK_CONN_ISBOUND_SET( lc );
1463 case LDAP_AUTH_SIMPLE:
1464 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1465 binddn.bv_val, LDAP_SASL_SIMPLE,
1466 &bindcred, NULL, NULL, &msgid );
1471 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1472 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1473 if ( sendok & LDAP_BACK_SENDERR ) {
1474 send_ldap_result( op, rs );
1479 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1480 if ( rc == LDAP_SUCCESS ) {
1481 LDAP_BACK_CONN_ISBOUND_SET( lc );
1484 return LDAP_BACK_CONN_ISBOUND( lc );
1488 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1489 * to existing server-side controls if required; if not,
1490 * the existing server-side controls are placed in *pctrls.
1491 * The caller, after using the controls in client API
1492 * operations, if ( *pctrls != op->o_ctrls ), should
1493 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1494 * The function returns success if the control could
1495 * be added if required, or if it did nothing; in the future,
1496 * it might return some error if it failed.
1498 * if no bind took place yet, but the connection is bound
1499 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1500 * and explicitly add proxyAuthz the control to every operation
1501 * with the dn bound to the connection as control value.
1503 * If no server-side controls are defined for the operation,
1504 * simply add the proxyAuthz control; otherwise, if the
1505 * proxyAuthz control is not already set, add it as
1508 * FIXME: is controls order significant for security?
1509 * ANSWER: controls ordering and interoperability
1510 * must be indicated by the specs of each control; if none
1511 * is specified, the order is irrelevant.
1514 ldap_back_proxy_authz_ctrl(
1518 LDAPControl ***pctrls )
1520 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
1521 LDAPControl **ctrls = NULL;
1524 struct berval assertedID,
1529 rs->sr_err = LDAP_SUCCESS;
1531 /* don't proxyAuthz if protocol is not LDAPv3 */
1532 switch ( li->li_version ) {
1537 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1546 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1547 * but if it is not set this test fails. We need a different
1548 * means to detect if idassert is enabled */
1549 if ( ( BER_BVISNULL( &li->li_idassert_authcID ) || BER_BVISEMPTY( &li->li_idassert_authcID ) )
1550 && ( BER_BVISNULL( &li->li_idassert_authcDN ) || BER_BVISEMPTY( &li->li_idassert_authcDN ) ) )
1555 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1559 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1560 ndn = op->o_conn->c_ndn;
1566 if ( li->li_idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1567 if ( op->o_proxy_authz ) {
1569 * FIXME: we do not want to perform proxyAuthz
1570 * on behalf of the client, because this would
1571 * be performed with "proxyauthzdn" privileges.
1573 * This might actually be too strict, since
1574 * the "proxyauthzdn" authzTo, and each entry's
1575 * authzFrom attributes may be crafted
1576 * to avoid unwanted proxyAuthz to take place.
1579 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1580 rs->sr_text = "proxyAuthz not allowed within namingContext";
1585 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1589 if ( BER_BVISNULL( &ndn ) ) {
1593 if ( BER_BVISNULL( &li->li_idassert_authcDN ) ) {
1597 } else if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1598 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1599 /* && ( !BER_BVISNULL( &ndn )
1600 || LDAP_BACK_CONN_ISBOUND( lc ) ) */ )
1602 /* already asserted in SASL via native authz */
1603 /* NOTE: the test on lc->lc_bound is used to trap
1604 * native authorization of anonymous users,
1605 * since in that case ndn is NULL */
1609 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1611 struct berval authcDN;
1613 if ( BER_BVISNULL( &ndn ) ) {
1614 authcDN = slap_empty_bv;
1618 rc = slap_sasl_matches( op, li->li_idassert_authz,
1619 &authcDN, & authcDN );
1620 if ( rc != LDAP_SUCCESS ) {
1621 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1623 /* ndn is not authorized
1624 * to use idassert */
1631 if ( op->o_proxy_authz ) {
1634 * 1) ignore the already set proxyAuthz control
1635 * 2) leave it in place, and don't set ours
1637 * 4) reject the operation
1639 * option (4) is very drastic
1640 * option (3) will make the remote server reject
1641 * the operation, thus being equivalent to (4)
1642 * option (2) will likely break the idassert
1643 * assumptions, so we cannot accept it;
1644 * option (1) means that we are contradicting
1645 * the client's reques.
1647 * I think (4) is the only correct choice.
1649 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1650 rs->sr_text = "proxyAuthz not allowed within namingContext";
1653 if ( op->o_is_auth_check ) {
1654 mode = LDAP_BACK_IDASSERT_NOASSERT;
1657 mode = li->li_idassert_mode;
1661 case LDAP_BACK_IDASSERT_SELF:
1662 if ( BER_BVISNULL( &ndn ) ) {
1668 case LDAP_BACK_IDASSERT_LEGACY:
1669 /* original behavior:
1670 * assert the client's identity */
1671 if ( BER_BVISNULL( &ndn ) ) {
1672 assertedID = slap_empty_bv;
1678 case LDAP_BACK_IDASSERT_ANONYMOUS:
1679 /* assert "anonymous" */
1680 assertedID = slap_empty_bv;
1683 case LDAP_BACK_IDASSERT_NOASSERT:
1684 /* don't assert; bind as proxyauthzdn */
1687 case LDAP_BACK_IDASSERT_OTHERID:
1688 case LDAP_BACK_IDASSERT_OTHERDN:
1689 /* assert idassert DN */
1690 assertedID = li->li_idassert_authzID;
1697 if ( BER_BVISNULL( &assertedID ) ) {
1698 assertedID = slap_empty_bv;
1701 /* don't idassert the bound DN (ITS#4497) */
1702 if ( dn_match( &assertedID, &lc->lc_bound_ndn ) ) {
1706 if ( op->o_ctrls ) {
1707 for ( i = 0; op->o_ctrls[ i ]; i++ )
1708 /* just count ctrls */ ;
1711 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
1713 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
1715 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1716 ctrls[ 0 ]->ldctl_iscritical = 1;
1718 switch ( li->li_idassert_mode ) {
1719 /* already in u:ID or dn:DN form */
1720 case LDAP_BACK_IDASSERT_OTHERID:
1721 case LDAP_BACK_IDASSERT_OTHERDN:
1722 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
1725 /* needs the dn: prefix */
1727 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1728 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
1730 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1731 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1732 assertedID.bv_val, assertedID.bv_len + 1 );
1736 /* Older versions of <draft-weltman-ldapv3-proxy> required
1737 * to encode the value of the authzID (and called it proxyDN);
1738 * this hack provides compatibility with those DSAs that
1739 * implement it this way */
1740 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
1741 struct berval authzID = ctrls[ 0 ]->ldctl_value;
1742 BerElementBuffer berbuf;
1743 BerElement *ber = (BerElement *)&berbuf;
1746 ber_init2( ber, 0, LBER_USE_DER );
1747 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1749 tag = ber_printf( ber, "O", &authzID );
1750 if ( tag == LBER_ERROR ) {
1751 rs->sr_err = LDAP_OTHER;
1755 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1756 rs->sr_err = LDAP_OTHER;
1761 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1762 ber_free_buf( ber );
1764 if ( rs->sr_err != LDAP_SUCCESS ) {
1765 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1770 } else if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
1771 struct berval authzID = ctrls[ 0 ]->ldctl_value,
1773 BerElementBuffer berbuf;
1774 BerElement *ber = (BerElement *)&berbuf;
1777 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
1778 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1779 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1781 rs->sr_err = LDAP_PROTOCOL_ERROR;
1786 tmp.bv_val += STRLENOF( "dn:" );
1787 tmp.bv_len -= STRLENOF( "dn:" );
1789 ber_init2( ber, 0, LBER_USE_DER );
1790 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1792 /* apparently, Mozilla API encodes this
1793 * as "SEQUENCE { LDAPDN }" */
1794 tag = ber_printf( ber, "{O}", &tmp );
1795 if ( tag == LBER_ERROR ) {
1796 rs->sr_err = LDAP_OTHER;
1800 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1801 rs->sr_err = LDAP_OTHER;
1806 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1807 ber_free_buf( ber );
1809 if ( rs->sr_err != LDAP_SUCCESS ) {
1810 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1815 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1818 if ( op->o_ctrls ) {
1819 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1820 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1823 ctrls[ i + 1 ] = NULL;
1826 if ( ctrls == NULL ) {
1827 ctrls = op->o_ctrls;
1836 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1838 LDAPControl **ctrls = *pctrls;
1840 /* we assume that the first control is the proxyAuthz
1841 * added by back-ldap, so it's the only one we explicitly
1843 if ( ctrls && ctrls != op->o_ctrls ) {
1844 assert( ctrls[ 0 ] != NULL );
1846 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1847 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1850 op->o_tmpfree( ctrls, op->o_tmpmemctx );