connection.c 35.6 KB
Newer Older
1
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
4
5
6
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
7
#include "portable.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
8
9

#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
10
#include <limits.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
11

12
#include <ac/socket.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
13
14
15
16
#include <ac/errno.h>
#include <ac/signal.h>
#include <ac/string.h>
#include <ac/time.h>
Pierangelo Masarati's avatar
Pierangelo Masarati committed
17
#include <ac/unistd.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
18

Kurt Zeilenga's avatar
Kurt Zeilenga committed
19
#include "ldap_pvt.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
20
#include "lutil.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
21
22
#include "slap.h"

23
24
25
/* protected by connections_mutex */
static ldap_pvt_thread_mutex_t connections_mutex;
static Connection *connections = NULL;
26
static unsigned long conn_nextid = 0;
27

28
/* structure state (protected by connections_mutex) */
29
30
31
#define SLAP_C_UNINITIALIZED	0x00	/* MUST BE ZERO (0) */
#define SLAP_C_UNUSED			0x01
#define SLAP_C_USED				0x02
32
33

/* connection state (protected by c_mutex ) */
34
35
36
37
38
#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 */
39

40
41
42
const char *
connection_state2str( int state )
{
43
44
45
46
47
48
49
50
51
52
53
	switch( state ) {
	case SLAP_C_INVALID:	return "!";		
	case SLAP_C_INACTIVE:	return "|";		
	case SLAP_C_ACTIVE:		return "";			
	case SLAP_C_BINDING:	return "B";
	case SLAP_C_CLOSING:	return "C";			
	}

	return "?";
}

54
static Connection* connection_get( ber_socket_t s );
55
56

static int connection_input( Connection *c );
57
static void connection_close( Connection *c );
58

59
60
static int connection_op_activate( Connection *conn, Operation *op );
static int connection_resched( Connection *conn );
61
static void connection_abandon( Connection *conn );
62
static void connection_destroy( Connection *c );
63

Kurt Zeilenga's avatar
Kurt Zeilenga committed
64
65
66
67
68
struct co_arg {
	Connection	*co_conn;
	Operation	*co_op;
};

69
70
71
72
73
74
75
/*
 * Initialize connection management infrastructure.
 */
int connections_init(void)
{
	assert( connections == NULL );

76
	if( connections != NULL) {
77
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
78
79
		LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
			   "connections_init:  already initialized.\n" ));
80
#else
81
82
		Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
			0, 0, 0 );
83
#endif
84
85
86
87
88
89
90
91
92
		return -1;
	}

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

	connections = (Connection *) calloc( dtblsize, sizeof(Connection) );

	if( connections == NULL ) {
93
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
94
95
96
		LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
			   "connections_init: allocation (%d * %ld) of connection array failed\n",
			   dtblsize, (long) sizeof(Connection) ));
97
#else
98
		Debug( LDAP_DEBUG_ANY,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
99
			"connections_init: allocation (%d*%ld) of connection array failed\n",
100
			dtblsize, (long) sizeof(Connection), 0 );
101
#endif
102
103
104
		return -1;
	}

105
106
107
    assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
    assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );

108
109
110
111
112
113
114
115
	/*
	 * per entry initialization of the Connection array initialization
	 * will be done by connection_init()
	 */ 

	return 0;
}

116
117
118
119
120
/*
 * Destroy connection management infrastructure.
 */
int connections_destroy(void)
{
121
	ber_socket_t i;
122
123
124
125

	/* should check return of every call */

	if( connections == NULL) {
126
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
127
128
		LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
			   "connections_destroy: nothing to destroy.\n"));
129
#else
130
131
		Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
			0, 0, 0 );
132
#endif
133
134
135
136
		return -1;
	}

	for ( i = 0; i < dtblsize; i++ ) {
137
		if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
138
			ber_sockbuf_free( connections[i].c_sb );
139
140
141
142
			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 );
		}
