001: package migration.modules.ldap;
002:
003: import netscape.ldap.util.*;
004:
005: import netscape.ldap.LDAPAttribute;
006: import netscape.ldap.LDAPAttributeSet;
007:
008: import java.util.*;
009: import java.io.*;
010:
011: import com.iplanet.portalserver.util.Password;
012:
013: public class DecryptPassword {
014: public static void main(String args[]) {
015:
016: String outputfile, ldiffile;
017:
018: ldiffile = new String();// Input file, the LDIF file to be converted....
019: outputfile = new String();
020: if (args.length == 2) {
021: outputfile = args[1];
022: ldiffile = args[0];
023: } else if (args.length == 1) {
024: outputfile = new String("default.ldif");
025: ldiffile = args[0];
026: } else if (args.length == 0) {
027: System.out
028: .println("Input file to be converted not specified!");
029: System.out
030: .println("Invokation Format is:java UsersConvert infile outfile");
031: System.exit(1);
032: }
033:
034: doConvert(ldiffile, outputfile);
035: }
036:
037: public static void doConvert(String ldiffile, String outputfile) {
038: LDIFRecord out;
039: OutputStreamWriter outFile;
040: int count = 0;
041: LDIF l1;
042:
043: try {
044:
045: l1 = new LDIF(ldiffile);
046:
047: //outFile=new FileWriter(outputfile);
048: outFile = new OutputStreamWriter(new FileOutputStream(
049: outputfile), "UTF-8");
050:
051: LDIFRecord tmp = l1.nextRecord();
052:
053: while (tmp != null) {
054: out = ConvertRecord(tmp, count++);
055: try {
056: OutputRecord(outFile, out);
057: } catch (Exception e) {
058: System.out.println("Error writing to output file:"
059: + outputfile);
060: e.printStackTrace();
061: }
062: tmp = l1.nextRecord();
063: }
064: outFile.close();
065: //System.out.println("Processed "+count+" entries");
066: //System.out.println("Output available in file "+outputfile);
067: } catch (IOException e) {
068: System.out.println("Error:" + e.toString());
069: e.printStackTrace();
070: }
071: }
072:
073: static String processNLines(String inName) {
074:
075: String toRet;
076: int index, tmp;
077:
078: index = inName.indexOf("\n");
079: if (index < 0)
080: return inName;
081: toRet = inName.substring(0, index);
082: while (index >= 0 && index < inName.length()) {
083: tmp = inName.indexOf("\n", index + 1);
084: if (tmp != -1)
085: toRet += "\n " + inName.substring(index + 1, tmp);
086: else
087: toRet += "\n " + inName.substring(index + 1);
088: index = tmp;
089: }
090:
091: return toRet;
092:
093: }
094:
095: static LDIFRecord ConvertRecord(LDIFRecord toConvert, int count) {
096: LDIFAttributeContent con, converted;
097: LDAPAttribute tmp;
098: LDAPAttributeSet theAttrSet;
099:
100: MimeBase64Encoder MBE = new MimeBase64Encoder();
101:
102: String attrVal, firstname;
103: String[] allAttrs;
104: String[] listAllAttrs;
105: Vector dispProf = new Vector();
106: String lastname = new String();
107: boolean cnValuetobeSet = false;
108: String dn;
109: firstname = new String();
110:
111: LDAPAttribute[] attrList;
112: int i, typeAttr;
113:
114: ByteBuf tmpBuf;
115:
116: con = (LDIFAttributeContent) toConvert.getContent();
117: converted = new LDIFAttributeContent();
118: attrList = con.getAttributes();
119: theAttrSet = new LDAPAttributeSet(attrList);
120:
121: converted.setControls(con.getControls());
122:
123: for (i = 0; i < attrList.length; ++i) {
124: if ((attrList[i].getName())
125: .equalsIgnoreCase("iwtAuthMembership-password-at")) {
126: allAttrs = (((LDAPAttribute) (theAttrSet
127: .getAttribute(attrList[i].getName())))
128: .getStringValueArray());
129: attrVal = allAttrs[1];
130: tmpBuf = new ByteBuf();
131: tmpBuf.setLength(0);
132: //System.out.println("attrName:"+attrList[i].getName());
133: attrVal = Password.decrypt(attrVal);
134: MBE.translate(new ByteBuf(attrVal), tmpBuf);
135: allAttrs[1] = tmpBuf.toString();
136: //allAttrs[1]=attrVal;
137: converted.addElement(new LDAPAttribute(
138: "iwtAuthMembership-password-at", allAttrs));
139: } else {
140: if ((attrList[i].getName())
141: .equalsIgnoreCase("iwtUser-IMAPPassword-at")) {
142: allAttrs = (((LDAPAttribute) (theAttrSet
143: .getAttribute(attrList[i].getName())))
144: .getStringValueArray());
145: attrVal = allAttrs[1];
146: tmpBuf = new ByteBuf();
147: tmpBuf.setLength(0);
148: //System.out.println("attrName:"+attrList[i].getName());
149: attrVal = Password.decrypt(attrVal);
150: MBE.translate(new ByteBuf(attrVal), tmpBuf);
151: allAttrs[1] = tmpBuf.toString();
152: //allAttrs[1]=attrVal;
153: converted.addElement(new LDAPAttribute(
154: "iwtUser-IMAPPassword-at", allAttrs));
155: } else {
156: if ((attrList[i].getName())
157: .equalsIgnoreCase("iwtNetMail-Signature-at")) {
158: allAttrs = (((LDAPAttribute) (theAttrSet
159: .getAttribute(attrList[i].getName())))
160: .getStringValueArray());
161: attrVal = allAttrs[1];
162: tmpBuf = new ByteBuf();
163: MBE.translate(new ByteBuf(attrVal), tmpBuf);
164: allAttrs[1] = tmpBuf.toString();
165: converted.addElement(new LDAPAttribute(
166: "iwtNetMail-Signature-at", allAttrs));
167: } else {
168:
169: allAttrs = (((LDAPAttribute) (theAttrSet
170: .getAttribute(attrList[i].getName())))
171: .getStringValueArray());
172: for (int kk = 0; kk < allAttrs.length; ++kk) {
173: allAttrs[kk] = processNLines(allAttrs[kk]);
174: }
175:
176: converted.addElement(new LDAPAttribute(
177: attrList[i].getName(), allAttrs));
178: }
179: }
180: }
181: }
182:
183: return new LDIFRecord(toConvert.getDN(), converted);
184: }
185:
186: static void OutputRecord(OutputStreamWriter fw, LDIFRecord toOutput)
187: throws Exception {
188: LDIFAttributeContent con;
189: LDAPAttribute[] list;
190: LDAPAttributeSet theAttrSet;
191: String[] values;
192: String dnVal = new String();
193: boolean snExists = false;
194: boolean cnExists = false;
195:
196: con = (LDIFAttributeContent) toOutput.getContent();
197: list = con.getAttributes();
198: theAttrSet = new LDAPAttributeSet(list);
199: //System.out.println("\ndn:"+toOutput.getDN());
200: //dnVal=processDN(toOutput.getDN());
201: fw.write("\ndn: " + toOutput.getDN());
202: for (int i = 0; i < theAttrSet.size(); ++i) {
203: values = (String[]) (theAttrSet.elementAt(i))
204: .getStringValueArray();
205: for (int j = 0; j < values.length; ++j) {
206: /* System.out.print((theAttrSet.elementAt(i)).getName());
207: System.out.println(":"+values[j]+" "); */
208: fw.write("\n" + (theAttrSet.elementAt(i)).getName());
209: fw.write(": " + values[j]);
210: }
211: if (((theAttrSet.elementAt(i)).getName()).equals("cn"))
212: cnExists = true;
213: if (((theAttrSet.elementAt(i)).getName()).equals("sn"))
214: snExists = true;
215: }
216:
217: fw.write("\n");
218: //System.out.print("\n OUTPUTTING....dn: "+toOutput.getDN()+"\n");
219:
220: return;
221: }
222: }
|