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>
34 #include "back-ldap.h"
36 #define PRINT_CONNTREE 0
38 static LDAP_REBIND_PROC ldap_back_rebind;
45 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
48 struct berval mdn = BER_BVNULL;
53 lc = ldap_back_getconn(op, rs);
59 * Rewrite the bind dn if needed
61 dc.rwmap = &li->rwmap;
70 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
71 send_ldap_result( op, rs );
75 if ( !BER_BVISNULL( &lc->bound_dn ) ) {
76 ch_free( lc->bound_dn.bv_val );
77 BER_BVZERO( &lc->bound_dn );
80 /* method is always LDAP_AUTH_SIMPLE if we got here */
81 rs->sr_err = ldap_sasl_bind(lc->ld, mdn.bv_val, LDAP_SASL_SIMPLE,
82 &op->oq_bind.rb_cred, op->o_ctrls, NULL, &msgid);
83 rc = ldap_back_op_result( lc, op, rs, msgid, 1 );
84 if (rc == LDAP_SUCCESS) {
86 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
89 ber_dupbv( &lc->bound_dn, &op->o_req_dn );
94 if ( !BER_BVISNULL( &lc->cred ) ) {
95 memset( lc->cred.bv_val, 0, lc->cred.bv_len );
96 ch_free( lc->cred.bv_val );
98 ber_dupbv( &lc->cred, &op->oq_bind.rb_cred );
99 ldap_set_rebind_proc( lc->ld, ldap_back_rebind, lc );
103 /* must re-insert if local DN changed as result of bind */
104 if ( lc->bound && !bvmatch(&op->o_req_ndn, &lc->local_dn ) ) {
107 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
108 lc = avl_delete( &li->conntree, (caddr_t)lc,
109 ldap_back_conn_cmp );
110 if ( !BER_BVISNULL( &lc->local_dn ) )
111 ch_free( lc->local_dn.bv_val );
112 ber_dupbv( &lc->local_dn, &op->o_req_ndn );
113 lerr = avl_insert( &li->conntree, (caddr_t)lc,
114 ldap_back_conn_cmp, ldap_back_conn_dup );
115 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
117 ldap_back_conn_free( lc );
121 if ( !BER_BVISNULL( &mdn ) && mdn.bv_val != op->o_req_dn.bv_val ) {
131 * compares two struct ldapconn based on the value of the conn pointer;
140 const struct ldapconn *lc1 = (const struct ldapconn *)c1;
141 const struct ldapconn *lc2 = (const struct ldapconn *)c2;
144 /* If local DNs don't match, it is definitely not a match */
145 if ( ( rc = ber_bvcmp( &lc1->local_dn, &lc2->local_dn )) )
148 /* For shared sessions, conn is NULL. Only explicitly
149 * bound sessions will have non-NULL conn.
151 return SLAP_PTRCMP(lc1->conn, lc2->conn);
157 * returns -1 in case a duplicate struct ldapconn has been inserted;
166 struct ldapconn *lc1 = (struct ldapconn *)c1;
167 struct ldapconn *lc2 = (struct ldapconn *)c2;
169 /* Cannot have more than one shared session with same DN */
170 if ( dn_match( &lc1->local_dn, &lc2->local_dn ) &&
171 lc1->conn == lc2->conn ) return -1;
176 #if PRINT_CONNTREE > 0
177 static void ravl_print( Avlnode *root, int depth )
185 ravl_print( root->avl_right, depth+1 );
187 for ( i = 0; i < depth; i++ )
191 printf( "lc(%lx) local(%s) conn(%lx) %d\n",
192 lc, lc->local_dn.bv_val, lc->conn, root->avl_bf );
194 ravl_print( root->avl_left, depth+1 );
197 static void myprint( Avlnode *root )
199 printf( "********\n" );
202 printf( "\tNULL\n" );
205 ravl_print( root, 0 );
207 printf( "********\n" );
209 #endif /* PRINT_CONNTREE */
212 ldap_back_getconn(Operation *op, SlapReply *rs)
214 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
215 struct ldapconn *lc, lc_curr;
219 /* Searches for a ldapconn in the avl tree */
221 /* Explicit binds must not be shared */
222 if ( op->o_tag == LDAP_REQ_BIND
224 && (op->o_bd == op->o_conn->c_authz_backend ))) {
225 lc_curr.conn = op->o_conn;
230 /* Internal searches are privileged and shared. So is root. */
231 if ( op->o_do_not_cache || be_isroot_dn( li->be, &op->o_ndn ) ) {
232 lc_curr.local_dn = li->be->be_rootndn;
236 lc_curr.local_dn = op->o_ndn;
239 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
240 lc = (struct ldapconn *)avl_find( li->conntree,
241 (caddr_t)&lc_curr, ldap_back_conn_cmp );
242 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
244 /* Looks like we didn't get a bind. Open a new session... */
246 int vers = op->o_protocol;
247 rs->sr_err = ldap_initialize(&ld, li->url);
249 if (rs->sr_err != LDAP_SUCCESS) {
250 rs->sr_err = slap_map_api2result( rs );
251 if (rs->sr_text == NULL) {
252 rs->sr_text = "ldap_initialize() failed";
254 if (op->o_conn) send_ldap_result( op, rs );
258 /* Set LDAP version. This will always succeed: If the client
259 * bound with a particular version, then so can we.
261 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION,
262 (const void *)&vers);
263 /* FIXME: configurable? */
264 ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON);
266 lc = (struct ldapconn *)ch_malloc(sizeof(struct ldapconn));
267 lc->conn = lc_curr.conn;
269 ber_dupbv( &lc->local_dn, &lc_curr.local_dn );
271 #ifdef ENABLE_REWRITE
273 * Sets a cookie for the rewrite session
275 * FIXME: the o_conn might be no longer valid,
276 * since we may have different entries
277 * for the same connection
279 ( void )rewrite_session_init( li->rwmap.rwm_rw, op->o_conn );
280 #endif /* ENABLE_REWRITE */
282 ldap_pvt_thread_mutex_init( &lc->lc_mutex );
285 ber_dupbv( &lc->cred, &li->bindpw );
286 ber_dupbv( &lc->bound_dn, &li->binddn );
288 BER_BVZERO( &lc->cred );
289 BER_BVZERO( &lc->bound_dn );
290 if ( op->o_conn && !BER_BVISEMPTY( &op->o_conn->c_dn )
291 && ( op->o_bd == op->o_conn->c_authz_backend ) ) {
297 * Rewrite the bind dn if needed
299 dc.rwmap = &li->rwmap;
300 #ifdef ENABLE_REWRITE
301 dc.conn = op->o_conn;
309 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn, &bv ) ) {
310 send_ldap_result( op, rs );
314 if ( bv.bv_val == op->o_conn->c_dn.bv_val ) {
315 ber_dupbv( &lc->bound_dn, &bv );
324 /* Inserts the newly created ldapconn in the avl tree */
325 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
326 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
327 ldap_back_conn_cmp, ldap_back_conn_dup );
329 #if PRINT_CONNTREE > 0
330 myprint( li->conntree );
331 #endif /* PRINT_CONNTREE */
333 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
336 LDAP_LOG( BACK_LDAP, INFO,
337 "ldap_back_getconn: conn %p inserted\n", (void *) lc, 0, 0);
338 #else /* !NEW_LOGGING */
339 Debug( LDAP_DEBUG_TRACE,
340 "=>ldap_back_getconn: conn %p inserted\n", (void *) lc, 0, 0 );
341 #endif /* !NEW_LOGGING */
343 /* Err could be -1 in case a duplicate ldapconn is inserted */
344 if ( rs->sr_err != 0 ) {
345 ldap_back_conn_free( lc );
347 send_ldap_error( op, rs, LDAP_OTHER,
348 "internal server error" );
354 LDAP_LOG( BACK_LDAP, INFO,
355 "ldap_back_getconn: conn %p fetched\n",
357 #else /* !NEW_LOGGING */
358 Debug( LDAP_DEBUG_TRACE,
359 "=>ldap_back_getconn: conn %p fetched\n", (void *) lc, 0, 0 );
360 #endif /* !NEW_LOGGING */
369 * Note: as the check for the value of lc->bound was already here, I removed
370 * it from all the callers, and I made the function return the flag, so
371 * it can be used to simplify the check.
374 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs )
376 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
380 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
382 #ifdef LDAP_BACK_PROXY_AUTHZ
386 * FIXME: we need to let clients use proxyAuthz
387 * otherwise we cannot do symmetric pools of servers;
388 * we have to live with the fact that a user can
389 * authorize itself as any ID that is allowed
390 * by the saslAuthzTo directive of the "proxyauthzdn".
393 * NOTE: current Proxy Authorization specification
394 * and implementation do not allow proxy authorization
395 * control to be provided with Bind requests
397 gotit = op->o_proxy_authz;
401 * if no bind took place yet, but the connection is bound
402 * and the "proxyauthzdn" is set, then bind as
403 * "proxyauthzdn" and explicitly add the proxyAuthz
404 * control to every operation with the dn bound
405 * to the connection as control value.
407 if ( op->o_conn != NULL
408 && ( BER_BVISNULL( &lc->bound_dn ) || BER_BVISEMPTY( &lc->bound_dn ) ) ) {
409 struct berval binddn = slap_empty_bv;
410 struct berval bindcred = slap_empty_bv;
412 /* bind as proxyauthzdn only if no idassert mode is requested,
413 * or if the client's identity is authorized */
414 switch ( li->idassert_mode ) {
415 case LDAP_BACK_IDASSERT_NONE:
416 if ( !BER_BVISNULL( &op->o_conn->c_dn ) && !BER_BVISEMPTY( &op->o_conn->c_dn )
417 && !BER_BVISNULL( &li->proxyauthzdn ) && !BER_BVISEMPTY( &li->proxyauthzdn )
419 binddn = li->proxyauthzdn;
420 bindcred = li->proxyauthzpw;
425 if ( li->idassert_authz ) {
426 struct berval authcDN = BER_BVISNULL( &op->o_conn->c_dn ) ? slap_empty_bv : op->o_conn->c_dn;
428 rc = slap_sasl_matches( op, li->idassert_authz,
429 &authcDN, &authcDN );
430 if ( rc != LDAP_SUCCESS ) {
434 binddn = li->proxyauthzdn;
435 bindcred = li->proxyauthzpw;
439 rs->sr_err = ldap_sasl_bind(lc->ld, binddn.bv_val,
440 LDAP_SASL_SIMPLE, &bindcred, NULL, NULL, &msgid);
443 #endif /* LDAP_BACK_PROXY_AUTHZ */
445 rs->sr_err = ldap_sasl_bind(lc->ld, lc->bound_dn.bv_val,
446 LDAP_SASL_SIMPLE, &lc->cred, NULL, NULL, &msgid);
449 rc = ldap_back_op_result( lc, op, rs, msgid, 0 );
450 if (rc == LDAP_SUCCESS) {
455 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
462 * This is a callback used for chasing referrals using the same
463 * credentials as the original user on this session.
466 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
467 ber_int_t msgid, void *params )
469 struct ldapconn *lc = params;
471 return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val, LDAP_AUTH_SIMPLE );
474 #if 0 /* deprecated in favour of slap_map_api2result() */
475 /* Map API errors to protocol errors... */
477 ldap_back_map_result( SlapReply *rs )
481 case LDAP_SERVER_DOWN:
482 return LDAP_UNAVAILABLE;
483 case LDAP_LOCAL_ERROR:
485 case LDAP_ENCODING_ERROR:
486 case LDAP_DECODING_ERROR:
487 return LDAP_PROTOCOL_ERROR;
489 return LDAP_UNAVAILABLE;
490 case LDAP_AUTH_UNKNOWN:
491 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
492 case LDAP_FILTER_ERROR:
493 rs->sr_text = "Filter error";
495 case LDAP_USER_CANCELLED:
496 rs->sr_text = "User cancelled";
498 case LDAP_PARAM_ERROR:
499 return LDAP_PROTOCOL_ERROR;
502 case LDAP_CONNECT_ERROR:
503 return LDAP_UNAVAILABLE;
504 case LDAP_NOT_SUPPORTED:
505 return LDAP_UNWILLING_TO_PERFORM;
506 case LDAP_CONTROL_NOT_FOUND:
507 return LDAP_PROTOCOL_ERROR;
508 case LDAP_NO_RESULTS_RETURNED:
509 return LDAP_NO_SUCH_OBJECT;
510 case LDAP_MORE_RESULTS_TO_RETURN:
511 rs->sr_text = "More results to return";
513 case LDAP_CLIENT_LOOP:
514 case LDAP_REFERRAL_LIMIT_EXCEEDED:
515 return LDAP_LOOP_DETECT;
517 if ( LDAP_API_ERROR(rs->sr_err) )
525 ldap_back_op_result(struct ldapconn *lc, Operation *op, SlapReply *rs,
526 ber_int_t msgid, int sendok)
528 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
530 LDAPMessage *res = NULL;
533 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
536 rs->sr_matched = NULL;
538 /* if the error recorded in the reply corresponds
539 * to a successful state, get the error from the
540 * remote server response */
541 if ( ERR_OK( rs->sr_err ) ) {
542 /* if result parsing fails, note the failure reason */
543 if ( ldap_result( lc->ld, msgid, 1, NULL, &res ) == -1 ) {
544 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER,
547 /* otherwise get the result; if it is not
548 * LDAP_SUCCESS, record it in the reply
549 * structure (this includes
550 * LDAP_COMPARE_{TRUE|FALSE}) */
552 int rc = ldap_parse_result(lc->ld, res, &rs->sr_err,
553 &match, &text, NULL, NULL, 1);
555 if ( rc != LDAP_SUCCESS ) rs->sr_err = rc;
559 /* if the error in the reply structure is not
560 * LDAP_SUCCESS, try to map it from client
562 if ( !ERR_OK( rs->sr_err ) ) {
563 rs->sr_err = slap_map_api2result( rs );
565 /* internal ops ( op->o_conn == NULL )
566 * must not reply to client */
567 if ( op->o_conn && !op->o_do_not_cache && match ) {
568 struct berval dn, mdn;
571 dc.rwmap = &li->rwmap;
572 #ifdef ENABLE_REWRITE
573 dc.conn = op->o_conn;
575 dc.ctx = "matchedDN";
580 ber_str2bv(match, 0, 0, &dn);
581 ldap_back_dn_massage(&dc, &dn, &mdn);
583 /* record the (massaged) matched
584 * DN into the reply structure */
585 rs->sr_matched = mdn.bv_val;
589 if ( op->o_conn && ( sendok || rs->sr_err != LDAP_SUCCESS ) ) {
590 send_ldap_result( op, rs );
593 if ( rs->sr_matched != match ) {
594 free( (char *)rs->sr_matched );
596 rs->sr_matched = NULL;
597 ldap_memfree( match );
600 ldap_memfree( text );
603 return( ERR_OK( rs->sr_err ) ? 0 : -1 );
606 #ifdef LDAP_BACK_PROXY_AUTHZ
608 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
609 * to existing server-side controls if required; if not,
610 * the existing server-side controls are placed in *pctrls.
611 * The caller, after using the controls in client API
612 * operations, if ( *pctrls != op->o_ctrls ), should
613 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
614 * The function returns success if the control could
615 * be added if required, or if it did nothing; in the future,
616 * it might return some error if it failed.
618 * if no bind took place yet, but the connection is bound
619 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
620 * and explicitly add proxyAuthz the control to every operation
621 * with the dn bound to the connection as control value.
623 * If no server-side controls are defined for the operation,
624 * simply add the proxyAuthz control; otherwise, if the
625 * proxyAuthz control is not already set, add it as
626 * the first one (FIXME: is controls order significant
630 ldap_back_proxy_authz_ctrl(
634 LDAPControl ***pctrls )
636 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
637 LDAPControl **ctrls = NULL;
639 struct berval assertedDN;
643 if ( BER_BVISNULL( &li->proxyauthzdn ) ) {
651 if ( li->idassert_mode == LDAP_BACK_IDASSERT_NONE ) {
652 if ( op->o_proxy_authz ) {
654 * FIXME: we do not want to perform proxyAuthz
655 * on behalf of the client, because this would
656 * be performed with "proxyauthzdn" privileges.
658 * This might actually be too strict, since
659 * the "proxyauthzdn" saslAuthzTo, and each entry's
660 * saslAuthzFrom attributes may be crafted
661 * to avoid unwanted proxyAuthz to take place.
664 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
665 rs->sr_text = "proxyAuthz not allowed within namingContext";
670 if ( !BER_BVISNULL( &lc->bound_dn ) && !BER_BVISEMPTY( &lc->bound_dn ) ) {
674 if ( BER_BVISNULL( &op->o_conn->c_dn ) || BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
678 if ( BER_BVISEMPTY( &li->proxyauthzdn ) ) {
682 } else if ( li->idassert_authz ) {
684 struct berval authcDN = BER_BVISNULL( &op->o_conn->c_dn ) ? slap_empty_bv : op->o_conn->c_dn;
687 rc = slap_sasl_matches( op, li->idassert_authz,
688 &authcDN, & authcDN );
689 if ( rc != LDAP_SUCCESS ) {
690 /* op->o_conn->c_dn is not authorized
696 switch ( li->idassert_mode ) {
697 case LDAP_BACK_IDASSERT_NONE:
698 case LDAP_BACK_IDASSERT_SELF:
699 /* original behavior:
700 * assert the client's identity */
701 assertedDN = op->o_conn->c_dn;
704 case LDAP_BACK_IDASSERT_ANONYMOUS:
705 /* assert "anonymous" */
706 assertedDN = slap_empty_bv;
709 case LDAP_BACK_IDASSERT_PROXYID:
710 /* don't assert; bind as proxyauthzdn */
713 case LDAP_BACK_IDASSERT_OTHER:
714 /* assert idassert DN */
715 assertedDN = li->idassert_dn;
722 if ( BER_BVISNULL( &assertedDN ) ) {
723 assertedDN = slap_empty_bv;
726 ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
727 ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
729 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
730 ctrls[ 0 ]->ldctl_iscritical = 1;
731 ctrls[ 0 ]->ldctl_value.bv_len = assertedDN.bv_len + STRLENOF( "dn:" );
732 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
733 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
734 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val + STRLENOF( "dn:" ),
735 assertedDN.bv_val, assertedDN.bv_len );
736 ctrls[ 0 ]->ldctl_value.bv_val[ ctrls[ 0 ]->ldctl_value.bv_len ] = '\0';
739 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
740 ctrls[ i + 1 ] = op->o_ctrls[ i ];
743 ctrls[ i + 1 ] = NULL;
746 if ( ctrls == NULL ) {
754 #endif /* LDAP_BACK_PROXY_AUTHZ */