> developer > dnu > courses > beginning eDirectory page 25
eDirectory for the Beginner
February 2002
DeveloperNet University Course
Reader Rating    from ratings rate this article
View an eBook Version of this course - LARGE FILE! Send this page to a friend

eDirectory Development

With the Novell Developer Kit, you can access NDS on popular servers any way you choose today. For example, LDAP Services for NDS supports the latest LDAP v.3 specifications. Whether you're working with C/C++, Java, ActiveX controls, JavaBeans components, Oracle databases, scripting interfaces such as JavaScript, NetBasic and Perl, ODBC Drivers or client-based programming tools (Visual Basic, Java Studio, Java Cafe, etc.), NDS is readily accessible on NetWare, Windows NT, Windows 2000, Linux and Sun Solaris platforms. In addition, you can now build kernel-level applications for NetWare using your favorite language and compiler with the new NetWare DLL Loader.

http://developer.novell.com/ndk

C programming

(http://developer.novell.com/ndk/cldap.htm)

Programming libraries for Solaris and Linux also available

Dependencies:

For full functionality, the LDAP Libraries for C kit is dependent upon the following:

  • LDAP Extensions. The LDAP extensions for partition and replica management, getting effective rights, and refreshing the LDAP server require NDS eDirectory 8.5.

  • LDAP Controls. The LDAP controls for server-side sorting and virtual list view require NDS 8 or higher.

examples

Java Programming

http://developer.novell.com/ndk/jldap.htm

Programming libraries for Unix and Linux also available

Dependencies:

The LDAP Class Libraries for Java kit is dependent upon the following:

  • Novell SSL for Java or a Sun compliant implementation of JSSE to create SSL connections

  • NDS eDirectory 8.5 to use the extensions for naming context and replica management

  • JVM 1.1.7 or higher

  • JDK 1.1.7 or higher. Creating a Keystore file for SSL connections requires JDK 1.2 or higher (recommend 1.3)

examples:

C: Add a User

Note:
You must have admin rights to do this and the user can not already exist.


/*************************************************************************

Copyright 1999, 2000 Novell, Inc. All Rights Reserved.

With respect to this file, Novell hereby grants to Developer a royalty-free,
non-exclusive license to include this sample code and derivative binaries in
its product. Novell grants to Developer worldwide distribution rights to market,
distribute or sell this sample code file and derivative binaries as a component
of Developer's product(s). Novell shall have no obligations to Developer or
Developer's customers with respect to this code.

DISCLAIMER:

Novell disclaims and excludes any and all express, implied, and statutory
warranties, including, without limitation, warranties of good title, warranties
against infringement, and the implied warranties of merchantability and fitness
for a particular purpose. Novell does not warrant that the software will satisfy
customer's requirements or that the licensed works are without defect or error or
that the operation of the software will be uninterrupted. Novell makes no
warranties respecting any technical services or support tools provided under the
agreement, and disclaims all other warranties, including the implied warranties
of merchantability and fitness for a particular purpose.

***************************************************************************
addentry.c
***************************************************************************
Description: The addentry.c sample adds an entry from the commandline with its
attributes to a specified container. This version dynamically allocates the
memory used for input data.

For the inetOrgPerson class which is usually mapped to the User class, NDS
requires values for cn, surname, and objectClass attributes.
***************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ldap.h>

static void mods_free( LDAPMod **mods, int freemods );
static void memvfree( void **vec );

static char usage[] =
"\n Usage: addentry1 <host name> <port number> <login dn> <password>"
"\n\t <container name> \n"
"\n Example: addentry1 Acme.com 389 cn=admin,o=Acme secret"
" ou=Sales,o=Acme\n"
" entryName entryFirstName entryLastName entryPhone entryEmail";

int main( int argc, char **argv)
{

int i, version, ldapPort, rc, modNumber = 6;
char *ldapHost;
char *loginDN;
char *password;
char *containerName;
char *dn;
LDAP *ld;
LDAPMod **mods;

char *entryName;
char *entryFirst;
char *entryLast;
char *entryPhone;
char *entryMail;
char tempStr[25];

char **objectclassValues = NULL;
char **commonnameValues = NULL;
char **givennameValues = NULL;
char **surnameValues = NULL;
char **phonenumberValues = NULL;
char **mailValues = NULL;

if (argc != 11)
{

printf ("%s", usage);
return (1);

}

ldapHost = argv[1];
ldapPort = atoi(argv[2]);
loginDN = argv[3];
password = argv[4];
containerName = argv[5];

entryName = argv[6];
entryFirst = argv[7];
entryLast = argv[8];
entryPhone = argv[9];
entryMail = argv[10];


/* Set LDAP version to 3 */
version = LDAP_VERSION3;
ldap_set_option ( NULL, LDAP_OPT_PROTOCOL_VERSION, &version);

/* Initialize the LDAP session */
if (( ld = ldap_init( ldapHost, ldapPort )) == NULL)
{

printf ( "\n\tLDAP session initialization failed\n");
return ( 1 );

}
printf ( "\n\tLDAP session initialized\n");

/* Bind to the server */
rc = ldap_simple_bind_s( ld, loginDN, password );
if (rc != LDAP_SUCCESS )
{

printf ("ldap_simple_bind_s: %s\n", ldap_err2string( rc ));
ldap_unbind_s ( ld );
return( 1 );

}
printf("\n\tBind successful\n");

/*
* To add an entry to the directory:
* 1. Create the array of LDAPMod structures representing the
* attributes of the newentry
* 2. Specify the dn of the entry to create
* 3. Call ldap_add_ext_s to add the entry to the directory
*/

mods = ( LDAPMod ** ) malloc(( modNumber + 1 ) * sizeof( LDAPMod * ));

if ( mods == NULL )
{

printf ("No memory for array of mods\n" );
ldap_unbind_s ( ld );
return ( 1 );

}

for ( i = 0; i < modNumber; i++ )
{

if (( mods[ i ] = ( LDAPMod * ) malloc( sizeof( LDAPMod ))) == NULL )
{

printf ("No memory for mods element\n" );
ldap_unbind_s ( ld );
return ( 1 );

}

}

/*
* This example shows complete dynamic allocation of the LDAPMod
* structures, as may be the case when the application is receiving
* entry data from an external source.
*
* Note: data allocated by the application must be freed
* by application functions, not LDAP library functions.
*
* Note: data allocated by the LDAP library must be freed
* by LDAP library functions.
*/
objectclassValues = (char**)malloc(sizeof(char*)*(1+1));
commonnameValues = (char**)malloc(sizeof(char*)*(1+1));
givennameValues = (char**)malloc(sizeof(char*)*(2+1));

surnameValues = (char**)malloc(sizeof(char*)*(1+1));
phonenumberValues = (char**)malloc(sizeof(char*)*(1+1));
mailValues = (char**)malloc(sizeof(char*)*(1+1));

objectclassValues[0] = strdup("inetOrgPerson");
objectclassValues[1] = NULL;
commonnameValues[0] = strdup(entryName);
commonnameValues[1] = NULL;
givennameValues[0] = strdup(entryFirst);
givennameValues[1] = strdup("Fluffy");
givennameValues[2] = NULL;
surnameValues[0] = strdup(entryLast);
surnameValues[1] = NULL;
phonenumberValues[0] = strdup(entryPhone);
phonenumberValues[1] = NULL;
mailValues[0] = strdup(entryMail);
mailValues[1] = NULL;

mods[0]->mod_op = LDAP_MOD_ADD;
mods[0]->mod_type = strdup("objectclass");
mods[0]->mod_values = objectclassValues;
mods[1]->mod_op = LDAP_MOD_ADD;
mods[1]->mod_type = strdup("cn");
mods[1]->mod_values = commonnameValues;
mods[2]->mod_op = LDAP_MOD_ADD;
mods[2]->mod_type = strdup("givenname");
mods[2]->mod_values = givennameValues;
mods[3]->mod_op = LDAP_MOD_ADD;
mods[3]->mod_type = strdup("sn");
mods[3]->mod_values = surnameValues;
mods[4]->mod_op = LDAP_MOD_ADD;
mods[4]->mod_type = strdup("telephonenumber");
mods[4]->mod_values = phonenumberValues;
mods[5]->mod_op = LDAP_MOD_ADD;
mods[5]->mod_type = strdup("mail");
mods[5]->mod_values = mailValues;
mods[6] = NULL;

strcpy (tempStr, "cn=");
strcat (tempStr, entryFirst);
strcat (tempStr, ",");

dn=(char*)malloc(strlen(tempStr) + strlen(containerName));
strcpy ( dn, tempStr);
strcat ( dn, containerName );


/*
* Now add the object
*/
rc = ldap_add_ext_s ( ld, /* LDAP session handle */

dn, /* dn of the object to be added */
mods, /* array of add options */
NULL, /* server controls */
NULL ); /* client controls */


if ( rc != LDAP_SUCCESS )
{

printf ("ldap_add_ext_s: %s\n", ldap_err2string( rc ));
mods_free ( mods, 1 );
ldap_unbind_s ( ld );
return (1);

}
printf("\n\tEntry %s added successfully.\n", dn );

mods_free ( mods, 1 );
ldap_unbind_s ( ld );

return (0);

}

