connection.c 47.7 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-2006 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
#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
87
88
89
90
91
92
93
94
95

typedef struct conn_readinfo {
	Operation *op;
	ldap_pvt_thread_start_t *func;
	void *arg;
	int nullop;
} conn_readinfo;

static int connection_input( Connection *c, conn_readinfo *cri );
96
#else
97
static int connection_input( Connection *c );
98
#endif
99
static void connection_close( Connection *c );
100

101
static int connection_op_activate( Operation *op );
102
#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
103
104
static void connection_op_queue( Operation *op );
#endif
105
static int connection_resched( Connection *conn );
106
static void connection_abandon( Connection *conn );
107
static void connection_destroy( Connection *c );
108

109
110
static ldap_pvt_thread_start_t connection_operation;

111
112
113
114
115
/*
 * Initialize connection management infrastructure.
 */
int connections_init(void)
{
116
117
	int i;

118
119
	assert( connections == NULL );

120
	if( connections != NULL) {
121
122
123
124
125
126
127
		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 );
128
	ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
129

130
	connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
131
132

	if( connections == NULL ) {
133
134
		Debug( LDAP_DEBUG_ANY, "connections_init: "
			"allocation (%d*%ld) of connection array failed\n",
135
			dtblsize, (long) sizeof(Connection), 0 );
136
137
138
		return -1;
	}

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
139
	assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
140
	assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
141

142
	for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
143

144
145
146
147
148
149
150
151
	/*
	 * per entry initialization of the Connection array initialization
	 * will be done by connection_init()
	 */ 

	return 0;
}

152
153
154
/*
 * Destroy connection management infrastructure.
 */
155

156
157
int connections_destroy(void)
{
158
	ber_socket_t i;
159
160
161
162
163
164
165
166
167

	/* should check return of every call */

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

168
	for ( i = 0; i < dtblsize; i++ ) {
169
		if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
170
			ber_sockbuf_free( connections[i].c_sb );
171
172
173
			ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
			ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
			ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
174
#ifdef LDAP_SLAPI
175
			if ( slapi_plugins_used ) {
176
177
				slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
					&connections[i] );
Howard Chu's avatar
Howard Chu committed
178
			}
179
#endif
180
		}
181
182
183
184
185
186
	}

	free( connections );
	connections = NULL;

	ldap_pvt_thread_mutex_destroy( &connections_mutex );
187
	ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
188
189
190
191
192
193
194
195
	return 0;
}

/*
 * shutdown all connections
 */
int connections_shutdown(void)
{
196
	ber_socket_t i;
197

198
	for ( i = 0; i < dtblsize; i++ ) {
199
200
201
		if( connections[i].c_struct_state != SLAP_C_USED ) {
			continue;
		}
202
203
		/* give persistent clients a chance to cleanup */
		if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
204
			ldap_pvt_thread_pool_submit( &connection_pool,
205
206
207
			connections[i].c_clientfunc, connections[i].c_clientarg );
			continue;
		}
208
209

		ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
210

Howard Chu's avatar
Howard Chu committed
211
		/* c_mutex is locked */
212
		connection_closing( &connections[i], "slapd shutdown" );
213
		connection_close( &connections[i] );
214

215
216
217
		ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
218
	return 0;
219
220
}

221
222
223
224
225
226
227
228
229
/*
 * Timeout idle connections.
 */
