connection.c 52.1 KB
Newer Older
1
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
3
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
4
 * Copyright 1998-2013 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * All rights reserved.
 *
 * 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>.
 */
/* Portions Copyright (c) 1995 Regents of the University of Michigan.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that this notice is preserved and that due credit is given
 * to the University of Michigan at Ann Arbor. The name of the University
 * may not be used to endorse or promote products derived from this
 * software without specific prior written permission. This software
 * is provided ``as is'' without express or implied warranty.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
24
25
 */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
26
#include "portable.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
27
28

#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
29
#ifdef HAVE_LIMITS_H
Kurt Zeilenga's avatar
Kurt Zeilenga committed
30
#include <limits.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
31
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
32

33
#include <ac/socket.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
34
35
36
#include <ac/errno.h>
#include <ac/string.h>
#include <ac/time.h>
Pierangelo Masarati's avatar
Pierangelo Masarati committed
37
#include <ac/unistd.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
38

Kurt Zeilenga's avatar
Kurt Zeilenga committed
39
#include "lutil.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
40
41
#include "slap.h"

42
43
44
45
#ifdef LDAP_CONNECTIONLESS
#include "../../libraries/liblber/lber-int.h"	/* ber_int_sb_read() */
#endif

46
#ifdef LDAP_SLAPI
47
#include "slapi/slapi.h"
48
49
#endif

50
51
52
/* protected by connections_mutex */
static ldap_pvt_thread_mutex_t connections_mutex;
static Connection *connections = NULL;
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
53

54
static ldap_pvt_thread_mutex_t conn_nextid_mutex;
55
static unsigned long conn_nextid = SLAPD_SYNC_SYNCCONN_OFFSET;
56

57
58
static const char conn_lost_str[] = "connection lost";

59
60
61
const char *
connection_state2str( int state )
{
62
	switch( state ) {
63
64
	case SLAP_C_INVALID:	return "!";
	case SLAP_C_INACTIVE:	return "|";
65
	case SLAP_C_CLOSING:	return "C";
66
	case SLAP_C_ACTIVE:		return "";
67
	case SLAP_C_BINDING:	return "B";
68
	case SLAP_C_CLIENT:		return "L";
69
70
71
72
73
	}

	return "?";
}

74
static Connection* connection_get( ber_socket_t s );
75

76
77
78
79
typedef struct conn_readinfo {
	Operation *op;
	ldap_pvt_thread_start_t *func;
	void *arg;
Howard Chu's avatar
Howard Chu committed
80
	void *ctx;
81
82
83
84
	int nullop;
} conn_readinfo;

static int connection_input( Connection *c, conn_readinfo *cri );
85
static void connection_close( Connection *c );
86

87
static int connection_op_activate( Operation *op );
88
static void connection_op_queue( Operation *op );
89
static int connection_resched( Connection *conn );
90
static void connection_abandon( Connection *conn );
91
static void connection_destroy( Connection *c );
92

93
94
static ldap_pvt_thread_start_t connection_operation;

95
96
97
98
99
/*
 * Initialize connection management infrastructure.
 */
int connections_init(void)
{
100
101
	int i;

102
103
	assert( connections == NULL );

104
	if( connections != NULL) {
105
106
107
108
109
110
111
		Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
			0, 0, 0 );
		return -1;
	}

	/* should check return of every call */
	ldap_pvt_thread_mutex_init( &connections_mutex );
112
	ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
113

114
	connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
115
116

	if( connections == NULL ) {
117
118
		Debug( LDAP_DEBUG_ANY, "connections_init: "
			"allocation (%d*%ld) of connection array failed\n",
119
			dtblsize, (long) sizeof(Connection), 0 );
120
121
122
		return -1;
	}

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
123
	assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
124
	assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
125

126
	for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
127

128
129
130
131
132
133
134
135
	/*
	 * per entry initialization of the Connection array initialization
	 * will be done by connection_init()
	 */ 

	return 0;
}

