Enhance init handling of existing database config

GSoC/Meterpreter_Web_Console
Matthew Kienow 2018-08-07 17:37:01 -04:00
parent 9b93e0cfcd
commit d9eae8e113
No known key found for this signature in database
GPG Key ID: 40787F8B1EAC6E41
1 changed files with 69 additions and 31 deletions

100
msfdb
View File

@ -194,15 +194,77 @@ def init_db
return return
end end
if File.exist?(@db_conf) if File.exist?(@db_conf) &&
if !ask_yn("Found database config at #{@db_conf}, do you want to overwrite it?") !ask_yn("Found database config at #{@db_conf}, do you want to overwrite it?")
if !load_db_config
puts "Failed to load existing database config. Please reinit and overwrite the file."
return return
end end
else
write_db_config
end end
# Generate new database passwords create_db
msf_pass = pw_gen start_db
msftest_pass = pw_gen
puts 'Creating database users'
run_psql("create user #{@options[:msf_db_user]} with password '#{@msf_pass}'")
run_psql("create user #{@options[:msftest_db_user]} with password '#{@msftest_pass}'")
run_psql("alter role #{@options[:msf_db_user]} createdb")
run_psql("alter role #{@options[:msftest_db_user]} createdb")
run_psql("alter role #{@options[:msf_db_user]} with password '#{@msf_pass}'")
run_psql("alter role #{@options[:msftest_db_user]} with password '#{@msftest_pass}'")
run_cmd("createdb -p #{@options[:db_port]} -O #{@options[:msf_db_user]} -h 127.0.0.1 -U #{@options[:msf_db_user]} -E UTF-8 -T template0 #{@options[:msf_db_name]}",
"#{@msf_pass}\n#{@msf_pass}\n")
run_cmd("createdb -p #{@options[:db_port]} -O #{@options[:msftest_db_user]} -h 127.0.0.1 -U #{@options[:msftest_db_user]} -E UTF-8 -T template0 #{@options[:msftest_db_name]}",
"#{@msftest_pass}\n#{@msftest_pass}\n")
write_db_client_auth_config
restart_db
puts 'Creating initial database schema'
Dir.chdir(@framework) do
run_cmd('bundle exec rake db:migrate')
end
end
def load_db_config
if File.file?(@db_conf)
config = YAML.load(File.read(@db_conf))
production = config['production']
if production.nil?
puts "No production section found in database config #{@db_conf}."
return false
end
test = config['test']
if test.nil?
puts "No test section found in database config #{@db_conf}."
return false
end
# get values for development and production
@options[:msf_db_name] = production['database']
@options[:msf_db_user] = production['username']
@msf_pass = production['password']
@options[:db_port] = production['port']
@options[:db_pool] = production['pool']
# get values for test
@options[:msftest_db_name] = test['database']
@options[:msftest_db_user] = test['username']
@msftest_pass = test['password']
return true
end
return false
end
def write_db_config
# Generate new database passwords if not already assigned
@msf_pass ||= pw_gen
@msftest_pass ||= pw_gen
# Write a default database config file # Write a default database config file
Dir.mkdir(@localconf) unless File.directory?(@localconf) Dir.mkdir(@localconf) unless File.directory?(@localconf)
@ -212,7 +274,7 @@ def init_db
adapter: postgresql adapter: postgresql
database: #{@options[:msf_db_name]} database: #{@options[:msf_db_name]}
username: #{@options[:msf_db_user]} username: #{@options[:msf_db_user]}
password: #{msf_pass} password: #{@msf_pass}
host: 127.0.0.1 host: 127.0.0.1
port: #{@options[:db_port]} port: #{@options[:db_port]}
pool: #{@options[:db_pool]} pool: #{@options[:db_pool]}
@ -224,35 +286,11 @@ def init_db
<<: *pgsql <<: *pgsql
database: #{@options[:msftest_db_name]} database: #{@options[:msftest_db_name]}
username: #{@options[:msftest_db_user]} username: #{@options[:msftest_db_user]}
password: #{msftest_pass} password: #{@msftest_pass}
EOF EOF
end end
File.chmod(0640, @db_conf) File.chmod(0640, @db_conf)
create_db
start_db
puts 'Creating database users'
run_psql("create user #{@options[:msf_db_user]} with password '#{msf_pass}'")
run_psql("create user #{@options[:msftest_db_user]} with password '#{msftest_pass}'")
run_psql("alter role #{@options[:msf_db_user]} createdb")
run_psql("alter role #{@options[:msftest_db_user]} createdb")
run_psql("alter role #{@options[:msf_db_user]} with password '#{msf_pass}'")
run_psql("alter role #{@options[:msftest_db_user]} with password '#{msftest_pass}'")
run_cmd("createdb -p #{@options[:db_port]} -O #{@options[:msf_db_user]} -h 127.0.0.1 -U #{@options[:msf_db_user]} -E UTF-8 -T template0 #{@options[:msf_db_name]}",
"#{msf_pass}\n#{msf_pass}\n")
run_cmd("createdb -p #{@options[:db_port]} -O #{@options[:msftest_db_user]} -h 127.0.0.1 -U #{@options[:msftest_db_user]} -E UTF-8 -T template0 #{@options[:msftest_db_name]}",
"#{msftest_pass}\n#{msftest_pass}\n")
write_db_client_auth_config
restart_db
puts 'Creating initial database schema'
Dir.chdir(@framework) do
run_cmd('bundle exec rake db:migrate')
end
end end
def write_db_client_auth_config def write_db_client_auth_config