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 "got_compat.h"
22 #include <sys/param.h>
23 #include <sys/ioctl.h>
24 #include <sys/queue.h>
27 #include <sys/resource.h>
28 #include <sys/socket.h>
31 #include <sys/types.h>
36 #include <netinet/in.h>
52 #include "got_error.h"
53 #include "got_opentemp.h"
54 #include "got_reference.h"
55 #include "got_repository.h"
56 #include "got_privsep.h"
61 #define SOCKS_BACKLOG 5
62 #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
64 volatile int client_cnt;
66 static struct timeval timeout = { TIMEOUT_DEFAULT, 0 };
68 static void sockets_sighdlr(int, short, void *);
69 static void sockets_shutdown(void);
70 static void sockets_launch(void);
71 static void sockets_purge(struct gotwebd *);
72 static void sockets_accept_paused(int, short, void *);
73 static void sockets_rlimit(int);
75 static void sockets_dispatch_main(int, short, void *);
76 static int sockets_unix_socket_listen(struct gotwebd *, struct socket *);
77 static int sockets_create_socket(struct address *);
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 *);
89 sockets(struct gotwebd *env, int fd)
91 struct event sighup, sigpipe, sigusr1, sigchld;
97 if (config_init(env) == -1)
98 fatal("failed to initialize configuration");
100 if ((env->iev_parent = malloc(sizeof(*env->iev_parent))) == NULL)
102 imsg_init(&env->iev_parent->ibuf, fd);
103 env->iev_parent->handler = sockets_dispatch_main;
104 env->iev_parent->data = env->iev_parent;
105 event_set(&env->iev_parent->ev, fd, EV_READ, sockets_dispatch_main,
107 event_add(&env->iev_parent->ev, NULL);
109 signal(SIGPIPE, SIG_IGN);
111 signal_set(&sighup, SIGCHLD, sockets_sighdlr, env);
112 signal_add(&sighup, NULL);
113 signal_set(&sigpipe, SIGCHLD, sockets_sighdlr, env);
114 signal_add(&sigpipe, NULL);
115 signal_set(&sigusr1, SIGCHLD, sockets_sighdlr, env);
116 signal_add(&sigusr1, NULL);
117 signal_set(&sigchld, SIGCHLD, sockets_sighdlr, env);
118 signal_add(&sigchld, NULL);
121 if (pledge("stdio rpath inet recvfd proc exec sendfd unveil",
131 sockets_parse_sockets(struct gotwebd *env)
135 struct socket *new_sock = NULL;
138 TAILQ_FOREACH(srv, &env->servers, entry) {
139 if (srv->unix_socket) {
140 new_sock = sockets_conf_new_socket_unix(env, srv,
144 TAILQ_INSERT_TAIL(&env->sockets, new_sock,
149 if (srv->fcgi_socket) {
150 if (TAILQ_EMPTY(&srv->al)) {
151 fatalx("%s: server %s has no IP addresses to "
152 "listen for FCGI connections", __func__,
155 TAILQ_FOREACH(a, &srv->al, entry) {
156 if (a->ss.ss_family != AF_INET &&
157 a->ss.ss_family != AF_INET6)
159 new_sock = sockets_conf_new_socket_fcgi(env,
163 TAILQ_INSERT_TAIL(&env->sockets,
171 static struct socket *
172 sockets_conf_new_socket_unix(struct gotwebd *env, struct server *srv, int id)
177 if ((sock = calloc(1, sizeof(*sock))) == NULL)
178 fatalx("%s: calloc", __func__);
182 sock->conf.af_type = AF_UNIX;
184 if (strlcpy(sock->conf.unix_socket_name,
185 srv->unix_socket_name,
186 sizeof(sock->conf.unix_socket_name)) >=
187 sizeof(sock->conf.unix_socket_name)) {
189 fatalx("%s: strlcpy", __func__);
192 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
194 if (n < 0 || (size_t)n >= GOTWEBD_MAXTEXT) {
196 fatalx("%s: snprintf", __func__);
199 if (strlcpy(sock->conf.srv_name, srv->name,
200 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
202 fatalx("%s: strlcpy", __func__);
208 static struct socket *
209 sockets_conf_new_socket_fcgi(struct gotwebd *env, struct server *srv, int id,
216 if ((sock = calloc(1, sizeof(*sock))) == NULL)
217 fatalx("%s: calloc", __func__);
221 sock->conf.af_type = a->ss.ss_family;
223 sock->conf.fcgi_socket_port = a->port;
225 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
227 if (n < 0 || (size_t)n >= GOTWEBD_MAXTEXT) {
229 fatalx("%s: snprintf", __func__);
232 if (strlcpy(sock->conf.srv_name, srv->name,
233 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
235 fatalx("%s: strlcpy", __func__);
238 acp = &sock->conf.addr;
240 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
242 acp->ai_family = a->ai_family;
243 acp->ai_socktype = a->ai_socktype;
244 acp->ai_protocol = a->ai_protocol;
246 if (*a->ifname != '\0') {
247 if (strlcpy(acp->ifname, a->ifname,
248 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
249 fatalx("%s: interface name truncated",
262 const struct got_error *error;
264 TAILQ_FOREACH(sock, &gotwebd_env->sockets, entry) {
265 log_debug("%s: configuring socket %d (%d)", __func__,
266 sock->conf.id, sock->fd);
268 event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
269 sockets_socket_accept, sock);
271 if (event_add(&sock->ev, NULL))
272 fatalx("event add sock");
274 evtimer_set(&sock->pause, sockets_accept_paused, sock);
276 log_debug("%s: running socket listener %d", __func__,
280 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry) {
281 if (unveil(srv->repos_path, "r") == -1)
282 fatal("unveil %s", srv->repos_path);
285 error = got_privsep_unveil_exec_helpers();
287 fatal("%s", error->msg);
289 if (unveil(NULL, NULL) == -1)
294 sockets_purge(struct gotwebd *env)
296 struct socket *sock, *tsock;
298 /* shutdown and remove sockets */
299 TAILQ_FOREACH_SAFE(sock, &env->sockets, entry, tsock) {
300 if (event_initialized(&sock->ev))
301 event_del(&sock->ev);
302 if (evtimer_initialized(&sock->evt))
303 evtimer_del(&sock->evt);
304 if (evtimer_initialized(&sock->pause))
305 evtimer_del(&sock->pause);
308 TAILQ_REMOVE(&env->sockets, sock, entry);
313 sockets_dispatch_main(int fd, short event, void *arg)
315 struct imsgev *iev = arg;
316 struct imsgbuf *ibuf;
318 struct gotwebd *env = gotwebd_env;
324 if (event & EV_READ) {
325 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
326 fatal("imsg_read error");
327 if (n == 0) /* Connection closed */
330 if (event & EV_WRITE) {
331 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
332 fatal("msgbuf_write");
333 if (n == 0) /* Connection closed */
338 if ((n = imsg_get(ibuf, &imsg)) == -1)
340 if (n == 0) /* No more messages. */
343 switch (imsg.hdr.type) {
345 config_getserver(env, &imsg);
348 config_getsock(env, &imsg);
351 config_getfd(env, &imsg);
354 config_getcfg(env, &imsg);
360 fatalx("%s: unknown imsg type %d", __func__,
370 /* This pipe is dead. Remove its event handler */
372 event_loopexit(NULL);
377 sockets_sighdlr(int sig, short event, void *arg)
381 log_info("%s: ignoring SIGHUP", __func__);
384 log_info("%s: ignoring SIGPIPE", __func__);
387 log_info("%s: ignoring SIGUSR1", __func__);
392 log_info("SIGNAL: %d", sig);
393 fatalx("unexpected signal");
398 sockets_shutdown(void)
400 struct server *srv, *tsrv;
401 struct socket *sock, *tsock;
403 sockets_purge(gotwebd_env);
406 TAILQ_FOREACH_SAFE(sock, &gotwebd_env->sockets, entry, tsock) {
407 TAILQ_REMOVE(&gotwebd_env->sockets, sock, entry);
413 TAILQ_FOREACH_SAFE(srv, &gotwebd_env->servers, entry, tsrv)
420 sockets_privinit(struct gotwebd *env, struct socket *sock)
422 if (sock->conf.af_type == AF_UNIX) {
423 log_debug("%s: initializing unix socket %s", __func__,
424 sock->conf.unix_socket_name);
425 sock->fd = sockets_unix_socket_listen(env, sock);
426 if (sock->fd == -1) {
427 log_warnx("%s: create unix socket failed", __func__);
432 if (sock->conf.af_type == AF_INET || sock->conf.af_type == AF_INET6) {
433 log_debug("%s: initializing %s FCGI socket on port %d for %s",
434 __func__, sock->conf.af_type == AF_INET ? "inet" : "inet6",
435 sock->conf.fcgi_socket_port, sock->conf.name);
436 sock->fd = sockets_create_socket(&sock->conf.addr);
437 if (sock->fd == -1) {
438 log_warnx("%s: create FCGI socket failed", __func__);
447 sockets_unix_socket_listen(struct gotwebd *env, struct socket *sock)
449 struct sockaddr_un sun;
450 struct socket *tsock;
452 mode_t old_umask, mode;
454 TAILQ_FOREACH(tsock, &env->sockets, entry) {
455 if (strcmp(tsock->conf.unix_socket_name,
456 sock->conf.unix_socket_name) == 0 &&
461 /* TA: FIXME: this needs upstreaming. */
462 int socket_flags = SOCK_STREAM | SOCK_NONBLOCK;
464 socket_flags |= SOCK_CLOEXEC;
466 u_fd = socket(AF_UNIX, socket_flags, 0);
468 log_warn("%s: socket", __func__);
472 sun.sun_family = AF_UNIX;
473 if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
474 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
475 log_warn("%s: %s name too long", __func__,
476 sock->conf.unix_socket_name);
481 if (unlink(sock->conf.unix_socket_name) == -1) {
482 if (errno != ENOENT) {
483 log_warn("%s: unlink %s", __func__,
484 sock->conf.unix_socket_name);
490 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
491 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
493 if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
494 log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
496 (void)umask(old_umask);
500 (void)umask(old_umask);
502 if (chmod(sock->conf.unix_socket_name, mode) == -1) {
503 log_warn("%s: chmod", __func__);
505 (void)unlink(sock->conf.unix_socket_name);
509 if (chown(sock->conf.unix_socket_name, env->pw->pw_uid,
510 env->pw->pw_gid) == -1) {
511 log_warn("%s: chown", __func__);
513 (void)unlink(sock->conf.unix_socket_name);
517 if (listen(u_fd, SOCKS_BACKLOG) == -1) {
518 log_warn("%s: listen", __func__);
526 sockets_create_socket(struct address *a)
528 int fd = -1, o_val = 1, flags;
530 fd = socket(a->ai_family, a->ai_socktype, a->ai_protocol);
534 log_debug("%s: opened socket (%d) for %s", __func__,
537 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
538 sizeof(int)) == -1) {
539 log_warn("%s: setsockopt error", __func__);
545 flags = fcntl(fd, F_GETFL);
547 if (fcntl(fd, F_SETFL, flags) == -1) {
548 log_info("%s: could not enable non-blocking I/O", __func__);
553 if (bind(fd, (struct sockaddr *)&a->ss, a->slen) == -1) {
555 log_info("%s: can't bind to port %d", __func__, a->port);
559 if (listen(fd, SOMAXCONN) == -1) {
560 log_warn("%s, unable to listen on socket", __func__);
569 sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
570 int reserve, volatile int *counter)
574 if (getdtablecount() + reserve +
575 ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
576 log_debug("inflight fds exceeded");
580 /* TA: This needs fixing upstream. */
582 ret = accept(sockfd, addr, addrlen);
584 ret = accept4(sockfd, addr, addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC);
589 log_debug("inflight incremented, now %d", *counter);
596 sockets_accept_paused(int fd, short events, void *arg)
598 struct socket *sock = (struct socket *)arg;
600 event_add(&sock->ev, NULL);
604 sockets_socket_accept(int fd, short event, void *arg)
606 struct socket *sock = (struct socket *)arg;
607 struct sockaddr_storage ss;
608 struct timeval backoff;
609 struct request *c = NULL;
616 event_add(&sock->ev, NULL);
617 if (event & EV_TIMEOUT)
622 s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
623 FD_RESERVE, &cgi_inflight);
633 event_del(&sock->ev);
634 evtimer_add(&sock->pause, &backoff);
637 log_warn("%s: accept", __func__);
641 if (client_cnt > GOTWEBD_MAXCLIENTS)
644 c = calloc(1, sizeof(struct request));
646 log_warn("%s", __func__);
652 c->tp = template(c, &fcgi_write, c->outbuf, sizeof(c->outbuf));
654 log_warn("%s", __func__);
663 memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
666 c->request_started = 0;
667 c->sock->client_status = CLIENT_CONNECT;
669 event_set(&c->ev, s, EV_READ|EV_PERSIST, fcgi_request, c);
670 event_add(&c->ev, NULL);
672 evtimer_set(&c->tmo, fcgi_timeout, c);
673 evtimer_add(&c->tmo, &timeout);
686 sockets_rlimit(int maxfd)
690 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
691 fatal("%s: failed to get resource limit", __func__);
692 log_debug("%s: max open files %llu", __func__,
693 (unsigned long long)rl.rlim_max);
696 * Allow the maximum number of open file descriptors for this
697 * login class (which should be the class "daemon" by default).
700 rl.rlim_cur = rl.rlim_max;
702 rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
703 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
704 fatal("%s: failed to set resource limit", __func__);