connection.c 50.3 KB
Newer Older
1
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
3
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
4
 * Copyright 1998-2009 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* Portions Copyright (c) 1995 Regents of the University of Michigan.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that this notice is preserved and that due credit is given
 * to the University of Michigan at Ann Arbor. The name of the University
 * may not be used to endorse or promote products derived from this
 * software without specific prior written permission. This software
 * is provided ``as is'' without express or implied warranty.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
24
25
 */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
26
#include "portable.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
27
28

#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
29
#ifdef HAVE_LIMITS_H
Kurt Zeilenga's avatar
Kurt Zeilenga committed
30
#include <limits.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
31
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
32

33
#include <ac/socket.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
34
35
36
#include <ac/errno.h>
#include <ac/string.h>
#include <ac/time.h>
Pierangelo Masarati's avatar
Pierangelo Masarati committed
37
#include <ac/unistd.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
38

Kurt Zeilenga's avatar
Kurt Zeilenga committed
39
#include "lutil.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
40
41
#include "slap.h"

42
#ifdef LDAP_SLAPI
43
#include "slapi/slapi.h"
44
45
#endif

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

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

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

55
/* structure state (protected by connections_mutex) */
56
57
58
59
60
61
enum sc_struct_state {
	SLAP_C_UNINITIALIZED = 0,	/* MUST BE ZERO (0) */
	SLAP_C_UNUSED,
	SLAP_C_USED,
	SLAP_C_PENDING
};
62
63

/* connection state (protected by c_mutex ) */
64
65
66
67
68
69
70
71
enum sc_conn_state {
	SLAP_C_INVALID = 0,		/* MUST BE ZERO (0) */
	SLAP_C_INACTIVE,		/* zero threads */
	SLAP_C_CLOSING,			/* closing */
	SLAP_C_ACTIVE,			/* one or more threads */
	SLAP_C_BINDING,			/* binding */
	SLAP_C_CLIENT			/* outbound client conn */
};
72

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

	return "?";
}

88
static Connection* connection_get( ber_socket_t s );
89

90
91
92
93
typedef struct conn_readinfo {
	Operation *op;
	ldap_pvt_thread_start_t *func;
	void *arg;
Howard Chu's avatar
Howard Chu committed
94
	void *ctx;
95
96
97
98
	int nullop;
} conn_readinfo;

static int connection_input( Connection *c, conn_readinfo *cri );
99
static void connection_close( Connection *c );
100

101
static int connection_op_activate( Operation *op );
102
static void connection_op_queue( Operation *op );
103
static int connection_resched( Connection *conn );
104
static void connection_abandon( Connection *conn );
105
static void connection_destroy( Connection *c );
106

107
108
static ldap_pvt_thread_start_t connection_operation;

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

116
117
	assert( connections == NULL );

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

128
	connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
129
130

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

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

140
	for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
141

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

	return 0;
}

150
151
152
/*
 * Destroy connection management infrastructure.
 */
153

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

	/* should check return of every call */

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

166
	for ( i = 0; i < dtblsize; i++ ) {
167
		if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
168
			ber_sockbuf_free( connections[i].c_sb );
169
			ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
170
171
172
173
			ldap_pvt_thread_mutex_destroy( &connections[i].c_write1_mutex );
			ldap_pvt_thread_mutex_destroy( &connections[i].c_write2_mutex );
			ldap_pvt_thread_cond_destroy( &connections[i].c_write1_cv );
			ldap_pvt_thread_cond_destroy( &connections[i].c_write2_cv );
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
202
203
204
205
206
207
208
209
210
211
		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
212
			}
213
			ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
Howard Chu's avatar
Howard Chu committed
214
		}
215
216
	}

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

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

	old = slapd_get_writetime();
231

Gary Williams's avatar
Gary Williams committed
232
	for( c = connection_first( &connindex );
233
234
235
		c != NULL;
		c = connection_next( c, &connindex ) )
	{
236
		/* Don't timeout a slow-running request or a persistent
237
238
239
240
241
		 * outbound connection. But if it has a writewaiter, see
		 * if the waiter has been there too long.
		 */
		if(( c->c_n_ops_executing && !c->c_writewaiter)
			|| c->c_conn_state == SLAP_C_CLIENT ) {
242
243
			continue;
		}
244

245
246
		if( global_idletimeout && 
			difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
247
			/* close it */
248
			connection_closing( c, "idletimeout" );
249
250
			connection_close( c );
			i++;
251
252
			continue;
		}
253
		if ( c->c_writewaiter && global_writetimeout ) {
254
255
256
257
258
259
260
			writers = 1;
			if( difftime( c->c_activitytime+global_writetimeout, now) < 0 ) {
				/* close it */
				connection_closing( c, "writetimeout" );
				connection_close( c );
				i++;
			}
261
262
263
		}
	}
	connection_done( c );
