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;
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 ) ) {
80 case REWRITE_REGEXEC_OK:
82 mdn = ( char * )dn->bv_val;
85 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
86 "[rw] bindDn: \"%s\" -> \"%s\"\n", dn->bv_val, mdn ));
87 #else /* !NEW_LOGGING */
88 Debug( LDAP_DEBUG_ARGS, "rw> bindDn: \"%s\" -> \"%s\"\n%s",
89 dn->bv_val, mdn, "" );
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 mdn = ldap_back_dn_massage( li, ch_strdup( dn->bv_val ), 0 );
105 #endif /* !ENABLE_REWRITE */
107 rc = ldap_bind_s(lc->ld, mdn, cred->bv_val, method);
108 if (rc != LDAP_SUCCESS) {
109 rc = ldap_back_op_result( lc, op );
114 #ifdef ENABLE_REWRITE
115 if ( mdn != dn->bv_val ) {
116 #endif /* ENABLE_REWRITE */
118 #ifdef ENABLE_REWRITE
120 #endif /* ENABLE_REWRITE */
128 * compares two struct ldapconn based on the value of the conn pointer;
137 struct ldapconn *lc1 = (struct ldapconn *)c1;
138 struct ldapconn *lc2 = (struct ldapconn *)c2;
140 return ( ( lc1->conn < lc2->conn ) ? -1 : ( ( lc1->conn > lc2-> conn ) ? 1 : 0 ) );
146 * returns -1 in case a duplicate struct ldapconn has been inserted;
155 struct ldapconn *lc1 = (struct ldapconn *)c1;
156 struct ldapconn *lc2 = (struct ldapconn *)c2;
158 return( ( lc1->conn == lc2->conn ) ? -1 : 0 );
161 #if PRINT_CONNTREE > 0
162 static void ravl_print( Avlnode *root, int depth )
169 ravl_print( root->avl_right, depth+1 );
171 for ( i = 0; i < depth; i++ )
174 printf( "c(%ld) %d\n", ((struct ldapconn *) root->avl_data)->conn->c_connid, root->avl_bf );
176 ravl_print( root->avl_left, depth+1 );
179 static void myprint( Avlnode *root )
181 printf( "********\n" );
184 printf( "\tNULL\n" );
187 ravl_print( root, 0 );
189 printf( "********\n" );
191 #endif /* PRINT_CONNTREE */
194 ldap_back_getconn(struct ldapinfo *li, Connection *conn, Operation *op)
196 struct ldapconn *lc, lc_curr;
199 /* Searches for a ldapconn in the avl tree */
201 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
202 lc = (struct ldapconn *)avl_find( li->conntree,
203 (caddr_t)&lc_curr, ldap_back_conn_cmp );
204 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
206 /* Looks like we didn't get a bind. Open a new session... */
208 int vers = conn->c_protocol;
209 int err = ldap_initialize(&ld, li->url);
211 if (err != LDAP_SUCCESS) {
212 err = ldap_back_map_result(err);
213 send_ldap_result( conn, op, err,
214 NULL, "ldap_init failed", NULL, NULL );
217 /* Set LDAP version. This will always succeed: If the client
218 * bound with a particular version, then so can we.
220 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &vers);
222 lc = (struct ldapconn *)ch_malloc(sizeof(struct ldapconn));
226 #ifdef ENABLE_REWRITE
228 * Sets a cookie for the rewrite session
230 ( void )rewrite_session_init( li->rwinfo, conn );
231 #endif /* ENABLE_REWRITE */
233 if ( lc->conn->c_cdn != NULL && lc->conn->c_cdn[0] != '\0' ) {
236 * Rewrite the bind dn if needed
238 #ifdef ENABLE_REWRITE
240 switch ( rewrite_session( li->rwinfo, "bindDn",
241 lc->conn->c_cdn, conn,
243 case REWRITE_REGEXEC_OK:
244 if ( lc->bound_dn == NULL ) {
246 ch_strdup( lc->conn->c_cdn );
249 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
250 "[rw] bindDn: \"%s\" ->"
254 #else /* !NEW_LOGGING */
255 Debug( LDAP_DEBUG_ARGS,
256 "rw> bindDn: \"%s\" ->"
260 #endif /* !NEW_LOGGING */
263 case REWRITE_REGEXEC_UNWILLING:
264 send_ldap_result( conn, op,
265 LDAP_UNWILLING_TO_PERFORM,
266 NULL, "Unwilling to perform",
270 case REWRITE_REGEXEC_ERR:
271 send_ldap_result( conn, op,
272 LDAP_OPERATIONS_ERROR,
273 NULL, "Operations error",
277 #else /* !ENABLE_REWRITE */
278 lc->bound_dn = ldap_back_dn_massage( li,
279 ch_strdup( lc->conn->c_cdn ), 0 );
280 #endif /* !ENABLE_REWRITE */
286 /* Inserts the newly created ldapconn in the avl tree */
287 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
288 err = avl_insert( &li->conntree, (caddr_t)lc,
289 ldap_back_conn_cmp, ldap_back_conn_dup );
291 #if PRINT_CONNTREE > 0
292 myprint( li->conntree );
293 #endif /* PRINT_CONNTREE */
295 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
298 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
299 "ldap_back_getconn: conn %ld inserted\n",
300 lc->conn->c_connid ));
301 #else /* !NEW_LOGGING */
302 Debug( LDAP_DEBUG_TRACE,
303 "=>ldap_back_getconn: conn %ld inserted\n%s%s",
304 lc->conn->c_connid, "", "" );
305 #endif /* !NEW_LOGGING */
307 /* Err could be -1 in case a duplicate ldapconn is inserted */
309 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
310 NULL, "internal server error", NULL, NULL );
311 /* better destroy the ldapconn struct? */
316 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
317 "ldap_back_getconn: conn %ld inserted\n",
318 lc->conn->c_connid ));
319 #else /* !NEW_LOGGING */
320 Debug( LDAP_DEBUG_TRACE,
321 "=>ldap_back_getconn: conn %ld fetched%s%s\n",
322 lc->conn->c_connid, "", "" );
323 #endif /* !NEW_LOGGING */
332 * Note: as the check for the value of lc->bound was already here, I removed
333 * it from all the callers, and I made the function return the flag, so
334 * it can be used to simplify the check.
337 ldap_back_dobind(struct ldapconn *lc, Operation *op)
343 if (ldap_bind_s(lc->ld, lc->bound_dn, NULL, LDAP_AUTH_SIMPLE) !=
345 ldap_back_op_result(lc, op);
348 return( lc->bound = 1 );
351 /* Map API errors to protocol errors... */
354 ldap_back_map_result(int err)
358 case LDAP_SERVER_DOWN:
359 return LDAP_UNAVAILABLE;
360 case LDAP_LOCAL_ERROR:
361 return LDAP_OPERATIONS_ERROR;
362 case LDAP_ENCODING_ERROR:
363 case LDAP_DECODING_ERROR:
364 return LDAP_PROTOCOL_ERROR;
366 return LDAP_UNAVAILABLE;
367 case LDAP_AUTH_UNKNOWN:
368 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
369 case LDAP_FILTER_ERROR:
370 return LDAP_OPERATIONS_ERROR;
371 case LDAP_USER_CANCELLED:
372 return LDAP_OPERATIONS_ERROR;
373 case LDAP_PARAM_ERROR:
374 return LDAP_PROTOCOL_ERROR;
376 return LDAP_OPERATIONS_ERROR;
377 case LDAP_CONNECT_ERROR:
378 return LDAP_UNAVAILABLE;
379 case LDAP_NOT_SUPPORTED:
380 return LDAP_UNWILLING_TO_PERFORM;
381 case LDAP_CONTROL_NOT_FOUND:
382 return LDAP_PROTOCOL_ERROR;
383 case LDAP_NO_RESULTS_RETURNED:
384 return LDAP_NO_SUCH_OBJECT;
385 case LDAP_MORE_RESULTS_TO_RETURN:
387 case LDAP_CLIENT_LOOP:
388 case LDAP_REFERRAL_LIMIT_EXCEEDED:
389 return LDAP_LOOP_DETECT;
391 if LDAP_API_ERROR(err)
399 ldap_back_op_result(struct ldapconn *lc, Operation *op)
401 int err = LDAP_SUCCESS;
405 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER, &err);
406 ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &msg);
407 ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
408 err = ldap_back_map_result(err);
410 #ifdef ENABLE_REWRITE
413 * need rewrite info; mmmh ...
416 #else /* !ENABLE_REWRITE */
418 send_ldap_result( lc->conn, op, err, match, msg, NULL, NULL );
419 /* better test the pointers before freeing? */
423 #endif /* !ENABLE_REWRITE */
425 if ( msg ) free( msg );
426 return( (err==LDAP_SUCCESS) ? 0 : -1 );