1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2005 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 ( lc->bound_dn.bv_val ) {
76 ch_free( lc->bound_dn.bv_val );
77 lc->bound_dn.bv_len = 0;
78 lc->bound_dn.bv_val = NULL;
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 ( lc->cred.bv_val ) {
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 ( lc->local_dn.bv_val )
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 ( mdn.bv_val && 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_freeconn( Operation *op, struct ldapconn *lc )
215 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
217 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
218 lc = avl_delete( &li->conntree, (caddr_t)lc,
219 ldap_back_conn_cmp );
220 ldap_back_conn_free( (void *)lc );
221 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
227 ldap_back_getconn(Operation *op, SlapReply *rs)
229 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
230 struct ldapconn *lc, lc_curr;
234 /* Searches for a ldapconn in the avl tree */
236 /* Explicit binds must not be shared */
237 if ( op->o_tag == LDAP_REQ_BIND
239 && (op->o_bd == op->o_conn->c_authz_backend ))) {
240 lc_curr.conn = op->o_conn;
245 /* Internal searches are privileged and shared. So is root. */
246 if ( op->o_do_not_cache || be_isroot( op ) ) {
247 lc_curr.local_dn = op->o_bd->be_rootndn;
251 lc_curr.local_dn = op->o_ndn;
254 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
255 lc = (struct ldapconn *)avl_find( li->conntree,
256 (caddr_t)&lc_curr, ldap_back_conn_cmp );
257 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
259 /* Looks like we didn't get a bind. Open a new session... */
261 int vers = op->o_protocol;
262 rs->sr_err = ldap_initialize(&ld, li->url);
264 if (rs->sr_err != LDAP_SUCCESS) {
265 rs->sr_err = slap_map_api2result( rs );
266 if (rs->sr_text == NULL) {
267 rs->sr_text = "ldap_initialize() failed";
269 if (op->o_conn) send_ldap_result( op, rs );
273 /* Set LDAP version. This will always succeed: If the client
274 * bound with a particular version, then so can we.
276 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION,
277 (const void *)&vers);
278 /* FIXME: configurable? */
279 ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON);
281 lc = (struct ldapconn *)ch_malloc(sizeof(struct ldapconn));
282 lc->conn = lc_curr.conn;
284 ber_dupbv( &lc->local_dn, &lc_curr.local_dn );
286 #ifdef ENABLE_REWRITE
288 * Sets a cookie for the rewrite session
290 * FIXME: the o_conn might be no longer valid,
291 * since we may have different entries
292 * for the same connection
294 ( void )rewrite_session_init( li->rwmap.rwm_rw, op->o_conn );
295 #endif /* ENABLE_REWRITE */
297 ldap_pvt_thread_mutex_init( &lc->lc_mutex );
300 ber_dupbv( &lc->cred, &li->bindpw );
301 ber_dupbv( &lc->bound_dn, &li->binddn );
304 lc->cred.bv_val = NULL;
305 lc->bound_dn.bv_val = NULL;
306 lc->bound_dn.bv_len = 0;
307 if ( op->o_conn && op->o_conn->c_dn.bv_len != 0
308 && ( op->o_bd == op->o_conn->c_authz_backend ) ) {
314 * Rewrite the bind dn if needed
316 dc.rwmap = &li->rwmap;
317 #ifdef ENABLE_REWRITE
318 dc.conn = op->o_conn;
326 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn, &bv ) ) {
327 send_ldap_result( op, rs );
331 if ( bv.bv_val == op->o_conn->c_dn.bv_val ) {
332 ber_dupbv( &lc->bound_dn, &bv );
341 /* Inserts the newly created ldapconn in the avl tree */
342 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
343 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
344 ldap_back_conn_cmp, ldap_back_conn_dup );
346 #if PRINT_CONNTREE > 0
347 myprint( li->conntree );
348 #endif /* PRINT_CONNTREE */
350 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
353 LDAP_LOG( BACK_LDAP, INFO,
354 "ldap_back_getconn: conn %p inserted\n", (void *) lc, 0, 0);
355 #else /* !NEW_LOGGING */
356 Debug( LDAP_DEBUG_TRACE,
357 "=>ldap_back_getconn: conn %p inserted\n", (void *) lc, 0, 0 );
358 #endif /* !NEW_LOGGING */
360 /* Err could be -1 in case a duplicate ldapconn is inserted */
361 if ( rs->sr_err != 0 ) {
362 ldap_back_conn_free( lc );
364 send_ldap_error( op, rs, LDAP_OTHER,
365 "internal server error" );
371 LDAP_LOG( BACK_LDAP, INFO,
372 "ldap_back_getconn: conn %p fetched\n",
374 #else /* !NEW_LOGGING */
375 Debug( LDAP_DEBUG_TRACE,
376 "=>ldap_back_getconn: conn %p fetched\n", (void *) lc, 0, 0 );
377 #endif /* !NEW_LOGGING */
386 * Note: as the check for the value of lc->bound was already here, I removed
387 * it from all the callers, and I made the function return the flag, so
388 * it can be used to simplify the check.
391 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs )
393 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
397 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
399 #ifdef LDAP_BACK_PROXY_AUTHZ
403 * FIXME: we need to let clients use proxyAuthz
404 * otherwise we cannot do symmetric pools of servers;
405 * we have to live with the fact that a user can
406 * authorize itself as any ID that is allowed
407 * by the saslAuthzTo directive of the "proxyauthzdn".
410 * NOTE: current Proxy Authorization specification
411 * and implementation do not allow proxy authorization
412 * control to be provided with Bind requests
414 gotit = op->o_proxy_authz;
418 * if no bind took place yet, but the connection is bound
419 * and the "proxyauthzdn" is set, then bind as
420 * "proxyauthzdn" and explicitly add the proxyAuthz
421 * control to every operation with the dn bound
422 * to the connection as control value.
424 if ( ( lc->bound_dn.bv_val == NULL || lc->bound_dn.bv_len == 0 )
425 && ( op->o_conn && op->o_conn->c_dn.bv_val != NULL && op->o_conn->c_dn.bv_len != 0 )
426 && ( li->proxyauthzdn.bv_val != NULL && li->proxyauthzdn.bv_len != 0 )
428 rs->sr_err = ldap_sasl_bind(lc->ld, li->proxyauthzdn.bv_val,
429 LDAP_SASL_SIMPLE, &li->proxyauthzpw, NULL, NULL, &msgid);
432 #endif /* LDAP_BACK_PROXY_AUTHZ */
434 rs->sr_err = ldap_sasl_bind(lc->ld, lc->bound_dn.bv_val,
435 LDAP_SASL_SIMPLE, &lc->cred, NULL, NULL, &msgid);
438 rc = ldap_back_op_result( lc, op, rs, msgid, 0 );
439 if (rc == LDAP_SUCCESS) {
444 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
451 * This is a callback used for chasing referrals using the same
452 * credentials as the original user on this session.
455 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
456 ber_int_t msgid, void *params )
458 struct ldapconn *lc = params;
460 return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val, LDAP_AUTH_SIMPLE );
463 #if 0 /* deprecated in favour of slap_map_api2result() */
464 /* Map API errors to protocol errors... */
466 ldap_back_map_result( SlapReply *rs )
470 case LDAP_SERVER_DOWN:
471 return LDAP_UNAVAILABLE;
472 case LDAP_LOCAL_ERROR:
474 case LDAP_ENCODING_ERROR:
475 case LDAP_DECODING_ERROR:
476 return LDAP_PROTOCOL_ERROR;
478 return LDAP_UNAVAILABLE;
479 case LDAP_AUTH_UNKNOWN:
480 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
481 case LDAP_FILTER_ERROR:
482 rs->sr_text = "Filter error";
484 case LDAP_USER_CANCELLED:
485 rs->sr_text = "User cancelled";
487 case LDAP_PARAM_ERROR:
488 return LDAP_PROTOCOL_ERROR;
491 case LDAP_CONNECT_ERROR:
492 return LDAP_UNAVAILABLE;
493 case LDAP_NOT_SUPPORTED:
494 return LDAP_UNWILLING_TO_PERFORM;
495 case LDAP_CONTROL_NOT_FOUND:
496 return LDAP_PROTOCOL_ERROR;
497 case LDAP_NO_RESULTS_RETURNED:
498 return LDAP_NO_SUCH_OBJECT;
499 case LDAP_MORE_RESULTS_TO_RETURN:
500 rs->sr_text = "More results to return";
502 case LDAP_CLIENT_LOOP:
503 case LDAP_REFERRAL_LIMIT_EXCEEDED:
504 return LDAP_LOOP_DETECT;
506 if ( LDAP_API_ERROR(rs->sr_err) )
514 ldap_back_op_result(struct ldapconn *lc, Operation *op, SlapReply *rs,
515 ber_int_t msgid, int sendok)
517 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
519 LDAPMessage *res = NULL;
522 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
525 rs->sr_matched = NULL;
527 /* if the error recorded in the reply corresponds
528 * to a successful state, get the error from the
529 * remote server response */
530 if ( ERR_OK( rs->sr_err ) ) {
531 /* if result parsing fails, note the failure reason */
532 if ( ldap_result( lc->ld, msgid, 1, NULL, &res ) == -1 ) {
533 ldap_get_option( lc->ld, LDAP_OPT_ERROR_NUMBER,
536 /* otherwise get the result; if it is not
537 * LDAP_SUCCESS, record it in the reply
538 * structure (this includes
539 * LDAP_COMPARE_{TRUE|FALSE}) */
541 int rc = ldap_parse_result( lc->ld, res, &rs->sr_err,
542 &match, &text, NULL, NULL, 1 );
544 if ( rc != LDAP_SUCCESS ) rs->sr_err = rc;
548 /* if the error in the reply structure is not
549 * LDAP_SUCCESS, try to map it from client
551 if ( !ERR_OK( rs->sr_err ) ) {
552 rs->sr_err = slap_map_api2result( rs );
554 /* internal ops ( op->o_conn == NULL )
555 * must not reply to client */
556 if ( op->o_conn && !op->o_do_not_cache && match ) {
557 struct berval dn, mdn;
560 dc.rwmap = &li->rwmap;
561 #ifdef ENABLE_REWRITE
562 dc.conn = op->o_conn;
564 dc.ctx = "matchedDN";
569 ber_str2bv(match, 0, 0, &dn);
570 ldap_back_dn_massage(&dc, &dn, &mdn);
572 /* record the (massaged) matched
573 * DN into the reply structure */
574 rs->sr_matched = mdn.bv_val;
578 if ( op->o_conn && ( sendok || rs->sr_err != LDAP_SUCCESS ) ) {
579 send_ldap_result( op, rs );
582 if ( rs->sr_matched != match ) {
583 free( (char *)rs->sr_matched );
585 rs->sr_matched = NULL;
586 ldap_memfree( match );
589 ldap_memfree( text );
592 return( ERR_OK( rs->sr_err ) ? 0 : -1 );
595 /* return true if bound, false if failed */
597 ldap_back_retry( struct ldapconn *lc, Operation *op, SlapReply *rs )
599 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
600 int vers = op->o_protocol;
603 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
604 ldap_unbind( lc->ld );
606 rs->sr_err = ldap_initialize(&ld, li->url);
608 if (rs->sr_err != LDAP_SUCCESS) {
609 rs->sr_err = slap_map_api2result( rs );
610 if (rs->sr_text == NULL) {
611 rs->sr_text = "ldap_initialize() failed";
613 if (op->o_conn) send_ldap_result( op, rs );
617 /* Set LDAP version. This will always succeed: If the client
618 * bound with a particular version, then so can we.
620 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers);
621 /* FIXME: configurable? */
622 ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON);
624 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
625 return ldap_back_dobind( lc, op, rs );
628 #ifdef LDAP_BACK_PROXY_AUTHZ
630 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
631 * to existing server-side controls if required; if not,
632 * the existing server-side controls are placed in *pctrls.
633 * The caller, after using the controls in client API
634 * operations, if ( *pctrls != op->o_ctrls ), should
635 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
636 * The function returns success if the control could
637 * be added if required, or if it did nothing; in the future,
638 * it might return some error if it failed.
640 * if no bind took place yet, but the connection is bound
641 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
642 * and explicitly add proxyAuthz the control to every operation
643 * with the dn bound to the connection as control value.
645 * If no server-side controls are defined for the operation,
646 * simply add the proxyAuthz control; otherwise, if the
647 * proxyAuthz control is not already set, add it as
648 * the first one (FIXME: is controls order significant
652 ldap_back_proxy_authz_ctrl(
656 LDAPControl ***pctrls )
658 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
659 LDAPControl **ctrls = NULL;
663 if ( ( lc->bound_dn.bv_val == NULL || lc->bound_dn.bv_len == 0 )
664 && ( op->o_conn && op->o_conn->c_dn.bv_val != NULL && op->o_conn->c_dn.bv_len != 0 )
665 && ( li->proxyauthzdn.bv_val != NULL && li->proxyauthzdn.bv_len != 0 ) ) {
668 if ( !op->o_proxy_authz ) {
669 ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
670 ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
672 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
673 ctrls[ 0 ]->ldctl_iscritical = 1;
674 ctrls[ 0 ]->ldctl_value.bv_len = op->o_conn->c_dn.bv_len + 3;
675 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
676 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", sizeof( "dn:" ) - 1 );
677 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val + sizeof( "dn:") - 1,
678 op->o_conn->c_dn.bv_val, op->o_conn->c_dn.bv_len + 1 );
681 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
682 ctrls[ i + 1 ] = op->o_ctrls[ i ];
685 ctrls[ i + 1 ] = NULL;
689 * FIXME: we do not want to perform proxyAuthz
690 * on behalf of the client, because this would
691 * be performed with "proxyauthzdn" privileges.
693 * This might actually be too strict, since
694 * the "proxyauthzdn" saslAuthzTo, and each entry's
695 * saslAuthzFrom attributes may be crafted
696 * to avoid unwanted proxyAuthz to take place.
699 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
700 rs->sr_text = "proxyAuthz not allowed within namingContext";
705 if ( ctrls == NULL ) {
713 #endif /* LDAP_BACK_PROXY_AUTHZ */