core: move functions for env variable name encoding to libnm-glib-aux

They will be used by the dispatcher service.
This commit is contained in:
Beniamino Galvani 2023-11-08 16:35:39 +01:00
parent 7b769e9e49
commit 38acb7a57d
5 changed files with 118 additions and 111 deletions

View file

@ -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)

View file

@ -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[] = {

View file

@ -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));
}
}

View file

@ -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;
}

View file

@ -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__ */