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"
40 #include "got_reference.h"
42 #include "gotwebd.h"
44 int
45 config_init(struct gotwebd *env)
46 {
47 strlcpy(env->httpd_chroot, D_HTTPD_CHROOT, sizeof(env->httpd_chroot));
49 env->prefork_gotwebd = GOTWEBD_NUMPROC;
50 env->server_cnt = 0;
51 TAILQ_INIT(&env->servers);
52 TAILQ_INIT(&env->sockets);
53 TAILQ_INIT(&env->addresses);
55 return 0;
56 }
58 int
59 config_getcfg(struct gotwebd *env, struct imsg *imsg)
60 {
61 /* nothing to do but tell gotwebd configuration is done */
62 if (sockets_compose_main(env, IMSG_CFG_DONE, NULL, 0) == -1)
63 fatal("sockets_compose_main IMSG_CFG_DONE");
64 return 0;
65 }
67 int
68 config_setserver(struct gotwebd *env, struct server *srv)
69 {
70 if (main_compose_sockets(env, IMSG_CFG_SRV, -1, srv, sizeof(*srv))
71 == -1)
72 fatal("main_compose_sockets IMSG_CFG_SRV");
73 return 0;
74 }
76 int
77 config_getserver(struct gotwebd *env, struct imsg *imsg)
78 {
79 struct server *srv;
80 uint8_t *p = imsg->data;
82 srv = calloc(1, sizeof(*srv));
83 if (srv == NULL)
84 fatalx("%s: calloc", __func__);
86 if (IMSG_DATA_SIZE(imsg) != sizeof(*srv))
87 fatalx("%s: wrong size", __func__);
89 memcpy(srv, p, sizeof(*srv));
91 /* log server info */
92 log_debug("%s: server=%s", __func__, srv->name);
94 TAILQ_INSERT_TAIL(&env->servers, srv, entry);
96 return 0;
97 }
99 int
100 config_setsock(struct gotwebd *env, struct socket *sock)
102 /* open listening sockets */
103 if (sockets_privinit(env, sock) == -1)
104 return -1;
106 if (main_compose_sockets(env, IMSG_CFG_SOCK, sock->fd,
107 &sock->conf, sizeof(sock->conf)) == -1)
108 fatal("main_compose_sockets IMSG_CFG_SOCK");
110 sock->fd = -1;
111 return 0;
114 int
115 config_getsock(struct gotwebd *env, struct imsg *imsg)
117 struct socket *sock = NULL;
118 struct socket_conf sock_conf;
119 uint8_t *p = imsg->data;
120 int i;
122 if (IMSG_DATA_SIZE(imsg) != sizeof(sock_conf))
123 fatalx("%s: wrong size", __func__);
125 memcpy(&sock_conf, p, sizeof(sock_conf));
127 if (IMSG_DATA_SIZE(imsg) != sizeof(sock_conf)) {
128 log_debug("%s: imsg size error", __func__);
129 return 1;
132 /* create a new socket */
133 if ((sock = calloc(1, sizeof(*sock))) == NULL) {
134 return 1;
137 memcpy(&sock->conf, &sock_conf, sizeof(sock->conf));
138 sock->fd = imsg_get_fd(imsg);
140 TAILQ_INSERT_TAIL(&env->sockets, sock, entry);
142 for (i = 0; i < PRIV_FDS__MAX; i++)
143 sock->priv_fd[i] = -1;
145 for (i = 0; i < GOTWEB_PACK_NUM_TEMPFILES; i++)
146 sock->pack_fds[i] = -1;
148 /* log new socket info */
149 log_debug("%s: id=%d af_type=%s socket_path=%s",
150 __func__, sock->conf.id,
151 sock->conf.af_type == AF_UNIX ? "unix" :
152 (sock->conf.af_type == AF_INET ? "inet" :
153 (sock->conf.af_type == AF_INET6 ? "inet6" : "unknown")),
154 *sock->conf.unix_socket_name != '\0' ?
155 sock->conf.unix_socket_name : "none");
157 return 0;
160 int
161 config_setfd(struct gotwebd *env, struct socket *sock)
163 int i, j, ret, fd;
165 log_debug("%s: Allocating %d file descriptors",
166 __func__, PRIV_FDS__MAX + GOTWEB_PACK_NUM_TEMPFILES);
168 for (i = 0; i < PRIV_FDS__MAX + GOTWEB_PACK_NUM_TEMPFILES; i++) {
169 for (j = 0; j < env->nserver; ++j) {
170 fd = got_opentempfd();
171 if (fd == -1)
172 fatal("got_opentemp");
173 if (imsg_compose_event(&env->iev_server[j],
174 IMSG_CFG_FD, 0, -1, fd, &sock->conf.id,
175 sizeof(sock->conf.id)) == -1)
176 fatal("imsg_compose_event IMSG_CFG_FD");
178 do {
179 ret = imsg_flush(&env->iev_server[j].ibuf);
180 } while (ret == -1 && errno == EAGAIN);
181 if (ret == -1)
182 fatal("imsg_flush");
183 imsg_event_add(&env->iev_server[j]);
187 return 0;
190 int
191 config_getfd(struct gotwebd *env, struct imsg *imsg)
193 struct socket *sock;
194 uint8_t *p = imsg->data;
195 int sock_id, match = 0, i, j;
197 if (IMSG_DATA_SIZE(imsg) != sizeof(sock_id))
198 fatalx("%s: wrong size", __func__);
200 memcpy(&sock_id, p, sizeof(sock_id));
202 TAILQ_FOREACH(sock, &env->sockets, entry) {
203 const int nfds = (GOTWEB_PACK_NUM_TEMPFILES + PRIV_FDS__MAX);
204 for (i = 0; i < nfds; i++) {
205 if (i < PRIV_FDS__MAX && sock->priv_fd[i] == -1) {
206 sock->priv_fd[i] = imsg_get_fd(imsg);
207 log_debug("%s: assigning socket %d priv_fd %d",
208 __func__, sock_id, sock->priv_fd[i]);
209 match = 1;
210 break;
213 j = i - PRIV_FDS__MAX;
214 if (sock->pack_fds[j] == -1) {
215 sock->pack_fds[j] = imsg_get_fd(imsg);
216 log_debug("%s: assigning socket %d pack_fd %d",
217 __func__, sock_id, sock->pack_fds[j]);
218 match = 1;
219 break;
224 if (match)
225 return 0;
226 else
227 return 1;