001: package com.sun.portal.search.rdmgr;
002:
003: import java.util.*;
004: import java.io.*;
005: import sun.misc.BASE64Decoder;
006: import netscape.ldap.*;
007: import netscape.ldap.util.*;
008:
009: public class PIPMigrate {
010: static public void main(String args[]) throws Exception {
011: String out_file = "tmp/PKMigrate.ldif";
012:
013: if (args.length != 1 && args.length != 6) {
014: Usage();
015: System.exit(1);
016: }
017: out_file = args[0];
018:
019: BufferedReader sie = null;
020: try {
021: sie = new BufferedReader(new FileReader("config/sie.conf"));
022: } catch (Exception e) {
023: System.out.println("Error to open config/sie.conf: " + e);
024: System.out
025: .println("Make sure to run in the server directory where Compass PK installed.");
026: return;
027: }
028: BufferedReader dbswitch = null;
029: try {
030: dbswitch = new BufferedReader(new FileReader(
031: "../userdb/dbswitch.conf"));
032: } catch (Exception e) {
033: System.out
034: .println("Error to open ../userdb/dbswitch.conf: "
035: + e);
036: System.out
037: .println("Make sure to run in the server directory where Compass PK installed.");
038: return;
039: }
040:
041: String ServerEntry = null;
042: String host = null;
043: int port = -1;
044: String basedn = null;
045: String mgr_dn = null;
046: String mgr_pw = null;
047:
048: String buf = sie.readLine();
049: try {
050: while (buf != null) {
051: buf = buf.trim();
052: if (buf.startsWith("ServerEntry=")) {
053: ServerEntry = buf.substring(12);
054: break;
055: }
056: buf = sie.readLine();
057: }
058: sie.close();
059: if (ServerEntry == null)
060: throw new Exception("No ServerEntry Found");
061: } catch (Exception e) {
062: System.out.println("Failed to read config/sie.conf: " + e);
063: return;
064: }
065:
066: boolean local_ldap = false;
067:
068: buf = dbswitch.readLine();
069: try {
070: while (buf != null) {
071: buf = buf.trim();
072: if (buf.startsWith("default:binddn ")) {
073: mgr_dn = buf.substring(15);
074: } else if (buf.startsWith("default:encoded bindpw ")) {
075: mgr_pw = buf.substring(23);
076: BASE64Decoder d = new BASE64Decoder();
077: mgr_pw = new String(d.decodeBuffer(mgr_pw));
078: } else if (buf.startsWith("directory default ")) {
079: buf = buf.substring(18);
080: try {
081: LDAPUrl u = new LDAPUrl(buf);
082: host = u.getHost();
083: port = u.getPort();
084: basedn = u.getDN();
085: } catch (Exception e) {
086: // it is ldapdb local db
087: mgr_pw = mgr_dn = host = "";
088: port = 0;
089: local_ldap = true;
090: }
091: }
092: buf = dbswitch.readLine();
093: }
094: dbswitch.close();
095: if (mgr_pw == null)
096: throw new Exception("No 'default:encoded bindpw' Found");
097: if (mgr_dn == null)
098: throw new Exception("No 'default:binddn' Found");
099: if (host == null || port == -1)
100: throw new Exception("No 'directory default' Found");
101: } catch (Exception e) {
102: System.out
103: .println("Failed to read ../userdb/dbswitch.conf: "
104: + e);
105: return;
106: }
107:
108: if (local_ldap)
109: System.out
110: .println("ldapdb, expecting input file: ../userdb/301b.ldif");
111: else
112: System.out.println(mgr_dn + ":" + mgr_pw + "@" + host + ":"
113: + port + "-" + basedn);
114:
115: System.out.println(ServerEntry);
116:
117: PrintWriter out = null;
118: try {
119: out = new PrintWriter(new BufferedWriter(new FileWriter(
120: out_file)));
121: // for older ldapmodify, don't put comment
122: //out.println("# Compass Migrate ldif file.");
123: //out.println("# Import with ldapmodify after inspection.");
124: } catch (Exception e) {
125: System.err.println("Error to open output " + out_file
126: + ": " + e);
127: return;
128: }
129:
130: LDAPConnection dsame = null;
131: String dsame_base = "";
132: if (args.length == 6) {
133: try {
134: dsame = new LDAPConnection();
135: String dsame_host = args[1];
136: int dsame_port = Integer.parseInt(args[2]);
137: String dsame_mgr_dn = args[3];
138: String dsame_mgr_pw = args[4];
139: dsame_base = args[5];
140:
141: dsame.connect(dsame_host, dsame_port);
142: dsame.authenticate(dsame_mgr_dn, dsame_mgr_pw);
143: } catch (LDAPException e) {
144: System.out.println("Error for new ldap: "
145: + e.toString());
146: return;
147: }
148: }
149:
150: if (local_ldap) {
151:
152: try {
153: // Parse the LDIF file test.ldif.
154: LDIF parser = new LDIF("../userdb/301b.ldif");
155: // Iterate through each LDIF record in the file.
156: LDIFRecord nextRec = parser.nextRecord();
157: while (nextRec != null) {
158: // Based on the type of content in the record,
159: // get the content and cast it as the appropriate
160: // type.
161: int ldif_type = nextRec.getContent().getType();
162: if (ldif_type != LDIFContent.ATTRIBUTE_CONTENT)
163: continue;
164: LDIFAttributeContent attrContent = (LDIFAttributeContent) nextRec
165: .getContent();
166: String dn = nextRec.getDN();
167:
168: if (dn.length() > ServerEntry.length()
169: && dn.endsWith(ServerEntry)
170: && dn.startsWith("pipuid=")) {
171:
172: LDAPAttributeSet findAttrs = new LDAPAttributeSet(
173: attrContent.getAttributes());
174:
175: String uid = findPIPUID(findAttrs, dn);
176: if (uid == null)
177: continue;
178:
179: String newUserDN = getUserDN(uid, dsame,
180: dsame_base);
181: if (newUserDN == null) {
182: // just the uid, admin need to adjust
183: newUserDN = uid;
184: }
185: handleEntry(findAttrs, out, newUserDN);
186:
187: }
188:
189: nextRec = parser.nextRecord();
190: }
191: } catch (IOException e) {
192: System.out.println("Error for old ldif file: "
193: + e.toString());
194: return;
195: }
196: } else { /* ! if (local_ldap) */
197: LDAPConnection ld = null;
198: LDAPEntry findEntry = null;
199:
200: LDIF parser = null;
201:
202: try {
203: ld = new LDAPConnection();
204: /* Connect to server */
205: ld.connect(host, port);
206:
207: /* Authenticate to the server as directory manager */
208: ld.authenticate(mgr_dn, mgr_pw);
209:
210: /* search for all entries with surname of Jensen */
211: String MY_FILTER = "pipuid=*";
212:
213: LDAPSearchConstraints cons = ld.getSearchConstraints();
214: /* Setting the batchSize to one will cause the result
215: enumeration below to block on one result at a time,
216: allowing us to update a list or do other things as
217: results come in. */
218: /* We could set it to 0 if we just wanted to get all
219: results and were willing to block until then */
220: cons.setBatchSize(0);
221: cons.setMaxResults(0);
222: LDAPSearchResults res = ld.search(ServerEntry,
223: LDAPConnection.SCOPE_SUB, // one_level might be quicker
224: MY_FILTER, null, false, cons);
225:
226: /* Loop on results until finished */
227: while (res.hasMoreElements()) {
228: /* Next directory entry */
229: try {
230: findEntry = res.next();
231:
232: /* Get the attributes of the entry */
233: LDAPAttributeSet findAttrs = findEntry
234: .getAttributeSet();
235: String dn = findEntry.getDN();
236:
237: String uid = findPIPUID(findAttrs, dn);
238: if (uid == null)
239: continue;
240:
241: /* check whether the pip entry is valid */
242: String userdn = getUserDN(uid, ld, basedn);
243: if (userdn == null) {
244: System.out.println("Error get uid: " + uid);
245: continue;
246: }
247:
248: String newUserDN = getUserDN(uid, dsame,
249: dsame_base);
250: if (newUserDN == null) {
251: // just the uid, admin need to adjust
252: newUserDN = userdn;
253: }
254: handleEntry(findAttrs, out, newUserDN);
255:
256: } catch (LDAPReferralException e) {
257: System.out.println("Search reference: ");
258: LDAPUrl refUrls[] = e.getURLs();
259: for (int i = 0; i < refUrls.length; i++) {
260: System.out.println("\t"
261: + refUrls[i].getUrl());
262: }
263: continue;
264: } catch (LDAPException e) {
265: System.out.println("Error: " + e.toString());
266: continue;
267: }
268: }
269: } catch (LDAPException e) {
270: System.out.println("Error for old ldap: "
271: + e.toString());
272: }
273:
274: /* Done, so disconnect */
275: if ((ld != null) && ld.isConnected()) {
276: try {
277: ld.disconnect();
278: } catch (LDAPException e) {
279: System.out.println("Error: " + e.toString());
280: }
281: }
282: } /* if (local_ldap) */
283:
284: if ((dsame != null) && dsame.isConnected()) {
285: try {
286: dsame.disconnect();
287: } catch (LDAPException e) {
288: System.out.println("Error: " + e.toString());
289: }
290: }
291: out.close();
292: }
293:
294: static String getFirstValue(LDAPAttribute anAttr) {
295: if (anAttr == null)
296: return null;
297: Enumeration enumVals = anAttr.getStringValues();
298: if (enumVals != null) {
299: if (enumVals.hasMoreElements()) {
300: return (String) enumVals.nextElement();
301: }
302: }
303: return null;
304: }
305:
306: static int handleEntry(LDAPAttributeSet findAttrs, PrintWriter out,
307: String userdn) {
308: LDAPAttribute temp = null;
309:
310: out.println("dn: " + userdn);
311: out.println("changetype: modify");
312: out.println("add: objectClass");
313: out.println("objectClass: sunPortalPKSubscriptionsPerson");
314:
315: String timestamp = null;
316: String maxhits = "3";
317: int count = 0;
318: long frequency = 0;
319:
320: try {
321: temp = findAttrs.getAttribute("pipstatus");
322: if (getFirstValue(temp).equals("0")) // disabled
323: return 0;
324: temp = findAttrs.getAttribute("piptimestamp");
325: timestamp = getFirstValue(temp);
326: temp = findAttrs.getAttribute("pipmaxhits");
327: maxhits = getFirstValue(temp);
328: temp = findAttrs.getAttribute("pipidstcount");
329: count = Integer.parseInt(getFirstValue(temp));
330: temp = findAttrs.getAttribute("pipfrequency"); // hourly
331: frequency = Integer.parseInt(getFirstValue(temp));
332: if (frequency < 3)
333: frequency = 7; // hourly, daily, weekly -> weekly
334: else if (frequency == 3)
335: frequency = 30; // monthly
336: else
337: frequency = 365; // yearly
338:
339: } catch (Exception e) { // don't care, use default
340: }
341:
342: // write something XXX
343: // out.println("maxhits: " + maxhits);
344:
345: if (count < 1)
346: return 0;
347:
348: temp = findAttrs.getAttribute("pipstquery");
349: if (temp == null) // no query at all despite the count
350: return 0;
351: Enumeration queries = temp.getStringValues();
352: if (queries == null) // no query at all despite the count
353: return 0;
354:
355: Enumeration interestes = null;
356: temp = findAttrs.getAttribute("pipstinterest");
357: if (temp != null)
358: interestes = temp.getStringValues();
359:
360: Enumeration st_status = null;
361: temp = findAttrs.getAttribute("pipststatus");
362: if (temp != null)
363: st_status = temp.getStringValues();
364:
365: Enumeration names = null;
366: temp = findAttrs.getAttribute("pipstname");
367: if (temp != null)
368: names = temp.getStringValues();
369:
370: String buf;
371: int i = 1;
372: while (queries.hasMoreElements()) {
373: if (i > count)
374: break; // mismatched
375: String name;
376: if (names.hasMoreElements()) {
377: name = (String) names.nextElement();
378: name = name.substring(name.indexOf(' ') + 1);
379: // XXX urlencode or base64encoded
380: } else
381: name = "Query" + i;
382:
383: int interest = 0;
384: if (interestes.hasMoreElements()) {
385: try {
386: buf = (String) interestes.nextElement();
387: buf = buf.substring(buf.indexOf(' ') + 1);
388: interest = Integer.parseInt(buf);
389: if (interest > 3 || interest < 0)
390: interest = 0;
391: } catch (Exception e) {
392: }
393: }
394:
395: String st_enable;
396: if (st_status.hasMoreElements()) {
397: st_enable = (String) st_status.nextElement();
398: st_enable = st_enable
399: .substring(st_enable.indexOf(' ') + 1);
400: } else
401: st_enable = "1";
402: // ???
403:
404: String query = (String) queries.nextElement();
405: query = query.substring(query.indexOf(' ') + 1);
406: String new_entry = name + "|" + query;
407: if (name.equals("_sendall"))
408: new_entry = "All|( rd-rating > " + interest + ")";
409: else if (interest != 0)
410: new_entry += " and ( rd-rating > " + interest + ")";
411: new_entry += "|" + frequency;
412:
413: /* " and ( rd-last-changed > " + timestamp + ")"; */
414: out.println("sunPortalSavedSearch: " + new_entry);
415:
416: i += 1;
417: }
418: out.println("");
419:
420: /*
421: temp = findAttrs.getAttribute("pipresultset"); // ???
422: temp = findAttrs.getAttribute("pipsortorder"); // ???
423:
424: // not supported prior or any more
425: temp = findAttrs.getAttribute("pipnotify"); // new alert
426: temp = findAttrs.getAttribute("piphour"); // not used
427: temp = findAttrs.getAttribute("pipusertype"); // lite or pro
428: temp = findAttrs.getAttribute("pipmedium"); // mail pwp
429: temp = findAttrs.getAttribute("pipformat"); // mime text
430: temp = findAttrs.getAttribute("pipirlist"); // resource
431: temp = findAttrs.getAttribute("pipiroption"); // resource
432: temp = findAttrs.getAttribute("pippwp"); // pwp name
433: temp = findAttrs.getAttribute("piplastcount"); // static
434: temp = findAttrs.getAttribute("piptotalcount");
435: temp = findAttrs.getAttribute("piptotalrun");
436: temp = findAttrs.getAttribute("pipstlastcount"); // static
437: temp = findAttrs.getAttribute("pipsttotalcount");
438: temp = findAttrs.getAttribute("pipsttotalrun");
439: temp = findAttrs.getAttribute("pipstid"); // no more
440: temp = findAttrs.getAttribute("pipstprivacy"); // public?
441: temp = findAttrs.getAttribute("pipsttype"); // cat. or free
442: temp = findAttrs.getAttribute("pipsttaxonomy"); // csid
443: */
444: return 1;
445: }
446:
447: static String getUserDN(String uid, LDAPConnection ld, String basedn) {
448: if (ld == null)
449: return null;
450:
451: String user_filter = "uid=" + uid;
452: try {
453: LDAPSearchResults u_res = ld.search(basedn,
454: LDAPConnection.SCOPE_SUB, user_filter, null, false);
455: LDAPEntry u_entry = u_res.next();
456: if (u_entry == null) {
457: return null;
458: } else {
459: String userdn = u_entry.getDN();
460: return userdn;
461: }
462: } catch (Exception e) {
463: return null;
464: }
465: }
466:
467: static String findPIPUID(LDAPAttributeSet findAttrs, String dn) {
468: LDAPAttribute temp = null;
469: temp = findAttrs.getAttribute("pipuid");
470: if (temp == null) {
471: System.out.println("Error to get pipuid: " + dn);
472: return null;
473: }
474:
475: String uid = getFirstValue(temp);
476: if (uid == null) {
477: System.out.println("Error get null pipuid: " + dn);
478: return null;
479: }
480: return uid;
481: }
482:
483: static void Usage() {
484: System.out.println("PIPMigrate outfile");
485: System.out
486: .println("PIPMigrate outfile host port binddn pw basedn");
487: }
488: }
|