Skip to content

Commit da160a8

Browse files
author
Brent Cook
committed
Land rapid7#8179, cleanup msfupdate, add git config checks
2 parents 416a5cd + 1892ac0 commit da160a8

File tree

1 file changed

+56
-29
lines changed

1 file changed

+56
-29
lines changed

msfupdate

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,19 @@ while File.symlink?(msfbase)
1313
msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
1414
end
1515

16-
1716
class Msfupdate
1817
attr_reader :stdin
1918
attr_reader :stdout
2019
attr_reader :stderr
2120

22-
def initialize(msfbase_dir, stdin=$stdin, stdout=$stdout, stderr=$stderr)
21+
def initialize(msfbase_dir, stdin = $stdin, stdout = $stdout, stderr = $stderr)
2322
@msfbase_dir = msfbase_dir
2423
@stdin = stdin
2524
@stdout = stdout
2625
@stderr = stderr
2726
end
2827

29-
def usage(io=stdout)
28+
def usage(io = stdout)
3029
help = "usage: msfupdate [options...]\n"
3130
help << "Options:\n"
3231
help << "-h, --help show help\n"
@@ -42,14 +41,14 @@ class Msfupdate
4241
# Copy args into ARGV, then restore ARGV after GetoptLong
4342
real_args = ARGV.clone
4443
ARGV.clear
45-
args.each {|arg| ARGV << arg}
44+
args.each { |arg| ARGV << arg }
4645

4746
require 'getoptlong'
4847
opts = GetoptLong.new(
4948
['--help', '-h', GetoptLong::NO_ARGUMENT],
5049
['--git-remote', GetoptLong::REQUIRED_ARGUMENT],
5150
['--git-branch', GetoptLong::REQUIRED_ARGUMENT],
52-
['--offline-file', GetoptLong::REQUIRED_ARGUMENT],
51+
['--offline-file', GetoptLong::REQUIRED_ARGUMENT]
5352
)
5453

5554
begin
@@ -67,7 +66,7 @@ class Msfupdate
6766
end
6867
end
6968
rescue GetoptLong::Error
70-
stderr.puts "#{$0}: try 'msfupdate --help' for more information"
69+
stderr.puts "#{$PROGRAM_NAME}: try 'msfupdate --help' for more information"
7170
maybe_wait_and_exit 0x20
7271
end
7372

@@ -79,7 +78,7 @@ class Msfupdate
7978
ensure
8079
# Restore the original ARGV value
8180
ARGV.clear
82-
real_args.each {|arg| ARGV << arg}
81+
real_args.each { |arg| ARGV << arg }
8382
end
8483
end
8584

@@ -126,7 +125,7 @@ class Msfupdate
126125
stderr.puts ""
127126

128127
# Bail right away, no waiting around for consoles.
129-
if not (Process.uid == 0 or File.stat(@msfbase_dir).owned?)
128+
unless Process.uid.zero? || File.stat(@msfbase_dir).owned?
130129
stderr.puts "[-] ERROR: User running msfupdate does not own the Metasploit installation"
131130
stderr.puts "[-] Please run msfupdate as the same user who installed Metasploit."
132131
maybe_wait_and_exit 0x10
@@ -140,17 +139,47 @@ class Msfupdate
140139
elsif apt?
141140
update_apt!
142141
else
143-
raise RuntimeError, "Cannot determine checkout type: `#{@msfbase_dir}'"
142+
raise "Cannot determine checkout type: `#{@msfbase_dir}'"
144143
end
145144
end
146145
end
147146

