Sun Java System Directory Server Enterprise Edition 6.2 Developer's Guide

Extending the Compare Operation

This section shows how to develop functionality called by Directory Server before a client compare operation. The following example logs the target DN and attribute of the entry with which to compare values. For complete example code, refer to install-path/examples/testpreop.c.

Example 6–7 Plug-In Comparison Function (testpreop.c)

#include "slapi-plugin.h"

testpreop_cmp(Slapi_PBlock * pb)
    char * dn;                         /* Target DN               */
    char * attr_type;                  /* Type of attr to compare */
    /* Attribute value could be lots of things, even a binary file.
     * Here, do not try to retrieve the value to compare.         */
    int    connId, opId, rc = 0;
    long   msgId;

    rc |= slapi_pblock_get(pb, SLAPI_COMPARE_TARGET,  &dn);
    rc |= slapi_pblock_get(pb, SLAPI_COMPARE_TYPE,    &attr_type);
    rc |= slapi_pblock_get(pb, SLAPI_OPERATION_MSGID, &msgId);
    rc |= slapi_pblock_get(pb, SLAPI_CONN_ID,         &connId);
    rc |= slapi_pblock_get(pb, SLAPI_OPERATION_ID,    &opId);
    if (rc == 0) {
            "testpreop_cmp in test-preop plug-in",
            "Target DN: %s\tAttribute type: %s\n", dn, attr_type

    return (rc);

The plug-in function can access the attribute value to use for the comparison as well. The attribute value in the parameter block is in a berval structure. Thus, the value could be binary data such as a JPEG image. No attempt is made to write the value to the logs.

The following example shows the slapi_pblock_get() call used to obtain the attribute value.

Example 6–8 Obtaining the Attribute Value

#include "slapi-plugin.h"

my_compare_fn(Slapi_PBlock * pb)
    int             rc = 0;
    struct berval * attr_val;

    /* Obtain the attribute value from the parameter block */
    rc |= slapi_pblock_get(pb, SLAPI_COMPARE_VALUE, &attr_val);

    if (rc != 0) {
        slapi_send_ldap_result(pb, rc, NULL, NULL, 0, NULL);
        return 0;

    /* Do something with the value here.                   */

    return 0;

Before using the plug-in function as described here, set up the example suffix and register the plug-in. See Extending the Bind Operation and “To register the Plug-in”, as described previously.

Try the example plug-in function using the ldapcompare tool installed with the Directory Server Resource Kit.

Example 6–9 Performing a Comparison

$ ldapcompare sn:Jensen uid=bjensen,ou=people,dc=example,dc=com
comparing type: "sn" value: "Jensen" in entry
compare TRUE

The log entry in instance-path/logs/errors shows the following results, not including housekeeping information at the beginning of the log entry:

Target DN: uid=bjensen,ou=people,dc=example,dc=com	Attribute type: sn