Autopsy  4.0
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;
34 import org.sleuthkit.datamodel.CaseDbConnectionInfo;
35 import org.sleuthkit.datamodel.TskData.DbType;
36 
41 public final class UserPreferences {
42 
43  private static final boolean isWindowsOS = 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_SOURCES_TREE = "HideKnownFilesInDataSourcesTree"; //NON-NLS
47  public static final String HIDE_KNOWN_FILES_IN_VIEWS_TREE = "HideKnownFilesInViewsTree"; //NON-NLS
48  public static final String DISPLAY_TIMES_IN_LOCAL_TIME = "DisplayTimesInLocalTime"; //NON-NLS
49  public static final String NUMBER_OF_FILE_INGEST_THREADS = "NumberOfFileIngestThreads"; //NON-NLS
50  public static final String IS_MULTI_USER_MODE_ENABLED = "IsMultiUserModeEnabled"; //NON-NLS
51  public static final String EXTERNAL_DATABASE_HOSTNAME_OR_IP = "ExternalDatabaseHostnameOrIp"; //NON-NLS
52  public static final String EXTERNAL_DATABASE_PORTNUMBER = "ExternalDatabasePortNumber"; //NON-NLS
53  public static final String EXTERNAL_DATABASE_NAME = "ExternalDatabaseName"; //NON-NLS
54  public static final String EXTERNAL_DATABASE_USER = "ExternalDatabaseUsername"; //NON-NLS
55  public static final String EXTERNAL_DATABASE_PASSWORD = "ExternalDatabasePassword"; //NON-NLS
56  public static final String EXTERNAL_DATABASE_TYPE = "ExternalDatabaseType"; //NON-NLS
57  public static final String INDEXING_SERVER_HOST = "IndexingServerHost"; //NON-NLS
58  public static final String INDEXING_SERVER_PORT = "IndexingServerPort"; //NON-NLS
59  private static final String MESSAGE_SERVICE_PASSWORD = "MessageServicePassword"; //NON-NLS
60  private static final String MESSAGE_SERVICE_USER = "MessageServiceUser"; //NON-NLS
61  private static final String MESSAGE_SERVICE_HOST = "MessageServiceHost"; //NON-NLS
62  private static final String MESSAGE_SERVICE_PORT = "MessageServicePort"; //NON-NLS
63  public static final String PROCESS_TIME_OUT_ENABLED = "ProcessTimeOutEnabled"; //NON-NLS
64  public static final String PROCESS_TIME_OUT_HOURS = "ProcessTimeOutHours"; //NON-NLS
65  private static final int DEFAULT_PROCESS_TIMEOUT_HR = 60;
66  private static final String DEFAULT_PORT_STRING = "61616";
67  private static final int DEFAULT_PORT_INT = 61616;
68 
69  // Prevent instantiation.
70  private UserPreferences() {
71  }
72 
79  public static void reloadFromStorage() throws BackingStoreException {
80  preferences.sync();
81  }
82 
90  public static void saveToStorage() throws BackingStoreException {
91  preferences.flush();
92  }
93 
94  public static void addChangeListener(PreferenceChangeListener listener) {
95  preferences.addPreferenceChangeListener(listener);
96  }
97 
98  public static void removeChangeListener(PreferenceChangeListener listener) {
99  preferences.removePreferenceChangeListener(listener);
100  }
101 
102  public static boolean keepPreferredContentViewer() {
103  return preferences.getBoolean(KEEP_PREFERRED_VIEWER, false);
104  }
105 
106  public static void setKeepPreferredContentViewer(boolean value) {
107  preferences.putBoolean(KEEP_PREFERRED_VIEWER, value);
108  }
109 
110  public static boolean hideKnownFilesInDataSourcesTree() {
111  return preferences.getBoolean(HIDE_KNOWN_FILES_IN_DATA_SOURCES_TREE, false);
112  }
113 
114  public static void setHideKnownFilesInDataSourcesTree(boolean value) {
115  preferences.putBoolean(HIDE_KNOWN_FILES_IN_DATA_SOURCES_TREE, value);
116  }
117 
118  public static boolean hideKnownFilesInViewsTree() {
119  return preferences.getBoolean(HIDE_KNOWN_FILES_IN_VIEWS_TREE, true);
120  }
121 
122  public static void setHideKnownFilesInViewsTree(boolean value) {
123  preferences.putBoolean(HIDE_KNOWN_FILES_IN_VIEWS_TREE, value);
124  }
125 
126  public static boolean displayTimesInLocalTime() {
127  return preferences.getBoolean(DISPLAY_TIMES_IN_LOCAL_TIME, true);
128  }
129 
130  public static void setDisplayTimesInLocalTime(boolean value) {
131  preferences.putBoolean(DISPLAY_TIMES_IN_LOCAL_TIME, value);
132  }
133 
134  public static int numberOfFileIngestThreads() {
135  return preferences.getInt(NUMBER_OF_FILE_INGEST_THREADS, 2);
136  }
137 
138  public static void setNumberOfFileIngestThreads(int value) {
139  preferences.putInt(NUMBER_OF_FILE_INGEST_THREADS, value);
140  }
141 
147  public static CaseDbConnectionInfo getDatabaseConnectionInfo() throws UserPreferencesException {
148  DbType dbType;
149  try {
150  dbType = DbType.valueOf(preferences.get(EXTERNAL_DATABASE_TYPE, "POSTGRESQL")); //NON-NLS
151  } catch (Exception ex) {
152  dbType = DbType.SQLITE;
153  }
154  return new CaseDbConnectionInfo(
155  preferences.get(EXTERNAL_DATABASE_HOSTNAME_OR_IP, ""),
156  preferences.get(EXTERNAL_DATABASE_PORTNUMBER, "5432"),
157  preferences.get(EXTERNAL_DATABASE_USER, ""),
158  TextConverter.convertHexTextToText(preferences.get(EXTERNAL_DATABASE_PASSWORD, "")),
159  dbType);
160  }
161 
169  public static void setDatabaseConnectionInfo(CaseDbConnectionInfo connectionInfo) throws UserPreferencesException {
170  preferences.put(EXTERNAL_DATABASE_HOSTNAME_OR_IP, connectionInfo.getHost());
171  preferences.put(EXTERNAL_DATABASE_PORTNUMBER, connectionInfo.getPort());
172  preferences.put(EXTERNAL_DATABASE_USER, connectionInfo.getUserName());
173  preferences.put(EXTERNAL_DATABASE_PASSWORD, TextConverter.convertTextToHexText(connectionInfo.getPassword()));
174  preferences.put(EXTERNAL_DATABASE_TYPE, connectionInfo.getDbType().toString());
175  }
176 
177  public static void setIsMultiUserModeEnabled(boolean enabled) {
178  preferences.putBoolean(IS_MULTI_USER_MODE_ENABLED, enabled);
179  }
180 
181  public static boolean getIsMultiUserModeEnabled() {
182  if (!isWindowsOS) {
183  return false;
184  }
185  return preferences.getBoolean(IS_MULTI_USER_MODE_ENABLED, false);
186  }
187 
188  public static String getIndexingServerHost() {
189  return preferences.get(INDEXING_SERVER_HOST, "");
190  }
191 
192  public static void setIndexingServerHost(String hostName) {
193  preferences.put(INDEXING_SERVER_HOST, hostName);
194  }
195 
196  public static String getIndexingServerPort() {
197  return preferences.get(INDEXING_SERVER_PORT, "8983");
198  }
199 
200  public static void setIndexingServerPort(int port) {
201  preferences.putInt(INDEXING_SERVER_PORT, port);
202  }
203 
211  preferences.put(MESSAGE_SERVICE_HOST, info.getHost());
212  preferences.put(MESSAGE_SERVICE_PORT, Integer.toString(info.getPort()));
213  preferences.put(MESSAGE_SERVICE_USER, info.getUserName());
214  preferences.put(MESSAGE_SERVICE_PASSWORD, TextConverter.convertTextToHexText(info.getPassword()));
215  }
216 
224  int port;
225  try {
226  port = Integer.parseInt(preferences.get(MESSAGE_SERVICE_PORT, DEFAULT_PORT_STRING));
227  } catch (NumberFormatException ex) {
228  // if there is an error parsing the port number, use the default port number
229  port = DEFAULT_PORT_INT;
230  }
231 
232  return new MessageServiceConnectionInfo(
233  preferences.get(MESSAGE_SERVICE_HOST, ""),
234  port,
235  preferences.get(MESSAGE_SERVICE_USER, ""),
236  TextConverter.convertHexTextToText(preferences.get(MESSAGE_SERVICE_PASSWORD, "")));
237  }
238 
244  public static int getProcessTimeOutHrs() {
245  int timeOut = preferences.getInt(PROCESS_TIME_OUT_HOURS, DEFAULT_PROCESS_TIMEOUT_HR);
246  if (timeOut < 0) {
247  timeOut = 0;
248  }
249  return timeOut;
250  }
251 
257  public static void setProcessTimeOutHrs(int value) {
258  if (value < 0) {
259  value = 0;
260  }
261  preferences.putInt(PROCESS_TIME_OUT_HOURS, value);
262  }
263 
271  public static boolean getIsTimeOutEnabled() {
272  boolean enabled = preferences.getBoolean(PROCESS_TIME_OUT_ENABLED, false);
273  return enabled;
274  }
275 
283  public static void setIsTimeOutEnabled(boolean enabled) {
284  preferences.putBoolean(PROCESS_TIME_OUT_ENABLED, enabled);
285  }
286 
287 
291  static final class TextConverter {
292 
293  private static final char[] TMP = "hgleri21auty84fwe".toCharArray(); //NON-NLS
294  private static final byte[] SALT = {
295  (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12,
296  (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12,};
297 
307  static String convertTextToHexText(String property) throws UserPreferencesException {
308  try {
309  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); //NON-NLS
310  SecretKey key = keyFactory.generateSecret(new PBEKeySpec(TMP));
311  Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); //NON-NLS
312  pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
313  return base64Encode(pbeCipher.doFinal(property.getBytes("UTF-8")));
314  } catch (Exception ex) {
315  throw new UserPreferencesException(
316  NbBundle.getMessage(TextConverter.class, "TextConverter.convert.exception.txt"));
317  }
318  }
319 
320  private static String base64Encode(byte[] bytes) {
321  return Base64.getEncoder().encodeToString(bytes);
322  }
323 
333  static String convertHexTextToText(String property) throws UserPreferencesException {
334  try {
335  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); //NON-NLS
336  SecretKey key = keyFactory.generateSecret(new PBEKeySpec(TMP));
337  Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); //NON-NLS
338  pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
339  return new String(pbeCipher.doFinal(base64Decode(property)), "UTF-8");
340  } catch (Exception ex) {
341  throw new UserPreferencesException(
342  NbBundle.getMessage(TextConverter.class, "TextConverter.convertFromHex.exception.txt"));
343  }
344  }
345 
346  private static byte[] base64Decode(String property) {
347  return Base64.getDecoder().decode(property);
348  }
349  }
350 }
static final String HIDE_KNOWN_FILES_IN_DATA_SOURCES_TREE
static void setKeepPreferredContentViewer(boolean value)
static void setDisplayTimesInLocalTime(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 setHideKnownFilesInDataSourcesTree(boolean value)
static void addChangeListener(PreferenceChangeListener listener)
static MessageServiceConnectionInfo getMessageServiceConnectionInfo()

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