Skip to content

Commit ed53543

Browse files
committed
simplified sshd fork code
1 parent 31989d6 commit ed53543

File tree

3 files changed

+57
-171
lines changed

3 files changed

+57
-171
lines changed

servconf.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,6 @@ initialize_server_options(ServerOptions *options)
182182
options->ip_qos_bulk = -1;
183183
options->version_addendum = NULL;
184184
options->fingerprint_hash = -1;
185-
#ifdef WIN32_FIXME
186-
options->i_am_a_fake_fork = 0;
187-
#endif
188185
}
189186

190187
/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */

servconf.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,7 @@ typedef struct {
198198
u_int num_auth_methods;
199199
char *auth_methods[MAX_AUTH_METHODS];
200200

201-
int fingerprint_hash;
202-
#ifdef WIN32_FIXME
203-
int i_am_a_fake_fork;
204-
char *pamLibrary_;
205-
#endif
206-
201+
int fingerprint_hash;
207202

208203
} ServerOptions;
209204

sshd.c

Lines changed: 56 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,79 +1569,64 @@ server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s)
15691569
}
15701570
break;
15711571
}
1572+
1573+
/*
1574+
* Normal production daemon. Fork, and have
1575+
* the child process the connection. The
1576+
* parent continues listening.
1577+
*/
1578+
platform_pre_fork();
15721579
#ifdef WIN32_FIXME
1573-
1574-
/*
1575-
* Win32 code.
1576-
*/
1580+
{
1581+
PROCESS_INFORMATION pi;
1582+
STARTUPINFO si;
1583+
BOOL b;
1584+
char path[MAX_PATH];
15771585

1578-
{
1579-
PROCESS_INFORMATION pi;
1580-
1581-
STARTUPINFO si;
1582-
1583-
BOOL b;
1586+
memset(&si, 0, sizeof(STARTUPINFO));
15841587

1588+
char remotesoc[64];
1589+
snprintf(remotesoc, sizeof(remotesoc), "%d", sfd_to_handle(*newsock));
1590+
SetEnvironmentVariable("SSHD_REMSOC", remotesoc);
15851591

1586-
/*
1587-
* Assign sockets to StartupInfo.
1588-
*/
1589-
1590-
memset(&si, 0 , sizeof(STARTUPINFO));
1591-
1592-
char remotesoc[64];
1593-
snprintf ( remotesoc, sizeof(remotesoc), "%d", sfd_to_handle(*newsock));
1594-
SetEnvironmentVariable("SSHD_REMSOC", remotesoc);
1595-
1596-
si.cb = sizeof(STARTUPINFO);
1597-
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE); //(HANDLE) sfd_to_handle(*newsock);
1598-
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
1599-
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
1600-
si.wShowWindow = SW_HIDE;
1601-
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
1602-
1603-
/*
1604-
* Create the child process
1605-
*/
1606-
1607-
b = CreateProcess(NULL, fake_fork_args, NULL, NULL, TRUE,
1608-
CREATE_NEW_PROCESS_GROUP, NULL, NULL,
1609-
&si, &pi);
1610-
if (!b)
1611-
{
1612-
debug("CreateProcess failure: %d", GetLastError());
1613-
1614-
exit(1);
1615-
}
1592+
si.cb = sizeof(STARTUPINFO);
1593+
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
1594+
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
1595+
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
1596+
si.wShowWindow = SW_HIDE;
1597+
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
16161598

1617-
/*
1618-
* Close child thread and process handles so it can go away
1619-
*/
1599+
/*
1600+
* Create the child process
1601+
*/
1602+
strncpy(path, GetCommandLineA(), MAX_PATH);
1603+
if (CreateProcess(NULL, path, NULL, NULL, TRUE,
1604+
CREATE_NEW_PROCESS_GROUP, NULL, NULL,
1605+
&si, &pi) == FALSE) {
1606+
debug("CreateProcess failure: %d", GetLastError());
1607+
exit(1);
1608+
}
16201609

1621-
CloseHandle(pi.hThread);
1622-
CloseHandle(pi.hProcess);
1623-
1624-
close(*newsock);
1610+
/*
1611+
* Close child thread and process handles so it can go away
1612+
*/
16251613

1626-
/*
1627-
* FIXME pipes are not used so instead of
1628-
* cleaning we can disable creation.
1629-
*/
1630-
1631-
close(startup_pipes[i]);
1632-
startup_pipes[i] = -1;
1633-
startups--;
1634-
}
1614+
CloseHandle(pi.hThread);
1615+
CloseHandle(pi.hProcess);
16351616

