0001: /**
0002: * $Id: NetFileHostDataProcessor.java,v 1.27 2005/11/30 11:26:34 ss150821 Exp $
0003: * Copyright 2002 Sun Microsystems, Inc. All
0004: * rights reserved. Use of this product is subject
0005: * to license terms. Federal Acquisitions:
0006: * Commercial Software -- Government Users
0007: * Subject to Standard License Terms and
0008: * Conditions.
0009: *
0010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
0011: * are trademarks or registered trademarks of Sun Microsystems,
0012: * Inc. in the United States and other countries.
0013: */package com.sun.portal.netfile.servlet.java1;
0014:
0015: import java.net.*;
0016: import com.sun.portal.log.common.PortalLogger;
0017: import java.util.*;
0018: import java.util.logging.*;
0019:
0020: import java.io.InputStream;
0021: import java.io.IOException;
0022: import java.security.AccessController;
0023:
0024: import com.sun.identity.security.DecryptAction;
0025:
0026: import com.iplanet.sso.SSOToken;
0027:
0028: /**
0029: * @author Suresh Yellamaraju
0030: */
0031: public class NetFileHostDataProcessor {
0032: public NetFileLogManager logMgr;
0033: private static Logger logger = PortalLogger
0034: .getLogger(NetFileHostDataProcessor.class);
0035: private char c_eol = '\n';
0036: private String s_empty_string = "";
0037: private List allowedHosts;
0038: private List deniedHosts;
0039: private List listOfHosts;
0040: private List commondata;
0041: private String defaultDomain = s_empty_string;
0042: private Object[] localHostAddrs;
0043: private SSOToken ssoToken;
0044: private NetFileResource nfRes;
0045: private ArrayList invalidHosts;
0046:
0047: public NetFileHostDataProcessor(NetFileLogManager logMgr,
0048: SSOToken token, NetFileResource nfres) {
0049: this .logMgr = logMgr;
0050: ssoToken = token;
0051: nfRes = nfres;
0052: localHostAddrs = getLocalHostNames();
0053: invalidHosts = new ArrayList();
0054: }
0055:
0056: /*
0057: * Process host data which involves host data saved by user,
0058: * common host info by admin and the denied hosts
0059: */
0060: StringBuffer processHostsData(StringBuffer returnbufstr,
0061: NetFileContext nfContext,
0062: NetFileAttributeExtractor nfAttrs,
0063: NetFileResource nfr_user_locale_i18n_bucket)
0064: throws NetFileException {
0065:
0066: List listCommonHosts = null;
0067: loadHostDataSets(nfAttrs);
0068:
0069: InetAddress[] ia_denied_hosts;
0070: NetFileHostDenialProcessor nfhdp = new NetFileHostDenialProcessor();
0071:
0072: if ((deniedHosts != null) && (!deniedHosts.isEmpty()))
0073: ia_denied_hosts = nfhdp
0074: .getDeniedHostsAddresses(deniedHosts);
0075: else
0076: ia_denied_hosts = new InetAddress[0];
0077:
0078: if ((commondata != null) && (!commondata.isEmpty())) {
0079: listCommonHosts = commondata;
0080: }
0081:
0082: ArrayList al_denied_hosts = new ArrayList();//Will contain the names of denied hosts
0083:
0084: try {
0085: if ((listCommonHosts != null)
0086: && (!listCommonHosts.isEmpty())) {
0087: HashMap ht_hosts_shares_set_by_admin = parseCommonHostsData(
0088: listCommonHosts, nfContext,
0089: nfr_user_locale_i18n_bucket);
0090:
0091: Iterator e_machine_type_password_shares = listOfHosts
0092: .iterator();
0093:
0094: /*
0095: *Each user-added host is considered once only
0096: */
0097: while (e_machine_type_password_shares.hasNext()) {
0098: String s_user_added_host_share = (String) (e_machine_type_password_shares
0099: .next());
0100: s_user_added_host_share = getDecryptedHostInfo(s_user_added_host_share);
0101: String s_host_name = getMachineName(s_user_added_host_share);
0102: String szFQHostName = nfhdp.getFQHostName(
0103: s_host_name, this .defaultDomain);
0104:
0105: InetAddress[] hostIAddresses = null;
0106: try {
0107: //hostIAddresses = getInetAddresses(szFQHostName);
0108: hostIAddresses = nfhdp.getHostAddress(
0109: s_host_name, this .defaultDomain);
0110: } catch (java.net.UnknownHostException uhe) {
0111: // logger.log(Level.SEVERE, "Exception in determining the host address", uhe);
0112: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1110");
0113: invalidHosts.add(s_host_name);
0114: continue;
0115: }
0116:
0117: if (isLocalHost(hostIAddresses))
0118: continue;
0119:
0120: if (nfhdp.isHostAllowed(allowedHosts, deniedHosts,
0121: szFQHostName, hostIAddresses, true)) {
0122: if (ia_denied_hosts != null) {
0123: if (nfhdp.isHostDenied(hostIAddresses,
0124: ia_denied_hosts)) {
0125: al_denied_hosts.add(s_host_name);
0126: }
0127: }
0128: } else {
0129: al_denied_hosts.add(s_host_name);
0130: }
0131:
0132: try {
0133: HashMap ht_matching_host = getMatchingHost(
0134: szFQHostName,
0135: ht_hosts_shares_set_by_admin);
0136: if (ht_matching_host != null) {
0137: s_user_added_host_share = mergeHost(
0138: s_user_added_host_share,
0139: ht_matching_host);
0140: removeValue(ht_hosts_shares_set_by_admin,
0141: ht_matching_host);
0142: }
0143: } catch (Exception e) {
0144: // logger.log(Level.SEVERE, "Error in merging hosts",e);
0145: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1111");
0146: }
0147:
0148: returnbufstr.append(
0149: "sunPortalNetFileHostTypePassShare=")
0150: .append(s_user_added_host_share).append(
0151: c_eol);
0152: }
0153: Object[] o_hosts_and_denied_hosts = addUnmergedHosts(
0154: nfhdp, ht_hosts_shares_set_by_admin,
0155: al_denied_hosts, ia_denied_hosts);
0156:
0157: String s_common_hosts_not_added_by_user = (String) (o_hosts_and_denied_hosts[0]);
0158: al_denied_hosts = (ArrayList) (o_hosts_and_denied_hosts[1]);
0159: returnbufstr.append(s_common_hosts_not_added_by_user)
0160: .append(c_eol);
0161:
0162: } else {
0163: if (listOfHosts.isEmpty()) {
0164: } else {
0165: Iterator e_machine_type_password_shares = listOfHosts
0166: .iterator();
0167: while (e_machine_type_password_shares.hasNext()) {
0168: String s_user_added_host_share = (String) (e_machine_type_password_shares
0169: .next());
0170:
0171: s_user_added_host_share = getDecryptedHostInfo(s_user_added_host_share);
0172: String s_host_name = getMachineName(s_user_added_host_share);
0173: String szFQHostName = nfhdp.getFQHostName(
0174: s_host_name, this .defaultDomain);
0175:
0176: InetAddress[] hostIAddresses = null;
0177: try {
0178: //hostIAddresses = getInetAddresses(szFQHostName);
0179: hostIAddresses = nfhdp.getHostAddress(
0180: s_host_name, this .defaultDomain);
0181: } catch (java.net.UnknownHostException uhe) {
0182: // logger.log(Level.SEVERE, "Exception in determining the host address", uhe);
0183: logger.log(Level.SEVERE,
0184: "PSSRNF_CSPNSJ1112");
0185: invalidHosts.add(s_host_name);
0186: continue;
0187: }
0188:
0189: if (isLocalHost(hostIAddresses))
0190: continue;
0191:
0192: if (nfhdp.isHostAllowed(allowedHosts,
0193: deniedHosts, szFQHostName,
0194: hostIAddresses, true)) {
0195: if (ia_denied_hosts != null) {
0196: if (nfhdp.isHostDenied(
0197: s_user_added_host_share,
0198: ia_denied_hosts)) {
0199: al_denied_hosts.add(s_host_name);
0200: }
0201: returnbufstr
0202: .append(
0203: "sunPortalNetFileHostTypePassShare=")
0204: .append(s_user_added_host_share)
0205: .append(c_eol);
0206:
0207: } else {
0208: returnbufstr
0209: .append(
0210: "sunPortalNetFileHostTypePassShare=")
0211: .append(s_user_added_host_share)
0212: .append(c_eol);
0213: }
0214: } else {
0215: returnbufstr
0216: .append(
0217: "sunPortalNetFileHostTypePassShare=")
0218: .append(s_user_added_host_share)
0219: .append(c_eol);
0220: al_denied_hosts.add(s_host_name);
0221: }
0222: }
0223: }
0224: }
0225: } catch (Exception e) {
0226: // logger.log(Level.SEVERE, "Exception extracting common data or host data or in merging them",e);
0227: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1113");
0228: }
0229:
0230: if (ia_denied_hosts != null) {
0231: for (int i = 0; i < al_denied_hosts.size(); ++i) {
0232: returnbufstr
0233: .append("iplanet-srap-netfile-denied-host=")
0234: .append(((String) al_denied_hosts.get(i)))
0235: .append(c_eol);
0236: }
0237: }
0238:
0239: if (!invalidHosts.isEmpty()) {
0240: returnbufstr.append("sunPortalNetFileInvalidHosts="
0241: + invalidHosts.toString());
0242: returnbufstr.append("\n");
0243: }
0244:
0245: return returnbufstr;
0246: }
0247:
0248: void removeValue(HashMap ht_contaier, HashMap ht_contained) {
0249: if (ht_contaier.containsValue(ht_contained)) {
0250: Object key = ht_contained.get("machine_name");
0251: if (key != null) {
0252: Object o = ht_contaier.remove(key);
0253:
0254: // logger.info("Removed " + key);
0255: Object[] params4 = { key };
0256: logger.log(Level.INFO, "PSSRNF_CSPNSJ1114", params4);
0257: }
0258: }
0259: }
0260:
0261: Object[] addUnmergedHosts(NetFileHostDenialProcessor nfhdp,
0262: HashMap ht_hosts_added_by_admin, ArrayList al_denied_hosts,
0263: InetAddress[] ia_denied_hosts) throws NetFileException {
0264:
0265: Collection keys = ht_hosts_added_by_admin.values();
0266: Iterator i_keys = keys.iterator();
0267: StringBuffer sb_host_data = new StringBuffer();
0268:
0269: while (i_keys.hasNext()) {
0270: try {
0271: HashMap ht_admin_added_host = (HashMap) (i_keys.next());
0272: /* Check if any common host is denied as well */
0273: String s_host_name = (String) (ht_admin_added_host
0274: .get("machine_name"));
0275: String szFQHostName = nfhdp.getFQHostName(s_host_name
0276: .trim(), this .defaultDomain);
0277:
0278: InetAddress[] hostIAddresses = null;
0279: try {
0280: hostIAddresses = getInetAddresses(szFQHostName);
0281: } catch (java.net.UnknownHostException uhe) {
0282: // logger.log(Level.SEVERE, "Exception in determining the host address", uhe);
0283: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1115");
0284: invalidHosts.add(s_host_name);
0285: continue;
0286: }
0287:
0288: if (isLocalHost(hostIAddresses))
0289: continue;
0290:
0291: if (nfhdp.isHostAllowed(allowedHosts, deniedHosts,
0292: szFQHostName, hostIAddresses, true)) {
0293: if (nfhdp.isHostDenied(szFQHostName,
0294: ia_denied_hosts)) {
0295: al_denied_hosts.add(s_host_name);
0296: }
0297: } else {
0298: al_denied_hosts.add(s_host_name);
0299: }
0300:
0301: filterWinAdminShares(ht_admin_added_host, null);
0302:
0303: sb_host_data
0304: .append("sunPortalNetFileHostTypePassShare=");
0305: StringBuffer sb_host_name = new StringBuffer();
0306: sb_host_name.append("machine_name=")
0307: .append(s_host_name).append(c_eol);
0308: sb_host_data = mergeAttribute("machine_name",
0309: "machine_name=", ht_admin_added_host,
0310: sb_host_data);
0311: sb_host_data = mergeAttribute("machine_type",
0312: "machine_type=", ht_admin_added_host,
0313: sb_host_data);
0314: sb_host_data = mergeAttribute("machine_encoding",
0315: "machine_encoding=", ht_admin_added_host,
0316: sb_host_data);
0317: sb_host_data = mergeAttribute("machine_domain",
0318: "machine_domain=", ht_admin_added_host,
0319: sb_host_data);
0320: sb_host_data = mergeAttribute("machine_password",
0321: "machine_password=", ht_admin_added_host,
0322: sb_host_data);
0323: sb_host_data = mergeAttribute("machine_user_name",
0324: "machine_user_name=", ht_admin_added_host,
0325: sb_host_data);
0326:
0327: if (ht_admin_added_host.get("shares") == null) {
0328: sb_host_data.append(c_eol).append(c_eol);
0329: continue;
0330: }
0331: if (ht_admin_added_host.get("share_passwords") == null) {
0332: sb_host_data.append(c_eol).append(c_eol);
0333: continue;
0334: }
0335:
0336: ArrayList v_common_shares = (ArrayList) ht_admin_added_host
0337: .get("shares");
0338: ArrayList v_share_passwords = (ArrayList) ht_admin_added_host
0339: .get("share_passwords");
0340: if ((!v_common_shares.isEmpty())
0341: || (!v_share_passwords.isEmpty())) {
0342: int i_num_shares = v_common_shares.size();
0343: for (int i = 0; i < i_num_shares; ++i) {
0344: sb_host_data.append("share_name=").append(
0345: (String) v_common_shares.get(i))
0346: .append(c_eol)
0347: .append("share_password=").append(
0348: (String) v_share_passwords
0349: .get(i)).append(c_eol);
0350: }
0351: }
0352: sb_host_data.append(c_eol).append(c_eol);
0353: } catch (Exception e) {
0354: // logger.log(Level.SEVERE, "Exception in common hosts unmerged with the hosts added by the user",e);
0355: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1116");
0356: }
0357: }
0358: return new Object[] { sb_host_data.toString(), al_denied_hosts };
0359: }
0360:
0361: StringBuffer mergeAttribute(String s_attribute_name,
0362: String attribute_tag, HashMap ht_host_data,
0363: StringBuffer sb_host_data) {
0364: Object o_temp = ht_host_data.get(s_attribute_name);
0365: String s_temp = s_empty_string;
0366: if (o_temp == null) {
0367: } else {
0368: s_temp = (String) o_temp;
0369: }
0370: sb_host_data.append(attribute_tag).append(s_temp).append(c_eol);
0371: return sb_host_data;
0372: }
0373:
0374: InetAddress[] getAllInetAddresses(String s_host_name)
0375: throws Exception {
0376: return InetAddress.getAllByName(s_host_name.trim());
0377: }
0378:
0379: HashMap parseCommonHostsData(List listCommonHosts,
0380: NetFileContext nfContext,
0381: NetFileResource nfr_user_locale_i18n_bucket) {
0382:
0383: /* Parse each host and fill up the hash maps */
0384: int i_common_hosts = listCommonHosts.size();
0385: HashMap ht_common_hosts_data = new HashMap(i_common_hosts * 2);
0386: for (int i = 0; i < i_common_hosts; ++i) {
0387: /* If there is any problem in parsing the host we skip it and move on*/
0388: try {
0389: String s_common_host_data = (String) listCommonHosts
0390: .get(i);
0391: HashMap ht_s_common_host_data = parseHost(
0392: s_common_host_data, "\n");
0393: if (ht_s_common_host_data != null) {
0394: Object o_host_name = ht_s_common_host_data
0395: .get("machine_name");
0396: if (o_host_name != null) {
0397: String s_host_name = (String) o_host_name;
0398: ht_common_hosts_data.put(s_host_name,
0399: ht_s_common_host_data);
0400: }
0401: }
0402: } catch (NetFileException e) {
0403:
0404: // logger.info("Unable to parse common host data"
0405: logger
0406: .info("Unable to parse common host data"
0407: + ((NetFileException) e)
0408: .getMessage(nfr_user_locale_i18n_bucket));
0409: } catch (Throwable e) {
0410: // logger.log(Level.SEVERE, "Unable to parse common host data",e);
0411: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1118");
0412:
0413: }
0414: }
0415: return ht_common_hosts_data;
0416: }
0417:
0418: String mergeHost(String s_user_added_host_share,
0419: HashMap ht_matching_common_host) throws NetFileException {
0420: HashMap ht_user_added_host = parseHost(s_user_added_host_share,
0421: "\n");
0422: filterWinAdminShares(ht_matching_common_host,
0423: ht_user_added_host);
0424:
0425: StringBuffer sb_host_data = new StringBuffer();
0426: sb_host_data.append("machine_name=").append(
0427: ht_user_added_host.get("machine_name")).append(c_eol);
0428:
0429: /*If machine types are different donot add the common shares*/
0430: boolean b_are_types_different = areTypeDifferent(
0431: ht_matching_common_host, ht_user_added_host);
0432: sb_host_data = mergeAttribute(sb_host_data, "machine_type",
0433: "machine_type", ht_matching_common_host,
0434: ht_user_added_host);
0435: sb_host_data = mergeAttribute(sb_host_data, "machine_encoding",
0436: "machine_encoding", ht_matching_common_host,
0437: ht_user_added_host);
0438: sb_host_data = mergeAttribute(sb_host_data, "machine_domain",
0439: "machine_domain", ht_matching_common_host,
0440: ht_user_added_host);
0441: sb_host_data = mergeAttribute(sb_host_data, "machine_password",
0442: "machine_password", ht_matching_common_host,
0443: ht_user_added_host);
0444: sb_host_data = mergeAttribute(sb_host_data,
0445: "machine_user_name", "machine_user_name",
0446: ht_matching_common_host, ht_user_added_host);
0447: ArrayList v_shares = (ArrayList) ht_user_added_host
0448: .get("shares");
0449: ArrayList v_passwords = (ArrayList) ht_user_added_host
0450: .get("share_passwords");
0451: ArrayList v_common_shares = (ArrayList) ht_matching_common_host
0452: .get("shares");
0453: ArrayList v_common_share_passwords = (ArrayList) ht_matching_common_host
0454: .get("share_passwords");
0455:
0456: if (!b_are_types_different) {
0457: ArrayList[] v_share_data = mergeShares(v_shares,
0458: v_passwords, v_common_shares,
0459: v_common_share_passwords);
0460: v_shares = v_share_data[0];
0461: v_passwords = v_share_data[1];
0462: }
0463: int i_num_shares = v_shares.size();
0464: for (int i = 0; i < i_num_shares; ++i) {
0465: sb_host_data.append("share_name=").append(
0466: (String) v_shares.get(i)).append(c_eol).append(
0467: "share_password=").append(
0468: (String) v_passwords.get(i)).append(c_eol);
0469: }
0470: sb_host_data.append(c_eol);
0471: return sb_host_data.toString();
0472: }
0473:
0474: boolean areTypeDifferent(HashMap ht_matching_common_host,
0475: HashMap ht_user_added_host) {
0476: Object o_common_type = ht_matching_common_host
0477: .get("machine_type");
0478: if (o_common_type == null) {
0479: return false;
0480: }
0481: return !(((String) ht_user_added_host.get("machine_type"))
0482: .equals((String) o_common_type));
0483: }
0484:
0485: ArrayList[] mergeShares(ArrayList v_shares, ArrayList v_passwords,
0486: ArrayList v_common_shares,
0487: ArrayList v_common_share_passwords) {
0488: Iterator e_shares = v_shares.iterator();
0489: while (e_shares.hasNext()) {
0490: String s_share = (String) e_shares.next();
0491: if (v_common_shares.contains(s_share)) {
0492: v_common_shares.remove(s_share);
0493: }
0494: }
0495: Iterator e_common_shares = v_common_shares.iterator();
0496: Iterator e_common_share_passwords = v_common_share_passwords
0497: .iterator();
0498: while (e_common_shares.hasNext()) {
0499: v_shares.add(e_common_shares.next());
0500: if (e_common_share_passwords.hasNext()) {
0501: v_passwords.add((String) e_common_share_passwords
0502: .next());
0503: } else {
0504: v_passwords.add(s_empty_string);
0505: }
0506: }
0507: return new ArrayList[] { v_shares, v_passwords };
0508: }
0509:
0510: StringBuffer mergeAttribute(StringBuffer sb_host_data,
0511: String attribute_name1, String attribute_name2,
0512: HashMap ht_matching_common_host, HashMap ht_user_added_host) {
0513: sb_host_data.append(attribute_name1).append("=");
0514: Object o_temp = ht_user_added_host.get(attribute_name1);
0515: if (o_temp == null) {
0516: o_temp = ht_matching_common_host.get(attribute_name2);
0517: }
0518: if (o_temp != null) {
0519: sb_host_data.append((String) o_temp);
0520: }
0521: sb_host_data.append(c_eol);
0522: return sb_host_data;
0523: }
0524:
0525: HashMap parseHost(String s_user_added_host_share, String separator)
0526: throws NetFileException {
0527: StringTokenizer st_tokens = new StringTokenizer(
0528: s_user_added_host_share, separator);
0529: HashMap ht_host = new HashMap(st_tokens.countTokens() * 2);
0530: try {
0531: ArrayList v_shares = new ArrayList();
0532: ArrayList v_share_passwords = new ArrayList();
0533: ht_host.put("shares", v_shares);
0534: ht_host.put("share_passwords", v_share_passwords);
0535: while (st_tokens.hasMoreTokens()) {
0536: String token = st_tokens.nextToken();
0537: if (token.startsWith("machine_name=")) {
0538: String s_name = removePrefix("machine_name=", token);
0539: s_name = removeCtrlMChar(s_name);
0540: ht_host.put("machine_name", s_name);
0541: InetAddress[] ia_common_host = getAllInetAddresses(s_name);
0542: ht_host.put("all_inet_addresses", ia_common_host);
0543: } else if (token.startsWith("machine_type=")) {
0544: ht_host.put("machine_type",
0545: removeCtrlMChar(removePrefix(
0546: "machine_type=", token)));
0547: } else if (token.startsWith("machine_encoding=")) {
0548: ht_host.put("machine_encoding",
0549: removeCtrlMChar(removePrefix(
0550: "machine_encoding=", token)));
0551: } else if (token.startsWith("machine_domain=")) {
0552: ht_host.put("machine_domain",
0553: removeCtrlMChar(removePrefix(
0554: "machine_domain=", token)));
0555: } else if (token.startsWith("machine_password=")) {
0556: ht_host.put("machine_password",
0557: removeCtrlMChar(removePrefix(
0558: "machine_password=", token)));
0559: } else if (token.startsWith("machine_user_name=")) {
0560: ht_host.put("machine_user_name",
0561: removeCtrlMChar(removePrefix(
0562: "machine_user_name=", token)));
0563: } else if (token.startsWith("share_name=")) {
0564: v_shares.add(removeCtrlMChar(removePrefix(
0565: "share_name=", token)));
0566: String s_password = st_tokens.nextToken();
0567: v_share_passwords.add(removeCtrlMChar(removePrefix(
0568: "share_password=", s_password)));
0569: }
0570: }
0571: } catch (Exception e) {
0572: // logger.log(Level.SEVERE, "Unable to parse host data",e);
0573: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1119");
0574: throw new NetFileException(new String[] {
0575: NetFileException.KEY_IDENTIFIER_PREFIX
0576: + "illegal_machine_data_format",
0577: s_user_added_host_share });
0578: }
0579: return ht_host;
0580: }
0581:
0582: String removePrefix(String prefix, String string)
0583: throws NetFileException {
0584: if (!string.startsWith(prefix)) {
0585: throw new NetFileException(
0586: NetFileException.KEY_IDENTIFIER_PREFIX
0587: + "illegal_machine_data_format");
0588: }
0589: return string.substring(prefix.length(), string.length());
0590: }
0591:
0592: HashMap getMatchingHost(String s_host_added_by_user,
0593: HashMap ht_host_shares_set_by_admin) {
0594: try {
0595: InetAddress ia_host_added_by_user = InetAddress
0596: .getByName(s_host_added_by_user);
0597: Collection c_keys = ht_host_shares_set_by_admin.values();
0598: Iterator i_keys = c_keys.iterator();
0599: while (i_keys.hasNext()) {
0600: HashMap ht_host = (HashMap) (i_keys.next());
0601: Object o_inet_addresses = ht_host
0602: .get("all_inet_addresses");
0603: if (o_inet_addresses == null) {
0604: return null;
0605: } else {
0606: InetAddress[] ia_host = (InetAddress[]) o_inet_addresses;
0607: if (ia_host[0].equals(ia_host_added_by_user)) {
0608: // logger.info("Got matching host");
0609: logger.info("PSSRNF_CSPNSJ1120");
0610: return ht_host;
0611: }
0612: }
0613: }
0614: } catch (Exception e) {
0615: // logger.log(Level.SEVERE, "Exception while finding host matching a given host among common hosts by IP",e);
0616: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1121");
0617: return null;
0618: }
0619: return null;
0620: }
0621:
0622: private String getMachineName(String s_user_added_host_share)
0623: throws Exception {
0624: String s_machine_name = null;
0625: if (s_user_added_host_share == null) {
0626: throw new NullPointerException("null host information");
0627: } else {
0628: int i_index_of_name = s_user_added_host_share
0629: .indexOf("machine_name=");
0630: if (i_index_of_name < -1) {
0631: throw new Exception(
0632: "Invalid host data - does not contain machine name");
0633: } else {
0634: i_index_of_name = "machine_name=".length();
0635: int i_index_of_eol = s_user_added_host_share.indexOf(
0636: c_eol, i_index_of_name);
0637: if (i_index_of_eol <= i_index_of_name) {
0638: throw new Exception(
0639: "Invalid host data - machine name does not end in \\n");
0640: }
0641: s_machine_name = s_user_added_host_share.substring(
0642: i_index_of_name, i_index_of_eol);
0643: }
0644: }
0645: return s_machine_name;
0646: }
0647:
0648: /*
0649: * Obtains the allowed hosts List
0650: */
0651: List getAllowedHostsList(NetFileAttributeExtractor nfAttrs) {
0652: return nfAttrs.getStringList("sunPortalNetFileAllowedHostList");
0653: }
0654:
0655: /*
0656: * Obtains the denied hosts List
0657: */
0658: List getDeniedHostsList(NetFileAttributeExtractor nfAttrs) {
0659: return nfAttrs.getStringList("sunPortalNetFileDeniedHostList");
0660: }
0661:
0662: /*
0663: * Obtains the common hosts as a List
0664: */
0665: List getCommonHosts(NetFileAttributeExtractor nfAttrs) {
0666: ArrayList decryptedNetFileHosts = null;
0667: List encryptedHosts = nfAttrs
0668: .getStringList("sunPortalNetFileCommonHostData");
0669: if (encryptedHosts != null) {
0670: if (!encryptedHosts.isEmpty()) {
0671: decryptedNetFileHosts = new ArrayList();
0672: Iterator hosts = encryptedHosts.iterator();
0673: while (hosts.hasNext()) {
0674: decryptedNetFileHosts
0675: .add(getDecryptedHostInfo((String) hosts
0676: .next()));
0677: }
0678: }
0679: }
0680: return decryptedNetFileHosts;
0681: }
0682:
0683: /*
0684: * Obtains the user-added hosts' list
0685: */
0686: List getUserAddedHostsList(NetFileAttributeExtractor nfAttrs) {
0687: return nfAttrs
0688: .getStringList("sunPortalNetFileHostTypePassShare");
0689: }
0690:
0691: /*
0692: * Return the decrypted host information
0693: */
0694: String getDecryptedHostInfo(String szEncryptedHostInfo) {
0695: return (String) AccessController
0696: .doPrivileged(new DecryptAction(szEncryptedHostInfo));
0697: }
0698:
0699: /*
0700: * Returns the given host's InetAddress's as an array.
0701: * Throws UnknownHostException if host not known
0702: */
0703: InetAddress[] getInetAddresses(String szHost)
0704: throws java.net.UnknownHostException {
0705: return InetAddress.getAllByName(szHost);
0706: }
0707:
0708: /*
0709: * Sets the default domain value
0710: */
0711: void setDefaultDomain(String szDefaultDomain) {
0712: this .defaultDomain = szDefaultDomain;
0713: }
0714:
0715: /*
0716: * Loads the denied hosts, allowed hosts, common hosts and
0717: * user-added hosts into the corresponding instance variables
0718: */
0719: void loadHostDataSets(NetFileAttributeExtractor nfAttrs) {
0720: /*
0721: * Obtain the denied hosts
0722: */
0723: deniedHosts = getDeniedHostsList(nfAttrs);
0724: // logger.info("Denied hosts are "+ deniedHosts);
0725: Object[] params12 = { deniedHosts };
0726: logger.log(Level.INFO, "PSSRNF_CSPNSJ1122", params12);
0727: /*
0728: * Obtain the allowed hosts
0729: */
0730: allowedHosts = getAllowedHostsList(nfAttrs);
0731: // logger.info("Allowed hosts are " + allowedHosts);
0732: Object[] params13 = { allowedHosts };
0733: logger.log(Level.INFO, "PSSRNF_CSPNSJ1123", params13);
0734:
0735: /*
0736: * Obtain the common hosts data
0737: */
0738: commondata = getCommonHosts(nfAttrs);
0739:
0740: /*
0741: * Obtain the user-added hosts list
0742: */
0743: listOfHosts = getUserAddedHostsList(nfAttrs);
0744: }
0745:
0746: /*
0747: * Verifies if the host is a local host. Returns
0748: * true if so or if the given host cannot be verified.
0749: */
0750: boolean isLocalHost(InetAddress[] hostIAddresses) {
0751:
0752: if ((localHostAddrs == null) || (localHostAddrs.length < 1))
0753: return true;
0754:
0755: // now see if there's a match
0756: for (int i = 0; i < hostIAddresses.length; i++) {
0757: for (int k = 0; k < localHostAddrs.length; k++) {
0758: if (((String) localHostAddrs[k]).trim().equals(
0759: hostIAddresses[i].getHostAddress().trim())) {
0760: // logger.info(hostIAddresses[i].getHostName() + " is a local host");
0761: Object[] params14 = { " is a local host" };
0762: logger.log(Level.INFO, "PSSRNF_CSPNSJ1124",
0763: params14);
0764: return true;
0765: }
0766: }
0767: }
0768: return false;
0769: }
0770:
0771: /*Object[] getRPaddrs() {
0772:
0773: // read equivalent attributes for platform.conf and get the server names
0774: // from profile service
0775: ArrayList rpHostIP = new ArrayList();
0776: String configparam = "";
0777: Runtime rt;
0778:
0779: try {
0780: rt = Runtime.getRuntime();
0781: int c;
0782: StringBuffer buftxt = new StringBuffer(80);
0783: String[] progarr = new String[2];
0784:
0785: progarr[0] = "/sbin/ifconfig";
0786: progarr[1] = "-a";
0787:
0788: Process shellconn = rt.exec(progarr);
0789: InputStream shellout = shellconn.getInputStream();
0790: while ((c = shellout.read()) > -1) {
0791: char chtxt = ((char) c);
0792: buftxt.append(chtxt);
0793: }
0794: rt = null;
0795: shellout.close();
0796: shellout = null;
0797: shellconn.destroy();
0798: shellconn = null;
0799: configparam = buftxt.toString();
0800: } catch (IOException ex) {
0801: // logger.log(Level.SEVERE, "FileOption IOException in getrpaddrs: " , ex);
0802: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1125");
0803: rpHostIP.add("Failed Execution: " + ex);
0804: rt = null;
0805: return rpHostIP.toArray();
0806: }
0807:
0808: if (configparam.equals("")) {
0809: // logger.info("Cannot get local machine hostname.");
0810: logger.info("PSSRNF_CSPNSJ1126");
0811: rpHostIP.add("Cannot get local machine hostname.");
0812: return rpHostIP.toArray();
0813: } else {
0814: StringTokenizer pars = new StringTokenizer(configparam, "\n");
0815: int parscnt = pars.countTokens();
0816: String[] hostip = new String[100];
0817: int inetindx = 0;
0818: int inetmaskindx = 0;
0819: for (int j = 0; j < parscnt; j++) {
0820: hostip[j] = pars.nextToken();
0821: inetindx = hostip[j].indexOf("inet", 0);
0822: inetmaskindx = hostip[j].indexOf("netmask", 0);
0823: if ((inetindx >= 0) && (inetmaskindx >= 0)) {
0824: rpHostIP.add(hostip[j]
0825: .substring(inetindx + 5, inetmaskindx));
0826: }
0827: }
0828: }
0829: return rpHostIP.toArray();
0830: }*/
0831:
0832: public static Object[] getLocalHostNames() {
0833: ArrayList hostIP = new ArrayList();
0834: String configparam = "";
0835: try {
0836: java.util.Enumeration infList = java.net.NetworkInterface
0837: .getNetworkInterfaces();
0838: java.util.Enumeration iAddList = null;
0839: java.net.NetworkInterface nInf = null;
0840: java.net.InetAddress iAdd = null;
0841:
0842: while (infList.hasMoreElements()) {
0843: nInf = (java.net.NetworkInterface) infList
0844: .nextElement();
0845: iAddList = nInf.getInetAddresses();
0846: while (iAddList.hasMoreElements()) {
0847: iAdd = (java.net.InetAddress) iAddList
0848: .nextElement();
0849: hostIP.add(iAdd.getHostAddress());
0850: }
0851: }
0852: } catch (Exception e) {
0853: }
0854: return hostIP.toArray();
0855: }
0856:
0857: /*
0858: * Uses the given common host and user-saved host (given set of hosts).
0859: * For the given set of hosts, verifies if the host type in each match.
0860: * If they match, then it verifies if the host is windows type (NT or WIN).
0861: * So, if there is a match, then the host information inside the common host
0862: * is changed.
0863: *
0864: * Verifies for each $ share specified in the common host list, if the user has access.
0865: * The credentials used for this are from the common host information.
0866: *
0867: * If there is a host in the common host list, then verifies for each $ share
0868: * specified in the common host list if the user has access. The credentials
0869: * used in this case are from the user-saved host information.
0870: */
0871:
0872: private void filterWinAdminShares(HashMap commonHost,
0873: HashMap userSavedHost) throws NetFileException {
0874: Object oSharesList = (ArrayList) commonHost.remove("shares");
0875: Object oSharePwdsList = (ArrayList) commonHost
0876: .remove("share_passwords");
0877:
0878: if (oSharesList == null)
0879: return;
0880: if (oSharePwdsList == null)
0881: return;
0882:
0883: ArrayList sharesList = (ArrayList) oSharesList;
0884: ArrayList sharePwdsList = (ArrayList) oSharePwdsList;
0885:
0886: if (sharesList.isEmpty())
0887: return;
0888: if (sharePwdsList.isEmpty())
0889: return;
0890:
0891: Iterator shares = sharesList.iterator();
0892: Iterator pwds = sharePwdsList.iterator();
0893:
0894: if (userSavedHost == null) {
0895: if (isWinType((String) commonHost.get("machine_type"))) {
0896: verifySharesAccess((String) commonHost
0897: .get("machine_user_name"), (String) commonHost
0898: .get("machine_password"), (String) commonHost
0899: .get("machine_name"), (String) commonHost
0900: .get("machine_domain"), (String) commonHost
0901: .get("machine_encoding"), shares, pwds);
0902: }
0903: } else if (isSameType((String) commonHost.get("machine_type"),
0904: (String) userSavedHost.get("machine_type"))) {
0905: verifySharesAccess((String) userSavedHost
0906: .get("machine_user_name"), (String) userSavedHost
0907: .get("machine_password"), (String) userSavedHost
0908: .get("machine_name"), (String) commonHost
0909: .get("machine_domain"), (String) commonHost
0910: .get("machine_encoding"), shares, pwds);
0911: }
0912: commonHost.put("shares", sharesList);
0913: commonHost.put("share_passwords", sharePwdsList);
0914: }
0915:
0916: /*
0917: * Verifies for the given list of shares, if the given $-share is accessible.
0918: * If not accessible, then the share and its corresponding share password
0919: * is removed from the respective lists
0920: */
0921:
0922: private void verifySharesAccess(String userName, String machPwd,
0923: String machineName, String domain, String encoding,
0924: Iterator shares, Iterator pwds) throws NetFileException {
0925:
0926: String actualEncoding = encoding.substring(encoding
0927: .indexOf("(") + 1, encoding.indexOf(")"));
0928: WinFile winFile = new WinFile(this .logMgr, actualEncoding,
0929: this .ssoToken);
0930:
0931: while (shares.hasNext() && pwds.hasNext()) {
0932: String share = (String) shares.next();
0933: if (share.trim().endsWith("$")) {
0934: boolean accessible = isShareAccessible(winFile,
0935: userName, machineName, domain, share,
0936: (String) pwds.next());
0937: if (!accessible) {
0938: shares.remove();
0939: pwds.remove();
0940: } else {
0941: // logger.info("Access to share - " + share + " is allowed to user " + userName);
0942: Object[] params17 = { share,
0943: " is allowed to user ", userName };
0944: logger.log(Level.INFO, "PSSRNF_CSPNSJ1127",
0945: params17);
0946: }
0947: } else {
0948: pwds.next();
0949: }
0950: }
0951: }
0952:
0953: /*
0954: * Calls the isShareAccessible for a given share to verify if it is accessible
0955: */
0956:
0957: private boolean isShareAccessible(WinFile winFile, String userName,
0958: String machineName, String domain, String shareName,
0959: String sharePwd) {
0960: return winFile.isShareAccessible(userName, sharePwd,
0961: machineName, shareName, domain, nfRes);
0962: }
0963:
0964: /*
0965: * Verifies for the given types, if they are same in a case insensitive way.
0966: * Also, if the types match then a further check is made to see if the type
0967: * is WIN or NT in a case-insensitive way.
0968: *
0969: * Returns true, if types match and type is WIN or NT. Returns false
0970: * otherwise.
0971: */
0972:
0973: private boolean isSameType(String commonHostType,
0974: String userSavedHostType) {
0975: if (commonHostType.equalsIgnoreCase(userSavedHostType))
0976: return isWinType(commonHostType);
0977: return false;
0978: }
0979:
0980: /*
0981: * Verifies if the given type is WIN or NT in a case-insensitive way.
0982: * Returns true if so. Returns false otherwise.
0983: */
0984:
0985: private boolean isWinType(String type) {
0986: if ((type.equalsIgnoreCase("WIN"))
0987: || (type.equalsIgnoreCase("NT")))
0988: return true;
0989: return false;
0990: }
0991:
0992: /*
0993: * Removes \n and \r characters. If neither exists, then the string is
0994: * returned as it is.
0995: *
0996: * Called by parseHost to remove these characters for common hosts. If called
0997: * for other hosts (user-added), then the strings are returned as they were
0998: * given.
0999: */
1000: private String removeCtrlMChar(String szValue) {
1001: int i = szValue.lastIndexOf('\n');
1002: int j = szValue.lastIndexOf('\r');
1003: if ((i < 0) && (j < 0))
1004: return szValue;
1005: else if ((j < 0) && (i >= 0))
1006: return szValue.substring(0, i);
1007: else
1008: return szValue.substring(0, j);
1009: }
1010:
1011: }
|