shadowircd / src / sslproc.c

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/*
 *  sslproc.c: An interface to ssld
 *  Copyright (C) 2007 Aaron Sethman <androsyn@ratbox.org>
 *  Copyright (C) 2007 ircd-ratbox development team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 *  USA
 *
 *  $Id: sslproc.c 25179 2008-03-30 16:34:57Z androsyn $
 */

#include <ratbox_lib.h>
#include "stdinc.h"
#include "s_conf.h"
#include "logger.h"
#include "listener.h"
#include "sslproc.h"
#include "s_serv.h"
#include "ircd.h"
#include "hash.h"
#include "client.h"
#include "send.h"
#include "packet.h"

#define ZIPSTATS_TIME           60

static void collect_zipstats(void *unused);
static void ssl_read_ctl(rb_fde_t *F, void *data);
static int ssld_count;

static char tmpbuf[READBUF_SIZE];	
static char nul = '\0';

#define MAXPASSFD 4
#define READSIZE 1024
typedef struct _ssl_ctl_buf
{
	rb_dlink_node node;
	char *buf;
	size_t buflen;
	rb_fde_t *F[MAXPASSFD];
	int nfds;
} ssl_ctl_buf_t;


struct _ssl_ctl
{
	rb_dlink_node node;
	int cli_count;
	rb_fde_t *F;
	rb_fde_t *P;
	pid_t pid;
	rb_dlink_list readq;
	rb_dlink_list writeq;
	rb_uint8_t dead;
};

static void send_new_ssl_certs_one(ssl_ctl_t *ctl, const char *ssl_cert, const char *ssl_private_key, const char *ssl_dh_params);
static void send_init_prng(ssl_ctl_t *ctl, prng_seed_t seedtype, const char *path);


static rb_dlink_list ssl_daemons;

static inline rb_int32_t buf_to_int32(char *buf)
{
	rb_int32_t x;
	x = *buf << 24;
	x |= *(++buf) << 16;
	x |= *(++buf) << 8;
	x |= *(++buf);
	return x;
}

static inline void int32_to_buf(char *buf, rb_int32_t x)
{
	*(buf)   = x >> 24 & 0xFF;
	*(++buf) = x >> 16 & 0xFF; 
	*(++buf) = x >> 8 & 0xFF;
	*(++buf) = x & 0xFF;
	return;
}


static inline rb_uint16_t buf_to_uint16(char *buf)
{
	rb_uint16_t x;
	x = *(buf) << 8;
	x |= *(++buf);
	return x;
}

static inline void uint16_to_buf(char *buf, rb_uint16_t x)
{
	*(buf) = x >> 8 & 0xFF;
	*(++buf) = x & 0xFF;
	return;
}


static ssl_ctl_t *
allocate_ssl_daemon(rb_fde_t *F, rb_fde_t *P, int pid)
{
	ssl_ctl_t *ctl;
	
	if(F == NULL || pid < 0)
		return NULL;
	ctl = rb_malloc(sizeof(ssl_ctl_t));	
	ctl->F = F;
	ctl->P = P;
	ctl->pid = pid;
	ssld_count++;
	rb_dlinkAdd(ctl, &ctl->node, &ssl_daemons);
	return ctl;
}

static void
free_ssl_daemon(ssl_ctl_t *ctl)
{
	rb_dlink_node *ptr;
	ssl_ctl_buf_t *ctl_buf;
	int x;
	if(ctl->cli_count)
		return;
	
	RB_DLINK_FOREACH(ptr, ctl->readq.head)
	{
		ctl_buf = ptr->data;
		for(x = 0; x < ctl_buf->nfds; x++)
			rb_close(ctl_buf->F[x]);	

		rb_free(ctl_buf->buf);
		rb_free(ctl_buf);	
	}

	RB_DLINK_FOREACH(ptr, ctl->writeq.head)
	{
		ctl_buf = ptr->data;
		for(x = 0; x < ctl_buf->nfds; x++)
			rb_close(ctl_buf->F[x]);

		rb_free(ctl_buf->buf);
		rb_free(ctl_buf);
	}
	rb_close(ctl->F);
	rb_close(ctl->P);
	rb_dlinkDelete(&ctl->node, &ssl_daemons);
	rb_free(ctl);
}

static char *ssld_path;

static int ssld_spin_count = 0;
static time_t last_spin;
static int ssld_wait = 0;


