Beispiel #1
0
 /** Idempotent. */
 private void init() throws CollectException {
   if (!applicable) {
     String errmsg = JOVALMsg.getMessage(JOVALMsg.STATUS_XINETD_NOCONFIG, CONFIG);
     throw new CollectException(errmsg, FlagEnumeration.NOT_APPLICABLE);
   } else if (services != null) {
     return;
   }
   services = new HashMap<String, Service>();
   try {
     IFilesystem fs = session.getFilesystem();
     IFile config = fs.getFile(CONFIG, IFile.Flags.NOCACHE);
     if (config.exists()) {
       try {
         parseConfigFile(config);
       } catch (IOException e) {
         session.getLogger().warn(JOVALMsg.ERROR_XINETD_FILE, config.getPath(), e.getMessage());
       } catch (IllegalArgumentException e) {
         session.getLogger().warn(JOVALMsg.ERROR_XINETD_FILE, config.getPath(), e.getMessage());
       }
     } else {
       applicable = false;
       String errmsg = JOVALMsg.getMessage(JOVALMsg.STATUS_XINETD_NOCONFIG, CONFIG);
       throw new CollectException(errmsg, FlagEnumeration.NOT_APPLICABLE);
     }
   } catch (IOException e) {
     session.getLogger().warn(JOVALMsg.getMessage(Message.ERROR_IO, CONFIG, e.getMessage()));
   }
 }
Beispiel #2
0
 public void writeXML(File f) {
   OutputStream out = null;
   try {
     Marshaller marshaller = ctx.createMarshaller();
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
     out = new FileOutputStream(f);
     marshaller.marshal(getOvalVariables(), out);
   } catch (JAXBException e) {
     logger.warn(JOVALMsg.ERROR_FILE_GENERATE, f.toString());
     logger.warn(JOVALMsg.getMessage(JOVALMsg.ERROR_EXCEPTION), e);
   } catch (FactoryConfigurationError e) {
     logger.warn(JOVALMsg.ERROR_FILE_GENERATE, f.toString());
     logger.warn(JOVALMsg.getMessage(JOVALMsg.ERROR_EXCEPTION), e);
   } catch (FileNotFoundException e) {
     logger.warn(JOVALMsg.ERROR_FILE_GENERATE, f.toString());
     logger.warn(JOVALMsg.getMessage(JOVALMsg.ERROR_EXCEPTION), e);
   } finally {
     if (out != null) {
       try {
         out.close();
       } catch (IOException e) {
         logger.warn(JOVALMsg.ERROR_FILE_CLOSE, f.toString());
       }
     }
   }
 }
Beispiel #3
0
  public Collection<FilesetItem> getItems(ObjectType obj, IRequestContext rc)
      throws CollectException {
    FilesetObject fObj = (FilesetObject) obj;
    Collection<FilesetItem> items = new Vector<FilesetItem>();
    switch (fObj.getFlstinst().getOperation()) {
      case EQUALS:
        try {
          String fileset = SafeCLI.checkArgument((String) fObj.getFlstinst().getValue(), session);
          session.getLogger().trace(JOVALMsg.STATUS_AIX_FILESET, fileset);
          for (String line :
              SafeCLI.multiLine("lslpp -lac '" + fileset + "'", session, IUnixSession.Timeout.M)) {
            if (!line.startsWith("#")) {
              List<String> info = StringTools.toList(StringTools.tokenize(line, ":"));
              if (info.get(0).equals("/etc/objrepos")) {
                FilesetItem item = Factories.sc.aix.createFilesetItem();

                EntityItemStringType flstinst = Factories.sc.core.createEntityItemStringType();
                flstinst.setValue(info.get(1));
                item.setFlstinst(flstinst);

                EntityItemVersionType level = Factories.sc.core.createEntityItemVersionType();
                level.setValue(info.get(2));
                level.setDatatype(SimpleDatatypeEnumeration.VERSION.value());
                item.setLevel(level);

                EntityItemFilesetStateType state =
                    Factories.sc.aix.createEntityItemFilesetStateType();
                state.setValue(info.get(4));
                item.setState(state);

                EntityItemStringType description = Factories.sc.core.createEntityItemStringType();
                description.setValue(info.get(6));
                item.setDescription(description);

                items.add(item);
              }
            }
          }
        } catch (Exception e) {
          MessageType msg = Factories.common.createMessageType();
          msg.setLevel(MessageLevelEnumeration.ERROR);
          msg.setValue(e.getMessage());
          rc.addMessage(msg);
          session.getLogger().warn(JOVALMsg.getMessage(JOVALMsg.ERROR_EXCEPTION), e);
        }
        break;

      default:
        {
          String msg =
              JOVALMsg.getMessage(
                  JOVALMsg.ERROR_UNSUPPORTED_OPERATION, fObj.getFlstinst().getOperation());
          throw new CollectException(msg, FlagEnumeration.NOT_COLLECTED);
        }
    }

    return items;
  }
