1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2012 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_back_controls_free( op, rs, &ctrls );
280 if ( rc == LDAP_SUCCESS ) {
281 op->o_conn->c_authz_cookie = op->o_bd->be_private;
283 /* If defined, proxyAuthz will be used also when
284 * back-ldap is the authorizing backend; for this
285 * purpose, after a successful bind the connection
286 * is left for further binds, and further operations
287 * on this client connection will use a default
288 * connection with identity assertion */
289 /* NOTE: use with care */
290 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
291 ldap_back_release_conn( li, lc );
295 /* rebind is now done inside ldap_back_proxy_authz_bind()
296 * in case of success */
297 LDAP_BACK_CONN_ISBOUND_SET( lc );
298 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
300 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
301 memset( lc->lc_cred.bv_val, 0,
302 lc->lc_cred.bv_len );
305 if ( LDAP_BACK_SAVECRED( li ) ) {
306 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
307 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
310 lc->lc_cred.bv_len = 0;
314 /* must re-insert if local DN changed as result of bind */
315 if ( !LDAP_BACK_CONN_ISBOUND( lc )
316 || ( !dn_match( &op->o_req_ndn, &lc->lc_local_ndn )
317 && !LDAP_BACK_PCONN_ISPRIV( lc ) ) )
322 /* wait for all other ops to release the connection */
324 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
325 if ( lc->lc_refcnt > 1 ) {
326 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
327 ldap_pvt_thread_yield();
331 #if LDAP_BACK_PRINT_CONNTREE > 0
332 ldap_back_print_conntree( li, ">>> ldap_back_bind" );
333 #endif /* LDAP_BACK_PRINT_CONNTREE */
335 assert( lc->lc_refcnt == 1 );
336 ldap_back_conn_delete( li, lc );
338 /* delete all cached connections with the current connection */
339 if ( LDAP_BACK_SINGLECONN( li ) ) {
340 while ( ( tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc, ldap_back_conn_cmp ) ) != NULL )
342 assert( !LDAP_BACK_PCONN_ISPRIV( lc ) );
343 Debug( LDAP_DEBUG_TRACE,
344 "=>ldap_back_bind: destroying conn %lu (refcnt=%u)\n",
345 lc->lc_conn->c_connid, lc->lc_refcnt, 0 );
347 if ( tmplc->lc_refcnt != 0 ) {
349 LDAP_BACK_CONN_TAINTED_SET( tmplc );
350 LDAP_BACK_CONN_CACHED_CLEAR( tmplc );
354 * Needs a test because the handler may be corrupted,
355 * and calling ldap_unbind on a corrupted header results
356 * in a segmentation fault
358 ldap_back_conn_free( tmplc );
363 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
364 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
365 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
366 LDAP_BACK_PCONN_ROOTDN_SET( lc, op );
368 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
369 ldap_back_conndn_cmp, ldap_back_conndn_dup );
372 #if LDAP_BACK_PRINT_CONNTREE > 0
373 ldap_back_print_conntree( li, "<<< ldap_back_bind" );
374 #endif /* LDAP_BACK_PRINT_CONNTREE */
376 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
379 LDAP_BACK_CONN_CACHED_SET( lc );
383 /* duplicate; someone else successfully bound
384 * on the same connection with the same identity;
385 * we can do this because lc_refcnt == 1 */
386 ldap_back_conn_free( lc );
392 ldap_back_release_conn( li, lc );
399 * ldap_back_conndn_cmp
401 * compares two ldapconn_t based on the value of the conn pointer
402 * and of the local DN; used by avl stuff for insert, lookup
406 ldap_back_conndn_cmp( const void *c1, const void *c2 )
408 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
409 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
412 /* If local DNs don't match, it is definitely not a match */
413 /* For shared sessions, conn is NULL. Only explicitly
414 * bound sessions will have non-NULL conn.
416 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
418 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
425 * ldap_back_conndnlc_cmp
427 * compares two ldapconn_t based on the value of the conn pointer,
428 * the local DN and the lc pointer; used by avl stuff for insert, lookup
432 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
434 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
435 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
438 /* If local DNs don't match, it is definitely not a match */
439 /* For shared sessions, conn is NULL. Only explicitly
440 * bound sessions will have non-NULL conn.
442 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
444 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
446 rc = SLAP_PTRCMP( lc1, lc2 );
456 * compares two ldapconn_t based on the value of the conn pointer;
457 * used by avl stuff for delete of all conns with the same connid
460 ldap_back_conn_cmp( const void *c1, const void *c2 )
462 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
463 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
465 /* For shared sessions, conn is NULL. Only explicitly
466 * bound sessions will have non-NULL conn.
468 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
472 * ldap_back_conndn_dup
474 * returns -1 in case a duplicate ldapconn_t has been inserted;
478 ldap_back_conndn_dup( void *c1, void *c2 )
480 ldapconn_t *lc1 = (ldapconn_t *)c1;
481 ldapconn_t *lc2 = (ldapconn_t *)c2;
483 /* Cannot have more than one shared session with same DN */
484 if ( lc1->lc_conn == lc2->lc_conn &&
485 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
494 ldap_back_freeconn( ldapinfo_t *li, ldapconn_t *lc, int dolock )
497 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
500 #if LDAP_BACK_PRINT_CONNTREE > 0
501 ldap_back_print_conntree( li, ">>> ldap_back_freeconn" );
502 #endif /* LDAP_BACK_PRINT_CONNTREE */
504 (void)ldap_back_conn_delete( li, lc );
506 if ( lc->lc_refcnt == 0 ) {
507 ldap_back_conn_free( (void *)lc );
510 #if LDAP_BACK_PRINT_CONNTREE > 0
511 ldap_back_print_conntree( li, "<<< ldap_back_freeconn" );
512 #endif /* LDAP_BACK_PRINT_CONNTREE */
515 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
532 int rc = LDAP_SUCCESS;
534 /* start TLS ("tls-[try-]{start,propagate}" statements) */
535 if ( ( LDAP_BACK_USE_TLS_F( flags ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS_F( flags ) ) )
536 && !ldap_is_ldaps_url( url ) )
538 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
540 * use asynchronous StartTLS
541 * in case, chase referral (not implemented yet)
545 if ( protocol == 0 ) {
546 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
550 if ( protocol < LDAP_VERSION3 ) {
551 /* we should rather bail out... */
552 rc = LDAP_UNWILLING_TO_PERFORM;
553 *text = "invalid protocol version";
556 if ( rc == LDAP_SUCCESS ) {
557 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
560 if ( rc == LDAP_SUCCESS ) {
561 LDAPMessage *res = NULL;
564 LDAP_BACK_TV_SET( &tv );
567 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
569 rc = LDAP_UNAVAILABLE;
571 } else if ( rc == 0 ) {
572 if ( retries != LDAP_BACK_RETRY_NEVER ) {
573 ldap_pvt_thread_yield();
577 LDAP_BACK_TV_SET( &tv );
580 rc = LDAP_UNAVAILABLE;
582 } else if ( rc == LDAP_RES_EXTENDED ) {
583 struct berval *data = NULL;
585 rc = ldap_parse_extended_result( ld, res,
587 if ( rc == LDAP_SUCCESS ) {
589 rc = ldap_parse_result( ld, res, &rs.sr_err,
590 NULL, NULL, NULL, NULL, 1 );
591 if ( rc != LDAP_SUCCESS ) {
594 rc = slap_map_api2result( &rs );
597 /* FIXME: in case a referral
598 * is returned, should we try
599 * using it instead of the
601 if ( rc == LDAP_SUCCESS ) {
602 rc = ldap_install_tls( ld );
604 } else if ( rc == LDAP_REFERRAL ) {
605 rc = LDAP_UNWILLING_TO_PERFORM;
606 *text = "unwilling to chase referral returned by Start TLS exop";
610 if ( data->bv_val ) {
611 ber_memfree( data->bv_val );
625 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
627 * use synchronous StartTLS
629 rc = ldap_start_tls_s( ld, NULL, NULL );
630 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
632 /* if StartTLS is requested, only attempt it if the URL
633 * is not "ldaps://"; this may occur not only in case
634 * of misconfiguration, but also when used in the chain
635 * overlay, where the "uri" can be parsed out of a referral */
641 case LDAP_SERVER_DOWN:
645 if ( LDAP_BACK_TLS_CRITICAL_F( flags ) ) {
646 *text = "could not start TLS";
650 /* in case Start TLS is not critical */
662 #endif /* HAVE_TLS */
665 ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
667 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
671 int is_tls = op->o_conn->c_is_tls;
672 int flags = li->li_flags;
673 time_t lctime = (time_t)(-1);
675 #endif /* HAVE_TLS */
677 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
678 rs->sr_err = ldap_initialize( &ld, li->li_uri );
679 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
680 if ( rs->sr_err != LDAP_SUCCESS ) {
684 if ( li->li_urllist_f ) {
685 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
688 /* Set LDAP version. This will always succeed: If the client
689 * bound with a particular version, then so can we.
691 if ( li->li_version != 0 ) {
692 version = li->li_version;
694 } else if ( op->o_protocol != 0 ) {
695 version = op->o_protocol;
698 /* assume it's an internal op; set to LDAPv3 */
699 version = LDAP_VERSION3;
701 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
703 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
704 ldap_set_option( ld, LDAP_OPT_REFERRALS,
705 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
707 if ( li->li_network_timeout > 0 ) {
710 tv.tv_sec = li->li_network_timeout;
712 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
716 if ( LDAP_BACK_CONN_ISPRIV( lc ) ) {
717 /* See "rationale" comment in ldap_back_getconn() */
718 if ( li->li_acl_authmethod == LDAP_AUTH_NONE &&
719 li->li_idassert_authmethod != LDAP_AUTH_NONE )
720 sb = &li->li_idassert.si_bc;
724 } else if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
725 sb = &li->li_idassert.si_bc;
731 if ( sb->sb_tls_do_init ) {
732 bindconf_tls_set( sb, ld );
733 } else if ( sb->sb_tls_ctx ) {
734 ldap_set_option( ld, LDAP_OPT_X_TLS_CTX, sb->sb_tls_ctx );
737 /* if required by the bindconf configuration, force TLS */
738 if ( ( sb == &li->li_acl || sb == &li->li_idassert.si_bc ) &&
741 flags |= LDAP_BACK_F_USE_TLS;
744 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
745 assert( li->li_uri_mutex_do_not_lock == 0 );
746 li->li_uri_mutex_do_not_lock = 1;
747 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
748 li->li_uri, flags, li->li_nretries, &rs->sr_text );
749 li->li_uri_mutex_do_not_lock = 0;
750 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
751 if ( rs->sr_err != LDAP_SUCCESS ) {
752 ldap_unbind_ext( ld, NULL, NULL );
753 rs->sr_text = "Start TLS failed";
756 } else if ( li->li_idle_timeout ) {
757 /* only touch when activity actually took place... */
760 #endif /* HAVE_TLS */
766 LDAP_BACK_CONN_ISTLS_SET( lc );
768 LDAP_BACK_CONN_ISTLS_CLEAR( lc );
770 if ( lctime != (time_t)(-1) ) {
771 lc->lc_time = lctime;
773 #endif /* HAVE_TLS */
776 if ( rs->sr_err != LDAP_SUCCESS ) {
777 rs->sr_err = slap_map_api2result( rs );
778 if ( sendok & LDAP_BACK_SENDERR ) {
779 if ( rs->sr_text == NULL ) {
780 rs->sr_text = "Proxy connection initialization failed";
782 send_ldap_result( op, rs );
786 if ( li->li_conn_ttl > 0 ) {
787 lc->lc_create_time = op->o_time;
798 ldap_back_send_t sendok,
799 struct berval *binddn,
800 struct berval *bindcred )
802 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
803 ldapconn_t *lc = NULL,
806 lookupconn = !( sendok & LDAP_BACK_BINDING );
808 /* if the server is quarantined, and
809 * - the current interval did not expire yet, or
810 * - no more retries should occur,
811 * don't return the connection */
812 if ( li->li_isquarantined ) {
813 slap_retry_info_t *ri = &li->li_quarantine;
816 if ( li->li_quarantine.ri_interval ) {
817 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
818 if ( li->li_isquarantined == LDAP_BACK_FQ_YES ) {
819 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
820 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
822 Debug( LDAP_DEBUG_ANY,
823 "%s: ldap_back_getconn quarantine "
824 "retry block #%d try #%d.\n",
825 op->o_log_prefix, ri->ri_idx, ri->ri_count );
826 li->li_isquarantined = LDAP_BACK_FQ_RETRYING;
829 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
833 rs->sr_err = LDAP_UNAVAILABLE;
834 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
835 rs->sr_text = "Target is quarantined";
836 send_ldap_result( op, rs );
842 /* Internal searches are privileged and shared. So is root. */
843 if ( op->o_do_not_cache || be_isroot( op ) ) {
844 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
845 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
846 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
849 struct berval tmpbinddn,
856 if ( binddn == NULL ) {
859 if ( bindcred == NULL ) {
860 bindcred = &tmpbindcred;
862 if ( op->o_tag == LDAP_REQ_BIND ) {
863 save_o_dn = op->o_dn;
864 save_o_ndn = op->o_ndn;
865 op->o_dn = op->o_req_dn;
866 op->o_ndn = op->o_req_ndn;
868 isproxyauthz = ldap_back_is_proxy_authz( op, rs, sendok, binddn, bindcred );
869 if ( op->o_tag == LDAP_REQ_BIND ) {
870 op->o_dn = save_o_dn;
871 op->o_ndn = save_o_ndn;
873 if ( isproxyauthz == -1 ) {
877 lc_curr.lc_local_ndn = op->o_ndn;
878 /* Explicit binds must not be shared;
879 * however, explicit binds are piped in a special connection
880 * when idassert is to occur with "override" set */
881 if ( op->o_tag == LDAP_REQ_BIND && !isproxyauthz ) {
882 lc_curr.lc_conn = op->o_conn;
885 if ( isproxyauthz && !( sendok & LDAP_BACK_BINDING ) ) {
886 lc_curr.lc_local_ndn = *binddn;
887 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
888 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
890 } else if ( isproxyauthz && ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) {
891 lc_curr.lc_local_ndn = slap_empty_bv;
892 LDAP_BACK_PCONN_BIND_SET( &lc_curr, op );
893 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
896 } else if ( SLAP_IS_AUTHZ_BACKEND( op ) ) {
897 lc_curr.lc_conn = op->o_conn;
900 LDAP_BACK_PCONN_ANON_SET( &lc_curr, op );
905 /* Explicit Bind requests always get their own conn */
908 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
909 if ( LDAP_BACK_PCONN_ISPRIV( &lc_curr ) ) {
910 /* lookup a conn that's not binding */
911 LDAP_TAILQ_FOREACH( lc,
912 &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv,
915 if ( !LDAP_BACK_CONN_BINDING( lc ) && lc->lc_refcnt == 0 ) {
921 if ( lc != LDAP_TAILQ_LAST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
924 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
926 LDAP_TAILQ_ENTRY_INIT( lc, lc_q );
927 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
931 } else if ( !LDAP_BACK_USE_TEMPORARIES( li )
932 && li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_num == li->li_conn_priv_max )
934 lc = LDAP_TAILQ_FIRST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv );
939 /* Searches for a ldapconn in the avl tree */
940 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
941 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
945 /* Don't reuse connections while they're still binding */
946 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
947 if ( !LDAP_BACK_USE_TEMPORARIES( li ) ) {
948 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
950 ldap_pvt_thread_yield();
957 if ( op->o_tag == LDAP_REQ_BIND ) {
958 /* right now, this is the only possible case */
959 assert( ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) );
960 LDAP_BACK_CONN_BINDING_SET( lc );
963 refcnt = ++lc->lc_refcnt;
966 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
969 /* Looks like we didn't get a bind. Open a new session... */
971 lc = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
972 lc->lc_flags = li->li_flags;
973 lc->lc_lcflags = lc_curr.lc_lcflags;
974 if ( ldap_back_prepare_conn( lc, op, rs, sendok ) != LDAP_SUCCESS ) {
979 if ( sendok & LDAP_BACK_BINDING ) {
980 LDAP_BACK_CONN_BINDING_SET( lc );
983 lc->lc_conn = lc_curr.lc_conn;
984 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
987 * the rationale is: connections as the rootdn are privileged,
988 * so li_acl is to be used; however, in some cases
989 * one already configured identity assertion with a highly
990 * privileged idassert_authcDN, so if li_acl is not configured
991 * and idassert is, use idassert instead.
993 * might change in the future, because it's preferable
994 * to make clear what identity is being used, since
995 * the only drawback is that one risks to configure
996 * the same identity twice...
998 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
999 if ( li->li_acl_authmethod == LDAP_AUTH_NONE &&
1000 li->li_idassert_authmethod != LDAP_AUTH_NONE ) {
1001 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
1002 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
1005 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
1006 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
1008 LDAP_BACK_CONN_ISPRIV_SET( lc );
1010 } else if ( LDAP_BACK_CONN_ISIDASSERT( &lc_curr ) ) {
1011 if ( !LDAP_BACK_PCONN_ISBIND( &lc_curr ) ) {
1012 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
1013 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
1015 LDAP_BACK_CONN_ISIDASSERT_SET( lc );
1018 BER_BVZERO( &lc->lc_cred );
1019 BER_BVZERO( &lc->lc_bound_ndn );
1020 if ( !BER_BVISEMPTY( &op->o_ndn )
1021 && SLAP_IS_AUTHZ_BACKEND( op ) )
1023 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
1028 /* if start TLS failed but it was not mandatory,
1029 * check if the non-TLS connection was already
1030 * in cache; in case, destroy the newly created
1031 * connection and use the existing one */
1032 if ( LDAP_BACK_PCONN_ISTLS( lc )
1033 && !ldap_tls_inplace( lc->lc_ld ) )
1035 ldapconn_t *tmplc = NULL;
1036 int idx = LDAP_BACK_CONN2PRIV( &lc_curr ) - 1;
1038 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1039 LDAP_TAILQ_FOREACH( tmplc,
1040 &li->li_conn_priv[ idx ].lic_priv,
1043 if ( !LDAP_BACK_CONN_BINDING( tmplc ) ) {
1048 if ( tmplc != NULL ) {
1049 refcnt = ++tmplc->lc_refcnt;
1050 ldap_back_conn_free( lc );
1053 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1055 if ( tmplc != NULL ) {
1059 #endif /* HAVE_TLS */
1061 /* Inserts the newly created ldapconn in the avl tree */
1062 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1064 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1066 assert( lc->lc_refcnt == 1 );
1068 #if LDAP_BACK_PRINT_CONNTREE > 0
1069 ldap_back_print_conntree( li, ">>> ldap_back_getconn(insert)" );
1070 #endif /* LDAP_BACK_PRINT_CONNTREE */
1072 if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1073 if ( li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num < li->li_conn_priv_max ) {
1074 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv, lc, lc_q );
1075 li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num++;
1076 LDAP_BACK_CONN_CACHED_SET( lc );
1079 LDAP_BACK_CONN_TAINTED_SET( lc );
1084 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
1085 ldap_back_conndn_cmp, ldap_back_conndn_dup );
1086 LDAP_BACK_CONN_CACHED_SET( lc );
1089 #if LDAP_BACK_PRINT_CONNTREE > 0
1090 ldap_back_print_conntree( li, "<<< ldap_back_getconn(insert)" );
1091 #endif /* LDAP_BACK_PRINT_CONNTREE */
1093 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1095 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1096 char buf[ SLAP_TEXT_BUFLEN ];
1098 snprintf( buf, sizeof( buf ),
1099 "lc=%p inserted refcnt=%u rc=%d",
1100 (void *)lc, refcnt, rs->sr_err );
1102 Debug( LDAP_DEBUG_TRACE,
1103 "=>ldap_back_getconn: %s: %s\n",
1104 op->o_log_prefix, buf, 0 );
1107 if ( !LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1108 /* Err could be -1 in case a duplicate ldapconn is inserted */
1109 switch ( rs->sr_err ) {
1114 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1115 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( li ) ) {
1116 /* duplicate: free and try to get the newly created one */
1117 ldap_back_conn_free( lc );
1122 /* taint connection, so that it'll be freed when released */
1123 LDAP_BACK_CONN_TAINTED_SET( lc );
1127 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1128 ldap_back_conn_free( lc );
1129 rs->sr_err = LDAP_OTHER;
1130 rs->sr_text = "Proxy bind collision";
1131 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1132 send_ldap_result( op, rs );
1141 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
1142 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
1146 /* let it be used, but taint/delete it so that
1147 * no-one else can look it up any further */
1148 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1150 #if LDAP_BACK_PRINT_CONNTREE > 0
1151 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1152 #endif /* LDAP_BACK_PRINT_CONNTREE */
1154 (void)ldap_back_conn_delete( li, lc );
1155 LDAP_BACK_CONN_TAINTED_SET( lc );
1157 #if LDAP_BACK_PRINT_CONNTREE > 0
1158 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1159 #endif /* LDAP_BACK_PRINT_CONNTREE */
1161 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1164 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1165 char buf[ SLAP_TEXT_BUFLEN ];
1167 snprintf( buf, sizeof( buf ),
1168 "conn %p fetched refcnt=%u%s",
1170 expiring ? " expiring" : "" );
1171 Debug( LDAP_DEBUG_TRACE,
1172 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
1178 #endif /* HAVE_TLS */
1184 ldap_back_release_conn_lock(
1190 ldapconn_t *lc = *lcp;
1193 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1195 assert( lc->lc_refcnt > 0 );
1196 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1198 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
1199 ldap_back_freeconn( li, lc, 0 );
1203 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1208 ldap_back_quarantine(
1212 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1214 slap_retry_info_t *ri = &li->li_quarantine;
1216 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
1218 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1219 time_t new_last = slap_get_time();
1221 switch ( li->li_isquarantined ) {
1222 case LDAP_BACK_FQ_NO:
1223 if ( ri->ri_last == new_last ) {
1227 Debug( LDAP_DEBUG_ANY,
1228 "%s: ldap_back_quarantine enter.\n",
1229 op->o_log_prefix, 0, 0 );
1235 case LDAP_BACK_FQ_RETRYING:
1236 Debug( LDAP_DEBUG_ANY,
1237 "%s: ldap_back_quarantine block #%d try #%d failed.\n",
1238 op->o_log_prefix, ri->ri_idx, ri->ri_count );
1241 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1242 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1253 li->li_isquarantined = LDAP_BACK_FQ_YES;
1254 ri->ri_last = new_last;
1256 } else if ( li->li_isquarantined != LDAP_BACK_FQ_NO ) {
1257 if ( ri->ri_last == slap_get_time() ) {
1261 Debug( LDAP_DEBUG_ANY,
1262 "%s: ldap_back_quarantine exit (%d) err=%d.\n",
1263 op->o_log_prefix, li->li_isquarantined, rs->sr_err );
1265 if ( li->li_quarantine_f ) {
1266 (void)li->li_quarantine_f( li, li->li_quarantine_p );
1271 li->li_isquarantined = LDAP_BACK_FQ_NO;
1275 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
1279 ldap_back_dobind_cb(
1284 ber_tag_t *tptr = op->o_callback->sc_private;
1286 rs->sr_tag = slap_req2res( op->o_tag );
1288 return SLAP_CB_CONTINUE;
1292 * ldap_back_dobind_int
1294 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1297 ldap_back_dobind_int(
1301 ldap_back_send_t sendok,
1305 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1308 struct berval binddn = slap_empty_bv,
1309 bindcred = slap_empty_bv;
1315 ber_tag_t o_tag = op->o_tag;
1316 slap_callback cb = {0};
1319 assert( lcp != NULL );
1320 assert( retries >= 0 );
1322 if ( sendok & LDAP_BACK_GETCONN ) {
1323 assert( *lcp == NULL );
1325 lc = ldap_back_getconn( op, rs, sendok, &binddn, &bindcred );
1335 assert( lc != NULL );
1339 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1342 if ( binding == 0 ) {
1343 /* check if already bound */
1344 rc = isbound = LDAP_BACK_CONN_ISBOUND( lc );
1347 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1352 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
1353 /* if someone else is about to bind it, give up and retry */
1355 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1357 ldap_pvt_thread_yield();
1361 /* otherwise this thread will bind it */
1362 LDAP_BACK_CONN_BINDING_SET( lc );
1368 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1372 * FIXME: we need to let clients use proxyAuthz
1373 * otherwise we cannot do symmetric pools of servers;
1374 * we have to live with the fact that a user can
1375 * authorize itself as any ID that is allowed
1376 * by the authzTo directive of the "proxyauthzdn".
1379 * NOTE: current Proxy Authorization specification
1380 * and implementation do not allow proxy authorization
1381 * control to be provided with Bind requests
1384 * if no bind took place yet, but the connection is bound
1385 * and the "idassert-authcDN" (or other ID) is set,
1386 * then bind as the asserting identity and explicitly
1387 * add the proxyAuthz control to every operation with the
1388 * dn bound to the connection as control value.
1389 * This is done also if this is the authorizing backend,
1390 * but the "override" flag is given to idassert.
1391 * It allows to use SASL bind and yet proxyAuthz users
1393 op->o_tag = LDAP_REQ_BIND;
1394 cb.sc_next = op->o_callback;
1395 cb.sc_private = &o_tag;
1396 cb.sc_response = ldap_back_dobind_cb;
1397 op->o_callback = &cb;
1399 if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
1400 if ( BER_BVISEMPTY( &binddn ) && BER_BVISEMPTY( &bindcred ) ) {
1401 /* if we got here, it shouldn't return result */
1402 rc = ldap_back_is_proxy_authz( op, rs,
1403 LDAP_BACK_DONTSEND, &binddn, &bindcred );
1405 Debug( LDAP_DEBUG_ANY, "Error: ldap_back_is_proxy_authz "
1406 "returned %d, misconfigured URI?\n", rc, 0, 0 );
1407 rs->sr_err = LDAP_OTHER;
1408 rs->sr_text = "misconfigured URI?";
1409 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1410 if ( sendok & LDAP_BACK_SENDERR ) {
1411 send_ldap_result( op, rs );
1416 rc = ldap_back_proxy_authz_bind( lc, op, rs, sendok, &binddn, &bindcred );
1420 #ifdef HAVE_CYRUS_SASL
1421 if ( LDAP_BACK_CONN_ISPRIV( lc )) {
1423 if ( li->li_acl_authmethod != LDAP_AUTH_NONE )
1426 sb = &li->li_idassert.si_bc;
1428 if ( sb->sb_method == LDAP_AUTH_SASL ) {
1429 void *defaults = NULL;
1431 if ( sb->sb_secprops != NULL ) {
1432 rc = ldap_set_option( lc->lc_ld,
1433 LDAP_OPT_X_SASL_SECPROPS, sb->sb_secprops );
1435 if ( rc != LDAP_OPT_SUCCESS ) {
1436 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
1437 "(SECPROPS,\"%s\") failed!\n",
1438 sb->sb_secprops, 0, 0 );
1443 defaults = lutil_sasl_defaults( lc->lc_ld,
1444 sb->sb_saslmech.bv_val,
1445 sb->sb_realm.bv_val,
1446 sb->sb_authcId.bv_val,
1449 if ( defaults == NULL ) {
1450 rs->sr_err = LDAP_OTHER;
1451 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1452 if ( sendok & LDAP_BACK_SENDERR ) {
1453 send_ldap_result( op, rs );
1458 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
1459 sb->sb_binddn.bv_val,
1460 sb->sb_saslmech.bv_val, NULL, NULL,
1461 LDAP_SASL_QUIET, lutil_sasl_interact,
1464 lutil_sasl_freedefs( defaults );
1466 switch ( rs->sr_err ) {
1468 LDAP_BACK_CONN_ISBOUND_SET( lc );
1471 case LDAP_LOCAL_ERROR:
1472 /* list client API error codes that require
1473 * to taint the connection */
1474 /* FIXME: should actually retry? */
1475 LDAP_BACK_CONN_TAINTED_SET( lc );
1480 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1481 rs->sr_err = slap_map_api2result( rs );
1482 if ( sendok & LDAP_BACK_SENDERR ) {
1483 send_ldap_result( op, rs );
1488 if ( LDAP_BACK_QUARANTINE( li ) ) {
1489 ldap_back_quarantine( op, rs );
1495 #endif /* HAVE_CYRUS_SASL */
1498 if ( BER_BVISNULL( &lc->lc_cred ) ) {
1500 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) && !BER_BVISEMPTY( &lc->lc_bound_ndn ) ) {
1501 Debug( LDAP_DEBUG_ANY, "%s ldap_back_dobind_int: DN=\"%s\" without creds, binding anonymously",
1502 op->o_log_prefix, lc->lc_bound_ndn.bv_val, 0 );
1506 tmp_dn = lc->lc_bound_ndn.bv_val;
1508 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1510 LDAP_SASL_SIMPLE, &lc->lc_cred,
1511 NULL, NULL, &msgid );
1513 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
1514 if ( retries != LDAP_BACK_RETRY_NEVER ) {
1516 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1519 assert( lc->lc_refcnt > 0 );
1520 if ( lc->lc_refcnt == 1 ) {
1521 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1524 /* lc here must be the regular lc, reset and ready for init */
1525 rs->sr_err = ldap_back_prepare_conn( lc, op, rs, sendok );
1526 if ( rs->sr_err != LDAP_SUCCESS ) {
1527 sendok &= ~LDAP_BACK_SENDERR;
1533 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1536 if ( rs->sr_err == LDAP_SUCCESS ) {
1537 if ( retries > 0 ) {
1544 assert( lc->lc_refcnt == 1 );
1546 ldap_back_freeconn( li, lc, dolock );
1548 rs->sr_err = slap_map_api2result( rs );
1550 if ( LDAP_BACK_QUARANTINE( li ) ) {
1551 ldap_back_quarantine( op, rs );
1554 if ( rs->sr_err != LDAP_SUCCESS &&
1555 ( sendok & LDAP_BACK_SENDERR ) )
1557 if ( op->o_callback == &cb )
1558 op->o_callback = cb.sc_next;
1560 rs->sr_text = "Proxy can't contact remote server";
1561 send_ldap_result( op, rs );
1568 rc = ldap_back_op_result( lc, op, rs, msgid,
1569 -1, ( sendok | LDAP_BACK_BINDING ) );
1570 if ( rc == LDAP_SUCCESS ) {
1571 op->o_conn->c_authz_cookie = op->o_bd->be_private;
1572 LDAP_BACK_CONN_ISBOUND_SET( lc );
1576 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1577 rc = LDAP_BACK_CONN_ISBOUND( lc );
1579 ldap_back_release_conn_lock( li, lcp, dolock );
1581 } else if ( LDAP_BACK_SAVECRED( li ) ) {
1582 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1586 if ( op->o_callback == &cb )
1587 op->o_callback = cb.sc_next;
1596 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1599 ldap_back_dobind( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1601 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1603 return ldap_back_dobind_int( lcp, op, rs,
1604 ( sendok | LDAP_BACK_GETCONN ), li->li_nretries, 1 );
1608 * ldap_back_default_rebind
1610 * This is a callback used for chasing referrals using the same
1611 * credentials as the original user on this session.
1614 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1615 ber_int_t msgid, void *params )
1617 ldapconn_t *lc = (ldapconn_t *)params;
1620 /* ... otherwise we couldn't get here */
1621 assert( lc != NULL );
1623 if ( !ldap_tls_inplace( ld ) ) {
1624 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1626 const char *text = NULL;
1628 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1629 LDAP_BACK_RETRY_DEFAULT, &text );
1630 if ( rc != LDAP_SUCCESS ) {
1634 #endif /* HAVE_TLS */
1636 /* FIXME: add checks on the URL/identity? */
1638 return ldap_sasl_bind_s( ld,
1639 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1640 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1644 * ldap_back_default_urllist
1647 ldap_back_default_urllist(
1649 LDAPURLDesc **urllist,
1653 ldapinfo_t *li = (ldapinfo_t *)params;
1654 LDAPURLDesc **urltail;
1656 if ( urllist == url ) {
1657 return LDAP_SUCCESS;
1660 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1663 *urltail = *urllist;
1667 if ( !li->li_uri_mutex_do_not_lock ) {
1668 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1672 ch_free( li->li_uri );
1675 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1677 if ( !li->li_uri_mutex_do_not_lock ) {
1678 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1681 return LDAP_SUCCESS;
1690 ldap_back_send_t sendok )
1692 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1694 /* default behavior */
1695 if ( LDAP_BACK_ABANDON( li ) ) {
1696 return ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1699 if ( LDAP_BACK_IGNORE( li ) ) {
1700 return ldap_pvt_discard( lc->lc_ld, msgid );
1703 if ( LDAP_BACK_CANCEL( li ) ) {
1704 /* FIXME: asynchronous? */
1705 return ldap_cancel_s( lc->lc_ld, msgid, NULL, NULL );
1714 ldap_back_op_result(
1720 ldap_back_send_t sendok )
1722 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1727 LDAPControl **ctrls = NULL;
1730 rs->sr_matched = NULL;
1732 rs->sr_ctrls = NULL;
1734 /* if the error recorded in the reply corresponds
1735 * to a successful state, get the error from the
1736 * remote server response */
1737 if ( LDAP_ERR_OK( rs->sr_err ) ) {
1740 LDAPMessage *res = NULL;
1741 time_t stoptime = (time_t)(-1);
1742 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
1743 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1744 const char *timeout_text = "Operation timed out";
1746 /* if timeout is not specified, compute and use
1747 * the one specific to the ongoing operation */
1748 if ( timeout == (time_t)(-1) ) {
1749 slap_op_t opidx = slap_req2op( op->o_tag );
1751 if ( opidx == SLAP_OP_SEARCH ) {
1752 if ( op->ors_tlimit <= 0 ) {
1756 timeout = op->ors_tlimit;
1757 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
1758 timeout_text = NULL;
1762 timeout = li->li_timeout[ opidx ];
1766 /* better than nothing :) */
1767 if ( timeout == 0 ) {
1768 if ( li->li_idle_timeout ) {
1769 timeout = li->li_idle_timeout;
1771 } else if ( li->li_conn_ttl ) {
1772 timeout = li->li_conn_ttl;
1777 stoptime = op->o_time + timeout;
1780 LDAP_BACK_TV_SET( &tv );
1783 /* if result parsing fails, note the failure reason */
1784 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1787 if ( timeout && slap_get_time() > stoptime ) {
1788 if ( sendok & LDAP_BACK_BINDING ) {
1789 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1792 /* let it be used, but taint/delete it so that
1793 * no-one else can look it up any further */
1794 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1796 #if LDAP_BACK_PRINT_CONNTREE > 0
1797 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1798 #endif /* LDAP_BACK_PRINT_CONNTREE */
1800 (void)ldap_back_conn_delete( li, lc );
1801 LDAP_BACK_CONN_TAINTED_SET( lc );
1803 #if LDAP_BACK_PRINT_CONNTREE > 0
1804 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1805 #endif /* LDAP_BACK_PRINT_CONNTREE */
1806 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1809 (void)ldap_back_cancel( lc, op, rs, msgid, sendok );
1811 rs->sr_err = timeout_err;
1812 rs->sr_text = timeout_text;
1817 LDAP_BACK_TV_SET( &tv );
1818 ldap_pvt_thread_yield();
1822 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1827 /* otherwise get the result; if it is not
1828 * LDAP_SUCCESS, record it in the reply
1829 * structure (this includes
1830 * LDAP_COMPARE_{TRUE|FALSE}) */
1832 /* only touch when activity actually took place... */
1833 if ( li->li_idle_timeout && lc ) {
1834 lc->lc_time = op->o_time;
1837 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1838 &match, &text, &refs, &ctrls, 1 );
1839 if ( rc == LDAP_SUCCESS ) {
1844 rs->sr_err = slap_map_api2result( rs );
1846 /* RFC 4511: referrals can only appear
1847 * if result code is LDAP_REFERRAL */
1849 && refs[ 0 ] != NULL
1850 && refs[ 0 ][ 0 ] != '\0' )
1852 if ( rs->sr_err != LDAP_REFERRAL ) {
1853 Debug( LDAP_DEBUG_ANY,
1854 "%s ldap_back_op_result: "
1855 "got referrals with err=%d\n",
1862 for ( i = 0; refs[ i ] != NULL; i++ )
1864 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1866 for ( i = 0; refs[ i ] != NULL; i++ ) {
1867 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1869 BER_BVZERO( &rs->sr_ref[ i ] );
1872 } else if ( rs->sr_err == LDAP_REFERRAL ) {
1873 Debug( LDAP_DEBUG_ANY,
1874 "%s ldap_back_op_result: "
1875 "got err=%d with null "
1876 "or empty referrals\n",
1880 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1883 if ( ctrls != NULL ) {
1884 rs->sr_ctrls = ctrls;
1889 /* if the error in the reply structure is not
1890 * LDAP_SUCCESS, try to map it from client
1891 * to server error */
1892 if ( !LDAP_ERR_OK( rs->sr_err ) ) {
1893 rs->sr_err = slap_map_api2result( rs );
1895 /* internal ops ( op->o_conn == NULL )
1896 * must not reply to client */
1897 if ( op->o_conn && !op->o_do_not_cache && match ) {
1899 /* record the (massaged) matched
1900 * DN into the reply structure */
1901 rs->sr_matched = match;
1905 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1906 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1907 if ( LDAP_BACK_QUARANTINE( li ) ) {
1908 ldap_back_quarantine( op, rs );
1910 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1911 if ( rs->sr_text == NULL ) rs->sr_text = "Proxy operation retry failed";
1912 send_ldap_result( op, rs );
1916 } else if ( op->o_conn &&
1917 ( ( ( sendok & LDAP_BACK_SENDOK ) && LDAP_ERR_OK( rs->sr_err ) )
1918 || ( ( sendok & LDAP_BACK_SENDERR ) && !LDAP_ERR_OK( rs->sr_err ) ) ) )
1920 send_ldap_result( op, rs );
1924 ldap_memfree( text );
1928 /* there can't be refs with a (successful) bind */
1930 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1935 ber_memvfree( (void **)refs );
1938 /* match should not be possible with a successful bind */
1940 if ( rs->sr_matched != match ) {
1941 free( (char *)rs->sr_matched );
1943 rs->sr_matched = NULL;
1944 ldap_memfree( match );
1947 if ( ctrls != NULL ) {
1948 if ( op->o_tag == LDAP_REQ_BIND && rs->sr_err == LDAP_SUCCESS ) {
1951 for ( i = 0; ctrls[i] != NULL; i++ );
1953 rs->sr_ctrls = op->o_tmpalloc( sizeof( LDAPControl * )*( i + 1 ),
1955 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
1957 ber_len_t oidlen = strlen( ctrls[i]->ldctl_oid );
1958 ber_len_t size = sizeof( LDAPControl )
1960 + ctrls[i]->ldctl_value.bv_len + 1;
1962 rs->sr_ctrls[ i ] = op->o_tmpalloc( size, op->o_tmpmemctx );
1963 rs->sr_ctrls[ i ]->ldctl_oid = (char *)&rs->sr_ctrls[ i ][ 1 ];
1964 lutil_strcopy( rs->sr_ctrls[ i ]->ldctl_oid, ctrls[i]->ldctl_oid );
1965 rs->sr_ctrls[ i ]->ldctl_value.bv_val
1966 = (char *)&rs->sr_ctrls[ i ]->ldctl_oid[oidlen + 1];
1967 rs->sr_ctrls[ i ]->ldctl_value.bv_len
1968 = ctrls[i]->ldctl_value.bv_len;
1969 ptr = lutil_memcopy( rs->sr_ctrls[ i ]->ldctl_value.bv_val,
1970 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len );
1973 rs->sr_ctrls[ i ] = NULL;
1974 rs->sr_flags |= REP_CTRLS_MUSTBEFREED;
1977 assert( rs->sr_ctrls != NULL );
1978 rs->sr_ctrls = NULL;
1981 ldap_controls_free( ctrls );
1984 return( LDAP_ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1987 /* return true if bound, false if failed */
1989 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1991 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1994 assert( lcp != NULL );
1995 assert( *lcp != NULL );
1997 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1999 if ( (*lcp)->lc_refcnt == 1 ) {
2000 int binding = LDAP_BACK_CONN_BINDING( *lcp );
2002 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
2003 Debug( LDAP_DEBUG_ANY,
2004 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
2005 op->o_log_prefix, li->li_uri,
2006 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
2007 "" : (*lcp)->lc_bound_ndn.bv_val );
2008 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
2010 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
2011 (*lcp)->lc_ld = NULL;
2012 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
2014 /* lc here must be the regular lc, reset and ready for init */
2015 rc = ldap_back_prepare_conn( *lcp, op, rs, sendok );
2016 if ( rc != LDAP_SUCCESS ) {
2017 /* freeit, because lc_refcnt == 1 */
2018 (*lcp)->lc_refcnt = 0;
2019 (void)ldap_back_freeconn( li, *lcp, 0 );
2023 } else if ( ( sendok & LDAP_BACK_BINDING ) ) {
2025 LDAP_BACK_CONN_BINDING_SET( *lcp );
2030 rc = ldap_back_dobind_int( lcp, op, rs, sendok, 0, 0 );
2031 if ( rc == 0 && *lcp != NULL ) {
2032 /* freeit, because lc_refcnt == 1 */
2033 (*lcp)->lc_refcnt = 0;
2034 LDAP_BACK_CONN_TAINTED_SET( *lcp );
2035 (void)ldap_back_freeconn( li, *lcp, 0 );
2041 Debug( LDAP_DEBUG_TRACE,
2042 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
2043 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
2045 LDAP_BACK_CONN_TAINTED_SET( *lcp );
2046 ldap_back_release_conn_lock( li, lcp, 0 );
2047 assert( *lcp == NULL );
2049 if ( sendok & LDAP_BACK_SENDERR ) {
2050 rs->sr_err = LDAP_UNAVAILABLE;
2051 rs->sr_text = "Unable to retry";
2052 send_ldap_result( op, rs );
2056 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
2062 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
2063 struct berval *binddn, struct berval *bindcred )
2065 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2069 if ( op->o_conn == NULL || op->o_do_not_cache ) {
2073 /* don't proxyAuthz if protocol is not LDAPv3 */
2074 switch ( li->li_version ) {
2079 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2085 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2086 if ( sendok & LDAP_BACK_SENDERR ) {
2087 send_ldap_result( op, rs );
2094 *binddn = slap_empty_bv;
2095 *bindcred = slap_empty_bv;
2097 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2098 ndn = op->o_conn->c_ndn;
2104 switch ( li->li_idassert_mode ) {
2105 case LDAP_BACK_IDASSERT_LEGACY:
2106 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
2107 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
2109 *binddn = li->li_idassert_authcDN;
2110 *bindcred = li->li_idassert_passwd;
2117 /* NOTE: rootdn can always idassert */
2118 if ( BER_BVISNULL( &ndn )
2119 && li->li_idassert_authz == NULL
2120 && !( li->li_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL ) )
2122 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2123 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
2124 if ( sendok & LDAP_BACK_SENDERR ) {
2125 send_ldap_result( op, rs );
2130 rs->sr_err = LDAP_SUCCESS;
2131 *binddn = slap_empty_bv;
2132 *bindcred = slap_empty_bv;
2138 } else if ( !be_isroot( op ) ) {
2139 if ( li->li_idassert_passthru ) {
2140 struct berval authcDN;
2142 if ( BER_BVISNULL( &ndn ) ) {
2143 authcDN = slap_empty_bv;
2148 rs->sr_err = slap_sasl_matches( op, li->li_idassert_passthru,
2149 &authcDN, &authcDN );
2150 if ( rs->sr_err == LDAP_SUCCESS ) {
2156 if ( li->li_idassert_authz ) {
2157 struct berval authcDN;
2159 if ( BER_BVISNULL( &ndn ) ) {
2160 authcDN = slap_empty_bv;
2165 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
2166 &authcDN, &authcDN );
2167 if ( rs->sr_err != LDAP_SUCCESS ) {
2168 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2169 if ( sendok & LDAP_BACK_SENDERR ) {
2170 send_ldap_result( op, rs );
2175 rs->sr_err = LDAP_SUCCESS;
2176 *binddn = slap_empty_bv;
2177 *bindcred = slap_empty_bv;
2186 *binddn = li->li_idassert_authcDN;
2187 *bindcred = li->li_idassert_passwd;
2197 ldap_back_proxy_authz_bind(
2201 ldap_back_send_t sendok,
2202 struct berval *binddn,
2203 struct berval *bindcred )
2205 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2210 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2211 ndn = op->o_conn->c_ndn;
2217 if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
2218 #ifdef HAVE_CYRUS_SASL
2219 void *defaults = NULL;
2220 struct berval authzID = BER_BVNULL;
2222 LDAPControl **ctrlsp = NULL;
2223 LDAPMessage *result = NULL;
2224 const char *rmech = NULL;
2225 const char *save_text = rs->sr_text;
2228 LDAPControl ctrl, *ctrls[2];
2230 #endif /* SLAP_AUTH_DN */
2232 /* if SASL supports native authz, prepare for it */
2233 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
2234 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2236 switch ( li->li_idassert_mode ) {
2237 case LDAP_BACK_IDASSERT_OTHERID:
2238 case LDAP_BACK_IDASSERT_OTHERDN:
2239 authzID = li->li_idassert_authzID;
2242 case LDAP_BACK_IDASSERT_ANONYMOUS:
2243 BER_BVSTR( &authzID, "dn:" );
2246 case LDAP_BACK_IDASSERT_SELF:
2247 if ( BER_BVISNULL( &ndn ) ) {
2248 /* connection is not authc'd, so don't idassert */
2249 BER_BVSTR( &authzID, "dn:" );
2252 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
2253 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
2254 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
2255 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
2256 ndn.bv_val, ndn.bv_len + 1 );
2265 if ( li->li_idassert_secprops != NULL ) {
2266 rs->sr_err = ldap_set_option( lc->lc_ld,
2267 LDAP_OPT_X_SASL_SECPROPS,
2268 (void *)li->li_idassert_secprops );
2270 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
2271 rs->sr_err = LDAP_OTHER;
2272 if ( sendok & LDAP_BACK_SENDERR ) {
2273 send_ldap_result( op, rs );
2275 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2280 defaults = lutil_sasl_defaults( lc->lc_ld,
2281 li->li_idassert_sasl_mech.bv_val,
2282 li->li_idassert_sasl_realm.bv_val,
2283 li->li_idassert_authcID.bv_val,
2284 li->li_idassert_passwd.bv_val,
2286 if ( defaults == NULL ) {
2287 rs->sr_err = LDAP_OTHER;
2288 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2289 if ( sendok & LDAP_BACK_SENDERR ) {
2290 send_ldap_result( op, rs );
2296 if ( li->li_idassert_flags & LDAP_BACK_AUTH_DN_AUTHZID ) {
2297 assert( BER_BVISNULL( binddn ) );
2299 ctrl.ldctl_oid = LDAP_CONTROL_AUTHZID_REQUEST;
2300 ctrl.ldctl_iscritical = 0;
2301 BER_BVZERO( &ctrl.ldctl_value );
2306 #endif /* SLAP_AUTH_DN */
2309 rs->sr_err = ldap_sasl_interactive_bind( lc->lc_ld, binddn->bv_val,
2310 li->li_idassert_sasl_mech.bv_val,
2311 ctrlsp, NULL, LDAP_SASL_QUIET, lutil_sasl_interact, defaults,
2312 result, &rmech, &msgid );
2314 if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS )
2317 ldap_msgfree( result );
2319 if ( ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 || !result ) {
2320 ldap_get_option( lc->lc_ld, LDAP_OPT_RESULT_CODE, (void*)&rs->sr_err );
2321 ldap_get_option( lc->lc_ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&rs->sr_text );
2324 } while ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
2326 switch ( rs->sr_err ) {
2329 /* FIXME: right now, the only reason to check
2330 * response controls is RFC 3829 authzid */
2331 if ( li->li_idassert_flags & LDAP_BACK_AUTH_DN_AUTHZID ) {
2333 rc = ldap_parse_result( lc->lc_ld, result, NULL, NULL, NULL, NULL,
2335 if ( rc == LDAP_SUCCESS && ctrlsp ) {
2338 ctrl = ldap_control_find( LDAP_CONTROL_AUTHZID_RESPONSE,
2341 Debug( LDAP_DEBUG_TRACE, "%s: ldap_back_proxy_authz_bind: authzID=\"%s\" (authzid)\n",
2342 op->o_log_prefix, ctrl->ldctl_value.bv_val, 0 );
2343 if ( ctrl->ldctl_value.bv_len > STRLENOF("dn:") &&
2344 strncasecmp( ctrl->ldctl_value.bv_val, "dn:", STRLENOF("dn:") ) == 0 )
2347 bv.bv_val = &ctrl->ldctl_value.bv_val[STRLENOF("dn:")];
2348 bv.bv_len = ctrl->ldctl_value.bv_len - STRLENOF("dn:");
2349 ber_bvreplace( &lc->lc_bound_ndn, &bv );
2354 ldap_controls_free( ctrlsp );
2356 } else if ( li->li_idassert_flags & LDAP_BACK_AUTH_DN_WHOAMI ) {
2357 struct berval *val = NULL;
2358 rc = ldap_whoami_s( lc->lc_ld, &val, NULL, NULL );
2359 if ( rc == LDAP_SUCCESS && val != NULL ) {
2360 Debug( LDAP_DEBUG_TRACE, "%s: ldap_back_proxy_authz_bind: authzID=\"%s\" (whoami)\n",
2361 op->o_log_prefix, val->bv_val, 0 );
2362 if ( val->bv_len > STRLENOF("dn:") &&
2363 strncasecmp( val->bv_val, "dn:", STRLENOF("dn:") ) == 0 )
2366 bv.bv_val = &val->bv_val[STRLENOF("dn:")];
2367 bv.bv_len = val->bv_len - STRLENOF("dn:");
2368 ber_bvreplace( &lc->lc_bound_ndn, &bv );
2374 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_DN_MASK ) &&
2375 BER_BVISNULL( &lc->lc_bound_ndn ) )
2377 /* all in all, we only need it to be non-null */
2378 /* FIXME: should this be configurable? */
2379 static struct berval bv = BER_BVC("cn=authzdn");
2380 ber_bvreplace( &lc->lc_bound_ndn, &bv );
2382 #endif /* SLAP_AUTH_DN */
2383 op->o_conn->c_authz_cookie = op->o_bd->be_private;
2384 LDAP_BACK_CONN_ISBOUND_SET( lc );
2387 case LDAP_LOCAL_ERROR:
2388 /* list client API error codes that require
2389 * to taint the connection */
2390 /* FIXME: should actually retry? */
2391 LDAP_BACK_CONN_TAINTED_SET( lc );
2396 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2397 rs->sr_err = slap_map_api2result( rs );
2398 if ( sendok & LDAP_BACK_SENDERR ) {
2399 send_ldap_result( op, rs );
2404 if ( save_text != rs->sr_text ) {
2405 ldap_memfree( (char *)rs->sr_text );
2406 rs->sr_text = save_text;
2409 ldap_msgfree( result );
2411 lutil_sasl_freedefs( defaults );
2413 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
2417 #endif /* HAVE_CYRUS_SASL */
2420 switch ( li->li_idassert_authmethod ) {
2421 case LDAP_AUTH_NONE:
2422 /* FIXME: do we really need this? */
2423 BER_BVSTR( binddn, "" );
2424 BER_BVSTR( bindcred, "" );
2427 case LDAP_AUTH_SIMPLE:
2428 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
2429 binddn->bv_val, LDAP_SASL_SIMPLE,
2430 bindcred, NULL, NULL, &msgid );
2431 rc = ldap_back_op_result( lc, op, rs, msgid,
2432 -1, ( sendok | LDAP_BACK_BINDING ) );
2437 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2438 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
2439 if ( sendok & LDAP_BACK_SENDERR ) {
2440 send_ldap_result( op, rs );
2445 if ( rc == LDAP_SUCCESS ) {
2446 /* set rebind stuff in case of successful proxyAuthz bind,
2447 * so that referral chasing is attempted using the right
2449 LDAP_BACK_CONN_ISBOUND_SET( lc );
2450 op->o_conn->c_authz_cookie = op->o_bd->be_private;
2451 if ( !BER_BVISNULL( binddn ) ) {
2452 ber_bvreplace( &lc->lc_bound_ndn, binddn );
2455 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
2456 memset( lc->lc_cred.bv_val, 0,
2457 lc->lc_cred.bv_len );
2460 if ( LDAP_BACK_SAVECRED( li ) ) {
2461 if ( !BER_BVISNULL( bindcred ) ) {
2462 ber_bvreplace( &lc->lc_cred, bindcred );
2463 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
2467 lc->lc_cred.bv_len = 0;
2472 return LDAP_BACK_CONN_ISBOUND( lc );
2476 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
2477 * to existing server-side controls if required; if not,
2478 * the existing server-side controls are placed in *pctrls.
2479 * The caller, after using the controls in client API
2480 * operations, if ( *pctrls != op->o_ctrls ), should
2481 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
2482 * The function returns success if the control could
2483 * be added if required, or if it did nothing; in the future,
2484 * it might return some error if it failed.
2486 * if no bind took place yet, but the connection is bound
2487 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
2488 * and explicitly add proxyAuthz the control to every operation
2489 * with the dn bound to the connection as control value.
2491 * If no server-side controls are defined for the operation,
2492 * simply add the proxyAuthz control; otherwise, if the
2493 * proxyAuthz control is not already set, add it as
2496 * FIXME: is controls order significant for security?
2497 * ANSWER: controls ordering and interoperability
2498 * must be indicated by the specs of each control; if none
2499 * is specified, the order is irrelevant.
2502 ldap_back_proxy_authz_ctrl(
2505 struct berval *bound_ndn,
2507 slap_idassert_t *si,
2510 slap_idassert_mode_t mode;
2511 struct berval assertedID,
2515 rs->sr_err = SLAP_CB_CONTINUE;
2517 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
2518 * but if it is not set this test fails. We need a different
2519 * means to detect if idassert is enabled */
2520 if ( ( BER_BVISNULL( &si->si_bc.sb_authcId ) || BER_BVISEMPTY( &si->si_bc.sb_authcId ) )
2521 && ( BER_BVISNULL( &si->si_bc.sb_binddn ) || BER_BVISEMPTY( &si->si_bc.sb_binddn ) )
2522 && BER_BVISNULL( &si->si_bc.sb_saslmech ) )
2527 if ( !op->o_conn || op->o_do_not_cache || ( isroot = be_isroot( op ) ) ) {
2531 if ( op->o_tag == LDAP_REQ_BIND ) {
2532 ndn = op->o_req_ndn;
2534 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2535 ndn = op->o_conn->c_ndn;
2541 if ( si->si_mode == LDAP_BACK_IDASSERT_LEGACY ) {
2542 if ( op->o_proxy_authz ) {
2544 * FIXME: we do not want to perform proxyAuthz
2545 * on behalf of the client, because this would
2546 * be performed with "proxyauthzdn" privileges.
2548 * This might actually be too strict, since
2549 * the "proxyauthzdn" authzTo, and each entry's
2550 * authzFrom attributes may be crafted
2551 * to avoid unwanted proxyAuthz to take place.
2554 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2555 rs->sr_text = "proxyAuthz not allowed within namingContext";
2560 if ( !BER_BVISNULL( bound_ndn ) ) {
2564 if ( BER_BVISNULL( &ndn ) ) {
2568 if ( BER_BVISNULL( &si->si_bc.sb_binddn ) ) {
2572 } else if ( si->si_bc.sb_method == LDAP_AUTH_SASL ) {
2573 if ( ( si->si_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2575 /* already asserted in SASL via native authz */
2579 } else if ( si->si_authz && !isroot ) {
2581 struct berval authcDN;
2583 if ( BER_BVISNULL( &ndn ) ) {
2584 authcDN = slap_empty_bv;
2588 rc = slap_sasl_matches( op, si->si_authz,
2589 &authcDN, &authcDN );
2590 if ( rc != LDAP_SUCCESS ) {
2591 if ( si->si_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2592 /* ndn is not authorized
2593 * to use idassert */
2600 if ( op->o_proxy_authz ) {
2603 * 1) ignore the already set proxyAuthz control
2604 * 2) leave it in place, and don't set ours
2606 * 4) reject the operation
2608 * option (4) is very drastic
2609 * option (3) will make the remote server reject
2610 * the operation, thus being equivalent to (4)
2611 * option (2) will likely break the idassert
2612 * assumptions, so we cannot accept it;
2613 * option (1) means that we are contradicting
2614 * the client's reques.
2616 * I think (4) is the only correct choice.
2618 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2619 rs->sr_text = "proxyAuthz not allowed within namingContext";
2622 if ( op->o_is_auth_check ) {
2623 mode = LDAP_BACK_IDASSERT_NOASSERT;
2630 case LDAP_BACK_IDASSERT_LEGACY:
2631 /* original behavior:
2632 * assert the client's identity */
2633 case LDAP_BACK_IDASSERT_SELF:
2637 case LDAP_BACK_IDASSERT_ANONYMOUS:
2638 /* assert "anonymous" */
2639 assertedID = slap_empty_bv;
2642 case LDAP_BACK_IDASSERT_NOASSERT:
2643 /* don't assert; bind as proxyauthzdn */
2646 case LDAP_BACK_IDASSERT_OTHERID:
2647 case LDAP_BACK_IDASSERT_OTHERDN:
2648 /* assert idassert DN */
2649 assertedID = si->si_bc.sb_authzId;
2656 /* if we got here, "" is allowed to proxyAuthz */
2657 if ( BER_BVISNULL( &assertedID ) ) {
2658 assertedID = slap_empty_bv;
2661 /* don't idassert the bound DN (ITS#4497) */
2662 if ( dn_match( &assertedID, bound_ndn ) ) {
2666 ctrl->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
2667 ctrl->ldctl_iscritical = ( ( si->si_flags & LDAP_BACK_AUTH_PROXYAUTHZ_CRITICAL ) == LDAP_BACK_AUTH_PROXYAUTHZ_CRITICAL );
2669 switch ( si->si_mode ) {
2670 /* already in u:ID or dn:DN form */
2671 case LDAP_BACK_IDASSERT_OTHERID:
2672 case LDAP_BACK_IDASSERT_OTHERDN:
2673 ber_dupbv_x( &ctrl->ldctl_value, &assertedID, op->o_tmpmemctx );
2674 rs->sr_err = LDAP_SUCCESS;
2677 /* needs the dn: prefix */
2679 ctrl->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
2680 ctrl->ldctl_value.bv_val = op->o_tmpalloc( ctrl->ldctl_value.bv_len + 1,
2682 AC_MEMCPY( ctrl->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
2683 AC_MEMCPY( &ctrl->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
2684 assertedID.bv_val, assertedID.bv_len + 1 );
2685 rs->sr_err = LDAP_SUCCESS;
2689 /* Older versions of <draft-weltman-ldapv3-proxy> required
2690 * to encode the value of the authzID (and called it proxyDN);
2691 * this hack provides compatibility with those DSAs that
2692 * implement it this way */
2693 if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
2694 struct berval authzID = ctrl->ldctl_value;
2695 BerElementBuffer berbuf;
2696 BerElement *ber = (BerElement *)&berbuf;
2699 ber_init2( ber, 0, LBER_USE_DER );
2700 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2702 tag = ber_printf( ber, "O", &authzID );
2703 if ( tag == LBER_ERROR ) {
2704 rs->sr_err = LDAP_OTHER;
2708 if ( ber_flatten2( ber, &ctrl->ldctl_value, 1 ) == -1 ) {
2709 rs->sr_err = LDAP_OTHER;
2713 rs->sr_err = LDAP_SUCCESS;
2716 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2717 ber_free_buf( ber );
2719 if ( rs->sr_err != LDAP_SUCCESS ) {
2723 } else if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
2724 struct berval authzID = ctrl->ldctl_value,
2726 BerElementBuffer berbuf;
2727 BerElement *ber = (BerElement *)&berbuf;
2730 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
2731 rs->sr_err = LDAP_PROTOCOL_ERROR;
2736 tmp.bv_val += STRLENOF( "dn:" );
2737 tmp.bv_len -= STRLENOF( "dn:" );
2739 ber_init2( ber, 0, LBER_USE_DER );
2740 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2742 /* apparently, Mozilla API encodes this
2743 * as "SEQUENCE { LDAPDN }" */
2744 tag = ber_printf( ber, "{O}", &tmp );
2745 if ( tag == LBER_ERROR ) {
2746 rs->sr_err = LDAP_OTHER;
2750 if ( ber_flatten2( ber, &ctrl->ldctl_value, 1 ) == -1 ) {
2751 rs->sr_err = LDAP_OTHER;
2755 ctrl->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
2756 rs->sr_err = LDAP_SUCCESS;
2759 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2760 ber_free_buf( ber );
2762 if ( rs->sr_err != LDAP_SUCCESS ) {
2775 * if any needs to be added, it is prepended to existing ones,
2776 * in a newly allocated array. The companion function
2777 * ldap_back_controls_free() must be used to restore the original
2778 * status of op->o_ctrls.
2781 ldap_back_controls_add(
2785 LDAPControl ***pctrls )
2787 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2789 LDAPControl **ctrls = NULL;
2790 /* set to the maximum number of controls this backend can add */
2791 LDAPControl c[ 2 ] = { { 0 } };
2792 int n = 0, i, j1 = 0, j2 = 0;
2796 rs->sr_err = LDAP_SUCCESS;
2798 /* don't add controls if protocol is not LDAPv3 */
2799 switch ( li->li_version ) {
2804 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2813 /* put controls that go __before__ existing ones here */
2815 /* proxyAuthz for identity assertion */
2816 switch ( ldap_back_proxy_authz_ctrl( op, rs, &lc->lc_bound_ndn,
2817 li->li_version, &li->li_idassert, &c[ j1 ] ) )
2819 case SLAP_CB_CONTINUE:
2830 /* put controls that go __after__ existing ones here */
2832 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
2833 /* FIXME: according to <draft-wahl-ldap-session>,
2834 * the server should check if the control can be added
2835 * based on the identity of the client and so */
2837 /* session tracking */
2838 if ( LDAP_BACK_ST_REQUEST( li ) ) {
2839 switch ( slap_ctrl_session_tracking_request_add( op, rs, &c[ j1 + j2 ] ) ) {
2840 case SLAP_CB_CONTINUE:
2851 #endif /* SLAP_CONTROL_X_SESSION_TRACKING */
2853 if ( rs->sr_err == SLAP_CB_CONTINUE ) {
2854 rs->sr_err = LDAP_SUCCESS;
2857 /* if nothing to do, just bail out */
2858 if ( j1 == 0 && j2 == 0 ) {
2862 assert( j1 + j2 <= (int) (sizeof( c )/sizeof( c[0] )) );
2864 if ( op->o_ctrls ) {
2865 for ( n = 0; op->o_ctrls[ n ]; n++ )
2866 /* just count ctrls */ ;
2869 ctrls = op->o_tmpalloc( (n + j1 + j2 + 1) * sizeof( LDAPControl * ) + ( j1 + j2 ) * sizeof( LDAPControl ),
2872 ctrls[ 0 ] = (LDAPControl *)&ctrls[ n + j1 + j2 + 1 ];
2873 *ctrls[ 0 ] = c[ 0 ];
2874 for ( i = 1; i < j1; i++ ) {
2875 ctrls[ i ] = &ctrls[ 0 ][ i ];
2876 *ctrls[ i ] = c[ i ];
2881 if ( op->o_ctrls ) {
2882 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
2883 ctrls[ i + j1 ] = op->o_ctrls[ i ];
2889 ctrls[ n ] = (LDAPControl *)&ctrls[ n + j2 + 1 ] + j1;
2890 *ctrls[ n ] = c[ j1 ];
2891 for ( i = 1; i < j2; i++ ) {
2892 ctrls[ n + i ] = &ctrls[ n ][ i ];
2893 *ctrls[ n + i ] = c[ i ];
2897 ctrls[ n + j2 ] = NULL;
2900 if ( ctrls == NULL ) {
2901 ctrls = op->o_ctrls;
2910 ldap_back_controls_free( Operation *op, SlapReply *rs, LDAPControl ***pctrls )
2912 LDAPControl **ctrls = *pctrls;
2914 /* we assume that the controls added by the proxy come first,
2915 * so as soon as we find op->o_ctrls[ 0 ] we can stop */
2916 if ( ctrls && ctrls != op->o_ctrls ) {
2917 int i = 0, n = 0, n_added;
2918 LDAPControl *lower, *upper;
2920 assert( ctrls[ 0 ] != NULL );
2922 for ( n = 0; ctrls[ n ] != NULL; n++ )
2925 if ( op->o_ctrls ) {
2926 for ( i = 0; op->o_ctrls[ i ] != NULL; i++ )
2931 lower = (LDAPControl *)&ctrls[ n ];
2932 upper = &lower[ n_added ];
2934 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
2935 if ( ctrls[ i ] < lower || ctrls[ i ] >= upper ) {
2936 /* original; don't touch */
2940 if ( !BER_BVISNULL( &ctrls[ i ]->ldctl_value ) ) {
2941 op->o_tmpfree( ctrls[ i ]->ldctl_value.bv_val, op->o_tmpmemctx );
2945 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2954 ldap_back_conn2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen )
2956 char tbuf[ SLAP_TEXT_BUFLEN ];
2957 char *ptr = buf, *end = buf + buflen;
2960 if ( ptr + sizeof("conn=") > end ) return -1;
2961 ptr = lutil_strcopy( ptr, "conn=" );
2963 len = ldap_back_connid2str( lc, ptr, (ber_len_t)(end - ptr) );
2965 if ( ptr >= end ) return -1;
2967 if ( !BER_BVISNULL( &lc->lcb_local_ndn ) ) {
2968 if ( ptr + sizeof(" DN=\"\"") + lc->lcb_local_ndn.bv_len > end ) return -1;
2969 ptr = lutil_strcopy( ptr, " DN=\"" );
2970 ptr = lutil_strncopy( ptr, lc->lcb_local_ndn.bv_val, lc->lcb_local_ndn.bv_len );
2974 if ( lc->lcb_create_time != 0 ) {
2975 len = snprintf( tbuf, sizeof(tbuf), "%ld", lc->lcb_create_time );
2976 if ( ptr + sizeof(" created=") + len >= end ) return -1;
2977 ptr = lutil_strcopy( ptr, " created=" );
2978 ptr = lutil_strcopy( ptr, tbuf );
2981 if ( lc->lcb_time != 0 ) {
2982 len = snprintf( tbuf, sizeof(tbuf), "%ld", lc->lcb_time );
2983 if ( ptr + sizeof(" modified=") + len >= end ) return -1;
2984 ptr = lutil_strcopy( ptr, " modified=" );
2985 ptr = lutil_strcopy( ptr, tbuf );
2988 len = snprintf( tbuf, sizeof(tbuf), "%u", lc->lcb_refcnt );
2989 if ( ptr + sizeof(" refcnt=") + len >= end ) return -1;
2990 ptr = lutil_strcopy( ptr, " refcnt=" );
2991 ptr = lutil_strcopy( ptr, tbuf );
2997 ldap_back_connid2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen )
2999 static struct berval conns[] = {
3001 BER_BVC("ROOTDN-TLS"),
3003 BER_BVC("ANON-TLS"),
3005 BER_BVC("BIND-TLS"),
3011 if ( LDAP_BACK_PCONN_ISPRIV( (const ldapconn_t *)lc ) ) {
3015 cid = (long)lc->lcb_conn;
3016 assert( cid >= LDAP_BACK_PCONN_FIRST && cid < LDAP_BACK_PCONN_LAST );
3020 if ( bv->bv_len >= buflen ) {
3021 return bv->bv_len + 1;
3025 lutil_strncopy( buf, bv->bv_val, bv->bv_len + 1 );
3028 len = snprintf( buf, buflen, "%lu", lc->lcb_conn->c_connid );