-
Notifications
You must be signed in to change notification settings - Fork 133
/
hosts.go
121 lines (91 loc) · 2.88 KB
/
hosts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
117
118
119
120
121
// Copyright 2020 Mohammed El Bahja. All rights reserved.
// Use of this source code is governed by a MIT license.
package goph
import (
"errors"
"fmt"
"net"
"os"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/knownhosts"
)
// DefaultKnownHosts returns host key callback from default known hosts path, and error if any.
func DefaultKnownHosts() (ssh.HostKeyCallback, error) {
path, err := DefaultKnownHostsPath()
if err != nil {
return nil, err
}
return KnownHosts(path)
}
// KnownHosts returns host key callback from a custom known hosts path.
func KnownHosts(file string) (ssh.HostKeyCallback, error) {
return knownhosts.New(file)
}
// CheckKnownHost checks is host in known hosts file.
// it returns is the host found in known_hosts file and error, if the host found in
// known_hosts file and error not nil that means public key mismatch, maybe MAN IN THE MIDDLE ATTACK! you should not handshake.
func CheckKnownHost(host string, remote net.Addr, key ssh.PublicKey, knownFile string) (found bool, err error) {
var keyErr *knownhosts.KeyError
// Fallback to default known_hosts file
if knownFile == "" {
path, err := DefaultKnownHostsPath()
if err != nil {
return false, err
}
knownFile = path
}
// Get host key callback
callback, err := KnownHosts(knownFile)
if err != nil {
return false, err
}
// check if host already exists.
err = callback(host, remote, key)
// Known host already exists.
if err == nil {
return true, nil
}
// Make sure that the error returned from the callback is host not in file error.
// If keyErr.Want is greater than 0 length, that means host is in file with different key.
if errors.As(err, &keyErr) && len(keyErr.Want) > 0 {
return true, keyErr
}
// Some other error occurred and safest way to handle is to pass it back to user.
if err != nil {
return false, err
}
// Key is not trusted because it is not in the file.
return false, nil
}
// AddKnownHost add a a host to known hosts file.
func AddKnownHost(host string, remote net.Addr, key ssh.PublicKey, knownFile string) (err error) {
// Fallback to default known_hosts file
if knownFile == "" {
path, err := DefaultKnownHostsPath()
if err != nil {
return err
}
knownFile = path
}
f, err := os.OpenFile(knownFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
if err != nil {
return err
}
defer f.Close()
remoteNormalized := knownhosts.Normalize(remote.String())
hostNormalized := knownhosts.Normalize(host)
addresses := []string{remoteNormalized}
if hostNormalized != remoteNormalized {
addresses = append(addresses, hostNormalized)
}
_, err = f.WriteString(knownhosts.Line(addresses, key) + "\n")
return err
}
// DefaultKnownHostsPath returns default user knows hosts file.
func DefaultKnownHostsPath() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return fmt.Sprintf("%s/.ssh/known_hosts", home), err
}