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 = { 0, NULL };
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_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( 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 = ldap_back_map_result(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 );
290 lc->cred.bv_val = NULL;
291 lc->bound_dn.bv_val = NULL;
292 lc->bound_dn.bv_len = 0;
293 if ( op->o_conn && op->o_conn->c_dn.bv_len != 0
294 && ( op->o_bd == op->o_conn->c_authz_backend ) ) {
300 * Rewrite the bind dn if needed
302 dc.rwmap = &li->rwmap;
303 #ifdef ENABLE_REWRITE
304 dc.conn = op->o_conn;
312 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn, &bv ) ) {
313 send_ldap_result( op, rs );
317 if ( bv.bv_val == op->o_conn->c_dn.bv_val ) {
318 ber_dupbv( &lc->bound_dn, &bv );
327 /* Inserts the newly created ldapconn in the avl tree */
328 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
329 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
330 ldap_back_conn_cmp, ldap_back_conn_dup );
332 #if PRINT_CONNTREE > 0
333 myprint( li->conntree );
334 #endif /* PRINT_CONNTREE */
336 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
339 LDAP_LOG( BACK_LDAP, INFO,
340 "ldap_back_getconn: conn %p inserted\n", (void *) lc, 0, 0);
341 #else /* !NEW_LOGGING */
342 Debug( LDAP_DEBUG_TRACE,
343 "=>ldap_back_getconn: conn %p inserted\n", (void *) lc, 0, 0 );
344 #endif /* !NEW_LOGGING */
346 /* Err could be -1 in case a duplicate ldapconn is inserted */
347 if ( rs->sr_err != 0 ) {
348 ldap_back_conn_free( lc );
350 send_ldap_error( op, rs, LDAP_OTHER,
351 "internal server error" );
357 LDAP_LOG( BACK_LDAP, INFO,
358 "ldap_back_getconn: conn %p fetched\n",
360 #else /* !NEW_LOGGING */
361 Debug( LDAP_DEBUG_TRACE,
362 "=>ldap_back_getconn: conn %p fetched\n", (void *) lc, 0, 0 );
363 #endif /* !NEW_LOGGING */
372 * Note: as the check for the value of lc->bound was already here, I removed
373 * it from all the callers, and I made the function return the flag, so
374 * it can be used to simplify the check.
377 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs )
379 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
383 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
385 #ifdef LDAP_BACK_PROXY_AUTHZ
389 * FIXME: we need to let clients use proxyAuthz
390 * otherwise we cannot do symmetric pools of servers;
391 * we have to live with the fact that a user can
392 * authorize itself as any ID that is allowed
393 * by the saslAuthzTo directive of the "proxyauthzdn".
396 * NOTE: current Proxy Authorization specification
397 * and implementation do not allow proxy authorization
398 * control to be provided with Bind requests
400 gotit = op->o_proxy_authz;
404 * if no bind took place yet, but the connection is bound
405 * and the "proxyauthzdn" is set, then bind as
406 * "proxyauthzdn" and explicitly add the proxyAuthz
407 * control to every operation with the dn bound
408 * to the connection as control value.
410 if ( ( lc->bound_dn.bv_val == NULL || lc->bound_dn.bv_len == 0 )
411 && ( op->o_conn && op->o_conn->c_dn.bv_val != NULL && op->o_conn->c_dn.bv_len != 0 )
412 && ( li->proxyauthzdn.bv_val != NULL && li->proxyauthzdn.bv_len != 0 )
414 rs->sr_err = ldap_sasl_bind(lc->ld, li->proxyauthzdn.bv_val,
415 LDAP_SASL_SIMPLE, &li->proxyauthzpw, NULL, NULL, &msgid);
418 #endif /* LDAP_BACK_PROXY_AUTHZ */
420 rs->sr_err = ldap_sasl_bind(lc->ld, lc->bound_dn.bv_val,
421 LDAP_SASL_SIMPLE, &lc->cred, NULL, NULL, &msgid);
424 rc = ldap_back_op_result( lc, op, rs, msgid, 0 );
425 if (rc == LDAP_SUCCESS) {
430 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
437 * This is a callback used for chasing referrals using the same
438 * credentials as the original user on this session.
441 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
442 ber_int_t msgid, void *params )
444 struct ldapconn *lc = params;
446 return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val, LDAP_AUTH_SIMPLE );
449 /* Map API errors to protocol errors... */
452 ldap_back_map_result(SlapReply *rs)
456 case LDAP_SERVER_DOWN:
457 return LDAP_UNAVAILABLE;
458 case LDAP_LOCAL_ERROR:
460 case LDAP_ENCODING_ERROR:
461 case LDAP_DECODING_ERROR:
462 return LDAP_PROTOCOL_ERROR;
464 return LDAP_UNAVAILABLE;
465 case LDAP_AUTH_UNKNOWN:
466 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
467 case LDAP_FILTER_ERROR:
468 rs->sr_text = "Filter error";
470 case LDAP_USER_CANCELLED:
471 rs->sr_text = "User cancelled";
473 case LDAP_PARAM_ERROR:
474 return LDAP_PROTOCOL_ERROR;
477 case LDAP_CONNECT_ERROR:
478 return LDAP_UNAVAILABLE;
479 case LDAP_NOT_SUPPORTED:
480 return LDAP_UNWILLING_TO_PERFORM;
481 case LDAP_CONTROL_NOT_FOUND:
482 return LDAP_PROTOCOL_ERROR;
483 case LDAP_NO_RESULTS_RETURNED:
484 return LDAP_NO_SUCH_OBJECT;
485 case LDAP_MORE_RESULTS_TO_RETURN:
486 rs->sr_text = "More results to return";
488 case LDAP_CLIENT_LOOP:
489 case LDAP_REFERRAL_LIMIT_EXCEEDED:
490 return LDAP_LOOP_DETECT;
492 if LDAP_API_ERROR(rs->sr_err)
500 ldap_back_op_result(struct ldapconn *lc, Operation *op, SlapReply *rs,
501 ber_int_t msgid, int sendok)
503 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
509 rs->sr_matched = NULL;
511 if (rs->sr_err == LDAP_SUCCESS) {
512 if (ldap_result(lc->ld, msgid, 1, NULL, &res) == -1) {
513 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER,
516 int rc = ldap_parse_result(lc->ld, res, &rs->sr_err,
517 &match, &text, NULL, NULL, 1);
519 if (rc != LDAP_SUCCESS) rs->sr_err = rc;
523 if (rs->sr_err != LDAP_SUCCESS) {
524 rs->sr_err = ldap_back_map_result(rs);
526 /* internal ops must not reply to client */
527 if ( op->o_conn && !op->o_do_not_cache && match ) {
528 struct berval dn, mdn;
531 dc.rwmap = &li->rwmap;
532 #ifdef ENABLE_REWRITE
533 dc.conn = op->o_conn;
535 dc.ctx = "matchedDN";
540 ber_str2bv(match, 0, 0, &dn);
541 ldap_back_dn_massage(&dc, &dn, &mdn);
542 rs->sr_matched = mdn.bv_val;
546 if (op->o_conn && (sendok || rs->sr_err != LDAP_SUCCESS)) {
547 send_ldap_result( op, rs );
550 if ( rs->sr_matched != match ) {
551 free( (char *)rs->sr_matched );
553 rs->sr_matched = NULL;
554 ldap_memfree( match );
557 ldap_memfree( text );
560 return( (rs->sr_err == LDAP_SUCCESS) ? 0 : -1 );
563 #ifdef LDAP_BACK_PROXY_AUTHZ
565 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
566 * to existing server-side controls if required; if not,
567 * the existing server-side controls are placed in *pctrls.
568 * The caller, after using the controls in client API
569 * operations, if ( *pctrls != op->o_ctrls ), should
570 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
571 * The function returns success if the control could
572 * be added if required, or if it did nothing; in the future,
573 * it might return some error if it failed.
575 * if no bind took place yet, but the connection is bound
576 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
577 * and explicitly add proxyAuthz the control to every operation
578 * with the dn bound to the connection as control value.
580 * If no server-side controls are defined for the operation,
581 * simply add the proxyAuthz control; otherwise, if the
582 * proxyAuthz control is not already set, add it as
583 * the first one (FIXME: is controls order significant
587 ldap_back_proxy_authz_ctrl(
591 LDAPControl ***pctrls )
593 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
594 LDAPControl **ctrls = NULL;
598 if ( ( lc->bound_dn.bv_val == NULL || lc->bound_dn.bv_len == 0 )
599 && ( op->o_conn && op->o_conn->c_dn.bv_val != NULL && op->o_conn->c_dn.bv_len != 0 )
600 && ( li->proxyauthzdn.bv_val != NULL && li->proxyauthzdn.bv_len != 0 ) ) {
603 if ( !op->o_proxy_authz ) {
604 ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
605 ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
607 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
608 ctrls[ 0 ]->ldctl_iscritical = 1;
609 ctrls[ 0 ]->ldctl_value.bv_len = op->o_conn->c_dn.bv_len + 3;
610 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
611 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", sizeof( "dn:" ) - 1 );
612 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val + sizeof( "dn:") - 1,
613 op->o_conn->c_dn.bv_val, op->o_conn->c_dn.bv_len + 1 );
616 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
617 ctrls[ i + 1 ] = op->o_ctrls[ i ];
620 ctrls[ i + 1 ] = NULL;
624 * FIXME: we do not want to perform proxyAuthz
625 * on behalf of the client, because this would
626 * be performed with "proxyauthzdn" privileges.
628 * This might actually be too strict, since
629 * the "proxyauthzdn" saslAuthzTo, and each entry's
630 * saslAuthzFrom attributes may be crafted
631 * to avoid unwanted proxyAuthz to take place.
634 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
635 rs->sr_text = "proxyAuthz not allowed within namingContext";
640 if ( ctrls == NULL ) {
648 #endif /* LDAP_BACK_PROXY_AUTHZ */