Beispiel #4
0
 /** Create empty Variables. */
 Variables() {
   logger = JOVALMsg.getLogger();
   variables = new Hashtable<String, List<IType>>();
   comments = new Hashtable<String, String>();
   try {
     ctx = JAXBContext.newInstance(SchemaRegistry.lookup(SchemaRegistry.OVAL_VARIABLES));
   } catch (JAXBException e) {
     logger.error(JOVALMsg.getMessage(JOVALMsg.ERROR_EXCEPTION), e);
   }
 }
Beispiel #5
0
 /** Create a Results based on the specified Definitions and SystemCharacteristics. */
 public Results(IDefinitions definitions, ISystemCharacteristics sc) {
   this.definitions = definitions;
   this.sc = sc;
   logger = JOVALMsg.getLogger();
   definitionTable = new Hashtable<String, DefinitionType>();
   testTable = new Hashtable<String, TestType>();
   directives = new Directives();
   try {
     ctx = JAXBContext.newInstance(SchemaRegistry.lookup(SchemaRegistry.OVAL_RESULTS));
   } catch (JAXBException e) {
     logger.error(JOVALMsg.getMessage(JOVALMsg.ERROR_EXCEPTION), e);
   }
 }
Beispiel #6
0
 /** Transform using the specified template, and serialize to the specified file. */
 public void writeTransform(File transform, File output) {
   try {
     TransformerFactory xf = TransformerFactory.newInstance();
     Transformer transformer = xf.newTransformer(new StreamSource(new FileInputStream(transform)));
     transformer.transform(getSource(), new StreamResult(output));
   } catch (JAXBException e) {
     logger.warn(JOVALMsg.getMessage(JOVALMsg.ERROR_EXCEPTION), e);
   } catch (OvalException e) {
     logger.warn(JOVALMsg.getMessage(JOVALMsg.ERROR_EXCEPTION), e);
   } catch (FileNotFoundException e) {
     logger.warn(JOVALMsg.ERROR_FILE_GENERATE, output);
   } catch (TransformerConfigurationException e) {
     logger.warn(JOVALMsg.ERROR_FILE_GENERATE, output);
   } catch (TransformerException e) {
     logger.warn(JOVALMsg.ERROR_FILE_GENERATE, output);
   }
 }
Beispiel #7
0
 public List<IType> getValue(String id) throws NoSuchElementException {
   List<IType> values = variables.get(id);
   if (values == null) {
     throw new NoSuchElementException(JOVALMsg.getMessage(JOVALMsg.ERROR_EXTERNAL_VARIABLE, id));
   } else {
     return values;
   }
 }
  private void init() throws CollectException {
    try {
      //
      // Get a runspace if there are any in the pool, or create a new one, and load the
      // Get-SystemMetrics
      // Powershell module code.
      //
      IRunspace runspace = null;
      IWindowsSession.View view = session.getNativeView();
      for (IRunspace rs : session.getRunspacePool().enumerate()) {
        if (rs.getView() == view) {
          runspace = rs;
          break;
        }
      }
      if (runspace == null) {
        runspace = session.getRunspacePool().spawn(view);
      }
      runspace.loadAssembly(getClass().getResourceAsStream("Systemmetric.dll"));
      runspace.loadModule(getClass().getResourceAsStream("Systemmetric.psm1"));

      //
      // Run the Get-SystemMetrics module and parse the output
      //
      metrics = new HashMap<String, SystemmetricItem>();
      for (String line : runspace.invoke("Get-SystemMetrics").split("\n")) {
        int ptr = line.indexOf(":");
        String key = null, val = null;
        if (ptr > 0) {
          key = line.substring(0, ptr).trim();
          val = line.substring(ptr + 1).trim();
          try {
            EntityItemSystemMetricIndexType index =
                Factories.sc.windows.createEntityItemSystemMetricIndexType();
            index.setDatatype(SimpleDatatypeEnumeration.STRING.value());
            index.setValue(key);

            EntityItemIntType value = Factories.sc.core.createEntityItemIntType();
            value.setDatatype(SimpleDatatypeEnumeration.INT.value());
            value.setValue(new Integer(val).toString());

            SystemmetricItem item = Factories.sc.windows.createSystemmetricItem();
            item.setIndex(index);
            item.setValue(value);
            metrics.put(key, item);
          } catch (IllegalArgumentException e) {
            session.getLogger().warn(JOVALMsg.ERROR_WIN_SYSTEMMETRIC_VALUE, e.getMessage(), key);
          }
        }
      }
    } catch (Exception e) {
      session.getLogger().warn(JOVALMsg.getMessage(JOVALMsg.ERROR_EXCEPTION), e);
      error = new CollectException(e.getMessage(), FlagEnumeration.ERROR);
      throw error;
    }
  }
