| CONTENTS | PREV | NEXT |
Appendix B:
|
B.6.1 Rename Using LDAP C API |
B.6.2 Rename Using JNDI |
/*
* Copyright (c) 1996. Netscape Communications Corporation. All
* rights reserved.
*
* Modify the RDN (relative distinguished name) of an entry. In this
* example, we change the dn "cn=Jacques Smith, o=Ace Industry, c=US"
* to "cn=Jacques M Smith, o=Ace Industry, c=US".
*
* Since it is an error to either (1) attempt to modrdn an entry which
* does not exist, or (2) modrdn an entry where the destination name
* already exists, we take some steps, for this example, to make sure
* we will succeed. We (1) add "cn=Jacques Smith" (if the entry exists,
* we just ignore the error, and (2) delete "cn=Jacques M Smith" (if the
* entry doesn't exist, we ignore the error).
*
* We pass 0 for the "deleteoldrdn" argument to ldap_modrdn2_s(). This
* means that after we change the RDN, the server will put the value
* "Jacques Smith" into the cn attribute of the new entry, in addition to
* "Jacques M Smith".
*/
#include "examples.h"
#define NMODS 4
unsigned long global_counter = 0;
static void free_mods( LDAPMod **mods );
int
main( int argc, char **argv )
{
LDAP *ld;
char *dn, *ndn, *nrdn;
int i;
int rc;
LDAPMod **mods;
/* Values we will use in creating the entry */
char *objectclass_values[] = { "top", "person", "organizationalPerson",
"inetOrgPerson", NULL };
char *cn_values[] = { "Jacques Smith", NULL };
char *sn_values[] = { "Smith", NULL };
char *givenname_values[] = { "Jacques", NULL };
/* Specify the DN we are adding */
dn = "cn=Jacques Smith, o=Ace Industry, c=US";
/* the destination DN */
ndn = "cn=Jacques M Smith, o=Ace Industry, c=US";
/* the new RDN */
nrdn = "cn=Jacques M Smith";
/* get a handle to an LDAP connection */
if ( (ld = ldap_init( MY_HOST, MY_PORT )) == NULL ) {
perror( "ldap_init" );
return( 1 );
}
/* authenticate to the directory as the Directory Manager */
if ( ldap_simple_bind_s( ld, MGR_DN, MGR_PW ) != LDAP_SUCCESS ) {
ldap_perror( ld, "ldap_simple_bind_s" );
return( 1 );
}
if (( mods = ( LDAPMod ** ) malloc(( NMODS + 1 ) * sizeof( LDAPMod *)))
== NULL ) {
fprintf( stderr, "Cannot allocate memory for mods array\n" );
return( 1 );
}
/* Construct the array of values to add */
for ( i = 0; i < NMODS; i++ ) {
if (( mods[ i ] = ( LDAPMod * ) malloc( sizeof( LDAPMod ))) == NULL ) {
fprintf( stderr, "Cannot allocate memory for mods element\n" );
return( 1 );
}
}
mods[ 0 ]->mod_op = 0;
mods[ 0 ]->mod_type = "objectclass";
mods[ 0 ]->mod_values = objectclass_values;
mods[ 1 ]->mod_op = 0;
mods[ 1 ]->mod_type = "cn";
mods[ 1 ]->mod_values = cn_values;
mods[ 2 ]->mod_op = 0;
mods[ 2 ]->mod_type = "sn";
mods[ 2 ]->mod_values = sn_values;
mods[ 3 ]->mod_op = 0;
mods[ 3 ]->mod_type = "givenname";
mods[ 3 ]->mod_values = givenname_values;
mods[ 4 ] = NULL;
/* Add the entry */
if (( rc = ldap_add_s( ld, dn, mods )) != LDAP_SUCCESS ) {
/* If entry exists already, fine. Ignore this error. */
if ( rc == LDAP_ALREADY_EXISTS ) {
printf( "Entry \"%s is already in the directory.\n", dn );
} else {
ldap_perror( ld, "ldap_add_s" );
free_mods( mods );
return( 1 );
}
} else {
printf( "Added entry \"%s\".\n", dn );
}
free_mods( mods );
/* Delete the destination entry, for this example */
if (( rc = ldap_delete_s( ld, ndn )) != LDAP_SUCCESS ) {
/* If entry does not exist, fine. Ignore this error. */
if ( rc == LDAP_NO_SUCH_OBJECT ) {
printf( "Entry \"%s\" is not in the directory. "
"No need to delete.\n", ndn );
} else {
ldap_perror( ld, "ldap_delete_s" );
return( 1 )
}
} else {
printf( "Deleted entry \"%s\".\n", ndn );
}
/* Do the modrdn operation */
if ( ldap_modrdn2_s( ld, dn, nrdn, 0 ) != LDAP_SUCCESS ) {
ldap_perror( ld, "ldap_modrdn2_s" );
return( 1 );
}
printf( "The modrdn operation was successful. Entry\n"
"\"%s\" has been changed to\n"
"\"%s\".\n", dn, ndn );
ldap_unbind( ld );
return 0;
}
/*
* Free a mods array.
*/
static void
free_mods( LDAPMod **mods )
{
int i;
for ( i = 0; i < NMODS; i++ ) {
free( mods[ i ] );
}
free( mods );
}
/*
* @(#)Modrdn.java 1.2 99/07/26
*
* Copyright (c) 1997, 1999, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
import java.util.Hashtable;
import java.util.Date;
import javax.naming.*;
import javax.naming.directory.*;
/*
* Modify the RDN (relative distinguished name) of an entry. In this
* example, we change the dn "cn=Jacques Smith, o=Ace Industry, c=US"
* to "cn=Jacques M Smith, o=Ace Industry, c=US".
*
* Since it is an error to either (1) attempt to modrdn an entry which
* does not exist, or (2) modrdn an entry where the destination name
* already exists, we take some steps, for this example, to make sure
* we'll succeed. We (1) add "cn=Jacques Smith" (if the entry exists,
* we just ignore the error, and (2) delete "cn=Jacques M Smith" (if the
* entry doesn't exist, we ignore the error).
*
* After renaming, we add back the attribute "Jacques Smith" into the cn
* attribute.
*
* [based on modrdn.c of Netscape SDK]
*/
class Modrdn {
public static void main(String[] args) {
/* Values we'll use in creating the entry */
Attribute objClasses = new BasicAttribute("objectclass");
objClasses.add("top");
objClasses.add("person");
objClasses.add("organizationalPerson");
objClasses.add("inetOrgPerson");
Attribute cn = new BasicAttribute("cn", "Jacques Smith");
Attribute sn = new BasicAttribute("sn", "Smith");
Attribute givenNames = new BasicAttribute("givenname", "Jacques");
/* Specify the DN we're adding */
String dn = "cn=Jacques Smith, " + Env.MY_MODBASE;
/* the destination DN */
String ndn = "cn=Jacques M Smith, " + Env.MY_MODBASE;
/* the new RDN */
String nrdn = "cn=Jacques M Smith";
Hashtable env = new Hashtable(5, 0.75f);
/*
* Specify the initial context implementation to use.
* This could also be set by using the -D option to the java program.
* For example,
* java -Djava.naming.factory.initial=com.sun.jndi.ldap.LdapCtxFactory \
* Modrdn
*/
env.put(Context.INITIAL_CONTEXT_FACTORY, Env.INITCTX);
/* Specify host and port to use for directory service */
env.put(Context.PROVIDER_URL, Env.MY_SERVICE);
/* specify authentication information */
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, Env.MGR_DN);
env.put(Context.SECURITY_CREDENTIALS, Env.MGR_PW);
DirContext ctx = null;
try {
/* get a handle to an Initial DirContext */
ctx = new InitialDirContext(env);
Attributes orig = new BasicAttributes();
orig.put(objClasses);
orig.put(cn);
orig.put(sn);
orig.put(givenNames);
/* Add the entry */
ctx.createSubcontext(dn, orig);
System.out.println( "Added entry " + dn + ".");
} catch (NameAlreadyBoundException e) {
/* If entry exists already, fine. Ignore this error. */
System.out.println("Entry " + dn + " already exists, no need to add");
} catch (NamingException e) {
System.err.println("Modrdn: problem adding entry." + e);
System.exit(1);
}
try {
/* Delete the destination entry, for this example */
ctx.destroySubcontext(ndn);
System.out.println( "Deleted entry " + ndn + "." );
} catch (NameNotFoundException e) {
/* If entry does not exist, fine. Ignore this error. */
System.out.println( "Entry " + ndn + " is not in the directory. " +
"No need to delete.");
} catch (NamingException e) {
System.err.println("Modrdn: problem deleting entry." + e);
System.exit(1);
}
/* Do the modrdn operation */
try {
ctx.rename(dn, ndn);
System.out.println("The modrdn operation was successful. Entry " +
dn + " has been changed to " + ndn + ".");
} catch (NamingException e) {
System.err.println("Modify operation failed." + e);
}
}
}