136
137
138
/*
 * Destroy connection management infrastructure.
 */
139

140
141
int connections_destroy(void)
{
142
	ber_socket_t i;
143
144
145
146
147
148
149
150
151

	/* should check return of every call */

	if( connections == NULL) {
		Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
			0, 0, 0 );
		return -1;
	}

152
	for ( i = 0; i < dtblsize; i++ ) {
153
		if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
154
			ber_sockbuf_free( connections[i].c_sb );
155
			ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
156
157
158
159
			ldap_pvt_thread_mutex_destroy( &connections[i].c_write1_mutex );
			ldap_pvt_thread_mutex_destroy( &connections[i].c_write2_mutex );
			ldap_pvt_thread_cond_destroy( &connections[i].c_write1_cv );
			ldap_pvt_thread_cond_destroy( &connections[i].c_write2_cv );
160
#ifdef LDAP_SLAPI
161
			if ( slapi_plugins_used ) {
162
163
				slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
					&connections[i] );
Howard Chu's avatar
Howard Chu committed
164
			}
165
#endif
166
		}
167
168
169
170
171
172
	}

	free( connections );
	connections = NULL;

	ldap_pvt_thread_mutex_destroy( &connections_mutex );
173
	ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
174
175
176
177
178
179
180
181
	return 0;
}

/*
 * shutdown all connections
 */
int connections_shutdown(void)
{
182
	ber_socket_t i;
183

184
	for ( i = 0; i < dtblsize; i++ ) {
185
186
187
188
189
190
191
192
193
194
195
196
197
		if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
			ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
			if( connections[i].c_struct_state == SLAP_C_USED ) {

				/* give persistent clients a chance to cleanup */
				if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
					ldap_pvt_thread_pool_submit( &connection_pool,
					connections[i].c_clientfunc, connections[i].c_clientarg );
				} else {
					/* c_mutex is locked */
					connection_closing( &connections[i], "slapd shutdown" );
					connection_close( &connections[i] );
				}
Howard Chu's avatar
Howard Chu committed
198
			}
199
			ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
Howard Chu's avatar
Howard Chu committed
200
		}
201
202
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
203
	return 0;
204
205
}

206
207
208
209
210
/*
 * Timeout idle connections.
 */
int connections_timeout_idle(time_t now)
{
211
	int i = 0, writers = 0;
212
	ber_socket_t connindex;
213
	Connection* c;
214
215
216
	time_t old;

	old = slapd_get_writetime();
217

Gary Williams's avatar
Gary Williams committed
218
	for( c = connection_first( &connindex );
219
220
221
		c != NULL;
		c = connection_next( c, &connindex ) )
	{
222
		/* Don't timeout a slow-running request or a persistent
223
224
225
226
227
		 * outbound connection. But if it has a writewaiter, see
		 * if the waiter has been there too long.
		 */
		if(( c->c_n_ops_executing && !c->c_writewaiter)
			|| c->c_conn_state == SLAP_C_CLIENT ) {
228
229
			continue;
		}
230

231
232
		if( global_idletimeout && 
			difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
233
			/* close it */
234
			connection_closing( c, "idletimeout" );
235
236
			connection_close( c );
			i++;
237
238
			continue;
		}
239
		if ( c->c_writewaiter && global_writetimeout ) {
240
241
242
243
244
245
			writers = 1;
			if( difftime( c->c_activitytime+global_writetimeout, now) < 0 ) {
				/* close it */
				connection_closing( c, "writetimeout" );
				connection_close( c );
				i++;
Howard Chu's avatar
Howard Chu committed
246
				continue;
247
			}
248
249
		}
	}
250
	connection_done( c );
251
	if ( old && !writers )
252
		slapd_clr_writetime( old );
253
254
255
256

	return i;
}

