Skip to content

Commit 6a86fdd

Browse files
committed
Source snapshot from Powershell/openssh-portable:latestw_all
1 parent 353a788 commit 6a86fdd

Some content is hidden

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

53 files changed

+2025
-1912
lines changed

appveyor.yml

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

44
branches:

auth.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
#include "authfile.h"
7777
#include "ssherr.h"
7878
#include "compat.h"
79+
#include "sshfileperm.h"
7980

8081
/* import */
8182
extern ServerOptions options;
@@ -489,10 +490,6 @@ int
489490
auth_secure_path(const char *name, struct stat *stp, const char *pw_dir,
490491
uid_t uid, char *err, size_t errlen)
491492
{
492-
#ifdef WINDOWS
493-
error("auth_secure_path should not be called in Windows yet");
494-
return -1;
495-
#else /* !WINDOWS */
496493
char buf[PATH_MAX], homedir[PATH_MAX];
497494
char *cp;
498495
int comparehome = 0;
@@ -545,7 +542,6 @@ auth_secure_path(const char *name, struct stat *stp, const char *pw_dir,
545542
break;
546543
}
547544
return 0;
548-
#endif /* !WINDOWS */
549545
}
550546

551547
/*
@@ -585,7 +581,12 @@ auth_openfile(const char *file, struct passwd *pw, int strict_modes,
585581
strerror(errno));
586582
return NULL;
587583
}
588-
/* TODO check permissions */
584+
if (strict_modes && check_secure_file_permission(file, pw) != 0) {
585+
fclose(f);
586+
logit("Authentication refused.");
587+
auth_debug_add("Ignored %s", file_type);
588+
return NULL;
589+
}
589590
#else /* !WINDOWS */
590591
if ((fd = open(file, O_RDONLY|O_NONBLOCK)) == -1) {
591592
if (log_missing || errno != ENOENT)

authfile.c

+18-3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include "sshbuf.h"
5050
#include "ssherr.h"
5151
#include "krl.h"
52+
#include "sshfileperm.h"
5253

5354
#define MAX_KEY_FILE_SIZE (1024 * 1024)
5455

@@ -60,6 +61,14 @@ sshkey_save_private_blob(struct sshbuf *keybuf, const char *filename)
6061

6162
if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0)
6263
return SSH_ERR_SYSTEM_ERROR;
64+
#ifdef WINDOWS /* WINDOWS */
65+
/*
66+
Set the owner of the private key file to current user and only grant
67+
current user the full control access
68+
*/
69+
if (set_secure_file_permission(filename, NULL) != 0)
70+
return SSH_ERR_SYSTEM_ERROR;
71+
#endif /* WINDOWS */
6372
if (atomicio(vwrite, fd, (u_char *)sshbuf_ptr(keybuf),
6473
sshbuf_len(keybuf)) != sshbuf_len(keybuf)) {
6574
oerrno = errno;
@@ -193,19 +202,25 @@ sshkey_perm_ok(int fd, const char *filename)
193202
#ifdef HAVE_CYGWIN
194203
if (check_ntsec(filename))
195204
#endif
196-
197-
#ifndef WINDOWS /*TODO - implement permission checks on Windows*/
205+
206+
#ifdef WINDOWS /*implement permission checks on Windows*/
207+
if(check_secure_file_permission(filename, NULL) != 0) {
208+
error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
209+
error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
210+
error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
211+
error("Permissions for '%s' are too open.", filename);
212+
#else
198213
if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
199214
error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
200215
error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
201216
error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
202217
error("Permissions 0%3.3o for '%s' are too open.",
203218
(u_int)st.st_mode & 0777, filename);
219+
#endif /* !WINDOWS */
204220
error("It is required that your private key files are NOT accessible by others.");
205221
error("This private key will be ignored.");
206222
return SSH_ERR_KEY_BAD_PERMISSIONS;
207223
}
208-
#endif /* !WINDOWS */
209224
return 0;
210225
}
211226

contrib/win32/openssh/AppveyorHelper.psm1

+2-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ function Publish-Artifact
173173
[System.Collections.ArrayList] $artifacts = [System.Collections.ArrayList]::new()
174174

175175
# Get the build.log file for each build configuration
176-
Add-BuildLog -artifacts $artifacts -buildLog (Get-BuildLogFile -root $repoRoot.FullName)
176+
Add-BuildLog -artifacts $artifacts -buildLog (Get-BuildLogFile -root $repoRoot.FullName -Configuration Release -NativeHostArch x64)
177+
Add-BuildLog -artifacts $artifacts -buildLog (Get-BuildLogFile -root $repoRoot.FullName -Configuration Debug -NativeHostArch x86)
177178

