Blob


1 /*
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>
6 *
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.
10 *
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.
18 */
20 #include "got_compat.h"
22 #include <sys/param.h>
23 #include <sys/ioctl.h>
24 #include <sys/queue.h>
25 #include <sys/wait.h>
26 #include <sys/uio.h>
27 #include <sys/resource.h>
28 #include <sys/socket.h>
29 #include <sys/stat.h>
30 #include <sys/time.h>
31 #include <sys/types.h>
32 #include <sys/mman.h>
33 #include <sys/un.h>
35 #include <net/if.h>
36 #include <netinet/in.h>
38 #include <errno.h>
39 #include <event.h>
40 #include <fcntl.h>
41 #include <ifaddrs.h>
42 #include <limits.h>
43 #include <netdb.h>
44 #include <poll.h>
45 #include <pwd.h>
46 #include <stddef.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.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"
58 #include "gotwebd.h"
59 #include "tmpl.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 *,
79 int, volatile int *);
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 *);
86 int cgi_inflight = 0;
88 void
89 sockets(struct gotwebd *env, int fd)
90 {
91 struct event sighup, sigusr1, sigchld;
93 event_init();
95 sockets_rlimit(-1);
97 if (config_init(env) == -1)
98 fatal("failed to initialize configuration");
100 if ((env->iev_parent = malloc(sizeof(*env->iev_parent))) == NULL)
101 fatal("malloc");
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,
106 env->iev_parent);
107 event_add(&env->iev_parent->ev, NULL);
109 signal(SIGPIPE, SIG_IGN);
111 signal_set(&sighup, SIGHUP, sockets_sighdlr, env);
112 signal_add(&sighup, NULL);
113 signal_set(&sigusr1, SIGUSR1, sockets_sighdlr, env);
114 signal_add(&sigusr1, NULL);
115 signal_set(&sigchld, SIGCHLD, sockets_sighdlr, env);
116 signal_add(&sigchld, NULL);
118 #ifndef PROFILE
119 if (pledge("stdio rpath inet recvfd proc exec sendfd unveil",
120 NULL) == -1)
121 fatal("pledge");
122 #endif
124 event_dispatch();
125 sockets_shutdown();
128 void
129 sockets_parse_sockets(struct gotwebd *env)
131 struct server *srv;
132 struct address *a;
133 struct socket *new_sock = NULL;
134 int sock_id = 1;
136 TAILQ_FOREACH(srv, &env->servers, entry) {
137 if (srv->unix_socket) {
138 new_sock = sockets_conf_new_socket_unix(env, srv,
139 sock_id);
140 if (new_sock) {
141 sock_id++;
142 TAILQ_INSERT_TAIL(&env->sockets, new_sock,
143 entry);
147 if (srv->fcgi_socket) {
148 if (TAILQ_EMPTY(&srv->al)) {
149 fatalx("%s: server %s has no IP addresses to "
150 "listen for FCGI connections", __func__,
151 srv->name);
153 TAILQ_FOREACH(a, &srv->al, entry) {
154 if (a->ss.ss_family != AF_INET &&
155 a->ss.ss_family != AF_INET6)
156 continue;
157 new_sock = sockets_conf_new_socket_fcgi(env,
158 srv, sock_id, a);
159 if (new_sock) {
160 sock_id++;
161 TAILQ_INSERT_TAIL(&env->sockets,
162 new_sock, entry);
169 static struct socket *
170 sockets_conf_new_socket_unix(struct gotwebd *env, struct server *srv, int id)
172 struct socket *sock;
173 int n;
175 if ((sock = calloc(1, sizeof(*sock))) == NULL)
176 fatalx("%s: calloc", __func__);
178 sock->conf.id = id;
179 sock->fd = -1;
180 sock->conf.af_type = AF_UNIX;
182 if (strlcpy(sock->conf.unix_socket_name,
183 srv->unix_socket_name,
184 sizeof(sock->conf.unix_socket_name)) >=
185 sizeof(sock->conf.unix_socket_name)) {
186 free(sock);
187 fatalx("%s: strlcpy", __func__);
190 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
191 srv->name);
192 if (n < 0 || (size_t)n >= GOTWEBD_MAXTEXT) {
193 free(sock);
194 fatalx("%s: snprintf", __func__);
197 if (strlcpy(sock->conf.srv_name, srv->name,
198 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
199 free(sock);
200 fatalx("%s: strlcpy", __func__);
203 return sock;
206 static struct socket *
207 sockets_conf_new_socket_fcgi(struct gotwebd *env, struct server *srv, int id,
208 struct address *a)
210 struct socket *sock;
211 struct address *acp;
212 int n;
214 if ((sock = calloc(1, sizeof(*sock))) == NULL)
215 fatalx("%s: calloc", __func__);
217 sock->conf.id = id;
218 sock->fd = -1;
219 sock->conf.af_type = a->ss.ss_family;
221 sock->conf.fcgi_socket_port = a->port;
223 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
224 srv->name);
225 if (n < 0 || (size_t)n >= GOTWEBD_MAXTEXT) {
226 free(sock);
227 fatalx("%s: snprintf", __func__);
230 if (strlcpy(sock->conf.srv_name, srv->name,
231 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
232 free(sock);
233 fatalx("%s: strlcpy", __func__);
236 acp = &sock->conf.addr;
238 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
239 acp->slen = a->slen;
240 acp->ai_family = a->ai_family;
241 acp->ai_socktype = a->ai_socktype;
242 acp->ai_protocol = a->ai_protocol;
243 acp->port = a->port;
244 if (*a->ifname != '\0') {
245 if (strlcpy(acp->ifname, a->ifname,
246 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
247 fatalx("%s: interface name truncated",
248 __func__);
252 return (sock);
255 static void
256 sockets_launch(void)
258 struct socket *sock;
259 struct server *srv;
260 const struct got_error *error;
262 TAILQ_FOREACH(sock, &gotwebd_env->sockets, entry) {
263 log_debug("%s: configuring socket %d (%d)", __func__,
264 sock->conf.id, sock->fd);
266 event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
267 sockets_socket_accept, sock);
269 if (event_add(&sock->ev, NULL))
270 fatalx("event add sock");
272 evtimer_set(&sock->pause, sockets_accept_paused, sock);
274 log_debug("%s: running socket listener %d", __func__,
275 sock->conf.id);
278 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry) {
279 if (unveil(srv->repos_path, "r") == -1)
280 fatal("unveil %s", srv->repos_path);
283 error = got_privsep_unveil_exec_helpers();
284 if (error)
285 fatal("%s", error->msg);
287 if (unveil(NULL, NULL) == -1)
288 fatal("unveil");
291 static void
292 sockets_purge(struct gotwebd *env)
294 struct socket *sock, *tsock;
296 /* shutdown and remove sockets */
297 TAILQ_FOREACH_SAFE(sock, &env->sockets, entry, tsock) {
298 if (event_initialized(&sock->ev))
299 event_del(&sock->ev);
300 if (evtimer_initialized(&sock->evt))
301 evtimer_del(&sock->evt);
302 if (evtimer_initialized(&sock->pause))
303 evtimer_del(&sock->pause);
304 if (sock->fd != -1)
305 close(sock->fd);
306 TAILQ_REMOVE(&env->sockets, sock, entry);
310 static void
311 sockets_dispatch_main(int fd, short event, void *arg)
313 struct imsgev *iev = arg;
314 struct imsgbuf *ibuf;
315 struct imsg imsg;
316 struct gotwebd *env = gotwebd_env;
317 ssize_t n;
318 int shut = 0;
320 ibuf = &iev->ibuf;
322 if (event & EV_READ) {
323 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
324 fatal("imsg_read error");
325 if (n == 0) /* Connection closed */
326 shut = 1;
328 if (event & EV_WRITE) {
329 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
330 fatal("msgbuf_write");
331 if (n == 0) /* Connection closed */
332 shut = 1;
335 for (;;) {
336 if ((n = imsg_get(ibuf, &imsg)) == -1)
337 fatal("imsg_get");
338 if (n == 0) /* No more messages. */
339 break;
341 switch (imsg.hdr.type) {
342 case IMSG_CFG_SRV:
343 config_getserver(env, &imsg);
344 break;
345 case IMSG_CFG_SOCK:
346 config_getsock(env, &imsg);
347 break;
348 case IMSG_CFG_FD:
349 config_getfd(env, &imsg);
350 break;
351 case IMSG_CFG_DONE:
352 config_getcfg(env, &imsg);
353 break;
354 case IMSG_CTL_START:
355 sockets_launch();
356 break;
357 default:
358 fatalx("%s: unknown imsg type %d", __func__,
359 imsg.hdr.type);
362 imsg_free(&imsg);
365 if (!shut)
366 imsg_event_add(iev);
367 else {
368 /* This pipe is dead. Remove its event handler */
369 event_del(&iev->ev);
370 event_loopexit(NULL);
374 static void
375 sockets_sighdlr(int sig, short event, void *arg)
377 switch (sig) {
378 case SIGHUP:
379 log_info("%s: ignoring SIGHUP", __func__);
380 break;
381 case SIGPIPE:
382 log_info("%s: ignoring SIGPIPE", __func__);
383 break;
384 case SIGUSR1:
385 log_info("%s: ignoring SIGUSR1", __func__);
386 break;
387 case SIGCHLD:
388 break;
389 default:
390 log_info("SIGNAL: %d", sig);
391 fatalx("unexpected signal");
395 static void
396 sockets_shutdown(void)
398 struct server *srv, *tsrv;
399 struct socket *sock, *tsock;
401 sockets_purge(gotwebd_env);
403 /* clean sockets */
404 TAILQ_FOREACH_SAFE(sock, &gotwebd_env->sockets, entry, tsock) {
405 TAILQ_REMOVE(&gotwebd_env->sockets, sock, entry);
406 close(sock->fd);
407 free(sock);
410 /* clean servers */
411 TAILQ_FOREACH_SAFE(srv, &gotwebd_env->servers, entry, tsrv)
412 free(srv);
414 free(gotwebd_env);
417 int
418 sockets_privinit(struct gotwebd *env, struct socket *sock)
420 if (sock->conf.af_type == AF_UNIX) {
421 log_debug("%s: initializing unix socket %s", __func__,
422 sock->conf.unix_socket_name);
423 sock->fd = sockets_unix_socket_listen(env, sock);
424 if (sock->fd == -1) {
425 log_warnx("%s: create unix socket failed", __func__);
426 return -1;
430 if (sock->conf.af_type == AF_INET || sock->conf.af_type == AF_INET6) {
431 log_debug("%s: initializing %s FCGI socket on port %d for %s",
432 __func__, sock->conf.af_type == AF_INET ? "inet" : "inet6",
433 sock->conf.fcgi_socket_port, sock->conf.name);
434 sock->fd = sockets_create_socket(&sock->conf.addr);
435 if (sock->fd == -1) {
436 log_warnx("%s: create FCGI socket failed", __func__);
437 return -1;
441 return 0;
444 static int
445 sockets_unix_socket_listen(struct gotwebd *env, struct socket *sock)
447 struct sockaddr_un sun;
448 struct socket *tsock;
449 int u_fd = -1;
450 mode_t old_umask, mode;
452 TAILQ_FOREACH(tsock, &env->sockets, entry) {
453 if (strcmp(tsock->conf.unix_socket_name,
454 sock->conf.unix_socket_name) == 0 &&
455 tsock->fd != -1)
456 return (tsock->fd);
459 /* TA: FIXME: this needs upstreaming. */
460 int socket_flags = SOCK_STREAM | SOCK_NONBLOCK;
461 #ifdef SOCK_CLOEXEC
462 socket_flags |= SOCK_CLOEXEC;
463 #endif
464 u_fd = socket(AF_UNIX, socket_flags, 0);
465 if (u_fd == -1) {
466 log_warn("%s: socket", __func__);
467 return -1;
470 sun.sun_family = AF_UNIX;
471 if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
472 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
473 log_warn("%s: %s name too long", __func__,
474 sock->conf.unix_socket_name);
475 close(u_fd);
476 return -1;
479 if (unlink(sock->conf.unix_socket_name) == -1) {
480 if (errno != ENOENT) {
481 log_warn("%s: unlink %s", __func__,
482 sock->conf.unix_socket_name);
483 close(u_fd);
484 return -1;
488 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
489 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
491 if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
492 log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
493 close(u_fd);
494 (void)umask(old_umask);
495 return -1;
498 (void)umask(old_umask);
500 if (chmod(sock->conf.unix_socket_name, mode) == -1) {
501 log_warn("%s: chmod", __func__);
502 close(u_fd);
503 (void)unlink(sock->conf.unix_socket_name);
504 return -1;
507 if (chown(sock->conf.unix_socket_name, env->pw->pw_uid,
508 env->pw->pw_gid) == -1) {
509 log_warn("%s: chown", __func__);
510 close(u_fd);
511 (void)unlink(sock->conf.unix_socket_name);
512 return -1;
515 if (listen(u_fd, SOCKS_BACKLOG) == -1) {
516 log_warn("%s: listen", __func__);
517 return -1;
520 return u_fd;
523 static int
524 sockets_create_socket(struct address *a)
526 int fd = -1, o_val = 1, flags;
528 fd = socket(a->ai_family, a->ai_socktype, a->ai_protocol);
529 if (fd == -1)
530 return -1;
532 log_debug("%s: opened socket (%d) for %s", __func__,
533 fd, a->ifname);
535 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
536 sizeof(int)) == -1) {
537 log_warn("%s: setsockopt error", __func__);
538 close(fd);
539 return -1;
542 /* non-blocking */
543 flags = fcntl(fd, F_GETFL);
544 flags |= O_NONBLOCK;
545 if (fcntl(fd, F_SETFL, flags) == -1) {
546 log_info("%s: could not enable non-blocking I/O", __func__);
547 close(fd);
548 return -1;
551 if (bind(fd, (struct sockaddr *)&a->ss, a->slen) == -1) {
552 close(fd);
553 log_info("%s: can't bind to port %d", __func__, a->port);
554 return -1;
557 if (listen(fd, SOMAXCONN) == -1) {
558 log_warn("%s, unable to listen on socket", __func__);
559 close(fd);
560 return -1;
563 return (fd);
566 static int
567 sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
568 int reserve, volatile int *counter)
570 int ret;
572 if (getdtablecount() + reserve +
573 ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
574 log_debug("inflight fds exceeded");
575 errno = EMFILE;
576 return -1;
578 /* TA: This needs fixing upstream. */
579 #ifdef __APPLE__
580 ret = accept(sockfd, addr, addrlen);
581 #else
582 ret = accept4(sockfd, addr, addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC);
583 #endif
585 if (ret > -1) {
586 (*counter)++;
587 log_debug("inflight incremented, now %d", *counter);
590 return ret;
593 static void
594 sockets_accept_paused(int fd, short events, void *arg)
596 struct socket *sock = (struct socket *)arg;
598 event_add(&sock->ev, NULL);
601 void
602 sockets_socket_accept(int fd, short event, void *arg)
604 struct socket *sock = (struct socket *)arg;
605 struct sockaddr_storage ss;
606 struct timeval backoff;
607 struct request *c = NULL;
608 socklen_t len;
609 int s;
611 backoff.tv_sec = 1;
612 backoff.tv_usec = 0;
614 event_add(&sock->ev, NULL);
615 if (event & EV_TIMEOUT)
616 return;
618 len = sizeof(ss);
620 s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
621 FD_RESERVE, &cgi_inflight);
623 if (s == -1) {
624 switch (errno) {
625 case EINTR:
626 case EWOULDBLOCK:
627 case ECONNABORTED:
628 return;
629 case EMFILE:
630 case ENFILE:
631 event_del(&sock->ev);
632 evtimer_add(&sock->pause, &backoff);
633 return;
634 default:
635 log_warn("%s: accept", __func__);
639 if (client_cnt > GOTWEBD_MAXCLIENTS)
640 goto err;
642 c = calloc(1, sizeof(struct request));
643 if (c == NULL) {
644 log_warn("%s", __func__);
645 close(s);
646 cgi_inflight--;
647 return;
650 c->tp = template(c, &fcgi_write, c->outbuf, sizeof(c->outbuf));
651 if (c->tp == NULL) {
652 log_warn("%s", __func__);
653 close(s);
654 cgi_inflight--;
655 free(c);
656 return;
659 c->fd = s;
660 c->sock = sock;
661 memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
662 c->buf_pos = 0;
663 c->buf_len = 0;
664 c->request_started = 0;
665 c->sock->client_status = CLIENT_CONNECT;
667 event_set(&c->ev, s, EV_READ|EV_PERSIST, fcgi_request, c);
668 event_add(&c->ev, NULL);
670 evtimer_set(&c->tmo, fcgi_timeout, c);
671 evtimer_add(&c->tmo, &timeout);
673 client_cnt++;
675 return;
676 err:
677 cgi_inflight--;
678 close(s);
679 if (c != NULL)
680 free(c);
683 static void
684 sockets_rlimit(int maxfd)
686 struct rlimit rl;
688 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
689 fatal("%s: failed to get resource limit", __func__);
690 log_debug("%s: max open files %llu", __func__,
691 (unsigned long long)rl.rlim_max);
693 /*
694 * Allow the maximum number of open file descriptors for this
695 * login class (which should be the class "daemon" by default).
696 */
697 if (maxfd == -1)
698 rl.rlim_cur = rl.rlim_max;
699 else
700 rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
701 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
702 fatal("%s: failed to set resource limit", __func__);