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