Skip to content

Commit 9fc5b65

Browse files
committed
fixes to spawn sshd-auth properly
1 parent 6b010c6 commit 9fc5b65

File tree

5 files changed

+221
-80
lines changed

5 files changed

+221
-80
lines changed

contrib/win32/openssh/sshd-auth.vcxproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,6 @@
448448
<ClCompile Include="$(OpenSSH-Src-Path)gss-serv-krb5.c" />
449449
<ClCompile Include="$(OpenSSH-Src-Path)gss-serv.c" />
450450
<ClCompile Include="$(OpenSSH-Src-Path)loginrec.c" />
451-
<ClCompile Include="$(OpenSSH-Src-Path)monitor.c" />
452451
<ClCompile Include="$(OpenSSH-Src-Path)monitor_wrap.c" />
453452
<ClCompile Include="$(OpenSSH-Src-Path)platform.c" />
454453
<ClCompile Include="$(OpenSSH-Src-Path)sandbox-capsicum.c" />
@@ -461,7 +460,6 @@
461460
<ClCompile Include="$(OpenSSH-Src-Path)session.c" />
462461
<ClCompile Include="$(OpenSSH-Src-Path)sftp-common.c" />
463462
<ClCompile Include="$(OpenSSH-Src-Path)sshlogin.c" />
464-
<ClCompile Include="$(OpenSSH-Src-Path)sshd-session.c" />
465463
<ClCompile Include="$(OpenSSH-Src-Path)contrib\win32\win32compat\win32_groupaccess.c" />
466464
<ClCompile Include="$(OpenSSH-Src-Path)contrib\win32\win32compat\win32_sshpty.c" />
467465
<ClCompile Include="$(OpenSSH-Src-Path)contrib\win32\win32compat\w32-doexec.c" />
@@ -471,9 +469,10 @@
471469
<ClCompile Include="..\..\..\platform-listen.c" />
472470
<ClCompile Include="..\..\..\srclimit.c" />
473471
<ClCompile Include="..\..\..\ssh-sk-client.c" />
472+
<ClCompile Include="..\..\..\sshd-auth.c" />
474473
<ClCompile Include="..\..\..\sshkey.c" />
475474
<ClCompile Include="..\win32compat\win32-utf8.c" />
476-
<ClCompile Include="..\win32compat\wmain_sshd-session.c" />
475+
<ClCompile Include="..\win32compat\wmain_sshd-auth.c" />
477476
</ItemGroup>
478477
<ItemGroup>
479478
<ResourceCompile Include="version.rc" />

