Autopsy  4.13.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 
185  //get connection
186  try {
187  try {
188  db = EamDb.getInstance();
189  } catch (EamDbException ex) {
190  LOGGER.log(Level.SEVERE, "Error updating central repository, unable to make connection", ex);
191  throw new EamDbException("Error updating central repository, unable to make connection", Bundle.EamDbUtil_centralRepoConnectionFailed_message() + Bundle.EamDbUtil_centralRepoDisabled_message(), ex);
192  }
193  //get lock necessary for upgrade
194  if (db != null) {
195  try {
196  // This may return null if locking isn't supported, which is fine. It will
197  // throw an exception if locking is supported but we can't get the lock
198  // (meaning the database is in use by another user)
199  lock = db.getExclusiveMultiUserDbLock();
200  //perform upgrade
201  } catch (EamDbException ex) {
202  LOGGER.log(Level.SEVERE, "Error updating central repository, unable to acquire exclusive lock", ex);
203  throw new EamDbException("Error updating central repository, unable to acquire exclusive lock", Bundle.EamDbUtil_exclusiveLockAquisitionFailure_message() + Bundle.EamDbUtil_centralRepoDisabled_message(), ex);
204  }
205 
206  try {
207  db.upgradeSchema();
208  } catch (EamDbException ex) {
209  LOGGER.log(Level.SEVERE, "Error updating central repository", ex);
210  throw new EamDbException("Error updating central repository", ex.getUserMessage() + Bundle.EamDbUtil_centralRepoDisabled_message(), ex);
211  } catch (SQLException ex) {
212  LOGGER.log(Level.SEVERE, "Error updating central repository", ex);
213  throw new EamDbException("Error updating central repository", Bundle.EamDbUtil_centralRepoUpgradeFailed_message() + Bundle.EamDbUtil_centralRepoDisabled_message(), ex);
214  } catch (IncompatibleCentralRepoException ex) {
215  LOGGER.log(Level.SEVERE, "Error updating central repository", ex);
216  throw new EamDbException("Error updating central repository", ex.getMessage() + "\n\n" + Bundle.EamDbUtil_centralRepoUpgradeFailed_message() + Bundle.EamDbUtil_centralRepoDisabled_message(), ex);
217  } finally {
218  if (lock != null) {
219  try {
220  lock.release();
221  } catch (CoordinationServiceException ex) {
222  LOGGER.log(Level.SEVERE, "Error releasing database lock", ex);
223  }
224  }
225  }
226  } else {
227  throw new EamDbException("Unable to connect to database", Bundle.EamDbUtil_centralRepoConnectionFailed_message() + Bundle.EamDbUtil_centralRepoDisabled_message());
228  }
229  } catch (EamDbException ex) {
230  // Disable the central repo and clear the current settings.
231  try {
232  if (null != EamDb.getInstance()) {
234  }
235  } catch (EamDbException ex2) {
236  LOGGER.log(Level.SEVERE, "Error shutting down central repo connection pool", ex2);
237  }
240  throw ex;
241  }
242  }
243 
249  public static String getDefaultOrgName() {
250  return DEFAULT_ORG_NAME;
251  }
252 
260  public static boolean isDefaultOrg(EamOrganization org) {
261  return DEFAULT_ORG_NAME.equals(org.getName());
262  }
263 
271  static boolean insertDefaultOrganization(Connection conn) {
272  if (null == conn) {
273  return false;
274  }
275 
276  PreparedStatement preparedStatement = null;
277  String sql = "INSERT INTO organizations(org_name, poc_name, poc_email, poc_phone) VALUES (?, ?, ?, ?)";
278  try {
279  preparedStatement = conn.prepareStatement(sql);
280  preparedStatement.setString(1, DEFAULT_ORG_NAME);
281  preparedStatement.setString(2, "");
282  preparedStatement.setString(3, "");
283  preparedStatement.setString(4, "");
284  preparedStatement.executeUpdate();
285  } catch (SQLException ex) {
286  LOGGER.log(Level.SEVERE, "Error adding default organization", ex);
287  return false;
288  } finally {
289  EamDbUtil.closePreparedStatement(preparedStatement);
290  }
291 
292  return true;
293  }
294 
303  public static boolean allowUseOfCentralRepository() {
304  //In almost all situations EamDb.isEnabled() should be used instead of this method
305  //as EamDb.isEnabled() will call this method as well as checking that the selected type of central repository is not DISABLED
306  return Boolean.parseBoolean(ModuleSettings.getConfigSetting(CENTRAL_REPO_NAME, CENTRAL_REPO_USE_KEY));
307  }
308 
316  public static void setUseCentralRepo(boolean centralRepoCheckBoxIsSelected) {
317  ModuleSettings.setConfigSetting(CENTRAL_REPO_NAME, CENTRAL_REPO_USE_KEY, Boolean.toString(centralRepoCheckBoxIsSelected));
318  }
319 
326  public static boolean executeValidationQuery(Connection conn, String validationQuery) {
327  if (null == conn) {
328  return false;
329  }
330 
331  ResultSet resultSet = null;
332  try {
333  Statement tester = conn.createStatement();
334  resultSet = tester.executeQuery(validationQuery);
335  if (resultSet.next()) {
336  return true;
337  }
338  } catch (SQLException ex) {
339  return false;
340  } finally {
341  EamDbUtil.closeResultSet(resultSet);
342  }
343 
344  return false;
345  }
346 
355  return type.getDbTableName() + "_instances";
356  }
357 
366  return "reference_" + type.getDbTableName();
367  }
368 
378  @Deprecated
379  public static void closePreparedStatement(PreparedStatement preparedStatement) {
380  closeStatement(preparedStatement);
381  }
382 
383 }
static String correlationTypeToInstanceTableName(CorrelationAttributeInstance.Type type)
Definition: EamDbUtil.java:354
static boolean executeValidationQuery(Connection conn, String validationQuery)
Definition: EamDbUtil.java:326
static boolean isDefaultOrg(EamOrganization org)
Definition: EamDbUtil.java:260
static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal)
static void setUseCentralRepo(boolean centralRepoCheckBoxIsSelected)
Definition: EamDbUtil.java:316
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:365
static void closePreparedStatement(PreparedStatement preparedStatement)
Definition: EamDbUtil.java:379

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