Commit Diff


commit - fb19edde339286346d944b66d9fbcd7cfc4686bcaa2db632c8ad1f5546b1e5aa
commit + c817a5d46ea7f05c5013b643e1e11055aafd553fe5dd61e596bf2c695ce69788
blob - 6dfdb6e42e1e4819085a54c4cd152c68cabde40960d839fa46204b097bbc8ca2
blob + 2a0dd85070dcd6bbc125b3abae2b495e39ac36ca6df91f6e88dc4f4699cb9956
--- gotwebd/Makefile	
+++ gotwebd/Makefile	
@@ -5,7 +5,7 @@
 .include "Makefile.inc"
 
 PROG =		gotwebd
-SRCS =		config.c sockets.c login.c gotwebd.c parse.y \
+SRCS =		config.c sockets.c auth.c login.c gotwebd.c parse.y \
 		fcgi.c gotweb.c got_operations.c tmpl.c pages.c
 SRCS +=		blame.c commit_graph.c delta.c diff.c \
 		diffreg.c error.c object.c object_cache.c \
blob - b3544cc94c55dc33841cd3cd84e715c4c17d9718b934d05b806b9d36b3d7676d
blob + ce2ebb7c64210c0d091f19e07ccc0dc85b7fbfc3b3b1c18a90069077c715de96
--- gotwebd/config.c	
+++ gotwebd/config.c	
@@ -56,6 +56,7 @@ config_init(struct gotwebd *env)
 	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;
@@ -89,6 +90,8 @@ config_getserver(struct gotwebd *env, struct imsg *ims
 		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);
@@ -209,3 +212,95 @@ config_getfd(struct gotwebd *env, struct imsg *imsg)
 
 	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_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);
