-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.go
177 lines (166 loc) · 4.98 KB
/
search.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
package ldapserver
import "bytes"
// SearchRequest ::= [APPLICATION 3] SEQUENCE {
// baseObject LDAPDN,
// scope ENUMERATED {
// baseObject (0),
// singleLevel (1),
// wholeSubtree (2),
// ... },
// derefAliases ENUMERATED {
// neverDerefAliases (0),
// derefInSearching (1),
// derefFindingBaseObj (2),
// derefAlways (3) },
// sizeLimit INTEGER (0 .. maxInt),
// timeLimit INTEGER (0 .. maxInt),
// typesOnly BOOLEAN,
// filter Filter,
// attributes AttributeSelection }
type SearchRequest struct {
BaseObject string
Scope SearchScope
DerefAliases AliasDerefType
SizeLimit uint32
TimeLimit uint32
TypesOnly bool
Filter *Filter
Attributes []string
}
type SearchScope uint8
const (
SearchScopeBaseObject SearchScope = 0
SearchScopeSingleLevel SearchScope = 1
SearchScopeWholeSubtree SearchScope = 2
// Defined in a draft, not always supported
SearchScopeSubordinateSubtree SearchScope = 3
// extensible, more possible
)
type AliasDerefType uint8
const (
AliasDerefNever AliasDerefType = 0
AliasDerefInSearching AliasDerefType = 1
AliasDerefFindingBaseObj AliasDerefType = 2
AliasDerefAlways AliasDerefType = 3
)
// SearchResultReference ::= [APPLICATION 19] SEQUENCE
//
// SIZE (1..MAX) OF uri URI
type SearchResultReference []string
// SearchResultEntry ::= [APPLICATION 4] SEQUENCE {
// objectName LDAPDN,
// attributes PartialAttributeList }
//
// PartialAttributeList ::= SEQUENCE OF partialAttribute PartialAttribute
type SearchResultEntry struct {
ObjectName string
Attributes []Attribute
}
func GetSearchRequest(data []byte) (*SearchRequest, error) {
seq, err := BerGetSequence(data)
if err != nil {
return nil, err
}
if len(seq) != 8 {
return nil, ErrWrongSequenceLength.WithInfo("SearchRequest sequence length", len(seq))
}
if seq[0].Type != BerTypeOctetString {
return nil, ErrWrongElementType.WithInfo("SearchRequest baseObject type", seq[0].Type)
}
baseObject := BerGetOctetString(seq[0].Data)
if seq[1].Type != BerTypeEnumerated {
return nil, ErrWrongElementType.WithInfo("SearchRequest scope type", seq[1].Type)
}
scope, err := BerGetEnumerated(seq[1].Data)
if err != nil {
return nil, err
}
if scope < 0 || scope > 128 {
return nil, ErrIntegerTooLarge.WithInfo("SearchRequest scope", scope)
}
if seq[2].Type != BerTypeEnumerated {
return nil, ErrWrongElementType.WithInfo("SearchRequest derefAliases type", seq[2].Type)
}
aliasderef, err := BerGetEnumerated(seq[2].Data)
if err != nil {
return nil, err
}
if aliasderef < 0 || aliasderef > 3 {
return nil, ErrIntegerTooLarge.WithInfo("SearchRequest derefAliases", aliasderef)
}
if seq[3].Type != BerTypeInteger {
return nil, ErrWrongElementType.WithInfo("SearchRequest sizeLimit type", seq[3].Type)
}
sizeLimit, err := BerGetInteger(seq[3].Data)
if err != nil {
return nil, err
}
if sizeLimit < 0 || sizeLimit > maxInt {
return nil, ErrIntegerTooLarge.WithInfo("SearchRequest sizeLimit", sizeLimit)
}
if seq[4].Type != BerTypeInteger {
return nil, ErrWrongElementType.WithInfo("SearchRequest timeLimit type", seq[4].Type)
}
timeLimit, err := BerGetInteger(seq[4].Data)
if err != nil {
return nil, err
}
if timeLimit < 0 || timeLimit > maxInt {
return nil, ErrIntegerTooLarge.WithInfo("SearchRequest timeLimit", timeLimit)
}
if seq[5].Type != BerTypeBoolean {
return nil, ErrWrongElementType.WithInfo("SearchRequest typesOnly type", seq[5].Type)
}
typesOnly, err := BerGetBoolean(seq[5].Data)
if err != nil {
return nil, err
}
filter, err := GetFilter(seq[6])
if err != nil {
return nil, err
}
if seq[7].Type != BerTypeSequence {
return nil, ErrWrongElementType.WithInfo("SearchRequest attributes type", seq[7].Type)
}
attrs_seq, err := BerGetSequence(seq[7].Data)
if err != nil {
return nil, err
}
var attrs []string
for _, a := range attrs_seq {
if a.Type != BerTypeOctetString {
return nil, ErrWrongElementType.WithInfo("SearchRequest attribute type", a.Type)
}
attrs = append(attrs, BerGetOctetString(a.Data))
}
req := &SearchRequest{
BaseObject: baseObject,
Scope: SearchScope(scope),
DerefAliases: AliasDerefType(aliasderef),
SizeLimit: uint32(sizeLimit),
TimeLimit: uint32(timeLimit),
TypesOnly: typesOnly,
Filter: filter,
Attributes: attrs,
}
return req, nil
}
// Return the BER-encoded sequence (without element header)
func (s SearchResultReference) Encode() []byte {
b := bytes.NewBuffer(nil)
for _, r := range s {
b.Write(BerEncodeOctetString(r))
}
return b.Bytes()
}
// Return the BER-encoded struct (without element header)
func (s *SearchResultEntry) Encode() []byte {
b := bytes.NewBuffer(nil)
b.Write(BerEncodeOctetString(s.ObjectName))
ab := bytes.NewBuffer(nil)
for _, attr := range s.Attributes {
ab.Write(BerEncodeSequence(attr.Encode()))
}
b.Write(BerEncodeSequence(ab.Bytes()))
return b.Bytes()
}