1 /* bind.c - ldap backend bind function */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 /* This is an altered version */
9 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
11 * Permission is granted to anyone to use this software for any purpose
12 * on any computer system, and to alter it and redistribute it, subject
13 * to the following restrictions:
15 * 1. The author is not responsible for the consequences of use of this
16 * software, no matter how awful, even if they arise from flaws in it.
18 * 2. The origin of this software must not be misrepresented, either by
19 * explicit claim or by omission. Since few users ever read sources,
20 * credits should appear in the documentation.
22 * 3. Altered versions must be plainly marked as such, and must not be
23 * misrepresented as being the original software. Since few users
24 * ever read sources, credits should appear in the documentation.
26 * 4. This notice may not be removed or altered.
30 * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
32 * This software is being modified by Pierangelo Masarati.
33 * The previously reported conditions apply to the modified code as well.
34 * Changes in the original code are highlighted where required.
35 * Credits for the original code go to the author, Howard Chu.
42 #include <ac/socket.h>
43 #include <ac/string.h>
48 #include "back-ldap.h"
50 #define PRINT_CONNTREE 0
52 static LDAP_REBIND_PROC ldap_back_rebind;
59 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
62 struct berval mdn = { 0, NULL };
67 lc = ldap_back_getconn(op, rs);
73 * Rewrite the bind dn if needed
84 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
85 send_ldap_result( op, rs );
89 if ( lc->bound_dn.bv_val ) {
90 ch_free( lc->bound_dn.bv_val );
91 lc->bound_dn.bv_len = 0;
92 lc->bound_dn.bv_val = NULL;
95 /* method is always LDAP_AUTH_SIMPLE if we got here */
96 rs->sr_err = ldap_sasl_bind(lc->ld, mdn.bv_val, LDAP_SASL_SIMPLE,
97 &op->oq_bind.rb_cred, op->o_ctrls, NULL, &msgid);
98 rc = ldap_back_op_result( lc, op, rs, msgid, 1 );
99 if (rc == LDAP_SUCCESS) {
101 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
104 ber_dupbv( &lc->bound_dn, &op->o_req_dn );
108 if ( li->savecred ) {
109 if ( lc->cred.bv_val )
110 ch_free( lc->cred.bv_val );
111 ber_dupbv( &lc->cred, &op->oq_bind.rb_cred );
112 ldap_set_rebind_proc( lc->ld, ldap_back_rebind, lc );
116 /* must re-insert if local DN changed as result of bind */
117 if ( lc->bound && !bvmatch(&op->o_req_ndn, &lc->local_dn ) ) {
120 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
121 lc = avl_delete( &li->conntree, (caddr_t)lc,
122 ldap_back_conn_cmp );
123 if ( lc->local_dn.bv_val )
124 ch_free( lc->local_dn.bv_val );
125 ber_dupbv( &lc->local_dn, &op->o_req_ndn );
126 lerr = avl_insert( &li->conntree, (caddr_t)lc,
127 ldap_back_conn_cmp, ldap_back_conn_dup );
128 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
130 ldap_back_conn_free( lc );
134 if ( mdn.bv_val && mdn.bv_val != op->o_req_dn.bv_val ) {
144 * compares two struct ldapconn based on the value of the conn pointer;
153 const struct ldapconn *lc1 = (const struct ldapconn *)c1;
154 const struct ldapconn *lc2 = (const struct ldapconn *)c2;
157 /* If local DNs don't match, it is definitely not a match */
158 if ( ( rc = ber_bvcmp( &lc1->local_dn, &lc2->local_dn )) )
161 /* For shared sessions, conn is NULL. Only explicitly
162 * bound sessions will have non-NULL conn.
164 return lc1->conn - lc2->conn;
170 * returns -1 in case a duplicate struct ldapconn has been inserted;
179 struct ldapconn *lc1 = (struct ldapconn *)c1;
180 struct ldapconn *lc2 = (struct ldapconn *)c2;
182 /* Cannot have more than one shared session with same DN */
183 if ( dn_match( &lc1->local_dn, &lc2->local_dn ) &&
184 lc1->conn == lc2->conn ) return -1;
189 #if PRINT_CONNTREE > 0
190 static void ravl_print( Avlnode *root, int depth )
198 ravl_print( root->avl_right, depth+1 );
200 for ( i = 0; i < depth; i++ )
204 printf( "lc(%lx) local(%s) conn(%lx) %d\n",
205 lc, lc->local_dn.bv_val, lc->conn, root->avl_bf );
207 ravl_print( root->avl_left, depth+1 );
210 static void myprint( Avlnode *root )
212 printf( "********\n" );
215 printf( "\tNULL\n" );
218 ravl_print( root, 0 );
220 printf( "********\n" );
222 #endif /* PRINT_CONNTREE */
225 ldap_back_getconn(Operation *op, SlapReply *rs)
227 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
228 struct ldapconn *lc, lc_curr;
232 /* Searches for a ldapconn in the avl tree */
234 /* Explicit binds must not be shared */
235 if ( op->o_tag == LDAP_REQ_BIND
237 && (op->o_bd == op->o_conn->c_authz_backend ))) {
238 lc_curr.conn = op->o_conn;
243 /* Internal searches are privileged and shared. So is root. */
244 if ( op->o_do_not_cache || be_isroot( li->be, &op->o_ndn ) ) {
245 lc_curr.local_dn = li->be->be_rootndn;
249 lc_curr.local_dn = op->o_ndn;
252 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
253 lc = (struct ldapconn *)avl_find( li->conntree,
254 (caddr_t)&lc_curr, ldap_back_conn_cmp );
255 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
257 /* Looks like we didn't get a bind. Open a new session... */
259 int vers = op->o_protocol;
260 rs->sr_err = ldap_initialize(&ld, li->url);
262 if (rs->sr_err != LDAP_SUCCESS) {
263 rs->sr_err = ldap_back_map_result(rs);
264 if (rs->sr_text == NULL) {
265 rs->sr_text = "ldap_initialize() failed";
267 if (op->o_conn) send_ldap_result( op, rs );
271 /* Set LDAP version. This will always succeed: If the client
272 * bound with a particular version, then so can we.
274 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION,
275 (const void *)&vers);
276 /* FIXME: configurable? */
277 ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON);
279 lc = (struct ldapconn *)ch_malloc(sizeof(struct ldapconn));
280 lc->conn = lc_curr.conn;
282 ber_dupbv( &lc->local_dn, &lc_curr.local_dn );
284 #ifdef ENABLE_REWRITE
286 * Sets a cookie for the rewrite session
288 * FIXME: the o_conn might be no longer valid,
289 * since we may have different entries
290 * for the same connection
292 ( void )rewrite_session_init( li->rwinfo, op->o_conn );
293 #endif /* ENABLE_REWRITE */
295 ldap_pvt_thread_mutex_init( &lc->lc_mutex );
298 ber_dupbv( &lc->cred, &li->bindpw );
299 ber_dupbv( &lc->bound_dn, &li->binddn );
302 lc->cred.bv_val = NULL;
303 lc->bound_dn.bv_val = NULL;
304 lc->bound_dn.bv_len = 0;
305 if ( op->o_conn->c_dn.bv_len != 0
306 && ( op->o_bd == op->o_conn->c_authz_backend ) ) {
312 * Rewrite the bind dn if needed
315 #ifdef ENABLE_REWRITE
316 dc.conn = op->o_conn;
324 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn, &bv ) ) {
325 if (op->o_conn) send_ldap_result( op, rs );
329 if ( bv.bv_val == op->o_conn->c_dn.bv_val ) {
330 ber_dupbv( &lc->bound_dn, &bv );
339 /* Inserts the newly created ldapconn in the avl tree */
340 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
341 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
342 ldap_back_conn_cmp, ldap_back_conn_dup );
344 #if PRINT_CONNTREE > 0
345 myprint( li->conntree );
346 #endif /* PRINT_CONNTREE */
348 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
351 LDAP_LOG( BACK_LDAP, INFO,
352 "ldap_back_getconn: conn %lx inserted\n", lc, 0, 0);
353 #else /* !NEW_LOGGING */
354 Debug( LDAP_DEBUG_TRACE,
355 "=>ldap_back_getconn: conn %lx inserted\n", lc, 0, 0 );
356 #endif /* !NEW_LOGGING */
358 /* Err could be -1 in case a duplicate ldapconn is inserted */
359 if ( rs->sr_err != 0 ) {
360 ldap_back_conn_free( lc );
362 send_ldap_error( op, rs, LDAP_OTHER,
363 "internal server error" );
369 LDAP_LOG( BACK_LDAP, INFO,
370 "ldap_back_getconn: conn %lx fetched\n",
372 #else /* !NEW_LOGGING */
373 Debug( LDAP_DEBUG_TRACE,
374 "=>ldap_back_getconn: conn %lx fetched\n", lc, 0, 0 );
375 #endif /* !NEW_LOGGING */
384 * Note: as the check for the value of lc->bound was already here, I removed
385 * it from all the callers, and I made the function return the flag, so
386 * it can be used to simplify the check.
389 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs )
394 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
396 rs->sr_err = ldap_sasl_bind(lc->ld, lc->bound_dn.bv_val,
397 LDAP_SASL_SIMPLE, &lc->cred, NULL, NULL, &msgid);
398 rc = ldap_back_op_result( lc, op, rs, msgid, 0 );
399 if (rc == LDAP_SUCCESS) {
404 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
411 * This is a callback used for chasing referrals using the same
412 * credentials as the original user on this session.
415 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
416 ber_int_t msgid, void *params )
418 struct ldapconn *lc = params;
420 return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val, LDAP_AUTH_SIMPLE );
423 /* Map API errors to protocol errors... */
426 ldap_back_map_result(SlapReply *rs)
430 case LDAP_SERVER_DOWN:
431 return LDAP_UNAVAILABLE;
432 case LDAP_LOCAL_ERROR:
434 case LDAP_ENCODING_ERROR:
435 case LDAP_DECODING_ERROR:
436 return LDAP_PROTOCOL_ERROR;
438 return LDAP_UNAVAILABLE;
439 case LDAP_AUTH_UNKNOWN:
440 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
441 case LDAP_FILTER_ERROR:
442 rs->sr_text = "Filter error";
444 case LDAP_USER_CANCELLED:
445 rs->sr_text = "User cancelled";
447 case LDAP_PARAM_ERROR:
448 return LDAP_PROTOCOL_ERROR;
451 case LDAP_CONNECT_ERROR:
452 return LDAP_UNAVAILABLE;
453 case LDAP_NOT_SUPPORTED:
454 return LDAP_UNWILLING_TO_PERFORM;
455 case LDAP_CONTROL_NOT_FOUND:
456 return LDAP_PROTOCOL_ERROR;
457 case LDAP_NO_RESULTS_RETURNED:
458 return LDAP_NO_SUCH_OBJECT;
459 case LDAP_MORE_RESULTS_TO_RETURN:
460 rs->sr_text = "More results to return";
462 case LDAP_CLIENT_LOOP:
463 case LDAP_REFERRAL_LIMIT_EXCEEDED:
464 return LDAP_LOOP_DETECT;
466 if LDAP_API_ERROR(rs->sr_err)
474 ldap_back_op_result(struct ldapconn *lc, Operation *op, SlapReply *rs,
475 ber_int_t msgid, int sendok)
477 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
483 rs->sr_matched = NULL;
485 if (rs->sr_err == LDAP_SUCCESS) {
486 if (ldap_result(lc->ld, msgid, 1, NULL, &res) == -1) {
487 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER,
490 int rc = ldap_parse_result(lc->ld, res, &rs->sr_err,
491 &match, &text, NULL, NULL, 1);
493 if (rc != LDAP_SUCCESS) rs->sr_err = rc;
497 if (rs->sr_err != LDAP_SUCCESS) {
498 rs->sr_err = ldap_back_map_result(rs);
500 /* internal ops must not reply to client */
501 if ( op->o_conn && !op->o_do_not_cache && match ) {
502 struct berval dn, mdn;
506 #ifdef ENABLE_REWRITE
507 dc.conn = op->o_conn;
509 dc.ctx = "matchedDn";
514 ber_str2bv(match, 0, 0, &dn);
515 ldap_back_dn_massage(&dc, &dn, &mdn);
516 rs->sr_matched = mdn.bv_val;
520 if (op->o_conn && (sendok || rs->sr_err != LDAP_SUCCESS)) {
521 send_ldap_result( op, rs );
524 if ( rs->sr_matched != match ) {
525 free( (char *)rs->sr_matched );
527 rs->sr_matched = NULL;
528 ldap_memfree( match );
531 ldap_memfree( text );
534 return( (rs->sr_err == LDAP_SUCCESS) ? 0 : -1 );