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