2017-07-07 16:38:11 +00:00
|
|
|
require "tempfile"
|
|
|
|
|
2017-10-24 19:02:01 +00:00
|
|
|
require_relative '../../../communicators/winssh/communicator'
|
|
|
|
|
2017-07-07 16:38:11 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module GuestWindows
|
|
|
|
module Cap
|
|
|
|
class PublicKey
|
|
|
|
def self.insert_public_key(machine, contents)
|
|
|
|
if machine.communicate.is_a?(CommunicatorWinSSH::Communicator)
|
2017-07-19 07:49:12 +00:00
|
|
|
contents = contents.strip
|
2017-08-21 21:48:52 +00:00
|
|
|
winssh_modify_authorized_keys machine do |keys|
|
2017-07-19 07:49:12 +00:00
|
|
|
if !keys.include?(contents)
|
|
|
|
keys << contents
|
|
|
|
end
|
|
|
|
end
|
2017-07-07 16:38:11 +00:00
|
|
|
else
|
|
|
|
raise Vagrant::Errors::SSHInsertKeyUnsupported
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.remove_public_key(machine, contents)
|
|
|
|
if machine.communicate.is_a?(CommunicatorWinSSH::Communicator)
|
2017-08-21 21:48:52 +00:00
|
|
|
winssh_modify_authorized_keys machine do |keys|
|
2017-07-19 07:49:12 +00:00
|
|
|
keys.delete(contents)
|
|
|
|
end
|
2017-07-07 16:38:11 +00:00
|
|
|
else
|
|
|
|
raise Vagrant::Errors::SSHInsertKeyUnsupported
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-21 21:48:52 +00:00
|
|
|
def self.winssh_modify_authorized_keys(machine)
|
2017-07-07 16:38:11 +00:00
|
|
|
comm = machine.communicate
|
|
|
|
directories = fetch_guest_paths(comm)
|
|
|
|
home_dir = directories[:home]
|
|
|
|
temp_dir = directories[:temp]
|
|
|
|
|
|
|
|
# Ensure the user's ssh directory exists
|
2017-07-18 08:34:49 +00:00
|
|
|
remote_ssh_dir = "#{home_dir}\\.ssh"
|
2017-07-07 16:38:11 +00:00
|
|
|
comm.execute("dir \"#{remote_ssh_dir}\"\n if errorlevel 1 (mkdir \"#{remote_ssh_dir}\")", shell: "cmd")
|
|
|
|
remote_upload_path = "#{temp_dir}\\vagrant-insert-pubkey-#{Time.now.to_i}"
|
|
|
|
remote_authkeys_path = "#{remote_ssh_dir}\\authorized_keys"
|
|
|
|
|
|
|
|
keys_file = Tempfile.new("vagrant-windows-insert-public-key")
|
2017-07-18 08:34:49 +00:00
|
|
|
keys_file.close
|
2017-07-07 16:38:11 +00:00
|
|
|
# Check if an authorized_keys file already exists
|
|
|
|
result = comm.execute("dir \"#{remote_authkeys_path}\"", shell: "cmd", error_check: false)
|
|
|
|
if result == 0
|
|
|
|
comm.download(remote_authkeys_path, keys_file.path)
|
2017-07-19 07:49:12 +00:00
|
|
|
keys = File.read(keys_file.path).split(/[\r\n]+/)
|
2017-07-07 16:38:11 +00:00
|
|
|
else
|
2017-07-19 07:49:12 +00:00
|
|
|
keys = []
|
2017-07-07 16:38:11 +00:00
|
|
|
end
|
2017-07-19 07:49:12 +00:00
|
|
|
yield keys
|
|
|
|
File.write(keys_file.path, keys.join("\r\n") + "\r\n")
|
2017-07-07 16:38:11 +00:00
|
|
|
comm.upload(keys_file.path, remote_upload_path)
|
2017-07-18 08:34:49 +00:00
|
|
|
keys_file.delete
|
2017-07-19 07:49:12 +00:00
|
|
|
comm.execute <<-EOC.gsub(/^\s*/, ""), shell: "powershell"
|
|
|
|
Set-Acl "#{remote_upload_path}" (Get-Acl "#{remote_authkeys_path}")
|
|
|
|
Move-Item -Force "#{remote_upload_path}" "#{remote_authkeys_path}"
|
|
|
|
EOC
|
2017-07-07 16:38:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Fetch user's temporary and home directory paths from the Windows guest
|
|
|
|
#
|
|
|
|
# @param [Communicator]
|
|
|
|
# @return [Hash] {:temp, :home}
|
|
|
|
def self.fetch_guest_paths(communicator)
|
|
|
|
output = ""
|
|
|
|
communicator.execute("echo %TEMP%\necho %USERPROFILE%", shell: "cmd") do |type, data|
|
|
|
|
if type == :stdout
|
|
|
|
output << data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
temp_dir, home_dir = output.strip.split(/[\r\n]+/)
|
|
|
|
if temp_dir.nil? || home_dir.nil?
|
|
|
|
raise Errors::PublicKeyDirectoryFailure
|
|
|
|
end
|
|
|
|
{temp: temp_dir, home: home_dir}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|