int connections_timeout_idle(time_t now)
{
	int i = 0;
	int connindex;
	Connection* c;

Gary Williams's avatar
Gary Williams committed
230
	for( c = connection_first( &connindex );
231
232
233
		c != NULL;
		c = connection_next( c, &connindex ) )
	{
234
235
		/* Don't timeout a slow-running request or a persistent
		 * outbound connection */
236
237
238
		if( c->c_n_ops_executing || c->c_conn_state == SLAP_C_CLIENT ) {
			continue;
		}
239

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

	return i;
}

252
static Connection* connection_get( ber_socket_t s )
253
{
254
255
256
	Connection *c;

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

	assert( connections != NULL );

262
	if(s == AC_SOCKET_INVALID) return NULL;
263
264

#ifndef HAVE_WINSOCK
Howard Chu's avatar
Howard Chu committed
265
266
	assert( s < dtblsize );
	c = &connections[s];
267

268
#else
269
	c = NULL;
270
	{
271
		ber_socket_t i, sd;
272

Howard Chu's avatar
Howard Chu committed
273
		ldap_pvt_thread_mutex_lock( &connections_mutex );
274
		for(i=0; i<dtblsize; i++) {
Howard Chu's avatar
Howard Chu committed
275
276
277
			if( connections[i].c_struct_state == SLAP_C_PENDING )
				continue;

Gary Williams's avatar
NT port    
Gary Williams committed
278
			if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
279
				assert( connections[i].c_conn_state == SLAP_C_INVALID );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
280
				assert( connections[i].c_sb == 0 );
281
282
283
				break;
			}

284
285
286
			ber_sockbuf_ctrl( connections[i].c_sb,
				LBER_SB_OPT_GET_FD, &sd );

Gary Williams's avatar
NT port    
Gary Williams committed
287
			if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
288
				assert( connections[i].c_conn_state == SLAP_C_INVALID );
289
				assert( sd == AC_SOCKET_INVALID );
290
291
292
				continue;
			}

293
294
295
			/* state can actually change from used -> unused by resched,
			 * so don't assert details here.
			 */
296

297
			if( sd == s ) {
Gary Williams's avatar
NT port    
Gary Williams committed
298
				c = &connections[i];
299
300
301
				break;
			}
		}
Howard Chu's avatar
Howard Chu committed
302
		ldap_pvt_thread_mutex_unlock( &connections_mutex );
303
304
	}
#endif
305

