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;
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  private static final String APP_NAME = "AppName";
69 
70  // Prevent instantiation.
71  private UserPreferences() {
72  }
73 
80  public static void reloadFromStorage() throws BackingStoreException {
81  preferences.sync();
82  }
83 
91  public static void saveToStorage() throws BackingStoreException {
92  preferences.flush();
93  }
94 
95  public static void addChangeListener(PreferenceChangeListener listener) {
96  preferences.addPreferenceChangeListener(listener);
97  }
98 
99  public static void removeChangeListener(PreferenceChangeListener listener) {
100  preferences.removePreferenceChangeListener(listener);
101  }
102 
103  public static boolean keepPreferredContentViewer() {
104  return preferences.getBoolean(KEEP_PREFERRED_VIEWER, false);
105  }
106 
107  public static void setKeepPreferredContentViewer(boolean value) {
108  preferences.putBoolean(KEEP_PREFERRED_VIEWER, value);
109  }
110 
111  public static boolean hideKnownFilesInDataSourcesTree() {
112  return preferences.getBoolean(HIDE_KNOWN_FILES_IN_DATA_SOURCES_TREE, false);
113  }
114 
115  public static void setHideKnownFilesInDataSourcesTree(boolean value) {
116  preferences.putBoolean(HIDE_KNOWN_FILES_IN_DATA_SOURCES_TREE, value);
117  }
118 
119  public static boolean hideKnownFilesInViewsTree() {
120  return preferences.getBoolean(HIDE_KNOWN_FILES_IN_VIEWS_TREE, true);
121  }
122 
123  public static void setHideKnownFilesInViewsTree(boolean value) {
124  preferences.putBoolean(HIDE_KNOWN_FILES_IN_VIEWS_TREE, value);
125  }
126 
127  public static boolean displayTimesInLocalTime() {
128  return preferences.getBoolean(DISPLAY_TIMES_IN_LOCAL_TIME, true);
129  }
130 
131  public static void setDisplayTimesInLocalTime(boolean value) {
132  preferences.putBoolean(DISPLAY_TIMES_IN_LOCAL_TIME, value);
133  }
134 
135  public static int numberOfFileIngestThreads() {
136  return preferences.getInt(NUMBER_OF_FILE_INGEST_THREADS, 2);
137  }
138 
139  public static void setNumberOfFileIngestThreads(int value) {
140  preferences.putInt(NUMBER_OF_FILE_INGEST_THREADS, value);
141  }
142 
148  public static CaseDbConnectionInfo getDatabaseConnectionInfo() throws UserPreferencesException {
149  DbType dbType;
150  try {
151  dbType = DbType.valueOf(preferences.get(EXTERNAL_DATABASE_TYPE, "POSTGRESQL")); //NON-NLS
152  } catch (Exception ex) {
153  dbType = DbType.SQLITE;
154  }
155  return new CaseDbConnectionInfo(
156  preferences.get(EXTERNAL_DATABASE_HOSTNAME_OR_IP, ""),
157  preferences.get(EXTERNAL_DATABASE_PORTNUMBER, "5432"),
158  preferences.get(EXTERNAL_DATABASE_USER, ""),
159  TextConverter.convertHexTextToText(preferences.get(EXTERNAL_DATABASE_PASSWORD, "")),
160  dbType);
161  }
162 
170  public static void setDatabaseConnectionInfo(CaseDbConnectionInfo connectionInfo) throws UserPreferencesException {
171  preferences.put(EXTERNAL_DATABASE_HOSTNAME_OR_IP, connectionInfo.getHost());
172  preferences.put(EXTERNAL_DATABASE_PORTNUMBER, connectionInfo.getPort());
173  preferences.put(EXTERNAL_DATABASE_USER, connectionInfo.getUserName());
174  preferences.put(EXTERNAL_DATABASE_PASSWORD, TextConverter.convertTextToHexText(connectionInfo.getPassword()));
175  preferences.put(EXTERNAL_DATABASE_TYPE, connectionInfo.getDbType().toString());
176  }
177 
178  public static void setIsMultiUserModeEnabled(boolean enabled) {
179  preferences.putBoolean(IS_MULTI_USER_MODE_ENABLED, enabled);
180  }
181 
182  public static boolean getIsMultiUserModeEnabled() {
183  if (!isWindowsOS) {
184  return false;
185  }
186  return preferences.getBoolean(IS_MULTI_USER_MODE_ENABLED, false);
187  }
188 
189  public static String getIndexingServerHost() {
190  return preferences.get(INDEXING_SERVER_HOST, "");
191  }
192 
193  public static void setIndexingServerHost(String hostName) {
194  preferences.put(INDEXING_SERVER_HOST, hostName);
195  }
196 
197  public static String getIndexingServerPort() {
198  return preferences.get(INDEXING_SERVER_PORT, "8983");
199  }
200 
201  public static void setIndexingServerPort(int port) {
202  preferences.putInt(INDEXING_SERVER_PORT, port);
203  }
204 
212  preferences.put(MESSAGE_SERVICE_HOST, info.getHost());
213  preferences.put(MESSAGE_SERVICE_PORT, Integer.toString(info.getPort()));
214  preferences.put(MESSAGE_SERVICE_USER, info.getUserName());
215  preferences.put(MESSAGE_SERVICE_PASSWORD, TextConverter.convertTextToHexText(info.getPassword()));
216  }
217 
225  int port;
226  try {
227  port = Integer.parseInt(preferences.get(MESSAGE_SERVICE_PORT, DEFAULT_PORT_STRING));
228  } catch (NumberFormatException ex) {
229  // if there is an error parsing the port number, use the default port number
230  port = DEFAULT_PORT_INT;
231  }
232 
233  return new MessageServiceConnectionInfo(
234  preferences.get(MESSAGE_SERVICE_HOST, ""),
235  port,
236  preferences.get(MESSAGE_SERVICE_USER, ""),
237  TextConverter.convertHexTextToText(preferences.get(MESSAGE_SERVICE_PASSWORD, "")));
238  }
239 
245  public static int getProcessTimeOutHrs() {
246  int timeOut = preferences.getInt(PROCESS_TIME_OUT_HOURS, DEFAULT_PROCESS_TIMEOUT_HR);
247  if (timeOut < 0) {
248  timeOut = 0;
249  }
250  return timeOut;
251  }
252 
258  public static void setProcessTimeOutHrs(int value) {
259  if (value < 0) {
260  value = 0;
261  }
262  preferences.putInt(PROCESS_TIME_OUT_HOURS, value);
263  }
264 
272  public static boolean getIsTimeOutEnabled() {
273  boolean enabled = preferences.getBoolean(PROCESS_TIME_OUT_ENABLED, false);
274  return enabled;
275  }
276 
284  public static void setIsTimeOutEnabled(boolean enabled) {
285  preferences.putBoolean(PROCESS_TIME_OUT_ENABLED, enabled);
286  }
287 
292  public static String getAppName(){
293  return preferences.get(APP_NAME, "Autopsy");
294  }
295 
301  public static void setAppName(String name){
302  preferences.put(APP_NAME, name);
303  }
304 
305 
309  static final class TextConverter {
310 
311  private static final char[] TMP = "hgleri21auty84fwe".toCharArray(); //NON-NLS
312  private static final byte[] SALT = {
313  (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12,
314  (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12,};
315 
325  static String convertTextToHexText(String property) throws UserPreferencesException {
326  try {
327  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); //NON-NLS
328  SecretKey key = keyFactory.generateSecret(new PBEKeySpec(TMP));
329  Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); //NON-NLS
330  pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
331  return base64Encode(pbeCipher.doFinal(property.getBytes("UTF-8")));
332  } catch (Exception ex) {
333  throw new UserPreferencesException(
334  NbBundle.getMessage(TextConverter.class, "TextConverter.convert.exception.txt"));
335  }
336  }
337 
338  private static String base64Encode(byte[] bytes) {
339  return Base64.getEncoder().encodeToString(bytes);
340  }
341 
351  static String convertHexTextToText(String property) throws UserPreferencesException {
352  try {
353  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); //NON-NLS
354  SecretKey key = keyFactory.generateSecret(new PBEKeySpec(TMP));
355  Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); //NON-NLS
356  pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
357  return new String(pbeCipher.doFinal(base64Decode(property)), "UTF-8");
358  } catch (Exception ex) {
359  throw new UserPreferencesException(
360  NbBundle.getMessage(TextConverter.class, "TextConverter.convertFromHex.exception.txt"));
361  }
362  }
363 
364  private static byte[] base64Decode(String property) {
365  return Base64.getDecoder().decode(property);
366  }
367  }
368 }
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-2016 Basis Technology. Generated on: Tue Oct 25 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.