]>
Commit | Line | Data |
---|---|---|
35eb2d36 LT |
1 | #include "cache.h" |
2 | #include "quote.h" | |
77cb17e9 | 3 | #include "exec_cmd.h" |
0c696fe7 | 4 | #include "strbuf.h" |
e69164dd | 5 | #include "run-command.h" |
35eb2d36 | 6 | |
2dbc887e | 7 | #define COMMAND_DIR "git-shell-commands" |
e69164dd | 8 | #define HELP_COMMAND COMMAND_DIR "/help" |
35297089 | 9 | #define NOLOGIN_COMMAND COMMAND_DIR "/no-interactive-login" |
2dbc887e | 10 | |
35eb2d36 LT |
11 | static int do_generic_cmd(const char *me, char *arg) |
12 | { | |
13 | const char *my_argv[4]; | |
14 | ||
e1464ca7 | 15 | setup_path(); |
3ec80449 | 16 | if (!arg || !(arg = sq_dequote(arg)) || *arg == '-') |
35eb2d36 | 17 | die("bad argument"); |
59556548 | 18 | if (!starts_with(me, "git-")) |
77cb17e9 | 19 | die("bad command"); |
35eb2d36 | 20 | |
77cb17e9 | 21 | my_argv[0] = me + 4; |
35eb2d36 LT |
22 | my_argv[1] = arg; |
23 | my_argv[2] = NULL; | |
24 | ||
9201c707 | 25 | return execv_git_cmd(my_argv); |
35eb2d36 LT |
26 | } |
27 | ||
2dbc887e GB |
28 | static int is_valid_cmd_name(const char *cmd) |
29 | { | |
30 | /* Test command contains no . or / characters */ | |
31 | return cmd[strcspn(cmd, "./")] == '\0'; | |
32 | } | |
33 | ||
34 | static char *make_cmd(const char *prog) | |
35 | { | |
b2724c87 | 36 | return xstrfmt("%s/%s", COMMAND_DIR, prog); |
2dbc887e GB |
37 | } |
38 | ||
39 | static void cd_to_homedir(void) | |
40 | { | |
41 | const char *home = getenv("HOME"); | |
42 | if (!home) | |
43 | die("could not determine user's home directory; HOME is unset"); | |
44 | if (chdir(home) == -1) | |
45 | die("could not chdir to user's home directory"); | |
46 | } | |
0c696fe7 | 47 | |
e69164dd GB |
48 | static void run_shell(void) |
49 | { | |
50 | int done = 0; | |
51 | static const char *help_argv[] = { HELP_COMMAND, NULL }; | |
35297089 JN |
52 | |
53 | if (!access(NOLOGIN_COMMAND, F_OK)) { | |
54 | /* Interactive login disabled. */ | |
55 | const char *argv[] = { NOLOGIN_COMMAND, NULL }; | |
56 | int status; | |
57 | ||
58 | status = run_command_v_opt(argv, 0); | |
59 | if (status < 0) | |
60 | exit(127); | |
61 | exit(status); | |
62 | } | |
63 | ||
e69164dd GB |
64 | /* Print help if enabled */ |
65 | run_command_v_opt(help_argv, RUN_SILENT_EXEC_FAILURE); | |
66 | ||
67 | do { | |
68 | struct strbuf line = STRBUF_INIT; | |
69 | const char *prog; | |
70 | char *full_cmd; | |
71 | char *rawargs; | |
9f29fe9a | 72 | char *split_args; |
e69164dd GB |
73 | const char **argv; |
74 | int code; | |
9f29fe9a | 75 | int count; |
e69164dd GB |
76 | |
77 | fprintf(stderr, "git> "); | |
8f309aeb | 78 | if (strbuf_getline_lf(&line, stdin) == EOF) { |
e69164dd GB |
79 | fprintf(stderr, "\n"); |
80 | strbuf_release(&line); | |
81 | break; | |
82 | } | |
83 | strbuf_trim(&line); | |
84 | rawargs = strbuf_detach(&line, NULL); | |
9f29fe9a GB |
85 | split_args = xstrdup(rawargs); |
86 | count = split_cmdline(split_args, &argv); | |
87 | if (count < 0) { | |
88 | fprintf(stderr, "invalid command format '%s': %s\n", rawargs, | |
89 | split_cmdline_strerror(count)); | |
90 | free(split_args); | |
e69164dd GB |
91 | free(rawargs); |
92 | continue; | |
93 | } | |
94 | ||
95 | prog = argv[0]; | |
96 | if (!strcmp(prog, "")) { | |
97 | } else if (!strcmp(prog, "quit") || !strcmp(prog, "logout") || | |
98 | !strcmp(prog, "exit") || !strcmp(prog, "bye")) { | |
99 | done = 1; | |
100 | } else if (is_valid_cmd_name(prog)) { | |
101 | full_cmd = make_cmd(prog); | |
102 | argv[0] = full_cmd; | |
103 | code = run_command_v_opt(argv, RUN_SILENT_EXEC_FAILURE); | |
104 | if (code == -1 && errno == ENOENT) { | |
105 | fprintf(stderr, "unrecognized command '%s'\n", prog); | |
106 | } | |
107 | free(full_cmd); | |
108 | } else { | |
109 | fprintf(stderr, "invalid command format '%s'\n", prog); | |
110 | } | |
111 | ||
112 | free(argv); | |
113 | free(rawargs); | |
114 | } while (!done); | |
115 | } | |
116 | ||
35eb2d36 LT |
117 | static struct commands { |
118 | const char *name; | |
119 | int (*exec)(const char *me, char *arg); | |
120 | } cmd_list[] = { | |
121 | { "git-receive-pack", do_generic_cmd }, | |
122 | { "git-upload-pack", do_generic_cmd }, | |
79f72b97 | 123 | { "git-upload-archive", do_generic_cmd }, |
35eb2d36 LT |
124 | { NULL }, |
125 | }; | |
126 | ||
3f2e2297 | 127 | int cmd_main(int argc, const char **argv) |
35eb2d36 LT |
128 | { |
129 | char *prog; | |
2dbc887e | 130 | const char **user_argv; |
35eb2d36 | 131 | struct commands *cmd; |
9f29fe9a | 132 | int count; |
0cfeed2e | 133 | |
bc7c73e2 JH |
134 | /* |
135 | * Special hack to pretend to be a CVS server | |
136 | */ | |
e69164dd | 137 | if (argc == 2 && !strcmp(argv[1], "cvs server")) { |
0c696fe7 | 138 | argv--; |
e69164dd GB |
139 | } else if (argc == 1) { |
140 | /* Allow the user to run an interactive shell */ | |
141 | cd_to_homedir(); | |
70256a3a RR |
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 | } | |
e69164dd GB |
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 | } | |
35eb2d36 | 157 | |
2dbc887e | 158 | prog = xstrdup(argv[2]); |
bc7c73e2 JH |
159 | if (!strncmp(prog, "git", 3) && isspace(prog[3])) |
160 | /* Accept "git foo" as if the caller said "git-foo". */ | |
161 | prog[3] = '-'; | |
162 | ||
35eb2d36 LT |
163 | for (cmd = cmd_list ; cmd->name ; cmd++) { |
164 | int len = strlen(cmd->name); | |
165 | char *arg; | |
166 | if (strncmp(cmd->name, prog, len)) | |
167 | continue; | |
168 | arg = NULL; | |
169 | switch (prog[len]) { | |
170 | case '\0': | |
171 | arg = NULL; | |
172 | break; | |
173 | case ' ': | |
174 | arg = prog + len + 1; | |
175 | break; | |
176 | default: | |
177 | continue; | |
178 | } | |
179 | exit(cmd->exec(cmd->name, arg)); | |
180 | } | |
2dbc887e GB |
181 | |
182 | cd_to_homedir(); | |
9f29fe9a GB |
183 | count = split_cmdline(prog, &user_argv); |
184 | if (count >= 0) { | |
2dbc887e GB |
185 | if (is_valid_cmd_name(user_argv[0])) { |
186 | prog = make_cmd(user_argv[0]); | |
187 | user_argv[0] = prog; | |
188 | execv(user_argv[0], (char *const *) user_argv); | |
189 | } | |
190 | free(prog); | |
191 | free(user_argv); | |
192 | die("unrecognized command '%s'", argv[2]); | |
193 | } else { | |
194 | free(prog); | |
9f29fe9a GB |
195 | die("invalid command format '%s': %s", argv[2], |
196 | split_cmdline_strerror(count)); | |
2dbc887e | 197 | } |
35eb2d36 | 198 | } |