Beispiel #9
0
 public static final OvalResults getOvalResults(File f) throws OvalException {
   try {
     String packages = SchemaRegistry.lookup(SchemaRegistry.OVAL_RESULTS);
     JAXBContext ctx = JAXBContext.newInstance(packages);
     Unmarshaller unmarshaller = ctx.createUnmarshaller();
     Object rootObj = unmarshaller.unmarshal(f);
     if (rootObj instanceof OvalResults) {
       return (OvalResults) rootObj;
     } else {
       throw new OvalException(JOVALMsg.getMessage(JOVALMsg.ERROR_RESULTS_BAD_SOURCE, f));
     }
   } catch (JAXBException e) {
     throw new OvalException(e);
   }
 }
Beispiel #10
0
 public static final OvalVariables getOvalVariables(Source source) throws OvalException {
   try {
     JAXBContext ctx =
         JAXBContext.newInstance(SchemaRegistry.lookup(SchemaRegistry.OVAL_VARIABLES));
     Unmarshaller unmarshaller = ctx.createUnmarshaller();
     Object rootObj = unmarshaller.unmarshal(source);
     if (rootObj instanceof OvalVariables) {
       return (OvalVariables) rootObj;
     } else {
       throw new OvalException(
           JOVALMsg.getMessage(JOVALMsg.ERROR_VARIABLES_BAD_SOURCE, source.getSystemId()));
     }
   } catch (JAXBException e) {
     throw new OvalException(e);
   }
 }
Beispiel #11
0
  public Collection<? extends ItemType> getItems(ObjectType obj, IRequestContext rc)
      throws CollectException {
    if (error != null) {
      throw error;
    } else if (metrics == null) {
      init();
    }
    Collection<SystemmetricItem> items = new ArrayList<SystemmetricItem>();
    SystemmetricObject sObj = (SystemmetricObject) obj;
    EntityObjectSystemMetricIndexType index = sObj.getIndex();
    OperationEnumeration op = index.getOperation();
    String s = (String) index.getValue();
    switch (op) {
      case EQUALS:
        if (metrics.containsKey(s)) {
          items.add(metrics.get(s));
        }
        break;

      case CASE_INSENSITIVE_EQUALS:
        for (Map.Entry<String, SystemmetricItem> entry : metrics.entrySet()) {
          if (s.equalsIgnoreCase(entry.getKey())) {
            items.add(entry.getValue());
          }
        }
        break;

      case NOT_EQUAL:
        for (Map.Entry<String, SystemmetricItem> entry : metrics.entrySet()) {
          if (!s.equals(entry.getKey())) {
            items.add(entry.getValue());
          }
        }
        break;

      default:
        String msg = JOVALMsg.getMessage(JOVALMsg.ERROR_UNSUPPORTED_OPERATION, op);
        throw new CollectException(msg, FlagEnumeration.NOT_COLLECTED);
    }
    return items;
  }
