-
Notifications
You must be signed in to change notification settings - Fork 1
/
modify.go
80 lines (75 loc) · 2.3 KB
/
modify.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
package ldapserver
// ModifyRequest ::= [APPLICATION 6] SEQUENCE {
// object LDAPDN,
// changes SEQUENCE OF change SEQUENCE {
// operation ENUMERATED {
// add (0),
// delete (1),
// replace (2) },
// modification Attribute }
type ModifyRequest struct {
Object string
Changes []ModifyChange
}
type ModifyChange struct {
Operation ModifyOperation
Modification Attribute
}
type ModifyOperation uint8
// Defined operations
const (
ModifyAdd ModifyOperation = 0
ModifyDelete ModifyOperation = 1
ModifyReplace ModifyOperation = 2
// extensible, more possible
)
// Return a ModifyRequest from BER-encoded data
func GetModifyRequest(data []byte) (*ModifyRequest, error) {
seq, err := BerGetSequence(data)
if err != nil {
return nil, err
}
if len(seq) != 2 {
return nil, ErrWrongSequenceLength.WithInfo("ModifyRequest sequence length", len(seq))
}
if seq[0].Type != BerTypeOctetString {
return nil, ErrWrongElementType.WithInfo("ModifyRequest object type", seq[0].Type)
}
object := BerGetOctetString(seq[0].Data)
if seq[1].Type != BerTypeSequence {
return nil, ErrWrongElementType.WithInfo("ModifyRequest changes type", seq[1].Type)
}
ch_seq, err := BerGetSequence(seq[1].Data)
if err != nil {
return nil, err
}
var changes []ModifyChange
for _, c := range ch_seq {
if c.Type != BerTypeSequence {
return nil, ErrWrongElementType.WithInfo("ModifyRequest change type", c.Type)
}
c_seq, err := BerGetSequence(c.Data)
if err != nil {
return nil, err
}
if len(c_seq) != 2 {
return nil, ErrWrongSequenceLength.WithInfo("ModifyRequest change sequence length", len(c_seq))
}
if c_seq[0].Type != BerTypeEnumerated {
return nil, ErrWrongElementType.WithInfo("ModifyRequest change operation type", c_seq[0].Type)
}
op, err := BerGetEnumerated(c_seq[0].Data)
if err != nil {
return nil, err
}
if c_seq[1].Type != BerTypeSequence {
return nil, ErrWrongElementType.WithInfo("ModifyRequest change modification type", c_seq[1].Type)
}
attr, err := GetAttribute(c_seq[1].Data)
if err != nil {
return nil, err
}
changes = append(changes, ModifyChange{Operation: ModifyOperation(op), Modification: attr})
}
return &ModifyRequest{Object: object, Changes: changes}, nil
}