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
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.bv_len != 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.bv_val, conn,
239 &lc->bound_dn.bv_val ) ) {
240 case REWRITE_REGEXEC_OK:
241 if ( lc->bound_dn.bv_val == NULL ) {
242 ber_dupbv( &lc->bound_dn, &lc->conn->c_cdn );
245 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
246 "[rw] bindDn: \"%s\" ->"
248 lc->conn->c_cdn.bv_val,
249 lc->bound_dn.bv_val ));
250 #else /* !NEW_LOGGING */
251 Debug( LDAP_DEBUG_ARGS,
252 "rw> bindDn: \"%s\" ->"
254 lc->conn->c_cdn.bv_val,
255 lc->bound_dn.bv_val, "" );
256 #endif /* !NEW_LOGGING */
259 case REWRITE_REGEXEC_UNWILLING:
260 send_ldap_result( conn, op,
261 LDAP_UNWILLING_TO_PERFORM,
262 NULL, "Unwilling to perform",
266 case REWRITE_REGEXEC_ERR:
267 send_ldap_result( conn, op,
268 LDAP_OPERATIONS_ERROR,
269 NULL, "Operations error",
273 #else /* !ENABLE_REWRITE */
275 ldap_back_dn_massage( li, &lc->conn->c_cdn, &bv, 0, 1 );
276 if ( bv.bv_val == lc->conn->c_cdn.bv_val )
277 ber_dupbv( &lc->bound_dn, &bv );
280 #endif /* !ENABLE_REWRITE */
282 lc->bound_dn.bv_val = NULL;
283 lc->bound_dn.bv_len = 0;
287 /* Inserts the newly created ldapconn in the avl tree */
288 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
289 err = avl_insert( &li->conntree, (caddr_t)lc,
290 ldap_back_conn_cmp, ldap_back_conn_dup );
292 #if PRINT_CONNTREE > 0
293 myprint( li->conntree );
294 #endif /* PRINT_CONNTREE */
296 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
299 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
300 "ldap_back_getconn: conn %ld inserted\n",
301 lc->conn->c_connid ));
302 #else /* !NEW_LOGGING */
303 Debug( LDAP_DEBUG_TRACE,
304 "=>ldap_back_getconn: conn %ld inserted\n%s%s",
305 lc->conn->c_connid, "", "" );
306 #endif /* !NEW_LOGGING */
308 /* Err could be -1 in case a duplicate ldapconn is inserted */
310 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
311 NULL, "internal server error", NULL, NULL );
312 /* better destroy the ldapconn struct? */
317 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
318 "ldap_back_getconn: conn %ld inserted\n",
319 lc->conn->c_connid ));
320 #else /* !NEW_LOGGING */
321 Debug( LDAP_DEBUG_TRACE,
322 "=>ldap_back_getconn: conn %ld fetched%s%s\n",
323 lc->conn->c_connid, "", "" );
324 #endif /* !NEW_LOGGING */
333 * Note: as the check for the value of lc->bound was already here, I removed
334 * it from all the callers, and I made the function return the flag, so
335 * it can be used to simplify the check.
338 ldap_back_dobind(struct ldapconn *lc, Operation *op)
344 if (ldap_bind_s(lc->ld, lc->bound_dn.bv_val, NULL, LDAP_AUTH_SIMPLE) !=
346 ldap_back_op_result(lc, op);
349 return( lc->bound = 1 );
352 /* Map API errors to protocol errors... */
355 ldap_back_map_result(int err)
359 case LDAP_SERVER_DOWN:
360 return LDAP_UNAVAILABLE;
361 case LDAP_LOCAL_ERROR:
362 return LDAP_OPERATIONS_ERROR;
363 case LDAP_ENCODING_ERROR:
364 case LDAP_DECODING_ERROR:
365 return LDAP_PROTOCOL_ERROR;
367 return LDAP_UNAVAILABLE;
368 case LDAP_AUTH_UNKNOWN:
369 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
370 case LDAP_FILTER_ERROR:
371 return LDAP_OPERATIONS_ERROR;
372 case LDAP_USER_CANCELLED:
373 return LDAP_OPERATIONS_ERROR;
374 case LDAP_PARAM_ERROR:
375 return LDAP_PROTOCOL_ERROR;
377 return LDAP_OPERATIONS_ERROR;
378 case LDAP_CONNECT_ERROR:
379 return LDAP_UNAVAILABLE;
380 case LDAP_NOT_SUPPORTED:
381 return LDAP_UNWILLING_TO_PERFORM;
382 case LDAP_CONTROL_NOT_FOUND:
383 return LDAP_PROTOCOL_ERROR;
384 case LDAP_NO_RESULTS_RETURNED:
385 return LDAP_NO_SUCH_OBJECT;
386 case LDAP_MORE_RESULTS_TO_RETURN:
388 case LDAP_CLIENT_LOOP:
389 case LDAP_REFERRAL_LIMIT_EXCEEDED:
390 return LDAP_LOOP_DETECT;
392 if LDAP_API_ERROR(err)
400 ldap_back_op_result(struct ldapconn *lc, Operation *op)
402 int err = LDAP_SUCCESS;
406 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER, &err);
407 ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &msg);
408 ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
409 err = ldap_back_map_result(err);
411 #ifdef ENABLE_REWRITE
414 * need rewrite info; mmmh ...
417 #else /* !ENABLE_REWRITE */
419 send_ldap_result( lc->conn, op, err, match, msg, NULL, NULL );
420 /* better test the pointers before freeing? */
424 #endif /* !ENABLE_REWRITE */
426 if ( msg ) free( msg );
427 return( (err==LDAP_SUCCESS) ? 0 : -1 );