Blob


1 /*
2 * Copyright (c) 2016, 2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
5 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
20 #include <sys/param.h>
21 #include <sys/ioctl.h>
22 #include <sys/wait.h>
23 #include <sys/uio.h>
24 #include <sys/resource.h>
25 #include <sys/socket.h>
26 #include <sys/stat.h>
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <sys/mman.h>
30 #include <sys/un.h>
32 #include <net/if.h>
33 #include <netinet/in.h>
35 #include <errno.h>
36 #include <event.h>
37 #include <fcntl.h>
38 #include <ifaddrs.h>
39 #include <limits.h>
40 #include <netdb.h>
41 #include <poll.h>
42 #include <pwd.h>
43 #include <stddef.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
49 #include "got_error.h"
50 #include "got_opentemp.h"
52 #include "proc.h"
53 #include "gotwebd.h"
55 #include "got_compat.h"
57 #define SOCKS_BACKLOG 5
58 #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
61 volatile int client_cnt;
63 struct timeval timeout = { TIMEOUT_DEFAULT, 0 };
65 void sockets_sighdlr(int, short, void *);
66 void sockets_run(struct privsep *, struct privsep_proc *, void *);
67 void sockets_launch(void);
68 void sockets_purge(struct gotwebd *);
69 void sockets_accept_paused(int, short, void *);
70 void sockets_dup_new_socket(struct socket *, struct socket *);
71 void sockets_rlimit(int);
74 int sockets_dispatch_gotwebd(int, struct privsep_proc *, struct imsg *);
75 int sockets_unix_socket_listen(struct privsep *, struct socket *);
76 int sockets_create_socket(struct addresslist *, in_port_t);
77 int sockets_socket_af(struct sockaddr_storage *, in_port_t);
78 int sockets_accept_reserve(int, struct sockaddr *, socklen_t *, int,
79 volatile int *);
81 struct socket
82 *sockets_conf_new_socket(struct gotwebd *, struct server *, int, int,
83 int);
85 int cgi_inflight = 0;
87 static struct privsep_proc procs[] = {
88 { "gotwebd", PROC_GOTWEBD, sockets_dispatch_gotwebd },
89 };
91 void
92 sockets(struct privsep *ps, struct privsep_proc *p)
93 {
94 proc_run(ps, p, procs, nitems(procs), sockets_run, NULL);
95 }
97 void
98 sockets_run(struct privsep *ps, struct privsep_proc *p, void *arg)
99 {
100 if (config_init(ps->ps_env) == -1)
101 fatal("failed to initialize configuration");
103 p->p_shutdown = sockets_shutdown;
105 sockets_rlimit(-1);
107 signal_del(&ps->ps_evsigchld);
108 signal_set(&ps->ps_evsigchld, SIGCHLD, sockets_sighdlr, ps);
109 signal_add(&ps->ps_evsigchld, NULL);
111 #ifndef PROFILE
112 if (pledge("stdio rpath wpath cpath inet recvfd proc exec sendfd",
113 NULL) == -1)
114 fatal("pledge");
115 #endif
118 void
119 sockets_parse_sockets(struct gotwebd *env)
121 struct server *srv;
122 struct socket *sock, *new_sock = NULL;
123 struct address *a;
124 int sock_id = 0, ipv4 = 0, ipv6 = 0;
126 TAILQ_FOREACH(srv, env->servers, entry) {
127 if (srv->unix_socket) {
128 sock_id++;
129 new_sock = sockets_conf_new_socket(env, srv,
130 sock_id, UNIX, 0);
131 TAILQ_INSERT_TAIL(env->sockets, new_sock, entry);
134 if (srv->fcgi_socket) {
135 sock_id++;
136 new_sock = sockets_conf_new_socket(env, srv,
137 sock_id, FCGI, 0);
138 TAILQ_INSERT_TAIL(env->sockets, new_sock, entry);
140 /* add ipv6 children */
141 TAILQ_FOREACH(sock, env->sockets, entry) {
142 ipv4 = ipv6 = 0;
144 TAILQ_FOREACH(a, sock->conf.al, entry) {
145 if (a->ss.ss_family == AF_INET)
146 ipv4 = 1;
147 if (a->ss.ss_family == AF_INET6)
148 ipv6 = 1;
151 /* create ipv6 sock */
152 if (ipv4 == 1 && ipv6 == 1) {
153 sock_id++;
154 sock->conf.child_id = sock_id;
155 new_sock = sockets_conf_new_socket(env,
156 srv, sock_id, FCGI, 1);
157 sockets_dup_new_socket(sock, new_sock);
158 TAILQ_INSERT_TAIL(env->sockets,
159 new_sock, entry);
160 continue;
167 void
168 sockets_dup_new_socket(struct socket *p_sock, struct socket *sock)
170 struct address *a, *acp;
171 int n;
173 sock->conf.parent_id = p_sock->conf.id;
174 sock->conf.ipv4 = 0;
175 sock->conf.ipv6 = 1;
177 memcpy(&sock->conf.srv_name, p_sock->conf.srv_name,
178 sizeof(sock->conf.srv_name));
180 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_child",
181 p_sock->conf.srv_name);
182 if (n < 0) {
183 free(p_sock->conf.al);
184 free(p_sock);
185 free(sock->conf.al);
186 free(sock);
187 fatalx("%s: snprintf", __func__);
190 TAILQ_FOREACH(a, p_sock->conf.al, entry) {
191 if (a->ss.ss_family == AF_INET)
192 continue;
194 if ((acp = calloc(1, sizeof(*acp))) == NULL)
195 fatal("%s: calloc", __func__);
196 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
197 acp->ipproto = a->ipproto;
198 acp->prefixlen = a->prefixlen;
199 acp->port = a->port;
200 if (strlen(a->ifname) != 0) {
201 if (strlcpy(acp->ifname, a->ifname,
202 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
203 fatalx("%s: interface name truncated",
204 __func__);
208 TAILQ_INSERT_TAIL(sock->conf.al, acp, entry);
212 struct socket *
213 sockets_conf_new_socket(struct gotwebd *env, struct server *srv, int id,
214 int type, int is_dup)
216 struct socket *sock;
217 struct address *a, *acp;
218 int n;
220 if ((sock = calloc(1, sizeof(*sock))) == NULL)
221 fatalx("%s: calloc", __func__);
223 if ((sock->conf.al = calloc(1, sizeof(*sock->conf.al))) == NULL) {
224 free(sock);
225 fatalx("%s: calloc", __func__);
227 TAILQ_INIT(sock->conf.al);
229 sock->conf.parent_id = 0;
230 sock->conf.id = id;
232 sock->fd = -1;
234 sock->conf.type = type;
236 if (type == UNIX) {
237 if (strlcpy(sock->conf.unix_socket_name,
238 srv->unix_socket_name,
239 sizeof(sock->conf.unix_socket_name)) >=
240 sizeof(sock->conf.unix_socket_name)) {
241 free(sock->conf.al);
242 free(sock);
243 fatalx("%s: strlcpy", __func__);
245 } else
246 sock->conf.ipv4 = 1;
248 sock->conf.fcgi_socket_port = srv->fcgi_socket_port;
250 if (is_dup)
251 goto done;
253 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
254 srv->name);
255 if (n < 0) {
256 free(sock->conf.al);
257 free(sock);
258 fatalx("%s: snprintf", __func__);
261 if (strlcpy(sock->conf.srv_name, srv->name,
262 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
263 free(sock->conf.al);
264 free(sock);
265 fatalx("%s: strlcpy", __func__);
268 TAILQ_FOREACH(a, srv->al, entry) {
269 if ((acp = calloc(1, sizeof(*acp))) == NULL) {
270 free(sock->conf.al);
271 free(sock);
272 fatal("%s: calloc", __func__);
274 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
275 acp->ipproto = a->ipproto;
276 acp->prefixlen = a->prefixlen;
277 acp->port = a->port;
278 if (strlen(a->ifname) != 0) {
279 if (strlcpy(acp->ifname, a->ifname,
280 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
281 fatalx("%s: interface name truncated",
282 __func__);
286 TAILQ_INSERT_TAIL(sock->conf.al, acp, entry);
288 done:
289 return (sock);
292 int
293 sockets_socket_af(struct sockaddr_storage *ss, in_port_t port)
295 switch (ss->ss_family) {
296 case AF_INET:
297 ((struct sockaddr_in *)ss)->sin_port = port;
298 /* TA: Iffy... */
299 #ifndef __linux__
300 ((struct sockaddr_in *)ss)->sin_len =
301 sizeof(struct sockaddr_in);
302 #endif
303 break;
304 case AF_INET6:
305 ((struct sockaddr_in6 *)ss)->sin6_port = port;
306 /* TA: Iffy... */
307 #ifndef __linux__
308 ((struct sockaddr_in6 *)ss)->sin6_len =
309 sizeof(struct sockaddr_in6);
310 #endif
311 break;
312 default:
313 return -1;
316 return 0;
319 void
320 sockets_launch(void)
322 struct socket *sock;
324 TAILQ_FOREACH(sock, gotwebd_env->sockets, entry) {
325 log_debug("%s: configuring socket %d (%d)", __func__,
326 sock->conf.id, sock->fd);
328 event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
329 sockets_socket_accept, sock);
331 if (event_add(&sock->ev, NULL))
332 fatalx("event add sock");
334 evtimer_set(&sock->pause, sockets_accept_paused, sock);
336 log_debug("%s: running socket listener %d", __func__,
337 sock->conf.id);
341 void
342 sockets_purge(struct gotwebd *env)
344 struct socket *sock, *tsock;
346 /* shutdown and remove sockets */
347 TAILQ_FOREACH_SAFE(sock, env->sockets, entry, tsock) {
348 if (event_initialized(&sock->ev))
349 event_del(&sock->ev);
350 if (evtimer_initialized(&sock->evt))
351 evtimer_del(&sock->evt);
352 if (evtimer_initialized(&sock->pause))
353 evtimer_del(&sock->pause);
354 if (sock->fd != -1)
355 close(sock->fd);
356 TAILQ_REMOVE(env->sockets, sock, entry);
360 int
361 sockets_dispatch_gotwebd(int fd, struct privsep_proc *p, struct imsg *imsg)
363 struct privsep *ps = p->p_ps;
364 int res = 0, cmd = 0, verbose;
366 switch (imsg->hdr.type) {
367 case IMSG_CFG_SRV:
368 config_getserver(gotwebd_env, imsg);
369 break;
370 case IMSG_CFG_SOCK:
371 config_getsock(gotwebd_env, imsg);
372 break;
373 case IMSG_CFG_FD:
374 config_getfd(gotwebd_env, imsg);
375 break;
376 case IMSG_CFG_DONE:
377 config_getcfg(gotwebd_env, imsg);
378 break;
379 case IMSG_CTL_START:
380 sockets_launch();
381 break;
382 case IMSG_CTL_VERBOSE:
383 IMSG_SIZE_CHECK(imsg, &verbose);
384 memcpy(&verbose, imsg->data, sizeof(verbose));
385 log_setverbose(verbose);
386 break;
387 default:
388 return -1;
391 switch (cmd) {
392 case 0:
393 break;
394 default:
395 if (proc_compose_imsg(ps, PROC_GOTWEBD, -1, cmd,
396 imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
397 return -1;
398 break;
401 return 0;
404 void
405 sockets_sighdlr(int sig, short event, void *arg)
407 switch (sig) {
408 case SIGHUP:
409 log_info("%s: ignoring SIGHUP", __func__);
410 break;
411 case SIGPIPE:
412 log_info("%s: ignoring SIGPIPE", __func__);
413 break;
414 case SIGUSR1:
415 log_info("%s: ignoring SIGUSR1", __func__);
416 break;
417 case SIGCHLD:
418 break;
419 default:
420 log_info("SIGNAL: %d", sig);
421 fatalx("unexpected signal");
425 void
426 sockets_shutdown(void)
428 struct server *srv, *tsrv;
429 struct socket *sock, *tsock;
431 sockets_purge(gotwebd_env);
433 /* clean sockets */
434 TAILQ_FOREACH_SAFE(sock, gotwebd_env->sockets, entry, tsock) {
435 TAILQ_REMOVE(gotwebd_env->sockets, sock, entry);
436 close(sock->fd);
437 free(sock);
440 /* clean servers */
441 TAILQ_FOREACH_SAFE(srv, gotwebd_env->servers, entry, tsrv)
442 free(srv);
444 free(gotwebd_env->sockets);
445 free(gotwebd_env->servers);
446 free(gotwebd_env);
449 int
450 sockets_privinit(struct gotwebd *env, struct socket *sock)
452 struct privsep *ps = env->gotwebd_ps;
454 if (sock->conf.type == UNIX) {
455 log_debug("%s: initializing unix socket %s", __func__,
456 sock->conf.unix_socket_name);
457 sock->fd = sockets_unix_socket_listen(ps, sock);
458 if (sock->fd == -1) {
459 log_warnx("%s: create unix socket failed", __func__);
460 return -1;
464 if (sock->conf.type == FCGI) {
465 log_debug("%s: initializing fcgi socket for %s", __func__,
466 sock->conf.name);
467 sock->fd = sockets_create_socket(sock->conf.al,
468 sock->conf.fcgi_socket_port);
469 if (sock->fd == -1) {
470 log_warnx("%s: create unix socket failed", __func__);
471 return -1;
475 return 0;
478 int
479 sockets_unix_socket_listen(struct privsep *ps, struct socket *sock)
481 struct gotwebd *env = ps->ps_env;
482 struct sockaddr_un sun;
483 struct socket *tsock;
484 int u_fd = -1;
485 mode_t old_umask, mode;
487 TAILQ_FOREACH(tsock, env->sockets, entry) {
488 if (strcmp(tsock->conf.unix_socket_name,
489 sock->conf.unix_socket_name) == 0 &&
490 tsock->fd != -1)
491 return (tsock->fd);
494 /* TA: FIXME: this needs upstreaming. */
495 int socket_flags = SOCK_STREAM | SOCK_NONBLOCK;
496 #ifdef SOCK_CLOEXEC
497 socket_flags |= SOCK_CLOEXEC;
498 #endif
499 u_fd = socket(AF_UNIX, socket_flags, 0);
500 if (u_fd == -1) {
501 log_warn("%s: socket", __func__);
502 return -1;
505 sun.sun_family = AF_UNIX;
506 if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
507 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
508 log_warn("%s: %s name too long", __func__,
509 sock->conf.unix_socket_name);
510 close(u_fd);
511 return -1;
514 if (unlink(sock->conf.unix_socket_name) == -1) {
515 if (errno != ENOENT) {
516 log_warn("%s: unlink %s", __func__,
517 sock->conf.unix_socket_name);
518 close(u_fd);
519 return -1;
523 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
524 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
526 if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
527 log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
528 close(u_fd);
529 (void)umask(old_umask);
530 return -1;
533 (void)umask(old_umask);
535 if (chmod(sock->conf.unix_socket_name, mode) == -1) {
536 log_warn("%s: chmod", __func__);
537 close(u_fd);
538 (void)unlink(sock->conf.unix_socket_name);
539 return -1;
542 if (chown(sock->conf.unix_socket_name, ps->ps_pw->pw_uid,
543 ps->ps_pw->pw_gid) == -1) {
544 log_warn("%s: chown", __func__);
545 close(u_fd);
546 (void)unlink(sock->conf.unix_socket_name);
547 return -1;
550 if (listen(u_fd, SOCKS_BACKLOG) == -1) {
551 log_warn("%s: listen", __func__);
552 return -1;
555 return u_fd;
558 int
559 sockets_create_socket(struct addresslist *al, in_port_t port)
561 struct addrinfo hints;
562 struct address *a;
563 int fd = -1, o_val = 1, flags;
565 memset(&hints, 0, sizeof(hints));
566 hints.ai_family = AF_UNSPEC;
567 hints.ai_socktype = SOCK_STREAM;
568 hints.ai_flags |= AI_PASSIVE;
570 TAILQ_FOREACH(a, al, entry) {
571 if (sockets_socket_af(&a->ss, port) == -1) {
572 log_warnx("%s: sockets_socket_af", __func__);
573 goto fail;
576 fd = socket(a->ss.ss_family, hints.ai_socktype,
577 a->ipproto);
578 log_debug("%s: opening socket (%d) for %s", __func__,
579 fd, a->ifname);
581 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
582 sizeof(int)) == -1) {
583 log_warn("%s: setsockopt error", __func__);
584 return -1;
587 /* non-blocking */
588 flags = fcntl(fd, F_GETFL);
589 flags |= O_NONBLOCK;
590 fcntl(fd, F_SETFL, flags);
592 /* TA: 'sizeof a' vs a->ss.len */
593 if (bind(fd, (struct sockaddr *)&a->ss, sizeof a) == -1) {
594 close(fd);
595 log_info("%s: can't bind to port %d", __func__,
596 ntohs(port));
597 goto fail;
600 if (listen(fd, SOMAXCONN) == -1) {
601 log_warn("%s, unable to listen on socket", __func__);
602 goto fail;
606 free(a);
607 return (fd);
608 fail:
609 free(a);
610 return -1;
613 int
614 sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
615 int reserve, volatile int *counter)
617 int ret;
619 if (getdtablecount() + reserve +
620 ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
621 log_debug("inflight fds exceeded");
622 errno = EMFILE;
623 return -1;
625 /* TA: This needs fixing upstream. */
626 #ifdef __APPLE__
627 ret = accept(sockfd, addr, addrlen);
628 #else
629 ret = accept4(sockfd, addr, addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC);
630 #endif
632 if (ret > -1) {
633 (*counter)++;
634 log_debug("inflight incremented, now %d", *counter);
637 return ret;
640 void
641 sockets_accept_paused(int fd, short events, void *arg)
643 struct socket *sock = (struct socket *)arg;
645 event_add(&sock->ev, NULL);
648 void
649 sockets_socket_accept(int fd, short event, void *arg)
651 struct socket *sock = (struct socket *)arg;
652 struct sockaddr_storage ss;
653 struct timeval backoff;
654 struct request *c = NULL;
655 socklen_t len;
656 int s;
658 backoff.tv_sec = 1;
659 backoff.tv_usec = 0;
661 event_add(&sock->ev, NULL);
662 if (event & EV_TIMEOUT)
663 return;
665 len = sizeof(ss);
667 s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
668 FD_RESERVE, &cgi_inflight);
670 if (s == -1) {
671 switch (errno) {
672 case EINTR:
673 case EWOULDBLOCK:
674 case ECONNABORTED:
675 return;
676 case EMFILE:
677 case ENFILE:
678 event_del(&sock->ev);
679 evtimer_add(&sock->pause, &backoff);
680 return;
681 default:
682 log_warn("%s: accept", __func__);
686 if (client_cnt > GOTWEBD_MAXCLIENTS)
687 goto err;
689 c = calloc(1, sizeof(struct request));
690 if (c == NULL) {
691 log_warn("%s", __func__);
692 close(s);
693 cgi_inflight--;
694 return;
697 c->fd = s;
698 c->sock = sock;
699 memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
700 c->buf_pos = 0;
701 c->buf_len = 0;
702 c->request_started = 0;
703 c->sock->client_status = CLIENT_CONNECT;
705 event_set(&c->ev, s, EV_READ, fcgi_request, c);
706 event_add(&c->ev, NULL);
708 evtimer_set(&c->tmo, fcgi_timeout, c);
709 evtimer_add(&c->tmo, &timeout);
711 client_cnt++;
713 return;
714 err:
715 cgi_inflight--;
716 close(s);
717 if (c != NULL)
718 free(c);
721 void
722 sockets_rlimit(int maxfd)
724 struct rlimit rl;
726 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
727 fatal("%s: failed to get resource limit", __func__);
728 log_debug("%s: max open files %llu", __func__, rl.rlim_max);
730 /*
731 * Allow the maximum number of open file descriptors for this
732 * login class (which should be the class "daemon" by default).
733 */
734 if (maxfd == -1)
735 rl.rlim_cur = rl.rlim_max;
736 else
737 rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
738 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
739 fatal("%s: failed to set resource limit", __func__);