]>
Commit | Line | Data |
---|---|---|
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 | ||
34 | static void setup_signals(void); | |
3202e249 | 35 | static pid_t ppid; |
8d99443b | 36 | |
408a29c6 AC |
37 | static inline uint32_t |
38 | buf_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 | 45 | static inline void |
408a29c6 | 46 | uint32_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 |
52 | typedef 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 | ||
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 | ||
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 |
143 | static rb_dlink_list connid_hash_table[CONN_HASH_SIZE]; |
144 | static rb_dlink_list dead_list; | |
145 | ||
3202e249 | 146 | static void conn_mod_read_cb(rb_fde_t *fd, void *data); |
8d99443b VY |
147 | static void conn_mod_write_sendq(rb_fde_t *, void *data); |
148 | static void conn_plain_write_sendq(rb_fde_t *, void *data); | |
149 | static void mod_write_ctl(rb_fde_t *, void *data); | |
3202e249 | 150 | static void conn_plain_read_cb(rb_fde_t *fd, void *data); |
e99f6122 | 151 | static void conn_plain_read_shutdown_cb(rb_fde_t *fd, void *data); |
3202e249 | 152 | static void mod_cmd_write_queue(mod_ctl_t * ctl, const void *data, size_t len); |
8d99443b | 153 | static const char *remote_closed = "Remote host closed the connection"; |
d35870ee | 154 | static bool ssld_ssl_ok; |
cf430c1a | 155 | static int certfp_method = RB_SSL_CERTFP_METH_CERT_SHA1; |
8d99443b | 156 | #ifdef HAVE_LIBZ |
503727d1 | 157 | static bool zlib_ok = true; |
8d99443b | 158 | #else |
503727d1 | 159 | static bool zlib_ok = false; |
8d99443b | 160 | #endif |
4b6a4d47 VY |
161 | |
162 | ||
163 | #ifdef HAVE_LIBZ | |
8d99443b VY |
164 | static void * |
165 | ssld_alloc(void *unused, size_t count, size_t size) | |
166 | { | |
167 | return rb_malloc(count * size); | |
168 | } | |
169 | ||
170 | static void | |
171 | ssld_free(void *unused, void *ptr) | |
172 | { | |
3202e249 | 173 | rb_free(ptr); |
8d99443b | 174 | } |
4b6a4d47 | 175 | #endif |
8d99443b VY |
176 | |
177 | static conn_t * | |
408a29c6 | 178 | conn_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 | ||
192 | static void | |
408a29c6 | 193 | conn_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 | ||
199 | static void | |
200 | free_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 | ||
216 | static void | |
217 | clean_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 | ||
230 | static void | |
231 | close_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 | ||
267 | static conn_t * | |
3202e249 | 268 | make_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 |
283 | static void |
284 | check_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 | } |
303 | HASH_WALK_END} | |
4b6a4d47 | 304 | |
8d99443b | 305 | static void |
3202e249 | 306 | conn_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 | ||
358 | static void | |
359 | conn_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 | ||
366 | static void | |
367 | conn_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 | ||
374 | static void | |
375 | mod_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 | |
388 | static void | |
389 | common_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 | ||
422 | static void | |
423 | common_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 | ||
25a89965 | 462 | static bool |
8d99443b VY |
463 | plain_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); | |
25a89965 | 473 | return true; |
8d99443b | 474 | } |
25a89965 | 475 | return false; |
8d99443b VY |
476 | } |
477 | ||
478 | ||
479 | static void | |
3202e249 | 480 | conn_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 |
528 | static void |
529 | conn_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 | 557 | static void |
3202e249 | 558 | conn_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 | ||
622 | static void | |
3202e249 | 623 | conn_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 | ||
648 | static int | |
649 | maxconn(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 |
662 | static void |
663 | ssl_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 | ||
686 | static void | |
687 | ssl_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 | 702 | static void |
3202e249 | 703 | ssl_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 | ||
720 | static void | |
3202e249 | 721 | ssl_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 |
741 | static void |
742 | cleanup_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 |
751 | static void |
752 | ssl_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 |
772 | static void |
773 | ssl_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 |
778 | static void |
779 | ssl_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 | ||
799 | static void | |
800 | process_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 | ||
827 | #ifdef HAVE_LIBZ | |
8d99443b VY |
828 | static void |
829 | zlib_process(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb) | |
830 | { | |
7edb4f16 | 831 | uint8_t level; |
8d99443b | 832 | size_t recvqlen; |
408a29c6 | 833 | size_t hdr = (sizeof(uint8_t) * 2) + sizeof(uint32_t); |
8d99443b VY |
834 | void *recvq_start; |
835 | z_stream *instream, *outstream; | |
836 | conn_t *conn; | |
408a29c6 | 837 | uint32_t id; |
8d99443b VY |
838 | |
839 | conn = make_conn(ctl, ctlb->F[0], ctlb->F[1]); | |
840 | if(rb_get_type(conn->mod_fd) == RB_FD_UNKNOWN) | |
841 | rb_set_type(conn->mod_fd, RB_FD_SOCKET); | |
842 | ||
843 | if(rb_get_type(conn->plain_fd) == RB_FD_UNKNOWN) | |
844 | rb_set_type(conn->plain_fd, RB_FD_SOCKET); | |
845 | ||
408a29c6 | 846 | id = buf_to_uint32(&ctlb->buf[1]); |
8d99443b VY |
847 | conn_add_id_hash(conn, id); |
848 | ||
3202e249 | 849 | level = (uint8_t)ctlb->buf[5]; |
8d99443b VY |
850 | |
851 | recvqlen = ctlb->buflen - hdr; | |
852 | recvq_start = &ctlb->buf[6]; | |
853 | ||
854 | SetZip(conn); | |
855 | conn->stream = rb_malloc(sizeof(zlib_stream_t)); | |
3202e249 VY |
856 | instream = &((zlib_stream_t *) conn->stream)->instream; |
857 | outstream = &((zlib_stream_t *) conn->stream)->outstream; | |
858 | ||
8d99443b VY |
859 | instream->total_in = 0; |
860 | instream->total_out = 0; | |
861 | instream->zalloc = (alloc_func) ssld_alloc; | |
862 | instream->zfree = (free_func) ssld_free; | |
863 | instream->data_type = Z_ASCII; | |
3202e249 | 864 | inflateInit(&((zlib_stream_t *) conn->stream)->instream); |
8d99443b VY |
865 | |
866 | outstream->total_in = 0; | |
867 | outstream->total_out = 0; | |
868 | outstream->zalloc = (alloc_func) ssld_alloc; | |
869 | outstream->zfree = (free_func) ssld_free; | |
870 | outstream->data_type = Z_ASCII; | |
871 | ||
872 | if(level > 9) | |
408a29c6 | 873 | level = (uint8_t) Z_DEFAULT_COMPRESSION; |
8d99443b | 874 | |
3202e249 | 875 | deflateInit(&((zlib_stream_t *) conn->stream)->outstream, level); |
8d99443b VY |
876 | if(recvqlen > 0) |
877 | common_zlib_inflate(conn, recvq_start, recvqlen); | |
0bd120ed | 878 | |
8d99443b VY |
879 | conn_mod_read_cb(conn->mod_fd, conn); |
880 | conn_plain_read_cb(conn->plain_fd, conn); | |
881 | return; | |
882 | ||
883 | } | |
884 | #endif | |
885 | ||
886 | static void | |
887 | init_prng(mod_ctl_t * ctl, mod_ctl_buf_t * ctl_buf) | |
888 | { | |
889 | char *path; | |
890 | prng_seed_t seed_type; | |
3202e249 VY |
891 | |
892 | seed_type = (prng_seed_t) ctl_buf->buf[1]; | |
85e9bf41 | 893 | path = (char *) &ctl_buf->buf[2]; |
8d99443b VY |
894 | rb_init_prng(path, seed_type); |
895 | } | |
896 | ||
897 | ||
898 | static void | |
899 | ssl_new_keys(mod_ctl_t * ctl, mod_ctl_buf_t * ctl_buf) | |
900 | { | |
901 | char *buf; | |
c1725bda | 902 | char *cert, *key, *dhparam, *cipher_list; |
8d99443b | 903 | |
85e9bf41 | 904 | buf = (char *) &ctl_buf->buf[2]; |
8d99443b VY |
905 | cert = buf; |
906 | buf += strlen(cert) + 1; | |
907 | key = buf; | |
908 | buf += strlen(key) + 1; | |
909 | dhparam = buf; | |
910 | if(strlen(dhparam) == 0) | |
911 | dhparam = NULL; | |
c1725bda AC |
912 | buf += strlen(dhparam) + 1; |
913 | cipher_list = buf; | |
914 | if(strlen(cipher_list) == 0) | |
915 | cipher_list = NULL; | |
8d99443b | 916 | |
c1725bda | 917 | if(!rb_setup_ssl_server(cert, key, dhparam, cipher_list)) |
8d99443b VY |
918 | { |
919 | const char *invalid = "I"; | |
920 | mod_cmd_write_queue(ctl, invalid, strlen(invalid)); | |
921 | return; | |
3202e249 | 922 | } |
8d99443b VY |
923 | } |
924 | ||
925 | static void | |
3202e249 | 926 | send_nossl_support(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb) |
8d99443b VY |
927 | { |
928 | static const char *nossl_cmd = "N"; | |
929 | conn_t *conn; | |
408a29c6 | 930 | uint32_t id; |
8d99443b VY |
931 | |
932 | if(ctlb != NULL) | |
3202e249 | 933 | { |
8d99443b | 934 | conn = make_conn(ctl, ctlb->F[0], ctlb->F[1]); |
408a29c6 AC |
935 | id = buf_to_uint32(&ctlb->buf[1]); |
936 | conn_add_id_hash(conn, id); | |
8d99443b | 937 | close_conn(conn, WAIT_PLAIN, "libratbox reports no SSL/TLS support"); |
3202e249 VY |
938 | } |
939 | mod_cmd_write_queue(ctl, nossl_cmd, strlen(nossl_cmd)); | |
8d99443b VY |
940 | } |
941 | ||
942 | static void | |
3202e249 | 943 | send_i_am_useless(mod_ctl_t * ctl) |
8d99443b VY |
944 | { |
945 | static const char *useless = "U"; | |
946 | mod_cmd_write_queue(ctl, useless, strlen(useless)); | |
947 | } | |
948 | ||
e9ffc3c1 SA |
949 | static void |
950 | send_version(mod_ctl_t * ctl) | |
951 | { | |
952 | char version[256] = { 'V', 0 }; | |
953 | strncpy(&version[1], rb_lib_version(), sizeof(version) - 2); | |
954 | mod_cmd_write_queue(ctl, version, strlen(version)); | |
955 | } | |
956 | ||
8d99443b | 957 | static void |
3202e249 | 958 | send_nozlib_support(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb) |
8d99443b VY |
959 | { |
960 | static const char *nozlib_cmd = "z"; | |
961 | conn_t *conn; | |
408a29c6 | 962 | uint32_t id; |
8d99443b VY |
963 | if(ctlb != NULL) |
964 | { | |
965 | conn = make_conn(ctl, ctlb->F[0], ctlb->F[1]); | |
408a29c6 AC |
966 | id = buf_to_uint32(&ctlb->buf[1]); |
967 | conn_add_id_hash(conn, id); | |
8d99443b | 968 | close_conn(conn, WAIT_PLAIN, "libratbox reports no zlib support"); |
3202e249 | 969 | } |
8d99443b VY |
970 | mod_cmd_write_queue(ctl, nozlib_cmd, strlen(nozlib_cmd)); |
971 | } | |
972 | ||
973 | static void | |
974 | mod_process_cmd_recv(mod_ctl_t * ctl) | |
975 | { | |
976 | rb_dlink_node *ptr, *next; | |
977 | mod_ctl_buf_t *ctl_buf; | |
978 | ||
979 | RB_DLINK_FOREACH_SAFE(ptr, next, ctl->readq.head) | |
980 | { | |
981 | ctl_buf = ptr->data; | |
982 | ||
983 | switch (*ctl_buf->buf) | |
984 | { | |
985 | case 'A': | |
986 | { | |
c03677e9 JT |
987 | if (ctl_buf->nfds != 2 || ctl_buf->buflen != 5) |
988 | { | |
989 | cleanup_bad_message(ctl, ctl_buf); | |
990 | break; | |
991 | } | |
992 | ||
d35870ee | 993 | if(!ssld_ssl_ok) |
8d99443b VY |
994 | { |
995 | send_nossl_support(ctl, ctl_buf); | |
996 | break; | |
997 | } | |
998 | ssl_process_accept(ctl, ctl_buf); | |
999 | break; | |
1000 | } | |
1001 | case 'C': | |
1002 | { | |
9e98a842 | 1003 | if (ctl_buf->buflen != 5) |
c03677e9 JT |
1004 | { |
1005 | cleanup_bad_message(ctl, ctl_buf); | |
1006 | break; | |
1007 | } | |
1008 | ||
d35870ee | 1009 | if(!ssld_ssl_ok) |
8d99443b VY |
1010 | { |
1011 | send_nossl_support(ctl, ctl_buf); | |
1012 | break; | |
1013 | } | |
1014 | ssl_process_connect(ctl, ctl_buf); | |
1015 | break; | |
1016 | } | |
e6bbb410 EM |
1017 | case 'F': |
1018 | { | |
5a9fa2e2 | 1019 | if (ctl_buf->buflen != 5) |
e6bbb410 EM |
1020 | { |
1021 | cleanup_bad_message(ctl, ctl_buf); | |
1022 | break; | |
1023 | } | |
1024 | ssl_change_certfp_method(ctl, ctl_buf); | |
1025 | break; | |
1026 | } | |
8d99443b VY |
1027 | case 'K': |
1028 | { | |
d35870ee | 1029 | if(!ssld_ssl_ok) |
8d99443b VY |
1030 | { |
1031 | send_nossl_support(ctl, ctl_buf); | |
1032 | break; | |
1033 | } | |
1034 | ssl_new_keys(ctl, ctl_buf); | |
1035 | break; | |
1036 | } | |
1037 | case 'I': | |
3202e249 VY |
1038 | init_prng(ctl, ctl_buf); |
1039 | break; | |
8d99443b VY |
1040 | case 'S': |
1041 | { | |
1042 | process_stats(ctl, ctl_buf); | |
1043 | break; | |
1044 | } | |
07c2bb75 | 1045 | |
8d99443b VY |
1046 | #ifdef HAVE_LIBZ |
1047 | case 'Z': | |
1048 | { | |
c03677e9 JT |
1049 | if (ctl_buf->nfds != 2 || ctl_buf->buflen < 6) |
1050 | { | |
1051 | cleanup_bad_message(ctl, ctl_buf); | |
1052 | break; | |
1053 | } | |
1054 | ||
8d99443b VY |
1055 | /* just zlib only */ |
1056 | zlib_process(ctl, ctl_buf); | |
1057 | break; | |
1058 | } | |
1059 | #else | |
55abcbb2 | 1060 | |
8d99443b | 1061 | case 'Z': |
21192997 | 1062 | send_nozlib_support(ctl, ctl_buf); |
8d99443b | 1063 | break; |
3202e249 | 1064 | |
8d99443b VY |
1065 | #endif |
1066 | default: | |
1067 | break; | |
1068 | /* Log unknown commands */ | |
1069 | } | |
1070 | rb_dlinkDelete(ptr, &ctl->readq); | |
1071 | rb_free(ctl_buf->buf); | |
1072 | rb_free(ctl_buf); | |
1073 | } | |
1074 | ||
1075 | } | |
1076 | ||
1077 | ||
1078 | ||
1079 | static void | |
3202e249 | 1080 | mod_read_ctl(rb_fde_t *F, void *data) |
8d99443b VY |
1081 | { |
1082 | mod_ctl_buf_t *ctl_buf; | |
1083 | mod_ctl_t *ctl = data; | |
1084 | int retlen; | |
c03677e9 | 1085 | int i; |
8d99443b VY |
1086 | |
1087 | do | |
1088 | { | |
1089 | ctl_buf = rb_malloc(sizeof(mod_ctl_buf_t)); | |
1090 | ctl_buf->buf = rb_malloc(READBUF_SIZE); | |
1091 | ctl_buf->buflen = READBUF_SIZE; | |
1092 | retlen = rb_recv_fd_buf(ctl->F, ctl_buf->buf, ctl_buf->buflen, ctl_buf->F, | |
1093 | MAXPASSFD); | |
1094 | if(retlen <= 0) | |
1095 | { | |
1096 | rb_free(ctl_buf->buf); | |
1097 | rb_free(ctl_buf); | |
1098 | } | |
1099 | else | |
1100 | { | |
1101 | ctl_buf->buflen = retlen; | |
1102 | rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->readq); | |
c03677e9 JT |
1103 | for (i = 0; i < MAXPASSFD && ctl_buf->F[i] != NULL; i++) |
1104 | ; | |
1105 | ctl_buf->nfds = i; | |
8d99443b VY |
1106 | } |
1107 | } | |
3202e249 | 1108 | while(retlen > 0); |
8d99443b VY |
1109 | |
1110 | if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno))) | |
1111 | exit(0); | |
1112 | ||
1113 | mod_process_cmd_recv(ctl); | |
1114 | rb_setselect(ctl->F, RB_SELECT_READ, mod_read_ctl, ctl); | |
1115 | } | |
1116 | ||
1117 | static void | |
3202e249 | 1118 | mod_write_ctl(rb_fde_t *F, void *data) |
8d99443b VY |
1119 | { |
1120 | mod_ctl_t *ctl = data; | |
1121 | mod_ctl_buf_t *ctl_buf; | |
1122 | rb_dlink_node *ptr, *next; | |
1123 | int retlen, x; | |
1124 | ||
1125 | RB_DLINK_FOREACH_SAFE(ptr, next, ctl->writeq.head) | |
1126 | { | |
1127 | ctl_buf = ptr->data; | |
1128 | retlen = rb_send_fd_buf(ctl->F, ctl_buf->F, ctl_buf->nfds, ctl_buf->buf, | |
3202e249 | 1129 | ctl_buf->buflen, ppid); |
8d99443b VY |
1130 | if(retlen > 0) |
1131 | { | |
1132 | rb_dlinkDelete(ptr, &ctl->writeq); | |
3202e249 | 1133 | for(x = 0; x < ctl_buf->nfds; x++) |
8d99443b VY |
1134 | rb_close(ctl_buf->F[x]); |
1135 | rb_free(ctl_buf->buf); | |
1136 | rb_free(ctl_buf); | |
1137 | ||
1138 | } | |
1139 | if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno))) | |
3202e249 VY |
1140 | exit(0); |
1141 | ||
8d99443b | 1142 | } |
464b7606 JT |
1143 | if(rb_dlink_list_length(&ctl->writeq) > 0) |
1144 | rb_setselect(ctl->F, RB_SELECT_WRITE, mod_write_ctl, ctl); | |
8d99443b VY |
1145 | } |
1146 | ||
1147 | ||
1148 | static void | |
3202e249 | 1149 | read_pipe_ctl(rb_fde_t *F, void *data) |
8d99443b | 1150 | { |
6cd1aca7 | 1151 | char inbuf[READBUF_SIZE]; |
8d99443b | 1152 | int retlen; |
3202e249 | 1153 | while((retlen = rb_read(F, inbuf, sizeof(inbuf))) > 0) |
8d99443b VY |
1154 | { |
1155 | ;; /* we don't do anything with the pipe really, just care if the other process dies.. */ | |
1156 | } | |
1157 | if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno))) | |
1158 | exit(0); | |
1159 | rb_setselect(F, RB_SELECT_READ, read_pipe_ctl, NULL); | |
1160 | ||
1161 | } | |
1162 | ||
1163 | int | |
1164 | main(int argc, char **argv) | |
1165 | { | |
3202e249 | 1166 | const char *s_ctlfd, *s_pipe, *s_pid; |
8d99443b VY |
1167 | int ctlfd, pipefd, x, maxfd; |
1168 | maxfd = maxconn(); | |
3202e249 | 1169 | |
8d99443b VY |
1170 | s_ctlfd = getenv("CTL_FD"); |
1171 | s_pipe = getenv("CTL_PIPE"); | |
3202e249 | 1172 | s_pid = getenv("CTL_PPID"); |
8d99443b | 1173 | |
3202e249 | 1174 | if(s_ctlfd == NULL || s_pipe == NULL || s_pid == NULL) |
8d99443b | 1175 | { |
3202e249 | 1176 | fprintf(stderr, |
4dbb75ee | 1177 | "This is the charybdis ssld for internal ircd use.\n"); |
3202e249 | 1178 | fprintf(stderr, |
4dbb75ee | 1179 | "You aren't supposed to run me directly. Exiting.\n"); |
8d99443b VY |
1180 | exit(1); |
1181 | } | |
1182 | ||
1183 | ctlfd = atoi(s_ctlfd); | |
1184 | pipefd = atoi(s_pipe); | |
3202e249 VY |
1185 | ppid = atoi(s_pid); |
1186 | x = 0; | |
1187 | #ifndef _WIN32 | |
1188 | for(x = 0; x < maxfd; x++) | |
8d99443b VY |
1189 | { |
1190 | if(x != ctlfd && x != pipefd && x > 2) | |
1191 | close(x); | |
1192 | } | |
8d99443b | 1193 | x = open("/dev/null", O_RDWR); |
464b7606 | 1194 | |
8d99443b VY |
1195 | if(x >= 0) |
1196 | { | |
1197 | if(ctlfd != 0 && pipefd != 0) | |
1198 | dup2(x, 0); | |
1199 | if(ctlfd != 1 && pipefd != 1) | |
1200 | dup2(x, 1); | |
1201 | if(ctlfd != 2 && pipefd != 2) | |
1202 | dup2(x, 2); | |
1203 | if(x > 2) | |
1204 | close(x); | |
1205 | } | |
3202e249 | 1206 | #endif |
8d99443b VY |
1207 | setup_signals(); |
1208 | rb_lib_init(NULL, NULL, NULL, 0, maxfd, 1024, 4096); | |
1209 | rb_init_rawbuffers(1024); | |
d35870ee | 1210 | ssld_ssl_ok = rb_supports_ssl(); |
8d99443b VY |
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); | |
4b6a4d47 | 1217 | rb_event_add("check_handshake_flood", check_handshake_flood, NULL, 10); |
8d99443b VY |
1218 | read_pipe_ctl(mod_ctl->F_pipe, NULL); |
1219 | mod_read_ctl(mod_ctl->F, mod_ctl); | |
e9ffc3c1 | 1220 | send_version(mod_ctl); |
d35870ee | 1221 | if(!zlib_ok && !ssld_ssl_ok) |
8d99443b VY |
1222 | { |
1223 | /* this is really useless... */ | |
1224 | send_i_am_useless(mod_ctl); | |
1225 | /* sleep until the ircd kills us */ | |
3202e249 | 1226 | rb_sleep(2 << 30, 0); |
8d99443b VY |
1227 | exit(1); |
1228 | } | |
1229 | ||
1230 | if(!zlib_ok) | |
1231 | send_nozlib_support(mod_ctl, NULL); | |
d35870ee | 1232 | if(!ssld_ssl_ok) |
8d99443b VY |
1233 | send_nossl_support(mod_ctl, NULL); |
1234 | rb_lib_loop(0); | |
1235 | return 0; | |
1236 | } | |
1237 | ||
1238 | ||
3202e249 | 1239 | #ifndef _WIN32 |
8d99443b VY |
1240 | static void |
1241 | dummy_handler(int sig) | |
1242 | { | |
1243 | return; | |
1244 | } | |
3202e249 | 1245 | #endif |
8d99443b VY |
1246 | |
1247 | static void | |
1248 | setup_signals() | |
1249 | { | |
3202e249 | 1250 | #ifndef _WIN32 |
8d99443b VY |
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); | |
3202e249 | 1273 | #endif |
8d99443b | 1274 | } |