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.
 
 
 
 
 
 

224 lines
5.1 KiB

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