Skip to content

Commit af19868

Browse files
committed
Merge pull request #48 from pbitty/windows_elevate_privileges
elevate privileges in Windows host if necessary
2 parents 02758e3 + 1caa3c3 commit af19868

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ config.hostmanager.ip_resolver = proc do |vm|
9191
end
9292
```
9393

94+
Windows support
95+
---------------
96+
97+
Hostmanager will detect Windows guests and hosts and use the appropriate
98+
path for the ```hosts``` file: ```%WINDIR%\System32\drivers\etc\hosts```
99+
100+
By default on a Windows host, the ```hosts``` file is not writable without
101+
elevated privileges. If hostmanager detects that it cannot overwrite the file,
102+
it will attempt to do so with elevated privileges, causing the
103+
[UAC](http://en.wikipedia.org/wiki/User_Account_Control) prompt to appear.
104+
105+
### UAC limitations
106+
107+
Due to limitations caused by UAC, cancelling out of the UAC prompt will not cause any
108+
visible errors, however the ```hosts``` file will not be updated.
109+
94110
Contribute
95111
----------
96112
Contributions are welcome.

lib/vagrant-hostmanager/hosts_file.rb

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,23 @@ def update_guest(machine)
3434
def update_host
3535
# copy and modify hosts file on host with Vagrant-managed entries
3636
file = @global_env.tmp_path.join('hosts.local')
37-
# add a "if windows..."
38-
hosts_location = '/etc/hosts'
39-
copy_cmd = 'sudo cp'
40-
# handles the windows hosts file...
41-
if ENV['SystemRoot'] != nil
37+
38+
if WindowsSupport.windows?
39+
# lazily include windows Module
40+
class << self
41+
include WindowsSupport unless include? WindowsSupport
42+
end
43+
4244
hosts_location = "#{ENV['WINDIR']}\\System32\\drivers\\etc\\hosts"
43-
copy_cmd = 'cp'
45+
copy_proc = Proc.new { windows_copy_file(file, hosts_location) }
46+
else
47+
hosts_location = '/etc/hosts'
48+
copy_proc = Proc.new { `sudo cp #{file} #{hosts_location}` }
4449
end
50+
4551
FileUtils.cp(hosts_location, file)
4652
update_file(file)
47-
48-
# copy modified file using sudo for permission
49-
`#{copy_cmd} #{file} #{hosts_location}`
53+
copy_proc.call
5054
end
5155

5256
private
@@ -122,6 +126,45 @@ def get_machines
122126
@global_env.active_machines
123127
end
124128
end
129+
130+
## Windows support for copying files, requesting elevated privileges if necessary
131+
module WindowsSupport
132+
require 'rbconfig'
133+
134+
def self.windows?
135+
RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
136+
end
137+
138+
require 'win32ole' if windows?
139+
140+
def windows_copy_file(source, dest)
141+
begin
142+
# First, try Ruby copy
143+
FileUtils.cp(source, dest)
144+
rescue Errno::EACCES
145+
# Access denied, try with elevated privileges
146+
windows_copy_file_elevated(source, dest)
147+
end
148+
end
149+
150+
private
151+
152+
def windows_copy_file_elevated(source, dest)
153+
# copy command only supports backslashes as separators
154+
source, dest = [source, dest].map { |s| s.to_s.gsub(/\//, '\\') }
155+
156+
# run 'cmd /C copy ...' with elevated privilege, minimized
157+
copy_cmd = "copy \"#{source}\" \"#{dest}\""
158+
WIN32OLE.new('Shell.Application').ShellExecute('cmd', "/C #{copy_cmd}", nil, 'runas', 7)
159+
160+
# Unfortunately, ShellExecute does not give us a status code,
161+
# and it is non-blocking so we can't reliably compare the file contents
162+
# to see if they were copied.
163+
#
164+
# If the user rejects the UAC prompt, vagrant will silently continue
165+
# without updating the hostsfile.
166+
end
167+
end
125168
end
126169
end
127170
end

0 commit comments

Comments
 (0)