178179
if($Global:OpenSSHTestInfo)
179180
{

contrib/win32/openssh/OpenSSHBuildHelper.psm1

+18-8
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,12 @@ function Package-OpenSSH
307307
if ($NativeHostArch -eq 'x86') {
308308
$packageName = "OpenSSH-Win32"
309309
}
310+
while((($service = Get-Service ssh-agent -ErrorAction Ignore) -ne $null) -and ($service.Status -ine 'Stopped'))
311+
{
312+
Stop-Service ssh-agent -Force
313+
#sleep to wait the servicelog file write
314+
Start-Sleep 5
315+
}
310316

311317
$packageDir = Join-Path $buildDir $packageName
312318
Remove-Item $packageDir -Recurse -Force -ErrorAction SilentlyContinue
@@ -320,23 +326,23 @@ function Package-OpenSSH
320326
if ((-not(Test-Path (Join-Path $buildDir $file)))) {
321327
Throw "Cannot find $file under $buildDir. Did you run Build-OpenSSH?"
322328
}
323-
Copy-Item (Join-Path $buildDir $file) $packageDir
329+
Copy-Item (Join-Path $buildDir $file) $packageDir -Force
324330
if ($file.EndsWith(".exe")) {
325331
$pdb = $file.Replace(".exe", ".pdb")
326-
Copy-Item (Join-Path $buildDir $pdb) $symbolsDir
332+
Copy-Item (Join-Path $buildDir $pdb) $symbolsDir -Force
327333
}
328334
if ($file.EndsWith(".dll")) {
329335
$pdb = $file.Replace(".dll", ".pdb")
330-
Copy-Item (Join-Path $buildDir $pdb) $symbolsDir
336+
Copy-Item (Join-Path $buildDir $pdb) $symbolsDir -Force
331337
}
332338
}
333339

334340
if ($DestinationPath -ne "") {
335-
if (Test-Path $DestinationPath) {
336-
Remove-Item $DestinationPath\* -Force
341+
if (Test-Path $DestinationPath) {
342+
Remove-Item $DestinationPath\* -Force -Recurse
337343
}
338344
else {
339-
New-Item -ItemType Directory $DestinationPath | Out-Null
345+
New-Item -ItemType Directory $DestinationPath -Force | Out-Null
340346
}
341347
Copy-Item -Path $packageDir\* -Destination $DestinationPath -Force -Recurse
342348
}
@@ -493,8 +499,12 @@ function Install-OpenSSH
493499
Package-OpenSSH -NativeHostArch $NativeHostArch -Configuration $Configuration -DestinationPath $OpenSSHDir
494500

495501
Push-Location $OpenSSHDir
496-
& ( "$OpenSSHDir\install-sshd.ps1")
497-
.\ssh-keygen.exe -A
502+
& "$OpenSSHDir\install-sshd.ps1"
503+
& "$OpenSSHDir\ssh-keygen.exe" -A
504+
505+
$keyFiles = Get-ChildItem "$OpenSSHDir\ssh_host_*_key*" | % {
506+
Add-PermissionToFileACL -FilePath $_.FullName -User "NT Service\sshd" -Perm "Read"
507+
}
498508

499509

500510
#machine will be reboot after Install-openssh anyway

contrib/win32/openssh/OpenSSHCommonUtils.psm1

+77-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,80 @@ function Get-RepositoryRoot
2828
throw new-object System.IO.DirectoryNotFoundException("Could not find the root of the GIT repository")
2929
}
3030