static void
ssl_killall(void)
{
	rb_dlink_node *ptr, *next;
	ssl_ctl_t *ctl;
	RB_DLINK_FOREACH_SAFE(ptr, next, ssl_daemons.head)
	{
		ctl = ptr->data;
		if(ctl->dead)
			continue;
		ctl->dead = 1;
		ssld_count--;
		kill(ctl->pid, SIGKILL);
	}
}

static void
ssl_dead(ssl_ctl_t *ctl)
{
	if(ctl->dead)
		return;
		
	ctl->dead = 1;
	ssld_count--;
	kill(ctl->pid, SIGKILL); /* make sure the process is really gone */
	ilog(L_MAIN, "ssld helper died - attempting to restart");
	sendto_realops_snomask(SNO_GENERAL, L_ALL, "ssld helper died - attempting to restart");
	start_ssldaemon(1, ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params);
}

static void
ssl_do_pipe(rb_fde_t *F, void *data)
{
	int retlen;
	ssl_ctl_t *ctl = data;
	retlen = rb_write(F, "0", 1);
	if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
	{
		ssl_dead(ctl);
		return;
	}
	rb_setselect(F, RB_SELECT_READ, ssl_do_pipe, data);
}

static void
restart_ssld_event(void *unused)
{
	ssld_spin_count = 0;
	last_spin = 0;
	ssld_wait = 0;
	if(ServerInfo.ssld_count > get_ssld_count())
	{
		int start = ServerInfo.ssld_count - get_ssld_count();
		ilog(L_MAIN, "Attempting to restart ssld processes");
		sendto_realops_snomask(SNO_GENERAL, L_ALL, "Attempt to restart ssld processes");
		start_ssldaemon(start, ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params);
	}
}

int
start_ssldaemon(int count, const char *ssl_cert, const char *ssl_private_key, const char *ssl_dh_params)
{
	rb_fde_t *F1, *F2;
	rb_fde_t *P1, *P2;
	char fullpath[PATH_MAX + 1];
	char fdarg[6];
	const char *parv[2];
	char buf[128];
	pid_t pid;
	int started = 0, i;

	if(ssld_wait)
		return 0;

	if(ssld_spin_count > 20 && (rb_current_time() - last_spin < 5))
	{
		ilog(L_MAIN, "ssld helper is spinning - will attempt to restart in 5 minutes");
		sendto_realops_snomask(SNO_GENERAL, L_ALL, "ssld helper is spinning - will attempt to restart in 1 minute");
		rb_event_add("restart_ssld_event", restart_ssld_event, NULL, 60);
		ssld_wait = 1;
		return 0;
	}

	ssld_spin_count++;
	last_spin = rb_current_time();
	if(ssld_path == NULL)
	{
		rb_snprintf(fullpath, sizeof(fullpath), "%s/ssld", BINPATH);
		
		if(access(fullpath, X_OK) == -1)
		{
			rb_snprintf(fullpath, sizeof(fullpath), "%s/bin/ssld", ConfigFileEntry.dpath);
			if(access(fullpath, X_OK) == -1)
			{
				ilog(L_MAIN, "Unable to execute ssld in %s/bin or %s", ConfigFileEntry.dpath, BINPATH);
				return 0 ;
			}
		}
		ssld_path = rb_strdup(fullpath);
	}

	rb_strlcpy(buf, "-ircd ssld daemon helper", sizeof(buf));
	parv[0] = buf;
	parv[1] = NULL;

	for(i = 0; i < count; i++)
	{
		ssl_ctl_t *ctl;
		rb_socketpair(AF_UNIX, SOCK_DGRAM, 0, &F1, &F2, "SSL/TLS handle passing socket");
		rb_set_buffers(F1, READBUF_SIZE);
		rb_set_buffers(F2, READBUF_SIZE);
		rb_snprintf(fdarg, sizeof(fdarg), "%d", rb_get_fd(F2));
		setenv("CTL_FD", fdarg, 1);
		rb_pipe(&P1, &P2, "SSL/TLS pipe");
		rb_snprintf(fdarg, sizeof(fdarg), "%d", rb_get_fd(P1));
		setenv("CTL_PIPE", fdarg, 1);
		
		pid = rb_spawn_process(ssld_path, (const char **)parv);
		if(pid == -1)
		{
			ilog(L_MAIN, "Unable to create ssld: %s\n", strerror(errno));
			rb_close(F1);
			rb_close(F2);
			rb_close(P1);
			rb_close(P2);
			return started;
		}
		started++;
		rb_close(F2);
		rb_close(P1);
		ctl = allocate_ssl_daemon(F1, P2, pid);
		if(ssl_ok)
		{
			if(ConfigFileEntry.use_egd && (ConfigFileEntry.egdpool_path != NULL))
				send_init_prng(ctl, RB_PRNG_EGD, ConfigFileEntry.egdpool_path);
			else
				send_init_prng(ctl, RB_PRNG_DEFAULT, NULL);
		}
		if(ssl_ok && ssl_cert != NULL && ssl_private_key != NULL)
			send_new_ssl_certs_one(ctl, ssl_cert, ssl_private_key, ssl_dh_params != NULL ? ssl_dh_params : "");
		ssl_read_ctl(ctl->F, ctl);
		ssl_do_pipe(P2, ctl);
	}
	return started;	
}

