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->sockets);
408 free(gotwebd_env->servers);
409 free(gotwebd_env);
412 int
413 sockets_privinit(struct gotwebd *env, struct socket *sock)
415 struct privsep *ps = env->gotwebd_ps;
417 if (sock->conf.type == UNIX) {
418 log_debug("%s: initializing unix socket %s", __func__,
419 sock->conf.unix_socket_name);
420 sock->fd = sockets_unix_socket_listen(ps, sock);
421 if (sock->fd == -1) {
422 log_warnx("%s: create unix socket failed", __func__);
423 return -1;
427 if (sock->conf.type == FCGI) {
428 log_debug("%s: initializing fcgi socket for %s", __func__,
429 sock->conf.name);
430 sock->fd = sockets_create_socket(&sock->conf.al,
431 sock->conf.fcgi_socket_port);
432 if (sock->fd == -1) {
433 log_warnx("%s: create unix socket failed", __func__);
434 return -1;
438 return 0;
441 int
442 sockets_unix_socket_listen(struct privsep *ps, struct socket *sock)
444 struct gotwebd *env = ps->ps_env;
445 struct sockaddr_un sun;
446 struct socket *tsock;
447 int u_fd = -1;
448 mode_t old_umask, mode;
450 TAILQ_FOREACH(tsock, env->sockets, entry) {
451 if (strcmp(tsock->conf.unix_socket_name,
452 sock->conf.unix_socket_name) == 0 &&
453 tsock->fd != -1)
454 return (tsock->fd);
457 u_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
458 if (u_fd == -1) {
459 log_warn("%s: socket", __func__);
460 return -1;
463 sun.sun_family = AF_UNIX;
464 if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
465 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
466 log_warn("%s: %s name too long", __func__,
467 sock->conf.unix_socket_name);
468 close(u_fd);
469 return -1;
472 if (unlink(sock->conf.unix_socket_name) == -1) {
473 if (errno != ENOENT) {
474 log_warn("%s: unlink %s", __func__,
475 sock->conf.unix_socket_name);
476 close(u_fd);
477 return -1;
481 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
482 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
484 if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
485 log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
486 close(u_fd);
487 (void)umask(old_umask);
488 return -1;
491 (void)umask(old_umask);
493 if (chmod(sock->conf.unix_socket_name, mode) == -1) {
494 log_warn("%s: chmod", __func__);
495 close(u_fd);
496 (void)unlink(sock->conf.unix_socket_name);
497 return -1;
500 if (chown(sock->conf.unix_socket_name, ps->ps_pw->pw_uid,
501 ps->ps_pw->pw_gid) == -1) {
502 log_warn("%s: chown", __func__);
503 close(u_fd);
504 (void)unlink(sock->conf.unix_socket_name);
505 return -1;
508 if (listen(u_fd, SOCKS_BACKLOG) == -1) {
509 log_warn("%s: listen", __func__);
510 return -1;
513 return u_fd;
516 int
517 sockets_create_socket(struct addresslist *al, in_port_t port)
519 struct addrinfo hints;
520 struct address *a;
521 int fd = -1, o_val = 1, flags;
523 memset(&hints, 0, sizeof(hints));
524 hints.ai_family = AF_UNSPEC;
525 hints.ai_socktype = SOCK_STREAM;
526 hints.ai_flags |= AI_PASSIVE;
528 TAILQ_FOREACH(a, al, entry) {
529 switch (a->ss.ss_family) {
530 case AF_INET:
531 ((struct sockaddr_in *)(&a->ss))->sin_port = port;
532 break;
533 case AF_INET6:
534 ((struct sockaddr_in6 *)(&a->ss))->sin6_port = port;
535 break;
536 default:
537 log_warnx("%s: unknown address family", __func__);
538 goto fail;
541 fd = socket(a->ss.ss_family, hints.ai_socktype,
542 a->ipproto);
543 log_debug("%s: opening socket (%d) for %s", __func__,
544 fd, a->ifname);
546 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
547 sizeof(int)) == -1) {
548 log_warn("%s: setsockopt error", __func__);
549 return -1;
552 /* non-blocking */
553 flags = fcntl(fd, F_GETFL);
554 flags |= O_NONBLOCK;
555 fcntl(fd, F_SETFL, flags);
557 if (bind(fd, (struct sockaddr *)&a->ss, a->ss.ss_len) == -1) {
558 close(fd);
559 log_info("%s: can't bind to port %d", __func__,
560 ntohs(port));
561 goto fail;
564 if (listen(fd, SOMAXCONN) == -1) {
565 log_warn("%s, unable to listen on socket", __func__);
566 goto fail;
570 free(a);
571 return (fd);
572 fail:
573 free(a);
574 return -1;
577 int
578 sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
579 int reserve, volatile int *counter)
581 int ret;
583 if (getdtablecount() + reserve +
584 ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
585 log_debug("inflight fds exceeded");
586 errno = EMFILE;
587 return -1;
590 if ((ret = accept4(sockfd, addr, addrlen,
591 SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
592 (*counter)++;
593 log_debug("inflight incremented, now %d", *counter);
596 return ret;
599 void
600 sockets_accept_paused(int fd, short events, void *arg)
602 struct socket *sock = (struct socket *)arg;
604 event_add(&sock->ev, NULL);
607 void
608 sockets_socket_accept(int fd, short event, void *arg)
610 struct socket *sock = (struct socket *)arg;
611 struct sockaddr_storage ss;
612 struct timeval backoff;
613 struct request *c = NULL;
614 socklen_t len;
615 int s;
617 backoff.tv_sec = 1;
618 backoff.tv_usec = 0;
620 event_add(&sock->ev, NULL);
621 if (event & EV_TIMEOUT)
622 return;
624 len = sizeof(ss);
626 s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
627 FD_RESERVE, &cgi_inflight);
629 if (s == -1) {
630 switch (errno) {
631 case EINTR:
632 case EWOULDBLOCK:
633 case ECONNABORTED:
634 return;
635 case EMFILE:
636 case ENFILE:
637 event_del(&sock->ev);
638 evtimer_add(&sock->pause, &backoff);
639 return;
640 default:
641 log_warn("%s: accept", __func__);
645 if (client_cnt > GOTWEBD_MAXCLIENTS)
646 goto err;
648 c = calloc(1, sizeof(struct request));
649 if (c == NULL) {
650 log_warn("%s", __func__);
651 close(s);
652 cgi_inflight--;
653 return;
656 c->fd = s;
657 c->sock = sock;
658 memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
659 c->buf_pos = 0;
660 c->buf_len = 0;
661 c->request_started = 0;
662 c->sock->client_status = CLIENT_CONNECT;
664 event_set(&c->ev, s, EV_READ, fcgi_request, c);
665 event_add(&c->ev, NULL);
667 evtimer_set(&c->tmo, fcgi_timeout, c);
668 evtimer_add(&c->tmo, &timeout);
670 client_cnt++;
672 return;
673 err:
674 cgi_inflight--;
675 close(s);
676 if (c != NULL)
677 free(c);
680 void
681 sockets_rlimit(int maxfd)
683 struct rlimit rl;
685 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
686 fatal("%s: failed to get resource limit", __func__);
687 log_debug("%s: max open files %llu", __func__,
688 (unsigned long long)rl.rlim_max);
690 /*
691 * Allow the maximum number of open file descriptors for this
692 * login class (which should be the class "daemon" by default).
693 */
694 if (maxfd == -1)
695 rl.rlim_cur = rl.rlim_max;
696 else
697 rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
698 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
699 fatal("%s: failed to set resource limit", __func__);