You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.6 KiB

  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.TypePrimitive, 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. err := GetLDAPError(packet)
  61. switch {
  62. case IsErrorWithCode(err, LDAPResultCompareTrue):
  63. return true, nil
  64. case IsErrorWithCode(err, LDAPResultCompareFalse):
  65. return false, nil
  66. default:
  67. return false, err
  68. }
  69. }
  70. return false, fmt.Errorf("unexpected Response: %d", packet.Children[1].Tag)
  71. }