2015-10-07 20:10:00 +00:00
|
|
|
//
|
|
|
|
// https://tools.ietf.org/html/rfc4511
|
|
|
|
//
|
|
|
|
// AddRequest ::= [APPLICATION 8] SEQUENCE {
|
|
|
|
// entry LDAPDN,
|
|
|
|
// attributes AttributeList }
|
|
|
|
//
|
|
|
|
// AttributeList ::= SEQUENCE OF attribute Attribute
|
|
|
|
|
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"gopkg.in/asn1-ber.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Attribute struct {
|
2016-06-08 14:33:08 +00:00
|
|
|
Type string
|
|
|
|
Vals []string
|
2015-10-07 20:10:00 +00:00
|
|
|
}
|
|
|
|
|
2016-06-08 14:33:08 +00:00
|
|
|
|
|
|
|
|
2015-10-07 20:10:00 +00:00
|
|
|
func (a *Attribute) encode() *ber.Packet {
|
|
|
|
seq := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Attribute")
|
2016-06-08 14:33:08 +00:00
|
|
|
seq.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, a.Type, "Type"))
|
2015-10-07 20:10:00 +00:00
|
|
|
set := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSet, nil, "AttributeValue")
|
2016-06-08 14:33:08 +00:00
|
|
|
for _, value := range a.Vals {
|
2015-10-07 20:10:00 +00:00
|
|
|
set.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, value, "Vals"))
|
|
|
|
}
|
|
|
|
seq.AppendChild(set)
|
|
|
|
return seq
|
|
|
|
}
|
|
|
|
|
|
|
|
type AddRequest struct {
|
2016-06-08 14:33:08 +00:00
|
|
|
DN string
|
|
|
|
Attributes []Attribute
|
2015-10-07 20:10:00 +00:00
|
|
|
}
|
|
|
|
|
2016-06-08 14:33:08 +00:00
|
|
|
|
2015-10-07 20:10:00 +00:00
|
|
|
func (a AddRequest) encode() *ber.Packet {
|
|
|
|
request := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationAddRequest, nil, "Add Request")
|
2016-06-08 14:33:08 +00:00
|
|
|
request.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, a.DN, "DN"))
|
2015-10-07 20:10:00 +00:00
|
|
|
attributes := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Attributes")
|
2016-06-08 14:33:08 +00:00
|
|
|
for _, attribute := range a.Attributes {
|
2015-10-07 20:10:00 +00:00
|
|
|
attributes.AppendChild(attribute.encode())
|
|
|
|
}
|
|
|
|
request.AppendChild(attributes)
|
|
|
|
return request
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AddRequest) Attribute(attrType string, attrVals []string) {
|
2016-06-08 14:33:08 +00:00
|
|
|
a.Attributes = append(a.Attributes, Attribute{Type: attrType, Vals: attrVals})
|
2015-10-07 20:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewAddRequest(dn string) *AddRequest {
|
|
|
|
return &AddRequest{
|
2016-06-08 14:33:08 +00:00
|
|
|
DN: dn,
|
2015-10-07 20:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Conn) Add(addRequest *AddRequest) error {
|
|
|
|
messageID := l.nextMessageID()
|
|
|
|
packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request")
|
|
|
|
packet.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, messageID, "MessageID"))
|
|
|
|
packet.AppendChild(addRequest.encode())
|
|
|
|
|
|
|
|
l.Debug.PrintPacket(packet)
|
|
|
|
|
|
|
|
channel, err := l.sendMessage(packet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if channel == nil {
|
|
|
|
return NewError(ErrorNetwork, errors.New("ldap: could not send message"))
|
|
|
|
}
|
|
|
|
defer l.finishMessage(messageID)
|
|
|
|
|
|
|
|
l.Debug.Printf("%d: waiting for response", messageID)
|
2016-04-26 00:18:04 +00:00
|
|
|
packetResponse, ok := <-channel
|
|
|
|
if !ok {
|
|
|
|
return NewError(ErrorNetwork, errors.New("ldap: channel closed"))
|
|
|
|
}
|
|
|
|
packet, err = packetResponse.ReadPacket()
|
2015-10-07 20:10:00 +00:00
|
|
|
l.Debug.Printf("%d: got response %p", messageID, packet)
|
2016-04-26 00:18:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-10-07 20:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if l.Debug {
|
|
|
|
if err := addLDAPDescriptions(packet); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ber.PrintPacket(packet)
|
|
|
|
}
|
|
|
|
|
|
|
|
if packet.Children[1].Tag == ApplicationAddResponse {
|
|
|
|
resultCode, resultDescription := getLDAPResultCode(packet)
|
|
|
|
if resultCode != 0 {
|
|
|
|
return NewError(resultCode, errors.New(resultDescription))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("Unexpected Response: %d", packet.Children[1].Tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Debug.Printf("%d: returning", messageID)
|
|
|
|
return nil
|
|
|
|
}
|