2006-03-21 04:37:48 +00:00
|
|
|
require 'msf/core'
|
|
|
|
require 'msf/core/db'
|
|
|
|
|
|
|
|
module Msf
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# The db module provides persistent storage and events
|
|
|
|
#
|
|
|
|
###
|
|
|
|
|
|
|
|
class DBManager
|
|
|
|
|
|
|
|
# Provides :framework and other accessors
|
|
|
|
include Framework::Offspring
|
|
|
|
|
|
|
|
# Returns true if we are ready to load/store data
|
|
|
|
attr_accessor :active
|
|
|
|
|
|
|
|
# Returns true if the prerequisites have been installed
|
|
|
|
attr_accessor :usable
|
|
|
|
|
2009-03-28 20:45:46 +00:00
|
|
|
# Returns the list of usable database drivers
|
|
|
|
attr_accessor :drivers
|
|
|
|
|
2009-03-28 21:42:30 +00:00
|
|
|
# Returns the active driver
|
|
|
|
attr_accessor :driver
|
|
|
|
|
2009-06-03 16:28:28 +00:00
|
|
|
# Stores the error message for why the db was not loaded
|
|
|
|
attr_accessor :error
|
|
|
|
|
2006-03-21 04:37:48 +00:00
|
|
|
def initialize(framework)
|
|
|
|
|
|
|
|
self.framework = framework
|
|
|
|
@usable = false
|
|
|
|
@active = false
|
|
|
|
|
2008-11-09 21:54:04 +00:00
|
|
|
#
|
|
|
|
# Prefer our local copy of active_record
|
|
|
|
#
|
|
|
|
dir_ar = File.join(Msf::Config.data_directory, 'msfweb', 'vendor', 'rails', 'activerecord', 'lib')
|
|
|
|
if(File.directory?(dir_ar) and not $:.include?(dir_ar))
|
|
|
|
$:.unshift(dir_ar)
|
|
|
|
end
|
|
|
|
|
2007-02-11 23:47:34 +00:00
|
|
|
# Load ActiveRecord if it is available
|
2006-04-02 22:33:34 +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'
|
2007-02-11 23:47:34 +00:00
|
|
|
@usable = true
|
|
|
|
|
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}")
|
2009-06-03 16:28:28 +00:00
|
|
|
return
|
2006-03-21 04:37:48 +00:00
|
|
|
end
|
2009-03-28 20:45:46 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Determine what drivers are available
|
|
|
|
#
|
|
|
|
initialize_drivers
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
def initialize_drivers
|
|
|
|
self.drivers = []
|
2009-03-28 21:42:30 +00:00
|
|
|
tdrivers = %W{ sqlite3 mysql postgresql }
|
2009-03-28 20:45:46 +00:00
|
|
|
tdrivers.each do |driver|
|
|
|
|
begin
|
|
|
|
ActiveRecord::Base.establish_connection(:adapter => driver)
|
|
|
|
ActiveRecord::Base.remove_connection
|
|
|
|
self.drivers << driver
|
|
|
|
rescue ::Exception
|
|
|
|
end
|
|
|
|
end
|
2009-03-28 21:42:30 +00:00
|
|
|
|
|
|
|
if(not self.drivers.empty?)
|
|
|
|
self.driver = self.drivers[0]
|
|
|
|
end
|
2006-03-21 04:37:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# 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
|
2007-07-17 18:42:26 +00:00
|
|
|
|
|
|
|
nopts = opts.dup
|
|
|
|
if (nopts['port'])
|
|
|
|
nopts['port'] = nopts['port'].to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2006-03-21 04:37:48 +00:00
|
|
|
begin
|
2007-07-17 19:01:04 +00:00
|
|
|
ActiveRecord::Base.establish_connection(nopts)
|
2006-03-21 04:37:48 +00:00
|
|
|
rescue ::Exception => e
|
2008-12-19 07:11:08 +00:00
|
|
|
elog("DB.connect threw an exception: #{e}")
|
2006-03-21 04:37:48 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
@active = true
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Disconnects a database session
|
|
|
|
#
|
|
|
|
def disconnect
|
|
|
|
begin
|
|
|
|
ActiveRecord::Base.remove_connection
|
|
|
|
rescue ::Exception => e
|
2008-12-19 07:11:08 +00:00
|
|
|
elog("DB.disconnect threw an exception: #{e}")
|
2006-03-21 04:37:48 +00:00
|
|
|
end
|
|
|
|
@active = false
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2008-11-09 21:54:04 +00:00
|
|
|
end
|