mirror of
https://github.com/PowerShell/Win32-OpenSSH.git
synced 2025-07-26 07:25:25 +02:00
Refactored do_exec* calls
This commit is contained in:
parent
92ddf4f9e1
commit
122ed4bf4d
Binary file not shown.
181
session.c
181
session.c
@ -491,37 +491,20 @@ do_authenticated1(Authctxt *authctxt)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PRAGMA:TODO
|
#ifdef WINDOWS
|
||||||
#ifndef WIN32_FIXME
|
|
||||||
#define USE_PIPES 1
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef WIN32_FIXME
|
int do_exec_windows(Session *s, const char *command, int pty) {
|
||||||
extern int debug_flag;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This is called to fork and execute a command when we have no tty. This
|
|
||||||
* will call do_child from the child, and server_loop from the parent after
|
|
||||||
* setting up file descriptors and such.
|
|
||||||
*/
|
|
||||||
int
|
|
||||||
do_exec_no_pty(Session *s, const char *command)
|
|
||||||
{
|
|
||||||
#ifdef WIN32_FIXME
|
|
||||||
wchar_t* pw_dir_utf16 = utf8_to_utf16(s->pw->pw_dir);
|
wchar_t* pw_dir_utf16 = utf8_to_utf16(s->pw->pw_dir);
|
||||||
|
extern int debug_flag;
|
||||||
|
|
||||||
/*
|
if (s->is_subsystem == SUBSYSTEM_INT_SFTP_ERROR)
|
||||||
* Win32 code.
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (s -> is_subsystem == SUBSYSTEM_INT_SFTP_ERROR)
|
|
||||||
{
|
{
|
||||||
printf("This service allows sftp connections only.\n");
|
printf("This service allows sftp connections only.\n");
|
||||||
fflush(NULL);
|
fflush(NULL);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
else if (s -> is_subsystem == SUBSYSTEM_INT_SFTP)
|
else if (s->is_subsystem == SUBSYSTEM_INT_SFTP)
|
||||||
{
|
{
|
||||||
u_int i;
|
u_int i;
|
||||||
for (i = 0; i < options.num_subsystems; i++)
|
for (i = 0; i < options.num_subsystems; i++)
|
||||||
@ -544,44 +527,24 @@ do_exec_no_pty(Session *s, const char *command)
|
|||||||
|
|
||||||
HANDLE hToken = INVALID_HANDLE_VALUE;
|
HANDLE hToken = INVALID_HANDLE_VALUE;
|
||||||
|
|
||||||
int do_xauth;
|
|
||||||
|
|
||||||
FILE *f = NULL;
|
|
||||||
|
|
||||||
char cmd[1024];
|
char cmd[1024];
|
||||||
char *exec_command;
|
char *exec_command;
|
||||||
char *laddr;
|
char *laddr;
|
||||||
char buf[256];
|
char buf[256];
|
||||||
#ifdef WIN32_PRAGMA_REMCON
|
#ifdef WIN32_PRAGMA_REMCON
|
||||||
char exec_command_str[512];
|
char exec_command_str[512];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (!command)
|
if (!command)
|
||||||
{
|
{
|
||||||
#ifndef WIN32_PRAGMA_REMCON
|
|
||||||
exec_command = s->pw->pw_shell;
|
exec_command = s->pw->pw_shell;
|
||||||
#else
|
|
||||||
if ( PathFileExists("\\program files\\pragma\\shared files\\cmdserver.exe") )
|
|
||||||
snprintf(exec_command_str, sizeof(exec_command_str),
|
|
||||||
"\\program files\\pragma\\shared files\\cmdserver.exe SSHD %d %d", s->row, s->col );
|
|
||||||
else {
|
|
||||||
// find base path of our executable
|
|
||||||
char basepath[MAX_PATH];
|
|
||||||
strcpy_s(basepath, MAX_PATH, __progname);
|
|
||||||
PathRemoveFileSpec(basepath); // get the full dir part of the name
|
|
||||||
snprintf(exec_command_str, sizeof(exec_command_str),
|
|
||||||
"%s\\cmdserver.exe SSHD %d %d", basepath,s->row, s->col);
|
|
||||||
}
|
|
||||||
exec_command = exec_command_str;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
exec_command = command;
|
exec_command = command;
|
||||||
}
|
}
|
||||||
|
|
||||||
do_xauth = s -> display != NULL && s -> auth_proto != NULL && s -> auth_data != NULL;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Create three socket pairs for stdin, stdout and stderr
|
* Create three socket pairs for stdin, stdout and stderr
|
||||||
*/
|
*/
|
||||||
@ -590,13 +553,13 @@ do_exec_no_pty(Session *s, const char *command)
|
|||||||
pipe(pipeerr);
|
pipe(pipeerr);
|
||||||
|
|
||||||
int retcode = -1;
|
int retcode = -1;
|
||||||
if ( (!s -> is_subsystem) && (s ->ttyfd != -1))
|
if ((!s->is_subsystem) && (s->ttyfd != -1))
|
||||||
{
|
{
|
||||||
extern HANDLE hInputConsole;
|
extern HANDLE hInputConsole;
|
||||||
extern HANDLE hOutputConsole ;
|
extern HANDLE hOutputConsole;
|
||||||
hInputConsole = GetConsoleInputHandle();
|
hInputConsole = GetConsoleInputHandle();
|
||||||
hOutputConsole = GetConsoleOutputHandle();
|
hOutputConsole = GetConsoleOutputHandle();
|
||||||
ConSetScreenSize( s->col, s->row );
|
ConSetScreenSize(s->col, s->row);
|
||||||
s->ptyfd = pipein[1]; // hConsole; // the pty is the Windows console output handle in our Win32 port
|
s->ptyfd = pipein[1]; // hConsole; // the pty is the Windows console output handle in our Win32 port
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -613,7 +576,7 @@ do_exec_no_pty(Session *s, const char *command)
|
|||||||
* Assign sockets to StartupInfo
|
* Assign sockets to StartupInfo
|
||||||
*/
|
*/
|
||||||
|
|
||||||
memset(&si, 0 , sizeof(STARTUPINFO));
|
memset(&si, 0, sizeof(STARTUPINFO));
|
||||||
|
|
||||||
si.cb = sizeof(STARTUPINFO);
|
si.cb = sizeof(STARTUPINFO);
|
||||||
si.lpReserved = 0;
|
si.lpReserved = 0;
|
||||||
@ -630,54 +593,15 @@ do_exec_no_pty(Session *s, const char *command)
|
|||||||
si.cbReserved2 = 0;
|
si.cbReserved2 = 0;
|
||||||
si.lpReserved2 = 0;
|
si.lpReserved2 = 0;
|
||||||
|
|
||||||
si.hStdInput = (HANDLE) sfd_to_handle(pipein[0]);
|
si.hStdInput = (HANDLE)sfd_to_handle(pipein[0]);
|
||||||
si.hStdOutput = (HANDLE) sfd_to_handle(pipeout[1]);
|
si.hStdOutput = (HANDLE)sfd_to_handle(pipeout[1]);
|
||||||
si.hStdError = (HANDLE) sfd_to_handle(pipeerr[1]);
|
si.hStdError = (HANDLE)sfd_to_handle(pipeerr[1]);
|
||||||
si.lpDesktop = NULL;
|
si.lpDesktop = NULL;
|
||||||
|
|
||||||
SetEnvironmentVariable("USER", s->pw->pw_name);
|
SetEnvironmentVariable("USER", s->pw->pw_name);
|
||||||
SetEnvironmentVariable("USERNAME", s->pw->pw_name);
|
SetEnvironmentVariable("USERNAME", s->pw->pw_name);
|
||||||
SetEnvironmentVariable("LOGNAME", s->pw->pw_name);
|
SetEnvironmentVariable("LOGNAME", s->pw->pw_name);
|
||||||
|
|
||||||
/*
|
|
||||||
* Setup xauth if needed
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (do_xauth && options.xauth_location != NULL)
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
* Add authority data to .Xauthority if appropriate.
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (debug_flag)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Running %.500s remove %.100s\n",
|
|
||||||
options.xauth_location, s->auth_display);
|
|
||||||
|
|
||||||
fprintf(stderr, "%.500s add %.100s %.100s %.100s\n", options.xauth_location,
|
|
||||||
s -> auth_display, s -> auth_proto, s -> auth_data);
|
|
||||||
}
|
|
||||||
|
|
||||||
snprintf(cmd, sizeof cmd, "%s -q -", options.xauth_location);
|
|
||||||
|
|
||||||
f = _popen(cmd, "w");
|
|
||||||
|
|
||||||
if (f)
|
|
||||||
{
|
|
||||||
fprintf(f, "remove %s\n", s -> auth_display);
|
|
||||||
|
|
||||||
fprintf(f, "add %s %s %s\n", s -> auth_display, s -> auth_proto, s -> auth_data);
|
|
||||||
|
|
||||||
_pclose(f);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Could not run %s\n", cmd);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#if 1
|
|
||||||
|
|
||||||
set_nonblock(pipein[0]);
|
set_nonblock(pipein[0]);
|
||||||
set_nonblock(pipein[1]);
|
set_nonblock(pipein[1]);
|
||||||
set_nonblock(pipeout[0]);
|
set_nonblock(pipeout[0]);
|
||||||
@ -685,8 +609,6 @@ do_exec_no_pty(Session *s, const char *command)
|
|||||||
set_nonblock(pipeerr[0]);
|
set_nonblock(pipeerr[0]);
|
||||||
set_nonblock(pipeerr[1]);
|
set_nonblock(pipeerr[1]);
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If we get this far, the user has already been authenticated
|
* If we get this far, the user has already been authenticated
|
||||||
* We should either have a user token in authctxt -> methoddata
|
* We should either have a user token in authctxt -> methoddata
|
||||||
@ -694,12 +616,12 @@ do_exec_no_pty(Session *s, const char *command)
|
|||||||
* token using CreateUserToken for non-password auth mechanisms.
|
* token using CreateUserToken for non-password auth mechanisms.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
hToken = s -> authctxt -> methoddata;
|
hToken = s->authctxt->methoddata;
|
||||||
|
|
||||||
|
|
||||||
if (s -> display)
|
if (s->display)
|
||||||
{
|
{
|
||||||
SetEnvironmentVariable("DISPLAY", s -> display);
|
SetEnvironmentVariable("DISPLAY", s->display);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -720,15 +642,13 @@ do_exec_no_pty(Session *s, const char *command)
|
|||||||
*(wstr + 1) = '\0';
|
*(wstr + 1) = '\0';
|
||||||
SetEnvironmentVariableW(L"HOMEDRIVE", pw_dir_utf16);
|
SetEnvironmentVariableW(L"HOMEDRIVE", pw_dir_utf16);
|
||||||
*(wstr + 1) = wchr;
|
*(wstr + 1) = wchr;
|
||||||
SetEnvironmentVariableW(L"HOMEPATH", (wstr+1));
|
SetEnvironmentVariableW(L"HOMEPATH", (wstr + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// find the server name of the domain controller which created this token
|
// find the server name of the domain controller which created this token
|
||||||
GetDomainFromToken ( &hToken, buf, sizeof(buf));
|
GetDomainFromToken(&hToken, buf, sizeof(buf));
|
||||||
if (buf[0])
|
if (buf[0])
|
||||||
SetEnvironmentVariable("USERDOMAIN", buf );
|
SetEnvironmentVariable("USERDOMAIN", buf);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Set SSH_CLIENT variable.
|
* Set SSH_CLIENT variable.
|
||||||
@ -757,8 +677,8 @@ do_exec_no_pty(Session *s, const char *command)
|
|||||||
|
|
||||||
|
|
||||||
// set better prompt for Windows cmd shell
|
// set better prompt for Windows cmd shell
|
||||||
if (!s -> is_subsystem) {
|
if (!s->is_subsystem) {
|
||||||
snprintf(buf,sizeof buf, "%s@%s $P$G", s->pw->pw_name, getenv("COMPUTERNAME"));
|
snprintf(buf, sizeof buf, "%s@%s $P$G", s->pw->pw_name, getenv("COMPUTERNAME"));
|
||||||
SetEnvironmentVariableA("PROMPT", buf);
|
SetEnvironmentVariableA("PROMPT", buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -766,7 +686,7 @@ do_exec_no_pty(Session *s, const char *command)
|
|||||||
* Get the current user's name (associated with sshd thread).
|
* Get the current user's name (associated with sshd thread).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
debug3("Home path before CreateProcessAsUser [%ls]", s -> pw -> pw_dir);
|
debug3("Home path before CreateProcessAsUser [%ls]", s->pw->pw_dir);
|
||||||
|
|
||||||
DWORD size = 256;
|
DWORD size = 256;
|
||||||
|
|
||||||
@ -774,7 +694,7 @@ do_exec_no_pty(Session *s, const char *command)
|
|||||||
|
|
||||||
GetUserName(name, &size);
|
GetUserName(name, &size);
|
||||||
|
|
||||||
if ( (s->term) && (s->term[0]) )
|
if ((s->term) && (s->term[0]))
|
||||||
SetEnvironmentVariable("TERM", s->term);
|
SetEnvironmentVariable("TERM", s->term);
|
||||||
/*
|
/*
|
||||||
* Create new process as other user using access token object.
|
* Create new process as other user using access token object.
|
||||||
@ -840,7 +760,7 @@ do_exec_no_pty(Session *s, const char *command)
|
|||||||
* Log the process handle (fake it as the pid) for termination lookups
|
* Log the process handle (fake it as the pid) for termination lookups
|
||||||
*/
|
*/
|
||||||
|
|
||||||
s -> pid = pi.dwProcessId;
|
s->pid = pi.dwProcessId;
|
||||||
sw_add_child(pi.hProcess, pi.dwProcessId);
|
sw_add_child(pi.hProcess, pi.dwProcessId);
|
||||||
|
|
||||||
// Add the child process created to select mux so that during our select data call we know if the process has exited
|
// Add the child process created to select mux so that during our select data call we know if the process has exited
|
||||||
@ -852,7 +772,7 @@ do_exec_no_pty(Session *s, const char *command)
|
|||||||
* Set interactive/non-interactive mode.
|
* Set interactive/non-interactive mode.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
packet_set_interactive(s -> display != NULL, options.ip_qos_interactive,
|
packet_set_interactive(s->display != NULL, options.ip_qos_interactive,
|
||||||
options.ip_qos_bulk);
|
options.ip_qos_bulk);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -863,7 +783,7 @@ do_exec_no_pty(Session *s, const char *command)
|
|||||||
close(pipeout[1]);
|
close(pipeout[1]);
|
||||||
close(pipeerr[1]);
|
close(pipeerr[1]);
|
||||||
|
|
||||||
ResumeThread ( pi.hThread ); /* now let cmd shell main thread be active s we have closed all i/o file handle that cmd will use */
|
ResumeThread(pi.hThread); /* now let cmd shell main thread be active s we have closed all i/o file handle that cmd will use */
|
||||||
SetConsoleCtrlHandler(NULL, TRUE);
|
SetConsoleCtrlHandler(NULL, TRUE);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -889,10 +809,10 @@ do_exec_no_pty(Session *s, const char *command)
|
|||||||
|
|
||||||
if (compat20)
|
if (compat20)
|
||||||
{
|
{
|
||||||
if ( s->ttyfd == -1)
|
if (s->ttyfd == -1)
|
||||||
session_set_fds(s, pipein[1], pipeout[0], pipeerr[0], s -> is_subsystem, 0);
|
session_set_fds(s, pipein[1], pipeout[0], pipeerr[0], s->is_subsystem, 0);
|
||||||
else
|
else
|
||||||
session_set_fds(s, pipein[1], pipeout[0], pipeerr[0], s -> is_subsystem, 1); // tty interactive session
|
session_set_fds(s, pipein[1], pipeout[0], pipeerr[0], s->is_subsystem, 1); // tty interactive session
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -905,11 +825,28 @@ do_exec_no_pty(Session *s, const char *command)
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
#else
|
}
|
||||||
|
|
||||||
/*
|
int
|
||||||
* Original OpenSSH code.
|
do_exec_no_pty(Session *s, const char *command) {
|
||||||
|
return do_exec_windows(s, command, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
do_exec_pty(Session *s, const char *command) {
|
||||||
|
return do_exec_windows(s, command, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This is called to fork and execute a command when we have no tty. This
|
||||||
|
* will call do_child from the child, and server_loop from the parent after
|
||||||
|
* setting up file descriptors and such.
|
||||||
*/
|
*/
|
||||||
|
int
|
||||||
|
do_exec_no_pty(Session *s, const char *command)
|
||||||
|
{
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
|
|
||||||
#ifdef USE_PIPES
|
#ifdef USE_PIPES
|
||||||
@ -1094,7 +1031,7 @@ do_exec_no_pty(Session *s, const char *command)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return 0;
|
return 0;
|
||||||
#endif /* else WIN32_FIXME */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -1106,11 +1043,6 @@ do_exec_no_pty(Session *s, const char *command)
|
|||||||
int
|
int
|
||||||
do_exec_pty(Session *s, const char *command)
|
do_exec_pty(Session *s, const char *command)
|
||||||
{
|
{
|
||||||
#ifndef WIN32_FIXME
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Original OpenSSH code.
|
|
||||||
*/
|
|
||||||
int fdout, ptyfd, ttyfd, ptymaster;
|
int fdout, ptyfd, ttyfd, ptymaster;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
|
|
||||||
@ -1222,17 +1154,8 @@ do_exec_pty(Session *s, const char *command)
|
|||||||
/* server_loop _has_ closed ptyfd and fdout. */
|
/* server_loop _has_ closed ptyfd and fdout. */
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
#else
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Win32 code.
|
|
||||||
*/
|
|
||||||
|
|
||||||
//return 0;
|
|
||||||
return do_exec_no_pty(s, command);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef LOGIN_NEEDS_UTMPX
|
#ifdef LOGIN_NEEDS_UTMPX
|
||||||
static void
|
static void
|
||||||
|
Loading…
x
Reference in New Issue
Block a user