+}
blob - /dev/null
blob + 1a96690071f790669544eec759d011be2b15faf2d59d7076f8f4cbb88dcf5a74 (mode 644)
--- /dev/null	
+++ gotwebd/auth.c	
@@ -0,0 +1,923 @@
+/*
+ * Copyright (c) 2025 Stefan Sperling <stsp@openbsd.org>
+ * Copyright (c) 2025 Omar Polo <op@openbsd.org>
+ * Copyright (c) 2015 Ted Unangst <tedu@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 <sys/queue.h>
+
+#include <errno.h>
+#include <event.h>
+#include <imsg.h>
+#include <pwd.h>
+#include <sha1.h>
+#include <sha2.h>
+#include <string.h>
+#include <signal.h>
+#include <stdio.h>
+#include <grp.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "got_error.h"
+#include "got_reference.h"
+#include "got_object.h"
+
+#include "gotwebd.h"
+#include "log.h"
+#include "tmpl.h"
+
+static char login_token_secret[32];
+static char auth_token_secret[32];
+
+static void
+auth_shutdown(void)
+{
+	struct gotwebd *env = gotwebd_env;
+
+	imsgbuf_clear(&env->iev_parent->ibuf);
+	imsgbuf_clear(&env->iev_sockets->ibuf);
+	imsgbuf_clear(&env->iev_gotweb->ibuf);
+
+	free(env->iev_parent);
+	free(env->iev_sockets);
+	free(env->iev_gotweb);
+	free(env);
+
+	exit(0);
+}
+
+static void
+auth_sighdlr(int sig, short event, void *arg)
+{
+	switch (sig) {
+	case SIGHUP:
+		log_info("%s: ignoring SIGHUP", __func__);
+		break;
+	case SIGPIPE:
+		log_info("%s: ignoring SIGPIPE", __func__);
+		break;
+	case SIGUSR1:
+		log_info("%s: ignoring SIGUSR1", __func__);
+		break;
+	case SIGCHLD:
+		break;
+	case SIGINT:
+	case SIGTERM:
+		auth_shutdown();
+		break;
+	default:
+		log_warn("unexpected signal %d", sig);
+		break;
+	}
+}
+
+static int
+parseuid(const char *s, uid_t *uid)
+{
+	struct passwd *pw;
+	const char *errstr;
+
+	if ((pw = getpwnam(s)) != NULL) {
+		*uid = pw->pw_uid;
+		if (*uid == UID_MAX)
+			return -1;
+		return 0;
+	}
+	*uid = strtonum(s, 0, UID_MAX - 1, &errstr);
+	if (errstr)
+		return -1;
+	return 0;
+}
+
+static int
+uidcheck(const char *s, uid_t desired)
+{
+	uid_t uid;
+
+	if (parseuid(s, &uid) != 0)
+		return -1;
+	if (uid != desired)
+		return -1;
+	return 0;
+}
+
+static int
+parsegid(const char *s, gid_t *gid)
+{
+	struct group *gr;
+	const char *errstr;
+
+	if ((gr = getgrnam(s)) != NULL) {
+		*gid = gr->gr_gid;
+		if (*gid == GID_MAX)
+			return -1;
+		return 0;
+	}
+	*gid = strtonum(s, 0, GID_MAX - 1, &errstr);
+	if (errstr)
+		return -1;
+	return 0;
+}
+
+static int
+match_identifier(const char *identifier, gid_t *groups, int ngroups,
+    uid_t euid, gid_t egid)
+{
+	int i;
+
+	if (identifier[0] == ':') {
+		gid_t rgid;
+		if (parsegid(identifier + 1, &rgid) == -1)
+			return 0;
+		if (rgid == egid)
+			return 1;
+		for (i = 0; i < ngroups; i++) {
+			if (rgid == groups[i])
+				break;
+		}
+		if (i == ngroups)
+			return 0;
+	} else if (uidcheck(identifier, euid) != 0)
+		return 0;
+
+	return 1;
+}
+
+static enum gotwebd_access
+auth_check(const char **identifier, uid_t uid,
+    struct gotwebd_access_rule_list *rules)
+{
+	struct gotwebd_access_rule *rule;
+	enum gotwebd_access access = GOTWEBD_ACCESS_NO_MATCH;
+	struct passwd *pw;
+	gid_t groups[NGROUPS_MAX];
+	int ngroups = NGROUPS_MAX;
+	gid_t gid;
+
+	if (identifier)
+		*identifier = NULL;
+
+	pw = getpwuid(uid);
+	if (pw == NULL)
+		return GOTWEBD_ACCESS_DENIED;
+
+	gid = pw->pw_gid;
+
+	if (getgrouplist(pw->pw_name, gid, groups, &ngroups) == -1)
+		log_warnx("group membership list truncated");
+
+	STAILQ_FOREACH(rule, rules, entry) {
+		if (!match_identifier(rule->identifier, groups, ngroups,
+		    uid, gid))
+			continue;
+
+		access = rule->access;
+		if (identifier)
+			*identifier = rule->identifier;
+	}
+
+	return access;
+}
+
+static void
+auth_launch(struct gotwebd *env)
+{
+	if (env->iev_sockets == NULL)
+		fatal("sockets process not connected");
+	if (env->iev_gotweb == NULL)
+		fatal("gotweb process not connected");
+
+	event_add(&env->iev_sockets->ev, NULL);
+	event_add(&env->iev_gotweb->ev, NULL);
+}
+
+static void
+render_error(struct request *c, const struct got_error *error)
+{
+	int status;
+
+	log_warnx("%s", error->msg);
+
+	c->t->error = error;
+
+	if (error->code == GOT_ERR_LOGIN_FAILED)
+		status = 401;
+	else
+		status = 400;
+
+	if (gotweb_reply(c, status, "text/html", NULL) == -1)
+		return;
+	gotweb_render_page(c->tp, gotweb_render_error);
+}
+
+static void
+abort_request(uint32_t request_id)
+{
+	if (imsg_compose_event(gotwebd_env->iev_sockets, GOTWEBD_IMSG_REQ_ABORT,
+	    GOTWEBD_PROC_GOTWEB, -1, -1, &request_id, sizeof(request_id)) == -1)
+		log_warn("imsg_compose_event");
+}
+
+static void
+forward_request(struct request *c)
+{
+	struct gotwebd *env = gotwebd_env;
+	const struct got_error *error;
+	int ret;
+
+	ret = imsg_compose_event(env->iev_gotweb, GOTWEBD_IMSG_REQ_PROCESS,
+	    GOTWEBD_PROC_AUTH, -1, c->fd, c, sizeof(*c));
+	if (ret == -1) {
+		error = got_error_set_errno(ret, "could not forward request "
+		    "to gotweb process");
+		render_error(c, error);
+		return;
+	}
+
+	c->fd = -1;
+}
+
+static void
+do_login(struct request *c)
+{
+	const struct got_error *error = NULL;
+	struct gotwebd *env = gotwebd_env;
+	uid_t uid;
+	struct server *srv;
+	char *hostname = NULL;
+	char *token = NULL;
+	const char *identifier = NULL;
+	const time_t validity = 24 * 60 * 60; /* 1 day */
+	struct gotweb_url url;
+
+	int r;
+
+	if (login_check_token(&uid, &hostname, c->fcgi_params.qs.login,
+	    login_token_secret, sizeof(login_token_secret), "login") == -1) {
+		error = got_error(GOT_ERR_LOGIN_FAILED);
+		goto err;
+	}
+
+	/*
+	 * The www user ID represents the case where no authentication
+	 * occurred. This user must not be allowed to log in.
+	 */
+	if (uid == env->www_uid) {
+		error = got_error(GOT_ERR_LOGIN_FAILED);
+		goto err;
+	}
+
+	c->client_uid = uid;
+	if (strcmp(hostname, c->fcgi_params.server_name) != 0) {
+		error = got_error_msg(GOT_ERR_LOGIN_FAILED,
+		    "wrong server name in login token");
+		goto err;
+	}
+
+	srv = gotweb_get_server(c->fcgi_params.server_name);
+	if (srv == NULL) {
+		error = got_error_msg(GOT_ERR_LOGIN_FAILED,
+		    "invalid server name for login");
+		goto err;
+	}
+
+	switch (auth_check(&identifier, uid, &srv->access_rules)) {
+	case GOTWEBD_ACCESS_PERMITTED:
+		goto logged_in;
+	case GOTWEBD_ACCESS_DENIED:
+		error = got_error_msg(GOT_ERR_LOGIN_FAILED,
+		    "permission denied");
+		goto err;
+	case GOTWEBD_ACCESS_NO_MATCH:
+		break;
+	default:
+		error = got_error_fmt(GOT_ERR_LOGIN_FAILED,
+		     "access check error for uid %u\n", uid);
+		goto err;
+	}
+
+	switch (auth_check(&identifier, uid, &env->access_rules)) {
+	case GOTWEBD_ACCESS_PERMITTED:
+		break;
+	case GOTWEBD_ACCESS_DENIED:
+	case GOTWEBD_ACCESS_NO_MATCH:
+		error = got_error_msg(GOT_ERR_LOGIN_FAILED,
+		    "permission denied");
+		goto err;
+	default:
+		error = got_error_fmt(GOT_ERR_LOGIN_FAILED,
+		     "access check error for uid %u\n", uid);
+		goto err;
+	}
+
+logged_in:
+	if (gotwebd_env->gotwebd_verbose > 0) {
+		log_info("successful login of uid %u as %s for server \"%s\"",
+		    uid, identifier, hostname);
+	}
+
+	/*
+	 * Generate a long-lasting token for the browser cookie.
+	 * TODO: make validity configurable?
+	 */
+	token = login_gen_token(uid, hostname, validity,
+	    auth_token_secret, sizeof(auth_token_secret),
+	    "authentication");
+	if (token == NULL) {
+		error = got_error_msg(GOT_ERR_LOGIN_FAILED,
+		    "failed to generate authentication cookie");
+		goto err;
+	}
+
+	r = tp_writef(c->tp, "Set-Cookie: gwdauth=%s;"
+	    " SameSite=Strict;%s Path=/; HttpOnly; Max-Age=%llu\r\n", token,
+	    env->auth_config == GOTWEBD_AUTH_SECURE ? " Secure;" : "",
+	    validity);
+	explicit_bzero(token, strlen(token));
+	free(token);
+	if (r == -1) {
+		error = got_error_from_errno("tp_writef");
+		goto err;
+	}
+
+	memset(&url, 0, sizeof(url));
+	url.action = INDEX;
+	gotweb_reply(c, 307, "text/html", &url);
+	return;
+
+err:
+	free(hostname);
+	hostname = NULL;
+
+	log_warnx("%s: %s", __func__, error->msg);
+	c->t->error = error;
+	if (error->code == GOT_ERR_LOGIN_FAILED) {
+		if (gotweb_reply(c, 401, "text/html", NULL) == -1)
+			return;
+		gotweb_render_page(c->tp, gotweb_render_unauthorized);
+	} else {
+		if (gotweb_reply(c, 400, "text/html", NULL) == -1)
+			return;
+		gotweb_render_page(c->tp, gotweb_render_error);
+	}
+}
+
+static void
+process_request(struct request *c)
+{
+	const struct got_error *error = NULL;
+	struct gotwebd *env = gotwebd_env;
+	uid_t uid;
+	struct server *srv;
+	struct gotwebd_repo *repo = NULL;
+	enum gotwebd_auth_config auth_config;
+	char *hostname = NULL;
+	const char *identifier = NULL;
+
+	srv = gotweb_get_server(c->fcgi_params.server_name);
+	if (srv == NULL) {
+		log_warnx("request for unknown server name");
+		error = got_error(GOT_ERR_BAD_QUERYSTRING);
+		goto done;
+	}
+
+	auth_config = srv->auth_config;
+	if (c->fcgi_params.qs.path[0] != '\0') {
+		repo = gotweb_get_repository(srv, c->fcgi_params.qs.path);
+		if (repo)
+			auth_config = repo->auth_config;
+	}
+
+	switch (auth_config) {
+	case GOTWEBD_AUTH_SECURE:
+	case GOTWEBD_AUTH_INSECURE:
+		break;
+	case GOTWEBD_AUTH_DISABLED:
+		forward_request(c);
+		return;
+	default:
+		fatalx("bad auth_config %d", env->auth_config);
+	}
+
+	if (login_check_token(&uid, &hostname, c->fcgi_params.auth_cookie,
+	    auth_token_secret, sizeof(auth_token_secret),
+	    "authentication") == -1) {
+		error = got_error(GOT_ERR_LOGIN_FAILED);
+		goto done;
+	}
+
+	/*
+	 * The www user ID represents the case where no authentication
+	 * occurred. This user is not allowed in authentication cookies.
+	 */
+	if (uid == env->www_uid) {
+		error = got_error(GOT_ERR_LOGIN_FAILED);
+		goto done;
+	}
+
+	c->client_uid = uid;
+	if (strcmp(hostname, c->fcgi_params.server_name) != 0) {
+		error = got_error_msg(GOT_ERR_LOGIN_FAILED,
+		    "bad server name in login token");
+		goto done;
+	}
+
+	if (repo) {
+		switch (auth_check(&identifier, uid, &repo->access_rules)) {
+		case GOTWEBD_ACCESS_DENIED:
+			error = got_error_msg(GOT_ERR_LOGIN_FAILED,
+			    "permission denied");
+			goto done;
+		case GOTWEBD_ACCESS_PERMITTED:
+			goto permitted;
+		case GOTWEBD_ACCESS_NO_MATCH:
+			break;
+		default:
+			error = got_error_fmt(GOT_ERR_LOGIN_FAILED,
+			    "access check error for uid %u\n", uid);
+			goto done;
+		}
+	}
+
+	switch (auth_check(&identifier, uid, &srv->access_rules)) {
+	case GOTWEBD_ACCESS_DENIED:
+		error = got_error_msg(GOT_ERR_LOGIN_FAILED,
+		    "permission denied");
+		goto done;
+	case GOTWEBD_ACCESS_PERMITTED:
+		goto permitted;
+	case GOTWEBD_ACCESS_NO_MATCH:
+		break;
+	default:
+		error = got_error_fmt(GOT_ERR_LOGIN_FAILED,
+		    "access check error for uid %u\n", uid);
+		goto done;
+	}
+
+	switch (auth_check(&identifier, uid, &env->access_rules)) {
+	case GOTWEBD_ACCESS_DENIED:
+	case GOTWEBD_ACCESS_NO_MATCH:
+		error = got_error_msg(GOT_ERR_LOGIN_FAILED,
+		    "permission denied");
+		goto done;
+	case GOTWEBD_ACCESS_PERMITTED:
+		goto permitted;
+	default:
+		error = got_error_fmt(GOT_ERR_LOGIN_FAILED,
+		    "access check error for uid %u\n", uid);
+		goto done;
+	}
+
+permitted:
+	if (strlcpy(c->access_identifier, identifier,
+	    sizeof(c->access_identifier)) >= sizeof(c->access_identifier)) {
+		error = got_error_msg(GOT_ERR_NO_SPACE,
+		    "identifier too long");
+		goto done;
+	}
+
+	if (gotwebd_env->gotwebd_verbose > 0) {
+		log_info("authenticated UID %u as %s for server \"%s\"",
+		    uid, identifier, hostname);
+	}
+done:
+	free(hostname);
+	if (error)
+		render_error(c, error);
+	else
+		forward_request(c);
+}
+
+static struct request *
+recv_request(struct imsg *imsg)
+{
+	const struct got_error *error = NULL;
+	struct request *c;
+	struct server *srv;
+	size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
+	int fd = -1;
+	uint8_t *outbuf = NULL;
+
+	if (datalen != sizeof(*c)) {
+		log_warnx("bad request size received over imsg");
+		return NULL;
+	}
+
+	fd = imsg_get_fd(imsg);
+	if (fd == -1) {
+		log_warnx("no client file descriptor");
+		return NULL;
+	}
+
+	c = calloc(1, sizeof(*c));
+	if (c == NULL) {
+		log_warn("calloc");
+		return NULL;
+	}
+
+	outbuf = calloc(1, GOTWEBD_CACHESIZE);
+	if (outbuf == NULL) {
+		log_warn("calloc");
+		free(c);
+		return NULL;
+	}
+
+	memcpy(c, imsg->data, sizeof(*c));
+
+	/* Non-NULL pointers, if any, are not from our address space. */
+	c->sock = NULL;
+	c->srv = NULL;
+	c->t = NULL;
+	c->tp = NULL;
+	c->buf = NULL;
+	c->outbuf = outbuf;
+
+	memset(&c->ev, 0, sizeof(c->ev));
+	memset(&c->tmo, 0, sizeof(c->tmo));
+
+	/* Use our own temporary file descriptors. */
+	memcpy(c->priv_fd, gotwebd_env->priv_fd, sizeof(c->priv_fd));
+
+	c->fd = fd;
+
+	c->client_uid = gotwebd_env->www_uid;
+
+	c->tp = template(c, fcgi_write, c->outbuf, GOTWEBD_CACHESIZE);
+	if (c->tp == NULL) {
+		log_warn("gotweb init template");
+		fcgi_cleanup_request(c);
+		return NULL;
+	}
+
+	/* init the transport */
+	error = gotweb_init_transport(&c->t);
+	if (error) {
+		log_warnx("gotweb init transport: %s", error->msg);
+		fcgi_cleanup_request(c);
+		return NULL;
+	}
+
+	/* querystring */
+	c->t->qs = &c->fcgi_params.qs;
+
+	/* get the gotwebd server */
+	srv = gotweb_get_server(c->fcgi_params.server_name);
+	if (srv == NULL) {
+		log_warnx("server '%s' not found", c->fcgi_params.server_name);
+		fcgi_cleanup_request(c);
+		return NULL;
+	}
+	c->srv = srv;
+
+	return c;
+}
+
+static void
+auth_dispatch_sockets(int fd, short event, void *arg)
+{
+	struct imsgev		*iev = arg;
+	struct imsgbuf		*ibuf;
+	struct imsg		 imsg;
+	ssize_t			 n;
+	struct request		*c;
+	int			 shut = 0;
+
+	ibuf = &iev->ibuf;
+
+	if (event & EV_READ) {
+		if ((n = imsgbuf_read(ibuf)) == -1)
+			fatal("imsgbuf_read error");
+		if (n == 0)	/* Connection closed */
+			shut = 1;
+	}
+	if (event & EV_WRITE) {
+		if (imsgbuf_write(ibuf) == -1)
+			fatal("imsgbuf_write");
+	}
+
+	for (;;) {
+		if ((n = imsg_get(ibuf, &imsg)) == -1)
+			fatal("imsg_get");
+		if (n == 0)	/* No more messages. */
+			break;
+
+		switch (imsg.hdr.type) {
+		case GOTWEBD_IMSG_REQ_PROCESS:
+			c = recv_request(&imsg);
+			if (c == NULL)
+				break;
+
+			if (c->fcgi_params.qs.login[0] != '\0')
+				do_login(c);
+			else
+				process_request(c);
+
+			/*
+			 * If we have not forwarded the request to the gotweb
+			 * process we must flush and clean up ourselves.
+			 */
+			if (c->fd != -1) {
+				uint32_t request_id = c->request_id;
+
+				if (template_flush(c->tp) == -1) {
+					log_warn("request %u flush",
+					    c->request_id);
+				}
+				fcgi_create_end_record(c);
+				fcgi_cleanup_request(c);
+				abort_request(request_id);
+			}
+			break;
+		default:
+			fatalx("%s: unknown imsg type %d", __func__,
+			    imsg.hdr.type);
+		}
+
+		imsg_free(&imsg);
+	}
+
+	if (!shut)
+		imsg_event_add(iev);
+	else {
+		/* This pipe is dead.  Remove its event handler */
+		event_del(&iev->ev);
+		event_loopexit(NULL);
+	}
+}
+
+static void
+recv_sockets_pipe(struct gotwebd *env, struct imsg *imsg)
+{
+	struct imsgev *iev;
+	int fd;
+
+	if (env->iev_sockets != NULL)
+		fatalx("sockets process already connected");
+
+	fd = imsg_get_fd(imsg);
+	if (fd == -1)
+		fatalx("invalid login pipe fd");
+
+	iev = calloc(1, sizeof(*iev));
+	if (iev == NULL)
+		fatal("calloc");
+
+	if (imsgbuf_init(&iev->ibuf, fd) == -1)
+		fatal("imsgbuf_init");
+	imsgbuf_allow_fdpass(&iev->ibuf);
+	imsgbuf_set_maxsize(&iev->ibuf, sizeof(struct request));
+
+	iev->handler = auth_dispatch_sockets;
+	iev->data = iev;
+	event_set(&iev->ev, fd, EV_READ, auth_dispatch_sockets, iev);
+	imsg_event_add(iev);
+
+	env->iev_sockets = iev;
+}
+
+static void
+auth_dispatch_gotweb(int fd, short event, void *arg)
+{
+	struct imsgev		*iev = arg;
+	struct imsgbuf		*ibuf;
+	struct imsg		 imsg;
+	ssize_t			 n;
+	int			 shut = 0;
+
+	ibuf = &iev->ibuf;
+
+	if (event & EV_READ) {
+		if ((n = imsgbuf_read(ibuf)) == -1)
+			fatal("imsgbuf_read error");
+		if (n == 0)	/* Connection closed */
+			shut = 1;
+	}
+	if (event & EV_WRITE) {
+		if (imsgbuf_write(ibuf) == -1)
+			fatal("imsgbuf_write");
+	}
+
+	for (;;) {
+		if ((n = imsg_get(ibuf, &imsg)) == -1)
+			fatal("imsg_get");
+		if (n == 0)	/* No more messages. */
+			break;
+
+		switch (imsg.hdr.type) {
+		case GOTWEBD_IMSG_REQ_ABORT: {
+			uint32_t request_id;
+
+			if (imsg_get_data(&imsg, &request_id,
+			    sizeof(request_id)) == -1)
+				fatalx("invalid REQ_ABORT msg");
+
+			abort_request(request_id);
+			break;
+		}
+		default:
+			fatalx("%s: unknown imsg type %d", __func__,
+			    imsg.hdr.type);
+		}
+
+		imsg_free(&imsg);
+	}
+
+	if (!shut)
+		imsg_event_add(iev);
+	else {
+		/* This pipe is dead.  Remove its event handler */
+		event_del(&iev->ev);
+		event_loopexit(NULL);
+	}
+}
+
+static void
+recv_gotweb_pipe(struct gotwebd *env, struct imsg *imsg)
+{
+	struct imsgev *iev;
+	int fd;
+
+	if (env->iev_gotweb != NULL)
+		fatalx("gotweb process already connected");
+
+	fd = imsg_get_fd(imsg);
+	if (fd == -1)
+		fatalx("invalid login pipe fd");
+
+	iev = calloc(1, sizeof(*iev));
+	if (iev == NULL)
+		fatal("calloc");
+
+	if (imsgbuf_init(&iev->ibuf, fd) == -1)
+		fatal("imsgbuf_init");
+	imsgbuf_allow_fdpass(&iev->ibuf);
+	imsgbuf_set_maxsize(&iev->ibuf, sizeof(struct request));
+
+	iev->handler = auth_dispatch_gotweb;
+	iev->data = iev;
+	event_set(&iev->ev, fd, EV_READ, auth_dispatch_gotweb, iev);
+	imsg_event_add(iev);
+
+	env->iev_gotweb = iev;
+}
+
+static void
+auth_dispatch_main(int fd, short event, void *arg)
+{
+	struct imsgev		*iev = arg;
+	struct imsgbuf		*ibuf;
+	struct imsg		 imsg;
+	struct gotwebd		*env = gotwebd_env;
+	struct server		*srv;
+	struct gotwebd_repo	*repo;
+	ssize_t			 n;
+	int			 shut = 0;
+
+	ibuf = &iev->ibuf;
+
+	if (event & EV_READ) {
+		if ((n = imsgbuf_read(ibuf)) == -1)
+			fatal("imsgbuf_read error");
+		if (n == 0)	/* Connection closed */
+			shut = 1;
+	}
+	if (event & EV_WRITE) {
+		if (imsgbuf_write(ibuf) == -1)
+			fatal("imsgbuf_write");
+	}
+
+	for (;;) {
+		if ((n = imsg_get(ibuf, &imsg)) == -1)
+			fatal("imsg_get");
+		if (n == 0)	/* No more messages. */
+			break;
+
+		switch (imsg.hdr.type) {
+		case GOTWEBD_IMSG_CFG_ACCESS_RULE:
+			if (TAILQ_EMPTY(&env->servers)) {
+				/* global access rule */
+				config_get_access_rule(&env->access_rules,
+				    &imsg);
+			} else {
+				srv = TAILQ_LAST(&env->servers, serverlist);
+				if (TAILQ_EMPTY(&srv->repos)) {
+					/* per-server access rule */
+					config_get_access_rule(
+					    &srv->access_rules, &imsg);
+				} else {
+					/* per-repository access rule */
+					repo = TAILQ_LAST(&srv->repos,
+					    gotwebd_repolist);
+					config_get_access_rule(
+					    &repo->access_rules, &imsg);
+				}
+			}
+			break;
+		case GOTWEBD_IMSG_CFG_SRV:
+			config_getserver(gotwebd_env, &imsg);
+			break;
+		case GOTWEBD_IMSG_CFG_REPO:
+			if (TAILQ_EMPTY(&env->servers))
+				fatalx("%s: unexpected CFG_REPO msg", __func__);
+			srv = TAILQ_LAST(&env->servers, serverlist);
+			config_get_repository(&srv->repos, &imsg);
+			break;
+		case GOTWEBD_IMSG_CTL_PIPE:
+			if (env->iev_sockets == NULL)
+				recv_sockets_pipe(env, &imsg);
+			else
+				recv_gotweb_pipe(env, &imsg);
+			break;
+		case GOTWEBD_IMSG_CTL_START:
+			auth_launch(env);
+			break;
+		case GOTWEBD_IMSG_LOGIN_SECRET:
+			if (imsg_get_data(&imsg, login_token_secret,
+			    sizeof(login_token_secret)) == -1)
+				fatalx("invalid LOGIN_SECRET msg");
+			break;
+		case GOTWEBD_IMSG_AUTH_SECRET:
+			if (imsg_get_data(&imsg, auth_token_secret,
+			    sizeof(auth_token_secret)) == -1)
+				fatalx("invalid AUTH_SECRET msg");
+			break;
+		case GOTWEBD_IMSG_AUTH_CONF:
+			if (imsg_get_data(&imsg, &env->auth_config,
+			    sizeof(env->auth_config)) == -1)
+				fatalx("invalid AUTH_CONF msg");
+			break;
+		case GOTWEBD_IMSG_WWW_UID:
+			if (imsg_get_data(&imsg, &env->www_uid,
+			    sizeof(env->www_uid)) == -1)
+				fatalx("invalid WWW_UID msg");
+			break;
+		default:
+			fatalx("%s: unknown imsg type %d", __func__,
+			    imsg.hdr.type);
+		}
+
+		imsg_free(&imsg);
+	}
+
+	if (!shut)
+		imsg_event_add(iev);
+	else {
+		/* This pipe is dead.  Remove its event handler */
+		event_del(&iev->ev);
+		event_loopexit(NULL);
+	}
+}
+
+void
+gotwebd_auth(struct gotwebd *env, int fd)
+{
+	struct event	 sighup, sigint, sigusr1, sigchld, sigterm;
+	struct event_base *evb;
+
+	evb = event_init();
+
+	if ((env->iev_parent = malloc(sizeof(*env->iev_parent))) == NULL)
+		fatal("malloc");
+	if (imsgbuf_init(&env->iev_parent->ibuf, fd) == -1)
+		fatal("imsgbuf_init");
+	imsgbuf_allow_fdpass(&env->iev_parent->ibuf);
+	env->iev_parent->handler = auth_dispatch_main;
+	env->iev_parent->data = env->iev_parent;
+	event_set(&env->iev_parent->ev, fd, EV_READ, auth_dispatch_main,
+	    env->iev_parent);
+	event_add(&env->iev_parent->ev, NULL);
+
+	signal(SIGPIPE, SIG_IGN);
+
+	signal_set(&sighup, SIGHUP, auth_sighdlr, env);
+	signal_add(&sighup, NULL);
+	signal_set(&sigint, SIGINT, auth_sighdlr, env);
+	signal_add(&sigint, NULL);
+	signal_set(&sigusr1, SIGUSR1, auth_sighdlr, env);
+	signal_add(&sigusr1, NULL);
+	signal_set(&sigchld, SIGCHLD, auth_sighdlr, env);
+	signal_add(&sigchld, NULL);
+	signal_set(&sigterm, SIGTERM, auth_sighdlr, env);
+	signal_add(&sigterm, NULL);
+
+#ifndef PROFILE
+	if (pledge("stdio getpw recvfd sendfd", NULL) == -1)
+		fatal("pledge");
+#endif
+	event_dispatch();
+	event_base_free(evb);
+	auth_shutdown();
+}
blob - dff93340975a0e8de1f9747339134df42f320da2523fd0a424792b825870fbb2
blob + 08894075768151b633f2b9d9ab75cc3fb355a58a5d591e6d7d61455f7f577201
--- gotwebd/fcgi.c	
+++ gotwebd/fcgi.c	
@@ -367,7 +367,15 @@ assign_querystring(struct querystring *qs, char *key, 
 				goto done;
 			}
 			break;
