5 #include "run-command.h"
7 #define COMMAND_DIR "git-shell-commands"
8 #define HELP_COMMAND COMMAND_DIR "/help"
9 #define NOLOGIN_COMMAND COMMAND_DIR "/no-interactive-login"
11 static int do_generic_cmd(const char *me, char *arg)
13 const char *my_argv[4];
16 if (!arg || !(arg = sq_dequote(arg)))
18 if (!starts_with(me, "git-"))
25 return execv_git_cmd(my_argv);
28 static int is_valid_cmd_name(const char *cmd)
30 /* Test command contains no . or / characters */
31 return cmd[strcspn(cmd, "./")] == '\0';
34 static char *make_cmd(const char *prog)
36 char *prefix = xmalloc((strlen(prog) + strlen(COMMAND_DIR) + 2));
37 strcpy(prefix, COMMAND_DIR);
43 static void cd_to_homedir(void)
45 const char *home = getenv("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");
52 static void run_shell(void)
55 static const char *help_argv[] = { HELP_COMMAND, NULL };
57 if (!access(NOLOGIN_COMMAND, F_OK)) {
58 /* Interactive login disabled. */
59 const char *argv[] = { NOLOGIN_COMMAND, NULL };
62 status = run_command_v_opt(argv, 0);
68 /* Print help if enabled */
69 run_command_v_opt(help_argv, RUN_SILENT_EXEC_FAILURE);
72 struct strbuf line = STRBUF_INIT;
81 fprintf(stderr, "git> ");
82 if (strbuf_getline(&line, stdin, '\n') == EOF) {
83 fprintf(stderr, "\n");
84 strbuf_release(&line);
88 rawargs = strbuf_detach(&line, NULL);
89 split_args = xstrdup(rawargs);
90 count = split_cmdline(split_args, &argv);
92 fprintf(stderr, "invalid command format '%s': %s\n", rawargs,
93 split_cmdline_strerror(count));
100 if (!strcmp(prog, "")) {
101 } else if (!strcmp(prog, "quit") || !strcmp(prog, "logout") ||
102 !strcmp(prog, "exit") || !strcmp(prog, "bye")) {
104 } else if (is_valid_cmd_name(prog)) {
105 full_cmd = make_cmd(prog);
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);
113 fprintf(stderr, "invalid command format '%s'\n", prog);
121 static struct commands {
123 int (*exec)(const char *me, char *arg);
125 { "git-receive-pack", do_generic_cmd },
126 { "git-upload-pack", do_generic_cmd },
127 { "git-upload-archive", do_generic_cmd },
131 int main(int argc, char **argv)
134 const char **user_argv;
135 struct commands *cmd;
140 git_extract_argv0_path(argv[0]);
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.
150 /* Allow the user to run an interactive shell */
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.");
159 } else if (argc != 3 || strcmp(argv[1], "-c")) {
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
165 die("Run with no arguments or with -c cmd");
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". */
173 for (cmd = cmd_list ; cmd->name ; cmd++) {
174 int len = strlen(cmd->name);
176 if (strncmp(cmd->name, prog, len))
184 arg = prog + len + 1;
189 exit(cmd->exec(cmd->name, arg));
193 count = split_cmdline(prog, &user_argv);
195 if (is_valid_cmd_name(user_argv[0])) {
196 prog = make_cmd(user_argv[0]);
198 execv(user_argv[0], (char *const *) user_argv);
202 die("unrecognized command '%s'", argv[2]);
205 die("invalid command format '%s': %s", argv[2],
206 split_cmdline_strerror(count));