threads / patch / 5367

patchgit-daemon virtual hosting implementation.

Subject: [PATCH] git-daemon virtual hosting implementation.

## tl;dr

16 messages between Aug 23, 2006 and Aug 27, 2006. Diffs are folded; open one to read it.

replies: 15people: 6as markdown or json

Pierre Habouzit· Aug 23, 2006, 18:52 UTC · lore

just add the hostname in the path when using --base-path and --user-path. this should be enough for most needs.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
 Here is a proposal for daemon side virtualhosting support.
 This is quite simple, and just uses the hostname in the path where it looks
 for the git repository. Only works in conjuction of --base-path or
 --user-path btw.
 Documentation/git-daemon.txt |   12 +++++++
 daemon.c                     |   73 +++++++++++++++++++++++++++++++++++-------
 2 files changed, 73 insertions(+), 12 deletions(-)
Show changes to 2 files +73 −12

Documentation/git-daemon.txt, daemon.c

diff --git a/Documentation/git-daemon.txt b/Documentation/git-daemon.txt
index 0f7d274..ab5b232 100644
--- a/Documentation/git-daemon.txt
+++ b/Documentation/git-daemon.txt
@@ -79,6 +79,18 @@ OPTIONS
 	taken as a request to access `path/foo` repository in
 	the home directory of user `alice`.
 
+--use-vhosts::
+	Use git daemon cheap virtual hosting scheme.
++
+Used with --base-path=/foo, it will search the repositories in /foo/HOSTNAME
+instead of /foo - if you try to pull git://git.example.com/hello.git, it will
+search into /foo/git.example.com/hello.git.
++
+When using --user-path the HOSTNAME is appended to the path as well, so for
+--user-path=some/path, request to git://git.example.com/~alice/foo is taken as
+a request to access some/path/git.example.com/foo in the home directory of
+user `alice`.
+
 --verbose::
 	Log details about the incoming connections and requested files.
 
diff --git a/daemon.c b/daemon.c
index 012936f..146482c 100644
--- a/daemon.c
+++ b/daemon.c
@@ -17,7 +17,7 @@ static int reuseaddr;
 
 static const char daemon_usage[] =
 "git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all]\n"
-"           [--timeout=n] [--init-timeout=n] [--strict-paths]\n"
+"           [--timeout=n] [--init-timeout=n] [--use-vhosts] [--strict-paths]\n"
 "           [--base-path=path] [--user-path | --user-path=path]\n"
 "           [--reuseaddr] [--detach] [--pid-file=file] [directory...]";
 
@@ -25,6 +25,8 @@ static const char daemon_usage[] =
 static char **ok_paths;
 static int strict_paths;
 
+static int use_vhosts;
+
 /* If this is set, git-daemon-export-ok is not required */
 static int export_all_trees;
 
@@ -148,7 +150,7 @@ static int avoid_alias(char *p)
 	}
 }
 
-static char *path_ok(char *dir)
+static char *path_ok(char *dir, char *vhost)
 {
 	static char rpath[PATH_MAX];
 	char *path;
@@ -158,6 +160,11 @@ static char *path_ok(char *dir)
 		return NULL;
 	}
 
+	if (use_vhosts && !vhost) {
+		logerror("using virtual hosting, and not host= was specified !");
+		return NULL;
+	}
+
 	if (*dir == '~') {
 		if (!user_path) {
 			logerror("'%s': User-path not allowed", dir);
@@ -174,9 +181,25 @@ static char *path_ok(char *dir)
 				slash = dir + restlen;
 			namlen = slash - dir;
 			restlen -= namlen;
-			loginfo("userpath <%s>, request <%s>, namlen %d, restlen %d, slash <%s>", user_path, dir, namlen, restlen, slash);
-			snprintf(rpath, PATH_MAX, "%.*s/%s%.*s",
-				 namlen, dir, user_path, restlen, slash);
+
+			if (use_vhosts) {
+				loginfo("host <%s>, "
+					"userpath <%s>, request <%s>, "
+					"namlen %d, restlen %d, slash <%s>",
+					vhost,
+					user_path, dir,
+					namlen, restlen, slash);
+				snprintf(rpath, PATH_MAX, "%.*s/%s/%s%.*s",
+					 namlen, dir, user_path, vhost,
+					 restlen, slash);
+			} else {
+				loginfo("userpath <%s>, request <%s>, "
+					"namlen %d, restlen %d, slash <%s>",
+					user_path, dir,
+					namlen, restlen, slash);
+				snprintf(rpath, PATH_MAX, "%.*s/%s%.*s",
+					 namlen, dir, user_path, restlen, slash);
+			}
 			dir = rpath;
 		}
 	}
