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
|
|
|
|
|
|
|
|
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
|
2006-04-02 22:33:34 +00:00
|
|
|
elog("DB is not enabled due to load error: #{e.to_s}")
|
2006-03-21 04:37:48 +00:00
|
|
|
end
|
|
|
|
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
|
|
|
|
elog("DB.connect threw an exception: #{e.to_s}")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
@active = true
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Disconnects a database session
|
|
|
|
#
|
|
|
|
def disconnect
|
|
|
|
begin
|
|
|
|
ActiveRecord::Base.remove_connection
|
|
|
|
rescue ::Exception => e
|
|
|
|
elog("DB.disconnect threw an exception: #{e.to_s}")
|
|
|
|
end
|
|
|
|
@active = false
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2008-11-09 21:54:04 +00:00
|
|
|
end
|