Beispiel #12
0
  public Collection<PatchItem> getItems(ObjectType obj, IRequestContext rc)
      throws CollectException {
    if (!initialized) {
      scanRevisions();
    }

    PatchObject pObj = (PatchObject) obj;
    Collection<PatchItem> items = new ArrayList<PatchItem>();
    if (error != null) {
      MessageType msg = Factories.common.createMessageType();
      msg.setLevel(MessageLevelEnumeration.ERROR);
      msg.setValue(error);
      rc.addMessage(msg);
    }

    try {
      int iBase = Integer.parseInt((String) pObj.getBase().getValue());

      switch (pObj.getBase().getOperation()) {
        case EQUALS:
          items.addAll(getItems((String) pObj.getBase().getValue()));
          break;

        case NOT_EQUAL:
          for (String base : revisions.keySet()) {
            if (!base.equals((String) pObj.getBase().getValue())) {
              items.addAll(getItems(base));
            }
          }
          break;

        case LESS_THAN:
          for (String base : revisions.keySet()) {
            if (Integer.parseInt(base) < iBase) {
              items.addAll(getItems(base));
            }
          }
          break;

        case LESS_THAN_OR_EQUAL:
          for (String base : revisions.keySet()) {
            if (Integer.parseInt(base) <= iBase) {
              items.addAll(getItems(base));
            }
          }
          break;

        case GREATER_THAN:
          for (String base : revisions.keySet()) {
            if (Integer.parseInt(base) > iBase) {
              items.addAll(getItems(base));
            }
          }
          break;

        case GREATER_THAN_OR_EQUAL:
          for (String base : revisions.keySet()) {
            if (Integer.parseInt(base) >= iBase) {
              items.addAll(getItems(base));
            }
          }
          break;

        case PATTERN_MATCH:
          Pattern p = StringTools.pattern((String) pObj.getBase().getValue());
          for (String base : revisions.keySet()) {
            if (p.matcher(base).find()) {
              items.addAll(getItems(base));
            }
          }
          break;

        default:
          String msg =
              JOVALMsg.getMessage(
                  JOVALMsg.ERROR_UNSUPPORTED_OPERATION, pObj.getBase().getOperation());
          throw new CollectException(msg, FlagEnumeration.NOT_COLLECTED);
      }
    } catch (NumberFormatException e) {
      MessageType msg = Factories.common.createMessageType();
      msg.setLevel(MessageLevelEnumeration.ERROR);
      msg.setValue(e.getMessage());
      rc.addMessage(msg);
      session.getLogger().warn(JOVALMsg.getMessage(JOVALMsg.ERROR_EXCEPTION), e);
    } catch (PatternSyntaxException e) {
      MessageType msg = Factories.common.createMessageType();
      msg.setLevel(MessageLevelEnumeration.ERROR);
      msg.setValue(JOVALMsg.getMessage(JOVALMsg.ERROR_PATTERN, e.getMessage()));
      rc.addMessage(msg);
      session.getLogger().warn(JOVALMsg.getMessage(JOVALMsg.ERROR_EXCEPTION), e);
    }

    return items;
  }
