Autopsy  4.17.0
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.List;
27 import java.util.logging.Level;
28 import javax.swing.SwingUtilities;
29 import org.openide.windows.TopComponent;
30 import org.openide.windows.WindowManager;
33 import static org.sleuthkit.autopsy.centralrepository.datamodel.RdbmsCentralRepo.SOFTWARE_CR_DB_SCHEMA_VERSION;
34 
38 public class CentralRepoDbUtil {
39 
40  private final static Logger LOGGER = Logger.getLogger(CentralRepoDbUtil.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 (CentralRepoException | SQLException ex) {
121  LOGGER.log(Level.SEVERE, "Error inserting default correlation types.", ex); // NON-NLS
122  return false;
123  } finally {
124  CentralRepoDbUtil.closePreparedStatement(preparedStatement);
125  }
126  return true;
127  }
128 
136  public static void insertCorrelationType(Connection conn, CorrelationAttributeInstance.Type correlationType) throws SQLException {
137 
138  String sql = "INSERT INTO correlation_types(id, display_name, db_table_name, supported, enabled) VALUES (?, ?, ?, ?, ?)";
139  try (PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
140 
141  preparedStatement.setInt(1, correlationType.getId());
142  preparedStatement.setString(2, correlationType.getDisplayName());
143  preparedStatement.setString(3, correlationType.getDbTableName());
144  preparedStatement.setInt(4, correlationType.isSupported() ? 1 : 0);
145  preparedStatement.setInt(5, correlationType.isEnabled() ? 1 : 0);
146 
147  preparedStatement.execute();
148  }
149  }
150 
158  static void updateSchemaVersion(Connection conn) throws SQLException {
159  try (Statement statement = conn.createStatement()) {
160  statement.execute("UPDATE db_info SET value = '" + SOFTWARE_CR_DB_SCHEMA_VERSION.getMajor() + "' WHERE name = '" + RdbmsCentralRepo.SCHEMA_MAJOR_VERSION_KEY + "'");
161  statement.execute("UPDATE db_info SET value = '" + SOFTWARE_CR_DB_SCHEMA_VERSION.getMinor() + "' WHERE name = '" + RdbmsCentralRepo.SCHEMA_MINOR_VERSION_KEY + "'");
162  }
163  }
164 
170  public static boolean schemaVersionIsSet(Connection conn) {
171  if (null == conn) {
172  return false;
173  }
174 
175  ResultSet resultSet = null;
176  try {
177  Statement tester = conn.createStatement();
178  String sql = "SELECT value FROM db_info WHERE name='SCHEMA_VERSION'";
179  resultSet = tester.executeQuery(sql);
180  if (resultSet.next()) {
181  String value = resultSet.getString("value");
182  }
183  } catch (SQLException ex) {
184  return false;
185  } finally {
187  }
188  return true;
189  }
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 
357 }
static synchronized String getConfigSetting(String moduleName, String settingName)
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 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 Jan 19 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.