306
	if( c != NULL ) {
307
308
		ber_socket_t	sd;

Howard Chu's avatar
Cleanup    
Howard Chu committed
309
310
		assert( c->c_struct_state != SLAP_C_UNINITIALIZED );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
311
312
		ldap_pvt_thread_mutex_lock( &c->c_mutex );

313
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
314
315
		if( c->c_struct_state != SLAP_C_USED ) {
			/* connection must have been closed due to resched */
316

317
			assert( c->c_conn_state == SLAP_C_INVALID );
318
			assert( sd == AC_SOCKET_INVALID );
319
320

			Debug( LDAP_DEBUG_TRACE,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
321
322
				"connection_get(%d): connection not used\n",
				s, 0, 0 );
323

324
			ldap_pvt_thread_mutex_unlock( &c->c_mutex );
325
326
327
			return NULL;
		}

328
		Debug( LDAP_DEBUG_TRACE,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
329
			"connection_get(%d): got connid=%lu\n",
330
			s, c->c_connid, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
331

332
333
		c->c_n_get++;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
334
335
		assert( c->c_struct_state == SLAP_C_USED );
		assert( c->c_conn_state != SLAP_C_INVALID );
336
		assert( sd != AC_SOCKET_INVALID );
337

338
#ifndef SLAPD_MONITOR
339
		if ( global_idletimeout > 0 )
340
341
#endif /* ! SLAPD_MONITOR */
		{
342
343
			c->c_activitytime = slap_get_time();
		}
344
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
345

346
347
348
349
350
351
352
353
354
	return c;
}

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

long connection_init(
355
	ber_socket_t s,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
356
	Listener *listener,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
357
358
	const char* dnsname,
	const char* peername,
359
	int flags,
360
	slap_ssf_t ssf,
361
	struct berval *authid )
362
{
363
	unsigned long id;
364
	Connection *c;
Howard Chu's avatar
Howard Chu committed
365
	int doinit = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
366

367
368
	assert( connections != NULL );

Pierangelo Masarati's avatar
Pierangelo Masarati committed
369
	assert( listener != NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
370
371
372
	assert( dnsname != NULL );
	assert( peername != NULL );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
373
#ifndef HAVE_TLS
374
	assert( flags != CONN_IS_TLS );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
375
376
#endif

377
	if( s == AC_SOCKET_INVALID ) {
Gary Williams's avatar
Gary Williams committed
378
		Debug( LDAP_DEBUG_ANY,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
379
			"connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
380
381
382
383
384
		return -1;
	}

	assert( s >= 0 );
#ifndef HAVE_WINSOCK
385
	assert( s < dtblsize );
Howard Chu's avatar
Howard Chu committed
386
387
388
389
390
391
	c = &connections[s];
	if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
		doinit = 1;
	} else {
		assert( c->c_struct_state == SLAP_C_UNUSED );
	}
392
393
#else
	{
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
394
		ber_socket_t i;
395
396
		c = NULL;

Howard Chu's avatar
Howard Chu committed
397
		ldap_pvt_thread_mutex_lock( &connections_mutex );
398
		for( i=0; i < dtblsize; i++) {
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
399
			ber_socket_t	sd;
400

Howard Chu's avatar
Howard Chu committed
401
402
403
			if ( connections[i].c_struct_state == SLAP_C_PENDING )
				continue;

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
404
405
406
			if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
				assert( connections[i].c_sb == 0 );
				c = &connections[i];
Howard Chu's avatar
Howard Chu committed
407
408
				c->c_struct_state = SLAP_C_PENDING;
				doinit = 1;
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
409
410
				break;
			}
Gary Williams's avatar
NT port    
Gary Williams committed
411

412
			sd = AC_SOCKET_INVALID;
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
413
414
415
416
			if (connections[i].c_sb != NULL) {
				ber_sockbuf_ctrl( connections[i].c_sb,
					LBER_SB_OPT_GET_FD, &sd );
			}
Gary Williams's avatar
Gary Williams committed
417

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
418
419
420
			if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
				assert( sd == AC_SOCKET_INVALID );
				c = &connections[i];
Howard Chu's avatar
Howard Chu committed
421
				c->c_struct_state = SLAP_C_PENDING;
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
422
423
				break;
			}
Gary Williams's avatar
Gary Williams committed
424

425
			if( connections[i].c_conn_state == SLAP_C_CLIENT ) continue;
426

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
427
428
429
430
			assert( connections[i].c_struct_state == SLAP_C_USED );
			assert( connections[i].c_conn_state != SLAP_C_INVALID );
			assert( sd != AC_SOCKET_INVALID );
		}
Howard Chu's avatar
Howard Chu committed
431
		ldap_pvt_thread_mutex_unlock( &connections_mutex );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
432
433
434

		if( c == NULL ) {
			Debug( LDAP_DEBUG_ANY,
435
				"connection_init(%d): connection table full "
436
				"(%d/%d)\n", s, i, dtblsize);
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
437
438
			return -1;
		}
Gary Williams's avatar
Gary Williams committed
439
	}
440
441
#endif

Howard Chu's avatar
Howard Chu committed
442
	if( doinit ) {
443
444
445
446
		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;
447
		c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
448

449
450
451
		BER_BVZERO( &c->c_authmech );
		BER_BVZERO( &c->c_dn );
		BER_BVZERO( &c->c_ndn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
452

Pierangelo Masarati's avatar
Pierangelo Masarati committed
453
		c->c_listener = NULL;
454
455
		BER_BVZERO( &c->c_peer_domain );
		BER_BVZERO( &c->c_peer_name );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
456

457
458
		LDAP_STAILQ_INIT(&c->c_ops);
		LDAP_STAILQ_INIT(&c->c_pending_ops);
459

Kurt Zeilenga's avatar
Kurt Zeilenga committed
460
#ifdef LDAP_X_TXN
Kurt Zeilenga's avatar
Kurt Zeilenga committed
461
		c->c_txn = CONN_TXN_INACTIVE;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
462
463
464
465
		c->c_txn_backend = NULL;
		LDAP_STAILQ_INIT(&c->c_txn_ops);
#endif

466
		BER_BVZERO( &c->c_sasl_bind_mech );
467
468
469
		c->c_sasl_done = 0;
		c->c_sasl_authctx = NULL;
		c->c_sasl_sockctx = NULL;
470
		c->c_sasl_extra = NULL;
471
		c->c_sasl_bindop = NULL;
472

473
474
475
		c->c_sb = ber_sockbuf_alloc( );

		{
476
			ber_len_t max = sockbuf_max_incoming;
477
478
479
			ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
480
		c->c_currentber = NULL;
481

482
483
484
485
		/* should check status of thread calls */
		ldap_pvt_thread_mutex_init( &c->c_mutex );
		ldap_pvt_thread_mutex_init( &c->c_write_mutex );
		ldap_pvt_thread_cond_init( &c->c_write_cv );
486

487
#ifdef LDAP_SLAPI
488
		if ( slapi_plugins_used ) {
489
			slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
490
		}
491
#endif
492
	}
493

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

496
497
498
	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
499
	assert( c->c_listener == NULL );
500
501
	assert( BER_BVISNULL( &c->c_peer_domain ) );
	assert( BER_BVISNULL( &c->c_peer_name ) );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
502
503
	assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
	assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
504
#ifdef LDAP_X_TXN
Kurt Zeilenga's avatar
Kurt Zeilenga committed
505
	assert( c->c_txn == CONN_TXN_INACTIVE );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
506
507
508
	assert( c->c_txn_backend == NULL );
	assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
#endif
509
	assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
510
511
512
	assert( c->c_sasl_done == 0 );
	assert( c->c_sasl_authctx == NULL );
	assert( c->c_sasl_sockctx == NULL );
513
	assert( c->c_sasl_extra == NULL );
514
	assert( c->c_sasl_bindop == NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
515
	assert( c->c_currentber == NULL );
516
	assert( c->c_writewaiter == 0);
517

Pierangelo Masarati's avatar
Pierangelo Masarati committed
518
	c->c_listener = listener;
519
520
521
522

	if ( flags == CONN_IS_CLIENT ) {
		c->c_conn_state = SLAP_C_CLIENT;
		c->c_struct_state = SLAP_C_USED;
523
		c->c_close_reason = "?";			/* should never be needed */
524
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
525
526
527
528
529
		ldap_pvt_thread_mutex_unlock( &c->c_mutex );

		return 0;
	}

530
531
	ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
	ber_str2bv( peername, 0, 1, &c->c_peer_name );
532

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
533
534
535
536
	c->c_n_ops_received = 0;
	c->c_n_ops_executing = 0;
	c->c_n_ops_pending = 0;
	c->c_n_ops_completed = 0;
537
538
539
540

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

542
543
	/* set to zero until bind, implies LDAP_VERSION3 */
	c->c_protocol = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
544

545
#ifndef SLAPD_MONITOR
546
	if ( global_idletimeout > 0 )
547
548
#endif /* ! SLAPD_MONITOR */
	{
549
550
		c->c_activitytime = c->c_starttime = slap_get_time();
	}
551

552
553
#ifdef LDAP_CONNECTIONLESS
	c->c_is_udp = 0;
554
	if( flags == CONN_IS_UDP ) {
555
556
		c->c_is_udp = 1;
#ifdef LDAP_DEBUG
Kurt Zeilenga's avatar
Kurt Zeilenga committed
557
558
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
			LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
559
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
560
561
562
563
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
			LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
			LBER_SBIOD_LEVEL_PROVIDER, NULL );
564
565
566
	} else
#endif
	{
567
#ifdef LDAP_DEBUG
Kurt Zeilenga's avatar
Kurt Zeilenga committed
568
569
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
			LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
570
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
571
572
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
			LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
573
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
574
575
576
577

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
580
581
582
583
584
	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
585
			s, c->c_peer_name.bv_val, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
586
	}
587

588
	ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
589
	id = c->c_connid = conn_nextid++;
590
	ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
591

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
592
593
	c->c_conn_state = SLAP_C_INACTIVE;
	c->c_struct_state = SLAP_C_USED;
594
	c->c_close_reason = "?";			/* should never be needed */
595

596
597
598
	c->c_ssf = c->c_transport_ssf = ssf;
	c->c_tls_ssf = 0;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
599
#ifdef HAVE_TLS
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
600
601
602
603
604
605
606
	if ( flags == CONN_IS_TLS ) {
		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
607
#endif
608

609
	slap_sasl_open( c, 0 );
610
	slap_sasl_external( c, ssf, authid );
611

612
	slapd_add_internal( s, 1 );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
613
	ldap_pvt_thread_mutex_unlock( &c->c_mutex );
614

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
615
	backend_connection_init(c);
616

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
617
	return id;
618
619
}

620
621
void connection2anonymous( Connection *c )
{
622
623
	assert( connections != NULL );
	assert( c != NULL );
624

625
	{
626
		ber_len_t max = sockbuf_max_incoming;
627
628
629
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
	}

630
	if ( !BER_BVISNULL( &c->c_authmech ) ) {
631
		ch_free(c->c_authmech.bv_val);
632
	}
633
	BER_BVZERO( &c->c_authmech );
634

635
	if ( !BER_BVISNULL( &c->c_dn ) ) {
636
		ch_free(c->c_dn.bv_val);
637
	}
638
	BER_BVZERO( &c->c_dn );
639

640
	if ( !BER_BVISNULL( &c->c_ndn ) ) {
641
		ch_free(c->c_ndn.bv_val);
642
	}
643
	BER_BVZERO( &c->c_ndn );
644

645
646
	if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
		ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
647
648
	}
	BER_BVZERO( &c->c_sasl_authz_dn );
649
650
651
652

	c->c_authz_backend = NULL;
}

653
654
655
static void
connection_destroy( Connection *c )
{
656
	ber_socket_t	sd;
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
657
	unsigned long	connid;
658
	const char		*close_reason;
Howard Chu's avatar
Howard Chu committed
659
	Sockbuf			*sb;
660

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
661
662
663
664
665
	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
666
667
668
669
670
671
	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
672
	assert( c->c_writewaiter == 0);
673

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

Howard Chu's avatar
Howard Chu committed
678
679
680
681
	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
682
	backend_connection_destroy(c);
683

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
684
685
	c->c_protocol = 0;
	c->c_connid = -1;
686

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
687
	c->c_activitytime = c->c_starttime = 0;
688

689
	connection2anonymous( c );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
690
	c->c_listener = NULL;
691

692
693
	if(c->c_peer_domain.bv_val != NULL) {
		free(c->c_peer_domain.bv_val);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
694
	}
695
	BER_BVZERO( &c->c_peer_domain );
696
697
	if(c->c_peer_name.bv_val != NULL) {
		free(c->c_peer_name.bv_val);
698
	}
699
	BER_BVZERO( &c->c_peer_name );
700

701
	c->c_sasl_bind_in_progress = 0;
702
703
	if(c->c_sasl_bind_mech.bv_val != NULL) {
		free(c->c_sasl_bind_mech.bv_val);
704
	}
705
	BER_BVZERO( &c->c_sasl_bind_mech );
706
707

	slap_sasl_close( c );
708

Kurt Zeilenga's avatar
Kurt Zeilenga committed
709
710
711
712
713
	if ( c->c_currentber != NULL ) {
		ber_free( c->c_currentber, 1 );
		c->c_currentber = NULL;
	}

714

Howard Chu's avatar
Howard Chu committed
715
716
717
718
#ifdef LDAP_SLAPI
	/* call destructors, then constructors; avoids unnecessary allocation */
	if ( slapi_plugins_used ) {
		slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
719
	}
Howard Chu's avatar
Howard Chu committed
720
#endif
721

Howard Chu's avatar
Howard Chu committed
722
723
724
	c->c_conn_state = SLAP_C_INVALID;
	c->c_struct_state = SLAP_C_UNUSED;
	c->c_close_reason = "?";			/* should never be needed */
725

Howard Chu's avatar
Howard Chu committed
726
727
	sb = c->c_sb;
	c->c_sb = ber_sockbuf_alloc( );
728
	{
729
		ber_len_t max = sockbuf_max_incoming;
730
731
732
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
	}

Howard Chu's avatar
Howard Chu committed
733
734
735
736
737
738
	ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );

	/* c must be fully reset by this point; when we call slapd_remove
	 * it may get immediately reused by a new connection.
	 */
	if ( sd != AC_SOCKET_INVALID ) {
739
		slapd_remove( sd, sb, 1, 0, 0 );
Howard Chu's avatar
Howard Chu committed
740
741
742
743
744

		Statslog( LDAP_DEBUG_STATS, (close_reason
									 ? "conn=%lu fd=%ld closed (%s)\n"
									 : "conn=%lu fd=%ld closed\n"),
			connid, (long) sd, close_reason, 0, 0 );
745
	}
746
747
}

748
749
int connection_state_closing( Connection *c )
{
750
751
	/* c_mutex must be locked by caller */

752
	int state;
753
754
755
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );

756
757
758
759
760
	state = c->c_conn_state;

	assert( state != SLAP_C_INVALID );

	return state == SLAP_C_CLOSING;
761
762
}

