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,v 1.34 2004/05/26 16:04:48 gandalf Exp $
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);
63 handle = iptc_init(tablename);
67 exit_error(PARAMETER_PROBLEM, "%s: unable to initialize"
68 "table '%s'\n", program_name, tablename);
74 int parse_counters(char *string, struct ipt_counters *ctr)
76 return (sscanf(string, "[%llu:%llu]", (unsigned long long *)&ctr->pcnt, (unsigned long long *)&ctr->bcnt) == 2);
79 /* global new argv and argc */
80 static char *newargv[255];
83 /* function adding one argument to newargv, updating newargc
84 * returns true if argument added, false otherwise */
85 static int add_argv(char *what) {
86 DEBUGP("add_argv: %s\n", what);
87 if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) {
88 newargv[newargc] = strdup(what);
95 static void free_argv(void) {
98 for (i = 0; i < newargc; i++)
102 int main(int argc, char *argv[])
104 iptc_handle_t handle = NULL;
107 char curtable[IPT_TABLE_MAXNAMELEN + 1];
109 const char *modprobe = 0;
110 int in_table = 0, testing = 0;
112 program_name = "iptables-restore";
113 program_version = IPTABLES_VERSION;
116 #ifdef NO_SHARED_LIBS
120 while ((c = getopt_long(argc, argv, "bcvthnM:", options, NULL)) != -1) {
135 print_usage("iptables-restore",
147 if (optind == argc - 1) {
148 in = fopen(argv[optind], "r");
150 fprintf(stderr, "Can't open %s: %s", argv[optind],
155 else if (optind < argc) {
156 fprintf(stderr, "Unknown arguments found on commandline");
161 /* Grab standard input. */
162 while (fgets(buffer, sizeof(buffer), in)) {
166 if (buffer[0] == '\n')
168 else if (buffer[0] == '#') {
170 fputs(buffer, stdout);
172 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
174 DEBUGP("Calling commit\n");
175 ret = iptc_commit(&handle);
177 DEBUGP("Not calling commit, testing\n");
181 } else if ((buffer[0] == '*') && (!in_table)) {
185 table = strtok(buffer+1, " \t\n");
186 DEBUGP("line %u, table '%s'\n", line, table);
188 exit_error(PARAMETER_PROBLEM,
189 "%s: line %u table name invalid\n",
193 strncpy(curtable, table, IPT_TABLE_MAXNAMELEN);
194 curtable[IPT_TABLE_MAXNAMELEN] = '\0';
199 handle = create_handle(table, modprobe);
201 DEBUGP("Cleaning all chains of table '%s'\n",
203 for_each_chain(flush_entries, verbose, 1,
206 DEBUGP("Deleting all user-defined chains "
207 "of table '%s'\n", table);
208 for_each_chain(delete_chain, verbose, 0,
215 } else if ((buffer[0] == ':') && (in_table)) {
217 char *policy, *chain;
219 chain = strtok(buffer+1, " \t\n");
220 DEBUGP("line %u, chain '%s'\n", line, chain);
222 exit_error(PARAMETER_PROBLEM,
223 "%s: line %u chain name invalid\n",
228 if (!iptc_builtin(chain, handle)) {
229 DEBUGP("Creating new chain '%s'\n", chain);
230 if (!iptc_create_chain(chain, &handle))
231 exit_error(PARAMETER_PROBLEM,
232 "error creating chain "
237 policy = strtok(NULL, " \t\n");
238 DEBUGP("line %u, policy '%s'\n", line, policy);
240 exit_error(PARAMETER_PROBLEM,
241 "%s: line %u policy invalid\n",
246 if (strcmp(policy, "-") != 0) {
247 struct ipt_counters count;
251 ctrs = strtok(NULL, " \t\n");
253 parse_counters(ctrs, &count);
257 sizeof(struct ipt_counters));
260 DEBUGP("Setting policy of chain %s to %s\n",
263 if (!iptc_set_policy(chain, policy, &count,
265 exit_error(OTHER_PROBLEM,
266 "Can't set policy `%s'"
267 " on `%s' line %u: %s\n",
269 iptc_strerror(errno));
274 } else if (in_table) {
282 char *param_start, *curchar;
285 /* reset the newargv */
288 if (buffer[0] == '[') {
289 /* we have counters in our input */
290 ptr = strchr(buffer, ']');
292 exit_error(PARAMETER_PROBLEM,
293 "Bad line %u: need ]\n",
296 pcnt = strtok(buffer+1, ":");
298 exit_error(PARAMETER_PROBLEM,
299 "Bad line %u: need :\n",
302 bcnt = strtok(NULL, "]");
304 exit_error(PARAMETER_PROBLEM,
305 "Bad line %u: need ]\n",
308 /* start command parsing after counter */
309 parsestart = ptr + 1;
311 /* start command parsing at start of line */
317 add_argv((char *) &curtable);
319 if (counters && pcnt && bcnt) {
320 add_argv("--set-counters");
321 add_argv((char *) pcnt);
322 add_argv((char *) bcnt);
325 /* After fighting with strtok enough, here's now
326 * a 'real' parser. According to Rusty I'm now no
327 * longer a real hacker, but I can live with that */
330 param_start = parsestart;
332 for (curchar = parsestart; *curchar; curchar++) {
333 if (*curchar == '"') {
334 /* quote_open cannot be true if there
335 * was no previous character. Thus,
336 * curchar-1 has to be within bounds */
338 *(curchar-1) != '\\') {
348 || * curchar == '\n') {
349 char param_buffer[1024];
350 int param_len = curchar-param_start;
361 /* end of one parameter */
362 strncpy(param_buffer, param_start,
364 *(param_buffer+param_len) = '\0';
366 /* check if table name specified */
367 if (!strncmp(param_buffer, "-t", 3)
368 || !strncmp(param_buffer, "--table", 8)) {
369 exit_error(PARAMETER_PROBLEM,
370 "Line %u seems to have a "
371 "-t table option.\n", line);
375 add_argv(param_buffer);
376 param_start += param_len + 1;
378 /* regular character, skip */
382 DEBUGP("calling do_command(%u, argv, &%s, handle):\n",
385 for (a = 0; a < newargc; a++)
386 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
388 ret = do_command(newargc, newargv,
389 &newargv[2], &handle);
394 fprintf(stderr, "%s: line %u failed\n",
400 fprintf(stderr, "%s: COMMIT expected at line %u\n",
401 program_name, line + 1);