connection.c 49.8 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-2009 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
#ifdef LDAP_SLAPI
43
#include "slapi/slapi.h"
44
45
#endif

46
47
48
/* protected by connections_mutex */
static ldap_pvt_thread_mutex_t connections_mutex;
static Connection *connections = NULL;
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
49

50
static ldap_pvt_thread_mutex_t conn_nextid_mutex;
51
static unsigned long conn_nextid = 0;
52

53
54
static const char conn_lost_str[] = "connection lost";

55
/* structure state (protected by connections_mutex) */
56
57
58
#define SLAP_C_UNINITIALIZED	0x00	/* MUST BE ZERO (0) */
#define SLAP_C_UNUSED			0x01
#define SLAP_C_USED				0x02
Howard Chu's avatar
Howard Chu committed
59
#define	SLAP_C_PENDING			0x03
60
61

/* connection state (protected by c_mutex ) */
62
63
64
65
66
#define SLAP_C_INVALID			0x00	/* MUST BE ZERO (0) */
#define SLAP_C_INACTIVE			0x01	/* zero threads */
#define SLAP_C_ACTIVE			0x02	/* one or more threads */
#define SLAP_C_BINDING			0x03	/* binding */
#define SLAP_C_CLOSING			0x04	/* closing */
67
#define SLAP_C_CLIENT			0x05	/* outbound client conn */
68

69
70
71
const char *
connection_state2str( int state )
{
72
	switch( state ) {
73
74
75
	case SLAP_C_INVALID:	return "!";
	case SLAP_C_INACTIVE:	return "|";
	case SLAP_C_ACTIVE:		return "";
76
	case SLAP_C_BINDING:	return "B";
77
78
	case SLAP_C_CLOSING:	return "C";
	case SLAP_C_CLIENT:		return "L";
79
80
81
82
83
	}

	return "?";
}

84
static Connection* connection_get( ber_socket_t s );
85

86
87
88
89
typedef struct conn_readinfo {
	Operation *op;
	ldap_pvt_thread_start_t *func;
	void *arg;
Howard Chu's avatar
Howard Chu committed
90
	void *ctx;
91
92
93
94
	int nullop;
} conn_readinfo;

static int connection_input( Connection *c, conn_readinfo *cri );
95
static void connection_close( Connection *c );
96

97
static int connection_op_activate( Operation *op );
98
static void connection_op_queue( Operation *op );
99
static int connection_resched( Connection *conn );
100
static void connection_abandon( Connection *conn );
101
static void connection_destroy( Connection *c );
102

103
104
static ldap_pvt_thread_start_t connection_operation;

105
106
107
108
109
/*
 * Initialize connection management infrastructure.
 */
int connections_init(void)
{
110
111
	int i;

112
113
	assert( connections == NULL );

114
	if( connections != NULL) {
115
116
117
118
119
120
121
		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 );
122
	ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
123

124
	connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
125
126

	if( connections == NULL ) {
127
128
		Debug( LDAP_DEBUG_ANY, "connections_init: "
			"allocation (%d*%ld) of connection array failed\n",
129
			dtblsize, (long) sizeof(Connection), 0 );
130
131
132
		return -1;
	}

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
133
	assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
134
	assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
135

136
	for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
137

138
139
140
141
142
143
144
145
	/*
	 * per entry initialization of the Connection array initialization
	 * will be done by connection_init()
	 */ 

	return 0;
}

146
147
148
/*
 * Destroy connection management infrastructure.
 */
149

150
151
int connections_destroy(void)
{
152
	ber_socket_t i;
153
154
155
156
157
158
159
160
161

	/* should check return of every call */

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

162
	for ( i = 0; i < dtblsize; i++ ) {
163
		if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
164
			ber_sockbuf_free( connections[i].c_sb );
165
			ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
166
167
168
169
			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 );
170
#ifdef LDAP_SLAPI
171
			if ( slapi_plugins_used ) {
172
173
				slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
					&connections[i] );
Howard Chu's avatar
Howard Chu committed
174
			}
175
#endif
176
		}
