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