264
	if ( old && !writers )
265
		slapd_clr_writetime( old );
266
267
268
269

	return i;
}

270
static Connection* connection_get( ber_socket_t s )
271
{
272
273
274
	Connection *c;

	Debug( LDAP_DEBUG_ARGS,
275
276
		"connection_get(%ld)\n",
		(long) s, 0, 0 );
277
278
279

	assert( connections != NULL );

280
	if(s == AC_SOCKET_INVALID) return NULL;
281

Howard Chu's avatar
Howard Chu committed
282
283
	assert( s < dtblsize );
	c = &connections[s];
284

285
	if( c != NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
286
287
		ldap_pvt_thread_mutex_lock( &c->c_mutex );

288
289
		assert( c->c_struct_state != SLAP_C_UNINITIALIZED );

290
291
		if( c->c_struct_state != SLAP_C_USED ) {
			/* connection must have been closed due to resched */
292

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
			Debug( LDAP_DEBUG_CONNS,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
297
298
				"connection_get(%d): connection not used\n",
				s, 0, 0 );
299

300
			ldap_pvt_thread_mutex_unlock( &c->c_mutex );
301
302
303
			return NULL;
		}

304
		Debug( LDAP_DEBUG_TRACE,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
305
			"connection_get(%d): got connid=%lu\n",
306
			s, c->c_connid, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
307

308
309
		c->c_n_get++;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
310
311
		assert( c->c_struct_state == SLAP_C_USED );
		assert( c->c_conn_state != SLAP_C_INVALID );
Howard Chu's avatar
Howard Chu committed
312
		assert( c->c_sd != AC_SOCKET_INVALID );
313

314
#ifndef SLAPD_MONITOR
315
		if ( global_idletimeout > 0 )
316
317
#endif /* ! SLAPD_MONITOR */
		{
318
319
			c->c_activitytime = slap_get_time();
		}
320
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
321

322
323
324
325
326
327
328
329
	return c;
}

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

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

345
346
	assert( connections != NULL );

Pierangelo Masarati's avatar
Pierangelo Masarati committed
347
	assert( listener != NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
348
349
350
	assert( dnsname != NULL );
	assert( peername != NULL );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
351
#ifndef HAVE_TLS
Howard Chu's avatar
Howard Chu committed
352
	assert( !( flags & CONN_IS_TLS ));
Kurt Zeilenga's avatar
Kurt Zeilenga committed
353
354
#endif

355
	if( s == AC_SOCKET_INVALID ) {
Gary Williams's avatar
Gary Williams committed
356
		Debug( LDAP_DEBUG_ANY,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
357
			"connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
358
		return NULL;
359
360
361
	}

	assert( s >= 0 );
362
	assert( s < dtblsize );
Howard Chu's avatar
Howard Chu committed
363
364
365
366
367
368
	c = &connections[s];
	if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
		doinit = 1;
	} else {
		assert( c->c_struct_state == SLAP_C_UNUSED );
	}
369

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

377
378
379
		BER_BVZERO( &c->c_authmech );
		BER_BVZERO( &c->c_dn );
		BER_BVZERO( &c->c_ndn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
380

Pierangelo Masarati's avatar
Pierangelo Masarati committed
381
		c->c_listener = NULL;
382
383
		BER_BVZERO( &c->c_peer_domain );
		BER_BVZERO( &c->c_peer_name );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
384

385
386
		LDAP_STAILQ_INIT(&c->c_ops);
		LDAP_STAILQ_INIT(&c->c_pending_ops);
387

Kurt Zeilenga's avatar
Kurt Zeilenga committed
388
#ifdef LDAP_X_TXN
Kurt Zeilenga's avatar
Kurt Zeilenga committed
389
		c->c_txn = CONN_TXN_INACTIVE;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
390
391
392
393
		c->c_txn_backend = NULL;
		LDAP_STAILQ_INIT(&c->c_txn_ops);
#endif

394
		BER_BVZERO( &c->c_sasl_bind_mech );
395
396
397
		c->c_sasl_done = 0;
		c->c_sasl_authctx = NULL;
		c->c_sasl_sockctx = NULL;
398
		c->c_sasl_extra = NULL;
399
		c->c_sasl_bindop = NULL;
400

401
402
403
		c->c_sb = ber_sockbuf_alloc( );

		{
404
			ber_len_t max = sockbuf_max_incoming;
405
406
407
			ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
408
		c->c_currentber = NULL;
409

410
411
		/* should check status of thread calls */
		ldap_pvt_thread_mutex_init( &c->c_mutex );
412
413
414
415
		ldap_pvt_thread_mutex_init( &c->c_write1_mutex );
		ldap_pvt_thread_mutex_init( &c->c_write2_mutex );
		ldap_pvt_thread_cond_init( &c->c_write1_cv );
		ldap_pvt_thread_cond_init( &c->c_write2_cv );
416

417
#ifdef LDAP_SLAPI
418
		if ( slapi_plugins_used ) {
419
			slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
420
		}
421
#endif
422
	}
423

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

426
427
428
	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
429
	assert( c->c_listener == NULL );
430
431
	assert( BER_BVISNULL( &c->c_peer_domain ) );
	assert( BER_BVISNULL( &c->c_peer_name ) );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
432
433
	assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
	assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
434
#ifdef LDAP_X_TXN
Kurt Zeilenga's avatar
Kurt Zeilenga committed
435
	assert( c->c_txn == CONN_TXN_INACTIVE );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
436
437
438
	assert( c->c_txn_backend == NULL );
	assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
#endif
439
	assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
440
441
442
	assert( c->c_sasl_done == 0 );
	assert( c->c_sasl_authctx == NULL );
	assert( c->c_sasl_sockctx == NULL );
443
	assert( c->c_sasl_extra == NULL );
444
	assert( c->c_sasl_bindop == NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
445
	assert( c->c_currentber == NULL );
446
	assert( c->c_writewaiter == 0);
447
	assert( c->c_writers == 0);
448

Pierangelo Masarati's avatar
Pierangelo Masarati committed
449
	c->c_listener = listener;
Howard Chu's avatar
Howard Chu committed
450
	c->c_sd = s;
451

Howard Chu's avatar
Howard Chu committed
452
	if ( flags & CONN_IS_CLIENT ) {
453
		c->c_connid = 0;
454
		ldap_pvt_thread_mutex_lock( &connections_mutex );
455
		c->c_conn_state = SLAP_C_CLIENT;
456
		c->c_struct_state = SLAP_C_USED;
457
		ldap_pvt_thread_mutex_unlock( &connections_mutex );
458
		c->c_close_reason = "?";			/* should never be needed */
459
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &sfd );
460
461
		ldap_pvt_thread_mutex_unlock( &c->c_mutex );

462
		return c;
463
464
	}

465
466
	ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
	ber_str2bv( peername, 0, 1, &c->c_peer_name );
467

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
468
469
470
471
	c->c_n_ops_received = 0;
	c->c_n_ops_executing = 0;
	c->c_n_ops_pending = 0;
	c->c_n_ops_completed = 0;
472
473
474
475

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

477
478
	/* set to zero until bind, implies LDAP_VERSION3 */
	c->c_protocol = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
479

480
#ifndef SLAPD_MONITOR
481
	if ( global_idletimeout > 0 )
482
483
#endif /* ! SLAPD_MONITOR */
	{
484
485
		c->c_activitytime = c->c_starttime = slap_get_time();
	}
486

487
488
#ifdef LDAP_CONNECTIONLESS
	c->c_is_udp = 0;
Howard Chu's avatar
Howard Chu committed
489
	if( flags & CONN_IS_UDP ) {
490
491
		c->c_is_udp = 1;
#ifdef LDAP_DEBUG
Kurt Zeilenga's avatar
Kurt Zeilenga committed
492
493
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
			LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
494
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
495
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
496
			LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
497
498
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
			LBER_SBIOD_LEVEL_PROVIDER, NULL );
499
	} else
Howard Chu's avatar
Howard Chu committed
500
501
502
503
504
505
#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_" );
506
#endif
Howard Chu's avatar
Howard Chu committed
507
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_fd,
508
			LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
Howard Chu's avatar
Howard Chu committed
509
510
511
512
513
514
#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 */
515
	{
516
#ifdef LDAP_DEBUG
Kurt Zeilenga's avatar
Kurt Zeilenga committed
517
518
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
			LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
519
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
520
		ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
521
			LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
522
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
523
524
525
526

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
529
530
531
532
533
	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
534
			s, c->c_peer_name.bv_val, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
535
	}
536

537
	ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
538
	id = c->c_connid = conn_nextid++;
539
	ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
540

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

547
548
549
	c->c_ssf = c->c_transport_ssf = ssf;
	c->c_tls_ssf = 0;

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

560
	slap_sasl_open( c, 0 );
561
	slap_sasl_external( c, ssf, authid );
562

563
	slapd_add_internal( s, 1 );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
564
	ldap_pvt_thread_mutex_unlock( &c->c_mutex );
565

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
566
	backend_connection_init(c);
567

568
	return c;
569
570
}

571
572
void connection2anonymous( Connection *c )
{
573
574
	assert( connections != NULL );
	assert( c != NULL );
575

576
	{
577
		ber_len_t max = sockbuf_max_incoming;
578
579
580
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
	}

581
	if ( !BER_BVISNULL( &c->c_authmech ) ) {
582
		ch_free(c->c_authmech.bv_val);
583
	}
584
	BER_BVZERO( &c->c_authmech );
585

586
	if ( !BER_BVISNULL( &c->c_dn ) ) {
587
		ch_free(c->c_dn.bv_val);
588
	}
589
	BER_BVZERO( &c->c_dn );
590

591
	if ( !BER_BVISNULL( &c->c_ndn ) ) {
592
		ch_free(c->c_ndn.bv_val);
593
	}
594
	BER_BVZERO( &c->c_ndn );
595

596
597
	if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
		ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
598
599
	}
	BER_BVZERO( &c->c_sasl_authz_dn );
600
601
602
603

	c->c_authz_backend = NULL;
}

604
605
606
static void
connection_destroy( Connection *c )
{
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
607
	unsigned long	connid;
608
	const char		*close_reason;
Howard Chu's avatar
Howard Chu committed
609
	Sockbuf			*sb;
Howard Chu's avatar
Howard Chu committed
610
	ber_socket_t	sd;
611

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
612
613
614
615
616
	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
617
618
619
620
621
622
	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
623
	assert( c->c_writewaiter == 0);
624
	assert( c->c_writers == 0);
625

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

Howard Chu's avatar
Howard Chu committed
630
631
632
633
	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
634
	backend_connection_destroy(c);
635

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
636
637
	c->c_protocol = 0;
	c->c_connid = -1;
638

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
639
	c->c_activitytime = c->c_starttime = 0;
640

641
	connection2anonymous( c );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
642
	c->c_listener = NULL;
643

644
645
	if(c->c_peer_domain.bv_val != NULL) {
		free(c->c_peer_domain.bv_val);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
646
	}
647
	BER_BVZERO( &c->c_peer_domain );
648
649
	if(c->c_peer_name.bv_val != NULL) {
		free(c->c_peer_name.bv_val);
650
	}
651
	BER_BVZERO( &c->c_peer_name );
652

653
	c->c_sasl_bind_in_progress = 0;
654
655
	if(c->c_sasl_bind_mech.bv_val != NULL) {
		free(c->c_sasl_bind_mech.bv_val);
656
	}
657
	BER_BVZERO( &c->c_sasl_bind_mech );
658
659

	slap_sasl_close( c );
660

Kurt Zeilenga's avatar
Kurt Zeilenga committed
661
662
663
664
665
	if ( c->c_currentber != NULL ) {
		ber_free( c->c_currentber, 1 );
		c->c_currentber = NULL;
	}

666

Howard Chu's avatar
Howard Chu committed
667
668
669
670
#ifdef LDAP_SLAPI
	/* call destructors, then constructors; avoids unnecessary allocation */
	if ( slapi_plugins_used ) {
		slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
671
	}
Howard Chu's avatar
Howard Chu committed
672
#endif
673

Howard Chu's avatar
Howard Chu committed
674
675
	sd = c->c_sd;
	c->c_sd = AC_SOCKET_INVALID;
Howard Chu's avatar
Howard Chu committed
676
677
678
	c->c_conn_state = SLAP_C_INVALID;
	c->c_struct_state = SLAP_C_UNUSED;
	c->c_close_reason = "?";			/* should never be needed */
679

Howard Chu's avatar
Howard Chu committed
680
681
	sb = c->c_sb;
	c->c_sb = ber_sockbuf_alloc( );
682
	{
683
		ber_len_t max = sockbuf_max_incoming;
684
685
686
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
	}

Howard Chu's avatar
Howard Chu committed
687
688
689
	/* 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
690
691
	if ( sd != AC_SOCKET_INVALID ) {
		slapd_remove( sd, sb, 1, 0, 0 );
Howard Chu's avatar
Howard Chu committed
692

693
		if ( close_reason == NULL ) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
694
			Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed\n",
Howard Chu's avatar
Howard Chu committed
695
				connid, (long) sd, 0, 0, 0 );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
696
697
		} else {
			Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
Howard Chu's avatar
Howard Chu committed
698
				connid, (long) sd, close_reason, 0, 0 );
699
		}
700
	}
701
702
}

703
int connection_valid( Connection *c )
704
{
705
706
	/* c_mutex must be locked by caller */

707
	assert( c != NULL );
708

709
710
711
	return c->c_struct_state == SLAP_C_USED &&
		c->c_conn_state >= SLAP_C_ACTIVE &&
		c->c_conn_state <= SLAP_C_CLIENT;
712
713
}

714
715
716
717
static void connection_abandon( Connection *c )
{
	/* c_mutex must be locked by caller */

718
	Operation *o, *next, op = {0};
Howard Chu's avatar
Howard Chu committed
719
	Opheader ohdr = {0};
720
	SlapReply rs = {0};
721

Howard Chu's avatar
Howard Chu committed
722
723
724
	op.o_hdr = &ohdr;
	op.o_conn = c;
	op.o_connid = c->c_connid;
725
	op.o_tag = LDAP_REQ_ABANDON;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
726

727
728
729
	for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
		next = LDAP_STAILQ_NEXT( o, o_next );
		op.orn_msgid = o->o_msgid;
730
		o->o_abandon = 1;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
731
		op.o_bd = frontendDB;
732
		frontendDB->be_abandon( &op, &rs );
733
734
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
735
736
737
738
739
#ifdef LDAP_X_TXN
	/* remove operations in pending transaction */
	while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
		LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
		LDAP_STAILQ_NEXT(o, o_next) = NULL;
Howard Chu's avatar
Howard Chu committed
740
		slap_op_free( o, NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
741
742
743
744
745
746
747
	}

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

748
	/* remove pending operations */
749
750
751
	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
752
		slap_op_free( o, NULL );
753
754
755
	}
}

