- Fixes rhbz#733483 as a result - Add support for generating UUIDs in Windows - Stores first time agents UUID in registry for persistence
Signed-off-by: Adam Stokes astokes@fedoraproject.org --- src/lib/CMakeLists.txt | 2 +- src/lib/utilities.c | 31 ++++++++------------------ src/lib/utilities_linux.c | 19 ++++++++++++++++ src/lib/utilities_private.h | 6 +++++ src/lib/utilities_windows.c | 49 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 22 deletions(-)
diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 1cd41d4..610040f 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -32,7 +32,7 @@ target_link_libraries(msysconfig ${pcre_LIBRARIES} mcommon ${glib_LIBRARIES})
if(WIN32) foreach (lib ${LIBS}) - target_link_libraries(${lib} wsock32 dnsapi) + target_link_libraries(${lib} wsock32 dnsapi rpcrt4) install(TARGETS ${lib} DESTINATION sbin) endforeach (lib ${LIBS}) else(WIN32) diff --git a/src/lib/utilities.c b/src/lib/utilities.c index 9d60de8..30720a4 100644 --- a/src/lib/utilities.c +++ b/src/lib/utilities.c @@ -382,6 +382,16 @@ mh_dnsdomainname(void) }
const char * +mh_uuid(void) +{ + const char *res; + + res = mh_os_uuid(); + + return res; +} + +const char * mh_hostname(void) { static char *hostname = NULL; @@ -437,27 +447,6 @@ char *mh_file_first_line(const char *file) return buffer; }
-const char * -mh_uuid(void) -{ - static char *uuid = NULL; - -#ifdef __linux__ - if (uuid == NULL && g_file_test("/etc/machine-id", G_FILE_TEST_EXISTS)) { - uuid = mh_file_first_line("/etc/machine-id"); - } - if (uuid == NULL && g_file_test("/var/lib/dbus/machine-id", G_FILE_TEST_EXISTS)) { - /* For pre-systemd machines */ - uuid = mh_file_first_line("/var/lib/dbus/machine-id"); - } -#endif - - if(uuid) { - mh_trace("Got uuid: %s", uuid); - } - return uuid; -} - #ifdef WIN32 wchar_t * char2wide(const char *str) diff --git a/src/lib/utilities_linux.c b/src/lib/utilities_linux.c index c848802..f0aae14 100644 --- a/src/lib/utilities_linux.c +++ b/src/lib/utilities_linux.c @@ -63,3 +63,22 @@ mh_os_dnsdomainname(void)
return dnsdomainname ? dnsdomainname : ""; } + +const char * +mh_os_uuid(void) +{ + static char *uuid = NULL; + + if (uuid == NULL && g_file_test("/etc/machine-id", G_FILE_TEST_EXISTS)) { + uuid = mh_file_first_line("/etc/machine-id"); + } + if (uuid == NULL && g_file_test("/var/lib/dbus/machine-id", G_FILE_TEST_EXISTS)) { + /* For pre-systemd machines */ + uuid = mh_file_first_line("/var/lib/dbus/machine-id"); + } + + if(uuid) { + mh_trace("Got uuid: %s", uuid); + } + return uuid; +} diff --git a/src/lib/utilities_private.h b/src/lib/utilities_private.h index e39d22c..5219198 100644 --- a/src/lib/utilities_private.h +++ b/src/lib/utilities_private.h @@ -36,4 +36,10 @@ const char * mh_os_dnsdomainname(void);
+/** + * Generate UUID + */ +const char * +mh_os_uuid(void); + #endif /* __MH_UTILITIES_PRIVATE_H__ */ diff --git a/src/lib/utilities_windows.c b/src/lib/utilities_windows.c index 26a3679..c87dfe4 100644 --- a/src/lib/utilities_windows.c +++ b/src/lib/utilities_windows.c @@ -18,9 +18,14 @@
#include "config.h"
+#include <windows.h> +#include <rpc.h> #include "matahari/utilities.h" +#include "matahari/logging.h" #include "utilities_private.h"
+#define MAXUUIDLEN 255 + const char * mh_os_dnsdomainname(void) { @@ -32,3 +37,47 @@ mh_os_dnsdomainname(void)
return mh_domainname(); } + +const char * +mh_os_uuid(void) +{ + UUID u; + HKEY hKey; + DWORD nSize = MAXUUIDLEN; + wchar_t szData[MAXUUIDLEN]; + static char s[MAXUUIDLEN]; + wchar_t *szValue = char2wide("UUID"); + unsigned char __RPC_FAR *rs; + long lSuccess = RegOpenKey(HKEY_LOCAL_MACHINE, + L"SYSTEM\CurrentControlSet\services\Matahari", + &hKey); + + if (lSuccess != ERROR_SUCCESS) { + mh_debug("Could not open Matahari key from the registry: %ld", + lSuccess); + goto bail; + } + + lSuccess = RegQueryValueEx(hKey, szValue, NULL, NULL, (LPBYTE) szData, + &nSize); + if (lSuccess == ERROR_SUCCESS) { + wcstombs(s, szData, (size_t) MAXUUIDLEN); + return s; + } + + UuidCreate(&u); + + if (UuidToStringA(&u, &rs) == RPC_S_OK) { + strncpy(s,(char __RPC_FAR *)rs, MAXUUIDLEN); + s[MAXUUIDLEN - 1] = '\0'; + RpcStringFreeA(&rs); + mh_trace("Got uuid: %s", s); + lSuccess = RegSetValueEx(hKey, szValue, 0, REG_SZ, (CONST BYTE *)char2wide(s), strlen(s) * sizeof(wchar_t *)); + if (lSuccess != ERROR_SUCCESS) { + goto bail; + } + } + return s; + bail: + return ""; +} \ No newline at end of file