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) {
183 free(p_sock->conf.al);
184 free(p_sock);
185 free(sock->conf.al);
186 free(sock);
187 fatalx("%s: snprintf", __func__);
190 TAILQ_FOREACH(a, p_sock->conf.al, entry) {
191 if (a->ss.ss_family == AF_INET)
192 continue;
194 if ((acp = calloc(1, sizeof(*acp))) == NULL)
195 fatal("%s: calloc", __func__);
196 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
197 acp->ipproto = a->ipproto;
198 acp->prefixlen = a->prefixlen;
199 acp->port = a->port;
200 if (strlen(a->ifname) != 0) {
201 if (strlcpy(acp->ifname, a->ifname,
202 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
203 fatalx("%s: interface name truncated",
204 __func__);
208 TAILQ_INSERT_TAIL(sock->conf.al, acp, entry);
212 struct socket *
213 sockets_conf_new_socket(struct gotwebd *env, struct server *srv, int id,
214 int type, int is_dup)
216 struct socket *sock;
217 struct address *a, *acp;
218 int n;
220 if ((sock = calloc(1, sizeof(*sock))) == NULL)
221 fatalx("%s: calloc", __func__);
223 if ((sock->conf.al = calloc(1, sizeof(*sock->conf.al))) == NULL) {
224 free(sock);
225 fatalx("%s: calloc", __func__);
227 TAILQ_INIT(sock->conf.al);
229 sock->conf.parent_id = 0;
230 sock->conf.id = id;
232 sock->fd = -1;
234 sock->conf.type = type;
236 if (type == UNIX) {
237 if (strlcpy(sock->conf.unix_socket_name,
238 srv->unix_socket_name,
239 sizeof(sock->conf.unix_socket_name)) >=
240 sizeof(sock->conf.unix_socket_name)) {
241 free(sock->conf.al);
242 free(sock);
243 fatalx("%s: strlcpy", __func__);
245 } else
246 sock->conf.ipv4 = 1;
248 sock->conf.fcgi_socket_port = srv->fcgi_socket_port;
250 if (is_dup)
251 goto done;
253 n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
254 srv->name);
255 if (n < 0) {
256 free(sock->conf.al);
257 free(sock);
258 fatalx("%s: snprintf", __func__);
261 if (strlcpy(sock->conf.srv_name, srv->name,
262 sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
263 free(sock->conf.al);
264 free(sock);
265 fatalx("%s: strlcpy", __func__);
268 TAILQ_FOREACH(a, srv->al, entry) {
269 if ((acp = calloc(1, sizeof(*acp))) == NULL) {
270 free(sock->conf.al);
271 free(sock);
272 fatal("%s: calloc", __func__);
274 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
275 acp->ipproto = a->ipproto;
276 acp->prefixlen = a->prefixlen;
277 acp->port = a->port;
278 if (strlen(a->ifname) != 0) {
279 if (strlcpy(acp->ifname, a->ifname,
280 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
281 fatalx("%s: interface name truncated",
282 __func__);
286 TAILQ_INSERT_TAIL(sock->conf.al, acp, entry);
288 done:
289 return (sock);
292 void
293 sockets_launch(void)
295 struct socket *sock;
297 TAILQ_FOREACH(sock, gotwebd_env->sockets, entry) {
298 log_debug("%s: configuring socket %d (%d)", __func__,
299 sock->conf.id, sock->fd);
301 event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
302 sockets_socket_accept, sock);
304 if (event_add(&sock->ev, NULL))
305 fatalx("event add sock");
307 evtimer_set(&sock->pause, sockets_accept_paused, sock);
309 log_debug("%s: running socket listener %d", __func__,
310 sock->conf.id);
314 void
315 sockets_purge(struct gotwebd *env)
317 struct socket *sock, *tsock;
319 /* shutdown and remove sockets */
320 TAILQ_FOREACH_SAFE(sock, env->sockets, entry, tsock) {
321 if (event_initialized(&sock->ev))
322 event_del(&sock->ev);
323 if (evtimer_initialized(&sock->evt))
324 evtimer_del(&sock->evt);
325 if (evtimer_initialized(&sock->pause))
326 evtimer_del(&sock->pause);
327 if (sock->fd != -1)
328 close(sock->fd);
329 TAILQ_REMOVE(env->sockets, sock, entry);
333 int
334 sockets_dispatch_gotwebd(int fd, struct privsep_proc *p, struct imsg *imsg)
336 struct privsep *ps = p->p_ps;
337 int res = 0, cmd = 0, verbose;
339 switch (imsg->hdr.type) {
340 case IMSG_CFG_SRV:
341 config_getserver(gotwebd_env, imsg);
342 break;
343 case IMSG_CFG_SOCK:
344 config_getsock(gotwebd_env, imsg);
345 break;
346 case IMSG_CFG_FD:
347 config_getfd(gotwebd_env, imsg);
348 break;
349 case IMSG_CFG_DONE:
350 config_getcfg(gotwebd_env, imsg);
351 break;
352 case IMSG_CTL_START:
353 sockets_launch();
354 break;
355 case IMSG_CTL_VERBOSE:
356 IMSG_SIZE_CHECK(imsg, &verbose);
357 memcpy(&verbose, imsg->data, sizeof(verbose));
358 log_setverbose(verbose);
359 break;
360 default:
361 return -1;
364 switch (cmd) {
365 case 0:
366 break;
367 default:
368 if (proc_compose_imsg(ps, PROC_GOTWEBD, -1, cmd,
369 imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
370 return -1;
371 break;
374 return 0;
377 void
378 sockets_sighdlr(int sig, short event, void *arg)
380 switch (sig) {
381 case SIGHUP:
382 log_info("%s: ignoring SIGHUP", __func__);
383 break;
384 case SIGPIPE:
385 log_info("%s: ignoring SIGPIPE", __func__);
386 break;
387 case SIGUSR1:
388 log_info("%s: ignoring SIGUSR1", __func__);
389 break;
390 case SIGCHLD:
391 break;
392 default:
393 log_info("SIGNAL: %d", sig);
394 fatalx("unexpected signal");
398 void
399 sockets_shutdown(void)
401 struct server *srv, *tsrv;
402 struct socket *sock, *tsock;
404 sockets_purge(gotwebd_env);
406 /* clean sockets */
407 TAILQ_FOREACH_SAFE(sock, gotwebd_env->sockets, entry, tsock) {
408 TAILQ_REMOVE(gotwebd_env->sockets, sock, entry);
409 close(sock->fd);
410 free(sock);
413 /* clean servers */
414 TAILQ_FOREACH_SAFE(srv, gotwebd_env->servers, entry, tsrv)
415 free(srv);
417 free(gotwebd_env->sockets);
418 free(gotwebd_env->servers);
419 free(gotwebd_env);
422 int
423 sockets_privinit(struct gotwebd *env, struct socket *sock)
425 struct privsep *ps = env->gotwebd_ps;
427 if (sock->conf.type == UNIX) {
428 log_debug("%s: initializing unix socket %s", __func__,
429 sock->conf.unix_socket_name);
430 sock->fd = sockets_unix_socket_listen(ps, sock);
431 if (sock->fd == -1) {
432 log_warnx("%s: create unix socket failed", __func__);
433 return -1;
437 if (sock->conf.type == FCGI) {
438 log_debug("%s: initializing fcgi socket for %s", __func__,
439 sock->conf.name);
440 sock->fd = sockets_create_socket(sock->conf.al,
441 sock->conf.fcgi_socket_port);
442 if (sock->fd == -1) {
443 log_warnx("%s: create unix socket failed", __func__);
444 return -1;
448 return 0;
451 int
452 sockets_unix_socket_listen(struct privsep *ps, struct socket *sock)
454 struct gotwebd *env = ps->ps_env;
455 struct sockaddr_un sun;
456 struct socket *tsock;
457 int u_fd = -1;
458 mode_t old_umask, mode;
460 TAILQ_FOREACH(tsock, env->sockets, entry) {
461 if (strcmp(tsock->conf.unix_socket_name,
462 sock->conf.unix_socket_name) == 0 &&
463 tsock->fd != -1)
464 return (tsock->fd);
467 u_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
468 if (u_fd == -1) {
469 log_warn("%s: socket", __func__);
470 return -1;
473 sun.sun_family = AF_UNIX;
474 if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
475 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
476 log_warn("%s: %s name too long", __func__,
477 sock->conf.unix_socket_name);
478 close(u_fd);
479 return -1;
482 if (unlink(sock->conf.unix_socket_name) == -1) {
483 if (errno != ENOENT) {
484 log_warn("%s: unlink %s", __func__,
485 sock->conf.unix_socket_name);
486 close(u_fd);
487 return -1;
491 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
492 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
494 if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
495 log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
496 close(u_fd);
497 (void)umask(old_umask);
498 return -1;
501 (void)umask(old_umask);
503 if (chmod(sock->conf.unix_socket_name, mode) == -1) {
504 log_warn("%s: chmod", __func__);
505 close(u_fd);
506 (void)unlink(sock->conf.unix_socket_name);
507 return -1;
510 if (chown(sock->conf.unix_socket_name, ps->ps_pw->pw_uid,
511 ps->ps_pw->pw_gid) == -1) {
512 log_warn("%s: chown", __func__);
513 close(u_fd);
514 (void)unlink(sock->conf.unix_socket_name);
515 return -1;
518 if (listen(u_fd, SOCKS_BACKLOG) == -1) {
519 log_warn("%s: listen", __func__);
520 return -1;
523 return u_fd;
526 int
527 sockets_create_socket(struct addresslist *al, in_port_t port)
529 struct addrinfo hints;
530 struct address *a;
531 int fd = -1, o_val = 1, flags;
533 memset(&hints, 0, sizeof(hints));
534 hints.ai_family = AF_UNSPEC;
535 hints.ai_socktype = SOCK_STREAM;
536 hints.ai_flags |= AI_PASSIVE;
538 TAILQ_FOREACH(a, al, entry) {
539 switch (a->ss.ss_family) {
540 case AF_INET:
541 ((struct sockaddr_in *)(&a->ss))->sin_port = port;
542 break;
543 case AF_INET6:
544 ((struct sockaddr_in6 *)(&a->ss))->sin6_port = port;
545 break;
546 default:
547 log_warnx("%s: unknown address family", __func__);
548 goto fail;
551 fd = socket(a->ss.ss_family, hints.ai_socktype,
552 a->ipproto);
553 log_debug("%s: opening socket (%d) for %s", __func__,
554 fd, a->ifname);
556 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
557 sizeof(int)) == -1) {
558 log_warn("%s: setsockopt error", __func__);
559 return -1;
562 /* non-blocking */
563 flags = fcntl(fd, F_GETFL);
564 flags |= O_NONBLOCK;
565 fcntl(fd, F_SETFL, flags);
567 if (bind(fd, (struct sockaddr *)&a->ss, a->ss.ss_len) == -1) {
568 close(fd);
569 log_info("%s: can't bind to port %d", __func__,
570 ntohs(port));
571 goto fail;
574 if (listen(fd, SOMAXCONN) == -1) {
575 log_warn("%s, unable to listen on socket", __func__);
576 goto fail;
580 free(a);
581 return (fd);
582 fail:
583 free(a);
584 return -1;
587 int
588 sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
589 int reserve, volatile int *counter)
591 int ret;
593 if (getdtablecount() + reserve +
594 ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
595 log_debug("inflight fds exceeded");
596 errno = EMFILE;
597 return -1;
600 if ((ret = accept4(sockfd, addr, addrlen,
601 SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
602 (*counter)++;
603 log_debug("inflight incremented, now %d", *counter);
606 return ret;
609 void
610 sockets_accept_paused(int fd, short events, void *arg)
612 struct socket *sock = (struct socket *)arg;
614 event_add(&sock->ev, NULL);
617 void
618 sockets_socket_accept(int fd, short event, void *arg)
620 struct socket *sock = (struct socket *)arg;
621 struct sockaddr_storage ss;
622 struct timeval backoff;
623 struct request *c = NULL;
624 socklen_t len;
625 int s;
627 backoff.tv_sec = 1;
628 backoff.tv_usec = 0;
630 event_add(&sock->ev, NULL);
631 if (event & EV_TIMEOUT)
632 return;
634 len = sizeof(ss);
636 s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
637 FD_RESERVE, &cgi_inflight);
639 if (s == -1) {
640 switch (errno) {
641 case EINTR:
642 case EWOULDBLOCK:
643 case ECONNABORTED:
644 return;
645 case EMFILE:
646 case ENFILE:
647 event_del(&sock->ev);
648 evtimer_add(&sock->pause, &backoff);
649 return;
650 default:
651 log_warn("%s: accept", __func__);
655 if (client_cnt > GOTWEBD_MAXCLIENTS)
656 goto err;
658 c = calloc(1, sizeof(struct request));
659 if (c == NULL) {
660 log_warn("%s", __func__);
661 close(s);
662 cgi_inflight--;
663 return;
666 c->fd = s;
667 c->sock = sock;
668 memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
669 c->buf_pos = 0;
670 c->buf_len = 0;
671 c->request_started = 0;
672 c->sock->client_status = CLIENT_CONNECT;
674 event_set(&c->ev, s, EV_READ, fcgi_request, c);
675 event_add(&c->ev, NULL);
677 evtimer_set(&c->tmo, fcgi_timeout, c);
678 evtimer_add(&c->tmo, &timeout);
680 client_cnt++;
682 return;
683 err:
684 cgi_inflight--;
685 close(s);
686 if (c != NULL)
687 free(c);
690 void
691 sockets_rlimit(int maxfd)
693 struct rlimit rl;
695 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
696 fatal("%s: failed to get resource limit", __func__);
697 log_debug("%s: max open files %llu", __func__,
698 (unsigned long long)rl.rlim_max);
700 /*
701 * Allow the maximum number of open file descriptors for this
702 * login class (which should be the class "daemon" by default).
703 */
704 if (maxfd == -1)
705 rl.rlim_cur = rl.rlim_max;
706 else
707 rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
708 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
709 fatal("%s: failed to set resource limit", __func__);