From 1f03b276fd254292f07361f4a1c59b070ca56b19 Mon Sep 17 00:00:00 2001
From: Tess Gauthier <tgauth@bu.edu>
Date: Thu, 3 Nov 2022 11:57:30 -0400
Subject: [PATCH] fix installers not updating system path (#622)

* fix msi installer not updating system path

* modify install script to add binary path to system path

* debug appveyor

* add debug message to add-path and update appveyor to be compatible with changes

* fix appveyorhelper.psm1 after accidentally removing call to uninstall

* fix typo

* use PS drive to modify PATH

* rename Add-Path to Add-MachinePath in export list
---
 contrib/win32/install/product.wxs         |  3 +++
 contrib/win32/openssh/AppveyorHelper.psm1 |  5 ++++-
 contrib/win32/openssh/OpenSSHUtils.psm1   | 23 ++++++++++++++++++++++-
 contrib/win32/openssh/install-sshd.ps1    |  4 ++++
 4 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/contrib/win32/install/product.wxs b/contrib/win32/install/product.wxs
index 96920cf96..30c07a8ab 100644
--- a/contrib/win32/install/product.wxs
+++ b/contrib/win32/install/product.wxs
@@ -23,6 +23,9 @@
         <MajorUpgrade Schedule="afterInstallInitialize" DowngradeErrorMessage="A newer version of !(bind.property.ProductName) is already installed." />
         <Condition Message="OpenSSH is supported only on Windows 7 and newer."><![CDATA[VersionNT >= 601]]></Condition>
 
+        <!-- assume user wants path to be updated when client binaries are installed, can be overridden during install with ADD_PATH=0 -->
+        <Property Id="ADD_PATH" Value="1" />
+
         <Feature Id="Client" AllowAdvertise="no">
             <ComponentGroupRef Id="Client" />
         </Feature>
diff --git a/contrib/win32/openssh/AppveyorHelper.psm1 b/contrib/win32/openssh/AppveyorHelper.psm1
index 1f897fcaf..c382ff9f0 100644
--- a/contrib/win32/openssh/AppveyorHelper.psm1
+++ b/contrib/win32/openssh/AppveyorHelper.psm1
@@ -171,9 +171,12 @@ function Install-OpenSSH
 
     $machinePath = [Environment]::GetEnvironmentVariable('Path', 'MACHINE')
     $newMachineEnvironmentPath = $machinePath
-    if (-not ($machinePath.ToLower().Contains($OpenSSHDir.ToLower())))
+    if (-not $machinePath.ToLower().Contains("$OpenSSHDir;".ToLower()))
     {
         $newMachineEnvironmentPath = "$OpenSSHDir;$newMachineEnvironmentPath"
+    }
+    if (-not $env:Path.ToLower().Contains("$OpenSSHDir;".ToLower()))
+    {
         $env:Path = "$OpenSSHDir;$env:Path"
     }
     # Update machine environment path
diff --git a/contrib/win32/openssh/OpenSSHUtils.psm1 b/contrib/win32/openssh/OpenSSHUtils.psm1
index cace31ecb..a6b6c46a0 100644
--- a/contrib/win32/openssh/OpenSSHUtils.psm1
+++ b/contrib/win32/openssh/OpenSSHUtils.psm1
@@ -829,4 +829,25 @@ function Enable-Privilege {
     $type[0]::EnablePrivilege($Privilege, $Disable)
 }
 
-Export-ModuleMember -Function Repair-FilePermission, Repair-SshdConfigPermission, Repair-SshdHostKeyPermission, Repair-AuthorizedKeyPermission, Repair-UserKeyPermission, Repair-UserSshConfigPermission, Enable-Privilege, Get-UserAccount, Get-UserSID, Repair-AdministratorsAuthorizedKeysPermission, Repair-ModuliFilePermission, Repair-SSHFolderPermission, Repair-SSHFolderFilePermission, Repair-SSHFolderPrivateKeyPermission
+Function Add-MachinePath {
+    [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="High")]
+    param
+    (
+        [parameter(Mandatory=$true)]
+        [string]$FilePath
+    )
+
+    if (Test-Path $FilePath) {
+        $machinePath = (Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path
+        if (-not $machinePath.ToLower().Contains("$FilePath;".ToLower()))
+        {
+            $newPath = $FilePath + ’;’ + $machinePath 
+            Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH –Value $newPath
+            if ((Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path -eq $newPath) {
+                Write-Host "Updated Machine PATH to include OpenSSH directory, restart/re-login required to take effect globally" -ForegroundColor Yellow
+            }
+        }
+    }
+}
+
+Export-ModuleMember -Function Repair-FilePermission, Repair-SshdConfigPermission, Repair-SshdHostKeyPermission, Repair-AuthorizedKeyPermission, Repair-UserKeyPermission, Repair-UserSshConfigPermission, Enable-Privilege, Get-UserAccount, Get-UserSID, Repair-AdministratorsAuthorizedKeysPermission, Repair-ModuliFilePermission, Repair-SSHFolderPermission, Repair-SSHFolderFilePermission, Repair-SSHFolderPrivateKeyPermission, Add-MachinePath
diff --git a/contrib/win32/openssh/install-sshd.ps1 b/contrib/win32/openssh/install-sshd.ps1
index e4343bcb9..46246b845 100644
--- a/contrib/win32/openssh/install-sshd.ps1
+++ b/contrib/win32/openssh/install-sshd.ps1
@@ -3,6 +3,7 @@
 # @manojampalam - removed ntrights.exe dependency
 # @bingbing8 - removed secedit.exe dependency
 # @tessgauthier - added permissions check for %programData%/ssh
+# @tessgauthier - added update to system path for scp/sftp discoverability
 
 [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="High")]
 param ()
@@ -126,3 +127,6 @@ New-Service -Name sshd -DisplayName "OpenSSH SSH Server" -BinaryPathName `"$sshd
 sc.exe privs sshd SeAssignPrimaryTokenPrivilege/SeTcbPrivilege/SeBackupPrivilege/SeRestorePrivilege/SeImpersonatePrivilege
 
 Write-Host -ForegroundColor Green "sshd and ssh-agent services successfully installed"
+
+# add folder to system PATH
+Add-MachinePath -FilePath $scriptdir @psBoundParameters