KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > upgrade > common > PasswordVerifier


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.tools.upgrade.common;
25
26 import com.sun.enterprise.tools.upgrade.certconversion.ProcessAdaptor;
27
28 import java.io.*;
29 import java.security.KeyStore JavaDoc;
30 import java.util.logging.Level JavaDoc;
31
32 /**
33  *
34  * author : Servesh Singh
35  *
36  */

37
38 public class PasswordVerifier {
39     private static final String JavaDoc NSS_DB_LOCATION_OPTION = "-d";
40     private static final String JavaDoc LIST_KEY_ID = "-K";
41     private static final String JavaDoc CERT_NSS_PWD_OPTION = "-f";
42     private static final String JavaDoc CERT_UTIL_UNIX = "certutil.sh";
43     private static final String JavaDoc CERT_UTIL_WIN = "certutil.bat";
44     private static final String JavaDoc CONFIG = "config";
45     private static final String JavaDoc BIN = "bin";
46     private static final String JavaDoc LIB = "lib";
47     private static final String JavaDoc UPGRADE = "upgrade";
48     
49     public static boolean verifySourceNSSPassword(CommonInfoModel commonInfo,String JavaDoc configDir) {
50         if(commonInfo.getSourceDomainRootFlag()&& (new File(commonInfo.getSourceDomainRoot()).equals(new File(commonInfo.getTargetDomainRoot()))))
51             return true;
52         if(!(new File(configDir).exists()))
53             return true;
54         String JavaDoc osName = commonInfo.getOSName();
55         String JavaDoc libDir = "";
56         String JavaDoc binDir = "";
57         String JavaDoc certutilLocation = "";
58         if(commonInfo.getSourceVersion().equals(UpgradeConstants.VERSION_7X)){
59             libDir = commonInfo.getSourceInstallDir() +File.separator + LIB;
60             binDir = commonInfo.getSourceInstallDir() +File.separator + BIN;
61             //certutil/pk12util should be picked from target installation
62
certutilLocation = commonInfo.getTargetInstallDir() +File.separator + LIB + File.separator + UPGRADE;
63         } else {
64             libDir = commonInfo.getTargetInstallDir() +File.separator + LIB;
65             binDir = commonInfo.getTargetInstallDir() +File.separator + BIN;
66             certutilLocation = commonInfo.getTargetInstallDir() +File.separator + LIB;
67         }
68         String JavaDoc certUtilPath = "";
69         if(osName.indexOf("Windows") == -1)
70             certUtilPath = commonInfo.getTargetInstallDir() + File.separator + LIB + File.separator + CERT_UTIL_UNIX;
71         else
72             certUtilPath = commonInfo.getTargetInstallDir() + File.separator + LIB + File.separator + CERT_UTIL_WIN;
73         
74         StringWriter result = new StringWriter();
75         String JavaDoc nssPwd = commonInfo.getCertDbPassword();
76         String JavaDoc domainRoot = commonInfo.getTargetDomainRoot();
77         //String domainPath = commonInfo.getDestinationDomainPath();
78
String JavaDoc nssPwdFile = domainRoot +File.separator +"pwdfile";
79         PrintWriter pw = null;
80         try{
81             pw = new PrintWriter(new FileOutputStream(nssPwdFile ));
82             pw.println(nssPwd);
83             pw.flush();
84             pw.close();
85         }catch(FileNotFoundException fe) {
86         }finally {
87             try {
88                 if(pw !=null)
89                     pw.close();
90             }catch(Exception JavaDoc e){}
91         }
92         String JavaDoc commandString = certUtilPath + " " +
93                 LIST_KEY_ID + " " + NSS_DB_LOCATION_OPTION +
94                 " " + configDir + " " + CERT_NSS_PWD_OPTION +
95                 " " + nssPwdFile +
96                 " " +libDir +
97                 " " +binDir +
98                 " " +certutilLocation;
99         int exitVal = ProcessAdaptor.executeProcess(commandString, result);
100         result.flush();
101         String JavaDoc resultString = result.toString();
102         File pwdfile = new File(nssPwdFile);
103         pwdfile.delete();
104         if(exitVal == 0) {
105             return parseAndVerify(resultString);
106         } else if(exitVal == 255) { //no keys found
107
return true;
108         } else {
109             return false;
110         }
111         
112     }
113     
114     public static boolean verifyTargetNSSPassword(CommonInfoModel commonInfo,String JavaDoc configDir) {
115         if(commonInfo.getSourceDomainRootFlag()&& (new File(commonInfo.getSourceDomainRoot()).equals(new File(commonInfo.getTargetDomainRoot()))))
116             return true;
117         if(!(new File(configDir).exists()))
118             return true;
119         String JavaDoc osName = commonInfo.getOSName();
120         String JavaDoc libDir = "";
121         String JavaDoc binDir = "";
122         libDir = commonInfo.getTargetInstallDir() +File.separator + LIB;
123         binDir = commonInfo.getTargetInstallDir() +File.separator + BIN;
124         String JavaDoc certutilLocation = libDir;
125         String JavaDoc certUtilPath = "";
126         if(osName.indexOf("Windows") == -1)
127             certUtilPath = commonInfo.getTargetInstallDir() + File.separator + LIB + File.separator + CERT_UTIL_UNIX;
128         else
129             certUtilPath = commonInfo.getTargetInstallDir() + File.separator + LIB + File.separator + CERT_UTIL_WIN;
130         
131         StringWriter result = new StringWriter();
132         String JavaDoc nssPwd = commonInfo.getTargetCertDbPassword();
133         String JavaDoc domainRoot = commonInfo.getTargetDomainRoot();
134         //String domainPath = commonInfo.getDestinationDomainPath();
135
String JavaDoc nssPwdFile = domainRoot +File.separator +"pwdfile";
136         PrintWriter pw = null;
137         try{
138             pw = new PrintWriter(new FileOutputStream(nssPwdFile ));
139             pw.println(nssPwd);
140             pw.flush();
141             pw.close();
142         }catch(FileNotFoundException fe) {
143         }finally {
144             try {
145                 if(pw !=null)
146                     pw.close();
147             }catch(Exception JavaDoc e){}
148         }
149         String JavaDoc commandString = certUtilPath + " " +
150                 LIST_KEY_ID + " " + NSS_DB_LOCATION_OPTION +
151                 " " + configDir + " " + CERT_NSS_PWD_OPTION +
152                 " " + nssPwdFile +
153                 " " +libDir +
154                 " " +binDir +
155                 " " +certutilLocation;
156         int exitVal = ProcessAdaptor.executeProcess(commandString, result);
157         result.flush();
158         String JavaDoc resultString = result.toString();
159         File pwdfile = new File(nssPwdFile);
160         pwdfile.delete();
161         if(exitVal == 0) {
162             return parseAndVerify(resultString);
163         }else {
164             return false;
165         }
166         
167     }
168     
169     public static boolean verifySourceNSSPassword(CommonInfoModel commonInfo) {
170         if(commonInfo.getSourceDomainRootFlag()&& (new File(commonInfo.getSourceDomainRoot()).equals(new File(commonInfo.getTargetDomainRoot()))))
171             return true;
172         /*File sourceDomain = new File(commonInfo.getSourceDomainPath());
173         String [] dirs = sourceDomain.list();
174         if(dirs.length == 1) {
175             //_logger.log(Level.WARNING, sm.getString("enterprise.tools.upgrade.no_server_instance", sourceDomain));
176             //continue;
177             return false;
178         }
179         String instanceName ="";
180         if(dirs[0].equals("admin-server"))
181             instanceName = dirs[1];
182         else
183             instanceName = dirs[0];
184         commonInfo.setCurrentSourceInstance(instanceName);
185          */

186         String JavaDoc instanceName = commonInfo.getCurrentSourceInstance();
187         String JavaDoc certificateDomainDir = "";
188         if((commonInfo.getSourceVersionAndEdition().equals(UpgradeConstants.VERSION_AS80_PE) ||
189                 commonInfo.getSourceVersionAndEdition().equals(UpgradeConstants.VERSION_AS81_PE) ||
190                 commonInfo.getSourceVersionAndEdition().equals(UpgradeConstants.VERSION_AS90_PE))&&
191                 (commonInfo.getTargetVersionAndEdition().equals(UpgradeConstants.VERSION_AS90_SE) ||
192                 commonInfo.getTargetVersionAndEdition().equals(UpgradeConstants.VERSION_AS90_EE)))
193             certificateDomainDir = commonInfo.getDestinationDomainPath();
194         else
195             certificateDomainDir = commonInfo.getSourceInstancePath();
196         String JavaDoc configDir = certificateDomainDir + File.separator + CONFIG;
197         return verifySourceNSSPassword(commonInfo,configDir);
198     }
199     
200     private static boolean parseAndVerify(String JavaDoc input) {
201         try {
202             BufferedReader reader = new BufferedReader(new StringReader(input));
203             //Reading the Line <0> KEY
204
String JavaDoc readString =reader.readLine();
205             while(readString != null) {
206                 //Key starts from 4th Index
207
String JavaDoc marker = readString.substring(0,1);
208                 String JavaDoc anotherMarker = readString.substring(2,3);
209                 if(!(marker.equals("<") && anotherMarker.equals(">"))) {
210                     return false;
211                 }
212                 readString =reader.readLine();
213             }
214         }catch (Exception JavaDoc e) {
215             return false;
216         }
217         return true;
218     }
219     
220     public static boolean verifyKeystorePassword(String JavaDoc jksPath, String JavaDoc jksKeyStorePassword ){
221         //if(commonInfo.getSourceDomainRootFlag()&& (new File(commonInfo.getSourceDomainRoot()).getPath().equals(new File(commonInfo.getTargetDomainRoot()).getPath())))
222
//return true;
223
File jksfile = new File(jksPath);
224         if(!jksfile.exists()) {
225             if(jksKeyStorePassword.equals("changeit"))
226                 return true;
227             else
228                 return false;
229         }
230         InputStream inputStreamJks = null;
231         KeyStore JavaDoc jksKeyStore;
232         try{
233             inputStreamJks = new FileInputStream(jksPath);
234             jksKeyStore = KeyStore.getInstance("JKS");
235             jksKeyStore.load(inputStreamJks, jksKeyStorePassword.toCharArray());
236         }catch(Exception JavaDoc e){
237             return false;
238         }finally{
239             if(inputStreamJks!=null)
240                 try{inputStreamJks.close();}catch(Exception JavaDoc e){}
241         }
242         return true;
243     }
244 }
245
Popular Tags