1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
/*
* Copyright (c) 2020-2021 Tracey Emery <tracey@traceyemery.net>
* Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "got_compat.h"
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <event.h>
#include <fcntl.h>
#include <errno.h>
#include <imsg.h>
#include "got_opentemp.h"
#include "got_reference.h"
#include "got_object.h"
#include "gotwebd.h"
#include "log.h"
int
config_init(struct gotwebd *env)
{
int i;
strlcpy(env->httpd_chroot, D_HTTPD_CHROOT, sizeof(env->httpd_chroot));
env->prefork = GOTWEBD_NUMPROC;
TAILQ_INIT(&env->servers);
TAILQ_INIT(&env->sockets);
TAILQ_INIT(&env->addresses);
STAILQ_INIT(&env->access_rules);
for (i = 0; i < PRIV_FDS__MAX; i++)
env->priv_fd[i] = -1;
for (i = 0; i < GOTWEB_PACK_NUM_TEMPFILES; i++)
env->pack_fds[i] = -1;
return 0;
}
int
config_getcfg(struct gotwebd *env, struct imsg *imsg)
{
/* nothing to do but tell gotwebd configuration is done */
if (sockets_compose_main(env, GOTWEBD_IMSG_CFG_DONE, NULL, 0) == -1)
fatal("sockets_compose_main IMSG_CFG_DONE");
return 0;
}
int
config_getserver(struct gotwebd *env, struct imsg *imsg)
{
struct server *srv;
uint8_t *p = imsg->data;
srv = calloc(1, sizeof(*srv));
if (srv == NULL)
fatalx("%s: calloc", __func__);
if (IMSG_DATA_SIZE(imsg) != sizeof(*srv))
fatalx("%s: wrong size", __func__);
memcpy(srv, p, sizeof(*srv));
STAILQ_INIT(&srv->access_rules);
TAILQ_INIT(&srv->repos);
/* log server info */
log_debug("%s: server=%s", __func__, srv->name);
TAILQ_INSERT_TAIL(&env->servers, srv, entry);
return 0;
}
int
config_setsock(struct gotwebd *env, struct socket *sock, uid_t uid, gid_t gid)
{
/* open listening sockets */
if (sockets_privinit(env, sock, uid, gid) == -1)
return -1;
if (main_compose_sockets(env, GOTWEBD_IMSG_CFG_SOCK, sock->fd,
&sock->conf, sizeof(sock->conf)) == -1)
fatal("main_compose_sockets IMSG_CFG_SOCK");
if (main_compose_gotweb(env, GOTWEBD_IMSG_CFG_SOCK, sock->fd,
&sock->conf, sizeof(sock->conf)) == -1)
fatal("main_compose_gotweb GOTWEBD_IMSG_CFG_SOCK");
return 0;
}
int
config_getsock(struct gotwebd *env, struct imsg *imsg)
{
struct socket *sock = NULL;
struct socket_conf sock_conf;
uint8_t *p = imsg->data;
if (IMSG_DATA_SIZE(imsg) != sizeof(sock_conf))
fatalx("%s: wrong size", __func__);
memcpy(&sock_conf, p, sizeof(sock_conf));
if (IMSG_DATA_SIZE(imsg) != sizeof(sock_conf)) {
log_warnx("%s: imsg size error", __func__);
return 1;
}
/* create a new socket */
if ((sock = calloc(1, sizeof(*sock))) == NULL) {
return 1;
}
memcpy(&sock->conf, &sock_conf, sizeof(sock->conf));
sock->fd = imsg_get_fd(imsg);
TAILQ_INSERT_TAIL(&env->sockets, sock, entry);
/* log new socket info */
log_debug("%s: id=%d af_type=%s socket_path=%s",
__func__, sock->conf.id,
sock->conf.af_type == AF_UNIX ? "unix" :
(sock->conf.af_type == AF_INET ? "inet" :
(sock->conf.af_type == AF_INET6 ? "inet6" : "unknown")),
*sock->conf.unix_socket_name != '\0' ?
sock->conf.unix_socket_name : "none");
return 0;
}
int
config_setfd(struct gotwebd *env)
{
int i, j, fd;
log_info("%s: Allocating %d file descriptors",
__func__, PRIV_FDS__MAX + GOTWEB_PACK_NUM_TEMPFILES);
for (i = 0; i < PRIV_FDS__MAX + GOTWEB_PACK_NUM_TEMPFILES; i++) {
for (j = 0; j < env->prefork; j++) {
fd = got_opentempfd();
if (fd == -1)
fatal("got_opentemp");
if (imsg_compose_event(&env->iev_gotweb[j],
GOTWEBD_IMSG_CFG_FD, 0, -1, fd, NULL, 0) == -1)
fatal("imsg_compose_event GOTWEBD_IMSG_CFG_FD");
if (imsgbuf_flush(&env->iev_gotweb[j].ibuf) == -1)
fatal("imsgbuf_flush");
imsg_event_add(&env->iev_gotweb[j]);
}
}
return 0;
}
int
config_getfd(struct gotwebd *env, struct imsg *imsg)
{
int i;
if (imsg_get_len(imsg) != 0)
fatalx("%s: wrong size", __func__);
for (i = 0; i < nitems(env->priv_fd); ++i) {
if (env->priv_fd[i] == -1) {
env->priv_fd[i] = imsg_get_fd(imsg);
log_debug("%s: assigning priv_fd %d",
__func__, env->priv_fd[i]);
return 0;
}
}
for (i = 0; i < nitems(env->pack_fds); ++i) {
if (env->pack_fds[i] == -1) {
env->pack_fds[i] = imsg_get_fd(imsg);
log_debug("%s: assigning pack_fd %d",
__func__, env->pack_fds[i]);
return 0;
}
}
return 1;
}
void
config_set_access_rules(struct imsgev *iev,
struct gotwebd_access_rule_list *rules)
{
struct gotwebd_access_rule *rule;
STAILQ_FOREACH(rule, rules, entry) {
if (imsg_compose_event(iev, GOTWEBD_IMSG_CFG_ACCESS_RULE,
0, -1, -1, rule, sizeof(*rule)) == -1)
fatal("imsg_compose_event "
"GOTWEBD_IMSG_CFG_ACCESS_RULE");
}
}
void
config_get_access_rule(struct gotwebd_access_rule_list *rules,
struct imsg *imsg)
{
struct gotwebd_access_rule *rule;
size_t len;
rule = calloc(1, sizeof(*rule));
if (rule == NULL)
fatal("malloc");
if (imsg_get_data(imsg, rule, sizeof(*rule)))
fatalx("%s: invalid CFG_ACCESS_RULE message", __func__);
switch (rule->access) {
case GOTWEBD_ACCESS_DENIED:
case GOTWEBD_ACCESS_PERMITTED:
break;
default:
fatalx("%s: invalid CFG_ACCESS_RULE message", __func__);
}
len = strnlen(rule->identifier, sizeof(rule->identifier));
if (len == 0 || len >= sizeof(rule->identifier))
fatalx("%s: invalid CFG_ACCESS_RULE message", __func__);
STAILQ_INSERT_TAIL(rules, rule, entry);
}
void
config_free_access_rules(struct gotwebd_access_rule_list *rules)
{
struct gotwebd_access_rule *rule;
while (!STAILQ_EMPTY(rules)) {
rule = STAILQ_FIRST(rules);
STAILQ_REMOVE(rules, rule, gotwebd_access_rule, entry);
free(rule);
}
}
void
config_free_repos(struct gotwebd_repolist *repos)
{
struct gotwebd_repo *repo;
while (!TAILQ_EMPTY(repos)) {
repo = TAILQ_FIRST(repos);
TAILQ_REMOVE(repos, repo, entry);
config_free_access_rules(&repo->access_rules);
free(repo);
}
}
void
config_set_repository(struct imsgev *iev, struct gotwebd_repo *repo)
{
if (imsg_compose_event(iev,
GOTWEBD_IMSG_CFG_REPO, 0, -1, -1, repo, sizeof(*repo)) == -1)
fatal("imsg_compose_event GOTWEBD_IMSG_CFG_REPO");
}
void
config_get_repository(struct gotwebd_repolist *repos, struct imsg *imsg)
{
struct gotwebd_repo *repo;
size_t len;
repo = calloc(1, sizeof(*repo));
if (repo == NULL)
fatal("malloc");
if (imsg_get_data(imsg, repo, sizeof(*repo)))
fatalx("%s: invalid CFG_REPO message", __func__);
switch (repo->auth_config) {
case GOTWEBD_AUTH_DISABLED:
case GOTWEBD_AUTH_SECURE:
case GOTWEBD_AUTH_INSECURE:
break;
default:
fatalx("%s: invalid CFG_REPO message", __func__);
}
len = strnlen(repo->name, sizeof(repo->name));
if (len == 0 || len >= sizeof(repo->name))
fatalx("%s: invalid CFG_REPO message", __func__);
if (strchr(repo->name, '/') != NULL) {
fatalx("repository names must not contain slashes: %s",
repo->name);
}
if (strchr(repo->name, '\n') != NULL) {
fatalx("repository names must not contain linefeeds: %s",
repo->name);
}
STAILQ_INIT(&repo->access_rules);
TAILQ_INSERT_TAIL(repos, repo, entry);
}
|