1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2003 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by the Howard Chu for inclusion
18 * in OpenLDAP Software and subsequently enhanced by Pierangelo
21 /* This is an altered version */
23 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
25 * Permission is granted to anyone to use this software for any purpose
26 * on any computer system, and to alter it and redistribute it, subject
27 * to the following restrictions:
29 * 1. The author is not responsible for the consequences of use of this
30 * software, no matter how awful, even if they arise from flaws in it.
32 * 2. The origin of this software must not be misrepresented, either by
33 * explicit claim or by omission. Since few users ever read sources,
34 * credits should appear in the documentation.
36 * 3. Altered versions must be plainly marked as such, and must not be
37 * misrepresented as being the original software. Since few users
38 * ever read sources, credits should appear in the documentation.
40 * 4. This notice may not be removed or altered.
44 * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
46 * This software is being modified by Pierangelo Masarati.
47 * The previously reported conditions apply to the modified code as well.
48 * Changes in the original code are highlighted where required.
49 * Credits for the original code go to the author, Howard Chu.
56 #include <ac/socket.h>
57 #include <ac/string.h>
62 #include "back-ldap.h"
64 #define PRINT_CONNTREE 0
66 static LDAP_REBIND_PROC ldap_back_rebind;
73 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
76 struct berval mdn = { 0, NULL };
81 lc = ldap_back_getconn(op, rs);
87 * Rewrite the bind dn if needed
89 dc.rwmap = &li->rwmap;
98 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
99 send_ldap_result( op, rs );
103 if ( lc->bound_dn.bv_val ) {
104 ch_free( lc->bound_dn.bv_val );
105 lc->bound_dn.bv_len = 0;
106 lc->bound_dn.bv_val = NULL;
109 /* method is always LDAP_AUTH_SIMPLE if we got here */
110 rs->sr_err = ldap_sasl_bind(lc->ld, mdn.bv_val, LDAP_SASL_SIMPLE,
111 &op->oq_bind.rb_cred, op->o_ctrls, NULL, &msgid);
112 rc = ldap_back_op_result( lc, op, rs, msgid, 1 );
113 if (rc == LDAP_SUCCESS) {
115 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
118 ber_dupbv( &lc->bound_dn, &op->o_req_dn );
122 if ( li->savecred ) {
123 if ( lc->cred.bv_val ) {
124 memset( lc->cred.bv_val, 0, lc->cred.bv_len );
125 ch_free( lc->cred.bv_val );
127 ber_dupbv( &lc->cred, &op->oq_bind.rb_cred );
128 ldap_set_rebind_proc( lc->ld, ldap_back_rebind, lc );
132 /* must re-insert if local DN changed as result of bind */
133 if ( lc->bound && !bvmatch(&op->o_req_ndn, &lc->local_dn ) ) {
136 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
137 lc = avl_delete( &li->conntree, (caddr_t)lc,
138 ldap_back_conn_cmp );
139 if ( lc->local_dn.bv_val )
140 ch_free( lc->local_dn.bv_val );
141 ber_dupbv( &lc->local_dn, &op->o_req_ndn );
142 lerr = avl_insert( &li->conntree, (caddr_t)lc,
143 ldap_back_conn_cmp, ldap_back_conn_dup );
144 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
146 ldap_back_conn_free( lc );
150 if ( mdn.bv_val && mdn.bv_val != op->o_req_dn.bv_val ) {
160 * compares two struct ldapconn based on the value of the conn pointer;
169 const struct ldapconn *lc1 = (const struct ldapconn *)c1;
170 const struct ldapconn *lc2 = (const struct ldapconn *)c2;
173 /* If local DNs don't match, it is definitely not a match */
174 if ( ( rc = ber_bvcmp( &lc1->local_dn, &lc2->local_dn )) )
177 /* For shared sessions, conn is NULL. Only explicitly
178 * bound sessions will have non-NULL conn.
180 return SLAP_PTRCMP(lc1->conn, lc2->conn);
186 * returns -1 in case a duplicate struct ldapconn has been inserted;
195 struct ldapconn *lc1 = (struct ldapconn *)c1;
196 struct ldapconn *lc2 = (struct ldapconn *)c2;
198 /* Cannot have more than one shared session with same DN */
199 if ( dn_match( &lc1->local_dn, &lc2->local_dn ) &&
200 lc1->conn == lc2->conn ) return -1;
205 #if PRINT_CONNTREE > 0
206 static void ravl_print( Avlnode *root, int depth )
214 ravl_print( root->avl_right, depth+1 );
216 for ( i = 0; i < depth; i++ )
220 printf( "lc(%lx) local(%s) conn(%lx) %d\n",
221 lc, lc->local_dn.bv_val, lc->conn, root->avl_bf );
223 ravl_print( root->avl_left, depth+1 );
226 static void myprint( Avlnode *root )
228 printf( "********\n" );
231 printf( "\tNULL\n" );
234 ravl_print( root, 0 );
236 printf( "********\n" );
238 #endif /* PRINT_CONNTREE */
241 ldap_back_getconn(Operation *op, SlapReply *rs)
243 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
244 struct ldapconn *lc, lc_curr;
248 /* Searches for a ldapconn in the avl tree */
250 /* Explicit binds must not be shared */
251 if ( op->o_tag == LDAP_REQ_BIND
253 && (op->o_bd == op->o_conn->c_authz_backend ))) {
254 lc_curr.conn = op->o_conn;
259 /* Internal searches are privileged and shared. So is root. */
260 if ( op->o_do_not_cache || be_isroot( li->be, &op->o_ndn ) ) {
261 lc_curr.local_dn = li->be->be_rootndn;
265 lc_curr.local_dn = op->o_ndn;
268 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
269 lc = (struct ldapconn *)avl_find( li->conntree,
270 (caddr_t)&lc_curr, ldap_back_conn_cmp );
271 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
273 /* Looks like we didn't get a bind. Open a new session... */
275 int vers = op->o_protocol;
276 rs->sr_err = ldap_initialize(&ld, li->url);
278 if (rs->sr_err != LDAP_SUCCESS) {
279 rs->sr_err = ldap_back_map_result(rs);
280 if (rs->sr_text == NULL) {
281 rs->sr_text = "ldap_initialize() failed";
283 if (op->o_conn) send_ldap_result( op, rs );
287 /* Set LDAP version. This will always succeed: If the client
288 * bound with a particular version, then so can we.
290 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION,
291 (const void *)&vers);
292 /* FIXME: configurable? */
293 ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON);
295 lc = (struct ldapconn *)ch_malloc(sizeof(struct ldapconn));
296 lc->conn = lc_curr.conn;
298 ber_dupbv( &lc->local_dn, &lc_curr.local_dn );
300 #ifdef ENABLE_REWRITE
302 * Sets a cookie for the rewrite session
304 * FIXME: the o_conn might be no longer valid,
305 * since we may have different entries
306 * for the same connection
308 ( void )rewrite_session_init( li->rwmap.rwm_rw, op->o_conn );
309 #endif /* ENABLE_REWRITE */
311 ldap_pvt_thread_mutex_init( &lc->lc_mutex );
314 ber_dupbv( &lc->cred, &li->bindpw );
315 ber_dupbv( &lc->bound_dn, &li->binddn );
318 lc->cred.bv_val = NULL;
319 lc->bound_dn.bv_val = NULL;
320 lc->bound_dn.bv_len = 0;
321 if ( op->o_conn && op->o_conn->c_dn.bv_len != 0
322 && ( op->o_bd == op->o_conn->c_authz_backend ) ) {
328 * Rewrite the bind dn if needed
330 dc.rwmap = &li->rwmap;
331 #ifdef ENABLE_REWRITE
332 dc.conn = op->o_conn;
340 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn, &bv ) ) {
341 send_ldap_result( op, rs );
345 if ( bv.bv_val == op->o_conn->c_dn.bv_val ) {
346 ber_dupbv( &lc->bound_dn, &bv );
355 /* Inserts the newly created ldapconn in the avl tree */
356 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
357 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
358 ldap_back_conn_cmp, ldap_back_conn_dup );
360 #if PRINT_CONNTREE > 0
361 myprint( li->conntree );
362 #endif /* PRINT_CONNTREE */
364 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
367 LDAP_LOG( BACK_LDAP, INFO,
368 "ldap_back_getconn: conn %p inserted\n", (void *) lc, 0, 0);
369 #else /* !NEW_LOGGING */
370 Debug( LDAP_DEBUG_TRACE,
371 "=>ldap_back_getconn: conn %p inserted\n", (void *) lc, 0, 0 );
372 #endif /* !NEW_LOGGING */
374 /* Err could be -1 in case a duplicate ldapconn is inserted */
375 if ( rs->sr_err != 0 ) {
376 ldap_back_conn_free( lc );
378 send_ldap_error( op, rs, LDAP_OTHER,
379 "internal server error" );
385 LDAP_LOG( BACK_LDAP, INFO,
386 "ldap_back_getconn: conn %p fetched\n",
388 #else /* !NEW_LOGGING */
389 Debug( LDAP_DEBUG_TRACE,
390 "=>ldap_back_getconn: conn %p fetched\n", (void *) lc, 0, 0 );
391 #endif /* !NEW_LOGGING */
400 * Note: as the check for the value of lc->bound was already here, I removed
401 * it from all the callers, and I made the function return the flag, so
402 * it can be used to simplify the check.
405 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs )
410 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
412 rs->sr_err = ldap_sasl_bind(lc->ld, lc->bound_dn.bv_val,
413 LDAP_SASL_SIMPLE, &lc->cred, NULL, NULL, &msgid);
414 rc = ldap_back_op_result( lc, op, rs, msgid, 0 );
415 if (rc == LDAP_SUCCESS) {
420 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
427 * This is a callback used for chasing referrals using the same
428 * credentials as the original user on this session.
431 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
432 ber_int_t msgid, void *params )
434 struct ldapconn *lc = params;
436 return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val, LDAP_AUTH_SIMPLE );
439 /* Map API errors to protocol errors... */
442 ldap_back_map_result(SlapReply *rs)
446 case LDAP_SERVER_DOWN:
447 return LDAP_UNAVAILABLE;
448 case LDAP_LOCAL_ERROR:
450 case LDAP_ENCODING_ERROR:
451 case LDAP_DECODING_ERROR:
452 return LDAP_PROTOCOL_ERROR;
454 return LDAP_UNAVAILABLE;
455 case LDAP_AUTH_UNKNOWN:
456 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
457 case LDAP_FILTER_ERROR:
458 rs->sr_text = "Filter error";
460 case LDAP_USER_CANCELLED:
461 rs->sr_text = "User cancelled";
463 case LDAP_PARAM_ERROR:
464 return LDAP_PROTOCOL_ERROR;
467 case LDAP_CONNECT_ERROR:
468 return LDAP_UNAVAILABLE;
469 case LDAP_NOT_SUPPORTED:
470 return LDAP_UNWILLING_TO_PERFORM;
471 case LDAP_CONTROL_NOT_FOUND:
472 return LDAP_PROTOCOL_ERROR;
473 case LDAP_NO_RESULTS_RETURNED:
474 return LDAP_NO_SUCH_OBJECT;
475 case LDAP_MORE_RESULTS_TO_RETURN:
476 rs->sr_text = "More results to return";
478 case LDAP_CLIENT_LOOP:
479 case LDAP_REFERRAL_LIMIT_EXCEEDED:
480 return LDAP_LOOP_DETECT;
482 if LDAP_API_ERROR(rs->sr_err)
490 ldap_back_op_result(struct ldapconn *lc, Operation *op, SlapReply *rs,
491 ber_int_t msgid, int sendok)
493 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
499 rs->sr_matched = NULL;
501 if (rs->sr_err == LDAP_SUCCESS) {
502 if (ldap_result(lc->ld, msgid, 1, NULL, &res) == -1) {
503 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER,
506 int rc = ldap_parse_result(lc->ld, res, &rs->sr_err,
507 &match, &text, NULL, NULL, 1);
509 if (rc != LDAP_SUCCESS) rs->sr_err = rc;
513 if (rs->sr_err != LDAP_SUCCESS) {
514 rs->sr_err = ldap_back_map_result(rs);
516 /* internal ops must not reply to client */
517 if ( op->o_conn && !op->o_do_not_cache && match ) {
518 struct berval dn, mdn;
521 dc.rwmap = &li->rwmap;
522 #ifdef ENABLE_REWRITE
523 dc.conn = op->o_conn;
525 dc.ctx = "matchedDn";
530 ber_str2bv(match, 0, 0, &dn);
531 ldap_back_dn_massage(&dc, &dn, &mdn);
532 rs->sr_matched = mdn.bv_val;
536 if (op->o_conn && (sendok || rs->sr_err != LDAP_SUCCESS)) {
537 send_ldap_result( op, rs );
540 if ( rs->sr_matched != match ) {
541 free( (char *)rs->sr_matched );
543 rs->sr_matched = NULL;
544 ldap_memfree( match );
547 ldap_memfree( text );
550 return( (rs->sr_err == LDAP_SUCCESS) ? 0 : -1 );