Skip to content
Newer
Older
100644 221 lines (195 sloc) 5.08 KB
35eb2d3 Add git-shell.
Linus Torvalds authored
1 #include "cache.h"
2 #include "quote.h"
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
3 #include "exec_cmd.h"
0c696fe @dscho Support cvs via git-shell
dscho authored
4 #include "strbuf.h"
e69164d Add interactive mode to git-shell for user-friendliness
Greg Brockman authored
5 #include "run-command.h"
35eb2d3 Add git-shell.
Linus Torvalds authored
6
2dbc887 Allow creation of arbitrary git-shell commands
Greg Brockman authored
7 #define COMMAND_DIR "git-shell-commands"
e69164d Add interactive mode to git-shell for user-friendliness
Greg Brockman authored
8 #define HELP_COMMAND COMMAND_DIR "/help"
2dbc887 Allow creation of arbitrary git-shell commands
Greg Brockman authored
9
35eb2d3 Add git-shell.
Linus Torvalds authored
10 static int do_generic_cmd(const char *me, char *arg)
11 {
12 const char *my_argv[4];
13
e1464ca Record the command invocation path early
Johannes Sixt authored
14 setup_path();
ab5f862 shell.c: complain on insufficient arguments.
Junio C Hamano authored
15 if (!arg || !(arg = sq_dequote(arg)))
35eb2d3 Add git-shell.
Linus Torvalds authored
16 die("bad argument");
cc44c76 Mechanical conversion to use prefixcmp()
Junio C Hamano authored
17 if (prefixcmp(me, "git-"))
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
18 die("bad command");
35eb2d3 Add git-shell.
Linus Torvalds authored
19
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
20 my_argv[0] = me + 4;
35eb2d3 Add git-shell.
Linus Torvalds authored
21 my_argv[1] = arg;
22 my_argv[2] = NULL;
23
9201c70 Const tightening.
Junio C Hamano authored
24 return execv_git_cmd(my_argv);
35eb2d3 Add git-shell.
Linus Torvalds authored
25 }
26
0c696fe @dscho Support cvs via git-shell
dscho authored
27 static int do_cvs_cmd(const char *me, char *arg)
28 {
29 const char *cvsserver_argv[3] = {
30 "cvsserver", "server", NULL
31 };
32
33 if (!arg || strcmp(arg, "server"))
34 die("git-cvsserver only handles server: %s", arg);
35
e1464ca Record the command invocation path early
Johannes Sixt authored
36 setup_path();
0c696fe @dscho Support cvs via git-shell
dscho authored
37 return execv_git_cmd(cvsserver_argv);
38 }
39
2dbc887 Allow creation of arbitrary git-shell commands
Greg Brockman authored
40 static int is_valid_cmd_name(const char *cmd)
41 {
42 /* Test command contains no . or / characters */
43 return cmd[strcspn(cmd, "./")] == '\0';
44 }
45
46 static char *make_cmd(const char *prog)
47 {
48 char *prefix = xmalloc((strlen(prog) + strlen(COMMAND_DIR) + 2));
49 strcpy(prefix, COMMAND_DIR);
50 strcat(prefix, "/");
51 strcat(prefix, prog);
52 return prefix;
53 }
54
55 static void cd_to_homedir(void)
56 {
57 const char *home = getenv("HOME");
58 if (!home)
59 die("could not determine user's home directory; HOME is unset");
60 if (chdir(home) == -1)
61 die("could not chdir to user's home directory");
62 }
0c696fe @dscho Support cvs via git-shell
dscho authored
63
e69164d Add interactive mode to git-shell for user-friendliness
Greg Brockman authored
64 static void run_shell(void)
65 {
66 int done = 0;
67 static const char *help_argv[] = { HELP_COMMAND, NULL };
68 /* Print help if enabled */
69 run_command_v_opt(help_argv, RUN_SILENT_EXEC_FAILURE);
70
71 do {
72 struct strbuf line = STRBUF_INIT;
73 const char *prog;
74 char *full_cmd;
75 char *rawargs;
9f29fe9 shell: Display errors from improperly-formatted command lines
Greg Brockman authored
76 char *split_args;
e69164d Add interactive mode to git-shell for user-friendliness
Greg Brockman authored
77 const char **argv;
78 int code;
9f29fe9 shell: Display errors from improperly-formatted command lines
Greg Brockman authored
79 int count;
e69164d Add interactive mode to git-shell for user-friendliness
Greg Brockman authored
80
81 fprintf(stderr, "git> ");
82 if (strbuf_getline(&line, stdin, '\n') == EOF) {
83 fprintf(stderr, "\n");
84 strbuf_release(&line);
85 break;
86 }
87 strbuf_trim(&line);
88 rawargs = strbuf_detach(&line, NULL);
9f29fe9 shell: Display errors from improperly-formatted command lines
Greg Brockman authored
89 split_args = xstrdup(rawargs);
90 count = split_cmdline(split_args, &argv);
91 if (count < 0) {
92 fprintf(stderr, "invalid command format '%s': %s\n", rawargs,
93 split_cmdline_strerror(count));
94 free(split_args);
e69164d Add interactive mode to git-shell for user-friendliness
Greg Brockman authored
95 free(rawargs);
96 continue;
97 }
98
99 prog = argv[0];
100 if (!strcmp(prog, "")) {
101 } else if (!strcmp(prog, "quit") || !strcmp(prog, "logout") ||
102 !strcmp(prog, "exit") || !strcmp(prog, "bye")) {
103 done = 1;
104 } else if (is_valid_cmd_name(prog)) {
105 full_cmd = make_cmd(prog);
106 argv[0] = full_cmd;
107 code = run_command_v_opt(argv, RUN_SILENT_EXEC_FAILURE);
108 if (code == -1 && errno == ENOENT) {
109 fprintf(stderr, "unrecognized command '%s'\n", prog);
110 }
111 free(full_cmd);
112 } else {
113 fprintf(stderr, "invalid command format '%s'\n", prog);
114 }
115
116 free(argv);
117 free(rawargs);
118 } while (!done);
119 }
120
35eb2d3 Add git-shell.
Linus Torvalds authored
121 static struct commands {
122 const char *name;
123 int (*exec)(const char *me, char *arg);
124 } cmd_list[] = {
125 { "git-receive-pack", do_generic_cmd },
126 { "git-upload-pack", do_generic_cmd },
79f72b9 git-shell: Add 'git-upload-archive' to allowed commands.
Erik Broes authored
127 { "git-upload-archive", do_generic_cmd },
0c696fe @dscho Support cvs via git-shell
dscho authored
128 { "cvs", do_cvs_cmd },
35eb2d3 Add git-shell.
Linus Torvalds authored
129 { NULL },
130 };
131
1e7abc5 @gitster Revert "Build-in "git-shell""
gitster authored
132 int main(int argc, char **argv)
35eb2d3 Add git-shell.
Linus Torvalds authored
133 {
134 char *prog;
2dbc887 Allow creation of arbitrary git-shell commands
Greg Brockman authored
135 const char **user_argv;
35eb2d3 Add git-shell.
Linus Torvalds authored
136 struct commands *cmd;
0cfeed2 @bonzini make git-shell paranoid about closed stdin/stdout/stderr
bonzini authored
137 int devnull_fd;
9f29fe9 shell: Display errors from improperly-formatted command lines
Greg Brockman authored
138 int count;
0cfeed2 @bonzini make git-shell paranoid about closed stdin/stdout/stderr
bonzini authored
139
5e9637c @avar i18n: add infrastructure for translating Git with gettext
avar authored
140 git_setup_gettext();
141
2d9932c shell: add missing initialization of argv0_path
Dima Sharov authored
142 git_extract_argv0_path(argv[0]);
143
0cfeed2 @bonzini make git-shell paranoid about closed stdin/stdout/stderr
bonzini authored
144 /*
145 * Always open file descriptors 0/1/2 to avoid clobbering files
146 * in die(). It also avoids not messing up when the pipes are
147 * dup'ed onto stdin/stdout/stderr in the child processes we spawn.
148 */
149 devnull_fd = open("/dev/null", O_RDWR);
150 while (devnull_fd >= 0 && devnull_fd <= 2)
151 devnull_fd = dup(devnull_fd);
152 if (devnull_fd == -1)
d824cbb @trast Convert existing die(..., strerror(errno)) to die_errno()
trast authored
153 die_errno("opening /dev/null failed");
0cfeed2 @bonzini make git-shell paranoid about closed stdin/stdout/stderr
bonzini authored
154 close (devnull_fd);
35eb2d3 Add git-shell.
Linus Torvalds authored
155
bc7c73e @gitster git-shell: accept "git foo" form
gitster authored
156 /*
157 * Special hack to pretend to be a CVS server
158 */
e69164d Add interactive mode to git-shell for user-friendliness
Greg Brockman authored
159 if (argc == 2 && !strcmp(argv[1], "cvs server")) {
0c696fe @dscho Support cvs via git-shell
dscho authored
160 argv--;
e69164d Add interactive mode to git-shell for user-friendliness
Greg Brockman authored
161 } else if (argc == 1) {
162 /* Allow the user to run an interactive shell */
163 cd_to_homedir();
70256a3 @artagnon shell: Rewrite documentation and improve error message
artagnon authored
164 if (access(COMMAND_DIR, R_OK | X_OK) == -1) {
165 die("Interactive git shell is not enabled.\n"
166 "hint: ~/" COMMAND_DIR " should exist "
167 "and have read and execute access.");
168 }
e69164d Add interactive mode to git-shell for user-friendliness
Greg Brockman authored
169 run_shell();
170 exit(0);
171 } else if (argc != 3 || strcmp(argv[1], "-c")) {
172 /*
173 * We do not accept any other modes except "-c" followed by
174 * "cmd arg", where "cmd" is a very limited subset of git
175 * commands or a command in the COMMAND_DIR
176 */
177 die("Run with no arguments or with -c cmd");
178 }
35eb2d3 Add git-shell.
Linus Torvalds authored
179
2dbc887 Allow creation of arbitrary git-shell commands
Greg Brockman authored
180 prog = xstrdup(argv[2]);
bc7c73e @gitster git-shell: accept "git foo" form
gitster authored
181 if (!strncmp(prog, "git", 3) && isspace(prog[3]))
182 /* Accept "git foo" as if the caller said "git-foo". */
183 prog[3] = '-';
184
35eb2d3 Add git-shell.
Linus Torvalds authored
185 for (cmd = cmd_list ; cmd->name ; cmd++) {
186 int len = strlen(cmd->name);
187 char *arg;
188 if (strncmp(cmd->name, prog, len))
189 continue;
190 arg = NULL;
191 switch (prog[len]) {
192 case '\0':
193 arg = NULL;
194 break;
195 case ' ':
196 arg = prog + len + 1;
197 break;
198 default:
199 continue;
200 }
201 exit(cmd->exec(cmd->name, arg));
202 }
2dbc887 Allow creation of arbitrary git-shell commands
Greg Brockman authored
203
204 cd_to_homedir();
9f29fe9 shell: Display errors from improperly-formatted command lines
Greg Brockman authored
205 count = split_cmdline(prog, &user_argv);
206 if (count >= 0) {
2dbc887 Allow creation of arbitrary git-shell commands
Greg Brockman authored
207 if (is_valid_cmd_name(user_argv[0])) {
208 prog = make_cmd(user_argv[0]);
209 user_argv[0] = prog;
210 execv(user_argv[0], (char *const *) user_argv);
211 }
212 free(prog);
213 free(user_argv);
214 die("unrecognized command '%s'", argv[2]);
215 } else {
216 free(prog);
9f29fe9 shell: Display errors from improperly-formatted command lines
Greg Brockman authored
217 die("invalid command format '%s': %s", argv[2],
218 split_cmdline_strerror(count));
2dbc887 Allow creation of arbitrary git-shell commands
Greg Brockman authored
219 }
35eb2d3 Add git-shell.
Linus Torvalds authored
220 }
Something went wrong with that request. Please try again.