//Sample code file: var/ndk/webBuildengine/tmp/viewable_samples/a4ad0b48-dd95-46b6-8289-721e99c8dc76/mgmt_api/ldap/java/PwdAPIExample.java //Warning: This code has been marked up for HTML


import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPJSSESecureSocketFactory;
import com.novell.ldap.LDAPSocketFactory;
import com.novell.security.nmas.mgmt.NMASPwdMgr;
import com.novell.security.nmas.mgmt.NMASPwdException;
import com.novell.security.nmas.mgmt.PwdJLdapTransport;
import com.novell.security.nmas.mgmt.NMASPwdStatus;

import java.security.Security;

public class PwdAPIExample
{
    public static void main( String[] args )
    {
        boolean jldap = true;       // Set to "true" for JLDAP and "false" for JNDI
        int      methodID[]   = {0x00};
        NMASPwdMgr pwdMgr = null;

        if (args.length != 7) {
            System.err.println("Usage:   java PwdAPIExample "
                            + "<host Name> <port number> <login dn> <login password> "
                            + "\n          <object dn> <Password1> <Password2>");
            System.err.println("Example: java TestLoginConfigAPIs Acme.com 389 "
                            + "\"cn=Admin,o=Acme\" secret\n         "
                            + "\"cn=james,o=Acme\" "
                            + "\"secret1\"  \"secret2\"");
            System.exit(1);
        }

        int    ldapVersion = LDAPConnection.LDAP_V3;
        String ldapHost    = args[0];
        int    ldapPort    = Integer.parseInt(args[1]);
        String loginDN     = args[2];
        String password    = args[3];
        String objectDN    = args[4];
        String pwd1        = args[5];
        String pwd2        = args[6];

        if(jldap)
        {
            LDAPConnection ld  = new LDAPConnection();

            try
            {
                ld.connect( ldapHost, ldapPort);           // connect to the server
                ld.bind( ldapVersion, loginDN, password ); // bind to the server
                System.out.println( "bind() succeeded");
            }
            catch( LDAPException e )
            {
                System.out.println( "Error: " + e.toString() );
            }

            pwdMgr = new NMASPwdMgr(new PwdJLdapTransport(ld));
        }
        else
        {
            pwdMgr = new NMASPwdMgr(MyLdapCtx.getLdapCtx(ldapHost, loginDN, password, false));
        }


        try {
           // =============> Set the password <===============
            pwdMgr.setPwd("",objectDN, pwd1);
            System.out.println("setPwd() succeeded: ");

           // Get the password
            String returnData = pwdMgr.getPwd("",objectDN);
            if(returnData != null)
            {
                System.out.println("getPwd() returned: " + returnData);
            }

           // =============> Change the password <================
            pwdMgr.changePwd("",objectDN, pwd1, pwd2);
            System.out.println("changePwd() succeeded: ");

           // Verify the password was changed
            returnData = null;
            returnData = pwdMgr.getPwd("",objectDN);
            if(returnData != null)
            {
                System.out.println("getPwd() returned: " + returnData);
            }

           // =============> Delete the password <================
            pwdMgr.deletePwd("",objectDN);
            System.out.println("deletePwd() succeeded: ");

           // Verify the password was deleted
            returnData = pwdMgr.getPwd("",objectDN);
            if(returnData != null)
            {
                System.out.println("getPwd() returned: " + returnData);
            }

           // ==============> Password policy check <=================
            System.out.println("pwdPolicyCheck() returned: " + pwdMgr.pwdPolicyCheck("", objectDN, pwd1));
            System.out.println("pwdPolicyCheck() (Existing) returned: " + pwdMgr.pwdPolicyCheck("", objectDN, null));

           // ==============> Get password policy DN <==================
            System.out.println("getPwdPolicyDN() returned: " + pwdMgr.getPwdPolicyDN("", objectDN));

           // ==============> Get Password Status <=================
            NMASPwdStatus nmasPwdStatus = pwdMgr.getPwdStatus("", objectDN);
            System.out.println("getPwdStatus =>");
            System.out.println("\t==> Universal Password <==");
            System.out.println("\tIs UPwd Enabled:  " + nmasPwdStatus.isSpmUpwdEnabled());
            System.out.println("\tIs the UPwd history full:  " + nmasPwdStatus.isSpmUpwdHistoryFull());
            System.out.println("\tDoes UPwd match NDSPwd:  " + nmasPwdStatus.isSpmUpwdMatchesNDS());
            System.out.println("\tDoes UPwd match SimplePwd:  " + nmasPwdStatus.isSpmUpwdMatchesSPWD());
            System.out.println("\tIs UPwd older than NDSPwd:  " + nmasPwdStatus.isSpmUpwdOlderThanNDS());
            System.out.println("\t==> Simple Password <==");
            System.out.println("\tIs Simple Password Set:  " + nmasPwdStatus.isSpmSpwdSet());
            System.out.println("\tIs Simple Password Clear Text:  " + nmasPwdStatus.isSpmSpwdClearText());
            System.out.println("\tDoes Simple Password match NDSPwd:  " + nmasPwdStatus.isSpmSpwdMatchesNDS());
        }
        catch( NMASPwdException pwde )
        {
            System.out.println( "Error: " + pwde.toString() + "NMAS Return Code (" + pwde.getNmasRetCode() + ")");
        }
    }
}