1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2004 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 the Howard Chu for inclusion
20 * in OpenLDAP Software and subsequently enhanced by Pierangelo
28 #include <ac/socket.h>
29 #include <ac/string.h>
33 #include "back-ldap.h"
35 #include <lutil_ldap.h>
37 #define PRINT_CONNTREE 0
39 static LDAP_REBIND_PROC ldap_back_rebind;
46 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
49 struct berval mdn = BER_BVNULL;
54 lc = ldap_back_getconn(op, rs);
60 * Rewrite the bind dn if needed
62 dc.rwmap = &li->rwmap;
71 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
72 send_ldap_result( op, rs );
76 if ( !BER_BVISNULL( &lc->bound_dn ) ) {
77 ch_free( lc->bound_dn.bv_val );
78 BER_BVZERO( &lc->bound_dn );
81 /* method is always LDAP_AUTH_SIMPLE if we got here */
82 rs->sr_err = ldap_sasl_bind(lc->ld, mdn.bv_val, LDAP_SASL_SIMPLE,
83 &op->oq_bind.rb_cred, op->o_ctrls, NULL, &msgid);
84 rc = ldap_back_op_result( lc, op, rs, msgid, 1 );
85 if (rc == LDAP_SUCCESS) {
87 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
90 ber_dupbv( &lc->bound_dn, &op->o_req_dn );
95 if ( !BER_BVISNULL( &lc->cred ) ) {
96 memset( lc->cred.bv_val, 0, lc->cred.bv_len );
97 ch_free( lc->cred.bv_val );
99 ber_dupbv( &lc->cred, &op->oq_bind.rb_cred );
100 ldap_set_rebind_proc( lc->ld, ldap_back_rebind, lc );
104 /* must re-insert if local DN changed as result of bind */
105 if ( lc->bound && !bvmatch(&op->o_req_ndn, &lc->local_dn ) ) {
108 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
109 lc = avl_delete( &li->conntree, (caddr_t)lc,
110 ldap_back_conn_cmp );
111 if ( !BER_BVISNULL( &lc->local_dn ) )
112 ch_free( lc->local_dn.bv_val );
113 ber_dupbv( &lc->local_dn, &op->o_req_ndn );
114 lerr = avl_insert( &li->conntree, (caddr_t)lc,
115 ldap_back_conn_cmp, ldap_back_conn_dup );
116 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
118 ldap_back_conn_free( lc );
122 if ( !BER_BVISNULL( &mdn ) && mdn.bv_val != op->o_req_dn.bv_val ) {
132 * compares two struct ldapconn based on the value of the conn pointer;
141 const struct ldapconn *lc1 = (const struct ldapconn *)c1;
142 const struct ldapconn *lc2 = (const struct ldapconn *)c2;
145 /* If local DNs don't match, it is definitely not a match */
146 if ( ( rc = ber_bvcmp( &lc1->local_dn, &lc2->local_dn )) )
149 /* For shared sessions, conn is NULL. Only explicitly
150 * bound sessions will have non-NULL conn.
152 return SLAP_PTRCMP(lc1->conn, lc2->conn);
158 * returns -1 in case a duplicate struct ldapconn has been inserted;
167 struct ldapconn *lc1 = (struct ldapconn *)c1;
168 struct ldapconn *lc2 = (struct ldapconn *)c2;
170 /* Cannot have more than one shared session with same DN */
171 if ( dn_match( &lc1->local_dn, &lc2->local_dn ) &&
172 lc1->conn == lc2->conn ) return -1;
177 #if PRINT_CONNTREE > 0
178 static void ravl_print( Avlnode *root, int depth )
186 ravl_print( root->avl_right, depth+1 );
188 for ( i = 0; i < depth; i++ )
192 printf( "lc(%lx) local(%s) conn(%lx) %d\n",
193 lc, lc->local_dn.bv_val, lc->conn, root->avl_bf );
195 ravl_print( root->avl_left, depth+1 );
198 static void myprint( Avlnode *root )
200 printf( "********\n" );
203 printf( "\tNULL\n" );
206 ravl_print( root, 0 );
208 printf( "********\n" );
210 #endif /* PRINT_CONNTREE */
213 ldap_back_getconn(Operation *op, SlapReply *rs)
215 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
216 struct ldapconn *lc, lc_curr;
220 /* Searches for a ldapconn in the avl tree */
222 /* Explicit binds must not be shared */
223 if ( op->o_tag == LDAP_REQ_BIND
225 && (op->o_bd == op->o_conn->c_authz_backend ))) {
226 lc_curr.conn = op->o_conn;
231 /* Internal searches are privileged and shared. So is root. */
232 if ( op->o_do_not_cache || be_isroot_dn( li->be, &op->o_ndn ) ) {
233 lc_curr.local_dn = li->be->be_rootndn;
237 lc_curr.local_dn = op->o_ndn;
240 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
241 lc = (struct ldapconn *)avl_find( li->conntree,
242 (caddr_t)&lc_curr, ldap_back_conn_cmp );
243 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
245 /* Looks like we didn't get a bind. Open a new session... */
247 int vers = op->o_protocol;
248 rs->sr_err = ldap_initialize(&ld, li->url);
250 if (rs->sr_err != LDAP_SUCCESS) {
251 rs->sr_err = slap_map_api2result( rs );
252 if (rs->sr_text == NULL) {
253 rs->sr_text = "ldap_initialize() failed";
255 if (op->o_conn) send_ldap_result( op, rs );
259 /* Set LDAP version. This will always succeed: If the client
260 * bound with a particular version, then so can we.
262 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION,
263 (const void *)&vers);
264 /* FIXME: configurable? */
265 ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON);
267 lc = (struct ldapconn *)ch_malloc(sizeof(struct ldapconn));
268 lc->conn = lc_curr.conn;
270 ber_dupbv( &lc->local_dn, &lc_curr.local_dn );
272 #ifdef ENABLE_REWRITE
274 * Sets a cookie for the rewrite session
276 * FIXME: the o_conn might be no longer valid,
277 * since we may have different entries
278 * for the same connection
280 ( void )rewrite_session_init( li->rwmap.rwm_rw, op->o_conn );
281 #endif /* ENABLE_REWRITE */
283 ldap_pvt_thread_mutex_init( &lc->lc_mutex );
286 ber_dupbv( &lc->cred, &li->bindpw );
287 ber_dupbv( &lc->bound_dn, &li->binddn );
289 BER_BVZERO( &lc->cred );
290 BER_BVZERO( &lc->bound_dn );
291 if ( op->o_conn && !BER_BVISEMPTY( &op->o_conn->c_dn )
292 && ( op->o_bd == op->o_conn->c_authz_backend ) ) {
298 * Rewrite the bind dn if needed
300 dc.rwmap = &li->rwmap;
301 #ifdef ENABLE_REWRITE
302 dc.conn = op->o_conn;
310 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn, &bv ) ) {
311 send_ldap_result( op, rs );
315 if ( bv.bv_val == op->o_conn->c_dn.bv_val ) {
316 ber_dupbv( &lc->bound_dn, &bv );
325 /* Inserts the newly created ldapconn in the avl tree */
326 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
327 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
328 ldap_back_conn_cmp, ldap_back_conn_dup );
330 #if PRINT_CONNTREE > 0
331 myprint( li->conntree );
332 #endif /* PRINT_CONNTREE */
334 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
337 LDAP_LOG( BACK_LDAP, INFO,
338 "ldap_back_getconn: conn %p inserted\n", (void *) lc, 0, 0);
339 #else /* !NEW_LOGGING */
340 Debug( LDAP_DEBUG_TRACE,
341 "=>ldap_back_getconn: conn %p inserted\n", (void *) lc, 0, 0 );
342 #endif /* !NEW_LOGGING */
344 /* Err could be -1 in case a duplicate ldapconn is inserted */
345 if ( rs->sr_err != 0 ) {
346 ldap_back_conn_free( lc );
348 send_ldap_error( op, rs, LDAP_OTHER,
349 "internal server error" );
355 LDAP_LOG( BACK_LDAP, INFO,
356 "ldap_back_getconn: conn %p fetched\n",
358 #else /* !NEW_LOGGING */
359 Debug( LDAP_DEBUG_TRACE,
360 "=>ldap_back_getconn: conn %p fetched\n", (void *) lc, 0, 0 );
361 #endif /* !NEW_LOGGING */
370 * Note: as the check for the value of lc->bound was already here, I removed
371 * it from all the callers, and I made the function return the flag, so
372 * it can be used to simplify the check.
375 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs )
377 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
381 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
383 #ifdef LDAP_BACK_PROXY_AUTHZ
385 * FIXME: we need to let clients use proxyAuthz
386 * otherwise we cannot do symmetric pools of servers;
387 * we have to live with the fact that a user can
388 * authorize itself as any ID that is allowed
389 * by the saslAuthzTo directive of the "proxyauthzdn".
392 * NOTE: current Proxy Authorization specification
393 * and implementation do not allow proxy authorization
394 * control to be provided with Bind requests
397 * if no bind took place yet, but the connection is bound
398 * and the "proxyauthzdn" is set, then bind as
399 * "proxyauthzdn" and explicitly add the proxyAuthz
400 * control to every operation with the dn bound
401 * to the connection as control value.
403 if ( op->o_conn != NULL
404 && ( BER_BVISNULL( &lc->bound_dn ) || BER_BVISEMPTY( &lc->bound_dn ) ) ) {
405 struct berval binddn = slap_empty_bv;
406 struct berval bindcred = slap_empty_bv;
408 /* bind as proxyauthzdn only if no idassert mode is requested,
409 * or if the client's identity is authorized */
410 switch ( li->idassert_mode ) {
411 case LDAP_BACK_IDASSERT_LEGACY:
412 if ( !BER_BVISNULL( &op->o_conn->c_dn ) && !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
413 if ( li->idassert_authmethod != LDAP_AUTH_SASL
414 && !BER_BVISNULL( &li->idassert_authcDN ) && !BER_BVISEMPTY( &li->idassert_authcDN ) )
416 binddn = li->idassert_authcDN;
417 bindcred = li->idassert_passwd;
423 if ( li->idassert_authz ) {
424 struct berval authcDN = BER_BVISNULL( &op->o_conn->c_dn ) ? slap_empty_bv : op->o_conn->c_dn;
426 rs->sr_err = slap_sasl_matches( op, li->idassert_authz,
427 &authcDN, &authcDN );
428 if ( rs->sr_err != LDAP_SUCCESS ) {
429 send_ldap_result( op, rs );
435 if ( li->idassert_authmethod != LDAP_AUTH_SASL ) {
436 binddn = li->idassert_authcDN;
438 bindcred = li->idassert_passwd;
442 /* NOTE: essentially copied from clients/tools/common.c :) */
443 switch ( li->idassert_authmethod ) {
444 #ifdef HAVE_CYRUS_SASL
447 void *defaults = NULL;
448 struct berval authzID = BER_BVNULL;
450 #if 0 /* will deal with this later... */
451 if ( sasl_secprops != NULL ) {
452 rs->sr_err = ldap_set_option( lc->ld, LDAP_OPT_X_SASL_SECPROPS,
453 (void *) sasl_secprops );
455 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
456 send_ldap_result( op, rs );
464 switch ( li->idassert_mode ) {
465 case LDAP_BACK_IDASSERT_OTHERID:
466 case LDAP_BACK_IDASSERT_OTHERDN:
467 authzID = li->idassert_authzID;
470 defaults = lutil_sasl_defaults( lc->ld,
471 li->idassert_sasl_mech.bv_val,
472 li->idassert_sasl_realm.bv_val,
473 li->idassert_authcID.bv_val,
474 li->idassert_passwd.bv_val,
477 rs->sr_err = ldap_sasl_interactive_bind_s( lc->ld, NULL,
478 li->idassert_sasl_mech.bv_val, NULL, NULL,
479 li->idassert_sasl_flags, lutil_sasl_interact,
482 lutil_sasl_freedefs( defaults );
484 rs->sr_err = slap_map_api2result( rs );
485 if ( rs->sr_err != LDAP_SUCCESS ) {
487 send_ldap_result( op, rs );
494 #endif /* HAVE_CYRUS_SASL */
496 case LDAP_AUTH_SIMPLE:
497 rs->sr_err = ldap_sasl_bind(lc->ld,
498 binddn.bv_val, LDAP_SASL_SIMPLE,
499 &bindcred, NULL, NULL, &msgid);
509 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
510 send_ldap_result( op, rs );
515 #endif /* LDAP_BACK_PROXY_AUTHZ */
517 rs->sr_err = ldap_sasl_bind(lc->ld, lc->bound_dn.bv_val,
518 LDAP_SASL_SIMPLE, &lc->cred, NULL, NULL, &msgid);
521 rc = ldap_back_op_result( lc, op, rs, msgid, 0 );
522 if (rc == LDAP_SUCCESS) {
529 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
536 * This is a callback used for chasing referrals using the same
537 * credentials as the original user on this session.
540 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
541 ber_int_t msgid, void *params )
543 struct ldapconn *lc = params;
545 return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val, LDAP_AUTH_SIMPLE );
548 #if 0 /* deprecated in favour of slap_map_api2result() */
549 /* Map API errors to protocol errors... */
551 ldap_back_map_result( SlapReply *rs )
555 case LDAP_SERVER_DOWN:
556 return LDAP_UNAVAILABLE;
557 case LDAP_LOCAL_ERROR:
559 case LDAP_ENCODING_ERROR:
560 case LDAP_DECODING_ERROR:
561 return LDAP_PROTOCOL_ERROR;
563 return LDAP_UNAVAILABLE;
564 case LDAP_AUTH_UNKNOWN:
565 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
566 case LDAP_FILTER_ERROR:
567 rs->sr_text = "Filter error";
569 case LDAP_USER_CANCELLED:
570 rs->sr_text = "User cancelled";
572 case LDAP_PARAM_ERROR:
573 return LDAP_PROTOCOL_ERROR;
576 case LDAP_CONNECT_ERROR:
577 return LDAP_UNAVAILABLE;
578 case LDAP_NOT_SUPPORTED:
579 return LDAP_UNWILLING_TO_PERFORM;
580 case LDAP_CONTROL_NOT_FOUND:
581 return LDAP_PROTOCOL_ERROR;
582 case LDAP_NO_RESULTS_RETURNED:
583 return LDAP_NO_SUCH_OBJECT;
584 case LDAP_MORE_RESULTS_TO_RETURN:
585 rs->sr_text = "More results to return";
587 case LDAP_CLIENT_LOOP:
588 case LDAP_REFERRAL_LIMIT_EXCEEDED:
589 return LDAP_LOOP_DETECT;
591 if ( LDAP_API_ERROR(rs->sr_err) )
599 ldap_back_op_result(struct ldapconn *lc, Operation *op, SlapReply *rs,
600 ber_int_t msgid, int sendok)
602 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
604 LDAPMessage *res = NULL;
607 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
610 rs->sr_matched = NULL;
612 /* if the error recorded in the reply corresponds
613 * to a successful state, get the error from the
614 * remote server response */
615 if ( ERR_OK( rs->sr_err ) ) {
616 /* if result parsing fails, note the failure reason */
617 if ( ldap_result( lc->ld, msgid, 1, NULL, &res ) == -1 ) {
618 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER,
621 /* otherwise get the result; if it is not
622 * LDAP_SUCCESS, record it in the reply
623 * structure (this includes
624 * LDAP_COMPARE_{TRUE|FALSE}) */
626 int rc = ldap_parse_result(lc->ld, res, &rs->sr_err,
627 &match, &text, NULL, NULL, 1);
629 if ( rc != LDAP_SUCCESS ) rs->sr_err = rc;
633 /* if the error in the reply structure is not
634 * LDAP_SUCCESS, try to map it from client
636 if ( !ERR_OK( rs->sr_err ) ) {
637 rs->sr_err = slap_map_api2result( rs );
639 /* internal ops ( op->o_conn == NULL )
640 * must not reply to client */
641 if ( op->o_conn && !op->o_do_not_cache && match ) {
642 struct berval dn, mdn;
645 dc.rwmap = &li->rwmap;
646 #ifdef ENABLE_REWRITE
647 dc.conn = op->o_conn;
649 dc.ctx = "matchedDN";
654 ber_str2bv(match, 0, 0, &dn);
655 ldap_back_dn_massage(&dc, &dn, &mdn);
657 /* record the (massaged) matched
658 * DN into the reply structure */
659 rs->sr_matched = mdn.bv_val;
663 if ( op->o_conn && ( sendok || rs->sr_err != LDAP_SUCCESS ) ) {
664 send_ldap_result( op, rs );
667 if ( rs->sr_matched != match ) {
668 free( (char *)rs->sr_matched );
670 rs->sr_matched = NULL;
671 ldap_memfree( match );
674 ldap_memfree( text );
677 return( ERR_OK( rs->sr_err ) ? 0 : -1 );
680 #ifdef LDAP_BACK_PROXY_AUTHZ
682 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
683 * to existing server-side controls if required; if not,
684 * the existing server-side controls are placed in *pctrls.
685 * The caller, after using the controls in client API
686 * operations, if ( *pctrls != op->o_ctrls ), should
687 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
688 * The function returns success if the control could
689 * be added if required, or if it did nothing; in the future,
690 * it might return some error if it failed.
692 * if no bind took place yet, but the connection is bound
693 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
694 * and explicitly add proxyAuthz the control to every operation
695 * with the dn bound to the connection as control value.
697 * If no server-side controls are defined for the operation,
698 * simply add the proxyAuthz control; otherwise, if the
699 * proxyAuthz control is not already set, add it as
700 * the first one (FIXME: is controls order significant
704 ldap_back_proxy_authz_ctrl(
708 LDAPControl ***pctrls )
710 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
711 LDAPControl **ctrls = NULL;
713 struct berval assertedID;
717 if ( BER_BVISNULL( &li->idassert_authcID ) ) {
725 if ( li->idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
726 if ( op->o_proxy_authz ) {
728 * FIXME: we do not want to perform proxyAuthz
729 * on behalf of the client, because this would
730 * be performed with "proxyauthzdn" privileges.
732 * This might actually be too strict, since
733 * the "proxyauthzdn" saslAuthzTo, and each entry's
734 * saslAuthzFrom attributes may be crafted
735 * to avoid unwanted proxyAuthz to take place.
738 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
739 rs->sr_text = "proxyAuthz not allowed within namingContext";
744 if ( !BER_BVISNULL( &lc->bound_dn ) && !BER_BVISEMPTY( &lc->bound_dn ) ) {
748 if ( BER_BVISNULL( &op->o_conn->c_dn ) || BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
752 if ( BER_BVISEMPTY( &li->idassert_authcID ) ) {
756 } else if ( li->idassert_mode == LDAP_BACK_IDASSERT_OTHERID && li->idassert_authmethod == LDAP_AUTH_SASL ) {
757 /* already asserted in SASL */
760 } else if ( li->idassert_authz ) {
762 struct berval authcDN = BER_BVISNULL( &op->o_conn->c_dn ) ? slap_empty_bv : op->o_conn->c_dn;
765 rc = slap_sasl_matches( op, li->idassert_authz,
766 &authcDN, & authcDN );
767 if ( rc != LDAP_SUCCESS ) {
768 /* op->o_conn->c_dn is not authorized
774 switch ( li->idassert_mode ) {
775 case LDAP_BACK_IDASSERT_LEGACY:
776 case LDAP_BACK_IDASSERT_SELF:
777 /* original behavior:
778 * assert the client's identity */
779 assertedID = op->o_conn->c_dn;
782 case LDAP_BACK_IDASSERT_ANONYMOUS:
783 /* assert "anonymous" */
784 assertedID = slap_empty_bv;
787 case LDAP_BACK_IDASSERT_NOASSERT:
788 /* don't assert; bind as proxyauthzdn */
791 case LDAP_BACK_IDASSERT_OTHERID:
792 case LDAP_BACK_IDASSERT_OTHERDN:
793 /* assert idassert DN */
794 assertedID = li->idassert_authzID;
801 if ( BER_BVISNULL( &assertedID ) ) {
802 assertedID = slap_empty_bv;
805 ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
806 ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
808 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
809 ctrls[ 0 ]->ldctl_iscritical = 1;
811 switch ( li->idassert_mode ) {
812 /* already in u:ID or dn:DN form */
813 case LDAP_BACK_IDASSERT_OTHERID:
814 case LDAP_BACK_IDASSERT_OTHERDN:
815 ber_dupbv( &ctrls[ 0 ]->ldctl_value, &assertedID );
818 /* needs the dn: prefix */
820 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
821 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
822 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
823 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val + STRLENOF( "dn:" ),
824 assertedID.bv_val, assertedID.bv_len + 1 );
829 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
830 ctrls[ i + 1 ] = op->o_ctrls[ i ];
833 ctrls[ i + 1 ] = NULL;
836 if ( ctrls == NULL ) {
844 #endif /* LDAP_BACK_PROXY_AUTHZ */