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 /* rebind is now done inside ldap_back_proxy_authz_bind()
137 * in case of success */
138 LDAP_BACK_CONN_ISBOUND_SET( lc );
139 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
141 if ( LDAP_BACK_SAVECRED( li ) ) {
142 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
143 memset( lc->lc_cred.bv_val, 0,
144 lc->lc_cred.bv_len );
146 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
147 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
152 assert( lc->lc_binding == 1 );
155 /* must re-insert if local DN changed as result of bind */
156 if ( !LDAP_BACK_CONN_ISBOUND( lc )
157 || ( LDAP_BACK_CONN_ISBOUND( lc )
158 && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) ) )
163 /* wait for all other ops to release the connection */
165 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
166 if ( lc->lc_refcnt > 1 ) {
167 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
168 ldap_pvt_thread_yield();
172 assert( lc->lc_refcnt == 1 );
173 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
174 ldap_back_conndnlc_cmp );
175 assert( tmplc == NULL || lc == tmplc );
177 /* delete all cached connections with the current connection */
178 if ( LDAP_BACK_SINGLECONN( li ) ) {
179 while ( ( tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc, ldap_back_conn_cmp ) ) != NULL )
181 Debug( LDAP_DEBUG_TRACE,
182 "=>ldap_back_bind: destroying conn %ld (refcnt=%u)\n",
183 LDAP_BACK_PCONN_ID( lc->lc_conn ), lc->lc_refcnt, 0 );
185 if ( tmplc->lc_refcnt != 0 ) {
187 LDAP_BACK_CONN_TAINTED_SET( tmplc );
191 * Needs a test because the handler may be corrupted,
192 * and calling ldap_unbind on a corrupted header results
193 * in a segmentation fault
195 ldap_back_conn_free( tmplc );
200 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
201 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
202 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
203 lc->lc_conn = LDAP_BACK_PCONN_SET( op );
205 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
206 ldap_back_conndn_cmp, ldap_back_conndn_dup );
209 #if PRINT_CONNTREE > 0
210 myprint( li->li_conninfo.lai_tree, "ldap_back_bind" );
211 #endif /* PRINT_CONNTREE */
213 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
219 /* duplicate; someone else successfully bound
220 * on the same connection with the same identity;
221 * we can do this because lc_refcnt == 1 */
222 ldap_back_conn_free( lc );
228 ldap_back_release_conn( op, rs, lc );
235 * ldap_back_conndn_cmp
237 * compares two ldapconn_t based on the value of the conn pointer
238 * and of the local DN; used by avl stuff for insert, lookup
242 ldap_back_conndn_cmp( const void *c1, const void *c2 )
244 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
245 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
248 /* If local DNs don't match, it is definitely not a match */
249 /* For shared sessions, conn is NULL. Only explicitly
250 * bound sessions will have non-NULL conn.
252 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
254 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
261 * ldap_back_conndnlc_cmp
263 * compares two ldapconn_t based on the value of the conn pointer,
264 * the local DN and the lc pointer; used by avl stuff for insert, lookup
268 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
270 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
271 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
274 /* If local DNs don't match, it is definitely not a match */
275 /* For shared sessions, conn is NULL. Only explicitly
276 * bound sessions will have non-NULL conn.
278 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
280 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
282 rc = SLAP_PTRCMP( lc1, lc2 );
292 * compares two ldapconn_t based on the value of the conn pointer;
293 * used by avl stuff for delete of all conns with the same connid
296 ldap_back_conn_cmp( const void *c1, const void *c2 )
298 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
299 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
301 /* For shared sessions, conn is NULL. Only explicitly
302 * bound sessions will have non-NULL conn.
304 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
308 * ldap_back_conndn_dup
310 * returns -1 in case a duplicate ldapconn_t has been inserted;
314 ldap_back_conndn_dup( void *c1, void *c2 )
316 ldapconn_t *lc1 = (ldapconn_t *)c1;
317 ldapconn_t *lc2 = (ldapconn_t *)c2;
319 /* Cannot have more than one shared session with same DN */
320 if ( lc1->lc_conn == lc2->lc_conn &&
321 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
330 ldap_back_freeconn( Operation *op, ldapconn_t *lc, int dolock )
332 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
336 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
339 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
340 ldap_back_conndnlc_cmp );
341 assert( LDAP_BACK_CONN_TAINTED( lc ) || tmplc == lc );
342 if ( lc->lc_refcnt == 0 ) {
343 ldap_back_conn_free( (void *)lc );
347 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
364 int rc = LDAP_SUCCESS;
367 /* this is ridiculous... */
368 dummy.li_flags = flags;
370 /* start TLS ("tls-[try-]{start,propagate}" statements) */
371 if ( ( LDAP_BACK_USE_TLS( &dummy ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS( &dummy ) ) )
372 && !ldap_is_ldaps_url( url ) )
374 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
376 * use asynchronous StartTLS
377 * in case, chase referral (not implemented yet)
381 if ( protocol == 0 ) {
382 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
386 if ( protocol < LDAP_VERSION3 ) {
387 /* we should rather bail out... */
388 rc = LDAP_UNWILLING_TO_PERFORM;
389 *text = "invalid protocol version";
392 if ( rc == LDAP_SUCCESS ) {
393 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
396 if ( rc == LDAP_SUCCESS ) {
397 LDAPMessage *res = NULL;
400 LDAP_BACK_TV_SET( &tv );
403 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
405 rc = LDAP_UNAVAILABLE;
407 } else if ( rc == 0 ) {
408 if ( retries != LDAP_BACK_RETRY_NEVER ) {
409 ldap_pvt_thread_yield();
413 LDAP_BACK_TV_SET( &tv );
416 rc = LDAP_UNAVAILABLE;
418 } else if ( rc == LDAP_RES_EXTENDED ) {
419 struct berval *data = NULL;
421 rc = ldap_parse_extended_result( ld, res,
423 if ( rc == LDAP_SUCCESS ) {
425 rc = ldap_parse_result( ld, res, &err,
426 NULL, NULL, NULL, NULL, 1 );
427 if ( rc == LDAP_SUCCESS ) {
432 /* FIXME: in case a referral
433 * is returned, should we try
434 * using it instead of the
436 if ( rc == LDAP_SUCCESS ) {
437 rc = ldap_install_tls( ld );
439 } else if ( rc == LDAP_REFERRAL ) {
440 rc = LDAP_UNWILLING_TO_PERFORM;
441 *text = "unwilling to chase referral returned by Start TLS exop";
445 if ( data->bv_val ) {
446 ber_memfree( data->bv_val );
460 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
462 * use synchronous StartTLS
464 rc = ldap_start_tls_s( ld, NULL, NULL );
465 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
467 /* if StartTLS is requested, only attempt it if the URL
468 * is not "ldaps://"; this may occur not only in case
469 * of misconfiguration, but also when used in the chain
470 * overlay, where the "uri" can be parsed out of a referral */
476 case LDAP_SERVER_DOWN:
480 if ( LDAP_BACK_TLS_CRITICAL( &dummy ) ) {
481 *text = "could not start TLS";
485 /* in case Start TLS is not critical */
497 #endif /* HAVE_TLS */
500 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
502 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
506 int is_tls = op->o_conn->c_is_tls;
507 #endif /* HAVE_TLS */
508 time_t lc_time = (time_t)(-1);
510 assert( lcp != NULL );
512 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
513 rs->sr_err = ldap_initialize( &ld, li->li_uri );
514 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
515 if ( rs->sr_err != LDAP_SUCCESS ) {
519 if ( li->li_urllist_f ) {
520 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
523 /* Set LDAP version. This will always succeed: If the client
524 * bound with a particular version, then so can we.
526 if ( li->li_version != 0 ) {
527 version = li->li_version;
529 } else if ( op->o_protocol != 0 ) {
530 version = op->o_protocol;
533 /* assume it's an internal op; set to LDAPv3 */
534 version = LDAP_VERSION3;
536 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
538 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
539 ldap_set_option( ld, LDAP_OPT_REFERRALS,
540 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
542 if ( li->li_network_timeout > 0 ) {
545 tv.tv_sec = li->li_network_timeout;
547 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
551 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
552 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
553 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
554 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
555 if ( rs->sr_err != LDAP_SUCCESS ) {
556 ldap_unbind_ext( ld, NULL, NULL );
559 } else if ( li->li_idle_timeout ) {
560 /* only touch when activity actually took place... */
561 lc_time = op->o_time;
563 #endif /* HAVE_TLS */
565 if ( *lcp == NULL ) {
566 *lcp = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
567 (*lcp)->lc_flags = li->li_flags;
570 (*lcp)->lc_refcnt = 1;
571 (*lcp)->lc_binding = 1;
574 LDAP_BACK_CONN_ISTLS_SET( *lcp );
576 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
578 if ( lc_time != (time_t)(-1) ) {
579 (*lcp)->lc_time = lc_time;
581 #endif /* HAVE_TLS */
584 if ( rs->sr_err != LDAP_SUCCESS ) {
585 rs->sr_err = slap_map_api2result( rs );
586 if ( sendok & LDAP_BACK_SENDERR ) {
587 if ( rs->sr_text == NULL ) {
588 rs->sr_text = "ldap_initialize() failed";
590 send_ldap_result( op, rs );
595 if ( li->li_conn_ttl > 0 ) {
596 (*lcp)->lc_create_time = op->o_time;
604 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
606 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
607 ldapconn_t *lc = NULL,
609 int refcnt = 1, binding = 1;
611 /* if the server is quarantined, and
612 * - the current interval did not expire yet, or
613 * - no more retries should occur,
614 * don't return the connection */
615 if ( li->li_isquarantined ) {
616 slap_retry_info_t *ri = &li->li_quarantine;
619 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
620 if ( li->li_isquarantined == LDAP_BACK_FQ_YES ) {
621 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
622 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
624 Debug( LDAP_DEBUG_ANY,
625 "%s: ldap_back_getconn quarantine "
626 "retry block #%d try #%d.\n",
627 op->o_log_prefix, ri->ri_idx, ri->ri_count );
628 li->li_isquarantined = LDAP_BACK_FQ_RETRYING;
631 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
634 rs->sr_err = LDAP_UNAVAILABLE;
635 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
636 send_ldap_result( op, rs );
642 /* Internal searches are privileged and shared. So is root. */
643 if ( op->o_do_not_cache || be_isroot( op ) ) {
644 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
645 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
646 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
649 lc_curr.lc_local_ndn = op->o_ndn;
650 /* Explicit binds must not be shared */
651 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
652 lc_curr.lc_conn = op->o_conn;
655 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
659 /* Explicit Bind requests always get their own conn */
660 if ( !( sendok & LDAP_BACK_BINDING ) ) {
661 /* Searches for a ldapconn in the avl tree */
663 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
665 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
666 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
668 /* Don't reuse connections while they're still binding */
669 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
670 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
671 ldap_pvt_thread_yield();
675 refcnt = ++lc->lc_refcnt;
676 binding = ++lc->lc_binding;
678 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
681 /* Looks like we didn't get a bind. Open a new session... */
683 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
686 if ( sendok & LDAP_BACK_BINDING ) {
687 LDAP_BACK_CONN_BINDING_SET( lc );
689 lc->lc_conn = lc_curr.lc_conn;
690 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
692 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
693 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
694 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
695 LDAP_BACK_CONN_ISPRIV_SET( lc );
698 BER_BVZERO( &lc->lc_cred );
699 BER_BVZERO( &lc->lc_bound_ndn );
700 if ( !BER_BVISEMPTY( &op->o_ndn )
701 && SLAP_IS_AUTHZ_BACKEND( op ) )
703 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
708 /* if start TLS failed but it was not mandatory,
709 * check if the non-TLS connection was already
710 * in cache; in case, destroy the newly created
711 * connection and use the existing one */
712 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
713 && !ldap_tls_inplace( lc->lc_ld ) )
717 lc_curr.lc_conn = LDAP_BACK_PCONN;
718 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
719 tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
720 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
721 if ( tmplc != NULL ) {
722 refcnt = ++tmplc->lc_refcnt;
723 binding = ++tmplc->lc_binding;
724 ldap_back_conn_free( lc );
727 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
729 if ( tmplc != NULL ) {
733 #endif /* HAVE_TLS */
735 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
737 /* Inserts the newly created ldapconn in the avl tree */
738 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
740 assert( lc->lc_refcnt == 1 );
741 assert( lc->lc_binding == 1 );
742 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
743 ldap_back_conndn_cmp, ldap_back_conndn_dup );
745 #if PRINT_CONNTREE > 0
746 myprint( li->li_conninfo.lai_tree, "ldap_back_getconn" );
747 #endif /* PRINT_CONNTREE */
749 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
751 Debug( LDAP_DEBUG_TRACE,
752 "=>ldap_back_getconn: conn %p inserted refcnt=%u binding=%u\n",
753 (void *)lc, refcnt, binding );
755 /* Err could be -1 in case a duplicate ldapconn is inserted */
756 switch ( rs->sr_err ) {
761 if ( !( sendok & LDAP_BACK_BINDING ) ) {
762 /* duplicate: free and try to get the newly created one */
765 /* taint connection, so that it'll be freed when released */
766 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
767 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
768 ldap_back_conndnlc_cmp );
769 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
770 LDAP_BACK_CONN_TAINTED_SET( lc );
774 ldap_back_conn_free( lc );
775 rs->sr_err = LDAP_OTHER;
776 rs->sr_text = "proxy bind collision";
777 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
778 send_ldap_result( op, rs );
787 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
788 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
792 /* let it be used, but taint/delete it so that
793 * no-one else can look it up any further */
794 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
795 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
796 ldap_back_conndnlc_cmp );
797 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
798 LDAP_BACK_CONN_TAINTED_SET( lc );
801 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
802 char buf[ SLAP_TEXT_BUFLEN ];
804 snprintf( buf, sizeof( buf ),
805 "conn %p fetched refcnt=%u binding=%u%s",
806 (void *)lc, refcnt, binding, expiring ? " expiring" : "" );
807 Debug( LDAP_DEBUG_TRACE,
808 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
814 #endif /* HAVE_TLS */
820 ldap_back_release_conn_lock(
826 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
829 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
831 assert( lc->lc_refcnt > 0 );
832 LDAP_BACK_CONN_BINDING_CLEAR( lc );
834 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
835 ldap_back_freeconn( op, lc, 0 );
838 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
843 ldap_back_quarantine(
847 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
849 slap_retry_info_t *ri = &li->li_quarantine;
851 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
853 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
854 time_t new_last = slap_get_time();
856 switch ( li->li_isquarantined ) {
857 case LDAP_BACK_FQ_NO:
858 if ( ri->ri_last == new_last ) {
862 Debug( LDAP_DEBUG_ANY,
863 "%s: ldap_back_quarantine enter.\n",
864 op->o_log_prefix, 0, 0 );
870 case LDAP_BACK_FQ_RETRYING:
871 Debug( LDAP_DEBUG_ANY,
872 "%s: ldap_back_quarantine block #%d try #%d failed.\n",
873 op->o_log_prefix, ri->ri_idx, ri->ri_count );
876 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
877 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
888 li->li_isquarantined = LDAP_BACK_FQ_YES;
889 ri->ri_last = new_last;
891 } else if ( li->li_isquarantined != LDAP_BACK_FQ_NO ) {
892 Debug( LDAP_DEBUG_ANY,
893 "%s: ldap_back_quarantine exit.\n",
894 op->o_log_prefix, ri->ri_idx, ri->ri_count );
896 if ( li->li_quarantine_f ) {
897 (void)li->li_quarantine_f( li, li->li_quarantine_p );
902 li->li_isquarantined = LDAP_BACK_FQ_NO;
906 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
912 * Note: as the check for the value of lc->lc_bound was already here, I removed
913 * it from all the callers, and I made the function return the flag, so
914 * it can be used to simplify the check.
916 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
919 ldap_back_dobind_int(
923 ldap_back_send_t sendok,
927 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
934 assert( retries >= 0 );
938 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
941 if ( binding == 0 ) {
942 /* check if already bound */
943 rc = isbound = LDAP_BACK_CONN_ISBOUND( lc );
947 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
952 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
953 /* if someone else is about to bind it, give up and retry */
955 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
957 ldap_pvt_thread_yield();
961 /* otherwise this thread will bind it */
962 LDAP_BACK_CONN_BINDING_SET( lc );
967 /* wait for pending operations to finish */
968 /* FIXME: may become a bottleneck! */
969 if ( lc->lc_refcnt != lc->lc_binding ) {
971 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
973 ldap_pvt_thread_yield();
978 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
982 * FIXME: we need to let clients use proxyAuthz
983 * otherwise we cannot do symmetric pools of servers;
984 * we have to live with the fact that a user can
985 * authorize itself as any ID that is allowed
986 * by the authzTo directive of the "proxyauthzdn".
989 * NOTE: current Proxy Authorization specification
990 * and implementation do not allow proxy authorization
991 * control to be provided with Bind requests
994 * if no bind took place yet, but the connection is bound
995 * and the "idassert-authcDN" (or other ID) is set,
996 * then bind as the asserting identity and explicitly
997 * add the proxyAuthz control to every operation with the
998 * dn bound to the connection as control value.
999 * This is done also if this is the authrizing backend,
1000 * but the "override" flag is given to idassert.
1001 * It allows to use SASL bind and yet proxyAuthz users
1003 if ( op->o_conn != NULL && !op->o_do_not_cache &&
1004 ( !LDAP_BACK_CONN_ISPRIV( lc ) || BER_BVISEMPTY( &lc->lc_bound_ndn )) &&
1005 ( !isbound || ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
1007 (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok );
1011 #ifdef HAVE_CYRUS_SASL
1012 if ( LDAP_BACK_CONN_ISPRIV( lc )
1013 && li->li_acl_authmethod == LDAP_AUTH_SASL )
1015 void *defaults = NULL;
1017 if ( li->li_acl_secprops != NULL ) {
1018 rc = ldap_set_option( lc->lc_ld,
1019 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops );
1021 if ( rc != LDAP_OPT_SUCCESS ) {
1022 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
1023 "(SECPROPS,\"%s\") failed!\n",
1024 li->li_acl_secprops, 0, 0 );
1029 defaults = lutil_sasl_defaults( lc->lc_ld,
1030 li->li_acl_sasl_mech.bv_val,
1031 li->li_acl_sasl_realm.bv_val,
1032 li->li_acl_authcID.bv_val,
1033 li->li_acl_passwd.bv_val,
1036 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
1037 li->li_acl_authcDN.bv_val,
1038 li->li_acl_sasl_mech.bv_val, NULL, NULL,
1039 LDAP_SASL_QUIET, lutil_sasl_interact,
1042 lutil_sasl_freedefs( defaults );
1044 rs->sr_err = slap_map_api2result( rs );
1045 if ( rs->sr_err != LDAP_SUCCESS ) {
1046 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1047 send_ldap_result( op, rs );
1050 LDAP_BACK_CONN_ISBOUND_SET( lc );
1053 if ( LDAP_BACK_QUARANTINE( li ) ) {
1054 ldap_back_quarantine( op, rs );
1059 #endif /* HAVE_CYRUS_SASL */
1062 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1063 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1064 LDAP_SASL_SIMPLE, &lc->lc_cred,
1065 NULL, NULL, &msgid );
1067 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
1068 if ( retries != LDAP_BACK_RETRY_NEVER ) {
1070 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1073 assert( lc->lc_refcnt > 0 );
1074 if ( lc->lc_refcnt == 1 ) {
1075 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1078 /* lc here must be the regular lc, reset and ready for init */
1079 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
1080 if ( rs->sr_err != LDAP_SUCCESS ) {
1087 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1090 if ( rs->sr_err == LDAP_SUCCESS ) {
1091 if ( retries > 0 ) {
1099 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1103 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1107 /* FIXME: one binding-- too many? */
1109 ldap_back_freeconn( op, lc, dolock );
1110 rs->sr_err = slap_map_api2result( rs );
1112 if ( LDAP_BACK_QUARANTINE( li ) ) {
1113 ldap_back_quarantine( op, rs );
1119 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1120 if ( rc == LDAP_SUCCESS ) {
1121 LDAP_BACK_CONN_ISBOUND_SET( lc );
1126 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1127 rc = LDAP_BACK_CONN_ISBOUND( lc );
1129 ldap_back_release_conn_lock( op, rs, lc, dolock );
1136 ldap_back_dobind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1138 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1140 return ldap_back_dobind_int( lc, op, rs, sendok, li->li_nretries, 1 );
1144 * ldap_back_default_rebind
1146 * This is a callback used for chasing referrals using the same
1147 * credentials as the original user on this session.
1150 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1151 ber_int_t msgid, void *params )
1153 ldapconn_t *lc = (ldapconn_t *)params;
1156 /* ... otherwise we couldn't get here */
1157 assert( lc != NULL );
1159 if ( !ldap_tls_inplace( ld ) ) {
1160 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1162 const char *text = NULL;
1164 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1165 LDAP_BACK_RETRY_DEFAULT, &text );
1166 if ( rc != LDAP_SUCCESS ) {
1170 #endif /* HAVE_TLS */
1172 /* FIXME: add checks on the URL/identity? */
1174 return ldap_sasl_bind_s( ld,
1175 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1176 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1180 * ldap_back_default_urllist
1183 ldap_back_default_urllist(
1185 LDAPURLDesc **urllist,
1189 ldapinfo_t *li = (ldapinfo_t *)params;
1190 LDAPURLDesc **urltail;
1192 if ( urllist == url ) {
1193 return LDAP_SUCCESS;
1196 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1199 *urltail = *urllist;
1203 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1205 ch_free( li->li_uri );
1208 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1209 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1211 return LDAP_SUCCESS;
1220 ldap_back_send_t sendok )
1222 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1224 /* default behavior */
1225 if ( LDAP_BACK_ABANDON( li ) ) {
1226 return ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1229 if ( LDAP_BACK_IGNORE( li ) ) {
1230 return LDAP_SUCCESS;
1233 if ( LDAP_BACK_CANCEL( li ) ) {
1234 /* FIXME: asynchronous? */
1235 return ldap_cancel_s( lc->lc_ld, msgid, NULL, NULL );
1244 ldap_back_op_result(
1250 ldap_back_send_t sendok )
1252 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1257 LDAPControl **ctrls = NULL;
1259 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1262 rs->sr_matched = NULL;
1264 rs->sr_ctrls = NULL;
1266 /* if the error recorded in the reply corresponds
1267 * to a successful state, get the error from the
1268 * remote server response */
1269 if ( ERR_OK( rs->sr_err ) ) {
1272 LDAPMessage *res = NULL;
1275 tv.tv_sec = timeout;
1279 LDAP_BACK_TV_SET( &tv );
1283 /* if result parsing fails, note the failure reason */
1284 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1288 (void)ldap_back_cancel( lc, op, rs, msgid, sendok );
1289 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
1290 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1291 rs->sr_text = "Operation timed out";
1295 LDAP_BACK_TV_SET( &tv );
1296 ldap_pvt_thread_yield();
1300 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1305 /* otherwise get the result; if it is not
1306 * LDAP_SUCCESS, record it in the reply
1307 * structure (this includes
1308 * LDAP_COMPARE_{TRUE|FALSE}) */
1310 /* only touch when activity actually took place... */
1311 if ( li->li_idle_timeout && lc ) {
1312 lc->lc_time = op->o_time;
1315 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1316 &match, &text, &refs, &ctrls, 1 );
1318 if ( rc != LDAP_SUCCESS ) {
1321 if ( refs != NULL ) {
1324 for ( i = 0; refs[ i ] != NULL; i++ )
1326 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1328 for ( i = 0; refs[ i ] != NULL; i++ ) {
1329 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1331 BER_BVZERO( &rs->sr_ref[ i ] );
1333 if ( ctrls != NULL ) {
1334 rs->sr_ctrls = ctrls;
1339 /* if the error in the reply structure is not
1340 * LDAP_SUCCESS, try to map it from client
1341 * to server error */
1342 if ( !ERR_OK( rs->sr_err ) ) {
1343 rs->sr_err = slap_map_api2result( rs );
1345 /* internal ops ( op->o_conn == NULL )
1346 * must not reply to client */
1347 if ( op->o_conn && !op->o_do_not_cache && match ) {
1349 /* record the (massaged) matched
1350 * DN into the reply structure */
1351 rs->sr_matched = match;
1355 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1356 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1357 if ( LDAP_BACK_QUARANTINE( li ) ) {
1358 ldap_back_quarantine( op, rs );
1360 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1361 send_ldap_result( op, rs );
1365 } else if ( op->o_conn &&
1366 ( ( ( sendok & LDAP_BACK_SENDOK ) && ERR_OK( rs->sr_err ) )
1367 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1369 send_ldap_result( op, rs );
1373 if ( rs->sr_matched != match ) {
1374 free( (char *)rs->sr_matched );
1376 rs->sr_matched = NULL;
1377 ldap_memfree( match );
1381 ldap_memfree( text );
1386 assert( refs != NULL );
1387 ber_memvfree( (void **)refs );
1388 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1393 assert( rs->sr_ctrls != NULL );
1394 ldap_controls_free( ctrls );
1395 rs->sr_ctrls = NULL;
1398 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1401 /* return true if bound, false if failed */
1403 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1406 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1408 assert( lcp != NULL );
1409 assert( *lcp != NULL );
1411 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1413 if ( (*lcp)->lc_refcnt == 1 ) {
1414 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1415 Debug( LDAP_DEBUG_ANY,
1416 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1417 op->o_log_prefix, li->li_uri,
1418 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1419 "" : (*lcp)->lc_bound_ndn.bv_val );
1420 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1422 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1423 (*lcp)->lc_ld = NULL;
1424 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1426 /* lc here must be the regular lc, reset and ready for init */
1427 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1428 if ( rc != LDAP_SUCCESS ) {
1430 /* freeit, because lc_refcnt == 1 */
1431 (*lcp)->lc_refcnt = 0;
1432 (void)ldap_back_freeconn( op, *lcp, 0 );
1436 rc = ldap_back_dobind_int( *lcp, op, rs, sendok, 0, 0 );
1437 if ( rc == 0 && *lcp != NULL ) {
1438 /* freeit, because lc_refcnt == 1 */
1439 (*lcp)->lc_refcnt = 0;
1440 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1441 (void)ldap_back_freeconn( op, *lcp, 0 );
1447 Debug( LDAP_DEBUG_TRACE,
1448 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1449 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1451 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1452 ldap_back_release_conn_lock( op, rs, *lcp, 0 );
1456 rs->sr_err = LDAP_UNAVAILABLE;
1457 rs->sr_text = "unable to retry";
1458 send_ldap_result( op, rs );
1462 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1468 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1470 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1471 struct berval binddn = slap_empty_bv;
1472 struct berval bindcred = slap_empty_bv;
1478 /* don't proxyAuthz if protocol is not LDAPv3 */
1479 switch ( li->li_version ) {
1484 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1490 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1491 if ( sendok & LDAP_BACK_SENDERR ) {
1492 send_ldap_result( op, rs );
1494 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1498 if ( op->o_tag == LDAP_REQ_BIND ) {
1499 ndn = op->o_req_ndn;
1501 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1502 ndn = op->o_conn->c_ndn;
1509 * FIXME: we need to let clients use proxyAuthz
1510 * otherwise we cannot do symmetric pools of servers;
1511 * we have to live with the fact that a user can
1512 * authorize itself as any ID that is allowed
1513 * by the authzTo directive of the "proxyauthzdn".
1516 * NOTE: current Proxy Authorization specification
1517 * and implementation do not allow proxy authorization
1518 * control to be provided with Bind requests
1521 * if no bind took place yet, but the connection is bound
1522 * and the "proxyauthzdn" is set, then bind as
1523 * "proxyauthzdn" and explicitly add the proxyAuthz
1524 * control to every operation with the dn bound
1525 * to the connection as control value.
1528 /* bind as proxyauthzdn only if no idassert mode
1529 * is requested, or if the client's identity
1531 switch ( li->li_idassert_mode ) {
1532 case LDAP_BACK_IDASSERT_LEGACY:
1533 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1534 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1536 binddn = li->li_idassert_authcDN;
1537 bindcred = li->li_idassert_passwd;
1544 /* NOTE: rootdn can always idassert */
1545 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1546 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1547 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1548 if ( sendok & LDAP_BACK_SENDERR ) {
1549 send_ldap_result( op, rs );
1551 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1554 rs->sr_err = LDAP_SUCCESS;
1555 binddn = slap_empty_bv;
1556 bindcred = slap_empty_bv;
1562 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1563 struct berval authcDN;
1565 if ( BER_BVISNULL( &ndn ) ) {
1566 authcDN = slap_empty_bv;
1571 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1572 &authcDN, &authcDN );
1573 if ( rs->sr_err != LDAP_SUCCESS ) {
1574 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1575 if ( sendok & LDAP_BACK_SENDERR ) {
1576 send_ldap_result( op, rs );
1578 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1581 rs->sr_err = LDAP_SUCCESS;
1582 binddn = slap_empty_bv;
1583 bindcred = slap_empty_bv;
1591 binddn = li->li_idassert_authcDN;
1592 bindcred = li->li_idassert_passwd;
1597 if ( dobind && li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1598 #ifdef HAVE_CYRUS_SASL
1599 void *defaults = NULL;
1600 struct berval authzID = BER_BVNULL;
1603 /* if SASL supports native authz, prepare for it */
1604 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1605 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1607 switch ( li->li_idassert_mode ) {
1608 case LDAP_BACK_IDASSERT_OTHERID:
1609 case LDAP_BACK_IDASSERT_OTHERDN:
1610 authzID = li->li_idassert_authzID;
1613 case LDAP_BACK_IDASSERT_ANONYMOUS:
1614 BER_BVSTR( &authzID, "dn:" );
1617 case LDAP_BACK_IDASSERT_SELF:
1618 if ( BER_BVISNULL( &ndn ) ) {
1619 /* connection is not authc'd, so don't idassert */
1620 BER_BVSTR( &authzID, "dn:" );
1623 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1624 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1625 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1626 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1627 ndn.bv_val, ndn.bv_len + 1 );
1636 if ( li->li_idassert_secprops != NULL ) {
1637 rs->sr_err = ldap_set_option( lc->lc_ld,
1638 LDAP_OPT_X_SASL_SECPROPS,
1639 (void *)li->li_idassert_secprops );
1641 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1642 rs->sr_err = LDAP_OTHER;
1643 if ( sendok & LDAP_BACK_SENDERR ) {
1644 send_ldap_result( op, rs );
1646 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1651 defaults = lutil_sasl_defaults( lc->lc_ld,
1652 li->li_idassert_sasl_mech.bv_val,
1653 li->li_idassert_sasl_realm.bv_val,
1654 li->li_idassert_authcID.bv_val,
1655 li->li_idassert_passwd.bv_val,
1658 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1659 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1660 LDAP_SASL_QUIET, lutil_sasl_interact,
1663 rs->sr_err = slap_map_api2result( rs );
1664 if ( rs->sr_err != LDAP_SUCCESS ) {
1665 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1666 if ( sendok & LDAP_BACK_SENDERR ) {
1667 send_ldap_result( op, rs );
1671 LDAP_BACK_CONN_ISBOUND_SET( lc );
1674 lutil_sasl_freedefs( defaults );
1676 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1680 #endif /* HAVE_CYRUS_SASL */
1683 switch ( li->li_idassert_authmethod ) {
1684 case LDAP_AUTH_NONE:
1685 BER_BVSTR( &binddn, "" );
1686 BER_BVSTR( &bindcred, "" );
1689 case LDAP_AUTH_SIMPLE:
1690 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1691 binddn.bv_val, LDAP_SASL_SIMPLE,
1692 &bindcred, NULL, NULL, &msgid );
1693 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1698 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1699 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1700 if ( sendok & LDAP_BACK_SENDERR ) {
1701 send_ldap_result( op, rs );
1706 if ( rc == LDAP_SUCCESS ) {
1707 /* set rebind stuff in case of successful proxyAuthz bind,
1708 * so that referral chasing is attempted using the right
1710 LDAP_BACK_CONN_ISBOUND_SET( lc );
1711 ber_bvreplace( &lc->lc_bound_ndn, &binddn );
1713 if ( LDAP_BACK_SAVECRED( li ) ) {
1714 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
1715 memset( lc->lc_cred.bv_val, 0,
1716 lc->lc_cred.bv_len );
1718 ber_bvreplace( &lc->lc_cred, &bindcred );
1719 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1723 return LDAP_BACK_CONN_ISBOUND( lc );
1727 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1728 * to existing server-side controls if required; if not,
1729 * the existing server-side controls are placed in *pctrls.
1730 * The caller, after using the controls in client API
1731 * operations, if ( *pctrls != op->o_ctrls ), should
1732 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1733 * The function returns success if the control could
1734 * be added if required, or if it did nothing; in the future,
1735 * it might return some error if it failed.
1737 * if no bind took place yet, but the connection is bound
1738 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1739 * and explicitly add proxyAuthz the control to every operation
1740 * with the dn bound to the connection as control value.
1742 * If no server-side controls are defined for the operation,
1743 * simply add the proxyAuthz control; otherwise, if the
1744 * proxyAuthz control is not already set, add it as
1747 * FIXME: is controls order significant for security?
1748 * ANSWER: controls ordering and interoperability
1749 * must be indicated by the specs of each control; if none
1750 * is specified, the order is irrelevant.
1753 ldap_back_proxy_authz_ctrl(
1754 struct berval *bound_ndn,
1756 slap_idassert_t *si,
1759 LDAPControl ***pctrls )
1761 LDAPControl **ctrls = NULL;
1763 slap_idassert_mode_t mode;
1764 struct berval assertedID,
1769 rs->sr_err = LDAP_SUCCESS;
1771 /* don't proxyAuthz if protocol is not LDAPv3 */
1772 switch ( version ) {
1777 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1786 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1787 * but if it is not set this test fails. We need a different
1788 * means to detect if idassert is enabled */
1789 if ( ( BER_BVISNULL( &si->si_bc.sb_authcId ) || BER_BVISEMPTY( &si->si_bc.sb_authcId ) )
1790 && ( BER_BVISNULL( &si->si_bc.sb_binddn ) || BER_BVISEMPTY( &si->si_bc.sb_binddn ) ) )
1795 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1799 if ( op->o_tag == LDAP_REQ_BIND ) {
1800 ndn = op->o_req_ndn;
1802 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1803 ndn = op->o_conn->c_ndn;
1809 if ( si->si_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1810 if ( op->o_proxy_authz ) {
1812 * FIXME: we do not want to perform proxyAuthz
1813 * on behalf of the client, because this would
1814 * be performed with "proxyauthzdn" privileges.
1816 * This might actually be too strict, since
1817 * the "proxyauthzdn" authzTo, and each entry's
1818 * authzFrom attributes may be crafted
1819 * to avoid unwanted proxyAuthz to take place.
1822 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1823 rs->sr_text = "proxyAuthz not allowed within namingContext";
1828 if ( !BER_BVISNULL( bound_ndn ) ) {
1832 if ( BER_BVISNULL( &ndn ) ) {
1836 if ( BER_BVISNULL( &si->si_bc.sb_binddn ) ) {
1840 } else if ( si->si_bc.sb_method == LDAP_AUTH_SASL ) {
1841 if ( ( si->si_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1843 /* already asserted in SASL via native authz */
1847 } else if ( si->si_authz && !be_isroot( op ) ) {
1849 struct berval authcDN;
1851 if ( BER_BVISNULL( &ndn ) ) {
1852 authcDN = slap_empty_bv;
1856 rc = slap_sasl_matches( op, si->si_authz,
1857 &authcDN, & authcDN );
1858 if ( rc != LDAP_SUCCESS ) {
1859 if ( si->si_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1860 /* ndn is not authorized
1861 * to use idassert */
1868 if ( op->o_proxy_authz ) {
1871 * 1) ignore the already set proxyAuthz control
1872 * 2) leave it in place, and don't set ours
1874 * 4) reject the operation
1876 * option (4) is very drastic
1877 * option (3) will make the remote server reject
1878 * the operation, thus being equivalent to (4)
1879 * option (2) will likely break the idassert
1880 * assumptions, so we cannot accept it;
1881 * option (1) means that we are contradicting
1882 * the client's reques.
1884 * I think (4) is the only correct choice.
1886 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1887 rs->sr_text = "proxyAuthz not allowed within namingContext";
1890 if ( op->o_is_auth_check ) {
1891 mode = LDAP_BACK_IDASSERT_NOASSERT;
1898 case LDAP_BACK_IDASSERT_SELF:
1899 if ( BER_BVISNULL( &ndn ) ) {
1905 case LDAP_BACK_IDASSERT_LEGACY:
1906 /* original behavior:
1907 * assert the client's identity */
1908 if ( BER_BVISNULL( &ndn ) ) {
1909 assertedID = slap_empty_bv;
1915 case LDAP_BACK_IDASSERT_ANONYMOUS:
1916 /* assert "anonymous" */
1917 assertedID = slap_empty_bv;
1920 case LDAP_BACK_IDASSERT_NOASSERT:
1921 /* don't assert; bind as proxyauthzdn */
1924 case LDAP_BACK_IDASSERT_OTHERID:
1925 case LDAP_BACK_IDASSERT_OTHERDN:
1926 /* assert idassert DN */
1927 assertedID = si->si_bc.sb_authzId;
1934 if ( BER_BVISNULL( &assertedID ) ) {
1935 assertedID = slap_empty_bv;
1938 /* don't idassert the bound DN (ITS#4497) */
1939 if ( dn_match( &assertedID, bound_ndn ) ) {
1943 if ( op->o_ctrls ) {
1944 for ( i = 0; op->o_ctrls[ i ]; i++ )
1945 /* just count ctrls */ ;
1948 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
1950 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
1952 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1953 ctrls[ 0 ]->ldctl_iscritical = 1;
1955 switch ( si->si_mode ) {
1956 /* already in u:ID or dn:DN form */
1957 case LDAP_BACK_IDASSERT_OTHERID:
1958 case LDAP_BACK_IDASSERT_OTHERDN:
1959 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
1962 /* needs the dn: prefix */
1964 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1965 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
1967 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1968 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1969 assertedID.bv_val, assertedID.bv_len + 1 );
1973 /* Older versions of <draft-weltman-ldapv3-proxy> required
1974 * to encode the value of the authzID (and called it proxyDN);
1975 * this hack provides compatibility with those DSAs that
1976 * implement it this way */
1977 if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
1978 struct berval authzID = ctrls[ 0 ]->ldctl_value;
1979 BerElementBuffer berbuf;
1980 BerElement *ber = (BerElement *)&berbuf;
1983 ber_init2( ber, 0, LBER_USE_DER );
1984 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1986 tag = ber_printf( ber, "O", &authzID );
1987 if ( tag == LBER_ERROR ) {
1988 rs->sr_err = LDAP_OTHER;
1992 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1993 rs->sr_err = LDAP_OTHER;
1998 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1999 ber_free_buf( ber );
2001 if ( rs->sr_err != LDAP_SUCCESS ) {
2002 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2007 } else if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
2008 struct berval authzID = ctrls[ 0 ]->ldctl_value,
2010 BerElementBuffer berbuf;
2011 BerElement *ber = (BerElement *)&berbuf;
2014 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
2015 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2016 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2018 rs->sr_err = LDAP_PROTOCOL_ERROR;
2023 tmp.bv_val += STRLENOF( "dn:" );
2024 tmp.bv_len -= STRLENOF( "dn:" );
2026 ber_init2( ber, 0, LBER_USE_DER );
2027 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2029 /* apparently, Mozilla API encodes this
2030 * as "SEQUENCE { LDAPDN }" */
2031 tag = ber_printf( ber, "{O}", &tmp );
2032 if ( tag == LBER_ERROR ) {
2033 rs->sr_err = LDAP_OTHER;
2037 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
2038 rs->sr_err = LDAP_OTHER;
2043 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2044 ber_free_buf( ber );
2046 if ( rs->sr_err != LDAP_SUCCESS ) {
2047 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2052 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
2055 if ( op->o_ctrls ) {
2056 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
2057 ctrls[ i + 1 ] = op->o_ctrls[ i ];
2060 ctrls[ i + 1 ] = NULL;
2063 if ( ctrls == NULL ) {
2064 ctrls = op->o_ctrls;
2073 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
2075 LDAPControl **ctrls = *pctrls;
2077 /* we assume that the first control is the proxyAuthz
2078 * added by back-ldap, so it's the only one we explicitly
2080 if ( ctrls && ctrls != op->o_ctrls ) {
2081 assert( ctrls[ 0 ] != NULL );
2083 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
2084 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2087 op->o_tmpfree( ctrls, op->o_tmpmemctx );