From f137af2e236ae1b8fb1ed1a718e29ae113067d80 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 25 Apr 2016 21:18:06 +0200 Subject: [PATCH] core: add nm_utils_machine_id_read() and parse() util --- src/Makefile.am | 1 + src/nm-core-utils.c | 58 +++++++++++++++++++++++++++++++++++++++++++ src/nm-core-utils.h | 3 +++ src/tests/Makefile.am | 1 + 4 files changed, 63 insertions(+) diff --git a/src/Makefile.am b/src/Makefile.am index e567fe0a7..4ec7b4015 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -209,6 +209,7 @@ libNetworkManager_base_la_CPPFLAGS = \ -DNETWORKMANAGER_COMPILATION=NM_NETWORKMANAGER_COMPILATION_INSIDE_DAEMON \ -DNO_SYSTEMD_JOURNAL \ -DPREFIX=\"$(prefix)\" \ + -DLOCALSTATEDIR=\"$(localstatedir)\" \ -DNMSTATEDIR=\"$(nmstatedir)\" \ $(GLIB_CFLAGS) diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c index b93de1e2a..6581d6b71 100644 --- a/src/nm-core-utils.c +++ b/src/nm-core-utils.c @@ -2588,6 +2588,64 @@ nm_utils_is_specific_hostname (const char *name) /******************************************************************/ +gboolean +nm_utils_machine_id_parse (const char *id_str, /*uuid_t*/ guchar *out_uuid) +{ + int i; + guint8 v0, v1; + + if (!id_str) + return FALSE; + + for (i = 0; i < 32; i++) { + if (!g_ascii_isxdigit (id_str[i])) + return FALSE; + } + if (id_str[i] != '\0') + return FALSE; + + if (out_uuid) { + for (i = 0; i < 16; i++) { + v0 = g_ascii_xdigit_value (*(id_str++)); + v1 = g_ascii_xdigit_value (*(id_str++)); + out_uuid[i] = (v0 << 4) + v1; + } + } + return TRUE; +} + +char * +nm_utils_machine_id_read (void) +{ + gs_free char *contents = NULL; + int i; + + /* Get the machine ID from /etc/machine-id; it's always in /etc no matter + * where our configured SYSCONFDIR is. Alternatively, it might be in + * LOCALSTATEDIR /lib/dbus/machine-id. + */ + if ( !g_file_get_contents ("/etc/machine-id", &contents, NULL, NULL) + && !g_file_get_contents (LOCALSTATEDIR "/lib/dbus/machine-id", &contents, NULL, NULL)) + return FALSE; + + contents = g_strstrip (contents); + + for (i = 0; i < 32; i++) { + if (!g_ascii_isxdigit (contents[i])) + return FALSE; + if (contents[i] >= 'A' && contents[i] <= 'F') { + /* canonicalize to lower-case */ + contents[i] = 'a' + (contents[i] - 'A'); + } + } + if (contents[i] != '\0') + return FALSE; + + return nm_unauto (&contents); +} + +/*****************************************************************************/ + guint8 * nm_utils_secret_key_read (gsize *out_key_len, GError **error) { diff --git a/src/nm-core-utils.h b/src/nm-core-utils.h index 83794b092..3033c33a5 100644 --- a/src/nm-core-utils.h +++ b/src/nm-core-utils.h @@ -307,6 +307,9 @@ const char *nm_utils_ip4_property_path (const char *ifname, const char *property gboolean nm_utils_is_specific_hostname (const char *name); +char *nm_utils_machine_id_read (void); +gboolean nm_utils_machine_id_parse (const char *id_str, /*uuid_t*/ guchar *out_uuid); + guint8 *nm_utils_secret_key_read (gsize *out_key_len, GError **error); /* IPv6 Interface Identifer helpers */ diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index b93d4da3b..5b4e6b729 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -142,6 +142,7 @@ test_utils_DEPENDENCIES = \ test_utils_CPPFLAGS = \ $(AM_CPPFLAGS) \ -DPREFIX=\"/nonexistent\" \ + -DLOCALSTATEDIR=\"$(localstatedir)\" \ -DNMSTATEDIR=\"/nonsense\" test_utils_LDADD = \