/*
* free a null-terminated array of pointers to mod structures. the
* structures are freed, not the array itself, unless the freemods
* flag is set.
*/

static void
mods_free( LDAPMod **mods, int freemods )
{

int i;

if ( mods == NULL )

return;


for ( i = 0; mods[i] != NULL; i++ )
{

/*
* Free is the same whether bvalues or values,
* because all buffers were alloced by the app, not by sdk.
*/
if ( mods[i]->mod_values != NULL )

memvfree( (void**) (mods[i]->mod_values) );


if ( mods[i]->mod_type != NULL )

free( mods[i]->mod_type );


free( (char *) mods[i] );

}

if ( freemods )

free( (char *) mods );


return;

}

/*
* free a null-terminated array of mod values
*/
static void
memvfree( void **vec )
{

int i;

if ( vec == NULL )
return;

for ( i = 0; vec[i] != NULL; i++ )
free( vec[i] );

free( vec);

return;

}

C: Delete a User

Note:
You must have admin rights to do this and the user must exist. DO NOT delete the Admin user!


/*************************************************************************

Copyright 1999, 2000 Novell, Inc. All Rights Reserved.

With respect to this file, Novell hereby grants to Developer a royalty-free,
non-exclusive license to include this sample code and derivative binaries in
its product. Novell grants to Developer worldwide distribution rights to market,
distribute or sell this sample code file and derivative binaries as a component
of Developer's product(s). Novell shall have no obligations to Developer or
Developer's customers with respect to this code.

DISCLAIMER:

Novell disclaims and excludes any and all express, implied, and statutory
warranties, including, without limitation, warranties of good title, warranties
against infringement, and the implied warranties of merchantability and fitness
for a particular purpose. Novell does not warrant that the software will satisfy
customer's requirements or that the licensed works are without defect or error or
that the operation of the software will be uninterrupted. Novell makes no
warranties respecting any technical services or support tools provided under the
agreement, and disclaims all other warranties, including the implied warranties
of merchantability and fitness for a particular purpose.

***************************************************************************
delentry.c
***************************************************************************
Description: The delentry.c sample deletes the entry with the
distinguished name provided by the user.
***************************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <ldap.h>

static char usage[] =
"\n Usage: delentry <host name> <port number> <login dn> <password>"
"\n\t <deleteDN>"
"\n Example: delentry acme.com 389 cn=admin,o=acme secret"
"\n\t cn=james,ou=sales,o=acme\n";

int main( int argc, char **argv)
{


int version, ldapPort, rc;
char *ldapHost, *loginDN, *password, *deleteDN;
LDAP *ld;


if (argc != 6)
{
printf("%s", usage);
return(1);
}

ldapHost = argv[1];
ldapPort = atoi(argv[2]);
loginDN = argv[3];
password = argv[4];
deleteDN = argv[5];

/* Set LDAP version to 3 */
version = LDAP_VERSION3;
ldap_set_option( NULL, LDAP_OPT_PROTOCOL_VERSION, &version);