177
178
179
180
181
182
	}

	free( connections );
	connections = NULL;

	ldap_pvt_thread_mutex_destroy( &connections_mutex );
183
	ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
184
185
186
187
188
189
190
191
	return 0;
}

/*
 * shutdown all connections
 */
int connections_shutdown(void)
{
192
	ber_socket_t i;
193

194
	for ( i = 0; i < dtblsize; i++ ) {
195
196
197
198
199
200
201
202
203
204
205
206
207
		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
208
			}
209
			ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
Howard Chu's avatar
Howard Chu committed
210
		}
211
212
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
213
	return 0;
214
215
}

216
217
218
219
220
221
222
223
224
/*
 * Timeout idle connections.
 */
int connections_timeout_idle(time_t now)
{
	int i = 0;
	int connindex;
	Connection* c;

Gary Williams's avatar
Gary Williams committed
225
	for( c = connection_first( &connindex );
226
227
228
		c != NULL;
		c = connection_next( c, &connindex ) )
	{
229
		/* Don't timeout a slow-running request or a persistent
230
231
232
233
234
		 * 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 ) {
235
236
			continue;
		}
237

238
		if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
239
			/* close it */
240
			connection_closing( c, "idletimeout" );
241
242
243
244
245
246
247
248
249
			connection_close( c );
			i++;
		}
	}
	connection_done( c );

	return i;
}