143
144
145
146
147
148
149
150
151
152
153
154
155
156
	}

	free( connections );
	connections = NULL;

	ldap_pvt_thread_mutex_destroy( &connections_mutex );
	return 0;
}

/*
 * shutdown all connections
 */
int connections_shutdown(void)
{
157
	ber_socket_t i;
158
159
160
161
162
163
164
165
166

	ldap_pvt_thread_mutex_lock( &connections_mutex );

	for ( i = 0; i < dtblsize; i++ ) {
		if( connections[i].c_struct_state != SLAP_C_USED ) {
			continue;
		}

		ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
167
168

		/* connections_mutex and c_mutex are locked */
169
170
		connection_closing( &connections[i] );
		connection_close( &connections[i] );
171

172
173
174
175
		ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
	}

	ldap_pvt_thread_mutex_unlock( &connections_mutex );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
176
177

	return 0;
178
179
}

180
181
182
183
184
185
186
187
188
/*
 * Timeout idle connections.
 */
int connections_timeout_idle(time_t now)
{
	int i = 0;
	int connindex;
	Connection* c;

Gary Williams's avatar
Gary Williams committed
189
	for( c = connection_first( &connindex );
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
		c != NULL;
		c = connection_next( c, &connindex ) )
	{
		if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
			/* close it */
			connection_closing( c );
			connection_close( c );
			i++;
		}
	}
	connection_done( c );

	return i;
}

205
static Connection* connection_get( ber_socket_t s )
206
{
207
208
	/* connections_mutex should be locked by caller */

209
210
	Connection *c;

211
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
212
213
	LDAP_LOG(( "connection", LDAP_LEVEL_ENTRY,
		   "connection_get: socket %ld\n", (long)s ));
214
#else
215
	Debug( LDAP_DEBUG_ARGS,
216
217
		"connection_get(%ld)\n",
		(long) s, 0, 0 );
218
#endif
219
220
221

	assert( connections != NULL );

222
	if(s == AC_SOCKET_INVALID) {
223
224
225
226
227
		return NULL;
	}

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

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

231
#else
232
	c = NULL;
233
	{
234
		ber_socket_t i, sd;
235
236

		for(i=0; i<dtblsize; i++) {
237
238
239
			ber_sockbuf_ctrl( connections[i].c_sb,
				LBER_SB_OPT_GET_FD, &sd );

Gary Williams's avatar
NT port    
Gary Williams committed
240
			if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
241
				assert( connections[i].c_conn_state == SLAP_C_INVALID );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
242
				assert( connections[i].c_sb == 0 );
243
244
245
				break;
			}

Gary Williams's avatar
NT port    
Gary Williams committed
246
			if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
247
				assert( connections[i].c_conn_state == SLAP_C_INVALID );
248
				assert( sd == AC_SOCKET_INVALID );
249
250
251
				continue;
			}

252
253
254
			/* state can actually change from used -> unused by resched,
			 * so don't assert details here.
			 */
255

256
			if( sd == s ) {
Gary Williams's avatar
NT port    
Gary Williams committed
257
				c = &connections[i];
258
259
260
261
262
				break;
			}
		}
	}
#endif
263

264
	if( c != NULL ) {
265
266
		ber_socket_t	sd;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
267
268
		ldap_pvt_thread_mutex_lock( &c->c_mutex );

269
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
270
271
		if( c->c_struct_state != SLAP_C_USED ) {
			/* connection must have been closed due to resched */
272

273
			assert( c->c_conn_state == SLAP_C_INVALID );
274
			assert( sd == AC_SOCKET_INVALID );
275

276
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
277
278
			LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
				   "connection_get:  connection %d not used\n", s ));
279
#else
280
			Debug( LDAP_DEBUG_TRACE,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
281
282
				"connection_get(%d): connection not used\n",
				s, 0, 0 );
283
#endif
284

285
			ldap_pvt_thread_mutex_unlock( &c->c_mutex );
