2 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5 * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
7 * This work has been developed to fulfill the requirements
8 * of SysNet s.n.c. <http:www.sys-net.it> and it has been donated
9 * to the OpenLDAP Foundation in the hope that it may be useful
10 * to the Open Source community, but WITHOUT ANY WARRANTY.
12 * Permission is granted to anyone to use this software for any purpose
13 * on any computer system, and to alter it and redistribute it, subject
14 * to the following restrictions:
16 * 1. The author and SysNet s.n.c. are not responsible for the consequences
17 * of use of this software, no matter how awful, even if they arise from
20 * 2. The origin of this software must not be misrepresented, either by
21 * explicit claim or by omission. Since few users ever read sources,
22 * credits should appear in the documentation.
24 * 3. Altered versions must be plainly marked as such, and must not be
25 * misrepresented as being the original software. Since few users
26 * ever read sources, credits should appear in the documentation.
27 * SysNet s.n.c. cannot be responsible for the consequences of the
30 * 4. This notice may not be removed or altered.
33 * This software is based on the backend back-ldap, implemented
34 * by Howard Chu <hyc@highlandsun.com>, and modified by Mark Valence
35 * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
36 * contributors. The contribution of the original software to the present
37 * implementation is acknowledged in this copyright statement.
39 * A special acknowledgement goes to Howard for the overall architecture
40 * (and for borrowing large pieces of code), and to Mark, who implemented
41 * from scratch the attribute/objectclass mapping.
43 * The original copyright statement follows.
45 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
47 * Permission is granted to anyone to use this software for any purpose
48 * on any computer system, and to alter it and redistribute it, subject
49 * to the following restrictions:
51 * 1. The author is not responsible for the consequences of use of this
52 * software, no matter how awful, even if they arise from flaws in it.
54 * 2. The origin of this software must not be misrepresented, either by
55 * explicit claim or by omission. Since few users ever read sources,
56 * credits should appear in the documentation.
58 * 3. Altered versions must be plainly marked as such, and must not be
59 * misrepresented as being the original software. Since few users
60 * ever read sources, credits should appear in the
63 * 4. This notice may not be removed or altered.
71 #include <ac/socket.h>
72 #include <ac/string.h>
77 #include "../back-ldap/back-ldap.h"
78 #include "back-meta.h"
80 static LDAP_REBIND_PROC meta_back_rebind;
83 meta_back_do_single_bind(
91 meta_back_bind( Operation *op, SlapReply *rs )
93 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
96 int rc = -1, i, gotit = 0, ndnlen, isroot = 0;
97 int op_type = META_OP_ALLOW_MULTIPLE;
99 rs->sr_err = LDAP_SUCCESS;
102 LDAP_LOG( BACK_META, ENTRY, "meta_back_bind: dn: %s.\n",
103 op->o_req_dn.bv_val, 0, 0 );
104 #else /* !NEW_LOGGING */
105 Debug( LDAP_DEBUG_ARGS, "meta_back_bind: dn: %s.\n%s%s",
106 op->o_req_dn.bv_val, "", "" );
107 #endif /* !NEW_LOGGING */
109 if ( op->oq_bind.rb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
111 ber_dupbv( &op->oq_bind.rb_edn, be_root_dn( op->o_bd ) );
112 op_type = META_OP_REQUIRE_ALL;
114 lc = meta_back_getconn( op, rs, op_type,
115 &op->o_req_ndn, NULL );
118 LDAP_LOG( BACK_META, NOTICE,
119 "meta_back_bind: no target for dn %s.\n", dn->bv_val, 0, 0 );
120 #else /* !NEW_LOGGING */
121 Debug( LDAP_DEBUG_ANY,
122 "meta_back_bind: no target for dn %s.\n%s%s",
123 op->o_req_dn.bv_val, "", "");
124 #endif /* !NEW_LOGGING */
126 send_ldap_result( op, rs );
131 * Each target is scanned ...
133 lc->bound_target = META_BOUND_NONE;
134 ndnlen = op->o_req_ndn.bv_len;
135 for ( i = 0; i < li->ntargets; i++ ) {
137 struct berval orig_dn = op->o_req_dn;
138 struct berval orig_ndn = op->o_req_ndn;
139 struct berval orig_cred = op->oq_bind.rb_cred;
140 int orig_method = op->oq_bind.rb_method;
144 * Skip non-candidates
146 if ( lc->conns[ i ].candidate != META_CANDIDATE ) {
154 * A bind operation is expected to have
155 * ONE CANDIDATE ONLY!
158 LDAP_LOG( BACK_META, WARNING,
159 "==>meta_back_bind: more than one"
160 " candidate is attempting to bind"
161 " ...\n" , 0, 0, 0 );
162 #else /* !NEW_LOGGING */
163 Debug( LDAP_DEBUG_ANY,
164 "==>meta_back_bind: more than one"
165 " candidate is attempting to bind"
168 #endif /* !NEW_LOGGING */
171 if ( isroot && li->targets[ i ]->pseudorootdn.bv_val != NULL ) {
172 op->o_req_dn = li->targets[ i ]->pseudorootdn;
173 op->o_req_ndn = li->targets[ i ]->pseudorootdn;
174 op->oq_bind.rb_cred = li->targets[ i ]->pseudorootpw;
175 op->oq_bind.rb_method = LDAP_AUTH_SIMPLE;
178 lerr = meta_back_do_single_bind( lc, op, rs, i );
179 if ( lerr != LDAP_SUCCESS ) {
181 ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
186 op->o_req_dn = orig_dn;
187 op->o_req_ndn = orig_ndn;
188 op->oq_bind.rb_cred = orig_cred;
189 op->oq_bind.rb_method = orig_method;
193 lc->bound_target = META_BOUND_ALL;
197 * rc is LDAP_SUCCESS if at least one bind succeeded,
198 * err is the last error that occurred during a bind;
199 * if at least (and at most?) one bind succeedes, fine.
201 if ( rc != LDAP_SUCCESS /* && rs->sr_err != LDAP_SUCCESS */ ) {
204 * deal with bind failure ...
208 * no target was found within the naming context,
209 * so bind must fail with invalid credentials
211 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
212 rs->sr_err = LDAP_INVALID_CREDENTIALS;
215 rs->sr_err = ldap_back_map_result( rs );
216 send_ldap_result( op, rs );
224 * meta_back_do_single_bind
226 * attempts to perform a bind with creds
229 meta_back_do_single_bind(
236 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
237 struct berval mdn = { 0, NULL };
240 struct metasingleconn *lsc = &lc->conns[ candidate ];
243 * Rewrite the bind dn if needed
245 dc.rwmap = &li->targets[ candidate ]->rwmap;
246 dc.conn = op->o_conn;
250 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
251 send_ldap_result( op, rs );
256 rs->sr_err = ldap_set_option( lsc->ld,
257 LDAP_OPT_SERVER_CONTROLS, op->o_ctrls );
258 if ( rs->sr_err != LDAP_SUCCESS ) {
259 rs->sr_err = ldap_back_map_result( rs );
264 rs->sr_err = ldap_sasl_bind(lsc->ld, mdn.bv_val,
265 LDAP_SASL_SIMPLE, &op->oq_bind.rb_cred,
266 op->o_ctrls, NULL, &msgid);
267 if ( rs->sr_err != LDAP_SUCCESS ) {
268 rs->sr_err = ldap_back_map_result( rs );
272 * FIXME: handle response!!!
274 if ( lsc->bound_dn.bv_val != NULL ) {
275 ber_memfree( lsc->bound_dn.bv_val );
277 ber_dupbv( &lsc->bound_dn, &op->o_req_dn );
278 lsc->bound = META_BOUND;
279 lc->bound_target = candidate;
281 if ( li->savecred ) {
282 if ( lsc->cred.bv_val ) {
283 memset( lsc->cred.bv_val, 0, lsc->cred.bv_len );
284 ber_memfree( lsc->cred.bv_val );
286 ber_dupbv( &lsc->cred, &op->oq_bind.rb_cred );
287 ldap_set_rebind_proc( lsc->ld, meta_back_rebind, lsc );
290 if ( li->cache.ttl != META_DNCACHE_DISABLED
291 && op->o_req_ndn.bv_len != 0 ) {
292 ( void )meta_dncache_update_entry( &li->cache,
293 &op->o_req_ndn, candidate );
299 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
310 meta_back_dobind( struct metaconn *lc, Operation *op )
312 struct metasingleconn *lsc;
316 * all the targets are bound as pseudoroot
318 if ( lc->bound_target == META_BOUND_ALL ) {
322 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
326 * Not a candidate or something wrong with this target ...
328 if ( lsc->ld == NULL ) {
333 * If required, set controls
336 if ( ldap_set_option( lsc->ld, LDAP_OPT_SERVER_CONTROLS,
337 op->o_ctrls ) != LDAP_SUCCESS ) {
338 ( void )meta_clear_one_candidate( lsc, 1 );
344 * If the target is already bound it is skipped
346 if ( lsc->bound == META_BOUND && lc->bound_target == i ) {
352 * Otherwise an anonymous bind is performed
353 * (note: if the target was already bound, the anonymous
354 * bind clears the previous bind).
356 if ( lsc->bound_dn.bv_val ) {
357 ber_memfree( lsc->bound_dn.bv_val );
358 lsc->bound_dn.bv_val = NULL;
359 lsc->bound_dn.bv_len = 0;
362 if ( /* FIXME: need li ... li->savecred && */
364 memset( lsc->cred.bv_val, 0, lsc->cred.bv_len );
365 ber_memfree( lsc->cred.bv_val );
366 lsc->cred.bv_val = NULL;
367 lsc->cred.bv_len = 0;
370 rc = ldap_bind_s( lsc->ld, 0, NULL, LDAP_AUTH_SIMPLE );
371 if ( rc != LDAP_SUCCESS ) {
374 LDAP_LOG( BACK_META, WARNING,
375 "meta_back_dobind: (anonymous)"
377 " with error %d (%s)\n",
378 rc, ldap_err2string( rc ), 0 );
379 #else /* !NEW_LOGGING */
380 Debug( LDAP_DEBUG_ANY,
381 "==>meta_back_dobind: (anonymous)"
383 " with error %d (%s)\n",
384 rc, ldap_err2string( rc ), 0 );
385 #endif /* !NEW_LOGGING */
388 * null cred bind should always succeed
389 * as anonymous, so a failure means
390 * the target is no longer candidate possibly
391 * due to technical reasons (remote host down?)
392 * so better clear the handle
394 ( void )meta_clear_one_candidate( lsc, 1 );
398 lsc->bound = META_ANONYMOUS;
409 meta_back_is_valid( struct metaconn *lc, int candidate )
411 struct metasingleconn *lsc;
416 if ( candidate < 0 ) {
420 for ( i = 0, lsc = lc->conns; !META_LAST(lsc) && i < candidate;
423 if ( !META_LAST(lsc) ) {
424 return( lsc->ld != NULL );
433 * This is a callback used for chasing referrals using the same
434 * credentials as the original user on this session.
437 meta_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
438 ber_int_t msgid, void *params )
440 struct metasingleconn *lc = params;
442 return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val,
447 * FIXME: error return must be handled in a cleaner way ...
450 meta_back_op_result( struct metaconn *lc, Operation *op, SlapReply *rs )
452 int i, rerr = LDAP_SUCCESS;
453 struct metasingleconn *lsc;
457 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
461 rs->sr_err = LDAP_SUCCESS;
463 ldap_get_option( lsc->ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
464 if ( rs->sr_err != LDAP_SUCCESS ) {
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 ...
471 ldap_get_option( lsc->ld,
472 LDAP_OPT_ERROR_STRING, &msg );
473 ldap_get_option( lsc->ld,
474 LDAP_OPT_MATCHED_DN, &match );
475 rs->sr_err = ldap_back_map_result( rs );
478 LDAP_LOG( BACK_META, RESULTS,
479 "meta_back_op_result: target"
480 " <%d> sending msg \"%s\""
481 " (matched \"%s\")\n",
482 i, ( msg ? msg : "" ),
483 ( match ? match : "" ) );
484 #else /* !NEW_LOGGING */
485 Debug(LDAP_DEBUG_ANY,
486 "==> meta_back_op_result: target"
487 " <%d> sending msg \"%s\""
488 " (matched \"%s\")\n",
489 i, ( msg ? msg : "" ),
490 ( match ? match : "" ) );
491 #endif /* !NEW_LOGGING */
494 * FIXME: need to rewrite "match" (need rwinfo)
496 switch ( rs->sr_err ) {
506 /* better test the pointers before freeing? */
518 rs->sr_matched = rmatch;
519 send_ldap_result( op, rs );
521 rs->sr_matched = NULL;
523 return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );