From: Michael Tremer
Date: Sat, 26 Apr 2025 11:54:30 +0000 (+0200)
Subject: wireguard.cgi: Rebuild the importer
X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0dc47e5dbd6df2ba54f20617bd54b2ae3f0bbec5;p=people%2Fms%2Fipfire-2.x.git
wireguard.cgi: Rebuild the importer
This is now a two-step process that is asking for all sorts of required
information.
Signed-off-by: Michael Tremer
---
diff --git a/config/cfgroot/wireguard-functions.pl b/config/cfgroot/wireguard-functions.pl
index c8af939b5..e9dc66747 100644
--- a/config/cfgroot/wireguard-functions.pl
+++ b/config/cfgroot/wireguard-functions.pl
@@ -453,10 +453,13 @@ sub generate_peer_configuration($$) {
return join("\n", @conf);
}
-sub parse_configuration($) {
+sub parse_configuration($$) {
+ my $name = shift;
my $fh = shift;
- my %peer = ();
+ my %peer = (
+ "NAME" => $name,
+ );
# Collect any errors
my @errormessages = ();
@@ -465,6 +468,16 @@ sub parse_configuration($) {
my $key = undef;
my $val = undef;
+ # Check if the name is valid
+ unless (&Wireguard::name_is_valid($name)) {
+ push(@errormessages, $Lang::tr{'wg invalid name'});
+ }
+
+ # Check if the name is already taken
+ unless (&Wireguard::name_is_free($name)) {
+ push(@errormessages, $Lang::tr{'wg name is already used'});
+ }
+
while (<$fh>) {
# Remove line breaks
chomp;
@@ -498,6 +511,14 @@ sub parse_configuration($) {
push(@errormessages, $Lang::tr{'invalid ip address'});
}
+ # Port
+ } elsif ($key eq "Port") {
+ if (&General::validport($val)) {
+ $peer{'PORT'} = $val;
+ } else {
+ push(@errormessages, $Lang::tr{'wg invalid endpoint port'});
+ }
+
# PrivateKey
} elsif ($key eq "PrivateKey") {
if (&key_is_valid($val)) {
@@ -536,7 +557,7 @@ sub parse_configuration($) {
}
}
- $peer{'REMOTE_SUBNETS'} = join(", ", @networks);
+ $peer{'REMOTE_SUBNETS'} = \@networks;
# Endpoint
} elsif ($key eq "Endpoint") {
my $address = $val;
@@ -578,7 +599,7 @@ sub parse_configuration($) {
}
}
- return %peer, @errormessages;
+ return \%peer, @errormessages;
}
sub get_free_port() {
diff --git a/doc/language_issues.de b/doc/language_issues.de
index 090850fbe..39e934047 100644
--- a/doc/language_issues.de
+++ b/doc/language_issues.de
@@ -957,6 +957,7 @@ WARNING: untranslated string: cake profile pppoe-llcsnap 40 = PPPoE LLC SNAP (40
WARNING: untranslated string: cake profile pppoe-ptm 27 = PPPoE PTM (27 bytes)
WARNING: untranslated string: cake profile pppoe-vcmux 32 = PPPoE VC-MUX (32 bytes)
WARNING: untranslated string: cake profile raw 0 = Raw (no overhead compensation)
+WARNING: untranslated string: configuration file = Configuration File
WARNING: untranslated string: data transfer = Data Transfer
WARNING: untranslated string: desired = Desired
WARNING: untranslated string: disable = Disable
@@ -1047,6 +1048,7 @@ WARNING: untranslated string: wg download configuration file = Download the conf
WARNING: untranslated string: wg edit host-to-net peer = Edit Host-To-Net Peer
WARNING: untranslated string: wg edit net-to-net peer = Edit Net-To-Net Peer
WARNING: untranslated string: wg host to net client settings = Host-To-Net Client Settings
+WARNING: untranslated string: wg import peer = Import Peer
WARNING: untranslated string: wg invalid client dns = Invalid client DNS address
WARNING: untranslated string: wg invalid client pool = Invalid client pool
WARNING: untranslated string: wg invalid endpoint address = Invalid endpoint address
diff --git a/doc/language_issues.en b/doc/language_issues.en
index 1c1c546f7..834ffa13b 100644
--- a/doc/language_issues.en
+++ b/doc/language_issues.en
@@ -479,6 +479,7 @@ WARNING: untranslated string: common name = Common name
WARNING: untranslated string: comp-lzo = LZO-Compression:
WARNING: untranslated string: computer to modem rate = Computer to modem rate:
WARNING: untranslated string: concentrator name = Concentrator name:
+WARNING: untranslated string: configuration file = Configuration File
WARNING: untranslated string: confirmation = confirmation
WARNING: untranslated string: connect timeout = Connect timeout:
WARNING: untranslated string: connected = Connected
@@ -2164,6 +2165,7 @@ WARNING: untranslated string: wg download configuration file = Download the conf
WARNING: untranslated string: wg edit host-to-net peer = Edit Host-To-Net Peer
WARNING: untranslated string: wg edit net-to-net peer = Edit Net-To-Net Peer
WARNING: untranslated string: wg host to net client settings = Host-To-Net Client Settings
+WARNING: untranslated string: wg import peer = Import Peer
WARNING: untranslated string: wg invalid client dns = Invalid client DNS address
WARNING: untranslated string: wg invalid client pool = Invalid client pool
WARNING: untranslated string: wg invalid endpoint address = Invalid endpoint address
diff --git a/doc/language_issues.es b/doc/language_issues.es
index cf7237435..ae4146621 100644
--- a/doc/language_issues.es
+++ b/doc/language_issues.es
@@ -1012,6 +1012,7 @@ WARNING: untranslated string: access point name is required = Access Point Name
WARNING: untranslated string: allowed subnets = Allowed Subnets
WARNING: untranslated string: bypassed = Bypassed
WARNING: untranslated string: ca name must only contain characters and spaces = unknown string
+WARNING: untranslated string: configuration file = Configuration File
WARNING: untranslated string: cpu frequency = CPU frequency
WARNING: untranslated string: data transfer = Data Transfer
WARNING: untranslated string: dhcp fixed ip address in dynamic range = Fixed IP Address in dynamic range
@@ -1122,6 +1123,7 @@ WARNING: untranslated string: wg download configuration file = Download the conf
WARNING: untranslated string: wg edit host-to-net peer = Edit Host-To-Net Peer
WARNING: untranslated string: wg edit net-to-net peer = Edit Net-To-Net Peer
WARNING: untranslated string: wg host to net client settings = Host-To-Net Client Settings
+WARNING: untranslated string: wg import peer = Import Peer
WARNING: untranslated string: wg invalid client dns = Invalid client DNS address
WARNING: untranslated string: wg invalid client pool = Invalid client pool
WARNING: untranslated string: wg invalid endpoint address = Invalid endpoint address
diff --git a/doc/language_issues.fr b/doc/language_issues.fr
index 702911061..ad26957a6 100644
--- a/doc/language_issues.fr
+++ b/doc/language_issues.fr
@@ -977,6 +977,7 @@ WARNING: translation string unused: zoneconf val zoneslave amount error
WARNING: untranslated string: allowed subnets = Allowed Subnets
WARNING: untranslated string: bypassed = Bypassed
WARNING: untranslated string: ca name must only contain characters and spaces = unknown string
+WARNING: untranslated string: configuration file = Configuration File
WARNING: untranslated string: core notice 3 = available.
WARNING: untranslated string: data transfer = Data Transfer
WARNING: untranslated string: done = Done
@@ -1062,6 +1063,7 @@ WARNING: untranslated string: wg download configuration file = Download the conf
WARNING: untranslated string: wg edit host-to-net peer = Edit Host-To-Net Peer
WARNING: untranslated string: wg edit net-to-net peer = Edit Net-To-Net Peer
WARNING: untranslated string: wg host to net client settings = Host-To-Net Client Settings
+WARNING: untranslated string: wg import peer = Import Peer
WARNING: untranslated string: wg invalid client dns = Invalid client DNS address
WARNING: untranslated string: wg invalid client pool = Invalid client pool
WARNING: untranslated string: wg invalid endpoint address = Invalid endpoint address
diff --git a/doc/language_issues.it b/doc/language_issues.it
index 3d93239af..bf4b2a302 100644
--- a/doc/language_issues.it
+++ b/doc/language_issues.it
@@ -996,6 +996,7 @@ WARNING: untranslated string: cake profile pppoe-ptm 27 = PPPoE PTM (27 bytes)
WARNING: untranslated string: cake profile pppoe-vcmux 32 = PPPoE VC-MUX (32 bytes)
WARNING: untranslated string: cake profile raw 0 = Raw (no overhead compensation)
WARNING: untranslated string: check all = Check all
+WARNING: untranslated string: configuration file = Configuration File
WARNING: untranslated string: core update = Core-Update
WARNING: untranslated string: cpu frequency = CPU frequency
WARNING: untranslated string: crypto error = Cryptographic error
@@ -1408,6 +1409,7 @@ WARNING: untranslated string: wg download configuration file = Download the conf
WARNING: untranslated string: wg edit host-to-net peer = Edit Host-To-Net Peer
WARNING: untranslated string: wg edit net-to-net peer = Edit Net-To-Net Peer
WARNING: untranslated string: wg host to net client settings = Host-To-Net Client Settings
+WARNING: untranslated string: wg import peer = Import Peer
WARNING: untranslated string: wg invalid client dns = Invalid client DNS address
WARNING: untranslated string: wg invalid client pool = Invalid client pool
WARNING: untranslated string: wg invalid endpoint address = Invalid endpoint address
diff --git a/doc/language_issues.nl b/doc/language_issues.nl
index f1090fc33..6fcfa82bc 100644
--- a/doc/language_issues.nl
+++ b/doc/language_issues.nl
@@ -998,6 +998,7 @@ WARNING: untranslated string: cake profile pppoe-vcmux 32 = PPPoE VC-MUX (32 byt
WARNING: untranslated string: cake profile raw 0 = Raw (no overhead compensation)
WARNING: untranslated string: capabilities = Capabilities
WARNING: untranslated string: check all = Check all
+WARNING: untranslated string: configuration file = Configuration File
WARNING: untranslated string: cpu frequency = CPU frequency
WARNING: untranslated string: crypto error = Cryptographic error
WARNING: untranslated string: crypto warning = Cryptographic warning
@@ -1429,6 +1430,7 @@ WARNING: untranslated string: wg download configuration file = Download the conf
WARNING: untranslated string: wg edit host-to-net peer = Edit Host-To-Net Peer
WARNING: untranslated string: wg edit net-to-net peer = Edit Net-To-Net Peer
WARNING: untranslated string: wg host to net client settings = Host-To-Net Client Settings
+WARNING: untranslated string: wg import peer = Import Peer
WARNING: untranslated string: wg invalid client dns = Invalid client DNS address
WARNING: untranslated string: wg invalid client pool = Invalid client pool
WARNING: untranslated string: wg invalid endpoint address = Invalid endpoint address
diff --git a/doc/language_issues.pl b/doc/language_issues.pl
index 1db36fb67..c5346243a 100644
--- a/doc/language_issues.pl
+++ b/doc/language_issues.pl
@@ -960,6 +960,7 @@ WARNING: untranslated string: ccd routes = Routing:
WARNING: untranslated string: ccd subnet = Subnet
WARNING: untranslated string: ccd used = Used addresses
WARNING: untranslated string: check all = Check all
+WARNING: untranslated string: configuration file = Configuration File
WARNING: untranslated string: core update = Core-Update
WARNING: untranslated string: count = Count
WARNING: untranslated string: countries = Countries
@@ -1671,6 +1672,7 @@ WARNING: untranslated string: wg download configuration file = Download the conf
WARNING: untranslated string: wg edit host-to-net peer = Edit Host-To-Net Peer
WARNING: untranslated string: wg edit net-to-net peer = Edit Net-To-Net Peer
WARNING: untranslated string: wg host to net client settings = Host-To-Net Client Settings
+WARNING: untranslated string: wg import peer = Import Peer
WARNING: untranslated string: wg invalid client dns = Invalid client DNS address
WARNING: untranslated string: wg invalid client pool = Invalid client pool
WARNING: untranslated string: wg invalid endpoint address = Invalid endpoint address
diff --git a/doc/language_issues.ru b/doc/language_issues.ru
index 4d29c4f95..4798572d6 100644
--- a/doc/language_issues.ru
+++ b/doc/language_issues.ru
@@ -955,6 +955,7 @@ WARNING: untranslated string: ccd routes = Routing:
WARNING: untranslated string: ccd subnet = Subnet
WARNING: untranslated string: ccd used = Used addresses
WARNING: untranslated string: check all = Check all
+WARNING: untranslated string: configuration file = Configuration File
WARNING: untranslated string: core update = Core-Update
WARNING: untranslated string: count = Count
WARNING: untranslated string: countries = Countries
@@ -1664,6 +1665,7 @@ WARNING: untranslated string: wg download configuration file = Download the conf
WARNING: untranslated string: wg edit host-to-net peer = Edit Host-To-Net Peer
WARNING: untranslated string: wg edit net-to-net peer = Edit Net-To-Net Peer
WARNING: untranslated string: wg host to net client settings = Host-To-Net Client Settings
+WARNING: untranslated string: wg import peer = Import Peer
WARNING: untranslated string: wg invalid client dns = Invalid client DNS address
WARNING: untranslated string: wg invalid client pool = Invalid client pool
WARNING: untranslated string: wg invalid endpoint address = Invalid endpoint address
diff --git a/doc/language_issues.tr b/doc/language_issues.tr
index 2da19f276..5878f55fa 100644
--- a/doc/language_issues.tr
+++ b/doc/language_issues.tr
@@ -981,6 +981,7 @@ WARNING: untranslated string: cake profile pppoe-llcsnap 40 = PPPoE LLC SNAP (40
WARNING: untranslated string: cake profile pppoe-ptm 27 = PPPoE PTM (27 bytes)
WARNING: untranslated string: cake profile pppoe-vcmux 32 = PPPoE VC-MUX (32 bytes)
WARNING: untranslated string: cake profile raw 0 = Raw (no overhead compensation)
+WARNING: untranslated string: configuration file = Configuration File
WARNING: untranslated string: core update = Core-Update
WARNING: untranslated string: cpu frequency = CPU frequency
WARNING: untranslated string: crypto error = Cryptographic error
@@ -1291,6 +1292,7 @@ WARNING: untranslated string: wg download configuration file = Download the conf
WARNING: untranslated string: wg edit host-to-net peer = Edit Host-To-Net Peer
WARNING: untranslated string: wg edit net-to-net peer = Edit Net-To-Net Peer
WARNING: untranslated string: wg host to net client settings = Host-To-Net Client Settings
+WARNING: untranslated string: wg import peer = Import Peer
WARNING: untranslated string: wg invalid client dns = Invalid client DNS address
WARNING: untranslated string: wg invalid client pool = Invalid client pool
WARNING: untranslated string: wg invalid endpoint address = Invalid endpoint address
diff --git a/doc/language_missings b/doc/language_missings
index 48b98ce74..6c96352c8 100644
--- a/doc/language_missings
+++ b/doc/language_missings
@@ -46,6 +46,7 @@
< Captive invalid coupon
< Captive please enter a coupon code
< choose media
+< configuration file
< could not connect to www ipfire org
< cryptographic settings
< data transfer
@@ -129,6 +130,7 @@
< wg edit net-to-net peer
< wg edit peer
< wg host to net client settings
+< wg import peer
< wg invalid client dns
< wg invalid client pool
< wg invalid endpoint address
@@ -170,6 +172,7 @@
< allowed subnets
< bypassed
< ca name must only contain characters or spaces
+< configuration file
< cpu frequency
< data transfer
< dhcp fixed ip address in dynamic range
@@ -246,6 +249,7 @@
< wg edit net-to-net peer
< wg edit peer
< wg host to net client settings
+< wg import peer
< wg invalid client dns
< wg invalid client pool
< wg invalid endpoint address
@@ -286,6 +290,7 @@
< bewan adsl usb
< bypassed
< ca name must only contain characters or spaces
+< configuration file
< data transfer
< done
< endpoint
@@ -343,6 +348,7 @@
< wg edit net-to-net peer
< wg edit peer
< wg host to net client settings
+< wg import peer
< wg invalid client dns
< wg invalid client pool
< wg invalid endpoint address
@@ -482,6 +488,7 @@
< Captive WiFi coupon
< Captive wrong type
< check all
+< configuration file
< core update
< cpu frequency
< crypto error
@@ -894,6 +901,7 @@
< wg edit net-to-net peer
< wg edit peer
< wg host to net client settings
+< wg import peer
< wg invalid client dns
< wg invalid client pool
< wg invalid endpoint address
@@ -1085,6 +1093,7 @@
< Captive WiFi coupon
< Captive wrong type
< check all
+< configuration file
< cpu frequency
< crypto error
< cryptographic settings
@@ -1521,6 +1530,7 @@
< wg edit net-to-net peer
< wg edit peer
< wg host to net client settings
+< wg import peer
< wg invalid client dns
< wg invalid client pool
< wg invalid endpoint address
@@ -1766,6 +1776,7 @@
< ccd used
< check all
< community rules
+< configuration file
< ConnSched dial
< ConnSched hangup
< ConnSched reboot
@@ -2564,6 +2575,7 @@
< wg edit net-to-net peer
< wg edit peer
< wg host to net client settings
+< wg import peer
< wg invalid client dns
< wg invalid client pool
< wg invalid endpoint address
@@ -2841,6 +2853,7 @@
< ccd used
< check all
< community rules
+< configuration file
< ConnSched dial
< ConnSched hangup
< ConnSched reboot
@@ -3644,6 +3657,7 @@
< wg edit net-to-net peer
< wg edit peer
< wg host to net client settings
+< wg import peer
< wg invalid client dns
< wg invalid client pool
< wg invalid endpoint address
@@ -3802,6 +3816,7 @@
< cake profile raw 0
< ca name must only contain characters or spaces
< Captive delete logo
+< configuration file
< core update
< cpu frequency
< crypto error
@@ -4101,6 +4116,7 @@
< wg edit net-to-net peer
< wg edit peer
< wg host to net client settings
+< wg import peer
< wg invalid client dns
< wg invalid client pool
< wg invalid endpoint address
diff --git a/html/cgi-bin/wireguard.cgi b/html/cgi-bin/wireguard.cgi
index 7d7e3422f..e8836439d 100644
--- a/html/cgi-bin/wireguard.cgi
+++ b/html/cgi-bin/wireguard.cgi
@@ -164,6 +164,73 @@ if ($cgiparams{"ACTION"} eq $Lang::tr{'save'}) {
die "Unsupported type: $type";
}
+} elsif ($cgiparams{"ACTION"} eq "IMPORT") {
+ my @local_subnets = ();
+ my $peer;
+
+ # Parse the configuration file
+ ($peer, @errormessages) = &Wireguard::parse_configuration($cgiparams{'NAME'}, $cgiparams{'FH'});
+
+ # Check local subnets
+ if (defined $cgiparams{'LOCAL_SUBNETS'}) {
+ @local_subnets = split(/,/, $cgiparams{'LOCAL_SUBNETS'});
+
+ foreach my $subnet (@local_subnets) {
+ $subnet =~ s/^\s+//g;
+ $subnet =~ s/\s+$//g;
+
+ unless (&Network::check_subnet($subnet)) {
+ push(@errormessages, $Lang::tr{'wg invalid local subnet'} . ": ${subnet}");
+ }
+ }
+ } else {
+ push(@errormessages, $Lang::tr{'wg no local subnets'});
+ }
+
+ # Show any error messages
+ goto IMPORT if (@errormessages);
+
+ # Allocate a new key
+ my $key = &General::findhasharraykey(\%Wireguard::peers);
+
+ # Save the connection
+ $Wireguard::peers{$key} = [
+ # 0 = Enabled
+ "on",
+ # 1 = Type
+ "net",
+ # 2 = Name
+ $peer->{"NAME"},
+ # 3 = Remote Public Key
+ $peer->{"PUBLIC_KEY"},
+ # 4 = Local Private Key
+ $peer->{"PRIVATE_KEY"},
+ # 5 = Port
+ $peer->{"PORT"},
+ # 6 = Endpoint Address
+ $peer->{"ENDPOINT_ADDRESS"},
+ # 7 = Endpoint Port
+ $peer->{"ENDPOINT_PORT"},
+ # 8 = Remote Subnets
+ &Wireguard::encode_subnets(@{ $peer->{"REMOTE_SUBNETS"} }),
+ # 9 = Remark
+ &Wireguard::encode_remarks($cgiparams{"REMARKS"}),
+ # 10 = Local Subnets
+ &Wireguard::encode_subnets(@local_subnets),
+ # 11 = PSK
+ $peer->{"PSK"},
+ # 12 = Keepalive
+ $peer->{"KEEPALIVE"} || $Wireguard::DEFAULT_KEEPALIVE,
+ ];
+
+ # Store the configuration
+ &General::writehasharray("/var/ipfire/wireguard/peers", \%Wireguard::peers);
+
+ # Reload if enabled
+ if ($Wireguard::settings{'ENABLED'} eq "on") {
+ &General::system("/usr/local/bin/wireguardctrl", "start");
+ }
+
} elsif ($cgiparams{"ACTION"} eq "CREATE-PEER-NET") {
my @local_subnets = ();
my @remote_subnets = ();
@@ -666,12 +733,7 @@ END
goto CREATEHOST;
} elsif ($cgiparams{"TYPE"} eq "import") {
- # Parse the configuration file
- (%cgiparams, @errormessages) = &Wireguard::parse_configuration($cgiparams{'FH'});
-
- # We basically don't support importing RW connections, so we always
- # need to go and show the N2N editor.
- goto EDITNET;
+ goto IMPORT;
# Ask the user what type they want
} else {
@@ -1003,8 +1065,6 @@ ADD:
$Lang::tr{'import connection'}
-
-