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 static struct timeval timeout = { TIMEOUT_DEFAULT, 0 };
66 static void sockets_sighdlr(int, short, void *);
67 static void sockets_run(struct privsep *, struct privsep_proc *, void *);
68 static void sockets_launch(void);
69 static void sockets_purge(struct gotwebd *);
70 static void sockets_accept_paused(int, short, void *);
71 static void sockets_rlimit(int);
73 static int sockets_dispatch_gotwebd(int, struct privsep_proc *,
74 struct imsg *);
75 static int sockets_unix_socket_listen(struct privsep *, struct socket *);
76 static int sockets_create_socket(struct address *, in_port_t);
77 static int sockets_accept_reserve(int, struct sockaddr *, socklen_t *,
78 int, volatile int *);
80 static struct socket *sockets_conf_new_socket_unix(struct gotwebd *,
81 struct server *, int);
82 static struct socket *sockets_conf_new_socket_fcgi(struct gotwebd *,
83 struct server *, int, struct address *);
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 static 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 address *a;
123 struct socket *new_sock = NULL;
124 int sock_id = 1;
126 TAILQ_FOREACH(srv, &env->servers, entry) {
127 if (srv->unix_socket) {
128 new_sock = sockets_conf_new_socket_unix(env, srv,
129 sock_id);
130 if (new_sock) {
131 sock_id++;
132 TAILQ_INSERT_TAIL(&env->sockets, new_sock,
133 entry);
137 if (srv->fcgi_socket) {
138 if (TAILQ_EMPTY(&srv->al)) {
139 fatalx("%s: server %s has no IP addresses to "
140 "listen for FCGI connections", __func__,
141 srv->name);
143 TAILQ_FOREACH(a, &srv->al, entry) {
144 if (a->ss.ss_family != AF_INET &&
145 a->ss.ss_family != AF_INET6)
146 continue;
147 new_sock = sockets_conf_new_socket_fcgi(env,
148 srv, sock_id, a);
149 if (new_sock) {
150 sock_id++;
151 TAILQ_INSERT_TAIL(&env->sockets,
152 new_sock, entry);
159 static struct socket *
160 sockets_conf_new_socket_unix(struct gotwebd *env, struct server *srv, int id)
162 struct socket *sock;
163 int n;
165 if ((sock = calloc(1, sizeof(*sock))) == NULL)
166 fatalx("%s: calloc", __func__);
168 sock->conf.id = id;
169 sock->fd = -1;
170 sock->conf.af_type = AF_UNIX;
172 if (strlcpy(sock->conf.unix_socket_name,
173 srv->unix_socket_name,
174 sizeof(sock->conf.unix_socket_name)) >=
175 sizeof(sock->conf.unix_socket_name)) {
176 free(sock);
177 fatalx("%s: strlcpy", __func__);
180 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
181 srv->name);
182 if (n < 0 || (size_t)n >= GOTWEBD_MAXTEXT) {
183 free(sock);
184 fatalx("%s: snprintf", __func__);
187 if (strlcpy(sock->conf.srv_name, srv->name,
188 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
189 free(sock);
190 fatalx("%s: strlcpy", __func__);
193 return sock;
196 static struct socket *
197 sockets_conf_new_socket_fcgi(struct gotwebd *env, struct server *srv, int id,
198 struct address *a)
200 struct socket *sock;
201 struct address *acp;
202 int n;
204 if ((sock = calloc(1, sizeof(*sock))) == NULL)
205 fatalx("%s: calloc", __func__);
207 sock->conf.id = id;
208 sock->fd = -1;
209 sock->conf.af_type = a->ss.ss_family;
211 sock->conf.fcgi_socket_port = a->port;
213 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
214 srv->name);
215 if (n < 0 || (size_t)n >= GOTWEBD_MAXTEXT) {
216 free(sock);
217 fatalx("%s: snprintf", __func__);
220 if (strlcpy(sock->conf.srv_name, srv->name,
221 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
222 free(sock);
223 fatalx("%s: strlcpy", __func__);
226 acp = &sock->conf.addr;
228 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
229 acp->ipproto = a->ipproto;
230 acp->prefixlen = a->prefixlen;
231 acp->port = a->port;
232 if (strlen(a->ifname) != 0) {
233 if (strlcpy(acp->ifname, a->ifname,
234 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
235 fatalx("%s: interface name truncated",
236 __func__);
240 return (sock);
243 static void
244 sockets_launch(void)
246 struct socket *sock;
248 TAILQ_FOREACH(sock, &gotwebd_env->sockets, entry) {
249 log_debug("%s: configuring socket %d (%d)", __func__,
250 sock->conf.id, sock->fd);
252 event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
253 sockets_socket_accept, sock);
255 if (event_add(&sock->ev, NULL))
256 fatalx("event add sock");
258 evtimer_set(&sock->pause, sockets_accept_paused, sock);
260 log_debug("%s: running socket listener %d", __func__,
261 sock->conf.id);
265 static void
266 sockets_purge(struct gotwebd *env)
268 struct socket *sock, *tsock;
270 /* shutdown and remove sockets */
271 TAILQ_FOREACH_SAFE(sock, &env->sockets, entry, tsock) {
272 if (event_initialized(&sock->ev))
273 event_del(&sock->ev);
274 if (evtimer_initialized(&sock->evt))
275 evtimer_del(&sock->evt);
276 if (evtimer_initialized(&sock->pause))
277 evtimer_del(&sock->pause);
278 if (sock->fd != -1)
279 close(sock->fd);
280 TAILQ_REMOVE(&env->sockets, sock, entry);
284 static int
285 sockets_dispatch_gotwebd(int fd, struct privsep_proc *p, struct imsg *imsg)
287 struct privsep *ps = p->p_ps;
288 int res = 0, cmd = 0, verbose;
290 switch (imsg->hdr.type) {
291 case IMSG_CFG_SRV:
292 config_getserver(gotwebd_env, imsg);
293 break;
294 case IMSG_CFG_SOCK:
295 config_getsock(gotwebd_env, imsg);
296 break;
297 case IMSG_CFG_FD:
298 config_getfd(gotwebd_env, imsg);
299 break;
300 case IMSG_CFG_DONE:
301 config_getcfg(gotwebd_env, imsg);
302 break;
303 case IMSG_CTL_START:
304 sockets_launch();
305 break;
306 case IMSG_CTL_VERBOSE:
307 IMSG_SIZE_CHECK(imsg, &verbose);
308 memcpy(&verbose, imsg->data, sizeof(verbose));
309 log_setverbose(verbose);
310 break;
311 default:
312 return -1;
315 switch (cmd) {
316 case 0:
317 break;
318 default:
319 if (proc_compose_imsg(ps, PROC_GOTWEBD, -1, cmd,
320 imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
321 return -1;
322 break;
325 return 0;
328 static void
329 sockets_sighdlr(int sig, short event, void *arg)
331 switch (sig) {
332 case SIGHUP:
333 log_info("%s: ignoring SIGHUP", __func__);
334 break;
335 case SIGPIPE:
336 log_info("%s: ignoring SIGPIPE", __func__);
337 break;
338 case SIGUSR1:
339 log_info("%s: ignoring SIGUSR1", __func__);
340 break;
341 case SIGCHLD:
342 break;
343 default:
344 log_info("SIGNAL: %d", sig);
345 fatalx("unexpected signal");
349 void
350 sockets_shutdown(void)
352 struct server *srv, *tsrv;
353 struct socket *sock, *tsock;
355 sockets_purge(gotwebd_env);
357 /* clean sockets */
358 TAILQ_FOREACH_SAFE(sock, &gotwebd_env->sockets, entry, tsock) {
359 TAILQ_REMOVE(&gotwebd_env->sockets, sock, entry);
360 close(sock->fd);
361 free(sock);
364 /* clean servers */
365 TAILQ_FOREACH_SAFE(srv, &gotwebd_env->servers, entry, tsrv)
366 free(srv);
368 free(gotwebd_env);
371 int
372 sockets_privinit(struct gotwebd *env, struct socket *sock)
374 struct privsep *ps = env->gotwebd_ps;
376 if (sock->conf.af_type == AF_UNIX) {
377 log_debug("%s: initializing unix socket %s", __func__,
378 sock->conf.unix_socket_name);
379 sock->fd = sockets_unix_socket_listen(ps, sock);
380 if (sock->fd == -1) {
381 log_warnx("%s: create unix socket failed", __func__);
382 return -1;
386 if (sock->conf.af_type == AF_INET || sock->conf.af_type == AF_INET6) {
387 log_debug("%s: initializing %s FCGI socket on port %d for %s",
388 __func__, sock->conf.af_type == AF_INET ? "inet" : "inet6",
389 sock->conf.fcgi_socket_port, sock->conf.name);
390 sock->fd = sockets_create_socket(&sock->conf.addr,
391 sock->conf.fcgi_socket_port);
392 if (sock->fd == -1) {
393 log_warnx("%s: create FCGI socket failed", __func__);
394 return -1;
398 return 0;
401 static int
402 sockets_unix_socket_listen(struct privsep *ps, struct socket *sock)
404 struct gotwebd *env = ps->ps_env;
405 struct sockaddr_un sun;
406 struct socket *tsock;
407 int u_fd = -1;
408 mode_t old_umask, mode;
410 TAILQ_FOREACH(tsock, &env->sockets, entry) {
411 if (strcmp(tsock->conf.unix_socket_name,
412 sock->conf.unix_socket_name) == 0 &&
413 tsock->fd != -1)
414 return (tsock->fd);
417 u_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
418 if (u_fd == -1) {
419 log_warn("%s: socket", __func__);
420 return -1;
423 sun.sun_family = AF_UNIX;
424 if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
425 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
426 log_warn("%s: %s name too long", __func__,
427 sock->conf.unix_socket_name);
428 close(u_fd);
429 return -1;
432 if (unlink(sock->conf.unix_socket_name) == -1) {
433 if (errno != ENOENT) {
434 log_warn("%s: unlink %s", __func__,
435 sock->conf.unix_socket_name);
436 close(u_fd);
437 return -1;
441 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
442 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
444 if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
445 log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
446 close(u_fd);
447 (void)umask(old_umask);
448 return -1;
451 (void)umask(old_umask);
453 if (chmod(sock->conf.unix_socket_name, mode) == -1) {
454 log_warn("%s: chmod", __func__);
455 close(u_fd);
456 (void)unlink(sock->conf.unix_socket_name);
457 return -1;
460 if (chown(sock->conf.unix_socket_name, ps->ps_pw->pw_uid,
461 ps->ps_pw->pw_gid) == -1) {
462 log_warn("%s: chown", __func__);
463 close(u_fd);
464 (void)unlink(sock->conf.unix_socket_name);
465 return -1;
468 if (listen(u_fd, SOCKS_BACKLOG) == -1) {
469 log_warn("%s: listen", __func__);
470 return -1;
473 return u_fd;
476 static int
477 sockets_create_socket(struct address *a, in_port_t port)
479 struct addrinfo hints;
480 int fd = -1, o_val = 1, flags;
482 memset(&hints, 0, sizeof(hints));
483 hints.ai_family = AF_UNSPEC;
484 hints.ai_socktype = SOCK_STREAM;
485 hints.ai_flags |= AI_PASSIVE;
487 switch (a->ss.ss_family) {
488 case AF_INET:
489 ((struct sockaddr_in *)(&a->ss))->sin_port = htons(port);
490 break;
491 case AF_INET6:
492 ((struct sockaddr_in6 *)(&a->ss))->sin6_port = htons(port);
493 break;
494 default:
495 log_warnx("%s: unknown address family", __func__);
496 return -1;
499 fd = socket(a->ss.ss_family, hints.ai_socktype, a->ipproto);
500 if (fd == -1)
501 return -1;
503 log_debug("%s: opened socket (%d) for %s", __func__,
504 fd, a->ifname);
506 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
507 sizeof(int)) == -1) {
508 log_warn("%s: setsockopt error", __func__);
509 close(fd);
510 return -1;
513 /* non-blocking */
514 flags = fcntl(fd, F_GETFL);
515 flags |= O_NONBLOCK;
516 if (fcntl(fd, F_SETFL, flags) == -1) {
517 log_info("%s: could not enable non-blocking I/O", __func__);
518 close(fd);
519 return -1;
522 if (bind(fd, (struct sockaddr *)&a->ss, a->ss.ss_len) == -1) {
523 close(fd);
524 log_info("%s: can't bind to port %d", __func__,
525 ntohs(port));
526 return -1;
529 if (listen(fd, SOMAXCONN) == -1) {
530 log_warn("%s, unable to listen on socket", __func__);
531 close(fd);
532 return -1;
535 return (fd);
538 static int
539 sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
540 int reserve, volatile int *counter)
542 int ret;
544 if (getdtablecount() + reserve +
545 ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
546 log_debug("inflight fds exceeded");
547 errno = EMFILE;
548 return -1;
551 if ((ret = accept4(sockfd, addr, addrlen,
552 SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
553 (*counter)++;
554 log_debug("inflight incremented, now %d", *counter);
557 return ret;
560 static void
561 sockets_accept_paused(int fd, short events, void *arg)
563 struct socket *sock = (struct socket *)arg;
565 event_add(&sock->ev, NULL);
568 void
569 sockets_socket_accept(int fd, short event, void *arg)
571 struct socket *sock = (struct socket *)arg;
572 struct sockaddr_storage ss;
573 struct timeval backoff;
574 struct request *c = NULL;
575 socklen_t len;
576 int s;
578 backoff.tv_sec = 1;
579 backoff.tv_usec = 0;
581 event_add(&sock->ev, NULL);
582 if (event & EV_TIMEOUT)
583 return;
585 len = sizeof(ss);
587 s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
588 FD_RESERVE, &cgi_inflight);
590 if (s == -1) {
591 switch (errno) {
592 case EINTR:
593 case EWOULDBLOCK:
594 case ECONNABORTED:
595 return;
596 case EMFILE:
597 case ENFILE:
598 event_del(&sock->ev);
599 evtimer_add(&sock->pause, &backoff);
600 return;
601 default:
602 log_warn("%s: accept", __func__);
606 if (client_cnt > GOTWEBD_MAXCLIENTS)
607 goto err;
609 c = calloc(1, sizeof(struct request));
610 if (c == NULL) {
611 log_warn("%s", __func__);
612 close(s);
613 cgi_inflight--;
614 return;
617 c->fd = s;
618 c->sock = sock;
619 memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
620 c->buf_pos = 0;
621 c->buf_len = 0;
622 c->request_started = 0;
623 c->sock->client_status = CLIENT_CONNECT;
625 event_set(&c->ev, s, EV_READ, fcgi_request, c);
626 event_add(&c->ev, NULL);
628 evtimer_set(&c->tmo, fcgi_timeout, c);
629 evtimer_add(&c->tmo, &timeout);
631 client_cnt++;
633 return;
634 err:
635 cgi_inflight--;
636 close(s);
637 if (c != NULL)
638 free(c);
641 static void
642 sockets_rlimit(int maxfd)
644 struct rlimit rl;
646 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
647 fatal("%s: failed to get resource limit", __func__);
648 log_debug("%s: max open files %llu", __func__,
649 (unsigned long long)rl.rlim_max);
651 /*
652 * Allow the maximum number of open file descriptors for this
653 * login class (which should be the class "daemon" by default).
654 */
655 if (maxfd == -1)
656 rl.rlim_cur = rl.rlim_max;
657 else
658 rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
659 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
660 fatal("%s: failed to set resource limit", __func__);