Autopsy  4.9.1
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 static org.sleuthkit.autopsy.centralrepository.datamodel.AbstractSqlEamDb.CURRENT_DB_SCHEMA_VERSION;
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 {
108  preparedStatement = conn.prepareStatement(sql);
109  for (CorrelationAttributeInstance.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 
135  static void updateSchemaVersion(Connection conn) throws SQLException {
136  try (Statement statement = conn.createStatement()) {
137  statement.execute("UPDATE db_info SET value = '" + CURRENT_DB_SCHEMA_VERSION.getMajor() + "' WHERE name = '" + AbstractSqlEamDb.SCHEMA_MAJOR_VERSION_KEY + "'");
138  statement.execute("UPDATE db_info SET value = '" + CURRENT_DB_SCHEMA_VERSION.getMinor() + "' WHERE name = '" + AbstractSqlEamDb.SCHEMA_MINOR_VERSION_KEY + "'");
139  }
140  }
141 
147  public static boolean schemaVersionIsSet(Connection conn) {
148  if (null == conn) {
149  return false;
150  }
151 
152  ResultSet resultSet = null;
153  try {
154  Statement tester = conn.createStatement();
155  String sql = "SELECT value FROM db_info WHERE name='SCHEMA_VERSION'";
156  resultSet = tester.executeQuery(sql);
157  if (resultSet.next()) {
158  String value = resultSet.getString("value");
159  }
160  } catch (SQLException ex) {
161  return false;
162  } finally {
163  EamDbUtil.closeResultSet(resultSet);
164  }
165  return true;
166  }
167 
175  public static boolean upgradeDatabase() {
176  if (!EamDb.isEnabled()) {
177  return true;
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 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", ex);
201  }
202  setUseCentralRepo(false);
205 
206  return false;
207  } finally {
208  if (lock != null) {
209  try {
210  lock.release();
211  } catch (CoordinationServiceException ex) {
212  LOGGER.log(Level.SEVERE, "Error releasing database lock", ex);
213  }
214  }
215  }
216  return true;
217  }
218 
224  public static String getDefaultOrgName() {
225  return DEFAULT_ORG_NAME;
226  }
227 
235  public static boolean isDefaultOrg(EamOrganization org) {
236  return DEFAULT_ORG_NAME.equals(org.getName());
237  }
238 
246  static boolean insertDefaultOrganization(Connection conn) {
247  if (null == conn) {
248  return false;
249  }
250 
251  PreparedStatement preparedStatement = null;
252  String sql = "INSERT INTO organizations(org_name, poc_name, poc_email, poc_phone) VALUES (?, ?, ?, ?)";
253  try {
254  preparedStatement = conn.prepareStatement(sql);
255  preparedStatement.setString(1, DEFAULT_ORG_NAME);
256  preparedStatement.setString(2, "");
257  preparedStatement.setString(3, "");
258  preparedStatement.setString(4, "");
259  preparedStatement.executeUpdate();
260  } catch (SQLException ex) {
261  LOGGER.log(Level.SEVERE, "Error adding default organization", ex);
262  return false;
263  } finally {
264  EamDbUtil.closePreparedStatement(preparedStatement);
265  }
266 
267  return true;
268  }
269 
276  public static boolean useCentralRepo() {
277  return Boolean.parseBoolean(ModuleSettings.getConfigSetting(CENTRAL_REPO_NAME, CENTRAL_REPO_USE_KEY));
278  }
279 
287  public static void setUseCentralRepo(boolean centralRepoCheckBoxIsSelected) {
288  ModuleSettings.setConfigSetting(CENTRAL_REPO_NAME, CENTRAL_REPO_USE_KEY, Boolean.toString(centralRepoCheckBoxIsSelected));
289  }
290 
297  public static boolean executeValidationQuery(Connection conn, String validationQuery) {
298  if (null == conn) {
299  return false;
300  }
301 
302  ResultSet resultSet = null;
303  try {
304  Statement tester = conn.createStatement();
305  resultSet = tester.executeQuery(validationQuery);
306  if (resultSet.next()) {
307  return true;
308  }
309  } catch (SQLException ex) {
310  return false;
311  } finally {
312  EamDbUtil.closeResultSet(resultSet);
313  }
314 
315  return false;
316  }
317 
326  return type.getDbTableName() + "_instances";
327  }
328 
337  return "reference_" + type.getDbTableName();
338  }
339 
349  @Deprecated
350  public static void closePreparedStatement(PreparedStatement preparedStatement) {
351  closeStatement(preparedStatement);
352  }
353 
354 }
static String correlationTypeToInstanceTableName(CorrelationAttributeInstance.Type type)
Definition: EamDbUtil.java:325
static boolean executeValidationQuery(Connection conn, String validationQuery)
Definition: EamDbUtil.java:297
static boolean isDefaultOrg(EamOrganization org)
Definition: EamDbUtil.java:235
static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal)
static void setUseCentralRepo(boolean centralRepoCheckBoxIsSelected)
Definition: EamDbUtil.java:287
static boolean insertDefaultCorrelationTypes(Connection conn)
Definition: EamDbUtil.java:102
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:336
static void closePreparedStatement(PreparedStatement preparedStatement)
Definition: EamDbUtil.java:350

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