]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/bind.c
more on error handling reworking; should address ITS#3672 and ITS#3676
[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 = { 0, 0 };
211                 int             rc;
212                 int             nretries = META_BIND_NRETRIES;
213
214                 /*
215                  * handle response!!!
216                  */
217 retry:;
218                 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
219                 case 0:
220                         if ( nretries > 0 ) {
221                                 ldap_pvt_thread_yield();
222                                 tv.tv_sec = 0;
223                                 tv.tv_usec = META_BIND_TIMEOUT;
224                                 nretries--;
225                                 goto retry;
226                         }
227                         rs->sr_err = LDAP_BUSY;
228                         break;
229
230                 case -1:
231                         ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
232                                         &rs->sr_err );
233
234                         Debug( LDAP_DEBUG_ANY, "### %s meta_back_single_bind(%s) err=%d\n",
235                                 op->o_log_prefix, mdn.bv_val, rs->sr_err );
236
237                         if ( rs->sr_err == LDAP_UNAVAILABLE && nretries > 0 ) {
238                                 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
239                                 msc->msc_ld = NULL;
240                                 msc->msc_bound = 0;
241
242                                 /* mc here must be the regular mc, reset and ready for init */
243                                 rc = meta_back_init_one_conn( op, rs, mt, msc,
244                                                 LDAP_BACK_DONTSEND );
245                                 if ( rc ) {
246                                         nretries--;
247                                         ldap_pvt_thread_yield();
248                                         goto rebind;
249                                 }
250                         }
251                         break;
252
253                 default:
254                         rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
255                                         NULL, NULL, NULL, NULL, 1 );
256                         if ( rc != LDAP_SUCCESS ) {
257                                 rs->sr_err = rc;
258                         }
259                         break;
260                 }
261         }
262
263         if ( rs->sr_err != LDAP_SUCCESS ) {
264                 rs->sr_err = slap_map_api2result( rs );
265                 goto return_results;
266         }
267
268         ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_dn );
269         msc->msc_bound = META_BOUND;
270         mc->mc_auth_target = candidate;
271
272         if ( LDAP_BACK_SAVECRED( mi ) ) {
273                 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
274                 ldap_set_rebind_proc( msc->msc_ld, meta_back_rebind, msc );
275         }
276
277         if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
278                         && op->o_req_ndn.bv_len != 0 )
279         {
280                 ( void )meta_dncache_update_entry( &mi->mi_cache,
281                                 &op->o_req_ndn, candidate );
282         }
283
284 return_results:;
285         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
286                 free( mdn.bv_val );
287         }
288
289         return rs->sr_err;
290 }
291
292 /*
293  * meta_back_single_dobind
294  */
295 int
296 meta_back_single_dobind(
297         Operation               *op,
298         SlapReply               *rs,
299         metaconn_t              *mc,
300         int                     candidate,
301         ldap_back_send_t        sendok,
302         int                     retries )
303 {
304         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
305         metatarget_t            *mt = mi->mi_targets[ candidate ];
306         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
307         int                     rc;
308         struct berval           cred = BER_BVC( "" );
309         int                     msgid;
310
311         /*
312          * Otherwise an anonymous bind is performed
313          * (note: if the target was already bound, the anonymous
314          * bind clears the previous bind).
315          */
316         if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
317                 ber_memfree( msc->msc_bound_ndn.bv_val );
318                 BER_BVZERO( &msc->msc_bound_ndn );
319         }
320                 
321         if ( !BER_BVISNULL( &msc->msc_cred ) ) {
322                 /* destroy sensitive data */
323                 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
324                 ber_memfree( msc->msc_cred.bv_val );
325                 BER_BVZERO( &msc->msc_cred );
326         }
327
328         /* FIXME: should we check if at least some of the op->o_ctrls
329          * can/should be passed? */
330 rebind:;
331         rc = ldap_sasl_bind( msc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
332                         NULL, NULL, &msgid );
333         if ( rc == LDAP_SUCCESS ) {
334                 LDAPMessage     *res;
335                 struct timeval  tv = { 0, 0 };
336
337                 /*
338                  * handle response!!!
339                  */
340 retry:;
341                 tv.tv_sec = 0;
342                 tv.tv_usec = META_BIND_TIMEOUT;
343                 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
344                 case 0:
345                         if ( retries > 0 ) {
346                                 ldap_pvt_thread_yield();
347                                 retries--;
348                                 goto retry;
349                         }
350
351                         rc = LDAP_BUSY;
352                         break;
353
354                 case -1:
355                         ldap_get_option( msc->msc_ld,
356                                         LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
357
358                         Debug( LDAP_DEBUG_ANY, "### %s meta_back_single_dobind(\"\") err=%d\n",
359                                 op->o_log_prefix, rs->sr_err, 0 );
360
361                         rc = slap_map_api2result( rs );
362                         if ( rc == LDAP_UNAVAILABLE && retries > 0 ) {
363                                 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
364                                 msc->msc_ld = NULL;
365                                 msc->msc_bound = 0;
366
367                                 /* mc here must be the regular mc, reset and ready for init */
368                                 rc = meta_back_init_one_conn( op, rs, mt, msc, LDAP_BACK_DONTSEND );
369
370                                 if ( rc ) {
371                                         ldap_pvt_thread_yield();
372                                         retries--;
373                                         goto rebind;
374                                 }
375                         }
376                         break;
377
378                 default:
379                         rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
380                                         NULL, NULL, NULL, NULL, 1 );
381                         if ( rc == LDAP_SUCCESS ) {
382                                 rc = slap_map_api2result( rs );
383                         }
384                         break;
385                 }
386         }
387
388         if ( rc != LDAP_SUCCESS && ( sendok & LDAP_BACK_SENDERR ) ) {
389                 rs->sr_err = rc;
390                 send_ldap_result( op, rs );
391         }
392
393         return rc;
394 }
395
396 /*
397  * meta_back_dobind
398  */
399 int
400 meta_back_dobind(
401         Operation               *op,
402         SlapReply               *rs,
403         metaconn_t              *mc,
404         ldap_back_send_t        sendok )
405 {
406         metasingleconn_t        *msc;
407         int                     bound = 0, i;
408
409         SlapReply               *candidates = meta_back_candidates_get( op );
410
411         ldap_pvt_thread_mutex_lock( &mc->mc_mutex );
412
413         /*
414          * all the targets are bound as pseudoroot
415          */
416         if ( mc->mc_auth_target == META_BOUND_ALL ) {
417                 bound = 1;
418                 goto done;
419         }
420
421         for ( i = 0, msc = &mc->mc_conns[ 0 ]; !META_LAST( msc ); ++i, ++msc ) {
422                 int             rc;
423
424                 /*
425                  * Not a candidate or something wrong with this target ...
426                  */
427                 if ( msc->msc_ld == NULL ) {
428                         continue;
429                 }
430
431                 /*
432                  * If the target is already bound it is skipped
433                  */
434                 if ( msc->msc_bound == META_BOUND && mc->mc_auth_target == i ) {
435                         ++bound;
436                         continue;
437                 }
438
439                 rc = meta_back_single_dobind( op, rs, mc, i,
440                         LDAP_BACK_DONTSEND, META_BIND_NRETRIES );
441                 if ( rc != LDAP_SUCCESS ) {
442                         Debug( LDAP_DEBUG_ANY, "%s meta_back_dobind[%d]: "
443                                         "(anonymous) err=%d\n",
444                                         op->o_log_prefix, i, rc );
445
446                         /*
447                          * null cred bind should always succeed
448                          * as anonymous, so a failure means
449                          * the target is no longer candidate possibly
450                          * due to technical reasons (remote host down?)
451                          * so better clear the handle
452                          */
453                         candidates[ i ].sr_tag = META_NOT_CANDIDATE;
454 #if 0
455                         ( void )meta_clear_one_candidate( msc );
456 #endif
457                         continue;
458                 } /* else */
459                 
460                 candidates[ i ].sr_tag = META_CANDIDATE;
461                 msc->msc_bound = META_ANONYMOUS;
462                 ++bound;
463         }
464
465 done:;
466         ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
467
468         if ( bound == 0 && sendok & LDAP_BACK_SENDERR ) {
469                 if ( rs->sr_err == LDAP_SUCCESS ) {
470                         rs->sr_err = LDAP_BUSY;
471                 }
472                 send_ldap_result( op, rs );
473         }
474
475         return( bound > 0 );
476 }
477
478 /*
479  * meta_back_rebind
480  *
481  * This is a callback used for chasing referrals using the same
482  * credentials as the original user on this session.
483  */
484 static int 
485 meta_back_rebind(
486         LDAP                    *ld,
487         LDAP_CONST char         *url,
488         ber_tag_t               request,
489         ber_int_t               msgid,
490         void                    *params )
491 {
492         metasingleconn_t        *msc = ( metasingleconn_t * )params;
493
494         return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
495                         LDAP_SASL_SIMPLE, &msc->msc_cred,
496                         NULL, NULL, NULL );
497 }
498
499 /*
500  * FIXME: error return must be handled in a cleaner way ...
501  */
502 int
503 meta_back_op_result(
504         metaconn_t      *mc,
505         Operation       *op,
506         SlapReply       *rs,
507         int             candidate )
508 {
509         int                     i,
510                                 rerr = LDAP_SUCCESS;
511         metasingleconn_t        *msc;
512         char                    *rmsg = NULL;
513         char                    *rmatch = NULL;
514         int                     free_rmsg = 0,
515                                 free_rmatch = 0;
516
517         if ( candidate != META_TARGET_NONE ) {
518                 msc = &mc->mc_conns[ candidate ];
519
520                 rs->sr_err = LDAP_SUCCESS;
521
522                 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
523                 if ( rs->sr_err != LDAP_SUCCESS ) {
524                         /*
525                          * better check the type of error. In some cases
526                          * (search ?) it might be better to return a
527                          * success if at least one of the targets gave
528                          * positive result ...
529                          */
530                         ldap_get_option( msc->msc_ld,
531                                         LDAP_OPT_ERROR_STRING, &rmsg );
532                         ldap_get_option( msc->msc_ld,
533                                         LDAP_OPT_MATCHED_DN, &rmatch );
534                         rerr = rs->sr_err = slap_map_api2result( rs );
535
536                         if ( rmsg ) {
537                                 free_rmsg = 1;
538                         }
539                         if ( rmatch ) {
540                                 free_rmatch = 1;
541                         }
542
543                         Debug(LDAP_DEBUG_ANY,
544                                         "==> meta_back_op_result: target"
545                                         " <%d> sending msg \"%s\""
546                                         " (matched \"%s\")\n", 
547                                         candidate, ( rmsg ? rmsg : "" ),
548                                         ( rmatch ? rmatch : "" ) );
549                 }
550
551         } else {
552                 for ( i = 0, msc = &mc->mc_conns[ 0 ]; !META_LAST( msc ); ++i, ++msc ) {
553                         char    *msg = NULL;
554                         char    *match = NULL;
555
556                         rs->sr_err = LDAP_SUCCESS;
557
558                         ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
559                         if ( rs->sr_err != LDAP_SUCCESS ) {
560                                 /*
561                                  * better check the type of error. In some cases
562                                  * (search ?) it might be better to return a
563                                  * success if at least one of the targets gave
564                                  * positive result ...
565                                  */
566                                 ldap_get_option( msc->msc_ld,
567                                                 LDAP_OPT_ERROR_STRING, &msg );
568                                 ldap_get_option( msc->msc_ld,
569                                                 LDAP_OPT_MATCHED_DN, &match );
570                                 rs->sr_err = slap_map_api2result( rs );
571         
572                                 Debug(LDAP_DEBUG_ANY,
573                                                 "==> meta_back_op_result: target"
574                                                 " <%d> sending msg \"%s\""
575                                                 " (matched \"%s\")\n", 
576                                                 i, ( msg ? msg : "" ),
577                                                 ( match ? match : "" ) );
578         
579                                 /*
580                                  * FIXME: need to rewrite "match" (need rwinfo)
581                                  */
582                                 switch ( rs->sr_err ) {
583                                 default:
584                                         rerr = rs->sr_err;
585                                         if ( rmsg ) {
586                                                 ber_memfree( rmsg );
587                                         }
588                                         rmsg = msg;
589                                         free_rmsg = 1;
590                                         msg = NULL;
591                                         if ( rmatch ) {
592                                                 ber_memfree( rmatch );
593                                         }
594                                         rmatch = match;
595                                         free_rmatch = 1;
596                                         match = NULL;
597                                         break;
598                                 }
599         
600                                 /* better test the pointers before freeing? */
601                                 if ( match ) {
602                                         free( match );
603                                 }
604                                 if ( msg ) {
605                                         free( msg );
606                                 }
607                         }
608                 }
609         }
610         
611         rs->sr_err = rerr;
612         rs->sr_text = rmsg;
613         rs->sr_matched = rmatch;
614         send_ldap_result( op, rs );
615         if ( free_rmsg ) {
616                 ber_memfree( rmsg );
617         }
618         if ( free_rmatch ) {
619                 ber_memfree( rmatch );
620         }
621         rs->sr_text = NULL;
622         rs->sr_matched = NULL;
623
624         return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );
625 }
626