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 <limits.h>
41 #include <netdb.h>
42 #include <poll.h>
43 #include <pwd.h>
44 #include <stddef.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
50 #include "got_error.h"
51 #include "got_opentemp.h"
53 #include "proc.h"
54 #include "gotwebd.h"
56 #include "got_compat.h"
58 #define SOCKS_BACKLOG 5
59 #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
62 volatile int client_cnt;
64 struct timeval timeout = { TIMEOUT_DEFAULT, 0 };
66 void sockets_sighdlr(int, short, void *);
67 void sockets_run(struct privsep *, struct privsep_proc *, void *);
68 void sockets_launch(void);
69 void sockets_purge(struct gotwebd *);
70 void sockets_accept_paused(int, short, void *);
71 void sockets_dup_new_socket(struct socket *, struct socket *);
72 void sockets_rlimit(int);
75 int sockets_dispatch_gotwebd(int, struct privsep_proc *, struct imsg *);
76 int sockets_unix_socket_listen(struct privsep *, struct socket *);
77 int sockets_create_socket(struct addresslist *, in_port_t);
78 int sockets_socket_af(struct sockaddr_storage *, in_port_t);
79 int sockets_accept_reserve(int, struct sockaddr *, socklen_t *, int,
80 volatile int *);
82 struct socket
83 *sockets_conf_new_socket(struct gotwebd *, struct server *, int, int,
84 int);
86 int cgi_inflight = 0;
88 static struct privsep_proc procs[] = {
89 { "gotwebd", PROC_GOTWEBD, sockets_dispatch_gotwebd },
90 };
92 void
93 sockets(struct privsep *ps, struct privsep_proc *p)
94 {
95 proc_run(ps, p, procs, nitems(procs), sockets_run, NULL);
96 }
98 void
99 sockets_run(struct privsep *ps, struct privsep_proc *p, void *arg)
101 if (config_init(ps->ps_env) == -1)
102 fatal("failed to initialize configuration");
104 p->p_shutdown = sockets_shutdown;
106 sockets_rlimit(-1);
108 signal_del(&ps->ps_evsigchld);
109 signal_set(&ps->ps_evsigchld, SIGCHLD, sockets_sighdlr, ps);
110 signal_add(&ps->ps_evsigchld, NULL);
112 #ifndef PROFILE
113 if (pledge("stdio rpath wpath cpath inet recvfd proc exec sendfd",
114 NULL) == -1)
115 fatal("pledge");
116 #endif
119 void
120 sockets_parse_sockets(struct gotwebd *env)
122 struct server *srv;
123 struct socket *sock, *new_sock = NULL;
124 struct address *a;
125 int sock_id = 0, ipv4 = 0, ipv6 = 0;
127 TAILQ_FOREACH(srv, env->servers, entry) {
128 if (srv->unix_socket) {
129 sock_id++;
130 new_sock = sockets_conf_new_socket(env, srv,
131 sock_id, UNIX, 0);
132 TAILQ_INSERT_TAIL(env->sockets, new_sock, entry);
135 if (srv->fcgi_socket) {
136 sock_id++;
137 new_sock = sockets_conf_new_socket(env, srv,
138 sock_id, FCGI, 0);
139 TAILQ_INSERT_TAIL(env->sockets, new_sock, entry);
141 /* add ipv6 children */
142 TAILQ_FOREACH(sock, env->sockets, entry) {
143 ipv4 = ipv6 = 0;
145 TAILQ_FOREACH(a, sock->conf.al, entry) {
146 if (a->ss.ss_family == AF_INET)
147 ipv4 = 1;
148 if (a->ss.ss_family == AF_INET6)
149 ipv6 = 1;
152 /* create ipv6 sock */
153 if (ipv4 == 1 && ipv6 == 1) {
154 sock_id++;
155 sock->conf.child_id = sock_id;
156 new_sock = sockets_conf_new_socket(env,
157 srv, sock_id, FCGI, 1);
158 sockets_dup_new_socket(sock, new_sock);
159 TAILQ_INSERT_TAIL(env->sockets,
160 new_sock, entry);
161 continue;
168 void
169 sockets_dup_new_socket(struct socket *p_sock, struct socket *sock)
171 struct address *a, *acp;
172 int n;
174 sock->conf.parent_id = p_sock->conf.id;
175 sock->conf.ipv4 = 0;
176 sock->conf.ipv6 = 1;
178 memcpy(&sock->conf.srv_name, p_sock->conf.srv_name,
179 sizeof(sock->conf.srv_name));
181 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_child",
182 p_sock->conf.srv_name);
183 if (n < 0) {
184 free(p_sock->conf.al);
185 free(p_sock);
186 free(sock->conf.al);
187 free(sock);
188 fatalx("%s: snprintf", __func__);
191 TAILQ_FOREACH(a, p_sock->conf.al, entry) {
192 if (a->ss.ss_family == AF_INET)
193 continue;
195 if ((acp = calloc(1, sizeof(*acp))) == NULL)
196 fatal("%s: calloc", __func__);
197 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
198 acp->ipproto = a->ipproto;
199 acp->prefixlen = a->prefixlen;
200 acp->port = a->port;
201 if (strlen(a->ifname) != 0) {
202 if (strlcpy(acp->ifname, a->ifname,
203 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
204 fatalx("%s: interface name truncated",
205 __func__);
209 TAILQ_INSERT_TAIL(sock->conf.al, acp, entry);
213 struct socket *
214 sockets_conf_new_socket(struct gotwebd *env, struct server *srv, int id,
215 int type, int is_dup)
217 struct socket *sock;
218 struct address *a, *acp;
219 int n;
221 if ((sock = calloc(1, sizeof(*sock))) == NULL)
222 fatalx("%s: calloc", __func__);
224 if ((sock->conf.al = calloc(1, sizeof(*sock->conf.al))) == NULL) {
225 free(sock);
226 fatalx("%s: calloc", __func__);
228 TAILQ_INIT(sock->conf.al);
230 sock->conf.parent_id = 0;
231 sock->conf.id = id;
233 sock->fd = -1;
235 sock->conf.type = type;
237 if (type == UNIX) {
238 if (strlcpy(sock->conf.unix_socket_name,
239 srv->unix_socket_name,
240 sizeof(sock->conf.unix_socket_name)) >=
241 sizeof(sock->conf.unix_socket_name)) {
242 free(sock->conf.al);
243 free(sock);
244 fatalx("%s: strlcpy", __func__);
246 } else
247 sock->conf.ipv4 = 1;
249 sock->conf.fcgi_socket_port = srv->fcgi_socket_port;
251 if (is_dup)
252 goto done;
254 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
255 srv->name);
256 if (n < 0) {
257 free(sock->conf.al);
258 free(sock);
259 fatalx("%s: snprintf", __func__);
262 if (strlcpy(sock->conf.srv_name, srv->name,
263 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
264 free(sock->conf.al);
265 free(sock);
266 fatalx("%s: strlcpy", __func__);
269 TAILQ_FOREACH(a, srv->al, entry) {
270 if ((acp = calloc(1, sizeof(*acp))) == NULL) {
271 free(sock->conf.al);
272 free(sock);
273 fatal("%s: calloc", __func__);
275 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
276 acp->ipproto = a->ipproto;
277 acp->prefixlen = a->prefixlen;
278 acp->port = a->port;
279 if (strlen(a->ifname) != 0) {
280 if (strlcpy(acp->ifname, a->ifname,
281 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
282 fatalx("%s: interface name truncated",
283 __func__);
287 TAILQ_INSERT_TAIL(sock->conf.al, acp, entry);
289 done:
290 return (sock);
293 int
294 sockets_socket_af(struct sockaddr_storage *ss, in_port_t port)
296 switch (ss->ss_family) {
297 case AF_INET:
298 ((struct sockaddr_in *)ss)->sin_port = port;
299 /* TA: Iffy... */
300 #ifndef __linux__
301 ((struct sockaddr_in *)ss)->sin_len =
302 sizeof(struct sockaddr_in);
303 #endif
304 break;
305 case AF_INET6:
306 ((struct sockaddr_in6 *)ss)->sin6_port = port;
307 /* TA: Iffy... */
308 #ifndef __linux__
309 ((struct sockaddr_in6 *)ss)->sin6_len =
310 sizeof(struct sockaddr_in6);
311 #endif
312 break;
313 default:
314 return -1;
317 return 0;
320 void
321 sockets_launch(void)
323 struct socket *sock;
325 TAILQ_FOREACH(sock, gotwebd_env->sockets, entry) {
326 log_debug("%s: configuring socket %d (%d)", __func__,
327 sock->conf.id, sock->fd);
329 event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
330 sockets_socket_accept, sock);
332 if (event_add(&sock->ev, NULL))
333 fatalx("event add sock");
335 evtimer_set(&sock->pause, sockets_accept_paused, sock);
337 log_debug("%s: running socket listener %d", __func__,
338 sock->conf.id);
342 void
343 sockets_purge(struct gotwebd *env)
345 struct socket *sock, *tsock;
347 /* shutdown and remove sockets */
348 TAILQ_FOREACH_SAFE(sock, env->sockets, entry, tsock) {
349 if (event_initialized(&sock->ev))
350 event_del(&sock->ev);
351 if (evtimer_initialized(&sock->evt))
352 evtimer_del(&sock->evt);
353 if (evtimer_initialized(&sock->pause))
354 evtimer_del(&sock->pause);
355 if (sock->fd != -1)
356 close(sock->fd);
357 TAILQ_REMOVE(env->sockets, sock, entry);
361 int
362 sockets_dispatch_gotwebd(int fd, struct privsep_proc *p, struct imsg *imsg)
364 struct privsep *ps = p->p_ps;
365 int res = 0, cmd = 0, verbose;
367 switch (imsg->hdr.type) {
368 case IMSG_CFG_SRV:
369 config_getserver(gotwebd_env, imsg);
370 break;
371 case IMSG_CFG_SOCK:
372 config_getsock(gotwebd_env, imsg);
373 break;
374 case IMSG_CFG_FD:
375 config_getfd(gotwebd_env, imsg);
376 break;
377 case IMSG_CFG_DONE:
378 config_getcfg(gotwebd_env, imsg);
379 break;
380 case IMSG_CTL_START:
381 sockets_launch();
382 break;
383 case IMSG_CTL_VERBOSE:
384 IMSG_SIZE_CHECK(imsg, &verbose);
385 memcpy(&verbose, imsg->data, sizeof(verbose));
386 log_setverbose(verbose);
387 break;
388 default:
389 return -1;
392 switch (cmd) {
393 case 0:
394 break;
395 default:
396 if (proc_compose_imsg(ps, PROC_GOTWEBD, -1, cmd,
397 imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
398 return -1;
399 break;
402 return 0;
405 void
406 sockets_sighdlr(int sig, short event, void *arg)
408 switch (sig) {
409 case SIGHUP:
410 log_info("%s: ignoring SIGHUP", __func__);
411 break;
412 case SIGPIPE:
413 log_info("%s: ignoring SIGPIPE", __func__);
414 break;
415 case SIGUSR1:
416 log_info("%s: ignoring SIGUSR1", __func__);
417 break;
418 case SIGCHLD:
419 break;
420 default:
421 log_info("SIGNAL: %d", sig);
422 fatalx("unexpected signal");
426 void
427 sockets_shutdown(void)
429 struct server *srv, *tsrv;
430 struct socket *sock, *tsock;
432 sockets_purge(gotwebd_env);
434 /* clean sockets */
435 TAILQ_FOREACH_SAFE(sock, gotwebd_env->sockets, entry, tsock) {
436 TAILQ_REMOVE(gotwebd_env->sockets, sock, entry);
437 close(sock->fd);
438 free(sock);
441 /* clean servers */
442 TAILQ_FOREACH_SAFE(srv, gotwebd_env->servers, entry, tsrv)
443 free(srv);
445 free(gotwebd_env->sockets);
446 free(gotwebd_env->servers);
447 free(gotwebd_env);
450 int
451 sockets_privinit(struct gotwebd *env, struct socket *sock)
453 struct privsep *ps = env->gotwebd_ps;
455 if (sock->conf.type == UNIX) {
456 log_debug("%s: initializing unix socket %s", __func__,
457 sock->conf.unix_socket_name);
458 sock->fd = sockets_unix_socket_listen(ps, sock);
459 if (sock->fd == -1) {
460 log_warnx("%s: create unix socket failed", __func__);
461 return -1;
465 if (sock->conf.type == FCGI) {
466 log_debug("%s: initializing fcgi socket for %s", __func__,
467 sock->conf.name);
468 sock->fd = sockets_create_socket(sock->conf.al,
469 sock->conf.fcgi_socket_port);
470 if (sock->fd == -1) {
471 log_warnx("%s: create unix socket failed", __func__);
472 return -1;
476 return 0;
479 int
480 sockets_unix_socket_listen(struct privsep *ps, struct socket *sock)
482 struct gotwebd *env = ps->ps_env;
483 struct sockaddr_un sun;
484 struct socket *tsock;
485 int u_fd = -1;
486 mode_t old_umask, mode;
488 TAILQ_FOREACH(tsock, env->sockets, entry) {
489 if (strcmp(tsock->conf.unix_socket_name,
490 sock->conf.unix_socket_name) == 0 &&
491 tsock->fd != -1)
492 return (tsock->fd);
495 /* TA: FIXME: this needs upstreaming. */
496 int socket_flags = SOCK_STREAM | SOCK_NONBLOCK;
497 #ifdef SOCK_CLOEXEC
498 socket_flags |= SOCK_CLOEXEC;
499 #endif
500 u_fd = socket(AF_UNIX, socket_flags, 0);
501 if (u_fd == -1) {
502 log_warn("%s: socket", __func__);
503 return -1;
506 sun.sun_family = AF_UNIX;
507 if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
508 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
509 log_warn("%s: %s name too long", __func__,
510 sock->conf.unix_socket_name);
511 close(u_fd);
512 return -1;
515 if (unlink(sock->conf.unix_socket_name) == -1) {
516 if (errno != ENOENT) {
517 log_warn("%s: unlink %s", __func__,
518 sock->conf.unix_socket_name);
519 close(u_fd);
520 return -1;
524 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
525 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
527 if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
528 log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
529 close(u_fd);
530 (void)umask(old_umask);
531 return -1;
534 (void)umask(old_umask);
536 if (chmod(sock->conf.unix_socket_name, mode) == -1) {
537 log_warn("%s: chmod", __func__);
538 close(u_fd);
539 (void)unlink(sock->conf.unix_socket_name);
540 return -1;
543 if (chown(sock->conf.unix_socket_name, ps->ps_pw->pw_uid,
544 ps->ps_pw->pw_gid) == -1) {
545 log_warn("%s: chown", __func__);
546 close(u_fd);
547 (void)unlink(sock->conf.unix_socket_name);
548 return -1;
551 if (listen(u_fd, SOCKS_BACKLOG) == -1) {
552 log_warn("%s: listen", __func__);
553 return -1;
556 return u_fd;
559 int
560 sockets_create_socket(struct addresslist *al, in_port_t port)
562 struct addrinfo hints;
563 struct address *a;
564 int fd = -1, o_val = 1, flags;
566 memset(&hints, 0, sizeof(hints));
567 hints.ai_family = AF_UNSPEC;
568 hints.ai_socktype = SOCK_STREAM;
569 hints.ai_flags |= AI_PASSIVE;
571 TAILQ_FOREACH(a, al, entry) {
572 if (sockets_socket_af(&a->ss, port) == -1) {
573 log_warnx("%s: sockets_socket_af", __func__);
574 goto fail;
577 fd = socket(a->ss.ss_family, hints.ai_socktype,
578 a->ipproto);
579 log_debug("%s: opening socket (%d) for %s", __func__,
580 fd, a->ifname);
582 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
583 sizeof(int)) == -1) {
584 log_warn("%s: setsockopt error", __func__);
585 return -1;
588 /* non-blocking */
589 flags = fcntl(fd, F_GETFL);
590 flags |= O_NONBLOCK;
591 fcntl(fd, F_SETFL, flags);
593 /* TA: 'sizeof a' vs a->ss.len */
594 if (bind(fd, (struct sockaddr *)&a->ss, sizeof a) == -1) {
595 close(fd);
596 log_info("%s: can't bind to port %d", __func__,
597 ntohs(port));
598 goto fail;
601 if (listen(fd, SOMAXCONN) == -1) {
602 log_warn("%s, unable to listen on socket", __func__);
603 goto fail;
607 free(a);
608 return (fd);
609 fail:
610 free(a);
611 return -1;
614 int
615 sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
616 int reserve, volatile int *counter)
618 int ret;
620 if (getdtablecount() + reserve +
621 ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
622 log_debug("inflight fds exceeded");
623 errno = EMFILE;
624 return -1;
626 /* TA: This needs fixing upstream. */
627 #ifdef __APPLE__
628 ret = accept(sockfd, addr, addrlen);
629 #else
630 ret = accept4(sockfd, addr, addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC);
631 #endif
633 if (ret > -1) {
634 (*counter)++;
635 log_debug("inflight incremented, now %d", *counter);
638 return ret;
641 void
642 sockets_accept_paused(int fd, short events, void *arg)
644 struct socket *sock = (struct socket *)arg;
646 event_add(&sock->ev, NULL);
649 void
650 sockets_socket_accept(int fd, short event, void *arg)
652 struct socket *sock = (struct socket *)arg;
653 struct sockaddr_storage ss;
654 struct timeval backoff;
655 struct request *c = NULL;
656 socklen_t len;
657 int s;
659 backoff.tv_sec = 1;
660 backoff.tv_usec = 0;
662 event_add(&sock->ev, NULL);
663 if (event & EV_TIMEOUT)
664 return;
666 len = sizeof(ss);
668 s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
669 FD_RESERVE, &cgi_inflight);
671 if (s == -1) {
672 switch (errno) {
673 case EINTR:
674 case EWOULDBLOCK:
675 case ECONNABORTED:
676 return;
677 case EMFILE:
678 case ENFILE:
679 event_del(&sock->ev);
680 evtimer_add(&sock->pause, &backoff);
681 return;
682 default:
683 log_warn("%s: accept", __func__);
687 if (client_cnt > GOTWEBD_MAXCLIENTS)
688 goto err;
690 c = calloc(1, sizeof(struct request));
691 if (c == NULL) {
692 log_warn("%s", __func__);
693 close(s);
694 cgi_inflight--;
695 return;
698 c->fd = s;
699 c->sock = sock;
700 memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
701 c->buf_pos = 0;
702 c->buf_len = 0;
703 c->request_started = 0;
704 c->sock->client_status = CLIENT_CONNECT;
706 event_set(&c->ev, s, EV_READ, fcgi_request, c);
707 event_add(&c->ev, NULL);
709 evtimer_set(&c->tmo, fcgi_timeout, c);
710 evtimer_add(&c->tmo, &timeout);
712 client_cnt++;
714 return;
715 err:
716 cgi_inflight--;
717 close(s);
718 if (c != NULL)
719 free(c);
722 void
723 sockets_rlimit(int maxfd)
725 struct rlimit rl;
727 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
728 fatal("%s: failed to get resource limit", __func__);
729 log_debug("%s: max open files %llu", __func__,
730 (unsigned long long)rl.rlim_max);
732 /*
733 * Allow the maximum number of open file descriptors for this
734 * login class (which should be the class "daemon" by default).
735 */
736 if (maxfd == -1)
737 rl.rlim_cur = rl.rlim_max;
738 else
739 rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
740 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
741 fatal("%s: failed to set resource limit", __func__);