connection.c 42.4 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-2005 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;
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
59
60

/* connection state (protected by c_mutex ) */
61
62
63
64
65
#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 */
66
#define SLAP_C_CLIENT			0x05	/* outbound client conn */
67

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

	return "?";
}

83
static Connection* connection_get( ber_socket_t s );
84
85

static int connection_input( Connection *c );
86
static void connection_close( Connection *c );
87

88
static int connection_op_activate( 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
117

	if( connections == NULL ) {
		Debug( LDAP_DEBUG_ANY,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
118
			"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
139
140
/*
 * Destroy connection management infrastructure.
 */
int connections_destroy(void)
{
141
	ber_socket_t i;
142
143
144
145
146
147
148
149
150

	/* should check return of every call */

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

151
	for ( i = 0; i < dtblsize; i++ ) {
152
		if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
153
			ber_sockbuf_free( connections[i].c_sb );
154
155
156
			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 );
157
#ifdef LDAP_SLAPI
158
			if ( slapi_plugins_used ) {
159
				slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, &connections[i] );
Howard Chu's avatar
Howard Chu committed
160
			}
161
#endif
162
		}
163
164
165
166
167
168
	}

	free( connections );
	connections = NULL;

	ldap_pvt_thread_mutex_destroy( &connections_mutex );
169
	ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
170
171
172
173
174
175
176
177
	return 0;
}

/*
 * shutdown all connections
 */
int connections_shutdown(void)
{
178
	ber_socket_t i;
179
180
181

	ldap_pvt_thread_mutex_lock( &connections_mutex );

182
	for ( i = 0; i < dtblsize; i++ ) {
183
184
185
		if( connections[i].c_struct_state != SLAP_C_USED ) {
			continue;
		}
186
187
		/* give persistent clients a chance to cleanup */
		if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
188
			ldap_pvt_thread_pool_submit( &connection_pool,
189
190
191
			connections[i].c_clientfunc, connections[i].c_clientarg );
			continue;
		}
192
193

		ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
194
195

		/* connections_mutex and c_mutex are locked */
196
		connection_closing( &connections[i], "slapd shutdown" );
197
		connection_close( &connections[i] );
198

199
200
201
202
		ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
	}

	ldap_pvt_thread_mutex_unlock( &connections_mutex );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
203
204

	return 0;
205
206
}

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

Gary Williams's avatar
Gary Williams committed
216
	for( c = connection_first( &connindex );
217
218
219
		c != NULL;
		c = connection_next( c, &connindex ) )
	{
220
221
222
223
		/* Don't timeout a slow-running request or a persistent
		 * outbound connection */
		if( c->c_n_ops_executing ||
			c->c_conn_state == SLAP_C_CLIENT ) continue;
224

225
		if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
226
			/* close it */
227
			connection_closing( c, "idletimeout" );
228
229
230
231
232
233
234
235
236
			connection_close( c );
			i++;
		}
	}
	connection_done( c );

	return i;
}

237
static Connection* connection_get( ber_socket_t s )
238
{
239
240
	/* connections_mutex should be locked by caller */

241
242
243
	Connection *c;

	Debug( LDAP_DEBUG_ARGS,
244
245
		"connection_get(%ld)\n",
		(long) s, 0, 0 );
246
247
248

	assert( connections != NULL );

249
	if(s == AC_SOCKET_INVALID) {
250
251
252
253
254
		return NULL;
	}

#ifndef HAVE_WINSOCK
	c = &connections[s];
255

Kurt Zeilenga's avatar
Kurt Zeilenga committed
256
	assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
257

258
#else
259
	c = NULL;
260
	{
261
		ber_socket_t i, sd;
262

263
		for(i=0; i<dtblsize; i++) {
Gary Williams's avatar
NT port    
Gary Williams committed
264
			if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
265
				assert( connections[i].c_conn_state == SLAP_C_INVALID );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
266
				assert( connections[i].c_sb == 0 );
267
268
269
				break;
			}

270
271
272
			ber_sockbuf_ctrl( connections[i].c_sb,
				LBER_SB_OPT_GET_FD, &sd );

Gary Williams's avatar
NT port    
Gary Williams committed
273
			if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
274
				assert( connections[i].c_conn_state == SLAP_C_INVALID );
275
				assert( sd == AC_SOCKET_INVALID );
276
277
278
				continue;
			}

279
280
281
			/* state can actually change from used -> unused by resched,
			 * so don't assert details here.
			 */
282

283
			if( sd == s ) {
Gary Williams's avatar
NT port    
Gary Williams committed
284
				c = &connections[i];
285
286
287
288
289
				break;
			}
		}
	}
