]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/bind.c
Mostly based on patches provided by Hallvard B. Furuseth
[openldap] / servers / slapd / back-ldap / bind.c
1 /* bind.c - ldap backend bind function */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7 /* This is an altered version */
8 /*
9  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
10  * 
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:
14  * 
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.
17  * 
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.
21  * 
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.
25  * 
26  * 4. This notice may not be removed or altered.
27  *
28  *
29  *
30  * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
31  * 
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.
36  */
37
38 #include "portable.h"
39
40 #include <stdio.h>
41
42 #include <ac/socket.h>
43 #include <ac/string.h>
44
45
46 #define AVL_INTERNAL
47 #include "slap.h"
48 #include "back-ldap.h"
49
50 #define PRINT_CONNTREE 0
51
52 int
53 ldap_back_bind(
54     Backend             *be,
55     Connection          *conn,
56     Operation           *op,
57     struct berval       *dn,
58     struct berval       *ndn,
59     int                 method,
60     struct berval       *cred,
61     struct berval       *edn
62 )
63 {
64         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
65         struct ldapconn *lc;
66
67         struct berval mdn = { 0, NULL };
68         int rc = 0;
69
70         lc = ldap_back_getconn(li, conn, op);
71         if ( !lc ) {
72                 return( -1 );
73         }
74
75         /*
76          * Rewrite the bind dn if needed
77          */
78 #ifdef ENABLE_REWRITE
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;
83                 }
84 #ifdef NEW_LOGGING
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 */
91                 break;
92                 
93         case REWRITE_REGEXEC_UNWILLING:
94                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
95                                 NULL, "Unwilling to perform", NULL, NULL );
96                 return( -1 );
97
98         case REWRITE_REGEXEC_ERR:
99                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
100                                 NULL, "Operations error", NULL, NULL );
101                 return( -1 );
102         }
103 #else /* !ENABLE_REWRITE */
104         ldap_back_dn_massage( li, dn, &mdn, 0, 1 );
105 #endif /* !ENABLE_REWRITE */
106
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 );
110         } else {
111                 lc->bound = 1;
112         }
113
114         if ( mdn.bv_val != dn->bv_val ) {
115                 free( mdn.bv_val );
116         }
117         
118         return( rc );
119 }
120
121 /*
122  * ldap_back_conn_cmp
123  *
124  * compares two struct ldapconn based on the value of the conn pointer;
125  * used by avl stuff
126  */
127 int
128 ldap_back_conn_cmp(
129         const void *c1,
130         const void *c2
131         )
132 {
133         const struct ldapconn *lc1 = (const struct ldapconn *)c1;
134         const struct ldapconn *lc2 = (const struct ldapconn *)c2;
135         
136         return ( ( lc1->conn < lc2->conn ) ? -1 : ( ( lc1->conn > lc2-> conn ) ? 1 : 0 ) );
137 }
138
139 /*
140  * ldap_back_conn_dup
141  *
142  * returns -1 in case a duplicate struct ldapconn has been inserted;
143  * used by avl stuff
144  */
145 int
146 ldap_back_conn_dup(
147         void *c1,
148         void *c2
149         )
150 {
151         struct ldapconn *lc1 = (struct ldapconn *)c1;
152         struct ldapconn *lc2 = (struct ldapconn *)c2;
153
154         return( ( lc1->conn == lc2->conn ) ? -1 : 0 );
155 }
156
157 #if PRINT_CONNTREE > 0
158 static void ravl_print( Avlnode *root, int depth )
159 {
160         int     i;
161         
162         if ( root == 0 )
163                 return;
164         
165         ravl_print( root->avl_right, depth+1 );
166         
167         for ( i = 0; i < depth; i++ )
168                 printf( "   " );
169
170         printf( "c(%ld) %d\n", ((struct ldapconn *) root->avl_data)->conn->c_connid, root->avl_bf );
171         
172         ravl_print( root->avl_left, depth+1 );
173 }
174
175 static void myprint( Avlnode *root )
176 {
177         printf( "********\n" );
178         
179         if ( root == 0 )
180                 printf( "\tNULL\n" );
181
182         else
183                 ravl_print( root, 0 );
184         
185         printf( "********\n" );
186 }
187 #endif /* PRINT_CONNTREE */
188
189 struct ldapconn *
190 ldap_back_getconn(struct ldapinfo *li, Connection *conn, Operation *op)
191 {
192         struct ldapconn *lc, lc_curr;
193         LDAP *ld;
194
195         /* Searches for a ldapconn in the avl tree */
196         lc_curr.conn = conn;
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 );
201
202         /* Looks like we didn't get a bind. Open a new session... */
203         if (!lc) {
204                 int vers = conn->c_protocol;
205                 int err = ldap_initialize(&ld, li->url);
206                 
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 );
211                         return( NULL );
212                 }
213                 /* Set LDAP version. This will always succeed: If the client
214                  * bound with a particular version, then so can we.
215                  */
216                 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &vers);
217
218                 lc = (struct ldapconn *)ch_malloc(sizeof(struct ldapconn));
219                 lc->conn = conn;
220                 lc->ld = ld;
221
222 #ifdef ENABLE_REWRITE
223                 /*
224                  * Sets a cookie for the rewrite session
225                  */
226                 ( void )rewrite_session_init( li->rwinfo, conn );
227 #endif /* ENABLE_REWRITE */
228
229                 if ( lc->conn->c_cdn.bv_len != 0 ) {
230                         
231                         /*
232                          * Rewrite the bind dn if needed
233                          */
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 );
243                                 }
244 #ifdef NEW_LOGGING
245                                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
246                                                 "[rw] bindDn: \"%s\" ->"
247                                                 " \"%s\"\n%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\" ->"
253                                                 " \"%s\"\n%s",
254                                                 lc->conn->c_cdn.bv_val,
255                                                 lc->bound_dn.bv_val, "" );
256 #endif /* !NEW_LOGGING */
257                                 break;
258                                 
259                         case REWRITE_REGEXEC_UNWILLING:
260                                 send_ldap_result( conn, op,
261                                                 LDAP_UNWILLING_TO_PERFORM,
262                                                 NULL, "Unwilling to perform",
263                                                 NULL, NULL );
264                                 return( NULL );
265                                 
266                         case REWRITE_REGEXEC_ERR:
267                                 send_ldap_result( conn, op,
268                                                 LDAP_OPERATIONS_ERROR,
269                                                 NULL, "Operations error",
270                                                 NULL, NULL );
271                                 return( NULL );
272                         }
273 #else /* !ENABLE_REWRITE */
274                         struct berval bv;
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 );
278                         else
279                                 lc->bound_dn = bv;
280 #endif /* !ENABLE_REWRITE */
281                 } else {
282                         lc->bound_dn.bv_val = NULL;
283                         lc->bound_dn.bv_len = 0;
284                 }
285                 lc->bound = 0;
286
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 );
291
292 #if PRINT_CONNTREE > 0
293                 myprint( li->conntree );
294 #endif /* PRINT_CONNTREE */
295                 
296                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
297
298 #ifdef NEW_LOGGING
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 */
307                 
308                 /* Err could be -1 in case a duplicate ldapconn is inserted */
309                 if ( err != 0 ) {
310                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
311                         NULL, "internal server error", NULL, NULL );
312                         /* better destroy the ldapconn struct? */
313                         return( NULL );
314                 }
315         } else {
316 #ifdef NEW_LOGGING
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 */
325         }
326         
327         return( lc );
328 }
329
330 /*
331  * ldap_back_dobind
332  *
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.
336  */
337 int
338 ldap_back_dobind(struct ldapconn *lc, Operation *op)
339 {
340         if (lc->bound) {
341                 return( lc->bound );
342         }
343
344         if (ldap_bind_s(lc->ld, lc->bound_dn.bv_val, NULL, LDAP_AUTH_SIMPLE) !=
345                 LDAP_SUCCESS) {
346                 ldap_back_op_result(lc, op);
347                 return( 0 );
348         } /* else */
349         return( lc->bound = 1 );
350 }
351
352 /* Map API errors to protocol errors... */
353
354 int
355 ldap_back_map_result(int err)
356 {
357         switch(err)
358         {
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;
366         case LDAP_TIMEOUT:
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;
376         case LDAP_NO_MEMORY:
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:
387                 return LDAP_OTHER;
388         case LDAP_CLIENT_LOOP:
389         case LDAP_REFERRAL_LIMIT_EXCEEDED:
390                 return LDAP_LOOP_DETECT;
391         default:
392                 if LDAP_API_ERROR(err)
393                         return LDAP_OTHER;
394                 else
395                         return err;
396         }
397 }
398
399 int
400 ldap_back_op_result(struct ldapconn *lc, Operation *op)
401 {
402         int err = LDAP_SUCCESS;
403         char *msg = NULL;
404         char *match = NULL;
405
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);
410
411 #ifdef ENABLE_REWRITE
412         
413         /*
414          * FIXME: need rewrite info for match; mmmh ...
415          */
416         send_ldap_result( lc->conn, op, err, match, msg, NULL, NULL );
417         /* better test the pointers before freeing? */
418         if ( match ) {
419                 free( match );
420         }
421
422 #else /* !ENABLE_REWRITE */
423
424         send_ldap_result( lc->conn, op, err, match, msg, NULL, NULL );
425         /* better test the pointers before freeing? */
426         if ( match ) {
427                 free( match );
428         }
429
430 #endif /* !ENABLE_REWRITE */
431
432         if ( msg ) free( msg );
433         return( (err==LDAP_SUCCESS) ? 0 : -1 );
434 }
435