add.c
This is an example of using an add operation to add a new entry (a person) to the directory.
To compile this example on Unix:
cc -I /opt/isode/include -c add.c
To compile this example on Windows:
cl /nologo /I C:\Progra~1\Isode\include /c add.c
To link this example on Unix:
cc -o add add.o -L/opt/isode/lib \ -ldua -lisode -libase -lldap -llber \ -lssl -lcrypto -lpthread
To link this example on Windows:
cl /nologo /o add.exe add.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>
#include <isode/ds/dsapi/dsapi.h>
#define S(a) (a), strlen(a)
int main ( void )
{
DS_Session *ds = NULL;
DS_Indication *di = NULL;
DS_ErrorType t;
DS_DN *dn = NULL;
DS_Attr *objectclass = NULL;
DS_Attr *cn = NULL;
DS_Attr *sn = NULL;
DS_Attr *mail = NULL;
DS_Entry *entry = NULL;
DS_Status status;
int rc = 1;
status = DS_Initialize( );
fprintf( stderr, "Initialization failed\n" );
goto cleanup;
}
if ( status != DS_E_NOERROR ) {
fprintf( stderr, "Bad Manager DN\n" );
goto cleanup;
}
if ( status != DS_E_NOERROR ) {
fprintf( stderr, "Failed to create session\n" );
goto cleanup;
}
t != DS_E_SUCCESS )
status = DS_E_DSOPFAILED;
DS_Indication_Delete( di );
DS_DN_Delete( dn );
dn = NULL;
if ( status != DS_E_NOERROR ) {
fprintf( stderr, "Bind failed\n" );
goto cleanup;
}
/* Build an entry to add */
status = DS_Entry_New( &entry );
if ( status != DS_E_NOERROR ) {
fprintf( stderr, "Cannot create new entry\n" );
goto cleanup;
}
status = DS_String2DN( "cn=John Smith,ou=People,dc=example,dc=com", &dn );
if ( status != DS_E_NOERROR ) {
fprintf( stderr, "Bad New DN\n" );
goto cleanup;
}
status = DS_Entry_SetDN( entry, dn );
if ( status != DS_E_NOERROR ) {
fprintf( stderr, "Cannot set new DN\n" );
goto cleanup;
}
if ( status != DS_E_NOERROR ) {
fprintf( stderr, "Cannot create new objectclass\n" );
goto cleanup;
}
if ( status != DS_E_NOERROR) {
fprintf( stderr, "Cannot add top\n" );
goto cleanup;
}
status = DS_Attr_AddStringValue( objectclass, S("person") );
if ( status != DS_E_NOERROR) {
fprintf( stderr, "Cannot add person\n" );
goto cleanup;
}
status = DS_Attr_AddStringValue( objectclass, S("organizationalPerson") );
if ( status != DS_E_NOERROR) {
fprintf( stderr, "Cannot add organizationalPerson\n" );
goto cleanup;
}
status = DS_Attr_AddStringValue( objectclass, S("inetOrgPerson") );
if ( status != DS_E_NOERROR) {
fprintf( stderr, "Cannot add inetOrgPerson\n" );
goto cleanup;
}
status = DS_Attr_New( "cn", &cn );
if ( status != DS_E_NOERROR) {
fprintf( stderr, "Cannot create new cn\n" );
goto cleanup;
}
status = DS_Attr_AddStringValue( cn, S("John Smith") );
if ( status != DS_E_NOERROR) {
fprintf( stderr, "Cannot add John Smith\n" );
goto cleanup;
}
status = DS_Attr_New( "sn", &sn );
if ( status != DS_E_NOERROR) {
fprintf( stderr, "Cannot create new sn\n" );
goto cleanup;
}
status = DS_Attr_AddStringValue( sn, S("Smith") );
if ( status != DS_E_NOERROR) {
fprintf( stderr, "Cannot add Smith\n" );
goto cleanup;
}
status = DS_Attr_New( "mail", &mail );
if ( status != DS_E_NOERROR) {
fprintf( stderr, "Cannot create new mail\n" );
goto cleanup;
}
status = DS_Attr_AddStringValue( mail, S("john.smith@example.com") );
if ( status != DS_E_NOERROR) {
fprintf( stderr, "Cannot add john.smith@example.com\n" );
goto cleanup;
}
status = DS_Entry_AddValues( entry, objectclass );
if ( status != DS_E_NOERROR ) {
fprintf( stderr, "Cannot add objectclass attribute\n" );
goto cleanup;
}
status = DS_Entry_AddValues( entry, cn );
if ( status != DS_E_NOERROR ) {
fprintf( stderr, "Cannot add cn attribute\n" );
goto cleanup;
}
status = DS_Entry_AddValues( entry, sn );
if ( status != DS_E_NOERROR ) {
fprintf( stderr, "Cannot add sn attribute\n" );
goto cleanup;
}
status = DS_Entry_AddValues( entry, mail );
if ( status != DS_E_NOERROR ) {
fprintf( stderr, "Cannot add mail attribute\n" );
goto cleanup;
}
status = DS_AddSync( ds, entry, NULL, &di );
if ( di != NULL &&
DS_Indication_GetErrorCodes( di, &t, &v ) != DS_E_NOERROR )
fprintf( stderr, "Add returns error type %d and error value %d\n",
t, v );
if ( status != DS_E_NOERROR ) {
fprintf( stderr, "Add operation failed\n" );
goto cleanup;
}
rc = 0;
cleanup:
if ( di != NULL ) DS_Indication_Delete( di );
if ( sn != NULL ) DS_Attr_Delete( sn );
if ( cn != NULL ) DS_Attr_Delete( cn );
if ( objectclass != NULL ) DS_Attr_Delete( objectclass );
return rc;
}
DS_Status DS_AddSync(DS_Session *session, const DS_Entry *entry, const DS_CommonArgs *common_args, DS_Indication **indication_p)
Perform a synchronous directory add operation.
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_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_Indication_GetErrorCodes(DS_Indication *indication, DS_ErrorType *type_p, DS_ErrorValue *value_p)
Get the directory (operation) error type and value codes.
DS_Status DS_Session_New(const char *address, int force_tls, DS_Session **session_p)
Create an unbound directory session, validating the specified address.
Methods for session management and invoking directory operations.
DS_Status DS_Entry_AddValues(DS_Entry *entry, const DS_Attr *attr)
Append an add-attribute (with values) change to a DS_Entry.
DS_Status DS_String2DN(const char *str_dn, DS_DN **dn_p)
Convert an LDAPv3 string formatted DN to the API structure.
void DS_Indication_Delete(DS_Indication *indication)
Free a DS_Indication structure.
DS_Status DS_Entry_New(DS_Entry **entryp)
Create a DS_Entry structure for a Modify change-entry.