Skip to content

Commit e626a74

Browse files
committed
session refactoring part 1
1 parent 601c2b6 commit e626a74

File tree

5 files changed

+90
-57
lines changed

5 files changed

+90
-57
lines changed

contrib/win32/win32compat/inc/w32posix.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ void w32_freeaddrinfo(struct addrinfo *);
7575
int w32_getaddrinfo(const char *, const char *,
7676
const struct addrinfo *, struct addrinfo **);
7777
FILE* w32_fopen_utf8(const char *, const char *);
78+
char* w32_programdir();
7879

7980

8081
/* Shutdown constants */

contrib/win32/win32compat/misc.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,28 @@ utf16_to_utf8(const wchar_t* utf16) {
140140
WideCharToMultiByte(CP_UTF8, 0, utf16, -1, utf8, needed, NULL, NULL) == 0)
141141
return NULL;
142142
return utf8;
143+
}
144+
145+
static char* s_programdir = NULL;
146+
char* w32_programdir() {
147+
if (s_programdir != NULL)
148+
return s_programdir;
149+
150+
if ((s_programdir = utf16_to_utf8(_wpgmptr)) == NULL)
151+
return NULL;
152+
153+
/* null terminate after directory path */
154+
{
155+
char* tail = s_programdir + strlen(s_programdir);
156+
while (tail > s_programdir && *tail != '\\' && *tail != '/')
157+
tail--;
158+
159+
if (tail > s_programdir)
160+
*tail = '\0';
161+
else
162+
*tail = '.'; /* current directory */
163+
}
164+
165+
return s_programdir;
166+
143167
}

contrib/win32/win32compat/w32fd.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,10 @@ w32posix_initialize() {
125125
|| (socketio_initialize() != 0))
126126
DebugBreak();
127127
main_thread = OpenThread(THREAD_SET_CONTEXT, FALSE, GetCurrentThreadId());
128-
if ((main_thread == NULL) || (sw_initialize() != 0))
129-
DebugBreak();
128+
if ((main_thread == NULL) || (sw_initialize() != 0) || w32_programdir() == NULL) {
129+
DebugBreak();
130+
fatal("failed to initialize w32posix wrapper");
131+
}
130132
}
131133

132134
void

session.c

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -491,17 +491,64 @@ do_authenticated1(Authctxt *authctxt)
491491
#ifdef WINDOWS
492492

