Commit Diff


commit - dd498279549244fc8c9c3c1fef4e147e441375388d3fe41c5a05d805829b288f
commit + e019e61b9b185dc80cca8bd0340ae3a075a1e28df7ab5c9ee801937d3ab260a8
blob - eb769fde86233b7a33778c220214e1b401e1bfddcf1aa2a733d24745ec4b856e
blob + 6dfdb6e42e1e4819085a54c4cd152c68cabde40960d839fa46204b097bbc8ca2
--- gotwebd/Makefile	
+++ gotwebd/Makefile	
@@ -5,7 +5,7 @@
 .include "Makefile.inc"
 
 PROG =		gotwebd
-SRCS =		config.c sockets.c gotwebd.c parse.y \
+SRCS =		config.c sockets.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 \
@@ -37,9 +37,9 @@ MAN =		${PROG}.conf.5 ${PROG}.8
 
 CPPFLAGS +=	-I${.CURDIR}/../include -I${.CURDIR}/../lib -I${.CURDIR}
 CPPFLAGS +=	-I${.CURDIR}/../template
-LDADD +=	-lz -levent -lutil -lm
+LDADD +=	-lz -levent -lutil -lm -lcrypto
 YFLAGS =
-DPADD =		${LIBEVENT} ${LIBUTIL} ${LIBM}
+DPADD =		${LIBEVENT} ${LIBUTIL} ${LIBM} ${LIBCRYPTO}
 #CFLAGS +=	-DGOT_NO_OBJ_CACHE
 
 .if ${GOT_RELEASE} != "Yes"
blob - 21ab2c5d25f903b7271ca62c77dd46c05b1699a74406522b7a5b728415c4c22c
blob + ad4966dcfb2ecfb9075ee2961fc5a28f33c5558bfa8f43ad49da247a3ad50970
--- gotwebd/gotwebd.c	
+++ gotwebd/gotwebd.c	
@@ -57,6 +57,7 @@ void	 gotwebd_sighdlr(int sig, short event, void *arg)
 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_gotweb(int, short, void *);
 
 struct gotwebd	*gotwebd_env;
@@ -127,6 +128,13 @@ main_compose_sockets(struct gotwebd *env, uint32_t typ
 }
 
 int
