You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.5 KiB
92 lines
2.5 KiB
8 years ago
|
package ldap
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
|
||
4 years ago
|
ber "github.com/go-asn1-ber/asn1-ber"
|
||
8 years ago
|
)
|
||
|
|
||
|
// 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
|
||
6 years ago
|
// Controls hold optional controls to send with the request
|
||
|
Controls []Control
|
||
8 years ago
|
}
|
||
|
|
||
4 years ago
|
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"))
|
||
8 years ago
|
attributes := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Attributes")
|
||
4 years ago
|
for _, attribute := range req.Attributes {
|
||
8 years ago
|
attributes.AppendChild(attribute.encode())
|
||
|
}
|
||
4 years ago
|
pkt.AppendChild(attributes)
|
||
|
|
||
|
envelope.AppendChild(pkt)
|
||
|
if len(req.Controls) > 0 {
|
||
|
envelope.AppendChild(encodeControls(req.Controls))
|
||
|
}
|
||
|
|
||
|
return nil
|
||
8 years ago
|
}
|
||
|
|
||
|
// Attribute adds an attribute with the given type and values
|
||
4 years ago
|
func (req *AddRequest) Attribute(attrType string, attrVals []string) {
|
||
|
req.Attributes = append(req.Attributes, Attribute{Type: attrType, Vals: attrVals})
|
||
8 years ago
|
}
|
||
|
|
||
|
// NewAddRequest returns an AddRequest for the given DN, with no attributes
|
||
6 years ago
|
func NewAddRequest(dn string, controls []Control) *AddRequest {
|
||
8 years ago
|
return &AddRequest{
|
||
6 years ago
|
DN: dn,
|
||
|
Controls: controls,
|
||
8 years ago
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
// Add performs the given AddRequest
|
||
|
func (l *Conn) Add(addRequest *AddRequest) error {
|
||
4 years ago
|
msgCtx, err := l.doRequest(addRequest)
|
||
8 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer l.finishMessage(msgCtx)
|
||
|
|
||
4 years ago
|
packet, err := l.readPacket(msgCtx)
|
||
8 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if packet.Children[1].Tag == ApplicationAddResponse {
|
||
6 years ago
|
err := GetLDAPError(packet)
|
||
|
if err != nil {
|
||
|
return err
|
||
8 years ago
|
}
|
||
|
} else {
|
||
|
log.Printf("Unexpected Response: %d", packet.Children[1].Tag)
|
||
|
}
|
||
|
return nil
|
||
|
}
|