Class: RVC::KnownHosts

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

Instance Method Summary collapse

Constructor Details

#initializeKnownHosts

Returns a new instance of KnownHosts.



28
29
30
# File 'lib/rvc/known_hosts.rb', line 28

def initialize
  @ignore_permissions = RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
end

Instance Method Details

#add(protocol, hostname, public_key) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/rvc/known_hosts.rb', line 62

def add protocol, hostname, public_key
  FileUtils.mkdir_p File.dirname(filename)
  File.open(filename, 'a') do |io|
    io.chmod 0600
    io.write "#{hash_host protocol, hostname} #{hash_public_key public_key}\n"
  end
end

#filenameObject



32
33
34
# File 'lib/rvc/known_hosts.rb', line 32

def filename
  File.join(ENV['HOME'], ".rvc", "known_hosts");
end

#hash_host(protocol, hostname) ⇒ Object



36
37
38
# File 'lib/rvc/known_hosts.rb', line 36

def hash_host protocol, hostname
  Digest::SHA2.hexdigest([protocol, hostname] * "\0")
end

#hash_public_key(public_key) ⇒ Object



40
41
42
# File 'lib/rvc/known_hosts.rb', line 40

def hash_public_key public_key
  Digest::SHA2.hexdigest(public_key)
end

#verify(protocol, hostname, public_key) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rvc/known_hosts.rb', line 44

def verify protocol, hostname, public_key
  expected_hashed_host = hash_host protocol, hostname
  expected_hashed_public_key = hash_public_key public_key
  if File.exists? filename
    fail "bad permissions on #{filename}, expected 0600" unless @ignore_permissions or File.stat(filename).mode & 0666 == 0600
    File.readlines(filename).each_with_index do |l,i|
      hashed_host, hashed_public_key = l.split
      next unless hashed_host == expected_hashed_host
      if hashed_public_key == expected_hashed_public_key
        return :ok
      else
        return :mismatch, i
      end
    end
  end
  return :not_found, expected_hashed_public_key
end