]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/bind.c
e6df502d878821a55bf3d7bc2ba17680597de66f
[openldap] / servers / slapd / back-ldap / bind.c
1 /* bind.c - ldap backend bind function */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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         char *mdn = 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 ) ) {
80         case REWRITE_REGEXEC_OK:
81                 if ( mdn == NULL ) {
82                         mdn = ( 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 ));
87 #else /* !NEW_LOGGING */
88                 Debug( LDAP_DEBUG_ARGS, "rw> bindDn: \"%s\" -> \"%s\"\n%s",
89                                 dn->bv_val, mdn, "" );
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         mdn = ldap_back_dn_massage( li, ch_strdup( dn->bv_val ), 0 );
105 #endif /* !ENABLE_REWRITE */
106
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 );
110         } else {
111                 lc->bound = 1;
112         }
113
114 #ifdef ENABLE_REWRITE   
115         if ( mdn != dn->bv_val ) {
116 #endif /* ENABLE_REWRITE */
117         free( mdn );
118 #ifdef ENABLE_REWRITE
119         }
120 #endif /* ENABLE_REWRITE */
121         
122         return( rc );
123 }
124
125 /*
126  * ldap_back_conn_cmp
127  *
128  * compares two struct ldapconn based on the value of the conn pointer;
129  * used by avl stuff
130  */
131 int
132 ldap_back_conn_cmp(
133         const void *c1,
134         const void *c2
135         )
136 {
137         struct ldapconn *lc1 = (struct ldapconn *)c1;
138         struct ldapconn *lc2 = (struct ldapconn *)c2;
139         
140         return ( ( lc1->conn < lc2->conn ) ? -1 : ( ( lc1->conn > lc2-> conn ) ? 1 : 0 ) );
141 }
142
143 /*
144  * ldap_back_conn_dup
145  *
146  * returns -1 in case a duplicate struct ldapconn has been inserted;
147  * used by avl stuff
148  */
149 int
150 ldap_back_conn_dup(
151         void *c1,
152         void *c2
153         )
154 {
155         struct ldapconn *lc1 = (struct ldapconn *)c1;
156         struct ldapconn *lc2 = (struct ldapconn *)c2;
157
158         return( ( lc1->conn == lc2->conn ) ? -1 : 0 );
159 }
160
161 #if PRINT_CONNTREE > 0
162 static void ravl_print( Avlnode *root, int depth )
163 {
164         int     i;
165         
166         if ( root == 0 )
167                 return;
168         
169         ravl_print( root->avl_right, depth+1 );
170         
171         for ( i = 0; i < depth; i++ )
172                 printf( "   " );
173
174         printf( "c(%ld) %d\n", ((struct ldapconn *) root->avl_data)->conn->c_connid, root->avl_bf );
175         
176         ravl_print( root->avl_left, depth+1 );
177 }
178
179 static void myprint( Avlnode *root )
180 {
181         printf( "********\n" );
182         
183         if ( root == 0 )
184                 printf( "\tNULL\n" );
185
186         else
187                 ravl_print( root, 0 );
188         
189         printf( "********\n" );
190 }
191 #endif /* PRINT_CONNTREE */
192
193 struct ldapconn *
194 ldap_back_getconn(struct ldapinfo *li, Connection *conn, Operation *op)
195 {
196         struct ldapconn *lc, lc_curr;
197         LDAP *ld;
198
199         /* Searches for a ldapconn in the avl tree */
200         lc_curr.conn = conn;
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 );
205
206         /* Looks like we didn't get a bind. Open a new session... */
207         if (!lc) {
208                 int vers = conn->c_protocol;
209                 int err = ldap_initialize(&ld, li->url);
210                 
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 );
215                         return( NULL );
216                 }
217                 /* Set LDAP version. This will always succeed: If the client
218                  * bound with a particular version, then so can we.
219                  */
220                 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &vers);
221
222                 lc = (struct ldapconn *)ch_malloc(sizeof(struct ldapconn));
223                 lc->conn = conn;
224                 lc->ld = ld;
225
226 #ifdef ENABLE_REWRITE
227                 /*
228                  * Sets a cookie for the rewrite session
229                  */
230                 ( void )rewrite_session_init( li->rwinfo, conn );
231 #endif /* ENABLE_REWRITE */
232
233                 if ( lc->conn->c_cdn != NULL && lc->conn->c_cdn[0] != '\0' ) {
234                         
235                         /*
236                          * Rewrite the bind dn if needed
237                          */
238 #ifdef ENABLE_REWRITE                   
239                         lc->bound_dn = NULL;
240                         switch ( rewrite_session( li->rwinfo, "bindDn",
241                                                 lc->conn->c_cdn, conn,
242                                                 &lc->bound_dn ) ) {
243                         case REWRITE_REGEXEC_OK:
244                                 if ( lc->bound_dn == NULL ) {
245                                         lc->bound_dn = 
246                                                 ch_strdup( lc->conn->c_cdn );
247                                 }
248 #ifdef NEW_LOGGING
249                                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
250                                                 "[rw] bindDn: \"%s\" ->"
251                                                 " \"%s\"\n%s",
252                                                 lc->conn->c_cdn,
253                                                 lc->bound_dn ));
254 #else /* !NEW_LOGGING */
255                                 Debug( LDAP_DEBUG_ARGS,
256                                                 "rw> bindDn: \"%s\" ->"
257                                                 " \"%s\"\n%s",
258                                                 lc->conn->c_cdn,
259                                                 lc->bound_dn, "" );
260 #endif /* !NEW_LOGGING */
261                                 break;
262                                 
263                         case REWRITE_REGEXEC_UNWILLING:
264                                 send_ldap_result( conn, op,
265                                                 LDAP_UNWILLING_TO_PERFORM,
266                                                 NULL, "Unwilling to perform",
267                                                 NULL, NULL );
268                                 return( NULL );
269                                 
270                         case REWRITE_REGEXEC_ERR:
271                                 send_ldap_result( conn, op,
272                                                 LDAP_OPERATIONS_ERROR,
273                                                 NULL, "Operations error",
274                                                 NULL, NULL );
275                                 return( NULL );
276                         }
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 */
281                 } else {
282                         lc->bound_dn = NULL;
283                 }
284                 lc->bound = 0;
285
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 );
290
291 #if PRINT_CONNTREE > 0
292                 myprint( li->conntree );
293 #endif /* PRINT_CONNTREE */
294                 
295                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
296
297 #ifdef NEW_LOGGING
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 */
306                 
307                 /* Err could be -1 in case a duplicate ldapconn is inserted */
308                 if ( err != 0 ) {
309                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
310                         NULL, "internal server error", NULL, NULL );
311                         /* better destroy the ldapconn struct? */
312                         return( NULL );
313                 }
314         } else {
315 #ifdef NEW_LOGGING
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 */
324         }
325         
326         return( lc );
327 }
328
329 /*
330  * ldap_back_dobind
331  *
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.
335  */
336 int
337 ldap_back_dobind(struct ldapconn *lc, Operation *op)
338 {
339         if (lc->bound) {
340                 return( lc->bound );
341         }
342
343         if (ldap_bind_s(lc->ld, lc->bound_dn, NULL, LDAP_AUTH_SIMPLE) !=
344                 LDAP_SUCCESS) {
345                 ldap_back_op_result(lc, op);
346                 return( 0 );
347         } /* else */
348         return( lc->bound = 1 );
349 }
350
351 /* Map API errors to protocol errors... */
352
353 int
354 ldap_back_map_result(int err)
355 {
356         switch(err)
357         {
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;
365         case LDAP_TIMEOUT:
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;
375         case LDAP_NO_MEMORY:
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:
386                 return LDAP_OTHER;
387         case LDAP_CLIENT_LOOP:
388         case LDAP_REFERRAL_LIMIT_EXCEEDED:
389                 return LDAP_LOOP_DETECT;
390         default:
391                 if LDAP_API_ERROR(err)
392                         return LDAP_OTHER;
393                 else
394                         return err;
395         }
396 }
397
398 int
399 ldap_back_op_result(struct ldapconn *lc, Operation *op)
400 {
401         int err = LDAP_SUCCESS;
402         char *msg = NULL;
403         char *match = NULL;
404
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);
409
410 #ifdef ENABLE_REWRITE
411         
412         /*
413          * need rewrite info; mmmh ...
414          */
415
416 #else /* !ENABLE_REWRITE */
417
418         send_ldap_result( lc->conn, op, err, match, msg, NULL, NULL );
419         /* better test the pointers before freeing? */
420         if ( match ) {
421                 free( match );
422         }
423 #endif /* !ENABLE_REWRITE */
424
425         if ( msg ) free( msg );
426         return( (err==LDAP_SUCCESS) ? 0 : -1 );
427 }
428