diff --git a/src/flb_utils.c b/src/flb_utils.c index 2f445980d84..7c287de05e4 100644 --- a/src/flb_utils.c +++ b/src/flb_utils.c @@ -1403,7 +1403,7 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Cryptography"), 0, - KEY_QUERY_VALUE, + KEY_QUERY_VALUE|KEY_WOW64_64KEY, &hKey); if (status != ERROR_SUCCESS) { @@ -1420,9 +1420,16 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) return -1; } - *out_size = dwBufSize; + memcpy(*out_id, buf, dwBufSize); + + /* RegQueryValueEx sets dwBufSize to strlen()+1 for the NULL + * terminator, but we only need strlen(). */ + *out_size = dwBufSize-1; return 0; } + else { + flb_error("unable to retrieve MachineGUID, error code: %d", status); + } #elif defined (FLB_SYSTEM_MACOS) bool bret; CFStringRef serialNumber; @@ -1464,6 +1471,8 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) } #endif + flb_warn("falling back on random machine UUID"); + /* generate a random uuid */ uuid = flb_malloc(38); if (!uuid) { diff --git a/tests/internal/utils.c b/tests/internal/utils.c index bcb9053d86f..f55a3672c37 100644 --- a/tests/internal/utils.c +++ b/tests/internal/utils.c @@ -608,14 +608,38 @@ void test_flb_utils_split_quoted_errors() void test_flb_utils_get_machine_id() { int ret; + int idx; char *id = NULL; size_t size; + char *id2 = NULL; + size_t size2; ret = flb_utils_get_machine_id(&id, &size); + TEST_CHECK(ret == 0); TEST_CHECK(size != 0); TEST_CHECK(id != NULL); + for (idx = 0; idx < size; idx++) { + if (!TEST_CHECK(id[idx] != 0)) { + fprintf(stderr, "zero in ID: id[%d] = 0x%02x\n", idx, id[idx]); + } + } + + ret = flb_utils_get_machine_id(&id2, &size2); + TEST_CHECK(ret == 0); + TEST_CHECK(size2 != 0); + TEST_CHECK(id2 != NULL); + TEST_CHECK(size2 == size); + + for (idx = 0; idx < size; idx++) { + if (!TEST_CHECK(id[idx] == id2[idx])) { + fprintf(stderr, "bad byte in id v2 id2: id[%d] = 0x%02x, id2[%d] = 0x%02x\n", + idx, id[idx], idx, id2[idx]); + } + } + flb_free(id); + flb_free(id2); } struct size_to_bytes_check {