]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/bind.c
760b152c96427efe42ff35bf35543e4c39433dc1
[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 static LDAP_REBIND_PROC ldap_back_rebind;
53
54 int
55 ldap_back_bind(
56     Backend             *be,
57     Connection          *conn,
58     Operation           *op,
59     struct berval       *dn,
60     struct berval       *ndn,
61     int                 method,
62     struct berval       *cred,
63     struct berval       *edn
64 )
65 {
66         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
67         struct ldapconn *lc;
68
69         struct berval mdn = { 0, NULL };
70         int rc = 0;
71
72         lc = ldap_back_getconn(li, conn, op);
73         if ( !lc ) {
74                 return( -1 );
75         }
76
77         if ( op->o_ctrls ) {
78                 ldap_set_option( lc->ld, LDAP_OPT_SERVER_CONTROLS, op->o_ctrls );
79         }
80         
81         /*
82          * Rewrite the bind dn if needed
83          */
84 #ifdef ENABLE_REWRITE
85         switch ( rewrite_session( li->rwinfo, "bindDn", dn->bv_val, conn, &mdn.bv_val ) ) {
86         case REWRITE_REGEXEC_OK:
87                 if ( mdn.bv_val == NULL ) {
88                         mdn.bv_val = ( char * )dn->bv_val;
89                 }
90 #ifdef NEW_LOGGING
91                 LDAP_LOG( BACK_LDAP, DETAIL1, 
92                         "[rw] bindDn: \"%s\" -> \"%s\"\n", dn->bv_val, mdn.bv_val, 0 );
93 #else /* !NEW_LOGGING */
94                 Debug( LDAP_DEBUG_ARGS, "rw> bindDn: \"%s\" -> \"%s\"\n%s",
95                                 dn->bv_val, mdn.bv_val, "" );
96 #endif /* !NEW_LOGGING */
97                 break;
98                 
99         case REWRITE_REGEXEC_UNWILLING:
100                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
101                                 NULL, "Unwilling to perform", NULL, NULL );
102                 return( -1 );
103
104         case REWRITE_REGEXEC_ERR:
105                 send_ldap_result( conn, op, LDAP_OTHER,
106                                 NULL, "Operations error", NULL, NULL );
107                 return( -1 );
108         }
109 #else /* !ENABLE_REWRITE */
110         ldap_back_dn_massage( li, dn, &mdn, 0, 1 );
111 #endif /* !ENABLE_REWRITE */
112
113         rc = ldap_bind_s(lc->ld, mdn.bv_val, cred->bv_val, method);
114         if (rc != LDAP_SUCCESS) {
115                 rc = ldap_back_op_result( lc, op );
116         } else {
117                 lc->bound = 1;
118         }
119
120         if ( li->savecred ) {
121                 if ( lc->cred.bv_val )
122                         ch_free( lc->cred.bv_val );
123                 ber_dupbv( &lc->cred, cred );
124                 ldap_set_rebind_proc( lc->ld, ldap_back_rebind, lc );
125         }
126
127         if ( lc->bound_dn.bv_val )
128                 ch_free( lc->bound_dn.bv_val );
129         if ( mdn.bv_val != dn->bv_val ) {
130                 lc->bound_dn = mdn;
131         } else {
132                 ber_dupbv( &lc->bound_dn, dn );
133         }
134         
135         return( rc );
136 }
137
138 /*
139  * ldap_back_conn_cmp
140  *
141  * compares two struct ldapconn based on the value of the conn pointer;
142  * used by avl stuff
143  */
144 int
145 ldap_back_conn_cmp(
146         const void *c1,
147         const void *c2
148         )
149 {
150         const struct ldapconn *lc1 = (const struct ldapconn *)c1;
151         const struct ldapconn *lc2 = (const struct ldapconn *)c2;
152         
153         return ( ( lc1->conn < lc2->conn ) ? -1 : ( ( lc1->conn > lc2-> conn ) ? 1 : 0 ) );
154 }
155
156 /*
157  * ldap_back_conn_dup
158  *
159  * returns -1 in case a duplicate struct ldapconn has been inserted;
160  * used by avl stuff
161  */
162 int
163 ldap_back_conn_dup(
164         void *c1,
165         void *c2
166         )
167 {
168         struct ldapconn *lc1 = (struct ldapconn *)c1;
169         struct ldapconn *lc2 = (struct ldapconn *)c2;
170
171         return( ( lc1->conn == lc2->conn ) ? -1 : 0 );
172 }
173
174 #if PRINT_CONNTREE > 0
175 static void ravl_print( Avlnode *root, int depth )
176 {
177         int     i;
178         
179         if ( root == 0 )
180                 return;
181         
182         ravl_print( root->avl_right, depth+1 );
183         
184         for ( i = 0; i < depth; i++ )
185                 printf( "   " );
186
187         printf( "c(%ld) %d\n", ((struct ldapconn *) root->avl_data)->conn->c_connid, root->avl_bf );
188         
189         ravl_print( root->avl_left, depth+1 );
190 }
191
192 static void myprint( Avlnode *root )
193 {
194         printf( "********\n" );
195         
196         if ( root == 0 )
197                 printf( "\tNULL\n" );
198
199         else
200                 ravl_print( root, 0 );
201         
202         printf( "********\n" );
203 }
204 #endif /* PRINT_CONNTREE */
205
206 struct ldapconn *
207 ldap_back_getconn(struct ldapinfo *li, Connection *conn, Operation *op)
208 {
209         struct ldapconn *lc, lc_curr;
210         LDAP *ld;
211
212         /* Searches for a ldapconn in the avl tree */
213         lc_curr.conn = conn;
214         ldap_pvt_thread_mutex_lock( &li->conn_mutex );
215         lc = (struct ldapconn *)avl_find( li->conntree, 
216                 (caddr_t)&lc_curr, ldap_back_conn_cmp );
217         ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
218
219         /* Looks like we didn't get a bind. Open a new session... */
220         if (!lc) {
221                 int vers = conn->c_protocol;
222                 int err = ldap_initialize(&ld, li->url);
223                 
224                 if (err != LDAP_SUCCESS) {
225                         err = ldap_back_map_result(err);
226                         send_ldap_result( conn, op, err,
227                                 NULL, "ldap_init failed", NULL, NULL );
228                         return( NULL );
229                 }
230                 /* Set LDAP version. This will always succeed: If the client
231                  * bound with a particular version, then so can we.
232                  */
233                 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &vers);
234
235                 lc = (struct ldapconn *)ch_malloc(sizeof(struct ldapconn));
236                 lc->conn = conn;
237                 lc->ld = ld;
238
239                 lc->cred.bv_len = 0;
240                 lc->cred.bv_val = NULL;
241
242 #ifdef ENABLE_REWRITE
243                 /*
244                  * Sets a cookie for the rewrite session
245                  */
246                 ( void )rewrite_session_init( li->rwinfo, conn );
247 #endif /* ENABLE_REWRITE */
248
249                 if ( lc->conn->c_dn.bv_len != 0 ) {
250                         
251                         /*
252                          * Rewrite the bind dn if needed
253                          */
254 #ifdef ENABLE_REWRITE                   
255                         lc->bound_dn.bv_val = NULL;
256                         lc->bound_dn.bv_len = 0;
257                         switch ( rewrite_session( li->rwinfo, "bindDn",
258                                                 lc->conn->c_dn.bv_val, conn,
259                                                 &lc->bound_dn.bv_val ) ) {
260                         case REWRITE_REGEXEC_OK:
261                                 if ( lc->bound_dn.bv_val == NULL ) {
262                                         ber_dupbv( &lc->bound_dn, &lc->conn->c_dn );
263                                 }
264 #ifdef NEW_LOGGING
265                                 LDAP_LOG( BACK_LDAP, DETAIL1, 
266                                                 "[rw] bindDn: \"%s\" ->" 
267                                                 " \"%s\"\n%s",
268                                                 lc->conn->c_dn.bv_val, 
269                                                 lc->bound_dn.bv_val, "" );
270 #else /* !NEW_LOGGING */
271                                 Debug( LDAP_DEBUG_ARGS,
272                                                 "rw> bindDn: \"%s\" ->"
273                                                 " \"%s\"\n%s",
274                                                 lc->conn->c_dn.bv_val,
275                                                 lc->bound_dn.bv_val, "" );
276 #endif /* !NEW_LOGGING */
277                                 break;
278                                 
279                         case REWRITE_REGEXEC_UNWILLING:
280                                 send_ldap_result( conn, op,
281                                                 LDAP_UNWILLING_TO_PERFORM,
282                                                 NULL, "Unwilling to perform",
283                                                 NULL, NULL );
284                                 return( NULL );
285                                 
286                         case REWRITE_REGEXEC_ERR:
287                                 send_ldap_result( conn, op,
288                                                 LDAP_OTHER,
289                                                 NULL, "Operations error",
290                                                 NULL, NULL );
291                                 return( NULL );
292                         }
293 #else /* !ENABLE_REWRITE */
294                         struct berval bv;
295                         ldap_back_dn_massage( li, &lc->conn->c_dn, &bv, 0, 1 );
296                         if ( bv.bv_val == lc->conn->c_dn.bv_val )
297                                 ber_dupbv( &lc->bound_dn, &bv );
298                         else
299                                 lc->bound_dn = bv;
300 #endif /* !ENABLE_REWRITE */
301                 } else {
302                         lc->bound_dn.bv_val = NULL;
303                         lc->bound_dn.bv_len = 0;
304                 }
305                 lc->bound = 0;
306
307                 /* Inserts the newly created ldapconn in the avl tree */
308                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
309                 err = avl_insert( &li->conntree, (caddr_t)lc,
310                         ldap_back_conn_cmp, ldap_back_conn_dup );
311
312 #if PRINT_CONNTREE > 0
313                 myprint( li->conntree );
314 #endif /* PRINT_CONNTREE */
315                 
316                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
317
318 #ifdef NEW_LOGGING
319                 LDAP_LOG( BACK_LDAP, INFO, 
320                         "ldap_back_getconn: conn %ld inserted\n", lc->conn->c_connid, 0, 0);
321 #else /* !NEW_LOGGING */
322                 Debug( LDAP_DEBUG_TRACE,
323                         "=>ldap_back_getconn: conn %ld inserted\n%s%s",
324                         lc->conn->c_connid, "", "" );
325 #endif /* !NEW_LOGGING */
326                 
327                 /* Err could be -1 in case a duplicate ldapconn is inserted */
328                 if ( err != 0 ) {
329                         send_ldap_result( conn, op, LDAP_OTHER,
330                         NULL, "internal server error", NULL, NULL );
331                         /* better destroy the ldapconn struct? */
332                         return( NULL );
333                 }
334         } else {
335 #ifdef NEW_LOGGING
336                 LDAP_LOG( BACK_LDAP, INFO, 
337                         "ldap_back_getconn: conn %ld inserted\n", 
338                         lc->conn->c_connid, 0, 0 );
339 #else /* !NEW_LOGGING */
340                 Debug( LDAP_DEBUG_TRACE,
341                         "=>ldap_back_getconn: conn %ld fetched%s%s\n",
342                         lc->conn->c_connid, "", "" );
343 #endif /* !NEW_LOGGING */
344         }
345         
346         return( lc );
347 }
348
349 /*
350  * ldap_back_dobind
351  *
352  * Note: as the check for the value of lc->bound was already here, I removed
353  * it from all the callers, and I made the function return the flag, so
354  * it can be used to simplify the check.
355  */
356 int
357 ldap_back_dobind( struct ldapconn *lc, Operation *op )
358 {
359         if ( lc->bound ) {
360                 return( lc->bound );
361         }
362
363         if ( op->o_ctrls ) {
364                 ldap_set_option( lc->ld, LDAP_OPT_SERVER_CONTROLS,
365                                 op->o_ctrls );
366         }
367         
368         if ( ldap_bind_s( lc->ld, lc->bound_dn.bv_val, lc->cred.bv_val, 
369                                 LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
370                 ldap_back_op_result( lc, op );
371                 return( 0 );
372         } /* else */
373         return( lc->bound = 1 );
374 }
375
376 /*
377  * ldap_back_rebind
378  *
379  * This is a callback used for chasing referrals using the same
380  * credentials as the original user on this session.
381  */
382 static int 
383 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
384         ber_int_t msgid, void *params )
385 {
386         struct ldapconn *lc = params;
387
388         return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val, LDAP_AUTH_SIMPLE );
389 }
390
391 /* Map API errors to protocol errors... */
392
393 int
394 ldap_back_map_result(int err)
395 {
396         switch(err)
397         {
398         case LDAP_SERVER_DOWN:
399                 return LDAP_UNAVAILABLE;
400         case LDAP_LOCAL_ERROR:
401                 return LDAP_OTHER;
402         case LDAP_ENCODING_ERROR:
403         case LDAP_DECODING_ERROR:
404                 return LDAP_PROTOCOL_ERROR;
405         case LDAP_TIMEOUT:
406                 return LDAP_UNAVAILABLE;
407         case LDAP_AUTH_UNKNOWN:
408                 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
409         case LDAP_FILTER_ERROR:
410                 return LDAP_OTHER;
411         case LDAP_USER_CANCELLED:
412                 return LDAP_OTHER;
413         case LDAP_PARAM_ERROR:
414                 return LDAP_PROTOCOL_ERROR;
415         case LDAP_NO_MEMORY:
416                 return LDAP_OTHER;
417         case LDAP_CONNECT_ERROR:
418                 return LDAP_UNAVAILABLE;
419         case LDAP_NOT_SUPPORTED:
420                 return LDAP_UNWILLING_TO_PERFORM;
421         case LDAP_CONTROL_NOT_FOUND:
422                 return LDAP_PROTOCOL_ERROR;
423         case LDAP_NO_RESULTS_RETURNED:
424                 return LDAP_NO_SUCH_OBJECT;
425         case LDAP_MORE_RESULTS_TO_RETURN:
426                 return LDAP_OTHER;
427         case LDAP_CLIENT_LOOP:
428         case LDAP_REFERRAL_LIMIT_EXCEEDED:
429                 return LDAP_LOOP_DETECT;
430         default:
431                 if LDAP_API_ERROR(err)
432                         return LDAP_OTHER;
433                 else
434                         return err;
435         }
436 }
437
438 int
439 ldap_back_op_result(struct ldapconn *lc, Operation *op)
440 {
441         int err = LDAP_SUCCESS;
442         char *msg = NULL;
443         char *match = NULL;
444
445         ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER, &err);
446         ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &msg);
447         ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
448         err = ldap_back_map_result(err);
449
450 #ifdef ENABLE_REWRITE
451         
452         /*
453          * FIXME: need rewrite info for match; mmmh ...
454          */
455         send_ldap_result( lc->conn, op, err, match, msg, NULL, NULL );
456         /* better test the pointers before freeing? */
457         if ( match ) {
458                 free( match );
459         }
460
461 #else /* !ENABLE_REWRITE */
462
463         send_ldap_result( lc->conn, op, err, match, msg, NULL, NULL );
464         /* better test the pointers before freeing? */
465         if ( match ) {
466                 free( match );
467         }
468
469 #endif /* !ENABLE_REWRITE */
470
471         if ( msg ) free( msg );
472         return( (err==LDAP_SUCCESS) ? 0 : -1 );
473 }
474