-
Notifications
You must be signed in to change notification settings - Fork 16
/
header_fields.go
244 lines (206 loc) · 6.72 KB
/
header_fields.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
/*
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
package tacquito
import (
"encoding/binary"
"fmt"
"strings"
)
// constants related to Version
const (
// MajorVersion is the major TACACS version number.
MajorVersion = 0xc
// MinorVersionDefault is TACACS
MinorVersionDefault = 0x0
// MinorVersionOne is TACACS+
MinorVersionOne = 0x1
// HeaderMaxSequence if reached, connection must terminate and start at 1 again
HeaderMaxSequence = 255
)
// Version stores MajorVersion and MinorVersion in a single uint8. The upper 4 bits
// hold major, and the lower 4 bits hold minor. a uint8 is used to represent both
// at the struct level, but are bitshifted into a single uint8 during MarshalBinary
// and unshifted at UnmarshalBinary
type Version struct {
MajorVersion uint8
MinorVersion uint8
}
// Validate known constants
func (v Version) Validate(condition interface{}) error {
if v.MajorVersion != MajorVersion {
return fmt.Errorf("invalid MajorVersion: [%v]", v.MajorVersion)
}
switch v.MinorVersion {
case MinorVersionDefault, MinorVersionOne:
return nil
default:
return fmt.Errorf("invalid MinorVersion: [%v]", v.MinorVersion)
}
}
// Len of Version value.
func (v Version) Len() int {
// a single uint8
return 1
}
// String maps Version to string representation.
func (v Version) String() string {
return fmt.Sprintf("major:%d,minor:%d", v.MajorVersion, v.MinorVersion)
}
// MarshalBinary encodes T into a wire format
func (v *Version) MarshalBinary() ([]byte, error) {
if err := v.Validate(nil); err != nil {
return nil, err
}
return []byte{v.MajorVersion<<4 | v.MinorVersion}, nil
}
// UnmarshalBinary decodes T from a wire format into struct values
func (v *Version) UnmarshalBinary(data []byte) error {
v.MajorVersion = data[0] >> 4
v.MinorVersion = data[0] & 0xf
return nil
}
// SessionID is the Id for a TACACS+ session. This field does not change for the
// duration of the TACACS+ session. This number MUST be generated by a
// cryptographically strong random number generation method.
type SessionID uint32
// MarshalBinary just returns a uint32 in bytes, BigEndian byte order
func (s *SessionID) MarshalBinary() ([]byte, error) {
sb := make([]byte, 4)
binary.BigEndian.PutUint32(sb, uint32(*s))
return sb, nil
}
// UnmarshalBinary populates data parameter with Uint32 in BigEndian byte order
func (s *SessionID) UnmarshalBinary(data []byte) error {
binary.BigEndian.PutUint32(data, uint32(*s))
return nil
}
// String converts SessionID to a string
func (s SessionID) String() string {
return fmt.Sprint(uint32(s))
}
// HeaderType indicates the type of tacacs packet contained in the bytes that follow.
type HeaderType uint8
const (
// Authenticate per rfc
Authenticate HeaderType = 0x01
// Authorize per rfc
Authorize HeaderType = 0x02
// Accounting per rfc
Accounting HeaderType = 0x03
)
// Validate characterics of type based on rfc and usage.
func (t HeaderType) Validate(condition interface{}) error {
switch t {
case Authenticate, Authorize, Accounting:
return nil
}
return fmt.Errorf("unknown HeaderType value [%v]", t)
}
// Len returns the length of HeaderType.
func (t HeaderType) Len() int {
return 1
}
// String returns HeaderType as a string.
func (t HeaderType) String() string {
switch t {
case Authenticate:
return "Authenticate"
case Authorize:
return "Authorize"
case Accounting:
return "Accounting"
}
return fmt.Sprintf("unknown HeaderType[%d]", uint8(t))
}
// SequenceNumber is the sequence number of the current packet. The first packet
// in a session MUST have the sequence number 1 and each subsequent
// packet will increment the sequence number by one. Thus clients only
// send packets containing odd sequence numbers, and TACACS+ servers
// only send packets containing even sequence numbers.
//
// The sequence number must never wrap i.e. if the sequence number 2^8-1
// is ever reached, that session must terminate and be restarted with a
// sequence number of 1.
type SequenceNumber uint16
// Len returns the length of SequenceNumber.
func (t SequenceNumber) Len() int {
return 1
}
// String returns SequenceNumber as a string.
func (t SequenceNumber) String() string {
return fmt.Sprint(uint8(t))
}
// Inc will return the next seqno as an int
func (t SequenceNumber) Inc() int {
return int(t) + 1
}
// Validate Sequence Number.
func (t SequenceNumber) Validate(condition interface{}) error {
switch v := uint16(t); {
case v < 1:
return fmt.Errorf("sequence number must be greater than zero, [%v]", t)
case v > HeaderMaxSequence:
return fmt.Errorf("headerMaxSequence exceeded [%v]", t)
}
return nil
}
// ClientSequenceNumber is used when we want to consider sequences that the client
// sends and validate that they are allowed
type ClientSequenceNumber uint8
// Validate ensures we don't have even sequence numbers from clients
func (t ClientSequenceNumber) Validate(condition interface{}) error {
switch v := uint8(t); {
case v%2 == 0:
return fmt.Errorf("client sent an even sequence number")
}
return nil
}
// LastSequence is used to compare the previous sequence nubmber with the
// current sequence number and validate it
type LastSequence uint8
// Validate ensures we have a sane progression of sequence numbers in a packet exchange
func (t LastSequence) Validate(condition interface{}) error {
last := uint8(t)
var current uint8
switch v := condition.(type) {
case SequenceNumber:
current = uint8(v)
default:
return fmt.Errorf("invalid type passed as a condition, it must be a SequenceNumber")
}
if last >= current {
return fmt.Errorf("the last sequence number is >= to the current sequence")
}
return nil
}
// HeaderFlag set obfuscation and connect options.
type HeaderFlag uint8
const (
// UnencryptedFlag indicates that the sender did not obfuscate the body of the packet.
// Normal tacacs pacekts have this set to 0x0.
UnencryptedFlag HeaderFlag = 0x01
// SingleConnect is used to allow a client and server to negotiate single connection mode
SingleConnect HeaderFlag = 0x04
)
// Set HeaderFlag's f bit.
func (b *HeaderFlag) Set(f HeaderFlag) { *b = *b | f }
// Clear HeaderFlag's f bit.
func (b *HeaderFlag) Clear(f HeaderFlag) { *b = *b &^ f }
// Toggle HeaderFlag's f bit.
func (b *HeaderFlag) Toggle(f HeaderFlag) { *b = *b ^ f }
// Has returns true when b has the f bit set.
func (b *HeaderFlag) Has(f HeaderFlag) bool { return *b&f != 0 }
// String to satisfy Fields interface
func (b HeaderFlag) String() string {
flags := make([]string, 0, 2) // 2 supported flags
if b.Has(UnencryptedFlag) {
flags = append(flags, "UnencryptedFlag")
}
if b.Has(SingleConnect) {
flags = append(flags, "SingleConnect")
}
return strings.Join(flags, "|")
}