From patchwork Mon Dec 5 11:58:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kohei Suzuki X-Patchwork-Id: 702715 X-Patchwork-Delegate: pablo@netfilter.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3tXNgZ43wXz9t14 for ; Mon, 5 Dec 2016 23:03:06 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="lfQNory3"; dkim-atps=neutral Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751492AbcLEMBA (ORCPT ); Mon, 5 Dec 2016 07:01:00 -0500 Received: from mail-qk0-f181.google.com ([209.85.220.181]:35604 "EHLO mail-qk0-f181.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751722AbcLEL71 (ORCPT ); Mon, 5 Dec 2016 06:59:27 -0500 Received: by mail-qk0-f181.google.com with SMTP id n204so343115857qke.2 for ; Mon, 05 Dec 2016 03:59:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to; bh=s9JZLjEy+HrKkcIukEGJaUBOY3Oebn4VAjfGtLnazyI=; b=lfQNory3P5hNm3+dVp26OYsO8ULQWFEmRC79E66sbuoZ5loCHrTNWLRzzKgYQE7z3h Gmfg1Oa+Snb4qYBIqusTNwFvHojqnd7+cVFVLc0Q2hR+Zes4b2mw9+Mh5ZNmUyeQ3xIA ELBZtfItdvDOWf6w/crm8l7US7ICh/RirK9tJ4SMsrztqFU92YfcZLjSbrLYyS7Unosw 6ASdsd4u77Pz2qXwxw+xFd2GNhnaAohWisHtkejZRZxSBh4v46JJ5W2LDeMCBLbcfaLj J5vGPXce44UUBKPY0eRZ3X3Hx+UGCVDZdOY/eF3qb3pBOYu/Ofw29gNMKA2ht/nNvkeV COsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=s9JZLjEy+HrKkcIukEGJaUBOY3Oebn4VAjfGtLnazyI=; b=Z+5PaUcEuC5e8ROqrhhYGfjho7ZDdKq6p+S8W1CZSn0OLbiY5BuijkKbzg01fTyGhG uzToiZPQXjqMfsK88ROaVnRsIGDeOa4U7GmvfwaW8WPFZqj/OeW1W1h9u87pomA+FgRm Ms9OycvM5BcRHKsKpEvFVn4rzPCOU2CadN1cbSenLxdV2RI+oKv2SpFLbrewkIrc1U+r 67QHBCXDnTwuG2HGrnv3xaB4T7CuhZRfMwJByG1aDmY6YIY06mPCJvu0COxzSV7tfVwj oBhwo27FbfDOPSxlYyWJlafT9em2zFJSnByce9ucqZt3O4g0FSJPboVSmoupSp9A0kQg eRUQ== X-Gm-Message-State: AKaTC01ubC3gp80WnHMGzOWn1rSVilLFNcvVj48A165XZeblBsI4WDr7SuUmiHfCCVGifWkTiUv1ilrMc6xCeA== X-Received: by 10.55.102.83 with SMTP id a80mr48853741qkc.292.1480939139090; Mon, 05 Dec 2016 03:58:59 -0800 (PST) MIME-Version: 1.0 Received: by 10.200.35.86 with HTTP; Mon, 5 Dec 2016 03:58:38 -0800 (PST) From: Kohei Suzuki Date: Mon, 5 Dec 2016 20:58:38 +0900 Message-ID: Subject: [PATCH nft] parser: Add glob support to include directive To: netfilter-devel@vger.kernel.org Sender: netfilter-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netfilter-devel@vger.kernel.org --- src/scanner.l | 36 +++++++++++++++++---------- tests/shell/testcases/include/0005glob_0 | 32 ++++++++++++++++++++++++ tests/shell/testcases/include/0006globempty_1 | 14 +++++++++++ 3 files changed, 69 insertions(+), 13 deletions(-) create mode 100755 tests/shell/testcases/include/0005glob_0 create mode 100755 tests/shell/testcases/include/0006globempty_1 diff --git a/src/scanner.l b/src/scanner.l index 625023f..64fe6fc 100644 --- a/src/scanner.l +++ b/src/scanner.l @@ -11,6 +11,7 @@ %{ #include +#include #include #include #include @@ -640,37 +641,46 @@ int scanner_include_file(void *scanner, const char *filename, struct parser_state *state = yyget_extra(scanner); struct error_record *erec; char buf[PATH_MAX]; - const char *name = buf; unsigned int i; - FILE *f; + glob_t globbuf; - f = NULL; + globbuf.gl_pathc = 0; if (search_in_include_path(filename)) { for (i = 0; i < INCLUDE_PATHS_MAX; i++) { if (include_paths[i] == NULL) break; snprintf(buf, sizeof(buf), "%s/%s", include_paths[i], filename); - f = fopen(buf, "r"); - if (f != NULL) + if (glob(buf, 0, NULL, &globbuf) != 0) { break; + } } } else { - f = fopen(filename, "r"); - name = filename; + glob(filename, 0, NULL, &globbuf); } - if (f == NULL) { - erec = error(loc, "Could not open file \"%s\": %s", - filename, strerror(errno)); + if (globbuf.gl_pathc == 0) { + erec = error(loc, "Could not find file matching \"%s\"\n", filename); goto err; } - erec = scanner_push_file(scanner, name, f, loc); - if (erec != NULL) - goto err; + for (i = 0; i < globbuf.gl_pathc; i++) { + const char *name = globbuf.gl_pathv[i]; + FILE *f = fopen(name, "r"); + if (f == NULL) { + erec = error(loc, "Could not open file \"%s\": %s\n", name, strerror(errno)); + goto err; + } + erec = scanner_push_file(scanner, name, f, loc); + if (erec != NULL) { + goto err; + } + } + + globfree(&globbuf); return 0; err: + globfree(&globbuf); erec_queue(erec, state->msgs); return -1; } diff --git a/tests/shell/testcases/include/0005glob_0 b/tests/shell/testcases/include/0005glob_0 new file mode 100755 index 0000000..99dbf53 --- /dev/null +++ b/tests/shell/testcases/include/0005glob_0 @@ -0,0 +1,32 @@ +#!/bin/bash + +set -e + +tmpdir=$(mktemp -d) +tmpfile=$(mktemp) + +trap "rm -rf $tmpdir $tmpfile" EXIT # cleanup if aborted + +RULESET1="add table x" +RULESET2="add table y" +RULESET3="include \"$tmpdir/*.conf\"" + +echo "$RULESET1" > $tmpdir/ruleset1.conf +echo "$RULESET2" > $tmpdir/ruleset2.conf +echo "$RULESET3" > $tmpfile + +$NFT -f $tmpfile +if [ $? -ne 0 ] ; then + echo "E: unable to load good ruleset" >&2 + exit 1 +fi +$NFT list table x +if [ $? -ne 0 ] ; then + echo "E: unable to include ruleset1.conf" >&2 + exit 1 +fi +$NFT list table y +if [ $? -ne 0 ] ; then + echo "E: unable to include ruleset2.conf" >&2 + exit 1 +fi diff --git a/tests/shell/testcases/include/0006globempty_1 b/tests/shell/testcases/include/0006globempty_1 new file mode 100755 index 0000000..3ac8c72 --- /dev/null +++ b/tests/shell/testcases/include/0006globempty_1 @@ -0,0 +1,14 @@ +#!/bin/bash + +set -e + +tmpdir=$(mktemp -d) +tmpfile=$(mktemp) + +trap "rm -rf $tmpdir $tmpfile" EXIT # cleanup if aborted + +RULESET="include \"$tmpdir/*.conf\"" + +echo "$RULESET" > $tmpfile + +$NFT -f $tmpfile 2>/dev/null