@@ -187,7 +210,11 @@ static char *path_ok(char *dir)
 			return NULL;
 		}
 		else {
-			snprintf(rpath, PATH_MAX, "%s%s", base_path, dir);
+			if (use_vhosts) {
+				snprintf(rpath, PATH_MAX, "%s/%s%s", base_path, vhost, dir);
+			} else {
+				snprintf(rpath, PATH_MAX, "%s%s", base_path, dir);
+			}
 			dir = rpath;
 		}
 	}
@@ -229,15 +256,19 @@ static char *path_ok(char *dir)
 	return NULL;		/* Fallthrough. Deny by default */
 }
 
-static int upload(char *dir)
+static int upload(char *dir, char *vhost)
 {
 	/* Timeout as string */
 	char timeout_buf[64];
 	const char *path;
 
-	loginfo("Request for '%s'", dir);
+	if (vhost) {
+		loginfo("Request for '%s' (host = %s)", dir, vhost);
+	} else {
+		loginfo("Request for '%s'", dir);
+	}
 
-	if (!(path = path_ok(dir)))
+	if (!(path = path_ok(dir, vhost)))
 		return -1;
 
 	/*
@@ -274,6 +305,7 @@ static int execute(struct sockaddr *addr
 {
 	static char line[1000];
 	int pktlen, len;
+        char *vhost = NULL;
 
 	if (addr) {
 		char addrbuf[256] = "";
@@ -303,15 +335,28 @@ #endif
 	alarm(0);
 
 	len = strlen(line);
-	if (pktlen != len)
+	if (pktlen != len) {
+		int arg_pos = len + 1;
+
 		loginfo("Extended attributes (%d bytes) exist <%.*s>",
 			(int) pktlen - len,
-			(int) pktlen - len, line + len + 1);
+			(int) pktlen - len, line + arg_pos);
+
+		while (arg_pos < pktlen) {
+			int arg_len = strlen(line + arg_pos);
+
+			if (!strncmp("host=", line + arg_pos, 5)) {
+				vhost = line + arg_pos + 5;
+			}
+
+			arg_pos += arg_len + 1;
+		}
+	}
 	if (len && line[len-1] == '\n')
 		line[--len] = 0;
 
 	if (!strncmp("git-upload-pack ", line, 16))
-		return upload(line+16);
+		return upload(line+16, vhost);
 
 	logerror("Protocol error: '%s'", line);
 	return -1;
@@ -762,6 +807,10 @@ int main(int argc, char **argv)
 			init_timeout = atoi(arg+15);
 			continue;
 		}
+		if (!strcmp(arg, "--use-vhosts")) {
+			use_vhosts = 1;
+			continue;
+		}
 		if (!strcmp(arg, "--strict-paths")) {
 			strict_paths = 1;
 			continue;
-- 
1.4.1.1
Junio C Hamano· Aug 23, 2006, 20:11 UTC · re: Pierre Habouzit · lore

Re: [PATCH] git-daemon virtual hosting implementation.

Pierre Habouzit <madcoder@debian.org> writes:
Show 6 quoted lines
> just add the hostname in the path when using --base-path and --user-path.
> this should be enough for most needs.
>
> Signed-off-by: Pierre Habouzit <madcoder@debian.org>
> ---
>  Here is a proposal for daemon side virtualhosting support.
Show 9 quoted lines
> @@ -158,6 +160,11 @@ static char *path_ok(char *dir)
>  		return NULL;
>  	}
>  
> +	if (use_vhosts && !vhost) {
> +		logerror("using virtual hosting, and not host= was specified !");
> +		return NULL;
> +	}
> +

This part is objectionable -- older clients do not give "host=". I think the plan, when virtual hosting was proposed and we added this to the client side first, was to treat older clients as if they specified the "primary" host. So we would need some mechanism to say where the repositories of the "primary" host lives.

Show 10 quoted lines
> +			if (use_vhosts) {
> +				loginfo("host <%s>, "
> +					"userpath <%s>, request <%s>, "
> +					"namlen %d, restlen %d, slash <%s>",
> +					vhost,
> +					user_path, dir,
> +					namlen, restlen, slash);
> +				snprintf(rpath, PATH_MAX, "%.*s/%s/%s%.*s",
> +					 namlen, dir, user_path, vhost,
> +					 restlen, slash);

I am not sure if the interaction between user-path and vhost should go like this, but I do not think of a good alternative to suggest right now. Your code allows ~user/host1 and ~user/host2 to host different set of repositories, but I suspect if somebody is setting up a virtual hosting of two hosts, he might want to have two distinct set of users (iow to pretend that ~user exist only on host1 but not on host2). I dunno.

Pierre Habouzit· Aug 23, 2006, 20:56 UTC · re: Junio C Hamano · lore

Re: [PATCH] git-daemon virtual hosting implementation.

Le mer 23 août 2006 22:11, Junio C Hamano a écrit :
Show 24 quoted lines
> Pierre Habouzit <madcoder@debian.org> writes:
> > just add the hostname in the path when using --base-path and
> > --user-path. this should be enough for most needs.
> >
> > Signed-off-by: Pierre Habouzit <madcoder@debian.org>
> > ---
> >  Here is a proposal for daemon side virtualhosting support.
> >
> > @@ -158,6 +160,11 @@ static char *path_ok(char *dir)
> >  		return NULL;
> >  	}
> >
> > +	if (use_vhosts && !vhost) {
> > +		logerror("using virtual hosting, and not host= was specified
> > !"); +		return NULL;
> > +	}
> > +
>
> This part is objectionable -- older clients do not give "host=".
> I think the plan, when virtual hosting was proposed and we added
> this to the client side first, was to treat older clients as if
> they specified the "primary" host.  So we would need some
> mechanism to say where the repositories of the "primary" host
> lives.
fair enough, so I suppose there is many choices:
 * replace --use-vhosts with --use-vhosts=${default hostname}
   but I do not like it very much
 * use `hostname -f` as the default hostname (not very nice either)
 * use the magic _default_ (à la apache) hostname ?

But just note that if a git repository is accessible on an host that is not the default, and only on that one, an older client won't be able to clone the repository anyway, because he will be redirected to the default virtual host. Meaning, that someone that uses virtual hosts will break older clients anyway.

what would be nicer would be a way to gracefully reject older clients in that case with an understandable error message, so that the user knows why it does not work. I don't know PROTO_GIT very well yet,so I don't know if older client support such communications yet or not.

Show 18 quoted lines
> > +			if (use_vhosts) {
> > +				loginfo("host <%s>, "
> > +					"userpath <%s>, request <%s>, "
> > +					"namlen %d, restlen %d, slash <%s>",
> > +					vhost,
> > +					user_path, dir,
> > +					namlen, restlen, slash);
> > +				snprintf(rpath, PATH_MAX, "%.*s/%s/%s%.*s",
> > +					 namlen, dir, user_path, vhost,
> > +					 restlen, slash);
>
> I am not sure if the interaction between user-path and vhost
> should go like this, but I do not think of a good alternative to
> suggest right now.  Your code allows ~user/host1 and ~user/host2
> to host different set of repositories, but I suspect if somebody
> is setting up a virtual hosting of two hosts, he might want to
> have two distinct set of users (iow to pretend that ~user exist
> only on host1 but not on host2).  I dunno.
Another option would be not to support virtual hosts, but instead 
superseed the --base-path and --user-path with some --base-path-fmt 
and --user-path-fmt where the user can specify how to build the path 
with simple sprintf-like formats. One could e.g. support:
 * %% obviously ;
 * %h that will be replaced with the hostname
 * %u (only for --user-path-fmt)
 * %p (asked path)
 * ...

I think that's more clever, and allow more flexible use of the virtual hosting code. It e.g. allow to use the virtual host scheme for the `base-path` repos and to disallow it for the users.

--*-path and --*-path-fmt are obviously mutually exclusive.
What do you think ?
-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org
Jon Loeliger· Aug 24, 2006, 20:15 UTC · re: Pierre Habouzit · lore

Re: [PATCH] git-daemon virtual hosting implementation.

On Wed, 2006-08-23 at 15:56, Pierre Habouzit wrote:
Show 9 quoted lines
> Another option would be not to support virtual hosts, but instead 
> superseed the --base-path and --user-path with some --base-path-fmt 
> and --user-path-fmt where the user can specify how to build the path 
> with simple sprintf-like formats. One could e.g. support:
>  * %% obviously ;
>  * %h that will be replaced with the hostname
>  * %u (only for --user-path-fmt)
>  * %p (asked path)
>  * ...

And this is exactly what I have implemented and running on my system today!

Show 7 quoted lines
> I think that's more clever, and allow more flexible use of the virtual 
> hosting code. It e.g. allow to use the virtual host scheme for the 
> `base-path` repos and to disallow it for the users.
> 
> --*-path and --*-path-fmt are obviously mutually exclusive.
> 
> What do you think ?
I kinda like it... :-)
jdl
Junio C Hamano· Aug 24, 2006, 20:35 UTC · re: Jon Loeliger · lore

Re: [PATCH] git-daemon virtual hosting implementation.

Jon Loeliger <jdl@freescale.com> writes:
Show 14 quoted lines
> On Wed, 2006-08-23 at 15:56, Pierre Habouzit wrote:
>
>> Another option would be not to support virtual hosts, but instead 
>> superseed the --base-path and --user-path with some --base-path-fmt 
>> and --user-path-fmt where the user can specify how to build the path 
>> with simple sprintf-like formats. One could e.g. support:
>>  * %% obviously ;
>>  * %h that will be replaced with the hostname
>>  * %u (only for --user-path-fmt)
>>  * %p (asked path)
>>  * ...
>
> And this is exactly what I have implemented and
> running on my system today!
Where is the patch ;-)?
Jon Loeliger· Aug 24, 2006, 20:34 UTC · re: Junio C Hamano · lore

Re: [PATCH] git-daemon virtual hosting implementation.

On Thu, 2006-08-24 at 15:35, Junio C Hamano wrote:
> Where is the patch ;-)?
Heh.

Well, I rewrote the interpolation code. As you pointed out earlier, it was crap. I can send that in; no problem.

There was some outstanding debate if this was actually the right way to go about things. Specifically, the problem of canonical hostname. A proposal was to use peer IP addresses instead, and I just haven't gotten around to messing with that yet. So that is why I hadn't sent it in yet. Sorry.

But, I'll rebase it and send it in this evening.
jdl
Pierre Habouzit· Aug 23, 2006, 23:32 UTC · re: Junio C Hamano · lore

[PATCH] git-daemon: more powerful base-path/user-path settings, using formats.

Allow a form of virtualhosting, when %h format is used.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
    This is intended to be a more flexible solution, that also gives virtual
    hosting as a bonus. I still see no way to deal with older clients when
    virtual hosting is used by the admin though, having a "default" hostname
    won't solve anything at all anyway.
 Documentation/git-daemon.txt |   42 ++++++++++
 daemon.c                     |  170 +++++++++++++++++++++++++++++++++++++++---
 2 files changed, 201 insertions(+), 11 deletions(-)
Show changes to 2 files +201 −11

Documentation/git-daemon.txt, daemon.c

diff --git a/Documentation/git-daemon.txt b/Documentation/git-daemon.txt
index 0f7d274..0aa34d4 100644
--- a/Documentation/git-daemon.txt
+++ b/Documentation/git-daemon.txt
@@ -11,6 +11,7 @@ SYNOPSIS
 'git-daemon' [--verbose] [--syslog] [--inetd | --port=n] [--export-all]
              [--timeout=n] [--init-timeout=n] [--strict-paths]
              [--base-path=path] [--user-path | --user-path=path]
+             [--base-path-fmt=pathfmt] [--user-path-fmt=pathfmt]
 	     [--reuseaddr] [--detach] [--pid-file=file] [directory...]
 
 DESCRIPTION
@@ -45,6 +46,10 @@ OPTIONS
 	'git://example.com/hello.git', `git-daemon` will interpret the path
 	as '/srv/git/hello.git'.
 
+--base-path-fmt=pathfmt::
+	Works like --base-path, but uses a format instead. See Path
+	Formats section below.
+
 --export-all::
 	Allow pulling from all directories that look like GIT repositories
 	(have the 'objects' and 'refs' subdirectories), even if they
@@ -79,6 +84,11 @@ OPTIONS
 	taken as a request to access `path/foo` repository in
 	the home directory of user `alice`.
 
+--user-path-fmt=pathfmt::
+	Allow ~user notation to be used in requests. The path used to look
+	for the git repository is a format string, see Path Formats
+	section below.
+
 --verbose::
 	Log details about the incoming connections and requested files.
 
@@ -98,6 +108,38 @@ OPTIONS
 	--strict-paths is specified this will also include subdirectories
 	of each named directory.
 
+Path Formats
+------------
+
+%h::
+	requested hostname (won't work with clients older than 1.4.0).
+
+%p::
+	requested path.
+
+%P::
+	requested path without the first slash when exists (`/`).
+
+%u::
+	requested username (only allowed for --user-path-fmt).
+
+%%::
+	plain '%'
+
+Examples
+~~~~~~~~
+
+* `--base-path-fmt=/srv/git/%P` emulates `--base-path=/srv/git`
+
+* `--base-path-fmt=/srv/git/%h%p` will look into
+  `/srv/git/git.example.com/foo` if the request goes to
+  git://git.example.com/foo
+
+* `--user-path-fmt=/home/%u/public_git/%P` works mostly like
+  `--user-path=public_git` (the difference is that it assumes that home
+  directories live in `/home/username` instead of using the real `username`
+  home).
+
 Author
 ------
 Written by Linus Torvalds <torvalds@osdl.org>, YOSHIFUJI Hideaki
diff --git a/daemon.c b/daemon.c
index 012936f..1ffebd6 100644
--- a/daemon.c
+++ b/daemon.c
@@ -19,6 +19,7 @@ static const char daemon_usage[] =
 "git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all]\n"
 "           [--timeout=n] [--init-timeout=n] [--strict-paths]\n"
 "           [--base-path=path] [--user-path | --user-path=path]\n"
+"           [--base-path-fmt=pathfmt] [--user-path-fmt=pathfmt]\n"
 "           [--reuseaddr] [--detach] [--pid-file=file] [directory...]";
 
 /* List of acceptable pathname prefixes */
@@ -29,12 +30,14 @@ static int strict_paths;
 static int export_all_trees;
 
 /* Take all paths relative to this one if non-NULL */
+int is_base_path_fmt;
 static char *base_path;
 
 /* If defined, ~user notation is allowed and the string is inserted
  * after ~user/.  E.g. a request to git://host/~alice/frotz would
  * go to /home/alice/pub_git/frotz with --user-path=pub_git.
  */
+int is_user_path_fmt;
 static const char *user_path;
 
 /* Timeout, and initial timeout */
@@ -148,7 +151,100 @@ static int avoid_alias(char *p)
 	}
 }
 