1636-
#else
1617+
close(*newsock);
1618+
close(startup_pipes[i]);
1619+
startup_pipes[i] = -1;
1620+
startups--;
1621+
1622+
pid = pi.dwProcessId;
1623+
}
1624+
1625+
if (pid == 0) {
1626+
#else
16371627

1638-
/*
1639-
* Normal production daemon. Fork, and have
1640-
* the child process the connection. The
1641-
* parent continues listening.
1642-
*/
1643-
platform_pre_fork();
16441628
if ((pid = fork()) == 0) {
1629+
#endif /* else WIN32_FIXME */
16451630
/*
16461631
* Child. Close the listening and
16471632
* max_startup sockets. Start using
@@ -1704,7 +1689,6 @@ server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s)
17041689
RAND_seed(rnd, sizeof(rnd));
17051690
#endif
17061691
explicit_bzero(rnd, sizeof(rnd));
1707-
#endif /* else WIN32_FIXME */
17081692
}
17091693

17101694
/* child process check (or debug mode) */
@@ -1719,21 +1703,6 @@ server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s)
17191703
* Win32 only.
17201704
*/
17211705

1722-
char *create_fake_fork_args(int ac, char **av)
1723-
{
1724-
char *orig_cmd_line = GetCommandLine();
1725-
1726-
char fake_fork_param[] = " -~";
1727-
1728-
int orig_cmd_line_len = strlen(orig_cmd_line);
1729-
1730-
char *new_cmd_line = xmalloc (orig_cmd_line_len + 1 + sizeof(fake_fork_param));
1731-
1732-
strcpy(new_cmd_line, orig_cmd_line);
1733-
strcpy(new_cmd_line + orig_cmd_line_len, fake_fork_param);
1734-
1735-
return new_cmd_line;
1736-
}
17371706

17381707
/*
17391708
* This function handles exit signal from parent process.
@@ -1826,16 +1795,7 @@ main(int ac, char **av)
18261795
saved_argv[i] = xstrdup(av[i]);
18271796
saved_argv[i] = NULL;
18281797

1829-
#ifdef WIN32_FIXME
1830-
1831-
/*
1832-
* Create arguments for starting fake forked sshd.exe instances.
1833-
*/
1834-
1835-
fake_fork_args = create_fake_fork_args(ac, av);
1836-
1837-
#endif /* WIN32_FIXME */
1838-
1798+
18391799
#ifndef HAVE_SETPROCTITLE
18401800
/* Prepare for later setproctitle emulation */
18411801
compat_init_setproctitle(ac, av);
@@ -1852,35 +1812,11 @@ main(int ac, char **av)
18521812

18531813
/* Initialize configuration options to their default values. */
18541814
initialize_server_options(&options);
1855-
1856-
1857-
#ifdef WIN32_FIXME
1858-
1859-
//debug_flag = 1;
1860-
1861-
#define FAKE_FORK_ARG "~"
1862-
1863-
#else
1864-
1865-
#define FAKE_FORK_ARG
1866-
1867-
#endif
18681815

