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"
43 #include "log.h"
45 int
46 config_init(struct gotwebd *env)
47 {
48 int i;
50 strlcpy(env->httpd_chroot, D_HTTPD_CHROOT, sizeof(env->httpd_chroot));
52 env->prefork_gotwebd = GOTWEBD_NUMPROC;
53 env->server_cnt = 0;
54 TAILQ_INIT(&env->servers);
55 TAILQ_INIT(&env->sockets);
56 TAILQ_INIT(&env->addresses);
58 for (i = 0; i < PRIV_FDS__MAX; i++)
59 env->priv_fd[i] = -1;
61 for (i = 0; i < GOTWEB_PACK_NUM_TEMPFILES; i++)
62 env->pack_fds[i] = -1;
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 (sockets_compose_main(env, IMSG_CFG_DONE, NULL, 0) == -1)
72 fatal("sockets_compose_main IMSG_CFG_DONE");
73 return 0;
74 }
76 int
77 config_setserver(struct gotwebd *env, struct server *srv)
78 {
79 if (main_compose_sockets(env, IMSG_CFG_SRV, -1, srv, sizeof(*srv))
80 == -1)
81 fatal("main_compose_sockets IMSG_CFG_SRV");
82 return 0;
83 }
85 int
86 config_getserver(struct gotwebd *env, struct imsg *imsg)
87 {
88 struct server *srv;
89 uint8_t *p = imsg->data;
91 srv = calloc(1, sizeof(*srv));
92 if (srv == NULL)
93 fatalx("%s: calloc", __func__);
95 if (IMSG_DATA_SIZE(imsg) != sizeof(*srv))
96 fatalx("%s: wrong size", __func__);
98 memcpy(srv, p, sizeof(*srv));
100 /* log server info */
101 log_debug("%s: server=%s", __func__, srv->name);
103 TAILQ_INSERT_TAIL(&env->servers, srv, entry);
105 return 0;
108 int
109 config_setsock(struct gotwebd *env, struct socket *sock)
111 /* open listening sockets */
112 if (sockets_privinit(env, sock) == -1)
113 return -1;
115 if (main_compose_sockets(env, IMSG_CFG_SOCK, sock->fd,
116 &sock->conf, sizeof(sock->conf)) == -1)
117 fatal("main_compose_sockets IMSG_CFG_SOCK");
119 sock->fd = -1;
120 return 0;
123 int
124 config_getsock(struct gotwebd *env, struct imsg *imsg)
126 struct socket *sock = NULL;
127 struct socket_conf sock_conf;
128 uint8_t *p = imsg->data;
130 if (IMSG_DATA_SIZE(imsg) != sizeof(sock_conf))
131 fatalx("%s: wrong size", __func__);
133 memcpy(&sock_conf, p, sizeof(sock_conf));
135 if (IMSG_DATA_SIZE(imsg) != sizeof(sock_conf)) {
136 log_debug("%s: imsg size error", __func__);
137 return 1;
140 /* create a new socket */
141 if ((sock = calloc(1, sizeof(*sock))) == NULL) {
142 return 1;
145 memcpy(&sock->conf, &sock_conf, sizeof(sock->conf));
146 sock->fd = imsg_get_fd(imsg);
148 TAILQ_INSERT_TAIL(&env->sockets, sock, entry);
150 /* log new socket info */
151 log_debug("%s: id=%d af_type=%s socket_path=%s",
152 __func__, sock->conf.id,
153 sock->conf.af_type == AF_UNIX ? "unix" :
154 (sock->conf.af_type == AF_INET ? "inet" :
155 (sock->conf.af_type == AF_INET6 ? "inet6" : "unknown")),
156 *sock->conf.unix_socket_name != '\0' ?
157 sock->conf.unix_socket_name : "none");
159 return 0;
162 int
163 config_setfd(struct gotwebd *env)
165 int i, j, ret, fd;
167 log_debug("%s: Allocating %d file descriptors",
168 __func__, PRIV_FDS__MAX + GOTWEB_PACK_NUM_TEMPFILES);
170 for (i = 0; i < PRIV_FDS__MAX + GOTWEB_PACK_NUM_TEMPFILES; i++) {
171 for (j = 0; j < env->nserver; ++j) {
172 fd = got_opentempfd();
173 if (fd == -1)
174 fatal("got_opentemp");
175 if (imsg_compose_event(&env->iev_server[j],
176 IMSG_CFG_FD, 0, -1, fd, NULL, 0) == -1)
177 fatal("imsg_compose_event IMSG_CFG_FD");
179 do {
180 ret = imsg_flush(&env->iev_server[j].ibuf);
181 } while (ret == -1 && errno == EAGAIN);
182 if (ret == -1)
183 fatal("imsg_flush");
184 imsg_event_add(&env->iev_server[j]);
188 return 0;
191 int
192 config_getfd(struct gotwebd *env, struct imsg *imsg)
194 int match = 0, i, j;
195 const int nfds = GOTWEB_PACK_NUM_TEMPFILES + PRIV_FDS__MAX;
197 if (imsg_get_len(imsg) != 0)
198 fatalx("%s: wrong size", __func__);
200 for (i = 0; i < nfds; i++) {
201 if (i < PRIV_FDS__MAX && env->priv_fd[i] == -1) {
202 env->priv_fd[i] = imsg_get_fd(imsg);
203 log_debug("%s: assigning priv_fd %d",
204 __func__, env->priv_fd[i]);
205 match = 1;
206 break;
209 j = i - PRIV_FDS__MAX;
210 if (env->pack_fds[j] == -1) {
211 env->pack_fds[j] = imsg_get_fd(imsg);
212 log_debug("%s: assigning pack_fd %d",
213 __func__, env->pack_fds[j]);
214 match = 1;
215 break;
219 if (match)
220 return 0;
221 else
222 return 1;