-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
skel.go
181 lines (140 loc) · 3.74 KB
/
skel.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
package main
import (
"fmt"
"os"
"path"
"path/filepath"
log "github.com/sirupsen/logrus"
"github.com/tg123/sshpiper/libplugin"
)
type workdingdirFactory struct {
root string
allowBadUsername bool
noPasswordAuth bool
noCheckPerm bool
strictHostKey bool
recursiveSearch bool
}
type skelpipeWrapper struct {
dir *workingdir
host string
username string
}
type skelpipeFromWrapper struct {
skelpipeWrapper
}
type skelpipePasswordWrapper struct {
skelpipeFromWrapper
}
type skelpipePublicKeyWrapper struct {
skelpipeFromWrapper
}
type skelpipeToWrapper struct {
skelpipeWrapper
}
type skelpipeToPasswordWrapper struct {
skelpipeToWrapper
}
type skelpipeToPrivateKeyWrapper struct {
skelpipeToWrapper
}
func (s *skelpipeWrapper) From() []libplugin.SkelPipeFrom {
w := skelpipeFromWrapper{
skelpipeWrapper: *s,
}
if s.dir.Exists(userAuthorizedKeysFile) && s.dir.Exists(userKeyFile) {
return []libplugin.SkelPipeFrom{&skelpipePublicKeyWrapper{
skelpipeFromWrapper: w,
}}
} else {
return []libplugin.SkelPipeFrom{&skelpipePasswordWrapper{
skelpipeFromWrapper: w,
}}
}
}
func (s *skelpipeToWrapper) User(conn libplugin.ConnMetadata) string {
return s.username
}
func (s *skelpipeToWrapper) Host(conn libplugin.ConnMetadata) string {
return s.host
}
func (s *skelpipeToWrapper) IgnoreHostKey(conn libplugin.ConnMetadata) bool {
return !s.dir.Strict
}
func (s *skelpipeToWrapper) KnownHosts(conn libplugin.ConnMetadata) ([]byte, error) {
return s.dir.Readfile(userKnownHosts)
}
func (s *skelpipeFromWrapper) MatchConn(conn libplugin.ConnMetadata) (libplugin.SkelPipeTo, error) {
if s.dir.Exists(userKeyFile) {
return &skelpipeToPrivateKeyWrapper{
skelpipeToWrapper: skelpipeToWrapper(*s),
}, nil
}
return &skelpipeToPasswordWrapper{
skelpipeToWrapper: skelpipeToWrapper(*s),
}, nil
}
func (s *skelpipePasswordWrapper) TestPassword(conn libplugin.ConnMetadata, password []byte) (bool, error) {
return true, nil // TODO support later
}
func (s *skelpipePublicKeyWrapper) AuthorizedKeys(conn libplugin.ConnMetadata) ([]byte, error) {
return s.dir.Readfile(userAuthorizedKeysFile)
}
func (s *skelpipePublicKeyWrapper) TrustedUserCAKeys(conn libplugin.ConnMetadata) ([]byte, error) {
return nil, nil // TODO support this
}
func (s *skelpipeToPrivateKeyWrapper) PrivateKey(conn libplugin.ConnMetadata) ([]byte, []byte, error) {
k, err := s.dir.Readfile(userKeyFile)
if err != nil {
return nil, nil, err
}
return k, nil, nil
}
func (s *skelpipeToPasswordWrapper) OverridePassword(conn libplugin.ConnMetadata) ([]byte, error) {
return nil, nil
}
func (wf *workdingdirFactory) listPipe(conn libplugin.ConnMetadata) ([]libplugin.SkelPipe, error) {
user := conn.User()
if !wf.allowBadUsername {
if !isUsernameSecure(user) {
return nil, fmt.Errorf("bad username: %s", user)
}
}
var pipes []libplugin.SkelPipe
userdir := path.Join(wf.root, conn.User())
_ = filepath.Walk(userdir, func(path string, info os.FileInfo, err error) (stop error) {
log.Infof("search upstreams in path: %v", path)
if err != nil {
log.Infof("error walking path: %v", err)
return
}
if !info.IsDir() {
return
}
if !wf.recursiveSearch {
stop = fmt.Errorf("stop")
}
w := &workingdir{
Path: path,
NoCheckPerm: wf.noCheckPerm,
Strict: wf.strictHostKey,
}
data, err := w.Readfile(userUpstreamFile)
if err != nil {
log.Infof("error reading upstream file: %v in %v", err, w.Path)
return
}
host, user, err := parseUpstreamFile(string(data))
if err != nil {
log.Infof("ignore upstream folder %v due to: %v", w.Path, err)
return
}
pipes = append(pipes, &skelpipeWrapper{
dir: w,
host: host,
username: user,
})
return
})
return pipes, nil
}