diff options
author | Thomas Haller <thaller@redhat.com> | 2021-06-05 14:56:12 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2021-06-08 08:24:14 +0200 |
commit | 2b2c818e033585d34afaa4aede059be129dae0ba (patch) | |
tree | 9b6c68ce0005f8e0c0dc30be91c2c3a8bac43ce5 | |
parent | 10e5f10f9d5eddbfb14075c0f35e4955c6c3ab89 (diff) | |
download | NetworkManager-2b2c818e033585d34afaa4aede059be129dae0ba.tar.gz |
glib-aux/uuid: use NMUuid typed argument for nm_uuid_generate_from_string*()
nm_uuid_generate_from_string*() accepts an optional namespace parameter,
to seed the hashing. This previously was a UUID in string format, so it
first had to be parsed.
Rework the code to pass a NMUuid instance that can be used directly.
Also, as the type_args parameter is always of the same type, change
the argument from a void pointer to "const NMUuid *" pointer.
-rw-r--r-- | src/core/nm-core-utils.c | 14 | ||||
-rw-r--r-- | src/libnm-core-impl/tests/test-general.c | 100 | ||||
-rw-r--r-- | src/libnm-glib-aux/nm-uuid.c | 58 | ||||
-rw-r--r-- | src/libnm-glib-aux/nm-uuid.h | 29 |
4 files changed, 124 insertions, 77 deletions
diff --git a/src/core/nm-core-utils.c b/src/core/nm-core-utils.c index 7148732bc2..bb2471c6a4 100644 --- a/src/core/nm-core-utils.c +++ b/src/core/nm-core-utils.c @@ -2466,7 +2466,7 @@ again: if (is_fake) { const guint8 *seed_bin; - const char * hash_seed; + const NMUuid *hash_seed; gsize seed_len; if (!allow_fake) { @@ -2476,6 +2476,9 @@ again: } if (nm_utils_host_id_get(&seed_bin, &seed_len)) { + static const NMUuid u = + NM_UUID_INIT(ab, 08, 5f, 06, b6, 29, 46, d1, a5, 53, 84, ee, ba, 56, 83, b6); + /* We have no valid machine-id but we have a valid secrey_key. * Generate a fake machine ID by hashing the secret-key. The secret_key * is commonly persisted, so it should be stable across reboots (despite @@ -2488,8 +2491,11 @@ again: * will call _machine_id_get(FALSE), so it won't allow accessing a fake * machine-id, thus avoiding the problem. */ fake_type = "secret-key"; - hash_seed = "ab085f06-b629-46d1-a553-84eeba5683b6"; + hash_seed = &u; } else { + static const NMUuid u = + NM_UUID_INIT(7f, f0, c8, f5, 53, 99, 49, 01, ab, 63, 61, bf, 59, 4a, be, 8b); + /* the secret-key is not valid/persistent either. That happens when we fail * to read/write the secret-key to disk. Fallback to boot-id. The boot-id * itself may be fake and randomly generated ad-hoc, but that is as best @@ -2497,7 +2503,7 @@ again: seed_bin = (const guint8 *) nm_utils_boot_id_bin(); seed_len = sizeof(NMUuid); fake_type = "boot-id"; - hash_seed = "7ff0c8f5-5399-4901-ab63-61bf594abe8b"; + hash_seed = &u; } /* the fake machine-id is based on secret-key/boot-id, but we hash it @@ -2506,7 +2512,7 @@ again: (const char *) seed_bin, seed_len, NM_UUID_TYPE_VERSION5, - (gpointer) hash_seed); + hash_seed); } if (!g_once_init_enter(&lock)) diff --git a/src/libnm-core-impl/tests/test-general.c b/src/libnm-core-impl/tests/test-general.c index dc5780ab00..dc51fe25ce 100644 --- a/src/libnm-core-impl/tests/test-general.c +++ b/src/libnm-core-impl/tests/test-general.c @@ -7236,11 +7236,10 @@ test_nm_strquote(void) /*****************************************************************************/ -#define UUID_NS_ZERO "00000000-0000-0000-0000-000000000000" -#define UUID_NS_DNS "6ba7b810-9dad-11d1-80b4-00c04fd430c8" -#define UUID_NS_URL "6ba7b811-9dad-11d1-80b4-00c04fd430c8" -#define UUID_NS_OID "6ba7b812-9dad-11d1-80b4-00c04fd430c8" -#define UUID_NS_X500 "6ba7b814-9dad-11d1-80b4-00c04fd430c8" +#define NM_UUID_NS_DNS "6ba7b810-9dad-11d1-80b4-00c04fd430c8" +#define NM_UUID_NS_URL "6ba7b811-9dad-11d1-80b4-00c04fd430c8" +#define NM_UUID_NS_OID "6ba7b812-9dad-11d1-80b4-00c04fd430c8" +#define NM_UUID_NS_X500 "6ba7b814-9dad-11d1-80b4-00c04fd430c8" static const NMUuid * _uuid(const char *str) @@ -7257,11 +7256,18 @@ _test_uuid(int uuid_type, const char *expected_uuid, const char *str, gssize slen, - gpointer type_args) + const char *type_args) { - gs_free char *uuid_test = NULL; + gs_free char *uuid_test = NULL; + NMUuid type_args_u = NM_UUID_INIT_ZERO(); - uuid_test = nm_uuid_generate_from_string_str(str, slen, uuid_type, type_args); + if (type_args) { + if (!nm_uuid_parse(type_args, &type_args_u)) + g_assert_not_reached(); + } + + uuid_test = + nm_uuid_generate_from_string_str(str, slen, uuid_type, type_args ? &type_args_u : NULL); g_assert(uuid_test); g_assert(nm_utils_is_uuid(uuid_test)); @@ -7272,7 +7278,7 @@ _test_uuid(int uuid_type, str, (long long) slen, NM_IN_SET(uuid_type, NM_UUID_TYPE_VERSION3, NM_UUID_TYPE_VERSION5) - ? (((const char *) type_args) ?: "(all-zero)") + ? (type_args ?: "(all-zero)") : (type_args ? "(unknown)" : "(null)"), uuid_test, expected_uuid); @@ -7287,8 +7293,8 @@ _test_uuid(int uuid_type, } if (NM_IN_SET(uuid_type, NM_UUID_TYPE_VERSION3, NM_UUID_TYPE_VERSION5) && !type_args) { - /* For version3 and version5, a missing @type_args is equal to UUID_NS_ZERO */ - _test_uuid(uuid_type, expected_uuid, str, slen, UUID_NS_ZERO); + /* For version3 and version5, a missing @type_args is equal to NM_UUID_NS_ZERO */ + _test_uuid(uuid_type, expected_uuid, str, slen, NM_UUID_NS_ZERO); } } @@ -7722,33 +7728,53 @@ test_nm_utils_uuid_generate_from_string(void) _test_uuid(NM_UUID_TYPE_VERSION3, "96e17d7a-ac89-38cf-95e1-bf5098da34e1", "test", -1, NULL); _test_uuid(NM_UUID_TYPE_VERSION3, "8156568e-4ae6-3f34-a93e-18e2c6cbbf78", "a\0b", 3, NULL); - _test_uuid(NM_UUID_TYPE_VERSION3, "c87ee674-4ddc-3efe-a74e-dfe25da5d7b3", "", -1, UUID_NS_DNS); - _test_uuid(NM_UUID_TYPE_VERSION3, "4c104dd0-4821-30d5-9ce3-0e7a1f8b7c0d", "a", -1, UUID_NS_DNS); + _test_uuid(NM_UUID_TYPE_VERSION3, + "c87ee674-4ddc-3efe-a74e-dfe25da5d7b3", + "", + -1, + NM_UUID_NS_DNS); + _test_uuid(NM_UUID_TYPE_VERSION3, + "4c104dd0-4821-30d5-9ce3-0e7a1f8b7c0d", + "a", + -1, + NM_UUID_NS_DNS); _test_uuid(NM_UUID_TYPE_VERSION3, "45a113ac-c7f2-30b0-90a5-a399ab912716", "test", -1, - UUID_NS_DNS); + NM_UUID_NS_DNS); _test_uuid(NM_UUID_TYPE_VERSION3, "002a0ada-f547-375a-bab5-896a11d1927e", "a\0b", 3, - UUID_NS_DNS); + NM_UUID_NS_DNS); _test_uuid(NM_UUID_TYPE_VERSION3, "9a75f5f2-195e-31a9-9d07-8c18b5d3b285", "test123", -1, - UUID_NS_DNS); - _test_uuid(NM_UUID_TYPE_VERSION3, "ec794efe-a384-3b11-a0b6-ec8995bc6acc", "x", -1, UUID_NS_DNS); + NM_UUID_NS_DNS); + _test_uuid(NM_UUID_TYPE_VERSION3, + "ec794efe-a384-3b11-a0b6-ec8995bc6acc", + "x", + -1, + NM_UUID_NS_DNS); _test_uuid(NM_UUID_TYPE_VERSION5, "a7650b9f-f19f-5300-8a13-91160ea8de2c", "a\0b", 3, NULL); - _test_uuid(NM_UUID_TYPE_VERSION5, "4f3f2898-69e3-5a0d-820a-c4e87987dbce", "a", -1, UUID_NS_DNS); - _test_uuid(NM_UUID_TYPE_VERSION5, "05b16a01-46c6-56dd-bd6e-c6dfb4a1427a", "x", -1, UUID_NS_DNS); + _test_uuid(NM_UUID_TYPE_VERSION5, + "4f3f2898-69e3-5a0d-820a-c4e87987dbce", + "a", + -1, + NM_UUID_NS_DNS); + _test_uuid(NM_UUID_TYPE_VERSION5, + "05b16a01-46c6-56dd-bd6e-c6dfb4a1427a", + "x", + -1, + NM_UUID_NS_DNS); _test_uuid(NM_UUID_TYPE_VERSION5, "c9ed566a-6b79-5d3a-b2b7-96a936b48cf3", "test123", -1, - UUID_NS_DNS); + NM_UUID_NS_DNS); for (i = 0; i < G_N_ELEMENTS(zero_uuids); i++) { nm_sprintf_buf(i_str, "%u", i), @@ -7757,8 +7783,8 @@ test_nm_utils_uuid_generate_from_string(void) } for (i = 0; i < G_N_ELEMENTS(dns_uuids); i++) { nm_sprintf_buf(i_str, "%u", i), - _test_uuid(NM_UUID_TYPE_VERSION3, dns_uuids[i].uuid3, i_str, -1, UUID_NS_DNS); - _test_uuid(NM_UUID_TYPE_VERSION5, dns_uuids[i].uuid5, i_str, -1, UUID_NS_DNS); + _test_uuid(NM_UUID_TYPE_VERSION3, dns_uuids[i].uuid3, i_str, -1, NM_UUID_NS_DNS); + _test_uuid(NM_UUID_TYPE_VERSION5, dns_uuids[i].uuid5, i_str, -1, NM_UUID_NS_DNS); } /* examples from cpython unit tests: */ @@ -7766,48 +7792,48 @@ test_nm_utils_uuid_generate_from_string(void) "6fa459ea-ee8a-3ca4-894e-db77e160355e", "python.org", -1, - UUID_NS_DNS); + NM_UUID_NS_DNS); _test_uuid(NM_UUID_TYPE_VERSION5, "886313e1-3b8a-5372-9b90-0c9aee199e5d", "python.org", -1, - UUID_NS_DNS); + NM_UUID_NS_DNS); _test_uuid(NM_UUID_TYPE_VERSION3, "9fe8e8c4-aaa8-32a9-a55c-4535a88b748d", "http://python.org/", -1, - UUID_NS_URL); + NM_UUID_NS_URL); _test_uuid(NM_UUID_TYPE_VERSION5, "4c565f0d-3f5a-5890-b41b-20cf47701c5e", "http://python.org/", -1, - UUID_NS_URL); + NM_UUID_NS_URL); _test_uuid(NM_UUID_TYPE_VERSION3, "dd1a1cef-13d5-368a-ad82-eca71acd4cd1", "1.3.6.1", -1, - UUID_NS_OID); + NM_UUID_NS_OID); _test_uuid(NM_UUID_TYPE_VERSION5, "1447fa61-5277-5fef-a9b3-fbc6e44f4af3", "1.3.6.1", -1, - UUID_NS_OID); + NM_UUID_NS_OID); _test_uuid(NM_UUID_TYPE_VERSION3, "658d3002-db6b-3040-a1d1-8ddd7d189a4d", "c=ca", -1, - UUID_NS_X500); + NM_UUID_NS_X500); _test_uuid(NM_UUID_TYPE_VERSION5, "cc957dd1-a972-5349-98cd-874190002798", "c=ca", -1, - UUID_NS_X500); + NM_UUID_NS_X500); _test_uuid(NM_UUID_TYPE_VERSION5, "74738ff5-5367-5958-9aee-98fffdcd1876", "www.example.org", -1, - UUID_NS_DNS); + NM_UUID_NS_DNS); } /*****************************************************************************/ @@ -7827,7 +7853,7 @@ __test_uuid(const char *expected_uuid, const char *str, gssize slen, char *uuid_ } g_free(uuid_test); - uuid_test = nm_uuid_generate_from_string_str(str, slen, NM_UUID_TYPE_VERSION3, NM_UUID_NS1); + uuid_test = nm_uuid_generate_from_string_str(str, slen, NM_UUID_TYPE_VERSION3, &nm_uuid_ns_1); g_assert(uuid_test); g_assert(nm_utils_is_uuid(uuid_test)); @@ -7848,15 +7874,23 @@ __test_uuid(const char *expected_uuid, const char *str, gssize slen, char *uuid_ static void test_nm_utils_uuid_generate_from_strings(void) { - const NMUuid uuid0 = {}; + const NMUuid uuid0 = NM_UUID_INIT_ZERO(); + const NMUuid uuid1 = {}; + char buf[37]; g_assert_cmpmem(&uuid0, sizeof(uuid0), _uuid("00000000-0000-0000-0000-000000000000"), 16); + g_assert_cmpmem(&uuid0, sizeof(NMUuid), &uuid1, sizeof(NMUuid)); + g_assert(nm_uuid_is_null(NULL)); g_assert(nm_uuid_is_null(&uuid0)); + g_assert(nm_uuid_is_null(&nm_uuid_ns_zero)); g_assert(nm_uuid_is_null(_uuid("00000000-0000-0000-0000-000000000000"))); g_assert(!nm_uuid_is_null(_uuid("10000000-0000-0000-0000-000000000000"))); + g_assert_cmpstr(NM_UUID_NS_1, ==, nm_uuid_unparse(&nm_uuid_ns_1, buf)); + g_assert_cmpstr(NM_UUID_NS_ZERO, ==, nm_uuid_unparse(&nm_uuid_ns_zero, buf)); + _test_uuid("b07c334a-399b-32de-8d50-58e4e08f98e3", "", 0, NULL); _test_uuid("b8a426cb-bcb5-30a3-bd8f-6786fea72df9", "\0", 1, ""); _test_uuid("12a4a982-7aae-39e1-951e-41aeb1250959", "a\0", 2, "a"); diff --git a/src/libnm-glib-aux/nm-uuid.c b/src/libnm-glib-aux/nm-uuid.c index 2c6e218a7c..0e3f4673e7 100644 --- a/src/libnm-glib-aux/nm-uuid.c +++ b/src/libnm-glib-aux/nm-uuid.c @@ -9,6 +9,13 @@ /*****************************************************************************/ +const NMUuid nm_uuid_ns_zero = + NM_UUID_INIT(00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00); + +/* arbitrarily chosen namespace UUID for nm_uuid_generate_from_strings() */ +const NMUuid nm_uuid_ns_1 = + NM_UUID_INIT(b4, 25, e9, fb, 75, 98, 44, b4, 9e, 3b, 5a, 2e, 3a, aa, 49, 05); + char * nm_uuid_unparse_case(const NMUuid *uuid, char out_str[static 37], gboolean upper_case) { @@ -239,11 +246,12 @@ nm_uuid_is_valid_nm(const char *str, str_lower[i] = g_ascii_tolower(str_lower[i]); /* The namespace UUID is chosen randomly. */ - nm_uuid_generate_from_string(&uuid, - str_lower, - -1, - NM_UUID_TYPE_VERSION5, - "4e72f709-ca95-4405-9053-1f43294a618c"); + nm_uuid_generate_from_string( + &uuid, + str_lower, + -1, + NM_UUID_TYPE_VERSION5, + &NM_UUID_INIT(4e, 72, f7, 09, ca, 95, 44, 05, 90, 53, 1f, 43, 29, 4a, 61, 8c)); nm_uuid_unparse(&uuid, out_normalized_str); } return TRUE; @@ -299,11 +307,11 @@ nm_uuid_generate_random_str(char buf[static 37]) * Returns: the input @uuid. This function cannot fail. **/ NMUuid * -nm_uuid_generate_from_string(NMUuid * uuid, - const char *s, - gssize slen, - NMUuidType uuid_type, - gpointer type_args) +nm_uuid_generate_from_string(NMUuid * uuid, + const char * s, + gssize slen, + NMUuidType uuid_type, + const NMUuid *type_args) { g_return_val_if_fail(uuid, FALSE); g_return_val_if_fail(slen == 0 || s, FALSE); @@ -313,26 +321,20 @@ nm_uuid_generate_from_string(NMUuid * uuid, switch (uuid_type) { case NM_UUID_TYPE_LEGACY: - g_return_val_if_fail(!type_args, NULL); + nm_assert(!type_args); nm_crypto_md5_hash(NULL, 0, (guint8 *) s, slen, (guint8 *) uuid, sizeof(*uuid)); break; case NM_UUID_TYPE_VERSION3: case NM_UUID_TYPE_VERSION5: { - NMUuid ns_uuid; - - if (type_args) { - /* type_args can be a name space UUID. Interpret it as (char *) */ - if (!nm_uuid_parse(type_args, &ns_uuid)) - g_return_val_if_reached(NULL); - } else - ns_uuid = (NMUuid){}; + if (!type_args) + type_args = &nm_uuid_ns_zero; if (uuid_type == NM_UUID_TYPE_VERSION3) { nm_crypto_md5_hash((guint8 *) s, slen, - (guint8 *) &ns_uuid, - sizeof(ns_uuid), + (guint8 *) type_args, + sizeof(*type_args), (guint8 *) uuid, sizeof(*uuid)); } else { @@ -343,7 +345,7 @@ nm_uuid_generate_from_string(NMUuid * uuid, } digest; sum = g_checksum_new(G_CHECKSUM_SHA1); - g_checksum_update(sum, (guchar *) &ns_uuid, sizeof(ns_uuid)); + g_checksum_update(sum, (guchar *) type_args, sizeof(*type_args)); g_checksum_update(sum, (guchar *) s, slen); nm_utils_checksum_get_digest(sum, digest.sha1); @@ -377,10 +379,10 @@ nm_uuid_generate_from_string(NMUuid * uuid, * object's #NMSettingConnection:id: property **/ char * -nm_uuid_generate_from_string_str(const char *s, - gssize slen, - NMUuidType uuid_type, - gpointer type_args) +nm_uuid_generate_from_string_str(const char * s, + gssize slen, + NMUuidType uuid_type, + const NMUuid *type_args) { NMUuid uuid; @@ -405,7 +407,7 @@ char * nm_uuid_generate_from_strings(const char *string1, ...) { if (!string1) - return nm_uuid_generate_from_string_str(NULL, 0, NM_UUID_TYPE_VERSION3, NM_UUID_NS1); + return nm_uuid_generate_from_string_str(NULL, 0, NM_UUID_TYPE_VERSION3, &nm_uuid_ns_1); { nm_auto_str_buf NMStrBuf str = NM_STR_BUF_INIT(NM_UTILS_GET_NEXT_REALLOC_SIZE_104, FALSE); @@ -425,6 +427,6 @@ nm_uuid_generate_from_strings(const char *string1, ...) return nm_uuid_generate_from_string_str(nm_str_buf_get_str_unsafe(&str), str.len, NM_UUID_TYPE_VERSION3, - NM_UUID_NS1); + &nm_uuid_ns_1); } } diff --git a/src/libnm-glib-aux/nm-uuid.h b/src/libnm-glib-aux/nm-uuid.h index fb0f6780a2..504ec789be 100644 --- a/src/libnm-glib-aux/nm-uuid.h +++ b/src/libnm-glib-aux/nm-uuid.h @@ -95,25 +95,30 @@ char *nm_uuid_generate_random_str(char buf[static 37]); /*****************************************************************************/ +extern const NMUuid nm_uuid_ns_zero; +extern const NMUuid nm_uuid_ns_1; + +#define NM_UUID_NS_ZERO "00000000-0000-0000-0000-000000000000" +#define NM_UUID_NS_1 "b425e9fb-7598-44b4-9e3b-5a2e3aaa4905" + +/*****************************************************************************/ + typedef enum { NM_UUID_TYPE_LEGACY = 0, NM_UUID_TYPE_VERSION3 = 3, NM_UUID_TYPE_VERSION5 = 5, } NMUuidType; -NMUuid *nm_uuid_generate_from_string(NMUuid * uuid, - const char *s, - gssize slen, - NMUuidType uuid_type, - gpointer type_args); - -char *nm_uuid_generate_from_string_str(const char *s, - gssize slen, - NMUuidType uuid_type, - gpointer type_args); +NMUuid *nm_uuid_generate_from_string(NMUuid * uuid, + const char * s, + gssize slen, + NMUuidType uuid_type, + const NMUuid *type_args); -/* arbitrarily chosen namespace UUID for nm_uuid_generate_from_strings() */ -#define NM_UUID_NS1 "b425e9fb-7598-44b4-9e3b-5a2e3aaa4905" +char *nm_uuid_generate_from_string_str(const char * s, + gssize slen, + NMUuidType uuid_type, + const NMUuid *type_args); char *nm_uuid_generate_from_strings(const char *string1, ...) G_GNUC_NULL_TERMINATED; |