286
287
288
			return NULL;
		}

289
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
290
291
		LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
			   "connection_get: get for %d got connid %ld\n",s, c->c_connid ));
292
#else
293
294
295
		Debug( LDAP_DEBUG_TRACE,
			"connection_get(%d): got connid=%ld\n",
			s, c->c_connid, 0 );
296
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
297

298
299
		c->c_n_get++;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
300
301
		assert( c->c_struct_state == SLAP_C_USED );
		assert( c->c_conn_state != SLAP_C_INVALID );
302
		assert( sd != AC_SOCKET_INVALID );
303

304
305
306
307
308
309
310
#ifdef SLAPD_MONITOR
		c->c_activitytime = slap_get_time();
#else
		if( global_idletimeout > 0 ) {
			c->c_activitytime = slap_get_time();
		}
#endif
311
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
312

313
314
315
316
317
318
319
320
321
	return c;
}

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

long connection_init(
322
	ber_socket_t s,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
323
324
325
326
	const char* url,
	const char* dnsname,
	const char* peername,
	const char* sockname,
327
	int tls_udp_option,
328
	slap_ssf_t ssf,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
329
	const char *authid )
330
{
331
	unsigned long id;
332
	Connection *c;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
333

334
335
	assert( connections != NULL );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
336
337
338
339
	assert( dnsname != NULL );
	assert( peername != NULL );
	assert( sockname != NULL );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
340
#ifndef HAVE_TLS
341
	assert( tls_udp_option != 1 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
342
343
#endif

344
	if( s == AC_SOCKET_INVALID ) {
345
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
346
347
		LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
			   "connection_init: init of socket %ld invalid.\n", (long)s ));
348
#else
Gary Williams's avatar
Gary Williams committed
349
350
351
		Debug( LDAP_DEBUG_ANY,
		       "connection_init(%ld): invalid.\n",
		       (long) s, 0, 0 );
352
#endif
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
		return -1;
	}

	assert( s >= 0 );
#ifndef HAVE_WINSOCK
	assert( s < dtblsize );
#endif

	ldap_pvt_thread_mutex_lock( &connections_mutex );

#ifndef HAVE_WINSOCK
	c = &connections[s];

#else
	{
Howard Chu's avatar
Howard Chu committed
368
		ber_socket_t	i;
369

370
371
		c = NULL;

Gary Williams's avatar
Gary Williams committed
372
373
	for( i=0; i < dtblsize; i++) {
		ber_socket_t	sd;
374

Gary Williams's avatar
Gary Williams committed
375
376
377
378
379
	    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
380

381
382
			sd = AC_SOCKET_INVALID;
			if (connections[i].c_sb != NULL)
Gary Williams's avatar
Gary Williams committed
383
			ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
384
	    
Gary Williams's avatar
Gary Williams committed
385
386
387
388
389
390
391
392
393
394
395
396
	    if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
		assert( sd == AC_SOCKET_INVALID );
		c = &connections[i];
		break;
	    }

	    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 ) {
397
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
398
399
400
		LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
			   "connection_init: skt %d	 connection table full (%d/%d)\n",
			   s, i, dtblsize ));
401
#else
Gary Williams's avatar
Gary Williams committed
402
		Debug( LDAP_DEBUG_ANY,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
403
				"connection_init(%d): connection table full (%d/%d)\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
404
				s, i, dtblsize);
405
#endif
Gary Williams's avatar
Gary Williams committed
406
407
408
	    ldap_pvt_thread_mutex_unlock( &connections_mutex );
	    return -1;
	}
Gary Williams's avatar
NT port    
Gary Williams committed
409
    }
410
411
#endif

Gary Williams's avatar
NT port    
Gary Williams committed
412
    assert( c != NULL );
413

414
	if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
415
416
		c->c_authmech.bv_val = NULL;
		c->c_authmech.bv_len = 0;
