From 743d3cefc244e669c9265da8355e8722d990fc8e Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 26 Jul 2021 15:06:06 +0200 Subject: [PATCH] Let the CLI prevent adding incorrect Subnets. We did a sanitiy check when trying to add a Subnet, but we only printed an error message, we still added the incorrect Subnet. This change ensures we abort with a non-zero exit code. --- src/tincctl.c | 1 + test/variables.test | 63 +++++++++++++++++++++++++++++---------------- 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/tincctl.c b/src/tincctl.c index ec434696..499a7b86 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -1936,6 +1936,7 @@ static int cmd_config(int argc, char *argv[]) { if(!str2net(&s, value)) { fprintf(stderr, "Malformed subnet definition %s\n", value); + return 1; } if(!subnetcheck(s)) { diff --git a/test/variables.test b/test/variables.test index e63138f3..897059de 100755 --- a/test/variables.test +++ b/test/variables.test @@ -34,17 +34,17 @@ test "$(tinc foo get Mode)" = "hub" echo [STEP] Test addition/deletion of multivalued variables -tinc foo add Subnet 1 -tinc foo add Subnet 2 -tinc foo add Subnet 2 -tinc foo add Subnet 3 -test "$(tinc foo get Subnet | rm_cr)" = "1 -2 -3" - -tinc foo del Subnet 2 -test "$(tinc foo get Subnet | rm_cr)" = "1 -3" +tinc foo add Subnet 1.1.1.1 +tinc foo add Subnet 2.2.2.2 +tinc foo add Subnet 2.2.2.2 +tinc foo add Subnet 3.3.3.3 +test "$(tinc foo get Subnet | rm_cr)" = "1.1.1.1 +2.2.2.2 +3.3.3.3" + +tinc foo del Subnet 2.2.2.2 +test "$(tinc foo get Subnet | rm_cr)" = "1.1.1.1 +3.3.3.3" tinc foo del Subnet test -z "$(tinc foo get Subnet)" @@ -62,17 +62,17 @@ tinc foo add bar.PMTU 1 tinc foo add bar.PMTU 2 test "$(tinc foo get bar.PMTU)" = "2" -tinc foo add bar.Subnet 1 -tinc foo add bar.Subnet 2 -tinc foo add bar.Subnet 2 -tinc foo add bar.Subnet 3 -test "$(tinc foo get bar.Subnet | rm_cr)" = "1 -2 -3" +tinc foo add bar.Subnet 1.1.1.1 +tinc foo add bar.Subnet 2.2.2.2 +tinc foo add bar.Subnet 2.2.2.2 +tinc foo add bar.Subnet 3.3.3.3 +test "$(tinc foo get bar.Subnet | rm_cr)" = "1.1.1.1 +2.2.2.2 +3.3.3.3" -tinc foo del bar.Subnet 2 -test "$(tinc foo get bar.Subnet | rm_cr)" = "1 -3" +tinc foo del bar.Subnet 2.2.2.2 +test "$(tinc foo get bar.Subnet | rm_cr)" = "1.1.1.1 +3.3.3.3" tinc foo del bar.Subnet test -z "$(tinc foo get bar.Subnet)" @@ -80,7 +80,7 @@ test -z "$(tinc foo get bar.Subnet)" echo [STEP] We should not be able to get/set for nodes with invalid names touch "$DIR_FOO/hosts/qu-ux" -expect_code "$EXIT_FAILURE" tinc foo set qu-ux.Subnet 1 +expect_code "$EXIT_FAILURE" tinc foo set qu-ux.Subnet 1.1.1.1 echo [STEP] We should not be able to set obsolete variables unless forced @@ -90,3 +90,22 @@ test "$(tinc foo get PrivateKey)" = "12345" tinc foo del PrivateKey test -z "$(tinc foo get PrivateKey)" + +echo [STEP] We should not be able to set/add malformed Subnets + +expect_code "$EXIT_FAILURE" tinc foo add Subnet 1.1.1 +expect_code "$EXIT_FAILURE" tinc foo add Subnet 1:2:3:4:5: +expect_code "$EXIT_FAILURE" tinc foo add Subnet 1:2:3:4:5:::6 +expect_code "$EXIT_FAILURE" tinc foo add Subnet 1:2:3:4:5:6:7:8:9 +expect_code "$EXIT_FAILURE" tinc foo add Subnet 256.256.256.256 +expect_code "$EXIT_FAILURE" tinc foo add Subnet 1:2:3:4:5:6:7:8.123 +expect_code "$EXIT_FAILURE" tinc foo add Subnet 1:2:3:4:5:6:7:1.2.3.4 +expect_code "$EXIT_FAILURE" tinc foo add Subnet a:b:c:d:e:f:g:h +expect_code "$EXIT_FAILURE" tinc foo add Subnet 1.1.1.1/0 +expect_code "$EXIT_FAILURE" tinc foo add Subnet 1.1.1.1/-1 +expect_code "$EXIT_FAILURE" tinc foo add Subnet 1.1.1.1/33 +expect_code "$EXIT_FAILURE" tinc foo add Subnet 1::/0 +expect_code "$EXIT_FAILURE" tinc foo add Subnet 1::/-1 +expect_code "$EXIT_FAILURE" tinc foo add Subnet 1::/129 +expect_code "$EXIT_FAILURE" tinc foo add Subnet :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +test -z "$(tinc foo get Subnet)" -- 2.20.1