2 * Copyright 1998-2001 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"
92 struct metainfo *li = ( struct metainfo * )be->be_private;
95 int rc = -1, i, gotit = 0, ndnlen, isroot = 0;
96 int op_type = META_OP_ALLOW_MULTIPLE;
97 int err = LDAP_SUCCESS;
99 struct berval *realdn = dn;
100 struct berval *realndn = ndn;
101 struct berval *realcred = cred;
102 int realmethod = method;
105 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
106 "meta_back_bind: dn: %s.\n", dn->bv_val ));
107 #else /* !NEW_LOGGING */
108 Debug( LDAP_DEBUG_ARGS, "meta_back_bind: dn: %s.\n%s%s", dn->bv_val, "", "" );
109 #endif /* !NEW_LOGGING */
111 if ( method == LDAP_AUTH_SIMPLE
112 && be_isroot_pw( be, conn, ndn, cred ) ) {
114 ber_dupbv( edn, be_root_dn( be ) );
115 op_type = META_OP_REQUIRE_ALL;
117 lc = meta_back_getconn( li, conn, op, op_type, ndn, NULL );
120 LDAP_LOG(( "backend", LDAP_LEVEL_NOTICE,
121 "meta_back_bind: no target for dn %s.\n",
123 #else /* !NEW_LOGGING */
124 Debug( LDAP_DEBUG_ANY,
125 "meta_back_bind: no target for dn %s.\n%s%s",
127 #endif /* !NEW_LOGGING */
128 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
129 NULL, NULL, NULL, NULL );
134 * Each target is scanned ...
136 lc->bound_target = META_BOUND_NONE;
137 ndnlen = ndn->bv_len;
138 for ( i = 0; i < li->ntargets; i++ ) {
142 * Skip non-candidates
144 if ( lc->conns[ i ]->candidate != META_CANDIDATE ) {
152 * A bind operation is expected to have
153 * ONE CANDIDATE ONLY!
156 LDAP_LOG(( "backend", LDAP_LEVEL_WARNING,
157 "==>meta_back_bind: more than one"
158 " candidate is attempting to bind"
160 #else /* !NEW_LOGGING */
161 Debug( LDAP_DEBUG_ANY,
162 "==>meta_back_bind: more than one"
163 " candidate is attempting to bind"
166 #endif /* !NEW_LOGGING */
169 if ( isroot && li->targets[ i ]->pseudorootdn.bv_val != NULL ) {
170 realdn = &li->targets[ i ]->pseudorootdn;
171 realndn = &li->targets[ i ]->pseudorootdn;
172 realcred = &li->targets[ i ]->pseudorootpw;
173 realmethod = LDAP_AUTH_SIMPLE;
176 lerr = meta_back_do_single_bind( li, lc,
177 realdn, realndn, realcred, realmethod, i );
178 if ( lerr != LDAP_SUCCESS ) {
180 ( void )meta_clear_one_candidate( lc->conns[ i ], 1 );
187 lc->bound_target = META_BOUND_ALL;
191 * rc is LDAP_SUCCESS if at least one bind succeeded,
192 * err is the last error that occurred during a bind;
193 * if at least (and at most?) one bind succeedes, fine.
195 if ( rc != LDAP_SUCCESS /* && err != LDAP_SUCCESS */ ) {
198 * deal with bind failure ...
200 err = ldap_back_map_result( err );
201 send_ldap_result( conn, op, err, NULL, NULL, NULL, NULL );
209 * meta_back_do_single_bind
211 * attempts to perform a bind with creds
214 meta_back_do_single_bind(
228 * Rewrite the bind dn if needed
230 switch ( rewrite_session( li->targets[ candidate ]->rwinfo,
231 "bindDn", dn->bv_val, lc->conn, &mdn ) ) {
232 case REWRITE_REGEXEC_OK:
234 mdn = ( char * )dn->bv_val;
237 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
238 "[rw] bindDn: \"%s\" -> \"%s\"\n", dn->bv_val, mdn ));
239 #else /* !NEW_LOGGING */
240 Debug( LDAP_DEBUG_ARGS,
241 "rw> bindDn: \"%s\" -> \"%s\"\n%s",
242 dn->bv_val, mdn, "" );
243 #endif /* !NEW_LOGGING */
246 case REWRITE_REGEXEC_UNWILLING:
247 return LDAP_UNWILLING_TO_PERFORM;
249 case REWRITE_REGEXEC_ERR:
250 return LDAP_OPERATIONS_ERROR;
253 rc = ldap_bind_s( lc->conns[ candidate ]->ld, mdn, cred->bv_val, method );
254 if ( rc != LDAP_SUCCESS ) {
255 rc = ldap_back_map_result( rc );
257 lc->conns[ candidate ]->bound_dn = ber_bvdup( dn );
258 lc->conns[ candidate ]->bound = META_BOUND;
259 lc->bound_target = candidate;
261 if ( li->cache.ttl != META_DNCACHE_DISABLED
262 && ndn->bv_len != 0 ) {
263 ( void )meta_dncache_update_entry( &li->cache,
264 ber_bvdup( ndn ), candidate );
268 if ( mdn != dn->bv_val ) {
279 meta_back_dobind( struct metaconn *lc, Operation *op )
281 struct metasingleconn **lsc;
285 * all the targets are bound as pseudoroot
287 if ( lc->bound_target == META_BOUND_ALL ) {
291 for ( i = 0, lsc = lc->conns; lsc[ 0 ] != NULL; ++i, ++lsc ) {
295 * Not a candidate or something wrong with this target ...
297 if ( lsc[ 0 ]->ld == NULL ) {
302 * If the target is already bound it is skipped
304 if ( lsc[ 0 ]->bound == META_BOUND && lc->bound_target == i ) {
310 * Otherwise an anonymous bind is performed
311 * (note: if the target was already bound, the anonymous
312 * bind clears the previous bind).
314 rc = ldap_bind_s( lsc[ 0 ]->ld, lsc[ 0 ]->bound_dn->bv_val,
315 NULL, LDAP_AUTH_SIMPLE );
316 if ( rc != LDAP_SUCCESS ) {
319 LDAP_LOG(( "backend", LDAP_LEVEL_WARNING,
320 "meta_back_dobind: (anonymous)"
321 " bind as \"%s\" failed"
322 " with error \"%s\"\n",
323 lsc[ 0 ]->bound_dn->bv_val,
324 ldap_err2string( rc ) ));
325 #else /* !NEW_LOGGING */
326 Debug( LDAP_DEBUG_ANY,
327 "==>meta_back_dobind: (anonymous)"
328 " bind as \"%s\" failed"
329 " with error \"%s\"\n%s",
330 lsc[ 0 ]->bound_dn->bv_val,
331 ldap_err2string( rc ), "" );
332 #endif /* !NEW_LOGGING */
335 * null cred bind should always succeed
336 * as anonymous, so a failure means
337 * the target is no longer candidate possibly
338 * due to technical reasons (remote host down?)
339 * so better clear the handle
341 ( void )meta_clear_one_candidate( lsc[ 0 ], 1 );
345 lsc[ 0 ]->bound = META_ANONYMOUS;
356 meta_back_is_valid( struct metaconn *lc, int candidate )
358 struct metasingleconn **lsc;
363 if ( candidate < 0 ) {
367 for ( i = 0, lsc = lc->conns;
368 lsc[ 0 ] != NULL && i < candidate;
372 return( lsc[ 0 ]->ld != NULL );
379 * FIXME: error return must be handled in a cleaner way ...
382 meta_back_op_result( struct metaconn *lc, Operation *op )
384 int i, rerr = LDAP_SUCCESS;
385 struct metasingleconn **lsc;
389 for ( i = 0, lsc = lc->conns; lsc[ 0 ] != NULL; ++i, ++lsc ) {
390 int err = LDAP_SUCCESS;
394 ldap_get_option( lsc[ 0 ]->ld, LDAP_OPT_ERROR_NUMBER, &err );
395 if ( err != LDAP_SUCCESS ) {
397 * better check the type of error. In some cases
398 * (search ?) it might be better to return a
399 * success if at least one of the targets gave
400 * positive result ...
402 ldap_get_option( lsc[ 0 ]->ld,
403 LDAP_OPT_ERROR_STRING, &msg );
404 ldap_get_option( lsc[ 0 ]->ld,
405 LDAP_OPT_MATCHED_DN, &match );
406 err = ldap_back_map_result( err );
409 LDAP_LOG(( "backend", LDAP_DEBUG_NOTICE,
410 "meta_back_op_result: target"
411 " <%d> sending msg \"%s\""
412 " (matched \"%s\")\n",
413 i, ( msg ? msg : "" ),
414 ( match ? match : "" ) ));
415 #else /* !NEW_LOGGING */
416 Debug(LDAP_DEBUG_ANY,
417 "==> meta_back_op_result: target"
418 " <%d> sending msg \"%s\""
419 " (matched \"%s\")\n",
420 i, ( msg ? msg : "" ),
421 ( match ? match : "" ) );
422 #endif /* !NEW_LOGGING */
425 * FIXME: need to rewrite "match" (need rwinfo)
437 /* better test the pointers before freeing? */
447 send_ldap_result( lc->conn, op, rerr, rmatch, rmsg, NULL, NULL );
449 return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );