2 * Copyright 1998-2002 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"
81 meta_back_do_single_bind(
104 struct metainfo *li = ( struct metainfo * )be->be_private;
107 int rc = -1, i, gotit = 0, ndnlen, isroot = 0;
108 int op_type = META_OP_ALLOW_MULTIPLE;
109 int err = LDAP_SUCCESS;
111 struct berval *realdn = dn;
112 struct berval *realndn = ndn;
113 struct berval *realcred = cred;
114 int realmethod = method;
117 LDAP_LOG( BACK_META, ENTRY,
118 "meta_back_bind: dn: %s.\n", dn->bv_val, 0, 0 );
119 #else /* !NEW_LOGGING */
120 Debug( LDAP_DEBUG_ARGS, "meta_back_bind: dn: %s.\n%s%s", dn->bv_val, "", "" );
121 #endif /* !NEW_LOGGING */
123 if ( method == LDAP_AUTH_SIMPLE
124 && be_isroot_pw( be, conn, ndn, cred ) ) {
126 ber_dupbv( edn, be_root_dn( be ) );
127 op_type = META_OP_REQUIRE_ALL;
129 lc = meta_back_getconn( li, conn, op, op_type, ndn, NULL );
132 LDAP_LOG( BACK_META, NOTICE,
133 "meta_back_bind: no target for dn %s.\n", dn->bv_val, 0, 0 );
134 #else /* !NEW_LOGGING */
135 Debug( LDAP_DEBUG_ANY,
136 "meta_back_bind: no target for dn %s.\n%s%s",
138 #endif /* !NEW_LOGGING */
139 send_ldap_result( conn, op, LDAP_OTHER,
140 NULL, NULL, NULL, NULL );
145 * Each target is scanned ...
147 lc->bound_target = META_BOUND_NONE;
148 ndnlen = ndn->bv_len;
149 for ( i = 0; i < li->ntargets; i++ ) {
153 * Skip non-candidates
155 if ( lc->conns[ i ].candidate != META_CANDIDATE ) {
163 * A bind operation is expected to have
164 * ONE CANDIDATE ONLY!
167 LDAP_LOG( BACK_META, WARNING,
168 "==>meta_back_bind: more than one"
169 " candidate is attempting to bind"
170 " ...\n" , 0, 0, 0 );
171 #else /* !NEW_LOGGING */
172 Debug( LDAP_DEBUG_ANY,
173 "==>meta_back_bind: more than one"
174 " candidate is attempting to bind"
177 #endif /* !NEW_LOGGING */
180 if ( isroot && li->targets[ i ]->pseudorootdn.bv_val != NULL ) {
181 realdn = &li->targets[ i ]->pseudorootdn;
182 realndn = &li->targets[ i ]->pseudorootdn;
183 realcred = &li->targets[ i ]->pseudorootpw;
184 realmethod = LDAP_AUTH_SIMPLE;
192 lerr = meta_back_do_single_bind( li, lc, op,
193 realdn, realndn, realcred, realmethod, i );
194 if ( lerr != LDAP_SUCCESS ) {
196 ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
203 lc->bound_target = META_BOUND_ALL;
207 * rc is LDAP_SUCCESS if at least one bind succeeded,
208 * err is the last error that occurred during a bind;
209 * if at least (and at most?) one bind succeedes, fine.
211 if ( rc != LDAP_SUCCESS /* && err != LDAP_SUCCESS */ ) {
214 * deal with bind failure ...
218 * no target was found within the naming context,
219 * so bind must fail with invalid credentials
221 if ( err == LDAP_SUCCESS && gotit == 0 ) {
222 err = LDAP_INVALID_CREDENTIALS;
225 err = ldap_back_map_result( err );
226 send_ldap_result( conn, op, err, NULL, NULL, NULL, NULL );
234 * meta_back_do_single_bind
236 * attempts to perform a bind with creds
239 meta_back_do_single_bind(
250 struct berval mdn = { 0, NULL };
254 * Rewrite the bind dn if needed
256 switch ( rewrite_session( li->targets[ candidate ]->rwinfo,
257 "bindDn", dn->bv_val, lc->conn, &mdn.bv_val ) ) {
258 case REWRITE_REGEXEC_OK:
259 if ( mdn.bv_val == NULL ) {
263 LDAP_LOG( BACK_META, DETAIL1,
264 "[rw] bindDn: \"%s\" -> \"%s\"\n", dn->bv_val, mdn.bv_val, 0 );
265 #else /* !NEW_LOGGING */
266 Debug( LDAP_DEBUG_ARGS,
267 "rw> bindDn: \"%s\" -> \"%s\"\n%s",
268 dn->bv_val, mdn.bv_val, "" );
269 #endif /* !NEW_LOGGING */
272 case REWRITE_REGEXEC_UNWILLING:
273 return LDAP_UNWILLING_TO_PERFORM;
275 case REWRITE_REGEXEC_ERR:
280 ldap_set_option( lc->conns[ candidate ].ld,
281 LDAP_OPT_SERVER_CONTROLS, op->o_ctrls );
284 rc = ldap_bind_s( lc->conns[ candidate ].ld, mdn.bv_val, cred->bv_val, method );
285 if ( rc != LDAP_SUCCESS ) {
286 rc = ldap_back_map_result( rc );
288 ber_dupbv( &lc->conns[ candidate ].bound_dn, dn );
289 lc->conns[ candidate ].bound = META_BOUND;
290 lc->bound_target = candidate;
292 if ( li->cache.ttl != META_DNCACHE_DISABLED
293 && ndn->bv_len != 0 ) {
294 ( void )meta_dncache_update_entry( &li->cache,
299 if ( mdn.bv_val != 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 the target is already bound it is skipped
335 if ( lsc->bound == META_BOUND && lc->bound_target == i ) {
341 * Otherwise an anonymous bind is performed
342 * (note: if the target was already bound, the anonymous
343 * bind clears the previous bind).
345 if ( lsc->bound_dn.bv_val ) {
346 ch_free( lsc->bound_dn.bv_val );
347 lsc->bound_dn.bv_val = NULL;
348 lsc->bound_dn.bv_len = 0;
352 ldap_set_option( lsc->ld, LDAP_OPT_SERVER_CONTROLS,
356 rc = ldap_bind_s( lsc->ld, 0, NULL, LDAP_AUTH_SIMPLE );
357 if ( rc != LDAP_SUCCESS ) {
360 LDAP_LOG( BACK_META, WARNING,
361 "meta_back_dobind: (anonymous)"
362 " bind as \"%s\" failed"
363 " with error \"%s\"\n",
364 lsc->bound_dn.bv_val,
365 ldap_err2string( rc ), 0 );
366 #else /* !NEW_LOGGING */
367 Debug( LDAP_DEBUG_ANY,
368 "==>meta_back_dobind: (anonymous)"
369 " bind as \"%s\" failed"
370 " with error \"%s\"\n%s",
371 lsc->bound_dn.bv_val,
372 ldap_err2string( rc ), "" );
373 #endif /* !NEW_LOGGING */
376 * null cred bind should always succeed
377 * as anonymous, so a failure means
378 * the target is no longer candidate possibly
379 * due to technical reasons (remote host down?)
380 * so better clear the handle
382 ( void )meta_clear_one_candidate( lsc, 1 );
386 lsc->bound = META_ANONYMOUS;
397 meta_back_is_valid( struct metaconn *lc, int candidate )
399 struct metasingleconn *lsc;
404 if ( candidate < 0 ) {
408 for ( i = 0, lsc = lc->conns; !META_LAST(lsc) && i < candidate;
411 if ( !META_LAST(lsc) ) {
412 return( lsc->ld != NULL );
419 * FIXME: error return must be handled in a cleaner way ...
422 meta_back_op_result( struct metaconn *lc, Operation *op )
424 int i, rerr = LDAP_SUCCESS;
425 struct metasingleconn *lsc;
429 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
430 int err = LDAP_SUCCESS;
434 ldap_get_option( lsc->ld, LDAP_OPT_ERROR_NUMBER, &err );
435 if ( err != LDAP_SUCCESS ) {
437 * better check the type of error. In some cases
438 * (search ?) it might be better to return a
439 * success if at least one of the targets gave
440 * positive result ...
442 ldap_get_option( lsc->ld,
443 LDAP_OPT_ERROR_STRING, &msg );
444 ldap_get_option( lsc->ld,
445 LDAP_OPT_MATCHED_DN, &match );
446 err = ldap_back_map_result( err );
449 LDAP_LOG( BACK_META, RESULTS,
450 "meta_back_op_result: target"
451 " <%d> sending msg \"%s\""
452 " (matched \"%s\")\n",
453 i, ( msg ? msg : "" ),
454 ( match ? match : "" ) );
455 #else /* !NEW_LOGGING */
456 Debug(LDAP_DEBUG_ANY,
457 "==> meta_back_op_result: target"
458 " <%d> sending msg \"%s\""
459 " (matched \"%s\")\n",
460 i, ( msg ? msg : "" ),
461 ( match ? match : "" ) );
462 #endif /* !NEW_LOGGING */
465 * FIXME: need to rewrite "match" (need rwinfo)
477 /* better test the pointers before freeing? */
487 send_ldap_result( lc->conn, op, rerr, rmatch, rmsg, NULL, NULL );
489 return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );