[RFC PATCH v2 0/2] Two new remote helpers

DORMANTno replies

3 messages, 1 author, 2016-06-15 · open the first message on its own page

[RFC PATCH v2 0/2] Two new remote helpers

From: Ilari Liusvaara <hidden>
Date: 2016-06-15 22:49:01

The first helper (fd) is meant for frontends that want to override
how transport link is created. Currently this would require hacks based
on either GIT_SSH or GIT_PROXY (see tortoiseplink). It works by reflecting
the smart transport stream to specified file descriptors.

The second one (ext) invokes specified command, reading/writing smart
transport stream (with optional git://-style request) to its stdin/stdout.
It is meant for situations where one wants unusual kind of transport link.
It can be ssh connection with one-off parameters[1], things like rsh (if
you have to use it because some absolutely incomprehensible reason,
hopefully its really krb5-rsh)[2], wrapping git:// in TLS[3], accessing
servers on unix domain sockets[4], etc...

Changes from last time:
- This cover letter.
- Support \G and \V which are for sending git:// style request in-channel.[5]

[1] "ext::ssh -i somekey user@host.example \S /path/to/repo.git"
[2] "ext::rsh -l user host.example \S /path/to/repo.git"
[3] Don't ask me how, especially not how to get connection to git-daemon2
that way.
[4] "ext::socat - ABSTRACT-CONNECT:/tmp/gits \G/gitolite-admin"
[5] "ext::nc host.example 9418 \G/repo.git \Vfoo.host.example" (with vhost)
or "ext::nc host.example 9418 \G/repo.git" (without vhost).

Ilari Liusvaara (2):
  New remote helper git-remote-fd
  New remote helper: git-remote-ext

 Makefile             |    2 +
 builtin.h            |    2 +
 builtin/remote-ext.c |  300 ++++++++++++++++++++++++++++++++++++++++++++++++++
 builtin/remote-fd.c  |   88 +++++++++++++++
 git.c                |    2 +
 transport-helper.c   |  130 ++++++++++++++++++++++
 transport.h          |    1 +
 7 files changed, 525 insertions(+), 0 deletions(-)
 create mode 100644 builtin/remote-ext.c
 create mode 100644 builtin/remote-fd.c

[RFC PATCH v2 1/2] New remote helper git-remote-fd

From: Ilari Liusvaara <hidden>
Date: 2016-06-15 22:49:01

This remote helper reflects the outgoing smart transport stream back to
caller. This is useful if some frontend wants to handle the transport
stream for some reason (e.g. offering more options for SSH).
---
 Makefile            |    1 +
 builtin.h           |    1 +
 builtin/remote-fd.c |   88 ++++++++++++++++++++++++++++++++++
 git.c               |    1 +
 transport-helper.c  |  130 +++++++++++++++++++++++++++++++++++++++++++++++++++
 transport.h         |    1 +
 6 files changed, 222 insertions(+), 0 deletions(-)
 create mode 100644 builtin/remote-fd.c
diff --git a/Makefile b/Makefile
index 5fa893c..ad53b52 100644
--- a/Makefile
+++ b/Makefile
@@ -702,6 +702,7 @@ BUILTIN_OBJS += builtin/read-tree.o
 BUILTIN_OBJS += builtin/receive-pack.o
 BUILTIN_OBJS += builtin/reflog.o
 BUILTIN_OBJS += builtin/remote.o
+BUILTIN_OBJS += builtin/remote-fd.o
 BUILTIN_OBJS += builtin/replace.o
 BUILTIN_OBJS += builtin/rerere.o
 BUILTIN_OBJS += builtin/reset.o
diff --git a/builtin.h b/builtin.h
index 5c887ef..af60b28 100644
--- a/builtin.h
+++ b/builtin.h
@@ -138,5 +138,6 @@ extern int cmd_verify_pack(int argc, const char **argv, const char *prefix);
 extern int cmd_show_ref(int argc, const char **argv, const char *prefix);
 extern int cmd_pack_refs(int argc, const char **argv, const char *prefix);
 extern int cmd_replace(int argc, const char **argv, const char *prefix);
+extern int cmd_remote_fd(int argc, const char **argv, const char *prefix);
 
 #endif
diff --git a/builtin/remote-fd.c b/builtin/remote-fd.c
new file mode 100644
index 0000000..08ff522
--- /dev/null
+++ b/builtin/remote-fd.c
@@ -0,0 +1,88 @@
+#include "git-compat-util.h"
+#include "transport.h"
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <unistd.h>
+
+
+/*
+ * URL syntax:
+ *	'fd::<inoutfd>[/<anything>]'		Read/write socket pair
+ *						<inoutfd>.
+ *	'fd::<infd>,<outfd>[/<anything>]'	Read pipe <infd> and write
+ *						pipe <outfd>.
+ *	[foo] indicates 'foo' is optional. <anything> is any string.
+ *
+ * The data output to <outfd>/<inoutfd> should be passed unmolested to
+ * git-receive-pack/git-upload-pack/git-upload-archive and output of
+ * git-receive-pack/git-upload-pack/git-upload-archive should be passed
+ * unmolested to <infd>/<inoutfd>.
+ *
+ */
+
+int input_fd = -1;
+int output_fd = -1;
+
+#define MAXCOMMAND 4096
+
+static int command_loop()
+{
+	char buffer[MAXCOMMAND];
+
+	while (1) {
+		if (!fgets(buffer, MAXCOMMAND - 1, stdin))
+			exit(0);
+		//Strip end of line characters.
+		while (isspace((unsigned char)buffer[strlen(buffer) - 1]))
+			buffer[strlen(buffer) - 1] = 0;
+
+		if (!strcmp(buffer, "capabilities")) {
+			printf("*connect\n\n");
+			fflush(stdout);
+		} else if (!strncmp(buffer, "connect ", 8)) {
+			printf("\n");
+			fflush(stdout);
+			return bidirectional_transfer_loop(input_fd,
+				output_fd);
+		} else {
+			fprintf(stderr, "Bad command");
+			return 1;
+		}
+	}
+}
+
+int cmd_remote_fd(int argc, const char **argv, const char *prefix)
+{
+	char* end;
+	unsigned long r;
+
+	if (argc < 3) {
+		fprintf(stderr, "Error: URL missing");
+		exit(1);
+	}
+
+	r = strtoul(argv[2], &end, 10);
+	input_fd = (int)r;
+
+	if ((*end != ',' && *end !='/' && *end) || end == argv[2]) {
+		fprintf(stderr, "Error: Bad URL syntax");
+		exit(1);
+	}
+
+	if (*end == '/' || !*end) {
+		output_fd = input_fd;
+	} else {
+		char* end2;
+		r = strtoul(end + 1, &end2, 10);
+		output_fd = (int)r;
+
+		if ((*end2 !='/' && *end2) || end2 == end + 1) {
+			fprintf(stderr, "Error: Bad URL syntax");
+			exit(1);
+		}
+	}
+
+	return command_loop();
+}
diff --git a/git.c b/git.c
index 99f0363..91de3d6 100644
--- a/git.c
+++ b/git.c
@@ -368,6 +368,7 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "receive-pack", cmd_receive_pack },
 		{ "reflog", cmd_reflog, RUN_SETUP },
 		{ "remote", cmd_remote, RUN_SETUP },
