1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2010 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 #undef ldap_debug /* silence a warning in ldap-int.h */
37 #include "../../../libraries/libldap/ldap-int.h"
39 #include "lutil_ldap.h"
41 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
43 #if LDAP_BACK_PRINT_CONNTREE > 0
49 { LDAP_BACK_FCONN_ISBOUND, 'B' },
50 { LDAP_BACK_FCONN_ISANON, 'A' },
51 { LDAP_BACK_FCONN_ISPRIV, 'P' },
52 { LDAP_BACK_FCONN_ISTLS, 'T' },
53 { LDAP_BACK_FCONN_BINDING, 'X' },
54 { LDAP_BACK_FCONN_TAINTED, 'E' },
55 { LDAP_BACK_FCONN_ABANDON, 'N' },
56 { LDAP_BACK_FCONN_ISIDASR, 'S' },
57 { LDAP_BACK_FCONN_CACHED, 'C' },
62 ldap_back_conn_print( ldapconn_t *lc, const char *avlstr )
64 char buf[ SLAP_TEXT_BUFLEN ];
65 char fbuf[ sizeof("BAPTIENSC") ];
68 ldap_back_conn2str( &lc->lc_base, buf, sizeof( buf ) );
69 for ( i = 0; flagsmap[ i ].c != '\0'; i++ ) {
70 if ( lc->lc_lcflags & flagsmap[i].f ) {
71 fbuf[i] = flagsmap[i].c;
79 fprintf( stderr, "lc=%p %s %s flags=0x%08x (%s)\n",
80 (void *)lc, buf, avlstr, lc->lc_lcflags, fbuf );
84 ldap_back_ravl_print( Avlnode *root, int depth )
93 ldap_back_ravl_print( root->avl_right, depth+1 );
95 for ( i = 0; i < depth; i++ ) {
96 fprintf( stderr, "-" );
100 ldap_back_conn_print( lc, avl_bf2str( root->avl_bf ) );
102 ldap_back_ravl_print( root->avl_left, depth + 1 );
105 static char* priv2str[] = {
116 ldap_back_print_conntree( ldapinfo_t *li, char *msg )
120 fprintf( stderr, "========> %s\n", msg );
122 for ( c = LDAP_BACK_PCONN_FIRST; c < LDAP_BACK_PCONN_LAST; c++ ) {
126 fprintf( stderr, " %s[%d]\n", priv2str[ c ], li->li_conn_priv[ c ].lic_num );
128 LDAP_TAILQ_FOREACH( lc, &li->li_conn_priv[ c ].lic_priv, lc_q )
130 fprintf( stderr, " [%d] ", i );
131 ldap_back_conn_print( lc, "" );
136 if ( li->li_conninfo.lai_tree == 0 ) {
137 fprintf( stderr, "\t(empty)\n" );
140 ldap_back_ravl_print( li->li_conninfo.lai_tree, 0 );
143 fprintf( stderr, "<======== %s\n", msg );
145 #endif /* LDAP_BACK_PRINT_CONNTREE */
148 ldap_back_freeconn( ldapinfo_t *li, ldapconn_t *lc, int dolock );
151 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
152 struct berval *binddn, struct berval *bindcred );
155 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
156 struct berval *binddn, struct berval *bindcred );
159 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs,
160 ldap_back_send_t sendok, struct berval *binddn, struct berval *bindcred );
163 ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs,
164 ldap_back_send_t sendok );
167 ldap_back_conndnlc_cmp( const void *c1, const void *c2 );
170 ldap_back_conn_delete( ldapinfo_t *li, ldapconn_t *lc )
172 if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) {
173 if ( LDAP_BACK_CONN_CACHED( lc ) ) {
174 assert( lc->lc_q.tqe_prev != NULL );
175 assert( li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num > 0 );
176 li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num--;
177 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv, lc, lc_q );
178 LDAP_TAILQ_ENTRY_INIT( lc, lc_q );
179 LDAP_BACK_CONN_CACHED_CLEAR( lc );
182 assert( LDAP_BACK_CONN_TAINTED( lc ) );
183 assert( lc->lc_q.tqe_prev == NULL );
187 ldapconn_t *tmplc = NULL;
189 if ( LDAP_BACK_CONN_CACHED( lc ) ) {
190 assert( !LDAP_BACK_CONN_TAINTED( lc ) );
191 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
192 ldap_back_conndnlc_cmp );
193 assert( tmplc == lc );
194 LDAP_BACK_CONN_CACHED_CLEAR( lc );
197 assert( LDAP_BACK_CONN_TAINTED( lc ) || tmplc == lc );
204 ldap_back_bind( Operation *op, SlapReply *rs )
206 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
209 LDAPControl **ctrls = NULL;
210 struct berval save_o_dn;
211 int save_o_do_not_cache,
214 ldap_back_send_t retrying = LDAP_BACK_RETRYING;
216 /* allow rootdn as a means to auth without the need to actually
217 * contact the proxied DSA */
218 switch ( be_rootdn_bind( op, rs ) ) {
219 case SLAP_CB_CONTINUE:
226 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR, NULL, NULL );
231 /* we can do (almost) whatever we want with this conn,
232 * because either it's temporary, or it's marked as binding */
233 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
234 ch_free( lc->lc_bound_ndn.bv_val );
235 BER_BVZERO( &lc->lc_bound_ndn );
237 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
238 memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
239 ch_free( lc->lc_cred.bv_val );
240 BER_BVZERO( &lc->lc_cred );
242 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
244 /* don't add proxyAuthz; set the bindDN */
245 save_o_dn = op->o_dn;
246 save_o_do_not_cache = op->o_do_not_cache;
247 op->o_dn = op->o_req_dn;
248 op->o_do_not_cache = 1;
251 rc = ldap_back_controls_add( op, rs, lc, &ctrls );
252 op->o_dn = save_o_dn;
253 op->o_do_not_cache = save_o_do_not_cache;
254 if ( rc != LDAP_SUCCESS ) {
255 send_ldap_result( op, rs );
256 ldap_back_release_conn( li, lc );
261 /* method is always LDAP_AUTH_SIMPLE if we got here */
262 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
264 &op->orb_cred, ctrls, NULL, &msgid );
265 /* FIXME: should we always retry, or only when piping the bind
266 * in the "override" connection pool? */
267 rc = ldap_back_op_result( lc, op, rs, msgid,
268 li->li_timeout[ SLAP_OP_BIND ],
269 LDAP_BACK_BIND_SERR | retrying );
270 if ( rc == LDAP_UNAVAILABLE && retrying ) {
271 retrying &= ~LDAP_BACK_RETRYING;
272 if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_BIND_SERR ) ) {
277 ldap_back_controls_free( op, rs, &ctrls );
279 if ( rc == LDAP_SUCCESS ) {
280 op->o_conn->c_authz_cookie = op->o_bd->be_private;
282 /* If defined, proxyAuthz will be used also when
283 * back-ldap is the authorizing backend; for this
284 * purpose, after a successful bind the connection
285 * is left for further binds, and further operations
286 * on this client connection will use a default
287 * connection with identity assertion */
288 /* NOTE: use with care */
289 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
290 ldap_back_release_conn( li, lc );
294 /* rebind is now done inside ldap_back_proxy_authz_bind()
295 * in case of success */
296 LDAP_BACK_CONN_ISBOUND_SET( lc );
297 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
299 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
300 memset( lc->lc_cred.bv_val, 0,
301 lc->lc_cred.bv_len );
304 if ( LDAP_BACK_SAVECRED( li ) ) {
305 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
306 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
309 lc->lc_cred.bv_len = 0;
313 /* must re-insert if local DN changed as result of bind */
314 if ( !LDAP_BACK_CONN_ISBOUND( lc )
315 || ( !dn_match( &op->o_req_ndn, &lc->lc_local_ndn )
316 && !LDAP_BACK_PCONN_ISPRIV( lc ) ) )
321 /* wait for all other ops to release the connection */
323 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
324 if ( lc->lc_refcnt > 1 ) {
325 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
326 ldap_pvt_thread_yield();
330 #if LDAP_BACK_PRINT_CONNTREE > 0
331 ldap_back_print_conntree( li, ">>> ldap_back_bind" );
332 #endif /* LDAP_BACK_PRINT_CONNTREE */
334 assert( lc->lc_refcnt == 1 );
335 ldap_back_conn_delete( li, lc );
337 /* delete all cached connections with the current connection */
338 if ( LDAP_BACK_SINGLECONN( li ) ) {
339 while ( ( tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc, ldap_back_conn_cmp ) ) != NULL )
341 assert( !LDAP_BACK_PCONN_ISPRIV( lc ) );
342 Debug( LDAP_DEBUG_TRACE,
343 "=>ldap_back_bind: destroying conn %lu (refcnt=%u)\n",
344 lc->lc_conn->c_connid, lc->lc_refcnt, 0 );
346 if ( tmplc->lc_refcnt != 0 ) {
348 LDAP_BACK_CONN_TAINTED_SET( tmplc );
349 LDAP_BACK_CONN_CACHED_CLEAR( tmplc );
353 * Needs a test because the handler may be corrupted,
354 * and calling ldap_unbind on a corrupted header results
355 * in a segmentation fault
357 ldap_back_conn_free( tmplc );
362 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
363 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
364 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
365 LDAP_BACK_PCONN_ROOTDN_SET( lc, op );
367 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
368 ldap_back_conndn_cmp, ldap_back_conndn_dup );
371 #if LDAP_BACK_PRINT_CONNTREE > 0
372 ldap_back_print_conntree( li, "<<< ldap_back_bind" );
373 #endif /* LDAP_BACK_PRINT_CONNTREE */
375 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
378 LDAP_BACK_CONN_CACHED_SET( lc );
382 /* duplicate; someone else successfully bound
383 * on the same connection with the same identity;
384 * we can do this because lc_refcnt == 1 */
385 ldap_back_conn_free( lc );
391 ldap_back_release_conn( li, lc );
398 * ldap_back_conndn_cmp
400 * compares two ldapconn_t based on the value of the conn pointer
401 * and of the local DN; used by avl stuff for insert, lookup
405 ldap_back_conndn_cmp( const void *c1, const void *c2 )
407 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
408 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
411 /* If local DNs don't match, it is definitely not a match */
412 /* For shared sessions, conn is NULL. Only explicitly
413 * bound sessions will have non-NULL conn.
415 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
417 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
424 * ldap_back_conndnlc_cmp
426 * compares two ldapconn_t based on the value of the conn pointer,
427 * the local DN and the lc pointer; used by avl stuff for insert, lookup
431 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
433 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
434 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
437 /* If local DNs don't match, it is definitely not a match */
438 /* For shared sessions, conn is NULL. Only explicitly
439 * bound sessions will have non-NULL conn.
441 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
443 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
445 rc = SLAP_PTRCMP( lc1, lc2 );
455 * compares two ldapconn_t based on the value of the conn pointer;
456 * used by avl stuff for delete of all conns with the same connid
459 ldap_back_conn_cmp( const void *c1, const void *c2 )
461 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
462 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
464 /* For shared sessions, conn is NULL. Only explicitly
465 * bound sessions will have non-NULL conn.
467 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
471 * ldap_back_conndn_dup
473 * returns -1 in case a duplicate ldapconn_t has been inserted;
477 ldap_back_conndn_dup( void *c1, void *c2 )
479 ldapconn_t *lc1 = (ldapconn_t *)c1;
480 ldapconn_t *lc2 = (ldapconn_t *)c2;
482 /* Cannot have more than one shared session with same DN */
483 if ( lc1->lc_conn == lc2->lc_conn &&
484 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
493 ldap_back_freeconn( ldapinfo_t *li, ldapconn_t *lc, int dolock )
496 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
499 #if LDAP_BACK_PRINT_CONNTREE > 0
500 ldap_back_print_conntree( li, ">>> ldap_back_freeconn" );
501 #endif /* LDAP_BACK_PRINT_CONNTREE */
503 (void)ldap_back_conn_delete( li, lc );
505 if ( lc->lc_refcnt == 0 ) {
506 ldap_back_conn_free( (void *)lc );
509 #if LDAP_BACK_PRINT_CONNTREE > 0
510 ldap_back_print_conntree( li, "<<< ldap_back_freeconn" );
511 #endif /* LDAP_BACK_PRINT_CONNTREE */
514 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
531 int rc = LDAP_SUCCESS;
533 /* start TLS ("tls-[try-]{start,propagate}" statements) */
534 if ( ( LDAP_BACK_USE_TLS_F( flags ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS_F( flags ) ) )
535 && !ldap_is_ldaps_url( url ) )
537 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
539 * use asynchronous StartTLS
540 * in case, chase referral (not implemented yet)
544 if ( protocol == 0 ) {
545 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
549 if ( protocol < LDAP_VERSION3 ) {
550 /* we should rather bail out... */
551 rc = LDAP_UNWILLING_TO_PERFORM;
552 *text = "invalid protocol version";
555 if ( rc == LDAP_SUCCESS ) {
556 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
559 if ( rc == LDAP_SUCCESS ) {
560 LDAPMessage *res = NULL;
563 LDAP_BACK_TV_SET( &tv );
566 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
568 rc = LDAP_UNAVAILABLE;
570 } else if ( rc == 0 ) {
571 if ( retries != LDAP_BACK_RETRY_NEVER ) {
572 ldap_pvt_thread_yield();
576 LDAP_BACK_TV_SET( &tv );
579 rc = LDAP_UNAVAILABLE;
581 } else if ( rc == LDAP_RES_EXTENDED ) {
582 struct berval *data = NULL;
584 rc = ldap_parse_extended_result( ld, res,
586 if ( rc == LDAP_SUCCESS ) {
588 rc = ldap_parse_result( ld, res, &err,
589 NULL, NULL, NULL, NULL, 1 );
590 if ( rc == LDAP_SUCCESS ) {
595 /* FIXME: in case a referral
596 * is returned, should we try
597 * using it instead of the
599 if ( rc == LDAP_SUCCESS ) {
600 rc = ldap_install_tls( ld );
602 } else if ( rc == LDAP_REFERRAL ) {
603 rc = LDAP_UNWILLING_TO_PERFORM;
604 *text = "unwilling to chase referral returned by Start TLS exop";
608 if ( data->bv_val ) {
609 ber_memfree( data->bv_val );
623 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
625 * use synchronous StartTLS
627 rc = ldap_start_tls_s( ld, NULL, NULL );
628 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
630 /* if StartTLS is requested, only attempt it if the URL
631 * is not "ldaps://"; this may occur not only in case
632 * of misconfiguration, but also when used in the chain
633 * overlay, where the "uri" can be parsed out of a referral */
639 case LDAP_SERVER_DOWN:
643 if ( LDAP_BACK_TLS_CRITICAL_F( flags ) ) {
644 *text = "could not start TLS";
648 /* in case Start TLS is not critical */
660 #endif /* HAVE_TLS */
663 ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
665 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
669 int is_tls = op->o_conn->c_is_tls;
670 time_t lctime = (time_t)(-1);
672 #endif /* HAVE_TLS */
674 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
675 rs->sr_err = ldap_initialize( &ld, li->li_uri );
676 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
677 if ( rs->sr_err != LDAP_SUCCESS ) {
681 if ( li->li_urllist_f ) {
682 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
685 /* Set LDAP version. This will always succeed: If the client
686 * bound with a particular version, then so can we.
688 if ( li->li_version != 0 ) {
689 version = li->li_version;
691 } else if ( op->o_protocol != 0 ) {
692 version = op->o_protocol;
695 /* assume it's an internal op; set to LDAPv3 */
696 version = LDAP_VERSION3;
698 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
700 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
701 ldap_set_option( ld, LDAP_OPT_REFERRALS,
702 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
704 if ( li->li_network_timeout > 0 ) {
707 tv.tv_sec = li->li_network_timeout;
709 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
713 if ( LDAP_BACK_CONN_ISPRIV( lc ) ) {
716 } else if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
717 sb = &li->li_idassert.si_bc;
723 if ( sb->sb_tls_do_init ) {
724 bindconf_tls_set( sb, ld );
725 } else if ( sb->sb_tls_ctx ) {
726 ldap_set_option( ld, LDAP_OPT_X_TLS_CTX, sb->sb_tls_ctx );
729 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
730 assert( li->li_uri_mutex_do_not_lock == 0 );
731 li->li_uri_mutex_do_not_lock = 1;
732 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
733 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
734 li->li_uri_mutex_do_not_lock = 0;
735 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
736 if ( rs->sr_err != LDAP_SUCCESS ) {
737 ldap_unbind_ext( ld, NULL, NULL );
738 rs->sr_text = "Start TLS failed";
741 } else if ( li->li_idle_timeout ) {
742 /* only touch when activity actually took place... */
745 #endif /* HAVE_TLS */
751 LDAP_BACK_CONN_ISTLS_SET( lc );
753 LDAP_BACK_CONN_ISTLS_CLEAR( lc );
755 if ( lctime != (time_t)(-1) ) {
756 lc->lc_time = lctime;
758 #endif /* HAVE_TLS */
761 if ( rs->sr_err != LDAP_SUCCESS ) {
762 rs->sr_err = slap_map_api2result( rs );
763 if ( sendok & LDAP_BACK_SENDERR ) {
764 if ( rs->sr_text == NULL ) {
765 rs->sr_text = "Proxy connection initialization failed";
767 send_ldap_result( op, rs );
771 if ( li->li_conn_ttl > 0 ) {
772 lc->lc_create_time = op->o_time;
783 ldap_back_send_t sendok,
784 struct berval *binddn,
785 struct berval *bindcred )
787 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
788 ldapconn_t *lc = NULL,
791 lookupconn = !( sendok & LDAP_BACK_BINDING );
793 /* if the server is quarantined, and
794 * - the current interval did not expire yet, or
795 * - no more retries should occur,
796 * don't return the connection */
797 if ( li->li_isquarantined ) {
798 slap_retry_info_t *ri = &li->li_quarantine;
801 if ( li->li_quarantine.ri_interval ) {
802 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
803 if ( li->li_isquarantined == LDAP_BACK_FQ_YES ) {
804 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
805 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
807 Debug( LDAP_DEBUG_ANY,
808 "%s: ldap_back_getconn quarantine "
809 "retry block #%d try #%d.\n",
810 op->o_log_prefix, ri->ri_idx, ri->ri_count );
811 li->li_isquarantined = LDAP_BACK_FQ_RETRYING;
814 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
818 rs->sr_err = LDAP_UNAVAILABLE;
819 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
820 rs->sr_text = "Target is quarantined";
821 send_ldap_result( op, rs );
827 /* Internal searches are privileged and shared. So is root. */
828 if ( op->o_do_not_cache || be_isroot( op ) ) {
829 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
830 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
831 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
834 struct berval tmpbinddn,
841 if ( binddn == NULL ) {
844 if ( bindcred == NULL ) {
845 bindcred = &tmpbindcred;
847 if ( op->o_tag == LDAP_REQ_BIND ) {
848 save_o_dn = op->o_dn;
849 save_o_ndn = op->o_ndn;
850 op->o_dn = op->o_req_dn;
851 op->o_ndn = op->o_req_ndn;
853 isproxyauthz = ldap_back_is_proxy_authz( op, rs, sendok, binddn, bindcred );
854 if ( op->o_tag == LDAP_REQ_BIND ) {
855 op->o_dn = save_o_dn;
856 op->o_ndn = save_o_ndn;
858 if ( isproxyauthz == -1 ) {
862 lc_curr.lc_local_ndn = op->o_ndn;
863 /* Explicit binds must not be shared;
864 * however, explicit binds are piped in a special connection
865 * when idassert is to occur with "override" set */
866 if ( op->o_tag == LDAP_REQ_BIND && !isproxyauthz ) {
867 lc_curr.lc_conn = op->o_conn;
870 if ( isproxyauthz && !( sendok & LDAP_BACK_BINDING ) ) {
871 lc_curr.lc_local_ndn = *binddn;
872 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
873 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
875 } else if ( isproxyauthz && ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) {
876 lc_curr.lc_local_ndn = slap_empty_bv;
877 LDAP_BACK_PCONN_BIND_SET( &lc_curr, op );
878 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
881 } else if ( SLAP_IS_AUTHZ_BACKEND( op ) ) {
882 lc_curr.lc_conn = op->o_conn;
885 LDAP_BACK_PCONN_ANON_SET( &lc_curr, op );
890 /* Explicit Bind requests always get their own conn */
893 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
894 if ( LDAP_BACK_PCONN_ISPRIV( &lc_curr ) ) {
895 /* lookup a conn that's not binding */
896 LDAP_TAILQ_FOREACH( lc,
897 &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv,
900 if ( !LDAP_BACK_CONN_BINDING( lc ) && lc->lc_refcnt == 0 ) {
906 if ( lc != LDAP_TAILQ_LAST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
909 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
911 LDAP_TAILQ_ENTRY_INIT( lc, lc_q );
912 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
916 } else if ( !LDAP_BACK_USE_TEMPORARIES( li )
917 && li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_num == li->li_conn_priv_max )
919 lc = LDAP_TAILQ_FIRST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv );
924 /* Searches for a ldapconn in the avl tree */
925 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
926 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
930 /* Don't reuse connections while they're still binding */
931 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
932 if ( !LDAP_BACK_USE_TEMPORARIES( li ) ) {
933 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
935 ldap_pvt_thread_yield();
942 if ( op->o_tag == LDAP_REQ_BIND ) {
943 /* right now, this is the only possible case */
944 assert( ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) );
945 LDAP_BACK_CONN_BINDING_SET( lc );
948 refcnt = ++lc->lc_refcnt;
951 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
954 /* Looks like we didn't get a bind. Open a new session... */
956 lc = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
957 lc->lc_flags = li->li_flags;
958 lc->lc_lcflags = lc_curr.lc_lcflags;
959 if ( ldap_back_prepare_conn( lc, op, rs, sendok ) != LDAP_SUCCESS ) {
964 if ( sendok & LDAP_BACK_BINDING ) {
965 LDAP_BACK_CONN_BINDING_SET( lc );
968 lc->lc_conn = lc_curr.lc_conn;
969 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
972 * the rationale is: connections as the rootdn are privileged,
973 * so acl_authcDN is to be used; however, in some cases
974 * one already configured identity assertion with a highly
975 * privileged idassert_authcDN, so if acl_authcDN is NULL
976 * and idassert_authcDN is not, use the second instead.
978 * might change in the future, because it's preferable
979 * to make clear what identity is being used, since
980 * the only drawback is that one risks to configure
981 * the same identity twice...
983 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
984 if ( BER_BVISNULL( &li->li_acl_authcDN ) && !BER_BVISNULL( &li->li_idassert_authcDN ) ) {
985 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
986 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
989 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
990 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
992 LDAP_BACK_CONN_ISPRIV_SET( lc );
994 } else if ( LDAP_BACK_CONN_ISIDASSERT( &lc_curr ) ) {
995 if ( !LDAP_BACK_PCONN_ISBIND( &lc_curr ) ) {
996 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
997 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
999 LDAP_BACK_CONN_ISIDASSERT_SET( lc );
1002 BER_BVZERO( &lc->lc_cred );
1003 BER_BVZERO( &lc->lc_bound_ndn );
1004 if ( !BER_BVISEMPTY( &op->o_ndn )
1005 && SLAP_IS_AUTHZ_BACKEND( op ) )
1007 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
1012 /* if start TLS failed but it was not mandatory,
1013 * check if the non-TLS connection was already
1014 * in cache; in case, destroy the newly created
1015 * connection and use the existing one */
1016 if ( LDAP_BACK_PCONN_ISTLS( lc )
1017 && !ldap_tls_inplace( lc->lc_ld ) )
1019 ldapconn_t *tmplc = NULL;
1020 int idx = LDAP_BACK_CONN2PRIV( &lc_curr ) - 1;
1022 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1023 LDAP_TAILQ_FOREACH( tmplc,
1024 &li->li_conn_priv[ idx ].lic_priv,
1027 if ( !LDAP_BACK_CONN_BINDING( tmplc ) ) {
1032 if ( tmplc != NULL ) {
1033 refcnt = ++tmplc->lc_refcnt;
1034 ldap_back_conn_free( lc );
1037 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1039 if ( tmplc != NULL ) {
1043 #endif /* HAVE_TLS */
1045 /* Inserts the newly created ldapconn in the avl tree */
1046 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1048 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1050 assert( lc->lc_refcnt == 1 );
1052 #if LDAP_BACK_PRINT_CONNTREE > 0
1053 ldap_back_print_conntree( li, ">>> ldap_back_getconn(insert)" );
1054 #endif /* LDAP_BACK_PRINT_CONNTREE */
1056 if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1057 if ( li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num < li->li_conn_priv_max ) {
1058 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv, lc, lc_q );
1059 li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num++;
1060 LDAP_BACK_CONN_CACHED_SET( lc );
1063 LDAP_BACK_CONN_TAINTED_SET( lc );
1068 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
1069 ldap_back_conndn_cmp, ldap_back_conndn_dup );
1070 LDAP_BACK_CONN_CACHED_SET( lc );
1073 #if LDAP_BACK_PRINT_CONNTREE > 0
1074 ldap_back_print_conntree( li, "<<< ldap_back_getconn(insert)" );
1075 #endif /* LDAP_BACK_PRINT_CONNTREE */
1077 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1079 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1080 char buf[ SLAP_TEXT_BUFLEN ];
1082 snprintf( buf, sizeof( buf ),
1083 "lc=%p inserted refcnt=%u rc=%d",
1084 (void *)lc, refcnt, rs->sr_err );
1086 Debug( LDAP_DEBUG_TRACE,
1087 "=>ldap_back_getconn: %s: %s\n",
1088 op->o_log_prefix, buf, 0 );
1091 if ( !LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1092 /* Err could be -1 in case a duplicate ldapconn is inserted */
1093 switch ( rs->sr_err ) {
1098 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1099 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( li ) ) {
1100 /* duplicate: free and try to get the newly created one */
1101 ldap_back_conn_free( lc );
1106 /* taint connection, so that it'll be freed when released */
1107 LDAP_BACK_CONN_TAINTED_SET( lc );
1111 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1112 ldap_back_conn_free( lc );
1113 rs->sr_err = LDAP_OTHER;
1114 rs->sr_text = "Proxy bind collision";
1115 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1116 send_ldap_result( op, rs );
1125 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
1126 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
1130 /* let it be used, but taint/delete it so that
1131 * no-one else can look it up any further */
1132 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1134 #if LDAP_BACK_PRINT_CONNTREE > 0
1135 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1136 #endif /* LDAP_BACK_PRINT_CONNTREE */
1138 (void)ldap_back_conn_delete( li, lc );
1139 LDAP_BACK_CONN_TAINTED_SET( lc );
1141 #if LDAP_BACK_PRINT_CONNTREE > 0
1142 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1143 #endif /* LDAP_BACK_PRINT_CONNTREE */
1145 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1148 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1149 char buf[ SLAP_TEXT_BUFLEN ];
1151 snprintf( buf, sizeof( buf ),
1152 "conn %p fetched refcnt=%u%s",
1154 expiring ? " expiring" : "" );
1155 Debug( LDAP_DEBUG_TRACE,
1156 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
1162 #endif /* HAVE_TLS */
1168 ldap_back_release_conn_lock(
1174 ldapconn_t *lc = *lcp;
1177 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1179 assert( lc->lc_refcnt > 0 );
1180 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1182 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
1183 ldap_back_freeconn( li, lc, 0 );
1187 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1192 ldap_back_quarantine(
1196 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1198 slap_retry_info_t *ri = &li->li_quarantine;
1200 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
1202 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1203 time_t new_last = slap_get_time();
1205 switch ( li->li_isquarantined ) {
1206 case LDAP_BACK_FQ_NO:
1207 if ( ri->ri_last == new_last ) {
1211 Debug( LDAP_DEBUG_ANY,
1212 "%s: ldap_back_quarantine enter.\n",
1213 op->o_log_prefix, 0, 0 );
1219 case LDAP_BACK_FQ_RETRYING:
1220 Debug( LDAP_DEBUG_ANY,
1221 "%s: ldap_back_quarantine block #%d try #%d failed.\n",
1222 op->o_log_prefix, ri->ri_idx, ri->ri_count );
1225 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1226 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1237 li->li_isquarantined = LDAP_BACK_FQ_YES;
1238 ri->ri_last = new_last;
1240 } else if ( li->li_isquarantined != LDAP_BACK_FQ_NO ) {
1241 if ( ri->ri_last == slap_get_time() ) {
1245 Debug( LDAP_DEBUG_ANY,
1246 "%s: ldap_back_quarantine exit (%d) err=%d.\n",
1247 op->o_log_prefix, li->li_isquarantined, rs->sr_err );
1249 if ( li->li_quarantine_f ) {
1250 (void)li->li_quarantine_f( li, li->li_quarantine_p );
1255 li->li_isquarantined = LDAP_BACK_FQ_NO;
1259 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
1263 ldap_back_dobind_cb(
1268 ber_tag_t *tptr = op->o_callback->sc_private;
1270 rs->sr_tag = slap_req2res( op->o_tag );
1272 return SLAP_CB_CONTINUE;
1276 * ldap_back_dobind_int
1278 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1281 ldap_back_dobind_int(
1285 ldap_back_send_t sendok,
1289 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1292 struct berval binddn = slap_empty_bv,
1293 bindcred = slap_empty_bv;
1299 ber_tag_t o_tag = op->o_tag;
1300 slap_callback cb = {0};
1303 assert( lcp != NULL );
1304 assert( retries >= 0 );
1306 if ( sendok & LDAP_BACK_GETCONN ) {
1307 assert( *lcp == NULL );
1309 lc = ldap_back_getconn( op, rs, sendok, &binddn, &bindcred );
1319 assert( lc != NULL );
1323 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1326 if ( binding == 0 ) {
1327 /* check if already bound */
1328 rc = isbound = LDAP_BACK_CONN_ISBOUND( lc );
1331 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1336 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
1337 /* if someone else is about to bind it, give up and retry */
1339 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1341 ldap_pvt_thread_yield();
1345 /* otherwise this thread will bind it */
1346 LDAP_BACK_CONN_BINDING_SET( lc );
1352 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1356 * FIXME: we need to let clients use proxyAuthz
1357 * otherwise we cannot do symmetric pools of servers;
1358 * we have to live with the fact that a user can
1359 * authorize itself as any ID that is allowed
1360 * by the authzTo directive of the "proxyauthzdn".
1363 * NOTE: current Proxy Authorization specification
1364 * and implementation do not allow proxy authorization
1365 * control to be provided with Bind requests
1368 * if no bind took place yet, but the connection is bound
1369 * and the "idassert-authcDN" (or other ID) is set,
1370 * then bind as the asserting identity and explicitly
1371 * add the proxyAuthz control to every operation with the
1372 * dn bound to the connection as control value.
1373 * This is done also if this is the authorizing backend,
1374 * but the "override" flag is given to idassert.
1375 * It allows to use SASL bind and yet proxyAuthz users
1377 op->o_tag = LDAP_REQ_BIND;
1378 cb.sc_next = op->o_callback;
1379 cb.sc_private = &o_tag;
1380 cb.sc_response = ldap_back_dobind_cb;
1381 op->o_callback = &cb;
1383 if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
1384 if ( BER_BVISEMPTY( &binddn ) && BER_BVISEMPTY( &bindcred ) ) {
1385 /* if we got here, it shouldn't return result */
1386 rc = ldap_back_is_proxy_authz( op, rs,
1387 LDAP_BACK_DONTSEND, &binddn, &bindcred );
1390 rc = ldap_back_proxy_authz_bind( lc, op, rs, sendok, &binddn, &bindcred );
1394 #ifdef HAVE_CYRUS_SASL
1395 if ( LDAP_BACK_CONN_ISPRIV( lc )
1396 && li->li_acl_authmethod == LDAP_AUTH_SASL )
1398 void *defaults = NULL;
1400 if ( li->li_acl_secprops != NULL ) {
1401 rc = ldap_set_option( lc->lc_ld,
1402 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops );
1404 if ( rc != LDAP_OPT_SUCCESS ) {
1405 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
1406 "(SECPROPS,\"%s\") failed!\n",
1407 li->li_acl_secprops, 0, 0 );
1412 defaults = lutil_sasl_defaults( lc->lc_ld,
1413 li->li_acl_sasl_mech.bv_val,
1414 li->li_acl_sasl_realm.bv_val,
1415 li->li_acl_authcID.bv_val,
1416 li->li_acl_passwd.bv_val,
1418 if ( defaults == NULL ) {
1419 rs->sr_err = LDAP_OTHER;
1420 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1421 if ( sendok & LDAP_BACK_SENDERR ) {
1422 send_ldap_result( op, rs );
1427 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
1428 li->li_acl_authcDN.bv_val,
1429 li->li_acl_sasl_mech.bv_val, NULL, NULL,
1430 LDAP_SASL_QUIET, lutil_sasl_interact,
1433 lutil_sasl_freedefs( defaults );
1435 switch ( rs->sr_err ) {
1437 LDAP_BACK_CONN_ISBOUND_SET( lc );
1440 case LDAP_LOCAL_ERROR:
1441 /* list client API error codes that require
1442 * to taint the connection */
1443 /* FIXME: should actually retry? */
1444 LDAP_BACK_CONN_TAINTED_SET( lc );
1449 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1450 rs->sr_err = slap_map_api2result( rs );
1451 if ( sendok & LDAP_BACK_SENDERR ) {
1452 send_ldap_result( op, rs );
1457 if ( LDAP_BACK_QUARANTINE( li ) ) {
1458 ldap_back_quarantine( op, rs );
1463 #endif /* HAVE_CYRUS_SASL */
1466 if ( BER_BVISNULL( &lc->lc_cred ) ) {
1468 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) && !BER_BVISEMPTY( &lc->lc_bound_ndn ) ) {
1469 Debug( LDAP_DEBUG_ANY, "%s ldap_back_dobind_int: DN=\"%s\" without creds, binding anonymously",
1470 op->o_log_prefix, lc->lc_bound_ndn.bv_val, 0 );
1474 tmp_dn = lc->lc_bound_ndn.bv_val;
1476 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1478 LDAP_SASL_SIMPLE, &lc->lc_cred,
1479 NULL, NULL, &msgid );
1481 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
1482 if ( retries != LDAP_BACK_RETRY_NEVER ) {
1484 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1487 assert( lc->lc_refcnt > 0 );
1488 if ( lc->lc_refcnt == 1 ) {
1489 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1492 /* lc here must be the regular lc, reset and ready for init */
1493 rs->sr_err = ldap_back_prepare_conn( lc, op, rs, sendok );
1494 if ( rs->sr_err != LDAP_SUCCESS ) {
1495 sendok &= ~LDAP_BACK_SENDERR;
1501 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1504 if ( rs->sr_err == LDAP_SUCCESS ) {
1505 if ( retries > 0 ) {
1512 assert( lc->lc_refcnt == 1 );
1514 ldap_back_freeconn( li, lc, dolock );
1516 rs->sr_err = slap_map_api2result( rs );
1518 if ( LDAP_BACK_QUARANTINE( li ) ) {
1519 ldap_back_quarantine( op, rs );
1522 if ( rs->sr_err != LDAP_SUCCESS &&
1523 ( sendok & LDAP_BACK_SENDERR ) )
1525 if ( op->o_callback == &cb )
1526 op->o_callback = cb.sc_next;
1528 rs->sr_text = "Proxy can't contact remote server";
1529 send_ldap_result( op, rs );
1536 rc = ldap_back_op_result( lc, op, rs, msgid,
1537 -1, ( sendok | LDAP_BACK_BINDING ) );
1538 if ( rc == LDAP_SUCCESS ) {
1539 op->o_conn->c_authz_cookie = op->o_bd->be_private;
1540 LDAP_BACK_CONN_ISBOUND_SET( lc );
1544 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1545 rc = LDAP_BACK_CONN_ISBOUND( lc );
1547 ldap_back_release_conn_lock( li, lcp, dolock );
1549 } else if ( LDAP_BACK_SAVECRED( li ) ) {
1550 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1554 if ( op->o_callback == &cb )
1555 op->o_callback = cb.sc_next;
1564 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1567 ldap_back_dobind( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1569 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1571 return ldap_back_dobind_int( lcp, op, rs,
1572 ( sendok | LDAP_BACK_GETCONN ), li->li_nretries, 1 );
1576 * ldap_back_default_rebind
1578 * This is a callback used for chasing referrals using the same
1579 * credentials as the original user on this session.
1582 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1583 ber_int_t msgid, void *params )
1585 ldapconn_t *lc = (ldapconn_t *)params;
1588 /* ... otherwise we couldn't get here */
1589 assert( lc != NULL );
1591 if ( !ldap_tls_inplace( ld ) ) {
1592 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1594 const char *text = NULL;
1596 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1597 LDAP_BACK_RETRY_DEFAULT, &text );
1598 if ( rc != LDAP_SUCCESS ) {
1602 #endif /* HAVE_TLS */
1604 /* FIXME: add checks on the URL/identity? */
1606 return ldap_sasl_bind_s( ld,
1607 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1608 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1612 * ldap_back_default_urllist
1615 ldap_back_default_urllist(
1617 LDAPURLDesc **urllist,
1621 ldapinfo_t *li = (ldapinfo_t *)params;
1622 LDAPURLDesc **urltail;
1624 if ( urllist == url ) {
1625 return LDAP_SUCCESS;
1628 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1631 *urltail = *urllist;
1635 if ( !li->li_uri_mutex_do_not_lock ) {
1636 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1640 ch_free( li->li_uri );
1643 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1645 if ( !li->li_uri_mutex_do_not_lock ) {
1646 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1649 return LDAP_SUCCESS;
1658 ldap_back_send_t sendok )
1660 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1662 /* default behavior */
1663 if ( LDAP_BACK_ABANDON( li ) ) {
1664 return ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1667 if ( LDAP_BACK_IGNORE( li ) ) {
1668 return ldap_pvt_discard( lc->lc_ld, msgid );
1671 if ( LDAP_BACK_CANCEL( li ) ) {
1672 /* FIXME: asynchronous? */
1673 return ldap_cancel_s( lc->lc_ld, msgid, NULL, NULL );
1682 ldap_back_op_result(
1688 ldap_back_send_t sendok )
1690 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1695 LDAPControl **ctrls = NULL;
1698 rs->sr_matched = NULL;
1700 rs->sr_ctrls = NULL;
1702 /* if the error recorded in the reply corresponds
1703 * to a successful state, get the error from the
1704 * remote server response */
1705 if ( LDAP_ERR_OK( rs->sr_err ) ) {
1708 LDAPMessage *res = NULL;
1709 time_t stoptime = (time_t)(-1);
1710 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
1711 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1712 const char *timeout_text = "Operation timed out";
1714 /* if timeout is not specified, compute and use
1715 * the one specific to the ongoing operation */
1716 if ( timeout == (time_t)(-1) ) {
1717 slap_op_t opidx = slap_req2op( op->o_tag );
1719 if ( opidx == SLAP_OP_SEARCH ) {
1720 if ( op->ors_tlimit <= 0 ) {
1724 timeout = op->ors_tlimit;
1725 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
1726 timeout_text = NULL;
1730 timeout = li->li_timeout[ opidx ];
1734 /* better than nothing :) */
1735 if ( timeout == 0 ) {
1736 if ( li->li_idle_timeout ) {
1737 timeout = li->li_idle_timeout;
1739 } else if ( li->li_conn_ttl ) {
1740 timeout = li->li_conn_ttl;
1745 stoptime = op->o_time + timeout;
1748 LDAP_BACK_TV_SET( &tv );
1751 /* if result parsing fails, note the failure reason */
1752 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1755 if ( timeout && slap_get_time() > stoptime ) {
1756 if ( sendok & LDAP_BACK_BINDING ) {
1757 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1760 /* let it be used, but taint/delete it so that
1761 * no-one else can look it up any further */
1762 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1764 #if LDAP_BACK_PRINT_CONNTREE > 0
1765 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1766 #endif /* LDAP_BACK_PRINT_CONNTREE */
1768 (void)ldap_back_conn_delete( li, lc );
1769 LDAP_BACK_CONN_TAINTED_SET( lc );
1771 #if LDAP_BACK_PRINT_CONNTREE > 0
1772 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1773 #endif /* LDAP_BACK_PRINT_CONNTREE */
1774 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1777 (void)ldap_back_cancel( lc, op, rs, msgid, sendok );
1779 rs->sr_err = timeout_err;
1780 rs->sr_text = timeout_text;
1785 LDAP_BACK_TV_SET( &tv );
1786 ldap_pvt_thread_yield();
1790 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1795 /* otherwise get the result; if it is not
1796 * LDAP_SUCCESS, record it in the reply
1797 * structure (this includes
1798 * LDAP_COMPARE_{TRUE|FALSE}) */
1800 /* only touch when activity actually took place... */
1801 if ( li->li_idle_timeout && lc ) {
1802 lc->lc_time = op->o_time;
1805 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1806 &match, &text, &refs, &ctrls, 1 );
1808 if ( rc != LDAP_SUCCESS ) {
1812 /* RFC 4511: referrals can only appear
1813 * if result code is LDAP_REFERRAL */
1815 && refs[ 0 ] != NULL
1816 && refs[ 0 ][ 0 ] != '\0' )
1818 if ( rs->sr_err != LDAP_REFERRAL ) {
1819 Debug( LDAP_DEBUG_ANY,
1820 "%s ldap_back_op_result: "
1821 "got referrals with err=%d\n",
1828 for ( i = 0; refs[ i ] != NULL; i++ )
1830 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1832 for ( i = 0; refs[ i ] != NULL; i++ ) {
1833 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1835 BER_BVZERO( &rs->sr_ref[ i ] );
1838 } else if ( rs->sr_err == LDAP_REFERRAL ) {
1839 Debug( LDAP_DEBUG_ANY,
1840 "%s ldap_back_op_result: "
1841 "got err=%d with null "
1842 "or empty referrals\n",
1846 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1849 if ( ctrls != NULL ) {
1850 rs->sr_ctrls = ctrls;
1855 /* if the error in the reply structure is not
1856 * LDAP_SUCCESS, try to map it from client
1857 * to server error */
1858 if ( !LDAP_ERR_OK( rs->sr_err ) ) {
1859 rs->sr_err = slap_map_api2result( rs );
1861 /* internal ops ( op->o_conn == NULL )
1862 * must not reply to client */
1863 if ( op->o_conn && !op->o_do_not_cache && match ) {
1865 /* record the (massaged) matched
1866 * DN into the reply structure */
1867 rs->sr_matched = match;
1871 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1872 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1873 if ( LDAP_BACK_QUARANTINE( li ) ) {
1874 ldap_back_quarantine( op, rs );
1876 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1877 if ( rs->sr_text == NULL ) rs->sr_text = "Proxy operation retry failed";
1878 send_ldap_result( op, rs );
1882 } else if ( op->o_conn &&
1883 ( ( ( sendok & LDAP_BACK_SENDOK ) && LDAP_ERR_OK( rs->sr_err ) )
1884 || ( ( sendok & LDAP_BACK_SENDERR ) && !LDAP_ERR_OK( rs->sr_err ) ) ) )
1886 send_ldap_result( op, rs );
1890 ldap_memfree( text );
1894 /* there can't be refs with a (successful) bind */
1896 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1901 ber_memvfree( (void **)refs );
1904 /* match should not be possible with a successful bind */
1906 if ( rs->sr_matched != match ) {
1907 free( (char *)rs->sr_matched );
1909 rs->sr_matched = NULL;
1910 ldap_memfree( match );
1913 if ( ctrls != NULL ) {
1914 if ( op->o_tag == LDAP_REQ_BIND && rs->sr_err == LDAP_SUCCESS ) {
1917 for ( i = 0; ctrls[i] != NULL; i++ );
1919 rs->sr_ctrls = op->o_tmpalloc( sizeof( LDAPControl * )*( i + 1 ),
1921 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
1923 ber_len_t oidlen = strlen( ctrls[i]->ldctl_oid );
1924 ber_len_t size = sizeof( LDAPControl )
1926 + ctrls[i]->ldctl_value.bv_len + 1;
1928 rs->sr_ctrls[ i ] = op->o_tmpalloc( size, op->o_tmpmemctx );
1929 rs->sr_ctrls[ i ]->ldctl_oid = (char *)&rs->sr_ctrls[ i ][ 1 ];
1930 lutil_strcopy( rs->sr_ctrls[ i ]->ldctl_oid, ctrls[i]->ldctl_oid );
1931 rs->sr_ctrls[ i ]->ldctl_value.bv_val
1932 = (char *)&rs->sr_ctrls[ i ]->ldctl_oid[oidlen + 1];
1933 rs->sr_ctrls[ i ]->ldctl_value.bv_len
1934 = ctrls[i]->ldctl_value.bv_len;
1935 ptr = lutil_memcopy( rs->sr_ctrls[ i ]->ldctl_value.bv_val,
1936 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len );
1939 rs->sr_ctrls[ i ] = NULL;
1940 rs->sr_flags |= REP_CTRLS_MUSTBEFREED;
1943 assert( rs->sr_ctrls != NULL );
1944 rs->sr_ctrls = NULL;
1947 ldap_controls_free( ctrls );
1950 return( LDAP_ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1953 /* return true if bound, false if failed */
1955 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1957 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1960 assert( lcp != NULL );
1961 assert( *lcp != NULL );
1963 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1965 if ( (*lcp)->lc_refcnt == 1 ) {
1966 int binding = LDAP_BACK_CONN_BINDING( *lcp );
1968 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1969 Debug( LDAP_DEBUG_ANY,
1970 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1971 op->o_log_prefix, li->li_uri,
1972 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1973 "" : (*lcp)->lc_bound_ndn.bv_val );
1974 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1976 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1977 (*lcp)->lc_ld = NULL;
1978 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1980 /* lc here must be the regular lc, reset and ready for init */
1981 rc = ldap_back_prepare_conn( *lcp, op, rs, sendok );
1982 if ( rc != LDAP_SUCCESS ) {
1983 /* freeit, because lc_refcnt == 1 */
1984 (*lcp)->lc_refcnt = 0;
1985 (void)ldap_back_freeconn( li, *lcp, 0 );
1989 } else if ( ( sendok & LDAP_BACK_BINDING ) ) {
1991 LDAP_BACK_CONN_BINDING_SET( *lcp );
1996 rc = ldap_back_dobind_int( lcp, op, rs, sendok, 0, 0 );
1997 if ( rc == 0 && *lcp != NULL ) {
1998 /* freeit, because lc_refcnt == 1 */
1999 (*lcp)->lc_refcnt = 0;
2000 LDAP_BACK_CONN_TAINTED_SET( *lcp );
2001 (void)ldap_back_freeconn( li, *lcp, 0 );
2007 Debug( LDAP_DEBUG_TRACE,
2008 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
2009 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
2011 LDAP_BACK_CONN_TAINTED_SET( *lcp );
2012 ldap_back_release_conn_lock( li, lcp, 0 );
2013 assert( *lcp == NULL );
2015 if ( sendok & LDAP_BACK_SENDERR ) {
2016 rs->sr_err = LDAP_UNAVAILABLE;
2017 rs->sr_text = "Unable to retry";
2018 send_ldap_result( op, rs );
2022 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
2028 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
2029 struct berval *binddn, struct berval *bindcred )
2031 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2035 if ( op->o_conn == NULL || op->o_do_not_cache ) {
2039 /* don't proxyAuthz if protocol is not LDAPv3 */
2040 switch ( li->li_version ) {
2045 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2051 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2052 if ( sendok & LDAP_BACK_SENDERR ) {
2053 send_ldap_result( op, rs );
2060 *binddn = slap_empty_bv;
2061 *bindcred = slap_empty_bv;
2063 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2064 ndn = op->o_conn->c_ndn;
2070 switch ( li->li_idassert_mode ) {
2071 case LDAP_BACK_IDASSERT_LEGACY:
2072 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
2073 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
2075 *binddn = li->li_idassert_authcDN;
2076 *bindcred = li->li_idassert_passwd;
2083 /* NOTE: rootdn can always idassert */
2084 if ( BER_BVISNULL( &ndn )
2085 && li->li_idassert_authz == NULL
2086 && !( li->li_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL ) )
2088 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2089 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
2090 if ( sendok & LDAP_BACK_SENDERR ) {
2091 send_ldap_result( op, rs );
2096 rs->sr_err = LDAP_SUCCESS;
2097 *binddn = slap_empty_bv;
2098 *bindcred = slap_empty_bv;
2104 } else if ( !be_isroot( op ) ) {
2105 if ( li->li_idassert_passthru ) {
2106 struct berval authcDN;
2108 if ( BER_BVISNULL( &ndn ) ) {
2109 authcDN = slap_empty_bv;
2114 rs->sr_err = slap_sasl_matches( op, li->li_idassert_passthru,
2115 &authcDN, &authcDN );
2116 if ( rs->sr_err == LDAP_SUCCESS ) {
2122 if ( li->li_idassert_authz ) {
2123 struct berval authcDN;
2125 if ( BER_BVISNULL( &ndn ) ) {
2126 authcDN = slap_empty_bv;
2131 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
2132 &authcDN, &authcDN );
2133 if ( rs->sr_err != LDAP_SUCCESS ) {
2134 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2135 if ( sendok & LDAP_BACK_SENDERR ) {
2136 send_ldap_result( op, rs );
2141 rs->sr_err = LDAP_SUCCESS;
2142 *binddn = slap_empty_bv;
2143 *bindcred = slap_empty_bv;
2152 *binddn = li->li_idassert_authcDN;
2153 *bindcred = li->li_idassert_passwd;
2163 ldap_back_proxy_authz_bind(
2167 ldap_back_send_t sendok,
2168 struct berval *binddn,
2169 struct berval *bindcred )
2171 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2176 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2177 ndn = op->o_conn->c_ndn;
2183 if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
2184 #ifdef HAVE_CYRUS_SASL
2185 void *defaults = NULL;
2186 struct berval authzID = BER_BVNULL;
2189 /* if SASL supports native authz, prepare for it */
2190 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
2191 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2193 switch ( li->li_idassert_mode ) {
2194 case LDAP_BACK_IDASSERT_OTHERID:
2195 case LDAP_BACK_IDASSERT_OTHERDN:
2196 authzID = li->li_idassert_authzID;
2199 case LDAP_BACK_IDASSERT_ANONYMOUS:
2200 BER_BVSTR( &authzID, "dn:" );
2203 case LDAP_BACK_IDASSERT_SELF:
2204 if ( BER_BVISNULL( &ndn ) ) {
2205 /* connection is not authc'd, so don't idassert */
2206 BER_BVSTR( &authzID, "dn:" );
2209 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
2210 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
2211 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
2212 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
2213 ndn.bv_val, ndn.bv_len + 1 );
2222 if ( li->li_idassert_secprops != NULL ) {
2223 rs->sr_err = ldap_set_option( lc->lc_ld,
2224 LDAP_OPT_X_SASL_SECPROPS,
2225 (void *)li->li_idassert_secprops );
2227 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
2228 rs->sr_err = LDAP_OTHER;
2229 if ( sendok & LDAP_BACK_SENDERR ) {
2230 send_ldap_result( op, rs );
2232 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2237 defaults = lutil_sasl_defaults( lc->lc_ld,
2238 li->li_idassert_sasl_mech.bv_val,
2239 li->li_idassert_sasl_realm.bv_val,
2240 li->li_idassert_authcID.bv_val,
2241 li->li_idassert_passwd.bv_val,
2243 if ( defaults == NULL ) {
2244 rs->sr_err = LDAP_OTHER;
2245 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2246 if ( sendok & LDAP_BACK_SENDERR ) {
2247 send_ldap_result( op, rs );
2252 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn->bv_val,
2253 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
2254 LDAP_SASL_QUIET, lutil_sasl_interact,
2257 switch ( rs->sr_err ) {
2259 LDAP_BACK_CONN_ISBOUND_SET( lc );
2262 case LDAP_LOCAL_ERROR:
2263 /* list client API error codes that require
2264 * to taint the connection */
2265 /* FIXME: should actually retry? */
2266 LDAP_BACK_CONN_TAINTED_SET( lc );
2271 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2272 rs->sr_err = slap_map_api2result( rs );
2273 if ( sendok & LDAP_BACK_SENDERR ) {
2274 send_ldap_result( op, rs );
2279 lutil_sasl_freedefs( defaults );
2281 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
2285 #endif /* HAVE_CYRUS_SASL */
2288 switch ( li->li_idassert_authmethod ) {
2289 case LDAP_AUTH_NONE:
2290 /* FIXME: do we really need this? */
2291 BER_BVSTR( binddn, "" );
2292 BER_BVSTR( bindcred, "" );
2295 case LDAP_AUTH_SIMPLE:
2296 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
2297 binddn->bv_val, LDAP_SASL_SIMPLE,
2298 bindcred, NULL, NULL, &msgid );
2299 rc = ldap_back_op_result( lc, op, rs, msgid,
2300 -1, ( sendok | LDAP_BACK_BINDING ) );
2305 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2306 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
2307 if ( sendok & LDAP_BACK_SENDERR ) {
2308 send_ldap_result( op, rs );
2313 if ( rc == LDAP_SUCCESS ) {
2314 /* set rebind stuff in case of successful proxyAuthz bind,
2315 * so that referral chasing is attempted using the right
2317 LDAP_BACK_CONN_ISBOUND_SET( lc );
2318 op->o_conn->c_authz_cookie = op->o_bd->be_private;
2319 if ( !BER_BVISNULL( binddn ) ) {
2320 ber_bvreplace( &lc->lc_bound_ndn, binddn );
2323 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
2324 memset( lc->lc_cred.bv_val, 0,
2325 lc->lc_cred.bv_len );
2328 if ( LDAP_BACK_SAVECRED( li ) ) {
2329 if ( !BER_BVISNULL( bindcred ) ) {
2330 ber_bvreplace( &lc->lc_cred, bindcred );
2331 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
2335 lc->lc_cred.bv_len = 0;
2339 return LDAP_BACK_CONN_ISBOUND( lc );
2343 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
2344 * to existing server-side controls if required; if not,
2345 * the existing server-side controls are placed in *pctrls.
2346 * The caller, after using the controls in client API
2347 * operations, if ( *pctrls != op->o_ctrls ), should
2348 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
2349 * The function returns success if the control could
2350 * be added if required, or if it did nothing; in the future,
2351 * it might return some error if it failed.
2353 * if no bind took place yet, but the connection is bound
2354 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
2355 * and explicitly add proxyAuthz the control to every operation
2356 * with the dn bound to the connection as control value.
2358 * If no server-side controls are defined for the operation,
2359 * simply add the proxyAuthz control; otherwise, if the
2360 * proxyAuthz control is not already set, add it as
2363 * FIXME: is controls order significant for security?
2364 * ANSWER: controls ordering and interoperability
2365 * must be indicated by the specs of each control; if none
2366 * is specified, the order is irrelevant.
2369 ldap_back_proxy_authz_ctrl(
2372 struct berval *bound_ndn,
2374 slap_idassert_t *si,
2377 slap_idassert_mode_t mode;
2378 struct berval assertedID,
2382 rs->sr_err = SLAP_CB_CONTINUE;
2384 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
2385 * but if it is not set this test fails. We need a different
2386 * means to detect if idassert is enabled */
2387 if ( ( BER_BVISNULL( &si->si_bc.sb_authcId ) || BER_BVISEMPTY( &si->si_bc.sb_authcId ) )
2388 && ( BER_BVISNULL( &si->si_bc.sb_binddn ) || BER_BVISEMPTY( &si->si_bc.sb_binddn ) )
2389 && BER_BVISNULL( &si->si_bc.sb_saslmech ) )
2394 if ( !op->o_conn || op->o_do_not_cache || ( isroot = be_isroot( op ) ) ) {
2398 if ( op->o_tag == LDAP_REQ_BIND ) {
2399 ndn = op->o_req_ndn;
2401 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2402 ndn = op->o_conn->c_ndn;
2408 if ( si->si_mode == LDAP_BACK_IDASSERT_LEGACY ) {
2409 if ( op->o_proxy_authz ) {
2411 * FIXME: we do not want to perform proxyAuthz
2412 * on behalf of the client, because this would
2413 * be performed with "proxyauthzdn" privileges.
2415 * This might actually be too strict, since
2416 * the "proxyauthzdn" authzTo, and each entry's
2417 * authzFrom attributes may be crafted
2418 * to avoid unwanted proxyAuthz to take place.
2421 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2422 rs->sr_text = "proxyAuthz not allowed within namingContext";
2427 if ( !BER_BVISNULL( bound_ndn ) ) {
2431 if ( BER_BVISNULL( &ndn ) ) {
2435 if ( BER_BVISNULL( &si->si_bc.sb_binddn ) ) {
2439 } else if ( si->si_bc.sb_method == LDAP_AUTH_SASL ) {
2440 if ( ( si->si_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2442 /* already asserted in SASL via native authz */
2446 } else if ( si->si_authz && !isroot ) {
2448 struct berval authcDN;
2450 if ( BER_BVISNULL( &ndn ) ) {
2451 authcDN = slap_empty_bv;
2455 rc = slap_sasl_matches( op, si->si_authz,
2456 &authcDN, &authcDN );
2457 if ( rc != LDAP_SUCCESS ) {
2458 if ( si->si_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2459 /* ndn is not authorized
2460 * to use idassert */
2467 if ( op->o_proxy_authz ) {
2470 * 1) ignore the already set proxyAuthz control
2471 * 2) leave it in place, and don't set ours
2473 * 4) reject the operation
2475 * option (4) is very drastic
2476 * option (3) will make the remote server reject
2477 * the operation, thus being equivalent to (4)
2478 * option (2) will likely break the idassert
2479 * assumptions, so we cannot accept it;
2480 * option (1) means that we are contradicting
2481 * the client's reques.
2483 * I think (4) is the only correct choice.
2485 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2486 rs->sr_text = "proxyAuthz not allowed within namingContext";
2489 if ( op->o_is_auth_check ) {
2490 mode = LDAP_BACK_IDASSERT_NOASSERT;
2497 case LDAP_BACK_IDASSERT_LEGACY:
2498 /* original behavior:
2499 * assert the client's identity */
2500 case LDAP_BACK_IDASSERT_SELF:
2504 case LDAP_BACK_IDASSERT_ANONYMOUS:
2505 /* assert "anonymous" */
2506 assertedID = slap_empty_bv;
2509 case LDAP_BACK_IDASSERT_NOASSERT:
2510 /* don't assert; bind as proxyauthzdn */
2513 case LDAP_BACK_IDASSERT_OTHERID:
2514 case LDAP_BACK_IDASSERT_OTHERDN:
2515 /* assert idassert DN */
2516 assertedID = si->si_bc.sb_authzId;
2523 /* if we got here, "" is allowed to proxyAuthz */
2524 if ( BER_BVISNULL( &assertedID ) ) {
2525 assertedID = slap_empty_bv;
2528 /* don't idassert the bound DN (ITS#4497) */
2529 if ( dn_match( &assertedID, bound_ndn ) ) {
2533 ctrl->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
2534 ctrl->ldctl_iscritical = ( ( si->si_flags & LDAP_BACK_AUTH_PROXYAUTHZ_CRITICAL ) == LDAP_BACK_AUTH_PROXYAUTHZ_CRITICAL );
2536 switch ( si->si_mode ) {
2537 /* already in u:ID or dn:DN form */
2538 case LDAP_BACK_IDASSERT_OTHERID:
2539 case LDAP_BACK_IDASSERT_OTHERDN:
2540 ber_dupbv_x( &ctrl->ldctl_value, &assertedID, op->o_tmpmemctx );
2541 rs->sr_err = LDAP_SUCCESS;
2544 /* needs the dn: prefix */
2546 ctrl->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
2547 ctrl->ldctl_value.bv_val = op->o_tmpalloc( ctrl->ldctl_value.bv_len + 1,
2549 AC_MEMCPY( ctrl->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
2550 AC_MEMCPY( &ctrl->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
2551 assertedID.bv_val, assertedID.bv_len + 1 );
2552 rs->sr_err = LDAP_SUCCESS;
2556 /* Older versions of <draft-weltman-ldapv3-proxy> required
2557 * to encode the value of the authzID (and called it proxyDN);
2558 * this hack provides compatibility with those DSAs that
2559 * implement it this way */
2560 if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
2561 struct berval authzID = ctrl->ldctl_value;
2562 BerElementBuffer berbuf;
2563 BerElement *ber = (BerElement *)&berbuf;
2566 ber_init2( ber, 0, LBER_USE_DER );
2567 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2569 tag = ber_printf( ber, "O", &authzID );
2570 if ( tag == LBER_ERROR ) {
2571 rs->sr_err = LDAP_OTHER;
2575 if ( ber_flatten2( ber, &ctrl->ldctl_value, 1 ) == -1 ) {
2576 rs->sr_err = LDAP_OTHER;
2580 rs->sr_err = LDAP_SUCCESS;
2583 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2584 ber_free_buf( ber );
2586 if ( rs->sr_err != LDAP_SUCCESS ) {
2590 } else if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
2591 struct berval authzID = ctrl->ldctl_value,
2593 BerElementBuffer berbuf;
2594 BerElement *ber = (BerElement *)&berbuf;
2597 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
2598 rs->sr_err = LDAP_PROTOCOL_ERROR;
2603 tmp.bv_val += STRLENOF( "dn:" );
2604 tmp.bv_len -= STRLENOF( "dn:" );
2606 ber_init2( ber, 0, LBER_USE_DER );
2607 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2609 /* apparently, Mozilla API encodes this
2610 * as "SEQUENCE { LDAPDN }" */
2611 tag = ber_printf( ber, "{O}", &tmp );
2612 if ( tag == LBER_ERROR ) {
2613 rs->sr_err = LDAP_OTHER;
2617 if ( ber_flatten2( ber, &ctrl->ldctl_value, 1 ) == -1 ) {
2618 rs->sr_err = LDAP_OTHER;
2622 ctrl->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
2623 rs->sr_err = LDAP_SUCCESS;
2626 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2627 ber_free_buf( ber );
2629 if ( rs->sr_err != LDAP_SUCCESS ) {
2642 * if any needs to be added, it is prepended to existing ones,
2643 * in a newly allocated array. The companion function
2644 * ldap_back_controls_free() must be used to restore the original
2645 * status of op->o_ctrls.
2648 ldap_back_controls_add(
2652 LDAPControl ***pctrls )
2654 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2656 LDAPControl **ctrls = NULL;
2657 /* set to the maximum number of controls this backend can add */
2658 LDAPControl c[ 2 ] = { { 0 } };
2659 int n = 0, i, j1 = 0, j2 = 0;
2663 rs->sr_err = LDAP_SUCCESS;
2665 /* don't add controls if protocol is not LDAPv3 */
2666 switch ( li->li_version ) {
2671 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2680 /* put controls that go __before__ existing ones here */
2682 /* proxyAuthz for identity assertion */
2683 switch ( ldap_back_proxy_authz_ctrl( op, rs, &lc->lc_bound_ndn,
2684 li->li_version, &li->li_idassert, &c[ j1 ] ) )
2686 case SLAP_CB_CONTINUE:
2697 /* put controls that go __after__ existing ones here */
2699 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
2700 /* FIXME: according to <draft-wahl-ldap-session>,
2701 * the server should check if the control can be added
2702 * based on the identity of the client and so */
2704 /* session tracking */
2705 if ( LDAP_BACK_ST_REQUEST( li ) ) {
2706 switch ( slap_ctrl_session_tracking_request_add( op, rs, &c[ j1 + j2 ] ) ) {
2707 case SLAP_CB_CONTINUE:
2718 #endif /* SLAP_CONTROL_X_SESSION_TRACKING */
2720 if ( rs->sr_err == SLAP_CB_CONTINUE ) {
2721 rs->sr_err = LDAP_SUCCESS;
2724 /* if nothing to do, just bail out */
2725 if ( j1 == 0 && j2 == 0 ) {
2729 assert( j1 + j2 <= (int) (sizeof( c )/sizeof( c[0] )) );
2731 if ( op->o_ctrls ) {
2732 for ( n = 0; op->o_ctrls[ n ]; n++ )
2733 /* just count ctrls */ ;
2736 ctrls = op->o_tmpalloc( (n + j1 + j2 + 1) * sizeof( LDAPControl * ) + ( j1 + j2 ) * sizeof( LDAPControl ),
2739 ctrls[ 0 ] = (LDAPControl *)&ctrls[ n + j1 + j2 + 1 ];
2740 *ctrls[ 0 ] = c[ 0 ];
2741 for ( i = 1; i < j1; i++ ) {
2742 ctrls[ i ] = &ctrls[ 0 ][ i ];
2743 *ctrls[ i ] = c[ i ];
2748 if ( op->o_ctrls ) {
2749 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
2750 ctrls[ i + j1 ] = op->o_ctrls[ i ];
2756 ctrls[ n ] = (LDAPControl *)&ctrls[ n + j2 + 1 ] + j1;
2757 *ctrls[ n ] = c[ j1 ];
2758 for ( i = 1; i < j2; i++ ) {
2759 ctrls[ n + i ] = &ctrls[ n ][ i ];
2760 *ctrls[ n + i ] = c[ i ];
2764 ctrls[ n + j2 ] = NULL;
2767 if ( ctrls == NULL ) {
2768 ctrls = op->o_ctrls;
2777 ldap_back_controls_free( Operation *op, SlapReply *rs, LDAPControl ***pctrls )
2779 LDAPControl **ctrls = *pctrls;
2781 /* we assume that the controls added by the proxy come first,
2782 * so as soon as we find op->o_ctrls[ 0 ] we can stop */
2783 if ( ctrls && ctrls != op->o_ctrls ) {
2784 int i = 0, n = 0, n_added;
2785 LDAPControl *lower, *upper;
2787 assert( ctrls[ 0 ] != NULL );
2789 for ( n = 0; ctrls[ n ] != NULL; n++ )
2792 if ( op->o_ctrls ) {
2793 for ( i = 0; op->o_ctrls[ i ] != NULL; i++ )
2798 lower = (LDAPControl *)&ctrls[ n ];
2799 upper = &lower[ n_added ];
2801 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
2802 if ( ctrls[ i ] < lower || ctrls[ i ] >= upper ) {
2803 /* original; don't touch */
2807 if ( !BER_BVISNULL( &ctrls[ i ]->ldctl_value ) ) {
2808 op->o_tmpfree( ctrls[ i ]->ldctl_value.bv_val, op->o_tmpmemctx );
2812 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2821 ldap_back_conn2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen )
2823 char tbuf[ SLAP_TEXT_BUFLEN ];
2824 char *ptr = buf, *end = buf + buflen;
2827 if ( ptr + sizeof("conn=") > end ) return -1;
2828 ptr = lutil_strcopy( ptr, "conn=" );
2830 len = ldap_back_connid2str( lc, ptr, (ber_len_t)(end - ptr) );
2832 if ( ptr >= end ) return -1;
2834 if ( !BER_BVISNULL( &lc->lcb_local_ndn ) ) {
2835 if ( ptr + sizeof(" DN=\"\"") + lc->lcb_local_ndn.bv_len > end ) return -1;
2836 ptr = lutil_strcopy( ptr, " DN=\"" );
2837 ptr = lutil_strncopy( ptr, lc->lcb_local_ndn.bv_val, lc->lcb_local_ndn.bv_len );
2841 if ( lc->lcb_create_time != 0 ) {
2842 len = snprintf( tbuf, sizeof(tbuf), "%ld", lc->lcb_create_time );
2843 if ( ptr + sizeof(" created=") + len >= end ) return -1;
2844 ptr = lutil_strcopy( ptr, " created=" );
2845 ptr = lutil_strcopy( ptr, tbuf );
2848 if ( lc->lcb_time != 0 ) {
2849 len = snprintf( tbuf, sizeof(tbuf), "%ld", lc->lcb_time );
2850 if ( ptr + sizeof(" modified=") + len >= end ) return -1;
2851 ptr = lutil_strcopy( ptr, " modified=" );
2852 ptr = lutil_strcopy( ptr, tbuf );
2855 len = snprintf( tbuf, sizeof(tbuf), "%u", lc->lcb_refcnt );
2856 if ( ptr + sizeof(" refcnt=") + len >= end ) return -1;
2857 ptr = lutil_strcopy( ptr, " refcnt=" );
2858 ptr = lutil_strcopy( ptr, tbuf );
2864 ldap_back_connid2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen )
2866 static struct berval conns[] = {
2868 BER_BVC("ROOTDN-TLS"),
2870 BER_BVC("ANON-TLS"),
2872 BER_BVC("BIND-TLS"),
2878 if ( LDAP_BACK_PCONN_ISPRIV( (const ldapconn_t *)lc ) ) {
2882 cid = (long)lc->lcb_conn;
2883 assert( cid >= LDAP_BACK_PCONN_FIRST && cid < LDAP_BACK_PCONN_LAST );
2887 if ( bv->bv_len >= buflen ) {
2888 return bv->bv_len + 1;
2892 lutil_strncopy( buf, bv->bv_val, bv->bv_len + 1 );
2895 len = snprintf( buf, buflen, "%lu", lc->lcb_conn->c_connid );