1 /* bind.c - ldap backend bind function */
4 * Copyright 1998-2002 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 * Rewrite the bind dn if needed
81 switch ( rewrite_session( li->rwinfo, "bindDn", dn->bv_val, conn, &mdn.bv_val ) ) {
82 case REWRITE_REGEXEC_OK:
83 if ( mdn.bv_val == NULL ) {
84 mdn.bv_val = ( char * )dn->bv_val;
87 LDAP_LOG( BACK_LDAP, DETAIL1,
88 "[rw] bindDn: \"%s\" -> \"%s\"\n", dn->bv_val, mdn.bv_val, 0 );
89 #else /* !NEW_LOGGING */
90 Debug( LDAP_DEBUG_ARGS, "rw> bindDn: \"%s\" -> \"%s\"\n%s",
91 dn->bv_val, mdn.bv_val, "" );
92 #endif /* !NEW_LOGGING */
95 case REWRITE_REGEXEC_UNWILLING:
96 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
97 NULL, "Unwilling to perform", NULL, NULL );
100 case REWRITE_REGEXEC_ERR:
101 send_ldap_result( conn, op, LDAP_OTHER,
102 NULL, "Operations error", NULL, NULL );
105 #else /* !ENABLE_REWRITE */
106 ldap_back_dn_massage( li, dn, &mdn, 0, 1 );
107 #endif /* !ENABLE_REWRITE */
109 rc = ldap_bind_s(lc->ld, mdn.bv_val, cred->bv_val, method);
110 if (rc != LDAP_SUCCESS) {
111 rc = ldap_back_op_result( lc, op );
116 if ( li->savecred ) {
117 if ( lc->cred.bv_val )
118 ch_free( lc->cred.bv_val );
119 ber_dupbv( &lc->cred, cred );
120 ldap_set_rebind_proc( lc->ld, ldap_back_rebind, lc );
123 if ( lc->bound_dn.bv_val )
124 ch_free( lc->bound_dn.bv_val );
125 if ( mdn.bv_val != dn->bv_val ) {
128 ber_dupbv( &lc->bound_dn, dn );
137 * compares two struct ldapconn based on the value of the conn pointer;
146 const struct ldapconn *lc1 = (const struct ldapconn *)c1;
147 const struct ldapconn *lc2 = (const struct ldapconn *)c2;
149 return ( ( lc1->conn < lc2->conn ) ? -1 : ( ( lc1->conn > lc2-> conn ) ? 1 : 0 ) );
155 * returns -1 in case a duplicate struct ldapconn has been inserted;
164 struct ldapconn *lc1 = (struct ldapconn *)c1;
165 struct ldapconn *lc2 = (struct ldapconn *)c2;
167 return( ( lc1->conn == lc2->conn ) ? -1 : 0 );
170 #if PRINT_CONNTREE > 0
171 static void ravl_print( Avlnode *root, int depth )
178 ravl_print( root->avl_right, depth+1 );
180 for ( i = 0; i < depth; i++ )
183 printf( "c(%ld) %d\n", ((struct ldapconn *) root->avl_data)->conn->c_connid, root->avl_bf );
185 ravl_print( root->avl_left, depth+1 );
188 static void myprint( Avlnode *root )
190 printf( "********\n" );
193 printf( "\tNULL\n" );
196 ravl_print( root, 0 );
198 printf( "********\n" );
200 #endif /* PRINT_CONNTREE */
203 ldap_back_getconn(struct ldapinfo *li, Connection *conn, Operation *op)
205 struct ldapconn *lc, lc_curr;
208 /* Searches for a ldapconn in the avl tree */
210 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
211 lc = (struct ldapconn *)avl_find( li->conntree,
212 (caddr_t)&lc_curr, ldap_back_conn_cmp );
213 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
215 /* Looks like we didn't get a bind. Open a new session... */
217 int vers = conn->c_protocol;
218 int err = ldap_initialize(&ld, li->url);
220 if (err != LDAP_SUCCESS) {
221 err = ldap_back_map_result(err);
222 send_ldap_result( conn, op, err,
223 NULL, "ldap_init failed", NULL, NULL );
226 /* Set LDAP version. This will always succeed: If the client
227 * bound with a particular version, then so can we.
229 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &vers);
231 lc = (struct ldapconn *)ch_malloc(sizeof(struct ldapconn));
236 lc->cred.bv_val = NULL;
238 #ifdef ENABLE_REWRITE
240 * Sets a cookie for the rewrite session
242 ( void )rewrite_session_init( li->rwinfo, conn );
243 #endif /* ENABLE_REWRITE */
245 if ( lc->conn->c_dn.bv_len != 0 ) {
248 * Rewrite the bind dn if needed
250 #ifdef ENABLE_REWRITE
251 lc->bound_dn.bv_val = NULL;
252 lc->bound_dn.bv_len = 0;
253 switch ( rewrite_session( li->rwinfo, "bindDn",
254 lc->conn->c_dn.bv_val, conn,
255 &lc->bound_dn.bv_val ) ) {
256 case REWRITE_REGEXEC_OK:
257 if ( lc->bound_dn.bv_val == NULL ) {
258 ber_dupbv( &lc->bound_dn, &lc->conn->c_dn );
261 LDAP_LOG( BACK_LDAP, DETAIL1,
262 "[rw] bindDn: \"%s\" ->"
264 lc->conn->c_dn.bv_val,
265 lc->bound_dn.bv_val, "" );
266 #else /* !NEW_LOGGING */
267 Debug( LDAP_DEBUG_ARGS,
268 "rw> bindDn: \"%s\" ->"
270 lc->conn->c_dn.bv_val,
271 lc->bound_dn.bv_val, "" );
272 #endif /* !NEW_LOGGING */
275 case REWRITE_REGEXEC_UNWILLING:
276 send_ldap_result( conn, op,
277 LDAP_UNWILLING_TO_PERFORM,
278 NULL, "Unwilling to perform",
282 case REWRITE_REGEXEC_ERR:
283 send_ldap_result( conn, op,
285 NULL, "Operations error",
289 #else /* !ENABLE_REWRITE */
291 ldap_back_dn_massage( li, &lc->conn->c_dn, &bv, 0, 1 );
292 if ( bv.bv_val == lc->conn->c_dn.bv_val )
293 ber_dupbv( &lc->bound_dn, &bv );
296 #endif /* !ENABLE_REWRITE */
298 lc->bound_dn.bv_val = NULL;
299 lc->bound_dn.bv_len = 0;
303 /* Inserts the newly created ldapconn in the avl tree */
304 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
305 err = avl_insert( &li->conntree, (caddr_t)lc,
306 ldap_back_conn_cmp, ldap_back_conn_dup );
308 #if PRINT_CONNTREE > 0
309 myprint( li->conntree );
310 #endif /* PRINT_CONNTREE */
312 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
315 LDAP_LOG( BACK_LDAP, INFO,
316 "ldap_back_getconn: conn %ld inserted\n", lc->conn->c_connid, 0, 0);
317 #else /* !NEW_LOGGING */
318 Debug( LDAP_DEBUG_TRACE,
319 "=>ldap_back_getconn: conn %ld inserted\n%s%s",
320 lc->conn->c_connid, "", "" );
321 #endif /* !NEW_LOGGING */
323 /* Err could be -1 in case a duplicate ldapconn is inserted */
325 send_ldap_result( conn, op, LDAP_OTHER,
326 NULL, "internal server error", NULL, NULL );
327 /* better destroy the ldapconn struct? */
332 LDAP_LOG( BACK_LDAP, INFO,
333 "ldap_back_getconn: conn %ld inserted\n",
334 lc->conn->c_connid, 0, 0 );
335 #else /* !NEW_LOGGING */
336 Debug( LDAP_DEBUG_TRACE,
337 "=>ldap_back_getconn: conn %ld fetched%s%s\n",
338 lc->conn->c_connid, "", "" );
339 #endif /* !NEW_LOGGING */
348 * Note: as the check for the value of lc->bound was already here, I removed
349 * it from all the callers, and I made the function return the flag, so
350 * it can be used to simplify the check.
353 ldap_back_dobind(struct ldapconn *lc, Operation *op)
359 if (ldap_bind_s(lc->ld, lc->bound_dn.bv_val, lc->cred.bv_val, LDAP_AUTH_SIMPLE) !=
361 ldap_back_op_result(lc, op);
364 return( lc->bound = 1 );
370 * This is a callback used for chasing referrals using the same
371 * credentials as the original user on this session.
374 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
375 ber_int_t msgid, void *params )
377 struct ldapconn *lc = params;
379 return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val, LDAP_AUTH_SIMPLE );
382 /* Map API errors to protocol errors... */
385 ldap_back_map_result(int err)
389 case LDAP_SERVER_DOWN:
390 return LDAP_UNAVAILABLE;
391 case LDAP_LOCAL_ERROR:
393 case LDAP_ENCODING_ERROR:
394 case LDAP_DECODING_ERROR:
395 return LDAP_PROTOCOL_ERROR;
397 return LDAP_UNAVAILABLE;
398 case LDAP_AUTH_UNKNOWN:
399 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
400 case LDAP_FILTER_ERROR:
402 case LDAP_USER_CANCELLED:
404 case LDAP_PARAM_ERROR:
405 return LDAP_PROTOCOL_ERROR;
408 case LDAP_CONNECT_ERROR:
409 return LDAP_UNAVAILABLE;
410 case LDAP_NOT_SUPPORTED:
411 return LDAP_UNWILLING_TO_PERFORM;
412 case LDAP_CONTROL_NOT_FOUND:
413 return LDAP_PROTOCOL_ERROR;
414 case LDAP_NO_RESULTS_RETURNED:
415 return LDAP_NO_SUCH_OBJECT;
416 case LDAP_MORE_RESULTS_TO_RETURN:
418 case LDAP_CLIENT_LOOP:
419 case LDAP_REFERRAL_LIMIT_EXCEEDED:
420 return LDAP_LOOP_DETECT;
422 if LDAP_API_ERROR(err)
430 ldap_back_op_result(struct ldapconn *lc, Operation *op)
432 int err = LDAP_SUCCESS;
436 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER, &err);
437 ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &msg);
438 ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
439 err = ldap_back_map_result(err);
441 #ifdef ENABLE_REWRITE
444 * FIXME: need rewrite info for match; mmmh ...
446 send_ldap_result( lc->conn, op, err, match, msg, NULL, NULL );
447 /* better test the pointers before freeing? */
452 #else /* !ENABLE_REWRITE */
454 send_ldap_result( lc->conn, op, err, match, msg, NULL, NULL );
455 /* better test the pointers before freeing? */
460 #endif /* !ENABLE_REWRITE */
462 if ( msg ) free( msg );
463 return( (err==LDAP_SUCCESS) ? 0 : -1 );