Class: Net::SSH::KnownHosts

Inherits:
Object
  • Object
show all
Defined in:
lib/net/ssh/known_hosts.rb

Overview

Searches an OpenSSH-style known-host file for a given host, and returns all matching keys. This is used to implement host-key verification, as well as to determine what key a user prefers to use for a given host.

This is used internally by Net::SSH, and will never need to be used directly by consumers of the library.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ KnownHosts

Instantiate a new KnownHosts instance that will search the given known-hosts file. The path is expanded file File.expand_path.



70
71
72
# File 'lib/net/ssh/known_hosts.rb', line 70

def initialize(source)
  @source = File.expand_path(source)
end

Instance Attribute Details

#sourceObject (readonly)

The host-key file name that this KnownHosts instance will use to search for keys.



66
67
68
# File 'lib/net/ssh/known_hosts.rb', line 66

def source
  @source
end

Class Method Details

.add(host, key, options = {}) ⇒ Object

Looks in all user known host files (see KnownHosts.hostfiles) and tries to add an entry for the given host and key to the first file it is able to.



52
53
54
55
56
57
58
59
60
61
# File 'lib/net/ssh/known_hosts.rb', line 52

def add(host, key, options={})
  hostfiles(options, :user).each do |file|
    begin
      KnownHosts.new(file).add(host, key)
      return
    rescue SystemCallError
      # try the next hostfile
    end
  end
end

.hostfiles(options, which = :all) ⇒ Object

Looks in the given options hash for the :user_known_hosts_file and :global_known_hosts_file keys, and returns an array of all known hosts files. If the :user_known_hosts_file key is not set, the default is returned (~/.ssh/known_hosts and ~/.ssh/known_hosts2). If :global_known_hosts_file is not set, the default is used (/etc/ssh/known_hosts and /etc/ssh/known_hosts2).

If you only want the user known host files, you can pass :user as the second option.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/net/ssh/known_hosts.rb', line 35

def hostfiles(options, which=:all)
  files = []

  if which == :all || which == :user
    files += Array(options[:user_known_hosts_file] || %w(~/.ssh/known_hosts ~/.ssh/known_hosts2))
  end

  if which == :all || which == :global
    files += Array(options[:global_known_hosts_file] || %w(/etc/ssh/known_hosts /etc/ssh/known_hosts2))
  end

  return files
end

.search_for(host, options = {}) ⇒ Object

Searches all known host files (see KnownHosts.hostfiles) for all keys of the given host. Returns an array of keys found.



16
17
18
# File 'lib/net/ssh/known_hosts.rb', line 16

def search_for(host, options={})
  search_in(hostfiles(options), host)
end

.search_in(files, host) ⇒ Object

Search for all known keys for the given host, in every file given in the files array. Returns the list of keys.



22
23
24
# File 'lib/net/ssh/known_hosts.rb', line 22

def search_in(files, host)
  files.map { |file| KnownHosts.new(file).keys_for(host) }.flatten
end

Instance Method Details

#add(host, key) ⇒ Object

Tries to append an entry to the current source file for the given host and key. If it is unable to (because the file is not writable, for instance), an exception will be raised.



121
122
123
124
125
126
# File 'lib/net/ssh/known_hosts.rb', line 121

def add(host, key)
  File.open(source, "a") do |file|
    blob = [Net::SSH::Buffer.from(:key, key).to_s].pack("m*").gsub(/\s/, "")
    file.puts "#{host} #{key.ssh_type} #{blob}"
  end
end

#keys_for(host) ⇒ Object

Returns an array of all keys that are known to be associatd with the given host. The host parameter is either the domain name or ip address of the host, or both (comma-separated). Additionally, if a non-standard port is being used, it may be specified by putting the host (or ip, or both) in square brackets, and appending the port outside the brackets after a colon. Possible formats for host, then, are;

"net.ssh.test"
"1.2.3.4"
"net.ssh.test,1.2.3.4"
"[net.ssh.test]:5555"
"[1,2,3,4]:5555"
"[net.ssh.test]:5555,[1.2.3.4]:5555


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/net/ssh/known_hosts.rb', line 87

def keys_for(host)
  keys = []
  return keys unless File.readable?(source)

  entries = host.split(/,/)

  File.open(source) do |file|
    scanner = StringScanner.new("")
    file.each_line do |line|
      scanner.string = line

      scanner.skip(/\s*/)
      next if scanner.match?(/$|#/)

      hostlist = scanner.scan(/\S+/).split(/,/)
      next unless entries.all? { |entry| hostlist.include?(entry) }

      scanner.skip(/\s*/)
      type = scanner.scan(/\S+/)

      next unless %w(ssh-rsa ssh-dss).include?(type)

      scanner.skip(/\s*/)
      blob = scanner.rest.unpack("m*").first
      keys << Net::SSH::Buffer.new(blob).read_key
    end
  end

  keys
end