5 #include "run-command.h"
 
   8 #define COMMAND_DIR "git-shell-commands"
 
   9 #define HELP_COMMAND COMMAND_DIR "/help"
 
  10 #define NOLOGIN_COMMAND COMMAND_DIR "/no-interactive-login"
 
  12 static int do_generic_cmd(const char *me, char *arg)
 
  14         const char *my_argv[4];
 
  17         if (!arg || !(arg = sq_dequote(arg)) || *arg == '-')
 
  19         if (!starts_with(me, "git-"))
 
  26         return execv_git_cmd(my_argv);
 
  29 static int is_valid_cmd_name(const char *cmd)
 
  31         /* Test command contains no . or / characters */
 
  32         return cmd[strcspn(cmd, "./")] == '\0';
 
  35 static char *make_cmd(const char *prog)
 
  37         return xstrfmt("%s/%s", COMMAND_DIR, prog);
 
  40 static void cd_to_homedir(void)
 
  42         const char *home = getenv("HOME");
 
  44                 die("could not determine user's home directory; HOME is unset");
 
  45         if (chdir(home) == -1)
 
  46                 die("could not chdir to user's home directory");
 
  49 static void run_shell(void)
 
  52         static const char *help_argv[] = { HELP_COMMAND, NULL };
 
  54         if (!access(NOLOGIN_COMMAND, F_OK)) {
 
  55                 /* Interactive login disabled. */
 
  56                 const char *argv[] = { NOLOGIN_COMMAND, NULL };
 
  59                 status = run_command_v_opt(argv, 0);
 
  65         /* Print help if enabled */
 
  66         run_command_v_opt(help_argv, RUN_SILENT_EXEC_FAILURE);
 
  69                 struct strbuf line = STRBUF_INIT;
 
  78                 fprintf(stderr, "git> ");
 
  79                 if (strbuf_getline_lf(&line, stdin) == EOF) {
 
  80                         fprintf(stderr, "\n");
 
  81                         strbuf_release(&line);
 
  85                 rawargs = strbuf_detach(&line, NULL);
 
  86                 split_args = xstrdup(rawargs);
 
  87                 count = split_cmdline(split_args, &argv);
 
  89                         fprintf(stderr, "invalid command format '%s': %s\n", rawargs,
 
  90                                 split_cmdline_strerror(count));
 
  97                 if (!strcmp(prog, "")) {
 
  98                 } else if (!strcmp(prog, "quit") || !strcmp(prog, "logout") ||
 
  99                            !strcmp(prog, "exit") || !strcmp(prog, "bye")) {
 
 101                 } else if (is_valid_cmd_name(prog)) {
 
 102                         full_cmd = make_cmd(prog);
 
 104                         code = run_command_v_opt(argv, RUN_SILENT_EXEC_FAILURE);
 
 105                         if (code == -1 && errno == ENOENT) {
 
 106                                 fprintf(stderr, "unrecognized command '%s'\n", prog);
 
 110                         fprintf(stderr, "invalid command format '%s'\n", prog);
 
 118 static struct commands {
 
 120         int (*exec)(const char *me, char *arg);
 
 122         { "git-receive-pack", do_generic_cmd },
 
 123         { "git-upload-pack", do_generic_cmd },
 
 124         { "git-upload-archive", do_generic_cmd },
 
 128 int cmd_main(int argc, const char **argv)
 
 131         const char **user_argv;
 
 132         struct commands *cmd;
 
 136          * Special hack to pretend to be a CVS server
 
 138         if (argc == 2 && !strcmp(argv[1], "cvs server")) {
 
 140         } else if (argc == 1) {
 
 141                 /* Allow the user to run an interactive shell */
 
 143                 if (access(COMMAND_DIR, R_OK | X_OK) == -1) {
 
 144                         die("Interactive git shell is not enabled.\n"
 
 145                             "hint: ~/" COMMAND_DIR " should exist "
 
 146                             "and have read and execute access.");
 
 150         } else if (argc != 3 || strcmp(argv[1], "-c")) {
 
 152                  * We do not accept any other modes except "-c" followed by
 
 153                  * "cmd arg", where "cmd" is a very limited subset of git
 
 154                  * commands or a command in the COMMAND_DIR
 
 156                 die("Run with no arguments or with -c cmd");
 
 159         prog = xstrdup(argv[2]);
 
 160         if (!strncmp(prog, "git", 3) && isspace(prog[3]))
 
 161                 /* Accept "git foo" as if the caller said "git-foo". */
 
 164         for (cmd = cmd_list ; cmd->name ; cmd++) {
 
 165                 int len = strlen(cmd->name);
 
 167                 if (strncmp(cmd->name, prog, len))
 
 175                         arg = prog + len + 1;
 
 180                 exit(cmd->exec(cmd->name, arg));
 
 184         count = split_cmdline(prog, &user_argv);
 
 186                 if (is_valid_cmd_name(user_argv[0])) {
 
 187                         prog = make_cmd(user_argv[0]);
 
 189                         execv(user_argv[0], (char *const *) user_argv);
 
 193                 die("unrecognized command '%s'", argv[2]);
 
 196                 die("invalid command format '%s': %s", argv[2],
 
 197                     split_cmdline_strerror(count));