Module: Metasploit::Framework::Database

Defined in:
lib/metasploit/framework/database.rb

Constant Summary collapse

CONFIGURATIONS_PATHNAME_PRECEDENCE =

CONSTANTS

[
    :environment_configurations_pathname,
    :user_configurations_pathname,
    :project_configurations_pathname
]

Class Method Summary collapse

Class Method Details

.configurations_pathname(options = {}) ⇒ Pathname?

Returns first configuration pathname from configuration_pathnames or the overridding ‘:path`.

Parameters:

  • options (Hash{Symbol=>String}) (defaults to: {})

Options Hash (options):

  • :path (String)

    Path to use instead of first element of configurations_pathnames

Returns:

  • (Pathname)

    if configuration pathname exists.

  • (nil)

    if configuration pathname does not exist.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/metasploit/framework/database.rb', line 26

def self.configurations_pathname(options={})
  options.assert_valid_keys(:path)

  path = options[:path]

  if path.present?
    pathname = Pathname.new(path)
  else
    pathname = configurations_pathnames.first
  end

  if !pathname.nil? && pathname.exist?
    pathname
  else
    nil
  end
end

.configurations_pathnamesArray<Pathname>

Return configuration pathnames that exist.

Returns ‘Pathnames` in order of precedence

  1. environment_configurations_pathname

  2. user_configurations_pathname

  3. project_configurations_pathname

Returns:

  • (Array<Pathname>)


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/metasploit/framework/database.rb', line 53

def self.configurations_pathnames
  configurations_pathnames = []

  CONFIGURATIONS_PATHNAME_PRECEDENCE.each do |configurations_pathname_message|
    configurations_pathname = public_send(configurations_pathname_message)

    if !configurations_pathname.nil? && configurations_pathname.exist?
      configurations_pathnames << configurations_pathname
    end
  end

  configurations_pathnames
end

.environment_configurations_pathnamePathname?

Pathname to ‘database.yml` pointed to by `MSF_DATABASE_CONFIG` environment variable.

Returns:

  • (Pathname)

    if ‘MSF_DATABASE_CONFIG` is not blank.

  • (nil)

    otherwise



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/metasploit/framework/database.rb', line 71

def self.environment_configurations_pathname
  msf_database_config = ENV['MSF_DATABASE_CONFIG']

  if msf_database_config.blank?
    msf_database_config = nil
  else
    msf_database_config = Pathname.new(msf_database_config)
  end

  msf_database_config
end

.project_configurations_pathnamePathname

Pathname to ‘database.yml` for the metasploit-framework project in `config/database.yml`.

Returns:

  • (Pathname)


86
87
88
89
# File 'lib/metasploit/framework/database.rb', line 86

def self.project_configurations_pathname
  root = Pathname.new(__FILE__).realpath.parent.parent.parent.parent
  root.join('config', 'database.yml')
end

.user_configurations_pathnamePathname?

Pathname to ‘database.yml` in the user’s config directory.

Returns:

  • (Pathname)

    if the user has a ‘database.yml` in their config directory (`~/.msf4` by default).

  • (nil)

    if the user does not have a ‘database.yml` in their config directory.



95
96
97
# File 'lib/metasploit/framework/database.rb', line 95

def self.user_configurations_pathname
  Pathname.new(Msf::Config.config_directory).join('database.yml')
end