Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E84ACCA9EA9 for ; Fri, 18 Oct 2019 15:53:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A6E7E2064B for ; Fri, 18 Oct 2019 15:53:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2405953AbfJRPxZ (ORCPT ); Fri, 18 Oct 2019 11:53:25 -0400 Received: from orbyte.nwl.cc ([151.80.46.58]:44358 "EHLO orbyte.nwl.cc" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727668AbfJRPxZ (ORCPT ); Fri, 18 Oct 2019 11:53:25 -0400 Received: from localhost ([::1]:57448 helo=tatos) by orbyte.nwl.cc with esmtp (Exim 4.91) (envelope-from ) id 1iLUZ9-0006Ll-4m; Fri, 18 Oct 2019 17:53:23 +0200 From: Phil Sutter To: Pablo Neira Ayuso Cc: netfilter-devel@vger.kernel.org Subject: [iptables PATCH 1/2] iptables-xml: Use add_param_to_argv() Date: Fri, 18 Oct 2019 17:53:08 +0200 Message-Id: <20191018155309.8250-1-phil@nwl.cc> X-Mailer: git-send-email 2.23.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: netfilter-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netfilter-devel@vger.kernel.org Extend the shared argv parser by storing whether a given argument was quoted or not, then use it in iptables-xml. One remaining extra bit is extraction of chain name in -A commands, do that afterwards in a loop. Signed-off-by: Phil Sutter --- iptables/iptables-xml.c | 78 +- .../testcases/ipt-save/0006iptables-xml_0 | 13 + .../ipt-save/dumps/fedora27-iptables.xml | 925 ++++++++++++++++++ iptables/xshared.c | 6 +- 4 files changed, 949 insertions(+), 73 deletions(-) create mode 100755 iptables/tests/shell/testcases/ipt-save/0006iptables-xml_0 create mode 100644 iptables/tests/shell/testcases/ipt-save/dumps/fedora27-iptables.xml diff --git a/iptables/iptables-xml.c b/iptables/iptables-xml.c index 5255e097eba88..eafee64f5e954 100644 --- a/iptables/iptables-xml.c +++ b/iptables/iptables-xml.c @@ -647,78 +647,8 @@ iptables_xml_main(int argc, char *argv[]) char *parsestart = buffer; char *chain = NULL; - /* the parser */ - char *param_start, *curchar; - int quote_open, quoted; - char param_buffer[1024]; - tokenize_rule_counters(&parsestart, &pcnt, &bcnt, line); - - /* This is a 'real' parser crafted in artist mode - * not hacker mode. If the author can live with that - * then so can everyone else */ - - quote_open = 0; - /* We need to know which args were quoted so we - can preserve quote */ - quoted = 0; - param_start = parsestart; - - for (curchar = parsestart; *curchar; curchar++) { - if (*curchar == '"') { - /* quote_open cannot be true if there - * was no previous character. Thus, - * curchar-1 has to be within bounds */ - if (quote_open && - *(curchar - 1) != '\\') { - quote_open = 0; - *curchar = ' '; - } else { - quote_open = 1; - quoted = 1; - param_start++; - } - } - if (*curchar == ' ' - || *curchar == '\t' || *curchar == '\n') { - int param_len = curchar - param_start; - - if (quote_open) - continue; - - if (!param_len) { - /* two spaces? */ - param_start++; - continue; - } - - /* end of one parameter */ - strncpy(param_buffer, param_start, - param_len); - *(param_buffer + param_len) = '\0'; - - /* check if table name specified */ - if ((param_buffer[0] == '-' && - param_buffer[1] != '-' && - strchr(param_buffer, 't')) || - (!strncmp(param_buffer, "--t", 3) && - !strncmp(param_buffer, "--table", strlen(param_buffer)))) - xtables_error(PARAMETER_PROBLEM, - "Line %u seems to have a " - "-t table option.\n", - line); - - add_argv(param_buffer, quoted); - if (newargc >= 2 - && 0 == - strcmp(newargv[newargc - 2], "-A")) - chain = newargv[newargc - 1]; - quoted = 0; - param_start += param_len + 1; - } else { - /* regular character, skip */ - } - } + add_param_to_argv(parsestart, line); DEBUGP("calling do_command4(%u, argv, &%s, handle):\n", newargc, curTable); @@ -726,6 +656,12 @@ iptables_xml_main(int argc, char *argv[]) for (a = 0; a < newargc; a++) DEBUGP("argv[%u]: %s\n", a, newargv[a]); + for (a = 1; a < newargc; a++) { + if (strcmp(newargv[a - 1], "-A")) + continue; + chain = newargv[a]; + break; + } if (!chain) { fprintf(stderr, "%s: line %u failed - no chain found\n", prog_name, line); diff --git a/iptables/tests/shell/testcases/ipt-save/0006iptables-xml_0 b/iptables/tests/shell/testcases/ipt-save/0006iptables-xml_0 new file mode 100755 index 0000000000000..50c0cae888341 --- /dev/null +++ b/iptables/tests/shell/testcases/ipt-save/0006iptables-xml_0 @@ -0,0 +1,13 @@ +#!/bin/bash + +case "$(basename $XT_MULTI)" in + xtables-legacy-multi) + ;; + *) + echo "skip $XT_MULTI" + exit 0 + ;; +esac + +dump=$(dirname $0)/dumps/fedora27-iptables +diff -u -Z <(cat ${dump}.xml) <($XT_MULTI iptables-xml <$dump) diff --git a/iptables/tests/shell/testcases/ipt-save/dumps/fedora27-iptables.xml b/iptables/tests/shell/testcases/ipt-save/dumps/fedora27-iptables.xml new file mode 100644 index 0000000000000..400be032fbd20 --- /dev/null +++ b/iptables/tests/shell/testcases/ipt-save/dumps/fedora27-iptables.xml @@ -0,0 +1,925 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + virbr0 +

udp

+
+ + 68 + +
+ + + + + + +
+ + + + + + + + + + +
+ + + + + wlp58s0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + wlp58s0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

udp

+
+ + 137 + +
+ + + netbios-ns + + + +
+ +
+ + + + + +
+ + + + + + + + virbr0 +

udp

+
+ + 53 + +
+ + + + +
+ + + + + virbr0 +

tcp

+
+ + 53 + +
+ + + + +
+ + + + + virbr0 +

udp

+
+ + 67 + +
+ + + + +
+ + + + + virbr0 +

tcp

+
+ + 67 + +
+ + + + +
+ + + + + RELATED,ESTABLISHED + + + + + + + + + + + + lo + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + INVALID + + + + + + + + + + + + icmp-host-prohibited + + + + + +
+ + + + + 192.168.122.0/24 + virbr0 + + + RELATED,ESTABLISHED + + + + + + + + + + + + 192.168.122.0/24 + virbr0 + + + + + + + + + + + + virbr0 + virbr0 + + + + + + + + + + + + virbr0 + + + + + icmp-port-unreachable + + + + + + + + + virbr0 + + + + + icmp-port-unreachable + + + + + + + + + RELATED,ESTABLISHED + + + + + + + + + + + + lo + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + INVALID + + + + + + + + + + + + icmp-host-prohibited + + + + + + + + + + + virbr0 +

udp

+
+ + 68 + +
+ + + + +
+ + + + + + + + + + +
+ + + + + wlp58s0 + + + + + + + + + + + + + + + + + + + + + + + + + wlp58s0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

icmp

+
+
+ + + + +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + wlp58s0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

icmp

+
+
+ + + + +
+ +
+ + + + +

udp

+
+ + 137 + + + NEW + +
+ + + + +
+ + + + +

udp

+
+ + 138 + + + NEW + +
+ + + + +
+ + + + +

tcp

+
+ + 22 + + + NEW + +
+ + + + +
+ + + + + 224.0.0.251/32 +

udp

+
+ + 5353 + + + NEW + +
+ + + + +
+ + + + +

udp

+
+ + 1025:65535 + + + NEW + +
+ + + + +
+ + + + +

tcp

+
+ + 1025:65535 + + + NEW + +
+ + + + +
+ +
+ + + + + + + + + + + + + + +
+ +
diff --git a/iptables/xshared.c b/iptables/xshared.c index ba723f59dbaad..4c012e32c775f 100644 --- a/iptables/xshared.c +++ b/iptables/xshared.c @@ -484,7 +484,7 @@ static void add_param(struct xt_param_buf *param, const char *curchar) void add_param_to_argv(char *parsestart, int line) { - int quote_open = 0, escaped = 0; + int quote_open = 0, escaped = 0, quoted = 0; struct xt_param_buf param = {}; char *curchar; @@ -511,6 +511,7 @@ void add_param_to_argv(char *parsestart, int line) } else { if (*curchar == '"') { quote_open = 1; + quoted = 1; continue; } } @@ -545,8 +546,9 @@ void add_param_to_argv(char *parsestart, int line) line, xt_params->program_name); } - add_argv(param.buffer, 0); + add_argv(param.buffer, quoted); param.len = 0; + quoted = 0; } } -- 2.23.0