]> jfr.im git - irc/rqf/shadowircd.git/blame - src/sslproc.c
Do not check floodcount if user is messaging self.
[irc/rqf/shadowircd.git] / src / sslproc.c
CommitLineData
2b6c8773
WP
1/*
2 * sslproc.c: An interface to ssld
3 * Copyright (C) 2007 Aaron Sethman <androsyn@ratbox.org>
4 * Copyright (C) 2007 ircd-ratbox development team
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 * USA
20 *
21 * $Id: sslproc.c 25179 2008-03-30 16:34:57Z androsyn $
22 */
23
24#include <ratbox_lib.h>
25#include "stdinc.h"
26#include "s_conf.h"
27#include "logger.h"
28#include "listener.h"
29#include "sslproc.h"
30#include "s_serv.h"
31#include "ircd.h"
32#include "hash.h"
33#include "client.h"
34#include "send.h"
35#include "packet.h"
36
37#define ZIPSTATS_TIME 60
38
39static void collect_zipstats(void *unused);
40static void ssl_read_ctl(rb_fde_t *F, void *data);
41static int ssld_count;
42
43static char tmpbuf[READBUF_SIZE];
44static char nul = '\0';
45
46#define MAXPASSFD 4
47#define READSIZE 1024
48typedef struct _ssl_ctl_buf
49{
50 rb_dlink_node node;
51 char *buf;
52 size_t buflen;
53 rb_fde_t *F[MAXPASSFD];
54 int nfds;
55} ssl_ctl_buf_t;
56
57
58struct _ssl_ctl
59{
60 rb_dlink_node node;
61 int cli_count;
62 rb_fde_t *F;
63 rb_fde_t *P;
64 pid_t pid;
65 rb_dlink_list readq;
66 rb_dlink_list writeq;
67 rb_uint8_t dead;
68};
69
70static void send_new_ssl_certs_one(ssl_ctl_t *ctl, const char *ssl_cert, const char *ssl_private_key, const char *ssl_dh_params);
71static void send_init_prng(ssl_ctl_t *ctl, prng_seed_t seedtype, const char *path);
72
73
74static rb_dlink_list ssl_daemons;
75
76static inline rb_int32_t buf_to_int32(char *buf)
77{
78 rb_int32_t x;
79 x = *buf << 24;
80 x |= *(++buf) << 16;
81 x |= *(++buf) << 8;
82 x |= *(++buf);
83 return x;
84}
85
86static inline void int32_to_buf(char *buf, rb_int32_t x)
87{
88 *(buf) = x >> 24 & 0xFF;
89 *(++buf) = x >> 16 & 0xFF;
90 *(++buf) = x >> 8 & 0xFF;
91 *(++buf) = x & 0xFF;
92 return;
93}
94
95
96static inline rb_uint16_t buf_to_uint16(char *buf)
97{
98 rb_uint16_t x;
99 x = *(buf) << 8;
100 x |= *(++buf);
101 return x;
102}
103
104static inline void uint16_to_buf(char *buf, rb_uint16_t x)
105{
106 *(buf) = x >> 8 & 0xFF;
107 *(++buf) = x & 0xFF;
108 return;
109}
110
111
112static ssl_ctl_t *
113allocate_ssl_daemon(rb_fde_t *F, rb_fde_t *P, int pid)
114{
115 ssl_ctl_t *ctl;
116
117 if(F == NULL || pid < 0)
118 return NULL;
119 ctl = rb_malloc(sizeof(ssl_ctl_t));
120 ctl->F = F;
121 ctl->P = P;
122 ctl->pid = pid;
123 ssld_count++;
124 rb_dlinkAdd(ctl, &ctl->node, &ssl_daemons);
125 return ctl;
126}
127
128static void
129free_ssl_daemon(ssl_ctl_t *ctl)
130{
131 rb_dlink_node *ptr;
132 ssl_ctl_buf_t *ctl_buf;
133 int x;
134 if(ctl->cli_count)
135 return;
136
137 RB_DLINK_FOREACH(ptr, ctl->readq.head)
138 {
139 ctl_buf = ptr->data;
140 for(x = 0; x < ctl_buf->nfds; x++)
141 rb_close(ctl_buf->F[x]);
142
143 rb_free(ctl_buf->buf);
144 rb_free(ctl_buf);
145 }
146
147 RB_DLINK_FOREACH(ptr, ctl->writeq.head)
148 {
149 ctl_buf = ptr->data;
150 for(x = 0; x < ctl_buf->nfds; x++)
151 rb_close(ctl_buf->F[x]);
152
153 rb_free(ctl_buf->buf);
154 rb_free(ctl_buf);
155 }
156 rb_close(ctl->F);
157 rb_close(ctl->P);
158 rb_dlinkDelete(&ctl->node, &ssl_daemons);
159 rb_free(ctl);
160}
161
162static char *ssld_path;
163
164static int ssld_spin_count = 0;
165static time_t last_spin;
166static int ssld_wait = 0;
167
168
169static void
170ssl_killall(void)
171{
172 rb_dlink_node *ptr, *next;
173 ssl_ctl_t *ctl;
174 RB_DLINK_FOREACH_SAFE(ptr, next, ssl_daemons.head)
175 {
176 ctl = ptr->data;
177 if(ctl->dead)
178 continue;
179 ctl->dead = 1;
180 ssld_count--;
181 kill(ctl->pid, SIGKILL);
182 }
183}
184
185static void
186ssl_dead(ssl_ctl_t *ctl)
187{
188 if(ctl->dead)
189 return;
190
191 ctl->dead = 1;
192 ssld_count--;
193 kill(ctl->pid, SIGKILL); /* make sure the process is really gone */
194 ilog(L_MAIN, "ssld helper died - attempting to restart");
79c3c679 195 sendto_realops_snomask(SNO_GENERAL, L_ALL, "ssld helper died - attempting to restart");
2b6c8773
WP
196 start_ssldaemon(1, ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params);
197}
198
199static void
200ssl_do_pipe(rb_fde_t *F, void *data)
201{
202 int retlen;
203 ssl_ctl_t *ctl = data;
204 retlen = rb_write(F, "0", 1);
205 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
206 {
207 ssl_dead(ctl);
208 return;
209 }
210 rb_setselect(F, RB_SELECT_READ, ssl_do_pipe, data);
211}
212
213static void
214restart_ssld_event(void *unused)
215{
216 ssld_spin_count = 0;
217 last_spin = 0;
218 ssld_wait = 0;
219 if(ServerInfo.ssld_count > get_ssld_count())
220 {
221 int start = ServerInfo.ssld_count - get_ssld_count();
222 ilog(L_MAIN, "Attempting to restart ssld processes");
79c3c679 223 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Attempt to restart ssld processes");
2b6c8773
WP
224 start_ssldaemon(start, ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params);
225 }
226}
227
228int
229start_ssldaemon(int count, const char *ssl_cert, const char *ssl_private_key, const char *ssl_dh_params)
230{
231 rb_fde_t *F1, *F2;
232 rb_fde_t *P1, *P2;
233 char fullpath[PATH_MAX + 1];
234 char fdarg[6];
235 const char *parv[2];
236 char buf[128];
237 pid_t pid;
238 int started = 0, i;
239
240 if(ssld_wait)
241 return 0;
242
243 if(ssld_spin_count > 20 && (rb_current_time() - last_spin < 5))
244 {
245 ilog(L_MAIN, "ssld helper is spinning - will attempt to restart in 5 minutes");
79c3c679 246 sendto_realops_snomask(SNO_GENERAL, L_ALL, "ssld helper is spinning - will attempt to restart in 1 minute");
2b6c8773
WP
247 rb_event_add("restart_ssld_event", restart_ssld_event, NULL, 60);
248 ssld_wait = 1;
249 return 0;
250 }
251
252 ssld_spin_count++;
253 last_spin = rb_current_time();
254 if(ssld_path == NULL)
255 {
256 rb_snprintf(fullpath, sizeof(fullpath), "%s/ssld", BINPATH);
257
258 if(access(fullpath, X_OK) == -1)
259 {
260 rb_snprintf(fullpath, sizeof(fullpath), "%s/bin/ssld", ConfigFileEntry.dpath);
261 if(access(fullpath, X_OK) == -1)
262 {
263 ilog(L_MAIN, "Unable to execute ssld in %s/bin or %s", ConfigFileEntry.dpath, BINPATH);
264 return 0 ;
265 }
266 }
267 ssld_path = rb_strdup(fullpath);
268 }
269
270 rb_strlcpy(buf, "-ircd ssld daemon helper", sizeof(buf));
271 parv[0] = buf;
272 parv[1] = NULL;
273
274 for(i = 0; i < count; i++)
275 {
276 ssl_ctl_t *ctl;
277 rb_socketpair(AF_UNIX, SOCK_DGRAM, 0, &F1, &F2, "SSL/TLS handle passing socket");
278 rb_set_buffers(F1, READBUF_SIZE);
279 rb_set_buffers(F2, READBUF_SIZE);
280 rb_snprintf(fdarg, sizeof(fdarg), "%d", rb_get_fd(F2));
281 setenv("CTL_FD", fdarg, 1);
282 rb_pipe(&P1, &P2, "SSL/TLS pipe");
283 rb_snprintf(fdarg, sizeof(fdarg), "%d", rb_get_fd(P1));
284 setenv("CTL_PIPE", fdarg, 1);
285
286 pid = rb_spawn_process(ssld_path, (const char **)parv);
287 if(pid == -1)
288 {
289 ilog(L_MAIN, "Unable to create ssld: %s\n", strerror(errno));
290 rb_close(F1);
291 rb_close(F2);
292 rb_close(P1);
293 rb_close(P2);
294 return started;
295 }
296 started++;
297 rb_close(F2);
298 rb_close(P1);
299 ctl = allocate_ssl_daemon(F1, P2, pid);
300 if(ssl_ok)
301 {
302 if(ConfigFileEntry.use_egd && (ConfigFileEntry.egdpool_path != NULL))
303 send_init_prng(ctl, RB_PRNG_EGD, ConfigFileEntry.egdpool_path);
304 else
305 send_init_prng(ctl, RB_PRNG_DEFAULT, NULL);
306 }
307 if(ssl_ok && ssl_cert != NULL && ssl_private_key != NULL)
308 send_new_ssl_certs_one(ctl, ssl_cert, ssl_private_key, ssl_dh_params != NULL ? ssl_dh_params : "");
309 ssl_read_ctl(ctl->F, ctl);
310 ssl_do_pipe(P2, ctl);
311 }
312 return started;
313}
314
315static void
316ssl_process_zipstats(ssl_ctl_t *ctl, ssl_ctl_buf_t *ctl_buf)
317{
318 struct Client *server;
319 struct ZipStats *zips;
320 int parc;
321 char *parv[6];
322 parc = rb_string_to_array(ctl_buf->buf, parv, 6);
323 server = find_server(NULL, parv[1]);
324 if(server == NULL || server->localClient == NULL || !IsCapable(server, CAP_ZIP))
325 return;
326 if(server->localClient->zipstats == NULL)
327 server->localClient->zipstats = rb_malloc(sizeof(struct ZipStats));
328
329 zips = server->localClient->zipstats;
330
331 zips->in += strtoull(parv[2], NULL, 10);
332 zips->in_wire += strtoull(parv[3], NULL, 10);
333 zips->out += strtoull(parv[4], NULL, 10);
334 zips->out_wire += strtoull(parv[5], NULL, 10);
335
336 if(zips->in > 0)
337 zips->in_ratio = ((double)(zips->in - zips->in_wire) / (double) zips->in) * 100.00;
338 else
339 zips->in_ratio = 0;
340
341 if(zips->out > 0)
342 zips->out_ratio = ((double)(zips->out - zips->out_wire) / (double) zips->out) * 100.00;
343 else
344 zips->out_ratio = 0;
345}
346
347static void
348ssl_process_dead_fd(ssl_ctl_t *ctl, ssl_ctl_buf_t *ctl_buf)
349{
350 struct Client *client_p;
351 char reason[256];
352 rb_int32_t fd;
353
354 if(ctl_buf->buflen < 6)
355 return; /* bogus message..drop it.. XXX should warn here */
356
357 fd = buf_to_int32(&ctl_buf->buf[1]);
358 rb_strlcpy(reason, &ctl_buf->buf[5], sizeof(reason));
359 client_p = find_cli_fd_hash(fd);
360 if(client_p == NULL)
361 return;
362 if(IsAnyServer(client_p))
e5cdec73 363 {
cd0ba907 364 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) && !IsServer(client_p) ? L_NETWIDE : L_ALL, "ssld error for %s: %s", client_p->name, reason);
e5cdec73
JT
365 ilog(L_SERVER, "ssld error for %s: %s", log_client_name(client_p, SHOW_IP), reason);
366 }
2b6c8773
WP
367 exit_client(client_p, client_p, &me, reason);
368}
369
370
371static void
372ssl_process_zip_ready(ssl_ctl_t *ctl, ssl_ctl_buf_t *ctl_buf)
373{
374 struct Client *client_p;
375 rb_int32_t fd;
376
377 if(ctl_buf->buflen < 5)
378 return; /* bogus message..drop it.. XXX should warn here */
379
380 fd = buf_to_int32(&ctl_buf->buf[1]);
381 client_p = find_cli_fd_hash(fd);
382 if(client_p == NULL)
383 return;
384
385 /* Now start sending the data that should be compressed. */
386 // ClearCork(client_p);
387 send_pop_queue(client_p);
388 /* Start reading uncompressed data. */
389 read_packet(client_p->localClient->F, client_p);
390}
391
392
393static void
394ssl_process_cmd_recv(ssl_ctl_t *ctl)
395{
396 static const char *cannot_setup_ssl = "ssld cannot setup ssl, check your certificates and private key";
397 static const char *no_ssl_or_zlib = "ssld has neither SSL/TLS or zlib support killing all sslds";
398 rb_dlink_node *ptr, *next;
399 ssl_ctl_buf_t *ctl_buf;
400 if(ctl->dead)
401 return;
402 RB_DLINK_FOREACH_SAFE(ptr, next, ctl->readq.head)
403 {
404 ctl_buf = ptr->data;
405 switch(*ctl_buf->buf)
406 {
407 case 'N':
408 ssl_ok = 0; /* ssld says it can't do ssl/tls */
409 break;
410 case 'D':
411 ssl_process_dead_fd(ctl, ctl_buf);
412 break;
413 case 'S':
414 ssl_process_zipstats(ctl, ctl_buf);
415 break;
416 case 'I':
417 ssl_ok = 0;
418 ilog(L_MAIN, cannot_setup_ssl);
79c3c679 419 sendto_realops_snomask(SNO_GENERAL, L_ALL, cannot_setup_ssl);
2b6c8773
WP
420 case 'U':
421 zlib_ok = 0;
422 ssl_ok = 0;
423 ilog(L_MAIN, no_ssl_or_zlib);
79c3c679 424 sendto_realops_snomask(SNO_GENERAL, L_ALL, no_ssl_or_zlib);
2b6c8773
WP
425 ssl_killall();
426 break;
427 case 'R':
428 ssl_process_zip_ready(ctl, ctl_buf);
429 break;
430 case 'z':
431 zlib_ok = 0;
432 break;
433 default:
434 ilog(L_MAIN, "Received invalid command from ssld: %s", ctl_buf->buf);
79c3c679 435 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Received invalid command from ssld");
2b6c8773
WP
436 break;
437 }
438 rb_dlinkDelete(ptr, &ctl->readq);
439 rb_free(ctl_buf->buf);
440 rb_free(ctl_buf);
441 }
442
443}
444
445
446static void
447ssl_read_ctl(rb_fde_t *F, void *data)
448{
449 ssl_ctl_buf_t *ctl_buf;
450 ssl_ctl_t *ctl = data;
451 int retlen;
452
453 if(ctl->dead)
454 return;
455 do
456 {
457 ctl_buf = rb_malloc(sizeof(ssl_ctl_buf_t));
458 ctl_buf->buf = rb_malloc(READSIZE);
459 retlen = rb_recv_fd_buf(ctl->F, ctl_buf->buf, READSIZE, ctl_buf->F, 4);
460 ctl_buf->buflen = retlen;
461 if(retlen <= 0) {
462 rb_free(ctl_buf->buf);
463 rb_free(ctl_buf);
464 }
465 else
466 rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->readq);
467 } while(retlen > 0);
468
469 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
470 {
471 ssl_dead(ctl);
472 return;
473 }
474 ssl_process_cmd_recv(ctl);
475 rb_setselect(ctl->F, RB_SELECT_READ, ssl_read_ctl, ctl);
476}
477
478static ssl_ctl_t *
479which_ssld(void)
480{
481 ssl_ctl_t *ctl, *lowest = NULL;
482 rb_dlink_node *ptr;
483
484 RB_DLINK_FOREACH(ptr, ssl_daemons.head)
485 {
486 ctl = ptr->data;
487 if(ctl->dead)
488 continue;
489 if(lowest == NULL) {
490 lowest = ctl;
491 continue;
492 }
493 if(ctl->cli_count < lowest->cli_count)
494 lowest = ctl;
495 }
496 return(lowest);
497}
498
499static void
500ssl_write_ctl(rb_fde_t *F, void *data)
501{
502 ssl_ctl_t *ctl = data;
503 ssl_ctl_buf_t *ctl_buf;
504 rb_dlink_node *ptr, *next;
505 int retlen, x;
506
507 if(ctl->dead)
508 return;
509
510 RB_DLINK_FOREACH_SAFE(ptr, next, ctl->writeq.head)
511 {
512 ctl_buf = ptr->data;
513 /* in theory unix sock_dgram shouldn't ever short write this.. */
514 retlen = rb_send_fd_buf(ctl->F, ctl_buf->F, ctl_buf->nfds, ctl_buf->buf, ctl_buf->buflen);
515 if(retlen > 0)
516 {
517 rb_dlinkDelete(ptr, &ctl->writeq);
518 for(x = 0; x < ctl_buf->nfds; x++)
519 rb_close(ctl_buf->F[x]);
520 rb_free(ctl_buf->buf);
521 rb_free(ctl_buf);
522
523 }
524 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
525 {
526 ssl_dead(ctl);
527 return;
528 } else {
529 rb_setselect(ctl->F, RB_SELECT_WRITE, ssl_write_ctl, ctl);
530 }
531 }
532}
533
534static void
535ssl_cmd_write_queue(ssl_ctl_t *ctl, rb_fde_t **F, int count, const void *buf, size_t buflen)
536{
537 ssl_ctl_buf_t *ctl_buf;
538 int x;
539
540 /* don't bother */
541 if(ctl->dead)
542 return;
543
544 ctl_buf = rb_malloc(sizeof(ssl_ctl_buf_t));
545 ctl_buf->buf = rb_malloc(buflen);
546 memcpy(ctl_buf->buf, buf, buflen);
547 ctl_buf->buflen = buflen;
548
549 for(x = 0; x < count && x < MAXPASSFD; x++)
550 {
551 ctl_buf->F[x] = F[x];
552 }
553 ctl_buf->nfds = count;
554 rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->writeq);
555 ssl_write_ctl(ctl->F, ctl);
556}
557
558
559static void
560send_new_ssl_certs_one(ssl_ctl_t *ctl, const char *ssl_cert, const char *ssl_private_key, const char *ssl_dh_params)
561{
562 size_t len;
563
564 len = strlen(ssl_cert) + strlen(ssl_private_key) + strlen(ssl_dh_params) + 5;
565 if(len > sizeof(tmpbuf))
566 {
79c3c679 567 sendto_realops_snomask(SNO_GENERAL, L_ALL,
2b6c8773
WP
568 "Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...",
569 len, sizeof(tmpbuf));
570 ilog(L_MAIN, "Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...",
571 len, sizeof(tmpbuf));
572 return;
573 }
574 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);
575 ssl_cmd_write_queue(ctl, NULL, 0, tmpbuf, len);
576}
577
578static void
579send_init_prng(ssl_ctl_t *ctl, prng_seed_t seedtype, const char *path)
580{
581 size_t len;
582 const char *s;
583 rb_uint8_t seed = (rb_uint8_t) seedtype;
584
585 if(path == NULL)
586 s = "";
587 else
588 s = path;
589
590 len = strlen(s) + 3;
591 if(len > sizeof(tmpbuf))
592 {
79c3c679 593 sendto_realops_snomask(SNO_GENERAL, L_ALL,
2b6c8773
WP
594 "Parameters for send_init_prng too long (%zd > %zd) to pass to ssld, not sending...",
595 len, sizeof(tmpbuf));
596 ilog(L_MAIN, "Parameters for send_init_prng too long (%zd > %zd) to pass to ssld, not sending...",
597 len, sizeof(tmpbuf));
598 return;
599
600 }
601 len = rb_snprintf(tmpbuf, sizeof(tmpbuf), "I%c%s%c", seed, s, nul);
602 ssl_cmd_write_queue(ctl, NULL, 0, tmpbuf, len);
603}
604
605void
606send_new_ssl_certs(const char *ssl_cert, const char *ssl_private_key, const char *ssl_dh_params)
607{
608 rb_dlink_node *ptr;
609 if(ssl_cert == NULL || ssl_private_key == NULL || ssl_dh_params == NULL)
610 {
611 ssl_ok = 0;
612 return;
613 }
614 RB_DLINK_FOREACH(ptr, ssl_daemons.head)
615 {
616 ssl_ctl_t *ctl = ptr->data;
617 send_new_ssl_certs_one(ctl, ssl_cert, ssl_private_key, ssl_dh_params);
618 }
619}
620
621
622ssl_ctl_t *
623start_ssld_accept(rb_fde_t *sslF, rb_fde_t *plainF, rb_int32_t id)
624{
625 rb_fde_t *F[2];
626 ssl_ctl_t *ctl;
627 char buf[5];
628 F[0] = sslF;
629 F[1] = plainF;
630
631 buf[0] = 'A';
632 int32_to_buf(&buf[1], id);
633 ctl = which_ssld();
634 ctl->cli_count++;
635 ssl_cmd_write_queue(ctl, F, 2, buf, sizeof(buf));
636 return ctl;
637}
638
639ssl_ctl_t *
640start_ssld_connect(rb_fde_t *sslF, rb_fde_t *plainF, rb_int32_t id)
641{
642 rb_fde_t *F[2];
643 ssl_ctl_t *ctl;
644 char buf[5];
645 F[0] = sslF;
646 F[1] = plainF;
647
648 buf[0] = 'C';
649 int32_to_buf(&buf[1], id);
650
651 ctl = which_ssld();
652 ctl->cli_count++;
653 ssl_cmd_write_queue(ctl, F, 2, buf, sizeof(buf));
654 return ctl;
655}
656
657void
658ssld_decrement_clicount(ssl_ctl_t *ctl)
659{
660 if(ctl == NULL)
661 return;
662
663 ctl->cli_count--;
664 if(ctl->dead && !ctl->cli_count)
665 {
666 free_ssl_daemon(ctl);
667 }
668}
669
670/*
671 * what we end up sending to the ssld process for ziplinks is the following
672 * Z[ourfd][level][RECVQ]
673 * Z = ziplinks command = buf[0]
674 * ourfd = Our end of the socketpair = buf[1..4]
675 * level = zip level buf[5]
676 * recvqlen = our recvq len = buf[6-7]
677 * recvq = any data we read prior to starting ziplinks
678 */
679void
680start_zlib_session(void *data)
681{
682 struct Client *server = (struct Client *)data;
683 rb_uint16_t recvqlen;
684 rb_uint8_t level;
685 void *xbuf;
686
687 rb_fde_t *F[2];
688 rb_fde_t *xF1, *xF2;
689 char *buf;
690 void *recvq_start;
691
692 size_t hdr = (sizeof(rb_uint8_t) * 2) + sizeof(rb_int32_t);
693 size_t len;
694 int cpylen, left;
695
696 server->localClient->event = NULL;
697
698 recvqlen = rb_linebuf_len(&server->localClient->buf_recvq);
699
700 len = recvqlen + hdr;
701
702 if(len > READBUF_SIZE)
703 {
704 rb_free(buf);
79c3c679 705 sendto_realops_snomask(SNO_GENERAL, L_ALL, "ssld - attempted to pass message of %zd len, max len %d, giving up", len, READBUF_SIZE);
2b6c8773
WP
706 ilog(L_MAIN, "ssld - attempted to pass message of %zd len, max len %d, giving up", len, READBUF_SIZE);
707 exit_client(server, server, server, "ssld readbuf exceeded");
708 return;
709 }
710
711 buf = rb_malloc(len);
712 level = ConfigFileEntry.compression_level;
713
714 int32_to_buf(&buf[1], rb_get_fd(server->localClient->F));
715 buf[5] = (char)level;
716
717 recvq_start = &buf[6];
718 server->localClient->zipstats = rb_malloc(sizeof(struct ZipStats));
719
720 xbuf = recvq_start;
721 left = recvqlen;
722
723 do
724 {
725 cpylen = rb_linebuf_get(&server->localClient->buf_recvq, xbuf, left, LINEBUF_PARTIAL, LINEBUF_RAW);
726 left -= cpylen;
727 xbuf += cpylen;
728 } while(cpylen > 0);
729
730 /* Pass the socket to ssld. */
731 *buf = 'Z';
732 rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF1, &xF2, "Initial zlib socketpairs");
733
734 F[0] = server->localClient->F;
735 F[1] = xF1;
736 del_from_cli_fd_hash(server);
737 server->localClient->F = xF2;
738 /* need to redo as what we did before isn't valid now */
739 int32_to_buf(&buf[1], rb_get_fd(server->localClient->F));
740 add_to_cli_fd_hash(server);
741 server->localClient->ssl_ctl = which_ssld();
742 server->localClient->ssl_ctl->cli_count++;
743 ssl_cmd_write_queue(server->localClient->ssl_ctl, F, 2, buf, len);
744 rb_free(buf);
745}
746
747static void
748collect_zipstats(void *unused)
749{
750 rb_dlink_node *ptr;
751 struct Client *target_p;
752 char buf[sizeof(rb_uint8_t) + sizeof(rb_int32_t) + HOSTLEN];
753 void *odata;
754 size_t len;
755 rb_int32_t id;
756
757 buf[0] = 'S';
758 odata = buf + sizeof(rb_uint8_t) + sizeof(rb_int32_t);
759
760 RB_DLINK_FOREACH(ptr, serv_list.head)
761 {
762 target_p = ptr->data;
763 if(IsCapable(target_p, CAP_ZIP))
764 {
765 len = sizeof(rb_uint8_t) + sizeof(rb_uint32_t);
766
767 id = rb_get_fd(target_p->localClient->F);
768 int32_to_buf(&buf[1], rb_get_fd(target_p->localClient->F));
769 rb_strlcpy(odata, target_p->name, (sizeof(buf)-len));
770 len += strlen(odata) + 1; /* Get the \0 as well */
771 ssl_cmd_write_queue(target_p->localClient->ssl_ctl, NULL, 0, buf, len);
772 }
773 }
774}
775
776static void
777cleanup_dead_ssl(void *unused)
778{
779 rb_dlink_node *ptr, *next;
780 ssl_ctl_t *ctl;
781 RB_DLINK_FOREACH_SAFE(ptr, next, ssl_daemons.head)
782 {
783 ctl = ptr->data;
784 if(ctl->dead && !ctl->cli_count)
785 {
786 free_ssl_daemon(ctl);
787 }
788 }
789}
790
791int
792get_ssld_count(void)
793{
794 return ssld_count;
795}
796
797void init_ssld(void)
798{
799 rb_event_addish("collect_zipstats", collect_zipstats, NULL, ZIPSTATS_TIME);
800 rb_event_addish("cleanup_dead_ssld", cleanup_dead_ssl, NULL, 1200);
801}
802