417
418
419
420
		c->c_dn.bv_val = NULL;
		c->c_dn.bv_len = 0;
		c->c_ndn.bv_val = NULL;
		c->c_ndn.bv_len = 0;
421
422
		c->c_cdn.bv_val = NULL;
		c->c_cdn.bv_len = 0;
423
		c->c_groups = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
424
425
426

		c->c_listener_url = NULL;
		c->c_peer_domain = NULL;
427
428
		c->c_peer_name = NULL;
		c->c_sock_name = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
429

430
431
		LDAP_STAILQ_INIT(&c->c_ops);
		LDAP_STAILQ_INIT(&c->c_pending_ops);
432

433
434
		c->c_sasl_bind_mech.bv_val = NULL;
		c->c_sasl_bind_mech.bv_len = 0;
435
		c->c_sasl_context = NULL;
436
		c->c_sasl_extra = NULL;
437

438
439
440
		c->c_sb = ber_sockbuf_alloc( );

		{
441
			ber_len_t max = sockbuf_max_incoming;
442
443
444
			ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
445
		c->c_currentber = NULL;
446

447
448
449
450
		/* 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 );
451

452
453
		c->c_struct_state = SLAP_C_UNUSED;
	}
454

Gary Williams's avatar
NT port    
Gary Williams committed
455
    ldap_pvt_thread_mutex_lock( &c->c_mutex );
456

Gary Williams's avatar
NT port    
Gary Williams committed
457
    assert( c->c_struct_state == SLAP_C_UNUSED );
458
459
460
461
    assert( c->c_authmech.bv_val == NULL );
    assert( c->c_dn.bv_val == NULL );
    assert( c->c_ndn.bv_val == NULL );
    assert( c->c_cdn.bv_val == NULL );
462
    assert( c->c_groups == NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
463
464
465
466
    assert( c->c_listener_url == NULL );
    assert( c->c_peer_domain == NULL );
    assert( c->c_peer_name == NULL );
    assert( c->c_sock_name == NULL );
467
468
    assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
    assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
469
	assert( c->c_sasl_bind_mech.bv_val == NULL );
470
	assert( c->c_sasl_context == NULL );
471
	assert( c->c_sasl_extra == NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
472
	assert( c->c_currentber == NULL );
473

Kurt Zeilenga's avatar
Kurt Zeilenga committed
474
475
476
477
	c->c_listener_url = ch_strdup( url  );
	c->c_peer_domain = ch_strdup( dnsname  );
    c->c_peer_name = ch_strdup( peername  );
    c->c_sock_name = ch_strdup( sockname );
478

Gary Williams's avatar
NT port    
Gary Williams committed
479
480
481
482
    c->c_n_ops_received = 0;
    c->c_n_ops_executing = 0;
    c->c_n_ops_pending = 0;
    c->c_n_ops_completed = 0;
483
484
485
486

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

488
489
	/* set to zero until bind, implies LDAP_VERSION3 */
	c->c_protocol = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
490

491
492
493
494
495
496
497
#ifdef SLAPD_MONITOR
	c->c_activitytime = c->c_starttime = slap_get_time();
#else
	if( global_idletimeout > 0 ) {
		c->c_activitytime = c->c_starttime = slap_get_time();
	}
#endif
498

499
500
501
502
503
504
505
506
507
508
509
#ifdef LDAP_CONNECTIONLESS
	c->c_is_udp = 0;
	if (tls_udp_option == 2)
	{
		c->c_is_udp = 1;
#ifdef LDAP_DEBUG
	ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
		LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
#endif
	ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
		LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
510
511
	ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
		LBER_SBIOD_LEVEL_PROVIDER, NULL );
512
513
514
	} else
#endif
	{
515
#ifdef LDAP_DEBUG
Kurt Zeilenga's avatar
Kurt Zeilenga committed
516
517
518
519
520
	ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
		LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
#endif
	ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
		LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
521
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
522
523
524
525

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
528
529
530
	if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
		c /* non-NULL */ ) < 0 )
	{
531
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
532
533
534
		LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
			   "connection_init: conn %d  set nonblocking failed\n",
			   c->c_connid ));