250
static Connection* connection_get( ber_socket_t s )
251
{
252
253
254
	Connection *c;

	Debug( LDAP_DEBUG_ARGS,
255
256
		"connection_get(%ld)\n",
		(long) s, 0, 0 );
257
258
259

	assert( connections != NULL );

260
	if(s == AC_SOCKET_INVALID) return NULL;
261

Howard Chu's avatar
Howard Chu committed
262
263
	assert( s < dtblsize );
	c = &connections[s];
264

265
	if( c != NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
266
267
		ldap_pvt_thread_mutex_lock( &c->c_mutex );

268
269
		assert( c->c_struct_state != SLAP_C_UNINITIALIZED );

270
271
		if( c->c_struct_state != SLAP_C_USED ) {
			/* connection must have been closed due to resched */
272

273
			assert( c->c_conn_state == SLAP_C_INVALID );
Howard Chu's avatar
Howard Chu committed
274
			assert( c->c_sd == AC_SOCKET_INVALID );
275

276
			Debug( LDAP_DEBUG_CONNS,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
277
278
				"connection_get(%d): connection not used\n",
				s, 0, 0 );
279

280
			ldap_pvt_thread_mutex_unlock( &c->c_mutex );
281
282
283
			return NULL;
		}

284
		Debug( LDAP_DEBUG_TRACE,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
285
			"connection_get(%d): got connid=%lu\n",
286
			s, c->c_connid, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
287

288
289
		c->c_n_get++;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
290
291
		assert( c->c_struct_state == SLAP_C_USED );
		assert( c->c_conn_state != SLAP_C_INVALID );
Howard Chu's avatar
Howard Chu committed
292
		assert( c->c_sd != AC_SOCKET_INVALID );
293

294
#ifndef SLAPD_MONITOR
295
		if ( global_idletimeout > 0 )
296
297
#endif /* ! SLAPD_MONITOR */
		{
298
299
			c->c_activitytime = slap_get_time();
		}
300
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
301

302
303
304
305
306
307
308
309
	return c;
}

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

310
Connection * connection_init(
311
	ber_socket_t s,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
312
	Listener *listener,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
313
314
	const char* dnsname,
	const char* peername,
315
	int flags,
316
	slap_ssf_t ssf,
Howard Chu's avatar
Howard Chu committed
317
	struct berval *authid
318
	LDAP_PF_LOCAL_SENDMSG_ARG(struct berval *peerbv))
319
{
320
	unsigned long id;
321
	Connection *c;
Howard Chu's avatar
Howard Chu committed
322
	int doinit = 0;
323
	ber_socket_t sfd = SLAP_FD2SOCK(s);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
324

325
326
	assert( connections != NULL );

Pierangelo Masarati's avatar
Pierangelo Masarati committed
327
	assert( listener != NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
328
329
330
	assert( dnsname != NULL );
	assert( peername != NULL );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
331
#ifndef HAVE_TLS
Howard Chu's avatar
Howard Chu committed
332
	assert( !( flags & CONN_IS_TLS ));
Kurt Zeilenga's avatar
Kurt Zeilenga committed
333
334
#endif

335
	if( s == AC_SOCKET_INVALID ) {
Gary Williams's avatar
Gary Williams committed
336
		Debug( LDAP_DEBUG_ANY,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
337
			"connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
338
		return NULL;
339
340
341
	}

	assert( s >= 0 );
342
	assert( s < dtblsize );
Howard Chu's avatar
Howard Chu committed
343
344
345
346
347
348
	c = &connections[s];
	if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
		doinit = 1;
	} else {
		assert( c->c_struct_state == SLAP_C_UNUSED );
	}
349

Howard Chu's avatar
Howard Chu committed
350
	if( doinit ) {
351
352
353
354
		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;
355
		c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
356

357
358
359
		BER_BVZERO( &c->c_authmech );
		BER_BVZERO( &c->c_dn );
		BER_BVZERO( &c->c_ndn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
360

Pierangelo Masarati's avatar
Pierangelo Masarati committed
361
		c->c_listener = NULL;
362
363
		BER_BVZERO( &c->c_peer_domain );
		BER_BVZERO( &c->c_peer_name );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
364

365
366
		LDAP_STAILQ_INIT(&c->c_ops);
		LDAP_STAILQ_INIT(&c->c_pending_ops);
367

Kurt Zeilenga's avatar
Kurt Zeilenga committed
368
#ifdef LDAP_X_TXN
Kurt Zeilenga's avatar
Kurt Zeilenga committed
369
		c->c_txn = CONN_TXN_INACTIVE;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
370
371
372
373
		c->c_txn_backend = NULL;
		LDAP_STAILQ_INIT(&c->c_txn_ops);
#endif

374
		BER_BVZERO( &c->c_sasl_bind_mech );
375
376
377
		c->c_sasl_done = 0;
		c->c_sasl_authctx = NULL;
		c->c_sasl_sockctx = NULL;
378
		c->c_sasl_extra = NULL;
379
		c->c_sasl_bindop = NULL;
380

381
382
383
		c->c_sb = ber_sockbuf_alloc( );

		{
384
			ber_len_t max = sockbuf_max_incoming;
385
386
387
			ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
388
		c->c_currentber = NULL;
389

390
391
		/* should check status of thread calls */
		ldap_pvt_thread_mutex_init( &c->c_mutex );
392
393
394
395
		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 );
396

397
#ifdef LDAP_SLAPI
398
		if ( slapi_plugins_used ) {
399
			slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
400
		}
401
#endif
402
	}
403

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

406
407
408
	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
409
	assert( c->c_listener == NULL );
410
411
	assert( BER_BVISNULL( &c->c_peer_domain ) );
	assert( BER_BVISNULL( &c->c_peer_name ) );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
412
413
	assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
	assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
414
#ifdef LDAP_X_TXN
Kurt Zeilenga's avatar
Kurt Zeilenga committed
415
	assert( c->c_txn == CONN_TXN_INACTIVE );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
416
417
418
	assert( c->c_txn_backend == NULL );
	assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
#endif
419
	assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
420
421
422
	assert( c->c_sasl_done == 0 );
	assert( c->c_sasl_authctx == NULL );
	assert( c->c_sasl_sockctx == NULL );
423
	assert( c->c_sasl_extra == NULL );
424
	assert( c->c_sasl_bindop == NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
425
	assert( c->c_currentber == NULL );
426
	assert( c->c_writewaiter == 0);
427
	assert( c->c_writers == 0);
428

Pierangelo Masarati's avatar
Pierangelo Masarati committed
429
	c->c_listener = listener;
Howard Chu's avatar
Howard Chu committed
430
	c->c_sd = s;
431

Howard Chu's avatar
Howard Chu committed
432
	if ( flags & CONN_IS_CLIENT ) {
433
		c->c_connid = 0;
434
		ldap_pvt_thread_mutex_lock( &connections_mutex );
435
		c->c_conn_state = SLAP_C_CLIENT;
436
		c->c_struct_state = SLAP_C_USED;
437
		ldap_pvt_thread_mutex_unlock( &connections_mutex );
438
		c->c_close_reason = "?";			/* should never be needed */
439
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &sfd );
440
441
		ldap_pvt_thread_mutex_unlock( &c->c_mutex );

442
		return c;
443
444
	}

445
446
	ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
	ber_str2bv( peername, 0, 1, &c->c_peer_name );
447

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
448
449
450
451
	c->c_n_ops_received = 0;
	c->c_n_ops_executing = 0;
	c->c_n_ops_pending = 0;
	c->c_n_ops_completed = 0;
452
453
454
455

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

457
458
	/* set to zero until bind, implies LDAP_VERSION3 */
	c->c_protocol = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
459

460
#ifndef SLAPD_MONITOR
461
	if ( global_idletimeout > 0 )
462
463
#endif /* ! SLAPD_MONITOR */
	{
464
465
		c->c_activitytime = c->c_starttime = slap_get_time();
	}
466

467
468
#ifdef LDAP_CONNECTIONLESS
	c->c_is_udp = 0;
Howard Chu's avatar
Howard Chu committed
469
	if( flags & CONN_IS_UDP ) {
470
471
		c->c_is_udp = 1;
#ifdef LDAP_DEBUG
Kurt Zeilenga's avatar
Kurt Zeilenga committed
472
473
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
			LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
474
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
475
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
476
			LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
477
478
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
			LBER_SBIOD_LEVEL_PROVIDER, NULL );
479
	} else
Howard Chu's avatar
Howard Chu committed
480
481
482
483
484
485
#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_" );
486
#endif
Howard Chu's avatar
Howard Chu committed
487
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_fd,
488
			LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
Howard Chu's avatar
Howard Chu committed
489
490
491
492
493
494
#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 */
495
	{
496
#ifdef LDAP_DEBUG
Kurt Zeilenga's avatar
Kurt Zeilenga committed
497
498
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
			LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
499
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
500
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
501
			LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
502
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
503
504
505
506

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
509
510
511
512
513
	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
514
			s, c->c_peer_name.bv_val, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
515
	}
516

517
	ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
518
	id = c->c_connid = conn_nextid++;
519
	ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
520

521
	ldap_pvt_thread_mutex_lock( &connections_mutex );
522
	c->c_conn_state = SLAP_C_INACTIVE;
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
523
	c->c_struct_state = SLAP_C_USED;
524
	ldap_pvt_thread_mutex_unlock( &connections_mutex );
525
	c->c_close_reason = "?";			/* should never be needed */
526

527
528
529
	c->c_ssf = c->c_transport_ssf = ssf;
	c->c_tls_ssf = 0;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
530
#ifdef HAVE_TLS
Howard Chu's avatar
Howard Chu committed
531
	if ( flags & CONN_IS_TLS ) {
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
532
533
534
535
536
537
		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
538
#endif
539

540
	slap_sasl_open( c, 0 );
541
	slap_sasl_external( c, ssf, authid );
542

543
	slapd_add_internal( s, 1 );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
544
	ldap_pvt_thread_mutex_unlock( &c->c_mutex );
545

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
546
	backend_connection_init(c);
547

548
	return c;
549
550
}

551
552
void connection2anonymous( Connection *c )
{
553
554
	assert( connections != NULL );
	assert( c != NULL );
555

556
	{
557
		ber_len_t max = sockbuf_max_incoming;
558
559
560
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
	}

561
	if ( !BER_BVISNULL( &c->c_authmech ) ) {
562
		ch_free(c->c_authmech.bv_val);
563
	}
564
	BER_BVZERO( &c->c_authmech );
565

566
	if ( !BER_BVISNULL( &c->c_dn ) ) {
567
		ch_free(c->c_dn.bv_val);
568
	}
569
	BER_BVZERO( &c->c_dn );
570

571
	if ( !BER_BVISNULL( &c->c_ndn ) ) {
572
		ch_free(c->c_ndn.bv_val);
573
	}
574
	BER_BVZERO( &c->c_ndn );
575

576
577
	if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
		ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
578
579
	}
	BER_BVZERO( &c->c_sasl_authz_dn );
580
581
582
583

	c->c_authz_backend = NULL;
}

584
585
586
static void
connection_destroy( Connection *c )
{
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
587
	unsigned long	connid;
588
	const char		*close_reason;
Howard Chu's avatar
Howard Chu committed
589
	Sockbuf			*sb;
Howard Chu's avatar
Howard Chu committed
590
	ber_socket_t	sd;
591

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
592
593
594
595
596
	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
597
598
599
600
601
602
	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
603
	assert( c->c_writewaiter == 0);
604
	assert( c->c_writers == 0);
605

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

Howard Chu's avatar
Howard Chu committed
610
611
612
613
	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
614
	backend_connection_destroy(c);
615

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
616
617
	c->c_protocol = 0;
	c->c_connid = -1;
618

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
619
	c->c_activitytime = c->c_starttime = 0;
620

621
	connection2anonymous( c );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
622
	c->c_listener = NULL;
623

624
625
	if(c->c_peer_domain.bv_val != NULL) {
		free(c->c_peer_domain.bv_val);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
626
	}
627
	BER_BVZERO( &c->c_peer_domain );
628
629
	if(c->c_peer_name.bv_val != NULL) {
		free(c->c_peer_name.bv_val);
630
	}
631
	BER_BVZERO( &c->c_peer_name );
632

633
	c->c_sasl_bind_in_progress = 0;
634
635
	if(c->c_sasl_bind_mech.bv_val != NULL) {
		free(c->c_sasl_bind_mech.bv_val);
636
	}
637
	BER_BVZERO( &c->c_sasl_bind_mech );
638
639

	slap_sasl_close( c );
640

Kurt Zeilenga's avatar
Kurt Zeilenga committed
641
642
643
644
645
	if ( c->c_currentber != NULL ) {
		ber_free( c->c_currentber, 1 );
		c->c_currentber = NULL;
	}

646

Howard Chu's avatar
Howard Chu committed
647
648
649
650
#ifdef LDAP_SLAPI
	/* call destructors, then constructors; avoids unnecessary allocation */
	if ( slapi_plugins_used ) {
		slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
651
	}
Howard Chu's avatar
Howard Chu committed
652
#endif
653

Howard Chu's avatar
Howard Chu committed
654
655
	sd = c->c_sd;
	c->c_sd = AC_SOCKET_INVALID;
Howard Chu's avatar
Howard Chu committed
656
657
658
	c->c_conn_state = SLAP_C_INVALID;
	c->c_struct_state = SLAP_C_UNUSED;
	c->c_close_reason = "?";			/* should never be needed */
659

Howard Chu's avatar
Howard Chu committed
660
661
	sb = c->c_sb;
	c->c_sb = ber_sockbuf_alloc( );
662
	{
663
		ber_len_t max = sockbuf_max_incoming;
664
665
666
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
	}

Howard Chu's avatar
Howard Chu committed
667
668
669
	/* 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
670
671
	if ( sd != AC_SOCKET_INVALID ) {
		slapd_remove( sd, sb, 1, 0, 0 );
Howard Chu's avatar
Howard Chu committed
672

673
		if ( close_reason == NULL ) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
674
			Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed\n",
Howard Chu's avatar
Howard Chu committed
675
				connid, (long) sd, 0, 0, 0 );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
676
677
		} else {
			Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
Howard Chu's avatar
Howard Chu committed
678
				connid, (long) sd, close_reason, 0, 0 );
679
		}
680
	}
681
682
}

683
684
int connection_state_closing( Connection *c )
{
685
686
	/* c_mutex must be locked by caller */

687
	int state;
688
689
690
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );

691
692
693
694
695
	state = c->c_conn_state;

	assert( state != SLAP_C_INVALID );

	return state == SLAP_C_CLOSING;
696
697
}

698
699
700
701
static void connection_abandon( Connection *c )
{
	/* c_mutex must be locked by caller */

702
	Operation *o, *next, op = {0};
Howard Chu's avatar
Howard Chu committed
703
	Opheader ohdr = {0};
704
	SlapReply rs = {0};
705

Howard Chu's avatar
Howard Chu committed
706
707
708
	op.o_hdr = &ohdr;
	op.o_conn = c;
	op.o_connid = c->c_connid;
709
	op.o_tag = LDAP_REQ_ABANDON;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
710

711
712
713
	for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
		next = LDAP_STAILQ_NEXT( o, o_next );
		op.orn_msgid = o->o_msgid;
714
		o->o_abandon = 1;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
715
		op.o_bd = frontendDB;
716
		frontendDB->be_abandon( &op, &rs );
717
718
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
719
720
721
722
723
#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
724
		slap_op_free( o, NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
725
726
727
728
729
730
731
	}

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

732
	/* remove pending operations */
733
734
735
	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
736
		slap_op_free( o, NULL );
737
738
739
	}
}

740
void connection_closing( Connection *c, const char *why )
741
742
743
744
745
746
{
	assert( connections != NULL );
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );
	assert( c->c_conn_state != SLAP_C_INVALID );

747
748
	/* c_mutex must be locked by caller */

749
	if( c->c_conn_state != SLAP_C_CLOSING ) {
750
		Debug( LDAP_DEBUG_CONNS,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
751
			"connection_closing: readying conn=%lu sd=%d for close\n",
Howard Chu's avatar
Howard Chu committed
752
			c->c_connid, c->c_sd, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
753
754
		/* update state to closing */
		c->c_conn_state = SLAP_C_CLOSING;
755
		c->c_close_reason = why;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
756

757
		/* don't listen on this port anymore */
Howard Chu's avatar
Howard Chu committed
758
		slapd_clr_read( c->c_sd, 0 );
759
760

		/* abandon active operations */
761
		connection_abandon( c );
762
763

		/* wake write blocked operations */
764
		ldap_pvt_thread_mutex_lock( &c->c_write1_mutex );
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
		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 );
780
		} else {
781
			ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
Howard Chu's avatar
Howard Chu committed
782
			slapd_clr_write( c->c_sd, 1 );
783
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
784

785
786
787
	} else if( why == NULL && c->c_close_reason == conn_lost_str ) {
		/* Client closed connection after doing Unbind. */
		c->c_close_reason = NULL;
788
789
790
	}
}

