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 };
72 lc = ldap_back_getconn(li, conn, op);
78 if ( ldap_set_option( lc->ld, LDAP_OPT_SERVER_CONTROLS,
79 op->o_ctrls ) != LDAP_SUCCESS ) {
80 ldap_back_op_result( lc, conn, op );
86 * Rewrite the bind dn if needed
89 switch ( rewrite_session( li->rwinfo, "bindDn", dn->bv_val, conn, &mdn.bv_val ) ) {
90 case REWRITE_REGEXEC_OK:
91 if ( mdn.bv_val == NULL ) {
92 mdn.bv_val = ( char * )dn->bv_val;
95 LDAP_LOG( BACK_LDAP, DETAIL1,
96 "[rw] bindDn: \"%s\" -> \"%s\"\n", dn->bv_val, mdn.bv_val, 0 );
97 #else /* !NEW_LOGGING */
98 Debug( LDAP_DEBUG_ARGS, "rw> bindDn: \"%s\" -> \"%s\"\n%s",
99 dn->bv_val, mdn.bv_val, "" );
100 #endif /* !NEW_LOGGING */
103 case REWRITE_REGEXEC_UNWILLING:
104 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
105 NULL, "Operation not allowed", NULL, NULL );
108 case REWRITE_REGEXEC_ERR:
109 send_ldap_result( conn, op, LDAP_OTHER,
110 NULL, "Rewrite error", NULL, NULL );
113 #else /* !ENABLE_REWRITE */
114 ldap_back_dn_massage( li, dn, &mdn, 0, 1 );
115 #endif /* !ENABLE_REWRITE */
117 if ( lc->bound_dn.bv_val ) {
118 ch_free( lc->bound_dn.bv_val );
119 lc->bound_dn.bv_len = 0;
120 lc->bound_dn.bv_val = NULL;
123 rc = ldap_bind_s(lc->ld, mdn.bv_val, cred->bv_val, method);
124 if (rc != LDAP_SUCCESS) {
125 rc = ldap_back_op_result( lc, conn, op );
128 if ( mdn.bv_val != dn->bv_val ) {
131 ber_dupbv( &lc->bound_dn, dn );
135 if ( li->savecred ) {
136 if ( lc->cred.bv_val )
137 ch_free( lc->cred.bv_val );
138 ber_dupbv( &lc->cred, cred );
139 ldap_set_rebind_proc( lc->ld, ldap_back_rebind, lc );
142 /* must re-insert if local DN changed as result of bind */
143 if ( lc->bound && ber_bvcmp(ndn, &lc->local_dn ) ) {
145 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
146 lc = avl_delete( &li->conntree, (caddr_t)lc, ldap_back_conn_cmp );
147 if ( lc->local_dn.bv_val )
148 ch_free( lc->local_dn.bv_val );
149 ber_dupbv( &lc->local_dn, ndn );
150 err = avl_insert( &li->conntree, (caddr_t)lc,
151 ldap_back_conn_cmp, ldap_back_conn_dup );
152 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
154 ldap_back_conn_free( lc );
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", lc, lc->local_dn.bv_val, lc->conn, root->avl_bf );
226 ravl_print( root->avl_left, depth+1 );
229 static void myprint( Avlnode *root )
231 printf( "********\n" );
234 printf( "\tNULL\n" );
237 ravl_print( root, 0 );
239 printf( "********\n" );
241 #endif /* PRINT_CONNTREE */
244 ldap_back_getconn(struct ldapinfo *li, Connection *conn, Operation *op)
246 struct ldapconn *lc, lc_curr;
250 /* Searches for a ldapconn in the avl tree */
252 /* Explicit binds must not be shared */
253 if ( op->o_tag == LDAP_REQ_BIND ) {
259 /* Internal searches are privileged. 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;
264 lc_curr.local_dn = op->o_ndn;
266 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
267 lc = (struct ldapconn *)avl_find( li->conntree,
268 (caddr_t)&lc_curr, ldap_back_conn_cmp );
269 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
271 /* Looks like we didn't get a bind. Open a new session... */
273 int vers = conn->c_protocol;
274 int err = ldap_initialize(&ld, li->url);
276 if (err != LDAP_SUCCESS) {
277 err = ldap_back_map_result(err);
278 send_ldap_result( conn, op, err,
279 NULL, "ldap_initialize() failed", NULL, NULL );
282 /* Set LDAP version. This will always succeed: If the client
283 * bound with a particular version, then so can we.
285 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &vers);
287 lc = (struct ldapconn *)ch_malloc(sizeof(struct ldapconn));
288 lc->conn = lc_curr.conn;
290 ber_dupbv( &lc->local_dn, &lc_curr.local_dn );
293 ber_str2bv( li->bindpw, 0, 1, &lc->cred );
296 lc->cred.bv_val = NULL;
299 ldap_pvt_thread_mutex_init( &lc->lc_mutex );
301 #ifdef ENABLE_REWRITE
303 * Sets a cookie for the rewrite session
305 ( void )rewrite_session_init( li->rwinfo, conn );
306 #endif /* ENABLE_REWRITE */
308 if ( conn->c_dn.bv_len != 0 ) {
311 * Rewrite the bind dn if needed
313 #ifdef ENABLE_REWRITE
314 lc->bound_dn.bv_val = NULL;
315 lc->bound_dn.bv_len = 0;
316 switch ( rewrite_session( li->rwinfo, "bindDn",
317 conn->c_dn.bv_val, conn,
318 &lc->bound_dn.bv_val ) ) {
319 case REWRITE_REGEXEC_OK:
320 if ( lc->bound_dn.bv_val == NULL ) {
321 ber_dupbv( &lc->bound_dn,
325 LDAP_LOG( BACK_LDAP, DETAIL1,
326 "[rw] bindDn: \"%s\" ->"
329 lc->bound_dn.bv_val, "" );
330 #else /* !NEW_LOGGING */
331 Debug( LDAP_DEBUG_ARGS,
332 "rw> bindDn: \"%s\" ->"
335 lc->bound_dn.bv_val, "" );
336 #endif /* !NEW_LOGGING */
339 case REWRITE_REGEXEC_UNWILLING:
340 send_ldap_result( conn, op,
341 LDAP_UNWILLING_TO_PERFORM,
342 NULL, "Operation not allowed",
346 case REWRITE_REGEXEC_ERR:
347 send_ldap_result( conn, op,
349 NULL, "Rewrite error",
354 #else /* !ENABLE_REWRITE */
356 ldap_back_dn_massage( li, &conn->c_dn, &bv, 0, 1 );
357 if ( bv.bv_val == conn->c_dn.bv_val ) {
358 ber_dupbv( &lc->bound_dn, &bv );
362 #endif /* !ENABLE_REWRITE */
365 lc->bound_dn.bv_val = NULL;
366 lc->bound_dn.bv_len = 0;
370 /* Inserts the newly created ldapconn in the avl tree */
371 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
372 err = avl_insert( &li->conntree, (caddr_t)lc,
373 ldap_back_conn_cmp, ldap_back_conn_dup );
375 #if PRINT_CONNTREE > 0
376 myprint( li->conntree );
377 #endif /* PRINT_CONNTREE */
379 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
382 LDAP_LOG( BACK_LDAP, INFO,
383 "ldap_back_getconn: conn %lx inserted\n", lc, 0, 0);
384 #else /* !NEW_LOGGING */
385 Debug( LDAP_DEBUG_TRACE,
386 "=>ldap_back_getconn: conn %lx inserted\n%s%s",
388 #endif /* !NEW_LOGGING */
390 /* Err could be -1 in case a duplicate ldapconn is inserted */
392 ldap_back_conn_free( lc );
393 send_ldap_result( conn, op, LDAP_OTHER,
394 NULL, "internal server error", NULL, NULL );
399 LDAP_LOG( BACK_LDAP, INFO,
400 "ldap_back_getconn: conn %lx fetched\n",
402 #else /* !NEW_LOGGING */
403 Debug( LDAP_DEBUG_TRACE,
404 "=>ldap_back_getconn: conn %lx fetched%s%s\n",
406 #endif /* !NEW_LOGGING */
415 * Note: as the check for the value of lc->bound was already here, I removed
416 * it from all the callers, and I made the function return the flag, so
417 * it can be used to simplify the check.
420 ldap_back_dobind( struct ldapconn *lc, Connection *conn, Operation *op )
424 ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
426 if ( ldap_set_option( lc->ld, LDAP_OPT_SERVER_CONTROLS,
427 op->o_ctrls ) != LDAP_SUCCESS ) {
428 ldap_back_op_result( lc, conn, op );
438 if ( ldap_bind_s( lc->ld, lc->bound_dn.bv_val, lc->cred.bv_val,
439 LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
440 ldap_back_op_result( lc, conn, op );
446 ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
453 * This is a callback used for chasing referrals using the same
454 * credentials as the original user on this session.
457 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
458 ber_int_t msgid, void *params )
460 struct ldapconn *lc = params;
462 return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val, LDAP_AUTH_SIMPLE );
465 /* Map API errors to protocol errors... */
468 ldap_back_map_result(int err)
472 case LDAP_SERVER_DOWN:
473 return LDAP_UNAVAILABLE;
474 case LDAP_LOCAL_ERROR:
476 case LDAP_ENCODING_ERROR:
477 case LDAP_DECODING_ERROR:
478 return LDAP_PROTOCOL_ERROR;
480 return LDAP_UNAVAILABLE;
481 case LDAP_AUTH_UNKNOWN:
482 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
483 case LDAP_FILTER_ERROR:
485 case LDAP_USER_CANCELLED:
487 case LDAP_PARAM_ERROR:
488 return LDAP_PROTOCOL_ERROR;
491 case LDAP_CONNECT_ERROR:
492 return LDAP_UNAVAILABLE;
493 case LDAP_NOT_SUPPORTED:
494 return LDAP_UNWILLING_TO_PERFORM;
495 case LDAP_CONTROL_NOT_FOUND:
496 return LDAP_PROTOCOL_ERROR;
497 case LDAP_NO_RESULTS_RETURNED:
498 return LDAP_NO_SUCH_OBJECT;
499 case LDAP_MORE_RESULTS_TO_RETURN:
501 case LDAP_CLIENT_LOOP:
502 case LDAP_REFERRAL_LIMIT_EXCEEDED:
503 return LDAP_LOOP_DETECT;
505 if LDAP_API_ERROR(err)
513 ldap_back_op_result(struct ldapconn *lc, Connection *conn, Operation *op)
515 int err = LDAP_SUCCESS;
519 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER, &err);
521 /* internal ops must not reply to client */
522 if ( !conn || op->o_do_not_cache ) goto quiet;
524 ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &msg);
525 ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
526 err = ldap_back_map_result(err);
528 #ifdef ENABLE_REWRITE
531 * FIXME: need rewrite info for match; mmmh ...
533 send_ldap_result( conn, op, err, match, msg, NULL, NULL );
534 /* better test the pointers before freeing? */
539 #else /* !ENABLE_REWRITE */
541 send_ldap_result( conn, op, err, match, msg, NULL, NULL );
542 /* better test the pointers before freeing? */
547 #endif /* !ENABLE_REWRITE */
549 if ( msg ) free( msg );
551 return( (err==LDAP_SUCCESS) ? 0 : -1 );