Skip to content

Commit baf1779

Browse files
authored
Merge pull request #119 from cogitatio/feature/uac-support
Widnows users get UAC Prompt and fixes #40 thanks to @QWp6t (#86)
2 parents 478ade2 + 0867960 commit baf1779

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ To keep your /etc/hosts file unchanged simply add the line below to your `Vagran
8282
This disables vagrant-hostsupdater from running on **suspend** and **halt**.
8383

8484

85-
## Passwordless sudo
85+
## Suppressing prompts for elevating privileges
86+
87+
These prompts exist to prevent anything that is being run by the user from inadvertently updating the hosts file.
88+
If you understand the risks that go with supressing them, here's how to do it.
89+
90+
### Linux/OS X: Passwordless sudo
8691

8792
Add the following snippet to the top of the sudoers file using `sudo visudo`. It will make vagrant
8893
stop asking password when updating hosts file:
@@ -92,7 +97,12 @@ stop asking password when updating hosts file:
9297
Cmnd_Alias VAGRANT_HOSTS_REMOVE = /usr/bin/sed -i -e /*/ d /etc/hosts
9398
%admin ALL=(root) NOPASSWD: VAGRANT_HOSTS_ADD, VAGRANT_HOSTS_REMOVE
9499

95-
100+
### Windows: UAC Prompt
101+
102+
You can use `cacls` or `icacls` to grant your user account permanent write permission to the system's hosts file.
103+
You have to open an elevated command prompt; hold `❖ Win` and press `X`, then choose "Command Prompt (Admin)"
104+
105+
cacls %SYSTEMROOT%\system32\drivers\etc\hosts /E /G %USERNAME%:W
96106

97107
## Installing development version
98108

@@ -117,6 +127,10 @@ vagrant plugin install vagrant-hostsupdater-*.gem
117127

118128
## Versions
119129

130+
### next version
131+
* Bugfix: Windows users get UAC prompt [#40](/../../issues/40)
132+
* Misc: Added a note about suppressing UAC prompts
133+
120134
### 1.0.2
121135
* Feature: Added `remove_on_suspend` for `vagrant_halt` [#71](/../../issues/71)
122136
* Feature: Skip entries if they already exist [#69](/../../issues/69)

lib/vagrant-hostsupdater/HostsUpdater.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ def addToHosts(entries)
114114
@ui.error "[vagrant-hostsupdater] Failed to add hosts, could not use sudo"
115115
adviseOnSudo
116116
end
117+
elsif Vagrant::Util::Platform.windows?
118+
require 'tmpdir'
119+
uuid = @machine.id || @machine.config.hostsupdater.id
120+
tmpPath = File.join(Dir.tmpdir, 'hosts-' + uuid + '.cmd')
121+
File.open(tmpPath, "w") do |tmpFile|
122+
entries.each { |line| tmpFile.puts(">>\"#{@@hosts_path}\" echo #{line}") }
123+
end
124+
sudo(tmpPath)
125+
File.delete(tmpPath)
117126
else
118127
content = "\n" + content
119128
hostsFile = File.open(@@hosts_path, "a")
@@ -125,7 +134,7 @@ def addToHosts(entries)
125134
def removeFromHosts(options = {})
126135
uuid = @machine.id || @machine.config.hostsupdater.id
127136
hashedId = Digest::MD5.hexdigest(uuid)
128-
if !File.writable_real?(@@hosts_path)
137+
if !File.writable_real?(@@hosts_path) || Vagrant::Util::Platform.windows?
129138
if !sudo(%Q(sed -i -e '/#{hashedId}/ d' #@@hosts_path))
130139
@ui.error "[vagrant-hostsupdater] Failed to remove hosts, could not use sudo"
131140
adviseOnSudo
@@ -151,15 +160,19 @@ def signature(name, uuid = self.uuid)
151160
def sudo(command)
152161
return if !command
153162
if Vagrant::Util::Platform.windows?
154-
`#{command}`
163+
require 'win32ole'
164+
args = command.split(" ")
165+
command = args.shift
166+
sh = WIN32OLE.new('Shell.Application')
167+
sh.ShellExecute(command, args.join(" "), '', 'runas', 0)
155168
else
156169
return system("sudo #{command}")
157170
end
158171
end
159172

160173
def adviseOnSudo
161174
@ui.error "[vagrant-hostsupdater] Consider adding the following to your sudoers file:"
162-
@ui.error "[vagrant-hostsupdater] https://github.com/cogitatio/vagrant-hostsupdater#passwordless-sudo"
175+
@ui.error "[vagrant-hostsupdater] https://github.com/cogitatio/vagrant-hostsupdater#suppressing-prompts-for-elevating-privileges"
163176
end
164177
end
165178
end

0 commit comments

Comments
 (0)