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
75 dc.rwmap = &li->rwmap;
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 memset( lc->cred.bv_val, 0, lc->cred.bv_len );
111 ch_free( lc->cred.bv_val );
113 ber_dupbv( &lc->cred, &op->oq_bind.rb_cred );
114 ldap_set_rebind_proc( lc->ld, ldap_back_rebind, lc );
118 /* must re-insert if local DN changed as result of bind */
119 if ( lc->bound && !bvmatch(&op->o_req_ndn, &lc->local_dn ) ) {
122 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
123 lc = avl_delete( &li->conntree, (caddr_t)lc,
124 ldap_back_conn_cmp );
125 if ( lc->local_dn.bv_val )
126 ch_free( lc->local_dn.bv_val );
127 ber_dupbv( &lc->local_dn, &op->o_req_ndn );
128 lerr = avl_insert( &li->conntree, (caddr_t)lc,
129 ldap_back_conn_cmp, ldap_back_conn_dup );
130 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
132 ldap_back_conn_free( lc );
136 if ( mdn.bv_val && mdn.bv_val != op->o_req_dn.bv_val ) {
146 * compares two struct ldapconn based on the value of the conn pointer;
155 const struct ldapconn *lc1 = (const struct ldapconn *)c1;
156 const struct ldapconn *lc2 = (const struct ldapconn *)c2;
159 /* If local DNs don't match, it is definitely not a match */
160 if ( ( rc = ber_bvcmp( &lc1->local_dn, &lc2->local_dn )) )
163 /* For shared sessions, conn is NULL. Only explicitly
164 * bound sessions will have non-NULL conn.
166 return (unsigned)lc1->conn - (unsigned)lc2->conn;
172 * returns -1 in case a duplicate struct ldapconn has been inserted;
181 struct ldapconn *lc1 = (struct ldapconn *)c1;
182 struct ldapconn *lc2 = (struct ldapconn *)c2;
184 /* Cannot have more than one shared session with same DN */
185 if ( dn_match( &lc1->local_dn, &lc2->local_dn ) &&
186 lc1->conn == lc2->conn ) return -1;
191 #if PRINT_CONNTREE > 0
192 static void ravl_print( Avlnode *root, int depth )
200 ravl_print( root->avl_right, depth+1 );
202 for ( i = 0; i < depth; i++ )
206 printf( "lc(%lx) local(%s) conn(%lx) %d\n",
207 lc, lc->local_dn.bv_val, lc->conn, root->avl_bf );
209 ravl_print( root->avl_left, depth+1 );
212 static void myprint( Avlnode *root )
214 printf( "********\n" );
217 printf( "\tNULL\n" );
220 ravl_print( root, 0 );
222 printf( "********\n" );
224 #endif /* PRINT_CONNTREE */
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( li->be, &op->o_ndn ) ) {
247 lc_curr.local_dn = li->be->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 = ldap_back_map_result(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->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 if (op->o_conn) 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 %lx inserted\n", lc, 0, 0);
355 #else /* !NEW_LOGGING */
356 Debug( LDAP_DEBUG_TRACE,
357 "=>ldap_back_getconn: conn %lx inserted\n", 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 %lx fetched\n",
374 #else /* !NEW_LOGGING */
375 Debug( LDAP_DEBUG_TRACE,
376 "=>ldap_back_getconn: conn %lx fetched\n", 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 )
396 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
398 rs->sr_err = ldap_sasl_bind(lc->ld, lc->bound_dn.bv_val,
399 LDAP_SASL_SIMPLE, &lc->cred, NULL, NULL, &msgid);
400 rc = ldap_back_op_result( lc, op, rs, msgid, 0 );
401 if (rc == LDAP_SUCCESS) {
406 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
413 * This is a callback used for chasing referrals using the same
414 * credentials as the original user on this session.
417 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
418 ber_int_t msgid, void *params )
420 struct ldapconn *lc = params;
422 return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val, LDAP_AUTH_SIMPLE );
425 /* Map API errors to protocol errors... */
428 ldap_back_map_result(SlapReply *rs)
432 case LDAP_SERVER_DOWN:
433 return LDAP_UNAVAILABLE;
434 case LDAP_LOCAL_ERROR:
436 case LDAP_ENCODING_ERROR:
437 case LDAP_DECODING_ERROR:
438 return LDAP_PROTOCOL_ERROR;
440 return LDAP_UNAVAILABLE;
441 case LDAP_AUTH_UNKNOWN:
442 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
443 case LDAP_FILTER_ERROR:
444 rs->sr_text = "Filter error";
446 case LDAP_USER_CANCELLED:
447 rs->sr_text = "User cancelled";
449 case LDAP_PARAM_ERROR:
450 return LDAP_PROTOCOL_ERROR;
453 case LDAP_CONNECT_ERROR:
454 return LDAP_UNAVAILABLE;
455 case LDAP_NOT_SUPPORTED:
456 return LDAP_UNWILLING_TO_PERFORM;
457 case LDAP_CONTROL_NOT_FOUND:
458 return LDAP_PROTOCOL_ERROR;
459 case LDAP_NO_RESULTS_RETURNED:
460 return LDAP_NO_SUCH_OBJECT;
461 case LDAP_MORE_RESULTS_TO_RETURN:
462 rs->sr_text = "More results to return";
464 case LDAP_CLIENT_LOOP:
465 case LDAP_REFERRAL_LIMIT_EXCEEDED:
466 return LDAP_LOOP_DETECT;
468 if LDAP_API_ERROR(rs->sr_err)
476 ldap_back_op_result(struct ldapconn *lc, Operation *op, SlapReply *rs,
477 ber_int_t msgid, int sendok)
479 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
485 rs->sr_matched = NULL;
487 if (rs->sr_err == LDAP_SUCCESS) {
488 if (ldap_result(lc->ld, msgid, 1, NULL, &res) == -1) {
489 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER,
492 int rc = ldap_parse_result(lc->ld, res, &rs->sr_err,
493 &match, &text, NULL, NULL, 1);
495 if (rc != LDAP_SUCCESS) rs->sr_err = rc;
499 if (rs->sr_err != LDAP_SUCCESS) {
500 rs->sr_err = ldap_back_map_result(rs);
502 /* internal ops must not reply to client */
503 if ( op->o_conn && !op->o_do_not_cache && match ) {
504 struct berval dn, mdn;
507 dc.rwmap = &li->rwmap;
508 #ifdef ENABLE_REWRITE
509 dc.conn = op->o_conn;
511 dc.ctx = "matchedDn";
516 ber_str2bv(match, 0, 0, &dn);
517 ldap_back_dn_massage(&dc, &dn, &mdn);
518 rs->sr_matched = mdn.bv_val;
522 if (op->o_conn && (sendok || rs->sr_err != LDAP_SUCCESS)) {
523 send_ldap_result( op, rs );
526 if ( rs->sr_matched != match ) {
527 free( (char *)rs->sr_matched );
529 rs->sr_matched = NULL;
530 ldap_memfree( match );
533 ldap_memfree( text );
536 return( (rs->sr_err == LDAP_SUCCESS) ? 0 : -1 );