]> jfr.im git - solanum.git/blob - wsockd/wsockd.c
Merge branch 'master' into authd-framework-2
[solanum.git] / wsockd / wsockd.c
1 /*
2 * wsockd.c: charybdis websockets helper
3 * Copyright (C) 2007 Aaron Sethman <androsyn@ratbox.org>
4 * Copyright (C) 2007 ircd-ratbox development team
5 * Copyright (C) 2016 William Pitcock <nenolod@dereferenced.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20 * USA
21 */
22
23 #include "stdinc.h"
24
25 #define MAXPASSFD 4
26 #ifndef READBUF_SIZE
27 #define READBUF_SIZE 16384
28 #endif
29
30 static void setup_signals(void);
31 static pid_t ppid;
32
33 static inline uint32_t
34 buf_to_uint32(uint8_t *buf)
35 {
36 uint32_t x;
37 memcpy(&x, buf, sizeof(x));
38 return x;
39 }
40
41 static inline void
42 uint32_to_buf(uint8_t *buf, uint32_t x)
43 {
44 memcpy(buf, &x, sizeof(x));
45 return;
46 }
47
48 typedef struct _mod_ctl_buf
49 {
50 rb_dlink_node node;
51 uint8_t *buf;
52 size_t buflen;
53 rb_fde_t *F[MAXPASSFD];
54 int nfds;
55 } mod_ctl_buf_t;
56
57 typedef struct _mod_ctl
58 {
59 rb_dlink_node node;
60 int cli_count;
61 rb_fde_t *F;
62 rb_fde_t *F_pipe;
63 rb_dlink_list readq;
64 rb_dlink_list writeq;
65 } mod_ctl_t;
66
67 static mod_ctl_t *mod_ctl;
68
69 typedef struct _conn
70 {
71 rb_dlink_node node;
72 mod_ctl_t *ctl;
73 rawbuf_head_t *modbuf_out;
74 rawbuf_head_t *plainbuf_out;
75
76 uint32_t id;
77
78 rb_fde_t *mod_fd;
79 rb_fde_t *plain_fd;
80 uint64_t mod_out;
81 uint64_t mod_in;
82 uint64_t plain_in;
83 uint64_t plain_out;
84 uint8_t flags;
85 } conn_t;
86
87 #define FLAG_CORK 0x01
88 #define FLAG_DEAD 0x02
89 #define FLAG_WSOCK 0x04
90
91 #define IsCork(x) ((x)->flags & FLAG_CORK)
92 #define IsDead(x) ((x)->flags & FLAG_DEAD)
93 #define IsWS(x) ((x)->flags & FLAG_WSOCK)
94
95 #define SetCork(x) ((x)->flags |= FLAG_CORK)
96 #define SetDead(x) ((x)->flags |= FLAG_DEAD)
97 #define SetWS(x) ((x)->flags |= FLAG_WSOCK)
98
99 #define ClearCork(x) ((x)->flags &= ~FLAG_CORK)
100 #define ClearDead(x) ((x)->flags &= ~FLAG_DEAD)
101 #define ClearWS(x) ((x)->flags &= ~FLAG_WSOCK)
102
103 #define NO_WAIT 0x0
104 #define WAIT_PLAIN 0x1
105
106 #define HASH_WALK_SAFE(i, max, ptr, next, table) for(i = 0; i < max; i++) { RB_DLINK_FOREACH_SAFE(ptr, next, table[i].head)
107 #define HASH_WALK_END }
108 #define CONN_HASH_SIZE 2000
109 #define connid_hash(x) (&connid_hash_table[(x % CONN_HASH_SIZE)])
110
111 static rb_dlink_list connid_hash_table[CONN_HASH_SIZE];
112 static rb_dlink_list dead_list;
113
114 static void conn_plain_read_shutdown_cb(rb_fde_t *fd, void *data);
115
116 #ifndef _WIN32
117 static void
118 dummy_handler(int sig)
119 {
120 return;
121 }
122 #endif
123
124 static void
125 setup_signals()
126 {
127 #ifndef _WIN32
128 struct sigaction act;
129
130 act.sa_flags = 0;
131 act.sa_handler = SIG_IGN;
132 sigemptyset(&act.sa_mask);
133 sigaddset(&act.sa_mask, SIGPIPE);
134 sigaddset(&act.sa_mask, SIGALRM);
135 #ifdef SIGTRAP
136 sigaddset(&act.sa_mask, SIGTRAP);
137 #endif
138
139 #ifdef SIGWINCH
140 sigaddset(&act.sa_mask, SIGWINCH);
141 sigaction(SIGWINCH, &act, 0);
142 #endif
143 sigaction(SIGPIPE, &act, 0);
144 #ifdef SIGTRAP
145 sigaction(SIGTRAP, &act, 0);
146 #endif
147
148 act.sa_handler = dummy_handler;
149 sigaction(SIGALRM, &act, 0);
150 #endif
151 }
152
153 static int
154 maxconn(void)
155 {
156 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
157 struct rlimit limit;
158
159 if(!getrlimit(RLIMIT_NOFILE, &limit))
160 {
161 return limit.rlim_cur;
162 }
163 #endif /* RLIMIT_FD_MAX */
164 return MAXCONNECTIONS;
165 }
166
167 static conn_t *
168 conn_find_by_id(uint32_t id)
169 {
170 rb_dlink_node *ptr;
171 conn_t *conn;
172
173 RB_DLINK_FOREACH(ptr, (connid_hash(id))->head)
174 {
175 conn = ptr->data;
176 if(conn->id == id && !IsDead(conn))
177 return conn;
178 }
179 return NULL;
180 }
181
182 static void
183 conn_add_id_hash(conn_t * conn, uint32_t id)
184 {
185 conn->id = id;
186 rb_dlinkAdd(conn, &conn->node, connid_hash(id));
187 }
188
189 static void
190 free_conn(conn_t * conn)
191 {
192 rb_free_rawbuffer(conn->modbuf_out);
193 rb_free_rawbuffer(conn->plainbuf_out);
194 rb_free(conn);
195 }
196
197 static void
198 clean_dead_conns(void *unused)
199 {
200 conn_t *conn;
201 rb_dlink_node *ptr, *next;
202
203 RB_DLINK_FOREACH_SAFE(ptr, next, dead_list.head)
204 {
205 conn = ptr->data;
206 free_conn(conn);
207 }
208
209 dead_list.tail = dead_list.head = NULL;
210 }
211
212 static void
213 mod_write_ctl(rb_fde_t *F, void *data)
214 {
215 mod_ctl_t *ctl = data;
216 mod_ctl_buf_t *ctl_buf;
217 rb_dlink_node *ptr, *next;
218 int retlen, x;
219
220 RB_DLINK_FOREACH_SAFE(ptr, next, ctl->writeq.head)
221 {
222 ctl_buf = ptr->data;
223 retlen = rb_send_fd_buf(ctl->F, ctl_buf->F, ctl_buf->nfds, ctl_buf->buf,
224 ctl_buf->buflen, ppid);
225 if(retlen > 0)
226 {
227 rb_dlinkDelete(ptr, &ctl->writeq);
228 for(x = 0; x < ctl_buf->nfds; x++)
229 rb_close(ctl_buf->F[x]);
230 rb_free(ctl_buf->buf);
231 rb_free(ctl_buf);
232
233 }
234 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
235 exit(0);
236
237 }
238 if(rb_dlink_list_length(&ctl->writeq) > 0)
239 rb_setselect(ctl->F, RB_SELECT_WRITE, mod_write_ctl, ctl);
240 }
241
242 static void
243 mod_cmd_write_queue(mod_ctl_t * ctl, const void *data, size_t len)
244 {
245 mod_ctl_buf_t *ctl_buf;
246 ctl_buf = rb_malloc(sizeof(mod_ctl_buf_t));
247 ctl_buf->buf = rb_malloc(len);
248 ctl_buf->buflen = len;
249 memcpy(ctl_buf->buf, data, len);
250 ctl_buf->nfds = 0;
251 rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->writeq);
252 mod_write_ctl(ctl->F, ctl);
253 }
254
255 static void
256 close_conn(conn_t * conn, int wait_plain, const char *fmt, ...)
257 {
258 va_list ap;
259 char reason[128]; /* must always be under 250 bytes */
260 uint8_t buf[256];
261 int len;
262 if(IsDead(conn))
263 return;
264
265 rb_rawbuf_flush(conn->modbuf_out, conn->mod_fd);
266 rb_rawbuf_flush(conn->plainbuf_out, conn->plain_fd);
267 rb_close(conn->mod_fd);
268 SetDead(conn);
269
270 rb_dlinkDelete(&conn->node, connid_hash(conn->id));
271
272 if(!wait_plain || fmt == NULL)
273 {
274 rb_close(conn->plain_fd);
275 rb_dlinkAdd(conn, &conn->node, &dead_list);
276 return;
277 }
278
279 rb_setselect(conn->plain_fd, RB_SELECT_READ, conn_plain_read_shutdown_cb, conn);
280 rb_setselect(conn->plain_fd, RB_SELECT_WRITE, NULL, NULL);
281
282 va_start(ap, fmt);
283 vsnprintf(reason, sizeof(reason), fmt, ap);
284 va_end(ap);
285
286 buf[0] = 'D';
287 uint32_to_buf(&buf[1], conn->id);
288 rb_strlcpy((char *) &buf[5], reason, sizeof(buf) - 5);
289 len = (strlen(reason) + 1) + 5;
290 mod_cmd_write_queue(conn->ctl, buf, len);
291 }
292
293 static conn_t *
294 make_conn(mod_ctl_t * ctl, rb_fde_t *mod_fd, rb_fde_t *plain_fd)
295 {
296 conn_t *conn = rb_malloc(sizeof(conn_t));
297 conn->ctl = ctl;
298 conn->modbuf_out = rb_new_rawbuffer();
299 conn->plainbuf_out = rb_new_rawbuffer();
300 conn->mod_fd = mod_fd;
301 conn->plain_fd = plain_fd;
302 conn->id = -1;
303 rb_set_nb(mod_fd);
304 rb_set_nb(plain_fd);
305 return conn;
306 }
307
308 static void
309 cleanup_bad_message(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb)
310 {
311 int i;
312
313 /* XXX should log this somehow */
314 for (i = 0; i < ctlb->nfds; i++)
315 rb_close(ctlb->F[i]);
316 }
317
318 static void
319 conn_mod_handshake_cb(rb_fde_t *fd, void *data)
320 {
321 char inbuf[READBUF_SIZE];
322 conn_t *conn = data;
323 int length = 0;
324 if (conn == NULL)
325 return;
326
327 if (IsDead(conn))
328 return;
329
330 while (1)
331 {
332 if (IsDead(conn))
333 return;
334
335 length = rb_read(conn->plain_fd, inbuf, sizeof(inbuf));
336 if (length == 0 || (length < 0 && !rb_ignore_errno(errno)))
337 {
338 close_conn(conn, NO_WAIT, "Connection closed");
339 return;
340 }
341 }
342 }
343
344 static void
345 conn_mod_read_cb(rb_fde_t *fd, void *data)
346 {
347 }
348
349 static void
350 conn_plain_read_cb(rb_fde_t *fd, void *data)
351 {
352 }
353
354 static void
355 conn_plain_read_shutdown_cb(rb_fde_t *fd, void *data)
356 {
357 char inbuf[READBUF_SIZE];
358 conn_t *conn = data;
359 int length = 0;
360
361 if(conn == NULL)
362 return;
363
364 while(1)
365 {
366 length = rb_read(conn->plain_fd, inbuf, sizeof(inbuf));
367
368 if(length == 0 || (length < 0 && !rb_ignore_errno(errno)))
369 {
370 rb_close(conn->plain_fd);
371 rb_dlinkAdd(conn, &conn->node, &dead_list);
372 return;
373 }
374
375 if(length < 0)
376 {
377 rb_setselect(conn->plain_fd, RB_SELECT_READ, conn_plain_read_shutdown_cb, conn);
378 return;
379 }
380 }
381 }
382
383 static void
384 wsock_process(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb)
385 {
386 conn_t *conn;
387 uint32_t id;
388
389 conn = make_conn(ctl, ctlb->F[0], ctlb->F[1]);
390
391 id = buf_to_uint32(&ctlb->buf[1]);
392 conn_add_id_hash(conn, id);
393 SetWS(conn);
394
395 if(rb_get_type(conn->mod_fd) & RB_FD_UNKNOWN)
396 rb_set_type(conn->mod_fd, RB_FD_SOCKET);
397
398 if(rb_get_type(conn->plain_fd) == RB_FD_UNKNOWN)
399 rb_set_type(conn->plain_fd, RB_FD_SOCKET);
400
401 conn_mod_handshake_cb(conn->mod_fd, conn);
402 }
403
404 static void
405 mod_process_cmd_recv(mod_ctl_t * ctl)
406 {
407 rb_dlink_node *ptr, *next;
408 mod_ctl_buf_t *ctl_buf;
409
410 RB_DLINK_FOREACH_SAFE(ptr, next, ctl->readq.head)
411 {
412 ctl_buf = ptr->data;
413
414 switch (*ctl_buf->buf)
415 {
416 case 'A':
417 {
418 if (ctl_buf->nfds != 2 || ctl_buf->buflen != 5)
419 {
420 cleanup_bad_message(ctl, ctl_buf);
421 break;
422 }
423 wsock_process(ctl, ctl_buf);
424 break;
425 }
426 default:
427 break;
428 /* Log unknown commands */
429 }
430 rb_dlinkDelete(ptr, &ctl->readq);
431 rb_free(ctl_buf->buf);
432 rb_free(ctl_buf);
433 }
434
435 }
436
437 static void
438 mod_read_ctl(rb_fde_t *F, void *data)
439 {
440 mod_ctl_buf_t *ctl_buf;
441 mod_ctl_t *ctl = data;
442 int retlen;
443 int i;
444
445 do
446 {
447 ctl_buf = rb_malloc(sizeof(mod_ctl_buf_t));
448 ctl_buf->buf = rb_malloc(READBUF_SIZE);
449 ctl_buf->buflen = READBUF_SIZE;
450 retlen = rb_recv_fd_buf(ctl->F, ctl_buf->buf, ctl_buf->buflen, ctl_buf->F,
451 MAXPASSFD);
452 if(retlen <= 0)
453 {
454 rb_free(ctl_buf->buf);
455 rb_free(ctl_buf);
456 }
457 else
458 {
459 ctl_buf->buflen = retlen;
460 rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->readq);
461 for (i = 0; i < MAXPASSFD && ctl_buf->F[i] != NULL; i++)
462 ;
463 ctl_buf->nfds = i;
464 }
465 }
466 while(retlen > 0);
467
468 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
469 exit(0);
470
471 mod_process_cmd_recv(ctl);
472 rb_setselect(ctl->F, RB_SELECT_READ, mod_read_ctl, ctl);
473 }
474
475 static void
476 read_pipe_ctl(rb_fde_t *F, void *data)
477 {
478 char inbuf[READBUF_SIZE];
479 int retlen;
480 while((retlen = rb_read(F, inbuf, sizeof(inbuf))) > 0)
481 {
482 ;; /* we don't do anything with the pipe really, just care if the other process dies.. */
483 }
484 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
485 exit(0);
486 rb_setselect(F, RB_SELECT_READ, read_pipe_ctl, NULL);
487 }
488
489 int
490 main(int argc, char **argv)
491 {
492 const char *s_ctlfd, *s_pipe, *s_pid;
493 int ctlfd, pipefd, x, maxfd;
494 maxfd = maxconn();
495
496 s_ctlfd = getenv("CTL_FD");
497 s_pipe = getenv("CTL_PIPE");
498 s_pid = getenv("CTL_PPID");
499
500 if(s_ctlfd == NULL || s_pipe == NULL || s_pid == NULL)
501 {
502 fprintf(stderr,
503 "This is the charybdis wsockd for internal ircd use.\n");
504 fprintf(stderr,
505 "You aren't supposed to run me directly. Exiting.\n");
506 exit(1);
507 }
508
509 ctlfd = atoi(s_ctlfd);
510 pipefd = atoi(s_pipe);
511 ppid = atoi(s_pid);
512 x = 0;
513 #ifndef _WIN32
514 for(x = 0; x < maxfd; x++)
515 {
516 if(x != ctlfd && x != pipefd && x > 2)
517 close(x);
518 }
519 x = open("/dev/null", O_RDWR);
520
521 if(x >= 0)
522 {
523 if(ctlfd != 0 && pipefd != 0)
524 dup2(x, 0);
525 if(ctlfd != 1 && pipefd != 1)
526 dup2(x, 1);
527 if(ctlfd != 2 && pipefd != 2)
528 dup2(x, 2);
529 if(x > 2)
530 close(x);
531 }
532 #endif
533 setup_signals();
534 rb_lib_init(NULL, NULL, NULL, 0, maxfd, 1024, 4096);
535 rb_init_rawbuffers(1024);
536
537 mod_ctl = rb_malloc(sizeof(mod_ctl_t));
538 mod_ctl->F = rb_open(ctlfd, RB_FD_SOCKET, "ircd control socket");
539 mod_ctl->F_pipe = rb_open(pipefd, RB_FD_PIPE, "ircd pipe");
540 rb_set_nb(mod_ctl->F);
541 rb_set_nb(mod_ctl->F_pipe);
542 rb_event_addish("clean_dead_conns", clean_dead_conns, NULL, 10);
543 read_pipe_ctl(mod_ctl->F_pipe, NULL);
544 mod_read_ctl(mod_ctl->F, mod_ctl);
545
546 rb_lib_loop(0);
547 return 0;
548 }