1 23 package com.sun.enterprise.security.store; 24 25 import com.sun.enterprise.util.SystemPropertyConstants; 26 27 import java.io.BufferedReader ; 28 import java.io.InputStreamReader ; 29 import java.io.InputStream ; 30 import java.io.IOException ; 31 import java.io.File ; 32 import java.io.OutputStream ; 33 import java.io.OutputStreamWriter ; 34 import java.io.PrintWriter ; 35 import java.io.BufferedWriter ; 36 import java.util.Hashtable ; 37 import java.util.HashMap ; 38 import java.util.Map ; 39 import java.util.Iterator ; 40 import java.util.ArrayList ; 41 import java.util.Properties ; 42 import java.security.cert.CertificateException ; 43 import java.security.KeyStoreException ; 44 import java.security.NoSuchAlgorithmException ; 45 import java.security.UnrecoverableKeyException ; 46 47 public class IdentityManager { 48 49 public static final String PROPMPT_FOR_IDENTITY_SYSTEM_PROPERTY = "com.sun.aas.promptForIdentity"; 50 private static final String USER_ALIAS="admin-user"; 51 private static final String PASSWORD_ALIAS="admin-password"; 52 private static final String MASTER_PASSWORD_ALIAS="master-password"; 53 private static final String IDENTITY_STORE_FILE_NAME=".identity"; 54 55 private static String _user=null; 56 private static String _password=null; 57 private static String _masterPassword=null; 58 private static Hashtable _htIdentity=new Hashtable (); 59 private static boolean bDebug=false; 60 private static boolean _keystorePropertyWasSet = true; 61 private static boolean _truststorePropertyWasSet = true; 62 private static boolean _nssDbPasswordPropertyWasSet = true; 63 64 private IdentityManager() {} 66 67 71 public static String [] getIdentityArray() { 72 ArrayList ar=new ArrayList (); 73 ar.add(getUser()); 75 ar.add(getPassword()); 76 ar.add(getMasterPassword()); 77 78 Iterator it=_htIdentity.keySet().iterator(); 80 String key=null; 81 while(it.hasNext()) { 82 key=(String )it.next(); 83 ar.add(key + "=" + (String )_htIdentity.get(key)); 84 } 85 86 String [] identity=new String [ar.size()]; 87 identity=(String [])ar.toArray(identity); 88 89 return identity; 90 } 91 92 93 97 public static void populateFromInputStream() throws IOException { 98 populateFromInputStream(System.in); 99 } 100 101 102 106 public static void populateFromInputStream(InputStream in) throws IOException { 107 108 if (bDebug) System.out.println("IM seeing if need to read in security properties from stdin"); 112 if (in == null || System.getProperty(PROPMPT_FOR_IDENTITY_SYSTEM_PROPERTY) == null) { 113 return; 114 } 115 116 BufferedReader br=null; 117 try { 118 if (bDebug) System.out.println("IM attempting to read from inputstream"); 120 br=new BufferedReader (new InputStreamReader (System.in)); 121 String sxLine=null; 122 int cnt=0, ipos=0; 123 System.out.println("Enter Admin User:"); 125 while ((sxLine=br.readLine()) != null) { 126 if (bDebug) System.out.println("IM Number read - Reading Line:" + cnt + " - " + sxLine); 127 128 switch (cnt) { 130 case 0: 131 setUser(sxLine); 132 System.out.println("Enter Admin Password:"); 134 break; 135 case 1: 136 setPassword(sxLine); 137 System.out.println("Enter Master Password:"); 139 break; 140 case 2: 141 setMasterPassword(sxLine); 142 System.out.println("Enter Other Password Information (or ctrl-D or ctrl-Z):"); 143 break; 144 default: 145 putTokenizedString(sxLine) ; 147 System.out.println("Enter Other Password Information (or ctrl-D or ctrl-Z):"); 148 } 149 cnt++; 151 152 } 153 } catch (IOException e) { 154 throw e; 155 } 156 } 157 158 159 163 public static void writeToOutputStream(OutputStream out) { 164 if (out == null) return; 166 167 PrintWriter writer=null; 168 writer = new PrintWriter (new BufferedWriter (new OutputStreamWriter (out))); 170 if (bDebug) System.out.println("Writing to OutputStream: " + getFormatedContents()); 171 writer.println(getUser()); 173 writer.println(getPassword()); 174 writer.println(getMasterPassword()); 175 176 Iterator it=_htIdentity.keySet().iterator(); 178 String key=null; 179 while(it.hasNext()) { 180 key=(String )it.next(); 181 writer.println(key + "=" + (String )_htIdentity.get(key)); 182 } 183 writer.flush(); 184 writer.close(); 185 } 186 187 188 191 public static String getFormatedContents() { 192 StringBuffer sb=new StringBuffer ("IdentityManager Data: User:" + getUser()); 193 if (bDebug) { 196 sb.append(", "); 197 sb.append("Password:" + getPassword() + ", "); 198 sb.append("MasterPassword:" + getMasterPassword() + ", "); 199 Iterator it=_htIdentity.keySet().iterator(); 200 String key=null; 201 while(it.hasNext()) { 202 key=(String )it.next(); 203 sb.append(key + ":" + (String )_htIdentity.get(key) + ", "); 204 } 205 } 206 return sb.toString(); 207 } 208 209 210 public static void setUser(String userx) { 213 _user=userx; 214 } 215 public static String getUser() { 216 return _user; 217 } 218 219 public static void setPassword(String passwordx) { 220 _password=passwordx; 221 } 222 223 public static String getPassword() { 224 return _password; 225 } 226 227 public static void setMasterPassword(String masterPasswordx) { 228 _masterPassword=masterPasswordx; 229 if (System.getProperty(SystemPropertyConstants.KEYSTORE_PROPERTY) != null) { 238 if (!_keystorePropertyWasSet || 239 System.getProperty(SystemPropertyConstants.KEYSTORE_PASSWORD_PROPERTY) == null) 240 { 241 System.setProperty(SystemPropertyConstants.KEYSTORE_PASSWORD_PROPERTY, 242 getMasterPassword()); 243 _keystorePropertyWasSet = false; 244 } 245 } 246 if (System.getProperty(SystemPropertyConstants.TRUSTSTORE_PROPERTY) != null) { 247 if (!_truststorePropertyWasSet || 248 System.getProperty(SystemPropertyConstants.TRUSTSTORE_PASSWORD_PROPERTY) == null) 249 { 250 System.setProperty(SystemPropertyConstants.TRUSTSTORE_PASSWORD_PROPERTY, 251 getMasterPassword()); 252 _truststorePropertyWasSet = false; 253 } 254 } 255 if (System.getProperty(SystemPropertyConstants.NSS_DB_PROPERTY) != null) { 256 if (!_nssDbPasswordPropertyWasSet || 257 System.getProperty(SystemPropertyConstants.NSS_DB_PASSWORD_PROPERTY) == null) 258 { 259 System.setProperty(SystemPropertyConstants.NSS_DB_PASSWORD_PROPERTY, 260 getMasterPassword()); 261 _nssDbPasswordPropertyWasSet = false; 262 } 263 } 264 } 265 266 267 public static String getMasterPassword() { 268 return _masterPassword; 269 } 270 271 272 public static void putTokenizedString(String sxToken) { 273 int ipos=sxToken.indexOf("="); 276 if (ipos > 0) { 277 put(sxToken.substring(0, ipos), sxToken.substring(ipos + 1)); 279 } 280 } 281 282 283 public static void put(String key, String value) { 284 _htIdentity.put(key, value); 286 } 287 288 public static String get(String key) { 289 return (String )_htIdentity.get(key); 290 } 291 292 public static void addToMap(HashMap map) 293 { 294 Iterator it = map.keySet().iterator(); 295 String key = null; 296 while(it.hasNext()) { 297 key = (String )it.next(); 298 put(key, (String )map.get(key)); 299 } 300 } 301 302 public static Map getMap() { 303 HashMap hm=new HashMap (); 306 Iterator it=_htIdentity.keySet().iterator(); 307 String key=null; 308 while(it.hasNext()) { 309 key=(String )it.next(); 310 hm.put(new String (key), new String ((String )_htIdentity.get(key))); 311 } 312 return hm; 313 } 314 315 316 317 318 319 320 323 329 public static void createIdentityStore() 330 throws KeyStoreException , CertificateException , NoSuchAlgorithmException , IOException 331 { 332 333 Properties aliasPasswordProps=new Properties (); 335 aliasPasswordProps.setProperty(USER_ALIAS, getUser()); 336 aliasPasswordProps.setProperty(PASSWORD_ALIAS, getPassword()); 337 aliasPasswordProps.setProperty(MASTER_PASSWORD_ALIAS, getMasterPassword()); 338 339 File instanceRoot = new File (System.getProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY), IDENTITY_STORE_FILE_NAME); 340 341 PasswordAdapter p = new PasswordAdapter(instanceRoot.getAbsolutePath(), 343 getMasterPasswordPassword()); 344 345 Iterator iter=aliasPasswordProps.keySet().iterator(); 347 String alias=null, pass=null; 348 while(iter.hasNext()) { 349 alias=(String )iter.next(); 350 pass=aliasPasswordProps.getProperty(alias); 351 p.setPasswordForAlias(alias, pass.getBytes()); 352 } 353 } 354 355 356 362 public static void readIdentityStore() 363 throws KeyStoreException , CertificateException , NoSuchAlgorithmException , IOException , UnrecoverableKeyException 364 { 365 366 367 File instanceRoot = new File (System.getProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY), IDENTITY_STORE_FILE_NAME); 368 369 System.out.println("****** READING IDENTITY FROM ====>" + instanceRoot.getAbsolutePath()); 370 371 if (instanceRoot.exists()) { 372 PasswordAdapter p = new PasswordAdapter(instanceRoot.getAbsolutePath(), 373 getMasterPasswordPassword()); 374 375 setUser(p.getPasswordForAlias(USER_ALIAS)); 376 setPassword(p.getPasswordForAlias(PASSWORD_ALIAS)); 377 setMasterPassword(p.getPasswordForAlias(MASTER_PASSWORD_ALIAS)); 378 } 379 } 380 381 382 383 public static void deleteIdentityStore() { 384 File instanceRoot = new File (System.getProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY), IDENTITY_STORE_FILE_NAME); 385 instanceRoot.delete(); 386 } 387 388 389 390 394 private static char[] getMasterPasswordPassword() 395 { 396 return MASTER_PASSWORD_ALIAS.toCharArray(); 398 } 399 400 401 } 402 | Popular Tags |