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"
44 static LDAP_REBIND_PROC ldap_back_default_rebind;
46 LDAP_REBIND_PROC *ldap_back_rebind_f = ldap_back_default_rebind;
49 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
52 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
55 ldap_back_bind( Operation *op, SlapReply *rs )
57 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
63 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR );
68 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
69 ch_free( lc->lc_bound_ndn.bv_val );
70 BER_BVZERO( &lc->lc_bound_ndn );
72 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
74 /* method is always LDAP_AUTH_SIMPLE if we got here */
75 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
77 &op->orb_cred, op->o_ctrls, NULL, &msgid );
78 rc = ldap_back_op_result( lc, op, rs, msgid, 0, LDAP_BACK_SENDERR );
80 if ( rc == LDAP_SUCCESS ) {
81 /* If defined, proxyAuthz will be used also when
82 * back-ldap is the authorizing backend; for this
83 * purpose, a successful bind is followed by a
84 * bind with the configured identity assertion */
85 /* NOTE: use with care */
86 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
87 ldap_back_proxy_authz_bind( lc, op, rs, LDAP_BACK_SENDERR );
88 if ( !LDAP_BACK_CONN_ISBOUND( lc ) ) {
94 LDAP_BACK_CONN_ISBOUND_SET( lc );
95 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
97 if ( LDAP_BACK_SAVECRED( li ) ) {
98 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
99 memset( lc->lc_cred.bv_val, 0,
100 lc->lc_cred.bv_len );
102 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
103 ldap_set_rebind_proc( lc->lc_ld, ldap_back_rebind_f, lc );
108 assert( lc->lc_binding == 1 );
111 /* must re-insert if local DN changed as result of bind */
112 if ( !LDAP_BACK_CONN_ISBOUND( lc )
113 || ( LDAP_BACK_CONN_ISBOUND( lc )
114 && !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 lc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
129 ldap_back_conndn_cmp );
130 assert( lc != NULL );
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 );
140 /* we can do this because lc_refcnt == 1 */
141 ldap_back_conn_free( lc );
147 ldap_back_release_conn( op, rs, lc );
154 * ldap_back_conndn_cmp
156 * compares two ldapconn_t based on the value of the conn pointer
157 * and of the local DN; used by avl stuff for insert, lookup
161 ldap_back_conndn_cmp( const void *c1, const void *c2 )
163 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
164 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
167 /* If local DNs don't match, it is definitely not a match */
168 /* For shared sessions, conn is NULL. Only explicitly
169 * bound sessions will have non-NULL conn.
171 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
173 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
180 * ldap_back_conndnlc_cmp
182 * compares two ldapconn_t based on the value of the conn pointer,
183 * the local DN and the lc pointer; used by avl stuff for insert, lookup
187 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
189 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
190 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
193 /* If local DNs don't match, it is definitely not a match */
194 /* For shared sessions, conn is NULL. Only explicitly
195 * bound sessions will have non-NULL conn.
197 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
199 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
201 rc = SLAP_PTRCMP( lc1, lc2 );
211 * compares two ldapconn_t based on the value of the conn pointer;
212 * used by avl stuff for delete of all conns with the same connid
215 ldap_back_conn_cmp( const void *c1, const void *c2 )
217 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
218 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
220 /* For shared sessions, conn is NULL. Only explicitly
221 * bound sessions will have non-NULL conn.
223 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
227 * ldap_back_conndn_dup
229 * returns -1 in case a duplicate ldapconn_t has been inserted;
233 ldap_back_conndn_dup( void *c1, void *c2 )
235 ldapconn_t *lc1 = (ldapconn_t *)c1;
236 ldapconn_t *lc2 = (ldapconn_t *)c2;
238 /* Cannot have more than one shared session with same DN */
239 if ( lc1->lc_conn == lc2->lc_conn &&
240 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
248 #if PRINT_CONNTREE > 0
250 ravl_print( Avlnode *root, int depth )
259 ravl_print( root->avl_right, depth+1 );
261 for ( i = 0; i < depth; i++ ) {
262 fprintf( stderr, "-" );
266 fprintf( stderr, "lc=%p local=\"%s\" conn=%p %s refcnt=%d\n",
267 (void *)lc, lc->lc_local_ndn.bv_val, (void *)lc->lc_conn,
268 avl_bf2str( root->avl_bf ), lc->lc_refcnt );
270 ravl_print( root->avl_left, depth+1 );
274 myprint( Avlnode *root )
276 fprintf( stderr, "========>\n" );
279 fprintf( stderr, "\tNULL\n" );
282 ravl_print( root, 0 );
285 fprintf( stderr, "<========\n" );
287 #endif /* PRINT_CONNTREE */
290 ldap_back_freeconn( Operation *op, ldapconn_t *lc, int dolock )
292 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
295 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
298 assert( lc->lc_refcnt > 0 );
299 if ( --lc->lc_refcnt == 0 ) {
300 lc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
301 ldap_back_conndn_cmp );
302 assert( lc != NULL );
304 ldap_back_conn_free( (void *)lc );
308 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
325 int rc = LDAP_SUCCESS;
328 /* this is ridiculous... */
329 dummy.li_flags = flags;
331 /* start TLS ("tls-[try-]{start,propagate}" statements) */
332 if ( ( LDAP_BACK_USE_TLS( &dummy ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS( &dummy ) ) )
333 && !ldap_is_ldaps_url( url ) )
335 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
337 * use asynchronous StartTLS
338 * in case, chase referral (not implemented yet)
342 if ( protocol == 0 ) {
343 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
347 if ( protocol < LDAP_VERSION3 ) {
348 protocol = LDAP_VERSION3;
349 /* Set LDAP version */
350 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
351 (const void *)&protocol );
354 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
355 if ( rc == LDAP_SUCCESS ) {
356 LDAPMessage *res = NULL;
359 LDAP_BACK_TV_SET( &tv );
362 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
364 rc = LDAP_UNAVAILABLE;
366 } else if ( rc == 0 ) {
367 if ( retries != LDAP_BACK_RETRY_NEVER ) {
368 ldap_pvt_thread_yield();
372 LDAP_BACK_TV_SET( &tv );
375 rc = LDAP_UNAVAILABLE;
377 } else if ( rc == LDAP_RES_EXTENDED ) {
378 struct berval *data = NULL;
380 rc = ldap_parse_extended_result( ld, res,
382 if ( rc == LDAP_SUCCESS ) {
384 rc = ldap_parse_result( ld, res, &err,
385 NULL, NULL, NULL, NULL, 1 );
386 if ( rc == LDAP_SUCCESS ) {
391 /* FIXME: in case a referral
392 * is returned, should we try
393 * using it instead of the
395 if ( rc == LDAP_SUCCESS ) {
396 rc = ldap_install_tls( ld );
398 } else if ( rc == LDAP_REFERRAL ) {
399 rc = LDAP_UNWILLING_TO_PERFORM;
400 *text = "unwilling to chase referral returned by Start TLS exop";
404 if ( data->bv_val ) {
405 ber_memfree( data->bv_val );
419 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
421 * use synchronous StartTLS
423 rc = ldap_start_tls_s( ld, NULL, NULL );
424 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
426 /* if StartTLS is requested, only attempt it if the URL
427 * is not "ldaps://"; this may occur not only in case
428 * of misconfiguration, but also when used in the chain
429 * overlay, where the "uri" can be parsed out of a referral */
435 case LDAP_SERVER_DOWN:
439 if ( LDAP_BACK_TLS_CRITICAL( &dummy ) ) {
440 *text = "could not start TLS";
444 /* in case Start TLS is not critical */
456 #endif /* HAVE_TLS */
459 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
461 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
462 int vers = op->o_protocol;
465 int is_tls = op->o_conn->c_is_tls;
466 #endif /* HAVE_TLS */
468 assert( lcp != NULL );
470 rs->sr_err = ldap_initialize( &ld, li->li_uri );
471 if ( rs->sr_err != LDAP_SUCCESS ) {
475 /* Set LDAP version. This will always succeed: If the client
476 * bound with a particular version, then so can we.
479 /* assume it's an internal op; set to LDAPv3 */
480 vers = LDAP_VERSION3;
482 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
484 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
485 ldap_set_option( ld, LDAP_OPT_REFERRALS,
486 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
488 if ( li->li_network_timeout > 0 ) {
491 tv.tv_sec = li->li_network_timeout;
493 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
497 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
498 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
499 if ( rs->sr_err != LDAP_SUCCESS ) {
500 ldap_unbind_ext( ld, NULL, NULL );
503 #endif /* HAVE_TLS */
505 if ( *lcp == NULL ) {
506 *lcp = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
507 (*lcp)->lc_flags= li->li_flags;
510 (*lcp)->lc_refcnt = 1;
511 (*lcp)->lc_binding = 1;
514 LDAP_BACK_CONN_ISTLS_SET( *lcp );
516 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
518 #endif /* HAVE_TLS */
521 if ( rs->sr_err != LDAP_SUCCESS ) {
522 rs->sr_err = slap_map_api2result( rs );
523 if ( sendok & LDAP_BACK_SENDERR ) {
524 if ( rs->sr_text == NULL ) {
525 rs->sr_text = "ldap_initialize() failed";
527 send_ldap_result( op, rs );
532 if ( li->li_conn_ttl > 0 ) {
533 (*lcp)->lc_create_time = op->o_time;
541 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
543 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
544 ldapconn_t *lc = NULL,
546 int refcnt = 1, binding = 1;
548 /* Internal searches are privileged and shared. So is root. */
549 if ( op->o_do_not_cache || be_isroot( op ) ) {
550 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
551 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
552 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
555 lc_curr.lc_local_ndn = op->o_ndn;
556 /* Explicit binds must not be shared */
557 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
558 lc_curr.lc_conn = op->o_conn;
561 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
565 /* Explicit Bind requests always get their own conn */
566 if ( !( sendok & LDAP_BACK_BINDING ) ) {
567 /* Searches for a ldapconn in the avl tree */
569 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
571 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
572 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
574 /* Don't reuse connections while they're still binding */
575 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
576 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
577 ldap_pvt_thread_yield();
580 refcnt = ++lc->lc_refcnt;
581 binding = ++lc->lc_binding;
583 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
586 /* Looks like we didn't get a bind. Open a new session... */
588 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
591 if ( sendok & LDAP_BACK_BINDING ) {
592 LDAP_BACK_CONN_BINDING_SET( lc );
594 lc->lc_conn = lc_curr.lc_conn;
595 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
597 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
598 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
599 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
600 LDAP_BACK_CONN_ISPRIV_SET( lc );
603 BER_BVZERO( &lc->lc_cred );
604 BER_BVZERO( &lc->lc_bound_ndn );
606 /* FIXME: if we set lc_bound_ndn = o_ndn
607 * we end up with a bind with DN but no password! */
608 if ( !BER_BVISEMPTY( &op->o_ndn )
609 && SLAP_IS_AUTHZ_BACKEND( op ) )
611 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
617 /* if start TLS failed but it was not mandatory,
618 * check if the non-TLS connection was already
619 * in cache; in case, destroy the newly created
620 * connection and use the existing one */
621 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
622 && !ldap_tls_inplace( lc->lc_ld ) )
626 lc_curr.lc_conn = LDAP_BACK_PCONN;
627 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
628 tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
629 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
630 if ( tmplc != NULL ) {
631 refcnt = ++tmplc->lc_refcnt;
632 binding = ++tmplc->lc_binding;
633 ldap_back_conn_free( lc );
636 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
638 if ( tmplc != NULL ) {
642 #endif /* HAVE_TLS */
644 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
646 /* Inserts the newly created ldapconn in the avl tree */
647 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
649 assert( lc->lc_refcnt == 1 );
650 assert( lc->lc_binding == 1 );
651 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
652 ldap_back_conndn_cmp, ldap_back_conndn_dup );
654 #if PRINT_CONNTREE > 0
655 myprint( li->li_conninfo.lai_tree );
656 #endif /* PRINT_CONNTREE */
658 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
660 Debug( LDAP_DEBUG_TRACE,
661 "=>ldap_back_getconn: conn %p inserted refcnt=%u binding=%u\n",
662 (void *)lc, refcnt, binding );
664 /* Err could be -1 in case a duplicate ldapconn is inserted */
665 switch ( rs->sr_err ) {
670 if ( !( sendok & LDAP_BACK_BINDING ) ) {
671 /* duplicate: free and try to get the newly created one */
674 /* taint connection, so that it'll be freed when released */
675 LDAP_BACK_CONN_TAINTED_SET( lc );
679 ldap_back_conn_free( lc );
680 rs->sr_err = LDAP_OTHER;
681 rs->sr_text = "proxy bind collision";
682 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
683 send_ldap_result( op, rs );
690 char buf[ SLAP_TEXT_BUFLEN ];
693 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
694 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
698 /* let it be used, but taint/delete it so that
699 * no-one else can look it up any further */
700 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
701 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
702 ldap_back_conndnlc_cmp );
703 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
704 LDAP_BACK_CONN_TAINTED_SET( lc );
707 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
708 snprintf( buf, sizeof( buf ),
709 "conn %p fetched refcnt=%u binding=%u%s",
710 (void *)lc, refcnt, binding, expiring ? " expiring" : "" );
711 Debug( LDAP_DEBUG_TRACE,
712 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
716 if ( li->li_idle_timeout && lc ) {
717 lc->lc_time = op->o_time;
725 ldap_back_release_conn_lock(
731 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
734 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
736 assert( lc->lc_refcnt > 0 );
737 LDAP_BACK_CONN_BINDING_CLEAR( lc );
738 if ( --lc->lc_refcnt == 0 && LDAP_BACK_CONN_TAINTED( lc ) ) {
739 ldap_back_freeconn( op, rs, 0 );
742 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
749 * Note: as the check for the value of lc->lc_bound was already here, I removed
750 * it from all the callers, and I made the function return the flag, so
751 * it can be used to simplify the check.
753 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
756 ldap_back_dobind_int(
760 ldap_back_send_t sendok,
764 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
769 assert( retries >= 0 );
773 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
776 if ( binding == 0 ) {
777 /* check if already bound */
778 rc = LDAP_BACK_CONN_ISBOUND( lc );
782 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
787 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
788 /* if someone else is about to bind it, give up and retry */
790 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
792 ldap_pvt_thread_yield();
796 /* otherwise this thread will bind it */
797 LDAP_BACK_CONN_BINDING_SET( lc );
802 /* wait for pending operations to finish */
803 /* FIXME: may become a bottleneck! */
804 if ( lc->lc_refcnt != lc->lc_binding ) {
806 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
808 ldap_pvt_thread_yield();
813 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
817 while ( lc->lc_refcnt > 1 ) {
818 ldap_pvt_thread_yield();
819 rc = LDAP_BACK_CONN_ISBOUND( lc );
826 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
828 LDAP_BACK_CONN_BINDING_SET( lc );
830 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
835 * FIXME: we need to let clients use proxyAuthz
836 * otherwise we cannot do symmetric pools of servers;
837 * we have to live with the fact that a user can
838 * authorize itself as any ID that is allowed
839 * by the authzTo directive of the "proxyauthzdn".
842 * NOTE: current Proxy Authorization specification
843 * and implementation do not allow proxy authorization
844 * control to be provided with Bind requests
847 * if no bind took place yet, but the connection is bound
848 * and the "idassert-authcDN" (or other ID) is set,
849 * then bind as the asserting identity and explicitly
850 * add the proxyAuthz control to every operation with the
851 * dn bound to the connection as control value.
852 * This is done also if this is the authrizing backend,
853 * but the "override" flag is given to idassert.
854 * It allows to use SASL bind and yet proxyAuthz users
856 if ( op->o_conn != NULL &&
857 !op->o_do_not_cache &&
858 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
859 ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
861 (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok );
865 #ifdef HAVE_CYRUS_SASL
866 if ( LDAP_BACK_CONN_ISPRIV( lc )
867 && li->li_acl_authmethod == LDAP_AUTH_SASL )
869 void *defaults = NULL;
871 if ( li->li_acl_secprops != NULL ) {
872 rc = ldap_set_option( lc->lc_ld,
873 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops);
875 if ( rc != LDAP_OPT_SUCCESS ) {
876 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
877 "(%s,SECPROPS,\"%s\") failed!\n",
878 li->li_uri, li->li_acl_secprops, 0 );
883 defaults = lutil_sasl_defaults( lc->lc_ld,
884 li->li_acl_sasl_mech.bv_val,
885 li->li_acl_sasl_realm.bv_val,
886 li->li_acl_authcID.bv_val,
887 li->li_acl_passwd.bv_val,
890 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
891 li->li_acl_authcDN.bv_val,
892 li->li_acl_sasl_mech.bv_val, NULL, NULL,
893 LDAP_SASL_QUIET, lutil_sasl_interact,
896 lutil_sasl_freedefs( defaults );
898 rs->sr_err = slap_map_api2result( rs );
899 if ( rs->sr_err != LDAP_SUCCESS ) {
900 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
901 send_ldap_result( op, rs );
904 LDAP_BACK_CONN_ISBOUND_SET( lc );
908 #endif /* HAVE_CYRUS_SASL */
911 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
912 lc->lc_bound_ndn.bv_val,
913 LDAP_SASL_SIMPLE, &lc->lc_cred,
914 NULL, NULL, &msgid );
916 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
917 if ( retries != LDAP_BACK_RETRY_NEVER ) {
919 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
922 assert( lc->lc_refcnt > 0 );
923 if ( lc->lc_refcnt == 1 ) {
924 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
927 /* lc here must be the regular lc, reset and ready for init */
928 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
931 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
933 if ( rs->sr_err == LDAP_SUCCESS ) {
942 ldap_back_freeconn( op, lc, dolock );
943 rs->sr_err = slap_map_api2result( rs );
948 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
949 if ( rc == LDAP_SUCCESS ) {
950 LDAP_BACK_CONN_ISBOUND_SET( lc );
955 LDAP_BACK_CONN_BINDING_CLEAR( lc );
956 rc = LDAP_BACK_CONN_ISBOUND( lc );
958 ldap_back_release_conn_lock( op, rs, lc, dolock );
965 ldap_back_dobind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
967 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
969 return ldap_back_dobind_int( lc, op, rs, sendok, li->li_nretries, 1 );
973 * ldap_back_default_rebind
975 * This is a callback used for chasing referrals using the same
976 * credentials as the original user on this session.
979 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
980 ber_int_t msgid, void *params )
982 ldapconn_t *lc = (ldapconn_t *)params;
985 /* ... otherwise we couldn't get here */
986 assert( lc != NULL );
988 if ( !ldap_tls_inplace( ld ) ) {
989 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
991 const char *text = NULL;
993 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
994 LDAP_BACK_RETRY_DEFAULT, &text );
995 if ( rc != LDAP_SUCCESS ) {
999 #endif /* HAVE_TLS */
1001 /* FIXME: add checks on the URL/identity? */
1003 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
1004 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1008 ldap_back_op_result(
1014 ldap_back_send_t sendok )
1017 LDAPMessage *res = NULL;
1020 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1023 rs->sr_matched = NULL;
1025 /* if the error recorded in the reply corresponds
1026 * to a successful state, get the error from the
1027 * remote server response */
1028 if ( ERR_OK( rs->sr_err ) ) {
1033 tv.tv_sec = timeout;
1037 LDAP_BACK_TV_SET( &tv );
1041 /* if result parsing fails, note the failure reason */
1042 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1046 (void)ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1047 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
1048 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
1049 rs->sr_text = "Operation timed out";
1053 LDAP_BACK_TV_SET( &tv );
1054 ldap_pvt_thread_yield();
1058 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1063 /* otherwise get the result; if it is not
1064 * LDAP_SUCCESS, record it in the reply
1065 * structure (this includes
1066 * LDAP_COMPARE_{TRUE|FALSE}) */
1068 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1069 &match, &text, NULL, NULL, 1 );
1071 if ( rc != LDAP_SUCCESS ) {
1077 /* if the error in the reply structure is not
1078 * LDAP_SUCCESS, try to map it from client
1079 * to server error */
1080 if ( !ERR_OK( rs->sr_err ) ) {
1081 rs->sr_err = slap_map_api2result( rs );
1083 /* internal ops ( op->o_conn == NULL )
1084 * must not reply to client */
1085 if ( op->o_conn && !op->o_do_not_cache && match ) {
1087 /* record the (massaged) matched
1088 * DN into the reply structure */
1089 rs->sr_matched = match;
1093 ( ( sendok & LDAP_BACK_SENDOK )
1094 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1096 send_ldap_result( op, rs );
1099 if ( rs->sr_matched != match ) {
1100 free( (char *)rs->sr_matched );
1102 rs->sr_matched = NULL;
1103 ldap_memfree( match );
1106 ldap_memfree( text );
1109 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1112 /* return true if bound, false if failed */
1114 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1117 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1119 assert( lcp != NULL );
1120 assert( *lcp != NULL );
1122 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1124 if ( (*lcp)->lc_refcnt == 1 ) {
1125 Debug( LDAP_DEBUG_ANY,
1126 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1127 op->o_log_prefix, li->li_uri,
1128 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1129 "" : (*lcp)->lc_bound_ndn.bv_val );
1131 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1132 (*lcp)->lc_ld = NULL;
1133 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1135 /* lc here must be the regular lc, reset and ready for init */
1136 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1137 if ( rc != LDAP_SUCCESS ) {
1142 rc = ldap_back_dobind_int( *lcp, op, rs, sendok, 0, 0 );
1149 Debug( LDAP_DEBUG_TRACE,
1150 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1151 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1153 ldap_back_release_conn_lock( op, rs, *lcp, 0 );
1157 rs->sr_err = LDAP_UNAVAILABLE;
1158 rs->sr_text = "unable to retry";
1159 send_ldap_result( op, rs );
1163 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1169 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1171 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1172 struct berval binddn = slap_empty_bv;
1173 struct berval bindcred = slap_empty_bv;
1179 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1180 ndn = op->o_conn->c_ndn;
1187 * FIXME: we need to let clients use proxyAuthz
1188 * otherwise we cannot do symmetric pools of servers;
1189 * we have to live with the fact that a user can
1190 * authorize itself as any ID that is allowed
1191 * by the authzTo directive of the "proxyauthzdn".
1194 * NOTE: current Proxy Authorization specification
1195 * and implementation do not allow proxy authorization
1196 * control to be provided with Bind requests
1199 * if no bind took place yet, but the connection is bound
1200 * and the "proxyauthzdn" is set, then bind as
1201 * "proxyauthzdn" and explicitly add the proxyAuthz
1202 * control to every operation with the dn bound
1203 * to the connection as control value.
1206 /* bind as proxyauthzdn only if no idassert mode
1207 * is requested, or if the client's identity
1209 switch ( li->li_idassert_mode ) {
1210 case LDAP_BACK_IDASSERT_LEGACY:
1211 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1212 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1214 binddn = li->li_idassert_authcDN;
1215 bindcred = li->li_idassert_passwd;
1222 /* NOTE: rootdn can always idassert */
1223 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1224 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1225 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1226 if ( sendok & LDAP_BACK_SENDERR ) {
1227 send_ldap_result( op, rs );
1229 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1232 rs->sr_err = LDAP_SUCCESS;
1233 binddn = slap_empty_bv;
1234 bindcred = slap_empty_bv;
1240 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1241 struct berval authcDN;
1243 if ( BER_BVISNULL( &ndn ) ) {
1244 authcDN = slap_empty_bv;
1249 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1250 &authcDN, &authcDN );
1251 if ( rs->sr_err != LDAP_SUCCESS ) {
1252 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1253 if ( sendok & LDAP_BACK_SENDERR ) {
1254 send_ldap_result( op, rs );
1256 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1259 rs->sr_err = LDAP_SUCCESS;
1260 binddn = slap_empty_bv;
1261 bindcred = slap_empty_bv;
1269 binddn = li->li_idassert_authcDN;
1270 bindcred = li->li_idassert_passwd;
1275 if ( dobind && li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1276 #ifdef HAVE_CYRUS_SASL
1277 void *defaults = NULL;
1278 struct berval authzID = BER_BVNULL;
1281 /* if SASL supports native authz, prepare for it */
1282 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1283 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1285 switch ( li->li_idassert_mode ) {
1286 case LDAP_BACK_IDASSERT_OTHERID:
1287 case LDAP_BACK_IDASSERT_OTHERDN:
1288 authzID = li->li_idassert_authzID;
1291 case LDAP_BACK_IDASSERT_ANONYMOUS:
1292 BER_BVSTR( &authzID, "dn:" );
1295 case LDAP_BACK_IDASSERT_SELF:
1296 if ( BER_BVISNULL( &ndn ) ) {
1297 /* connection is not authc'd, so don't idassert */
1298 BER_BVSTR( &authzID, "dn:" );
1301 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1302 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1303 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1304 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1305 ndn.bv_val, ndn.bv_len + 1 );
1314 if ( li->li_idassert_secprops != NULL ) {
1315 rs->sr_err = ldap_set_option( lc->lc_ld,
1316 LDAP_OPT_X_SASL_SECPROPS,
1317 (void *)li->li_idassert_secprops );
1319 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1320 rs->sr_err = LDAP_OTHER;
1321 if ( sendok & LDAP_BACK_SENDERR ) {
1322 send_ldap_result( op, rs );
1324 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1329 defaults = lutil_sasl_defaults( lc->lc_ld,
1330 li->li_idassert_sasl_mech.bv_val,
1331 li->li_idassert_sasl_realm.bv_val,
1332 li->li_idassert_authcID.bv_val,
1333 li->li_idassert_passwd.bv_val,
1336 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1337 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1338 LDAP_SASL_QUIET, lutil_sasl_interact,
1341 rs->sr_err = slap_map_api2result( rs );
1342 if ( rs->sr_err != LDAP_SUCCESS ) {
1343 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1344 if ( sendok & LDAP_BACK_SENDERR ) {
1345 send_ldap_result( op, rs );
1349 LDAP_BACK_CONN_ISBOUND_SET( lc );
1352 lutil_sasl_freedefs( defaults );
1354 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1358 #endif /* HAVE_CYRUS_SASL */
1361 switch ( li->li_idassert_authmethod ) {
1362 case LDAP_AUTH_NONE:
1363 LDAP_BACK_CONN_ISBOUND_SET( lc );
1366 case LDAP_AUTH_SIMPLE:
1367 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1368 binddn.bv_val, LDAP_SASL_SIMPLE,
1369 &bindcred, NULL, NULL, &msgid );
1374 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1375 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1376 if ( sendok & LDAP_BACK_SENDERR ) {
1377 send_ldap_result( op, rs );
1382 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1383 if ( rc == LDAP_SUCCESS ) {
1384 LDAP_BACK_CONN_ISBOUND_SET( lc );
1387 return LDAP_BACK_CONN_ISBOUND( lc );
1391 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1392 * to existing server-side controls if required; if not,
1393 * the existing server-side controls are placed in *pctrls.
1394 * The caller, after using the controls in client API
1395 * operations, if ( *pctrls != op->o_ctrls ), should
1396 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1397 * The function returns success if the control could
1398 * be added if required, or if it did nothing; in the future,
1399 * it might return some error if it failed.
1401 * if no bind took place yet, but the connection is bound
1402 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1403 * and explicitly add proxyAuthz the control to every operation
1404 * with the dn bound to the connection as control value.
1406 * If no server-side controls are defined for the operation,
1407 * simply add the proxyAuthz control; otherwise, if the
1408 * proxyAuthz control is not already set, add it as
1411 * FIXME: is controls order significant for security?
1412 * ANSWER: controls ordering and interoperability
1413 * must be indicated by the specs of each control; if none
1414 * is specified, the order is irrelevant.
1417 ldap_back_proxy_authz_ctrl(
1421 LDAPControl ***pctrls )
1423 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
1424 LDAPControl **ctrls = NULL;
1427 struct berval assertedID,
1432 rs->sr_err = LDAP_SUCCESS;
1434 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1435 * but if it is not set this test fails. We need a different
1436 * means to detect if idassert is enabled */
1437 if ( ( BER_BVISNULL( &li->li_idassert_authcID ) || BER_BVISEMPTY( &li->li_idassert_authcID ) )
1438 && ( BER_BVISNULL( &li->li_idassert_authcDN ) || BER_BVISEMPTY( &li->li_idassert_authcDN ) ) )
1443 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1447 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1448 ndn = op->o_conn->c_ndn;
1454 if ( li->li_idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1455 if ( op->o_proxy_authz ) {
1457 * FIXME: we do not want to perform proxyAuthz
1458 * on behalf of the client, because this would
1459 * be performed with "proxyauthzdn" privileges.
1461 * This might actually be too strict, since
1462 * the "proxyauthzdn" authzTo, and each entry's
1463 * authzFrom attributes may be crafted
1464 * to avoid unwanted proxyAuthz to take place.
1467 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1468 rs->sr_text = "proxyAuthz not allowed within namingContext";
1473 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1477 if ( BER_BVISNULL( &ndn ) ) {
1481 if ( BER_BVISNULL( &li->li_idassert_authcDN ) ) {
1485 } else if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1486 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1487 /* && ( !BER_BVISNULL( &ndn )
1488 || LDAP_BACK_CONN_ISBOUND( lc ) ) */ )
1490 /* already asserted in SASL via native authz */
1491 /* NOTE: the test on lc->lc_bound is used to trap
1492 * native authorization of anonymous users,
1493 * since in that case ndn is NULL */
1497 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1499 struct berval authcDN;
1501 if ( BER_BVISNULL( &ndn ) ) {
1502 authcDN = slap_empty_bv;
1506 rc = slap_sasl_matches( op, li->li_idassert_authz,
1507 &authcDN, & authcDN );
1508 if ( rc != LDAP_SUCCESS ) {
1509 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1511 /* ndn is not authorized
1512 * to use idassert */
1519 if ( op->o_proxy_authz ) {
1522 * 1) ignore the already set proxyAuthz control
1523 * 2) leave it in place, and don't set ours
1525 * 4) reject the operation
1527 * option (4) is very drastic
1528 * option (3) will make the remote server reject
1529 * the operation, thus being equivalent to (4)
1530 * option (2) will likely break the idassert
1531 * assumptions, so we cannot accept it;
1532 * option (1) means that we are contradicting
1533 * the client's reques.
1535 * I think (4) is the only correct choice.
1537 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1538 rs->sr_text = "proxyAuthz not allowed within namingContext";
1541 if ( op->o_is_auth_check ) {
1542 mode = LDAP_BACK_IDASSERT_NOASSERT;
1545 mode = li->li_idassert_mode;
1549 case LDAP_BACK_IDASSERT_SELF:
1550 if ( BER_BVISNULL( &ndn ) ) {
1556 case LDAP_BACK_IDASSERT_LEGACY:
1557 /* original behavior:
1558 * assert the client's identity */
1559 if ( BER_BVISNULL( &ndn ) ) {
1560 assertedID = slap_empty_bv;
1566 case LDAP_BACK_IDASSERT_ANONYMOUS:
1567 /* assert "anonymous" */
1568 assertedID = slap_empty_bv;
1571 case LDAP_BACK_IDASSERT_NOASSERT:
1572 /* don't assert; bind as proxyauthzdn */
1575 case LDAP_BACK_IDASSERT_OTHERID:
1576 case LDAP_BACK_IDASSERT_OTHERDN:
1577 /* assert idassert DN */
1578 assertedID = li->li_idassert_authzID;
1585 if ( BER_BVISNULL( &assertedID ) ) {
1586 assertedID = slap_empty_bv;
1589 if ( op->o_ctrls ) {
1590 for ( i = 0; op->o_ctrls[ i ]; i++ )
1591 /* just count ctrls */ ;
1594 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
1596 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
1598 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1599 ctrls[ 0 ]->ldctl_iscritical = 1;
1601 switch ( li->li_idassert_mode ) {
1602 /* already in u:ID or dn:DN form */
1603 case LDAP_BACK_IDASSERT_OTHERID:
1604 case LDAP_BACK_IDASSERT_OTHERDN:
1605 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
1608 /* needs the dn: prefix */
1610 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1611 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
1613 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1614 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1615 assertedID.bv_val, assertedID.bv_len + 1 );
1619 /* Older versions of <draft-weltman-ldapv3-proxy> required
1620 * to encode the value of the authzID (and called it proxyDN);
1621 * this hack provides compatibility with those DSAs that
1622 * implement it this way */
1623 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
1624 struct berval authzID = ctrls[ 0 ]->ldctl_value;
1625 BerElementBuffer berbuf;
1626 BerElement *ber = (BerElement *)&berbuf;
1629 ber_init2( ber, 0, LBER_USE_DER );
1630 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1632 tag = ber_printf( ber, "O", &authzID );
1633 if ( tag == LBER_ERROR ) {
1634 rs->sr_err = LDAP_OTHER;
1638 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1639 rs->sr_err = LDAP_OTHER;
1644 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1645 ber_free_buf( ber );
1647 if ( rs->sr_err != LDAP_SUCCESS ) {
1648 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1652 } else if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
1653 struct berval authzID = ctrls[ 0 ]->ldctl_value,
1655 BerElementBuffer berbuf;
1656 BerElement *ber = (BerElement *)&berbuf;
1659 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
1660 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1661 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1662 rs->sr_err = LDAP_PROTOCOL_ERROR;
1667 tmp.bv_val += STRLENOF( "dn:" );
1668 tmp.bv_len -= STRLENOF( "dn:" );
1670 ber_init2( ber, 0, LBER_USE_DER );
1671 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1673 /* apparently, Mozilla API encodes this
1674 * as "SEQUENCE { LDAPDN }" */
1675 tag = ber_printf( ber, "{O}", &tmp );
1676 if ( tag == LBER_ERROR ) {
1677 rs->sr_err = LDAP_OTHER;
1681 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1682 rs->sr_err = LDAP_OTHER;
1687 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1688 ber_free_buf( ber );
1690 if ( rs->sr_err != LDAP_SUCCESS ) {
1691 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1696 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1699 if ( op->o_ctrls ) {
1700 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1701 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1704 ctrls[ i + 1 ] = NULL;
1707 if ( ctrls == NULL ) {
1708 ctrls = op->o_ctrls;
1717 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1719 LDAPControl **ctrls = *pctrls;
1721 /* we assume that the first control is the proxyAuthz
1722 * added by back-ldap, so it's the only one we explicitly
1724 if ( ctrls && ctrls != op->o_ctrls ) {
1725 assert( ctrls[ 0 ] != NULL );
1727 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1728 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1731 op->o_tmpfree( ctrls, op->o_tmpmemctx );