openssh.ps1 4.16 KB
Newer Older
dylanmei's avatar
dylanmei committed
1 2 3
param (
  [switch]$AutoStart = $false
)
4

5
Write-Output "AutoStart: $AutoStart"
dylanmei's avatar
dylanmei committed
6 7 8
$is_64bit = [IntPtr]::size -eq 8

# setup openssh
Stefan Scherer's avatar
Stefan Scherer committed
9
$ssh_download_url = "http://www.mls-software.com/files/setupssh-7.1p1-1.exe"
dylanmei's avatar
dylanmei committed
10 11

if (!(Test-Path "C:\Program Files\OpenSSH\bin\ssh.exe")) {
12
    Write-Output "Downloading $ssh_download_url"
13
    (New-Object System.Net.WebClient).DownloadFile($ssh_download_url, "C:\Windows\Temp\openssh.exe")
14 15 16 17

    # initially set the port to 2222 so that there is not a race
    # condition in which packer connects to SSH before we can disable the service
    Start-Process "C:\Windows\Temp\openssh.exe" "/S /port=2222 /privsep=1 /password=D@rj33l1ng" -NoNewWindow -Wait
dylanmei's avatar
dylanmei committed
18 19 20 21 22
}

Stop-Service "OpenSSHd" -Force

# ensure vagrant can log in
23
Write-Output "Setting vagrant user file permissions"
dylanmei's avatar
dylanmei committed
24 25 26 27 28
New-Item -ItemType Directory -Force -Path "C:\Users\vagrant\.ssh"
C:\Windows\System32\icacls.exe "C:\Users\vagrant" /grant "vagrant:(OI)(CI)F"
C:\Windows\System32\icacls.exe "C:\Program Files\OpenSSH\bin" /grant "vagrant:(OI)RX"
C:\Windows\System32\icacls.exe "C:\Program Files\OpenSSH\usr\sbin" /grant "vagrant:(OI)RX"

29
Write-Output "Setting SSH home directories"
30 31
    (Get-Content "C:\Program Files\OpenSSH\etc\passwd") |
    Foreach-Object { $_ -replace '/home/(\w+)', '/cygdrive/c/Users/$1' } |
dylanmei's avatar
dylanmei committed
32 33
    Set-Content 'C:\Program Files\OpenSSH\etc\passwd'

34
# disabled for vcloud to make vagrant-serverspec work
35
# Set shell to /bin/sh to return exit status
36 37 38
# $passwd_file = Get-Content 'C:\Program Files\OpenSSH\etc\passwd'
# $passwd_file = $passwd_file -replace '/bin/bash', '/bin/sh'
# Set-Content 'C:\Program Files\OpenSSH\etc\passwd' $passwd_file
39

dylanmei's avatar
dylanmei committed
40
# fix opensshd to not be strict
41
Write-Output "Setting OpenSSH to be non-strict"
dylanmei's avatar
dylanmei committed
42 43 44 45
$sshd_config = Get-Content "C:\Program Files\OpenSSH\etc\sshd_config"
$sshd_config = $sshd_config -replace 'StrictModes yes', 'StrictModes no'
$sshd_config = $sshd_config -replace '#PubkeyAuthentication yes', 'PubkeyAuthentication yes'
$sshd_config = $sshd_config -replace '#PermitUserEnvironment no', 'PermitUserEnvironment yes'
46 47
# disable the use of DNS to speed up the time it takes to establish a connection
$sshd_config = $sshd_config -replace '#UseDNS yes', 'UseDNS no'
48 49
# disable the login banner
$sshd_config = $sshd_config -replace 'Banner /etc/banner.txt', '#Banner /etc/banner.txt'
50 51
# next time OpenSSH starts have it listen on th eproper port
$sshd_config = $sshd_config -replace 'Port 2222', "Port 22"
dylanmei's avatar
dylanmei committed
52 53
Set-Content "C:\Program Files\OpenSSH\etc\sshd_config" $sshd_config

Stefan Scherer's avatar
Stefan Scherer committed
54
Write-Output "Removing ed25519 key as Vagrant net-ssh 2.9.1 does not support it"
55 56 57
Remove-Item -Force -ErrorAction SilentlyContinue "C:\Program Files\OpenSSH\etc\ssh_host_ed25519_key"
Remove-Item -Force -ErrorAction SilentlyContinue "C:\Program Files\OpenSSH\etc\ssh_host_ed25519_key.pub"

dylanmei's avatar
dylanmei committed
58
# use c:\Windows\Temp as /tmp location
59
Write-Output "Setting temp directory location"
dylanmei's avatar
dylanmei committed
60 61 62 63 64
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue "C:\Program Files\OpenSSH\tmp"
C:\Program` Files\OpenSSH\bin\junction.exe /accepteula "C:\Program Files\OpenSSH\tmp" "C:\Windows\Temp"
C:\Windows\System32\icacls.exe "C:\Windows\Temp" /grant "vagrant:(OI)(CI)F"

# add 64 bit environment variables missing from SSH
65
Write-Output "Setting SSH environment"
dylanmei's avatar
dylanmei committed
66 67
$sshenv = "TEMP=C:\Windows\Temp"
if ($is_64bit) {
68 69 70 71 72
    $env_vars = "ProgramFiles(x86)=C:\Program Files (x86)", `
        "ProgramW6432=C:\Program Files", `
        "CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files", `
        "CommonProgramW6432=C:\Program Files\Common Files"
    $sshenv = $sshenv + "`r`n" + ($env_vars -join "`r`n")
dylanmei's avatar
dylanmei committed
73 74 75 76
}
Set-Content C:\Users\vagrant\.ssh\environment $sshenv

# record the path for provisioners (without the newline)
77
Write-Output "Recording PATH for provisioners"
dylanmei's avatar
dylanmei committed
78 79 80
Set-Content C:\Windows\Temp\PATH ([byte[]][char[]] $env:PATH) -Encoding Byte

# configure firewall
81
Write-Output "Configuring firewall"
dylanmei's avatar
dylanmei committed
82 83 84 85 86
netsh advfirewall firewall add rule name="SSHD" dir=in action=allow service=OpenSSHd enable=yes
netsh advfirewall firewall add rule name="SSHD" dir=in action=allow program="C:\Program Files\OpenSSH\usr\sbin\sshd.exe" enable=yes
netsh advfirewall firewall add rule name="ssh" dir=in action=allow protocol=TCP localport=22

if ($AutoStart -eq $true) {
87
    Start-Service "OpenSSHd"
88
}