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