Add virtualization support to git-daemon
Signed-off-by: Jon Loeliger Signed-off-by: Junio C Hamano <junkio@cox.net>
Jon Loeliger committed
Sep 19, 2006 at 20:31 UTC
49ba83fb67d9e447b86953965ce5f949c6a93b81
5 files changed
+229
-11
Documentation/git-daemon.txt
+47
@@ -11,6 +11,7 @@ SYNOPSIS
11
'git-daemon' [--verbose] [--syslog] [--inetd | --port=n] [--export-all]
12
[--timeout=n] [--init-timeout=n] [--strict-paths]
13
[--base-path=path] [--user-path | --user-path=path]
14
+ [--interpolated-path=pathtemplate]
15
[--enable=service] [--disable=service]
16
[--allow-override=service] [--forbid-override=service]
17
[--reuseaddr] [--detach] [--pid-file=file]
@@ -50,6 +51,12 @@ OPTIONS
51
'git://example.com/hello.git', `git-daemon` will interpret the path
52
as '/srv/git/hello.git'.
53
54
+--interpolated-path=pathtemplate::
55
+ To support virtual hosting, an interpolated path template can be
56
+ used to dynamically construct alternate paths. The template
57
+ supports %H for the target hostname as supplied by the client,
58
+ and %D for the absolute path of the named repository.
59
+
60
--export-all::
61
Allow pulling from all directories that look like GIT repositories
62
(have the 'objects' and 'refs' subdirectories), even if they
@@ -135,6 +142,46 @@ upload-pack::
142
disable it by setting `daemon.uploadpack` configuration
143
item to `false`.
144
145
+EXAMPLES
146
+--------
147
+git-daemon as inetd server::
148
+ To set up `git-daemon` as an inetd service that handles any
149
+ repository under the whitelisted set of directories, /pub/foo
150
+ and /pub/bar, place an entry like the following into
151
+ /etc/inetd all on one line:
152
++
153
+------------------------------------------------
154
+ git stream tcp nowait nobody /usr/bin/git-daemon
155
+ git-daemon --inetd --verbose
156
+ --syslog --export-all
157
+ /pub/foo /pub/bar
158
+------------------------------------------------
159
+
160
+
161
+git-daemon as inetd server for virtual hosts::
162
+ To set up `git-daemon` as an inetd service that handles
163
+ repositories for different virtual hosts, `www.example.com`
164
+ and `www.example.org`, place an entry like the following into
165
+ `/etc/inetd` all on one line:
166
++
167
+------------------------------------------------
168
+ git stream tcp nowait nobody /usr/bin/git-daemon
169
+ git-daemon --inetd --verbose
170
+ --syslog --export-all
171
+ --interpolated-path=/pub/%H%D
172
+ /pub/www.example.org/software
173
+ /pub/www.example.com/software
174
+ /software
175
+------------------------------------------------
176
++
177
+In this example, the root-level directory `/pub` will contain
178
+a subdirectory for each virtual host name supported.
179
+Further, both hosts advertise repositories simply as
180
+`git://www.example.com/software/repo.git`. For pre-1.4.0
181
+clients, a symlink from `/software` into the appropriate
182
+default repository could be made as well.
183
+
184
+
185
Author
186
------
187
Written by Linus Torvalds <torvalds@osdl.org>, YOSHIFUJI Hideaki
Makefile
+3
-1
@@ -246,7 +246,9 @@ DIFF_OBJS = \
246
247
LIB_OBJS = \
248
blob.o commit.o connect.o csum-file.o cache-tree.o base85.o \
249
- date.o diff-delta.o entry.o exec_cmd.o ident.o lockfile.o \
249
+ date.o diff-delta.o entry.o exec_cmd.o ident.o \
250
+ interpolate.o \
251
+ lockfile.o \
252
object.o pack-check.o patch-delta.o path.o pkt-line.o sideband.o \
253
quote.o read-cache.o refs.o run-command.o dir.o object-refs.o \
254
server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
daemon.c
+79
-10
@@ -12,6 +12,7 @@
12
#include "pkt-line.h"
13
#include "cache.h"
14
#include "exec_cmd.h"
15
+#include "interpolate.h"
16
17
static int log_syslog;
18
static int verbose;
@@ -21,6 +22,7 @@ static const char daemon_usage[] =
22
"git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all]\n"
23
" [--timeout=n] [--init-timeout=n] [--strict-paths]\n"
24
" [--base-path=path] [--user-path | --user-path=path]\n"
25
+" [--interpolated-path=path]\n"
26
" [--reuseaddr] [--detach] [--pid-file=file]\n"
27
" [--[enable|disable|allow-override|forbid-override]=service]\n"
28
" [--user=user [[--group=group]] [directory...]";
@@ -34,6 +36,10 @@ static int export_all_trees;
36
37
/* Take all paths relative to this one if non-NULL */
38
static char *base_path;
39
+static char *interpolated_path;
40
+
41
+/* Flag indicating client sent extra args. */
42
+static int saw_extended_args;
43
44
/* If defined, ~user notation is allowed and the string is inserted
45
* after ~user/. E.g. a request to git://host/~alice/frotz would
@@ -45,6 +51,21 @@ static const char *user_path;
51
static unsigned int timeout;
52
static unsigned int init_timeout;
53
54
+/*
55
+ * Static table for now. Ugh.
56
+ * Feel free to make dynamic as needed.
57
+ */
58
+#define INTERP_SLOT_HOST (0)
59
+#define INTERP_SLOT_DIR (1)
60
+#define INTERP_SLOT_PERCENT (2)
61
+
62
+static struct interp interp_table[] = {
63
+ { "%H", 0},
64
+ { "%D", 0},
65
+ { "%%", "%"},
66
+};
67
+
68
+
69
static void logreport(int priority, const char *err, va_list params)
70
{
71
/* We should do a single write so that it is atomic and output
@@ -152,10 +173,14 @@ static int avoid_alias(char *p)
173
}
174
}
175
155
-static char *path_ok(char *dir)
176
+static char *path_ok(struct interp *itable)
177
{
178
static char rpath[PATH_MAX];
179
+ static char interp_path[PATH_MAX];
180
char *path;
181
+ char *dir;
182
+
183
+ dir = itable[INTERP_SLOT_DIR].value;
184
185
if (avoid_alias(dir)) {
186
logerror("'%s': aliased", dir);
@@ -184,16 +209,27 @@ static char *path_ok(char *dir)
209
dir = rpath;
210
}
211
}
212
+ else if (interpolated_path && saw_extended_args) {
213
+ if (*dir != '/') {
214
+ /* Allow only absolute */
215
+ logerror("'%s': Non-absolute path denied (interpolated-path active)", dir);
216
+ return NULL;
217
+ }
218
+
219
+ interpolate(interp_path, PATH_MAX, interpolated_path,
220
+ interp_table, ARRAY_SIZE(interp_table));
221
+ loginfo("Interpolated dir '%s'", interp_path);
222
+
223
+ dir = interp_path;
224
+ }
225
else if (base_path) {
226
if (*dir != '/') {
227
/* Allow only absolute */
228
logerror("'%s': Non-absolute path denied (base-path active)", dir);
229
return NULL;
230
}
193
- else {
194
- snprintf(rpath, PATH_MAX, "%s%s", base_path, dir);
195
- dir = rpath;
196
- }
231
+ snprintf(rpath, PATH_MAX, "%s%s", base_path, dir);
232
+ dir = rpath;
233
}
234
235
path = enter_repo(dir, strict_paths);
@@ -257,12 +293,14 @@ static int git_daemon_config(const char *var, const char *value)
293
return 0;
294
}
295
260
-static int run_service(char *dir, struct daemon_service *service)
296
+static int run_service(struct interp *itable, struct daemon_service *service)
297
{
298
const char *path;
299
int enabled = service->enabled;
300
265
- loginfo("Request %s for '%s'", service->name, dir);
301
+ loginfo("Request %s for '%s'",
302
+ service->name,
303
+ itable[INTERP_SLOT_DIR].value);
304
305
if (!enabled && !service->overridable) {
306
logerror("'%s': service not enabled.", service->name);
@@ -270,7 +308,7 @@ static int run_service(char *dir, struct daemon_service *service)
308
return -1;
309
}
310
273
- if (!(path = path_ok(dir)))
311
+ if (!(path = path_ok(itable)))
312
return -1;
313
314
/*
@@ -358,6 +396,28 @@ static void make_service_overridable(const char *name, int ena) {
396
die("No such service %s", name);
397
}
398
399
+static void parse_extra_args(char *extra_args, int buflen)
400
+{
401
+ char *val;
402
+ int vallen;
403
+ char *end = extra_args + buflen;
404
+
405
+ while (extra_args < end && *extra_args) {
406
+ saw_extended_args = 1;
407
+ if (strncasecmp("host=", extra_args, 5) == 0) {
408
+ val = extra_args + 5;
409
+ vallen = strlen(val) + 1;
410
+ if (*val) {
411
+ char *save = xmalloc(vallen);
412
+ interp_table[INTERP_SLOT_HOST].value = save;
413
+ strlcpy(save, val, vallen);
414
+ }
415
+ /* On to the next one */
416
+ extra_args = val + vallen;
417
+ }
418
+ }
419
+}
420
+
421
static int execute(struct sockaddr *addr)
422
{
423
static char line[1000];
@@ -398,13 +458,18 @@ static int execute(struct sockaddr *addr)
458
if (len && line[len-1] == '\n')
459
line[--len] = 0;
460
461
+ if (len != pktlen)
462
+ parse_extra_args(line + len + 1, pktlen - len - 1);
463
+
464
for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
465
struct daemon_service *s = &(daemon_service[i]);
466
int namelen = strlen(s->name);
467
if (!strncmp("git-", line, 4) &&
468
!strncmp(s->name, line + 4, namelen) &&
406
- line[namelen + 4] == ' ')
407
- return run_service(line + namelen + 5, s);
469
+ line[namelen + 4] == ' ') {
470
+ interp_table[INTERP_SLOT_DIR].value = line+namelen+5;
471
+ return run_service(interp_table, s);
472
+ }
473
}
474
475
logerror("Protocol error: '%s'", line);
@@ -867,6 +932,10 @@ int main(int argc, char **argv)
932
base_path = arg+12;
933
continue;
934
}
935
+ if (!strncmp(arg, "--interpolated-path=", 20)) {
936
+ interpolated_path = arg+20;
937
+ continue;
938
+ }
939
if (!strcmp(arg, "--reuseaddr")) {
940
reuseaddr = 1;
941
continue;
interpolate.c
new
+82
@@ -0,0 +1,82 @@
1
+/*
2
+ * Copyright 2006 Jon Loeliger
3
+ */
4
+
5
+#include <string.h>
6
+
7
+#include "interpolate.h"
8
+
9
+
10
+/*
11
+ * Convert a NUL-terminated string in buffer orig
12
+ * into the supplied buffer, result, whose length is reslen,
13
+ * performing substitutions on %-named sub-strings from
14
+ * the table, interps, with ninterps entries.
15
+ *
16
+ * Example interps:
17
+ * {
18
+ * { "%H", "example.org"},
19
+ * { "%port", "123"},
20
+ * { "%%", "%"},
21
+ * }
22
+ *
23
+ * Returns 1 on a successful substitution pass that fits in result,
24
+ * Returns 0 on a failed or overflowing substitution pass.
25
+ */
26
+
27
+int interpolate(char *result, int reslen,
28
+ char *orig,
29
+ struct interp *interps, int ninterps)
30
+{
31
+ char *src = orig;
32
+ char *dest = result;
33
+ int newlen = 0;
34
+ char *name, *value;
35
+ int namelen, valuelen;
36
+ int i;
37
+ char c;
38
+
39
+ memset(result, 0, reslen);
40
+
41
+ while ((c = *src) && newlen < reslen - 1) {
42
+ if (c == '%') {
43
+ /* Try to match an interpolation string. */
44
+ for (i = 0; i < ninterps; i++) {
45
+ name = interps[i].name;
46
+ namelen = strlen(name);
47
+ if (strncmp(src, name, namelen) == 0) {
48
+ break;
49
+ }
50
+ }
51
+
52
+ /* Check for valid interpolation. */
53
+ if (i < ninterps) {
54
+ value = interps[i].value;
55
+ valuelen = strlen(value);
56
+
57
+ if (newlen + valuelen < reslen - 1) {
58
+ /* Substitute. */
59
+ strncpy(dest, value, valuelen);
60
+ newlen += valuelen;
61
+ dest += valuelen;
62
+ src += namelen;
63
+ } else {
64
+ /* Something's not fitting. */
65
+ return 0;
66
+ }
67
+
68
+ } else {
69
+ /* Skip bogus interpolation. */
70
+ *dest++ = *src++;
71
+ newlen++;
72
+ }
73
+
74
+ } else {
75
+ /* Straight copy one non-interpolation character. */
76
+ *dest++ = *src++;
77
+ newlen++;
78
+ }
79
+ }
80
+
81
+ return newlen < reslen - 1;
82
+}
interpolate.h
new
+18
@@ -0,0 +1,18 @@
1
+/*
2
+ * Copyright 2006 Jon Loeliger
3
+ */
4
+
5
+#ifndef INTERPOLATE_H
6
+#define INTERPOLATE_H
7
+
8
+
9
+struct interp {
10
+ char *name;
11
+ char *value;
12
+};
13
+
14
+extern int interpolate(char *result, int reslen,
15
+ char *orig,
16
+ struct interp *interps, int ninterps);
17
+
18
+#endif /* INTERPOLATE_H */