Autopsy  4.21.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-2024 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;
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 = 2; // Changed tag type from TskData.FileKnown to TskData.TagType
58 
59  private final String displayName;
60  private final String description;
61  private final TagName.HTML_COLOR color;
62  private final TskData.TagType tagType;
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.TagType.SUSPICIOUS));
69  STANDARD_TAGS_DEFINITIONS.add(new TagNameDefinition(Bundle.TagNameDefinition_predefTagNames_followUp_text(), "", TagName.HTML_COLOR.NONE, TskData.TagType.SUSPICIOUS));
70  STANDARD_TAGS_DEFINITIONS.add(new TagNameDefinition(Bundle.TagNameDefinition_predefTagNames_notableItem_text(), "", TagName.HTML_COLOR.NONE, TskData.TagType.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 
90  @Deprecated
91  public TagNameDefinition(String displayName, String description, TagName.HTML_COLOR color, TskData.FileKnown status) {
92  this.displayName = displayName;
93  this.description = description;
94  this.color = color;
96  }
97 
98  public TagNameDefinition(String displayName, String description, TagName.HTML_COLOR color, TskData.TagType status) {
99  this.displayName = displayName;
100  this.description = description;
101  this.color = color;
102  this.tagType = status;
103  }
104 
105  static Collection<TagNameDefinition> getStandardTagNameDefinitions() {
106  return Collections.unmodifiableCollection(STANDARD_TAGS_DEFINITIONS);
107  }
108 
109  static List<String> getStandardTagNames() {
110  List<String> strList = new ArrayList<>();
111 
112  for (TagNameDefinition def : STANDARD_TAGS_DEFINITIONS) {
113  strList.add(def.getDisplayName());
114  }
115 
116  return strList;
117  }
118 
124  static String getBookmarkTagDisplayName() {
125  return Bundle.TagNameDefinition_predefTagNames_bookmark_text();
126  }
127 
133  static String getFollowUpTagDisplayName() {
134  return Bundle.TagNameDefinition_predefTagNames_followUp_text();
135  }
136 
142  static String getNotableTagDisplayName() {
143  return Bundle.TagNameDefinition_predefTagNames_notableItem_text();
144  }
145 
151  public String getDisplayName() {
152  return displayName;
153  }
154 
160  public String getDescription() {
161  return description;
162  }
163 
170  return color;
171  }
172 
179  return tagType;
180  }
181 
188  @Deprecated
191  }
192 
204  @Override
205  public int compareTo(TagNameDefinition other) {
206  return this.getDisplayName().toLowerCase().compareTo(other.getDisplayName().toLowerCase());
207  }
208 
214  @Override
215  public int hashCode() {
216  int hash = 7;
217  hash = 83 * hash + Objects.hashCode(this.displayName);
218  return hash;
219  }
220 
229  @Override
230  public boolean equals(Object obj) {
231  if (!(obj instanceof TagNameDefinition)) {
232  return false;
233  }
234  boolean sameName = this.getDisplayName().equals(((TagNameDefinition) obj).getDisplayName());
235  boolean sameStatus = this.getTagType().equals(((TagNameDefinition) obj).getTagType());
236  return sameName && sameStatus;
237  }
238 
244  @Override
245  public String toString() {
246  return displayName;
247  }
248 
253  private String toSettingsFormat() {
254  return displayName + "," + description + "," + color.name() + "," + tagType.toString();
255  }
256 
257  TagName saveToCase(SleuthkitCase caseDb) {
258  TagName tagName = null;
259  try {
260  tagName = caseDb.getTaggingManager().addOrUpdateTagName(displayName, description, color, tagType);
261  } catch (TskCoreException ex) {
262  LOGGER.log(Level.SEVERE, "Error saving tag name definition", ex);
263  }
264  return tagName;
265  }
266 
281  static synchronized Set<TagNameDefinition> getTagNameDefinitions() {
282  if (needsVersionUpdate()) {
284  }
285 
286  String tagsProperty = ModuleSettings.getConfigSetting(TAGS_SETTINGS_NAME, TAG_NAMES_SETTING_KEY);
287  if (tagsProperty == null || tagsProperty.isEmpty()) {
288  return new HashSet<>();
289  }
290 
291  List<String> individualTags = Arrays.asList(tagsProperty.split(";"));
292 
293  if (individualTags == null || individualTags.isEmpty()) {
294  return new HashSet<>();
295  }
296 
297  Set<TagNameDefinition> definitions = new HashSet<>();
298  for (String tagProps : individualTags) {
299  String[] attributes = tagProps.split(",");
300 
301  definitions.add(new TagNameDefinition(attributes[0], attributes[1],
302  TagName.HTML_COLOR.valueOf(attributes[2]), TskData.TagType.valueOf(attributes[3])));
303  }
304 
305  return definitions;
306  }
307 
313  static synchronized void setTagNameDefinitions(Set<TagNameDefinition> tagNames) {
314  StringBuilder setting = new StringBuilder();
315  for (TagNameDefinition tagName : tagNames) {
316  if (setting.length() != 0) {
317  setting.append(";");
318  }
319  setting.append(tagName.toSettingsFormat());
320  try {
321  SleuthkitCase caseDb = Case.getCurrentCaseThrows().getSleuthkitCase();
322  tagName.saveToCase(caseDb);
323  } catch (NoCurrentCaseException ex) {
324  LOGGER.log(Level.SEVERE, "Exception while getting open case.", ex);
325  }
326  }
327 
328  ModuleSettings.setConfigSetting(TAGS_SETTINGS_NAME, TAG_SETTING_VERSION_KEY, Integer.toString(TAG_SETTINGS_VERSION));
329  ModuleSettings.setConfigSetting(TAGS_SETTINGS_NAME, TAG_NAMES_SETTING_KEY, setting.toString());
330  }
331 
335  private static void updatePropertyFile() {
336  Integer version = getPropertyFileVersion();
337  List<TagNameDefinition> definitions = new ArrayList<>();
338 
339  if (version == null || version == 1) {
340  String tagsProperty = ModuleSettings.getConfigSetting(TAGS_SETTINGS_NAME, TAG_NAMES_SETTING_KEY);
341  if (tagsProperty == null || tagsProperty.isEmpty()) {
342  ModuleSettings.setConfigSetting(TAGS_SETTINGS_NAME, TAG_SETTING_VERSION_KEY, Integer.toString(TAG_SETTINGS_VERSION));
343  return;
344  }
345 
346  List<String> individualTags = Arrays.asList(tagsProperty.split(";"));
347 
348  if (individualTags == null || individualTags.isEmpty()) {
349  return;
350  }
351 
352  List<String> notableTagList = null;
353  for (String tagProps : individualTags) {
354  String[] attributes = tagProps.split(",");
356  if (attributes.length == 3) {
357  // If notableTagList is null load it from the CR.
358  if (notableTagList == null) {
359  notableTagList = getCRNotableList();
360  } else {
361  if (notableTagList.contains(attributes[0])) {
363  }
364  }
365  } else {
366  if (version == 1) {
367  // handle backwards compatibility
369  } else {
370  tagType = TskData.TagType.valueOf(attributes[3]);
371  }
372  }
373 
374  definitions.add(new TagNameDefinition(attributes[0], attributes[1],
375  TagName.HTML_COLOR.valueOf(attributes[2]), tagType));
376  }
377  }
378 
379  if (definitions.isEmpty()) {
380  return;
381  }
382 
383  // Remove the standard and Project VIC tags from the list
384  List<String> tagStringsToKeep = new ArrayList<>();
385  List<String> standardTags = getStandardTagNames();
386  for (TagNameDefinition def : definitions) {
387  if (!standardTags.contains(def.getDisplayName())
388  && !PROJECT_VIC_NAMES_NO_LONGER_USED.contains(def.getDisplayName())) {
389  tagStringsToKeep.add(def.toSettingsFormat());
390  }
391  }
392 
393  // Write out the version and the new tag list.
394  ModuleSettings.setConfigSetting(TAGS_SETTINGS_NAME, TAG_SETTING_VERSION_KEY, Integer.toString(TAG_SETTINGS_VERSION));
395  ModuleSettings.setConfigSetting(TAGS_SETTINGS_NAME, TAG_NAMES_SETTING_KEY, String.join(";", tagStringsToKeep));
396  }
397 
403  private static List<String> getCRNotableList() {
404  String notableTagsProp = ModuleSettings.getConfigSetting(CentralRepoSettings.getInstance().getModuleSettingsKey(), "db.badTags"); // NON-NLS
405  if (notableTagsProp != null && !notableTagsProp.isEmpty()) {
406  return Arrays.asList(notableTagsProp.split(","));
407  }
408 
409  return new ArrayList<>();
410  }
411 
418  private static boolean needsVersionUpdate() {
419  Integer version = getPropertyFileVersion();
420  return version == null || version < TAG_SETTINGS_VERSION;
421  }
422 
429  private static Integer getPropertyFileVersion() {
430  String version = ModuleSettings.getConfigSetting(TAGS_SETTINGS_NAME, TAG_SETTING_VERSION_KEY);
431  if (version == null || version.isEmpty()) {
432  return null;
433  }
434 
435  try {
436  return Integer.parseInt(version);
437  } catch (NumberFormatException ex) {
438  // The version is not an integer
439  return null;
440  }
441  }
442 
443 }
static synchronized String getConfigSetting(String moduleName, String settingName)
static FileKnown valueOf(byte known)
TagName addOrUpdateTagName(String displayName, String description, TagName.HTML_COLOR color, TskData.FileKnown knownStatus)
static final List< TagNameDefinition > STANDARD_TAGS_DEFINITIONS
TagNameDefinition(String displayName, String description, TagName.HTML_COLOR color, TskData.FileKnown status)
synchronized TaggingManager getTaggingManager()
static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal)
static FileKnown convertTagTypeToFileKnown(TagType tagType)
TagNameDefinition(String displayName, String description, TagName.HTML_COLOR color, TskData.TagType status)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static TagType convertFileKnownToTagType(FileKnown fileKnown)

Copyright © 2012-2024 Sleuth Kit Labs. Generated on: Mon Mar 17 2025
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.