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

171 lines
5.8 KiB
Go
Raw Normal View History

2015-05-07 17:59:38 +00:00
// Copyright 2014 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.
//
// 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
}
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
// AddAttributes contain the attributes to add
AddAttributes []PartialAttribute
// DeleteAttributes contain the attributes to delete
DeleteAttributes []PartialAttribute
// ReplaceAttributes contain the attributes to replace
2016-06-08 14:33:08 +00:00
ReplaceAttributes []PartialAttribute
2015-05-07 17:59:38 +00:00
}
2016-07-23 00:11:47 +00:00
// Add inserts the given attribute to the list of attributes to add
2015-05-07 17:59:38 +00:00
func (m *ModifyRequest) Add(attrType string, attrVals []string) {
2016-06-08 14:33:08 +00:00
m.AddAttributes = append(m.AddAttributes, PartialAttribute{Type: attrType, Vals: attrVals})
2015-05-07 17:59:38 +00:00
}
2016-07-23 00:11:47 +00:00
// Delete inserts the given attribute to the list of attributes to delete
2015-05-07 17:59:38 +00:00
func (m *ModifyRequest) Delete(attrType string, attrVals []string) {
2016-06-08 14:33:08 +00:00
m.DeleteAttributes = append(m.DeleteAttributes, PartialAttribute{Type: attrType, Vals: attrVals})
2015-05-07 17:59:38 +00:00
}
2016-07-23 00:11:47 +00:00
// Replace inserts the given attribute to the list of attributes to replace
2015-05-07 17:59:38 +00:00
func (m *ModifyRequest) Replace(attrType string, attrVals []string) {
2016-06-08 14:33:08 +00:00
m.ReplaceAttributes = append(m.ReplaceAttributes, 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")
2016-06-08 14:33:08 +00:00
for _, attribute := range m.AddAttributes {
2015-05-07 17:59:38 +00:00
change := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Change")
2015-06-29 21:50:55 +00:00
change.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagEnumerated, uint64(AddAttribute), "Operation"))
2015-05-07 17:59:38 +00:00
change.AppendChild(attribute.encode())
changes.AppendChild(change)
}
2016-06-08 14:33:08 +00:00
for _, attribute := range m.DeleteAttributes {
2015-05-07 17:59:38 +00:00
change := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Change")
2015-06-29 21:50:55 +00:00
change.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagEnumerated, uint64(DeleteAttribute), "Operation"))
2015-05-07 17:59:38 +00:00
change.AppendChild(attribute.encode())
changes.AppendChild(change)
}
2016-06-08 14:33:08 +00:00
for _, attribute := range m.ReplaceAttributes {
2015-05-07 17:59:38 +00:00
change := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Change")
2015-06-29 21:50:55 +00:00
change.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagEnumerated, uint64(ReplaceAttribute), "Operation"))
2015-05-07 17:59:38 +00:00
change.AppendChild(attribute.encode())
changes.AppendChild(change)
}
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,
) *ModifyRequest {
return &ModifyRequest{
2016-06-08 14:33:08 +00:00
DN: dn,
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())
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
}