493493
int do_exec_windows(Session *s, const char *command, int pty) {
494+
int pipein[2], pipeout[2], pipeerr[2], r;
495+
char *exec_command = NULL, *progdir = w32_programdir();
496+
wchar_t* exec_command_w = NULL;
494497

498+
if (s->is_subsystem >= SUBSYSTEM_INT_SFTP_ERROR)
499+
{
500+
error("sub system not supported, exiting\n");
501+
fflush(NULL);
502+
exit(1);
503+
}
504+
505+
/* Create three pipes for stdin, stdout and stderr */
506+
if (pipe(pipein) == -1 || pipe(pipeout) == -1 || pipe(pipeerr) == -1) {
507+
error("%s: cannot create pipe: %.100s", __func__, strerror(errno));
508+
return -1;
509+
}
510+
511+
set_nonblock(pipein[0]);
512+
set_nonblock(pipein[1]);
513+
set_nonblock(pipeout[0]);
514+
set_nonblock(pipeout[1]);
515+
set_nonblock(pipeerr[0]);
516+
set_nonblock(pipeerr[1]);
517+
518+
fcntl(pipein[1], F_SETFD, FD_CLOEXEC);
519+
fcntl(pipeout[0], F_SETFD, FD_CLOEXEC);
520+
fcntl(pipeerr[0], F_SETFD, FD_CLOEXEC);
521+
522+
/* prepare exec - path used with CreateProcess() */
523+
if (s->is_subsystem) {
524+
/* relative or absolute */
525+
if (command == NULL || command[0] == '\0')
526+
fatal("expecting command for a subsystem");
527+
528+
if (command[1] != ':') /* absolute */
529+
exec_command = xstrdup(command);
530+
else {/*relative*/
531+
exec_command = malloc(strlen(progdir) + 1 + strlen(command));
532+
if (exec_command == NULL)
533+
fatal("%s, out of memory");
534+
memcpy(exec_command, progdir, strlen(progdir));
535+
exec_command[strlen(progdir)] = '\\';
536+
memcpy(exec_command + strlen(progdir) + 1, command, strlen(command) + 1);
537+
}
538+
} else {
539+
char* shell_host = pty ? "ssh-shellhost.exe -t " : "ssh-shellhost.exe ";
540+
exec_command = malloc(strlen(progdir) + strlen(shell_host) + (command ? strlen(command) : 0));
541+
if (exec_command == NULL)
542+
fatal("%s, out of memory");
543+
544+
}
545+
495546
wchar_t* pw_dir_utf16 = utf8_to_utf16(s->pw->pw_dir);
496547
extern int debug_flag;
497548

498549
PROCESS_INFORMATION pi;
499550
STARTUPINFOW si;
500551

501-
int pipein[2];
502-
int pipeout[2];
503-
int pipeerr[2];
504-
505552
BOOL b;
506553

507554
HANDLE hToken = INVALID_HANDLE_VALUE;
@@ -512,12 +559,6 @@ int do_exec_windows(Session *s, const char *command, int pty) {
512559
char *laddr;
513560
char buf[256];
514561

515-
if (s->is_subsystem >= SUBSYSTEM_INT_SFTP_ERROR)
516-
{
517-
error("sub system not supported, exiting\n");
518-
fflush(NULL);
519-
exit(1);
520-
}
521562

522563

523564
if (!command)
@@ -529,27 +570,12 @@ int do_exec_windows(Session *s, const char *command, int pty) {
529570
exec_command = command;
530571
}
531572

532-
/*
533-
* Create three socket pairs for stdin, stdout and stderr
534-
*/
535-
pipe(pipein);
536-
pipe(pipeout);
537-
pipe(pipeerr);
538573

539574
int retcode = -1;
540575
if ((!s->is_subsystem) && (s->ttyfd != -1))
541576
{
542577
}
543578

544-
debug3("sockin[0]: %d sockin[1]: %d", pipein[0], pipein[1]);
545-
debug3("sockout[0]: %d sockout[1]: %d", pipeout[0], pipeout[1]);
546-
debug3("sockerr[0]: %d sockerr[1]: %d", pipeerr[0], pipeerr[1]);
547-
548-
549-
SetHandleInformation(sfd_to_handle(pipein[1]), HANDLE_FLAG_INHERIT, 0);
550-
SetHandleInformation(sfd_to_handle(pipeout[0]), HANDLE_FLAG_INHERIT, 0);
551-
SetHandleInformation(sfd_to_handle(pipeerr[0]), HANDLE_FLAG_INHERIT, 0);
552-
553579
/*
554580
* Assign sockets to StartupInfo
555581
*/
@@ -580,13 +606,6 @@ int do_exec_windows(Session *s, const char *command, int pty) {
580606
SetEnvironmentVariable("USERNAME", s->pw->pw_name);
581607
SetEnvironmentVariable("LOGNAME", s->pw->pw_name);
582608

583-
set_nonblock(pipein[0]);
584-
set_nonblock(pipein[1]);
585-
set_nonblock(pipeout[0]);
586-
set_nonblock(pipeout[1]);
587-
set_nonblock(pipeerr[0]);
588-
set_nonblock(pipeerr[1]);
589-
590609
/*
591610
* If we get this far, the user has already been authenticated
592611
* We should either have a user token in authctxt -> methoddata

sshd.c

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -281,25 +281,16 @@ static void do_ssh2_kex(void);
281281
{
282282
int exitCode = -1;
283283

284-
//
285-
// Windows.
286-
//
287-
288-
#ifdef WIN32_FIXME
289-
290-
if (GetModuleFileName(NULL, path, pathSize)){
291-
int i;
292-
int lastSlashPos = 0;
293-
294-
for (i = 0; path[i]; i++) {
295-
if (path[i] == '/' || path[i] == '\\') {
296-
lastSlashPos = i;
297-
}
298-
}
299-
300-
path[lastSlashPos] = 0;
284+
/* Windows */
285+
#ifdef WINDOWS
286+
287+
char* dir = w32_programdir();
288+
if (strnlen(dir, pathSize) == pathSize)
289+
error("program directory path size exceeded provided pathSize %d", pathSize);
290+
else {
291+
memcpy(path, dir, strnlen(dir, pathSize) + 1);
301292
exitCode = 0;
302-
}
293+
}
303294

304295
#endif
305296

@@ -1972,11 +1963,7 @@ main(int ac, char **av)
19721963

19731964
struct stat s;
19741965

1975-
#ifdef WIN32_FIXME
1976-
#define PATH_SIZE MAX_PATH
1977-
#else
1978-
#define PATH_SIZE PATH_MAX
1979-
#endif
1966+
#define PATH_SIZE PATH_MAX
19801967

19811968
char basePath[PATH_SIZE] = {0};
19821969
char path[PATH_SIZE] = {0};

0 commit comments

Comments
 (0)