Autopsy  4.4
Graphical digital forensics platform for The Sleuth Kit and other tools.
UserPreferences.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2014 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.core;
20 
21 import java.util.Base64;
22 import java.util.prefs.BackingStoreException;
24 import java.util.prefs.PreferenceChangeListener;
25 import java.util.prefs.Preferences;
26 import javax.crypto.Cipher;
27 import javax.crypto.SecretKey;
28 import javax.crypto.SecretKeyFactory;
29 import javax.crypto.spec.PBEKeySpec;
30 import javax.crypto.spec.PBEParameterSpec;
31 import org.openide.util.NbBundle;
32 import org.openide.util.NbPreferences;
36 import org.sleuthkit.datamodel.CaseDbConnectionInfo;
37 import org.sleuthkit.datamodel.TskData.DbType;
38 
43 public final class UserPreferences {
44 
45  private static final boolean IS_WINDOWS_OS = PlatformUtil.isWindowsOS();
46  private static final Preferences preferences = NbPreferences.forModule(UserPreferences.class);
47  public static final String KEEP_PREFERRED_VIEWER = "KeepPreferredViewer"; // NON-NLS
48  public static final String HIDE_KNOWN_FILES_IN_DATA_SRCS_TREE = "HideKnownFilesInDataSourcesTree"; //NON-NLS
49  public static final String HIDE_KNOWN_FILES_IN_VIEWS_TREE = "HideKnownFilesInViewsTree"; //NON-NLS
50  public static final String HIDE_SLACK_FILES_IN_DATA_SRCS_TREE = "HideSlackFilesInDataSourcesTree"; //NON-NLS
51  public static final String HIDE_SLACK_FILES_IN_VIEWS_TREE = "HideSlackFilesInViewsTree"; //NON-NLS
52  public static final String DISPLAY_TIMES_IN_LOCAL_TIME = "DisplayTimesInLocalTime"; //NON-NLS
53  public static final String NUMBER_OF_FILE_INGEST_THREADS = "NumberOfFileIngestThreads"; //NON-NLS
54  public static final String IS_MULTI_USER_MODE_ENABLED = "IsMultiUserModeEnabled"; //NON-NLS
55  public static final String EXTERNAL_DATABASE_HOSTNAME_OR_IP = "ExternalDatabaseHostnameOrIp"; //NON-NLS
56  public static final String EXTERNAL_DATABASE_PORTNUMBER = "ExternalDatabasePortNumber"; //NON-NLS
57  public static final String EXTERNAL_DATABASE_NAME = "ExternalDatabaseName"; //NON-NLS
58  public static final String EXTERNAL_DATABASE_USER = "ExternalDatabaseUsername"; //NON-NLS
59  public static final String EXTERNAL_DATABASE_PASSWORD = "ExternalDatabasePassword"; //NON-NLS
60  public static final String EXTERNAL_DATABASE_TYPE = "ExternalDatabaseType"; //NON-NLS
61  public static final String INDEXING_SERVER_HOST = "IndexingServerHost"; //NON-NLS
62  public static final String INDEXING_SERVER_PORT = "IndexingServerPort"; //NON-NLS
63  private static final String MESSAGE_SERVICE_PASSWORD = "MessageServicePassword"; //NON-NLS
64  private static final String MESSAGE_SERVICE_USER = "MessageServiceUser"; //NON-NLS
65  private static final String MESSAGE_SERVICE_HOST = "MessageServiceHost"; //NON-NLS
66  private static final String MESSAGE_SERVICE_PORT = "MessageServicePort"; //NON-NLS
67  public static final String PROCESS_TIME_OUT_ENABLED = "ProcessTimeOutEnabled"; //NON-NLS
68  public static final String PROCESS_TIME_OUT_HOURS = "ProcessTimeOutHours"; //NON-NLS
69  private static final int DEFAULT_PROCESS_TIMEOUT_HR = 60;
70  private static final String DEFAULT_PORT_STRING = "61616";
71  private static final int DEFAULT_PORT_INT = 61616;
72  private static final String APP_NAME = "AppName";
73  public static final String SETTINGS_PROPERTIES = "AutoIngest";
74  private static final String MODE = "AutopsyMode"; // NON-NLS
75 
76  // Prevent instantiation.
77  private UserPreferences() {
78  }
79 
80  public enum SelectedMode {
81 
84  REVIEW
85  };
86 
92  public static SelectedMode getMode() {
93  if (ModuleSettings.settingExists(SETTINGS_PROPERTIES, MODE)) {
94  int ordinal = Integer.parseInt(ModuleSettings.getConfigSetting(SETTINGS_PROPERTIES, MODE));
95  return UserPreferences.SelectedMode.values()[ordinal];
96  }
98  }
99 
105  public static void setMode(SelectedMode mode) {
106  ModuleSettings.setConfigSetting(SETTINGS_PROPERTIES, MODE, Integer.toString(mode.ordinal()));
107  }
108 
115  public static void reloadFromStorage() throws BackingStoreException {
116  preferences.sync();
117  }
118 
126  public static void saveToStorage() throws BackingStoreException {
127  preferences.flush();
128  }
129 
130  public static void addChangeListener(PreferenceChangeListener listener) {
131  preferences.addPreferenceChangeListener(listener);
132  }
133 
134  public static void removeChangeListener(PreferenceChangeListener listener) {
135  preferences.removePreferenceChangeListener(listener);
136  }
137 
138  public static boolean keepPreferredContentViewer() {
139  return preferences.getBoolean(KEEP_PREFERRED_VIEWER, false);
140  }
141 
142  public static void setKeepPreferredContentViewer(boolean value) {
143  preferences.putBoolean(KEEP_PREFERRED_VIEWER, value);
144  }
145 
146  public static boolean hideKnownFilesInDataSourcesTree() {
147  return preferences.getBoolean(HIDE_KNOWN_FILES_IN_DATA_SRCS_TREE, false);
148  }
149 
150  public static void setHideKnownFilesInDataSourcesTree(boolean value) {
151  preferences.putBoolean(HIDE_KNOWN_FILES_IN_DATA_SRCS_TREE, value);
152  }
153 
154  public static boolean hideKnownFilesInViewsTree() {
155  return preferences.getBoolean(HIDE_KNOWN_FILES_IN_VIEWS_TREE, true);
156  }
157 
158  public static void setHideKnownFilesInViewsTree(boolean value) {
159  preferences.putBoolean(HIDE_KNOWN_FILES_IN_VIEWS_TREE, value);
160  }
161 
162  public static boolean hideSlackFilesInDataSourcesTree() {
163  return preferences.getBoolean(HIDE_SLACK_FILES_IN_DATA_SRCS_TREE, true);
164  }
165 
166  public static void setHideSlackFilesInDataSourcesTree(boolean value) {
167  preferences.putBoolean(HIDE_SLACK_FILES_IN_DATA_SRCS_TREE, value);
168  }
169 
170  public static boolean hideSlackFilesInViewsTree() {
171  return preferences.getBoolean(HIDE_SLACK_FILES_IN_VIEWS_TREE, true);
172  }
173 
174  public static void setHideSlackFilesInViewsTree(boolean value) {
175  preferences.putBoolean(HIDE_SLACK_FILES_IN_VIEWS_TREE, value);
176  }
177 
178  public static boolean displayTimesInLocalTime() {
179  return preferences.getBoolean(DISPLAY_TIMES_IN_LOCAL_TIME, true);
180  }
181 
182  public static void setDisplayTimesInLocalTime(boolean value) {
183  preferences.putBoolean(DISPLAY_TIMES_IN_LOCAL_TIME, value);
184  }
185 
186  public static int numberOfFileIngestThreads() {
187  return preferences.getInt(NUMBER_OF_FILE_INGEST_THREADS, 2);
188  }
189 
190  public static void setNumberOfFileIngestThreads(int value) {
191  preferences.putInt(NUMBER_OF_FILE_INGEST_THREADS, value);
192  }
193 
201  public static CaseDbConnectionInfo getDatabaseConnectionInfo() throws UserPreferencesException {
202  DbType dbType;
203  try {
204  dbType = DbType.valueOf(preferences.get(EXTERNAL_DATABASE_TYPE, "POSTGRESQL")); //NON-NLS
205  } catch (Exception ex) {
206  dbType = DbType.SQLITE;
207  }
208  return new CaseDbConnectionInfo(
209  preferences.get(EXTERNAL_DATABASE_HOSTNAME_OR_IP, ""),
210  preferences.get(EXTERNAL_DATABASE_PORTNUMBER, "5432"),
211  preferences.get(EXTERNAL_DATABASE_USER, ""),
212  TextConverter.convertHexTextToText(preferences.get(EXTERNAL_DATABASE_PASSWORD, "")),
213  dbType);
214  }
215 
224  public static void setDatabaseConnectionInfo(CaseDbConnectionInfo connectionInfo) throws UserPreferencesException {
225  preferences.put(EXTERNAL_DATABASE_HOSTNAME_OR_IP, connectionInfo.getHost());
226  preferences.put(EXTERNAL_DATABASE_PORTNUMBER, connectionInfo.getPort());
227  preferences.put(EXTERNAL_DATABASE_USER, connectionInfo.getUserName());
228  preferences.put(EXTERNAL_DATABASE_PASSWORD, TextConverter.convertTextToHexText(connectionInfo.getPassword()));
229  preferences.put(EXTERNAL_DATABASE_TYPE, connectionInfo.getDbType().toString());
230  }
231 
232  public static void setIsMultiUserModeEnabled(boolean enabled) {
233  preferences.putBoolean(IS_MULTI_USER_MODE_ENABLED, enabled);
234  }
235 
236  public static boolean getIsMultiUserModeEnabled() {
237  if (!IS_WINDOWS_OS) {
238  return false;
239  }
240  return preferences.getBoolean(IS_MULTI_USER_MODE_ENABLED, false);
241  }
242 
243  public static String getIndexingServerHost() {
244  return preferences.get(INDEXING_SERVER_HOST, "");
245  }
246 
247  public static void setIndexingServerHost(String hostName) {
248  preferences.put(INDEXING_SERVER_HOST, hostName);
249  }
250 
251  public static String getIndexingServerPort() {
252  return preferences.get(INDEXING_SERVER_PORT, "8983");
253  }
254 
255  public static void setIndexingServerPort(int port) {
256  preferences.putInt(INDEXING_SERVER_PORT, port);
257  }
258 
267  preferences.put(MESSAGE_SERVICE_HOST, info.getHost());
268  preferences.put(MESSAGE_SERVICE_PORT, Integer.toString(info.getPort()));
269  preferences.put(MESSAGE_SERVICE_USER, info.getUserName());
270  preferences.put(MESSAGE_SERVICE_PASSWORD, TextConverter.convertTextToHexText(info.getPassword()));
271  }
272 
281  int port;
282  try {
283  port = Integer.parseInt(preferences.get(MESSAGE_SERVICE_PORT, DEFAULT_PORT_STRING));
284  } catch (NumberFormatException ex) {
285  // if there is an error parsing the port number, use the default port number
286  port = DEFAULT_PORT_INT;
287  }
288 
289  return new MessageServiceConnectionInfo(
290  preferences.get(MESSAGE_SERVICE_HOST, ""),
291  port,
292  preferences.get(MESSAGE_SERVICE_USER, ""),
293  TextConverter.convertHexTextToText(preferences.get(MESSAGE_SERVICE_PASSWORD, "")));
294  }
295 
301  public static int getProcessTimeOutHrs() {
302  int timeOut = preferences.getInt(PROCESS_TIME_OUT_HOURS, DEFAULT_PROCESS_TIMEOUT_HR);
303  if (timeOut < 0) {
304  timeOut = 0;
305  }
306  return timeOut;
307  }
308 
314  public static void setProcessTimeOutHrs(int value) {
315  if (value < 0) {
316  value = 0;
317  }
318  preferences.putInt(PROCESS_TIME_OUT_HOURS, value);
319  }
320 
328  public static boolean getIsTimeOutEnabled() {
329  boolean enabled = preferences.getBoolean(PROCESS_TIME_OUT_ENABLED, false);
330  return enabled;
331  }
332 
340  public static void setIsTimeOutEnabled(boolean enabled) {
341  preferences.putBoolean(PROCESS_TIME_OUT_ENABLED, enabled);
342  }
343 
349  public static String getAppName() {
350  return preferences.get(APP_NAME, String.format("%s %s", Version.getName(), Version.getVersion()));
351  }
352 
358  public static void setAppName(String name) {
359  preferences.put(APP_NAME, name);
360  }
361 
365  static final class TextConverter {
366 
367  private static final char[] TMP = "hgleri21auty84fwe".toCharArray(); //NON-NLS
368  private static final byte[] SALT = {
369  (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12,
370  (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12,};
371 
381  static String convertTextToHexText(String property) throws UserPreferencesException {
382  try {
383  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); //NON-NLS
384  SecretKey key = keyFactory.generateSecret(new PBEKeySpec(TMP));
385  Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); //NON-NLS
386  pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
387  return base64Encode(pbeCipher.doFinal(property.getBytes("UTF-8")));
388  } catch (Exception ex) {
389  throw new UserPreferencesException(
390  NbBundle.getMessage(TextConverter.class, "TextConverter.convert.exception.txt"));
391  }
392  }
393 
394  private static String base64Encode(byte[] bytes) {
395  return Base64.getEncoder().encodeToString(bytes);
396  }
397 
407  static String convertHexTextToText(String property) throws UserPreferencesException {
408  try {
409  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); //NON-NLS
410  SecretKey key = keyFactory.generateSecret(new PBEKeySpec(TMP));
411  Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); //NON-NLS
412  pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
413  return new String(pbeCipher.doFinal(base64Decode(property)), "UTF-8");
414  } catch (Exception ex) {
415  throw new UserPreferencesException(
416  NbBundle.getMessage(TextConverter.class, "TextConverter.convertFromHex.exception.txt"));
417  }
418  }
419 
420  private static byte[] base64Decode(String property) {
421  return Base64.getDecoder().decode(property);
422  }
423  }
424 }
static void setKeepPreferredContentViewer(boolean value)
static void setDisplayTimesInLocalTime(boolean value)
static void setMode(SelectedMode mode)
static void setHideSlackFilesInViewsTree(boolean value)
static CaseDbConnectionInfo getDatabaseConnectionInfo()
static void setIsTimeOutEnabled(boolean enabled)
static void setIsMultiUserModeEnabled(boolean enabled)
static void removeChangeListener(PreferenceChangeListener listener)
static void setMessageServiceConnectionInfo(MessageServiceConnectionInfo info)
static void setHideKnownFilesInViewsTree(boolean value)
static void setDatabaseConnectionInfo(CaseDbConnectionInfo connectionInfo)
static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal)
static void setIndexingServerHost(String hostName)
static void setHideSlackFilesInDataSourcesTree(boolean value)
static void setHideKnownFilesInDataSourcesTree(boolean value)
static String getConfigSetting(String moduleName, String settingName)
static void addChangeListener(PreferenceChangeListener listener)
static boolean settingExists(String moduleName, String settingName)
static MessageServiceConnectionInfo getMessageServiceConnectionInfo()

Copyright © 2012-2016 Basis Technology. Generated on: Tue Jun 13 2017
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.