Commit Diff


commit - 0c015ab4872bcbdfc2e941da7bb6ca6bb32ce98865d971a29416a1e881372bf0
commit + dd498279549244fc8c9c3c1fef4e147e441375388d3fe41c5a05d805829b288f
blob - 1f2ea3c45677601db9ebde7cbc87f69849859b10a84f25b47760caa21c32d933
blob + 9b51ed6a7422963866db221e1533e48787fedddb00eb262297edca98593554c3
--- gotsh/Makefile	
+++ gotsh/Makefile	
@@ -9,7 +9,8 @@ SRCS=		gotsh.c error.c pkt.c hash.c serve.c path.c git
 
 MAN =		${PROG}.1
 
-CPPFLAGS = -I${.CURDIR}/../include -I${.CURDIR}/../lib -I${.CURDIR}/../gotd
+CPPFLAGS = -I${.CURDIR}/../include -I${.CURDIR}/../lib -I${.CURDIR}/../gotd \
+	-I${.CURDIR}/../gotwebd
 
 .if defined(PROFILE)
 LDADD = -lutil_p -lc_p -levent_p
blob - f942b54ea8a4cfc00f83e6817db87248ec7ae7c67f9fdc9d41e2886d3d4ef55d
blob + 5d9f63cfe45a47b0a4d7afa6e7a4cd845ec59108aaef143a2a22858db32e4b05
--- gotsh/gotsh.1	
+++ gotsh/gotsh.1	
@@ -22,6 +22,7 @@
 .Sh SYNOPSIS
 .Nm Fl c Sq Cm git-receive-pack Ar repository-path
 .Nm Fl c Sq Cm git-upload-pack Ar repository-path
+.Nm Fl c Sq Cm weblogin Oo Ar hostname Oc
 .Sh DESCRIPTION
 .Nm
 is the network-facing interface to
@@ -75,6 +76,27 @@ accessing the unix socket of
 via
 .Nm .
 .Pp
+The
+.Cm weblogin
+command provides user authentication for
+.Xr gotwebd 8 .
+.Nm
+will connect to
+.Xr gotwebd 8
+and obtain a login URL which allows browsing private repositories the user
+has been granted read access to in
+.Xr gotwebd.conf 5 .
+If multiple servers are declared in
+.Xr gotwebd.conf 5
+the
+.Ar hostname
+parameter is required and indicates the desired virtual host to use in the URL.
+If no
+.Ar hostname
+is specified and only one server is declared in
+.Xr gotwebd.conf 5
+then the name of this server will be used in the URL.
+.Pp
 It is recommended to restrict
 .Xr ssh 1
 features available to users of
@@ -139,12 +161,48 @@ Match User anonymous
     DisableForwarding yes
     PermitTTY no
 .Ed
+.Pp
+Obtain a
+.Xr gotwebd 8
+login URL for got.example.com:
+.Bd -literal -offset indent
+$ ssh got.example.com weblogin
+.Ed
+.Pp
+If the web server at got.example.com serves virtual hosts then two
+hostnames must be provided.
+One for
+.Xr ssh 1
+to connect to, and another to identify the virtual host served by
+.Xr gotwebd 8 :
+.Bd -literal -offset indent
+$ ssh got.example.com weblogin got.example.com
+.Ed
+.Pp
+In practice both hostnames will often be the same, but this is not guaranteed.
+There is no reliable way determine the desired virtual host automatically.
+An
+.Xr ssh_config 5
+entry like the following can save some typing:
+.Bd -literal -offset indent
+Host weblogin
+	Hostname got.example.com
+	RemoteCommand weblogin %h
+.Ed
+.Pp
+The following command is now equivalent to the above:
+.Bd -literal -offset indent
+$ ssh weblogin
+.Ed
+.Ed
 .Sh SEE ALSO
 .Xr gitwrapper 1 ,
 .Xr got 1 ,
 .Xr ssh 1 ,
 .Xr gotd.conf 5 ,
