1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2006 The OpenLDAP Foundation.
6 * Portions Copyright 2000-2003 Pierangelo Masarati.
7 * Portions Copyright 1999-2003 Howard Chu.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
19 * This work was initially developed by Howard Chu for inclusion
20 * in OpenLDAP Software and subsequently enhanced by Pierangelo
29 #include <ac/socket.h>
30 #include <ac/string.h>
34 #include "back-ldap.h"
36 #include <lutil_ldap.h>
38 #ifndef PRINT_CONNTREE
39 #define PRINT_CONNTREE 0
40 #endif /* !PRINT_CONNTREE */
42 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
44 #if PRINT_CONNTREE > 0
46 ravl_print( Avlnode *root, int depth )
55 ravl_print( root->avl_right, depth+1 );
57 for ( i = 0; i < depth; i++ ) {
58 fprintf( stderr, "-" );
62 fprintf( stderr, "lc=%p local=\"%s\" conn=%p %s refcnt=%d\n",
63 (void *)lc, lc->lc_local_ndn.bv_val, (void *)lc->lc_conn,
64 avl_bf2str( root->avl_bf ), lc->lc_refcnt );
66 ravl_print( root->avl_left, depth+1 );
70 myprint( Avlnode *root, char *msg )
72 fprintf( stderr, "========> %s\n", msg );
75 fprintf( stderr, "\tNULL\n" );
78 ravl_print( root, 0 );
81 fprintf( stderr, "<======== %s\n", msg );
83 #endif /* PRINT_CONNTREE */
86 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
89 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
92 ldap_back_conndnlc_cmp( const void *c1, const void *c2 );
95 ldap_back_bind( Operation *op, SlapReply *rs )
97 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
103 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR );
108 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
109 ch_free( lc->lc_bound_ndn.bv_val );
110 BER_BVZERO( &lc->lc_bound_ndn );
112 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
114 /* method is always LDAP_AUTH_SIMPLE if we got here */
115 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
117 &op->orb_cred, op->o_ctrls, NULL, &msgid );
118 rc = ldap_back_op_result( lc, op, rs, msgid, 0, LDAP_BACK_SENDERR );
120 if ( rc == LDAP_SUCCESS ) {
121 /* If defined, proxyAuthz will be used also when
122 * back-ldap is the authorizing backend; for this
123 * purpose, a successful bind is followed by a
124 * bind with the configured identity assertion */
125 /* NOTE: use with care */
126 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
127 ldap_back_proxy_authz_bind( lc, op, rs, LDAP_BACK_SENDERR );
128 if ( !LDAP_BACK_CONN_ISBOUND( lc ) ) {
134 LDAP_BACK_CONN_ISBOUND_SET( lc );
135 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
137 if ( LDAP_BACK_SAVECRED( li ) ) {
138 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
139 memset( lc->lc_cred.bv_val, 0,
140 lc->lc_cred.bv_len );
142 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
143 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
148 assert( lc->lc_binding == 1 );
151 /* must re-insert if local DN changed as result of bind */
152 if ( !LDAP_BACK_CONN_ISBOUND( lc )
153 || ( LDAP_BACK_CONN_ISBOUND( lc )
154 && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) ) )
159 /* wait for all other ops to release the connection */
161 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
162 if ( lc->lc_refcnt > 1 ) {
163 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
164 ldap_pvt_thread_yield();
168 assert( lc->lc_refcnt == 1 );
169 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
170 ldap_back_conndnlc_cmp );
171 assert( tmplc == NULL || lc == tmplc );
173 /* delete all cached connections with the current connection */
174 if ( LDAP_BACK_SINGLECONN( li ) ) {
175 while ( ( tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc, ldap_back_conn_cmp ) ) != NULL )
177 Debug( LDAP_DEBUG_TRACE,
178 "=>ldap_back_bind: destroying conn %ld (refcnt=%u)\n",
179 LDAP_BACK_PCONN_ID( lc->lc_conn ), lc->lc_refcnt, 0 );
181 if ( lc->lc_refcnt != 0 ) {
183 LDAP_BACK_CONN_TAINTED_SET( tmplc );
187 * Needs a test because the handler may be corrupted,
188 * and calling ldap_unbind on a corrupted header results
189 * in a segmentation fault
191 ldap_back_conn_free( tmplc );
196 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
197 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
198 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
199 ldap_back_conndn_cmp, ldap_back_conndn_dup );
202 #if PRINT_CONNTREE > 0
203 myprint( li->li_conninfo.lai_tree, "ldap_back_bind" );
204 #endif /* PRINT_CONNTREE */
206 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
212 /* duplicate; someone else successfully bound
213 * on the same connection with the same identity;
214 * we can do this because lc_refcnt == 1 */
215 ldap_back_conn_free( lc );
221 ldap_back_release_conn( op, rs, lc );
228 * ldap_back_conndn_cmp
230 * compares two ldapconn_t based on the value of the conn pointer
231 * and of the local DN; used by avl stuff for insert, lookup
235 ldap_back_conndn_cmp( const void *c1, const void *c2 )
237 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
238 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
241 /* If local DNs don't match, it is definitely not a match */
242 /* For shared sessions, conn is NULL. Only explicitly
243 * bound sessions will have non-NULL conn.
245 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
247 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
254 * ldap_back_conndnlc_cmp
256 * compares two ldapconn_t based on the value of the conn pointer,
257 * the local DN and the lc pointer; used by avl stuff for insert, lookup
261 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
263 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
264 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
267 /* If local DNs don't match, it is definitely not a match */
268 /* For shared sessions, conn is NULL. Only explicitly
269 * bound sessions will have non-NULL conn.
271 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
273 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
275 rc = SLAP_PTRCMP( lc1, lc2 );
285 * compares two ldapconn_t based on the value of the conn pointer;
286 * used by avl stuff for delete of all conns with the same connid
289 ldap_back_conn_cmp( const void *c1, const void *c2 )
291 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
292 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
294 /* For shared sessions, conn is NULL. Only explicitly
295 * bound sessions will have non-NULL conn.
297 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
301 * ldap_back_conndn_dup
303 * returns -1 in case a duplicate ldapconn_t has been inserted;
307 ldap_back_conndn_dup( void *c1, void *c2 )
309 ldapconn_t *lc1 = (ldapconn_t *)c1;
310 ldapconn_t *lc2 = (ldapconn_t *)c2;
312 /* Cannot have more than one shared session with same DN */
313 if ( lc1->lc_conn == lc2->lc_conn &&
314 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
323 ldap_back_freeconn( Operation *op, ldapconn_t *lc, int dolock )
325 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
329 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
332 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
333 ldap_back_conndnlc_cmp );
334 assert( LDAP_BACK_CONN_TAINTED( lc ) || tmplc == lc );
335 if ( lc->lc_refcnt == 0 ) {
336 ldap_back_conn_free( (void *)lc );
340 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
357 int rc = LDAP_SUCCESS;
360 /* this is ridiculous... */
361 dummy.li_flags = flags;
363 /* start TLS ("tls-[try-]{start,propagate}" statements) */
364 if ( ( LDAP_BACK_USE_TLS( &dummy ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS( &dummy ) ) )
365 && !ldap_is_ldaps_url( url ) )
367 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
369 * use asynchronous StartTLS
370 * in case, chase referral (not implemented yet)
374 if ( protocol == 0 ) {
375 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
379 if ( protocol < LDAP_VERSION3 ) {
380 /* we should rather bail out... */
381 rc = LDAP_UNWILLING_TO_PERFORM;
382 *text = "invalid protocol version";
385 if ( rc == LDAP_SUCCESS ) {
386 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
389 if ( rc == LDAP_SUCCESS ) {
390 LDAPMessage *res = NULL;
393 LDAP_BACK_TV_SET( &tv );
396 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
398 rc = LDAP_UNAVAILABLE;
400 } else if ( rc == 0 ) {
401 if ( retries != LDAP_BACK_RETRY_NEVER ) {
402 ldap_pvt_thread_yield();
406 LDAP_BACK_TV_SET( &tv );
409 rc = LDAP_UNAVAILABLE;
411 } else if ( rc == LDAP_RES_EXTENDED ) {
412 struct berval *data = NULL;
414 rc = ldap_parse_extended_result( ld, res,
416 if ( rc == LDAP_SUCCESS ) {
418 rc = ldap_parse_result( ld, res, &err,
419 NULL, NULL, NULL, NULL, 1 );
420 if ( rc == LDAP_SUCCESS ) {
425 /* FIXME: in case a referral
426 * is returned, should we try
427 * using it instead of the
429 if ( rc == LDAP_SUCCESS ) {
430 rc = ldap_install_tls( ld );
432 } else if ( rc == LDAP_REFERRAL ) {
433 rc = LDAP_UNWILLING_TO_PERFORM;
434 *text = "unwilling to chase referral returned by Start TLS exop";
438 if ( data->bv_val ) {
439 ber_memfree( data->bv_val );
453 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
455 * use synchronous StartTLS
457 rc = ldap_start_tls_s( ld, NULL, NULL );
458 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
460 /* if StartTLS is requested, only attempt it if the URL
461 * is not "ldaps://"; this may occur not only in case
462 * of misconfiguration, but also when used in the chain
463 * overlay, where the "uri" can be parsed out of a referral */
469 case LDAP_SERVER_DOWN:
473 if ( LDAP_BACK_TLS_CRITICAL( &dummy ) ) {
474 *text = "could not start TLS";
478 /* in case Start TLS is not critical */
490 #endif /* HAVE_TLS */
493 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
495 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
499 int is_tls = op->o_conn->c_is_tls;
500 #endif /* HAVE_TLS */
502 assert( lcp != NULL );
504 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
505 rs->sr_err = ldap_initialize( &ld, li->li_uri );
506 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
507 if ( rs->sr_err != LDAP_SUCCESS ) {
511 if ( li->li_urllist_f ) {
512 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
515 /* Set LDAP version. This will always succeed: If the client
516 * bound with a particular version, then so can we.
518 if ( li->li_version != 0 ) {
519 version = li->li_version;
521 } else if ( op->o_protocol != 0 ) {
522 version = op->o_protocol;
525 /* assume it's an internal op; set to LDAPv3 */
526 version = LDAP_VERSION3;
528 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
530 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
531 ldap_set_option( ld, LDAP_OPT_REFERRALS,
532 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
534 if ( li->li_network_timeout > 0 ) {
537 tv.tv_sec = li->li_network_timeout;
539 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
543 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
544 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
545 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
546 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
547 if ( rs->sr_err != LDAP_SUCCESS ) {
548 ldap_unbind_ext( ld, NULL, NULL );
551 #endif /* HAVE_TLS */
553 if ( *lcp == NULL ) {
554 *lcp = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
555 (*lcp)->lc_flags = li->li_flags;
558 (*lcp)->lc_refcnt = 1;
559 (*lcp)->lc_binding = 1;
562 LDAP_BACK_CONN_ISTLS_SET( *lcp );
564 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
566 #endif /* HAVE_TLS */
569 if ( rs->sr_err != LDAP_SUCCESS ) {
570 rs->sr_err = slap_map_api2result( rs );
571 if ( sendok & LDAP_BACK_SENDERR ) {
572 if ( rs->sr_text == NULL ) {
573 rs->sr_text = "ldap_initialize() failed";
575 send_ldap_result( op, rs );
580 if ( li->li_conn_ttl > 0 ) {
581 (*lcp)->lc_create_time = op->o_time;
589 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
591 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
592 ldapconn_t *lc = NULL,
594 int refcnt = 1, binding = 1;
596 /* Internal searches are privileged and shared. So is root. */
597 if ( op->o_do_not_cache || be_isroot( op ) ) {
598 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
599 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
600 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
603 lc_curr.lc_local_ndn = op->o_ndn;
604 /* Explicit binds must not be shared */
605 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
606 lc_curr.lc_conn = op->o_conn;
609 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
613 /* Explicit Bind requests always get their own conn */
614 if ( !( sendok & LDAP_BACK_BINDING ) ) {
615 /* Searches for a ldapconn in the avl tree */
617 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
619 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
620 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
622 /* Don't reuse connections while they're still binding */
623 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
624 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
625 ldap_pvt_thread_yield();
628 refcnt = ++lc->lc_refcnt;
629 binding = ++lc->lc_binding;
631 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
634 /* Looks like we didn't get a bind. Open a new session... */
636 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
639 if ( sendok & LDAP_BACK_BINDING ) {
640 LDAP_BACK_CONN_BINDING_SET( lc );
642 lc->lc_conn = lc_curr.lc_conn;
643 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
645 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
646 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
647 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
648 LDAP_BACK_CONN_ISPRIV_SET( lc );
651 BER_BVZERO( &lc->lc_cred );
652 BER_BVZERO( &lc->lc_bound_ndn );
654 /* FIXME: if we set lc_bound_ndn = o_ndn
655 * we end up with a bind with DN but no password! */
656 if ( !BER_BVISEMPTY( &op->o_ndn )
657 && SLAP_IS_AUTHZ_BACKEND( op ) )
659 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
665 /* if start TLS failed but it was not mandatory,
666 * check if the non-TLS connection was already
667 * in cache; in case, destroy the newly created
668 * connection and use the existing one */
669 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
670 && !ldap_tls_inplace( lc->lc_ld ) )
674 lc_curr.lc_conn = LDAP_BACK_PCONN;
675 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
676 tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
677 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
678 if ( tmplc != NULL ) {
679 refcnt = ++tmplc->lc_refcnt;
680 binding = ++tmplc->lc_binding;
681 ldap_back_conn_free( lc );
684 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
686 if ( tmplc != NULL ) {
690 #endif /* HAVE_TLS */
692 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
694 /* Inserts the newly created ldapconn in the avl tree */
695 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
697 assert( lc->lc_refcnt == 1 );
698 assert( lc->lc_binding == 1 );
699 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
700 ldap_back_conndn_cmp, ldap_back_conndn_dup );
702 #if PRINT_CONNTREE > 0
703 myprint( li->li_conninfo.lai_tree, "ldap_back_getconn" );
704 #endif /* PRINT_CONNTREE */
706 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
708 Debug( LDAP_DEBUG_TRACE,
709 "=>ldap_back_getconn: conn %p inserted refcnt=%u binding=%u\n",
710 (void *)lc, refcnt, binding );
712 /* Err could be -1 in case a duplicate ldapconn is inserted */
713 switch ( rs->sr_err ) {
718 if ( !( sendok & LDAP_BACK_BINDING ) ) {
719 /* duplicate: free and try to get the newly created one */
722 /* taint connection, so that it'll be freed when released */
723 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
724 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
725 ldap_back_conndnlc_cmp );
726 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
727 LDAP_BACK_CONN_TAINTED_SET( lc );
731 ldap_back_conn_free( lc );
732 rs->sr_err = LDAP_OTHER;
733 rs->sr_text = "proxy bind collision";
734 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
735 send_ldap_result( op, rs );
742 char buf[ SLAP_TEXT_BUFLEN ];
745 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
746 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
750 /* let it be used, but taint/delete it so that
751 * no-one else can look it up any further */
752 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
753 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
754 ldap_back_conndnlc_cmp );
755 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
756 LDAP_BACK_CONN_TAINTED_SET( lc );
759 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
760 snprintf( buf, sizeof( buf ),
761 "conn %p fetched refcnt=%u binding=%u%s",
762 (void *)lc, refcnt, binding, expiring ? " expiring" : "" );
763 Debug( LDAP_DEBUG_TRACE,
764 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
771 #endif /* HAVE_TLS */
772 if ( li->li_idle_timeout && lc ) {
773 lc->lc_time = op->o_time;
780 ldap_back_release_conn_lock(
786 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
789 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
791 assert( lc->lc_refcnt > 0 );
792 LDAP_BACK_CONN_BINDING_CLEAR( lc );
794 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
795 ldap_back_freeconn( op, lc, 0 );
798 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
805 * Note: as the check for the value of lc->lc_bound was already here, I removed
806 * it from all the callers, and I made the function return the flag, so
807 * it can be used to simplify the check.
809 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
812 ldap_back_dobind_int(
816 ldap_back_send_t sendok,
820 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
825 assert( retries >= 0 );
829 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
832 if ( binding == 0 ) {
833 /* check if already bound */
834 rc = LDAP_BACK_CONN_ISBOUND( lc );
838 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
843 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
844 /* if someone else is about to bind it, give up and retry */
846 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
848 ldap_pvt_thread_yield();
852 /* otherwise this thread will bind it */
853 LDAP_BACK_CONN_BINDING_SET( lc );
858 /* wait for pending operations to finish */
859 /* FIXME: may become a bottleneck! */
860 if ( lc->lc_refcnt != lc->lc_binding ) {
862 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
864 ldap_pvt_thread_yield();
869 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
873 while ( lc->lc_refcnt > 1 ) {
874 ldap_pvt_thread_yield();
875 rc = LDAP_BACK_CONN_ISBOUND( lc );
882 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
884 LDAP_BACK_CONN_BINDING_SET( lc );
886 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
891 * FIXME: we need to let clients use proxyAuthz
892 * otherwise we cannot do symmetric pools of servers;
893 * we have to live with the fact that a user can
894 * authorize itself as any ID that is allowed
895 * by the authzTo directive of the "proxyauthzdn".
898 * NOTE: current Proxy Authorization specification
899 * and implementation do not allow proxy authorization
900 * control to be provided with Bind requests
903 * if no bind took place yet, but the connection is bound
904 * and the "idassert-authcDN" (or other ID) is set,
905 * then bind as the asserting identity and explicitly
906 * add the proxyAuthz control to every operation with the
907 * dn bound to the connection as control value.
908 * This is done also if this is the authrizing backend,
909 * but the "override" flag is given to idassert.
910 * It allows to use SASL bind and yet proxyAuthz users
912 if ( op->o_conn != NULL &&
913 !op->o_do_not_cache &&
914 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
915 ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
917 (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok );
921 #ifdef HAVE_CYRUS_SASL
922 if ( LDAP_BACK_CONN_ISPRIV( lc )
923 && li->li_acl_authmethod == LDAP_AUTH_SASL )
925 void *defaults = NULL;
927 if ( li->li_acl_secprops != NULL ) {
928 rc = ldap_set_option( lc->lc_ld,
929 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops);
931 if ( rc != LDAP_OPT_SUCCESS ) {
932 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
933 "(SECPROPS,\"%s\") failed!\n",
934 li->li_acl_secprops, 0, 0 );
939 defaults = lutil_sasl_defaults( lc->lc_ld,
940 li->li_acl_sasl_mech.bv_val,
941 li->li_acl_sasl_realm.bv_val,
942 li->li_acl_authcID.bv_val,
943 li->li_acl_passwd.bv_val,
946 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
947 li->li_acl_authcDN.bv_val,
948 li->li_acl_sasl_mech.bv_val, NULL, NULL,
949 LDAP_SASL_QUIET, lutil_sasl_interact,
952 lutil_sasl_freedefs( defaults );
954 rs->sr_err = slap_map_api2result( rs );
955 if ( rs->sr_err != LDAP_SUCCESS ) {
956 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
957 send_ldap_result( op, rs );
960 LDAP_BACK_CONN_ISBOUND_SET( lc );
964 #endif /* HAVE_CYRUS_SASL */
967 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
968 lc->lc_bound_ndn.bv_val,
969 LDAP_SASL_SIMPLE, &lc->lc_cred,
970 NULL, NULL, &msgid );
972 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
973 if ( retries != LDAP_BACK_RETRY_NEVER ) {
975 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
978 assert( lc->lc_refcnt > 0 );
979 if ( lc->lc_refcnt == 1 ) {
980 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
983 /* lc here must be the regular lc, reset and ready for init */
984 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
985 if ( rs->sr_err != LDAP_SUCCESS ) {
992 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
995 if ( rs->sr_err == LDAP_SUCCESS ) {
1004 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1008 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1013 ldap_back_freeconn( op, lc, dolock );
1014 rs->sr_err = slap_map_api2result( rs );
1019 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1020 if ( rc == LDAP_SUCCESS ) {
1021 LDAP_BACK_CONN_ISBOUND_SET( lc );
1026 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1027 rc = LDAP_BACK_CONN_ISBOUND( lc );
1029 ldap_back_release_conn_lock( op, rs, lc, dolock );
1036 ldap_back_dobind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1038 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1040 return ldap_back_dobind_int( lc, op, rs, sendok, li->li_nretries, 1 );
1044 * ldap_back_default_rebind
1046 * This is a callback used for chasing referrals using the same
1047 * credentials as the original user on this session.
1050 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1051 ber_int_t msgid, void *params )
1053 ldapconn_t *lc = (ldapconn_t *)params;
1056 /* ... otherwise we couldn't get here */
1057 assert( lc != NULL );
1059 if ( !ldap_tls_inplace( ld ) ) {
1060 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1062 const char *text = NULL;
1064 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1065 LDAP_BACK_RETRY_DEFAULT, &text );
1066 if ( rc != LDAP_SUCCESS ) {
1070 #endif /* HAVE_TLS */
1072 /* FIXME: add checks on the URL/identity? */
1074 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
1075 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1079 * ldap_back_default_urllist
1082 ldap_back_default_urllist(
1084 LDAPURLDesc **urllist,
1088 ldapinfo_t *li = (ldapinfo_t *)params;
1089 LDAPURLDesc **urltail;
1091 if ( urllist == url ) {
1092 return LDAP_SUCCESS;
1095 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1098 *urltail = *urllist;
1102 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1104 ch_free( li->li_uri );
1107 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1108 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1110 return LDAP_SUCCESS;
1114 ldap_back_op_result(
1120 ldap_back_send_t sendok )
1123 LDAPMessage *res = NULL;
1126 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1129 rs->sr_matched = NULL;
1131 /* if the error recorded in the reply corresponds
1132 * to a successful state, get the error from the
1133 * remote server response */
1134 if ( ERR_OK( rs->sr_err ) ) {
1139 tv.tv_sec = timeout;
1143 LDAP_BACK_TV_SET( &tv );
1147 /* if result parsing fails, note the failure reason */
1148 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1152 (void)ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1153 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
1154 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
1155 rs->sr_text = "Operation timed out";
1159 LDAP_BACK_TV_SET( &tv );
1160 ldap_pvt_thread_yield();
1164 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1169 /* otherwise get the result; if it is not
1170 * LDAP_SUCCESS, record it in the reply
1171 * structure (this includes
1172 * LDAP_COMPARE_{TRUE|FALSE}) */
1174 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1175 &match, &text, NULL, NULL, 1 );
1177 if ( rc != LDAP_SUCCESS ) {
1183 /* if the error in the reply structure is not
1184 * LDAP_SUCCESS, try to map it from client
1185 * to server error */
1186 if ( !ERR_OK( rs->sr_err ) ) {
1187 rs->sr_err = slap_map_api2result( rs );
1189 /* internal ops ( op->o_conn == NULL )
1190 * must not reply to client */
1191 if ( op->o_conn && !op->o_do_not_cache && match ) {
1193 /* record the (massaged) matched
1194 * DN into the reply structure */
1195 rs->sr_matched = match;
1199 ( ( sendok & LDAP_BACK_SENDOK )
1200 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1202 send_ldap_result( op, rs );
1205 if ( rs->sr_matched != match ) {
1206 free( (char *)rs->sr_matched );
1208 rs->sr_matched = NULL;
1209 ldap_memfree( match );
1212 ldap_memfree( text );
1215 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1218 /* return true if bound, false if failed */
1220 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1223 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1225 assert( lcp != NULL );
1226 assert( *lcp != NULL );
1228 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1230 if ( (*lcp)->lc_refcnt == 1 ) {
1231 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1232 Debug( LDAP_DEBUG_ANY,
1233 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1234 op->o_log_prefix, li->li_uri,
1235 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1236 "" : (*lcp)->lc_bound_ndn.bv_val );
1237 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1239 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1240 (*lcp)->lc_ld = NULL;
1241 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1243 /* lc here must be the regular lc, reset and ready for init */
1244 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1245 if ( rc != LDAP_SUCCESS ) {
1250 rc = ldap_back_dobind_int( *lcp, op, rs, sendok, 0, 0 );
1257 Debug( LDAP_DEBUG_TRACE,
1258 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1259 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1261 ldap_back_release_conn_lock( op, rs, *lcp, 0 );
1265 rs->sr_err = LDAP_UNAVAILABLE;
1266 rs->sr_text = "unable to retry";
1267 send_ldap_result( op, rs );
1271 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1277 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1279 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1280 struct berval binddn = slap_empty_bv;
1281 struct berval bindcred = slap_empty_bv;
1287 /* don't proxyAuthz if protocol is not LDAPv3 */
1288 switch ( li->li_version ) {
1293 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1302 if ( op->o_tag == LDAP_REQ_BIND ) {
1303 ndn = op->o_req_ndn;
1305 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1306 ndn = op->o_conn->c_ndn;
1313 * FIXME: we need to let clients use proxyAuthz
1314 * otherwise we cannot do symmetric pools of servers;
1315 * we have to live with the fact that a user can
1316 * authorize itself as any ID that is allowed
1317 * by the authzTo directive of the "proxyauthzdn".
1320 * NOTE: current Proxy Authorization specification
1321 * and implementation do not allow proxy authorization
1322 * control to be provided with Bind requests
1325 * if no bind took place yet, but the connection is bound
1326 * and the "proxyauthzdn" is set, then bind as
1327 * "proxyauthzdn" and explicitly add the proxyAuthz
1328 * control to every operation with the dn bound
1329 * to the connection as control value.
1332 /* bind as proxyauthzdn only if no idassert mode
1333 * is requested, or if the client's identity
1335 switch ( li->li_idassert_mode ) {
1336 case LDAP_BACK_IDASSERT_LEGACY:
1337 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1338 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1340 binddn = li->li_idassert_authcDN;
1341 bindcred = li->li_idassert_passwd;
1348 /* NOTE: rootdn can always idassert */
1349 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1350 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1351 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1352 if ( sendok & LDAP_BACK_SENDERR ) {
1353 send_ldap_result( op, rs );
1355 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1358 rs->sr_err = LDAP_SUCCESS;
1359 binddn = slap_empty_bv;
1360 bindcred = slap_empty_bv;
1366 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1367 struct berval authcDN;
1369 if ( BER_BVISNULL( &ndn ) ) {
1370 authcDN = slap_empty_bv;
1375 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1376 &authcDN, &authcDN );
1377 if ( rs->sr_err != LDAP_SUCCESS ) {
1378 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1379 if ( sendok & LDAP_BACK_SENDERR ) {
1380 send_ldap_result( op, rs );
1382 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1385 rs->sr_err = LDAP_SUCCESS;
1386 binddn = slap_empty_bv;
1387 bindcred = slap_empty_bv;
1395 binddn = li->li_idassert_authcDN;
1396 bindcred = li->li_idassert_passwd;
1401 if ( dobind && li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1402 #ifdef HAVE_CYRUS_SASL
1403 void *defaults = NULL;
1404 struct berval authzID = BER_BVNULL;
1407 /* if SASL supports native authz, prepare for it */
1408 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1409 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1411 switch ( li->li_idassert_mode ) {
1412 case LDAP_BACK_IDASSERT_OTHERID:
1413 case LDAP_BACK_IDASSERT_OTHERDN:
1414 authzID = li->li_idassert_authzID;
1417 case LDAP_BACK_IDASSERT_ANONYMOUS:
1418 BER_BVSTR( &authzID, "dn:" );
1421 case LDAP_BACK_IDASSERT_SELF:
1422 if ( BER_BVISNULL( &ndn ) ) {
1423 /* connection is not authc'd, so don't idassert */
1424 BER_BVSTR( &authzID, "dn:" );
1427 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1428 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1429 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1430 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1431 ndn.bv_val, ndn.bv_len + 1 );
1440 if ( li->li_idassert_secprops != NULL ) {
1441 rs->sr_err = ldap_set_option( lc->lc_ld,
1442 LDAP_OPT_X_SASL_SECPROPS,
1443 (void *)li->li_idassert_secprops );
1445 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1446 rs->sr_err = LDAP_OTHER;
1447 if ( sendok & LDAP_BACK_SENDERR ) {
1448 send_ldap_result( op, rs );
1450 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1455 defaults = lutil_sasl_defaults( lc->lc_ld,
1456 li->li_idassert_sasl_mech.bv_val,
1457 li->li_idassert_sasl_realm.bv_val,
1458 li->li_idassert_authcID.bv_val,
1459 li->li_idassert_passwd.bv_val,
1462 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1463 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1464 LDAP_SASL_QUIET, lutil_sasl_interact,
1467 rs->sr_err = slap_map_api2result( rs );
1468 if ( rs->sr_err != LDAP_SUCCESS ) {
1469 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1470 if ( sendok & LDAP_BACK_SENDERR ) {
1471 send_ldap_result( op, rs );
1475 LDAP_BACK_CONN_ISBOUND_SET( lc );
1478 lutil_sasl_freedefs( defaults );
1480 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1484 #endif /* HAVE_CYRUS_SASL */
1487 switch ( li->li_idassert_authmethod ) {
1488 case LDAP_AUTH_NONE:
1489 LDAP_BACK_CONN_ISBOUND_SET( lc );
1492 case LDAP_AUTH_SIMPLE:
1493 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1494 binddn.bv_val, LDAP_SASL_SIMPLE,
1495 &bindcred, NULL, NULL, &msgid );
1500 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1501 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1502 if ( sendok & LDAP_BACK_SENDERR ) {
1503 send_ldap_result( op, rs );
1508 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1509 if ( rc == LDAP_SUCCESS ) {
1510 LDAP_BACK_CONN_ISBOUND_SET( lc );
1513 return LDAP_BACK_CONN_ISBOUND( lc );
1517 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1518 * to existing server-side controls if required; if not,
1519 * the existing server-side controls are placed in *pctrls.
1520 * The caller, after using the controls in client API
1521 * operations, if ( *pctrls != op->o_ctrls ), should
1522 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1523 * The function returns success if the control could
1524 * be added if required, or if it did nothing; in the future,
1525 * it might return some error if it failed.
1527 * if no bind took place yet, but the connection is bound
1528 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1529 * and explicitly add proxyAuthz the control to every operation
1530 * with the dn bound to the connection as control value.
1532 * If no server-side controls are defined for the operation,
1533 * simply add the proxyAuthz control; otherwise, if the
1534 * proxyAuthz control is not already set, add it as
1537 * FIXME: is controls order significant for security?
1538 * ANSWER: controls ordering and interoperability
1539 * must be indicated by the specs of each control; if none
1540 * is specified, the order is irrelevant.
1543 ldap_back_proxy_authz_ctrl(
1547 LDAPControl ***pctrls )
1549 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
1550 LDAPControl **ctrls = NULL;
1553 struct berval assertedID,
1558 rs->sr_err = LDAP_SUCCESS;
1560 /* don't proxyAuthz if protocol is not LDAPv3 */
1561 switch ( li->li_version ) {
1566 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1575 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1576 * but if it is not set this test fails. We need a different
1577 * means to detect if idassert is enabled */
1578 if ( ( BER_BVISNULL( &li->li_idassert_authcID ) || BER_BVISEMPTY( &li->li_idassert_authcID ) )
1579 && ( BER_BVISNULL( &li->li_idassert_authcDN ) || BER_BVISEMPTY( &li->li_idassert_authcDN ) ) )
1584 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1588 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1589 ndn = op->o_conn->c_ndn;
1595 if ( li->li_idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1596 if ( op->o_proxy_authz ) {
1598 * FIXME: we do not want to perform proxyAuthz
1599 * on behalf of the client, because this would
1600 * be performed with "proxyauthzdn" privileges.
1602 * This might actually be too strict, since
1603 * the "proxyauthzdn" authzTo, and each entry's
1604 * authzFrom attributes may be crafted
1605 * to avoid unwanted proxyAuthz to take place.
1608 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1609 rs->sr_text = "proxyAuthz not allowed within namingContext";
1614 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1618 if ( BER_BVISNULL( &ndn ) ) {
1622 if ( BER_BVISNULL( &li->li_idassert_authcDN ) ) {
1626 } else if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1627 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1628 /* && ( !BER_BVISNULL( &ndn )
1629 || LDAP_BACK_CONN_ISBOUND( lc ) ) */ )
1631 /* already asserted in SASL via native authz */
1632 /* NOTE: the test on lc->lc_bound is used to trap
1633 * native authorization of anonymous users,
1634 * since in that case ndn is NULL */
1638 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1640 struct berval authcDN;
1642 if ( BER_BVISNULL( &ndn ) ) {
1643 authcDN = slap_empty_bv;
1647 rc = slap_sasl_matches( op, li->li_idassert_authz,
1648 &authcDN, & authcDN );
1649 if ( rc != LDAP_SUCCESS ) {
1650 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1652 /* ndn is not authorized
1653 * to use idassert */
1660 if ( op->o_proxy_authz ) {
1663 * 1) ignore the already set proxyAuthz control
1664 * 2) leave it in place, and don't set ours
1666 * 4) reject the operation
1668 * option (4) is very drastic
1669 * option (3) will make the remote server reject
1670 * the operation, thus being equivalent to (4)
1671 * option (2) will likely break the idassert
1672 * assumptions, so we cannot accept it;
1673 * option (1) means that we are contradicting
1674 * the client's reques.
1676 * I think (4) is the only correct choice.
1678 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1679 rs->sr_text = "proxyAuthz not allowed within namingContext";
1682 if ( op->o_is_auth_check ) {
1683 mode = LDAP_BACK_IDASSERT_NOASSERT;
1686 mode = li->li_idassert_mode;
1690 case LDAP_BACK_IDASSERT_SELF:
1691 if ( BER_BVISNULL( &ndn ) ) {
1697 case LDAP_BACK_IDASSERT_LEGACY:
1698 /* original behavior:
1699 * assert the client's identity */
1700 if ( BER_BVISNULL( &ndn ) ) {
1701 assertedID = slap_empty_bv;
1707 case LDAP_BACK_IDASSERT_ANONYMOUS:
1708 /* assert "anonymous" */
1709 assertedID = slap_empty_bv;
1712 case LDAP_BACK_IDASSERT_NOASSERT:
1713 /* don't assert; bind as proxyauthzdn */
1716 case LDAP_BACK_IDASSERT_OTHERID:
1717 case LDAP_BACK_IDASSERT_OTHERDN:
1718 /* assert idassert DN */
1719 assertedID = li->li_idassert_authzID;
1726 if ( BER_BVISNULL( &assertedID ) ) {
1727 assertedID = slap_empty_bv;
1730 /* don't idassert the bound DN (ITS#4497) */
1731 if ( dn_match( &assertedID, &lc->lc_bound_ndn ) ) {
1735 if ( op->o_ctrls ) {
1736 for ( i = 0; op->o_ctrls[ i ]; i++ )
1737 /* just count ctrls */ ;
1740 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
1742 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
1744 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1745 ctrls[ 0 ]->ldctl_iscritical = 1;
1747 switch ( li->li_idassert_mode ) {
1748 /* already in u:ID or dn:DN form */
1749 case LDAP_BACK_IDASSERT_OTHERID:
1750 case LDAP_BACK_IDASSERT_OTHERDN:
1751 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
1754 /* needs the dn: prefix */
1756 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1757 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
1759 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1760 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1761 assertedID.bv_val, assertedID.bv_len + 1 );
1765 /* Older versions of <draft-weltman-ldapv3-proxy> required
1766 * to encode the value of the authzID (and called it proxyDN);
1767 * this hack provides compatibility with those DSAs that
1768 * implement it this way */
1769 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
1770 struct berval authzID = ctrls[ 0 ]->ldctl_value;
1771 BerElementBuffer berbuf;
1772 BerElement *ber = (BerElement *)&berbuf;
1775 ber_init2( ber, 0, LBER_USE_DER );
1776 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1778 tag = ber_printf( ber, "O", &authzID );
1779 if ( tag == LBER_ERROR ) {
1780 rs->sr_err = LDAP_OTHER;
1784 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1785 rs->sr_err = LDAP_OTHER;
1790 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1791 ber_free_buf( ber );
1793 if ( rs->sr_err != LDAP_SUCCESS ) {
1794 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1799 } else if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
1800 struct berval authzID = ctrls[ 0 ]->ldctl_value,
1802 BerElementBuffer berbuf;
1803 BerElement *ber = (BerElement *)&berbuf;
1806 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
1807 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1808 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1810 rs->sr_err = LDAP_PROTOCOL_ERROR;
1815 tmp.bv_val += STRLENOF( "dn:" );
1816 tmp.bv_len -= STRLENOF( "dn:" );
1818 ber_init2( ber, 0, LBER_USE_DER );
1819 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1821 /* apparently, Mozilla API encodes this
1822 * as "SEQUENCE { LDAPDN }" */
1823 tag = ber_printf( ber, "{O}", &tmp );
1824 if ( tag == LBER_ERROR ) {
1825 rs->sr_err = LDAP_OTHER;
1829 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1830 rs->sr_err = LDAP_OTHER;
1835 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1836 ber_free_buf( ber );
1838 if ( rs->sr_err != LDAP_SUCCESS ) {
1839 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1844 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1847 if ( op->o_ctrls ) {
1848 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1849 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1852 ctrls[ i + 1 ] = NULL;
1855 if ( ctrls == NULL ) {
1856 ctrls = op->o_ctrls;
1865 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1867 LDAPControl **ctrls = *pctrls;
1869 /* we assume that the first control is the proxyAuthz
1870 * added by back-ldap, so it's the only one we explicitly
1872 if ( ctrls && ctrls != op->o_ctrls ) {
1873 assert( ctrls[ 0 ] != NULL );
1875 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1876 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1879 op->o_tmpfree( ctrls, op->o_tmpmemctx );