]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/bind.c
major namespace cleanup & minor fixes
[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                 } else {
122                         rc = LDAP_SUCCESS;
123                 }
124
125                 op->o_req_dn = orig_dn;
126                 op->o_req_ndn = orig_ndn;
127                 op->orb_cred = orig_cred;
128                 op->orb_method = orig_method;
129         }
130
131         if ( isroot ) {
132                 lc->mc_bound_target = META_BOUND_ALL;
133         }
134
135         /*
136          * rc is LDAP_SUCCESS if at least one bind succeeded,
137          * err is the last error that occurred during a bind;
138          * if at least (and at most?) one bind succeedes, fine.
139          */
140         if ( rc != LDAP_SUCCESS /* && rs->sr_err != LDAP_SUCCESS */ ) {
141                 
142                 /*
143                  * deal with bind failure ...
144                  */
145
146                 /*
147                  * no target was found within the naming context, 
148                  * so bind must fail with invalid credentials
149                  */
150                 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
151                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
152                 }
153
154                 rs->sr_err = slap_map_api2result( rs );
155                 send_ldap_result( op, rs );
156                 return -1;
157         }
158
159         return 0;
160 }
161
162 /*
163  * meta_back_do_single_bind
164  *
165  * attempts to perform a bind with creds
166  */
167 static int
168 meta_back_do_single_bind(
169                 struct metaconn         *lc,
170                 Operation               *op,
171                 SlapReply               *rs,
172                 int                     candidate
173 )
174 {
175         struct metainfo         *li = ( struct metainfo * )op->o_bd->be_private;
176         struct berval           mdn = BER_BVNULL;
177         dncookie                dc;
178         struct metasingleconn   *lsc = &lc->mc_conns[ candidate ];
179         int                     msgid;
180         
181         /*
182          * Rewrite the bind dn if needed
183          */
184         dc.rwmap = &li->targets[ candidate ]->mt_rwmap;
185         dc.conn = op->o_conn;
186         dc.rs = rs;
187         dc.ctx = "bindDN";
188
189         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
190                 send_ldap_result( op, rs );
191                 return -1;
192         }
193
194         if ( op->o_ctrls ) {
195                 rs->sr_err = ldap_set_option( lsc->msc_ld, 
196                                 LDAP_OPT_SERVER_CONTROLS, op->o_ctrls );
197                 if ( rs->sr_err != LDAP_SUCCESS ) {
198                         rs->sr_err = slap_map_api2result( rs );
199                         goto return_results;
200                 }
201         }
202
203         /* FIXME: this fixes the bind problem right now; we need
204          * to use the asynchronous version to get the "matched"
205          * and more in case of failure ... */
206         rs->sr_err = ldap_sasl_bind( lsc->msc_ld, mdn.bv_val,
207                         LDAP_SASL_SIMPLE, &op->orb_cred,
208                         op->o_ctrls, NULL, &msgid );
209         if ( rs->sr_err == LDAP_SUCCESS ) {
210                 LDAPMessage     *res;
211                 struct timeval  tv = { 0, 0 };
212                 int             rc;
213
214                 /*
215                  * handle response!!!
216                  */
217 retry:;
218                 switch ( ldap_result( lsc->msc_ld, msgid, 0, &tv, &res ) ) {
219                 case 0:
220                         ldap_pvt_thread_yield();
221                         tv.tv_sec = 0;
222                         tv.tv_usec = 100000;    /* 0.1 s */
223                         goto retry;
224
225                 case -1:
226                         ldap_get_option( lsc->msc_ld, LDAP_OPT_ERROR_NUMBER,
227                                         &rs->sr_err );
228                         break;
229
230                 default:
231                         rc = ldap_parse_result( lsc->msc_ld, res, &rs->sr_err,
232                                         NULL, NULL, NULL, NULL, 1 );
233                         if ( rc != LDAP_SUCCESS ) {
234                                 rs->sr_err = rc;
235                         }
236                         break;
237                 }
238         }
239
240         if ( rs->sr_err != LDAP_SUCCESS ) {
241                 rs->sr_err = slap_map_api2result( rs );
242                 goto return_results;
243         }
244
245         if ( !BER_BVISNULL( &lsc->msc_bound_ndn ) ) {
246                 ber_memfree( lsc->msc_bound_ndn.bv_val );
247         }
248         ber_dupbv( &lsc->msc_bound_ndn, &op->o_req_dn );
249         lsc->msc_bound = META_BOUND;
250         lc->mc_bound_target = candidate;
251
252         if ( li->savecred ) {
253                 if ( !BER_BVISNULL( &lsc->msc_cred ) ) {
254                         /* destroy sensitive data */
255                         memset( lsc->msc_cred.bv_val, 0, lsc->msc_cred.bv_len );
256                         ber_memfree( lsc->msc_cred.bv_val );
257                 }
258                 ber_dupbv( &lsc->msc_cred, &op->orb_cred );
259                 ldap_set_rebind_proc( lsc->msc_ld, meta_back_rebind, lsc );
260         }
261
262         if ( li->cache.ttl != META_DNCACHE_DISABLED
263                         && op->o_req_ndn.bv_len != 0 ) {
264                 ( void )meta_dncache_update_entry( &li->cache,
265                                 &op->o_req_ndn, candidate );
266         }
267
268 return_results:;
269         
270         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
271                 free( mdn.bv_val );
272         }
273
274         return rs->sr_err;
275 }
276
277 /*
278  * meta_back_dobind
279  */
280 int
281 meta_back_dobind( struct metaconn *lc, Operation *op )
282 {
283         struct metasingleconn   *lsc;
284         int                     bound = 0, i;
285
286         /*
287          * all the targets are bound as pseudoroot
288          */
289         if ( lc->mc_bound_target == META_BOUND_ALL ) {
290                 return 1;
291         }
292
293         for ( i = 0, lsc = lc->mc_conns; !META_LAST( lsc ); ++i, ++lsc ) {
294                 int             rc;
295                 struct berval   cred = BER_BVC("");
296                 int             msgid;
297
298                 /*
299                  * Not a candidate or something wrong with this target ...
300                  */
301                 if ( lsc->msc_ld == NULL ) {
302                         continue;
303                 }
304
305                 /*
306                  * If required, set controls
307                  */
308                 if ( op->o_ctrls ) {
309                         if ( ldap_set_option( lsc->msc_ld, LDAP_OPT_SERVER_CONTROLS,
310                                         op->o_ctrls ) != LDAP_SUCCESS ) {
311                                 ( void )meta_clear_one_candidate( lsc, 1 );
312                                 continue;
313                         }
314                 }
315         
316                 /*
317                  * If the target is already bound it is skipped
318                  */
319                 if ( lsc->msc_bound == META_BOUND && lc->mc_bound_target == i ) {
320                         ++bound;
321                         continue;
322                 }
323
324                 /*
325                  * Otherwise an anonymous bind is performed
326                  * (note: if the target was already bound, the anonymous
327                  * bind clears the previous bind).
328                  */
329                 if ( !BER_BVISNULL( &lsc->msc_bound_ndn ) ) {
330                         ber_memfree( lsc->msc_bound_ndn.bv_val );
331                         BER_BVZERO( &lsc->msc_bound_ndn );
332                 }
333                 
334                 if ( /* FIXME: need li ... li->savecred && */ 
335                                 !BER_BVISNULL( &lsc->msc_cred ) )
336                 {
337                         /* destroy sensitive data */
338                         memset( lsc->msc_cred.bv_val, 0, lsc->msc_cred.bv_len );
339                         ber_memfree( lsc->msc_cred.bv_val );
340                         BER_BVZERO( &lsc->msc_cred );
341                 }
342
343                 rc = ldap_sasl_bind( lsc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
344                                 op->o_ctrls, NULL, &msgid );
345                 if ( rc == LDAP_SUCCESS ) {
346                         LDAPMessage     *res;
347                         struct timeval  tv = { 0, 0 };
348                         int             err;
349
350                         /*
351                          * handle response!!!
352                          */
353 retry:;
354                         switch ( ldap_result( lsc->msc_ld, msgid, 0, &tv, &res ) ) {
355                         case 0:
356                                 ldap_pvt_thread_yield();
357                                 tv.tv_sec = 0;
358                                 tv.tv_usec = 100000;    /* 0.1 s */
359                                 goto retry;
360
361                         case -1:
362                                 ldap_get_option( lsc->msc_ld, LDAP_OPT_ERROR_NUMBER,
363                                                 &rc );
364                                 break;
365
366                         default:
367                                 rc = ldap_parse_result( lsc->msc_ld, res, &err,
368                                                 NULL, NULL, NULL, NULL, 1 );
369                                 if ( rc == LDAP_SUCCESS ) {
370                                         rc = err;
371                                 }
372                                 break;
373                         }
374                 }
375
376                 if ( rc != LDAP_SUCCESS ) {
377                         Debug( LDAP_DEBUG_ANY,
378                                         "==>meta_back_dobind: (anonymous)"
379                                         " bind failed"
380                                         " with error %d (%s)\n",
381                                         rc, ldap_err2string( rc ), 0 );
382
383                         /*
384                          * null cred bind should always succeed
385                          * as anonymous, so a failure means
386                          * the target is no longer candidate possibly
387                          * due to technical reasons (remote host down?)
388                          * so better clear the handle
389                          */
390                         ( void )meta_clear_one_candidate( lsc, 1 );
391                         continue;
392                 } /* else */
393                 
394                 lsc->msc_bound = META_ANONYMOUS;
395                 ++bound;
396         }
397
398         return( bound > 0 );
399 }
400
401 /*
402  *
403  */
404 int
405 meta_back_is_valid( struct metaconn *lc, int candidate )
406 {
407         struct metasingleconn   *lsc;
408         int                     i;
409
410         assert( lc );
411
412         if ( candidate < 0 ) {
413                 return 0;
414         }
415
416         for ( i = 0, lsc = lc->mc_conns; !META_LAST( lsc ) && i < candidate; 
417                         ++i, ++lsc );
418         
419         if ( !META_LAST( lsc ) ) {
420                 return ( lsc->msc_ld != NULL );
421         }
422
423         return 0;
424 }
425
426 /*
427  * meta_back_rebind
428  *
429  * This is a callback used for chasing referrals using the same
430  * credentials as the original user on this session.
431  */
432 static int 
433 meta_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
434         ber_int_t msgid, void *params )
435 {
436         struct metasingleconn   *lsc = params;
437
438         return ldap_sasl_bind_s( ld, lsc->msc_bound_ndn.bv_val,
439                         LDAP_SASL_SIMPLE, &lsc->msc_cred,
440                         NULL, NULL, NULL );
441 }
442
443 /*
444  * FIXME: error return must be handled in a cleaner way ...
445  */
446 int
447 meta_back_op_result( struct metaconn *lc, Operation *op, SlapReply *rs )
448 {
449         int                     i,
450                                 rerr = LDAP_SUCCESS;
451         struct metasingleconn   *lsc;
452         char                    *rmsg = NULL;
453         char                    *rmatch = NULL;
454         int                     free_rmsg = 0,
455                                 free_rmatch = 0;
456
457         for ( i = 0, lsc = lc->mc_conns; !META_LAST( lsc ); ++i, ++lsc ) {
458                 char    *msg = NULL;
459                 char    *match = NULL;
460
461                 rs->sr_err = LDAP_SUCCESS;
462
463                 ldap_get_option( lsc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
464                 if ( rs->sr_err != LDAP_SUCCESS ) {
465                         /*
466                          * better check the type of error. In some cases
467                          * (search ?) it might be better to return a
468                          * success if at least one of the targets gave
469                          * positive result ...
470                          */
471                         ldap_get_option( lsc->msc_ld,
472                                         LDAP_OPT_ERROR_STRING, &msg );
473                         ldap_get_option( lsc->msc_ld,
474                                         LDAP_OPT_MATCHED_DN, &match );
475                         rs->sr_err = slap_map_api2result( rs );
476
477                         Debug(LDAP_DEBUG_ANY,
478                                         "==> meta_back_op_result: target"
479                                         " <%d> sending msg \"%s\""
480                                         " (matched \"%s\")\n", 
481                                         i, ( msg ? msg : "" ),
482                                         ( match ? match : "" ) );
483
484                         /*
485                          * FIXME: need to rewrite "match" (need rwinfo)
486                          */
487                         switch ( rs->sr_err ) {
488                         default:
489                                 rerr = rs->sr_err;
490                                 if ( rmsg ) {
491                                         ber_memfree( rmsg );
492                                 }
493                                 rmsg = msg;
494                                 free_rmsg = 1;
495                                 msg = NULL;
496                                 if ( rmatch ) {
497                                         ber_memfree( rmatch );
498                                 }
499                                 rmatch = match;
500                                 free_rmatch = 1;
501                                 match = NULL;
502                                 break;
503                         }
504
505                         /* better test the pointers before freeing? */
506                         if ( match ) {
507                                 free( match );
508                         }
509                         if ( msg ) {
510                                 free( msg );
511                         }
512                 }
513         }
514
515         rs->sr_err = rerr;
516         rs->sr_text = rmsg;
517         rs->sr_matched = rmatch;
518         send_ldap_result( op, rs );
519         if ( free_rmsg ) {
520                 ber_memfree( rmsg );
521         }
522         if ( free_rmatch ) {
523                 ber_memfree( rmatch );
524         }
525         rs->sr_text = NULL;
526         rs->sr_matched = NULL;
527
528         return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );
529 }
530