summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2022-11-17 12:43:55 +0100
committerThomas Haller <thaller@redhat.com>2022-11-17 12:43:55 +0100
commitb8c4936d950351642fb30471169ee6a14c9cc4d5 (patch)
treece14e5f954e1b190143b849a051f87d2b9323420
parent6f330a56bcd47460ac23fe52b391533eeb555c26 (diff)
parenta8d660a0c5d007e79d7c620bebc36ca0aad788f3 (diff)
downloadnetwork-manager-applet-b8c4936d950351642fb30471169ee6a14c9cc4d5.tar.gz
c-e: merge branch 'th/import-wg'
https://gitlab.gnome.org/GNOME/network-manager-applet/-/merge_requests/127
-rw-r--r--src/connection-editor/ce-page.c24
-rw-r--r--src/connection-editor/connection-helpers.c98
-rw-r--r--src/connection-editor/connection-helpers.h5
-rw-r--r--src/connection-editor/nm-connection-list.c24
-rw-r--r--src/connection-editor/page-vpn.c11
-rw-r--r--src/connection-editor/page-wireguard.c21
6 files changed, 130 insertions, 53 deletions
diff --git a/src/connection-editor/ce-page.c b/src/connection-editor/ce-page.c
index 1e523e47..d808912e 100644
--- a/src/connection-editor/ce-page.c
+++ b/src/connection-editor/ce-page.c
@@ -859,8 +859,6 @@ ce_page_complete_connection (NMConnection *connection,
NMClient *client)
{
NMSettingConnection *s_con;
- char *id, *uuid;
- const GPtrArray *connections;
s_con = nm_connection_get_setting_connection (connection);
if (!s_con) {
@@ -869,19 +867,25 @@ ce_page_complete_connection (NMConnection *connection,
}
if (!nm_setting_connection_get_id (s_con)) {
+ const GPtrArray *connections;
+ gs_free char *id = NULL;
+
connections = nm_client_get_connections (client);
id = ce_page_get_next_available_name (connections, format);
g_object_set (s_con, NM_SETTING_CONNECTION_ID, id, NULL);
- g_free (id);
}
- uuid = nm_utils_uuid_generate ();
- g_object_set (s_con,
- NM_SETTING_CONNECTION_UUID, uuid,
- NM_SETTING_CONNECTION_TYPE, ctype,
- NM_SETTING_CONNECTION_AUTOCONNECT, autoconnect,
- NULL);
- g_free (uuid);
+ if (!nm_setting_connection_get_uuid (s_con)) {
+ gs_free char *uuid = NULL;
+
+ uuid = nm_utils_uuid_generate ();
+ g_object_set (s_con, NM_SETTING_CONNECTION_UUID, uuid, NULL);
+ }
+
+ if (ctype)
+ g_object_set (s_con, NM_SETTING_CONNECTION_TYPE, ctype, NULL);
+
+ g_object_set (s_con, NM_SETTING_CONNECTION_AUTOCONNECT, autoconnect, NULL);
}
CEPage *
diff --git a/src/connection-editor/connection-helpers.c b/src/connection-editor/connection-helpers.c
index 0d29f20a..a58a2fbb 100644
--- a/src/connection-editor/connection-helpers.c
+++ b/src/connection-editor/connection-helpers.c
@@ -163,36 +163,80 @@ no_description:
}
NMConnection *
-vpn_connection_from_file (const char *filename, GError **error)
+connection_import_from_file (const char *filename,
+ GType ctype,
+ const char *vpn_detail,
+ GError **error)
{
+ gs_free_error GError *unused_error = NULL;
NMConnection *connection = NULL;
GSList *iter;
- for (iter = vpn_get_plugin_infos (); !connection && iter; iter = iter->next) {
+ if (!error) {
+ /* Some VPN plugins crash when passing no error variable ([1]). Work
+ * around that. In the meantime, libnm does the same workaround ([2]).
+ *
+ *
+ * [1] https://gitlab.gnome.org/GNOME/NetworkManager-vpnc/-/blob/c7d197477c94c5bae0396f0ef826db4d835e487d/properties/nm-vpnc-editor-plugin.c#L281
+ * [2] https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/commit/3b2eb689f3da1e957216b6106382b9a46bae266f
+ */
+ error = &unused_error;
+ }
+
+ if (NM_IN_SET (ctype, G_TYPE_INVALID, NM_TYPE_SETTING_VPN)) {
+ GSList *plugin_infos = vpn_get_plugin_infos ();
+ NMVpnPluginInfo *plugin_info;
NMVpnEditorPlugin *plugin;
- plugin = nm_vpn_plugin_info_get_editor_plugin (iter->data);
- g_clear_error (error);
- connection = nm_vpn_editor_plugin_import (plugin, filename, error);
- if (connection)
- break;
- }
+ if (vpn_detail) {
+ gs_free char *service_type = NULL;
+
+ if ((service_type =
+ nm_vpn_plugin_info_list_find_service_type (plugin_infos, vpn_detail)) &&
+ (plugin_info =
+ nm_vpn_plugin_info_list_find_by_service (plugin_infos, service_type))) {
+ plugin = nm_vpn_plugin_info_get_editor_plugin (plugin_info);
+ connection = nm_vpn_editor_plugin_import (plugin, filename, error);
+ } else {
+ g_set_error (error, NMA_ERROR, NMA_ERROR_GENERIC,
+ _("VPN plugin “%s” not found"), vpn_detail);
+ }
+ } else {
+ for (iter = plugin_infos; iter; iter = iter->next) {
+ plugin_info = iter->data;
+ plugin = nm_vpn_plugin_info_get_editor_plugin (plugin_info);
+ g_clear_error (error);
+ connection = nm_vpn_editor_plugin_import (plugin, filename, error);
+ if (connection)
+ break;
+ }
+ }
- if (connection) {
- NMSettingVpn *s_vpn;
- const char *service_type;
+ if (connection) {
+ NMSettingVpn *s_vpn;
+ const char *service_type;
- s_vpn = nm_connection_get_setting_vpn (connection);
- service_type = s_vpn ? nm_setting_vpn_get_service_type (s_vpn) : NULL;
+ s_vpn = nm_connection_get_setting_vpn (connection);
+ service_type = s_vpn ? nm_setting_vpn_get_service_type (s_vpn) : NULL;
- /* Check connection sanity. */
- if (!service_type || !strlen (service_type)) {
- g_object_unref (connection);
- connection = NULL;
- g_set_error_literal (error, NMA_ERROR, NMA_ERROR_GENERIC, _("No VPN service type."));
+ /* Check connection sanity. */
+ if (!service_type || !strlen (service_type)) {
+ g_object_unref (connection);
+ connection = NULL;
+ g_set_error_literal (error, NMA_ERROR, NMA_ERROR_GENERIC,
+ _("No VPN service type."));
+ }
}
}
+ if (!connection && NM_IN_SET (ctype, G_TYPE_INVALID, NM_TYPE_SETTING_WIREGUARD)) {
+#if NM_CHECK_VERSION(1, 40, 0)
+ G_GNUC_BEGIN_IGNORE_DEPRECATIONS
+ connection = nm_conn_wireguard_import (filename, error && !*error ? error : NULL);
+ G_GNUC_END_IGNORE_DEPRECATIONS
+#endif
+ }
+
if (!connection)
g_prefix_error (error, _("The VPN plugin failed to import the VPN connection correctly: "));
@@ -209,10 +253,10 @@ typedef struct {
static void
import_vpn_from_file_cb (GtkWidget *dialog, gint response, gpointer user_data)
{
- char *filename = NULL;
+ gs_free char *filename = NULL;
ImportVpnInfo *info = (ImportVpnInfo *) user_data;
- NMConnection *connection = NULL;
- GError *error = NULL;
+ gs_unref_object NMConnection *connection = NULL;
+ gs_free_error GError *error = NULL;
gboolean canceled = TRUE;
if (response != GTK_RESPONSE_ACCEPT)
@@ -225,8 +269,14 @@ import_vpn_from_file_cb (GtkWidget *dialog, gint response, gpointer user_data)
}
canceled = FALSE;
- connection = vpn_connection_from_file (filename, &error);
- if (connection) {
+ connection = connection_import_from_file (filename, G_TYPE_INVALID, NULL, &error);
+ if (!connection) {
+ /* pass */
+ } else if (nm_streq (nm_connection_get_connection_type (connection),
+ NM_SETTING_WIREGUARD_SETTING_NAME)) {
+ info->result_func (FUNC_TAG_PAGE_NEW_CONNECTION_RESULT_CALL, connection, FALSE, NULL,
+ info->user_data);
+ } else {
/* Wrap around the actual new function so that the page can complete
* the missing parts, such as UUID or make up the connection name. */
vpn_connection_new (FUNC_TAG_PAGE_NEW_CONNECTION_CALL,
@@ -239,8 +289,6 @@ import_vpn_from_file_cb (GtkWidget *dialog, gint response, gpointer user_data)
info->user_data);
}
- g_free (filename);
-
out:
if (!connection) {
info->result_func (FUNC_TAG_PAGE_NEW_CONNECTION_RESULT_CALL,
diff --git a/src/connection-editor/connection-helpers.h b/src/connection-editor/connection-helpers.h
index bf7d77fb..37a8ad40 100644
--- a/src/connection-editor/connection-helpers.h
+++ b/src/connection-editor/connection-helpers.h
@@ -73,7 +73,10 @@ gboolean connection_supports_proxy (NMConnection *connection);
gboolean connection_supports_ip4 (NMConnection *connection);
gboolean connection_supports_ip6 (NMConnection *connection);
-NMConnection *vpn_connection_from_file (const char *filename, GError **error);
+NMConnection *connection_import_from_file (const char *filename,
+ GType ctype,
+ const char *vpn_detail,
+ GError **error);
#endif /* __CONNECTION_HELPERS_H__ */
diff --git a/src/connection-editor/nm-connection-list.c b/src/connection-editor/nm-connection-list.c
index d897d1d0..1eb0a4c2 100644
--- a/src/connection-editor/nm-connection-list.c
+++ b/src/connection-editor/nm-connection-list.c
@@ -1045,14 +1045,13 @@ nm_connection_list_create (NMConnectionList *list,
if (import_filename) {
if (ctype == G_TYPE_INVALID) {
- /* Atempt a VPN import */
- connection = vpn_connection_from_file (import_filename, NULL);
- if (connection)
- ctype = NM_TYPE_SETTING_VPN;
- else
- g_set_error (&error, NMA_ERROR, NMA_ERROR_GENERIC, _("Unrecognized connection type"));
- } else if (ctype == NM_TYPE_SETTING_VPN) {
- connection = vpn_connection_from_file (import_filename, &error);
+ connection = connection_import_from_file (import_filename, ctype, NULL, NULL);
+ if (!connection) {
+ g_set_error (&error, NMA_ERROR, NMA_ERROR_GENERIC,
+ _ ("Unrecognized connection type"));
+ }
+ } else if (NM_IN_SET (ctype, NM_TYPE_SETTING_VPN, NM_TYPE_SETTING_WIREGUARD)) {
+ connection = connection_import_from_file (import_filename, ctype, detail, &error);
} else {
g_set_error (&error, NMA_ERROR, NMA_ERROR_GENERIC,
_("Don’t know how to import “%s” connections"), g_type_name (ctype));
@@ -1063,6 +1062,15 @@ nm_connection_list_create (NMConnectionList *list,
callback (list, user_data);
return;
}
+
+ if (nm_streq0 (nm_connection_get_connection_type (connection),
+ NM_SETTING_WIREGUARD_SETTING_NAME))
+ ctype = NM_TYPE_SETTING_WIREGUARD;
+ else {
+ nm_assert (NM_IN_STRSET (nm_connection_get_connection_type (connection), NULL,
+ NM_SETTING_VPN_SETTING_NAME));
+ ctype = NM_TYPE_SETTING_VPN;
+ }
}
if (ctype == G_TYPE_INVALID) {
diff --git a/src/connection-editor/page-vpn.c b/src/connection-editor/page-vpn.c
index 0eab8b62..194cd4c7 100644
--- a/src/connection-editor/page-vpn.c
+++ b/src/connection-editor/page-vpn.c
@@ -256,6 +256,7 @@ vpn_connection_new (FUNC_TAG_PAGE_NEW_CONNECTION_IMPL,
}
connection = _ensure_connection_other (connection, &connection_tmp);
+
if (detail) {
service_type = detail;
add_detail_key = vpn_data ? vpn_data->add_detail_key : NULL;
@@ -300,8 +301,14 @@ vpn_connection_new (FUNC_TAG_PAGE_NEW_CONNECTION_IMPL,
if (!service_type)
service_type = detail;
- s_vpn = nm_setting_vpn_new ();
- g_object_set (s_vpn, NM_SETTING_VPN_SERVICE_TYPE, service_type, NULL);
+ s_vpn = NM_SETTING (nm_connection_get_setting_vpn (connection));
+ if (!s_vpn)
+ s_vpn = nm_setting_vpn_new ();
+ else
+ g_object_ref (s_vpn);
+
+ if (!nm_setting_vpn_get_service_type (NM_SETTING_VPN(s_vpn)))
+ g_object_set (s_vpn, NM_SETTING_VPN_SERVICE_TYPE, service_type, NULL);
if (add_detail_key)
nm_setting_vpn_add_data_item ((NMSettingVpn *) s_vpn, add_detail_key, add_detail_val);
diff --git a/src/connection-editor/page-wireguard.c b/src/connection-editor/page-wireguard.c
index 745faa45..9ba4dc9f 100644
--- a/src/connection-editor/page-wireguard.c
+++ b/src/connection-editor/page-wireguard.c
@@ -629,15 +629,22 @@ wireguard_connection_new (FUNC_TAG_PAGE_NEW_CONNECTION_IMPL,
NM_SETTING_WIREGUARD_SETTING_NAME,
FALSE,
client);
- nm_connection_add_setting (connection, nm_setting_wireguard_new ());
- s_ip = (NMSettingIPConfig *) nm_setting_ip4_config_new ();
- g_object_set (s_ip, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_DISABLED, NULL);
- nm_connection_add_setting (connection, (NMSetting *) s_ip);
+ if (!nm_connection_get_setting (connection, NM_TYPE_SETTING_WIREGUARD))
+ nm_connection_add_setting (connection, nm_setting_wireguard_new ());
- s_ip = (NMSettingIPConfig *) nm_setting_ip6_config_new ();
- g_object_set (s_ip, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, NULL);
- nm_connection_add_setting (connection, (NMSetting *) s_ip);
+ if (!nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG)) {
+ s_ip = (NMSettingIPConfig *)nm_setting_ip4_config_new ();
+ g_object_set (s_ip, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_DISABLED,
+ NULL);
+ nm_connection_add_setting (connection, (NMSetting *)s_ip);
+ }
+
+ if (!nm_connection_get_setting (connection, NM_TYPE_SETTING_IP6_CONFIG)) {
+ s_ip = (NMSettingIPConfig *)nm_setting_ip6_config_new ();
+ g_object_set (s_ip, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, NULL);
+ nm_connection_add_setting (connection, (NMSetting *)s_ip);
+ }
(*result_func) (FUNC_TAG_PAGE_NEW_CONNECTION_RESULT_CALL, connection, FALSE, NULL, user_data);
}