1 23 24 29 30 package com.sun.enterprise.admin.servermgmt; 31 32 import com.sun.enterprise.admin.servermgmt.pe.PEFileLayout; 33 import com.sun.enterprise.security.store.PasswordAdapter; 34 import com.sun.enterprise.security.util.SSHA; 35 import com.sun.enterprise.util.i18n.StringManager; 36 import com.sun.enterprise.util.SystemPropertyConstants; 37 38 import java.io.ByteArrayOutputStream ; 39 import java.io.BufferedInputStream ; 40 import java.io.BufferedOutputStream ; 41 import java.io.File ; 42 import java.io.FileInputStream ; 43 import java.io.FileOutputStream ; 44 import java.io.IOException ; 45 46 47 56 public class MasterPasswordFileManager extends KeystoreManager { 57 58 private static final String MASTER_PASSWORD_ALIAS="master-password"; 59 private static final String ENCODED_CHARSET = "UTF-8"; 60 private static final int SALT_SIZE = 8; 61 62 private static final StringManager _strMgr = 63 StringManager.getManager(MasterPasswordFileManager.class); 64 65 66 public MasterPasswordFileManager() { 67 super(); 68 } 69 70 74 private char[] getMasterPasswordPassword(RepositoryConfig config) 75 throws RepositoryException 76 { 77 return MASTER_PASSWORD_ALIAS.toCharArray(); 79 } 80 81 protected void deleteMasterPasswordFile(RepositoryConfig config) 82 { 83 final PEFileLayout layout = getFileLayout(config); 84 final File pwdFile = layout.getMasterPasswordFile(); 85 pwdFile.delete(); 86 } 87 88 95 protected void createMasterPasswordFile( 96 RepositoryConfig config, String masterPassword) 97 throws RepositoryException 98 { 99 final PEFileLayout layout = getFileLayout(config); 100 final File pwdFile = layout.getMasterPasswordFile(); 101 try { 102 PasswordAdapter p = new PasswordAdapter(pwdFile.getAbsolutePath(), 103 getMasterPasswordPassword(config)); 104 p.setPasswordForAlias(MASTER_PASSWORD_ALIAS, masterPassword.getBytes()); 105 chmod("600", pwdFile); 106 } catch (Exception ex) { 107 throw new RepositoryException(_strMgr.getString("masterPasswordFileNotCreated", pwdFile), 108 ex); 109 } 110 } 111 112 118 public String readMasterPasswordFile( 119 RepositoryConfig config) throws RepositoryException 120 { 121 final PEFileLayout layout = getFileLayout(config); 122 final File pwdFile = layout.getMasterPasswordFile(); 123 if (pwdFile.exists()) { 124 try { 125 PasswordAdapter p = new PasswordAdapter(pwdFile.getAbsolutePath(), 126 getMasterPasswordPassword(config)); 127 return p.getPasswordForAlias(MASTER_PASSWORD_ALIAS); 128 } catch (Exception ex) { 129 throw new RepositoryException(_strMgr.getString("masterPasswordFileNotRead", pwdFile), 130 ex); 131 } 132 } else { 133 return null; 135 } 136 } 137 144 protected void changeMasterPasswordInMasterPasswordFile(RepositoryConfig config, String newPassword, 145 boolean saveMasterPassword) throws RepositoryException 146 { 147 deleteMasterPasswordFile(config); 148 if (saveMasterPassword) { 149 createMasterPasswordFile(config, newPassword); 150 } 151 } 152 } 153 | Popular Tags |