@matheusbsbs / matheus-bagliano / commits / 4dbd135279
Fork 0

git-daemon support for user-relative paths.

Dropped a fair amount of reundant code in favour of the library code in path.c Added option --strict-paths with documentation, with backwards compatibility for whitelist entries with symlinks. Everything that worked earlier still works insofar as I have remembered testing it. Signed-off-by: Andreas Ericsson <ae@op5.se> Signed-off-by: Junio C Hamano <junkio@cox.net>

Andreas Ericsson committed Nov 17, 2005 at 20:37 UTC 4dbd13527933261076bbed1a5a2daa60a752d096
3 files changed +72 -87
Documentation/git-daemon.txt
+14 -2
@@ -29,9 +29,15 @@ This is ideally suited for read-only updates, ie pulling from git repositories.
29
30 OPTIONS
31 -------
32 ++--strict-paths::
33 + Match paths exactly (i.e. don't allow "/foo/repo" when the real path is
34 + "/foo/repo.git" or "/foo/repo/.git") and don't do user-relative paths.
35 + git-daemon will refuse to start when this option is enabled and no
36 + whitelist is specified.
37 +
38 --export-all::
39 Allow pulling from all directories that look like GIT repositories
34 - (have the 'objects' subdirectory and a 'HEAD' file), even if they
40 + (have the 'objects' and 'refs' subdirectories), even if they
41 do not have the 'git-daemon-export-ok' file.
42
43 --inetd::
@@ -57,9 +63,15 @@ OPTIONS
63 --verbose::
64 Log details about the incoming connections and requested files.
65
66 +<directory>::
67 + A directory to add to the whitelist of allowed directories. Unless
68 + --strict-paths is specified this will also include subdirectories
69 + of each named directory.
70 +
71 Author
72 ------
62 -Written by Linus Torvalds <torvalds@osdl.org> and YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
73 +Written by Linus Torvalds <torvalds@osdl.org>, YOSHIFUJI Hideaki
74 +<yoshfuji@linux-ipv6.org> and the git-list <git@vger.kernel.org>
75
76 Documentation
77 --------------
Documentation/pull-fetch-param.txt
+4 -3
@@ -9,15 +9,16 @@
9 - http://host.xz/path/to/repo.git/
10 - https://host.xz/path/to/repo.git/
11 - git://host.xz/path/to/repo.git/
12 +- git://host.xz/~user/path/to/repo.git/
13 - ssh://host.xz/path/to/repo.git/
14 - ssh://host.xz/~user/path/to/repo.git/
15 - ssh://host.xz/~/path/to/repo.git
16 ===============================================================
17 +
18 SSH Is the default transport protocol and also supports an
18 - scp-like syntax. Both syntaxes support username expansion.
19 - The following three are identical to the last three above,
20 - respectively:
19 + scp-like syntax. Both syntaxes support username expansion,
20 + as does the native git protocol. The following three are
21 + identical to the last three above, respectively:
22 +
23 ===============================================================
24 - host.xz:/path/to/repo.git/
daemon.c
+54 -82
@@ -15,10 +15,11 @@ static int verbose;
15
16 static const char daemon_usage[] =
17 "git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all]\n"
18 -" [--timeout=n] [--init-timeout=n] [directory...]";
18 +" [--timeout=n] [--init-timeout=n] [--strict-paths] [directory...]";
19
20 /* List of acceptable pathname prefixes */
21 static char **ok_paths = NULL;
22 +static int strict_paths = 0;
23
24 /* If this is set, git-daemon-export-ok is not required */
25 static int export_all_trees = 0;
@@ -81,69 +82,56 @@ static void loginfo(const char *err, ...)
82 va_end(params);
83 }
84
84 -static int path_ok(const char *dir)
85 +static char *path_ok(char *dir)
86 {
86 - const char *p = dir;
87 - char **pp;
88 - int sl, ndot;
87 + char *path = enter_repo(dir, strict_paths);
88
90 - /* The pathname here should be an absolute path. */
91 - if ( *p++ != '/' )
92 - return 0;
93 -
94 - sl = 1; ndot = 0;
95 -
96 - for (;;) {
97 - if ( *p == '.' ) {
98 - ndot++;
99 - } else if ( *p == '\0' ) {
100 - /* Reject "." and ".." at the end of the path */
101 - if ( sl && ndot > 0 && ndot < 3 )
102 - return 0;
103 -
104 - /* Otherwise OK */
105 - break;
106 - } else if ( *p == '/' ) {
107 - /* Refuse "", "." or ".." */
108 - if ( sl && ndot < 3 )
109 - return 0;
110 - sl = 1;
111 - ndot = 0;
112 - } else {
113 - sl = ndot = 0;
114 - }
115 - p++;
89 + if (!path) {
90 + logerror("'%s': unable to chdir or not a git archive", dir);
91 + return NULL;
92 }
93
94 if ( ok_paths && *ok_paths ) {
119 - int ok = 0;
95 + char **pp = NULL;
96 int dirlen = strlen(dir);
97 + int pathlen = strlen(path);
98
99 for ( pp = ok_paths ; *pp ; pp++ ) {
100 int len = strlen(*pp);
124 - if ( len <= dirlen &&
125 - !strncmp(*pp, dir, len) &&
126 - (dir[len] == '/' || dir[len] == '\0') ) {
127 - ok = 1;
128 - break;
101 + /* because of symlinks we must match both what the
102 + * user passed and the canonicalized path, otherwise
103 + * the user can send a string matching either a whitelist
104 + * entry or an actual directory exactly and still not
105 + * get through */
106 + if (len <= pathlen && !memcmp(*pp, path, len)) {
107 + if (path[len] == '\0' || (!strict_paths && path[len] == '/'))
108 + return path;
109 + }
110 + if (len <= dirlen && !memcmp(*pp, dir, len)) {
111 + if (dir[len] == '\0' || (!strict_paths && dir[len] == '/'))
112 + return path;
113 }
114 }
131 -
132 - if ( !ok )
133 - return 0; /* Path not in whitelist */
115 + }
116 + else {
117 + /* be backwards compatible */
118 + if (!strict_paths)
119 + return path;
120 }
121
136 - return 1; /* Path acceptable */
122 + logerror("'%s': not in whitelist", path);
123 + return NULL; /* Fallthrough. Deny by default */
124 }
125
139 -static int set_dir(const char *dir)
126 +static int upload(char *dir)
127 {
141 - if (!path_ok(dir)) {
142 - errno = EACCES;
143 - return -1;
144 - }
128 + /* Timeout as string */
129 + char timeout_buf[64];
130 + const char *path;
131 +
132 + loginfo("Request for '%s'", dir);
133
146 - if ( chdir(dir) )
134 + if (!(path = path_ok(dir)))
135 return -1;
136
137 /*
@@ -152,45 +140,17 @@ static int set_dir(const char *dir)
140 * We want a readable HEAD, usable "objects" directory, and
141 * a "git-daemon-export-ok" flag that says that the other side
142 * is ok with us doing this.
143 + *
144 + * path_ok() uses enter_repo() and does whitelist checking.
145 + * We only need to make sure the repository is exported.
146 */
147 +
148 if (!export_all_trees && access("git-daemon-export-ok", F_OK)) {
149 + logerror("'%s': repository not exported.", path);
150 errno = EACCES;
151 return -1;
152 }
153
161 - if (access("objects/", X_OK) || access("HEAD", R_OK)) {
162 - errno = EINVAL;
163 - return -1;
164 - }
165 -
166 - /* If all this passed, we're OK */
167 - return 0;
168 -}
169 -
170 -static int upload(char *dir)
171 -{
172 - /* Try paths in this order */
173 - static const char *paths[] = { "%s", "%s/.git", "%s.git", "%s.git/.git", NULL };
174 - const char **pp;
175 - /* Enough for the longest path above including final null */
176 - int buflen = strlen(dir)+10;
177 - char *dirbuf = xmalloc(buflen);
178 - /* Timeout as string */
179 - char timeout_buf[64];
180 -
181 - loginfo("Request for '%s'", dir);
182 -
183 - for ( pp = paths ; *pp ; pp++ ) {
184 - snprintf(dirbuf, buflen, *pp, dir);
185 - if ( !set_dir(dirbuf) )
186 - break;
187 - }
188 -
189 - if ( !*pp ) {
190 - logerror("Cannot set directory '%s': %s", dir, strerror(errno));
191 - return -1;
192 - }
193 -
154 /*
155 * We'll ignore SIGTERM from now on, we have a
156 * good client.
@@ -200,7 +160,7 @@ static int upload(char *dir)
160 snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout);
161
162 /* git-upload-pack only ever reads stuff, so this is safe */
203 - execlp("git-upload-pack", "git-upload-pack", "--strict", timeout_buf, ".", NULL);
163 + execlp("git-upload-pack", "git-upload-pack", "--strict", timeout_buf, path, NULL);
164 return -1;
165 }
166
@@ -216,7 +176,7 @@ static int execute(void)
176 if (len && line[len-1] == '\n')
177 line[--len] = 0;
178
219 - if (!strncmp("git-upload-pack /", line, 17))
179 + if (!strncmp("git-upload-pack ", line, 16))
180 return upload(line+16);
181
182 logerror("Protocol error: '%s'", line);
@@ -617,6 +577,10 @@ int main(int argc, char **argv)
577 init_timeout = atoi(arg+15);
578 continue;
579 }
580 + if (!strcmp(arg, "--strict-paths")) {
581 + strict_paths = 1;
582 + continue;
583 + }
584 if (!strcmp(arg, "--")) {
585 ok_paths = &argv[i+1];
586 break;
@@ -631,6 +595,14 @@ int main(int argc, char **argv)
595 if (log_syslog)
596 openlog("git-daemon", 0, LOG_DAEMON);
597
598 + if (strict_paths && (!ok_paths || !*ok_paths)) {
599 + if (!inetd_mode)
600 + die("git-daemon: option --strict-paths requires a whitelist");
601 +
602 + logerror("option --strict-paths requires a whitelist");
603 + exit (1);
604 + }
605 +
606 if (inetd_mode) {
607 fclose(stderr); //FIXME: workaround
608 return execute();