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.26 2003/05/02 15:30:11 laforge 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", 1, 0, 'v' },
31 { "help", 0, 0, 'h' },
32 { "noflush", 0, 0, 'n'},
33 { "modprobe", 1, 0, 'M'},
37 static void print_usage(const char *name, const char *version) __attribute__((noreturn));
39 static void print_usage(const char *name, const char *version)
41 fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-h]\n"
47 " [ --modprobe=<command>]\n", name);
52 iptc_handle_t create_handle(const char *tablename, const char* modprobe )
56 handle = iptc_init(tablename);
59 /* try to insmod the module if iptc_init failed */
60 iptables_insmod("ip_tables", modprobe);
61 handle = iptc_init(tablename);
65 exit_error(PARAMETER_PROBLEM, "%s: unable to initialize"
66 "table '%s'\n", program_name, tablename);
72 int parse_counters(char *string, struct ipt_counters *ctr)
74 return (sscanf(string, "[%llu:%llu]", &ctr->pcnt, &ctr->bcnt) == 2);
77 /* global new argv and argc */
78 static char *newargv[255];
81 /* function adding one argument to newargv, updating newargc
82 * returns true if argument added, false otherwise */
83 static int add_argv(char *what) {
84 DEBUGP("add_argv: %s\n", what);
85 if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) {
86 newargv[newargc] = strdup(what);
93 static void free_argv(void) {
96 for (i = 0; i < newargc; i++)
100 int main(int argc, char *argv[])
102 iptc_handle_t handle = NULL;
105 char curtable[IPT_TABLE_MAXNAMELEN + 1];
107 const char *modprobe = 0;
110 program_name = "iptables-restore";
111 program_version = IPTABLES_VERSION;
114 #ifdef NO_SHARED_LIBS
118 while ((c = getopt_long(argc, argv, "bcvhnM:", options, NULL)) != -1) {
130 print_usage("iptables-restore",
142 if (optind == argc - 1) {
143 in = fopen(argv[optind], "r");
145 fprintf(stderr, "Can't open %s: %s", argv[optind],
150 else if (optind < argc) {
151 fprintf(stderr, "Unknown arguments found on commandline");
156 /* Grab standard input. */
157 while (fgets(buffer, sizeof(buffer), in)) {
161 if (buffer[0] == '\n') continue;
162 else if (buffer[0] == '#') {
163 if (verbose) fputs(buffer, stdout);
165 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
166 DEBUGP("Calling commit\n");
167 ret = iptc_commit(&handle);
169 } else if ((buffer[0] == '*') && (!in_table)) {
173 table = strtok(buffer+1, " \t\n");
174 DEBUGP("line %u, table '%s'\n", line, table);
176 exit_error(PARAMETER_PROBLEM,
177 "%s: line %u table name invalid\n",
181 strncpy(curtable, table, IPT_TABLE_MAXNAMELEN);
186 handle = create_handle(table, modprobe);
188 DEBUGP("Cleaning all chains of table '%s'\n",
190 for_each_chain(flush_entries, verbose, 1,
193 DEBUGP("Deleting all user-defined chains "
194 "of table '%s'\n", table);
195 for_each_chain(delete_chain, verbose, 0,
202 } else if ((buffer[0] == ':') && (in_table)) {
204 char *policy, *chain;
206 chain = strtok(buffer+1, " \t\n");
207 DEBUGP("line %u, chain '%s'\n", line, chain);
209 exit_error(PARAMETER_PROBLEM,
210 "%s: line %u chain name invalid\n",
215 if (!iptc_builtin(chain, handle)) {
216 DEBUGP("Creating new chain '%s'\n", chain);
217 if (!iptc_create_chain(chain, &handle))
218 exit_error(PARAMETER_PROBLEM,
219 "error creating chain "
224 policy = strtok(NULL, " \t\n");
225 DEBUGP("line %u, policy '%s'\n", line, policy);
227 exit_error(PARAMETER_PROBLEM,
228 "%s: line %u policy invalid\n",
233 if (strcmp(policy, "-") != 0) {
234 struct ipt_counters count;
238 ctrs = strtok(NULL, " \t\n");
240 parse_counters(ctrs, &count);
244 sizeof(struct ipt_counters));
247 DEBUGP("Setting policy of chain %s to %s\n",
250 if (!iptc_set_policy(chain, policy, &count,
252 exit_error(OTHER_PROBLEM,
253 "Can't set policy `%s'"
254 " on `%s' line %u: %s\n",
256 iptc_strerror(errno));
261 } else if (in_table) {
269 char *param_start, *curchar;
272 /* reset the newargv */
275 if (buffer[0] == '[') {
276 /* we have counters in our input */
277 ptr = strchr(buffer, ']');
279 exit_error(PARAMETER_PROBLEM,
280 "Bad line %u: need ]\n",
283 pcnt = strtok(buffer+1, ":");
285 exit_error(PARAMETER_PROBLEM,
286 "Bad line %u: need :\n",
289 bcnt = strtok(NULL, "]");
291 exit_error(PARAMETER_PROBLEM,
292 "Bad line %u: need ]\n",
295 /* start command parsing after counter */
296 parsestart = ptr + 1;
298 /* start command parsing at start of line */
304 add_argv((char *) &curtable);
306 if (counters && pcnt && bcnt) {
307 add_argv("--set-counters");
308 add_argv((char *) pcnt);
309 add_argv((char *) bcnt);
312 /* After fighting with strtok enough, here's now
313 * a 'real' parser. According to Rusty I'm now no
314 * longer a real hacker, but I can live with that */
317 param_start = parsestart;
319 for (curchar = parsestart; *curchar; curchar++) {
320 if (*curchar == '"') {
331 || * curchar == '\n') {
332 char param_buffer[1024];
333 int param_len = curchar-param_start;
344 /* end of one parameter */
345 strncpy(param_buffer, param_start,
347 *(param_buffer+param_len) = '\0';
349 /* check if table name specified */
350 if (!strncmp(param_buffer, "-t", 3)
351 || !strncmp(param_buffer, "--table", 8)) {
352 exit_error(PARAMETER_PROBLEM,
353 "Line %u seems to have a "
354 "-t table option.\n", line);
358 add_argv(param_buffer);
359 param_start += param_len + 1;
361 /* regular character, skip */
365 DEBUGP("calling do_command(%u, argv, &%s, handle):\n",
368 for (a = 0; a < newargc; a++)
369 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
371 ret = do_command(newargc, newargv,
372 &newargv[2], &handle);
377 fprintf(stderr, "%s: line %u failed\n",