Autopsy  4.1
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 
41 public final class UserPreferences {
42 
43  private static final boolean IS_WINDOWS_OS = PlatformUtil.isWindowsOS();
44  private static final Preferences preferences = NbPreferences.forModule(UserPreferences.class);
45  public static final String KEEP_PREFERRED_VIEWER = "KeepPreferredViewer"; // NON-NLS
46  public static final String HIDE_KNOWN_FILES_IN_DATA_SRCS_TREE = "HideKnownFilesInDataSourcesTree"; //NON-NLS
47  public static final String HIDE_KNOWN_FILES_IN_VIEWS_TREE = "HideKnownFilesInViewsTree"; //NON-NLS
48  public static final String HIDE_SLACK_FILES_IN_DATA_SRCS_TREE = "HideSlackFilesInDataSourcesTree"; //NON-NLS
49  public static final String HIDE_SLACK_FILES_IN_VIEWS_TREE = "HideSlackFilesInViewsTree"; //NON-NLS
50  public static final String DISPLAY_TIMES_IN_LOCAL_TIME = "DisplayTimesInLocalTime"; //NON-NLS
51  public static final String NUMBER_OF_FILE_INGEST_THREADS = "NumberOfFileIngestThreads"; //NON-NLS
52  public static final String IS_MULTI_USER_MODE_ENABLED = "IsMultiUserModeEnabled"; //NON-NLS
53  public static final String EXTERNAL_DATABASE_HOSTNAME_OR_IP = "ExternalDatabaseHostnameOrIp"; //NON-NLS
54  public static final String EXTERNAL_DATABASE_PORTNUMBER = "ExternalDatabasePortNumber"; //NON-NLS
55  public static final String EXTERNAL_DATABASE_NAME = "ExternalDatabaseName"; //NON-NLS
56  public static final String EXTERNAL_DATABASE_USER = "ExternalDatabaseUsername"; //NON-NLS
57  public static final String EXTERNAL_DATABASE_PASSWORD = "ExternalDatabasePassword"; //NON-NLS
58  public static final String EXTERNAL_DATABASE_TYPE = "ExternalDatabaseType"; //NON-NLS
59  public static final String INDEXING_SERVER_HOST = "IndexingServerHost"; //NON-NLS
60  public static final String INDEXING_SERVER_PORT = "IndexingServerPort"; //NON-NLS
61  private static final String MESSAGE_SERVICE_PASSWORD = "MessageServicePassword"; //NON-NLS
62  private static final String MESSAGE_SERVICE_USER = "MessageServiceUser"; //NON-NLS
63  private static final String MESSAGE_SERVICE_HOST = "MessageServiceHost"; //NON-NLS
64  private static final String MESSAGE_SERVICE_PORT = "MessageServicePort"; //NON-NLS
65  public static final String PROCESS_TIME_OUT_ENABLED = "ProcessTimeOutEnabled"; //NON-NLS
66  public static final String PROCESS_TIME_OUT_HOURS = "ProcessTimeOutHours"; //NON-NLS
67  private static final int DEFAULT_PROCESS_TIMEOUT_HR = 60;
68  private static final String DEFAULT_PORT_STRING = "61616";
69  private static final int DEFAULT_PORT_INT = 61616;
70  private static final String APP_NAME = "AppName";
71 
72  // Prevent instantiation.
73  private UserPreferences() {
74  }
75 
82  public static void reloadFromStorage() throws BackingStoreException {
83  preferences.sync();
84  }
85 
93  public static void saveToStorage() throws BackingStoreException {
94  preferences.flush();
95  }
96 
97  public static void addChangeListener(PreferenceChangeListener listener) {
98  preferences.addPreferenceChangeListener(listener);
99  }
100 
101  public static void removeChangeListener(PreferenceChangeListener listener) {
102  preferences.removePreferenceChangeListener(listener);
103  }
104 
105  public static boolean keepPreferredContentViewer() {
106  return preferences.getBoolean(KEEP_PREFERRED_VIEWER, false);
107  }
108 
109  public static void setKeepPreferredContentViewer(boolean value) {
110  preferences.putBoolean(KEEP_PREFERRED_VIEWER, value);
111  }
112 
113  public static boolean hideKnownFilesInDataSourcesTree() {
114  return preferences.getBoolean(HIDE_KNOWN_FILES_IN_DATA_SRCS_TREE, false);
115  }
116 
117  public static void setHideKnownFilesInDataSourcesTree(boolean value) {
118  preferences.putBoolean(HIDE_KNOWN_FILES_IN_DATA_SRCS_TREE, value);
119  }
120 
121  public static boolean hideKnownFilesInViewsTree() {
122  return preferences.getBoolean(HIDE_KNOWN_FILES_IN_VIEWS_TREE, true);
123  }
124 
125  public static void setHideKnownFilesInViewsTree(boolean value) {
126  preferences.putBoolean(HIDE_KNOWN_FILES_IN_VIEWS_TREE, value);
127  }
128 
129  public static boolean hideSlackFilesInDataSourcesTree() {
130  return preferences.getBoolean(HIDE_SLACK_FILES_IN_DATA_SRCS_TREE, true);
131  }
132 
133  public static void setHideSlackFilesInDataSourcesTree(boolean value) {
134  preferences.putBoolean(HIDE_SLACK_FILES_IN_DATA_SRCS_TREE, value);
135  }
136 
137  public static boolean hideSlackFilesInViewsTree() {
138  return preferences.getBoolean(HIDE_SLACK_FILES_IN_VIEWS_TREE, true);
139  }
140 
141  public static void setHideSlackFilesInViewsTree(boolean value) {
142  preferences.putBoolean(HIDE_SLACK_FILES_IN_VIEWS_TREE, value);
143  }
144 
145  public static boolean displayTimesInLocalTime() {
146  return preferences.getBoolean(DISPLAY_TIMES_IN_LOCAL_TIME, true);
147  }
148 
149  public static void setDisplayTimesInLocalTime(boolean value) {
150  preferences.putBoolean(DISPLAY_TIMES_IN_LOCAL_TIME, value);
151  }
152 
153  public static int numberOfFileIngestThreads() {
154  return preferences.getInt(NUMBER_OF_FILE_INGEST_THREADS, 2);
155  }
156 
157  public static void setNumberOfFileIngestThreads(int value) {
158  preferences.putInt(NUMBER_OF_FILE_INGEST_THREADS, value);
159  }
160 
167  DbType dbType;
168  try {
169  dbType = DbType.valueOf(preferences.get(EXTERNAL_DATABASE_TYPE, "POSTGRESQL")); //NON-NLS
170  } catch (Exception ex) {
171  dbType = DbType.SQLITE;
172  }
173  return new CaseDbConnectionInfo(
174  preferences.get(EXTERNAL_DATABASE_HOSTNAME_OR_IP, ""),
175  preferences.get(EXTERNAL_DATABASE_PORTNUMBER, "5432"),
176  preferences.get(EXTERNAL_DATABASE_USER, ""),
177  TextConverter.convertHexTextToText(preferences.get(EXTERNAL_DATABASE_PASSWORD, "")),
178  dbType);
179  }
180 
188  public static void setDatabaseConnectionInfo(CaseDbConnectionInfo connectionInfo) throws UserPreferencesException {
189  preferences.put(EXTERNAL_DATABASE_HOSTNAME_OR_IP, connectionInfo.getHost());
190  preferences.put(EXTERNAL_DATABASE_PORTNUMBER, connectionInfo.getPort());
191  preferences.put(EXTERNAL_DATABASE_USER, connectionInfo.getUserName());
192  preferences.put(EXTERNAL_DATABASE_PASSWORD, TextConverter.convertTextToHexText(connectionInfo.getPassword()));
193  preferences.put(EXTERNAL_DATABASE_TYPE, connectionInfo.getDbType().toString());
194  }
195 
196  public static void setIsMultiUserModeEnabled(boolean enabled) {
197  preferences.putBoolean(IS_MULTI_USER_MODE_ENABLED, enabled);
198  }
199 
200  public static boolean getIsMultiUserModeEnabled() {
201  if (!IS_WINDOWS_OS) {
202  return false;
203  }
204  return preferences.getBoolean(IS_MULTI_USER_MODE_ENABLED, false);
205  }
206 
207  public static String getIndexingServerHost() {
208  return preferences.get(INDEXING_SERVER_HOST, "");
209  }
210 
211  public static void setIndexingServerHost(String hostName) {
212  preferences.put(INDEXING_SERVER_HOST, hostName);
213  }
214 
215  public static String getIndexingServerPort() {
216  return preferences.get(INDEXING_SERVER_PORT, "8983");
217  }
218 
219  public static void setIndexingServerPort(int port) {
220  preferences.putInt(INDEXING_SERVER_PORT, port);
221  }
222 
230  preferences.put(MESSAGE_SERVICE_HOST, info.getHost());
231  preferences.put(MESSAGE_SERVICE_PORT, Integer.toString(info.getPort()));
232  preferences.put(MESSAGE_SERVICE_USER, info.getUserName());
233  preferences.put(MESSAGE_SERVICE_PASSWORD, TextConverter.convertTextToHexText(info.getPassword()));
234  }
235 
243  int port;
244  try {
245  port = Integer.parseInt(preferences.get(MESSAGE_SERVICE_PORT, DEFAULT_PORT_STRING));
246  } catch (NumberFormatException ex) {
247  // if there is an error parsing the port number, use the default port number
248  port = DEFAULT_PORT_INT;
249  }
250 
251  return new MessageServiceConnectionInfo(
252  preferences.get(MESSAGE_SERVICE_HOST, ""),
253  port,
254  preferences.get(MESSAGE_SERVICE_USER, ""),
255  TextConverter.convertHexTextToText(preferences.get(MESSAGE_SERVICE_PASSWORD, "")));
256  }
257 
263  public static int getProcessTimeOutHrs() {
264  int timeOut = preferences.getInt(PROCESS_TIME_OUT_HOURS, DEFAULT_PROCESS_TIMEOUT_HR);
265  if (timeOut < 0) {
266  timeOut = 0;
267  }
268  return timeOut;
269  }
270 
276  public static void setProcessTimeOutHrs(int value) {
277  if (value < 0) {
278  value = 0;
279  }
280  preferences.putInt(PROCESS_TIME_OUT_HOURS, value);
281  }
282 
290  public static boolean getIsTimeOutEnabled() {
291  boolean enabled = preferences.getBoolean(PROCESS_TIME_OUT_ENABLED, false);
292  return enabled;
293  }
294 
302  public static void setIsTimeOutEnabled(boolean enabled) {
303  preferences.putBoolean(PROCESS_TIME_OUT_ENABLED, enabled);
304  }
305 
310  public static String getAppName(){
311  return preferences.get(APP_NAME, "Autopsy");
312  }
313 
319  public static void setAppName(String name){
320  preferences.put(APP_NAME, name);
321  }
322 
323 
327  static final class TextConverter {
328 
329  private static final char[] TMP = "hgleri21auty84fwe".toCharArray(); //NON-NLS
330  private static final byte[] SALT = {
331  (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12,
332  (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12,};
333 
343  static String convertTextToHexText(String property) throws UserPreferencesException {
344  try {
345  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); //NON-NLS
346  SecretKey key = keyFactory.generateSecret(new PBEKeySpec(TMP));
347  Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); //NON-NLS
348  pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
349  return base64Encode(pbeCipher.doFinal(property.getBytes("UTF-8")));
350  } catch (Exception ex) {
351  throw new UserPreferencesException(
352  NbBundle.getMessage(TextConverter.class, "TextConverter.convert.exception.txt"));
353  }
354  }
355 
356  private static String base64Encode(byte[] bytes) {
357  return Base64.getEncoder().encodeToString(bytes);
358  }
359 
369  static String convertHexTextToText(String property) throws UserPreferencesException {
370  try {
371  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); //NON-NLS
372  SecretKey key = keyFactory.generateSecret(new PBEKeySpec(TMP));
373  Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); //NON-NLS
374  pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
375  return new String(pbeCipher.doFinal(base64Decode(property)), "UTF-8");
376  } catch (Exception ex) {
377  throw new UserPreferencesException(
378  NbBundle.getMessage(TextConverter.class, "TextConverter.convertFromHex.exception.txt"));
379  }
380  }
381 
382  private static byte[] base64Decode(String property) {
383  return Base64.getDecoder().decode(property);
384  }
385  }
386 }
static void setKeepPreferredContentViewer(boolean value)
static void setDisplayTimesInLocalTime(boolean value)
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 void setIndexingServerHost(String hostName)
static void setHideSlackFilesInDataSourcesTree(boolean value)
static void setHideKnownFilesInDataSourcesTree(boolean value)
static void addChangeListener(PreferenceChangeListener listener)
static MessageServiceConnectionInfo getMessageServiceConnectionInfo()

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