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_repository.h"
56 #include "proc.h"
57 #include "gotwebd.h"
58 #include "tmpl.h"
60 #define SOCKS_BACKLOG 5
61 #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_run(struct privsep *, struct privsep_proc *, 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 int sockets_dispatch_gotwebd(int, struct privsep_proc *,
76 struct imsg *);
77 static int sockets_unix_socket_listen(struct privsep *, struct socket *);
78 static int sockets_create_socket(struct address *, in_port_t);
79 static int sockets_accept_reserve(int, struct sockaddr *, socklen_t *,
80 int, volatile int *);
82 static struct socket *sockets_conf_new_socket_unix(struct gotwebd *,
83 struct server *, int);
84 static struct socket *sockets_conf_new_socket_fcgi(struct gotwebd *,
85 struct server *, int, struct address *);
87 int cgi_inflight = 0;
89 static struct privsep_proc procs[] = {
90 { "gotwebd", PROC_GOTWEBD, sockets_dispatch_gotwebd },
91 };
93 void
94 sockets(struct privsep *ps, struct privsep_proc *p)
95 {
96 proc_run(ps, p, procs, nitems(procs), sockets_run, NULL);
97 }
99 static void
100 sockets_run(struct privsep *ps, struct privsep_proc *p, void *arg)
102 if (config_init(ps->ps_env) == -1)
103 fatal("failed to initialize configuration");
105 p->p_shutdown = sockets_shutdown;
107 sockets_rlimit(-1);
109 signal_del(&ps->ps_evsigchld);
110 signal_set(&ps->ps_evsigchld, SIGCHLD, sockets_sighdlr, ps);
111 signal_add(&ps->ps_evsigchld, NULL);
113 #ifndef PROFILE
114 if (pledge("stdio rpath wpath cpath inet recvfd proc exec sendfd",
115 NULL) == -1)
116 fatal("pledge");
117 #endif
120 void
121 sockets_parse_sockets(struct gotwebd *env)
123 struct server *srv;
124 struct address *a;
125 struct socket *new_sock = NULL;
126 int sock_id = 1;
128 TAILQ_FOREACH(srv, &env->servers, entry) {
129 if (srv->unix_socket) {
130 new_sock = sockets_conf_new_socket_unix(env, srv,
131 sock_id);
132 if (new_sock) {
133 sock_id++;
134 TAILQ_INSERT_TAIL(&env->sockets, new_sock,
135 entry);
139 if (srv->fcgi_socket) {
140 if (TAILQ_EMPTY(&srv->al)) {
141 fatalx("%s: server %s has no IP addresses to "
142 "listen for FCGI connections", __func__,
143 srv->name);
145 TAILQ_FOREACH(a, &srv->al, entry) {
146 if (a->ss.ss_family != AF_INET &&
147 a->ss.ss_family != AF_INET6)
148 continue;
149 new_sock = sockets_conf_new_socket_fcgi(env,
150 srv, sock_id, a);
151 if (new_sock) {
152 sock_id++;
153 TAILQ_INSERT_TAIL(&env->sockets,
154 new_sock, entry);
161 static struct socket *
162 sockets_conf_new_socket_unix(struct gotwebd *env, struct server *srv, int id)
164 struct socket *sock;
165 int n;
167 if ((sock = calloc(1, sizeof(*sock))) == NULL)
168 fatalx("%s: calloc", __func__);
170 sock->conf.id = id;
171 sock->fd = -1;
172 sock->conf.af_type = AF_UNIX;
174 if (strlcpy(sock->conf.unix_socket_name,
175 srv->unix_socket_name,
176 sizeof(sock->conf.unix_socket_name)) >=
177 sizeof(sock->conf.unix_socket_name)) {
178 free(sock);
179 fatalx("%s: strlcpy", __func__);
182 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
183 srv->name);
184 if (n < 0 || (size_t)n >= GOTWEBD_MAXTEXT) {
185 free(sock);
186 fatalx("%s: snprintf", __func__);
189 if (strlcpy(sock->conf.srv_name, srv->name,
190 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
191 free(sock);
192 fatalx("%s: strlcpy", __func__);
195 return sock;
198 static struct socket *
199 sockets_conf_new_socket_fcgi(struct gotwebd *env, struct server *srv, int id,
200 struct address *a)
202 struct socket *sock;
203 struct address *acp;
204 int n;
206 if ((sock = calloc(1, sizeof(*sock))) == NULL)
207 fatalx("%s: calloc", __func__);
209 sock->conf.id = id;
210 sock->fd = -1;
211 sock->conf.af_type = a->ss.ss_family;
213 sock->conf.fcgi_socket_port = a->port;
215 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
216 srv->name);
217 if (n < 0 || (size_t)n >= GOTWEBD_MAXTEXT) {
218 free(sock);
219 fatalx("%s: snprintf", __func__);
222 if (strlcpy(sock->conf.srv_name, srv->name,
223 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
224 free(sock);
225 fatalx("%s: strlcpy", __func__);
228 acp = &sock->conf.addr;
230 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
231 acp->ipproto = a->ipproto;
232 acp->prefixlen = a->prefixlen;
233 acp->port = a->port;
234 if (strlen(a->ifname) != 0) {
235 if (strlcpy(acp->ifname, a->ifname,
236 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
237 fatalx("%s: interface name truncated",
238 __func__);
242 return (sock);
245 static void
246 sockets_launch(void)
248 struct socket *sock;
250 TAILQ_FOREACH(sock, &gotwebd_env->sockets, entry) {
251 log_debug("%s: configuring socket %d (%d)", __func__,
252 sock->conf.id, sock->fd);
254 event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
255 sockets_socket_accept, sock);
257 if (event_add(&sock->ev, NULL))
258 fatalx("event add sock");
260 evtimer_set(&sock->pause, sockets_accept_paused, sock);
262 log_debug("%s: running socket listener %d", __func__,
263 sock->conf.id);
267 static void
268 sockets_purge(struct gotwebd *env)
270 struct socket *sock, *tsock;
272 /* shutdown and remove sockets */
273 TAILQ_FOREACH_SAFE(sock, &env->sockets, entry, tsock) {
274 if (event_initialized(&sock->ev))
275 event_del(&sock->ev);
276 if (evtimer_initialized(&sock->evt))
277 evtimer_del(&sock->evt);
278 if (evtimer_initialized(&sock->pause))
279 evtimer_del(&sock->pause);
280 if (sock->fd != -1)
281 close(sock->fd);
282 TAILQ_REMOVE(&env->sockets, sock, entry);
286 static int
287 sockets_dispatch_gotwebd(int fd, struct privsep_proc *p, struct imsg *imsg)
289 struct privsep *ps = p->p_ps;
290 int res = 0, cmd = 0, verbose;
292 switch (imsg->hdr.type) {
293 case IMSG_CFG_SRV:
294 config_getserver(gotwebd_env, imsg);
295 break;
296 case IMSG_CFG_SOCK:
297 config_getsock(gotwebd_env, imsg);
298 break;
299 case IMSG_CFG_FD:
300 config_getfd(gotwebd_env, imsg);
301 break;
302 case IMSG_CFG_DONE:
303 config_getcfg(gotwebd_env, imsg);
304 break;
305 case IMSG_CTL_START:
306 sockets_launch();
307 break;
308 case IMSG_CTL_VERBOSE:
309 IMSG_SIZE_CHECK(imsg, &verbose);
310 memcpy(&verbose, imsg->data, sizeof(verbose));
311 log_setverbose(verbose);
312 break;
313 default:
314 return -1;
317 switch (cmd) {
318 case 0:
319 break;
320 default:
321 if (proc_compose_imsg(ps, PROC_GOTWEBD, -1, cmd,
322 imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
323 return -1;
324 break;
327 return 0;
330 static void
331 sockets_sighdlr(int sig, short event, void *arg)
333 switch (sig) {
334 case SIGHUP:
335 log_info("%s: ignoring SIGHUP", __func__);
336 break;
337 case SIGPIPE:
338 log_info("%s: ignoring SIGPIPE", __func__);
339 break;
340 case SIGUSR1:
341 log_info("%s: ignoring SIGUSR1", __func__);
342 break;
343 case SIGCHLD:
344 break;
345 default:
346 log_info("SIGNAL: %d", sig);
347 fatalx("unexpected signal");
351 void
352 sockets_shutdown(void)
354 struct server *srv, *tsrv;
355 struct socket *sock, *tsock;
356 int i;
358 sockets_purge(gotwebd_env);
360 /* clean sockets */
361 TAILQ_FOREACH_SAFE(sock, &gotwebd_env->sockets, entry, tsock) {
362 TAILQ_REMOVE(&gotwebd_env->sockets, sock, entry);
363 close(sock->fd);
364 free(sock);
367 /* clean servers */
368 TAILQ_FOREACH_SAFE(srv, &gotwebd_env->servers, entry, tsrv) {
369 for (i = 0; i < srv->ncached_repos; i++)
370 got_repo_close(srv->cached_repos[i].repo);
371 free(srv);
374 free(gotwebd_env);
377 int
378 sockets_privinit(struct gotwebd *env, struct socket *sock)
380 struct privsep *ps = env->gotwebd_ps;
382 if (sock->conf.af_type == AF_UNIX) {
383 log_debug("%s: initializing unix socket %s", __func__,
384 sock->conf.unix_socket_name);
385 sock->fd = sockets_unix_socket_listen(ps, sock);
386 if (sock->fd == -1) {
387 log_warnx("%s: create unix socket failed", __func__);
388 return -1;
392 if (sock->conf.af_type == AF_INET || sock->conf.af_type == AF_INET6) {
393 log_debug("%s: initializing %s FCGI socket on port %d for %s",
394 __func__, sock->conf.af_type == AF_INET ? "inet" : "inet6",
395 sock->conf.fcgi_socket_port, sock->conf.name);
396 sock->fd = sockets_create_socket(&sock->conf.addr,
397 sock->conf.fcgi_socket_port);
398 if (sock->fd == -1) {
399 log_warnx("%s: create FCGI socket failed", __func__);
400 return -1;
404 return 0;
407 static int
408 sockets_unix_socket_listen(struct privsep *ps, struct socket *sock)
410 struct gotwebd *env = ps->ps_env;
411 struct sockaddr_un sun;
412 struct socket *tsock;
413 int u_fd = -1;
414 mode_t old_umask, mode;
416 TAILQ_FOREACH(tsock, &env->sockets, entry) {
417 if (strcmp(tsock->conf.unix_socket_name,
418 sock->conf.unix_socket_name) == 0 &&
419 tsock->fd != -1)
420 return (tsock->fd);
423 u_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
424 if (u_fd == -1) {
425 log_warn("%s: socket", __func__);
426 return -1;
429 sun.sun_family = AF_UNIX;
430 if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
431 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
432 log_warn("%s: %s name too long", __func__,
433 sock->conf.unix_socket_name);
434 close(u_fd);
435 return -1;
438 if (unlink(sock->conf.unix_socket_name) == -1) {
439 if (errno != ENOENT) {
440 log_warn("%s: unlink %s", __func__,
441 sock->conf.unix_socket_name);
442 close(u_fd);
443 return -1;
447 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
448 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
450 if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
451 log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
452 close(u_fd);
453 (void)umask(old_umask);
454 return -1;
457 (void)umask(old_umask);
459 if (chmod(sock->conf.unix_socket_name, mode) == -1) {
460 log_warn("%s: chmod", __func__);
461 close(u_fd);
462 (void)unlink(sock->conf.unix_socket_name);
463 return -1;
466 if (chown(sock->conf.unix_socket_name, ps->ps_pw->pw_uid,
467 ps->ps_pw->pw_gid) == -1) {
468 log_warn("%s: chown", __func__);
469 close(u_fd);
470 (void)unlink(sock->conf.unix_socket_name);
471 return -1;
474 if (listen(u_fd, SOCKS_BACKLOG) == -1) {
475 log_warn("%s: listen", __func__);
476 return -1;
479 return u_fd;
482 static int
483 sockets_create_socket(struct address *a, in_port_t port)
485 struct addrinfo hints;
486 int fd = -1, o_val = 1, flags;
488 memset(&hints, 0, sizeof(hints));
489 hints.ai_family = AF_UNSPEC;
490 hints.ai_socktype = SOCK_STREAM;
491 hints.ai_flags |= AI_PASSIVE;
493 switch (a->ss.ss_family) {
494 case AF_INET:
495 ((struct sockaddr_in *)(&a->ss))->sin_port = htons(port);
496 break;
497 case AF_INET6:
498 ((struct sockaddr_in6 *)(&a->ss))->sin6_port = htons(port);
499 break;
500 default:
501 log_warnx("%s: unknown address family", __func__);
502 return -1;
505 fd = socket(a->ss.ss_family, hints.ai_socktype, a->ipproto);
506 if (fd == -1)
507 return -1;
509 log_debug("%s: opened socket (%d) for %s", __func__,
510 fd, a->ifname);
512 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
513 sizeof(int)) == -1) {
514 log_warn("%s: setsockopt error", __func__);
515 close(fd);
516 return -1;
519 /* non-blocking */
520 flags = fcntl(fd, F_GETFL);
521 flags |= O_NONBLOCK;
522 if (fcntl(fd, F_SETFL, flags) == -1) {
523 log_info("%s: could not enable non-blocking I/O", __func__);
524 close(fd);
525 return -1;
528 if (bind(fd, (struct sockaddr *)&a->ss, a->ss.ss_len) == -1) {
529 close(fd);
530 log_info("%s: can't bind to port %d", __func__,
531 ntohs(port));
532 return -1;
535 if (listen(fd, SOMAXCONN) == -1) {
536 log_warn("%s, unable to listen on socket", __func__);
537 close(fd);
538 return -1;
541 return (fd);
544 static int
545 sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
546 int reserve, volatile int *counter)
548 int ret;
550 if (getdtablecount() + reserve +
551 ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
552 log_debug("inflight fds exceeded");
553 errno = EMFILE;
554 return -1;
557 if ((ret = accept4(sockfd, addr, addrlen,
558 SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
559 (*counter)++;
560 log_debug("inflight incremented, now %d", *counter);
563 return ret;
566 static void
567 sockets_accept_paused(int fd, short events, void *arg)
569 struct socket *sock = (struct socket *)arg;
571 event_add(&sock->ev, NULL);
574 void
575 sockets_socket_accept(int fd, short event, void *arg)
577 struct socket *sock = (struct socket *)arg;
578 struct sockaddr_storage ss;
579 struct timeval backoff;
580 struct request *c = NULL;
581 socklen_t len;
582 int s;
584 backoff.tv_sec = 1;
585 backoff.tv_usec = 0;
587 event_add(&sock->ev, NULL);
588 if (event & EV_TIMEOUT)
589 return;
591 len = sizeof(ss);
593 s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
594 FD_RESERVE, &cgi_inflight);
596 if (s == -1) {
597 switch (errno) {
598 case EINTR:
599 case EWOULDBLOCK:
600 case ECONNABORTED:
601 return;
602 case EMFILE:
603 case ENFILE:
604 event_del(&sock->ev);
605 evtimer_add(&sock->pause, &backoff);
606 return;
607 default:
608 log_warn("%s: accept", __func__);
612 if (client_cnt > GOTWEBD_MAXCLIENTS)
613 goto err;
615 c = calloc(1, sizeof(struct request));
616 if (c == NULL) {
617 log_warn("%s", __func__);
618 close(s);
619 cgi_inflight--;
620 return;
623 c->tp = template(c, fcgi_puts, fcgi_putc);
624 if (c->tp == NULL) {
625 log_warn("%s", __func__);
626 close(s);
627 cgi_inflight--;
628 free(c);
629 return;
632 c->fd = s;
633 c->sock = sock;
634 memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
635 c->buf_pos = 0;
636 c->buf_len = 0;
637 c->request_started = 0;
638 c->sock->client_status = CLIENT_CONNECT;
640 event_set(&c->ev, s, EV_READ, fcgi_request, c);
641 event_add(&c->ev, NULL);
643 evtimer_set(&c->tmo, fcgi_timeout, c);
644 evtimer_add(&c->tmo, &timeout);
646 client_cnt++;
648 return;
649 err:
650 cgi_inflight--;
651 close(s);
652 if (c != NULL)
653 free(c);
656 static void
657 sockets_rlimit(int maxfd)
659 struct rlimit rl;
661 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
662 fatal("%s: failed to get resource limit", __func__);
663 log_debug("%s: max open files %llu", __func__,
664 (unsigned long long)rl.rlim_max);
666 /*
667 * Allow the maximum number of open file descriptors for this
668 * login class (which should be the class "daemon" by default).
669 */
670 if (maxfd == -1)
671 rl.rlim_cur = rl.rlim_max;
672 else
673 rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
674 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
675 fatal("%s: failed to set resource limit", __func__);