Autopsy  4.14.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;
30 import static org.sleuthkit.autopsy.centralrepository.datamodel.RdbmsCentralRepo.SOFTWARE_CR_DB_SCHEMA_VERSION;
31 
35 public class CentralRepoDbUtil {
36 
37  private final static Logger LOGGER = Logger.getLogger(CentralRepoDbUtil.class.getName());
38  private static final String CENTRAL_REPO_NAME = "CentralRepository";
39  private static final String CENTRAL_REPO_USE_KEY = "db.useCentralRepo";
40  private static final String DEFAULT_ORG_NAME = "Not Specified";
41 
49  public static void closeStatement(Statement statement) {
50  if (null != statement) {
51  try {
52  statement.close();
53  } catch (SQLException ex) {
54  LOGGER.log(Level.SEVERE, "Error closing Statement.", ex);
55  }
56  }
57  }
58 
66  public static void closeResultSet(ResultSet resultSet) {
67  if (null != resultSet) {
68  try {
69  resultSet.close();
70  } catch (SQLException ex) {
71  LOGGER.log(Level.SEVERE, "Error closing ResultSet.", ex);
72  }
73  }
74  }
75 
83  public static void closeConnection(Connection conn) {
84  if (null != conn) {
85  try {
86  conn.close();
87  } catch (SQLException ex) {
88  LOGGER.log(Level.SEVERE, "Error closing Connection.", ex);
89  }
90  }
91  }
92 
100  public static boolean insertDefaultCorrelationTypes(Connection conn) {
101  PreparedStatement preparedStatement = null;
102  String sql = "INSERT INTO correlation_types(id, display_name, db_table_name, supported, enabled) VALUES (?, ?, ?, ?, ?)";
103 
104  try {
106  preparedStatement = conn.prepareStatement(sql);
107  for (CorrelationAttributeInstance.Type newType : DEFAULT_CORRELATION_TYPES) {
108  preparedStatement.setInt(1, newType.getId());
109  preparedStatement.setString(2, newType.getDisplayName());
110  preparedStatement.setString(3, newType.getDbTableName());
111  preparedStatement.setInt(4, newType.isSupported() ? 1 : 0);
112  preparedStatement.setInt(5, newType.isEnabled() ? 1 : 0);
113 
114  preparedStatement.addBatch();
115  }
116  preparedStatement.executeBatch();
117  } catch (CentralRepoException | SQLException ex) {
118  LOGGER.log(Level.SEVERE, "Error inserting default correlation types.", ex); // NON-NLS
119  return false;
120  } finally {
121  CentralRepoDbUtil.closePreparedStatement(preparedStatement);
122  }
123  return true;
124  }
125 
133  public static void insertCorrelationType(Connection conn, CorrelationAttributeInstance.Type correlationType) throws SQLException {
134 
135  String sql = "INSERT INTO correlation_types(id, display_name, db_table_name, supported, enabled) VALUES (?, ?, ?, ?, ?)";
136  try (PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
137 
138  preparedStatement.setInt(1, correlationType.getId());
139  preparedStatement.setString(2, correlationType.getDisplayName());
140  preparedStatement.setString(3, correlationType.getDbTableName());
141  preparedStatement.setInt(4, correlationType.isSupported() ? 1 : 0);
142  preparedStatement.setInt(5, correlationType.isEnabled() ? 1 : 0);
143 
144  preparedStatement.execute();
145  }
146  }
147 
155  static void updateSchemaVersion(Connection conn) throws SQLException {
156  try (Statement statement = conn.createStatement()) {
157  statement.execute("UPDATE db_info SET value = '" + SOFTWARE_CR_DB_SCHEMA_VERSION.getMajor() + "' WHERE name = '" + RdbmsCentralRepo.SCHEMA_MAJOR_VERSION_KEY + "'");
158  statement.execute("UPDATE db_info SET value = '" + SOFTWARE_CR_DB_SCHEMA_VERSION.getMinor() + "' WHERE name = '" + RdbmsCentralRepo.SCHEMA_MINOR_VERSION_KEY + "'");
159  }
160  }
161 
167  public static boolean schemaVersionIsSet(Connection conn) {
168  if (null == conn) {
169  return false;
170  }
171 
172  ResultSet resultSet = null;
173  try {
174  Statement tester = conn.createStatement();
175  String sql = "SELECT value FROM db_info WHERE name='SCHEMA_VERSION'";
176  resultSet = tester.executeQuery(sql);
177  if (resultSet.next()) {
178  String value = resultSet.getString("value");
179  }
180  } catch (SQLException ex) {
181  return false;
182  } finally {
184  }
185  return true;
186  }
187 
188 
194  public static String getDefaultOrgName() {
195  return DEFAULT_ORG_NAME;
196  }
197 
205  public static boolean isDefaultOrg(CentralRepoOrganization org) {
206  return DEFAULT_ORG_NAME.equals(org.getName());
207  }
208 
216  static boolean insertDefaultOrganization(Connection conn) {
217  if (null == conn) {
218  return false;
219  }
220 
221  PreparedStatement preparedStatement = null;
222  String sql = "INSERT INTO organizations(org_name, poc_name, poc_email, poc_phone) VALUES (?, ?, ?, ?)";
223  try {
224  preparedStatement = conn.prepareStatement(sql);
225  preparedStatement.setString(1, DEFAULT_ORG_NAME);
226  preparedStatement.setString(2, "");
227  preparedStatement.setString(3, "");
228  preparedStatement.setString(4, "");
229  preparedStatement.executeUpdate();
230  } catch (SQLException ex) {
231  LOGGER.log(Level.SEVERE, "Error adding default organization", ex);
232  return false;
233  } finally {
234  CentralRepoDbUtil.closePreparedStatement(preparedStatement);
235  }
236 
237  return true;
238  }
239 
248  public static boolean allowUseOfCentralRepository() {
249  //In almost all situations EamDb.isEnabled() should be used instead of this method
250  //as EamDb.isEnabled() will call this method as well as checking that the selected type of central repository is not DISABLED
251  return Boolean.parseBoolean(ModuleSettings.getConfigSetting(CENTRAL_REPO_NAME, CENTRAL_REPO_USE_KEY));
252  }
253 
261  public static void setUseCentralRepo(boolean centralRepoCheckBoxIsSelected) {
262  ModuleSettings.setConfigSetting(CENTRAL_REPO_NAME, CENTRAL_REPO_USE_KEY, Boolean.toString(centralRepoCheckBoxIsSelected));
263  }
264 
271  public static boolean executeValidationQuery(Connection conn, String validationQuery) {
272  if (null == conn) {
273  return false;
274  }
275 
276  ResultSet resultSet = null;
277  try {
278  Statement tester = conn.createStatement();
279  resultSet = tester.executeQuery(validationQuery);
280  if (resultSet.next()) {
281  return true;
282  }
283  } catch (SQLException ex) {
284  return false;
285  } finally {
287  }
288 
289  return false;
290  }
291 
300  return type.getDbTableName() + "_instances";
301  }
302 
311  return "reference_" + type.getDbTableName();
312  }
313 
323  @Deprecated
324  public static void closePreparedStatement(PreparedStatement preparedStatement) {
325  closeStatement(preparedStatement);
326  }
327 
335  static boolean correlationAttribHasAnAccount(CorrelationAttributeInstance.Type type) {
338  || type.getId() == CorrelationAttributeInstance.EMAIL_TYPE_ID;
339  }
340 
341 }
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-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.