763
764
765
766
static void connection_abandon( Connection *c )
{
	/* c_mutex must be locked by caller */

767
	Operation *o, *next, op = {0};
Howard Chu's avatar
Howard Chu committed
768
	Opheader ohdr = {0};
769
	SlapReply rs = {0};
770

Howard Chu's avatar
Howard Chu committed
771
772
773
	op.o_hdr = &ohdr;
	op.o_conn = c;
	op.o_connid = c->c_connid;
774
	op.o_tag = LDAP_REQ_ABANDON;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
775

776
777
778
	for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
		next = LDAP_STAILQ_NEXT( o, o_next );
		op.orn_msgid = o->o_msgid;
779
		o->o_abandon = 1;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
780
		op.o_bd = frontendDB;
781
		frontendDB->be_abandon( &op, &rs );
782
783
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
784
785
786
787
788
789
790
791
792
793
794
795
796
#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;
		slap_op_free( o );
	}

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

797
	/* remove pending operations */
798
799
800
	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;
801
802
803
804
		slap_op_free( o );
	}
}

805
void connection_closing( Connection *c, const char *why )
806
807
808
809
810
811
{
	assert( connections != NULL );
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );
	assert( c->c_conn_state != SLAP_C_INVALID );

812
813
	/* c_mutex must be locked by caller */

