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>
52 #include "got_error.h"
53 #include "got_opentemp.h"
54 #include "got_repository.h"
59 #define SOCKS_BACKLOG 5
60 #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
63 volatile int client_cnt;
65 static struct timeval timeout = { TIMEOUT_DEFAULT, 0 };
67 static void sockets_sighdlr(int, short, void *);
68 static void sockets_run(struct privsep *, struct privsep_proc *, void *);
69 static void sockets_launch(void);
70 static void sockets_purge(struct gotwebd *);
71 static void sockets_accept_paused(int, short, void *);
72 static void sockets_rlimit(int);
74 static int sockets_dispatch_gotwebd(int, struct privsep_proc *,
76 static int sockets_unix_socket_listen(struct privsep *, struct socket *);
77 static int sockets_create_socket(struct address *, in_port_t);
78 static int sockets_accept_reserve(int, struct sockaddr *, socklen_t *,
81 static struct socket *sockets_conf_new_socket_unix(struct gotwebd *,
82 struct server *, int);
83 static struct socket *sockets_conf_new_socket_fcgi(struct gotwebd *,
84 struct server *, int, struct address *);
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)
124 struct socket *new_sock = NULL;
127 TAILQ_FOREACH(srv, &env->servers, entry) {
128 if (srv->unix_socket) {
129 new_sock = sockets_conf_new_socket_unix(env, srv,
133 TAILQ_INSERT_TAIL(&env->sockets, new_sock,
138 if (srv->fcgi_socket) {
139 if (TAILQ_EMPTY(&srv->al)) {
140 fatalx("%s: server %s has no IP addresses to "
141 "listen for FCGI connections", __func__,
144 TAILQ_FOREACH(a, &srv->al, entry) {
145 if (a->ss.ss_family != AF_INET &&
146 a->ss.ss_family != AF_INET6)
148 new_sock = sockets_conf_new_socket_fcgi(env,
152 TAILQ_INSERT_TAIL(&env->sockets,
160 static struct socket *
161 sockets_conf_new_socket_unix(struct gotwebd *env, struct server *srv, int id)
166 if ((sock = calloc(1, sizeof(*sock))) == NULL)
167 fatalx("%s: calloc", __func__);
171 sock->conf.af_type = AF_UNIX;
173 if (strlcpy(sock->conf.unix_socket_name,
174 srv->unix_socket_name,
175 sizeof(sock->conf.unix_socket_name)) >=
176 sizeof(sock->conf.unix_socket_name)) {
178 fatalx("%s: strlcpy", __func__);
181 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
183 if (n < 0 || (size_t)n >= GOTWEBD_MAXTEXT) {
185 fatalx("%s: snprintf", __func__);
188 if (strlcpy(sock->conf.srv_name, srv->name,
189 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
191 fatalx("%s: strlcpy", __func__);
197 static struct socket *
198 sockets_conf_new_socket_fcgi(struct gotwebd *env, struct server *srv, int id,
205 if ((sock = calloc(1, sizeof(*sock))) == NULL)
206 fatalx("%s: calloc", __func__);
210 sock->conf.af_type = a->ss.ss_family;
212 sock->conf.fcgi_socket_port = a->port;
214 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
216 if (n < 0 || (size_t)n >= GOTWEBD_MAXTEXT) {
218 fatalx("%s: snprintf", __func__);
221 if (strlcpy(sock->conf.srv_name, srv->name,
222 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
224 fatalx("%s: strlcpy", __func__);
227 acp = &sock->conf.addr;
229 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
230 acp->ipproto = a->ipproto;
231 acp->prefixlen = a->prefixlen;
233 if (strlen(a->ifname) != 0) {
234 if (strlcpy(acp->ifname, a->ifname,
235 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
236 fatalx("%s: interface name truncated",
249 TAILQ_FOREACH(sock, &gotwebd_env->sockets, entry) {
250 log_debug("%s: configuring socket %d (%d)", __func__,
251 sock->conf.id, sock->fd);
253 event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
254 sockets_socket_accept, sock);
256 if (event_add(&sock->ev, NULL))
257 fatalx("event add sock");
259 evtimer_set(&sock->pause, sockets_accept_paused, sock);
261 log_debug("%s: running socket listener %d", __func__,
267 sockets_purge(struct gotwebd *env)
269 struct socket *sock, *tsock;
271 /* shutdown and remove sockets */
272 TAILQ_FOREACH_SAFE(sock, &env->sockets, entry, tsock) {
273 if (event_initialized(&sock->ev))
274 event_del(&sock->ev);
275 if (evtimer_initialized(&sock->evt))
276 evtimer_del(&sock->evt);
277 if (evtimer_initialized(&sock->pause))
278 evtimer_del(&sock->pause);
281 TAILQ_REMOVE(&env->sockets, sock, entry);
286 sockets_dispatch_gotwebd(int fd, struct privsep_proc *p, struct imsg *imsg)
288 struct privsep *ps = p->p_ps;
289 int res = 0, cmd = 0, verbose;
291 switch (imsg->hdr.type) {
293 config_getserver(gotwebd_env, imsg);
296 config_getsock(gotwebd_env, imsg);
299 config_getfd(gotwebd_env, imsg);
302 config_getcfg(gotwebd_env, imsg);
307 case IMSG_CTL_VERBOSE:
308 IMSG_SIZE_CHECK(imsg, &verbose);
309 memcpy(&verbose, imsg->data, sizeof(verbose));
310 log_setverbose(verbose);
320 if (proc_compose_imsg(ps, PROC_GOTWEBD, -1, cmd,
321 imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
330 sockets_sighdlr(int sig, short event, void *arg)
334 log_info("%s: ignoring SIGHUP", __func__);
337 log_info("%s: ignoring SIGPIPE", __func__);
340 log_info("%s: ignoring SIGUSR1", __func__);
345 log_info("SIGNAL: %d", sig);
346 fatalx("unexpected signal");
351 sockets_shutdown(void)
353 struct server *srv, *tsrv;
354 struct socket *sock, *tsock;
357 sockets_purge(gotwebd_env);
360 TAILQ_FOREACH_SAFE(sock, &gotwebd_env->sockets, entry, tsock) {
361 TAILQ_REMOVE(&gotwebd_env->sockets, sock, entry);
367 TAILQ_FOREACH_SAFE(srv, &gotwebd_env->servers, entry, tsrv) {
368 for (i = 0; i < srv->ncached_repos; i++)
369 got_repo_close(srv->cached_repos[i].repo);
377 sockets_privinit(struct gotwebd *env, struct socket *sock)
379 struct privsep *ps = env->gotwebd_ps;
381 if (sock->conf.af_type == AF_UNIX) {
382 log_debug("%s: initializing unix socket %s", __func__,
383 sock->conf.unix_socket_name);
384 sock->fd = sockets_unix_socket_listen(ps, sock);
385 if (sock->fd == -1) {
386 log_warnx("%s: create unix socket failed", __func__);
391 if (sock->conf.af_type == AF_INET || sock->conf.af_type == AF_INET6) {
392 log_debug("%s: initializing %s FCGI socket on port %d for %s",
393 __func__, sock->conf.af_type == AF_INET ? "inet" : "inet6",
394 sock->conf.fcgi_socket_port, sock->conf.name);
395 sock->fd = sockets_create_socket(&sock->conf.addr,
396 sock->conf.fcgi_socket_port);
397 if (sock->fd == -1) {
398 log_warnx("%s: create FCGI socket failed", __func__);
407 sockets_unix_socket_listen(struct privsep *ps, struct socket *sock)
409 struct gotwebd *env = ps->ps_env;
410 struct sockaddr_un sun;
411 struct socket *tsock;
413 mode_t old_umask, mode;
415 TAILQ_FOREACH(tsock, &env->sockets, entry) {
416 if (strcmp(tsock->conf.unix_socket_name,
417 sock->conf.unix_socket_name) == 0 &&
422 u_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
424 log_warn("%s: socket", __func__);
428 sun.sun_family = AF_UNIX;
429 if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
430 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
431 log_warn("%s: %s name too long", __func__,
432 sock->conf.unix_socket_name);
437 if (unlink(sock->conf.unix_socket_name) == -1) {
438 if (errno != ENOENT) {
439 log_warn("%s: unlink %s", __func__,
440 sock->conf.unix_socket_name);
446 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
447 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
449 if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
450 log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
452 (void)umask(old_umask);
456 (void)umask(old_umask);
458 if (chmod(sock->conf.unix_socket_name, mode) == -1) {
459 log_warn("%s: chmod", __func__);
461 (void)unlink(sock->conf.unix_socket_name);
465 if (chown(sock->conf.unix_socket_name, ps->ps_pw->pw_uid,
466 ps->ps_pw->pw_gid) == -1) {
467 log_warn("%s: chown", __func__);
469 (void)unlink(sock->conf.unix_socket_name);
473 if (listen(u_fd, SOCKS_BACKLOG) == -1) {
474 log_warn("%s: listen", __func__);
482 sockets_create_socket(struct address *a, in_port_t port)
484 struct addrinfo hints;
485 int fd = -1, o_val = 1, flags;
487 memset(&hints, 0, sizeof(hints));
488 hints.ai_family = AF_UNSPEC;
489 hints.ai_socktype = SOCK_STREAM;
490 hints.ai_flags |= AI_PASSIVE;
492 switch (a->ss.ss_family) {
494 ((struct sockaddr_in *)(&a->ss))->sin_port = htons(port);
497 ((struct sockaddr_in6 *)(&a->ss))->sin6_port = htons(port);
500 log_warnx("%s: unknown address family", __func__);
504 fd = socket(a->ss.ss_family, hints.ai_socktype, a->ipproto);
508 log_debug("%s: opened socket (%d) for %s", __func__,
511 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
512 sizeof(int)) == -1) {
513 log_warn("%s: setsockopt error", __func__);
519 flags = fcntl(fd, F_GETFL);
521 if (fcntl(fd, F_SETFL, flags) == -1) {
522 log_info("%s: could not enable non-blocking I/O", __func__);
527 if (bind(fd, (struct sockaddr *)&a->ss, a->ss.ss_len) == -1) {
529 log_info("%s: can't bind to port %d", __func__,
534 if (listen(fd, SOMAXCONN) == -1) {
535 log_warn("%s, unable to listen on socket", __func__);
544 sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
545 int reserve, volatile int *counter)
549 if (getdtablecount() + reserve +
550 ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
551 log_debug("inflight fds exceeded");
556 if ((ret = accept4(sockfd, addr, addrlen,
557 SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
559 log_debug("inflight incremented, now %d", *counter);
566 sockets_accept_paused(int fd, short events, void *arg)
568 struct socket *sock = (struct socket *)arg;
570 event_add(&sock->ev, NULL);
574 sockets_socket_accept(int fd, short event, void *arg)
576 struct socket *sock = (struct socket *)arg;
577 struct sockaddr_storage ss;
578 struct timeval backoff;
579 struct request *c = NULL;
586 event_add(&sock->ev, NULL);
587 if (event & EV_TIMEOUT)
592 s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
593 FD_RESERVE, &cgi_inflight);
603 event_del(&sock->ev);
604 evtimer_add(&sock->pause, &backoff);
607 log_warn("%s: accept", __func__);
611 if (client_cnt > GOTWEBD_MAXCLIENTS)
614 c = calloc(1, sizeof(struct request));
616 log_warn("%s", __func__);
624 memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
627 c->request_started = 0;
628 c->sock->client_status = CLIENT_CONNECT;
630 event_set(&c->ev, s, EV_READ, fcgi_request, c);
631 event_add(&c->ev, NULL);
633 evtimer_set(&c->tmo, fcgi_timeout, c);
634 evtimer_add(&c->tmo, &timeout);
647 sockets_rlimit(int maxfd)
651 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
652 fatal("%s: failed to get resource limit", __func__);
653 log_debug("%s: max open files %llu", __func__,
654 (unsigned long long)rl.rlim_max);
657 * Allow the maximum number of open file descriptors for this
658 * login class (which should be the class "daemon" by default).
661 rl.rlim_cur = rl.rlim_max;
663 rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
664 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
665 fatal("%s: failed to set resource limit", __func__);