Files
metasploit-gs/lib/msfdb_helpers/standalone.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

83 lines
2.4 KiB
Ruby
Raw Normal View History

2021-04-26 02:24:11 +01:00
require 'msfdb_helpers/db_interface'
2021-04-30 14:24:56 +01:00
module MsfdbHelpers
class Standalone < DbInterface
2021-04-26 02:24:11 +01:00
2021-04-30 14:24:56 +01:00
def initialize(options:, db_conf:, connection_string:)
@options = options
@db_conf = db_conf
2021-05-05 14:43:12 +01:00
begin
@conn = PG.connect(connection_string)
rescue PG::ConnectionBad
print_error('Could not connect to standalone PostgreSQL instance. Ensure that the connection string is valid, and that the database is accessible')
raise
end
2021-04-30 14:24:56 +01:00
conninfo = @conn.conninfo_hash
@options[:db_port] = conninfo[:port]
@options[:db_host] = conninfo[:host]
super(options)
end
2021-04-26 02:24:11 +01:00
2021-04-30 14:24:56 +01:00
def init(msf_pass, msftest_pass)
create_db_users(msf_pass, msftest_pass)
2021-04-30 14:24:56 +01:00
end
2021-04-26 02:24:11 +01:00
2021-04-30 14:24:56 +01:00
def delete
@conn.exec("DROP DATABASE IF EXISTS #{@options[:msf_db_name]};")
@conn.exec("DROP DATABASE IF EXISTS #{@options[:msftest_db_name]};")
@conn.exec("DROP USER IF EXISTS #{@options[:msf_db_user]};")
@conn.exec("DROP USER IF EXISTS #{@options[:msftest_db_user]};")
FileUtils.rm_r(@db_conf, force: true)
2021-04-26 02:24:11 +01:00
end
2021-04-30 14:24:56 +01:00
def start
true
2021-04-30 14:24:56 +01:00
end
2021-04-26 02:24:11 +01:00
2021-04-30 14:24:56 +01:00
def stop
puts 'A standalone database cannot be stopped by msfdb'
false
2021-04-30 14:24:56 +01:00
end
2021-04-26 02:24:11 +01:00
2021-04-30 14:24:56 +01:00
def restart
raise NotImplementedError
end
2021-04-26 02:24:11 +01:00
def exists?
!@conn.nil?
end
2021-04-30 14:24:56 +01:00
def status
# Search for the database name
is_initialized = @conn.exec_params('select * from pg_catalog.pg_database where datname = $1', [@options[:msf_db_name]]).any?
if !is_initialized
DatabaseStatus::NEEDS_INIT
else
DatabaseStatus::RUNNING
end
2021-04-30 14:24:56 +01:00
end
2021-04-26 02:24:11 +01:00
2021-04-30 14:24:56 +01:00
def write_db_client_auth_config
raise NotImplementedError
end
2021-04-26 02:24:11 +01:00
2021-04-30 14:24:56 +01:00
def self.requirements
2021-05-05 14:43:12 +01:00
[]
2021-04-30 14:24:56 +01:00
end
2021-04-26 02:24:11 +01:00
private
def create_db_users(msf_pass, msftest_pass)
@conn.exec("create user #{@options[:msf_db_user]} with password '#{msf_pass}'")
@conn.exec("create user #{@options[:msftest_db_user]} with password '#{msftest_pass}'")
@conn.exec("alter role #{@options[:msf_db_user]} createdb")
@conn.exec("alter role #{@options[:msftest_db_user]} createdb")
@conn.exec("alter role #{@options[:msf_db_user]} with password '#{msf_pass}'")
@conn.exec("alter role #{@options[:msftest_db_user]} with password '#{msftest_pass}'")
@conn.exec("CREATE DATABASE #{@options[:msf_db_name]}")
@conn.exec("CREATE DATABASE #{@options[:msftest_db_name]}")
@conn.finish
end
2021-04-26 02:24:11 +01:00
end
end