conn.c 47.2 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
2
3
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
4
 * Copyright 1999-2021 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
6
 * Portions Copyright 2001-2003 Pierangelo Masarati.
 * Portions Copyright 1999-2003 Howard Chu.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
7
 * All rights reserved.
Pierangelo Masarati's avatar
Pierangelo Masarati committed
8
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
9
10
11
12
13
14
15
16
17
18
19
20
21
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was initially developed by the Howard Chu for inclusion
 * in OpenLDAP Software and subsequently enhanced by Pierangelo
 * Masarati.
 */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
22
23
24
25
26

#include "portable.h"

#include <stdio.h>

27
#include <ac/errno.h>
Pierangelo Masarati's avatar
Pierangelo Masarati committed
28
29
30
31
32
33
34
35
36
37
#include <ac/socket.h>
#include <ac/string.h>


#define AVL_INTERNAL
#include "slap.h"
#include "../back-ldap/back-ldap.h"
#include "back-meta.h"

/*
38
 * meta_back_conndn_cmp
Pierangelo Masarati's avatar
Pierangelo Masarati committed
39
 *
40
41
 * compares two struct metaconn based on the value of the conn pointer
 * and of the local DN; used by avl stuff
Pierangelo Masarati's avatar
Pierangelo Masarati committed
42
43
 */
int
44
meta_back_conndn_cmp(
Pierangelo Masarati's avatar
Pierangelo Masarati committed
45
	const void *c1,
46
	const void *c2 )
Pierangelo Masarati's avatar
Pierangelo Masarati committed
47
{
48
49
	metaconn_t	*mc1 = ( metaconn_t * )c1;
        metaconn_t	*mc2 = ( metaconn_t * )c2;
50
	int		rc;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
51
	
52
	/* If local DNs don't match, it is definitely not a match */
53
54
55
56
57
58
	/* For shared sessions, conn is NULL. Only explicitly
	 * bound sessions will have non-NULL conn.
	 */
	rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
	if ( rc == 0 ) {
		rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn );
59
60
	}

61
62
63
	return rc;
}

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * meta_back_conndnmc_cmp
 *
 * compares two struct metaconn based on the value of the conn pointer,
 * the local DN and the struct pointer; used by avl stuff
 */
static int
meta_back_conndnmc_cmp(
	const void *c1,
	const void *c2 )
{
	metaconn_t	*mc1 = ( metaconn_t * )c1;
        metaconn_t	*mc2 = ( metaconn_t * )c2;
	int		rc;
	
	/* If local DNs don't match, it is definitely not a match */
	/* For shared sessions, conn is NULL. Only explicitly
	 * bound sessions will have non-NULL conn.
	 */
	rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
	if ( rc == 0 ) {
		rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn );
		if ( rc == 0 ) {
			rc = SLAP_PTRCMP( mc1, mc2 );
		}
	}

	return rc;
}

94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * meta_back_conn_cmp
 *
 * compares two struct metaconn based on the value of the conn pointer;
 * used by avl stuff
 */
int
meta_back_conn_cmp(
	const void *c1,
	const void *c2 )
{
	metaconn_t	*mc1 = ( metaconn_t * )c1;
        metaconn_t	*mc2 = ( metaconn_t * )c2;
	
108
109
110
	/* For shared sessions, conn is NULL. Only explicitly
	 * bound sessions will have non-NULL conn.
	 */
111
	return SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
112
113
114
}

/*
115
 * meta_back_conndn_dup
Pierangelo Masarati's avatar
Pierangelo Masarati committed
116
117
118
119
120
 *
 * returns -1 in case a duplicate struct metaconn has been inserted;
 * used by avl stuff
 */
int
121
meta_back_conndn_dup(
Pierangelo Masarati's avatar
Pierangelo Masarati committed
122
	void *c1,
123
	void *c2 )
Pierangelo Masarati's avatar
Pierangelo Masarati committed
124
{
125
126
	metaconn_t	*mc1 = ( metaconn_t * )c1;
	metaconn_t	*mc2 = ( metaconn_t * )c2;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
127

128
	/* Cannot have more than one shared session with same DN */
129
130
	if ( mc1->mc_conn == mc2->mc_conn &&
		dn_match( &mc1->mc_local_ndn, &mc2->mc_local_ndn ) )
131
132
133
134
135
	{
		return -1;
	}
		
	return 0;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
136
137
138
139
140
}

/*
 * Debug stuff (got it from libavl)
 */