18691816
/* Parse command-line arguments. */
18701817
while ((opt = getopt(ac, av,
1871-
"C:E:b:c:f:g:h:k:o:p:u:46DQRTdeiqrt" FAKE_FORK_ARG)) != -1) {
1818+
"C:E:b:c:f:g:h:k:o:p:u:46DQRTdeiqrt")) != -1) {
18721819
switch (opt) {
1873-
1874-
#ifdef WIN32_FIXME
1875-
case '~':
1876-
{
1877-
debug("fake fork child");
1878-
1879-
options.i_am_a_fake_fork = 1;
1880-
1881-
break;
1882-
}
1883-
#endif
18841820

18851821
case '4':
18861822
options.address_family = AF_INET;
@@ -2000,37 +1936,7 @@ main(int ac, char **av)
20001936
}
20011937

20021938
#ifdef WIN32_FIXME
2003-
2004-
/*
2005-
* Win32 only.
2006-
*/
2007-
2008-
/*
2009-
* Handle install and uninstall service options
2010-
*/
2011-
2012-
if (ac > 1 && strcmp("install", av[1]) == 0)
2013-
{
2014-
/*
2015-
* Install the service
2016-
*/
2017-
2018-
SvcInstall();
2019-
2020-
return 0;
2021-
}
2022-
else if (ac > 1 && strcmp("uninstall", av[1]) == 0)
2023-
{
2024-
/*
2025-
* Remove the service
2026-
*/
2027-
2028-
SvcUninstall();
2029-
2030-
return 0;
2031-
}
2032-
2033-
if (!options.i_am_a_fake_fork)
1939+
if (getenv("SSHD_REMSOC") == NULL)
20341940
{
20351941
if (!ranServiceMain)
20361942
{
@@ -2575,7 +2481,7 @@ main(int ac, char **av)
25752481
} else {
25762482
platform_pre_listen();
25772483
#ifdef WIN32_FIXME
2578-
if (!options.i_am_a_fake_fork)
2484+
if (getenv("SSHD_REMSOC") == NULL)
25792485
#endif
25802486
server_listen();
25812487

@@ -2604,7 +2510,7 @@ main(int ac, char **av)
26042510
}
26052511
#ifdef WIN32_FIXME
26062512

2607-
if (!options.i_am_a_fake_fork)
2513+
if (getenv("SSHD_REMSOC") == NULL)
26082514
{
26092515
/*
26102516
* Accept a connection and return in a forked child
@@ -2613,23 +2519,11 @@ main(int ac, char **av)
26132519
server_accept_loop(&sock_in, &sock_out, &newsock, config_s);
26142520
}
26152521
else
2616-
{
2617-
//STARTUPINFO si;
2618-
2619-
//memset(&si, 0 , sizeof(STARTUPINFO));
2620-
2621-
//si.cb = sizeof(STARTUPINFO);
2622-
2623-
/*
2624-
* Get the stdin handle from process info to use for client
2625-
*/
2626-
2627-
//GetStartupInfo(&si);
2628-
2522+
{
26292523
int remotesochandle ;
26302524
remotesochandle = atoi( getenv("SSHD_REMSOC") );
26312525

2632-
sock_in = sock_out = newsock = w32_allocate_fd_for_handle(remotesochandle, TRUE) ; //si.hStdInput);
2526+
sock_in = sock_out = newsock = w32_allocate_fd_for_handle(remotesochandle, TRUE) ;
26332527

26342528
// we have the socket handle, delete it for child processes we create like shell
26352529
SetEnvironmentVariable("SSHD_REMSOC", NULL);
@@ -3249,7 +3143,7 @@ cleanup_exit(int i)
32493143
audit_event(SSH_CONNECTION_ABANDON);
32503144
#endif
32513145
#ifdef WIN32_FIXME
3252-
if (!iAmAService || options.i_am_a_fake_fork)
3146+
if (!iAmAService || (getenv("SSHD_REMSOC")))
32533147
#endif
32543148
_exit(i);
32553149
}

0 commit comments

Comments
 (0)