forked from jmckaskill/goldap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
588 lines (469 loc) · 9.9 KB
/
db.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
package ldap
import (
"bytes"
"crypto/tls"
"encoding/binary"
"io"
"net"
"net/url"
"strconv"
"sync"
"github.com/jmckaskill/asn1"
)
type AuthInfo struct {
RemoteAddr string
TLS *tls.ConnectionState
}
type AuthMechanism interface {
MechanismName() string
Connect(rw io.ReadWriter) (io.ReadWriter, error)
}
type ClientConfig struct {
Dial func(net, addr string) (net.Conn, error)
Auth []AuthMechanism
TLS *tls.Config
}
type status int
const (
finished status = iota
abandon
data
ignored
)
type replyHandler interface {
onReply(tag int, data []byte) status
}
type DB struct {
url string
cfg *ClientConfig
lk sync.Mutex
conn net.Conn
rw io.ReadWriter
closed bool
replies map[uint32]replyHandler
nextId uint32
buffer []byte
}
func Open(url string, cfg *ClientConfig) *DB {
return &DB{
url: url,
cfg: cfg,
replies: make(map[uint32]replyHandler),
}
}
func (db *DB) Close() error {
db.lk.Lock()
defer db.lk.Unlock()
if db.conn != nil {
db.conn.Close()
db.conn = nil
db.rw = nil
}
db.closed = true
return nil
}
func dial(network, addr string) (net.Conn, error) {
host, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
dialer := &net.Dialer{
DualStack: true,
}
// SRV
_, addrs, _ := net.LookupSRV("ldap", network, host)
for _, a := range addrs {
sock, err := dialer.Dial("tcp", net.JoinHostPort(a.Target, strconv.Itoa(int(a.Port))))
if err == nil {
return sock, nil
}
}
// Non-SRV
return dialer.Dial(network, addr)
}
func open(urlstr string, cfg *ClientConfig) (rw io.ReadWriter, sock net.Conn, err error) {
// URL
u, err := url.Parse(urlstr)
if err != nil {
return nil, nil, err
}
addr := u.Host
if _, _, err := net.SplitHostPort(addr); err != nil {
addr = net.JoinHostPort(addr, "389")
}
// Dial
if cfg.Dial != nil {
sock, err = cfg.Dial("tcp", addr)
} else {
sock, err = dial("tcp", addr)
}
if err != nil {
return nil, nil, err
}
// TLS
if cfg.TLS != nil {
if err := sendStartTLS(sock); err != nil {
sock.Close()
return nil, nil, err
}
sock = tls.Client(sock, cfg.TLS)
}
// Bind
for _, m := range cfg.Auth {
auth := authSock{Conn: sock, mech: m.MechanismName()}
rw, err := m.Connect(&auth)
if err == nil && auth.waitReply {
_, err = auth.Read(nil)
}
if err == nil && !auth.finished {
err = ErrIncompleteAuth
}
if err == ErrAuthNotSupported {
continue
} else if err != nil {
sock.Close()
return nil, nil, err
}
if rw == nil {
rw = sock
}
return rw, sock, nil
}
return sock, sock, nil
}
type SimpleAuth struct {
User, Pass string
}
func (s SimpleAuth) MechanismName() string {
return "SIMPLE"
}
func (s SimpleAuth) Connect(rw io.ReadWriter) (io.ReadWriter, error) {
out := []byte(s.User)
out = append(out, 0)
out = append(out, s.Pass...)
if _, err := rw.Write(out); err != nil {
return nil, err
}
return nil, nil
}
type authSock struct {
net.Conn
mech string
// LDAP bind requests need to be a clean request response, even if the
// SASL mechanism doesn't want to.
waitReply bool
// After the bind requests have finished we send/receive SASL PDUs
// which are a 4 byte big endian length followed by a chunk of data.
finished bool
}
func (s *authSock) Write(b []byte) (int, error) {
if s.finished {
sz := [4]byte{}
binary.BigEndian.PutUint32(sz[:], uint32(len(b)))
if _, err := s.Conn.Write(sz[:]); err != nil {
return 0, err
}
return s.Conn.Write(b)
}
if s.waitReply {
if _, err := s.Read(nil); err != nil {
return 0, err
}
}
var err error
req := bindRequest{
Version: ldapVersion,
}
if s.mech == "SIMPLE" {
idx := bytes.IndexByte(b, 0)
req.BindDN = b[:idx]
req.Auth.FullBytes, err = asn1.MarshalWithParams(b[idx+1:], simpleBindParam)
} else {
req.Auth.FullBytes, err = asn1.MarshalWithParams(saslCredentials{[]byte(s.mech), b}, saslBindParam)
}
if err != nil {
return 0, err
}
if err := dosend(s.Conn, bindRequestParam, req); err != nil {
return 0, err
}
s.waitReply = true
return len(b), nil
}
func (s *authSock) Read(b []byte) (int, error) {
if s.finished {
bsz := [4]byte{}
if _, err := io.ReadFull(s.Conn, bsz[:]); err != nil {
return 0, err
}
sz := binary.BigEndian.Uint32(bsz[:])
if sz > uint32(len(b)) {
return 0, ErrShortRead
}
if _, err := io.ReadFull(s.Conn, b[:sz]); err != nil {
return 0, err
}
return int(sz), nil
}
if !s.waitReply {
if _, err := s.Write(nil); err != nil {
return 0, err
}
}
res := result{}
if err := getresult(s.Conn, bindResultParam, &res); err != nil {
return 0, err
}
s.waitReply = false
switch LdapResultCode(res.Code) {
case SaslBindInProgress:
s.finished = false
case SuccessError:
s.finished = true
case AuthMethodNotSupported:
return 0, ErrAuthNotSupported
default:
return 0, ErrLdap{&res}
}
return copy(b, res.SaslCredentials), nil
}
func sendStartTLS(rw io.ReadWriter) error {
req := extendedMessage{[]byte(startTLS), nil}
res := result{}
if err := dosend(rw, extendedRequestParam, req); err != nil {
return err
}
if err := getresult(rw, extendedResultParam, &res); err != nil {
return err
}
if LdapResultCode(res.Code) != SuccessError {
return ErrLdap{&res}
}
if string(res.ExtendedName) != startTLS {
return ErrProtocol
}
return nil
}
func dosend(rw io.ReadWriter, param string, req interface{}) error {
rdata, err := asn1.MarshalWithParams(req, param)
if err != nil {
return err
}
msg := message{
Id: 0,
Data: asn1.RawValue{
FullBytes: rdata,
},
}
mdata, err := asn1.Marshal(msg)
if err != nil {
return err
}
if _, err := rw.Write(mdata); err != nil {
return err
}
return nil
}
func getresult(rw io.ReadWriter, param string, res *result) error {
// We need to be careful about only reading the response and no more
// so this can work with start tls
var b []byte
var err error
var de, he int
if b, err = readExactly(rw, b, 2); err != nil {
return err
}
if he, err = headerEnd(b); err != nil {
return err
}
if b, err = readExactly(rw, b, he); err != nil {
return err
}
if de, err = dataEnd(b); err != nil {
return err
}
if b, err = readExactly(rw, b, de); err != nil {
return err
}
msg := message{}
if _, err := asn1.Unmarshal(b, &msg); err != nil {
return err
}
if msg.Id != 0 {
return ErrProtocol
}
if _, err := asn1.UnmarshalWithParams(msg.Data.FullBytes, res, param); err != nil {
return err
}
return nil
}
func headerEnd(h []byte) (int, error) {
if h[0] != universalSequenceTag {
return 0, ErrProtocol
}
if h[1] == 0x80 || h[1] > 0x84 {
return 0, ErrProtocol
}
if h[1] < 0x80 {
return 2, nil
}
return int(h[1]) - 0x80 + 2, nil
}
func dataEnd(h []byte) (int, error) {
switch len(h) {
case 6:
sz := binary.BigEndian.Uint32(h[2:])
if sz > maxMessageSize {
return 0, ErrProtocol
}
return int(sz) + 6, nil
case 5:
h[1] = 0
sz := binary.BigEndian.Uint32(h[1:])
if sz > maxMessageSize {
return 0, ErrProtocol
}
return int(sz) + 5, nil
case 4:
return int(binary.BigEndian.Uint16(h[2:])) + 4, nil
case 3:
return int(h[2]) + 3, nil
case 2:
return int(h[1]) + 2, nil
}
panic("")
}
func resize(b []byte, sz int) []byte {
if sz < cap(b) {
return b
}
bn := make([]byte, len(b), sz+4096)
copy(bn, b)
return bn
}
func readExactly(r io.Reader, buf []byte, sz int) ([]byte, error) {
buf = resize(buf, sz)
n, err := io.ReadFull(r, buf[len(buf):sz])
return buf[:len(buf)+n], err
}
func readAtLeast(r io.Reader, buf []byte, sz int) ([]byte, error) {
buf = resize(buf, sz)
n, err := io.ReadAtLeast(r, buf[len(buf):cap(buf)], sz-len(buf))
return buf[:len(buf)+n], err
}
func mustMarshal(val interface{}, param string) []byte {
data, err := asn1.MarshalWithParams(val, param)
if err != nil {
panic(err)
}
return data
}
// TODO: figure out better routing of read errors
func (db *DB) rxThread(rw io.ReadWriter, conn net.Conn) {
defer func() {
conn.Close()
db.lk.Lock()
if db.conn == conn {
db.conn = nil
db.rw = nil
}
db.lk.Unlock()
}()
var b []byte
for {
var err error
var he, de int
if b, err = readAtLeast(rw, b, 2); err != nil {
return
}
if he, err = headerEnd(b); err != nil {
return
}
if b, err = readAtLeast(rw, b, he); err != nil {
return
}
if de, err = dataEnd(b[:he]); err != nil {
return
}
if b, err = readAtLeast(rw, b, de); err != nil {
return
}
msg := message{}
if _, err := asn1.Unmarshal(b, &msg); err != nil {
return
}
if msg.Data.Class != classApplication {
return
}
db.lk.Lock()
reply := db.replies[msg.Id]
db.lk.Unlock()
if reply != nil {
switch reply.onReply(msg.Data.Tag, msg.Data.FullBytes) {
case finished:
db.lk.Lock()
delete(db.replies, msg.Id)
db.lk.Unlock()
case abandon:
db.lk.Lock()
delete(db.replies, msg.Id)
id := db.nextId
db.nextId++
db.lk.Unlock()
data := mustMarshal(abandonRequest(msg.Id), abandonRequestParam)
msg := message{
Id: id,
Data: asn1.RawValue{
FullBytes: data,
},
}
if _, err := rw.Write(mustMarshal(msg, "")); err != nil {
return
}
}
}
// Note don't overwrite the early part of the buffer as the
// handler may still be referencing it. EG []byte members in a
// SearchTree result.
b = b[de:]
}
}
func (db *DB) send(param string, req interface{}, reply replyHandler) error {
data, err := asn1.MarshalWithParams(req, param)
if err != nil {
return err
}
db.lk.Lock()
defer db.lk.Unlock()
if db.closed {
return ErrClosed
}
if db.conn == nil {
rw, conn, err := open(db.url, db.cfg)
if err != nil {
return err
}
db.conn = conn
db.rw = rw
go db.rxThread(rw, conn)
}
id := db.nextId
db.nextId++
msg := message{
Id: id,
Data: asn1.RawValue{
FullBytes: data,
},
}
out, err := asn1.Marshal(msg)
if err != nil {
return err
}
if _, err := db.rw.Write(out); err != nil {
return err
}
if reply != nil {
db.replies[id] = reply
}
return nil
}