//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:         SetPassword.java
* $description:  The SetPassword.java sample shows how to set the password
*                of an entry by setting the userPassword attribute
*                of the entry.
*
*                In Novell eDirectory, only an admin can set a password
*                without supplying the old password.  Consequently this
*                method works on any Novell LDAP server, but only when the
*                caller has admin privileges.  Users cannot change their
*                own password with this method (see ModifyPassword.java).
******************************************************************************/
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 SetPassword
{
    public static void main( String[] args )
    {
        if (args.length != 5) {
            usage();
        }

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


        try {
            /* Setup environment properties */
            Hashtable env = new Hashtable();
            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[] mod = new ModificationItem[1];

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

            mod[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                     new BasicAttribute("userPassword", newPassword));
           // Perform the requested modifications on the named object

            ctx.modifyAttributes(modifyDN, mod);

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

        }
        catch( NoSuchAttributeException na ) {
            System.err.println( "\n    Entry: " + modifyDN + " has no password");
            System.err.println( "    Error: " + na.toString() );
            System.exit(1);
        }
        catch (NamingException e) {
            System.err.println("\n\tSetPassword example failed.");
            e.printStackTrace();
        }
        finally {
            System.exit(0);
        }
    }

    public static void usage() {
        System.err.println("\n Usage:   java SetPassword <host URL> <login dn>"
            + " <password> <modify obj dn>\n          <new password>");
        System.out.println("\n Example: java SetPassword ldap://Acme.com:389 "
            + "cn=admin,o=acme secret\n          cn=james,o=acme newSecret");
        System.exit(1);
    }
}