open-vault/vendor/github.com/go-ldap/ldap/modify.go

174 lines
5.3 KiB
Go
Raw Normal View History

2015-05-07 17:59:38 +00:00
// File contains Modify functionality
//
// https://tools.ietf.org/html/rfc4511
//
// ModifyRequest ::= [APPLICATION 6] SEQUENCE {
// object LDAPDN,
// changes SEQUENCE OF change SEQUENCE {
// operation ENUMERATED {
// add (0),
// delete (1),
// replace (2),
// ... },
// modification PartialAttribute } }
//
// PartialAttribute ::= SEQUENCE {
// type AttributeDescription,
// vals SET OF value AttributeValue }
//
// AttributeDescription ::= LDAPString
// -- Constrained to <attributedescription>
// -- [RFC4512]
//
// AttributeValue ::= OCTET STRING
//
package ldap
import (
"errors"
"log"
2015-06-29 21:50:55 +00:00
"gopkg.in/asn1-ber.v1"
2015-05-07 17:59:38 +00:00
)
2016-07-23 00:11:47 +00:00
// Change operation choices
2015-05-07 17:59:38 +00:00
const (
AddAttribute = 0
DeleteAttribute = 1
ReplaceAttribute = 2
)
2016-07-23 00:11:47 +00:00
// PartialAttribute for a ModifyRequest as defined in https://tools.ietf.org/html/rfc4511
2015-05-07 17:59:38 +00:00
type PartialAttribute struct {
2016-07-23 00:11:47 +00:00
// Type is the type of the partial attribute
2016-06-08 14:33:08 +00:00
Type string
2016-07-23 00:11:47 +00:00
// Vals are the values of the partial attribute
2016-06-08 14:33:08 +00:00
Vals []string
2015-05-07 17:59:38 +00:00
}
func (p *PartialAttribute) encode() *ber.Packet {
seq := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "PartialAttribute")
2016-06-08 14:33:08 +00:00
seq.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, p.Type, "Type"))
2015-05-07 17:59:38 +00:00
set := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSet, nil, "AttributeValue")
2016-06-08 14:33:08 +00:00
for _, value := range p.Vals {
2015-05-07 17:59:38 +00:00
set.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, value, "Vals"))
}
seq.AppendChild(set)
return seq
}
2018-06-15 17:13:57 +00:00
// Change for a ModifyRequest as defined in https://tools.ietf.org/html/rfc4511
type Change struct {
// Operation is the type of change to be made
Operation uint
// Modification is the attribute to be modified
Modification PartialAttribute
}
func (c *Change) encode() *ber.Packet {
change := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Change")
change.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagEnumerated, uint64(c.Operation), "Operation"))
change.AppendChild(c.Modification.encode())
return change
}
2016-07-23 00:11:47 +00:00
// ModifyRequest as defined in https://tools.ietf.org/html/rfc4511
2015-05-07 17:59:38 +00:00
type ModifyRequest struct {
2016-07-23 00:11:47 +00:00
// DN is the distinguishedName of the directory entry to modify
DN string
2018-06-15 17:13:57 +00:00
// Changes contain the attributes to modify
Changes []Change
// Controls hold optional controls to send with the request
Controls []Control
2015-05-07 17:59:38 +00:00
}
2018-06-15 17:13:57 +00:00
// Add appends the given attribute to the list of changes to be made
2015-05-07 17:59:38 +00:00
func (m *ModifyRequest) Add(attrType string, attrVals []string) {
2018-06-15 17:13:57 +00:00
m.appendChange(AddAttribute, attrType, attrVals)
2015-05-07 17:59:38 +00:00
}
2018-06-15 17:13:57 +00:00
// Delete appends the given attribute to the list of changes to be made
2015-05-07 17:59:38 +00:00
func (m *ModifyRequest) Delete(attrType string, attrVals []string) {
2018-06-15 17:13:57 +00:00
m.appendChange(DeleteAttribute, attrType, attrVals)
2015-05-07 17:59:38 +00:00
}
2018-06-15 17:13:57 +00:00
// Replace appends the given attribute to the list of changes to be made
2015-05-07 17:59:38 +00:00
func (m *ModifyRequest) Replace(attrType string, attrVals []string) {
2018-06-15 17:13:57 +00:00
m.appendChange(ReplaceAttribute, attrType, attrVals)
}
func (m *ModifyRequest) appendChange(operation uint, attrType string, attrVals []string) {
m.Changes = append(m.Changes, Change{operation, PartialAttribute{Type: attrType, Vals: attrVals}})
2015-05-07 17:59:38 +00:00
}
func (m ModifyRequest) encode() *ber.Packet {
request := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationModifyRequest, nil, "Modify Request")
2016-06-08 14:33:08 +00:00
request.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, m.DN, "DN"))
2015-05-07 17:59:38 +00:00
changes := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Changes")
2018-06-15 17:13:57 +00:00
for _, change := range m.Changes {
changes.AppendChild(change.encode())
2015-05-07 17:59:38 +00:00
}
request.AppendChild(changes)
return request
}
2016-07-23 00:11:47 +00:00
// NewModifyRequest creates a modify request for the given DN
2015-05-07 17:59:38 +00:00
func NewModifyRequest(
dn string,
2018-06-15 17:13:57 +00:00
controls []Control,
2015-05-07 17:59:38 +00:00
) *ModifyRequest {
return &ModifyRequest{
2018-06-15 17:13:57 +00:00
DN: dn,
Controls: controls,
2015-05-07 17:59:38 +00:00
}
}
2016-07-23 00:11:47 +00:00
// Modify performs the ModifyRequest
2015-05-07 17:59:38 +00:00
func (l *Conn) Modify(modifyRequest *ModifyRequest) error {
packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request")
2016-06-30 18:19:03 +00:00
packet.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, l.nextMessageID(), "MessageID"))
2015-05-07 17:59:38 +00:00
packet.AppendChild(modifyRequest.encode())
2018-06-15 17:13:57 +00:00
if len(modifyRequest.Controls) > 0 {
packet.AppendChild(encodeControls(modifyRequest.Controls))
}
2015-05-07 17:59:38 +00:00
l.Debug.PrintPacket(packet)
2016-06-30 18:19:03 +00:00
msgCtx, err := l.sendMessage(packet)
2015-05-07 17:59:38 +00:00
if err != nil {
return err
}
2016-06-30 18:19:03 +00:00
defer l.finishMessage(msgCtx)
2015-05-07 17:59:38 +00:00
2016-06-30 18:19:03 +00:00
l.Debug.Printf("%d: waiting for response", msgCtx.id)
packetResponse, ok := <-msgCtx.responses
2016-04-26 00:18:04 +00:00
if !ok {
2016-06-30 18:19:03 +00:00
return NewError(ErrorNetwork, errors.New("ldap: response channel closed"))
2016-04-26 00:18:04 +00:00
}
packet, err = packetResponse.ReadPacket()
2016-06-30 18:19:03 +00:00
l.Debug.Printf("%d: got response %p", msgCtx.id, packet)
2016-04-26 00:18:04 +00:00
if err != nil {
return err
2015-05-07 17:59:38 +00:00
}
if l.Debug {
if err := addLDAPDescriptions(packet); err != nil {
return err
}
ber.PrintPacket(packet)
}
if packet.Children[1].Tag == ApplicationModifyResponse {
resultCode, resultDescription := getLDAPResultCode(packet)
if resultCode != 0 {
return NewError(resultCode, errors.New(resultDescription))
}
} else {
log.Printf("Unexpected Response: %d", packet.Children[1].Tag)
}
2016-06-30 18:19:03 +00:00
l.Debug.Printf("%d: returning", msgCtx.id)
2015-05-07 17:59:38 +00:00
return nil
}