From 732a03c1c4f8775a448097178a5b537e44a65978 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 8 Jan 2014 15:12:26 -0800 Subject: [PATCH] guests/linux: properly quote and insert public key --- plugins/communicators/ssh/communicator.rb | 2 +- plugins/guests/linux/cap/insert_public_key.rb | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/plugins/communicators/ssh/communicator.rb b/plugins/communicators/ssh/communicator.rb index 0a9845436..a239b67ab 100644 --- a/plugins/communicators/ssh/communicator.rb +++ b/plugins/communicators/ssh/communicator.rb @@ -63,7 +63,7 @@ module VagrantPlugins @machine.ui.info(I18n.t("vagrant.inserting_insecure_key")) @machine.guest.capability( :insert_public_key, - Vagrant.source_root.join("keys", "vagrant.pub").read) + Vagrant.source_root.join("keys", "vagrant.pub").read.chomp) # Write out the private key in the data dir so that the # machine automatically picks it up. diff --git a/plugins/guests/linux/cap/insert_public_key.rb b/plugins/guests/linux/cap/insert_public_key.rb index 69a526541..f33e3329f 100644 --- a/plugins/guests/linux/cap/insert_public_key.rb +++ b/plugins/guests/linux/cap/insert_public_key.rb @@ -1,13 +1,17 @@ +require "vagrant/util/shell_quote" + module VagrantPlugins module GuestLinux module Cap class InsertPublicKey def self.insert_public_key(machine, contents) + contents = Vagrant::Util::ShellQuote.escape(contents, "'") + contents = contents.gsub("\n", "\\n") + machine.communicate.tap do |comm| - comm.execute("echo #{contents} > /tmp/key.pub") comm.execute("mkdir -p ~/.ssh") comm.execute("chmod 0700 ~/.ssh") - comm.execute("cat /tmp/key.pub >> ~/.ssh/authorized_keys") + comm.execute("printf '#{contents}' >> ~/.ssh/authorized_keys") comm.execute("chmod 0600 ~/.ssh/authorized_keys") end end