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 <sys/param.h>
21 #include <sys/ioctl.h>
22 #include <sys/queue.h>
23 #include <sys/wait.h>
24 #include <sys/uio.h>
25 #include <sys/resource.h>
26 #include <sys/socket.h>
27 #include <sys/stat.h>
28 #include <sys/time.h>
29 #include <sys/types.h>
30 #include <sys/mman.h>
31 #include <sys/un.h>
33 #include <net/if.h>
34 #include <netinet/in.h>
36 #include <errno.h>
37 #include <event.h>
38 #include <fcntl.h>
39 #include <ifaddrs.h>
40 #include <imsg.h>
41 #include <limits.h>
42 #include <netdb.h>
43 #include <poll.h>
44 #include <pwd.h>
45 #include <stddef.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <util.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 },
93 };
95 void
96 sockets(struct privsep *ps, struct privsep_proc *p)
97 {
98 proc_run(ps, p, procs, nitems(procs), sockets_run, NULL);
99 }
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);
392 int
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 u_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
439 if (u_fd == -1) {
440 log_warn("%s: socket", __func__);
441 return -1;
444 sun.sun_family = AF_UNIX;
445 if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
446 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
447 log_warn("%s: %s name too long", __func__,
448 sock->conf.unix_socket_name);
449 close(u_fd);
450 return -1;
453 if (unlink(sock->conf.unix_socket_name) == -1) {
454 if (errno != ENOENT) {
455 log_warn("%s: unlink %s", __func__,
456 sock->conf.unix_socket_name);
457 close(u_fd);
458 return -1;
462 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
463 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
465 if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
466 log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
467 close(u_fd);
468 (void)umask(old_umask);
469 return -1;
472 (void)umask(old_umask);
474 if (chmod(sock->conf.unix_socket_name, mode) == -1) {
475 log_warn("%s: chmod", __func__);
476 close(u_fd);
477 (void)unlink(sock->conf.unix_socket_name);
478 return -1;
481 if (chown(sock->conf.unix_socket_name, ps->ps_pw->pw_uid,
482 ps->ps_pw->pw_gid) == -1) {
483 log_warn("%s: chown", __func__);
484 close(u_fd);
485 (void)unlink(sock->conf.unix_socket_name);
486 return -1;
489 if (listen(u_fd, SOCKS_BACKLOG) == -1) {
490 log_warn("%s: listen", __func__);
491 return -1;
494 return u_fd;
497 static int
498 sockets_create_socket(struct address *a, in_port_t port)
500 struct addrinfo hints;
501 int fd = -1, o_val = 1, flags;
503 memset(&hints, 0, sizeof(hints));
504 hints.ai_family = AF_UNSPEC;
505 hints.ai_socktype = SOCK_STREAM;
506 hints.ai_flags |= AI_PASSIVE;
508 switch (a->ss.ss_family) {
509 case AF_INET:
510 ((struct sockaddr_in *)(&a->ss))->sin_port = htons(port);
511 break;
512 case AF_INET6:
513 ((struct sockaddr_in6 *)(&a->ss))->sin6_port = htons(port);
514 break;
515 default:
516 log_warnx("%s: unknown address family", __func__);
517 return -1;
520 fd = socket(a->ss.ss_family, hints.ai_socktype, a->ipproto);
521 if (fd == -1)
522 return -1;
524 log_debug("%s: opened socket (%d) for %s", __func__,
525 fd, a->ifname);
527 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
528 sizeof(int)) == -1) {
529 log_warn("%s: setsockopt error", __func__);
530 close(fd);
531 return -1;
534 /* non-blocking */
535 flags = fcntl(fd, F_GETFL);
536 flags |= O_NONBLOCK;
537 if (fcntl(fd, F_SETFL, flags) == -1) {
538 log_info("%s: could not enable non-blocking I/O", __func__);
539 close(fd);
540 return -1;
543 if (bind(fd, (struct sockaddr *)&a->ss, a->ss.ss_len) == -1) {
544 close(fd);
545 log_info("%s: can't bind to port %d", __func__,
546 ntohs(port));
547 return -1;
550 if (listen(fd, SOMAXCONN) == -1) {
551 log_warn("%s, unable to listen on socket", __func__);
552 close(fd);
553 return -1;
556 return (fd);
559 static int
560 sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
561 int reserve, volatile int *counter)
563 int ret;
565 if (getdtablecount() + reserve +
566 ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
567 log_debug("inflight fds exceeded");
568 errno = EMFILE;
569 return -1;
572 if ((ret = accept4(sockfd, addr, addrlen,
573 SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
574 (*counter)++;
575 log_debug("inflight incremented, now %d", *counter);
578 return ret;
581 static void
582 sockets_accept_paused(int fd, short events, void *arg)
584 struct socket *sock = (struct socket *)arg;
586 event_add(&sock->ev, NULL);
589 void
590 sockets_socket_accept(int fd, short event, void *arg)
592 struct socket *sock = (struct socket *)arg;
593 struct sockaddr_storage ss;
594 struct timeval backoff;
595 struct request *c = NULL;
596 socklen_t len;
597 int s;
599 backoff.tv_sec = 1;
600 backoff.tv_usec = 0;
602 event_add(&sock->ev, NULL);
603 if (event & EV_TIMEOUT)
604 return;
606 len = sizeof(ss);
608 s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
609 FD_RESERVE, &cgi_inflight);
611 if (s == -1) {
612 switch (errno) {
613 case EINTR:
614 case EWOULDBLOCK:
615 case ECONNABORTED:
616 return;
617 case EMFILE:
618 case ENFILE:
619 event_del(&sock->ev);
620 evtimer_add(&sock->pause, &backoff);
621 return;
622 default:
623 log_warn("%s: accept", __func__);
627 if (client_cnt > GOTWEBD_MAXCLIENTS)
628 goto err;
630 c = calloc(1, sizeof(struct request));
631 if (c == NULL) {
632 log_warn("%s", __func__);
633 close(s);
634 cgi_inflight--;
635 return;
638 c->tp = template(c, &fcgi_write, c->outbuf, sizeof(c->outbuf));
639 if (c->tp == NULL) {
640 log_warn("%s", __func__);
641 close(s);
642 cgi_inflight--;
643 free(c);
644 return;
647 c->fd = s;
648 c->sock = sock;
649 memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
650 c->buf_pos = 0;
651 c->buf_len = 0;
652 c->request_started = 0;
653 c->sock->client_status = CLIENT_CONNECT;
655 event_set(&c->ev, s, EV_READ|EV_PERSIST, fcgi_request, c);
656 event_add(&c->ev, NULL);
658 evtimer_set(&c->tmo, fcgi_timeout, c);
659 evtimer_add(&c->tmo, &timeout);
661 client_cnt++;
663 return;
664 err:
665 cgi_inflight--;
666 close(s);
667 if (c != NULL)
668 free(c);
671 static void
672 sockets_rlimit(int maxfd)
674 struct rlimit rl;
676 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
677 fatal("%s: failed to get resource limit", __func__);
678 log_debug("%s: max open files %llu", __func__,
679 (unsigned long long)rl.rlim_max);
681 /*
682 * Allow the maximum number of open file descriptors for this
683 * login class (which should be the class "daemon" by default).
684 */
685 if (maxfd == -1)
686 rl.rlim_cur = rl.rlim_max;
687 else
688 rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
689 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
690 fatal("%s: failed to set resource limit", __func__);