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