-
Notifications
You must be signed in to change notification settings - Fork 7
/
entry.go
239 lines (220 loc) · 6 KB
/
entry.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package entry
import (
"bufio"
"fmt"
"log"
"net"
"os"
"os/user"
"path/filepath"
"time"
"github.com/pkg/errors"
"github.com/skeema/knownhosts"
"golang.org/x/crypto/ssh"
"github.com/vimiix/ssx/internal/lg"
"github.com/vimiix/ssx/internal/terminal"
"github.com/vimiix/ssx/internal/utils"
"github.com/vimiix/ssx/ssx/env"
)
const (
SourceSSHConfig = "ssh_config"
SourceSSXStore = "ssx_store"
)
const defaultIdentityFile = "~/.ssh/id_rsa"
// Entry represent a target server
type Entry struct {
ID uint64 `json:"id"`
Host string `json:"host"`
User string `json:"user"`
Port string `json:"port"`
VisitCount int `json:"visit_count"` // Perhaps I will support sorting by VisitCount in the future
KeyPath string `json:"key_path"`
Passphrase string `json:"passphrase"`
Password string `json:"password"`
Tags []string `json:"tags"`
Source string `json:"source"` // Data source, used to distinguish that it is from ssx stored or local ssh configuration
CreateAt time.Time `json:"create_at"`
UpdateAt time.Time `json:"update_at"`
// TODO support jump server
}
func (e *Entry) String() string {
return fmt.Sprintf("%s@%s:%s", e.User, e.Host, e.Port)
}
func getConnectTimeout() time.Duration {
var defaultTimeout = time.Second * 10
val := os.Getenv(env.SSXConnectTimeout)
if len(val) <= 0 {
return defaultTimeout
}
d, err := time.ParseDuration(val)
if err != nil {
lg.Debug("invalid %q value: %q", env.SSXConnectTimeout, val)
d = defaultTimeout
}
return d
}
func (e *Entry) GenSSHConfig() (*ssh.ClientConfig, error) {
cb, err := e.sshHostKeyCallback()
if err != nil {
return nil, err
}
cfg := &ssh.ClientConfig{
User: e.User,
Auth: e.AuthMethods(),
HostKeyCallback: cb,
Timeout: getConnectTimeout(),
}
cfg.SetDefaults()
return cfg, nil
}
func (e *Entry) sshHostKeyCallback() (ssh.HostKeyCallback, error) {
khPath := utils.ExpandHomeDir("~/.ssh/known_hosts")
if !utils.FileExists(khPath) {
f, err := os.OpenFile(khPath, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return nil, err
}
_ = f.Close()
}
kh, err := knownhosts.New(khPath)
if err != nil {
lg.Error("failed to read known_hosts: ", err)
return nil, err
}
// Create a custom permissive hostkey callback which still errors on hosts
// with changed keys, but allows unknown hosts and adds them to known_hosts
cb := ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error {
err := kh(hostname, remote, key)
if knownhosts.IsHostKeyChanged(err) {
lg.Error("REMOTE HOST IDENTIFICATION HAS CHANGED for host %s! This may indicate a MitM attack.", hostname)
return errors.Errorf("host key changed for host %s", hostname)
} else if knownhosts.IsHostUnknown(err) {
f, ferr := os.OpenFile(khPath, os.O_APPEND|os.O_WRONLY, 0600)
if ferr == nil {
defer f.Close()
ferr = knownhosts.WriteKnownHost(f, hostname, remote, key)
}
if ferr == nil {
log.Printf("Added host %s to known_hosts\n", hostname)
} else {
log.Printf("Failed to add host %s to known_hosts: %v\n", hostname, ferr)
}
return nil
}
return err
})
return cb, nil
}
func (e *Entry) Tidy() error {
if len(e.User) <= 0 {
curUser, err := user.Current()
if err != nil {
return err
}
e.User = curUser.Username
}
if len(e.Port) <= 0 {
e.Port = "22"
}
if e.KeyPath == "" {
e.KeyPath = defaultIdentityFile
}
return nil
}
// AuthMethods all possible auth methods
func (e *Entry) AuthMethods() []ssh.AuthMethod {
var authMethods []ssh.AuthMethod
// password auth
if e.Password != "" {
authMethods = append(authMethods, ssh.Password(e.Password))
}
// key file auth methods
keyfileAuths := e.privateKeyAuthMethods()
if len(keyfileAuths) > 0 {
authMethods = append(authMethods, keyfileAuths...)
}
authMethods = append(authMethods, e.interactAuth())
return authMethods
}
func (e *Entry) interactAuth() ssh.AuthMethod {
return ssh.KeyboardInteractive(func(name, instruction string, questions []string, echos []bool) (answers []string, err error) {
answers = make([]string, 0, len(questions))
for i, q := range questions {
fmt.Print(q)
if echos[i] {
scan := bufio.NewScanner(os.Stdin)
if scan.Scan() {
answers = append(answers, scan.Text())
}
if err := scan.Err(); err != nil {
return nil, err
}
} else {
b, err := terminal.ReadPassword()
if err != nil {
return nil, err
}
fmt.Println()
answers = append(answers, string(b))
}
}
return answers, nil
})
}
func (e *Entry) privateKeyAuthMethods() []ssh.AuthMethod {
keyfiles := e.collectKeyfiles()
if len(keyfiles) == 0 {
return nil
}
var methods []ssh.AuthMethod
for _, f := range keyfiles {
auth := e.keyfileAuth(f)
if auth != nil {
methods = append(methods, auth)
}
}
return methods
}
func (e *Entry) keyfileAuth(keypath string) ssh.AuthMethod {
pemBytes, err := os.ReadFile(keypath)
if err != nil {
lg.Debug("generate rsaAuth: failed to read file %q: %s", keypath, err)
return nil
}
var signer ssh.Signer
if e.Passphrase == "" {
signer, err = ssh.ParsePrivateKey(pemBytes)
} else {
signer, err = ssh.ParsePrivateKeyWithPassphrase(pemBytes, []byte(e.Passphrase))
}
if err != nil {
lg.Debug("generate rsaAuth: %s", err)
return nil
}
return ssh.PublicKeys(signer)
}
// defaultRSAKeyFiles List of possible key files
// The order of the list represents the priority
var defaultRSAKeyFiles = []string{
"id_rsa", "id_ecdsa", "id_ecdsa_sk",
"id_ed25519", "id_ed25519_sk", "id_rsa",
}
func (e *Entry) collectKeyfiles() []string {
var keypaths []string
if e.KeyPath != "" && utils.FileExists(e.KeyPath) {
keypaths = append(keypaths, e.KeyPath)
}
u, err := user.Current()
if err != nil {
lg.Debug("failed to get current user, ignore default rsa keys")
return keypaths
}
for _, fn := range defaultRSAKeyFiles {
fp := filepath.Join(u.HomeDir, ".ssh", fn)
if fp == e.KeyPath || !utils.FileExists(fp) {
continue
}
keypaths = append(keypaths, fp)
}
return keypaths
}