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>
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.
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.
20 #include <sys/param.h>
21 #include <sys/ioctl.h>
22 #include <sys/queue.h>
25 #include <sys/resource.h>
26 #include <sys/socket.h>
29 #include <sys/types.h>
34 #include <netinet/in.h>
52 #include "got_error.h"
53 #include "got_opentemp.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_socket_af(struct sockaddr_storage *, in_port_t);
79 int sockets_accept_reserve(int, struct sockaddr *, socklen_t *, int,
83 *sockets_conf_new_socket(struct gotwebd *, struct server *, int, int,
88 static struct privsep_proc procs[] = {
89 { "gotwebd", PROC_GOTWEBD, sockets_dispatch_gotwebd },
93 sockets(struct privsep *ps, struct privsep_proc *p)
95 proc_run(ps, p, procs, nitems(procs), sockets_run, NULL);
99 sockets_run(struct privsep *ps, struct privsep_proc *p, void *arg)
101 if (config_init(ps->ps_env) == -1)
102 fatal("failed to initialize configuration");
104 p->p_shutdown = sockets_shutdown;
108 signal_del(&ps->ps_evsigchld);
109 signal_set(&ps->ps_evsigchld, SIGCHLD, sockets_sighdlr, ps);
110 signal_add(&ps->ps_evsigchld, NULL);
113 if (pledge("stdio rpath wpath cpath inet recvfd proc exec sendfd",
120 sockets_parse_sockets(struct gotwebd *env)
123 struct socket *sock, *new_sock = NULL;
125 int sock_id = 0, ipv4 = 0, ipv6 = 0;
127 TAILQ_FOREACH(srv, env->servers, entry) {
128 if (srv->unix_socket) {
130 new_sock = sockets_conf_new_socket(env, srv,
132 TAILQ_INSERT_TAIL(env->sockets, new_sock, entry);
135 if (srv->fcgi_socket) {
137 new_sock = sockets_conf_new_socket(env, srv,
139 TAILQ_INSERT_TAIL(env->sockets, new_sock, entry);
141 /* add ipv6 children */
142 TAILQ_FOREACH(sock, env->sockets, entry) {
145 TAILQ_FOREACH(a, sock->conf.al, entry) {
146 if (a->ss.ss_family == AF_INET)
148 if (a->ss.ss_family == AF_INET6)
152 /* create ipv6 sock */
153 if (ipv4 == 1 && ipv6 == 1) {
155 sock->conf.child_id = sock_id;
156 new_sock = sockets_conf_new_socket(env,
157 srv, sock_id, FCGI, 1);
158 sockets_dup_new_socket(sock, new_sock);
159 TAILQ_INSERT_TAIL(env->sockets,
169 sockets_dup_new_socket(struct socket *p_sock, struct socket *sock)
171 struct address *a, *acp;
174 sock->conf.parent_id = p_sock->conf.id;
178 memcpy(&sock->conf.srv_name, p_sock->conf.srv_name,
179 sizeof(sock->conf.srv_name));
181 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_child",
182 p_sock->conf.srv_name);
184 free(p_sock->conf.al);
188 fatalx("%s: snprintf", __func__);
191 TAILQ_FOREACH(a, p_sock->conf.al, entry) {
192 if (a->ss.ss_family == AF_INET)
195 if ((acp = calloc(1, sizeof(*acp))) == NULL)
196 fatal("%s: calloc", __func__);
197 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
198 acp->ipproto = a->ipproto;
199 acp->prefixlen = a->prefixlen;
201 if (strlen(a->ifname) != 0) {
202 if (strlcpy(acp->ifname, a->ifname,
203 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
204 fatalx("%s: interface name truncated",
209 TAILQ_INSERT_TAIL(sock->conf.al, acp, entry);
214 sockets_conf_new_socket(struct gotwebd *env, struct server *srv, int id,
215 int type, int is_dup)
218 struct address *a, *acp;
221 if ((sock = calloc(1, sizeof(*sock))) == NULL)
222 fatalx("%s: calloc", __func__);
224 if ((sock->conf.al = calloc(1, sizeof(*sock->conf.al))) == NULL) {
226 fatalx("%s: calloc", __func__);
228 TAILQ_INIT(sock->conf.al);
230 sock->conf.parent_id = 0;
235 sock->conf.type = type;
238 if (strlcpy(sock->conf.unix_socket_name,
239 srv->unix_socket_name,
240 sizeof(sock->conf.unix_socket_name)) >=
241 sizeof(sock->conf.unix_socket_name)) {
244 fatalx("%s: strlcpy", __func__);
249 sock->conf.fcgi_socket_port = srv->fcgi_socket_port;
254 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
259 fatalx("%s: snprintf", __func__);
262 if (strlcpy(sock->conf.srv_name, srv->name,
263 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
266 fatalx("%s: strlcpy", __func__);
269 TAILQ_FOREACH(a, srv->al, entry) {
270 if ((acp = calloc(1, sizeof(*acp))) == NULL) {
273 fatal("%s: calloc", __func__);
275 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
276 acp->ipproto = a->ipproto;
277 acp->prefixlen = a->prefixlen;
279 if (strlen(a->ifname) != 0) {
280 if (strlcpy(acp->ifname, a->ifname,
281 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
282 fatalx("%s: interface name truncated",
287 TAILQ_INSERT_TAIL(sock->conf.al, acp, entry);
294 sockets_socket_af(struct sockaddr_storage *ss, in_port_t port)
296 switch (ss->ss_family) {
298 ((struct sockaddr_in *)ss)->sin_port = port;
299 ((struct sockaddr_in *)ss)->sin_len =
300 sizeof(struct sockaddr_in);
303 ((struct sockaddr_in6 *)ss)->sin6_port = port;
304 ((struct sockaddr_in6 *)ss)->sin6_len =
305 sizeof(struct sockaddr_in6);
319 TAILQ_FOREACH(sock, gotwebd_env->sockets, entry) {
320 log_debug("%s: configuring socket %d (%d)", __func__,
321 sock->conf.id, sock->fd);
323 event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
324 sockets_socket_accept, sock);
326 if (event_add(&sock->ev, NULL))
327 fatalx("event add sock");
329 evtimer_set(&sock->pause, sockets_accept_paused, sock);
331 log_debug("%s: running socket listener %d", __func__,
337 sockets_purge(struct gotwebd *env)
339 struct socket *sock, *tsock;
341 /* shutdown and remove sockets */
342 TAILQ_FOREACH_SAFE(sock, env->sockets, entry, tsock) {
343 if (event_initialized(&sock->ev))
344 event_del(&sock->ev);
345 if (evtimer_initialized(&sock->evt))
346 evtimer_del(&sock->evt);
347 if (evtimer_initialized(&sock->pause))
348 evtimer_del(&sock->pause);
351 TAILQ_REMOVE(env->sockets, sock, entry);
356 sockets_dispatch_gotwebd(int fd, struct privsep_proc *p, struct imsg *imsg)
358 struct privsep *ps = p->p_ps;
359 int res = 0, cmd = 0, verbose;
361 switch (imsg->hdr.type) {
363 config_getserver(gotwebd_env, imsg);
366 config_getsock(gotwebd_env, imsg);
369 config_getfd(gotwebd_env, imsg);
372 config_getcfg(gotwebd_env, imsg);
377 case IMSG_CTL_VERBOSE:
378 IMSG_SIZE_CHECK(imsg, &verbose);
379 memcpy(&verbose, imsg->data, sizeof(verbose));
380 log_setverbose(verbose);
390 if (proc_compose_imsg(ps, PROC_GOTWEBD, -1, cmd,
391 imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
400 sockets_sighdlr(int sig, short event, void *arg)
404 log_info("%s: ignoring SIGHUP", __func__);
407 log_info("%s: ignoring SIGPIPE", __func__);
410 log_info("%s: ignoring SIGUSR1", __func__);
415 log_info("SIGNAL: %d", sig);
416 fatalx("unexpected signal");
421 sockets_shutdown(void)
423 struct server *srv, *tsrv;
424 struct socket *sock, *tsock;
426 sockets_purge(gotwebd_env);
429 TAILQ_FOREACH_SAFE(sock, gotwebd_env->sockets, entry, tsock) {
430 TAILQ_REMOVE(gotwebd_env->sockets, sock, entry);
436 TAILQ_FOREACH_SAFE(srv, gotwebd_env->servers, entry, tsrv)
439 free(gotwebd_env->sockets);
440 free(gotwebd_env->servers);
445 sockets_privinit(struct gotwebd *env, struct socket *sock)
447 struct privsep *ps = env->gotwebd_ps;
449 if (sock->conf.type == UNIX) {
450 log_debug("%s: initializing unix socket %s", __func__,
451 sock->conf.unix_socket_name);
452 sock->fd = sockets_unix_socket_listen(ps, sock);
453 if (sock->fd == -1) {
454 log_warnx("%s: create unix socket failed", __func__);
459 if (sock->conf.type == FCGI) {
460 log_debug("%s: initializing fcgi socket for %s", __func__,
462 sock->fd = sockets_create_socket(sock->conf.al,
463 sock->conf.fcgi_socket_port);
464 if (sock->fd == -1) {
465 log_warnx("%s: create unix socket failed", __func__);
474 sockets_unix_socket_listen(struct privsep *ps, struct socket *sock)
476 struct gotwebd *env = ps->ps_env;
477 struct sockaddr_un sun;
478 struct socket *tsock;
480 mode_t old_umask, mode;
482 TAILQ_FOREACH(tsock, env->sockets, entry) {
483 if (strcmp(tsock->conf.unix_socket_name,
484 sock->conf.unix_socket_name) == 0 &&
489 u_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
491 log_warn("%s: socket", __func__);
495 sun.sun_family = AF_UNIX;
496 if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
497 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
498 log_warn("%s: %s name too long", __func__,
499 sock->conf.unix_socket_name);
504 if (unlink(sock->conf.unix_socket_name) == -1) {
505 if (errno != ENOENT) {
506 log_warn("%s: unlink %s", __func__,
507 sock->conf.unix_socket_name);
513 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
514 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
516 if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
517 log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
519 (void)umask(old_umask);
523 (void)umask(old_umask);
525 if (chmod(sock->conf.unix_socket_name, mode) == -1) {
526 log_warn("%s: chmod", __func__);
528 (void)unlink(sock->conf.unix_socket_name);
532 if (chown(sock->conf.unix_socket_name, ps->ps_pw->pw_uid,
533 ps->ps_pw->pw_gid) == -1) {
534 log_warn("%s: chown", __func__);
536 (void)unlink(sock->conf.unix_socket_name);
540 if (listen(u_fd, SOCKS_BACKLOG) == -1) {
541 log_warn("%s: listen", __func__);
549 sockets_create_socket(struct addresslist *al, in_port_t port)
551 struct addrinfo hints;
553 int fd = -1, o_val = 1, flags;
555 memset(&hints, 0, sizeof(hints));
556 hints.ai_family = AF_UNSPEC;
557 hints.ai_socktype = SOCK_STREAM;
558 hints.ai_flags |= AI_PASSIVE;
560 TAILQ_FOREACH(a, al, entry) {
561 if (sockets_socket_af(&a->ss, port) == -1) {
562 log_warnx("%s: sockets_socket_af", __func__);
566 fd = socket(a->ss.ss_family, hints.ai_socktype,
568 log_debug("%s: opening socket (%d) for %s", __func__,
571 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
572 sizeof(int)) == -1) {
573 log_warn("%s: setsockopt error", __func__);
578 flags = fcntl(fd, F_GETFL);
580 fcntl(fd, F_SETFL, flags);
582 if (bind(fd, (struct sockaddr *)&a->ss, a->ss.ss_len) == -1) {
584 log_info("%s: can't bind to port %d", __func__,
589 if (listen(fd, SOMAXCONN) == -1) {
590 log_warn("%s, unable to listen on socket", __func__);
603 sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
604 int reserve, volatile int *counter)
608 if (getdtablecount() + reserve +
609 ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
610 log_debug("inflight fds exceeded");
615 if ((ret = accept4(sockfd, addr, addrlen,
616 SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
618 log_debug("inflight incremented, now %d", *counter);
625 sockets_accept_paused(int fd, short events, void *arg)
627 struct socket *sock = (struct socket *)arg;
629 event_add(&sock->ev, NULL);
633 sockets_socket_accept(int fd, short event, void *arg)
635 struct socket *sock = (struct socket *)arg;
636 struct sockaddr_storage ss;
637 struct timeval backoff;
638 struct request *c = NULL;
645 event_add(&sock->ev, NULL);
646 if (event & EV_TIMEOUT)
651 s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
652 FD_RESERVE, &cgi_inflight);
662 event_del(&sock->ev);
663 evtimer_add(&sock->pause, &backoff);
666 log_warn("%s: accept", __func__);
670 if (client_cnt > GOTWEBD_MAXCLIENTS)
673 c = calloc(1, sizeof(struct request));
675 log_warn("%s", __func__);
683 memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
686 c->request_started = 0;
687 c->sock->client_status = CLIENT_CONNECT;
689 event_set(&c->ev, s, EV_READ, fcgi_request, c);
690 event_add(&c->ev, NULL);
692 evtimer_set(&c->tmo, fcgi_timeout, c);
693 evtimer_add(&c->tmo, &timeout);
706 sockets_rlimit(int maxfd)
710 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
711 fatal("%s: failed to get resource limit", __func__);
712 log_debug("%s: max open files %llu", __func__,
713 (unsigned long long)rl.rlim_max);
716 * Allow the maximum number of open file descriptors for this
717 * login class (which should be the class "daemon" by default).
720 rl.rlim_cur = rl.rlim_max;
722 rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
723 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
724 fatal("%s: failed to set resource limit", __func__);