535
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
536
537
538
		Debug( LDAP_DEBUG_ANY,
			"connection_init(%d, %s): set nonblocking failed\n",
			s, c->c_peer_name, 0 );
539
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
540
	}
541

Gary Williams's avatar
NT port    
Gary Williams committed
542
    id = c->c_connid = conn_nextid++;
543

Gary Williams's avatar
NT port    
Gary Williams committed
544
545
    c->c_conn_state = SLAP_C_INACTIVE;
    c->c_struct_state = SLAP_C_USED;
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
551
    if ( tls_udp_option == 1 ) {
552
553
	    c->c_is_tls = 1;
	    c->c_needs_tls_accept = 1;
554
555
556
    } else {
	    c->c_is_tls = 0;
	    c->c_needs_tls_accept = 0;
557
    }
Kurt Zeilenga's avatar
Kurt Zeilenga committed
558
#endif
559

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

Gary Williams's avatar
NT port    
Gary Williams committed
563
564
    ldap_pvt_thread_mutex_unlock( &c->c_mutex );
    ldap_pvt_thread_mutex_unlock( &connections_mutex );
565

566
567
    backend_connection_init(c);

Gary Williams's avatar
NT port    
Gary Williams committed
568
    return id;
569
570
}

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

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

581
582
583
	if(c->c_authmech.bv_val != NULL ) {
		free(c->c_authmech.bv_val);
		c->c_authmech.bv_val = NULL;
584
	}
585
	c->c_authmech.bv_len = 0;
586

587
588
589
    if(c->c_dn.bv_val != NULL) {
	free(c->c_dn.bv_val);
	c->c_dn.bv_val = NULL;
590
    }
591
592
593
594
    c->c_dn.bv_len = 0;
    if(c->c_ndn.bv_val != NULL) {
	free(c->c_ndn.bv_val);
	c->c_ndn.bv_val = NULL;
595
    }
596
    c->c_ndn.bv_len = 0;
597

598
599
600
	if(c->c_cdn.bv_val != NULL) {
		free(c->c_cdn.bv_val);
		c->c_cdn.bv_val = NULL;
601
	}
602
	c->c_cdn.bv_len = 0;
603
604
605

	c->c_authc_backend = NULL;
	c->c_authz_backend = NULL;
606
607
608
609
610
611
612
613
614
615
616
    
    {
	GroupAssertion *g, *n;
	for (g = c->c_groups; g; g=n)
	{
	    n = g->next;
	    free(g);
	}
	c->c_groups = NULL;
    }

617
618
}

