Autopsy  4.19.2
Graphical digital forensics platform for The Sleuth Kit and other tools.
CentralRepoDbUtil.java
Go to the documentation of this file.
1 /*
2  * Central Repository
3  *
4  * Copyright 2015-2020 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.ArrayList;
27 import java.util.List;
28 import java.util.logging.Level;
29 import javax.swing.SwingUtilities;
30 import org.openide.windows.TopComponent;
31 import org.openide.windows.WindowManager;
34 import static org.sleuthkit.autopsy.centralrepository.datamodel.RdbmsCentralRepo.SOFTWARE_CR_DB_SCHEMA_VERSION;
35 
39 public class CentralRepoDbUtil {
40 
41  private final static Logger LOGGER = Logger.getLogger(CentralRepoDbUtil.class.getName());
42  private static final String CENTRAL_REPO_NAME = "CentralRepository";
43  private static final String CENTRAL_REPO_USE_KEY = "db.useCentralRepo";
44  private static final String DEFAULT_ORG_NAME = "Not Specified";
45 
53  public static void closeStatement(Statement statement) {
54  if (null != statement) {
55  try {
56  statement.close();
57  } catch (SQLException ex) {
58  LOGGER.log(Level.SEVERE, "Error closing Statement.", ex);
59  }
60  }
61  }
62 
70  public static void closeResultSet(ResultSet resultSet) {
71  if (null != resultSet) {
72  try {
73  resultSet.close();
74  } catch (SQLException ex) {
75  LOGGER.log(Level.SEVERE, "Error closing ResultSet.", ex);
76  }
77  }
78  }
79 
87  public static void closeConnection(Connection conn) {
88  if (null != conn) {
89  try {
90  conn.close();
91  } catch (SQLException ex) {
92  LOGGER.log(Level.SEVERE, "Error closing Connection.", ex);
93  }
94  }
95  }
96 
104  public static boolean insertDefaultCorrelationTypes(Connection conn) {
105  PreparedStatement preparedStatement = null;
106  String sql = "INSERT INTO correlation_types(id, display_name, db_table_name, supported, enabled) VALUES (?, ?, ?, ?, ?)";
107 
108  try {
110  preparedStatement = conn.prepareStatement(sql);
111  for (CorrelationAttributeInstance.Type newType : DEFAULT_CORRELATION_TYPES) {
112  preparedStatement.setInt(1, newType.getId());
113  preparedStatement.setString(2, newType.getDisplayName());
114  preparedStatement.setString(3, newType.getDbTableName());
115  preparedStatement.setInt(4, newType.isSupported() ? 1 : 0);
116  preparedStatement.setInt(5, newType.isEnabled() ? 1 : 0);
117 
118  preparedStatement.addBatch();
119  }
120  preparedStatement.executeBatch();
121  } catch (CentralRepoException | SQLException ex) {
122  LOGGER.log(Level.SEVERE, "Error inserting default correlation types.", ex); // NON-NLS
123  return false;
124  } finally {
125  CentralRepoDbUtil.closePreparedStatement(preparedStatement);
126  }
127  return true;
128  }
129 
137  public static void insertCorrelationType(Connection conn, CorrelationAttributeInstance.Type correlationType) throws SQLException {
138 
139  String sql = "INSERT INTO correlation_types(id, display_name, db_table_name, supported, enabled) VALUES (?, ?, ?, ?, ?)";
140  try (PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
141 
142  preparedStatement.setInt(1, correlationType.getId());
143  preparedStatement.setString(2, correlationType.getDisplayName());
144  preparedStatement.setString(3, correlationType.getDbTableName());
145  preparedStatement.setInt(4, correlationType.isSupported() ? 1 : 0);
146  preparedStatement.setInt(5, correlationType.isEnabled() ? 1 : 0);
147 
148  preparedStatement.execute();
149  }
150  }
151 
159  static void updateSchemaVersion(Connection conn) throws SQLException {
160  try (Statement statement = conn.createStatement()) {
161  statement.execute("UPDATE db_info SET value = '" + SOFTWARE_CR_DB_SCHEMA_VERSION.getMajor() + "' WHERE name = '" + RdbmsCentralRepo.SCHEMA_MAJOR_VERSION_KEY + "'");
162  statement.execute("UPDATE db_info SET value = '" + SOFTWARE_CR_DB_SCHEMA_VERSION.getMinor() + "' WHERE name = '" + RdbmsCentralRepo.SCHEMA_MINOR_VERSION_KEY + "'");
163  }
164  }
165 
171  public static boolean schemaVersionIsSet(Connection conn) {
172  if (null == conn) {
173  return false;
174  }
175 
176  ResultSet resultSet = null;
177  try {
178  Statement tester = conn.createStatement();
179  String sql = "SELECT value FROM db_info WHERE name='SCHEMA_VERSION'";
180  resultSet = tester.executeQuery(sql);
181  if (resultSet.next()) {
182  String value = resultSet.getString("value");
183  }
184  } catch (SQLException ex) {
185  return false;
186  } finally {
188  }
189  return true;
190  }
191 
197  public static String getDefaultOrgName() {
198  return DEFAULT_ORG_NAME;
199  }
200 
208  public static boolean isDefaultOrg(CentralRepoOrganization org) {
209  return DEFAULT_ORG_NAME.equals(org.getName());
210  }
211 
219  static boolean insertDefaultOrganization(Connection conn) {
220  if (null == conn) {
221  return false;
222  }
223 
224  PreparedStatement preparedStatement = null;
225  String sql = "INSERT INTO organizations(org_name, poc_name, poc_email, poc_phone) VALUES (?, ?, ?, ?)";
226  try {
227  preparedStatement = conn.prepareStatement(sql);
228  preparedStatement.setString(1, DEFAULT_ORG_NAME);
229  preparedStatement.setString(2, "");
230  preparedStatement.setString(3, "");
231  preparedStatement.setString(4, "");
232  preparedStatement.executeUpdate();
233  } catch (SQLException ex) {
234  LOGGER.log(Level.SEVERE, "Error adding default organization", ex);
235  return false;
236  } finally {
237  CentralRepoDbUtil.closePreparedStatement(preparedStatement);
238  }
239 
240  return true;
241  }
242 
251  public static boolean allowUseOfCentralRepository() {
252  //In almost all situations EamDb.isEnabled() should be used instead of this method
253  //as EamDb.isEnabled() will call this method as well as checking that the selected type of central repository is not DISABLED
254  return Boolean.parseBoolean(ModuleSettings.getConfigSetting(CENTRAL_REPO_NAME, CENTRAL_REPO_USE_KEY));
255  }
256 
264  public static void setUseCentralRepo(boolean centralRepoCheckBoxIsSelected) {
266  ModuleSettings.setConfigSetting(CENTRAL_REPO_NAME, CENTRAL_REPO_USE_KEY, Boolean.toString(centralRepoCheckBoxIsSelected));
267  }
268 
272  private static void closePersonasTopComponent() {
273  SwingUtilities.invokeLater(() -> {
274  TopComponent personasWindow = WindowManager.getDefault().findTopComponent("PersonasTopComponent");
275  if (personasWindow != null && personasWindow.isOpened()) {
276  personasWindow.close();
277  }
278  });
279  }
280 
287  public static boolean executeValidationQuery(Connection conn, String validationQuery) {
288  if (null == conn) {
289  return false;
290  }
291 
292  ResultSet resultSet = null;
293  try {
294  Statement tester = conn.createStatement();
295  resultSet = tester.executeQuery(validationQuery);
296  if (resultSet.next()) {
297  return true;
298  }
299  } catch (SQLException ex) {
300  return false;
301  } finally {
303  }
304 
305  return false;
306  }
307 
316  return type.getDbTableName() + "_instances";
317  }
318 
327  return "reference_" + type.getDbTableName();
328  }
329 
339  @Deprecated
340  public static void closePreparedStatement(PreparedStatement preparedStatement) {
341  closeStatement(preparedStatement);
342  }
343 
351  static boolean correlationAttribHasAnAccount(CorrelationAttributeInstance.Type type) {
354  || type.getId() == CorrelationAttributeInstance.EMAIL_TYPE_ID;
355  }
356 
372  public static boolean commentExistsOnAttributes(List<CorrelationAttributeInstance> attributes) throws CentralRepoException {
373  boolean commentExists = false;
374  if (CentralRepository.isEnabled() && !attributes.isEmpty()) {
376  //Query to check for the presence of a comment on any matching value in the specified table.
377  String sqlSelect = "SELECT EXISTS "
378  + "(SELECT 1 "
379  + "FROM ";
380  String sqlWhere = " WHERE value=? "
381  + "AND comment<>''"
382  + "LIMIT 1)";
383  List<Object> params;
384  CommentExistsCallback commentCallback = new CommentExistsCallback();
385  for (CorrelationAttributeInstance instance : attributes) {
386  params = new ArrayList<>();
387  params.add(instance.getCorrelationValue());
388  String sql = sqlSelect + CentralRepoDbUtil.correlationTypeToInstanceTableName(instance.getCorrelationType()) + sqlWhere;
389  crInstance.executeQuery(sql, params, commentCallback);
390  if (commentCallback.doesCommentExist()) {
391  //we are checking a binary condition so as soon as any query returns true we can stop
392  commentExists = true;
393  break;
394  }
395  }
396  }
397  return commentExists;
398  }
399 
404  private static class CommentExistsCallback implements CentralRepositoryDbQueryCallback {
405 
406  private boolean commentExists = false;
407 
408  @Override
409  public void process(ResultSet rs) throws CentralRepoException, SQLException {
410  //there should only be 1 result here with 1 column
411  if (rs.next()) {
412  commentExists = rs.getBoolean(1);
413  }
414  }
415 
421  boolean doesCommentExist() {
422  return commentExists;
423  }
424 
425  }
426 
427 }
static synchronized String getConfigSetting(String moduleName, String settingName)
void executeQuery(String sql, List< Object > params, CentralRepositoryDbQueryCallback queryCallback)
static void insertCorrelationType(Connection conn, CorrelationAttributeInstance.Type correlationType)
static String correlationTypeToReferenceTableName(CorrelationAttributeInstance.Type type)
static void setUseCentralRepo(boolean centralRepoCheckBoxIsSelected)
static String correlationTypeToInstanceTableName(CorrelationAttributeInstance.Type type)
static boolean commentExistsOnAttributes(List< CorrelationAttributeInstance > attributes)
static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal)
static void closePreparedStatement(PreparedStatement preparedStatement)
static boolean executeValidationQuery(Connection conn, String validationQuery)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2021 Basis Technology. Generated on: Tue Feb 22 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.