forked from Matir/sshdog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scp.go
437 lines (405 loc) · 9.34 KB
/
scp.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// Copyright 2016 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// TODO: High-level file comment.
package main
import (
"bufio"
"errors"
"fmt"
"golang.org/x/crypto/ssh"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
)
const (
SCPCopy = iota
SCPDir
SCPEndDir
SCPTime
)
const (
SCPOK = iota
SCPError
SCPFatal
)
type SCPCommand struct {
CommandType int
Mode int16
Length int64
Name string
}
var (
SCP_END_COMMANDS = "\x00"
ErrInvalidAck = errors.New("Invalid ack code.")
ErrInvalidPieces = errors.New("Invalid number of command pieces.")
ErrNotRegularFile = errors.New("Not a regular file.")
ErrNotDirectory = errors.New("Not a directory.")
)
// Manage SCP operations in a built-in fashion
func (conn *ServerConn) SCPHandler(shellCmd []string, ch ssh.Channel) error {
shellCmd = shellCmd[1:] // pop scp off
var path string
var source bool
var dirMode bool
var recursive bool
for _, opt := range shellCmd {
switch opt {
case "-t":
source = false
case "-f":
source = true
case "-d":
dirMode = true
case "-r":
recursive = true
case "-p":
case "-v":
default:
dbg.Debug("scp path: %s", opt)
path = opt
}
}
var err error
if source {
err = conn.SCPSource(path, dirMode, recursive, ch)
} else {
err = conn.SCPSink(path, dirMode, ch)
}
if err != nil {
scpSendError(ch, err)
}
ch.CloseWrite()
return err
}
// Handle the 'source' side of an SCP connection
func (conn *ServerConn) SCPSource(path string, dirMode bool, recursive bool, ch ssh.Channel) error {
src := bufio.NewReader(ch)
if err := readAck(src); err != nil {
return err
}
if recursive {
return SCPSendDir(path, nil, src, ch)
}
return SCPSendFile(path, src, ch)
}
// Send a directory
func SCPSendDir(path string, fi os.FileInfo, src *bufio.Reader, dst io.Writer) error {
if fi == nil {
if statfi, err := os.Stat(path); err != nil {
return err
} else {
fi = statfi
}
}
dbg.Debug("Preparing to send dir: %s", path)
cmd := buildSCPCommand(fi)
if _, err := dst.Write([]byte(cmd)); err != nil {
return err
}
dbg.Debug("sent header")
if err := readAck(src); err != nil {
return err
}
// Children
if contents, err := ioutil.ReadDir(path); err != nil {
scpSendAck(dst, SCPFatal, err.Error())
return err
} else {
for _, child := range contents {
lpath := filepath.Join(path, child.Name())
if child.IsDir() {
SCPSendDir(lpath, child, src, dst)
} else {
SCPSendFile2(lpath, child, src, dst)
}
}
}
if _, err := dst.Write([]byte("E\n")); err != nil {
return err
}
if err := readAck(src); err != nil {
return err
}
dbg.Debug("Done sending dir: %s", path)
return nil
}
// Send a file
func SCPSendFile(path string, src *bufio.Reader, dst io.Writer) error {
dbg.Debug("Preparing to send %s", path)
fi, err := os.Stat(path)
if err != nil {
return err
}
return SCPSendFile2(path, fi, src, dst)
}
// Actually send the file
func SCPSendFile2(path string, fi os.FileInfo, src *bufio.Reader, dst io.Writer) error {
if fi.Mode()&os.ModeType != 0 {
scpSendAck(dst, SCPFatal, ErrNotRegularFile.Error())
return ErrNotRegularFile
}
fp, err := os.Open(path)
if err != nil {
scpSendAck(dst, SCPFatal, err.Error())
return err
}
defer fp.Close()
cmd := buildSCPCommand(fi)
if _, err := dst.Write([]byte(cmd)); err != nil {
return err
}
dbg.Debug("sent header")
if err := readAck(src); err != nil {
return err
}
dbg.Debug("sending data...")
if _, err := io.Copy(dst, fp); err != nil {
scpSendAck(dst, SCPFatal, err.Error())
return err
}
dbg.Debug("data sent")
scpSendAck(dst, SCPOK, "")
if err := readAck(src); err != nil {
return err
}
dbg.Debug("successfully sent file")
return nil
}
func buildSCPCommand(fi os.FileInfo) string {
c := 'C'
if fi.IsDir() {
c = 'D'
}
ret := fmt.Sprintf("%c%04o %d %s\n",
c, fi.Mode()&os.ModePerm, fi.Size(), fi.Name())
dbg.Debug("cmd: %s", strings.TrimSpace(ret))
return ret
}
// Read an acknowledgement
func readAck(src *bufio.Reader) error {
if ack, ackMsg, err := readAckDetails(src); err != nil {
return err
} else if ack != SCPOK {
dbg.Debug("SCP Ack error %d, msg \"%s\"", ack, ackMsg)
return fmt.Errorf("SCP Error %d", ack)
}
return nil
}
func readAckDetails(src *bufio.Reader) (int, string, error) {
b, err := src.ReadByte()
if err != nil {
return SCPFatal, "", err
}
if b > SCPFatal {
return SCPFatal, "", fmt.Errorf("%d is not a valid SCP status code", b)
}
if b > SCPOK {
msg, err := src.ReadString('\n')
if err != nil {
return SCPFatal, "", err
}
return int(b), strings.TrimRight(msg, "\n"), nil
}
return SCPOK, "", nil
}
// Handle the 'sink' side of an SCP connection
func (conn *ServerConn) SCPSink(path string, dirMode bool, ch ssh.Channel) error {
readbuf := bufio.NewReader(ch)
for {
if err := scpSendAck(ch, 0, ""); err != nil {
return err
}
// Get the text of the command
cmd, err := scpReadCommand(readbuf)
if err != nil {
if err == io.EOF {
// EOF here isn't bad
dbg.Debug("eof in scp sink")
scpSendAck(ch, 0, "")
return nil
}
scpSendAck(ch, 2, err.Error())
dbg.Debug("error in scp sink: %v", err)
return err
}
if cmd == SCP_END_COMMANDS {
dbg.Debug("continue scp")
continue
}
// Parse it
parsed, err := parseSCPCommand(cmd)
if err != nil {
scpSendAck(ch, 2, err.Error())
dbg.Debug("error in scp sink: %v", err)
return err
}
dbg.Debug("scp command: %v", parsed)
switch parsed.CommandType {
case SCPCopy:
if err := scpSendAck(ch, 0, ""); err != nil {
return err
}
if err := receiveFile(path, parsed, readbuf); err != nil {
scpSendAck(ch, 2, err.Error())
return err
}
case SCPDir:
path = filepath.Join(path, parsed.Name)
if err := maybeMakeDir(path, parsed.Mode); err != nil {
scpSendAck(ch, 2, err.Error())
return err
}
case SCPEndDir:
path = filepath.Clean(filepath.Join(path, ".."))
case SCPTime:
}
}
return nil
}
// receive the single file from the scp stream
func receiveFile(name string, cmd *SCPCommand, src io.Reader) error {
left := cmd.Length
os.Remove(name) // to rewrite
fp, err := os.Create(name)
if err != nil {
return err
}
defer fp.Close()
if err := fp.Chmod(os.FileMode(cmd.Mode)); err != nil {
return err
}
// TODO: refactor to io.CopyN
for left > 0 {
var max int64 = 2048
if left < max {
max = left
}
buf := make([]byte, max)
if n, err := src.Read(buf); err != nil {
return err
} else {
if _, err := fp.Write(buf[:n]); err != nil {
return err
}
left -= int64(n)
}
}
b := make([]byte, 1)
if _, err := src.Read(b); err != nil {
return err
}
if b[0] != byte(0) {
return fmt.Errorf("Expected null byte for EOF.")
}
return nil
}
// Make a directory if it doesn't exist
func maybeMakeDir(path string, mode int16) error {
if fi, err := os.Stat(path); err != nil {
if err := os.Mkdir(path, os.FileMode(mode)); err != nil {
return err
}
return nil
} else {
if !fi.IsDir() {
return fmt.Errorf("Path %s exists, but is not a directory.", path)
}
return nil
}
}
// Read a command from the SCP channel
func scpReadCommand(src *bufio.Reader) (string, error) {
buf, err := src.ReadBytes(byte('\n'))
if err != nil {
return "", err
}
return strings.TrimSpace(string(buf)), nil
}
// Parse the SCP command
func parseSCPCommand(cmd string) (*SCPCommand, error) {
parsePieces := func(cstr string, c *SCPCommand) error {
pieces := strings.SplitN(cmd, " ", 3)
if len(pieces) != 3 {
return ErrInvalidPieces
}
// Mode
if m, err := strconv.ParseInt(pieces[0][1:], 8, 16); err != nil {
return err
} else {
c.Mode = int16(m)
}
// Length
if m, err := strconv.ParseInt(pieces[1], 10, 64); err != nil {
return err
} else {
c.Length = m
}
c.Name = pieces[2]
return nil
}
info := &SCPCommand{}
switch cmd[0] {
case 'C':
info.CommandType = SCPCopy
if err := parsePieces(cmd, info); err != nil {
return nil, err
}
case 'D':
info.CommandType = SCPDir
if err := parsePieces(cmd, info); err != nil {
return nil, err
}
case 'E':
info.CommandType = SCPEndDir
case 'T':
info.CommandType = SCPTime
default:
return nil, fmt.Errorf("Unknown message type: %v", cmd[0])
}
return info, nil
}
// Acknowledge SCP message
func scpSendAck(dst io.Writer, code int, msg string) error {
buf := []byte{byte(code)}
if code > SCPFatal {
return ErrInvalidAck
}
if code > SCPOK {
buf = append(buf, []byte(msg)...)
buf = append(buf, byte('\n'))
}
return scpWriter(dst, buf)
}
// Send an error message
func scpSendError(dst io.Writer, err error) error {
buf := []byte("\x01scp: ")
buf = append(buf, []byte(err.Error())...)
buf = append(buf, byte('\n'))
return scpWriter(dst, buf)
}
// Write all bytes
func scpWriter(dst io.Writer, buf []byte) error {
sent := 0
for sent < len(buf) {
s, err := dst.Write(buf[sent:])
if err != nil {
return err
}
sent += s
}
return nil
}