2016-11-03 22:16:01 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2020-10-15 19:27:33 +00:00
|
|
|
ber "github.com/go-asn1-ber/asn1-ber"
|
2016-11-03 22:16:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Attribute represents an LDAP attribute
|
|
|
|
type Attribute struct {
|
|
|
|
// Type is the name of the LDAP attribute
|
|
|
|
Type string
|
|
|
|
// Vals are the LDAP attribute values
|
|
|
|
Vals []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Attribute) encode() *ber.Packet {
|
|
|
|
seq := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Attribute")
|
|
|
|
seq.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, a.Type, "Type"))
|
|
|
|
set := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSet, nil, "AttributeValue")
|
|
|
|
for _, value := range a.Vals {
|
|
|
|
set.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, value, "Vals"))
|
|
|
|
}
|
|
|
|
seq.AppendChild(set)
|
|
|
|
return seq
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddRequest represents an LDAP AddRequest operation
|
|
|
|
type AddRequest struct {
|
|
|
|
// DN identifies the entry being added
|
|
|
|
DN string
|
|
|
|
// Attributes list the attributes of the new entry
|
|
|
|
Attributes []Attribute
|
2019-02-18 12:34:37 +00:00
|
|
|
// Controls hold optional controls to send with the request
|
|
|
|
Controls []Control
|
2016-11-03 22:16:01 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 19:27:33 +00:00
|
|
|
func (req *AddRequest) appendTo(envelope *ber.Packet) error {
|
|
|
|
pkt := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationAddRequest, nil, "Add Request")
|
|
|
|
pkt.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, req.DN, "DN"))
|
2016-11-03 22:16:01 +00:00
|
|
|
attributes := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Attributes")
|
2020-10-15 19:27:33 +00:00
|
|
|
for _, attribute := range req.Attributes {
|
2016-11-03 22:16:01 +00:00
|
|
|
attributes.AppendChild(attribute.encode())
|
|
|
|
}
|
2020-10-15 19:27:33 +00:00
|
|
|
pkt.AppendChild(attributes)
|
|
|
|
|
|
|
|
envelope.AppendChild(pkt)
|
|
|
|
if len(req.Controls) > 0 {
|
|
|
|
envelope.AppendChild(encodeControls(req.Controls))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-11-03 22:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Attribute adds an attribute with the given type and values
|
2020-10-15 19:27:33 +00:00
|
|
|
func (req *AddRequest) Attribute(attrType string, attrVals []string) {
|
|
|
|
req.Attributes = append(req.Attributes, Attribute{Type: attrType, Vals: attrVals})
|
2016-11-03 22:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAddRequest returns an AddRequest for the given DN, with no attributes
|
2019-02-18 12:34:37 +00:00
|
|
|
func NewAddRequest(dn string, controls []Control) *AddRequest {
|
2016-11-03 22:16:01 +00:00
|
|
|
return &AddRequest{
|
2019-02-18 12:34:37 +00:00
|
|
|
DN: dn,
|
|
|
|
Controls: controls,
|
2016-11-03 22:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add performs the given AddRequest
|
|
|
|
func (l *Conn) Add(addRequest *AddRequest) error {
|
2020-10-15 19:27:33 +00:00
|
|
|
msgCtx, err := l.doRequest(addRequest)
|
2016-11-03 22:16:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer l.finishMessage(msgCtx)
|
|
|
|
|
2020-10-15 19:27:33 +00:00
|
|
|
packet, err := l.readPacket(msgCtx)
|
2016-11-03 22:16:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if packet.Children[1].Tag == ApplicationAddResponse {
|
2019-02-18 12:34:37 +00:00
|
|
|
err := GetLDAPError(packet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-11-03 22:16:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("Unexpected Response: %d", packet.Children[1].Tag)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|