network-manager-wireguard/properties/nm-wireguard-editor.c

729 lines
22 KiB
C
Raw Normal View History

2017-11-06 15:02:05 +03:00
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/***************************************************************************
* CVSID: $Id: nm-openvpn.c 4232 2008-10-29 09:13:40Z tambeti $
*
* nm-openvpn.c : GNOME UI dialogs for configuring openvpn VPN connections
*
* Copyright (C) 2005 Tim Niemueller <tim@niemueller.de>
* Copyright (C) 2008 - 2010 Dan Williams, <dcbw@redhat.com>
* Copyright (C) 2008 - 2011 Red Hat, Inc.
* Based on work by David Zeuthen, <davidz@redhat.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
**************************************************************************/
#include "nm-default.h"
2018-02-12 18:06:08 +03:00
#include "nm-wireguard-editor.h"
2017-11-06 15:02:05 +03:00
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include "utils.h"
/*****************************************************************************/
static void wireguard_editor_plugin_widget_interface_init (NMVpnEditorInterface *iface_class);
2017-11-06 15:02:05 +03:00
G_DEFINE_TYPE_EXTENDED (WireguardEditor, wireguard_editor_plugin_widget, G_TYPE_OBJECT, 0,
2017-11-06 15:02:05 +03:00
G_IMPLEMENT_INTERFACE (NM_TYPE_VPN_EDITOR,
wireguard_editor_plugin_widget_interface_init))
2017-11-06 15:02:05 +03:00
#define WIREGUARD_EDITOR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), WIREGUARD_TYPE_EDITOR, WireguardEditorPrivate))
2017-11-06 15:02:05 +03:00
typedef struct {
GtkBuilder *builder;
GtkWidget *widget;
gboolean new_connection;
} WireguardEditorPrivate;
2017-11-06 15:02:05 +03:00
/*****************************************************************************/
// functions for checking the contents of the input fields in the GUI
static gboolean
check_interface_ip4_entry(const char *str)
{
return is_ip4((char *)str);
}
static gboolean
check_interface_ip6_entry(const char *str)
{
return is_ip6((char *)str);
}
static gboolean
check_interface_dns_entry(const char *str)
{
if(is_empty(str)){
return TRUE;
}
else if(is_ip4((char *)str)){
return TRUE;
}
else if(is_ip6((char *)str)){
return TRUE;
2018-02-07 23:05:54 +03:00
}
return FALSE;
}
static gboolean
check_interface_mtu_entry(const char *str)
{
if(is_empty(str)){
return TRUE;
}
else if(!g_ascii_string_to_unsigned(str, 10, 0, 1500, NULL, NULL)){
2018-02-07 23:05:54 +03:00
return FALSE;
}
return TRUE;
}
static gboolean
check_peer_preshared_key(const char *str)
2018-02-07 23:05:54 +03:00
{
if(is_empty(str)){
return TRUE;
}
// WireGuard has Base64-encoded PSKs of length 44
if(strlen(str) != 44){
2018-02-07 23:05:54 +03:00
return FALSE;
}
return is_base64((char *)str);
}
static gboolean
check_interface_private_key(const char *str)
{
return check_peer_preshared_key(str);
}
static gboolean
check_peer_public_key(const char *str){
return check_peer_preshared_key(str);
2018-02-07 23:05:54 +03:00
}
static gboolean
check_interface_listen_port(const char *str)
{
if(!g_ascii_string_to_unsigned(str, 10, 0, 65535, NULL, NULL)){
2018-02-07 23:05:54 +03:00
return FALSE;
}
return TRUE;
}
static gboolean
check_peer_allowed_ips(const char *str)
{
gchar **ips;
int idx = 0;
gboolean success = TRUE;
2018-02-07 23:05:54 +03:00
ips = g_strsplit_set(str, ", \t", 0);
2018-02-07 23:05:54 +03:00
while(ips && ips[idx]){
2017-11-06 15:02:05 +03:00
if(is_empty(ips[idx])){
idx++;
continue;
}
2017-11-06 15:02:05 +03:00
// there should not be any trailing commas, etc. anymore
// -> if any of the items is not a valid IPv4 or IPv6 address: error!
if(!is_ip4(ips[idx]) && !is_ip6(ips[idx])){
success = FALSE;
break;
}
idx++;
2017-11-06 15:02:05 +03:00
}
g_strfreev(ips);
2017-11-06 15:02:05 +03:00
return success;
}
static gboolean
check_peer_endpoint(const char *str)
{
return is_ip4((char *)str) || is_ip6((char *)str) || is_fqdn((char *)str);
}
// used in 'check()', matches the functions above
2018-02-07 23:05:54 +03:00
typedef gboolean (*CheckFunc)(const char *str);
// helper function to reduce boilerplate code in 'check_validity()'
static gboolean
check (WireguardEditorPrivate *priv,
char *widget_name,
2018-04-05 22:22:29 +03:00
CheckFunc chk,
const char *key,
gboolean set_error,
GError **error)
2018-02-07 23:05:54 +03:00
{
const char *str;
GtkWidget *widget;
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, widget_name));
str = gtk_entry_get_text (GTK_ENTRY (widget));
if (str && chk(str))
gtk_style_context_remove_class (gtk_widget_get_style_context (widget), "error");
else {
gtk_style_context_add_class (gtk_widget_get_style_context (widget), "error");
// only set the error if it's NULL
if(error == NULL && set_error){
2018-02-07 23:05:54 +03:00
g_set_error (error,
NMV_EDITOR_PLUGIN_ERROR,
NMV_EDITOR_PLUGIN_ERROR_INVALID_PROPERTY,
2018-04-05 22:22:29 +03:00
"%s",
key);
2018-02-07 23:05:54 +03:00
}
return FALSE;
}
return TRUE;
}
// add or remove the "error" class from the specified input field
static void
set_error_class(WireguardEditorPrivate *priv, char *widget_name, gboolean error)
{
GtkWidget *widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, widget_name));
if(error){
gtk_style_context_add_class (gtk_widget_get_style_context (widget), "error");
}
else{
gtk_style_context_remove_class (gtk_widget_get_style_context (widget), "error");
}
}
// check if the specified input field contains any user input
static gboolean
is_filled_out(WireguardEditorPrivate *priv, char *widget_name)
{
const char *str;
GtkWidget *widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, widget_name));
str = gtk_entry_get_text(GTK_ENTRY (widget));
return !is_empty(str);
}
// check validity of the input fields in the GUI
// if there is an error in one or more of the input fields, mark the corresponding
// input fields with the "error" class
2017-11-06 15:02:05 +03:00
static gboolean
check_validity (WireguardEditor *self, GError **error)
2017-11-06 15:02:05 +03:00
{
WireguardEditorPrivate *priv = WIREGUARD_EDITOR_GET_PRIVATE (self);
gboolean success = TRUE;
gboolean ip4_ok = TRUE;
gboolean ip6_ok = TRUE;
2017-11-06 15:02:05 +03:00
2018-02-07 23:05:54 +03:00
// check the various input fields for errors
if(!check(priv, "interface_ip4_entry", check_interface_ip4_entry, NM_WG_KEY_ADDR_IP4, FALSE, error)){
ip4_ok = FALSE;
}
if(!check(priv, "interface_ip6_entry", check_interface_ip6_entry, NM_WG_KEY_ADDR_IP6, FALSE, error)){
ip6_ok = FALSE;
}
if(!check(priv, "interface_private_key_entry", check_interface_private_key, NM_WG_KEY_PRIVATE_KEY, TRUE, error)){
2018-02-07 23:05:54 +03:00
success = FALSE;
}
if(!check(priv, "interface_port_entry", check_interface_listen_port, NM_WG_KEY_LISTEN_PORT, TRUE, error)){
2018-02-07 23:05:54 +03:00
success = FALSE;
}
if(!check(priv, "interface_dns_entry", check_interface_dns_entry, NM_WG_KEY_DNS, TRUE, error)){
2018-02-07 23:05:54 +03:00
success = FALSE;
}
if(!check(priv, "interface_mtu_entry", check_interface_mtu_entry, NM_WG_KEY_MTU, TRUE, error)){
2018-02-07 23:05:54 +03:00
success = FALSE;
}
if(!check(priv, "peer_public_key_entry", check_peer_public_key, NM_WG_KEY_PUBLIC_KEY, TRUE, error)){
2018-02-07 23:05:54 +03:00
success = FALSE;
}
if(!check(priv, "peer_allowed_ips_entry", check_peer_allowed_ips, NM_WG_KEY_ALLOWED_IPS, TRUE, error)){
2018-02-07 23:05:54 +03:00
success = FALSE;
2017-11-06 15:02:05 +03:00
}
if(!check(priv, "peer_endpoint_entry", check_peer_endpoint, NM_WG_KEY_ENDPOINT, TRUE, error)){
success = FALSE;
}
if(!check(priv, "peer_psk_entry", check_peer_preshared_key, NM_WG_KEY_PRESHARED_KEY, TRUE, error)){
success = FALSE;
}
// pre-up, post-up, pre-down, post-down are scripts and don't get validated
if(ip4_ok && ip6_ok){
// IP4 and IP6 are both set: OK
set_error_class(priv, "interface_ip4_entry", FALSE);
set_error_class(priv, "interface_ip6_entry", FALSE);
}
else if(ip4_ok){
if(is_filled_out(priv, "interface_ip6_entry")){
// IP6 is filled out but not ok: NOK
success = FALSE;
g_set_error (error,
NMV_EDITOR_PLUGIN_ERROR,
NMV_EDITOR_PLUGIN_ERROR_INVALID_PROPERTY,
NM_WG_KEY_ADDR_IP6);
}
else{
// IP6 is not filled out: OK
set_error_class(priv, "interface_ip4_entry", FALSE);
set_error_class(priv, "interface_ip6_entry", FALSE);
}
}
else if(ip6_ok){
if(is_filled_out(priv, "interface_ip4_entry")){
// IP4 is filled out but not ok: NOK
success = FALSE;
g_set_error (error,
NMV_EDITOR_PLUGIN_ERROR,
NMV_EDITOR_PLUGIN_ERROR_INVALID_PROPERTY,
NM_WG_KEY_ADDR_IP4);
}
else{
// IP4 is not filled out: OK
set_error_class(priv, "interface_ip4_entry", FALSE);
set_error_class(priv, "interface_ip6_entry", FALSE);
}
}
2017-11-06 15:02:05 +03:00
2018-02-07 23:05:54 +03:00
return success;
2017-11-06 15:02:05 +03:00
}
// callback when input has changed
2017-11-06 15:02:05 +03:00
static void
stuff_changed_cb (GtkWidget *widget, gpointer user_data)
{
g_signal_emit_by_name (WIREGUARD_EDITOR (user_data), "changed");
2017-11-06 15:02:05 +03:00
}
// set up the GUI: fill the contents of the input fields with the stuff contained
// in our NMConnection
2017-11-06 15:02:05 +03:00
static gboolean
init_editor_plugin (WireguardEditor *self, NMConnection *connection, GError **error)
2017-11-06 15:02:05 +03:00
{
WireguardEditorPrivate *priv = WIREGUARD_EDITOR_GET_PRIVATE (self);
2017-11-06 15:02:05 +03:00
NMSettingVpn *s_vpn;
GtkWidget *widget;
const char *value;
s_vpn = nm_connection_get_setting_vpn (connection);
2018-02-07 23:05:54 +03:00
// Local IPv4 address
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "interface_ip4_entry"));
g_return_val_if_fail (widget != NULL, FALSE);
if (s_vpn) {
value = nm_setting_vpn_get_data_item (s_vpn, NM_WG_KEY_ADDR_IP4);
if (value)
gtk_entry_set_text (GTK_ENTRY (widget), value);
}
g_signal_connect (G_OBJECT (widget), "changed", G_CALLBACK (stuff_changed_cb), self);
// Local IPv6 address
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "interface_ip6_entry"));
g_return_val_if_fail (widget != NULL, FALSE);
if (s_vpn) {
value = nm_setting_vpn_get_data_item (s_vpn, NM_WG_KEY_ADDR_IP6);
if (value)
gtk_entry_set_text (GTK_ENTRY (widget), value);
}
g_signal_connect (G_OBJECT (widget), "changed", G_CALLBACK (stuff_changed_cb), self);
// DNS
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "interface_dns_entry"));
g_return_val_if_fail (widget != NULL, FALSE);
if (s_vpn) {
value = nm_setting_vpn_get_data_item (s_vpn, NM_WG_KEY_DNS);
if (value)
gtk_entry_set_text (GTK_ENTRY (widget), value);
}
g_signal_connect (G_OBJECT (widget), "changed", G_CALLBACK (stuff_changed_cb), self);
// Interface Private Key
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "interface_mtu_entry"));
g_return_val_if_fail (widget != NULL, FALSE);
if (s_vpn) {
value = nm_setting_vpn_get_data_item (s_vpn, NM_WG_KEY_MTU);
if (value)
gtk_entry_set_text (GTK_ENTRY (widget), value);
}
g_signal_connect (G_OBJECT (widget), "changed", G_CALLBACK (stuff_changed_cb), self);
2018-02-07 23:05:54 +03:00
// Interface Private Key
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "interface_private_key_entry"));
g_return_val_if_fail (widget != NULL, FALSE);
if (s_vpn) {
value = nm_setting_vpn_get_data_item (s_vpn, NM_WG_KEY_PRIVATE_KEY);
2018-02-07 23:05:54 +03:00
if (value)
gtk_entry_set_text (GTK_ENTRY (widget), value);
}
g_signal_connect (G_OBJECT (widget), "changed", G_CALLBACK (stuff_changed_cb), self);
// Interface Listening Port
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "interface_port_entry"));
g_return_val_if_fail (widget != NULL, FALSE);
if (s_vpn) {
value = nm_setting_vpn_get_data_item (s_vpn, NM_WG_KEY_LISTEN_PORT);
if (value)
gtk_entry_set_text (GTK_ENTRY (widget), value);
}
g_signal_connect (G_OBJECT (widget), "changed", G_CALLBACK (stuff_changed_cb), self);
// Interface Pre Up
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "interface_pre_up_entry"));
g_return_val_if_fail (widget != NULL, FALSE);
if (s_vpn) {
value = nm_setting_vpn_get_data_item (s_vpn, NM_WG_KEY_PRE_UP);
if (value)
gtk_entry_set_text (GTK_ENTRY (widget), value);
}
g_signal_connect (G_OBJECT (widget), "changed", G_CALLBACK (stuff_changed_cb), self);
// Interface Post Up
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "interface_post_up_entry"));
g_return_val_if_fail (widget != NULL, FALSE);
if (s_vpn) {
value = nm_setting_vpn_get_data_item (s_vpn, NM_WG_KEY_POST_UP);
if (value)
gtk_entry_set_text (GTK_ENTRY (widget), value);
}
g_signal_connect (G_OBJECT (widget), "changed", G_CALLBACK (stuff_changed_cb), self);
// Interface Pre Down
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "interface_pre_down_entry"));
g_return_val_if_fail (widget != NULL, FALSE);
if (s_vpn) {
value = nm_setting_vpn_get_data_item (s_vpn, NM_WG_KEY_PRE_DOWN);
if (value)
gtk_entry_set_text (GTK_ENTRY (widget), value);
}
g_signal_connect (G_OBJECT (widget), "changed", G_CALLBACK (stuff_changed_cb), self);
// Interface Post Down
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "interface_post_down_entry"));
g_return_val_if_fail (widget != NULL, FALSE);
if (s_vpn) {
value = nm_setting_vpn_get_data_item (s_vpn, NM_WG_KEY_POST_DOWN);
if (value)
gtk_entry_set_text (GTK_ENTRY (widget), value);
}
g_signal_connect (G_OBJECT (widget), "changed", G_CALLBACK (stuff_changed_cb), self);
// Interface Preshared Key
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "peer_psk_entry"));
g_return_val_if_fail (widget != NULL, FALSE);
if (s_vpn) {
value = nm_setting_vpn_get_data_item (s_vpn, NM_WG_KEY_PRESHARED_KEY);
2018-02-07 23:05:54 +03:00
if (value)
gtk_entry_set_text (GTK_ENTRY (widget), value);
}
g_signal_connect (G_OBJECT (widget), "changed", G_CALLBACK (stuff_changed_cb), self);
// Peer Public Key
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "peer_public_key_entry"));
g_return_val_if_fail (widget != NULL, FALSE);
if (s_vpn) {
value = nm_setting_vpn_get_data_item (s_vpn, NM_WG_KEY_PUBLIC_KEY);
2018-02-07 23:05:54 +03:00
if (value)
gtk_entry_set_text (GTK_ENTRY (widget), value);
}
g_signal_connect (G_OBJECT (widget), "changed", G_CALLBACK (stuff_changed_cb), self);
// Peer Allowed IPs
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "peer_allowed_ips_entry"));
g_return_val_if_fail (widget != NULL, FALSE);
if (s_vpn) {
value = nm_setting_vpn_get_data_item (s_vpn, NM_WG_KEY_ALLOWED_IPS);
2018-02-07 23:05:54 +03:00
if (value)
gtk_entry_set_text (GTK_ENTRY (widget), value);
}
g_signal_connect (G_OBJECT (widget), "changed", G_CALLBACK (stuff_changed_cb), self);
// Peer Endpoint
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "peer_endpoint_entry"));
2017-11-06 15:02:05 +03:00
g_return_val_if_fail (widget != NULL, FALSE);
if (s_vpn) {
value = nm_setting_vpn_get_data_item (s_vpn, NM_WG_KEY_ENDPOINT);
2017-11-06 15:02:05 +03:00
if (value)
gtk_entry_set_text (GTK_ENTRY (widget), value);
}
g_signal_connect (G_OBJECT (widget), "changed", G_CALLBACK (stuff_changed_cb), self);
return TRUE;
}
// get the active widget (config GUI)
2017-11-06 15:02:05 +03:00
static GObject *
get_widget (NMVpnEditor *iface)
{
WireguardEditor *self = WIREGUARD_EDITOR (iface);
WireguardEditorPrivate *priv = WIREGUARD_EDITOR_GET_PRIVATE (self);
2017-11-06 15:02:05 +03:00
return G_OBJECT (priv->widget);
}
// check if the user's inputs are valid and if so, update the NMConnection's
// NMSettingVpn data items (gets called everytime something changes, afaik)
2017-11-06 15:02:05 +03:00
static gboolean
update_connection (NMVpnEditor *iface,
NMConnection *connection,
GError **error)
{
WireguardEditor *self = WIREGUARD_EDITOR (iface);
WireguardEditorPrivate *priv = WIREGUARD_EDITOR_GET_PRIVATE (self);
2017-11-06 15:02:05 +03:00
NMSettingVpn *s_vpn;
GtkWidget *widget;
const char *str;
gboolean valid = FALSE;
// validity check is done before anything else
if (!check_validity (self, error)){
2017-11-06 15:02:05 +03:00
return FALSE;
}
2017-11-06 15:02:05 +03:00
s_vpn = NM_SETTING_VPN (nm_setting_vpn_new ());
g_object_set (s_vpn, NM_SETTING_VPN_SERVICE_TYPE, NM_VPN_SERVICE_TYPE_WIREGUARD, NULL);
2017-11-06 15:02:05 +03:00
2018-02-07 23:05:54 +03:00
// local ip4
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "interface_ip4_entry"));
str = gtk_entry_get_text (GTK_ENTRY (widget));
if (str && str[0]){
2018-02-07 23:05:54 +03:00
nm_setting_vpn_add_data_item (s_vpn, NM_WG_KEY_ADDR_IP4, str);
}
2018-02-07 23:05:54 +03:00
// local ip6
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "interface_ip6_entry"));
str = gtk_entry_get_text (GTK_ENTRY (widget));
if (str && str[0]){
nm_setting_vpn_add_data_item (s_vpn, NM_WG_KEY_ADDR_IP6, str);
}
2018-02-07 23:05:54 +03:00
// private key
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "interface_private_key_entry"));
str = gtk_entry_get_text (GTK_ENTRY (widget));
if (str && str[0]){
2018-02-07 23:05:54 +03:00
nm_setting_vpn_add_data_item (s_vpn, NM_WG_KEY_PRIVATE_KEY, str);
}
2018-02-07 23:05:54 +03:00
// dns
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "interface_dns_entry"));
str = gtk_entry_get_text (GTK_ENTRY (widget));
if (str && str[0]){
nm_setting_vpn_add_data_item (s_vpn, NM_WG_KEY_DNS, str);
}
// mtu
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "interface_mtu_entry"));
str = gtk_entry_get_text (GTK_ENTRY (widget));
if (str && str[0]){
nm_setting_vpn_add_data_item (s_vpn, NM_WG_KEY_MTU, str);
}
2018-02-07 23:05:54 +03:00
// listen port
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "interface_port_entry"));
str = gtk_entry_get_text (GTK_ENTRY (widget));
if (str && str[0]){
2018-02-07 23:05:54 +03:00
nm_setting_vpn_add_data_item (s_vpn, NM_WG_KEY_LISTEN_PORT, str);
}
2018-02-07 23:05:54 +03:00
// pre up script
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "interface_pre_up_entry"));
str = gtk_entry_get_text (GTK_ENTRY (widget));
if (str && str[0]){
nm_setting_vpn_add_data_item (s_vpn, NM_WG_KEY_PRE_UP, str);
}
// post up script
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "interface_post_up_entry"));
str = gtk_entry_get_text (GTK_ENTRY (widget));
if (str && str[0]){
nm_setting_vpn_add_data_item (s_vpn, NM_WG_KEY_POST_UP, str);
}
// pre up script
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "interface_pre_down_entry"));
str = gtk_entry_get_text (GTK_ENTRY (widget));
if (str && str[0]){
nm_setting_vpn_add_data_item (s_vpn, NM_WG_KEY_PRE_DOWN, str);
}
// post down script
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "interface_post_down_entry"));
str = gtk_entry_get_text (GTK_ENTRY (widget));
if (str && str[0]){
nm_setting_vpn_add_data_item (s_vpn, NM_WG_KEY_POST_DOWN, str);
}
// preshared key
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "peer_psk_entry"));
str = gtk_entry_get_text (GTK_ENTRY (widget));
if (str && str[0]){
nm_setting_vpn_add_data_item (s_vpn, NM_WG_KEY_PRESHARED_KEY, str);
}
2018-02-07 23:05:54 +03:00
// peer public key
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "peer_public_key_entry"));
str = gtk_entry_get_text (GTK_ENTRY (widget));
if (str && str[0]){
2018-02-07 23:05:54 +03:00
nm_setting_vpn_add_data_item (s_vpn, NM_WG_KEY_PUBLIC_KEY, str);
}
2018-02-07 23:05:54 +03:00
// allowed IPs
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "peer_allowed_ips_entry"));
str = gtk_entry_get_text (GTK_ENTRY (widget));
if (str && str[0]){
2018-02-07 23:05:54 +03:00
nm_setting_vpn_add_data_item (s_vpn, NM_WG_KEY_ALLOWED_IPS, str);
}
2018-02-07 23:05:54 +03:00
// endpoint
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "peer_endpoint_entry"));
2017-11-06 15:02:05 +03:00
str = gtk_entry_get_text (GTK_ENTRY (widget));
if (str && str[0]){
2018-02-07 23:05:54 +03:00
nm_setting_vpn_add_data_item (s_vpn, NM_WG_KEY_ENDPOINT, str);
}
2017-11-06 15:02:05 +03:00
nm_connection_add_setting (connection, NM_SETTING (s_vpn));
valid = TRUE;
return valid;
}
// function to determine if the connection is new, according to its data items
2017-11-06 15:02:05 +03:00
static void
is_new_func (const char *key, const char *value, gpointer user_data)
{
gboolean *is_new = user_data;
/* If there are any VPN data items the connection isn't new */
*is_new = FALSE;
}
/*****************************************************************************/
static void
wireguard_editor_plugin_widget_init (WireguardEditor *plugin)
2017-11-06 15:02:05 +03:00
{
}
NMVpnEditor *
wireguard_editor_new (NMConnection *connection, GError **error)
2017-11-06 15:02:05 +03:00
{
NMVpnEditor *object;
WireguardEditorPrivate *priv;
2017-11-06 15:02:05 +03:00
gboolean new = TRUE;
NMSettingVpn *s_vpn;
if (error)
g_return_val_if_fail (*error == NULL, NULL);
object = g_object_new (WIREGUARD_TYPE_EDITOR, NULL);
2017-11-06 15:02:05 +03:00
if (!object) {
g_set_error_literal (error, NMV_EDITOR_PLUGIN_ERROR, 0, "Could not create wireguard object");
2017-11-06 15:02:05 +03:00
return NULL;
}
priv = WIREGUARD_EDITOR_GET_PRIVATE (object);
2017-11-06 15:02:05 +03:00
priv->builder = gtk_builder_new ();
gtk_builder_set_translation_domain (priv->builder, GETTEXT_PACKAGE);
// create the GUI from our .ui file
// note: the resource is described in gresource.xml and gets compiled to resources.c
if (!gtk_builder_add_from_resource (priv->builder, "/org/freedesktop/network-manager-wireguard/nm-wireguard-dialog.ui", error)) {
2017-11-06 15:02:05 +03:00
g_object_unref (object);
g_return_val_if_reached (NULL);
}
2018-02-07 23:05:54 +03:00
priv->widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "wg-vbox"));
2017-11-06 15:02:05 +03:00
if (!priv->widget) {
g_set_error_literal (error, NMV_EDITOR_PLUGIN_ERROR, 0, _("could not load UI widget"));
g_object_unref (object);
g_return_val_if_reached (NULL);
}
g_object_ref_sink (priv->widget);
s_vpn = nm_connection_get_setting_vpn (connection);
2018-02-07 23:05:54 +03:00
// if there is at least one item to iterate over, the connection can't be new
2017-11-06 15:02:05 +03:00
if (s_vpn)
nm_setting_vpn_foreach_data_item (s_vpn, is_new_func, &new);
priv->new_connection = new;
if (!init_editor_plugin (WIREGUARD_EDITOR (object), connection, error)) {
2017-11-06 15:02:05 +03:00
g_object_unref (object);
return NULL;
}
return object;
}
static void
dispose (GObject *object)
{
WireguardEditor *plugin = WIREGUARD_EDITOR (object);
WireguardEditorPrivate *priv = WIREGUARD_EDITOR_GET_PRIVATE (plugin);
2017-11-06 15:02:05 +03:00
g_clear_object (&priv->widget);
g_clear_object (&priv->builder);
G_OBJECT_CLASS (wireguard_editor_plugin_widget_parent_class)->dispose (object);
2017-11-06 15:02:05 +03:00
}
static void
wireguard_editor_plugin_widget_interface_init (NMVpnEditorInterface *iface_class)
2017-11-06 15:02:05 +03:00
{
/* interface implementation */
iface_class->get_widget = get_widget;
iface_class->update_connection = update_connection;
}
static void
wireguard_editor_plugin_widget_class_init (WireguardEditorClass *req_class)
2017-11-06 15:02:05 +03:00
{
GObjectClass *object_class = G_OBJECT_CLASS (req_class);
g_type_class_add_private (req_class, sizeof (WireguardEditorPrivate));
2017-11-06 15:02:05 +03:00
object_class->dispose = dispose;
}
/*****************************************************************************/
#ifndef NM_VPN_OLD
2018-02-12 18:06:08 +03:00
#include "nm-wireguard-editor-plugin.h"
2017-11-06 15:02:05 +03:00
G_MODULE_EXPORT NMVpnEditor *
nm_vpn_editor_factory_wireguard (NMVpnEditorPlugin *editor_plugin,
2017-11-06 15:02:05 +03:00
NMConnection *connection,
GError **error)
{
g_return_val_if_fail (!error || !*error, NULL);
return wireguard_editor_new (connection, error);
2017-11-06 15:02:05 +03:00
}
#endif