-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathserver_test.go
296 lines (260 loc) · 6.09 KB
/
server_test.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package sftpd
import (
"crypto/rand"
"errors"
"io"
"net"
"os"
"strings"
"testing"
client "github.com/pkg/sftp"
"github.com/taruti/sshutil"
"golang.org/x/crypto/ssh"
)
const alnum = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var testUser = "test"
var testPass = func() []byte {
bs := make([]byte, 16)
_, e := io.ReadFull(rand.Reader, bs)
if e != nil {
panic(e)
}
for i, b := range bs {
bs[i] = alnum[int(b)%len(alnum)]
}
return bs
}()
var tdebug = debug
var tdebugf = debugf
func failOnErr(t *testing.T, err error, reason string) {
if err != nil {
t.Fatalf("%s: %v", reason, err)
}
}
func PrintDiscardRequests(in <-chan *ssh.Request) {
for req := range in {
tdebug("PRINTDISC", req.Type, *req)
if req.WantReply {
req.Reply(false, nil)
}
}
}
func TestServer(t *testing.T) {
tdebug = t.Log
tdebugf = t.Logf
tdebugf("Listening on port 2022 user %s pass %s\n", testUser, testPass)
config := &ssh.ServerConfig{
PasswordCallback: sshutil.CreatePasswordCheck(testUser, testPass),
}
// Add the sshutil.RSA2048 and sshutil.Save flags if needed for the server in question...
hkey, e := sshutil.KeyLoader{Flags: sshutil.Create}.Load()
failOnErr(t, e, "Failed to parse host key")
tdebugf("Public key: %s\n", sshutil.PublicKeyHash(hkey.PublicKey()))
config.AddHostKey(hkey)
listener, e := net.Listen("tcp", "127.0.0.1:2022")
failOnErr(t, e, "Failed to listen")
go ClientDo()
// for {
nConn, e := listener.Accept()
failOnErr(t, e, "Failed to accept")
handleTestConn(nConn, config, t, EmptyFS{})
// }
go ClientDo()
// for {
nConn, e = listener.Accept()
failOnErr(t, e, "Failed to accept")
os.Mkdir("/tmp/test-sftpd", 0700)
handleTestConn(nConn, config, t, rfs{})
// }
}
func handleTestConn(conn net.Conn, config *ssh.ServerConfig, t *testing.T, fs FileSystem) {
sc, chans, reqs, e := ssh.NewServerConn(conn, config)
failOnErr(t, e, "Failed to initiate new connection")
tdebug("sc", sc)
// The incoming Request channel must be serviced.
go PrintDiscardRequests(reqs)
// Service the incoming Channel channel.
for newChannel := range chans {
tdebug("NEWCHANNEL", newChannel, newChannel.ChannelType())
if newChannel.ChannelType() != "session" {
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
continue
}
channel, requests, err := newChannel.Accept()
if err != nil {
panic("could not accept channel.")
}
go func(in <-chan *ssh.Request) {
for req := range in {
tdebug("REQUEST:", *req)
ok := false
switch {
case IsSftpRequest(req):
ok = true
go func() { tdebug(ServeChannel(channel, fs)) }()
}
req.Reply(ok, nil)
}
}(requests)
}
}
func ClientDo() {
e := clientDo()
if e != nil {
tdebug("CLIENT ERROR", e)
}
}
func clientDo() error {
var cc ssh.ClientConfig
cc.User = string(testUser)
cc.Auth = append(cc.Auth, ssh.Password(string(testPass)))
// Use this only for localhost testing.
cc.HostKeyCallback = ssh.InsecureIgnoreHostKey()
conn, e := ssh.Dial("tcp4", "127.0.0.1:2022", &cc)
if e != nil {
return e
}
defer conn.Close()
cl, e := client.NewClient(conn)
if e != nil {
return e
}
cl.Mkdir("/D")
cl.Chmod("/D", 0700)
cl.Remove("/D")
rs, e := cl.ReadDir("/")
if e != nil {
return e
}
tdebug(rs)
return nil
}
func TestRandomInput(t *testing.T) {
fs := EmptyFS{}
rd := &fakeRandChannel{}
for i := 0; i < 10000; i++ {
rd.rem = 5
ServeChannel(rd, fs)
}
for i := 0; i < 257; i++ {
for j := 0; j < 1000; j++ {
rd.rem = i
ServeChannel(rd, fs)
}
}
}
type fakeRandChannel struct{ rem int }
func (fr *fakeRandChannel) Read(data []byte) (int, error) {
if len(data) > fr.rem {
data = data[0:fr.rem]
}
n, e := rand.Read(data)
fr.rem -= n
if fr.rem <= 0 {
fr.rem = 0
e = io.EOF
}
if len(data) >= 4 && data[0]&1 == 0 {
data[0], data[1], data[2] = 0, 0, 0
for i := 5; len(data) >= 4+i; i += 4 {
data[i], data[i+1], data[i+2] = 0, 0, 0
}
}
return n, e
}
func (*fakeRandChannel) Write(bs []byte) (int, error) {
tmp := make([]byte, 1)
rand.Read(tmp)
if tmp[0]&7 == 7 {
return 0, errors.New("Random write error")
}
return len(bs), nil
}
func (*fakeRandChannel) Close() error { return nil }
func (*fakeRandChannel) CloseWrite() error { return nil }
func (*fakeRandChannel) SendRequest(name string, wantReply bool, payload []byte) (bool, error) {
return true, nil
}
func (fr *fakeRandChannel) Stderr() io.ReadWriter { return fr }
type rfile struct {
EmptyFile
f *os.File
}
func (rf rfile) Close() error { return rf.f.Close() }
func (rf rfile) ReadAt(bs []byte, pos int64) (int, error) { return rf.f.ReadAt(bs, pos) }
type rfs struct {
EmptyFS
}
type rdir struct {
d *os.File
}
func (d rdir) Readdir(count int) ([]NamedAttr, error) {
fis, e := d.d.Readdir(count)
if e != nil {
return nil, e
}
nas := make([]NamedAttr, len(fis))
for i, fi := range fis {
nas[i].Name = fi.Name()
nas[i].FillFrom(fi)
}
return nas, nil
}
func (d rdir) Close() error {
return d.d.Close()
}
// Warning:
// Use your own path mangling functionality in production code.
// This can be quite non-trivial depending on the operating system.
// The code below is not sufficient for production servers.
func rfsMangle(path string) (string, error) {
if strings.Contains(path, "..") {
return "<invalid>", errors.New("Invalid path")
}
if len(path) > 0 && path[0] == '/' {
path = path[1:]
}
path = "/tmp/test-sftpd/" + path
tdebug("MANGLE -> " + path)
return path, nil
}
func (fs rfs) OpenDir(path string) (Dir, error) {
p, e := rfsMangle(path)
if e != nil {
return nil, e
}
f, e := os.Open(p)
if e != nil {
return nil, e
}
return rdir{f}, nil
}
func (fs rfs) OpenFile(path string, mode uint32, a *Attr) (File, error) {
p, e := rfsMangle(path)
if e != nil {
return nil, e
}
f, e := os.Open(p)
if e != nil {
return nil, e
}
return rfile{f: f}, nil
}
func (fs rfs) Stat(name string, islstat bool) (*Attr, error) {
p, e := rfsMangle(name)
if e != nil {
return nil, e
}
var fi os.FileInfo
if islstat {
fi, e = os.Lstat(p)
} else {
fi, e = os.Stat(p)
}
if e != nil {
return nil, e
}
var a Attr
a.FillFrom(fi)
return &a, nil
}