#endif
290

291
	if( c != NULL ) {
292
293
		ber_socket_t	sd;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
294
295
		ldap_pvt_thread_mutex_lock( &c->c_mutex );

296
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
297
298
		if( c->c_struct_state != SLAP_C_USED ) {
			/* connection must have been closed due to resched */
299

300
			assert( c->c_conn_state == SLAP_C_INVALID );
301
			assert( sd == AC_SOCKET_INVALID );
302
303

			Debug( LDAP_DEBUG_TRACE,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
304
305
				"connection_get(%d): connection not used\n",
				s, 0, 0 );
306

307
			ldap_pvt_thread_mutex_unlock( &c->c_mutex );
308
309
310
			return NULL;
		}

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

315
316
		c->c_n_get++;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
317
318
		assert( c->c_struct_state == SLAP_C_USED );
		assert( c->c_conn_state != SLAP_C_INVALID );
319
		assert( sd != AC_SOCKET_INVALID );
320

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

329
330
331
332
333
334
335
336
337
	return c;
}

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

long connection_init(
338
	ber_socket_t s,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
339
	Listener *listener,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
340
341
	const char* dnsname,
	const char* peername,
342
	int flags,
343
	slap_ssf_t ssf,
344
	struct berval *authid )
345
{
346
	unsigned long id;
347
	Connection *c;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
348

349
350
	assert( connections != NULL );

Pierangelo Masarati's avatar
Pierangelo Masarati committed
351
	assert( listener != NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
352
353
354
	assert( dnsname != NULL );
	assert( peername != NULL );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
355
#ifndef HAVE_TLS
356
	assert( flags != CONN_IS_TLS );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
357
358
#endif

359
	if( s == AC_SOCKET_INVALID ) {
Gary Williams's avatar
Gary Williams committed
360
		Debug( LDAP_DEBUG_ANY,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
361
			"connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
362
363
364
365
366
		return -1;
	}

	assert( s >= 0 );
#ifndef HAVE_WINSOCK
367
	assert( s < dtblsize );
368
369
370
371
372
373
374
375
376
#endif

	ldap_pvt_thread_mutex_lock( &connections_mutex );

#ifndef HAVE_WINSOCK
	c = &connections[s];

#else
	{
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
377
		ber_socket_t i;
378
379
		c = NULL;

380
		for( i=0; i < dtblsize; i++) {
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
381
			ber_socket_t	sd;
382

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
383
384
385
386
387
			if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
				assert( connections[i].c_sb == 0 );
				c = &connections[i];
				break;
			}
Gary Williams's avatar
NT port    
Gary Williams committed
388

389
			sd = AC_SOCKET_INVALID;
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
390
391
392
393
			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
394

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
395
396
397
398
399
			if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
				assert( sd == AC_SOCKET_INVALID );
				c = &connections[i];
				break;
			}
Gary Williams's avatar
Gary Williams committed
400

401
402
403
404
			if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
				continue;
			}

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
405
406
407
408
409
410
411
			assert( connections[i].c_struct_state == SLAP_C_USED );
			assert( connections[i].c_conn_state != SLAP_C_INVALID );
			assert( sd != AC_SOCKET_INVALID );
		}

		if( c == NULL ) {
			Debug( LDAP_DEBUG_ANY,
412
				"connection_init(%d): connection table full "
413
				"(%d/%d)\n", s, i, dtblsize);
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
414
415
416
			ldap_pvt_thread_mutex_unlock( &connections_mutex );
			return -1;
		}
Gary Williams's avatar
Gary Williams committed
417
	}
