1 17 package org.apache.servicemix.jbi.jmx; 18 19 26 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.security.MessageDigest ; 32 import java.security.NoSuchAlgorithmException ; 33 import java.util.Collections ; 34 import java.util.HashMap ; 35 import java.util.HashSet ; 36 import java.util.Map ; 37 import java.util.Properties ; 38 import java.util.Set ; 39 import javax.management.remote.JMXAuthenticator ; 40 import javax.management.remote.JMXPrincipal ; 41 import javax.security.auth.Subject ; 42 43 import mx4j.util.Base64Codec; 44 45 93 public class PasswordAuthenticator implements JMXAuthenticator 94 { 95 private static final String LEFT_DELIMITER = "OBF("; 96 private static final String RIGHT_DELIMITER = "):"; 97 98 104 public static void main(String [] args) throws Exception 105 { 106 if (args.length == 1) 107 { 108 if (!"-help".equals(args[0])) 109 { 110 printPassword("MD5", args[0]); 111 return; 112 } 113 } 114 else if (args.length == 3) 115 { 116 if ("-alg".equals(args[0])) 117 { 118 printPassword(args[1], args[2]); 119 return; 120 } 121 } 122 printUsage(); 123 } 124 125 private static void printPassword(String algorithm, String input) 126 { 127 String password = obfuscatePassword(input, algorithm); 128 System.out.println(password); 129 } 130 131 private static void printUsage() 132 { 133 System.out.println(); 134 System.out.println("Usage: java -cp <lib>/mx4j-tools.jar mx4j.tools.remote.PasswordAuthenticator <options> <password>"); 135 System.out.println("Where <options> is one of the following:"); 136 System.out.println(" -help Prints this message"); 137 System.out.println(" -alg <digest algorithm> Specifies the digest algorithm (default is MD5)"); 138 System.out.println(); 139 } 140 141 146 public static String obfuscatePassword(String password) 147 { 148 return obfuscatePassword(password, "MD5"); 149 } 150 151 159 public static String obfuscatePassword(String password, String algorithm) 160 { 161 try 162 { 163 MessageDigest digest = MessageDigest.getInstance(algorithm); 164 byte[] digestedBytes = digest.digest(password.getBytes()); 165 byte[] obfuscatedBytes = Base64Codec.encodeBase64(digestedBytes); 166 return LEFT_DELIMITER + algorithm + RIGHT_DELIMITER + new String (obfuscatedBytes); 167 } 168 catch (NoSuchAlgorithmException x) 169 { 170 throw new SecurityException ("Could not find digest algorithm " + algorithm); 171 } 172 } 173 174 private Map passwords; 175 176 182 public PasswordAuthenticator(File passwordFile) throws IOException 183 { 184 this(new FileInputStream (passwordFile)); 185 } 186 187 193 public PasswordAuthenticator(InputStream is) throws IOException 194 { 195 passwords = readPasswords(is); 196 } 197 198 private Map readPasswords(InputStream is) throws IOException 199 { 200 Properties properties = new Properties (); 201 try 202 { 203 properties.load(is); 204 } 205 finally 206 { 207 is.close(); 208 } 209 return new HashMap (properties); 210 } 211 212 public Subject authenticate(Object credentials) throws SecurityException 213 { 214 if (!(credentials instanceof String [])) throw new SecurityException ("Bad credentials"); 215 String [] creds = (String [])credentials; 216 if (creds.length != 2) throw new SecurityException ("Bad credentials"); 217 218 String user = creds[0]; 219 String password = creds[1]; 220 221 if (password == null) throw new SecurityException ("Bad password"); 222 223 if (!passwords.containsKey(user)) throw new SecurityException ("Unknown user " + user); 224 225 String storedPassword = (String )passwords.get(user); 226 if (!isPasswordCorrect(password, storedPassword)) throw new SecurityException ("Bad password"); 227 228 Set principals = new HashSet (); 229 principals.add(new JMXPrincipal (user)); 230 return new Subject (true, principals, Collections.EMPTY_SET, Collections.EMPTY_SET); 231 } 232 233 private boolean isPasswordCorrect(String password, String storedPassword) 234 { 235 if (password.startsWith(LEFT_DELIMITER)) 236 { 237 if (storedPassword.startsWith(LEFT_DELIMITER)) 238 { 239 return password.equals(storedPassword); 240 } 241 else 242 { 243 String algorithm = getAlgorithm(password); 244 String obfuscated = obfuscatePassword(storedPassword, algorithm); 245 return password.equals(obfuscated); 246 } 247 } 248 else 249 { 250 if (storedPassword.startsWith(LEFT_DELIMITER)) 251 { 252 String algorithm = getAlgorithm(storedPassword); 254 String obfuscated = obfuscatePassword(password, algorithm); 255 return obfuscated.equals(storedPassword); 256 } 257 else 258 { 259 return password.equals(storedPassword); 260 } 261 } 262 } 263 264 private String getAlgorithm(String obfuscatedPassword) 265 { 266 try 267 { 268 return obfuscatedPassword.substring(LEFT_DELIMITER.length(), obfuscatedPassword.indexOf(RIGHT_DELIMITER)); 269 } 270 catch (IndexOutOfBoundsException x) 271 { 272 throw new SecurityException ("Bad password"); 273 } 274 } 275 } 276 | Popular Tags |