2006-03-21 04:37:48 +00:00
|
|
|
require 'msf/core'
|
|
|
|
require 'msf/core/db'
|
2009-12-28 00:21:21 +00:00
|
|
|
require 'msf/core/task_manager'
|
2006-03-21 04:37:48 +00:00
|
|
|
|
|
|
|
module Msf
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
2009-10-25 17:18:23 +00:00
|
|
|
# The db module provides persistent storage and events. This class should be instantiated LAST
|
|
|
|
# as the active_suppport library overrides Kernel.require, slowing down all future code loads.
|
2006-03-21 04:37:48 +00:00
|
|
|
#
|
|
|
|
###
|
|
|
|
|
|
|
|
class DBManager
|
|
|
|
|
|
|
|
# Provides :framework and other accessors
|
|
|
|
include Framework::Offspring
|
2009-11-14 21:41:38 +00:00
|
|
|
|
2006-03-21 04:37:48 +00:00
|
|
|
# Returns true if we are ready to load/store data
|
2010-05-14 18:57:48 +00:00
|
|
|
def active
|
2010-08-05 02:24:40 +00:00
|
|
|
return false if not @usable
|
2010-05-14 18:57:48 +00:00
|
|
|
(ActiveRecord::Base.connected? && ActiveRecord::Base.connection.active?)
|
|
|
|
end
|
2009-11-14 21:41:38 +00:00
|
|
|
|
2006-03-21 04:37:48 +00:00
|
|
|
# Returns true if the prerequisites have been installed
|
|
|
|
attr_accessor :usable
|
2009-11-14 21:41:38 +00:00
|
|
|
|
2009-03-28 20:45:46 +00:00
|
|
|
# Returns the list of usable database drivers
|
|
|
|
attr_accessor :drivers
|
2009-11-14 21:41:38 +00:00
|
|
|
|
2009-03-28 21:42:30 +00:00
|
|
|
# Returns the active driver
|
|
|
|
attr_accessor :driver
|
2009-11-14 21:41:38 +00:00
|
|
|
|
2009-06-03 16:28:28 +00:00
|
|
|
# Stores the error message for why the db was not loaded
|
|
|
|
attr_accessor :error
|
2009-11-14 21:41:38 +00:00
|
|
|
|
2009-12-28 00:21:21 +00:00
|
|
|
# Stores a TaskManager for serializing database events
|
|
|
|
attr_accessor :sink
|
|
|
|
|
2010-08-05 02:24:40 +00:00
|
|
|
def initialize(framework, opts = {})
|
2009-11-14 21:41:38 +00:00
|
|
|
|
2006-03-21 04:37:48 +00:00
|
|
|
self.framework = framework
|
|
|
|
@usable = false
|
2009-11-14 21:41:38 +00:00
|
|
|
|
2011-01-25 22:58:21 +00:00
|
|
|
# Don't load the database if the user said they didn't need it.
|
2010-08-05 02:24:40 +00:00
|
|
|
if (opts['DisableDatabase'])
|
2011-01-25 22:58:21 +00:00
|
|
|
self.error = "disabled"
|
2010-08-05 02:24:40 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
initialize_database_support
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Do what is necessary to load our database support
|
|
|
|
#
|
|
|
|
def initialize_database_support
|
2009-11-14 21:41:38 +00:00
|
|
|
|
2007-02-11 23:47:34 +00:00
|
|
|
# Load ActiveRecord if it is available
|
2009-11-14 21:41:38 +00:00
|
|
|
begin
|
2007-02-11 23:47:34 +00:00
|
|
|
require 'rubygems'
|
|
|
|
require 'active_record'
|
2006-03-21 04:37:48 +00:00
|
|
|
require 'msf/core/db_objects'
|
2009-12-14 22:52:34 +00:00
|
|
|
require 'msf/core/model'
|
2010-03-20 21:40:59 +00:00
|
|
|
|
|
|
|
# Database drivers can reset our KCODE, do not let them
|
2010-03-22 12:34:06 +00:00
|
|
|
$KCODE = 'NONE' if RUBY_VERSION =~ /^1\.8\./
|
2010-03-20 21:40:59 +00:00
|
|
|
|
2007-02-11 23:47:34 +00:00
|
|
|
@usable = true
|
2009-11-14 21:41:38 +00:00
|
|
|
|
2006-03-21 04:37:48 +00:00
|
|
|
rescue ::Exception => e
|
2009-06-03 16:28:28 +00:00
|
|
|
self.error = e
|
2008-12-19 07:11:08 +00:00
|
|
|
elog("DB is not enabled due to load error: #{e}")
|
2010-08-05 02:24:40 +00:00
|
|
|
return false
|
2006-03-21 04:37:48 +00:00
|
|
|
end
|
2009-11-14 21:41:38 +00:00
|
|
|
|
2009-03-28 20:45:46 +00:00
|
|
|
#
|
|
|
|
# Determine what drivers are available
|
|
|
|
#
|
|
|
|
initialize_drivers
|
2009-12-28 00:21:21 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Instantiate the database sink
|
|
|
|
#
|
|
|
|
initialize_sink
|
2010-08-05 02:24:40 +00:00
|
|
|
|
|
|
|
true
|
2009-03-28 20:45:46 +00:00
|
|
|
end
|
2009-11-14 21:41:38 +00:00
|
|
|
|
|
|
|
#
|
2009-12-28 00:21:21 +00:00
|
|
|
# Scan through available drivers
|
2009-03-28 20:45:46 +00:00
|
|
|
#
|
|
|
|
def initialize_drivers
|
|
|
|
self.drivers = []
|
2010-05-20 19:37:46 +00:00
|
|
|
tdrivers = %W{ postgresql mysql sqlite3 }
|
2009-03-28 20:45:46 +00:00
|
|
|
tdrivers.each do |driver|
|
|
|
|
begin
|
2010-03-29 17:04:40 +00:00
|
|
|
ActiveRecord::Base.default_timezone = :utc
|
2009-03-28 20:45:46 +00:00
|
|
|
ActiveRecord::Base.establish_connection(:adapter => driver)
|
2009-11-14 21:41:38 +00:00
|
|
|
if(self.respond_to?("driver_check_#{driver}"))
|
|
|
|
self.send("driver_check_#{driver}")
|
|
|
|
end
|
2009-03-28 20:45:46 +00:00
|
|
|
ActiveRecord::Base.remove_connection
|
|
|
|
self.drivers << driver
|
|
|
|
rescue ::Exception
|
|
|
|
end
|
|
|
|
end
|
2009-11-14 21:41:38 +00:00
|
|
|
|
2009-03-28 21:42:30 +00:00
|
|
|
if(not self.drivers.empty?)
|
|
|
|
self.driver = self.drivers[0]
|
|
|
|
end
|
2010-03-20 21:40:59 +00:00
|
|
|
|
|
|
|
# Database drivers can reset our KCODE, do not let them
|
2010-03-22 12:34:06 +00:00
|
|
|
$KCODE = 'NONE' if RUBY_VERSION =~ /^1\.8\./
|
2006-03-21 04:37:48 +00:00
|
|
|
end
|
2009-11-14 21:41:38 +00:00
|
|
|
|
2009-12-28 00:21:21 +00:00
|
|
|
#
|
|
|
|
# Create a new database sink and initialize it
|
|
|
|
#
|
|
|
|
def initialize_sink
|
|
|
|
self.sink = TaskManager.new(framework)
|
|
|
|
self.sink.start
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Add a new task to the sink
|
|
|
|
#
|
|
|
|
def queue(proc)
|
|
|
|
self.sink.queue_proc(proc)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2009-11-14 21:41:38 +00:00
|
|
|
# Verify that sqlite3 is ready
|
|
|
|
def driver_check_sqlite3
|
|
|
|
require 'sqlite3'
|
|
|
|
end
|
|
|
|
|
|
|
|
# Verify that mysql is ready
|
|
|
|
def driver_check_mysql
|
|
|
|
require 'mysql'
|
|
|
|
end
|
|
|
|
|
2006-03-21 04:37:48 +00:00
|
|
|
#
|
|
|
|
# Connects this instance to a database
|
|
|
|
#
|
|
|
|
def connect(opts={})
|
2007-12-31 04:05:51 +00:00
|
|
|
|
2006-03-21 04:37:48 +00:00
|
|
|
return false if not @usable
|
2009-11-14 21:41:38 +00:00
|
|
|
|
2007-07-17 18:42:26 +00:00
|
|
|
nopts = opts.dup
|
|
|
|
if (nopts['port'])
|
|
|
|
nopts['port'] = nopts['port'].to_i
|
|
|
|
end
|
2009-11-14 21:41:38 +00:00
|
|
|
|
2010-10-04 07:11:07 +00:00
|
|
|
nopts['pool'] = 75
|
2010-03-04 07:10:20 +00:00
|
|
|
|
2006-03-21 04:37:48 +00:00
|
|
|
begin
|
2010-05-10 06:14:57 +00:00
|
|
|
create_db(nopts)
|
|
|
|
|
2009-11-14 21:41:38 +00:00
|
|
|
# Configure the database adapter
|
2007-07-17 19:01:04 +00:00
|
|
|
ActiveRecord::Base.establish_connection(nopts)
|
2009-12-14 22:52:34 +00:00
|
|
|
|
|
|
|
# Migrate the database, if needed
|
|
|
|
migrate
|
|
|
|
|
|
|
|
# Set the default workspace
|
|
|
|
framework.db.workspace = framework.db.default_workspace
|
2006-03-21 04:37:48 +00:00
|
|
|
rescue ::Exception => e
|
2009-11-14 21:41:38 +00:00
|
|
|
self.error = e
|
2008-12-19 07:11:08 +00:00
|
|
|
elog("DB.connect threw an exception: #{e}")
|
2009-12-29 23:48:45 +00:00
|
|
|
dlog("Call stack: #{$@.join"\n"}", LEV_1)
|
2006-03-21 04:37:48 +00:00
|
|
|
return false
|
2010-03-20 21:40:59 +00:00
|
|
|
ensure
|
|
|
|
# Database drivers can reset our KCODE, do not let them
|
2010-03-22 12:34:06 +00:00
|
|
|
$KCODE = 'NONE' if RUBY_VERSION =~ /^1\.8\./
|
2006-03-21 04:37:48 +00:00
|
|
|
end
|
2009-11-14 21:41:38 +00:00
|
|
|
|
2006-03-21 04:37:48 +00:00
|
|
|
end
|
2009-11-14 21:41:38 +00:00
|
|
|
|
2010-05-10 06:14:57 +00:00
|
|
|
#
|
|
|
|
# Attempt to create the database
|
|
|
|
#
|
|
|
|
# If the database already exists this will fail and we will continue on our
|
|
|
|
# merry way, connecting anyway. If it doesn't, we try to create it. If
|
|
|
|
# that fails, then it wasn't meant to be and the connect will raise a
|
|
|
|
# useful exception so the user won't be in the dark; no need to raise
|
|
|
|
# anything at all here.
|
|
|
|
#
|
|
|
|
def create_db(opts)
|
|
|
|
begin
|
|
|
|
case opts["adapter"]
|
|
|
|
when 'sqlite3'
|
|
|
|
# Sqlite just needs the file to be writable. ActiveRecord creates
|
|
|
|
# it if it doesn't exist and checks permissions if it does. This
|
|
|
|
# all happens during establish_connection(), so we don't need to
|
|
|
|
# bother with creating anything here.
|
|
|
|
when 'postgresql','mysql'
|
2010-10-11 23:48:53 +00:00
|
|
|
# Try to force a connection to be made to the database, if it succeeds
|
|
|
|
# then we know we don't need to create it :)
|
|
|
|
ActiveRecord::Base.establish_connection(opts)
|
|
|
|
conn = ActiveRecord::Base.connection
|
|
|
|
end
|
|
|
|
rescue ::Exception => e
|
|
|
|
errstr = e.to_s
|
|
|
|
if errstr =~ /does not exist/i or errstr =~ /Unknown database/
|
|
|
|
ilog("Database doesn't exist \"#{opts['database']}\", attempting to create it.")
|
2010-05-10 06:14:57 +00:00
|
|
|
ActiveRecord::Base.establish_connection(opts.merge('database' => nil))
|
|
|
|
ActiveRecord::Base.connection.create_database(opts['database'])
|
2010-10-11 23:48:53 +00:00
|
|
|
else
|
|
|
|
ilog("Trying to continue despite failed database creation: #{e}")
|
2010-05-10 06:14:57 +00:00
|
|
|
end
|
|
|
|
end
|
2010-10-11 23:48:53 +00:00
|
|
|
ActiveRecord::Base.remove_connection
|
2010-05-10 06:14:57 +00:00
|
|
|
end
|
|
|
|
|
2006-03-21 04:37:48 +00:00
|
|
|
#
|
|
|
|
# Disconnects a database session
|
|
|
|
#
|
|
|
|
def disconnect
|
|
|
|
begin
|
|
|
|
ActiveRecord::Base.remove_connection
|
|
|
|
rescue ::Exception => e
|
2009-11-14 21:41:38 +00:00
|
|
|
self.error = e
|
2008-12-19 07:11:08 +00:00
|
|
|
elog("DB.disconnect threw an exception: #{e}")
|
2010-03-20 21:40:59 +00:00
|
|
|
ensure
|
|
|
|
# Database drivers can reset our KCODE, do not let them
|
2010-03-22 12:34:06 +00:00
|
|
|
$KCODE = 'NONE' if RUBY_VERSION =~ /^1\.8\./
|
2006-03-21 04:37:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-07 17:03:27 +00:00
|
|
|
#
|
|
|
|
# Migrate database to latest schema version
|
|
|
|
#
|
|
|
|
def migrate(verbose = false)
|
|
|
|
begin
|
|
|
|
migrate_dir = ::File.join(Msf::Config.install_root, "data", "sql", "migrate")
|
|
|
|
ActiveRecord::Migration.verbose = verbose
|
|
|
|
ActiveRecord::Migrator.migrate(migrate_dir, nil)
|
|
|
|
rescue ::Exception => e
|
|
|
|
self.error = e
|
|
|
|
elog("DB.migrate threw an exception: #{e}")
|
2010-04-11 16:33:21 +00:00
|
|
|
dlog("Call stack:\n#{e.backtrace.join "\n"}")
|
2009-12-07 17:03:27 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2009-12-23 20:37:30 +00:00
|
|
|
def workspace=(workspace)
|
|
|
|
@workspace_name = workspace.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def workspace
|
|
|
|
framework.db.find_workspace(@workspace_name)
|
|
|
|
end
|
|
|
|
|
2006-03-21 04:37:48 +00:00
|
|
|
end
|
2008-11-09 21:54:04 +00:00
|
|
|
end
|
2009-11-14 21:41:38 +00:00
|
|
|
|