Git 2.8.4
[git/git.git] / shell.c
CommitLineData
35eb2d36
LT
1#include "cache.h"
2#include "quote.h"
77cb17e9 3#include "exec_cmd.h"
0c696fe7 4#include "strbuf.h"
e69164dd 5#include "run-command.h"
35eb2d36 6
2dbc887e 7#define COMMAND_DIR "git-shell-commands"
e69164dd 8#define HELP_COMMAND COMMAND_DIR "/help"
35297089 9#define NOLOGIN_COMMAND COMMAND_DIR "/no-interactive-login"
2dbc887e 10
35eb2d36
LT
11static int do_generic_cmd(const char *me, char *arg)
12{
13 const char *my_argv[4];
14
e1464ca7 15 setup_path();
ab5f8627 16 if (!arg || !(arg = sq_dequote(arg)))
35eb2d36 17 die("bad argument");
59556548 18 if (!starts_with(me, "git-"))
77cb17e9 19 die("bad command");
35eb2d36 20
77cb17e9 21 my_argv[0] = me + 4;
35eb2d36
LT
22 my_argv[1] = arg;
23 my_argv[2] = NULL;
24
9201c707 25 return execv_git_cmd(my_argv);
35eb2d36
LT
26}
27
0c696fe7
JS
28static int do_cvs_cmd(const char *me, char *arg)
29{
30 const char *cvsserver_argv[3] = {
31 "cvsserver", "server", NULL
32 };
0c696fe7
JS
33
34 if (!arg || strcmp(arg, "server"))
35 die("git-cvsserver only handles server: %s", arg);
36
e1464ca7 37 setup_path();
0c696fe7
JS
38 return execv_git_cmd(cvsserver_argv);
39}
40
2dbc887e
GB
41static int is_valid_cmd_name(const char *cmd)
42{
43 /* Test command contains no . or / characters */
44 return cmd[strcspn(cmd, "./")] == '\0';
45}
46
47static char *make_cmd(const char *prog)
48{
b2724c87 49 return xstrfmt("%s/%s", COMMAND_DIR, prog);
2dbc887e
GB
50}
51
52static void cd_to_homedir(void)
53{
54 const char *home = getenv("HOME");
55 if (!home)
56 die("could not determine user's home directory; HOME is unset");
57 if (chdir(home) == -1)
58 die("could not chdir to user's home directory");
59}
0c696fe7 60
e69164dd
GB
61static void run_shell(void)
62{
63 int done = 0;
64 static const char *help_argv[] = { HELP_COMMAND, NULL };
35297089
JN
65
66 if (!access(NOLOGIN_COMMAND, F_OK)) {
67 /* Interactive login disabled. */
68 const char *argv[] = { NOLOGIN_COMMAND, NULL };
69 int status;
70
71 status = run_command_v_opt(argv, 0);
72 if (status < 0)
73 exit(127);
74 exit(status);
75 }
76
e69164dd
GB
77 /* Print help if enabled */
78 run_command_v_opt(help_argv, RUN_SILENT_EXEC_FAILURE);
79
80 do {
81 struct strbuf line = STRBUF_INIT;
82 const char *prog;
83 char *full_cmd;
84 char *rawargs;
9f29fe9a 85 char *split_args;
e69164dd
GB
86 const char **argv;
87 int code;
9f29fe9a 88 int count;
e69164dd
GB
89
90 fprintf(stderr, "git> ");
8f309aeb 91 if (strbuf_getline_lf(&line, stdin) == EOF) {
e69164dd
GB
92 fprintf(stderr, "\n");
93 strbuf_release(&line);
94 break;
95 }
96 strbuf_trim(&line);
97 rawargs = strbuf_detach(&line, NULL);
9f29fe9a
GB
98 split_args = xstrdup(rawargs);
99 count = split_cmdline(split_args, &argv);
100 if (count < 0) {
101 fprintf(stderr, "invalid command format '%s': %s\n", rawargs,
102 split_cmdline_strerror(count));
103 free(split_args);
e69164dd
GB
104 free(rawargs);
105 continue;
106 }
107
108 prog = argv[0];
109 if (!strcmp(prog, "")) {
110 } else if (!strcmp(prog, "quit") || !strcmp(prog, "logout") ||
111 !strcmp(prog, "exit") || !strcmp(prog, "bye")) {
112 done = 1;
113 } else if (is_valid_cmd_name(prog)) {
114 full_cmd = make_cmd(prog);
115 argv[0] = full_cmd;
116 code = run_command_v_opt(argv, RUN_SILENT_EXEC_FAILURE);
117 if (code == -1 && errno == ENOENT) {
118 fprintf(stderr, "unrecognized command '%s'\n", prog);
119 }
120 free(full_cmd);
121 } else {
122 fprintf(stderr, "invalid command format '%s'\n", prog);
123 }
124
125 free(argv);
126 free(rawargs);
127 } while (!done);
128}
129
35eb2d36
LT
130static struct commands {
131 const char *name;
132 int (*exec)(const char *me, char *arg);
133} cmd_list[] = {
134 { "git-receive-pack", do_generic_cmd },
135 { "git-upload-pack", do_generic_cmd },
79f72b97 136 { "git-upload-archive", do_generic_cmd },
0c696fe7 137 { "cvs", do_cvs_cmd },
35eb2d36
LT
138 { NULL },
139};
140
1e7abc59 141int main(int argc, char **argv)
35eb2d36
LT
142{
143 char *prog;
2dbc887e 144 const char **user_argv;
35eb2d36 145 struct commands *cmd;
9f29fe9a 146 int count;
0cfeed2e 147
5e9637c6
ÆAB
148 git_setup_gettext();
149
2d9932cf
DS
150 git_extract_argv0_path(argv[0]);
151
0cfeed2e
PB
152 /*
153 * Always open file descriptors 0/1/2 to avoid clobbering files
1d999ddd
TR
154 * in die(). It also avoids messing up when the pipes are dup'ed
155 * onto stdin/stdout/stderr in the child processes we spawn.
0cfeed2e 156 */
1d999ddd 157 sanitize_stdfds();
35eb2d36 158
bc7c73e2
JH
159 /*
160 * Special hack to pretend to be a CVS server
161 */
e69164dd 162 if (argc == 2 && !strcmp(argv[1], "cvs server")) {
0c696fe7 163 argv--;
e69164dd
GB
164 } else if (argc == 1) {
165 /* Allow the user to run an interactive shell */
166 cd_to_homedir();
70256a3a
RR
167 if (access(COMMAND_DIR, R_OK | X_OK) == -1) {
168 die("Interactive git shell is not enabled.\n"
169 "hint: ~/" COMMAND_DIR " should exist "
170 "and have read and execute access.");
171 }
e69164dd
GB
172 run_shell();
173 exit(0);
174 } else if (argc != 3 || strcmp(argv[1], "-c")) {
175 /*
176 * We do not accept any other modes except "-c" followed by
177 * "cmd arg", where "cmd" is a very limited subset of git
178 * commands or a command in the COMMAND_DIR
179 */
180 die("Run with no arguments or with -c cmd");
181 }
35eb2d36 182
2dbc887e 183 prog = xstrdup(argv[2]);
bc7c73e2
JH
184 if (!strncmp(prog, "git", 3) && isspace(prog[3]))
185 /* Accept "git foo" as if the caller said "git-foo". */
186 prog[3] = '-';
187
35eb2d36
LT
188 for (cmd = cmd_list ; cmd->name ; cmd++) {
189 int len = strlen(cmd->name);
190 char *arg;
191 if (strncmp(cmd->name, prog, len))
192 continue;
193 arg = NULL;
194 switch (prog[len]) {
195 case '\0':
196 arg = NULL;
197 break;
198 case ' ':
199 arg = prog + len + 1;
200 break;
201 default:
202 continue;
203 }
204 exit(cmd->exec(cmd->name, arg));
205 }
2dbc887e
GB
206
207 cd_to_homedir();
9f29fe9a
GB
208 count = split_cmdline(prog, &user_argv);
209 if (count >= 0) {
2dbc887e
GB
210 if (is_valid_cmd_name(user_argv[0])) {
211 prog = make_cmd(user_argv[0]);
212 user_argv[0] = prog;
213 execv(user_argv[0], (char *const *) user_argv);
214 }
215 free(prog);
216 free(user_argv);
217 die("unrecognized command '%s'", argv[2]);
218 } else {
219 free(prog);
9f29fe9a
GB
220 die("invalid command format '%s': %s", argv[2],
221 split_cmdline_strerror(count));
2dbc887e 222 }
35eb2d36 223}