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 #if PRINT_CONNTREE > 0
46 ravl_print( Avlnode *root, int depth )
55 ravl_print( root->avl_right, depth+1 );
57 for ( i = 0; i < depth; i++ ) {
58 fprintf( stderr, "-" );
62 fprintf( stderr, "lc=%p local=\"%s\" conn=%p %s refcnt=%d\n",
64 lc->lc_local_ndn.bv_val ? lc->lc_local_ndn.bv_val : "",
66 avl_bf2str( root->avl_bf ), lc->lc_refcnt );
68 ravl_print( root->avl_left, depth+1 );
72 myprint( Avlnode *root, char *msg )
74 fprintf( stderr, "========> %s\n", msg );
77 fprintf( stderr, "\tNULL\n" );
80 ravl_print( root, 0 );
83 fprintf( stderr, "<======== %s\n", msg );
85 #endif /* PRINT_CONNTREE */
88 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
91 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
94 ldap_back_conndnlc_cmp( const void *c1, const void *c2 );
97 ldap_back_bind( Operation *op, SlapReply *rs )
99 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
105 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR );
110 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
111 ch_free( lc->lc_bound_ndn.bv_val );
112 BER_BVZERO( &lc->lc_bound_ndn );
114 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
116 /* method is always LDAP_AUTH_SIMPLE if we got here */
117 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
119 &op->orb_cred, op->o_ctrls, NULL, &msgid );
120 rc = ldap_back_op_result( lc, op, rs, msgid, 0, LDAP_BACK_SENDERR );
122 if ( rc == LDAP_SUCCESS ) {
123 /* If defined, proxyAuthz will be used also when
124 * back-ldap is the authorizing backend; for this
125 * purpose, a successful bind is followed by a
126 * bind with the configured identity assertion */
127 /* NOTE: use with care */
128 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
129 ldap_back_proxy_authz_bind( lc, op, rs, LDAP_BACK_SENDERR );
130 if ( !LDAP_BACK_CONN_ISBOUND( lc ) ) {
136 LDAP_BACK_CONN_ISBOUND_SET( lc );
137 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
139 if ( LDAP_BACK_SAVECRED( li ) ) {
140 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
141 memset( lc->lc_cred.bv_val, 0,
142 lc->lc_cred.bv_len );
144 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
145 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
150 assert( lc->lc_binding == 1 );
153 /* must re-insert if local DN changed as result of bind */
154 if ( !LDAP_BACK_CONN_ISBOUND( lc )
155 || ( LDAP_BACK_CONN_ISBOUND( lc )
156 && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) ) )
161 /* wait for all other ops to release the connection */
163 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
164 if ( lc->lc_refcnt > 1 ) {
165 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
166 ldap_pvt_thread_yield();
170 assert( lc->lc_refcnt == 1 );
171 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
172 ldap_back_conndnlc_cmp );
173 assert( tmplc == NULL || lc == tmplc );
175 /* delete all cached connections with the current connection */
176 if ( LDAP_BACK_SINGLECONN( li ) ) {
177 while ( ( tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc, ldap_back_conn_cmp ) ) != NULL )
179 Debug( LDAP_DEBUG_TRACE,
180 "=>ldap_back_bind: destroying conn %ld (refcnt=%u)\n",
181 LDAP_BACK_PCONN_ID( lc->lc_conn ), lc->lc_refcnt, 0 );
183 if ( lc->lc_refcnt != 0 ) {
185 LDAP_BACK_CONN_TAINTED_SET( tmplc );
189 * Needs a test because the handler may be corrupted,
190 * and calling ldap_unbind on a corrupted header results
191 * in a segmentation fault
193 ldap_back_conn_free( tmplc );
198 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
199 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
200 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
201 lc->lc_conn = LDAP_BACK_PCONN_SET( op );
203 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
204 ldap_back_conndn_cmp, ldap_back_conndn_dup );
207 #if PRINT_CONNTREE > 0
208 myprint( li->li_conninfo.lai_tree, "ldap_back_bind" );
209 #endif /* PRINT_CONNTREE */
211 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
217 /* duplicate; someone else successfully bound
218 * on the same connection with the same identity;
219 * we can do this because lc_refcnt == 1 */
220 ldap_back_conn_free( lc );
226 ldap_back_release_conn( op, rs, lc );
233 * ldap_back_conndn_cmp
235 * compares two ldapconn_t based on the value of the conn pointer
236 * and of the local DN; used by avl stuff for insert, lookup
240 ldap_back_conndn_cmp( const void *c1, const void *c2 )
242 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
243 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
246 /* If local DNs don't match, it is definitely not a match */
247 /* For shared sessions, conn is NULL. Only explicitly
248 * bound sessions will have non-NULL conn.
250 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
252 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
259 * ldap_back_conndnlc_cmp
261 * compares two ldapconn_t based on the value of the conn pointer,
262 * the local DN and the lc pointer; used by avl stuff for insert, lookup
266 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
268 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
269 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
272 /* If local DNs don't match, it is definitely not a match */
273 /* For shared sessions, conn is NULL. Only explicitly
274 * bound sessions will have non-NULL conn.
276 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
278 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
280 rc = SLAP_PTRCMP( lc1, lc2 );
290 * compares two ldapconn_t based on the value of the conn pointer;
291 * used by avl stuff for delete of all conns with the same connid
294 ldap_back_conn_cmp( const void *c1, const void *c2 )
296 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
297 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
299 /* For shared sessions, conn is NULL. Only explicitly
300 * bound sessions will have non-NULL conn.
302 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
306 * ldap_back_conndn_dup
308 * returns -1 in case a duplicate ldapconn_t has been inserted;
312 ldap_back_conndn_dup( void *c1, void *c2 )
314 ldapconn_t *lc1 = (ldapconn_t *)c1;
315 ldapconn_t *lc2 = (ldapconn_t *)c2;
317 /* Cannot have more than one shared session with same DN */
318 if ( lc1->lc_conn == lc2->lc_conn &&
319 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
328 ldap_back_freeconn( Operation *op, ldapconn_t *lc, int dolock )
330 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
334 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
337 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
338 ldap_back_conndnlc_cmp );
339 assert( LDAP_BACK_CONN_TAINTED( lc ) || tmplc == lc );
340 if ( lc->lc_refcnt == 0 ) {
341 ldap_back_conn_free( (void *)lc );
345 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
362 int rc = LDAP_SUCCESS;
365 /* this is ridiculous... */
366 dummy.li_flags = flags;
368 /* start TLS ("tls-[try-]{start,propagate}" statements) */
369 if ( ( LDAP_BACK_USE_TLS( &dummy ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS( &dummy ) ) )
370 && !ldap_is_ldaps_url( url ) )
372 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
374 * use asynchronous StartTLS
375 * in case, chase referral (not implemented yet)
379 if ( protocol == 0 ) {
380 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
384 if ( protocol < LDAP_VERSION3 ) {
385 /* we should rather bail out... */
386 rc = LDAP_UNWILLING_TO_PERFORM;
387 *text = "invalid protocol version";
390 if ( rc == LDAP_SUCCESS ) {
391 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
394 if ( rc == LDAP_SUCCESS ) {
395 LDAPMessage *res = NULL;
398 LDAP_BACK_TV_SET( &tv );
401 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
403 rc = LDAP_UNAVAILABLE;
405 } else if ( rc == 0 ) {
406 if ( retries != LDAP_BACK_RETRY_NEVER ) {
407 ldap_pvt_thread_yield();
411 LDAP_BACK_TV_SET( &tv );
414 rc = LDAP_UNAVAILABLE;
416 } else if ( rc == LDAP_RES_EXTENDED ) {
417 struct berval *data = NULL;
419 rc = ldap_parse_extended_result( ld, res,
421 if ( rc == LDAP_SUCCESS ) {
423 rc = ldap_parse_result( ld, res, &err,
424 NULL, NULL, NULL, NULL, 1 );
425 if ( rc == LDAP_SUCCESS ) {
430 /* FIXME: in case a referral
431 * is returned, should we try
432 * using it instead of the
434 if ( rc == LDAP_SUCCESS ) {
435 rc = ldap_install_tls( ld );
437 } else if ( rc == LDAP_REFERRAL ) {
438 rc = LDAP_UNWILLING_TO_PERFORM;
439 *text = "unwilling to chase referral returned by Start TLS exop";
443 if ( data->bv_val ) {
444 ber_memfree( data->bv_val );
458 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
460 * use synchronous StartTLS
462 rc = ldap_start_tls_s( ld, NULL, NULL );
463 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
465 /* if StartTLS is requested, only attempt it if the URL
466 * is not "ldaps://"; this may occur not only in case
467 * of misconfiguration, but also when used in the chain
468 * overlay, where the "uri" can be parsed out of a referral */
474 case LDAP_SERVER_DOWN:
478 if ( LDAP_BACK_TLS_CRITICAL( &dummy ) ) {
479 *text = "could not start TLS";
483 /* in case Start TLS is not critical */
495 #endif /* HAVE_TLS */
498 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
500 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
504 int is_tls = op->o_conn->c_is_tls;
505 #endif /* HAVE_TLS */
507 assert( lcp != NULL );
509 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
510 rs->sr_err = ldap_initialize( &ld, li->li_uri );
511 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
512 if ( rs->sr_err != LDAP_SUCCESS ) {
516 if ( li->li_urllist_f ) {
517 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
520 /* Set LDAP version. This will always succeed: If the client
521 * bound with a particular version, then so can we.
523 if ( li->li_version != 0 ) {
524 version = li->li_version;
526 } else if ( op->o_protocol != 0 ) {
527 version = op->o_protocol;
530 /* assume it's an internal op; set to LDAPv3 */
531 version = LDAP_VERSION3;
533 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
535 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
536 ldap_set_option( ld, LDAP_OPT_REFERRALS,
537 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
539 if ( li->li_network_timeout > 0 ) {
542 tv.tv_sec = li->li_network_timeout;
544 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
548 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
549 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
550 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
551 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
552 if ( rs->sr_err != LDAP_SUCCESS ) {
553 ldap_unbind_ext( ld, NULL, NULL );
556 #endif /* HAVE_TLS */
558 if ( *lcp == NULL ) {
559 *lcp = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
560 (*lcp)->lc_flags = li->li_flags;
563 (*lcp)->lc_refcnt = 1;
564 (*lcp)->lc_binding = 1;
567 LDAP_BACK_CONN_ISTLS_SET( *lcp );
569 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
571 #endif /* HAVE_TLS */
574 if ( rs->sr_err != LDAP_SUCCESS ) {
575 rs->sr_err = slap_map_api2result( rs );
576 if ( sendok & LDAP_BACK_SENDERR ) {
577 if ( rs->sr_text == NULL ) {
578 rs->sr_text = "ldap_initialize() failed";
580 send_ldap_result( op, rs );
585 if ( li->li_conn_ttl > 0 ) {
586 (*lcp)->lc_create_time = op->o_time;
594 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
596 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
597 ldapconn_t *lc = NULL,
599 int refcnt = 1, binding = 1;
601 /* Internal searches are privileged and shared. So is root. */
602 if ( op->o_do_not_cache || be_isroot( op ) ) {
603 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
604 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
605 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
608 lc_curr.lc_local_ndn = op->o_ndn;
609 /* Explicit binds must not be shared */
610 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
611 lc_curr.lc_conn = op->o_conn;
614 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
618 /* Explicit Bind requests always get their own conn */
619 if ( !( sendok & LDAP_BACK_BINDING ) ) {
620 /* Searches for a ldapconn in the avl tree */
622 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
624 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
625 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
627 /* Don't reuse connections while they're still binding */
628 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
629 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
630 ldap_pvt_thread_yield();
633 refcnt = ++lc->lc_refcnt;
634 binding = ++lc->lc_binding;
636 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
639 /* Looks like we didn't get a bind. Open a new session... */
641 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
644 if ( sendok & LDAP_BACK_BINDING ) {
645 LDAP_BACK_CONN_BINDING_SET( lc );
647 lc->lc_conn = lc_curr.lc_conn;
648 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
650 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
651 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
652 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
653 LDAP_BACK_CONN_ISPRIV_SET( lc );
656 BER_BVZERO( &lc->lc_cred );
657 BER_BVZERO( &lc->lc_bound_ndn );
659 /* FIXME: if we set lc_bound_ndn = o_ndn
660 * we end up with a bind with DN but no password! */
661 if ( !BER_BVISEMPTY( &op->o_ndn )
662 && SLAP_IS_AUTHZ_BACKEND( op ) )
664 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
670 /* if start TLS failed but it was not mandatory,
671 * check if the non-TLS connection was already
672 * in cache; in case, destroy the newly created
673 * connection and use the existing one */
674 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
675 && !ldap_tls_inplace( lc->lc_ld ) )
679 lc_curr.lc_conn = LDAP_BACK_PCONN;
680 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
681 tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
682 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
683 if ( tmplc != NULL ) {
684 refcnt = ++tmplc->lc_refcnt;
685 binding = ++tmplc->lc_binding;
686 ldap_back_conn_free( lc );
689 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
691 if ( tmplc != NULL ) {
695 #endif /* HAVE_TLS */
697 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
699 /* Inserts the newly created ldapconn in the avl tree */
700 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
702 assert( lc->lc_refcnt == 1 );
703 assert( lc->lc_binding == 1 );
704 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
705 ldap_back_conndn_cmp, ldap_back_conndn_dup );
707 #if PRINT_CONNTREE > 0
708 myprint( li->li_conninfo.lai_tree, "ldap_back_getconn" );
709 #endif /* PRINT_CONNTREE */
711 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
713 Debug( LDAP_DEBUG_TRACE,
714 "=>ldap_back_getconn: conn %p inserted refcnt=%u binding=%u\n",
715 (void *)lc, refcnt, binding );
717 /* Err could be -1 in case a duplicate ldapconn is inserted */
718 switch ( rs->sr_err ) {
723 if ( !( sendok & LDAP_BACK_BINDING ) ) {
724 /* duplicate: free and try to get the newly created one */
727 /* taint connection, so that it'll be freed when released */
728 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
729 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
730 ldap_back_conndnlc_cmp );
731 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
732 LDAP_BACK_CONN_TAINTED_SET( lc );
736 ldap_back_conn_free( lc );
737 rs->sr_err = LDAP_OTHER;
738 rs->sr_text = "proxy bind collision";
739 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
740 send_ldap_result( op, rs );
747 char buf[ SLAP_TEXT_BUFLEN ];
750 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
751 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
755 /* let it be used, but taint/delete it so that
756 * no-one else can look it up any further */
757 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
758 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
759 ldap_back_conndnlc_cmp );
760 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
761 LDAP_BACK_CONN_TAINTED_SET( lc );
764 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
765 snprintf( buf, sizeof( buf ),
766 "conn %p fetched refcnt=%u binding=%u%s",
767 (void *)lc, refcnt, binding, expiring ? " expiring" : "" );
768 Debug( LDAP_DEBUG_TRACE,
769 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
776 #endif /* HAVE_TLS */
777 if ( li->li_idle_timeout && lc ) {
778 lc->lc_time = op->o_time;
785 ldap_back_release_conn_lock(
791 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
794 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
796 assert( lc->lc_refcnt > 0 );
797 LDAP_BACK_CONN_BINDING_CLEAR( lc );
799 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
800 ldap_back_freeconn( op, lc, 0 );
803 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
810 * Note: as the check for the value of lc->lc_bound was already here, I removed
811 * it from all the callers, and I made the function return the flag, so
812 * it can be used to simplify the check.
814 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
817 ldap_back_dobind_int(
821 ldap_back_send_t sendok,
825 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
830 assert( retries >= 0 );
834 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
837 if ( binding == 0 ) {
838 /* check if already bound */
839 rc = LDAP_BACK_CONN_ISBOUND( lc );
843 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
848 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
849 /* if someone else is about to bind it, give up and retry */
851 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
853 ldap_pvt_thread_yield();
857 /* otherwise this thread will bind it */
858 LDAP_BACK_CONN_BINDING_SET( lc );
863 /* wait for pending operations to finish */
864 /* FIXME: may become a bottleneck! */
865 if ( lc->lc_refcnt != lc->lc_binding ) {
867 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
869 ldap_pvt_thread_yield();
874 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
878 while ( lc->lc_refcnt > 1 ) {
879 ldap_pvt_thread_yield();
880 rc = LDAP_BACK_CONN_ISBOUND( lc );
887 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
889 LDAP_BACK_CONN_BINDING_SET( lc );
891 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
896 * FIXME: we need to let clients use proxyAuthz
897 * otherwise we cannot do symmetric pools of servers;
898 * we have to live with the fact that a user can
899 * authorize itself as any ID that is allowed
900 * by the authzTo directive of the "proxyauthzdn".
903 * NOTE: current Proxy Authorization specification
904 * and implementation do not allow proxy authorization
905 * control to be provided with Bind requests
908 * if no bind took place yet, but the connection is bound
909 * and the "idassert-authcDN" (or other ID) is set,
910 * then bind as the asserting identity and explicitly
911 * add the proxyAuthz control to every operation with the
912 * dn bound to the connection as control value.
913 * This is done also if this is the authrizing backend,
914 * but the "override" flag is given to idassert.
915 * It allows to use SASL bind and yet proxyAuthz users
917 if ( op->o_conn != NULL &&
918 !op->o_do_not_cache &&
919 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
920 ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
922 (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok );
926 #ifdef HAVE_CYRUS_SASL
927 if ( LDAP_BACK_CONN_ISPRIV( lc )
928 && li->li_acl_authmethod == LDAP_AUTH_SASL )
930 void *defaults = NULL;
932 if ( li->li_acl_secprops != NULL ) {
933 rc = ldap_set_option( lc->lc_ld,
934 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops);
936 if ( rc != LDAP_OPT_SUCCESS ) {
937 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
938 "(SECPROPS,\"%s\") failed!\n",
939 li->li_acl_secprops, 0, 0 );
944 defaults = lutil_sasl_defaults( lc->lc_ld,
945 li->li_acl_sasl_mech.bv_val,
946 li->li_acl_sasl_realm.bv_val,
947 li->li_acl_authcID.bv_val,
948 li->li_acl_passwd.bv_val,
951 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
952 li->li_acl_authcDN.bv_val,
953 li->li_acl_sasl_mech.bv_val, NULL, NULL,
954 LDAP_SASL_QUIET, lutil_sasl_interact,
957 lutil_sasl_freedefs( defaults );
959 rs->sr_err = slap_map_api2result( rs );
960 if ( rs->sr_err != LDAP_SUCCESS ) {
961 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
962 send_ldap_result( op, rs );
965 LDAP_BACK_CONN_ISBOUND_SET( lc );
969 #endif /* HAVE_CYRUS_SASL */
972 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
973 lc->lc_bound_ndn.bv_val,
974 LDAP_SASL_SIMPLE, &lc->lc_cred,
975 NULL, NULL, &msgid );
977 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
978 if ( retries != LDAP_BACK_RETRY_NEVER ) {
980 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
983 assert( lc->lc_refcnt > 0 );
984 if ( lc->lc_refcnt == 1 ) {
985 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
988 /* lc here must be the regular lc, reset and ready for init */
989 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
990 if ( rs->sr_err != LDAP_SUCCESS ) {
997 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1000 if ( rs->sr_err == LDAP_SUCCESS ) {
1001 if ( retries > 0 ) {
1009 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1013 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1018 ldap_back_freeconn( op, lc, dolock );
1019 rs->sr_err = slap_map_api2result( rs );
1024 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1025 if ( rc == LDAP_SUCCESS ) {
1026 LDAP_BACK_CONN_ISBOUND_SET( lc );
1031 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1032 rc = LDAP_BACK_CONN_ISBOUND( lc );
1034 ldap_back_release_conn_lock( op, rs, lc, dolock );
1041 ldap_back_dobind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1043 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1045 return ldap_back_dobind_int( lc, op, rs, sendok, li->li_nretries, 1 );
1049 * ldap_back_default_rebind
1051 * This is a callback used for chasing referrals using the same
1052 * credentials as the original user on this session.
1055 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1056 ber_int_t msgid, void *params )
1058 ldapconn_t *lc = (ldapconn_t *)params;
1061 /* ... otherwise we couldn't get here */
1062 assert( lc != NULL );
1064 if ( !ldap_tls_inplace( ld ) ) {
1065 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1067 const char *text = NULL;
1069 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1070 LDAP_BACK_RETRY_DEFAULT, &text );
1071 if ( rc != LDAP_SUCCESS ) {
1075 #endif /* HAVE_TLS */
1077 /* FIXME: add checks on the URL/identity? */
1079 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
1080 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1084 * ldap_back_default_urllist
1087 ldap_back_default_urllist(
1089 LDAPURLDesc **urllist,
1093 ldapinfo_t *li = (ldapinfo_t *)params;
1094 LDAPURLDesc **urltail;
1096 if ( urllist == url ) {
1097 return LDAP_SUCCESS;
1100 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1103 *urltail = *urllist;
1107 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1109 ch_free( li->li_uri );
1112 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1113 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1115 return LDAP_SUCCESS;
1119 ldap_back_op_result(
1125 ldap_back_send_t sendok )
1128 LDAPMessage *res = NULL;
1131 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1134 rs->sr_matched = NULL;
1136 /* if the error recorded in the reply corresponds
1137 * to a successful state, get the error from the
1138 * remote server response */
1139 if ( ERR_OK( rs->sr_err ) ) {
1144 tv.tv_sec = timeout;
1148 LDAP_BACK_TV_SET( &tv );
1152 /* if result parsing fails, note the failure reason */
1153 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1157 (void)ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1158 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
1159 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
1160 rs->sr_text = "Operation timed out";
1164 LDAP_BACK_TV_SET( &tv );
1165 ldap_pvt_thread_yield();
1169 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1174 /* otherwise get the result; if it is not
1175 * LDAP_SUCCESS, record it in the reply
1176 * structure (this includes
1177 * LDAP_COMPARE_{TRUE|FALSE}) */
1179 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1180 &match, &text, NULL, NULL, 1 );
1182 if ( rc != LDAP_SUCCESS ) {
1188 /* if the error in the reply structure is not
1189 * LDAP_SUCCESS, try to map it from client
1190 * to server error */
1191 if ( !ERR_OK( rs->sr_err ) ) {
1192 rs->sr_err = slap_map_api2result( rs );
1194 /* internal ops ( op->o_conn == NULL )
1195 * must not reply to client */
1196 if ( op->o_conn && !op->o_do_not_cache && match ) {
1198 /* record the (massaged) matched
1199 * DN into the reply structure */
1200 rs->sr_matched = match;
1204 ( ( sendok & LDAP_BACK_SENDOK )
1205 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1207 send_ldap_result( op, rs );
1210 if ( rs->sr_matched != match ) {
1211 free( (char *)rs->sr_matched );
1213 rs->sr_matched = NULL;
1214 ldap_memfree( match );
1217 ldap_memfree( text );
1220 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1223 /* return true if bound, false if failed */
1225 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1228 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1230 assert( lcp != NULL );
1231 assert( *lcp != NULL );
1233 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1235 if ( (*lcp)->lc_refcnt == 1 ) {
1236 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1237 Debug( LDAP_DEBUG_ANY,
1238 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1239 op->o_log_prefix, li->li_uri,
1240 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1241 "" : (*lcp)->lc_bound_ndn.bv_val );
1242 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1244 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1245 (*lcp)->lc_ld = NULL;
1246 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1248 /* lc here must be the regular lc, reset and ready for init */
1249 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1250 if ( rc != LDAP_SUCCESS ) {
1255 rc = ldap_back_dobind_int( *lcp, op, rs, sendok, 0, 0 );
1262 Debug( LDAP_DEBUG_TRACE,
1263 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1264 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1266 ldap_back_release_conn_lock( op, rs, *lcp, 0 );
1270 rs->sr_err = LDAP_UNAVAILABLE;
1271 rs->sr_text = "unable to retry";
1272 send_ldap_result( op, rs );
1276 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1282 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1284 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1285 struct berval binddn = slap_empty_bv;
1286 struct berval bindcred = slap_empty_bv;
1292 /* don't proxyAuthz if protocol is not LDAPv3 */
1293 switch ( li->li_version ) {
1298 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1307 if ( op->o_tag == LDAP_REQ_BIND ) {
1308 ndn = op->o_req_ndn;
1310 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1311 ndn = op->o_conn->c_ndn;
1318 * FIXME: we need to let clients use proxyAuthz
1319 * otherwise we cannot do symmetric pools of servers;
1320 * we have to live with the fact that a user can
1321 * authorize itself as any ID that is allowed
1322 * by the authzTo directive of the "proxyauthzdn".
1325 * NOTE: current Proxy Authorization specification
1326 * and implementation do not allow proxy authorization
1327 * control to be provided with Bind requests
1330 * if no bind took place yet, but the connection is bound
1331 * and the "proxyauthzdn" is set, then bind as
1332 * "proxyauthzdn" and explicitly add the proxyAuthz
1333 * control to every operation with the dn bound
1334 * to the connection as control value.
1337 /* bind as proxyauthzdn only if no idassert mode
1338 * is requested, or if the client's identity
1340 switch ( li->li_idassert_mode ) {
1341 case LDAP_BACK_IDASSERT_LEGACY:
1342 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1343 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1345 binddn = li->li_idassert_authcDN;
1346 bindcred = li->li_idassert_passwd;
1353 /* NOTE: rootdn can always idassert */
1354 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1355 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1356 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1357 if ( sendok & LDAP_BACK_SENDERR ) {
1358 send_ldap_result( op, rs );
1360 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1363 rs->sr_err = LDAP_SUCCESS;
1364 binddn = slap_empty_bv;
1365 bindcred = slap_empty_bv;
1371 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1372 struct berval authcDN;
1374 if ( BER_BVISNULL( &ndn ) ) {
1375 authcDN = slap_empty_bv;
1380 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1381 &authcDN, &authcDN );
1382 if ( rs->sr_err != LDAP_SUCCESS ) {
1383 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1384 if ( sendok & LDAP_BACK_SENDERR ) {
1385 send_ldap_result( op, rs );
1387 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1390 rs->sr_err = LDAP_SUCCESS;
1391 binddn = slap_empty_bv;
1392 bindcred = slap_empty_bv;
1400 binddn = li->li_idassert_authcDN;
1401 bindcred = li->li_idassert_passwd;
1406 if ( dobind && li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1407 #ifdef HAVE_CYRUS_SASL
1408 void *defaults = NULL;
1409 struct berval authzID = BER_BVNULL;
1412 /* if SASL supports native authz, prepare for it */
1413 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1414 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1416 switch ( li->li_idassert_mode ) {
1417 case LDAP_BACK_IDASSERT_OTHERID:
1418 case LDAP_BACK_IDASSERT_OTHERDN:
1419 authzID = li->li_idassert_authzID;
1422 case LDAP_BACK_IDASSERT_ANONYMOUS:
1423 BER_BVSTR( &authzID, "dn:" );
1426 case LDAP_BACK_IDASSERT_SELF:
1427 if ( BER_BVISNULL( &ndn ) ) {
1428 /* connection is not authc'd, so don't idassert */
1429 BER_BVSTR( &authzID, "dn:" );
1432 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1433 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1434 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1435 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1436 ndn.bv_val, ndn.bv_len + 1 );
1445 if ( li->li_idassert_secprops != NULL ) {
1446 rs->sr_err = ldap_set_option( lc->lc_ld,
1447 LDAP_OPT_X_SASL_SECPROPS,
1448 (void *)li->li_idassert_secprops );
1450 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1451 rs->sr_err = LDAP_OTHER;
1452 if ( sendok & LDAP_BACK_SENDERR ) {
1453 send_ldap_result( op, rs );
1455 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1460 defaults = lutil_sasl_defaults( lc->lc_ld,
1461 li->li_idassert_sasl_mech.bv_val,
1462 li->li_idassert_sasl_realm.bv_val,
1463 li->li_idassert_authcID.bv_val,
1464 li->li_idassert_passwd.bv_val,
1467 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1468 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1469 LDAP_SASL_QUIET, lutil_sasl_interact,
1472 rs->sr_err = slap_map_api2result( rs );
1473 if ( rs->sr_err != LDAP_SUCCESS ) {
1474 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1475 if ( sendok & LDAP_BACK_SENDERR ) {
1476 send_ldap_result( op, rs );
1480 LDAP_BACK_CONN_ISBOUND_SET( lc );
1483 lutil_sasl_freedefs( defaults );
1485 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1489 #endif /* HAVE_CYRUS_SASL */
1492 switch ( li->li_idassert_authmethod ) {
1493 case LDAP_AUTH_NONE:
1494 LDAP_BACK_CONN_ISBOUND_SET( lc );
1497 case LDAP_AUTH_SIMPLE:
1498 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1499 binddn.bv_val, LDAP_SASL_SIMPLE,
1500 &bindcred, NULL, NULL, &msgid );
1505 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1506 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1507 if ( sendok & LDAP_BACK_SENDERR ) {
1508 send_ldap_result( op, rs );
1513 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1514 if ( rc == LDAP_SUCCESS ) {
1515 LDAP_BACK_CONN_ISBOUND_SET( lc );
1518 return LDAP_BACK_CONN_ISBOUND( lc );
1522 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1523 * to existing server-side controls if required; if not,
1524 * the existing server-side controls are placed in *pctrls.
1525 * The caller, after using the controls in client API
1526 * operations, if ( *pctrls != op->o_ctrls ), should
1527 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1528 * The function returns success if the control could
1529 * be added if required, or if it did nothing; in the future,
1530 * it might return some error if it failed.
1532 * if no bind took place yet, but the connection is bound
1533 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1534 * and explicitly add proxyAuthz the control to every operation
1535 * with the dn bound to the connection as control value.
1537 * If no server-side controls are defined for the operation,
1538 * simply add the proxyAuthz control; otherwise, if the
1539 * proxyAuthz control is not already set, add it as
1542 * FIXME: is controls order significant for security?
1543 * ANSWER: controls ordering and interoperability
1544 * must be indicated by the specs of each control; if none
1545 * is specified, the order is irrelevant.
1548 ldap_back_proxy_authz_ctrl(
1552 LDAPControl ***pctrls )
1554 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
1555 LDAPControl **ctrls = NULL;
1558 struct berval assertedID,
1563 rs->sr_err = LDAP_SUCCESS;
1565 /* don't proxyAuthz if protocol is not LDAPv3 */
1566 switch ( li->li_version ) {
1571 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1580 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1581 * but if it is not set this test fails. We need a different
1582 * means to detect if idassert is enabled */
1583 if ( ( BER_BVISNULL( &li->li_idassert_authcID ) || BER_BVISEMPTY( &li->li_idassert_authcID ) )
1584 && ( BER_BVISNULL( &li->li_idassert_authcDN ) || BER_BVISEMPTY( &li->li_idassert_authcDN ) ) )
1589 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1593 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1594 ndn = op->o_conn->c_ndn;
1600 if ( li->li_idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1601 if ( op->o_proxy_authz ) {
1603 * FIXME: we do not want to perform proxyAuthz
1604 * on behalf of the client, because this would
1605 * be performed with "proxyauthzdn" privileges.
1607 * This might actually be too strict, since
1608 * the "proxyauthzdn" authzTo, and each entry's
1609 * authzFrom attributes may be crafted
1610 * to avoid unwanted proxyAuthz to take place.
1613 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1614 rs->sr_text = "proxyAuthz not allowed within namingContext";
1619 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1623 if ( BER_BVISNULL( &ndn ) ) {
1627 if ( BER_BVISNULL( &li->li_idassert_authcDN ) ) {
1631 } else if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1632 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1633 /* && ( !BER_BVISNULL( &ndn )
1634 || LDAP_BACK_CONN_ISBOUND( lc ) ) */ )
1636 /* already asserted in SASL via native authz */
1637 /* NOTE: the test on lc->lc_bound is used to trap
1638 * native authorization of anonymous users,
1639 * since in that case ndn is NULL */
1643 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1645 struct berval authcDN;
1647 if ( BER_BVISNULL( &ndn ) ) {
1648 authcDN = slap_empty_bv;
1652 rc = slap_sasl_matches( op, li->li_idassert_authz,
1653 &authcDN, & authcDN );
1654 if ( rc != LDAP_SUCCESS ) {
1655 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1657 /* ndn is not authorized
1658 * to use idassert */
1665 if ( op->o_proxy_authz ) {
1668 * 1) ignore the already set proxyAuthz control
1669 * 2) leave it in place, and don't set ours
1671 * 4) reject the operation
1673 * option (4) is very drastic
1674 * option (3) will make the remote server reject
1675 * the operation, thus being equivalent to (4)
1676 * option (2) will likely break the idassert
1677 * assumptions, so we cannot accept it;
1678 * option (1) means that we are contradicting
1679 * the client's reques.
1681 * I think (4) is the only correct choice.
1683 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1684 rs->sr_text = "proxyAuthz not allowed within namingContext";
1687 if ( op->o_is_auth_check ) {
1688 mode = LDAP_BACK_IDASSERT_NOASSERT;
1691 mode = li->li_idassert_mode;
1695 case LDAP_BACK_IDASSERT_SELF:
1696 if ( BER_BVISNULL( &ndn ) ) {
1702 case LDAP_BACK_IDASSERT_LEGACY:
1703 /* original behavior:
1704 * assert the client's identity */
1705 if ( BER_BVISNULL( &ndn ) ) {
1706 assertedID = slap_empty_bv;
1712 case LDAP_BACK_IDASSERT_ANONYMOUS:
1713 /* assert "anonymous" */
1714 assertedID = slap_empty_bv;
1717 case LDAP_BACK_IDASSERT_NOASSERT:
1718 /* don't assert; bind as proxyauthzdn */
1721 case LDAP_BACK_IDASSERT_OTHERID:
1722 case LDAP_BACK_IDASSERT_OTHERDN:
1723 /* assert idassert DN */
1724 assertedID = li->li_idassert_authzID;
1731 if ( BER_BVISNULL( &assertedID ) ) {
1732 assertedID = slap_empty_bv;
1735 /* don't idassert the bound DN (ITS#4497) */
1736 if ( dn_match( &assertedID, &lc->lc_bound_ndn ) ) {
1740 if ( op->o_ctrls ) {
1741 for ( i = 0; op->o_ctrls[ i ]; i++ )
1742 /* just count ctrls */ ;
1745 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
1747 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
1749 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1750 ctrls[ 0 ]->ldctl_iscritical = 1;
1752 switch ( li->li_idassert_mode ) {
1753 /* already in u:ID or dn:DN form */
1754 case LDAP_BACK_IDASSERT_OTHERID:
1755 case LDAP_BACK_IDASSERT_OTHERDN:
1756 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
1759 /* needs the dn: prefix */
1761 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1762 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
1764 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1765 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1766 assertedID.bv_val, assertedID.bv_len + 1 );
1770 /* Older versions of <draft-weltman-ldapv3-proxy> required
1771 * to encode the value of the authzID (and called it proxyDN);
1772 * this hack provides compatibility with those DSAs that
1773 * implement it this way */
1774 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
1775 struct berval authzID = ctrls[ 0 ]->ldctl_value;
1776 BerElementBuffer berbuf;
1777 BerElement *ber = (BerElement *)&berbuf;
1780 ber_init2( ber, 0, LBER_USE_DER );
1781 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1783 tag = ber_printf( ber, "O", &authzID );
1784 if ( tag == LBER_ERROR ) {
1785 rs->sr_err = LDAP_OTHER;
1789 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1790 rs->sr_err = LDAP_OTHER;
1795 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1796 ber_free_buf( ber );
1798 if ( rs->sr_err != LDAP_SUCCESS ) {
1799 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1804 } else if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
1805 struct berval authzID = ctrls[ 0 ]->ldctl_value,
1807 BerElementBuffer berbuf;
1808 BerElement *ber = (BerElement *)&berbuf;
1811 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
1812 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1813 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1815 rs->sr_err = LDAP_PROTOCOL_ERROR;
1820 tmp.bv_val += STRLENOF( "dn:" );
1821 tmp.bv_len -= STRLENOF( "dn:" );
1823 ber_init2( ber, 0, LBER_USE_DER );
1824 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1826 /* apparently, Mozilla API encodes this
1827 * as "SEQUENCE { LDAPDN }" */
1828 tag = ber_printf( ber, "{O}", &tmp );
1829 if ( tag == LBER_ERROR ) {
1830 rs->sr_err = LDAP_OTHER;
1834 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1835 rs->sr_err = LDAP_OTHER;
1840 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1841 ber_free_buf( ber );
1843 if ( rs->sr_err != LDAP_SUCCESS ) {
1844 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1849 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1852 if ( op->o_ctrls ) {
1853 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1854 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1857 ctrls[ i + 1 ] = NULL;
1860 if ( ctrls == NULL ) {
1861 ctrls = op->o_ctrls;
1870 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1872 LDAPControl **ctrls = *pctrls;
1874 /* we assume that the first control is the proxyAuthz
1875 * added by back-ldap, so it's the only one we explicitly
1877 if ( ctrls && ctrls != op->o_ctrls ) {
1878 assert( ctrls[ 0 ] != NULL );
1880 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1881 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1884 op->o_tmpfree( ctrls, op->o_tmpmemctx );