31-
Export-ModuleMember -Function Get-RepositoryRoot
31+
<#
32+
.Synopsis
33+
Sets the Secure File ACL.
34+
1. Removed all user acl except Administrators group, system, and current user
35+
2. whether or not take the owner
36+
37+
.Outputs
38+
N/A
39+
40+
.Inputs
41+
FilePath - The path to the file
42+
takeowner - if want to take the ownership
43+
#>
44+
function Cleanup-SecureFileACL
45+
{
46+
[CmdletBinding()]
47+
param([string]$FilePath, [System.Security.Principal.NTAccount] $Owner)
48+
49+
$myACL = Get-ACL $filePath
50+
$myACL.SetAccessRuleProtection($True, $True)
51+
Set-Acl -Path $filePath -AclObject $myACL
52+
53+
$myACL = Get-ACL $filePath
54+
if($owner -ne $null)
55+
{
56+
$myACL.SetOwner($owner)
57+
}
58+
59+
if($myACL.Access)
60+
{
61+
$myACL.Access | % {
62+
if (($_ -ne $null) -and ($_.IdentityReference.Value -ine "BUILTIN\Administrators") -and
63+
($_.IdentityReference.Value -ine "NT AUTHORITY\SYSTEM") -and
64+
($_.IdentityReference.Value -ine "$(whoami)"))
65+
{
66+
if(-not ($myACL.RemoveAccessRule($_)))
67+
{
68+
throw "failed to remove access of $($_.IdentityReference.Value) rule in setup "
69+
}
70+
}
71+
}
72+
}
73+
74+
Set-Acl -Path $filePath -AclObject $myACL
75+
}
76+
77+
<#
78+
.Synopsis
79+
add a file permission to an account
80+
81+
.Outputs
82+
N/A
83+
84+
.Inputs
85+
FilePath - The path to the file
86+
User - account name
87+
Perm - The permission to grant.
88+
#>
89+
function Add-PermissionToFileACL
90+
{
91+
[CmdletBinding()]
92+
param(
93+
[string]$FilePath,
94+
[System.Security.Principal.NTAccount] $User,
95+
[System.Security.AccessControl.FileSystemRights]$Perm
96+
)
97+
98+
$myACL = Get-ACL $filePath
99+
100+
$objACE = New-Object System.Security.AccessControl.FileSystemAccessRule `
101+
($User, $perm, "None", "None", "Allow")
102+
$myACL.AddAccessRule($objACE)
103+
104+
Set-Acl -Path $filePath -AclObject $myACL
105+
}
106+
107+
Export-ModuleMember -Function Get-RepositoryRoot, Add-PermissionToFileACL, Cleanup-SecureFileACL

contrib/win32/openssh/OpenSSHTestHelper.psm1

+50-32
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ function Setup-OpenSSHTestEnvironment
108108
}
109109

110110
$Global:OpenSSHTestInfo.Add("OpenSSHBinPath", $script:OpenSSHBinPath)
111+
if (-not ($env:Path.ToLower().Contains($script:OpenSSHBinPath.ToLower())))
112+
{
113+
$env:Path = "$($script:OpenSSHBinPath);$($env:path)"
114+
}
111115

112116
$warning = @"
113117
WARNING: Following changes will be made to OpenSSH configuration
@@ -125,15 +129,12 @@ WARNING: Following changes will be made to OpenSSH configuration
125129
if (-not $Quiet) {
126130
Write-Warning $warning
127131
$continue = Read-Host -Prompt "Do you want to continue with the above changes? [Yes] Y; [No] N (default is `"Y`")"
128-
if( ($continue -eq "") -or ($continue -ieq "Y") -or ($continue -ieq "Yes") )
129-
{
130-
}
131-
elseif( ($continue -ieq "N") -or ($continue -ieq "No") )
132+
if( ($continue -ieq "N") -or ($continue -ieq "No") )
132133
{
133134
Write-Host "User decided not to make the changes."
134135
return
135136
}
136-
else
137+
elseif(($continue -ne "") -and ($continue -ine "Y") -and ($continue -ine "Yes"))
137138
{
138139
Throw "User entered invalid option ($continue). Exit now."
139140
}
@@ -152,9 +153,21 @@ WARNING: Following changes will be made to OpenSSH configuration
152153
Copy-Item (Join-Path $script:OpenSSHBinPath sshd_config) $backupConfigPath -Force
153154
}
154155

155-
# copy new sshd_config
156-
Copy-Item (Join-Path $Script:E2ETestDirectory sshd_config) (Join-Path $script:OpenSSHBinPath sshd_config) -Force
157-
Copy-Item "$($Script:E2ETestDirectory)\sshtest*hostkey*" $script:OpenSSHBinPath -Force
156+
# copy new sshd_config
157+
Copy-Item (Join-Path $Script:E2ETestDirectory sshd_config) (Join-Path $script:OpenSSHBinPath sshd_config) -Force
158+
159+
#workaround for the cariggage new line added by git before copy them
160+
Get-ChildItem "$($Script:E2ETestDirectory)\sshtest_*key*" | % {
161+
(Get-Content $_.FullName -Raw).Replace("`r`n","`n") | Set-Content $_.FullName -Force
162+
}
163+
164+
#copy sshtest keys
165+
Copy-Item "$($Script:E2ETestDirectory)\sshtest*hostkey*" $script:OpenSSHBinPath -Force
166+
$owner = New-Object System.Security.Principal.NTAccount($env:USERDOMAIN, $env:USERNAME)
167+
Get-ChildItem "$($script:OpenSSHBinPath)\sshtest*hostkey*" -Exclude *.pub | % {
168+
Cleanup-SecureFileACL -FilePath $_.FullName -Owner $owner
169+
Add-PermissionToFileACL -FilePath $_.FullName -User "NT Service\sshd" -Perm "Read"
170+
}
158171
Restart-Service sshd -Force
159172