-		}
+		case LOGIN:
+			if (strlcpy(qs->login, value, sizeof(qs->login)) >=
+			    sizeof(qs->login)) {
+				error = got_error_msg(GOT_ERR_NO_SPACE,
+				    "login token too long");
+				goto done;
+			}
+			break;
+ 		}
 
 		/* entry found */
 		break;
@@ -426,6 +434,47 @@ err:
 	return error;
 }
 
+static void
+parse_cookie_hdr(struct gotwebd_fcgi_params *params, char *hdr, size_t len)
+{
+	size_t	 l;
+	char	*end;
+
+	memset(params->auth_cookie, 0, sizeof(params->auth_cookie));
+
+	while (len > 0) {
+		if (hdr[0] == ' ' || hdr[0] == '\t') {
+			hdr++;
+			len--;
+			continue;
+		}
+
+		/* looking at the start of name=val */
+
+		if ((end = memchr(hdr, ' ', len)) == NULL ||
+		    (end = memchr(hdr, '\t', len)) == NULL)
+			end = hdr + len;
+		l = end - hdr;
+
+		if (len > 8 && !strncmp(hdr, "gwdauth=", 8)) {
+			hdr += 8;
+			len -= 8;
+			l -= 8;
+
+			if (l < MAX_AUTH_COOKIE - 1) {
+				memcpy(params->auth_cookie, hdr, l);
+				params->auth_cookie[l] = '\0';
+			}
+
+			return;
+		}
+
+		/* skip to the next one */
+		hdr += l;
+		len -= l;
+	}
+}
+
 int
 fcgi_parse_params(uint8_t *buf, uint16_t n, struct gotwebd_fcgi_params *params)
 {
@@ -508,6 +557,10 @@ fcgi_parse_params(uint8_t *buf, uint16_t n, struct got
 		    strncmp(buf, "HTTPS", 5) == 0)
 			params->https = 1;
 
+		if (name_len == 11 &&
+		    strncmp(buf, "HTTP_COOKIE", 11) == 0)
+			parse_cookie_hdr(params, val, val_len);
+
 		buf += name_len + val_len;
 		n -= name_len - val_len;
 	}
