]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/bind.c
Free IDL_CACHE locks
[openldap] / servers / slapd / back-ldap / bind.c
1 /* bind.c - ldap backend bind function */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 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     Operation           *op,
57     SlapReply           *rs )
58 {
59         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
60         struct ldapconn *lc;
61
62         struct berval mdn = { 0, NULL };
63         int rc = 0;
64         ber_int_t msgid;
65         dncookie dc;
66
67         lc = ldap_back_getconn(op, rs);
68         if ( !lc ) {
69                 return( -1 );
70         }
71
72         /*
73          * Rewrite the bind dn if needed
74          */
75         dc.rwmap = &li->rwmap;
76 #ifdef ENABLE_REWRITE
77         dc.conn = op->o_conn;
78         dc.rs = rs;
79         dc.ctx = "bindDn";
80 #else
81         dc.tofrom = 1;
82         dc.normalized = 0;
83 #endif
84         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
85                 send_ldap_result( op, rs );
86                 return -1;
87         }
88
89         if ( lc->bound_dn.bv_val ) {
90                 ch_free( lc->bound_dn.bv_val );
91                 lc->bound_dn.bv_len = 0;
92                 lc->bound_dn.bv_val = NULL;
93         }
94         lc->bound = 0;
95         /* method is always LDAP_AUTH_SIMPLE if we got here */
96         rs->sr_err = ldap_sasl_bind(lc->ld, mdn.bv_val, LDAP_SASL_SIMPLE,
97                 &op->oq_bind.rb_cred, op->o_ctrls, NULL, &msgid);
98         rc = ldap_back_op_result( lc, op, rs, msgid, 1 );
99         if (rc == LDAP_SUCCESS) {
100                 lc->bound = 1;
101                 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
102                         lc->bound_dn = mdn;
103                 } else {
104                         ber_dupbv( &lc->bound_dn, &op->o_req_dn );
105                 }
106                 mdn.bv_val = NULL;
107
108                 if ( li->savecred ) {
109                         if ( lc->cred.bv_val )
110                                 ch_free( lc->cred.bv_val );
111                         ber_dupbv( &lc->cred, &op->oq_bind.rb_cred );
112                         ldap_set_rebind_proc( lc->ld, ldap_back_rebind, lc );
113                 }
114         }
115
116         /* must re-insert if local DN changed as result of bind */
117         if ( lc->bound && !bvmatch(&op->o_req_ndn, &lc->local_dn ) ) {
118                 int lerr;
119
120                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
121                 lc = avl_delete( &li->conntree, (caddr_t)lc,
122                                 ldap_back_conn_cmp );
123                 if ( lc->local_dn.bv_val )
124                         ch_free( lc->local_dn.bv_val );
125                 ber_dupbv( &lc->local_dn, &op->o_req_ndn );
126                 lerr = avl_insert( &li->conntree, (caddr_t)lc,
127                         ldap_back_conn_cmp, ldap_back_conn_dup );
128                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
129                 if ( lerr == -1 ) {
130                         ldap_back_conn_free( lc );
131                 }
132         }
133
134         if ( mdn.bv_val && mdn.bv_val != op->o_req_dn.bv_val ) {
135                 free( mdn.bv_val );
136         }
137
138         return( rc );
139 }
140
141 /*
142  * ldap_back_conn_cmp
143  *
144  * compares two struct ldapconn based on the value of the conn pointer;
145  * used by avl stuff
146  */
147 int
148 ldap_back_conn_cmp(
149         const void *c1,
150         const void *c2
151         )
152 {
153         const struct ldapconn *lc1 = (const struct ldapconn *)c1;
154         const struct ldapconn *lc2 = (const struct ldapconn *)c2;
155         int rc;
156         
157         /* If local DNs don't match, it is definitely not a match */
158         if ( ( rc = ber_bvcmp( &lc1->local_dn, &lc2->local_dn )) )
159                 return rc;
160
161         /* For shared sessions, conn is NULL. Only explicitly
162          * bound sessions will have non-NULL conn.
163          */
164         return lc1->conn - lc2->conn;
165 }
166
167 /*
168  * ldap_back_conn_dup
169  *
170  * returns -1 in case a duplicate struct ldapconn has been inserted;
171  * used by avl stuff
172  */
173 int
174 ldap_back_conn_dup(
175         void *c1,
176         void *c2
177         )
178 {
179         struct ldapconn *lc1 = (struct ldapconn *)c1;
180         struct ldapconn *lc2 = (struct ldapconn *)c2;
181
182         /* Cannot have more than one shared session with same DN */
183         if ( dn_match( &lc1->local_dn, &lc2->local_dn ) &&
184                  lc1->conn == lc2->conn ) return -1;
185                 
186         return 0;
187 }
188
189 #if PRINT_CONNTREE > 0
190 static void ravl_print( Avlnode *root, int depth )
191 {
192         int     i;
193         struct ldapconn *lc;
194         
195         if ( root == 0 )
196                 return;
197         
198         ravl_print( root->avl_right, depth+1 );
199         
200         for ( i = 0; i < depth; i++ )
201                 printf( "   " );
202
203         lc = root->avl_data;
204         printf( "lc(%lx) local(%s) conn(%lx) %d\n",
205                         lc, lc->local_dn.bv_val, lc->conn, root->avl_bf );
206         
207         ravl_print( root->avl_left, depth+1 );
208 }
209
210 static void myprint( Avlnode *root )
211 {
212         printf( "********\n" );
213         
214         if ( root == 0 )
215                 printf( "\tNULL\n" );
216
217         else
218                 ravl_print( root, 0 );
219         
220         printf( "********\n" );
221 }
222 #endif /* PRINT_CONNTREE */
223
224 struct ldapconn *
225 ldap_back_getconn(Operation *op, SlapReply *rs)
226 {
227         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
228         struct ldapconn *lc, lc_curr;
229         LDAP *ld;
230         int is_priv = 0;
231
232         /* Searches for a ldapconn in the avl tree */
233
234         /* Explicit binds must not be shared */
235         if ( op->o_tag == LDAP_REQ_BIND
236                 || (op->o_conn
237                   && (op->o_bd == op->o_conn->c_authz_backend ))) {
238                 lc_curr.conn = op->o_conn;
239         } else {
240                 lc_curr.conn = NULL;
241         }
242         
243         /* Internal searches are privileged and shared. So is root. */
244         if ( op->o_do_not_cache || be_isroot( li->be, &op->o_ndn ) ) {
245                 lc_curr.local_dn = li->be->be_rootndn;
246                 lc_curr.conn = NULL;
247                 is_priv = 1;
248         } else {
249                 lc_curr.local_dn = op->o_ndn;
250         }
251
252         ldap_pvt_thread_mutex_lock( &li->conn_mutex );
253         lc = (struct ldapconn *)avl_find( li->conntree, 
254                 (caddr_t)&lc_curr, ldap_back_conn_cmp );
255         ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
256
257         /* Looks like we didn't get a bind. Open a new session... */
258         if (!lc) {
259                 int vers = op->o_protocol;
260                 rs->sr_err = ldap_initialize(&ld, li->url);
261                 
262                 if (rs->sr_err != LDAP_SUCCESS) {
263                         rs->sr_err = ldap_back_map_result(rs);
264                         if (rs->sr_text == NULL) {
265                                 rs->sr_text = "ldap_initialize() failed";
266                         }
267                         if (op->o_conn) send_ldap_result( op, rs );
268                         rs->sr_text = NULL;
269                         return( NULL );
270                 }
271                 /* Set LDAP version. This will always succeed: If the client
272                  * bound with a particular version, then so can we.
273                  */
274                 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION,
275                                 (const void *)&vers);
276                 /* FIXME: configurable? */
277                 ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON);
278
279                 lc = (struct ldapconn *)ch_malloc(sizeof(struct ldapconn));
280                 lc->conn = lc_curr.conn;
281                 lc->ld = ld;
282                 ber_dupbv( &lc->local_dn, &lc_curr.local_dn );
283
284 #ifdef ENABLE_REWRITE
285                 /*
286                  * Sets a cookie for the rewrite session
287                  *
288                  * FIXME: the o_conn might be no longer valid,
289                  * since we may have different entries
290                  * for the same connection
291                  */
292                 ( void )rewrite_session_init( li->rwmap.rwm_rw, op->o_conn );
293 #endif /* ENABLE_REWRITE */
294
295                 ldap_pvt_thread_mutex_init( &lc->lc_mutex );
296
297                 if ( is_priv ) {
298                         ber_dupbv( &lc->cred, &li->bindpw );
299                         ber_dupbv( &lc->bound_dn, &li->binddn );
300                 } else {
301                         lc->cred.bv_len = 0;
302                         lc->cred.bv_val = NULL;
303                         lc->bound_dn.bv_val = NULL;
304                         lc->bound_dn.bv_len = 0;
305                         if ( op->o_conn->c_dn.bv_len != 0
306                                         && ( op->o_bd == op->o_conn->c_authz_backend ) ) {
307                                 
308                                 dncookie dc;
309                                 struct berval bv;
310
311                                 /*
312                                  * Rewrite the bind dn if needed
313                                  */
314                                 dc.rwmap = &li->rwmap;
315 #ifdef ENABLE_REWRITE
316                                 dc.conn = op->o_conn;
317                                 dc.rs = rs;
318                                 dc.ctx = "bindDn";
319 #else
320                                 dc.tofrom = 1;
321                                 dc.normalized = 0;
322 #endif
323
324                                 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn, &bv ) ) {
325                                         if (op->o_conn) send_ldap_result( op, rs );
326                                         return NULL;
327                                 }
328
329                                 if ( bv.bv_val == op->o_conn->c_dn.bv_val ) {
330                                         ber_dupbv( &lc->bound_dn, &bv );
331                                 } else {
332                                         lc->bound_dn = bv;
333                                 }
334                         }
335                 }
336
337                 lc->bound = 0;
338
339                 /* Inserts the newly created ldapconn in the avl tree */
340                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
341                 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
342                         ldap_back_conn_cmp, ldap_back_conn_dup );
343
344 #if PRINT_CONNTREE > 0
345                 myprint( li->conntree );
346 #endif /* PRINT_CONNTREE */
347         
348                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
349
350 #ifdef NEW_LOGGING
351                 LDAP_LOG( BACK_LDAP, INFO, 
352                         "ldap_back_getconn: conn %lx inserted\n", lc, 0, 0);
353 #else /* !NEW_LOGGING */
354                 Debug( LDAP_DEBUG_TRACE,
355                         "=>ldap_back_getconn: conn %lx inserted\n", lc, 0, 0 );
356 #endif /* !NEW_LOGGING */
357         
358                 /* Err could be -1 in case a duplicate ldapconn is inserted */
359                 if ( rs->sr_err != 0 ) {
360                         ldap_back_conn_free( lc );
361                         if (op->o_conn) {
362                                 send_ldap_error( op, rs, LDAP_OTHER,
363                                 "internal server error" );
364                         }
365                         return( NULL );
366                 }
367         } else {
368 #ifdef NEW_LOGGING
369                 LDAP_LOG( BACK_LDAP, INFO, 
370                         "ldap_back_getconn: conn %lx fetched\n", 
371                         lc, 0, 0 );
372 #else /* !NEW_LOGGING */
373                 Debug( LDAP_DEBUG_TRACE,
374                         "=>ldap_back_getconn: conn %lx fetched\n", lc, 0, 0 );
375 #endif /* !NEW_LOGGING */
376         }
377         
378         return( lc );
379 }
380
381 /*
382  * ldap_back_dobind
383  *
384  * Note: as the check for the value of lc->bound was already here, I removed
385  * it from all the callers, and I made the function return the flag, so
386  * it can be used to simplify the check.
387  */
388 int
389 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs )
390 {       
391         int rc;
392         ber_int_t msgid;
393
394         ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
395         if ( !lc->bound ) {
396                 rs->sr_err = ldap_sasl_bind(lc->ld, lc->bound_dn.bv_val,
397                         LDAP_SASL_SIMPLE, &lc->cred, NULL, NULL, &msgid);
398                 rc = ldap_back_op_result( lc, op, rs, msgid, 0 );
399                 if (rc == LDAP_SUCCESS) {
400                         lc->bound = 1;
401                 }
402         }
403         rc = lc->bound;
404         ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
405         return rc;
406 }
407
408 /*
409  * ldap_back_rebind
410  *
411  * This is a callback used for chasing referrals using the same
412  * credentials as the original user on this session.
413  */
414 static int 
415 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
416         ber_int_t msgid, void *params )
417 {
418         struct ldapconn *lc = params;
419
420         return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val, LDAP_AUTH_SIMPLE );
421 }
422
423 /* Map API errors to protocol errors... */
424
425 int
426 ldap_back_map_result(SlapReply *rs)
427 {
428         switch(rs->sr_err)
429         {
430         case LDAP_SERVER_DOWN:
431                 return LDAP_UNAVAILABLE;
432         case LDAP_LOCAL_ERROR:
433                 return LDAP_OTHER;
434         case LDAP_ENCODING_ERROR:
435         case LDAP_DECODING_ERROR:
436                 return LDAP_PROTOCOL_ERROR;
437         case LDAP_TIMEOUT:
438                 return LDAP_UNAVAILABLE;
439         case LDAP_AUTH_UNKNOWN:
440                 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
441         case LDAP_FILTER_ERROR:
442                 rs->sr_text = "Filter error";
443                 return LDAP_OTHER;
444         case LDAP_USER_CANCELLED:
445                 rs->sr_text = "User cancelled";
446                 return LDAP_OTHER;
447         case LDAP_PARAM_ERROR:
448                 return LDAP_PROTOCOL_ERROR;
449         case LDAP_NO_MEMORY:
450                 return LDAP_OTHER;
451         case LDAP_CONNECT_ERROR:
452                 return LDAP_UNAVAILABLE;
453         case LDAP_NOT_SUPPORTED:
454                 return LDAP_UNWILLING_TO_PERFORM;
455         case LDAP_CONTROL_NOT_FOUND:
456                 return LDAP_PROTOCOL_ERROR;
457         case LDAP_NO_RESULTS_RETURNED:
458                 return LDAP_NO_SUCH_OBJECT;
459         case LDAP_MORE_RESULTS_TO_RETURN:
460                 rs->sr_text = "More results to return";
461                 return LDAP_OTHER;
462         case LDAP_CLIENT_LOOP:
463         case LDAP_REFERRAL_LIMIT_EXCEEDED:
464                 return LDAP_LOOP_DETECT;
465         default:
466                 if LDAP_API_ERROR(rs->sr_err)
467                         return LDAP_OTHER;
468                 else
469                         return rs->sr_err;
470         }
471 }
472
473 int
474 ldap_back_op_result(struct ldapconn *lc, Operation *op, SlapReply *rs,
475         ber_int_t msgid, int sendok)
476 {
477         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
478         char *match = NULL;
479         LDAPMessage *res;
480         char *text = NULL;
481
482         rs->sr_text = NULL;
483         rs->sr_matched = NULL;
484
485         if (rs->sr_err == LDAP_SUCCESS) {
486                 if (ldap_result(lc->ld, msgid, 1, NULL, &res) == -1) {
487                         ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER,
488                                         &rs->sr_err);
489                 } else {
490                         int rc = ldap_parse_result(lc->ld, res, &rs->sr_err,
491                                         &match, &text, NULL, NULL, 1);
492                         rs->sr_text = text;
493                         if (rc != LDAP_SUCCESS) rs->sr_err = rc;
494                 }
495         }
496
497         if (rs->sr_err != LDAP_SUCCESS) {
498                 rs->sr_err = ldap_back_map_result(rs);
499
500                 /* internal ops must not reply to client */
501                 if ( op->o_conn && !op->o_do_not_cache && match ) {
502                         struct berval dn, mdn;
503                         dncookie dc;
504
505                         dc.rwmap = &li->rwmap;
506 #ifdef ENABLE_REWRITE
507                         dc.conn = op->o_conn;
508                         dc.rs = rs;
509                         dc.ctx = "matchedDn";
510 #else
511                         dc.tofrom = 0;
512                         dc.normalized = 0;
513 #endif
514                         ber_str2bv(match, 0, 0, &dn);
515                         ldap_back_dn_massage(&dc, &dn, &mdn);
516                         rs->sr_matched = mdn.bv_val;
517                                 
518                 }
519         }
520         if (op->o_conn && (sendok || rs->sr_err != LDAP_SUCCESS)) {
521                 send_ldap_result( op, rs );
522         }
523         if ( match ) {
524                 if ( rs->sr_matched != match ) {
525                         free( (char *)rs->sr_matched );
526                 }
527                 rs->sr_matched = NULL;
528                 ldap_memfree( match );
529         }
530         if ( text ) {
531                 ldap_memfree( text );
532         }
533         rs->sr_text = NULL;
534         return( (rs->sr_err == LDAP_SUCCESS) ? 0 : -1 );
535 }
536