]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/bind.c
a) implement a new candidate selection procedure, based on target naming
[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 = LDAP_OTHER,
53                         i, gotit = 0, ndnlen, isroot = 0;
54
55         char            *candidates = meta_back_candidates_get( op );
56
57         rs->sr_err = LDAP_SUCCESS;
58
59         Debug( LDAP_DEBUG_ARGS, "meta_back_bind: dn: %s.\n%s%s",
60                         op->o_req_dn.bv_val, "", "" );
61
62         if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
63                 isroot = 1;
64                 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ) );
65         }
66
67         /* we need meta_back_getconn() not send result even on error,
68          * because we want to intercept the error and make it
69          * invalidCredentials */
70         lc = meta_back_getconn( op, rs, NULL, LDAP_BACK_DONTSEND );
71         if ( !lc ) {
72                 Debug( LDAP_DEBUG_ANY,
73                                 "meta_back_bind: no target "
74                                 "for dn \"%s\" (%d: %s).\n",
75                                 op->o_req_dn.bv_val, rs->sr_err,
76                                 rs->sr_text ? rs->sr_text : "" );
77                 /* FIXME: there might be cases where we don't want
78                  * to map the error onto invalidCredentials */
79                 switch ( rs->sr_err ) {
80                 case LDAP_NO_SUCH_OBJECT:
81                 case LDAP_UNWILLING_TO_PERFORM:
82                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
83                         rs->sr_text = NULL;
84                         break;
85                 }
86                 send_ldap_result( op, rs );
87                 return rs->sr_err;
88         }
89
90         /*
91          * Each target is scanned ...
92          */
93         lc->mc_bound_target = META_BOUND_NONE;
94         ndnlen = op->o_req_ndn.bv_len;
95         for ( i = 0; i < li->mi_ntargets; i++ ) {
96                 int             lerr;
97                 struct berval   orig_dn = op->o_req_dn;
98                 struct berval   orig_ndn = op->o_req_ndn;
99                 struct berval   orig_cred = op->orb_cred;
100                 int             orig_method = op->orb_method;
101                 
102
103                 /*
104                  * Skip non-candidates
105                  */
106                 if ( candidates[ i ] != META_CANDIDATE ) {
107                         continue;
108                 }
109
110                 if ( gotit == 0 ) {
111                         gotit = 1;
112
113                 } else if ( isroot == 0 ) {
114                         /*
115                          * A bind operation is expected to have
116                          * ONE CANDIDATE ONLY!
117                          */
118                         Debug( LDAP_DEBUG_ANY,
119                                         "==>meta_back_bind: more than one"
120                                         " candidate is trying to bind...\n",
121                                         0, 0, 0 );
122                 }
123
124                 if ( isroot && !BER_BVISNULL( &li->mi_targets[ i ]->mt_pseudorootdn ) )
125                 {
126                         op->o_req_dn = li->mi_targets[ i ]->mt_pseudorootdn;
127                         op->o_req_ndn = li->mi_targets[ i ]->mt_pseudorootdn;
128                         op->orb_cred = li->mi_targets[ i ]->mt_pseudorootpw;
129                         op->orb_method = LDAP_AUTH_SIMPLE;
130                 }
131                 
132                 lerr = meta_back_do_single_bind( lc, op, rs, i );
133                 if ( lerr != LDAP_SUCCESS ) {
134                         rs->sr_err = lerr;
135                         candidates[ i ] = META_NOT_CANDIDATE;
136 #if 0
137                         ( void )meta_clear_one_candidate( &lc->mc_conns[ i ] );
138 #endif
139
140                 } else {
141                         rc = LDAP_SUCCESS;
142                 }
143
144                 op->o_req_dn = orig_dn;
145                 op->o_req_ndn = orig_ndn;
146                 op->orb_cred = orig_cred;
147                 op->orb_method = orig_method;
148         }
149
150         if ( isroot ) {
151                 lc->mc_bound_target = META_BOUND_ALL;
152         }
153
154         /*
155          * rc is LDAP_SUCCESS if at least one bind succeeded,
156          * err is the last error that occurred during a bind;
157          * if at least (and at most?) one bind succeedes, fine.
158          */
159         if ( rc != LDAP_SUCCESS ) {
160                 
161                 /*
162                  * deal with bind failure ...
163                  */
164
165                 /*
166                  * no target was found within the naming context, 
167                  * so bind must fail with invalid credentials
168                  */
169                 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
170                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
171                 }
172
173                 rs->sr_err = slap_map_api2result( rs );
174                 send_ldap_result( op, rs );
175                 return rs->sr_err;
176         }
177
178         return LDAP_SUCCESS;
179 }
180
181 /*
182  * meta_back_do_single_bind
183  *
184  * attempts to perform a bind with creds
185  */
186 static int
187 meta_back_do_single_bind(
188                 struct metaconn         *lc,
189                 Operation               *op,
190                 SlapReply               *rs,
191                 int                     candidate
192 )
193 {
194         struct metainfo         *li = ( struct metainfo * )op->o_bd->be_private;
195         struct berval           mdn = BER_BVNULL;
196         dncookie                dc;
197         struct metasingleconn   *lsc = &lc->mc_conns[ candidate ];
198         int                     msgid;
199         
200         /*
201          * Rewrite the bind dn if needed
202          */
203         dc.rwmap = &li->mi_targets[ candidate ]->mt_rwmap;
204         dc.conn = op->o_conn;
205         dc.rs = rs;
206         dc.ctx = "bindDN";
207
208         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
209                 send_ldap_result( op, rs );
210                 return -1;
211         }
212
213         /* FIXME: this fixes the bind problem right now; we need
214          * to use the asynchronous version to get the "matched"
215          * and more in case of failure ... */
216         /* FIXME: should be check if at least some of the op->o_ctrls
217          * can/should be passed? */
218         rs->sr_err = ldap_sasl_bind( lsc->msc_ld, mdn.bv_val,
219                         LDAP_SASL_SIMPLE, &op->orb_cred,
220                         op->o_ctrls, NULL, &msgid );
221         if ( rs->sr_err == LDAP_SUCCESS ) {
222                 LDAPMessage     *res;
223                 struct timeval  tv = { 0, 0 };
224                 int             rc;
225                 int             nretries = 0;
226
227                 /*
228                  * handle response!!!
229                  */
230 retry:;
231                 switch ( ldap_result( lsc->msc_ld, msgid, 0, &tv, &res ) ) {
232                 case 0:
233                         if ( ++nretries <= META_BIND_NRETRIES ) {
234                                 ldap_pvt_thread_yield();
235                                 tv.tv_sec = 0;
236                                 tv.tv_usec = META_BIND_TIMEOUT;
237                                 goto retry;
238                         }
239                         rs->sr_err = LDAP_BUSY;
240                         break;
241
242                 case -1:
243                         ldap_get_option( lsc->msc_ld, LDAP_OPT_ERROR_NUMBER,
244                                         &rs->sr_err );
245                         break;
246
247                 default:
248                         rc = ldap_parse_result( lsc->msc_ld, res, &rs->sr_err,
249                                         NULL, NULL, NULL, NULL, 1 );
250                         if ( rc != LDAP_SUCCESS ) {
251                                 rs->sr_err = rc;
252                         }
253                         break;
254                 }
255         }
256
257         if ( rs->sr_err != LDAP_SUCCESS ) {
258                 rs->sr_err = slap_map_api2result( rs );
259                 goto return_results;
260         }
261
262         if ( !BER_BVISNULL( &lsc->msc_bound_ndn ) ) {
263                 ber_memfree( lsc->msc_bound_ndn.bv_val );
264         }
265         ber_dupbv( &lsc->msc_bound_ndn, &op->o_req_dn );
266         lsc->msc_bound = META_BOUND;
267         lc->mc_bound_target = candidate;
268
269         if ( LDAP_BACK_SAVECRED( li ) ) {
270                 if ( !BER_BVISNULL( &lsc->msc_cred ) ) {
271                         /* destroy sensitive data */
272                         memset( lsc->msc_cred.bv_val, 0, lsc->msc_cred.bv_len );
273                         ber_memfree( lsc->msc_cred.bv_val );
274                 }
275                 ber_dupbv( &lsc->msc_cred, &op->orb_cred );
276                 ldap_set_rebind_proc( lsc->msc_ld, meta_back_rebind, lsc );
277         }
278
279         if ( li->mi_cache.ttl != META_DNCACHE_DISABLED
280                         && op->o_req_ndn.bv_len != 0 ) {
281                 ( void )meta_dncache_update_entry( &li->mi_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, ldap_back_send_t sendok )
299 {
300         struct metasingleconn   *lsc;
301         int                     bound = 0, i;
302
303         char            *candidates = meta_back_candidates_get( op );
304
305         /*
306          * all the targets are bound as pseudoroot
307          */
308         if ( lc->mc_bound_target == META_BOUND_ALL ) {
309                 return 1;
310         }
311
312         for ( i = 0, lsc = lc->mc_conns; !META_LAST( lsc ); ++i, ++lsc ) {
313                 int             rc;
314                 struct berval   cred = BER_BVC("");
315                 int             msgid;
316
317                 /*
318                  * Not a candidate or something wrong with this target ...
319                  */
320                 if ( lsc->msc_ld == NULL ) {
321                         continue;
322                 }
323
324                 /*
325                  * If the target is already bound it is skipped
326                  */
327                 if ( lsc->msc_bound == META_BOUND && lc->mc_bound_target == i ) {
328                         ++bound;
329                         continue;
330                 }
331
332                 /*
333                  * Otherwise an anonymous bind is performed
334                  * (note: if the target was already bound, the anonymous
335                  * bind clears the previous bind).
336                  */
337                 if ( !BER_BVISNULL( &lsc->msc_bound_ndn ) ) {
338                         ber_memfree( lsc->msc_bound_ndn.bv_val );
339                         BER_BVZERO( &lsc->msc_bound_ndn );
340                 }
341                 
342                 if ( /* FIXME: need li ... li->savecred && */ 
343                                 !BER_BVISNULL( &lsc->msc_cred ) )
344                 {
345                         /* destroy sensitive data */
346                         memset( lsc->msc_cred.bv_val, 0, lsc->msc_cred.bv_len );
347                         ber_memfree( lsc->msc_cred.bv_val );
348                         BER_BVZERO( &lsc->msc_cred );
349                 }
350
351                 /* FIXME: should we check if at least some of the op->o_ctrls
352                  * can/should be passed? */
353                 rc = ldap_sasl_bind( lsc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
354                                 NULL, NULL, &msgid );
355                 if ( rc == LDAP_SUCCESS ) {
356                         LDAPMessage     *res;
357                         struct timeval  tv = { 0, 0 };
358                         int             err;
359                         int             nretries = 0;
360
361                         /*
362                          * handle response!!!
363                          */
364 retry:;
365                         switch ( ldap_result( lsc->msc_ld, msgid, 0, &tv, &res ) ) {
366                         case 0:
367                                 if ( ++nretries <= META_BIND_NRETRIES ) {
368                                         ldap_pvt_thread_yield();
369                                         tv.tv_sec = 0;
370                                         tv.tv_usec = META_BIND_TIMEOUT;
371                                         goto retry;
372                                 }
373
374                                 rc = LDAP_BUSY;
375                                 break;
376
377                         case -1:
378                                 ldap_get_option( lsc->msc_ld, LDAP_OPT_ERROR_NUMBER,
379                                                 &rc );
380                                 break;
381
382                         default:
383                                 rc = ldap_parse_result( lsc->msc_ld, res, &err,
384                                                 NULL, NULL, NULL, NULL, 1 );
385                                 if ( rc == LDAP_SUCCESS ) {
386                                         rc = err;
387                                 }
388                                 break;
389                         }
390                 }
391
392                 if ( rc != LDAP_SUCCESS ) {
393                         Debug( LDAP_DEBUG_ANY,
394                                         "==>meta_back_dobind: (anonymous)"
395                                         " bind failed"
396                                         " with error %d (%s)\n",
397                                         rc, ldap_err2string( rc ), 0 );
398
399                         /*
400                          * null cred bind should always succeed
401                          * as anonymous, so a failure means
402                          * the target is no longer candidate possibly
403                          * due to technical reasons (remote host down?)
404                          * so better clear the handle
405                          */
406                         candidates[ i ] = META_NOT_CANDIDATE;
407 #if 0
408                         ( void )meta_clear_one_candidate( lsc );
409 #endif
410                         continue;
411                 } /* else */
412                 
413                 lsc->msc_bound = META_ANONYMOUS;
414                 ++bound;
415         }
416
417         return( bound > 0 );
418 }
419
420 /*
421  *
422  */
423 int
424 meta_back_is_valid( struct metaconn *lc, int candidate )
425 {
426         struct metasingleconn   *lsc;
427         int                     i;
428
429         assert( lc );
430
431         if ( candidate < 0 ) {
432                 return 0;
433         }
434
435         for ( i = 0, lsc = lc->mc_conns; !META_LAST( lsc ) && i < candidate; 
436                         ++i, ++lsc );
437         
438         if ( !META_LAST( lsc ) ) {
439                 return ( lsc->msc_ld != NULL );
440         }
441
442         return 0;
443 }
444
445 /*
446  * meta_back_rebind
447  *
448  * This is a callback used for chasing referrals using the same
449  * credentials as the original user on this session.
450  */
451 static int 
452 meta_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
453         ber_int_t msgid, void *params )
454 {
455         struct metasingleconn   *lsc = params;
456
457         return ldap_sasl_bind_s( ld, lsc->msc_bound_ndn.bv_val,
458                         LDAP_SASL_SIMPLE, &lsc->msc_cred,
459                         NULL, NULL, NULL );
460 }
461
462 /*
463  * FIXME: error return must be handled in a cleaner way ...
464  */
465 int
466 meta_back_op_result(
467         struct metaconn *lc,
468         Operation       *op,
469         SlapReply       *rs,
470         int             candidate )
471 {
472         int                     i,
473                                 rerr = LDAP_SUCCESS;
474         struct metasingleconn   *lsc;
475         char                    *rmsg = NULL;
476         char                    *rmatch = NULL;
477         int                     free_rmsg = 0,
478                                 free_rmatch = 0;
479
480         if ( candidate != META_TARGET_NONE ) {
481                 lsc = &lc->mc_conns[ candidate ];
482
483                 rs->sr_err = LDAP_SUCCESS;
484
485                 ldap_get_option( lsc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
486                 if ( rs->sr_err != LDAP_SUCCESS ) {
487                         /*
488                          * better check the type of error. In some cases
489                          * (search ?) it might be better to return a
490                          * success if at least one of the targets gave
491                          * positive result ...
492                          */
493                         ldap_get_option( lsc->msc_ld,
494                                         LDAP_OPT_ERROR_STRING, &rmsg );
495                         ldap_get_option( lsc->msc_ld,
496                                         LDAP_OPT_MATCHED_DN, &rmatch );
497                         rerr = rs->sr_err = slap_map_api2result( rs );
498
499                         if ( rmsg ) {
500                                 free_rmsg = 1;
501                         }
502                         if ( rmatch ) {
503                                 free_rmatch = 1;
504                         }
505
506                         Debug(LDAP_DEBUG_ANY,
507                                         "==> meta_back_op_result: target"
508                                         " <%d> sending msg \"%s\""
509                                         " (matched \"%s\")\n", 
510                                         candidate, ( rmsg ? rmsg : "" ),
511                                         ( rmatch ? rmatch : "" ) );
512                 }
513
514         } else {
515                 for ( i = 0, lsc = lc->mc_conns; !META_LAST( lsc ); ++i, ++lsc ) {
516                         char    *msg = NULL;
517                         char    *match = NULL;
518
519                         rs->sr_err = LDAP_SUCCESS;
520
521                         ldap_get_option( lsc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
522                         if ( rs->sr_err != LDAP_SUCCESS ) {
523                                 /*
524                                  * better check the type of error. In some cases
525                                  * (search ?) it might be better to return a
526                                  * success if at least one of the targets gave
527                                  * positive result ...
528                                  */
529                                 ldap_get_option( lsc->msc_ld,
530                                                 LDAP_OPT_ERROR_STRING, &msg );
531                                 ldap_get_option( lsc->msc_ld,
532                                                 LDAP_OPT_MATCHED_DN, &match );
533                                 rs->sr_err = slap_map_api2result( rs );
534         
535                                 Debug(LDAP_DEBUG_ANY,
536                                                 "==> meta_back_op_result: target"
537                                                 " <%d> sending msg \"%s\""
538                                                 " (matched \"%s\")\n", 
539                                                 i, ( msg ? msg : "" ),
540                                                 ( match ? match : "" ) );
541         
542                                 /*
543                                  * FIXME: need to rewrite "match" (need rwinfo)
544                                  */
545                                 switch ( rs->sr_err ) {
546                                 default:
547                                         rerr = rs->sr_err;
548                                         if ( rmsg ) {
549                                                 ber_memfree( rmsg );
550                                         }
551                                         rmsg = msg;
552                                         free_rmsg = 1;
553                                         msg = NULL;
554                                         if ( rmatch ) {
555                                                 ber_memfree( rmatch );
556                                         }
557                                         rmatch = match;
558                                         free_rmatch = 1;
559                                         match = NULL;
560                                         break;
561                                 }
562         
563                                 /* better test the pointers before freeing? */
564                                 if ( match ) {
565                                         free( match );
566                                 }
567                                 if ( msg ) {
568                                         free( msg );
569                                 }
570                         }
571                 }
572         }
573         
574         rs->sr_err = rerr;
575         rs->sr_text = rmsg;
576         rs->sr_matched = rmatch;
577         send_ldap_result( op, rs );
578         if ( free_rmsg ) {
579                 ber_memfree( rmsg );
580         }
581         if ( free_rmatch ) {
582                 ber_memfree( rmatch );
583         }
584         rs->sr_text = NULL;
585         rs->sr_matched = NULL;
586
587         return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );
588 }
589