Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cf-agent/cf-agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
38 changes: 38 additions & 0 deletions cf-agent/simulate_mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <unix.h> /* GetGroupName(), GetUserName() */
#include <json.h> /* JsonElement */
#include <writer.h> /* StringWriter() */
#include <hash.h> /* HashFile(), HashPrintSafe() */
#include <definitions.h> /* CF_PERMS_DEFAULT */

#include <simulate_mode.h>
Expand Down Expand Up @@ -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;
}

Expand Down
18 changes: 15 additions & 3 deletions tests/acceptance/29_simulate_mode/simulate_json.cf.expected
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,34 @@
"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",
"path": "WORKDIR/tmp/delete-me"
},
{
"change": "created",
"gid": GID,
"path": "WORKDIR/tmp/link",
"type": "symbolic link"
"type": "symbolic link",
"uid": UID
}
],
"format_version": 1,
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/simulate_mode_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}

Expand Down