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