Howard Chu's avatar
Howard Chu committed
257
258
259
260
/* Drop all client connections */
void connections_drop()
{
	Connection* c;
261
	ber_socket_t connindex;
Howard Chu's avatar
Howard Chu committed
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276

	for( c = connection_first( &connindex );
		c != NULL;
		c = connection_next( c, &connindex ) )
	{
		/* Don't close a slow-running request or a persistent
		 * outbound connection.
		 */
		if(( c->c_n_ops_executing && !c->c_writewaiter)
			|| c->c_conn_state == SLAP_C_CLIENT ) {
			continue;
		}
		connection_closing( c, "dropping" );
		connection_close( c );
	}
277
	connection_done( c );
Howard Chu's avatar
Howard Chu committed
278
279
}

280
static Connection* connection_get( ber_socket_t s )
281
{
282
283
284
	Connection *c;

	Debug( LDAP_DEBUG_ARGS,
285
286
		"connection_get(%ld)\n",
		(long) s, 0, 0 );
287
288
289

	assert( connections != NULL );

290
	if(s == AC_SOCKET_INVALID) return NULL;
291

Howard Chu's avatar
Howard Chu committed
292
293
	assert( s < dtblsize );
	c = &connections[s];
294

295
	if( c != NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
296
297
		ldap_pvt_thread_mutex_lock( &c->c_mutex );

298
299
		assert( c->c_struct_state != SLAP_C_UNINITIALIZED );

300
301
		if( c->c_struct_state != SLAP_C_USED ) {
			/* connection must have been closed due to resched */
302

303
			Debug( LDAP_DEBUG_CONNS,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
304
305
				"connection_get(%d): connection not used\n",
				s, 0, 0 );
306
307
			assert( c->c_conn_state == SLAP_C_INVALID );
			assert( c->c_sd == AC_SOCKET_INVALID );
308

309
			ldap_pvt_thread_mutex_unlock( &c->c_mutex );
310
311
312
			return NULL;
		}

313
		Debug( LDAP_DEBUG_TRACE,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
314
			"connection_get(%d): got connid=%lu\n",
315
			s, c->c_connid, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
316

317
318
		c->c_n_get++;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
319
320
		assert( c->c_struct_state == SLAP_C_USED );
		assert( c->c_conn_state != SLAP_C_INVALID );
Howard Chu's avatar
Howard Chu committed
321
		assert( c->c_sd != AC_SOCKET_INVALID );
322

323
#ifndef SLAPD_MONITOR
324
		if ( global_idletimeout > 0 )
325
326
#endif /* ! SLAPD_MONITOR */
		{
327
328
			c->c_activitytime = slap_get_time();
		}
329
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
330

331
332
333
334
335
336
337
338
	return c;
}

static void connection_return( Connection *c )
{
	ldap_pvt_thread_mutex_unlock( &c->c_mutex );
}

339
Connection * connection_init(
340
	ber_socket_t s,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
341
	Listener *listener,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
342
343
	const char* dnsname,
	const char* peername,
344
	int flags,
345
	slap_ssf_t ssf,
Howard Chu's avatar
Howard Chu committed
346
	struct berval *authid
347
	LDAP_PF_LOCAL_SENDMSG_ARG(struct berval *peerbv))
348
{
349
	unsigned long id;
350
	Connection *c;
Howard Chu's avatar
Howard Chu committed
351
	int doinit = 0;
352
	ber_socket_t sfd = SLAP_FD2SOCK(s);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
353

354
355
	assert( connections != NULL );

Pierangelo Masarati's avatar
Pierangelo Masarati committed
356
	assert( listener != NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
357
358
359
	assert( dnsname != NULL );
	assert( peername != NULL );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
360
#ifndef HAVE_TLS
Howard Chu's avatar
Howard Chu committed
361
	assert( !( flags & CONN_IS_TLS ));
Kurt Zeilenga's avatar
Kurt Zeilenga committed
362
363
#endif

364
	if( s == AC_SOCKET_INVALID ) {
Gary Williams's avatar
Gary Williams committed
365
		Debug( LDAP_DEBUG_ANY,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
366
			"connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
367
		return NULL;
368
369
370
	}

	assert( s >= 0 );
371
	assert( s < dtblsize );
Howard Chu's avatar
Howard Chu committed
372
373
374
375
376
377
	c = &connections[s];
	if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
		doinit = 1;
	} else {
		assert( c->c_struct_state == SLAP_C_UNUSED );
	}
378

Howard Chu's avatar
Howard Chu committed
379
	if( doinit ) {
380
381
382
383
		c->c_send_ldap_result = slap_send_ldap_result;
		c->c_send_search_entry = slap_send_search_entry;
		c->c_send_search_reference = slap_send_search_reference;
		c->c_send_ldap_extended = slap_send_ldap_extended;
384
		c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
385

386
387
388
		BER_BVZERO( &c->c_authmech );
		BER_BVZERO( &c->c_dn );
		BER_BVZERO( &c->c_ndn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
389

Pierangelo Masarati's avatar
Pierangelo Masarati committed
390
		c->c_listener = NULL;
391
392
		BER_BVZERO( &c->c_peer_domain );
		BER_BVZERO( &c->c_peer_name );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
393

394
395
		LDAP_STAILQ_INIT(&c->c_ops);
		LDAP_STAILQ_INIT(&c->c_pending_ops);
396

Kurt Zeilenga's avatar
Kurt Zeilenga committed
397
#ifdef LDAP_X_TXN
Kurt Zeilenga's avatar
Kurt Zeilenga committed
398
		c->c_txn = CONN_TXN_INACTIVE;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
399
400
401
402
		c->c_txn_backend = NULL;
		LDAP_STAILQ_INIT(&c->c_txn_ops);
#endif

403
		BER_BVZERO( &c->c_sasl_bind_mech );
404
405
406
		c->c_sasl_done = 0;
		c->c_sasl_authctx = NULL;
		c->c_sasl_sockctx = NULL;
407
		c->c_sasl_extra = NULL;
408
		c->c_sasl_bindop = NULL;
409

410
411
412
		c->c_sb = ber_sockbuf_alloc( );

		{
413
			ber_len_t max = sockbuf_max_incoming;
414
415
416
			ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
417
		c->c_currentber = NULL;
418

419
420
		/* should check status of thread calls */
		ldap_pvt_thread_mutex_init( &c->c_mutex );
421
422
423
424
		ldap_pvt_thread_mutex_init( &c->c_write1_mutex );
		ldap_pvt_thread_mutex_init( &c->c_write2_mutex );
		ldap_pvt_thread_cond_init( &c->c_write1_cv );
		ldap_pvt_thread_cond_init( &c->c_write2_cv );
425

426
#ifdef LDAP_SLAPI
427
		if ( slapi_plugins_used ) {
428
			slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
429
		}
430
#endif
431
	}
432

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
433
	ldap_pvt_thread_mutex_lock( &c->c_mutex );
434

435
436
437
	assert( BER_BVISNULL( &c->c_authmech ) );
	assert( BER_BVISNULL( &c->c_dn ) );
	assert( BER_BVISNULL( &c->c_ndn ) );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
438
	assert( c->c_listener == NULL );
439
440
	assert( BER_BVISNULL( &c->c_peer_domain ) );
	assert( BER_BVISNULL( &c->c_peer_name ) );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
441
442
	assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
	assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
443
#ifdef LDAP_X_TXN
Kurt Zeilenga's avatar
Kurt Zeilenga committed
444
	assert( c->c_txn == CONN_TXN_INACTIVE );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
445
446
447
	assert( c->c_txn_backend == NULL );
	assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
#endif
448
	assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
449
450
451
	assert( c->c_sasl_done == 0 );
	assert( c->c_sasl_authctx == NULL );
	assert( c->c_sasl_sockctx == NULL );
452
	assert( c->c_sasl_extra == NULL );
453
	assert( c->c_sasl_bindop == NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
454
	assert( c->c_currentber == NULL );
455
	assert( c->c_writewaiter == 0);
456
	assert( c->c_writers == 0);
457

Pierangelo Masarati's avatar
Pierangelo Masarati committed
458
	c->c_listener = listener;
Howard Chu's avatar
Howard Chu committed
459
	c->c_sd = s;
460

Howard Chu's avatar
Howard Chu committed
461
	if ( flags & CONN_IS_CLIENT ) {
462
		c->c_connid = 0;
463
		ldap_pvt_thread_mutex_lock( &connections_mutex );
464
		c->c_conn_state = SLAP_C_CLIENT;
465
		c->c_struct_state = SLAP_C_USED;
466
		ldap_pvt_thread_mutex_unlock( &connections_mutex );
467
		c->c_close_reason = "?";			/* should never be needed */
468
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &sfd );
469
470
		ldap_pvt_thread_mutex_unlock( &c->c_mutex );

471
		return c;
472
473
	}

474
475
	ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
	ber_str2bv( peername, 0, 1, &c->c_peer_name );
476

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
477
478
479
480
	c->c_n_ops_received = 0;
	c->c_n_ops_executing = 0;
	c->c_n_ops_pending = 0;
	c->c_n_ops_completed = 0;
481
482
483
484

	c->c_n_get = 0;
	c->c_n_read = 0;
	c->c_n_write = 0;
485

486
487
	/* set to zero until bind, implies LDAP_VERSION3 */
	c->c_protocol = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
488

489
#ifndef SLAPD_MONITOR
490
	if ( global_idletimeout > 0 )
491
492
#endif /* ! SLAPD_MONITOR */
	{
493
494
		c->c_activitytime = c->c_starttime = slap_get_time();
	}
495

496
497
#ifdef LDAP_CONNECTIONLESS
	c->c_is_udp = 0;
Howard Chu's avatar
Howard Chu committed
498
	if( flags & CONN_IS_UDP ) {
499
500
		c->c_is_udp = 1;
#ifdef LDAP_DEBUG
Kurt Zeilenga's avatar
Kurt Zeilenga committed
501
502
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
			LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
503
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
504
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
505
			LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
506
507
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
			LBER_SBIOD_LEVEL_PROVIDER, NULL );
508
	} else
Howard Chu's avatar
Howard Chu committed
509
510
511
512
513
514
#endif /* LDAP_CONNECTIONLESS */
#ifdef LDAP_PF_LOCAL
	if ( flags & CONN_IS_IPC ) {
#ifdef LDAP_DEBUG
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
			LBER_SBIOD_LEVEL_PROVIDER, (void*)"ipc_" );
515
#endif
Howard Chu's avatar
Howard Chu committed
516
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_fd,
517
			LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
Howard Chu's avatar
Howard Chu committed
518
519
520
521
522
523
#ifdef LDAP_PF_LOCAL_SENDMSG
		if ( !BER_BVISEMPTY( peerbv ))
			ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_UNGET_BUF, peerbv );
#endif
	} else
#endif /* LDAP_PF_LOCAL */
524
	{
525
#ifdef LDAP_DEBUG
Kurt Zeilenga's avatar
Kurt Zeilenga committed
526
527
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
			LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
528
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
529
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
530
			LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
531
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
532
533
534
535

#ifdef LDAP_DEBUG
	ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
		INT_MAX, (void*)"ldap_" );
536
#endif
537

Kurt Zeilenga's avatar
Kurt Zeilenga committed
538
539
540
541
542
	if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
		c /* non-NULL */ ) < 0 )
	{
		Debug( LDAP_DEBUG_ANY,
			"connection_init(%d, %s): set nonblocking failed\n",
Pierangelo Masarati's avatar
Pierangelo Masarati committed
543
			s, c->c_peer_name.bv_val, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
544
	}
545

546
	ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
547
	id = c->c_connid = conn_nextid++;
548
	ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
549

550
	ldap_pvt_thread_mutex_lock( &connections_mutex );
551
	c->c_conn_state = SLAP_C_INACTIVE;
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
552
	c->c_struct_state = SLAP_C_USED;
553
	ldap_pvt_thread_mutex_unlock( &connections_mutex );
554
	c->c_close_reason = "?";			/* should never be needed */
555

556
557
558
	c->c_ssf = c->c_transport_ssf = ssf;
	c->c_tls_ssf = 0;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
559
#ifdef HAVE_TLS
Howard Chu's avatar
Howard Chu committed
560
	if ( flags & CONN_IS_TLS ) {
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
561
562
563
564
565
566
		c->c_is_tls = 1;
		c->c_needs_tls_accept = 1;
	} else {
		c->c_is_tls = 0;
		c->c_needs_tls_accept = 0;
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
567
#endif
568

569
	slap_sasl_open( c, 0 );
570
	slap_sasl_external( c, ssf, authid );
571

572
	slapd_add_internal( s, 1 );
573

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
574
	backend_connection_init(c);
575
	ldap_pvt_thread_mutex_unlock( &c->c_mutex );
576

577
578
579
580
581
	if ( !(flags & CONN_IS_UDP ))
		Statslog( LDAP_DEBUG_STATS,
			"conn=%ld fd=%ld ACCEPT from %s (%s)\n",
			id, (long) s, peername, listener->sl_name.bv_val, 0 );

582
	return c;
583
584
}

585
586
void connection2anonymous( Connection *c )
{
587
588
	assert( connections != NULL );
	assert( c != NULL );
589

590
	{
591
		ber_len_t max = sockbuf_max_incoming;
592
593
594
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
	}

595
	if ( !BER_BVISNULL( &c->c_authmech ) ) {
596
		ch_free(c->c_authmech.bv_val);
597
	}
598
	BER_BVZERO( &c->c_authmech );
599

600
	if ( !BER_BVISNULL( &c->c_dn ) ) {
601
		ch_free(c->c_dn.bv_val);
602
	}
603
	BER_BVZERO( &c->c_dn );
604

605
	if ( !BER_BVISNULL( &c->c_ndn ) ) {
606
		ch_free(c->c_ndn.bv_val);
607
	}
608
	BER_BVZERO( &c->c_ndn );
609

610
611
	if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
		ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
612
613
	}
	BER_BVZERO( &c->c_sasl_authz_dn );
614
615
616
617

	c->c_authz_backend = NULL;
}

618
619
620
static void
connection_destroy( Connection *c )
{
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
621
	unsigned long	connid;
622
	const char		*close_reason;
Howard Chu's avatar
Howard Chu committed
623
	Sockbuf			*sb;
Howard Chu's avatar
Howard Chu committed
624
	ber_socket_t	sd;
625

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
626
627
628
629
630
	assert( connections != NULL );
	assert( c != NULL );
	assert( c->c_struct_state != SLAP_C_UNUSED );
	assert( c->c_conn_state != SLAP_C_INVALID );
	assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
631
632
633
634
635
636
	assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
#ifdef LDAP_X_TXN
	assert( c->c_txn == CONN_TXN_INACTIVE );
	assert( c->c_txn_backend == NULL );
	assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
#endif
637
	assert( c->c_writewaiter == 0);
638
	assert( c->c_writers == 0);
639

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
640
641
	/* only for stats (print -1 as "%lu" may give unexpected results ;) */
	connid = c->c_connid;
642
	close_reason = c->c_close_reason;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
643

Howard Chu's avatar
Howard Chu committed
644
645
646
647
	ldap_pvt_thread_mutex_lock( &connections_mutex );
	c->c_struct_state = SLAP_C_PENDING;
	ldap_pvt_thread_mutex_unlock( &connections_mutex );

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
648
	backend_connection_destroy(c);
649

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
650
651
	c->c_protocol = 0;
	c->c_connid = -1;
652

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
653
	c->c_activitytime = c->c_starttime = 0;
654

655
	connection2anonymous( c );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
656
	c->c_listener = NULL;
657

658
659
	if(c->c_peer_domain.bv_val != NULL) {
		free(c->c_peer_domain.bv_val);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
660
	}
661
	BER_BVZERO( &c->c_peer_domain );
662
663
	if(c->c_peer_name.bv_val != NULL) {
		free(c->c_peer_name.bv_val);
664
	}
665
	BER_BVZERO( &c->c_peer_name );
666

667
	c->c_sasl_bind_in_progress = 0;
668
669
	if(c->c_sasl_bind_mech.bv_val != NULL) {
		free(c->c_sasl_bind_mech.bv_val);
670
	}
671
	BER_BVZERO( &c->c_sasl_bind_mech );
672
673

	slap_sasl_close( c );
674

Kurt Zeilenga's avatar
Kurt Zeilenga committed
675
676
677
678
679
	if ( c->c_currentber != NULL ) {
		ber_free( c->c_currentber, 1 );
		c->c_currentber = NULL;
	}

680

Howard Chu's avatar
Howard Chu committed
681
682
683
684
#ifdef LDAP_SLAPI
	/* call destructors, then constructors; avoids unnecessary allocation */
	if ( slapi_plugins_used ) {
		slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
685
	}
Howard Chu's avatar
Howard Chu committed
686
#endif
687

Howard Chu's avatar
Howard Chu committed
688
689
	sd = c->c_sd;
	c->c_sd = AC_SOCKET_INVALID;
Howard Chu's avatar
Howard Chu committed
690
691
692
	c->c_conn_state = SLAP_C_INVALID;
	c->c_struct_state = SLAP_C_UNUSED;
	c->c_close_reason = "?";			/* should never be needed */
693

Howard Chu's avatar
Howard Chu committed
694
695
	sb = c->c_sb;
	c->c_sb = ber_sockbuf_alloc( );
696
	{
697
		ber_len_t max = sockbuf_max_incoming;
698
699
700
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
	}

Howard Chu's avatar
Howard Chu committed
701
702
703
	/* c must be fully reset by this point; when we call slapd_remove
	 * it may get immediately reused by a new connection.
	 */
Howard Chu's avatar
Howard Chu committed
704
705
	if ( sd != AC_SOCKET_INVALID ) {
		slapd_remove( sd, sb, 1, 0, 0 );
Howard Chu's avatar
Howard Chu committed
706

707
		if ( close_reason == NULL ) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
708
			Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed\n",
Howard Chu's avatar
Howard Chu committed
709
				connid, (long) sd, 0, 0, 0 );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
710
711
		} else {
			Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
Howard Chu's avatar
Howard Chu committed
712
				connid, (long) sd, close_reason, 0, 0 );
713
		}