141
#if META_BACK_PRINT_CONNTREE > 0
Pierangelo Masarati's avatar
Pierangelo Masarati committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
static void
meta_back_print( metaconn_t *mc, char *avlstr )
{
	int	i;

	fputs( "targets=[", stderr );
	for ( i = 0; i < mc->mc_info->mi_ntargets; i++ ) {
		fputc( mc->mc_conns[ i ].msc_ld ? '*' : 'o', stderr);
	}
	fputc( ']', stderr );

	fprintf( stderr, " mc=%p local=\"%s\" conn=%p refcnt=%d%s %s\n",
		(void *)mc,
		mc->mc_local_ndn.bv_val ? mc->mc_local_ndn.bv_val : "",
		(void *)mc->mc_conn,
		mc->mc_refcnt,
		LDAP_BACK_CONN_TAINTED( mc ) ? " tainted" : "",
		avlstr );
}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
162
static void
163
meta_back_ravl_print( TAvlnode *root, int depth )
Pierangelo Masarati's avatar
Pierangelo Masarati committed
164
{
165
	int     	i;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
166

Pierangelo Masarati's avatar
Pierangelo Masarati committed
167
168
169
170
	if ( root == 0 ) {
		return;
	}
	
171
	meta_back_ravl_print( root->avl_right, depth + 1 );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
172
173
	
	for ( i = 0; i < depth; i++ ) {
174
		fprintf( stderr, "-" );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
175
	}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
176
177
178
179
	fputc( ' ', stderr );

	meta_back_print( (metaconn_t *)root->avl_data,
		avl_bf2str( root->avl_bf ) );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
180

181
	meta_back_ravl_print( root->avl_left, depth + 1 );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
182
183
}

184
185
186
187
188
189
190
191
192
193
194
/* NOTE: duplicate from back-ldap/bind.c */
static char* priv2str[] = {
	"privileged",
	"privileged/TLS",
	"anonymous",
	"anonymous/TLS",
	"bind",
	"bind/TLS",
	NULL
};

195
void
196
meta_back_print_conntree( metainfo_t *mi, char *msg )
Pierangelo Masarati's avatar
Pierangelo Masarati committed
197
{
198
199
	int	c;

200
	fprintf( stderr, "========> %s\n", msg );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
201
	
202
203
204
205
206
207
208
209
	for ( c = LDAP_BACK_PCONN_FIRST; c < LDAP_BACK_PCONN_LAST; c++ ) {
		int		i = 0;
		metaconn_t	*mc;

		fprintf( stderr, "  %s[%d]\n", priv2str[ c ], mi->mi_conn_priv[ c ].mic_num );

		LDAP_TAILQ_FOREACH( mc, &mi->mi_conn_priv[ c ].mic_priv, mc_q )
		{
Pierangelo Masarati's avatar
Pierangelo Masarati committed
210
211
			fprintf( stderr, "    [%d] ", i );
			meta_back_print( mc, "" );
212
213
214
215
216
			i++;
		}
	}
	
	if ( mi->mi_conninfo.lai_tree == NULL ) {
217
		fprintf( stderr, "\t(empty)\n" );
218

Pierangelo Masarati's avatar
Pierangelo Masarati committed
219
	} else {
220
		meta_back_ravl_print( mi->mi_conninfo.lai_tree, 0 );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
221
222
	}
	
223
	fprintf( stderr, "<======== %s\n", msg );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
224
}
225
#endif /* META_BACK_PRINT_CONNTREE */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
226
227
228
229
230
231
232
233
234
/*
 * End of debug stuff
 */

/*
 * metaconn_alloc
 * 
 * Allocates a connection structure, making room for all the referenced targets
 */
235
236
static metaconn_t *
metaconn_alloc(
237
       	Operation 		*op )
Pierangelo Masarati's avatar
Pierangelo Masarati committed
238
{
239
	metainfo_t	*mi = ( metainfo_t * )op->o_bd->be_private;
240
	metaconn_t	*mc;
241
	int		ntargets = mi->mi_ntargets;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
242
243
244

	assert( ntargets > 0 );

245
	/* malloc all in one */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
246
	mc = ( metaconn_t * )ch_calloc( 1, sizeof( metaconn_t )
247
		+ sizeof( metasingleconn_t ) * ( ntargets - 1 ) );
248
	if ( mc == NULL ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
249
250
		return NULL;
	}
251

252
	mc->mc_info = mi;
253

254
	mc->mc_authz_target = META_BOUND_NONE;
255
	mc->mc_refcnt = 1;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
256

257
	return mc;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
258
259
260
}