+.Xr gotwebd.conf 5 ,
 .Xr sshd_config 5 ,
-.Xr gotd 8
+.Xr gotd 8 ,
+.Xr gotwebd 8
 .Sh AUTHORS
 .An Stefan Sperling Aq Mt stsp@openbsd.org
blob - 173536a801371f1a24d5d9efab01142edc1d57821bf71d8e2c8bbb0205e4b240
blob + 01360780e95162d32312decb86f6a4451460b7ea2d7df04866e8e21a25eb72d2
--- gotsh/gotsh.c	
+++ gotsh/gotsh.c	
@@ -19,6 +19,7 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 
+#include <ctype.h>
 #include <err.h>
 #include <event.h>
 #include <imsg.h>
@@ -34,10 +35,13 @@
 #include "got_object.h"
 #include "got_serve.h"
 #include "got_path.h"
+#include "got_reference.h"
 
 #include "got_lib_dial.h"
+#include "got_lib_poll.h"
 
 #include "gotd.h"
+#include "gotwebd.h"
 
 static int chattygot;
 
@@ -46,6 +50,7 @@ usage(void)
 {
 	fprintf(stderr, "usage: %s -c '%s|%s repository-path'\n",
 	    getprogname(), GOT_DIAL_CMD_SEND, GOT_DIAL_CMD_FETCH);
+	fprintf(stderr, "       %s -c 'weblogin [hostname]'\n", getprogname());
 	exit(1);
 }
 
@@ -65,46 +70,209 @@ apply_unveil(const char *unix_socket_path)
 	return NULL;
 }
 
