/
messages.go
389 lines (319 loc) · 8.98 KB
/
messages.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
// Copyright 2014 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package sftp
import (
"fmt"
"code.google.com/p/gosshnew/ssh"
)
// SSH file transer protocol request packet types, defined in section 3,
// "General Packet Format".
const (
fxpPacketInit = iota + 1
fxpPacketVersion
fxpPacketOpen
fxpPacketClose
fxpPacketRead
fxpPacketWrite
fxpPacketLStat
fxpPacketFStat
fxpPacketSetStat
fxpPacketFSetStat
fxpPacketOpenDir
fxpPacketReadDir
fxpPacketRemove
fxpPacketMkDir
fxpPacketRmDir
fxpPacketRealPath
fxpPacketStat
fxpPacketRename
fxpPacketReadLink
fxpPacketSymLink
)
// SSH file transer protocol response packet types, defined in section 3,
// "General Packet Format".
const (
fxpPacketStatus = iota + 101
fxpPacketHandle
fxpPacketData
fxpPacketName
fxpPacketAttrs
)
// SSH file transfer protocol message types to support vendor-specific
// extentions.
const (
fxpPacketExtended = 200
fxpPacketExtendedReply = 201
)
// ider specifies functions for a struct to contain an identifier.
type ider interface {
// GetID returns the identifier of the instance.
GetID() uint32
// SetID sets the identifier of the instance.
SetID(uint32)
}
// fxpInitMsg is the first message sent from client to server in the SFTP
// protocol.
type fxpInitMsg struct {
// Version specifies the highest version number of the protocol
// implemented.
Version uint32 `sshtype:"1"`
}
// fxpVersionMsg is the response to an fxpInitMsg.
type fxpVersionMsg struct {
// Version contains the lowest of the server's and client's implemented
// version.
Version uint32 `sshtype:"2"`
// Ext may contain a list of strings representing optional extensions.
Ext []byte `ssh:"rest"`
}
// Apologies for the repetition of GetID and SetID: they are required because
// the ssh.Marshal routine can't handle pointers to ints. If it could, we could
// use a new embedded type instead of "ID uint32". Alternatively, we could
// duplicate *even more* code in client.go.
type fxpOpenMsg struct {
ID uint32 `sshtype:"3"`
Filename string
Pflags uint32
AttrData []byte `ssh:"rest"`
}
func (f *fxpOpenMsg) GetID() uint32 { return f.ID }
func (f *fxpOpenMsg) SetID(id uint32) { f.ID = id }
type fxpCloseMsg struct {
ID uint32 `sshtype:"4"`
Handle string
}
func (f *fxpCloseMsg) GetID() uint32 { return f.ID }
func (f *fxpCloseMsg) SetID(id uint32) { f.ID = id }
type fxpReadMsg struct {
ID uint32 `sshtype:"5"`
Handle string
Offset uint64
Length uint32
}
func (f *fxpReadMsg) GetID() uint32 { return f.ID }
func (f *fxpReadMsg) SetID(id uint32) { f.ID = id }
type fxpWriteMsg struct {
ID uint32 `sshtype:"6"`
Handle string
Offset uint64
Data []byte
}
func (f *fxpWriteMsg) GetID() uint32 { return f.ID }
func (f *fxpWriteMsg) SetID(id uint32) { f.ID = id }
type fxpRemoveMsg struct {
ID uint32 `sshtype:"13"`
Filename string
}
func (f *fxpRemoveMsg) GetID() uint32 { return f.ID }
func (f *fxpRemoveMsg) SetID(id uint32) { f.ID = id }
type fxpRenameMsg struct {
ID uint32 `sshtype:"18"`
OldPath string
NewPath string
}
func (f *fxpRenameMsg) GetID() uint32 { return f.ID }
func (f *fxpRenameMsg) SetID(id uint32) { f.ID = id }
type fxpMkdirMsg struct {
ID uint32 `sshtype:"14"`
Path string
AttrData []byte `ssh:"rest"`
}
func (f *fxpMkdirMsg) GetID() uint32 { return f.ID }
func (f *fxpMkdirMsg) SetID(id uint32) { f.ID = id }
type fxpRmdirMsg struct {
ID uint32 `sshtype:"15"`
Path string
}
func (f *fxpRmdirMsg) GetID() uint32 { return f.ID }
func (f *fxpRmdirMsg) SetID(id uint32) { f.ID = id }
type fxpOpenDirMsg struct {
ID uint32 `sshtype:"11"`
Path string
}
func (f *fxpOpenDirMsg) GetID() uint32 { return f.ID }
func (f *fxpOpenDirMsg) SetID(id uint32) { f.ID = id }
type fxpReadDirMsg struct {
ID uint32 `sshtype:"12"`
Handle string
}
func (f *fxpReadDirMsg) GetID() uint32 { return f.ID }
func (f *fxpReadDirMsg) SetID(id uint32) { f.ID = id }
type fxpStatMsg struct {
ID uint32 `sshtype:"17"`
Path string
}
func (f *fxpStatMsg) GetID() uint32 { return f.ID }
func (f *fxpStatMsg) SetID(id uint32) { f.ID = id }
type fxpLStatMsg struct {
ID uint32 `sshtype:"7"`
Path string
}
func (f *fxpLStatMsg) GetID() uint32 { return f.ID }
func (f *fxpLStatMsg) SetID(id uint32) { f.ID = id }
type fxpFStatMsg struct {
ID uint32 `sshtype:"8"`
Handle string
}
func (f *fxpFStatMsg) GetID() uint32 { return f.ID }
func (f *fxpFStatMsg) SetID(id uint32) { f.ID = id }
type fxpSetStatMsg struct {
ID uint32 `sshtype:"9"`
Path string
AttrData []byte `ssh:"rest"`
}
func (f *fxpSetStatMsg) GetID() uint32 { return f.ID }
func (f *fxpSetStatMsg) SetID(id uint32) { f.ID = id }
type fxpFSetStatMsg struct {
ID uint32 `sshtype:"10"`
Handle string
AttrData []byte `ssh:"rest"`
}
func (f *fxpFSetStatMsg) GetID() uint32 { return f.ID }
func (f *fxpFSetStatMsg) SetID(id uint32) { f.ID = id }
type fxpReadLinkMsg struct {
ID uint32 `sshtype:"19"`
Path string
}
func (f *fxpReadLinkMsg) GetID() uint32 { return f.ID }
func (f *fxpReadLinkMsg) SetID(id uint32) { f.ID = id }
type fxpRealPathMsg struct {
ID uint32 `sshtype:"16"`
Path string
}
func (f *fxpRealPathMsg) GetID() uint32 { return f.ID }
func (f *fxpRealPathMsg) SetID(id uint32) { f.ID = id }
type fxpSymlinkMsg struct {
ID uint32 `sshtype:"20"`
LinkPath string
TargetPath string
}
func (f *fxpSymlinkMsg) GetID() uint32 { return f.ID }
func (f *fxpSymlinkMsg) SetID(id uint32) { f.ID = id }
type posixRenameMsg struct {
ID uint32 `sshtype:"200"`
Extension string
OldPath string
NewPath string
}
func (f *posixRenameMsg) GetID() uint32 { return f.ID }
func (f *posixRenameMsg) SetID(id uint32) { f.ID = id }
// Status is a error number defined by section 7, "Responses from the Server to
// the Client".
type Status uint32
// The list of error codes defined by the protocol.
const (
OK Status = iota
eof
NoSuchFile
PermissionDenied
Failure
BadMessage
NoConnection
ConnectionLost
OpUnsupported
)
type fxpStatusResp struct {
ID uint32 `sshtype:"101"`
Status Status
Msg string
Language string
}
func (f *fxpStatusResp) GetID() uint32 { return f.ID }
func (f *fxpStatusResp) SetID(id uint32) { f.ID = id }
func (f fxpStatusResp) Error() string {
return fmt.Sprintf("sftp: %s", f.Msg)
}
type fxpHandleResp struct {
ID uint32 `sshtype:"102"`
Handle string
}
func (f *fxpHandleResp) GetID() uint32 { return f.ID }
func (f *fxpHandleResp) SetID(id uint32) { f.ID = id }
type fxpDataResp struct {
ID uint32 `sshtype:"103"`
Data []byte
}
func (f *fxpDataResp) GetID() uint32 { return f.ID }
func (f *fxpDataResp) SetID(id uint32) { f.ID = id }
type fxpNameData struct {
Filename string
Longname string
Data []byte `ssh:"rest"`
}
type fxpNameResp struct {
ID uint32 `sshtype:"104"`
Count uint32
Data []byte `ssh:"rest"`
}
func (f *fxpNameResp) GetID() uint32 { return f.ID }
func (f *fxpNameResp) SetID(id uint32) { f.ID = id }
type nameData struct {
filename string
longname string
attr *FileAttributes
}
// Names extracts the repeated name data from the Data buffer. This message
// structure is not supported by Unmarshal, so we take advantage of the "rest"
// field.
func (r *fxpNameResp) names() ([]nameData, error) {
if r.Count == 0 {
return nil, nil
}
data := r.Data
r.Data = nil
names := make([]nameData, 0, r.Count)
for len(data) > 0 {
name := fxpNameData{}
if err := ssh.Unmarshal(data, &name); err != nil {
return nil, err
}
data = name.Data
var attr *FileAttributes
var err error
attr, data, err = newFileAttributes(data)
if err != nil {
return nil, err
}
names = append(names, nameData{
filename: name.Filename,
longname: name.Longname,
attr: attr,
})
}
if len(data) != 0 {
return nil, fmt.Errorf("Expected to have 0 bytes of data left, have %d", len(data))
}
return names, nil
}
type fxpAttrsResp struct {
ID uint32 `sshtype:"105"`
AttrData []byte `ssh:"rest"`
}
func (f *fxpAttrsResp) GetID() uint32 { return f.ID }
func (f *fxpAttrsResp) SetID(id uint32) { f.ID = id }
type fxpExtendedResp struct {
ID uint32 `sshtype:"201"`
Data []byte `ssh:"rest"`
}
func (f *fxpExtendedResp) GetID() uint32 { return f.ID }
func (f *fxpExtendedResp) SetID(id uint32) { f.ID = id }
// UnexpectedMessageError results when the SSH message that was received did
// not match what the protocol specifies as the proper returned message type.
type UnexpectedMessageError struct {
expected, got uint8
}
func (u UnexpectedMessageError) Error() string {
return fmt.Sprintf("ssh: unexpected message type %d (expected %d)", u.got, u.expected)
}
// GetStatus retrieves the sftp status code inside the sftp error. The second
// returned value will be false if the given error is not an sftp error.
func GetStatus(err error) (Status, bool) {
resp, ok := err.(*fxpStatusResp)
if !ok {
return 0, ok
}
return resp.Status, true
}