diff --git a/cf-agent/cf-agent.c b/cf-agent/cf-agent.c index ddcb93e4d4b..5853d959187 100644 --- a/cf-agent/cf-agent.c +++ b/cf-agent/cf-agent.c @@ -341,6 +341,8 @@ int main(int argc, char *argv[]) Nova_NoteAgentExecutionPerformance(config->input_file, start); + /* Before GenericAgentFinalize(), which deinitializes the crypto library + * needed for the file digests. */ if ((SIMULATE_JSON_FILE != NULL) && !WriteChangesJson(SIMULATE_JSON_FILE) && (ret == 0)) { diff --git a/cf-agent/simulate_mode.c b/cf-agent/simulate_mode.c index bfbdd60cfb1..0335781fe6a 100644 --- a/cf-agent/simulate_mode.c +++ b/cf-agent/simulate_mode.c @@ -43,6 +43,7 @@ #include /* GetGroupName(), GetUserName() */ #include /* JsonElement */ #include /* StringWriter() */ +#include /* HashFile(), HashPrintSafe() */ #include /* CF_PERMS_DEFAULT */ #include @@ -1174,6 +1175,43 @@ static JsonElement *ChangedFileAsJson(const char *path) existed ? "modified" : "created"); JsonObjectAppendString(file_info, "type", GetFileTypeDescription(st.st_mode)); + /* 64-bit, so that e.g. uid 4294967294 is not written as -2. */ + JsonObjectAppendInteger64(file_info, "uid", (int64_t) st.st_uid); + JsonObjectAppendInteger64(file_info, "gid", (int64_t) st.st_gid); + +#ifndef __MINGW32__ + /* The permissions of a symbolic link are meaningless. */ + if (S_ISLNK(st.st_mode)) + { + return file_info; + } +#endif /* !__MINGW32__ */ + + char perms[5]; + xsnprintf(perms, sizeof(perms), "%04o", st.st_mode & CHMOD_MODE_BITS); + JsonObjectAppendString(file_info, "permissions", perms); + + if (S_ISREG(st.st_mode)) + { + JsonObjectAppendInteger64(file_info, "size", (int64_t) st.st_size); + + /* HashFile() cannot report failure, it leaves the digest zeroed. */ + static const unsigned char no_digest[EVP_MAX_MD_SIZE + 1] = {0}; + unsigned char digest[EVP_MAX_MD_SIZE + 1]; + HashFile(chrooted_path, digest, HASH_METHOD_SHA256, false); + if (memcmp(digest, no_digest, sizeof(digest)) != 0) + { + char digest_str[CF_HOSTKEY_STRING_SIZE]; + HashPrintSafe(digest_str, sizeof(digest_str), digest, + HASH_METHOD_SHA256, false); + JsonObjectAppendString(file_info, "sha256", digest_str); + } + else + { + Log(LOG_LEVEL_ERR, "Failed to compute the SHA-256 digest of '%s'", + chrooted_path); + } + } return file_info; } diff --git a/tests/acceptance/29_simulate_mode/simulate_json.cf.expected b/tests/acceptance/29_simulate_mode/simulate_json.cf.expected index d7006261d1b..14ec57a300b 100644 --- a/tests/acceptance/29_simulate_mode/simulate_json.cf.expected +++ b/tests/acceptance/29_simulate_mode/simulate_json.cf.expected @@ -2,13 +2,23 @@ "files": [ { "change": "created", + "gid": GID, "path": "WORKDIR/tmp/create-me", - "type": "regular file" + "permissions": "0600", + "sha256": "406effb1e9c59672c66a598c2b21e331b23b16c54024e96d6df3e7c173549791", + "size": 7, + "type": "regular file", + "uid": UID }, { "change": "modified", + "gid": GID, "path": "WORKDIR/tmp/modify-me", - "type": "regular file" + "permissions": "0600", + "sha256": "f39592393ef0859cb196a52693d2cea00fb2df784b3c04ae54aa7cadb8e562f8", + "size": 5, + "type": "regular file", + "uid": UID }, { "change": "deleted", @@ -16,8 +26,10 @@ }, { "change": "created", + "gid": GID, "path": "WORKDIR/tmp/link", - "type": "symbolic link" + "type": "symbolic link", + "uid": UID } ], "format_version": 1, diff --git a/tests/unit/simulate_mode_test.c b/tests/unit/simulate_mode_test.c index 1fffe875e3c..354190ce32c 100644 --- a/tests/unit/simulate_mode_test.c +++ b/tests/unit/simulate_mode_test.c @@ -292,6 +292,7 @@ static void test_json_created_file(void) strlcpy(chrooted, ToChangesChroot(path), sizeof(chrooted)); assert_true(MakeParentDirectory(chrooted, true, NULL)); create_file(chrooted, "Hello, CFEngine!\n"); + assert_int_equal(chmod(chrooted, 0640), 0); write_records(CHROOT_CHANGES_LIST_FILE, paths, 2); JsonElement *json = write_and_parse_changes(); @@ -306,6 +307,13 @@ static void test_json_created_file(void) "created"); assert_string_equal(JsonObjectGetAsString(file_info, "type"), "regular file"); + assert_string_equal(JsonObjectGetAsString(file_info, "permissions"), + "0640"); + assert_int_equal( + JsonPrimitiveGetAsInteger(JsonObjectGet(file_info, "size")), 17); + assert_string_equal( + JsonObjectGetAsString(file_info, "sha256"), + "9be7023e1f91bae9d1f734b49c579cc2091c71924ae7494c9a5a3a8006527615"); JsonDestroy(json); }