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

85 lines
2.1 KiB
Go
Raw Normal View History

2015-10-07 20:10:00 +00:00
//
// https://tools.ietf.org/html/rfc4511
//
// DelRequest ::= [APPLICATION 10] LDAPDN
package ldap
import (
"errors"
"log"
"gopkg.in/asn1-ber.v1"
)
2016-07-23 00:11:47 +00:00
// DelRequest implements an LDAP deletion request
2015-10-07 20:10:00 +00:00
type DelRequest struct {
2016-07-23 00:11:47 +00:00
// DN is the name of the directory entry to delete
DN string
// Controls hold optional controls to send with the request
2015-10-07 20:10:00 +00:00
Controls []Control
}
func (d DelRequest) encode() *ber.Packet {
request := ber.Encode(ber.ClassApplication, ber.TypePrimitive, ApplicationDelRequest, d.DN, "Del Request")
request.Data.Write([]byte(d.DN))
return request
}
2016-07-23 00:11:47 +00:00
// NewDelRequest creates a delete request for the given DN and controls
2015-10-07 20:10:00 +00:00
func NewDelRequest(DN string,
Controls []Control) *DelRequest {
return &DelRequest{
DN: DN,
Controls: Controls,
}
}
2016-07-23 00:11:47 +00:00
// Del executes the given delete request
2015-10-07 20:10:00 +00:00
func (l *Conn) Del(delRequest *DelRequest) 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-10-07 20:10:00 +00:00
packet.AppendChild(delRequest.encode())
if len(delRequest.Controls) > 0 {
2015-10-07 20:10:00 +00:00
packet.AppendChild(encodeControls(delRequest.Controls))
}
l.Debug.PrintPacket(packet)
2016-06-30 18:19:03 +00:00
msgCtx, err := l.sendMessage(packet)
2015-10-07 20:10:00 +00:00
if err != nil {
return err
}
2016-06-30 18:19:03 +00:00
defer l.finishMessage(msgCtx)
2015-10-07 20:10:00 +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-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 == ApplicationDelResponse {
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-10-07 20:10:00 +00:00
return nil
}