diff --git a/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c b/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c index 04e79725d..3bcbb71b9 100644 --- a/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c +++ b/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c @@ -1723,7 +1723,7 @@ make_user_setting(shvarFile *ifcfg) else g_string_set_size(str, 0); - if (!nms_ifcfg_rh_utils_user_key_decode(key + NM_STRLEN("NM_USER_"), str)) + if (!nm_utils_env_var_decode_name(key + NM_STRLEN("NM_USER_"), str)) continue; if (!s_user) diff --git a/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.c b/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.c index 50e352d30..b4edefbbf 100644 --- a/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.c +++ b/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.c @@ -398,115 +398,6 @@ utils_detect_ifcfg_path(const char *path, gboolean only_ifcfg) return utils_get_ifcfg_path(path); } -void -nms_ifcfg_rh_utils_user_key_encode(const char *key, GString *str_buffer) -{ - gsize i; - - nm_assert(key); - nm_assert(str_buffer); - - for (i = 0; key[i]; i++) { - char ch = key[i]; - - /* we encode the key in only upper case letters, digits, and underscore. - * As we expect lower-case letters to be more common, we encode lower-case - * letters as upper case, and upper-case letters with a leading underscore. */ - - if (ch >= '0' && ch <= '9') { - g_string_append_c(str_buffer, ch); - continue; - } - if (ch >= 'a' && ch <= 'z') { - g_string_append_c(str_buffer, ch - 'a' + 'A'); - continue; - } - if (ch == '.') { - g_string_append(str_buffer, "__"); - continue; - } - if (ch >= 'A' && ch <= 'Z') { - g_string_append_c(str_buffer, '_'); - g_string_append_c(str_buffer, ch); - continue; - } - g_string_append_printf(str_buffer, "_%03o", (unsigned) ch); - } -} - -gboolean -nms_ifcfg_rh_utils_user_key_decode(const char *name, GString *str_buffer) -{ - gsize i; - - nm_assert(name); - nm_assert(str_buffer); - - if (!name[0]) - return FALSE; - - for (i = 0; name[i];) { - char ch = name[i]; - - if (ch >= '0' && ch <= '9') { - g_string_append_c(str_buffer, ch); - i++; - continue; - } - if (ch >= 'A' && ch <= 'Z') { - g_string_append_c(str_buffer, ch - 'A' + 'a'); - i++; - continue; - } - - if (ch == '_') { - ch = name[i + 1]; - if (ch == '_') { - g_string_append_c(str_buffer, '.'); - i += 2; - continue; - } - if (ch >= 'A' && ch <= 'Z') { - g_string_append_c(str_buffer, ch); - i += 2; - continue; - } - if (ch >= '0' && ch <= '7') { - char ch2, ch3; - unsigned v; - - ch2 = name[i + 2]; - if (!(ch2 >= '0' && ch2 <= '7')) - return FALSE; - - ch3 = name[i + 3]; - if (!(ch3 >= '0' && ch3 <= '7')) - return FALSE; - -#define OCTAL_VALUE(ch) ((unsigned) ((ch) - '0')) - v = (OCTAL_VALUE(ch) << 6) + (OCTAL_VALUE(ch2) << 3) + OCTAL_VALUE(ch3); - if (v > 0xFF || v == 0) - return FALSE; - ch = (char) v; - if ((ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || (ch == '.') - || (ch >= 'a' && ch <= 'z')) { - /* such characters are not expected to be encoded via - * octal representation. The encoding is invalid. */ - return FALSE; - } - g_string_append_c(str_buffer, ch); - i += 4; - continue; - } - return FALSE; - } - - return FALSE; - } - - return TRUE; -} - /*****************************************************************************/ const char *const _nm_ethtool_ifcfg_names[] = { diff --git a/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c b/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c index 07e5e64d1..617c5ef60 100644 --- a/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c +++ b/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c @@ -2604,7 +2604,7 @@ write_user_setting(NMConnection *connection, shvarFile *ifcfg, GError **error) g_string_set_size(str, 0); g_string_append(str, "NM_USER_"); - nms_ifcfg_rh_utils_user_key_encode(key, str); + nm_utils_env_var_encode_name(key, str); svSetValue(ifcfg, str->str, nm_setting_user_get_data(s_user, key)); } } diff --git a/src/libnm-glib-aux/nm-shared-utils.c b/src/libnm-glib-aux/nm-shared-utils.c index 7d623bd9d..421e4d1be 100644 --- a/src/libnm-glib-aux/nm-shared-utils.c +++ b/src/libnm-glib-aux/nm-shared-utils.c @@ -7330,3 +7330,114 @@ nm_utils_poll_finish(GAsyncResult *result, gpointer *probe_user_data, GError **e return g_task_propagate_boolean(task, error); } + +/*****************************************************************************/ + +void +nm_utils_env_var_encode_name(const char *key, GString *str_buffer) +{ + gsize i; + + nm_assert(key); + nm_assert(str_buffer); + + for (i = 0; key[i]; i++) { + char ch = key[i]; + + /* we encode the key in only upper case letters, digits, and underscore. + * As we expect lower-case letters to be more common, we encode lower-case + * letters as upper case, and upper-case letters with a leading underscore. */ + + if (ch >= '0' && ch <= '9') { + g_string_append_c(str_buffer, ch); + continue; + } + if (ch >= 'a' && ch <= 'z') { + g_string_append_c(str_buffer, ch - 'a' + 'A'); + continue; + } + if (ch == '.') { + g_string_append(str_buffer, "__"); + continue; + } + if (ch >= 'A' && ch <= 'Z') { + g_string_append_c(str_buffer, '_'); + g_string_append_c(str_buffer, ch); + continue; + } + g_string_append_printf(str_buffer, "_%03o", (unsigned) ch); + } +} + +gboolean +nm_utils_env_var_decode_name(const char *name, GString *str_buffer) +{ + gsize i; + + nm_assert(name); + nm_assert(str_buffer); + + if (!name[0]) + return FALSE; + + for (i = 0; name[i];) { + char ch = name[i]; + + if (ch >= '0' && ch <= '9') { + g_string_append_c(str_buffer, ch); + i++; + continue; + } + if (ch >= 'A' && ch <= 'Z') { + g_string_append_c(str_buffer, ch - 'A' + 'a'); + i++; + continue; + } + + if (ch == '_') { + ch = name[i + 1]; + if (ch == '_') { + g_string_append_c(str_buffer, '.'); + i += 2; + continue; + } + if (ch >= 'A' && ch <= 'Z') { + g_string_append_c(str_buffer, ch); + i += 2; + continue; + } + if (ch >= '0' && ch <= '7') { + char ch2, ch3; + unsigned v; + + ch2 = name[i + 2]; + if (!(ch2 >= '0' && ch2 <= '7')) + return FALSE; + + ch3 = name[i + 3]; + if (!(ch3 >= '0' && ch3 <= '7')) + return FALSE; + +#define OCTAL_VALUE(ch) ((unsigned) ((ch) - '0')) + v = (OCTAL_VALUE(ch) << 6) + (OCTAL_VALUE(ch2) << 3) + OCTAL_VALUE(ch3); + if (v > 0xFF || v == 0) + return FALSE; + ch = (char) v; + if ((ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || (ch == '.') + || (ch >= 'a' && ch <= 'z')) { + /* such characters are not expected to be encoded via + * octal representation. The encoding is invalid. */ + return FALSE; + } + g_string_append_c(str_buffer, ch); + i += 4; + continue; + } + return FALSE; + } + + return FALSE; + } + + return TRUE; +} diff --git a/src/libnm-glib-aux/nm-shared-utils.h b/src/libnm-glib-aux/nm-shared-utils.h index ea38e083c..804034d23 100644 --- a/src/libnm-glib-aux/nm-shared-utils.h +++ b/src/libnm-glib-aux/nm-shared-utils.h @@ -3551,4 +3551,9 @@ void nm_utils_poll(int poll_timeout_ms, gboolean nm_utils_poll_finish(GAsyncResult *result, gpointer *probe_user_data, GError **error); +/*****************************************************************************/ + +void nm_utils_env_var_encode_name(const char *key, GString *str_buffer); +gboolean nm_utils_env_var_decode_name(const char *name, GString *str_buffer); + #endif /* __NM_SHARED_UTILS_H__ */