src/main/java/dbm/tw/security/ActiveDirectory.java
public void changePassword(String oldPass, String newPass, boolean trustAllCerts, LdapContext context)
                throws java.io.IOException, NamingException {
            String dn = getDistinguishedName();


            //Switch to SSL/TLS
            StartTlsResponse tls = null;
            try{
                tls = (StartTlsResponse) context.extendedOperation(new StartTlsRequest());
            }
            catch(Exception e){
                //"Problem creating object: javax.naming.ServiceUnavailableException: [LDAP: error code 52 - 00000000: LdapErr: DSID-0C090E09, comment: Error initializing SSL/TLS, data 0, v1db0"
                throw new java.io.IOException("Failed to establish SSL connection to the Domain Controller. Is LDAPS enabled?");
            }


            //Exchange certificates
            if (trustAllCerts){
                tls.setHostnameVerifier(DO_NOT_VERIFY);
                SSLSocketFactory sf = null;
                try {
                    SSLContext sc = SSLContext.getInstance("TLS");
                    sc.init(null, TRUST_ALL_CERTS, null);
                    sf = sc.getSocketFactory();
                }
                catch(java.security.NoSuchAlgorithmException e) {}
                catch(java.security.KeyManagementException e) {}
                tls.negotiate(sf);
            }
            else{
                tls.negotiate();
            }


            //Change password
            try {
                //ModificationItem[] modificationItems = new ModificationItem[1];
                //modificationItems[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("unicodePwd", getPassword(newPass)));

                ModificationItem[] modificationItems = new ModificationItem[2];
                modificationItems[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("unicodePwd", getPassword(oldPass)) );
                modificationItems[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("unicodePwd", getPassword(newPass)) );
                context.modifyAttributes(dn, modificationItems);
            }
            catch(javax.naming.directory.InvalidAttributeValueException e){
                String error = e.getMessage().trim();
                if (error.startsWith("[") && error.endsWith("]")){
                    error = error.substring(1, error.length()-1);
                }
                System.err.println(error);
                //e.printStackTrace();
                tls.close();
                throw new NamingException(
                        "New password does not meet Active Directory requirements. " +
                                "Please ensure that the new password meets password complexity, " +
                                "length, minimum password age, and password history requirements."
                );
            }
            catch(NamingException e) {
                tls.close();
                throw e;
            }

            //Close the TLS/SSL session
            tls.close();
        }
/** Used to change the user password. Throws an IOException if the Domain
         *  Controller is not LDAPS enabled.
         *  @param trustAllCerts If true, bypasses all certificate and host name
         *  validation. If false, ensure that the LDAPS certificate has been
         *  imported into a trust store and sourced before calling this method.
         *  Example:
        String keystore = "/usr/java/jdk1.5.0_01/jre/lib/security/cacerts";
        System.setProperty("javax.net.ssl.trustStore",keystore);
         */
