2 a596b957 2022-07-14 tracey * Copyright (c) 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 a596b957 2022-07-14 tracey * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
5 a596b957 2022-07-14 tracey * Permission to use, copy, modify, and distribute this software for any
6 a596b957 2022-07-14 tracey * purpose with or without fee is hereby granted, provided that the above
7 a596b957 2022-07-14 tracey * copyright notice and this permission notice appear in all copies.
9 a596b957 2022-07-14 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 a596b957 2022-07-14 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 a596b957 2022-07-14 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 a596b957 2022-07-14 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 a596b957 2022-07-14 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 a596b957 2022-07-14 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 a596b957 2022-07-14 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 a596b957 2022-07-14 tracey #include <sys/types.h>
19 a596b957 2022-07-14 tracey #include <sys/queue.h>
20 a596b957 2022-07-14 tracey #include <sys/time.h>
21 a596b957 2022-07-14 tracey #include <sys/uio.h>
22 a596b957 2022-07-14 tracey #include <sys/socket.h>
24 a596b957 2022-07-14 tracey #include <net/if.h>
25 a596b957 2022-07-14 tracey #include <netinet/in.h>
27 a596b957 2022-07-14 tracey #include <stdio.h>
28 a596b957 2022-07-14 tracey #include <stdlib.h>
29 a596b957 2022-07-14 tracey #include <termios.h>
30 a596b957 2022-07-14 tracey #include <unistd.h>
31 a596b957 2022-07-14 tracey #include <limits.h>
32 a596b957 2022-07-14 tracey #include <string.h>
33 a596b957 2022-07-14 tracey #include <event.h>
34 a596b957 2022-07-14 tracey #include <fcntl.h>
35 a596b957 2022-07-14 tracey #include <util.h>
36 a596b957 2022-07-14 tracey #include <errno.h>
37 a596b957 2022-07-14 tracey #include <imsg.h>
39 a596b957 2022-07-14 tracey #include "got_opentemp.h"
40 df2d3cd2 2023-03-11 op #include "got_reference.h"
42 a596b957 2022-07-14 tracey #include "proc.h"
43 a596b957 2022-07-14 tracey #include "gotwebd.h"
46 a596b957 2022-07-14 tracey config_init(struct gotwebd *env)
48 a596b957 2022-07-14 tracey struct privsep *ps = env->gotwebd_ps;
49 a596b957 2022-07-14 tracey unsigned int what;
51 a596b957 2022-07-14 tracey /* Global configuration. */
52 a596b957 2022-07-14 tracey if (privsep_process == PROC_GOTWEBD)
53 a596b957 2022-07-14 tracey env->prefork_gotwebd = GOTWEBD_NUMPROC;
55 a596b957 2022-07-14 tracey ps->ps_what[PROC_GOTWEBD] = CONFIG_ALL;
56 a596b957 2022-07-14 tracey ps->ps_what[PROC_SOCKS] = CONFIG_SOCKS;
58 a596b957 2022-07-14 tracey /* Other configuration. */
59 a596b957 2022-07-14 tracey what = ps->ps_what[privsep_process];
60 a596b957 2022-07-14 tracey if (what & CONFIG_SOCKS) {
61 a596b957 2022-07-14 tracey env->server_cnt = 0;
62 2ad48e9a 2022-08-16 stsp TAILQ_INIT(&env->servers);
63 2ad48e9a 2022-08-16 stsp TAILQ_INIT(&env->sockets);
69 a596b957 2022-07-14 tracey config_getcfg(struct gotwebd *env, struct imsg *imsg)
71 a596b957 2022-07-14 tracey /* nothing to do but tell gotwebd configuration is done */
72 a596b957 2022-07-14 tracey if (privsep_process != PROC_GOTWEBD)
73 a596b957 2022-07-14 tracey proc_compose(env->gotwebd_ps, PROC_GOTWEBD,
74 a596b957 2022-07-14 tracey IMSG_CFG_DONE, NULL, 0);
80 a596b957 2022-07-14 tracey config_setserver(struct gotwebd *env, struct server *srv)
82 a596b957 2022-07-14 tracey struct server ssrv;
83 a596b957 2022-07-14 tracey struct privsep *ps = env->gotwebd_ps;
85 a596b957 2022-07-14 tracey memcpy(&ssrv, srv, sizeof(ssrv));
86 7e0ec052 2022-09-06 op if (proc_compose(ps, PROC_SOCKS, IMSG_CFG_SRV, &ssrv, sizeof(ssrv))
88 7e0ec052 2022-09-06 op fatal("proc_compose");
93 a596b957 2022-07-14 tracey config_getserver(struct gotwebd *env, struct imsg *imsg)
95 a596b957 2022-07-14 tracey struct server *srv;
96 a596b957 2022-07-14 tracey uint8_t *p = imsg->data;
98 a596b957 2022-07-14 tracey IMSG_SIZE_CHECK(imsg, &srv);
100 a596b957 2022-07-14 tracey srv = calloc(1, sizeof(*srv));
101 a596b957 2022-07-14 tracey if (srv == NULL)
102 a596b957 2022-07-14 tracey fatalx("%s: calloc", __func__);
104 a596b957 2022-07-14 tracey if (IMSG_DATA_SIZE(imsg) != sizeof(*srv)) {
105 a596b957 2022-07-14 tracey log_debug("%s: imsg size error", __func__);
106 a596b957 2022-07-14 tracey free(srv);
107 a596b957 2022-07-14 tracey return 1;
110 7e0ec052 2022-09-06 op memcpy(srv, p, sizeof(*srv));
111 7e0ec052 2022-09-06 op srv->cached_repos = calloc(GOTWEBD_REPO_CACHESIZE,
112 7e0ec052 2022-09-06 op sizeof(*srv->cached_repos));
113 7e0ec052 2022-09-06 op if (srv->cached_repos == NULL)
114 7e0ec052 2022-09-06 op fatal("%s: calloc", __func__);
115 7e0ec052 2022-09-06 op srv->ncached_repos = 0;
117 a596b957 2022-07-14 tracey /* log server info */
118 a596b957 2022-07-14 tracey log_debug("%s: server=%s fcgi_socket=%s unix_socket=%s", __func__,
119 a596b957 2022-07-14 tracey srv->name, srv->fcgi_socket ? "yes" : "no", srv->unix_socket ?
120 a596b957 2022-07-14 tracey "yes" : "no");
122 2ad48e9a 2022-08-16 stsp TAILQ_INSERT_TAIL(&env->servers, srv, entry);
124 a596b957 2022-07-14 tracey return 0;
128 a596b957 2022-07-14 tracey config_setsock(struct gotwebd *env, struct socket *sock)
130 a596b957 2022-07-14 tracey struct privsep *ps = env->gotwebd_ps;
131 a596b957 2022-07-14 tracey struct socket_conf s;
133 a596b957 2022-07-14 tracey int fd = -1, n, m;
134 a596b957 2022-07-14 tracey struct iovec iov[6];
135 a596b957 2022-07-14 tracey size_t c;
136 a596b957 2022-07-14 tracey unsigned int what;
138 a596b957 2022-07-14 tracey /* open listening sockets */
139 a596b957 2022-07-14 tracey if (sockets_privinit(env, sock) == -1)
140 a596b957 2022-07-14 tracey return -1;
142 a596b957 2022-07-14 tracey for (id = 0; id < PROC_MAX; id++) {
143 a596b957 2022-07-14 tracey what = ps->ps_what[id];
145 a596b957 2022-07-14 tracey if ((what & CONFIG_SOCKS) == 0 || id == privsep_process)
146 a596b957 2022-07-14 tracey continue;
148 a596b957 2022-07-14 tracey memcpy(&s, &sock->conf, sizeof(s));
151 a596b957 2022-07-14 tracey iov[c].iov_base = &s;
152 a596b957 2022-07-14 tracey iov[c++].iov_len = sizeof(s);
154 a596b957 2022-07-14 tracey if (id == PROC_SOCKS) {
155 a596b957 2022-07-14 tracey /* XXX imsg code will close the fd after 1st call */
157 a596b957 2022-07-14 tracey proc_range(ps, id, &n, &m);
158 a596b957 2022-07-14 tracey for (n = 0; n < m; n++) {
159 a596b957 2022-07-14 tracey if (sock->fd == -1)
161 a596b957 2022-07-14 tracey else if ((fd = dup(sock->fd)) == -1)
162 a596b957 2022-07-14 tracey return 1;
163 a596b957 2022-07-14 tracey if (proc_composev_imsg(ps, id, n, IMSG_CFG_SOCK,
164 a596b957 2022-07-14 tracey -1, fd, iov, c) != 0) {
165 a596b957 2022-07-14 tracey log_warn("%s: failed to compose "
166 a596b957 2022-07-14 tracey "IMSG_CFG_SOCK imsg",
167 a596b957 2022-07-14 tracey __func__);
168 a596b957 2022-07-14 tracey return 1;
170 a596b957 2022-07-14 tracey if (proc_flush_imsg(ps, id, n) == -1) {
171 a596b957 2022-07-14 tracey log_warn("%s: failed to flush "
172 a596b957 2022-07-14 tracey "IMSG_CFG_SOCK imsg",
173 a596b957 2022-07-14 tracey __func__);
174 a596b957 2022-07-14 tracey return 1;
180 a596b957 2022-07-14 tracey /* Close socket early to prevent fd exhaustion in gotwebd. */
181 a596b957 2022-07-14 tracey if (sock->fd != -1) {
182 a596b957 2022-07-14 tracey close(sock->fd);
183 a596b957 2022-07-14 tracey sock->fd = -1;
186 a596b957 2022-07-14 tracey return 0;
190 a596b957 2022-07-14 tracey config_getsock(struct gotwebd *env, struct imsg *imsg)
192 a596b957 2022-07-14 tracey struct socket *sock = NULL;
193 a596b957 2022-07-14 tracey struct socket_conf sock_conf;
194 a596b957 2022-07-14 tracey uint8_t *p = imsg->data;
197 a596b957 2022-07-14 tracey IMSG_SIZE_CHECK(imsg, &sock_conf);
198 a596b957 2022-07-14 tracey memcpy(&sock_conf, p, sizeof(sock_conf));
200 a596b957 2022-07-14 tracey if (IMSG_DATA_SIZE(imsg) != sizeof(sock_conf)) {
201 a596b957 2022-07-14 tracey log_debug("%s: imsg size error", __func__);
202 a596b957 2022-07-14 tracey return 1;
205 a596b957 2022-07-14 tracey /* create a new socket */
206 a596b957 2022-07-14 tracey if ((sock = calloc(1, sizeof(*sock))) == NULL) {
207 a596b957 2022-07-14 tracey if (imsg->fd != -1)
208 a596b957 2022-07-14 tracey close(imsg->fd);
209 a596b957 2022-07-14 tracey return 1;
212 a596b957 2022-07-14 tracey memcpy(&sock->conf, &sock_conf, sizeof(sock->conf));
213 a596b957 2022-07-14 tracey sock->fd = imsg->fd;
215 2ad48e9a 2022-08-16 stsp TAILQ_INSERT_TAIL(&env->sockets, sock, entry);
217 a596b957 2022-07-14 tracey for (i = 0; i < PRIV_FDS__MAX; i++)
218 a596b957 2022-07-14 tracey sock->priv_fd[i] = -1;
220 0f91044a 2022-07-22 stsp for (i = 0; i < GOTWEB_PACK_NUM_TEMPFILES; i++)
221 a596b957 2022-07-14 tracey sock->pack_fds[i] = -1;
223 a596b957 2022-07-14 tracey /* log new socket info */
224 4fcc9f74 2022-08-16 stsp log_debug("%s: name=%s id=%d server=%s af_type=%s socket_path=%s",
225 a596b957 2022-07-14 tracey __func__, sock->conf.name, sock->conf.id, sock->conf.srv_name,
226 4fcc9f74 2022-08-16 stsp sock->conf.af_type == AF_UNIX ? "unix" :
227 4fcc9f74 2022-08-16 stsp (sock->conf.af_type == AF_INET ? "inet" :
228 4fcc9f74 2022-08-16 stsp (sock->conf.af_type == AF_INET6 ? "inet6" : "unknown")),
229 a596b957 2022-07-14 tracey strlen(sock->conf.unix_socket_name) ?
230 a596b957 2022-07-14 tracey sock->conf.unix_socket_name : "none");
232 a596b957 2022-07-14 tracey return 0;
236 a596b957 2022-07-14 tracey config_setfd(struct gotwebd *env, struct socket *sock)
238 a596b957 2022-07-14 tracey struct privsep *ps = env->gotwebd_ps;
239 a596b957 2022-07-14 tracey int id, s;
240 a596b957 2022-07-14 tracey int fd = -1, n, m, j;
241 a596b957 2022-07-14 tracey struct iovec iov[6];
242 a596b957 2022-07-14 tracey size_t c;
243 a596b957 2022-07-14 tracey unsigned int what;
245 a596b957 2022-07-14 tracey log_debug("%s: Allocating %d file descriptors",
246 0f91044a 2022-07-22 stsp __func__, PRIV_FDS__MAX + GOTWEB_PACK_NUM_TEMPFILES);
248 0f91044a 2022-07-22 stsp for (j = 0; j < PRIV_FDS__MAX + GOTWEB_PACK_NUM_TEMPFILES; j++) {
249 a596b957 2022-07-14 tracey for (id = 0; id < PROC_MAX; id++) {
250 a596b957 2022-07-14 tracey what = ps->ps_what[id];
252 a596b957 2022-07-14 tracey if ((what & CONFIG_SOCKS) == 0 || id == privsep_process)
253 a596b957 2022-07-14 tracey continue;
255 a596b957 2022-07-14 tracey s = sock->conf.id;
257 a596b957 2022-07-14 tracey iov[c].iov_base = &s;
258 a596b957 2022-07-14 tracey iov[c++].iov_len = sizeof(s);
260 a596b957 2022-07-14 tracey if (id == PROC_SOCKS) {
262 a596b957 2022-07-14 tracey * XXX imsg code will close the fd
263 a596b957 2022-07-14 tracey * after 1st call
266 a596b957 2022-07-14 tracey proc_range(ps, id, &n, &m);
267 a596b957 2022-07-14 tracey for (n = 0; n < m; n++) {
268 a596b957 2022-07-14 tracey fd = got_opentempfd();
269 a596b957 2022-07-14 tracey if (fd == -1)
270 a596b957 2022-07-14 tracey return 1;
271 a596b957 2022-07-14 tracey if (proc_composev_imsg(ps, id, n,
272 a596b957 2022-07-14 tracey IMSG_CFG_FD, -1, fd, iov, c) != 0) {
273 a596b957 2022-07-14 tracey log_warn("%s: failed to compose "
274 a596b957 2022-07-14 tracey "IMSG_CFG_FD imsg",
275 a596b957 2022-07-14 tracey __func__);
276 a596b957 2022-07-14 tracey return 1;
278 a596b957 2022-07-14 tracey if (proc_flush_imsg(ps, id, n) == -1) {
279 a596b957 2022-07-14 tracey log_warn("%s: failed to flush "
280 a596b957 2022-07-14 tracey "IMSG_CFG_FD imsg",
281 a596b957 2022-07-14 tracey __func__);
282 a596b957 2022-07-14 tracey return 1;
288 a596b957 2022-07-14 tracey /* Close fd early to prevent fd exhaustion in gotwebd. */
289 a596b957 2022-07-14 tracey if (fd != -1)
290 a596b957 2022-07-14 tracey close(fd);
292 a596b957 2022-07-14 tracey return 0;
296 a596b957 2022-07-14 tracey config_getfd(struct gotwebd *env, struct imsg *imsg)
298 a596b957 2022-07-14 tracey struct socket *sock;
299 a596b957 2022-07-14 tracey uint8_t *p = imsg->data;
300 a596b957 2022-07-14 tracey int sock_id, match = 0, i;
302 a596b957 2022-07-14 tracey IMSG_SIZE_CHECK(imsg, &sock_id);
303 a596b957 2022-07-14 tracey memcpy(&sock_id, p, sizeof(sock_id));
305 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(sock, &env->sockets, entry) {
306 0f91044a 2022-07-22 stsp const int nfds = (GOTWEB_PACK_NUM_TEMPFILES + PRIV_FDS__MAX);
307 0f91044a 2022-07-22 stsp for (i = 0; i < nfds; i++) {
308 a596b957 2022-07-14 tracey if (i < PRIV_FDS__MAX && sock->priv_fd[i] == -1) {
309 a596b957 2022-07-14 tracey log_debug("%s: assigning socket %d priv_fd %d",
310 a596b957 2022-07-14 tracey __func__, sock_id, imsg->fd);
311 a596b957 2022-07-14 tracey sock->priv_fd[i] = imsg->fd;
312 a596b957 2022-07-14 tracey match = 1;
315 a596b957 2022-07-14 tracey if (sock->pack_fds[i - PRIV_FDS__MAX] == -1) {
316 a596b957 2022-07-14 tracey log_debug("%s: assigning socket %d pack_fd %d",
317 a596b957 2022-07-14 tracey __func__, sock_id, imsg->fd);
318 a596b957 2022-07-14 tracey sock->pack_fds[i - PRIV_FDS__MAX] = imsg->fd;
319 a596b957 2022-07-14 tracey match = 1;
325 a596b957 2022-07-14 tracey if (match)
326 a596b957 2022-07-14 tracey return 0;
328 a596b957 2022-07-14 tracey return 1;