diff --git a/sshd/README.md b/sshd/README.md new file mode 100644 index 000000000..2af1e1ddb --- /dev/null +++ b/sshd/README.md @@ -0,0 +1,30 @@ +--- +title: OpenSSH (for Windows) +homepage: https://webinstall.dev/sshd +tagline: | + OpenSSH: Window's built-in SSH implementation for remote login +--- + +To update (replacing the current version) run `webi sudo`. + +## Cheat Sheet + +> Does the tedious work of installing, registering, and starting Windows' built-in OpenSSH Server (`sshd`) + +As this requires Administrator permissions, you must run the command yourself: + +```sh +sshd-service-install +``` + +### Files + +These are the files / directories that are created and/or modified with this +install: + +```text +~/.local/bin/sudo.bat +~/.local/bin/sshd-service-install.bat +``` + + diff --git a/sshd/install.ps1 b/sshd/install.ps1 new file mode 100644 index 000000000..fd96fae1c --- /dev/null +++ b/sshd/install.ps1 @@ -0,0 +1,52 @@ +#!/usr/bin/env pwsh + +$ErrorActionPreference = 'stop' + +function Repair-MissingCommand { + Param( + [string]$Name, + [string]$Package, + [string]$Command + ) + + Write-Host " Checking for $Name ..." + $HasCommand = Get-Command -Name $Command -ErrorAction Silent + IF ($HasCommand) { + Return + } + + & $HOME\.local\bin\webi-pwsh.ps1 $Package + $null = Sync-EnvPath +} + +function Install-WebiHostedScript () { + Param( + [string]$Package, + [string]$ScriptName + ) + $PwshName = "_${ScriptName}.ps1" + $PwshUrl = "${Env:WEBI_HOST}/packages/${Package}/${ScriptName}.ps1" + $PwshPath = "$HOME\.local\bin\${PwshName}" + $OldPath = "$HOME\.local\bin\${ScriptName}.ps1" + + $BatPath = "$HOME\.local\bin\${ScriptName}.bat" + $PwshExec = "powershell -ExecutionPolicy Bypass" + $Bat = "@echo off`r`n$PwshExec %USERPROFILE%\.local\bin\${PwshName} %*" + + Invoke-DownloadUrl -Force -URL $PwshUrl -Path $PwshPath + Set-Content -Path $BatPath -Value $Bat + Write-Host " Created alias ${BatPath}" + Write-Host " to run ${PwshPath}" + + # fix for old installs + Remove-Item -Path $OldPath -Force -ErrorAction Ignore +} + + +Repair-MissingCommand -Name "sudo (RunAs alias)" -Package "sudo" -Command "sudo" +Install-WebiHostedScript -Package "sshd" -ScriptName "sshd-service-install" + +Write-Output "" +Write-Output "${TTask}Copy, paste, and run${TReset} the following to install sshd as a system service" +Write-Output " ${TCmd}sshd-service-install${TReset}" +Write-Output "" diff --git a/sshd/install.sh b/sshd/install.sh new file mode 100644 index 000000000..7c4262cd2 --- /dev/null +++ b/sshd/install.sh @@ -0,0 +1,35 @@ +__install_sshd() { + my_os="$(uname -s)" + if test "Darwin" = "${my_os}"; then + echo >&2 "" + echo >&2 "Copy, paste, and run the following to enable the built-in sshd:" + echo >&2 " sudo systemsetup -f -setremotelogin on" + echo >&2 " sudo systemsetup -getremotelogin" + echo >&2 "" + exit 1 + fi + + echo >&2 "Install and enable sshd using your system package manager:" + my_cmd="" + if test command -v sudo > /dev/null; then + my_cmd="sudo " + fi + + if test command -v apt > /dev/null; then + echo " ${my_cmd}apt install -y openssh-server" + echo " ${my_cmd}systemctl enable ssh" + echo " ${my_cmd}systemctl start ssh" + elif test command -v yum > /dev/null; then + echo " ${my_cmd}yum -y install openssh-server" + echo " ${my_cmd}systemctl enable ssh" + echo " ${my_cmd}systemctl start ssh" + elif test command -v apk > /dev/null; then + echo " ${my_cmd}apk add --no-cache openssh" + echo " ${my_cmd}service sshd added to runlevel default" + echo " ${my_cmd}service sshd start" + else + echo " (unknown package manager / init daemon)" + fi + + exit 1 +} diff --git a/sshd/sshd-service-install.ps1 b/sshd/sshd-service-install.ps1 new file mode 100644 index 000000000..d2ceeadc6 --- /dev/null +++ b/sshd/sshd-service-install.ps1 @@ -0,0 +1,62 @@ +#!/usr/bin/env pwsh + +$Esc = [char]27 +$Warn = "${Esc}[1m[33m" +$ResetAll = "${Esc}[0m" + +# See +# - +# - +# - + +function InstallOpenSSHServer { + $OpenSSHServer = Get-WindowsCapability -Online | ` + Where-Object -Property Name -Like "OpenSSH.Server*" + IF (-Not ($OpenSSHServer.State -eq "Installed")) { + Add-WindowsCapability -Online -Name $sshd.Name + } + + $Sshd = Get-Service -Name "sshd" + IF (-Not ($Sshd.Status -eq "Running")) { + Start-Service "sshd" + } + IF (-Not ($Sshd.StartupType -eq "Automatic")) { + Set-Service -Name "sshd" -StartupType "Automatic" + } + + $SshAgent = Get-Service -Name "ssh-agent" + IF (-Not ($SshAgent.Status -eq "Running")) { + Start-Service "ssh-agent" + } + IF (-Not ($SshAgent.StartupType -eq "Automatic")) { + Set-Service -Name "ssh-agent" -StartupType "Automatic" + } + + Install-Module -Force OpenSSHUtils -Scope AllUsers +} + +function SelfElevate { + Write-Host "${Warn}Installing 'sshd' requires Admin privileges${ResetAll}" + Write-Host "Install will continue automatically in 5 seconds..." + Sleep 5.0 + + # Self-elevate the script if required + $CurUser = New-Object Security.Principal.WindowsPrincipal( + [Security.Principal.WindowsIdentity]::GetCurrent() + ) + $IsAdmin = $CurUser.IsInRole( + [Security.Principal.WindowsBuiltInRole]::Administrator + ) + if ($IsAdmin) { + Return 0 + } + + $CurLoc = Get-Location + $CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments + Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine + Set-Location $CurLoc + Exit 0 +} + +SelfElevate +InstallOpenSSHServer