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