connection.c 54.6 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/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
4
 * Copyright 1998-2020 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
		Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n" );
106
107
108
109
110
		return -1;
	}

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

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

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

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

125
126
127
128
129
130
131
	for (i=0; i<dtblsize; i++) {
		connections[i].c_conn_idx = i;
		ldap_pvt_thread_mutex_init( &connections[i].c_mutex );
		ldap_pvt_thread_mutex_init( &connections[i].c_write1_mutex );
		ldap_pvt_thread_cond_init( &connections[i].c_write1_cv );
	}

132

133
134
135
136
137
138
139
140
	/*
	 * per entry initialization of the Connection array initialization
	 * will be done by connection_init()
	 */ 

	return 0;
}

141
142
143
/*
 * Destroy connection management infrastructure.
 */
144

145
146
int connections_destroy(void)
{
147
	ber_socket_t i;
148
149
150
151

	/* should check return of every call */

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

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

	free( connections );
	connections = NULL;

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

/*
 * shutdown all connections
 */
int connections_shutdown(void)
{
184
	ber_socket_t i;
185

186
	for ( i = 0; i < dtblsize; i++ ) {
187
188
189
190
191
192
193
194
195
196
197
198
199
		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
200
			}
201
			ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
Howard Chu's avatar
Howard Chu committed
202
		}
203
204
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
205
	return 0;
206
207
}

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

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

229
230
		if( global_idletimeout && 
			difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
231
			/* close it */
232
			connection_closing( c, "idletimeout" );
233
234
			connection_close( c );
			i++;
235
236
			continue;
		}
237
	}
238
	connection_done( c );
239
240
241
242

	return i;
}

Howard Chu's avatar
Howard Chu committed
243
244
245
246
/* Drop all client connections */
void connections_drop()
{
	Connection* c;
247
	ber_socket_t connindex;
Howard Chu's avatar
Howard Chu committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

	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 );
	}
263
	connection_done( c );
Howard Chu's avatar
Howard Chu committed
264
265
}

