From 4d6045630bb90182abf553df1b7f2764a24620b0 Mon Sep 17 00:00:00 2001 From: Arturo Borrero Gonzalez Date: Wed, 22 May 2013 00:33:25 +0000 Subject: examples: XML parsing examples Some code snipplets to add tables/chain/rules using the XML representation. The examples contains: * A binary to parse/add the object using libnftables. * A shellscript to easily call that binary, doing some tests. * table/chain/rule sample XML file. I included my name in new files, but I don't know if this is correct. Please let me know. Instructions: $ cd examples/ ; make nft-table-xml-add # cd test/ ; ./nft-table-xml-add.sh NOTE: Some kernel changes are required to allow reinsert exactly what is printed (handle handling, flags..) Signed-off-by: Arturo Borrero Gonzalez Signed-off-by: Pablo Neira Ayuso --- test/nft-chain-xml-add.sh | 123 ++++++++++++++++++++++++++++++++++++++++++ test/nft-rule-xml-add.sh | 132 ++++++++++++++++++++++++++++++++++++++++++++++ test/nft-table-xml-add.sh | 75 ++++++++++++++++++++++++++ 3 files changed, 330 insertions(+) create mode 100755 test/nft-chain-xml-add.sh create mode 100755 test/nft-rule-xml-add.sh create mode 100755 test/nft-table-xml-add.sh (limited to 'test') diff --git a/test/nft-chain-xml-add.sh b/test/nft-chain-xml-add.sh new file mode 100755 index 0000000..d1bd839 --- /dev/null +++ b/test/nft-chain-xml-add.sh @@ -0,0 +1,123 @@ +#!/bin/bash + +# +# (C) 2013 by Arturo Borrero Gonzalez +# +# 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 is a small testbench for adding nftables chains to kernel +# in XML format. + +BINARY="../examples/nft-chain-xml-add" +NFT=$( which nft ) +MKTEMP=$( which mktemp) +TMPFILE=$( $MKTEMP ) + +if [ ! -x "$BINARY" ] ; then + echo "E: Binary not found $BINARY" + exit 1 +fi + +if [ ! -x "$MKTEMP" ] ; then + echo "E: mktemp not found and is neccesary" + exit 1 +fi + +if [ ! -w "$TMPFILE" ] ; then + echo "E: Unable to create temp file via mktemp" + exit 1 +fi + +[ ! -x "$NFT" ] && echo "W: nftables main binary not found but continuing anyway $NFT" + +XML=" + + filter + filter
+ 0 + 0 + 2 + 1 + 2 +
+
" + +$NFT delete chain ip filter test1 2>/dev/null >&2 +echo $XML > $TMPFILE +if ! $BINARY "$TMPFILE" ; then + echo "E: Unable to add XML:" + echo "$XML" + exit 1 +fi + +# This is valid (as long as the table exist) +XML=" + + filter + filter
+ 1 + 0 + 4 + 1 + 10 +
+
" + +$NFT delete chain ip6 filter test2 2>/dev/null >&2 +echo $XML > $TMPFILE +if ! $BINARY "$TMPFILE" ; then + echo "E: Unable to add XML:" + echo "$XML" + rm -rf $TMPFILE 2>/dev/null + exit 1 +fi + +# This is valid (as long as the table exist) +XML=" + + filter + filter
+ 0 + 0 + 4 + 1 + 2 +
+
" + +$NFT delete chain ip6 filter test3 2>/dev/null >&2 +echo $XML > $TMPFILE +if ! $BINARY "$TMPFILE" ; then + echo "E: Unable to add XML:" + echo "$XML" + rm -rf $TMPFILE 2>/dev/null + exit 1 +fi + +# This is invalid +XML=" + + asdasd + filter + filter
+ asdasd + asdasd + asdasd + asdasd + asdasd +
+
" + +if $BINARY "$XML" 2>/dev/null; then + echo "E: Accepted invalid XML:" + echo "$XML" + rm -rf $TMPFILE 2>/dev/null + exit 1 +fi + +rm -rf $TMPFILE 2>/dev/null +echo "I: Test OK" diff --git a/test/nft-rule-xml-add.sh b/test/nft-rule-xml-add.sh new file mode 100755 index 0000000..426b975 --- /dev/null +++ b/test/nft-rule-xml-add.sh @@ -0,0 +1,132 @@ +#!/bin/bash + +# +# (C) 2013 by Arturo Borrero Gonzalez +# +# 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 is a small testbench for adding nftables rules to kernel +# in XML format. + +BINARY="../examples/nft-rule-xml-add" +NFT="$( which nft )" +MKTEMP="$( which mktemp )" +TMPFILE="$( $MKTEMP )" + +if [ ! -x "$BINARY" ] ; then + echo "E: Binary not found $BINARY" + exit 1 +fi + +if [ ! -x "$MKTEMP" ] ; then + echo "E: mktemp not found. Is mandatory." + exit 1 +fi + +if [ ! -w "$TMPFILE" ] ; then + echo "E: Unable to create tempfile with mktemp" + exit 1 +fi + +[ ! -x "$NFT" ] && echo "W: nftables main binary not found but continuing anyway $NFT" + +XML=" + 0 + 127 + 0 + 0 + + 1 + 4 + + + 1 + eq + + + 1 + 0x04000000 + + + + + 1 + 1 + 12 + 4 + + + 1 + eq + + + 1 + 0x96d60496 + + + + + 1 + 1 + 16 + 4 + + + 1 + eq + + + 1 + 0x96d60329 + + + + + 1 + 1 + 9 + 1 + + + 1 + eq + + + 1 + 0x06000000 + + + + + state + 0 + + + + + 123123 + 321321 + + + LOG + 0 + + + +" + +$NFT add table filter 2>/dev/null >&2 +$NFT add chain filter INPUT 2>/dev/null >&2 + +echo $XML > $TMPFILE +if ! $BINARY "$TMPFILE" ; then + echo "E: Unable to add XML." + rm -rf $TMPFILE 2>/dev/null + exit 1 +fi + +rm -rf $TMPFILE 2>/dev/null +echo "I: Test OK" diff --git a/test/nft-table-xml-add.sh b/test/nft-table-xml-add.sh new file mode 100755 index 0000000..2c55edc --- /dev/null +++ b/test/nft-table-xml-add.sh @@ -0,0 +1,75 @@ +#!/bin/bash + +# +# (C) 2013 by Arturo Borrero Gonzalez +# +# 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 is a small testbench for adding nftables tables to kernel +# in XML format. + +BINARY="../examples/nft-table-xml-add" +NFT="$( which nft )" +MKTEMP="$( which mktemp)" +TMPFILE="$( $MKTEMP )" + +if [ ! -x "$BINARY" ] ; then + echo "E: Binary not found $BINARY" + exit 1 +fi + +if [ ! -x "$MKTEMP" ] ; then + echo "E: mktemp not found and is neccesary" + exit 1 +fi + +if [ ! -w "$TMPFILE" ] ; then + echo "E: Unable to create temp file via mktemp" + exit 1 +fi + + +if [ ! -x "$NFT" ] ; then + echo "W: nftables main binary not found but continuing anyway $NFT" +fi + +# This is valid +XML=" + + 2 + 0 + +
" + +$NFT delete table filter_test 2>/dev/null >&2 +echo $XML > $TMPFILE +if ! $BINARY "$TMPFILE" ; then + echo "E: Unable to add XML:" + echo "$XML" + rm -rf $TMPFILE 2>/dev/null + exit 1 +fi + +# This is valid +XML=" + + 10 + 0 + +
" + +$NFT delete table filter6_test 2>/dev/null >&2 +echo $XML > $TMPFILE +if ! $BINARY "$TMPFILE" ; then + echo "E: Unable to add XML:" + echo "$XML" + rm -rf $TMPFILE 2>/dev/null + exit 1 +fi + +rm -rf $TMPFILE 2>/dev/null +echo "I: Test OK" -- cgit v1.2.3