1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2014 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 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
40 #define SLAP_AUTH_DN 1
42 #if LDAP_BACK_PRINT_CONNTREE > 0
48 { LDAP_BACK_FCONN_ISBOUND, 'B' },
49 { LDAP_BACK_FCONN_ISANON, 'A' },
50 { LDAP_BACK_FCONN_ISPRIV, 'P' },
51 { LDAP_BACK_FCONN_ISTLS, 'T' },
52 { LDAP_BACK_FCONN_BINDING, 'X' },
53 { LDAP_BACK_FCONN_TAINTED, 'E' },
54 { LDAP_BACK_FCONN_ABANDON, 'N' },
55 { LDAP_BACK_FCONN_ISIDASR, 'S' },
56 { LDAP_BACK_FCONN_CACHED, 'C' },
61 ldap_back_conn_print( ldapconn_t *lc, const char *avlstr )
63 char buf[ SLAP_TEXT_BUFLEN ];
64 char fbuf[ sizeof("BAPTIENSC") ];
67 ldap_back_conn2str( &lc->lc_base, buf, sizeof( buf ) );
68 for ( i = 0; flagsmap[ i ].c != '\0'; i++ ) {
69 if ( lc->lc_lcflags & flagsmap[i].f ) {
70 fbuf[i] = flagsmap[i].c;
78 fprintf( stderr, "lc=%p %s %s flags=0x%08x (%s)\n",
79 (void *)lc, buf, avlstr, lc->lc_lcflags, fbuf );
83 ldap_back_ravl_print( Avlnode *root, int depth )
92 ldap_back_ravl_print( root->avl_right, depth+1 );
94 for ( i = 0; i < depth; i++ ) {
95 fprintf( stderr, "-" );
99 ldap_back_conn_print( lc, avl_bf2str( root->avl_bf ) );
101 ldap_back_ravl_print( root->avl_left, depth + 1 );
104 static char* priv2str[] = {
115 ldap_back_print_conntree( ldapinfo_t *li, char *msg )
119 fprintf( stderr, "========> %s\n", msg );
121 for ( c = LDAP_BACK_PCONN_FIRST; c < LDAP_BACK_PCONN_LAST; c++ ) {
125 fprintf( stderr, " %s[%d]\n", priv2str[ c ], li->li_conn_priv[ c ].lic_num );
127 LDAP_TAILQ_FOREACH( lc, &li->li_conn_priv[ c ].lic_priv, lc_q )
129 fprintf( stderr, " [%d] ", i );
130 ldap_back_conn_print( lc, "" );
135 if ( li->li_conninfo.lai_tree == 0 ) {
136 fprintf( stderr, "\t(empty)\n" );
139 ldap_back_ravl_print( li->li_conninfo.lai_tree, 0 );
142 fprintf( stderr, "<======== %s\n", msg );
144 #endif /* LDAP_BACK_PRINT_CONNTREE */
147 ldap_back_freeconn( ldapinfo_t *li, ldapconn_t *lc, int dolock );
150 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
151 struct berval *binddn, struct berval *bindcred );
154 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
155 struct berval *binddn, struct berval *bindcred );
158 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs,
159 ldap_back_send_t sendok, struct berval *binddn, struct berval *bindcred );
162 ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs,
163 ldap_back_send_t sendok );
166 ldap_back_conndnlc_cmp( const void *c1, const void *c2 );
169 ldap_back_conn_delete( ldapinfo_t *li, ldapconn_t *lc )
171 if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) {
172 if ( LDAP_BACK_CONN_CACHED( lc ) ) {
173 assert( lc->lc_q.tqe_prev != NULL );
174 assert( li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num > 0 );
175 li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num--;
176 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv, lc, lc_q );
177 LDAP_TAILQ_ENTRY_INIT( lc, lc_q );
178 LDAP_BACK_CONN_CACHED_CLEAR( lc );
181 assert( LDAP_BACK_CONN_TAINTED( lc ) );
182 assert( lc->lc_q.tqe_prev == NULL );
186 ldapconn_t *tmplc = NULL;
188 if ( LDAP_BACK_CONN_CACHED( lc ) ) {
189 assert( !LDAP_BACK_CONN_TAINTED( lc ) );
190 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
191 ldap_back_conndnlc_cmp );
192 assert( tmplc == lc );
193 LDAP_BACK_CONN_CACHED_CLEAR( lc );
196 assert( LDAP_BACK_CONN_TAINTED( lc ) || tmplc == lc );
203 ldap_back_bind( Operation *op, SlapReply *rs )
205 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
208 LDAPControl **ctrls = NULL;
209 struct berval save_o_dn;
210 int save_o_do_not_cache,
213 ldap_back_send_t retrying = LDAP_BACK_RETRYING;
215 /* allow rootdn as a means to auth without the need to actually
216 * contact the proxied DSA */
217 switch ( be_rootdn_bind( op, rs ) ) {
218 case SLAP_CB_CONTINUE:
225 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR, NULL, NULL );
230 /* we can do (almost) whatever we want with this conn,
231 * because either it's temporary, or it's marked as binding */
232 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
233 ch_free( lc->lc_bound_ndn.bv_val );
234 BER_BVZERO( &lc->lc_bound_ndn );
236 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
237 memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
238 ch_free( lc->lc_cred.bv_val );
239 BER_BVZERO( &lc->lc_cred );
241 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
243 /* don't add proxyAuthz; set the bindDN */
244 save_o_dn = op->o_dn;
245 save_o_do_not_cache = op->o_do_not_cache;
246 op->o_dn = op->o_req_dn;
247 op->o_do_not_cache = 1;
250 rc = ldap_back_controls_add( op, rs, lc, &ctrls );
251 op->o_dn = save_o_dn;
252 op->o_do_not_cache = save_o_do_not_cache;
253 if ( rc != LDAP_SUCCESS ) {
254 send_ldap_result( op, rs );
255 ldap_back_release_conn( li, lc );
260 /* method is always LDAP_AUTH_SIMPLE if we got here */
261 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
263 &op->orb_cred, ctrls, NULL, &msgid );
264 /* FIXME: should we always retry, or only when piping the bind
265 * in the "override" connection pool? */
266 rc = ldap_back_op_result( lc, op, rs, msgid,
267 li->li_timeout[ SLAP_OP_BIND ],
268 LDAP_BACK_BIND_SERR | retrying );
269 if ( rc == LDAP_UNAVAILABLE && retrying ) {
270 retrying &= ~LDAP_BACK_RETRYING;
271 if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_BIND_SERR ) ) {
276 ldap_pvt_thread_mutex_lock( &li->li_counter_mutex );
277 ldap_pvt_mp_add( li->li_ops_completed[ SLAP_OP_BIND ], 1 );
278 ldap_pvt_thread_mutex_unlock( &li->li_counter_mutex );
280 ldap_back_controls_free( op, rs, &ctrls );
282 if ( rc == LDAP_SUCCESS ) {
283 op->o_conn->c_authz_cookie = op->o_bd->be_private;
285 /* If defined, proxyAuthz will be used also when
286 * back-ldap is the authorizing backend; for this
287 * purpose, after a successful bind the connection
288 * is left for further binds, and further operations
289 * on this client connection will use a default
290 * connection with identity assertion */
291 /* NOTE: use with care */
292 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
293 ldap_back_release_conn( li, lc );
297 /* rebind is now done inside ldap_back_proxy_authz_bind()
298 * in case of success */
299 LDAP_BACK_CONN_ISBOUND_SET( lc );
300 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
302 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
303 memset( lc->lc_cred.bv_val, 0,
304 lc->lc_cred.bv_len );
307 if ( LDAP_BACK_SAVECRED( li ) ) {
308 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
309 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
312 lc->lc_cred.bv_len = 0;
316 /* must re-insert if local DN changed as result of bind */
317 if ( !LDAP_BACK_CONN_ISBOUND( lc )
318 || ( !dn_match( &op->o_req_ndn, &lc->lc_local_ndn )
319 && !LDAP_BACK_PCONN_ISPRIV( lc ) ) )
324 /* wait for all other ops to release the connection */
326 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
327 if ( lc->lc_refcnt > 1 ) {
328 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
329 ldap_pvt_thread_yield();
333 #if LDAP_BACK_PRINT_CONNTREE > 0
334 ldap_back_print_conntree( li, ">>> ldap_back_bind" );
335 #endif /* LDAP_BACK_PRINT_CONNTREE */
337 assert( lc->lc_refcnt == 1 );
338 ldap_back_conn_delete( li, lc );
340 /* delete all cached connections with the current connection */
341 if ( LDAP_BACK_SINGLECONN( li ) ) {
342 while ( ( tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc, ldap_back_conn_cmp ) ) != NULL )
344 assert( !LDAP_BACK_PCONN_ISPRIV( lc ) );
345 Debug( LDAP_DEBUG_TRACE,
346 "=>ldap_back_bind: destroying conn %lu (refcnt=%u)\n",
347 lc->lc_conn->c_connid, lc->lc_refcnt, 0 );
349 if ( tmplc->lc_refcnt != 0 ) {
351 LDAP_BACK_CONN_TAINTED_SET( tmplc );
352 LDAP_BACK_CONN_CACHED_CLEAR( tmplc );
356 * Needs a test because the handler may be corrupted,
357 * and calling ldap_unbind on a corrupted header results
358 * in a segmentation fault
360 ldap_back_conn_free( tmplc );
365 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
366 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
367 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
368 LDAP_BACK_PCONN_ROOTDN_SET( lc, op );
370 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
371 ldap_back_conndn_cmp, ldap_back_conndn_dup );
374 #if LDAP_BACK_PRINT_CONNTREE > 0
375 ldap_back_print_conntree( li, "<<< ldap_back_bind" );
376 #endif /* LDAP_BACK_PRINT_CONNTREE */
378 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
381 LDAP_BACK_CONN_CACHED_SET( lc );
385 /* duplicate; someone else successfully bound
386 * on the same connection with the same identity;
387 * we can do this because lc_refcnt == 1 */
388 ldap_back_conn_free( lc );
394 ldap_back_release_conn( li, lc );
401 * ldap_back_conndn_cmp
403 * compares two ldapconn_t based on the value of the conn pointer
404 * and of the local DN; used by avl stuff for insert, lookup
408 ldap_back_conndn_cmp( const void *c1, const void *c2 )
410 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
411 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
414 /* If local DNs don't match, it is definitely not a match */
415 /* For shared sessions, conn is NULL. Only explicitly
416 * bound sessions will have non-NULL conn.
418 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
420 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
427 * ldap_back_conndnlc_cmp
429 * compares two ldapconn_t based on the value of the conn pointer,
430 * the local DN and the lc pointer; used by avl stuff for insert, lookup
434 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
436 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
437 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
440 /* If local DNs don't match, it is definitely not a match */
441 /* For shared sessions, conn is NULL. Only explicitly
442 * bound sessions will have non-NULL conn.
444 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
446 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
448 rc = SLAP_PTRCMP( lc1, lc2 );
458 * compares two ldapconn_t based on the value of the conn pointer;
459 * used by avl stuff for delete of all conns with the same connid
462 ldap_back_conn_cmp( const void *c1, const void *c2 )
464 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
465 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
467 /* For shared sessions, conn is NULL. Only explicitly
468 * bound sessions will have non-NULL conn.
470 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
474 * ldap_back_conndn_dup
476 * returns -1 in case a duplicate ldapconn_t has been inserted;
480 ldap_back_conndn_dup( void *c1, void *c2 )
482 ldapconn_t *lc1 = (ldapconn_t *)c1;
483 ldapconn_t *lc2 = (ldapconn_t *)c2;
485 /* Cannot have more than one shared session with same DN */
486 if ( lc1->lc_conn == lc2->lc_conn &&
487 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
496 ldap_back_freeconn( ldapinfo_t *li, ldapconn_t *lc, int dolock )
499 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
502 #if LDAP_BACK_PRINT_CONNTREE > 0
503 ldap_back_print_conntree( li, ">>> ldap_back_freeconn" );
504 #endif /* LDAP_BACK_PRINT_CONNTREE */
506 (void)ldap_back_conn_delete( li, lc );
508 if ( lc->lc_refcnt == 0 ) {
509 ldap_back_conn_free( (void *)lc );
512 #if LDAP_BACK_PRINT_CONNTREE > 0
513 ldap_back_print_conntree( li, "<<< ldap_back_freeconn" );
514 #endif /* LDAP_BACK_PRINT_CONNTREE */
517 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
534 int rc = LDAP_SUCCESS;
536 /* start TLS ("tls-[try-]{start,propagate}" statements) */
537 if ( ( LDAP_BACK_USE_TLS_F( flags ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS_F( flags ) ) )
538 && !ldap_is_ldaps_url( url ) )
540 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
542 * use asynchronous StartTLS
543 * in case, chase referral (not implemented yet)
547 if ( protocol == 0 ) {
548 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
552 if ( protocol < LDAP_VERSION3 ) {
553 /* we should rather bail out... */
554 rc = LDAP_UNWILLING_TO_PERFORM;
555 *text = "invalid protocol version";
558 if ( rc == LDAP_SUCCESS ) {
559 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
562 if ( rc == LDAP_SUCCESS ) {
563 LDAPMessage *res = NULL;
566 LDAP_BACK_TV_SET( &tv );
569 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
571 rc = LDAP_UNAVAILABLE;
573 } else if ( rc == 0 ) {
574 if ( retries != LDAP_BACK_RETRY_NEVER ) {
575 ldap_pvt_thread_yield();
579 LDAP_BACK_TV_SET( &tv );
582 rc = LDAP_UNAVAILABLE;
584 } else if ( rc == LDAP_RES_EXTENDED ) {
585 struct berval *data = NULL;
587 rc = ldap_parse_extended_result( ld, res,
589 if ( rc == LDAP_SUCCESS ) {
591 rc = ldap_parse_result( ld, res, &rs.sr_err,
592 NULL, NULL, NULL, NULL, 1 );
593 if ( rc != LDAP_SUCCESS ) {
596 rc = slap_map_api2result( &rs );
599 /* FIXME: in case a referral
600 * is returned, should we try
601 * using it instead of the
603 if ( rc == LDAP_SUCCESS ) {
604 rc = ldap_install_tls( ld );
606 } else if ( rc == LDAP_REFERRAL ) {
607 rc = LDAP_UNWILLING_TO_PERFORM;
608 *text = "unwilling to chase referral returned by Start TLS exop";
612 if ( data->bv_val ) {
613 ber_memfree( data->bv_val );
627 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
629 * use synchronous StartTLS
631 rc = ldap_start_tls_s( ld, NULL, NULL );
632 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
634 /* if StartTLS is requested, only attempt it if the URL
635 * is not "ldaps://"; this may occur not only in case
636 * of misconfiguration, but also when used in the chain
637 * overlay, where the "uri" can be parsed out of a referral */
643 case LDAP_SERVER_DOWN:
647 if ( LDAP_BACK_TLS_CRITICAL_F( flags ) ) {
648 *text = "could not start TLS";
652 /* in case Start TLS is not critical */
664 #endif /* HAVE_TLS */
667 ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
669 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
673 int is_tls = op->o_conn->c_is_tls;
674 int flags = li->li_flags;
675 time_t lctime = (time_t)(-1);
677 #endif /* HAVE_TLS */
679 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
680 rs->sr_err = ldap_initialize( &ld, li->li_uri );
681 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
682 if ( rs->sr_err != LDAP_SUCCESS ) {
686 if ( li->li_urllist_f ) {
687 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
690 /* Set LDAP version. This will always succeed: If the client
691 * bound with a particular version, then so can we.
693 if ( li->li_version != 0 ) {
694 version = li->li_version;
696 } else if ( op->o_protocol != 0 ) {
697 version = op->o_protocol;
700 /* assume it's an internal op; set to LDAPv3 */
701 version = LDAP_VERSION3;
703 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
705 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
706 ldap_set_option( ld, LDAP_OPT_REFERRALS,
707 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
709 if ( li->li_network_timeout > 0 ) {
712 tv.tv_sec = li->li_network_timeout;
714 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
717 /* turn on network keepalive, if configured so */
718 slap_client_keepalive(ld, &li->li_tls.sb_keepalive);
721 if ( LDAP_BACK_CONN_ISPRIV( lc ) ) {
722 /* See "rationale" comment in ldap_back_getconn() */
723 if ( li->li_acl_authmethod == LDAP_AUTH_NONE &&
724 li->li_idassert_authmethod != LDAP_AUTH_NONE )
725 sb = &li->li_idassert.si_bc;
729 } else if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
730 sb = &li->li_idassert.si_bc;
736 if ( sb->sb_tls_do_init ) {
737 bindconf_tls_set( sb, ld );
738 } else if ( sb->sb_tls_ctx ) {
739 ldap_set_option( ld, LDAP_OPT_X_TLS_CTX, sb->sb_tls_ctx );
742 /* if required by the bindconf configuration, force TLS */
743 if ( ( sb == &li->li_acl || sb == &li->li_idassert.si_bc ) &&
746 flags |= LDAP_BACK_F_USE_TLS;
749 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
750 assert( li->li_uri_mutex_do_not_lock == 0 );
751 li->li_uri_mutex_do_not_lock = 1;
752 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
753 li->li_uri, flags, li->li_nretries, &rs->sr_text );
754 li->li_uri_mutex_do_not_lock = 0;
755 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
756 if ( rs->sr_err != LDAP_SUCCESS ) {
757 ldap_unbind_ext( ld, NULL, NULL );
758 rs->sr_text = "Start TLS failed";
761 } else if ( li->li_idle_timeout ) {
762 /* only touch when activity actually took place... */
765 #endif /* HAVE_TLS */
771 LDAP_BACK_CONN_ISTLS_SET( lc );
773 LDAP_BACK_CONN_ISTLS_CLEAR( lc );
775 if ( lctime != (time_t)(-1) ) {
776 lc->lc_time = lctime;
778 #endif /* HAVE_TLS */
781 if ( rs->sr_err != LDAP_SUCCESS ) {
782 rs->sr_err = slap_map_api2result( rs );
783 if ( sendok & LDAP_BACK_SENDERR ) {
784 if ( rs->sr_text == NULL ) {
785 rs->sr_text = "Proxy connection initialization failed";
787 send_ldap_result( op, rs );
791 if ( li->li_conn_ttl > 0 ) {
792 lc->lc_create_time = op->o_time;
803 ldap_back_send_t sendok,
804 struct berval *binddn,
805 struct berval *bindcred )
807 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
808 ldapconn_t *lc = NULL,
811 lookupconn = !( sendok & LDAP_BACK_BINDING );
813 /* if the server is quarantined, and
814 * - the current interval did not expire yet, or
815 * - no more retries should occur,
816 * don't return the connection */
817 if ( li->li_isquarantined ) {
818 slap_retry_info_t *ri = &li->li_quarantine;
821 if ( li->li_quarantine.ri_interval ) {
822 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
823 if ( li->li_isquarantined == LDAP_BACK_FQ_YES ) {
824 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
825 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
827 Debug( LDAP_DEBUG_ANY,
828 "%s: ldap_back_getconn quarantine "
829 "retry block #%d try #%d.\n",
830 op->o_log_prefix, ri->ri_idx, ri->ri_count );
831 li->li_isquarantined = LDAP_BACK_FQ_RETRYING;
834 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
838 rs->sr_err = LDAP_UNAVAILABLE;
839 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
840 rs->sr_text = "Target is quarantined";
841 send_ldap_result( op, rs );
847 /* Internal searches are privileged and shared. So is root. */
848 if ( op->o_do_not_cache || be_isroot( op ) ) {
849 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
850 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
851 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
854 struct berval tmpbinddn,
861 if ( binddn == NULL ) {
864 if ( bindcred == NULL ) {
865 bindcred = &tmpbindcred;
867 if ( op->o_tag == LDAP_REQ_BIND ) {
868 save_o_dn = op->o_dn;
869 save_o_ndn = op->o_ndn;
870 op->o_dn = op->o_req_dn;
871 op->o_ndn = op->o_req_ndn;
873 isproxyauthz = ldap_back_is_proxy_authz( op, rs, sendok, binddn, bindcred );
874 if ( op->o_tag == LDAP_REQ_BIND ) {
875 op->o_dn = save_o_dn;
876 op->o_ndn = save_o_ndn;
878 if ( isproxyauthz == -1 ) {
882 lc_curr.lc_local_ndn = op->o_ndn;
883 /* Explicit binds must not be shared;
884 * however, explicit binds are piped in a special connection
885 * when idassert is to occur with "override" set */
886 if ( op->o_tag == LDAP_REQ_BIND && !isproxyauthz ) {
887 lc_curr.lc_conn = op->o_conn;
890 if ( isproxyauthz && !( sendok & LDAP_BACK_BINDING ) ) {
891 lc_curr.lc_local_ndn = *binddn;
892 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
893 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
895 } else if ( isproxyauthz && ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) {
896 lc_curr.lc_local_ndn = slap_empty_bv;
897 LDAP_BACK_PCONN_BIND_SET( &lc_curr, op );
898 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
901 } else if ( SLAP_IS_AUTHZ_BACKEND( op ) ) {
902 lc_curr.lc_conn = op->o_conn;
905 LDAP_BACK_PCONN_ANON_SET( &lc_curr, op );
910 /* Explicit Bind requests always get their own conn */
913 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
914 if ( LDAP_BACK_PCONN_ISPRIV( &lc_curr ) ) {
915 /* lookup a conn that's not binding */
916 LDAP_TAILQ_FOREACH( lc,
917 &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv,
920 if ( !LDAP_BACK_CONN_BINDING( lc ) && lc->lc_refcnt == 0 ) {
926 if ( lc != LDAP_TAILQ_LAST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
929 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
931 LDAP_TAILQ_ENTRY_INIT( lc, lc_q );
932 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
936 } else if ( !LDAP_BACK_USE_TEMPORARIES( li )
937 && li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_num == li->li_conn_priv_max )
939 lc = LDAP_TAILQ_FIRST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv );
944 /* Searches for a ldapconn in the avl tree */
945 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
946 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
950 /* Don't reuse connections while they're still binding */
951 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
952 if ( !LDAP_BACK_USE_TEMPORARIES( li ) ) {
953 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
955 ldap_pvt_thread_yield();
962 if ( op->o_tag == LDAP_REQ_BIND ) {
963 /* right now, this is the only possible case */
964 assert( ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) );
965 LDAP_BACK_CONN_BINDING_SET( lc );
968 refcnt = ++lc->lc_refcnt;
971 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
974 /* Looks like we didn't get a bind. Open a new session... */
976 lc = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
977 lc->lc_flags = li->li_flags;
978 lc->lc_lcflags = lc_curr.lc_lcflags;
979 if ( ldap_back_prepare_conn( lc, op, rs, sendok ) != LDAP_SUCCESS ) {
984 if ( sendok & LDAP_BACK_BINDING ) {
985 LDAP_BACK_CONN_BINDING_SET( lc );
988 lc->lc_conn = lc_curr.lc_conn;
989 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
992 * the rationale is: connections as the rootdn are privileged,
993 * so li_acl is to be used; however, in some cases
994 * one already configured identity assertion with a highly
995 * privileged idassert_authcDN, so if li_acl is not configured
996 * and idassert is, use idassert instead.
998 * might change in the future, because it's preferable
999 * to make clear what identity is being used, since
1000 * the only drawback is that one risks to configure
1001 * the same identity twice...
1003 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
1004 if ( li->li_acl_authmethod == LDAP_AUTH_NONE &&
1005 li->li_idassert_authmethod != LDAP_AUTH_NONE ) {
1006 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
1007 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
1010 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
1011 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
1013 LDAP_BACK_CONN_ISPRIV_SET( lc );
1015 } else if ( LDAP_BACK_CONN_ISIDASSERT( &lc_curr ) ) {
1016 if ( !LDAP_BACK_PCONN_ISBIND( &lc_curr ) ) {
1017 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
1018 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
1020 LDAP_BACK_CONN_ISIDASSERT_SET( lc );
1023 BER_BVZERO( &lc->lc_cred );
1024 BER_BVZERO( &lc->lc_bound_ndn );
1025 if ( !BER_BVISEMPTY( &op->o_ndn )
1026 && SLAP_IS_AUTHZ_BACKEND( op ) )
1028 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
1033 /* if start TLS failed but it was not mandatory,
1034 * check if the non-TLS connection was already
1035 * in cache; in case, destroy the newly created
1036 * connection and use the existing one */
1037 if ( LDAP_BACK_PCONN_ISTLS( lc )
1038 && !ldap_tls_inplace( lc->lc_ld ) )
1040 ldapconn_t *tmplc = NULL;
1041 int idx = LDAP_BACK_CONN2PRIV( &lc_curr ) - 1;
1043 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1044 LDAP_TAILQ_FOREACH( tmplc,
1045 &li->li_conn_priv[ idx ].lic_priv,
1048 if ( !LDAP_BACK_CONN_BINDING( tmplc ) ) {
1053 if ( tmplc != NULL ) {
1054 refcnt = ++tmplc->lc_refcnt;
1055 ldap_back_conn_free( lc );
1058 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1060 if ( tmplc != NULL ) {
1064 #endif /* HAVE_TLS */
1066 /* Inserts the newly created ldapconn in the avl tree */
1067 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1069 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1070 lc->lc_connid = li->li_conn_nextid++;
1072 assert( lc->lc_refcnt == 1 );
1074 #if LDAP_BACK_PRINT_CONNTREE > 0
1075 ldap_back_print_conntree( li, ">>> ldap_back_getconn(insert)" );
1076 #endif /* LDAP_BACK_PRINT_CONNTREE */
1078 if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1079 if ( li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num < li->li_conn_priv_max ) {
1080 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv, lc, lc_q );
1081 li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num++;
1082 LDAP_BACK_CONN_CACHED_SET( lc );
1085 LDAP_BACK_CONN_TAINTED_SET( lc );
1090 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
1091 ldap_back_conndn_cmp, ldap_back_conndn_dup );
1092 LDAP_BACK_CONN_CACHED_SET( lc );
1095 #if LDAP_BACK_PRINT_CONNTREE > 0
1096 ldap_back_print_conntree( li, "<<< ldap_back_getconn(insert)" );
1097 #endif /* LDAP_BACK_PRINT_CONNTREE */
1099 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1101 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1102 char buf[ SLAP_TEXT_BUFLEN ];
1104 snprintf( buf, sizeof( buf ),
1105 "lc=%p inserted refcnt=%u rc=%d",
1106 (void *)lc, refcnt, rs->sr_err );
1108 Debug( LDAP_DEBUG_TRACE,
1109 "=>ldap_back_getconn: %s: %s\n",
1110 op->o_log_prefix, buf, 0 );
1113 if ( !LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1114 /* Err could be -1 in case a duplicate ldapconn is inserted */
1115 switch ( rs->sr_err ) {
1120 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1121 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( li ) ) {
1122 /* duplicate: free and try to get the newly created one */
1123 ldap_back_conn_free( lc );
1128 /* taint connection, so that it'll be freed when released */
1129 LDAP_BACK_CONN_TAINTED_SET( lc );
1133 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1134 ldap_back_conn_free( lc );
1135 rs->sr_err = LDAP_OTHER;
1136 rs->sr_text = "Proxy bind collision";
1137 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1138 send_ldap_result( op, rs );
1147 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
1148 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
1152 /* let it be used, but taint/delete it so that
1153 * no-one else can look it up any further */
1154 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1156 #if LDAP_BACK_PRINT_CONNTREE > 0
1157 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1158 #endif /* LDAP_BACK_PRINT_CONNTREE */
1160 (void)ldap_back_conn_delete( li, lc );
1161 LDAP_BACK_CONN_TAINTED_SET( lc );
1163 #if LDAP_BACK_PRINT_CONNTREE > 0
1164 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1165 #endif /* LDAP_BACK_PRINT_CONNTREE */
1167 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1170 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1171 char buf[ SLAP_TEXT_BUFLEN ];
1173 snprintf( buf, sizeof( buf ),
1174 "conn %p fetched refcnt=%u%s",
1176 expiring ? " expiring" : "" );
1177 Debug( LDAP_DEBUG_TRACE,
1178 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
1184 #endif /* HAVE_TLS */
1190 ldap_back_release_conn_lock(
1196 ldapconn_t *lc = *lcp;
1199 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1201 assert( lc->lc_refcnt > 0 );
1202 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1204 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
1205 ldap_back_freeconn( li, lc, 0 );
1209 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1214 ldap_back_quarantine(
1218 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1220 slap_retry_info_t *ri = &li->li_quarantine;
1222 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
1224 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1225 time_t new_last = slap_get_time();
1227 switch ( li->li_isquarantined ) {
1228 case LDAP_BACK_FQ_NO:
1229 if ( ri->ri_last == new_last ) {
1233 Debug( LDAP_DEBUG_ANY,
1234 "%s: ldap_back_quarantine enter.\n",
1235 op->o_log_prefix, 0, 0 );
1241 case LDAP_BACK_FQ_RETRYING:
1242 Debug( LDAP_DEBUG_ANY,
1243 "%s: ldap_back_quarantine block #%d try #%d failed.\n",
1244 op->o_log_prefix, ri->ri_idx, ri->ri_count );
1247 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1248 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1259 li->li_isquarantined = LDAP_BACK_FQ_YES;
1260 ri->ri_last = new_last;
1262 } else if ( li->li_isquarantined != LDAP_BACK_FQ_NO ) {
1263 if ( ri->ri_last == slap_get_time() ) {
1267 Debug( LDAP_DEBUG_ANY,
1268 "%s: ldap_back_quarantine exit (%d) err=%d.\n",
1269 op->o_log_prefix, li->li_isquarantined, rs->sr_err );
1271 if ( li->li_quarantine_f ) {
1272 (void)li->li_quarantine_f( li, li->li_quarantine_p );
1277 li->li_isquarantined = LDAP_BACK_FQ_NO;
1281 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
1285 ldap_back_dobind_cb(
1290 ber_tag_t *tptr = op->o_callback->sc_private;
1292 rs->sr_tag = slap_req2res( op->o_tag );
1294 return SLAP_CB_CONTINUE;
1298 * ldap_back_dobind_int
1300 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1303 ldap_back_dobind_int(
1307 ldap_back_send_t sendok,
1311 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1314 struct berval binddn = slap_empty_bv,
1315 bindcred = slap_empty_bv;
1321 ber_tag_t o_tag = op->o_tag;
1322 slap_callback cb = {0};
1325 assert( lcp != NULL );
1326 assert( retries >= 0 );
1328 if ( sendok & LDAP_BACK_GETCONN ) {
1329 assert( *lcp == NULL );
1331 lc = ldap_back_getconn( op, rs, sendok, &binddn, &bindcred );
1341 assert( lc != NULL );
1345 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1348 if ( binding == 0 ) {
1349 /* check if already bound */
1350 rc = isbound = LDAP_BACK_CONN_ISBOUND( lc );
1353 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1358 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
1359 /* if someone else is about to bind it, give up and retry */
1361 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1363 ldap_pvt_thread_yield();
1367 /* otherwise this thread will bind it */
1368 LDAP_BACK_CONN_BINDING_SET( lc );
1374 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1378 * FIXME: we need to let clients use proxyAuthz
1379 * otherwise we cannot do symmetric pools of servers;
1380 * we have to live with the fact that a user can
1381 * authorize itself as any ID that is allowed
1382 * by the authzTo directive of the "proxyauthzdn".
1385 * NOTE: current Proxy Authorization specification
1386 * and implementation do not allow proxy authorization
1387 * control to be provided with Bind requests
1390 * if no bind took place yet, but the connection is bound
1391 * and the "idassert-authcDN" (or other ID) is set,
1392 * then bind as the asserting identity and explicitly
1393 * add the proxyAuthz control to every operation with the
1394 * dn bound to the connection as control value.
1395 * This is done also if this is the authorizing backend,
1396 * but the "override" flag is given to idassert.
1397 * It allows to use SASL bind and yet proxyAuthz users
1399 op->o_tag = LDAP_REQ_BIND;
1400 cb.sc_next = op->o_callback;
1401 cb.sc_private = &o_tag;
1402 cb.sc_response = ldap_back_dobind_cb;
1403 op->o_callback = &cb;
1405 if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
1406 if ( BER_BVISEMPTY( &binddn ) && BER_BVISEMPTY( &bindcred ) ) {
1407 /* if we got here, it shouldn't return result */
1408 rc = ldap_back_is_proxy_authz( op, rs,
1409 LDAP_BACK_DONTSEND, &binddn, &bindcred );
1411 Debug( LDAP_DEBUG_ANY, "Error: ldap_back_is_proxy_authz "
1412 "returned %d, misconfigured URI?\n", rc, 0, 0 );
1413 rs->sr_err = LDAP_OTHER;
1414 rs->sr_text = "misconfigured URI?";
1415 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1416 if ( sendok & LDAP_BACK_SENDERR ) {
1417 send_ldap_result( op, rs );
1422 rc = ldap_back_proxy_authz_bind( lc, op, rs, sendok, &binddn, &bindcred );
1426 #ifdef HAVE_CYRUS_SASL
1427 if ( LDAP_BACK_CONN_ISPRIV( lc )) {
1429 if ( li->li_acl_authmethod != LDAP_AUTH_NONE )
1432 sb = &li->li_idassert.si_bc;
1434 if ( sb->sb_method == LDAP_AUTH_SASL ) {
1435 void *defaults = NULL;
1437 if ( sb->sb_secprops != NULL ) {
1438 rc = ldap_set_option( lc->lc_ld,
1439 LDAP_OPT_X_SASL_SECPROPS, sb->sb_secprops );
1441 if ( rc != LDAP_OPT_SUCCESS ) {
1442 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
1443 "(SECPROPS,\"%s\") failed!\n",
1444 sb->sb_secprops, 0, 0 );
1449 defaults = lutil_sasl_defaults( lc->lc_ld,
1450 sb->sb_saslmech.bv_val,
1451 sb->sb_realm.bv_val,
1452 sb->sb_authcId.bv_val,
1455 if ( defaults == NULL ) {
1456 rs->sr_err = LDAP_OTHER;
1457 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1458 if ( sendok & LDAP_BACK_SENDERR ) {
1459 send_ldap_result( op, rs );
1464 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
1465 sb->sb_binddn.bv_val,
1466 sb->sb_saslmech.bv_val, NULL, NULL,
1467 LDAP_SASL_QUIET, lutil_sasl_interact,
1470 ldap_pvt_thread_mutex_lock( &li->li_counter_mutex );
1471 ldap_pvt_mp_add( li->li_ops_completed[ SLAP_OP_BIND ], 1 );
1472 ldap_pvt_thread_mutex_unlock( &li->li_counter_mutex );
1474 lutil_sasl_freedefs( defaults );
1476 switch ( rs->sr_err ) {
1478 LDAP_BACK_CONN_ISBOUND_SET( lc );
1481 case LDAP_LOCAL_ERROR:
1482 /* list client API error codes that require
1483 * to taint the connection */
1484 /* FIXME: should actually retry? */
1485 LDAP_BACK_CONN_TAINTED_SET( lc );
1490 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1491 rs->sr_err = slap_map_api2result( rs );
1492 if ( sendok & LDAP_BACK_SENDERR ) {
1493 send_ldap_result( op, rs );
1498 if ( LDAP_BACK_QUARANTINE( li ) ) {
1499 ldap_back_quarantine( op, rs );
1505 #endif /* HAVE_CYRUS_SASL */
1508 if ( BER_BVISNULL( &lc->lc_cred ) ) {
1510 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) && !BER_BVISEMPTY( &lc->lc_bound_ndn ) ) {
1511 Debug( LDAP_DEBUG_ANY, "%s ldap_back_dobind_int: DN=\"%s\" without creds, binding anonymously",
1512 op->o_log_prefix, lc->lc_bound_ndn.bv_val, 0 );
1516 tmp_dn = lc->lc_bound_ndn.bv_val;
1518 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1520 LDAP_SASL_SIMPLE, &lc->lc_cred,
1521 NULL, NULL, &msgid );
1523 ldap_pvt_thread_mutex_lock( &li->li_counter_mutex );
1524 ldap_pvt_mp_add( li->li_ops_completed[ SLAP_OP_BIND ], 1 );
1525 ldap_pvt_thread_mutex_unlock( &li->li_counter_mutex );
1527 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
1528 if ( retries != LDAP_BACK_RETRY_NEVER ) {
1530 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1533 assert( lc->lc_refcnt > 0 );
1534 if ( lc->lc_refcnt == 1 ) {
1535 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1538 /* lc here must be the regular lc, reset and ready for init */
1539 rs->sr_err = ldap_back_prepare_conn( lc, op, rs, sendok );
1540 if ( rs->sr_err != LDAP_SUCCESS ) {
1541 sendok &= ~LDAP_BACK_SENDERR;
1547 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1550 if ( rs->sr_err == LDAP_SUCCESS ) {
1551 if ( retries > 0 ) {
1558 assert( lc->lc_refcnt == 1 );
1560 ldap_back_freeconn( li, lc, dolock );
1562 rs->sr_err = slap_map_api2result( rs );
1564 if ( LDAP_BACK_QUARANTINE( li ) ) {
1565 ldap_back_quarantine( op, rs );
1568 if ( rs->sr_err != LDAP_SUCCESS &&
1569 ( sendok & LDAP_BACK_SENDERR ) )
1571 if ( op->o_callback == &cb )
1572 op->o_callback = cb.sc_next;
1574 rs->sr_text = "Proxy can't contact remote server";
1575 send_ldap_result( op, rs );
1582 rc = ldap_back_op_result( lc, op, rs, msgid,
1583 -1, ( sendok | LDAP_BACK_BINDING ) );
1584 if ( rc == LDAP_SUCCESS ) {
1585 LDAP_BACK_CONN_ISBOUND_SET( lc );
1589 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1590 rc = LDAP_BACK_CONN_ISBOUND( lc );
1592 ldap_back_release_conn_lock( li, lcp, dolock );
1594 } else if ( LDAP_BACK_SAVECRED( li ) ) {
1595 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1599 if ( op->o_callback == &cb )
1600 op->o_callback = cb.sc_next;
1609 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1612 ldap_back_dobind( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1614 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1616 return ldap_back_dobind_int( lcp, op, rs,
1617 ( sendok | LDAP_BACK_GETCONN ), li->li_nretries, 1 );
1621 * ldap_back_default_rebind
1623 * This is a callback used for chasing referrals using the same
1624 * credentials as the original user on this session.
1627 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1628 ber_int_t msgid, void *params )
1630 ldapconn_t *lc = (ldapconn_t *)params;
1633 /* ... otherwise we couldn't get here */
1634 assert( lc != NULL );
1636 if ( !ldap_tls_inplace( ld ) ) {
1637 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1639 const char *text = NULL;
1641 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1642 LDAP_BACK_RETRY_DEFAULT, &text );
1643 if ( rc != LDAP_SUCCESS ) {
1647 #endif /* HAVE_TLS */
1649 /* FIXME: add checks on the URL/identity? */
1650 /* TODO: would like to count this bind operation for monitoring
1651 * too, but where do we get the ldapinfo_t? */
1653 return ldap_sasl_bind_s( ld,
1654 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1655 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1659 * ldap_back_default_urllist
1662 ldap_back_default_urllist(
1664 LDAPURLDesc **urllist,
1668 ldapinfo_t *li = (ldapinfo_t *)params;
1669 LDAPURLDesc **urltail;
1671 if ( urllist == url ) {
1672 return LDAP_SUCCESS;
1675 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1678 *urltail = *urllist;
1682 if ( !li->li_uri_mutex_do_not_lock ) {
1683 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1687 ch_free( li->li_uri );
1690 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1692 if ( !li->li_uri_mutex_do_not_lock ) {
1693 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1696 return LDAP_SUCCESS;
1705 ldap_back_send_t sendok )
1707 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1709 /* default behavior */
1710 if ( LDAP_BACK_ABANDON( li ) ) {
1711 return ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1714 if ( LDAP_BACK_IGNORE( li ) ) {
1715 return ldap_pvt_discard( lc->lc_ld, msgid );
1718 if ( LDAP_BACK_CANCEL( li ) ) {
1719 /* FIXME: asynchronous? */
1720 return ldap_cancel_s( lc->lc_ld, msgid, NULL, NULL );
1729 ldap_back_op_result(
1735 ldap_back_send_t sendok )
1737 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1742 LDAPControl **ctrls = NULL;
1745 rs->sr_matched = NULL;
1747 rs->sr_ctrls = NULL;
1749 /* if the error recorded in the reply corresponds
1750 * to a successful state, get the error from the
1751 * remote server response */
1752 if ( LDAP_ERR_OK( rs->sr_err ) ) {
1755 LDAPMessage *res = NULL;
1756 time_t stoptime = (time_t)(-1);
1757 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
1758 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1759 const char *timeout_text = "Operation timed out";
1761 /* if timeout is not specified, compute and use
1762 * the one specific to the ongoing operation */
1763 if ( timeout == (time_t)(-1) ) {
1764 slap_op_t opidx = slap_req2op( op->o_tag );
1766 if ( opidx == SLAP_OP_SEARCH ) {
1767 if ( op->ors_tlimit <= 0 ) {
1771 timeout = op->ors_tlimit;
1772 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
1773 timeout_text = NULL;
1777 timeout = li->li_timeout[ opidx ];
1781 /* better than nothing :) */
1782 if ( timeout == 0 ) {
1783 if ( li->li_idle_timeout ) {
1784 timeout = li->li_idle_timeout;
1786 } else if ( li->li_conn_ttl ) {
1787 timeout = li->li_conn_ttl;
1792 stoptime = op->o_time + timeout;
1795 LDAP_BACK_TV_SET( &tv );
1798 /* if result parsing fails, note the failure reason */
1799 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1802 if ( timeout && slap_get_time() > stoptime ) {
1803 if ( sendok & LDAP_BACK_BINDING ) {
1804 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1807 /* let it be used, but taint/delete it so that
1808 * no-one else can look it up any further */
1809 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1811 #if LDAP_BACK_PRINT_CONNTREE > 0
1812 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1813 #endif /* LDAP_BACK_PRINT_CONNTREE */
1815 (void)ldap_back_conn_delete( li, lc );
1816 LDAP_BACK_CONN_TAINTED_SET( lc );
1818 #if LDAP_BACK_PRINT_CONNTREE > 0
1819 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1820 #endif /* LDAP_BACK_PRINT_CONNTREE */
1821 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1824 (void)ldap_back_cancel( lc, op, rs, msgid, sendok );
1826 rs->sr_err = timeout_err;
1827 rs->sr_text = timeout_text;
1832 LDAP_BACK_TV_SET( &tv );
1833 ldap_pvt_thread_yield();
1837 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1842 /* otherwise get the result; if it is not
1843 * LDAP_SUCCESS, record it in the reply
1844 * structure (this includes
1845 * LDAP_COMPARE_{TRUE|FALSE}) */
1847 /* only touch when activity actually took place... */
1848 if ( li->li_idle_timeout ) {
1849 lc->lc_time = op->o_time;
1852 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1853 &match, &text, &refs, &ctrls, 1 );
1854 if ( rc == LDAP_SUCCESS ) {
1859 rs->sr_err = slap_map_api2result( rs );
1861 /* RFC 4511: referrals can only appear
1862 * if result code is LDAP_REFERRAL */
1864 && refs[ 0 ] != NULL
1865 && refs[ 0 ][ 0 ] != '\0' )
1867 if ( rs->sr_err != LDAP_REFERRAL ) {
1868 Debug( LDAP_DEBUG_ANY,
1869 "%s ldap_back_op_result: "
1870 "got referrals with err=%d\n",
1877 for ( i = 0; refs[ i ] != NULL; i++ )
1879 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1881 for ( i = 0; refs[ i ] != NULL; i++ ) {
1882 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1884 BER_BVZERO( &rs->sr_ref[ i ] );
1887 } else if ( rs->sr_err == LDAP_REFERRAL ) {
1888 Debug( LDAP_DEBUG_ANY,
1889 "%s ldap_back_op_result: "
1890 "got err=%d with null "
1891 "or empty referrals\n",
1895 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1898 if ( ctrls != NULL ) {
1899 rs->sr_ctrls = ctrls;
1904 /* if the error in the reply structure is not
1905 * LDAP_SUCCESS, try to map it from client
1906 * to server error */
1907 if ( !LDAP_ERR_OK( rs->sr_err ) ) {
1908 rs->sr_err = slap_map_api2result( rs );
1910 /* internal ops ( op->o_conn == NULL )
1911 * must not reply to client */
1912 if ( op->o_conn && !op->o_do_not_cache && match ) {
1914 /* record the (massaged) matched
1915 * DN into the reply structure */
1916 rs->sr_matched = match;
1920 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1921 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1922 if ( LDAP_BACK_QUARANTINE( li ) ) {
1923 ldap_back_quarantine( op, rs );
1925 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1926 if ( rs->sr_text == NULL ) rs->sr_text = "Proxy operation retry failed";
1927 send_ldap_result( op, rs );
1931 } else if ( op->o_conn &&
1932 ( ( ( sendok & LDAP_BACK_SENDOK ) && LDAP_ERR_OK( rs->sr_err ) )
1933 || ( ( sendok & LDAP_BACK_SENDERR ) && !LDAP_ERR_OK( rs->sr_err ) ) ) )
1935 send_ldap_result( op, rs );
1939 ldap_memfree( text );
1943 /* there can't be refs with a (successful) bind */
1945 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1950 ber_memvfree( (void **)refs );
1953 /* match should not be possible with a successful bind */
1955 if ( rs->sr_matched != match ) {
1956 free( (char *)rs->sr_matched );
1958 rs->sr_matched = NULL;
1959 ldap_memfree( match );
1962 if ( ctrls != NULL ) {
1963 if ( op->o_tag == LDAP_REQ_BIND && rs->sr_err == LDAP_SUCCESS ) {
1966 for ( i = 0; ctrls[i] != NULL; i++ );
1968 rs->sr_ctrls = op->o_tmpalloc( sizeof( LDAPControl * )*( i + 1 ),
1970 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
1972 ber_len_t oidlen = strlen( ctrls[i]->ldctl_oid );
1973 ber_len_t size = sizeof( LDAPControl )
1975 + ctrls[i]->ldctl_value.bv_len + 1;
1977 rs->sr_ctrls[ i ] = op->o_tmpalloc( size, op->o_tmpmemctx );
1978 rs->sr_ctrls[ i ]->ldctl_oid = (char *)&rs->sr_ctrls[ i ][ 1 ];
1979 lutil_strcopy( rs->sr_ctrls[ i ]->ldctl_oid, ctrls[i]->ldctl_oid );
1980 rs->sr_ctrls[ i ]->ldctl_value.bv_val
1981 = (char *)&rs->sr_ctrls[ i ]->ldctl_oid[oidlen + 1];
1982 rs->sr_ctrls[ i ]->ldctl_value.bv_len
1983 = ctrls[i]->ldctl_value.bv_len;
1984 ptr = lutil_memcopy( rs->sr_ctrls[ i ]->ldctl_value.bv_val,
1985 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len );
1988 rs->sr_ctrls[ i ] = NULL;
1989 rs->sr_flags |= REP_CTRLS_MUSTBEFREED;
1992 assert( rs->sr_ctrls != NULL );
1993 rs->sr_ctrls = NULL;
1996 ldap_controls_free( ctrls );
1999 return( LDAP_ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
2002 /* return true if bound, false if failed */
2004 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
2006 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2009 assert( lcp != NULL );
2010 assert( *lcp != NULL );
2012 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
2014 if ( (*lcp)->lc_refcnt == 1 ) {
2015 int binding = LDAP_BACK_CONN_BINDING( *lcp );
2017 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
2018 Debug( LDAP_DEBUG_ANY,
2019 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
2020 op->o_log_prefix, li->li_uri,
2021 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
2022 "" : (*lcp)->lc_bound_ndn.bv_val );
2023 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
2025 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
2026 (*lcp)->lc_ld = NULL;
2027 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
2029 /* lc here must be the regular lc, reset and ready for init */
2030 rc = ldap_back_prepare_conn( *lcp, op, rs, sendok );
2031 if ( rc != LDAP_SUCCESS ) {
2032 /* freeit, because lc_refcnt == 1 */
2033 (*lcp)->lc_refcnt = 0;
2034 (void)ldap_back_freeconn( li, *lcp, 0 );
2038 } else if ( ( sendok & LDAP_BACK_BINDING ) ) {
2040 LDAP_BACK_CONN_BINDING_SET( *lcp );
2045 rc = ldap_back_dobind_int( lcp, op, rs, sendok, 0, 0 );
2046 if ( rc == 0 && *lcp != NULL ) {
2047 /* freeit, because lc_refcnt == 1 */
2048 (*lcp)->lc_refcnt = 0;
2049 LDAP_BACK_CONN_TAINTED_SET( *lcp );
2050 (void)ldap_back_freeconn( li, *lcp, 0 );
2056 Debug( LDAP_DEBUG_TRACE,
2057 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
2058 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
2060 LDAP_BACK_CONN_TAINTED_SET( *lcp );
2061 ldap_back_release_conn_lock( li, lcp, 0 );
2062 assert( *lcp == NULL );
2064 if ( sendok & LDAP_BACK_SENDERR ) {
2065 rs->sr_err = LDAP_UNAVAILABLE;
2066 rs->sr_text = "Unable to retry";
2067 send_ldap_result( op, rs );
2071 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
2077 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
2078 struct berval *binddn, struct berval *bindcred )
2080 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2084 if ( op->o_conn == NULL || op->o_do_not_cache ) {
2088 /* don't proxyAuthz if protocol is not LDAPv3 */
2089 switch ( li->li_version ) {
2094 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2100 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2101 if ( sendok & LDAP_BACK_SENDERR ) {
2102 send_ldap_result( op, rs );
2109 *binddn = slap_empty_bv;
2110 *bindcred = slap_empty_bv;
2112 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2113 ndn = op->o_conn->c_ndn;
2119 if ( !( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE )) {
2120 if ( op->o_tag == LDAP_REQ_BIND ) {
2121 if ( !BER_BVISEMPTY( &ndn )) {
2125 } else if ( SLAP_IS_AUTHZ_BACKEND( op )) {
2131 switch ( li->li_idassert_mode ) {
2132 case LDAP_BACK_IDASSERT_LEGACY:
2133 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
2134 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
2136 *binddn = li->li_idassert_authcDN;
2137 *bindcred = li->li_idassert_passwd;
2144 /* NOTE: rootdn can always idassert */
2145 if ( BER_BVISNULL( &ndn )
2146 && li->li_idassert_authz == NULL
2147 && !( li->li_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL ) )
2149 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2150 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
2151 if ( sendok & LDAP_BACK_SENDERR ) {
2152 send_ldap_result( op, rs );
2157 rs->sr_err = LDAP_SUCCESS;
2158 *binddn = slap_empty_bv;
2159 *bindcred = slap_empty_bv;
2165 } else if ( !be_isroot( op ) ) {
2166 if ( li->li_idassert_passthru ) {
2167 struct berval authcDN;
2169 if ( BER_BVISNULL( &ndn ) ) {
2170 authcDN = slap_empty_bv;
2175 rs->sr_err = slap_sasl_matches( op, li->li_idassert_passthru,
2176 &authcDN, &authcDN );
2177 if ( rs->sr_err == LDAP_SUCCESS ) {
2183 if ( li->li_idassert_authz ) {
2184 struct berval authcDN;
2186 if ( BER_BVISNULL( &ndn ) ) {
2187 authcDN = slap_empty_bv;
2192 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
2193 &authcDN, &authcDN );
2194 if ( rs->sr_err != LDAP_SUCCESS ) {
2195 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2196 if ( sendok & LDAP_BACK_SENDERR ) {
2197 send_ldap_result( op, rs );
2202 rs->sr_err = LDAP_SUCCESS;
2203 *binddn = slap_empty_bv;
2204 *bindcred = slap_empty_bv;
2213 *binddn = li->li_idassert_authcDN;
2214 *bindcred = li->li_idassert_passwd;
2224 ldap_back_proxy_authz_bind(
2228 ldap_back_send_t sendok,
2229 struct berval *binddn,
2230 struct berval *bindcred )
2232 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2237 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2238 ndn = op->o_conn->c_ndn;
2244 if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
2245 #ifdef HAVE_CYRUS_SASL
2246 void *defaults = NULL;
2247 struct berval authzID = BER_BVNULL;
2249 LDAPControl **ctrlsp = NULL;
2250 LDAPMessage *result = NULL;
2251 const char *rmech = NULL;
2252 const char *save_text = rs->sr_text;
2255 LDAPControl ctrl, *ctrls[2];
2257 #endif /* SLAP_AUTH_DN */
2259 /* if SASL supports native authz, prepare for it */
2260 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
2261 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2263 switch ( li->li_idassert_mode ) {
2264 case LDAP_BACK_IDASSERT_OTHERID:
2265 case LDAP_BACK_IDASSERT_OTHERDN:
2266 authzID = li->li_idassert_authzID;
2269 case LDAP_BACK_IDASSERT_ANONYMOUS:
2270 BER_BVSTR( &authzID, "dn:" );
2273 case LDAP_BACK_IDASSERT_SELF:
2274 if ( BER_BVISNULL( &ndn ) ) {
2275 /* connection is not authc'd, so don't idassert */
2276 BER_BVSTR( &authzID, "dn:" );
2279 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
2280 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
2281 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
2282 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
2283 ndn.bv_val, ndn.bv_len + 1 );
2292 if ( li->li_idassert_secprops != NULL ) {
2293 rs->sr_err = ldap_set_option( lc->lc_ld,
2294 LDAP_OPT_X_SASL_SECPROPS,
2295 (void *)li->li_idassert_secprops );
2297 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
2298 rs->sr_err = LDAP_OTHER;
2299 if ( sendok & LDAP_BACK_SENDERR ) {
2300 send_ldap_result( op, rs );
2302 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2307 defaults = lutil_sasl_defaults( lc->lc_ld,
2308 li->li_idassert_sasl_mech.bv_val,
2309 li->li_idassert_sasl_realm.bv_val,
2310 li->li_idassert_authcID.bv_val,
2311 li->li_idassert_passwd.bv_val,
2313 if ( defaults == NULL ) {
2314 rs->sr_err = LDAP_OTHER;
2315 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2316 if ( sendok & LDAP_BACK_SENDERR ) {
2317 send_ldap_result( op, rs );
2323 if ( li->li_idassert_flags & LDAP_BACK_AUTH_DN_AUTHZID ) {
2324 assert( BER_BVISNULL( binddn ) );
2326 ctrl.ldctl_oid = LDAP_CONTROL_AUTHZID_REQUEST;
2327 ctrl.ldctl_iscritical = 0;
2328 BER_BVZERO( &ctrl.ldctl_value );
2333 #endif /* SLAP_AUTH_DN */
2336 rs->sr_err = ldap_sasl_interactive_bind( lc->lc_ld, binddn->bv_val,
2337 li->li_idassert_sasl_mech.bv_val,
2338 ctrlsp, NULL, LDAP_SASL_QUIET, lutil_sasl_interact, defaults,
2339 result, &rmech, &msgid );
2341 if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS )
2344 ldap_msgfree( result );
2346 if ( ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 || !result ) {
2347 ldap_get_option( lc->lc_ld, LDAP_OPT_RESULT_CODE, (void*)&rs->sr_err );
2348 ldap_get_option( lc->lc_ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&rs->sr_text );
2351 } while ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
2353 ldap_pvt_thread_mutex_lock( &li->li_counter_mutex );
2354 ldap_pvt_mp_add( li->li_ops_completed[ SLAP_OP_BIND ], 1 );
2355 ldap_pvt_thread_mutex_unlock( &li->li_counter_mutex );
2357 switch ( rs->sr_err ) {
2360 /* FIXME: right now, the only reason to check
2361 * response controls is RFC 3829 authzid */
2362 if ( li->li_idassert_flags & LDAP_BACK_AUTH_DN_AUTHZID ) {
2364 rc = ldap_parse_result( lc->lc_ld, result, NULL, NULL, NULL, NULL,
2366 if ( rc == LDAP_SUCCESS && ctrlsp ) {
2369 ctrl = ldap_control_find( LDAP_CONTROL_AUTHZID_RESPONSE,
2372 Debug( LDAP_DEBUG_TRACE, "%s: ldap_back_proxy_authz_bind: authzID=\"%s\" (authzid)\n",
2373 op->o_log_prefix, ctrl->ldctl_value.bv_val, 0 );
2374 if ( ctrl->ldctl_value.bv_len > STRLENOF("dn:") &&
2375 strncasecmp( ctrl->ldctl_value.bv_val, "dn:", STRLENOF("dn:") ) == 0 )
2378 bv.bv_val = &ctrl->ldctl_value.bv_val[STRLENOF("dn:")];
2379 bv.bv_len = ctrl->ldctl_value.bv_len - STRLENOF("dn:");
2380 ber_bvreplace( &lc->lc_bound_ndn, &bv );
2385 ldap_controls_free( ctrlsp );
2387 } else if ( li->li_idassert_flags & LDAP_BACK_AUTH_DN_WHOAMI ) {
2388 struct berval *val = NULL;
2389 rc = ldap_whoami_s( lc->lc_ld, &val, NULL, NULL );
2390 if ( rc == LDAP_SUCCESS && val != NULL ) {
2391 Debug( LDAP_DEBUG_TRACE, "%s: ldap_back_proxy_authz_bind: authzID=\"%s\" (whoami)\n",
2392 op->o_log_prefix, val->bv_val, 0 );
2393 if ( val->bv_len > STRLENOF("dn:") &&
2394 strncasecmp( val->bv_val, "dn:", STRLENOF("dn:") ) == 0 )
2397 bv.bv_val = &val->bv_val[STRLENOF("dn:")];
2398 bv.bv_len = val->bv_len - STRLENOF("dn:");
2399 ber_bvreplace( &lc->lc_bound_ndn, &bv );
2405 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_DN_MASK ) &&
2406 BER_BVISNULL( &lc->lc_bound_ndn ) )
2408 /* all in all, we only need it to be non-null */
2409 /* FIXME: should this be configurable? */
2410 static struct berval bv = BER_BVC("cn=authzdn");
2411 ber_bvreplace( &lc->lc_bound_ndn, &bv );
2413 #endif /* SLAP_AUTH_DN */
2414 LDAP_BACK_CONN_ISBOUND_SET( lc );
2417 case LDAP_LOCAL_ERROR:
2418 /* list client API error codes that require
2419 * to taint the connection */
2420 /* FIXME: should actually retry? */
2421 LDAP_BACK_CONN_TAINTED_SET( lc );
2426 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2427 rs->sr_err = slap_map_api2result( rs );
2428 if ( sendok & LDAP_BACK_SENDERR ) {
2429 send_ldap_result( op, rs );
2434 if ( save_text != rs->sr_text ) {
2435 ldap_memfree( (char *)rs->sr_text );
2436 rs->sr_text = save_text;
2439 ldap_msgfree( result );
2441 lutil_sasl_freedefs( defaults );
2443 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
2447 #endif /* HAVE_CYRUS_SASL */
2450 switch ( li->li_idassert_authmethod ) {
2451 case LDAP_AUTH_NONE:
2452 /* FIXME: do we really need this? */
2453 BER_BVSTR( binddn, "" );
2454 BER_BVSTR( bindcred, "" );
2457 case LDAP_AUTH_SIMPLE:
2458 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
2459 binddn->bv_val, LDAP_SASL_SIMPLE,
2460 bindcred, NULL, NULL, &msgid );
2461 rc = ldap_back_op_result( lc, op, rs, msgid,
2462 -1, ( sendok | LDAP_BACK_BINDING ) );
2464 ldap_pvt_thread_mutex_lock( &li->li_counter_mutex );
2465 ldap_pvt_mp_add( li->li_ops_completed[ SLAP_OP_BIND ], 1 );
2466 ldap_pvt_thread_mutex_unlock( &li->li_counter_mutex );
2471 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2472 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
2473 if ( sendok & LDAP_BACK_SENDERR ) {
2474 send_ldap_result( op, rs );
2479 if ( rc == LDAP_SUCCESS ) {
2480 /* set rebind stuff in case of successful proxyAuthz bind,
2481 * so that referral chasing is attempted using the right
2483 LDAP_BACK_CONN_ISBOUND_SET( lc );
2484 if ( !BER_BVISNULL( binddn ) ) {
2485 ber_bvreplace( &lc->lc_bound_ndn, binddn );
2488 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
2489 memset( lc->lc_cred.bv_val, 0,
2490 lc->lc_cred.bv_len );
2493 if ( LDAP_BACK_SAVECRED( li ) ) {
2494 if ( !BER_BVISNULL( bindcred ) ) {
2495 ber_bvreplace( &lc->lc_cred, bindcred );
2496 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
2500 lc->lc_cred.bv_len = 0;
2505 return LDAP_BACK_CONN_ISBOUND( lc );
2509 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
2510 * to existing server-side controls if required; if not,
2511 * the existing server-side controls are placed in *pctrls.
2512 * The caller, after using the controls in client API
2513 * operations, if ( *pctrls != op->o_ctrls ), should
2514 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
2515 * The function returns success if the control could
2516 * be added if required, or if it did nothing; in the future,
2517 * it might return some error if it failed.
2519 * if no bind took place yet, but the connection is bound
2520 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
2521 * and explicitly add proxyAuthz the control to every operation
2522 * with the dn bound to the connection as control value.
2524 * If no server-side controls are defined for the operation,
2525 * simply add the proxyAuthz control; otherwise, if the
2526 * proxyAuthz control is not already set, add it as
2529 * FIXME: is controls order significant for security?
2530 * ANSWER: controls ordering and interoperability
2531 * must be indicated by the specs of each control; if none
2532 * is specified, the order is irrelevant.
2535 ldap_back_proxy_authz_ctrl(
2538 struct berval *bound_ndn,
2540 slap_idassert_t *si,
2543 slap_idassert_mode_t mode;
2544 struct berval assertedID,
2548 rs->sr_err = SLAP_CB_CONTINUE;
2550 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
2551 * but if it is not set this test fails. We need a different
2552 * means to detect if idassert is enabled */
2553 if ( ( BER_BVISNULL( &si->si_bc.sb_authcId ) || BER_BVISEMPTY( &si->si_bc.sb_authcId ) )
2554 && ( BER_BVISNULL( &si->si_bc.sb_binddn ) || BER_BVISEMPTY( &si->si_bc.sb_binddn ) )
2555 && BER_BVISNULL( &si->si_bc.sb_saslmech ) )
2560 if ( !op->o_conn || op->o_do_not_cache || ( isroot = be_isroot( op ) ) ) {
2564 if ( op->o_tag == LDAP_REQ_BIND ) {
2565 ndn = op->o_req_ndn;
2567 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2568 ndn = op->o_conn->c_ndn;
2574 if ( si->si_mode == LDAP_BACK_IDASSERT_LEGACY ) {
2575 if ( op->o_proxy_authz ) {
2577 * FIXME: we do not want to perform proxyAuthz
2578 * on behalf of the client, because this would
2579 * be performed with "proxyauthzdn" privileges.
2581 * This might actually be too strict, since
2582 * the "proxyauthzdn" authzTo, and each entry's
2583 * authzFrom attributes may be crafted
2584 * to avoid unwanted proxyAuthz to take place.
2587 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2588 rs->sr_text = "proxyAuthz not allowed within namingContext";
2593 if ( !BER_BVISNULL( bound_ndn ) ) {
2597 if ( BER_BVISNULL( &ndn ) ) {
2601 if ( BER_BVISNULL( &si->si_bc.sb_binddn ) ) {
2605 } else if ( si->si_bc.sb_method == LDAP_AUTH_SASL ) {
2606 if ( ( si->si_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2608 /* already asserted in SASL via native authz */
2612 } else if ( si->si_authz && !isroot ) {
2614 struct berval authcDN;
2616 if ( BER_BVISNULL( &ndn ) ) {
2617 authcDN = slap_empty_bv;
2621 rc = slap_sasl_matches( op, si->si_authz,
2622 &authcDN, &authcDN );
2623 if ( rc != LDAP_SUCCESS ) {
2624 if ( si->si_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2625 /* ndn is not authorized
2626 * to use idassert */
2633 if ( op->o_proxy_authz ) {
2636 * 1) ignore the already set proxyAuthz control
2637 * 2) leave it in place, and don't set ours
2639 * 4) reject the operation
2641 * option (4) is very drastic
2642 * option (3) will make the remote server reject
2643 * the operation, thus being equivalent to (4)
2644 * option (2) will likely break the idassert
2645 * assumptions, so we cannot accept it;
2646 * option (1) means that we are contradicting
2647 * the client's reques.
2649 * I think (4) is the only correct choice.
2651 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2652 rs->sr_text = "proxyAuthz not allowed within namingContext";
2655 if ( op->o_is_auth_check ) {
2656 mode = LDAP_BACK_IDASSERT_NOASSERT;
2663 case LDAP_BACK_IDASSERT_LEGACY:
2664 /* original behavior:
2665 * assert the client's identity */
2666 case LDAP_BACK_IDASSERT_SELF:
2670 case LDAP_BACK_IDASSERT_ANONYMOUS:
2671 /* assert "anonymous" */
2672 assertedID = slap_empty_bv;
2675 case LDAP_BACK_IDASSERT_NOASSERT:
2676 /* don't assert; bind as proxyauthzdn */
2679 case LDAP_BACK_IDASSERT_OTHERID:
2680 case LDAP_BACK_IDASSERT_OTHERDN:
2681 /* assert idassert DN */
2682 assertedID = si->si_bc.sb_authzId;
2689 /* if we got here, "" is allowed to proxyAuthz */
2690 if ( BER_BVISNULL( &assertedID ) ) {
2691 assertedID = slap_empty_bv;
2694 /* don't idassert the bound DN (ITS#4497) */
2695 if ( dn_match( &assertedID, bound_ndn ) ) {
2699 ctrl->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
2700 ctrl->ldctl_iscritical = ( ( si->si_flags & LDAP_BACK_AUTH_PROXYAUTHZ_CRITICAL ) == LDAP_BACK_AUTH_PROXYAUTHZ_CRITICAL );
2702 switch ( si->si_mode ) {
2703 /* already in u:ID or dn:DN form */
2704 case LDAP_BACK_IDASSERT_OTHERID:
2705 case LDAP_BACK_IDASSERT_OTHERDN:
2706 ber_dupbv_x( &ctrl->ldctl_value, &assertedID, op->o_tmpmemctx );
2707 rs->sr_err = LDAP_SUCCESS;
2710 /* needs the dn: prefix */
2712 ctrl->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
2713 ctrl->ldctl_value.bv_val = op->o_tmpalloc( ctrl->ldctl_value.bv_len + 1,
2715 AC_MEMCPY( ctrl->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
2716 AC_MEMCPY( &ctrl->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
2717 assertedID.bv_val, assertedID.bv_len + 1 );
2718 rs->sr_err = LDAP_SUCCESS;
2722 /* Older versions of <draft-weltman-ldapv3-proxy> required
2723 * to encode the value of the authzID (and called it proxyDN);
2724 * this hack provides compatibility with those DSAs that
2725 * implement it this way */
2726 if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
2727 struct berval authzID = ctrl->ldctl_value;
2728 BerElementBuffer berbuf;
2729 BerElement *ber = (BerElement *)&berbuf;
2732 ber_init2( ber, 0, LBER_USE_DER );
2733 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2735 tag = ber_printf( ber, "O", &authzID );
2736 if ( tag == LBER_ERROR ) {
2737 rs->sr_err = LDAP_OTHER;
2741 if ( ber_flatten2( ber, &ctrl->ldctl_value, 1 ) == -1 ) {
2742 rs->sr_err = LDAP_OTHER;
2746 rs->sr_err = LDAP_SUCCESS;
2749 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2750 ber_free_buf( ber );
2752 if ( rs->sr_err != LDAP_SUCCESS ) {
2756 } else if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
2757 struct berval authzID = ctrl->ldctl_value,
2759 BerElementBuffer berbuf;
2760 BerElement *ber = (BerElement *)&berbuf;
2763 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
2764 rs->sr_err = LDAP_PROTOCOL_ERROR;
2769 tmp.bv_val += STRLENOF( "dn:" );
2770 tmp.bv_len -= STRLENOF( "dn:" );
2772 ber_init2( ber, 0, LBER_USE_DER );
2773 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2775 /* apparently, Mozilla API encodes this
2776 * as "SEQUENCE { LDAPDN }" */
2777 tag = ber_printf( ber, "{O}", &tmp );
2778 if ( tag == LBER_ERROR ) {
2779 rs->sr_err = LDAP_OTHER;
2783 if ( ber_flatten2( ber, &ctrl->ldctl_value, 1 ) == -1 ) {
2784 rs->sr_err = LDAP_OTHER;
2788 ctrl->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
2789 rs->sr_err = LDAP_SUCCESS;
2792 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2793 ber_free_buf( ber );
2795 if ( rs->sr_err != LDAP_SUCCESS ) {
2808 * if any needs to be added, it is prepended to existing ones,
2809 * in a newly allocated array. The companion function
2810 * ldap_back_controls_free() must be used to restore the original
2811 * status of op->o_ctrls.
2814 ldap_back_controls_add(
2818 LDAPControl ***pctrls )
2820 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2822 LDAPControl **ctrls = NULL;
2823 /* set to the maximum number of controls this backend can add */
2824 LDAPControl c[ 2 ] = { { 0 } };
2825 int n = 0, i, j1 = 0, j2 = 0;
2829 rs->sr_err = LDAP_SUCCESS;
2831 /* don't add controls if protocol is not LDAPv3 */
2832 switch ( li->li_version ) {
2837 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2846 /* put controls that go __before__ existing ones here */
2848 /* proxyAuthz for identity assertion */
2849 switch ( ldap_back_proxy_authz_ctrl( op, rs, &lc->lc_bound_ndn,
2850 li->li_version, &li->li_idassert, &c[ j1 ] ) )
2852 case SLAP_CB_CONTINUE:
2863 /* put controls that go __after__ existing ones here */
2865 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
2866 /* FIXME: according to <draft-wahl-ldap-session>,
2867 * the server should check if the control can be added
2868 * based on the identity of the client and so */
2870 /* session tracking */
2871 if ( LDAP_BACK_ST_REQUEST( li ) ) {
2872 switch ( slap_ctrl_session_tracking_request_add( op, rs, &c[ j1 + j2 ] ) ) {
2873 case SLAP_CB_CONTINUE:
2884 #endif /* SLAP_CONTROL_X_SESSION_TRACKING */
2886 if ( rs->sr_err == SLAP_CB_CONTINUE ) {
2887 rs->sr_err = LDAP_SUCCESS;
2890 /* if nothing to do, just bail out */
2891 if ( j1 == 0 && j2 == 0 ) {
2895 assert( j1 + j2 <= (int) (sizeof( c )/sizeof( c[0] )) );
2897 if ( op->o_ctrls ) {
2898 for ( n = 0; op->o_ctrls[ n ]; n++ )
2899 /* just count ctrls */ ;
2902 ctrls = op->o_tmpalloc( (n + j1 + j2 + 1) * sizeof( LDAPControl * ) + ( j1 + j2 ) * sizeof( LDAPControl ),
2905 ctrls[ 0 ] = (LDAPControl *)&ctrls[ n + j1 + j2 + 1 ];
2906 *ctrls[ 0 ] = c[ 0 ];
2907 for ( i = 1; i < j1; i++ ) {
2908 ctrls[ i ] = &ctrls[ 0 ][ i ];
2909 *ctrls[ i ] = c[ i ];
2914 if ( op->o_ctrls ) {
2915 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
2916 ctrls[ i + j1 ] = op->o_ctrls[ i ];
2922 ctrls[ n ] = (LDAPControl *)&ctrls[ n + j2 + 1 ] + j1;
2923 *ctrls[ n ] = c[ j1 ];
2924 for ( i = 1; i < j2; i++ ) {
2925 ctrls[ n + i ] = &ctrls[ n ][ i ];
2926 *ctrls[ n + i ] = c[ i ];
2930 ctrls[ n + j2 ] = NULL;
2933 if ( ctrls == NULL ) {
2934 ctrls = op->o_ctrls;
2943 ldap_back_controls_free( Operation *op, SlapReply *rs, LDAPControl ***pctrls )
2945 LDAPControl **ctrls = *pctrls;
2947 /* we assume that the controls added by the proxy come first,
2948 * so as soon as we find op->o_ctrls[ 0 ] we can stop */
2949 if ( ctrls && ctrls != op->o_ctrls ) {
2950 int i = 0, n = 0, n_added;
2951 LDAPControl *lower, *upper;
2953 assert( ctrls[ 0 ] != NULL );
2955 for ( n = 0; ctrls[ n ] != NULL; n++ )
2958 if ( op->o_ctrls ) {
2959 for ( i = 0; op->o_ctrls[ i ] != NULL; i++ )
2964 lower = (LDAPControl *)&ctrls[ n ];
2965 upper = &lower[ n_added ];
2967 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
2968 if ( ctrls[ i ] < lower || ctrls[ i ] >= upper ) {
2969 /* original; don't touch */
2973 if ( !BER_BVISNULL( &ctrls[ i ]->ldctl_value ) ) {
2974 op->o_tmpfree( ctrls[ i ]->ldctl_value.bv_val, op->o_tmpmemctx );
2978 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2987 ldap_back_conn2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen )
2989 char tbuf[ SLAP_TEXT_BUFLEN ];
2990 char *ptr = buf, *end = buf + buflen;
2993 if ( ptr + sizeof("conn=") > end ) return -1;
2994 ptr = lutil_strcopy( ptr, "conn=" );
2996 len = ldap_back_connid2str( lc, ptr, (ber_len_t)(end - ptr) );
2998 if ( ptr >= end ) return -1;
3000 if ( !BER_BVISNULL( &lc->lcb_local_ndn ) ) {
3001 if ( ptr + sizeof(" DN=\"\"") + lc->lcb_local_ndn.bv_len > end ) return -1;
3002 ptr = lutil_strcopy( ptr, " DN=\"" );
3003 ptr = lutil_strncopy( ptr, lc->lcb_local_ndn.bv_val, lc->lcb_local_ndn.bv_len );
3007 if ( lc->lcb_create_time != 0 ) {
3008 len = snprintf( tbuf, sizeof(tbuf), "%ld", lc->lcb_create_time );
3009 if ( ptr + sizeof(" created=") + len >= end ) return -1;
3010 ptr = lutil_strcopy( ptr, " created=" );
3011 ptr = lutil_strcopy( ptr, tbuf );
3014 if ( lc->lcb_time != 0 ) {
3015 len = snprintf( tbuf, sizeof(tbuf), "%ld", lc->lcb_time );
3016 if ( ptr + sizeof(" modified=") + len >= end ) return -1;
3017 ptr = lutil_strcopy( ptr, " modified=" );
3018 ptr = lutil_strcopy( ptr, tbuf );
3021 len = snprintf( tbuf, sizeof(tbuf), "%u", lc->lcb_refcnt );
3022 if ( ptr + sizeof(" refcnt=") + len >= end ) return -1;
3023 ptr = lutil_strcopy( ptr, " refcnt=" );
3024 ptr = lutil_strcopy( ptr, tbuf );
3030 ldap_back_connid2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen )
3032 static struct berval conns[] = {
3034 BER_BVC("ROOTDN-TLS"),
3036 BER_BVC("ANON-TLS"),
3038 BER_BVC("BIND-TLS"),
3044 if ( LDAP_BACK_PCONN_ISPRIV( (const ldapconn_t *)lc ) ) {
3048 cid = (long)lc->lcb_conn;
3049 assert( cid >= LDAP_BACK_PCONN_FIRST && cid < LDAP_BACK_PCONN_LAST );
3053 if ( bv->bv_len >= buflen ) {
3054 return bv->bv_len + 1;
3058 lutil_strncopy( buf, bv->bv_val, bv->bv_len + 1 );
3061 len = snprintf( buf, buflen, "%lu", lc->lcb_conn->c_connid );