-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilemanagers.go
211 lines (186 loc) · 5.46 KB
/
filemanagers.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
package revssh
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"os/user"
"path/filepath"
"runtime"
"sort"
"strings"
"github.com/cnf/revssh/revutil"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/knownhosts"
)
var (
keynames = []string{"ssh_host_ecdsa_key", "ssh_host_ed25519_key", "ssh_host_rsa_key"}
)
// FileClientSettings ...
type FileClientSettings struct {
KeyManager
remote string
user string
hostname string
// KeyManager *FileKeyManager
}
// NewFileClientSettings ...
func NewFileClientSettings() *FileClientSettings {
dpath := getDefaultPath()
cuser, _ := user.Current()
name := cuser.Username
var path = flag.String("path", dpath, "configuration path")
var remote = flag.String("remote", "127.0.0.1:2222", "address:port to connect")
var username = flag.String("user", name, "ssh user")
var hostname = flag.String("hostname", "", "hostname to register as")
flag.Parse()
return &FileClientSettings{remote: *remote, user: *username, hostname: *hostname, KeyManager: &FileKeyManager{path: *path}}
}
func (s *FileClientSettings) Remote() string {
return s.remote
}
func (s *FileClientSettings) User() string {
return s.user
}
func (s *FileClientSettings) Listen() string {
return ""
}
func (s *FileClientSettings) Hostname() string {
if s.hostname == "" {
hostname, err := os.Hostname()
if err != nil || hostname == "" {
return "revssh"
}
s.hostname = hostname
}
return s.hostname
}
// FileServerSettings ...
type FileServerSettings struct {
KeyManager
Listen string
// path string
// KeyManager *FileKeyManager
}
// NewFileServerSettings ...
func NewFileServerSettings() *FileServerSettings {
cuser, _ := user.Current()
dpath := fmt.Sprintf("%s/.config/revssh", cuser.HomeDir)
cdpath, _ := filepath.Abs(dpath)
// var path = flag.String("path", cdpath, "configuration path")
var listen = flag.String("listen", ":22", "address:port to listen on")
flag.Parse()
// s.path = *path
// s.path = cdpath
// s.Listen = *listen
return &FileServerSettings{Listen: *listen, KeyManager: &FileKeyManager{path: cdpath}}
}
// FileKeyManager ...
type FileKeyManager struct {
path string
}
// NewFileKeyManager ...
func NewFileKeyManager(path string) *FileKeyManager {
return &FileKeyManager{path: path}
}
// GetPublicKeys returns all publickeys for a specific username.
func (km *FileKeyManager) GetPublicKeys(username string) ([]ssh.PublicKey, error) {
return nil, nil
}
// GetAuthorizedKeys returns all public keys that are authorized to connect to this server.
func (km *FileKeyManager) GetAuthorizedKeys() []ssh.PublicKey {
// TODO: actually reading authorized_keys
var keys []ssh.PublicKey
file, err := os.Open(km.getAuthorizedKeysPath())
if err != nil {
log.Printf("ERROR: %+v", err)
return nil
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
// log.Println(scanner.Text())
key, _, _, _, err := ssh.ParseAuthorizedKey([]byte(scanner.Text()))
if err != nil {
log.Printf("ERROR: %+v", err)
return nil
}
keys = append(keys, key)
}
return keys
}
// IsKnownHost , like a ssh.HostKeyCallback, must return nil if the host key is OK,
// or an error to reject it. If no entry is found, it will add it.
func (km *FileKeyManager) IsKnownHost(hostname string, remote net.Addr, key ssh.PublicKey) error {
khkb, err := knownhosts.New(km.getKnownHostPath())
if err != nil {
// if strings.HasSuffix(err.Error(), "no such file or directory") || strings.HasSuffix(err.Error(), "The system cannot find the file specified") {
if os.IsNotExist(err) {
return revutil.AppendLine(km.getKnownHostPath(), knownhosts.Line([]string{knownhosts.Normalize(hostname)}, key))
}
return err
}
err = khkb(hostname, remote, key)
if err != nil {
if os.IsNotExist(err) || strings.HasSuffix(err.Error(), "knownhosts: key is unknown") {
// TODO: do we need to add remote net.Addr as one of the hostnames?
return revutil.AppendLine(km.getKnownHostPath(), knownhosts.Line([]string{knownhosts.Normalize(hostname)}, key))
}
return err
}
return nil
}
func (km *FileKeyManager) getKnownHostPath() string {
// return fmt.Sprintf("%s/known_hosts", km.path)
return filepath.Join(km.path, "known_hosts")
}
func (km *FileKeyManager) getAuthorizedKeysPath() string {
return filepath.Join(km.path, "authorized_keys")
}
// GetPrivateKeys returns a list of signers.
// If no private keys are available, one should be created.
func (km *FileKeyManager) GetPrivateKeys() []ssh.Signer {
path, err := getConfigDir(km.path)
if err != nil {
log.Panicf("ERROR: can't get config dir: %s", err)
}
files, err := ioutil.ReadDir(path)
if err != nil {
log.Panicf("ERROR: can't get config dir: %s", err)
}
sort.Strings(keynames)
hostKeys := make([]ssh.Signer, 0)
for fi := range files {
ki := sort.SearchStrings(keynames, files[fi].Name())
if ki < len(keynames) && keynames[ki] == files[fi].Name() {
hostKey, err := revutil.ParsePrivateKeyFile(fmt.Sprintf("%s/%s", path, files[fi].Name()))
if err != nil {
log.Printf("%+v", err)
continue
}
hostKeys = append(hostKeys, hostKey)
}
}
return hostKeys
}
func getConfigDir(path string) (string, error) {
// TODO: validate
err := os.MkdirAll(path, os.ModePerm)
if err != nil {
return "", err
}
return path, nil
}
func getDefaultPath() string {
var dpath string
if runtime.GOOS == "windows" {
dpath, _ = filepath.Abs("C:/RevSSH")
} else {
cuser, _ := user.Current()
dpath, _ = filepath.Abs(fmt.Sprintf("%s/.config/revssh", cuser.HomeDir))
}
return dpath
}