Skip to content

Commit c4d0727

Browse files
committed
Source snapshot from Powershell/openssh-portable:latestw_all
1 parent 1ad6c95 commit c4d0727

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1945
-1070
lines changed

appveyor.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 0.0.24.0.{build}
1+
version: 1.0.0.0.{build}
22
image: Visual Studio 2015
33

44
branches:

auth-passwd.c

+70-17
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@
4141
#include <sys/types.h>
4242

4343
#include <pwd.h>
44-
#ifdef WINDOWS
45-
#include <logonuser.h>
46-
#endif
4744
#include <stdio.h>
4845
#include <string.h>
4946
#include <stdarg.h>
@@ -59,6 +56,11 @@
5956
#include "auth-options.h"
6057
#include "authfd.h"
6158

59+
#ifdef WINDOWS
60+
#include "logonuser.h"
61+
#include "monitor_wrap.h"
62+
#endif
63+
6264
extern Buffer loginmsg;
6365
extern ServerOptions options;
6466

@@ -228,10 +230,53 @@ sys_auth_passwd(Authctxt *authctxt, const char *password)
228230
}
229231

230232
#elif defined(WINDOWS)
233+
HANDLE password_auth_token = NULL;
234+
HANDLE process_custom_lsa_auth(char*, const char*, char*);
235+
236+
void
237+
sys_auth_passwd_lsa(Authctxt *authctxt, const char *password)
238+
{
239+
char *lsa_auth_pkg = NULL;
240+
wchar_t *lsa_auth_pkg_w = NULL;
241+
int domain_len = 0, lsa_auth_pkg_len = 0;
242+
HKEY reg_key = 0;
243+
REGSAM mask = STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_WOW64_64KEY;
244+
245+
if ((RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\OpenSSH", 0, mask, &reg_key) == ERROR_SUCCESS) &&
246+
(RegQueryValueExW(reg_key, L"LSAAuthenticationPackage", 0, NULL, NULL, &lsa_auth_pkg_len) == ERROR_SUCCESS)) {
247+
lsa_auth_pkg_w = (wchar_t *) malloc(lsa_auth_pkg_len); // lsa_auth_pkg_len includes the null terminating character.
248+
if (!lsa_auth_pkg_w)
249+
fatal("%s: out of memory", __func__);
250+
251+
memset(lsa_auth_pkg_w, 0, lsa_auth_pkg_len);
252+
if (RegQueryValueExW(reg_key, L"LSAAuthenticationPackage", 0, NULL, (LPBYTE)lsa_auth_pkg_w, &lsa_auth_pkg_len) == ERROR_SUCCESS) {
253+
lsa_auth_pkg = utf16_to_utf8(lsa_auth_pkg_w);
254+
if (!lsa_auth_pkg)
255+
fatal("utf16_to_utf8 failed to convert lsa_auth_pkg_w:%ls", lsa_auth_pkg_w);
256+
257+
debug("Authenticating using LSA Auth Package:%ls", lsa_auth_pkg_w);
258+
password_auth_token = process_custom_lsa_auth(authctxt->pw->pw_name, password, lsa_auth_pkg);
259+
}
260+
}
261+
262+
done:
263+
if (lsa_auth_pkg_w)
264+
free(lsa_auth_pkg_w);
265+
266+
if (lsa_auth_pkg)
267+
free(lsa_auth_pkg);
268+
269+
if (reg_key)
270+
RegCloseKey(reg_key);
271+
}
272+
231273
/*
232-
* Authenticate on Windows - Call LogonUser and retrieve user token
274+
* Authenticate on Windows
275+
* - Call LogonUser and retrieve user token
276+
* - If LogonUser fails, then try the LSA (Local Security Authority) authentication.
233277
*/
234-
int sys_auth_passwd(Authctxt *authctxt, const char *password)
278+
int
279+
sys_auth_passwd(Authctxt *authctxt, const char *password)
235280
{
236281
wchar_t *user_utf16 = NULL, *udom_utf16 = NULL, *pwd_utf16 = NULL, *tmp;
237282
HANDLE token = NULL;
@@ -249,25 +294,33 @@ int sys_auth_passwd(Authctxt *authctxt, const char *password)
249294
}
250295

251296
if (LogonUserExExWHelper(user_utf16, udom_utf16, pwd_utf16, LOGON32_LOGON_NETWORK_CLEARTEXT,
252-
LOGON32_PROVIDER_DEFAULT, NULL, &token, NULL, NULL, NULL, NULL) == FALSE) {
253-
if (GetLastError() == ERROR_PASSWORD_MUST_CHANGE)
254-
/*
255-
* TODO - need to add support to force password change
256-
* by sending back SSH_MSG_USERAUTH_PASSWD_CHANGEREQ
257-
*/
297+
LOGON32_PROVIDER_DEFAULT, NULL, &token, NULL, NULL, NULL, NULL) == TRUE)
298+
password_auth_token = token;
299+
else {
300+
if (GetLastError() == ERROR_PASSWORD_MUST_CHANGE)
301+
/*
302+
* TODO - need to add support to force password change
303+
* by sending back SSH_MSG_USERAUTH_PASSWD_CHANGEREQ
304+
*/
258305
error("password for user %s has expired", authctxt->pw->pw_name);
259-
else
260-
debug("failed to logon user: %ls domain: %ls error:%d", user_utf16, udom_utf16, GetLastError());
261-
goto done;
262-
}
306+
else {
307+
debug("Windows authentication failed for user: %ls domain: %ls error:%d", user_utf16, udom_utf16, GetLastError());
263308

264-
authctxt->auth_token = (void*)(INT_PTR)token;
265-
r = 1;
309+
/* If LSA authentication package is configured then it will return the auth_token */
310+
sys_auth_passwd_lsa(authctxt, password);
311+
}
312+
}
313+
266314
done:
315+
if (password_auth_token)
316+
r = 1;
317+
267318
if (user_utf16)
268319
free(user_utf16);
320+
269321
if (pwd_utf16)
270322
SecureZeroMemory(pwd_utf16, sizeof(wchar_t) * wcslen(pwd_utf16));
323+
271324
return r;
272325
}
273326
#endif /* WINDOWS */

