]> jfr.im git - solanum.git/blob - ircd/sslproc.c
Show account name in cliconn snotes when SASL is used (#135)
[solanum.git] / ircd / sslproc.c
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
22 #include <rb_lib.h>
23 #include "stdinc.h"
24
25
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 #include "certfp.h"
37
38 #define ZIPSTATS_TIME 60
39
40 static void collect_zipstats(void *unused);
41 static void ssl_read_ctl(rb_fde_t * F, void *data);
42 static int ssld_count;
43
44 static char tmpbuf[READBUF_SIZE];
45 static char nul = '\0';
46
47 #define MAXPASSFD 4
48 #define READSIZE 1024
49 typedef struct _ssl_ctl_buf
50 {
51 rb_dlink_node node;
52 char *buf;
53 size_t buflen;
54 rb_fde_t *F[MAXPASSFD];
55 int nfds;
56 } ssl_ctl_buf_t;
57
58
59 struct _ssl_ctl
60 {
61 rb_dlink_node node;
62 int cli_count;
63 rb_fde_t *F;
64 rb_fde_t *P;
65 pid_t pid;
66 rb_dlink_list readq;
67 rb_dlink_list writeq;
68 uint8_t shutdown;
69 uint8_t dead;
70 char version[256];
71 };
72
73 static void ssld_update_config_one(ssl_ctl_t *ctl);
74 static void send_new_ssl_certs_one(ssl_ctl_t * ctl);
75 static void send_certfp_method(ssl_ctl_t *ctl);
76
77
78 static rb_dlink_list ssl_daemons;
79
80 static inline uint32_t
81 buf_to_uint32(char *buf)
82 {
83 uint32_t x;
84 memcpy(&x, buf, sizeof(x));
85 return x;
86 }
87
88 static inline void
89 uint32_to_buf(char *buf, uint32_t x)
90 {
91 memcpy(buf, &x, sizeof(x));
92 return;
93 }
94
95 static ssl_ctl_t *
96 allocate_ssl_daemon(rb_fde_t * F, rb_fde_t * P, int pid)
97 {
98 ssl_ctl_t *ctl;
99
100 if(F == NULL || pid < 0)
101 return NULL;
102 ctl = rb_malloc(sizeof(ssl_ctl_t));
103 ctl->F = F;
104 ctl->P = P;
105 ctl->pid = pid;
106 ssld_count++;
107 rb_dlinkAdd(ctl, &ctl->node, &ssl_daemons);
108 return ctl;
109 }
110
111 static void
112 free_ssl_daemon(ssl_ctl_t * ctl)
113 {
114 rb_dlink_node *ptr;
115 ssl_ctl_buf_t *ctl_buf;
116 int x;
117 if(ctl->cli_count)
118 return;
119
120 RB_DLINK_FOREACH(ptr, ctl->readq.head)
121 {
122 ctl_buf = ptr->data;
123 for(x = 0; x < ctl_buf->nfds; x++)
124 rb_close(ctl_buf->F[x]);
125
126 rb_free(ctl_buf->buf);
127 rb_free(ctl_buf);
128 }
129
130 RB_DLINK_FOREACH(ptr, ctl->writeq.head)
131 {
132 ctl_buf = ptr->data;
133 for(x = 0; x < ctl_buf->nfds; x++)
134 rb_close(ctl_buf->F[x]);
135
136 rb_free(ctl_buf->buf);
137 rb_free(ctl_buf);
138 }
139 rb_close(ctl->F);
140 rb_close(ctl->P);
141 rb_dlinkDelete(&ctl->node, &ssl_daemons);
142 rb_free(ctl);
143 }
144
145 static char *ssld_path;
146
147 static int ssld_spin_count = 0;
148 static time_t last_spin;
149 static int ssld_wait = 0;
150
151
152 void
153 restart_ssld(void)
154 {
155 rb_dlink_node *ptr, *next;
156 ssl_ctl_t *ctl;
157
158 RB_DLINK_FOREACH_SAFE(ptr, next, ssl_daemons.head)
159 {
160 ctl = ptr->data;
161 if(ctl->dead)
162 continue;
163 if(ctl->shutdown)
164 continue;
165 ctl->shutdown = 1;
166 ssld_count--;
167 if(!ctl->cli_count)
168 {
169 rb_kill(ctl->pid, SIGKILL);
170 free_ssl_daemon(ctl);
171 }
172 }
173
174 ssld_spin_count = 0;
175 last_spin = 0;
176 ssld_wait = 0;
177 start_ssldaemon(ServerInfo.ssld_count);
178 }
179
180 static void
181 ssl_killall(void)
182 {
183 rb_dlink_node *ptr, *next;
184 ssl_ctl_t *ctl;
185 RB_DLINK_FOREACH_SAFE(ptr, next, ssl_daemons.head)
186 {
187 ctl = ptr->data;
188 if(ctl->dead)
189 continue;
190 ctl->dead = 1;
191 if(!ctl->shutdown)
192 ssld_count--;
193 rb_kill(ctl->pid, SIGKILL);
194 if(!ctl->cli_count)
195 free_ssl_daemon(ctl);
196 }
197 }
198
199 static void
200 ssl_dead(ssl_ctl_t * ctl)
201 {
202 if(ctl->dead)
203 return;
204
205 ctl->dead = 1;
206 rb_kill(ctl->pid, SIGKILL); /* make sure the process is really gone */
207
208 if(!ctl->shutdown)
209 {
210 ssld_count--;
211 ilog(L_MAIN, "ssld helper died - attempting to restart");
212 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "ssld helper died - attempting to restart");
213 start_ssldaemon(1);
214 }
215 }
216
217 static void
218 ssl_do_pipe(rb_fde_t * F, void *data)
219 {
220 int retlen;
221 ssl_ctl_t *ctl = data;
222 retlen = rb_write(F, "0", 1);
223 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
224 {
225 ssl_dead(ctl);
226 return;
227 }
228 rb_setselect(F, RB_SELECT_READ, ssl_do_pipe, data);
229 }
230
231 static void
232 restart_ssld_event(void *unused)
233 {
234 ssld_spin_count = 0;
235 last_spin = 0;
236 ssld_wait = 0;
237 if(ServerInfo.ssld_count > get_ssld_count())
238 {
239 int start = ServerInfo.ssld_count - get_ssld_count();
240 ilog(L_MAIN, "Attempting to restart ssld processes");
241 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "Attempt to restart ssld processes");
242 start_ssldaemon(start);
243 }
244 }
245
246 int
247 start_ssldaemon(int count)
248 {
249 rb_fde_t *F1, *F2;
250 rb_fde_t *P1, *P2;
251 #ifdef _WIN32
252 const char *suffix = ".exe";
253 #else
254 const char *suffix = "";
255 #endif
256
257 char fullpath[PATH_MAX + 1];
258 char fdarg[6];
259 const char *parv[2];
260 char buf[128];
261 char s_pid[10];
262 pid_t pid;
263 int started = 0, i;
264
265 if(ssld_wait)
266 return 0;
267
268 if(ssld_spin_count > 20 && (rb_current_time() - last_spin < 5))
269 {
270 ilog(L_MAIN, "ssld helper is spinning - will attempt to restart in 1 minute");
271 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
272 "ssld helper is spinning - will attempt to restart in 1 minute");
273 rb_event_add("restart_ssld_event", restart_ssld_event, NULL, 60);
274 ssld_wait = 1;
275 return 0;
276 }
277
278 ssld_spin_count++;
279 last_spin = rb_current_time();
280
281 if(ssld_path == NULL)
282 {
283 snprintf(fullpath, sizeof(fullpath), "%s%cssld%s", ircd_paths[IRCD_PATH_LIBEXEC], RB_PATH_SEPARATOR, suffix);
284
285 if(access(fullpath, X_OK) == -1)
286 {
287 snprintf(fullpath, sizeof(fullpath), "%s%cbin%cssld%s",
288 ConfigFileEntry.dpath, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR, suffix);
289 if(access(fullpath, X_OK) == -1)
290 {
291 ilog(L_MAIN,
292 "Unable to execute ssld%s in %s or %s/bin",
293 suffix, ircd_paths[IRCD_PATH_LIBEXEC], ConfigFileEntry.dpath);
294 return 0;
295 }
296 }
297 ssld_path = rb_strdup(fullpath);
298 }
299 rb_strlcpy(buf, "-ircd ssld daemon", sizeof(buf));
300 parv[0] = buf;
301 parv[1] = NULL;
302
303 for(i = 0; i < count; i++)
304 {
305 ssl_ctl_t *ctl;
306 if(rb_socketpair(AF_UNIX, SOCK_DGRAM, 0, &F1, &F2, "SSL/TLS handle passing socket") == -1)
307 {
308 ilog(L_MAIN, "Unable to create ssld - rb_socketpair failed: %s", strerror(errno));
309 return started;
310 }
311
312 rb_set_buffers(F1, READBUF_SIZE);
313 rb_set_buffers(F2, READBUF_SIZE);
314 snprintf(fdarg, sizeof(fdarg), "%d", rb_get_fd(F2));
315 rb_setenv("CTL_FD", fdarg, 1);
316 if(rb_pipe(&P1, &P2, "SSL/TLS pipe") == -1)
317 {
318 ilog(L_MAIN, "Unable to create ssld - rb_pipe failed: %s", strerror(errno));
319 return started;
320 }
321 snprintf(fdarg, sizeof(fdarg), "%d", rb_get_fd(P1));
322 rb_setenv("CTL_PIPE", fdarg, 1);
323 snprintf(s_pid, sizeof(s_pid), "%d", (int)getpid());
324 rb_setenv("CTL_PPID", s_pid, 1);
325
326 rb_clear_cloexec(F2);
327 rb_clear_cloexec(P1);
328
329 pid = rb_spawn_process(ssld_path, (const char **) parv);
330 if(pid == -1)
331 {
332 ilog(L_MAIN, "Unable to create ssld: %s\n", strerror(errno));
333 rb_close(F1);
334 rb_close(F2);
335 rb_close(P1);
336 rb_close(P2);
337 return started;
338 }
339 started++;
340 rb_close(F2);
341 rb_close(P1);
342 ctl = allocate_ssl_daemon(F1, P2, pid);
343 if(ircd_ssl_ok)
344 ssld_update_config_one(ctl);
345 ssl_read_ctl(ctl->F, ctl);
346 ssl_do_pipe(P2, ctl);
347
348 }
349 return started;
350 }
351
352 static void
353 ssl_process_zipstats(ssl_ctl_t * ctl, ssl_ctl_buf_t * ctl_buf)
354 {
355 struct Client *server;
356 struct ZipStats *zips;
357 char *parv[6];
358 int parc = rb_string_to_array(ctl_buf->buf, parv, sizeof(parv));
359
360 if (parc < sizeof(parv))
361 return;
362
363 server = find_server(NULL, parv[1]);
364 if(server == NULL || server->localClient == NULL || !IsCapable(server, CAP_ZIP))
365 return;
366 if(server->localClient->zipstats == NULL)
367 server->localClient->zipstats = rb_malloc(sizeof(struct ZipStats));
368
369 zips = server->localClient->zipstats;
370
371 zips->in += strtoull(parv[2], NULL, 10);
372 zips->in_wire += strtoull(parv[3], NULL, 10);
373 zips->out += strtoull(parv[4], NULL, 10);
374 zips->out_wire += strtoull(parv[5], NULL, 10);
375
376 if(zips->in > 0)
377 zips->in_ratio = ((double) (zips->in - zips->in_wire) / (double) zips->in) * 100.00;
378 else
379 zips->in_ratio = 0;
380
381 if(zips->out > 0)
382 zips->out_ratio = ((double) (zips->out - zips->out_wire) / (double) zips->out) * 100.00;
383 else
384 zips->out_ratio = 0;
385 }
386
387 static void
388 ssl_process_open_fd(ssl_ctl_t * ctl, ssl_ctl_buf_t * ctl_buf)
389 {
390 struct Client *client_p;
391 uint32_t fd;
392
393 if(ctl_buf->buflen < 5)
394 return; /* bogus message..drop it.. XXX should warn here */
395
396 fd = buf_to_uint32(&ctl_buf->buf[1]);
397 client_p = find_cli_connid_hash(fd);
398 if(client_p == NULL || client_p->localClient == NULL)
399 return;
400
401 if(client_p->localClient->ssl_callback)
402 {
403 SSL_OPEN_CB *hdl = client_p->localClient->ssl_callback;
404
405 client_p->localClient->ssl_callback = NULL;
406
407 hdl(client_p, RB_OK);
408 }
409 }
410
411 static void
412 ssl_process_dead_fd(ssl_ctl_t * ctl, ssl_ctl_buf_t * ctl_buf)
413 {
414 struct Client *client_p;
415 char reason[256];
416 uint32_t fd;
417
418 if(ctl_buf->buflen < 6)
419 return; /* bogus message..drop it.. XXX should warn here */
420
421 fd = buf_to_uint32(&ctl_buf->buf[1]);
422 rb_strlcpy(reason, &ctl_buf->buf[5], sizeof(reason));
423 client_p = find_cli_connid_hash(fd);
424 if(client_p == NULL || client_p->localClient == NULL)
425 return;
426
427 if(IsAnyServer(client_p))
428 {
429 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "ssld error for %s: %s", client_p->name, reason);
430 ilog(L_SERVER, "ssld error for %s: %s", log_client_name(client_p, SHOW_IP), reason);
431 }
432
433 /* if there is still a pending callback, call it now */
434 if(client_p->localClient->ssl_callback)
435 {
436 SSL_OPEN_CB *hdl = client_p->localClient->ssl_callback;
437
438 client_p->localClient->ssl_callback = NULL;
439
440 if (hdl(client_p, RB_ERROR_SSL))
441 {
442 /* the callback has exited the client */
443 return;
444 }
445 }
446
447 if(IsAnyServer(client_p) || IsRegistered(client_p))
448 {
449 /* read any last moment ERROR, QUIT or the like -- jilles */
450 if (!strcmp(reason, "Remote host closed the connection"))
451 read_packet(client_p->localClient->F, client_p);
452 if (IsAnyDead(client_p))
453 return;
454 }
455 exit_client(client_p, client_p, &me, reason);
456 }
457
458
459 static void
460 ssl_process_cipher_string(ssl_ctl_t *ctl, ssl_ctl_buf_t *ctl_buf)
461 {
462 struct Client *client_p;
463 const char *cstring;
464 uint32_t fd;
465
466 if(ctl_buf->buflen < 6)
467 return; /* bogus message..drop it.. XXX should warn here */
468
469 fd = buf_to_uint32(&ctl_buf->buf[1]);
470 cstring = (const char *)&ctl_buf->buf[5];
471
472 if(EmptyString(cstring))
473 return;
474
475 client_p = find_cli_connid_hash(fd);
476 if(client_p != NULL && client_p->localClient != NULL)
477 {
478 rb_free(client_p->localClient->cipher_string);
479 client_p->localClient->cipher_string = rb_strdup(cstring);
480 }
481 }
482
483
484 static void
485 ssl_process_certfp(ssl_ctl_t * ctl, ssl_ctl_buf_t * ctl_buf)
486 {
487 struct Client *client_p;
488 uint32_t fd;
489 uint32_t certfp_method;
490 uint32_t len;
491 uint8_t *certfp;
492 char *certfp_string;
493 const char *method_string;
494 int method_len;
495
496 if(ctl_buf->buflen > 13 + RB_SSL_CERTFP_LEN)
497 return; /* bogus message..drop it.. XXX should warn here */
498
499 fd = buf_to_uint32(&ctl_buf->buf[1]);
500 certfp_method = buf_to_uint32(&ctl_buf->buf[5]);
501 len = buf_to_uint32(&ctl_buf->buf[9]);
502 certfp = (uint8_t *)&ctl_buf->buf[13];
503 client_p = find_cli_connid_hash(fd);
504 if(client_p == NULL)
505 return;
506
507 switch (certfp_method) {
508 case RB_SSL_CERTFP_METH_CERT_SHA1:
509 method_string = CERTFP_PREFIX_CERT_SHA1;
510 break;
511 case RB_SSL_CERTFP_METH_CERT_SHA256:
512 method_string = CERTFP_PREFIX_CERT_SHA256;
513 break;
514 case RB_SSL_CERTFP_METH_CERT_SHA512:
515 method_string = CERTFP_PREFIX_CERT_SHA512;
516 break;
517 case RB_SSL_CERTFP_METH_SPKI_SHA256:
518 method_string = CERTFP_PREFIX_SPKI_SHA256;
519 break;
520 case RB_SSL_CERTFP_METH_SPKI_SHA512:
521 method_string = CERTFP_PREFIX_SPKI_SHA512;
522 break;
523 default:
524 return;
525 }
526 method_len = strlen(method_string);
527
528 rb_free(client_p->certfp);
529 certfp_string = rb_malloc(method_len + len * 2 + 1);
530 rb_strlcpy(certfp_string, method_string, method_len + len * 2 + 1);
531 for(uint32_t i = 0; i < len; i++)
532 snprintf(certfp_string + method_len + 2 * i, 3, "%02x",
533 certfp[i]);
534 client_p->certfp = certfp_string;
535 }
536
537 static void
538 ssl_process_cmd_recv(ssl_ctl_t * ctl)
539 {
540 static const char *cannot_setup_ssl = "ssld cannot setup ssl, check your certificates and private key";
541 static const char *no_ssl_or_zlib = "ssld has neither SSL/TLS or zlib support killing all sslds";
542 rb_dlink_node *ptr, *next;
543 ssl_ctl_buf_t *ctl_buf;
544 unsigned long len;
545
546 if(ctl->dead)
547 return;
548
549 RB_DLINK_FOREACH_SAFE(ptr, next, ctl->readq.head)
550 {
551 ctl_buf = ptr->data;
552 switch (*ctl_buf->buf)
553 {
554 case 'N':
555 ircd_ssl_ok = false; /* ssld says it can't do ssl/tls */
556 break;
557 case 'O':
558 ssl_process_open_fd(ctl, ctl_buf);
559 break;
560 case 'D':
561 ssl_process_dead_fd(ctl, ctl_buf);
562 break;
563 case 'C':
564 ssl_process_cipher_string(ctl, ctl_buf);
565 break;
566 case 'F':
567 ssl_process_certfp(ctl, ctl_buf);
568 break;
569 case 'S':
570 ssl_process_zipstats(ctl, ctl_buf);
571 break;
572 case 'I':
573 ircd_ssl_ok = false;
574 ilog(L_MAIN, "%s", cannot_setup_ssl);
575 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s", cannot_setup_ssl);
576 break;
577 case 'U':
578 ircd_zlib_ok = 0;
579 ircd_ssl_ok = false;
580 ilog(L_MAIN, "%s", no_ssl_or_zlib);
581 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s", no_ssl_or_zlib);
582 ssl_killall();
583 return;
584 case 'V':
585 len = ctl_buf->buflen - 1;
586 if (len > sizeof(ctl->version) - 1)
587 len = sizeof(ctl->version) - 1;
588 strncpy(ctl->version, &ctl_buf->buf[1], len);
589 case 'z':
590 ircd_zlib_ok = 0;
591 break;
592 default:
593 ilog(L_MAIN, "Received invalid command from ssld: %s", ctl_buf->buf);
594 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "Received invalid command from ssld");
595 break;
596 }
597 rb_dlinkDelete(ptr, &ctl->readq);
598 rb_free(ctl_buf->buf);
599 rb_free(ctl_buf);
600 }
601
602 }
603
604
605 static void
606 ssl_read_ctl(rb_fde_t * F, void *data)
607 {
608 ssl_ctl_buf_t *ctl_buf;
609 ssl_ctl_t *ctl = data;
610 int retlen;
611
612 if(ctl->dead)
613 return;
614 do
615 {
616 ctl_buf = rb_malloc(sizeof(ssl_ctl_buf_t));
617 ctl_buf->buf = rb_malloc(READSIZE);
618 retlen = rb_recv_fd_buf(ctl->F, ctl_buf->buf, READSIZE, ctl_buf->F, 4);
619 ctl_buf->buflen = retlen;
620 if(retlen <= 0)
621 {
622 rb_free(ctl_buf->buf);
623 rb_free(ctl_buf);
624 }
625 else
626 rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->readq);
627 }
628 while(retlen > 0);
629
630 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
631 {
632 ssl_dead(ctl);
633 return;
634 }
635 ssl_process_cmd_recv(ctl);
636 rb_setselect(ctl->F, RB_SELECT_READ, ssl_read_ctl, ctl);
637 }
638
639 static ssl_ctl_t *
640 which_ssld(void)
641 {
642 ssl_ctl_t *ctl, *lowest = NULL;
643 rb_dlink_node *ptr;
644
645 RB_DLINK_FOREACH(ptr, ssl_daemons.head)
646 {
647 ctl = ptr->data;
648 if(ctl->dead)
649 continue;
650 if(ctl->shutdown)
651 continue;
652 if(lowest == NULL)
653 {
654 lowest = ctl;
655 continue;
656 }
657 if(ctl->cli_count < lowest->cli_count)
658 lowest = ctl;
659 }
660 return (lowest);
661 }
662
663 static void
664 ssl_write_ctl(rb_fde_t * F, void *data)
665 {
666 ssl_ctl_t *ctl = data;
667 ssl_ctl_buf_t *ctl_buf;
668 rb_dlink_node *ptr, *next;
669 int retlen, x;
670
671 if(ctl->dead)
672 return;
673
674 RB_DLINK_FOREACH_SAFE(ptr, next, ctl->writeq.head)
675 {
676 ctl_buf = ptr->data;
677 /* in theory unix sock_dgram shouldn't ever short write this.. */
678 retlen = rb_send_fd_buf(ctl->F, ctl_buf->F, ctl_buf->nfds, ctl_buf->buf, ctl_buf->buflen, ctl->pid);
679 if(retlen > 0)
680 {
681 rb_dlinkDelete(ptr, &ctl->writeq);
682 for(x = 0; x < ctl_buf->nfds; x++)
683 rb_close(ctl_buf->F[x]);
684 rb_free(ctl_buf->buf);
685 rb_free(ctl_buf);
686
687 }
688 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
689 {
690 ssl_dead(ctl);
691 return;
692 }
693 else
694 {
695 rb_setselect(ctl->F, RB_SELECT_WRITE, ssl_write_ctl, ctl);
696 }
697 }
698 }
699
700 static void
701 ssl_cmd_write_queue(ssl_ctl_t * ctl, rb_fde_t ** F, int count, const void *buf, size_t buflen)
702 {
703 ssl_ctl_buf_t *ctl_buf;
704 int x;
705
706 /* don't bother */
707 if(ctl->dead)
708 return;
709
710 ctl_buf = rb_malloc(sizeof(ssl_ctl_buf_t));
711 ctl_buf->buf = rb_malloc(buflen);
712 memcpy(ctl_buf->buf, buf, buflen);
713 ctl_buf->buflen = buflen;
714
715 for(x = 0; x < count && x < MAXPASSFD; x++)
716 {
717 ctl_buf->F[x] = F[x];
718 }
719 ctl_buf->nfds = count;
720 rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->writeq);
721 ssl_write_ctl(ctl->F, ctl);
722 }
723
724
725 static void
726 send_new_ssl_certs_one(ssl_ctl_t * ctl)
727 {
728 size_t len = 5;
729
730 if(ServerInfo.ssl_cert)
731 len += strlen(ServerInfo.ssl_cert);
732 else
733 return;
734
735 if(ServerInfo.ssl_private_key)
736 len += strlen(ServerInfo.ssl_private_key);
737
738 if(ServerInfo.ssl_dh_params)
739 len += strlen(ServerInfo.ssl_dh_params);
740
741 if(ServerInfo.ssl_cipher_list)
742 len += strlen(ServerInfo.ssl_cipher_list);
743
744 if(len > sizeof(tmpbuf))
745 {
746 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
747 "Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...",
748 len, sizeof(tmpbuf));
749 ilog(L_MAIN,
750 "Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...",
751 len, sizeof(tmpbuf));
752 return;
753 }
754
755 int ret = snprintf(tmpbuf, sizeof(tmpbuf), "K%c%s%c%s%c%s%c%s%c", nul,
756 ServerInfo.ssl_cert, nul,
757 ServerInfo.ssl_private_key != NULL ? ServerInfo.ssl_private_key : "", nul,
758 ServerInfo.ssl_dh_params != NULL ? ServerInfo.ssl_dh_params : "", nul,
759 ServerInfo.ssl_cipher_list != NULL ? ServerInfo.ssl_cipher_list : "", nul);
760
761 if(ret > 5)
762 ssl_cmd_write_queue(ctl, NULL, 0, tmpbuf, (size_t) ret);
763 }
764
765 static void
766 send_certfp_method(ssl_ctl_t *ctl)
767 {
768 char buf[5];
769
770 buf[0] = 'F';
771 uint32_to_buf(&buf[1], ConfigFileEntry.certfp_method);
772 ssl_cmd_write_queue(ctl, NULL, 0, buf, sizeof(buf));
773 }
774
775 static void
776 ssld_update_config_one(ssl_ctl_t *ctl)
777 {
778 send_certfp_method(ctl);
779 send_new_ssl_certs_one(ctl);
780 }
781
782 void
783 ssld_update_config(void)
784 {
785 rb_dlink_node *ptr;
786
787 RB_DLINK_FOREACH(ptr, ssl_daemons.head)
788 {
789 ssl_ctl_t *ctl = ptr->data;
790
791 if (ctl->dead || ctl->shutdown)
792 continue;
793
794 ssld_update_config_one(ctl);
795 }
796 }
797
798 ssl_ctl_t *
799 start_ssld_accept(rb_fde_t * sslF, rb_fde_t * plainF, uint32_t id)
800 {
801 rb_fde_t *F[2];
802 ssl_ctl_t *ctl;
803 char buf[5];
804 F[0] = sslF;
805 F[1] = plainF;
806
807 buf[0] = 'A';
808 uint32_to_buf(&buf[1], id);
809 ctl = which_ssld();
810 if(!ctl)
811 return NULL;
812 ctl->cli_count++;
813 ssl_cmd_write_queue(ctl, F, 2, buf, sizeof(buf));
814 return ctl;
815 }
816
817 ssl_ctl_t *
818 start_ssld_connect(rb_fde_t * sslF, rb_fde_t * plainF, uint32_t id)
819 {
820 rb_fde_t *F[2];
821 ssl_ctl_t *ctl;
822 char buf[5];
823 F[0] = sslF;
824 F[1] = plainF;
825
826 buf[0] = 'C';
827 uint32_to_buf(&buf[1], id);
828
829 ctl = which_ssld();
830 if(!ctl)
831 return NULL;
832 ctl->cli_count++;
833 ssl_cmd_write_queue(ctl, F, 2, buf, sizeof(buf));
834 return ctl;
835 }
836
837 void
838 ssld_decrement_clicount(ssl_ctl_t * ctl)
839 {
840 if(ctl == NULL)
841 return;
842
843 ctl->cli_count--;
844 if(ctl->shutdown && !ctl->cli_count)
845 {
846 ctl->dead = 1;
847 rb_kill(ctl->pid, SIGKILL);
848 }
849 if(ctl->dead && !ctl->cli_count)
850 {
851 free_ssl_daemon(ctl);
852 }
853 }
854
855 /*
856 * what we end up sending to the ssld process for ziplinks is the following
857 * Z[ourfd][level][RECVQ]
858 * Z = ziplinks command = buf[0]
859 * ourfd = Our end of the socketpair = buf[1..4]
860 * level = zip level buf[5]
861 * recvqlen = our recvq len = buf[6-7]
862 * recvq = any data we read prior to starting ziplinks
863 */
864 void
865 start_zlib_session(void *data)
866 {
867 struct Client *server = (struct Client *) data;
868 uint16_t recvqlen;
869 uint8_t level;
870 void *xbuf;
871
872 rb_fde_t *F[2];
873 rb_fde_t *xF1, *xF2;
874 char *buf;
875 void *recvq_start;
876
877 size_t hdr = (sizeof(uint8_t) * 2) + sizeof(uint32_t);
878 size_t len;
879 int cpylen, left;
880
881 server->localClient->event = NULL;
882
883 recvqlen = rb_linebuf_len(&server->localClient->buf_recvq);
884
885 len = recvqlen + hdr;
886
887 if(len > READBUF_SIZE)
888 {
889 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
890 "ssld - attempted to pass message of %zd len, max len %d, giving up",
891 len, READBUF_SIZE);
892 ilog(L_MAIN, "ssld - attempted to pass message of %zd len, max len %d, giving up", len, READBUF_SIZE);
893 exit_client(server, server, server, "ssld readbuf exceeded");
894 return;
895 }
896
897 buf = rb_malloc(len);
898 level = ConfigFileEntry.compression_level;
899
900 uint32_to_buf(&buf[1], rb_get_fd(server->localClient->F));
901 buf[5] = (char) level;
902
903 recvq_start = &buf[6];
904 server->localClient->zipstats = rb_malloc(sizeof(struct ZipStats));
905
906 xbuf = recvq_start;
907 left = recvqlen;
908
909 do
910 {
911 cpylen = rb_linebuf_get(&server->localClient->buf_recvq, xbuf, left, LINEBUF_PARTIAL, LINEBUF_RAW);
912 left -= cpylen;
913 xbuf = (void *) (((uintptr_t) xbuf) + cpylen);
914 }
915 while(cpylen > 0);
916
917 /* Pass the socket to ssld. */
918 *buf = 'Z';
919 if(rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF1, &xF2, "Initial zlib socketpairs") == -1)
920 {
921 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "Error creating zlib socketpair - %s", strerror(errno));
922 ilog(L_MAIN, "Error creating zlib socketpairs - %s", strerror(errno));
923 exit_client(server, server, server, "Error creating zlib socketpair");
924 rb_free(buf);
925 return;
926 }
927
928 F[0] = server->localClient->F;
929 F[1] = xF1;
930 server->localClient->F = xF2;
931 /* need to redo as what we did before isn't valid now */
932 uint32_to_buf(&buf[1], connid_get(server));
933
934 server->localClient->z_ctl = which_ssld();
935 if(!server->localClient->z_ctl)
936 {
937 exit_client(server, server, server, "Error finding available ssld");
938 rb_free(buf);
939 return;
940 }
941 server->localClient->z_ctl->cli_count++;
942 ssl_cmd_write_queue(server->localClient->z_ctl, F, 2, buf, len);
943 rb_free(buf);
944 }
945
946 static void
947 collect_zipstats(void *unused)
948 {
949 rb_dlink_node *ptr;
950 struct Client *target_p;
951 char buf[sizeof(uint8_t) + sizeof(uint32_t) + HOSTLEN];
952 void *odata;
953 size_t len;
954 uint32_t id;
955
956 buf[0] = 'S';
957 odata = buf + sizeof(uint8_t) + sizeof(uint32_t);
958
959 RB_DLINK_FOREACH(ptr, serv_list.head)
960 {
961 target_p = ptr->data;
962 if(IsCapable(target_p, CAP_ZIP))
963 {
964 len = sizeof(uint8_t) + sizeof(uint32_t);
965
966 id = rb_get_fd(target_p->localClient->F);
967 uint32_to_buf(&buf[1], id);
968 rb_strlcpy(odata, target_p->name, (sizeof(buf) - len));
969 len += strlen(odata) + 1; /* Get the \0 as well */
970 ssl_cmd_write_queue(target_p->localClient->z_ctl, NULL, 0, buf, len);
971 }
972 }
973 }
974
975 static void
976 cleanup_dead_ssl(void *unused)
977 {
978 rb_dlink_node *ptr, *next;
979 ssl_ctl_t *ctl;
980 RB_DLINK_FOREACH_SAFE(ptr, next, ssl_daemons.head)
981 {
982 ctl = ptr->data;
983 if(ctl->dead && !ctl->cli_count)
984 {
985 free_ssl_daemon(ctl);
986 }
987 }
988 }
989
990 int
991 get_ssld_count(void)
992 {
993 return ssld_count;
994 }
995
996 void
997 ssld_foreach_info(void (*func)(void *data, pid_t pid, int cli_count, enum ssld_status status, const char *version), void *data)
998 {
999 rb_dlink_node *ptr, *next;
1000 ssl_ctl_t *ctl;
1001 RB_DLINK_FOREACH_SAFE(ptr, next, ssl_daemons.head)
1002 {
1003 ctl = ptr->data;
1004 func(data, ctl->pid, ctl->cli_count,
1005 ctl->dead ? SSLD_DEAD :
1006 (ctl->shutdown ? SSLD_SHUTDOWN : SSLD_ACTIVE),
1007 ctl->version);
1008 }
1009 }
1010
1011 void
1012 init_ssld(void)
1013 {
1014 rb_event_addish("collect_zipstats", collect_zipstats, NULL, ZIPSTATS_TIME);
1015 rb_event_addish("cleanup_dead_ssld", cleanup_dead_ssl, NULL, 60);
1016 }