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);
54 return 0;
55 }
57 int
58 config_getcfg(struct gotwebd *env, struct imsg *imsg)
59 {
60 /* nothing to do but tell gotwebd configuration is done */
61 if (sockets_compose_main(env, IMSG_CFG_DONE, NULL, 0) == -1)
62 fatal("sockets_compose_main IMSG_CFG_DONE");
63 return 0;
64 }
66 int
67 config_setserver(struct gotwebd *env, struct server *srv)
68 {
69 if (main_compose_sockets(env, IMSG_CFG_SRV, -1, srv, sizeof(*srv))
70 == -1)
71 fatal("main_compose_sockets IMSG_CFG_SRV");
72 return 0;
73 }
75 int
76 config_getserver(struct gotwebd *env, struct imsg *imsg)
77 {
78 struct server *srv;
79 uint8_t *p = imsg->data;
81 srv = calloc(1, sizeof(*srv));
82 if (srv == NULL)
83 fatalx("%s: calloc", __func__);
85 IMSG_SIZE_CHECK(imsg, srv);
87 memcpy(srv, p, sizeof(*srv));
88 srv->cached_repos = calloc(GOTWEBD_REPO_CACHESIZE,
89 sizeof(*srv->cached_repos));
90 if (srv->cached_repos == NULL)
91 fatal("%s: calloc", __func__);
92 srv->ncached_repos = 0;
94 /* log server info */
95 log_debug("%s: server=%s fcgi_socket=%s unix_socket=%s", __func__,
96 srv->name, srv->fcgi_socket ? "yes" : "no", srv->unix_socket ?
97 "yes" : "no");
99 TAILQ_INSERT_TAIL(&env->servers, srv, entry);
101 return 0;
104 int
105 config_setsock(struct gotwebd *env, struct socket *sock)
107 /* open listening sockets */
108 if (sockets_privinit(env, sock) == -1)
109 return -1;
111 if (main_compose_sockets(env, IMSG_CFG_SOCK, sock->fd,
112 &sock->conf, sizeof(sock->conf)) == -1)
113 fatal("main_compose_sockets IMSG_CFG_SOCK");
115 sock->fd = -1;
116 return 0;
119 int
120 config_getsock(struct gotwebd *env, struct imsg *imsg)
122 struct socket *sock = NULL;
123 struct socket_conf sock_conf;
124 uint8_t *p = imsg->data;
125 int i;
127 IMSG_SIZE_CHECK(imsg, &sock_conf);
128 memcpy(&sock_conf, p, sizeof(sock_conf));
130 if (IMSG_DATA_SIZE(imsg) != sizeof(sock_conf)) {
131 log_debug("%s: imsg size error", __func__);
132 return 1;
135 /* create a new socket */
136 if ((sock = calloc(1, sizeof(*sock))) == NULL) {
137 if (imsg->fd != -1)
138 close(imsg->fd);
139 return 1;
142 memcpy(&sock->conf, &sock_conf, sizeof(sock->conf));
143 sock->fd = imsg->fd;
145 TAILQ_INSERT_TAIL(&env->sockets, sock, entry);
147 for (i = 0; i < PRIV_FDS__MAX; i++)
148 sock->priv_fd[i] = -1;
150 for (i = 0; i < GOTWEB_PACK_NUM_TEMPFILES; i++)
151 sock->pack_fds[i] = -1;
153 /* log new socket info */
154 log_debug("%s: name=%s id=%d server=%s af_type=%s socket_path=%s",
155 __func__, sock->conf.name, sock->conf.id, sock->conf.srv_name,
156 sock->conf.af_type == AF_UNIX ? "unix" :
157 (sock->conf.af_type == AF_INET ? "inet" :
158 (sock->conf.af_type == AF_INET6 ? "inet6" : "unknown")),
159 *sock->conf.unix_socket_name != '\0' ?
160 sock->conf.unix_socket_name : "none");
162 return 0;
165 int
166 config_setfd(struct gotwebd *env, struct socket *sock)
168 int i, fd;
170 log_debug("%s: Allocating %d file descriptors",
171 __func__, PRIV_FDS__MAX + GOTWEB_PACK_NUM_TEMPFILES);
173 for (i = 0; i < PRIV_FDS__MAX + GOTWEB_PACK_NUM_TEMPFILES; i++) {
174 fd = got_opentempfd();
175 if (fd == -1)
176 fatal("got_opentemp");
177 if (main_compose_sockets(env, IMSG_CFG_FD, fd,
178 &sock->conf.id, sizeof(sock->conf.id)) == -1)
179 fatal("main_compose_sockets IMSG_CFG_FD");
182 return 0;
185 int
186 config_getfd(struct gotwebd *env, struct imsg *imsg)
188 struct socket *sock;
189 uint8_t *p = imsg->data;
190 int sock_id, match = 0, i;
192 IMSG_SIZE_CHECK(imsg, &sock_id);
193 memcpy(&sock_id, p, sizeof(sock_id));
195 TAILQ_FOREACH(sock, &env->sockets, entry) {
196 const int nfds = (GOTWEB_PACK_NUM_TEMPFILES + PRIV_FDS__MAX);
197 for (i = 0; i < nfds; i++) {
198 if (i < PRIV_FDS__MAX && sock->priv_fd[i] == -1) {
199 log_debug("%s: assigning socket %d priv_fd %d",
200 __func__, sock_id, imsg->fd);
201 sock->priv_fd[i] = imsg->fd;
202 match = 1;
203 break;
205 if (sock->pack_fds[i - PRIV_FDS__MAX] == -1) {
206 log_debug("%s: assigning socket %d pack_fd %d",
207 __func__, sock_id, imsg->fd);
208 sock->pack_fds[i - PRIV_FDS__MAX] = imsg->fd;
209 match = 1;
210 break;
215 if (match)
216 return 0;
217 else
218 return 1;