+/* Read session URL from gotwebd's auth socket and send it to the client. */
+static const struct got_error *
+weblogin(FILE *out, int sock, const char *hostname)
+{
+	const struct got_error *err = NULL;
+	FILE *fp;
+	int ret;
+	char *line = NULL;
+	size_t linesize;
+	ssize_t linelen;
+
+	fp = fdopen(sock, "w+");
+	if (fp == NULL)
+		return got_error_from_errno("fdopen");
+
+	ret = fprintf(fp, "login%s%s\n", hostname != NULL ? " " : "",
+	    hostname != NULL ? hostname : "");
+	if (ret < 0) {
+		err = got_error_from_errno("fprintf");
+		goto done;
+	}
+
+	/*
+	 * gotwebd will return "ok URL", likewise terminated by \n,
+	 * or might return an arbitrary error message + \n.
+	 * We don't know how long this line will be, so keep reading
+	 * in chunks until we have read all of it.
+	 * For forward compatibilty, ignore any trailing lines received.
+	 */
+	linelen = getline(&line, &linesize, fp);
+	if (linelen == -1) {
+		err = got_error(GOT_ERR_EOF);
+		if (ferror(fp))
+			err = got_error_from_errno("getline");
+		goto done;
+	}
+
+	if (strncmp(line, "ok ", 3) == 0) {
+		fprintf(out, "Login successful.  Please visit the following "
+		    "URL within the next %d minutes: %s\n",
+		    GOTWEBD_LOGIN_TIMEOUT / 60, line + 3);
+		goto done;
+	}
+
+	if (strncmp(line, "err ", 4) == 0) {
+		err = got_error_fmt(GOT_ERR_LOGIN_FAILED, "%s", line + 4);
+		goto done;
+	}
+
+	err = got_error(GOT_ERR_UNKNOWN_COMMAND);
+done:
+	if (line != NULL)
+		free(line);
+	if (fp != NULL && fclose(fp) == EOF && err == NULL)
+		err = got_error_from_errno("fclose");
+	return err;
+}
+
+
+static const struct got_error *
+parse_weblogin_command(char **hostname, char *cmd)
+{
+	size_t len, cmdlen;
+
+	*hostname = NULL;
+
+	len = strlen(cmd);
+
+	while (len > 0 && isspace(cmd[len - 1]))
+		cmd[--len] = '\0';
+
+	if (len == 0)
+		return got_error(GOT_ERR_BAD_PACKET);
+
+	if (len >= strlen(GOTWEBD_LOGIN_CMD) &&
+	    strncmp(cmd, GOTWEBD_LOGIN_CMD, strlen(GOTWEBD_LOGIN_CMD)) == 0)
+		cmdlen = strlen(GOTWEBD_LOGIN_CMD);
+	else
+		return got_error(GOT_ERR_BAD_PACKET);
+
+	/* The hostname parameter is optional. */
+	if (len == cmdlen)
+		return NULL;
+
+	if (len <= cmdlen + 1 || cmd[cmdlen] != ' ')
+		return got_error(GOT_ERR_BAD_PACKET);
+
+	if (memchr(&cmd[cmdlen + 1], '\0', len - cmdlen) == NULL)
+		return got_error(GOT_ERR_BAD_PACKET);
+
+	/* Forbid linefeeds in hostnames. We use \n as internal terminator. */
+	if (memchr(&cmd[cmdlen + 1], '\n', len - cmdlen) != NULL)
+		return got_error(GOT_ERR_BAD_PACKET);
+
+	*hostname = strdup(&cmd[cmdlen + 1]);
+	if (*hostname == NULL)
+		return got_error_from_errno("strdup");
+
+	/* Deny an empty hostname. */
+	if ((*hostname)[0] == '\0') {
+		free(*hostname);
+		*hostname = NULL;
+		return got_error(GOT_ERR_BAD_PACKET);
+	}
+
+	/* Deny overlong hostnames ,*/
+	if (len - cmdlen > _POSIX_HOST_NAME_MAX)
+		return got_error_fmt(GOT_ERR_NO_SPACE,
+		    "hostname length exceeds %d bytes", _POSIX_HOST_NAME_MAX);
+
+	/*
+	 * TODO: More hostname verification? In any case, the provided
+	 * value will have to match a string obtained from gotwebd.conf.
+	 */
+
+	return NULL;
+}
+
 int
 main(int argc, char *argv[])
 {
 	const struct got_error *error;
 	const char *unix_socket_path;
-	int gotd_sock = -1;
+	int sock = -1;
 	struct sockaddr_un	 sun;
 	char *gitcmd = NULL, *command = NULL, *repo_path = NULL;
+	char *hostname = NULL;
+	int do_weblogin = 0;
 
 #ifndef PROFILE
 	if (pledge("stdio recvfd unix unveil", NULL) == -1)
 		err(1, "pledge");
 #endif
-
-	unix_socket_path = getenv("GOTD_UNIX_SOCKET");
-	if (unix_socket_path == NULL)
-		unix_socket_path = GOTD_UNIX_SOCKET;
-
-	error = apply_unveil(unix_socket_path);
-	if (error)
-		goto done;
-
-	if (strcmp(argv[0], GOT_DIAL_CMD_SEND) == 0 ||
+	if (strcmp(argv[0], GOTWEBD_LOGIN_CMD) == 0) {
+		if (argc != 1 && argc != 2)
+			usage();
+		unix_socket_path = getenv("GOTWEBD_LOGIN_SOCKET");
+		if (unix_socket_path == NULL)
+			unix_socket_path = GOTWEBD_LOGIN_SOCKET;
+		error = apply_unveil(unix_socket_path);
+		if (error)
+			goto done;
+		if (argc == 2) {
+			hostname = strdup(argv[1]);
+			if (hostname == NULL) {
+				error = got_error_from_errno("strdup");
+				goto done;
+			}
+		}
+		do_weblogin = 1;
+	} else if (strcmp(argv[0], GOT_DIAL_CMD_SEND) == 0 ||
 	    strcmp(argv[0], GOT_DIAL_CMD_FETCH) == 0) {
 		if (argc != 2)
 			usage();
+		unix_socket_path = getenv("GOTD_UNIX_SOCKET");
+		if (unix_socket_path == NULL)
+			unix_socket_path = GOTD_UNIX_SOCKET;
+		error = apply_unveil(unix_socket_path);
+		if (error)
+			goto done;
 		if (asprintf(&gitcmd, "%s %s", argv[0], argv[1]) == -1)
 			err(1, "asprintf");
 		error = got_dial_parse_command(&command, &repo_path, gitcmd);
-	} else {
-		if (argc != 3 || strcmp(argv[1], "-c") != 0)
-			usage();
-		error = got_dial_parse_command(&command, &repo_path, argv[2]);
-	}
-	if (error && error->code == GOT_ERR_BAD_PACKET)
+		if (error) {
+			if (error->code == GOT_ERR_BAD_PACKET)
+				usage();
+			goto done;
+		}
+	} else if (argc == 3 && strcmp(argv[1], "-c") == 0) {
+		if (strncmp(argv[2], GOTWEBD_LOGIN_CMD,
+		    strlen(GOTWEBD_LOGIN_CMD)) == 0) {
+			unix_socket_path = getenv("GOTWEBD_LOGIN_SOCKET");
+			if (unix_socket_path == NULL)
+				unix_socket_path = GOTWEBD_LOGIN_SOCKET;
+			error = apply_unveil(unix_socket_path);
+			if (error)
+				goto done;
+			error = parse_weblogin_command(&hostname, argv[2]);
+			if (error) {
+				if (error->code == GOT_ERR_BAD_PACKET)
+					usage();
+				goto done;
+			}
+			do_weblogin = 1;
+		} else {
+			unix_socket_path = getenv("GOTD_UNIX_SOCKET");
+			if (unix_socket_path == NULL)
+				unix_socket_path = GOTD_UNIX_SOCKET;
+			error = apply_unveil(unix_socket_path);
+			if (error)
+				goto done;
+			error = got_dial_parse_command(&command, &repo_path,
+			    argv[2]);
+			if (error) {
+				if (error->code == GOT_ERR_BAD_PACKET)
+					usage();
+				goto done;
+			}
+		}
+	} else
 		usage();
-	if (error)
-		goto done;
 
-	if ((gotd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
+	if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
 		err(1, "socket");
 
 	memset(&sun, 0, sizeof(sun));
@@ -112,21 +280,30 @@ main(int argc, char *argv[])
 	if (strlcpy(sun.sun_path, unix_socket_path,
 	    sizeof(sun.sun_path)) >= sizeof(sun.sun_path))
 		errx(1, "gotd socket path too long");
-	if (connect(gotd_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
+	if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
 		err(1, "connect: %s", unix_socket_path);
 
+	if (do_weblogin) {
 #ifndef PROFILE
-	if (pledge("stdio recvfd", NULL) == -1)
-		err(1, "pledge");
+		if (pledge("stdio", NULL) == -1)
+			err(1, "pledge");
 #endif
-	error = got_serve(STDIN_FILENO, STDOUT_FILENO, command, repo_path,
-	    gotd_sock, chattygot);
+		error = weblogin(stdout, sock, hostname);
+	} else {
+#ifndef PROFILE
+		if (pledge("stdio recvfd", NULL) == -1)
+			err(1, "pledge");
+#endif
+		error = got_serve(STDIN_FILENO, STDOUT_FILENO, command,
+		    repo_path, sock, chattygot);
+	}
 done:
 	free(gitcmd);
 	free(command);
 	free(repo_path);
-	if (gotd_sock != -1)
-		close(gotd_sock);
+	free(hostname);
+	if (sock != -1)
+		close(sock);
 	if (error) {
 		fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
 		return 1;
blob - 7048d1ad9d5cbbe00e2beea61079f79e6fc2b879d7d35d45e6e0df5e271e606c
blob + 6908c04165db18fdc7db0b1ca08380cea29759795e25adc0315033f95f970895
--- gotwebd/gotwebd.conf.5	
+++ gotwebd/gotwebd.conf.5	
@@ -80,6 +80,18 @@ While the specified
 .Ar path
 must be absolute, it should usually point inside the web server's chroot
 directory such that the web server can access the socket.
+.It Ic login socket Ar path
+Set the
+.Ar path
+to the
+.Ux Ns -domain
+socket for
+.Xr gotsh 1
+.Ic weblogin
+commands.
+By default the path
+.Pa /var/run/gotweb-login.sock
+will be used.
 .It Ic prefork Ar number
 Spawn enough processes such that
 .Ar number
blob - d75843b991deeebb6107fc3cfa866d6663fa1807ca6939c417a9c56985ece217
blob + 3ea8bb3187e6528e8395e94bc7975b0fbbc501aab7834efa93bf6b641da6815f
--- gotwebd/gotwebd.h	
+++ gotwebd/gotwebd.h	
@@ -39,6 +39,10 @@
 #define GOTWEBD_WWW_USER	 "www"
 #endif
 
+#define GOTWEBD_LOGIN_CMD	"weblogin"
+#define GOTWEBD_LOGIN_SOCKET	 "/var/run/gotweb-login.sock"
+#define GOTWEBD_LOGIN_TIMEOUT	 300 /* in seconds */
+
 #define GOTWEBD_MAXDESCRSZ	 1024
 #define GOTWEBD_MAXCLONEURLSZ	 1024
 #define GOTWEBD_CACHESIZE	 1024
blob - 5eae740af7035feea6899f6918d3f7913d4be8a051e2a325134cd745a39dedbe
blob + f8283b673abc08f45899e81fe354be1a70f146fab906ab3b890f947f3b87cec2
--- include/got_error.h	
+++ include/got_error.h	
@@ -197,6 +197,8 @@
 #define GOT_ERR_AUTHORIZED_KEY	180
 #define GOT_ERR_CONNECTION_LIMIT 190
 #define GOT_ERR_ON_SERVER_SIDE	191
+#define GOT_ERR_LOGIN_FAILED	192
+#define GOT_ERR_UNKNOWN_COMMAND	193
 
 struct got_error {
         int code;
blob - a4cc1e868466853221008d463d499319c36161afec6cfbf795ba524a5f0f562b
blob + 7977b835b356398cb1c386190245407f549b2fce6c49cd74f3959a7a4ec8e698
--- lib/error.c	
+++ lib/error.c	
@@ -248,6 +248,8 @@ static const struct got_error got_errors[] = {
 	{ GOT_ERR_AUTHORIZED_KEY, "no authorized key found" },
 	{ GOT_ERR_CONNECTION_LIMIT, "connection limit exceeded" },
 	{ GOT_ERR_ON_SERVER_SIDE, "see server-side logs for error details" },
+	{ GOT_ERR_LOGIN_FAILED, "login failed" },
+	{ GOT_ERR_UNKNOWN_COMMAND, "command not found" },
 };
 
 static struct got_custom_error {
blob - cf1dc2245c1bcf28059108a6a47c471b4b5e6661338cc8652ade9c05bb00687e
blob + 489d6ffd2905dff9e87c4c8b88ad97b5cef7eb0537ad17c32ac13edb4e0ce2a6
--- regress/gotsysd/test_gotsysd.sh	
+++ regress/gotsysd/test_gotsysd.sh	
@@ -230,6 +230,7 @@ EOF
 
 	cat > $testroot/stderr.expected <<EOF
 usage: gotsh -c 'git-receive-pack|git-upload-pack repository-path'
+       gotsh -c 'weblogin [hostname]'
 EOF
 	cmp -s $testroot/stderr.expected $testroot/stderr
 	ret=$?
@@ -268,6 +269,7 @@ EOF
 
 	cat > $testroot/stderr.expected <<EOF
 usage: gotsh -c 'git-receive-pack|git-upload-pack repository-path'
+       gotsh -c 'weblogin [hostname]'
 EOF
 	cmp -s $testroot/stderr.expected $testroot/stderr
 	ret=$?