Autopsy  4.19.3
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;
36 
40 public class CentralRepoDbUtil {
41 
42  private final static Logger LOGGER = Logger.getLogger(CentralRepoDbUtil.class.getName());
44  private static final String CENTRAL_REPO_USE_KEY = "db.useCentralRepo";
45  private static final String DEFAULT_ORG_NAME = "Not Specified";
46 
54  public static void closeStatement(Statement statement) {
55  if (null != statement) {
56  try {
57  statement.close();
58  } catch (SQLException ex) {
59  LOGGER.log(Level.SEVERE, "Error closing Statement.", ex);
60  }
61  }
62  }
63 
71  public static void closeResultSet(ResultSet resultSet) {
72  if (null != resultSet) {
73  try {
74  resultSet.close();
75  } catch (SQLException ex) {
76  LOGGER.log(Level.SEVERE, "Error closing ResultSet.", ex);
77  }
78  }
79  }
80 
88  public static void closeConnection(Connection conn) {
89  if (null != conn) {
90  try {
91  conn.close();
92  } catch (SQLException ex) {
93  LOGGER.log(Level.SEVERE, "Error closing Connection.", ex);
94  }
95  }
96  }
97 
105  public static boolean insertDefaultCorrelationTypes(Connection conn) {
106  PreparedStatement preparedStatement = null;
107  String sql = "INSERT INTO correlation_types(id, display_name, db_table_name, supported, enabled) VALUES (?, ?, ?, ?, ?)";
108 
109  try {
111  preparedStatement = conn.prepareStatement(sql);
112  for (CorrelationAttributeInstance.Type newType : DEFAULT_CORRELATION_TYPES) {
113  preparedStatement.setInt(1, newType.getId());
114  preparedStatement.setString(2, newType.getDisplayName());
115  preparedStatement.setString(3, newType.getDbTableName());
116  preparedStatement.setInt(4, newType.isSupported() ? 1 : 0);
117  preparedStatement.setInt(5, newType.isEnabled() ? 1 : 0);
118 
119  preparedStatement.addBatch();
120  }
121  preparedStatement.executeBatch();
122  } catch (CentralRepoException | SQLException ex) {
123  LOGGER.log(Level.SEVERE, "Error inserting default correlation types.", ex); // NON-NLS
124  return false;
125  } finally {
126  CentralRepoDbUtil.closePreparedStatement(preparedStatement);
127  }
128  return true;
129  }
130 
138  public static void insertCorrelationType(Connection conn, CorrelationAttributeInstance.Type correlationType) throws SQLException {
139 
140  String sql = "INSERT INTO correlation_types(id, display_name, db_table_name, supported, enabled) VALUES (?, ?, ?, ?, ?)";
141  try (PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
142 
143  preparedStatement.setInt(1, correlationType.getId());
144  preparedStatement.setString(2, correlationType.getDisplayName());
145  preparedStatement.setString(3, correlationType.getDbTableName());
146  preparedStatement.setInt(4, correlationType.isSupported() ? 1 : 0);
147  preparedStatement.setInt(5, correlationType.isEnabled() ? 1 : 0);
148 
149  preparedStatement.execute();
150  }
151  }
152 
160  static void updateSchemaVersion(Connection conn) throws SQLException {
161  try (Statement statement = conn.createStatement()) {
162  statement.execute("UPDATE db_info SET value = '" + SOFTWARE_CR_DB_SCHEMA_VERSION.getMajor() + "' WHERE name = '" + RdbmsCentralRepo.SCHEMA_MAJOR_VERSION_KEY + "'");
163  statement.execute("UPDATE db_info SET value = '" + SOFTWARE_CR_DB_SCHEMA_VERSION.getMinor() + "' WHERE name = '" + RdbmsCentralRepo.SCHEMA_MINOR_VERSION_KEY + "'");
164  }
165  }
166 
172  public static boolean schemaVersionIsSet(Connection conn) {
173  if (null == conn) {
174  return false;
175  }
176 
177  ResultSet resultSet = null;
178  try {
179  Statement tester = conn.createStatement();
180  String sql = "SELECT value FROM db_info WHERE name='SCHEMA_VERSION'";
181  resultSet = tester.executeQuery(sql);
182  if (resultSet.next()) {
183  String value = resultSet.getString("value");
184  }
185  } catch (SQLException ex) {
186  return false;
187  } finally {
189  }
190  return true;
191  }
192 
198  public static String getDefaultOrgName() {
199  return DEFAULT_ORG_NAME;
200  }
201 
209  public static boolean isDefaultOrg(CentralRepoOrganization org) {
210  return DEFAULT_ORG_NAME.equals(org.getName());
211  }
212 
220  static boolean insertDefaultOrganization(Connection conn) {
221  if (null == conn) {
222  return false;
223  }
224 
225  PreparedStatement preparedStatement = null;
226  String sql = "INSERT INTO organizations(org_name, poc_name, poc_email, poc_phone) VALUES (?, ?, ?, ?)";
227  try {
228  preparedStatement = conn.prepareStatement(sql);
229  preparedStatement.setString(1, DEFAULT_ORG_NAME);
230  preparedStatement.setString(2, "");
231  preparedStatement.setString(3, "");
232  preparedStatement.setString(4, "");
233  preparedStatement.executeUpdate();
234  } catch (SQLException ex) {
235  LOGGER.log(Level.SEVERE, "Error adding default organization", ex);
236  return false;
237  } finally {
238  CentralRepoDbUtil.closePreparedStatement(preparedStatement);
239  }
240 
241  return true;
242  }
243 
252  public static boolean allowUseOfCentralRepository() {
253  //In almost all situations EamDb.isEnabled() should be used instead of this method
254  //as EamDb.isEnabled() will call this method as well as checking that the selected type of central repository is not DISABLED
255  return Boolean.parseBoolean(ModuleSettings.getConfigSetting(CENTRAL_REPO_NAME, CENTRAL_REPO_USE_KEY));
256  }
257 
265  public static void setUseCentralRepo(boolean centralRepoCheckBoxIsSelected) {
267  ModuleSettings.setConfigSetting(CENTRAL_REPO_NAME, CENTRAL_REPO_USE_KEY, Boolean.toString(centralRepoCheckBoxIsSelected));
268  }
269 
273  private static void closePersonasTopComponent() {
274  SwingUtilities.invokeLater(() -> {
275  TopComponent personasWindow = WindowManager.getDefault().findTopComponent("PersonasTopComponent");
276  if (personasWindow != null && personasWindow.isOpened()) {
277  personasWindow.close();
278  }
279  });
280  }
281 
288  public static boolean executeValidationQuery(Connection conn, String validationQuery) {
289  if (null == conn) {
290  return false;
291  }
292 
293  ResultSet resultSet = null;
294  try {
295  Statement tester = conn.createStatement();
296  resultSet = tester.executeQuery(validationQuery);
297  if (resultSet.next()) {
298  return true;
299  }
300  } catch (SQLException ex) {
301  return false;
302  } finally {
304  }
305 
306  return false;
307  }
308 
317  return type.getDbTableName() + "_instances";
318  }
319 
328  return "reference_" + type.getDbTableName();
329  }
330 
340  @Deprecated
341  public static void closePreparedStatement(PreparedStatement preparedStatement) {
342  closeStatement(preparedStatement);
343  }
344 
352  static boolean correlationAttribHasAnAccount(CorrelationAttributeInstance.Type type) {
355  || type.getId() == CorrelationAttributeInstance.EMAIL_TYPE_ID;
356  }
357 
373  public static boolean commentExistsOnAttributes(List<CorrelationAttributeInstance> attributes) throws CentralRepoException {
374  boolean commentExists = false;
375  if (CentralRepository.isEnabled() && !attributes.isEmpty()) {
377  //Query to check for the presence of a comment on any matching value in the specified table.
378  String sqlSelect = "SELECT EXISTS "
379  + "(SELECT 1 "
380  + "FROM ";
381  String sqlWhere = " WHERE value=? "
382  + "AND comment<>''"
383  + "LIMIT 1)";
384  List<Object> params;
385  CommentExistsCallback commentCallback = new CommentExistsCallback();
386  for (CorrelationAttributeInstance instance : attributes) {
387  params = new ArrayList<>();
388  params.add(instance.getCorrelationValue());
389  String sql = sqlSelect + CentralRepoDbUtil.correlationTypeToInstanceTableName(instance.getCorrelationType()) + sqlWhere;
390  crInstance.executeQuery(sql, params, commentCallback);
391  if (commentCallback.doesCommentExist()) {
392  //we are checking a binary condition so as soon as any query returns true we can stop
393  commentExists = true;
394  break;
395  }
396  }
397  }
398  return commentExists;
399  }
400 
405  private static class CommentExistsCallback implements CentralRepositoryDbQueryCallback {
406 
407  private boolean commentExists = false;
408 
409  @Override
410  public void process(ResultSet rs) throws CentralRepoException, SQLException {
411  //there should only be 1 result here with 1 column
412  if (rs.next()) {
413  commentExists = rs.getBoolean(1);
414  }
415  }
416 
422  boolean doesCommentExist() {
423  return commentExists;
424  }
425 
426  }
427 
428 }
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-2022 Basis Technology. Generated on: Thu Dec 8 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.