CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/msfdb_helpers/standalone.rb
Views: 1904
1
require 'msfdb_helpers/db_interface'
2
module MsfdbHelpers
3
class Standalone < DbInterface
4
5
def initialize(options:, db_conf:, connection_string:)
6
@options = options
7
@db_conf = db_conf
8
begin
9
@conn = PG.connect(connection_string)
10
rescue PG::ConnectionBad
11
print_error('Could not connect to standalone PostgreSQL instance. Ensure that the connection string is valid, and that the database is accessible')
12
raise
13
end
14
15
conninfo = @conn.conninfo_hash
16
@options[:db_port] = conninfo[:port]
17
@options[:db_host] = conninfo[:host]
18
super(options)
19
end
20
21
def init(msf_pass, msftest_pass)
22
create_db_users(msf_pass, msftest_pass)
23
end
24
25
def delete
26
@conn.exec("DROP DATABASE IF EXISTS #{@options[:msf_db_name]};")
27
@conn.exec("DROP DATABASE IF EXISTS #{@options[:msftest_db_name]};")
28
@conn.exec("DROP USER IF EXISTS #{@options[:msf_db_user]};")
29
@conn.exec("DROP USER IF EXISTS #{@options[:msftest_db_user]};")
30
FileUtils.rm_r(@db_conf, force: true)
31
end
32
33
def start
34
true
35
end
36
37
def stop
38
puts 'A standalone database cannot be stopped by msfdb'
39
false
40
end
41
42
def restart
43
raise NotImplementedError
44
end
45
46
def exists?
47
!@conn.nil?
48
end
49
50
def status
51
# Search for the database name
52
is_initialized = @conn.exec_params('select * from pg_catalog.pg_database where datname = $1', [@options[:msf_db_name]]).any?
53
if !is_initialized
54
DatabaseStatus::NEEDS_INIT
55
else
56
DatabaseStatus::RUNNING
57
end
58
end
59
60
def write_db_client_auth_config
61
raise NotImplementedError
62
end
63
64
def self.requirements
65
[]
66
end
67
68
private
69
70
def create_db_users(msf_pass, msftest_pass)
71
@conn.exec("create user #{@options[:msf_db_user]} with password '#{msf_pass}'")
72
@conn.exec("create user #{@options[:msftest_db_user]} with password '#{msftest_pass}'")
73
@conn.exec("alter role #{@options[:msf_db_user]} createdb")
74
@conn.exec("alter role #{@options[:msftest_db_user]} createdb")
75
@conn.exec("alter role #{@options[:msf_db_user]} with password '#{msf_pass}'")
76
@conn.exec("alter role #{@options[:msftest_db_user]} with password '#{msftest_pass}'")
77
@conn.exec("CREATE DATABASE #{@options[:msf_db_name]}")
78
@conn.exec("CREATE DATABASE #{@options[:msftest_db_name]}")
79
@conn.finish
80
end
81
end
82
end
83
84