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>
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.
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.
20 #include <sys/param.h>
21 #include <sys/ioctl.h>
22 #include <sys/queue.h>
25 #include <sys/resource.h>
26 #include <sys/socket.h>
29 #include <sys/types.h>
34 #include <netinet/in.h>
50 #include "got_error.h"
51 #include "got_opentemp.h"
56 #include "got_compat.h"
58 #define SOCKS_BACKLOG 5
59 #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
62 volatile int client_cnt;
64 struct timeval timeout = { TIMEOUT_DEFAULT, 0 };
66 void sockets_sighdlr(int, short, void *);
67 void sockets_run(struct privsep *, struct privsep_proc *, void *);
68 void sockets_launch(void);
69 void sockets_purge(struct gotwebd *);
70 void sockets_accept_paused(int, short, void *);
71 void sockets_dup_new_socket(struct socket *, struct socket *);
72 void sockets_rlimit(int);
75 int sockets_dispatch_gotwebd(int, struct privsep_proc *, struct imsg *);
76 int sockets_unix_socket_listen(struct privsep *, struct socket *);
77 int sockets_create_socket(struct addresslist *, in_port_t);
78 int sockets_socket_af(struct sockaddr_storage *, in_port_t);
79 int sockets_accept_reserve(int, struct sockaddr *, socklen_t *, int,
83 *sockets_conf_new_socket(struct gotwebd *, struct server *, int, int,
88 static struct privsep_proc procs[] = {
89 { "gotwebd", PROC_GOTWEBD, sockets_dispatch_gotwebd },
93 sockets(struct privsep *ps, struct privsep_proc *p)
95 proc_run(ps, p, procs, nitems(procs), sockets_run, NULL);
99 sockets_run(struct privsep *ps, struct privsep_proc *p, void *arg)
101 if (config_init(ps->ps_env) == -1)
102 fatal("failed to initialize configuration");
104 p->p_shutdown = sockets_shutdown;
108 signal_del(&ps->ps_evsigchld);
109 signal_set(&ps->ps_evsigchld, SIGCHLD, sockets_sighdlr, ps);
110 signal_add(&ps->ps_evsigchld, NULL);
113 if (pledge("stdio rpath wpath cpath inet recvfd proc exec sendfd",
120 sockets_parse_sockets(struct gotwebd *env)
123 struct socket *sock, *new_sock = NULL;
125 int sock_id = 0, ipv4 = 0, ipv6 = 0;
127 TAILQ_FOREACH(srv, env->servers, entry) {
128 if (srv->unix_socket) {
130 new_sock = sockets_conf_new_socket(env, srv,
132 TAILQ_INSERT_TAIL(env->sockets, new_sock, entry);
135 if (srv->fcgi_socket) {
137 new_sock = sockets_conf_new_socket(env, srv,
139 TAILQ_INSERT_TAIL(env->sockets, new_sock, entry);
141 /* add ipv6 children */
142 TAILQ_FOREACH(sock, env->sockets, entry) {
145 TAILQ_FOREACH(a, sock->conf.al, entry) {
146 if (a->ss.ss_family == AF_INET)
148 if (a->ss.ss_family == AF_INET6)
152 /* create ipv6 sock */
153 if (ipv4 == 1 && ipv6 == 1) {
155 sock->conf.child_id = sock_id;
156 new_sock = sockets_conf_new_socket(env,
157 srv, sock_id, FCGI, 1);
158 sockets_dup_new_socket(sock, new_sock);
159 TAILQ_INSERT_TAIL(env->sockets,
169 sockets_dup_new_socket(struct socket *p_sock, struct socket *sock)
171 struct address *a, *acp;
174 sock->conf.parent_id = p_sock->conf.id;
178 memcpy(&sock->conf.srv_name, p_sock->conf.srv_name,
179 sizeof(sock->conf.srv_name));
181 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_child",
182 p_sock->conf.srv_name);
184 free(p_sock->conf.al);
188 fatalx("%s: snprintf", __func__);
191 TAILQ_FOREACH(a, p_sock->conf.al, entry) {
192 if (a->ss.ss_family == AF_INET)
195 if ((acp = calloc(1, sizeof(*acp))) == NULL)
196 fatal("%s: calloc", __func__);
197 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
198 acp->ipproto = a->ipproto;
199 acp->prefixlen = a->prefixlen;
201 if (strlen(a->ifname) != 0) {
202 if (strlcpy(acp->ifname, a->ifname,
203 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
204 fatalx("%s: interface name truncated",
209 TAILQ_INSERT_TAIL(sock->conf.al, acp, entry);
214 sockets_conf_new_socket(struct gotwebd *env, struct server *srv, int id,
215 int type, int is_dup)
218 struct address *a, *acp;
221 if ((sock = calloc(1, sizeof(*sock))) == NULL)
222 fatalx("%s: calloc", __func__);
224 if ((sock->conf.al = calloc(1, sizeof(*sock->conf.al))) == NULL) {
226 fatalx("%s: calloc", __func__);
228 TAILQ_INIT(sock->conf.al);
230 sock->conf.parent_id = 0;
235 sock->conf.type = type;
238 if (strlcpy(sock->conf.unix_socket_name,
239 srv->unix_socket_name,
240 sizeof(sock->conf.unix_socket_name)) >=
241 sizeof(sock->conf.unix_socket_name)) {
244 fatalx("%s: strlcpy", __func__);
249 sock->conf.fcgi_socket_port = srv->fcgi_socket_port;
254 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
259 fatalx("%s: snprintf", __func__);
262 if (strlcpy(sock->conf.srv_name, srv->name,
263 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
266 fatalx("%s: strlcpy", __func__);
269 TAILQ_FOREACH(a, srv->al, entry) {
270 if ((acp = calloc(1, sizeof(*acp))) == NULL) {
273 fatal("%s: calloc", __func__);
275 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
276 acp->ipproto = a->ipproto;
277 acp->prefixlen = a->prefixlen;
279 if (strlen(a->ifname) != 0) {
280 if (strlcpy(acp->ifname, a->ifname,
281 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
282 fatalx("%s: interface name truncated",
287 TAILQ_INSERT_TAIL(sock->conf.al, acp, entry);
294 sockets_socket_af(struct sockaddr_storage *ss, in_port_t port)
296 switch (ss->ss_family) {
298 ((struct sockaddr_in *)ss)->sin_port = port;
301 ((struct sockaddr_in *)ss)->sin_len =
302 sizeof(struct sockaddr_in);
306 ((struct sockaddr_in6 *)ss)->sin6_port = port;
309 ((struct sockaddr_in6 *)ss)->sin6_len =
310 sizeof(struct sockaddr_in6);
325 TAILQ_FOREACH(sock, gotwebd_env->sockets, entry) {
326 log_debug("%s: configuring socket %d (%d)", __func__,
327 sock->conf.id, sock->fd);
329 event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
330 sockets_socket_accept, sock);
332 if (event_add(&sock->ev, NULL))
333 fatalx("event add sock");
335 evtimer_set(&sock->pause, sockets_accept_paused, sock);
337 log_debug("%s: running socket listener %d", __func__,
343 sockets_purge(struct gotwebd *env)
345 struct socket *sock, *tsock;
347 /* shutdown and remove sockets */
348 TAILQ_FOREACH_SAFE(sock, env->sockets, entry, tsock) {
349 if (event_initialized(&sock->ev))
350 event_del(&sock->ev);
351 if (evtimer_initialized(&sock->evt))
352 evtimer_del(&sock->evt);
353 if (evtimer_initialized(&sock->pause))
354 evtimer_del(&sock->pause);
357 TAILQ_REMOVE(env->sockets, sock, entry);
362 sockets_dispatch_gotwebd(int fd, struct privsep_proc *p, struct imsg *imsg)
364 struct privsep *ps = p->p_ps;
365 int res = 0, cmd = 0, verbose;
367 switch (imsg->hdr.type) {
369 config_getserver(gotwebd_env, imsg);
372 config_getsock(gotwebd_env, imsg);
375 config_getfd(gotwebd_env, imsg);
378 config_getcfg(gotwebd_env, imsg);
383 case IMSG_CTL_VERBOSE:
384 IMSG_SIZE_CHECK(imsg, &verbose);
385 memcpy(&verbose, imsg->data, sizeof(verbose));
386 log_setverbose(verbose);
396 if (proc_compose_imsg(ps, PROC_GOTWEBD, -1, cmd,
397 imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
406 sockets_sighdlr(int sig, short event, void *arg)
410 log_info("%s: ignoring SIGHUP", __func__);
413 log_info("%s: ignoring SIGPIPE", __func__);
416 log_info("%s: ignoring SIGUSR1", __func__);
421 log_info("SIGNAL: %d", sig);
422 fatalx("unexpected signal");
427 sockets_shutdown(void)
429 struct server *srv, *tsrv;
430 struct socket *sock, *tsock;
432 sockets_purge(gotwebd_env);
435 TAILQ_FOREACH_SAFE(sock, gotwebd_env->sockets, entry, tsock) {
436 TAILQ_REMOVE(gotwebd_env->sockets, sock, entry);
442 TAILQ_FOREACH_SAFE(srv, gotwebd_env->servers, entry, tsrv)
445 free(gotwebd_env->sockets);
446 free(gotwebd_env->servers);
451 sockets_privinit(struct gotwebd *env, struct socket *sock)
453 struct privsep *ps = env->gotwebd_ps;
455 if (sock->conf.type == UNIX) {
456 log_debug("%s: initializing unix socket %s", __func__,
457 sock->conf.unix_socket_name);
458 sock->fd = sockets_unix_socket_listen(ps, sock);
459 if (sock->fd == -1) {
460 log_warnx("%s: create unix socket failed", __func__);
465 if (sock->conf.type == FCGI) {
466 log_debug("%s: initializing fcgi socket for %s", __func__,
468 sock->fd = sockets_create_socket(sock->conf.al,
469 sock->conf.fcgi_socket_port);
470 if (sock->fd == -1) {
471 log_warnx("%s: create unix socket failed", __func__);
480 sockets_unix_socket_listen(struct privsep *ps, struct socket *sock)
482 struct gotwebd *env = ps->ps_env;
483 struct sockaddr_un sun;
484 struct socket *tsock;
486 mode_t old_umask, mode;
488 TAILQ_FOREACH(tsock, env->sockets, entry) {
489 if (strcmp(tsock->conf.unix_socket_name,
490 sock->conf.unix_socket_name) == 0 &&
495 /* TA: FIXME: this needs upstreaming. */
496 int socket_flags = SOCK_STREAM | SOCK_NONBLOCK;
498 socket_flags |= SOCK_CLOEXEC;
500 u_fd = socket(AF_UNIX, socket_flags, 0);
502 log_warn("%s: socket", __func__);
506 sun.sun_family = AF_UNIX;
507 if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
508 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
509 log_warn("%s: %s name too long", __func__,
510 sock->conf.unix_socket_name);
515 if (unlink(sock->conf.unix_socket_name) == -1) {
516 if (errno != ENOENT) {
517 log_warn("%s: unlink %s", __func__,
518 sock->conf.unix_socket_name);
524 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
525 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
527 if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
528 log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
530 (void)umask(old_umask);
534 (void)umask(old_umask);
536 if (chmod(sock->conf.unix_socket_name, mode) == -1) {
537 log_warn("%s: chmod", __func__);
539 (void)unlink(sock->conf.unix_socket_name);
543 if (chown(sock->conf.unix_socket_name, ps->ps_pw->pw_uid,
544 ps->ps_pw->pw_gid) == -1) {
545 log_warn("%s: chown", __func__);
547 (void)unlink(sock->conf.unix_socket_name);
551 if (listen(u_fd, SOCKS_BACKLOG) == -1) {
552 log_warn("%s: listen", __func__);
560 sockets_create_socket(struct addresslist *al, in_port_t port)
562 struct addrinfo hints;
564 int fd = -1, o_val = 1, flags;
566 memset(&hints, 0, sizeof(hints));
567 hints.ai_family = AF_UNSPEC;
568 hints.ai_socktype = SOCK_STREAM;
569 hints.ai_flags |= AI_PASSIVE;
571 TAILQ_FOREACH(a, al, entry) {
572 if (sockets_socket_af(&a->ss, port) == -1) {
573 log_warnx("%s: sockets_socket_af", __func__);
577 fd = socket(a->ss.ss_family, hints.ai_socktype,
579 log_debug("%s: opening socket (%d) for %s", __func__,
582 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
583 sizeof(int)) == -1) {
584 log_warn("%s: setsockopt error", __func__);
589 flags = fcntl(fd, F_GETFL);
591 fcntl(fd, F_SETFL, flags);
593 /* TA: 'sizeof a' vs a->ss.len */
594 if (bind(fd, (struct sockaddr *)&a->ss, sizeof a) == -1) {
596 log_info("%s: can't bind to port %d", __func__,
601 if (listen(fd, SOMAXCONN) == -1) {
602 log_warn("%s, unable to listen on socket", __func__);
615 sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
616 int reserve, volatile int *counter)
620 if (getdtablecount() + reserve +
621 ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
622 log_debug("inflight fds exceeded");
626 /* TA: This needs fixing upstream. */
628 ret = accept(sockfd, addr, addrlen);
630 ret = accept4(sockfd, addr, addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC);
635 log_debug("inflight incremented, now %d", *counter);
642 sockets_accept_paused(int fd, short events, void *arg)
644 struct socket *sock = (struct socket *)arg;
646 event_add(&sock->ev, NULL);
650 sockets_socket_accept(int fd, short event, void *arg)
652 struct socket *sock = (struct socket *)arg;
653 struct sockaddr_storage ss;
654 struct timeval backoff;
655 struct request *c = NULL;
662 event_add(&sock->ev, NULL);
663 if (event & EV_TIMEOUT)
668 s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
669 FD_RESERVE, &cgi_inflight);
679 event_del(&sock->ev);
680 evtimer_add(&sock->pause, &backoff);
683 log_warn("%s: accept", __func__);
687 if (client_cnt > GOTWEBD_MAXCLIENTS)
690 c = calloc(1, sizeof(struct request));
692 log_warn("%s", __func__);
700 memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
703 c->request_started = 0;
704 c->sock->client_status = CLIENT_CONNECT;
706 event_set(&c->ev, s, EV_READ, fcgi_request, c);
707 event_add(&c->ev, NULL);
709 evtimer_set(&c->tmo, fcgi_timeout, c);
710 evtimer_add(&c->tmo, &timeout);
723 sockets_rlimit(int maxfd)
727 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
728 fatal("%s: failed to get resource limit", __func__);
729 log_debug("%s: max open files %llu", __func__,
730 (unsigned long long)rl.rlim_max);
733 * Allow the maximum number of open file descriptors for this
734 * login class (which should be the class "daemon" by default).
737 rl.rlim_cur = rl.rlim_max;
739 rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
740 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
741 fatal("%s: failed to set resource limit", __func__);