1 /* bind.c - ldap backend bind function */
4 * Copyright 1998-2000 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
64 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
67 struct berval mdn = { 0, NULL };
70 lc = ldap_back_getconn(li, conn, op);
76 * Rewrite the bind dn if needed
79 switch ( rewrite_session( li->rwinfo, "bindDn", dn->bv_val, conn, &mdn.bv_val ) ) {
80 case REWRITE_REGEXEC_OK:
81 if ( mdn.bv_val == NULL ) {
82 mdn.bv_val = ( char * )dn->bv_val;
85 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
86 "[rw] bindDn: \"%s\" -> \"%s\"\n", dn->bv_val, mdn.bv_val ));
87 #else /* !NEW_LOGGING */
88 Debug( LDAP_DEBUG_ARGS, "rw> bindDn: \"%s\" -> \"%s\"\n%s",
89 dn->bv_val, mdn.bv_val, "" );
90 #endif /* !NEW_LOGGING */
93 case REWRITE_REGEXEC_UNWILLING:
94 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
95 NULL, "Unwilling to perform", NULL, NULL );
98 case REWRITE_REGEXEC_ERR:
99 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
100 NULL, "Operations error", NULL, NULL );
103 #else /* !ENABLE_REWRITE */
104 ldap_back_dn_massage( li, dn, &mdn, 0, 1 );
105 #endif /* !ENABLE_REWRITE */
107 rc = ldap_bind_s(lc->ld, mdn.bv_val, cred->bv_val, method);
108 if (rc != LDAP_SUCCESS) {
109 rc = ldap_back_op_result( lc, op );
114 if ( mdn.bv_val != dn->bv_val ) {
124 * compares two struct ldapconn based on the value of the conn pointer;
133 struct ldapconn *lc1 = (struct ldapconn *)c1;
134 struct ldapconn *lc2 = (struct ldapconn *)c2;
136 return ( ( lc1->conn < lc2->conn ) ? -1 : ( ( lc1->conn > lc2-> conn ) ? 1 : 0 ) );
142 * returns -1 in case a duplicate struct ldapconn has been inserted;
151 struct ldapconn *lc1 = (struct ldapconn *)c1;
152 struct ldapconn *lc2 = (struct ldapconn *)c2;
154 return( ( lc1->conn == lc2->conn ) ? -1 : 0 );
157 #if PRINT_CONNTREE > 0
158 static void ravl_print( Avlnode *root, int depth )
165 ravl_print( root->avl_right, depth+1 );
167 for ( i = 0; i < depth; i++ )
170 printf( "c(%ld) %d\n", ((struct ldapconn *) root->avl_data)->conn->c_connid, root->avl_bf );
172 ravl_print( root->avl_left, depth+1 );
175 static void myprint( Avlnode *root )
177 printf( "********\n" );
180 printf( "\tNULL\n" );
183 ravl_print( root, 0 );
185 printf( "********\n" );
187 #endif /* PRINT_CONNTREE */
190 ldap_back_getconn(struct ldapinfo *li, Connection *conn, Operation *op)
192 struct ldapconn *lc, lc_curr;
195 /* Searches for a ldapconn in the avl tree */
197 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
198 lc = (struct ldapconn *)avl_find( li->conntree,
199 (caddr_t)&lc_curr, ldap_back_conn_cmp );
200 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
202 /* Looks like we didn't get a bind. Open a new session... */
204 int vers = conn->c_protocol;
205 int err = ldap_initialize(&ld, li->url);
207 if (err != LDAP_SUCCESS) {
208 err = ldap_back_map_result(err);
209 send_ldap_result( conn, op, err,
210 NULL, "ldap_init failed", NULL, NULL );
213 /* Set LDAP version. This will always succeed: If the client
214 * bound with a particular version, then so can we.
216 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &vers);
218 lc = (struct ldapconn *)ch_malloc(sizeof(struct ldapconn));
222 #ifdef ENABLE_REWRITE
224 * Sets a cookie for the rewrite session
226 ( void )rewrite_session_init( li->rwinfo, conn );
227 #endif /* ENABLE_REWRITE */
229 if ( lc->conn->c_cdn != NULL && lc->conn->c_cdn[0] != '\0' ) {
232 * Rewrite the bind dn if needed
234 #ifdef ENABLE_REWRITE
235 lc->bound_dn.bv_val = NULL;
236 lc->bound_dn.bv_len = 0;
237 switch ( rewrite_session( li->rwinfo, "bindDn",
238 lc->conn->c_cdn, conn,
239 &lc->bound_dn.bv_val ) ) {
240 case REWRITE_REGEXEC_OK:
241 if ( lc->bound_dn.bv_val == NULL ) {
242 lc->bound_dn.bv_val =
243 ch_strdup( lc->conn->c_cdn );
246 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
247 "[rw] bindDn: \"%s\" ->"
250 lc->bound_dn.bv_val ));
251 #else /* !NEW_LOGGING */
252 Debug( LDAP_DEBUG_ARGS,
253 "rw> bindDn: \"%s\" ->"
256 lc->bound_dn.bv_val, "" );
257 #endif /* !NEW_LOGGING */
260 case REWRITE_REGEXEC_UNWILLING:
261 send_ldap_result( conn, op,
262 LDAP_UNWILLING_TO_PERFORM,
263 NULL, "Unwilling to perform",
267 case REWRITE_REGEXEC_ERR:
268 send_ldap_result( conn, op,
269 LDAP_OPERATIONS_ERROR,
270 NULL, "Operations error",
274 #else /* !ENABLE_REWRITE */
276 ber_str2bv( lc->conn->c_cdn, 0, 0, &bv );
277 ldap_back_dn_massage( li, &bv, &lc->bound_dn, 0, 1 );
278 #endif /* !ENABLE_REWRITE */
280 lc->bound_dn.bv_val = NULL;
281 lc->bound_dn.bv_len = 0;
285 /* Inserts the newly created ldapconn in the avl tree */
286 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
287 err = avl_insert( &li->conntree, (caddr_t)lc,
288 ldap_back_conn_cmp, ldap_back_conn_dup );
290 #if PRINT_CONNTREE > 0
291 myprint( li->conntree );
292 #endif /* PRINT_CONNTREE */
294 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
297 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
298 "ldap_back_getconn: conn %ld inserted\n",
299 lc->conn->c_connid ));
300 #else /* !NEW_LOGGING */
301 Debug( LDAP_DEBUG_TRACE,
302 "=>ldap_back_getconn: conn %ld inserted\n%s%s",
303 lc->conn->c_connid, "", "" );
304 #endif /* !NEW_LOGGING */
306 /* Err could be -1 in case a duplicate ldapconn is inserted */
308 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
309 NULL, "internal server error", NULL, NULL );
310 /* better destroy the ldapconn struct? */
315 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
316 "ldap_back_getconn: conn %ld inserted\n",
317 lc->conn->c_connid ));
318 #else /* !NEW_LOGGING */
319 Debug( LDAP_DEBUG_TRACE,
320 "=>ldap_back_getconn: conn %ld fetched%s%s\n",
321 lc->conn->c_connid, "", "" );
322 #endif /* !NEW_LOGGING */
331 * Note: as the check for the value of lc->bound was already here, I removed
332 * it from all the callers, and I made the function return the flag, so
333 * it can be used to simplify the check.
336 ldap_back_dobind(struct ldapconn *lc, Operation *op)
342 if (ldap_bind_s(lc->ld, lc->bound_dn.bv_val, NULL, LDAP_AUTH_SIMPLE) !=
344 ldap_back_op_result(lc, op);
347 return( lc->bound = 1 );
350 /* Map API errors to protocol errors... */
353 ldap_back_map_result(int err)
357 case LDAP_SERVER_DOWN:
358 return LDAP_UNAVAILABLE;
359 case LDAP_LOCAL_ERROR:
360 return LDAP_OPERATIONS_ERROR;
361 case LDAP_ENCODING_ERROR:
362 case LDAP_DECODING_ERROR:
363 return LDAP_PROTOCOL_ERROR;
365 return LDAP_UNAVAILABLE;
366 case LDAP_AUTH_UNKNOWN:
367 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
368 case LDAP_FILTER_ERROR:
369 return LDAP_OPERATIONS_ERROR;
370 case LDAP_USER_CANCELLED:
371 return LDAP_OPERATIONS_ERROR;
372 case LDAP_PARAM_ERROR:
373 return LDAP_PROTOCOL_ERROR;
375 return LDAP_OPERATIONS_ERROR;
376 case LDAP_CONNECT_ERROR:
377 return LDAP_UNAVAILABLE;
378 case LDAP_NOT_SUPPORTED:
379 return LDAP_UNWILLING_TO_PERFORM;
380 case LDAP_CONTROL_NOT_FOUND:
381 return LDAP_PROTOCOL_ERROR;
382 case LDAP_NO_RESULTS_RETURNED:
383 return LDAP_NO_SUCH_OBJECT;
384 case LDAP_MORE_RESULTS_TO_RETURN:
386 case LDAP_CLIENT_LOOP:
387 case LDAP_REFERRAL_LIMIT_EXCEEDED:
388 return LDAP_LOOP_DETECT;
390 if LDAP_API_ERROR(err)
398 ldap_back_op_result(struct ldapconn *lc, Operation *op)
400 int err = LDAP_SUCCESS;
404 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER, &err);
405 ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &msg);
406 ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
407 err = ldap_back_map_result(err);
409 #ifdef ENABLE_REWRITE
412 * need rewrite info; mmmh ...
415 #else /* !ENABLE_REWRITE */
417 send_ldap_result( lc->conn, op, err, match, msg, NULL, NULL );
418 /* better test the pointers before freeing? */
422 #endif /* !ENABLE_REWRITE */
424 if ( msg ) free( msg );
425 return( (err==LDAP_SUCCESS) ? 0 : -1 );