Autopsy  4.19.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
FileExtMismatchSettings.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2016 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.modules.fileextmismatch;
20 
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.Serializable;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.Set;
29 import java.util.logging.Level;
30 import org.openide.util.io.NbObjectInputStream;
31 import org.openide.util.io.NbObjectOutputStream;
35 import org.w3c.dom.Document;
36 import org.w3c.dom.Element;
37 import org.w3c.dom.NodeList;
38 
44 class FileExtMismatchSettings implements Serializable {
45 
46  private static final long serialVersionUID = 1L;
47  private HashMap<String, Set<String>> mimeTypeToExtsMap;
48  private static final Logger logger = Logger.getLogger(FileExtMismatchSettings.class.getName());
49  private static final String SIG_EL = "signature"; //NON-NLS
50  private static final String EXT_EL = "ext"; //NON-NLS
51  private static final String SIG_MIMETYPE_ATTR = "mimetype"; //NON-NLS
52 
53  private static final String DEFAULT_CONFIG_FILE_NAME = "mismatch_config.xml"; //NON-NLS
54  private static final String FILTER_CONFIG_FILE = PlatformUtil.getUserConfigDirectory() + File.separator + DEFAULT_CONFIG_FILE_NAME;
55  private static final String DEFAULT_SERIALIZED_FILE_NAME = "mismatch_config.settings";
56  private static final String DEFAULT_SERIALIZED_FILE_PATH = PlatformUtil.getUserConfigDirectory() + File.separator + DEFAULT_SERIALIZED_FILE_NAME;
57 
58  static {
59  try {
60  PlatformUtil.extractResourceToUserConfigDir(FileExtMismatchSettings.class, DEFAULT_CONFIG_FILE_NAME, false);
61  } catch (IOException ex) {
62  logger.log(Level.SEVERE, "Error copying default mismatch configuration to user dir ", ex); //NON-NLS
63  }
64  }
65 
71  FileExtMismatchSettings(HashMap<String, Set<String>> mimeTypeToExtsMap) {
72  this.mimeTypeToExtsMap = mimeTypeToExtsMap;
73  }
74 
78  HashMap<String, Set<String>> getMimeTypeToExtsMap() {
79  return mimeTypeToExtsMap;
80  }
81 
85  void setMimeTypeToExtsMap(HashMap<String, Set<String>> mimeTypeToExtsMap) {
86  this.mimeTypeToExtsMap = mimeTypeToExtsMap;
87  }
88 
94  static synchronized FileExtMismatchSettings readSettings() throws FileExtMismatchSettingsException {
95  File serializedFile = new File(DEFAULT_SERIALIZED_FILE_PATH);
96  //Tries reading the serialized file first, as this is the prioritized settings.
97  if (serializedFile.exists()) {
98  return readSerializedSettings();
99  }
100  return readXmlSettings();
101  }
102 
103  private static FileExtMismatchSettings readSerializedSettings() throws FileExtMismatchSettingsException {
104  File serializedFile = new File(DEFAULT_SERIALIZED_FILE_PATH);
105  try {
106  try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(serializedFile))) {
107  FileExtMismatchSettings fileExtMismatchSettings = (FileExtMismatchSettings) in.readObject();
108  return fileExtMismatchSettings;
109  }
110  } catch (IOException | ClassNotFoundException ex) {
111  throw new FileExtMismatchSettingsException("Couldn't read serialized settings.", ex);
112  }
113  }
114 
115  private static FileExtMismatchSettings readXmlSettings() throws FileExtMismatchSettingsException {
116  HashMap<String, Set<String>> sigTypeToExtMap = new HashMap<>();
117  //Next tries to read the xml file if the serialized file did not exist
118  File xmlFile = new File(FILTER_CONFIG_FILE);
119  if (xmlFile.exists()) {
120  try {
121  final Document doc = XMLUtil.loadDoc(FileExtMismatchSettings.class, FILTER_CONFIG_FILE);
122  if (doc == null) {
123  throw new FileExtMismatchSettingsException("Error loading config file: invalid file format (could not load doc).");
124  }
125 
126  Element root = doc.getDocumentElement();
127  if (root == null) {
128  throw new FileExtMismatchSettingsException("Error loading config file: invalid file format (bad root)."); //NON-NLS
129  }
130 
131  NodeList sigNList = root.getElementsByTagName(SIG_EL);
132  final int numSigs = sigNList.getLength();
133 
134  if (numSigs == 0) {
135  throw new FileExtMismatchSettingsException("Error loading config file: invalid file format (no signature)."); //NON-NLS
136  }
137 
138  for (int sigIndex = 0; sigIndex < numSigs; ++sigIndex) {
139  Element sigEl = (Element) sigNList.item(sigIndex);
140  final String mimetype = sigEl.getAttribute(SIG_MIMETYPE_ATTR);
141 
142  NodeList extNList = sigEl.getElementsByTagName(EXT_EL);
143  final int numExts = extNList.getLength();
144 
145  if (numExts != 0) {
146  Set<String> extStrings = new HashSet<>();
147  for (int extIndex = 0; extIndex < numExts; ++extIndex) {
148  Element extEl = (Element) extNList.item(extIndex);
149  extStrings.add(extEl.getTextContent());
150  }
151  sigTypeToExtMap.put(mimetype, extStrings);
152  } else {
153  sigTypeToExtMap.put(mimetype, null); //ok to have an empty type (the ingest module will not use it)
154  }
155  }
156 
157  } catch (Exception e) {
158  throw new FileExtMismatchSettingsException("Error loading config file.", e); //NON-NLS
159  }
160  }
161  return new FileExtMismatchSettings(sigTypeToExtMap);
162  }
163 
171  static synchronized void writeSettings(FileExtMismatchSettings settings) throws FileExtMismatchSettingsException {
172  try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(DEFAULT_SERIALIZED_FILE_PATH))) {
173  out.writeObject(settings);
174  } catch (IOException ex) {
175  throw new FileExtMismatchSettingsException(String.format("Failed to write settings to %s", DEFAULT_SERIALIZED_FILE_PATH), ex);
176  }
177  }
178 
184  static class FileExtMismatchSettingsException extends Exception {
185 
186  private static final long serialVersionUID = 1L;
187 
188  FileExtMismatchSettingsException(String message) {
189  super(message);
190  }
191 
192  FileExtMismatchSettingsException(String message, Throwable throwable) {
193  super(message, throwable);
194  }
195  }
196 }

Copyright © 2012-2021 Basis Technology. Generated on: Thu Sep 30 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.