static void
ssl_process_zipstats(ssl_ctl_t *ctl, ssl_ctl_buf_t *ctl_buf)
{
	struct Client *server;
	struct ZipStats *zips;
	int parc;
	char *parv[6];
	parc = rb_string_to_array(ctl_buf->buf, parv, 6);		
	server = find_server(NULL, parv[1]);
	if(server == NULL || server->localClient == NULL || !IsCapable(server, CAP_ZIP))
		return;
	if(server->localClient->zipstats == NULL)
		server->localClient->zipstats = rb_malloc(sizeof(struct ZipStats));
	
	zips = server->localClient->zipstats;

	zips->in += strtoull(parv[2], NULL, 10);
	zips->in_wire += strtoull(parv[3], NULL, 10);
	zips->out += strtoull(parv[4], NULL, 10);
	zips->out_wire += strtoull(parv[5], NULL, 10);
	
	if(zips->in > 0)
		zips->in_ratio = ((double)(zips->in - zips->in_wire) / (double) zips->in) * 100.00;
	else
		zips->in_ratio = 0;
		
	if(zips->out > 0)
		zips->out_ratio = ((double)(zips->out - zips->out_wire) / (double) zips->out) * 100.00;
	else
		zips->out_ratio = 0;
}

static void
ssl_process_dead_fd(ssl_ctl_t *ctl, ssl_ctl_buf_t *ctl_buf)
{
	struct Client *client_p;
	char reason[256];
	rb_int32_t fd;

	if(ctl_buf->buflen < 6)
		return; /* bogus message..drop it.. XXX should warn here */
	
	fd = buf_to_int32(&ctl_buf->buf[1]);
	rb_strlcpy(reason, &ctl_buf->buf[5], sizeof(reason));
	client_p = find_cli_fd_hash(fd);
	if(client_p == NULL)
		return;
	if(IsAnyServer(client_p))
		sendto_realops_snomask(SNO_GENERAL, L_ALL, "ssld error for %s: %s", client_p->name, reason);
	exit_client(client_p, client_p, &me, reason);
}


static void
ssl_process_zip_ready(ssl_ctl_t *ctl, ssl_ctl_buf_t *ctl_buf)
{
	struct Client *client_p;
	rb_int32_t fd;

	if(ctl_buf->buflen < 5)
		return; /* bogus message..drop it.. XXX should warn here */
	
	fd = buf_to_int32(&ctl_buf->buf[1]);
	client_p = find_cli_fd_hash(fd);
	if(client_p == NULL)
		return;

	/* Now start sending the data that should be compressed. */
	// ClearCork(client_p);
	send_pop_queue(client_p);
	/* Start reading uncompressed data. */
	read_packet(client_p->localClient->F, client_p);
}


