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