-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter.go
303 lines (293 loc) · 8.36 KB
/
filter.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
package ldapserver
import (
"bytes"
"strings"
)
// Defined filter types
const (
FilterTypeAnd uint8 = 0
FilterTypeOr uint8 = 1
FilterTypeNot uint8 = 2
FilterTypeEqual uint8 = 3
FilterTypeSubstrings uint8 = 4
FilterTypeGreaterOrEqual uint8 = 5
FilterTypeLessOrEqual uint8 = 6
FilterTypePresent uint8 = 7
FilterTypeApproxMatch uint8 = 8
FilterTypeExtensibleMatch uint8 = 9
FilterTypeAbsoluteTrue uint8 = 0xa0
FilterTypeAbsoluteFalse uint8 = 0xa1
)
// Filter ::= CHOICE {
// and [0] SET SIZE (1..MAX) OF filter Filter,
// or [1] SET SIZE (1..MAX) OF filter Filter,
// not [2] Filter,
// equalityMatch [3] AttributeValueAssertion,
// substrings [4] SubstringFilter,
// greaterOrEqual [5] AttributeValueAssertion,
// lessOrEqual [6] AttributeValueAssertion,
// present [7] AttributeDescription,
// approxMatch [8] AttributeValueAssertion,
// extensibleMatch [9] MatchingRuleAssertion,
// ... }
type Filter struct {
Type uint8
Data any
}
// SubstringFilter ::= SEQUENCE {
// type AttributeDescription,
// substrings SEQUENCE SIZE (1..MAX) OF substring CHOICE {
// initial [0] AssertionValue, -- can occur at most once
// any [1] AssertionValue,
// final [2] AssertionValue } -- can occur at most once
// }
type SubstringFilter struct {
Attribute string
Initial string
Any []string
Final string
}
// MatchingRuleAssertion ::= SEQUENCE {
// matchingRule [1] MatchingRuleId OPTIONAL,
// type [2] AttributeDescription OPTIONAL,
// matchValue [3] AssertionValue,
// dnAttributes [4] BOOLEAN DEFAULT FALSE }
type MatchingRuleAssertion struct {
MatchingRule string
Attribute string
Value string
DNAttributes bool
}
// Return a Filter from a raw BER element
func GetFilter(raw BerRawElement) (*Filter, error) {
if raw.Type.Class() != BerClassContextSpecific {
return nil, ErrWrongElementType.WithInfo("Filter type", raw.Type)
}
ftype := raw.Type.TagNumber()
switch {
case raw.Type == BerContextSpecificType(0, true) && len(raw.Data) == 0:
ftype = FilterTypeAbsoluteTrue
case raw.Type == BerContextSpecificType(1, true) && len(raw.Data) == 0:
ftype = FilterTypeAbsoluteFalse
}
f := &Filter{
Type: ftype,
}
switch ftype {
case FilterTypeAnd, FilterTypeOr:
var filters []Filter
seq, err := BerGetSequence(raw.Data)
if err != nil {
return nil, err
}
for _, rf := range seq {
filter, err := GetFilter(rf)
if err != nil {
return nil, err
}
filters = append(filters, *filter)
}
f.Data = filters
case FilterTypeNot:
elmt, err := BerReadElement(bytes.NewReader(raw.Data))
if err != nil {
return nil, err
}
filter, err := GetFilter(elmt)
if err != nil {
return nil, err
}
f.Data = filter
case FilterTypeEqual, FilterTypeGreaterOrEqual, FilterTypeLessOrEqual, FilterTypeApproxMatch:
ass, err := GetAttributeValueAssertion(raw.Data)
if err != nil {
return nil, err
}
f.Data = ass
case FilterTypeSubstrings:
seq, err := BerGetSequence(raw.Data)
if err != nil {
return nil, err
}
if len(seq) != 2 {
return nil, ErrWrongSequenceLength.WithInfo("SubstringFilter sequence length", len(seq))
}
if seq[0].Type != BerTypeOctetString {
return nil, ErrWrongElementType.WithInfo("SubstringFilter type type", seq[0].Type)
}
sf := &SubstringFilter{Attribute: BerGetOctetString(seq[0].Data)}
if seq[1].Type != BerTypeSequence {
return nil, ErrWrongElementType.WithInfo("SubstringFilter substrings type", seq[1].Type)
}
seq, err = BerGetSequence(seq[1].Data)
if err != nil {
return nil, err
}
for _, rs := range seq {
if rs.Type.Class() != BerClassContextSpecific {
return nil, ErrWrongElementType.WithInfo("SubstringFilter substring type", rs.Type)
}
switch rs.Type.TagNumber() {
case 0:
if sf.Initial != "" {
return nil, ErrWrongElementType.WithInfo("Multiple initial substrings", string(rs.Data))
}
sf.Initial = BerGetOctetString(rs.Data)
case 1:
sf.Any = append(sf.Any, BerGetOctetString(rs.Data))
case 2:
if sf.Final != "" {
return nil, ErrWrongElementType.WithInfo("Multiple final substrings", string(rs.Data))
}
sf.Final = BerGetOctetString(rs.Data)
default:
return nil, ErrWrongElementType.WithInfo("SubstringFilter substring type", rs.Type)
}
}
f.Data = sf
case FilterTypePresent:
f.Data = BerGetOctetString(raw.Data)
case FilterTypeExtensibleMatch:
seq, err := BerGetSequence(raw.Data)
if err != nil {
return nil, err
}
m := MatchingRuleAssertion{}
i := 0
if len(seq) > i && seq[i].Type == BerContextSpecificType(1, false) {
m.MatchingRule = BerGetOctetString(seq[i].Data)
i++
}
if len(seq) > i && seq[i].Type == BerContextSpecificType(2, false) {
m.Attribute = BerGetOctetString(seq[i].Data)
i++
}
if len(seq) <= i || len(seq) > i+2 {
return nil, ErrWrongSequenceLength.WithInfo("MatchingRuleAssertion sequence length", len(seq))
}
if seq[i].Type != BerContextSpecificType(3, false) {
return nil, ErrWrongElementType.WithInfo("MatchingRuleAssertion matchValue type", seq[i].Type)
}
m.Value = BerGetOctetString(seq[i].Data)
i++
if i < len(seq) {
if seq[i].Type != BerContextSpecificType(4, false) {
return nil, ErrWrongElementType.WithInfo("MatchingRuleAssertion dnAttributes type", seq[i].Type)
}
dna, err := BerGetBoolean(seq[i].Data)
if err != nil {
return nil, err
}
m.DNAttributes = dna
}
f.Data = &m
default:
f.Data = &raw
}
return f, nil
}
// Return a string representation of the filter.
//
// NOTE: The output of this function is not valid LDAP if an unrecognized filter type is encountered.
// It outputs unrecognized types as (?<data>), where <data> is the raw data of the unrecognized filter.
func (f *Filter) String() string {
res := strings.Builder{}
f.writeToBuilder(&res)
return res.String()
}
func (f *Filter) writeToBuilder(w *strings.Builder) {
w.WriteRune('(')
switch f.Type {
case FilterTypeAnd:
w.WriteRune('&')
for _, filter := range f.Data.([]Filter) {
filter.writeToBuilder(w)
}
case FilterTypeOr:
w.WriteRune('|')
for _, filter := range f.Data.([]Filter) {
filter.writeToBuilder(w)
}
case FilterTypeNot:
w.WriteRune('!')
f.Data.(*Filter).writeToBuilder(w)
case FilterTypeEqual:
ava := f.Data.(*AttributeValueAssertion)
w.WriteString(ava.Description)
w.WriteRune('=')
w.Write(encodeAssertionValue(ava.Value))
case FilterTypeSubstrings:
sf := f.Data.(*SubstringFilter)
w.WriteString(sf.Attribute)
w.WriteRune('=')
if sf.Initial != "" {
w.Write(encodeAssertionValue(sf.Initial))
}
w.WriteRune('*')
for _, mid := range sf.Any {
w.Write(encodeAssertionValue(mid))
w.WriteRune('*')
}
if sf.Final != "" {
w.Write(encodeAssertionValue(sf.Final))
}
case FilterTypeGreaterOrEqual:
ava := f.Data.(*AttributeValueAssertion)
w.WriteString(ava.Description)
w.WriteString(">=")
w.Write(encodeAssertionValue(ava.Value))
case FilterTypeLessOrEqual:
ava := f.Data.(*AttributeValueAssertion)
w.WriteString(ava.Description)
w.WriteString("<=")
w.Write(encodeAssertionValue(ava.Value))
case FilterTypePresent:
w.WriteString(f.Data.(string))
w.WriteString("=*")
case FilterTypeApproxMatch:
ava := f.Data.(*AttributeValueAssertion)
w.WriteString(ava.Description)
w.WriteString("~=")
w.Write(encodeAssertionValue(ava.Value))
case FilterTypeExtensibleMatch:
mra := f.Data.(*MatchingRuleAssertion)
w.WriteString(mra.Attribute)
if mra.DNAttributes {
w.WriteString(":dn")
}
if mra.MatchingRule != "" {
w.WriteRune(':')
w.WriteString(mra.MatchingRule)
}
w.WriteString(":=")
w.Write(encodeAssertionValue(mra.Value))
case FilterTypeAbsoluteTrue:
w.WriteRune('&')
case FilterTypeAbsoluteFalse:
w.WriteRune('|')
default:
w.WriteRune('?')
w.Write(encodeAssertionValue(string(f.Data.(*BerRawElement).Data)))
}
w.WriteRune(')')
}
var avEscapeMap = map[byte]string{
'*': "\\2a",
'(': "\\28",
')': "\\29",
'\\': "\\5c",
'\x00': "\\00",
}
// Encode an assertion value according to RFC 4515
func encodeAssertionValue(value string) []byte {
buf := make([]byte, 0, len(value))
for i := 0; i < len(value); i++ {
switch value[i] {
case '*', '(', ')', '\\', '\x00':
buf = append(buf, avEscapeMap[value[i]]...)
default:
buf = append(buf, value[i])
}
}
return buf
}