-
Notifications
You must be signed in to change notification settings - Fork 15
/
ldif.go
534 lines (483 loc) · 11.9 KB
/
ldif.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
// Package ldif contains an LDIF parser and marshaller (RFC 2849).
package ldif
import (
"bufio"
"bytes"
"encoding/base64"
"errors"
"fmt"
"io"
"io/ioutil"
"net/url"
"strconv"
"strings"
"github.com/go-ldap/ldap/v3"
)
// Entry is one entry in the LDIF
type Entry struct {
Entry *ldap.Entry
Add *ldap.AddRequest
Del *ldap.DelRequest
Modify *ldap.ModifyRequest
}
// The LDIF struct is used for parsing an LDIF. The Controls
// is used to tell the parser to ignore any controls found
// when parsing (default: false to ignore the controls).
// FoldWidth is used for the line lenght when marshalling.
type LDIF struct {
Entries []*Entry
Version int
changeType string
FoldWidth int
Controls bool
firstEntry bool
}
// The ParseError holds the error message and the line in the ldif
// where the error occurred.
type ParseError struct {
Line int
Message string
}
// Error implements the error interface
func (e *ParseError) Error() string {
return fmt.Sprintf("Error in line %d: %s", e.Line, e.Message)
}
var cr byte = '\x0D'
var lf byte = '\x0A'
var sep = string([]byte{cr, lf})
var comment byte = '#'
var space byte = ' '
var spaces = string(space)
// Parse wraps Unmarshal to parse an LDIF from a string
func Parse(str string) (l *LDIF, err error) {
buf := bytes.NewBuffer([]byte(str))
l = &LDIF{}
err = Unmarshal(buf, l)
return
}
// ParseWithControls wraps Unmarshal to parse an LDIF from
// a string, controls are added to change records
func ParseWithControls(str string) (l *LDIF, err error) {
buf := bytes.NewBuffer([]byte(str))
l = &LDIF{Controls: true}
err = Unmarshal(buf, l)
return
}
// Unmarshal parses the LDIF from the given io.Reader into the LDIF struct.
// The caller is responsible for closing the io.Reader if that is
// needed.
func Unmarshal(r io.Reader, l *LDIF) (err error) {
if r == nil {
return &ParseError{Line: 0, Message: "No reader present"}
}
curLine := 0
l.Version = 0
l.changeType = ""
isComment := false
reader := bufio.NewReader(r)
var lines []string
var line, nextLine string
l.firstEntry = true
for {
curLine++
nextLine, err = reader.ReadString(lf)
nextLine = strings.TrimRight(nextLine, sep)
switch err {
case nil, io.EOF:
switch len(nextLine) {
case 0:
if len(line) == 0 && err == io.EOF {
return nil
}
if len(line) == 0 && len(lines) == 0 {
continue
}
lines = append(lines, line)
entry, perr := l.parseEntry(lines)
if perr != nil {
return &ParseError{Line: curLine, Message: perr.Error()}
}
l.Entries = append(l.Entries, entry)
line = ""
lines = []string{}
if err == io.EOF {
return nil
}
default:
switch nextLine[0] {
case comment:
isComment = true
continue
case space:
if isComment {
continue
}
line += nextLine[1:]
continue
default:
isComment = false
if len(line) != 0 {
lines = append(lines, line)
}
line = nextLine
continue
}
}
default:
return &ParseError{Line: curLine, Message: err.Error()}
}
}
}
func (l *LDIF) parseEntry(lines []string) (entry *Entry, err error) {
if len(lines) == 0 {
return nil, errors.New("empty entry?")
}
if l.firstEntry && strings.HasPrefix(lines[0], "version:") {
l.firstEntry = false
line := strings.TrimLeft(lines[0][8:], spaces)
if l.Version, err = strconv.Atoi(line); err != nil {
return nil, err
}
if l.Version != 1 {
return nil, errors.New("Invalid version spec " + string(line))
}
l.Version = 1
if len(lines) == 1 {
return nil, nil
}
lines = lines[1:]
}
l.firstEntry = false
if len(lines) == 0 {
return nil, nil
}
if !strings.HasPrefix(lines[0], "dn:") {
return nil, errors.New("missing 'dn:'")
}
_, val, err := l.parseLine(lines[0])
if err != nil {
return nil, err
}
dn := val
if len(lines) == 1 {
return nil, errors.New("only a dn: line")
}
lines = lines[1:]
var controls []ldap.Control
controls, lines, err = l.parseControls(lines)
if err != nil {
return nil, err
}
if strings.HasPrefix(lines[0], "changetype:") {
_, val, err := l.parseLine(lines[0])
if err != nil {
return nil, err
}
l.changeType = val
if len(lines) > 1 {
lines = lines[1:]
}
}
switch l.changeType {
case "":
if len(controls) != 0 {
return nil, errors.New("controls found without changetype")
}
attrs, err := l.parseAttrs(lines)
if err != nil {
return nil, err
}
return &Entry{Entry: ldap.NewEntry(dn, attrs)}, nil
case "add":
attrs, err := l.parseAttrs(lines)
if err != nil {
return nil, err
}
// FIXME: controls for add - see https://github.com/go-ldap/ldap/issues/81
add := ldap.NewAddRequest(dn, controls)
for attr, vals := range attrs {
add.Attribute(attr, vals)
}
return &Entry{Add: add}, nil
case "delete":
if len(lines) > 1 {
return nil, errors.New("no attributes allowed for changetype delete")
}
return &Entry{Del: ldap.NewDelRequest(dn, controls)}, nil
case "modify":
// FIXME: controls for modify - see https://github.com/go-ldap/ldap/issues/81
mod := ldap.NewModifyRequest(dn, controls)
var op, attribute string
var values []string
if lines[len(lines)-1] != "-" {
return nil, errors.New("modify request does not close with a single dash")
}
for i := 0; i < len(lines); i++ {
if lines[i] == "-" {
switch op {
case "":
return nil, fmt.Errorf("empty operation")
case "add":
mod.Add(attribute, values)
op = ""
attribute = ""
values = nil
case "replace":
mod.Replace(attribute, values)
op = ""
attribute = ""
values = nil
case "delete":
mod.Delete(attribute, values)
op = ""
attribute = ""
values = nil
default:
return nil, fmt.Errorf("invalid operation %s in modify request", op)
}
continue
}
attr, val, err := l.parseLine(lines[i])
if err != nil {
return nil, err
}
if op == "" {
op = attr
attribute = val
} else {
if attr != attribute {
return nil, fmt.Errorf("invalid attribute %s in %s request for %s", attr, op, attribute)
}
values = append(values, val)
}
}
return &Entry{Modify: mod}, nil
case "moddn", "modrdn":
return nil, fmt.Errorf("unsupported changetype %s", l.changeType)
default:
return nil, fmt.Errorf("invalid changetype %s", l.changeType)
}
}
func (l *LDIF) parseAttrs(lines []string) (map[string][]string, error) {
attrs := make(map[string][]string)
for i := 0; i < len(lines); i++ {
attr, val, err := l.parseLine(lines[i])
if err != nil {
return nil, err
}
attrs[attr] = append(attrs[attr], val)
}
return attrs, nil
}
func (l *LDIF) parseLine(line string) (attr, val string, err error) {
off := 0
for len(line) > off && line[off] != ':' {
off++
if off >= len(line) {
err = fmt.Errorf("Missing : in line `%s`", line)
return
}
}
if off == len(line) {
err = fmt.Errorf("Missing : in the line `%s`", line)
return
}
if off > len(line)-2 {
err = errors.New("empty value")
// FIXME: this is allowed for some attributes, e.g. seeAlso
return
}
attr = line[0:off]
if err = validAttr(attr); err != nil {
attr = ""
val = ""
return
}
switch line[off+1] {
case ':':
val, err = decodeBase64(strings.TrimLeft(line[off+2:], spaces))
if err != nil {
return
}
case '<':
val, err = readURLValue(strings.TrimLeft(line[off+2:], spaces))
if err != nil {
return
}
default:
val = strings.TrimLeft(line[off+1:], spaces)
}
return
}
func (l *LDIF) parseControls(lines []string) ([]ldap.Control, []string, error) {
var controls []ldap.Control
for {
if !strings.HasPrefix(lines[0], "control:") {
break
}
if !l.Controls {
if len(lines) == 1 {
return nil, nil, errors.New("only controls found")
}
lines = lines[1:]
continue
}
_, val, err := l.parseLine(lines[0])
if err != nil {
return nil, nil, err
}
var oid, ctrlValue string
criticality := false
parts := strings.SplitN(val, " ", 3)
if err = validOID(parts[0]); err != nil {
return nil, nil, fmt.Errorf("%s is not a valid oid: %s", oid, err)
}
oid = parts[0]
if len(parts) > 1 {
switch parts[1] {
case "true":
criticality = true
if len(parts) > 2 {
parts[1] = parts[2]
parts = parts[0:2]
}
case "false":
criticality = false
if len(parts) > 2 {
parts[1] = parts[2]
parts = parts[0:2]
}
}
}
if len(parts) == 2 {
ctrlValue = parts[1]
}
if ctrlValue == "" {
switch oid {
case ldap.ControlTypeManageDsaIT:
controls = append(controls, &ldap.ControlManageDsaIT{Criticality: criticality})
default:
return nil, nil, fmt.Errorf("unsupported control found: %s", oid)
}
} else {
switch ctrlValue[0] { // where is this documented?
case ':':
if len(ctrlValue) == 1 {
return nil, nil, errors.New("missing value for base64 encoded control value")
}
ctrlValue, err = decodeBase64(strings.TrimLeft(ctrlValue[1:], spaces))
if err != nil {
return nil, nil, err
}
if ctrlValue == "" {
return nil, nil, errors.New("base64 decoded to empty value")
}
case '<':
if len(ctrlValue) == 1 {
return nil, nil, errors.New("missing value for url control value")
}
ctrlValue, err = readURLValue(strings.TrimLeft(ctrlValue[1:], spaces))
if err != nil {
return nil, nil, err
}
if ctrlValue == "" {
return nil, nil, errors.New("url resolved to an empty value")
}
}
// TODO:
// convert ctrlValue to *ber.Packet and decode with something like
// ctrl := ldap.DecodeControl()
// ... FIXME: the controls need a Decode() interface
// so we can just do a
// ctrl := ldap.ControlByOID(oid) // returns an empty &ControlSomething{}
// ctrl.Decode((*ber.Packet)(ctrlValue))
// ctrl.Criticality = criticality
// that should be usable in github.com/go-ldap/ldap/control.go also
// to decode the incoming control
// controls = append(controls, ctrl)
return nil, nil, fmt.Errorf("controls with values are not supported, oid: %s", oid)
}
if len(lines) == 1 {
return nil, nil, errors.New("only controls found")
}
lines = lines[1:]
}
return controls, lines, nil
}
func readURLValue(val string) (string, error) {
u, err := url.Parse(val)
if err != nil {
return "", fmt.Errorf("failed to parse URL: %s", err)
}
if u.Scheme != "file" {
return "", fmt.Errorf("unsupported URL scheme %s", u.Scheme)
}
data, err := ioutil.ReadFile(toPath(u))
if err != nil {
return "", fmt.Errorf("failed to read %s: %s", u.Path, err)
}
val = string(data) // FIXME: safe?
return val, nil
}
func decodeBase64(enc string) (string, error) {
dec := make([]byte, base64.StdEncoding.DecodedLen(len([]byte(enc))))
n, err := base64.StdEncoding.Decode(dec, []byte(enc))
if err != nil {
return "", err
}
return string(dec[:n]), nil
}
func validOID(oid string) error {
lastDot := true
for _, c := range oid {
switch {
case c == '.' && lastDot:
return errors.New("OID with at least 2 consecutive dots")
case c == '.':
lastDot = true
case c >= '0' && c <= '9':
lastDot = false
default:
return errors.New("Invalid character in OID")
}
}
return nil
}
func validAttr(attr string) error {
if len(attr) == 0 {
return errors.New("empty attribute name")
}
switch {
case attr[0] >= 'A' && attr[0] <= 'Z':
// A-Z
case attr[0] >= 'a' && attr[0] <= 'z':
// a-z
default:
if attr[0] >= '0' && attr[0] <= '9' {
return validOID(attr)
}
return errors.New("invalid first character in attribute")
}
for i := 1; i < len(attr); i++ {
c := attr[i]
switch {
case c >= '0' && c <= '9':
case c >= 'A' && c <= 'Z':
case c >= 'a' && c <= 'z':
case c == '-':
case c == ';':
default:
return errors.New("invalid character in attribute name")
}
}
return nil
}
// AllEntries returns all *ldap.Entries in the LDIF
func (l *LDIF) AllEntries() (entries []*ldap.Entry) {
for _, entry := range l.Entries {
if entry.Entry != nil {
entries = append(entries, entry.Entry)
}
}
return entries
}