static void
ssl_process_cmd_recv(ssl_ctl_t *ctl)
{
	static const char *cannot_setup_ssl = "ssld cannot setup ssl, check your certificates and private key";
	static const char *no_ssl_or_zlib = "ssld has neither SSL/TLS or zlib support killing all sslds";
	rb_dlink_node *ptr, *next;	
	ssl_ctl_buf_t *ctl_buf;
	if(ctl->dead)
		return;
	RB_DLINK_FOREACH_SAFE(ptr, next, ctl->readq.head)
	{
		ctl_buf = ptr->data;		
		switch(*ctl_buf->buf)
		{
			case 'N':
				ssl_ok = 0; /* ssld says it can't do ssl/tls */
				break;
			case 'D':
				ssl_process_dead_fd(ctl, ctl_buf);
				break;				
			case 'S':
				ssl_process_zipstats(ctl, ctl_buf);
				break;
			case 'I':
				ssl_ok = 0;
				ilog(L_MAIN, cannot_setup_ssl);				
				sendto_realops_snomask(SNO_GENERAL, L_ALL, cannot_setup_ssl);
			case 'U':
				zlib_ok = 0;
				ssl_ok = 0;
				ilog(L_MAIN, no_ssl_or_zlib);
				sendto_realops_snomask(SNO_GENERAL, L_ALL, no_ssl_or_zlib);
				ssl_killall();
				break;
			case 'R':
				ssl_process_zip_ready(ctl, ctl_buf);
				break;
			case 'z':
				zlib_ok = 0;
				break;
			default:
				ilog(L_MAIN, "Received invalid command from ssld: %s", ctl_buf->buf);
				sendto_realops_snomask(SNO_GENERAL, L_ALL, "Received invalid command from ssld");
				break;
		}
		rb_dlinkDelete(ptr, &ctl->readq);
		rb_free(ctl_buf->buf);
		rb_free(ctl_buf);
	}

}


static void
ssl_read_ctl(rb_fde_t *F, void *data)
{
	ssl_ctl_buf_t *ctl_buf;
	ssl_ctl_t *ctl = data;
	int retlen;

	if(ctl->dead)
		return;
	do
	{
		ctl_buf = rb_malloc(sizeof(ssl_ctl_buf_t));
		ctl_buf->buf = rb_malloc(READSIZE);
		retlen = rb_recv_fd_buf(ctl->F, ctl_buf->buf, READSIZE, ctl_buf->F, 4);
		ctl_buf->buflen = retlen;
		if(retlen <= 0) {
			rb_free(ctl_buf->buf);
			rb_free(ctl_buf);
		}
		else
			rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->readq);
	} while(retlen > 0);	
	
	if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
	{
		ssl_dead(ctl);
		return;
	} 
	ssl_process_cmd_recv(ctl);
	rb_setselect(ctl->F, RB_SELECT_READ, ssl_read_ctl, ctl);
}

static ssl_ctl_t *
which_ssld(void)
{
	ssl_ctl_t *ctl, *lowest = NULL;
	rb_dlink_node *ptr;
	
	RB_DLINK_FOREACH(ptr, ssl_daemons.head)
	{
		ctl = ptr->data;
		if(ctl->dead)
			continue;
		if(lowest == NULL) {
			lowest = ctl;
			continue;
		}
		if(ctl->cli_count < lowest->cli_count)
			lowest = ctl;
	}
	return(lowest);
}

static void
ssl_write_ctl(rb_fde_t *F, void *data)
{
	ssl_ctl_t *ctl = data;
	ssl_ctl_buf_t *ctl_buf;
	rb_dlink_node *ptr, *next;
	int retlen, x;

	if(ctl->dead)
		return;

	RB_DLINK_FOREACH_SAFE(ptr, next, ctl->writeq.head)
	{
		ctl_buf = ptr->data;
		/* in theory unix sock_dgram shouldn't ever short write this.. */
		retlen = rb_send_fd_buf(ctl->F, ctl_buf->F, ctl_buf->nfds,  ctl_buf->buf, ctl_buf->buflen);
		if(retlen > 0)
		{
			rb_dlinkDelete(ptr, &ctl->writeq);
			for(x = 0; x < ctl_buf->nfds; x++)
				rb_close(ctl_buf->F[x]);
			rb_free(ctl_buf->buf);
			rb_free(ctl_buf);
			
		}
		if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
		{
			ssl_dead(ctl);
			return;
		} else  {
			rb_setselect(ctl->F, RB_SELECT_WRITE, ssl_write_ctl, ctl);
		}
	}
}

static void
ssl_cmd_write_queue(ssl_ctl_t *ctl, rb_fde_t **F, int count, const void *buf, size_t buflen)
{
	ssl_ctl_buf_t *ctl_buf;
	int x; 

	/* don't bother */
	if(ctl->dead)
		return;
		
	ctl_buf = rb_malloc(sizeof(ssl_ctl_buf_t));
	ctl_buf->buf = rb_malloc(buflen);
	memcpy(ctl_buf->buf, buf, buflen);
	ctl_buf->buflen = buflen;
		
	for(x = 0; x < count && x < MAXPASSFD; x++)
	{
		ctl_buf->F[x] = F[x];	
	}
	ctl_buf->nfds = count;
	rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->writeq);
	ssl_write_ctl(ctl->F, ctl);
}


