//Warning: This code has been marked up for HTML

/**************************************************************************
*  Novell Software Developer Kit
*
*  Copyright (C) 2002-2003 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 DEVELOPER 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:         ModifyPassword.java
* $description:  ModifyPassword.java sample shows how to modify your own
*                password, giving both the old and new password.
*                Unless the caller has admin privilegs, Novell eDirectory
*                requires both the old and new passwords in order to change
*                a password.
******************************************************************************/
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.ModificationItem;
import javax.naming.directory.NoSuchAttributeException;

public class ModifyPassword
{
    public static void main( String[] args )
    {
        if (args.length != 4) {
            usage();
        }

        String hostURL     = args[0];
        String loginDN     = args[1];
        String password    = args[2];
        String newPassword = args[3];

        try {
            /* Setup environment properties */
            Hashtable env = new Hashtable(5, 0.75f);
            env.put(Context.INITIAL_CONTEXT_FACTORY,
                         "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.PROVIDER_URL, hostURL);
            env.put( Context.SECURITY_PRINCIPAL, loginDN );
            env.put( Context.SECURITY_CREDENTIALS, password );

           // create the initial directory context

            DirContext ctx = new InitialDirContext(env);

           // Specify the changes to make

            ModificationItem[] mods = new ModificationItem[2];

           // Replace the "userPassword" attribute with a new value

            mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE,
                     new BasicAttribute("userPassword", password));

           // Replace the "userPassword" attribute with a new value

            mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
                     new BasicAttribute("userPassword", newPassword));

           // Perform the requested modifications on the named object

            ctx.modifyAttributes(loginDN, mods);

            System.out.println("\n\tModifyPassword example succeeded.\n");

        }
        catch( NoSuchAttributeException nae ) {
            System.err.println("\n\tModifyPassword example failed.\n");
            nae.printStackTrace();
        }
        catch (NamingException e) {
            System.err.println("\n\tModifyPassword example failed.\n");
            e.printStackTrace();
        }
        finally {    
            System.exit(0);
        }
    }

    public static void usage() {
        System.out.println("\n Usage:   java ModifyPassword <host URL>"
                        + " <login dn> <password>\n          newPassword\n");
        System.out.println(" Example: java ModifyPassword ldap://Acme.com:389"
            +  " \"cn=James,o=Acme\" secret\n          newPassword\n");
        System.exit(1);
    }
}