+		{ "remote-fd", cmd_remote_fd, 0 },
 		{ "replace", cmd_replace, RUN_SETUP },
 		{ "repo-config", cmd_config },
 		{ "rerere", cmd_rerere, RUN_SETUP },
diff --git a/transport-helper.c b/transport-helper.c
index 0381de5..e07aa40 100644
--- a/transport-helper.c
+++ b/transport-helper.c
@@ -862,3 +862,133 @@ int transport_helper_init(struct transport *transport, const char *name)
 	transport->smart_options = &(data->transport_options);
 	return 0;
 }
+
+
+#define BUFFERSIZE 4096
+
+/* Copy data from stdin to output and from input to stdout. */
+int bidirectional_transfer_loop(int input, int output)
+{
+	struct pollfd polls[4];
+	char in_buffer[BUFFERSIZE];
+	char out_buffer[BUFFERSIZE];
+	size_t in_buffer_use = 0;
+	size_t out_buffer_use = 0;
+	int in_hup = 0;
+	int out_hup = 0;
+	int socket_mode = 0;
+	int input_index = 2;
+	int output_index = 3;
+	int poll_count = 4;
+
+	if(input == output) {
+		output_index = input_index;
+		poll_count = 3;
+		socket_mode = 1;
+	}
+
+	while(in_buffer_use || out_buffer_use || !in_hup || !out_hup) {
+		int r, i;
+		/* Set up the poll and do it. */
+		polls[0].fd = 0;
+		polls[1].fd = 1;
+		polls[input_index].fd = input;
+		polls[output_index].fd = output;
+		for(i = 0; i < 4; i++)
+			polls[i].events = polls[i].revents = 0;
+
+		if(in_buffer_use > 0)
+			polls[output_index].events |= POLLOUT;
+		if(in_buffer_use < BUFFERSIZE && !in_hup)
+			polls[0].events |= POLLIN;
+		if(out_buffer_use > 0)
+			polls[1].events |= POLLOUT;
+		if(out_buffer_use < BUFFERSIZE && !out_hup)
+			polls[input_index].events |= POLLIN;
+		r = poll(polls, poll_count, -1);
+		if(r < 0) {
+			if(errno == EWOULDBLOCK || errno == EAGAIN ||
+				errno == EINTR)
+				continue;
+			perror("poll failed");
+			return 1;
+		} else if(r == 0)
+			continue;
+
+		/* Something interesting has happened... */
+		if(polls[0].revents & (POLLIN | POLLHUP)) {
+			/* Stdin is readable. */
+			r = read(0, in_buffer + in_buffer_use, BUFFERSIZE -
+				in_buffer_use);
+			if(r < 0 && errno != EWOULDBLOCK && errno != EAGAIN &&
+				errno != EINTR) {
+				perror("read(git) failed");
+				return 1;
+			} else if(r == 0) {
+				in_hup = 1;
+				if(!in_buffer_use) {
+					if(socket_mode)
+						shutdown(output, SHUT_WR);
+					else
+						close(output);
+				}
+			} else
+				in_buffer_use += r;
+		}
+
+		if(polls[input_index].revents & (POLLIN | POLLHUP)) {
+			/* Connection is readable. */
+			r = read(input, out_buffer + out_buffer_use,
+				BUFFERSIZE - out_buffer_use);
+			if(r < 0 && errno != EWOULDBLOCK && errno != EAGAIN &&
+				errno != EINTR) {
+				perror("read(connection) failed");
+				return 1;
+			} else if(r == 0) {
+				out_hup = 1;
+				if(!out_buffer_use)
+					close(1);
+			} else
+				out_buffer_use += r;
+		}
+
+		if(polls[1].revents & POLLOUT) {
+			/* Stdout is writable. */
+			r = write(1, out_buffer, out_buffer_use);
+			if(r < 0 && errno != EWOULDBLOCK && errno != EAGAIN &&
+				errno != EINTR) {
+				perror("write(git) failed");
+				return 1;
+			} else {
+				out_buffer_use -= r;
+				if(out_buffer_use > 0)
+					memmove(out_buffer, out_buffer + r,
+						out_buffer_use);
+				if(out_hup && !out_buffer_use)
+					close(1);
+			}
+		}
+
+		if(polls[output_index].revents & POLLOUT) {
+			/* Connection is writable. */
+			r = write(output, in_buffer, in_buffer_use);
+			if(r < 0 && errno != EWOULDBLOCK && errno != EAGAIN &&
+				errno != EINTR) {
+				perror("write(connection) failed");
+				return 1;
+			} else {
+				in_buffer_use -= r;
+				if(in_buffer_use > 0)
+					memmove(in_buffer, in_buffer + r,
+						in_buffer_use);
+				if(in_hup && !in_buffer_use) {
+					if(socket_mode)
+						shutdown(output, SHUT_WR);
+					else
+						close(output);
+				}
+			}
+		}
+	}
+	return 0;
+}
diff --git a/transport.h b/transport.h
index c59d973..e803c0e 100644
--- a/transport.h
+++ b/transport.h
@@ -154,6 +154,7 @@ int transport_connect(struct transport *transport, const char *name,
 
 /* Transport methods defined outside transport.c */
 int transport_helper_init(struct transport *transport, const char *name);
+int bidirectional_transfer_loop(int input, int output);
 
 /* common methods used by transport.c and builtin-send-pack.c */
 void transport_verify_remote_names(int nr_heads, const char **heads);
-- 
1.7.1.335.g93705.dirty

[RFC PATCH v2 2/2] New remote helper: git-remote-ext

From: Ilari Liusvaara <hidden>
Date: 2016-06-15 22:49:01

Invokes specified command and directs smart transport streams to its
stdin/stdout. Handy for e.g. invoking ssh with some one-off parameters.
---
 Makefile             |    1 +
 builtin.h            |    1 +
 builtin/remote-ext.c |  300 ++++++++++++++++++++++++++++++++++++++++++++++++++
 git.c                |    1 +
 4 files changed, 303 insertions(+), 0 deletions(-)
 create mode 100644 builtin/remote-ext.c
diff --git a/Makefile b/Makefile
index ad53b52..88e752f 100644
--- a/Makefile
+++ b/Makefile
@@ -702,6 +702,7 @@ BUILTIN_OBJS += builtin/read-tree.o
 BUILTIN_OBJS += builtin/receive-pack.o
 BUILTIN_OBJS += builtin/reflog.o
 BUILTIN_OBJS += builtin/remote.o
+BUILTIN_OBJS += builtin/remote-ext.o
 BUILTIN_OBJS += builtin/remote-fd.o
 BUILTIN_OBJS += builtin/replace.o
 BUILTIN_OBJS += builtin/rerere.o
diff --git a/builtin.h b/builtin.h
index af60b28..eb9074d 100644
--- a/builtin.h
+++ b/builtin.h
@@ -139,5 +139,6 @@ extern int cmd_show_ref(int argc, const char **argv, const char *prefix);
 extern int cmd_pack_refs(int argc, const char **argv, const char *prefix);
 extern int cmd_replace(int argc, const char **argv, const char *prefix);
 extern int cmd_remote_fd(int argc, const char **argv, const char *prefix);
+extern int cmd_remote_ext(int argc, const char **argv, const char *prefix);
 
 #endif
diff --git a/builtin/remote-ext.c b/builtin/remote-ext.c
new file mode 100644
index 0000000..e9852ca
--- /dev/null
+++ b/builtin/remote-ext.c
@@ -0,0 +1,300 @@
+#include "git-compat-util.h"
+#include "transport.h"
+#include "run-command.h"
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <unistd.h>
+
+/*
+ * URL syntax:
+ *	'command [arg1 [arg2 [...]]]'	Invoke command with given arguments.
+ *	Special characters:
+ *	'\ ': Literal space in argument.
+ *	'\\': Literal backslash.
+ *	'\S': Name of service (git-upload-pack/git-upload-archive/
+ *		git-receive-pack.
+ *	'\s': Same as \s, but with possible git- prefix stripped.
+ *	'\G': Only allowed as first 'character' of argument. Do not pass this
+ *		Argument to command, instead send this as name of repository
+ *		in in-line git://-style request (also activates sending this
+ *		style of request).
+ *	'\V': Only allowed as first 'character' of argument. Used in
+ *		conjunction with '\G': Do not pass this argument to command,
+ *		instead send this as vhost in git://-style request (note: does
+ *		not activate sending git:// style request).
+ */
+
+char* git_req = NULL;
+char* git_req_vhost = NULL;
+
+static char *strip_escapes(const char *str, const char *service,
+	const char **next)
+{
+	char *ret;
+	size_t rpos = 0;
+	size_t wpos = 0;
+	size_t finallen = 0;
+	int escape = 0;
+	char special = 0;
+	size_t pslen = 0;
+	size_t pSlen = 0;
+	size_t psoff = 0;
+
+	/* Calculate prefix length for \s and lengths for \s and \S */
+	if (!strncmp(service, "git-", 4))
+		psoff = 4;
+	pSlen = strlen(service);
+	pslen = pSlen - psoff;
+
+	/* Calculate output length and start of next argument. */
+	while (str[rpos] && (escape || str[rpos] != ' ')) {
+		if (escape) {
+			switch (str[rpos]) {
+			case ' ':
+			case '\\':
+				finallen++;
+				break;
+			case 's':
+				finallen += pslen;
+				break;
+			case 'S':
+				finallen += pSlen;
+				break;
+			case 'G':
+			case 'V':
+				special = str[rpos];
+				if (rpos == 1)
+					break;
+				/* Fall-through to error. */
+			default:
+				die("Bad remote-ext placeholder '\\%c'.",
+					str[rpos]);
+			}
+			escape = 0;
+		} else
+			switch (str[rpos]) {
+			case '\\':
+				escape = 1;
+				break;
+			default:
+				finallen++;
+				break;
+			}
+		rpos++;
+	}
+	if (escape && !str[rpos])
+		die("remote-ext command has incomplete placeholder");
+	*next = str + rpos;
+	if (**next == ' ')
+		++*next;	/* Skip over space */
+
+	/*
+	 * Do the actual placeholder substitution. The string will be short
+	 * enough not to overflow integers.
+	 */
+	ret = xmalloc(finallen + 1);
+	rpos = special ? 2 : 0;		/* Skip first 2 bytes in specials. */
+	escape = 0;
+	while (str[rpos] && (escape || str[rpos] != ' ')) {
+		if (escape) {
+			switch(str[rpos]) {
+			case ' ':
+			case '\\':
+				ret[wpos++] = str[rpos];
+				break;
+			case 's':
+				strcpy(ret + wpos, service + psoff);
+				wpos += pslen;
+				break;
+			case 'S':
+				strcpy(ret + wpos, service);
+				wpos += pSlen;
+				break;
+			}
+			escape = 0;
+		} else
+			switch(str[rpos]) {
+			case '\\':
+				escape = 1;
+				break;
+			default:
+				ret[wpos++] = str[rpos];
+				break;
+			}
+		rpos++;
+	}
+	ret[wpos] = 0;
+	switch(special) {
+	case 'G':
+		git_req = ret;
+		return NULL;
+	case 'V':
+		git_req_vhost = ret;
+		return NULL;
+	default:
+		return ret;
+	}
+}
+
+/* Should be enough... */
+#define MAXARGUMENTS 256
+
+static const char **parse_argv(const char *arg, const char *service)
+{
+	int arguments = 0;
+	int i;
+	char** ret;
+	char *(temparray[MAXARGUMENTS + 1]);
+
+	while (*arg) {
+		char* ret;
+		if (arguments == MAXARGUMENTS)
+			die("remote-ext command has too many arguments");
+		ret = strip_escapes(arg, service, &arg);
+		if (ret)
+			temparray[arguments++] = ret;
+	}
+
+	ret = xcalloc(arguments + 1, sizeof(char*));
+	for (i = 0; i < arguments; i++)
+		ret[i] = temparray[i];
+
+	return (const char**)ret;
+}
+
+static void send_git_request(int stdin_fd, const char *serv, const char *repo,
+	const char *vhost)
+{
+	size_t bufferspace;
+	size_t wpos = 0;
+	size_t spos = 0;
+	size_t tmp;
+	char* buffer;
+
+	/*
+	 * Request needs 12 bytes extra if there is vhost (xxxx \0host=\0) and
+	 * 6 bytes extra (xxxx \0) if there is no vhost.
+	 */
+	if (vhost)
+		bufferspace = strlen(serv) + strlen(repo) + strlen(vhost) + 12;
+	else
+		bufferspace = strlen(serv) + strlen(repo) + 6;
+
+	if (bufferspace > 0xFFFF)
+		die("Request too large to send");
+	buffer = xmalloc(bufferspace);
+
+	/* Packet length. */
+	sprintf(buffer + wpos, "%04x", (unsigned)bufferspace);
+	wpos += 4;
+
+	/* Service. */
+	tmp = strlen(serv);
+	memcpy(buffer + wpos, serv, tmp);
+	wpos += tmp;
+
+	/* Space. */
+	buffer[wpos++] = ' ';
+
+	/* Repo. */
+	tmp = strlen(repo);
+	memcpy(buffer + wpos, repo, tmp);
+	wpos += tmp;
+
+	/* NUL. */
+	buffer[wpos++] = '\0';
+
+	/* Vhost if any. */
+	if (vhost) {
+		/* Header name. */
+		strcpy(buffer + wpos, "host=");
+		wpos += 5;
+
+		/* Actual vhost */
+		tmp = strlen(vhost);
+		memcpy(buffer + wpos, vhost, tmp);
+		wpos += tmp;
+
+		/* NUL. */
+		buffer[wpos++] = '\0';
+	}
+
+	/* Send the request */
+	while (spos < wpos) {
+		ssize_t r;
+		r = write(stdin_fd, buffer + spos, wpos - spos);
+		if (r < 0 && errno != EINTR && errno != EAGAIN &&
+			errno != EWOULDBLOCK)
+			die_errno("Failed to send request");
+		else if (r < 0)
+			continue;	/* Try again. */
+		else
+			spos += r;
+	}
+
+	free(buffer);
+}
+
+static int run_child(const char *arg, const char *service)
+{
+	int r;
+	struct child_process child;
+
+	memset(&child, 0, sizeof(child));
+	child.in = -1;
+	child.out = -1;
+	child.err = 0;
+	child.argv = parse_argv(arg, service);
+
+	if (start_command(&child) < 0)
+		die("Can't run specified command");
+
+	if (git_req)
+		send_git_request(child.in, service, git_req, git_req_vhost);
+
+	r = bidirectional_transfer_loop(child.out, child.in);
+	if (!r)
+		r = finish_command(&child);
+	else
+		finish_command(&child);
+	return r;
+}
+
+#define MAXCOMMAND 4096
+
+static int command_loop(const char *child)
+{
+	char buffer[MAXCOMMAND];
+
+	while (1) {
+		if (!fgets(buffer, MAXCOMMAND - 1, stdin))
+			exit(0);
+		//Strip end of line characters.
+		while (isspace((unsigned char)buffer[strlen(buffer) - 1]))
+			buffer[strlen(buffer) - 1] = 0;
+
+		if (!strcmp(buffer, "capabilities")) {
+			printf("*connect\n\n");
+			fflush(stdout);
+		} else if (!strncmp(buffer, "connect ", 8)) {
+			printf("\n");
+			fflush(stdout);
+			return run_child(child, buffer + 8);
+		} else {
+			fprintf(stderr, "Bad command");
+			return 1;
+		}
+	}
+}
+
+int cmd_remote_ext(int argc, const char **argv, const char *prefix)
+{
+	if (argc < 3) {
+		fprintf(stderr, "Error: URL missing");
+		exit(1);
+	}
+
+	return command_loop(argv[2]);
+}
diff --git a/git.c b/git.c
index 91de3d6..2cc0826 100644
--- a/git.c
+++ b/git.c
@@ -368,6 +368,7 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "receive-pack", cmd_receive_pack },
 		{ "reflog", cmd_reflog, RUN_SETUP },
 		{ "remote", cmd_remote, RUN_SETUP },
+		{ "remote-ext", cmd_remote_ext, 0 },
 		{ "remote-fd", cmd_remote_fd, 0 },
 		{ "replace", cmd_replace, RUN_SETUP },
 		{ "repo-config", cmd_config },
-- 
1.7.1.335.g93705.dirty
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help