Blame


1 a596b957 2022-07-14 tracey /*
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>
4 a596b957 2022-07-14 tracey *
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.
8 a596b957 2022-07-14 tracey *
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.
16 a596b957 2022-07-14 tracey */
17 a596b957 2022-07-14 tracey
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>
23 a596b957 2022-07-14 tracey
24 a596b957 2022-07-14 tracey #include <net/if.h>
25 a596b957 2022-07-14 tracey #include <netinet/in.h>
26 a596b957 2022-07-14 tracey
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>
38 a596b957 2022-07-14 tracey
39 a596b957 2022-07-14 tracey #include "got_opentemp.h"
40 df2d3cd2 2023-03-11 op #include "got_reference.h"
41 a596b957 2022-07-14 tracey
42 a596b957 2022-07-14 tracey #include "proc.h"
43 a596b957 2022-07-14 tracey #include "gotwebd.h"
44 a596b957 2022-07-14 tracey
45 a596b957 2022-07-14 tracey int
46 a596b957 2022-07-14 tracey config_init(struct gotwebd *env)
47 a596b957 2022-07-14 tracey {
48 a596b957 2022-07-14 tracey struct privsep *ps = env->gotwebd_ps;
49 a596b957 2022-07-14 tracey unsigned int what;
50 a596b957 2022-07-14 tracey
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;
54 a596b957 2022-07-14 tracey
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;
57 a596b957 2022-07-14 tracey
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);
64 a596b957 2022-07-14 tracey }
65 a596b957 2022-07-14 tracey return 0;
66 a596b957 2022-07-14 tracey }
67 a596b957 2022-07-14 tracey
68 a596b957 2022-07-14 tracey int
69 a596b957 2022-07-14 tracey config_getcfg(struct gotwebd *env, struct imsg *imsg)
70 a596b957 2022-07-14 tracey {
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);
75 a596b957 2022-07-14 tracey
76 a596b957 2022-07-14 tracey return 0;
77 a596b957 2022-07-14 tracey }
78 a596b957 2022-07-14 tracey
79 a596b957 2022-07-14 tracey int
80 a596b957 2022-07-14 tracey config_setserver(struct gotwebd *env, struct server *srv)
81 a596b957 2022-07-14 tracey {
82 a596b957 2022-07-14 tracey struct server ssrv;
83 a596b957 2022-07-14 tracey struct privsep *ps = env->gotwebd_ps;
84 a596b957 2022-07-14 tracey
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))
87 7e0ec052 2022-09-06 op == -1)
88 7e0ec052 2022-09-06 op fatal("proc_compose");
89 a596b957 2022-07-14 tracey return 0;
90 a596b957 2022-07-14 tracey }
91 a596b957 2022-07-14 tracey
92 a596b957 2022-07-14 tracey int
93 a596b957 2022-07-14 tracey config_getserver(struct gotwebd *env, struct imsg *imsg)
94 a596b957 2022-07-14 tracey {
95 a596b957 2022-07-14 tracey struct server *srv;
96 a596b957 2022-07-14 tracey uint8_t *p = imsg->data;
97 a596b957 2022-07-14 tracey
98 a596b957 2022-07-14 tracey IMSG_SIZE_CHECK(imsg, &srv);
99 a596b957 2022-07-14 tracey
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__);
103 a596b957 2022-07-14 tracey
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;
108 a596b957 2022-07-14 tracey }
109 7e0ec052 2022-09-06 op
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;
116 a596b957 2022-07-14 tracey
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");
121 a596b957 2022-07-14 tracey
122 2ad48e9a 2022-08-16 stsp TAILQ_INSERT_TAIL(&env->servers, srv, entry);
123 a596b957 2022-07-14 tracey
124 a596b957 2022-07-14 tracey return 0;
125 a596b957 2022-07-14 tracey }
126 a596b957 2022-07-14 tracey
127 a596b957 2022-07-14 tracey int
128 a596b957 2022-07-14 tracey config_setsock(struct gotwebd *env, struct socket *sock)
129 a596b957 2022-07-14 tracey {
130 a596b957 2022-07-14 tracey struct privsep *ps = env->gotwebd_ps;
131 a596b957 2022-07-14 tracey struct socket_conf s;
132 a596b957 2022-07-14 tracey int id;
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;
137 a596b957 2022-07-14 tracey
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;
141 a596b957 2022-07-14 tracey
142 a596b957 2022-07-14 tracey for (id = 0; id < PROC_MAX; id++) {
143 a596b957 2022-07-14 tracey what = ps->ps_what[id];
144 a596b957 2022-07-14 tracey
145 a596b957 2022-07-14 tracey if ((what & CONFIG_SOCKS) == 0 || id == privsep_process)
146 a596b957 2022-07-14 tracey continue;
147 a596b957 2022-07-14 tracey
148 a596b957 2022-07-14 tracey memcpy(&s, &sock->conf, sizeof(s));
149 a596b957 2022-07-14 tracey
150 a596b957 2022-07-14 tracey c = 0;
151 a596b957 2022-07-14 tracey iov[c].iov_base = &s;
152 a596b957 2022-07-14 tracey iov[c++].iov_len = sizeof(s);
153 a596b957 2022-07-14 tracey
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 */
156 a596b957 2022-07-14 tracey n = -1;
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)
160 a596b957 2022-07-14 tracey 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;
169 a596b957 2022-07-14 tracey }
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;
175 a596b957 2022-07-14 tracey }
176 a596b957 2022-07-14 tracey }
177 a596b957 2022-07-14 tracey }
178 a596b957 2022-07-14 tracey }
179 a596b957 2022-07-14 tracey
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;
184 a596b957 2022-07-14 tracey }
185 a596b957 2022-07-14 tracey
186 a596b957 2022-07-14 tracey return 0;
187 a596b957 2022-07-14 tracey }
188 a596b957 2022-07-14 tracey
189 a596b957 2022-07-14 tracey int
190 a596b957 2022-07-14 tracey config_getsock(struct gotwebd *env, struct imsg *imsg)
191 a596b957 2022-07-14 tracey {
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;
195 a596b957 2022-07-14 tracey int i;
196 a596b957 2022-07-14 tracey
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));
199 a596b957 2022-07-14 tracey
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;
203 a596b957 2022-07-14 tracey }
204 a596b957 2022-07-14 tracey
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;
210 a596b957 2022-07-14 tracey }
211 a596b957 2022-07-14 tracey
212 a596b957 2022-07-14 tracey memcpy(&sock->conf, &sock_conf, sizeof(sock->conf));
213 a596b957 2022-07-14 tracey sock->fd = imsg->fd;
214 a596b957 2022-07-14 tracey
215 2ad48e9a 2022-08-16 stsp TAILQ_INSERT_TAIL(&env->sockets, sock, entry);
216 a596b957 2022-07-14 tracey
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;
219 a596b957 2022-07-14 tracey
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;
222 a596b957 2022-07-14 tracey
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");
231 a596b957 2022-07-14 tracey
232 a596b957 2022-07-14 tracey return 0;
233 a596b957 2022-07-14 tracey }
234 a596b957 2022-07-14 tracey
235 a596b957 2022-07-14 tracey int
236 a596b957 2022-07-14 tracey config_setfd(struct gotwebd *env, struct socket *sock)
237 a596b957 2022-07-14 tracey {
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;
244 a596b957 2022-07-14 tracey
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);
247 a596b957 2022-07-14 tracey
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];
251 a596b957 2022-07-14 tracey
252 a596b957 2022-07-14 tracey if ((what & CONFIG_SOCKS) == 0 || id == privsep_process)
253 a596b957 2022-07-14 tracey continue;
254 a596b957 2022-07-14 tracey
255 a596b957 2022-07-14 tracey s = sock->conf.id;
256 a596b957 2022-07-14 tracey c = 0;
257 a596b957 2022-07-14 tracey iov[c].iov_base = &s;
258 a596b957 2022-07-14 tracey iov[c++].iov_len = sizeof(s);
259 a596b957 2022-07-14 tracey
260 a596b957 2022-07-14 tracey if (id == PROC_SOCKS) {
261 a596b957 2022-07-14 tracey /*
262 a596b957 2022-07-14 tracey * XXX imsg code will close the fd
263 a596b957 2022-07-14 tracey * after 1st call
264 a596b957 2022-07-14 tracey */
265 a596b957 2022-07-14 tracey n = -1;
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;
277 a596b957 2022-07-14 tracey }
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;
283 a596b957 2022-07-14 tracey }
284 a596b957 2022-07-14 tracey }
285 a596b957 2022-07-14 tracey }
286 a596b957 2022-07-14 tracey }
287 a596b957 2022-07-14 tracey
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);
291 a596b957 2022-07-14 tracey }
292 a596b957 2022-07-14 tracey return 0;
293 a596b957 2022-07-14 tracey }
294 a596b957 2022-07-14 tracey
295 a596b957 2022-07-14 tracey int
296 a596b957 2022-07-14 tracey config_getfd(struct gotwebd *env, struct imsg *imsg)
297 a596b957 2022-07-14 tracey {
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;
301 a596b957 2022-07-14 tracey
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));
304 a596b957 2022-07-14 tracey
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;
313 a596b957 2022-07-14 tracey break;
314 a596b957 2022-07-14 tracey }
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;
320 a596b957 2022-07-14 tracey break;
321 a596b957 2022-07-14 tracey }
322 a596b957 2022-07-14 tracey }
323 a596b957 2022-07-14 tracey }
324 a596b957 2022-07-14 tracey
325 a596b957 2022-07-14 tracey if (match)
326 a596b957 2022-07-14 tracey return 0;
327 a596b957 2022-07-14 tracey else
328 a596b957 2022-07-14 tracey return 1;
329 a596b957 2022-07-14 tracey }