del.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // https://tools.ietf.org/html/rfc4511
  3. //
  4. // DelRequest ::= [APPLICATION 10] LDAPDN
  5. package ldap
  6. import (
  7. "errors"
  8. "log"
  9. "gopkg.in/asn1-ber.v1"
  10. )
  11. // DelRequest implements an LDAP deletion request
  12. type DelRequest struct {
  13. // DN is the name of the directory entry to delete
  14. DN string
  15. // Controls hold optional controls to send with the request
  16. Controls []Control
  17. }
  18. func (d DelRequest) encode() *ber.Packet {
  19. request := ber.Encode(ber.ClassApplication, ber.TypePrimitive, ApplicationDelRequest, d.DN, "Del Request")
  20. request.Data.Write([]byte(d.DN))
  21. return request
  22. }
  23. // NewDelRequest creates a delete request for the given DN and controls
  24. func NewDelRequest(DN string,
  25. Controls []Control) *DelRequest {
  26. return &DelRequest{
  27. DN: DN,
  28. Controls: Controls,
  29. }
  30. }
  31. // Del executes the given delete request
  32. func (l *Conn) Del(delRequest *DelRequest) error {
  33. packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request")
  34. packet.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, l.nextMessageID(), "MessageID"))
  35. packet.AppendChild(delRequest.encode())
  36. if delRequest.Controls != nil {
  37. packet.AppendChild(encodeControls(delRequest.Controls))
  38. }
  39. l.Debug.PrintPacket(packet)
  40. msgCtx, err := l.sendMessage(packet)
  41. if err != nil {
  42. return err
  43. }
  44. defer l.finishMessage(msgCtx)
  45. l.Debug.Printf("%d: waiting for response", msgCtx.id)
  46. packetResponse, ok := <-msgCtx.responses
  47. if !ok {
  48. return NewError(ErrorNetwork, errors.New("ldap: response channel closed"))
  49. }
  50. packet, err = packetResponse.ReadPacket()
  51. l.Debug.Printf("%d: got response %p", msgCtx.id, packet)
  52. if err != nil {
  53. return err
  54. }
  55. if l.Debug {
  56. if err := addLDAPDescriptions(packet); err != nil {
  57. return err
  58. }
  59. ber.PrintPacket(packet)
  60. }
  61. if packet.Children[1].Tag == ApplicationDelResponse {
  62. resultCode, resultDescription := getLDAPResultCode(packet)
  63. if resultCode != 0 {
  64. return NewError(resultCode, errors.New(resultDescription))
  65. }
  66. } else {
  67. log.Printf("Unexpected Response: %d", packet.Children[1].Tag)
  68. }
  69. l.Debug.Printf("%d: returning", msgCtx.id)
  70. return nil
  71. }