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 do_cvs_cmd(const char *me, char *arg)
30 const char *cvsserver_argv[3] = {
31 "cvsserver", "server", NULL
34 if (!arg || strcmp(arg, "server"))
35 die("git-cvsserver only handles server: %s", arg);
38 return execv_git_cmd(cvsserver_argv);
41 static int is_valid_cmd_name(const char *cmd)
43 /* Test command contains no . or / characters */
44 return cmd[strcspn(cmd, "./")] == '\0';
47 static char *make_cmd(const char *prog)
49 return xstrfmt("%s/%s", COMMAND_DIR, prog);
52 static void cd_to_homedir(void)
54 const char *home = getenv("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");
61 static void run_shell(void)
64 static const char *help_argv[] = { HELP_COMMAND, NULL };
66 if (!access(NOLOGIN_COMMAND, F_OK)) {
67 /* Interactive login disabled. */
68 const char *argv[] = { NOLOGIN_COMMAND, NULL };
71 status = run_command_v_opt(argv, 0);
77 /* Print help if enabled */
78 run_command_v_opt(help_argv, RUN_SILENT_EXEC_FAILURE);
81 struct strbuf line = STRBUF_INIT;
90 fprintf(stderr, "git> ");
91 if (strbuf_getline_lf(&line, stdin) == EOF) {
92 fprintf(stderr, "\n");
93 strbuf_release(&line);
97 rawargs = strbuf_detach(&line, NULL);
98 split_args = xstrdup(rawargs);
99 count = split_cmdline(split_args, &argv);
101 fprintf(stderr, "invalid command format '%s': %s\n", rawargs,
102 split_cmdline_strerror(count));
109 if (!strcmp(prog, "")) {
110 } else if (!strcmp(prog, "quit") || !strcmp(prog, "logout") ||
111 !strcmp(prog, "exit") || !strcmp(prog, "bye")) {
113 } else if (is_valid_cmd_name(prog)) {
114 full_cmd = make_cmd(prog);
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);
122 fprintf(stderr, "invalid command format '%s'\n", prog);
130 static struct commands {
132 int (*exec)(const char *me, char *arg);
134 { "git-receive-pack", do_generic_cmd },
135 { "git-upload-pack", do_generic_cmd },
136 { "git-upload-archive", do_generic_cmd },
137 { "cvs", do_cvs_cmd },
141 int main(int argc, char **argv)
144 const char **user_argv;
145 struct commands *cmd;
150 git_extract_argv0_path(argv[0]);
153 * Always open file descriptors 0/1/2 to avoid clobbering files
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.
160 * Special hack to pretend to be a CVS server
162 if (argc == 2 && !strcmp(argv[1], "cvs server")) {
164 } else if (argc == 1) {
165 /* Allow the user to run an interactive shell */
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.");
174 } else if (argc != 3 || strcmp(argv[1], "-c")) {
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
180 die("Run with no arguments or with -c cmd");
183 prog = xstrdup(argv[2]);
184 if (!strncmp(prog, "git", 3) && isspace(prog[3]))
185 /* Accept "git foo" as if the caller said "git-foo". */
188 for (cmd = cmd_list ; cmd->name ; cmd++) {
189 int len = strlen(cmd->name);
191 if (strncmp(cmd->name, prog, len))
199 arg = prog + len + 1;
204 exit(cmd->exec(cmd->name, arg));
208 count = split_cmdline(prog, &user_argv);
210 if (is_valid_cmd_name(user_argv[0])) {
211 prog = make_cmd(user_argv[0]);
213 execv(user_argv[0], (char *const *) user_argv);
217 die("unrecognized command '%s'", argv[2]);
220 die("invalid command format '%s': %s", argv[2],
221 split_cmdline_strerror(count));