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