{"id":284,"date":"2018-04-27T05:31:02","date_gmt":"2018-04-27T05:31:02","guid":{"rendered":"https:\/\/ldap.com\/ldapv3-wire-protocol-reference-the-ldap-compare-operation\/"},"modified":"2018-05-06T17:48:24","modified_gmt":"2018-05-06T22:48:24","slug":"ldapv3-wire-protocol-reference-compare","status":"publish","type":"page","link":"https:\/\/ldap.com\/ldapv3-wire-protocol-reference-compare\/","title":{"rendered":"LDAPv3 Wire Protocol Reference: The LDAP Compare Operation"},"content":{"rendered":"<p>\n  The LDAP compare operation may be used to determine whether a given entry has a specified attribute value. Each compare operation consists of one request message and one response message.\n<\/p>\n<p>\n  Technically, you can accomplish this with a search (using a base DN of the target entry DN, a scope of baseObject, and an equality filter with the target attribute type and assertion value; if the server returns a search result entry, the entry has the specified value), but the compare operation does this more efficiently because the compare operation can do this without the need for the extra search result entry message.\n<\/p>\n<p>\n  Honestly, the compare operation isn\u2019t used all that often in most deployments. Nevertheless, it is a core LDAPv3 operation type, so it is described here for completeness.\n<\/p>\n<p><a name=\"compare-request\"><\/a><\/p>\n<h3>The Compare Request<\/h3>\n<p>\n  <a href=\"https:\/\/docs.ldap.com\/specs\/rfc4511.txt\" target=\"_blank\">RFC 4511<\/a> section 4.10 defines the compare request protocol operation as follows:\n<\/p>\n<pre>CompareRequest ::= [APPLICATION 14] SEQUENCE {\n     entry           LDAPDN,\n     ava             AttributeValueAssertion }<\/pre>\n<p>\n  And its dependencies, described elsewhere in <a href=\"https:\/\/docs.ldap.com\/specs\/rfc4511.txt\" target=\"_blank\">RFC 4511<\/a>, are:\n<\/p>\n<pre>LDAPDN ::= LDAPString -- Constrained to &lt;distinguishedName&gt;\n        -- [RFC4514]\n\nAttributeValueAssertion ::= SEQUENCE {\n     attributeDesc   AttributeDescription,\n     assertionValue  AssertionValue }\n\nLDAPString ::= OCTET STRING -- UTF-8 encoded,\n              -- [ISO10646] characters\n\nAttributeDescription ::= LDAPString\n          -- Constrained to &lt;attributedescription&gt;\n          -- [RFC4512]\n\nAttributeValue ::= OCTET STRING<\/pre>\n<p>\n  That is to say that the compare request protocol operation is a sequence with a BER type of <tt>0x6E<\/tt> (application class, constructed, tag number fourteen) and the following two elements:\n<\/p>\n<ul>\n<li><tt>entry<\/tt> \u2014 The DN of the target entry. This is an octet string containing the string representation of the entry DN.<\/li>\n<\/ul>\n<ul>\n<li><tt>ava<\/tt> \u2014 The attribute-value assertion. This is a sequence containing two octet strings: the first for the attribute description, and the second for the assertion value. Most of the time, the attribute description is just the name or OID for the target attribute type, but it may also include one or more attribute options (each of which would be preceded by a semicolon).<\/li>\n<\/ul>\n<p>\n  For example, let\u2019s say that you want to determine whether the entry <tt>uid=jdoe,ou=People,dc=example,dc=com<\/tt> has an <tt>employeeType<\/tt> attribute with a value of <tt>salaried<\/tt>. If the compare request has message ID two and no request controls, it would be encoded as follows:\n<\/p>\n<pre>30 45 -- Begin the LDAPMessage sequence\n   02 01 02 -- The message ID (integer value 2)\n   6e 40 -- Begin the compare request protocol op\n   04 24 75 69 64 3d 6a 64 6f 65 -- The target entry DN (octet string\n         2c 6f 75 3d 50 65 6f 70 -- \"uid=jdoe,ou=People,dc=example,dc=com\")\n         6c 65 2c 64 63 3d 65 78\n         61 6d 70 6c 65 2c 64 63\n         3d 63 6f 6d\n   30 18 -- Begin the attribute value assertion sequence\n      04 0c 65 6d 70 6c 6f 79 65 65 -- The attribute description (octet string\n            54 79 70 65             -- \"employeeType\")\n      04 08 73 61 6c 61 72 69 65 64 -- The assertion value (octet string \"salaried\")<\/pre>\n<p><a name=\"compare-response\"><\/a><\/p>\n<h3>The Compare Response<\/h3>\n<p>\n  The compare response protocol operation is also defined in <a href=\"https:\/\/docs.ldap.com\/specs\/rfc4511.txt\" target=\"_blank\">RFC 4511<\/a> section 4.10. That definition is:\n<\/p>\n<pre>CompareResponse ::= [APPLICATION 15] LDAPResult<\/pre>\n<p>\n  We\u2019ve already covered the <tt>LDAPResult<\/tt> element in an <a href=\"https:\/\/ldap.com\/ldapv3-wire-protocol-reference-ldap-result\">earlier section<\/a>, so there\u2019s no need to go into a lot of detail about it again here. But there is one unusual thing about the compare response: it won\u2019t ever include a result code of <tt>success<\/tt> (0). Some of the result codes that may be used for the compare operation are:\n<\/p>\n<ul>\n<li><tt>compareTrue<\/tt> (6) \u2014 Indicates that the target entry exists and contains the specified attribute with the indicated value.<\/li>\n<li><tt>compareFalse<\/tt> (5) \u2014 Indicates that the target entry exists and contains the specified attribute, but that the attribute does not have the indicated value.<\/li>\n<li><tt>noSuchObject<\/tt> (32) \u2014 Indicates that the target entry does not exist.<\/li>\n<li><tt>noSuchAttribute<\/tt> (16) \u2014 Indicates that the target entry exists but does not contain the specified attribute.<\/li>\n<\/ul>\n<p>\n  There may, of course, be other result codes for other error conditions (e.g., if the requester doesn\u2019t have permission to make the comparison, if the server is too busy, if the server encounters a referral while locating the target entry, if an internal server error occurs, etc.), but the above result codes are the most directly applicable.\n<\/p>\n<p>\n  If the server returns a compare response with a result code of <tt>compareTrue<\/tt>, then that would be encoded as:\n<\/p>\n<pre>30 0c -- Begin the LDAPMessage sequence\n   02 01 02 -- The message ID (integer value 2)\n   6f 07 -- Begin the compare response protocol op\n      0a 01 06 -- compareTrue result code (enumerated value 6)\n      04 00 -- No matched DN (0-byte octet string)\n      04 00 -- No diagnostic message (0-byte octet string)<\/pre>\n<\/p>\n<table border=\"0\" width=\"100%\">\n<tr>\n<td align=\"left\">Previous: <a href=\"https:\/\/ldap.com\/ldapv3-wire-protocol-reference-bind\">The LDAP Bind Operation<\/a><\/td>\n<td align=\"right\">Next: <a href=\"https:\/\/ldap.com\/ldapv3-wire-protocol-reference-delete\">The LDAP Delete Operation<\/a><\/td>\n<\/tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>The LDAP compare operation may be used to determine whether a given entry has a specified attribute value. Each compare operation consists of one request message and one response message. Technically, you can accomplish this with a search (using a base DN of the target entry DN, a scope of baseObject, and an equality filter&hellip; <span class=\"excerpt-more\"><a href=\"https:\/\/ldap.com\/ldapv3-wire-protocol-reference-compare\/\">Read More<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"footnotes":""},"tags":[],"class_list":["post-284","page","type-page","status-publish","hentry"],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/P9RddA-4A","jetpack_likes_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ldap.com\/wp-json\/wp\/v2\/pages\/284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ldap.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/ldap.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/ldap.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ldap.com\/wp-json\/wp\/v2\/comments?post=284"}],"version-history":[{"count":3,"href":"https:\/\/ldap.com\/wp-json\/wp\/v2\/pages\/284\/revisions"}],"predecessor-version":[{"id":547,"href":"https:\/\/ldap.com\/wp-json\/wp\/v2\/pages\/284\/revisions\/547"}],"wp:attachment":[{"href":"https:\/\/ldap.com\/wp-json\/wp\/v2\/media?parent=284"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ldap.com\/wp-json\/wp\/v2\/tags?post=284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}