@@ -13,20 +13,19 @@ while File.symlink?(msfbase)
13
13
msfbase = File . expand_path ( File . readlink ( msfbase ) , File . dirname ( msfbase ) )
14
14
end
15
15
16
-
17
16
class Msfupdate
18
17
attr_reader :stdin
19
18
attr_reader :stdout
20
19
attr_reader :stderr
21
20
22
- def initialize ( msfbase_dir , stdin = $stdin, stdout = $stdout, stderr = $stderr)
21
+ def initialize ( msfbase_dir , stdin = $stdin, stdout = $stdout, stderr = $stderr)
23
22
@msfbase_dir = msfbase_dir
24
23
@stdin = stdin
25
24
@stdout = stdout
26
25
@stderr = stderr
27
26
end
28
27
29
- def usage ( io = stdout )
28
+ def usage ( io = stdout )
30
29
help = "usage: msfupdate [options...]\n "
31
30
help << "Options:\n "
32
31
help << "-h, --help show help\n "
@@ -42,14 +41,14 @@ class Msfupdate
42
41
# Copy args into ARGV, then restore ARGV after GetoptLong
43
42
real_args = ARGV . clone
44
43
ARGV . clear
45
- args . each { |arg | ARGV << arg }
44
+ args . each { |arg | ARGV << arg }
46
45
47
46
require 'getoptlong'
48
47
opts = GetoptLong . new (
49
48
[ '--help' , '-h' , GetoptLong ::NO_ARGUMENT ] ,
50
49
[ '--git-remote' , GetoptLong ::REQUIRED_ARGUMENT ] ,
51
50
[ '--git-branch' , GetoptLong ::REQUIRED_ARGUMENT ] ,
52
- [ '--offline-file' , GetoptLong ::REQUIRED_ARGUMENT ] ,
51
+ [ '--offline-file' , GetoptLong ::REQUIRED_ARGUMENT ]
53
52
)
54
53
55
54
begin
@@ -67,7 +66,7 @@ class Msfupdate
67
66
end
68
67
end
69
68
rescue GetoptLong ::Error
70
- stderr . puts "#{ $0 } : try 'msfupdate --help' for more information"
69
+ stderr . puts "#{ $PROGRAM_NAME } : try 'msfupdate --help' for more information"
71
70
maybe_wait_and_exit 0x20
72
71
end
73
72
@@ -79,7 +78,7 @@ class Msfupdate
79
78
ensure
80
79
# Restore the original ARGV value
81
80
ARGV . clear
82
- real_args . each { |arg | ARGV << arg }
81
+ real_args . each { |arg | ARGV << arg }
83
82
end
84
83
end
85
84
@@ -126,7 +125,7 @@ class Msfupdate
126
125
stderr . puts ""
127
126
128
127
# 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?
130
129
stderr . puts "[-] ERROR: User running msfupdate does not own the Metasploit installation"
131
130
stderr . puts "[-] Please run msfupdate as the same user who installed Metasploit."
132
131
maybe_wait_and_exit 0x10
@@ -140,63 +139,47 @@ class Msfupdate
140
139
elsif apt?
141
140
update_apt!
142
141
else
143
- raise RuntimeError , "Cannot determine checkout type: `#{ @msfbase_dir } '"
142
+ raise "Cannot determine checkout type: `#{ @msfbase_dir } '"
144
143
end
145
144
end
146
145
end
147
146
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`
149
148
# and check the output of those. (it's a bit quieter)
150
149
def git_globals_okay?
151
- require 'tempfile'
152
150
require 'os'
153
151
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'
163
156
return false
164
157
end
165
158
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'
177
160
stderr . puts '[-] ERROR: user.name is not set in your global git configuration'
178
161
stderr . puts '[-] Set it by running: \'git config --global user.name "NAME HERE"\''
179
162
stderr . puts ''
180
163
return false
181
164
end
182
165
183
- if ! output . include? 'user.email'
166
+ unless output . include? 'user.email'
184
167
stderr . puts '[-] ERROR: user.email is not set in your global git configuration'
185
168
stderr . puts '[-] Set it by running: \'git config --global user.email "[email protected] "\''
186
169
stderr . puts ''
187
170
return false
188
171
end
189
172
190
173
true
191
-
192
174
end
193
175
194
176
def update_git!
195
177
####### Since we're Git, do it all that way #######
196
178
stdout . puts "[*] Checking for updates via git"
197
179
stdout . puts "[*] Note: Updating from bleeding edge"
198
180
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)}
200
183
201
184
remote = @git_remote || "upstream"
202
185
branch = @git_branch || "master"
@@ -214,9 +197,8 @@ class Msfupdate
214
197
215
198
# Checks user.name and user.email
216
199
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
+
220
202
# We shouldn't get here if the globals dont check out
221
203
committed = system ( "git" , "diff" , "--quiet" , "HEAD" )
222
204
if committed . nil?
@@ -226,7 +208,7 @@ class Msfupdate
226
208
stderr . puts "[-] /usr/local/bin instead of running this file directly (e.g.: ./msfupdate)"
227
209
stderr . puts "[-] to ensure a proper environment."
228
210
maybe_wait_and_exit 1
229
- elsif not committed
211
+ elsif ! committed
230
212
system ( "git" , "stash" )
231
213
stdout . puts "[*] Stashed local changes to avoid merge conflicts."
232
214
stdout . puts "[*] Run `git stash pop` to reapply local changes."
@@ -256,7 +238,7 @@ class Msfupdate
256
238
product_key = File . expand_path ( File . join ( @msfbase_dir , ".." , "engine" , "license" , "product.key" ) )
257
239
if File . exist? product_key
258
240
if File . readable? product_key
259
- if ( @offline_file )
241
+ if @offline_file
260
242
system ( "ruby" , update_script , @offline_file )
261
243
else
262
244
system ( "ruby" , update_script )
@@ -288,19 +270,13 @@ class Msfupdate
288
270
stdout . puts "[*] Note: expect weekly(ish) updates using this method"
289
271
system ( "apt-get" , "-qq" , "update" )
290
272
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' )
294
274
295
- if packages . empty ?
275
+ if framework_version . blank ?
296
276
stdout . puts "[*] No updates available"
297
277
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" )
304
280
end
305
281
end
306
282
@@ -315,23 +291,21 @@ class Msfupdate
315
291
316
292
# This only exits if you actually pass a wait option, otherwise
317
293
# 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 )
319
295
if @actually_wait
320
296
stdout . puts ""
321
297
stdout . puts "[*] Please hit enter to exit"
322
298
stdout . puts ""
323
299
stdin . readline
324
- exit exit_code
325
- else
326
- exit exit_code
327
300
end
301
+ exit exit_code
328
302
end
329
303
330
304
def apt_upgrade_available ( package )
331
305
require 'open3'
332
306
installed = nil
333
307
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 |
335
309
stdout . each do |line |
336
310
installed = $1 if line =~ /Installed: ([\w \- +.:~]+)$/
337
311
upgrade = $1 if line =~ /Candidate: ([\w \- +.:~]+)$/
0 commit comments