/*
261
 * meta_back_init_one_conn
Pierangelo Masarati's avatar
Pierangelo Masarati committed
262
263
264
 * 
 * Initializes one connection
 */
265
int
266
meta_back_init_one_conn(
267
268
	Operation		*op,
	SlapReply		*rs,
269
	metaconn_t		*mc,
270
	int			candidate,
271
	int			ispriv,
272
273
	ldap_back_send_t	sendok,
	int			dolock )
Pierangelo Masarati's avatar
Pierangelo Masarati committed
274
{
275
	metainfo_t		*mi = ( metainfo_t * )op->o_bd->be_private;
276
	metatarget_t		*mt = mi->mi_targets[ candidate ];
277
	metasingleconn_t	*msc = &mc->mc_conns[ candidate ];
278
	int			version;
279
280
	dncookie		dc;
	int			isauthz = ( candidate == mc->mc_authz_target );
281
	int			do_return = 0;
282
283
#ifdef HAVE_TLS
	int			is_ldaps = 0;
284
	int			do_start_tls = 0;
285
#endif /* HAVE_TLS */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
286

287
288
289
290
291
292
	/* if the server is quarantined, and
	 * - the current interval did not expire yet, or
	 * - no more retries should occur,
	 * don't return the connection */
	if ( mt->mt_isquarantined ) {
		slap_retry_info_t	*ri = &mt->mt_quarantine;
293
		int			dont_retry = 0;
294

295
296
		if ( mt->mt_quarantine.ri_interval ) {
			ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
297
298
			dont_retry = ( mt->mt_isquarantined > LDAP_BACK_FQ_NO );
			if ( dont_retry ) {
299
300
301
				dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
					|| slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
				if ( !dont_retry ) {
302
303
304
305
306
					Debug(LDAP_DEBUG_ANY,
					      "%s meta_back_init_one_conn[%d]: quarantine " "retry block #%d try #%d.\n",
					      op->o_log_prefix,
					      candidate, ri->ri_idx,
					      ri->ri_count );
307
308

					mt->mt_isquarantined = LDAP_BACK_FQ_RETRYING;
309
310
				}

311
			}
312
			ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );
313
314
315
316
317
		}

		if ( dont_retry ) {
			rs->sr_err = LDAP_UNAVAILABLE;
			if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
318
				rs->sr_text = "Target is quarantined";
319
320
321
322
323
324
				send_ldap_result( op, rs );
			}
			return rs->sr_err;
		}
	}

325
326
327
328
329
retry_lock:;
	if ( dolock ) {
		ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
	}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
330
331
332
	/*
	 * Already init'ed
	 */
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
	if ( LDAP_BACK_CONN_ISBOUND( msc )
		|| LDAP_BACK_CONN_ISANON( msc ) )
	{
		assert( msc->msc_ld != NULL );
		rs->sr_err = LDAP_SUCCESS;
		do_return = 1;

	} else if ( META_BACK_CONN_CREATING( msc )
		|| LDAP_BACK_CONN_BINDING( msc ) )
	{
		if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
			if ( dolock ) {
				ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
			}

			ldap_pvt_thread_yield();
			goto retry_lock;
		}

		/* sounds more appropriate */
		rs->sr_err = LDAP_BUSY;
354
		rs->sr_text = "No connections to target are available";
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
		do_return = 1;

	} else if ( META_BACK_CONN_INITED( msc ) ) {
		assert( msc->msc_ld != NULL );
		rs->sr_err = LDAP_SUCCESS;
		do_return = 1;

	} else {
		/*
		 * creating...
		 */
		META_BACK_CONN_CREATING_SET( msc );
	}

	if ( dolock ) {
		ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
	}

	if ( do_return ) {
374
375
376
377
		if ( rs->sr_err != LDAP_SUCCESS
			&& op->o_conn
			&& ( sendok & LDAP_BACK_SENDERR ) )
		{
378
379
380
381
			send_ldap_result( op, rs );
		}

		return rs->sr_err;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
382
	}
383

384
	assert( msc->msc_ld == NULL );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
385
386
387
388
       
	/*
	 * Attempts to initialize the connection to the target ds
	 */
389
	ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
390
	rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
391
392
393
394
#ifdef HAVE_TLS
	is_ldaps = ldap_is_ldaps_url( mt->mt_uri );
#endif /* HAVE_TLS */
	ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
395
	if ( rs->sr_err != LDAP_SUCCESS ) {
396
		goto error_return;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
397
	}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
398

