5 #include "run-command.h"
7 #define COMMAND_DIR "git-shell-commands"
8 #define HELP_COMMAND COMMAND_DIR "/help"
10 static int do_generic_cmd(const char *me
, char *arg
)
12 const char *my_argv
[4];
15 if (!arg
|| !(arg
= sq_dequote(arg
)))
17 if (prefixcmp(me
, "git-"))
24 return execv_git_cmd(my_argv
);
27 static int do_cvs_cmd(const char *me
, char *arg
)
29 const char *cvsserver_argv
[3] = {
30 "cvsserver", "server", NULL
33 if (!arg
|| strcmp(arg
, "server"))
34 die("git-cvsserver only handles server: %s", arg
);
37 return execv_git_cmd(cvsserver_argv
);
40 static int is_valid_cmd_name(const char *cmd
)
42 /* Test command contains no . or / characters */
43 return cmd
[strcspn(cmd
, "./")] == '\0';
46 static char *make_cmd(const char *prog
)
48 char *prefix
= xmalloc((strlen(prog
) + strlen(COMMAND_DIR
) + 2));
49 strcpy(prefix
, COMMAND_DIR
);
55 static void cd_to_homedir(void)
57 const char *home
= getenv("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");
64 static void run_shell(void)
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
);
72 struct strbuf line
= STRBUF_INIT
;
81 fprintf(stderr
, "git> ");
82 if (strbuf_getline(&line
, stdin
, '\n') == EOF
) {
83 fprintf(stderr
, "\n");
84 strbuf_release(&line
);
88 rawargs
= strbuf_detach(&line
, NULL
);
89 split_args
= xstrdup(rawargs
);
90 count
= split_cmdline(split_args
, &argv
);
92 fprintf(stderr
, "invalid command format '%s': %s\n", rawargs
,
93 split_cmdline_strerror(count
));
100 if (!strcmp(prog
, "")) {
101 } else if (!strcmp(prog
, "quit") || !strcmp(prog
, "logout") ||
102 !strcmp(prog
, "exit") || !strcmp(prog
, "bye")) {
104 } else if (is_valid_cmd_name(prog
)) {
105 full_cmd
= make_cmd(prog
);
107 code
= run_command_v_opt(argv
, RUN_SILENT_EXEC_FAILURE
);
108 if (code
== -1 && errno
== ENOENT
) {
109 fprintf(stderr
, "unrecognized command '%s'\n", prog
);
113 fprintf(stderr
, "invalid command format '%s'\n", prog
);
121 static struct commands
{
123 int (*exec
)(const char *me
, char *arg
);
125 { "git-receive-pack", do_generic_cmd
},
126 { "git-upload-pack", do_generic_cmd
},
127 { "git-upload-archive", do_generic_cmd
},
128 { "cvs", do_cvs_cmd
},
132 int main(int argc
, char **argv
)
135 const char **user_argv
;
136 struct commands
*cmd
;
142 git_extract_argv0_path(argv
[0]);
145 * Always open file descriptors 0/1/2 to avoid clobbering files
146 * in die(). It also avoids not messing up when the pipes are
147 * dup'ed onto stdin/stdout/stderr in the child processes we spawn.
149 devnull_fd
= open("/dev/null", O_RDWR
);
150 while (devnull_fd
>= 0 && devnull_fd
<= 2)
151 devnull_fd
= dup(devnull_fd
);
152 if (devnull_fd
== -1)
153 die_errno("opening /dev/null failed");
157 * Special hack to pretend to be a CVS server
159 if (argc
== 2 && !strcmp(argv
[1], "cvs server")) {
161 } else if (argc
== 1) {
162 /* Allow the user to run an interactive shell */
164 if (access(COMMAND_DIR
, R_OK
| X_OK
) == -1) {
165 die("Interactive git shell is not enabled.\n"
166 "hint: ~/" COMMAND_DIR
" should exist "
167 "and have read and execute access.");
171 } else if (argc
!= 3 || strcmp(argv
[1], "-c")) {
173 * We do not accept any other modes except "-c" followed by
174 * "cmd arg", where "cmd" is a very limited subset of git
175 * commands or a command in the COMMAND_DIR
177 die("Run with no arguments or with -c cmd");
180 prog
= xstrdup(argv
[2]);
181 if (!strncmp(prog
, "git", 3) && isspace(prog
[3]))
182 /* Accept "git foo" as if the caller said "git-foo". */
185 for (cmd
= cmd_list
; cmd
->name
; cmd
++) {
186 int len
= strlen(cmd
->name
);
188 if (strncmp(cmd
->name
, prog
, len
))
196 arg
= prog
+ len
+ 1;
201 exit(cmd
->exec(cmd
->name
, arg
));
205 count
= split_cmdline(prog
, &user_argv
);
207 if (is_valid_cmd_name(user_argv
[0])) {
208 prog
= make_cmd(user_argv
[0]);
210 execv(user_argv
[0], (char *const *) user_argv
);
214 die("unrecognized command '%s'", argv
[2]);
217 die("invalid command format '%s': %s", argv
[2],
218 split_cmdline_strerror(count
));