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 a596b957 2022-07-14 tracey
41 a596b957 2022-07-14 tracey #include "proc.h"
42 a596b957 2022-07-14 tracey #include "gotwebd.h"
43 a596b957 2022-07-14 tracey
44 a596b957 2022-07-14 tracey int
45 a596b957 2022-07-14 tracey config_init(struct gotwebd *env)
46 a596b957 2022-07-14 tracey {
47 a596b957 2022-07-14 tracey struct privsep *ps = env->gotwebd_ps;
48 a596b957 2022-07-14 tracey unsigned int what;
49 a596b957 2022-07-14 tracey
50 a596b957 2022-07-14 tracey /* Global configuration. */
51 a596b957 2022-07-14 tracey if (privsep_process == PROC_GOTWEBD)
52 a596b957 2022-07-14 tracey env->prefork_gotwebd = GOTWEBD_NUMPROC;
53 a596b957 2022-07-14 tracey
54 a596b957 2022-07-14 tracey ps->ps_what[PROC_GOTWEBD] = CONFIG_ALL;
55 a596b957 2022-07-14 tracey ps->ps_what[PROC_SOCKS] = CONFIG_SOCKS;
56 a596b957 2022-07-14 tracey
57 a596b957 2022-07-14 tracey /* Other configuration. */
58 a596b957 2022-07-14 tracey what = ps->ps_what[privsep_process];
59 a596b957 2022-07-14 tracey if (what & CONFIG_SOCKS) {
60 a596b957 2022-07-14 tracey env->server_cnt = 0;
61 2ad48e9a 2022-08-16 stsp TAILQ_INIT(&env->servers);
62 2ad48e9a 2022-08-16 stsp TAILQ_INIT(&env->sockets);
63 a596b957 2022-07-14 tracey }
64 a596b957 2022-07-14 tracey return 0;
65 a596b957 2022-07-14 tracey }
66 a596b957 2022-07-14 tracey
67 a596b957 2022-07-14 tracey int
68 a596b957 2022-07-14 tracey config_getcfg(struct gotwebd *env, struct imsg *imsg)
69 a596b957 2022-07-14 tracey {
70 a596b957 2022-07-14 tracey /* nothing to do but tell gotwebd configuration is done */
71 a596b957 2022-07-14 tracey if (privsep_process != PROC_GOTWEBD)
72 a596b957 2022-07-14 tracey proc_compose(env->gotwebd_ps, PROC_GOTWEBD,
73 a596b957 2022-07-14 tracey IMSG_CFG_DONE, NULL, 0);
74 a596b957 2022-07-14 tracey
75 a596b957 2022-07-14 tracey return 0;
76 a596b957 2022-07-14 tracey }
77 a596b957 2022-07-14 tracey
78 a596b957 2022-07-14 tracey int
79 a596b957 2022-07-14 tracey config_setserver(struct gotwebd *env, struct server *srv)
80 a596b957 2022-07-14 tracey {
81 a596b957 2022-07-14 tracey struct server ssrv;
82 a596b957 2022-07-14 tracey struct privsep *ps = env->gotwebd_ps;
83 a596b957 2022-07-14 tracey
84 a596b957 2022-07-14 tracey memcpy(&ssrv, srv, sizeof(ssrv));
85 7e0ec052 2022-09-06 op if (proc_compose(ps, PROC_SOCKS, IMSG_CFG_SRV, &ssrv, sizeof(ssrv))
86 7e0ec052 2022-09-06 op == -1)
87 7e0ec052 2022-09-06 op fatal("proc_compose");
88 a596b957 2022-07-14 tracey return 0;
89 a596b957 2022-07-14 tracey }
90 a596b957 2022-07-14 tracey
91 a596b957 2022-07-14 tracey int
92 a596b957 2022-07-14 tracey config_getserver(struct gotwebd *env, struct imsg *imsg)
93 a596b957 2022-07-14 tracey {
94 a596b957 2022-07-14 tracey struct server *srv;
95 a596b957 2022-07-14 tracey uint8_t *p = imsg->data;
96 a596b957 2022-07-14 tracey
97 a596b957 2022-07-14 tracey IMSG_SIZE_CHECK(imsg, &srv);
98 a596b957 2022-07-14 tracey
99 a596b957 2022-07-14 tracey srv = calloc(1, sizeof(*srv));
100 a596b957 2022-07-14 tracey if (srv == NULL)
101 a596b957 2022-07-14 tracey fatalx("%s: calloc", __func__);
102 a596b957 2022-07-14 tracey
103 a596b957 2022-07-14 tracey if (IMSG_DATA_SIZE(imsg) != sizeof(*srv)) {
104 a596b957 2022-07-14 tracey log_debug("%s: imsg size error", __func__);
105 a596b957 2022-07-14 tracey free(srv);
106 a596b957 2022-07-14 tracey return 1;
107 a596b957 2022-07-14 tracey }
108 7e0ec052 2022-09-06 op
109 7e0ec052 2022-09-06 op memcpy(srv, p, sizeof(*srv));
110 7e0ec052 2022-09-06 op srv->cached_repos = calloc(GOTWEBD_REPO_CACHESIZE,
111 7e0ec052 2022-09-06 op sizeof(*srv->cached_repos));
112 7e0ec052 2022-09-06 op if (srv->cached_repos == NULL)
113 7e0ec052 2022-09-06 op fatal("%s: calloc", __func__);
114 7e0ec052 2022-09-06 op srv->ncached_repos = 0;
115 a596b957 2022-07-14 tracey
116 a596b957 2022-07-14 tracey /* log server info */
117 a596b957 2022-07-14 tracey log_debug("%s: server=%s fcgi_socket=%s unix_socket=%s", __func__,
118 a596b957 2022-07-14 tracey srv->name, srv->fcgi_socket ? "yes" : "no", srv->unix_socket ?
119 a596b957 2022-07-14 tracey "yes" : "no");
120 a596b957 2022-07-14 tracey
121 2ad48e9a 2022-08-16 stsp TAILQ_INSERT_TAIL(&env->servers, srv, entry);
122 a596b957 2022-07-14 tracey
123 a596b957 2022-07-14 tracey return 0;
124 a596b957 2022-07-14 tracey }
125 a596b957 2022-07-14 tracey
126 a596b957 2022-07-14 tracey int
127 a596b957 2022-07-14 tracey config_setsock(struct gotwebd *env, struct socket *sock)
128 a596b957 2022-07-14 tracey {
129 a596b957 2022-07-14 tracey struct privsep *ps = env->gotwebd_ps;
130 a596b957 2022-07-14 tracey struct socket_conf s;
131 a596b957 2022-07-14 tracey int id;
132 a596b957 2022-07-14 tracey int fd = -1, n, m;
133 a596b957 2022-07-14 tracey struct iovec iov[6];
134 a596b957 2022-07-14 tracey size_t c;
135 a596b957 2022-07-14 tracey unsigned int what;
136 a596b957 2022-07-14 tracey
137 a596b957 2022-07-14 tracey /* open listening sockets */
138 a596b957 2022-07-14 tracey if (sockets_privinit(env, sock) == -1)
139 a596b957 2022-07-14 tracey return -1;
140 a596b957 2022-07-14 tracey
141 a596b957 2022-07-14 tracey for (id = 0; id < PROC_MAX; id++) {
142 a596b957 2022-07-14 tracey what = ps->ps_what[id];
143 a596b957 2022-07-14 tracey
144 a596b957 2022-07-14 tracey if ((what & CONFIG_SOCKS) == 0 || id == privsep_process)
145 a596b957 2022-07-14 tracey continue;
146 a596b957 2022-07-14 tracey
147 a596b957 2022-07-14 tracey memcpy(&s, &sock->conf, sizeof(s));
148 a596b957 2022-07-14 tracey
149 a596b957 2022-07-14 tracey c = 0;
150 a596b957 2022-07-14 tracey iov[c].iov_base = &s;
151 a596b957 2022-07-14 tracey iov[c++].iov_len = sizeof(s);
152 a596b957 2022-07-14 tracey
153 a596b957 2022-07-14 tracey if (id == PROC_SOCKS) {
154 a596b957 2022-07-14 tracey /* XXX imsg code will close the fd after 1st call */
155 a596b957 2022-07-14 tracey n = -1;
156 a596b957 2022-07-14 tracey proc_range(ps, id, &n, &m);
157 a596b957 2022-07-14 tracey for (n = 0; n < m; n++) {
158 a596b957 2022-07-14 tracey if (sock->fd == -1)
159 a596b957 2022-07-14 tracey fd = -1;
160 a596b957 2022-07-14 tracey else if ((fd = dup(sock->fd)) == -1)
161 a596b957 2022-07-14 tracey return 1;
162 a596b957 2022-07-14 tracey if (proc_composev_imsg(ps, id, n, IMSG_CFG_SOCK,
163 a596b957 2022-07-14 tracey -1, fd, iov, c) != 0) {
164 a596b957 2022-07-14 tracey log_warn("%s: failed to compose "
165 a596b957 2022-07-14 tracey "IMSG_CFG_SOCK imsg",
166 a596b957 2022-07-14 tracey __func__);
167 a596b957 2022-07-14 tracey return 1;
168 a596b957 2022-07-14 tracey }
169 a596b957 2022-07-14 tracey if (proc_flush_imsg(ps, id, n) == -1) {
170 a596b957 2022-07-14 tracey log_warn("%s: failed to flush "
171 a596b957 2022-07-14 tracey "IMSG_CFG_SOCK imsg",
172 a596b957 2022-07-14 tracey __func__);
173 a596b957 2022-07-14 tracey return 1;
174 a596b957 2022-07-14 tracey }
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 /* Close socket early to prevent fd exhaustion in gotwebd. */
180 a596b957 2022-07-14 tracey if (sock->fd != -1) {
181 a596b957 2022-07-14 tracey close(sock->fd);
182 a596b957 2022-07-14 tracey sock->fd = -1;
183 a596b957 2022-07-14 tracey }
184 a596b957 2022-07-14 tracey
185 a596b957 2022-07-14 tracey return 0;
186 a596b957 2022-07-14 tracey }
187 a596b957 2022-07-14 tracey
188 a596b957 2022-07-14 tracey int
189 a596b957 2022-07-14 tracey config_getsock(struct gotwebd *env, struct imsg *imsg)
190 a596b957 2022-07-14 tracey {
191 a596b957 2022-07-14 tracey struct socket *sock = NULL;
192 a596b957 2022-07-14 tracey struct socket_conf sock_conf;
193 a596b957 2022-07-14 tracey uint8_t *p = imsg->data;
194 a596b957 2022-07-14 tracey int i;
195 a596b957 2022-07-14 tracey
196 a596b957 2022-07-14 tracey IMSG_SIZE_CHECK(imsg, &sock_conf);
197 a596b957 2022-07-14 tracey memcpy(&sock_conf, p, sizeof(sock_conf));
198 a596b957 2022-07-14 tracey
199 a596b957 2022-07-14 tracey if (IMSG_DATA_SIZE(imsg) != sizeof(sock_conf)) {
200 a596b957 2022-07-14 tracey log_debug("%s: imsg size error", __func__);
201 a596b957 2022-07-14 tracey return 1;
202 a596b957 2022-07-14 tracey }
203 a596b957 2022-07-14 tracey
204 a596b957 2022-07-14 tracey /* create a new socket */
205 a596b957 2022-07-14 tracey if ((sock = calloc(1, sizeof(*sock))) == NULL) {
206 a596b957 2022-07-14 tracey if (imsg->fd != -1)
207 a596b957 2022-07-14 tracey close(imsg->fd);
208 a596b957 2022-07-14 tracey return 1;
209 a596b957 2022-07-14 tracey }
210 a596b957 2022-07-14 tracey
211 a596b957 2022-07-14 tracey memcpy(&sock->conf, &sock_conf, sizeof(sock->conf));
212 a596b957 2022-07-14 tracey sock->fd = imsg->fd;
213 a596b957 2022-07-14 tracey
214 2ad48e9a 2022-08-16 stsp TAILQ_INSERT_TAIL(&env->sockets, sock, entry);
215 a596b957 2022-07-14 tracey
216 a596b957 2022-07-14 tracey for (i = 0; i < PRIV_FDS__MAX; i++)
217 a596b957 2022-07-14 tracey sock->priv_fd[i] = -1;
218 a596b957 2022-07-14 tracey
219 0f91044a 2022-07-22 stsp for (i = 0; i < GOTWEB_PACK_NUM_TEMPFILES; i++)
220 a596b957 2022-07-14 tracey sock->pack_fds[i] = -1;
221 a596b957 2022-07-14 tracey
222 a596b957 2022-07-14 tracey /* log new socket info */
223 4fcc9f74 2022-08-16 stsp log_debug("%s: name=%s id=%d server=%s af_type=%s socket_path=%s",
224 a596b957 2022-07-14 tracey __func__, sock->conf.name, sock->conf.id, sock->conf.srv_name,
225 4fcc9f74 2022-08-16 stsp sock->conf.af_type == AF_UNIX ? "unix" :
226 4fcc9f74 2022-08-16 stsp (sock->conf.af_type == AF_INET ? "inet" :
227 4fcc9f74 2022-08-16 stsp (sock->conf.af_type == AF_INET6 ? "inet6" : "unknown")),
228 a596b957 2022-07-14 tracey strlen(sock->conf.unix_socket_name) ?
229 a596b957 2022-07-14 tracey sock->conf.unix_socket_name : "none");
230 a596b957 2022-07-14 tracey
231 a596b957 2022-07-14 tracey return 0;
232 a596b957 2022-07-14 tracey }
233 a596b957 2022-07-14 tracey
234 a596b957 2022-07-14 tracey int
235 a596b957 2022-07-14 tracey config_setfd(struct gotwebd *env, struct socket *sock)
236 a596b957 2022-07-14 tracey {
237 a596b957 2022-07-14 tracey struct privsep *ps = env->gotwebd_ps;
238 a596b957 2022-07-14 tracey int id, s;
239 a596b957 2022-07-14 tracey int fd = -1, n, m, j;
240 a596b957 2022-07-14 tracey struct iovec iov[6];
241 a596b957 2022-07-14 tracey size_t c;
242 a596b957 2022-07-14 tracey unsigned int what;
243 a596b957 2022-07-14 tracey
244 a596b957 2022-07-14 tracey log_debug("%s: Allocating %d file descriptors",
245 0f91044a 2022-07-22 stsp __func__, PRIV_FDS__MAX + GOTWEB_PACK_NUM_TEMPFILES);
246 a596b957 2022-07-14 tracey
247 0f91044a 2022-07-22 stsp for (j = 0; j < PRIV_FDS__MAX + GOTWEB_PACK_NUM_TEMPFILES; j++) {
248 a596b957 2022-07-14 tracey for (id = 0; id < PROC_MAX; id++) {
249 a596b957 2022-07-14 tracey what = ps->ps_what[id];
250 a596b957 2022-07-14 tracey
251 a596b957 2022-07-14 tracey if ((what & CONFIG_SOCKS) == 0 || id == privsep_process)
252 a596b957 2022-07-14 tracey continue;
253 a596b957 2022-07-14 tracey
254 a596b957 2022-07-14 tracey s = sock->conf.id;
255 a596b957 2022-07-14 tracey c = 0;
256 a596b957 2022-07-14 tracey iov[c].iov_base = &s;
257 a596b957 2022-07-14 tracey iov[c++].iov_len = sizeof(s);
258 a596b957 2022-07-14 tracey
259 a596b957 2022-07-14 tracey if (id == PROC_SOCKS) {
260 a596b957 2022-07-14 tracey /*
261 a596b957 2022-07-14 tracey * XXX imsg code will close the fd
262 a596b957 2022-07-14 tracey * after 1st call
263 a596b957 2022-07-14 tracey */
264 a596b957 2022-07-14 tracey n = -1;
265 a596b957 2022-07-14 tracey proc_range(ps, id, &n, &m);
266 a596b957 2022-07-14 tracey for (n = 0; n < m; n++) {
267 a596b957 2022-07-14 tracey fd = got_opentempfd();
268 a596b957 2022-07-14 tracey if (fd == -1)
269 a596b957 2022-07-14 tracey return 1;
270 a596b957 2022-07-14 tracey if (proc_composev_imsg(ps, id, n,
271 a596b957 2022-07-14 tracey IMSG_CFG_FD, -1, fd, iov, c) != 0) {
272 a596b957 2022-07-14 tracey log_warn("%s: failed to compose "
273 a596b957 2022-07-14 tracey "IMSG_CFG_FD imsg",
274 a596b957 2022-07-14 tracey __func__);
275 a596b957 2022-07-14 tracey return 1;
276 a596b957 2022-07-14 tracey }
277 a596b957 2022-07-14 tracey if (proc_flush_imsg(ps, id, n) == -1) {
278 a596b957 2022-07-14 tracey log_warn("%s: failed to flush "
279 a596b957 2022-07-14 tracey "IMSG_CFG_FD imsg",
280 a596b957 2022-07-14 tracey __func__);
281 a596b957 2022-07-14 tracey return 1;
282 a596b957 2022-07-14 tracey }
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 /* Close fd early to prevent fd exhaustion in gotwebd. */
288 a596b957 2022-07-14 tracey if (fd != -1)
289 a596b957 2022-07-14 tracey close(fd);
290 a596b957 2022-07-14 tracey }
291 a596b957 2022-07-14 tracey return 0;
292 a596b957 2022-07-14 tracey }
293 a596b957 2022-07-14 tracey
294 a596b957 2022-07-14 tracey int
295 a596b957 2022-07-14 tracey config_getfd(struct gotwebd *env, struct imsg *imsg)
296 a596b957 2022-07-14 tracey {
297 a596b957 2022-07-14 tracey struct socket *sock;
298 a596b957 2022-07-14 tracey uint8_t *p = imsg->data;
299 a596b957 2022-07-14 tracey int sock_id, match = 0, i;
300 a596b957 2022-07-14 tracey
301 a596b957 2022-07-14 tracey IMSG_SIZE_CHECK(imsg, &sock_id);
302 a596b957 2022-07-14 tracey memcpy(&sock_id, p, sizeof(sock_id));
303 a596b957 2022-07-14 tracey
304 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(sock, &env->sockets, entry) {
305 0f91044a 2022-07-22 stsp const int nfds = (GOTWEB_PACK_NUM_TEMPFILES + PRIV_FDS__MAX);
306 0f91044a 2022-07-22 stsp for (i = 0; i < nfds; i++) {
307 a596b957 2022-07-14 tracey if (i < PRIV_FDS__MAX && sock->priv_fd[i] == -1) {
308 a596b957 2022-07-14 tracey log_debug("%s: assigning socket %d priv_fd %d",
309 a596b957 2022-07-14 tracey __func__, sock_id, imsg->fd);
310 a596b957 2022-07-14 tracey sock->priv_fd[i] = imsg->fd;
311 a596b957 2022-07-14 tracey match = 1;
312 a596b957 2022-07-14 tracey break;
313 a596b957 2022-07-14 tracey }
314 a596b957 2022-07-14 tracey if (sock->pack_fds[i - PRIV_FDS__MAX] == -1) {
315 a596b957 2022-07-14 tracey log_debug("%s: assigning socket %d pack_fd %d",
316 a596b957 2022-07-14 tracey __func__, sock_id, imsg->fd);
317 a596b957 2022-07-14 tracey sock->pack_fds[i - PRIV_FDS__MAX] = imsg->fd;
318 a596b957 2022-07-14 tracey match = 1;
319 a596b957 2022-07-14 tracey break;
320 a596b957 2022-07-14 tracey }
321 a596b957 2022-07-14 tracey }
322 a596b957 2022-07-14 tracey }
323 a596b957 2022-07-14 tracey
324 a596b957 2022-07-14 tracey if (match)
325 a596b957 2022-07-14 tracey return 0;
326 a596b957 2022-07-14 tracey else
327 a596b957 2022-07-14 tracey return 1;
328 a596b957 2022-07-14 tracey }