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
7 * $Id: iptables-restore.c 6828 2007-05-10 15:00:39Z /C=EU/ST=EU/CN=Patrick McHardy/emailAddress=kaber@trash.net $
11 #include <sys/errno.h>
16 #include "libiptc/libiptc.h"
19 #define DEBUGP(x, args...) fprintf(stderr, x, ## args)
21 #define DEBUGP(x, args...)
24 static int binary
= 0, counters
= 0, verbose
= 0, noflush
= 0;
26 /* Keeping track of external matches and targets. */
27 static struct option options
[] = {
28 { "binary", 0, 0, 'b' },
29 { "counters", 0, 0, 'c' },
30 { "verbose", 0, 0, 'v' },
31 { "test", 0, 0, 't' },
32 { "help", 0, 0, 'h' },
33 { "noflush", 0, 0, 'n'},
34 { "modprobe", 1, 0, 'M'},
38 static void print_usage(const char *name
, const char *version
) __attribute__((noreturn
));
40 static void print_usage(const char *name
, const char *version
)
42 fprintf(stderr
, "Usage: %s [-b] [-c] [-v] [-t] [-h]\n"
49 " [ --modprobe=<command>]\n", name
);
54 iptc_handle_t
create_handle(const char *tablename
, const char* modprobe
)
58 handle
= iptc_init(tablename
);
61 /* try to insmod the module if iptc_init failed */
62 iptables_insmod("ip_tables", modprobe
, 0);
63 handle
= iptc_init(tablename
);
67 exit_error(PARAMETER_PROBLEM
, "%s: unable to initialize "
68 "table '%s'\n", program_name
, tablename
);
74 static int parse_counters(char *string
, struct ipt_counters
*ctr
)
76 unsigned long long pcnt
, bcnt
;
79 ret
= sscanf(string
, "[%llu:%llu]",
80 (unsigned long long *)&pcnt
,
81 (unsigned long long *)&bcnt
);
87 /* global new argv and argc */
88 static char *newargv
[255];
91 /* function adding one argument to newargv, updating newargc
92 * returns true if argument added, false otherwise */
93 static int add_argv(char *what
) {
94 DEBUGP("add_argv: %s\n", what
);
95 if (what
&& ((newargc
+ 1) < sizeof(newargv
)/sizeof(char *))) {
96 newargv
[newargc
] = strdup(what
);
103 static void free_argv(void) {
106 for (i
= 0; i
< newargc
; i
++)
110 #ifdef IPTABLES_MULTI
112 iptables_restore_main(int argc
, char *argv
[])
115 main(int argc
, char *argv
[])
118 iptc_handle_t handle
= NULL
;
121 char curtable
[IPT_TABLE_MAXNAMELEN
+ 1];
123 const char *modprobe
= 0;
124 int in_table
= 0, testing
= 0;
126 program_name
= "iptables-restore";
127 program_version
= IPTABLES_VERSION
;
130 lib_dir
= getenv("IPTABLES_LIB_DIR");
132 lib_dir
= IPT_LIB_DIR
;
134 #ifdef NO_SHARED_LIBS
138 while ((c
= getopt_long(argc
, argv
, "bcvthnM:", options
, NULL
)) != -1) {
153 print_usage("iptables-restore",
165 if (optind
== argc
- 1) {
166 in
= fopen(argv
[optind
], "r");
168 fprintf(stderr
, "Can't open %s: %s\n", argv
[optind
],
173 else if (optind
< argc
) {
174 fprintf(stderr
, "Unknown arguments found on commandline\n");
179 /* Grab standard input. */
180 while (fgets(buffer
, sizeof(buffer
), in
)) {
184 if (buffer
[0] == '\n')
186 else if (buffer
[0] == '#') {
188 fputs(buffer
, stdout
);
190 } else if ((strcmp(buffer
, "COMMIT\n") == 0) && (in_table
)) {
192 DEBUGP("Calling commit\n");
193 ret
= iptc_commit(&handle
);
195 DEBUGP("Not calling commit, testing\n");
199 } else if ((buffer
[0] == '*') && (!in_table
)) {
203 table
= strtok(buffer
+1, " \t\n");
204 DEBUGP("line %u, table '%s'\n", line
, table
);
206 exit_error(PARAMETER_PROBLEM
,
207 "%s: line %u table name invalid\n",
211 strncpy(curtable
, table
, IPT_TABLE_MAXNAMELEN
);
212 curtable
[IPT_TABLE_MAXNAMELEN
] = '\0';
217 handle
= create_handle(table
, modprobe
);
219 DEBUGP("Cleaning all chains of table '%s'\n",
221 for_each_chain(flush_entries
, verbose
, 1,
224 DEBUGP("Deleting all user-defined chains "
225 "of table '%s'\n", table
);
226 for_each_chain(delete_chain
, verbose
, 0,
233 } else if ((buffer
[0] == ':') && (in_table
)) {
235 char *policy
, *chain
;
237 chain
= strtok(buffer
+1, " \t\n");
238 DEBUGP("line %u, chain '%s'\n", line
, chain
);
240 exit_error(PARAMETER_PROBLEM
,
241 "%s: line %u chain name invalid\n",
246 if (iptc_builtin(chain
, handle
) <= 0) {
247 if (noflush
&& iptc_is_chain(chain
, handle
)) {
248 DEBUGP("Flushing existing user defined chain '%s'\n", chain
);
249 if (!iptc_flush_entries(chain
, &handle
))
250 exit_error(PARAMETER_PROBLEM
,
251 "error flushing chain "
255 DEBUGP("Creating new chain '%s'\n", chain
);
256 if (!iptc_create_chain(chain
, &handle
))
257 exit_error(PARAMETER_PROBLEM
,
258 "error creating chain "
264 policy
= strtok(NULL
, " \t\n");
265 DEBUGP("line %u, policy '%s'\n", line
, policy
);
267 exit_error(PARAMETER_PROBLEM
,
268 "%s: line %u policy invalid\n",
273 if (strcmp(policy
, "-") != 0) {
274 struct ipt_counters count
;
278 ctrs
= strtok(NULL
, " \t\n");
280 if (!ctrs
|| !parse_counters(ctrs
, &count
))
281 exit_error(PARAMETER_PROBLEM
,
282 "invalid policy counters "
283 "for chain '%s'\n", chain
);
287 sizeof(struct ipt_counters
));
290 DEBUGP("Setting policy of chain %s to %s\n",
293 if (!iptc_set_policy(chain
, policy
, &count
,
295 exit_error(OTHER_PROBLEM
,
296 "Can't set policy `%s'"
297 " on `%s' line %u: %s\n",
299 iptc_strerror(errno
));
304 } else if (in_table
) {
313 int quote_open
, escaped
;
316 /* reset the newargv */
319 if (buffer
[0] == '[') {
320 /* we have counters in our input */
321 ptr
= strchr(buffer
, ']');
323 exit_error(PARAMETER_PROBLEM
,
324 "Bad line %u: need ]\n",
327 pcnt
= strtok(buffer
+1, ":");
329 exit_error(PARAMETER_PROBLEM
,
330 "Bad line %u: need :\n",
333 bcnt
= strtok(NULL
, "]");
335 exit_error(PARAMETER_PROBLEM
,
336 "Bad line %u: need ]\n",
339 /* start command parsing after counter */
340 parsestart
= ptr
+ 1;
342 /* start command parsing at start of line */
348 add_argv((char *) &curtable
);
350 if (counters
&& pcnt
&& bcnt
) {
351 add_argv("--set-counters");
352 add_argv((char *) pcnt
);
353 add_argv((char *) bcnt
);
356 /* After fighting with strtok enough, here's now
357 * a 'real' parser. According to Rusty I'm now no
358 * longer a real hacker, but I can live with that */
364 for (curchar
= parsestart
; *curchar
; curchar
++) {
365 char param_buffer
[1024];
369 param_buffer
[param_len
++] = *curchar
;
372 } else if (*curchar
== '\\') {
375 } else if (*curchar
== '"') {
379 param_buffer
[param_len
++] = *curchar
;
383 if (*curchar
== '"') {
391 || * curchar
== '\n') {
397 param_buffer
[param_len
] = '\0';
399 /* check if table name specified */
400 if (!strncmp(param_buffer
, "-t", 2)
401 || !strncmp(param_buffer
, "--table", 8)) {
402 exit_error(PARAMETER_PROBLEM
,
403 "Line %u seems to have a "
404 "-t table option.\n", line
);
408 add_argv(param_buffer
);
411 /* regular character, copy to buffer */
412 param_buffer
[param_len
++] = *curchar
;
414 if (param_len
>= sizeof(param_buffer
))
415 exit_error(PARAMETER_PROBLEM
,
416 "Parameter too long!");
420 DEBUGP("calling do_command(%u, argv, &%s, handle):\n",
423 for (a
= 0; a
< newargc
; a
++)
424 DEBUGP("argv[%u]: %s\n", a
, newargv
[a
]);
426 ret
= do_command(newargc
, newargv
,
427 &newargv
[2], &handle
);
433 fprintf(stderr
, "%s: line %u failed\n",
439 fprintf(stderr
, "%s: COMMIT expected at line %u\n",
440 program_name
, line
+ 1);