modify.c

This is an example of using a modify operation to change an existing entry in the directory.

To compile this example on Unix:

cc -I /opt/isode/include -c modify.c 

To compile this example on Windows:

cl /nologo /I C:\Progra~1\Isode\include /c modify.c

To link this example on Unix:

cc -o modify modify.o -L/opt/isode/lib \
    -ldua -lisode -libase -lldap -llber \
    -lssl -lcrypto -lpthread

To link this example on Windows:

cl /nologo /o modify.exe modify.obj \
    C:\Progra~1\Isode\bin\libdua.lib
/*
* Copyright (c) 2008-2009, Isode Limited, London, England.
* All rights reserved.
*
* Acquisition and use of this software and related materials for any
* purpose requires a written licence agreement from Isode Limited,
* or a written licence from an organisation licenced by Isode Limited
* to grant such a licence.
*/
#include <stdio.h>
#include <string.h>
#define S(a) (a), strlen(a)
int main ( void )
{
DS_Session *ds = NULL;
DS_Indication *di = NULL;
DS_DN *dn = NULL;
DS_Attr *sn = NULL;
DS_Attr *mail = NULL;
DS_Attr *description = NULL;
DS_Entry *entry = NULL;
DS_Status status;
int rc = 1;
status = DS_Initialize( );
if ( status != DS_E_NOERROR ) {
fprintf( stderr, "Initialization failed\n" );
goto cleanup;
}
status = DS_String2DN( "cn=DSA Manager,cn=dsa,dc=example,dc=com", &dn );
if ( status != DS_E_NOERROR ) {
fprintf( stderr, "Bad Manager DN\n" );
goto cleanup;
}
status = DS_Session_New( "Internet=localhost+19999", 0, &ds );
if ( status != DS_E_NOERROR ) {
fprintf( stderr, "Failed to create session\n" );
goto cleanup;
}
status = DS_BindSimpleSync( ds, dn, "secret", NULL, &di );
if ( DS_Indication_GetErrorCodes( di, &t, &v ) == DS_E_NOERROR &&
t != DS_E_SUCCESS )
status = DS_E_DSOPFAILED;
DS_DN_Delete( dn );
dn = NULL;
if ( status != DS_E_NOERROR ) {
fprintf( stderr, "Bind failed\n" );
goto cleanup;
}
/* Build a set of changes */
status = DS_Entry_New( &entry );
status = DS_String2DN( "cn=John Smith,ou=People,dc=example,dc=com", &dn );
status = DS_Entry_SetDN( entry, dn );
status = DS_Attr_New( "sn", &sn );
status = DS_Attr_AddStringValue( sn, S("Schmidt") );
status = DS_Entry_AddValues( entry, sn );
status = DS_Attr_New( "mail", &mail );
/* no values means delete entire attribute */
status = DS_Entry_DeleteValues( entry, mail );
status = DS_Attr_New( "description", &description );
status = DS_Attr_AddStringValue( description, S("new surname") );
status = DS_Entry_ReplaceValues( entry, description );
status = DS_ModifySync( ds, entry, NULL, &di );
if ( di != NULL &&
fprintf( stderr, "Modify returns error type %d and error value %d\n",
t, v );
if ( status != DS_E_NOERROR ) {
fprintf( stderr, "Modify operation failed\n" );
goto cleanup;
}
rc = 0;
cleanup:
if ( di != NULL ) DS_Indication_Delete( di );
if ( entry != NULL ) DS_Entry_Delete( entry );
if ( mail != NULL ) DS_Attr_Delete( mail );
if ( sn != NULL ) DS_Attr_Delete( sn );
if ( description != NULL ) DS_Attr_Delete( description );
if ( ds != NULL ) DS_UnbindSync( &ds );
return rc;
}
struct DS_DN DS_DN
Definition: dsapi_types.h:103
DS_Status
Definition: dsapi_const.h:66
DS_Status DS_Attr_AddStringValue(DS_Attr *attr, const char *str, size_t len)
Add a new string value to a DS_Attr.
DS_Status DS_Entry_SetDN(DS_Entry *entry, const DS_DN *dn)
Set the DN of the given DS_Entry.
DS_ErrorType
Definition: dsapi_const.h:138
void DS_UnbindSync(DS_Session **session_p)
Perform a synchronous directory unbind.
void DS_Entry_Delete(DS_Entry *entry)
Free a DS_Entry.
DS_Status DS_BindSimpleSync(DS_Session *session, const DS_DN *dn, const char *password, DS_CommonArgs *common_args, DS_Indication **indication_p)
Perform a synchronous directory bind using simple credentials.
DS_Status DS_ModifySync(DS_Session *session, const DS_Entry *entry, const DS_CommonArgs *common_args, DS_Indication **indication_p)
Perform a synchronous directory modify operation.
@ DS_E_DSOPFAILED
Definition: dsapi_const.h:103
DS_Status DS_Entry_DeleteValues(DS_Entry *entry, const DS_Attr *attr)
Append a delete-attribute or delete-attribute-values change to a DS_Entry.
@ DS_E_NOERROR
Definition: dsapi_const.h:67
DS_Status DS_Indication_GetErrorCodes(DS_Indication *indication, DS_ErrorType *type_p, DS_ErrorValue *value_p)
Get the directory (operation) error type and value codes.
struct DS_Entry DS_Entry
Definition: dsapi_types.h:81
DS_Status DS_Session_New(const char *address, int force_tls, DS_Session **session_p)
Create an unbound directory session, validating the specified address.
DS_Status DS_Attr_New(const char *name, DS_Attr **attr_p)
Create a new DS_Attr structure.
Methods for session management and invoking directory operations.
struct DS_Indication DS_Indication
Definition: dsapi_types.h:121
struct DS_Session DS_Session
Definition: dsapi_types.h:44
void DS_Attr_Delete(DS_Attr *attr)
Free a DS_Attr structure.
DS_Status DS_Entry_ReplaceValues(DS_Entry *entry, const DS_Attr *attr)
Append a replace-attribute-values change to a DS_Entry.
DS_Status DS_Entry_AddValues(DS_Entry *entry, const DS_Attr *attr)
Append an add-attribute (with values) change to a DS_Entry.
DS_ErrorValue
Definition: dsapi_const.h:190
DS_Status DS_String2DN(const char *str_dn, DS_DN **dn_p)
Convert an LDAPv3 string formatted DN to the API structure.
struct DS_Attr DS_Attr
Definition: dsapi_types.h:75
void DS_Indication_Delete(DS_Indication *indication)
Free a DS_Indication structure.
void DS_DN_Delete(DS_DN *dn)
Free a DN.
DS_Status DS_Entry_New(DS_Entry **entryp)
Create a DS_Entry structure for a Modify change-entry.
DS_Status DS_Initialize(void)
Initialize the Simple Directory API.

All rights reserved © 2002 - 2024 Isode Ltd.