Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows users get UAC prompt, fixes #40 #86

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

This plugin adds an entry to your /etc/hosts file on the host system.

On **up**, **resume** and **reload** commands, it tries to add the information, if its not already existant in your hosts file. If it needs to be added, you will be asked for an administrator password, since it uses sudo to edit the file.
On **up**, **resume** and **reload** commands, it tries to add the information, if its not already existant in your
hosts file. If it needs to be added, you will be asked for an administrator password, since it uses sudo to edit
the file.

On **halt**, **destroy**, and **suspend**, those entries will be removed again.
By setting the `config.hostsupdater.remove_on_suspend = false`, **suspend** will not remove them.
Expand Down Expand Up @@ -53,7 +55,12 @@ Example:
hostsupdater: "skip"


## 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 @@ -62,20 +69,23 @@ stop asking password when updating hosts file:
Cmnd_Alias VAGRANT_HOSTS_ADD = /bin/sh -c echo "*" >> /etc/hosts
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

If you would like to install vagrant-hostsupdater on the development version perform the following:

```
git clone https://github.com/cogitatio/vagrant-hostsupdater
cd vagrant-hostsupdater
git checkout develop
gem build vagrant-hostsupdater.gemspec
vagrant plugin install vagrant-hostsupdater-*.gem
```
git clone https://github.com/cogitatio/vagrant-hostsupdater
cd vagrant-hostsupdater
git checkout develop
gem build vagrant-hostsupdater.gemspec
vagrant plugin install vagrant-hostsupdater-*.gem

## Contributing

Expand All @@ -88,6 +98,10 @@ vagrant plugin install vagrant-hostsupdater-*.gem

## Versions

### HEAD
* Bugfix: Windows users get UAC prompt [#40](/../../issues/40)
* Misc: Added note to suppress UAC prompts

### 1.0.1
* Bugfix: Fixing `up` issue on initialize [#28](/../../issues/28)

Expand Down
17 changes: 15 additions & 2 deletions lib/vagrant-hostsupdater/HostsUpdater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ def addToHosts(entries)
content = entries.join("\n").strip.concat("\n")
if !File.writable_real?(@@hosts_path)
sudo(%Q(sh -c 'echo "#{content}" >> #@@hosts_path'))
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 @@ -91,7 +100,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?
sudo(%Q(sed -i -e '/#{hashedId}/ d' #@@hosts_path))
else
hosts = ""
Expand All @@ -114,7 +123,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
`sudo #{command}`
end
Expand Down