/* Initialize the LDAP session */
if (( ld = ldap_init( ldapHost, ldapPort )) == NULL)
{

printf ( "\n\tLDAP session initialization failed\n");
return( 1 );

}
printf ( "\n\tLDAP session initialized\n");

/* Bind to the server */
rc = ldap_simple_bind_s( ld, loginDN, password );
if (rc != LDAP_SUCCESS )
{

printf("ldap_simple_bind_s: %s\n", ldap_err2string( rc ));
ldap_unbind_s ( ld );
return( 1 );

}
printf("\n\tBind successful\n");


/* Perform the delete operation. */
rc = ldap_delete_ext_s( ld, /* LDAP session handle */

deleteDN, /* dn of the object to delete */
NULL, /* server controls */
NULL ); /* client controls */


if ( rc != LDAP_SUCCESS )
{

printf("\n\tldap_delete_ext_s: %s\n", ldap_err2string( rc ));
ldap_unbind_s( ld );
return(1);

}
printf("\n\tEntry %s deleted successfully\n", deleteDN );

ldap_unbind_s( ld );

return( 0 );

}

Java: Add a User

Note:
You must have admin rights to do this and the user can not already exist.


/*******************************************************************************
* $Id: AddEntry.java,v 1.6 2000/10/19 15:44:49 fzhao Exp $
* Copyright (c) 2000 Novell, Inc. All Rights Reserved.
*
* THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
* TREATIES. USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE LICENSE
* AGREEMENT ACCOMPANYING THE SOFTWARE DEVELOPMENT KIT (SDK) THAT CONTAINS
* THIS WORK. PURSUANT TO THE SDK LICENSE AGREEMENT, NOVELL HEREBY GRANTS TO
* DEVELOPER A ROYALTY-FREE, NON-EXCLUSIVE LICENSE TO INCLUDE NOVELL'S SAMPLE
* CODE IN ITS PRODUCT. NOVELL GRANTS DEVELOPER WORLDWIDE DISTRIBUTION RIGHTS
* TO MARKET, DISTRIBUTE, OR SELL NOVELL'S SAMPLE CODE AS A COMPONENT OF
* DEVELOPER'S PRODUCTS. NOVELL SHALL HAVE NO OBLIGATIONS TO DEVELOPER OR
* DEVELOPER'S CUSTOMERS WITH RESPECT TO THIS CODE.
*
* $name: AddEntry.java
* $description: AddEntry adds an entry to the directory. First, it creates
* each attribute of the entry, adds it to the attribute
* set, and then uses the DN and the newly created attribute
* set to create an LDAPEntry entry, newEntry. Finally it calls
* the LDAPConnection add method to add the entry into the
* directory.
******************************************************************************/