blob - 5704c4f57617531be3fffd6c3368db367832433c6a06ef716caa80485e178dc1
blob + f1f912488272b75c448a28239fdefb14f513a076c1cfa7b5a539ada5e4b33230
--- gotwebd/gotweb.c	
+++ gotwebd/gotweb.c	
@@ -66,9 +66,7 @@ static const struct got_error *gotweb_get_clone_url(ch
 
 static void gotweb_free_repo_dir(struct repo_dir *);
 
-struct server *gotweb_get_server(const char *);
-
-static int
+int
 gotweb_reply(struct request *c, int status, const char *ctype,
     struct gotweb_url *location)
 {
@@ -128,7 +126,7 @@ cleanup_request(struct request *c)
 
 	fcgi_cleanup_request(c);
 
-	if (imsg_compose_event(gotwebd_env->iev_sockets, GOTWEBD_IMSG_REQ_ABORT,
+	if (imsg_compose_event(gotwebd_env->iev_auth, GOTWEBD_IMSG_REQ_ABORT,
 	    GOTWEBD_PROC_GOTWEB, -1, -1, &request_id, sizeof(request_id)) == -1)
 		log_warn("imsg_compose_event");
 }
@@ -541,6 +539,28 @@ gotweb_init_transport(struct transport **t)
 	return error;
 }
 
+struct gotwebd_repo *
+gotweb_get_repository(struct server *server, const char *name)
+{
+	struct gotwebd_repo *repo;
+
+	TAILQ_FOREACH(repo, &server->repos, entry) {
+		if (strncmp(repo->name, name, strlen(repo->name)) != 0)
+			continue;
+	
+		if (strlen(name) == strlen(repo->name))
+			return repo;
+
+		if (strlen(name) != strlen(repo->name) + 4)
+			continue;
+
+		if (strcmp(name + strlen(repo->name), ".git") == 0)
+			return repo;
+	}
+
+	return NULL;
+}
+
 void
 gotweb_free_repo_tag(struct repo_tag *rt)
 {
@@ -1171,8 +1191,8 @@ gotweb_shutdown(void)
 	imsgbuf_clear(&gotwebd_env->iev_parent->ibuf);
 	free(gotwebd_env->iev_parent);
 
-	imsgbuf_clear(&gotwebd_env->iev_sockets->ibuf);
-	free(gotwebd_env->iev_sockets);
+	imsgbuf_clear(&gotwebd_env->iev_auth->ibuf);
+	free(gotwebd_env->iev_auth);
 
 	while (!TAILQ_EMPTY(&gotwebd_env->servers)) {
 		struct server *srv;
@@ -1225,8 +1245,8 @@ gotweb_launch(struct gotwebd *env)
 	struct server *srv;
 	const struct got_error *error;
 
-	if (env->iev_sockets == NULL)
-		fatal("sockets process not connected");
+	if (env->iev_auth == NULL)
+		fatal("auth process not connected");
 
 #ifndef PROFILE
 	if (pledge("stdio rpath recvfd sendfd proc exec unveil", NULL) == -1)
@@ -1245,7 +1265,7 @@ gotweb_launch(struct gotwebd *env)
 	if (unveil(NULL, NULL) == -1)
 		fatal("unveil");
 
-	event_add(&env->iev_sockets->ev, NULL);
+	event_add(&env->iev_auth->ev, NULL);
 }
 
 static void
@@ -1314,14 +1334,17 @@ gotweb_dispatch_server(int fd, short event, void *arg)
 }
 
 static void
-recv_server_pipe(struct gotwebd *env, struct imsg *imsg)
+recv_auth_pipe(struct gotwebd *env, struct imsg *imsg)
 {
 	struct imsgev *iev;
 	int fd;
 
+	if (env->iev_auth != NULL)
+		fatalx("auth process already connected");
+
 	fd = imsg_get_fd(imsg);
 	if (fd == -1)
-		fatalx("invalid server pipe fd");
+		fatalx("invalid auth pipe fd");
 
 	iev = calloc(1, sizeof(*iev));
 	if (iev == NULL)
@@ -1335,7 +1358,7 @@ recv_server_pipe(struct gotwebd *env, struct imsg *ims
 	event_set(&iev->ev, fd, EV_READ, gotweb_dispatch_server, iev);
 	imsg_event_add(iev);
 
-	env->iev_sockets = iev;
+	env->iev_auth = iev;
 }
 
 static void
@@ -1345,6 +1368,8 @@ gotweb_dispatch_main(int fd, short event, void *arg)
 	struct imsgbuf		*ibuf;
 	struct imsg		 imsg;
 	struct gotwebd		*env = gotwebd_env;
+	struct server		*srv;
+	struct gotwebd_repo	*repo;
 	ssize_t			 n;
 	int			 shut = 0;
 
@@ -1368,9 +1393,35 @@ gotweb_dispatch_main(int fd, short event, void *arg)
 			break;
 
 		switch (imsg.hdr.type) {
+		case GOTWEBD_IMSG_CFG_ACCESS_RULE:
+			if (TAILQ_EMPTY(&env->servers)) {
+				/* global access rule */
+				config_get_access_rule(&env->access_rules,
+				    &imsg);
+			} else {
+				srv = TAILQ_LAST(&env->servers, serverlist);
+				if (TAILQ_EMPTY(&srv->repos)) {
+					/* per-server access rule */
+					config_get_access_rule(
+					    &srv->access_rules, &imsg);
+				} else {
+					/* per-repository access rule */
+					repo = TAILQ_LAST(&srv->repos,
+					    gotwebd_repolist);
+					config_get_access_rule(
+					    &repo->access_rules, &imsg);
+				}
+			}
+			break;
 		case GOTWEBD_IMSG_CFG_SRV:
 			config_getserver(env, &imsg);
 			break;
+		case GOTWEBD_IMSG_CFG_REPO:
+			if (TAILQ_EMPTY(&env->servers))
+				fatalx("%s: unexpected CFG_REPO msg", __func__);
+			srv = TAILQ_LAST(&env->servers, serverlist);
+			config_get_repository(&srv->repos, &imsg);
+			break;
 		case GOTWEBD_IMSG_CFG_FD:
 			config_getfd(env, &imsg);
 			break;
@@ -1381,8 +1432,18 @@ gotweb_dispatch_main(int fd, short event, void *arg)
 			config_getcfg(env, &imsg);
 			break;
 		case GOTWEBD_IMSG_CTL_PIPE:
-			recv_server_pipe(env, &imsg);
+			recv_auth_pipe(env, &imsg);
 			break;
+		case GOTWEBD_IMSG_AUTH_CONF:
+			if (imsg_get_data(&imsg, &env->auth_config,
+			    sizeof(env->auth_config)) == -1)
+				fatalx("%s: invalid AUTH_CONF msg", __func__);
+			break;
+		case GOTWEBD_IMSG_WWW_UID:
+			if (imsg_get_data(&imsg, &env->www_uid,
+			    sizeof(env->www_uid)) == -1)
+				fatalx("%s: invalid WWW_UID msg", __func__);
+			break;
 		case GOTWEBD_IMSG_CTL_START:
 			gotweb_launch(env);
 			break;
blob - ad4966dcfb2ecfb9075ee2961fc5a28f33c5558bfa8f43ad49da247a3ad50970
blob + d06c4527a939da0210fd550882db8a955470438a7db137eec20818fbf8e9b783
--- gotwebd/gotwebd.c	
+++ gotwebd/gotwebd.c	
@@ -58,6 +58,7 @@ void	 gotwebd_shutdown(void);
 void	 gotwebd_dispatch_server(int, short, void *);
 void	 gotwebd_dispatch_fcgi(int, short, void *);
 void	 gotwebd_dispatch_login(int, short, void *);
+void	 gotwebd_dispatch_auth(int, short, void *);
 void	 gotwebd_dispatch_gotweb(int, short, void *);
 
 struct gotwebd	*gotwebd_env;
@@ -151,6 +152,22 @@ main_compose_gotweb(struct gotwebd *env, uint32_t type
 }
 
 int
+main_compose_auth(struct gotwebd *env, uint32_t type, int fd,
+    const void *data, uint16_t len)
+{
+	size_t i;
+	int ret = 0;
+
+	for (i = 0; i < env->prefork; i++) {
+		ret = send_imsg(&env->iev_auth[i], type, fd, data, len);
+		if (ret)
+			break;
+	}
+
+	return ret;
+}
+
+int
 sockets_compose_main(struct gotwebd *env, uint32_t type, const void *d,
     uint16_t len)
 {
@@ -304,6 +321,56 @@ gotwebd_dispatch_fcgi(int fd, short event, void *arg)
 }
 
 void
+gotwebd_dispatch_auth(int fd, short event, void *arg)
+{
+	struct imsgev		*iev = arg;
+	struct imsgbuf		*ibuf;
+	struct imsg		 imsg;
+	struct gotwebd		*env = gotwebd_env;
+	ssize_t			 n;
+	int			 shut = 0;
+
+	ibuf = &iev->ibuf;
+
+	if (event & EV_READ) {
+		if ((n = imsgbuf_read(ibuf)) == -1)
+			fatal("imsgbuf_read error");
+		if (n == 0)	/* Connection closed */
+			shut = 1;
+	}
+	if (event & EV_WRITE) {
+		if (imsgbuf_write(ibuf) == -1)
+			fatal("imsgbuf_write");
+	}
+
+	for (;;) {
+		if ((n = imsg_get(ibuf, &imsg)) == -1)
+			fatal("imsg_get");
+		if (n == 0)	/* No more messages. */
+			break;
+
+		switch (imsg.hdr.type) {
+		case GOTWEBD_IMSG_CFG_DONE:
+			gotwebd_configure_done(env);
+			break;
+		default:
+			fatalx("%s: unknown imsg type %d", __func__,
+			    imsg.hdr.type);
+		}
+
+		imsg_free(&imsg);
+	}
+
+	if (!shut)
+		imsg_event_add(iev);
+	else {
+		/* This pipe is dead.  Remove its event handler */
+		event_del(&iev->ev);
+		event_loopexit(NULL);
+	}
+}
+
+void
 gotwebd_dispatch_gotweb(int fd, short event, void *arg)
 {
 	struct imsgev		*iev = arg;
@@ -425,6 +492,9 @@ spawn_process(struct gotwebd *env, const char *argv0, 
 	} else if (proc_type == GOTWEBD_PROC_FCGI) {
 		argv[argc++] = "-F";
 		argv[argc++] = username;
+	} else if (proc_type == GOTWEBD_PROC_AUTH) {
+		argv[argc++] = "-A";
+		argv[argc++] = username;
 	} else if (proc_type == GOTWEBD_PROC_GOTWEB) {
 		argv[argc++] = "-G";
 		argv[argc++] = username;
@@ -488,8 +558,12 @@ main(int argc, char **argv)
 		fatal("%s: calloc", __func__);
 	config_init(env);
 
-	while ((ch = getopt(argc, argv, "D:dG:f:F:L:nS:vW:")) != -1) {
+	while ((ch = getopt(argc, argv, "A:D:dG:f:F:L:nS:vW:")) != -1) {
 		switch (ch) {
+		case 'A':
+			proc_type = GOTWEBD_PROC_AUTH;
+			gotwebd_username = optarg;
+			break;
 		case 'D':
 			if (cmdline_symset(optarg) < 0)
 				log_warnx("could not parse macro definition %s",
@@ -565,6 +639,7 @@ main(int argc, char **argv)
 	pw = getpwnam(www_username);
 	if (pw == NULL)
 		fatalx("unknown user %s", www_username);
+	env->www_uid = pw->pw_uid;
 	www_gid = pw->pw_gid;
 
 	pw = getpwnam(gotwebd_username);
@@ -625,6 +700,17 @@ main(int argc, char **argv)
 
 		gotwebd_fcgi(env, GOTWEBD_SOCK_FILENO);
 		return 1;
+	case GOTWEBD_PROC_AUTH:
+		setproctitle("auth");
+		log_procinit("auth");
+
+		if (setgroups(1, &pw->pw_gid) == -1 ||
+		    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1 ||
+		    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
+			fatal("failed to drop privileges");
+
+		gotwebd_auth(env, GOTWEBD_SOCK_FILENO);
+		return 1;
 	case GOTWEBD_PROC_GOTWEB:
 		setproctitle("gotweb");
 		log_procinit("gotweb");
@@ -657,6 +743,10 @@ main(int argc, char **argv)
 	if (env->iev_fcgi == NULL)
 		fatal("calloc");
 
+	env->iev_auth = calloc(env->prefork, sizeof(*env->iev_auth));
+	if (env->iev_auth == NULL)
+		fatal("calloc");
+
 	env->iev_gotweb = calloc(env->prefork, sizeof(*env->iev_gotweb));
 	if (env->iev_gotweb == NULL)
 		fatal("calloc");
@@ -673,6 +763,9 @@ main(int argc, char **argv)
 	    gotwebd_dispatch_fcgi);
 
 	for (i = 0; i < env->prefork; ++i) {
+		spawn_process(env, argv0, &env->iev_auth[i],
+		    GOTWEBD_PROC_AUTH, gotwebd_username,
+		    gotwebd_dispatch_auth);
 		spawn_process(env, argv0, &env->iev_gotweb[i],
 		    GOTWEBD_PROC_GOTWEB, gotwebd_username,
 		    gotwebd_dispatch_gotweb);
@@ -732,7 +825,7 @@ main(int argc, char **argv)
 static void
 connect_children(struct gotwebd *env)
 {
-	struct imsgev *iev_gotweb;
+	struct imsgev *iev_gotweb, *iev_auth;
 	int pipe[2];
 	int i;
 
@@ -747,6 +840,7 @@ connect_children(struct gotwebd *env)
 
 	for (i = 0; i < env->prefork; i++) {
 		iev_gotweb = &env->iev_gotweb[i];
+		iev_auth = &env->iev_auth[i];
 
 		if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
 			fatal("socketpair");
@@ -755,6 +849,17 @@ connect_children(struct gotwebd *env)
 		    pipe[0], NULL, 0))
 			fatal("send_imsg");
 
+		if (send_imsg(iev_auth, GOTWEBD_IMSG_CTL_PIPE,
+		    pipe[1], NULL, 0))
+			fatal("send_imsg");
+
+		if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
+			fatal("socketpair");
+
+		if (send_imsg(iev_auth, GOTWEBD_IMSG_CTL_PIPE,
+		    pipe[0], NULL, 0))
+			fatal("send_imsg");
+
 		if (send_imsg(iev_gotweb, GOTWEBD_IMSG_CTL_PIPE,
 		    pipe[1], NULL, 0))
 			fatal("send_imsg");
@@ -766,19 +871,69 @@ gotwebd_configure(struct gotwebd *env, uid_t uid, gid_
 {
 	struct server *srv;
 	struct socket *sock;
-	char login_token_secret[32];
+	struct gotwebd_repo *repo;
+	char secret[32];
+	int i;
 
 	/* gotweb need to reload its config. */
 	env->gotweb_pending = env->prefork;
+	env->auth_pending = env->prefork;
 
+	/* send global access rules */
+	for (i = 0; i < env->prefork; ++i) {
+		config_set_access_rules(&env->iev_auth[i],
+		    &env->access_rules);
+		config_set_access_rules(&env->iev_gotweb[i],
+		    &env->access_rules);
+	}
+
 	/* send our gotweb servers */
 	TAILQ_FOREACH(srv, &env->servers, entry) {
 		if (main_compose_sockets(env, GOTWEBD_IMSG_CFG_SRV,
 		    -1, srv, sizeof(*srv)) == -1)
 			fatal("send_imsg GOTWEBD_IMSG_CFG_SRV");
+		if (main_compose_auth(env, GOTWEBD_IMSG_CFG_SRV,
+		    -1, srv, sizeof(*srv)) == -1)
+			fatal("main_compose_gotweb GOTWEBD_IMSG_CFG_SRV");
 		if (main_compose_gotweb(env, GOTWEBD_IMSG_CFG_SRV,
 		    -1, srv, sizeof(*srv)) == -1)
 			fatal("main_compose_gotweb GOTWEBD_IMSG_CFG_SRV");
+		if (main_compose_login(env, GOTWEBD_IMSG_CFG_SRV,
+		    -1, srv, sizeof(*srv)) == -1)
+			fatal("main_compose_gotweb GOTWEBD_IMSG_CFG_SRV");
+
+		/* send per-server access rules */
+		for (i = 0; i < env->prefork; ++i) {
+			config_set_access_rules(&env->iev_auth[i],
+			    &srv->access_rules);
+			config_set_access_rules(&env->iev_gotweb[i],
+			    &srv->access_rules);
+		}
+
+		/* send repositories and per-repository access rules */
+		TAILQ_FOREACH(repo, &srv->repos, entry) {
+			for (i = 0; i < env->prefork; i++) {
+				config_set_repository(&env->iev_auth[i],
+				    repo);
+				config_set_repository(&env->iev_gotweb[i],
+				    repo);
+
+				config_set_access_rules(&env->iev_auth[i],
+				    &repo->access_rules);
+				config_set_access_rules(&env->iev_gotweb[i],
+				    &repo->access_rules);
+			}
+		}
+
+		for (i = 0; i < env->prefork; i++) {
+			if (imsgbuf_flush(&env->iev_auth[i].ibuf) == -1)
+				fatal("imsgbuf_flush");
+			imsg_event_add(&env->iev_auth[i]);
+
+			if (imsgbuf_flush(&env->iev_gotweb[i].ibuf) == -1)
+				fatal("imsgbuf_flush");
+			imsg_event_add(&env->iev_gotweb[i]);
+		}
 	}
 
 	/* send our sockets */
@@ -794,20 +949,43 @@ gotwebd_configure(struct gotwebd *env, uid_t uid, gid_
 	/* Connect servers and gotwebs. */
 	connect_children(env);
 
-	arc4random_buf(login_token_secret, sizeof(login_token_secret));
+	if (main_compose_auth(env, GOTWEBD_IMSG_AUTH_CONF, -1,
+	    &env->auth_config, sizeof(env->auth_config)) == -1)
+		fatal("send_imsg GOTWEB_IMSG_AUTH_CONF");
+	if (main_compose_gotweb(env, GOTWEBD_IMSG_AUTH_CONF, -1,
+	    &env->auth_config, sizeof(env->auth_config)) == -1)
+		fatal("main_compose_gotweb GOTWEB_IMSG_AUTH_CONF");
 
+	if (main_compose_auth(env, GOTWEBD_IMSG_WWW_UID, -1,
+	    &env->www_uid, sizeof(env->www_uid)) == -1)
+		fatal("main_compose_auth GOTWEB_IMSG_WWW_UID");
+	if (main_compose_gotweb(env, GOTWEBD_IMSG_WWW_UID, -1,
+	    &env->www_uid, sizeof(env->www_uid)) == -1)
+		fatal("main_compose_gotweb GOTWEB_IMSG_WWW_UID");
+
+	arc4random_buf(secret, sizeof(secret));
+
 	if (main_compose_login(env, GOTWEBD_IMSG_LOGIN_SECRET, -1,
-	    login_token_secret, sizeof(login_token_secret)) == -1)
-		fatal("main_compose_gotweb GOTWEB_IMSG_LOGIN_SECRET");
+	    secret, sizeof(secret)) == -1)
+		fatal("main_compose_login GOTWEB_IMSG_LOGIN_SECRET");
+	if (main_compose_auth(env, GOTWEBD_IMSG_LOGIN_SECRET, -1,
+	    secret, sizeof(secret)) == -1)
+		fatal("main_compose_auth GOTWEB_IMSG_LOGIN_SECRET");
 
-	explicit_bzero(login_token_secret, sizeof(login_token_secret));
+	arc4random_buf(secret, sizeof(secret));
 
+	if (main_compose_auth(env, GOTWEBD_IMSG_AUTH_SECRET, -1,
+	    secret, sizeof(secret)) == -1)
+		fatal("main_compose_auth GOTWEB_IMSG_AUTH_SECRET");
+
+	explicit_bzero(secret, sizeof(secret));
+
 	if (login_privinit(env, uid, gid) == -1)
 		fatalx("cannot open authentication socket");
 
 	if (main_compose_login(env, GOTWEBD_IMSG_CFG_SOCK, env->login_sock->fd,
 	    NULL, 0) == -1)
-		fatal("main_compose_auth GOTWEBD_IMSG_CFG_SOCK");
+		fatal("main_compose_login GOTWEBD_IMSG_CFG_SOCK");
 
 	if (main_compose_sockets(env, GOTWEBD_IMSG_CFG_DONE, -1,
 	    NULL, 0) == -1)
@@ -828,9 +1006,17 @@ gotwebd_configure_done(struct gotwebd *env)
 		if (env->gotweb_pending == 0 &&
 		    main_compose_gotweb(env, GOTWEBD_IMSG_CTL_START,
 		        -1, NULL, 0) == -1)
-			fatal("main_compose_gotewb GOTWEBD_IMSG_CTL_START");
+			fatal("main_compose_gotweb GOTWEBD_IMSG_CTL_START");
 	}
 
+	if (env->auth_pending > 0) {
+		env->auth_pending--;
+		if (env->auth_pending == 0 &&
+		    main_compose_auth(env, GOTWEBD_IMSG_CTL_START,
+		        -1, NULL, 0) == -1)
+			fatal("main_compose_auth GOTWEBD_IMSG_CTL_START");
+	}
+
 	if (main_compose_login(env, GOTWEBD_IMSG_CTL_START,
 	    -1, NULL, 0) == -1)
 		fatal("send_imsg GOTWEBD_IMSG_CTL_START");
@@ -862,11 +1048,17 @@ gotwebd_shutdown(void)
 	free(env->iev_fcgi);
 
 	for (i = 0; i < env->prefork; ++i) {
+		event_del(&env->iev_auth[i].ev);
+		imsgbuf_clear(&env->iev_auth[i].ibuf);
+		close(env->iev_auth[i].ibuf.fd);
+		env->iev_auth[i].ibuf.fd = -1;
+
 		event_del(&env->iev_gotweb[i].ev);
 		imsgbuf_clear(&env->iev_gotweb[i].ibuf);
 		close(env->iev_gotweb[i].ibuf.fd);
 		env->iev_gotweb[i].ibuf.fd = -1;
 	}
+	free(env->iev_auth);
 	free(env->iev_gotweb);
 
 	free(env->login_sock);
blob - f17dcb7d1cb6eb55a8bb76ca55872db336f82fc4d3b647054bfe7d88031929cb
blob + 7a4bcc64fee1ddcbd24784d4d1de666171a44ca835a74efadbfe221200bb7d64
--- gotwebd/gotwebd.h	
+++ gotwebd/gotwebd.h	
@@ -131,6 +131,7 @@ enum gotwebd_proc_type {
 	GOTWEBD_PROC_SOCKETS,
 	GOTWEBD_PROC_FCGI,
 	GOTWEBD_PROC_LOGIN,
+	GOTWEBD_PROC_AUTH,
 	GOTWEBD_PROC_GOTWEB,
 };
 
@@ -138,12 +139,17 @@ enum imsg_type {
 	GOTWEBD_IMSG_CFG_SRV,
 	GOTWEBD_IMSG_CFG_SOCK,
 	GOTWEBD_IMSG_CFG_FD,
+	GOTWEBD_IMSG_CFG_ACCESS_RULE,
+	GOTWEBD_IMSG_CFG_REPO,
 	GOTWEBD_IMSG_CFG_DONE,
 	GOTWEBD_IMSG_CTL_PIPE,
 	GOTWEBD_IMSG_CTL_START,
 	GOTWEBD_IMSG_LOGIN_SECRET,
+	GOTWEBD_IMSG_AUTH_SECRET,
+	GOTWEBD_IMSG_AUTH_CONF,
 	GOTWEBD_IMSG_FCGI_PARSE_PARAMS,
 	GOTWEBD_IMSG_FCGI_PARAMS,
+	GOTWEBD_IMSG_WWW_UID,
 	GOTWEBD_IMSG_REQ_ABORT,
 	GOTWEBD_IMSG_REQ_PROCESS,
 };
@@ -358,6 +364,7 @@ enum gotwebd_auth_config {
 };
 
 enum gotwebd_access {
+	GOTWEBD_ACCESS_NO_MATCH = -2,
 	GOTWEBD_ACCESS_DENIED = -1,
 	GOTWEBD_ACCESS_PERMITTED = 1
 };
@@ -477,6 +484,7 @@ struct gotwebd {
 	int		 *worker_load;
 
 	char		 httpd_chroot[PATH_MAX];
+	uid_t		 www_uid;
 };
 
 /*
@@ -529,6 +537,8 @@ int	 main_compose_login(struct gotwebd *, uint32_t, in
 	    const void *, uint16_t);
 int	 sockets_compose_main(struct gotwebd *, uint32_t,
 	    const void *, uint16_t);
+int	 main_compose_auth(struct gotwebd *, uint32_t, int,
+	    const void *, uint16_t);
 int	 main_compose_gotweb(struct gotwebd *, uint32_t, int,
 	    const void *, uint16_t);
 
@@ -541,12 +551,21 @@ int sockets_privinit(struct gotwebd *, struct socket *
 void sockets_rlimit(int);
 
 /* login.c */
-char *login_gen_token(uint64_t, const char *, time_t, const char *, size_t);
-int login_check_token(uid_t *, char **, const char *, const char *, size_t);
+char *login_gen_token(uint64_t, const char *, time_t, const char *, size_t,
+    const char *);
+int login_check_token(uid_t *, char **, const char *, const char *, size_t,
+    const char *);
 int login_privinit(struct gotwebd *, uid_t, gid_t);
 void gotwebd_login(struct gotwebd *, int);
 
+/* auth.c */
+void gotwebd_auth(struct gotwebd *, int);
+
 /* gotweb.c */
+struct server *gotweb_get_server(const char *);
+struct gotwebd_repo * gotweb_get_repository(struct server *, const char *);
+int gotweb_reply(struct request *c, int status, const char *ctype,
+    struct gotweb_url *);
 void gotweb_index_navs(struct request *, struct gotweb_url *, int *,
     struct gotweb_url *, int *);
 int gotweb_render_age(struct template *, time_t);
@@ -578,6 +597,7 @@ int	gotweb_render_summary(struct template *);
 int	gotweb_render_blame(struct template *);
 int	gotweb_render_patch(struct template *);
 int	gotweb_render_rss(struct template *);
+int	gotweb_render_unauthorized(struct template *);
 
 /* parse.y */
 struct gotwebd_repo * gotwebd_new_repo(const char *);
@@ -617,4 +637,9 @@ int config_getsock(struct gotwebd *, struct imsg *);
 int config_setfd(struct gotwebd *);
 int config_getfd(struct gotwebd *, struct imsg *);
 int config_getcfg(struct gotwebd *, struct imsg *);
+void config_set_access_rules(struct imsgev *,
+    struct gotwebd_access_rule_list *);
+void config_get_access_rule(struct gotwebd_access_rule_list *, struct imsg *);
+void config_set_repository(struct imsgev *, struct gotwebd_repo *);
+void config_get_repository(struct gotwebd_repolist *, struct imsg *);
 int config_init(struct gotwebd *);
blob - 2a5a86845da6bc91a3b1f0247d78a170956a2733213d993dba6b5b9b71b4ad00
blob + 967d00206858dbdc190384cd24bb3f5e683b5595a379018c52ce8a9642979a23
--- gotwebd/login.c	
+++ gotwebd/login.c	
@@ -68,7 +68,8 @@ static char login_token_secret[32];
 /* checks whether the token's signature matches, i.e. if it looks good. */
 int
 login_check_token(uid_t *euid, char **hostname,
-    const char *token, const char *secret, size_t secret_len)
+    const char *token, const char *secret, size_t secret_len,
+    const char *purpose)
 {
 	time_t	 now;
 	uint64_t issued, expire, uid;
@@ -83,7 +84,7 @@ login_check_token(uid_t *euid, char **hostname,
 	 * a copy of the same secret.
 	 */
 	if (secret_len != sizeof(login_token_secret))
-		fatalx("login token secret length mismatch");
+		fatalx("%s token secret length mismatch", purpose);
 
 	/* xxx check for overflow */
 	len = (strlen(token) / 4) * 3;
@@ -103,13 +104,13 @@ login_check_token(uid_t *euid, char **hostname,
 		len--;
 
 	if (len < 28 + 32) { /* min length assuming empty hostname */
-		log_warnx("login token too short: %d", len);
+		log_warnx("%s token too short: %d", purpose, len);
 		free(data);
 		return -1;
 	}
 
 	if (memcmp(data, "v1", 3) != 0) {
-		log_warnx("unknown login token format version");
+		log_warnx("unknown %s token format version", purpose);
 		free(data);
 		return -1;
 	}
@@ -147,7 +148,7 @@ login_check_token(uid_t *euid, char **hostname,
 
 	now = time(NULL);
 	if (expire < now) {
-		log_warnx("uid %llu: login token has expired\n", uid);
+		log_warnx("uid %llu: %s token has expired\n", uid, purpose);
 		free(data);
 		return -1;
 	}
@@ -180,7 +181,7 @@ login_check_token(uid_t *euid, char **hostname,
 
 char *
 login_gen_token(uint64_t uid, const char *hostname, time_t validity,
-    const char *secret, size_t secret_len)
+    const char *secret, size_t secret_len, const char *purpose)
 {
 	BIO		*bmem, *b64;
 	BUF_MEM		*bufm;
@@ -198,7 +199,7 @@ login_gen_token(uint64_t uid, const char *hostname, ti
 	 * a copy of the same secret.
 	 */
 	if (secret_len != sizeof(login_token_secret))
-		fatalx("login token secret length mismatch");
+		fatalx("%s token secret length mismatch", purpose);
 
 	now = time(NULL);
 	issued = (uint64_t)now;
@@ -239,7 +240,7 @@ login_gen_token(uint64_t uid, const char *hostname, ti
 
 	/* Base64 encoder expects a length divisible by 4. */
 	if ((siz % 4) != 0)
-		fatalx("generated token with bad size %zu", siz);
+		fatalx("generated %s token with bad size %zu", purpose, siz);
 
 	if (siz > INT_MAX) {
 		/*
@@ -287,8 +288,10 @@ login_gen_token(uint64_t uid, const char *hostname, ti
 	free(tok);
 	BIO_free_all(b64);
 
-	if (login_check_token(NULL, NULL, enc, secret, secret_len) == -1)
-		fatalx("generated a token that won't pass validation!");
+	if (login_check_token(NULL, NULL, enc, secret, secret_len,
+	    purpose) == -1)
+		fatalx("generated %s token that won't pass validation",
+		    purpose);
 
 	return enc;
 }
@@ -479,10 +482,22 @@ client_read(struct bufferevent *bev, void *d)
 		cmd += 5;
 		cmd += strspn(cmd, " \t");
 		hostname = cmd;
+		if (hostname[0] == '\0') {
+			struct server *srv;
 
+			/*
+			 * In a multi-server setup we do not want to leak our
+			 * first server's hostname to random people. But if
+			 * we only have a single server, we'll expose it.
+			 */
+			srv = TAILQ_FIRST(&gotwebd_env->servers);
+			if (TAILQ_NEXT(srv, entry) == NULL)
+				hostname = srv->name;
+		}
+
 		code = login_gen_token(client->euid, hostname,
 		    5 * 60 /* 5 minutes */,
-		    login_token_secret, sizeof(login_token_secret));
+		    login_token_secret, sizeof(login_token_secret), "login");
 		if (code == NULL) {
 			log_warn("%s: login_gen_token failed", __func__);
 			client_err(bev, EVBUFFER_READ, client);
@@ -696,6 +711,9 @@ login_dispatch_main(int fd, short event, void *arg)
 			break;
 
 		switch (imsg.hdr.type) {
+		case GOTWEBD_IMSG_CFG_SRV:
+			config_getserver(env, &imsg);
+			break;
 		case GOTWEBD_IMSG_CFG_SOCK:
 			get_login_sock(env, &imsg);
 			break;
@@ -705,7 +723,7 @@ login_dispatch_main(int fd, short event, void *arg)
 		case GOTWEBD_IMSG_LOGIN_SECRET:
 			if (imsg_get_data(&imsg, login_token_secret,
 			    sizeof(login_token_secret)) == -1)
-				fatalx("%s: invalid LOGIN_SECRET msg", __func__);
+				fatalx("invalid LOGIN_SECRET msg");
 			break;
 		default:
 			fatalx("%s: unknown imsg type %d", __func__,
blob - e1910c93e4f1082d826ec918cf5c38463c4d84be5a0dcdba0b4e0a43afd7190f
blob + 2376376dbf630ecc7c9fe9db2421ec294d46832d22c8ee5a8470214d29c42cf0
--- gotwebd/pages.tmpl	
+++ gotwebd/pages.tmpl	
@@ -1370,3 +1370,7 @@ date: {{ datebuf }} {{ " UTC" }} {{ "\n" }}
   {{ mail }} {{" "}} ({{ author }})
 </author>
 {{ end }}
+
+{{ define gotweb_render_unauthorized(struct template *tp) }}
+<p>Wrong or missing authentication code</p>
+{{ end }}
blob - f963b4bdfa4b36397845657f6ac88a1a6eba3cf959c8cbda101a42c960fe90f7
blob + bca0b0d5f121bb68c3c17f0003475c64634b2079532b088bc54c0747770d88f3
--- gotwebd/sockets.c	
+++ gotwebd/sockets.c	
@@ -233,10 +233,10 @@ sockets(struct gotwebd *env, int fd)
 
 	if (env->prefork <= 0)
 		fatalx("invalid prefork count: %d", env->prefork);
-	env->iev_gotweb = calloc(env->prefork, sizeof(*env->iev_gotweb));
-	if (env->iev_gotweb == NULL)
+	env->iev_auth = calloc(env->prefork, sizeof(*env->iev_auth));
+	if (env->iev_auth == NULL)
 		fatal("calloc");
-	env->gotweb_pending = env->prefork;
+	env->auth_pending = env->prefork;
 
 	signal(SIGPIPE, SIG_IGN);
 
@@ -331,8 +331,8 @@ sockets_launch(struct gotwebd *env)
 
 	if (env->iev_fcgi == NULL)
 		fatalx("fcgi process not connected");
-	if (env->gotweb_pending != 0)
-		fatal("gotweb process not connected");
+	if (env->auth_pending != 0)
+		fatal("auth process not connected");
 
 	TAILQ_FOREACH(sock, &gotwebd_env->sockets, entry) {
 		if (sock->conf.af_type == AF_UNIX) {
@@ -375,7 +375,7 @@ sockets_launch(struct gotwebd *env)
 #endif
 	event_add(&env->iev_fcgi->ev, NULL);
 	for (i = 0; i < env->prefork; i++)
-		event_add(&env->iev_gotweb[i].ev, NULL);
+		event_add(&env->iev_auth[i].ev, NULL);
 }
 
 static void
@@ -397,7 +397,7 @@ abort_request(struct imsg *imsg)
 }
 
 static void
-server_dispatch_gotweb(int fd, short event, void *arg)
+server_dispatch_auth(int fd, short event, void *arg)
 {
 	struct imsgev		*iev = arg;
 	struct imsgbuf		*ibuf;
@@ -446,32 +446,32 @@ server_dispatch_gotweb(int fd, short event, void *arg)
 }
 
 static void
-recv_gotweb_pipe(struct gotwebd *env, struct imsg *imsg)
+recv_auth_pipe(struct gotwebd *env, struct imsg *imsg)
 {
 	struct imsgev *iev;
 	int fd;
 
-	if (env->gotweb_pending <= 0) {
-		log_warn("gotweb pipe already received");
+	if (env->auth_pending <= 0) {
+		log_warn("all auth pipes already received");
 		return;
 	}
 
 	fd = imsg_get_fd(imsg);
 	if (fd == -1)
-		fatalx("invalid gotweb pipe fd");
+		fatalx("invalid auth pipe fd");
 
-	iev = &env->iev_gotweb[env->gotweb_pending - 1];
+	iev = &env->iev_auth[env->auth_pending - 1];
 	if (imsgbuf_init(&iev->ibuf, fd) == -1)
 		fatal("imsgbuf_init");
 	imsgbuf_allow_fdpass(&iev->ibuf);
 	imsgbuf_set_maxsize(&iev->ibuf, sizeof(struct request));
 
-	iev->handler = server_dispatch_gotweb;
+	iev->handler = server_dispatch_auth;
 	iev->data = iev;
-	event_set(&iev->ev, fd, EV_READ, server_dispatch_gotweb, iev);
+	event_set(&iev->ev, fd, EV_READ, server_dispatch_auth, iev);
 	imsg_event_add(iev);
 
-	env->gotweb_pending--;
+	env->auth_pending--;
 }
 
 static struct imsgev *
@@ -494,15 +494,16 @@ select_worker(struct request *c)
 	    c->request_id, least_busy_worker_idx);
 
 	c->worker_idx = least_busy_worker_idx;
-	return &env->iev_gotweb[least_busy_worker_idx];
+	return &env->iev_auth[least_busy_worker_idx];
 }
 
 static int
 process_request(struct request *c)
 {
 	struct gotwebd *env = gotwebd_env;
-	struct querystring *qs = &c->fcgi_params.qs;
-	struct imsgev *iev_gotweb;
+	struct gotwebd_fcgi_params *params = &c->fcgi_params;
+	struct querystring *qs = &params->qs;
+	struct imsgev *iev_auth;
 	int ret, i;
 	struct request ic;
 
@@ -518,7 +519,17 @@ process_request(struct request *c)
 			return -1;
 		}
 	}
+	if (params->server_name[0] == '\0') {
+		struct server *srv = TAILQ_FIRST(&env->servers);
 
+		if (strlcpy(params->server_name, srv->name,
+		    sizeof(params->server_name)) >=
+		    sizeof(params->server_name)) {
+			log_warnx("server name buffer too small");
+			return -1;
+		}
+	}
+
 	memcpy(&ic, c, sizeof(ic));
 
 	/* Don't leak pointers from our address space to another process. */
@@ -534,8 +545,8 @@ process_request(struct request *c)
 		ic.priv_fd[i] = -1;
 	ic.fd = -1;
 
-	iev_gotweb = select_worker(c);
-	ret = imsg_compose_event(iev_gotweb, GOTWEBD_IMSG_REQ_PROCESS,
+	iev_auth = select_worker(c);
+	ret = imsg_compose_event(iev_auth, GOTWEBD_IMSG_REQ_PROCESS,
 	    GOTWEBD_PROC_SOCKETS, -1, c->fd, &ic, sizeof(ic));
 	if (ret == -1) {
 		log_warn("imsg_compose_event");
@@ -613,6 +624,13 @@ recv_parsed_params(struct imsg *imsg)
 		goto fail;
 	}
 
+	if (params.qs.login[0] != '\0' &&
+	    strlcpy(p->qs.login, params.qs.login,
+	    sizeof(p->qs.login)) >= sizeof(p->qs.login)) {
+		log_warnx("login token too long");
+		goto fail;
+	}
+
 	if (params.document_uri[0] != '\0' &&
 	    strlcpy(p->document_uri, params.document_uri,
 	    sizeof(p->document_uri)) >= sizeof(p->document_uri)) {
@@ -627,6 +645,13 @@ recv_parsed_params(struct imsg *imsg)
 		goto fail;
 	}
 
+	if (params.auth_cookie[0] != '\0' &&
+	    strlcpy(p->auth_cookie, params.auth_cookie,
+	    sizeof(p->auth_cookie)) >= sizeof(p->auth_cookie)) {
+		log_warnx("auth cookie too long");
+		goto fail;
+	}
+
 	if (params.https && !p->https)
 		p->https = 1;
 
@@ -770,7 +795,7 @@ sockets_dispatch_main(int fd, short event, void *arg)
 			if (env->iev_fcgi == NULL)
 				recv_fcgi_pipe(env, &imsg);
 			else
-				recv_gotweb_pipe(env, &imsg);
+				recv_auth_pipe(env, &imsg);
 			break;
 		case GOTWEBD_IMSG_CTL_START:
 			sockets_launch(env);
@@ -856,8 +881,8 @@ sockets_shutdown(void)
 	free(gotwebd_env->iev_fcgi);
 
 	for (i = 0; i < gotwebd_env->prefork; i++)
-		imsgbuf_clear(&gotwebd_env->iev_gotweb[i].ibuf);
-	free(gotwebd_env->iev_gotweb);
+		imsgbuf_clear(&gotwebd_env->iev_auth[i].ibuf);
+	free(gotwebd_env->iev_auth);
 	free(gotwebd_env->worker_load);
 	free(gotwebd_env);