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"
55 #include "proc.h"
56 #include "gotwebd.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 || (size_t)n >= GOTWEBD_MAXTEXT) {
183 free(p_sock);
184 free(sock);
185 fatalx("%s: snprintf", __func__);
188 TAILQ_FOREACH(a, &p_sock->conf.al, entry) {
189 if (a->ss.ss_family == AF_INET)
190 continue;
192 if ((acp = calloc(1, sizeof(*acp))) == NULL)
193 fatal("%s: calloc", __func__);
194 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
195 acp->ipproto = a->ipproto;
196 acp->prefixlen = a->prefixlen;
197 acp->port = a->port;
198 if (strlen(a->ifname) != 0) {
199 if (strlcpy(acp->ifname, a->ifname,
200 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
201 fatalx("%s: interface name truncated",
202 __func__);
206 TAILQ_INSERT_TAIL(&sock->conf.al, acp, entry);
210 struct socket *
211 sockets_conf_new_socket(struct gotwebd *env, struct server *srv, int id,
212 int type, int is_dup)
214 struct socket *sock;
215 struct address *a, *acp;
216 int n;
218 if ((sock = calloc(1, sizeof(*sock))) == NULL)
219 fatalx("%s: calloc", __func__);
221 TAILQ_INIT(&sock->conf.al);
223 sock->conf.parent_id = 0;
224 sock->conf.id = id;
226 sock->fd = -1;
228 sock->conf.type = type;
230 if (type == UNIX) {
231 if (strlcpy(sock->conf.unix_socket_name,
232 srv->unix_socket_name,
233 sizeof(sock->conf.unix_socket_name)) >=
234 sizeof(sock->conf.unix_socket_name)) {
235 free(sock);
236 fatalx("%s: strlcpy", __func__);
238 } else
239 sock->conf.ipv4 = 1;
241 sock->conf.fcgi_socket_port = srv->fcgi_socket_port;
243 if (is_dup)
244 goto done;
246 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
247 srv->name);
248 if (n < 0 || (size_t)n >= GOTWEBD_MAXTEXT) {
249 free(sock);
250 fatalx("%s: snprintf", __func__);
253 if (strlcpy(sock->conf.srv_name, srv->name,
254 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
255 free(sock);
256 fatalx("%s: strlcpy", __func__);
259 TAILQ_FOREACH(a, &srv->al, entry) {
260 if ((acp = calloc(1, sizeof(*acp))) == NULL) {
261 free(sock);
262 fatal("%s: calloc", __func__);
264 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
265 acp->ipproto = a->ipproto;
266 acp->prefixlen = a->prefixlen;
267 acp->port = a->port;
268 if (strlen(a->ifname) != 0) {
269 if (strlcpy(acp->ifname, a->ifname,
270 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
271 fatalx("%s: interface name truncated",
272 __func__);
276 TAILQ_INSERT_TAIL(&sock->conf.al, acp, entry);
278 done:
279 return (sock);
282 void
283 sockets_launch(void)
285 struct socket *sock;
287 TAILQ_FOREACH(sock, &gotwebd_env->sockets, entry) {
288 log_debug("%s: configuring socket %d (%d)", __func__,
289 sock->conf.id, sock->fd);
291 event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
292 sockets_socket_accept, sock);
294 if (event_add(&sock->ev, NULL))
295 fatalx("event add sock");
297 evtimer_set(&sock->pause, sockets_accept_paused, sock);
299 log_debug("%s: running socket listener %d", __func__,
300 sock->conf.id);
304 void
305 sockets_purge(struct gotwebd *env)
307 struct socket *sock, *tsock;
309 /* shutdown and remove sockets */
310 TAILQ_FOREACH_SAFE(sock, &env->sockets, entry, tsock) {
311 if (event_initialized(&sock->ev))
312 event_del(&sock->ev);
313 if (evtimer_initialized(&sock->evt))
314 evtimer_del(&sock->evt);
315 if (evtimer_initialized(&sock->pause))
316 evtimer_del(&sock->pause);
317 if (sock->fd != -1)
318 close(sock->fd);
319 TAILQ_REMOVE(&env->sockets, sock, entry);
323 int
324 sockets_dispatch_gotwebd(int fd, struct privsep_proc *p, struct imsg *imsg)
326 struct privsep *ps = p->p_ps;
327 int res = 0, cmd = 0, verbose;
329 switch (imsg->hdr.type) {
330 case IMSG_CFG_SRV:
331 config_getserver(gotwebd_env, imsg);
332 break;
333 case IMSG_CFG_SOCK:
334 config_getsock(gotwebd_env, imsg);
335 break;
336 case IMSG_CFG_FD:
337 config_getfd(gotwebd_env, imsg);
338 break;
339 case IMSG_CFG_DONE:
340 config_getcfg(gotwebd_env, imsg);
341 break;
342 case IMSG_CTL_START:
343 sockets_launch();
344 break;
345 case IMSG_CTL_VERBOSE:
346 IMSG_SIZE_CHECK(imsg, &verbose);
347 memcpy(&verbose, imsg->data, sizeof(verbose));
348 log_setverbose(verbose);
349 break;
350 default:
351 return -1;
354 switch (cmd) {
355 case 0:
356 break;
357 default:
358 if (proc_compose_imsg(ps, PROC_GOTWEBD, -1, cmd,
359 imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
360 return -1;
361 break;
364 return 0;
367 void
368 sockets_sighdlr(int sig, short event, void *arg)
370 switch (sig) {
371 case SIGHUP:
372 log_info("%s: ignoring SIGHUP", __func__);
373 break;
374 case SIGPIPE:
375 log_info("%s: ignoring SIGPIPE", __func__);
376 break;
377 case SIGUSR1:
378 log_info("%s: ignoring SIGUSR1", __func__);
379 break;
380 case SIGCHLD:
381 break;
382 default:
383 log_info("SIGNAL: %d", sig);
384 fatalx("unexpected signal");
388 void
389 sockets_shutdown(void)
391 struct server *srv, *tsrv;
392 struct socket *sock, *tsock;
394 sockets_purge(gotwebd_env);
396 /* clean sockets */
397 TAILQ_FOREACH_SAFE(sock, &gotwebd_env->sockets, entry, tsock) {
398 TAILQ_REMOVE(&gotwebd_env->sockets, sock, entry);
399 close(sock->fd);
400 free(sock);
403 /* clean servers */
404 TAILQ_FOREACH_SAFE(srv, &gotwebd_env->servers, entry, tsrv)
405 free(srv);
407 free(gotwebd_env);
410 int
411 sockets_privinit(struct gotwebd *env, struct socket *sock)
413 struct privsep *ps = env->gotwebd_ps;
415 if (sock->conf.type == UNIX) {
416 log_debug("%s: initializing unix socket %s", __func__,
417 sock->conf.unix_socket_name);
418 sock->fd = sockets_unix_socket_listen(ps, sock);
419 if (sock->fd == -1) {
420 log_warnx("%s: create unix socket failed", __func__);
421 return -1;
425 if (sock->conf.type == FCGI) {
426 log_debug("%s: initializing fcgi socket for %s", __func__,
427 sock->conf.name);
428 sock->fd = sockets_create_socket(&sock->conf.al,
429 sock->conf.fcgi_socket_port);
430 if (sock->fd == -1) {
431 log_warnx("%s: create unix socket failed", __func__);
432 return -1;
436 return 0;
439 int
440 sockets_unix_socket_listen(struct privsep *ps, struct socket *sock)
442 struct gotwebd *env = ps->ps_env;
443 struct sockaddr_un sun;
444 struct socket *tsock;
445 int u_fd = -1;
446 mode_t old_umask, mode;
448 TAILQ_FOREACH(tsock, &env->sockets, entry) {
449 if (strcmp(tsock->conf.unix_socket_name,
450 sock->conf.unix_socket_name) == 0 &&
451 tsock->fd != -1)
452 return (tsock->fd);
455 u_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
456 if (u_fd == -1) {
457 log_warn("%s: socket", __func__);
458 return -1;
461 sun.sun_family = AF_UNIX;
462 if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
463 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
464 log_warn("%s: %s name too long", __func__,
465 sock->conf.unix_socket_name);
466 close(u_fd);
467 return -1;
470 if (unlink(sock->conf.unix_socket_name) == -1) {
471 if (errno != ENOENT) {
472 log_warn("%s: unlink %s", __func__,
473 sock->conf.unix_socket_name);
474 close(u_fd);
475 return -1;
479 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
480 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
482 if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
483 log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
484 close(u_fd);
485 (void)umask(old_umask);
486 return -1;
489 (void)umask(old_umask);
491 if (chmod(sock->conf.unix_socket_name, mode) == -1) {
492 log_warn("%s: chmod", __func__);
493 close(u_fd);
494 (void)unlink(sock->conf.unix_socket_name);
495 return -1;
498 if (chown(sock->conf.unix_socket_name, ps->ps_pw->pw_uid,
499 ps->ps_pw->pw_gid) == -1) {
500 log_warn("%s: chown", __func__);
501 close(u_fd);
502 (void)unlink(sock->conf.unix_socket_name);
503 return -1;
506 if (listen(u_fd, SOCKS_BACKLOG) == -1) {
507 log_warn("%s: listen", __func__);
508 return -1;
511 return u_fd;
514 int
515 sockets_create_socket(struct addresslist *al, in_port_t port)
517 struct addrinfo hints;
518 struct address *a;
519 int fd = -1, o_val = 1, flags;
521 memset(&hints, 0, sizeof(hints));
522 hints.ai_family = AF_UNSPEC;
523 hints.ai_socktype = SOCK_STREAM;
524 hints.ai_flags |= AI_PASSIVE;
526 TAILQ_FOREACH(a, al, entry) {
527 switch (a->ss.ss_family) {
528 case AF_INET:
529 ((struct sockaddr_in *)(&a->ss))->sin_port = port;
530 break;
531 case AF_INET6:
532 ((struct sockaddr_in6 *)(&a->ss))->sin6_port = port;
533 break;
534 default:
535 log_warnx("%s: unknown address family", __func__);
536 goto fail;
539 fd = socket(a->ss.ss_family, hints.ai_socktype,
540 a->ipproto);
541 log_debug("%s: opening socket (%d) for %s", __func__,
542 fd, a->ifname);
544 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
545 sizeof(int)) == -1) {
546 log_warn("%s: setsockopt error", __func__);
547 return -1;
550 /* non-blocking */
551 flags = fcntl(fd, F_GETFL);
552 flags |= O_NONBLOCK;
553 fcntl(fd, F_SETFL, flags);
555 if (bind(fd, (struct sockaddr *)&a->ss, a->ss.ss_len) == -1) {
556 close(fd);
557 log_info("%s: can't bind to port %d", __func__,
558 ntohs(port));
559 goto fail;
562 if (listen(fd, SOMAXCONN) == -1) {
563 log_warn("%s, unable to listen on socket", __func__);
564 goto fail;
568 free(a);
569 return (fd);
570 fail:
571 free(a);
572 return -1;
575 int
576 sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
577 int reserve, volatile int *counter)
579 int ret;
581 if (getdtablecount() + reserve +
582 ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
583 log_debug("inflight fds exceeded");
584 errno = EMFILE;
585 return -1;
588 if ((ret = accept4(sockfd, addr, addrlen,
589 SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
590 (*counter)++;
591 log_debug("inflight incremented, now %d", *counter);
594 return ret;
597 void
598 sockets_accept_paused(int fd, short events, void *arg)
600 struct socket *sock = (struct socket *)arg;
602 event_add(&sock->ev, NULL);
605 void
606 sockets_socket_accept(int fd, short event, void *arg)
608 struct socket *sock = (struct socket *)arg;
609 struct sockaddr_storage ss;
610 struct timeval backoff;
611 struct request *c = NULL;
612 socklen_t len;
613 int s;
615 backoff.tv_sec = 1;
616 backoff.tv_usec = 0;
618 event_add(&sock->ev, NULL);
619 if (event & EV_TIMEOUT)
620 return;
622 len = sizeof(ss);
624 s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
625 FD_RESERVE, &cgi_inflight);
627 if (s == -1) {
628 switch (errno) {
629 case EINTR:
630 case EWOULDBLOCK:
631 case ECONNABORTED:
632 return;
633 case EMFILE:
634 case ENFILE:
635 event_del(&sock->ev);
636 evtimer_add(&sock->pause, &backoff);
637 return;
638 default:
639 log_warn("%s: accept", __func__);
643 if (client_cnt > GOTWEBD_MAXCLIENTS)
644 goto err;
646 c = calloc(1, sizeof(struct request));
647 if (c == NULL) {
648 log_warn("%s", __func__);
649 close(s);
650 cgi_inflight--;
651 return;
654 c->fd = s;
655 c->sock = sock;
656 memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
657 c->buf_pos = 0;
658 c->buf_len = 0;
659 c->request_started = 0;
660 c->sock->client_status = CLIENT_CONNECT;
662 event_set(&c->ev, s, EV_READ, fcgi_request, c);
663 event_add(&c->ev, NULL);
665 evtimer_set(&c->tmo, fcgi_timeout, c);
666 evtimer_add(&c->tmo, &timeout);
668 client_cnt++;
670 return;
671 err:
672 cgi_inflight--;
673 close(s);
674 if (c != NULL)
675 free(c);
678 void
679 sockets_rlimit(int maxfd)
681 struct rlimit rl;
683 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
684 fatal("%s: failed to get resource limit", __func__);
685 log_debug("%s: max open files %llu", __func__,
686 (unsigned long long)rl.rlim_max);
688 /*
689 * Allow the maximum number of open file descriptors for this
690 * login class (which should be the class "daemon" by default).
691 */
692 if (maxfd == -1)
693 rl.rlim_cur = rl.rlim_max;
694 else
695 rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
696 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
697 fatal("%s: failed to set resource limit", __func__);