import com.novell.ldap.*;

public class AddEntry {

public static void main( String[] args ) {
int ldapPort = LDAPConnection.DEFAULT_PORT;
int ldapVersion = LDAPConnection.LDAP_V3;
LDAPConnection lc = new LDAPConnection();
LDAPAttribute attribute = null;
LDAPAttributeSet attributeSet = new LDAPAttributeSet();


if (args.length != 9) {

System.out.println("Usage: java AddEntry <host name> <login dn> "
+ "<password> <container> <new dn> <dn first name> "
+ "<dn lastname> <dn phone> <dn email>");
System.out.println(

"Example: java AddEntry Acme.com cn=admin,o=Acme secret "
+ "ou=Sales,o=Acme cn=bjones bob "
+ "jones 1.666.6666 bjones@666.com");
System.exit(0);

}



String ldapHost = args [0];
String loginDN = args [1];
String password = args [2];
String containerName = args [3];
String entryName = args [4];
String entryFirst = args [5];
String entryLast = args [6];
String entryPhone = args [7];
String entryMail = args [8];


/* To Add an entry to the directory,
-- Create the attributes of the entry and add them to an attribute set
-- Specify the DN of the entry to be created
-- Create an LDAPEntry object with the DN and the attribute set
-- Call the LDAPConnection add method to add it to the directory
*/
String objectclass_values[] = { "inetOrgPerson" };
attribute = new LDAPAttribute( "objectclass", objectclass_values );
attributeSet.add( attribute );
attribute = new LDAPAttribute( "givenname", entryFirst);
attributeSet.add( attribute );
attributeSet.add( new LDAPAttribute( "sn", entryLast ) );
attributeSet.add( new LDAPAttribute( "telephonenumber", entryPhone) );
attributeSet.add( new LDAPAttribute( "mail", entryMail ) );
String dn = entryName + "," + containerName;
LDAPEntry newEntry = new LDAPEntry( dn, attributeSet );


try {

// connect to the server
lc.connect( ldapHost, ldapPort );
// authenticate to the server
lc.bind( ldapVersion, loginDN, password );


lc.add( newEntry );
System.out.println( "\nAdded object: " + dn + " successfully." );

}
catch( LDAPException e ) {

System.out.println( "Error: " + e.toString() );

}


try {

lc.disconnect();

}
catch( LDAPException e ) {

System.out.println( "Error: " + e.toString() );

}


System.exit(0);
}

}

