2 * Copyright (c) 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <sys/socket.h>
25 #include <netinet/in.h>
39 #include "got_opentemp.h"
45 config_init(struct gotwebd *env)
47 struct privsep *ps = env->gotwebd_ps;
50 /* Global configuration. */
51 if (privsep_process == PROC_GOTWEBD)
52 env->prefork_gotwebd = GOTWEBD_NUMPROC;
54 ps->ps_what[PROC_GOTWEBD] = CONFIG_ALL;
55 ps->ps_what[PROC_SOCKS] = CONFIG_SOCKS;
57 /* Other configuration. */
58 what = ps->ps_what[privsep_process];
59 if (what & CONFIG_SOCKS) {
61 TAILQ_INIT(&env->servers);
62 TAILQ_INIT(&env->sockets);
68 config_getcfg(struct gotwebd *env, struct imsg *imsg)
70 /* nothing to do but tell gotwebd configuration is done */
71 if (privsep_process != PROC_GOTWEBD)
72 proc_compose(env->gotwebd_ps, PROC_GOTWEBD,
73 IMSG_CFG_DONE, NULL, 0);
79 config_setserver(struct gotwebd *env, struct server *srv)
82 struct privsep *ps = env->gotwebd_ps;
84 memcpy(&ssrv, srv, sizeof(ssrv));
85 if (proc_compose(ps, PROC_SOCKS, IMSG_CFG_SRV, &ssrv, sizeof(ssrv))
87 fatal("proc_compose");
92 config_getserver(struct gotwebd *env, struct imsg *imsg)
95 uint8_t *p = imsg->data;
97 IMSG_SIZE_CHECK(imsg, &srv);
99 srv = calloc(1, sizeof(*srv));
101 fatalx("%s: calloc", __func__);
103 if (IMSG_DATA_SIZE(imsg) != sizeof(*srv)) {
104 log_debug("%s: imsg size error", __func__);
109 memcpy(srv, p, sizeof(*srv));
110 srv->cached_repos = calloc(GOTWEBD_REPO_CACHESIZE,
111 sizeof(*srv->cached_repos));
112 if (srv->cached_repos == NULL)
113 fatal("%s: calloc", __func__);
114 srv->ncached_repos = 0;
116 /* log server info */
117 log_debug("%s: server=%s fcgi_socket=%s unix_socket=%s", __func__,
118 srv->name, srv->fcgi_socket ? "yes" : "no", srv->unix_socket ?
121 TAILQ_INSERT_TAIL(&env->servers, srv, entry);
127 config_setsock(struct gotwebd *env, struct socket *sock)
129 struct privsep *ps = env->gotwebd_ps;
130 struct socket_conf s;
137 /* open listening sockets */
138 if (sockets_privinit(env, sock) == -1)
141 for (id = 0; id < PROC_MAX; id++) {
142 what = ps->ps_what[id];
144 if ((what & CONFIG_SOCKS) == 0 || id == privsep_process)
147 memcpy(&s, &sock->conf, sizeof(s));
150 iov[c].iov_base = &s;
151 iov[c++].iov_len = sizeof(s);
153 if (id == PROC_SOCKS) {
154 /* XXX imsg code will close the fd after 1st call */
156 proc_range(ps, id, &n, &m);
157 for (n = 0; n < m; n++) {
160 else if ((fd = dup(sock->fd)) == -1)
162 if (proc_composev_imsg(ps, id, n, IMSG_CFG_SOCK,
163 -1, fd, iov, c) != 0) {
164 log_warn("%s: failed to compose "
165 "IMSG_CFG_SOCK imsg",
169 if (proc_flush_imsg(ps, id, n) == -1) {
170 log_warn("%s: failed to flush "
171 "IMSG_CFG_SOCK imsg",
179 /* Close socket early to prevent fd exhaustion in gotwebd. */
180 if (sock->fd != -1) {
189 config_getsock(struct gotwebd *env, struct imsg *imsg)
191 struct socket *sock = NULL;
192 struct socket_conf sock_conf;
193 uint8_t *p = imsg->data;
196 IMSG_SIZE_CHECK(imsg, &sock_conf);
197 memcpy(&sock_conf, p, sizeof(sock_conf));
199 if (IMSG_DATA_SIZE(imsg) != sizeof(sock_conf)) {
200 log_debug("%s: imsg size error", __func__);
204 /* create a new socket */
205 if ((sock = calloc(1, sizeof(*sock))) == NULL) {
211 memcpy(&sock->conf, &sock_conf, sizeof(sock->conf));
214 TAILQ_INSERT_TAIL(&env->sockets, sock, entry);
216 for (i = 0; i < PRIV_FDS__MAX; i++)
217 sock->priv_fd[i] = -1;
219 for (i = 0; i < GOTWEB_PACK_NUM_TEMPFILES; i++)
220 sock->pack_fds[i] = -1;
222 /* log new socket info */
223 log_debug("%s: name=%s id=%d server=%s af_type=%s socket_path=%s",
224 __func__, sock->conf.name, sock->conf.id, sock->conf.srv_name,
225 sock->conf.af_type == AF_UNIX ? "unix" :
226 (sock->conf.af_type == AF_INET ? "inet" :
227 (sock->conf.af_type == AF_INET6 ? "inet6" : "unknown")),
228 strlen(sock->conf.unix_socket_name) ?
229 sock->conf.unix_socket_name : "none");
235 config_setfd(struct gotwebd *env, struct socket *sock)
237 struct privsep *ps = env->gotwebd_ps;
239 int fd = -1, n, m, j;
244 log_debug("%s: Allocating %d file descriptors",
245 __func__, PRIV_FDS__MAX + GOTWEB_PACK_NUM_TEMPFILES);
247 for (j = 0; j < PRIV_FDS__MAX + GOTWEB_PACK_NUM_TEMPFILES; j++) {
248 for (id = 0; id < PROC_MAX; id++) {
249 what = ps->ps_what[id];
251 if ((what & CONFIG_SOCKS) == 0 || id == privsep_process)
256 iov[c].iov_base = &s;
257 iov[c++].iov_len = sizeof(s);
259 if (id == PROC_SOCKS) {
261 * XXX imsg code will close the fd
265 proc_range(ps, id, &n, &m);
266 for (n = 0; n < m; n++) {
267 fd = got_opentempfd();
270 if (proc_composev_imsg(ps, id, n,
271 IMSG_CFG_FD, -1, fd, iov, c) != 0) {
272 log_warn("%s: failed to compose "
277 if (proc_flush_imsg(ps, id, n) == -1) {
278 log_warn("%s: failed to flush "
287 /* Close fd early to prevent fd exhaustion in gotwebd. */
295 config_getfd(struct gotwebd *env, struct imsg *imsg)
298 uint8_t *p = imsg->data;
299 int sock_id, match = 0, i;
301 IMSG_SIZE_CHECK(imsg, &sock_id);
302 memcpy(&sock_id, p, sizeof(sock_id));
304 TAILQ_FOREACH(sock, &env->sockets, entry) {
305 const int nfds = (GOTWEB_PACK_NUM_TEMPFILES + PRIV_FDS__MAX);
306 for (i = 0; i < nfds; i++) {
307 if (i < PRIV_FDS__MAX && sock->priv_fd[i] == -1) {
308 log_debug("%s: assigning socket %d priv_fd %d",
309 __func__, sock_id, imsg->fd);
310 sock->priv_fd[i] = imsg->fd;
314 if (sock->pack_fds[i - PRIV_FDS__MAX] == -1) {
315 log_debug("%s: assigning socket %d pack_fd %d",
316 __func__, sock_id, imsg->fd);
317 sock->pack_fds[i - PRIV_FDS__MAX] = imsg->fd;