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_conndnlc_cmp( const void *c1, const void *c2 );
58 ldap_back_bind( Operation *op, SlapReply *rs )
60 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
66 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR );
71 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
72 ch_free( lc->lc_bound_ndn.bv_val );
73 BER_BVZERO( &lc->lc_bound_ndn );
75 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
77 /* method is always LDAP_AUTH_SIMPLE if we got here */
78 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
80 &op->orb_cred, op->o_ctrls, NULL, &msgid );
81 rc = ldap_back_op_result( lc, op, rs, msgid, 0, LDAP_BACK_SENDERR );
83 if ( rc == LDAP_SUCCESS ) {
84 /* If defined, proxyAuthz will be used also when
85 * back-ldap is the authorizing backend; for this
86 * purpose, a successful bind is followed by a
87 * bind with the configured identity assertion */
88 /* NOTE: use with care */
89 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
90 ldap_back_proxy_authz_bind( lc, op, rs, LDAP_BACK_SENDERR );
91 if ( !LDAP_BACK_CONN_ISBOUND( lc ) ) {
97 LDAP_BACK_CONN_ISBOUND_SET( lc );
98 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
100 if ( LDAP_BACK_SAVECRED( li ) ) {
101 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
102 memset( lc->lc_cred.bv_val, 0,
103 lc->lc_cred.bv_len );
105 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
106 ldap_set_rebind_proc( lc->lc_ld, ldap_back_rebind_f, lc );
111 assert( lc->lc_binding == 1 );
114 /* must re-insert if local DN changed as result of bind */
115 if ( !LDAP_BACK_CONN_ISBOUND( lc )
116 || ( LDAP_BACK_CONN_ISBOUND( lc )
117 && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) ) )
122 /* wait for all other ops to release the connection */
124 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
125 if ( lc->lc_refcnt > 1 ) {
126 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
127 ldap_pvt_thread_yield();
131 assert( lc->lc_refcnt == 1 );
132 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
133 ldap_back_conndnlc_cmp );
134 assert( lc == tmplc );
136 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
137 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
138 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
139 ldap_back_conndn_cmp, ldap_back_conndn_dup );
142 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
148 /* duplicate; someone else successfully bound
149 * on the same connection with the same identity;
150 * we can do this because lc_refcnt == 1 */
151 ldap_back_conn_free( lc );
157 ldap_back_release_conn( op, rs, lc );
164 * ldap_back_conndn_cmp
166 * compares two ldapconn_t based on the value of the conn pointer
167 * and of the local DN; used by avl stuff for insert, lookup
171 ldap_back_conndn_cmp( const void *c1, const void *c2 )
173 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
174 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
177 /* If local DNs don't match, it is definitely not a match */
178 /* For shared sessions, conn is NULL. Only explicitly
179 * bound sessions will have non-NULL conn.
181 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
183 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
190 * ldap_back_conndnlc_cmp
192 * compares two ldapconn_t based on the value of the conn pointer,
193 * the local DN and the lc pointer; used by avl stuff for insert, lookup
197 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
199 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
200 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
203 /* If local DNs don't match, it is definitely not a match */
204 /* For shared sessions, conn is NULL. Only explicitly
205 * bound sessions will have non-NULL conn.
207 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
209 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
211 rc = SLAP_PTRCMP( lc1, lc2 );
221 * compares two ldapconn_t based on the value of the conn pointer;
222 * used by avl stuff for delete of all conns with the same connid
225 ldap_back_conn_cmp( const void *c1, const void *c2 )
227 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
228 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
230 /* For shared sessions, conn is NULL. Only explicitly
231 * bound sessions will have non-NULL conn.
233 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
237 * ldap_back_conndn_dup
239 * returns -1 in case a duplicate ldapconn_t has been inserted;
243 ldap_back_conndn_dup( void *c1, void *c2 )
245 ldapconn_t *lc1 = (ldapconn_t *)c1;
246 ldapconn_t *lc2 = (ldapconn_t *)c2;
248 /* Cannot have more than one shared session with same DN */
249 if ( lc1->lc_conn == lc2->lc_conn &&
250 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
258 #if PRINT_CONNTREE > 0
260 ravl_print( Avlnode *root, int depth )
269 ravl_print( root->avl_right, depth+1 );
271 for ( i = 0; i < depth; i++ ) {
272 fprintf( stderr, "-" );
276 fprintf( stderr, "lc=%p local=\"%s\" conn=%p %s refcnt=%d\n",
277 (void *)lc, lc->lc_local_ndn.bv_val, (void *)lc->lc_conn,
278 avl_bf2str( root->avl_bf ), lc->lc_refcnt );
280 ravl_print( root->avl_left, depth+1 );
284 myprint( Avlnode *root )
286 fprintf( stderr, "========>\n" );
289 fprintf( stderr, "\tNULL\n" );
292 ravl_print( root, 0 );
295 fprintf( stderr, "<========\n" );
297 #endif /* PRINT_CONNTREE */
300 ldap_back_freeconn( Operation *op, ldapconn_t *lc, int dolock )
302 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
306 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
309 assert( lc->lc_refcnt >= 0 );
310 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
311 ldap_back_conndnlc_cmp );
312 assert( LDAP_BACK_CONN_TAINTED( lc ) || tmplc == lc );
313 if ( lc->lc_refcnt == 0 ) {
314 ldap_back_conn_free( (void *)lc );
318 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
335 int rc = LDAP_SUCCESS;
338 /* this is ridiculous... */
339 dummy.li_flags = flags;
341 /* start TLS ("tls-[try-]{start,propagate}" statements) */
342 if ( ( LDAP_BACK_USE_TLS( &dummy ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS( &dummy ) ) )
343 && !ldap_is_ldaps_url( url ) )
345 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
347 * use asynchronous StartTLS
348 * in case, chase referral (not implemented yet)
352 if ( protocol == 0 ) {
353 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
357 if ( protocol < LDAP_VERSION3 ) {
358 protocol = LDAP_VERSION3;
359 /* Set LDAP version */
360 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
361 (const void *)&protocol );
364 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
365 if ( rc == LDAP_SUCCESS ) {
366 LDAPMessage *res = NULL;
369 LDAP_BACK_TV_SET( &tv );
372 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
374 rc = LDAP_UNAVAILABLE;
376 } else if ( rc == 0 ) {
377 if ( retries != LDAP_BACK_RETRY_NEVER ) {
378 ldap_pvt_thread_yield();
382 LDAP_BACK_TV_SET( &tv );
385 rc = LDAP_UNAVAILABLE;
387 } else if ( rc == LDAP_RES_EXTENDED ) {
388 struct berval *data = NULL;
390 rc = ldap_parse_extended_result( ld, res,
392 if ( rc == LDAP_SUCCESS ) {
394 rc = ldap_parse_result( ld, res, &err,
395 NULL, NULL, NULL, NULL, 1 );
396 if ( rc == LDAP_SUCCESS ) {
401 /* FIXME: in case a referral
402 * is returned, should we try
403 * using it instead of the
405 if ( rc == LDAP_SUCCESS ) {
406 rc = ldap_install_tls( ld );
408 } else if ( rc == LDAP_REFERRAL ) {
409 rc = LDAP_UNWILLING_TO_PERFORM;
410 *text = "unwilling to chase referral returned by Start TLS exop";
414 if ( data->bv_val ) {
415 ber_memfree( data->bv_val );
429 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
431 * use synchronous StartTLS
433 rc = ldap_start_tls_s( ld, NULL, NULL );
434 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
436 /* if StartTLS is requested, only attempt it if the URL
437 * is not "ldaps://"; this may occur not only in case
438 * of misconfiguration, but also when used in the chain
439 * overlay, where the "uri" can be parsed out of a referral */
445 case LDAP_SERVER_DOWN:
449 if ( LDAP_BACK_TLS_CRITICAL( &dummy ) ) {
450 *text = "could not start TLS";
454 /* in case Start TLS is not critical */
466 #endif /* HAVE_TLS */
469 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
471 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
472 int vers = op->o_protocol;
475 int is_tls = op->o_conn->c_is_tls;
476 #endif /* HAVE_TLS */
478 assert( lcp != NULL );
480 rs->sr_err = ldap_initialize( &ld, li->li_uri );
481 if ( rs->sr_err != LDAP_SUCCESS ) {
485 /* Set LDAP version. This will always succeed: If the client
486 * bound with a particular version, then so can we.
489 /* assume it's an internal op; set to LDAPv3 */
490 vers = LDAP_VERSION3;
492 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
494 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
495 ldap_set_option( ld, LDAP_OPT_REFERRALS,
496 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
498 if ( li->li_network_timeout > 0 ) {
501 tv.tv_sec = li->li_network_timeout;
503 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
507 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
508 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
509 if ( rs->sr_err != LDAP_SUCCESS ) {
510 ldap_unbind_ext( ld, NULL, NULL );
513 #endif /* HAVE_TLS */
515 if ( *lcp == NULL ) {
516 *lcp = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
517 (*lcp)->lc_flags= li->li_flags;
520 (*lcp)->lc_refcnt = 1;
521 (*lcp)->lc_binding = 1;
524 LDAP_BACK_CONN_ISTLS_SET( *lcp );
526 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
528 #endif /* HAVE_TLS */
531 if ( rs->sr_err != LDAP_SUCCESS ) {
532 rs->sr_err = slap_map_api2result( rs );
533 if ( sendok & LDAP_BACK_SENDERR ) {
534 if ( rs->sr_text == NULL ) {
535 rs->sr_text = "ldap_initialize() failed";
537 send_ldap_result( op, rs );
542 if ( li->li_conn_ttl > 0 ) {
543 (*lcp)->lc_create_time = op->o_time;
551 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
553 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
554 ldapconn_t *lc = NULL,
556 int refcnt = 1, binding = 1;
558 /* Internal searches are privileged and shared. So is root. */
559 if ( op->o_do_not_cache || be_isroot( op ) ) {
560 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
561 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
562 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
565 lc_curr.lc_local_ndn = op->o_ndn;
566 /* Explicit binds must not be shared */
567 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
568 lc_curr.lc_conn = op->o_conn;
571 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
575 /* Explicit Bind requests always get their own conn */
576 if ( !( sendok & LDAP_BACK_BINDING ) ) {
577 /* Searches for a ldapconn in the avl tree */
579 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
581 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
582 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
584 /* Don't reuse connections while they're still binding */
585 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
586 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
587 ldap_pvt_thread_yield();
590 refcnt = ++lc->lc_refcnt;
591 binding = ++lc->lc_binding;
593 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
596 /* Looks like we didn't get a bind. Open a new session... */
598 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
601 if ( sendok & LDAP_BACK_BINDING ) {
602 LDAP_BACK_CONN_BINDING_SET( lc );
604 lc->lc_conn = lc_curr.lc_conn;
605 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
607 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
608 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
609 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
610 LDAP_BACK_CONN_ISPRIV_SET( lc );
613 BER_BVZERO( &lc->lc_cred );
614 BER_BVZERO( &lc->lc_bound_ndn );
616 /* FIXME: if we set lc_bound_ndn = o_ndn
617 * we end up with a bind with DN but no password! */
618 if ( !BER_BVISEMPTY( &op->o_ndn )
619 && SLAP_IS_AUTHZ_BACKEND( op ) )
621 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
627 /* if start TLS failed but it was not mandatory,
628 * check if the non-TLS connection was already
629 * in cache; in case, destroy the newly created
630 * connection and use the existing one */
631 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
632 && !ldap_tls_inplace( lc->lc_ld ) )
636 lc_curr.lc_conn = LDAP_BACK_PCONN;
637 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
638 tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
639 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
640 if ( tmplc != NULL ) {
641 refcnt = ++tmplc->lc_refcnt;
642 binding = ++tmplc->lc_binding;
643 ldap_back_conn_free( lc );
646 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
648 if ( tmplc != NULL ) {
652 #endif /* HAVE_TLS */
654 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
656 /* Inserts the newly created ldapconn in the avl tree */
657 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
659 assert( lc->lc_refcnt == 1 );
660 assert( lc->lc_binding == 1 );
661 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
662 ldap_back_conndn_cmp, ldap_back_conndn_dup );
664 #if PRINT_CONNTREE > 0
665 myprint( li->li_conninfo.lai_tree );
666 #endif /* PRINT_CONNTREE */
668 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
670 Debug( LDAP_DEBUG_TRACE,
671 "=>ldap_back_getconn: conn %p inserted refcnt=%u binding=%u\n",
672 (void *)lc, refcnt, binding );
674 /* Err could be -1 in case a duplicate ldapconn is inserted */
675 switch ( rs->sr_err ) {
680 if ( !( sendok & LDAP_BACK_BINDING ) ) {
681 /* duplicate: free and try to get the newly created one */
684 /* taint connection, so that it'll be freed when released */
685 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
686 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
687 ldap_back_conndnlc_cmp );
688 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
689 LDAP_BACK_CONN_TAINTED_SET( lc );
693 ldap_back_conn_free( lc );
694 rs->sr_err = LDAP_OTHER;
695 rs->sr_text = "proxy bind collision";
696 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
697 send_ldap_result( op, rs );
704 char buf[ SLAP_TEXT_BUFLEN ];
707 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
708 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
712 /* let it be used, but taint/delete it so that
713 * no-one else can look it up any further */
714 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
715 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
716 ldap_back_conndnlc_cmp );
717 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
718 LDAP_BACK_CONN_TAINTED_SET( lc );
721 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
722 snprintf( buf, sizeof( buf ),
723 "conn %p fetched refcnt=%u binding=%u%s",
724 (void *)lc, refcnt, binding, expiring ? " expiring" : "" );
725 Debug( LDAP_DEBUG_TRACE,
726 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
730 if ( li->li_idle_timeout && lc ) {
731 lc->lc_time = op->o_time;
739 ldap_back_release_conn_lock(
745 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
748 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
750 assert( lc->lc_refcnt > 0 );
751 LDAP_BACK_CONN_BINDING_CLEAR( lc );
752 if ( --lc->lc_refcnt == 0 || LDAP_BACK_CONN_TAINTED( lc ) ) {
753 ldap_back_freeconn( op, lc, 0 );
756 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
763 * Note: as the check for the value of lc->lc_bound was already here, I removed
764 * it from all the callers, and I made the function return the flag, so
765 * it can be used to simplify the check.
767 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
770 ldap_back_dobind_int(
774 ldap_back_send_t sendok,
778 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
783 assert( retries >= 0 );
787 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
790 if ( binding == 0 ) {
791 /* check if already bound */
792 rc = LDAP_BACK_CONN_ISBOUND( lc );
796 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
801 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
802 /* if someone else is about to bind it, give up and retry */
804 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
806 ldap_pvt_thread_yield();
810 /* otherwise this thread will bind it */
811 LDAP_BACK_CONN_BINDING_SET( lc );
816 /* wait for pending operations to finish */
817 /* FIXME: may become a bottleneck! */
818 if ( lc->lc_refcnt != lc->lc_binding ) {
820 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
822 ldap_pvt_thread_yield();
827 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
831 while ( lc->lc_refcnt > 1 ) {
832 ldap_pvt_thread_yield();
833 rc = LDAP_BACK_CONN_ISBOUND( lc );
840 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
842 LDAP_BACK_CONN_BINDING_SET( lc );
844 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
849 * FIXME: we need to let clients use proxyAuthz
850 * otherwise we cannot do symmetric pools of servers;
851 * we have to live with the fact that a user can
852 * authorize itself as any ID that is allowed
853 * by the authzTo directive of the "proxyauthzdn".
856 * NOTE: current Proxy Authorization specification
857 * and implementation do not allow proxy authorization
858 * control to be provided with Bind requests
861 * if no bind took place yet, but the connection is bound
862 * and the "idassert-authcDN" (or other ID) is set,
863 * then bind as the asserting identity and explicitly
864 * add the proxyAuthz control to every operation with the
865 * dn bound to the connection as control value.
866 * This is done also if this is the authrizing backend,
867 * but the "override" flag is given to idassert.
868 * It allows to use SASL bind and yet proxyAuthz users
870 if ( op->o_conn != NULL &&
871 !op->o_do_not_cache &&
872 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
873 ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
875 (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok );
879 #ifdef HAVE_CYRUS_SASL
880 if ( LDAP_BACK_CONN_ISPRIV( lc )
881 && li->li_acl_authmethod == LDAP_AUTH_SASL )
883 void *defaults = NULL;
885 if ( li->li_acl_secprops != NULL ) {
886 rc = ldap_set_option( lc->lc_ld,
887 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops);
889 if ( rc != LDAP_OPT_SUCCESS ) {
890 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
891 "(%s,SECPROPS,\"%s\") failed!\n",
892 li->li_uri, li->li_acl_secprops, 0 );
897 defaults = lutil_sasl_defaults( lc->lc_ld,
898 li->li_acl_sasl_mech.bv_val,
899 li->li_acl_sasl_realm.bv_val,
900 li->li_acl_authcID.bv_val,
901 li->li_acl_passwd.bv_val,
904 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
905 li->li_acl_authcDN.bv_val,
906 li->li_acl_sasl_mech.bv_val, NULL, NULL,
907 LDAP_SASL_QUIET, lutil_sasl_interact,
910 lutil_sasl_freedefs( defaults );
912 rs->sr_err = slap_map_api2result( rs );
913 if ( rs->sr_err != LDAP_SUCCESS ) {
914 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
915 send_ldap_result( op, rs );
918 LDAP_BACK_CONN_ISBOUND_SET( lc );
922 #endif /* HAVE_CYRUS_SASL */
925 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
926 lc->lc_bound_ndn.bv_val,
927 LDAP_SASL_SIMPLE, &lc->lc_cred,
928 NULL, NULL, &msgid );
930 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
931 if ( retries != LDAP_BACK_RETRY_NEVER ) {
933 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
936 assert( lc->lc_refcnt > 0 );
937 if ( lc->lc_refcnt == 1 ) {
938 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
941 /* lc here must be the regular lc, reset and ready for init */
942 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
943 if ( rs->sr_err != LDAP_SUCCESS ) {
950 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
953 if ( rs->sr_err == LDAP_SUCCESS ) {
962 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
966 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
970 ldap_back_freeconn( op, lc, dolock );
971 rs->sr_err = slap_map_api2result( rs );
976 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
977 if ( rc == LDAP_SUCCESS ) {
978 LDAP_BACK_CONN_ISBOUND_SET( lc );
983 LDAP_BACK_CONN_BINDING_CLEAR( lc );
984 rc = LDAP_BACK_CONN_ISBOUND( lc );
986 ldap_back_release_conn_lock( op, rs, lc, dolock );
993 ldap_back_dobind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
995 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
997 return ldap_back_dobind_int( lc, op, rs, sendok, li->li_nretries, 1 );
1001 * ldap_back_default_rebind
1003 * This is a callback used for chasing referrals using the same
1004 * credentials as the original user on this session.
1007 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1008 ber_int_t msgid, void *params )
1010 ldapconn_t *lc = (ldapconn_t *)params;
1013 /* ... otherwise we couldn't get here */
1014 assert( lc != NULL );
1016 if ( !ldap_tls_inplace( ld ) ) {
1017 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1019 const char *text = NULL;
1021 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1022 LDAP_BACK_RETRY_DEFAULT, &text );
1023 if ( rc != LDAP_SUCCESS ) {
1027 #endif /* HAVE_TLS */
1029 /* FIXME: add checks on the URL/identity? */
1031 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
1032 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1036 ldap_back_op_result(
1042 ldap_back_send_t sendok )
1045 LDAPMessage *res = NULL;
1048 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1051 rs->sr_matched = NULL;
1053 /* if the error recorded in the reply corresponds
1054 * to a successful state, get the error from the
1055 * remote server response */
1056 if ( ERR_OK( rs->sr_err ) ) {
1061 tv.tv_sec = timeout;
1065 LDAP_BACK_TV_SET( &tv );
1069 /* if result parsing fails, note the failure reason */
1070 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1074 (void)ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1075 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
1076 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
1077 rs->sr_text = "Operation timed out";
1081 LDAP_BACK_TV_SET( &tv );
1082 ldap_pvt_thread_yield();
1086 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1091 /* otherwise get the result; if it is not
1092 * LDAP_SUCCESS, record it in the reply
1093 * structure (this includes
1094 * LDAP_COMPARE_{TRUE|FALSE}) */
1096 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1097 &match, &text, NULL, NULL, 1 );
1099 if ( rc != LDAP_SUCCESS ) {
1105 /* if the error in the reply structure is not
1106 * LDAP_SUCCESS, try to map it from client
1107 * to server error */
1108 if ( !ERR_OK( rs->sr_err ) ) {
1109 rs->sr_err = slap_map_api2result( rs );
1111 /* internal ops ( op->o_conn == NULL )
1112 * must not reply to client */
1113 if ( op->o_conn && !op->o_do_not_cache && match ) {
1115 /* record the (massaged) matched
1116 * DN into the reply structure */
1117 rs->sr_matched = match;
1121 ( ( sendok & LDAP_BACK_SENDOK )
1122 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1124 send_ldap_result( op, rs );
1127 if ( rs->sr_matched != match ) {
1128 free( (char *)rs->sr_matched );
1130 rs->sr_matched = NULL;
1131 ldap_memfree( match );
1134 ldap_memfree( text );
1137 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1140 /* return true if bound, false if failed */
1142 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1145 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1147 assert( lcp != NULL );
1148 assert( *lcp != NULL );
1150 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1152 if ( (*lcp)->lc_refcnt == 1 ) {
1153 Debug( LDAP_DEBUG_ANY,
1154 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1155 op->o_log_prefix, li->li_uri,
1156 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1157 "" : (*lcp)->lc_bound_ndn.bv_val );
1159 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1160 (*lcp)->lc_ld = NULL;
1161 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1163 /* lc here must be the regular lc, reset and ready for init */
1164 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1165 if ( rc != LDAP_SUCCESS ) {
1170 rc = ldap_back_dobind_int( *lcp, op, rs, sendok, 0, 0 );
1177 Debug( LDAP_DEBUG_TRACE,
1178 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1179 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1181 ldap_back_release_conn_lock( op, rs, *lcp, 0 );
1185 rs->sr_err = LDAP_UNAVAILABLE;
1186 rs->sr_text = "unable to retry";
1187 send_ldap_result( op, rs );
1191 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1197 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1199 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1200 struct berval binddn = slap_empty_bv;
1201 struct berval bindcred = slap_empty_bv;
1207 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1208 ndn = op->o_conn->c_ndn;
1215 * FIXME: we need to let clients use proxyAuthz
1216 * otherwise we cannot do symmetric pools of servers;
1217 * we have to live with the fact that a user can
1218 * authorize itself as any ID that is allowed
1219 * by the authzTo directive of the "proxyauthzdn".
1222 * NOTE: current Proxy Authorization specification
1223 * and implementation do not allow proxy authorization
1224 * control to be provided with Bind requests
1227 * if no bind took place yet, but the connection is bound
1228 * and the "proxyauthzdn" is set, then bind as
1229 * "proxyauthzdn" and explicitly add the proxyAuthz
1230 * control to every operation with the dn bound
1231 * to the connection as control value.
1234 /* bind as proxyauthzdn only if no idassert mode
1235 * is requested, or if the client's identity
1237 switch ( li->li_idassert_mode ) {
1238 case LDAP_BACK_IDASSERT_LEGACY:
1239 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1240 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1242 binddn = li->li_idassert_authcDN;
1243 bindcred = li->li_idassert_passwd;
1250 /* NOTE: rootdn can always idassert */
1251 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1252 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1253 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1254 if ( sendok & LDAP_BACK_SENDERR ) {
1255 send_ldap_result( op, rs );
1257 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1260 rs->sr_err = LDAP_SUCCESS;
1261 binddn = slap_empty_bv;
1262 bindcred = slap_empty_bv;
1268 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1269 struct berval authcDN;
1271 if ( BER_BVISNULL( &ndn ) ) {
1272 authcDN = slap_empty_bv;
1277 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1278 &authcDN, &authcDN );
1279 if ( rs->sr_err != LDAP_SUCCESS ) {
1280 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1281 if ( sendok & LDAP_BACK_SENDERR ) {
1282 send_ldap_result( op, rs );
1284 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1287 rs->sr_err = LDAP_SUCCESS;
1288 binddn = slap_empty_bv;
1289 bindcred = slap_empty_bv;
1297 binddn = li->li_idassert_authcDN;
1298 bindcred = li->li_idassert_passwd;
1303 if ( dobind && li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1304 #ifdef HAVE_CYRUS_SASL
1305 void *defaults = NULL;
1306 struct berval authzID = BER_BVNULL;
1309 /* if SASL supports native authz, prepare for it */
1310 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1311 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1313 switch ( li->li_idassert_mode ) {
1314 case LDAP_BACK_IDASSERT_OTHERID:
1315 case LDAP_BACK_IDASSERT_OTHERDN:
1316 authzID = li->li_idassert_authzID;
1319 case LDAP_BACK_IDASSERT_ANONYMOUS:
1320 BER_BVSTR( &authzID, "dn:" );
1323 case LDAP_BACK_IDASSERT_SELF:
1324 if ( BER_BVISNULL( &ndn ) ) {
1325 /* connection is not authc'd, so don't idassert */
1326 BER_BVSTR( &authzID, "dn:" );
1329 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1330 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1331 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1332 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1333 ndn.bv_val, ndn.bv_len + 1 );
1342 if ( li->li_idassert_secprops != NULL ) {
1343 rs->sr_err = ldap_set_option( lc->lc_ld,
1344 LDAP_OPT_X_SASL_SECPROPS,
1345 (void *)li->li_idassert_secprops );
1347 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1348 rs->sr_err = LDAP_OTHER;
1349 if ( sendok & LDAP_BACK_SENDERR ) {
1350 send_ldap_result( op, rs );
1352 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1357 defaults = lutil_sasl_defaults( lc->lc_ld,
1358 li->li_idassert_sasl_mech.bv_val,
1359 li->li_idassert_sasl_realm.bv_val,
1360 li->li_idassert_authcID.bv_val,
1361 li->li_idassert_passwd.bv_val,
1364 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1365 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1366 LDAP_SASL_QUIET, lutil_sasl_interact,
1369 rs->sr_err = slap_map_api2result( rs );
1370 if ( rs->sr_err != LDAP_SUCCESS ) {
1371 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1372 if ( sendok & LDAP_BACK_SENDERR ) {
1373 send_ldap_result( op, rs );
1377 LDAP_BACK_CONN_ISBOUND_SET( lc );
1380 lutil_sasl_freedefs( defaults );
1382 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1386 #endif /* HAVE_CYRUS_SASL */
1389 switch ( li->li_idassert_authmethod ) {
1390 case LDAP_AUTH_NONE:
1391 LDAP_BACK_CONN_ISBOUND_SET( lc );
1394 case LDAP_AUTH_SIMPLE:
1395 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1396 binddn.bv_val, LDAP_SASL_SIMPLE,
1397 &bindcred, NULL, NULL, &msgid );
1402 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1403 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1404 if ( sendok & LDAP_BACK_SENDERR ) {
1405 send_ldap_result( op, rs );
1410 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1411 if ( rc == LDAP_SUCCESS ) {
1412 LDAP_BACK_CONN_ISBOUND_SET( lc );
1415 return LDAP_BACK_CONN_ISBOUND( lc );
1419 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1420 * to existing server-side controls if required; if not,
1421 * the existing server-side controls are placed in *pctrls.
1422 * The caller, after using the controls in client API
1423 * operations, if ( *pctrls != op->o_ctrls ), should
1424 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1425 * The function returns success if the control could
1426 * be added if required, or if it did nothing; in the future,
1427 * it might return some error if it failed.
1429 * if no bind took place yet, but the connection is bound
1430 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1431 * and explicitly add proxyAuthz the control to every operation
1432 * with the dn bound to the connection as control value.
1434 * If no server-side controls are defined for the operation,
1435 * simply add the proxyAuthz control; otherwise, if the
1436 * proxyAuthz control is not already set, add it as
1439 * FIXME: is controls order significant for security?
1440 * ANSWER: controls ordering and interoperability
1441 * must be indicated by the specs of each control; if none
1442 * is specified, the order is irrelevant.
1445 ldap_back_proxy_authz_ctrl(
1449 LDAPControl ***pctrls )
1451 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
1452 LDAPControl **ctrls = NULL;
1455 struct berval assertedID,
1460 rs->sr_err = LDAP_SUCCESS;
1462 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1463 * but if it is not set this test fails. We need a different
1464 * means to detect if idassert is enabled */
1465 if ( ( BER_BVISNULL( &li->li_idassert_authcID ) || BER_BVISEMPTY( &li->li_idassert_authcID ) )
1466 && ( BER_BVISNULL( &li->li_idassert_authcDN ) || BER_BVISEMPTY( &li->li_idassert_authcDN ) ) )
1471 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1475 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1476 ndn = op->o_conn->c_ndn;
1482 if ( li->li_idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1483 if ( op->o_proxy_authz ) {
1485 * FIXME: we do not want to perform proxyAuthz
1486 * on behalf of the client, because this would
1487 * be performed with "proxyauthzdn" privileges.
1489 * This might actually be too strict, since
1490 * the "proxyauthzdn" authzTo, and each entry's
1491 * authzFrom attributes may be crafted
1492 * to avoid unwanted proxyAuthz to take place.
1495 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1496 rs->sr_text = "proxyAuthz not allowed within namingContext";
1501 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1505 if ( BER_BVISNULL( &ndn ) ) {
1509 if ( BER_BVISNULL( &li->li_idassert_authcDN ) ) {
1513 } else if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1514 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1515 /* && ( !BER_BVISNULL( &ndn )
1516 || LDAP_BACK_CONN_ISBOUND( lc ) ) */ )
1518 /* already asserted in SASL via native authz */
1519 /* NOTE: the test on lc->lc_bound is used to trap
1520 * native authorization of anonymous users,
1521 * since in that case ndn is NULL */
1525 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1527 struct berval authcDN;
1529 if ( BER_BVISNULL( &ndn ) ) {
1530 authcDN = slap_empty_bv;
1534 rc = slap_sasl_matches( op, li->li_idassert_authz,
1535 &authcDN, & authcDN );
1536 if ( rc != LDAP_SUCCESS ) {
1537 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1539 /* ndn is not authorized
1540 * to use idassert */
1547 if ( op->o_proxy_authz ) {
1550 * 1) ignore the already set proxyAuthz control
1551 * 2) leave it in place, and don't set ours
1553 * 4) reject the operation
1555 * option (4) is very drastic
1556 * option (3) will make the remote server reject
1557 * the operation, thus being equivalent to (4)
1558 * option (2) will likely break the idassert
1559 * assumptions, so we cannot accept it;
1560 * option (1) means that we are contradicting
1561 * the client's reques.
1563 * I think (4) is the only correct choice.
1565 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1566 rs->sr_text = "proxyAuthz not allowed within namingContext";
1569 if ( op->o_is_auth_check ) {
1570 mode = LDAP_BACK_IDASSERT_NOASSERT;
1573 mode = li->li_idassert_mode;
1577 case LDAP_BACK_IDASSERT_SELF:
1578 if ( BER_BVISNULL( &ndn ) ) {
1584 case LDAP_BACK_IDASSERT_LEGACY:
1585 /* original behavior:
1586 * assert the client's identity */
1587 if ( BER_BVISNULL( &ndn ) ) {
1588 assertedID = slap_empty_bv;
1594 case LDAP_BACK_IDASSERT_ANONYMOUS:
1595 /* assert "anonymous" */
1596 assertedID = slap_empty_bv;
1599 case LDAP_BACK_IDASSERT_NOASSERT:
1600 /* don't assert; bind as proxyauthzdn */
1603 case LDAP_BACK_IDASSERT_OTHERID:
1604 case LDAP_BACK_IDASSERT_OTHERDN:
1605 /* assert idassert DN */
1606 assertedID = li->li_idassert_authzID;
1613 if ( BER_BVISNULL( &assertedID ) ) {
1614 assertedID = slap_empty_bv;
1617 if ( op->o_ctrls ) {
1618 for ( i = 0; op->o_ctrls[ i ]; i++ )
1619 /* just count ctrls */ ;
1622 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
1624 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
1626 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1627 ctrls[ 0 ]->ldctl_iscritical = 1;
1629 switch ( li->li_idassert_mode ) {
1630 /* already in u:ID or dn:DN form */
1631 case LDAP_BACK_IDASSERT_OTHERID:
1632 case LDAP_BACK_IDASSERT_OTHERDN:
1633 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
1636 /* needs the dn: prefix */
1638 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1639 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
1641 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1642 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1643 assertedID.bv_val, assertedID.bv_len + 1 );
1647 /* Older versions of <draft-weltman-ldapv3-proxy> required
1648 * to encode the value of the authzID (and called it proxyDN);
1649 * this hack provides compatibility with those DSAs that
1650 * implement it this way */
1651 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
1652 struct berval authzID = ctrls[ 0 ]->ldctl_value;
1653 BerElementBuffer berbuf;
1654 BerElement *ber = (BerElement *)&berbuf;
1657 ber_init2( ber, 0, LBER_USE_DER );
1658 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1660 tag = ber_printf( ber, "O", &authzID );
1661 if ( tag == LBER_ERROR ) {
1662 rs->sr_err = LDAP_OTHER;
1666 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1667 rs->sr_err = LDAP_OTHER;
1672 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1673 ber_free_buf( ber );
1675 if ( rs->sr_err != LDAP_SUCCESS ) {
1676 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1680 } else if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
1681 struct berval authzID = ctrls[ 0 ]->ldctl_value,
1683 BerElementBuffer berbuf;
1684 BerElement *ber = (BerElement *)&berbuf;
1687 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
1688 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1689 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1690 rs->sr_err = LDAP_PROTOCOL_ERROR;
1695 tmp.bv_val += STRLENOF( "dn:" );
1696 tmp.bv_len -= STRLENOF( "dn:" );
1698 ber_init2( ber, 0, LBER_USE_DER );
1699 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1701 /* apparently, Mozilla API encodes this
1702 * as "SEQUENCE { LDAPDN }" */
1703 tag = ber_printf( ber, "{O}", &tmp );
1704 if ( tag == LBER_ERROR ) {
1705 rs->sr_err = LDAP_OTHER;
1709 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1710 rs->sr_err = LDAP_OTHER;
1715 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1716 ber_free_buf( ber );
1718 if ( rs->sr_err != LDAP_SUCCESS ) {
1719 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1724 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1727 if ( op->o_ctrls ) {
1728 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1729 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1732 ctrls[ i + 1 ] = NULL;
1735 if ( ctrls == NULL ) {
1736 ctrls = op->o_ctrls;
1745 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1747 LDAPControl **ctrls = *pctrls;
1749 /* we assume that the first control is the proxyAuthz
1750 * added by back-ldap, so it's the only one we explicitly
1752 if ( ctrls && ctrls != op->o_ctrls ) {
1753 assert( ctrls[ 0 ] != NULL );
1755 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1756 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1759 op->o_tmpfree( ctrls, op->o_tmpmemctx );