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 "got_compat.h"
22 #include <sys/param.h>
23 #include <sys/ioctl.h>
24 #include <sys/queue.h>
27 #include <sys/resource.h>
28 #include <sys/socket.h>
31 #include <sys/types.h>
36 #include <netinet/in.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"
62 #define SOCKS_BACKLOG 5
63 #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
66 volatile int client_cnt
;
68 static struct timeval timeout
= { TIMEOUT_DEFAULT
, 0 };
70 static void sockets_sighdlr(int, short, void *);
71 static void sockets_run(struct privsep
*, struct privsep_proc
*, void *);
72 static void sockets_launch(void);
73 static void sockets_purge(struct gotwebd
*);
74 static void sockets_accept_paused(int, short, void *);
75 static void sockets_rlimit(int);
77 static int sockets_dispatch_gotwebd(int, struct privsep_proc
*,
79 static int sockets_unix_socket_listen(struct privsep
*, struct socket
*);
80 static int sockets_create_socket(struct address
*);
81 static int sockets_accept_reserve(int, struct sockaddr
*, socklen_t
*,
84 static struct socket
*sockets_conf_new_socket_unix(struct gotwebd
*,
85 struct server
*, int);
86 static struct socket
*sockets_conf_new_socket_fcgi(struct gotwebd
*,
87 struct server
*, int, struct address
*);
91 static struct privsep_proc procs
[] = {
92 { "gotwebd", PROC_GOTWEBD
, sockets_dispatch_gotwebd
},
96 sockets(struct privsep
*ps
, struct privsep_proc
*p
)
98 proc_run(ps
, p
, procs
, nitems(procs
), sockets_run
, NULL
);
102 sockets_run(struct privsep
*ps
, struct privsep_proc
*p
, void *arg
)
104 if (config_init(ps
->ps_env
) == -1)
105 fatal("failed to initialize configuration");
107 p
->p_shutdown
= sockets_shutdown
;
111 signal_del(&ps
->ps_evsigchld
);
112 signal_set(&ps
->ps_evsigchld
, SIGCHLD
, sockets_sighdlr
, ps
);
113 signal_add(&ps
->ps_evsigchld
, NULL
);
116 if (pledge("stdio rpath inet recvfd proc exec sendfd unveil",
123 sockets_parse_sockets(struct gotwebd
*env
)
127 struct socket
*new_sock
= NULL
;
130 TAILQ_FOREACH(srv
, &env
->servers
, entry
) {
131 if (srv
->unix_socket
) {
132 new_sock
= sockets_conf_new_socket_unix(env
, srv
,
136 TAILQ_INSERT_TAIL(&env
->sockets
, new_sock
,
141 if (srv
->fcgi_socket
) {
142 if (TAILQ_EMPTY(&srv
->al
)) {
143 fatalx("%s: server %s has no IP addresses to "
144 "listen for FCGI connections", __func__
,
147 TAILQ_FOREACH(a
, &srv
->al
, entry
) {
148 if (a
->ss
.ss_family
!= AF_INET
&&
149 a
->ss
.ss_family
!= AF_INET6
)
151 new_sock
= sockets_conf_new_socket_fcgi(env
,
155 TAILQ_INSERT_TAIL(&env
->sockets
,
163 static struct socket
*
164 sockets_conf_new_socket_unix(struct gotwebd
*env
, struct server
*srv
, int id
)
169 if ((sock
= calloc(1, sizeof(*sock
))) == NULL
)
170 fatalx("%s: calloc", __func__
);
174 sock
->conf
.af_type
= AF_UNIX
;
176 if (strlcpy(sock
->conf
.unix_socket_name
,
177 srv
->unix_socket_name
,
178 sizeof(sock
->conf
.unix_socket_name
)) >=
179 sizeof(sock
->conf
.unix_socket_name
)) {
181 fatalx("%s: strlcpy", __func__
);
184 n
= snprintf(sock
->conf
.name
, GOTWEBD_MAXTEXT
, "%s_parent",
186 if (n
< 0 || (size_t)n
>= GOTWEBD_MAXTEXT
) {
188 fatalx("%s: snprintf", __func__
);
191 if (strlcpy(sock
->conf
.srv_name
, srv
->name
,
192 sizeof(sock
->conf
.srv_name
)) >= sizeof(sock
->conf
.srv_name
)) {
194 fatalx("%s: strlcpy", __func__
);
200 static struct socket
*
201 sockets_conf_new_socket_fcgi(struct gotwebd
*env
, struct server
*srv
, int id
,
208 if ((sock
= calloc(1, sizeof(*sock
))) == NULL
)
209 fatalx("%s: calloc", __func__
);
213 sock
->conf
.af_type
= a
->ss
.ss_family
;
215 sock
->conf
.fcgi_socket_port
= a
->port
;
217 n
= snprintf(sock
->conf
.name
, GOTWEBD_MAXTEXT
, "%s_parent",
219 if (n
< 0 || (size_t)n
>= GOTWEBD_MAXTEXT
) {
221 fatalx("%s: snprintf", __func__
);
224 if (strlcpy(sock
->conf
.srv_name
, srv
->name
,
225 sizeof(sock
->conf
.srv_name
)) >= sizeof(sock
->conf
.srv_name
)) {
227 fatalx("%s: strlcpy", __func__
);
230 acp
= &sock
->conf
.addr
;
232 memcpy(&acp
->ss
, &a
->ss
, sizeof(acp
->ss
));
234 acp
->ai_family
= a
->ai_family
;
235 acp
->ai_socktype
= a
->ai_socktype
;
236 acp
->ai_protocol
= a
->ai_protocol
;
238 if (*a
->ifname
!= '\0') {
239 if (strlcpy(acp
->ifname
, a
->ifname
,
240 sizeof(acp
->ifname
)) >= sizeof(acp
->ifname
)) {
241 fatalx("%s: interface name truncated",
254 const struct got_error
*error
;
256 TAILQ_FOREACH(sock
, &gotwebd_env
->sockets
, entry
) {
257 log_debug("%s: configuring socket %d (%d)", __func__
,
258 sock
->conf
.id
, sock
->fd
);
260 event_set(&sock
->ev
, sock
->fd
, EV_READ
| EV_PERSIST
,
261 sockets_socket_accept
, sock
);
263 if (event_add(&sock
->ev
, NULL
))
264 fatalx("event add sock");
266 evtimer_set(&sock
->pause
, sockets_accept_paused
, sock
);
268 log_debug("%s: running socket listener %d", __func__
,
272 TAILQ_FOREACH(srv
, &gotwebd_env
->servers
, entry
) {
273 if (unveil(srv
->repos_path
, "r") == -1)
274 fatal("unveil %s", srv
->repos_path
);
277 error
= got_privsep_unveil_exec_helpers();
279 fatal("%s", error
->msg
);
281 if (unveil(NULL
, NULL
) == -1)
286 sockets_purge(struct gotwebd
*env
)
288 struct socket
*sock
, *tsock
;
290 /* shutdown and remove sockets */
291 TAILQ_FOREACH_SAFE(sock
, &env
->sockets
, entry
, tsock
) {
292 if (event_initialized(&sock
->ev
))
293 event_del(&sock
->ev
);
294 if (evtimer_initialized(&sock
->evt
))
295 evtimer_del(&sock
->evt
);
296 if (evtimer_initialized(&sock
->pause
))
297 evtimer_del(&sock
->pause
);
300 TAILQ_REMOVE(&env
->sockets
, sock
, entry
);
305 sockets_dispatch_gotwebd(int fd
, struct privsep_proc
*p
, struct imsg
*imsg
)
307 struct privsep
*ps
= p
->p_ps
;
308 int res
= 0, cmd
= 0, verbose
;
310 switch (imsg
->hdr
.type
) {
312 config_getserver(gotwebd_env
, imsg
);
315 config_getsock(gotwebd_env
, imsg
);
318 config_getfd(gotwebd_env
, imsg
);
321 config_getcfg(gotwebd_env
, imsg
);
326 case IMSG_CTL_VERBOSE
:
327 IMSG_SIZE_CHECK(imsg
, &verbose
);
328 memcpy(&verbose
, imsg
->data
, sizeof(verbose
));
329 log_setverbose(verbose
);
339 if (proc_compose_imsg(ps
, PROC_GOTWEBD
, -1, cmd
,
340 imsg
->hdr
.peerid
, -1, &res
, sizeof(res
)) == -1)
349 sockets_sighdlr(int sig
, short event
, void *arg
)
353 log_info("%s: ignoring SIGHUP", __func__
);
356 log_info("%s: ignoring SIGPIPE", __func__
);
359 log_info("%s: ignoring SIGUSR1", __func__
);
364 log_info("SIGNAL: %d", sig
);
365 fatalx("unexpected signal");
370 sockets_shutdown(void)
372 struct server
*srv
, *tsrv
;
373 struct socket
*sock
, *tsock
;
376 sockets_purge(gotwebd_env
);
379 TAILQ_FOREACH_SAFE(sock
, &gotwebd_env
->sockets
, entry
, tsock
) {
380 TAILQ_REMOVE(&gotwebd_env
->sockets
, sock
, entry
);
386 TAILQ_FOREACH_SAFE(srv
, &gotwebd_env
->servers
, entry
, tsrv
) {
387 for (i
= 0; i
< srv
->ncached_repos
; i
++)
388 got_repo_close(srv
->cached_repos
[i
].repo
);
396 sockets_privinit(struct gotwebd
*env
, struct socket
*sock
)
398 struct privsep
*ps
= env
->gotwebd_ps
;
400 if (sock
->conf
.af_type
== AF_UNIX
) {
401 log_debug("%s: initializing unix socket %s", __func__
,
402 sock
->conf
.unix_socket_name
);
403 sock
->fd
= sockets_unix_socket_listen(ps
, sock
);
404 if (sock
->fd
== -1) {
405 log_warnx("%s: create unix socket failed", __func__
);
410 if (sock
->conf
.af_type
== AF_INET
|| sock
->conf
.af_type
== AF_INET6
) {
411 log_debug("%s: initializing %s FCGI socket on port %d for %s",
412 __func__
, sock
->conf
.af_type
== AF_INET
? "inet" : "inet6",
413 sock
->conf
.fcgi_socket_port
, sock
->conf
.name
);
414 sock
->fd
= sockets_create_socket(&sock
->conf
.addr
);
415 if (sock
->fd
== -1) {
416 log_warnx("%s: create FCGI socket failed", __func__
);
425 sockets_unix_socket_listen(struct privsep
*ps
, struct socket
*sock
)
427 struct gotwebd
*env
= ps
->ps_env
;
428 struct sockaddr_un sun
;
429 struct socket
*tsock
;
431 mode_t old_umask
, mode
;
433 TAILQ_FOREACH(tsock
, &env
->sockets
, entry
) {
434 if (strcmp(tsock
->conf
.unix_socket_name
,
435 sock
->conf
.unix_socket_name
) == 0 &&
440 /* TA: FIXME: this needs upstreaming. */
441 int socket_flags
= SOCK_STREAM
| SOCK_NONBLOCK
;
443 socket_flags
|= SOCK_CLOEXEC
;
445 u_fd
= socket(AF_UNIX
, socket_flags
, 0);
447 log_warn("%s: socket", __func__
);
451 sun
.sun_family
= AF_UNIX
;
452 if (strlcpy(sun
.sun_path
, sock
->conf
.unix_socket_name
,
453 sizeof(sun
.sun_path
)) >= sizeof(sun
.sun_path
)) {
454 log_warn("%s: %s name too long", __func__
,
455 sock
->conf
.unix_socket_name
);
460 if (unlink(sock
->conf
.unix_socket_name
) == -1) {
461 if (errno
!= ENOENT
) {
462 log_warn("%s: unlink %s", __func__
,
463 sock
->conf
.unix_socket_name
);
469 old_umask
= umask(S_IXUSR
|S_IXGRP
|S_IWOTH
|S_IROTH
|S_IXOTH
);
470 mode
= S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
;
472 if (bind(u_fd
, (struct sockaddr
*)&sun
, sizeof(sun
)) == -1) {
473 log_warn("%s: bind: %s", __func__
, sock
->conf
.unix_socket_name
);
475 (void)umask(old_umask
);
479 (void)umask(old_umask
);
481 if (chmod(sock
->conf
.unix_socket_name
, mode
) == -1) {
482 log_warn("%s: chmod", __func__
);
484 (void)unlink(sock
->conf
.unix_socket_name
);
488 if (chown(sock
->conf
.unix_socket_name
, ps
->ps_pw
->pw_uid
,
489 ps
->ps_pw
->pw_gid
) == -1) {
490 log_warn("%s: chown", __func__
);
492 (void)unlink(sock
->conf
.unix_socket_name
);
496 if (listen(u_fd
, SOCKS_BACKLOG
) == -1) {
497 log_warn("%s: listen", __func__
);
505 sockets_create_socket(struct address
*a
)
507 int fd
= -1, o_val
= 1, flags
;
509 fd
= socket(a
->ai_family
, a
->ai_socktype
, a
->ai_protocol
);
513 log_debug("%s: opened socket (%d) for %s", __func__
,
516 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEPORT
, &o_val
,
517 sizeof(int)) == -1) {
518 log_warn("%s: setsockopt error", __func__
);
524 flags
= fcntl(fd
, F_GETFL
);
526 if (fcntl(fd
, F_SETFL
, flags
) == -1) {
527 log_info("%s: could not enable non-blocking I/O", __func__
);
532 if (bind(fd
, (struct sockaddr
*)&a
->ss
, a
->slen
) == -1) {
534 log_info("%s: can't bind to port %d", __func__
, a
->port
);
538 if (listen(fd
, SOMAXCONN
) == -1) {
539 log_warn("%s, unable to listen on socket", __func__
);
548 sockets_accept_reserve(int sockfd
, struct sockaddr
*addr
, socklen_t
*addrlen
,
549 int reserve
, volatile int *counter
)
553 if (getdtablecount() + reserve
+
554 ((*counter
+ 1) * FD_NEEDED
) >= getdtablesize()) {
555 log_debug("inflight fds exceeded");
559 /* TA: This needs fixing upstream. */
561 ret
= accept(sockfd
, addr
, addrlen
);
563 ret
= accept4(sockfd
, addr
, addrlen
, SOCK_NONBLOCK
| SOCK_CLOEXEC
);
568 log_debug("inflight incremented, now %d", *counter
);
575 sockets_accept_paused(int fd
, short events
, void *arg
)
577 struct socket
*sock
= (struct socket
*)arg
;
579 event_add(&sock
->ev
, NULL
);
583 sockets_socket_accept(int fd
, short event
, void *arg
)
585 struct socket
*sock
= (struct socket
*)arg
;
586 struct sockaddr_storage ss
;
587 struct timeval backoff
;
588 struct request
*c
= NULL
;
595 event_add(&sock
->ev
, NULL
);
596 if (event
& EV_TIMEOUT
)
601 s
= sockets_accept_reserve(fd
, (struct sockaddr
*)&ss
, &len
,
602 FD_RESERVE
, &cgi_inflight
);
612 event_del(&sock
->ev
);
613 evtimer_add(&sock
->pause
, &backoff
);
616 log_warn("%s: accept", __func__
);
620 if (client_cnt
> GOTWEBD_MAXCLIENTS
)
623 c
= calloc(1, sizeof(struct request
));
625 log_warn("%s", __func__
);
631 c
->tp
= template(c
, &fcgi_write
, c
->outbuf
, sizeof(c
->outbuf
));
633 log_warn("%s", __func__
);
642 memcpy(c
->priv_fd
, sock
->priv_fd
, sizeof(c
->priv_fd
));
645 c
->request_started
= 0;
646 c
->sock
->client_status
= CLIENT_CONNECT
;
648 event_set(&c
->ev
, s
, EV_READ
|EV_PERSIST
, fcgi_request
, c
);
649 event_add(&c
->ev
, NULL
);
651 evtimer_set(&c
->tmo
, fcgi_timeout
, c
);
652 evtimer_add(&c
->tmo
, &timeout
);
665 sockets_rlimit(int maxfd
)
669 if (getrlimit(RLIMIT_NOFILE
, &rl
) == -1)
670 fatal("%s: failed to get resource limit", __func__
);
671 log_debug("%s: max open files %llu", __func__
,
672 (unsigned long long)rl
.rlim_max
);
675 * Allow the maximum number of open file descriptors for this
676 * login class (which should be the class "daemon" by default).
679 rl
.rlim_cur
= rl
.rlim_max
;
681 rl
.rlim_cur
= MAXIMUM(rl
.rlim_max
, (rlim_t
)maxfd
);
682 if (setrlimit(RLIMIT_NOFILE
, &rl
) == -1)
683 fatal("%s: failed to set resource limit", __func__
);