forked from mavricknz/ldap
-
Notifications
You must be signed in to change notification settings - Fork 2
/
add.go
111 lines (92 loc) · 3.27 KB
/
add.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
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ldap
import (
"fmt"
"github.com/mavricknz/asn1-ber"
)
type AddRequest struct {
Entry *Entry
Controls []Control
}
func (req *AddRequest) RecordType() uint8 {
return AddRecord
}
func (l *LDAPConnection) Add(req *AddRequest) error {
messageID, ok := l.nextMessageID()
if !ok {
return NewLDAPError(ErrorClosing, "messageID channel is closed.")
}
encodedAdd, err := encodeAddRequest(req)
if err != nil {
return err
}
packet, err := requestBuildPacket(messageID, encodedAdd, req.Controls)
if err != nil {
return err
}
return l.sendReqRespPacket(messageID, packet)
}
/*
AddRequest ::= [APPLICATION 8] SEQUENCE {
entry LDAPDN,
attributes AttributeList }
AttributeList ::= SEQUENCE OF attribute Attribute
Attribute ::= SEQUENCE {
type AttributeDescription,
vals SET OF value AttributeValue } // vals is not empty
*/
func encodeAddRequest(addReq *AddRequest) (*ber.Packet, error) {
addPacket := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationAddRequest, nil, ApplicationMap[ApplicationAddRequest])
addPacket.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimative, ber.TagOctetString, addReq.Entry.DN, "LDAP DN"))
attributeList := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "AttributeList")
for _, attr := range addReq.Entry.Attributes {
attribute := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Attribute")
attribute.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimative, ber.TagOctetString, attr.Name, "Attribute Desc"))
if len(attr.Values) == 0 {
return nil, NewLDAPError(ErrorEncoding, "attribute "+attr.Name+" had no values.")
}
valuesSet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSet, nil, "Attribute Value Set")
for _, val := range attr.Values {
valuesSet.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimative, ber.TagOctetString, val, "AttributeValue"))
}
attribute.AppendChild(valuesSet)
attributeList.AppendChild(attribute)
}
addPacket.AppendChild(attributeList)
return addPacket, nil
}
func (req *AddRequest) Bytes() []byte {
encoded, _ := encodeAddRequest(req)
return encoded.Bytes()
}
func NewAddRequest(dn string) (req *AddRequest) {
req = &AddRequest{Entry: NewEntry(dn), Controls: make([]Control, 0)}
return
}
func (req *AddRequest) AddAttribute(attr *EntryAttribute) {
req.Entry.AddAttributeValues(attr.Name, attr.Values)
}
func (req *AddRequest) AddAttributes(attrs []EntryAttribute) {
for _, attr := range attrs {
req.Entry.AddAttributeValues(attr.Name, attr.Values)
}
}
// DumpAddRequest - Basic LDIF "like" dump for testing, no formating, etc
func (addReq *AddRequest) String() (dump string) {
dump = fmt.Sprintf("dn: %s\n", addReq.Entry.DN)
for _, attr := range addReq.Entry.Attributes {
for _, val := range attr.Values {
dump += fmt.Sprintf("%s: %s\n", attr.Name, val)
}
}
dump += fmt.Sprintf("\n")
return
}
func (req *AddRequest) AddControl(control Control) {
if req.Controls == nil {
req.Controls = make([]Control, 0)
}
req.Controls = append(req.Controls, control)
}