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, sigint, sigusr1, sigchld, sigterm;
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(&sigint, SIGINT, sockets_sighdlr, env);
114 signal_add(&sigint, NULL);
115 signal_set(&sigusr1, SIGUSR1, sockets_sighdlr, env);
116 signal_add(&sigusr1, NULL);
117 signal_set(&sigchld, SIGCHLD, sockets_sighdlr, env);
118 signal_add(&sigchld, NULL);
119 signal_set(&sigterm, SIGTERM, sockets_sighdlr, env);
120 signal_add(&sigterm, NULL);
122 #ifndef PROFILE
123 if (pledge("stdio rpath inet recvfd proc exec sendfd unveil",
124 NULL) == -1)
125 fatal("pledge");
126 #endif
128 event_dispatch();
129 sockets_shutdown();
132 void
133 sockets_parse_sockets(struct gotwebd *env)
135 struct server *srv;
136 struct address *a;
137 struct socket *new_sock = NULL;
138 int sock_id = 1;
140 TAILQ_FOREACH(srv, &env->servers, entry) {
141 if (srv->unix_socket) {
142 new_sock = sockets_conf_new_socket_unix(env, srv,
143 sock_id);
144 if (new_sock) {
145 sock_id++;
146 TAILQ_INSERT_TAIL(&env->sockets, new_sock,
147 entry);
151 if (srv->fcgi_socket) {
152 if (TAILQ_EMPTY(&srv->al)) {
153 fatalx("%s: server %s has no IP addresses to "
154 "listen for FCGI connections", __func__,
155 srv->name);
157 TAILQ_FOREACH(a, &srv->al, entry) {
158 if (a->ss.ss_family != AF_INET &&
159 a->ss.ss_family != AF_INET6)
160 continue;
161 new_sock = sockets_conf_new_socket_fcgi(env,
162 srv, sock_id, a);
163 if (new_sock) {
164 sock_id++;
165 TAILQ_INSERT_TAIL(&env->sockets,
166 new_sock, entry);
173 static struct socket *
174 sockets_conf_new_socket_unix(struct gotwebd *env, struct server *srv, int id)
176 struct socket *sock;
177 int n;
179 if ((sock = calloc(1, sizeof(*sock))) == NULL)
180 fatalx("%s: calloc", __func__);
182 sock->conf.id = id;
183 sock->fd = -1;
184 sock->conf.af_type = AF_UNIX;
186 if (strlcpy(sock->conf.unix_socket_name,
187 srv->unix_socket_name,
188 sizeof(sock->conf.unix_socket_name)) >=
189 sizeof(sock->conf.unix_socket_name)) {
190 free(sock);
191 fatalx("%s: strlcpy", __func__);
194 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
195 srv->name);
196 if (n < 0 || (size_t)n >= GOTWEBD_MAXTEXT) {
197 free(sock);
198 fatalx("%s: snprintf", __func__);
201 if (strlcpy(sock->conf.srv_name, srv->name,
202 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
203 free(sock);
204 fatalx("%s: strlcpy", __func__);
207 return sock;
210 static struct socket *
211 sockets_conf_new_socket_fcgi(struct gotwebd *env, struct server *srv, int id,
212 struct address *a)
214 struct socket *sock;
215 struct address *acp;
216 int n;
218 if ((sock = calloc(1, sizeof(*sock))) == NULL)
219 fatalx("%s: calloc", __func__);
221 sock->conf.id = id;
222 sock->fd = -1;
223 sock->conf.af_type = a->ss.ss_family;
225 sock->conf.fcgi_socket_port = a->port;
227 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
228 srv->name);
229 if (n < 0 || (size_t)n >= GOTWEBD_MAXTEXT) {
230 free(sock);
231 fatalx("%s: snprintf", __func__);
234 if (strlcpy(sock->conf.srv_name, srv->name,
235 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
236 free(sock);
237 fatalx("%s: strlcpy", __func__);
240 acp = &sock->conf.addr;
242 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
243 acp->slen = a->slen;
244 acp->ai_family = a->ai_family;
245 acp->ai_socktype = a->ai_socktype;
246 acp->ai_protocol = a->ai_protocol;
247 acp->port = a->port;
248 if (*a->ifname != '\0') {
249 if (strlcpy(acp->ifname, a->ifname,
250 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
251 fatalx("%s: interface name truncated",
252 __func__);
256 return (sock);
259 static void
260 sockets_launch(void)
262 struct socket *sock;
263 struct server *srv;
264 const struct got_error *error;
266 TAILQ_FOREACH(sock, &gotwebd_env->sockets, entry) {
267 log_debug("%s: configuring socket %d (%d)", __func__,
268 sock->conf.id, sock->fd);
270 event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
271 sockets_socket_accept, sock);
273 if (event_add(&sock->ev, NULL))
274 fatalx("event add sock");
276 evtimer_set(&sock->pause, sockets_accept_paused, sock);
278 log_debug("%s: running socket listener %d", __func__,
279 sock->conf.id);
282 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry) {
283 if (unveil(srv->repos_path, "r") == -1)
284 fatal("unveil %s", srv->repos_path);
287 error = got_privsep_unveil_exec_helpers();
288 if (error)
289 fatal("%s", error->msg);
291 if (unveil(NULL, NULL) == -1)
292 fatal("unveil");
295 static void
296 sockets_purge(struct gotwebd *env)
298 struct socket *sock, *tsock;
300 /* shutdown and remove sockets */
301 TAILQ_FOREACH_SAFE(sock, &env->sockets, entry, tsock) {
302 if (event_initialized(&sock->ev))
303 event_del(&sock->ev);
304 if (evtimer_initialized(&sock->evt))
305 evtimer_del(&sock->evt);
306 if (evtimer_initialized(&sock->pause))
307 evtimer_del(&sock->pause);
308 if (sock->fd != -1)
309 close(sock->fd);
310 TAILQ_REMOVE(&env->sockets, sock, entry);
314 static void
315 sockets_dispatch_main(int fd, short event, void *arg)
317 struct imsgev *iev = arg;
318 struct imsgbuf *ibuf;
319 struct imsg imsg;
320 struct gotwebd *env = gotwebd_env;
321 ssize_t n;
322 int shut = 0;
324 ibuf = &iev->ibuf;
326 if (event & EV_READ) {
327 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
328 fatal("imsg_read error");
329 if (n == 0) /* Connection closed */
330 shut = 1;
332 if (event & EV_WRITE) {
333 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
334 fatal("msgbuf_write");
335 if (n == 0) /* Connection closed */
336 shut = 1;
339 for (;;) {
340 if ((n = imsg_get(ibuf, &imsg)) == -1)
341 fatal("imsg_get");
342 if (n == 0) /* No more messages. */
343 break;
345 switch (imsg.hdr.type) {
346 case IMSG_CFG_SRV:
347 config_getserver(env, &imsg);
348 break;
349 case IMSG_CFG_SOCK:
350 config_getsock(env, &imsg);
351 break;
352 case IMSG_CFG_FD:
353 config_getfd(env, &imsg);
354 break;
355 case IMSG_CFG_DONE:
356 config_getcfg(env, &imsg);
357 break;
358 case IMSG_CTL_START:
359 sockets_launch();
360 break;
361 default:
362 fatalx("%s: unknown imsg type %d", __func__,
363 imsg.hdr.type);
366 imsg_free(&imsg);
369 if (!shut)
370 imsg_event_add(iev);
371 else {
372 /* This pipe is dead. Remove its event handler */
373 event_del(&iev->ev);
374 event_loopexit(NULL);
378 static void
379 sockets_sighdlr(int sig, short event, void *arg)
381 switch (sig) {
382 case SIGHUP:
383 log_info("%s: ignoring SIGHUP", __func__);
384 break;
385 case SIGPIPE:
386 log_info("%s: ignoring SIGPIPE", __func__);
387 break;
388 case SIGUSR1:
389 log_info("%s: ignoring SIGUSR1", __func__);
390 break;
391 case SIGCHLD:
392 break;
393 case SIGINT:
394 case SIGTERM:
395 sockets_shutdown();
396 break;
397 default:
398 log_info("SIGNAL: %d", sig);
399 fatalx("unexpected signal");
403 static void
404 sockets_shutdown(void)
406 struct server *srv, *tsrv;
407 struct socket *sock, *tsock;
409 sockets_purge(gotwebd_env);
411 /* clean sockets */
412 TAILQ_FOREACH_SAFE(sock, &gotwebd_env->sockets, entry, tsock) {
413 TAILQ_REMOVE(&gotwebd_env->sockets, sock, entry);
414 close(sock->fd);
415 free(sock);
418 /* clean servers */
419 TAILQ_FOREACH_SAFE(srv, &gotwebd_env->servers, entry, tsrv)
420 free(srv);
422 free(gotwebd_env);
424 exit(0);
427 int
428 sockets_privinit(struct gotwebd *env, struct socket *sock)
430 if (sock->conf.af_type == AF_UNIX) {
431 log_debug("%s: initializing unix socket %s", __func__,
432 sock->conf.unix_socket_name);
433 sock->fd = sockets_unix_socket_listen(env, sock);
434 if (sock->fd == -1) {
435 log_warnx("%s: create unix socket failed", __func__);
436 return -1;
440 if (sock->conf.af_type == AF_INET || sock->conf.af_type == AF_INET6) {
441 log_debug("%s: initializing %s FCGI socket on port %d for %s",
442 __func__, sock->conf.af_type == AF_INET ? "inet" : "inet6",
443 sock->conf.fcgi_socket_port, sock->conf.name);
444 sock->fd = sockets_create_socket(&sock->conf.addr);
445 if (sock->fd == -1) {
446 log_warnx("%s: create FCGI socket failed", __func__);
447 return -1;
451 return 0;
454 static int
455 sockets_unix_socket_listen(struct gotwebd *env, struct socket *sock)
457 struct sockaddr_un sun;
458 struct socket *tsock;
459 int u_fd = -1;
460 mode_t old_umask, mode;
462 TAILQ_FOREACH(tsock, &env->sockets, entry) {
463 if (strcmp(tsock->conf.unix_socket_name,
464 sock->conf.unix_socket_name) == 0 &&
465 tsock->fd != -1)
466 return (tsock->fd);
469 /* TA: FIXME: this needs upstreaming. */
470 int socket_flags = SOCK_STREAM | SOCK_NONBLOCK;
471 #ifdef SOCK_CLOEXEC
472 socket_flags |= SOCK_CLOEXEC;
473 #endif
474 u_fd = socket(AF_UNIX, socket_flags, 0);
475 if (u_fd == -1) {
476 log_warn("%s: socket", __func__);
477 return -1;
480 sun.sun_family = AF_UNIX;
481 if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
482 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
483 log_warn("%s: %s name too long", __func__,
484 sock->conf.unix_socket_name);
485 close(u_fd);
486 return -1;
489 if (unlink(sock->conf.unix_socket_name) == -1) {
490 if (errno != ENOENT) {
491 log_warn("%s: unlink %s", __func__,
492 sock->conf.unix_socket_name);
493 close(u_fd);
494 return -1;
498 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
499 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
501 if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
502 log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
503 close(u_fd);
504 (void)umask(old_umask);
505 return -1;
508 (void)umask(old_umask);
510 if (chmod(sock->conf.unix_socket_name, mode) == -1) {
511 log_warn("%s: chmod", __func__);
512 close(u_fd);
513 (void)unlink(sock->conf.unix_socket_name);
514 return -1;
517 if (chown(sock->conf.unix_socket_name, env->pw->pw_uid,
518 env->pw->pw_gid) == -1) {
519 log_warn("%s: chown", __func__);
520 close(u_fd);
521 (void)unlink(sock->conf.unix_socket_name);
522 return -1;
525 if (listen(u_fd, SOCKS_BACKLOG) == -1) {
526 log_warn("%s: listen", __func__);
527 return -1;
530 return u_fd;
533 static int
534 sockets_create_socket(struct address *a)
536 int fd = -1, o_val = 1, flags;
538 fd = socket(a->ai_family, a->ai_socktype, a->ai_protocol);
539 if (fd == -1)
540 return -1;
542 log_debug("%s: opened socket (%d) for %s", __func__,
543 fd, a->ifname);
545 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
546 sizeof(int)) == -1) {
547 log_warn("%s: setsockopt error", __func__);
548 close(fd);
549 return -1;
552 /* non-blocking */
553 flags = fcntl(fd, F_GETFL);
554 flags |= O_NONBLOCK;
555 if (fcntl(fd, F_SETFL, flags) == -1) {
556 log_info("%s: could not enable non-blocking I/O", __func__);
557 close(fd);
558 return -1;
561 if (bind(fd, (struct sockaddr *)&a->ss, a->slen) == -1) {
562 close(fd);
563 log_info("%s: can't bind to port %d", __func__, a->port);
564 return -1;
567 if (listen(fd, SOMAXCONN) == -1) {
568 log_warn("%s, unable to listen on socket", __func__);
569 close(fd);
570 return -1;
573 return (fd);
576 static int
577 sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
578 int reserve, volatile int *counter)
580 int ret;
582 if (getdtablecount() + reserve +
583 ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
584 log_debug("inflight fds exceeded");
585 errno = EMFILE;
586 return -1;
588 /* TA: This needs fixing upstream. */
589 #ifdef __APPLE__
590 ret = accept(sockfd, addr, addrlen);
591 #else
592 ret = accept4(sockfd, addr, addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC);
593 #endif
595 if (ret > -1) {
596 (*counter)++;
597 log_debug("inflight incremented, now %d", *counter);
600 return ret;
603 static void
604 sockets_accept_paused(int fd, short events, void *arg)
606 struct socket *sock = (struct socket *)arg;
608 event_add(&sock->ev, NULL);
611 void
612 sockets_socket_accept(int fd, short event, void *arg)
614 struct socket *sock = (struct socket *)arg;
615 struct sockaddr_storage ss;
616 struct timeval backoff;
617 struct request *c = NULL;
618 socklen_t len;
619 int s;
621 backoff.tv_sec = 1;
622 backoff.tv_usec = 0;
624 event_add(&sock->ev, NULL);
625 if (event & EV_TIMEOUT)
626 return;
628 len = sizeof(ss);
630 s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
631 FD_RESERVE, &cgi_inflight);
633 if (s == -1) {
634 switch (errno) {
635 case EINTR:
636 case EWOULDBLOCK:
637 case ECONNABORTED:
638 return;
639 case EMFILE:
640 case ENFILE:
641 event_del(&sock->ev);
642 evtimer_add(&sock->pause, &backoff);
643 return;
644 default:
645 log_warn("%s: accept", __func__);
649 if (client_cnt > GOTWEBD_MAXCLIENTS)
650 goto err;
652 c = calloc(1, sizeof(struct request));
653 if (c == NULL) {
654 log_warn("%s", __func__);
655 close(s);
656 cgi_inflight--;
657 return;
660 c->tp = template(c, &fcgi_write, c->outbuf, sizeof(c->outbuf));
661 if (c->tp == NULL) {
662 log_warn("%s", __func__);
663 close(s);
664 cgi_inflight--;
665 free(c);
666 return;
669 c->fd = s;
670 c->sock = sock;
671 memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
672 c->buf_pos = 0;
673 c->buf_len = 0;
674 c->request_started = 0;
675 c->sock->client_status = CLIENT_CONNECT;
677 event_set(&c->ev, s, EV_READ|EV_PERSIST, fcgi_request, c);
678 event_add(&c->ev, NULL);
680 evtimer_set(&c->tmo, fcgi_timeout, c);
681 evtimer_add(&c->tmo, &timeout);
683 client_cnt++;
685 return;
686 err:
687 cgi_inflight--;
688 close(s);
689 if (c != NULL)
690 free(c);
693 static void
694 sockets_rlimit(int maxfd)
696 struct rlimit rl;
698 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
699 fatal("%s: failed to get resource limit", __func__);
700 log_debug("%s: max open files %llu", __func__,
701 (unsigned long long)rl.rlim_max);
703 /*
704 * Allow the maximum number of open file descriptors for this
705 * login class (which should be the class "daemon" by default).
706 */
707 if (maxfd == -1)
708 rl.rlim_cur = rl.rlim_max;
709 else
710 rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
711 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
712 fatal("%s: failed to set resource limit", __func__);