147+
# We could also do this by running `git config --global user.name` and `git config --global user.email`
148+
# and check the output of those. (it's a bit quieter)
149+
def git_globals_okay?
150+
require 'os'
151+
output = ''
152+
begin
153+
output = `git config --list`
154+
rescue Errno::ENOENT
155+
stderr.puts '[-] ERROR: Failed to check git settings, git not found'
156+
return false
157+
end
158+
159+
unless output.include? 'user.name'
160+
stderr.puts '[-] ERROR: user.name is not set in your global git configuration'
161+
stderr.puts '[-] Set it by running: \'git config --global user.name "NAME HERE"\''
162+
stderr.puts ''
163+
return false
164+
end
165+
166+
unless output.include? 'user.email'
167+
stderr.puts '[-] ERROR: user.email is not set in your global git configuration'
168+
stderr.puts '[-] Set it by running: \'git config --global user.email "[email protected]"\''
169+
stderr.puts ''
170+
return false
171+
end
172+
173+
true
174+
end
175+
148176
def update_git!
149177
####### Since we're Git, do it all that way #######
150178
stdout.puts "[*] Checking for updates via git"
151179
stdout.puts "[*] Note: Updating from bleeding edge"
152180
out = `git remote show upstream` # Actually need the output for this one.
153-
add_git_upstream unless $?.success? and out =~ %r{(https|git|git@github\.com):(//github\.com/)?(rapid7/metasploit-framework\.git)}
181+
add_git_upstream unless $?.success? &&
182+
out =~ %r{(https|git|git@github\.com):(//github\.com/)?(rapid7/metasploit-framework\.git)}
154183

155184
remote = @git_remote || "upstream"
156185
branch = @git_branch || "master"
@@ -163,8 +192,14 @@ class Msfupdate
163192
# to begin with.
164193
#
165194
# Note, this requires at least user.name and user.email
166-
# to be configured in the global git config. Installers should
167-
# take care that this is done. TODO: Enforce this in msfupdate
195+
# to be configured in the global git config. Installers
196+
# will be told to set them if they aren't already set.
197+
198+
# Checks user.name and user.email
199+
global_status = git_globals_okay?
200+
maybe_wait_and_exit(1) unless global_status
201+
202+
# We shouldn't get here if the globals dont check out
168203
committed = system("git", "diff", "--quiet", "HEAD")
169204
if committed.nil?
170205
stderr.puts "[-] ERROR: Failed to run git"
@@ -173,7 +208,7 @@ class Msfupdate
173208
stderr.puts "[-] /usr/local/bin instead of running this file directly (e.g.: ./msfupdate)"
174209
stderr.puts "[-] to ensure a proper environment."
175210
maybe_wait_and_exit 1
176-
elsif not committed
211+
elsif !committed
177212
system("git", "stash")
178213
stdout.puts "[*] Stashed local changes to avoid merge conflicts."
179214
stdout.puts "[*] Run `git stash pop` to reapply local changes."
@@ -203,7 +238,7 @@ class Msfupdate
203238
product_key = File.expand_path(File.join(@msfbase_dir, "..", "engine", "license", "product.key"))
204239
if File.exist? product_key
205240
if File.readable? product_key
206-
if (@offline_file)
241+
if @offline_file
207242
system("ruby", update_script, @offline_file)
208243
else
209244
system("ruby", update_script)
@@ -235,19 +270,13 @@ class Msfupdate
235270
stdout.puts "[*] Note: expect weekly(ish) updates using this method"
236271
system("apt-get", "-qq", "update")
237272

238-
packages = []
239-
packages << 'metasploit-framework' if framework_version = apt_upgrade_available('metasploit-framework')
240-
packages << 'metasploit' if pro_version = apt_upgrade_available('metasploit')
273+
framework_version = apt_upgrade_available('metasploit-framework')
241274

242-
if packages.empty?
275+
if framework_version.blank?
243276
stdout.puts "[*] No updates available"
244277
else
245-
stdout.puts "[*] Updating to version #{pro_version || framework_version}"
246-
system("apt-get", "install", "--assume-yes", *packages)
247-
if packages.include?('metasploit')
248-
start_cmd = File.expand_path(File.join(@msfbase_dir, '..', '..', '..', 'scripts', 'start.sh'))
249-
system(start_cmd) if ::File.executable_real? start_cmd
250-
end
278+
stdout.puts "[*] Updating to version #{framework_version}"
279+
system("apt-get", "install", "--assume-yes", "metasploit-framework")
251280
end
252281
end
253282

@@ -262,23 +291,21 @@ class Msfupdate
262291

263292
# This only exits if you actually pass a wait option, otherwise
264293
# just returns nil. This is likely unexpected, revisit this.
265-
def maybe_wait_and_exit(exit_code=0)
294+
def maybe_wait_and_exit(exit_code = 0)
266295
if @actually_wait
267296
stdout.puts ""
268297
stdout.puts "[*] Please hit enter to exit"
269298
stdout.puts ""
270299
stdin.readline
271-
exit exit_code
272-
else
273-
exit exit_code
274300
end
301+
exit exit_code
275302
end
276303

277304
def apt_upgrade_available(package)
278305
require 'open3'
279306
installed = nil
280307
upgrade = nil
281-
::Open3.popen3({'LANG'=>'en_US.UTF-8'}, "apt-cache", "policy", package) do |stdin, stdout, stderr|
308+
::Open3.popen3({ 'LANG' => 'en_US.UTF-8' }, "apt-cache", "policy", package) do |_stdin, stdout, _stderr|
282309
stdout.each do |line|
283310
installed = $1 if line =~ /Installed: ([\w\-+.:~]+)$/
284311
upgrade = $1 if line =~ /Candidate: ([\w\-+.:~]+)$/

0 commit comments

Comments
 (0)