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