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/metasploit/framework/database.rb
Views: 1904
1
require 'metasploit/framework'
2
3
module Metasploit
4
module Framework
5
module Database
6
#
7
# CONSTANTS
8
#
9
10
CONFIGURATIONS_PATHNAME_PRECEDENCE = [
11
:environment_configurations_pathname,
12
:user_configurations_pathname,
13
:project_configurations_pathname
14
]
15
16
#
17
# Module Methods
18
#
19
20
# Returns first configuration pathname from configuration_pathnames or the overriding `:path`.
21
#
22
# @param options [Hash{Symbol=>String}]
23
# @option options [String] :path Path to use instead of first element of configurations_pathnames
24
# @return [Pathname] if configuration pathname exists.
25
# @return [nil] if configuration pathname does not exist.
26
def self.configurations_pathname(options={})
27
options.assert_valid_keys(:path)
28
29
path = options[:path]
30
31
if path.present?
32
pathname = Pathname.new(path)
33
else
34
pathname = configurations_pathnames.first
35
end
36
37
if !pathname.nil? && pathname.exist?
38
pathname
39
else
40
nil
41
end
42
end
43
44
# Return configuration pathnames that exist.
45
#
46
# Returns `Pathnames` in order of precedence
47
#
48
# 1. {environment_configurations_pathname}
49
# 2. {user_configurations_pathname}
50
# 3. {project_configurations_pathname}
51
#
52
# @return [Array<Pathname>]
53
def self.configurations_pathnames
54
configurations_pathnames = []
55
56
CONFIGURATIONS_PATHNAME_PRECEDENCE.each do |configurations_pathname_message|
57
configurations_pathname = public_send(configurations_pathname_message)
58
59
if !configurations_pathname.nil? && configurations_pathname.exist?
60
configurations_pathnames << configurations_pathname
61
end
62
end
63
64
configurations_pathnames
65
end
66
67
# Pathname to `database.yml` pointed to by `MSF_DATABASE_CONFIG` environment variable.
68
#
69
# @return [Pathname] if `MSF_DATABASE_CONFIG` is not blank.
70
# @return [nil] otherwise
71
def self.environment_configurations_pathname
72
msf_database_config = ENV['MSF_DATABASE_CONFIG']
73
74
if msf_database_config.blank?
75
msf_database_config = nil
76
else
77
msf_database_config = Pathname.new(msf_database_config)
78
end
79
80
msf_database_config
81
end
82
83
# Pathname to `database.yml` for the metasploit-framework project in `config/database.yml`.
84
#
85
# @return [Pathname]
86
def self.project_configurations_pathname
87
root = Pathname.new(__FILE__).realpath.parent.parent.parent.parent
88
root.join('config', 'database.yml')
89
end
90
91
# Pathname to `database.yml` in the user's config directory.
92
#
93
# @return [Pathname] if the user has a `database.yml` in their config directory (`~/.msf4` by default).
94
# @return [nil] if the user does not have a `database.yml` in their config directory.
95
def self.user_configurations_pathname
96
Pathname.new(Msf::Config.config_directory).join('database.yml')
97
end
98
end
99
end
100
end
101
102