-static char *path_ok(char *dir)
+static void check_path_fmt(const char *check, int allow_user)
+{
+	const char *p = check;
+
+	while (*p) {
+		if (*p++ != '%')
+			continue;
+
+		switch (*p) {
+		  case '\0':
+		    die("invalid format: <%s> ends with an unescaped %%", check);
+
+		  case '%':
+		  case 'p': case 'P':
+		  case 'h':
+		    break;
+
+		  case 'u':
+		    if (allow_user)
+			    break;
+		    /* fallthrough */
+
+		  default:
+		    die("invalid format: <%s> uses unknown specifier %%%c",
+			check, *p);
+		}
+
+		p++;
+	}
+}
+
+static char *
+git_path_fmt(char rpath[PATH_MAX], const char *fmt,
+             const char *vhost, const char *path,
+             const char *username, int namlen)
+{
+	const char *p = fmt;
+	int pos = 0;
+
+	while (*p) {
+		if (*p != '%') {
+			rpath[pos++] = *p++;
+			continue;
+		}
+
+		switch (*++p) {
+		  case '%':
+		    rpath[pos++] = *p;
+		    break;
+
+		  case 'h':
+                    if (!vhost) {
+                        logerror("missing host=, client is too old");
+                        return NULL;
+                    }
+		    pos += strlcpy(rpath + pos, vhost, PATH_MAX - pos);
+		    break;
+
+		  case 'P':
+		    pos += strlcpy(rpath + pos, path + (*path == '/'),
+				   PATH_MAX - pos);
+		    break;
+
+		  case 'p':
+		    pos += strlcpy(rpath + pos, path, PATH_MAX - pos);
+		    break;
+
+		  case 'u':
+		    pos += snprintf(rpath + pos, PATH_MAX - pos, "%.*s",
+				    namlen, username);
+		    break;
+		}
+
+		p++;
+
+		if (pos >= PATH_MAX) {
+                        logerror("generated path is too long");
+			return NULL;
+		}
+	}
+
+	rpath[pos] = '\0';
+
+        return rpath;
+}
+
+static inline char *
+git_base_path_fmt(char rpath[PATH_MAX], const char *fmt,
+                  const char *vhost, const char *path)
+{
+    return git_path_fmt(rpath, fmt, vhost, path, NULL, 0);
+}
+
+static char *path_ok(char *dir, char *vhost)
 {
 	static char rpath[PATH_MAX];
 	char *path;
@@ -174,10 +270,25 @@ static char *path_ok(char *dir)
 				slash = dir + restlen;
 			namlen = slash - dir;
 			restlen -= namlen;
-			loginfo("userpath <%s>, request <%s>, namlen %d, restlen %d, slash <%s>", user_path, dir, namlen, restlen, slash);
-			snprintf(rpath, PATH_MAX, "%.*s/%s%.*s",
-				 namlen, dir, user_path, restlen, slash);
-			dir = rpath;
+
+			if (is_user_path_fmt) {
+				loginfo("host <%s>, "
+					"userpathfmt <%s>, request <%s>, "
+					"namlen %d, restlen %d, slash <%s>",
+					vhost,
+					user_path, dir,
+					namlen, restlen, slash);
+                                dir = git_path_fmt(rpath, user_path, vhost,
+                                                   slash, dir + 1, namlen - 1);
+			} else {
+				loginfo("userpath <%s>, request <%s>, "
+					"namlen %d, restlen %d, slash <%s>",
+					user_path, dir,
+					namlen, restlen, slash);
+				snprintf(rpath, PATH_MAX, "%.*s/%s%.*s",
+					 namlen, dir, user_path, restlen, slash);
+				dir = rpath;
+			}
 		}
 	}
 	else if (base_path) {
@@ -186,12 +297,18 @@ static char *path_ok(char *dir)
 			logerror("'%s': Non-absolute path denied (base-path active)", dir);
 			return NULL;
 		}