static void
send_new_ssl_certs_one(ssl_ctl_t *ctl, const char *ssl_cert, const char *ssl_private_key, const char *ssl_dh_params)
{
	size_t len;

	len = strlen(ssl_cert) + strlen(ssl_private_key) + strlen(ssl_dh_params) + 5; 
	if(len > sizeof(tmpbuf))
	{
		sendto_realops_snomask(SNO_GENERAL, L_ALL, 
			"Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...",
			len, sizeof(tmpbuf));
		ilog(L_MAIN, "Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...",
			len, sizeof(tmpbuf));
		return;
	}
	len = rb_snprintf(tmpbuf, sizeof(tmpbuf), "K%c%s%c%s%c%s%c", nul, ssl_cert, nul, ssl_private_key, nul, ssl_dh_params, nul);
	ssl_cmd_write_queue(ctl, NULL, 0, tmpbuf, len);
}

static void
send_init_prng(ssl_ctl_t *ctl, prng_seed_t seedtype, const char *path)
{
	size_t len;
	const char *s;
	rb_uint8_t seed = (rb_uint8_t) seedtype;

	if(path == NULL)
		s = "";
	else
		s = path;

	len = strlen(s) + 3;
	if(len > sizeof(tmpbuf))
	{
		sendto_realops_snomask(SNO_GENERAL, L_ALL, 
			"Parameters for send_init_prng too long (%zd > %zd) to pass to ssld, not sending...",
			len, sizeof(tmpbuf));
		ilog(L_MAIN, "Parameters for send_init_prng too long (%zd > %zd) to pass to ssld, not sending...",
			len, sizeof(tmpbuf));
		return;
	
	}	
	len = rb_snprintf(tmpbuf, sizeof(tmpbuf), "I%c%s%c", seed, s, nul);
	ssl_cmd_write_queue(ctl, NULL, 0, tmpbuf, len);
}

void
send_new_ssl_certs(const char *ssl_cert, const char *ssl_private_key, const char *ssl_dh_params)
{
	rb_dlink_node *ptr;
	if(ssl_cert == NULL || ssl_private_key == NULL || ssl_dh_params == NULL)
	{
		ssl_ok = 0;
		return;
	}
	RB_DLINK_FOREACH(ptr, ssl_daemons.head)
	{
		ssl_ctl_t *ctl = ptr->data;
		send_new_ssl_certs_one(ctl, ssl_cert, ssl_private_key, ssl_dh_params);	
	}
}


ssl_ctl_t * 
start_ssld_accept(rb_fde_t *sslF, rb_fde_t *plainF, rb_int32_t id)
{
	rb_fde_t *F[2];
	ssl_ctl_t *ctl;
	char buf[5];
	F[0] = sslF;
	F[1] = plainF;

	buf[0] = 'A';
	int32_to_buf(&buf[1], id);
	ctl = which_ssld();
	ctl->cli_count++;
	ssl_cmd_write_queue(ctl, F, 2, buf, sizeof(buf));
	return ctl;
}

ssl_ctl_t *
start_ssld_connect(rb_fde_t *sslF, rb_fde_t *plainF, rb_int32_t id)
{
	rb_fde_t *F[2];
	ssl_ctl_t *ctl;
	char buf[5];
	F[0] = sslF;
	F[1] = plainF;

	buf[0] = 'C';
	int32_to_buf(&buf[1], id);

	ctl = which_ssld();
	ctl->cli_count++;
	ssl_cmd_write_queue(ctl, F, 2, buf, sizeof(buf));
	return ctl; 
}

void 
ssld_decrement_clicount(ssl_ctl_t *ctl)
{
	if(ctl == NULL)
		return;

	ctl->cli_count--;
	if(ctl->dead && !ctl->cli_count)
	{
		free_ssl_daemon(ctl);
	}
}

/* 
 * what we end up sending to the ssld process for ziplinks is the following
 * Z[ourfd][level][RECVQ]  
 * Z = ziplinks command	= buf[0]   
 * ourfd = Our end of the socketpair = buf[1..4]
 * level = zip level buf[5]
 * recvqlen = our recvq len = buf[6-7]
 * recvq = any data we read prior to starting ziplinks
 */
