simplified sshd fork code

This commit is contained in:
manojampalam 2016-03-25 18:19:05 -07:00
parent 31989d6f07
commit ed535436d0
3 changed files with 63 additions and 177 deletions

View File

@ -182,9 +182,6 @@ initialize_server_options(ServerOptions *options)
options->ip_qos_bulk = -1; options->ip_qos_bulk = -1;
options->version_addendum = NULL; options->version_addendum = NULL;
options->fingerprint_hash = -1; options->fingerprint_hash = -1;
#ifdef WIN32_FIXME
options->i_am_a_fake_fork = 0;
#endif
} }
/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */ /* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */

View File

@ -199,11 +199,6 @@ typedef struct {
char *auth_methods[MAX_AUTH_METHODS]; char *auth_methods[MAX_AUTH_METHODS];
int fingerprint_hash; int fingerprint_hash;
#ifdef WIN32_FIXME
int i_am_a_fake_fork;
char *pamLibrary_;
#endif
} ServerOptions; } ServerOptions;

146
sshd.c
View File

@ -1569,23 +1569,19 @@ server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s)
} }
break; break;
} }
#ifdef WIN32_FIXME
/* /*
* Win32 code. * Normal production daemon. Fork, and have
* the child process the connection. The
* parent continues listening.
*/ */
platform_pre_fork();
#ifdef WIN32_FIXME
{ {
PROCESS_INFORMATION pi; PROCESS_INFORMATION pi;
STARTUPINFO si; STARTUPINFO si;
BOOL b; BOOL b;
char path[MAX_PATH];
/*
* Assign sockets to StartupInfo.
*/
memset(&si, 0, sizeof(STARTUPINFO)); memset(&si, 0, sizeof(STARTUPINFO));
@ -1594,7 +1590,7 @@ server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s)
SetEnvironmentVariable("SSHD_REMSOC", remotesoc); SetEnvironmentVariable("SSHD_REMSOC", remotesoc);
si.cb = sizeof(STARTUPINFO); si.cb = sizeof(STARTUPINFO);
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE); //(HANDLE) sfd_to_handle(*newsock); si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
si.hStdError = GetStdHandle(STD_ERROR_HANDLE); si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
si.wShowWindow = SW_HIDE; si.wShowWindow = SW_HIDE;
@ -1603,14 +1599,11 @@ server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s)
/* /*
* Create the child process * Create the child process
*/ */
strncpy(path, GetCommandLineA(), MAX_PATH);
b = CreateProcess(NULL, fake_fork_args, NULL, NULL, TRUE, if (CreateProcess(NULL, path, NULL, NULL, TRUE,
CREATE_NEW_PROCESS_GROUP, NULL, NULL, CREATE_NEW_PROCESS_GROUP, NULL, NULL,
&si, &pi); &si, &pi) == FALSE) {
if (!b)
{
debug("CreateProcess failure: %d", GetLastError()); debug("CreateProcess failure: %d", GetLastError());
exit(1); exit(1);
} }
@ -1622,26 +1615,18 @@ server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s)
CloseHandle(pi.hProcess); CloseHandle(pi.hProcess);
close(*newsock); close(*newsock);
/*
* FIXME pipes are not used so instead of
* cleaning we can disable creation.
*/
close(startup_pipes[i]); close(startup_pipes[i]);
startup_pipes[i] = -1; startup_pipes[i] = -1;
startups--; startups--;
pid = pi.dwProcessId;
} }
if (pid == 0) {
#else #else
/*
* Normal production daemon. Fork, and have
* the child process the connection. The
* parent continues listening.
*/
platform_pre_fork();
if ((pid = fork()) == 0) { if ((pid = fork()) == 0) {
#endif /* else WIN32_FIXME */
/* /*
* Child. Close the listening and * Child. Close the listening and
* max_startup sockets. Start using * max_startup sockets. Start using
@ -1704,7 +1689,6 @@ server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s)
RAND_seed(rnd, sizeof(rnd)); RAND_seed(rnd, sizeof(rnd));
#endif #endif
explicit_bzero(rnd, sizeof(rnd)); explicit_bzero(rnd, sizeof(rnd));
#endif /* else WIN32_FIXME */
} }
/* child process check (or debug mode) */ /* child process check (or debug mode) */
@ -1719,21 +1703,6 @@ server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s)
* Win32 only. * Win32 only.
*/ */
char *create_fake_fork_args(int ac, char **av)
{
char *orig_cmd_line = GetCommandLine();
char fake_fork_param[] = " -~";
int orig_cmd_line_len = strlen(orig_cmd_line);
char *new_cmd_line = xmalloc (orig_cmd_line_len + 1 + sizeof(fake_fork_param));
strcpy(new_cmd_line, orig_cmd_line);
strcpy(new_cmd_line + orig_cmd_line_len, fake_fork_param);
return new_cmd_line;
}
/* /*
* This function handles exit signal from parent process. * This function handles exit signal from parent process.
@ -1826,15 +1795,6 @@ main(int ac, char **av)
saved_argv[i] = xstrdup(av[i]); saved_argv[i] = xstrdup(av[i]);
saved_argv[i] = NULL; saved_argv[i] = NULL;
#ifdef WIN32_FIXME
/*
* Create arguments for starting fake forked sshd.exe instances.
*/
fake_fork_args = create_fake_fork_args(ac, av);
#endif /* WIN32_FIXME */
#ifndef HAVE_SETPROCTITLE #ifndef HAVE_SETPROCTITLE
/* Prepare for later setproctitle emulation */ /* Prepare for later setproctitle emulation */
@ -1853,35 +1813,11 @@ main(int ac, char **av)
/* Initialize configuration options to their default values. */ /* Initialize configuration options to their default values. */
initialize_server_options(&options); initialize_server_options(&options);
#ifdef WIN32_FIXME
//debug_flag = 1;
#define FAKE_FORK_ARG "~"
#else
#define FAKE_FORK_ARG
#endif
/* Parse command-line arguments. */ /* Parse command-line arguments. */
while ((opt = getopt(ac, av, while ((opt = getopt(ac, av,
"C:E:b:c:f:g:h:k:o:p:u:46DQRTdeiqrt" FAKE_FORK_ARG)) != -1) { "C:E:b:c:f:g:h:k:o:p:u:46DQRTdeiqrt")) != -1) {
switch (opt) { switch (opt) {
#ifdef WIN32_FIXME
case '~':
{
debug("fake fork child");
options.i_am_a_fake_fork = 1;
break;
}
#endif
case '4': case '4':
options.address_family = AF_INET; options.address_family = AF_INET;
break; break;
@ -2000,37 +1936,7 @@ main(int ac, char **av)
} }
#ifdef WIN32_FIXME #ifdef WIN32_FIXME
if (getenv("SSHD_REMSOC") == NULL)
/*
* Win32 only.
*/
/*
* Handle install and uninstall service options
*/
if (ac > 1 && strcmp("install", av[1]) == 0)
{
/*
* Install the service
*/
SvcInstall();
return 0;
}
else if (ac > 1 && strcmp("uninstall", av[1]) == 0)
{
/*
* Remove the service
*/
SvcUninstall();
return 0;
}
if (!options.i_am_a_fake_fork)
{ {
if (!ranServiceMain) if (!ranServiceMain)
{ {
@ -2575,7 +2481,7 @@ main(int ac, char **av)
} else { } else {
platform_pre_listen(); platform_pre_listen();
#ifdef WIN32_FIXME #ifdef WIN32_FIXME
if (!options.i_am_a_fake_fork) if (getenv("SSHD_REMSOC") == NULL)
#endif #endif
server_listen(); server_listen();
@ -2604,7 +2510,7 @@ main(int ac, char **av)
} }
#ifdef WIN32_FIXME #ifdef WIN32_FIXME
if (!options.i_am_a_fake_fork) if (getenv("SSHD_REMSOC") == NULL)
{ {
/* /*
* Accept a connection and return in a forked child * Accept a connection and return in a forked child
@ -2614,22 +2520,10 @@ main(int ac, char **av)
} }
else else
{ {
//STARTUPINFO si;
//memset(&si, 0 , sizeof(STARTUPINFO));
//si.cb = sizeof(STARTUPINFO);
/*
* Get the stdin handle from process info to use for client
*/
//GetStartupInfo(&si);
int remotesochandle ; int remotesochandle ;
remotesochandle = atoi( getenv("SSHD_REMSOC") ); remotesochandle = atoi( getenv("SSHD_REMSOC") );
sock_in = sock_out = newsock = w32_allocate_fd_for_handle(remotesochandle, TRUE) ; //si.hStdInput); sock_in = sock_out = newsock = w32_allocate_fd_for_handle(remotesochandle, TRUE) ;
// we have the socket handle, delete it for child processes we create like shell // we have the socket handle, delete it for child processes we create like shell
SetEnvironmentVariable("SSHD_REMSOC", NULL); SetEnvironmentVariable("SSHD_REMSOC", NULL);
@ -3249,7 +3143,7 @@ cleanup_exit(int i)
audit_event(SSH_CONNECTION_ABANDON); audit_event(SSH_CONNECTION_ABANDON);
#endif #endif
#ifdef WIN32_FIXME #ifdef WIN32_FIXME
if (!iAmAService || options.i_am_a_fake_fork) if (!iAmAService || (getenv("SSHD_REMSOC")))
#endif #endif
_exit(i); _exit(i);
} }