418
419
#endif

420
	assert( c != NULL );
421

422
	if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
423
424
425
426
		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;
427
428
#ifdef LDAP_RES_INTERMEDIATE
		c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
429
#endif
430

431
432
433
		BER_BVZERO( &c->c_authmech );
		BER_BVZERO( &c->c_dn );
		BER_BVZERO( &c->c_ndn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
434

Pierangelo Masarati's avatar
Pierangelo Masarati committed
435
		c->c_listener = NULL;
436
437
		BER_BVZERO( &c->c_peer_domain );
		BER_BVZERO( &c->c_peer_name );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
438

439
440
		LDAP_STAILQ_INIT(&c->c_ops);
		LDAP_STAILQ_INIT(&c->c_pending_ops);
441

442
		BER_BVZERO( &c->c_sasl_bind_mech );
443
444
445
		c->c_sasl_done = 0;
		c->c_sasl_authctx = NULL;
		c->c_sasl_sockctx = NULL;
446
		c->c_sasl_extra = NULL;
447
		c->c_sasl_bindop = NULL;
448

449
450
451
		c->c_sb = ber_sockbuf_alloc( );

		{
452
			ber_len_t max = sockbuf_max_incoming;
453
454
455
			ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
456
		c->c_currentber = NULL;
457

458
459
460
461
		/* 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 );
462

463
#ifdef LDAP_SLAPI
464
		if ( slapi_plugins_used ) {
465
			slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
466
		}
467
468
#endif

469
470
		c->c_struct_state = SLAP_C_UNUSED;
	}
471

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

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
474
	assert( c->c_struct_state == SLAP_C_UNUSED );
475
476
477
	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
478
	assert( c->c_listener == NULL );
479
480
	assert( BER_BVISNULL( &c->c_peer_domain ) );
	assert( BER_BVISNULL( &c->c_peer_name ) );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
481
482
	assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
	assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
483
	assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
484
485
486
	assert( c->c_sasl_done == 0 );
	assert( c->c_sasl_authctx == NULL );
	assert( c->c_sasl_sockctx == NULL );
487
	assert( c->c_sasl_extra == NULL );
488
	assert( c->c_sasl_bindop == NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
489
	assert( c->c_currentber == NULL );
490
	assert( c->c_writewaiter == 0);
491

Pierangelo Masarati's avatar
Pierangelo Masarati committed
492
	c->c_listener = listener;
493
494
495
496

	if ( flags == CONN_IS_CLIENT ) {
		c->c_conn_state = SLAP_C_CLIENT;
		c->c_struct_state = SLAP_C_USED;
497
		c->c_close_reason = "?";			/* should never be needed */
498
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
499
500
501
502
503
504
		ldap_pvt_thread_mutex_unlock( &c->c_mutex );
		ldap_pvt_thread_mutex_unlock( &connections_mutex );

		return 0;
	}

505
506
	ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
	ber_str2bv( peername, 0, 1, &c->c_peer_name );
507

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
508
509
510
511
	c->c_n_ops_received = 0;
	c->c_n_ops_executing = 0;
	c->c_n_ops_pending = 0;
	c->c_n_ops_completed = 0;
512
513
514
515

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

517
518
	/* set to zero until bind, implies LDAP_VERSION3 */
	c->c_protocol = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
519

520
#ifndef SLAPD_MONITOR
521
	if ( global_idletimeout > 0 )
522
523
#endif /* ! SLAPD_MONITOR */
	{
524
525
		c->c_activitytime = c->c_starttime = slap_get_time();
	}
526

527
528
#ifdef LDAP_CONNECTIONLESS
	c->c_is_udp = 0;
529
	if( flags == CONN_IS_UDP ) {
530
531
		c->c_is_udp = 1;
#ifdef LDAP_DEBUG
Kurt Zeilenga's avatar
Kurt Zeilenga committed
532
533
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
			LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
534
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
535
536
537
538
		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 );
539
540
541
	} else