Pierangelo Masarati's avatar
Pierangelo Masarati committed
399
400
401
402
	/*
	 * Set LDAP version. This will always succeed: If the client
	 * bound with a particular version, then so can we.
	 */
403
404
405
406
407
408
409
410
411
412
	if ( mt->mt_version != 0 ) {
		version = mt->mt_version;

	} else if ( op->o_conn->c_protocol != 0 ) {
		version = op->o_conn->c_protocol;

	} else {
		version = LDAP_VERSION3;
	}
	ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &version );
413
	ldap_set_urllist_proc( msc->msc_ld, mt->mt_urllist_f, mt->mt_urllist_p );
414

415
416
	/* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
	ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS,
417
		META_BACK_TGT_CHASE_REFERRALS( mt ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
418

419
420
	slap_client_keepalive(msc->msc_ld, &mt->mt_tls.sb_keepalive);

421
422
423
424
425
426
427
	if ( mt->mt_tls.sb_tcp_user_timeout > 0 ) {
		ldap_set_option( msc->msc_ld, LDAP_OPT_TCP_USER_TIMEOUT,
				&mt->mt_tls.sb_tcp_user_timeout );
	}



428
#ifdef HAVE_TLS
429
	{
430
431
432
433
434
435
436
437
		slap_bindconf *sb = NULL;

		if ( ispriv ) {
			sb = &mt->mt_idassert.si_bc;
		} else {
			sb = &mt->mt_tls;
		}

438
		bindconf_tls_set( sb, msc->msc_ld );
439

440
		if ( !is_ldaps ) {
441
			if ( META_BACK_TGT_USE_TLS( mt )
442
443
444
445
				|| ( op->o_conn->c_is_tls && META_BACK_TGT_PROPAGATE_TLS( mt ) ) )
			{
				do_start_tls = 1;
			}
446
447
448
		}
	}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
449
	/* start TLS ("tls [try-]{start|propagate}" statement) */
450
	if ( do_start_tls ) {
451
#ifdef SLAP_STARTTLS_ASYNCHRONOUS
452
		/*
Pierangelo Masarati's avatar
Pierangelo Masarati committed
453
454
		 * use asynchronous StartTLS; in case, chase referral
		 * FIXME: OpenLDAP does not return referral on StartTLS yet
455
456
		 */
		int		msgid;
457

458
		rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
459
460
		if ( rs->sr_err == LDAP_SUCCESS ) {
			LDAPMessage	*res = NULL;
461
			int		rc, nretries = mt->mt_nretries;
462
463
464
			struct timeval	tv;

			LDAP_BACK_TV_SET( &tv );
465

466
retry:;
467
			rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
468
469
			switch ( rc ) {
			case -1:
Howard Chu's avatar
Howard Chu committed
470
471
				rs->sr_err = LDAP_UNAVAILABLE;
				rs->sr_text = "Remote server down";
472
				break;
473

474
			case 0:
475
476
477
478
				if ( nretries != 0 ) {
					if ( nretries > 0 ) {
						nretries--;
					}
479
					LDAP_BACK_TV_SET( &tv );
480
481
482
					goto retry;
				}
				rs->sr_err = LDAP_OTHER;
Howard Chu's avatar
Howard Chu committed
483
				rs->sr_text = "Timeout, no more retries";
484
				break;
485

486
487
488
489
490
491
492
493
494
			default:
				/* only touch when activity actually took place... */
				if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
					msc->msc_time = op->o_time;
				}
				break;
			}

			if ( rc == LDAP_RES_EXTENDED ) {
495
				struct berval	*data = NULL;
496

497
				/* NOTE: right now, data is unused, so don't get it */
498
499
				rs->sr_err = ldap_parse_extended_result( msc->msc_ld,
					res, NULL, NULL /* &data */ , 0 );
500
				if ( rs->sr_err == LDAP_SUCCESS ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
501
502
					int		err;

503
					/* FIXME: matched? referrals? response controls? */
504
505
					rs->sr_err = ldap_parse_result( msc->msc_ld,
						res, &err, NULL, NULL, NULL, NULL, 1 );
506
					res = NULL;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
507
508
509
510

					if ( rs->sr_err == LDAP_SUCCESS ) {
						rs->sr_err = err;
					}
511
					rs->sr_err = slap_map_api2result( rs );
512
					
513
514
515
516
					/* FIXME: in case a referral 
					 * is returned, should we try
					 * using it instead of the 
					 * configured URI? */
517
					if ( rs->sr_err == LDAP_SUCCESS ) {
518
						rs->sr_err = ldap_install_tls( msc->msc_ld );
519

520
					} else if ( rs->sr_err == LDAP_REFERRAL ) {
521
						/* FIXME: LDAP_OPERATIONS_ERROR? */
522
						rs->sr_err = LDAP_OTHER;
523
524
						rs->sr_text = "Unwilling to chase referral "
							"returned by Start TLS exop";
525
526
					}

527
					if ( data ) {
528
						ber_bvfree( data );
529
					}
530
				}
531
532
533

			} else {
				rs->sr_err = LDAP_OTHER;
Howard Chu's avatar
Howard Chu committed
534
				rs->sr_text = "Unknown response to StartTLS request :"
535
					" an ExtendedResponse is expected";
536
537
538
539
			}

			if ( res != NULL ) {
				ldap_msgfree( res );
540
541
			}
		}
