THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
4.5KB

  1. #include "cache.h"
  2. #include "quote.h"
  3. #include "exec-cmd.h"
  4. #include "strbuf.h"
  5. #include "run-command.h"
  6. #include "alias.h"
  7. #define COMMAND_DIR "git-shell-commands"
  8. #define HELP_COMMAND COMMAND_DIR "/help"
  9. #define NOLOGIN_COMMAND COMMAND_DIR "/no-interactive-login"
  10. static int do_generic_cmd(const char *me, char *arg)
  11. {
  12. const char *my_argv[4];
  13. setup_path();
  14. if (!arg || !(arg = sq_dequote(arg)) || *arg == '-')
  15. die("bad argument");
  16. if (!starts_with(me, "git-"))
  17. die("bad command");
  18. my_argv[0] = me + 4;
  19. my_argv[1] = arg;
  20. my_argv[2] = NULL;
  21. return execv_git_cmd(my_argv);
  22. }
  23. static int is_valid_cmd_name(const char *cmd)
  24. {
  25. /* Test command contains no . or / characters */
  26. return cmd[strcspn(cmd, "./")] == '\0';
  27. }
  28. static char *make_cmd(const char *prog)
  29. {
  30. return xstrfmt("%s/%s", COMMAND_DIR, prog);
  31. }
  32. static void cd_to_homedir(void)
  33. {
  34. const char *home = getenv("HOME");
  35. if (!home)
  36. die("could not determine user's home directory; HOME is unset");
  37. if (chdir(home) == -1)
  38. die("could not chdir to user's home directory");
  39. }
  40. static void run_shell(void)
  41. {
  42. int done = 0;
  43. static const char *help_argv[] = { HELP_COMMAND, NULL };
  44. if (!access(NOLOGIN_COMMAND, F_OK)) {
  45. /* Interactive login disabled. */
  46. const char *argv[] = { NOLOGIN_COMMAND, NULL };
  47. int status;
  48. status = run_command_v_opt(argv, 0);
  49. if (status < 0)
  50. exit(127);
  51. exit(status);
  52. }
  53. /* Print help if enabled */
  54. run_command_v_opt(help_argv, RUN_SILENT_EXEC_FAILURE);
  55. do {
  56. struct strbuf line = STRBUF_INIT;
  57. const char *prog;
  58. char *full_cmd;
  59. char *rawargs;
  60. char *split_args;
  61. const char **argv;
  62. int code;
  63. int count;
  64. fprintf(stderr, "git> ");
  65. if (strbuf_getline_lf(&line, stdin) == EOF) {
  66. fprintf(stderr, "\n");
  67. strbuf_release(&line);
  68. break;
  69. }
  70. strbuf_trim(&line);
  71. rawargs = strbuf_detach(&line, NULL);
  72. split_args = xstrdup(rawargs);
  73. count = split_cmdline(split_args, &argv);
  74. if (count < 0) {
  75. fprintf(stderr, "invalid command format '%s': %s\n", rawargs,
  76. split_cmdline_strerror(count));
  77. free(split_args);
  78. free(rawargs);
  79. continue;
  80. }
  81. prog = argv[0];
  82. if (!strcmp(prog, "")) {
  83. } else if (!strcmp(prog, "quit") || !strcmp(prog, "logout") ||
  84. !strcmp(prog, "exit") || !strcmp(prog, "bye")) {
  85. done = 1;
  86. } else if (is_valid_cmd_name(prog)) {
  87. full_cmd = make_cmd(prog);
  88. argv[0] = full_cmd;
  89. code = run_command_v_opt(argv, RUN_SILENT_EXEC_FAILURE);
  90. if (code == -1 && errno == ENOENT) {
  91. fprintf(stderr, "unrecognized command '%s'\n", prog);
  92. }
  93. free(full_cmd);
  94. } else {
  95. fprintf(stderr, "invalid command format '%s'\n", prog);
  96. }
  97. free(argv);
  98. free(rawargs);
  99. } while (!done);
  100. }
  101. static struct commands {
  102. const char *name;
  103. int (*exec)(const char *me, char *arg);
  104. } cmd_list[] = {
  105. { "git-receive-pack", do_generic_cmd },
  106. { "git-upload-pack", do_generic_cmd },
  107. { "git-upload-archive", do_generic_cmd },
  108. { NULL },
  109. };
  110. int cmd_main(int argc, const char **argv)
  111. {
  112. char *prog;
  113. const char **user_argv;
  114. struct commands *cmd;
  115. int count;
  116. /*
  117. * Special hack to pretend to be a CVS server
  118. */
  119. if (argc == 2 && !strcmp(argv[1], "cvs server")) {
  120. argv--;
  121. } else if (argc == 1) {
  122. /* Allow the user to run an interactive shell */
  123. cd_to_homedir();
  124. if (access(COMMAND_DIR, R_OK | X_OK) == -1) {
  125. die("Interactive git shell is not enabled.\n"
  126. "hint: ~/" COMMAND_DIR " should exist "
  127. "and have read and execute access.");
  128. }
  129. run_shell();
  130. exit(0);
  131. } else if (argc != 3 || strcmp(argv[1], "-c")) {
  132. /*
  133. * We do not accept any other modes except "-c" followed by
  134. * "cmd arg", where "cmd" is a very limited subset of git
  135. * commands or a command in the COMMAND_DIR
  136. */
  137. die("Run with no arguments or with -c cmd");
  138. }
  139. prog = xstrdup(argv[2]);
  140. if (!strncmp(prog, "git", 3) && isspace(prog[3]))
  141. /* Accept "git foo" as if the caller said "git-foo". */
  142. prog[3] = '-';
  143. for (cmd = cmd_list ; cmd->name ; cmd++) {
  144. int len = strlen(cmd->name);
  145. char *arg;
  146. if (strncmp(cmd->name, prog, len))
  147. continue;
  148. arg = NULL;
  149. switch (prog[len]) {
  150. case '\0':
  151. arg = NULL;
  152. break;
  153. case ' ':
  154. arg = prog + len + 1;
  155. break;
  156. default:
  157. continue;
  158. }
  159. exit(cmd->exec(cmd->name, arg));
  160. }
  161. cd_to_homedir();
  162. count = split_cmdline(prog, &user_argv);
  163. if (count >= 0) {
  164. if (is_valid_cmd_name(user_argv[0])) {
  165. prog = make_cmd(user_argv[0]);
  166. user_argv[0] = prog;
  167. execv(user_argv[0], (char *const *) user_argv);
  168. }
  169. free(prog);
  170. free(user_argv);
  171. die("unrecognized command '%s'", argv[2]);
  172. } else {
  173. free(prog);
  174. die("invalid command format '%s': %s", argv[2],
  175. split_cmdline_strerror(count));
  176. }
  177. }