619
620
621
static void
connection_destroy( Connection *c )
{
622
	/* note: connections_mutex should be locked by caller */
623
    ber_socket_t	sd;
624

Gary Williams's avatar
NT port    
Gary Williams committed
625
626
627
628
    assert( connections != NULL );
    assert( c != NULL );
    assert( c->c_struct_state != SLAP_C_UNUSED );
    assert( c->c_conn_state != SLAP_C_INVALID );
629
    assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
630

631
632
    backend_connection_destroy(c);

Gary Williams's avatar
NT port    
Gary Williams committed
633
    c->c_protocol = 0;
634
    c->c_connid = -1;
635

636
    c->c_activitytime = c->c_starttime = 0;
637

638
639
	connection2anonymous( c );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
640
641
642
643
	if(c->c_listener_url != NULL) {
		free(c->c_listener_url);
		c->c_listener_url = NULL;
	}
644

Kurt Zeilenga's avatar
Kurt Zeilenga committed
645
646
647
648
649
	if(c->c_peer_domain != NULL) {
		free(c->c_peer_domain);
		c->c_peer_domain = NULL;
	}
	if(c->c_peer_name != NULL) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
650
#ifdef LDAP_PF_lOCAL
651
652
653
654
655
656
657
658
659
660
661
		/*
		 * If peer was a domain socket, unlink. Mind you,
		 * they may be un-named. Should we leave this to
		 * the client?
		 */
		if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
			char *path = c->c_peer_name + 5;
			if (path != '\0') {
				(void)unlink(path);
			}
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
662
#endif /* LDAP_PF_LOCAL */
663

Kurt Zeilenga's avatar
Kurt Zeilenga committed
664
665
		free(c->c_peer_name);
		c->c_peer_name = NULL;
666
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
667
668
669
	if(c->c_sock_name != NULL) {
		free(c->c_sock_name);
		c->c_sock_name = NULL;
670
671
	}

672
	c->c_sasl_bind_in_progress = 0;
673
674
675
	if(c->c_sasl_bind_mech.bv_val != NULL) {
		free(c->c_sasl_bind_mech.bv_val);
		c->c_sasl_bind_mech.bv_val = NULL;
676
	}
677
	c->c_sasl_bind_mech.bv_len = 0;
678
679

	slap_sasl_close( c );
680

Kurt Zeilenga's avatar
Kurt Zeilenga committed
681
682
683
684
685
	if ( c->c_currentber != NULL ) {
		ber_free( c->c_currentber, 1 );
		c->c_currentber = NULL;
	}

686
687
	ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
	if ( sd != AC_SOCKET_INVALID ) {
688
		slapd_remove( sd, 0 );
689
690

		Statslog( LDAP_DEBUG_STATS,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
691
		    "conn=%ld fd=%d closed\n",
692
693
694
			c->c_connid, sd, 0, 0, 0 );
	}

Gary Williams's avatar
Gary Williams committed
695
	ber_sockbuf_free( c->c_sb );
696

697
	c->c_sb = ber_sockbuf_alloc( );
698

699
	{
700
		ber_len_t max = sockbuf_max_incoming;
701
702
703
		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
	}

704
705
    c->c_conn_state = SLAP_C_INVALID;
    c->c_struct_state = SLAP_C_UNUSED;
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
727
728
static void connection_abandon( Connection *c )
{
	/* c_mutex must be locked by caller */

	Operation *o;

729
	LDAP_STAILQ_FOREACH(o, &c->c_ops, o_next) {
730
731
732
733
734
735
		ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
		o->o_abandon = 1;
		ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
	}

	/* remove pending operations */
736
737
738
	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;
739
740
741
742
		slap_op_free( o );
	}
}

743
744
745
746
747
748
749
void connection_closing( Connection *c )
{
	assert( connections != NULL );
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );
	assert( c->c_conn_state != SLAP_C_INVALID );

750
751
	/* c_mutex must be locked by caller */

752
	if( c->c_conn_state != SLAP_C_CLOSING ) {
753
754
755
		ber_socket_t	sd;

		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
756
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
757
758
759
		LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
			   "connection_closing: conn %d readying socket %d for close.\n",
			   c->c_connid, sd ));
760
#else
761
		Debug( LDAP_DEBUG_TRACE,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
762
			"connection_closing: readying conn=%ld sd=%d for close\n",
763
			c->c_connid, sd, 0 );
764
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
765
766
767
		/* update state to closing */
		c->c_conn_state = SLAP_C_CLOSING;

768
		/* don't listen on this port anymore */
769
		slapd_clr_read( sd, 1 );
770
771

		/* abandon active operations */
772
		connection_abandon( c );
773
774

		/* wake write blocked operations */
775
		slapd_clr_write( sd, 1 );
776
		ldap_pvt_thread_cond_signal( &c->c_write_cv );
777
778
779
	}
}

780
781
static void connection_close( Connection *c )
{
782
783
	ber_socket_t	sd;

784
785
786
787
788
	assert( connections != NULL );
	assert( c != NULL );
	assert( c->c_struct_state == SLAP_C_USED );
	assert( c->c_conn_state == SLAP_C_CLOSING );

789
	/* note: connections_mutex and c_mutex should be locked by caller */
790

791
	ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
792
	if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
793
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
794
795
796
		LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
			   "connection_close: conn %d  deferring sd %d\n",
			   c->c_connid, sd ));
