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_accept_reserve(int, struct sockaddr *, socklen_t *, int,
79 volatile int *);
81 struct socket
82 *sockets_conf_new_socket(struct gotwebd *, struct server *, int, int,
83 int);
85 int cgi_inflight = 0;
87 static struct privsep_proc procs[] = {
88 { "gotwebd", PROC_GOTWEBD, sockets_dispatch_gotwebd },
89 };
91 void
92 sockets(struct privsep *ps, struct privsep_proc *p)
93 {
94 proc_run(ps, p, procs, nitems(procs), sockets_run, NULL);
95 }
97 void
98 sockets_run(struct privsep *ps, struct privsep_proc *p, void *arg)
99 {
100 if (config_init(ps->ps_env) == -1)
101 fatal("failed to initialize configuration");
103 p->p_shutdown = sockets_shutdown;
105 sockets_rlimit(-1);
107 signal_del(&ps->ps_evsigchld);
108 signal_set(&ps->ps_evsigchld, SIGCHLD, sockets_sighdlr, ps);
109 signal_add(&ps->ps_evsigchld, NULL);
111 #ifndef PROFILE
112 if (pledge("stdio rpath wpath cpath inet recvfd proc exec sendfd",
113 NULL) == -1)
114 fatal("pledge");
115 #endif
118 void
119 sockets_parse_sockets(struct gotwebd *env)
121 struct server *srv;
122 struct socket *sock, *new_sock = NULL;
123 struct address *a;
124 int sock_id = 0, ipv4 = 0, ipv6 = 0;
126 TAILQ_FOREACH(srv, env->servers, entry) {
127 if (srv->unix_socket) {
128 sock_id++;
129 new_sock = sockets_conf_new_socket(env, srv,
130 sock_id, UNIX, 0);
131 TAILQ_INSERT_TAIL(env->sockets, new_sock, entry);
134 if (srv->fcgi_socket) {
135 sock_id++;
136 new_sock = sockets_conf_new_socket(env, srv,
137 sock_id, FCGI, 0);
138 TAILQ_INSERT_TAIL(env->sockets, new_sock, entry);
140 /* add ipv6 children */
141 TAILQ_FOREACH(sock, env->sockets, entry) {
142 ipv4 = ipv6 = 0;
144 TAILQ_FOREACH(a, sock->conf.al, entry) {
145 if (a->ss.ss_family == AF_INET)
146 ipv4 = 1;
147 if (a->ss.ss_family == AF_INET6)
148 ipv6 = 1;
151 /* create ipv6 sock */
152 if (ipv4 == 1 && ipv6 == 1) {
153 sock_id++;
154 sock->conf.child_id = sock_id;
155 new_sock = sockets_conf_new_socket(env,
156 srv, sock_id, FCGI, 1);
157 sockets_dup_new_socket(sock, new_sock);
158 TAILQ_INSERT_TAIL(env->sockets,
159 new_sock, entry);
160 continue;
167 void
168 sockets_dup_new_socket(struct socket *p_sock, struct socket *sock)
170 struct address *a, *acp;
171 int n;
173 sock->conf.parent_id = p_sock->conf.id;
174 sock->conf.ipv4 = 0;
175 sock->conf.ipv6 = 1;
177 memcpy(&sock->conf.srv_name, p_sock->conf.srv_name,
178 sizeof(sock->conf.srv_name));
180 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_child",
181 p_sock->conf.srv_name);
182 if (n < 0) {
183 free(p_sock->conf.al);
184 free(p_sock);
185 free(sock->conf.al);
186 free(sock);
187 fatalx("%s: snprintf", __func__);
190 TAILQ_FOREACH(a, p_sock->conf.al, entry) {
191 if (a->ss.ss_family == AF_INET)
192 continue;
194 if ((acp = calloc(1, sizeof(*acp))) == NULL)
195 fatal("%s: calloc", __func__);
196 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
197 acp->ipproto = a->ipproto;
198 acp->prefixlen = a->prefixlen;
199 acp->port = a->port;
200 if (strlen(a->ifname) != 0) {
201 if (strlcpy(acp->ifname, a->ifname,
202 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
203 fatalx("%s: interface name truncated",
204 __func__);
208 TAILQ_INSERT_TAIL(sock->conf.al, acp, entry);
212 struct socket *
213 sockets_conf_new_socket(struct gotwebd *env, struct server *srv, int id,
214 int type, int is_dup)
216 struct socket *sock;
217 struct address *a, *acp;
218 int n;
220 if ((sock = calloc(1, sizeof(*sock))) == NULL)
221 fatalx("%s: calloc", __func__);
223 if ((sock->conf.al = calloc(1, sizeof(*sock->conf.al))) == NULL) {
224 free(sock);
225 fatalx("%s: calloc", __func__);
227 TAILQ_INIT(sock->conf.al);
229 sock->conf.parent_id = 0;
230 sock->conf.id = id;
232 sock->fd = -1;
234 sock->conf.type = type;
236 if (type == UNIX) {
237 if (strlcpy(sock->conf.unix_socket_name,
238 srv->unix_socket_name,
239 sizeof(sock->conf.unix_socket_name)) >=
240 sizeof(sock->conf.unix_socket_name)) {
241 free(sock->conf.al);
242 free(sock);
243 fatalx("%s: strlcpy", __func__);
245 } else
246 sock->conf.ipv4 = 1;
248 sock->conf.fcgi_socket_port = srv->fcgi_socket_port;
250 if (is_dup)
251 goto done;
253 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
254 srv->name);
255 if (n < 0) {
256 free(sock->conf.al);
257 free(sock);
258 fatalx("%s: snprintf", __func__);
261 if (strlcpy(sock->conf.srv_name, srv->name,
262 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
263 free(sock->conf.al);
264 free(sock);
265 fatalx("%s: strlcpy", __func__);
268 TAILQ_FOREACH(a, srv->al, entry) {
269 if ((acp = calloc(1, sizeof(*acp))) == NULL) {
270 free(sock->conf.al);
271 free(sock);
272 fatal("%s: calloc", __func__);
274 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
275 acp->ipproto = a->ipproto;
276 acp->prefixlen = a->prefixlen;
277 acp->port = a->port;
278 if (strlen(a->ifname) != 0) {
279 if (strlcpy(acp->ifname, a->ifname,
280 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
281 fatalx("%s: interface name truncated",
282 __func__);
286 TAILQ_INSERT_TAIL(sock->conf.al, acp, entry);
288 done:
289 return (sock);
292 void
293 sockets_launch(void)
295 struct socket *sock;
297 TAILQ_FOREACH(sock, gotwebd_env->sockets, entry) {
298 log_debug("%s: configuring socket %d (%d)", __func__,
299 sock->conf.id, sock->fd);
301 event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
302 sockets_socket_accept, sock);
304 if (event_add(&sock->ev, NULL))
305 fatalx("event add sock");
307 evtimer_set(&sock->pause, sockets_accept_paused, sock);
309 log_debug("%s: running socket listener %d", __func__,
310 sock->conf.id);
314 void
315 sockets_purge(struct gotwebd *env)
317 struct socket *sock, *tsock;
319 /* shutdown and remove sockets */
320 TAILQ_FOREACH_SAFE(sock, env->sockets, entry, tsock) {
321 if (event_initialized(&sock->ev))
322 event_del(&sock->ev);
323 if (evtimer_initialized(&sock->evt))
324 evtimer_del(&sock->evt);
325 if (evtimer_initialized(&sock->pause))
326 evtimer_del(&sock->pause);
327 if (sock->fd != -1)
328 close(sock->fd);
329 TAILQ_REMOVE(env->sockets, sock, entry);
333 int
334 sockets_dispatch_gotwebd(int fd, struct privsep_proc *p, struct imsg *imsg)
336 struct privsep *ps = p->p_ps;
337 int res = 0, cmd = 0, verbose;
339 switch (imsg->hdr.type) {
340 case IMSG_CFG_SRV:
341 config_getserver(gotwebd_env, imsg);
342 break;
343 case IMSG_CFG_SOCK:
344 config_getsock(gotwebd_env, imsg);
345 break;
346 case IMSG_CFG_FD:
347 config_getfd(gotwebd_env, imsg);
348 break;
349 case IMSG_CFG_DONE:
350 config_getcfg(gotwebd_env, imsg);
351 break;
352 case IMSG_CTL_START:
353 sockets_launch();
354 break;
355 case IMSG_CTL_VERBOSE:
356 IMSG_SIZE_CHECK(imsg, &verbose);
357 memcpy(&verbose, imsg->data, sizeof(verbose));
358 log_setverbose(verbose);
359 break;
360 default:
361 return -1;
364 switch (cmd) {
365 case 0:
366 break;
367 default:
368 if (proc_compose_imsg(ps, PROC_GOTWEBD, -1, cmd,
369 imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
370 return -1;
371 break;
374 return 0;
377 void
378 sockets_sighdlr(int sig, short event, void *arg)
380 switch (sig) {
381 case SIGHUP:
382 log_info("%s: ignoring SIGHUP", __func__);
383 break;
384 case SIGPIPE:
385 log_info("%s: ignoring SIGPIPE", __func__);
386 break;
387 case SIGUSR1:
388 log_info("%s: ignoring SIGUSR1", __func__);
389 break;
390 case SIGCHLD:
391 break;
392 default:
393 log_info("SIGNAL: %d", sig);
394 fatalx("unexpected signal");
398 void
399 sockets_shutdown(void)
401 struct server *srv, *tsrv;
402 struct socket *sock, *tsock;
404 sockets_purge(gotwebd_env);
406 /* clean sockets */
407 TAILQ_FOREACH_SAFE(sock, gotwebd_env->sockets, entry, tsock) {
408 TAILQ_REMOVE(gotwebd_env->sockets, sock, entry);
409 close(sock->fd);
410 free(sock);
413 /* clean servers */
414 TAILQ_FOREACH_SAFE(srv, gotwebd_env->servers, entry, tsrv)
415 free(srv);
417 free(gotwebd_env->sockets);
418 free(gotwebd_env->servers);
419 free(gotwebd_env);
422 int
423 sockets_privinit(struct gotwebd *env, struct socket *sock)
425 struct privsep *ps = env->gotwebd_ps;
427 if (sock->conf.type == UNIX) {
428 log_debug("%s: initializing unix socket %s", __func__,
429 sock->conf.unix_socket_name);
430 sock->fd = sockets_unix_socket_listen(ps, sock);
431 if (sock->fd == -1) {
432 log_warnx("%s: create unix socket failed", __func__);
433 return -1;
437 if (sock->conf.type == FCGI) {
438 log_debug("%s: initializing fcgi socket for %s", __func__,
439 sock->conf.name);
440 sock->fd = sockets_create_socket(sock->conf.al,
441 sock->conf.fcgi_socket_port);
442 if (sock->fd == -1) {
443 log_warnx("%s: create unix socket failed", __func__);
444 return -1;
448 return 0;
451 int
452 sockets_unix_socket_listen(struct privsep *ps, struct socket *sock)
454 struct gotwebd *env = ps->ps_env;
455 struct sockaddr_un sun;
456 struct socket *tsock;
457 int u_fd = -1;
458 mode_t old_umask, mode;
460 TAILQ_FOREACH(tsock, env->sockets, entry) {
461 if (strcmp(tsock->conf.unix_socket_name,
462 sock->conf.unix_socket_name) == 0 &&
463 tsock->fd != -1)
464 return (tsock->fd);
467 /* TA: FIXME: this needs upstreaming. */
468 int socket_flags = SOCK_STREAM | SOCK_NONBLOCK;
469 #ifdef SOCK_CLOEXEC
470 socket_flags |= SOCK_CLOEXEC;
471 #endif
472 u_fd = socket(AF_UNIX, socket_flags, 0);
473 if (u_fd == -1) {
474 log_warn("%s: socket", __func__);
475 return -1;
478 sun.sun_family = AF_UNIX;
479 if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
480 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
481 log_warn("%s: %s name too long", __func__,
482 sock->conf.unix_socket_name);
483 close(u_fd);
484 return -1;
487 if (unlink(sock->conf.unix_socket_name) == -1) {
488 if (errno != ENOENT) {
489 log_warn("%s: unlink %s", __func__,
490 sock->conf.unix_socket_name);
491 close(u_fd);
492 return -1;
496 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
497 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
499 if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
500 log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
501 close(u_fd);
502 (void)umask(old_umask);
503 return -1;
506 (void)umask(old_umask);
508 if (chmod(sock->conf.unix_socket_name, mode) == -1) {
509 log_warn("%s: chmod", __func__);
510 close(u_fd);
511 (void)unlink(sock->conf.unix_socket_name);
512 return -1;
515 if (chown(sock->conf.unix_socket_name, ps->ps_pw->pw_uid,
516 ps->ps_pw->pw_gid) == -1) {
517 log_warn("%s: chown", __func__);
518 close(u_fd);
519 (void)unlink(sock->conf.unix_socket_name);
520 return -1;
523 if (listen(u_fd, SOCKS_BACKLOG) == -1) {
524 log_warn("%s: listen", __func__);
525 return -1;
528 return u_fd;
531 int
532 sockets_create_socket(struct addresslist *al, in_port_t port)
534 struct addrinfo hints;
535 struct address *a;
536 int fd = -1, o_val = 1, flags;
538 memset(&hints, 0, sizeof(hints));
539 hints.ai_family = AF_UNSPEC;
540 hints.ai_socktype = SOCK_STREAM;
541 hints.ai_flags |= AI_PASSIVE;
543 TAILQ_FOREACH(a, al, entry) {
544 switch (a->ss.ss_family) {
545 case AF_INET:
546 ((struct sockaddr_in *)(&a->ss))->sin_port = port;
547 break;
548 case AF_INET6:
549 ((struct sockaddr_in6 *)(&a->ss))->sin6_port = port;
550 break;
551 default:
552 log_warnx("%s: unknown address family", __func__);
553 goto fail;
556 fd = socket(a->ss.ss_family, hints.ai_socktype,
557 a->ipproto);
558 log_debug("%s: opening socket (%d) for %s", __func__,
559 fd, a->ifname);
561 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
562 sizeof(int)) == -1) {
563 log_warn("%s: setsockopt error", __func__);
564 return -1;
567 /* non-blocking */
568 flags = fcntl(fd, F_GETFL);
569 flags |= O_NONBLOCK;
570 fcntl(fd, F_SETFL, flags);
572 /* TA: 'sizeof a' vs a->ss.len */
573 if (bind(fd, (struct sockaddr *)&a->ss, sizeof a) == -1) {
574 close(fd);
575 log_info("%s: can't bind to port %d", __func__,
576 ntohs(port));
577 goto fail;
580 if (listen(fd, SOMAXCONN) == -1) {
581 log_warn("%s, unable to listen on socket", __func__);
582 goto fail;
586 free(a);
587 return (fd);
588 fail:
589 free(a);
590 return -1;
593 int
594 sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
595 int reserve, volatile int *counter)
597 int ret;
599 if (getdtablecount() + reserve +
600 ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
601 log_debug("inflight fds exceeded");
602 errno = EMFILE;
603 return -1;
605 /* TA: This needs fixing upstream. */
606 #ifdef __APPLE__
607 ret = accept(sockfd, addr, addrlen);
608 #else
609 ret = accept4(sockfd, addr, addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC);
610 #endif
612 if (ret > -1) {
613 (*counter)++;
614 log_debug("inflight incremented, now %d", *counter);
617 return ret;
620 void
621 sockets_accept_paused(int fd, short events, void *arg)
623 struct socket *sock = (struct socket *)arg;
625 event_add(&sock->ev, NULL);
628 void
629 sockets_socket_accept(int fd, short event, void *arg)
631 struct socket *sock = (struct socket *)arg;
632 struct sockaddr_storage ss;
633 struct timeval backoff;
634 struct request *c = NULL;
635 socklen_t len;
636 int s;
638 backoff.tv_sec = 1;
639 backoff.tv_usec = 0;
641 event_add(&sock->ev, NULL);
642 if (event & EV_TIMEOUT)
643 return;
645 len = sizeof(ss);
647 s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
648 FD_RESERVE, &cgi_inflight);
650 if (s == -1) {
651 switch (errno) {
652 case EINTR:
653 case EWOULDBLOCK:
654 case ECONNABORTED:
655 return;
656 case EMFILE:
657 case ENFILE:
658 event_del(&sock->ev);
659 evtimer_add(&sock->pause, &backoff);
660 return;
661 default:
662 log_warn("%s: accept", __func__);
666 if (client_cnt > GOTWEBD_MAXCLIENTS)
667 goto err;
669 c = calloc(1, sizeof(struct request));
670 if (c == NULL) {
671 log_warn("%s", __func__);
672 close(s);
673 cgi_inflight--;
674 return;
677 c->fd = s;
678 c->sock = sock;
679 memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
680 c->buf_pos = 0;
681 c->buf_len = 0;
682 c->request_started = 0;
683 c->sock->client_status = CLIENT_CONNECT;
685 event_set(&c->ev, s, EV_READ, fcgi_request, c);
686 event_add(&c->ev, NULL);
688 evtimer_set(&c->tmo, fcgi_timeout, c);
689 evtimer_add(&c->tmo, &timeout);
691 client_cnt++;
693 return;
694 err:
695 cgi_inflight--;
696 close(s);
697 if (c != NULL)
698 free(c);
701 void
702 sockets_rlimit(int maxfd)
704 struct rlimit rl;
706 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
707 fatal("%s: failed to get resource limit", __func__);
708 log_debug("%s: max open files %llu", __func__,
709 (unsigned long long)rl.rlim_max);
711 /*
712 * Allow the maximum number of open file descriptors for this
713 * login class (which should be the class "daemon" by default).
714 */
715 if (maxfd == -1)
716 rl.rlim_cur = rl.rlim_max;
717 else
718 rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
719 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
720 fatal("%s: failed to set resource limit", __func__);