Autopsy  4.12.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
EamDbUtil.java
Go to the documentation of this file.
1 /*
2  * Central Repository
3  *
4  * Copyright 2015-2017 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.sql.Connection;
22 import java.sql.PreparedStatement;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.sql.Statement;
26 import java.util.List;
27 import java.util.logging.Level;
28 import org.openide.util.NbBundle.Messages;
33 import static org.sleuthkit.autopsy.centralrepository.datamodel.AbstractSqlEamDb.SOFTWARE_CR_DB_SCHEMA_VERSION;
34 
38 public class EamDbUtil {
39 
40  private final static Logger LOGGER = Logger.getLogger(EamDbUtil.class.getName());
41  private static final String CENTRAL_REPO_NAME = "CentralRepository";
42  private static final String CENTRAL_REPO_USE_KEY = "db.useCentralRepo";
43  private static final String DEFAULT_ORG_NAME = "Not Specified";
44 
52  public static void closeStatement(Statement statement) {
53  if (null != statement) {
54  try {
55  statement.close();
56  } catch (SQLException ex) {
57  LOGGER.log(Level.SEVERE, "Error closing Statement.", ex);
58  }
59  }
60  }
61 
69  public static void closeResultSet(ResultSet resultSet) {
70  if (null != resultSet) {
71  try {
72  resultSet.close();
73  } catch (SQLException ex) {
74  LOGGER.log(Level.SEVERE, "Error closing ResultSet.", ex);
75  }
76  }
77  }
78 
86  public static void closeConnection(Connection conn) {
87  if (null != conn) {
88  try {
89  conn.close();
90  } catch (SQLException ex) {
91  LOGGER.log(Level.SEVERE, "Error closing Connection.", ex);
92  }
93  }
94  }
95 
103  public static boolean insertDefaultCorrelationTypes(Connection conn) {
104  PreparedStatement preparedStatement = null;
105  String sql = "INSERT INTO correlation_types(id, display_name, db_table_name, supported, enabled) VALUES (?, ?, ?, ?, ?)";
106 
107  try {
109  preparedStatement = conn.prepareStatement(sql);
110  for (CorrelationAttributeInstance.Type newType : DEFAULT_CORRELATION_TYPES) {
111  preparedStatement.setInt(1, newType.getId());
112  preparedStatement.setString(2, newType.getDisplayName());
113  preparedStatement.setString(3, newType.getDbTableName());
114  preparedStatement.setInt(4, newType.isSupported() ? 1 : 0);
115  preparedStatement.setInt(5, newType.isEnabled() ? 1 : 0);
116 
117  preparedStatement.addBatch();
118  }
119  preparedStatement.executeBatch();
120  } catch (EamDbException | SQLException ex) {
121  LOGGER.log(Level.SEVERE, "Error inserting default correlation types.", ex); // NON-NLS
122  return false;
123  } finally {
124  EamDbUtil.closePreparedStatement(preparedStatement);
125  }
126  return true;
127  }
128 
136  static void updateSchemaVersion(Connection conn) throws SQLException {
137  try (Statement statement = conn.createStatement()) {
138  statement.execute("UPDATE db_info SET value = '" + SOFTWARE_CR_DB_SCHEMA_VERSION.getMajor() + "' WHERE name = '" + AbstractSqlEamDb.SCHEMA_MAJOR_VERSION_KEY + "'");
139  statement.execute("UPDATE db_info SET value = '" + SOFTWARE_CR_DB_SCHEMA_VERSION.getMinor() + "' WHERE name = '" + AbstractSqlEamDb.SCHEMA_MINOR_VERSION_KEY + "'");
140  }
141  }
142 
148  public static boolean schemaVersionIsSet(Connection conn) {
149  if (null == conn) {
150  return false;
151  }
152 
153  ResultSet resultSet = null;
154  try {
155  Statement tester = conn.createStatement();
156  String sql = "SELECT value FROM db_info WHERE name='SCHEMA_VERSION'";
157  resultSet = tester.executeQuery(sql);
158  if (resultSet.next()) {
159  String value = resultSet.getString("value");
160  }
161  } catch (SQLException ex) {
162  return false;
163  } finally {
164  EamDbUtil.closeResultSet(resultSet);
165  }
166  return true;
167  }
168 
174  @Messages({"EamDbUtil.centralRepoDisabled.message= The Central Repository has been disabled.",
175  "EamDbUtil.centralRepoUpgradeFailed.message=Failed to upgrade Central Repository.",
176  "EamDbUtil.centralRepoConnectionFailed.message=Unable to connect to Central Repository.",
177  "EamDbUtil.exclusiveLockAquisitionFailure.message=Unable to acquire exclusive lock for Central Repository."})
178  public static void upgradeDatabase() throws EamDbException {
179  if (!EamDb.isEnabled()) {
180  return;
181  }
182  EamDb db = null;
183  CoordinationService.Lock lock = null;
184  String messageForDialog = "";
185  //get connection
186  try {
187  db = EamDb.getInstance();
188  } catch (EamDbException ex) {
189  LOGGER.log(Level.SEVERE, "Error updating central repository, unable to make connection", ex);
190  messageForDialog = Bundle.EamDbUtil_centralRepoConnectionFailed_message() + Bundle.EamDbUtil_centralRepoDisabled_message();
191  }
192  //get lock necessary for upgrade
193  if (db != null) {
194  try {
195  // This may return null if locking isn't supported, which is fine. It will
196  // throw an exception if locking is supported but we can't get the lock
197  // (meaning the database is in use by another user)
198  lock = db.getExclusiveMultiUserDbLock();
199  //perform upgrade
200  try {
201  db.upgradeSchema();
202  } catch (EamDbException | SQLException | IncompatibleCentralRepoException ex) {
203  LOGGER.log(Level.SEVERE, "Error updating central repository", ex);
204  messageForDialog = Bundle.EamDbUtil_centralRepoUpgradeFailed_message() + Bundle.EamDbUtil_centralRepoDisabled_message();
205  if (ex instanceof IncompatibleCentralRepoException) {
206  messageForDialog = ex.getMessage() + "\n\n" + messageForDialog;
207  } else if (ex instanceof EamDbException) {
208  messageForDialog = ex.getMessage() + Bundle.EamDbUtil_centralRepoDisabled_message();
209  }
210  } finally {
211  if (lock != null) {
212  try {
213  lock.release();
214  } catch (CoordinationServiceException ex) {
215  LOGGER.log(Level.SEVERE, "Error releasing database lock", ex);
216  }
217  }
218  }
219  } catch (EamDbException ex) {
220  LOGGER.log(Level.SEVERE, "Error updating central repository, unable to acquire exclusive lock", ex);
221  messageForDialog = Bundle.EamDbUtil_exclusiveLockAquisitionFailure_message() + Bundle.EamDbUtil_centralRepoDisabled_message();
222  }
223 
224  } else {
225  messageForDialog = Bundle.EamDbUtil_centralRepoConnectionFailed_message() + Bundle.EamDbUtil_centralRepoDisabled_message();
226  }
227  // Disable the central repo and clear the current settings.
228  if (!messageForDialog.isEmpty()) {
229  try {
230  if (null != EamDb.getInstance()) {
232  }
233  } catch (EamDbException ex2) {
234  LOGGER.log(Level.SEVERE, "Error shutting down central repo connection pool", ex2);
235  }
238 
239  throw new EamDbException(messageForDialog);
240  }
241  }
242 
248  public static String getDefaultOrgName() {
249  return DEFAULT_ORG_NAME;
250  }
251 
259  public static boolean isDefaultOrg(EamOrganization org) {
260  return DEFAULT_ORG_NAME.equals(org.getName());
261  }
262 
270  static boolean insertDefaultOrganization(Connection conn) {
271  if (null == conn) {
272  return false;
273  }
274 
275  PreparedStatement preparedStatement = null;
276  String sql = "INSERT INTO organizations(org_name, poc_name, poc_email, poc_phone) VALUES (?, ?, ?, ?)";
277  try {
278  preparedStatement = conn.prepareStatement(sql);
279  preparedStatement.setString(1, DEFAULT_ORG_NAME);
280  preparedStatement.setString(2, "");
281  preparedStatement.setString(3, "");
282  preparedStatement.setString(4, "");
283  preparedStatement.executeUpdate();
284  } catch (SQLException ex) {
285  LOGGER.log(Level.SEVERE, "Error adding default organization", ex);
286  return false;
287  } finally {
288  EamDbUtil.closePreparedStatement(preparedStatement);
289  }
290 
291  return true;
292  }
293 
302  public static boolean allowUseOfCentralRepository() {
303  //In almost all situations EamDb.isEnabled() should be used instead of this method
304  //as EamDb.isEnabled() will call this method as well as checking that the selected type of central repository is not DISABLED
305  return Boolean.parseBoolean(ModuleSettings.getConfigSetting(CENTRAL_REPO_NAME, CENTRAL_REPO_USE_KEY));
306  }
307 
315  public static void setUseCentralRepo(boolean centralRepoCheckBoxIsSelected) {
316  ModuleSettings.setConfigSetting(CENTRAL_REPO_NAME, CENTRAL_REPO_USE_KEY, Boolean.toString(centralRepoCheckBoxIsSelected));
317  }
318 
325  public static boolean executeValidationQuery(Connection conn, String validationQuery) {
326  if (null == conn) {
327  return false;
328  }
329 
330  ResultSet resultSet = null;
331  try {
332  Statement tester = conn.createStatement();
333  resultSet = tester.executeQuery(validationQuery);
334  if (resultSet.next()) {
335  return true;
336  }
337  } catch (SQLException ex) {
338  return false;
339  } finally {
340  EamDbUtil.closeResultSet(resultSet);
341  }
342 
343  return false;
344  }
345 
354  return type.getDbTableName() + "_instances";
355  }
356 
365  return "reference_" + type.getDbTableName();
366  }
367 
377  @Deprecated
378  public static void closePreparedStatement(PreparedStatement preparedStatement) {
379  closeStatement(preparedStatement);
380  }
381 
382 }
static String correlationTypeToInstanceTableName(CorrelationAttributeInstance.Type type)
Definition: EamDbUtil.java:353
static boolean executeValidationQuery(Connection conn, String validationQuery)
Definition: EamDbUtil.java:325
static boolean isDefaultOrg(EamOrganization org)
Definition: EamDbUtil.java:259
static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal)
static void setUseCentralRepo(boolean centralRepoCheckBoxIsSelected)
Definition: EamDbUtil.java:315
static boolean insertDefaultCorrelationTypes(Connection conn)
Definition: EamDbUtil.java:103
static String getConfigSetting(String moduleName, String settingName)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static String correlationTypeToReferenceTableName(CorrelationAttributeInstance.Type type)
Definition: EamDbUtil.java:364
static void closePreparedStatement(PreparedStatement preparedStatement)
Definition: EamDbUtil.java:378

Copyright © 2012-2018 Basis Technology. Generated on: Wed Sep 18 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.