compare.go 2.7 KB

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