contrib/win32/openssh/sshd-auth.vcxproj.filters

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,6 @@
8484
<ClCompile Include="$(OpenSSH-Src-Path)loginrec.c">
8585
<Filter>Source Files</Filter>
8686
</ClCompile>
87-
<ClCompile Include="$(OpenSSH-Src-Path)monitor.c">
88-
<Filter>Source Files</Filter>
89-
</ClCompile>
9087
<ClCompile Include="$(OpenSSH-Src-Path)monitor_wrap.c">
9188
<Filter>Source Files</Filter>
9289
</ClCompile>
@@ -120,9 +117,6 @@
120117
<ClCompile Include="$(OpenSSH-Src-Path)sftp-common.c">
121118
<Filter>Source Files</Filter>
122119
</ClCompile>
123-
<ClCompile Include="$(OpenSSH-Src-Path)sshd-session.c">
124-
<Filter>Source Files</Filter>
125-
</ClCompile>
126120
<ClCompile Include="$(OpenSSH-Src-Path)sshlogin.c">
127121
<Filter>Source Files</Filter>
128122
</ClCompile>
@@ -156,10 +150,13 @@
156150
<ClCompile Include="..\..\..\auth2-methods.c">
157151
<Filter>Source Files</Filter>
158152
</ClCompile>
159-
<ClCompile Include="..\win32compat\wmain_sshd-session.c">
153+
<ClCompile Include="..\..\..\platform-listen.c">
160154
<Filter>Source Files</Filter>
161155
</ClCompile>
162-
<ClCompile Include="..\..\..\platform-listen.c">
156+
<ClCompile Include="..\win32compat\wmain_sshd-auth.c">
157+
<Filter>Source Files</Filter>
158+
</ClCompile>
159+
<ClCompile Include="..\..\..\sshd-auth.c">
163160
<Filter>Source Files</Filter>
164161
</ClCompile>
165162
</ItemGroup>
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Author: Manoj Ampalam <[email protected]>
3+
*
4+
* wmain entry for sshd-session.
5+
*
6+
* Copyright (c) 2015 Microsoft Corp.
7+
* All rights reserved
8+
*
9+
* Microsoft openssh win32 port
10+
*
11+
* Redistribution and use in source and binary forms, with or without
12+
* modification, are permitted provided that the following conditions
13+
* are met:
14+
*
15+
* 1. Redistributions of source code must retain the above copyright
16+
* notice, this list of conditions and the following disclaimer.
17+
* 2. Redistributions in binary form must reproduce the above copyright
18+
* notice, this list of conditions and the following disclaimer in the
19+
* documentation and/or other materials provided with the distribution.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24+
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26+
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30+
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
33+
/* disable inclusion of compatability defitnitions in CRT headers */
34+
#define __STDC__ 1
35+
#include <Windows.h>
36+
#include <wchar.h>
37+
#include <Lm.h>
38+
#include <sddl.h>
39+
#include <stdio.h>
40+
41+
#include "inc\utf.h"
42+
#include "misc_internal.h"
43+
#include "Debug.h"
44+
45+
int main(int, char **);
46+
extern HANDLE main_thread;
47+
48+
int sshd_session_main(int argc, wchar_t **wargv) {
49+
char** argv = NULL;
50+
int i, r;
51+
_set_invalid_parameter_handler(invalid_parameter_handler);
52+
53+
if ((argv = malloc((argc + 1) * sizeof(char*))) == NULL)
54+
fatal("out of memory");
55+
56+
for (i = 0; i < argc; i++)
57+
if ((argv[i] = utf16_to_utf8(wargv[i])) == NULL)
58+
fatal("out of memory");
59+
argv[argc] = NULL;
60+
61+
w32posix_initialize();
62+
63+
r = main(argc, argv);
64+
w32posix_done();
65+
return r;
66+
}
67+
68+
int argc_original = 0;
69+
wchar_t **wargv_original = NULL;
70+
71+
int wmain(int argc, wchar_t **wargv) {
72+
wchar_t *path_value = NULL, *path_new_value;
73+
errno_t result = 0;
74+
size_t path_new_len = 0, len;
75+
argc_original = argc;
76+
wargv_original = wargv;
77+
78+
init_prog_paths();
79+
/* change current directory to sshd-session.exe root */
80+
_wchdir(__wprogdir);
81+
82+
/*
83+
* we want to launch scp and sftp executables from the binary directory
84+
* that sshd is hosted in. This will facilitate hosting and evaluating
85+
* multiple versions of OpenSSH at the same time.
86+
* it does not work well for powershell, cygwin, etc if program path is
87+
* prepended to executable directory.
88+
* To achive above, PATH is set to process environment
89+
*/
90+
_wdupenv_s(&path_value, &len, L"PATH");
91+
if (!path_value || (wcsstr(path_value, __wprogdir)) == NULL) {
92+
path_new_len = wcslen(__wprogdir) + wcslen(path_value) + 2;
93+
if ((path_new_value = (wchar_t *) malloc(path_new_len * sizeof(wchar_t))) == NULL) {
94+
errno = ENOMEM;
95+
error("failed to allocation memory");
96+
return -1;
97+
}
98+
swprintf_s(path_new_value, path_new_len, L"%s%s%s", __wprogdir, path_value ? L";" : L"", path_value);
99+
if (result = _wputenv_s(L"PATH", path_new_value)) {
100+
error("failed to set PATH environment variable: to value:%s, error:%d", path_new_value, result);
101+
errno = result;
102+
if (path_new_value)
103+
free(path_new_value);
104+
if(path_value)
105+
free(path_value);
106+
return -1;
107+
}
108+
if (path_new_value)
109+
free(path_new_value);
110+
if(path_value)
111+
free(path_value);
112+
}
113+
114+
return sshd_session_main(argc, wargv);
115+
}

sshd-auth.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,10 @@ main(int ac, char **av)
629629
pmonitor->m_recvfd = PRIVSEP_MONITOR_FD;
630630
pmonitor->m_log_sendfd = PRIVSEP_LOG_FD;
631631
set_log_handler(mm_log_handler, pmonitor);
632+
#ifdef WINDOWS
633+
fcntl(pmonitor->m_recvfd, F_SETFD, FD_CLOEXEC);
634+
fcntl(pmonitor->m_log_sendfd, F_SETFD, FD_CLOEXEC);
635+
#endif /* WINDOWS */
632636

633637
/* Check that there are no remaining arguments. */
634638
if (optind < ac) {

0 commit comments

Comments
 (0)