1 /* Code to restore the iptables state, from file by iptables-save.
2 * (C) 2000-2002 by Harald Welte <laforge@gnumonks.org>
3 * based on previous code from Rusty Russell <rusty@linuxcare.com.au>
5 * This code is distributed under the terms of GNU GPL v2
11 #include <sys/errno.h>
17 #include "libiptc/libiptc.h"
18 #include "iptables-multi.h"
21 #define DEBUGP(x, args...) fprintf(stderr, x, ## args)
23 #define DEBUGP(x, args...)
26 static int binary
= 0, counters
= 0, verbose
= 0, noflush
= 0;
28 /* Keeping track of external matches and targets. */
29 static const struct option options
[] = {
30 {.name
= "binary", .has_arg
= false, .val
= 'b'},
31 {.name
= "counters", .has_arg
= false, .val
= 'c'},
32 {.name
= "verbose", .has_arg
= false, .val
= 'v'},
33 {.name
= "test", .has_arg
= false, .val
= 't'},
34 {.name
= "help", .has_arg
= false, .val
= 'h'},
35 {.name
= "noflush", .has_arg
= false, .val
= 'n'},
36 {.name
= "modprobe", .has_arg
= true, .val
= 'M'},
37 {.name
= "table", .has_arg
= true, .val
= 'T'},
41 static void print_usage(const char *name
, const char *version
) __attribute__((noreturn
));
43 static void print_usage(const char *name
, const char *version
)
45 fprintf(stderr
, "Usage: %s [-b] [-c] [-v] [-t] [-h]\n"
52 " [ --table=<TABLE> ]\n"
53 " [ --modprobe=<command>]\n", name
);
58 static struct iptc_handle
*create_handle(const char *tablename
)
60 struct iptc_handle
*handle
;
62 handle
= iptc_init(tablename
);
65 /* try to insmod the module if iptc_init failed */
66 load_xtables_ko(modprobe_program
, 0);
67 handle
= iptc_init(tablename
);
71 exit_error(PARAMETER_PROBLEM
, "%s: unable to initialize "
72 "table '%s'\n", program_name
, tablename
);
78 static int parse_counters(char *string
, struct ipt_counters
*ctr
)
80 unsigned long long pcnt
, bcnt
;
83 ret
= sscanf(string
, "[%llu:%llu]",
84 (unsigned long long *)&pcnt
,
85 (unsigned long long *)&bcnt
);
91 /* global new argv and argc */
92 static char *newargv
[255];
95 /* function adding one argument to newargv, updating newargc
96 * returns true if argument added, false otherwise */
97 static int add_argv(char *what
) {
98 DEBUGP("add_argv: %s\n", what
);
99 if (what
&& ((newargc
+ 1) < sizeof(newargv
)/sizeof(char *))) {
100 newargv
[newargc
] = strdup(what
);
107 static void free_argv(void) {
110 for (i
= 0; i
< newargc
; i
++)
114 #ifdef IPTABLES_MULTI
116 iptables_restore_main(int argc
, char *argv
[])
119 main(int argc
, char *argv
[])
122 struct iptc_handle
*handle
= NULL
;
125 char curtable
[IPT_TABLE_MAXNAMELEN
+ 1];
127 int in_table
= 0, testing
= 0;
128 const char *tablename
= NULL
;
130 program_name
= "iptables-restore";
131 program_version
= XTABLES_VERSION
;
134 lib_dir
= getenv("XTABLES_LIBDIR");
135 if (lib_dir
== NULL
) {
136 lib_dir
= getenv("IPTABLES_LIB_DIR");
138 fprintf(stderr
, "IPTABLES_LIB_DIR is deprecated\n");
141 lib_dir
= XTABLES_LIBDIR
;
143 #ifdef NO_SHARED_LIBS
147 while ((c
= getopt_long(argc
, argv
, "bcvthnM:T:", options
, NULL
)) != -1) {
162 print_usage("iptables-restore",
169 modprobe_program
= optarg
;
177 if (optind
== argc
- 1) {
178 in
= fopen(argv
[optind
], "r");
180 fprintf(stderr
, "Can't open %s: %s\n", argv
[optind
],
185 else if (optind
< argc
) {
186 fprintf(stderr
, "Unknown arguments found on commandline\n");
191 /* Grab standard input. */
192 while (fgets(buffer
, sizeof(buffer
), in
)) {
196 if (buffer
[0] == '\n')
198 else if (buffer
[0] == '#') {
200 fputs(buffer
, stdout
);
202 } else if ((strcmp(buffer
, "COMMIT\n") == 0) && (in_table
)) {
204 DEBUGP("Calling commit\n");
205 ret
= iptc_commit(handle
);
209 DEBUGP("Not calling commit, testing\n");
213 } else if ((buffer
[0] == '*') && (!in_table
)) {
217 table
= strtok(buffer
+1, " \t\n");
218 DEBUGP("line %u, table '%s'\n", line
, table
);
220 exit_error(PARAMETER_PROBLEM
,
221 "%s: line %u table name invalid\n",
225 strncpy(curtable
, table
, IPT_TABLE_MAXNAMELEN
);
226 curtable
[IPT_TABLE_MAXNAMELEN
] = '\0';
228 if (tablename
&& (strcmp(tablename
, table
) != 0))
233 handle
= create_handle(table
);
235 DEBUGP("Cleaning all chains of table '%s'\n",
237 for_each_chain(flush_entries
, verbose
, 1,
240 DEBUGP("Deleting all user-defined chains "
241 "of table '%s'\n", table
);
242 for_each_chain(delete_chain
, verbose
, 0,
249 } else if ((buffer
[0] == ':') && (in_table
)) {
251 char *policy
, *chain
;
253 chain
= strtok(buffer
+1, " \t\n");
254 DEBUGP("line %u, chain '%s'\n", line
, chain
);
256 exit_error(PARAMETER_PROBLEM
,
257 "%s: line %u chain name invalid\n",
262 if (iptc_builtin(chain
, handle
) <= 0) {
263 if (noflush
&& iptc_is_chain(chain
, handle
)) {
264 DEBUGP("Flushing existing user defined chain '%s'\n", chain
);
265 if (!iptc_flush_entries(chain
, handle
))
266 exit_error(PARAMETER_PROBLEM
,
267 "error flushing chain "
271 DEBUGP("Creating new chain '%s'\n", chain
);
272 if (!iptc_create_chain(chain
, handle
))
273 exit_error(PARAMETER_PROBLEM
,
274 "error creating chain "
280 policy
= strtok(NULL
, " \t\n");
281 DEBUGP("line %u, policy '%s'\n", line
, policy
);
283 exit_error(PARAMETER_PROBLEM
,
284 "%s: line %u policy invalid\n",
289 if (strcmp(policy
, "-") != 0) {
290 struct ipt_counters count
;
294 ctrs
= strtok(NULL
, " \t\n");
296 if (!ctrs
|| !parse_counters(ctrs
, &count
))
297 exit_error(PARAMETER_PROBLEM
,
298 "invalid policy counters "
299 "for chain '%s'\n", chain
);
303 sizeof(struct ipt_counters
));
306 DEBUGP("Setting policy of chain %s to %s\n",
309 if (!iptc_set_policy(chain
, policy
, &count
,
311 exit_error(OTHER_PROBLEM
,
312 "Can't set policy `%s'"
313 " on `%s' line %u: %s\n",
315 iptc_strerror(errno
));
320 } else if (in_table
) {
329 int quote_open
, escaped
;
332 /* reset the newargv */
335 if (buffer
[0] == '[') {
336 /* we have counters in our input */
337 ptr
= strchr(buffer
, ']');
339 exit_error(PARAMETER_PROBLEM
,
340 "Bad line %u: need ]\n",
343 pcnt
= strtok(buffer
+1, ":");
345 exit_error(PARAMETER_PROBLEM
,
346 "Bad line %u: need :\n",
349 bcnt
= strtok(NULL
, "]");
351 exit_error(PARAMETER_PROBLEM
,
352 "Bad line %u: need ]\n",
355 /* start command parsing after counter */
356 parsestart
= ptr
+ 1;
358 /* start command parsing at start of line */
364 add_argv((char *) &curtable
);
366 if (counters
&& pcnt
&& bcnt
) {
367 add_argv("--set-counters");
368 add_argv((char *) pcnt
);
369 add_argv((char *) bcnt
);
372 /* After fighting with strtok enough, here's now
373 * a 'real' parser. According to Rusty I'm now no
374 * longer a real hacker, but I can live with that */
380 for (curchar
= parsestart
; *curchar
; curchar
++) {
381 char param_buffer
[1024];
385 param_buffer
[param_len
++] = *curchar
;
388 } else if (*curchar
== '\\') {
391 } else if (*curchar
== '"') {
395 param_buffer
[param_len
++] = *curchar
;
399 if (*curchar
== '"') {
407 || * curchar
== '\n') {
413 param_buffer
[param_len
] = '\0';
415 /* check if table name specified */
416 if (!strncmp(param_buffer
, "-t", 2)
417 || !strncmp(param_buffer
, "--table", 8)) {
418 exit_error(PARAMETER_PROBLEM
,
419 "Line %u seems to have a "
420 "-t table option.\n", line
);
424 add_argv(param_buffer
);
427 /* regular character, copy to buffer */
428 param_buffer
[param_len
++] = *curchar
;
430 if (param_len
>= sizeof(param_buffer
))
431 exit_error(PARAMETER_PROBLEM
,
432 "Parameter too long!");
436 DEBUGP("calling do_command(%u, argv, &%s, handle):\n",
439 for (a
= 0; a
< newargc
; a
++)
440 DEBUGP("argv[%u]: %s\n", a
, newargv
[a
]);
442 ret
= do_command(newargc
, newargv
,
443 &newargv
[2], &handle
);
448 if (tablename
&& (strcmp(tablename
, curtable
) != 0))
451 fprintf(stderr
, "%s: line %u failed\n",
457 fprintf(stderr
, "%s: COMMIT expected at line %u\n",
458 program_name
, line
+ 1);