Beispiel #13
0
  /** REMIND: Stops if it encounters any exceptions at all; make this more robust? */
  protected void scanRevisions() {
    try {
      for (String line :
          SafeCLI.multiLine("/usr/bin/showrev -p", session, IUnixSession.Timeout.M)) {
        if (!line.startsWith(PATCH)) {
          continue;
        }
        StringTokenizer tok = null;
        String buff = null;
        int begin, end;

        begin = line.indexOf(PATCH) + PATCH.length();
        end = line.indexOf(OBSOLETES);
        buff = line.substring(begin, end).trim();
        PatchEntry patch = new PatchEntry(buff);

        begin = line.indexOf(OBSOLETES) + OBSOLETES.length();
        end = line.indexOf(REQUIRES);
        buff = line.substring(begin, end).trim();
        ArrayList<PatchEntry> obsoletes = new ArrayList<PatchEntry>();
        tok = new StringTokenizer(buff, ",");
        while (tok.hasMoreTokens()) {
          PatchEntry superceded = new PatchEntry(tok.nextToken().trim());
          obsoletes.add(superceded);
          String obsoleteBase = superceded.getBaseString();
          Collection<SupercedenceEntry> list = supercedence.get(obsoleteBase);
          if (list == null) {
            list = new ArrayList<SupercedenceEntry>();
            supercedence.put(obsoleteBase, list);
          }
          SupercedenceEntry entry = new SupercedenceEntry(superceded, patch);
          if (!list.contains(entry)) {
            list.add(entry);
          }
        }

        begin = line.indexOf(REQUIRES) + REQUIRES.length();
        end = line.indexOf(INCOMPATIBLES);
        buff = line.substring(begin, end).trim();
        ArrayList<PatchEntry> requires = new ArrayList<PatchEntry>();
        tok = new StringTokenizer(buff, ",");
        while (tok.hasMoreTokens()) {
          requires.add(new PatchEntry(tok.nextToken().trim()));
        }

        begin = line.indexOf(INCOMPATIBLES) + INCOMPATIBLES.length();
        end = line.indexOf(PACKAGES);
        buff = line.substring(begin, end).trim();
        ArrayList<PatchEntry> incompatibles = new ArrayList<PatchEntry>();
        tok = new StringTokenizer(buff, ",");
        while (tok.hasMoreTokens()) {
          incompatibles.add(new PatchEntry(tok.nextToken().trim()));
        }

        begin = line.indexOf(PACKAGES) + PACKAGES.length();
        buff = line.substring(begin).trim();
        ArrayList<String> packages = new ArrayList<String>();
        tok = new StringTokenizer(buff, ",");
        while (tok.hasMoreTokens()) {
          packages.add(tok.nextToken().trim());
        }

        RevisionEntry entry =
            new RevisionEntry(patch, obsoletes, requires, incompatibles, packages);
        if (revisions.containsKey(patch.getBaseString())) {
          revisions.get(patch.getBaseString()).add(entry);
        } else {
          Collection<RevisionEntry> list = new ArrayList<RevisionEntry>();
          list.add(entry);
          revisions.put(patch.getBaseString(), list);
        }
      }
    } catch (Exception e) {
      error = e.getMessage();
      session.getLogger().error(JOVALMsg.getMessage(JOVALMsg.ERROR_EXCEPTION), e);
    }
    initialized = true;
  }
Beispiel #14
0
  public Collection<XinetdItem> getItems(ObjectType obj, IRequestContext rc)
      throws CollectException {
    init();
    Collection<XinetdItem> items = new ArrayList<XinetdItem>();
    List<Service> list = new ArrayList<Service>();
    XinetdObject xObj = (XinetdObject) obj;
    try {
      String protocol = (String) xObj.getProtocol().getValue();
      OperationEnumeration op = xObj.getProtocol().getOperation();
      Pattern p = null;
      if (op == OperationEnumeration.PATTERN_MATCH) {
        p = StringTools.pattern(protocol);
      }
      for (Service service : services.values()) {
        String serviceProtocol = "";
        try {
          serviceProtocol = service.getString(Property.PROTOCOL);
        } catch (NoSuchElementException e) {
        }
        switch (op) {
          case EQUALS:
            if (protocol.equals(serviceProtocol)) {
              list.add(service);
            }
            break;
          case CASE_INSENSITIVE_EQUALS:
            if (protocol.equalsIgnoreCase(serviceProtocol)) {
              list.add(service);
            }
            break;
          case NOT_EQUAL:
            if (!protocol.equals(serviceProtocol)) {
              list.add(service);
            }
            break;
          case PATTERN_MATCH:
            if (p.matcher(serviceProtocol).find()) {
              list.add(service);
            }
            break;
          default:
            String msg = JOVALMsg.getMessage(JOVALMsg.ERROR_UNSUPPORTED_OPERATION, op);
            throw new CollectException(msg, FlagEnumeration.NOT_COLLECTED);
        }
      }

      String name = (String) xObj.getServiceName().getValue();
      op = xObj.getServiceName().getOperation();
      if (op == OperationEnumeration.PATTERN_MATCH) {
        p = StringTools.pattern(name);
      }
      for (Service service : list) {
        String serviceName = service.getName();
        switch (op) {
          case EQUALS:
            if (name.equals(serviceName)) {
              items.add(makeItem(service));
            }
            break;
          case CASE_INSENSITIVE_EQUALS:
            if (name.equalsIgnoreCase(serviceName)) {
              items.add(makeItem(service));
            }
            break;
          case NOT_EQUAL:
            if (!name.equals(serviceName)) {
              items.add(makeItem(service));
            }
            break;
          case PATTERN_MATCH:
            if (p.matcher(serviceName).find()) {
              items.add(makeItem(service));
            }
            break;
          default:
            String msg = JOVALMsg.getMessage(JOVALMsg.ERROR_UNSUPPORTED_OPERATION, op);
            throw new CollectException(msg, FlagEnumeration.NOT_COLLECTED);
        }
      }
    } catch (PatternSyntaxException e) {
      MessageType msg = Factories.common.createMessageType();
      msg.setLevel(MessageLevelEnumeration.ERROR);
      msg.setValue(JOVALMsg.getMessage(JOVALMsg.ERROR_PATTERN, e.getMessage()));
      rc.addMessage(msg);
      session.getLogger().warn(JOVALMsg.getMessage(JOVALMsg.ERROR_EXCEPTION), e);
    }
    return items;
  }
