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_rlimit(int);
74 int sockets_dispatch_gotwebd(int, struct privsep_proc *, struct imsg *);
75 int sockets_unix_socket_listen(struct privsep *, struct socket *);
76 int sockets_create_socket(struct addresslist *, in_port_t);
77 int sockets_accept_reserve(int, struct sockaddr *, socklen_t *, int,
78 volatile int *);
80 struct socket *sockets_conf_new_socket(struct gotwebd *, struct server *,
81 int, int);
83 int cgi_inflight = 0;
85 static struct privsep_proc procs[] = {
86 { "gotwebd", PROC_GOTWEBD, sockets_dispatch_gotwebd },
87 };
89 void
90 sockets(struct privsep *ps, struct privsep_proc *p)
91 {
92 proc_run(ps, p, procs, nitems(procs), sockets_run, NULL);
93 }
95 void
96 sockets_run(struct privsep *ps, struct privsep_proc *p, void *arg)
97 {
98 if (config_init(ps->ps_env) == -1)
99 fatal("failed to initialize configuration");
101 p->p_shutdown = sockets_shutdown;
103 sockets_rlimit(-1);
105 signal_del(&ps->ps_evsigchld);
106 signal_set(&ps->ps_evsigchld, SIGCHLD, sockets_sighdlr, ps);
107 signal_add(&ps->ps_evsigchld, NULL);
109 #ifndef PROFILE
110 if (pledge("stdio rpath wpath cpath inet recvfd proc exec sendfd",
111 NULL) == -1)
112 fatal("pledge");
113 #endif
116 void
117 sockets_parse_sockets(struct gotwebd *env)
119 struct server *srv;
120 struct socket *new_sock = NULL;
121 int sock_id = 0, ipv4 = 0, ipv6 = 0;
123 TAILQ_FOREACH(srv, &env->servers, entry) {
124 if (srv->unix_socket) {
125 sock_id++;
126 new_sock = sockets_conf_new_socket(env, srv,
127 sock_id, AF_UNIX);
128 /* Should always succeed. */
129 TAILQ_INSERT_TAIL(&env->sockets, new_sock, entry);
132 if (srv->fcgi_socket) {
133 sock_id++;
134 new_sock = sockets_conf_new_socket(env, srv, sock_id,
135 AF_INET);
136 if (new_sock) {
137 TAILQ_INSERT_TAIL(&env->sockets, new_sock,
138 entry);
139 ipv4 = 1;
140 sock_id++;
143 new_sock = sockets_conf_new_socket(env, srv, sock_id,
144 AF_INET6);
145 if (new_sock) {
146 TAILQ_INSERT_TAIL(&env->sockets,
147 new_sock, entry);
148 ipv6 = 1;
151 if (ipv4 == 0 && ipv6 == 0) {
152 fatalx("%s: have no IP addresses to listen "
153 "for FCGI connections", __func__);
159 /* Returns NULL if no addresses in the requested family are configured. */
160 struct socket *
161 sockets_conf_new_socket(struct gotwebd *env, struct server *srv, int id,
162 int af_type)
164 struct socket *sock;
165 struct address *a, *acp;
166 int n;
168 if ((sock = calloc(1, sizeof(*sock))) == NULL)
169 fatalx("%s: calloc", __func__);
171 TAILQ_INIT(&sock->conf.al);
173 sock->conf.id = id;
174 sock->fd = -1;
175 sock->conf.af_type = af_type;
177 if (af_type == AF_UNIX) {
178 if (strlcpy(sock->conf.unix_socket_name,
179 srv->unix_socket_name,
180 sizeof(sock->conf.unix_socket_name)) >=
181 sizeof(sock->conf.unix_socket_name)) {
182 free(sock);
183 fatalx("%s: strlcpy", __func__);
187 sock->conf.fcgi_socket_port = srv->fcgi_socket_port;
189 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
190 srv->name);
191 if (n < 0 || (size_t)n >= GOTWEBD_MAXTEXT) {
192 free(sock);
193 fatalx("%s: snprintf", __func__);
196 if (strlcpy(sock->conf.srv_name, srv->name,
197 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
198 free(sock);
199 fatalx("%s: strlcpy", __func__);
202 TAILQ_FOREACH(a, &srv->al, entry) {
203 if (a->ss.ss_family != af_type)
204 continue;
206 if ((acp = calloc(1, sizeof(*acp))) == NULL) {
207 free(sock);
208 fatal("%s: calloc", __func__);
210 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
211 acp->ipproto = a->ipproto;
212 acp->prefixlen = a->prefixlen;
213 acp->port = a->port;
214 if (strlen(a->ifname) != 0) {
215 if (strlcpy(acp->ifname, a->ifname,
216 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
217 fatalx("%s: interface name truncated",
218 __func__);
222 TAILQ_INSERT_TAIL(&sock->conf.al, acp, entry);
225 if ((af_type == AF_INET || af_type == AF_INET6) &&
226 TAILQ_EMPTY(&sock->conf.al)) {
227 free(sock);
228 return NULL;
231 return (sock);
234 void
235 sockets_launch(void)
237 struct socket *sock;
239 TAILQ_FOREACH(sock, &gotwebd_env->sockets, entry) {
240 log_debug("%s: configuring socket %d (%d)", __func__,
241 sock->conf.id, sock->fd);
243 event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
244 sockets_socket_accept, sock);
246 if (event_add(&sock->ev, NULL))
247 fatalx("event add sock");
249 evtimer_set(&sock->pause, sockets_accept_paused, sock);
251 log_debug("%s: running socket listener %d", __func__,
252 sock->conf.id);
256 void
257 sockets_purge(struct gotwebd *env)
259 struct socket *sock, *tsock;
261 /* shutdown and remove sockets */
262 TAILQ_FOREACH_SAFE(sock, &env->sockets, entry, tsock) {
263 if (event_initialized(&sock->ev))
264 event_del(&sock->ev);
265 if (evtimer_initialized(&sock->evt))
266 evtimer_del(&sock->evt);
267 if (evtimer_initialized(&sock->pause))
268 evtimer_del(&sock->pause);
269 if (sock->fd != -1)
270 close(sock->fd);
271 TAILQ_REMOVE(&env->sockets, sock, entry);
275 int
276 sockets_dispatch_gotwebd(int fd, struct privsep_proc *p, struct imsg *imsg)
278 struct privsep *ps = p->p_ps;
279 int res = 0, cmd = 0, verbose;
281 switch (imsg->hdr.type) {
282 case IMSG_CFG_SRV:
283 config_getserver(gotwebd_env, imsg);
284 break;
285 case IMSG_CFG_SOCK:
286 config_getsock(gotwebd_env, imsg);
287 break;
288 case IMSG_CFG_FD:
289 config_getfd(gotwebd_env, imsg);
290 break;
291 case IMSG_CFG_DONE:
292 config_getcfg(gotwebd_env, imsg);
293 break;
294 case IMSG_CTL_START:
295 sockets_launch();
296 break;
297 case IMSG_CTL_VERBOSE:
298 IMSG_SIZE_CHECK(imsg, &verbose);
299 memcpy(&verbose, imsg->data, sizeof(verbose));
300 log_setverbose(verbose);
301 break;
302 default:
303 return -1;
306 switch (cmd) {
307 case 0:
308 break;
309 default:
310 if (proc_compose_imsg(ps, PROC_GOTWEBD, -1, cmd,
311 imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
312 return -1;
313 break;
316 return 0;
319 void
320 sockets_sighdlr(int sig, short event, void *arg)
322 switch (sig) {
323 case SIGHUP:
324 log_info("%s: ignoring SIGHUP", __func__);
325 break;
326 case SIGPIPE:
327 log_info("%s: ignoring SIGPIPE", __func__);
328 break;
329 case SIGUSR1:
330 log_info("%s: ignoring SIGUSR1", __func__);
331 break;
332 case SIGCHLD:
333 break;
334 default:
335 log_info("SIGNAL: %d", sig);
336 fatalx("unexpected signal");
340 void
341 sockets_shutdown(void)
343 struct server *srv, *tsrv;
344 struct socket *sock, *tsock;
346 sockets_purge(gotwebd_env);
348 /* clean sockets */
349 TAILQ_FOREACH_SAFE(sock, &gotwebd_env->sockets, entry, tsock) {
350 TAILQ_REMOVE(&gotwebd_env->sockets, sock, entry);
351 close(sock->fd);
352 free(sock);
355 /* clean servers */
356 TAILQ_FOREACH_SAFE(srv, &gotwebd_env->servers, entry, tsrv)
357 free(srv);
359 free(gotwebd_env);
362 int
363 sockets_privinit(struct gotwebd *env, struct socket *sock)
365 struct privsep *ps = env->gotwebd_ps;
367 if (sock->conf.af_type == AF_UNIX) {
368 log_debug("%s: initializing unix socket %s", __func__,
369 sock->conf.unix_socket_name);
370 sock->fd = sockets_unix_socket_listen(ps, sock);
371 if (sock->fd == -1) {
372 log_warnx("%s: create unix socket failed", __func__);
373 return -1;
377 if (sock->conf.af_type == AF_INET || sock->conf.af_type == AF_INET6) {
378 log_debug("%s: initializing %s FCGI socket on port %d for %s",
379 __func__, sock->conf.af_type == AF_INET ? "inet" : "inet6",
380 sock->conf.fcgi_socket_port, sock->conf.name);
381 sock->fd = sockets_create_socket(&sock->conf.al,
382 sock->conf.fcgi_socket_port);
383 if (sock->fd == -1) {
384 log_warnx("%s: create unix socket failed", __func__);
385 return -1;
389 return 0;
392 int
393 sockets_unix_socket_listen(struct privsep *ps, struct socket *sock)
395 struct gotwebd *env = ps->ps_env;
396 struct sockaddr_un sun;
397 struct socket *tsock;
398 int u_fd = -1;
399 mode_t old_umask, mode;
401 TAILQ_FOREACH(tsock, &env->sockets, entry) {
402 if (strcmp(tsock->conf.unix_socket_name,
403 sock->conf.unix_socket_name) == 0 &&
404 tsock->fd != -1)
405 return (tsock->fd);
408 /* TA: FIXME: this needs upstreaming. */
409 int socket_flags = SOCK_STREAM | SOCK_NONBLOCK;
410 #ifdef SOCK_CLOEXEC
411 socket_flags |= SOCK_CLOEXEC;
412 #endif
413 u_fd = socket(AF_UNIX, socket_flags, 0);
414 if (u_fd == -1) {
415 log_warn("%s: socket", __func__);
416 return -1;
419 sun.sun_family = AF_UNIX;
420 if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
421 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
422 log_warn("%s: %s name too long", __func__,
423 sock->conf.unix_socket_name);
424 close(u_fd);
425 return -1;
428 if (unlink(sock->conf.unix_socket_name) == -1) {
429 if (errno != ENOENT) {
430 log_warn("%s: unlink %s", __func__,
431 sock->conf.unix_socket_name);
432 close(u_fd);
433 return -1;
437 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
438 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
440 if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
441 log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
442 close(u_fd);
443 (void)umask(old_umask);
444 return -1;
447 (void)umask(old_umask);
449 if (chmod(sock->conf.unix_socket_name, mode) == -1) {
450 log_warn("%s: chmod", __func__);
451 close(u_fd);
452 (void)unlink(sock->conf.unix_socket_name);
453 return -1;
456 if (chown(sock->conf.unix_socket_name, ps->ps_pw->pw_uid,
457 ps->ps_pw->pw_gid) == -1) {
458 log_warn("%s: chown", __func__);
459 close(u_fd);
460 (void)unlink(sock->conf.unix_socket_name);
461 return -1;
464 if (listen(u_fd, SOCKS_BACKLOG) == -1) {
465 log_warn("%s: listen", __func__);
466 return -1;
469 return u_fd;
472 int
473 sockets_create_socket(struct addresslist *al, in_port_t port)
475 struct addrinfo hints;
476 struct address *a;
477 int fd = -1, o_val = 1, flags;
479 memset(&hints, 0, sizeof(hints));
480 hints.ai_family = AF_UNSPEC;
481 hints.ai_socktype = SOCK_STREAM;
482 hints.ai_flags |= AI_PASSIVE;
484 TAILQ_FOREACH(a, al, entry) {
485 switch (a->ss.ss_family) {
486 case AF_INET:
487 ((struct sockaddr_in *)(&a->ss))->sin_port = port;
488 break;
489 case AF_INET6:
490 ((struct sockaddr_in6 *)(&a->ss))->sin6_port = port;
491 break;
492 default:
493 log_warnx("%s: unknown address family", __func__);
494 goto fail;
497 fd = socket(a->ss.ss_family, hints.ai_socktype,
498 a->ipproto);
499 log_debug("%s: opening socket (%d) for %s", __func__,
500 fd, a->ifname);
502 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
503 sizeof(int)) == -1) {
504 log_warn("%s: setsockopt error", __func__);
505 return -1;
508 /* non-blocking */
509 flags = fcntl(fd, F_GETFL);
510 flags |= O_NONBLOCK;
511 fcntl(fd, F_SETFL, flags);
513 /* TA: 'sizeof a' vs a->ss.len */
514 if (bind(fd, (struct sockaddr *)&a->ss, sizeof a) == -1) {
515 close(fd);
516 log_info("%s: can't bind to port %d", __func__,
517 ntohs(port));
518 goto fail;
521 if (listen(fd, SOMAXCONN) == -1) {
522 log_warn("%s, unable to listen on socket", __func__);
523 goto fail;
527 free(a);
528 return (fd);
529 fail:
530 free(a);
531 return -1;
534 int
535 sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
536 int reserve, volatile int *counter)
538 int ret;
540 if (getdtablecount() + reserve +
541 ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
542 log_debug("inflight fds exceeded");
543 errno = EMFILE;
544 return -1;
546 /* TA: This needs fixing upstream. */
547 #ifdef __APPLE__
548 ret = accept(sockfd, addr, addrlen);
549 #else
550 ret = accept4(sockfd, addr, addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC);
551 #endif
553 if (ret > -1) {
554 (*counter)++;
555 log_debug("inflight incremented, now %d", *counter);
558 return ret;
561 void
562 sockets_accept_paused(int fd, short events, void *arg)
564 struct socket *sock = (struct socket *)arg;
566 event_add(&sock->ev, NULL);
569 void
570 sockets_socket_accept(int fd, short event, void *arg)
572 struct socket *sock = (struct socket *)arg;
573 struct sockaddr_storage ss;
574 struct timeval backoff;
575 struct request *c = NULL;
576 socklen_t len;
577 int s;
579 backoff.tv_sec = 1;
580 backoff.tv_usec = 0;
582 event_add(&sock->ev, NULL);
583 if (event & EV_TIMEOUT)
584 return;
586 len = sizeof(ss);
588 s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
589 FD_RESERVE, &cgi_inflight);
591 if (s == -1) {
592 switch (errno) {
593 case EINTR:
594 case EWOULDBLOCK:
595 case ECONNABORTED:
596 return;
597 case EMFILE:
598 case ENFILE:
599 event_del(&sock->ev);
600 evtimer_add(&sock->pause, &backoff);
601 return;
602 default:
603 log_warn("%s: accept", __func__);
607 if (client_cnt > GOTWEBD_MAXCLIENTS)
608 goto err;
610 c = calloc(1, sizeof(struct request));
611 if (c == NULL) {
612 log_warn("%s", __func__);
613 close(s);
614 cgi_inflight--;
615 return;
618 c->fd = s;
619 c->sock = sock;
620 memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
621 c->buf_pos = 0;
622 c->buf_len = 0;
623 c->request_started = 0;
624 c->sock->client_status = CLIENT_CONNECT;
626 event_set(&c->ev, s, EV_READ, fcgi_request, c);
627 event_add(&c->ev, NULL);
629 evtimer_set(&c->tmo, fcgi_timeout, c);
630 evtimer_add(&c->tmo, &timeout);
632 client_cnt++;
634 return;
635 err:
636 cgi_inflight--;
637 close(s);
638 if (c != NULL)
639 free(c);
642 void
643 sockets_rlimit(int maxfd)
645 struct rlimit rl;
647 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
648 fatal("%s: failed to get resource limit", __func__);
649 log_debug("%s: max open files %llu", __func__,
650 (unsigned long long)rl.rlim_max);
652 /*
653 * Allow the maximum number of open file descriptors for this
654 * login class (which should be the class "daemon" by default).
655 */
656 if (maxfd == -1)
657 rl.rlim_cur = rl.rlim_max;
658 else
659 rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
660 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
661 fatal("%s: failed to set resource limit", __func__);