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_accept_reserve(int, struct sockaddr *, socklen_t *, int,
82 *sockets_conf_new_socket(struct gotwebd *, struct server *, int, int,
87 static struct privsep_proc procs[] = {
88 { "gotwebd", PROC_GOTWEBD, sockets_dispatch_gotwebd },
92 sockets(struct privsep *ps, struct privsep_proc *p)
94 proc_run(ps, p, procs, nitems(procs), sockets_run, NULL);
98 sockets_run(struct privsep *ps, struct privsep_proc *p, void *arg)
100 if (config_init(ps->ps_env) == -1)
101 fatal("failed to initialize configuration");
103 p->p_shutdown = sockets_shutdown;
107 signal_del(&ps->ps_evsigchld);
108 signal_set(&ps->ps_evsigchld, SIGCHLD, sockets_sighdlr, ps);
109 signal_add(&ps->ps_evsigchld, NULL);
112 if (pledge("stdio rpath wpath cpath inet recvfd proc exec sendfd",
119 sockets_parse_sockets(struct gotwebd *env)
122 struct socket *sock, *new_sock = NULL;
124 int sock_id = 0, ipv4 = 0, ipv6 = 0;
126 TAILQ_FOREACH(srv, env->servers, entry) {
127 if (srv->unix_socket) {
129 new_sock = sockets_conf_new_socket(env, srv,
131 TAILQ_INSERT_TAIL(env->sockets, new_sock, entry);
134 if (srv->fcgi_socket) {
136 new_sock = sockets_conf_new_socket(env, srv,
138 TAILQ_INSERT_TAIL(env->sockets, new_sock, entry);
140 /* add ipv6 children */
141 TAILQ_FOREACH(sock, env->sockets, entry) {
144 TAILQ_FOREACH(a, sock->conf.al, entry) {
145 if (a->ss.ss_family == AF_INET)
147 if (a->ss.ss_family == AF_INET6)
151 /* create ipv6 sock */
152 if (ipv4 == 1 && ipv6 == 1) {
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,
168 sockets_dup_new_socket(struct socket *p_sock, struct socket *sock)
170 struct address *a, *acp;
173 sock->conf.parent_id = p_sock->conf.id;
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);
183 free(p_sock->conf.al);
187 fatalx("%s: snprintf", __func__);
190 TAILQ_FOREACH(a, p_sock->conf.al, entry) {
191 if (a->ss.ss_family == AF_INET)
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;
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",
208 TAILQ_INSERT_TAIL(sock->conf.al, acp, entry);
213 sockets_conf_new_socket(struct gotwebd *env, struct server *srv, int id,
214 int type, int is_dup)
217 struct address *a, *acp;
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) {
225 fatalx("%s: calloc", __func__);
227 TAILQ_INIT(sock->conf.al);
229 sock->conf.parent_id = 0;
234 sock->conf.type = type;
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)) {
243 fatalx("%s: strlcpy", __func__);
248 sock->conf.fcgi_socket_port = srv->fcgi_socket_port;
253 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
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)) {
265 fatalx("%s: strlcpy", __func__);
268 TAILQ_FOREACH(a, srv->al, entry) {
269 if ((acp = calloc(1, sizeof(*acp))) == NULL) {
272 fatal("%s: calloc", __func__);
274 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
275 acp->ipproto = a->ipproto;
276 acp->prefixlen = a->prefixlen;
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",
286 TAILQ_INSERT_TAIL(sock->conf.al, acp, entry);
297 TAILQ_FOREACH(sock, gotwebd_env->sockets, entry) {
298 log_debug("%s: configuring socket %d (%d)", __func__,
299 sock->conf.id, sock->fd);
301 event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
302 sockets_socket_accept, sock);
304 if (event_add(&sock->ev, NULL))
305 fatalx("event add sock");
307 evtimer_set(&sock->pause, sockets_accept_paused, sock);
309 log_debug("%s: running socket listener %d", __func__,
315 sockets_purge(struct gotwebd *env)
317 struct socket *sock, *tsock;
319 /* shutdown and remove sockets */
320 TAILQ_FOREACH_SAFE(sock, env->sockets, entry, tsock) {
321 if (event_initialized(&sock->ev))
322 event_del(&sock->ev);
323 if (evtimer_initialized(&sock->evt))
324 evtimer_del(&sock->evt);
325 if (evtimer_initialized(&sock->pause))
326 evtimer_del(&sock->pause);
329 TAILQ_REMOVE(env->sockets, sock, entry);
334 sockets_dispatch_gotwebd(int fd, struct privsep_proc *p, struct imsg *imsg)
336 struct privsep *ps = p->p_ps;
337 int res = 0, cmd = 0, verbose;
339 switch (imsg->hdr.type) {
341 config_getserver(gotwebd_env, imsg);
344 config_getsock(gotwebd_env, imsg);
347 config_getfd(gotwebd_env, imsg);
350 config_getcfg(gotwebd_env, imsg);
355 case IMSG_CTL_VERBOSE:
356 IMSG_SIZE_CHECK(imsg, &verbose);
357 memcpy(&verbose, imsg->data, sizeof(verbose));
358 log_setverbose(verbose);
368 if (proc_compose_imsg(ps, PROC_GOTWEBD, -1, cmd,
369 imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
378 sockets_sighdlr(int sig, short event, void *arg)
382 log_info("%s: ignoring SIGHUP", __func__);
385 log_info("%s: ignoring SIGPIPE", __func__);
388 log_info("%s: ignoring SIGUSR1", __func__);
393 log_info("SIGNAL: %d", sig);
394 fatalx("unexpected signal");
399 sockets_shutdown(void)
401 struct server *srv, *tsrv;
402 struct socket *sock, *tsock;
404 sockets_purge(gotwebd_env);
407 TAILQ_FOREACH_SAFE(sock, gotwebd_env->sockets, entry, tsock) {
408 TAILQ_REMOVE(gotwebd_env->sockets, sock, entry);
414 TAILQ_FOREACH_SAFE(srv, gotwebd_env->servers, entry, tsrv)
417 free(gotwebd_env->sockets);
418 free(gotwebd_env->servers);
423 sockets_privinit(struct gotwebd *env, struct socket *sock)
425 struct privsep *ps = env->gotwebd_ps;
427 if (sock->conf.type == UNIX) {
428 log_debug("%s: initializing unix socket %s", __func__,
429 sock->conf.unix_socket_name);
430 sock->fd = sockets_unix_socket_listen(ps, sock);
431 if (sock->fd == -1) {
432 log_warnx("%s: create unix socket failed", __func__);
437 if (sock->conf.type == FCGI) {
438 log_debug("%s: initializing fcgi socket for %s", __func__,
440 sock->fd = sockets_create_socket(sock->conf.al,
441 sock->conf.fcgi_socket_port);
442 if (sock->fd == -1) {
443 log_warnx("%s: create unix socket failed", __func__);
452 sockets_unix_socket_listen(struct privsep *ps, struct socket *sock)
454 struct gotwebd *env = ps->ps_env;
455 struct sockaddr_un sun;
456 struct socket *tsock;
458 mode_t old_umask, mode;
460 TAILQ_FOREACH(tsock, env->sockets, entry) {
461 if (strcmp(tsock->conf.unix_socket_name,
462 sock->conf.unix_socket_name) == 0 &&
467 /* TA: FIXME: this needs upstreaming. */
468 int socket_flags = SOCK_STREAM | SOCK_NONBLOCK;
470 socket_flags |= SOCK_CLOEXEC;
472 u_fd = socket(AF_UNIX, socket_flags, 0);
474 log_warn("%s: socket", __func__);
478 sun.sun_family = AF_UNIX;
479 if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
480 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
481 log_warn("%s: %s name too long", __func__,
482 sock->conf.unix_socket_name);
487 if (unlink(sock->conf.unix_socket_name) == -1) {
488 if (errno != ENOENT) {
489 log_warn("%s: unlink %s", __func__,
490 sock->conf.unix_socket_name);
496 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
497 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
499 if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
500 log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
502 (void)umask(old_umask);
506 (void)umask(old_umask);
508 if (chmod(sock->conf.unix_socket_name, mode) == -1) {
509 log_warn("%s: chmod", __func__);
511 (void)unlink(sock->conf.unix_socket_name);
515 if (chown(sock->conf.unix_socket_name, ps->ps_pw->pw_uid,
516 ps->ps_pw->pw_gid) == -1) {
517 log_warn("%s: chown", __func__);
519 (void)unlink(sock->conf.unix_socket_name);
523 if (listen(u_fd, SOCKS_BACKLOG) == -1) {
524 log_warn("%s: listen", __func__);
532 sockets_create_socket(struct addresslist *al, in_port_t port)
534 struct addrinfo hints;
536 int fd = -1, o_val = 1, flags;
538 memset(&hints, 0, sizeof(hints));
539 hints.ai_family = AF_UNSPEC;
540 hints.ai_socktype = SOCK_STREAM;
541 hints.ai_flags |= AI_PASSIVE;
543 TAILQ_FOREACH(a, al, entry) {
544 switch (a->ss.ss_family) {
546 ((struct sockaddr_in *)(&a->ss))->sin_port = port;
549 ((struct sockaddr_in6 *)(&a->ss))->sin6_port = port;
552 log_warnx("%s: unknown address family", __func__);
556 fd = socket(a->ss.ss_family, hints.ai_socktype,
558 log_debug("%s: opening socket (%d) for %s", __func__,
561 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
562 sizeof(int)) == -1) {
563 log_warn("%s: setsockopt error", __func__);
568 flags = fcntl(fd, F_GETFL);
570 fcntl(fd, F_SETFL, flags);
572 /* TA: 'sizeof a' vs a->ss.len */
573 if (bind(fd, (struct sockaddr *)&a->ss, sizeof a) == -1) {
575 log_info("%s: can't bind to port %d", __func__,
580 if (listen(fd, SOMAXCONN) == -1) {
581 log_warn("%s, unable to listen on socket", __func__);
594 sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
595 int reserve, volatile int *counter)
599 if (getdtablecount() + reserve +
600 ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
601 log_debug("inflight fds exceeded");
605 /* TA: This needs fixing upstream. */
607 ret = accept(sockfd, addr, addrlen);
609 ret = accept4(sockfd, addr, addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC);
614 log_debug("inflight incremented, now %d", *counter);
621 sockets_accept_paused(int fd, short events, void *arg)
623 struct socket *sock = (struct socket *)arg;
625 event_add(&sock->ev, NULL);
629 sockets_socket_accept(int fd, short event, void *arg)
631 struct socket *sock = (struct socket *)arg;
632 struct sockaddr_storage ss;
633 struct timeval backoff;
634 struct request *c = NULL;
641 event_add(&sock->ev, NULL);
642 if (event & EV_TIMEOUT)
647 s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
648 FD_RESERVE, &cgi_inflight);
658 event_del(&sock->ev);
659 evtimer_add(&sock->pause, &backoff);
662 log_warn("%s: accept", __func__);
666 if (client_cnt > GOTWEBD_MAXCLIENTS)
669 c = calloc(1, sizeof(struct request));
671 log_warn("%s", __func__);
679 memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
682 c->request_started = 0;
683 c->sock->client_status = CLIENT_CONNECT;
685 event_set(&c->ev, s, EV_READ, fcgi_request, c);
686 event_add(&c->ev, NULL);
688 evtimer_set(&c->tmo, fcgi_timeout, c);
689 evtimer_add(&c->tmo, &timeout);
702 sockets_rlimit(int maxfd)
706 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
707 fatal("%s: failed to get resource limit", __func__);
708 log_debug("%s: max open files %llu", __func__,
709 (unsigned long long)rl.rlim_max);
712 * Allow the maximum number of open file descriptors for this
713 * login class (which should be the class "daemon" by default).
716 rl.rlim_cur = rl.rlim_max;
718 rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
719 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
720 fatal("%s: failed to set resource limit", __func__);