+main_compose_login(struct gotwebd *env, uint32_t type, int fd,
+    const void *data, uint16_t len)
+{
+	return send_imsg(env->iev_login, type, fd, data, len);
+}
+
+int
 main_compose_gotweb(struct gotwebd *env, uint32_t type, int fd,
     const void *data, uint16_t len)
 {
@@ -149,6 +157,52 @@ sockets_compose_main(struct gotwebd *env, uint32_t typ
 	return (imsg_compose_event(env->iev_parent, type, 0, -1, -1, d, len));
 }
 
+ void
+gotwebd_dispatch_login(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) {
+		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_server(int fd, short event, void *arg)
 {
@@ -365,6 +419,9 @@ spawn_process(struct gotwebd *env, const char *argv0, 
 		if (asprintf(&s, "-S%d", env->prefork) == -1)
 			fatal("asprintf");
 		argv[argc++] = s;
+	} else if (proc_type == GOTWEBD_PROC_LOGIN) {
+		argv[argc++] = "-L";
+		argv[argc++] = username;
 	} else if (proc_type == GOTWEBD_PROC_FCGI) {
 		argv[argc++] = "-F";
 		argv[argc++] = username;
@@ -431,7 +488,7 @@ main(int argc, char **argv)
 		fatal("%s: calloc", __func__);
 	config_init(env);
 
-	while ((ch = getopt(argc, argv, "D:dG:f:F:nS:vW:")) != -1) {
+	while ((ch = getopt(argc, argv, "D:dG:f:F:L:nS:vW:")) != -1) {
 		switch (ch) {
 		case 'D':
 			if (cmdline_symset(optarg) < 0)
@@ -452,6 +509,10 @@ main(int argc, char **argv)
 			proc_type = GOTWEBD_PROC_FCGI;
 			gotwebd_username = optarg;
 			break;
+		case 'L':
+			proc_type = GOTWEBD_PROC_LOGIN;
+			gotwebd_username = optarg;
+			break;
 		case 'n':
 			no_action = 1;
 			break;
@@ -521,6 +582,17 @@ main(int argc, char **argv)
 	log_setverbose(env->gotwebd_verbose);
 
 	switch (proc_type) {
+	case GOTWEBD_PROC_LOGIN:
+		setproctitle("login");
+		log_procinit("login");
+
+		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_login(env, GOTWEBD_SOCK_FILENO);
+		return 1;
 	case GOTWEBD_PROC_SOCKETS:
 		setproctitle("sockets");
 		log_procinit("sockets");
@@ -577,6 +649,10 @@ main(int argc, char **argv)
 	if (env->iev_sockets == NULL)
 		fatal("calloc");
 
+	env->iev_login = calloc(1, sizeof(*env->iev_login));
+	if (env->iev_login == NULL)
+		fatal("calloc");
+
 	env->iev_fcgi = calloc(1, sizeof(*env->iev_fcgi));
 	if (env->iev_fcgi == NULL)
 		fatal("calloc");
@@ -589,6 +665,9 @@ main(int argc, char **argv)
 	    GOTWEBD_PROC_SOCKETS, gotwebd_username,
 	    gotwebd_dispatch_server);
 
+	spawn_process(env, argv0, env->iev_login, GOTWEBD_PROC_LOGIN,
+	    gotwebd_username, gotwebd_dispatch_login);
+
 	spawn_process(env, argv0, env->iev_fcgi,
 	    GOTWEBD_PROC_FCGI, gotwebd_username,
 	    gotwebd_dispatch_fcgi);
@@ -687,6 +766,7 @@ gotwebd_configure(struct gotwebd *env, uid_t uid, gid_
 {
 	struct server *srv;
 	struct socket *sock;
+	char login_token_secret[32];
 
 	/* gotweb need to reload its config. */
 	env->gotweb_pending = env->prefork;
@@ -714,6 +794,21 @@ 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_login(env, GOTWEBD_IMSG_LOGIN_SECRET, -1,
+	    login_token_secret, sizeof(login_token_secret)) == -1)
+		fatal("main_compose_gotweb GOTWEB_IMSG_LOGIN_SECRET");
+
+	explicit_bzero(login_token_secret, sizeof(login_token_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");
+
 	if (main_compose_sockets(env, GOTWEBD_IMSG_CFG_DONE, -1,
 	    NULL, 0) == -1)
 		fatal("send_imsg GOTWEBD_IMSG_CFG_DONE");
@@ -735,6 +830,10 @@ gotwebd_configure_done(struct gotwebd *env)
 		        -1, NULL, 0) == -1)
 			fatal("main_compose_gotewb GOTWEBD_IMSG_CTL_START");
 	}
+
+	if (main_compose_login(env, GOTWEBD_IMSG_CTL_START,
+	    -1, NULL, 0) == -1)
+		fatal("send_imsg GOTWEBD_IMSG_CTL_START");
 }
 
 void
@@ -744,6 +843,12 @@ gotwebd_shutdown(void)
 	pid_t		 pid;
 	int		 i, status;
 
+	event_del(&env->iev_login->ev);
+	imsgbuf_clear(&env->iev_login->ibuf);
+	close(env->iev_login->ibuf.fd);
+	env->iev_login->ibuf.fd = -1;
+	free(env->iev_login);
+
 	event_del(&env->iev_sockets->ev);
 	imsgbuf_clear(&env->iev_sockets->ibuf);
 	close(env->iev_sockets->ibuf.fd);
@@ -764,6 +869,8 @@ gotwebd_shutdown(void)
 	}
 	free(env->iev_gotweb);
 
+	free(env->login_sock);
+
 	do {
 		pid = waitpid(WAIT_ANY, &status, 0);
 		if (pid <= 0)
blob - 3ea8bb3187e6528e8395e94bc7975b0fbbc501aab7834efa93bf6b641da6815f
blob + 964956044084e15e68c131c4e14c9d229c4bb379fc21ffe26d7aa2c03f868c5c
--- gotwebd/gotwebd.h	
+++ gotwebd/gotwebd.h	
@@ -128,6 +128,7 @@ enum gotwebd_proc_type {
 	GOTWEBD_PROC_PARENT,
 	GOTWEBD_PROC_SOCKETS,
 	GOTWEBD_PROC_FCGI,
+	GOTWEBD_PROC_LOGIN,
 	GOTWEBD_PROC_GOTWEB,
 };
 
@@ -138,6 +139,7 @@ enum imsg_type {
 	GOTWEBD_IMSG_CFG_DONE,
 	GOTWEBD_IMSG_CTL_PIPE,
 	GOTWEBD_IMSG_CTL_START,
+	GOTWEBD_IMSG_LOGIN_SECRET,
 	GOTWEBD_IMSG_FCGI_PARSE_PARAMS,
 	GOTWEBD_IMSG_FCGI_PARAMS,
 	GOTWEBD_IMSG_REQ_ABORT,
@@ -404,6 +406,9 @@ struct gotwebd {
 	struct socketlist	sockets;
 	struct addresslist	addresses;
 
+	struct socket	*login_sock;
+	struct event	 login_pause_ev;
+
 	int		 pack_fds[GOTWEB_PACK_NUM_TEMPFILES];
 	int		 priv_fd[PRIV_FDS__MAX];
 
@@ -417,6 +422,8 @@ struct gotwebd {
 	struct imsgev	*iev_parent;
 	struct imsgev	*iev_sockets;
 	struct imsgev	*iev_fcgi;
+	struct imsgev	*iev_login;
+	struct imsgev	*iev_gotsh;
 	struct imsgev	*iev_gotweb;
 
 	uint16_t	 prefork;
@@ -471,6 +478,8 @@ int	 imsg_compose_event(struct imsgev *, uint16_t, uin
 	    pid_t, int, const void *, size_t);
 int	 main_compose_sockets(struct gotwebd *, uint32_t, int,
 	    const void *, uint16_t);
+int	 main_compose_login(struct gotwebd *, uint32_t, int,
+	    const void *, uint16_t);
 int	 sockets_compose_main(struct gotwebd *, uint32_t,
 	    const void *, uint16_t);
 int	 main_compose_gotweb(struct gotwebd *, uint32_t, int,
@@ -480,9 +489,16 @@ int	 main_compose_gotweb(struct gotwebd *, uint32_t, i
 void sockets(struct gotwebd *, int);
 void sockets_parse_sockets(struct gotwebd *);
 void sockets_socket_accept(int, short, void *);
+struct socket *sockets_conf_new_socket(int, struct address *);
 int sockets_privinit(struct gotwebd *, struct socket *, uid_t, gid_t);
 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);
+int login_privinit(struct gotwebd *, uid_t, gid_t);
+void gotwebd_login(struct gotwebd *, int);
+
 /* gotweb.c */
 void gotweb_index_navs(struct request *, struct gotweb_url *, int *,
     struct gotweb_url *, int *);
blob - /dev/null
blob + 2a5a86845da6bc91a3b1f0247d78a170956a2733213d993dba6b5b9b71b4ad00 (mode 644)
--- /dev/null	
+++ gotwebd/login.c	
@@ -0,0 +1,777 @@
+/*
+ * Copyright (c) 2025 Stefan Sperling <stsp@openbsd.org>
+ * Copyright (c) 2025 Omar Polo <op@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 <sys/stat.h>
+
+#include <errno.h>
+#include <event.h>
+#include <imsg.h>
+#include <sha1.h>
+#include <sha2.h>
+#include <signal.h>
+#include <siphash.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <openssl/bio.h>
+#include <openssl/buffer.h>
+#include <openssl/evp.h>
+#include <openssl/hmac.h>
+
+#include "got_error.h"
+#include "got_reference.h"
+#include "got_object.h"
+
+#include "gotwebd.h"
+#include "log.h"
+
+#define LOGIN_SOCKET_BACKLOG 4
+
+struct gotwebd_login_client {
+	int fd;
+	int cmd_done;
+	uid_t euid;
+	struct bufferevent *bev;
+};
+
+static volatile int client_cnt;
+static int inflight;
+
+static char login_token_secret[32];
+
+/*
+ * The token format is:
+ *
+ *    "v1\0"[issued at/64bit][expire/64bit][uid/64bit][host]"\0"
+ *
+ * Padded with additional \0 to a length divisible by 4, and then
+ * followed by the HMAC-SHA256 of it, all encoded in base64.
+ */
+
+/* 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)
+{
+	time_t	 now;
+	uint64_t issued, expire, uid;
+	uint8_t *data;
+	int	 len;
+	char	 hmac[32], exp[32];
+	unsigned int explen;
+	size_t	used = 0;
+
+	/*
+	 * We get called in several processes which should all have
+	 * a copy of the same secret.
+	 */
+	if (secret_len != sizeof(login_token_secret))
+		fatalx("login token secret length mismatch");
+
+	/* xxx check for overflow */
+	len = (strlen(token) / 4) * 3;
+
+	data = malloc(len);
+	if (data == NULL)
+		return -1;
+
+	len = EVP_DecodeBlock(data, token, strlen(token));
+	if (len == -1) {
+		free(data);
+		return -1;
+	}
+
+	/* Trim padding. */
+	while (len > 0 && (len % 4) != 0)
+		len--;
+
+	if (len < 28 + 32) { /* min length assuming empty hostname */
+		log_warnx("login token too short: %d", len);
+		free(data);
+		return -1;
+	}
+
+	if (memcmp(data, "v1", 3) != 0) {
+		log_warnx("unknown login token format version");
+		free(data);
+		return -1;
+	}
+	used = 3;
+
+	if (HMAC(EVP_sha256(), secret, secret_len, data, len - 32,
+	    exp, &explen) == NULL) {
+		log_warnx("HMAC computation failed\n");
+		free(data);
+		return -1;
+	}
+
+	if (explen != 32) {
+		log_warnx("unexpected HMAC length: %u\n", explen);
+		free(data);
+		return -1;
+	}
+
+	memcpy(hmac, data + len - explen, explen);
+
+	if (memcmp(hmac, exp, explen) != 0) {
+		log_warnx("HMAC check failed\n");
+		free(data);
+		return -1;
+	}
+
+	memcpy(&issued, data + used, sizeof(issued));
+	used += sizeof(issued);
+
+	memcpy(&expire, data + used, sizeof(expire));
+	used += sizeof(expire);
+
+	memcpy(&uid, data + used, sizeof(uid));
+	used += sizeof(uid);
+
+	now = time(NULL);
+	if (expire < now) {
+		log_warnx("uid %llu: login token has expired\n", uid);
+		free(data);
+		return -1;
+	}
+
+	if (euid)
+		*euid = (uid_t)uid;
+
+	if (hostname) {
+		if (used < len - explen) {
+			*hostname = strndup(data + used,
+			    len - explen - used);
+			if (*hostname == NULL) {
+				log_warn("strndup");
+				free(data);
+				return -1;
+			}
+		} else {
+			*hostname = strdup("");
+			if (*hostname == NULL) {
+				log_warn("strdup");
+				free(data);
+				return -1;
+			}
+		}
+	}
+		
+	free(data);
+	return 0;
+}
+
+char *
+login_gen_token(uint64_t uid, const char *hostname, time_t validity,
+    const char *secret, size_t secret_len)
+{
+	BIO		*bmem, *b64;
+	BUF_MEM		*bufm;
+	char		 hmac[EVP_MAX_MD_SIZE];
+	char		*enc;
+	FILE		*fp;
+	char		*tok;
+	time_t		 now;
+	uint64_t	 issued, expire;
+	size_t		 siz, hlen, pad;
+	unsigned int	 hmaclen;	/* openssl... */
+
+	/*
+	 * We get called in several processes which should all have
+	 * a copy of the same secret.
+	 */
+	if (secret_len != sizeof(login_token_secret))
+		fatalx("login token secret length mismatch");
+
+	now = time(NULL);
+	issued = (uint64_t)now;
+	expire = issued + validity;
+
+	fp = open_memstream(&tok, &siz);
+	if (fp == NULL)
+		return NULL;
+
+	/* include NUL */
+	hlen = strlen(hostname) + 1;
+
+	if (fwrite("v1", 1, 3, fp) != 3 ||
+	    fwrite(&issued, 1, 8, fp) != 8 ||
+	    fwrite(&expire, 1, 8, fp) != 8 ||
+	    fwrite(&uid, 1, 8, fp) != 8 ||
+	    fwrite(hostname, 1, hlen, fp) != hlen) {
+		fclose(fp);
+		free(tok);
+		return NULL;
+	}
+
+	/* Pad hostname with trailing NULs for base64 encoding. */
+	pad = 0;
+	while (((3 + 8 + 8 + 8 + hlen + pad) % 4) != 0) {
+		if (fwrite("", 1, 1, fp) != 1) {
+			fclose(fp);
+			free(tok);
+			return NULL;
+		}
+		pad++;
+	}
+
+	if (fclose(fp) == EOF) {
+		free(tok);
+		return NULL;
+	}
+
+	/* Base64 encoder expects a length divisible by 4. */
+	if ((siz % 4) != 0)
+		fatalx("generated token with bad size %zu", siz);
+
+	if (siz > INT_MAX) {
+		/*
+		 * can't really happen, isn't it?  yet, openssl
+		 * sometimes likes to take ints so I'd prefer to
+		 * assert.
+		 */
+		free(tok);
+		return NULL;
+	}
+
+	if (HMAC(EVP_sha256(), secret, secret_len, tok, siz,
+	    hmac, &hmaclen) == NULL) {
+		free(tok);
+		return NULL;
+	}
+
+	bmem = BIO_new(BIO_s_mem());
+	if (bmem == NULL) {
+		free(tok);
+		return NULL;
+	}
+
+	b64 = BIO_new(BIO_f_base64());
+	if (b64 == NULL) {
+		BIO_free(bmem);
+		free(tok);
+		return NULL;
+	}
+
+	BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
+	b64 = BIO_push(b64, bmem);
+
+	if (BIO_write(b64, tok, siz) != (int)siz ||
+	    BIO_write(b64, hmac, hmaclen) != hmaclen ||
+	    BIO_flush(b64) <= 0) {
+		free(tok);
+		BIO_free_all(b64);
+		return NULL;
+	}
+
+	BIO_get_mem_ptr(b64, &bufm);
+	enc = strndup(bufm->data, bufm->length);
+
+	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!");
+
+	return enc;
+}
+
+static int
+login_socket_listen(struct gotwebd *env, struct socket *sock,
+    uid_t uid, gid_t gid)
+{
+	int u_fd = -1;
+	mode_t old_umask, mode;
+
+	u_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
+	if (u_fd == -1) {
+		log_warn("%s: socket", __func__);
+		return -1;
+	}
+
+	if (unlink(sock->conf.unix_socket_name) == -1) {
+		if (errno != ENOENT) {
+			log_warn("%s: unlink %s", __func__,
+			    sock->conf.unix_socket_name);
+			close(u_fd);
+			return -1;
+		}
+	}
+
+	old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
+	mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
+
+	if (bind(u_fd, (struct sockaddr *)&sock->conf.addr.ss,
+	    sock->conf.addr.slen) == -1) {
+		log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
+		close(u_fd);
+		(void)umask(old_umask);
+		return -1;
+	}
+
+	(void)umask(old_umask);
+
+	if (chmod(sock->conf.unix_socket_name, mode) == -1) {
+		log_warn("%s: chmod", __func__);
+		close(u_fd);
+		(void)unlink(sock->conf.unix_socket_name);
+		return -1;
+	}
+
+	if (chown(sock->conf.unix_socket_name, uid, gid) == -1) {
+		log_warn("%s: chown", __func__);
+		close(u_fd);
+		(void)unlink(sock->conf.unix_socket_name);
+		return -1;
+	}
+
+	if (listen(u_fd, LOGIN_SOCKET_BACKLOG) == -1) {
+		log_warn("%s: listen", __func__);
+		return -1;
+	}
+
+	return u_fd;
+}
+
+int
+login_privinit(struct gotwebd *env, uid_t uid, gid_t gid)
+{
+	struct socket *sock = env->login_sock;
+
+	if (sock == NULL)
+		fatalx("no login socket configured");
+
+	log_info("initializing login socket %s",
+	    sock->conf.unix_socket_name);
+
+	sock->fd = login_socket_listen(env, sock, uid, gid);
+	if (sock->fd == -1)
+		return -1;
+
+	return 0;
+}
+
+static void
+login_shutdown(void)
+{
+	struct gotwebd *env = gotwebd_env;
+
+	imsgbuf_clear(&env->iev_parent->ibuf);
+	free(env->iev_parent);
+	if (env->iev_gotsh) {
+		imsgbuf_clear(&env->iev_gotsh->ibuf);
+		free(env->iev_gotsh);
+	}
+	free(env);
+
+	exit(0);
+}
+
+static void
+login_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:
+		login_shutdown();
+		break;
+	default:
+		log_warn("unexpected signal %d", sig);
+		break;
+	}
+}
+
+static int
+accept_reserve(int fd, struct sockaddr *addr, socklen_t *addrlen,
+    int reserve, volatile int *counter)
+{
+	int ret;
+
+	if (getdtablecount() + reserve + *counter + 1 >= getdtablesize()) {
+		log_debug("inflight fds exceeded");
+		errno = EMFILE;
+		return -1;
+	}
+
+	if ((ret = accept4(fd, addr, addrlen,
+	    SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
+		(*counter)++;
+	}
+
+	return ret;
+}
+
+static void
+client_err(struct bufferevent *bev, short error, void *d)
+{
+	struct gotwebd_login_client *client = d;
+
+	log_debug("closing connection with client fd=%d; error=%d",
+	    client->fd, error);
+
+	bufferevent_free(client->bev);
+	close(client->fd);
+	free(client);
+
+	inflight--;
+	client_cnt--;
+}
+
+static void
+client_read(struct bufferevent *bev, void *d)
+{
+	struct gotwebd_login_client *client = d;
+	struct evbuffer *in = EVBUFFER_INPUT(bev);
+	struct evbuffer *out = EVBUFFER_OUTPUT(bev);
+	char *line, *cmd, *code;
+	size_t linelen;
+	const char *hostname;
+
+	if (client->cmd_done) {
+		log_warnx("%s: client sent data even though login command "
+		    "has already completed", __func__);
+		client_err(bev, EVBUFFER_READ, client);
+		return;
+	}
+
+	line = evbuffer_readln(in, &linelen, EVBUFFER_EOL_LF);
+	if (line == NULL) {
+		/*
+		 * there is no line yet to read.  however, error if we
+		 * have too much data buffered without a newline
+		 * character.
+		 */
+		if (EVBUFFER_LENGTH(in) > LINE_MAX)
+			client_err(bev, EVBUFFER_READ, client);
+		return;
+	}
+
+	cmd = line;
+	if (strncmp(cmd, "login", 5) == 0) {
+		cmd += 5;
+		cmd += strspn(cmd, " \t");
+		hostname = cmd;
+
+		code = login_gen_token(client->euid, hostname,
+		    5 * 60 /* 5 minutes */,
+		    login_token_secret, sizeof(login_token_secret));
+		if (code == NULL) {
+			log_warn("%s: login_gen_token failed", __func__);
+			client_err(bev, EVBUFFER_READ, client);
+			return;
+		}
+
+		if (evbuffer_add_printf(out, "ok https://%s/?login=%s\n",
+		    hostname, code) == -1) {
+			log_warnx("%s: evbuffer_add_printf failed", __func__);
+			client_err(bev, EVBUFFER_READ, client);
+			free(code);
+			return;
+		}
+		free(code);
+
+		client->cmd_done = 1;
+		bufferevent_enable(client->bev, EV_READ|EV_WRITE);
+		return;
+	}
+
+	if (evbuffer_add_printf(out, "err unknown command\n") == -1) {
+		log_warnx("%s: evbuffer_add_printf failed", __func__);
+		client_err(bev, EVBUFFER_READ, client);
+		return;
+	}
+
+	client->cmd_done = 1;
+	return;
+}
+
+static void
+client_write(struct bufferevent *bev, void *d)
+{
+	struct gotwebd_login_client *client = d;
+	struct evbuffer *out = EVBUFFER_OUTPUT(bev);
+
+	if (client->cmd_done && EVBUFFER_LENGTH(out) == 0) {
+		/* reply sent */
+		client_err(bev, EVBUFFER_WRITE, client);
+		return;
+	}
+}
+
+static void
+login_accept(int fd, short event, void *arg)
+{
+	struct imsgev *iev = arg;
+	struct gotwebd *env = gotwebd_env;
+	struct sockaddr_storage ss;
+	struct timeval backoff;
+	socklen_t len;
+	int s = -1;
+	struct gotwebd_login_client *client = NULL;
+	uid_t euid;
+	gid_t egid;
+
+	backoff.tv_sec = 1;
+	backoff.tv_usec = 0;
+
+	if (event_add(&iev->ev, NULL) == -1) {
+		log_warn("event_add");
+		return;
+	}
+	if (event & EV_TIMEOUT)
+		return;
+
+	len = sizeof(ss);
+
+	/* Other backoff conditions apart from EMFILE/ENFILE? */
+	s = accept_reserve(fd, (struct sockaddr *)&ss, &len, FD_RESERVE,
+	    &inflight);
+	if (s == -1) {
+		switch (errno) {
+		case EINTR:
+		case EWOULDBLOCK:
+		case ECONNABORTED:
+			return;
+		case EMFILE:
+		case ENFILE:
+			event_del(&iev->ev);
+			evtimer_add(&env->login_pause_ev, &backoff);
+			return;
+		default:
+			log_warn("accept");
+			return;
+		}
+	}
+
+	if (client_cnt >= GOTWEBD_MAXCLIENTS)
+		goto err;
+
+	if (getpeereid(s, &euid, &egid) == -1) {
+		log_warn("getpeerid");
+		goto err;
+	}
+
+	client = calloc(1, sizeof(*client));
+	if (client == NULL) {
+		log_warn("%s: calloc", __func__);
+		goto err;
+	}
+	client->fd = s;
+	client->euid = euid;
+	s = -1;
+
+	client->bev = bufferevent_new(client->fd, client_read, client_write,
+	    client_err, client);
+	if (client->bev == NULL) {
+		log_warn("%s: bufferevent_new failed", __func__);
+		goto err;
+	}
+	bufferevent_enable(client->bev, EV_READ|EV_WRITE);
+
+	/*
+	 * undocumented; but these are seconds.  10s should be plenty
+	 * for both receiving a request and sending the reply.
+	 */
+	bufferevent_settimeout(client->bev, 10, 10);
+
+	log_debug("%s: new client connected on fd %d uid %d gid %d", __func__,
+	    client->fd, euid, egid);
+	client_cnt++;
+	return;
+err:
+	inflight--;
+	if (client) {
+		if (client->bev != NULL)
+			bufferevent_free(client->bev);
+		close(client->fd);
+		free(client);
+	}
+	if (s != -1)
+		close(s);
+}
+
+static void
+get_login_sock(struct gotwebd *env, struct imsg *imsg)
+{
+	const struct got_error *err;
+	struct imsgev *iev;
+	int fd;
+
+	if (env->iev_gotsh != NULL) {
+		err = got_error(GOT_ERR_PRIVSEP_MSG);
+		fatalx("%s", err->msg);
+	}
+
+	if (IMSG_DATA_SIZE(imsg) != 0) {
+		err = got_error(GOT_ERR_PRIVSEP_LEN);
+		fatalx("%s", err->msg);
+	}
+
+	fd = imsg_get_fd(imsg);
+	if (fd == -1) {
+		err = got_error(GOT_ERR_PRIVSEP_NO_FD);
+		fatalx("%s", err->msg);
+	}
+
+	iev = calloc(1, sizeof(*iev));
+	if (iev == NULL)
+		fatal("calloc");
+
+	if (imsgbuf_init(&iev->ibuf, fd) == -1)
+		fatal("imsgbuf_init");
+
+	iev->handler = login_accept;
+	iev->data = iev;
+	event_set(&iev->ev, fd, EV_READ, login_accept, iev);
+	imsg_event_add(iev);
+
+	env->iev_gotsh = iev;
+}
+
+static void
+login_launch(struct gotwebd *env)
+{
+#ifndef PROFILE
+	if (pledge("stdio unix", NULL) == -1)
+		fatal("pledge");
+#endif
+	event_add(&env->iev_gotsh->ev, NULL);
+}
+
+static void
+login_dispatch_main(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_SOCK:
+			get_login_sock(env, &imsg);
+			break;
+		case GOTWEBD_IMSG_CTL_START:
+			login_launch(env);
+			break;
+		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__);
+			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
+accept_paused(int fd, short event, void *arg)
+{
+	struct gotwebd *env = gotwebd_env;
+
+	event_add(&env->iev_gotsh->ev, NULL);
+}
+
+void
+gotwebd_login(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 = login_dispatch_main;
+	env->iev_parent->data = env->iev_parent;
+	event_set(&env->iev_parent->ev, fd, EV_READ, login_dispatch_main,
+	    env->iev_parent);
+	event_add(&env->iev_parent->ev, NULL);
+	evtimer_set(&env->login_pause_ev, accept_paused, NULL);
+
+	signal(SIGPIPE, SIG_IGN);
+
+	signal_set(&sighup, SIGHUP, login_sighdlr, env);
+	signal_add(&sighup, NULL);
+	signal_set(&sigint, SIGINT, login_sighdlr, env);
+	signal_add(&sigint, NULL);
+	signal_set(&sigusr1, SIGUSR1, login_sighdlr, env);
+	signal_add(&sigusr1, NULL);
+	signal_set(&sigchld, SIGCHLD, login_sighdlr, env);
+	signal_add(&sigchld, NULL);
+	signal_set(&sigterm, SIGTERM, login_sighdlr, env);
+	signal_add(&sigterm, NULL);
+
+#ifndef PROFILE
+	if (pledge("stdio recvfd unix", NULL) == -1)
+		fatal("pledge");
+#endif
+
+	event_dispatch();
+	event_base_free(evb);
+	login_shutdown();
+}
+
blob - f0467a85a7a175b93083123e65e38c8ce42477a559ca88b60f0270e1c11db871
blob + 2f2e8be4d1f40c81e09f467b13d81edb14dcc12bd05e816ef65eb266995625bb
--- gotwebd/parse.y	
+++ gotwebd/parse.y	
@@ -99,7 +99,7 @@ int				 getservice(const char *);
 int				 n;
 
 int		 get_addrs(const char *, const char *);
-int		 get_unix_addr(const char *);
+struct address *get_unix_addr(const char *);
 int		 addr_dup_check(struct addresslist *, struct address *);
 void		 add_addr(struct address *);
 
@@ -113,7 +113,7 @@ typedef struct {
 
 %}
 
-%token	LISTEN WWW SITE_NAME SITE_OWNER SITE_LINK LOGO
+%token	LISTEN LOGIN WWW SITE_NAME SITE_OWNER SITE_LINK LOGO
 %token	LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
 %token	MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
 %token	SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK RESPECT_EXPORTOK
@@ -237,11 +237,15 @@ main		: PREFORK NUMBER {
 			free($3);
 		}
 		| LISTEN ON SOCKET STRING {
-			if (get_unix_addr($4) == -1) {
+			struct address *h;
+
+			h = get_unix_addr($4);
+			if (h == NULL) {
 				yyerror("can't listen on %s", $4);
 				free($4);
 				YYERROR;
 			}
+			add_addr(h);
 			free($4);
 		}
 		| USER STRING {
@@ -256,6 +260,19 @@ main		: PREFORK NUMBER {
 			free(gotwebd->www_user);
 			gotwebd->www_user = $3;
 		}
+		| LOGIN SOCKET STRING {
+			struct address *h;
+			h = get_unix_addr($3);
+			if (h == NULL) {
+				yyerror("can't listen on %s", $3);
+				free($3);
+				YYERROR;
+			}
+			if (gotwebd->login_sock != NULL)
+				free(gotwebd->login_sock);
+			gotwebd->login_sock = sockets_conf_new_socket(-1, h);
+			free($3);
+		}
 		;
 
 server		: SERVER STRING {
@@ -460,6 +477,7 @@ lookup(char *s)
 		{ "chroot",			CHROOT },
 		{ "custom_css",			CUSTOM_CSS },
 		{ "listen",			LISTEN },
+		{ "login",			LOGIN },
 		{ "logo",			LOGO },
 		{ "logo_url",			LOGO_URL },
 		{ "max_commits_display",	MAX_COMMITS_DISPLAY },
@@ -849,6 +867,7 @@ parse_config(const char *filename, struct gotwebd *env
 	/* add the implicit listen on socket */
 	if (TAILQ_EMPTY(&gotwebd->addresses)) {
 		char path[_POSIX_PATH_MAX];
+		struct address *h;
 
 		if (strlcpy(path, gotwebd->httpd_chroot, sizeof(path))
 		    >= sizeof(path)) {
@@ -860,8 +879,11 @@ parse_config(const char *filename, struct gotwebd *env
 			yyerror("chroot path too long: %s",
 			    gotwebd->httpd_chroot);
 		}
-		if (get_unix_addr(path) == -1)
+		h = get_unix_addr(path);
+		if (h == NULL)
 			yyerror("can't listen on %s", path);
+		else
+			add_addr(h);
 	}
 
 	if (errors)
@@ -870,6 +892,18 @@ parse_config(const char *filename, struct gotwebd *env
 	/* setup our listening sockets */
 	sockets_parse_sockets(env);
 
+	/* Add implicit login socket */
+	if (gotwebd->login_sock == NULL) {
+		struct address *h;
+		h = get_unix_addr(GOTWEBD_LOGIN_SOCKET);
+		if (h == NULL) {
+			fprintf(stderr, "cannot listen on %s",
+			    GOTWEBD_LOGIN_SOCKET);
+			return (-1);
+		}
+		gotwebd->login_sock = sockets_conf_new_socket(-1, h);
+	}
+
 	return (0);
 }
 
@@ -1070,7 +1104,7 @@ get_addrs(const char *hostname, const char *servname)
 	return (0);
 }
 
-int
+struct address *
 get_unix_addr(const char *path)
 {
 	struct address *h;
@@ -1089,11 +1123,10 @@ get_unix_addr(const char *path)
 	if (strlcpy(sun->sun_path, path, sizeof(sun->sun_path)) >=
 	    sizeof(sun->sun_path)) {
 		log_warnx("socket path too long: %s", sun->sun_path);
-		return (-1);
+		return NULL;
 	}
 
-	add_addr(h);
-	return (0);
+	return h;
 }
 
 int
blob - e018e21f727f6f3e220117ac13f4f9507b95b449a7a7b895be4e15c8eacebc74
blob + f963b4bdfa4b36397845657f6ac88a1a6eba3cf959c8cbda101a42c960fe90f7
--- gotwebd/sockets.c	
+++ gotwebd/sockets.c	
@@ -76,9 +76,6 @@ static int	 sockets_create_socket(struct address *);
 static int	 sockets_accept_reserve(int, struct sockaddr *, socklen_t *,
 		    int, volatile int *);
 
-static struct socket *sockets_conf_new_socket(struct gotwebd *,
-		    int, struct address *);
-
 int cgi_inflight = 0;
 
 /* Request hash table needs some spare room to avoid collisions. */
@@ -272,7 +269,7 @@ sockets_parse_sockets(struct gotwebd *env)
 	int sock_id = 1;
 
 	TAILQ_FOREACH(a, &env->addresses, entry) {
-		new_sock = sockets_conf_new_socket(env, sock_id, a);
+		new_sock = sockets_conf_new_socket(sock_id, a);
 		if (new_sock) {
 			sock_id++;
 			TAILQ_INSERT_TAIL(&env->sockets,
@@ -281,8 +278,8 @@ sockets_parse_sockets(struct gotwebd *env)
 	}
 }
 
-static struct socket *
-sockets_conf_new_socket(struct gotwebd *env, int id, struct address *a)
+struct socket *
+sockets_conf_new_socket(int id, struct address *a)
 {
 	struct socket *sock;
 	struct address *acp;