#endif
	{
542
#ifdef LDAP_DEBUG
Kurt Zeilenga's avatar
Kurt Zeilenga committed
543
544
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
			LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
545
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
546
547
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
			LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
548
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
549
550
551
552

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
555
556
557
558
559
	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
560
			s, c->c_peer_name.bv_val, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
561
	}
562

563
	ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
564
	id = c->c_connid = conn_nextid++;
565
	ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
566

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
567
568
	c->c_conn_state = SLAP_C_INACTIVE;
	c->c_struct_state = SLAP_C_USED;
569
	c->c_close_reason = "?";			/* should never be needed */
570

571
572
573
	c->c_ssf = c->c_transport_ssf = ssf;
	c->c_tls_ssf = 0;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
574
#ifdef HAVE_TLS
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
575
576
577
578
579
580
581
	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
582
#endif
583

584
	slap_sasl_open( c, 0 );
585
	slap_sasl_external( c, ssf, authid );
586

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
587
588
	ldap_pvt_thread_mutex_unlock( &c->c_mutex );
	ldap_pvt_thread_mutex_unlock( &connections_mutex );
589

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
590
	backend_connection_init(c);
591

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
592
	return id;
593
594
}

595
596
void connection2anonymous( Connection *c )
{
597
598
	assert( connections != NULL );
	assert( c != NULL );
599

600
	{
601
		ber_len_t max = sockbuf_max_incoming;
602
603
604
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
	}

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

610
	if ( !BER_BVISNULL( &c->c_dn ) ) {
611
		ch_free(c->c_dn.bv_val);
612
	}
613
	BER_BVZERO( &c->c_dn );
614
	if ( !BER_BVISNULL( &c->c_ndn ) ) {
615
		ch_free(c->c_ndn.bv_val);
616
	}
617
	BER_BVZERO( &c->c_ndn );
618
619
	if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
		ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
620
621
	}
	BER_BVZERO( &c->c_sasl_authz_dn );
622
623
624
625

	c->c_authz_backend = NULL;
}

626
627
628
static void
connection_destroy( Connection *c )
{
629
	/* note: connections_mutex should be locked by caller */
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
630
631
	ber_socket_t	sd;
	unsigned long	connid;
632
	const char		*close_reason;
633

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
634
635
636
637
638
	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) );
639
	assert( c->c_writewaiter == 0);
640

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

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
645
	backend_connection_destroy(c);
646

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
647
648
	c->c_protocol = 0;
	c->c_connid = -1;
649

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
650
	c->c_activitytime = c->c_starttime = 0;
651

652
	connection2anonymous( c );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
653
	c->c_listener = NULL;
654

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

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

	slap_sasl_close( c );
671

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

677
678
	ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
	if ( sd != AC_SOCKET_INVALID ) {
679
		slapd_remove( sd, 1, 0 );
680

681
682
683
684
		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 );
685
686
	}

Gary Williams's avatar
Gary Williams committed
687
	ber_sockbuf_free( c->c_sb );
688

689
	c->c_sb = ber_sockbuf_alloc( );
690

691
	{
692
		ber_len_t max = sockbuf_max_incoming;
693
694
695
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
	}

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
696
697
	c->c_conn_state = SLAP_C_INVALID;
	c->c_struct_state = SLAP_C_UNUSED;
698
	c->c_close_reason = "?";			/* should never be needed */
699
700
701

#ifdef LDAP_SLAPI
	/* call destructors, then constructors; avoids unnecessary allocation */
702
	if ( slapi_plugins_used ) {
703
		slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
704
	}
705
#endif
706
707
}

708
709
int connection_state_closing( Connection *c )
{
710
711
	/* c_mutex must be locked by caller */

712
	int state;
713
714
715
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );

716
717
718
719
720
	state = c->c_conn_state;

	assert( state != SLAP_C_INVALID );

	return state == SLAP_C_CLOSING;
721
722
}

