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