542
#else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
543
544
545
		/*
		 * use synchronous StartTLS
		 */
546
		rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
547
#endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
548
549
550
551
552
553

		/* if StartTLS is requested, only attempt it if the URL
		 * is not "ldaps://"; this may occur not only in case
		 * of misconfiguration, but also when used in the chain 
		 * overlay, where the "uri" can be parsed out of a referral */
		if ( rs->sr_err == LDAP_SERVER_DOWN
554
			|| ( rs->sr_err != LDAP_SUCCESS
555
				&& META_BACK_TGT_TLS_CRITICAL( mt ) ) )
556
		{
557

Pierangelo Masarati's avatar
Pierangelo Masarati committed
558
#ifdef DEBUG_205
559
560
561
562
563
			Debug( LDAP_DEBUG_ANY,
				"### %s meta_back_init_one_conn(TLS) "
				"ldap_unbind_ext[%d] ld=%p\n",
				op->o_log_prefix, candidate,
				(void *)msc->msc_ld );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
564
#endif /* DEBUG_205 */
565

566
567
			/* need to trash a failed Start TLS */
			meta_clear_one_candidate( op, mc, candidate );
568
569
570
			goto error_return;
		}
	}
571
#endif /* HAVE_TLS */
572

573
574
575
	/*
	 * Set the network timeout if set
	 */
576
	if ( mt->mt_network_timeout != 0 ) {
577
		struct timeval	network_timeout;
578
579

		network_timeout.tv_usec = 0;
580
		network_timeout.tv_sec = mt->mt_network_timeout;
581

582
		ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
583
				(void *)&network_timeout );
584
585
	}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
586
	/*
587
	 * If the connection DN is not null, an attempt to rewrite it is made
Pierangelo Masarati's avatar
Pierangelo Masarati committed
588
	 */
589

590
	if ( ispriv ) {
591
592
593
		if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) ) {
			ber_bvreplace( &msc->msc_bound_ndn, &mt->mt_idassert_authcDN );
			if ( !BER_BVISNULL( &mt->mt_idassert_passwd ) ) {
594
595
596
597
				if ( !BER_BVISNULL( &msc->msc_cred ) ) {
					memset( msc->msc_cred.bv_val, 0,
						msc->msc_cred.bv_len );
				}
598
				ber_bvreplace( &msc->msc_cred, &mt->mt_idassert_passwd );
599
			}
600
			LDAP_BACK_CONN_ISIDASSERT_SET( msc );
601
602

		} else {
603
			ber_bvreplace( &msc->msc_bound_ndn, &slap_empty_bv );
604
605
		}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
606
	} else {
607
608
609
610
611
612
613
614
615
		if ( !BER_BVISNULL( &msc->msc_cred ) ) {
			memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
			ber_memfree_x( msc->msc_cred.bv_val, NULL );
			BER_BVZERO( &msc->msc_cred );
		}
		if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
			ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL );
			BER_BVZERO( &msc->msc_bound_ndn );
		}
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
		if ( !BER_BVISEMPTY( &op->o_ndn )
			&& SLAP_IS_AUTHZ_BACKEND( op )
			&& isauthz )
		{
			dc.target = mt;
			dc.conn = op->o_conn;
			dc.rs = rs;
			dc.ctx = "bindDN";
		
			/*
			 * Rewrite the bind dn if needed
			 */
			if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
						&msc->msc_bound_ndn ) )
			{
631

Pierangelo Masarati's avatar
Pierangelo Masarati committed
632
#ifdef DEBUG_205
633
634
635
636
637
				Debug( LDAP_DEBUG_ANY,
					"### %s meta_back_init_one_conn(rewrite) "
					"ldap_unbind_ext[%d] ld=%p\n",
					op->o_log_prefix, candidate,
					(void *)msc->msc_ld );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
638
#endif /* DEBUG_205 */
639

640
641
				/* need to trash a connection not fully established */
				meta_clear_one_candidate( op, mc, candidate );
642
643
644
				goto error_return;
			}
			
