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 */
509 assert( lcp != NULL );
511 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
512 rs->sr_err = ldap_initialize( &ld, li->li_uri );
513 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
514 if ( rs->sr_err != LDAP_SUCCESS ) {
518 if ( li->li_urllist_f ) {
519 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
522 /* Set LDAP version. This will always succeed: If the client
523 * bound with a particular version, then so can we.
525 if ( li->li_version != 0 ) {
526 version = li->li_version;
528 } else if ( op->o_protocol != 0 ) {
529 version = op->o_protocol;
532 /* assume it's an internal op; set to LDAPv3 */
533 version = LDAP_VERSION3;
535 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
537 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
538 ldap_set_option( ld, LDAP_OPT_REFERRALS,
539 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
541 if ( li->li_network_timeout > 0 ) {
544 tv.tv_sec = li->li_network_timeout;
546 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
550 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
551 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
552 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
553 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
554 if ( rs->sr_err != LDAP_SUCCESS ) {
555 ldap_unbind_ext( ld, NULL, NULL );
558 #endif /* HAVE_TLS */
560 if ( *lcp == NULL ) {
561 *lcp = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
562 (*lcp)->lc_flags = li->li_flags;
565 (*lcp)->lc_refcnt = 1;
566 (*lcp)->lc_binding = 1;
569 LDAP_BACK_CONN_ISTLS_SET( *lcp );
571 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
573 #endif /* HAVE_TLS */
576 if ( rs->sr_err != LDAP_SUCCESS ) {
577 rs->sr_err = slap_map_api2result( rs );
578 if ( sendok & LDAP_BACK_SENDERR ) {
579 if ( rs->sr_text == NULL ) {
580 rs->sr_text = "ldap_initialize() failed";
582 send_ldap_result( op, rs );
587 if ( li->li_conn_ttl > 0 ) {
588 (*lcp)->lc_create_time = op->o_time;
596 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
598 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
599 ldapconn_t *lc = NULL,
601 int refcnt = 1, binding = 1;
603 /* if the server is quarantined, and
604 * - the current interval did not expire yet, or
605 * - no more retries should occur,
606 * don't return the connection */
607 if ( li->li_isquarantined ) {
608 slap_retry_info_t *ri = &li->li_quarantine;
611 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
612 if ( li->li_isquarantined == LDAP_BACK_FQ_YES ) {
613 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
614 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
616 Debug( LDAP_DEBUG_ANY,
617 "%s: ldap_back_getconn quarantine "
618 "retry block #%d try #%d.\n",
619 op->o_log_prefix, ri->ri_idx, ri->ri_count );
620 li->li_isquarantined = LDAP_BACK_FQ_RETRYING;
623 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
626 rs->sr_err = LDAP_UNAVAILABLE;
627 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
628 send_ldap_result( op, rs );
634 /* Internal searches are privileged and shared. So is root. */
635 if ( op->o_do_not_cache || be_isroot( op ) ) {
636 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
637 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
638 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
641 lc_curr.lc_local_ndn = op->o_ndn;
642 /* Explicit binds must not be shared */
643 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
644 lc_curr.lc_conn = op->o_conn;
647 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
651 /* Explicit Bind requests always get their own conn */
652 if ( !( sendok & LDAP_BACK_BINDING ) ) {
653 /* Searches for a ldapconn in the avl tree */
655 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
657 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
658 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
660 /* Don't reuse connections while they're still binding */
661 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
662 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
663 ldap_pvt_thread_yield();
667 refcnt = ++lc->lc_refcnt;
668 binding = ++lc->lc_binding;
670 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
673 /* Looks like we didn't get a bind. Open a new session... */
675 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
678 if ( sendok & LDAP_BACK_BINDING ) {
679 LDAP_BACK_CONN_BINDING_SET( lc );
681 lc->lc_conn = lc_curr.lc_conn;
682 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
684 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
685 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
686 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
687 LDAP_BACK_CONN_ISPRIV_SET( lc );
690 BER_BVZERO( &lc->lc_cred );
691 BER_BVZERO( &lc->lc_bound_ndn );
692 if ( !BER_BVISEMPTY( &op->o_ndn )
693 && SLAP_IS_AUTHZ_BACKEND( op ) )
695 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
700 /* if start TLS failed but it was not mandatory,
701 * check if the non-TLS connection was already
702 * in cache; in case, destroy the newly created
703 * connection and use the existing one */
704 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
705 && !ldap_tls_inplace( lc->lc_ld ) )
709 lc_curr.lc_conn = LDAP_BACK_PCONN;
710 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
711 tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
712 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
713 if ( tmplc != NULL ) {
714 refcnt = ++tmplc->lc_refcnt;
715 binding = ++tmplc->lc_binding;
716 ldap_back_conn_free( lc );
719 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
721 if ( tmplc != NULL ) {
725 #endif /* HAVE_TLS */
727 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
729 /* Inserts the newly created ldapconn in the avl tree */
730 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
732 assert( lc->lc_refcnt == 1 );
733 assert( lc->lc_binding == 1 );
734 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
735 ldap_back_conndn_cmp, ldap_back_conndn_dup );
737 #if PRINT_CONNTREE > 0
738 myprint( li->li_conninfo.lai_tree, "ldap_back_getconn" );
739 #endif /* PRINT_CONNTREE */
741 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
743 Debug( LDAP_DEBUG_TRACE,
744 "=>ldap_back_getconn: conn %p inserted refcnt=%u binding=%u\n",
745 (void *)lc, refcnt, binding );
747 /* Err could be -1 in case a duplicate ldapconn is inserted */
748 switch ( rs->sr_err ) {
753 if ( !( sendok & LDAP_BACK_BINDING ) ) {
754 /* duplicate: free and try to get the newly created one */
757 /* taint connection, so that it'll be freed when released */
758 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
759 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
760 ldap_back_conndnlc_cmp );
761 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
762 LDAP_BACK_CONN_TAINTED_SET( lc );
766 ldap_back_conn_free( lc );
767 rs->sr_err = LDAP_OTHER;
768 rs->sr_text = "proxy bind collision";
769 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
770 send_ldap_result( op, rs );
779 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
780 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
784 /* let it be used, but taint/delete it so that
785 * no-one else can look it up any further */
786 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
787 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
788 ldap_back_conndnlc_cmp );
789 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
790 LDAP_BACK_CONN_TAINTED_SET( lc );
793 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
794 char buf[ SLAP_TEXT_BUFLEN ];
796 snprintf( buf, sizeof( buf ),
797 "conn %p fetched refcnt=%u binding=%u%s",
798 (void *)lc, refcnt, binding, expiring ? " expiring" : "" );
799 Debug( LDAP_DEBUG_TRACE,
800 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
806 #endif /* HAVE_TLS */
807 if ( li->li_idle_timeout && lc ) {
808 lc->lc_time = op->o_time;
815 ldap_back_release_conn_lock(
821 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
824 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
826 assert( lc->lc_refcnt > 0 );
827 LDAP_BACK_CONN_BINDING_CLEAR( lc );
829 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
830 ldap_back_freeconn( op, lc, 0 );
833 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
838 ldap_back_quarantine(
842 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
844 slap_retry_info_t *ri = &li->li_quarantine;
846 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
848 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
849 time_t new_last = slap_get_time();
851 switch ( li->li_isquarantined ) {
852 case LDAP_BACK_FQ_NO:
853 if ( ri->ri_last == new_last ) {
857 Debug( LDAP_DEBUG_ANY,
858 "%s: ldap_back_quarantine enter.\n",
859 op->o_log_prefix, 0, 0 );
865 case LDAP_BACK_FQ_RETRYING:
866 Debug( LDAP_DEBUG_ANY,
867 "%s: ldap_back_quarantine block #%d try #%d failed.\n",
868 op->o_log_prefix, ri->ri_idx, ri->ri_count );
871 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
872 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
883 li->li_isquarantined = LDAP_BACK_FQ_YES;
884 ri->ri_last = new_last;
886 } else if ( li->li_isquarantined != LDAP_BACK_FQ_NO ) {
887 Debug( LDAP_DEBUG_ANY,
888 "%s: ldap_back_quarantine exit.\n",
889 op->o_log_prefix, ri->ri_idx, ri->ri_count );
891 if ( li->li_quarantine_f ) {
892 (void)li->li_quarantine_f( li, li->li_quarantine_p );
897 li->li_isquarantined = LDAP_BACK_FQ_NO;
901 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
907 * Note: as the check for the value of lc->lc_bound was already here, I removed
908 * it from all the callers, and I made the function return the flag, so
909 * it can be used to simplify the check.
911 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
914 ldap_back_dobind_int(
918 ldap_back_send_t sendok,
922 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
929 assert( retries >= 0 );
933 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
936 if ( binding == 0 ) {
937 /* check if already bound */
938 rc = isbound = LDAP_BACK_CONN_ISBOUND( lc );
942 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
947 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
948 /* if someone else is about to bind it, give up and retry */
950 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
952 ldap_pvt_thread_yield();
956 /* otherwise this thread will bind it */
957 LDAP_BACK_CONN_BINDING_SET( lc );
962 /* wait for pending operations to finish */
963 /* FIXME: may become a bottleneck! */
964 if ( lc->lc_refcnt != lc->lc_binding ) {
966 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
968 ldap_pvt_thread_yield();
973 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
977 * FIXME: we need to let clients use proxyAuthz
978 * otherwise we cannot do symmetric pools of servers;
979 * we have to live with the fact that a user can
980 * authorize itself as any ID that is allowed
981 * by the authzTo directive of the "proxyauthzdn".
984 * NOTE: current Proxy Authorization specification
985 * and implementation do not allow proxy authorization
986 * control to be provided with Bind requests
989 * if no bind took place yet, but the connection is bound
990 * and the "idassert-authcDN" (or other ID) is set,
991 * then bind as the asserting identity and explicitly
992 * add the proxyAuthz control to every operation with the
993 * dn bound to the connection as control value.
994 * This is done also if this is the authrizing backend,
995 * but the "override" flag is given to idassert.
996 * It allows to use SASL bind and yet proxyAuthz users
998 if ( op->o_conn != NULL &&
999 !op->o_do_not_cache &&
1000 ( !isbound || ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
1002 (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok );
1006 #ifdef HAVE_CYRUS_SASL
1007 if ( LDAP_BACK_CONN_ISPRIV( lc )
1008 && li->li_acl_authmethod == LDAP_AUTH_SASL )
1010 void *defaults = NULL;
1012 if ( li->li_acl_secprops != NULL ) {
1013 rc = ldap_set_option( lc->lc_ld,
1014 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops );
1016 if ( rc != LDAP_OPT_SUCCESS ) {
1017 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
1018 "(SECPROPS,\"%s\") failed!\n",
1019 li->li_acl_secprops, 0, 0 );
1024 defaults = lutil_sasl_defaults( lc->lc_ld,
1025 li->li_acl_sasl_mech.bv_val,
1026 li->li_acl_sasl_realm.bv_val,
1027 li->li_acl_authcID.bv_val,
1028 li->li_acl_passwd.bv_val,
1031 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
1032 li->li_acl_authcDN.bv_val,
1033 li->li_acl_sasl_mech.bv_val, NULL, NULL,
1034 LDAP_SASL_QUIET, lutil_sasl_interact,
1037 lutil_sasl_freedefs( defaults );
1039 rs->sr_err = slap_map_api2result( rs );
1040 if ( rs->sr_err != LDAP_SUCCESS ) {
1041 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1042 send_ldap_result( op, rs );
1045 LDAP_BACK_CONN_ISBOUND_SET( lc );
1048 if ( LDAP_BACK_QUARANTINE( li ) ) {
1049 ldap_back_quarantine( op, rs );
1054 #endif /* HAVE_CYRUS_SASL */
1057 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1058 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1059 LDAP_SASL_SIMPLE, &lc->lc_cred,
1060 NULL, NULL, &msgid );
1062 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
1063 if ( retries != LDAP_BACK_RETRY_NEVER ) {
1065 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1068 assert( lc->lc_refcnt > 0 );
1069 if ( lc->lc_refcnt == 1 ) {
1070 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1073 /* lc here must be the regular lc, reset and ready for init */
1074 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
1075 if ( rs->sr_err != LDAP_SUCCESS ) {
1082 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1085 if ( rs->sr_err == LDAP_SUCCESS ) {
1086 if ( retries > 0 ) {
1094 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1098 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1102 /* FIXME: one binding-- too many? */
1104 ldap_back_freeconn( op, lc, dolock );
1105 rs->sr_err = slap_map_api2result( rs );
1107 if ( LDAP_BACK_QUARANTINE( li ) ) {
1108 ldap_back_quarantine( op, rs );
1114 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1115 if ( rc == LDAP_SUCCESS ) {
1116 LDAP_BACK_CONN_ISBOUND_SET( lc );
1121 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1122 rc = LDAP_BACK_CONN_ISBOUND( lc );
1124 ldap_back_release_conn_lock( op, rs, lc, dolock );
1131 ldap_back_dobind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1133 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1135 return ldap_back_dobind_int( lc, op, rs, sendok, li->li_nretries, 1 );
1139 * ldap_back_default_rebind
1141 * This is a callback used for chasing referrals using the same
1142 * credentials as the original user on this session.
1145 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1146 ber_int_t msgid, void *params )
1148 ldapconn_t *lc = (ldapconn_t *)params;
1151 /* ... otherwise we couldn't get here */
1152 assert( lc != NULL );
1154 if ( !ldap_tls_inplace( ld ) ) {
1155 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1157 const char *text = NULL;
1159 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1160 LDAP_BACK_RETRY_DEFAULT, &text );
1161 if ( rc != LDAP_SUCCESS ) {
1165 #endif /* HAVE_TLS */
1167 /* FIXME: add checks on the URL/identity? */
1169 return ldap_sasl_bind_s( ld,
1170 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1171 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1175 * ldap_back_default_urllist
1178 ldap_back_default_urllist(
1180 LDAPURLDesc **urllist,
1184 ldapinfo_t *li = (ldapinfo_t *)params;
1185 LDAPURLDesc **urltail;
1187 if ( urllist == url ) {
1188 return LDAP_SUCCESS;
1191 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1194 *urltail = *urllist;
1198 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1200 ch_free( li->li_uri );
1203 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1204 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1206 return LDAP_SUCCESS;
1215 ldap_back_send_t sendok )
1217 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1219 /* default behavior */
1220 if ( LDAP_BACK_ABANDON( li ) ) {
1221 return ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1224 if ( LDAP_BACK_IGNORE( li ) ) {
1225 return LDAP_SUCCESS;
1228 if ( LDAP_BACK_CANCEL( li ) ) {
1229 /* FIXME: asynchronous? */
1230 return ldap_cancel_s( lc->lc_ld, msgid, NULL, NULL );
1239 ldap_back_op_result(
1245 ldap_back_send_t sendok )
1247 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1252 LDAPControl **ctrls = NULL;
1254 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1257 rs->sr_matched = NULL;
1259 rs->sr_ctrls = NULL;
1261 /* if the error recorded in the reply corresponds
1262 * to a successful state, get the error from the
1263 * remote server response */
1264 if ( ERR_OK( rs->sr_err ) ) {
1267 LDAPMessage *res = NULL;
1270 tv.tv_sec = timeout;
1274 LDAP_BACK_TV_SET( &tv );
1278 /* if result parsing fails, note the failure reason */
1279 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1283 (void)ldap_back_cancel( lc, op, rs, msgid, sendok );
1284 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
1285 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1286 rs->sr_text = "Operation timed out";
1290 LDAP_BACK_TV_SET( &tv );
1291 ldap_pvt_thread_yield();
1295 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1300 /* otherwise get the result; if it is not
1301 * LDAP_SUCCESS, record it in the reply
1302 * structure (this includes
1303 * LDAP_COMPARE_{TRUE|FALSE}) */
1305 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1306 &match, &text, &refs, &ctrls, 1 );
1308 if ( rc != LDAP_SUCCESS ) {
1311 if ( refs != NULL ) {
1314 for ( i = 0; refs[ i ] != NULL; i++ )
1316 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1318 for ( i = 0; refs[ i ] != NULL; i++ ) {
1319 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1321 BER_BVZERO( &rs->sr_ref[ i ] );
1323 if ( ctrls != NULL ) {
1324 rs->sr_ctrls = ctrls;
1329 /* if the error in the reply structure is not
1330 * LDAP_SUCCESS, try to map it from client
1331 * to server error */
1332 if ( !ERR_OK( rs->sr_err ) ) {
1333 rs->sr_err = slap_map_api2result( rs );
1335 /* internal ops ( op->o_conn == NULL )
1336 * must not reply to client */
1337 if ( op->o_conn && !op->o_do_not_cache && match ) {
1339 /* record the (massaged) matched
1340 * DN into the reply structure */
1341 rs->sr_matched = match;
1345 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1346 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1347 if ( LDAP_BACK_QUARANTINE( li ) ) {
1348 ldap_back_quarantine( op, rs );
1350 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1351 send_ldap_result( op, rs );
1355 } else if ( op->o_conn &&
1356 ( ( ( sendok & LDAP_BACK_SENDOK ) && ERR_OK( rs->sr_err ) )
1357 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1359 send_ldap_result( op, rs );
1363 if ( rs->sr_matched != match ) {
1364 free( (char *)rs->sr_matched );
1366 rs->sr_matched = NULL;
1367 ldap_memfree( match );
1371 ldap_memfree( text );
1376 assert( refs != NULL );
1377 ber_memvfree( (void **)refs );
1378 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1383 assert( rs->sr_ctrls != NULL );
1384 ldap_controls_free( ctrls );
1385 rs->sr_ctrls = NULL;
1388 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1391 /* return true if bound, false if failed */
1393 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1396 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1398 assert( lcp != NULL );
1399 assert( *lcp != NULL );
1401 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1403 if ( (*lcp)->lc_refcnt == 1 ) {
1404 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1405 Debug( LDAP_DEBUG_ANY,
1406 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1407 op->o_log_prefix, li->li_uri,
1408 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1409 "" : (*lcp)->lc_bound_ndn.bv_val );
1410 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1412 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1413 (*lcp)->lc_ld = NULL;
1414 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1416 /* lc here must be the regular lc, reset and ready for init */
1417 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1418 if ( rc != LDAP_SUCCESS ) {
1420 /* freeit, because lc_refcnt == 1 */
1421 (*lcp)->lc_refcnt = 0;
1422 (void)ldap_back_freeconn( op, *lcp, 0 );
1426 rc = ldap_back_dobind_int( *lcp, op, rs, sendok, 0, 0 );
1427 if ( rc == 0 && *lcp != NULL ) {
1428 /* freeit, because lc_refcnt == 1 */
1429 (*lcp)->lc_refcnt = 0;
1430 (void)ldap_back_freeconn( op, *lcp, 0 );
1436 Debug( LDAP_DEBUG_TRACE,
1437 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1438 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1440 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1441 ldap_back_release_conn_lock( op, rs, *lcp, 0 );
1445 rs->sr_err = LDAP_UNAVAILABLE;
1446 rs->sr_text = "unable to retry";
1447 send_ldap_result( op, rs );
1451 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1457 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1459 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1460 struct berval binddn = slap_empty_bv;
1461 struct berval bindcred = slap_empty_bv;
1467 /* don't proxyAuthz if protocol is not LDAPv3 */
1468 switch ( li->li_version ) {
1473 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1479 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1480 if ( sendok & LDAP_BACK_SENDERR ) {
1481 send_ldap_result( op, rs );
1483 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1487 if ( op->o_tag == LDAP_REQ_BIND ) {
1488 ndn = op->o_req_ndn;
1490 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1491 ndn = op->o_conn->c_ndn;
1498 * FIXME: we need to let clients use proxyAuthz
1499 * otherwise we cannot do symmetric pools of servers;
1500 * we have to live with the fact that a user can
1501 * authorize itself as any ID that is allowed
1502 * by the authzTo directive of the "proxyauthzdn".
1505 * NOTE: current Proxy Authorization specification
1506 * and implementation do not allow proxy authorization
1507 * control to be provided with Bind requests
1510 * if no bind took place yet, but the connection is bound
1511 * and the "proxyauthzdn" is set, then bind as
1512 * "proxyauthzdn" and explicitly add the proxyAuthz
1513 * control to every operation with the dn bound
1514 * to the connection as control value.
1517 /* bind as proxyauthzdn only if no idassert mode
1518 * is requested, or if the client's identity
1520 switch ( li->li_idassert_mode ) {
1521 case LDAP_BACK_IDASSERT_LEGACY:
1522 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1523 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1525 binddn = li->li_idassert_authcDN;
1526 bindcred = li->li_idassert_passwd;
1533 /* NOTE: rootdn can always idassert */
1534 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1535 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1536 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1537 if ( sendok & LDAP_BACK_SENDERR ) {
1538 send_ldap_result( op, rs );
1540 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1543 rs->sr_err = LDAP_SUCCESS;
1544 binddn = slap_empty_bv;
1545 bindcred = slap_empty_bv;
1551 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1552 struct berval authcDN;
1554 if ( BER_BVISNULL( &ndn ) ) {
1555 authcDN = slap_empty_bv;
1560 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1561 &authcDN, &authcDN );
1562 if ( rs->sr_err != LDAP_SUCCESS ) {
1563 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1564 if ( sendok & LDAP_BACK_SENDERR ) {
1565 send_ldap_result( op, rs );
1567 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1570 rs->sr_err = LDAP_SUCCESS;
1571 binddn = slap_empty_bv;
1572 bindcred = slap_empty_bv;
1580 binddn = li->li_idassert_authcDN;
1581 bindcred = li->li_idassert_passwd;
1586 if ( dobind && li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1587 #ifdef HAVE_CYRUS_SASL
1588 void *defaults = NULL;
1589 struct berval authzID = BER_BVNULL;
1592 /* if SASL supports native authz, prepare for it */
1593 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1594 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1596 switch ( li->li_idassert_mode ) {
1597 case LDAP_BACK_IDASSERT_OTHERID:
1598 case LDAP_BACK_IDASSERT_OTHERDN:
1599 authzID = li->li_idassert_authzID;
1602 case LDAP_BACK_IDASSERT_ANONYMOUS:
1603 BER_BVSTR( &authzID, "dn:" );
1606 case LDAP_BACK_IDASSERT_SELF:
1607 if ( BER_BVISNULL( &ndn ) ) {
1608 /* connection is not authc'd, so don't idassert */
1609 BER_BVSTR( &authzID, "dn:" );
1612 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1613 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1614 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1615 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1616 ndn.bv_val, ndn.bv_len + 1 );
1625 if ( li->li_idassert_secprops != NULL ) {
1626 rs->sr_err = ldap_set_option( lc->lc_ld,
1627 LDAP_OPT_X_SASL_SECPROPS,
1628 (void *)li->li_idassert_secprops );
1630 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1631 rs->sr_err = LDAP_OTHER;
1632 if ( sendok & LDAP_BACK_SENDERR ) {
1633 send_ldap_result( op, rs );
1635 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1640 defaults = lutil_sasl_defaults( lc->lc_ld,
1641 li->li_idassert_sasl_mech.bv_val,
1642 li->li_idassert_sasl_realm.bv_val,
1643 li->li_idassert_authcID.bv_val,
1644 li->li_idassert_passwd.bv_val,
1647 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1648 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1649 LDAP_SASL_QUIET, lutil_sasl_interact,
1652 rs->sr_err = slap_map_api2result( rs );
1653 if ( rs->sr_err != LDAP_SUCCESS ) {
1654 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1655 if ( sendok & LDAP_BACK_SENDERR ) {
1656 send_ldap_result( op, rs );
1660 LDAP_BACK_CONN_ISBOUND_SET( lc );
1663 lutil_sasl_freedefs( defaults );
1665 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1669 #endif /* HAVE_CYRUS_SASL */
1672 switch ( li->li_idassert_authmethod ) {
1673 case LDAP_AUTH_NONE:
1674 BER_BVSTR( &binddn, "" );
1675 BER_BVSTR( &bindcred, "" );
1678 case LDAP_AUTH_SIMPLE:
1679 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1680 binddn.bv_val, LDAP_SASL_SIMPLE,
1681 &bindcred, NULL, NULL, &msgid );
1682 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1687 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1688 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1689 if ( sendok & LDAP_BACK_SENDERR ) {
1690 send_ldap_result( op, rs );
1695 if ( rc == LDAP_SUCCESS ) {
1696 /* set rebind stuff in case of successful proxyAuthz bind,
1697 * so that referral chasing is attempted using the right
1699 LDAP_BACK_CONN_ISBOUND_SET( lc );
1700 ber_dupbv( &lc->lc_bound_ndn, &binddn );
1702 if ( LDAP_BACK_SAVECRED( li ) ) {
1703 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
1704 memset( lc->lc_cred.bv_val, 0,
1705 lc->lc_cred.bv_len );
1707 ber_bvreplace( &lc->lc_cred, &bindcred );
1708 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1712 return LDAP_BACK_CONN_ISBOUND( lc );
1716 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1717 * to existing server-side controls if required; if not,
1718 * the existing server-side controls are placed in *pctrls.
1719 * The caller, after using the controls in client API
1720 * operations, if ( *pctrls != op->o_ctrls ), should
1721 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1722 * The function returns success if the control could
1723 * be added if required, or if it did nothing; in the future,
1724 * it might return some error if it failed.
1726 * if no bind took place yet, but the connection is bound
1727 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1728 * and explicitly add proxyAuthz the control to every operation
1729 * with the dn bound to the connection as control value.
1731 * If no server-side controls are defined for the operation,
1732 * simply add the proxyAuthz control; otherwise, if the
1733 * proxyAuthz control is not already set, add it as
1736 * FIXME: is controls order significant for security?
1737 * ANSWER: controls ordering and interoperability
1738 * must be indicated by the specs of each control; if none
1739 * is specified, the order is irrelevant.
1742 ldap_back_proxy_authz_ctrl(
1743 struct berval *bound_ndn,
1745 slap_idassert_t *si,
1748 LDAPControl ***pctrls )
1750 LDAPControl **ctrls = NULL;
1752 slap_idassert_mode_t mode;
1753 struct berval assertedID,
1758 rs->sr_err = LDAP_SUCCESS;
1760 /* don't proxyAuthz if protocol is not LDAPv3 */
1761 switch ( version ) {
1766 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1775 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1776 * but if it is not set this test fails. We need a different
1777 * means to detect if idassert is enabled */
1778 if ( ( BER_BVISNULL( &si->si_bc.sb_authcId ) || BER_BVISEMPTY( &si->si_bc.sb_authcId ) )
1779 && ( BER_BVISNULL( &si->si_bc.sb_binddn ) || BER_BVISEMPTY( &si->si_bc.sb_binddn ) ) )
1784 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1788 if ( op->o_tag == LDAP_REQ_BIND ) {
1789 ndn = op->o_req_ndn;
1791 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1792 ndn = op->o_conn->c_ndn;
1798 if ( si->si_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1799 if ( op->o_proxy_authz ) {
1801 * FIXME: we do not want to perform proxyAuthz
1802 * on behalf of the client, because this would
1803 * be performed with "proxyauthzdn" privileges.
1805 * This might actually be too strict, since
1806 * the "proxyauthzdn" authzTo, and each entry's
1807 * authzFrom attributes may be crafted
1808 * to avoid unwanted proxyAuthz to take place.
1811 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1812 rs->sr_text = "proxyAuthz not allowed within namingContext";
1817 if ( !BER_BVISNULL( bound_ndn ) ) {
1821 if ( BER_BVISNULL( &ndn ) ) {
1825 if ( BER_BVISNULL( &si->si_bc.sb_binddn ) ) {
1829 } else if ( si->si_bc.sb_method == LDAP_AUTH_SASL ) {
1830 if ( ( si->si_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1832 /* already asserted in SASL via native authz */
1836 } else if ( si->si_authz && !be_isroot( op ) ) {
1838 struct berval authcDN;
1840 if ( BER_BVISNULL( &ndn ) ) {
1841 authcDN = slap_empty_bv;
1845 rc = slap_sasl_matches( op, si->si_authz,
1846 &authcDN, & authcDN );
1847 if ( rc != LDAP_SUCCESS ) {
1848 if ( si->si_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1849 /* ndn is not authorized
1850 * to use idassert */
1857 if ( op->o_proxy_authz ) {
1860 * 1) ignore the already set proxyAuthz control
1861 * 2) leave it in place, and don't set ours
1863 * 4) reject the operation
1865 * option (4) is very drastic
1866 * option (3) will make the remote server reject
1867 * the operation, thus being equivalent to (4)
1868 * option (2) will likely break the idassert
1869 * assumptions, so we cannot accept it;
1870 * option (1) means that we are contradicting
1871 * the client's reques.
1873 * I think (4) is the only correct choice.
1875 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1876 rs->sr_text = "proxyAuthz not allowed within namingContext";
1879 if ( op->o_is_auth_check ) {
1880 mode = LDAP_BACK_IDASSERT_NOASSERT;
1887 case LDAP_BACK_IDASSERT_SELF:
1888 if ( BER_BVISNULL( &ndn ) ) {
1894 case LDAP_BACK_IDASSERT_LEGACY:
1895 /* original behavior:
1896 * assert the client's identity */
1897 if ( BER_BVISNULL( &ndn ) ) {
1898 assertedID = slap_empty_bv;
1904 case LDAP_BACK_IDASSERT_ANONYMOUS:
1905 /* assert "anonymous" */
1906 assertedID = slap_empty_bv;
1909 case LDAP_BACK_IDASSERT_NOASSERT:
1910 /* don't assert; bind as proxyauthzdn */
1913 case LDAP_BACK_IDASSERT_OTHERID:
1914 case LDAP_BACK_IDASSERT_OTHERDN:
1915 /* assert idassert DN */
1916 assertedID = si->si_bc.sb_authzId;
1923 if ( BER_BVISNULL( &assertedID ) ) {
1924 assertedID = slap_empty_bv;
1927 /* don't idassert the bound DN (ITS#4497) */
1928 if ( dn_match( &assertedID, bound_ndn ) ) {
1932 if ( op->o_ctrls ) {
1933 for ( i = 0; op->o_ctrls[ i ]; i++ )
1934 /* just count ctrls */ ;
1937 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
1939 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
1941 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1942 ctrls[ 0 ]->ldctl_iscritical = 1;
1944 switch ( si->si_mode ) {
1945 /* already in u:ID or dn:DN form */
1946 case LDAP_BACK_IDASSERT_OTHERID:
1947 case LDAP_BACK_IDASSERT_OTHERDN:
1948 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
1951 /* needs the dn: prefix */
1953 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1954 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
1956 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1957 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1958 assertedID.bv_val, assertedID.bv_len + 1 );
1962 /* Older versions of <draft-weltman-ldapv3-proxy> required
1963 * to encode the value of the authzID (and called it proxyDN);
1964 * this hack provides compatibility with those DSAs that
1965 * implement it this way */
1966 if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
1967 struct berval authzID = ctrls[ 0 ]->ldctl_value;
1968 BerElementBuffer berbuf;
1969 BerElement *ber = (BerElement *)&berbuf;
1972 ber_init2( ber, 0, LBER_USE_DER );
1973 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1975 tag = ber_printf( ber, "O", &authzID );
1976 if ( tag == LBER_ERROR ) {
1977 rs->sr_err = LDAP_OTHER;
1981 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1982 rs->sr_err = LDAP_OTHER;
1987 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1988 ber_free_buf( ber );
1990 if ( rs->sr_err != LDAP_SUCCESS ) {
1991 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1996 } else if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
1997 struct berval authzID = ctrls[ 0 ]->ldctl_value,
1999 BerElementBuffer berbuf;
2000 BerElement *ber = (BerElement *)&berbuf;
2003 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
2004 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2005 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2007 rs->sr_err = LDAP_PROTOCOL_ERROR;
2012 tmp.bv_val += STRLENOF( "dn:" );
2013 tmp.bv_len -= STRLENOF( "dn:" );
2015 ber_init2( ber, 0, LBER_USE_DER );
2016 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2018 /* apparently, Mozilla API encodes this
2019 * as "SEQUENCE { LDAPDN }" */
2020 tag = ber_printf( ber, "{O}", &tmp );
2021 if ( tag == LBER_ERROR ) {
2022 rs->sr_err = LDAP_OTHER;
2026 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
2027 rs->sr_err = LDAP_OTHER;
2032 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2033 ber_free_buf( ber );
2035 if ( rs->sr_err != LDAP_SUCCESS ) {
2036 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2041 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
2044 if ( op->o_ctrls ) {
2045 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
2046 ctrls[ i + 1 ] = op->o_ctrls[ i ];
2049 ctrls[ i + 1 ] = NULL;
2052 if ( ctrls == NULL ) {
2053 ctrls = op->o_ctrls;
2062 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
2064 LDAPControl **ctrls = *pctrls;
2066 /* we assume that the first control is the proxyAuthz
2067 * added by back-ldap, so it's the only one we explicitly
2069 if ( ctrls && ctrls != op->o_ctrls ) {
2070 assert( ctrls[ 0 ] != NULL );
2072 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
2073 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2076 op->o_tmpfree( ctrls, op->o_tmpmemctx );