Commit a6ee43d1 authored by Thomas Haller's avatar Thomas Haller

cli/wireguard: add import functionality for WireGuard

Support importing ".conf" files as `wg-quick up` supports it.

`wg-quick` parses several options under "[Interface]" and
passes the remainder to `wg setconf`.

The PreUp/PreDown/PostUp/PostDown options are of course not supported.

"Table" for the moment behaves different.

(cherry picked from commit a3a8583c)
parent 0d178a96
......@@ -8902,8 +8902,13 @@ do_connection_import (NmCli *nmc, int argc, char **argv)
NMC_RETURN (nmc, NMC_RESULT_ERROR_USER_INPUT);
}
if (argc == 1 && nmc->complete)
complete_option ((const NMMetaAbstractInfo *) nm_meta_property_info_vpn_service_type, *argv, NULL);
if ( argc == 1
&& nmc->complete) {
nmc_complete_strings (*argv, "wireguard");
complete_option ((const NMMetaAbstractInfo *) nm_meta_property_info_vpn_service_type,
*argv,
NULL);
}
if (!type)
type = *argv;
......@@ -8943,21 +8948,26 @@ do_connection_import (NmCli *nmc, int argc, char **argv)
NMC_RETURN (nmc, NMC_RESULT_ERROR_USER_INPUT);
}
service_type = nm_vpn_plugin_info_list_find_service_type (nm_vpn_get_plugin_infos (), type);
if (!service_type) {
g_string_printf (nmc->return_text, _("Error: failed to find VPN plugin for %s."), type);
NMC_RETURN (nmc, NMC_RESULT_ERROR_UNKNOWN);
}
if (nm_streq (type, "wireguard"))
connection = nm_vpn_wireguard_import (filename, &error);
else {
service_type = nm_vpn_plugin_info_list_find_service_type (nm_vpn_get_plugin_infos (), type);
if (!service_type) {
g_string_printf (nmc->return_text, _("Error: failed to find VPN plugin for %s."), type);
NMC_RETURN (nmc, NMC_RESULT_ERROR_UNKNOWN);
}
/* Import VPN configuration */
plugin = nm_vpn_get_editor_plugin (service_type, &error);
if (!plugin) {
g_string_printf (nmc->return_text, _("Error: failed to load VPN plugin: %s."),
error->message);
NMC_RETURN (nmc, NMC_RESULT_ERROR_UNKNOWN);
/* Import VPN configuration */
plugin = nm_vpn_get_editor_plugin (service_type, &error);
if (!plugin) {
g_string_printf (nmc->return_text, _("Error: failed to load VPN plugin: %s."),
error->message);
NMC_RETURN (nmc, NMC_RESULT_ERROR_UNKNOWN);
}
connection = nm_vpn_editor_plugin_import (plugin, filename, &error);
}
connection = nm_vpn_editor_plugin_import (plugin, filename, &error);
if (!connection) {
g_string_printf (nmc->return_text, _("Error: failed to import '%s': %s."),
filename, error->message);
......
......@@ -24,6 +24,13 @@
#include "nm-active-connection.h"
#include "nm-device.h"
#define nm_auto_unref_ip_address nm_auto (_nm_ip_address_unref)
NM_AUTO_DEFINE_FCN0 (NMIPAddress *, _nm_ip_address_unref, nm_ip_address_unref)
#define nm_auto_unref_wgpeer nm_auto (_nm_auto_unref_wgpeer)
NM_AUTO_DEFINE_FCN0 (NMWireGuardPeer *, _nm_auto_unref_wgpeer, nm_wireguard_peer_unref)
const NMObject **nmc_objects_sort_by_path (const NMObject *const*objs, gssize len);
const char *nmc_string_is_valid (const char *input, const char **allowed, GError **error);
......
This diff is collapsed.
......@@ -39,4 +39,7 @@ gboolean nm_vpn_openconnect_authenticate_helper (const char *host,
int *status,
GError **error);
NMConnection *nm_vpn_wireguard_import (const char *filename,
GError **error);
#endif /* __NM_VPN_HELPERS_H__ */
......@@ -268,8 +268,8 @@ nm_utils_fd_get_contents (int fd,
* @flags: %NMUtilsFileGetContentsFlags for reading the file.
* @contents: the output buffer with the file read. It is always
* NUL terminated. The buffer is at most @max_length long, including
* the NUL byte. That is, it reads only files up to a length of
* @max_length - 1 bytes.
* the NUL byte. That is, it reads only files up to a length of
* @max_length - 1 bytes.
* @length: optional output argument of the read file size.
*
* A reimplementation of g_file_get_contents() with a few differences:
......
......@@ -672,6 +672,8 @@ nm_utils_parse_inaddr_prefix_bin (int addr_family,
return FALSE;
if (slash) {
/* For IPv4, `ip addr add` supports the prefix-length as a netmask. We don't
* do that. */
prefix = _nm_utils_ascii_str_to_int64 (slash + 1, 10,
0,
addr_family == AF_INET ? 32 : 128,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment