forked from jmckaskill/goldap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
342 lines (276 loc) · 6.48 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
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
package ldap
import (
"github.com/jmckaskill/asn1"
"reflect"
"strconv"
"unicode"
"unsafe"
)
type ObjectDN string
func (s ObjectDN) String() string {return string(s)}
var dnType = reflect.TypeOf(ObjectDN(""))
type appendSlice struct {
val reflect.Value
typ reflect.Type
done chan error
}
func (s *appendSlice) onReply(tag int, msg []byte) status {
v := reflect.Indirect(reflect.New(s.typ))
sts, err := searchResult(tag, msg, v)
switch sts {
case finished:
s.done <- err
case data:
s.val.Set(reflect.Append(s.val, v))
}
return sts
}
type setStruct struct {
val reflect.Value
done chan error
haveData bool
}
func (s *setStruct) onReply(tag int, msg []byte) status {
sts, err := searchResult(tag, msg, s.val)
switch sts {
case finished:
if err == nil && !s.haveData {
err = ErrNotFound
}
s.done <- err
case data:
s.haveData = true
}
return sts
}
type callFunc struct {
fn reflect.Value
typ reflect.Type
done chan error
}
func (s *callFunc) onReply(tag int, msg []byte) status {
args := [1]reflect.Value{reflect.Indirect(reflect.New(s.typ))}
sts, err := searchResult(tag, msg, args[0])
switch sts {
case finished:
s.done <- err
case data:
rets := s.fn.Call(args[:])
if len(rets) > 0 {
if err, _ := rets[len(rets)-1].Interface().(error); err != nil {
s.done <- err
return abandon
}
}
}
return sts
}
type sendChan struct {
ch reflect.Value
typ reflect.Type
done chan error
}
func (s *sendChan) onReply(tag int, msg []byte) status {
v := reflect.Indirect(reflect.New(s.typ))
sts, err := searchResult(tag, msg, v)
switch sts {
case finished:
s.done <- err
case data:
s.ch.Send(v)
}
return sts
}
func searchResult(tag int, msg []byte, val reflect.Value) (status, error) {
switch tag {
case searchEntryTag:
if err := onSearchEntry(msg, val); err != nil {
return abandon, err
}
return data, nil
case extendedResultTag:
err := onSearchDone(msg, extendedResultParam)
return finished, err
case searchDoneTag:
err := onSearchDone(msg, searchDoneParam)
return finished, err
}
return ignored, nil
}
func onSearchDone(data []byte, param string) error {
res := result{}
if _, err := asn1.UnmarshalWithParams(data, &res, param); err != nil {
return err
}
if LdapResultCode(res.Code) != SuccessError {
return ErrLdap{&res}
}
return nil
}
func descEquals(desc []byte, name string) bool {
if len(desc) < len(name) {
return false
}
for i := 0; i < len(name); i++ {
if unicode.ToUpper(rune(desc[i])) != unicode.ToUpper(rune(name[i])) {
return false
}
}
return len(desc) == len(name) || desc[len(name)] == ';'
}
func getattr(e *searchEntry, name string) [][]byte {
for _, a := range e.Attrs {
if descEquals(a.Desc, name) {
return a.Vals
}
}
return nil
}
func onSearchEntry(data []byte, val reflect.Value) error {
e := searchEntry{}
if _, err := asn1.UnmarshalWithParams(data, &e, searchEntryParam); err != nil {
panic(err)
return err
}
typ := val.Type()
for i := 0; i < typ.NumField(); i++ {
ft := typ.Field(i)
fv := val.Field(i)
if ft.PkgPath != "" {
// private field
continue
}
if ft.Type == dnType {
fv.SetString(string(e.DN))
continue
}
vals := getattr(&e, ft.Name)
if len(vals) == 0 {
continue
}
switch ft.Type.Kind() {
case reflect.Slice, reflect.Array:
t := ft.Type.Elem()
switch t.Kind() {
case reflect.Slice, reflect.Array:
if t.Elem().Kind() == reflect.Uint8 {
// [][]byte
fv.Set(reflect.ValueOf(vals))
}
case reflect.Uint8:
// []byte
fv.Set(reflect.ValueOf(vals[0]))
case reflect.String:
// []string
strs := reflect.MakeSlice(ft.Type, len(vals), len(vals))
for i, v := range vals {
// We need to convert from a string to
// the users own string type. There
// doesn't seem to be a way to do this
// using reflect, so we fall back and
// use unsafe. This is safe because
// we've already checked the kind.
str := string(v)
str2 := reflect.NewAt(t, unsafe.Pointer(&str))
strs.Index(i).Set(str2.Elem())
}
fv.Set(strs)
}
case reflect.String:
fv.SetString(string(vals[0]))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v, err := strconv.ParseInt(string(vals[0]), 10, ft.Type.Bits())
if err != nil {
return err
}
fv.SetInt(v)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
v, err := strconv.ParseUint(string(vals[0]), 10, ft.Type.Bits())
if err != nil {
return err
}
fv.SetUint(v)
case reflect.Float32, reflect.Float64:
v, err := strconv.ParseFloat(string(vals[0]), ft.Type.Bits())
if err != nil {
return err
}
fv.SetFloat(v)
}
}
return nil
}
func (db *DB) search(out interface{}, base ObjectDN, filter Filter, scope asn1.Enumerated) error {
req := searchRequest{
BaseObject: []byte(base),
Scope: scope,
}
var err error
if filter != nil {
req.Filter.FullBytes, err = filter.marshal()
} else {
req.Filter.FullBytes, err = Present("ObjectClass").marshal()
}
if err != nil {
return err
}
var typ reflect.Type
var reply replyHandler
done := make(chan error)
ov := reflect.ValueOf(out)
switch ov.Kind() {
case reflect.Ptr:
ov = reflect.Indirect(ov)
switch ov.Kind() {
case reflect.Slice:
typ = ov.Type().Elem()
reply = &appendSlice{ov, typ, done}
case reflect.Struct:
typ = ov.Type()
reply = &setStruct{ov, done, false}
req.SizeLimit = 1
}
case reflect.Func:
ft := ov.Type()
if ft.NumIn() != 1 {
panic("expected function with 1 argument")
}
typ = ft.In(0)
reply = &callFunc{ov, typ, done}
case reflect.Chan:
typ = ov.Type().Elem()
reply = &sendChan{ov, typ, done}
default:
return ErrUnsupportedType{ov.Type()}
}
if typ.Kind() != reflect.Struct {
return ErrUnsupportedType{typ}
}
for i := 0; i < typ.NumField(); i++ {
f := typ.Field(i)
if f.PkgPath != "" {
// private field
continue
}
if f.Type == dnType || len(f.Name) == 0 {
continue
}
if f.Tag.Get("ldap") == "-" {
continue
}
req.Attrs = append(req.Attrs, []byte(f.Name))
}
if len(req.Attrs) == 0 {
req.Attrs = [][]byte{noAttributes}
}
if err := db.send(searchRequestParam, req, reply); err != nil {
return err
}
return <-done
}
func (db *DB) SearchTree(out interface{}, base ObjectDN, filter Filter) error {
return db.search(out, base, filter, wholeSubtreeScope)
}
func (db *DB) GetObject(out interface{}, dn ObjectDN) error {
return db.search(out, dn, nil, baseObjectScope)
}