connection.c 51.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/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
4
 * Copyright 1998-2021 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
static Connection *connections = NULL;
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
51

52
static ldap_pvt_thread_mutex_t conn_nextid_mutex;
53
static unsigned long conn_nextid = SLAPD_SYNC_SYNCCONN_OFFSET;
54

55
56
static const char conn_lost_str[] = "connection lost";

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

	return "?";
}

72
static Connection* connection_get( ber_socket_t s );
73

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

static int connection_input( Connection *c, conn_readinfo *cri );
83
static void connection_close( Connection *c );
84

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

91
92
static ldap_pvt_thread_start_t connection_operation;

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

100
101
	assert( connections == NULL );

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

	/* should check return of every call */
108
	ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
109

110
	connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
111
112

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

119
120
121
122
123
124
125
	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 );
	}

126

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

	return 0;
}

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

139
140
int connections_destroy(void)
{
141
	ber_socket_t i;
142
143
144
145

	/* should check return of every call */

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

150
	for ( i = 0; i < dtblsize; i++ ) {
151
152
153
154
		ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
		ldap_pvt_thread_mutex_destroy( &connections[i].c_write1_mutex );
		ldap_pvt_thread_cond_destroy( &connections[i].c_write1_cv );
		if( connections[i].c_sb ) {
155
			ber_sockbuf_free( connections[i].c_sb );
156
#ifdef LDAP_SLAPI
157
			if ( slapi_plugins_used ) {
158
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
	}

	free( connections );
	connections = NULL;

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

/*
 * shutdown all connections
 */
int connections_shutdown(void)
{
177
	ber_socket_t i;
178

179
	for ( i = 0; i < dtblsize; i++ ) {
180
181
182
183
184
185
186
187
188
189
190
		ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
		if( connections[i].c_conn_state > SLAP_C_INVALID ) {

			/* 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
191
192
			}
		}
193
		ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
194
195
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
196
	return 0;
197
198
}

199
200
201
202
203
/*
 * Timeout idle connections.
 */
int connections_timeout_idle(time_t now)
{
204
	int i = 0;
205
	ber_socket_t connindex;
206
207
	Connection* c;

Gary Williams's avatar
Gary Williams committed
208
	for( c = connection_first( &connindex );
209
210
211
		c != NULL;
		c = connection_next( c, &connindex ) )
	{
212
		/* Don't timeout a slow-running request or a persistent
Howard Chu's avatar
Howard Chu committed
213
		 * outbound connection.
214
215
216
		 */
		if(( c->c_n_ops_executing && !c->c_writewaiter)
			|| c->c_conn_state == SLAP_C_CLIENT ) {
217
218
			continue;
		}
219

220
221
		if( global_idletimeout && 
			difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
222
			/* close it */
223
			connection_closing( c, "idletimeout" );
224
225
			connection_close( c );
			i++;
226
227
			continue;
		}
228
	}
229
	connection_done( c );
230
231
232
233

	return i;
}

Howard Chu's avatar
Howard Chu committed
234
235
236
237
/* Drop all client connections */
void connections_drop()
{
	Connection* c;
238
	ber_socket_t connindex;
Howard Chu's avatar
Howard Chu committed
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253

	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 );
	}
254
	connection_done( c );
Howard Chu's avatar
Howard Chu committed
255
256
}

257
static Connection* connection_get( ber_socket_t s )
258
{
259
260
261
	Connection *c;

	Debug( LDAP_DEBUG_ARGS,
262
		"connection_get(%ld)\n",
263
		(long) s );
264
265
266

	assert( connections != NULL );

267
	if(s == AC_SOCKET_INVALID) return NULL;
268

Howard Chu's avatar
Howard Chu committed
269
270
	assert( s < dtblsize );
	c = &connections[s];
271

272
	if( c != NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
273
274
		ldap_pvt_thread_mutex_lock( &c->c_mutex );

275
		if( c->c_conn_state == SLAP_C_INVALID ) {
276
			/* connection must have been closed due to resched */
277

278
			Debug( LDAP_DEBUG_CONNS,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
279
				"connection_get(%d): connection not used\n",
280
				s );
281
			assert( c->c_sd == AC_SOCKET_INVALID );
282

283
			ldap_pvt_thread_mutex_unlock( &c->c_mutex );
284
285
286
			return NULL;
		}

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

291
292
		c->c_n_get++;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
293
		assert( c->c_conn_state != SLAP_C_INVALID );
Howard Chu's avatar
Howard Chu committed
294
		assert( c->c_sd != AC_SOCKET_INVALID );
295

296
		c->c_activitytime = slap_get_time();
297
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
298

299
300
301
302
303
304
305
306
	return c;
}

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

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

321
322
	assert( connections != NULL );

Pierangelo Masarati's avatar
Pierangelo Masarati committed
323
	assert( listener != NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
324
325
326
	assert( dnsname != NULL );
	assert( peername != NULL );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
327
#ifndef HAVE_TLS
Howard Chu's avatar
Howard Chu committed
328
	assert( !( flags & CONN_IS_TLS ));
Kurt Zeilenga's avatar
Kurt Zeilenga committed
329
330
#endif

331
	if( s == AC_SOCKET_INVALID ) {
Gary Williams's avatar
Gary Williams committed
332
		Debug( LDAP_DEBUG_ANY,
333
			"connection_init: init of socket %ld invalid.\n", (long)s );
334
		return NULL;
335
336
337
	}

	assert( s >= 0 );
338
	assert( s < dtblsize );
Howard Chu's avatar
Howard Chu committed
339
	c = &connections[s];
340
341

	ldap_pvt_thread_mutex_lock( &c->c_mutex );
342

343
	if( !c->c_sb ) {
344
345
346
347
		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;
348
		c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
349

350
351
352
		BER_BVZERO( &c->c_authmech );
		BER_BVZERO( &c->c_dn );
		BER_BVZERO( &c->c_ndn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
353

Pierangelo Masarati's avatar
Pierangelo Masarati committed
354
		c->c_listener = NULL;
355
356
		BER_BVZERO( &c->c_peer_domain );
		BER_BVZERO( &c->c_peer_name );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
357

358
359
		LDAP_STAILQ_INIT(&c->c_ops);
		LDAP_STAILQ_INIT(&c->c_pending_ops);
360

Kurt Zeilenga's avatar
Kurt Zeilenga committed
361
		c->c_txn = CONN_TXN_INACTIVE;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
362
363
364
		c->c_txn_backend = NULL;
		LDAP_STAILQ_INIT(&c->c_txn_ops);

365
		BER_BVZERO( &c->c_sasl_bind_mech );
366
367
368
		c->c_sasl_done = 0;
		c->c_sasl_authctx = NULL;
		c->c_sasl_sockctx = NULL;
369
		c->c_sasl_extra = NULL;
370
		c->c_sasl_bindop = NULL;
Howard Chu's avatar
Howard Chu committed
371
		c->c_sasl_cbind = NULL;
372

373
374
375
		c->c_sb = ber_sockbuf_alloc( );

		{
376
			ber_len_t max = sockbuf_max_incoming;
377
378
379
			ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
380
		c->c_currentber = NULL;
381

382
#ifdef LDAP_SLAPI
383
		if ( slapi_plugins_used ) {
384
			slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
385
		}
386
#endif
387
	}
388

389
390
391
	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
392
	assert( c->c_listener == NULL );
393
394
	assert( BER_BVISNULL( &c->c_peer_domain ) );
	assert( BER_BVISNULL( &c->c_peer_name ) );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
395
396
	assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
	assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
397
	assert( c->c_txn == CONN_TXN_INACTIVE );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
398
399
	assert( c->c_txn_backend == NULL );
	assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
400
	assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
401
402
403
	assert( c->c_sasl_done == 0 );
	assert( c->c_sasl_authctx == NULL );
	assert( c->c_sasl_sockctx == NULL );
404
	assert( c->c_sasl_extra == NULL );
405
	assert( c->c_sasl_bindop == NULL );
Howard Chu's avatar
Howard Chu committed
406
	assert( c->c_sasl_cbind == NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
407
	assert( c->c_currentber == NULL );
408
	assert( c->c_writewaiter == 0);
409
	assert( c->c_writers == 0);
410

Pierangelo Masarati's avatar
Pierangelo Masarati committed
411
	c->c_listener = listener;
Howard Chu's avatar
Howard Chu committed
412
	c->c_sd = s;
413

Howard Chu's avatar
Howard Chu committed
414
	if ( flags & CONN_IS_CLIENT ) {
415
		c->c_connid = 0;
416
		c->c_conn_state = SLAP_C_CLIENT;
417
		c->c_close_reason = "?";			/* should never be needed */
418
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &sfd );
419
420
		ldap_pvt_thread_mutex_unlock( &c->c_mutex );

421
		return c;
422
423
	}

424
425
	ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
	ber_str2bv( peername, 0, 1, &c->c_peer_name );
426

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
427
428
429
430
	c->c_n_ops_received = 0;
	c->c_n_ops_executing = 0;
	c->c_n_ops_pending = 0;
	c->c_n_ops_completed = 0;
431
432
433
434

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

436
437
	/* set to zero until bind, implies LDAP_VERSION3 */
	c->c_protocol = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
438

439
	c->c_activitytime = c->c_starttime = slap_get_time();
440

441
442
#ifdef LDAP_CONNECTIONLESS
	c->c_is_udp = 0;
Howard Chu's avatar
Howard Chu committed
443
	if( flags & CONN_IS_UDP ) {
444
445
		c->c_is_udp = 1;
#ifdef LDAP_DEBUG
Kurt Zeilenga's avatar
Kurt Zeilenga committed
446
447
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
			LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
448
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
449
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
450
			LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
451
452
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
			LBER_SBIOD_LEVEL_PROVIDER, NULL );
453
	} else
Howard Chu's avatar
Howard Chu committed
454
455
456
457
458
459
#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_" );
460
#endif
Howard Chu's avatar
Howard Chu committed
461
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_fd,
462
			LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
Howard Chu's avatar
Howard Chu committed
463
464
465
466
467
468
#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 */
469
	{
470
#ifdef LDAP_DEBUG
Kurt Zeilenga's avatar
Kurt Zeilenga committed
471
472
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
			LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
473
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
474
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
475
			LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
476
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
477
478
479
480

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
483
484
485
486
487
	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",
488
			s, c->c_peer_name.bv_val );
489
490
491
492
493
494
495
496
497
498
499
500

		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 );
501
502
503
504
505
506
		c->c_sb = ber_sockbuf_alloc( );
		{
			ber_len_t max = sockbuf_max_incoming;
			ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
		}

507
508
509
510
		c->c_sd = AC_SOCKET_INVALID;
		ldap_pvt_thread_mutex_unlock( &c->c_mutex );

		return NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
511
	}
512

513
	ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
514
	id = c->c_connid = conn_nextid++;
515
	ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
516

517
	c->c_conn_state = SLAP_C_INACTIVE;
518
	c->c_close_reason = "?";			/* should never be needed */
519

520
	c->c_ssf = c->c_transport_ssf = ssf;
521
	c->c_tls_ssf = c->c_sasl_ssf = 0;
522

Kurt Zeilenga's avatar
Kurt Zeilenga committed
523
#ifdef HAVE_TLS
Howard Chu's avatar
Howard Chu committed
524
	if ( flags & CONN_IS_TLS ) {
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
525
526
527
528
529
530
		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
531
#endif
532

533
	slap_sasl_open( c, 0 );
534
	slap_sasl_external( c, ssf, authid );
535

536
	slapd_add_internal( s, 1 );
537

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
538
	backend_connection_init(c);
539
	ldap_pvt_thread_mutex_unlock( &c->c_mutex );
540

541
	if ( !(flags & CONN_IS_UDP ))
542
		Debug( LDAP_DEBUG_STATS,
543
			"conn=%ld fd=%ld ACCEPT from %s (%s)\n",
544
			id, (long) s, peername, listener->sl_name.bv_val );
545

546
	return c;
547
548
}

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

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

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

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

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

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

	c->c_authz_backend = NULL;
}

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

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
590
591
592
593
	assert( connections != NULL );
	assert( c != NULL );
	assert( c->c_conn_state != SLAP_C_INVALID );
	assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
594
595
596
597
	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) );
598
	assert( c->c_writewaiter == 0);
599
	assert( c->c_writers == 0);
600

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

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
605
	backend_connection_destroy(c);
606

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
607
608
	c->c_protocol = 0;
	c->c_connid = -1;
609

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
610
	c->c_activitytime = c->c_starttime = 0;
611

612
	connection2anonymous( c );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
613
	c->c_listener = NULL;
614

615
616
	if(c->c_peer_domain.bv_val != NULL) {
		free(c->c_peer_domain.bv_val);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
617
	}
618
	BER_BVZERO( &c->c_peer_domain );
619
620
	if(c->c_peer_name.bv_val != NULL) {
		free(c->c_peer_name.bv_val);
621
	}
622
	BER_BVZERO( &c->c_peer_name );
623

624
	c->c_sasl_bind_in_progress = 0;
625
626
	if(c->c_sasl_bind_mech.bv_val != NULL) {
		free(c->c_sasl_bind_mech.bv_val);
627
	}
628
	BER_BVZERO( &c->c_sasl_bind_mech );
629
630

	slap_sasl_close( c );
631

Kurt Zeilenga's avatar
Kurt Zeilenga committed
632
633
634
635
636
	if ( c->c_currentber != NULL ) {
		ber_free( c->c_currentber, 1 );
		c->c_currentber = NULL;
	}

637

Howard Chu's avatar
Howard Chu committed
638
639
640
641
#ifdef LDAP_SLAPI
	/* call destructors, then constructors; avoids unnecessary allocation */
	if ( slapi_plugins_used ) {
		slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
642
	}
Howard Chu's avatar
Howard Chu committed
643
#endif
644

Howard Chu's avatar
Howard Chu committed
645
646
	sd = c->c_sd;
	c->c_sd = AC_SOCKET_INVALID;
Howard Chu's avatar
Howard Chu committed
647
	c->c_close_reason = "?";			/* should never be needed */
648

Howard Chu's avatar
Howard Chu committed
649
650
	sb = c->c_sb;
	c->c_sb = ber_sockbuf_alloc( );
651
	{
652
		ber_len_t max = sockbuf_max_incoming;
653
654
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
	}
Howard Chu's avatar
Howard Chu committed
655
	c->c_conn_state = SLAP_C_INVALID;
656

Howard Chu's avatar
Howard Chu committed
657
658
659
	/* 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
660
661
	if ( sd != AC_SOCKET_INVALID ) {
		slapd_remove( sd, sb, 1, 0, 0 );
Howard Chu's avatar
Howard Chu committed
662

663
		if ( close_reason == NULL ) {
664
665
			Debug( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed\n",
				connid, (long) sd );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
666
		} else {
667
668
			Debug( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
				connid, (long) sd, close_reason );
669
		}
670
	}
671
672
}

673
674
675
676
677
678
679
680
681
682
683
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 ;
}

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

688
	assert( c != NULL );
689

690
	return c->c_conn_state >= SLAP_C_ACTIVE &&
691
		c->c_conn_state <= SLAP_C_CLIENT;
692
693
}

694
695
696
697
static void connection_abandon( Connection *c )
{
	/* c_mutex must be locked by caller */

698
	Operation *o, *next, op = {0};
Howard Chu's avatar
Howard Chu committed
699
	Opheader ohdr = {0};
700

Howard Chu's avatar
Howard Chu committed
701
702
703
	op.o_hdr = &ohdr;
	op.o_conn = c;
	op.o_connid = c->c_connid;
704
	op.o_tag = LDAP_REQ_ABANDON;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
705

706
	for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
707
708
		SlapReply rs = {REP_RESULT};

709
		next = LDAP_STAILQ_NEXT( o, o_next );
710
711
712
		/* don't abandon an op twice */
		if ( o->o_abandon )
			continue;
713
		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
	/* 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
723
		slap_op_free( o, NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
724
725
726
727
728
729
	}

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

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

738
739
740
741
742
743
744
745
746
747
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
748
			slapd_shutsock( c->c_sd );
749
750
751
752
753
754
755
756
757
758
759
760
		}
		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 );
	}
}

761
void connection_closing( Connection *c, const char *why )
762
763
764
{
	assert( connections != NULL );
	assert( c != NULL );
Howard Chu's avatar
Howard Chu committed
765

Howard Chu's avatar
Howard Chu committed
766
767
	if( c->c_conn_state == SLAP_C_INVALID )
		return;
768

769
770
	/* c_mutex must be locked by caller */

771
	if( c->c_conn_state != SLAP_C_CLOSING ) {
772
		Debug( LDAP_DEBUG_CONNS,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
773
			"connection_closing: readying conn=%lu sd=%d for close\n",
774
			c->c_connid, c->c_sd );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
775
776
		/* update state to closing */
		c->c_conn_state = SLAP_C_CLOSING;
777
		c->c_close_reason = why;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
778

779
		/* don't listen on this port anymore */
Howard Chu's avatar
Howard Chu committed
780
		slapd_clr_read( c->c_sd, 0 );
781
782

		/* abandon active operations */
783
		connection_abandon( c );
784
785

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

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

794
795
static void
connection_close( Connection *c )
796
797
798
{
	assert( connections != NULL );
	assert( c != NULL );
Howard Chu's avatar
Howard Chu committed
799

Howard Chu's avatar
Howard Chu committed
800
801
	if ( c->c_conn_state != SLAP_C_CLOSING )
		return;
802

803
	/* NOTE: c_mutex should be locked by caller */
804

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

Pierangelo Masarati's avatar
Pierangelo Masarati committed
814
	Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
815
		c->c_connid, c->c_sd );
816

817
818
819
	connection_destroy( c );
}

820
unsigned long connections_nextid(void)
821
{
822
	unsigned long id;
823
824
	assert( connections != NULL );

825
	ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
826
827
828

	id = conn_nextid;

829
	ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
830
831
832