auth.h

-3
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,6 @@ struct Authctxt {
9393

9494
/* Information exposed to session */
9595
struct sshbuf *session_info; /* Auth info for environment */
96-
#ifdef WINDOWS
97-
void *auth_token;
98-
#endif
9996
};
10097

10198
/*

auth2-pubkey.c

-5
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,8 @@ userauth_pubkey(struct ssh *ssh)
199199
authenticated = 0;
200200

201201
if (PRIVSEP(user_key_allowed(authctxt->pw, key, 1)) &&
202-
#ifdef WINDOWS
203-
(authctxt->auth_token = mm_auth_pubkey(authctxt->pw->pw_name,
204-
key, sig, slen, b)) != NULL) {
205-
#else
206202
PRIVSEP(sshkey_verify(key, sig, slen, sshbuf_ptr(b),
207203
sshbuf_len(b), ssh->compat)) == 0) {
208-
#endif
209204
authenticated = 1;
210205
}
211206
sshbuf_free(b);

authfd.c

-5
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,7 @@ ssh_get_authentication_socket(int *fdp)
120120
}
121121

122122
/* Communicate with agent: send request and read reply */
123-
#ifdef WINDOWS
124-
/* for Windows we need to access this function from other places to talk to agent*/
125-
int
126-
#else /* !WINDOWS */
127123
static int
128-
#endif /* !WINDOWS */
129124
ssh_request_reply(int sock, struct sshbuf *request, struct sshbuf *reply)
130125
{
131126
int r;

contrib/win32/openssh/FixHostFilePermissions.ps1

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ If ($PSVersiontable.PSVersion.Major -le 2) {$PSScriptRoot = Split-Path -Parent $
55
Import-Module $PSScriptRoot\OpenSSHUtils -Force
66

77
#check sshd config file
8-
$sshdConfigPath = join-path $PSScriptRoot "sshd_config"
8+
$sshdConfigPath = join-path $env:ProgramData\ssh "sshd_config"
99
if(Test-Path $sshdConfigPath -PathType Leaf)
1010
{
1111
Repair-SshdConfigPermission -FilePath $sshdConfigPath @psBoundParameters
1212
}
1313
else
1414
{
15-
Write-host "$FilePath does not exist" -ForegroundColor Yellow
15+
Write-host "$sshdConfigPath does not exist" -ForegroundColor Yellow
1616
}
1717

1818
#check host keys
@@ -36,7 +36,7 @@ If you choose not to register the keys with ssh-agent, please grant sshd read ac
3636
Write-Host " "
3737
}#>
3838

39-
Get-ChildItem $PSScriptRoot\ssh_host_*_key -ErrorAction SilentlyContinue | % {
39+
Get-ChildItem $env:ProgramData\ssh\ssh_host_*_key -ErrorAction SilentlyContinue | % {
4040
Repair-SshdHostKeyPermission -FilePath $_.FullName @psBoundParameters
4141
}
4242

contrib/win32/openssh/OpenSSHBuildHelper.psm1

+2-2
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ function Start-OpenSSHPackage
344344
$buildDir = Join-Path $repositoryRoot ("bin\" + $folderName + "\" + $Configuration)
345345
$payload = "sshd.exe", "ssh.exe", "ssh-agent.exe", "ssh-add.exe", "sftp.exe"
346346
$payload += "sftp-server.exe", "scp.exe", "ssh-shellhost.exe", "ssh-keygen.exe", "ssh-keyscan.exe"
347-
$payload += "sshd_config", "install-sshd.ps1", "uninstall-sshd.ps1"
348-
$payload +="FixHostFilePermissions.ps1", "FixUserFilePermissions.ps1", "OpenSSHUtils.psm1", "OpenSSHUtils.psd1", "ssh-add-hostkey.ps1"
347+
$payload += "sshd_config_default", "install-sshd.ps1", "uninstall-sshd.ps1"
348+
$payload +="FixHostFilePermissions.ps1", "FixUserFilePermissions.ps1", "OpenSSHUtils.psm1", "OpenSSHUtils.psd1"
349349

350350
$packageName = "OpenSSH-Win64"
351351
if ($NativeHostArch -ieq 'x86') {

contrib/win32/openssh/OpenSSHTestHelper.psm1

+14-29
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ $PubKeyUser = "sshtest_pubkeyuser"
1313
$PasswdUser = "sshtest_passwduser"
1414
$OpenSSHTestAccountsPassword = "P@ssw0rd_1"
1515
$OpenSSHTestAccounts = $Script:SSOUser, $Script:PubKeyUser, $Script:PasswdUser
16+
$OpenSSHConfigPath = Join-Path $env:ProgramData "ssh"
1617

1718
$Script:TestDataPath = "$env:SystemDrive\OpenSSHTests"
1819
$Script:E2ETestResultsFile = Join-Path $TestDataPath $E2ETestResultsFileName
@@ -106,7 +107,7 @@ function Set-OpenSSHTestEnvironment
106107
}
107108
}
108109
else
109-
{
110+
{
110111
if (-not (Test-Path (Join-Path $OpenSSHBinPath ssh.exe) -PathType Leaf))
111112
{
112113
Throw "Cannot find OpenSSH binaries under $OpenSSHBinPath. Please specify -OpenSSHBinPath to the OpenSSH installed location"
@@ -162,42 +163,31 @@ WARNING: Following changes will be made to OpenSSH configuration
162163
}
163164

164165
#Backup existing OpenSSH configuration
165-
$backupConfigPath = Join-Path $script:OpenSSHBinPath sshd_config.ori
166+
$backupConfigPath = Join-Path $OpenSSHConfigPath sshd_config.ori
166167
if (-not (Test-Path $backupConfigPath -PathType Leaf)) {
167-
Copy-Item (Join-Path $script:OpenSSHBinPath sshd_config) $backupConfigPath -Force
168+
Copy-Item (Join-Path $OpenSSHConfigPath sshd_config) $backupConfigPath -Force
168169
}
169-
$targetsshdConfig = Join-Path $script:OpenSSHBinPath sshd_config
170+
$targetsshdConfig = Join-Path $OpenSSHConfigPath sshd_config
170171
# copy new sshd_config
171-
if($Script:WindowsInBox -and (Test-Path $targetsshdConfig))
172-
{
173-
$currentUser = New-Object System.Security.Principal.NTAccount($($env:USERDOMAIN), $($env:USERNAME))
174-
Add-PermissionToFileACL -FilePath $targetsshdConfig -User $currentUser -Perm "Read,Write"
175-
}
176-
177172
Copy-Item (Join-Path $Script:E2ETestDirectory sshd_config) $targetsshdConfig -Force
178173

179174
Start-Service ssh-agent
180175

181176
#copy sshtest keys
182-
Copy-Item "$($Script:E2ETestDirectory)\sshtest*hostkey*" $script:OpenSSHBinPath -Force
183-
Get-ChildItem "$($script:OpenSSHBinPath)\sshtest*hostkey*"| % {
177+
Copy-Item "$($Script:E2ETestDirectory)\sshtest*hostkey*" $OpenSSHConfigPath -Force
178+
Get-ChildItem "$($OpenSSHConfigPath)\sshtest*hostkey*"| % {
184179
#workaround for the cariggage new line added by git before copy them
185180
$filePath = "$($_.FullName)"
186181
$con = (Get-Content $filePath | Out-String).Replace("`r`n","`n")
187182
Set-Content -Path $filePath -Value "$con"
188183
if (-not ($_.Name.EndsWith(".pub")))
189184
{
190185
Repair-SshdHostKeyPermission -FilePath $_.FullName -confirm:$false
191-
if($psversiontable.BuildVersion.Major -gt 6)
192-
{
193-
#register private key with agent
194-
ssh-add-hostkey.ps1 $_.FullName
195-
}
196186
}
197187
}
198188

199-
#copy ca pubkey to SSHD bin path
200-
Copy-Item "$($Script:E2ETestDirectory)\sshtest_ca_userkeys.pub" $script:OpenSSHBinPath -Force
189+
#copy ca pubkey to ssh config path
190+
Copy-Item "$($Script:E2ETestDirectory)\sshtest_ca_userkeys.pub" $OpenSSHConfigPath -Force
201191

202192
#copy ca private key to test dir
203193
$ca_priv_key = (Join-Path $Global:OpenSSHTestInfo["TestDataPath"] sshtest_ca_userkeys)
@@ -462,11 +452,6 @@ function Clear-OpenSSHTestEnvironment
462452
Throw "Cannot find OpenSSH binaries under $script:OpenSSHBinPath. "
463453
}
464454

465-
#unregister test host keys from agent
466-
Get-ChildItem "$sshBinPath\sshtest*hostkey*.pub"| % {
467-
ssh-add-hostkey.ps1 -Delete_key $_.FullName
468-
}
469-
470455
if($Global:OpenSSHTestInfo["EnableAppVerifier"] -and (Test-path $env:windir\System32\appverif.exe))
471456
{
472457
# clear all applications in application verifier
@@ -479,14 +464,14 @@ function Clear-OpenSSHTestEnvironment
479464
Remove-ItemProperty "HKLM:Software\Microsoft\Windows NT\CurrentVersion\AeDebug" -Name Auto -ErrorAction SilentlyContinue -Force | Out-Null
480465
}
481466

482-
Remove-Item "$sshBinPath\sshtest*hostkey*" -Force -ErrorAction SilentlyContinue
483-
Remove-Item "$sshBinPath\sshtest*ca_userkeys*" -Force -ErrorAction SilentlyContinue
467+
Remove-Item "$OpenSSHConfigPath\sshtest*hostkey*" -Force -ErrorAction SilentlyContinue
468+
Remove-Item "$OpenSSHConfigPath\sshtest*ca_userkeys*" -Force -ErrorAction SilentlyContinue
484469

485470
#Restore sshd_config
486-
$backupConfigPath = Join-Path $sshBinPath sshd_config.ori
471+
$backupConfigPath = Join-Path $OpenSSHConfigPath sshd_config.ori
487472
if (Test-Path $backupConfigPath -PathType Leaf) {
488-
Copy-Item $backupConfigPath (Join-Path $sshBinPath sshd_config) -Force -ErrorAction SilentlyContinue
489-
Remove-Item (Join-Path $sshBinPath sshd_config.ori) -Force -ErrorAction SilentlyContinue
473+
Copy-Item $backupConfigPath (Join-Path $OpenSSHConfigPath sshd_config) -Force -ErrorAction SilentlyContinue
474+
Remove-Item (Join-Path $OpenSSHConfigPath sshd_config.ori) -Force -ErrorAction SilentlyContinue
490475
Restart-Service sshd
491476
}
492477

contrib/win32/openssh/OpenSSHUtils.psm1

+4-6
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ $adminsSid = Get-UserSID -WellKnownSidType ([System.Security.Principal.WellKnown
4141
# get the everyone
4242
$everyoneSid = Get-UserSID -WellKnownSidType ([System.Security.Principal.WellKnownSidType]::WorldSid)
4343

44-
$sshdSid = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-80-3847866527-469524349-687026318-516638107-1125189541")
45-
4644
$currentUserSid = Get-UserSID -User "$($env:USERDOMAIN)\$($env:USERNAME)"
4745

4846
#Taken from P/Invoke.NET with minor adjustments.
@@ -112,7 +110,7 @@ function Repair-SshdConfigPermission
112110
[ValidateNotNullOrEmpty()]
113111
[string]$FilePath)
114112

115-
Repair-FilePermission -Owners $systemSid,$adminsSid -FullAccessNeeded $systemSid -ReadAccessNeeded $sshdSid @psBoundParameters
113+
Repair-FilePermission -Owners $systemSid,$adminsSid -FullAccessNeeded $systemSid @psBoundParameters
116114
}
117115

118116
<#
@@ -134,10 +132,10 @@ function Repair-SshdHostKeyPermission
134132
$PSBoundParameters["FilePath"] = $PSBoundParameters["FilePath"].Replace(".pub", "")
135133
}
136134

137-
Repair-FilePermission -Owners $systemSid,$adminsSid -ReadAccessNeeded $sshdSid @psBoundParameters
135+
Repair-FilePermission -Owners $systemSid,$adminsSid @psBoundParameters
138136

139137
$PSBoundParameters["FilePath"] += ".pub"
140-
Repair-FilePermission -Owners $systemSid,$adminsSid -ReadAccessOK $everyoneSid -ReadAccessNeeded $sshdSid @psBoundParameters
138+
Repair-FilePermission -Owners $systemSid,$adminsSid -ReadAccessOK $everyoneSid @psBoundParameters
141139
}
142140

143141
<#
@@ -175,7 +173,7 @@ function Repair-AuthorizedKeyPermission
175173
if($profileItem)
176174
{
177175
$userSid = $profileItem.PSChildName
178-
Repair-FilePermission -Owners $userSid,$adminsSid,$systemSid -AnyAccessOK $userSid -FullAccessNeeded $systemSid -ReadAccessNeeded $sshdSid @psBoundParameters
176+
Repair-FilePermission -Owners $userSid,$adminsSid,$systemSid -AnyAccessOK $userSid -FullAccessNeeded $systemSid @psBoundParameters
179177

180178
}
181179
else

contrib/win32/openssh/config.h.vs

+4-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102

103103
/* Define if your platform needs to skip post auth
104104
file descriptor passing */
105-
#define DISABLE_FD_PASSING 1
105+
/* #undef DISABLE_FD_PASSING */
106106

107107
/* Define if you don't want to use lastlog */
108108
/* #undef DISABLE_LASTLOG */
@@ -1691,7 +1691,9 @@
16911691

16921692
#define HAVE_MBLEN 1
16931693

1694-
#define SSHDIR "."
1694+
#define _PATH_PRIVSEP_CHROOT_DIR "."
1695+
#define SSHDIR "__PROGRAMDATA__\\ssh"
16951696
#define _PATH_SFTP_SERVER "sftp-server.exe"
16961697
#define _PATH_SSH_PROGRAM "ssh.exe"
16971698
#define _PATH_LS "dir"
1699+
#define FORK_NOT_SUPPORTED 1

0 commit comments

Comments
 (0)