Autopsy  4.11.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.centralRepoUpgradeFailed.message=Failed to upgrade central repository. It has been disabled."})
175  public static void upgradeDatabase() throws EamDbException {
176  if (!EamDb.isEnabled()) {
177  return;
178  }
179 
180  CoordinationService.Lock lock = null;
181  try {
182  EamDb db = EamDb.getInstance();
183 
184  // This may return null if locking isn't supported, which is fine. It will
185  // throw an exception if locking is supported but we can't get the lock
186  // (meaning the database is in use by another user)
187  lock = db.getExclusiveMultiUserDbLock();
188 
189  db.upgradeSchema();
190 
191  } catch (EamDbException | SQLException | IncompatibleCentralRepoException ex) {
192  LOGGER.log(Level.SEVERE, "Error updating central repository", ex);
193 
194  // Disable the central repo and clear the current settings.
195  try {
196  if (null != EamDb.getInstance()) {
198  }
199  } catch (EamDbException ex2) {
200  LOGGER.log(Level.SEVERE, "Error shutting down central repo connection pool", ex2);
201  }
204  String messageForDialog = Bundle.EamDbUtil_centralRepoUpgradeFailed_message();
205  if (ex instanceof IncompatibleCentralRepoException) {
206  messageForDialog = ex.getMessage() + "\n\n" + messageForDialog;
207  }
208  throw new EamDbException(messageForDialog);
209  } finally {
210  if (lock != null) {
211  try {
212  lock.release();
213  } catch (CoordinationServiceException ex) {
214  LOGGER.log(Level.SEVERE, "Error releasing database lock", ex);
215  }
216  }
217  }
218  }
219 
225  public static String getDefaultOrgName() {
226  return DEFAULT_ORG_NAME;
227  }
228 
236  public static boolean isDefaultOrg(EamOrganization org) {
237  return DEFAULT_ORG_NAME.equals(org.getName());
238  }
239 
247  static boolean insertDefaultOrganization(Connection conn) {
248  if (null == conn) {
249  return false;
250  }
251 
252  PreparedStatement preparedStatement = null;
253  String sql = "INSERT INTO organizations(org_name, poc_name, poc_email, poc_phone) VALUES (?, ?, ?, ?)";
254  try {
255  preparedStatement = conn.prepareStatement(sql);
256  preparedStatement.setString(1, DEFAULT_ORG_NAME);
257  preparedStatement.setString(2, "");
258  preparedStatement.setString(3, "");
259  preparedStatement.setString(4, "");
260  preparedStatement.executeUpdate();
261  } catch (SQLException ex) {
262  LOGGER.log(Level.SEVERE, "Error adding default organization", ex);
263  return false;
264  } finally {
265  EamDbUtil.closePreparedStatement(preparedStatement);
266  }
267 
268  return true;
269  }
270 
279  public static boolean allowUseOfCentralRepository() {
280  //In almost all situations EamDb.isEnabled() should be used instead of this method
281  //as EamDb.isEnabled() will call this method as well as checking that the selected type of central repository is not DISABLED
282  return Boolean.parseBoolean(ModuleSettings.getConfigSetting(CENTRAL_REPO_NAME, CENTRAL_REPO_USE_KEY));
283  }
284 
292  public static void setUseCentralRepo(boolean centralRepoCheckBoxIsSelected) {
293  ModuleSettings.setConfigSetting(CENTRAL_REPO_NAME, CENTRAL_REPO_USE_KEY, Boolean.toString(centralRepoCheckBoxIsSelected));
294  }
295 
302  public static boolean executeValidationQuery(Connection conn, String validationQuery) {
303  if (null == conn) {
304  return false;
305  }
306 
307  ResultSet resultSet = null;
308  try {
309  Statement tester = conn.createStatement();
310  resultSet = tester.executeQuery(validationQuery);
311  if (resultSet.next()) {
312  return true;
313  }
314  } catch (SQLException ex) {
315  return false;
316  } finally {
317  EamDbUtil.closeResultSet(resultSet);
318  }
319 
320  return false;
321  }
322 
331  return type.getDbTableName() + "_instances";
332  }
333 
342  return "reference_" + type.getDbTableName();
343  }
344 
354  @Deprecated
355  public static void closePreparedStatement(PreparedStatement preparedStatement) {
356  closeStatement(preparedStatement);
357  }
358 
359 }
static String correlationTypeToInstanceTableName(CorrelationAttributeInstance.Type type)
Definition: EamDbUtil.java:330
static boolean executeValidationQuery(Connection conn, String validationQuery)
Definition: EamDbUtil.java:302
static boolean isDefaultOrg(EamOrganization org)
Definition: EamDbUtil.java:236
static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal)
static void setUseCentralRepo(boolean centralRepoCheckBoxIsSelected)
Definition: EamDbUtil.java:292
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:341
static void closePreparedStatement(PreparedStatement preparedStatement)
Definition: EamDbUtil.java:355

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