-
Notifications
You must be signed in to change notification settings - Fork 15
/
authorize.go
381 lines (327 loc) · 9.22 KB
/
authorize.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
/*
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 (
"fmt"
)
//
// tacplus authorization message
// https://datatracker.ietf.org/doc/html/rfc8907#section-6
//
// AuthorRequestLen minumum length of this packet type
const AuthorRequestLen = 0x8
// AuthorRequestOption is used to inject options when creating new AuthorRequest types
type AuthorRequestOption func(*AuthorRequest)
// SetAuthorRequestMethod sets the AuthenMethod.
func SetAuthorRequestMethod(v AuthenMethod) AuthorRequestOption {
return func(a *AuthorRequest) {
a.Method = v
}
}
// SetAuthorRequestPrivLvl sets the PrivLvl
func SetAuthorRequestPrivLvl(v PrivLvl) AuthorRequestOption {
return func(a *AuthorRequest) {
a.PrivLvl = v
}
}
// SetAuthorRequestType sets the AuthenType.
func SetAuthorRequestType(v AuthenType) AuthorRequestOption {
return func(a *AuthorRequest) {
a.Type = v
}
}
// SetAuthorRequestService sets the AuthenService.
func SetAuthorRequestService(v AuthenService) AuthorRequestOption {
return func(a *AuthorRequest) {
a.Service = v
}
}
// SetAuthorRequestUser sets the AuthenUser.
func SetAuthorRequestUser(v AuthenUser) AuthorRequestOption {
return func(a *AuthorRequest) {
a.User = v
}
}
// SetAuthorRequestPort sets the AuthenPort.
func SetAuthorRequestPort(v AuthenPort) AuthorRequestOption {
return func(a *AuthorRequest) {
a.Port = v
}
}
// SetAuthorRequestRemAddr sets the AuthenRemAddr.
func SetAuthorRequestRemAddr(v AuthenRemAddr) AuthorRequestOption {
return func(a *AuthorRequest) {
a.RemAddr = v
}
}
// SetAuthorRequestArgs sets the Args.
func SetAuthorRequestArgs(v Args) AuthorRequestOption {
return func(a *AuthorRequest) {
a.Args = v
}
}
// NewAuthorRequest will create a new AuthorRequest based on the provided options
func NewAuthorRequest(opts ...AuthorRequestOption) *AuthorRequest {
a := &AuthorRequest{}
for _, opt := range opts {
opt(a)
}
return a
}
// AuthorRequest https://datatracker.ietf.org/doc/html/rfc8907#section-6.1
type AuthorRequest struct {
Method AuthenMethod
PrivLvl PrivLvl
Type AuthenType
Service AuthenService
User AuthenUser
Port AuthenPort
RemAddr AuthenRemAddr
Args Args
}
// Validate all fields on this type
func (a *AuthorRequest) Validate() error {
// validate
for _, t := range []Field{a.Method, a.PrivLvl, a.Type, a.Service, a.User, a.Port, a.RemAddr} {
if err := t.Validate(a.Type); err != nil {
return err
}
}
for _, t := range a.Args {
if err := t.Validate(nil); err != nil {
return err
}
}
return nil
}
// MarshalBinary encodes AuthroRequest into tacacs bytes
func (a *AuthorRequest) MarshalBinary() ([]byte, error) {
// validate we have good data before encoding
if err := a.Validate(); err != nil {
return nil, err
}
buf := make([]byte, 0, AuthorRequestLen+len(a.Args))
buf = append(buf, uint8(a.Method))
buf = append(buf, uint8(a.PrivLvl))
buf = append(buf, uint8(a.Type))
buf = append(buf, uint8(a.Service))
buf = append(buf, uint8(a.User.Len()))
buf = append(buf, uint8(a.Port.Len()))
buf = append(buf, uint8(a.RemAddr.Len()))
buf = append(buf, uint8(len(a.Args)))
for _, arg := range a.Args {
buf = append(buf, uint8(arg.Len()))
}
buf = append(buf, a.User...)
buf = append(buf, a.Port...)
buf = append(buf, a.RemAddr...)
for _, arg := range a.Args {
buf = append(buf, arg...)
}
return buf, nil
}
// UnmarshalBinary decodes decrypted tacacs bytes into AuthorRequest
func (a *AuthorRequest) UnmarshalBinary(data []byte) error {
if len(data) < AuthorRequestLen {
return fmt.Errorf("authorRequest size [%v] is too small for the minimum size [%v]", len(data), AuthorRequestLen)
}
a.Method = AuthenMethod(data[0])
a.PrivLvl = PrivLvl(data[1])
a.Type = AuthenType(data[2])
a.Service = AuthenService(data[3])
buf := readBuffer(data[4:])
userLen := buf.int()
portLen := buf.int()
remAddrLen := buf.int()
argCnt := buf.int()
var totalArgLen int
argLens := make([]int, 0, argCnt)
for i := 0; i < argCnt; i++ {
aLen := buf.int()
totalArgLen += aLen
argLens = append(argLens, aLen)
}
a.User = AuthenUser(buf.string(userLen))
a.Port = AuthenPort(buf.string(portLen))
a.RemAddr = AuthenRemAddr(buf.string(remAddrLen))
a.Args = make(Args, 0, argCnt)
for _, n := range argLens {
a.Args = append(a.Args, Arg(buf.string(n)))
}
// detect secret mismatch
if a.Len() != userLen+portLen+remAddrLen+totalArgLen {
return NewBadSecretErr("bad secret detected authorrequest")
}
// validate
if err := a.Validate(); err != nil {
return err
}
return nil
}
// Len will return the unmarshalled size of the component types
func (a AuthorRequest) Len() int {
sum := a.User.Len()
sum += a.Port.Len()
sum += a.RemAddr.Len()
for _, arg := range a.Args {
sum += arg.Len()
}
return sum
}
// Fields returns fields from this packet compatible with a structured logger
func (a AuthorRequest) Fields() map[string]string {
return map[string]string{
"packet-type": "AuthorRequest",
"method": a.Method.String(),
"priv-lvl": a.PrivLvl.String(),
"type": a.Type.String(),
"service": a.Service.String(),
"user": a.User.String(),
"port": a.Port.String(),
"rem-addr": a.RemAddr.String(),
"args": a.Args.String(),
}
}
// AuthorReplyLen minumum length of this packet type
const AuthorReplyLen = 0x6
// AuthorReplyOption is used to inject options when creating new AuthorRequest types
type AuthorReplyOption func(*AuthorReply)
// SetAuthorReplyStatus sets the AuthorStatus.
func SetAuthorReplyStatus(v AuthorStatus) AuthorReplyOption {
return func(a *AuthorReply) {
a.Status = v
}
}
// SetAuthorReplyArgs sets the Args.
func SetAuthorReplyArgs(args ...string) AuthorReplyOption {
return func(a *AuthorReply) {
v := make(Args, 0, len(args))
for _, arg := range args {
v = append(v, Arg(arg))
}
a.Args = v
}
}
// SetAuthorReplyServerMsg sets the AuthorServerMsg.
func SetAuthorReplyServerMsg(v string) AuthorReplyOption {
return func(a *AuthorReply) {
a.ServerMsg = AuthorServerMsg(v)
}
}
// SetAuthorReplyData sets the AuthorData.
func SetAuthorReplyData(v AuthorData) AuthorReplyOption {
return func(a *AuthorReply) {
a.Data = v
}
}
// NewAuthorReply will create a new AuthorReply based on the provided options
func NewAuthorReply(opts ...AuthorReplyOption) *AuthorReply {
a := &AuthorReply{}
for _, opt := range opts {
opt(a)
}
return a
}
// AuthorReply https://datatracker.ietf.org/doc/html/rfc8907#section-6.2
type AuthorReply struct {
Status AuthorStatus
Args Args
ServerMsg AuthorServerMsg
Data AuthorData
}
// NewAuthorReplyFromBytes decodes decrypted tacacs bytes into AuthorReply
func NewAuthorReplyFromBytes(data []byte) (*AuthorReply, error) {
t := &AuthorReply{}
return t, t.UnmarshalBinary(data)
}
// Validate all fields on this type
func (a *AuthorReply) Validate() error {
// validate
for _, t := range []Field{a.Status, a.ServerMsg, a.Data} {
if err := t.Validate(nil); err != nil {
return err
}
}
for _, t := range a.Args {
if err := t.Validate(nil); err != nil {
return err
}
}
return nil
}
// MarshalBinary encodes AuthorReply into tacacs bytes
func (a *AuthorReply) MarshalBinary() ([]byte, error) {
// validate
if err := a.Validate(); err != nil {
return nil, err
}
buf := make([]byte, 0, AuthorReplyLen)
buf = append(buf, uint8(a.Status))
buf = append(buf, uint8(len(a.Args)))
buf = appendUint16(buf, a.ServerMsg.Len())
buf = appendUint16(buf, a.Data.Len())
for _, arg := range a.Args {
buf = append(buf, uint8(arg.Len()))
}
buf = append(buf, a.ServerMsg...)
buf = append(buf, a.Data...)
for _, arg := range a.Args {
buf = append(buf, arg...)
}
return buf, nil
}
// UnmarshalBinary decodes decrypted tacacs bytes into AuthorReply
func (a *AuthorReply) UnmarshalBinary(data []byte) error {
if len(data) < AuthorReplyLen {
return fmt.Errorf("authorReply size [%v] is too small for the minimum size [%v]", len(data), AuthorReplyLen)
}
buf := readBuffer(data)
a.Status = AuthorStatus(buf.byte())
argCnt := buf.int()
serverMsgLen := buf.uint16()
dataLen := buf.uint16()
var totalArgLen int
argLens := make([]int, 0, argCnt)
for i := 0; i < argCnt; i++ {
aLen := buf.int()
totalArgLen += aLen
argLens = append(argLens, aLen)
}
a.ServerMsg = AuthorServerMsg(buf.string(serverMsgLen))
a.Data = AuthorData(buf.string(dataLen))
a.Args = make(Args, 0, argCnt)
for _, n := range argLens {
a.Args = append(a.Args, Arg(buf.string(n)))
}
// detect secret mismatch
if a.Len() != serverMsgLen+dataLen+totalArgLen {
return NewBadSecretErr("bad secret detected authorreply")
}
// validate
if err := a.Validate(); err != nil {
return err
}
return nil
}
// Len will return the unmarshalled size of the component types
func (a AuthorReply) Len() int {
sum := a.ServerMsg.Len()
sum += a.Data.Len()
for _, arg := range a.Args {
sum += arg.Len()
}
return sum
}
// Fields returns fields from this packet compatible with a structured logger
func (a AuthorReply) Fields() map[string]string {
return map[string]string{
"packet-type": "AuthorReply",
"status": a.Status.String(),
"args": a.Args.String(),
"server-msg": a.ServerMsg.String(),
"data": a.Data.String(),
}
}