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>
24 #include <sys/resource.h>
25 #include <sys/socket.h>
28 #include <sys/types.h>
33 #include <netinet/in.h>
49 #include "got_error.h"
50 #include "got_opentemp.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,
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);
293 sockets_socket_af(struct sockaddr_storage *ss, in_port_t port)
295 switch (ss->ss_family) {
297 ((struct sockaddr_in *)ss)->sin_port = port;
300 ((struct sockaddr_in *)ss)->sin_len =
301 sizeof(struct sockaddr_in);
305 ((struct sockaddr_in6 *)ss)->sin6_port = port;
308 ((struct sockaddr_in6 *)ss)->sin6_len =
309 sizeof(struct sockaddr_in6);
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__,
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);
356 TAILQ_REMOVE(env->sockets, sock, entry);
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) {
368 config_getserver(gotwebd_env, imsg);
371 config_getsock(gotwebd_env, imsg);
374 config_getfd(gotwebd_env, imsg);
377 config_getcfg(gotwebd_env, imsg);
382 case IMSG_CTL_VERBOSE:
383 IMSG_SIZE_CHECK(imsg, &verbose);
384 memcpy(&verbose, imsg->data, sizeof(verbose));
385 log_setverbose(verbose);
395 if (proc_compose_imsg(ps, PROC_GOTWEBD, -1, cmd,
396 imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
405 sockets_sighdlr(int sig, short event, void *arg)
409 log_info("%s: ignoring SIGHUP", __func__);
412 log_info("%s: ignoring SIGPIPE", __func__);
415 log_info("%s: ignoring SIGUSR1", __func__);
420 log_info("SIGNAL: %d", sig);
421 fatalx("unexpected signal");
426 sockets_shutdown(void)
428 struct server *srv, *tsrv;
429 struct socket *sock, *tsock;
431 sockets_purge(gotwebd_env);
434 TAILQ_FOREACH_SAFE(sock, gotwebd_env->sockets, entry, tsock) {
435 TAILQ_REMOVE(gotwebd_env->sockets, sock, entry);
441 TAILQ_FOREACH_SAFE(srv, gotwebd_env->servers, entry, tsrv)
444 free(gotwebd_env->sockets);
445 free(gotwebd_env->servers);
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__);
464 if (sock->conf.type == FCGI) {
465 log_debug("%s: initializing fcgi socket for %s", __func__,
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__);
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;
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 &&
494 u_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
496 log_warn("%s: socket", __func__);
500 sun.sun_family = AF_UNIX;
501 if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
502 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
503 log_warn("%s: %s name too long", __func__,
504 sock->conf.unix_socket_name);
509 if (unlink(sock->conf.unix_socket_name) == -1) {
510 if (errno != ENOENT) {
511 log_warn("%s: unlink %s", __func__,
512 sock->conf.unix_socket_name);
518 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
519 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
521 if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
522 log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
524 (void)umask(old_umask);
528 (void)umask(old_umask);
530 if (chmod(sock->conf.unix_socket_name, mode) == -1) {
531 log_warn("%s: chmod", __func__);
533 (void)unlink(sock->conf.unix_socket_name);
537 if (chown(sock->conf.unix_socket_name, ps->ps_pw->pw_uid,
538 ps->ps_pw->pw_gid) == -1) {
539 log_warn("%s: chown", __func__);
541 (void)unlink(sock->conf.unix_socket_name);
545 if (listen(u_fd, SOCKS_BACKLOG) == -1) {
546 log_warn("%s: listen", __func__);
554 sockets_create_socket(struct addresslist *al, in_port_t port)
556 struct addrinfo hints;
558 int fd = -1, o_val = 1, flags;
560 memset(&hints, 0, sizeof(hints));
561 hints.ai_family = AF_UNSPEC;
562 hints.ai_socktype = SOCK_STREAM;
563 hints.ai_flags |= AI_PASSIVE;
565 TAILQ_FOREACH(a, al, entry) {
566 if (sockets_socket_af(&a->ss, port) == -1) {
567 log_warnx("%s: sockets_socket_af", __func__);
571 fd = socket(a->ss.ss_family, hints.ai_socktype,
573 log_debug("%s: opening socket (%d) for %s", __func__,
576 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
577 sizeof(int)) == -1) {
578 log_warn("%s: setsockopt error", __func__);
583 flags = fcntl(fd, F_GETFL);
585 fcntl(fd, F_SETFL, flags);
587 /* TA: 'sizeof a' vs a->ss.len */
588 if (bind(fd, (struct sockaddr *)&a->ss, sizeof a) == -1) {
590 log_info("%s: can't bind to port %d", __func__,
595 if (listen(fd, SOMAXCONN) == -1) {
596 log_warn("%s, unable to listen on socket", __func__);
609 sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
610 int reserve, volatile int *counter)
614 if (getdtablecount() + reserve +
615 ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
616 log_debug("inflight fds exceeded");
621 if ((ret = accept4(sockfd, addr, addrlen,
622 SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
624 log_debug("inflight incremented, now %d", *counter);
631 sockets_accept_paused(int fd, short events, void *arg)
633 struct socket *sock = (struct socket *)arg;
635 event_add(&sock->ev, NULL);
639 sockets_socket_accept(int fd, short event, void *arg)
641 struct socket *sock = (struct socket *)arg;
642 struct sockaddr_storage ss;
643 struct timeval backoff;
644 struct request *c = NULL;
651 event_add(&sock->ev, NULL);
652 if (event & EV_TIMEOUT)
657 s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
658 FD_RESERVE, &cgi_inflight);
668 event_del(&sock->ev);
669 evtimer_add(&sock->pause, &backoff);
672 log_warn("%s: accept", __func__);
676 if (client_cnt > GOTWEBD_MAXCLIENTS)
679 c = calloc(1, sizeof(struct request));
681 log_warn("%s", __func__);
689 memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
692 c->request_started = 0;
693 c->sock->client_status = CLIENT_CONNECT;
695 event_set(&c->ev, s, EV_READ, fcgi_request, c);
696 event_add(&c->ev, NULL);
698 evtimer_set(&c->tmo, fcgi_timeout, c);
699 evtimer_add(&c->tmo, &timeout);
712 sockets_rlimit(int maxfd)
716 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
717 fatal("%s: failed to get resource limit", __func__);
718 log_debug("%s: max open files %llu", __func__, rl.rlim_max);
721 * Allow the maximum number of open file descriptors for this
722 * login class (which should be the class "daemon" by default).
725 rl.rlim_cur = rl.rlim_max;
727 rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
728 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
729 fatal("%s: failed to set resource limit", __func__);