Autopsy  4.17.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
SqliteCentralRepoSettings.java
Go to the documentation of this file.
1 /*
2  * Central Repository
3  *
4  * Copyright 2015-2020 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.centralrepository.datamodel;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.nio.file.Files;
24 import java.nio.file.InvalidPathException;
25 import java.sql.Connection;
26 import java.sql.DriverManager;
27 import java.sql.SQLException;
28 import java.util.logging.Level;
29 import java.util.regex.Pattern;
33 
41 
42  public final static String DEFAULT_DBNAME = "central_repository.db"; // NON-NLS
43  private final static Logger LOGGER = Logger.getLogger(SqliteCentralRepoSettings.class.getName());
44  private final static String DEFAULT_DBDIRECTORY = PlatformUtil.getUserDirectory() + File.separator + "central_repository"; // NON-NLS
45  private final static String JDBC_DRIVER = "org.sqlite.JDBC"; // NON-NLS
46  private final static String JDBC_BASE_URI = "jdbc:sqlite:"; // NON-NLS
47  private final static String VALIDATION_QUERY = "SELECT count(*) from sqlite_master"; // NON-NLS
48 
49  private final static String DB_NAMES_REGEX = "[a-z][a-z0-9_]*(\\.db)?";
50  private String dbName;
51  private String dbDirectory;
52  private int bulkThreshold;
53 
55  loadSettings();
56  }
57 
58  public void loadSettings() {
59  dbName = ModuleSettings.getConfigSetting("CentralRepository", "db.sqlite.dbName"); // NON-NLS
60  if (dbName == null || dbName.isEmpty()) {
61  dbName = DEFAULT_DBNAME;
62  }
63 
64  dbDirectory = ModuleSettings.getConfigSetting("CentralRepository", "db.sqlite.dbDirectory"); // NON-NLS
65  if (dbDirectory == null || dbDirectory.isEmpty()) {
66  dbDirectory = DEFAULT_DBDIRECTORY;
67  }
68 
69  try {
70  String bulkThresholdString = ModuleSettings.getConfigSetting("CentralRepository", "db.sqlite.bulkThreshold"); // NON-NLS
71  if (bulkThresholdString == null || bulkThresholdString.isEmpty()) {
72  this.bulkThreshold = RdbmsCentralRepo.DEFAULT_BULK_THRESHHOLD;
73  } else {
74  this.bulkThreshold = Integer.parseInt(bulkThresholdString);
75  if (getBulkThreshold() <= 0) {
76  this.bulkThreshold = RdbmsCentralRepo.DEFAULT_BULK_THRESHHOLD;
77  }
78  }
79  } catch (NumberFormatException ex) {
80  this.bulkThreshold = RdbmsCentralRepo.DEFAULT_BULK_THRESHHOLD;
81  }
82  }
83 
84  public String toString() {
85  return String.format("SqliteCentralRepoSettings: [db type: sqlite, directory: %s, name: %s]", getDbDirectory(), getDbName());
86  }
87 
91  public void setupDefaultSettings() {
92  dbName = DEFAULT_DBNAME;
93  dbDirectory = DEFAULT_DBDIRECTORY;
94  }
95 
96  public void saveSettings() {
98 
99  ModuleSettings.setConfigSetting("CentralRepository", "db.sqlite.dbName", getDbName()); // NON-NLS
100  ModuleSettings.setConfigSetting("CentralRepository", "db.sqlite.dbDirectory", getDbDirectory()); // NON-NLS
101  ModuleSettings.setConfigSetting("CentralRepository", "db.sqlite.bulkThreshold", Integer.toString(getBulkThreshold())); // NON-NLS
102  }
103 
109  public boolean dbFileExists() {
110  File dbFile = new File(getFileNameWithPath());
111  if (!dbFile.exists()) {
112  return false;
113  }
114  // It's unlikely, but make sure the file isn't actually a directory
115  return (!dbFile.isDirectory());
116  }
117 
118  @Override
119  public boolean verifyDatabaseExists() {
120  return dbDirectoryExists();
121  }
122 
128  public boolean dbDirectoryExists() {
129  // Ensure dbDirectory is a valid directory
130  File dbDir = new File(getDbDirectory());
131 
132  if (!dbDir.exists()) {
133  return false;
134  } else if (!dbDir.isDirectory()) {
135  return false;
136  }
137 
138  return true;
139 
140  }
141 
147  @Override
148  public boolean createDatabase() {
149  return createDbDirectory();
150  }
151 
157  public boolean createDbDirectory() {
158  if (!dbDirectoryExists()) {
159  try {
160  File dbDir = new File(getDbDirectory());
161  Files.createDirectories(dbDir.toPath());
162  LOGGER.log(Level.INFO, "sqlite directory did not exist, created it at {0}.", getDbDirectory()); // NON-NLS
163  } catch (IOException | InvalidPathException | SecurityException ex) {
164  LOGGER.log(Level.SEVERE, "Failed to create sqlite database directory.", ex); // NON-NLS
165  return false;
166  }
167  }
168 
169  return true;
170  }
171 
177  public boolean deleteDatabase() {
178  File dbFile = new File(this.getFileNameWithPath());
179  return dbFile.delete();
180  }
181 
187  String getConnectionURL() {
188  StringBuilder url = new StringBuilder();
189  url.append(getJDBCBaseURI());
190  url.append(getFileNameWithPath());
191 
192  return url.toString();
193  }
194 
203  Connection getEphemeralConnection() {
204  if (!dbDirectoryExists()) {
205  return null;
206  }
207 
208  Connection conn;
209  try {
210  String url = getConnectionURL();
211  Class.forName(getDriver());
212  conn = DriverManager.getConnection(url);
213  } catch (ClassNotFoundException | SQLException ex) {
214  LOGGER.log(Level.SEVERE, "Failed to acquire ephemeral connection to sqlite.", ex); // NON-NLS
215  conn = null;
216  }
217  return conn;
218  }
219 
226  public boolean verifyConnection() {
227  Connection conn = getEphemeralConnection();
228  if (null == conn) {
229  return false;
230  }
231 
232  boolean result = CentralRepoDbUtil.executeValidationQuery(conn, VALIDATION_QUERY);
234  return result;
235  }
236 
243  public boolean verifyDatabaseSchema() {
244  Connection conn = getEphemeralConnection();
245  if (null == conn) {
246  return false;
247  }
248 
249  boolean result = CentralRepoDbUtil.schemaVersionIsSet(conn);
251  return result;
252  }
253 
254  boolean isChanged() {
255  String dbNameString = ModuleSettings.getConfigSetting("CentralRepository", "db.sqlite.dbName"); // NON-NLS
256  String dbDirectoryString = ModuleSettings.getConfigSetting("CentralRepository", "db.sqlite.dbDirectory"); // NON-NLS
257  String bulkThresholdString = ModuleSettings.getConfigSetting("CentralRepository", "db.sqlite.bulkThreshold"); // NON-NLS
258 
259  return !dbName.equals(dbNameString)
260  || !dbDirectory.equals(dbDirectoryString)
261  || !Integer.toString(bulkThreshold).equals(bulkThresholdString);
262  }
263 
267  public String getDbName() {
268  return dbName;
269  }
270 
276  public void setDbName(String dbName) throws CentralRepoException {
277  if (dbName == null || dbName.isEmpty()) {
278  throw new CentralRepoException("Invalid database file name. Cannot be null or empty."); // NON-NLS
279  } else if (!Pattern.matches(DB_NAMES_REGEX, dbName)) {
280  throw new CentralRepoException("Invalid database file name. Name must start with a lowercase letter and can only contain lowercase letters, numbers, and '_'."); // NON-NLS
281  }
282 
283  this.dbName = dbName;
284  }
285 
289  int getBulkThreshold() {
290  return bulkThreshold;
291  }
292 
296  void setBulkThreshold(int bulkThreshold) throws CentralRepoException {
297  if (bulkThreshold > 0) {
298  this.bulkThreshold = bulkThreshold;
299  } else {
300  throw new CentralRepoException("Invalid bulk threshold."); // NON-NLS
301  }
302  }
303 
307  public String getDbDirectory() {
308  return dbDirectory;
309  }
310 
318  public void setDbDirectory(String dbDirectory) throws CentralRepoException {
319  if (dbDirectory != null && !dbDirectory.isEmpty()) {
320  this.dbDirectory = dbDirectory;
321  } else {
322  throw new CentralRepoException("Invalid directory for sqlite database. Cannot empty"); // NON-NLS
323  }
324  }
325 
331  public String getFileNameWithPath() {
332  return getDbDirectory() + File.separator + getDbName();
333  }
334 
338  String getDriver() {
339  return JDBC_DRIVER;
340  }
341 
345  String getValidationQuery() {
346  return VALIDATION_QUERY;
347  }
348 
352  String getJDBCBaseURI() {
353  return JDBC_BASE_URI;
354  }
355 
356  @Override
358  if (dbFileExists()) {
359  if (verifyConnection()) {
360  if (verifyDatabaseSchema()) {
362  } else {
364  }
365  } else {
367  }
368  } else {
370  }
371  }
372 }
static synchronized String getConfigSetting(String moduleName, String settingName)
static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal)
static boolean executeValidationQuery(Connection conn, String validationQuery)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2021 Basis Technology. Generated on: Tue Jan 19 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.