001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.converter.bea;
017:
018: import java.util.regex.Pattern;
019: import java.util.regex.Matcher;
020: import java.util.Properties;
021: import java.util.Iterator;
022: import java.lang.reflect.Method;
023: import java.lang.reflect.InvocationTargetException;
024: import java.io.File;
025: import java.io.FileInputStream;
026: import java.io.FileNotFoundException;
027: import java.io.BufferedReader;
028: import java.io.FileReader;
029: import java.io.StringWriter;
030: import java.io.PrintWriter;
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.net.URLClassLoader;
034: import java.net.URL;
035:
036: /**
037: * Reads information out of the WebLogic domain directory.
038: * Needs access to the WebLogic JARs in the weblogic81/server/lib directory.
039: *
040: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
041: */
042: public class Weblogic81Utils {
043: private final static Pattern ENCRYPTED_STRING = Pattern
044: .compile("\\\"\\{\\S+\\}\\S+?\\\"");
045: private Object decoder;
046: private Method decode;
047: private Object decrypter;
048: private Method decrypt;
049: private File domainDir;
050:
051: public Weblogic81Utils(String libDirPath, String domainDirPath) {
052: File libDir = new File(libDirPath);
053: if (!libDir.exists() || !libDir.canRead()
054: || !libDir.isDirectory())
055: throw new IllegalArgumentException("Bad weblogic lib dir");
056: File weblogicJar = new File(libDir, "weblogic.jar");
057: File securityJar = new File(libDir, "jsafeFIPS.jar");
058: if (!weblogicJar.canRead())
059: throw new IllegalArgumentException(
060: "Cannot find JARs in provided lib dir");
061: domainDir = new File(domainDirPath);
062: if (!domainDir.exists() || !domainDir.canRead()
063: || !domainDir.isDirectory())
064: throw new IllegalArgumentException("Bad domain directory");
065: File state = new File(domainDir, "SerializedSystemIni.dat");
066: if (!state.canRead())
067: throw new IllegalArgumentException(
068: "Cannot find serialized state in domain directory");
069: try {
070: ClassLoader loader = new URLClassLoader(securityJar
071: .exists() ? new URL[] { weblogicJar.toURL(),
072: securityJar.toURL() } : new URL[] { weblogicJar
073: .toURL() }, Weblogic81Utils.class.getClassLoader());
074: initialize(loader, state);
075: } catch (Exception e) {
076: throw (RuntimeException) new IllegalArgumentException(
077: "Unable to initialize encryption routines from provided arguments")
078: .initCause(e);
079: }
080: }
081:
082: public Properties getBootProperties() {
083: File boot = new File(domainDir, "boot.properties");
084: FileInputStream bootIn = null;
085: try {
086: bootIn = new FileInputStream(boot);
087: } catch (FileNotFoundException e) {
088: return null;
089: }
090: try {
091: Properties props = new Properties();
092: props.load(bootIn);
093: bootIn.close();
094: for (Iterator it = props.keySet().iterator(); it.hasNext();) {
095: String key = (String) it.next();
096: String value = props.getProperty(key);
097: if (value != null && value.startsWith("{"))
098: props.setProperty(key, decryptString(value));
099: }
100: return props;
101: } catch (Exception e) {
102: return null;
103: }
104: }
105:
106: public String getConfigXML() throws FileNotFoundException {
107: File config = new File(domainDir, "config.xml");
108: BufferedReader in = new BufferedReader(new FileReader(config));
109: StringWriter string = new StringWriter();
110: PrintWriter out = new PrintWriter(string);
111: String line;
112: Matcher m = ENCRYPTED_STRING.matcher("");
113: try {
114: while ((line = in.readLine()) != null) {
115: m.reset(line);
116: int last = -1;
117: while (m.find()) {
118: out.print(line.substring(last + 1, m.start()));
119: String s = line.substring(m.start(), m.end());
120: out.print("\"");
121: out.print(decryptString(s.substring(1,
122: s.length() - 1)));
123: out.print("\"");
124: last = m.end() - 1;
125: }
126: if (last == -1) {
127: out.println(line);
128: } else {
129: if (line.length() > last + 1) {
130: out.print(line.substring(last + 1));
131: }
132: out.println();
133: }
134: out.flush();
135: }
136: in.close();
137: out.close();
138: } catch (Exception e) {
139: return null;
140: }
141: return string.getBuffer().toString();
142: }
143:
144: private void initialize(ClassLoader loader, File state)
145: throws IOException, IllegalAccessException,
146: NoSuchMethodException, InvocationTargetException,
147: ClassNotFoundException, InstantiationException {
148: byte[] salt = null, key = null;
149: FileInputStream in = new FileInputStream(state);
150: salt = readBytes(in);
151: int i = in.read();
152: if (i != -1) {
153: if (i != 1)
154: throw new IllegalStateException();
155: key = readBytes(in);
156: }
157: in.close();
158: decrypter = getEncryptionService(loader, salt, key);
159: decoder = loader.loadClass(
160: "weblogic.utils.encoders.BASE64Decoder").newInstance();
161: decode = decoder.getClass().getMethod("decodeBuffer",
162: new Class[] { String.class });
163: decrypt = decrypter.getClass().getMethod("decryptString",
164: new Class[] { byte[].class });
165: }
166:
167: private static byte[] readBytes(InputStream in) throws IOException {
168: int len = in.read();
169: if (len < 0)
170: throw new IOException("stream is empty");
171: byte result[] = new byte[len];
172: int index = 0;
173: while (true) {
174: if (index >= len) {
175: break;
176: }
177: int count = in.read(result, index, len - index);
178: if (count == -1)
179: break;
180: index += count;
181: }
182: return result;
183: }
184:
185: private String decryptString(String string)
186: throws IllegalAccessException, InvocationTargetException {
187: if (string.indexOf('}') > -1) {
188: string = string.substring(string.indexOf("}") + 1);
189: }
190: return (String) decrypt.invoke(decrypter, new Object[] { decode
191: .invoke(decoder, new Object[] { string }) });
192: }
193:
194: static Object getEncryptionService(ClassLoader loader, byte salt[],
195: byte key[]) throws NoSuchMethodException,
196: ClassNotFoundException, IllegalAccessException,
197: InvocationTargetException {
198: String magic = "0xccb97558940b82637c8bec3c770f86fa3a391a56";
199: Object factory = loader
200: .loadClass(
201: "weblogic.security.internal.encryption.JSafeEncryptionServiceImpl")
202: .getMethod("getFactory", new Class[0]).invoke(null,
203: null);
204: Method getter = factory.getClass()
205: .getMethod(
206: "getEncryptionService",
207: new Class[] { byte[].class, String.class,
208: byte[].class });
209: return getter
210: .invoke(factory, new Object[] { salt, magic, key });
211: }
212: }
|