Add git-shell.
This adds a very git specific restricted shell, that can be added to /etc/shells and set to the pw_shell in the /etc/passwd file, to give users ability to push into repositories over ssh without giving them full interactive shell acount. [jc: I updated Linus' patch to match what the current sq_quote() does.] Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Linus Torvalds committed
Oct 23, 2005 at 14:30 UTC
35eb2d36418a25f9e956b116a622d8d99f4abad9
4 files changed
+106
-2
Makefile
+1
-1
@@ -116,7 +116,7 @@ PROGRAMS = \
116
git-merge-index$X git-mktag$X git-pack-objects$X git-patch-id$X \
117
git-peek-remote$X git-prune-packed$X git-read-tree$X \
118
git-receive-pack$X git-rev-list$X git-rev-parse$X \
119
- git-send-pack$X git-show-branch$X \
119
+ git-send-pack$X git-show-branch$X git-shell$X \
120
git-show-index$X git-ssh-fetch$X \
121
git-ssh-upload$X git-tar-tree$X git-unpack-file$X \
122
git-unpack-objects$X git-update-index$X git-update-server-info$X \
quote.c
+40
-1
@@ -15,6 +15,11 @@
15
#undef EMIT
16
#define EMIT(x) ( (++len < n) && (*bp++ = (x)) )
17
18
+static inline int need_bs_quote(char c)
19
+{
20
+ return (c == '\'' || c == '!');
21
+}
22
+
23
size_t sq_quote_buf(char *dst, size_t n, const char *src)
24
{
25
char c;
@@ -23,7 +28,7 @@ size_t sq_quote_buf(char *dst, size_t n, const char *src)
28
29
EMIT('\'');
30
while ((c = *src++)) {
26
- if (c == '\'' || c == '!') {
31
+ if (need_bs_quote(c)) {
32
EMIT('\'');
33
EMIT('\\');
34
EMIT(c);
@@ -52,6 +57,40 @@ char *sq_quote(const char *src)
57
return buf;
58
}
59
60
+char *sq_dequote(char *arg)
61
+{
62
+ char *dst = arg;
63
+ char *src = arg;
64
+ char c;
65
+
66
+ if (*src != '\'')
67
+ return NULL;
68
+ for (;;) {
69
+ c = *++src;
70
+ if (!c)
71
+ return NULL;
72
+ if (c != '\'') {
73
+ *dst++ = c;
74
+ continue;
75
+ }
76
+ /* We stepped out of sq */
77
+ switch (*++src) {
78
+ case '\0':
79
+ *dst = 0;
80
+ return arg;
81
+ case '\\':
82
+ c = *++src;
83
+ if (need_bs_quote(c) && *++src == '\'') {
84
+ *dst++ = c;
85
+ continue;
86
+ }
87
+ /* Fallthrough */
88
+ default:
89
+ return NULL;
90
+ }
91
+ }
92
+}
93
+
94
/*
95
* C-style name quoting.
96
*
quote.h
+6
@@ -31,6 +31,12 @@
31
extern char *sq_quote(const char *src);
32
extern size_t sq_quote_buf(char *dst, size_t n, const char *src);
33
34
+/* This unwraps what sq_quote() produces in place, but returns
35
+ * NULL if the input does not look like what sq_quote would have
36
+ * produced.
37
+ */
38
+extern char *sq_dequote(char *);
39
+
40
extern int quote_c_style(const char *name, char *outbuf, FILE *outfp,
41
int nodq);
42
extern char *unquote_c_style(const char *quoted, const char **endp);
shell.c
new
+59
@@ -0,0 +1,59 @@
1
+#include "cache.h"
2
+#include "quote.h"
3
+
4
+static int do_generic_cmd(const char *me, char *arg)
5
+{
6
+ const char *my_argv[4];
7
+
8
+ arg = sq_dequote(arg);
9
+ if (!arg)
10
+ die("bad argument");
11
+
12
+ my_argv[0] = me;
13
+ my_argv[1] = arg;
14
+ my_argv[2] = NULL;
15
+
16
+ return execvp(me, (char**) my_argv);
17
+}
18
+
19
+static struct commands {
20
+ const char *name;
21
+ int (*exec)(const char *me, char *arg);
22
+} cmd_list[] = {
23
+ { "git-receive-pack", do_generic_cmd },
24
+ { "git-upload-pack", do_generic_cmd },
25
+ { NULL },
26
+};
27
+
28
+int main(int argc, char **argv)
29
+{
30
+ char *prog;
31
+ struct commands *cmd;
32
+
33
+ /* We want to see "-c cmd args", and nothing else */
34
+ if (argc != 3 || strcmp(argv[1], "-c"))
35
+ die("What do you think I am? A shell?");
36
+
37
+ prog = argv[2];
38
+ argv += 2;
39
+ argc -= 2;
40
+ for (cmd = cmd_list ; cmd->name ; cmd++) {
41
+ int len = strlen(cmd->name);
42
+ char *arg;
43
+ if (strncmp(cmd->name, prog, len))
44
+ continue;
45
+ arg = NULL;
46
+ switch (prog[len]) {
47
+ case '\0':
48
+ arg = NULL;
49
+ break;
50
+ case ' ':
51
+ arg = prog + len + 1;
52
+ break;
53
+ default:
54
+ continue;
55
+ }
56
+ exit(cmd->exec(cmd->name, arg));
57
+ }
58
+ die("unrecognized command '%s'", prog);
59
+}