2 8a35f56c 2022-07-16 thomas * Copyright (c) 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 8a35f56c 2022-07-16 thomas * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
5 8a35f56c 2022-07-16 thomas * Permission to use, copy, modify, and distribute this software for any
6 8a35f56c 2022-07-16 thomas * purpose with or without fee is hereby granted, provided that the above
7 8a35f56c 2022-07-16 thomas * copyright notice and this permission notice appear in all copies.
9 8a35f56c 2022-07-16 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 8a35f56c 2022-07-16 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 8a35f56c 2022-07-16 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 8a35f56c 2022-07-16 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 8a35f56c 2022-07-16 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 8a35f56c 2022-07-16 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 8a35f56c 2022-07-16 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 8a35f56c 2022-07-16 thomas #include <sys/types.h>
19 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
20 8a35f56c 2022-07-16 thomas #include <sys/time.h>
21 8a35f56c 2022-07-16 thomas #include <sys/uio.h>
22 8a35f56c 2022-07-16 thomas #include <sys/socket.h>
24 8a35f56c 2022-07-16 thomas #include <net/if.h>
25 8a35f56c 2022-07-16 thomas #include <netinet/in.h>
27 8a35f56c 2022-07-16 thomas #include <stdio.h>
28 8a35f56c 2022-07-16 thomas #include <stdlib.h>
29 8a35f56c 2022-07-16 thomas #include <termios.h>
30 8a35f56c 2022-07-16 thomas #include <unistd.h>
31 8a35f56c 2022-07-16 thomas #include <limits.h>
32 8a35f56c 2022-07-16 thomas #include <string.h>
33 8a35f56c 2022-07-16 thomas #include <event.h>
34 8a35f56c 2022-07-16 thomas #include <fcntl.h>
35 8a35f56c 2022-07-16 thomas #include <errno.h>
37 8a35f56c 2022-07-16 thomas #include "got_opentemp.h"
39 ff36aeea 2022-07-16 thomas #include "got_compat.h"
41 8a35f56c 2022-07-16 thomas #include "proc.h"
42 8a35f56c 2022-07-16 thomas #include "gotwebd.h"
45 8a35f56c 2022-07-16 thomas config_init(struct gotwebd *env)
47 8a35f56c 2022-07-16 thomas struct privsep *ps = env->gotwebd_ps;
48 8a35f56c 2022-07-16 thomas unsigned int what;
50 8a35f56c 2022-07-16 thomas /* Global configuration. */
51 8a35f56c 2022-07-16 thomas if (privsep_process == PROC_GOTWEBD)
52 8a35f56c 2022-07-16 thomas env->prefork_gotwebd = GOTWEBD_NUMPROC;
54 8a35f56c 2022-07-16 thomas ps->ps_what[PROC_GOTWEBD] = CONFIG_ALL;
55 8a35f56c 2022-07-16 thomas ps->ps_what[PROC_SOCKS] = CONFIG_SOCKS;
57 8a35f56c 2022-07-16 thomas /* Other configuration. */
58 8a35f56c 2022-07-16 thomas what = ps->ps_what[privsep_process];
59 8a35f56c 2022-07-16 thomas if (what & CONFIG_SOCKS) {
60 8a35f56c 2022-07-16 thomas env->server_cnt = 0;
61 8a35f56c 2022-07-16 thomas env->servers = calloc(1, sizeof(*env->servers));
62 8a35f56c 2022-07-16 thomas if (env->servers == NULL)
63 8a35f56c 2022-07-16 thomas fatalx("%s: calloc", __func__);
64 8a35f56c 2022-07-16 thomas env->sockets = calloc(1, sizeof(*env->sockets));
65 8a35f56c 2022-07-16 thomas if (env->sockets == NULL)
66 8a35f56c 2022-07-16 thomas fatalx("%s: calloc", __func__);
67 8a35f56c 2022-07-16 thomas TAILQ_INIT(env->servers);
68 8a35f56c 2022-07-16 thomas TAILQ_INIT(env->sockets);
74 8a35f56c 2022-07-16 thomas config_getcfg(struct gotwebd *env, struct imsg *imsg)
76 8a35f56c 2022-07-16 thomas /* nothing to do but tell gotwebd configuration is done */
77 8a35f56c 2022-07-16 thomas if (privsep_process != PROC_GOTWEBD)
78 8a35f56c 2022-07-16 thomas proc_compose(env->gotwebd_ps, PROC_GOTWEBD,
79 8a35f56c 2022-07-16 thomas IMSG_CFG_DONE, NULL, 0);
85 8a35f56c 2022-07-16 thomas config_setserver(struct gotwebd *env, struct server *srv)
87 8a35f56c 2022-07-16 thomas struct server ssrv;
88 8a35f56c 2022-07-16 thomas struct privsep *ps = env->gotwebd_ps;
90 8a35f56c 2022-07-16 thomas memcpy(&ssrv, srv, sizeof(ssrv));
91 8a35f56c 2022-07-16 thomas proc_compose(ps, PROC_SOCKS, IMSG_CFG_SRV, &ssrv, sizeof(ssrv));
96 8a35f56c 2022-07-16 thomas config_getserver(struct gotwebd *env, struct imsg *imsg)
98 8a35f56c 2022-07-16 thomas struct server *srv;
99 8a35f56c 2022-07-16 thomas uint8_t *p = imsg->data;
101 8a35f56c 2022-07-16 thomas IMSG_SIZE_CHECK(imsg, &srv);
103 8a35f56c 2022-07-16 thomas srv = calloc(1, sizeof(*srv));
104 8a35f56c 2022-07-16 thomas if (srv == NULL)
105 8a35f56c 2022-07-16 thomas fatalx("%s: calloc", __func__);
106 8a35f56c 2022-07-16 thomas memcpy(srv, p, sizeof(*srv));
108 8a35f56c 2022-07-16 thomas if (IMSG_DATA_SIZE(imsg) != sizeof(*srv)) {
109 8a35f56c 2022-07-16 thomas log_debug("%s: imsg size error", __func__);
110 8a35f56c 2022-07-16 thomas free(srv);
111 8a35f56c 2022-07-16 thomas return 1;
114 8a35f56c 2022-07-16 thomas /* log server info */
115 8a35f56c 2022-07-16 thomas log_debug("%s: server=%s fcgi_socket=%s unix_socket=%s", __func__,
116 8a35f56c 2022-07-16 thomas srv->name, srv->fcgi_socket ? "yes" : "no", srv->unix_socket ?
117 8a35f56c 2022-07-16 thomas "yes" : "no");
119 8a35f56c 2022-07-16 thomas TAILQ_INSERT_TAIL(env->servers, srv, entry);
121 8a35f56c 2022-07-16 thomas return 0;
125 8a35f56c 2022-07-16 thomas config_setsock(struct gotwebd *env, struct socket *sock)
127 8a35f56c 2022-07-16 thomas struct privsep *ps = env->gotwebd_ps;
128 8a35f56c 2022-07-16 thomas struct socket_conf s;
130 8a35f56c 2022-07-16 thomas int fd = -1, n, m;
131 8a35f56c 2022-07-16 thomas struct iovec iov[6];
132 8a35f56c 2022-07-16 thomas size_t c;
133 8a35f56c 2022-07-16 thomas unsigned int what;
135 8a35f56c 2022-07-16 thomas /* open listening sockets */
136 8a35f56c 2022-07-16 thomas if (sockets_privinit(env, sock) == -1)
137 8a35f56c 2022-07-16 thomas return -1;
139 8a35f56c 2022-07-16 thomas for (id = 0; id < PROC_MAX; id++) {
140 8a35f56c 2022-07-16 thomas what = ps->ps_what[id];
142 8a35f56c 2022-07-16 thomas if ((what & CONFIG_SOCKS) == 0 || id == privsep_process)
143 8a35f56c 2022-07-16 thomas continue;
145 8a35f56c 2022-07-16 thomas memcpy(&s, &sock->conf, sizeof(s));
148 8a35f56c 2022-07-16 thomas iov[c].iov_base = &s;
149 8a35f56c 2022-07-16 thomas iov[c++].iov_len = sizeof(s);
151 8a35f56c 2022-07-16 thomas if (id == PROC_SOCKS) {
152 8a35f56c 2022-07-16 thomas /* XXX imsg code will close the fd after 1st call */
154 8a35f56c 2022-07-16 thomas proc_range(ps, id, &n, &m);
155 8a35f56c 2022-07-16 thomas for (n = 0; n < m; n++) {
156 8a35f56c 2022-07-16 thomas if (sock->fd == -1)
158 8a35f56c 2022-07-16 thomas else if ((fd = dup(sock->fd)) == -1)
159 8a35f56c 2022-07-16 thomas return 1;
160 8a35f56c 2022-07-16 thomas if (proc_composev_imsg(ps, id, n, IMSG_CFG_SOCK,
161 8a35f56c 2022-07-16 thomas -1, fd, iov, c) != 0) {
162 8a35f56c 2022-07-16 thomas log_warn("%s: failed to compose "
163 8a35f56c 2022-07-16 thomas "IMSG_CFG_SOCK imsg",
164 8a35f56c 2022-07-16 thomas __func__);
165 8a35f56c 2022-07-16 thomas return 1;
167 8a35f56c 2022-07-16 thomas if (proc_flush_imsg(ps, id, n) == -1) {
168 8a35f56c 2022-07-16 thomas log_warn("%s: failed to flush "
169 8a35f56c 2022-07-16 thomas "IMSG_CFG_SOCK imsg",
170 8a35f56c 2022-07-16 thomas __func__);
171 8a35f56c 2022-07-16 thomas return 1;
177 8a35f56c 2022-07-16 thomas /* Close socket early to prevent fd exhaustion in gotwebd. */
178 8a35f56c 2022-07-16 thomas if (sock->fd != -1) {
179 8a35f56c 2022-07-16 thomas close(sock->fd);
180 8a35f56c 2022-07-16 thomas sock->fd = -1;
183 8a35f56c 2022-07-16 thomas return 0;
187 8a35f56c 2022-07-16 thomas config_getsock(struct gotwebd *env, struct imsg *imsg)
189 8a35f56c 2022-07-16 thomas struct socket *sock = NULL;
190 8a35f56c 2022-07-16 thomas struct socket_conf sock_conf;
191 8a35f56c 2022-07-16 thomas uint8_t *p = imsg->data;
194 8a35f56c 2022-07-16 thomas IMSG_SIZE_CHECK(imsg, &sock_conf);
195 8a35f56c 2022-07-16 thomas memcpy(&sock_conf, p, sizeof(sock_conf));
197 8a35f56c 2022-07-16 thomas if (IMSG_DATA_SIZE(imsg) != sizeof(sock_conf)) {
198 8a35f56c 2022-07-16 thomas log_debug("%s: imsg size error", __func__);
199 8a35f56c 2022-07-16 thomas return 1;
202 8a35f56c 2022-07-16 thomas /* create a new socket */
203 8a35f56c 2022-07-16 thomas if ((sock = calloc(1, sizeof(*sock))) == NULL) {
204 8a35f56c 2022-07-16 thomas if (imsg->fd != -1)
205 8a35f56c 2022-07-16 thomas close(imsg->fd);
206 8a35f56c 2022-07-16 thomas return 1;
209 8a35f56c 2022-07-16 thomas memcpy(&sock->conf, &sock_conf, sizeof(sock->conf));
210 8a35f56c 2022-07-16 thomas sock->fd = imsg->fd;
212 8a35f56c 2022-07-16 thomas TAILQ_INSERT_TAIL(env->sockets, sock, entry);
214 8a35f56c 2022-07-16 thomas for (i = 0; i < PRIV_FDS__MAX; i++)
215 8a35f56c 2022-07-16 thomas sock->priv_fd[i] = -1;
217 8a35f56c 2022-07-16 thomas for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++)
218 8a35f56c 2022-07-16 thomas sock->pack_fds[i] = -1;
220 8a35f56c 2022-07-16 thomas /* log new socket info */
221 8a35f56c 2022-07-16 thomas log_debug("%s: name=%s id=%d server=%s child_id=%d parent_id=%d "
222 8a35f56c 2022-07-16 thomas "type=%s ipv4=%d ipv6=%d socket_path=%s",
223 8a35f56c 2022-07-16 thomas __func__, sock->conf.name, sock->conf.id, sock->conf.srv_name,
224 8a35f56c 2022-07-16 thomas sock->conf.child_id, sock->conf.parent_id, sock->conf.type ?
225 8a35f56c 2022-07-16 thomas "fcgi" : "unix", sock->conf.ipv4, sock->conf.ipv6,
226 8a35f56c 2022-07-16 thomas strlen(sock->conf.unix_socket_name) ?
227 8a35f56c 2022-07-16 thomas sock->conf.unix_socket_name : "none");
229 8a35f56c 2022-07-16 thomas return 0;
233 8a35f56c 2022-07-16 thomas config_setfd(struct gotwebd *env, struct socket *sock)
235 8a35f56c 2022-07-16 thomas struct privsep *ps = env->gotwebd_ps;
236 8a35f56c 2022-07-16 thomas int id, s;
237 8a35f56c 2022-07-16 thomas int fd = -1, n, m, j;
238 8a35f56c 2022-07-16 thomas struct iovec iov[6];
239 8a35f56c 2022-07-16 thomas size_t c;
240 8a35f56c 2022-07-16 thomas unsigned int what;
242 8a35f56c 2022-07-16 thomas log_debug("%s: Allocating %d file descriptors",
243 8a35f56c 2022-07-16 thomas __func__, PRIV_FDS__MAX + GOT_PACK_NUM_TEMPFILES);
245 8a35f56c 2022-07-16 thomas for (j = 0; j < PRIV_FDS__MAX + GOT_PACK_NUM_TEMPFILES; j++) {
246 8a35f56c 2022-07-16 thomas for (id = 0; id < PROC_MAX; id++) {
247 8a35f56c 2022-07-16 thomas what = ps->ps_what[id];
249 8a35f56c 2022-07-16 thomas if ((what & CONFIG_SOCKS) == 0 || id == privsep_process)
250 8a35f56c 2022-07-16 thomas continue;
252 8a35f56c 2022-07-16 thomas s = sock->conf.id;
254 8a35f56c 2022-07-16 thomas iov[c].iov_base = &s;
255 8a35f56c 2022-07-16 thomas iov[c++].iov_len = sizeof(s);
257 8a35f56c 2022-07-16 thomas if (id == PROC_SOCKS) {
259 8a35f56c 2022-07-16 thomas * XXX imsg code will close the fd
260 8a35f56c 2022-07-16 thomas * after 1st call
263 8a35f56c 2022-07-16 thomas proc_range(ps, id, &n, &m);
264 8a35f56c 2022-07-16 thomas for (n = 0; n < m; n++) {
265 8a35f56c 2022-07-16 thomas fd = got_opentempfd();
266 8a35f56c 2022-07-16 thomas if (fd == -1)
267 8a35f56c 2022-07-16 thomas return 1;
268 8a35f56c 2022-07-16 thomas if (proc_composev_imsg(ps, id, n,
269 8a35f56c 2022-07-16 thomas IMSG_CFG_FD, -1, fd, iov, c) != 0) {
270 8a35f56c 2022-07-16 thomas log_warn("%s: failed to compose "
271 8a35f56c 2022-07-16 thomas "IMSG_CFG_FD imsg",
272 8a35f56c 2022-07-16 thomas __func__);
273 8a35f56c 2022-07-16 thomas return 1;
275 8a35f56c 2022-07-16 thomas if (proc_flush_imsg(ps, id, n) == -1) {
276 8a35f56c 2022-07-16 thomas log_warn("%s: failed to flush "
277 8a35f56c 2022-07-16 thomas "IMSG_CFG_FD imsg",
278 8a35f56c 2022-07-16 thomas __func__);
279 8a35f56c 2022-07-16 thomas return 1;
285 8a35f56c 2022-07-16 thomas /* Close fd early to prevent fd exhaustion in gotwebd. */
286 8a35f56c 2022-07-16 thomas if (fd != -1)
287 8a35f56c 2022-07-16 thomas close(fd);
289 8a35f56c 2022-07-16 thomas return 0;
293 8a35f56c 2022-07-16 thomas config_getfd(struct gotwebd *env, struct imsg *imsg)
295 8a35f56c 2022-07-16 thomas struct socket *sock;
296 8a35f56c 2022-07-16 thomas uint8_t *p = imsg->data;
297 8a35f56c 2022-07-16 thomas int sock_id, match = 0, i;
299 8a35f56c 2022-07-16 thomas IMSG_SIZE_CHECK(imsg, &sock_id);
300 8a35f56c 2022-07-16 thomas memcpy(&sock_id, p, sizeof(sock_id));
302 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(sock, env->sockets, entry) {
303 8a35f56c 2022-07-16 thomas for (i = 0; i < (GOT_PACK_NUM_TEMPFILES + PRIV_FDS__MAX); i++) {
304 8a35f56c 2022-07-16 thomas if (i < PRIV_FDS__MAX && sock->priv_fd[i] == -1) {
305 8a35f56c 2022-07-16 thomas log_debug("%s: assigning socket %d priv_fd %d",
306 8a35f56c 2022-07-16 thomas __func__, sock_id, imsg->fd);
307 8a35f56c 2022-07-16 thomas sock->priv_fd[i] = imsg->fd;
308 8a35f56c 2022-07-16 thomas match = 1;
311 8a35f56c 2022-07-16 thomas if (sock->pack_fds[i - PRIV_FDS__MAX] == -1) {
312 8a35f56c 2022-07-16 thomas log_debug("%s: assigning socket %d pack_fd %d",
313 8a35f56c 2022-07-16 thomas __func__, sock_id, imsg->fd);
314 8a35f56c 2022-07-16 thomas sock->pack_fds[i - PRIV_FDS__MAX] = imsg->fd;
315 8a35f56c 2022-07-16 thomas match = 1;
321 8a35f56c 2022-07-16 thomas if (match)
322 8a35f56c 2022-07-16 thomas return 0;
324 8a35f56c 2022-07-16 thomas return 1;