//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:         VerifyPassword.java
* $description:  VerifyPassword.java sample verifies that a password is
*                correct for the given entry.
******************************************************************************/
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.NoSuchAttributeException;
import javax.naming.directory.SearchControls;

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

        String hostURL  = args[0];
        String loginDN  = args[1];
        String password = args[2];
        String userDN   = args[3];
        String userPWD  = 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);

            System.out.println();
            System.out.println("    User DN: " + userDN );
            System.out.println("    Verifying " + userDN + "'s passwod...");

           // check user's password

           // do compare in JNDI:

           //     1. search scope: SearchControls.OBJECT_SCOPE

           //     2. return no attributes

           //     3. set filter to be name-value pair

            SearchControls ctls = new SearchControls();
            ctls.setSearchScope( SearchControls.OBJECT_SCOPE );
            ctls.setReturningAttributes( new String[0] );
            NamingEnumeration sre = ctx.search( userDN, "userPassword="
                            + userPWD, ctls );

            if ( sre != null && sre.hasMoreElements())
            System.out.println("    password is correct");
            else
            System.out.println("    password is incorrect");

           // close the context

            ctx.close();
        }
        catch( NoSuchAttributeException nae ) {
            System.err.println("VerifyPassword example failed.");
            nae.printStackTrace();
        }
        catch (NamingException e) {
            System.err.println("VerifyPassword example failed.");
            e.printStackTrace();
        }
        finally {
            System.exit(0);
        }
    }

    public static void usage() {
        System.err.println("\n Usage:   java VerifyPassword <host URL> "
            + "<login dn> <password> <user dn>\n          <user password>\n");
        System.err.println(" Example: java VerifyPassword ldap://Acme.com:389"
            + " \"cn=Admin,o=Acme\" secret"
            + "\n          \"cn=JSmith,ou=sales,o=Acme\" userPWD");
        System.exit(1);
    }
}