814
	if( c->c_conn_state != SLAP_C_CLOSING ) {
815
816
817
		ber_socket_t	sd;

		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
818
		Debug( LDAP_DEBUG_TRACE,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
819
			"connection_closing: readying conn=%lu sd=%d for close\n",
820
			c->c_connid, sd, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
821
822
		/* update state to closing */
		c->c_conn_state = SLAP_C_CLOSING;
823
		c->c_close_reason = why;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
824

825
		/* don't listen on this port anymore */
Howard Chu's avatar
Howard Chu committed
826
		slapd_clr_read( sd, 0 );
827
828

		/* abandon active operations */
829
		connection_abandon( c );
830
831

		/* wake write blocked operations */
832
		slapd_clr_write( sd, 1 );
833
834
835
		if ( c->c_writewaiter ) {
			ldap_pvt_thread_cond_signal( &c->c_write_cv );
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
836

837
838
839
	} else if( why == NULL && c->c_close_reason == conn_lost_str ) {
		/* Client closed connection after doing Unbind. */
		c->c_close_reason = NULL;
840
841
842
	}
}

843
844
static void
connection_close( Connection *c )
845
{
846
	ber_socket_t	sd = AC_SOCKET_INVALID;
847

848
849
850
851
852
	assert( connections != NULL );
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );
	assert( c->c_conn_state == SLAP_C_CLOSING );

853
	/* NOTE: c_mutex should be locked by caller */
854