-
Notifications
You must be signed in to change notification settings - Fork 4
/
ldap.go
314 lines (268 loc) · 6.59 KB
/
ldap.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
package ldap
import (
"bytes"
"crypto/tls"
"fmt"
"github.com/stesla/ldap/asn1"
"net"
"strings"
"sync"
)
type Conn interface {
net.Conn
Bind(user, password string) error
Unbind() error
Search(req SearchRequest) ([]SearchResult, error)
StartTLS(config *tls.Config) error
}
func RoundRobin(addr string, dialer func(string) (Conn, error)) (Conn, error) {
parts := strings.Split(addr, ":")
hosts, err := net.LookupHost(parts[0])
if err != nil {
return nil, fmt.Errorf("LookupHost: %v", err)
}
for _, host := range hosts {
conn, err := dialer(fmt.Sprintf("%s:%s", host, parts[1]))
if err == nil {
return conn, nil
}
}
return nil, fmt.Errorf("could not connect to an ldap server")
}
func Dial(addr string) (Conn, error) {
tcp, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
return newConn(tcp), nil
}
func DialSSL(addr string, tlsConfig *tls.Config) (Conn, error) {
tcp, err := tls.Dial("tcp", addr, tlsConfig)
if err != nil {
return nil, err
}
return newConn(tcp), nil
}
func DialTLS(addr string, tlsConfig *tls.Config) (Conn, error) {
tcp, err := Dial(addr)
if err != nil {
return nil, err
}
conn := newConn(tcp)
err = conn.StartTLS(tlsConfig)
if err != nil {
return nil, err
}
return conn, nil
}
type conn struct {
net.Conn
id sequence
}
func newConn(tcp net.Conn) *conn {
return &conn{
Conn: tcp,
}
}
type ldapMessage struct {
MessageId int
ProtocolOp interface{}
Controls []interface{} `asn1:"tag:0,optional"`
}
type ldapResultCode int16
const (
Success ldapResultCode = 0
InappropriateAuthentication ldapResultCode = 48
InvalidCredentials ldapResultCode = 49
InsufficientAccessRights ldapResultCode = 59
)
type ldapResult struct {
ResultCode ldapResultCode
MatchedDN []byte
Message []byte
Referral []interface{} `asn1:"tag:3,optional"`
}
type bindRequest struct {
Version int8
Name []byte
Auth interface{}
}
func (l *conn) Bind(user, password string) (err error) {
msg := ldapMessage{
MessageId: l.id.Next(),
ProtocolOp: asn1.OptionValue{
Opts: "application,tag:0", Value: bindRequest{
Version: 3,
Name: []byte(user),
// TODO: Support SASL
Auth: simpleAuth(password),
},
},
}
enc := asn1.NewEncoder(l)
enc.Implicit = true
if err = enc.Encode(msg); err != nil {
return fmt.Errorf("Encode: %v", err)
}
var result ldapResult
msg = ldapMessage{
ProtocolOp: asn1.OptionValue{Opts: "application,tag:1", Value: &result},
}
dec := asn1.NewDecoder(l)
dec.Implicit = true
if err = dec.Decode(&msg); err != nil {
return fmt.Errorf("Decode: %v", err)
}
if result.ResultCode != Success {
return fmt.Errorf("ldap.Bind unsuccessful: resultCode = %v", result.ResultCode)
}
return nil
}
func simpleAuth(password string) interface{} {
return asn1.OptionValue{Opts: "tag:0", Value: []byte(password)}
}
func (l *conn) Unbind() error {
defer l.Close()
msg := ldapMessage{
MessageId: l.id.Next(),
ProtocolOp: asn1.OptionValue{Opts: "application,tag:2", Value: asn1.RawValue{
Class: asn1.ClassUniversal,
Tag: asn1.TagNull,
},
},
}
enc := asn1.NewEncoder(l)
enc.Implicit = true
if err := enc.Encode(msg); err != nil {
return fmt.Errorf("Encode: %v", err)
}
return nil
}
type sequence struct {
next int
l sync.Mutex
}
func (gen *sequence) Next() (id int) {
gen.l.Lock()
defer gen.l.Unlock()
id = gen.next
gen.next++
return
}
type SearchRequest struct {
BaseObject []byte
Scope SearchScope `asn1:"enum"`
Deref DerefAliases `asn1:"enum"`
SizeLimit int
TimeLimit int
TypesOnly bool
Filter
Attributes [][]byte
}
type SearchScope int
const (
BaseObject SearchScope = 0
SingleLevel SearchScope = 1
WholeSubtree SearchScope = 2
)
type DerefAliases int
const (
NeverDerefAliases DerefAliases = 0
DerefInSearching DerefAliases = 1
DerefFindingBaseObj DerefAliases = 2
DerefAlways DerefAliases = 3
)
type SearchResult struct {
DN string
Attributes map[string][]string
}
func (l *conn) Search(req SearchRequest) ([]SearchResult, error) {
msg := ldapMessage{
MessageId: l.id.Next(),
ProtocolOp: asn1.OptionValue{Opts: "application,tag:3", Value: req},
}
enc := asn1.NewEncoder(l)
enc.Implicit = true
if err := enc.Encode(msg); err != nil {
return nil, fmt.Errorf("Encode: %v", err)
}
dec := asn1.NewDecoder(l)
dec.Implicit = true
results := []SearchResult{}
loop:
for {
var raw asn1.RawValue
resp := ldapMessage{ProtocolOp: &raw}
if err := dec.Decode(&resp); err != nil {
return nil, fmt.Errorf("Decode Envelope: %v", err)
}
rdec := asn1.NewDecoder(bytes.NewBuffer(raw.RawBytes))
rdec.Implicit = true
switch raw.Tag {
case 4:
var r struct {
Name []byte
Attributes []struct {
Type []byte
Values [][]byte `asn1:"set"`
}
}
if err := rdec.Decode(asn1.OptionValue{Opts: "application,tag:4", Value: &r}); err != nil {
return nil, fmt.Errorf("Decode SearchResult: %v", err)
}
result := SearchResult{string(r.Name), make(map[string][]string)}
for _, a := range r.Attributes {
vals := []string{}
for _, v := range a.Values {
vals = append(vals, string(v))
}
result.Attributes[string(a.Type)] = vals
}
results = append(results, result)
case 5: // SearchResultDone
var r ldapResult
if err := rdec.Decode(asn1.OptionValue{Opts: "application,tag:5", Value: &r}); err != nil {
return nil, fmt.Errorf("Decode SearchResultDone: %v", err)
}
if r.ResultCode != Success {
return nil, fmt.Errorf("ResultCode = %d", r.ResultCode)
}
break loop
case 19: // SearchResultReference
// TODO
}
}
return results, nil
}
type extendedRequest struct {
Name []byte `asn1:"tag:0"`
Value []byte `asn1:"tag:1,optional"`
}
type extendedResponse struct {
Result ldapResult `asn1:"components"`
Name []byte `asn1:"tag:10,optional"`
Value []byte `asn1:"tag:11,optional"`
}
func (l *conn) StartTLS(config *tls.Config) error {
msg := ldapMessage{
MessageId: l.id.Next(),
ProtocolOp: asn1.OptionValue{Opts: "application,tag:23", Value: extendedRequest{Name: []byte("1.3.6.1.4.1.1466.20037")}},
}
enc := asn1.NewEncoder(l)
enc.Implicit = true
if err := enc.Encode(msg); err != nil {
return fmt.Errorf("Encode: %v", err)
}
var r extendedResponse
resp := ldapMessage{ProtocolOp: asn1.OptionValue{Opts: "application,tag:24", Value: &r}}
dec := asn1.NewDecoder(l)
dec.Implicit = true
if err := dec.Decode(&resp); err != nil {
return fmt.Errorf("Decode: %v", err)
}
if r.Result.ResultCode != Success {
return fmt.Errorf("ResultCode = %d", r.Result.ResultCode)
}
l.Conn = tls.Client(l.Conn, config)
return nil
}