1 8 9 package mx4j.tools.remote; 10 11 import java.io.File ; 12 import java.io.FileInputStream ; 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.security.MessageDigest ; 16 import java.security.NoSuchAlgorithmException ; 17 import java.util.Collections ; 18 import java.util.HashMap ; 19 import java.util.HashSet ; 20 import java.util.Map ; 21 import java.util.Properties ; 22 import java.util.Set ; 23 import javax.management.remote.JMXAuthenticator ; 24 import javax.management.remote.JMXPrincipal ; 25 import javax.security.auth.Subject ; 26 27 import mx4j.util.Base64Codec; 28 29 77 public class PasswordAuthenticator implements JMXAuthenticator 78 { 79 private static final String LEFT_DELIMITER = "OBF("; 80 private static final String RIGHT_DELIMITER = "):"; 81 82 88 public static void main(String [] args) throws Exception 89 { 90 if (args.length == 1) 91 { 92 if (!"-help".equals(args[0])) 93 { 94 printPassword("MD5", args[0]); 95 return; 96 } 97 } 98 else if (args.length == 3) 99 { 100 if ("-alg".equals(args[0])) 101 { 102 printPassword(args[1], args[2]); 103 return; 104 } 105 } 106 printUsage(); 107 } 108 109 private static void printPassword(String algorithm, String input) 110 { 111 String password = obfuscatePassword(input, algorithm); 112 System.out.println(password); 113 } 114 115 private static void printUsage() 116 { 117 System.out.println(); 118 System.out.println("Usage: java -cp <lib>/mx4j-tools.jar mx4j.tools.remote.PasswordAuthenticator <options> <password>"); 119 System.out.println("Where <options> is one of the following:"); 120 System.out.println(" -help Prints this message"); 121 System.out.println(" -alg <digest algorithm> Specifies the digest algorithm (default is MD5)"); 122 System.out.println(); 123 } 124 125 130 public static String obfuscatePassword(String password) 131 { 132 return obfuscatePassword(password, "MD5"); 133 } 134 135 143 public static String obfuscatePassword(String password, String algorithm) 144 { 145 try 146 { 147 MessageDigest digest = MessageDigest.getInstance(algorithm); 148 byte[] digestedBytes = digest.digest(password.getBytes()); 149 byte[] obfuscatedBytes = Base64Codec.encodeBase64(digestedBytes); 150 return LEFT_DELIMITER + algorithm + RIGHT_DELIMITER + new String (obfuscatedBytes); 151 } 152 catch (NoSuchAlgorithmException x) 153 { 154 throw new SecurityException ("Could not find digest algorithm " + algorithm); 155 } 156 } 157 158 private Map passwords; 159 160 166 public PasswordAuthenticator(File passwordFile) throws IOException 167 { 168 this(new FileInputStream (passwordFile)); 169 } 170 171 177 public PasswordAuthenticator(InputStream is) throws IOException 178 { 179 passwords = readPasswords(is); 180 } 181 182 private Map readPasswords(InputStream is) throws IOException 183 { 184 Properties properties = new Properties (); 185 try 186 { 187 properties.load(is); 188 } 189 finally 190 { 191 is.close(); 192 } 193 return new HashMap (properties); 194 } 195 196 public Subject authenticate(Object credentials) throws SecurityException 197 { 198 if (!(credentials instanceof String [])) throw new SecurityException ("Bad credentials"); 199 String [] creds = (String [])credentials; 200 if (creds.length != 2) throw new SecurityException ("Bad credentials"); 201 202 String user = creds[0]; 203 String password = creds[1]; 204 205 if (password == null) throw new SecurityException ("Bad password"); 206 207 if (!passwords.containsKey(user)) throw new SecurityException ("Unknown user " + user); 208 209 String storedPassword = (String )passwords.get(user); 210 if (!isPasswordCorrect(password, storedPassword)) throw new SecurityException ("Bad password"); 211 212 Set principals = new HashSet (); 213 principals.add(new JMXPrincipal (user)); 214 return new Subject (true, principals, Collections.EMPTY_SET, Collections.EMPTY_SET); 215 } 216 217 private boolean isPasswordCorrect(String password, String storedPassword) 218 { 219 if (password.startsWith(LEFT_DELIMITER)) 220 { 221 if (storedPassword.startsWith(LEFT_DELIMITER)) 222 { 223 return password.equals(storedPassword); 224 } 225 else 226 { 227 String algorithm = getAlgorithm(password); 228 String obfuscated = obfuscatePassword(storedPassword, algorithm); 229 return password.equals(obfuscated); 230 } 231 } 232 else 233 { 234 if (storedPassword.startsWith(LEFT_DELIMITER)) 235 { 236 String algorithm = getAlgorithm(storedPassword); 238 String obfuscated = obfuscatePassword(password, algorithm); 239 return obfuscated.equals(storedPassword); 240 } 241 else 242 { 243 return password.equals(storedPassword); 244 } 245 } 246 } 247 248 private String getAlgorithm(String obfuscatedPassword) 249 { 250 try 251 { 252 return obfuscatedPassword.substring(LEFT_DELIMITER.length(), obfuscatedPassword.indexOf(RIGHT_DELIMITER)); 253 } 254 catch (IndexOutOfBoundsException x) 255 { 256 throw new SecurityException ("Bad password"); 257 } 258 } 259 } 260 | Popular Tags |