compare.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //
  5. // File contains Compare functionality
  6. //
  7. // https://tools.ietf.org/html/rfc4511
  8. //
  9. // CompareRequest ::= [APPLICATION 14] SEQUENCE {
  10. // entry LDAPDN,
  11. // ava AttributeValueAssertion }
  12. //
  13. // AttributeValueAssertion ::= SEQUENCE {
  14. // attributeDesc AttributeDescription,
  15. // assertionValue AssertionValue }
  16. //
  17. // AttributeDescription ::= LDAPString
  18. // -- Constrained to <attributedescription>
  19. // -- [RFC4512]
  20. //
  21. // AttributeValue ::= OCTET STRING
  22. //
  23. package ldap
  24. import (
  25. "errors"
  26. "fmt"
  27. "gopkg.in/asn1-ber.v1"
  28. )
  29. // Compare checks to see if the attribute of the dn matches value. Returns true if it does otherwise
  30. // false with any error that occurs if any.
  31. func (l *Conn) Compare(dn, attribute, value string) (bool, error) {
  32. packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request")
  33. packet.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, l.nextMessageID(), "MessageID"))
  34. request := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationCompareRequest, nil, "Compare Request")
  35. request.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, dn, "DN"))
  36. ava := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "AttributeValueAssertion")
  37. ava.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, attribute, "AttributeDesc"))
  38. ava.AppendChild(ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagOctetString, value, "AssertionValue"))
  39. request.AppendChild(ava)
  40. packet.AppendChild(request)
  41. l.Debug.PrintPacket(packet)
  42. msgCtx, err := l.sendMessage(packet)
  43. if err != nil {
  44. return false, err
  45. }
  46. defer l.finishMessage(msgCtx)
  47. l.Debug.Printf("%d: waiting for response", msgCtx.id)
  48. packetResponse, ok := <-msgCtx.responses
  49. if !ok {
  50. return false, NewError(ErrorNetwork, errors.New("ldap: response channel closed"))
  51. }
  52. packet, err = packetResponse.ReadPacket()
  53. l.Debug.Printf("%d: got response %p", msgCtx.id, packet)
  54. if err != nil {
  55. return false, err
  56. }
  57. if l.Debug {
  58. if err := addLDAPDescriptions(packet); err != nil {
  59. return false, err
  60. }
  61. ber.PrintPacket(packet)
  62. }
  63. if packet.Children[1].Tag == ApplicationCompareResponse {
  64. resultCode, resultDescription := getLDAPResultCode(packet)
  65. if resultCode == LDAPResultCompareTrue {
  66. return true, nil
  67. } else if resultCode == LDAPResultCompareFalse {
  68. return false, nil
  69. } else {
  70. return false, NewError(resultCode, errors.New(resultDescription))
  71. }
  72. }
  73. return false, fmt.Errorf("Unexpected Response: %d", packet.Children[1].Tag)
  74. }