]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/bind.c
fix controls propagation (ITS#3813)
[openldap] / servers / slapd / back-meta / bind.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2005 The OpenLDAP Foundation.
5  * Portions Copyright 2001-2003 Pierangelo Masarati.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by the Howard Chu for inclusion
19  * in OpenLDAP Software and subsequently enhanced by Pierangelo
20  * Masarati.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/socket.h>
28 #include <ac/string.h>
29
30
31 #define AVL_INTERNAL
32 #include "slap.h"
33 #include "../back-ldap/back-ldap.h"
34 #include "back-meta.h"
35
36 static LDAP_REBIND_PROC meta_back_rebind;
37
38 static int
39 meta_back_do_single_bind(
40                 struct metaconn         *lc,
41                 Operation               *op,
42                 SlapReply               *rs,
43                 int                     candidate
44 );
45
46 int
47 meta_back_bind( Operation *op, SlapReply *rs )
48 {
49         struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
50         struct metaconn *lc;
51
52         int rc = -1, i, gotit = 0, ndnlen, isroot = 0;
53         int op_type = META_OP_ALLOW_MULTIPLE;
54
55         rs->sr_err = LDAP_SUCCESS;
56
57 #ifdef NEW_LOGGING
58         LDAP_LOG( BACK_META, ENTRY, "meta_back_bind: dn: %s.\n",
59                         op->o_req_dn.bv_val, 0, 0 );
60 #else /* !NEW_LOGGING */
61         Debug( LDAP_DEBUG_ARGS, "meta_back_bind: dn: %s.\n%s%s",
62                         op->o_req_dn.bv_val, "", "" );
63 #endif /* !NEW_LOGGING */
64
65         if ( op->oq_bind.rb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
66                 isroot = 1;
67                 ber_dupbv( &op->oq_bind.rb_edn, be_root_dn( op->o_bd ) );
68                 op_type = META_OP_REQUIRE_ALL;
69         }
70         lc = meta_back_getconn( op, rs, op_type,
71                         &op->o_req_ndn, NULL );
72         if ( !lc ) {
73 #ifdef NEW_LOGGING
74                 LDAP_LOG( BACK_META, NOTICE,
75                                 "meta_back_bind: no target for dn %s.\n",
76                                 op->o_req_dn.bv_val, 0, 0 );
77 #else /* !NEW_LOGGING */
78                 Debug( LDAP_DEBUG_ANY,
79                                 "meta_back_bind: no target for dn %s.\n%s%s",
80                                 op->o_req_dn.bv_val, "", "");
81 #endif /* !NEW_LOGGING */
82
83                 send_ldap_result( op, rs );
84                 return -1;
85         }
86
87         /*
88          * Each target is scanned ...
89          */
90         lc->bound_target = META_BOUND_NONE;
91         ndnlen = op->o_req_ndn.bv_len;
92         for ( i = 0; i < li->ntargets; i++ ) {
93                 int             lerr;
94                 struct berval   orig_dn = op->o_req_dn;
95                 struct berval   orig_ndn = op->o_req_ndn;
96                 struct berval   orig_cred = op->oq_bind.rb_cred;
97                 int             orig_method = op->oq_bind.rb_method;
98                 
99
100                 /*
101                  * Skip non-candidates
102                  */
103                 if ( lc->conns[ i ].candidate != META_CANDIDATE ) {
104                         continue;
105                 }
106
107                 if ( gotit == 0 ) {
108                         gotit = 1;
109                 } else {
110                         /*
111                          * A bind operation is expected to have
112                          * ONE CANDIDATE ONLY!
113                          */
114 #ifdef NEW_LOGGING
115                         LDAP_LOG( BACK_META, WARNING,
116                                         "==>meta_back_bind: more than one"
117                                         " candidate is attempting to bind"
118                                         " ...\n" , 0, 0, 0 );
119 #else /* !NEW_LOGGING */
120                         Debug( LDAP_DEBUG_ANY,
121                                         "==>meta_back_bind: more than one"
122                                         " candidate is attempting to bind"
123                                         " ...\n%s%s%s", 
124                                         "", "", "" );
125 #endif /* !NEW_LOGGING */
126                 }
127
128                 if ( isroot && li->targets[ i ]->pseudorootdn.bv_val != NULL ) {
129                         op->o_req_dn = li->targets[ i ]->pseudorootdn;
130                         op->o_req_ndn = li->targets[ i ]->pseudorootdn;
131                         op->oq_bind.rb_cred = li->targets[ i ]->pseudorootpw;
132                         op->oq_bind.rb_method = LDAP_AUTH_SIMPLE;
133                 }
134                 
135                 lerr = meta_back_do_single_bind( lc, op, rs, i );
136                 if ( lerr != LDAP_SUCCESS ) {
137                         rs->sr_err = lerr;
138                         ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
139                 } else {
140                         rc = LDAP_SUCCESS;
141                 }
142
143                 op->o_req_dn = orig_dn;
144                 op->o_req_ndn = orig_ndn;
145                 op->oq_bind.rb_cred = orig_cred;
146                 op->oq_bind.rb_method = orig_method;
147         }
148
149         if ( isroot ) {
150                 lc->bound_target = META_BOUND_ALL;
151         }
152
153         /*
154          * rc is LDAP_SUCCESS if at least one bind succeeded,
155          * err is the last error that occurred during a bind;
156          * if at least (and at most?) one bind succeedes, fine.
157          */
158         if ( rc != LDAP_SUCCESS /* && rs->sr_err != LDAP_SUCCESS */ ) {
159                 
160                 /*
161                  * deal with bind failure ...
162                  */
163
164                 /*
165                  * no target was found within the naming context, 
166                  * so bind must fail with invalid credentials
167                  */
168                 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
169                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
170                 }
171
172                 rs->sr_err = slap_map_api2result( rs );
173                 send_ldap_result( op, rs );
174                 return -1;
175         }
176
177         return 0;
178 }
179
180 /*
181  * meta_back_do_single_bind
182  *
183  * attempts to perform a bind with creds
184  */
185 static int
186 meta_back_do_single_bind(
187                 struct metaconn         *lc,
188                 Operation               *op,
189                 SlapReply               *rs,
190                 int                     candidate
191 )
192 {
193         struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
194         struct berval   mdn = BER_BVNULL;
195         ber_int_t       msgid;
196         dncookie        dc;
197         struct metasingleconn   *lsc = &lc->conns[ candidate ];
198         
199         /*
200          * Rewrite the bind dn if needed
201          */
202         dc.rwmap = &li->targets[ candidate ]->rwmap;
203         dc.conn = op->o_conn;
204         dc.rs = rs;
205         dc.ctx = "bindDN";
206
207         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
208                 send_ldap_result( op, rs );
209                 return -1;
210         }
211
212         if ( op->o_ctrls ) {
213                 rs->sr_err = ldap_set_option( lsc->ld, 
214                                 LDAP_OPT_SERVER_CONTROLS, op->o_ctrls );
215                 if ( rs->sr_err != LDAP_SUCCESS ) {
216                         rs->sr_err = slap_map_api2result( rs );
217                         goto return_results;
218                 }
219         }
220
221         /* FIXME: this fixes the bind problem right now; we need
222          * to use the asynchronous version to get the "matched"
223          * and more in case of failure ... */
224         rs->sr_err = ldap_sasl_bind( lsc->ld, mdn.bv_val,
225                         LDAP_SASL_SIMPLE, &op->oq_bind.rb_cred,
226                         op->o_ctrls, NULL, &msgid );
227         if ( rs->sr_err == LDAP_SUCCESS ) {
228                 LDAPMessage     *res;
229                 struct timeval  tv = { 0, 0 };
230                 int             rc;
231
232 retry:;
233                 switch ( ldap_result( lsc->ld, msgid, 0, &tv, &res ) ) {
234                 case 0:
235                         tv.tv_sec = 0;
236                         tv.tv_usec = 1000;      /* 0.001 s */
237                         ldap_pvt_thread_yield();
238                         goto retry;
239
240                 case -1:
241                         ldap_get_option( lsc->ld, LDAP_OPT_ERROR_NUMBER,
242                                         &rs->sr_err );
243                         break;
244
245                 default:
246                         rc = ldap_parse_result( lsc->ld, res, &rs->sr_err,
247                                         NULL, NULL, NULL, NULL, 1 );
248                         if ( rc != LDAP_SUCCESS ) {
249                                 rs->sr_err = rc;
250                         }
251                         break;
252                 }
253         }
254
255         if ( rs->sr_err != LDAP_SUCCESS ) {
256                 rs->sr_err = slap_map_api2result( rs );
257                 goto return_results;
258         }
259
260         /*
261          * FIXME: handle response!!!
262          */
263         if ( lsc->bound_dn.bv_val != NULL ) {
264                 ber_memfree( lsc->bound_dn.bv_val );
265         }
266         ber_dupbv( &lsc->bound_dn, &op->o_req_dn );
267         lsc->bound = META_BOUND;
268         lc->bound_target = candidate;
269
270         if ( li->savecred ) {
271                 if ( lsc->cred.bv_val ) {
272                         memset( lsc->cred.bv_val, 0, lsc->cred.bv_len );
273                         ber_memfree( lsc->cred.bv_val );
274                 }
275                 ber_dupbv( &lsc->cred, &op->oq_bind.rb_cred );
276                 ldap_set_rebind_proc( lsc->ld, meta_back_rebind, lsc );
277         }
278
279         if ( li->cache.ttl != META_DNCACHE_DISABLED
280                         && op->o_req_ndn.bv_len != 0 ) {
281                 ( void )meta_dncache_update_entry( &li->cache,
282                                 &op->o_req_ndn, candidate );
283         }
284
285 return_results:;
286         
287         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
288                 free( mdn.bv_val );
289         }
290
291         return rs->sr_err;
292 }
293
294 /*
295  * meta_back_dobind
296  */
297 int
298 meta_back_dobind( struct metaconn *lc, Operation *op )
299 {
300         struct metasingleconn *lsc;
301         int bound = 0, i;
302
303         /*
304          * all the targets are bound as pseudoroot
305          */
306         if ( lc->bound_target == META_BOUND_ALL ) {
307                 return 1;
308         }
309
310         for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
311                 int             rc;
312                 struct berval   cred = BER_BVC("");
313                 int             msgid;
314
315                 /*
316                  * Not a candidate or something wrong with this target ...
317                  */
318                 if ( lsc->ld == NULL ) {
319                         continue;
320                 }
321
322                 /*
323                  * If required, set controls
324                  */
325                 if ( op->o_ctrls ) {
326                         if ( ldap_set_option( lsc->ld, LDAP_OPT_SERVER_CONTROLS,
327                                         op->o_ctrls ) != LDAP_SUCCESS ) {
328                                 ( void )meta_clear_one_candidate( lsc, 1 );
329                                 continue;
330                         }
331                 }
332         
333                 /*
334                  * If the target is already bound it is skipped
335                  */
336                 if ( lsc->bound == META_BOUND && lc->bound_target == i ) {
337                         ++bound;
338                         continue;
339                 }
340
341                 /*
342                  * Otherwise an anonymous bind is performed
343                  * (note: if the target was already bound, the anonymous
344                  * bind clears the previous bind).
345                  */
346                 if ( lsc->bound_dn.bv_val ) {
347                         ber_memfree( lsc->bound_dn.bv_val );
348                         lsc->bound_dn.bv_val = NULL;
349                         lsc->bound_dn.bv_len = 0;
350                 }
351                 
352                 if ( /* FIXME: need li ... li->savecred && */ 
353                                 lsc->cred.bv_val ) {
354                         memset( lsc->cred.bv_val, 0, lsc->cred.bv_len );
355                         ber_memfree( lsc->cred.bv_val );
356                         lsc->cred.bv_val = NULL;
357                         lsc->cred.bv_len = 0;
358                 }
359
360                 rc = ldap_sasl_bind( lsc->ld, "", LDAP_SASL_SIMPLE, &cred,
361                                 NULL, NULL, &msgid );
362                 if ( rc == LDAP_SUCCESS ) {
363                         LDAPMessage     *res;
364                         struct timeval  tv = { 0, 0 };
365                         int             err;
366
367 retry:;
368                         switch ( ldap_result( lsc->ld, msgid, 0, &tv, &res ) ) {
369                         case 0:
370                                 tv.tv_sec = 0;
371                                 tv.tv_usec = 1000;      /* 0.001 s */
372                                 ldap_pvt_thread_yield();
373                                 goto retry;
374
375                         case -1:
376                                 ldap_get_option( lsc->ld, LDAP_OPT_ERROR_NUMBER,
377                                                 &rc );
378                                 break;
379
380                         default:
381                                 rc = ldap_parse_result( lsc->ld, res, &err,
382                                                 NULL, NULL, NULL, NULL, 1 );
383                                 if ( rc == LDAP_SUCCESS ) {
384                                         rc = err;
385                                 }
386                                 break;
387                         }
388                 }
389
390                 if ( rc != LDAP_SUCCESS ) {
391 #ifdef NEW_LOGGING
392                         LDAP_LOG( BACK_META, WARNING,
393                                         "meta_back_dobind: (anonymous)"
394                                         " bind failed"
395                                         " with error %d (%s)\n",
396                                         rc, ldap_err2string( rc ), 0 );
397 #else /* !NEW_LOGGING */
398                         Debug( LDAP_DEBUG_ANY,
399                                         "==>meta_back_dobind: (anonymous)"
400                                         " bind failed"
401                                         " with error %d (%s)\n",
402                                         rc, ldap_err2string( rc ), 0 );
403 #endif /* !NEW_LOGGING */
404
405                         /*
406                          * null cred bind should always succeed
407                          * as anonymous, so a failure means
408                          * the target is no longer candidate possibly
409                          * due to technical reasons (remote host down?)
410                          * so better clear the handle
411                          */
412                         ( void )meta_clear_one_candidate( lsc, 1 );
413                         continue;
414                 } /* else */
415                 
416                 lsc->bound = META_ANONYMOUS;
417                 ++bound;
418         }
419
420         return( bound > 0 );
421 }
422
423 /*
424  *
425  */
426 int
427 meta_back_is_valid( struct metaconn *lc, int candidate )
428 {
429         struct metasingleconn   *lsc;
430         int                     i;
431
432         assert( lc );
433
434         if ( candidate < 0 ) {
435                 return 0;
436         }
437
438         for ( i = 0, lsc = lc->conns; !META_LAST(lsc) && i < candidate; 
439                         ++i, ++lsc );
440         
441         if ( !META_LAST(lsc) ) {
442                 return( lsc->ld != NULL );
443         }
444
445         return 0;
446 }
447
448 /*
449  * meta_back_rebind
450  *
451  * This is a callback used for chasing referrals using the same
452  * credentials as the original user on this session.
453  */
454 static int 
455 meta_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
456         ber_int_t msgid, void *params )
457 {
458         struct metasingleconn *lc = params;
459
460         return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val,
461                         LDAP_AUTH_SIMPLE );
462 }
463
464 /*
465  * FIXME: error return must be handled in a cleaner way ...
466  */
467 int
468 meta_back_op_result( struct metaconn *lc, Operation *op, SlapReply *rs )
469 {
470         int i, rerr = LDAP_SUCCESS;
471         struct metasingleconn *lsc;
472         char *rmsg = NULL;
473         char *rmatch = NULL;
474         int     free_rmsg = 0, free_rmatch = 0;
475
476         for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
477                 char *msg = NULL;
478                 char *match = NULL;
479
480                 rs->sr_err = LDAP_SUCCESS;
481
482                 ldap_get_option( lsc->ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
483                 if ( rs->sr_err != LDAP_SUCCESS ) {
484                         /*
485                          * better check the type of error. In some cases
486                          * (search ?) it might be better to return a
487                          * success if at least one of the targets gave
488                          * positive result ...
489                          */
490                         ldap_get_option( lsc->ld,
491                                         LDAP_OPT_ERROR_STRING, &msg );
492                         ldap_get_option( lsc->ld,
493                                         LDAP_OPT_MATCHED_DN, &match );
494                         rs->sr_err = slap_map_api2result( rs );
495
496 #ifdef NEW_LOGGING
497                         LDAP_LOG( BACK_META, RESULTS,
498                                         "meta_back_op_result: target"
499                                         " <%d> sending msg \"%s\""
500                                         " (matched \"%s\")\n",
501                                         i, ( msg ? msg : "" ),
502                                         ( match ? match : "" ) );
503 #else /* !NEW_LOGGING */
504                         Debug(LDAP_DEBUG_ANY,
505                                         "==> meta_back_op_result: target"
506                                         " <%d> sending msg \"%s\""
507                                         " (matched \"%s\")\n", 
508                                         i, ( msg ? msg : "" ),
509                                         ( match ? match : "" ) );
510 #endif /* !NEW_LOGGING */
511
512                         /*
513                          * FIXME: need to rewrite "match" (need rwinfo)
514                          */
515                         switch ( rs->sr_err ) {
516                         default:
517                                 rerr = rs->sr_err;
518                                 if ( rmsg ) {
519                                         ber_memfree( rmsg );
520                                 }
521                                 rmsg = msg;
522                                 free_rmsg = 1;
523                                 msg = NULL;
524                                 if ( rmatch ) {
525                                         ber_memfree( rmatch );
526                                 }
527                                 rmatch = match;
528                                 free_rmatch = 1;
529                                 match = NULL;
530                                 break;
531                         }
532
533                         /* better test the pointers before freeing? */
534                         if ( match ) {
535                                 free( match );
536                         }
537                         if ( msg ) {
538                                 free( msg );
539                         }
540                 }
541         }
542
543         rs->sr_err = rerr;
544         rs->sr_text = rmsg;
545         rs->sr_matched = rmatch;
546         send_ldap_result( op, rs );
547         if ( free_rmsg ) {
548                 ber_memfree( rmsg );
549         }
550         if ( free_rmatch ) {
551                 ber_memfree( rmatch );
552         }
553         rs->sr_text = NULL;
554         rs->sr_matched = NULL;
555
556         return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );
557 }
558