void
start_zlib_session(void *data)
{
	struct Client *server = (struct Client *)data;
	rb_uint16_t recvqlen;
	rb_uint8_t level;
	void *xbuf;

	rb_fde_t *F[2];
	rb_fde_t *xF1, *xF2;
	char *buf;
	void *recvq_start;

	size_t hdr = (sizeof(rb_uint8_t) * 2) + sizeof(rb_int32_t);
	size_t len;
	int cpylen, left;

	server->localClient->event = NULL;

	recvqlen = rb_linebuf_len(&server->localClient->buf_recvq);
	
	len = recvqlen + hdr;

	if(len > READBUF_SIZE)
	{
		rb_free(buf);
		sendto_realops_snomask(SNO_GENERAL, L_ALL, "ssld - attempted to pass message of %zd len, max len %d, giving up", len, READBUF_SIZE);
		ilog(L_MAIN, "ssld - attempted to pass message of %zd len, max len %d, giving up", len, READBUF_SIZE);
		exit_client(server, server, server, "ssld readbuf exceeded");
		return;
	}

	buf = rb_malloc(len);	
	level = ConfigFileEntry.compression_level;

	int32_to_buf(&buf[1], rb_get_fd(server->localClient->F));
	buf[5] = (char)level;

	recvq_start = &buf[6];	
	server->localClient->zipstats = rb_malloc(sizeof(struct ZipStats));

	xbuf = recvq_start;
	left = recvqlen;

	do
	{
		cpylen = rb_linebuf_get(&server->localClient->buf_recvq, xbuf, left, LINEBUF_PARTIAL, LINEBUF_RAW);
		left -= cpylen;
		xbuf += cpylen;
	} while(cpylen > 0);

	/* Pass the socket to ssld. */
	*buf = 'Z';
	rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF1, &xF2, "Initial zlib socketpairs");

	F[0] = server->localClient->F; 
	F[1] = xF1;
	del_from_cli_fd_hash(server);	
	server->localClient->F = xF2;
	/* need to redo as what we did before isn't valid now */
	int32_to_buf(&buf[1], rb_get_fd(server->localClient->F));
	add_to_cli_fd_hash(server);
	server->localClient->ssl_ctl = which_ssld();
	server->localClient->ssl_ctl->cli_count++;
	ssl_cmd_write_queue(server->localClient->ssl_ctl, F, 2, buf, len);
	rb_free(buf);
}

static void
collect_zipstats(void *unused)
{
	rb_dlink_node *ptr;
	struct Client *target_p;
	char buf[sizeof(rb_uint8_t) + sizeof(rb_int32_t) + HOSTLEN];
	void *odata;
	size_t len;
	rb_int32_t id;

	buf[0] = 'S';
	odata = buf + sizeof(rb_uint8_t) + sizeof(rb_int32_t);

	RB_DLINK_FOREACH(ptr, serv_list.head)
	{
		target_p = ptr->data;
		if(IsCapable(target_p, CAP_ZIP))
		{
			len = sizeof(rb_uint8_t) + sizeof(rb_uint32_t);

			id = rb_get_fd(target_p->localClient->F);
			int32_to_buf(&buf[1], rb_get_fd(target_p->localClient->F));
			rb_strlcpy(odata, target_p->name, (sizeof(buf)-len));
			len += strlen(odata) + 1; /* Get the \0 as well */
			ssl_cmd_write_queue(target_p->localClient->ssl_ctl, NULL, 0, buf, len); 
		}
	}
}

static void
cleanup_dead_ssl(void *unused)
{
	rb_dlink_node *ptr, *next;
	ssl_ctl_t *ctl;
	RB_DLINK_FOREACH_SAFE(ptr, next, ssl_daemons.head)
	{
		ctl = ptr->data;
		if(ctl->dead && !ctl->cli_count)
		{
			free_ssl_daemon(ctl);			
		}
	}
}

int
get_ssld_count(void)
{
	return ssld_count;
}

void init_ssld(void)
{
	rb_event_addish("collect_zipstats", collect_zipstats, NULL, ZIPSTATS_TIME);
	rb_event_addish("cleanup_dead_ssld", cleanup_dead_ssl, NULL, 1200);
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.