Autopsy  4.7.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;
33 
37 public class EamDbUtil {
38 
39  private final static Logger LOGGER = Logger.getLogger(EamDbUtil.class.getName());
40  private static final String CENTRAL_REPO_NAME = "CentralRepository";
41  private static final String CENTRAL_REPO_USE_KEY = "db.useCentralRepo";
42  private static final String DEFAULT_ORG_NAME = "Not Specified";
43 
51  public static void closeStatement(Statement statement) {
52  if (null != statement) {
53  try {
54  statement.close();
55  } catch (SQLException ex) {
56  LOGGER.log(Level.SEVERE, "Error closing Statement.", ex);
57  }
58  }
59  }
60 
68  public static void closeResultSet(ResultSet resultSet) {
69  if (null != resultSet) {
70  try {
71  resultSet.close();
72  } catch (SQLException ex) {
73  LOGGER.log(Level.SEVERE, "Error closing ResultSet.", ex);
74  }
75  }
76  }
77 
85  public static void closeConnection(Connection conn) {
86  if (null != conn) {
87  try {
88  conn.close();
89  } catch (SQLException ex) {
90  LOGGER.log(Level.SEVERE, "Error closing Connection.", ex);
91  }
92  }
93  }
94 
102  public static boolean insertDefaultCorrelationTypes(Connection conn) {
103  PreparedStatement preparedStatement = null;
104  String sql = "INSERT INTO correlation_types(id, display_name, db_table_name, supported, enabled) VALUES (?, ?, ?, ?, ?)";
105 
106  try {
107  List<CorrelationAttribute.Type> DEFAULT_CORRELATION_TYPES = CorrelationAttribute.getDefaultCorrelationTypes();
108  preparedStatement = conn.prepareStatement(sql);
109  for (CorrelationAttribute.Type newType : DEFAULT_CORRELATION_TYPES) {
110  preparedStatement.setInt(1, newType.getId());
111  preparedStatement.setString(2, newType.getDisplayName());
112  preparedStatement.setString(3, newType.getDbTableName());
113  preparedStatement.setInt(4, newType.isSupported() ? 1 : 0);
114  preparedStatement.setInt(5, newType.isEnabled() ? 1 : 0);
115 
116  preparedStatement.addBatch();
117  }
118  preparedStatement.executeBatch();
119  } catch (EamDbException | SQLException ex) {
120  LOGGER.log(Level.SEVERE, "Error inserting default correlation types.", ex); // NON-NLS
121  return false;
122  } finally {
123  EamDbUtil.closePreparedStatement(preparedStatement);
124  }
125  return true;
126  }
127 
138  static boolean updateSchemaVersion(Connection conn) {
139 
140  Statement statement;
141  ResultSet resultSet;
142  String sql = "INSERT INTO db_info (name, value) VALUES (?, ?)";
143  try {
144  statement = conn.createStatement();
145  resultSet = statement.executeQuery("SELECT id FROM db_info WHERE name='SCHEMA_VERSION'");
146  if (resultSet.next()) {
147  int id = resultSet.getInt("id");
148  statement.execute("UPDATE db_info SET value=" + CURRENT_DB_SCHEMA_VERSION.getMajor() + " WHERE id=" + id);
149  } else {
150  statement.execute("INSERT INTO db_info (name, value) VALUES ('SCHEMA_VERSION', '" + CURRENT_DB_SCHEMA_VERSION.getMajor() + "')");
151  }
152 
153  resultSet = statement.executeQuery("SELECT id FROM db_info WHERE name='SCHEMA_MINOR_VERSION'");
154  if (resultSet.next()) {
155  int id = resultSet.getInt("id");
156  statement.execute("UPDATE db_info SET value=" + CURRENT_DB_SCHEMA_VERSION.getMinor() + " WHERE id=" + id);
157  } else {
158  statement.execute("INSERT INTO db_info (name, value) VALUES ('SCHEMA_MINOR_VERSION', '" + CURRENT_DB_SCHEMA_VERSION.getMinor() + "')");
159  }
160  } catch (SQLException ex) {
161  LOGGER.log(Level.SEVERE, "Error adding schema version to db_info.", ex);
162  return false;
163  }
164 
165  return true;
166  }
167 
173  public static boolean schemaVersionIsSet(Connection conn) {
174  if (null == conn) {
175  return false;
176  }
177 
178  ResultSet resultSet = null;
179  try {
180  Statement tester = conn.createStatement();
181  String sql = "SELECT value FROM db_info WHERE name='SCHEMA_VERSION'";
182  resultSet = tester.executeQuery(sql);
183  if (resultSet.next()) {
184  String value = resultSet.getString("value");
185  }
186  } catch (SQLException ex) {
187  return false;
188  } finally {
189  EamDbUtil.closeResultSet(resultSet);
190  }
191  return true;
192  }
193 
201  public static boolean upgradeDatabase() {
202  if (!EamDb.isEnabled()) {
203  return true;
204  }
205 
206  CoordinationService.Lock lock = null;
207  try {
208  EamDb db = EamDb.getInstance();
209 
210  // This may return null if locking isn't supported, which is fine. It will
211  // throw an exception if locking is supported but we can't get the lock
212  // (meaning the database is in use by another user)
213  lock = db.getExclusiveMultiUserDbLock();
214 
215  db.upgradeSchema();
216 
217  } catch (EamDbException | SQLException ex) {
218  LOGGER.log(Level.SEVERE, "Error updating central repository", ex);
219 
220  // Disable the central repo and clear the current settings.
221  try{
222  if (null != EamDb.getInstance()) {
224  }
225  } catch (EamDbException ex2){
226  LOGGER.log(Level.SEVERE, "Error shutting down central repo connection pool", ex);
227  }
228  setUseCentralRepo(false);
231 
232  return false;
233  } finally {
234  if(lock != null){
235  try{
236  lock.release();
237  } catch (CoordinationServiceException ex){
238  LOGGER.log(Level.SEVERE, "Error releasing database lock", ex);
239  }
240  }
241  }
242  return true;
243  }
244 
250  public static String getDefaultOrgName() {
251  return DEFAULT_ORG_NAME;
252  }
253 
260  public static boolean isDefaultOrg(EamOrganization org) {
261  return DEFAULT_ORG_NAME.equals(org.getName());
262  }
263 
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 
300  public static boolean useCentralRepo() {
301  return Boolean.parseBoolean(ModuleSettings.getConfigSetting(CENTRAL_REPO_NAME, CENTRAL_REPO_USE_KEY));
302  }
303 
311  public static void setUseCentralRepo(boolean centralRepoCheckBoxIsSelected) {
312  ModuleSettings.setConfigSetting(CENTRAL_REPO_NAME, CENTRAL_REPO_USE_KEY, Boolean.toString(centralRepoCheckBoxIsSelected));
313  }
314 
321  public static boolean executeValidationQuery(Connection conn, String validationQuery) {
322  if (null == conn) {
323  return false;
324  }
325 
326  ResultSet resultSet = null;
327  try {
328  Statement tester = conn.createStatement();
329  resultSet = tester.executeQuery(validationQuery);
330  if (resultSet.next()) {
331  return true;
332  }
333  } catch (SQLException ex) {
334  return false;
335  } finally {
336  EamDbUtil.closeResultSet(resultSet);
337  }
338 
339  return false;
340  }
341 
350  return type.getDbTableName() + "_instances";
351  }
352 
361  return "reference_" + type.getDbTableName();
362  }
363 
373  @Deprecated
374  public static void closePreparedStatement(PreparedStatement preparedStatement) {
375  closeStatement(preparedStatement);
376  }
377 
378 }
static boolean executeValidationQuery(Connection conn, String validationQuery)
Definition: EamDbUtil.java:321
static boolean isDefaultOrg(EamOrganization org)
Definition: EamDbUtil.java:260
static String correlationTypeToReferenceTableName(CorrelationAttribute.Type type)
Definition: EamDbUtil.java:360
static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal)
static void setUseCentralRepo(boolean centralRepoCheckBoxIsSelected)
Definition: EamDbUtil.java:311
static boolean insertDefaultCorrelationTypes(Connection conn)
Definition: EamDbUtil.java:102
static String correlationTypeToInstanceTableName(CorrelationAttribute.Type type)
Definition: EamDbUtil.java:349
static String getConfigSetting(String moduleName, String settingName)
static final CaseDbSchemaVersionNumber CURRENT_DB_SCHEMA_VERSION
Definition: EamDb.java:36
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static void closePreparedStatement(PreparedStatement preparedStatement)
Definition: EamDbUtil.java:374

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