Skip to content

Commit

Permalink
Merge pull request #119 from cogitatio/feature/uac-support
Browse files Browse the repository at this point in the history
Widnows users get UAC Prompt and fixes #40 thanks to @QWp6t (#86)
  • Loading branch information
cgsmith authored Aug 11, 2016
2 parents 478ade2 + 0867960 commit baf1779
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand All @@ -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)
Expand Down
19 changes: 16 additions & 3 deletions lib/vagrant-hostsupdater/HostsUpdater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -151,15 +160,19 @@ 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
end

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
Expand Down

0 comments on commit baf1779

Please sign in to comment.