797
#else
798
		Debug( LDAP_DEBUG_TRACE,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
799
			"connection_close: deferring conn=%ld sd=%d\n",
800
			c->c_connid, sd, 0 );
801
#endif
802
803
804
		return;
	}

805
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
806
807
808
	LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
		   "connection_close: conn %d  sd %d\n",
		   c->c_connid, sd ));
809
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
810
	Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
811
		c->c_connid, sd, 0 );
812
#endif
813
814
815
	connection_destroy( c );
}

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

	ldap_pvt_thread_mutex_lock( &connections_mutex );

	id = conn_nextid;

	ldap_pvt_thread_mutex_unlock( &connections_mutex );

	return id;
}

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

	ldap_pvt_thread_mutex_lock( &connections_mutex );

837
	*index = 0;
838

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

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

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

	c = NULL;

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

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

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

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

	return c;
}

881
void connection_done( Connection *c )
882
883
884
885
886
887
888
889
890
891
{
	assert( connections != NULL );

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

	ldap_pvt_thread_mutex_unlock( &connections_mutex );
}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
892
893
894
895
896
897
/*
 * connection_activity - handle the request operation op on connection
 * conn.  This routine figures out what kind of operation it is and
 * calls the appropriate stub to handle it.
 */

898
899
static void *
connection_operation( void *arg_v )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
900
{
901
	int rc;
902
	struct co_arg	*arg = arg_v;
903
	ber_tag_t tag = arg->co_op->o_tag;
904
	Connection *conn = arg->co_conn;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
905

Kurt Zeilenga's avatar
Kurt Zeilenga committed
906
907
908
	ldap_pvt_thread_mutex_lock( &num_ops_mutex );
	num_ops_initiated++;
	ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
909

910
	if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
911
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
912
913
914
		LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
			   "connection_operation: conn %d  SASL bind in progress (tag=%ld).\n",
			   conn->c_connid, (long)tag ));
915
#else
916
917
918
		Debug( LDAP_DEBUG_ANY, "connection_operation: "
			"error: SASL bind in progress (tag=%ld).\n",
			(long) tag, 0, 0 );
919
#endif
920
921
		send_ldap_result( conn, arg->co_op,
			rc = LDAP_OPERATIONS_ERROR,
922
923
924
925
			NULL, "SASL bind in progress", NULL, NULL );
		goto operations_error;
	}

926
	switch ( tag ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
927
	case LDAP_REQ_BIND:
928
		rc = do_bind( conn, arg->co_op );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
929
930
931
		break;

	case LDAP_REQ_UNBIND:
932
		rc = do_unbind( conn, arg->co_op );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
933
934
935
		break;

	case LDAP_REQ_ADD:
936
		rc = do_add( conn, arg->co_op );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
937
938
939
		break;

	case LDAP_REQ_DELETE:
940
		rc = do_delete( conn, arg->co_op );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
941
942
943
		break;

	case LDAP_REQ_MODRDN:
944
		rc = do_modrdn( conn, arg->co_op );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
945
946
947
		break;

	case LDAP_REQ_MODIFY:
948
		rc = do_modify( conn, arg->co_op );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
949
950
951
		break;

	case LDAP_REQ_COMPARE:
952
		rc = do_compare( conn, arg->co_op );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
953
954
955
		break;

	case LDAP_REQ_SEARCH:
956
		rc = do_search( conn, arg->co_op );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
957
958
959
		break;

	case LDAP_REQ_ABANDON:
960
		rc = do_abandon( conn, arg->co_op );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
961
962
		break;

963
	case LDAP_REQ_EXTENDED:
964
		rc = do_extended( conn, arg->co_op );