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 rs->sr_err = 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( lc, op, rs, msgid, 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 );
128 if ( li->savecred ) {
129 if ( lc->cred.bv_val )
130 ch_free( lc->cred.bv_val );
131 ber_dupbv( &lc->cred, &op->oq_bind.rb_cred );
132 ldap_set_rebind_proc( lc->ld, ldap_back_rebind, lc );
136 /* must re-insert if local DN changed as result of bind */
137 if ( lc->bound && !bvmatch(&op->o_req_ndn, &lc->local_dn ) ) {
140 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
141 lc = avl_delete( &li->conntree, (caddr_t)lc,
142 ldap_back_conn_cmp );
143 if ( lc->local_dn.bv_val )
144 ch_free( lc->local_dn.bv_val );
145 ber_dupbv( &lc->local_dn, &op->o_req_ndn );
146 lerr = avl_insert( &li->conntree, (caddr_t)lc,
147 ldap_back_conn_cmp, ldap_back_conn_dup );
148 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
150 ldap_back_conn_free( lc );
154 if ( mdn.bv_val && mdn.bv_val != op->o_req_dn.bv_val ) {
164 * compares two struct ldapconn based on the value of the conn pointer;
173 const struct ldapconn *lc1 = (const struct ldapconn *)c1;
174 const struct ldapconn *lc2 = (const struct ldapconn *)c2;
177 /* If local DNs don't match, it is definitely not a match */
178 if ( ( rc = ber_bvcmp( &lc1->local_dn, &lc2->local_dn )) )
181 /* For shared sessions, conn is NULL. Only explicitly
182 * bound sessions will have non-NULL conn.
184 return lc1->conn - lc2->conn;
190 * returns -1 in case a duplicate struct ldapconn has been inserted;
199 struct ldapconn *lc1 = (struct ldapconn *)c1;
200 struct ldapconn *lc2 = (struct ldapconn *)c2;
202 /* Cannot have more than one shared session with same DN */
203 if ( dn_match( &lc1->local_dn, &lc2->local_dn ) &&
204 lc1->conn == lc2->conn ) return -1;
209 #if PRINT_CONNTREE > 0
210 static void ravl_print( Avlnode *root, int depth )
218 ravl_print( root->avl_right, depth+1 );
220 for ( i = 0; i < depth; i++ )
224 printf( "lc(%lx) local(%s) conn(%lx) %d\n",
225 lc, lc->local_dn.bv_val, lc->conn, root->avl_bf );
227 ravl_print( root->avl_left, depth+1 );
230 static void myprint( Avlnode *root )
232 printf( "********\n" );
235 printf( "\tNULL\n" );
238 ravl_print( root, 0 );
240 printf( "********\n" );
242 #endif /* PRINT_CONNTREE */
245 ldap_back_getconn(Operation *op, SlapReply *rs)
247 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
248 struct ldapconn *lc, lc_curr;
252 /* Searches for a ldapconn in the avl tree */
254 /* Explicit binds must not be shared */
255 if ( op->o_tag == LDAP_REQ_BIND
257 && (op->o_bd == op->o_conn->c_authz_backend ))) {
258 lc_curr.conn = op->o_conn;
263 /* Internal searches are privileged and shared. So is root. */
264 if ( op->o_do_not_cache || be_isroot( li->be, &op->o_ndn ) ) {
265 lc_curr.local_dn = li->be->be_rootndn;
269 lc_curr.local_dn = op->o_ndn;
272 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
273 lc = (struct ldapconn *)avl_find( li->conntree,
274 (caddr_t)&lc_curr, ldap_back_conn_cmp );
275 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
277 /* Looks like we didn't get a bind. Open a new session... */
279 int vers = op->o_conn->c_protocol;
280 rs->sr_err = ldap_initialize(&ld, li->url);
282 if (rs->sr_err != LDAP_SUCCESS) {
283 rs->sr_err = ldap_back_map_result(rs);
284 if (rs->sr_text == NULL) {
285 rs->sr_text = "ldap_initialize() failed";
287 send_ldap_result( op, rs );
291 /* Set LDAP version. This will always succeed: If the client
292 * bound with a particular version, then so can we.
294 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION,
295 (const void *)&vers);
296 /* FIXME: configurable? */
297 ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON);
299 lc = (struct ldapconn *)ch_malloc(sizeof(struct ldapconn));
300 lc->conn = lc_curr.conn;
302 ber_dupbv( &lc->local_dn, &lc_curr.local_dn );
304 #ifdef ENABLE_REWRITE
306 * Sets a cookie for the rewrite session
308 * FIXME: the o_conn might be no longer valid,
309 * since we may have different entries
310 * for the same connection
312 ( void )rewrite_session_init( li->rwinfo, op->o_conn );
313 #endif /* ENABLE_REWRITE */
315 ldap_pvt_thread_mutex_init( &lc->lc_mutex );
318 ber_dupbv( &lc->cred, &li->bindpw );
319 ber_dupbv( &lc->bound_dn, &li->binddn );
322 lc->cred.bv_val = NULL;
323 if ( op->o_conn->c_dn.bv_len != 0 ) {
326 * Rewrite the bind dn if needed
328 #ifdef ENABLE_REWRITE
329 lc->bound_dn.bv_val = NULL;
330 lc->bound_dn.bv_len = 0;
331 switch ( rewrite_session( li->rwinfo, "bindDn",
332 op->o_conn->c_dn.bv_val,
334 &lc->bound_dn.bv_val ) ) {
335 case REWRITE_REGEXEC_OK:
336 if ( lc->bound_dn.bv_val == NULL ) {
337 ber_dupbv( &lc->bound_dn,
340 lc->bound_dn.bv_len = strlen( lc->bound_dn.bv_val );
343 LDAP_LOG( BACK_LDAP, DETAIL1,
344 "[rw] bindDn: \"%s\" ->"
346 op->o_conn->c_dn.bv_val,
347 lc->bound_dn.bv_val, 0 );
348 #else /* !NEW_LOGGING */
349 Debug( LDAP_DEBUG_ARGS,
350 "rw> bindDn: \"%s\" ->"
352 op->o_conn->c_dn.bv_val,
353 lc->bound_dn.bv_val, 0 );
354 #endif /* !NEW_LOGGING */
357 case REWRITE_REGEXEC_UNWILLING:
358 send_ldap_error( op, rs,
359 LDAP_UNWILLING_TO_PERFORM,
360 "Operation not allowed" );
363 case REWRITE_REGEXEC_ERR:
364 send_ldap_error( op, rs,
370 #else /* !ENABLE_REWRITE */
372 ldap_back_dn_massage( li, &op->o_conn->c_dn, &bv, 0, 1 );
373 if ( bv.bv_val == op->o_conn->c_dn.bv_val ) {
374 ber_dupbv( &lc->bound_dn, &bv );
378 #endif /* !ENABLE_REWRITE */
381 lc->bound_dn.bv_val = NULL;
382 lc->bound_dn.bv_len = 0;
388 /* Inserts the newly created ldapconn in the avl tree */
389 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
390 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
391 ldap_back_conn_cmp, ldap_back_conn_dup );
393 #if PRINT_CONNTREE > 0
394 myprint( li->conntree );
395 #endif /* PRINT_CONNTREE */
397 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
400 LDAP_LOG( BACK_LDAP, INFO,
401 "ldap_back_getconn: conn %lx inserted\n", lc, 0, 0);
402 #else /* !NEW_LOGGING */
403 Debug( LDAP_DEBUG_TRACE,
404 "=>ldap_back_getconn: conn %lx inserted\n", lc, 0, 0 );
405 #endif /* !NEW_LOGGING */
407 /* Err could be -1 in case a duplicate ldapconn is inserted */
408 if ( rs->sr_err != 0 ) {
409 ldap_back_conn_free( lc );
410 send_ldap_error( op, rs, LDAP_OTHER,
411 "internal server error" );
416 LDAP_LOG( BACK_LDAP, INFO,
417 "ldap_back_getconn: conn %lx fetched\n",
419 #else /* !NEW_LOGGING */
420 Debug( LDAP_DEBUG_TRACE,
421 "=>ldap_back_getconn: conn %lx fetched\n", lc, 0, 0 );
422 #endif /* !NEW_LOGGING */
431 * Note: as the check for the value of lc->bound was already here, I removed
432 * it from all the callers, and I made the function return the flag, so
433 * it can be used to simplify the check.
436 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs )
441 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
443 rs->sr_err = ldap_sasl_bind(lc->ld, lc->bound_dn.bv_val,
444 LDAP_SASL_SIMPLE, &lc->cred, NULL, NULL, &msgid);
445 rc = ldap_back_op_result( lc, op, rs, msgid, 0 );
446 if (rc == LDAP_SUCCESS) {
451 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
458 * This is a callback used for chasing referrals using the same
459 * credentials as the original user on this session.
462 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
463 ber_int_t msgid, void *params )
465 struct ldapconn *lc = params;
467 return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val, LDAP_AUTH_SIMPLE );
470 /* Map API errors to protocol errors... */
473 ldap_back_map_result(SlapReply *rs)
477 case LDAP_SERVER_DOWN:
478 return LDAP_UNAVAILABLE;
479 case LDAP_LOCAL_ERROR:
481 case LDAP_ENCODING_ERROR:
482 case LDAP_DECODING_ERROR:
483 return LDAP_PROTOCOL_ERROR;
485 return LDAP_UNAVAILABLE;
486 case LDAP_AUTH_UNKNOWN:
487 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
488 case LDAP_FILTER_ERROR:
489 rs->sr_text = "Filter error";
491 case LDAP_USER_CANCELLED:
492 rs->sr_text = "User cancelled";
494 case LDAP_PARAM_ERROR:
495 return LDAP_PROTOCOL_ERROR;
498 case LDAP_CONNECT_ERROR:
499 return LDAP_UNAVAILABLE;
500 case LDAP_NOT_SUPPORTED:
501 return LDAP_UNWILLING_TO_PERFORM;
502 case LDAP_CONTROL_NOT_FOUND:
503 return LDAP_PROTOCOL_ERROR;
504 case LDAP_NO_RESULTS_RETURNED:
505 return LDAP_NO_SUCH_OBJECT;
506 case LDAP_MORE_RESULTS_TO_RETURN:
507 rs->sr_text = "More results to return";
509 case LDAP_CLIENT_LOOP:
510 case LDAP_REFERRAL_LIMIT_EXCEEDED:
511 return LDAP_LOOP_DETECT;
513 if LDAP_API_ERROR(rs->sr_err)
521 ldap_back_op_result(struct ldapconn *lc, Operation *op, SlapReply *rs,
522 ber_int_t msgid, int sendok)
524 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
530 rs->sr_matched = NULL;
532 if (rs->sr_err == LDAP_SUCCESS) {
533 if (ldap_result(lc->ld, msgid, 1, NULL, &res) == -1) {
534 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER,
537 int rc = ldap_parse_result(lc->ld, res, &rs->sr_err,
538 &match, &text, NULL, NULL, 1);
540 if (rc != LDAP_SUCCESS) rs->sr_err = rc;
544 if (rs->sr_err != LDAP_SUCCESS) {
545 rs->sr_err = ldap_back_map_result(rs);
547 /* internal ops must not reply to client */
548 if ( op->o_conn && !op->o_do_not_cache ) {
549 #ifdef ENABLE_REWRITE
552 switch(rewrite_session(li->rwinfo, "matchedDn", match, op->o_conn,
553 (char **)&rs->sr_matched)) {
554 case REWRITE_REGEXEC_OK:
555 if (!rs->sr_matched) rs->sr_matched = match; break;
556 case REWRITE_REGEXEC_UNWILLING:
557 case REWRITE_REGEXEC_ERR:
562 struct berval dn, mdn;
564 ber_str2bv(match, 0, 0, &dn);
565 ldap_back_dn_massage(li, &dn, &mdn, 0, 0);
566 rs->sr_matched = mdn.bv_val;
571 if (op->o_conn && (sendok || rs->sr_err != LDAP_SUCCESS)) {
572 send_ldap_result( op, rs );
575 if ( rs->sr_matched != match ) {
576 free( (char *)rs->sr_matched );
578 rs->sr_matched = NULL;
579 ldap_memfree( match );
582 ldap_memfree( text );
585 return( (rs->sr_err == LDAP_SUCCESS) ? 0 : -1 );