Autopsy  4.19.3
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;
37 import org.sleuthkit.datamodel.TagName;
38 import org.sleuthkit.datamodel.SleuthkitCase;
39 import org.sleuthkit.datamodel.TskCoreException;
40 import org.sleuthkit.datamodel.TskData;
41 
45 @Immutable
46 final public class TagNameDefinition implements Comparable<TagNameDefinition> {
47 
48  private static final Logger LOGGER = Logger.getLogger(TagNameDefinition.class.getName());
49  @Messages({
50  "TagNameDefinition.predefTagNames.bookmark.text=Bookmark",
51  "TagNameDefinition.predefTagNames.followUp.text=Follow Up",
52  "TagNameDefinition.predefTagNames.notableItem.text=Notable Item",})
53 
54  private static final String TAGS_SETTINGS_NAME = "Tags"; //NON-NLS
55  private static final String TAG_NAMES_SETTING_KEY = "TagNames"; //NON-NLS
56  private static final String TAG_SETTING_VERSION_KEY = "CustomTagNameVersion";
57  private static final int TAG_SETTINGS_VERSION = 1;
58 
59  private final String displayName;
60  private final String description;
61  private final TagName.HTML_COLOR color;
62  private final TskData.FileKnown knownStatus;
63 
64  private static final List<TagNameDefinition> STANDARD_TAGS_DEFINITIONS = new ArrayList<>();
65  private static final List<String> PROJECT_VIC_NAMES_NO_LONGER_USED = new ArrayList<>();
66 
67  static {
68  STANDARD_TAGS_DEFINITIONS.add(new TagNameDefinition(Bundle.TagNameDefinition_predefTagNames_bookmark_text(), "", TagName.HTML_COLOR.NONE, TskData.FileKnown.UNKNOWN));
69  STANDARD_TAGS_DEFINITIONS.add(new TagNameDefinition(Bundle.TagNameDefinition_predefTagNames_followUp_text(), "", TagName.HTML_COLOR.NONE, TskData.FileKnown.UNKNOWN));
70  STANDARD_TAGS_DEFINITIONS.add(new TagNameDefinition(Bundle.TagNameDefinition_predefTagNames_notableItem_text(), "", TagName.HTML_COLOR.NONE, TskData.FileKnown.BAD));
71 
72  PROJECT_VIC_NAMES_NO_LONGER_USED.add("CAT-1: Child Exploitation (Illegal)");
73  PROJECT_VIC_NAMES_NO_LONGER_USED.add("CAT-2: Child Exploitation (Non-Illegal/Age Difficult)");
74  PROJECT_VIC_NAMES_NO_LONGER_USED.add("CAT-3: CGI/Animation (Child Exploitive)");
75  PROJECT_VIC_NAMES_NO_LONGER_USED.add("CAT-4: Exemplar/Comparison (Internal Use Only)");
76  PROJECT_VIC_NAMES_NO_LONGER_USED.add("CAT-5: Non-pertinent");
77  PROJECT_VIC_NAMES_NO_LONGER_USED.add("CAT-0: Uncategorized");
78  }
79 
89  public TagNameDefinition(String displayName, String description, TagName.HTML_COLOR color, TskData.FileKnown status) {
90  this.displayName = displayName;
91  this.description = description;
92  this.color = color;
93  this.knownStatus = status;
94  }
95 
96  static Collection<TagNameDefinition> getStandardTagNameDefinitions() {
97  return Collections.unmodifiableCollection(STANDARD_TAGS_DEFINITIONS);
98  }
99 
100  static List<String> getStandardTagNames() {
101  List<String> strList = new ArrayList<>();
102 
103  for (TagNameDefinition def : STANDARD_TAGS_DEFINITIONS) {
104  strList.add(def.getDisplayName());
105  }
106 
107  return strList;
108  }
109 
115  static String getBookmarkTagDisplayName() {
116  return Bundle.TagNameDefinition_predefTagNames_bookmark_text();
117  }
118 
124  static String getFollowUpTagDisplayName() {
125  return Bundle.TagNameDefinition_predefTagNames_followUp_text();
126  }
127 
133  static String getNotableTagDisplayName() {
134  return Bundle.TagNameDefinition_predefTagNames_notableItem_text();
135  }
136 
142  public String getDisplayName() {
143  return displayName;
144  }
145 
151  public String getDescription() {
152  return description;
153  }
154 
160  public TagName.HTML_COLOR getColor() {
161  return color;
162  }
163 
169  public TskData.FileKnown getKnownStatus() {
170  return knownStatus;
171  }
172 
184  @Override
185  public int compareTo(TagNameDefinition other) {
186  return this.getDisplayName().toLowerCase().compareTo(other.getDisplayName().toLowerCase());
187  }
188 
194  @Override
195  public int hashCode() {
196  int hash = 7;
197  hash = 83 * hash + Objects.hashCode(this.displayName);
198  return hash;
199  }
200 
209  @Override
210  public boolean equals(Object obj) {
211  if (!(obj instanceof TagNameDefinition)) {
212  return false;
213  }
214  boolean sameName = this.getDisplayName().equals(((TagNameDefinition) obj).getDisplayName());
215  boolean sameStatus = this.getKnownStatus().equals(((TagNameDefinition) obj).getKnownStatus());
216  return sameName && sameStatus;
217  }
218 
224  @Override
225  public String toString() {
226  return displayName;
227  }
228 
233  private String toSettingsFormat() {
234  return displayName + "," + description + "," + color.name() + "," + knownStatus.toString();
235  }
236 
237  TagName saveToCase(SleuthkitCase caseDb) {
238  TagName tagName = null;
239  try {
240  tagName = caseDb.getTaggingManager().addOrUpdateTagName(displayName, description, color, knownStatus);
241  } catch (TskCoreException ex) {
242  LOGGER.log(Level.SEVERE, "Error saving tag name definition", ex);
243  }
244  return tagName;
245  }
246 
261  static synchronized Set<TagNameDefinition> getTagNameDefinitions() {
262  if (needsVersionUpdate()) {
264  }
265 
266  String tagsProperty = ModuleSettings.getConfigSetting(TAGS_SETTINGS_NAME, TAG_NAMES_SETTING_KEY);
267  if (tagsProperty == null || tagsProperty.isEmpty()) {
268  return new HashSet<>();
269  }
270 
271  List<String> individualTags = Arrays.asList(tagsProperty.split(";"));
272 
273  if (individualTags == null || individualTags.isEmpty()) {
274  return new HashSet<>();
275  }
276 
277  Set<TagNameDefinition> definitions = new HashSet<>();
278  for (String tagProps : individualTags) {
279  String[] attributes = tagProps.split(",");
280 
281  definitions.add(new TagNameDefinition(attributes[0], attributes[1],
282  TagName.HTML_COLOR.valueOf(attributes[2]), TskData.FileKnown.valueOf(attributes[3])));
283  }
284 
285  return definitions;
286  }
287 
293  static synchronized void setTagNameDefinitions(Set<TagNameDefinition> tagNames) {
294  StringBuilder setting = new StringBuilder();
295  for (TagNameDefinition tagName : tagNames) {
296  if (setting.length() != 0) {
297  setting.append(";");
298  }
299  setting.append(tagName.toSettingsFormat());
300  try {
301  SleuthkitCase caseDb = Case.getCurrentCaseThrows().getSleuthkitCase();
302  tagName.saveToCase(caseDb);
303  } catch (NoCurrentCaseException ex) {
304  LOGGER.log(Level.SEVERE, "Exception while getting open case.", ex);
305  }
306  }
307 
308  ModuleSettings.setConfigSetting(TAGS_SETTINGS_NAME, TAG_SETTING_VERSION_KEY, Integer.toString(TAG_SETTINGS_VERSION));
309  ModuleSettings.setConfigSetting(TAGS_SETTINGS_NAME, TAG_NAMES_SETTING_KEY, setting.toString());
310  }
311 
315  private static void updatePropertyFile() {
316  Integer version = getPropertyFileVersion();
317  List<TagNameDefinition> definitions = new ArrayList<>();
318 
319  if (version == null) {
320  String tagsProperty = ModuleSettings.getConfigSetting(TAGS_SETTINGS_NAME, TAG_NAMES_SETTING_KEY);
321  if (tagsProperty == null || tagsProperty.isEmpty()) {
322  ModuleSettings.setConfigSetting(TAGS_SETTINGS_NAME, TAG_SETTING_VERSION_KEY, Integer.toString(TAG_SETTINGS_VERSION));
323  return;
324  }
325 
326  List<String> individualTags = Arrays.asList(tagsProperty.split(";"));
327 
328  if (individualTags == null || individualTags.isEmpty()) {
329  return;
330  }
331 
332  List<String> notableTagList = null;
333  for (String tagProps : individualTags) {
334  String[] attributes = tagProps.split(",");
335  TskData.FileKnown fileKnown = TskData.FileKnown.UNKNOWN;
336  if (attributes.length == 3) {
337  // If notableTagList is null load it from the CR.
338  if (notableTagList == null) {
339  notableTagList = getCRNotableList();
340  } else {
341  if (notableTagList.contains(attributes[0])) {
342  fileKnown = TskData.FileKnown.BAD;
343  }
344  }
345  } else {
346  fileKnown = TskData.FileKnown.valueOf(attributes[3]);
347  }
348 
349  definitions.add(new TagNameDefinition(attributes[0], attributes[1],
350  TagName.HTML_COLOR.valueOf(attributes[2]), fileKnown));
351  }
352  }
353 
354  if (definitions.isEmpty()) {
355  return;
356  }
357 
358  // Remove the standard and Project VIC tags from the list
359  List<String> tagStringsToKeep = new ArrayList<>();
360  List<String> standardTags = getStandardTagNames();
361  for (TagNameDefinition def : definitions) {
362  if (!standardTags.contains(def.getDisplayName())
363  && !PROJECT_VIC_NAMES_NO_LONGER_USED.contains(def.getDisplayName())) {
364  tagStringsToKeep.add(def.toSettingsFormat());
365  }
366  }
367 
368  // Write out the version and the new tag list.
369  ModuleSettings.setConfigSetting(TAGS_SETTINGS_NAME, TAG_SETTING_VERSION_KEY, Integer.toString(TAG_SETTINGS_VERSION));
370  ModuleSettings.setConfigSetting(TAGS_SETTINGS_NAME, TAG_NAMES_SETTING_KEY, String.join(";", tagStringsToKeep));
371  }
372 
378  private static List<String> getCRNotableList() {
379  String notableTagsProp = ModuleSettings.getConfigSetting(CentralRepoSettings.getInstance().getModuleSettingsKey(), "db.badTags"); // NON-NLS
380  if (notableTagsProp != null && !notableTagsProp.isEmpty()) {
381  return Arrays.asList(notableTagsProp.split(","));
382  }
383 
384  return new ArrayList<>();
385  }
386 
393  private static boolean needsVersionUpdate() {
394  Integer version = getPropertyFileVersion();
395  return version == null || version < TAG_SETTINGS_VERSION;
396  }
397 
404  private static Integer getPropertyFileVersion() {
405  String version = ModuleSettings.getConfigSetting(TAGS_SETTINGS_NAME, TAG_SETTING_VERSION_KEY);
406  if (version == null || version.isEmpty()) {
407  return null;
408  }
409 
410  try {
411  return Integer.parseInt(version);
412  } catch (NumberFormatException ex) {
413  // The version is not an integer
414  return null;
415  }
416  }
417 
418 }
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-2022 Basis Technology. Generated on: Sat Sep 24 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.