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;
66 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
69 struct berval mdn = { 0, NULL };
73 lc = ldap_back_getconn(li, conn, op);
79 * Rewrite the bind dn if needed
82 switch ( rewrite_session( li->rwinfo, "bindDn", dn->bv_val, conn, &mdn.bv_val ) ) {
83 case REWRITE_REGEXEC_OK:
84 if ( mdn.bv_val == NULL ) {
85 mdn.bv_val = ( char * )dn->bv_val;
88 LDAP_LOG( BACK_LDAP, DETAIL1,
89 "[rw] bindDn: \"%s\" -> \"%s\"\n", dn->bv_val, mdn.bv_val, 0 );
90 #else /* !NEW_LOGGING */
91 Debug( LDAP_DEBUG_ARGS, "rw> bindDn: \"%s\" -> \"%s\"\n%s",
92 dn->bv_val, mdn.bv_val, "" );
93 #endif /* !NEW_LOGGING */
96 case REWRITE_REGEXEC_UNWILLING:
97 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
98 NULL, "Operation not allowed", NULL, NULL );
101 case REWRITE_REGEXEC_ERR:
102 send_ldap_result( conn, op, LDAP_OTHER,
103 NULL, "Rewrite error", NULL, NULL );
106 #else /* !ENABLE_REWRITE */
107 ldap_back_dn_massage( li, dn, &mdn, 0, 1 );
108 #endif /* !ENABLE_REWRITE */
110 if ( lc->bound_dn.bv_val ) {
111 ch_free( lc->bound_dn.bv_val );
112 lc->bound_dn.bv_len = 0;
113 lc->bound_dn.bv_val = NULL;
116 /* method is always LDAP_AUTH_SIMPLE if we got here */
117 rc = ldap_sasl_bind(lc->ld, mdn.bv_val, LDAP_SASL_SIMPLE,
118 cred, op->o_ctrls, NULL, &msgid);
119 rc = ldap_back_op_result( li, lc, conn, op, msgid, rc, 1 );
120 if (rc == LDAP_SUCCESS) {
122 if ( mdn.bv_val != dn->bv_val ) {
125 ber_dupbv( &lc->bound_dn, dn );
127 if ( li->savecred ) {
128 if ( lc->cred.bv_val )
129 ch_free( lc->cred.bv_val );
130 ber_dupbv( &lc->cred, cred );
131 ldap_set_rebind_proc( lc->ld, ldap_back_rebind, lc );
135 /* must re-insert if local DN changed as result of bind */
136 if ( lc->bound && ber_bvcmp(ndn, &lc->local_dn ) ) {
138 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
139 lc = avl_delete( &li->conntree, (caddr_t)lc, ldap_back_conn_cmp );
140 if ( lc->local_dn.bv_val )
141 ch_free( lc->local_dn.bv_val );
142 ber_dupbv( &lc->local_dn, ndn );
143 err = avl_insert( &li->conntree, (caddr_t)lc,
144 ldap_back_conn_cmp, ldap_back_conn_dup );
145 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
147 ldap_back_conn_free( lc );
157 * compares two struct ldapconn based on the value of the conn pointer;
166 const struct ldapconn *lc1 = (const struct ldapconn *)c1;
167 const struct ldapconn *lc2 = (const struct ldapconn *)c2;
170 /* If local DNs don't match, it is definitely not a match */
171 if ( ( rc = ber_bvcmp( &lc1->local_dn, &lc2->local_dn )) )
174 /* For shared sessions, conn is NULL. Only explicitly
175 * bound sessions will have non-NULL conn.
177 return lc1->conn - lc2->conn;
183 * returns -1 in case a duplicate struct ldapconn has been inserted;
192 struct ldapconn *lc1 = (struct ldapconn *)c1;
193 struct ldapconn *lc2 = (struct ldapconn *)c2;
195 /* Cannot have more than one shared session with same DN */
196 if ( dn_match( &lc1->local_dn, &lc2->local_dn ) &&
197 lc1->conn == lc2->conn ) return -1;
202 #if PRINT_CONNTREE > 0
203 static void ravl_print( Avlnode *root, int depth )
211 ravl_print( root->avl_right, depth+1 );
213 for ( i = 0; i < depth; i++ )
217 printf( "lc(%lx) local(%s) conn(%lx) %d\n", lc, lc->local_dn.bv_val, lc->conn, root->avl_bf );
219 ravl_print( root->avl_left, depth+1 );
222 static void myprint( Avlnode *root )
224 printf( "********\n" );
227 printf( "\tNULL\n" );
230 ravl_print( root, 0 );
232 printf( "********\n" );
234 #endif /* PRINT_CONNTREE */
237 ldap_back_getconn(struct ldapinfo *li, Connection *conn, Operation *op)
239 struct ldapconn *lc, lc_curr;
243 /* Searches for a ldapconn in the avl tree */
245 /* Explicit binds must not be shared */
246 if ( op->o_tag == LDAP_REQ_BIND ) {
252 /* Internal searches are privileged. So is root. */
253 if ( op->o_do_not_cache || be_isroot( li->be, &op->o_ndn ) ) {
254 lc_curr.local_dn = li->be->be_rootndn;
257 lc_curr.local_dn = op->o_ndn;
259 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
260 lc = (struct ldapconn *)avl_find( li->conntree,
261 (caddr_t)&lc_curr, ldap_back_conn_cmp );
262 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
264 /* Looks like we didn't get a bind. Open a new session... */
266 int vers = conn->c_protocol;
267 int err = ldap_initialize(&ld, li->url);
269 if (err != LDAP_SUCCESS) {
270 err = ldap_back_map_result(err);
271 send_ldap_result( conn, op, err,
272 NULL, "ldap_initialize() failed", NULL, NULL );
275 /* Set LDAP version. This will always succeed: If the client
276 * bound with a particular version, then so can we.
278 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &vers);
280 lc = (struct ldapconn *)ch_malloc(sizeof(struct ldapconn));
281 lc->conn = lc_curr.conn;
283 ber_dupbv( &lc->local_dn, &lc_curr.local_dn );
286 ber_str2bv( li->bindpw, 0, 1, &lc->cred );
289 lc->cred.bv_val = NULL;
292 ldap_pvt_thread_mutex_init( &lc->lc_mutex );
294 #ifdef ENABLE_REWRITE
296 * Sets a cookie for the rewrite session
298 ( void )rewrite_session_init( li->rwinfo, conn );
299 #endif /* ENABLE_REWRITE */
301 if ( conn->c_dn.bv_len != 0 ) {
304 * Rewrite the bind dn if needed
306 #ifdef ENABLE_REWRITE
307 lc->bound_dn.bv_val = NULL;
308 lc->bound_dn.bv_len = 0;
309 switch ( rewrite_session( li->rwinfo, "bindDn",
310 conn->c_dn.bv_val, conn,
311 &lc->bound_dn.bv_val ) ) {
312 case REWRITE_REGEXEC_OK:
313 if ( lc->bound_dn.bv_val == NULL ) {
314 ber_dupbv( &lc->bound_dn,
318 LDAP_LOG( BACK_LDAP, DETAIL1,
319 "[rw] bindDn: \"%s\" ->"
322 lc->bound_dn.bv_val, "" );
323 #else /* !NEW_LOGGING */
324 Debug( LDAP_DEBUG_ARGS,
325 "rw> bindDn: \"%s\" ->"
328 lc->bound_dn.bv_val, "" );
329 #endif /* !NEW_LOGGING */
332 case REWRITE_REGEXEC_UNWILLING:
333 send_ldap_result( conn, op,
334 LDAP_UNWILLING_TO_PERFORM,
335 NULL, "Operation not allowed",
339 case REWRITE_REGEXEC_ERR:
340 send_ldap_result( conn, op,
342 NULL, "Rewrite error",
347 #else /* !ENABLE_REWRITE */
349 ldap_back_dn_massage( li, &conn->c_dn, &bv, 0, 1 );
350 if ( bv.bv_val == conn->c_dn.bv_val ) {
351 ber_dupbv( &lc->bound_dn, &bv );
355 #endif /* !ENABLE_REWRITE */
358 lc->bound_dn.bv_val = NULL;
359 lc->bound_dn.bv_len = 0;
363 /* Inserts the newly created ldapconn in the avl tree */
364 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
365 err = avl_insert( &li->conntree, (caddr_t)lc,
366 ldap_back_conn_cmp, ldap_back_conn_dup );
368 #if PRINT_CONNTREE > 0
369 myprint( li->conntree );
370 #endif /* PRINT_CONNTREE */
372 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
375 LDAP_LOG( BACK_LDAP, INFO,
376 "ldap_back_getconn: conn %lx inserted\n", lc, 0, 0);
377 #else /* !NEW_LOGGING */
378 Debug( LDAP_DEBUG_TRACE,
379 "=>ldap_back_getconn: conn %lx inserted\n%s%s",
381 #endif /* !NEW_LOGGING */
383 /* Err could be -1 in case a duplicate ldapconn is inserted */
385 ldap_back_conn_free( lc );
386 send_ldap_result( conn, op, LDAP_OTHER,
387 NULL, "internal server error", NULL, NULL );
392 LDAP_LOG( BACK_LDAP, INFO,
393 "ldap_back_getconn: conn %lx fetched\n",
395 #else /* !NEW_LOGGING */
396 Debug( LDAP_DEBUG_TRACE,
397 "=>ldap_back_getconn: conn %lx fetched%s%s\n",
399 #endif /* !NEW_LOGGING */
408 * Note: as the check for the value of lc->bound was already here, I removed
409 * it from all the callers, and I made the function return the flag, so
410 * it can be used to simplify the check.
413 ldap_back_dobind( struct ldapinfo *li, struct ldapconn *lc, Connection *conn, Operation *op )
418 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
420 rc = ldap_sasl_bind(lc->ld, lc->bound_dn.bv_val,
421 LDAP_SASL_SIMPLE, &lc->cred, NULL, NULL, &msgid);
422 rc = ldap_back_op_result( li, lc, conn, op, msgid, rc, 0 );
423 if (rc == LDAP_SUCCESS) {
428 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
435 * This is a callback used for chasing referrals using the same
436 * credentials as the original user on this session.
439 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
440 ber_int_t msgid, void *params )
442 struct ldapconn *lc = params;
444 return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val, LDAP_AUTH_SIMPLE );
447 /* Map API errors to protocol errors... */
450 ldap_back_map_result(int err)
454 case LDAP_SERVER_DOWN:
455 return LDAP_UNAVAILABLE;
456 case LDAP_LOCAL_ERROR:
458 case LDAP_ENCODING_ERROR:
459 case LDAP_DECODING_ERROR:
460 return LDAP_PROTOCOL_ERROR;
462 return LDAP_UNAVAILABLE;
463 case LDAP_AUTH_UNKNOWN:
464 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
465 case LDAP_FILTER_ERROR:
467 case LDAP_USER_CANCELLED:
469 case LDAP_PARAM_ERROR:
470 return LDAP_PROTOCOL_ERROR;
473 case LDAP_CONNECT_ERROR:
474 return LDAP_UNAVAILABLE;
475 case LDAP_NOT_SUPPORTED:
476 return LDAP_UNWILLING_TO_PERFORM;
477 case LDAP_CONTROL_NOT_FOUND:
478 return LDAP_PROTOCOL_ERROR;
479 case LDAP_NO_RESULTS_RETURNED:
480 return LDAP_NO_SUCH_OBJECT;
481 case LDAP_MORE_RESULTS_TO_RETURN:
483 case LDAP_CLIENT_LOOP:
484 case LDAP_REFERRAL_LIMIT_EXCEEDED:
485 return LDAP_LOOP_DETECT;
487 if LDAP_API_ERROR(err)
495 ldap_back_op_result(struct ldapinfo *li, struct ldapconn *lc,
496 Connection *conn, Operation *op, ber_int_t msgid, int err, int sendok)
504 if (err == LDAP_SUCCESS) {
505 if (ldap_result(lc->ld, msgid, 1, NULL, &res) == -1) {
506 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER, &err);
508 rc = ldap_parse_result(lc->ld, res, &err, &match,
509 &msg, NULL, NULL, 1);
510 if (rc != LDAP_SUCCESS) err = rc;
513 if (err != LDAP_SUCCESS) {
514 err = ldap_back_map_result(err);
516 /* internal ops must not reply to client */
517 if ( conn && !op->o_do_not_cache ) {
518 #ifdef ENABLE_REWRITE
521 switch(rewrite_session(li->rwinfo, "matchedDn", match, conn,
523 case REWRITE_REGEXEC_OK:
524 if (!mmatch) mmatch = match; break;
525 case REWRITE_REGEXEC_UNWILLING:
526 case REWRITE_REGEXEC_ERR:
531 struct berval dn, mdn;
533 ber_str2bv(match, 0, 0, &dn);
534 ldap_back_dn_massage(li, &dn, &mdn, 0, 0);
540 if (sendok || err != LDAP_SUCCESS) {
541 send_ldap_result( conn, op, err, mmatch, msg, NULL, NULL );
543 if (mmatch != match) free(mmatch);
544 if ( match ) free( match );
545 if ( msg ) free( msg );
546 return( (err==LDAP_SUCCESS) ? 0 : -1 );