]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/bind.c
Happy New Year!
[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->oq_bind.rb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
61                 isroot = 1;
62                 ber_dupbv( &op->oq_bind.rb_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->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->oq_bind.rb_cred;
86                 int             orig_method = op->oq_bind.rb_method;
87                 
88
89                 /*
90                  * Skip non-candidates
91                  */
92                 if ( lc->conns[ i ].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 ]->pseudorootdn.bv_val != NULL ) {
111                         op->o_req_dn = li->targets[ i ]->pseudorootdn;
112                         op->o_req_ndn = li->targets[ i ]->pseudorootdn;
113                         op->oq_bind.rb_cred = li->targets[ i ]->pseudorootpw;
114                         op->oq_bind.rb_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->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->oq_bind.rb_cred = orig_cred;
128                 op->oq_bind.rb_method = orig_method;
129         }
130
131         if ( isroot ) {
132                 lc->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         ber_int_t       msgid;
178         dncookie        dc;
179         struct metasingleconn   *lsc = &lc->conns[ candidate ];
180         LDAPMessage     *res;
181         
182         /*
183          * Rewrite the bind dn if needed
184          */
185         dc.rwmap = &li->targets[ candidate ]->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->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_s(lsc->ld, mdn.bv_val,
208                         LDAP_SASL_SIMPLE, &op->oq_bind.rb_cred,
209                         op->o_ctrls, NULL, NULL);
210         if ( rs->sr_err != LDAP_SUCCESS ) {
211                 rs->sr_err = slap_map_api2result( rs );
212                 goto return_results;
213         }
214
215         /*
216          * FIXME: handle response!!!
217          */
218         if ( lsc->bound_dn.bv_val != NULL ) {
219                 ber_memfree( lsc->bound_dn.bv_val );
220         }
221         ber_dupbv( &lsc->bound_dn, &op->o_req_dn );
222         lsc->bound = META_BOUND;
223         lc->bound_target = candidate;
224
225         if ( li->savecred ) {
226                 if ( lsc->cred.bv_val ) {
227                         memset( lsc->cred.bv_val, 0, lsc->cred.bv_len );
228                         ber_memfree( lsc->cred.bv_val );
229                 }
230                 ber_dupbv( &lsc->cred, &op->oq_bind.rb_cred );
231                 ldap_set_rebind_proc( lsc->ld, meta_back_rebind, lsc );
232         }
233
234         if ( li->cache.ttl != META_DNCACHE_DISABLED
235                         && op->o_req_ndn.bv_len != 0 ) {
236                 ( void )meta_dncache_update_entry( &li->cache,
237                                 &op->o_req_ndn, candidate );
238         }
239
240 return_results:;
241         
242         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
243                 free( mdn.bv_val );
244         }
245
246         return rs->sr_err;
247 }
248
249 /*
250  * meta_back_dobind
251  */
252 int
253 meta_back_dobind( struct metaconn *lc, Operation *op )
254 {
255         struct metasingleconn *lsc;
256         int bound = 0, i;
257
258         /*
259          * all the targets are bound as pseudoroot
260          */
261         if ( lc->bound_target == META_BOUND_ALL ) {
262                 return 1;
263         }
264
265         for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
266                 int             rc;
267                 struct berval   cred = BER_BVC("");
268
269                 /*
270                  * Not a candidate or something wrong with this target ...
271                  */
272                 if ( lsc->ld == NULL ) {
273                         continue;
274                 }
275
276                 /*
277                  * If required, set controls
278                  */
279                 if ( op->o_ctrls ) {
280                         if ( ldap_set_option( lsc->ld, LDAP_OPT_SERVER_CONTROLS,
281                                         op->o_ctrls ) != LDAP_SUCCESS ) {
282                                 ( void )meta_clear_one_candidate( lsc, 1 );
283                                 continue;
284                         }
285                 }
286         
287                 /*
288                  * If the target is already bound it is skipped
289                  */
290                 if ( lsc->bound == META_BOUND && lc->bound_target == i ) {
291                         ++bound;
292                         continue;
293                 }
294
295                 /*
296                  * Otherwise an anonymous bind is performed
297                  * (note: if the target was already bound, the anonymous
298                  * bind clears the previous bind).
299                  */
300                 if ( lsc->bound_dn.bv_val ) {
301                         ber_memfree( lsc->bound_dn.bv_val );
302                         lsc->bound_dn.bv_val = NULL;
303                         lsc->bound_dn.bv_len = 0;
304                 }
305                 
306                 if ( /* FIXME: need li ... li->savecred && */ 
307                                 lsc->cred.bv_val ) {
308                         memset( lsc->cred.bv_val, 0, lsc->cred.bv_len );
309                         ber_memfree( lsc->cred.bv_val );
310                         lsc->cred.bv_val = NULL;
311                         lsc->cred.bv_len = 0;
312                 }
313
314                 rc = ldap_sasl_bind_s(lsc->ld, "", LDAP_SASL_SIMPLE, &cred,
315                                 op->o_ctrls, NULL, NULL);
316                 if ( rc != LDAP_SUCCESS ) {
317                         
318                         Debug( LDAP_DEBUG_ANY,
319                                         "==>meta_back_dobind: (anonymous)"
320                                         " bind failed"
321                                         " with error %d (%s)\n",
322                                         rc, ldap_err2string( rc ), 0 );
323
324                         /*
325                          * null cred bind should always succeed
326                          * as anonymous, so a failure means
327                          * the target is no longer candidate possibly
328                          * due to technical reasons (remote host down?)
329                          * so better clear the handle
330                          */
331                         ( void )meta_clear_one_candidate( lsc, 1 );
332                         continue;
333                 } /* else */
334                 
335                 lsc->bound = META_ANONYMOUS;
336                 ++bound;
337         }
338
339         return( bound > 0 );
340 }
341
342 /*
343  *
344  */
345 int
346 meta_back_is_valid( struct metaconn *lc, int candidate )
347 {
348         struct metasingleconn   *lsc;
349         int                     i;
350
351         assert( lc );
352
353         if ( candidate < 0 ) {
354                 return 0;
355         }
356
357         for ( i = 0, lsc = lc->conns; !META_LAST(lsc) && i < candidate; 
358                         ++i, ++lsc );
359         
360         if ( !META_LAST(lsc) ) {
361                 return( lsc->ld != NULL );
362         }
363
364         return 0;
365 }
366
367 /*
368  * meta_back_rebind
369  *
370  * This is a callback used for chasing referrals using the same
371  * credentials as the original user on this session.
372  */
373 static int 
374 meta_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
375         ber_int_t msgid, void *params )
376 {
377         struct metasingleconn *lc = params;
378
379         return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val,
380                         LDAP_AUTH_SIMPLE );
381 }
382
383 /*
384  * FIXME: error return must be handled in a cleaner way ...
385  */
386 int
387 meta_back_op_result( struct metaconn *lc, Operation *op, SlapReply *rs )
388 {
389         int i, rerr = LDAP_SUCCESS;
390         struct metasingleconn *lsc;
391         char *rmsg = NULL;
392         char *rmatch = NULL;
393         int     free_rmsg = 0, free_rmatch = 0;
394
395         for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
396                 char *msg = NULL;
397                 char *match = NULL;
398
399                 rs->sr_err = LDAP_SUCCESS;
400
401                 ldap_get_option( lsc->ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
402                 if ( rs->sr_err != LDAP_SUCCESS ) {
403                         /*
404                          * better check the type of error. In some cases
405                          * (search ?) it might be better to return a
406                          * success if at least one of the targets gave
407                          * positive result ...
408                          */
409                         ldap_get_option( lsc->ld,
410                                         LDAP_OPT_ERROR_STRING, &msg );
411                         ldap_get_option( lsc->ld,
412                                         LDAP_OPT_MATCHED_DN, &match );
413                         rs->sr_err = slap_map_api2result( rs );
414
415                         Debug(LDAP_DEBUG_ANY,
416                                         "==> meta_back_op_result: target"
417                                         " <%d> sending msg \"%s\""
418                                         " (matched \"%s\")\n", 
419                                         i, ( msg ? msg : "" ),
420                                         ( match ? match : "" ) );
421
422                         /*
423                          * FIXME: need to rewrite "match" (need rwinfo)
424                          */
425                         switch ( rs->sr_err ) {
426                         default:
427                                 rerr = rs->sr_err;
428                                 if ( rmsg ) {
429                                         ber_memfree( rmsg );
430                                 }
431                                 rmsg = msg;
432                                 free_rmsg = 1;
433                                 msg = NULL;
434                                 if ( rmatch ) {
435                                         ber_memfree( rmatch );
436                                 }
437                                 rmatch = match;
438                                 free_rmatch = 1;
439                                 match = NULL;
440                                 break;
441                         }
442
443                         /* better test the pointers before freeing? */
444                         if ( match ) {
445                                 free( match );
446                         }
447                         if ( msg ) {
448                                 free( msg );
449                         }
450                 }
451         }
452
453         rs->sr_err = rerr;
454         rs->sr_text = rmsg;
455         rs->sr_matched = rmatch;
456         send_ldap_result( op, rs );
457         if ( free_rmsg ) {
458                 ber_memfree( rmsg );
459         }
460         if ( free_rmatch ) {
461                 ber_memfree( rmatch );
462         }
463         rs->sr_text = NULL;
464         rs->sr_matched = NULL;
465
466         return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );
467 }
468