blob: 40084a30130ef844899bc1f3321285afdda82607 [file] [log] [blame]
Linus Torvalds35eb2d32005-10-23 14:30:45 -07001#include "cache.h"
2#include "quote.h"
Stefan Bellerd807c4a2018-04-10 14:26:18 -07003#include "exec-cmd.h"
Johannes Schindelin0c696fe2007-10-09 15:33:25 +01004#include "strbuf.h"
Greg Brockmane69164d2010-07-28 00:43:03 -07005#include "run-command.h"
Nguyễn Thái Ngọc Duy65b5f942018-05-20 20:40:06 +02006#include "alias.h"
Linus Torvalds35eb2d32005-10-23 14:30:45 -07007
Greg Brockman2dbc8872010-07-28 17:31:01 -07008#define COMMAND_DIR "git-shell-commands"
Greg Brockmane69164d2010-07-28 00:43:03 -07009#define HELP_COMMAND COMMAND_DIR "/help"
Jonathan Nieder35297082013-03-09 14:00:11 -080010#define NOLOGIN_COMMAND COMMAND_DIR "/no-interactive-login"
Greg Brockman2dbc8872010-07-28 17:31:01 -070011
Linus Torvalds35eb2d32005-10-23 14:30:45 -070012static int do_generic_cmd(const char *me, char *arg)
13{
14 const char *my_argv[4];
15
Johannes Sixte1464ca2008-07-21 21:19:52 +020016 setup_path();
Jeff King3ec80442017-04-29 08:36:44 -040017 if (!arg || !(arg = sq_dequote(arg)) || *arg == '-')
Linus Torvalds35eb2d32005-10-23 14:30:45 -070018 die("bad argument");
Christian Couder59556542013-11-30 21:55:40 +010019 if (!starts_with(me, "git-"))
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050020 die("bad command");
Linus Torvalds35eb2d32005-10-23 14:30:45 -070021
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050022 my_argv[0] = me + 4;
Linus Torvalds35eb2d32005-10-23 14:30:45 -070023 my_argv[1] = arg;
24 my_argv[2] = NULL;
25
Junio C Hamano9201c702006-03-05 02:47:29 -080026 return execv_git_cmd(my_argv);
Linus Torvalds35eb2d32005-10-23 14:30:45 -070027}
28
Greg Brockman2dbc8872010-07-28 17:31:01 -070029static int is_valid_cmd_name(const char *cmd)
30{
31 /* Test command contains no . or / characters */
32 return cmd[strcspn(cmd, "./")] == '\0';
33}
34
35static char *make_cmd(const char *prog)
36{
Jeff Kingb2724c82014-06-19 17:26:56 -040037 return xstrfmt("%s/%s", COMMAND_DIR, prog);
Greg Brockman2dbc8872010-07-28 17:31:01 -070038}
39
40static void cd_to_homedir(void)
41{
42 const char *home = getenv("HOME");
43 if (!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");
47}
Johannes Schindelin0c696fe2007-10-09 15:33:25 +010048
Greg Brockmane69164d2010-07-28 00:43:03 -070049static void run_shell(void)
50{
51 int done = 0;
52 static const char *help_argv[] = { HELP_COMMAND, NULL };
Jonathan Nieder35297082013-03-09 14:00:11 -080053
54 if (!access(NOLOGIN_COMMAND, F_OK)) {
55 /* Interactive login disabled. */
56 const char *argv[] = { NOLOGIN_COMMAND, NULL };
57 int status;
58
59 status = run_command_v_opt(argv, 0);
60 if (status < 0)
61 exit(127);
62 exit(status);
63 }
64
Greg Brockmane69164d2010-07-28 00:43:03 -070065 /* Print help if enabled */
66 run_command_v_opt(help_argv, RUN_SILENT_EXEC_FAILURE);
67
68 do {
69 struct strbuf line = STRBUF_INIT;
70 const char *prog;
71 char *full_cmd;
72 char *rawargs;
Greg Brockman9f29fe92010-08-27 01:36:13 -040073 char *split_args;
Greg Brockmane69164d2010-07-28 00:43:03 -070074 const char **argv;
75 int code;
Greg Brockman9f29fe92010-08-27 01:36:13 -040076 int count;
Greg Brockmane69164d2010-07-28 00:43:03 -070077
78 fprintf(stderr, "git> ");
Junio C Hamano8f309ae2016-01-13 15:31:17 -080079 if (strbuf_getline_lf(&line, stdin) == EOF) {
Greg Brockmane69164d2010-07-28 00:43:03 -070080 fprintf(stderr, "\n");
81 strbuf_release(&line);
82 break;
83 }
84 strbuf_trim(&line);
85 rawargs = strbuf_detach(&line, NULL);
Greg Brockman9f29fe92010-08-27 01:36:13 -040086 split_args = xstrdup(rawargs);
87 count = split_cmdline(split_args, &argv);
88 if (count < 0) {
89 fprintf(stderr, "invalid command format '%s': %s\n", rawargs,
90 split_cmdline_strerror(count));
91 free(split_args);
Greg Brockmane69164d2010-07-28 00:43:03 -070092 free(rawargs);
93 continue;
94 }
95
96 prog = argv[0];
97 if (!strcmp(prog, "")) {
98 } else if (!strcmp(prog, "quit") || !strcmp(prog, "logout") ||
99 !strcmp(prog, "exit") || !strcmp(prog, "bye")) {
100 done = 1;
101 } else if (is_valid_cmd_name(prog)) {
102 full_cmd = make_cmd(prog);
103 argv[0] = full_cmd;
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);
107 }
108 free(full_cmd);
109 } else {
110 fprintf(stderr, "invalid command format '%s'\n", prog);
111 }
112
113 free(argv);
114 free(rawargs);
115 } while (!done);
116}
117
Linus Torvalds35eb2d32005-10-23 14:30:45 -0700118static struct commands {
119 const char *name;
120 int (*exec)(const char *me, char *arg);
121} cmd_list[] = {
122 { "git-receive-pack", do_generic_cmd },
123 { "git-upload-pack", do_generic_cmd },
Erik Broes79f72b92009-04-09 21:58:52 +0200124 { "git-upload-archive", do_generic_cmd },
Linus Torvalds35eb2d32005-10-23 14:30:45 -0700125 { NULL },
126};
127
Jeff King3f2e2292016-07-01 01:58:58 -0400128int cmd_main(int argc, const char **argv)
Linus Torvalds35eb2d32005-10-23 14:30:45 -0700129{
130 char *prog;
Greg Brockman2dbc8872010-07-28 17:31:01 -0700131 const char **user_argv;
Linus Torvalds35eb2d32005-10-23 14:30:45 -0700132 struct commands *cmd;
Greg Brockman9f29fe92010-08-27 01:36:13 -0400133 int count;
Paolo Bonzini0cfeed22008-08-27 17:20:35 +0200134
135 /*
Junio C Hamanobc7c73e2007-12-01 22:16:19 -0800136 * Special hack to pretend to be a CVS server
137 */
Greg Brockmane69164d2010-07-28 00:43:03 -0700138 if (argc == 2 && !strcmp(argv[1], "cvs server")) {
Johannes Schindelin0c696fe2007-10-09 15:33:25 +0100139 argv--;
Greg Brockmane69164d2010-07-28 00:43:03 -0700140 } else if (argc == 1) {
141 /* Allow the user to run an interactive shell */
142 cd_to_homedir();
Ramkumar Ramachandra70256a32010-08-24 11:06:51 +0530143 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.");
147 }
Greg Brockmane69164d2010-07-28 00:43:03 -0700148 run_shell();
149 exit(0);
150 } else if (argc != 3 || strcmp(argv[1], "-c")) {
151 /*
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
155 */
156 die("Run with no arguments or with -c cmd");
157 }
Linus Torvalds35eb2d32005-10-23 14:30:45 -0700158
Greg Brockman2dbc8872010-07-28 17:31:01 -0700159 prog = xstrdup(argv[2]);
Junio C Hamanobc7c73e2007-12-01 22:16:19 -0800160 if (!strncmp(prog, "git", 3) && isspace(prog[3]))
161 /* Accept "git foo" as if the caller said "git-foo". */
162 prog[3] = '-';
163
Linus Torvalds35eb2d32005-10-23 14:30:45 -0700164 for (cmd = cmd_list ; cmd->name ; cmd++) {
165 int len = strlen(cmd->name);
166 char *arg;
167 if (strncmp(cmd->name, prog, len))
168 continue;
169 arg = NULL;
170 switch (prog[len]) {
171 case '\0':
172 arg = NULL;
173 break;
174 case ' ':
175 arg = prog + len + 1;
176 break;
177 default:
178 continue;
179 }
180 exit(cmd->exec(cmd->name, arg));
181 }
Greg Brockman2dbc8872010-07-28 17:31:01 -0700182
183 cd_to_homedir();
Greg Brockman9f29fe92010-08-27 01:36:13 -0400184 count = split_cmdline(prog, &user_argv);
185 if (count >= 0) {
Greg Brockman2dbc8872010-07-28 17:31:01 -0700186 if (is_valid_cmd_name(user_argv[0])) {
187 prog = make_cmd(user_argv[0]);
188 user_argv[0] = prog;
189 execv(user_argv[0], (char *const *) user_argv);
190 }
191 free(prog);
192 free(user_argv);
193 die("unrecognized command '%s'", argv[2]);
194 } else {
195 free(prog);
Greg Brockman9f29fe92010-08-27 01:36:13 -0400196 die("invalid command format '%s': %s", argv[2],
197 split_cmdline_strerror(count));
Greg Brockman2dbc8872010-07-28 17:31:01 -0700198 }
Linus Torvalds35eb2d32005-10-23 14:30:45 -0700199}