]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/bind.c
fix the problem right now
[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-2004 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 #ifdef NEW_LOGGING
58         LDAP_LOG( BACK_META, ENTRY, "meta_back_bind: dn: %s.\n",
59                         op->o_req_dn.bv_val, 0, 0 );
60 #else /* !NEW_LOGGING */
61         Debug( LDAP_DEBUG_ARGS, "meta_back_bind: dn: %s.\n%s%s",
62                         op->o_req_dn.bv_val, "", "" );
63 #endif /* !NEW_LOGGING */
64
65         if ( op->oq_bind.rb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
66                 isroot = 1;
67                 ber_dupbv( &op->oq_bind.rb_edn, be_root_dn( op->o_bd ) );
68                 op_type = META_OP_REQUIRE_ALL;
69         }
70         lc = meta_back_getconn( op, rs, op_type,
71                         &op->o_req_ndn, NULL );
72         if ( !lc ) {
73 #ifdef NEW_LOGGING
74                 LDAP_LOG( BACK_META, NOTICE,
75                                 "meta_back_bind: no target for dn %s.\n",
76                                 op->o_req_dn.bv_val, 0, 0 );
77 #else /* !NEW_LOGGING */
78                 Debug( LDAP_DEBUG_ANY,
79                                 "meta_back_bind: no target for dn %s.\n%s%s",
80                                 op->o_req_dn.bv_val, "", "");
81 #endif /* !NEW_LOGGING */
82
83                 send_ldap_result( op, rs );
84                 return -1;
85         }
86
87         /*
88          * Each target is scanned ...
89          */
90         lc->bound_target = META_BOUND_NONE;
91         ndnlen = op->o_req_ndn.bv_len;
92         for ( i = 0; i < li->ntargets; i++ ) {
93                 int             lerr;
94                 struct berval   orig_dn = op->o_req_dn;
95                 struct berval   orig_ndn = op->o_req_ndn;
96                 struct berval   orig_cred = op->oq_bind.rb_cred;
97                 int             orig_method = op->oq_bind.rb_method;
98                 
99
100                 /*
101                  * Skip non-candidates
102                  */
103                 if ( lc->conns[ i ].candidate != META_CANDIDATE ) {
104                         continue;
105                 }
106
107                 if ( gotit == 0 ) {
108                         gotit = 1;
109                 } else {
110                         /*
111                          * A bind operation is expected to have
112                          * ONE CANDIDATE ONLY!
113                          */
114 #ifdef NEW_LOGGING
115                         LDAP_LOG( BACK_META, WARNING,
116                                         "==>meta_back_bind: more than one"
117                                         " candidate is attempting to bind"
118                                         " ...\n" , 0, 0, 0 );
119 #else /* !NEW_LOGGING */
120                         Debug( LDAP_DEBUG_ANY,
121                                         "==>meta_back_bind: more than one"
122                                         " candidate is attempting to bind"
123                                         " ...\n%s%s%s", 
124                                         "", "", "" );
125 #endif /* !NEW_LOGGING */
126                 }
127
128                 if ( isroot && li->targets[ i ]->pseudorootdn.bv_val != NULL ) {
129                         op->o_req_dn = li->targets[ i ]->pseudorootdn;
130                         op->o_req_ndn = li->targets[ i ]->pseudorootdn;
131                         op->oq_bind.rb_cred = li->targets[ i ]->pseudorootpw;
132                         op->oq_bind.rb_method = LDAP_AUTH_SIMPLE;
133                 }
134                 
135                 lerr = meta_back_do_single_bind( lc, op, rs, i );
136                 if ( lerr != LDAP_SUCCESS ) {
137                         rs->sr_err = lerr;
138                         ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
139                 } else {
140                         rc = LDAP_SUCCESS;
141                 }
142
143                 op->o_req_dn = orig_dn;
144                 op->o_req_ndn = orig_ndn;
145                 op->oq_bind.rb_cred = orig_cred;
146                 op->oq_bind.rb_method = orig_method;
147         }
148
149         if ( isroot ) {
150                 lc->bound_target = META_BOUND_ALL;
151         }
152
153         /*
154          * rc is LDAP_SUCCESS if at least one bind succeeded,
155          * err is the last error that occurred during a bind;
156          * if at least (and at most?) one bind succeedes, fine.
157          */
158         if ( rc != LDAP_SUCCESS /* && rs->sr_err != LDAP_SUCCESS */ ) {
159                 
160                 /*
161                  * deal with bind failure ...
162                  */
163
164                 /*
165                  * no target was found within the naming context, 
166                  * so bind must fail with invalid credentials
167                  */
168                 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
169                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
170                 }
171
172                 rs->sr_err = ldap_back_map_result( rs );
173                 send_ldap_result( op, rs );
174                 return -1;
175         }
176
177         return 0;
178 }
179
180 /*
181  * meta_back_do_single_bind
182  *
183  * attempts to perform a bind with creds
184  */
185 static int
186 meta_back_do_single_bind(
187                 struct metaconn         *lc,
188                 Operation               *op,
189                 SlapReply               *rs,
190                 int                     candidate
191 )
192 {
193         struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
194         struct berval   mdn = { 0, NULL };
195         ber_int_t       msgid;
196         dncookie        dc;
197         struct metasingleconn   *lsc = &lc->conns[ candidate ];
198         LDAPMessage     *res;
199         
200         /*
201          * Rewrite the bind dn if needed
202          */
203         dc.rwmap = &li->targets[ candidate ]->rwmap;
204         dc.conn = op->o_conn;
205         dc.rs = rs;
206         dc.ctx = "bindDn";
207
208         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
209                 send_ldap_result( op, rs );
210                 return -1;
211         }
212
213         if ( op->o_ctrls ) {
214                 rs->sr_err = ldap_set_option( lsc->ld, 
215                                 LDAP_OPT_SERVER_CONTROLS, op->o_ctrls );
216                 if ( rs->sr_err != LDAP_SUCCESS ) {
217                         rs->sr_err = ldap_back_map_result( rs );
218                         goto return_results;
219                 }
220         }
221
222         /* FIXME: this fixes the bind problem right now; we need
223          * to use the asynchronous version to get the "matched"
224          * and more in case of failure ... */
225         rs->sr_err = ldap_sasl_bind_s(lsc->ld, mdn.bv_val,
226                         LDAP_SASL_SIMPLE, &op->oq_bind.rb_cred,
227                         op->o_ctrls, NULL, NULL);
228         if ( rs->sr_err != LDAP_SUCCESS ) {
229                 rs->sr_err = ldap_back_map_result( rs );
230                 goto return_results;
231         }
232
233         /*
234          * FIXME: handle response!!!
235          */
236         if ( lsc->bound_dn.bv_val != NULL ) {
237                 ber_memfree( lsc->bound_dn.bv_val );
238         }
239         ber_dupbv( &lsc->bound_dn, &op->o_req_dn );
240         lsc->bound = META_BOUND;
241         lc->bound_target = candidate;
242
243         if ( li->savecred ) {
244                 if ( lsc->cred.bv_val ) {
245                         memset( lsc->cred.bv_val, 0, lsc->cred.bv_len );
246                         ber_memfree( lsc->cred.bv_val );
247                 }
248                 ber_dupbv( &lsc->cred, &op->oq_bind.rb_cred );
249                 ldap_set_rebind_proc( lsc->ld, meta_back_rebind, lsc );
250         }
251
252         if ( li->cache.ttl != META_DNCACHE_DISABLED
253                         && op->o_req_ndn.bv_len != 0 ) {
254                 ( void )meta_dncache_update_entry( &li->cache,
255                                 &op->o_req_ndn, candidate );
256         }
257
258 return_results:;
259         
260         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
261                 free( mdn.bv_val );
262         }
263
264         return rs->sr_err;
265 }
266
267 /*
268  * meta_back_dobind
269  */
270 int
271 meta_back_dobind( struct metaconn *lc, Operation *op )
272 {
273         struct metasingleconn *lsc;
274         int bound = 0, i;
275
276         /*
277          * all the targets are bound as pseudoroot
278          */
279         if ( lc->bound_target == META_BOUND_ALL ) {
280                 return 1;
281         }
282
283         for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
284                 int rc;
285
286                 /*
287                  * Not a candidate or something wrong with this target ...
288                  */
289                 if ( lsc->ld == NULL ) {
290                         continue;
291                 }
292
293                 /*
294                  * If required, set controls
295                  */
296                 if ( op->o_ctrls ) {
297                         if ( ldap_set_option( lsc->ld, LDAP_OPT_SERVER_CONTROLS,
298                                         op->o_ctrls ) != LDAP_SUCCESS ) {
299                                 ( void )meta_clear_one_candidate( lsc, 1 );
300                                 continue;
301                         }
302                 }
303         
304                 /*
305                  * If the target is already bound it is skipped
306                  */
307                 if ( lsc->bound == META_BOUND && lc->bound_target == i ) {
308                         ++bound;
309                         continue;
310                 }
311
312                 /*
313                  * Otherwise an anonymous bind is performed
314                  * (note: if the target was already bound, the anonymous
315                  * bind clears the previous bind).
316                  */
317                 if ( lsc->bound_dn.bv_val ) {
318                         ber_memfree( lsc->bound_dn.bv_val );
319                         lsc->bound_dn.bv_val = NULL;
320                         lsc->bound_dn.bv_len = 0;
321                 }
322                 
323                 if ( /* FIXME: need li ... li->savecred && */ 
324                                 lsc->cred.bv_val ) {
325                         memset( lsc->cred.bv_val, 0, lsc->cred.bv_len );
326                         ber_memfree( lsc->cred.bv_val );
327                         lsc->cred.bv_val = NULL;
328                         lsc->cred.bv_len = 0;
329                 }
330
331                 rc = ldap_bind_s( lsc->ld, 0, NULL, LDAP_AUTH_SIMPLE );
332                 if ( rc != LDAP_SUCCESS ) {
333                         
334 #ifdef NEW_LOGGING
335                         LDAP_LOG( BACK_META, WARNING,
336                                         "meta_back_dobind: (anonymous)"
337                                         " bind failed"
338                                         " with error %d (%s)\n",
339                                         rc, ldap_err2string( rc ), 0 );
340 #else /* !NEW_LOGGING */
341                         Debug( LDAP_DEBUG_ANY,
342                                         "==>meta_back_dobind: (anonymous)"
343                                         " bind failed"
344                                         " with error %d (%s)\n",
345                                         rc, ldap_err2string( rc ), 0 );
346 #endif /* !NEW_LOGGING */
347
348                         /*
349                          * null cred bind should always succeed
350                          * as anonymous, so a failure means
351                          * the target is no longer candidate possibly
352                          * due to technical reasons (remote host down?)
353                          * so better clear the handle
354                          */
355                         ( void )meta_clear_one_candidate( lsc, 1 );
356                         continue;
357                 } /* else */
358                 
359                 lsc->bound = META_ANONYMOUS;
360                 ++bound;
361         }
362
363         return( bound > 0 );
364 }
365
366 /*
367  *
368  */
369 int
370 meta_back_is_valid( struct metaconn *lc, int candidate )
371 {
372         struct metasingleconn   *lsc;
373         int                     i;
374
375         assert( lc );
376
377         if ( candidate < 0 ) {
378                 return 0;
379         }
380
381         for ( i = 0, lsc = lc->conns; !META_LAST(lsc) && i < candidate; 
382                         ++i, ++lsc );
383         
384         if ( !META_LAST(lsc) ) {
385                 return( lsc->ld != NULL );
386         }
387
388         return 0;
389 }
390
391 /*
392  * meta_back_rebind
393  *
394  * This is a callback used for chasing referrals using the same
395  * credentials as the original user on this session.
396  */
397 static int 
398 meta_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
399         ber_int_t msgid, void *params )
400 {
401         struct metasingleconn *lc = params;
402
403         return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val,
404                         LDAP_AUTH_SIMPLE );
405 }
406
407 /*
408  * FIXME: error return must be handled in a cleaner way ...
409  */
410 int
411 meta_back_op_result( struct metaconn *lc, Operation *op, SlapReply *rs )
412 {
413         int i, rerr = LDAP_SUCCESS;
414         struct metasingleconn *lsc;
415         char *rmsg = NULL;
416         char *rmatch = NULL;
417
418         for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
419                 char *msg = NULL;
420                 char *match = NULL;
421
422                 rs->sr_err = LDAP_SUCCESS;
423
424                 ldap_get_option( lsc->ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
425                 if ( rs->sr_err != LDAP_SUCCESS ) {
426                         /*
427                          * better check the type of error. In some cases
428                          * (search ?) it might be better to return a
429                          * success if at least one of the targets gave
430                          * positive result ...
431                          */
432                         ldap_get_option( lsc->ld,
433                                         LDAP_OPT_ERROR_STRING, &msg );
434                         ldap_get_option( lsc->ld,
435                                         LDAP_OPT_MATCHED_DN, &match );
436                         rs->sr_err = ldap_back_map_result( rs );
437
438 #ifdef NEW_LOGGING
439                         LDAP_LOG( BACK_META, RESULTS,
440                                         "meta_back_op_result: target"
441                                         " <%d> sending msg \"%s\""
442                                         " (matched \"%s\")\n",
443                                         i, ( msg ? msg : "" ),
444                                         ( match ? match : "" ) );
445 #else /* !NEW_LOGGING */
446                         Debug(LDAP_DEBUG_ANY,
447                                         "==> meta_back_op_result: target"
448                                         " <%d> sending msg \"%s\""
449                                         " (matched \"%s\")\n", 
450                                         i, ( msg ? msg : "" ),
451                                         ( match ? match : "" ) );
452 #endif /* !NEW_LOGGING */
453
454                         /*
455                          * FIXME: need to rewrite "match" (need rwinfo)
456                          */
457                         switch ( rs->sr_err ) {
458                         default:
459                                 rerr = rs->sr_err;
460                                 rmsg = msg;
461                                 msg = NULL;
462                                 rmatch = match;
463                                 match = NULL;
464                                 break;
465                         }
466
467                         /* better test the pointers before freeing? */
468                         if ( match ) {
469                                 free( match );
470                         }
471                         if ( msg ) {
472                                 free( msg );
473                         }
474                 }
475         }
476
477         rs->sr_err = rerr;
478         rs->sr_text = rmsg;
479         rs->sr_matched = rmatch;
480         send_ldap_result( op, rs );
481         rs->sr_text = NULL;
482         rs->sr_matched = NULL;
483
484         return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );
485 }
486