Beispiel #15
0
  /**
   * Parses properties between { and }. Leaves the reader open. Throws an exception if no brackets
   * were found.
   */
  private HashMap<String, List<String>> parseConfigBlock(BufferedReader reader)
      throws IOException, IllegalArgumentException {
    String line = null;
    boolean open = false;
    HashMap<String, List<String>> props = null;
    while ((line = reader.readLine()) != null) {
      line = line.trim();
      if (line.length() == 0 || line.startsWith("#")) {
        continue;
      } else {
        int ptr = line.indexOf("#");
        if (ptr > 0) {
          line = line.substring(0, ptr); // strip any trailing comment
        }
        if (!open && line.equals("{")) {
          props = new HashMap<String, List<String>>();
          if (defaults != null) {
            for (Map.Entry<String, List<String>> entry : defaults.entrySet()) {
              List<String> val = new ArrayList<String>();
              val.addAll(entry.getValue());
              props.put(entry.getKey(), val);
            }
          }
          open = true;
        } else if (open && line.equals("}")) {
          break;
        } else if (open) {
          int eq = line.indexOf("=");
          int pe = line.indexOf("+=");
          int me = line.indexOf("-=");

          if (pe > 0) {
            String key = line.substring(0, pe).trim();
            StringTokenizer tok = new StringTokenizer(line.substring(pe + 2).trim());
            List<String> list = new ArrayList<String>(tok.countTokens());
            while (tok.hasMoreTokens()) {
              list.add(tok.nextToken());
            }
            if (!props.containsKey(key)) {
              props.put(key, new ArrayList<String>());
            }
            props.get(key).addAll(list);
          } else if (me > 0) {
            String key = line.substring(0, me).trim();
            StringTokenizer tok = new StringTokenizer(line.substring(me + 2).trim());
            List<String> list = new ArrayList<String>(tok.countTokens());
            while (tok.hasMoreTokens()) {
              list.add(tok.nextToken());
            }
            if (props.containsKey(key)) {
              for (String val : list) {
                props.get(key).remove(val);
              }
            }
          } else if (eq > 0) {
            String key = line.substring(0, eq).trim();
            StringTokenizer tok = new StringTokenizer(line.substring(eq + 1).trim());
            List<String> list = new ArrayList<String>(tok.countTokens());
            while (tok.hasMoreTokens()) {
              list.add(tok.nextToken());
            }
            props.put(key, list);
          }
        }
      }
    }
    if (props == null) {
      throw new IllegalArgumentException(JOVALMsg.getMessage(JOVALMsg.ERROR_XINETD_FORMAT));
    }
    return props;
  }