Used to change the user password. Throws an IOException if the Domain
Controller is not LDAPS enabled.
@param trustAllCerts If true, bypasses all certificate and host name
validation. If false, ensure that the LDAPS certificate has been
imported into a trust store and sourced before calling this method.
Used to change the user password . Throws an IOException if the Domain Controller is not LDAPS enabled . @param trustAllCerts If true bypasses all certificate and host name validation . If false ensure that the LDAPS certificate has been imported into a trust store and sourced before calling this method .
public void changePassword(String oldPass, String newPass, boolean trustAllCerts, LdapContext context)
                throws java.io.IOException, NamingException {
            String dn = getDistinguishedName();
            StartTlsResponse tls = null;
            try{
                tls = (StartTlsResponse) context.extendedOperation(new StartTlsRequest());
            }
            catch(Exception e){
                throw new java.io.IOException("Failed to establish SSL connection to the Domain Controller. Is LDAPS enabled?");
            }
            if (trustAllCerts){
                tls.setHostnameVerifier(DO_NOT_VERIFY);
                SSLSocketFactory sf = null;
                try {
                    SSLContext sc = SSLContext.getInstance("TLS");
                    sc.init(null, TRUST_ALL_CERTS, null);
                    sf = sc.getSocketFactory();
                }
                catch(java.security.NoSuchAlgorithmException e) {}
                catch(java.security.KeyManagementException e) {}
                tls.negotiate(sf);
            }
            else{
                tls.negotiate();
            }
            try {
                ModificationItem[] modificationItems = new ModificationItem[2];
                modificationItems[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("unicodePwd", getPassword(oldPass)) );
                modificationItems[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("unicodePwd", getPassword(newPass)) );
                context.modifyAttributes(dn, modificationItems);
            }
            catch(javax.naming.directory.InvalidAttributeValueException e){
                String error = e.getMessage().trim();
                if (error.startsWith("[") && error.endsWith("]")){
                    error = error.substring(1, error.length()-1);
                }
                System.err.println(error);
                tls.close();
                throw new NamingException(
                        "New password does not meet Active Directory requirements. " +
                                "Please ensure that the new password meets password complexity, " +
                                "length, minimum password age, and password history requirements."
                );
            }
            catch(NamingException e) {
                tls.close();
                throw e;
            }
            tls.close();
        }
public void changePassword ( String oldPass , String newPass , boolean trustAllCerts , LdapContext context ) throws java . io . IOException , NamingException { String dn = getDistinguishedName ( ) ; StartTlsResponse tls = null ; try { tls = ( StartTlsResponse ) context . extendedOperation ( new StartTlsRequest ( ) ) ; } catch ( Exception e ) { throw new java . io . IOException ( "Failed to establish SSL connection to the Domain Controller. Is LDAPS enabled?" ) ; } if ( trustAllCerts ) { tls . setHostnameVerifier ( DO_NOT_VERIFY ) ; SSLSocketFactory sf = null ; try { SSLContext sc = SSLContext . getInstance ( "TLS" ) ; sc . init ( null , TRUST_ALL_CERTS , null ) ; sf = sc . getSocketFactory ( ) ; } catch ( java . security . NoSuchAlgorithmException e ) { } catch ( java . security . KeyManagementException e ) { } tls . negotiate ( sf ) ; } else { tls . negotiate ( ) ; } try { ModificationItem [ ] modificationItems = new ModificationItem [ 2 ] ; modificationItems [ 0 ] = new ModificationItem ( DirContext . REMOVE_ATTRIBUTE , new BasicAttribute ( "unicodePwd" , getPassword ( oldPass ) ) ) ; modificationItems [ 1 ] = new ModificationItem ( DirContext . ADD_ATTRIBUTE , new BasicAttribute ( "unicodePwd" , getPassword ( newPass ) ) ) ; context . modifyAttributes ( dn , modificationItems ) ; } catch ( javax . naming . directory . InvalidAttributeValueException e ) { String error = e . getMessage ( ) . trim ( ) ; if ( error . startsWith ( "[" ) && error . endsWith ( "]" ) ) { error = error . substring ( 1 , error . length ( ) - 1 ) ; } System . err . println ( error ) ; tls . close ( ) ; throw new NamingException ( "New password does not meet Active Directory requirements. " + "Please ensure that the new password meets password complexity, " + "length, minimum password age, and password history requirements." ) ; } catch ( NamingException e ) { tls . close ( ) ; throw e ; } tls . close ( ) ; }
Used to change the user password.
Used to change the user password .
//Switch to SSL/TLS //"Problem creating object: javax.naming.ServiceUnavailableException: [LDAP: error code 52 - 00000000: LdapErr: DSID-0C090E09, comment: Error initializing SSL/TLS, data 0, v1db0" //Exchange certificates //Change password //ModificationItem[] modificationItems = new ModificationItem[1]; //modificationItems[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("unicodePwd", getPassword(newPass))); //e.printStackTrace(); //Close the TLS/SSL session