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, err = LDAP_SUCCESS;
98 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
99 "meta_back_bind: dn: %s.\n", dn ));
100 #else /* !NEW_LOGGING */
101 Debug( LDAP_DEBUG_ARGS, "meta_back_bind: dn: %s.\n%s%s", dn, "", "" );
102 #endif /* !NEW_LOGGING */
106 lc = meta_back_getconn( li, conn, op, META_OP_ALLOW_MULTIPLE,
110 LDAP_LOG(( "backend", LDAP_LEVEL_NOTICE,
111 "meta_back_bind: no target for dn %s.\n",
113 #else /* !NEW_LOGGING */
114 Debug( LDAP_DEBUG_ANY,
115 "meta_back_bind: no target for dn %s.\n%s%s",
117 #endif /* !NEW_LOGGING */
122 * Each target is scanned ...
124 ndnlen = strlen( ndn );
125 for ( i = 0; i < li->ntargets; i++ ) {
129 * Skip non-candidates
131 if ( lc->conns[ i ]->candidate != META_CANDIDATE ) {
139 * A bind operation is expected to have
140 * ONE CANDIDATE ONLY!
143 LDAP_LOG(( "backend", LDAP_LEVEL_WARNING,
144 "==>meta_back_bind: more than one"
145 " candidate is attempting to bind"
147 #else /* !NEW_LOGGING */
148 Debug( LDAP_DEBUG_ANY,
149 "==>meta_back_bind: more than one"
150 " candidate is attempting to bind"
153 #endif /* !NEW_LOGGING */
157 lerr = meta_back_do_single_bind( li, lc, dn, ndn, cred,
159 if ( lerr != LDAP_SUCCESS ) {
161 ( void )meta_clear_one_candidate( lc->conns[ i ], 1 );
167 if ( rc != LDAP_SUCCESS && err != LDAP_SUCCESS ) {
170 * deal with bind failure ...
172 err = ldap_back_map_result( err );
173 send_ldap_result( conn, op, err, NULL, "", NULL, NULL );
180 * meta_back_do_single_bind
182 * attempts to perform a bind with creds
185 meta_back_do_single_bind(
199 * Rewrite the bind dn if needed
201 switch ( rewrite_session( li->targets[ candidate ]->rwinfo,
202 "bindDn", dn, lc->conn, &mdn ) ) {
203 case REWRITE_REGEXEC_OK:
208 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
209 "[rw] bindDn: \"%s\" -> \"%s\"\n", dn, mdn ));
210 #else /* !NEW_LOGGING */
211 Debug( LDAP_DEBUG_ARGS,
212 "rw> bindDn: \"%s\" -> \"%s\"\n%s",
214 #endif /* !NEW_LOGGING */
217 case REWRITE_REGEXEC_UNWILLING:
218 return LDAP_UNWILLING_TO_PERFORM;
220 case REWRITE_REGEXEC_ERR:
221 return LDAP_OPERATIONS_ERROR;
224 rc = ldap_bind_s( lc->conns[ candidate ]->ld, mdn,
225 cred->bv_val, method );
226 if ( rc != LDAP_SUCCESS ) {
227 rc = ldap_back_map_result( rc );
229 lc->conns[ candidate ]->bound_dn = ch_strdup( dn );
230 lc->conns[ candidate ]->bound = META_BOUND;
231 lc->bound_target = candidate;
233 if ( li->cache.ttl != META_DNCACHE_DISABLED
234 && ndn[ 0 ] != '\0' ) {
235 ( void )meta_dncache_update_entry( &li->cache,
236 ch_strdup( ndn ), candidate );
251 meta_back_dobind( struct metaconn *lc, Operation *op )
253 struct metasingleconn **lsc;
256 for ( i = 0, lsc = lc->conns; lsc[ 0 ] != NULL; ++i, ++lsc ) {
260 * Not a candidate or something wrong with this target ...
262 if ( lsc[ 0 ]->ld == NULL ) {
267 * If the target is already bound it is skipped
269 if ( lsc[ 0 ]->bound == META_BOUND && lc->bound_target == i ) {
275 * Otherwise an anonymous bind is performed
276 * (note: if the target was already bound, the anonymous
277 * bind clears the previous bind).
279 rc = ldap_bind_s( lsc[ 0 ]->ld, lsc[ 0 ]->bound_dn,
280 NULL, LDAP_AUTH_SIMPLE );
281 if ( rc != LDAP_SUCCESS ) {
284 LDAP_LOG(( "backend", LDAP_LEVEL_WARNING,
285 "meta_back_dobind: (anonymous)"
286 " bind as \"%s\" failed"
287 " with error \"%s\"\n",
289 ldap_err2string( rc ) ));
290 #else /* !NEW_LOGGING */
291 Debug( LDAP_DEBUG_ANY,
292 "==>meta_back_dobind: (anonymous) bind as \"%s\" failed"
293 " with error \"%s\"\n%s",
295 ldap_err2string( rc ), "" );
296 #endif /* !NEW_LOGGING */
299 * null cred bind should always succeed
300 * as anonymous, so a failure means
301 * the target is no longer candidate possibly
302 * due to technical reasons (remote host down?)
304 * so better clear the handle
306 ( void )meta_clear_one_candidate( lsc[ 0 ], 1 );
310 lsc[ 0 ]->bound = META_ANONYMOUS;
318 * FIXME: error return must be handled in a cleaner way ...
321 meta_back_op_result( struct metaconn *lc, Operation *op )
323 int i, err = LDAP_SUCCESS;
326 struct metasingleconn **lsc;
328 for ( i = 0, lsc = lc->conns; lsc[ 0 ] != NULL; ++i, ++lsc ) {
329 ldap_get_option( lsc[ 0 ]->ld, LDAP_OPT_ERROR_NUMBER, &err );
330 if ( err != LDAP_SUCCESS ) {
332 * better check the type of error. In some cases
333 * (search ?) it might be better to return a
334 * success if at least one of the targets gave
335 * positive result ...
337 ldap_get_option( lsc[ 0 ]->ld,
338 LDAP_OPT_ERROR_STRING, &msg );
339 ldap_get_option( lsc[ 0 ]->ld,
340 LDAP_OPT_MATCHED_DN, &match );
341 err = ldap_back_map_result( err );
344 * FIXME: need to rewrite "match"
346 send_ldap_result( lc->conn, op, err, match, msg,
350 LDAP_LOG(( "backend", LDAP_DEBUG_NOTICE,
351 "meta_back_op_result: target"
352 " <%d> sending msg \"%s\""
353 " (matched \"%s\")\n",
354 i, ( msg ? msg : "" ),
355 ( match ? match : "" ) ));
356 #else /* !NEW_LOGGING */
357 Debug(LDAP_DEBUG_ANY,
358 "==> meta_back_op_result: target <%d> sending msg \"%s\" (matched \"%s\")\n",
361 ( match ? match : "" ) );
362 #endif /* !NEW_LOGGING */
364 /* better test the pointers before freeing? */
375 return ( err == LDAP_SUCCESS ) ? 0 : -1;