756
void connection_closing( Connection *c, const char *why )
757
758
759
760
761
762
{
	assert( connections != NULL );
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );
	assert( c->c_conn_state != SLAP_C_INVALID );

763
764
	/* c_mutex must be locked by caller */

765
	if( c->c_conn_state != SLAP_C_CLOSING ) {
766
		Debug( LDAP_DEBUG_CONNS,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
767
			"connection_closing: readying conn=%lu sd=%d for close\n",
Howard Chu's avatar
Howard Chu committed
768
			c->c_connid, c->c_sd, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
769
770
		/* update state to closing */
		c->c_conn_state = SLAP_C_CLOSING;
771
		c->c_close_reason = why;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
772

773
		/* don't listen on this port anymore */
Howard Chu's avatar
Howard Chu committed
774
		slapd_clr_read( c->c_sd, 0 );
775
776

		/* abandon active operations */
777
		connection_abandon( c );
778
779

		/* wake write blocked operations */
780
		ldap_pvt_thread_mutex_lock( &c->c_write1_mutex );
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
		if ( c->c_writers > 0 ) {
			c->c_writers = -c->c_writers;
			ldap_pvt_thread_cond_broadcast( &c->c_write1_cv );
			ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
			if ( c->c_writewaiter ) {
				ldap_pvt_thread_mutex_lock( &c->c_write2_mutex );
				ldap_pvt_thread_cond_signal( &c->c_write2_cv );
				slapd_clr_write( c->c_sd, 1 );
				ldap_pvt_thread_mutex_unlock( &c->c_write2_mutex );
			}
			ldap_pvt_thread_mutex_lock( &c->c_write1_mutex );
			while ( c->c_writers ) {
				ldap_pvt_thread_cond_wait( &c->c_write1_cv, &c->c_write1_mutex );
			}
			ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
796
		} else {
797
			ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
Howard Chu's avatar
Howard Chu committed
798
			slapd_clr_write( c->c_sd, 1 );
799
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
800

801
802
803
	} else if( why == NULL && c->c_close_reason == conn_lost_str ) {
		/* Client closed connection after doing Unbind. */
		c->c_close_reason = NULL;
804
805
806
	}
}

807
808
static void
connection_close( Connection *c )
809
810
811
812
{
	assert( connections != NULL );
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );
Howard Chu's avatar
Howard Chu committed
813
	assert( c->c_conn_state == SLAP_C_CLOSING );
814

815
	/* NOTE: c_mutex should be locked by caller */
816

817
818
819
	if ( !LDAP_STAILQ_EMPTY(&c->c_ops) ||
		!LDAP_STAILQ_EMPTY(&c->c_pending_ops) )
	{
820
		Debug( LDAP_DEBUG_CONNS,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
821
			"connection_close: deferring conn=%lu sd=%d\n",
Howard Chu's avatar
Howard Chu committed
822
			c->c_connid, c->c_sd, 0 );
823
824
825
		return;
	}

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

829
830
831
	connection_destroy( c );
}