1 23 package org.openharmonise.rm.security.authentication; 24 25 import java.sql.*; 26 27 28 35 public class PasswordMigrationUtil { 36 37 private String hashAlgorithm; 38 private String dbUrl; 39 private String dbUsr; 40 private String dbPassword; 41 42 public PasswordMigrationUtil(String hashAlgorithm, String dbDriver, String dbUsr, 43 String dbPassword, String dbUrl) { 44 45 if (hashAlgorithm.equals("MD5") || hashAlgorithm.equals("SHA-1")) { 46 this.hashAlgorithm = hashAlgorithm; 47 } 48 else { 49 System.err.println("Non supported hash algorithm" + hashAlgorithm); 50 System.exit(1); 51 } 52 53 this.dbUrl = dbUrl; 54 this.dbUsr = dbUsr; 55 this.dbPassword = dbPassword; 56 57 try { 58 Class.forName(dbDriver); 59 } 60 catch (Exception e) { 61 e.printStackTrace(); 62 System.exit(2); 63 } 64 } 65 66 public void execute() { 67 71 try { 72 Connection con = DriverManager.getConnection(dbUrl, dbUsr, dbPassword); 73 Statement getUsers = con.createStatement(); 74 ResultSet usersRS = getUsers.executeQuery("select id, password from users"); 75 while (usersRS.next()) { 76 int id = usersRS.getInt("id"); 77 System.out.println("Processing user with id " + id); 78 79 String curPasswd = usersRS.getString("password"); 80 String salt = getSalt(hashAlgorithm); 82 String hashedPasswd = getPasswordHelper(hashAlgorithm).getNewPassword(curPasswd, salt); 84 85 Statement updateUser = con.createStatement(); 87 updateUser.executeUpdate("update users set salt = '" + salt + "', password = '" + hashedPasswd + "' where id = " + id); 88 updateUser.close(); 89 Statement updatePreviousSalts = con.createStatement(); 91 updatePreviousSalts.executeUpdate("update users_hist set salt = '" + salt + "' where id = " + id); 92 93 updatePreviousSalts.close(); 94 95 } 96 usersRS.close(); 97 getUsers.close(); 98 con.close(); 99 } 100 catch (SQLException e) { 101 e.printStackTrace(); 103 } 104 105 try { 107 Connection con = DriverManager.getConnection(dbUrl, dbUsr, dbPassword); 108 Statement getPreviousUserVersions = con.createStatement(); 109 ResultSet previousUserVersionsRS = getPreviousUserVersions.executeQuery("select object_key, salt, password from users_hist"); 111 while (previousUserVersionsRS.next()) { 112 int object_key = previousUserVersionsRS.getInt("object_key"); 113 String salt = previousUserVersionsRS.getString("salt"); 114 if (salt == null) { 115 continue; } 117 String previousPasswd = previousUserVersionsRS.getString("password"); 118 String hashedPasswd = getPasswordHelper(hashAlgorithm).getNewPassword(previousPasswd, salt); 120 Statement updatePreviousVersion = con.createStatement(); 122 updatePreviousVersion.executeUpdate("update users_hist set password = '" + hashedPasswd + "' where object_key = " + object_key); 123 updatePreviousVersion.close(); 124 } 125 previousUserVersionsRS.close(); 126 127 Statement setPwdEncryption = con.createStatement(); 128 setPwdEncryption.executeUpdate("update oh_prop set prop_value = '" + hashAlgorithm + "' where prop_name = 'PWD_ENCRYPTION'"); 129 setPwdEncryption.close(); 130 131 con.close(); 132 } 133 catch (SQLException sqlE) { 134 sqlE.printStackTrace(); 135 } 136 137 138 139 try { 140 Connection con = DriverManager.getConnection(dbUrl, dbUsr, dbPassword); 141 144 Statement getDefunctUsers = con.createStatement(); 145 ResultSet defunctUsersRS = getDefunctUsers.executeQuery("select distinct id from users_hist where salt is null"); 146 while (defunctUsersRS.next()) { 147 int id = defunctUsersRS.getInt("id"); 149 String salt = getSalt(hashAlgorithm); 150 System.err.println("updating id " + id + " with salt " + salt); 151 Statement updateSaltForDefunctUsers = con.createStatement(); 152 updateSaltForDefunctUsers.executeUpdate("update users_hist set salt = '" + salt + "' where id = " + id); 153 updateSaltForDefunctUsers.close(); 155 } 156 defunctUsersRS.close(); 157 getDefunctUsers.close(); 158 con.close(); 159 } 160 catch (SQLException e) { 161 e.printStackTrace(); 162 } 163 164 } 165 166 private String getSalt(String algorithm) { 167 if (algorithm.equals("MD5")) { 168 return PasswordCryptUtil.getNewSalt(32); 169 } 170 else { 171 return PasswordCryptUtil.getNewSalt(40); 172 } 173 } 174 175 private PasswordHelper getPasswordHelper(String algorithm) { 176 return new CryptPasswordHelper(algorithm); 177 } 178 179 public static void main(String [] args) { 180 PasswordMigrationUtil app = new PasswordMigrationUtil(args[0], args[1], args[2], args[3], args[4]); 181 app.execute(); 182 } 183 } 184 | Popular Tags |