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 );
744 #endif /* HAVE_TLS */
745 if ( li->li_idle_timeout && lc ) {
746 lc->lc_time = op->o_time;
753 ldap_back_release_conn_lock(
759 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
762 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
764 assert( lc->lc_refcnt > 0 );
765 LDAP_BACK_CONN_BINDING_CLEAR( lc );
767 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
768 ldap_back_freeconn( op, lc, 0 );
771 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
778 * Note: as the check for the value of lc->lc_bound was already here, I removed
779 * it from all the callers, and I made the function return the flag, so
780 * it can be used to simplify the check.
782 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
785 ldap_back_dobind_int(
789 ldap_back_send_t sendok,
793 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
798 assert( retries >= 0 );
802 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
805 if ( binding == 0 ) {
806 /* check if already bound */
807 rc = LDAP_BACK_CONN_ISBOUND( lc );
811 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
816 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
817 /* if someone else is about to bind it, give up and retry */
819 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
821 ldap_pvt_thread_yield();
825 /* otherwise this thread will bind it */
826 LDAP_BACK_CONN_BINDING_SET( lc );
831 /* wait for pending operations to finish */
832 /* FIXME: may become a bottleneck! */
833 if ( lc->lc_refcnt != lc->lc_binding ) {
835 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
837 ldap_pvt_thread_yield();
842 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
846 while ( lc->lc_refcnt > 1 ) {
847 ldap_pvt_thread_yield();
848 rc = LDAP_BACK_CONN_ISBOUND( lc );
855 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
857 LDAP_BACK_CONN_BINDING_SET( lc );
859 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
864 * FIXME: we need to let clients use proxyAuthz
865 * otherwise we cannot do symmetric pools of servers;
866 * we have to live with the fact that a user can
867 * authorize itself as any ID that is allowed
868 * by the authzTo directive of the "proxyauthzdn".
871 * NOTE: current Proxy Authorization specification
872 * and implementation do not allow proxy authorization
873 * control to be provided with Bind requests
876 * if no bind took place yet, but the connection is bound
877 * and the "idassert-authcDN" (or other ID) is set,
878 * then bind as the asserting identity and explicitly
879 * add the proxyAuthz control to every operation with the
880 * dn bound to the connection as control value.
881 * This is done also if this is the authrizing backend,
882 * but the "override" flag is given to idassert.
883 * It allows to use SASL bind and yet proxyAuthz users
885 if ( op->o_conn != NULL &&
886 !op->o_do_not_cache &&
887 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
888 ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
890 (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok );
894 #ifdef HAVE_CYRUS_SASL
895 if ( LDAP_BACK_CONN_ISPRIV( lc )
896 && li->li_acl_authmethod == LDAP_AUTH_SASL )
898 void *defaults = NULL;
900 if ( li->li_acl_secprops != NULL ) {
901 rc = ldap_set_option( lc->lc_ld,
902 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops);
904 if ( rc != LDAP_OPT_SUCCESS ) {
905 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
906 "(SECPROPS,\"%s\") failed!\n",
907 li->li_acl_secprops, 0, 0 );
912 defaults = lutil_sasl_defaults( lc->lc_ld,
913 li->li_acl_sasl_mech.bv_val,
914 li->li_acl_sasl_realm.bv_val,
915 li->li_acl_authcID.bv_val,
916 li->li_acl_passwd.bv_val,
919 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
920 li->li_acl_authcDN.bv_val,
921 li->li_acl_sasl_mech.bv_val, NULL, NULL,
922 LDAP_SASL_QUIET, lutil_sasl_interact,
925 lutil_sasl_freedefs( defaults );
927 rs->sr_err = slap_map_api2result( rs );
928 if ( rs->sr_err != LDAP_SUCCESS ) {
929 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
930 send_ldap_result( op, rs );
933 LDAP_BACK_CONN_ISBOUND_SET( lc );
937 #endif /* HAVE_CYRUS_SASL */
940 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
941 lc->lc_bound_ndn.bv_val,
942 LDAP_SASL_SIMPLE, &lc->lc_cred,
943 NULL, NULL, &msgid );
945 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
946 if ( retries != LDAP_BACK_RETRY_NEVER ) {
948 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
951 assert( lc->lc_refcnt > 0 );
952 if ( lc->lc_refcnt == 1 ) {
953 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
956 /* lc here must be the regular lc, reset and ready for init */
957 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
958 if ( rs->sr_err != LDAP_SUCCESS ) {
965 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
968 if ( rs->sr_err == LDAP_SUCCESS ) {
977 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
981 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
986 ldap_back_freeconn( op, lc, dolock );
987 rs->sr_err = slap_map_api2result( rs );
992 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
993 if ( rc == LDAP_SUCCESS ) {
994 LDAP_BACK_CONN_ISBOUND_SET( lc );
999 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1000 rc = LDAP_BACK_CONN_ISBOUND( lc );
1002 ldap_back_release_conn_lock( op, rs, lc, dolock );
1009 ldap_back_dobind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1011 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1013 return ldap_back_dobind_int( lc, op, rs, sendok, li->li_nretries, 1 );
1017 * ldap_back_default_rebind
1019 * This is a callback used for chasing referrals using the same
1020 * credentials as the original user on this session.
1023 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1024 ber_int_t msgid, void *params )
1026 ldapconn_t *lc = (ldapconn_t *)params;
1029 /* ... otherwise we couldn't get here */
1030 assert( lc != NULL );
1032 if ( !ldap_tls_inplace( ld ) ) {
1033 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1035 const char *text = NULL;
1037 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1038 LDAP_BACK_RETRY_DEFAULT, &text );
1039 if ( rc != LDAP_SUCCESS ) {
1043 #endif /* HAVE_TLS */
1045 /* FIXME: add checks on the URL/identity? */
1047 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
1048 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1052 * ldap_back_default_urllist
1055 ldap_back_default_urllist(
1057 LDAPURLDesc **urllist,
1061 ldapinfo_t *li = (ldapinfo_t *)params;
1062 LDAPURLDesc **urltail;
1064 if ( urllist == url ) {
1065 return LDAP_SUCCESS;
1068 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1071 *urltail = *urllist;
1075 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1077 ch_free( li->li_uri );
1080 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1081 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1083 return LDAP_SUCCESS;
1087 ldap_back_op_result(
1093 ldap_back_send_t sendok )
1096 LDAPMessage *res = NULL;
1099 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1102 rs->sr_matched = NULL;
1104 /* if the error recorded in the reply corresponds
1105 * to a successful state, get the error from the
1106 * remote server response */
1107 if ( ERR_OK( rs->sr_err ) ) {
1112 tv.tv_sec = timeout;
1116 LDAP_BACK_TV_SET( &tv );
1120 /* if result parsing fails, note the failure reason */
1121 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1125 (void)ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1126 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
1127 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
1128 rs->sr_text = "Operation timed out";
1132 LDAP_BACK_TV_SET( &tv );
1133 ldap_pvt_thread_yield();
1137 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1142 /* otherwise get the result; if it is not
1143 * LDAP_SUCCESS, record it in the reply
1144 * structure (this includes
1145 * LDAP_COMPARE_{TRUE|FALSE}) */
1147 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1148 &match, &text, NULL, NULL, 1 );
1150 if ( rc != LDAP_SUCCESS ) {
1156 /* if the error in the reply structure is not
1157 * LDAP_SUCCESS, try to map it from client
1158 * to server error */
1159 if ( !ERR_OK( rs->sr_err ) ) {
1160 rs->sr_err = slap_map_api2result( rs );
1162 /* internal ops ( op->o_conn == NULL )
1163 * must not reply to client */
1164 if ( op->o_conn && !op->o_do_not_cache && match ) {
1166 /* record the (massaged) matched
1167 * DN into the reply structure */
1168 rs->sr_matched = match;
1172 ( ( sendok & LDAP_BACK_SENDOK )
1173 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1175 send_ldap_result( op, rs );
1178 if ( rs->sr_matched != match ) {
1179 free( (char *)rs->sr_matched );
1181 rs->sr_matched = NULL;
1182 ldap_memfree( match );
1185 ldap_memfree( text );
1188 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1191 /* return true if bound, false if failed */
1193 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1196 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1198 assert( lcp != NULL );
1199 assert( *lcp != NULL );
1201 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1203 if ( (*lcp)->lc_refcnt == 1 ) {
1204 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1205 Debug( LDAP_DEBUG_ANY,
1206 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1207 op->o_log_prefix, li->li_uri,
1208 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1209 "" : (*lcp)->lc_bound_ndn.bv_val );
1210 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1212 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1213 (*lcp)->lc_ld = NULL;
1214 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1216 /* lc here must be the regular lc, reset and ready for init */
1217 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1218 if ( rc != LDAP_SUCCESS ) {
1223 rc = ldap_back_dobind_int( *lcp, op, rs, sendok, 0, 0 );
1230 Debug( LDAP_DEBUG_TRACE,
1231 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1232 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1234 ldap_back_release_conn_lock( op, rs, *lcp, 0 );
1238 rs->sr_err = LDAP_UNAVAILABLE;
1239 rs->sr_text = "unable to retry";
1240 send_ldap_result( op, rs );
1244 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1250 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1252 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1253 struct berval binddn = slap_empty_bv;
1254 struct berval bindcred = slap_empty_bv;
1260 /* don't proxyAuthz if protocol is not LDAPv3 */
1261 switch ( li->li_version ) {
1266 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1275 if ( op->o_tag == LDAP_REQ_BIND ) {
1276 ndn = op->o_req_ndn;
1278 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1279 ndn = op->o_conn->c_ndn;
1286 * FIXME: we need to let clients use proxyAuthz
1287 * otherwise we cannot do symmetric pools of servers;
1288 * we have to live with the fact that a user can
1289 * authorize itself as any ID that is allowed
1290 * by the authzTo directive of the "proxyauthzdn".
1293 * NOTE: current Proxy Authorization specification
1294 * and implementation do not allow proxy authorization
1295 * control to be provided with Bind requests
1298 * if no bind took place yet, but the connection is bound
1299 * and the "proxyauthzdn" is set, then bind as
1300 * "proxyauthzdn" and explicitly add the proxyAuthz
1301 * control to every operation with the dn bound
1302 * to the connection as control value.
1305 /* bind as proxyauthzdn only if no idassert mode
1306 * is requested, or if the client's identity
1308 switch ( li->li_idassert_mode ) {
1309 case LDAP_BACK_IDASSERT_LEGACY:
1310 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1311 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1313 binddn = li->li_idassert_authcDN;
1314 bindcred = li->li_idassert_passwd;
1321 /* NOTE: rootdn can always idassert */
1322 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1323 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1324 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1325 if ( sendok & LDAP_BACK_SENDERR ) {
1326 send_ldap_result( op, rs );
1328 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1331 rs->sr_err = LDAP_SUCCESS;
1332 binddn = slap_empty_bv;
1333 bindcred = slap_empty_bv;
1339 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1340 struct berval authcDN;
1342 if ( BER_BVISNULL( &ndn ) ) {
1343 authcDN = slap_empty_bv;
1348 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1349 &authcDN, &authcDN );
1350 if ( rs->sr_err != LDAP_SUCCESS ) {
1351 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1352 if ( sendok & LDAP_BACK_SENDERR ) {
1353 send_ldap_result( op, rs );
1355 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1358 rs->sr_err = LDAP_SUCCESS;
1359 binddn = slap_empty_bv;
1360 bindcred = slap_empty_bv;
1368 binddn = li->li_idassert_authcDN;
1369 bindcred = li->li_idassert_passwd;
1374 if ( dobind && li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1375 #ifdef HAVE_CYRUS_SASL
1376 void *defaults = NULL;
1377 struct berval authzID = BER_BVNULL;
1380 /* if SASL supports native authz, prepare for it */
1381 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1382 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1384 switch ( li->li_idassert_mode ) {
1385 case LDAP_BACK_IDASSERT_OTHERID:
1386 case LDAP_BACK_IDASSERT_OTHERDN:
1387 authzID = li->li_idassert_authzID;
1390 case LDAP_BACK_IDASSERT_ANONYMOUS:
1391 BER_BVSTR( &authzID, "dn:" );
1394 case LDAP_BACK_IDASSERT_SELF:
1395 if ( BER_BVISNULL( &ndn ) ) {
1396 /* connection is not authc'd, so don't idassert */
1397 BER_BVSTR( &authzID, "dn:" );
1400 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1401 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1402 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1403 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1404 ndn.bv_val, ndn.bv_len + 1 );
1413 if ( li->li_idassert_secprops != NULL ) {
1414 rs->sr_err = ldap_set_option( lc->lc_ld,
1415 LDAP_OPT_X_SASL_SECPROPS,
1416 (void *)li->li_idassert_secprops );
1418 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1419 rs->sr_err = LDAP_OTHER;
1420 if ( sendok & LDAP_BACK_SENDERR ) {
1421 send_ldap_result( op, rs );
1423 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1428 defaults = lutil_sasl_defaults( lc->lc_ld,
1429 li->li_idassert_sasl_mech.bv_val,
1430 li->li_idassert_sasl_realm.bv_val,
1431 li->li_idassert_authcID.bv_val,
1432 li->li_idassert_passwd.bv_val,
1435 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1436 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1437 LDAP_SASL_QUIET, lutil_sasl_interact,
1440 rs->sr_err = slap_map_api2result( rs );
1441 if ( rs->sr_err != LDAP_SUCCESS ) {
1442 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1443 if ( sendok & LDAP_BACK_SENDERR ) {
1444 send_ldap_result( op, rs );
1448 LDAP_BACK_CONN_ISBOUND_SET( lc );
1451 lutil_sasl_freedefs( defaults );
1453 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1457 #endif /* HAVE_CYRUS_SASL */
1460 switch ( li->li_idassert_authmethod ) {
1461 case LDAP_AUTH_NONE:
1462 LDAP_BACK_CONN_ISBOUND_SET( lc );
1465 case LDAP_AUTH_SIMPLE:
1466 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1467 binddn.bv_val, LDAP_SASL_SIMPLE,
1468 &bindcred, NULL, NULL, &msgid );
1473 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1474 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1475 if ( sendok & LDAP_BACK_SENDERR ) {
1476 send_ldap_result( op, rs );
1481 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1482 if ( rc == LDAP_SUCCESS ) {
1483 LDAP_BACK_CONN_ISBOUND_SET( lc );
1486 return LDAP_BACK_CONN_ISBOUND( lc );
1490 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1491 * to existing server-side controls if required; if not,
1492 * the existing server-side controls are placed in *pctrls.
1493 * The caller, after using the controls in client API
1494 * operations, if ( *pctrls != op->o_ctrls ), should
1495 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1496 * The function returns success if the control could
1497 * be added if required, or if it did nothing; in the future,
1498 * it might return some error if it failed.
1500 * if no bind took place yet, but the connection is bound
1501 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1502 * and explicitly add proxyAuthz the control to every operation
1503 * with the dn bound to the connection as control value.
1505 * If no server-side controls are defined for the operation,
1506 * simply add the proxyAuthz control; otherwise, if the
1507 * proxyAuthz control is not already set, add it as
1510 * FIXME: is controls order significant for security?
1511 * ANSWER: controls ordering and interoperability
1512 * must be indicated by the specs of each control; if none
1513 * is specified, the order is irrelevant.
1516 ldap_back_proxy_authz_ctrl(
1520 LDAPControl ***pctrls )
1522 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
1523 LDAPControl **ctrls = NULL;
1526 struct berval assertedID,
1531 rs->sr_err = LDAP_SUCCESS;
1533 /* don't proxyAuthz if protocol is not LDAPv3 */
1534 switch ( li->li_version ) {
1539 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1548 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1549 * but if it is not set this test fails. We need a different
1550 * means to detect if idassert is enabled */
1551 if ( ( BER_BVISNULL( &li->li_idassert_authcID ) || BER_BVISEMPTY( &li->li_idassert_authcID ) )
1552 && ( BER_BVISNULL( &li->li_idassert_authcDN ) || BER_BVISEMPTY( &li->li_idassert_authcDN ) ) )
1557 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1561 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1562 ndn = op->o_conn->c_ndn;
1568 if ( li->li_idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1569 if ( op->o_proxy_authz ) {
1571 * FIXME: we do not want to perform proxyAuthz
1572 * on behalf of the client, because this would
1573 * be performed with "proxyauthzdn" privileges.
1575 * This might actually be too strict, since
1576 * the "proxyauthzdn" authzTo, and each entry's
1577 * authzFrom attributes may be crafted
1578 * to avoid unwanted proxyAuthz to take place.
1581 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1582 rs->sr_text = "proxyAuthz not allowed within namingContext";
1587 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1591 if ( BER_BVISNULL( &ndn ) ) {
1595 if ( BER_BVISNULL( &li->li_idassert_authcDN ) ) {
1599 } else if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1600 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1601 /* && ( !BER_BVISNULL( &ndn )
1602 || LDAP_BACK_CONN_ISBOUND( lc ) ) */ )
1604 /* already asserted in SASL via native authz */
1605 /* NOTE: the test on lc->lc_bound is used to trap
1606 * native authorization of anonymous users,
1607 * since in that case ndn is NULL */
1611 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1613 struct berval authcDN;
1615 if ( BER_BVISNULL( &ndn ) ) {
1616 authcDN = slap_empty_bv;
1620 rc = slap_sasl_matches( op, li->li_idassert_authz,
1621 &authcDN, & authcDN );
1622 if ( rc != LDAP_SUCCESS ) {
1623 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1625 /* ndn is not authorized
1626 * to use idassert */
1633 if ( op->o_proxy_authz ) {
1636 * 1) ignore the already set proxyAuthz control
1637 * 2) leave it in place, and don't set ours
1639 * 4) reject the operation
1641 * option (4) is very drastic
1642 * option (3) will make the remote server reject
1643 * the operation, thus being equivalent to (4)
1644 * option (2) will likely break the idassert
1645 * assumptions, so we cannot accept it;
1646 * option (1) means that we are contradicting
1647 * the client's reques.
1649 * I think (4) is the only correct choice.
1651 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1652 rs->sr_text = "proxyAuthz not allowed within namingContext";
1655 if ( op->o_is_auth_check ) {
1656 mode = LDAP_BACK_IDASSERT_NOASSERT;
1659 mode = li->li_idassert_mode;
1663 case LDAP_BACK_IDASSERT_SELF:
1664 if ( BER_BVISNULL( &ndn ) ) {
1670 case LDAP_BACK_IDASSERT_LEGACY:
1671 /* original behavior:
1672 * assert the client's identity */
1673 if ( BER_BVISNULL( &ndn ) ) {
1674 assertedID = slap_empty_bv;
1680 case LDAP_BACK_IDASSERT_ANONYMOUS:
1681 /* assert "anonymous" */
1682 assertedID = slap_empty_bv;
1685 case LDAP_BACK_IDASSERT_NOASSERT:
1686 /* don't assert; bind as proxyauthzdn */
1689 case LDAP_BACK_IDASSERT_OTHERID:
1690 case LDAP_BACK_IDASSERT_OTHERDN:
1691 /* assert idassert DN */
1692 assertedID = li->li_idassert_authzID;
1699 if ( BER_BVISNULL( &assertedID ) ) {
1700 assertedID = slap_empty_bv;
1703 /* don't idassert the bound DN (ITS#4497) */
1704 if ( dn_match( &assertedID, &lc->lc_bound_ndn ) ) {
1708 if ( op->o_ctrls ) {
1709 for ( i = 0; op->o_ctrls[ i ]; i++ )
1710 /* just count ctrls */ ;
1713 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
1715 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
1717 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1718 ctrls[ 0 ]->ldctl_iscritical = 1;
1720 switch ( li->li_idassert_mode ) {
1721 /* already in u:ID or dn:DN form */
1722 case LDAP_BACK_IDASSERT_OTHERID:
1723 case LDAP_BACK_IDASSERT_OTHERDN:
1724 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
1727 /* needs the dn: prefix */
1729 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1730 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
1732 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1733 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1734 assertedID.bv_val, assertedID.bv_len + 1 );
1738 /* Older versions of <draft-weltman-ldapv3-proxy> required
1739 * to encode the value of the authzID (and called it proxyDN);
1740 * this hack provides compatibility with those DSAs that
1741 * implement it this way */
1742 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
1743 struct berval authzID = ctrls[ 0 ]->ldctl_value;
1744 BerElementBuffer berbuf;
1745 BerElement *ber = (BerElement *)&berbuf;
1748 ber_init2( ber, 0, LBER_USE_DER );
1749 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1751 tag = ber_printf( ber, "O", &authzID );
1752 if ( tag == LBER_ERROR ) {
1753 rs->sr_err = LDAP_OTHER;
1757 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1758 rs->sr_err = LDAP_OTHER;
1763 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1764 ber_free_buf( ber );
1766 if ( rs->sr_err != LDAP_SUCCESS ) {
1767 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1772 } else if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
1773 struct berval authzID = ctrls[ 0 ]->ldctl_value,
1775 BerElementBuffer berbuf;
1776 BerElement *ber = (BerElement *)&berbuf;
1779 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
1780 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1781 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1783 rs->sr_err = LDAP_PROTOCOL_ERROR;
1788 tmp.bv_val += STRLENOF( "dn:" );
1789 tmp.bv_len -= STRLENOF( "dn:" );
1791 ber_init2( ber, 0, LBER_USE_DER );
1792 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1794 /* apparently, Mozilla API encodes this
1795 * as "SEQUENCE { LDAPDN }" */
1796 tag = ber_printf( ber, "{O}", &tmp );
1797 if ( tag == LBER_ERROR ) {
1798 rs->sr_err = LDAP_OTHER;
1802 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1803 rs->sr_err = LDAP_OTHER;
1808 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1809 ber_free_buf( ber );
1811 if ( rs->sr_err != LDAP_SUCCESS ) {
1812 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1817 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1820 if ( op->o_ctrls ) {
1821 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1822 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1825 ctrls[ i + 1 ] = NULL;
1828 if ( ctrls == NULL ) {
1829 ctrls = op->o_ctrls;
1838 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1840 LDAPControl **ctrls = *pctrls;
1842 /* we assume that the first control is the proxyAuthz
1843 * added by back-ldap, so it's the only one we explicitly
1845 if ( ctrls && ctrls != op->o_ctrls ) {
1846 assert( ctrls[ 0 ] != NULL );
1848 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1849 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1852 op->o_tmpfree( ctrls, op->o_tmpmemctx );