266
static Connection* connection_get( ber_socket_t s )
267
{
268
269
270
	Connection *c;

	Debug( LDAP_DEBUG_ARGS,
271
		"connection_get(%ld)\n",
272
		(long) s );
273
274
275

	assert( connections != NULL );

276
	if(s == AC_SOCKET_INVALID) return NULL;
277

Howard Chu's avatar
Howard Chu committed
278
279
	assert( s < dtblsize );
	c = &connections[s];
280

281
	if( c != NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
282
283
		ldap_pvt_thread_mutex_lock( &c->c_mutex );

284
285
		assert( c->c_struct_state != SLAP_C_UNINITIALIZED );

286
287
		if( c->c_struct_state != SLAP_C_USED ) {
			/* connection must have been closed due to resched */
288

289
			Debug( LDAP_DEBUG_CONNS,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
290
				"connection_get(%d): connection not used\n",
291
				s );
292
293
			assert( c->c_conn_state == SLAP_C_INVALID );
			assert( c->c_sd == AC_SOCKET_INVALID );
294

295
			ldap_pvt_thread_mutex_unlock( &c->c_mutex );
296
297
298
			return NULL;
		}

299
		Debug( LDAP_DEBUG_TRACE,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
300
			"connection_get(%d): got connid=%lu\n",
301
			s, c->c_connid );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
302

303
304
		c->c_n_get++;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
305
306
		assert( c->c_struct_state == SLAP_C_USED );
		assert( c->c_conn_state != SLAP_C_INVALID );
Howard Chu's avatar
Howard Chu committed
307
		assert( c->c_sd != AC_SOCKET_INVALID );
308

309
#ifndef SLAPD_MONITOR
310
		if ( global_idletimeout > 0 )
311
312
#endif /* ! SLAPD_MONITOR */
		{
313
314
			c->c_activitytime = slap_get_time();
		}
315
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
316

317
318
319
320
321
322
323
324
	return c;
}

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

325
Connection * connection_init(
326
	ber_socket_t s,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
327
	Listener *listener,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
328
329
	const char* dnsname,
	const char* peername,
330
	int flags,
331
	slap_ssf_t ssf,
Howard Chu's avatar
Howard Chu committed
332
	struct berval *authid
333
	LDAP_PF_LOCAL_SENDMSG_ARG(struct berval *peerbv))
334
{
335
	unsigned long id;
336
	Connection *c;
Howard Chu's avatar
Howard Chu committed
337
	int doinit = 0;
338
	ber_socket_t sfd = SLAP_FD2SOCK(s);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
339

340
341
	assert( connections != NULL );

Pierangelo Masarati's avatar
Pierangelo Masarati committed
342
	assert( listener != NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
343
344
345
	assert( dnsname != NULL );
	assert( peername != NULL );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
346
#ifndef HAVE_TLS
Howard Chu's avatar
Howard Chu committed
347
	assert( !( flags & CONN_IS_TLS ));
Kurt Zeilenga's avatar
Kurt Zeilenga committed
348
349
#endif

350
	if( s == AC_SOCKET_INVALID ) {
Gary Williams's avatar
Gary Williams committed
351
		Debug( LDAP_DEBUG_ANY,
352
			"connection_init: init of socket %ld invalid.\n", (long)s );
353
		return NULL;
354
355
356
	}

	assert( s >= 0 );
357
	assert( s < dtblsize );
Howard Chu's avatar
Howard Chu committed
358
	c = &connections[s];
359
360

	ldap_pvt_thread_mutex_lock( &c->c_mutex );
Howard Chu's avatar
Howard Chu committed
361
362
363
364
365
	if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
		doinit = 1;
	} else {
		assert( c->c_struct_state == SLAP_C_UNUSED );
	}
366

Howard Chu's avatar
Howard Chu committed
367
	if( doinit ) {
368
369
370
371
		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;
372
		c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
373

374
375
376
		BER_BVZERO( &c->c_authmech );
		BER_BVZERO( &c->c_dn );
		BER_BVZERO( &c->c_ndn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
377

Pierangelo Masarati's avatar
Pierangelo Masarati committed
378
		c->c_listener = NULL;
379
380
		BER_BVZERO( &c->c_peer_domain );
		BER_BVZERO( &c->c_peer_name );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
381

382
383
		LDAP_STAILQ_INIT(&c->c_ops);
		LDAP_STAILQ_INIT(&c->c_pending_ops);
384

Kurt Zeilenga's avatar
Kurt Zeilenga committed
385
		c->c_txn = CONN_TXN_INACTIVE;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
386
387
388
		c->c_txn_backend = NULL;
		LDAP_STAILQ_INIT(&c->c_txn_ops);

389
		BER_BVZERO( &c->c_sasl_bind_mech );
390
391
392
		c->c_sasl_done = 0;
		c->c_sasl_authctx = NULL;
		c->c_sasl_sockctx = NULL;
393
		c->c_sasl_extra = NULL;
394
		c->c_sasl_bindop = NULL;
Howard Chu's avatar
Howard Chu committed
395
		c->c_sasl_cbind = NULL;
396

397
398
399
		c->c_sb = ber_sockbuf_alloc( );

		{
400
			ber_len_t max = sockbuf_max_incoming;
401
402
403
			ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
404
		c->c_currentber = NULL;
405

406
#ifdef LDAP_SLAPI
407
		if ( slapi_plugins_used ) {
408
			slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
409
		}
410
#endif
411
	}
412

413
414
415
	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
416
	assert( c->c_listener == NULL );
417
418
	assert( BER_BVISNULL( &c->c_peer_domain ) );
	assert( BER_BVISNULL( &c->c_peer_name ) );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
419
420
	assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
	assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
421
	assert( c->c_txn == CONN_TXN_INACTIVE );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
422
423
	assert( c->c_txn_backend == NULL );
	assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
424
	assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
425
426
427
	assert( c->c_sasl_done == 0 );
	assert( c->c_sasl_authctx == NULL );
	assert( c->c_sasl_sockctx == NULL );
428
	assert( c->c_sasl_extra == NULL );
429
	assert( c->c_sasl_bindop == NULL );
Howard Chu's avatar
Howard Chu committed
430
	assert( c->c_sasl_cbind == NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
431
	assert( c->c_currentber == NULL );
432
	assert( c->c_writewaiter == 0);
433
	assert( c->c_writers == 0);
434

Pierangelo Masarati's avatar
Pierangelo Masarati committed
435
	c->c_listener = listener;
Howard Chu's avatar
Howard Chu committed
436
	c->c_sd = s;
437

Howard Chu's avatar
Howard Chu committed
438
	if ( flags & CONN_IS_CLIENT ) {
439
		c->c_connid = 0;
440
		ldap_pvt_thread_mutex_lock( &connections_mutex );
441
		c->c_conn_state = SLAP_C_CLIENT;
442
		c->c_struct_state = SLAP_C_USED;
443
		ldap_pvt_thread_mutex_unlock( &connections_mutex );
444
		c->c_close_reason = "?";			/* should never be needed */
445
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &sfd );
446
447
		ldap_pvt_thread_mutex_unlock( &c->c_mutex );

448
		return c;
449
450
	}

451
452
	ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
	ber_str2bv( peername, 0, 1, &c->c_peer_name );
453

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
454
455
456
457
	c->c_n_ops_received = 0;
	c->c_n_ops_executing = 0;
	c->c_n_ops_pending = 0;
	c->c_n_ops_completed = 0;
458
459
460
461

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

463
464
	/* set to zero until bind, implies LDAP_VERSION3 */
	c->c_protocol = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
465

466
#ifndef SLAPD_MONITOR
467
	if ( global_idletimeout > 0 )
468
469
#endif /* ! SLAPD_MONITOR */
	{
470
471
		c->c_activitytime = c->c_starttime = slap_get_time();
	}
472

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

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
515
516
517
518
519
	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",
520
			s, c->c_peer_name.bv_val );
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537

		c->c_listener = NULL;
		if(c->c_peer_domain.bv_val != NULL) {
			free(c->c_peer_domain.bv_val);
		}
		BER_BVZERO( &c->c_peer_domain );
		if(c->c_peer_name.bv_val != NULL) {
			free(c->c_peer_name.bv_val);
		}
		BER_BVZERO( &c->c_peer_name );

		ber_sockbuf_free( c->c_sb );
		c->c_sb = NULL;
		c->c_sd = AC_SOCKET_INVALID;
		ldap_pvt_thread_mutex_unlock( &c->c_mutex );

		return NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
538
	}
539

540
	ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
541
	id = c->c_connid = conn_nextid++;
542
	ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
543

544
	ldap_pvt_thread_mutex_lock( &connections_mutex );
545
	c->c_conn_state = SLAP_C_INACTIVE;
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
546
	c->c_struct_state = SLAP_C_USED;
547
	ldap_pvt_thread_mutex_unlock( &connections_mutex );
548
	c->c_close_reason = "?";			/* should never be needed */
549

550
	c->c_ssf = c->c_transport_ssf = ssf;
551
	c->c_tls_ssf = c->c_sasl_ssf = 0;
552

Kurt Zeilenga's avatar
Kurt Zeilenga committed
553
#ifdef HAVE_TLS
Howard Chu's avatar
Howard Chu committed
554
	if ( flags & CONN_IS_TLS ) {
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
555
556
557
558
559
560
		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
561
#endif
562

563
	slap_sasl_open( c, 0 );
564
	slap_sasl_external( c, ssf, authid );
565

566
	slapd_add_internal( s, 1 );
567

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
568
	backend_connection_init(c);
569
	ldap_pvt_thread_mutex_unlock( &c->c_mutex );
570

571
	if ( !(flags & CONN_IS_UDP ))
572
		Debug( LDAP_DEBUG_STATS,
573
			"conn=%ld fd=%ld ACCEPT from %s (%s)\n",
574
			id, (long) s, peername, listener->sl_name.bv_val );
575

576
	return c;
577
578
}

579
580
void connection2anonymous( Connection *c )
{
581
582
	assert( connections != NULL );
	assert( c != NULL );
583

584
	{
585
		ber_len_t max = sockbuf_max_incoming;
586
587
588
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
	}

589
	if ( !BER_BVISNULL( &c->c_authmech ) ) {
590
		ch_free(c->c_authmech.bv_val);
591
	}
592
	BER_BVZERO( &c->c_authmech );
593

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

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

604
605
	if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
		ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
606
607
	}
	BER_BVZERO( &c->c_sasl_authz_dn );
608
609
610
611

	c->c_authz_backend = NULL;
}

612
613
614
static void
connection_destroy( Connection *c )
{
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
615
	unsigned long	connid;
616
	const char		*close_reason;
Howard Chu's avatar
Howard Chu committed
617
	Sockbuf			*sb;
Howard Chu's avatar
Howard Chu committed
618
	ber_socket_t	sd;
619

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
620
621
622
623
624
	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
625
626
627
628
	assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
	assert( c->c_txn == CONN_TXN_INACTIVE );
	assert( c->c_txn_backend == NULL );
	assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
629
	assert( c->c_writewaiter == 0);
630
	assert( c->c_writers == 0);
631

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

Howard Chu's avatar
Howard Chu committed
636
637
638
639
	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
640
	backend_connection_destroy(c);
641

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
642
643
	c->c_protocol = 0;
	c->c_connid = -1;
644

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
645
	c->c_activitytime = c->c_starttime = 0;
646

647
	connection2anonymous( c );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
648
	c->c_listener = NULL;
649

650
651
	if(c->c_peer_domain.bv_val != NULL) {
		free(c->c_peer_domain.bv_val);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
652
	}
653
	BER_BVZERO( &c->c_peer_domain );
654
655
	if(c->c_peer_name.bv_val != NULL) {
		free(c->c_peer_name.bv_val);
656
	}
657
	BER_BVZERO( &c->c_peer_name );
658

659
	c->c_sasl_bind_in_progress = 0;
660
661
	if(c->c_sasl_bind_mech.bv_val != NULL) {
		free(c->c_sasl_bind_mech.bv_val);
662
	}
663
	BER_BVZERO( &c->c_sasl_bind_mech );
664
665

	slap_sasl_close( c );
666

Kurt Zeilenga's avatar
Kurt Zeilenga committed
667
668
669
670
671
	if ( c->c_currentber != NULL ) {
		ber_free( c->c_currentber, 1 );
		c->c_currentber = NULL;
	}

672

Howard Chu's avatar
Howard Chu committed
673
674
675
676
#ifdef LDAP_SLAPI
	/* call destructors, then constructors; avoids unnecessary allocation */
	if ( slapi_plugins_used ) {
		slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
677
	}
Howard Chu's avatar
Howard Chu committed
678
#endif
679

Howard Chu's avatar
Howard Chu committed
680
681
	sd = c->c_sd;
	c->c_sd = AC_SOCKET_INVALID;
Howard Chu's avatar
Howard Chu committed
682
	c->c_close_reason = "?";			/* should never be needed */
683

Howard Chu's avatar
Howard Chu committed
684
685
	sb = c->c_sb;
	c->c_sb = ber_sockbuf_alloc( );
686
	{
687
		ber_len_t max = sockbuf_max_incoming;
688
689
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
	}
Howard Chu's avatar
Howard Chu committed
690
691
	c->c_conn_state = SLAP_C_INVALID;
	c->c_struct_state = SLAP_C_UNUSED;
692

Howard Chu's avatar
Howard Chu committed
693
694
695
	/* 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
696
697
	if ( sd != AC_SOCKET_INVALID ) {
		slapd_remove( sd, sb, 1, 0, 0 );
Howard Chu's avatar
Howard Chu committed
698

699
		if ( close_reason == NULL ) {
700
701
			Debug( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed\n",
				connid, (long) sd );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
702
		} else {
703
704
			Debug( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
				connid, (long) sd, close_reason );
705
		}
706
	}
707
708
}

709
710
711
712
713
714
715
716
717
718
719
int connection_is_active( ber_socket_t s )
{
	Connection *c;

	assert( s < dtblsize );
	c = &connections[s];
	return c->c_conn_state == SLAP_C_CLOSING ||
		c->c_conn_state == SLAP_C_BINDING ||
		c->c_conn_state == SLAP_C_ACTIVE ;
}

720
int connection_valid( Connection *c )
721
{
722
723
	/* c_mutex must be locked by caller */

724
	assert( c != NULL );
725

726
727
728
	return c->c_struct_state == SLAP_C_USED &&
		c->c_conn_state >= SLAP_C_ACTIVE &&
		c->c_conn_state <= SLAP_C_CLIENT;
729
730
}

731
732
733
734
static void connection_abandon( Connection *c )
{
	/* c_mutex must be locked by caller */

735
	Operation *o, *next, op = {0};
Howard Chu's avatar
Howard Chu committed
736
	Opheader ohdr = {0};
737

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

743
	for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
744
745
		SlapReply rs = {REP_RESULT};

746
		next = LDAP_STAILQ_NEXT( o, o_next );
747
748
749
		/* don't abandon an op twice */
		if ( o->o_abandon )
			continue;
750
		op.orn_msgid = o->o_msgid;
751
		o->o_abandon = 1;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
752
		op.o_bd = frontendDB;
753
		frontendDB->be_abandon( &op, &rs );
754
755
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
756
757
758
759
	/* 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
760
		slap_op_free( o, NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
761
762
763
764
765
766
	}

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

767
	/* remove pending operations */
768
769
770
	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
771
		slap_op_free( o, NULL );
772
773
774
	}
}

775
776
777
778
779
780
781
782
783
784
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 ) {
Howard Chu's avatar
Howard Chu committed
785
			slapd_shutsock( c->c_sd );
786
787
788
789
790
791
792
793
794
795
796
797
		}
		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 );
	}
}

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

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

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

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

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

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

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

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

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

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

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