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 proc_compose(ps, PROC_SOCKS, IMSG_CFG_SRV, &ssrv, sizeof(ssrv));
86 return 0;
87 }
89 int
90 config_getserver(struct gotwebd *env, struct imsg *imsg)
91 {
92 struct server *srv;
93 uint8_t *p = imsg->data;
95 IMSG_SIZE_CHECK(imsg, &srv);
97 srv = calloc(1, sizeof(*srv));
98 if (srv == NULL)
99 fatalx("%s: calloc", __func__);
100 memcpy(srv, p, sizeof(*srv));
102 if (IMSG_DATA_SIZE(imsg) != sizeof(*srv)) {
103 log_debug("%s: imsg size error", __func__);
104 free(srv);
105 return 1;
108 /* log server info */
109 log_debug("%s: server=%s fcgi_socket=%s unix_socket=%s", __func__,
110 srv->name, srv->fcgi_socket ? "yes" : "no", srv->unix_socket ?
111 "yes" : "no");
113 TAILQ_INSERT_TAIL(&env->servers, srv, entry);
115 return 0;
118 int
119 config_setsock(struct gotwebd *env, struct socket *sock)
121 struct privsep *ps = env->gotwebd_ps;
122 struct socket_conf s;
123 int id;
124 int fd = -1, n, m;
125 struct iovec iov[6];
126 size_t c;
127 unsigned int what;
129 /* open listening sockets */
130 if (sockets_privinit(env, sock) == -1)
131 return -1;
133 for (id = 0; id < PROC_MAX; id++) {
134 what = ps->ps_what[id];
136 if ((what & CONFIG_SOCKS) == 0 || id == privsep_process)
137 continue;
139 memcpy(&s, &sock->conf, sizeof(s));
141 c = 0;
142 iov[c].iov_base = &s;
143 iov[c++].iov_len = sizeof(s);
145 if (id == PROC_SOCKS) {
146 /* XXX imsg code will close the fd after 1st call */
147 n = -1;
148 proc_range(ps, id, &n, &m);
149 for (n = 0; n < m; n++) {
150 if (sock->fd == -1)
151 fd = -1;
152 else if ((fd = dup(sock->fd)) == -1)
153 return 1;
154 if (proc_composev_imsg(ps, id, n, IMSG_CFG_SOCK,
155 -1, fd, iov, c) != 0) {
156 log_warn("%s: failed to compose "
157 "IMSG_CFG_SOCK imsg",
158 __func__);
159 return 1;
161 if (proc_flush_imsg(ps, id, n) == -1) {
162 log_warn("%s: failed to flush "
163 "IMSG_CFG_SOCK imsg",
164 __func__);
165 return 1;
171 /* Close socket early to prevent fd exhaustion in gotwebd. */
172 if (sock->fd != -1) {
173 close(sock->fd);
174 sock->fd = -1;
177 return 0;
180 int
181 config_getsock(struct gotwebd *env, struct imsg *imsg)
183 struct socket *sock = NULL;
184 struct socket_conf sock_conf;
185 uint8_t *p = imsg->data;
186 int i;
188 IMSG_SIZE_CHECK(imsg, &sock_conf);
189 memcpy(&sock_conf, p, sizeof(sock_conf));
191 if (IMSG_DATA_SIZE(imsg) != sizeof(sock_conf)) {
192 log_debug("%s: imsg size error", __func__);
193 return 1;
196 /* create a new socket */
197 if ((sock = calloc(1, sizeof(*sock))) == NULL) {
198 if (imsg->fd != -1)
199 close(imsg->fd);
200 return 1;
203 memcpy(&sock->conf, &sock_conf, sizeof(sock->conf));
204 sock->fd = imsg->fd;
206 TAILQ_INSERT_TAIL(&env->sockets, sock, entry);
208 for (i = 0; i < PRIV_FDS__MAX; i++)
209 sock->priv_fd[i] = -1;
211 for (i = 0; i < GOTWEB_PACK_NUM_TEMPFILES; i++)
212 sock->pack_fds[i] = -1;
214 /* log new socket info */
215 log_debug("%s: name=%s id=%d server=%s af_type=%s socket_path=%s",
216 __func__, sock->conf.name, sock->conf.id, sock->conf.srv_name,
217 sock->conf.af_type == AF_UNIX ? "unix" :
218 (sock->conf.af_type == AF_INET ? "inet" :
219 (sock->conf.af_type == AF_INET6 ? "inet6" : "unknown")),
220 strlen(sock->conf.unix_socket_name) ?
221 sock->conf.unix_socket_name : "none");
223 return 0;
226 int
227 config_setfd(struct gotwebd *env, struct socket *sock)
229 struct privsep *ps = env->gotwebd_ps;
230 int id, s;
231 int fd = -1, n, m, j;
232 struct iovec iov[6];
233 size_t c;
234 unsigned int what;
236 log_debug("%s: Allocating %d file descriptors",
237 __func__, PRIV_FDS__MAX + GOTWEB_PACK_NUM_TEMPFILES);
239 for (j = 0; j < PRIV_FDS__MAX + GOTWEB_PACK_NUM_TEMPFILES; j++) {
240 for (id = 0; id < PROC_MAX; id++) {
241 what = ps->ps_what[id];
243 if ((what & CONFIG_SOCKS) == 0 || id == privsep_process)
244 continue;
246 s = sock->conf.id;
247 c = 0;
248 iov[c].iov_base = &s;
249 iov[c++].iov_len = sizeof(s);
251 if (id == PROC_SOCKS) {
252 /*
253 * XXX imsg code will close the fd
254 * after 1st call
255 */
256 n = -1;
257 proc_range(ps, id, &n, &m);
258 for (n = 0; n < m; n++) {
259 fd = got_opentempfd();
260 if (fd == -1)
261 return 1;
262 if (proc_composev_imsg(ps, id, n,
263 IMSG_CFG_FD, -1, fd, iov, c) != 0) {
264 log_warn("%s: failed to compose "
265 "IMSG_CFG_FD imsg",
266 __func__);
267 return 1;
269 if (proc_flush_imsg(ps, id, n) == -1) {
270 log_warn("%s: failed to flush "
271 "IMSG_CFG_FD imsg",
272 __func__);
273 return 1;
279 /* Close fd early to prevent fd exhaustion in gotwebd. */
280 if (fd != -1)
281 close(fd);
283 return 0;
286 int
287 config_getfd(struct gotwebd *env, struct imsg *imsg)
289 struct socket *sock;
290 uint8_t *p = imsg->data;
291 int sock_id, match = 0, i;
293 IMSG_SIZE_CHECK(imsg, &sock_id);
294 memcpy(&sock_id, p, sizeof(sock_id));
296 TAILQ_FOREACH(sock, &env->sockets, entry) {
297 const int nfds = (GOTWEB_PACK_NUM_TEMPFILES + PRIV_FDS__MAX);
298 for (i = 0; i < nfds; i++) {
299 if (i < PRIV_FDS__MAX && sock->priv_fd[i] == -1) {
300 log_debug("%s: assigning socket %d priv_fd %d",
301 __func__, sock_id, imsg->fd);
302 sock->priv_fd[i] = imsg->fd;
303 match = 1;
304 break;
306 if (sock->pack_fds[i - PRIV_FDS__MAX] == -1) {
307 log_debug("%s: assigning socket %d pack_fd %d",
308 __func__, sock_id, imsg->fd);
309 sock->pack_fds[i - PRIV_FDS__MAX] = imsg->fd;
310 match = 1;
311 break;
316 if (match)
317 return 0;
318 else
319 return 1;