Beispiel #16
0
  /**
   * Read the Portable Execution format header information from the IFileEx, or directly from the
   * file itself, and convey it to the item.
   */
  private void addHeaderInfo(FileObject fObj, IFile file, FileItem item) throws IOException {
    session.getLogger().trace(JOVALMsg.STATUS_PE_READ, file.toString());
    IFileEx extended = file.getExtended();
    Map<String, String> peHeaders = null;
    if (file.isFile() && file.length() == 0) {
      MessageType msg = Factories.common.createMessageType();
      msg.setLevel(MessageLevelEnumeration.INFO);
      msg.setValue(JOVALMsg.getMessage(JOVALMsg.STATUS_PE_EMPTY));
      item.getMessage().add(msg);
    } else if (extended instanceof IWindowsFileInfo) {
      peHeaders = ((IWindowsFileInfo) extended).getPEHeaders();
    } else if (file.isFile()) {
      try {
        Header header = new Header(file);
        peHeaders = new HashMap<String, String>();
        String cs = Integer.toString(header.getNTHeader().getImageOptionalHeader().getChecksum());
        peHeaders.put(IWindowsFileInfo.PE_MS_CHECKSUM, cs);
        String key = VsVersionInfo.LANGID_KEY;
        VsVersionInfo versionInfo = header.getVersionInfo();
        VsFixedFileInfo value = null;
        if (versionInfo != null) {
          value = versionInfo.getValue();
          key = versionInfo.getDefaultTranslation();
        }
        if (value != null) {
          String version = versionInfo.getValue().getFileVersion().toString();
          peHeaders.put(IWindowsFileInfo.PE_VERSION, version);
          StringTokenizer tok = new StringTokenizer(version, ".");
          if (tok.countTokens() == 4) {
            peHeaders.put(IWindowsFileInfo.PE_VERSION_MAJOR_PART, tok.nextToken());
            peHeaders.put(IWindowsFileInfo.PE_VERSION_MINOR_PART, tok.nextToken());
            peHeaders.put(IWindowsFileInfo.PE_VERSION_BUILD_PART, tok.nextToken());
            peHeaders.put(IWindowsFileInfo.PE_VERSION_PRIVATE_PART, tok.nextToken());
          }
          String locale = LanguageConstants.getLocaleString(key);
          if (locale != null) {
            peHeaders.put(IWindowsFileInfo.PE_LANGUAGE, LanguageConstants.getLocaleString(key));
          }
          Map<String, String> stringTable = versionInfo.getStringTable(key);
          if (stringTable.containsKey("CompanyName")) {
            peHeaders.put(IWindowsFileInfo.PE_COMPANY_NAME, stringTable.get("CompanyName"));
          }
          if (stringTable.containsKey("InternalName")) {
            peHeaders.put(IWindowsFileInfo.PE_INTERNAL_NAME, stringTable.get("InternalName"));
          }
          if (stringTable.containsKey("ProductName")) {
            peHeaders.put(IWindowsFileInfo.PE_PRODUCT_NAME, stringTable.get("ProductName"));
          }
          if (stringTable.containsKey("OriginalFilename")) {
            peHeaders.put(IWindowsFileInfo.PE_ORIGINAL_NAME, stringTable.get("OriginalFilename"));
          }
          if (stringTable.containsKey("ProductVersion")) {
            peHeaders.put(IWindowsFileInfo.PE_PRODUCT_VERSION, stringTable.get("ProductVersion"));
          }
        }
      } catch (IllegalArgumentException e) {
        MessageType msg = Factories.common.createMessageType();
        msg.setLevel(MessageLevelEnumeration.ERROR);
        msg.setValue(e.getMessage());
        item.getMessage().add(msg);
      }
    }

    EntityItemStringType msChecksumType = Factories.sc.core.createEntityItemStringType();
    if (peHeaders != null && peHeaders.containsKey(IWindowsFileInfo.PE_MS_CHECKSUM)) {
      msChecksumType.setValue(peHeaders.get(IWindowsFileInfo.PE_MS_CHECKSUM));
    } else {
      msChecksumType.setStatus(StatusEnumeration.DOES_NOT_EXIST);
    }
    item.setMsChecksum(msChecksumType);

    EntityItemStringType languageType = Factories.sc.core.createEntityItemStringType();
    if (peHeaders != null && peHeaders.containsKey(IWindowsFileInfo.PE_LANGUAGE)) {
      languageType.setValue(peHeaders.get(IWindowsFileInfo.PE_LANGUAGE));
    } else {
      languageType.setStatus(StatusEnumeration.DOES_NOT_EXIST);
    }
    item.setLanguage(languageType);

    EntityItemVersionType versionType = Factories.sc.core.createEntityItemVersionType();
    versionType.setDatatype(SimpleDatatypeEnumeration.VERSION.value());
    if (peHeaders != null && peHeaders.containsKey(IWindowsFileInfo.PE_VERSION_MAJOR_PART)) {
      int major = Integer.parseInt(peHeaders.get(IWindowsFileInfo.PE_VERSION_MAJOR_PART));
      int minor = 0, build = 0, priv = 0;
      if (peHeaders.containsKey(IWindowsFileInfo.PE_VERSION_MINOR_PART)) {
        minor = Integer.parseInt(peHeaders.get(IWindowsFileInfo.PE_VERSION_MINOR_PART));
      }
      if (peHeaders.containsKey(IWindowsFileInfo.PE_VERSION_BUILD_PART)) {
        build = Integer.parseInt(peHeaders.get(IWindowsFileInfo.PE_VERSION_BUILD_PART));
      }
      if (peHeaders.containsKey(IWindowsFileInfo.PE_VERSION_PRIVATE_PART)) {
        priv = Integer.parseInt(peHeaders.get(IWindowsFileInfo.PE_VERSION_PRIVATE_PART));
      }
      if (major == 0 && minor == 0 && build == 0 && priv == 0) {
        versionType.setStatus(StatusEnumeration.DOES_NOT_EXIST);
      } else {
        versionType.setValue(new Version(major, minor, build, priv).toString());
      }
    } else {
      versionType.setStatus(StatusEnumeration.DOES_NOT_EXIST);
    }
    item.setFileVersion(versionType);

    EntityItemStringType companyType = Factories.sc.core.createEntityItemStringType();
    if (peHeaders != null && peHeaders.containsKey(IWindowsFileInfo.PE_COMPANY_NAME)) {
      companyType.setValue(peHeaders.get(IWindowsFileInfo.PE_COMPANY_NAME));
    } else {
      companyType.setStatus(StatusEnumeration.DOES_NOT_EXIST);
    }
    item.setCompany(companyType);

    EntityItemStringType internalNameType = Factories.sc.core.createEntityItemStringType();
    if (peHeaders != null && peHeaders.containsKey(IWindowsFileInfo.PE_INTERNAL_NAME)) {
      internalNameType.setValue(peHeaders.get(IWindowsFileInfo.PE_INTERNAL_NAME));
    } else {
      internalNameType.setStatus(StatusEnumeration.DOES_NOT_EXIST);
    }
    item.setInternalName(internalNameType);

    EntityItemStringType productNameType = Factories.sc.core.createEntityItemStringType();
    if (peHeaders != null && peHeaders.containsKey(IWindowsFileInfo.PE_PRODUCT_NAME)) {
      productNameType.setValue(peHeaders.get(IWindowsFileInfo.PE_PRODUCT_NAME));
    } else {
      productNameType.setStatus(StatusEnumeration.DOES_NOT_EXIST);
    }
    item.setProductName(productNameType);

    EntityItemStringType originalFilenameType = Factories.sc.core.createEntityItemStringType();
    if (peHeaders != null && peHeaders.containsKey(IWindowsFileInfo.PE_ORIGINAL_NAME)) {
      originalFilenameType.setValue(peHeaders.get(IWindowsFileInfo.PE_ORIGINAL_NAME));
    } else {
      originalFilenameType.setStatus(StatusEnumeration.DOES_NOT_EXIST);
    }
    item.setOriginalFilename(originalFilenameType);

    EntityItemVersionType productVersionType = Factories.sc.core.createEntityItemVersionType();
    productVersionType.setDatatype(SimpleDatatypeEnumeration.VERSION.value());
    if (peHeaders != null && peHeaders.containsKey(IWindowsFileInfo.PE_PRODUCT_VERSION)) {
      productVersionType.setValue(peHeaders.get(IWindowsFileInfo.PE_PRODUCT_VERSION));
    } else {
      productVersionType.setStatus(StatusEnumeration.DOES_NOT_EXIST);
    }
    item.setProductVersion(productVersionType);

    EntityItemStringType developmentClassType = Factories.sc.core.createEntityItemStringType();
    if (peHeaders != null && peHeaders.containsKey(IWindowsFileInfo.PE_VERSION)) {
      try {
        developmentClassType.setValue(
            getDevelopmentClass(peHeaders.get(IWindowsFileInfo.PE_VERSION)));
      } catch (IllegalArgumentException e) {
        MessageType msg = Factories.common.createMessageType();
        msg.setLevel(MessageLevelEnumeration.INFO);
        msg.setValue(JOVALMsg.getMessage(JOVALMsg.STATUS_PE_DEVCLASS, e.getMessage()));
        item.getMessage().add(msg);
        developmentClassType.setStatus(StatusEnumeration.DOES_NOT_EXIST);
      }
    } else {
      developmentClassType.setStatus(StatusEnumeration.DOES_NOT_EXIST);
    }
    item.setDevelopmentClass(developmentClassType);
  }
Beispiel #17
0
 public StreamLogger(InputStream in, File outLog) throws IOException {
   this(null, in, outLog, JOVALMsg.getLogger());
 }