]> jfr.im git - solanum.git/blame - ssld/ssld.c
crypt: fix strict-aliasing warnings by using an explicit alias.
[solanum.git] / ssld / ssld.c
CommitLineData
8d99443b
VY
1/*
2 * ssld.c: The ircd-ratbox ssl/zlib helper daemon thingy
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
8d99443b
VY
20 */
21
22
23#include "stdinc.h"
24
25#ifdef HAVE_LIBZ
26#include <zlib.h>
27#endif
28
29#define MAXPASSFD 4
30#ifndef READBUF_SIZE
31#define READBUF_SIZE 16384
32#endif
33
34static void setup_signals(void);
3202e249 35static pid_t ppid;
8d99443b 36
408a29c6
AC
37static inline uint32_t
38buf_to_uint32(uint8_t *buf)
8d99443b 39{
408a29c6 40 uint32_t x;
4b6a4d47 41 memcpy(&x, buf, sizeof(x));
8d99443b
VY
42 return x;
43}
44
3202e249 45static inline void
408a29c6 46uint32_to_buf(uint8_t *buf, uint32_t x)
8d99443b 47{
4b6a4d47 48 memcpy(buf, &x, sizeof(x));
8d99443b
VY
49 return;
50}
51
8d99443b
VY
52typedef struct _mod_ctl_buf
53{
54 rb_dlink_node node;
85e9bf41 55 uint8_t *buf;
8d99443b
VY
56 size_t buflen;
57 rb_fde_t *F[MAXPASSFD];
58 int nfds;
59} mod_ctl_buf_t;
60
61typedef struct _mod_ctl
62{
63 rb_dlink_node node;
64 int cli_count;
65 rb_fde_t *F;
66 rb_fde_t *F_pipe;
67 rb_dlink_list readq;
68 rb_dlink_list writeq;
69} mod_ctl_t;
70
71static mod_ctl_t *mod_ctl;
72
73
74#ifdef HAVE_LIBZ
75typedef struct _zlib_stream
76{
77 z_stream instream;
78 z_stream outstream;
79} zlib_stream_t;
80#endif
81
82typedef struct _conn
83{
84 rb_dlink_node node;
85 mod_ctl_t *ctl;
86 rawbuf_head_t *modbuf_out;
87 rawbuf_head_t *plainbuf_out;
88
408a29c6 89 uint32_t id;
8d99443b
VY
90
91 rb_fde_t *mod_fd;
92 rb_fde_t *plain_fd;
94356462
AC
93 uint64_t mod_out;
94 uint64_t mod_in;
95 uint64_t plain_in;
96 uint64_t plain_out;
7edb4f16 97 uint8_t flags;
8d99443b
VY
98 void *stream;
99} conn_t;
100
101#define FLAG_SSL 0x01
102#define FLAG_ZIP 0x02
103#define FLAG_CORK 0x04
104#define FLAG_DEAD 0x08
3202e249
VY
105#define FLAG_SSL_W_WANTS_R 0x10 /* output needs to wait until input possible */
106#define FLAG_SSL_R_WANTS_W 0x20 /* input needs to wait until output possible */
07c2bb75 107#define FLAG_ZIPSSL 0x40
8d99443b
VY
108
109#define IsSSL(x) ((x)->flags & FLAG_SSL)
110#define IsZip(x) ((x)->flags & FLAG_ZIP)
111#define IsCork(x) ((x)->flags & FLAG_CORK)
112#define IsDead(x) ((x)->flags & FLAG_DEAD)
73d6283c
VY
113#define IsSSLWWantsR(x) ((x)->flags & FLAG_SSL_W_WANTS_R)
114#define IsSSLRWantsW(x) ((x)->flags & FLAG_SSL_R_WANTS_W)
07c2bb75 115#define IsZipSSL(x) ((x)->flags & FLAG_ZIPSSL)
8d99443b
VY
116
117#define SetSSL(x) ((x)->flags |= FLAG_SSL)
118#define SetZip(x) ((x)->flags |= FLAG_ZIP)
119#define SetCork(x) ((x)->flags |= FLAG_CORK)
120#define SetDead(x) ((x)->flags |= FLAG_DEAD)
73d6283c
VY
121#define SetSSLWWantsR(x) ((x)->flags |= FLAG_SSL_W_WANTS_R)
122#define SetSSLRWantsW(x) ((x)->flags |= FLAG_SSL_R_WANTS_W)
07c2bb75 123#define SetZipSSL(x) ((x)->flags |= FLAG_ZIPSSL)
8d99443b
VY
124
125#define ClearSSL(x) ((x)->flags &= ~FLAG_SSL)
126#define ClearZip(x) ((x)->flags &= ~FLAG_ZIP)
127#define ClearCork(x) ((x)->flags &= ~FLAG_CORK)
128#define ClearDead(x) ((x)->flags &= ~FLAG_DEAD)
73d6283c
VY
129#define ClearSSLWWantsR(x) ((x)->flags &= ~FLAG_SSL_W_WANTS_R)
130#define ClearSSLRWantsW(x) ((x)->flags &= ~FLAG_SSL_R_WANTS_W)
07c2bb75 131#define ClearZipSSL(x) ((x)->flags &= ~FLAG_ZIPSSL)
8d99443b
VY
132
133#define NO_WAIT 0x0
134#define WAIT_PLAIN 0x1
135
4b6a4d47
VY
136#define HASH_WALK_SAFE(i, max, ptr, next, table) for(i = 0; i < max; i++) { RB_DLINK_FOREACH_SAFE(ptr, next, table[i].head)
137#define HASH_WALK_END }
8d99443b
VY
138#define CONN_HASH_SIZE 2000
139#define connid_hash(x) (&connid_hash_table[(x % CONN_HASH_SIZE)])
140
4b6a4d47
VY
141
142
8d99443b
VY
143static rb_dlink_list connid_hash_table[CONN_HASH_SIZE];
144static rb_dlink_list dead_list;
145
3202e249 146static void conn_mod_read_cb(rb_fde_t *fd, void *data);
8d99443b
VY
147static void conn_mod_write_sendq(rb_fde_t *, void *data);
148static void conn_plain_write_sendq(rb_fde_t *, void *data);
149static void mod_write_ctl(rb_fde_t *, void *data);
3202e249 150static void conn_plain_read_cb(rb_fde_t *fd, void *data);
e99f6122 151static void conn_plain_read_shutdown_cb(rb_fde_t *fd, void *data);
3202e249 152static void mod_cmd_write_queue(mod_ctl_t * ctl, const void *data, size_t len);
8d99443b 153static const char *remote_closed = "Remote host closed the connection";
503727d1 154static bool ssl_ok;
e6bbb410 155static int certfp_method = RB_SSL_CERTFP_METH_SHA1;
8d99443b 156#ifdef HAVE_LIBZ
503727d1 157static bool zlib_ok = true;
8d99443b 158#else
503727d1 159static bool zlib_ok = false;
8d99443b 160#endif
4b6a4d47
VY
161
162
163#ifdef HAVE_LIBZ
8d99443b
VY
164static void *
165ssld_alloc(void *unused, size_t count, size_t size)
166{
167 return rb_malloc(count * size);
168}
169
170static void
171ssld_free(void *unused, void *ptr)
172{
3202e249 173 rb_free(ptr);
8d99443b 174}
4b6a4d47 175#endif
8d99443b
VY
176
177static conn_t *
408a29c6 178conn_find_by_id(uint32_t id)
8d99443b
VY
179{
180 rb_dlink_node *ptr;
181 conn_t *conn;
182
183 RB_DLINK_FOREACH(ptr, (connid_hash(id))->head)
184 {
185 conn = ptr->data;
186 if(conn->id == id && !IsDead(conn))
187 return conn;
188 }
189 return NULL;
190}
191
192static void
408a29c6 193conn_add_id_hash(conn_t * conn, uint32_t id)
8d99443b
VY
194{
195 conn->id = id;
196 rb_dlinkAdd(conn, &conn->node, connid_hash(id));
197}
198
199static void
200free_conn(conn_t * conn)
201{
202 rb_free_rawbuffer(conn->modbuf_out);
203 rb_free_rawbuffer(conn->plainbuf_out);
4b6a4d47 204#ifdef HAVE_LIBZ
8d99443b
VY
205 if(IsZip(conn))
206 {
207 zlib_stream_t *stream = conn->stream;
208 inflateEnd(&stream->instream);
3202e249 209 deflateEnd(&stream->outstream);
84b49742 210 rb_free(stream);
8d99443b 211 }
4b6a4d47 212#endif
8d99443b
VY
213 rb_free(conn);
214}
215
216static void
217clean_dead_conns(void *unused)
218{
219 conn_t *conn;
220 rb_dlink_node *ptr, *next;
221 RB_DLINK_FOREACH_SAFE(ptr, next, dead_list.head)
222 {
223 conn = ptr->data;
224 free_conn(conn);
225 }
226 dead_list.tail = dead_list.head = NULL;
227}
228
229
230static void
231close_conn(conn_t * conn, int wait_plain, const char *fmt, ...)
232{
233 va_list ap;
3202e249 234 char reason[128]; /* must always be under 250 bytes */
85e9bf41 235 uint8_t buf[256];
8d99443b
VY
236 int len;
237 if(IsDead(conn))
238 return;
3202e249 239
8d99443b
VY
240 rb_rawbuf_flush(conn->modbuf_out, conn->mod_fd);
241 rb_rawbuf_flush(conn->plainbuf_out, conn->plain_fd);
242 rb_close(conn->mod_fd);
243 SetDead(conn);
244
408a29c6 245 if(!IsZipSSL(conn))
e99f6122
JT
246 rb_dlinkDelete(&conn->node, connid_hash(conn->id));
247
8d99443b
VY
248 if(!wait_plain || fmt == NULL)
249 {
250 rb_close(conn->plain_fd);
8d99443b
VY
251 rb_dlinkAdd(conn, &conn->node, &dead_list);
252 return;
253 }
e99f6122
JT
254 rb_setselect(conn->plain_fd, RB_SELECT_READ, conn_plain_read_shutdown_cb, conn);
255 rb_setselect(conn->plain_fd, RB_SELECT_WRITE, NULL, NULL);
8d99443b 256 va_start(ap, fmt);
5203cba5 257 vsnprintf(reason, sizeof(reason), fmt, ap);
8d99443b
VY
258 va_end(ap);
259
260 buf[0] = 'D';
408a29c6 261 uint32_to_buf(&buf[1], conn->id);
85e9bf41 262 rb_strlcpy((char *) &buf[5], reason, sizeof(buf) - 5);
8d99443b
VY
263 len = (strlen(reason) + 1) + 5;
264 mod_cmd_write_queue(conn->ctl, buf, len);
265}
266
267static conn_t *
3202e249 268make_conn(mod_ctl_t * ctl, rb_fde_t *mod_fd, rb_fde_t *plain_fd)
8d99443b
VY
269{
270 conn_t *conn = rb_malloc(sizeof(conn_t));
271 conn->ctl = ctl;
272 conn->modbuf_out = rb_new_rawbuffer();
273 conn->plainbuf_out = rb_new_rawbuffer();
274 conn->mod_fd = mod_fd;
275 conn->plain_fd = plain_fd;
276 conn->id = -1;
277 conn->stream = NULL;
278 rb_set_nb(mod_fd);
279 rb_set_nb(plain_fd);
280 return conn;
281}
282
4b6a4d47
VY
283static void
284check_handshake_flood(void *unused)
285{
286 conn_t *conn;
287 rb_dlink_node *ptr, *next;
288 unsigned int count;
289 int i;
290 HASH_WALK_SAFE(i, CONN_HASH_SIZE, ptr, next, connid_hash_table)
291 {
292 conn = ptr->data;
293 if(!IsSSL(conn))
294 continue;
3202e249 295
4b6a4d47
VY
296 count = rb_ssl_handshake_count(conn->mod_fd);
297 /* nothing needs to do this more than twice in ten seconds i don't think */
298 if(count > 2)
299 close_conn(conn, WAIT_PLAIN, "Handshake flooding");
300 else
301 rb_ssl_clear_handshake_count(conn->mod_fd);
3202e249
VY
302 }
303HASH_WALK_END}
4b6a4d47 304
8d99443b 305static void
3202e249 306conn_mod_write_sendq(rb_fde_t *fd, void *data)
8d99443b
VY
307{
308 conn_t *conn = data;
309 const char *err;
310 int retlen;
311 if(IsDead(conn))
312 return;
313
73d6283c
VY
314 if(IsSSLWWantsR(conn))
315 {
316 ClearSSLWWantsR(conn);
317 conn_mod_read_cb(conn->mod_fd, conn);
318 if(IsDead(conn))
319 return;
320 }
321
3202e249 322 while((retlen = rb_rawbuf_flush(conn->modbuf_out, fd)) > 0)
8d99443b
VY
323 conn->mod_out += retlen;
324
325 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
326 {
327 if(retlen == 0)
328 close_conn(conn, WAIT_PLAIN, "%s", remote_closed);
329 if(IsSSL(conn) && retlen == RB_RW_SSL_ERROR)
330 err = rb_get_ssl_strerror(conn->mod_fd);
331 else
332 err = strerror(errno);
333 close_conn(conn, WAIT_PLAIN, "Write error: %s", err);
334 return;
335 }
336 if(rb_rawbuf_length(conn->modbuf_out) > 0)
337 {
73d6283c
VY
338 if(retlen != RB_RW_SSL_NEED_READ)
339 rb_setselect(conn->mod_fd, RB_SELECT_WRITE, conn_mod_write_sendq, conn);
340 else
341 {
342 rb_setselect(conn->mod_fd, RB_SELECT_READ, conn_mod_write_sendq, conn);
343 rb_setselect(conn->mod_fd, RB_SELECT_WRITE, NULL, NULL);
344 SetSSLWWantsR(conn);
345 }
8d99443b
VY
346 }
347 else
348 rb_setselect(conn->mod_fd, RB_SELECT_WRITE, NULL, NULL);
349
350 if(IsCork(conn) && rb_rawbuf_length(conn->modbuf_out) == 0)
351 {
352 ClearCork(conn);
353 conn_plain_read_cb(conn->plain_fd, conn);
354 }
355
356}
357
358static void
359conn_mod_write(conn_t * conn, void *data, size_t len)
360{
361 if(IsDead(conn)) /* no point in queueing to a dead man */
362 return;
363 rb_rawbuf_append(conn->modbuf_out, data, len);
364}
365
366static void
367conn_plain_write(conn_t * conn, void *data, size_t len)
368{
369 if(IsDead(conn)) /* again no point in queueing to dead men */
370 return;
371 rb_rawbuf_append(conn->plainbuf_out, data, len);
372}
373
374static void
375mod_cmd_write_queue(mod_ctl_t * ctl, const void *data, size_t len)
376{
377 mod_ctl_buf_t *ctl_buf;
378 ctl_buf = rb_malloc(sizeof(mod_ctl_buf_t));
379 ctl_buf->buf = rb_malloc(len);
380 ctl_buf->buflen = len;
381 memcpy(ctl_buf->buf, data, len);
382 ctl_buf->nfds = 0;
383 rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->writeq);
384 mod_write_ctl(ctl->F, ctl);
385}
386
387#ifdef HAVE_LIBZ
388static void
389common_zlib_deflate(conn_t * conn, void *buf, size_t len)
390{
6cd1aca7 391 char outbuf[READBUF_SIZE];
8d99443b 392 int ret, have;
3202e249 393 z_stream *outstream = &((zlib_stream_t *) conn->stream)->outstream;
8d99443b
VY
394 outstream->next_in = buf;
395 outstream->avail_in = len;
396 outstream->next_out = (Bytef *) outbuf;
397 outstream->avail_out = sizeof(outbuf);
398
399 ret = deflate(outstream, Z_SYNC_FLUSH);
400 if(ret != Z_OK)
401 {
402 /* deflate error */
403 close_conn(conn, WAIT_PLAIN, "Deflate failed: %s", zError(ret));
404 return;
405 }
406 if(outstream->avail_out == 0)
407 {
408 /* avail_out empty */
409 close_conn(conn, WAIT_PLAIN, "error compressing data, avail_out == 0");
410 return;
411 }
412 if(outstream->avail_in != 0)
413 {
3202e249 414 /* avail_in isn't empty... */
8d99443b
VY
415 close_conn(conn, WAIT_PLAIN, "error compressing data, avail_in != 0");
416 return;
417 }
418 have = sizeof(outbuf) - outstream->avail_out;
419 conn_mod_write(conn, outbuf, have);
420}
421
422static void
423common_zlib_inflate(conn_t * conn, void *buf, size_t len)
424{
6cd1aca7 425 char outbuf[READBUF_SIZE];
3202e249
VY
426 int ret, have = 0;
427 ((zlib_stream_t *) conn->stream)->instream.next_in = buf;
428 ((zlib_stream_t *) conn->stream)->instream.avail_in = len;
429 ((zlib_stream_t *) conn->stream)->instream.next_out = (Bytef *) outbuf;
430 ((zlib_stream_t *) conn->stream)->instream.avail_out = sizeof(outbuf);
8d99443b 431
3202e249 432 while(((zlib_stream_t *) conn->stream)->instream.avail_in)
8d99443b 433 {
3202e249 434 ret = inflate(&((zlib_stream_t *) conn->stream)->instream, Z_NO_FLUSH);
8d99443b
VY
435 if(ret != Z_OK)
436 {
437 if(!strncmp("ERROR ", buf, 6))
438 {
439 close_conn(conn, WAIT_PLAIN, "Received uncompressed ERROR");
440 return;
441 }
442 close_conn(conn, WAIT_PLAIN, "Inflate failed: %s", zError(ret));
443 return;
444 }
3202e249 445 have = sizeof(outbuf) - ((zlib_stream_t *) conn->stream)->instream.avail_out;
8d99443b 446
3202e249 447 if(((zlib_stream_t *) conn->stream)->instream.avail_in)
8d99443b
VY
448 {
449 conn_plain_write(conn, outbuf, have);
450 have = 0;
3202e249
VY
451 ((zlib_stream_t *) conn->stream)->instream.next_out = (Bytef *) outbuf;
452 ((zlib_stream_t *) conn->stream)->instream.avail_out = sizeof(outbuf);
8d99443b
VY
453 }
454 }
455 if(have == 0)
456 return;
457
458 conn_plain_write(conn, outbuf, have);
459}
460#endif
461
462static int
463plain_check_cork(conn_t * conn)
464{
465 if(rb_rawbuf_length(conn->modbuf_out) >= 4096)
466 {
55abcbb2 467 /* if we have over 4k pending outbound, don't read until
8d99443b
VY
468 * we've cleared the queue */
469 SetCork(conn);
470 rb_setselect(conn->plain_fd, RB_SELECT_READ, NULL, NULL);
471 /* try to write */
472 conn_mod_write_sendq(conn->mod_fd, conn);
473 return 1;
474 }
475 return 0;
476}
477
478
479static void
3202e249 480conn_plain_read_cb(rb_fde_t *fd, void *data)
8d99443b 481{
6cd1aca7 482 char inbuf[READBUF_SIZE];
8d99443b
VY
483 conn_t *conn = data;
484 int length = 0;
485 if(conn == NULL)
486 return;
487
488 if(IsDead(conn))
489 return;
490
491 if(plain_check_cork(conn))
492 return;
493
3202e249 494 while(1)
8d99443b
VY
495 {
496 if(IsDead(conn))
497 return;
498
499 length = rb_read(conn->plain_fd, inbuf, sizeof(inbuf));
500
501 if(length == 0 || (length < 0 && !rb_ignore_errno(errno)))
502 {
503 close_conn(conn, NO_WAIT, NULL);
504 return;
505 }
506
507 if(length < 0)
508 {
509 rb_setselect(conn->plain_fd, RB_SELECT_READ, conn_plain_read_cb, conn);
510 conn_mod_write_sendq(conn->mod_fd, conn);
511 return;
512 }
513 conn->plain_in += length;
514
515#ifdef HAVE_LIBZ
516 if(IsZip(conn))
517 common_zlib_deflate(conn, inbuf, length);
518 else
519#endif
520 conn_mod_write(conn, inbuf, length);
521 if(IsDead(conn))
522 return;
523 if(plain_check_cork(conn))
524 return;
525 }
526}
527
e99f6122
JT
528static void
529conn_plain_read_shutdown_cb(rb_fde_t *fd, void *data)
530{
6cd1aca7 531 char inbuf[READBUF_SIZE];
e99f6122
JT
532 conn_t *conn = data;
533 int length = 0;
534
535 if(conn == NULL)
536 return;
537
538 while(1)
539 {
540 length = rb_read(conn->plain_fd, inbuf, sizeof(inbuf));
541
542 if(length == 0 || (length < 0 && !rb_ignore_errno(errno)))
543 {
544 rb_close(conn->plain_fd);
545 rb_dlinkAdd(conn, &conn->node, &dead_list);
546 return;
547 }
548
549 if(length < 0)
550 {
551 rb_setselect(conn->plain_fd, RB_SELECT_READ, conn_plain_read_shutdown_cb, conn);
552 return;
553 }
554 }
555}
556
8d99443b 557static void
3202e249 558conn_mod_read_cb(rb_fde_t *fd, void *data)
8d99443b 559{
6cd1aca7 560 char inbuf[READBUF_SIZE];
8d99443b
VY
561 conn_t *conn = data;
562 const char *err = remote_closed;
563 int length;
564 if(conn == NULL)
565 return;
566 if(IsDead(conn))
567 return;
568
73d6283c
VY
569 if(IsSSLRWantsW(conn))
570 {
571 ClearSSLRWantsW(conn);
572 conn_mod_write_sendq(conn->mod_fd, conn);
573 if(IsDead(conn))
574 return;
575 }
576
3202e249 577 while(1)
8d99443b
VY
578 {
579 if(IsDead(conn))
580 return;
581
582 length = rb_read(conn->mod_fd, inbuf, sizeof(inbuf));
583
584 if(length == 0 || (length < 0 && !rb_ignore_errno(errno)))
585 {
3202e249
VY
586 if(length == 0)
587 {
8d99443b
VY
588 close_conn(conn, WAIT_PLAIN, "%s", remote_closed);
589 return;
590 }
591
592 if(IsSSL(conn) && length == RB_RW_SSL_ERROR)
593 err = rb_get_ssl_strerror(conn->mod_fd);
594 else
595 err = strerror(errno);
596 close_conn(conn, WAIT_PLAIN, "Read error: %s", err);
597 return;
598 }
599 if(length < 0)
600 {
73d6283c
VY
601 if(length != RB_RW_SSL_NEED_WRITE)
602 rb_setselect(conn->mod_fd, RB_SELECT_READ, conn_mod_read_cb, conn);
603 else
604 {
605 rb_setselect(conn->mod_fd, RB_SELECT_READ, NULL, NULL);
606 rb_setselect(conn->mod_fd, RB_SELECT_WRITE, conn_mod_read_cb, conn);
607 SetSSLRWantsW(conn);
608 }
8d99443b
VY
609 conn_plain_write_sendq(conn->plain_fd, conn);
610 return;
3202e249 611 }
8d99443b
VY
612 conn->mod_in += length;
613#ifdef HAVE_LIBZ
614 if(IsZip(conn))
615 common_zlib_inflate(conn, inbuf, length);
616 else
617#endif
618 conn_plain_write(conn, inbuf, length);
619 }
620}
621
622static void
3202e249 623conn_plain_write_sendq(rb_fde_t *fd, void *data)
8d99443b
VY
624{
625 conn_t *conn = data;
626 int retlen;
627
628 if(IsDead(conn))
629 return;
630
3202e249 631 while((retlen = rb_rawbuf_flush(conn->plainbuf_out, fd)) > 0)
8d99443b
VY
632 {
633 conn->plain_out += retlen;
634 }
635 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
636 {
637 close_conn(data, NO_WAIT, NULL);
638 return;
639 }
3202e249 640
8d99443b
VY
641
642 if(rb_rawbuf_length(conn->plainbuf_out) > 0)
643 rb_setselect(conn->plain_fd, RB_SELECT_WRITE, conn_plain_write_sendq, conn);
644 else
645 rb_setselect(conn->plain_fd, RB_SELECT_WRITE, NULL, NULL);
646}
647
648static int
649maxconn(void)
650{
651#if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
652 struct rlimit limit;
653
654 if(!getrlimit(RLIMIT_NOFILE, &limit))
655 {
656 return limit.rlim_cur;
657 }
658#endif /* RLIMIT_FD_MAX */
659 return MAXCONNECTIONS;
660}
661
408a29c6
AC
662static void
663ssl_send_cipher(conn_t *conn)
664{
408a29c6 665 size_t len;
74ff144d 666 uint8_t buf[512];
408a29c6
AC
667 char cstring[256];
668 const char *p;
669 if(!IsSSL(conn))
670 return;
671
672 p = rb_ssl_get_cipher(conn->mod_fd);
673
674 if(p == NULL)
675 return;
676
677 rb_strlcpy(cstring, p, sizeof(cstring));
678
679 buf[0] = 'C';
680 uint32_to_buf(&buf[1], conn->id);
74ff144d 681 strcpy((char *) &buf[5], cstring);
408a29c6
AC
682 len = (strlen(cstring) + 1) + 5;
683 mod_cmd_write_queue(conn->ctl, buf, len);
408a29c6
AC
684}
685
686static void
687ssl_send_certfp(conn_t *conn)
688{
689 uint8_t buf[9 + RB_SSL_CERTFP_LEN];
690
691 int len = rb_get_ssl_certfp(conn->mod_fd, &buf[9], certfp_method);
692 if (!len)
693 return;
694
695 lrb_assert(len <= RB_SSL_CERTFP_LEN);
696 buf[0] = 'F';
697 uint32_to_buf(&buf[1], conn->id);
698 uint32_to_buf(&buf[5], len);
699 mod_cmd_write_queue(conn->ctl, buf, 9 + len);
700}
701
8d99443b 702static void
3202e249 703ssl_process_accept_cb(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t len, void *data)
8d99443b
VY
704{
705 conn_t *conn = data;
7247337a 706
8d99443b
VY
707 if(status == RB_OK)
708 {
709 conn_mod_read_cb(conn->mod_fd, conn);
710 conn_plain_read_cb(conn->plain_fd, conn);
408a29c6
AC
711 ssl_send_cipher(conn);
712 ssl_send_certfp(conn);
8d99443b
VY
713 return;
714 }
a444bb78 715 /* ircd doesn't care about the reason for this */
8d99443b
VY
716 close_conn(conn, NO_WAIT, 0);
717 return;
718}
719
720static void
3202e249 721ssl_process_connect_cb(rb_fde_t *F, int status, void *data)
8d99443b
VY
722{
723 conn_t *conn = data;
a7675ed2 724
8d99443b
VY
725 if(status == RB_OK)
726 {
727 conn_mod_read_cb(conn->mod_fd, conn);
728 conn_plain_read_cb(conn->plain_fd, conn);
408a29c6
AC
729 ssl_send_cipher(conn);
730 ssl_send_certfp(conn);
8d99443b 731 }
a444bb78
JT
732 else if(status == RB_ERR_TIMEOUT)
733 close_conn(conn, WAIT_PLAIN, "SSL handshake timed out");
734 else if(status == RB_ERROR_SSL)
735 close_conn(conn, WAIT_PLAIN, "%s", rb_get_ssl_strerror(conn->mod_fd));
736 else
737 close_conn(conn, WAIT_PLAIN, "SSL handshake failed");
8d99443b
VY
738}
739
740
c03677e9
JT
741static void
742cleanup_bad_message(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb)
743{
744 int i;
745
746 /* XXX should log this somehow */
747 for (i = 0; i < ctlb->nfds; i++)
748 rb_close(ctlb->F[i]);
749}
750
8d99443b
VY
751static void
752ssl_process_accept(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb)
753{
754 conn_t *conn;
408a29c6 755 uint32_t id;
8d99443b
VY
756
757 conn = make_conn(ctl, ctlb->F[0], ctlb->F[1]);
758
408a29c6
AC
759 id = buf_to_uint32(&ctlb->buf[1]);
760 conn_add_id_hash(conn, id);
8d99443b
VY
761 SetSSL(conn);
762
763 if(rb_get_type(conn->mod_fd) & RB_FD_UNKNOWN)
8d99443b 764 rb_set_type(conn->mod_fd, RB_FD_SOCKET);
a5ddb7df
AC
765
766 if(rb_get_type(conn->plain_fd) == RB_FD_UNKNOWN)
8d99443b
VY
767 rb_set_type(conn->plain_fd, RB_FD_SOCKET);
768
769 rb_ssl_start_accepted(ctlb->F[0], ssl_process_accept_cb, conn, 10);
770}
771
e6bbb410
EM
772static void
773ssl_change_certfp_method(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb)
774{
408a29c6 775 certfp_method = buf_to_uint32(&ctlb->buf[1]);
e6bbb410
EM
776}
777
8d99443b
VY
778static void
779ssl_process_connect(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb)
780{
781 conn_t *conn;
408a29c6 782 uint32_t id;
8d99443b
VY
783 conn = make_conn(ctl, ctlb->F[0], ctlb->F[1]);
784
408a29c6
AC
785 id = buf_to_uint32(&ctlb->buf[1]);
786 conn_add_id_hash(conn, id);
8d99443b
VY
787 SetSSL(conn);
788
789 if(rb_get_type(conn->mod_fd) == RB_FD_UNKNOWN)
790 rb_set_type(conn->mod_fd, RB_FD_SOCKET);
791
a5ddb7df 792 if(rb_get_type(conn->plain_fd) == RB_FD_UNKNOWN)
8d99443b
VY
793 rb_set_type(conn->plain_fd, RB_FD_SOCKET);
794
795
796 rb_ssl_start_connected(ctlb->F[0], ssl_process_connect_cb, conn, 10);
797}
798
799static void
800process_stats(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb)
801{
802 char outstat[512];
803 conn_t *conn;
85e9bf41 804 uint8_t *odata;
408a29c6 805 uint32_t id;
8d99443b 806
408a29c6 807 id = buf_to_uint32(&ctlb->buf[1]);
3202e249 808
8d99443b
VY
809 odata = &ctlb->buf[5];
810 conn = conn_find_by_id(id);
811
812 if(conn == NULL)
813 return;
814
5203cba5 815 snprintf(outstat, sizeof(outstat), "S %s %llu %llu %llu %llu", odata,
26b83fa0
SA
816 (unsigned long long)conn->plain_out,
817 (unsigned long long)conn->mod_in,
818 (unsigned long long)conn->plain_in,
819 (unsigned long long)conn->mod_out);
8d99443b
VY
820 conn->plain_out = 0;
821 conn->plain_in = 0;
822 conn->mod_in = 0;
823 conn->mod_out = 0;
824 mod_cmd_write_queue(ctl, outstat, strlen(outstat) + 1); /* +1 is so we send the \0 as well */
825}
826
07c2bb75
JT
827static void
828change_connid(mod_ctl_t *ctl, mod_ctl_buf_t *ctlb)
829{
408a29c6
AC
830 uint32_t id = buf_to_uint32(&ctlb->buf[1]);
831 uint32_t newid = buf_to_uint32(&ctlb->buf[5]);
07c2bb75 832 conn_t *conn = conn_find_by_id(id);
7beaee52
SA
833 lrb_assert(conn != NULL);
834 if(conn == NULL)
835 {
c84003ae 836 uint8_t buf[256];
7beaee52
SA
837 int len;
838
839 buf[0] = 'D';
840 uint32_to_buf(&buf[1], newid);
c84003ae
AC
841 sprintf((char *) &buf[5], "connid %d does not exist", id);
842 len = (strlen((char *) &buf[5]) + 1) + 5;
7beaee52
SA
843 mod_cmd_write_queue(ctl, buf, len);
844
845 return;
846 }
408a29c6 847 rb_dlinkDelete(&conn->node, connid_hash(conn->id));
07c2bb75
JT
848 SetZipSSL(conn);
849 conn->id = newid;
850}
851
8d99443b 852#ifdef HAVE_LIBZ
8d99443b
VY
853static void
854zlib_process(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb)
855{
7edb4f16 856 uint8_t level;
8d99443b 857 size_t recvqlen;
408a29c6 858 size_t hdr = (sizeof(uint8_t) * 2) + sizeof(uint32_t);
8d99443b
VY
859 void *recvq_start;
860 z_stream *instream, *outstream;
861 conn_t *conn;
408a29c6 862 uint32_t id;
8d99443b
VY
863
864 conn = make_conn(ctl, ctlb->F[0], ctlb->F[1]);
865 if(rb_get_type(conn->mod_fd) == RB_FD_UNKNOWN)
866 rb_set_type(conn->mod_fd, RB_FD_SOCKET);
867
868 if(rb_get_type(conn->plain_fd) == RB_FD_UNKNOWN)
869 rb_set_type(conn->plain_fd, RB_FD_SOCKET);
870
408a29c6 871 id = buf_to_uint32(&ctlb->buf[1]);
8d99443b
VY
872 conn_add_id_hash(conn, id);
873
3202e249 874 level = (uint8_t)ctlb->buf[5];
8d99443b
VY
875
876 recvqlen = ctlb->buflen - hdr;
877 recvq_start = &ctlb->buf[6];
878
879 SetZip(conn);
880 conn->stream = rb_malloc(sizeof(zlib_stream_t));
3202e249
VY
881 instream = &((zlib_stream_t *) conn->stream)->instream;
882 outstream = &((zlib_stream_t *) conn->stream)->outstream;
883
8d99443b
VY
884 instream->total_in = 0;
885 instream->total_out = 0;
886 instream->zalloc = (alloc_func) ssld_alloc;
887 instream->zfree = (free_func) ssld_free;
888 instream->data_type = Z_ASCII;
3202e249 889 inflateInit(&((zlib_stream_t *) conn->stream)->instream);
8d99443b
VY
890
891 outstream->total_in = 0;
892 outstream->total_out = 0;
893 outstream->zalloc = (alloc_func) ssld_alloc;
894 outstream->zfree = (free_func) ssld_free;
895 outstream->data_type = Z_ASCII;
896
897 if(level > 9)
408a29c6 898 level = (uint8_t) Z_DEFAULT_COMPRESSION;
8d99443b 899
3202e249 900 deflateInit(&((zlib_stream_t *) conn->stream)->outstream, level);
8d99443b
VY
901 if(recvqlen > 0)
902 common_zlib_inflate(conn, recvq_start, recvqlen);
0bd120ed 903
8d99443b
VY
904 conn_mod_read_cb(conn->mod_fd, conn);
905 conn_plain_read_cb(conn->plain_fd, conn);
906 return;
907
908}
909#endif
910
911static void
912init_prng(mod_ctl_t * ctl, mod_ctl_buf_t * ctl_buf)
913{
914 char *path;
915 prng_seed_t seed_type;
3202e249
VY
916
917 seed_type = (prng_seed_t) ctl_buf->buf[1];
85e9bf41 918 path = (char *) &ctl_buf->buf[2];
8d99443b
VY
919 rb_init_prng(path, seed_type);
920}
921
922
923static void
924ssl_new_keys(mod_ctl_t * ctl, mod_ctl_buf_t * ctl_buf)
925{
926 char *buf;
c1725bda 927 char *cert, *key, *dhparam, *cipher_list;
8d99443b 928
85e9bf41 929 buf = (char *) &ctl_buf->buf[2];
8d99443b
VY
930 cert = buf;
931 buf += strlen(cert) + 1;
932 key = buf;
933 buf += strlen(key) + 1;
934 dhparam = buf;
935 if(strlen(dhparam) == 0)
936 dhparam = NULL;
c1725bda
AC
937 buf += strlen(dhparam) + 1;
938 cipher_list = buf;
939 if(strlen(cipher_list) == 0)
940 cipher_list = NULL;
8d99443b 941
c1725bda 942 if(!rb_setup_ssl_server(cert, key, dhparam, cipher_list))
8d99443b
VY
943 {
944 const char *invalid = "I";
945 mod_cmd_write_queue(ctl, invalid, strlen(invalid));
946 return;
3202e249 947 }
8d99443b
VY
948}
949
950static void
3202e249 951send_nossl_support(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb)
8d99443b
VY
952{
953 static const char *nossl_cmd = "N";
954 conn_t *conn;
408a29c6 955 uint32_t id;
8d99443b
VY
956
957 if(ctlb != NULL)
3202e249 958 {
8d99443b 959 conn = make_conn(ctl, ctlb->F[0], ctlb->F[1]);
408a29c6
AC
960 id = buf_to_uint32(&ctlb->buf[1]);
961 conn_add_id_hash(conn, id);
8d99443b 962 close_conn(conn, WAIT_PLAIN, "libratbox reports no SSL/TLS support");
3202e249
VY
963 }
964 mod_cmd_write_queue(ctl, nossl_cmd, strlen(nossl_cmd));
8d99443b
VY
965}
966
967static void
3202e249 968send_i_am_useless(mod_ctl_t * ctl)
8d99443b
VY
969{
970 static const char *useless = "U";
971 mod_cmd_write_queue(ctl, useless, strlen(useless));
972}
973
e9ffc3c1
SA
974static void
975send_version(mod_ctl_t * ctl)
976{
977 char version[256] = { 'V', 0 };
978 strncpy(&version[1], rb_lib_version(), sizeof(version) - 2);
979 mod_cmd_write_queue(ctl, version, strlen(version));
980}
981
8d99443b 982static void
3202e249 983send_nozlib_support(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb)
8d99443b
VY
984{
985 static const char *nozlib_cmd = "z";
986 conn_t *conn;
408a29c6 987 uint32_t id;
8d99443b
VY
988 if(ctlb != NULL)
989 {
990 conn = make_conn(ctl, ctlb->F[0], ctlb->F[1]);
408a29c6
AC
991 id = buf_to_uint32(&ctlb->buf[1]);
992 conn_add_id_hash(conn, id);
8d99443b 993 close_conn(conn, WAIT_PLAIN, "libratbox reports no zlib support");
3202e249 994 }
8d99443b
VY
995 mod_cmd_write_queue(ctl, nozlib_cmd, strlen(nozlib_cmd));
996}
997
998static void
999mod_process_cmd_recv(mod_ctl_t * ctl)
1000{
1001 rb_dlink_node *ptr, *next;
1002 mod_ctl_buf_t *ctl_buf;
1003
1004 RB_DLINK_FOREACH_SAFE(ptr, next, ctl->readq.head)
1005 {
1006 ctl_buf = ptr->data;
1007
1008 switch (*ctl_buf->buf)
1009 {
1010 case 'A':
1011 {
c03677e9
JT
1012 if (ctl_buf->nfds != 2 || ctl_buf->buflen != 5)
1013 {
1014 cleanup_bad_message(ctl, ctl_buf);
1015 break;
1016 }
1017
8d99443b
VY
1018 if(!ssl_ok)
1019 {
1020 send_nossl_support(ctl, ctl_buf);
1021 break;
1022 }
1023 ssl_process_accept(ctl, ctl_buf);
1024 break;
1025 }
1026 case 'C':
1027 {
c03677e9
JT
1028 if (ctl_buf->nfds != 2 || ctl_buf->buflen != 5)
1029 {
1030 cleanup_bad_message(ctl, ctl_buf);
1031 break;
1032 }
1033
8d99443b
VY
1034 if(!ssl_ok)
1035 {
1036 send_nossl_support(ctl, ctl_buf);
1037 break;
1038 }
1039 ssl_process_connect(ctl, ctl_buf);
1040 break;
1041 }
e6bbb410
EM
1042 case 'F':
1043 {
1044 if (ctl_buf->nfds != 2 || ctl_buf->buflen != 5)
1045 {
1046 cleanup_bad_message(ctl, ctl_buf);
1047 break;
1048 }
1049 ssl_change_certfp_method(ctl, ctl_buf);
1050 break;
1051 }
8d99443b
VY
1052 case 'K':
1053 {
1054 if(!ssl_ok)
1055 {
1056 send_nossl_support(ctl, ctl_buf);
1057 break;
1058 }
1059 ssl_new_keys(ctl, ctl_buf);
1060 break;
1061 }
1062 case 'I':
3202e249
VY
1063 init_prng(ctl, ctl_buf);
1064 break;
8d99443b
VY
1065 case 'S':
1066 {
1067 process_stats(ctl, ctl_buf);
1068 break;
1069 }
07c2bb75
JT
1070 case 'Y':
1071 {
1072 change_connid(ctl, ctl_buf);
1073 break;
1074 }
1075
8d99443b
VY
1076#ifdef HAVE_LIBZ
1077 case 'Z':
1078 {
c03677e9
JT
1079 if (ctl_buf->nfds != 2 || ctl_buf->buflen < 6)
1080 {
1081 cleanup_bad_message(ctl, ctl_buf);
1082 break;
1083 }
1084
8d99443b
VY
1085 /* just zlib only */
1086 zlib_process(ctl, ctl_buf);
1087 break;
1088 }
1089#else
55abcbb2 1090
8d99443b 1091 case 'Z':
21192997 1092 send_nozlib_support(ctl, ctl_buf);
8d99443b 1093 break;
3202e249 1094
8d99443b
VY
1095#endif
1096 default:
1097 break;
1098 /* Log unknown commands */
1099 }
1100 rb_dlinkDelete(ptr, &ctl->readq);
1101 rb_free(ctl_buf->buf);
1102 rb_free(ctl_buf);
1103 }
1104
1105}
1106
1107
1108
1109static void
3202e249 1110mod_read_ctl(rb_fde_t *F, void *data)
8d99443b
VY
1111{
1112 mod_ctl_buf_t *ctl_buf;
1113 mod_ctl_t *ctl = data;
1114 int retlen;
c03677e9 1115 int i;
8d99443b
VY
1116
1117 do
1118 {
1119 ctl_buf = rb_malloc(sizeof(mod_ctl_buf_t));
1120 ctl_buf->buf = rb_malloc(READBUF_SIZE);
1121 ctl_buf->buflen = READBUF_SIZE;
1122 retlen = rb_recv_fd_buf(ctl->F, ctl_buf->buf, ctl_buf->buflen, ctl_buf->F,
1123 MAXPASSFD);
1124 if(retlen <= 0)
1125 {
1126 rb_free(ctl_buf->buf);
1127 rb_free(ctl_buf);
1128 }
1129 else
1130 {
1131 ctl_buf->buflen = retlen;
1132 rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->readq);
c03677e9
JT
1133 for (i = 0; i < MAXPASSFD && ctl_buf->F[i] != NULL; i++)
1134 ;
1135 ctl_buf->nfds = i;
8d99443b
VY
1136 }
1137 }
3202e249 1138 while(retlen > 0);
8d99443b
VY
1139
1140 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
1141 exit(0);
1142
1143 mod_process_cmd_recv(ctl);
1144 rb_setselect(ctl->F, RB_SELECT_READ, mod_read_ctl, ctl);
1145}
1146
1147static void
3202e249 1148mod_write_ctl(rb_fde_t *F, void *data)
8d99443b
VY
1149{
1150 mod_ctl_t *ctl = data;
1151 mod_ctl_buf_t *ctl_buf;
1152 rb_dlink_node *ptr, *next;
1153 int retlen, x;
1154
1155 RB_DLINK_FOREACH_SAFE(ptr, next, ctl->writeq.head)
1156 {
1157 ctl_buf = ptr->data;
1158 retlen = rb_send_fd_buf(ctl->F, ctl_buf->F, ctl_buf->nfds, ctl_buf->buf,
3202e249 1159 ctl_buf->buflen, ppid);
8d99443b
VY
1160 if(retlen > 0)
1161 {
1162 rb_dlinkDelete(ptr, &ctl->writeq);
3202e249 1163 for(x = 0; x < ctl_buf->nfds; x++)
8d99443b
VY
1164 rb_close(ctl_buf->F[x]);
1165 rb_free(ctl_buf->buf);
1166 rb_free(ctl_buf);
1167
1168 }
1169 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
3202e249
VY
1170 exit(0);
1171
8d99443b 1172 }
464b7606
JT
1173 if(rb_dlink_list_length(&ctl->writeq) > 0)
1174 rb_setselect(ctl->F, RB_SELECT_WRITE, mod_write_ctl, ctl);
8d99443b
VY
1175}
1176
1177
1178static void
3202e249 1179read_pipe_ctl(rb_fde_t *F, void *data)
8d99443b 1180{
6cd1aca7 1181 char inbuf[READBUF_SIZE];
8d99443b 1182 int retlen;
3202e249 1183 while((retlen = rb_read(F, inbuf, sizeof(inbuf))) > 0)
8d99443b
VY
1184 {
1185 ;; /* we don't do anything with the pipe really, just care if the other process dies.. */
1186 }
1187 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
1188 exit(0);
1189 rb_setselect(F, RB_SELECT_READ, read_pipe_ctl, NULL);
1190
1191}
1192
1193int
1194main(int argc, char **argv)
1195{
3202e249 1196 const char *s_ctlfd, *s_pipe, *s_pid;
8d99443b
VY
1197 int ctlfd, pipefd, x, maxfd;
1198 maxfd = maxconn();
3202e249 1199
8d99443b
VY
1200 s_ctlfd = getenv("CTL_FD");
1201 s_pipe = getenv("CTL_PIPE");
3202e249 1202 s_pid = getenv("CTL_PPID");
8d99443b 1203
3202e249 1204 if(s_ctlfd == NULL || s_pipe == NULL || s_pid == NULL)
8d99443b 1205 {
3202e249 1206 fprintf(stderr,
4dbb75ee 1207 "This is the charybdis ssld for internal ircd use.\n");
3202e249 1208 fprintf(stderr,
4dbb75ee 1209 "You aren't supposed to run me directly. Exiting.\n");
8d99443b
VY
1210 exit(1);
1211 }
1212
1213 ctlfd = atoi(s_ctlfd);
1214 pipefd = atoi(s_pipe);
3202e249
VY
1215 ppid = atoi(s_pid);
1216 x = 0;
1217#ifndef _WIN32
1218 for(x = 0; x < maxfd; x++)
8d99443b
VY
1219 {
1220 if(x != ctlfd && x != pipefd && x > 2)
1221 close(x);
1222 }
8d99443b 1223 x = open("/dev/null", O_RDWR);
464b7606 1224
8d99443b
VY
1225 if(x >= 0)
1226 {
1227 if(ctlfd != 0 && pipefd != 0)
1228 dup2(x, 0);
1229 if(ctlfd != 1 && pipefd != 1)
1230 dup2(x, 1);
1231 if(ctlfd != 2 && pipefd != 2)
1232 dup2(x, 2);
1233 if(x > 2)
1234 close(x);
1235 }
3202e249 1236#endif
8d99443b
VY
1237 setup_signals();
1238 rb_lib_init(NULL, NULL, NULL, 0, maxfd, 1024, 4096);
1239 rb_init_rawbuffers(1024);
3202e249 1240 ssl_ok = rb_supports_ssl();
8d99443b
VY
1241 mod_ctl = rb_malloc(sizeof(mod_ctl_t));
1242 mod_ctl->F = rb_open(ctlfd, RB_FD_SOCKET, "ircd control socket");
1243 mod_ctl->F_pipe = rb_open(pipefd, RB_FD_PIPE, "ircd pipe");
1244 rb_set_nb(mod_ctl->F);
1245 rb_set_nb(mod_ctl->F_pipe);
1246 rb_event_addish("clean_dead_conns", clean_dead_conns, NULL, 10);
4b6a4d47 1247 rb_event_add("check_handshake_flood", check_handshake_flood, NULL, 10);
8d99443b
VY
1248 read_pipe_ctl(mod_ctl->F_pipe, NULL);
1249 mod_read_ctl(mod_ctl->F, mod_ctl);
e9ffc3c1 1250 send_version(mod_ctl);
8d99443b
VY
1251 if(!zlib_ok && !ssl_ok)
1252 {
1253 /* this is really useless... */
1254 send_i_am_useless(mod_ctl);
1255 /* sleep until the ircd kills us */
3202e249 1256 rb_sleep(2 << 30, 0);
8d99443b
VY
1257 exit(1);
1258 }
1259
1260 if(!zlib_ok)
1261 send_nozlib_support(mod_ctl, NULL);
1262 if(!ssl_ok)
1263 send_nossl_support(mod_ctl, NULL);
1264 rb_lib_loop(0);
1265 return 0;
1266}
1267
1268
3202e249 1269#ifndef _WIN32
8d99443b
VY
1270static void
1271dummy_handler(int sig)
1272{
1273 return;
1274}
3202e249 1275#endif
8d99443b
VY
1276
1277static void
1278setup_signals()
1279{
3202e249 1280#ifndef _WIN32
8d99443b
VY
1281 struct sigaction act;
1282
1283 act.sa_flags = 0;
1284 act.sa_handler = SIG_IGN;
1285 sigemptyset(&act.sa_mask);
1286 sigaddset(&act.sa_mask, SIGPIPE);
1287 sigaddset(&act.sa_mask, SIGALRM);
1288#ifdef SIGTRAP
1289 sigaddset(&act.sa_mask, SIGTRAP);
1290#endif
1291
1292#ifdef SIGWINCH
1293 sigaddset(&act.sa_mask, SIGWINCH);
1294 sigaction(SIGWINCH, &act, 0);
1295#endif
1296 sigaction(SIGPIPE, &act, 0);
1297#ifdef SIGTRAP
1298 sigaction(SIGTRAP, &act, 0);
1299#endif
1300
1301 act.sa_handler = dummy_handler;
1302 sigaction(SIGALRM, &act, 0);
3202e249 1303#endif
8d99443b 1304}