714
	}
715
716
}

717
int connection_valid( Connection *c )
718
{
719
720
	/* c_mutex must be locked by caller */

721
	assert( c != NULL );
722

723
724
725
	return c->c_struct_state == SLAP_C_USED &&
		c->c_conn_state >= SLAP_C_ACTIVE &&
		c->c_conn_state <= SLAP_C_CLIENT;
726
727
}

728
729
730
731
static void connection_abandon( Connection *c )
{
	/* c_mutex must be locked by caller */

732
	Operation *o, *next, op = {0};
Howard Chu's avatar
Howard Chu committed
733
	Opheader ohdr = {0};
734

Howard Chu's avatar
Howard Chu committed
735
736
737
	op.o_hdr = &ohdr;
	op.o_conn = c;
	op.o_connid = c->c_connid;
738
	op.o_tag = LDAP_REQ_ABANDON;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
739

740
	for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
741
742
		SlapReply rs = {REP_RESULT};

743
744
		next = LDAP_STAILQ_NEXT( o, o_next );
		op.orn_msgid = o->o_msgid;
745
		o->o_abandon = 1;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
746
		op.o_bd = frontendDB;
747
		frontendDB->be_abandon( &op, &rs );
748
749
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
750
751
752
753
754
#ifdef LDAP_X_TXN
	/* remove operations in pending transaction */
	while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
		LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
		LDAP_STAILQ_NEXT(o, o_next) = NULL;
Howard Chu's avatar
Howard Chu committed
755
		slap_op_free( o, NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
756
757
758
759
760
761
762
	}

	/* clear transaction */
	c->c_txn_backend = NULL;
	c->c_txn = CONN_TXN_INACTIVE;
#endif

763
	/* remove pending operations */
764
765
766
	while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
		LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
		LDAP_STAILQ_NEXT(o, o_next) = NULL;
Howard Chu's avatar
Howard Chu committed
767
		slap_op_free( o, NULL );
768
769
770
	}
}

