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_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 u_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
409 if (u_fd == -1) {
410 log_warn("%s: socket", __func__);
411 return -1;
414 sun.sun_family = AF_UNIX;
415 if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
416 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
417 log_warn("%s: %s name too long", __func__,
418 sock->conf.unix_socket_name);
419 close(u_fd);
420 return -1;
423 if (unlink(sock->conf.unix_socket_name) == -1) {
424 if (errno != ENOENT) {
425 log_warn("%s: unlink %s", __func__,
426 sock->conf.unix_socket_name);
427 close(u_fd);
428 return -1;
432 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
433 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
435 if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
436 log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
437 close(u_fd);
438 (void)umask(old_umask);
439 return -1;
442 (void)umask(old_umask);
444 if (chmod(sock->conf.unix_socket_name, mode) == -1) {
445 log_warn("%s: chmod", __func__);
446 close(u_fd);
447 (void)unlink(sock->conf.unix_socket_name);
448 return -1;
451 if (chown(sock->conf.unix_socket_name, ps->ps_pw->pw_uid,
452 ps->ps_pw->pw_gid) == -1) {
453 log_warn("%s: chown", __func__);
454 close(u_fd);
455 (void)unlink(sock->conf.unix_socket_name);
456 return -1;
459 if (listen(u_fd, SOCKS_BACKLOG) == -1) {
460 log_warn("%s: listen", __func__);
461 return -1;
464 return u_fd;
467 int
468 sockets_create_socket(struct addresslist *al, in_port_t port)
470 struct addrinfo hints;
471 struct address *a;
472 int fd = -1, o_val = 1, flags;
474 memset(&hints, 0, sizeof(hints));
475 hints.ai_family = AF_UNSPEC;
476 hints.ai_socktype = SOCK_STREAM;
477 hints.ai_flags |= AI_PASSIVE;
479 TAILQ_FOREACH(a, al, entry) {
480 switch (a->ss.ss_family) {
481 case AF_INET:
482 ((struct sockaddr_in *)(&a->ss))->sin_port = port;
483 break;
484 case AF_INET6:
485 ((struct sockaddr_in6 *)(&a->ss))->sin6_port = port;
486 break;
487 default:
488 log_warnx("%s: unknown address family", __func__);
489 goto fail;
492 fd = socket(a->ss.ss_family, hints.ai_socktype,
493 a->ipproto);
494 log_debug("%s: opening socket (%d) for %s", __func__,
495 fd, a->ifname);
497 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
498 sizeof(int)) == -1) {
499 log_warn("%s: setsockopt error", __func__);
500 return -1;
503 /* non-blocking */
504 flags = fcntl(fd, F_GETFL);
505 flags |= O_NONBLOCK;
506 fcntl(fd, F_SETFL, flags);
508 if (bind(fd, (struct sockaddr *)&a->ss, a->ss.ss_len) == -1) {
509 close(fd);
510 log_info("%s: can't bind to port %d", __func__,
511 ntohs(port));
512 goto fail;
515 if (listen(fd, SOMAXCONN) == -1) {
516 log_warn("%s, unable to listen on socket", __func__);
517 goto fail;
521 free(a);
522 return (fd);
523 fail:
524 free(a);
525 return -1;
528 int
529 sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
530 int reserve, volatile int *counter)
532 int ret;
534 if (getdtablecount() + reserve +
535 ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
536 log_debug("inflight fds exceeded");
537 errno = EMFILE;
538 return -1;
541 if ((ret = accept4(sockfd, addr, addrlen,
542 SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
543 (*counter)++;
544 log_debug("inflight incremented, now %d", *counter);
547 return ret;
550 void
551 sockets_accept_paused(int fd, short events, void *arg)
553 struct socket *sock = (struct socket *)arg;
555 event_add(&sock->ev, NULL);
558 void
559 sockets_socket_accept(int fd, short event, void *arg)
561 struct socket *sock = (struct socket *)arg;
562 struct sockaddr_storage ss;
563 struct timeval backoff;
564 struct request *c = NULL;
565 socklen_t len;
566 int s;
568 backoff.tv_sec = 1;
569 backoff.tv_usec = 0;
571 event_add(&sock->ev, NULL);
572 if (event & EV_TIMEOUT)
573 return;
575 len = sizeof(ss);
577 s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
578 FD_RESERVE, &cgi_inflight);
580 if (s == -1) {
581 switch (errno) {
582 case EINTR:
583 case EWOULDBLOCK:
584 case ECONNABORTED:
585 return;
586 case EMFILE:
587 case ENFILE:
588 event_del(&sock->ev);
589 evtimer_add(&sock->pause, &backoff);
590 return;
591 default:
592 log_warn("%s: accept", __func__);
596 if (client_cnt > GOTWEBD_MAXCLIENTS)
597 goto err;
599 c = calloc(1, sizeof(struct request));
600 if (c == NULL) {
601 log_warn("%s", __func__);
602 close(s);
603 cgi_inflight--;
604 return;
607 c->fd = s;
608 c->sock = sock;
609 memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
610 c->buf_pos = 0;
611 c->buf_len = 0;
612 c->request_started = 0;
613 c->sock->client_status = CLIENT_CONNECT;
615 event_set(&c->ev, s, EV_READ, fcgi_request, c);
616 event_add(&c->ev, NULL);
618 evtimer_set(&c->tmo, fcgi_timeout, c);
619 evtimer_add(&c->tmo, &timeout);
621 client_cnt++;
623 return;
624 err:
625 cgi_inflight--;
626 close(s);
627 if (c != NULL)
628 free(c);
631 void
632 sockets_rlimit(int maxfd)
634 struct rlimit rl;
636 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
637 fatal("%s: failed to get resource limit", __func__);
638 log_debug("%s: max open files %llu", __func__,
639 (unsigned long long)rl.rlim_max);
641 /*
642 * Allow the maximum number of open file descriptors for this
643 * login class (which should be the class "daemon" by default).
644 */
645 if (maxfd == -1)
646 rl.rlim_cur = rl.rlim_max;
647 else
648 rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
649 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
650 fatal("%s: failed to set resource limit", __func__);