This repository has been archived by the owner on Nov 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
messages.go
454 lines (404 loc) · 9.76 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
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package ldap
import (
"errors"
"fmt"
"github.com/jmckaskill/asn1"
"reflect"
)
type message struct {
Id uint32
Data asn1.RawValue
}
type result struct {
Code asn1.Enumerated
DN []byte
Msg []byte
Referrals [][]byte `asn1:"optional,tag:3"`
SaslCredentials []byte `asn1:"optional,tag:7"`
ExtendedName []byte `asn1:"optional,tag:10"`
ExtendedValue []byte `asn1:"optional,tag:11"`
}
type bindRequest struct {
Version int
BindDN []byte
Auth asn1.RawValue
}
type saslCredentials struct {
Mechanism []byte
Credentials []byte `asn1:"optional"`
}
type searchRequest struct {
BaseObject []byte
Scope asn1.Enumerated
DerefAliases asn1.Enumerated
SizeLimit int
TimeLimit int
TypesOnly bool
Filter asn1.RawValue
Attrs [][]byte
}
const (
baseObjectScope asn1.Enumerated = iota
singleLevelScope
wholeSubtreeScope
)
const (
NeverDeferAliases asn1.Enumerated = iota
DerefInSearching
DerefFindingBaseObj
DerefAlways
)
type filterSet struct {
Filters []asn1.RawValue `"asn1:set"`
}
type assertion struct {
Attr []byte
Value []byte
}
type substringFilter struct {
Desc []byte
Substrings []asn1.RawValue
}
type attribute struct {
Desc []byte
Vals [][]byte `asn1:"set"`
}
type searchEntry struct {
DN []byte
Attrs []attribute
}
type searchReference [][]byte
type modifyRequest struct {
DN []byte
Ops []change
}
type change struct {
Op asn1.Enumerated
Attr []attribute
}
const (
addOp asn1.Enumerated = iota
deleteOp
replaceOp
)
type addRequest struct {
DN []byte
Attr []attribute
}
type deleteRequest []byte
type modifyDNRequest struct {
OldDN []byte
NewDN []byte // note this can be relative
DeleteOld bool
NewSuperior []byte `asn1:"explicit,tag:0,optional"`
}
type compareRequest struct {
DN []byte
Ava AttributeValueAssertion
}
type abandonRequest int
type extendedMessage struct {
Name []byte `asn1:"explicit,optional,tag:0"`
Value []byte `asn1:"explicit,optional,tag:1"`
}
const (
ldapVersion = 3
bindRequestTag = 0 // bindRequest
bindRequestParam = "application,tag:0"
bindResultTag = 1 // result
bindResultParam = "application,tag:1"
unbindRequestTag = 2 // nil
searchRequestTag = 3 // searchRequest
searchRequestParam = "application,tag:3"
searchEntryTag = 4 // searchEntry
searchEntryParam = "application,tag:4"
searchReferenceTag = 19 // searchReference
searchDoneTag = 5 // result
searchDoneParam = "application,tag:5"
modifyRequestTag = 6 // modifyRequest
modifyResultTag = 7 // result
addRequestTag = 8 // addRequest
addResultTag = 9 // result
deleteRequestTag = 10 // deleteRequest
deleteResultTag = 11 // result
modifyDNRequestTag = 12 // modifyDNRequest
modifyDNResultTag = 13 // result
compareRequestTag = 14 // compareRequest
compareResultTag = 15 // result
abandonRequestTag = 16 // abandonRequest
abandonRequestParam = "application,tag:16"
extendedRequestTag = 23 // extendedMessage
extendedRequestParam = "application,tag:23"
extendedResultTag = 24 // result
extendedResultParam = "application,tag:24"
intermediatTag = 25 // extendedMessage
simpleBindParam = "tag:0" // []byte
saslBindParam = "tag:3" // saslCredentials
andParam = "tag:0" // filterSet
orParam = "tag:1" // filterSet
notParam = "tag:2" // filter (asn1.RawValue)
equalParam = "tag:3" // AttributeValueAssertion
substringParam = "tag:4" // substringFilter
greaterOrEqualParam = "tag:5" // AttributeValueAssertion
lessOrEqualParam = "tag:6" // AttributeValueAssertion
presentParam = "tag:7" // []byte
approxEqualParam = "tag:8" // AttributeValueAssertion
extensibleMatchParam = "tag:9" // matchingRuleAssertion
substringInitialParam = "tag:0" // []byte
substringAnyParam = "tag:1"
substringFinalParam = "tag:2"
classApplication = 1
universalSequenceTag = 0x30
maxMessageSize = 16 * 1024 * 1024
)
var (
noAttributes = []byte("1.1")
startTLS = "1.3.6.1.4.1.1466.20037"
)
type LdapResultCode int
const (
SuccessError LdapResultCode = iota
OperationsError
ProtocolError
TimeLimitExceeded
SizeLimitExceeded
CompareFalse
CompareTrue
AuthMethodNotSupported
StrongerAuthRequired
_
ReferralError
AdminLimitExceeded
UnavailableCriticalExtension
ConfidentialityRequired
SaslBindInProgress
_
NoSuchAttribute
UndefinedAttributeType
InappropriateMatching
ConstraintViolation
AttributeOrValueExists
InvalidAttributeSyntax
_
_
_
_
_
_
_
_
_
_
NoSuchObject
AliasProblem
InvalidDNSyntax
_
AliasDereferncingProblem
_
_
_
_
_
_
_
_
_
_
_
InappropriateAuthentication
InvalidCredentials
InsufficientAccessRights
Busy
Unavailable
UnwillingToPerform
LoopDetect
_
_
_
_
_
_
_
_
_
NamingViolation
ObjectClassViolation
NotAllowedOnNonLeaf
NotAllowedOnRDN
EntryAlreadyExists
ObjectClassModeProhibited
_
AffectsMultipleDSAs
_
_
_
_
_
_
_
_
Other
)
func (s LdapResultCode) String() string {
switch s {
case SuccessError:
return "success"
case OperationsError:
return "operations error"
case ProtocolError:
return "protocol error"
case TimeLimitExceeded:
return "time limit exceeded"
case SizeLimitExceeded:
return "size limit exceeded"
case CompareFalse:
return "compare false"
case CompareTrue:
return "compare true"
case AuthMethodNotSupported:
return "auth method not supported"
case StrongerAuthRequired:
return "stronger auth required"
case ReferralError:
return "referral"
case AdminLimitExceeded:
return "admin limit exceeded"
case UnavailableCriticalExtension:
return "unavailble critical extension"
case ConfidentialityRequired:
return "confidentiality required"
case SaslBindInProgress:
return "sasl bind in progress"
case NoSuchAttribute:
return "no such attribute"
case UndefinedAttributeType:
return "undefined attribute type"
case InappropriateMatching:
return "inappropriate matching"
case ConstraintViolation:
return "constraint violation"
case AttributeOrValueExists:
return "attribute or value exists"
case InvalidAttributeSyntax:
return "invalid attribute syntax"
case NoSuchObject:
return "no such Object"
case AliasProblem:
return "alias problem"
case InvalidDNSyntax:
return "invalid DN syntax"
case AliasDereferncingProblem:
return "alias dereferencing problem"
case InappropriateAuthentication:
return "inappropriate authentication"
case InvalidCredentials:
return "invalid credentials"
case InsufficientAccessRights:
return "insufficient access rights"
case Busy:
return "busy"
case Unavailable:
return "unavailable"
case UnwillingToPerform:
return "unwilling to perform"
case LoopDetect:
return "loop detect"
case NamingViolation:
return "naming violation"
case ObjectClassViolation:
return "object class violation"
case NotAllowedOnNonLeaf:
return "not allowed on non-leaf"
case NotAllowedOnRDN:
return "not allowed on RDN"
case EntryAlreadyExists:
return "entry already exists"
case ObjectClassModeProhibited:
return "object class mode prohibited"
case AffectsMultipleDSAs:
return "affects multiple DSAs"
case Other:
return "other"
}
return fmt.Sprintf("unknown error %d", int(s))
}
var (
ErrAuthNotSupported = errors.New("ldap: auth not supported")
ErrProtocol = errors.New("ldap: protocol error")
ErrInvalidSID = errors.New("ldap: unable to parse SID")
ErrIncompleteAuth = errors.New("ldap: incomplete auth")
ErrClosed = errors.New("ldap: db closed")
ErrNotFound = errors.New("ldap: not found")
ErrShortRead = errors.New("ldap: short SASL read")
)
type ErrLdap struct {
res *result
}
func (e ErrLdap) Error() string {
return fmt.Sprintf("ldap: remote error (%s) %s", LdapResultCode(e.res.Code).String(), string(e.res.Msg))
}
type ErrUnsupportedType struct {
typ reflect.Type
}
func (e ErrUnsupportedType) Error() string {
return fmt.Sprintf("ldap: unsupported type %s", e.typ.String())
}
type Filter interface {
marshal() ([]byte, error)
}
type AttributeValueAssertion struct {
// attribute can be followed by zero or more options using ;
// seperators
Attr string
Value []byte
}
type And []Filter
type Or []Filter
type Not struct {
Filter Filter
}
type Equal AttributeValueAssertion
type ApproxEqual AttributeValueAssertion
type GreaterOrEqual AttributeValueAssertion
type LessOrEqual AttributeValueAssertion
type Present string
func (a And) marshal() (data []byte, err error) {
filters := make([]asn1.RawValue, len(a))
for i, f := range a {
if filters[i].FullBytes, err = f.marshal(); err != nil {
return nil, err
}
}
return asn1.MarshalWithParams(filterSet{filters}, andParam)
}
func (o Or) marshal() (data []byte, err error) {
filters := make([]asn1.RawValue, len(o))
for i, f := range o {
if filters[i].FullBytes, err = f.marshal(); err != nil {
return nil, err
}
}
return asn1.MarshalWithParams(filterSet{filters}, orParam)
}
func (n Not) marshal() ([]byte, error) {
f, err := n.Filter.marshal()
if err != nil {
return nil, err
}
return asn1.MarshalWithParams(asn1.RawValue{FullBytes: f}, notParam)
}
func (e Equal) marshal() ([]byte, error) {
return asn1.MarshalWithParams(assertion{[]byte(e.Attr), e.Value}, equalParam)
}
func (e ApproxEqual) marshal() ([]byte, error) {
return asn1.MarshalWithParams(assertion{[]byte(e.Attr), e.Value}, approxEqualParam)
}
func (e GreaterOrEqual) marshal() ([]byte, error) {
return asn1.MarshalWithParams(assertion{[]byte(e.Attr), e.Value}, greaterOrEqualParam)
}
func (e LessOrEqual) marshal() ([]byte, error) {
return asn1.MarshalWithParams(assertion{[]byte(e.Attr), e.Value}, lessOrEqualParam)
}
func (p Present) marshal() ([]byte, error) {
return asn1.MarshalWithParams([]byte(p), presentParam)
}