diff --git a/bindings/java/src/org/sleuthkit/datamodel/OsAccount.java b/bindings/java/src/org/sleuthkit/datamodel/OsAccount.java index 717f3fc7126d6e44d74247c59304ac22a904a743..5b896deb7f3e8f5f32b521375c1e514b6ccdbc84 100644 --- a/bindings/java/src/org/sleuthkit/datamodel/OsAccount.java +++ b/bindings/java/src/org/sleuthkit/datamodel/OsAccount.java @@ -64,6 +64,7 @@ public final class OsAccount extends AbstractContent { private boolean isDirty = false; // indicates that some member value has changed since construction and it should be updated in the database. + /** * Encapsulates status of an account - whether is it active or disabled or * deleted. @@ -371,18 +372,13 @@ void resetDirty() { } /** - * Adds account attributes to the account. Attributes can be at a host-level - * or domain-level (for domain-scoped accounts). - * - * @param osAccountAttributes List of attributes to add. - * - * @throws TskCoreException - * - * @throws org.sleuthkit.datamodel.TskCoreException + * This function is used by OsAccountManger to update the list of + * OsAccount attributes. + * + * @param osAccountAttribute The osAccount Attribute that is to be added. */ - public void addAttributes(List<OsAccountAttribute> osAccountAttributes) throws TskCoreException { - sleuthkitCase.getOsAccountManager().addOsAccountAttributes(this, osAccountAttributes); - osAccountAttributes.addAll(osAccountAttributes); + void setAttributesInternal(List<OsAccountAttribute> osAccountAttributes) { + this.osAccountAttributes = osAccountAttributes; } /** @@ -485,7 +481,7 @@ public OsAccountDbStatus getOsAccountDbStatus() { * * @throws TskCoreException */ - public List<OsAccountAttribute> getOsAccountAttributes() throws TskCoreException { + public synchronized List<OsAccountAttribute> getOsAccountAttributes() throws TskCoreException { if (osAccountAttributes == null) { osAccountAttributes = sleuthkitCase.getOsAccountManager().getOsAccountAttributes(this); } diff --git a/bindings/java/src/org/sleuthkit/datamodel/OsAccountManager.java b/bindings/java/src/org/sleuthkit/datamodel/OsAccountManager.java index c74475e52a46ed1061a23b64f90cfe4675aab06d..f5cfed31c83af8cc7e557ff19b72b3f2e20f803a 100644 --- a/bindings/java/src/org/sleuthkit/datamodel/OsAccountManager.java +++ b/bindings/java/src/org/sleuthkit/datamodel/OsAccountManager.java @@ -964,74 +964,77 @@ public Optional<OsAccount> getWindowsOsAccount(String sid, String loginName, Str * * @throws TskCoreException, */ - void addOsAccountAttributes(OsAccount account, List<OsAccountAttribute> accountAttributes) throws TskCoreException { + public void addOsAccountAttributes(OsAccount account, List<OsAccountAttribute> accountAttributes) throws TskCoreException { + + synchronized (account) { // synchronized to prevent multiple threads trying to add osAccount attributes concurrently to the same osAccount. + db.acquireSingleUserCaseWriteLock(); - db.acquireSingleUserCaseWriteLock(); + try (CaseDbConnection connection = db.getConnection()) { + for (OsAccountAttribute accountAttribute : accountAttributes) { - try (CaseDbConnection connection = db.getConnection()) { - for (OsAccountAttribute accountAttribute : accountAttributes) { + String attributeInsertSQL = "INSERT INTO tsk_os_account_attributes(os_account_obj_id, host_id, source_obj_id, attribute_type_id, value_type, value_byte, value_text, value_int32, value_int64, value_double)" + + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; // NON-NLS - String attributeInsertSQL = "INSERT INTO tsk_os_account_attributes(os_account_obj_id, host_id, source_obj_id, attribute_type_id, value_type, value_byte, value_text, value_int32, value_int64, value_double)" - + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; // NON-NLS + PreparedStatement preparedStatement = connection.getPreparedStatement(attributeInsertSQL, Statement.RETURN_GENERATED_KEYS); + preparedStatement.clearParameters(); - PreparedStatement preparedStatement = connection.getPreparedStatement(attributeInsertSQL, Statement.RETURN_GENERATED_KEYS); - preparedStatement.clearParameters(); + preparedStatement.setLong(1, account.getId()); + if (accountAttribute.getHostId().isPresent()) { + preparedStatement.setLong(2, accountAttribute.getHostId().get()); + } else { + preparedStatement.setNull(2, java.sql.Types.NULL); + } + if (accountAttribute.getSourceObjectId().isPresent()) { + preparedStatement.setLong(3, accountAttribute.getSourceObjectId().get()); + } else { + preparedStatement.setNull(3, java.sql.Types.NULL); + } - preparedStatement.setLong(1, account.getId()); - if (accountAttribute.getHostId().isPresent()) { - preparedStatement.setLong(2, accountAttribute.getHostId().get()); - } else { - preparedStatement.setNull(2, java.sql.Types.NULL); - } - if (accountAttribute.getSourceObjectId().isPresent()) { - preparedStatement.setLong(3, accountAttribute.getSourceObjectId().get()); - } else { - preparedStatement.setNull(3, java.sql.Types.NULL); - } + preparedStatement.setLong(4, accountAttribute.getAttributeType().getTypeID()); + preparedStatement.setLong(5, accountAttribute.getAttributeType().getValueType().getType()); - preparedStatement.setLong(4, accountAttribute.getAttributeType().getTypeID()); - preparedStatement.setLong(5, accountAttribute.getAttributeType().getValueType().getType()); + if (accountAttribute.getAttributeType().getValueType() == TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.BYTE) { + preparedStatement.setBytes(6, accountAttribute.getValueBytes()); + } else { + preparedStatement.setBytes(6, null); + } - if (accountAttribute.getAttributeType().getValueType() == TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.BYTE) { - preparedStatement.setBytes(6, accountAttribute.getValueBytes()); - } else { - preparedStatement.setBytes(6, null); - } + if (accountAttribute.getAttributeType().getValueType() == TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.STRING + || accountAttribute.getAttributeType().getValueType() == TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.JSON) { + preparedStatement.setString(7, accountAttribute.getValueString()); + } else { + preparedStatement.setString(7, null); + } + if (accountAttribute.getAttributeType().getValueType() == TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.INTEGER) { + preparedStatement.setInt(8, accountAttribute.getValueInt()); + } else { + preparedStatement.setNull(8, java.sql.Types.NULL); + } - if (accountAttribute.getAttributeType().getValueType() == TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.STRING - || accountAttribute.getAttributeType().getValueType() == TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.JSON) { - preparedStatement.setString(7, accountAttribute.getValueString()); - } else { - preparedStatement.setString(7, null); - } - if (accountAttribute.getAttributeType().getValueType() == TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.INTEGER) { - preparedStatement.setInt(8, accountAttribute.getValueInt()); - } else { - preparedStatement.setNull(8, java.sql.Types.NULL); - } + if (accountAttribute.getAttributeType().getValueType() == TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.DATETIME + || accountAttribute.getAttributeType().getValueType() == TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.LONG) { + preparedStatement.setLong(9, accountAttribute.getValueLong()); + } else { + preparedStatement.setNull(9, java.sql.Types.NULL); + } - if (accountAttribute.getAttributeType().getValueType() == TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.DATETIME - || accountAttribute.getAttributeType().getValueType() == TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.LONG) { - preparedStatement.setLong(9, accountAttribute.getValueLong()); - } else { - preparedStatement.setNull(9, java.sql.Types.NULL); - } + if (accountAttribute.getAttributeType().getValueType() == TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.DOUBLE) { + preparedStatement.setDouble(10, accountAttribute.getValueDouble()); + } else { + preparedStatement.setNull(10, java.sql.Types.NULL); + } - if (accountAttribute.getAttributeType().getValueType() == TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.DOUBLE) { - preparedStatement.setDouble(10, accountAttribute.getValueDouble()); - } else { - preparedStatement.setNull(10, java.sql.Types.NULL); + connection.executeUpdate(preparedStatement); } - - connection.executeUpdate(preparedStatement); - + } catch (SQLException ex) { + throw new TskCoreException(String.format("Error adding OS Account attribute for account id = %d", account.getId()), ex); + } finally { + db.releaseSingleUserCaseWriteLock(); } - } catch (SQLException ex) { - throw new TskCoreException(String.format("Error adding OS Account attribute for account id = %d", account.getId()), ex); - } finally { - db.releaseSingleUserCaseWriteLock(); + List<OsAccountAttribute> currentAttribsList = getOsAccountAttributes(account); + currentAttribsList.addAll(accountAttributes); + account.setAttributesInternal(currentAttribsList); } - fireChangeEvent(account); }