Skip to content

Commit b7eca59

Browse files
committed
Allow for switching between git and svn
Depending on the kind of checkout, msfupdate should be smart about this. This was first proposed by @corelanc0d3r I believe, but rejected because it didn't seem to solve any immediate problem. Now, it does, as we are ditching SVN Real Soon Now for performance reasons. This change is minimal functionality and doesn't handle switching over from one to the other.
1 parent 85dd212 commit b7eca59

File tree

1 file changed

+64
-16
lines changed

1 file changed

+64
-16
lines changed

msfupdate

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ while File.symlink?(msfbase)
99
msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
1010
end
1111

12+
msfbase_dir = File.dirname(msfbase)
13+
1214
@args = ARGV.dup
1315

1416
# May be changed
1517
@configdir = File.expand_path(File.join(File.dirname(msfbase), "data", "svn"))
1618

17-
Dir.chdir(File.dirname(msfbase))
19+
Dir.chdir(msfbase_dir)
1820

1921
$stderr.puts "[*]"
2022
$stderr.puts "[*] Attempting to update the Metasploit Framework..."
@@ -26,16 +28,32 @@ if not (Process.uid == 0 or File.stat(msfbase).owned?)
2628
$stderr.puts "Please run msfupdate as the same user who installed metasploit."
2729
end
2830

31+
32+
def is_git(msfbase_dir)
33+
File.directory?(File.join(msfbase_dir, ".git"))
34+
end
35+
36+
def is_svn
37+
File.directory?(File.join(msfbase_dir, ".svn"))
38+
end
39+
40+
# TODO
41+
def print_depreciation_warning
42+
43+
end
44+
45+
# Some of these args are meaningful for SVN, some for Git,
46+
# some for both. Fun times.
2947
@args.each_with_index do |arg,i|
3048
case arg
31-
# Handle the old wait/nowait argument behavior
49+
# Handle the old wait/nowait argument behavior
3250
when "wait", "nowait"
3351
@wait_index = i
3452
@actually_wait = (arg == "wait")
35-
# An empty or absent config-dir means a default config-dir
53+
# An empty or absent config-dir means a default config-dir
3654
when "--config-dir"
3755
@configdir_index = i
38-
# A defined config dir means a defined config-dir
56+
# A defined config dir means a defined config-dir
3957
when /--config-dir=(.*)?/
4058
# Spaces in the directory should be fine since this whole thing is passed
4159
# as a single argument via the multi-arg syntax for system() below.
@@ -48,19 +66,49 @@ end
4866
@args[@wait_index] = nil if @wait_index
4967
@args[@configdir_index] = nil if @configdir_index
5068
@args = @args.compact
51-
@args.push("--config-dir=#{@configdir}")
52-
@args.push("--non-interactive")
5369

54-
res = system("svn", "cleanup")
55-
if res.nil?
56-
$stderr.puts "[-] ERROR: Failed to run svn"
57-
$stderr.puts ""
58-
$stderr.puts "[-] If you used a binary installer, make sure you run the symlink in"
59-
$stderr.puts "[-] /usr/local/bin instead of running this file directly (e.g.: ./msfupdate)"
60-
$stderr.puts "[-] to ensure a proper environment."
61-
else
62-
# Cleanup worked, go ahead and update
63-
system("svn", "update", *@args)
70+
####### Since we're SVN, do it all this way #######
71+
if is_svn
72+
print_depreciation_warning
73+
@args.push("--config-dir=#{@configdir}")
74+
@args.push("--non-interactive")
75+
76+
res = system("svn", "cleanup")
77+
if res.nil?
78+
$stderr.puts "[-] ERROR: Failed to run svn"
79+
$stderr.puts ""
80+
$stderr.puts "[-] If you used a binary installer, make sure you run the symlink in"
81+
$stderr.puts "[-] /usr/local/bin instead of running this file directly (e.g.: ./msfupdate)"
82+
$stderr.puts "[-] to ensure a proper environment."
83+
exit 1
84+
else
85+
# Cleanup worked, go ahead and update
86+
system("svn", "update", *@args)
87+
end
88+
end
89+
90+
####### Since we're Git, do it all that way #######
91+
if is_git
92+
# Always lose any local changes, but not unchecked files
93+
# TODO: Allow for git stash and git stash pop
94+
res = system("git", "reset", "HEAD", "--hard")
95+
if res.nil?
96+
$stderr.puts "[-] ERROR: Failed to run git"
97+
$stderr.puts ""
98+
$stderr.puts "[-] If you used a binary installer, make sure you run the symlink in"
99+
$stderr.puts "[-] /usr/local/bin instead of running this file directly (e.g.: ./msfupdate)"
100+
$stderr.puts "[-] to ensure a proper environment."
101+
exit 1
102+
end
103+
104+
# TODO: Allow msfupdate to take a branch argument
105+
system("git", "checkout", "master")
106+
system("git", "fetch")
107+
system("git", "merge", "origin/master")
108+
end
109+
110+
unless is_svn || is_git
111+
raise RuntimeError, "Cannot determine checkout type: `#{msfbase_dir}'"
64112
end
65113

66114
if @actually_wait

0 commit comments

Comments
 (0)