]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/bind.c
work around the last issue: some times the result times out while the other party...
[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                                 rebinding = 0,
319                                 save_nretries = nretries;
320
321         /*
322          * Otherwise an anonymous bind is performed
323          * (note: if the target was already bound, the anonymous
324          * bind clears the previous bind).
325          */
326         if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
327                 ber_memfree( msc->msc_bound_ndn.bv_val );
328                 BER_BVZERO( &msc->msc_bound_ndn );
329         }
330                 
331         if ( !BER_BVISNULL( &msc->msc_cred ) ) {
332                 /* destroy sensitive data */
333                 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
334                 ber_memfree( msc->msc_cred.bv_val );
335                 BER_BVZERO( &msc->msc_cred );
336         }
337
338         /* FIXME: should we check if at least some of the op->o_ctrls
339          * can/should be passed? */
340 rebind:;
341         rc = ldap_sasl_bind( msc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
342                         NULL, NULL, &msgid );
343         if ( rc == LDAP_SUCCESS ) {
344                 LDAPMessage     *res;
345                 struct timeval  tv;
346
347                 /*
348                  * handle response!!!
349                  */
350 retry:;
351                 tv.tv_sec = 0;
352                 tv.tv_usec = META_BIND_TIMEOUT;
353                 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
354                 case 0:
355                         Debug( LDAP_DEBUG_ANY, "%s meta_back_single_dobind: ldap_result=%d nretries=%d\n",
356                                 op->o_log_prefix, 0, nretries );
357
358                         if ( nretries != META_RETRY_NEVER ) {
359                                 ldap_pvt_thread_yield();
360                                 if ( nretries > 0 ) {
361                                         nretries--;
362                                 }
363                                 goto retry;
364                         }
365
366                         rc = LDAP_BUSY;
367                         if ( rebinding ) {
368                                 break;
369                         }
370
371                         /* FIXME: some times the request times out
372                          * while the other party is not willing to
373                          * send a response any more.  Give it a second
374                          * chance with a freshly bound connection */
375                         rebinding = 1;
376                         nretries = save_nretries;
377                         /* fallthru */
378
379                 case -1:
380                         ldap_get_option( msc->msc_ld,
381                                         LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
382
383                         Debug( LDAP_DEBUG_ANY, "### %s meta_back_single_dobind: err=%d nretries=%d\n",
384                                         op->o_log_prefix, rs->sr_err, nretries );
385
386                         rc = slap_map_api2result( rs );
387                         if ( rc == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
388                                 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
389                                 msc->msc_ld = NULL;
390                                 msc->msc_bound = 0;
391
392                                 /* mc here must be the regular mc, reset and ready for init */
393                                 rc = meta_back_init_one_conn( op, rs, mt, msc, LDAP_BACK_DONTSEND );
394
395                                 if ( rc == LDAP_SUCCESS ) {
396                                         ldap_pvt_thread_yield();
397                                         if ( nretries > 0 ) {
398                                                 nretries--;
399                                         }
400                                         goto rebind;
401                                 }
402                         }
403                         break;
404
405                 default:
406                         rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
407                                         NULL, NULL, NULL, NULL, 1 );
408                         if ( rc == LDAP_SUCCESS ) {
409                                 rc = slap_map_api2result( rs );
410                         }
411                         break;
412                 }
413         }
414
415         rs->sr_err = rc;
416         if ( rc != LDAP_SUCCESS && ( sendok & LDAP_BACK_SENDERR ) ) {
417                 send_ldap_result( op, rs );
418         }
419
420         return rc;
421 }
422
423 /*
424  * meta_back_dobind
425  */
426 int
427 meta_back_dobind(
428         Operation               *op,
429         SlapReply               *rs,
430         metaconn_t              *mc,
431         ldap_back_send_t        sendok )
432 {
433         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
434
435         metasingleconn_t        *msc;
436         int                     bound = 0, i;
437
438         SlapReply               *candidates = meta_back_candidates_get( op );
439
440         ldap_pvt_thread_mutex_lock( &mc->mc_mutex );
441
442         /*
443          * all the targets are bound as pseudoroot
444          */
445         if ( mc->mc_auth_target == META_BOUND_ALL ) {
446                 bound = 1;
447                 goto done;
448         }
449
450         for ( i = 0, msc = &mc->mc_conns[ 0 ]; !META_LAST( msc ); ++i, ++msc ) {
451                 metatarget_t    *mt = mi->mi_targets[ i ];
452                 int             rc;
453
454                 /*
455                  * Not a candidate or something wrong with this target ...
456                  */
457                 if ( msc->msc_ld == NULL ) {
458                         continue;
459                 }
460
461                 /*
462                  * If the target is already bound it is skipped
463                  */
464                 if ( msc->msc_bound == META_BOUND && mc->mc_auth_target == i ) {
465                         ++bound;
466                         continue;
467                 }
468
469                 rc = meta_back_single_dobind( op, rs, mc, i,
470                                 LDAP_BACK_DONTSEND, mt->mt_nretries );
471                 if ( rc != LDAP_SUCCESS ) {
472                         Debug( LDAP_DEBUG_ANY, "%s meta_back_dobind[%d]: "
473                                         "(anonymous) err=%d\n",
474                                         op->o_log_prefix, i, rc );
475
476                         /*
477                          * null cred bind should always succeed
478                          * as anonymous, so a failure means
479                          * the target is no longer candidate possibly
480                          * due to technical reasons (remote host down?)
481                          * so better clear the handle
482                          */
483                         candidates[ i ].sr_tag = META_NOT_CANDIDATE;
484 #if 0
485                         ( void )meta_clear_one_candidate( msc );
486 #endif
487                         continue;
488                 } /* else */
489                 
490                 candidates[ i ].sr_tag = META_CANDIDATE;
491                 msc->msc_bound = META_ANONYMOUS;
492                 ++bound;
493         }
494
495 done:;
496         ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
497
498         if ( bound == 0 && sendok & LDAP_BACK_SENDERR ) {
499                 if ( rs->sr_err == LDAP_SUCCESS ) {
500                         rs->sr_err = LDAP_BUSY;
501                 }
502                 send_ldap_result( op, rs );
503         }
504
505         return( bound > 0 );
506 }
507
508 /*
509  * meta_back_rebind
510  *
511  * This is a callback used for chasing referrals using the same
512  * credentials as the original user on this session.
513  */
514 static int 
515 meta_back_rebind(
516         LDAP                    *ld,
517         LDAP_CONST char         *url,
518         ber_tag_t               request,
519         ber_int_t               msgid,
520         void                    *params )
521 {
522         metasingleconn_t        *msc = ( metasingleconn_t * )params;
523
524         return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
525                         LDAP_SASL_SIMPLE, &msc->msc_cred,
526                         NULL, NULL, NULL );
527 }
528
529 /*
530  * FIXME: error return must be handled in a cleaner way ...
531  */
532 int
533 meta_back_op_result(
534         metaconn_t      *mc,
535         Operation       *op,
536         SlapReply       *rs,
537         int             candidate )
538 {
539         int                     i,
540                                 rerr = LDAP_SUCCESS;
541         metasingleconn_t        *msc;
542         char                    *rmsg = NULL;
543         char                    *rmatch = NULL;
544         int                     free_rmsg = 0,
545                                 free_rmatch = 0;
546
547         if ( candidate != META_TARGET_NONE ) {
548                 msc = &mc->mc_conns[ candidate ];
549
550                 rs->sr_err = LDAP_SUCCESS;
551
552                 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
553                 if ( rs->sr_err != LDAP_SUCCESS ) {
554                         /*
555                          * better check the type of error. In some cases
556                          * (search ?) it might be better to return a
557                          * success if at least one of the targets gave
558                          * positive result ...
559                          */
560                         ldap_get_option( msc->msc_ld,
561                                         LDAP_OPT_ERROR_STRING, &rmsg );
562                         ldap_get_option( msc->msc_ld,
563                                         LDAP_OPT_MATCHED_DN, &rmatch );
564                         rerr = rs->sr_err = slap_map_api2result( rs );
565
566                         if ( rmsg ) {
567                                 free_rmsg = 1;
568                         }
569                         if ( rmatch ) {
570                                 free_rmatch = 1;
571                         }
572
573                         Debug(LDAP_DEBUG_ANY,
574                                         "==> meta_back_op_result: target"
575                                         " <%d> sending msg \"%s\""
576                                         " (matched \"%s\")\n", 
577                                         candidate, ( rmsg ? rmsg : "" ),
578                                         ( rmatch ? rmatch : "" ) );
579                 }
580
581         } else {
582                 for ( i = 0, msc = &mc->mc_conns[ 0 ]; !META_LAST( msc ); ++i, ++msc ) {
583                         char    *msg = NULL;
584                         char    *match = NULL;
585
586                         rs->sr_err = LDAP_SUCCESS;
587
588                         ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
589                         if ( rs->sr_err != LDAP_SUCCESS ) {
590                                 /*
591                                  * better check the type of error. In some cases
592                                  * (search ?) it might be better to return a
593                                  * success if at least one of the targets gave
594                                  * positive result ...
595                                  */
596                                 ldap_get_option( msc->msc_ld,
597                                                 LDAP_OPT_ERROR_STRING, &msg );
598                                 ldap_get_option( msc->msc_ld,
599                                                 LDAP_OPT_MATCHED_DN, &match );
600                                 rs->sr_err = slap_map_api2result( rs );
601         
602                                 Debug(LDAP_DEBUG_ANY,
603                                                 "==> meta_back_op_result: target"
604                                                 " <%d> sending msg \"%s\""
605                                                 " (matched \"%s\")\n", 
606                                                 i, ( msg ? msg : "" ),
607                                                 ( match ? match : "" ) );
608         
609                                 /*
610                                  * FIXME: need to rewrite "match" (need rwinfo)
611                                  */
612                                 switch ( rs->sr_err ) {
613                                 default:
614                                         rerr = rs->sr_err;
615                                         if ( rmsg ) {
616                                                 ber_memfree( rmsg );
617                                         }
618                                         rmsg = msg;
619                                         free_rmsg = 1;
620                                         msg = NULL;
621                                         if ( rmatch ) {
622                                                 ber_memfree( rmatch );
623                                         }
624                                         rmatch = match;
625                                         free_rmatch = 1;
626                                         match = NULL;
627                                         break;
628                                 }
629         
630                                 /* better test the pointers before freeing? */
631                                 if ( match ) {
632                                         free( match );
633                                 }
634                                 if ( msg ) {
635                                         free( msg );
636                                 }
637                         }
638                 }
639         }
640         
641         rs->sr_err = rerr;
642         rs->sr_text = rmsg;
643         rs->sr_matched = rmatch;
644         send_ldap_result( op, rs );
645         if ( free_rmsg ) {
646                 ber_memfree( rmsg );
647         }
648         if ( free_rmatch ) {
649                 ber_memfree( rmatch );
650         }
651         rs->sr_text = NULL;
652         rs->sr_matched = NULL;
653
654         return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );
655 }
656