-
Notifications
You must be signed in to change notification settings - Fork 4
/
request.go
377 lines (336 loc) · 11.6 KB
/
request.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
// Copyright (c) Jim Lambert
// SPDX-License-Identifier: MIT
package gldap
import (
"crypto/tls"
"errors"
"fmt"
ber "github.com/go-asn1-ber/asn1-ber"
)
// ExtendedOperationName is an extended operation request/response name
type ExtendedOperationName string
// Extended operation response/request names
const (
ExtendedOperationDisconnection ExtendedOperationName = "1.3.6.1.4.1.1466.2003"
ExtendedOperationCancel ExtendedOperationName = "1.3.6.1.1.8"
ExtendedOperationStartTLS ExtendedOperationName = "1.3.6.1.4.1.1466.20037"
ExtendedOperationWhoAmI ExtendedOperationName = "1.3.6.1.4.1.4203.1.11.3"
ExtendedOperationGetConnectionID ExtendedOperationName = "1.3.6.1.4.1.26027.1.6.2"
ExtendedOperationPasswordModify ExtendedOperationName = "1.3.6.1.4.1.4203.1.11.1"
ExtendedOperationUnknown ExtendedOperationName = "Unknown"
)
// Request represents an ldap request
type Request struct {
// ID is the request number for a specific connection. Every connection has
// its own request counter which starts at 1.
ID int
// conn is needed this for cancellation among other things.
conn *conn
message Message
routeOp routeOperation
extendedName ExtendedOperationName
}
func newRequest(id int, c *conn, p *packet) (*Request, error) {
const op = "gldap.newRequest"
if c == nil {
return nil, fmt.Errorf("%s: missing connection: %w", op, ErrInvalidParameter)
}
if p == nil {
return nil, fmt.Errorf("%s: missing packet: %w", op, ErrInvalidParameter)
}
m, err := newMessage(p)
if err != nil {
return nil, fmt.Errorf("%s: unable to build message for request %d: %w", op, id, err)
}
var extendedName ExtendedOperationName
var routeOp routeOperation
switch v := m.(type) {
case *SimpleBindMessage:
routeOp = bindRouteOperation
case *SearchMessage:
routeOp = searchRouteOperation
case *ExtendedOperationMessage:
routeOp = extendedRouteOperation
extendedName = v.Name
case *ModifyMessage:
routeOp = modifyRouteOperation
case *AddMessage:
routeOp = addRouteOperation
case *DeleteMessage:
routeOp = deleteRouteOperation
case *UnbindMessage:
routeOp = unbindRouteOperation
default:
// this should be unreachable, since newMessage defaults to returning an
// *ExtendedOperationMessage
return nil, fmt.Errorf("%s: %v is an unsupported route operation: %w", op, v, ErrInternal)
}
r := &Request{
ID: id,
conn: c,
message: m,
routeOp: routeOp,
extendedName: extendedName,
}
return r, nil
}
// ConnectionID returns the request's connection ID which enables you to know
// "who" (i.e. which connection) made a request. Using the connection ID you
// can do things like ensure a connection performing a search operation has
// successfully authenticated (a.k.a. performed a successful bind operation).
func (r *Request) ConnectionID() int {
return r.conn.connID
}
// NewModifyResponse creates a modify response
// Supported options: WithResponseCode, WithDiagnosticMessage, WithMatchedDN
func (r *Request) NewModifyResponse(opt ...Option) *ModifyResponse {
opts := getResponseOpts(opt...)
return &ModifyResponse{
GeneralResponse: r.NewResponse(
WithApplicationCode(ApplicationModifyResponse),
WithResponseCode(*opts.withResponseCode),
WithDiagnosticMessage(opts.withDiagnosticMessage),
WithMatchedDN(opts.withMatchedDN),
),
}
}
// StartTLS will start a TLS connection using the Message's existing connection
func (r *Request) StartTLS(tlsconfig *tls.Config) error {
const op = "gldap.(Message).StartTLS"
if tlsconfig == nil {
return fmt.Errorf("%s: missing tls configuration: %w", op, ErrInvalidParameter)
}
tlsConn := tls.Server(r.conn.netConn, tlsconfig)
if err := tlsConn.Handshake(); err != nil {
return fmt.Errorf("%s: handshake error: %w", op, err)
}
if err := r.conn.initConn(tlsConn); err != nil {
return fmt.Errorf("%s: %w", op, err)
}
return nil
}
// NewResponse creates a general response (not necessarily to any specific
// request because you can set WithApplicationCode).
// Supported options: WithResponseCode, WithApplicationCode,
// WithDiagnosticMessage, WithMatchedDN
func (r *Request) NewResponse(opt ...Option) *GeneralResponse {
const op = "gldap.NewResponse" // nolint:unused
opts := getResponseOpts(opt...)
if opts.withResponseCode == nil {
opts.withResponseCode = intPtr(ResultUnwillingToPerform)
}
if opts.withApplicationCode == nil {
opts.withApplicationCode = intPtr(ApplicationExtendedResponse)
}
return &GeneralResponse{
baseResponse: &baseResponse{
messageID: r.message.GetID(),
code: int16(*opts.withResponseCode),
diagMessage: opts.withDiagnosticMessage,
matchedDN: opts.withMatchedDN,
},
applicationCode: *opts.withApplicationCode,
}
}
// NewExtendedResponse creates a new extended response.
// Supported options: WithResponseCode
func (r *Request) NewExtendedResponse(opt ...Option) *ExtendedResponse {
const op = "gldap.NewExtendedResponse" // nolint:unused
opts := getResponseOpts(opt...)
resp := &ExtendedResponse{
baseResponse: &baseResponse{
messageID: r.message.GetID(),
},
}
if opts.withResponseCode != nil {
resp.code = int16(*opts.withResponseCode)
}
return resp
}
// NewBindResponse creates a new bind response.
// Supported options: WithResponseCode
func (r *Request) NewBindResponse(opt ...Option) *BindResponse {
const op = "gldap.NewBindResponse" // nolint:unused
opts := getResponseOpts(opt...)
resp := &BindResponse{
baseResponse: &baseResponse{
messageID: r.message.GetID(),
},
}
if opts.withResponseCode != nil {
resp.code = int16(*opts.withResponseCode)
}
return resp
}
// GetSimpleBindMessage retrieves the SimpleBindMessage from the request, which
// allows you handle the request based on the message attributes.
func (r *Request) GetSimpleBindMessage() (*SimpleBindMessage, error) {
const op = "gldap.(Request).GetSimpleBindMessage"
s, ok := r.message.(*SimpleBindMessage)
if !ok {
return nil, fmt.Errorf("%s: %T not a simple bind request: %w", op, r.message, ErrInvalidParameter)
}
return s, nil
}
// NewSearchDoneResponse creates a new search done response. If there are no
// results found, then set the response code by adding the option
// WithResponseCode(ResultNoSuchObject)
//
// Supported options: WithResponseCode
func (r *Request) NewSearchDoneResponse(opt ...Option) *SearchResponseDone {
const op = "gldap.(Request).NewSearchDoneResponse" // nolint:unused
opts := getResponseOpts(opt...)
resp := &SearchResponseDone{
baseResponse: &baseResponse{
messageID: r.message.GetID(),
},
}
if opts.withResponseCode != nil {
resp.code = int16(*opts.withResponseCode)
}
return resp
}
// GetSearchMessage retrieves the SearchMessage from the request, which
// allows you handle the request based on the message attributes.
func (r *Request) GetSearchMessage() (*SearchMessage, error) {
const op = "gldap.(Request).GetSearchMessage"
m, ok := r.message.(*SearchMessage)
if !ok {
return nil, fmt.Errorf("%s: %T not a search request: %w", op, r.message, ErrInvalidParameter)
}
return m, nil
}
// NewSearchResponseEntry is a search response entry.
// Supported options: WithAttributes
func (r *Request) NewSearchResponseEntry(entryDN string, opt ...Option) *SearchResponseEntry {
opts := getResponseOpts(opt...)
newAttrs := make([]*EntryAttribute, 0, len(opts.withAttributes))
for name, values := range opts.withAttributes {
newAttrs = append(newAttrs, NewEntryAttribute(name, values))
}
return &SearchResponseEntry{
baseResponse: &baseResponse{
messageID: r.message.GetID(),
},
entry: Entry{
DN: entryDN,
Attributes: newAttrs,
},
}
}
// GetModifyMessage retrieves the ModifyMessage from the request, which
// allows you handle the request based on the message attributes.
func (r *Request) GetModifyMessage() (*ModifyMessage, error) {
const op = "gldap.(Request).GetModifyMessage"
m, ok := r.message.(*ModifyMessage)
if !ok {
return nil, fmt.Errorf("%s: %T not a modify request: %w", op, r.message, ErrInvalidParameter)
}
return m, nil
}
// GetAddMessage retrieves the AddMessage from the request, which
// allows you handle the request based on the message attributes.
func (r *Request) GetAddMessage() (*AddMessage, error) {
const op = "gldap.(Request).GetAddMessage"
m, ok := r.message.(*AddMessage)
if !ok {
return nil, fmt.Errorf("%s: %T not a add request: %w", op, r.message, ErrInvalidParameter)
}
return m, nil
}
// GetDeleteMessage retrieves the DeleteMessage from the request, which
// allows you handle the request based on the message attributes.
func (r *Request) GetDeleteMessage() (*DeleteMessage, error) {
const op = "gldap.(Request).GetDeleteMessage"
m, ok := r.message.(*DeleteMessage)
if !ok {
return nil, fmt.Errorf("%s: %T not a delete request: %w", op, r.message, ErrInvalidParameter)
}
return m, nil
}
// GetUnbindMessage retrieves the UnbindMessage from the request, which
// allows you handle the request based on the message attributes.
func (r *Request) GetUnbindMessage() (*UnbindMessage, error) {
const op = "gldap.(Request).GetUnbindMessage"
m, ok := r.message.(*UnbindMessage)
if !ok {
return nil, fmt.Errorf("%s: %T not an unbind request: %w", op, r.message, ErrInvalidParameter)
}
return m, nil
}
// ConvertString will convert an ASN1 BER Octet string into a "native" go
// string. Support ber string encoding types: OctetString, GeneralString and
// all other types will return an error.
func ConvertString(octetString ...string) ([]string, error) {
const (
op = "gldap.ConvertOctetString"
berTagIdx = 0
startOfDataIdx = 1
)
converted := make([]string, 0, len(octetString))
for _, s := range octetString {
data := []byte(s)
switch {
case
ber.Tag(data[berTagIdx]) == ber.TagOctetString,
ber.Tag(data[berTagIdx]) == ber.TagGeneralString:
_, strDataLen, err := readLength(data[startOfDataIdx:])
if err != nil {
return nil, err
}
converted = append(converted, string(data[(startOfDataIdx+strDataLen):]))
default:
return nil, fmt.Errorf("%s: unsupported ber encoding type %s: %w", op, string(data[berTagIdx]), ErrInvalidParameter)
}
}
return converted, nil
}
// readLength(...)
// jimlambrt: 2/2023
// copied directly from github.com/go-asn1-ber/[email protected]/length.go
// it has an MIT license: https://github.com/go-asn1-ber/asn1-ber/blob/master/LICENSE
func readLength(bytes []byte) (length int, read int, err error) {
// length byte
b := bytes[0]
read++
switch {
case b == 0xFF:
// Invalid 0xFF (x.600, 8.1.3.5.c)
return 0, read, errors.New("invalid length byte 0xff")
case b == ber.LengthLongFormBitmask:
// Indefinite form, we have to decode packets until we encounter an EOC packet (x.600, 8.1.3.6)
length = ber.LengthIndefinite
case b&ber.LengthLongFormBitmask == 0:
// Short definite form, extract the length from the bottom 7 bits (x.600, 8.1.3.4)
length = int(b) & ber.LengthValueBitmask
case b&ber.LengthLongFormBitmask != 0:
// Long definite form, extract the number of length bytes to follow from the bottom 7 bits (x.600, 8.1.3.5.b)
lengthBytes := int(b) & ber.LengthValueBitmask
// Protect against overflow
// TODO: support big int length?
if lengthBytes > 8 {
return 0, read, errors.New("long-form length overflow")
}
// Accumulate into a 64-bit variable
var length64 int64
for i := 0; i < lengthBytes; i++ {
b = bytes[read]
read++
// x.600, 8.1.3.5
length64 <<= 8
length64 |= int64(b)
}
// Cast to a platform-specific integer
length = int(length64)
// Ensure we didn't overflow
if int64(length) != length64 {
return 0, read, errors.New("long-form length overflow")
}
default:
return 0, read, errors.New("invalid length byte")
}
return length, read, nil
}
func intPtr(i int) *int {
return &i
}