Autopsy  4.16.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
TagNameDefinition.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2018 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.casemodule.services;
20 
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Objects;
28 import java.util.Set;
29 import java.util.logging.Level;
30 import javax.annotation.concurrent.Immutable;
31 import org.openide.util.NbBundle.Messages;
36 import org.sleuthkit.datamodel.TagName;
37 import org.sleuthkit.datamodel.SleuthkitCase;
38 import org.sleuthkit.datamodel.TskCoreException;
39 import org.sleuthkit.datamodel.TskData;
40 
44 @Immutable
45 final public class TagNameDefinition implements Comparable<TagNameDefinition> {
46 
47  private static final Logger LOGGER = Logger.getLogger(TagNameDefinition.class.getName());
48  @Messages({
49  "TagNameDefinition.predefTagNames.bookmark.text=Bookmark",
50  "TagNameDefinition.predefTagNames.followUp.text=Follow Up",
51  "TagNameDefinition.predefTagNames.notableItem.text=Notable Item",})
52 
53  private static final String TAGS_SETTINGS_NAME = "Tags"; //NON-NLS
54  private static final String TAG_NAMES_SETTING_KEY = "TagNames"; //NON-NLS
55  private static final String TAG_SETTING_VERSION_KEY = "CustomTagNameVersion";
56  private static final int TAG_SETTINGS_VERSION = 1;
57 
58  private final String displayName;
59  private final String description;
60  private final TagName.HTML_COLOR color;
61  private final TskData.FileKnown knownStatus;
62 
63  private static final List<TagNameDefinition> STANDARD_TAGS_DEFINITIONS = new ArrayList<>();
64  private static final List<String> OLD_CATEGORY_TAG_NAMES = new ArrayList<>();
65 
66  static {
67  STANDARD_TAGS_DEFINITIONS.add(new TagNameDefinition(Bundle.TagNameDefinition_predefTagNames_bookmark_text(), "", TagName.HTML_COLOR.NONE, TskData.FileKnown.UNKNOWN));
68  STANDARD_TAGS_DEFINITIONS.add(new TagNameDefinition(Bundle.TagNameDefinition_predefTagNames_followUp_text(), "", TagName.HTML_COLOR.NONE, TskData.FileKnown.UNKNOWN));
69  STANDARD_TAGS_DEFINITIONS.add(new TagNameDefinition(Bundle.TagNameDefinition_predefTagNames_notableItem_text(), "", TagName.HTML_COLOR.NONE, TskData.FileKnown.BAD));
70 
71  OLD_CATEGORY_TAG_NAMES.add("CAT-1: Child Exploitation (Illegal)");
72  OLD_CATEGORY_TAG_NAMES.add("CAT-2: Child Exploitation (Non-Illegal/Age Difficult)");
73  OLD_CATEGORY_TAG_NAMES.add("CAT-3: CGI/Animation (Child Exploitive)");
74  OLD_CATEGORY_TAG_NAMES.add("CAT-4: Exemplar/Comparison (Internal Use Only)");
75  OLD_CATEGORY_TAG_NAMES.add("CAT-5: Non-pertinent");
76  OLD_CATEGORY_TAG_NAMES.add("CAT-0: Uncategorized");
77  }
78 
88  public TagNameDefinition(String displayName, String description, TagName.HTML_COLOR color, TskData.FileKnown status) {
89  this.displayName = displayName;
90  this.description = description;
91  this.color = color;
92  this.knownStatus = status;
93  }
94 
95  static Collection<TagNameDefinition> getStandardTagNameDefinitions() {
96  return Collections.unmodifiableCollection(STANDARD_TAGS_DEFINITIONS);
97  }
98 
99  static List<String> getStandardTagNames() {
100  List<String> strList = new ArrayList<>();
101 
102  for (TagNameDefinition def : STANDARD_TAGS_DEFINITIONS) {
103  strList.add(def.getDisplayName());
104  }
105 
106  return strList;
107  }
108 
114  static String getBookmarkTagDisplayName() {
115  return Bundle.TagNameDefinition_predefTagNames_bookmark_text();
116  }
117 
123  static String getFollowUpTagDisplayName() {
124  return Bundle.TagNameDefinition_predefTagNames_followUp_text();
125  }
126 
132  static String getNotableTagDisplayName() {
133  return Bundle.TagNameDefinition_predefTagNames_notableItem_text();
134  }
135 
141  public String getDisplayName() {
142  return displayName;
143  }
144 
150  public String getDescription() {
151  return description;
152  }
153 
159  public TagName.HTML_COLOR getColor() {
160  return color;
161  }
162 
168  public TskData.FileKnown getKnownStatus() {
169  return knownStatus;
170  }
171 
183  @Override
184  public int compareTo(TagNameDefinition other) {
185  return this.getDisplayName().toLowerCase().compareTo(other.getDisplayName().toLowerCase());
186  }
187 
193  @Override
194  public int hashCode() {
195  int hash = 7;
196  hash = 83 * hash + Objects.hashCode(this.displayName);
197  return hash;
198  }
199 
208  @Override
209  public boolean equals(Object obj) {
210  if (!(obj instanceof TagNameDefinition)) {
211  return false;
212  }
213  boolean sameName = this.getDisplayName().equals(((TagNameDefinition) obj).getDisplayName());
214  boolean sameStatus = this.getKnownStatus().equals(((TagNameDefinition) obj).getKnownStatus());
215  return sameName && sameStatus;
216  }
217 
223  @Override
224  public String toString() {
225  return displayName;
226  }
227 
232  private String toSettingsFormat() {
233  return displayName + "," + description + "," + color.name() + "," + knownStatus.toString();
234  }
235 
236  TagName saveToCase(SleuthkitCase caseDb) {
237  TagName tagName = null;
238  try {
239  tagName = caseDb.addOrUpdateTagName(displayName, description, color, knownStatus);
240  } catch (TskCoreException ex) {
241  LOGGER.log(Level.SEVERE, "Error saving tag name definition", ex);
242  }
243  return tagName;
244  }
245 
260  static synchronized Set<TagNameDefinition> getTagNameDefinitions() {
261  if (needsVersionUpdate()) {
263  }
264 
265  String tagsProperty = ModuleSettings.getConfigSetting(TAGS_SETTINGS_NAME, TAG_NAMES_SETTING_KEY);
266  if (tagsProperty == null || tagsProperty.isEmpty()) {
267  return new HashSet<>();
268  }
269 
270  List<String> individualTags = Arrays.asList(tagsProperty.split(";"));
271 
272  if (individualTags == null || individualTags.isEmpty()) {
273  return new HashSet<>();
274  }
275 
276  Set<TagNameDefinition> definitions = new HashSet<>();
277  for (String tagProps : individualTags) {
278  String[] attributes = tagProps.split(",");
279 
280  definitions.add(new TagNameDefinition(attributes[0], attributes[1],
281  TagName.HTML_COLOR.valueOf(attributes[2]), TskData.FileKnown.valueOf(attributes[3])));
282  }
283 
284  return definitions;
285  }
286 
292  static synchronized void setTagNameDefinitions(Set<TagNameDefinition> tagNames) {
293  StringBuilder setting = new StringBuilder();
294  for (TagNameDefinition tagName : tagNames) {
295  if (setting.length() != 0) {
296  setting.append(";");
297  }
298  setting.append(tagName.toSettingsFormat());
299  try {
300  SleuthkitCase caseDb = Case.getCurrentCaseThrows().getSleuthkitCase();
301  tagName.saveToCase(caseDb);
302  } catch (NoCurrentCaseException ex) {
303  LOGGER.log(Level.SEVERE, "Exception while getting open case.", ex);
304  }
305  }
306 
307  ModuleSettings.setConfigSetting(TAGS_SETTINGS_NAME, TAG_SETTING_VERSION_KEY, Integer.toString(TAG_SETTINGS_VERSION));
308  ModuleSettings.setConfigSetting(TAGS_SETTINGS_NAME, TAG_NAMES_SETTING_KEY, setting.toString());
309  }
310 
314  private static void updateTagDefinitions() {
315  Integer version = getPropertyFileVersion();
316  List<TagNameDefinition> definitions = new ArrayList<>();
317 
318  if (version == null) {
319  String tagsProperty = ModuleSettings.getConfigSetting(TAGS_SETTINGS_NAME, TAG_NAMES_SETTING_KEY);
320  if (tagsProperty == null || tagsProperty.isEmpty()) {
321  ModuleSettings.setConfigSetting(TAGS_SETTINGS_NAME, TAG_SETTING_VERSION_KEY, Integer.toString(TAG_SETTINGS_VERSION));
322  return;
323  }
324 
325  List<String> individualTags = Arrays.asList(tagsProperty.split(";"));
326 
327  if (individualTags == null || individualTags.isEmpty()) {
328  return;
329  }
330 
331  List<String> notableTagList = null;
332  for (String tagProps : individualTags) {
333  String[] attributes = tagProps.split(",");
334  TskData.FileKnown fileKnown = TskData.FileKnown.UNKNOWN;
335  if (attributes.length == 3) {
336  // If notableTagList is null load it from the CR.
337  if (notableTagList == null) {
338  notableTagList = getCRNotableList();
339  } else {
340  if (notableTagList.contains(attributes[0])) {
341  fileKnown = TskData.FileKnown.BAD;
342  }
343  }
344  } else {
345  fileKnown = TskData.FileKnown.valueOf(attributes[3]);
346  }
347 
348  definitions.add(new TagNameDefinition(attributes[0], attributes[1],
349  TagName.HTML_COLOR.valueOf(attributes[2]), fileKnown));
350  }
351  }
352 
353  if (definitions.isEmpty()) {
354  return;
355  }
356 
357  // Remove the standard and Project VIC tags from the list
358  List<String> tagStrings = new ArrayList<>();
359  List<String> standardTags = getStandardTagNames();
360  for (TagNameDefinition def : definitions) {
361  if (!standardTags.contains(def.getDisplayName())
362  && !OLD_CATEGORY_TAG_NAMES.contains(def.getDisplayName())) {
363  tagStrings.add(def.toSettingsFormat());
364  }
365  }
366 
367  // Write out the version and the new tag list.
368  ModuleSettings.setConfigSetting(TAGS_SETTINGS_NAME, TAG_SETTING_VERSION_KEY, Integer.toString(TAG_SETTINGS_VERSION));
369  ModuleSettings.setConfigSetting(TAGS_SETTINGS_NAME, TAG_NAMES_SETTING_KEY, String.join(";", tagStrings));
370  }
371 
377  private static List<String> getCRNotableList() {
378  String notableTagsProp = ModuleSettings.getConfigSetting("CentralRepository", "db.badTags"); // NON-NLS
379  if (notableTagsProp != null && !notableTagsProp.isEmpty()) {
380  return Arrays.asList(notableTagsProp.split(","));
381  }
382 
383  return new ArrayList<>();
384  }
385 
392  private static boolean needsVersionUpdate() {
393  Integer version = getPropertyFileVersion();
394  return version == null || version < TAG_SETTINGS_VERSION;
395  }
396 
403  private static Integer getPropertyFileVersion() {
404  String version = ModuleSettings.getConfigSetting(TAGS_SETTINGS_NAME, TAG_SETTING_VERSION_KEY);
405  if (version == null || version.isEmpty()) {
406  return null;
407  }
408 
409  try {
410  return Integer.parseInt(version);
411  } catch (NumberFormatException ex) {
412  // The version is not an integer
413  return null;
414  }
415  }
416 
417 }
static synchronized String getConfigSetting(String moduleName, String settingName)
static final List< TagNameDefinition > STANDARD_TAGS_DEFINITIONS
TagNameDefinition(String displayName, String description, TagName.HTML_COLOR color, TskData.FileKnown status)
static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

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