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 proc_compose(ps, PROC_SOCKS, IMSG_CFG_SRV, &ssrv, sizeof(ssrv));
90 config_getserver(struct gotwebd *env, struct imsg *imsg)
93 uint8_t *p = imsg->data;
95 IMSG_SIZE_CHECK(imsg, &srv);
97 srv = calloc(1, sizeof(*srv));
99 fatalx("%s: calloc", __func__);
100 memcpy(srv, p, sizeof(*srv));
102 if (IMSG_DATA_SIZE(imsg) != sizeof(*srv)) {
103 log_debug("%s: imsg size error", __func__);
108 /* log server info */
109 log_debug("%s: server=%s fcgi_socket=%s unix_socket=%s", __func__,
110 srv->name, srv->fcgi_socket ? "yes" : "no", srv->unix_socket ?
113 TAILQ_INSERT_TAIL(&env->servers, srv, entry);
119 config_setsock(struct gotwebd *env, struct socket *sock)
121 struct privsep *ps = env->gotwebd_ps;
122 struct socket_conf s;
129 /* open listening sockets */
130 if (sockets_privinit(env, sock) == -1)
133 for (id = 0; id < PROC_MAX; id++) {
134 what = ps->ps_what[id];
136 if ((what & CONFIG_SOCKS) == 0 || id == privsep_process)
139 memcpy(&s, &sock->conf, sizeof(s));
142 iov[c].iov_base = &s;
143 iov[c++].iov_len = sizeof(s);
145 if (id == PROC_SOCKS) {
146 /* XXX imsg code will close the fd after 1st call */
148 proc_range(ps, id, &n, &m);
149 for (n = 0; n < m; n++) {
152 else if ((fd = dup(sock->fd)) == -1)
154 if (proc_composev_imsg(ps, id, n, IMSG_CFG_SOCK,
155 -1, fd, iov, c) != 0) {
156 log_warn("%s: failed to compose "
157 "IMSG_CFG_SOCK imsg",
161 if (proc_flush_imsg(ps, id, n) == -1) {
162 log_warn("%s: failed to flush "
163 "IMSG_CFG_SOCK imsg",
171 /* Close socket early to prevent fd exhaustion in gotwebd. */
172 if (sock->fd != -1) {
181 config_getsock(struct gotwebd *env, struct imsg *imsg)
183 struct socket *sock = NULL;
184 struct socket_conf sock_conf;
185 uint8_t *p = imsg->data;
188 IMSG_SIZE_CHECK(imsg, &sock_conf);
189 memcpy(&sock_conf, p, sizeof(sock_conf));
191 if (IMSG_DATA_SIZE(imsg) != sizeof(sock_conf)) {
192 log_debug("%s: imsg size error", __func__);
196 /* create a new socket */
197 if ((sock = calloc(1, sizeof(*sock))) == NULL) {
203 memcpy(&sock->conf, &sock_conf, sizeof(sock->conf));
206 TAILQ_INSERT_TAIL(&env->sockets, sock, entry);
208 for (i = 0; i < PRIV_FDS__MAX; i++)
209 sock->priv_fd[i] = -1;
211 for (i = 0; i < GOTWEB_PACK_NUM_TEMPFILES; i++)
212 sock->pack_fds[i] = -1;
214 /* log new socket info */
215 log_debug("%s: name=%s id=%d server=%s af_type=%s socket_path=%s",
216 __func__, sock->conf.name, sock->conf.id, sock->conf.srv_name,
217 sock->conf.af_type == AF_UNIX ? "unix" :
218 (sock->conf.af_type == AF_INET ? "inet" :
219 (sock->conf.af_type == AF_INET6 ? "inet6" : "unknown")),
220 strlen(sock->conf.unix_socket_name) ?
221 sock->conf.unix_socket_name : "none");
227 config_setfd(struct gotwebd *env, struct socket *sock)
229 struct privsep *ps = env->gotwebd_ps;
231 int fd = -1, n, m, j;
236 log_debug("%s: Allocating %d file descriptors",
237 __func__, PRIV_FDS__MAX + GOTWEB_PACK_NUM_TEMPFILES);
239 for (j = 0; j < PRIV_FDS__MAX + GOTWEB_PACK_NUM_TEMPFILES; j++) {
240 for (id = 0; id < PROC_MAX; id++) {
241 what = ps->ps_what[id];
243 if ((what & CONFIG_SOCKS) == 0 || id == privsep_process)
248 iov[c].iov_base = &s;
249 iov[c++].iov_len = sizeof(s);
251 if (id == PROC_SOCKS) {
253 * XXX imsg code will close the fd
257 proc_range(ps, id, &n, &m);
258 for (n = 0; n < m; n++) {
259 fd = got_opentempfd();
262 if (proc_composev_imsg(ps, id, n,
263 IMSG_CFG_FD, -1, fd, iov, c) != 0) {
264 log_warn("%s: failed to compose "
269 if (proc_flush_imsg(ps, id, n) == -1) {
270 log_warn("%s: failed to flush "
279 /* Close fd early to prevent fd exhaustion in gotwebd. */
287 config_getfd(struct gotwebd *env, struct imsg *imsg)
290 uint8_t *p = imsg->data;
291 int sock_id, match = 0, i;
293 IMSG_SIZE_CHECK(imsg, &sock_id);
294 memcpy(&sock_id, p, sizeof(sock_id));
296 TAILQ_FOREACH(sock, &env->sockets, entry) {
297 const int nfds = (GOTWEB_PACK_NUM_TEMPFILES + PRIV_FDS__MAX);
298 for (i = 0; i < nfds; i++) {
299 if (i < PRIV_FDS__MAX && sock->priv_fd[i] == -1) {
300 log_debug("%s: assigning socket %d priv_fd %d",
301 __func__, sock_id, imsg->fd);
302 sock->priv_fd[i] = imsg->fd;
306 if (sock->pack_fds[i - PRIV_FDS__MAX] == -1) {
307 log_debug("%s: assigning socket %d pack_fd %d",
308 __func__, sock_id, imsg->fd);
309 sock->pack_fds[i - PRIV_FDS__MAX] = imsg->fd;