|
| 1 | +### Setup SSH and Rsync |
| 2 | +# |
| 3 | +# The script has been adapted from |
| 4 | +# |
| 5 | +# <https://github.com/joefitzgerald/packer-windows> |
| 6 | + |
| 7 | +# This section is based on openssh.ps1 in https://github.com/joefitzgerald/packer-windows |
| 8 | + |
| 9 | +$is_64bit = [IntPtr]::size -eq 8 |
| 10 | + |
| 11 | +# setup openssh |
| 12 | +$ssh_download_url = "http://www.mls-software.com/files/setupssh-6.6p1-1.exe" |
| 13 | +if ($is_64bit) { |
| 14 | + Write-Host "64 bit OS found" |
| 15 | + $ssh_download_url = "http://www.mls-software.com/files/setupssh-6.6p1-1(x64).exe" |
| 16 | +} |
| 17 | + |
| 18 | +if (!(Test-Path "C:\Program Files\OpenSSH\bin\ssh.exe")) { |
| 19 | + Write-Host "Downloading $ssh_download_url" |
| 20 | + (New-Object System.Net.WebClient).DownloadFile($ssh_download_url, "C:\Windows\Temp\openssh.exe") |
| 21 | + Start-Process "C:\Windows\Temp\openssh.exe" "/S /port=22 /privsep=1 /password=D@rj33l1ng" -NoNewWindow -Wait |
| 22 | +} |
| 23 | + |
| 24 | +Stop-Service "OpenSSHd" -Force |
| 25 | + |
| 26 | +# ensure vagrant can log in |
| 27 | +Write-Host "Setting vagrant user file permissions" |
| 28 | +New-Item -ItemType Directory -Force -Path "C:\Users\vagrant\.ssh" |
| 29 | +C:\Windows\System32\icacls.exe "C:\Users\vagrant" /grant "vagrant:(OI)(CI)F" |
| 30 | +C:\Windows\System32\icacls.exe "C:\Program Files\OpenSSH\bin" /grant "vagrant:(OI)RX" |
| 31 | +C:\Windows\System32\icacls.exe "C:\Program Files\OpenSSH\usr\sbin" /grant "vagrant:(OI)RX" |
| 32 | + |
| 33 | +Write-Host "Setting SSH home directories" |
| 34 | + (Get-Content "C:\Program Files\OpenSSH\etc\passwd") | |
| 35 | + Foreach-Object { $_ -replace '/home/(\w+)', '/cygdrive/c/Users/$1' } | |
| 36 | + Set-Content 'C:\Program Files\OpenSSH\etc\passwd' |
| 37 | + |
| 38 | +# Set shell to /bin/sh to return exit status |
| 39 | +$passwd_file = Get-Content 'C:\Program Files\OpenSSH\etc\passwd' |
| 40 | +$passwd_file = $passwd_file -replace '/bin/bash', '/bin/sh' |
| 41 | +Set-Content 'C:\Program Files\OpenSSH\etc\passwd' $passwd_file |
| 42 | + |
| 43 | +# fix opensshd to not be strict |
| 44 | +Write-Host "Setting OpenSSH to be non-strict" |
| 45 | +$sshd_config = Get-Content "C:\Program Files\OpenSSH\etc\sshd_config" |
| 46 | +$sshd_config = $sshd_config -replace 'StrictModes yes', 'StrictModes no' |
| 47 | +$sshd_config = $sshd_config -replace '#PubkeyAuthentication yes', 'PubkeyAuthentication yes' |
| 48 | +$sshd_config = $sshd_config -replace '#PermitUserEnvironment no', 'PermitUserEnvironment yes' |
| 49 | +# disable the use of DNS to speed up the time it takes to establish a connection |
| 50 | +$sshd_config = $sshd_config -replace '#UseDNS yes', 'UseDNS no' |
| 51 | +# disable the login banner |
| 52 | +$sshd_config = $sshd_config -replace 'Banner /etc/banner.txt', '#Banner /etc/banner.txt' |
| 53 | +Set-Content "C:\Program Files\OpenSSH\etc\sshd_config" $sshd_config |
| 54 | + |
| 55 | +# use c:\Windows\Temp as /tmp location |
| 56 | +Write-Host "Setting temp directory location" |
| 57 | +Remove-Item -Recurse -Force -ErrorAction SilentlyContinue "C:\Program Files\OpenSSH\tmp" |
| 58 | +C:\Program` Files\OpenSSH\bin\junction.exe /accepteula "C:\Program Files\OpenSSH\tmp" "C:\Windows\Temp" |
| 59 | +C:\Windows\System32\icacls.exe "C:\Windows\Temp" /grant "vagrant:(OI)(CI)F" |
| 60 | + |
| 61 | +# add 64 bit environment variables missing from SSH |
| 62 | +Write-Host "Setting SSH environment" |
| 63 | +$sshenv = "TEMP=C:\Windows\Temp" |
| 64 | +if ($is_64bit) { |
| 65 | + $env_vars = "ProgramFiles(x86)=C:\Program Files (x86)", ` |
| 66 | + "ProgramW6432=C:\Program Files", ` |
| 67 | + "CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files", ` |
| 68 | + "CommonProgramW6432=C:\Program Files\Common Files" |
| 69 | + $sshenv = $sshenv + "`r`n" + ($env_vars -join "`r`n") |
| 70 | +} |
| 71 | +Set-Content C:\Users\vagrant\.ssh\environment $sshenv |
| 72 | + |
| 73 | +# record the path for provisioners (without the newline) |
| 74 | +Write-Host "Recording PATH for provisioners" |
| 75 | +Set-Content C:\Windows\Temp\PATH ([byte[]][char[]] $env:PATH) -Encoding Byte |
| 76 | + |
| 77 | +# configure firewall |
| 78 | +Write-Host "Configuring firewall" |
| 79 | +netsh advfirewall firewall add rule name="SSHD" dir=in action=allow service=OpenSSHd enable=yes |
| 80 | +netsh advfirewall firewall add rule name="SSHD" dir=in action=allow program="C:\Program Files\OpenSSH\usr\sbin\sshd.exe" enable=yes |
| 81 | +netsh advfirewall firewall add rule name="ssh" dir=in action=allow protocol=TCP localport=22 |
| 82 | + |
| 83 | +Start-Service "OpenSSHd" |
| 84 | + |
| 85 | + |
| 86 | +# This section is adapted from rsync.bat in https://github.com/joefitzgerald/packer-windows |
| 87 | + |
| 88 | +Set-Location C:\Windows\Temp |
| 89 | + |
| 90 | +(New-Object System.Net.WebClient).DownloadFile('http://downloads.sourceforge.net/sevenzip/7z920-x64.msi', 'C:\Windows\Temp\7z920-x64.msi') |
| 91 | +msiexec /q /i C:\Windows\Temp\7z920-x64.msi |
| 92 | + |
| 93 | +(New-Object System.Net.WebClient).DownloadFile('http://mirrors.kernel.org/sourceware/cygwin/x86_64/release/rsync/rsync-3.1.0-1.tar.xz', 'C:\Windows\Temp\rsync-3.1.0-1.tar.xz') |
| 94 | +C:\Program` Files\7-Zip\7z.exe x rsync-3.1.0-1.tar.xz |
| 95 | +C:\Program` Files\7-Zip\7z.exe x rsync-3.1.0-1.tar |
| 96 | +Copy-Item usr\bin\rsync.exe C:\Program` Files\OpenSSH\bin\rsync.exe |
| 97 | + |
| 98 | +Remove-Item -Recurse -Path usr |
| 99 | +Remove-Item rsync-3.1.0-1.tar |
| 100 | +msiexec /q /x C:\Windows\Temp\7z920-x64.msi |
0 commit comments