contrib: remove 'mw-to-git'
[git] / shell.c
1 #include "cache.h"
2 #include "quote.h"
3 #include "exec_cmd.h"
4 #include "strbuf.h"
5 #include "run-command.h"
6
7 #define COMMAND_DIR "git-shell-commands"
8 #define HELP_COMMAND COMMAND_DIR "/help"
9 #define NOLOGIN_COMMAND COMMAND_DIR "/no-interactive-login"
10
11 static int do_generic_cmd(const char *me, char *arg)
12 {
13         const char *my_argv[4];
14
15         setup_path();
16         if (!arg || !(arg = sq_dequote(arg)))
17                 die("bad argument");
18         if (!starts_with(me, "git-"))
19                 die("bad command");
20
21         my_argv[0] = me + 4;
22         my_argv[1] = arg;
23         my_argv[2] = NULL;
24
25         return execv_git_cmd(my_argv);
26 }
27
28 static int is_valid_cmd_name(const char *cmd)
29 {
30         /* Test command contains no . or / characters */
31         return cmd[strcspn(cmd, "./")] == '\0';
32 }
33
34 static char *make_cmd(const char *prog)
35 {
36         char *prefix = xmalloc((strlen(prog) + strlen(COMMAND_DIR) + 2));
37         strcpy(prefix, COMMAND_DIR);
38         strcat(prefix, "/");
39         strcat(prefix, prog);
40         return prefix;
41 }
42
43 static void cd_to_homedir(void)
44 {
45         const char *home = getenv("HOME");
46         if (!home)
47                 die("could not determine user's home directory; HOME is unset");
48         if (chdir(home) == -1)
49                 die("could not chdir to user's home directory");
50 }
51
52 static void run_shell(void)
53 {
54         int done = 0;
55         static const char *help_argv[] = { HELP_COMMAND, NULL };
56
57         if (!access(NOLOGIN_COMMAND, F_OK)) {
58                 /* Interactive login disabled. */
59                 const char *argv[] = { NOLOGIN_COMMAND, NULL };
60                 int status;
61
62                 status = run_command_v_opt(argv, 0);
63                 if (status < 0)
64                         exit(127);
65                 exit(status);
66         }
67
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;
76                 char *split_args;
77                 const char **argv;
78                 int code;
79                 int count;
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);
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);
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
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 },
127         { "git-upload-archive", do_generic_cmd },
128         { NULL },
129 };
130
131 int main(int argc, char **argv)
132 {
133         char *prog;
134         const char **user_argv;
135         struct commands *cmd;
136         int count;
137
138         git_setup_gettext();
139
140         git_extract_argv0_path(argv[0]);
141
142         /*
143          * Always open file descriptors 0/1/2 to avoid clobbering files
144          * in die().  It also avoids messing up when the pipes are dup'ed
145          * onto stdin/stdout/stderr in the child processes we spawn.
146          */
147         sanitize_stdfds();
148
149         if (argc == 1) {
150                 /* Allow the user to run an interactive shell */
151                 cd_to_homedir();
152                 if (access(COMMAND_DIR, R_OK | X_OK) == -1) {
153                         die("Interactive git shell is not enabled.\n"
154                             "hint: ~/" COMMAND_DIR " should exist "
155                             "and have read and execute access.");
156                 }
157                 run_shell();
158                 exit(0);
159         } else if (argc != 3 || strcmp(argv[1], "-c")) {
160                 /*
161                  * We do not accept any other modes except "-c" followed by
162                  * "cmd arg", where "cmd" is a very limited subset of git
163                  * commands or a command in the COMMAND_DIR
164                  */
165                 die("Run with no arguments or with -c cmd");
166         }
167
168         prog = xstrdup(argv[2]);
169         if (!strncmp(prog, "git", 3) && isspace(prog[3]))
170                 /* Accept "git foo" as if the caller said "git-foo". */
171                 prog[3] = '-';
172
173         for (cmd = cmd_list ; cmd->name ; cmd++) {
174                 int len = strlen(cmd->name);
175                 char *arg;
176                 if (strncmp(cmd->name, prog, len))
177                         continue;
178                 arg = NULL;
179                 switch (prog[len]) {
180                 case '\0':
181                         arg = NULL;
182                         break;
183                 case ' ':
184                         arg = prog + len + 1;
185                         break;
186                 default:
187                         continue;
188                 }
189                 exit(cmd->exec(cmd->name, arg));
190         }
191
192         cd_to_homedir();
193         count = split_cmdline(prog, &user_argv);
194         if (count >= 0) {
195                 if (is_valid_cmd_name(user_argv[0])) {
196                         prog = make_cmd(user_argv[0]);
197                         user_argv[0] = prog;
198                         execv(user_argv[0], (char *const *) user_argv);
199                 }
200                 free(prog);
201                 free(user_argv);
202                 die("unrecognized command '%s'", argv[2]);
203         } else {
204                 free(prog);
205                 die("invalid command format '%s': %s", argv[2],
206                     split_cmdline_strerror(count));
207         }
208 }