Land #16094, Fix msfdb init command failure in systems that use the 'pg_ctl.rb' msfdb helper
This commit is contained in:
commit
09ea05754c
|
@ -60,12 +60,12 @@ module MsfdbHelpers
|
|||
status.exitstatus
|
||||
end
|
||||
|
||||
def run_psql(cmd, db_name: 'postgres')
|
||||
def run_psql(cmd, socket_directory= "#{Dir.tmpdir}", db_name: 'postgres')
|
||||
if @options[:debug]
|
||||
puts "psql -p #{@options[:db_port]} -c \"#{cmd};\" #{db_name}"
|
||||
puts "psql -h #{socket_directory} -p #{@options[:db_port]} -c \"#{cmd};\" #{db_name}"
|
||||
end
|
||||
|
||||
run_cmd("psql -p #{@options[:db_port]} -c \"#{cmd};\" #{db_name}")
|
||||
run_cmd("psql -h #{socket_directory} -p #{@options[:db_port]} -c \"#{cmd};\" #{db_name}")
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -8,6 +8,7 @@ module MsfdbHelpers
|
|||
@options = options
|
||||
@localconf = localconf
|
||||
@db_conf = db_conf
|
||||
@socket_directory = db_path
|
||||
super(options)
|
||||
end
|
||||
|
||||
|
@ -20,6 +21,17 @@ module MsfdbHelpers
|
|||
f.puts "port = #{@options[:db_port]}"
|
||||
end
|
||||
|
||||
# Try creating a test file at {Dir.tmpdir},
|
||||
# Else fallback to creation at @{db}
|
||||
# Else fail with error.
|
||||
if test_executable_file("#{Dir.tmpdir}")
|
||||
@socket_directory = Dir.tmpdir
|
||||
elsif test_executable_file("#{@db}")
|
||||
@socket_directory = @db
|
||||
else
|
||||
print_error("Attempt to create DB socket file at Temporary Directory and `~/.msf4/db` failed. Possibly because they are mounted with NOEXEC flags. Database initialization failed.")
|
||||
end
|
||||
|
||||
start
|
||||
|
||||
create_db_users(msf_pass, msftest_pass)
|
||||
|
@ -28,6 +40,37 @@ module MsfdbHelpers
|
|||
restart
|
||||
end
|
||||
|
||||
# Creates and attempts to execute a testfile in the specified directory,
|
||||
# to determine if it is mounted with NOEXEC flags.
|
||||
def test_executable_file(path)
|
||||
begin
|
||||
file_name = File.join(path, 'msfdb_testfile')
|
||||
File.open(file_name, 'w') do |f|
|
||||
f.puts "#!/bin/bash\necho exec"
|
||||
end
|
||||
File.chmod(0744, file_name)
|
||||
|
||||
if run_cmd(file_name)
|
||||
File.open("#{@db}/postgresql.conf", 'a') do |f|
|
||||
f.puts "unix_socket_directories = \'#{path}\'"
|
||||
end
|
||||
puts "Creating db socket file at #{path}"
|
||||
end
|
||||
return true
|
||||
|
||||
rescue => e
|
||||
return false
|
||||
|
||||
ensure
|
||||
begin
|
||||
File.delete(file_name)
|
||||
rescue
|
||||
print_error("Unable to delete test file #{file_name}")
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def delete
|
||||
if exists?
|
||||
stop
|
||||
|
@ -95,12 +138,12 @@ module MsfdbHelpers
|
|||
|
||||
def create_db_users(msf_pass, msftest_pass)
|
||||
puts 'Creating database users'
|
||||
run_psql("create user #{@options[:msf_db_user].shellescape} with password '#{msf_pass}'")
|
||||
run_psql("create user #{@options[:msftest_db_user].shellescape} with password '#{msftest_pass}'")
|
||||
run_psql("alter role #{@options[:msf_db_user].shellescape} createdb")
|
||||
run_psql("alter role #{@options[:msftest_db_user].shellescape} createdb")
|
||||
run_psql("alter role #{@options[:msf_db_user].shellescape} with password '#{msf_pass}'")
|
||||
run_psql("alter role #{@options[:msftest_db_user].shellescape} with password '#{msftest_pass}'")
|
||||
run_psql("create user #{@options[:msf_db_user].shellescape} with password '#{msf_pass}'", @socket_directory)
|
||||
run_psql("create user #{@options[:msftest_db_user].shellescape} with password '#{msftest_pass}'", @socket_directory)
|
||||
run_psql("alter role #{@options[:msf_db_user].shellescape} createdb", @socket_directory)
|
||||
run_psql("alter role #{@options[:msftest_db_user].shellescape} createdb", @socket_directory)
|
||||
run_psql("alter role #{@options[:msf_db_user].shellescape} with password '#{msf_pass}'", @socket_directory)
|
||||
run_psql("alter role #{@options[:msftest_db_user].shellescape} with password '#{msftest_pass}'", @socket_directory)
|
||||
|
||||
conn = PG.connect(host: @options[:db_host], dbname: 'postgres', port: @options[:db_port], user: @options[:msf_db_user], password: msf_pass)
|
||||
conn.exec("CREATE DATABASE #{@options[:msf_db_name]}")
|
||||
|
|
Loading…
Reference in New Issue