summaryrefslogtreecommitdiffstats
path: root/iptables/xtables-save.c
diff options
context:
space:
mode:
authorOliver Ford <ojford@gmail.com>2017-05-26 12:25:16 +0000
committerPablo Neira Ayuso <pablo@netfilter.org>2017-05-29 14:00:54 +0200
commit8d994bcf6be09cd0a13d6f22c6e01e98fb130415 (patch)
tree1521c2444d27f05569d282cc775bce1b4688adc2 /iptables/xtables-save.c
parentf8e5ebc5986bffa682ed9e4497e3c19f19bf961e (diff)
iptables: Add file output option to iptables-save
Adds an option to output the results of iptables-save, ip6tables-save, and xtables-save save to a file. Updates the man page with this new option. Uses the dup2 call to replace stdout with the specified file. Error output is unchanged. This is a feature requested by a Gentoo developer in Bugzilla #905. Signed-off-by: Oliver Ford <ojford@gmail.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'iptables/xtables-save.c')
-rw-r--r--iptables/xtables-save.c22
1 files changed, 20 insertions, 2 deletions
diff --git a/iptables/xtables-save.c b/iptables/xtables-save.c
index abd840af..5b498b04 100644
--- a/iptables/xtables-save.c
+++ b/iptables/xtables-save.c
@@ -14,6 +14,7 @@
#include <string.h>
#include <time.h>
#include <netdb.h>
+#include <unistd.h>
#include "libiptc/libiptc.h"
#include "iptables.h"
#include "xtables-multi.h"
@@ -32,6 +33,7 @@ static const struct option options[] = {
{.name = "dump", .has_arg = false, .val = 'd'},
{.name = "table", .has_arg = true, .val = 't'},
{.name = "modprobe", .has_arg = true, .val = 'M'},
+ {.name = "file", .has_arg = true, .val = 'f'},
{.name = "ipv4", .has_arg = false, .val = '4'},
{.name = "ipv6", .has_arg = false, .val = '6'},
{NULL},
@@ -82,7 +84,8 @@ xtables_save_main(int family, const char *progname, int argc, char *argv[])
struct nft_handle h = {
.family = family,
};
- int c;
+ FILE *file = NULL;
+ int ret, c;
xtables_globals.program_name = progname;
c = xtables_init_all(&xtables_globals, family);
@@ -104,7 +107,7 @@ xtables_save_main(int family, const char *progname, int argc, char *argv[])
exit(EXIT_FAILURE);
}
- while ((c = getopt_long(argc, argv, "bcdt:M:46", options, NULL)) != -1) {
+ while ((c = getopt_long(argc, argv, "bcdt:M:f:46", options, NULL)) != -1) {
switch (c) {
case 'b':
fprintf(stderr, "-b/--binary option is not implemented\n");
@@ -120,6 +123,21 @@ xtables_save_main(int family, const char *progname, int argc, char *argv[])
case 'M':
xtables_modprobe_program = optarg;
break;
+ case 'f':
+ file = fopen(optarg, "w");
+ if (file == NULL) {
+ fprintf(stderr, "Failed to open file, error: %s\n",
+ strerror(errno));
+ exit(1);
+ }
+ ret = dup2(fileno(file), STDOUT_FILENO);
+ if (ret == -1) {
+ fprintf(stderr, "Failed to redirect stdout, error: %s\n",
+ strerror(errno));
+ exit(1);
+ }
+ fclose(file);
+ break;
case 'd':
dump = true;
break;