Allow users to configure custom shell arguments (#480)

This commit is contained in:
bagajjal 2021-03-04 02:05:25 +00:00 committed by GitHub
parent 8ab565c53f
commit bc7adf5a84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 24 deletions

View File

@ -50,6 +50,7 @@
static struct passwd pw;
static char* pw_shellpath = NULL;
char* shell_command_option = NULL;
char* shell_arguments = NULL;
BOOLEAN arg_escape = TRUE;
/* returns 0 on success, and -1 with errno set on failure */
@ -59,8 +60,8 @@ set_defaultshell()
HKEY reg_key = 0;
int tmp_len, ret = -1;
REGSAM mask = STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_WOW64_64KEY;
wchar_t path_buf[PATH_MAX], option_buf[32];
char *pw_shellpath_local = NULL, *command_option_local = NULL;
wchar_t path_buf[PATH_MAX], option_buf[32], arg_buf[PATH_MAX];
char *pw_shellpath_local = NULL, *command_option_local = NULL, *shell_arguments_local = NULL;
errno = 0;
@ -70,6 +71,7 @@ set_defaultshell()
path_buf[0] = L'\0';
option_buf[0] = L'\0';
arg_buf[0] = L'\0';
tmp_len = _countof(path_buf);
if ((RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\OpenSSH", 0, mask, &reg_key) == ERROR_SUCCESS) &&
@ -81,6 +83,11 @@ set_defaultshell()
DWORD escape_option = 1;
if (RegQueryValueExW(reg_key, L"DefaultShellCommandOption", 0, NULL, (LPBYTE)option_buf, &tmp_len) != ERROR_SUCCESS)
option_buf[0] = L'\0';
tmp_len = _countof(arg_buf);
if (RegQueryValueExW(reg_key, L"DefaultShellArguments", 0, NULL, (LPBYTE)arg_buf, &tmp_len) != ERROR_SUCCESS)
arg_buf[0] = L'\0';
if (RegQueryValueExW(reg_key, L"DefaultShellEscapeArguments", 0, NULL, (LPBYTE)&escape_option, &size) == ERROR_SUCCESS)
arg_escape = (escape_option != 0) ? TRUE : FALSE;
} else {
@ -99,12 +106,18 @@ set_defaultshell()
if ((command_option_local = utf16_to_utf8(option_buf)) == NULL)
goto cleanup;
if (arg_buf[0] != L'\0')
if ((shell_arguments_local = utf16_to_utf8(arg_buf)) == NULL)
goto cleanup;
convertToBackslash(pw_shellpath_local);
to_lower_case(pw_shellpath_local);
pw_shellpath = pw_shellpath_local;
pw_shellpath_local = NULL;
shell_command_option = command_option_local;
shell_arguments = shell_arguments_local;
command_option_local = NULL;
shell_arguments_local = NULL;
ret = 0;
cleanup:
@ -114,6 +127,9 @@ cleanup:
if (command_option_local)
free(command_option_local);
if (shell_arguments_local)
free(shell_arguments_local);
return ret;
}

View File

@ -241,6 +241,7 @@ int do_exec_windows(struct ssh *ssh, Session *s, const char *command, int pty) {
char *exec_command = NULL, *posix_cmd_input = NULL, *shell = NULL;
HANDLE job = NULL, process_handle;
extern char* shell_command_option;
extern char* shell_arguments;
extern BOOLEAN arg_escape;
/* Create three pipes for stdin, stdout and stderr */
@ -282,7 +283,7 @@ int do_exec_windows(struct ssh *ssh, Session *s, const char *command, int pty) {
JOBOBJECT_EXTENDED_LIMIT_INFORMATION job_info;
HANDLE job_dup;
pid_t pid = -1;
char * shell_option = NULL;
char * shell_command_option_local = NULL;
int shell_len = 0;
/*account for the quotes and null*/
shell_len = strlen(s->pw->pw_shell) + 2 + 1;
@ -308,26 +309,35 @@ int do_exec_windows(struct ssh *ssh, Session *s, const char *command, int pty) {
shell_type = SH_CYGWIN;
if (shell_command_option)
shell_option = shell_command_option;
shell_command_option_local = shell_command_option;
else if (shell_type == SH_CMD)
shell_option = "/c";
shell_command_option_local = "/c";
else
shell_option = "-c";
debug3("shell_option: %s", shell_option);
shell_command_option_local = "-c";
debug3("shell_option: %s", shell_command_option_local);
if (pty) {
fcntl(s->ptyfd, F_SETFD, FD_CLOEXEC);
char *pty_cmd = NULL;
if (command) {
size_t len = strlen(shell) + 1 + strlen(shell_option) + 1 + strlen(command) + 1;
size_t len = strlen(shell) + 1 + strlen(shell_command_option_local) + 1 + strlen(command) + 1;
pty_cmd = calloc(1, len);
strcpy_s(pty_cmd, len, shell);
strcat_s(pty_cmd, len, " ");
strcat_s(pty_cmd, len, shell_option);
strcat_s(pty_cmd, len, shell_command_option_local);
strcat_s(pty_cmd, len, " ");
strcat_s(pty_cmd, len, command);
} else {
if (shell_arguments) {
size_t len = strlen(shell) + 1 + strlen(shell_arguments) + 1;
pty_cmd = calloc(1, len);
strcpy_s(pty_cmd, len, shell);
strcat_s(pty_cmd, len, " ");
strcat_s(pty_cmd, len, shell_arguments);
}
else
pty_cmd = shell;
}
@ -347,7 +357,7 @@ int do_exec_windows(struct ssh *ssh, Session *s, const char *command, int pty) {
spawn_argv[0] = shell;
if (exec_command) {
spawn_argv[1] = shell_option;
spawn_argv[1] = shell_command_option_local;
spawn_argv[2] = exec_command;
}
}
@ -361,7 +371,7 @@ int do_exec_windows(struct ssh *ssh, Session *s, const char *command, int pty) {
/* account for " around and null */
if (exec_command) {
posix_cmd_input_len += strlen(shell_option) + 1;
posix_cmd_input_len += strlen(shell_command_option_local) + 1;
posix_cmd_input_len += strlen(exec_command) + 2 + 1;
}
@ -372,7 +382,7 @@ int do_exec_windows(struct ssh *ssh, Session *s, const char *command, int pty) {
if (exec_command) {
sprintf_s(posix_cmd_input, posix_cmd_input_len, "%s %s \"%s\"",
shell, shell_option, exec_command);
shell, shell_command_option_local, exec_command);
} else {
sprintf_s(posix_cmd_input, posix_cmd_input_len, "%s",
shell);