771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
static void
connection_wake_writers( Connection *c )
{
	/* wake write blocked operations */
	ldap_pvt_thread_mutex_lock( &c->c_write1_mutex );
	if ( c->c_writers > 0 ) {
		c->c_writers = -c->c_writers;
		ldap_pvt_thread_cond_broadcast( &c->c_write1_cv );
		ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
		if ( c->c_writewaiter ) {
			ldap_pvt_thread_mutex_lock( &c->c_write2_mutex );
			ldap_pvt_thread_cond_signal( &c->c_write2_cv );
			slapd_clr_write( c->c_sd, 1 );
			ldap_pvt_thread_mutex_unlock( &c->c_write2_mutex );
		}
		ldap_pvt_thread_mutex_lock( &c->c_write1_mutex );
		while ( c->c_writers ) {
			ldap_pvt_thread_cond_wait( &c->c_write1_cv, &c->c_write1_mutex );
		}
		ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
	} else {
		ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
		slapd_clr_write( c->c_sd, 1 );
	}
}

797
void connection_closing( Connection *c, const char *why )
798
799
800
{
	assert( connections != NULL );
	assert( c != NULL );
Howard Chu's avatar
Howard Chu committed
801
802
803

	if ( c->c_struct_state != SLAP_C_USED ) return;

804
805
	assert( c->c_conn_state != SLAP_C_INVALID );

806
807
	/* c_mutex must be locked by caller */

808
	if( c->c_conn_state != SLAP_C_CLOSING ) {
809
		Debug( LDAP_DEBUG_CONNS,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
810
			"connection_closing: readying conn=%lu sd=%d for close\n",
Howard Chu's avatar
Howard Chu committed
811
			c->c_connid, c->c_sd, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
812
813
		/* update state to closing */
		c->c_conn_state = SLAP_C_CLOSING;
814
		c->c_close_reason = why;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
815

816
		/* don't listen on this port anymore */
Howard Chu's avatar
Howard Chu committed
817
		slapd_clr_read( c->c_sd, 0 );
818
819

		/* abandon active operations */
820
		connection_abandon( c );
821
822

		/* wake write blocked operations */
823
		connection_wake_writers( c );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
824

825
826
827
	} else if( why == NULL && c->c_close_reason == conn_lost_str ) {
		/* Client closed connection after doing Unbind. */
		c->c_close_reason = NULL;
828
829
830
	}
}

831
832
static void
connection_close( Connection *c )
833
834
835
{
	assert( connections != NULL );
	assert( c != NULL );
Howard Chu's avatar
Howard Chu committed
836
837
838

	if ( c->c_struct_state != SLAP_C_USED ) return;

Howard Chu's avatar
Howard Chu committed
839
	assert( c->c_conn_state == SLAP_C_CLOSING );
840

841
	/* NOTE: c_mutex should be locked by caller */
842

843
844
845
	if ( !LDAP_STAILQ_EMPTY(&c->c_ops) ||
		!LDAP_STAILQ_EMPTY(&c->c_pending_ops) )
	{
846
		Debug( LDAP_DEBUG_CONNS,