1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2013 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"
41 #define SLAP_AUTH_DN 1
44 #if LDAP_BACK_PRINT_CONNTREE > 0
50 { LDAP_BACK_FCONN_ISBOUND, 'B' },
51 { LDAP_BACK_FCONN_ISANON, 'A' },
52 { LDAP_BACK_FCONN_ISPRIV, 'P' },
53 { LDAP_BACK_FCONN_ISTLS, 'T' },
54 { LDAP_BACK_FCONN_BINDING, 'X' },
55 { LDAP_BACK_FCONN_TAINTED, 'E' },
56 { LDAP_BACK_FCONN_ABANDON, 'N' },
57 { LDAP_BACK_FCONN_ISIDASR, 'S' },
58 { LDAP_BACK_FCONN_CACHED, 'C' },
63 ldap_back_conn_print( ldapconn_t *lc, const char *avlstr )
65 char buf[ SLAP_TEXT_BUFLEN ];
66 char fbuf[ sizeof("BAPTIENSC") ];
69 ldap_back_conn2str( &lc->lc_base, buf, sizeof( buf ) );
70 for ( i = 0; flagsmap[ i ].c != '\0'; i++ ) {
71 if ( lc->lc_lcflags & flagsmap[i].f ) {
72 fbuf[i] = flagsmap[i].c;
80 fprintf( stderr, "lc=%p %s %s flags=0x%08x (%s)\n",
81 (void *)lc, buf, avlstr, lc->lc_lcflags, fbuf );
85 ldap_back_ravl_print( Avlnode *root, int depth )
94 ldap_back_ravl_print( root->avl_right, depth+1 );
96 for ( i = 0; i < depth; i++ ) {
97 fprintf( stderr, "-" );
101 ldap_back_conn_print( lc, avl_bf2str( root->avl_bf ) );
103 ldap_back_ravl_print( root->avl_left, depth + 1 );
106 static char* priv2str[] = {
117 ldap_back_print_conntree( ldapinfo_t *li, char *msg )
121 fprintf( stderr, "========> %s\n", msg );
123 for ( c = LDAP_BACK_PCONN_FIRST; c < LDAP_BACK_PCONN_LAST; c++ ) {
127 fprintf( stderr, " %s[%d]\n", priv2str[ c ], li->li_conn_priv[ c ].lic_num );
129 LDAP_TAILQ_FOREACH( lc, &li->li_conn_priv[ c ].lic_priv, lc_q )
131 fprintf( stderr, " [%d] ", i );
132 ldap_back_conn_print( lc, "" );
137 if ( li->li_conninfo.lai_tree == 0 ) {
138 fprintf( stderr, "\t(empty)\n" );
141 ldap_back_ravl_print( li->li_conninfo.lai_tree, 0 );
144 fprintf( stderr, "<======== %s\n", msg );
146 #endif /* LDAP_BACK_PRINT_CONNTREE */
149 ldap_back_freeconn( ldapinfo_t *li, ldapconn_t *lc, int dolock );
152 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
153 struct berval *binddn, struct berval *bindcred );
156 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
157 struct berval *binddn, struct berval *bindcred );
160 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs,
161 ldap_back_send_t sendok, struct berval *binddn, struct berval *bindcred );
164 ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs,
165 ldap_back_send_t sendok );
168 ldap_back_conndnlc_cmp( const void *c1, const void *c2 );
171 ldap_back_conn_delete( ldapinfo_t *li, ldapconn_t *lc )
173 if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) {
174 if ( LDAP_BACK_CONN_CACHED( lc ) ) {
175 assert( lc->lc_q.tqe_prev != NULL );
176 assert( li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num > 0 );
177 li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num--;
178 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv, lc, lc_q );
179 LDAP_TAILQ_ENTRY_INIT( lc, lc_q );
180 LDAP_BACK_CONN_CACHED_CLEAR( lc );
183 assert( LDAP_BACK_CONN_TAINTED( lc ) );
184 assert( lc->lc_q.tqe_prev == NULL );
188 ldapconn_t *tmplc = NULL;
190 if ( LDAP_BACK_CONN_CACHED( lc ) ) {
191 assert( !LDAP_BACK_CONN_TAINTED( lc ) );
192 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
193 ldap_back_conndnlc_cmp );
194 assert( tmplc == lc );
195 LDAP_BACK_CONN_CACHED_CLEAR( lc );
198 assert( LDAP_BACK_CONN_TAINTED( lc ) || tmplc == lc );
205 ldap_back_bind( Operation *op, SlapReply *rs )
207 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
210 LDAPControl **ctrls = NULL;
211 struct berval save_o_dn;
212 int save_o_do_not_cache,
215 ldap_back_send_t retrying = LDAP_BACK_RETRYING;
217 /* allow rootdn as a means to auth without the need to actually
218 * contact the proxied DSA */
219 switch ( be_rootdn_bind( op, rs ) ) {
220 case SLAP_CB_CONTINUE:
227 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR, NULL, NULL );
232 /* we can do (almost) whatever we want with this conn,
233 * because either it's temporary, or it's marked as binding */
234 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
235 ch_free( lc->lc_bound_ndn.bv_val );
236 BER_BVZERO( &lc->lc_bound_ndn );
238 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
239 memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
240 ch_free( lc->lc_cred.bv_val );
241 BER_BVZERO( &lc->lc_cred );
243 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
245 /* don't add proxyAuthz; set the bindDN */
246 save_o_dn = op->o_dn;
247 save_o_do_not_cache = op->o_do_not_cache;
248 op->o_dn = op->o_req_dn;
249 op->o_do_not_cache = 1;
252 rc = ldap_back_controls_add( op, rs, lc, &ctrls );
253 op->o_dn = save_o_dn;
254 op->o_do_not_cache = save_o_do_not_cache;
255 if ( rc != LDAP_SUCCESS ) {
256 send_ldap_result( op, rs );
257 ldap_back_release_conn( li, lc );
262 /* method is always LDAP_AUTH_SIMPLE if we got here */
263 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
265 &op->orb_cred, ctrls, NULL, &msgid );
266 /* FIXME: should we always retry, or only when piping the bind
267 * in the "override" connection pool? */
268 rc = ldap_back_op_result( lc, op, rs, msgid,
269 li->li_timeout[ SLAP_OP_BIND ],
270 LDAP_BACK_BIND_SERR | retrying );
271 if ( rc == LDAP_UNAVAILABLE && retrying ) {
272 retrying &= ~LDAP_BACK_RETRYING;
273 if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_BIND_SERR ) ) {
278 ldap_pvt_thread_mutex_lock( &li->li_counter_mutex );
279 ldap_pvt_mp_add( li->li_ops_completed[ SLAP_OP_BIND ], 1 );
280 ldap_pvt_thread_mutex_unlock( &li->li_counter_mutex );
282 ldap_back_controls_free( op, rs, &ctrls );
284 if ( rc == LDAP_SUCCESS ) {
285 op->o_conn->c_authz_cookie = op->o_bd->be_private;
287 /* If defined, proxyAuthz will be used also when
288 * back-ldap is the authorizing backend; for this
289 * purpose, after a successful bind the connection
290 * is left for further binds, and further operations
291 * on this client connection will use a default
292 * connection with identity assertion */
293 /* NOTE: use with care */
294 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
295 ldap_back_release_conn( li, lc );
299 /* rebind is now done inside ldap_back_proxy_authz_bind()
300 * in case of success */
301 LDAP_BACK_CONN_ISBOUND_SET( lc );
302 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
304 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
305 memset( lc->lc_cred.bv_val, 0,
306 lc->lc_cred.bv_len );
309 if ( LDAP_BACK_SAVECRED( li ) ) {
310 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
311 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
314 lc->lc_cred.bv_len = 0;
318 /* must re-insert if local DN changed as result of bind */
319 if ( !LDAP_BACK_CONN_ISBOUND( lc )
320 || ( !dn_match( &op->o_req_ndn, &lc->lc_local_ndn )
321 && !LDAP_BACK_PCONN_ISPRIV( lc ) ) )
326 /* wait for all other ops to release the connection */
328 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
329 if ( lc->lc_refcnt > 1 ) {
330 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
331 ldap_pvt_thread_yield();
335 #if LDAP_BACK_PRINT_CONNTREE > 0
336 ldap_back_print_conntree( li, ">>> ldap_back_bind" );
337 #endif /* LDAP_BACK_PRINT_CONNTREE */
339 assert( lc->lc_refcnt == 1 );
340 ldap_back_conn_delete( li, lc );
342 /* delete all cached connections with the current connection */
343 if ( LDAP_BACK_SINGLECONN( li ) ) {
344 while ( ( tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc, ldap_back_conn_cmp ) ) != NULL )
346 assert( !LDAP_BACK_PCONN_ISPRIV( lc ) );
347 Debug( LDAP_DEBUG_TRACE,
348 "=>ldap_back_bind: destroying conn %lu (refcnt=%u)\n",
349 lc->lc_conn->c_connid, lc->lc_refcnt, 0 );
351 if ( tmplc->lc_refcnt != 0 ) {
353 LDAP_BACK_CONN_TAINTED_SET( tmplc );
354 LDAP_BACK_CONN_CACHED_CLEAR( tmplc );
358 * Needs a test because the handler may be corrupted,
359 * and calling ldap_unbind on a corrupted header results
360 * in a segmentation fault
362 ldap_back_conn_free( tmplc );
367 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
368 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
369 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
370 LDAP_BACK_PCONN_ROOTDN_SET( lc, op );
372 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
373 ldap_back_conndn_cmp, ldap_back_conndn_dup );
376 #if LDAP_BACK_PRINT_CONNTREE > 0
377 ldap_back_print_conntree( li, "<<< ldap_back_bind" );
378 #endif /* LDAP_BACK_PRINT_CONNTREE */
380 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
383 LDAP_BACK_CONN_CACHED_SET( lc );
387 /* duplicate; someone else successfully bound
388 * on the same connection with the same identity;
389 * we can do this because lc_refcnt == 1 */
390 ldap_back_conn_free( lc );
396 ldap_back_release_conn( li, lc );
403 * ldap_back_conndn_cmp
405 * compares two ldapconn_t based on the value of the conn pointer
406 * and of the local DN; used by avl stuff for insert, lookup
410 ldap_back_conndn_cmp( const void *c1, const void *c2 )
412 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
413 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
416 /* If local DNs don't match, it is definitely not a match */
417 /* For shared sessions, conn is NULL. Only explicitly
418 * bound sessions will have non-NULL conn.
420 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
422 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
429 * ldap_back_conndnlc_cmp
431 * compares two ldapconn_t based on the value of the conn pointer,
432 * the local DN and the lc pointer; used by avl stuff for insert, lookup
436 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
438 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
439 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
442 /* If local DNs don't match, it is definitely not a match */
443 /* For shared sessions, conn is NULL. Only explicitly
444 * bound sessions will have non-NULL conn.
446 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
448 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
450 rc = SLAP_PTRCMP( lc1, lc2 );
460 * compares two ldapconn_t based on the value of the conn pointer;
461 * used by avl stuff for delete of all conns with the same connid
464 ldap_back_conn_cmp( const void *c1, const void *c2 )
466 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
467 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
469 /* For shared sessions, conn is NULL. Only explicitly
470 * bound sessions will have non-NULL conn.
472 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
476 * ldap_back_conndn_dup
478 * returns -1 in case a duplicate ldapconn_t has been inserted;
482 ldap_back_conndn_dup( void *c1, void *c2 )
484 ldapconn_t *lc1 = (ldapconn_t *)c1;
485 ldapconn_t *lc2 = (ldapconn_t *)c2;
487 /* Cannot have more than one shared session with same DN */
488 if ( lc1->lc_conn == lc2->lc_conn &&
489 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
498 ldap_back_freeconn( ldapinfo_t *li, ldapconn_t *lc, int dolock )
501 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
504 #if LDAP_BACK_PRINT_CONNTREE > 0
505 ldap_back_print_conntree( li, ">>> ldap_back_freeconn" );
506 #endif /* LDAP_BACK_PRINT_CONNTREE */
508 (void)ldap_back_conn_delete( li, lc );
510 if ( lc->lc_refcnt == 0 ) {
511 ldap_back_conn_free( (void *)lc );
514 #if LDAP_BACK_PRINT_CONNTREE > 0
515 ldap_back_print_conntree( li, "<<< ldap_back_freeconn" );
516 #endif /* LDAP_BACK_PRINT_CONNTREE */
519 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
536 int rc = LDAP_SUCCESS;
538 /* start TLS ("tls-[try-]{start,propagate}" statements) */
539 if ( ( LDAP_BACK_USE_TLS_F( flags ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS_F( flags ) ) )
540 && !ldap_is_ldaps_url( url ) )
542 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
544 * use asynchronous StartTLS
545 * in case, chase referral (not implemented yet)
549 if ( protocol == 0 ) {
550 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
554 if ( protocol < LDAP_VERSION3 ) {
555 /* we should rather bail out... */
556 rc = LDAP_UNWILLING_TO_PERFORM;
557 *text = "invalid protocol version";
560 if ( rc == LDAP_SUCCESS ) {
561 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
564 if ( rc == LDAP_SUCCESS ) {
565 LDAPMessage *res = NULL;
568 LDAP_BACK_TV_SET( &tv );
571 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
573 rc = LDAP_UNAVAILABLE;
575 } else if ( rc == 0 ) {
576 if ( retries != LDAP_BACK_RETRY_NEVER ) {
577 ldap_pvt_thread_yield();
581 LDAP_BACK_TV_SET( &tv );
584 rc = LDAP_UNAVAILABLE;
586 } else if ( rc == LDAP_RES_EXTENDED ) {
587 struct berval *data = NULL;
589 rc = ldap_parse_extended_result( ld, res,
591 if ( rc == LDAP_SUCCESS ) {
593 rc = ldap_parse_result( ld, res, &rs.sr_err,
594 NULL, NULL, NULL, NULL, 1 );
595 if ( rc != LDAP_SUCCESS ) {
598 rc = slap_map_api2result( &rs );
601 /* FIXME: in case a referral
602 * is returned, should we try
603 * using it instead of the
605 if ( rc == LDAP_SUCCESS ) {
606 rc = ldap_install_tls( ld );
608 } else if ( rc == LDAP_REFERRAL ) {
609 rc = LDAP_UNWILLING_TO_PERFORM;
610 *text = "unwilling to chase referral returned by Start TLS exop";
614 if ( data->bv_val ) {
615 ber_memfree( data->bv_val );
629 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
631 * use synchronous StartTLS
633 rc = ldap_start_tls_s( ld, NULL, NULL );
634 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
636 /* if StartTLS is requested, only attempt it if the URL
637 * is not "ldaps://"; this may occur not only in case
638 * of misconfiguration, but also when used in the chain
639 * overlay, where the "uri" can be parsed out of a referral */
645 case LDAP_SERVER_DOWN:
649 if ( LDAP_BACK_TLS_CRITICAL_F( flags ) ) {
650 *text = "could not start TLS";
654 /* in case Start TLS is not critical */
666 #endif /* HAVE_TLS */
669 ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
671 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
675 int is_tls = op->o_conn->c_is_tls;
676 int flags = li->li_flags;
677 time_t lctime = (time_t)(-1);
679 #endif /* HAVE_TLS */
681 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
682 rs->sr_err = ldap_initialize( &ld, li->li_uri );
683 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
684 if ( rs->sr_err != LDAP_SUCCESS ) {
688 if ( li->li_urllist_f ) {
689 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
692 /* Set LDAP version. This will always succeed: If the client
693 * bound with a particular version, then so can we.
695 if ( li->li_version != 0 ) {
696 version = li->li_version;
698 } else if ( op->o_protocol != 0 ) {
699 version = op->o_protocol;
702 /* assume it's an internal op; set to LDAPv3 */
703 version = LDAP_VERSION3;
705 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
707 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
708 ldap_set_option( ld, LDAP_OPT_REFERRALS,
709 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
711 if ( li->li_network_timeout > 0 ) {
714 tv.tv_sec = li->li_network_timeout;
716 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
719 /* turn on network keepalive, if configured so */
720 slap_client_keepalive(ld, &li->li_tls.sb_keepalive);
723 if ( LDAP_BACK_CONN_ISPRIV( lc ) ) {
724 /* See "rationale" comment in ldap_back_getconn() */
725 if ( li->li_acl_authmethod == LDAP_AUTH_NONE &&
726 li->li_idassert_authmethod != LDAP_AUTH_NONE )
727 sb = &li->li_idassert.si_bc;
731 } else if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
732 sb = &li->li_idassert.si_bc;
738 if ( sb->sb_tls_do_init ) {
739 bindconf_tls_set( sb, ld );
740 } else if ( sb->sb_tls_ctx ) {
741 ldap_set_option( ld, LDAP_OPT_X_TLS_CTX, sb->sb_tls_ctx );
744 /* if required by the bindconf configuration, force TLS */
745 if ( ( sb == &li->li_acl || sb == &li->li_idassert.si_bc ) &&
748 flags |= LDAP_BACK_F_USE_TLS;
751 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
752 assert( li->li_uri_mutex_do_not_lock == 0 );
753 li->li_uri_mutex_do_not_lock = 1;
754 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
755 li->li_uri, flags, li->li_nretries, &rs->sr_text );
756 li->li_uri_mutex_do_not_lock = 0;
757 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
758 if ( rs->sr_err != LDAP_SUCCESS ) {
759 ldap_unbind_ext( ld, NULL, NULL );
760 rs->sr_text = "Start TLS failed";
763 } else if ( li->li_idle_timeout ) {
764 /* only touch when activity actually took place... */
767 #endif /* HAVE_TLS */
773 LDAP_BACK_CONN_ISTLS_SET( lc );
775 LDAP_BACK_CONN_ISTLS_CLEAR( lc );
777 if ( lctime != (time_t)(-1) ) {
778 lc->lc_time = lctime;
780 #endif /* HAVE_TLS */
783 if ( rs->sr_err != LDAP_SUCCESS ) {
784 rs->sr_err = slap_map_api2result( rs );
785 if ( sendok & LDAP_BACK_SENDERR ) {
786 if ( rs->sr_text == NULL ) {
787 rs->sr_text = "Proxy connection initialization failed";
789 send_ldap_result( op, rs );
793 if ( li->li_conn_ttl > 0 ) {
794 lc->lc_create_time = op->o_time;
805 ldap_back_send_t sendok,
806 struct berval *binddn,
807 struct berval *bindcred )
809 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
810 ldapconn_t *lc = NULL,
813 lookupconn = !( sendok & LDAP_BACK_BINDING );
815 /* if the server is quarantined, and
816 * - the current interval did not expire yet, or
817 * - no more retries should occur,
818 * don't return the connection */
819 if ( li->li_isquarantined ) {
820 slap_retry_info_t *ri = &li->li_quarantine;
823 if ( li->li_quarantine.ri_interval ) {
824 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
825 if ( li->li_isquarantined == LDAP_BACK_FQ_YES ) {
826 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
827 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
829 Debug( LDAP_DEBUG_ANY,
830 "%s: ldap_back_getconn quarantine "
831 "retry block #%d try #%d.\n",
832 op->o_log_prefix, ri->ri_idx, ri->ri_count );
833 li->li_isquarantined = LDAP_BACK_FQ_RETRYING;
836 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
840 rs->sr_err = LDAP_UNAVAILABLE;
841 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
842 rs->sr_text = "Target is quarantined";
843 send_ldap_result( op, rs );
849 /* Internal searches are privileged and shared. So is root. */
850 if ( op->o_do_not_cache || be_isroot( op ) ) {
851 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
852 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
853 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
856 struct berval tmpbinddn,
863 if ( binddn == NULL ) {
866 if ( bindcred == NULL ) {
867 bindcred = &tmpbindcred;
869 if ( op->o_tag == LDAP_REQ_BIND ) {
870 save_o_dn = op->o_dn;
871 save_o_ndn = op->o_ndn;
872 op->o_dn = op->o_req_dn;
873 op->o_ndn = op->o_req_ndn;
875 isproxyauthz = ldap_back_is_proxy_authz( op, rs, sendok, binddn, bindcred );
876 if ( op->o_tag == LDAP_REQ_BIND ) {
877 op->o_dn = save_o_dn;
878 op->o_ndn = save_o_ndn;
880 if ( isproxyauthz == -1 ) {
884 lc_curr.lc_local_ndn = op->o_ndn;
885 /* Explicit binds must not be shared;
886 * however, explicit binds are piped in a special connection
887 * when idassert is to occur with "override" set */
888 if ( op->o_tag == LDAP_REQ_BIND && !isproxyauthz ) {
889 lc_curr.lc_conn = op->o_conn;
892 if ( isproxyauthz && !( sendok & LDAP_BACK_BINDING ) ) {
893 lc_curr.lc_local_ndn = *binddn;
894 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
895 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
897 } else if ( isproxyauthz && ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) {
898 lc_curr.lc_local_ndn = slap_empty_bv;
899 LDAP_BACK_PCONN_BIND_SET( &lc_curr, op );
900 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
903 } else if ( SLAP_IS_AUTHZ_BACKEND( op ) ) {
904 lc_curr.lc_conn = op->o_conn;
907 LDAP_BACK_PCONN_ANON_SET( &lc_curr, op );
912 /* Explicit Bind requests always get their own conn */
915 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
916 if ( LDAP_BACK_PCONN_ISPRIV( &lc_curr ) ) {
917 /* lookup a conn that's not binding */
918 LDAP_TAILQ_FOREACH( lc,
919 &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv,
922 if ( !LDAP_BACK_CONN_BINDING( lc ) && lc->lc_refcnt == 0 ) {
928 if ( lc != LDAP_TAILQ_LAST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
931 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
933 LDAP_TAILQ_ENTRY_INIT( lc, lc_q );
934 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
938 } else if ( !LDAP_BACK_USE_TEMPORARIES( li )
939 && li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_num == li->li_conn_priv_max )
941 lc = LDAP_TAILQ_FIRST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv );
946 /* Searches for a ldapconn in the avl tree */
947 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
948 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
952 /* Don't reuse connections while they're still binding */
953 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
954 if ( !LDAP_BACK_USE_TEMPORARIES( li ) ) {
955 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
957 ldap_pvt_thread_yield();
964 if ( op->o_tag == LDAP_REQ_BIND ) {
965 /* right now, this is the only possible case */
966 assert( ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) );
967 LDAP_BACK_CONN_BINDING_SET( lc );
970 refcnt = ++lc->lc_refcnt;
973 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
976 /* Looks like we didn't get a bind. Open a new session... */
978 lc = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
979 lc->lc_flags = li->li_flags;
980 lc->lc_lcflags = lc_curr.lc_lcflags;
981 if ( ldap_back_prepare_conn( lc, op, rs, sendok ) != LDAP_SUCCESS ) {
986 if ( sendok & LDAP_BACK_BINDING ) {
987 LDAP_BACK_CONN_BINDING_SET( lc );
990 lc->lc_conn = lc_curr.lc_conn;
991 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
994 * the rationale is: connections as the rootdn are privileged,
995 * so li_acl is to be used; however, in some cases
996 * one already configured identity assertion with a highly
997 * privileged idassert_authcDN, so if li_acl is not configured
998 * and idassert is, use idassert instead.
1000 * might change in the future, because it's preferable
1001 * to make clear what identity is being used, since
1002 * the only drawback is that one risks to configure
1003 * the same identity twice...
1005 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
1006 if ( li->li_acl_authmethod == LDAP_AUTH_NONE &&
1007 li->li_idassert_authmethod != LDAP_AUTH_NONE ) {
1008 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
1009 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
1012 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
1013 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
1015 LDAP_BACK_CONN_ISPRIV_SET( lc );
1017 } else if ( LDAP_BACK_CONN_ISIDASSERT( &lc_curr ) ) {
1018 if ( !LDAP_BACK_PCONN_ISBIND( &lc_curr ) ) {
1019 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
1020 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
1022 LDAP_BACK_CONN_ISIDASSERT_SET( lc );
1025 BER_BVZERO( &lc->lc_cred );
1026 BER_BVZERO( &lc->lc_bound_ndn );
1027 if ( !BER_BVISEMPTY( &op->o_ndn )
1028 && SLAP_IS_AUTHZ_BACKEND( op ) )
1030 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
1035 /* if start TLS failed but it was not mandatory,
1036 * check if the non-TLS connection was already
1037 * in cache; in case, destroy the newly created
1038 * connection and use the existing one */
1039 if ( LDAP_BACK_PCONN_ISTLS( lc )
1040 && !ldap_tls_inplace( lc->lc_ld ) )
1042 ldapconn_t *tmplc = NULL;
1043 int idx = LDAP_BACK_CONN2PRIV( &lc_curr ) - 1;
1045 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1046 LDAP_TAILQ_FOREACH( tmplc,
1047 &li->li_conn_priv[ idx ].lic_priv,
1050 if ( !LDAP_BACK_CONN_BINDING( tmplc ) ) {
1055 if ( tmplc != NULL ) {
1056 refcnt = ++tmplc->lc_refcnt;
1057 ldap_back_conn_free( lc );
1060 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1062 if ( tmplc != NULL ) {
1066 #endif /* HAVE_TLS */
1068 /* Inserts the newly created ldapconn in the avl tree */
1069 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1071 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1072 lc->lc_connid = li->li_conn_nextid++;
1074 assert( lc->lc_refcnt == 1 );
1076 #if LDAP_BACK_PRINT_CONNTREE > 0
1077 ldap_back_print_conntree( li, ">>> ldap_back_getconn(insert)" );
1078 #endif /* LDAP_BACK_PRINT_CONNTREE */
1080 if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1081 if ( li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num < li->li_conn_priv_max ) {
1082 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv, lc, lc_q );
1083 li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num++;
1084 LDAP_BACK_CONN_CACHED_SET( lc );
1087 LDAP_BACK_CONN_TAINTED_SET( lc );
1092 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
1093 ldap_back_conndn_cmp, ldap_back_conndn_dup );
1094 LDAP_BACK_CONN_CACHED_SET( lc );
1097 #if LDAP_BACK_PRINT_CONNTREE > 0
1098 ldap_back_print_conntree( li, "<<< ldap_back_getconn(insert)" );
1099 #endif /* LDAP_BACK_PRINT_CONNTREE */
1101 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1103 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1104 char buf[ SLAP_TEXT_BUFLEN ];
1106 snprintf( buf, sizeof( buf ),
1107 "lc=%p inserted refcnt=%u rc=%d",
1108 (void *)lc, refcnt, rs->sr_err );
1110 Debug( LDAP_DEBUG_TRACE,
1111 "=>ldap_back_getconn: %s: %s\n",
1112 op->o_log_prefix, buf, 0 );
1115 if ( !LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1116 /* Err could be -1 in case a duplicate ldapconn is inserted */
1117 switch ( rs->sr_err ) {
1122 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1123 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( li ) ) {
1124 /* duplicate: free and try to get the newly created one */
1125 ldap_back_conn_free( lc );
1130 /* taint connection, so that it'll be freed when released */
1131 LDAP_BACK_CONN_TAINTED_SET( lc );
1135 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1136 ldap_back_conn_free( lc );
1137 rs->sr_err = LDAP_OTHER;
1138 rs->sr_text = "Proxy bind collision";
1139 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1140 send_ldap_result( op, rs );
1149 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
1150 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
1154 /* let it be used, but taint/delete it so that
1155 * no-one else can look it up any further */
1156 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1158 #if LDAP_BACK_PRINT_CONNTREE > 0
1159 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1160 #endif /* LDAP_BACK_PRINT_CONNTREE */
1162 (void)ldap_back_conn_delete( li, lc );
1163 LDAP_BACK_CONN_TAINTED_SET( lc );
1165 #if LDAP_BACK_PRINT_CONNTREE > 0
1166 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1167 #endif /* LDAP_BACK_PRINT_CONNTREE */
1169 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1172 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1173 char buf[ SLAP_TEXT_BUFLEN ];
1175 snprintf( buf, sizeof( buf ),
1176 "conn %p fetched refcnt=%u%s",
1178 expiring ? " expiring" : "" );
1179 Debug( LDAP_DEBUG_TRACE,
1180 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
1186 #endif /* HAVE_TLS */
1192 ldap_back_release_conn_lock(
1198 ldapconn_t *lc = *lcp;
1201 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1203 assert( lc->lc_refcnt > 0 );
1204 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1206 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
1207 ldap_back_freeconn( li, lc, 0 );
1211 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1216 ldap_back_quarantine(
1220 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1222 slap_retry_info_t *ri = &li->li_quarantine;
1224 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
1226 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1227 time_t new_last = slap_get_time();
1229 switch ( li->li_isquarantined ) {
1230 case LDAP_BACK_FQ_NO:
1231 if ( ri->ri_last == new_last ) {
1235 Debug( LDAP_DEBUG_ANY,
1236 "%s: ldap_back_quarantine enter.\n",
1237 op->o_log_prefix, 0, 0 );
1243 case LDAP_BACK_FQ_RETRYING:
1244 Debug( LDAP_DEBUG_ANY,
1245 "%s: ldap_back_quarantine block #%d try #%d failed.\n",
1246 op->o_log_prefix, ri->ri_idx, ri->ri_count );
1249 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1250 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1261 li->li_isquarantined = LDAP_BACK_FQ_YES;
1262 ri->ri_last = new_last;
1264 } else if ( li->li_isquarantined != LDAP_BACK_FQ_NO ) {
1265 if ( ri->ri_last == slap_get_time() ) {
1269 Debug( LDAP_DEBUG_ANY,
1270 "%s: ldap_back_quarantine exit (%d) err=%d.\n",
1271 op->o_log_prefix, li->li_isquarantined, rs->sr_err );
1273 if ( li->li_quarantine_f ) {
1274 (void)li->li_quarantine_f( li, li->li_quarantine_p );
1279 li->li_isquarantined = LDAP_BACK_FQ_NO;
1283 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
1287 ldap_back_dobind_cb(
1292 ber_tag_t *tptr = op->o_callback->sc_private;
1294 rs->sr_tag = slap_req2res( op->o_tag );
1296 return SLAP_CB_CONTINUE;
1300 * ldap_back_dobind_int
1302 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1305 ldap_back_dobind_int(
1309 ldap_back_send_t sendok,
1313 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1316 struct berval binddn = slap_empty_bv,
1317 bindcred = slap_empty_bv;
1323 ber_tag_t o_tag = op->o_tag;
1324 slap_callback cb = {0};
1327 assert( lcp != NULL );
1328 assert( retries >= 0 );
1330 if ( sendok & LDAP_BACK_GETCONN ) {
1331 assert( *lcp == NULL );
1333 lc = ldap_back_getconn( op, rs, sendok, &binddn, &bindcred );
1343 assert( lc != NULL );
1347 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1350 if ( binding == 0 ) {
1351 /* check if already bound */
1352 rc = isbound = LDAP_BACK_CONN_ISBOUND( lc );
1355 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1360 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
1361 /* if someone else is about to bind it, give up and retry */
1363 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1365 ldap_pvt_thread_yield();
1369 /* otherwise this thread will bind it */
1370 LDAP_BACK_CONN_BINDING_SET( lc );
1376 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1380 * FIXME: we need to let clients use proxyAuthz
1381 * otherwise we cannot do symmetric pools of servers;
1382 * we have to live with the fact that a user can
1383 * authorize itself as any ID that is allowed
1384 * by the authzTo directive of the "proxyauthzdn".
1387 * NOTE: current Proxy Authorization specification
1388 * and implementation do not allow proxy authorization
1389 * control to be provided with Bind requests
1392 * if no bind took place yet, but the connection is bound
1393 * and the "idassert-authcDN" (or other ID) is set,
1394 * then bind as the asserting identity and explicitly
1395 * add the proxyAuthz control to every operation with the
1396 * dn bound to the connection as control value.
1397 * This is done also if this is the authorizing backend,
1398 * but the "override" flag is given to idassert.
1399 * It allows to use SASL bind and yet proxyAuthz users
1401 op->o_tag = LDAP_REQ_BIND;
1402 cb.sc_next = op->o_callback;
1403 cb.sc_private = &o_tag;
1404 cb.sc_response = ldap_back_dobind_cb;
1405 op->o_callback = &cb;
1407 if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
1408 if ( BER_BVISEMPTY( &binddn ) && BER_BVISEMPTY( &bindcred ) ) {
1409 /* if we got here, it shouldn't return result */
1410 rc = ldap_back_is_proxy_authz( op, rs,
1411 LDAP_BACK_DONTSEND, &binddn, &bindcred );
1413 Debug( LDAP_DEBUG_ANY, "Error: ldap_back_is_proxy_authz "
1414 "returned %d, misconfigured URI?\n", rc, 0, 0 );
1415 rs->sr_err = LDAP_OTHER;
1416 rs->sr_text = "misconfigured URI?";
1417 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1418 if ( sendok & LDAP_BACK_SENDERR ) {
1419 send_ldap_result( op, rs );
1424 rc = ldap_back_proxy_authz_bind( lc, op, rs, sendok, &binddn, &bindcred );
1428 #ifdef HAVE_CYRUS_SASL
1429 if ( LDAP_BACK_CONN_ISPRIV( lc )) {
1431 if ( li->li_acl_authmethod != LDAP_AUTH_NONE )
1434 sb = &li->li_idassert.si_bc;
1436 if ( sb->sb_method == LDAP_AUTH_SASL ) {
1437 void *defaults = NULL;
1439 if ( sb->sb_secprops != NULL ) {
1440 rc = ldap_set_option( lc->lc_ld,
1441 LDAP_OPT_X_SASL_SECPROPS, sb->sb_secprops );
1443 if ( rc != LDAP_OPT_SUCCESS ) {
1444 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
1445 "(SECPROPS,\"%s\") failed!\n",
1446 sb->sb_secprops, 0, 0 );
1451 defaults = lutil_sasl_defaults( lc->lc_ld,
1452 sb->sb_saslmech.bv_val,
1453 sb->sb_realm.bv_val,
1454 sb->sb_authcId.bv_val,
1457 if ( defaults == NULL ) {
1458 rs->sr_err = LDAP_OTHER;
1459 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1460 if ( sendok & LDAP_BACK_SENDERR ) {
1461 send_ldap_result( op, rs );
1466 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
1467 sb->sb_binddn.bv_val,
1468 sb->sb_saslmech.bv_val, NULL, NULL,
1469 LDAP_SASL_QUIET, lutil_sasl_interact,
1472 ldap_pvt_thread_mutex_lock( &li->li_counter_mutex );
1473 ldap_pvt_mp_add( li->li_ops_completed[ SLAP_OP_BIND ], 1 );
1474 ldap_pvt_thread_mutex_unlock( &li->li_counter_mutex );
1476 lutil_sasl_freedefs( defaults );
1478 switch ( rs->sr_err ) {
1480 LDAP_BACK_CONN_ISBOUND_SET( lc );
1483 case LDAP_LOCAL_ERROR:
1484 /* list client API error codes that require
1485 * to taint the connection */
1486 /* FIXME: should actually retry? */
1487 LDAP_BACK_CONN_TAINTED_SET( lc );
1492 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1493 rs->sr_err = slap_map_api2result( rs );
1494 if ( sendok & LDAP_BACK_SENDERR ) {
1495 send_ldap_result( op, rs );
1500 if ( LDAP_BACK_QUARANTINE( li ) ) {
1501 ldap_back_quarantine( op, rs );
1507 #endif /* HAVE_CYRUS_SASL */
1510 if ( BER_BVISNULL( &lc->lc_cred ) ) {
1512 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) && !BER_BVISEMPTY( &lc->lc_bound_ndn ) ) {
1513 Debug( LDAP_DEBUG_ANY, "%s ldap_back_dobind_int: DN=\"%s\" without creds, binding anonymously",
1514 op->o_log_prefix, lc->lc_bound_ndn.bv_val, 0 );
1518 tmp_dn = lc->lc_bound_ndn.bv_val;
1520 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1522 LDAP_SASL_SIMPLE, &lc->lc_cred,
1523 NULL, NULL, &msgid );
1525 ldap_pvt_thread_mutex_lock( &li->li_counter_mutex );
1526 ldap_pvt_mp_add( li->li_ops_completed[ SLAP_OP_BIND ], 1 );
1527 ldap_pvt_thread_mutex_unlock( &li->li_counter_mutex );
1529 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
1530 if ( retries != LDAP_BACK_RETRY_NEVER ) {
1532 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1535 assert( lc->lc_refcnt > 0 );
1536 if ( lc->lc_refcnt == 1 ) {
1537 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1540 /* lc here must be the regular lc, reset and ready for init */
1541 rs->sr_err = ldap_back_prepare_conn( lc, op, rs, sendok );
1542 if ( rs->sr_err != LDAP_SUCCESS ) {
1543 sendok &= ~LDAP_BACK_SENDERR;
1549 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1552 if ( rs->sr_err == LDAP_SUCCESS ) {
1553 if ( retries > 0 ) {
1560 assert( lc->lc_refcnt == 1 );
1562 ldap_back_freeconn( li, lc, dolock );
1564 rs->sr_err = slap_map_api2result( rs );
1566 if ( LDAP_BACK_QUARANTINE( li ) ) {
1567 ldap_back_quarantine( op, rs );
1570 if ( rs->sr_err != LDAP_SUCCESS &&
1571 ( sendok & LDAP_BACK_SENDERR ) )
1573 if ( op->o_callback == &cb )
1574 op->o_callback = cb.sc_next;
1576 rs->sr_text = "Proxy can't contact remote server";
1577 send_ldap_result( op, rs );
1584 rc = ldap_back_op_result( lc, op, rs, msgid,
1585 -1, ( sendok | LDAP_BACK_BINDING ) );
1586 if ( rc == LDAP_SUCCESS ) {
1587 LDAP_BACK_CONN_ISBOUND_SET( lc );
1591 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1592 rc = LDAP_BACK_CONN_ISBOUND( lc );
1594 ldap_back_release_conn_lock( li, lcp, dolock );
1596 } else if ( LDAP_BACK_SAVECRED( li ) ) {
1597 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1601 if ( op->o_callback == &cb )
1602 op->o_callback = cb.sc_next;
1611 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1614 ldap_back_dobind( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1616 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1618 return ldap_back_dobind_int( lcp, op, rs,
1619 ( sendok | LDAP_BACK_GETCONN ), li->li_nretries, 1 );
1623 * ldap_back_default_rebind
1625 * This is a callback used for chasing referrals using the same
1626 * credentials as the original user on this session.
1629 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1630 ber_int_t msgid, void *params )
1632 ldapconn_t *lc = (ldapconn_t *)params;
1635 /* ... otherwise we couldn't get here */
1636 assert( lc != NULL );
1638 if ( !ldap_tls_inplace( ld ) ) {
1639 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1641 const char *text = NULL;
1643 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1644 LDAP_BACK_RETRY_DEFAULT, &text );
1645 if ( rc != LDAP_SUCCESS ) {
1649 #endif /* HAVE_TLS */
1651 /* FIXME: add checks on the URL/identity? */
1652 /* TODO: would like to count this bind operation for monitoring
1653 * too, but where do we get the ldapinfo_t? */
1655 return ldap_sasl_bind_s( ld,
1656 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1657 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1661 * ldap_back_default_urllist
1664 ldap_back_default_urllist(
1666 LDAPURLDesc **urllist,
1670 ldapinfo_t *li = (ldapinfo_t *)params;
1671 LDAPURLDesc **urltail;
1673 if ( urllist == url ) {
1674 return LDAP_SUCCESS;
1677 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1680 *urltail = *urllist;
1684 if ( !li->li_uri_mutex_do_not_lock ) {
1685 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1689 ch_free( li->li_uri );
1692 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1694 if ( !li->li_uri_mutex_do_not_lock ) {
1695 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1698 return LDAP_SUCCESS;
1707 ldap_back_send_t sendok )
1709 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1711 /* default behavior */
1712 if ( LDAP_BACK_ABANDON( li ) ) {
1713 return ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1716 if ( LDAP_BACK_IGNORE( li ) ) {
1717 return ldap_pvt_discard( lc->lc_ld, msgid );
1720 if ( LDAP_BACK_CANCEL( li ) ) {
1721 /* FIXME: asynchronous? */
1722 return ldap_cancel_s( lc->lc_ld, msgid, NULL, NULL );
1731 ldap_back_op_result(
1737 ldap_back_send_t sendok )
1739 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1744 LDAPControl **ctrls = NULL;
1747 rs->sr_matched = NULL;
1749 rs->sr_ctrls = NULL;
1751 /* if the error recorded in the reply corresponds
1752 * to a successful state, get the error from the
1753 * remote server response */
1754 if ( LDAP_ERR_OK( rs->sr_err ) ) {
1757 LDAPMessage *res = NULL;
1758 time_t stoptime = (time_t)(-1);
1759 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
1760 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1761 const char *timeout_text = "Operation timed out";
1763 /* if timeout is not specified, compute and use
1764 * the one specific to the ongoing operation */
1765 if ( timeout == (time_t)(-1) ) {
1766 slap_op_t opidx = slap_req2op( op->o_tag );
1768 if ( opidx == SLAP_OP_SEARCH ) {
1769 if ( op->ors_tlimit <= 0 ) {
1773 timeout = op->ors_tlimit;
1774 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
1775 timeout_text = NULL;
1779 timeout = li->li_timeout[ opidx ];
1783 /* better than nothing :) */
1784 if ( timeout == 0 ) {
1785 if ( li->li_idle_timeout ) {
1786 timeout = li->li_idle_timeout;
1788 } else if ( li->li_conn_ttl ) {
1789 timeout = li->li_conn_ttl;
1794 stoptime = op->o_time + timeout;
1797 LDAP_BACK_TV_SET( &tv );
1800 /* if result parsing fails, note the failure reason */
1801 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1804 if ( timeout && slap_get_time() > stoptime ) {
1805 if ( sendok & LDAP_BACK_BINDING ) {
1806 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1809 /* let it be used, but taint/delete it so that
1810 * no-one else can look it up any further */
1811 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1813 #if LDAP_BACK_PRINT_CONNTREE > 0
1814 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1815 #endif /* LDAP_BACK_PRINT_CONNTREE */
1817 (void)ldap_back_conn_delete( li, lc );
1818 LDAP_BACK_CONN_TAINTED_SET( lc );
1820 #if LDAP_BACK_PRINT_CONNTREE > 0
1821 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1822 #endif /* LDAP_BACK_PRINT_CONNTREE */
1823 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1826 (void)ldap_back_cancel( lc, op, rs, msgid, sendok );
1828 rs->sr_err = timeout_err;
1829 rs->sr_text = timeout_text;
1834 LDAP_BACK_TV_SET( &tv );
1835 ldap_pvt_thread_yield();
1839 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1844 /* otherwise get the result; if it is not
1845 * LDAP_SUCCESS, record it in the reply
1846 * structure (this includes
1847 * LDAP_COMPARE_{TRUE|FALSE}) */
1849 /* only touch when activity actually took place... */
1850 if ( li->li_idle_timeout && lc ) {
1851 lc->lc_time = op->o_time;
1854 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1855 &match, &text, &refs, &ctrls, 1 );
1856 if ( rc == LDAP_SUCCESS ) {
1861 rs->sr_err = slap_map_api2result( rs );
1863 /* RFC 4511: referrals can only appear
1864 * if result code is LDAP_REFERRAL */
1866 && refs[ 0 ] != NULL
1867 && refs[ 0 ][ 0 ] != '\0' )
1869 if ( rs->sr_err != LDAP_REFERRAL ) {
1870 Debug( LDAP_DEBUG_ANY,
1871 "%s ldap_back_op_result: "
1872 "got referrals with err=%d\n",
1879 for ( i = 0; refs[ i ] != NULL; i++ )
1881 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1883 for ( i = 0; refs[ i ] != NULL; i++ ) {
1884 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1886 BER_BVZERO( &rs->sr_ref[ i ] );
1889 } else if ( rs->sr_err == LDAP_REFERRAL ) {
1890 Debug( LDAP_DEBUG_ANY,
1891 "%s ldap_back_op_result: "
1892 "got err=%d with null "
1893 "or empty referrals\n",
1897 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1900 if ( ctrls != NULL ) {
1901 rs->sr_ctrls = ctrls;
1906 /* if the error in the reply structure is not
1907 * LDAP_SUCCESS, try to map it from client
1908 * to server error */
1909 if ( !LDAP_ERR_OK( rs->sr_err ) ) {
1910 rs->sr_err = slap_map_api2result( rs );
1912 /* internal ops ( op->o_conn == NULL )
1913 * must not reply to client */
1914 if ( op->o_conn && !op->o_do_not_cache && match ) {
1916 /* record the (massaged) matched
1917 * DN into the reply structure */
1918 rs->sr_matched = match;
1922 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1923 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1924 if ( LDAP_BACK_QUARANTINE( li ) ) {
1925 ldap_back_quarantine( op, rs );
1927 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1928 if ( rs->sr_text == NULL ) rs->sr_text = "Proxy operation retry failed";
1929 send_ldap_result( op, rs );
1933 } else if ( op->o_conn &&
1934 ( ( ( sendok & LDAP_BACK_SENDOK ) && LDAP_ERR_OK( rs->sr_err ) )
1935 || ( ( sendok & LDAP_BACK_SENDERR ) && !LDAP_ERR_OK( rs->sr_err ) ) ) )
1937 send_ldap_result( op, rs );
1941 ldap_memfree( text );
1945 /* there can't be refs with a (successful) bind */
1947 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1952 ber_memvfree( (void **)refs );
1955 /* match should not be possible with a successful bind */
1957 if ( rs->sr_matched != match ) {
1958 free( (char *)rs->sr_matched );
1960 rs->sr_matched = NULL;
1961 ldap_memfree( match );
1964 if ( ctrls != NULL ) {
1965 if ( op->o_tag == LDAP_REQ_BIND && rs->sr_err == LDAP_SUCCESS ) {
1968 for ( i = 0; ctrls[i] != NULL; i++ );
1970 rs->sr_ctrls = op->o_tmpalloc( sizeof( LDAPControl * )*( i + 1 ),
1972 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
1974 ber_len_t oidlen = strlen( ctrls[i]->ldctl_oid );
1975 ber_len_t size = sizeof( LDAPControl )
1977 + ctrls[i]->ldctl_value.bv_len + 1;
1979 rs->sr_ctrls[ i ] = op->o_tmpalloc( size, op->o_tmpmemctx );
1980 rs->sr_ctrls[ i ]->ldctl_oid = (char *)&rs->sr_ctrls[ i ][ 1 ];
1981 lutil_strcopy( rs->sr_ctrls[ i ]->ldctl_oid, ctrls[i]->ldctl_oid );
1982 rs->sr_ctrls[ i ]->ldctl_value.bv_val
1983 = (char *)&rs->sr_ctrls[ i ]->ldctl_oid[oidlen + 1];
1984 rs->sr_ctrls[ i ]->ldctl_value.bv_len
1985 = ctrls[i]->ldctl_value.bv_len;
1986 ptr = lutil_memcopy( rs->sr_ctrls[ i ]->ldctl_value.bv_val,
1987 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len );
1990 rs->sr_ctrls[ i ] = NULL;
1991 rs->sr_flags |= REP_CTRLS_MUSTBEFREED;
1994 assert( rs->sr_ctrls != NULL );
1995 rs->sr_ctrls = NULL;
1998 ldap_controls_free( ctrls );
2001 return( LDAP_ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
2004 /* return true if bound, false if failed */
2006 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
2008 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2011 assert( lcp != NULL );
2012 assert( *lcp != NULL );
2014 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
2016 if ( (*lcp)->lc_refcnt == 1 ) {
2017 int binding = LDAP_BACK_CONN_BINDING( *lcp );
2019 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
2020 Debug( LDAP_DEBUG_ANY,
2021 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
2022 op->o_log_prefix, li->li_uri,
2023 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
2024 "" : (*lcp)->lc_bound_ndn.bv_val );
2025 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
2027 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
2028 (*lcp)->lc_ld = NULL;
2029 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
2031 /* lc here must be the regular lc, reset and ready for init */
2032 rc = ldap_back_prepare_conn( *lcp, op, rs, sendok );
2033 if ( rc != LDAP_SUCCESS ) {
2034 /* freeit, because lc_refcnt == 1 */
2035 (*lcp)->lc_refcnt = 0;
2036 (void)ldap_back_freeconn( li, *lcp, 0 );
2040 } else if ( ( sendok & LDAP_BACK_BINDING ) ) {
2042 LDAP_BACK_CONN_BINDING_SET( *lcp );
2047 rc = ldap_back_dobind_int( lcp, op, rs, sendok, 0, 0 );
2048 if ( rc == 0 && *lcp != NULL ) {
2049 /* freeit, because lc_refcnt == 1 */
2050 (*lcp)->lc_refcnt = 0;
2051 LDAP_BACK_CONN_TAINTED_SET( *lcp );
2052 (void)ldap_back_freeconn( li, *lcp, 0 );
2058 Debug( LDAP_DEBUG_TRACE,
2059 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
2060 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
2062 LDAP_BACK_CONN_TAINTED_SET( *lcp );
2063 ldap_back_release_conn_lock( li, lcp, 0 );
2064 assert( *lcp == NULL );
2066 if ( sendok & LDAP_BACK_SENDERR ) {
2067 rs->sr_err = LDAP_UNAVAILABLE;
2068 rs->sr_text = "Unable to retry";
2069 send_ldap_result( op, rs );
2073 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
2079 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
2080 struct berval *binddn, struct berval *bindcred )
2082 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2086 if ( op->o_conn == NULL || op->o_do_not_cache ) {
2090 /* don't proxyAuthz if protocol is not LDAPv3 */
2091 switch ( li->li_version ) {
2096 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2102 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2103 if ( sendok & LDAP_BACK_SENDERR ) {
2104 send_ldap_result( op, rs );
2111 *binddn = slap_empty_bv;
2112 *bindcred = slap_empty_bv;
2114 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2115 ndn = op->o_conn->c_ndn;
2121 if ( !( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE )) {
2122 if ( op->o_tag == LDAP_REQ_BIND ) {
2123 if ( !BER_BVISEMPTY( &ndn )) {
2127 } else if ( SLAP_IS_AUTHZ_BACKEND( op )) {
2133 switch ( li->li_idassert_mode ) {
2134 case LDAP_BACK_IDASSERT_LEGACY:
2135 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
2136 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
2138 *binddn = li->li_idassert_authcDN;
2139 *bindcred = li->li_idassert_passwd;
2146 /* NOTE: rootdn can always idassert */
2147 if ( BER_BVISNULL( &ndn )
2148 && li->li_idassert_authz == NULL
2149 && !( li->li_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL ) )
2151 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2152 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
2153 if ( sendok & LDAP_BACK_SENDERR ) {
2154 send_ldap_result( op, rs );
2159 rs->sr_err = LDAP_SUCCESS;
2160 *binddn = slap_empty_bv;
2161 *bindcred = slap_empty_bv;
2167 } else if ( !be_isroot( op ) ) {
2168 if ( li->li_idassert_passthru ) {
2169 struct berval authcDN;
2171 if ( BER_BVISNULL( &ndn ) ) {
2172 authcDN = slap_empty_bv;
2177 rs->sr_err = slap_sasl_matches( op, li->li_idassert_passthru,
2178 &authcDN, &authcDN );
2179 if ( rs->sr_err == LDAP_SUCCESS ) {
2185 if ( li->li_idassert_authz ) {
2186 struct berval authcDN;
2188 if ( BER_BVISNULL( &ndn ) ) {
2189 authcDN = slap_empty_bv;
2194 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
2195 &authcDN, &authcDN );
2196 if ( rs->sr_err != LDAP_SUCCESS ) {
2197 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2198 if ( sendok & LDAP_BACK_SENDERR ) {
2199 send_ldap_result( op, rs );
2204 rs->sr_err = LDAP_SUCCESS;
2205 *binddn = slap_empty_bv;
2206 *bindcred = slap_empty_bv;
2215 *binddn = li->li_idassert_authcDN;
2216 *bindcred = li->li_idassert_passwd;
2226 ldap_back_proxy_authz_bind(
2230 ldap_back_send_t sendok,
2231 struct berval *binddn,
2232 struct berval *bindcred )
2234 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2239 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2240 ndn = op->o_conn->c_ndn;
2246 if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
2247 #ifdef HAVE_CYRUS_SASL
2248 void *defaults = NULL;
2249 struct berval authzID = BER_BVNULL;
2251 LDAPControl **ctrlsp = NULL;
2252 LDAPMessage *result = NULL;
2253 const char *rmech = NULL;
2254 const char *save_text = rs->sr_text;
2257 LDAPControl ctrl, *ctrls[2];
2259 #endif /* SLAP_AUTH_DN */
2261 /* if SASL supports native authz, prepare for it */
2262 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
2263 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2265 switch ( li->li_idassert_mode ) {
2266 case LDAP_BACK_IDASSERT_OTHERID:
2267 case LDAP_BACK_IDASSERT_OTHERDN:
2268 authzID = li->li_idassert_authzID;
2271 case LDAP_BACK_IDASSERT_ANONYMOUS:
2272 BER_BVSTR( &authzID, "dn:" );
2275 case LDAP_BACK_IDASSERT_SELF:
2276 if ( BER_BVISNULL( &ndn ) ) {
2277 /* connection is not authc'd, so don't idassert */
2278 BER_BVSTR( &authzID, "dn:" );
2281 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
2282 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
2283 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
2284 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
2285 ndn.bv_val, ndn.bv_len + 1 );
2294 if ( li->li_idassert_secprops != NULL ) {
2295 rs->sr_err = ldap_set_option( lc->lc_ld,
2296 LDAP_OPT_X_SASL_SECPROPS,
2297 (void *)li->li_idassert_secprops );
2299 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
2300 rs->sr_err = LDAP_OTHER;
2301 if ( sendok & LDAP_BACK_SENDERR ) {
2302 send_ldap_result( op, rs );
2304 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2309 defaults = lutil_sasl_defaults( lc->lc_ld,
2310 li->li_idassert_sasl_mech.bv_val,
2311 li->li_idassert_sasl_realm.bv_val,
2312 li->li_idassert_authcID.bv_val,
2313 li->li_idassert_passwd.bv_val,
2315 if ( defaults == NULL ) {
2316 rs->sr_err = LDAP_OTHER;
2317 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2318 if ( sendok & LDAP_BACK_SENDERR ) {
2319 send_ldap_result( op, rs );
2325 if ( li->li_idassert_flags & LDAP_BACK_AUTH_DN_AUTHZID ) {
2326 assert( BER_BVISNULL( binddn ) );
2328 ctrl.ldctl_oid = LDAP_CONTROL_AUTHZID_REQUEST;
2329 ctrl.ldctl_iscritical = 0;
2330 BER_BVZERO( &ctrl.ldctl_value );
2335 #endif /* SLAP_AUTH_DN */
2338 rs->sr_err = ldap_sasl_interactive_bind( lc->lc_ld, binddn->bv_val,
2339 li->li_idassert_sasl_mech.bv_val,
2340 ctrlsp, NULL, LDAP_SASL_QUIET, lutil_sasl_interact, defaults,
2341 result, &rmech, &msgid );
2343 if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS )
2346 ldap_msgfree( result );
2348 if ( ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 || !result ) {
2349 ldap_get_option( lc->lc_ld, LDAP_OPT_RESULT_CODE, (void*)&rs->sr_err );
2350 ldap_get_option( lc->lc_ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&rs->sr_text );
2353 } while ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
2355 ldap_pvt_thread_mutex_lock( &li->li_counter_mutex );
2356 ldap_pvt_mp_add( li->li_ops_completed[ SLAP_OP_BIND ], 1 );
2357 ldap_pvt_thread_mutex_unlock( &li->li_counter_mutex );
2359 switch ( rs->sr_err ) {
2362 /* FIXME: right now, the only reason to check
2363 * response controls is RFC 3829 authzid */
2364 if ( li->li_idassert_flags & LDAP_BACK_AUTH_DN_AUTHZID ) {
2366 rc = ldap_parse_result( lc->lc_ld, result, NULL, NULL, NULL, NULL,
2368 if ( rc == LDAP_SUCCESS && ctrlsp ) {
2371 ctrl = ldap_control_find( LDAP_CONTROL_AUTHZID_RESPONSE,
2374 Debug( LDAP_DEBUG_TRACE, "%s: ldap_back_proxy_authz_bind: authzID=\"%s\" (authzid)\n",
2375 op->o_log_prefix, ctrl->ldctl_value.bv_val, 0 );
2376 if ( ctrl->ldctl_value.bv_len > STRLENOF("dn:") &&
2377 strncasecmp( ctrl->ldctl_value.bv_val, "dn:", STRLENOF("dn:") ) == 0 )
2380 bv.bv_val = &ctrl->ldctl_value.bv_val[STRLENOF("dn:")];
2381 bv.bv_len = ctrl->ldctl_value.bv_len - STRLENOF("dn:");
2382 ber_bvreplace( &lc->lc_bound_ndn, &bv );
2387 ldap_controls_free( ctrlsp );
2389 } else if ( li->li_idassert_flags & LDAP_BACK_AUTH_DN_WHOAMI ) {
2390 struct berval *val = NULL;
2391 rc = ldap_whoami_s( lc->lc_ld, &val, NULL, NULL );
2392 if ( rc == LDAP_SUCCESS && val != NULL ) {
2393 Debug( LDAP_DEBUG_TRACE, "%s: ldap_back_proxy_authz_bind: authzID=\"%s\" (whoami)\n",
2394 op->o_log_prefix, val->bv_val, 0 );
2395 if ( val->bv_len > STRLENOF("dn:") &&
2396 strncasecmp( val->bv_val, "dn:", STRLENOF("dn:") ) == 0 )
2399 bv.bv_val = &val->bv_val[STRLENOF("dn:")];
2400 bv.bv_len = val->bv_len - STRLENOF("dn:");
2401 ber_bvreplace( &lc->lc_bound_ndn, &bv );
2407 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_DN_MASK ) &&
2408 BER_BVISNULL( &lc->lc_bound_ndn ) )
2410 /* all in all, we only need it to be non-null */
2411 /* FIXME: should this be configurable? */
2412 static struct berval bv = BER_BVC("cn=authzdn");
2413 ber_bvreplace( &lc->lc_bound_ndn, &bv );
2415 #endif /* SLAP_AUTH_DN */
2416 LDAP_BACK_CONN_ISBOUND_SET( lc );
2419 case LDAP_LOCAL_ERROR:
2420 /* list client API error codes that require
2421 * to taint the connection */
2422 /* FIXME: should actually retry? */
2423 LDAP_BACK_CONN_TAINTED_SET( lc );
2428 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2429 rs->sr_err = slap_map_api2result( rs );
2430 if ( sendok & LDAP_BACK_SENDERR ) {
2431 send_ldap_result( op, rs );
2436 if ( save_text != rs->sr_text ) {
2437 ldap_memfree( (char *)rs->sr_text );
2438 rs->sr_text = save_text;
2441 ldap_msgfree( result );
2443 lutil_sasl_freedefs( defaults );
2445 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
2449 #endif /* HAVE_CYRUS_SASL */
2452 switch ( li->li_idassert_authmethod ) {
2453 case LDAP_AUTH_NONE:
2454 /* FIXME: do we really need this? */
2455 BER_BVSTR( binddn, "" );
2456 BER_BVSTR( bindcred, "" );
2459 case LDAP_AUTH_SIMPLE:
2460 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
2461 binddn->bv_val, LDAP_SASL_SIMPLE,
2462 bindcred, NULL, NULL, &msgid );
2463 rc = ldap_back_op_result( lc, op, rs, msgid,
2464 -1, ( sendok | LDAP_BACK_BINDING ) );
2466 ldap_pvt_thread_mutex_lock( &li->li_counter_mutex );
2467 ldap_pvt_mp_add( li->li_ops_completed[ SLAP_OP_BIND ], 1 );
2468 ldap_pvt_thread_mutex_unlock( &li->li_counter_mutex );
2473 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2474 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
2475 if ( sendok & LDAP_BACK_SENDERR ) {
2476 send_ldap_result( op, rs );
2481 if ( rc == LDAP_SUCCESS ) {
2482 /* set rebind stuff in case of successful proxyAuthz bind,
2483 * so that referral chasing is attempted using the right
2485 LDAP_BACK_CONN_ISBOUND_SET( lc );
2486 if ( !BER_BVISNULL( binddn ) ) {
2487 ber_bvreplace( &lc->lc_bound_ndn, binddn );
2490 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
2491 memset( lc->lc_cred.bv_val, 0,
2492 lc->lc_cred.bv_len );
2495 if ( LDAP_BACK_SAVECRED( li ) ) {
2496 if ( !BER_BVISNULL( bindcred ) ) {
2497 ber_bvreplace( &lc->lc_cred, bindcred );
2498 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
2502 lc->lc_cred.bv_len = 0;
2507 return LDAP_BACK_CONN_ISBOUND( lc );
2511 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
2512 * to existing server-side controls if required; if not,
2513 * the existing server-side controls are placed in *pctrls.
2514 * The caller, after using the controls in client API
2515 * operations, if ( *pctrls != op->o_ctrls ), should
2516 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
2517 * The function returns success if the control could
2518 * be added if required, or if it did nothing; in the future,
2519 * it might return some error if it failed.
2521 * if no bind took place yet, but the connection is bound
2522 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
2523 * and explicitly add proxyAuthz the control to every operation
2524 * with the dn bound to the connection as control value.
2526 * If no server-side controls are defined for the operation,
2527 * simply add the proxyAuthz control; otherwise, if the
2528 * proxyAuthz control is not already set, add it as
2531 * FIXME: is controls order significant for security?
2532 * ANSWER: controls ordering and interoperability
2533 * must be indicated by the specs of each control; if none
2534 * is specified, the order is irrelevant.
2537 ldap_back_proxy_authz_ctrl(
2540 struct berval *bound_ndn,
2542 slap_idassert_t *si,
2545 slap_idassert_mode_t mode;
2546 struct berval assertedID,
2550 rs->sr_err = SLAP_CB_CONTINUE;
2552 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
2553 * but if it is not set this test fails. We need a different
2554 * means to detect if idassert is enabled */
2555 if ( ( BER_BVISNULL( &si->si_bc.sb_authcId ) || BER_BVISEMPTY( &si->si_bc.sb_authcId ) )
2556 && ( BER_BVISNULL( &si->si_bc.sb_binddn ) || BER_BVISEMPTY( &si->si_bc.sb_binddn ) )
2557 && BER_BVISNULL( &si->si_bc.sb_saslmech ) )
2562 if ( !op->o_conn || op->o_do_not_cache || ( isroot = be_isroot( op ) ) ) {
2566 if ( op->o_tag == LDAP_REQ_BIND ) {
2567 ndn = op->o_req_ndn;
2569 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2570 ndn = op->o_conn->c_ndn;
2576 if ( si->si_mode == LDAP_BACK_IDASSERT_LEGACY ) {
2577 if ( op->o_proxy_authz ) {
2579 * FIXME: we do not want to perform proxyAuthz
2580 * on behalf of the client, because this would
2581 * be performed with "proxyauthzdn" privileges.
2583 * This might actually be too strict, since
2584 * the "proxyauthzdn" authzTo, and each entry's
2585 * authzFrom attributes may be crafted
2586 * to avoid unwanted proxyAuthz to take place.
2589 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2590 rs->sr_text = "proxyAuthz not allowed within namingContext";
2595 if ( !BER_BVISNULL( bound_ndn ) ) {
2599 if ( BER_BVISNULL( &ndn ) ) {
2603 if ( BER_BVISNULL( &si->si_bc.sb_binddn ) ) {
2607 } else if ( si->si_bc.sb_method == LDAP_AUTH_SASL ) {
2608 if ( ( si->si_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2610 /* already asserted in SASL via native authz */
2614 } else if ( si->si_authz && !isroot ) {
2616 struct berval authcDN;
2618 if ( BER_BVISNULL( &ndn ) ) {
2619 authcDN = slap_empty_bv;
2623 rc = slap_sasl_matches( op, si->si_authz,
2624 &authcDN, &authcDN );
2625 if ( rc != LDAP_SUCCESS ) {
2626 if ( si->si_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2627 /* ndn is not authorized
2628 * to use idassert */
2635 if ( op->o_proxy_authz ) {
2638 * 1) ignore the already set proxyAuthz control
2639 * 2) leave it in place, and don't set ours
2641 * 4) reject the operation
2643 * option (4) is very drastic
2644 * option (3) will make the remote server reject
2645 * the operation, thus being equivalent to (4)
2646 * option (2) will likely break the idassert
2647 * assumptions, so we cannot accept it;
2648 * option (1) means that we are contradicting
2649 * the client's reques.
2651 * I think (4) is the only correct choice.
2653 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2654 rs->sr_text = "proxyAuthz not allowed within namingContext";
2657 if ( op->o_is_auth_check ) {
2658 mode = LDAP_BACK_IDASSERT_NOASSERT;
2665 case LDAP_BACK_IDASSERT_LEGACY:
2666 /* original behavior:
2667 * assert the client's identity */
2668 case LDAP_BACK_IDASSERT_SELF:
2672 case LDAP_BACK_IDASSERT_ANONYMOUS:
2673 /* assert "anonymous" */
2674 assertedID = slap_empty_bv;
2677 case LDAP_BACK_IDASSERT_NOASSERT:
2678 /* don't assert; bind as proxyauthzdn */
2681 case LDAP_BACK_IDASSERT_OTHERID:
2682 case LDAP_BACK_IDASSERT_OTHERDN:
2683 /* assert idassert DN */
2684 assertedID = si->si_bc.sb_authzId;
2691 /* if we got here, "" is allowed to proxyAuthz */
2692 if ( BER_BVISNULL( &assertedID ) ) {
2693 assertedID = slap_empty_bv;
2696 /* don't idassert the bound DN (ITS#4497) */
2697 if ( dn_match( &assertedID, bound_ndn ) ) {
2701 ctrl->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
2702 ctrl->ldctl_iscritical = ( ( si->si_flags & LDAP_BACK_AUTH_PROXYAUTHZ_CRITICAL ) == LDAP_BACK_AUTH_PROXYAUTHZ_CRITICAL );
2704 switch ( si->si_mode ) {
2705 /* already in u:ID or dn:DN form */
2706 case LDAP_BACK_IDASSERT_OTHERID:
2707 case LDAP_BACK_IDASSERT_OTHERDN:
2708 ber_dupbv_x( &ctrl->ldctl_value, &assertedID, op->o_tmpmemctx );
2709 rs->sr_err = LDAP_SUCCESS;
2712 /* needs the dn: prefix */
2714 ctrl->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
2715 ctrl->ldctl_value.bv_val = op->o_tmpalloc( ctrl->ldctl_value.bv_len + 1,
2717 AC_MEMCPY( ctrl->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
2718 AC_MEMCPY( &ctrl->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
2719 assertedID.bv_val, assertedID.bv_len + 1 );
2720 rs->sr_err = LDAP_SUCCESS;
2724 /* Older versions of <draft-weltman-ldapv3-proxy> required
2725 * to encode the value of the authzID (and called it proxyDN);
2726 * this hack provides compatibility with those DSAs that
2727 * implement it this way */
2728 if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
2729 struct berval authzID = ctrl->ldctl_value;
2730 BerElementBuffer berbuf;
2731 BerElement *ber = (BerElement *)&berbuf;
2734 ber_init2( ber, 0, LBER_USE_DER );
2735 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2737 tag = ber_printf( ber, "O", &authzID );
2738 if ( tag == LBER_ERROR ) {
2739 rs->sr_err = LDAP_OTHER;
2743 if ( ber_flatten2( ber, &ctrl->ldctl_value, 1 ) == -1 ) {
2744 rs->sr_err = LDAP_OTHER;
2748 rs->sr_err = LDAP_SUCCESS;
2751 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2752 ber_free_buf( ber );
2754 if ( rs->sr_err != LDAP_SUCCESS ) {
2758 } else if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
2759 struct berval authzID = ctrl->ldctl_value,
2761 BerElementBuffer berbuf;
2762 BerElement *ber = (BerElement *)&berbuf;
2765 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
2766 rs->sr_err = LDAP_PROTOCOL_ERROR;
2771 tmp.bv_val += STRLENOF( "dn:" );
2772 tmp.bv_len -= STRLENOF( "dn:" );
2774 ber_init2( ber, 0, LBER_USE_DER );
2775 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2777 /* apparently, Mozilla API encodes this
2778 * as "SEQUENCE { LDAPDN }" */
2779 tag = ber_printf( ber, "{O}", &tmp );
2780 if ( tag == LBER_ERROR ) {
2781 rs->sr_err = LDAP_OTHER;
2785 if ( ber_flatten2( ber, &ctrl->ldctl_value, 1 ) == -1 ) {
2786 rs->sr_err = LDAP_OTHER;
2790 ctrl->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
2791 rs->sr_err = LDAP_SUCCESS;
2794 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2795 ber_free_buf( ber );
2797 if ( rs->sr_err != LDAP_SUCCESS ) {
2810 * if any needs to be added, it is prepended to existing ones,
2811 * in a newly allocated array. The companion function
2812 * ldap_back_controls_free() must be used to restore the original
2813 * status of op->o_ctrls.
2816 ldap_back_controls_add(
2820 LDAPControl ***pctrls )
2822 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2824 LDAPControl **ctrls = NULL;
2825 /* set to the maximum number of controls this backend can add */
2826 LDAPControl c[ 2 ] = { { 0 } };
2827 int n = 0, i, j1 = 0, j2 = 0;
2831 rs->sr_err = LDAP_SUCCESS;
2833 /* don't add controls if protocol is not LDAPv3 */
2834 switch ( li->li_version ) {
2839 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2848 /* put controls that go __before__ existing ones here */
2850 /* proxyAuthz for identity assertion */
2851 switch ( ldap_back_proxy_authz_ctrl( op, rs, &lc->lc_bound_ndn,
2852 li->li_version, &li->li_idassert, &c[ j1 ] ) )
2854 case SLAP_CB_CONTINUE:
2865 /* put controls that go __after__ existing ones here */
2867 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
2868 /* FIXME: according to <draft-wahl-ldap-session>,
2869 * the server should check if the control can be added
2870 * based on the identity of the client and so */
2872 /* session tracking */
2873 if ( LDAP_BACK_ST_REQUEST( li ) ) {
2874 switch ( slap_ctrl_session_tracking_request_add( op, rs, &c[ j1 + j2 ] ) ) {
2875 case SLAP_CB_CONTINUE:
2886 #endif /* SLAP_CONTROL_X_SESSION_TRACKING */
2888 if ( rs->sr_err == SLAP_CB_CONTINUE ) {
2889 rs->sr_err = LDAP_SUCCESS;
2892 /* if nothing to do, just bail out */
2893 if ( j1 == 0 && j2 == 0 ) {
2897 assert( j1 + j2 <= (int) (sizeof( c )/sizeof( c[0] )) );
2899 if ( op->o_ctrls ) {
2900 for ( n = 0; op->o_ctrls[ n ]; n++ )
2901 /* just count ctrls */ ;
2904 ctrls = op->o_tmpalloc( (n + j1 + j2 + 1) * sizeof( LDAPControl * ) + ( j1 + j2 ) * sizeof( LDAPControl ),
2907 ctrls[ 0 ] = (LDAPControl *)&ctrls[ n + j1 + j2 + 1 ];
2908 *ctrls[ 0 ] = c[ 0 ];
2909 for ( i = 1; i < j1; i++ ) {
2910 ctrls[ i ] = &ctrls[ 0 ][ i ];
2911 *ctrls[ i ] = c[ i ];
2916 if ( op->o_ctrls ) {
2917 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
2918 ctrls[ i + j1 ] = op->o_ctrls[ i ];
2924 ctrls[ n ] = (LDAPControl *)&ctrls[ n + j2 + 1 ] + j1;
2925 *ctrls[ n ] = c[ j1 ];
2926 for ( i = 1; i < j2; i++ ) {
2927 ctrls[ n + i ] = &ctrls[ n ][ i ];
2928 *ctrls[ n + i ] = c[ i ];
2932 ctrls[ n + j2 ] = NULL;
2935 if ( ctrls == NULL ) {
2936 ctrls = op->o_ctrls;
2945 ldap_back_controls_free( Operation *op, SlapReply *rs, LDAPControl ***pctrls )
2947 LDAPControl **ctrls = *pctrls;
2949 /* we assume that the controls added by the proxy come first,
2950 * so as soon as we find op->o_ctrls[ 0 ] we can stop */
2951 if ( ctrls && ctrls != op->o_ctrls ) {
2952 int i = 0, n = 0, n_added;
2953 LDAPControl *lower, *upper;
2955 assert( ctrls[ 0 ] != NULL );
2957 for ( n = 0; ctrls[ n ] != NULL; n++ )
2960 if ( op->o_ctrls ) {
2961 for ( i = 0; op->o_ctrls[ i ] != NULL; i++ )
2966 lower = (LDAPControl *)&ctrls[ n ];
2967 upper = &lower[ n_added ];
2969 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
2970 if ( ctrls[ i ] < lower || ctrls[ i ] >= upper ) {
2971 /* original; don't touch */
2975 if ( !BER_BVISNULL( &ctrls[ i ]->ldctl_value ) ) {
2976 op->o_tmpfree( ctrls[ i ]->ldctl_value.bv_val, op->o_tmpmemctx );
2980 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2989 ldap_back_conn2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen )
2991 char tbuf[ SLAP_TEXT_BUFLEN ];
2992 char *ptr = buf, *end = buf + buflen;
2995 if ( ptr + sizeof("conn=") > end ) return -1;
2996 ptr = lutil_strcopy( ptr, "conn=" );
2998 len = ldap_back_connid2str( lc, ptr, (ber_len_t)(end - ptr) );
3000 if ( ptr >= end ) return -1;
3002 if ( !BER_BVISNULL( &lc->lcb_local_ndn ) ) {
3003 if ( ptr + sizeof(" DN=\"\"") + lc->lcb_local_ndn.bv_len > end ) return -1;
3004 ptr = lutil_strcopy( ptr, " DN=\"" );
3005 ptr = lutil_strncopy( ptr, lc->lcb_local_ndn.bv_val, lc->lcb_local_ndn.bv_len );
3009 if ( lc->lcb_create_time != 0 ) {
3010 len = snprintf( tbuf, sizeof(tbuf), "%ld", lc->lcb_create_time );
3011 if ( ptr + sizeof(" created=") + len >= end ) return -1;
3012 ptr = lutil_strcopy( ptr, " created=" );
3013 ptr = lutil_strcopy( ptr, tbuf );
3016 if ( lc->lcb_time != 0 ) {
3017 len = snprintf( tbuf, sizeof(tbuf), "%ld", lc->lcb_time );
3018 if ( ptr + sizeof(" modified=") + len >= end ) return -1;
3019 ptr = lutil_strcopy( ptr, " modified=" );
3020 ptr = lutil_strcopy( ptr, tbuf );
3023 len = snprintf( tbuf, sizeof(tbuf), "%u", lc->lcb_refcnt );
3024 if ( ptr + sizeof(" refcnt=") + len >= end ) return -1;
3025 ptr = lutil_strcopy( ptr, " refcnt=" );
3026 ptr = lutil_strcopy( ptr, tbuf );
3032 ldap_back_connid2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen )
3034 static struct berval conns[] = {
3036 BER_BVC("ROOTDN-TLS"),
3038 BER_BVC("ANON-TLS"),
3040 BER_BVC("BIND-TLS"),
3046 if ( LDAP_BACK_PCONN_ISPRIV( (const ldapconn_t *)lc ) ) {
3050 cid = (long)lc->lcb_conn;
3051 assert( cid >= LDAP_BACK_PCONN_FIRST && cid < LDAP_BACK_PCONN_LAST );
3055 if ( bv->bv_len >= buflen ) {
3056 return bv->bv_len + 1;
3060 lutil_strncopy( buf, bv->bv_val, bv->bv_len + 1 );
3063 len = snprintf( buf, buflen, "%lu", lc->lcb_conn->c_connid );