diff options
author | Thomas Haller <thaller@redhat.com> | 2022-08-26 10:25:10 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2022-08-31 19:20:11 +0200 |
commit | 14828a09327eb089c4ed2fa7d99f591a24737069 (patch) | |
tree | 05c5a14ef65b82934d5094bcb7e08c019c8a6c3e | |
parent | 71a111bb9cf8f42639a61ba345491fba828cfe9d (diff) | |
download | NetworkManager-14828a09327eb089c4ed2fa7d99f591a24737069.tar.gz |
nmcli: support changing the connection type in offline mode
-rw-r--r-- | src/libnmc-setting/nm-meta-setting-desc.c | 44 |
1 files changed, 30 insertions, 14 deletions
diff --git a/src/libnmc-setting/nm-meta-setting-desc.c b/src/libnmc-setting/nm-meta-setting-desc.c index 4908342f66..90f9c2f1d6 100644 --- a/src/libnmc-setting/nm-meta-setting-desc.c +++ b/src/libnmc-setting/nm-meta-setting-desc.c @@ -15,6 +15,7 @@ #include "libnm-core-aux-intern/nm-common-macros.h" #include "libnm-glib-aux/nm-enum-utils.h" #include "libnm-glib-aux/nm-secret-utils.h" +#include "libnm-glib-aux/nm-uuid.h" #include "libnm-core-aux-intern/nm-libnm-core-utils.h" #include "libnm-core-aux-extern/nm-libnm-core-aux.h" @@ -2635,18 +2636,28 @@ _get_fcn_connection_permissions(ARGS_GET_FCN) static gboolean _set_fcn_connection_type(ARGS_SET_FCN) { - gs_free char *uuid = NULL; - - if (nm_setting_connection_get_uuid(NM_SETTING_CONNECTION(setting))) { - /* Don't allow setting type unless the connection is brand new. - * Just because it's a bad idea and the user wouldn't probably want that. - * No technical reason, really. - * Also, using uuid to see if the connection is brand new is a bit - * hacky: we can not see if the type is already set, because - * nmc_setting_set_property() is called only after the property - * we're setting (type) has been removed. */ - nm_utils_error_set(error, NM_UTILS_ERROR_UNKNOWN, _("Can not change the connection type")); - return FALSE; + const char *connection_type; + + connection_type = nm_setting_connection_get_connection_type(NM_SETTING_CONNECTION(setting)); + + if (connection_type) { + if (nm_streq0(connection_type, value)) { + /* No change. Setting the same again is fine. */ + return TRUE; + } + + /* There is no technical reason that a profile couldn't change its type. + * NetworkManager allows that just fine. + * + * However, it seems an unusual thing to do, so only allow that in + * offline mode. */ + if (!NM_FLAGS_HAS(nm_meta_environment_get_env_flags(environment, environment_user_data), + NM_META_ENV_FLAGS_OFFLINE)) { + nm_utils_error_set(error, + NM_UTILS_ERROR_UNKNOWN, + _("Can not change the connection type")); + return FALSE; + } } if (_SET_FCN_DO_RESET_DEFAULT(property_info, modifier, value)) { @@ -2654,8 +2665,13 @@ _set_fcn_connection_type(ARGS_SET_FCN) return TRUE; } - uuid = nm_utils_uuid_generate(); - g_object_set(G_OBJECT(setting), NM_SETTING_CONNECTION_UUID, uuid, NULL); + if (!nm_setting_connection_get_uuid(NM_SETTING_CONNECTION(setting))) { + /* If we so far have not UUID set, set it together with the connection type. */ + g_object_set(G_OBJECT(setting), + NM_SETTING_CONNECTION_UUID, + nm_uuid_generate_random_str_a(), + NULL); + } g_object_set(G_OBJECT(setting), property_info->property_name, value, NULL); return TRUE; |