19 package org.sleuthkit.datamodel;
21 import java.sql.PreparedStatement;
22 import java.sql.ResultSet;
23 import java.sql.SQLException;
24 import java.sql.Statement;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.List;
33 import java.util.concurrent.ConcurrentHashMap;
34 import java.util.logging.Level;
35 import java.util.logging.Logger;
53 private final Map<
Account.
Type, Integer> accountTypeToTypeIdMap
54 =
new ConcurrentHashMap<>();
55 private final Map<String,
Account.
Type> typeNameToAccountTypeMap
56 =
new ConcurrentHashMap<>();
59 private static final Set<Integer> RELATIONSHIP_ARTIFACT_TYPE_IDS =
new HashSet<Integer>(Arrays.asList(
65 private static final String RELATIONSHIP_ARTIFACT_TYPE_IDS_CSV_STR = StringUtils.buildCSVString(RELATIONSHIP_ARTIFACT_TYPE_IDS);
88 try (CaseDbConnection connection = db.getConnection();
89 Statement statement = connection.createStatement();) {
91 int count = readAccountTypes();
96 statement.execute(
"INSERT INTO account_types (type_name, display_name) VALUES ( '" + type.getTypeName() +
"', '" + type.getDisplayName() +
"')");
97 }
catch (SQLException ex) {
98 try (ResultSet resultSet = connection.executeQuery(statement,
"SELECT COUNT(*) AS count FROM account_types WHERE type_name = '" + type.getTypeName() +
"'")) {
100 if (resultSet.getLong(
"count") == 0) {
106 try (ResultSet rs2 = connection.executeQuery(statement,
"SELECT account_type_id FROM account_types WHERE type_name = '" + type.getTypeName() +
"'")) {
108 int typeID = rs2.getInt(
"account_type_id");
111 this.accountTypeToTypeIdMap.put(accountType, typeID);
112 this.typeNameToAccountTypeMap.put(type.getTypeName(), accountType);
116 }
catch (SQLException ex) {
117 LOGGER.log(Level.SEVERE,
"Failed to add row to account_types", ex);
132 CaseDbConnection connection = null;
133 Statement statement = null;
134 ResultSet resultSet = null;
139 connection = db.getConnection();
140 statement = connection.createStatement();
143 resultSet = connection.executeQuery(statement,
"SELECT COUNT(*) AS count FROM account_types");
145 if (resultSet.getLong(
"count") > 0) {
148 resultSet = connection.executeQuery(statement,
"SELECT * FROM account_types");
149 while (resultSet.next()) {
150 Account.
Type accountType =
new Account.
Type(resultSet.getString(
"type_name"), resultSet.getString(
"display_name"));
151 this.accountTypeToTypeIdMap.put(accountType, resultSet.getInt(
"account_type_id"));
152 this.typeNameToAccountTypeMap.put(accountType.getTypeName(), accountType);
154 count = this.typeNameToAccountTypeMap.size();
157 }
catch (SQLException ex) {
160 closeResultSet(resultSet);
161 closeStatement(statement);
162 closeConnection(connection);
195 if (this.accountTypeToTypeIdMap.containsKey(accountType)) {
203 s = trans.getConnection().createStatement();
204 rs = trans.getConnection().executeQuery(s,
"SELECT * FROM account_types WHERE type_name = '" + accountTypeName +
"'");
208 s.execute(
"INSERT INTO account_types (type_name, display_name) VALUES ( '" + accountTypeName +
"', '" + displayName +
"')");
211 rs = trans.getConnection().executeQuery(s,
"SELECT * FROM account_types WHERE type_name = '" + accountTypeName +
"'");
214 int typeID = rs.getInt(
"account_type_id");
215 accountType =
new Account.
Type(rs.getString(
"type_name"), rs.getString(
"display_name"));
217 this.accountTypeToTypeIdMap.put(accountType, typeID);
218 this.typeNameToAccountTypeMap.put(accountTypeName, accountType);
224 int typeID = rs.getInt(
"account_type_id");
226 accountType =
new Account.
Type(rs.getString(
"type_name"), rs.getString(
"display_name"));
227 this.accountTypeToTypeIdMap.put(accountType, typeID);
231 }
catch (SQLException ex) {
262 Account account = getOrCreateAccount(accountType, normalizeAccountID(accountType, accountUniqueID));
270 BlackboardArtifact accountArtifact = getOrCreateAccountFileInstanceArtifact(accountType, normalizeAccountID(accountType, accountUniqueID), moduleName, sourceFile);
298 try (CaseDbConnection connection = db.getConnection();
299 Statement s = connection.createStatement();
300 ResultSet rs = connection.executeQuery(s,
"SELECT * FROM accounts WHERE account_type_id = " + getAccountTypeId(accountType)
301 +
" AND account_unique_identifier = '" + normalizeAccountID(accountType, accountUniqueID) +
"'");) {
304 account =
new Account(rs.getInt(
"account_id"), accountType,
305 rs.getString(
"account_unique_identifier"));
307 }
catch (SQLException ex) {
308 throw new TskCoreException(
"Error getting account type id", ex);
342 if (sourceArtifact.getDataSourceObjectID() == null) {
343 throw new TskDataException(
"Source Artifact does not have a valid data source.");
346 if (relationshipType.isCreatableFrom(sourceArtifact) ==
false) {
347 throw new TskDataException(
"Can not make a " + relationshipType.getDisplayName()
348 +
" relationship from a" + sourceArtifact.getDisplayName());
357 List<Long> accountIDs =
new ArrayList<>();
359 if (null != sender) {
360 accountIDs.add(sender.getAccount().getAccountID());
361 if (!sender.getDataSourceObjectID().equals(sourceArtifact.getDataSourceObjectID())) {
362 throw new TskDataException(
"Sender and relationship are from different data sources :"
363 +
"Sender source ID" + sender.getDataSourceObjectID() +
" != relationship source ID" + sourceArtifact.getDataSourceObjectID());
368 accountIDs.add(recipient.getAccount().getAccountID());
369 if (!recipient.getDataSourceObjectID().equals(sourceArtifact.getDataSourceObjectID())) {
370 throw new TskDataException(
"Recipient and relationship are from different data sources :"
371 +
"Recipient source ID" + recipient.getDataSourceObjectID() +
" != relationship source ID" + sourceArtifact.getDataSourceObjectID());
376 String query =
"INTO account_relationships (account1_id, account2_id, relationship_source_obj_id, date_time, relationship_type, data_source_obj_id ) "
377 +
"VALUES (?,?,?,?,?,?)";
380 query =
"INSERT " + query +
" ON CONFLICT DO NOTHING";
383 query =
"INSERT OR IGNORE " + query;
386 throw new TskCoreException(
"Unknown DB Type: " + db.
getDatabaseType().name());
392 PreparedStatement preparedStatement = connection.getPreparedStatement(query, Statement.NO_GENERATED_KEYS);
394 for (
int i = 0; i < accountIDs.size(); i++) {
395 for (
int j = i + 1; j < accountIDs.size(); j++) {
396 long account1_id = accountIDs.get(i);
397 long account2_id = accountIDs.get(j);
399 preparedStatement.clearParameters();
400 preparedStatement.setLong(1, account1_id);
401 preparedStatement.setLong(2, account2_id);
402 preparedStatement.setLong(3, sourceArtifact.getId());
404 preparedStatement.setLong(4, dateTime);
406 preparedStatement.setNull(4, java.sql.Types.BIGINT);
408 preparedStatement.setInt(5, relationshipType.getTypeID());
409 preparedStatement.setLong(6, sourceArtifact.getDataSourceObjectID());
411 connection.executeUpdate(preparedStatement);
415 }
catch (SQLException ex) {
417 throw new TskCoreException(
"Error adding accounts relationship", ex);
437 if (null == account) {
438 String query =
" INTO accounts (account_type_id, account_unique_identifier) "
439 +
"VALUES ( " + getAccountTypeId(accountType) +
", '"
440 + normalizeAccountID(accountType, accountUniqueID) +
"'" +
")";
443 query =
"INSERT " + query +
" ON CONFLICT DO NOTHING";
446 query =
"INSERT OR IGNORE " + query;
449 throw new TskCoreException(
"Unknown DB Type: " + db.
getDatabaseType().name());
456 s = trans.getConnection().createStatement();
461 account =
getAccount(accountType, accountUniqueID);
462 }
catch (SQLException ex) {
464 throw new TskCoreException(
"Error adding an account", ex);
491 private BlackboardArtifact getOrCreateAccountFileInstanceArtifact(Account.Type accountType, String accountUniqueID, String moduleName, Content sourceFile)
throws TskCoreException {
492 if (sourceFile == null) {
493 throw new TskCoreException(
"Source file not provided.");
496 BlackboardArtifact accountArtifact = getAccountFileInstanceArtifact(accountType, accountUniqueID, sourceFile);
497 if (accountArtifact == null) {
498 List<BlackboardAttribute> attributes = Arrays.asList(
499 new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_ACCOUNT_TYPE, moduleName, accountType.getTypeName()),
500 new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_ID, moduleName, accountUniqueID)
503 accountArtifact = sourceFile.newDataArtifact(ACCOUNT_TYPE, attributes);
507 }
catch (BlackboardException ex) {
508 LOGGER.log(Level.SEVERE, String.format(
"Error posting new account artifact to the blackboard (object ID = %d)", accountArtifact.getId()), ex);
511 return accountArtifact;
527 private BlackboardArtifact getAccountFileInstanceArtifact(Account.Type accountType, String accountUniqueID, Content sourceFile)
throws TskCoreException {
528 BlackboardArtifact accountArtifact = null;
530 String queryStr =
"SELECT artifacts.artifact_id AS artifact_id,"
531 +
" artifacts.obj_id AS obj_id,"
532 +
" artifacts.artifact_obj_id AS artifact_obj_id,"
533 +
" artifacts.data_source_obj_id AS data_source_obj_id,"
534 +
" artifacts.artifact_type_id AS artifact_type_id,"
535 +
" artifacts.review_status_id AS review_status_id"
536 +
" FROM blackboard_artifacts AS artifacts"
537 +
" JOIN blackboard_attributes AS attr_account_type"
538 +
" ON artifacts.artifact_id = attr_account_type.artifact_id"
539 +
" JOIN blackboard_attributes AS attr_account_id"
540 +
" ON artifacts.artifact_id = attr_account_id.artifact_id"
541 +
" AND attr_account_id.attribute_type_id = " + BlackboardAttribute.ATTRIBUTE_TYPE.TSK_ID.getTypeID()
542 +
" AND attr_account_id.value_text = '" + accountUniqueID +
"'"
543 +
" WHERE artifacts.artifact_type_id = " + BlackboardArtifact.ARTIFACT_TYPE.TSK_ACCOUNT.getTypeID()
544 +
" AND attr_account_type.attribute_type_id = " + BlackboardAttribute.ATTRIBUTE_TYPE.TSK_ACCOUNT_TYPE.getTypeID()
545 +
" AND attr_account_type.value_text = '" + accountType.getTypeName() +
"'"
546 +
" AND artifacts.obj_id = " + sourceFile.getId();
549 try (CaseDbConnection connection = db.getConnection();
550 Statement s = connection.createStatement();
551 ResultSet rs = connection.executeQuery(s, queryStr);) {
553 BlackboardArtifact.Type bbartType = db.
getArtifactType(rs.getInt(
"artifact_type_id"));
555 accountArtifact =
new BlackboardArtifact(db, rs.getLong(
"artifact_id"), rs.getLong(
"obj_id"), rs.getLong(
"artifact_obj_id"),
556 rs.getObject(
"data_source_obj_id") != null ? rs.getLong(
"data_source_obj_id") : null,
557 bbartType.getTypeID(), bbartType.getTypeName(), bbartType.getDisplayName(),
558 BlackboardArtifact.ReviewStatus.withID(rs.getInt(
"review_status_id")));
560 }
catch (SQLException ex) {
561 throw new TskCoreException(
"Error getting account", ex);
566 return accountArtifact;
580 if (this.typeNameToAccountTypeMap.containsKey(accountTypeName)) {
581 return this.typeNameToAccountTypeMap.get(accountTypeName);
585 try (CaseDbConnection connection = db.getConnection();
586 Statement s = connection.createStatement();
587 ResultSet rs = connection.executeQuery(s,
"SELECT account_type_id, type_name, display_name FROM account_types WHERE type_name = '" + accountTypeName +
"'");) {
590 accountType =
new Account.
Type(accountTypeName, rs.getString(
"display_name"));
591 this.accountTypeToTypeIdMap.put(accountType, rs.getInt(
"account_type_id"));
592 this.typeNameToAccountTypeMap.put(accountTypeName, accountType);
595 }
catch (SQLException ex) {
596 throw new TskCoreException(
"Error getting account type id", ex);
619 Set<String> applicableInnerQueryFilters =
new HashSet<String>(Arrays.asList(
624 String relationshipFilterSQL = getCommunicationsFilterSQL(filter, applicableInnerQueryFilters);
626 String relationshipLimitSQL = getMostRecentFilterLimitSQL(filter);
628 String relTblfilterQuery
630 +
"FROM account_relationships as relationships"
631 + (relationshipFilterSQL.isEmpty() ?
"" :
" WHERE " + relationshipFilterSQL)
632 + (relationshipLimitSQL.isEmpty() ?
"" : relationshipLimitSQL);
634 String uniqueAccountQueryTemplate
635 =
" SELECT %1$1s as account_id,"
636 +
" data_source_obj_id"
637 +
" FROM ( " + relTblfilterQuery +
")AS %2$s";
639 String relationshipTableFilterQuery1 = String.format(uniqueAccountQueryTemplate,
"account1_id",
"union_query_1");
640 String relationshipTableFilterQuery2 = String.format(uniqueAccountQueryTemplate,
"account2_id",
"union_query_2");
643 String uniqueAccountQuery
644 =
"SELECT DISTINCT account_id, data_source_obj_id"
645 +
" FROM ( " + relationshipTableFilterQuery1 +
" UNION " + relationshipTableFilterQuery2 +
" ) AS inner_union"
646 +
" GROUP BY account_id, data_source_obj_id";
649 Set<String> applicableFilters =
new HashSet<String>(Arrays.asList(
653 String accountTypeFilterSQL = getCommunicationsFilterSQL(filter, applicableFilters);
657 " accounts.account_id AS account_id,"
658 +
" accounts.account_unique_identifier AS account_unique_identifier,"
660 +
" account_types.type_name AS type_name,"
662 +
" data_source_info.device_id AS device_id"
663 +
" FROM ( " + uniqueAccountQuery +
" ) AS account_device_instances"
664 +
" JOIN accounts AS accounts"
665 +
" ON accounts.account_id = account_device_instances.account_id"
666 +
" JOIN account_types AS account_types"
667 +
" ON accounts.account_type_id = account_types.account_type_id"
668 +
" JOIN data_source_info AS data_source_info"
669 +
" ON account_device_instances.data_source_obj_id = data_source_info.obj_id"
670 + (accountTypeFilterSQL.isEmpty() ?
"" :
" WHERE " + accountTypeFilterSQL);
674 queryStr =
"SELECT DISTINCT ON ( accounts.account_id, data_source_info.device_id) " + queryStr;
677 queryStr =
"SELECT " + queryStr +
" GROUP BY accounts.account_id, data_source_info.device_id";
680 throw new TskCoreException(
"Unknown DB Type: " + db.
getDatabaseType().name());
684 try (CaseDbConnection connection = db.getConnection();
685 Statement s = connection.createStatement();
686 ResultSet rs = connection.executeQuery(s, queryStr);) {
687 ArrayList<AccountDeviceInstance> accountDeviceInstances =
new ArrayList<AccountDeviceInstance>();
689 long account_id = rs.getLong(
"account_id");
690 String deviceID = rs.getString(
"device_id");
691 final String type_name = rs.getString(
"type_name");
692 final String account_unique_identifier = rs.getString(
"account_unique_identifier");
694 Account.
Type accountType = typeNameToAccountTypeMap.get(type_name);
695 Account account =
new Account(account_id, accountType, account_unique_identifier);
699 return accountDeviceInstances;
700 }
catch (SQLException ex) {
701 throw new TskCoreException(
"Error getting account device instances. " + ex.getMessage(), ex);
728 Set<Long> accountIDs =
new HashSet<Long>();
729 Set<String> accountDeviceIDs =
new HashSet<String>();
731 accountIDs.add(adi.getAccount().getAccountID());
732 accountDeviceIDs.add(
"'" + adi.getDeviceId() +
"'");
735 Set<String> applicableFilters =
new HashSet<String>(Arrays.asList(
741 String accountIDsCSL = StringUtils.buildCSVString(accountIDs);
742 String accountDeviceIDsCSL = StringUtils.buildCSVString(accountDeviceIDs);
743 String filterSQL = getCommunicationsFilterSQL(filter, applicableFilters);
745 final String queryString
746 =
" SELECT count(DISTINCT relationships.relationship_source_obj_id) AS count,"
747 +
" data_source_info.device_id AS device_id,"
749 +
" accounts1.account_id AS account1_id,"
750 +
" accounts1.account_unique_identifier AS account1_unique_identifier,"
751 +
" account_types1.type_name AS type_name1,"
752 +
" account_types1.display_name AS display_name1,"
754 +
" accounts2.account_id AS account2_id,"
755 +
" accounts2.account_unique_identifier AS account2_unique_identifier,"
756 +
" account_types2.type_name AS type_name2,"
757 +
" account_types2.display_name AS display_name2"
758 +
" FROM account_relationships AS relationships"
759 +
" JOIN data_source_info AS data_source_info"
760 +
" ON relationships.data_source_obj_id = data_source_info.obj_id "
762 +
" JOIN accounts AS accounts1 "
763 +
" ON accounts1.account_id = relationships.account1_id"
764 +
" JOIN account_types AS account_types1"
765 +
" ON accounts1.account_type_id = account_types1.account_type_id"
767 +
" JOIN accounts AS accounts2 "
768 +
" ON accounts2.account_id = relationships.account2_id"
769 +
" JOIN account_types AS account_types2"
770 +
" ON accounts2.account_type_id = account_types2.account_type_id"
771 +
" WHERE (( relationships.account1_id IN (" + accountIDsCSL +
")) "
772 +
" AND ( relationships.account2_id IN ( " + accountIDsCSL +
" ))"
773 +
" AND ( data_source_info.device_id IN (" + accountDeviceIDsCSL +
"))) "
774 + (filterSQL.isEmpty() ?
"" :
" AND " + filterSQL)
775 +
" GROUP BY data_source_info.device_id, "
776 +
" accounts1.account_id, "
777 +
" account_types1.type_name, "
778 +
" account_types1.display_name, "
779 +
" accounts2.account_id, "
780 +
" account_types2.type_name, "
781 +
" account_types2.display_name";
783 Map<AccountPair, Long> results =
new HashMap<AccountPair, Long>();
786 try (CaseDbConnection connection = db.getConnection();
787 Statement s = connection.createStatement();
788 ResultSet rs = connection.executeQuery(s, queryString);) {
794 rs.getString(
"account1_unique_identifier")),
795 rs.getString(
"device_id"));
800 rs.getString(
"account2_unique_identifier")),
801 rs.getString(
"device_id"));
804 long count = rs.getLong(
"count");
807 Long oldCount = results.get(relationshipKey);
808 if (oldCount != null) {
811 results.put(relationshipKey, count);
814 }
catch (SQLException ex) {
815 throw new TskCoreException(
"Error getting relationships between accounts. " + ex.getMessage(), ex);
838 long account_id = accountDeviceInstance.getAccount().
getAccountID();
841 String datasourceObjIdsCSV = StringUtils.buildCSVString(
842 db.getDataSourceObjIds(accountDeviceInstance.getDeviceId()));
845 Set<String> applicableFilters =
new HashSet<String>(Arrays.asList(
849 String filterSQL = getCommunicationsFilterSQL(filter, applicableFilters);
851 String innerQuery =
" account_relationships AS relationships";
852 String limitStr = getMostRecentFilterLimitSQL(filter);
854 if (!limitStr.isEmpty()) {
855 innerQuery =
"(SELECT * FROM account_relationships as relationships " + limitStr +
") as relationships";
859 =
"SELECT count(DISTINCT relationships.relationship_source_obj_id) as count "
860 +
" FROM" + innerQuery
861 +
" WHERE relationships.data_source_obj_id IN ( " + datasourceObjIdsCSV +
" )"
862 +
" AND ( relationships.account1_id = " + account_id
863 +
" OR relationships.account2_id = " + account_id +
" )"
864 + (filterSQL.isEmpty() ?
"" :
" AND " + filterSQL);
867 try (CaseDbConnection connection = db.getConnection();
868 Statement s = connection.createStatement();
869 ResultSet rs = connection.executeQuery(s, queryStr);) {
871 return (rs.getLong(
"count"));
872 }
catch (SQLException ex) {
873 throw new TskCoreException(
"Error getting relationships count for account device instance. " + ex.getMessage(), ex);
897 if (accountDeviceInstanceList.isEmpty()) {
899 return Collections.emptySet();
902 Map<Long, Set<Long>> accountIdToDatasourceObjIdMap =
new HashMap<Long, Set<Long>>();
904 long accountID = accountDeviceInstance.getAccount().
getAccountID();
905 List<Long> dataSourceObjIds = db.getDataSourceObjIds(accountDeviceInstance.getDeviceId());
907 if (accountIdToDatasourceObjIdMap.containsKey(accountID)) {
908 accountIdToDatasourceObjIdMap.get(accountID).addAll(dataSourceObjIds);
910 accountIdToDatasourceObjIdMap.put(accountID,
new HashSet<Long>(dataSourceObjIds));
914 List<String> adiSQLClauses =
new ArrayList<String>();
915 for (Map.Entry<Long, Set<Long>> entry : accountIdToDatasourceObjIdMap.entrySet()) {
916 final Long accountID = entry.getKey();
917 String datasourceObjIdsCSV = StringUtils.buildCSVString(entry.getValue());
920 "( ( relationships.data_source_obj_id IN ( " + datasourceObjIdsCSV +
" ) )"
921 +
" AND ( relationships.account1_id = " + accountID
922 +
" OR relationships.account2_id = " + accountID +
" ) )"
925 String adiSQLClause = StringUtils.joinAsStrings(adiSQLClauses,
" OR ");
928 Set<String> applicableFilters =
new HashSet<String>(Arrays.asList(
934 String filterSQL = getCommunicationsFilterSQL(filter, applicableFilters);
936 String limitQuery =
" account_relationships AS relationships";
937 String limitStr = getMostRecentFilterLimitSQL(filter);
938 if (!limitStr.isEmpty()) {
939 limitQuery =
"(SELECT * FROM account_relationships as relationships " + limitStr +
") as relationships";
943 =
"SELECT DISTINCT artifacts.artifact_id AS artifact_id,"
944 +
" artifacts.obj_id AS obj_id,"
945 +
" artifacts.artifact_obj_id AS artifact_obj_id,"
946 +
" artifacts.data_source_obj_id AS data_source_obj_id, "
947 +
" artifacts.artifact_type_id AS artifact_type_id, "
948 +
" artifacts.review_status_id AS review_status_id "
949 +
" FROM blackboard_artifacts as artifacts"
950 +
" JOIN " + limitQuery
951 +
" ON artifacts.artifact_obj_id = relationships.relationship_source_obj_id"
953 +
" WHERE (" + adiSQLClause +
" )"
955 + (filterSQL.isEmpty() ?
"" :
" AND (" + filterSQL +
" )");
959 try (CaseDbConnection connection = db.getConnection();
960 Statement s = connection.createStatement();
961 ResultSet rs = connection.executeQuery(s, queryStr);) {
962 Set<Content> relationshipSources =
new HashSet<Content>();
966 rs.getLong(
"obj_id"), rs.getLong(
"artifact_obj_id"),
967 rs.getObject(
"data_source_obj_id") != null ? rs.getLong(
"data_source_obj_id") : null,
968 bbartType.getTypeID(),
969 bbartType.getTypeName(), bbartType.getDisplayName(),
973 return relationshipSources;
974 }
catch (SQLException ex) {
975 throw new TskCoreException(
"Error getting relationships for account. " + ex.getMessage(), ex);
997 final List<Long> dataSourceObjIds
998 = getSleuthkitCase().getDataSourceObjIds(accountDeviceInstance.getDeviceId());
1001 Set<String> applicableInnerQueryFilters =
new HashSet<String>(Arrays.asList(
1007 String innerQueryfilterSQL = getCommunicationsFilterSQL(filter, applicableInnerQueryFilters);
1009 String innerQueryTemplate
1010 =
" SELECT %1$1s as account_id,"
1011 +
" data_source_obj_id"
1012 +
" FROM account_relationships as relationships"
1013 +
" WHERE %2$1s = " + accountDeviceInstance.getAccount().getAccountID() +
""
1014 +
" AND data_source_obj_id IN (" + StringUtils.buildCSVString(dataSourceObjIds) +
")"
1015 + (innerQueryfilterSQL.isEmpty() ?
"" :
" AND " + innerQueryfilterSQL);
1017 String innerQuery1 = String.format(innerQueryTemplate,
"account1_id",
"account2_id");
1018 String innerQuery2 = String.format(innerQueryTemplate,
"account2_id",
"account1_id");
1021 String combinedInnerQuery
1022 =
"SELECT account_id, data_source_obj_id "
1023 +
" FROM ( " + innerQuery1 +
" UNION " + innerQuery2 +
" ) AS inner_union"
1024 +
" GROUP BY account_id, data_source_obj_id";
1027 Set<String> applicableFilters =
new HashSet<String>(Arrays.asList(
1031 String filterSQL = getCommunicationsFilterSQL(filter, applicableFilters);
1035 " accounts.account_id AS account_id,"
1036 +
" accounts.account_unique_identifier AS account_unique_identifier,"
1038 +
" account_types.type_name AS type_name,"
1040 +
" data_source_info.device_id AS device_id"
1041 +
" FROM ( " + combinedInnerQuery +
" ) AS account_device_instances"
1042 +
" JOIN accounts AS accounts"
1043 +
" ON accounts.account_id = account_device_instances.account_id"
1044 +
" JOIN account_types AS account_types"
1045 +
" ON accounts.account_type_id = account_types.account_type_id"
1046 +
" JOIN data_source_info AS data_source_info"
1047 +
" ON account_device_instances.data_source_obj_id = data_source_info.obj_id"
1048 + (filterSQL.isEmpty() ?
"" :
" WHERE " + filterSQL);
1052 queryStr =
"SELECT DISTINCT ON ( accounts.account_id, data_source_info.device_id) " + queryStr;
1055 queryStr =
"SELECT " + queryStr +
" GROUP BY accounts.account_id, data_source_info.device_id";
1058 throw new TskCoreException(
"Unknown DB Type: " + db.
getDatabaseType().name());
1062 try (CaseDbConnection connection = db.getConnection();
1063 Statement s = connection.createStatement();
1064 ResultSet rs = connection.executeQuery(s, queryStr);) {
1065 ArrayList<AccountDeviceInstance> accountDeviceInstances =
new ArrayList<AccountDeviceInstance>();
1067 long account_id = rs.getLong(
"account_id");
1068 String deviceID = rs.getString(
"device_id");
1069 final String type_name = rs.getString(
"type_name");
1070 final String account_unique_identifier = rs.getString(
"account_unique_identifier");
1072 Account.
Type accountType = typeNameToAccountTypeMap.get(type_name);
1073 Account account =
new Account(account_id, accountType, account_unique_identifier);
1077 return accountDeviceInstances;
1078 }
catch (SQLException ex) {
1079 throw new TskCoreException(
"Error getting account device instances. " + ex.getMessage(), ex);
1104 Set<String> applicableFilters =
new HashSet<String>(Arrays.asList(
1110 String limitQuery =
" account_relationships AS relationships";
1111 String limitStr = getMostRecentFilterLimitSQL(filter);
1112 if (!limitStr.isEmpty()) {
1113 limitQuery =
"(SELECT * FROM account_relationships as relationships " + limitStr +
") as relationships";
1116 String filterSQL = getCommunicationsFilterSQL(filter, applicableFilters);
1117 final String queryString =
"SELECT artifacts.artifact_id AS artifact_id,"
1118 +
" artifacts.obj_id AS obj_id,"
1119 +
" artifacts.artifact_obj_id AS artifact_obj_id,"
1120 +
" artifacts.data_source_obj_id AS data_source_obj_id,"
1121 +
" artifacts.artifact_type_id AS artifact_type_id,"
1122 +
" artifacts.review_status_id AS review_status_id"
1123 +
" FROM blackboard_artifacts AS artifacts"
1124 +
" JOIN " + limitQuery
1125 +
" ON artifacts.artifact_obj_id = relationships.relationship_source_obj_id"
1126 +
" WHERE (( relationships.account1_id = " + account1.getAccount().getAccountID()
1127 +
" AND relationships.account2_id = " + account2.getAccount().getAccountID()
1128 +
" ) OR ( relationships.account2_id = " + account1.getAccount().getAccountID()
1129 +
" AND relationships.account1_id =" + account2.getAccount().getAccountID() +
" ))"
1130 + (filterSQL.isEmpty() ?
"" :
" AND " + filterSQL);
1133 try (CaseDbConnection connection = db.getConnection();
1134 Statement s = connection.createStatement();
1135 ResultSet rs = connection.executeQuery(s, queryString);) {
1137 ArrayList<Content> artifacts =
new ArrayList<Content>();
1140 artifacts.add(
new BlackboardArtifact(db, rs.getLong(
"artifact_id"), rs.getLong(
"obj_id"), rs.getLong(
"artifact_obj_id"),
1141 rs.getObject(
"data_source_obj_id") != null ? rs.getLong(
"data_source_obj_id") : null,
1142 bbartType.getTypeID(), bbartType.getTypeName(), bbartType.getDisplayName(),
1147 }
catch (SQLException ex) {
1148 throw new TskCoreException(
"Error getting relationships between accounts. " + ex.getMessage(), ex);
1165 List<AccountFileInstance> accountFileInstanceList =
new ArrayList<>();
1169 if (artifactList != null && !artifactList.isEmpty()) {
1175 if (!accountFileInstanceList.isEmpty()) {
1176 return accountFileInstanceList;
1192 String query =
"SELECT DISTINCT accounts.account_type_id, type_name, display_name FROM accounts JOIN account_types ON accounts.account_type_id = account_types.account_type_id";
1193 List<
Account.
Type> inUseAccounts =
new ArrayList<>();
1196 try (CaseDbConnection connection = db.getConnection();
1197 Statement s = connection.createStatement();
1198 ResultSet rs = connection.executeQuery(s, query);) {
1201 String accountTypeName = rs.getString(
"type_name");
1202 accountType = this.typeNameToAccountTypeMap.get(accountTypeName);
1204 if (accountType == null) {
1205 accountType =
new Account.
Type(accountTypeName, rs.getString(
"display_name"));
1206 this.accountTypeToTypeIdMap.put(accountType, rs.getInt(
"account_type_id"));
1209 inUseAccounts.add(accountType);
1211 return inUseAccounts;
1212 }
catch (SQLException ex) {
1213 throw new TskCoreException(
"Error getting account type id", ex);
1229 if (artifact == null) {
1230 throw new IllegalArgumentException(
"null arugment passed to getAccountsRelatedToArtifact");
1233 List<Account> accountList =
new ArrayList<>();
1235 try (CaseDbConnection connection = db.getConnection()) {
1241 String query = String.format(
"SELECT DISTINCT (account_id), account_type_id, account_unique_identifier"
1243 +
" SELECT DISTINCT (account_id), account_type_id, account_unique_identifier"
1245 +
" JOIN account_relationships ON account1_id = account_id"
1246 +
" WHERE relationship_source_obj_id = %d"
1248 +
" SELECT DISTINCT (account_id), account_type_id, account_unique_identifier"
1250 +
" JOIN account_relationships ON account2_id = account_id"
1251 +
" WHERE relationship_source_obj_id = %d) AS unionOfRelationships", artifact.getId(), artifact.getId());
1252 try (Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery(query)) {
1255 int accountTypeId = rs.getInt(
"account_type_id");
1256 for (Map.Entry<
Account.
Type, Integer> entry : accountTypeToTypeIdMap.entrySet()) {
1257 if (entry.getValue() == accountTypeId) {
1258 accountType = entry.getKey();
1263 accountList.add(
new Account(rs.getInt(
"account_id"), accountType, rs.getString(
"account_unique_identifier")));
1265 }
catch (SQLException ex) {
1266 throw new TskCoreException(
"Unable to get account list for give artifact " + artifact.getId(), ex);
1285 if (accountTypeToTypeIdMap.containsKey(accountType)) {
1286 return accountTypeToTypeIdMap.get(accountType);
1303 private String normalizeAccountID(Account.Type accountType, String accountUniqueID)
throws InvalidAccountIDException {
1305 if (accountUniqueID == null || accountUniqueID.isEmpty()) {
1306 throw new InvalidAccountIDException(
"Account id is null or empty.");
1309 String normalizedAccountID;
1310 if (accountType.equals(Account.Type.PHONE)) {
1311 normalizedAccountID = CommunicationsUtils.normalizePhoneNum(accountUniqueID);
1312 }
else if (accountType.equals(Account.Type.EMAIL)) {
1313 normalizedAccountID = CommunicationsUtils.normalizeEmailAddress(accountUniqueID);
1315 normalizedAccountID = accountUniqueID.toLowerCase().trim();
1318 return normalizedAccountID;
1333 private String getCommunicationsFilterSQL(CommunicationsFilter commFilter, Set<String> applicableFilters) {
1334 if (null == commFilter || commFilter.getAndFilters().isEmpty()) {
1339 StringBuilder sqlSB =
new StringBuilder();
1340 boolean first =
true;
1341 for (CommunicationsFilter.SubFilter subFilter : commFilter.getAndFilters()) {
1344 if (applicableFilters.contains(subFilter.getClass().getName())) {
1345 String subfilterSQL = subFilter.getSQL(
this);
1346 if (!subfilterSQL.isEmpty()) {
1350 sqlSB.append(
" AND ");
1353 sqlSB.append(subfilterSQL);
1359 if (!sqlSB.toString().isEmpty()) {
1360 sqlStr =
"( " + sqlSB.toString() +
" )";
1373 private String getMostRecentFilterLimitSQL(CommunicationsFilter filter) {
1374 String limitStr =
"";
1376 if (filter != null && !filter.getAndFilters().isEmpty()) {
1378 for (CommunicationsFilter.SubFilter subFilter : filter.getAndFilters()) {
1379 if (subFilter.getClass().getName().equals(CommunicationsFilter.MostRecentFilter.class.getName())) {
1380 limitStr = subFilter.getSQL(
this);
Set< Content > getRelationshipSources(Set< AccountDeviceInstance > accountDeviceInstanceList, CommunicationsFilter filter)
void postArtifact(BlackboardArtifact artifact, String moduleName)
CaseDbTransaction beginTransaction()
ArrayList< BlackboardArtifact > getBlackboardArtifacts(int artifactTypeID)
Blackboard getBlackboard()
List< AccountDeviceInstance > getRelatedAccountDeviceInstances(AccountDeviceInstance accountDeviceInstance, CommunicationsFilter filter)
org.sleuthkit.datamodel.Account.Type getAccountType(String accountTypeName)
AccountFileInstance createAccountFileInstance(org.sleuthkit.datamodel.Account.Type accountType, String accountUniqueID, String moduleName, Content sourceFile)
org.sleuthkit.datamodel.Account.Type addAccountType(String accountTypeName, String displayName)
List< Account > getAccountsRelatedToArtifact(BlackboardArtifact artifact)
String getTypeSpecificID()
Map< AccountPair, Long > getRelationshipCountsPairwise(Set< AccountDeviceInstance > accounts, CommunicationsFilter filter)
void releaseSingleUserCaseReadLock()
List< Account.Type > getAccountTypesInUse()
static final List< Account.Type > PREDEFINED_ACCOUNT_TYPES
void acquireSingleUserCaseWriteLock()
BlackboardArtifact.Type getArtifactType(String artTypeName)
void releaseSingleUserCaseWriteLock()
List< Content > getRelationshipSources(AccountDeviceInstance account1, AccountDeviceInstance account2, CommunicationsFilter filter)
long getRelationshipSourcesCount(AccountDeviceInstance accountDeviceInstance, CommunicationsFilter filter)
void acquireSingleUserCaseReadLock()
List< AccountDeviceInstance > getAccountDeviceInstancesWithRelationships(CommunicationsFilter filter)
static ReviewStatus withID(int id)
void addRelationships(AccountFileInstance sender, List< AccountFileInstance > recipients, BlackboardArtifact sourceArtifact, org.sleuthkit.datamodel.Relationship.Type relationshipType, long dateTime)
Account getAccount(org.sleuthkit.datamodel.Account.Type accountType, String accountUniqueID)
List< AccountFileInstance > getAccountFileInstances(Account account)