metasploit-framework/msfupdate

133 lines
3.7 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env ruby
# -*- coding: binary -*-
# $Id$
# $Revision$
msfbase = __FILE__
2012-10-03 22:06:38 +00:00
while File.symlink?(msfbase)
msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
end
2012-11-05 17:48:09 +00:00
@msfbase_dir = File.dirname(msfbase)
@args = ARGV.dup
2012-10-09 21:56:22 +00:00
# May be changed
@configdir = File.expand_path(File.join(File.dirname(msfbase), "data", "svn"))
2012-11-05 17:48:09 +00:00
Dir.chdir(@msfbase_dir)
$stderr.puts "[*]"
$stderr.puts "[*] Attempting to update the Metasploit Framework..."
$stderr.puts "[*]"
$stderr.puts ""
if not (Process.uid == 0 or File.stat(msfbase).owned?)
$stderr.puts "[-] ERROR: User running msfupdate does not own the metasploit install"
$stderr.puts "Please run msfupdate as the same user who installed metasploit."
end
2012-11-05 17:48:09 +00:00
def is_git
File.directory?(File.join(@msfbase_dir, ".git"))
end
2012-11-05 17:48:09 +00:00
def is_svn
File.directory?(File.join(@msfbase_dir, ".svn"))
end
2012-11-05 18:13:10 +00:00
def print_deprecation_warning
$stdout.puts "[*] Deprecation Note: The next version of Metasploit will"
$stdout.puts "[*] update over the git protocol, which requires outbound"
$stdout.puts "[*] access to github.com:9418/TCP."
$stdout.puts "[*] Please adjust your egress firewall rules accordingly."
end
# Some of these args are meaningful for SVN, some for Git,
# some for both. Fun times.
2012-10-01 17:41:36 +00:00
@args.each_with_index do |arg,i|
2012-10-01 18:07:51 +00:00
case arg
# Handle the old wait/nowait argument behavior
when "wait", "nowait"
@wait_index = i
@actually_wait = (arg == "wait")
# An empty or absent config-dir means a default config-dir
when "--config-dir"
@configdir_index = i
# A defined config dir means a defined config-dir
when /--config-dir=(.*)?/
2012-10-01 18:07:51 +00:00
# Spaces in the directory should be fine since this whole thing is passed
# as a single argument via the multi-arg syntax for system() below.
@configdir = $1
@configdir_index = i
2012-11-05 17:56:05 +00:00
when /--git-remote=([^\s]*)?/
@git_remote = $1
@git_remote_index = i
when /--git-branch=([^\s]*)?/
@git_branch = $1
@git_branch_index = i
2012-10-01 17:41:36 +00:00
end
end
@args[@wait_index] = nil if @wait_index
@args[@configdir_index] = nil if @configdir_index
2012-11-05 17:56:05 +00:00
@args[@git_remote_index] = nil if @git_remote_index
@args[@git_branch_index] = nil if @git_branch_index
@args = @args.compact
####### Since we're SVN, do it all this way #######
if is_svn
2012-11-05 18:13:10 +00:00
print_deprecation_warning
@args.push("--config-dir=#{@configdir}")
@args.push("--non-interactive")
res = system("svn", "cleanup")
if res.nil?
$stderr.puts "[-] ERROR: Failed to run svn"
$stderr.puts ""
$stderr.puts "[-] If you used a binary installer, make sure you run the symlink in"
$stderr.puts "[-] /usr/local/bin instead of running this file directly (e.g.: ./msfupdate)"
$stderr.puts "[-] to ensure a proper environment."
exit 1
else
# Cleanup worked, go ahead and update
system("svn", "update", *@args)
end
end
####### Since we're Git, do it all that way #######
if is_git
2012-11-05 17:56:05 +00:00
remote = @git_remote || "origin"
branch = @git_branch || "master"
# Always lose any local changes, but not unchecked files
2012-11-05 17:56:05 +00:00
# This is actually safer than the old svn way of msfupdate
# which would happily just generate conflicts over changes.
# TODO: Allow for git stash and git stash pop
res = system("git", "reset", "HEAD", "--hard")
if res.nil?
$stderr.puts "[-] ERROR: Failed to run git"
$stderr.puts ""
$stderr.puts "[-] If you used a binary installer, make sure you run the symlink in"
$stderr.puts "[-] /usr/local/bin instead of running this file directly (e.g.: ./msfupdate)"
$stderr.puts "[-] to ensure a proper environment."
exit 1
end
2012-11-05 17:56:05 +00:00
system("git", "checkout", branch)
system("git", "fetch")
2012-11-05 17:56:05 +00:00
system("git", "merge", "#{remote}/#{branch}")
end
unless is_svn || is_git
2012-11-05 17:48:09 +00:00
raise RuntimeError, "Cannot determine checkout type: `#{@msfbase_dir}'"
end
if @actually_wait
$stderr.puts ""
$stderr.puts "[*] Please hit enter to exit"
$stderr.puts ""
$stdin.readline
end