645
			/* copy the DN if needed */
646
647
648
649
			if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
				ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
			}

650
651
			assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );

652
		} else {
653
			ber_dupbv( &msc->msc_bound_ndn, (struct berval *)&slap_empty_bv );
654
		}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
655
656
	}

657
658
	assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );

659
error_return:;
660
661
662
663
	if ( dolock ) {
		ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
	}
	META_BACK_CONN_CREATING_CLEAR( msc );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
664
665
666
667
668
	if ( rs->sr_err == LDAP_SUCCESS ) {
		/*
		 * Sets a cookie for the rewrite session
		 */
		( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
669
670
671
672
673
		META_BACK_CONN_INITED_SET( msc );
	}
	if ( dolock ) {
		ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
	}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
674

675
	if ( rs->sr_err != LDAP_SUCCESS ) {
Howard Chu's avatar
Howard Chu committed
676
677
		/* Get the error message and print it in TRACE mode */
		if ( LogTest( LDAP_DEBUG_TRACE ) ) {
678
			Log( LDAP_DEBUG_TRACE, ldap_syslog_level, "%s: meta_back_init_one_conn[%d] failed err=%d text=%s\n",
Howard Chu's avatar
Howard Chu committed
679
680
681
				op->o_log_prefix, candidate, rs->sr_err, rs->sr_text );
		}

682
683
684
685
686
687
688
		rs->sr_err = slap_map_api2result( rs );
		if ( sendok & LDAP_BACK_SENDERR ) {
			send_ldap_result( op, rs );
		}
	}

	return rs->sr_err;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
689
690
}

691
/*
692
 * meta_back_retry
693
694
695
696
 * 
 * Retries one connection
 */
int
697
meta_back_retry(
698
699
	Operation		*op,
	SlapReply		*rs,
700
	metaconn_t		**mcp,
701
	int			candidate,
702
	ldap_back_send_t	sendok )
