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 \
      -lisode_ssl -lisode_crypto -lpthread
  

To link this example on Windows:

  cl /nologo /o add.exe add.obj \
      C:\Progra~1\Isode\bin\libdua.lib
  

00001 /* 
00002  * Copyright (c) 2008, Isode Limited, London, England.
00003  * All rights reserved.
00004  * 
00005  * Acquisition and use of this software and related materials for any
00006  * purpose requires a written licence agreement from Isode Limited,
00007  * or a written licence from an organisation licenced by Isode Limited
00008  * to grant such a licence.
00009  */
00010 #include <stdio.h>
00011 #include <string.h>
00012 
00013 #include <isode/ds/dsapi/dsapi.h>
00014 
00020 #define S(a) (a), strlen(a)
00021 
00026 int main ( void )
00027 {
00028     DS_Session    *ds = NULL;
00029     DS_Indication *di = NULL;
00030     DS_ErrorType   t;
00031     DS_ErrorValue  v;
00032     DS_DN         *dn = NULL;
00033     DS_Attr       *objectclass = NULL;
00034     DS_Attr       *cn = NULL;
00035     DS_Attr       *sn = NULL;
00036     DS_Attr       *mail = NULL;
00037     DS_Entry      *entry = NULL;
00038     DS_Status      status;
00039     int            rc = 1;
00040 
00041     status = DS_Initialize( );
00042     if ( status != DS_E_NOERROR ) {
00043         fprintf( stderr, "Initialization failed\n" );
00044         goto cleanup;
00045     }
00046 
00047     status = DS_String2DN( "cn=DSA Manager,cn=dsa,dc=example,dc=com", &dn );
00048     if ( status != DS_E_NOERROR ) {
00049         fprintf( stderr, "Bad Manager DN\n" );
00050         goto cleanup;
00051     }
00052     
00053     status = DS_BindSync_Simple( "Internet=localhost+19999",
00054                                  dn, "secret",
00055                                  &ds );
00056     DS_DN_Delete( dn );
00057     dn = NULL;
00058 
00059     if ( status != DS_E_NOERROR ) {
00060         fprintf( stderr, "Bind failed\n" );
00061         goto cleanup;
00062     }
00063 
00064     /* Build an entry to add */
00065     status = DS_Entry_New( &entry );
00066     if ( status != DS_E_NOERROR ) {
00067         fprintf( stderr, "Cannot create new entry\n" );
00068         goto cleanup;
00069     }
00070 
00071     status = DS_String2DN( "cn=John Smith,ou=People,dc=example,dc=com", &dn );
00072     if ( status != DS_E_NOERROR ) {
00073         fprintf( stderr, "Bad New DN\n" );
00074         goto cleanup;
00075     }
00076     
00077     status = DS_Entry_SetDN( entry, dn );
00078     if ( status != DS_E_NOERROR ) {
00079         fprintf( stderr, "Cannot set new DN\n" );
00080         goto cleanup;
00081     }
00082 
00083     status = DS_Attr_New( "objectClass", &objectclass );
00084     if ( status != DS_E_NOERROR ) {
00085         fprintf( stderr, "Cannot create new objectclass\n" );
00086         goto cleanup;
00087     }
00088 
00089     status = DS_Attr_AddStringValue( objectclass, S("top") );
00090     if ( status != DS_E_NOERROR) {
00091         fprintf( stderr, "Cannot add top\n" );
00092         goto cleanup;
00093     }
00094     status = DS_Attr_AddStringValue( objectclass, S("person") );
00095     if ( status != DS_E_NOERROR) {
00096         fprintf( stderr, "Cannot add person\n" );
00097         goto cleanup;
00098     }
00099     status = DS_Attr_AddStringValue( objectclass, S("organizationalPerson") );
00100     if ( status != DS_E_NOERROR) {
00101         fprintf( stderr, "Cannot add organizationalPerson\n" );
00102         goto cleanup;
00103     }
00104     status = DS_Attr_AddStringValue( objectclass, S("inetOrgPerson") );
00105     if ( status != DS_E_NOERROR) {
00106         fprintf( stderr, "Cannot add inetOrgPerson\n" );
00107         goto cleanup;
00108     }
00109     status = DS_Attr_New( "cn", &cn );
00110     if ( status != DS_E_NOERROR) {
00111         fprintf( stderr, "Cannot create new cn\n" );
00112         goto cleanup;
00113     }
00114     status = DS_Attr_AddStringValue( cn, S("John Smith") );
00115     if ( status != DS_E_NOERROR) {
00116         fprintf( stderr, "Cannot add John Smith\n" );
00117         goto cleanup;
00118     }
00119     status = DS_Attr_New( "sn", &sn );
00120     if ( status != DS_E_NOERROR) {
00121         fprintf( stderr, "Cannot create new sn\n" );
00122         goto cleanup;
00123     }
00124     status = DS_Attr_AddStringValue( sn, S("Smith") );
00125     if ( status != DS_E_NOERROR) {
00126         fprintf( stderr, "Cannot add Smith\n" );
00127         goto cleanup;
00128     }
00129     status = DS_Attr_New( "mail", &mail );
00130     if ( status != DS_E_NOERROR) {
00131         fprintf( stderr, "Cannot create new mail\n" );
00132         goto cleanup;
00133     }
00134     status = DS_Attr_AddStringValue( mail, S("john.smith@example.com") );
00135     if ( status != DS_E_NOERROR) {
00136         fprintf( stderr, "Cannot add john.smith@example.com\n" );
00137         goto cleanup;
00138     }
00139     status = DS_Entry_AddValues( entry, objectclass );
00140     if ( status != DS_E_NOERROR ) {
00141         fprintf( stderr, "Cannot add objectclass attribute\n" );
00142         goto cleanup;
00143     }
00144     status = DS_Entry_AddValues( entry, cn );
00145     if ( status != DS_E_NOERROR ) {
00146         fprintf( stderr, "Cannot add cn attribute\n" );
00147         goto cleanup;
00148     }
00149     status = DS_Entry_AddValues( entry, sn );
00150     if ( status != DS_E_NOERROR ) {
00151         fprintf( stderr, "Cannot add sn attribute\n" );
00152         goto cleanup;
00153     }
00154     status = DS_Entry_AddValues( entry, mail );
00155     if ( status != DS_E_NOERROR ) {
00156         fprintf( stderr, "Cannot add mail attribute\n" );
00157         goto cleanup;
00158     }
00159     
00160     status = DS_AddSync( ds, entry, NULL, &di );
00161     if ( di != NULL &&
00162          DS_Indication_GetErrorCodes( di, &t, &v ) != DS_E_NOERROR )
00163         fprintf( stderr, "Add returns error type %d and error value %d\n",
00164                  t, v );
00165     
00166     if ( status != DS_E_NOERROR ) {
00167         fprintf( stderr, "Add operation failed\n" );
00168         goto cleanup;
00169     }
00170 
00171     rc = 0;
00172     
00173 cleanup:
00174     if ( di != NULL ) DS_Indication_Delete( di );
00175     if ( entry != NULL ) DS_Entry_Delete( entry );
00176     if ( mail != NULL ) DS_Attr_Delete( mail );
00177     if ( sn != NULL ) DS_Attr_Delete( sn );
00178     if ( cn != NULL ) DS_Attr_Delete( cn );
00179     if ( objectclass != NULL ) DS_Attr_Delete( objectclass );
00180     if ( ds != NULL ) DS_UnbindSync( &ds );
00181 
00182     return rc;
00183 }
Copyright © 2008 Isode privacy   feedback Subscribe to our rss newsfeed