mirror of
https://github.com/PowerShell/Win32-OpenSSH.git
synced 2025-07-25 15:04:54 +02:00
session refactoring part 1
This commit is contained in:
parent
601c2b6e81
commit
e626a748ca
@ -75,6 +75,7 @@ void w32_freeaddrinfo(struct addrinfo *);
|
|||||||
int w32_getaddrinfo(const char *, const char *,
|
int w32_getaddrinfo(const char *, const char *,
|
||||||
const struct addrinfo *, struct addrinfo **);
|
const struct addrinfo *, struct addrinfo **);
|
||||||
FILE* w32_fopen_utf8(const char *, const char *);
|
FILE* w32_fopen_utf8(const char *, const char *);
|
||||||
|
char* w32_programdir();
|
||||||
|
|
||||||
|
|
||||||
/* Shutdown constants */
|
/* Shutdown constants */
|
||||||
|
@ -140,4 +140,28 @@ utf16_to_utf8(const wchar_t* utf16) {
|
|||||||
WideCharToMultiByte(CP_UTF8, 0, utf16, -1, utf8, needed, NULL, NULL) == 0)
|
WideCharToMultiByte(CP_UTF8, 0, utf16, -1, utf8, needed, NULL, NULL) == 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
return utf8;
|
return utf8;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char* s_programdir = NULL;
|
||||||
|
char* w32_programdir() {
|
||||||
|
if (s_programdir != NULL)
|
||||||
|
return s_programdir;
|
||||||
|
|
||||||
|
if ((s_programdir = utf16_to_utf8(_wpgmptr)) == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
/* null terminate after directory path */
|
||||||
|
{
|
||||||
|
char* tail = s_programdir + strlen(s_programdir);
|
||||||
|
while (tail > s_programdir && *tail != '\\' && *tail != '/')
|
||||||
|
tail--;
|
||||||
|
|
||||||
|
if (tail > s_programdir)
|
||||||
|
*tail = '\0';
|
||||||
|
else
|
||||||
|
*tail = '.'; /* current directory */
|
||||||
|
}
|
||||||
|
|
||||||
|
return s_programdir;
|
||||||
|
|
||||||
}
|
}
|
@ -125,8 +125,10 @@ w32posix_initialize() {
|
|||||||
|| (socketio_initialize() != 0))
|
|| (socketio_initialize() != 0))
|
||||||
DebugBreak();
|
DebugBreak();
|
||||||
main_thread = OpenThread(THREAD_SET_CONTEXT, FALSE, GetCurrentThreadId());
|
main_thread = OpenThread(THREAD_SET_CONTEXT, FALSE, GetCurrentThreadId());
|
||||||
if ((main_thread == NULL) || (sw_initialize() != 0))
|
if ((main_thread == NULL) || (sw_initialize() != 0) || w32_programdir() == NULL) {
|
||||||
DebugBreak();
|
DebugBreak();
|
||||||
|
fatal("failed to initialize w32posix wrapper");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
83
session.c
83
session.c
@ -491,17 +491,64 @@ do_authenticated1(Authctxt *authctxt)
|
|||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
|
|
||||||
int do_exec_windows(Session *s, const char *command, int pty) {
|
int do_exec_windows(Session *s, const char *command, int pty) {
|
||||||
|
int pipein[2], pipeout[2], pipeerr[2], r;
|
||||||
|
char *exec_command = NULL, *progdir = w32_programdir();
|
||||||
|
wchar_t* exec_command_w = NULL;
|
||||||
|
|
||||||
|
if (s->is_subsystem >= SUBSYSTEM_INT_SFTP_ERROR)
|
||||||
|
{
|
||||||
|
error("sub system not supported, exiting\n");
|
||||||
|
fflush(NULL);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Create three pipes for stdin, stdout and stderr */
|
||||||
|
if (pipe(pipein) == -1 || pipe(pipeout) == -1 || pipe(pipeerr) == -1) {
|
||||||
|
error("%s: cannot create pipe: %.100s", __func__, strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
set_nonblock(pipein[0]);
|
||||||
|
set_nonblock(pipein[1]);
|
||||||
|
set_nonblock(pipeout[0]);
|
||||||
|
set_nonblock(pipeout[1]);
|
||||||
|
set_nonblock(pipeerr[0]);
|
||||||
|
set_nonblock(pipeerr[1]);
|
||||||
|
|
||||||
|
fcntl(pipein[1], F_SETFD, FD_CLOEXEC);
|
||||||
|
fcntl(pipeout[0], F_SETFD, FD_CLOEXEC);
|
||||||
|
fcntl(pipeerr[0], F_SETFD, FD_CLOEXEC);
|
||||||
|
|
||||||
|
/* prepare exec - path used with CreateProcess() */
|
||||||
|
if (s->is_subsystem) {
|
||||||
|
/* relative or absolute */
|
||||||
|
if (command == NULL || command[0] == '\0')
|
||||||
|
fatal("expecting command for a subsystem");
|
||||||
|
|
||||||
|
if (command[1] != ':') /* absolute */
|
||||||
|
exec_command = xstrdup(command);
|
||||||
|
else {/*relative*/
|
||||||
|
exec_command = malloc(strlen(progdir) + 1 + strlen(command));
|
||||||
|
if (exec_command == NULL)
|
||||||
|
fatal("%s, out of memory");
|
||||||
|
memcpy(exec_command, progdir, strlen(progdir));
|
||||||
|
exec_command[strlen(progdir)] = '\\';
|
||||||
|
memcpy(exec_command + strlen(progdir) + 1, command, strlen(command) + 1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
char* shell_host = pty ? "ssh-shellhost.exe -t " : "ssh-shellhost.exe ";
|
||||||
|
exec_command = malloc(strlen(progdir) + strlen(shell_host) + (command ? strlen(command) : 0));
|
||||||
|
if (exec_command == NULL)
|
||||||
|
fatal("%s, out of memory");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
extern int debug_flag;
|
||||||
|
|
||||||
PROCESS_INFORMATION pi;
|
PROCESS_INFORMATION pi;
|
||||||
STARTUPINFOW si;
|
STARTUPINFOW si;
|
||||||
|
|
||||||
int pipein[2];
|
|
||||||
int pipeout[2];
|
|
||||||
int pipeerr[2];
|
|
||||||
|
|
||||||
BOOL b;
|
BOOL b;
|
||||||
|
|
||||||
HANDLE hToken = INVALID_HANDLE_VALUE;
|
HANDLE hToken = INVALID_HANDLE_VALUE;
|
||||||
@ -512,12 +559,6 @@ int do_exec_windows(Session *s, const char *command, int pty) {
|
|||||||
char *laddr;
|
char *laddr;
|
||||||
char buf[256];
|
char buf[256];
|
||||||
|
|
||||||
if (s->is_subsystem >= SUBSYSTEM_INT_SFTP_ERROR)
|
|
||||||
{
|
|
||||||
error("sub system not supported, exiting\n");
|
|
||||||
fflush(NULL);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (!command)
|
if (!command)
|
||||||
@ -529,27 +570,12 @@ int do_exec_windows(Session *s, const char *command, int pty) {
|
|||||||
exec_command = command;
|
exec_command = command;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Create three socket pairs for stdin, stdout and stderr
|
|
||||||
*/
|
|
||||||
pipe(pipein);
|
|
||||||
pipe(pipeout);
|
|
||||||
pipe(pipeerr);
|
|
||||||
|
|
||||||
int retcode = -1;
|
int retcode = -1;
|
||||||
if ((!s->is_subsystem) && (s->ttyfd != -1))
|
if ((!s->is_subsystem) && (s->ttyfd != -1))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
debug3("sockin[0]: %d sockin[1]: %d", pipein[0], pipein[1]);
|
|
||||||
debug3("sockout[0]: %d sockout[1]: %d", pipeout[0], pipeout[1]);
|
|
||||||
debug3("sockerr[0]: %d sockerr[1]: %d", pipeerr[0], pipeerr[1]);
|
|
||||||
|
|
||||||
|
|
||||||
SetHandleInformation(sfd_to_handle(pipein[1]), HANDLE_FLAG_INHERIT, 0);
|
|
||||||
SetHandleInformation(sfd_to_handle(pipeout[0]), HANDLE_FLAG_INHERIT, 0);
|
|
||||||
SetHandleInformation(sfd_to_handle(pipeerr[0]), HANDLE_FLAG_INHERIT, 0);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Assign sockets to StartupInfo
|
* Assign sockets to StartupInfo
|
||||||
*/
|
*/
|
||||||
@ -580,13 +606,6 @@ int do_exec_windows(Session *s, const char *command, int pty) {
|
|||||||
SetEnvironmentVariable("USERNAME", s->pw->pw_name);
|
SetEnvironmentVariable("USERNAME", s->pw->pw_name);
|
||||||
SetEnvironmentVariable("LOGNAME", s->pw->pw_name);
|
SetEnvironmentVariable("LOGNAME", s->pw->pw_name);
|
||||||
|
|
||||||
set_nonblock(pipein[0]);
|
|
||||||
set_nonblock(pipein[1]);
|
|
||||||
set_nonblock(pipeout[0]);
|
|
||||||
set_nonblock(pipeout[1]);
|
|
||||||
set_nonblock(pipeerr[0]);
|
|
||||||
set_nonblock(pipeerr[1]);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 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
|
||||||
|
33
sshd.c
33
sshd.c
@ -281,25 +281,16 @@ static void do_ssh2_kex(void);
|
|||||||
{
|
{
|
||||||
int exitCode = -1;
|
int exitCode = -1;
|
||||||
|
|
||||||
//
|
/* Windows */
|
||||||
// Windows.
|
#ifdef WINDOWS
|
||||||
//
|
|
||||||
|
char* dir = w32_programdir();
|
||||||
#ifdef WIN32_FIXME
|
if (strnlen(dir, pathSize) == pathSize)
|
||||||
|
error("program directory path size exceeded provided pathSize %d", pathSize);
|
||||||
if (GetModuleFileName(NULL, path, pathSize)){
|
else {
|
||||||
int i;
|
memcpy(path, dir, strnlen(dir, pathSize) + 1);
|
||||||
int lastSlashPos = 0;
|
|
||||||
|
|
||||||
for (i = 0; path[i]; i++) {
|
|
||||||
if (path[i] == '/' || path[i] == '\\') {
|
|
||||||
lastSlashPos = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
path[lastSlashPos] = 0;
|
|
||||||
exitCode = 0;
|
exitCode = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -1972,11 +1963,7 @@ main(int ac, char **av)
|
|||||||
|
|
||||||
struct stat s;
|
struct stat s;
|
||||||
|
|
||||||
#ifdef WIN32_FIXME
|
#define PATH_SIZE PATH_MAX
|
||||||
#define PATH_SIZE MAX_PATH
|
|
||||||
#else
|
|
||||||
#define PATH_SIZE PATH_MAX
|
|
||||||
#endif
|
|
||||||
|
|
||||||
char basePath[PATH_SIZE] = {0};
|
char basePath[PATH_SIZE] = {0};
|
||||||
char path[PATH_SIZE] = {0};
|
char path[PATH_SIZE] = {0};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user