-
Notifications
You must be signed in to change notification settings - Fork 0
/
tftp.go
432 lines (376 loc) · 10.4 KB
/
tftp.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
// packet contains types and functions to marshal/unmarshal TFTP packets as
// described in RFC1350, RFC2347, RFC2348, RFC2349 and RFC7440
package dit
import (
"encoding/binary"
"errors"
"fmt"
"strconv"
"strings"
"unicode/utf8"
)
// Packet is a TFTP protocol packet
type Packet interface {
opcode() Opcode
marshal() ([]byte, error)
unmarshal([]byte) error
}
// extract the opcode from a byte packet
func opcode(b []byte) Opcode {
return Opcode(binary.BigEndian.Uint16(b[0:2]))
}
// MarshalPacket marshals a binary packet into a packet structure
func Marshal(b []byte) (Packet, error) {
var p Packet
switch op := opcode(b); op {
case Rrq, Wrq:
p = &ReadWriteRequest{Opcode: op}
case Data:
p = &DataPacket{Opcode: op}
case Ack:
p = &AckPacket{Opcode: op}
case OAck:
p = &OAckPacket{Opcode: op}
case Error:
p = &ErrorPacket{Opcode: op}
default:
return nil, fmt.Errorf("opcode %d not recognized", op)
}
if err := p.unmarshal(b); err != nil {
return nil, fmt.Errorf("unmarshal packet: %w", err)
}
return p, nil
}
// UnmarshalPacket unmarshals a structured packet into its binary format
func Unmarshal(p Packet) ([]byte, error) {
if p == nil {
return nil, fmt.Errorf("cannot marshal nil packet")
}
return p.marshal()
}
func encode(op Opcode, args ...any) ([]byte, error) {
var p Packet
switch op {
case Error:
p = &ErrorPacket{
Opcode: op,
ErrorCode: args[0].(ErrorCode),
ErrMsg: args[1].(string),
}
default:
return nil, fmt.Errorf("decode for %s not implemented", op)
}
b, err := p.marshal()
if err != nil {
return nil, err
}
return b, nil
}
// A TFTP protocol opcode as specified in rfc1350 and rfc2347
type Opcode uint16
const (
Rrq Opcode = iota + 1 // A Read Request Type
Wrq // A Write Request Type
Data // Data Type
Ack // Acknowledgement Type
Error // Error Type
OAck // Optional Acknowlegdement type
)
// Optional extensions as specified in rfc2347, rfc2348, rfc2349 and rfc7440.
// The wire format of TFTP options are case insensitive null terminated strings
type Option uint8
const (
// RFC2348
Blksize Option = iota // block size option, valid values range from 8-65464 inclusive
// RFC2349
Timeout // timeout interval option, valid values range from 1-255 secs inclusive
Tsize // transfer size option, value is the size of file to be transfered
// RFC7440
//
// windowsize option. this option specifies the number of blocks to transmit
// before accepting an acknowledgment. valid values are between 1-65535
// inclusive
Windowsize
// unknown to signal the server cannot parse the null terminated option
// that it was presented
Unknown
)
// ErrInvalidOptVal is returned if the value of an option is not between the
// range of accepted values.
var ErrInvalidOptVal = errors.New("dit: invalid option value")
func ValidateOptValue(opt Option, val string) (int, error) {
valInt, err := strconv.Atoi(val)
if err != nil {
return valInt, err
}
switch opt {
case Blksize:
// valid values range from 8-65464
if valInt >= 8 && valInt <= 65464 {
return valInt, nil
}
case Timeout:
// valid values range from 1-255 secs inclusive
if valInt >= 1 && valInt <= 255 {
return valInt, nil
}
case Tsize:
return valInt, nil
case Windowsize:
// valid values are between 1-65535 inclusive
if valInt >= 1 && valInt <= 65535 {
return valInt, nil
}
}
return 0, ErrInvalidOptVal
}
// MarshalOpts mashals an option string to its Option equivalent. It returns
// Unknown if the option string is not recognized
func MarshalOpts(opt string) Option {
switch strings.ToLower(opt) {
case "blksize":
return Blksize
case "timeout":
return Timeout
case "tsize":
return Tsize
case "windowsize":
return Windowsize
default:
return Unknown
}
}
// Unmarshal convert an Option to its string equivalent. It returns "unknown" if
// the option is not recognized.
func UnmarshalOpts(opt Option) string {
switch opt {
case Blksize:
return "blksize"
case Timeout:
return "timeout"
case Tsize:
return "tsize"
case Windowsize:
return "windowsize"
default:
return "unknown"
}
}
// ReadWriteRequest is a TFTP read/write request packet as described in RFC1350,
// apendix I
type ReadWriteRequest struct {
Opcode Opcode
Filename string
Mode string
// tftp option extensions are appended to the read/write
// requests as null terminated string pairs (option => value)
Options map[Option]int
}
// loop through a byte slice and retrieve all null terminated strings as
// proper golang utf8 string values
func getNullTerminatedStrings(strs []byte) ([]string, error) {
var strVals []string
// loop only if we have atleast one null terminated character
if len(strs) >= 2 {
// loop through byte slice and pick out all the strings in it
var lastNull int
for i, s := range strs {
// if a null byte is encountered we read byte from last null position
// to new null position, and keep it in a slice for later processing
if s == 0 {
if bytes := strs[lastNull:i]; len(bytes) >= 1 {
if !utf8.Valid(bytes) {
// returns the string values extracted so far if an
// error is encountered while extracting
return strVals, fmt.Errorf("dit: filename contains illegal utf8 values, %s", bytes)
}
strVals = append(strVals, string(bytes))
lastNull = i + 1
}
}
}
}
return strVals, nil
}
func (p *ReadWriteRequest) unmarshal(b []byte) error {
strVals, err := getNullTerminatedStrings(b[2:])
if err != nil {
return err
}
// options are extensions and if there is a problem parsing one, it is not
// a reason to stop the parsing process, we continue to parse as much as
// we can and then return the errors encountered afterwards
if len(strVals) >= 2 {
// we got some filename, mode and probably options
p.Filename = strVals[0]
p.Mode = strVals[1]
if optVals := strVals[2:]; len(optVals) >= 2 {
options := make(map[Option]int)
for i := 0; i < len(optVals); i += 2 {
opt := MarshalOpts(optVals[i])
if opt == Unknown {
continue
}
var val int
val, err = ValidateOptValue(opt, optVals[i+1])
if err == nil {
options[opt] = val
}
}
// give the options to the request if we got some
if len(options) >= 1 {
p.Options = options
}
}
}
return err
}
// convert go string to null terminated string of bytes
func nullTerminate(s string) []byte {
return append([]byte(s), 0)
}
func (p *ReadWriteRequest) marshal() ([]byte, error) {
data := make([]byte, 2)
binary.BigEndian.PutUint16(data, uint16(p.Opcode))
data = append(data, nullTerminate(p.Filename)...)
data = append(data, nullTerminate(p.Mode)...)
if len(p.Options) >= 1 {
for opt, val := range p.Options {
valStr := strconv.Itoa(val)
data = append(data, nullTerminate(UnmarshalOpts(opt))...)
data = append(data, nullTerminate(valStr)...)
}
}
return data, nil
}
func (p ReadWriteRequest) opcode() Opcode {
return p.Opcode
}
// OAckPacket is an optional acknowledgement packet structure as specified in RFC2347
type OAckPacket struct {
Opcode Opcode
Options map[Option]int
}
func (OAckPacket) opcode() Opcode {
return OAck
}
func (p *OAckPacket) unmarshal(b []byte) error {
if optVals, err := getNullTerminatedStrings(b[2:]); len(optVals) >= 2 {
options := make(map[Option]int)
for i := 0; i < len(optVals); i += 2 {
opt := MarshalOpts(optVals[i])
if opt == Unknown {
continue
}
var val int
val, err = ValidateOptValue(opt, optVals[i+1])
if err == nil {
options[opt] = val
}
}
if len(options) >= 1 {
p.Options = options
}
} else if err != nil {
return err
}
return nil
}
func (p *OAckPacket) marshal() ([]byte, error) {
data := make([]byte, 2)
binary.BigEndian.PutUint16(data, uint16(p.Opcode))
if len(p.Options) >= 1 {
for opt, val := range p.Options {
data = append(data, nullTerminate(UnmarshalOpts(opt))...)
data = append(data, nullTerminate(strconv.Itoa(val))...)
}
}
return data, nil
}
// DataPacket is a TFTP data packet as described in RFC1350, apendix I
type DataPacket struct {
Opcode Opcode
BlockNumber uint16
Data []byte
}
func (DataPacket) opcode() Opcode {
return Data
}
func (p *DataPacket) unmarshal(b []byte) error {
p.BlockNumber = binary.BigEndian.Uint16(b[2:4])
if l := len(b[4:]); l > 0 {
p.Data = make([]byte, l)
if lc := copy(p.Data, b[4:]); lc != l {
return fmt.Errorf("dit: unable to copy all %d bytes", l)
}
}
return nil
}
func (p *DataPacket) marshal() ([]byte, error) {
data := make([]byte, 4, len(p.Data)+4)
binary.BigEndian.PutUint16(data[0:2], uint16(p.Opcode))
binary.BigEndian.PutUint16(data[2:4], p.BlockNumber)
return append(data, p.Data...), nil
}
// AckPacket is a TFTP acknowledgement packet as described in RFC1350,apendix I
type AckPacket struct {
Opcode Opcode
BlockNumber uint16
}
func (AckPacket) opcode() Opcode {
return Ack
}
func (p *AckPacket) unmarshal(b []byte) error {
p.BlockNumber = binary.BigEndian.Uint16(b[2:4])
return nil
}
func (p *AckPacket) marshal() ([]byte, error) {
data := make([]byte, 4)
binary.BigEndian.PutUint16(data[0:2], uint16(p.Opcode))
binary.BigEndian.PutUint16(data[2:4], p.BlockNumber)
return data, nil
}
// ErrorCode represents a TFTP error code as specified in RFC1350, apendix I
type ErrorCode uint16
// TFTP error code constants as specified in RFC1350, apendix I
const (
NotDefined ErrorCode = iota
FileNotFound
AccessViolation
DiskFull
IllegalOperation
UnknownTID
FileAlreadyExists
NoSuchUser
// RequestDenied was introduced in the tftp optional extension rfc2347. with
// code "8" it is used to terminate a connection during option negotiation
RequestDenied
)
// ErrorPacket is a TFTP error packet as described in RFC1350,apendix I
type ErrorPacket struct {
Opcode Opcode
ErrorCode ErrorCode
ErrMsg string
}
func (ErrorPacket) opcode() Opcode {
return Error
}
func (p *ErrorPacket) unmarshal(b []byte) error {
p.ErrorCode = ErrorCode(binary.BigEndian.Uint16(b[2:4]))
if strVals, err := getNullTerminatedStrings(b[4:]); len(strVals) >= 1 {
p.ErrMsg = strings.Join(strVals, " ")
if err != nil {
return err
}
} else if err != nil {
return err
}
return nil
}
func (p *ErrorPacket) marshal() ([]byte, error) {
data := make([]byte, 4, len(p.ErrMsg)+5)
binary.BigEndian.PutUint16(data[0:2], uint16(p.Opcode))
binary.BigEndian.PutUint16(data[2:4], uint16(p.ErrorCode))
data = append(data, nullTerminate(p.ErrMsg)...)
return data, nil
}