reduce gotwebd pledges to the minimum currently required; with op@
[got-portable.git] / gotwebd / sockets.c
blob17c797a987157d3e18fd32773d765f8c270c2938
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>
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>
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 "proc.h"
59 #include "gotwebd.h"
60 #include "tmpl.h"
62 #define SOCKS_BACKLOG 5
63 #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
66 volatile int client_cnt;
68 static struct timeval timeout = { TIMEOUT_DEFAULT, 0 };
70 static void sockets_sighdlr(int, short, void *);
71 static void sockets_run(struct privsep *, struct privsep_proc *, void *);
72 static void sockets_launch(void);
73 static void sockets_purge(struct gotwebd *);
74 static void sockets_accept_paused(int, short, void *);
75 static void sockets_rlimit(int);
77 static int sockets_dispatch_gotwebd(int, struct privsep_proc *,
78 struct imsg *);
79 static int sockets_unix_socket_listen(struct privsep *, struct socket *);
80 static int sockets_create_socket(struct address *, in_port_t);
81 static int sockets_accept_reserve(int, struct sockaddr *, socklen_t *,
82 int, volatile int *);
84 static struct socket *sockets_conf_new_socket_unix(struct gotwebd *,
85 struct server *, int);
86 static struct socket *sockets_conf_new_socket_fcgi(struct gotwebd *,
87 struct server *, int, struct address *);
89 int cgi_inflight = 0;
91 static struct privsep_proc procs[] = {
92 { "gotwebd", PROC_GOTWEBD, sockets_dispatch_gotwebd },
95 void
96 sockets(struct privsep *ps, struct privsep_proc *p)
98 proc_run(ps, p, procs, nitems(procs), sockets_run, NULL);
101 static void
102 sockets_run(struct privsep *ps, struct privsep_proc *p, void *arg)
104 if (config_init(ps->ps_env) == -1)
105 fatal("failed to initialize configuration");
107 p->p_shutdown = sockets_shutdown;
109 sockets_rlimit(-1);
111 signal_del(&ps->ps_evsigchld);
112 signal_set(&ps->ps_evsigchld, SIGCHLD, sockets_sighdlr, ps);
113 signal_add(&ps->ps_evsigchld, NULL);
115 #ifndef PROFILE
116 if (pledge("stdio rpath inet recvfd proc exec sendfd unveil",
117 NULL) == -1)
118 fatal("pledge");
119 #endif
122 void
123 sockets_parse_sockets(struct gotwebd *env)
125 struct server *srv;
126 struct address *a;
127 struct socket *new_sock = NULL;
128 int sock_id = 1;
130 TAILQ_FOREACH(srv, &env->servers, entry) {
131 if (srv->unix_socket) {
132 new_sock = sockets_conf_new_socket_unix(env, srv,
133 sock_id);
134 if (new_sock) {
135 sock_id++;
136 TAILQ_INSERT_TAIL(&env->sockets, new_sock,
137 entry);
141 if (srv->fcgi_socket) {
142 if (TAILQ_EMPTY(&srv->al)) {
143 fatalx("%s: server %s has no IP addresses to "
144 "listen for FCGI connections", __func__,
145 srv->name);
147 TAILQ_FOREACH(a, &srv->al, entry) {
148 if (a->ss.ss_family != AF_INET &&
149 a->ss.ss_family != AF_INET6)
150 continue;
151 new_sock = sockets_conf_new_socket_fcgi(env,
152 srv, sock_id, a);
153 if (new_sock) {
154 sock_id++;
155 TAILQ_INSERT_TAIL(&env->sockets,
156 new_sock, entry);
163 static struct socket *
164 sockets_conf_new_socket_unix(struct gotwebd *env, struct server *srv, int id)
166 struct socket *sock;
167 int n;
169 if ((sock = calloc(1, sizeof(*sock))) == NULL)
170 fatalx("%s: calloc", __func__);
172 sock->conf.id = id;
173 sock->fd = -1;
174 sock->conf.af_type = AF_UNIX;
176 if (strlcpy(sock->conf.unix_socket_name,
177 srv->unix_socket_name,
178 sizeof(sock->conf.unix_socket_name)) >=
179 sizeof(sock->conf.unix_socket_name)) {
180 free(sock);
181 fatalx("%s: strlcpy", __func__);
184 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
185 srv->name);
186 if (n < 0 || (size_t)n >= GOTWEBD_MAXTEXT) {
187 free(sock);
188 fatalx("%s: snprintf", __func__);
191 if (strlcpy(sock->conf.srv_name, srv->name,
192 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
193 free(sock);
194 fatalx("%s: strlcpy", __func__);
197 return sock;
200 static struct socket *
201 sockets_conf_new_socket_fcgi(struct gotwebd *env, struct server *srv, int id,
202 struct address *a)
204 struct socket *sock;
205 struct address *acp;
206 int n;
208 if ((sock = calloc(1, sizeof(*sock))) == NULL)
209 fatalx("%s: calloc", __func__);
211 sock->conf.id = id;
212 sock->fd = -1;
213 sock->conf.af_type = a->ss.ss_family;
215 sock->conf.fcgi_socket_port = a->port;
217 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
218 srv->name);
219 if (n < 0 || (size_t)n >= GOTWEBD_MAXTEXT) {
220 free(sock);
221 fatalx("%s: snprintf", __func__);
224 if (strlcpy(sock->conf.srv_name, srv->name,
225 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
226 free(sock);
227 fatalx("%s: strlcpy", __func__);
230 acp = &sock->conf.addr;
232 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
233 acp->ipproto = a->ipproto;
234 acp->port = a->port;
235 if (*a->ifname != '\0') {
236 if (strlcpy(acp->ifname, a->ifname,
237 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
238 fatalx("%s: interface name truncated",
239 __func__);
243 return (sock);
246 static void
247 sockets_launch(void)
249 struct socket *sock;
250 struct server *srv;
251 const struct got_error *error;
253 TAILQ_FOREACH(sock, &gotwebd_env->sockets, entry) {
254 log_debug("%s: configuring socket %d (%d)", __func__,
255 sock->conf.id, sock->fd);
257 event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
258 sockets_socket_accept, sock);
260 if (event_add(&sock->ev, NULL))
261 fatalx("event add sock");
263 evtimer_set(&sock->pause, sockets_accept_paused, sock);
265 log_debug("%s: running socket listener %d", __func__,
266 sock->conf.id);
269 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry) {
270 if (unveil(srv->repos_path, "r") == -1)
271 fatal("unveil %s", srv->repos_path);
274 error = got_privsep_unveil_exec_helpers();
275 if (error)
276 fatal("%s", error->msg);
278 if (unveil(NULL, NULL) == -1)
279 fatal("unveil");
282 static void
283 sockets_purge(struct gotwebd *env)
285 struct socket *sock, *tsock;
287 /* shutdown and remove sockets */
288 TAILQ_FOREACH_SAFE(sock, &env->sockets, entry, tsock) {
289 if (event_initialized(&sock->ev))
290 event_del(&sock->ev);
291 if (evtimer_initialized(&sock->evt))
292 evtimer_del(&sock->evt);
293 if (evtimer_initialized(&sock->pause))
294 evtimer_del(&sock->pause);
295 if (sock->fd != -1)
296 close(sock->fd);
297 TAILQ_REMOVE(&env->sockets, sock, entry);
301 static int
302 sockets_dispatch_gotwebd(int fd, struct privsep_proc *p, struct imsg *imsg)
304 struct privsep *ps = p->p_ps;
305 int res = 0, cmd = 0, verbose;
307 switch (imsg->hdr.type) {
308 case IMSG_CFG_SRV:
309 config_getserver(gotwebd_env, imsg);
310 break;
311 case IMSG_CFG_SOCK:
312 config_getsock(gotwebd_env, imsg);
313 break;
314 case IMSG_CFG_FD:
315 config_getfd(gotwebd_env, imsg);
316 break;
317 case IMSG_CFG_DONE:
318 config_getcfg(gotwebd_env, imsg);
319 break;
320 case IMSG_CTL_START:
321 sockets_launch();
322 break;
323 case IMSG_CTL_VERBOSE:
324 IMSG_SIZE_CHECK(imsg, &verbose);
325 memcpy(&verbose, imsg->data, sizeof(verbose));
326 log_setverbose(verbose);
327 break;
328 default:
329 return -1;
332 switch (cmd) {
333 case 0:
334 break;
335 default:
336 if (proc_compose_imsg(ps, PROC_GOTWEBD, -1, cmd,
337 imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
338 return -1;
339 break;
342 return 0;
345 static void
346 sockets_sighdlr(int sig, short event, void *arg)
348 switch (sig) {
349 case SIGHUP:
350 log_info("%s: ignoring SIGHUP", __func__);
351 break;
352 case SIGPIPE:
353 log_info("%s: ignoring SIGPIPE", __func__);
354 break;
355 case SIGUSR1:
356 log_info("%s: ignoring SIGUSR1", __func__);
357 break;
358 case SIGCHLD:
359 break;
360 default:
361 log_info("SIGNAL: %d", sig);
362 fatalx("unexpected signal");
366 void
367 sockets_shutdown(void)
369 struct server *srv, *tsrv;
370 struct socket *sock, *tsock;
371 int i;
373 sockets_purge(gotwebd_env);
375 /* clean sockets */
376 TAILQ_FOREACH_SAFE(sock, &gotwebd_env->sockets, entry, tsock) {
377 TAILQ_REMOVE(&gotwebd_env->sockets, sock, entry);
378 close(sock->fd);
379 free(sock);
382 /* clean servers */
383 TAILQ_FOREACH_SAFE(srv, &gotwebd_env->servers, entry, tsrv) {
384 for (i = 0; i < srv->ncached_repos; i++)
385 got_repo_close(srv->cached_repos[i].repo);
386 free(srv);
389 free(gotwebd_env);
393 sockets_privinit(struct gotwebd *env, struct socket *sock)
395 struct privsep *ps = env->gotwebd_ps;
397 if (sock->conf.af_type == AF_UNIX) {
398 log_debug("%s: initializing unix socket %s", __func__,
399 sock->conf.unix_socket_name);
400 sock->fd = sockets_unix_socket_listen(ps, sock);
401 if (sock->fd == -1) {
402 log_warnx("%s: create unix socket failed", __func__);
403 return -1;
407 if (sock->conf.af_type == AF_INET || sock->conf.af_type == AF_INET6) {
408 log_debug("%s: initializing %s FCGI socket on port %d for %s",
409 __func__, sock->conf.af_type == AF_INET ? "inet" : "inet6",
410 sock->conf.fcgi_socket_port, sock->conf.name);
411 sock->fd = sockets_create_socket(&sock->conf.addr,
412 sock->conf.fcgi_socket_port);
413 if (sock->fd == -1) {
414 log_warnx("%s: create FCGI socket failed", __func__);
415 return -1;
419 return 0;
422 static int
423 sockets_unix_socket_listen(struct privsep *ps, struct socket *sock)
425 struct gotwebd *env = ps->ps_env;
426 struct sockaddr_un sun;
427 struct socket *tsock;
428 int u_fd = -1;
429 mode_t old_umask, mode;
431 TAILQ_FOREACH(tsock, &env->sockets, entry) {
432 if (strcmp(tsock->conf.unix_socket_name,
433 sock->conf.unix_socket_name) == 0 &&
434 tsock->fd != -1)
435 return (tsock->fd);
438 /* TA: FIXME: this needs upstreaming. */
439 int socket_flags = SOCK_STREAM | SOCK_NONBLOCK;
440 #ifdef SOCK_CLOEXEC
441 socket_flags |= SOCK_CLOEXEC;
442 #endif
443 u_fd = socket(AF_UNIX, socket_flags, 0);
444 if (u_fd == -1) {
445 log_warn("%s: socket", __func__);
446 return -1;
449 sun.sun_family = AF_UNIX;
450 if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
451 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
452 log_warn("%s: %s name too long", __func__,
453 sock->conf.unix_socket_name);
454 close(u_fd);
455 return -1;
458 if (unlink(sock->conf.unix_socket_name) == -1) {
459 if (errno != ENOENT) {
460 log_warn("%s: unlink %s", __func__,
461 sock->conf.unix_socket_name);
462 close(u_fd);
463 return -1;
467 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
468 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
470 if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
471 log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
472 close(u_fd);
473 (void)umask(old_umask);
474 return -1;
477 (void)umask(old_umask);
479 if (chmod(sock->conf.unix_socket_name, mode) == -1) {
480 log_warn("%s: chmod", __func__);
481 close(u_fd);
482 (void)unlink(sock->conf.unix_socket_name);
483 return -1;
486 if (chown(sock->conf.unix_socket_name, ps->ps_pw->pw_uid,
487 ps->ps_pw->pw_gid) == -1) {
488 log_warn("%s: chown", __func__);
489 close(u_fd);
490 (void)unlink(sock->conf.unix_socket_name);
491 return -1;
494 if (listen(u_fd, SOCKS_BACKLOG) == -1) {
495 log_warn("%s: listen", __func__);
496 return -1;
499 return u_fd;
502 static int
503 sockets_create_socket(struct address *a, in_port_t port)
505 struct addrinfo hints;
506 int fd = -1, o_val = 1, flags;
508 memset(&hints, 0, sizeof(hints));
509 hints.ai_family = AF_UNSPEC;
510 hints.ai_socktype = SOCK_STREAM;
511 hints.ai_flags |= AI_PASSIVE;
513 switch (a->ss.ss_family) {
514 case AF_INET:
515 ((struct sockaddr_in *)(&a->ss))->sin_port = htons(port);
516 break;
517 case AF_INET6:
518 ((struct sockaddr_in6 *)(&a->ss))->sin6_port = htons(port);
519 break;
520 default:
521 log_warnx("%s: unknown address family", __func__);
522 return -1;
525 fd = socket(a->ss.ss_family, hints.ai_socktype, a->ipproto);
526 if (fd == -1)
527 return -1;
529 log_debug("%s: opened socket (%d) for %s", __func__,
530 fd, a->ifname);
532 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
533 sizeof(int)) == -1) {
534 log_warn("%s: setsockopt error", __func__);
535 close(fd);
536 return -1;
539 /* non-blocking */
540 flags = fcntl(fd, F_GETFL);
541 flags |= O_NONBLOCK;
542 if (fcntl(fd, F_SETFL, flags) == -1) {
543 log_info("%s: could not enable non-blocking I/O", __func__);
544 close(fd);
545 return -1;
548 if (bind(fd, (struct sockaddr *)&a->ss, SS_LEN(&a->ss)) == -1) {
549 close(fd);
550 log_info("%s: can't bind to port %d", __func__,
551 ntohs(port));
552 return -1;
555 if (listen(fd, SOMAXCONN) == -1) {
556 log_warn("%s, unable to listen on socket", __func__);
557 close(fd);
558 return -1;
561 return (fd);
564 static int
565 sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
566 int reserve, volatile int *counter)
568 int ret;
570 if (getdtablecount() + reserve +
571 ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
572 log_debug("inflight fds exceeded");
573 errno = EMFILE;
574 return -1;
576 /* TA: This needs fixing upstream. */
577 #ifdef __APPLE__
578 ret = accept(sockfd, addr, addrlen);
579 #else
580 ret = accept4(sockfd, addr, addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC);
581 #endif
583 if (ret > -1) {
584 (*counter)++;
585 log_debug("inflight incremented, now %d", *counter);
588 return ret;
591 static void
592 sockets_accept_paused(int fd, short events, void *arg)
594 struct socket *sock = (struct socket *)arg;
596 event_add(&sock->ev, NULL);
599 void
600 sockets_socket_accept(int fd, short event, void *arg)
602 struct socket *sock = (struct socket *)arg;
603 struct sockaddr_storage ss;
604 struct timeval backoff;
605 struct request *c = NULL;
606 socklen_t len;
607 int s;
609 backoff.tv_sec = 1;
610 backoff.tv_usec = 0;
612 event_add(&sock->ev, NULL);
613 if (event & EV_TIMEOUT)
614 return;
616 len = sizeof(ss);
618 s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
619 FD_RESERVE, &cgi_inflight);
621 if (s == -1) {
622 switch (errno) {
623 case EINTR:
624 case EWOULDBLOCK:
625 case ECONNABORTED:
626 return;
627 case EMFILE:
628 case ENFILE:
629 event_del(&sock->ev);
630 evtimer_add(&sock->pause, &backoff);
631 return;
632 default:
633 log_warn("%s: accept", __func__);
637 if (client_cnt > GOTWEBD_MAXCLIENTS)
638 goto err;
640 c = calloc(1, sizeof(struct request));
641 if (c == NULL) {
642 log_warn("%s", __func__);
643 close(s);
644 cgi_inflight--;
645 return;
648 c->tp = template(c, &fcgi_write, c->outbuf, sizeof(c->outbuf));
649 if (c->tp == NULL) {
650 log_warn("%s", __func__);
651 close(s);
652 cgi_inflight--;
653 free(c);
654 return;
657 c->fd = s;
658 c->sock = sock;
659 memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
660 c->buf_pos = 0;
661 c->buf_len = 0;
662 c->request_started = 0;
663 c->sock->client_status = CLIENT_CONNECT;
665 event_set(&c->ev, s, EV_READ|EV_PERSIST, fcgi_request, c);
666 event_add(&c->ev, NULL);
668 evtimer_set(&c->tmo, fcgi_timeout, c);
669 evtimer_add(&c->tmo, &timeout);
671 client_cnt++;
673 return;
674 err:
675 cgi_inflight--;
676 close(s);
677 if (c != NULL)
678 free(c);
681 static void
682 sockets_rlimit(int maxfd)
684 struct rlimit rl;
686 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
687 fatal("%s: failed to get resource limit", __func__);
688 log_debug("%s: max open files %llu", __func__,
689 (unsigned long long)rl.rlim_max);
692 * Allow the maximum number of open file descriptors for this
693 * login class (which should be the class "daemon" by default).
695 if (maxfd == -1)
696 rl.rlim_cur = rl.rlim_max;
697 else
698 rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
699 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
700 fatal("%s: failed to set resource limit", __func__);