2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2003 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This work was initially developed by the Howard Chu for inclusion
17 * in OpenLDAP Software and subsequently enhanced by Pierangelo
20 /* This is an altered version */
22 * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
24 * This work has been developed to fulfill the requirements
25 * of SysNet s.n.c. <http:www.sys-net.it> and it has been donated
26 * to the OpenLDAP Foundation in the hope that it may be useful
27 * to the Open Source community, but WITHOUT ANY WARRANTY.
29 * Permission is granted to anyone to use this software for any purpose
30 * on any computer system, and to alter it and redistribute it, subject
31 * to the following restrictions:
33 * 1. The author and SysNet s.n.c. are not responsible for the consequences
34 * of use of this software, no matter how awful, even if they arise from
37 * 2. The origin of this software must not be misrepresented, either by
38 * explicit claim or by omission. Since few users ever read sources,
39 * credits should appear in the documentation.
41 * 3. Altered versions must be plainly marked as such, and must not be
42 * misrepresented as being the original software. Since few users
43 * ever read sources, credits should appear in the documentation.
44 * SysNet s.n.c. cannot be responsible for the consequences of the
47 * 4. This notice may not be removed or altered.
50 * This software is based on the backend back-ldap, implemented
51 * by Howard Chu <hyc@highlandsun.com>, and modified by Mark Valence
52 * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
53 * contributors. The contribution of the original software to the present
54 * implementation is acknowledged in this copyright statement.
56 * A special acknowledgement goes to Howard for the overall architecture
57 * (and for borrowing large pieces of code), and to Mark, who implemented
58 * from scratch the attribute/objectclass mapping.
60 * The original copyright statement follows.
62 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
64 * Permission is granted to anyone to use this software for any purpose
65 * on any computer system, and to alter it and redistribute it, subject
66 * to the following restrictions:
68 * 1. The author is not responsible for the consequences of use of this
69 * software, no matter how awful, even if they arise from flaws in it.
71 * 2. The origin of this software must not be misrepresented, either by
72 * explicit claim or by omission. Since few users ever read sources,
73 * credits should appear in the documentation.
75 * 3. Altered versions must be plainly marked as such, and must not be
76 * misrepresented as being the original software. Since few users
77 * ever read sources, credits should appear in the
80 * 4. This notice may not be removed or altered.
88 #include <ac/socket.h>
89 #include <ac/string.h>
94 #include "../back-ldap/back-ldap.h"
95 #include "back-meta.h"
97 static LDAP_REBIND_PROC meta_back_rebind;
100 meta_back_do_single_bind(
108 meta_back_bind( Operation *op, SlapReply *rs )
110 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
113 int rc = -1, i, gotit = 0, ndnlen, isroot = 0;
114 int op_type = META_OP_ALLOW_MULTIPLE;
116 rs->sr_err = LDAP_SUCCESS;
119 LDAP_LOG( BACK_META, ENTRY, "meta_back_bind: dn: %s.\n",
120 op->o_req_dn.bv_val, 0, 0 );
121 #else /* !NEW_LOGGING */
122 Debug( LDAP_DEBUG_ARGS, "meta_back_bind: dn: %s.\n%s%s",
123 op->o_req_dn.bv_val, "", "" );
124 #endif /* !NEW_LOGGING */
126 if ( op->oq_bind.rb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
128 ber_dupbv( &op->oq_bind.rb_edn, be_root_dn( op->o_bd ) );
129 op_type = META_OP_REQUIRE_ALL;
131 lc = meta_back_getconn( op, rs, op_type,
132 &op->o_req_ndn, NULL );
135 LDAP_LOG( BACK_META, NOTICE,
136 "meta_back_bind: no target for dn %s.\n",
137 op->o_req_dn.bv_val, 0, 0 );
138 #else /* !NEW_LOGGING */
139 Debug( LDAP_DEBUG_ANY,
140 "meta_back_bind: no target for dn %s.\n%s%s",
141 op->o_req_dn.bv_val, "", "");
142 #endif /* !NEW_LOGGING */
144 send_ldap_result( op, rs );
149 * Each target is scanned ...
151 lc->bound_target = META_BOUND_NONE;
152 ndnlen = op->o_req_ndn.bv_len;
153 for ( i = 0; i < li->ntargets; i++ ) {
155 struct berval orig_dn = op->o_req_dn;
156 struct berval orig_ndn = op->o_req_ndn;
157 struct berval orig_cred = op->oq_bind.rb_cred;
158 int orig_method = op->oq_bind.rb_method;
162 * Skip non-candidates
164 if ( lc->conns[ i ].candidate != META_CANDIDATE ) {
172 * A bind operation is expected to have
173 * ONE CANDIDATE ONLY!
176 LDAP_LOG( BACK_META, WARNING,
177 "==>meta_back_bind: more than one"
178 " candidate is attempting to bind"
179 " ...\n" , 0, 0, 0 );
180 #else /* !NEW_LOGGING */
181 Debug( LDAP_DEBUG_ANY,
182 "==>meta_back_bind: more than one"
183 " candidate is attempting to bind"
186 #endif /* !NEW_LOGGING */
189 if ( isroot && li->targets[ i ]->pseudorootdn.bv_val != NULL ) {
190 op->o_req_dn = li->targets[ i ]->pseudorootdn;
191 op->o_req_ndn = li->targets[ i ]->pseudorootdn;
192 op->oq_bind.rb_cred = li->targets[ i ]->pseudorootpw;
193 op->oq_bind.rb_method = LDAP_AUTH_SIMPLE;
196 lerr = meta_back_do_single_bind( lc, op, rs, i );
197 if ( lerr != LDAP_SUCCESS ) {
199 ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
204 op->o_req_dn = orig_dn;
205 op->o_req_ndn = orig_ndn;
206 op->oq_bind.rb_cred = orig_cred;
207 op->oq_bind.rb_method = orig_method;
211 lc->bound_target = META_BOUND_ALL;
215 * rc is LDAP_SUCCESS if at least one bind succeeded,
216 * err is the last error that occurred during a bind;
217 * if at least (and at most?) one bind succeedes, fine.
219 if ( rc != LDAP_SUCCESS /* && rs->sr_err != LDAP_SUCCESS */ ) {
222 * deal with bind failure ...
226 * no target was found within the naming context,
227 * so bind must fail with invalid credentials
229 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
230 rs->sr_err = LDAP_INVALID_CREDENTIALS;
233 rs->sr_err = ldap_back_map_result( rs );
234 send_ldap_result( op, rs );
242 * meta_back_do_single_bind
244 * attempts to perform a bind with creds
247 meta_back_do_single_bind(
254 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
255 struct berval mdn = { 0, NULL };
258 struct metasingleconn *lsc = &lc->conns[ candidate ];
261 * Rewrite the bind dn if needed
263 dc.rwmap = &li->targets[ candidate ]->rwmap;
264 dc.conn = op->o_conn;
268 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
269 send_ldap_result( op, rs );
274 rs->sr_err = ldap_set_option( lsc->ld,
275 LDAP_OPT_SERVER_CONTROLS, op->o_ctrls );
276 if ( rs->sr_err != LDAP_SUCCESS ) {
277 rs->sr_err = ldap_back_map_result( rs );
282 rs->sr_err = ldap_sasl_bind(lsc->ld, mdn.bv_val,
283 LDAP_SASL_SIMPLE, &op->oq_bind.rb_cred,
284 op->o_ctrls, NULL, &msgid);
285 if ( rs->sr_err != LDAP_SUCCESS ) {
286 rs->sr_err = ldap_back_map_result( rs );
290 * FIXME: handle response!!!
292 if ( lsc->bound_dn.bv_val != NULL ) {
293 ber_memfree( lsc->bound_dn.bv_val );
295 ber_dupbv( &lsc->bound_dn, &op->o_req_dn );
296 lsc->bound = META_BOUND;
297 lc->bound_target = candidate;
299 if ( li->savecred ) {
300 if ( lsc->cred.bv_val ) {
301 memset( lsc->cred.bv_val, 0, lsc->cred.bv_len );
302 ber_memfree( lsc->cred.bv_val );
304 ber_dupbv( &lsc->cred, &op->oq_bind.rb_cred );
305 ldap_set_rebind_proc( lsc->ld, meta_back_rebind, lsc );
308 if ( li->cache.ttl != META_DNCACHE_DISABLED
309 && op->o_req_ndn.bv_len != 0 ) {
310 ( void )meta_dncache_update_entry( &li->cache,
311 &op->o_req_ndn, candidate );
317 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
328 meta_back_dobind( struct metaconn *lc, Operation *op )
330 struct metasingleconn *lsc;
334 * all the targets are bound as pseudoroot
336 if ( lc->bound_target == META_BOUND_ALL ) {
340 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
344 * Not a candidate or something wrong with this target ...
346 if ( lsc->ld == NULL ) {
351 * If required, set controls
354 if ( ldap_set_option( lsc->ld, LDAP_OPT_SERVER_CONTROLS,
355 op->o_ctrls ) != LDAP_SUCCESS ) {
356 ( void )meta_clear_one_candidate( lsc, 1 );
362 * If the target is already bound it is skipped
364 if ( lsc->bound == META_BOUND && lc->bound_target == i ) {
370 * Otherwise an anonymous bind is performed
371 * (note: if the target was already bound, the anonymous
372 * bind clears the previous bind).
374 if ( lsc->bound_dn.bv_val ) {
375 ber_memfree( lsc->bound_dn.bv_val );
376 lsc->bound_dn.bv_val = NULL;
377 lsc->bound_dn.bv_len = 0;
380 if ( /* FIXME: need li ... li->savecred && */
382 memset( lsc->cred.bv_val, 0, lsc->cred.bv_len );
383 ber_memfree( lsc->cred.bv_val );
384 lsc->cred.bv_val = NULL;
385 lsc->cred.bv_len = 0;
388 rc = ldap_bind_s( lsc->ld, 0, NULL, LDAP_AUTH_SIMPLE );
389 if ( rc != LDAP_SUCCESS ) {
392 LDAP_LOG( BACK_META, WARNING,
393 "meta_back_dobind: (anonymous)"
395 " with error %d (%s)\n",
396 rc, ldap_err2string( rc ), 0 );
397 #else /* !NEW_LOGGING */
398 Debug( LDAP_DEBUG_ANY,
399 "==>meta_back_dobind: (anonymous)"
401 " with error %d (%s)\n",
402 rc, ldap_err2string( rc ), 0 );
403 #endif /* !NEW_LOGGING */
406 * null cred bind should always succeed
407 * as anonymous, so a failure means
408 * the target is no longer candidate possibly
409 * due to technical reasons (remote host down?)
410 * so better clear the handle
412 ( void )meta_clear_one_candidate( lsc, 1 );
416 lsc->bound = META_ANONYMOUS;
427 meta_back_is_valid( struct metaconn *lc, int candidate )
429 struct metasingleconn *lsc;
434 if ( candidate < 0 ) {
438 for ( i = 0, lsc = lc->conns; !META_LAST(lsc) && i < candidate;
441 if ( !META_LAST(lsc) ) {
442 return( lsc->ld != NULL );
451 * This is a callback used for chasing referrals using the same
452 * credentials as the original user on this session.
455 meta_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
456 ber_int_t msgid, void *params )
458 struct metasingleconn *lc = params;
460 return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val,
465 * FIXME: error return must be handled in a cleaner way ...
468 meta_back_op_result( struct metaconn *lc, Operation *op, SlapReply *rs )
470 int i, rerr = LDAP_SUCCESS;
471 struct metasingleconn *lsc;
475 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
479 rs->sr_err = LDAP_SUCCESS;
481 ldap_get_option( lsc->ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
482 if ( rs->sr_err != LDAP_SUCCESS ) {
484 * better check the type of error. In some cases
485 * (search ?) it might be better to return a
486 * success if at least one of the targets gave
487 * positive result ...
489 ldap_get_option( lsc->ld,
490 LDAP_OPT_ERROR_STRING, &msg );
491 ldap_get_option( lsc->ld,
492 LDAP_OPT_MATCHED_DN, &match );
493 rs->sr_err = ldap_back_map_result( rs );
496 LDAP_LOG( BACK_META, RESULTS,
497 "meta_back_op_result: target"
498 " <%d> sending msg \"%s\""
499 " (matched \"%s\")\n",
500 i, ( msg ? msg : "" ),
501 ( match ? match : "" ) );
502 #else /* !NEW_LOGGING */
503 Debug(LDAP_DEBUG_ANY,
504 "==> meta_back_op_result: target"
505 " <%d> sending msg \"%s\""
506 " (matched \"%s\")\n",
507 i, ( msg ? msg : "" ),
508 ( match ? match : "" ) );
509 #endif /* !NEW_LOGGING */
512 * FIXME: need to rewrite "match" (need rwinfo)
514 switch ( rs->sr_err ) {
524 /* better test the pointers before freeing? */
536 rs->sr_matched = rmatch;
537 send_ldap_result( op, rs );
539 rs->sr_matched = NULL;
541 return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );