@matheusbsbs / matheus-bagliano / commits / 603968d22b
Fork 0

daemon: extend user-relative path notation.

Earlier, we made --base-path to automatically forbid user-relative paths, which was probably a mistake. This introduces --user-path (or --user-path=path) option to control the use of user-relative paths independently. The latter form of the option can be used to restrict accesses to a part of each user's home directory, similar to "public_html" some webservers supports. If we're invoked with --user-path=FOO option, then a URL of the form git://~USER/PATH/... resolves to the path HOME/FOO/PATH/..., where HOME is USER's home directory. [jc: This is much reworked by me so bugs are mine, but the original patch was done by Mark Wooding.] Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Feb 4, 2006 at 22:27 UTC 603968d22b19d1b98ff355cc32575a4d9845c151
2 files changed +54 -10
Documentation/git-daemon.txt
+12 -3
@@ -10,7 +10,8 @@ SYNOPSIS
10 [verse]
11 'git-daemon' [--verbose] [--syslog] [--inetd | --port=n] [--export-all]
12 [--timeout=n] [--init-timeout=n] [--strict-paths]
13 - [--base-path=path] [directory...]
13 + [--base-path=path] [--user-path | --user-path=path]
14 + [directory...]
15
16 DESCRIPTION
17 -----------
@@ -42,8 +43,7 @@ OPTIONS
43 This is sort of "GIT root" - if you run git-daemon with
44 '--base-path=/srv/git' on example.com, then if you later try to pull
45 'git://example.com/hello.git', `git-daemon` will interpret the path
45 - as '/srv/git/hello.git'. Home directories (the '~login' notation)
46 - access is disabled.
46 + as '/srv/git/hello.git'.
47
48 --export-all::
49 Allow pulling from all directories that look like GIT repositories
@@ -70,6 +70,15 @@ OPTIONS
70 Log to syslog instead of stderr. Note that this option does not imply
71 --verbose, thus by default only error conditions will be logged.
72
73 +--user-path, --user-path=path::
74 + Allow ~user notation to be used in requests. When
75 + specified with no parameter, requests to
76 + git://host/~alice/foo is taken as a request to access
77 + 'foo' repository in the home directory of user `alice`.
78 + If `--user-path=path` is specified, the same request is
79 + taken as a request to access `path/foo` repository in
80 + the home directory of user `alice`.
81 +
82 --verbose::
83 Log details about the incoming connections and requested files.
84
daemon.c
+42 -7
@@ -18,7 +18,8 @@ static int reuseaddr;
18 static const char daemon_usage[] =
19 "git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all]\n"
20 " [--timeout=n] [--init-timeout=n] [--strict-paths]\n"
21 -" [--base-path=path] [--reuseaddr] [directory...]";
21 +" [--base-path=path] [--user-path | --user-path=path]\n"
22 +" [--reuseaddr] [directory...]";
23
24 /* List of acceptable pathname prefixes */
25 static char **ok_paths = NULL;
@@ -30,6 +31,12 @@ static int export_all_trees = 0;
31 /* Take all paths relative to this one if non-NULL */
32 static char *base_path = NULL;
33
34 +/* If defined, ~user notation is allowed and the string is inserted
35 + * after ~user/. E.g. a request to git://host/~alice/frotz would
36 + * go to /home/alice/pub_git/frotz with --user-path=pub_git.
37 + */
38 +static char *user_path = NULL;
39 +
40 /* Timeout, and initial timeout */
41 static unsigned int timeout = 0;
42 static unsigned int init_timeout = 0;
@@ -137,6 +144,7 @@ static int avoid_alias(char *p)
144
145 static char *path_ok(char *dir)
146 {
147 + static char rpath[PATH_MAX];
148 char *path;
149
150 if (avoid_alias(dir)) {
@@ -144,12 +152,31 @@ static char *path_ok(char *dir)
152 return NULL;
153 }
154
147 - if (base_path) {
148 - static char rpath[PATH_MAX];
149 - if (!strict_paths && *dir == '~')
150 - ; /* allow user relative paths */
151 - else if (*dir != '/') {
152 - /* otherwise allow only absolute */
155 + if (*dir == '~') {
156 + if (!user_path) {
157 + logerror("'%s': User-path not allowed", dir);
158 + return NULL;
159 + }
160 + if (*user_path) {
161 + /* Got either "~alice" or "~alice/foo";
162 + * rewrite them to "~alice/%s" or
163 + * "~alice/%s/foo".
164 + */
165 + int namlen, restlen = strlen(dir);
166 + char *slash = strchr(dir, '/');
167 + if (!slash)
168 + slash = dir + restlen;
169 + namlen = slash - dir;
170 + restlen -= namlen;
171 + loginfo("userpath <%s>, request <%s>, namlen %d, restlen %d, slash <%s>", user_path, dir, namlen, restlen, slash);
172 + snprintf(rpath, PATH_MAX, "%.*s/%s%.*s",
173 + namlen, dir, user_path, restlen, slash);
174 + dir = rpath;
175 + }
176 + }
177 + else if (base_path) {
178 + if (*dir != '/') {
179 + /* Allow only absolute */
180 logerror("'%s': Non-absolute path denied (base-path active)", dir);
181 return NULL;
182 }
@@ -688,6 +715,14 @@ int main(int argc, char **argv)
715 reuseaddr = 1;
716 continue;
717 }
718 + if (!strcmp(arg, "--user-path")) {
719 + user_path = "";
720 + continue;
721 + }
722 + if (!strncmp(arg, "--user-path=", 12)) {
723 + user_path = arg + 12;
724 + continue;
725 + }
726 if (!strcmp(arg, "--")) {
727 ok_paths = &argv[i+1];
728 break;