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 };
66 lc = ldap_back_getconn(op, rs);
72 * Rewrite the bind dn if needed
75 switch ( rewrite_session( li->rwinfo, "bindDn",
77 op->o_conn, &mdn.bv_val ) ) {
78 case REWRITE_REGEXEC_OK:
79 if ( mdn.bv_val == NULL ) {
82 mdn.bv_len = strlen( mdn.bv_val );
86 LDAP_LOG( BACK_LDAP, DETAIL1,
87 "[rw] bindDn: \"%s\" -> \"%s\"\n",
88 op->o_req_dn.bv_val, mdn.bv_val, 0 );
89 #else /* !NEW_LOGGING */
90 Debug( LDAP_DEBUG_ARGS, "rw> bindDn: \"%s\" -> \"%s\"\n",
91 op->o_req_dn.bv_val, mdn.bv_val, 0 );
92 #endif /* !NEW_LOGGING */
95 case REWRITE_REGEXEC_UNWILLING:
96 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
97 "Operation not allowed" );
100 case REWRITE_REGEXEC_ERR:
101 send_ldap_error( op, rs, LDAP_OTHER,
105 #else /* !ENABLE_REWRITE */
106 ldap_back_dn_massage( li, &op->o_req_dn, &mdn, 0, 1 );
107 #endif /* !ENABLE_REWRITE */
109 if ( lc->bound_dn.bv_val ) {
110 ch_free( lc->bound_dn.bv_val );
111 lc->bound_dn.bv_len = 0;
112 lc->bound_dn.bv_val = NULL;
115 /* method is always LDAP_AUTH_SIMPLE if we got here */
116 rc = ldap_sasl_bind(lc->ld, mdn.bv_val, LDAP_SASL_SIMPLE,
117 &op->oq_bind.rb_cred, op->o_ctrls, NULL, &msgid);
118 rc = ldap_back_op_result( li, lc, op, rs, msgid, rc, 1 );
119 if (rc == LDAP_SUCCESS) {
121 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
124 ber_dupbv( &lc->bound_dn, &op->o_req_dn );
126 if ( li->savecred ) {
127 if ( lc->cred.bv_val )
128 ch_free( lc->cred.bv_val );
129 ber_dupbv( &lc->cred, &op->oq_bind.rb_cred );
130 ldap_set_rebind_proc( lc->ld, ldap_back_rebind, lc );
134 /* must re-insert if local DN changed as result of bind */
135 if ( lc->bound && !bvmatch(&op->o_req_ndn, &lc->local_dn ) ) {
138 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
139 lc = avl_delete( &li->conntree, (caddr_t)lc,
140 ldap_back_conn_cmp );
141 if ( lc->local_dn.bv_val )
142 ch_free( lc->local_dn.bv_val );
143 ber_dupbv( &lc->local_dn, &op->o_req_ndn );
144 lerr = avl_insert( &li->conntree, (caddr_t)lc,
145 ldap_back_conn_cmp, ldap_back_conn_dup );
146 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
148 ldap_back_conn_free( lc );
158 * compares two struct ldapconn based on the value of the conn pointer;
167 const struct ldapconn *lc1 = (const struct ldapconn *)c1;
168 const struct ldapconn *lc2 = (const struct ldapconn *)c2;
171 /* If local DNs don't match, it is definitely not a match */
172 if ( ( rc = ber_bvcmp( &lc1->local_dn, &lc2->local_dn )) )
175 /* For shared sessions, conn is NULL. Only explicitly
176 * bound sessions will have non-NULL conn.
178 return lc1->conn - lc2->conn;
184 * returns -1 in case a duplicate struct ldapconn has been inserted;
193 struct ldapconn *lc1 = (struct ldapconn *)c1;
194 struct ldapconn *lc2 = (struct ldapconn *)c2;
196 /* Cannot have more than one shared session with same DN */
197 if ( dn_match( &lc1->local_dn, &lc2->local_dn ) &&
198 lc1->conn == lc2->conn ) return -1;
203 #if PRINT_CONNTREE > 0
204 static void ravl_print( Avlnode *root, int depth )
212 ravl_print( root->avl_right, depth+1 );
214 for ( i = 0; i < depth; i++ )
218 printf( "lc(%lx) local(%s) conn(%lx) %d\n",
219 lc, lc->local_dn.bv_val, lc->conn, root->avl_bf );
221 ravl_print( root->avl_left, depth+1 );
224 static void myprint( Avlnode *root )
226 printf( "********\n" );
229 printf( "\tNULL\n" );
232 ravl_print( root, 0 );
234 printf( "********\n" );
236 #endif /* PRINT_CONNTREE */
239 ldap_back_getconn(Operation *op, SlapReply *rs)
241 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
242 struct ldapconn *lc, lc_curr;
247 * The local DN is the op->o_req_ndn if binding, otherwise
248 * it's the op->o_conn->c_ndn
250 struct berval *cdn = &op->o_conn->c_ndn;
252 /* Searches for a ldapconn in the avl tree */
254 /* Explicit binds must not be shared */
255 if ( op->o_tag == LDAP_REQ_BIND ) {
256 lc_curr.conn = op->o_conn;
257 cdn = &op->o_req_ndn;
259 } else if ( op->o_conn->c_ndn.bv_len ) {
260 lc_curr.conn = op->o_conn;
266 /* Internal searches are privileged. So is root. */
267 if ( op->o_do_not_cache || be_isroot( li->be, &op->o_ndn ) ) {
268 lc_curr.local_dn = li->be->be_rootndn;
271 lc_curr.local_dn = *cdn;
273 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
274 lc = (struct ldapconn *)avl_find( li->conntree,
275 (caddr_t)&lc_curr, ldap_back_conn_cmp );
276 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
278 /* Looks like we didn't get a bind. Open a new session... */
280 int vers = op->o_conn->c_protocol;
281 rs->sr_err = ldap_initialize(&ld, li->url);
283 if (rs->sr_err != LDAP_SUCCESS) {
284 rs->sr_err = ldap_back_map_result(rs->sr_err);
285 rs->sr_text = "ldap_initialize() failed";
286 send_ldap_result( op, rs );
289 /* Set LDAP version. This will always succeed: If the client
290 * bound with a particular version, then so can we.
292 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION,
293 (const void *)&vers);
294 /* FIXME: configurable? */
295 ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON);
297 lc = (struct ldapconn *)ch_malloc(sizeof(struct ldapconn));
298 lc->conn = lc_curr.conn;
300 ber_dupbv( &lc->local_dn, &lc_curr.local_dn );
303 ber_dupbv( &lc->cred, &li->bindpw );
306 lc->cred.bv_val = NULL;
309 ldap_pvt_thread_mutex_init( &lc->lc_mutex );
311 #ifdef ENABLE_REWRITE
313 * Sets a cookie for the rewrite session
315 * FIXME: the o_conn might be no longer valid,
316 * since we may have different entries
317 * for the same connection
319 ( void )rewrite_session_init( li->rwinfo, op->o_conn );
320 #endif /* ENABLE_REWRITE */
322 if ( !is_priv && op->o_conn->c_dn.bv_len != 0 ) {
325 * Rewrite the bind dn if needed
327 #ifdef ENABLE_REWRITE
328 lc->bound_dn.bv_val = NULL;
329 lc->bound_dn.bv_len = 0;
330 switch ( rewrite_session( li->rwinfo, "bindDn",
331 op->o_conn->c_dn.bv_val,
333 &lc->bound_dn.bv_val ) ) {
334 case REWRITE_REGEXEC_OK:
335 if ( lc->bound_dn.bv_val == NULL ) {
336 ber_dupbv( &lc->bound_dn,
339 lc->bound_dn.bv_len = strlen( lc->bound_dn.bv_val );
342 LDAP_LOG( BACK_LDAP, DETAIL1,
343 "[rw] bindDn: \"%s\" ->"
345 op->o_conn->c_dn.bv_val,
346 lc->bound_dn.bv_val, 0 );
347 #else /* !NEW_LOGGING */
348 Debug( LDAP_DEBUG_ARGS,
349 "rw> bindDn: \"%s\" ->"
351 op->o_conn->c_dn.bv_val,
352 lc->bound_dn.bv_val, 0 );
353 #endif /* !NEW_LOGGING */
356 case REWRITE_REGEXEC_UNWILLING:
357 send_ldap_error( op, rs,
358 LDAP_UNWILLING_TO_PERFORM,
359 "Operation not allowed" );
362 case REWRITE_REGEXEC_ERR:
363 send_ldap_error( op, rs,
369 #else /* !ENABLE_REWRITE */
371 ldap_back_dn_massage( li, &op->o_conn->c_dn, &bv, 0, 1 );
372 if ( bv.bv_val == op->o_conn->c_dn.bv_val ) {
373 ber_dupbv( &lc->bound_dn, &bv );
377 #endif /* !ENABLE_REWRITE */
380 lc->bound_dn.bv_val = NULL;
381 lc->bound_dn.bv_len = 0;
385 /* Inserts the newly created ldapconn in the avl tree */
386 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
387 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
388 ldap_back_conn_cmp, ldap_back_conn_dup );
390 #if PRINT_CONNTREE > 0
391 myprint( li->conntree );
392 #endif /* PRINT_CONNTREE */
394 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
397 LDAP_LOG( BACK_LDAP, INFO,
398 "ldap_back_getconn: conn %lx inserted\n", lc, 0, 0);
399 #else /* !NEW_LOGGING */
400 Debug( LDAP_DEBUG_TRACE,
401 "=>ldap_back_getconn: conn %lx inserted\n", lc, 0, 0 );
402 #endif /* !NEW_LOGGING */
404 /* Err could be -1 in case a duplicate ldapconn is inserted */
405 if ( rs->sr_err != 0 ) {
406 ldap_back_conn_free( lc );
407 send_ldap_error( op, rs, LDAP_OTHER,
408 "internal server error" );
413 LDAP_LOG( BACK_LDAP, INFO,
414 "ldap_back_getconn: conn %lx fetched\n",
416 #else /* !NEW_LOGGING */
417 Debug( LDAP_DEBUG_TRACE,
418 "=>ldap_back_getconn: conn %lx fetched\n", lc, 0, 0 );
419 #endif /* !NEW_LOGGING */
428 * Note: as the check for the value of lc->bound was already here, I removed
429 * it from all the callers, and I made the function return the flag, so
430 * it can be used to simplify the check.
433 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs )
435 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
439 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
441 rc = ldap_sasl_bind(lc->ld, lc->bound_dn.bv_val,
442 LDAP_SASL_SIMPLE, &lc->cred, NULL, NULL, &msgid);
443 rc = ldap_back_op_result( li, lc, op, rs, msgid, rc, 0 );
444 if (rc == LDAP_SUCCESS) {
449 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
456 * This is a callback used for chasing referrals using the same
457 * credentials as the original user on this session.
460 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
461 ber_int_t msgid, void *params )
463 struct ldapconn *lc = params;
465 return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val, LDAP_AUTH_SIMPLE );
468 /* Map API errors to protocol errors... */
471 ldap_back_map_result(int err)
475 case LDAP_SERVER_DOWN:
476 return LDAP_UNAVAILABLE;
477 case LDAP_LOCAL_ERROR:
479 case LDAP_ENCODING_ERROR:
480 case LDAP_DECODING_ERROR:
481 return LDAP_PROTOCOL_ERROR;
483 return LDAP_UNAVAILABLE;
484 case LDAP_AUTH_UNKNOWN:
485 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
486 case LDAP_FILTER_ERROR:
488 case LDAP_USER_CANCELLED:
490 case LDAP_PARAM_ERROR:
491 return LDAP_PROTOCOL_ERROR;
494 case LDAP_CONNECT_ERROR:
495 return LDAP_UNAVAILABLE;
496 case LDAP_NOT_SUPPORTED:
497 return LDAP_UNWILLING_TO_PERFORM;
498 case LDAP_CONTROL_NOT_FOUND:
499 return LDAP_PROTOCOL_ERROR;
500 case LDAP_NO_RESULTS_RETURNED:
501 return LDAP_NO_SUCH_OBJECT;
502 case LDAP_MORE_RESULTS_TO_RETURN:
504 case LDAP_CLIENT_LOOP:
505 case LDAP_REFERRAL_LIMIT_EXCEEDED:
506 return LDAP_LOOP_DETECT;
508 if LDAP_API_ERROR(err)
516 ldap_back_op_result(struct ldapinfo *li, struct ldapconn *lc,
517 Operation *op, SlapReply *rs, ber_int_t msgid, int err, int sendok)
524 rs->sr_matched = NULL;
526 if (err == LDAP_SUCCESS) {
527 if (ldap_result(lc->ld, msgid, 1, NULL, &res) == -1) {
528 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER, &err);
530 rc = ldap_parse_result(lc->ld, res, &err, &match,
531 (char **)&rs->sr_text, NULL, NULL, 1);
532 if (rc != LDAP_SUCCESS) err = rc;
535 if (err != LDAP_SUCCESS) {
536 err = ldap_back_map_result(err);
538 /* internal ops must not reply to client */
539 if ( op->o_conn && !op->o_do_not_cache ) {
540 #ifdef ENABLE_REWRITE
543 switch(rewrite_session(li->rwinfo, "matchedDn", match, op->o_conn,
544 (char **)&rs->sr_matched)) {
545 case REWRITE_REGEXEC_OK:
546 if (!rs->sr_matched) rs->sr_matched = match; break;
547 case REWRITE_REGEXEC_UNWILLING:
548 case REWRITE_REGEXEC_ERR:
553 struct berval dn, mdn;
555 ber_str2bv(match, 0, 0, &dn);
556 ldap_back_dn_massage(li, &dn, &mdn, 0, 0);
557 rs->sr_matched = mdn.bv_val;
562 if (sendok || err != LDAP_SUCCESS) {
564 send_ldap_result( op, rs );
566 if (rs->sr_matched != match) free((char *)rs->sr_matched);
567 rs->sr_matched = NULL;
568 if ( match ) ldap_memfree( match );
570 ldap_memfree( (char *)rs->sr_text );
573 return( (err==LDAP_SUCCESS) ? 0 : -1 );