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;
72 lc = ldap_back_getconn(li, conn, op);
78 * Rewrite the bind dn if needed
81 switch ( rewrite_session( li->rwinfo, "bindDn", dn, conn, &mdn ) ) {
82 case REWRITE_REGEXEC_OK:
87 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
88 "[rw] bindDn: \"%s\" -> \"%s\"\n", dn, mdn ));
89 #else /* !NEW_LOGGING */
90 Debug( LDAP_DEBUG_ARGS, "rw> bindDn: \"%s\" -> \"%s\"\n%s",
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_OPERATIONS_ERROR,
102 NULL, "Operations error", NULL, NULL );
105 #else /* !ENABLE_REWRITE */
106 mdn = ldap_back_dn_massage( li, ch_strdup( dn ), 0 );
107 #endif /* !ENABLE_REWRITE */
109 rc = ldap_bind_s(lc->ld, mdn, cred->bv_val, method);
110 if (rc != LDAP_SUCCESS) {
111 rc = ldap_back_op_result( lc, op );
116 #ifdef ENABLE_REWRITE
118 #endif /* ENABLE_REWRITE */
120 #ifdef ENABLE_REWRITE
122 #endif /* ENABLE_REWRITE */
130 * compares two struct ldapconn based on the value of the conn pointer;
139 struct ldapconn *lc1 = (struct ldapconn *)c1;
140 struct ldapconn *lc2 = (struct ldapconn *)c2;
142 return ( ( lc1->conn < lc2->conn ) ? -1 : ( ( lc1->conn > lc2-> conn ) ? 1 : 0 ) );
148 * returns -1 in case a duplicate struct ldapconn has been inserted;
157 struct ldapconn *lc1 = (struct ldapconn *)c1;
158 struct ldapconn *lc2 = (struct ldapconn *)c2;
160 return( ( lc1->conn == lc2->conn ) ? -1 : 0 );
163 #if PRINT_CONNTREE > 0
164 static void ravl_print( Avlnode *root, int depth )
171 ravl_print( root->avl_right, depth+1 );
173 for ( i = 0; i < depth; i++ )
176 printf( "c(%ld) %d\n", ((struct ldapconn *) root->avl_data)->conn->c_connid, root->avl_bf );
178 ravl_print( root->avl_left, depth+1 );
181 static void myprint( Avlnode *root )
183 printf( "********\n" );
186 printf( "\tNULL\n" );
189 ravl_print( root, 0 );
191 printf( "********\n" );
193 #endif /* PRINT_CONNTREE */
196 ldap_back_getconn(struct ldapinfo *li, Connection *conn, Operation *op)
198 struct ldapconn *lc, lc_curr;
201 /* Searches for a ldapconn in the avl tree */
203 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
204 lc = (struct ldapconn *)avl_find( li->conntree,
205 (caddr_t)&lc_curr, ldap_back_conn_cmp );
206 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
208 /* Looks like we didn't get a bind. Open a new session... */
210 int vers = conn->c_protocol;
211 int err = ldap_initialize(&ld, li->url);
213 if (err != LDAP_SUCCESS) {
214 err = ldap_back_map_result(err);
215 send_ldap_result( conn, op, err,
216 NULL, "ldap_init failed", NULL, NULL );
219 /* Set LDAP version. This will always succeed: If the client
220 * bound with a particular version, then so can we.
222 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &vers);
224 lc = (struct ldapconn *)ch_malloc(sizeof(struct ldapconn));
228 #ifdef ENABLE_REWRITE
230 * Sets a cookie for the rewrite session
232 ( void )rewrite_session_init( li->rwinfo, conn );
233 #endif /* ENABLE_REWRITE */
235 if ( lc->conn->c_cdn != NULL && lc->conn->c_cdn[0] != '\0' ) {
238 * Rewrite the bind dn if needed
240 #ifdef ENABLE_REWRITE
242 switch ( rewrite_session( li->rwinfo, "bindDn",
243 lc->conn->c_cdn, conn,
245 case REWRITE_REGEXEC_OK:
246 if ( lc->bound_dn == NULL ) {
248 ch_strdup( lc->conn->c_cdn );
251 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
252 "[rw] bindDn: \"%s\" ->"
256 #else /* !NEW_LOGGING */
257 Debug( LDAP_DEBUG_ARGS,
258 "rw> bindDn: \"%s\" ->"
262 #endif /* !NEW_LOGGING */
265 case REWRITE_REGEXEC_UNWILLING:
266 send_ldap_result( conn, op,
267 LDAP_UNWILLING_TO_PERFORM,
268 NULL, "Unwilling to perform",
272 case REWRITE_REGEXEC_ERR:
273 send_ldap_result( conn, op,
274 LDAP_OPERATIONS_ERROR,
275 NULL, "Operations error",
279 #else /* !ENABLE_REWRITE */
280 lc->bound_dn = ldap_back_dn_massage( li,
281 ch_strdup( lc->conn->c_cdn ), 0 );
282 #endif /* !ENABLE_REWRITE */
288 /* Inserts the newly created ldapconn in the avl tree */
289 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
290 err = avl_insert( &li->conntree, (caddr_t)lc,
291 ldap_back_conn_cmp, ldap_back_conn_dup );
293 #if PRINT_CONNTREE > 0
294 myprint( li->conntree );
295 #endif /* PRINT_CONNTREE */
297 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
300 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
301 "ldap_back_getconn: conn %ld inserted\n",
302 lc->conn->c_connid ));
303 #else /* !NEW_LOGGING */
304 Debug( LDAP_DEBUG_TRACE,
305 "=>ldap_back_getconn: conn %ld inserted\n%s%s",
306 lc->conn->c_connid, "", "" );
307 #endif /* !NEW_LOGGING */
309 /* Err could be -1 in case a duplicate ldapconn is inserted */
311 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
312 NULL, "internal server error", NULL, NULL );
313 /* better destroy the ldapconn struct? */
318 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
319 "ldap_back_getconn: conn %ld inserted\n",
320 lc->conn->c_connid ));
321 #else /* !NEW_LOGGING */
322 Debug( LDAP_DEBUG_TRACE,
323 "=>ldap_back_getconn: conn %ld fetched%s%s\n",
324 lc->conn->c_connid, "", "" );
325 #endif /* !NEW_LOGGING */
334 * Note: as the check for the value of lc->bound was already here, I removed
335 * it from all the callers, and I made the function return the flag, so
336 * it can be used to simplify the check.
339 ldap_back_dobind(struct ldapconn *lc, Operation *op)
345 if (ldap_bind_s(lc->ld, lc->bound_dn, NULL, LDAP_AUTH_SIMPLE) !=
347 ldap_back_op_result(lc, op);
350 return( lc->bound = 1 );
353 /* Map API errors to protocol errors... */
356 ldap_back_map_result(int err)
360 case LDAP_SERVER_DOWN:
361 return LDAP_UNAVAILABLE;
362 case LDAP_LOCAL_ERROR:
363 return LDAP_OPERATIONS_ERROR;
364 case LDAP_ENCODING_ERROR:
365 case LDAP_DECODING_ERROR:
366 return LDAP_PROTOCOL_ERROR;
368 return LDAP_UNAVAILABLE;
369 case LDAP_AUTH_UNKNOWN:
370 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
371 case LDAP_FILTER_ERROR:
372 return LDAP_OPERATIONS_ERROR;
373 case LDAP_USER_CANCELLED:
374 return LDAP_OPERATIONS_ERROR;
375 case LDAP_PARAM_ERROR:
376 return LDAP_PROTOCOL_ERROR;
378 return LDAP_OPERATIONS_ERROR;
379 case LDAP_CONNECT_ERROR:
380 return LDAP_UNAVAILABLE;
381 case LDAP_NOT_SUPPORTED:
382 return LDAP_UNWILLING_TO_PERFORM;
383 case LDAP_CONTROL_NOT_FOUND:
384 return LDAP_PROTOCOL_ERROR;
385 case LDAP_NO_RESULTS_RETURNED:
386 return LDAP_NO_SUCH_OBJECT;
387 case LDAP_MORE_RESULTS_TO_RETURN:
389 case LDAP_CLIENT_LOOP:
390 case LDAP_REFERRAL_LIMIT_EXCEEDED:
391 return LDAP_LOOP_DETECT;
393 if LDAP_API_ERROR(err)
401 ldap_back_op_result(struct ldapconn *lc, Operation *op)
403 int err = LDAP_SUCCESS;
407 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER, &err);
408 ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &msg);
409 ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
410 err = ldap_back_map_result(err);
412 #ifdef ENABLE_REWRITE
415 * need rewrite info; mmmh ...
418 #else /* !ENABLE_REWRITE */
420 send_ldap_result( lc->conn, op, err, match, msg, NULL, NULL );
421 /* better test the pointers before freeing? */
425 #endif /* !ENABLE_REWRITE */
427 if ( msg ) free( msg );
428 return( (err==LDAP_SUCCESS) ? 0 : -1 );