-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
222 lines (185 loc) · 4.8 KB
/
config.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package gossh
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"golang.org/x/crypto/ssh"
)
// Config englobes ssh client configuration with host/port
type Config struct {
Host string
Port int
ClientConfig *ssh.ClientConfig
}
// NewClientConfigWithKeyFile returns a configuration
// corresponding to a simple configuration with private key
func NewClientConfigWithKeyFile(username string, sshKey string, host string, port int, checkHostKey bool) (*Config, error) {
var hostKey ssh.PublicKey
c := &Config{
Host: host,
Port: port,
}
// Read private key
key, err := ioutil.ReadFile(sshKey)
if err != nil {
return nil, err
}
// Create the Signer for this private key.
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return nil, err
}
if checkHostKey {
//arr := strings.Split(host, ":")
hostKey, err = getHostKey(host, strconv.Itoa(port))
if err != nil {
return nil, err
}
}
c.ClientConfig = &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
//ssh.Password("chrYsal1s-adm1n"),
//ssh.PublicKeyFile("/home/uthng/.ssh/ssh_servers"),
ssh.PublicKeys(signer),
},
//HostKeyCallback: ssh.FixedHostKey(hostKey),
//HostKeyCallback: ssh.InsecureIgnoreHostKey(),
//HostKeyCallback: nil,
}
if checkHostKey {
c.ClientConfig.HostKeyCallback = ssh.FixedHostKey(hostKey)
} else {
c.ClientConfig.HostKeyCallback = ssh.InsecureIgnoreHostKey()
}
return c, nil
}
// NewClientConfigWithSignedPubKeyFile returns a configuration
// corresponding to the configuration using private key along with
// a signed public key.
func NewClientConfigWithSignedPubKeyFile(username, sshKey, signedPubKey, host string, port int, checkHostKey bool) (*Config, error) {
var hostKey ssh.PublicKey
c := &Config{
Host: host,
Port: port,
}
// Read private key
key, err := ioutil.ReadFile(sshKey)
if err != nil {
return nil, err
}
// Create the Signer for this private key.
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return nil, err
}
// Load the certificate
cert, err := ioutil.ReadFile(signedPubKey)
if err != nil {
return nil, err
}
pubKey, _, _, _, err := ssh.ParseAuthorizedKey(cert)
if err != nil {
return nil, err
}
certSigner, err := ssh.NewCertSigner(pubKey.(*ssh.Certificate), signer)
if err != nil {
return nil, err
}
if checkHostKey {
//arr := strings.Split(host, ":")
hostKey, err = getHostKey(host, strconv.Itoa(port))
if err != nil {
return nil, err
}
}
c.ClientConfig = &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
//ssh.Password("chrYsal1s-adm1n"),
//ssh.PublicKeyFile("/home/uthng/.ssh/ssh_servers"),
ssh.PublicKeys(certSigner),
},
//HostKeyCallback: ssh.FixedHostKey(hostKey),
//HostKeyCallback: ssh.InsecureIgnoreHostKey(),
//HostKeyCallback: nil,
}
if checkHostKey {
c.ClientConfig.HostKeyCallback = ssh.FixedHostKey(hostKey)
} else {
c.ClientConfig.HostKeyCallback = ssh.InsecureIgnoreHostKey()
}
return c, nil
}
// NewClientConfigWithUserPass returns a configuration
// with given parameters
func NewClientConfigWithUserPass(username string, password string, host string, port int, checkHostKey bool) (*Config, error) {
var hostKey ssh.PublicKey
var err error
c := &Config{
Host: host,
Port: port,
}
if checkHostKey {
//arr := strings.Split(host, ":")
hostKey, err = getHostKey(host, strconv.Itoa(port))
if err != nil {
return nil, err
}
}
c.ClientConfig = &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
}
if checkHostKey {
c.ClientConfig.HostKeyCallback = ssh.FixedHostKey(hostKey)
} else {
c.ClientConfig.HostKeyCallback = ssh.InsecureIgnoreHostKey()
}
return c, nil
}
/////////// PRIVATE FUNCTIONS ////////////////////////////
func getHostKey(host, port string) (ssh.PublicKey, error) {
// $HOME/.ssh/known_hosts
file, err := os.Open(filepath.Join(os.Getenv("HOME"), ".ssh", "known_hosts"))
if err != nil {
return nil, err
}
defer file.Close()
var hostport string
if port == "22" {
// standard port assumes 22
// 192.168.10.53 ssh-rsa AAAAB3Nza...vguvx+81N1xaw==
hostport = host
} else {
// non-standard port(s)
// [ssh.example.com]:1999,[93.184.216.34]:1999 ssh-rsa AAAAB3Nza...vguvx+81N1xaw==
hostport = "[" + host + "]:" + port
}
scanner := bufio.NewScanner(file)
var hostKey ssh.PublicKey
for scanner.Scan() {
fields := strings.Split(scanner.Text(), " ")
if len(fields) != 3 {
continue
}
if strings.Contains(fields[0], hostport) {
var err error
hostKey, _, _, _, err = ssh.ParseAuthorizedKey(scanner.Bytes())
if err != nil {
return nil, err
}
break // scanning line by line, first occurrence will be returned
}
}
if hostKey == nil {
return nil, fmt.Errorf("No hostkey for %s", host+":"+port)
}
return hostKey, nil
}