diff --git a/README.md b/README.md index 6283f76..4a609a5 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,12 @@ To keep your /etc/hosts file unchanged simply add the line below to your `Vagran This disables vagrant-hostsupdater from running on **suspend** and **halt**. -## Passwordless sudo +## Suppressing prompts for elevating privileges + +These prompts exist to prevent anything that is being run by the user from inadvertently updating the hosts file. +If you understand the risks that go with supressing them, here's how to do it. + +### Linux/OS X: Passwordless sudo Add the following snippet to the top of the sudoers file using `sudo visudo`. It will make vagrant stop asking password when updating hosts file: @@ -92,7 +97,12 @@ stop asking password when updating hosts file: Cmnd_Alias VAGRANT_HOSTS_REMOVE = /usr/bin/sed -i -e /*/ d /etc/hosts %admin ALL=(root) NOPASSWD: VAGRANT_HOSTS_ADD, VAGRANT_HOSTS_REMOVE - +### Windows: UAC Prompt + +You can use `cacls` or `icacls` to grant your user account permanent write permission to the system's hosts file. +You have to open an elevated command prompt; hold `❖ Win` and press `X`, then choose "Command Prompt (Admin)" + + cacls %SYSTEMROOT%\system32\drivers\etc\hosts /E /G %USERNAME%:W ## Installing development version @@ -117,6 +127,10 @@ vagrant plugin install vagrant-hostsupdater-*.gem ## Versions +### next version +* Bugfix: Windows users get UAC prompt [#40](/../../issues/40) +* Misc: Added a note about suppressing UAC prompts + ### 1.0.2 * Feature: Added `remove_on_suspend` for `vagrant_halt` [#71](/../../issues/71) * Feature: Skip entries if they already exist [#69](/../../issues/69) diff --git a/lib/vagrant-hostsupdater/HostsUpdater.rb b/lib/vagrant-hostsupdater/HostsUpdater.rb index be7b03d..d889a8b 100644 --- a/lib/vagrant-hostsupdater/HostsUpdater.rb +++ b/lib/vagrant-hostsupdater/HostsUpdater.rb @@ -114,6 +114,15 @@ def addToHosts(entries) @ui.error "[vagrant-hostsupdater] Failed to add hosts, could not use sudo" adviseOnSudo end + elsif Vagrant::Util::Platform.windows? + require 'tmpdir' + uuid = @machine.id || @machine.config.hostsupdater.id + tmpPath = File.join(Dir.tmpdir, 'hosts-' + uuid + '.cmd') + File.open(tmpPath, "w") do |tmpFile| + entries.each { |line| tmpFile.puts(">>\"#{@@hosts_path}\" echo #{line}") } + end + sudo(tmpPath) + File.delete(tmpPath) else content = "\n" + content hostsFile = File.open(@@hosts_path, "a") @@ -125,7 +134,7 @@ def addToHosts(entries) def removeFromHosts(options = {}) uuid = @machine.id || @machine.config.hostsupdater.id hashedId = Digest::MD5.hexdigest(uuid) - if !File.writable_real?(@@hosts_path) + if !File.writable_real?(@@hosts_path) || Vagrant::Util::Platform.windows? if !sudo(%Q(sed -i -e '/#{hashedId}/ d' #@@hosts_path)) @ui.error "[vagrant-hostsupdater] Failed to remove hosts, could not use sudo" adviseOnSudo @@ -151,7 +160,11 @@ def signature(name, uuid = self.uuid) def sudo(command) return if !command if Vagrant::Util::Platform.windows? - `#{command}` + require 'win32ole' + args = command.split(" ") + command = args.shift + sh = WIN32OLE.new('Shell.Application') + sh.ShellExecute(command, args.join(" "), '', 'runas', 0) else return system("sudo #{command}") end @@ -159,7 +172,7 @@ def sudo(command) def adviseOnSudo @ui.error "[vagrant-hostsupdater] Consider adding the following to your sudoers file:" - @ui.error "[vagrant-hostsupdater] https://github.com/cogitatio/vagrant-hostsupdater#passwordless-sudo" + @ui.error "[vagrant-hostsupdater] https://github.com/cogitatio/vagrant-hostsupdater#suppressing-prompts-for-elevating-privileges" end end end