Java: Delete a User

Note:
You must have admin rights to do this and the user must exist. DO NOT delete the Admin user!


/*******************************************************************************
* $Id: DeleteEntry.java,v 1.5 2000/10/19 15:43:27 fzhao Exp $
* Copyright (c) 2000 Novell, Inc. All Rights Reserved.
*
* THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
* TREATIES. USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE LICENSE
* AGREEMENT ACCOMPANYING THE SOFTWARE DEVELOPMENT KIT (SDK) THAT CONTAINS
* THIS WORK. PURSUANT TO THE SDK LICENSE AGREEMENT, NOVELL HEREBY GRANTS TO
* DEVELOPER A ROYALTY-FREE, NON-EXCLUSIVE LICENSE TO INCLUDE NOVELL'S SAMPLE
* CODE IN ITS PRODUCT. NOVELL GRANTS DEVELOPER WORLDWIDE DISTRIBUTION RIGHTS
* TO MARKET, DISTRIBUTE, OR SELL NOVELL'S SAMPLE CODE AS A COMPONENT OF
* DEVELOPER'S PRODUCTS. NOVELL SHALL HAVE NO OBLIGATIONS TO DEVELOPER OR
* DEVELOPER'S CUSTOMERS WITH RESPECT TO THIS CODE.
*
* $name: DeleteEntry.java
* $description: The DeleteEntry example deletes an entry from the directory.
******************************************************************************/

import com.novell.ldap.*;

public class DeleteEntry {

public static void main( String[] args ) {

int ldapPort = LDAPConnection.DEFAULT_PORT;
int ldapVersion = LDAPConnection.LDAP_V3;
LDAPConnection lc = new LDAPConnection();

if (args.length != 4) {
System.out.println("Usage: java DeleteEntry <host name> <login dn> "
+ "<password> <delete dn>");
System.out.println("Example: java DeleteEntry Acme.com cn=Admin,"
+ "o=Acme secret cn=JSmith,ou=Sales,o=Acme");
System.exit(0);

}
String ldapHost = args[0];
String loginDN = args[1];
String password = args[2];
String deleteDN = args[3];

try
{

// connect to the server
lc.connect( ldapHost, ldapPort );
// authenticate to the server
lc.bind( ldapVersion, loginDN, password );


// Deletes the entry from the directory
lc.delete( deleteDN );
System.out.println( "Entry: " + deleteDN + " was deleted." );

}
catch( LDAPException e ) {

if ( e.getLDAPResultCode() == LDAPException.NO_SUCH_OBJECT )

System.out.println( "Error: No such object" );

else if ( e.getLDAPResultCode() ==

LDAPException.INSUFFICIENT_ACCESS_RIGHTS )
System.out.println( "Error: Insufficient rights" );

else

System.out.println( "Error: " + e.toString() );

}


try {

lc.disconnect();

}
catch( LDAPException e ) {

System.out.println( "Error: " + e.toString() );

}


System.exit(0);
}

}

Previous Contents Next
download sample file