Skip to contentSkip to conversation

git-daemon --base-path

PATCH In review Petr Baudis 2 participants 2 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 git-daemon --base-path

Tommi Virtanen expressed a wish on #git to be able to use short and elegant git URLs by making git-daemon 'root' in a given directory. This patch implements this, causing git-daemon to interpret all paths relative to the given base path if any is given.

---

 Documentation/git-daemon.txt |   11 ++++++++++-
 daemon.c                     |   21 ++++++++++++++++++++-
 2 files changed, 30 insertions(+), 2 deletions(-)
Documentation/git-daemon.txt +10 -1
index 2a8f371..ac67ac2 100644
@@ -8,7 +8,8 @@ git-daemon - A really simple server for 
 SYNOPSIS
 --------
 'git-daemon' [--verbose] [--syslog] [--inetd | --port=n] [--export-all]
-             [--timeout=n] [--init-timeout=n] [--strict-paths] [directory...]
+             [--timeout=n] [--init-timeout=n] [--strict-paths]
+             [--base-path=path] [directory...]
 
 DESCRIPTION
 -----------
@@ -35,6 +36,14 @@ OPTIONS
 	git-daemon will refuse to start when this option is enabled and no
 	whitelist is specified.
 
+--base-path::
+	Remap all the path requests as relative to the given path.
+	This is sort of "GIT root" - if you run git-daemon with
+	'--base-path=/srv/git' on example.com, then if you later try to pull
+	'git://example.com/hello.git', `git-daemon` will interpret the path
+	as '/srv/git/hello.git'. Home directories (the '~login' notation)
+	access is disabled.
+
 --export-all::
 	Allow pulling from all directories that look like GIT repositories
 	(have the 'objects' and 'refs' subdirectories), even if they
daemon.c +20 -1
index 539f6e8..3bd1426 100644
@@ -15,7 +15,8 @@ static int verbose;
 
 static const char daemon_usage[] =
 "git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all]\n"
-"           [--timeout=n] [--init-timeout=n] [--strict-paths] [directory...]";
+"           [--timeout=n] [--init-timeout=n] [--strict-paths]\n"
+"           [--base-path=path] [directory...]";
 
 /* List of acceptable pathname prefixes */
 static char **ok_paths = NULL;
@@ -24,6 +25,9 @@ static int strict_paths = 0;
 /* If this is set, git-daemon-export-ok is not required */
 static int export_all_trees = 0;
 
+/* Take all paths relative to this one if non-NULL */
+static char *base_path = NULL;
+
 /* Timeout, and initial timeout */
 static unsigned int timeout = 0;
 static unsigned int init_timeout = 0;
@@ -138,6 +142,17 @@ static char *path_ok(char *dir)
 		return NULL;
 	}
 
+	if (base_path) {
+		static char rpath[PATH_MAX];
+		if (*dir != '/') {
+			/* Forbid possible base-path evasion using ~paths. */
+			logerror("'%s': Non-absolute path denied (base-path active)");
+			return NULL;
+		}
+		snprintf(rpath, PATH_MAX, "%s%s", base_path, dir);
+		dir = rpath;
+	}
+
 	path = enter_repo(dir, strict_paths);
 
 	if (!path) {
@@ -639,6 +654,10 @@ int main(int argc, char **argv)
 			strict_paths = 1;
 			continue;
 		}
+		if (!strncmp(arg, "--base-path=", 12)) {
+			base_path = arg+12;
+			continue;
+		}
 		if (!strcmp(arg, "--")) {
 			ok_paths = &argv[i+1];
 			break;
#1 lore raw
Quoted 4 lines · Petr Baudis

Tommi Virtanen expressed a wish on #git to be able to use short and elegant git URLs by making git-daemon 'root' in a given directory. This patch implements this, causing git-daemon to interpret all paths relative to the given base path if any is given.

This is much nicer than chrooting or having a symlink hanging under /. Another possibility would be to have ~git/ a symlink to such directory and make it a whitelist -- then you do not need this patch. The URL would be "git://ho.st/~git/xxx" which is short-and-sweet.

daemon.c static char *path_ok(char *dir) · line 154

Quoted 3 lines
daemon.c +3 -0
+		snprintf(rpath, PATH_MAX, "%s%s", base_path, dir);
+		dir = rpath;
+	}

We probably would want to verify base_path exists and is without excess trailing slash when parsing arguments, if we are going to do this.