From 3ff5b6cc984162c27949139306bd5acb9a6ea0e0 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 16 Nov 2022 12:52:24 +0100 Subject: c-e: support importing WireGuard profiles from wg-quick files Use new 1.40 API from libnm to import WireGuard profiles. The following now works as expected: nm-connection-editor -i $WG_CONF_FILE nm-connection-editor -i $WG_CONF_FILE -t wireguard This was already working before: nm-connection-editor -c -t wireguard What also works is to click "Import a saved VPN connection..." and select a wg-quick configuration file. --- src/connection-editor/connection-helpers.c | 59 +++++++++++++++++++----------- src/connection-editor/connection-helpers.h | 2 +- src/connection-editor/nm-connection-list.c | 24 ++++++++---- src/connection-editor/page-wireguard.c | 21 +++++++---- 4 files changed, 69 insertions(+), 37 deletions(-) diff --git a/src/connection-editor/connection-helpers.c b/src/connection-editor/connection-helpers.c index 0c365808..7ef75c67 100644 --- a/src/connection-editor/connection-helpers.c +++ b/src/connection-editor/connection-helpers.c @@ -163,7 +163,7 @@ no_description: } NMConnection * -connection_import_from_file (const char *filename, GError **error) +connection_import_from_file (const char *filename, GType ctype, GError **error) { gs_free_error GError *unused_error = NULL; NMConnection *connection = NULL; @@ -180,31 +180,42 @@ connection_import_from_file (const char *filename, GError **error) error = &unused_error; } - for (iter = vpn_get_plugin_infos (); !connection && iter; iter = iter->next) { - NMVpnEditorPlugin *plugin; + if (NM_IN_SET (ctype, G_TYPE_INVALID, NM_TYPE_SETTING_VPN)) { + for (iter = vpn_get_plugin_infos (); !connection && iter; iter = iter->next) { + 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; - } + 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 (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: ")); @@ -237,8 +248,14 @@ import_vpn_from_file_cb (GtkWidget *dialog, gint response, gpointer user_data) } canceled = FALSE; - connection = connection_import_from_file (filename, &error); - if (connection) { + connection = connection_import_from_file (filename, G_TYPE_INVALID, &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, diff --git a/src/connection-editor/connection-helpers.h b/src/connection-editor/connection-helpers.h index 7663c827..af9985ff 100644 --- a/src/connection-editor/connection-helpers.h +++ b/src/connection-editor/connection-helpers.h @@ -73,7 +73,7 @@ gboolean connection_supports_proxy (NMConnection *connection); gboolean connection_supports_ip4 (NMConnection *connection); gboolean connection_supports_ip6 (NMConnection *connection); -NMConnection *connection_import_from_file (const char *filename, GError **error); +NMConnection *connection_import_from_file (const char *filename, GType ctype, 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 49583f65..d0271e36 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 = connection_import_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 = connection_import_from_file (import_filename, &error); + connection = connection_import_from_file (import_filename, ctype, 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, &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-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); } -- cgit v1.2.1