Blob


1 /*
2 * Copyright (c) 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/time.h>
21 #include <sys/uio.h>
22 #include <sys/socket.h>
24 #include <net/if.h>
25 #include <netinet/in.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <termios.h>
30 #include <unistd.h>
31 #include <limits.h>
32 #include <string.h>
33 #include <event.h>
34 #include <fcntl.h>
35 #include <util.h>
36 #include <errno.h>
37 #include <imsg.h>
39 #include "got_opentemp.h"
41 #include "proc.h"
42 #include "gotwebd.h"
44 int
45 config_init(struct gotwebd *env)
46 {
47 struct privsep *ps = env->gotwebd_ps;
48 unsigned int what;
50 /* Global configuration. */
51 if (privsep_process == PROC_GOTWEBD)
52 env->prefork_gotwebd = GOTWEBD_NUMPROC;
54 ps->ps_what[PROC_GOTWEBD] = CONFIG_ALL;
55 ps->ps_what[PROC_SOCKS] = CONFIG_SOCKS;
57 /* Other configuration. */
58 what = ps->ps_what[privsep_process];
59 if (what & CONFIG_SOCKS) {
60 env->server_cnt = 0;
61 TAILQ_INIT(&env->servers);
62 TAILQ_INIT(&env->sockets);
63 }
64 return 0;
65 }
67 int
68 config_getcfg(struct gotwebd *env, struct imsg *imsg)
69 {
70 /* nothing to do but tell gotwebd configuration is done */
71 if (privsep_process != PROC_GOTWEBD)
72 proc_compose(env->gotwebd_ps, PROC_GOTWEBD,
73 IMSG_CFG_DONE, NULL, 0);
75 return 0;
76 }
78 int
79 config_setserver(struct gotwebd *env, struct server *srv)
80 {
81 struct server ssrv;
82 struct privsep *ps = env->gotwebd_ps;
84 memcpy(&ssrv, srv, sizeof(ssrv));
85 if (proc_compose(ps, PROC_SOCKS, IMSG_CFG_SRV, &ssrv, sizeof(ssrv))
86 == -1)
87 fatal("proc_compose");
88 return 0;
89 }
91 int
92 config_getserver(struct gotwebd *env, struct imsg *imsg)
93 {
94 struct server *srv;
95 uint8_t *p = imsg->data;
97 IMSG_SIZE_CHECK(imsg, &srv);
99 srv = calloc(1, sizeof(*srv));
100 if (srv == NULL)
101 fatalx("%s: calloc", __func__);
103 if (IMSG_DATA_SIZE(imsg) != sizeof(*srv)) {
104 log_debug("%s: imsg size error", __func__);
105 free(srv);
106 return 1;
109 memcpy(srv, p, sizeof(*srv));
110 srv->cached_repos = calloc(GOTWEBD_REPO_CACHESIZE,
111 sizeof(*srv->cached_repos));
112 if (srv->cached_repos == NULL)
113 fatal("%s: calloc", __func__);
114 srv->ncached_repos = 0;
116 /* log server info */
117 log_debug("%s: server=%s fcgi_socket=%s unix_socket=%s", __func__,
118 srv->name, srv->fcgi_socket ? "yes" : "no", srv->unix_socket ?
119 "yes" : "no");
121 TAILQ_INSERT_TAIL(&env->servers, srv, entry);
123 return 0;
126 int
127 config_setsock(struct gotwebd *env, struct socket *sock)
129 struct privsep *ps = env->gotwebd_ps;
130 struct socket_conf s;
131 int id;
132 int fd = -1, n, m;
133 struct iovec iov[6];
134 size_t c;
135 unsigned int what;
137 /* open listening sockets */
138 if (sockets_privinit(env, sock) == -1)
139 return -1;
141 for (id = 0; id < PROC_MAX; id++) {
142 what = ps->ps_what[id];
144 if ((what & CONFIG_SOCKS) == 0 || id == privsep_process)
145 continue;
147 memcpy(&s, &sock->conf, sizeof(s));
149 c = 0;
150 iov[c].iov_base = &s;
151 iov[c++].iov_len = sizeof(s);
153 if (id == PROC_SOCKS) {
154 /* XXX imsg code will close the fd after 1st call */
155 n = -1;
156 proc_range(ps, id, &n, &m);
157 for (n = 0; n < m; n++) {
158 if (sock->fd == -1)
159 fd = -1;
160 else if ((fd = dup(sock->fd)) == -1)
161 return 1;
162 if (proc_composev_imsg(ps, id, n, IMSG_CFG_SOCK,
163 -1, fd, iov, c) != 0) {
164 log_warn("%s: failed to compose "
165 "IMSG_CFG_SOCK imsg",
166 __func__);
167 return 1;
169 if (proc_flush_imsg(ps, id, n) == -1) {
170 log_warn("%s: failed to flush "
171 "IMSG_CFG_SOCK imsg",
172 __func__);
173 return 1;
179 /* Close socket early to prevent fd exhaustion in gotwebd. */
180 if (sock->fd != -1) {
181 close(sock->fd);
182 sock->fd = -1;
185 return 0;
188 int
189 config_getsock(struct gotwebd *env, struct imsg *imsg)
191 struct socket *sock = NULL;
192 struct socket_conf sock_conf;
193 uint8_t *p = imsg->data;
194 int i;
196 IMSG_SIZE_CHECK(imsg, &sock_conf);
197 memcpy(&sock_conf, p, sizeof(sock_conf));
199 if (IMSG_DATA_SIZE(imsg) != sizeof(sock_conf)) {
200 log_debug("%s: imsg size error", __func__);
201 return 1;
204 /* create a new socket */
205 if ((sock = calloc(1, sizeof(*sock))) == NULL) {
206 if (imsg->fd != -1)
207 close(imsg->fd);
208 return 1;
211 memcpy(&sock->conf, &sock_conf, sizeof(sock->conf));
212 sock->fd = imsg->fd;
214 TAILQ_INSERT_TAIL(&env->sockets, sock, entry);
216 for (i = 0; i < PRIV_FDS__MAX; i++)
217 sock->priv_fd[i] = -1;
219 for (i = 0; i < GOTWEB_PACK_NUM_TEMPFILES; i++)
220 sock->pack_fds[i] = -1;
222 /* log new socket info */
223 log_debug("%s: name=%s id=%d server=%s af_type=%s socket_path=%s",
224 __func__, sock->conf.name, sock->conf.id, sock->conf.srv_name,
225 sock->conf.af_type == AF_UNIX ? "unix" :
226 (sock->conf.af_type == AF_INET ? "inet" :
227 (sock->conf.af_type == AF_INET6 ? "inet6" : "unknown")),
228 strlen(sock->conf.unix_socket_name) ?
229 sock->conf.unix_socket_name : "none");
231 return 0;
234 int
235 config_setfd(struct gotwebd *env, struct socket *sock)
237 struct privsep *ps = env->gotwebd_ps;
238 int id, s;
239 int fd = -1, n, m, j;
240 struct iovec iov[6];
241 size_t c;
242 unsigned int what;
244 log_debug("%s: Allocating %d file descriptors",
245 __func__, PRIV_FDS__MAX + GOTWEB_PACK_NUM_TEMPFILES);
247 for (j = 0; j < PRIV_FDS__MAX + GOTWEB_PACK_NUM_TEMPFILES; j++) {
248 for (id = 0; id < PROC_MAX; id++) {
249 what = ps->ps_what[id];
251 if ((what & CONFIG_SOCKS) == 0 || id == privsep_process)
252 continue;
254 s = sock->conf.id;
255 c = 0;
256 iov[c].iov_base = &s;
257 iov[c++].iov_len = sizeof(s);
259 if (id == PROC_SOCKS) {
260 /*
261 * XXX imsg code will close the fd
262 * after 1st call
263 */
264 n = -1;
265 proc_range(ps, id, &n, &m);
266 for (n = 0; n < m; n++) {
267 fd = got_opentempfd();
268 if (fd == -1)
269 return 1;
270 if (proc_composev_imsg(ps, id, n,
271 IMSG_CFG_FD, -1, fd, iov, c) != 0) {
272 log_warn("%s: failed to compose "
273 "IMSG_CFG_FD imsg",
274 __func__);
275 return 1;
277 if (proc_flush_imsg(ps, id, n) == -1) {
278 log_warn("%s: failed to flush "
279 "IMSG_CFG_FD imsg",
280 __func__);
281 return 1;
287 /* Close fd early to prevent fd exhaustion in gotwebd. */
288 if (fd != -1)
289 close(fd);
291 return 0;
294 int
295 config_getfd(struct gotwebd *env, struct imsg *imsg)
297 struct socket *sock;
298 uint8_t *p = imsg->data;
299 int sock_id, match = 0, i;
301 IMSG_SIZE_CHECK(imsg, &sock_id);
302 memcpy(&sock_id, p, sizeof(sock_id));
304 TAILQ_FOREACH(sock, &env->sockets, entry) {
305 const int nfds = (GOTWEB_PACK_NUM_TEMPFILES + PRIV_FDS__MAX);
306 for (i = 0; i < nfds; i++) {
307 if (i < PRIV_FDS__MAX && sock->priv_fd[i] == -1) {
308 log_debug("%s: assigning socket %d priv_fd %d",
309 __func__, sock_id, imsg->fd);
310 sock->priv_fd[i] = imsg->fd;
311 match = 1;
312 break;
314 if (sock->pack_fds[i - PRIV_FDS__MAX] == -1) {
315 log_debug("%s: assigning socket %d pack_fd %d",
316 __func__, sock_id, imsg->fd);
317 sock->pack_fds[i - PRIV_FDS__MAX] = imsg->fd;
318 match = 1;
319 break;
324 if (match)
325 return 0;
326 else
327 return 1;