5 #include "run-command.h"
7 #define COMMAND_DIR "git-shell-commands"
8 #define HELP_COMMAND COMMAND_DIR "/help"
9 #define NOLOGIN_COMMAND COMMAND_DIR "/no-interactive-login"
11 static int do_generic_cmd(const char *me
, char *arg
)
13 const char *my_argv
[4];
16 if (!arg
|| !(arg
= sq_dequote(arg
)) || *arg
== '-')
18 if (!starts_with(me
, "git-"))
25 return execv_git_cmd(my_argv
);
28 static int is_valid_cmd_name(const char *cmd
)
30 /* Test command contains no . or / characters */
31 return cmd
[strcspn(cmd
, "./")] == '\0';
34 static char *make_cmd(const char *prog
)
36 return xstrfmt("%s/%s", COMMAND_DIR
, prog
);
39 static void cd_to_homedir(void)
41 const char *home
= getenv("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");
48 static void run_shell(void)
51 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
};
58 status
= run_command_v_opt(argv
, 0);
64 /* Print help if enabled */
65 run_command_v_opt(help_argv
, RUN_SILENT_EXEC_FAILURE
);
68 struct strbuf line
= STRBUF_INIT
;
77 fprintf(stderr
, "git> ");
78 if (strbuf_getline_lf(&line
, stdin
) == EOF
) {
79 fprintf(stderr
, "\n");
80 strbuf_release(&line
);
84 rawargs
= strbuf_detach(&line
, NULL
);
85 split_args
= xstrdup(rawargs
);
86 count
= split_cmdline(split_args
, &argv
);
88 fprintf(stderr
, "invalid command format '%s': %s\n", rawargs
,
89 split_cmdline_strerror(count
));
96 if (!strcmp(prog
, "")) {
97 } else if (!strcmp(prog
, "quit") || !strcmp(prog
, "logout") ||
98 !strcmp(prog
, "exit") || !strcmp(prog
, "bye")) {
100 } else if (is_valid_cmd_name(prog
)) {
101 full_cmd
= make_cmd(prog
);
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
);
109 fprintf(stderr
, "invalid command format '%s'\n", prog
);
117 static struct commands
{
119 int (*exec
)(const char *me
, char *arg
);
121 { "git-receive-pack", do_generic_cmd
},
122 { "git-upload-pack", do_generic_cmd
},
123 { "git-upload-archive", do_generic_cmd
},
127 int cmd_main(int argc
, const char **argv
)
130 const char **user_argv
;
131 struct commands
*cmd
;
135 * Special hack to pretend to be a CVS server
137 if (argc
== 2 && !strcmp(argv
[1], "cvs server")) {
139 } else if (argc
== 1) {
140 /* Allow the user to run an interactive shell */
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.");
149 } else if (argc
!= 3 || strcmp(argv
[1], "-c")) {
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
155 die("Run with no arguments or with -c cmd");
158 prog
= xstrdup(argv
[2]);
159 if (!strncmp(prog
, "git", 3) && isspace(prog
[3]))
160 /* Accept "git foo" as if the caller said "git-foo". */
163 for (cmd
= cmd_list
; cmd
->name
; cmd
++) {
164 int len
= strlen(cmd
->name
);
166 if (strncmp(cmd
->name
, prog
, len
))
174 arg
= prog
+ len
+ 1;
179 exit(cmd
->exec(cmd
->name
, arg
));
183 count
= split_cmdline(prog
, &user_argv
);
185 if (is_valid_cmd_name(user_argv
[0])) {
186 prog
= make_cmd(user_argv
[0]);
188 execv(user_argv
[0], (char *const *) user_argv
);
192 die("unrecognized command '%s'", argv
[2]);
195 die("invalid command format '%s': %s", argv
[2],
196 split_cmdline_strerror(count
));