723
724
725
726
static void connection_abandon( Connection *c )
{
	/* c_mutex must be locked by caller */

727
	Operation *o, *next, op = {0};
Howard Chu's avatar
Howard Chu committed
728
	Opheader ohdr = {0};
729
	SlapReply rs = {0};
730

Howard Chu's avatar
Howard Chu committed
731
732
733
	op.o_hdr = &ohdr;
	op.o_conn = c;
	op.o_connid = c->c_connid;
734
735
736
737
	op.o_tag = LDAP_REQ_ABANDON;
	for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
		next = LDAP_STAILQ_NEXT( o, o_next );
		op.orn_msgid = o->o_msgid;
738
		o->o_abandon = 1;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
739
		op.o_bd = frontendDB;
740
		frontendDB->be_abandon( &op, &rs );
741
742
743
	}

	/* remove pending operations */
744
745
746
	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;
747
748
749
750
		slap_op_free( o );
	}
}

751
void connection_closing( Connection *c, const char *why )
752
753
754
755
756
757
{
	assert( connections != NULL );
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );
	assert( c->c_conn_state != SLAP_C_INVALID );

758
759
	/* c_mutex must be locked by caller */

760
	if( c->c_conn_state != SLAP_C_CLOSING ) {
761
762
763
		ber_socket_t	sd;

		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
764
		Debug( LDAP_DEBUG_TRACE,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
765
			"connection_closing: readying conn=%lu sd=%d for close\n",
766
			c->c_connid, sd, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
767
768
		/* update state to closing */
		c->c_conn_state = SLAP_C_CLOSING;
769
		c->c_close_reason = why;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
770

771
		/* don't listen on this port anymore */
772
		slapd_clr_read( sd, 1 );
773
774

		/* abandon active operations */
775
		connection_abandon( c );
776
777

		/* wake write blocked operations */
778
		slapd_clr_write( sd, 1 );
779
780
781
782
783
784
		if ( c->c_writewaiter ) {
			ldap_pvt_thread_cond_signal( &c->c_write_cv );
			ldap_pvt_thread_mutex_unlock( &c->c_mutex );
			ldap_pvt_thread_yield();
			ldap_pvt_thread_mutex_lock( &c->c_mutex );
		}
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
	ber_socket_t	sd;

795
796
797
798
799
	assert( connections != NULL );
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );
	assert( c->c_conn_state == SLAP_C_CLOSING );

800
	/* note: connections_mutex and c_mutex should be locked by caller */
801

802
	ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
803
	if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
804
		Debug( LDAP_DEBUG_TRACE,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
805
			"connection_close: deferring conn=%lu sd=%d\n",
806
			c->c_connid, 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",
811
		c->c_connid, sd, 0 );
812
813
814
	connection_destroy( c );
}

815
unsigned long connections_nextid(void)
816
{
817
	unsigned long id;
818
819
820
821
822
823
824
825
826
827
828
	assert( connections != NULL );

	ldap_pvt_thread_mutex_lock( &connections_mutex );

	id = conn_nextid;

	ldap_pvt_thread_mutex_unlock( &connections_mutex );

	return id;
}

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

	ldap_pvt_thread_mutex_lock( &connections_mutex );

836
	*index = 0;
837

838
	return connection_next(NULL, index);
839
840
}

841
Connection* connection_next( Connection *c, ber_socket_t *index )
842
843
{
	assert( connections != NULL );
844
	assert( index != NULL );
845
	assert( *index <= dtblsize );
846
847
848
849
850
851
852

	if( c != NULL ) {
		ldap_pvt_thread_mutex_unlock( &c->c_mutex );
	}

	c = NULL;

853
	for(; *index < dtblsize; (*index)++) {
854
855
		if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
			assert( connections[*index].c_conn_state == SLAP_C_INVALID );
856
857
858
859
860
861
862
#ifndef HAVE_WINSOCK
			continue;
#else
			break;
#endif
		}

863
864
865
		if( connections[*index].c_struct_state == SLAP_C_USED ) {
			assert( connections[*index].c_conn_state != SLAP_C_INVALID );
			c = &connections[(*index)++];
866
867
868
			break;
		}

869
870
		assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
		assert( connections[*index].c_conn_state == SLAP_C_INVALID );
871
872
873
874
875
876
877