160173
#Backup existing known_hosts and replace with test version
@@ -174,45 +187,50 @@ WARNING: Following changes will be made to OpenSSH configuration
174187
#TODO - this is Windows specific. Need to be in PAL
175188
foreach ($user in $OpenSSHTestAccounts)
176189
{
177-
try
190+
try
178191
{
179192
$objUser = New-Object System.Security.Principal.NTAccount($user)
180193
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
181194
}
182195
catch
183-
{
196+
{
184197
#only add the local user when it does not exists on the machine
185198
net user $user $Script:OpenSSHTestAccountsPassword /ADD 2>&1 >> $Script:TestSetupLogFile
186-
}
199+
}
187200
}
188201

189-
#setup single sign on for ssouser
190-
#TODO - this is Windows specific. Need to be in PAL
191-
$ssousersid = Get-UserSID -User sshtest_ssouser
192-
$ssouserProfileRegistry = Join-Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" $ssousersid
193-
if (-not (Test-Path $ssouserProfileRegistry) ) {
194-
#create profile
195-
if (-not($env:DISPLAY)) { $env:DISPLAY = 1 }
196-
$env:SSH_ASKPASS="$($env:ComSpec) /c echo $($OpenSSHTestAccountsPassword)"
197-
cmd /c "ssh -p 47002 sshtest_ssouser@localhost echo %userprofile% > profile.txt"
198-
if ($env:DISPLAY -eq 1) { Remove-Item env:\DISPLAY }
199-
remove-item "env:SSH_ASKPASS" -ErrorAction SilentlyContinue
200-
}
201-
$ssouserProfile = (Get-ItemProperty -Path $ssouserProfileRegistry -Name 'ProfileImagePath').ProfileImagePath
202+
#setup single sign on for ssouser
203+
$ssouserProfile = Get-LocalUserProfile -User $SSOUser
204+
$Global:OpenSSHTestInfo.Add("SSOUserProfile", $ssouserProfile)
205+
$Global:OpenSSHTestInfo.Add("PubKeyUserProfile", (Get-LocalUserProfile -User $PubKeyUser))
206+
202207
New-Item -ItemType Directory -Path (Join-Path $ssouserProfile .ssh) -Force -ErrorAction SilentlyContinue | out-null
203208
$authorizedKeyPath = Join-Path $ssouserProfile .ssh\authorized_keys
204-
$testPubKeyPath = Join-Path $Script:E2ETestDirectory sshtest_userssokey_ed25519.pub
205-
#workaround for the cariggage new line added by git
206-
(Get-Content $testPubKeyPath -Raw).Replace("`r`n","`n") | Set-Content $testPubKeyPath -Force
209+
$testPubKeyPath = Join-Path $Script:E2ETestDirectory sshtest_userssokey_ed25519.pub
207210
Copy-Item $testPubKeyPath $authorizedKeyPath -Force -ErrorAction SilentlyContinue
208-
$acl = get-acl $authorizedKeyPath
209-
$ar = New-Object System.Security.AccessControl.FileSystemAccessRule("NT Service\sshd", "Read", "Allow")
210-
$acl.SetAccessRule($ar)
211-
Set-Acl $authorizedKeyPath $acl
211+
Add-PermissionToFileACL -FilePath $authorizedKeyPath -User "NT Service\sshd" -Perm "Read"
212212
$testPriKeypath = Join-Path $Script:E2ETestDirectory sshtest_userssokey_ed25519
213-
(Get-Content $testPriKeypath -Raw).Replace("`r`n","`n") | Set-Content $testPriKeypath -Force
213+
Cleanup-SecureFileACL -FilePath $testPriKeypath -owner $owner
214214
cmd /c "ssh-add $testPriKeypath 2>&1 >> $Script:TestSetupLogFile"
215215
}
216+
#TODO - this is Windows specific. Need to be in PAL
217+
function Get-LocalUserProfile
218+
{
219+
param([string]$User)
220+
$sid = Get-UserSID -User $User
221+
$userProfileRegistry = Join-Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" $sid
222+
if (-not (Test-Path $userProfileRegistry) ) {
223+
#create profile
224+
if (-not($env:DISPLAY)) { $env:DISPLAY = 1 }
225+
$env:SSH_ASKPASS="$($env:ComSpec) /c echo $($OpenSSHTestAccountsPassword)"
226+
$ret = ssh -p 47002 "$User@localhost" echo %userprofile%
227+
if ($env:DISPLAY -eq 1) { Remove-Item env:\DISPLAY }
228+
remove-item "env:SSH_ASKPASS" -ErrorAction SilentlyContinue
229+
}
230+
231+
(Get-ItemProperty -Path $userProfileRegistry -Name 'ProfileImagePath').ProfileImagePath
232+
}
233+
216234

217235
<#
218236
.SYNOPSIS

0 commit comments

Comments
 (0)