791
792
static void
connection_close( Connection *c )
793
794
795
796
{
	assert( connections != NULL );
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );
Howard Chu's avatar
Howard Chu committed
797
	assert( c->c_conn_state == SLAP_C_CLOSING );
798

799
	/* NOTE: c_mutex should be locked by caller */
800

801
802
803
	if ( !LDAP_STAILQ_EMPTY(&c->c_ops) ||
		!LDAP_STAILQ_EMPTY(&c->c_pending_ops) )
	{
804
		Debug( LDAP_DEBUG_CONNS,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
805
			"connection_close: deferring conn=%lu sd=%d\n",
Howard Chu's avatar
Howard Chu committed
806
			c->c_connid, c->c_sd, 0 );
807
808
809
		return;
	}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
810
	Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
Howard Chu's avatar
Howard Chu committed
811
		c->c_connid, c->c_sd, 0 );
812

813
814
815
	connection_destroy( c );
}

816
unsigned long connections_nextid(void)
817
{
818
	unsigned long id;
819
820
	assert( connections != NULL );

821
	ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
822
823
824

	id = conn_nextid;

825
	ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
826
827
828
829

	return id;
}

830
Connection* connection_first( ber_socket_t *index )
831
832
{
	assert( connections != NULL );
833
	assert( index != NULL );
834