CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/msfdb_helpers/db_interface.rb
Views: 11766
1
module MsfdbHelpers
2
class DbInterface
3
4
def initialize(options)
5
@options = options
6
end
7
8
def init
9
raise NotImplementedError
10
end
11
12
def delete
13
raise NotImplementedError
14
end
15
16
def start
17
raise NotImplementedError
18
end
19
20
def stop
21
raise NotImplementedError
22
end
23
24
def restart
25
raise NotImplementedError
26
end
27
28
def status
29
raise NotImplementedError
30
end
31
32
def write_db_client_auth_config(client_auth_config)
33
puts "Writing client authentication configuration file #{client_auth_config}"
34
File.open(client_auth_config, 'w') do |f|
35
f.puts "host \"#{@options[:msf_db_name]}\" \"#{@options[:msf_db_user]}\" 127.0.0.1/32 md5"
36
f.puts "host \"#{@options[:msftest_db_name]}\" \"#{@options[:msftest_db_user]}\" 127.0.0.1/32 md5"
37
f.puts "host \"postgres\" \"#{@options[:msftest_db_user]}\" 127.0.0.1/32 md5"
38
f.puts 'host "template1" all 127.0.0.1/32 trust'
39
if Gem.win_platform?
40
f.puts 'host all all 127.0.0.1/32 trust'
41
f.puts 'host all all ::1/128 trust'
42
else
43
f.puts 'local all all trust'
44
end
45
end
46
end
47
48
def self.requirements
49
[]
50
end
51
52
def run_cmd(cmd, input: nil, env: {})
53
puts "run_cmd: cmd=#{cmd}, input=#{input}, env=#{env}" if @options[:debug]
54
55
output, status = Open3.capture2e(env, cmd)
56
if @options[:debug]
57
puts "'#{cmd}' returned #{status.exitstatus}"
58
puts output
59
end
60
status.exitstatus
61
end
62
63
def run_psql(cmd, socket_directory= "#{Dir.tmpdir}", db_name: 'postgres')
64
if @options[:debug]
65
puts "psql -h #{socket_directory} -p #{@options[:db_port]} -c \"#{cmd};\" #{db_name}"
66
end
67
68
run_cmd("psql -h #{socket_directory} -p #{@options[:db_port]} -c \"#{cmd};\" #{db_name}")
69
end
70
71
end
72
end
73
74