14.4. LDAPMod

Specifies changes to an attribute in a directory entry.
LDAPMod is a type of structure that specifies changes to an attribute in a directory entry. Before you call the slapi_add_internal_pb() and slapi_modify_internal_pb() routines to add or modify an entry in the directory, you need to fill LDAPMod structures with the attribute values that you intend to add or change.
The following section of code sets up an LDAPMod structure to change the email address of a user's entry to bab@example.com:
Slapi_PBlock *mod_pb = slapi_pblock_new();
LDAPMod attribute1;
LDAPMod *list_of_attrs[2];
char *mail_values[] = { "bab@example.com" , NULL };
Slapi_DN *dn;
int ret = 0;

...
...

/* Identify the entry that you want changed */
dn = "cn=Barbara Jensen, ou=Product Development, l=US, dc=example,dc=com" ;

/* Specify that you want to replace the value of an attribute */
attribute1.mod_op = LDAP_MOD_REPLACE;

/* Specify that you want to change the value of the mail attribute */
attribute1.mod_type = "mail" ;

/* Specify the new value of the mail attribute */
attribute1.mod_values = mail_values;

/* Add the change to the list of attributes that you want changed */
list_of_attrs[0] = &attribute_change ;
list_of_attrs[1] = NULL;

/* Update the entry with the change */
slapi_modify_internal_set_pb(mod_pb, config_entry->dn,
                             list_of_attrs, 0, 0, getPluginID(), 0);
slapi_modify_internal_pb(mod_pb);
slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
slapi_pblock_destroy(mod_pb);

...
...
Table 14.4, “Frontend API Functions for Entry Attribute Changes” summarizes the functions available to specify changes to an attribute in a directory entry.

Table 14.4. Frontend API Functions for Entry Attribute Changes

To perform this action... Call this function
Translate from entry to LDAPMod. slapi_entry2mods()
Dump the contents of an LDAPMod to the server log. slapi_mod_dump()
Get a reference to the LDAPMod in a Slapi_Mod structure. slapi_mod_get_ldapmod_byref()
Retrieve the reference to the LDAPMod contained in a Slapi_Mod structure. slapi_mod_get_ldapmod_passout()
Syntax

This struct definition uses the following syntax:

typedef struct ldapmod {
 int mod_op;
 char *mod_type;
 union mod_vals_u{
 char **modv_strvals;
 struct berval **modv_bvals;
 } mod_vals;

#define mod_values mod_vals.modv_strvals
#define mod_bvalues mod_vals.modv_bvals
} LDAPMod;
Fields

This struct definition contains the following fields:

Table 14.5. ldapmod Field Listing

Field Description
mod_op The operation to be performed on the attribute and the type of data specified as the attribute values. This field can have one of the following values:
  • #define LDAP_MOD_ADD 0x00
    LDAP_MOD_ADD specifies to add the attribute values to the entry.
  • #define LDAP_MOD_DELETE 0x01
    LDAP_MOD_DELETE specifies to remove the attribute values from the entry.
  • #define LDAP_MOD_REPLACE 0x02
    LDAP_MOD_REPLACE specifies to replace the existing value of the attribute with the values in mod_values or mod_bvalues.
  • #define LDAP_MOD_BVALUES 0x80
In addition, if you are specifying binary values (as opposed to strings), you should OR (|) LDAP_MOD_BVALUES with the operation type. For example:
 mod->mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES
mod_type Pointer to the attribute type that you want to add, delete, or replace.
mod_values_u A NULL-terminated array of string values for the attribute.
modv_strvals Pointer to a NULL terminated array of string values for the attribute.
mod_bvalues Pointer to a NULL-terminated array of berval structures for the attribute.
mod_vals Values that you want to add, delete, or replace.