703
{
704
	metainfo_t		*mi = ( metainfo_t * )op->o_bd->be_private;
705
	metatarget_t		*mt = mi->mi_targets[ candidate ];
706
	metaconn_t		*mc = *mcp;
707
	metasingleconn_t	*msc = &mc->mc_conns[ candidate ];
708
	int			rc = LDAP_UNAVAILABLE,
709
710
				binding,
				quarantine = 1;
711

712
	ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
713
714

	assert( !META_BACK_CONN_CREATING( msc ) );
715
	binding = LDAP_BACK_CONN_BINDING( msc );
716
	LDAP_BACK_CONN_BINDING_CLEAR( msc );
717

Pierangelo Masarati's avatar
Pierangelo Masarati committed
718
719
	assert( mc->mc_refcnt > 0 );
	if ( mc->mc_refcnt == 1 ) {
720
721
		struct berval save_cred;

722
		if ( LogTest( LDAP_DEBUG_ANY ) ) {
723
724
			/* this lock is required; however,
			 * it's invoked only when logging is on */
725
			ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
726
727
728
729
			Debug(LDAP_DEBUG_ANY,
			      "%s meta_back_retry[%d]: retrying URI=\"%s\" DN=\"%s\".\n",
			      op->o_log_prefix, candidate, mt->mt_uri,
			      BER_BVISNULL(&msc->msc_bound_ndn) ? "" : msc->msc_bound_ndn.bv_val );
730
731
			ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
		}
732

733
734
735
736
737
		/* save credentials, if any, for later use;
		 * meta_clear_one_candidate() would free them */
		save_cred = msc->msc_cred;
		BER_BVZERO( &msc->msc_cred );

738
		meta_clear_one_candidate( op, mc, candidate );
739
		LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
740

Pierangelo Masarati's avatar
Pierangelo Masarati committed
741
742
		( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );

Pierangelo Masarati's avatar
Pierangelo Masarati committed
743
		/* mc here must be the regular mc, reset and ready for init */
744
		rc = meta_back_init_one_conn( op, rs, mc, candidate,
745
			LDAP_BACK_CONN_ISPRIV( mc ), sendok, 0 );
746

747
		/* restore credentials, if any and if needed;
748
749
		 * meta_back_init_one_conn() restores msc_bound_ndn, if any;
		 * if no msc_bound_ndn is restored, destroy credentials */
750
751
752
		if ( !BER_BVISNULL( &msc->msc_bound_ndn )
			&& BER_BVISNULL( &msc->msc_cred ) )
		{
753
754
755
756
			msc->msc_cred = save_cred;

		} else if ( !BER_BVISNULL( &save_cred ) ) {
			memset( save_cred.bv_val, 0, save_cred.bv_len );
757
			ber_memfree_x( save_cred.bv_val, NULL );
758
759
		}

760
		/* restore the "binding" flag, in case */
761
762
763
		if ( binding ) {
			LDAP_BACK_CONN_BINDING_SET( msc );
		}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
764
765

		if ( rc == LDAP_SUCCESS ) {
766
			quarantine = 0;
767
			LDAP_BACK_CONN_BINDING_SET( msc ); binding = 1;
768
769
			rc = meta_back_single_dobind( op, rs, mcp, candidate,
				sendok, mt->mt_nretries, 0 );
770
771
772
773
774
775

			Debug( LDAP_DEBUG_ANY,
				"%s meta_back_retry[%d]: "
				"meta_back_single_dobind=%d\n",
				op->o_log_prefix, candidate, rc );
			if ( rc == LDAP_SUCCESS ) {
776
777
778
				if ( !BER_BVISNULL( &msc->msc_bound_ndn ) &&
					!BER_BVISEMPTY( &msc->msc_bound_ndn ) )
				{
779
					LDAP_BACK_CONN_ISBOUND_SET( msc );
780

781
782
783
				} else {
					LDAP_BACK_CONN_ISANON_SET( msc );
				}
784
785
786
787
788

				/* when bound, dispose of the "binding" flag */
				if ( binding ) {
					LDAP_BACK_CONN_BINDING_CLEAR( msc );
				}
789
			}
790
		}
791

792
#if 0	/* ITS#7591, following stmt drops needed result msgs */
793
794
		/* don't send twice */
		sendok &= ~LDAP_BACK_SENDERR;
795
#endif
796
	}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
797

798
	if ( rc != LDAP_SUCCESS ) {
799
800
801
802
		SlapReply		*candidates = meta_back_candidates_get( op );

		candidates[ candidate ].sr_err = rc;

803
		if ( *mcp != NULL ) {
804
805
806
807
808
			if ( mc->mc_refcnt == 1 ) {
				if ( binding ) {
					LDAP_BACK_CONN_BINDING_CLEAR( msc );
				}
				(void)meta_clear_one_candidate( op, mc, candidate );
809
			}
810

811
			LDAP_BACK_CONN_TAINTED_SET( mc );
812
813
814
815
			/* only release if mandatory; otherwise
			 * let the caller do what's best before
			 * releasing */
			if ( META_BACK_ONERR_STOP( mi ) ) {
816
				meta_back_release_conn_lock( mi, mc, 0 );
817
				*mcp = NULL;
818
819
820

			} else {
#if META_BACK_PRINT_CONNTREE > 0
821
				meta_back_print_conntree( mi, ">>> meta_back_retry" );
822
#endif /* META_BACK_PRINT_CONNTREE */
823

824
				/* FIXME: could be done better, reworking meta_back_release_conn_lock() */
825
826
827
828
829
830
831
				if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
					if ( mc->mc_q.tqe_prev != NULL ) {
						assert( LDAP_BACK_CONN_CACHED( mc ) );
						assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
						LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
							mc, mc_q );
						mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
832
						LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
833
834
835
836
837
838
839

					} else {
						assert( !LDAP_BACK_CONN_CACHED( mc ) );
					}

				} else {
					/* FIXME: check if in tree, for consistency? */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
840
					(void)ldap_tavl_delete( &mi->mi_conninfo.lai_tree,
841
842
843
844
						( caddr_t )mc, meta_back_conndnmc_cmp );
				}
				LDAP_BACK_CONN_CACHED_CLEAR( mc );

845
#if META_BACK_PRINT_CONNTREE > 0
846
				meta_back_print_conntree( mi, "<<< meta_back_retry" );
847
#endif /* META_BACK_PRINT_CONNTREE */
848
			}
849
		}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
850

851
		if ( sendok & LDAP_BACK_SENDERR ) {
852
			rs->sr_err = rc;
853
			rs->sr_text = "Unable to retry";
854
855
			send_ldap_result( op, rs );
		}
856
857
	}

858
	if ( quarantine && META_BACK_TGT_QUARANTINE( mt ) ) {
859
		meta_back_quarantine( op, rs, candidate );
860
861
	}

862
	ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
863
864
865
866

	return rc == LDAP_SUCCESS ? 1 : 0;
}