-		else {
+
+		if (is_base_path_fmt) {
+                        dir = git_base_path_fmt(rpath, base_path, vhost, dir);
+		} else {
 			snprintf(rpath, PATH_MAX, "%s%s", base_path, dir);
 			dir = rpath;
 		}
 	}
 
+	if (!dir)
+		return NULL;
+
 	path = enter_repo(dir, strict_paths);
 
 	if (!path) {
@@ -229,7 +346,7 @@ static char *path_ok(char *dir)
 	return NULL;		/* Fallthrough. Deny by default */
 }
 
-static int upload(char *dir)
+static int upload(char *dir, char *vhost)
 {
 	/* Timeout as string */
 	char timeout_buf[64];
@@ -237,7 +354,7 @@ static int upload(char *dir)
 
 	loginfo("Request for '%s'", dir);
 
-	if (!(path = path_ok(dir)))
+	if (!(path = path_ok(dir, vhost)))
 		return -1;
 
 	/*
@@ -274,6 +391,7 @@ static int execute(struct sockaddr *addr
 {
 	static char line[1000];
 	int pktlen, len;
+	char *vhost = NULL;
 
 	if (addr) {
 		char addrbuf[256] = "";
@@ -303,15 +421,30 @@ #endif
 	alarm(0);
 
 	len = strlen(line);
-	if (pktlen != len)
+
+	if (pktlen != len) {
+		int arg_pos = len + 1;
+
 		loginfo("Extended attributes (%d bytes) exist <%.*s>",
 			(int) pktlen - len,
-			(int) pktlen - len, line + len + 1);
+			(int) pktlen - len, line + arg_pos);
+
+		while (arg_pos < pktlen) {
+			int arg_len = strlen(line + arg_pos);
+
+			if (!strncmp("host=", line + arg_pos, 5)) {
+				vhost = line + arg_pos + 5;
+			}
+
+			arg_pos += arg_len + 1;
+		}
+	}
+
 	if (len && line[len-1] == '\n')
 		line[--len] = 0;
 
 	if (!strncmp("git-upload-pack ", line, 16))
-		return upload(line+16);
+		return upload(line+16, vhost);
 
 	logerror("Protocol error: '%s'", line);
 	return -1;
@@ -768,6 +901,13 @@ int main(int argc, char **argv)
 		}
 		if (!strncmp(arg, "--base-path=", 12)) {
 			base_path = arg+12;
+			is_base_path_fmt = 0;
+			continue;
+		}
+		if (!strncmp(arg, "--base-path-fmt=", 16)) {
+			base_path = arg+16;
+			is_base_path_fmt = 1;
+			check_path_fmt(base_path, 0);
 			continue;
 		}
 		if (!strcmp(arg, "--reuseaddr")) {
@@ -776,10 +916,18 @@ int main(int argc, char **argv)
 		}
 		if (!strcmp(arg, "--user-path")) {
 			user_path = "";
+			is_user_path_fmt = 0;
 			continue;
 		}
 		if (!strncmp(arg, "--user-path=", 12)) {
 			user_path = arg + 12;
+			is_user_path_fmt = 0;
+			continue;
+		}
+		if (!strncmp(arg, "--user-path-fmt=", 16)) {
+			user_path = arg + 16;
+			is_user_path_fmt = 1;
+			check_path_fmt(user_path, 1);
 			continue;
 		}
 		if (!strncmp(arg, "--pid-file=", 11)) {
-- 
1.4.2.g44c10-dirty
Junio C Hamano· Aug 24, 2006, 00:17 UTC · re: Pierre Habouzit · lore

Re: [PATCH] git-daemon: more powerful base-path/user-path settings, using formats.

Pierre Habouzit <madcoder@debian.org> writes:
Show 9 quoted lines
> Allow a form of virtualhosting, when %h format is used.
>
> Signed-off-by: Pierre Habouzit <madcoder@debian.org>
> ---
>
>     This is intended to be a more flexible solution, that also gives virtual
>     hosting as a bonus. I still see no way to deal with older clients when
>     virtual hosting is used by the admin though, having a "default" hostname
>     won't solve anything at all anyway.

I mildly disagree about the last sentence. Enabling virtual hosting does not have to mean all virtual hosts are treated equal. It is conceivable that a site hosts the primary, "collection of public repositories everybody would want to go to" set, with supplemental ones for specific audiences that are done via virtual hosting. General public who would want to access the primary one can come with older clients that way, and only the narrower audiences have to be told to upgrade.

The client-side host= support was done post 1.4.0-rc1 timeframe so we have to be nicer to 1.3 based people, at least give them a way to slurp newer version with their client ;-).

Haven't looked at the rest of the patch yet. Will comment later.

Pierre Habouzit· Aug 24, 2006, 07:50 UTC · re: Junio C Hamano · lore

Re: [PATCH] git-daemon: more powerful base-path/user-path settings, using formats.

Le jeu 24 août 2006 02:17, Junio C Hamano a écrit :
Show 19 quoted lines
> Pierre Habouzit <madcoder@debian.org> writes:
> > Allow a form of virtualhosting, when %h format is used.
> >
> > Signed-off-by: Pierre Habouzit <madcoder@debian.org>
> > ---
> >
> >     This is intended to be a more flexible solution, that also
> > gives virtual hosting as a bonus. I still see no way to deal with
> > older clients when virtual hosting is used by the admin though,
> > having a "default" hostname won't solve anything at all anyway.
>
> I mildly disagree about the last sentence.  Enabling virtual
> hosting does not have to mean all virtual hosts are treated
> equal.  It is conceivable that a site hosts the primary,
> "collection of public repositories everybody would want to go
> to" set, with supplemental ones for specific audiences that are
> done via virtual hosting.  General public who would want to
> access the primary one can come with older clients that way, and
> only the narrower audiences have to be told to upgrade.

hmm, yes, that's indeed fair. Well, adding a --default-hostname that is used if no host= is passed is completely obvious and straightforward and solves that issue.

I also spotted a bug in the git_path_fmt function, I test 'pos' at the end of the loop, but the overflow test is not done when there is no format involved, wich could /theorically/ lead to buffer overflow. So please disregard that issue, I will at least provide a patch that fixes that.

> Haven't looked at the rest of the patch yet.  Will comment
> later.
np, TIA
-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org
Junio C Hamano· Aug 27, 2006, 06:12 UTC · re: Pierre Habouzit · lore

Re: [PATCH] git-daemon: more powerful base-path/user-path settings, using formats.

Pierre Habouzit <madcoder@debian.org> writes:
Show 7 quoted lines
> Allow a form of virtualhosting, when %h format is used.
>
> Signed-off-by: Pierre Habouzit <madcoder@debian.org>
> ---
>
>     This is intended to be a more flexible solution, that also gives virtual
>     hosting as a bonus.
Nicely done, almost.

Having to have the distinction between %p and %P formats feels somewhat unwieldy, though. Not that I have a better suggestion.

> +int is_base_path_fmt;
> +int is_user_path_fmt;
I prefer these to be of type "static int".

Although I am not an authority of variable naming, these sound funny to me. "is_XXX()" as a function name feels natural, "is_XXX" as a variable name does not --- it is not clear what the predicate is talking about.

Maybe "use_fmt_for_base_path" is easier to understand? I dunno. Or "user_path_is_fmt"? That's more logical but still somewhat feels funny.

Pierre Habouzit· Aug 27, 2006, 10:28 UTC · re: Junio C Hamano · lore

Re: [PATCH] git-daemon: more powerful base-path/user-path settings, using formats.

Le dim 27 août 2006 08:12, Junio C Hamano a écrit :
Show 18 quoted lines
> Pierre Habouzit <madcoder@debian.org> writes:
> > Allow a form of virtualhosting, when %h format is used.
> >
> > Signed-off-by: Pierre Habouzit <madcoder@debian.org>
> > ---
> >
> >     This is intended to be a more flexible solution, that also
> > gives virtual hosting as a bonus.
>
> Nicely done, almost.
>
> Having to have the distinction between %p and %P formats feels
> somewhat unwieldy, though.  Not that I have a better suggestion.
>
> > +int is_base_path_fmt;
> > +int is_user_path_fmt;
>
> I prefer these to be of type "static int".
omg, how did I missed that.
Show 8 quoted lines
> Although I am not an authority of variable naming, these sound
> funny to me.  "is_XXX()" as a function name feels natural,
> "is_XXX" as a variable name does not --- it is not clear what
> the predicate is talking about.
>
> Maybe "use_fmt_for_base_path" is easier to understand?  I dunno.
> Or "user_path_is_fmt"?  That's more logical but still somewhat
> feels funny.
agreed.
There is also a second patch that never made it to the list that fixes:
 * some indentation problems due to a bad vimrc
 * --default-hostname switch (to handle virtual hosts even with older
   clients)
 * possible overflow in the formatting method.

I'll recompute a new patch that superseeds that one, and merge your comments and my never sent patch too.

-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org
Junio C Hamano· Aug 27, 2006, 10:52 UTC · re: Pierre Habouzit · lore

Re: [PATCH] git-daemon: more powerful base-path/user-path settings, using formats.

Pierre Habouzit <madcoder@debian.org> writes:
Show 8 quoted lines
> There is also a second patch that never made it to the list that fixes:
>  * some indentation problems due to a bad vimrc
>  * --default-hostname switch (to handle virtual hosts even with older
>    clients)
>  * possible overflow in the formatting method.
>
> I'll recompute a new patch that superseeds that one, and merge your 
> comments and my never sent patch too.

I have to admit that I kinda liked JDL's simpler one first (and it has been in production use for some time). We'll see.

About vger potentially throwing things away, I use this script (called "taboo.perl") to check my messages before sending them out.

Obviously the taboo-word list itself is not attached here, but the actual script should have a copy of it after the __DATA__ marker.

-- >8 --
#!/usr/bin/perl -w
my $tmpl = '	if (%%PATTERN%%) {
		print "$lineno ${_}matches %%QPATTERN%%\n";
		return;
	}
';
my $stmt = "";
my $in_header = 1;
while (<DATA>) {
	if (/^\$global_taboo_body =/) {
		$in_header = 0;
	}
	next if (/^\043/ || /^\$/ || /^END$/ || /^\s*$/);
	chomp;
	my $p = $_;
	if ($in_header) {
		$p = '/^[-\w_]*:/ && ' . $p;
	}
	my $q = quotemeta($p);
	my $stmt1 = $tmpl;
	$stmt1 =~ s|%%PATTERN%%|$p|g;
	$stmt1 =~ s|%%QPATTERN%%|$q|g;
	$stmt .= $stmt1;
}
close DATA;
$stmt = 'sub check {
	my ($line, $lineno) = @_;
' . $stmt . '
}
';
eval $stmt;
while (<>) {
	check($_, $.);
}
my $how_to_update_this_script = <<'EOF' ;
	( sed -e '/^__DATA__$$/q' taboo.perl && \
	  wget -q -O - http://vger.kernel.org/majordomo-taboos.txt ) \
		>taboo.perl+
	if diff -u taboo.perl taboo.perl+; \
	then \
		rm -f taboo.perl+; \
		echo >&2 No changes.; \
	else \
		mv taboo.perl+ taboo.perl; \
		chmod +x taboo.perl; \
	fi
EOF
__DATA__
Pierre Habouzit· Aug 27, 2006, 11:40 UTC · re: Junio C Hamano · lore

Re: [PATCH] git-daemon: more powerful base-path/user-path settings, using formats.

Le dim 27 août 2006 12:52, Junio C Hamano a écrit :
> About vger potentially throwing things away, I use this script
> (called "taboo.perl") to check my messages before sending them
> out.

that was not it, I was biten (again) by git-send-mail that uses strftime (localized) to generate rfc822 dates, making them unparseable :|

I've resent the aggregated patch, that should work right now.
-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org
Jakub Narebski· Aug 27, 2006, 15:30 UTC · re: Pierre Habouzit · lore

Re: [PATCH] git-daemon: more powerful base-path/user-path settings, using formats.

Pierre Habouzit wrote:
Show 7 quoted lines
> Le dim 27 ao?t 2006 12:52, Junio C Hamano a écrit :
>> About vger potentially throwing things away, I use this script
>> (called "taboo.perl") to check my messages before sending them
>> out.
> 
> that was not it, I was biten (again) by git-send-mail that uses strftime 
> (localized) to generate rfc822 dates, making them unparseable :|

Update your git, or use ./git-send-email.perl directly from git repository (from 'master' branch, as 'next' branch version uses Git.pm). Strftime was replaced by pure Perl to generate rfc2822 date some time ago...

-- 
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git
Pierre Habouzit· Aug 27, 2006, 16:26 UTC · re: Jakub Narebski · lore

Re: [PATCH] git-daemon: more powerful base-path/user-path settings, using formats.

Le dim 27 août 2006 17:30, Jakub Narebski a écrit :
Show 14 quoted lines
> Pierre Habouzit wrote:
> > Le dim 27 ao?t 2006 12:52, Junio C Hamano a écrit :
> >> About vger potentially throwing things away, I use this script
> >> (called "taboo.perl") to check my messages before sending them
> >> out.
> >
> > that was not it, I was biten (again) by git-send-mail that uses
> > strftime (localized) to generate rfc822 dates, making them
> > unparseable :|
>
> Update your git, or use ./git-send-email.perl directly from git
> repository (from 'master' branch, as 'next' branch version uses
> Git.pm). Strftime was replaced by pure Perl to generate rfc2822 date
> some time ago...

I know that, I've done that on many of my system, but not the one where I hack git ... how lame isn't it ? :)

-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org
Randal L. Schwartz· Aug 27, 2006, 16:06 UTC · re: Junio C Hamano · lore

Re: [PATCH] git-daemon: more powerful base-path/user-path settings, using formats.

>>>>> "Junio" == Junio C Hamano <junkio@cox.net> writes:

Junio> About vger potentially throwing things away, I use this script Junio> (called "taboo.perl") to check my messages before sending them Junio> out.

Junio> Obviously the taboo-word list itself is not attached here, but Junio> the actual script should have a copy of it after the __DATA__ Junio> marker.

With "Inline::Files" from the CPAN, you could have a switch where the script updates itself with the new list:

    use Inline::Files;
    if (@ARGV == 1 and $ARGV[0] eq "-update") {
      use LWP::Simple;
      my $list = get "http://example.com/foo/bar.txt";
      open DATA, ">$DATA" or die "cannot write myself: $!";
      print DATA $list;
      close DATA;
      exit 0;
    }
    ... rest of your program here ...
    ... read using <DATA> as before ...
    __DATA__
    the list will magically go here.
-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

← back to recent threads