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"
54 #include "got_reference.h"
55 #include "got_repository.h"
56 #include "got_privsep.h"
58 #include "gotwebd.h"
59 #include "log.h"
60 #include "tmpl.h"
62 #define SOCKS_BACKLOG 5
63 #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
65 volatile int client_cnt;
67 static struct timeval timeout = { TIMEOUT_DEFAULT, 0 };
69 static void sockets_sighdlr(int, short, void *);
70 static void sockets_shutdown(void);
71 static void sockets_launch(void);
72 static void sockets_purge(struct gotwebd *);
73 static void sockets_accept_paused(int, short, void *);
74 static void sockets_rlimit(int);
76 static void sockets_dispatch_main(int, short, void *);
77 static int sockets_unix_socket_listen(struct gotwebd *, struct socket *);
78 static int sockets_create_socket(struct address *);
79 static int sockets_accept_reserve(int, struct sockaddr *, socklen_t *,
80 int, volatile int *);
82 static struct socket *sockets_conf_new_socket(struct gotwebd *,
83 int, struct address *);
85 int cgi_inflight = 0;
87 void
88 sockets(struct gotwebd *env, int fd)
89 {
90 struct event sighup, sigint, sigusr1, sigchld, sigterm;
92 event_init();
94 sockets_rlimit(-1);
96 if (config_init(env) == -1)
97 fatal("failed to initialize configuration");
99 if ((env->iev_parent = malloc(sizeof(*env->iev_parent))) == NULL)
100 fatal("malloc");
101 imsg_init(&env->iev_parent->ibuf, fd);
102 env->iev_parent->handler = sockets_dispatch_main;
103 env->iev_parent->data = env->iev_parent;
104 event_set(&env->iev_parent->ev, fd, EV_READ, sockets_dispatch_main,
105 env->iev_parent);
106 event_add(&env->iev_parent->ev, NULL);
108 signal(SIGPIPE, SIG_IGN);
110 signal_set(&sighup, SIGHUP, sockets_sighdlr, env);
111 signal_add(&sighup, NULL);
112 signal_set(&sigint, SIGINT, sockets_sighdlr, env);
113 signal_add(&sigint, NULL);
114 signal_set(&sigusr1, SIGUSR1, sockets_sighdlr, env);
115 signal_add(&sigusr1, NULL);
116 signal_set(&sigchld, SIGCHLD, sockets_sighdlr, env);
117 signal_add(&sigchld, NULL);
118 signal_set(&sigterm, SIGTERM, sockets_sighdlr, env);
119 signal_add(&sigterm, NULL);
121 #ifndef PROFILE
122 if (pledge("stdio rpath inet recvfd proc exec sendfd unveil",
123 NULL) == -1)
124 fatal("pledge");
125 #endif
127 event_dispatch();
128 sockets_shutdown();
131 void
132 sockets_parse_sockets(struct gotwebd *env)
134 struct address *a;
135 struct socket *new_sock = NULL;
136 int sock_id = 1;
138 TAILQ_FOREACH(a, &env->addresses, entry) {
139 new_sock = sockets_conf_new_socket(env, sock_id, a);
140 if (new_sock) {
141 sock_id++;
142 TAILQ_INSERT_TAIL(&env->sockets,
143 new_sock, entry);
148 static struct socket *
149 sockets_conf_new_socket(struct gotwebd *env, int id, struct address *a)
151 struct socket *sock;
152 struct address *acp;
154 if ((sock = calloc(1, sizeof(*sock))) == NULL)
155 fatalx("%s: calloc", __func__);
157 sock->conf.id = id;
158 sock->fd = -1;
159 sock->conf.af_type = a->ss.ss_family;
161 if (a->ss.ss_family == AF_UNIX) {
162 struct sockaddr_un *sun;
164 sun = (struct sockaddr_un *)&a->ss;
165 if (strlcpy(sock->conf.unix_socket_name, sun->sun_path,
166 sizeof(sock->conf.unix_socket_name)) >=
167 sizeof(sock->conf.unix_socket_name))
168 fatalx("unix socket path too long: %s", sun->sun_path);
171 sock->conf.fcgi_socket_port = a->port;
173 acp = &sock->conf.addr;
175 memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
176 acp->slen = a->slen;
177 acp->ai_family = a->ai_family;
178 acp->ai_socktype = a->ai_socktype;
179 acp->ai_protocol = a->ai_protocol;
180 acp->port = a->port;
181 if (*a->ifname != '\0') {
182 if (strlcpy(acp->ifname, a->ifname,
183 sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
184 fatalx("%s: interface name truncated",
185 __func__);
189 return (sock);
192 static void
193 sockets_launch(void)
195 struct socket *sock;
196 struct server *srv;
197 const struct got_error *error;
199 TAILQ_FOREACH(sock, &gotwebd_env->sockets, entry) {
200 log_debug("%s: configuring socket %d (%d)", __func__,
201 sock->conf.id, sock->fd);
203 event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
204 sockets_socket_accept, sock);
206 if (event_add(&sock->ev, NULL))
207 fatalx("event add sock");
209 evtimer_set(&sock->pause, sockets_accept_paused, sock);
211 log_debug("%s: running socket listener %d", __func__,
212 sock->conf.id);
215 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry) {
216 if (unveil(srv->repos_path, "r") == -1)
217 fatal("unveil %s", srv->repos_path);
220 error = got_privsep_unveil_exec_helpers();
221 if (error)
222 fatal("%s", error->msg);
224 if (unveil(NULL, NULL) == -1)
225 fatal("unveil");
228 static void
229 sockets_purge(struct gotwebd *env)
231 struct socket *sock, *tsock;
233 /* shutdown and remove sockets */
234 TAILQ_FOREACH_SAFE(sock, &env->sockets, entry, tsock) {
235 if (event_initialized(&sock->ev))
236 event_del(&sock->ev);
237 if (evtimer_initialized(&sock->evt))
238 evtimer_del(&sock->evt);
239 if (evtimer_initialized(&sock->pause))
240 evtimer_del(&sock->pause);
241 if (sock->fd != -1)
242 close(sock->fd);
243 TAILQ_REMOVE(&env->sockets, sock, entry);
247 static void
248 sockets_dispatch_main(int fd, short event, void *arg)
250 struct imsgev *iev = arg;
251 struct imsgbuf *ibuf;
252 struct imsg imsg;
253 struct gotwebd *env = gotwebd_env;
254 ssize_t n;
255 int shut = 0;
257 ibuf = &iev->ibuf;
259 if (event & EV_READ) {
260 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
261 fatal("imsg_read error");
262 if (n == 0) /* Connection closed */
263 shut = 1;
265 if (event & EV_WRITE) {
266 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
267 fatal("msgbuf_write");
268 if (n == 0) /* Connection closed */
269 shut = 1;
272 for (;;) {
273 if ((n = imsg_get(ibuf, &imsg)) == -1)
274 fatal("imsg_get");
275 if (n == 0) /* No more messages. */
276 break;
278 switch (imsg.hdr.type) {
279 case IMSG_CFG_SRV:
280 config_getserver(env, &imsg);
281 break;
282 case IMSG_CFG_SOCK:
283 config_getsock(env, &imsg);
284 break;
285 case IMSG_CFG_FD:
286 config_getfd(env, &imsg);
287 break;
288 case IMSG_CFG_DONE:
289 config_getcfg(env, &imsg);
290 break;
291 case IMSG_CTL_START:
292 sockets_launch();
293 break;
294 default:
295 fatalx("%s: unknown imsg type %d", __func__,
296 imsg.hdr.type);
299 imsg_free(&imsg);
302 if (!shut)
303 imsg_event_add(iev);
304 else {
305 /* This pipe is dead. Remove its event handler */
306 event_del(&iev->ev);
307 event_loopexit(NULL);
311 static void
312 sockets_sighdlr(int sig, short event, void *arg)
314 switch (sig) {
315 case SIGHUP:
316 log_info("%s: ignoring SIGHUP", __func__);
317 break;
318 case SIGPIPE:
319 log_info("%s: ignoring SIGPIPE", __func__);
320 break;
321 case SIGUSR1:
322 log_info("%s: ignoring SIGUSR1", __func__);
323 break;
324 case SIGCHLD:
325 break;
326 case SIGINT:
327 case SIGTERM:
328 sockets_shutdown();
329 break;
330 default:
331 log_info("SIGNAL: %d", sig);
332 fatalx("unexpected signal");
336 static void
337 sockets_shutdown(void)
339 struct server *srv, *tsrv;
340 struct socket *sock, *tsock;
342 sockets_purge(gotwebd_env);
344 /* clean sockets */
345 TAILQ_FOREACH_SAFE(sock, &gotwebd_env->sockets, entry, tsock) {
346 TAILQ_REMOVE(&gotwebd_env->sockets, sock, entry);
347 close(sock->fd);
348 free(sock);
351 /* clean servers */
352 TAILQ_FOREACH_SAFE(srv, &gotwebd_env->servers, entry, tsrv)
353 free(srv);
355 free(gotwebd_env);
357 exit(0);
360 int
361 sockets_privinit(struct gotwebd *env, struct socket *sock)
363 if (sock->conf.af_type == AF_UNIX) {
364 log_debug("%s: initializing unix socket %s", __func__,
365 sock->conf.unix_socket_name);
366 sock->fd = sockets_unix_socket_listen(env, sock);
367 if (sock->fd == -1) {
368 log_warnx("%s: create unix socket failed", __func__);
369 return -1;
373 if (sock->conf.af_type == AF_INET || sock->conf.af_type == AF_INET6) {
374 log_debug("%s: initializing %s FCGI socket on port %d",
375 __func__, sock->conf.af_type == AF_INET ? "inet" : "inet6",
376 sock->conf.fcgi_socket_port);
377 sock->fd = sockets_create_socket(&sock->conf.addr);
378 if (sock->fd == -1) {
379 log_warnx("%s: create FCGI socket failed", __func__);
380 return -1;
384 return 0;
387 static int
388 sockets_unix_socket_listen(struct gotwebd *env, struct socket *sock)
390 int u_fd = -1;
391 mode_t old_umask, mode;
393 u_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
394 if (u_fd == -1) {
395 log_warn("%s: socket", __func__);
396 return -1;
399 if (unlink(sock->conf.unix_socket_name) == -1) {
400 if (errno != ENOENT) {
401 log_warn("%s: unlink %s", __func__,
402 sock->conf.unix_socket_name);
403 close(u_fd);
404 return -1;
408 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
409 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
411 if (bind(u_fd, (struct sockaddr *)&sock->conf.addr.ss,
412 sock->conf.addr.slen) == -1) {
413 log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
414 close(u_fd);
415 (void)umask(old_umask);
416 return -1;
419 (void)umask(old_umask);
421 if (chmod(sock->conf.unix_socket_name, mode) == -1) {
422 log_warn("%s: chmod", __func__);
423 close(u_fd);
424 (void)unlink(sock->conf.unix_socket_name);
425 return -1;
428 if (chown(sock->conf.unix_socket_name, env->pw->pw_uid,
429 env->pw->pw_gid) == -1) {
430 log_warn("%s: chown", __func__);
431 close(u_fd);
432 (void)unlink(sock->conf.unix_socket_name);
433 return -1;
436 if (listen(u_fd, SOCKS_BACKLOG) == -1) {
437 log_warn("%s: listen", __func__);
438 return -1;
441 return u_fd;
444 static int
445 sockets_create_socket(struct address *a)
447 int fd = -1, o_val = 1, flags;
449 fd = socket(a->ai_family, a->ai_socktype, a->ai_protocol);
450 if (fd == -1)
451 return -1;
453 log_debug("%s: opened socket (%d) for %s", __func__,
454 fd, a->ifname);
456 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
457 sizeof(int)) == -1) {
458 log_warn("%s: setsockopt error", __func__);
459 close(fd);
460 return -1;
463 /* non-blocking */
464 flags = fcntl(fd, F_GETFL);
465 flags |= O_NONBLOCK;
466 if (fcntl(fd, F_SETFL, flags) == -1) {
467 log_info("%s: could not enable non-blocking I/O", __func__);
468 close(fd);
469 return -1;
472 if (bind(fd, (struct sockaddr *)&a->ss, a->slen) == -1) {
473 close(fd);
474 log_info("%s: can't bind to port %d", __func__, a->port);
475 return -1;
478 if (listen(fd, SOMAXCONN) == -1) {
479 log_warn("%s, unable to listen on socket", __func__);
480 close(fd);
481 return -1;
484 return (fd);
487 static int
488 sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
489 int reserve, volatile int *counter)
491 int ret;
493 if (getdtablecount() + reserve +
494 ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
495 log_debug("inflight fds exceeded");
496 errno = EMFILE;
497 return -1;
500 if ((ret = accept4(sockfd, addr, addrlen,
501 SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
502 (*counter)++;
503 log_debug("inflight incremented, now %d", *counter);
506 return ret;
509 static void
510 sockets_accept_paused(int fd, short events, void *arg)
512 struct socket *sock = (struct socket *)arg;
514 event_add(&sock->ev, NULL);
517 void
518 sockets_socket_accept(int fd, short event, void *arg)
520 struct socket *sock = (struct socket *)arg;
521 struct sockaddr_storage ss;
522 struct timeval backoff;
523 struct request *c = NULL;
524 socklen_t len;
525 int s;
527 backoff.tv_sec = 1;
528 backoff.tv_usec = 0;
530 event_add(&sock->ev, NULL);
531 if (event & EV_TIMEOUT)
532 return;
534 len = sizeof(ss);
536 s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
537 FD_RESERVE, &cgi_inflight);
539 if (s == -1) {
540 switch (errno) {
541 case EINTR:
542 case EWOULDBLOCK:
543 case ECONNABORTED:
544 return;
545 case EMFILE:
546 case ENFILE:
547 event_del(&sock->ev);
548 evtimer_add(&sock->pause, &backoff);
549 return;
550 default:
551 log_warn("%s: accept", __func__);
555 if (client_cnt > GOTWEBD_MAXCLIENTS)
556 goto err;
558 c = calloc(1, sizeof(struct request));
559 if (c == NULL) {
560 log_warn("%s", __func__);
561 close(s);
562 cgi_inflight--;
563 return;
566 c->tp = template(c, &fcgi_write, c->outbuf, sizeof(c->outbuf));
567 if (c->tp == NULL) {
568 log_warn("%s", __func__);
569 close(s);
570 cgi_inflight--;
571 free(c);
572 return;
575 c->fd = s;
576 c->sock = sock;
577 memcpy(c->priv_fd, gotwebd_env->priv_fd, sizeof(c->priv_fd));
578 c->buf_pos = 0;
579 c->buf_len = 0;
580 c->request_started = 0;
581 c->sock->client_status = CLIENT_CONNECT;
583 event_set(&c->ev, s, EV_READ|EV_PERSIST, fcgi_request, c);
584 event_add(&c->ev, NULL);
586 evtimer_set(&c->tmo, fcgi_timeout, c);
587 evtimer_add(&c->tmo, &timeout);
589 client_cnt++;
591 return;
592 err:
593 cgi_inflight--;
594 close(s);
595 if (c != NULL)
596 free(c);
599 static void
600 sockets_rlimit(int maxfd)
602 struct rlimit rl;
604 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
605 fatal("%s: failed to get resource limit", __func__);
606 log_debug("%s: max open files %llu", __func__,
607 (unsigned long long)rl.rlim_max);
609 /*
610 * Allow the maximum number of open file descriptors for this
611 * login class (which should be the class "daemon" by default).
612 */
613 if (maxfd == -1)
614 rl.rlim_cur = rl.rlim_max;
615 else
616 rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
617 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
618 fatal("%s: failed to set resource limit", __func__);