867
868
869
870
871
872
873
874
875
876
/*
 * callback for unique candidate selection
 */
static int
meta_back_conn_cb( Operation *op, SlapReply *rs )
{
	assert( op->o_tag == LDAP_REQ_SEARCH );

	switch ( rs->sr_type ) {
	case REP_SEARCH:
877
		((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
		break;

	case REP_SEARCHREF:
	case REP_RESULT:
		break;

	default:
		return rs->sr_err;
	}

	return 0;
}


static int
meta_back_get_candidate(
	Operation	*op,
	SlapReply	*rs,
	struct berval	*ndn )
{
898
	metainfo_t	*mi = ( metainfo_t * )op->o_bd->be_private;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
899
	long		candidate;
900
901
902
903
904

	/*
	 * tries to get a unique candidate
	 * (takes care of default target)
	 */
905
	candidate = meta_back_select_unique_candidate( mi, ndn );
906
907
908
909
910
911

	/*
	 * if any is found, inits the connection
	 */
	if ( candidate == META_TARGET_NONE ) {
		rs->sr_err = LDAP_NO_SUCH_OBJECT;
912
		rs->sr_text = "No suitable candidate target found";
913
914
915

	} else if ( candidate == META_TARGET_MULTIPLE ) {
		Operation	op2 = *op;
916
		SlapReply	rs2 = { REP_RESULT };
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
		slap_callback	cb2 = { 0 };
		int		rc;

		/* try to get a unique match for the request ndn
		 * among the multiple candidates available */
		op2.o_tag = LDAP_REQ_SEARCH;
		op2.o_req_dn = *ndn;
		op2.o_req_ndn = *ndn;
		op2.ors_scope = LDAP_SCOPE_BASE;
		op2.ors_deref = LDAP_DEREF_NEVER;
		op2.ors_attrs = slap_anlist_no_attrs;
		op2.ors_attrsonly = 0;
		op2.ors_limit = NULL;
		op2.ors_slimit = 1;
		op2.ors_tlimit = SLAP_NO_LIMIT;

Pierangelo Masarati's avatar
Pierangelo Masarati committed
933
		op2.ors_filter = (Filter *)slap_filter_objectClass_pres;
934
		op2.ors_filterstr = *slap_filterstr_objectClass_pres;
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951

		op2.o_callback = &cb2;
		cb2.sc_response = meta_back_conn_cb;
		cb2.sc_private = (void *)&candidate;

		rc = op->o_bd->be_search( &op2, &rs2 );

		switch ( rs2.sr_err ) {
		case LDAP_SUCCESS:
		default:
			rs->sr_err = rs2.sr_err;
			break;

		case LDAP_SIZELIMIT_EXCEEDED:
			/* if multiple candidates can serve the operation,
			 * and a default target is defined, and it is
			 * a candidate, try using it (FIXME: YMMV) */
952
			if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
Pierangelo Masarati's avatar
Pierangelo Masarati committed
953
				&& meta_back_is_candidate( mi->mi_targets[ mi->mi_defaulttarget ],
954
						ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
955
			{
956
				candidate = mi->mi_defaulttarget;
957
958
959
960
961
				rs->sr_err = LDAP_SUCCESS;
				rs->sr_text = NULL;

			} else {
				rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
962
				rs->sr_text = "Unable to select unique candidate target";
963
964
965
			}
			break;
		}
966
967
968

	} else {
		rs->sr_err = LDAP_SUCCESS;
969
970
971
972
973
	}

	return candidate;
}

974
975
static void	*meta_back_candidates_dummy;

976
static void
977
meta_back_candidates_keyfree(
978
979
	void		*key,
	void		*data )
980
{
981
982
983
	metacandidates_t	*mc = (metacandidates_t *)data;

	ber_memfree_x( mc->mc_candidates, NULL );
984
985
986
	ber_memfree_x( data, NULL );
}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
987
SlapReply *
988
989
meta_back_candidates_get( Operation *op )
{
990
991
	metainfo_t		*mi = ( metainfo_t * )op->o_bd->be_private;
	metacandidates_t	*mc;
992
993

	if ( op->o_threadctx ) {
994
995
		void		*data = NULL;

996
		ldap_pvt_thread_pool_getkey( op->o_threadctx,
997
				&meta_back_candidates_dummy, &data, NULL );
998
999
		mc = (metacandidates_t *)data;

1000
	} else {
1001
		mc = mi->mi_candidates;
1002
1003
	}

1004
1005
1006
1007
	if ( mc == NULL ) {