Skip to content
Snippets Groups Projects
Commit 6664eba0 authored by Greg DiCristofaro's avatar Greg DiCristofaro
Browse files

Merge branch '8440-develop' into 8440-new_table_load

parents 7d327ab0 3c79c151
No related branches found
No related tags found
No related merge requests found
...@@ -196,15 +196,17 @@ public org.sleuthkit.datamodel.Account.Type addAccountType(String accountTypeNam ...@@ -196,15 +196,17 @@ public org.sleuthkit.datamodel.Account.Type addAccountType(String accountTypeNam
if (this.accountTypeToTypeIdMap.containsKey(accountType)) { if (this.accountTypeToTypeIdMap.containsKey(accountType)) {
return accountType; return accountType;
} }
CaseDbTransaction transaction = this.db.beginTransaction(); CaseDbTransaction transaction = this.db.beginTransaction();
try { try {
org.sleuthkit.datamodel.Account.Type retAccountType = addAccountType(accountTypeName, displayName, transaction); org.sleuthkit.datamodel.Account.Type retAccountType = addAccountType(accountTypeName, displayName, transaction);
transaction.commit(); transaction.commit();
transaction = null;
return retAccountType; return retAccountType;
} catch (TskCoreException ex) { } finally {
transaction.rollback(); if (transaction != null) {
throw ex; transaction.rollback();
}
} }
} }
...@@ -232,12 +234,12 @@ public org.sleuthkit.datamodel.Account.Type addAccountType(String accountTypeNam ...@@ -232,12 +234,12 @@ public org.sleuthkit.datamodel.Account.Type addAccountType(String accountTypeNam
try { try {
PreparedStatement initialSelectStmt = trans.getConnection().getPreparedStatement( PreparedStatement initialSelectStmt = trans.getConnection().getPreparedStatement(
"SELECT * FROM account_types WHERE type_name = ?", "SELECT * FROM account_types WHERE type_name = ?",
Statement.RETURN_GENERATED_KEYS Statement.RETURN_GENERATED_KEYS
); );
initialSelectStmt.clearParameters(); initialSelectStmt.clearParameters();
initialSelectStmt.setString(1, accountTypeName); initialSelectStmt.setString(1, accountTypeName);
// try to get cached account type // try to get cached account type
try (ResultSet initialSelectRs = initialSelectStmt.executeQuery()) { try (ResultSet initialSelectRs = initialSelectStmt.executeQuery()) {
if (initialSelectRs.next()) { if (initialSelectRs.next()) {
...@@ -250,14 +252,14 @@ public org.sleuthkit.datamodel.Account.Type addAccountType(String accountTypeNam ...@@ -250,14 +252,14 @@ public org.sleuthkit.datamodel.Account.Type addAccountType(String accountTypeNam
// if not in database, insert // if not in database, insert
PreparedStatement insertStmt = trans.getConnection().getPreparedStatement( PreparedStatement insertStmt = trans.getConnection().getPreparedStatement(
"INSERT INTO account_types (type_name, display_name) VALUES (?, ?)", "INSERT INTO account_types (type_name, display_name) VALUES (?, ?)",
Statement.RETURN_GENERATED_KEYS Statement.RETURN_GENERATED_KEYS
); );
insertStmt.clearParameters(); insertStmt.clearParameters();
insertStmt.setString(1, accountTypeName); insertStmt.setString(1, accountTypeName);
insertStmt.setString(2, displayName); insertStmt.setString(2, displayName);
int affectedRows = insertStmt.executeUpdate(); int affectedRows = insertStmt.executeUpdate();
if (affectedRows > 0) { if (affectedRows > 0) {
try (ResultSet insertRsKeys = insertStmt.getGeneratedKeys()) { try (ResultSet insertRsKeys = insertStmt.getGeneratedKeys()) {
...@@ -269,7 +271,7 @@ public org.sleuthkit.datamodel.Account.Type addAccountType(String accountTypeNam ...@@ -269,7 +271,7 @@ public org.sleuthkit.datamodel.Account.Type addAccountType(String accountTypeNam
} }
} }
} }
throw new SQLException(MessageFormat.format("Account with type name: {0} and display name: {1} could not be created", accountTypeName, displayName)); throw new SQLException(MessageFormat.format("Account with type name: {0} and display name: {1} could not be created", accountTypeName, displayName));
} catch (SQLException ex) { } catch (SQLException ex) {
throw new TskCoreException("Error adding account type", ex); throw new TskCoreException("Error adding account type", ex);
...@@ -309,9 +311,11 @@ public AccountFileInstance createAccountFileInstance(org.sleuthkit.datamodel.Acc ...@@ -309,9 +311,11 @@ public AccountFileInstance createAccountFileInstance(org.sleuthkit.datamodel.Acc
sourceFile, attributes, ingestJobId, transaction); sourceFile, attributes, ingestJobId, transaction);
transaction.commit(); transaction.commit();
} catch (TskCoreException ex) { transaction = null;
transaction.rollback(); } finally {
throw ex; if (transaction != null) {
transaction.rollback();
}
} }
if (accountFileInstance != null) { if (accountFileInstance != null) {
...@@ -448,21 +452,21 @@ public Account getAccount(org.sleuthkit.datamodel.Account.Type accountType, Stri ...@@ -448,21 +452,21 @@ public Account getAccount(org.sleuthkit.datamodel.Account.Type accountType, Stri
public Account getAccount(org.sleuthkit.datamodel.Account.Type accountType, String accountUniqueID, CaseDbConnection caseDbConnection) throws TskCoreException, InvalidAccountIDException { public Account getAccount(org.sleuthkit.datamodel.Account.Type accountType, String accountUniqueID, CaseDbConnection caseDbConnection) throws TskCoreException, InvalidAccountIDException {
try { try {
PreparedStatement stmt = caseDbConnection.getPreparedStatement( PreparedStatement stmt = caseDbConnection.getPreparedStatement(
"SELECT * FROM accounts WHERE account_type_id = ? AND account_unique_identifier = ?", "SELECT * FROM accounts WHERE account_type_id = ? AND account_unique_identifier = ?",
Statement.NO_GENERATED_KEYS Statement.NO_GENERATED_KEYS
); );
stmt.clearParameters(); stmt.clearParameters();
stmt.setInt(1, getAccountTypeId(accountType)); stmt.setInt(1, getAccountTypeId(accountType));
stmt.setString(2, normalizeAccountID(accountType, accountUniqueID)); stmt.setString(2, normalizeAccountID(accountType, accountUniqueID));
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) { if (rs.next()) {
return new Account(rs.getInt("account_id"), accountType, return new Account(rs.getInt("account_id"), accountType,
rs.getString("account_unique_identifier")); rs.getString("account_unique_identifier"));
} else { } else {
return null; return null;
} }
} }
} catch (SQLException ex) { } catch (SQLException ex) {
throw new TskCoreException("Error adding an account", ex); throw new TskCoreException("Error adding an account", ex);
...@@ -480,17 +484,18 @@ public Account getAccount(org.sleuthkit.datamodel.Account.Type accountType, Stri ...@@ -480,17 +484,18 @@ public Account getAccount(org.sleuthkit.datamodel.Account.Type accountType, Stri
* *
* @throws TskCoreException * @throws TskCoreException
*/ */
private DataArtifact newDataArtifact(Content content, BlackboardArtifact.Type artType, private DataArtifact newDataArtifact(Content content, BlackboardArtifact.Type artType,
Collection<BlackboardAttribute> attributes, CaseDbTransaction trans) throws TskCoreException { Collection<BlackboardAttribute> attributes, CaseDbTransaction trans) throws TskCoreException {
Long dataSourceObjId; long dataSourceObjId;
if (content instanceof AbstractFile) { if (content instanceof AbstractFile) {
dataSourceObjId = ((AbstractFile) content).getDataSourceObjectId(); dataSourceObjId = ((AbstractFile) content).getDataSourceObjectId();
} else if (content instanceof BlackboardArtifact) {
dataSourceObjId = ((BlackboardArtifact) content).getDataSourceObjectID();
} else if (content instanceof DataSource) { } else if (content instanceof DataSource) {
dataSourceObjId = content.getId(); dataSourceObjId = content.getId();
} else { } else {
// can be null for anything else dataSourceObjId = getDataSourceObjId(trans.getConnection(), content.getId());
dataSourceObjId = null;
} }
return getSleuthkitCase().getBlackboard().newDataArtifact( return getSleuthkitCase().getBlackboard().newDataArtifact(
...@@ -502,7 +507,63 @@ private DataArtifact newDataArtifact(Content content, BlackboardArtifact.Type ar ...@@ -502,7 +507,63 @@ private DataArtifact newDataArtifact(Content content, BlackboardArtifact.Type ar
trans); trans);
} }
/**
* Iteratively checks tsk_objects table for parents until arriving at a
* tsk_objects entry that is of type IMG. If no IMG type is reached, returns
* 0.
*
* @param conn The database connection.
* @param objId The starting object id.
*
* @return The object id of the parent data source or 0.
*
* @throws TskCoreException
*/
private long getDataSourceObjId(CaseDbConnection conn, long objId) throws TskCoreException {
this.db.acquireSingleUserCaseReadLock();
try {
while (objId > 0) {
try (PreparedStatement pStmt = conn.getPreparedStatement(
"SELECT par_obj_id, type FROM tsk_objects WHERE obj_id = ?",
Statement.NO_GENERATED_KEYS)) {
pStmt.clearParameters();
pStmt.setLong(1, objId);
try (ResultSet rs = pStmt.executeQuery()) {
if (rs.next()) {
TskData.ObjectType type = TskData.ObjectType.valueOf(rs.getShort("type")); //NON-NLS
// if the current id is an image, we have found the data source id.
if (type == TskData.ObjectType.IMG) {
return objId;
}
Long parId = rs.getLong("par_obj_id");
// if no parent id, there is no data source associated with this content.
if (rs.wasNull()) {
return 0;
}
// otherwise, keep iterating
objId = parId;
} else {
// if no record matching the object id, set the data source object id to 0.
return 0;
}
}
}
}
} catch (SQLException ex) {
throw new TskCoreException("Error getting Content by ID.", ex);
} finally {
this.db.releaseSingleUserCaseReadLock();
}
// in this event, simply return 0.
return 0;
}
/** /**
* Adds relationships between the sender and each of the recipient account * Adds relationships between the sender and each of the recipient account
* instances and between all recipient account instances. All account * instances and between all recipient account instances. All account
...@@ -534,9 +595,11 @@ public void addRelationships(AccountFileInstance sender, List<AccountFileInstanc ...@@ -534,9 +595,11 @@ public void addRelationships(AccountFileInstance sender, List<AccountFileInstanc
try { try {
addRelationships(sender, recipients, sourceArtifact, relationshipType, dateTime, transaction); addRelationships(sender, recipients, sourceArtifact, relationshipType, dateTime, transaction);
transaction.commit(); transaction.commit();
} catch (TskCoreException ex) { transaction = null;
transaction.rollback(); } finally {
throw ex; if (transaction != null) {
transaction.rollback();
}
} }
} }
...@@ -684,14 +747,14 @@ private Account getOrCreateAccount(Account.Type accountType, String accountUniqu ...@@ -684,14 +747,14 @@ private Account getOrCreateAccount(Account.Type accountType, String accountUniqu
try (ResultSet rs = insertStmt.getGeneratedKeys()) { try (ResultSet rs = insertStmt.getGeneratedKeys()) {
if (rs.next()) { if (rs.next()) {
return new Account( return new Account(
rs.getInt(1), rs.getInt(1),
accountType, accountType,
accountUniqueIdentifier accountUniqueIdentifier
); );
} }
} }
} }
return null; return null;
} catch (SQLException ex) { } catch (SQLException ex) {
throw new TskCoreException("Error adding an account", ex); throw new TskCoreException("Error adding an account", ex);
...@@ -787,7 +850,7 @@ private BlackboardArtifact getAccountFileInstanceArtifact(Account.Type accountTy ...@@ -787,7 +850,7 @@ private BlackboardArtifact getAccountFileInstanceArtifact(Account.Type accountTy
try { try {
PreparedStatement pStatement = transaction.getConnection().getPreparedStatement(queryStr, Statement.NO_GENERATED_KEYS); PreparedStatement pStatement = transaction.getConnection().getPreparedStatement(queryStr, Statement.NO_GENERATED_KEYS);
pStatement.clearParameters(); pStatement.clearParameters();
int paramIdx = 0; int paramIdx = 0;
pStatement.setInt(++paramIdx, BlackboardAttribute.ATTRIBUTE_TYPE.TSK_ID.getTypeID()); pStatement.setInt(++paramIdx, BlackboardAttribute.ATTRIBUTE_TYPE.TSK_ID.getTypeID());
pStatement.setString(++paramIdx, accountUniqueID); pStatement.setString(++paramIdx, accountUniqueID);
...@@ -1167,7 +1230,7 @@ public Set<Content> getRelationshipSources(Set<AccountDeviceInstance> accountDev ...@@ -1167,7 +1230,7 @@ public Set<Content> getRelationshipSources(Set<AccountDeviceInstance> accountDev
for (Map.Entry<Long, Set<Long>> entry : accountIdToDatasourceObjIdMap.entrySet()) { for (Map.Entry<Long, Set<Long>> entry : accountIdToDatasourceObjIdMap.entrySet()) {
final Long accountID = entry.getKey(); final Long accountID = entry.getKey();
String datasourceObjIdsCSV = CommManagerSqlStringUtils.buildCSVString(entry.getValue()); String datasourceObjIdsCSV = CommManagerSqlStringUtils.buildCSVString(entry.getValue());
adiSQLClauses.add( adiSQLClauses.add(
"( " "( "
+ (!datasourceObjIdsCSV.isEmpty() ? "( relationships.data_source_obj_id IN ( " + datasourceObjIdsCSV + " ) ) AND" : "") + (!datasourceObjIdsCSV.isEmpty() ? "( relationships.data_source_obj_id IN ( " + datasourceObjIdsCSV + " ) ) AND" : "")
...@@ -1176,8 +1239,8 @@ public Set<Content> getRelationshipSources(Set<AccountDeviceInstance> accountDev ...@@ -1176,8 +1239,8 @@ public Set<Content> getRelationshipSources(Set<AccountDeviceInstance> accountDev
); );
} }
String adiSQLClause = CommManagerSqlStringUtils.joinAsStrings(adiSQLClauses, " OR "); String adiSQLClause = CommManagerSqlStringUtils.joinAsStrings(adiSQLClauses, " OR ");
if(adiSQLClause.isEmpty()) { if (adiSQLClause.isEmpty()) {
LOGGER.log(Level.SEVERE, "There set of AccountDeviceInstances had no valid data source ids."); LOGGER.log(Level.SEVERE, "There set of AccountDeviceInstances had no valid data source ids.");
return Collections.emptySet(); return Collections.emptySet();
} }
...@@ -1193,7 +1256,7 @@ public Set<Content> getRelationshipSources(Set<AccountDeviceInstance> accountDev ...@@ -1193,7 +1256,7 @@ public Set<Content> getRelationshipSources(Set<AccountDeviceInstance> accountDev
// Basic join. // Basic join.
String limitQuery = " account_relationships AS relationships"; String limitQuery = " account_relationships AS relationships";
// If the user set filters expand this to be a subquery that selects // If the user set filters expand this to be a subquery that selects
// accounts based on the filter. // accounts based on the filter.
String limitStr = getMostRecentFilterLimitSQL(filter); String limitStr = getMostRecentFilterLimitSQL(filter);
......
...@@ -8480,10 +8480,12 @@ public LayoutFile addLayoutFile(String fileName, ...@@ -8480,10 +8480,12 @@ public LayoutFile addLayoutFile(String fileName,
LayoutFile lf = addLayoutFile(fileName, size, dirFlag, metaFlag, LayoutFile lf = addLayoutFile(fileName, size, dirFlag, metaFlag,
ctime, crtime, atime, mtime, fileRanges, parent, dataSourceId, transaction); ctime, crtime, atime, mtime, fileRanges, parent, dataSourceId, transaction);
transaction.commit(); transaction.commit();
transaction = null;
return lf; return lf;
} catch (TskCoreException ex) { } finally {
transaction.rollback(); if (transaction != null) {
throw ex; transaction.rollback();
}
} }
} }
......
...@@ -315,9 +315,11 @@ public BlackboardArtifact addContact(String contactName, ...@@ -315,9 +315,11 @@ public BlackboardArtifact addContact(String contactName,
try { try {
contactArt = addContact(contactName, phoneNumber, homePhoneNumber, mobilePhoneNumber, emailAddr, transaction, additionalAttributes); contactArt = addContact(contactName, phoneNumber, homePhoneNumber, mobilePhoneNumber, emailAddr, transaction, additionalAttributes);
transaction.commit(); transaction.commit();
} catch (TskCoreException | BlackboardException ex) { transaction = null;
transaction.rollback(); } finally {
throw ex; if (transaction != null) {
transaction.rollback();
}
} }
if (contactArt != null) { if (contactArt != null) {
...@@ -615,9 +617,11 @@ public BlackboardArtifact addMessage(String messageType, ...@@ -615,9 +617,11 @@ public BlackboardArtifact addMessage(String messageType,
try { try {
msgArt = addMessage(messageType, direction, senderId, recipientIdsList, dateTime, readStatus, subject, messageText, threadId, transaction, otherAttributesList); msgArt = addMessage(messageType, direction, senderId, recipientIdsList, dateTime, readStatus, subject, messageText, threadId, transaction, otherAttributesList);
transaction.commit(); transaction.commit();
} catch (TskCoreException | BlackboardException ex) { transaction = null;
transaction.rollback(); } finally {
throw ex; if (transaction != null) {
transaction.rollback();
}
} }
if (msgArt != null) { if (msgArt != null) {
...@@ -943,9 +947,11 @@ public BlackboardArtifact addCalllog(CommunicationDirection direction, ...@@ -943,9 +947,11 @@ public BlackboardArtifact addCalllog(CommunicationDirection direction,
try { try {
callLogArt = addCalllog(direction, callerId, calleeIdsList, startDateTime, endDateTime, mediaType, transaction, otherAttributesList); callLogArt = addCalllog(direction, callerId, calleeIdsList, startDateTime, endDateTime, mediaType, transaction, otherAttributesList);
transaction.commit(); transaction.commit();
} catch (TskCoreException | BlackboardException ex) { transaction = null;
transaction.rollback(); } finally {
throw ex; if (transaction != null) {
transaction.rollback();
}
} }
if (callLogArt != null) { if (callLogArt != null) {
...@@ -1084,8 +1090,6 @@ public BlackboardArtifact addCalllog(CommunicationDirection direction, ...@@ -1084,8 +1090,6 @@ public BlackboardArtifact addCalllog(CommunicationDirection direction,
Content content = getContent(); Content content = getContent();
BlackboardArtifact callLogArtifact = newDataArtifact(content, CALLOG_TYPE, attributes, trans); BlackboardArtifact callLogArtifact = newDataArtifact(content, CALLOG_TYPE, attributes, trans);
callLogArtifact.addAttributes(attributes);
// create relationships between caller/callees // create relationships between caller/callees
try { try {
getSleuthkitCase().getCommunicationsManager().addRelationships(callerAccountInstance, getSleuthkitCase().getCommunicationsManager().addRelationships(callerAccountInstance,
...@@ -1130,9 +1134,11 @@ public void addAttachments(BlackboardArtifact message, MessageAttachments attach ...@@ -1130,9 +1134,11 @@ public void addAttachments(BlackboardArtifact message, MessageAttachments attach
dataSourceObjId = dataSourceObjId == null ? 0L : dataSourceObjId; dataSourceObjId = dataSourceObjId == null ? 0L : dataSourceObjId;
attachmentArts = addAttachments(message, dataSourceObjId, attachments, getModuleName(), transaction); attachmentArts = addAttachments(message, dataSourceObjId, attachments, getModuleName(), transaction);
transaction.commit(); transaction.commit();
} catch (TskCoreException ex) { transaction = null;
transaction.rollback(); } finally {
throw ex; if (transaction != null) {
transaction.rollback();
}
} }
attachmentArts = (attachmentArts == null) ? Collections.emptyList() : attachmentArts; attachmentArts = (attachmentArts == null) ? Collections.emptyList() : attachmentArts;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment