Skip to content

Commit 1892ac0

Browse files
author
Brent Cook
committed
tidy code, remove pro support, don't use tempfile, simplify checks
1 parent 97e47be commit 1892ac0

File tree

1 file changed

+28
-54
lines changed

1 file changed

+28
-54
lines changed

msfupdate

Lines changed: 28 additions & 54 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,63 +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

148-
# We could also do this by running `git config --global user.name` and `git config --global user.email`
147+
# We could also do this by running `git config --global user.name` and `git config --global user.email`
149148
# and check the output of those. (it's a bit quieter)
150149
def git_globals_okay?
151-
require 'tempfile'
152150
require 'os'
153151
output = ''
154-
155-
# Make it cross platform
156-
if !OS.windows?
157-
conf_out_status = system('git config --list > /dev/null 2>&1') # Just see if it runs or not
158-
else
159-
conf_out_status = system('git config --list > nul 2&>1')
160-
end
161-
if !conf_out_status || conf_out_status.nil?
162-
stderr.puts '[-] ERROR: Failed to check git settings'
152+
begin
153+
output = `git config --list`
154+
rescue Errno::ENOENT
155+
stderr.puts '[-] ERROR: Failed to check git settings, git not found'
163156
return false
164157
end
165158

166-
begin
167-
conf = Tempfile.new('gitconf') # Create a tempfile
168-
path = conf.path
169-
system("git config --list > #{path}") # Write to the tempfile (Pretty sure this command is cross platform)
170-
output >> conf.read
171-
ensure
172-
conf.close
173-
conf.unlink # Delete the temp file
174-
end
175-
176-
if !output.include? 'user.name'
159+
unless output.include? 'user.name'
177160
stderr.puts '[-] ERROR: user.name is not set in your global git configuration'
178161
stderr.puts '[-] Set it by running: \'git config --global user.name "NAME HERE"\''
179162
stderr.puts ''
180163
return false
181164
end
182165

183-
if !output.include? 'user.email'
166+
unless output.include? 'user.email'
184167
stderr.puts '[-] ERROR: user.email is not set in your global git configuration'
185168
stderr.puts '[-] Set it by running: \'git config --global user.email "[email protected]"\''
186169
stderr.puts ''
187170
return false
188171
end
189172

190173
true
191-
192174
end
193175

194176
def update_git!
195177
####### Since we're Git, do it all that way #######
196178
stdout.puts "[*] Checking for updates via git"
197179
stdout.puts "[*] Note: Updating from bleeding edge"
198180
out = `git remote show upstream` # Actually need the output for this one.
199-
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)}
200183

201184
remote = @git_remote || "upstream"
202185
branch = @git_branch || "master"
@@ -214,9 +197,8 @@ class Msfupdate
214197

215198
# Checks user.name and user.email
216199
global_status = git_globals_okay?
217-
maybe_wait_and_exit(1) if !global_status
218-
219-
200+
maybe_wait_and_exit(1) unless global_status
201+
220202
# We shouldn't get here if the globals dont check out
221203
committed = system("git", "diff", "--quiet", "HEAD")
222204
if committed.nil?
@@ -226,7 +208,7 @@ class Msfupdate
226208
stderr.puts "[-] /usr/local/bin instead of running this file directly (e.g.: ./msfupdate)"
227209
stderr.puts "[-] to ensure a proper environment."
228210
maybe_wait_and_exit 1
229-
elsif not committed
211+
elsif !committed
230212
system("git", "stash")
231213
stdout.puts "[*] Stashed local changes to avoid merge conflicts."
232214
stdout.puts "[*] Run `git stash pop` to reapply local changes."
@@ -256,7 +238,7 @@ class Msfupdate
256238
product_key = File.expand_path(File.join(@msfbase_dir, "..", "engine", "license", "product.key"))
257239
if File.exist? product_key
258240
if File.readable? product_key
259-
if (@offline_file)
241+
if @offline_file
260242
system("ruby", update_script, @offline_file)
261243
else
262244
system("ruby", update_script)
@@ -288,19 +270,13 @@ class Msfupdate
288270
stdout.puts "[*] Note: expect weekly(ish) updates using this method"
289271
system("apt-get", "-qq", "update")
290272

291-
packages = []
292-
packages << 'metasploit-framework' if framework_version = apt_upgrade_available('metasploit-framework')
293-
packages << 'metasploit' if pro_version = apt_upgrade_available('metasploit')
273+
framework_version = apt_upgrade_available('metasploit-framework')
294274

295-
if packages.empty?
275+
if framework_version.blank?
296276
stdout.puts "[*] No updates available"
297277
else
298-
stdout.puts "[*] Updating to version #{pro_version || framework_version}"
299-
system("apt-get", "install", "--assume-yes", *packages)
300-
if packages.include?('metasploit')
301-
start_cmd = File.expand_path(File.join(@msfbase_dir, '..', '..', '..', 'scripts', 'start.sh'))
302-
system(start_cmd) if ::File.executable_real? start_cmd
303-
end
278+
stdout.puts "[*] Updating to version #{framework_version}"
279+
system("apt-get", "install", "--assume-yes", "metasploit-framework")
304280
end
305281
end
306282

@@ -315,23 +291,21 @@ class Msfupdate
315291

316292
# This only exits if you actually pass a wait option, otherwise
317293
# just returns nil. This is likely unexpected, revisit this.
318-
def maybe_wait_and_exit(exit_code=0)
294+
def maybe_wait_and_exit(exit_code = 0)
319295
if @actually_wait
320296
stdout.puts ""
321297
stdout.puts "[*] Please hit enter to exit"
322298
stdout.puts ""
323299
stdin.readline
324-
exit exit_code
325-
else
326-
exit exit_code
327300
end
301+
exit exit_code
328302
end
329303

330304
def apt_upgrade_available(package)
331305
require 'open3'
332306
installed = nil
333307
upgrade = nil
334-
::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|
335309
stdout.each do |line|
336310
installed = $1 if line =~ /Installed: ([\w\-+.:~]+)$/
337311
upgrade = $1 if line =~ /Candidate: ([\w\-+.:~]+)$/

0 commit comments

Comments
 (0)