Refactored do_exec* calls

This commit is contained in:
Manoj Ampalam 2016-10-18 21:41:00 -07:00
parent 92ddf4f9e1
commit 122ed4bf4d
2 changed files with 306 additions and 383 deletions

123
session.c
View File

@ -491,29 +491,12 @@ do_authenticated1(Authctxt *authctxt)
}
}
// PRAGMA:TODO
#ifndef WIN32_FIXME
#define USE_PIPES 1
#endif
#ifdef WINDOWS
#ifdef WIN32_FIXME
extern int debug_flag;
#endif
int do_exec_windows(Session *s, const char *command, int pty) {
/*
* 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);
/*
* Win32 code.
*/
extern int debug_flag;
if (s->is_subsystem == SUBSYSTEM_INT_SFTP_ERROR)
{
@ -544,9 +527,6 @@ do_exec_no_pty(Session *s, const char *command)
HANDLE hToken = INVALID_HANDLE_VALUE;
int do_xauth;
FILE *f = NULL;
char cmd[1024];
char *exec_command;
@ -558,30 +538,13 @@ do_exec_no_pty(Session *s, const char *command)
if (!command)
{
#ifndef WIN32_PRAGMA_REMCON
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
{
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
*/
@ -639,45 +602,6 @@ do_exec_no_pty(Session *s, const char *command)
SetEnvironmentVariable("USERNAME", 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[1]);
set_nonblock(pipeout[0]);
@ -685,8 +609,6 @@ do_exec_no_pty(Session *s, const char *command)
set_nonblock(pipeerr[0]);
set_nonblock(pipeerr[1]);
#endif
/*
* If we get this far, the user has already been authenticated
* We should either have a user token in authctxt -> methoddata
@ -723,8 +645,6 @@ do_exec_no_pty(Session *s, const char *command)
SetEnvironmentVariableW(L"HOMEPATH", (wstr + 1));
}
// find the server name of the domain controller which created this token
GetDomainFromToken(&hToken, buf, sizeof(buf));
if (buf[0])
@ -905,11 +825,28 @@ do_exec_no_pty(Session *s, const char *command)
return 0;
}
int
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
/*
* Original OpenSSH code.
* 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;
#ifdef USE_PIPES
@ -1094,7 +1031,7 @@ do_exec_no_pty(Session *s, const char *command)
}
#endif
return 0;
#endif /* else WIN32_FIXME */
}
/*
@ -1106,11 +1043,6 @@ do_exec_no_pty(Session *s, const char *command)
int
do_exec_pty(Session *s, const char *command)
{
#ifndef WIN32_FIXME
/*
* Original OpenSSH code.
*/
int fdout, ptyfd, ttyfd, ptymaster;
pid_t pid;
@ -1222,17 +1154,8 @@ do_exec_pty(Session *s, const char *command)
/* server_loop _has_ closed ptyfd and fdout. */
}
return 0;
#else
/*
* Win32 code.
*/
//return 0;
return do_exec_no_pty(s, command);
#endif
}
#endif
#ifdef LOGIN_NEEDS_UTMPX
static void