Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix msfdb init command failure in systems that use the 'pg_ctl.rb' msfdb_helper #16094

Merged
merged 9 commits into from
Jul 27, 2022
6 changes: 3 additions & 3 deletions lib/msfdb_helpers/db_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ def run_cmd(cmd, input: nil, env: {})
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
Expand Down
55 changes: 49 additions & 6 deletions lib/msfdb_helpers/pg_ctl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def initialize(db_path:, options:, localconf:, db_conf:)
@options = options
@localconf = localconf
@db_conf = db_conf
@socket_directory = db_path
super(options)
end

Expand All @@ -20,6 +21,17 @@ def init(msf_pass, msftest_pass)
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)
Expand All @@ -28,6 +40,37 @@ def init(msf_pass, msftest_pass)
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
Expand Down Expand Up @@ -95,12 +138,12 @@ def status

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]}")
Expand Down
1 change: 1 addition & 0 deletions msfdb
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def init_db
Dir.chdir(@framework) do
@db_driver.run_cmd('bundle exec rake db:migrate')
end
puts 'Database initialization successful'.green.bold.to_s
end

def load_db_config
Expand Down