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