blob: cc49c738b2ced53f4cf273ada89f1799dc005e62 [file] [log] [blame]
Harald Welteae1ff9f2000-12-01 14:26:20 +00001/* Code to restore the iptables state, from file by iptables-save.
Harald Welte10a907f2002-08-07 09:07:41 +00002 * (C) 2000-2002 by Harald Welte <laforge@gnumonks.org>
Harald Welteae1ff9f2000-12-01 14:26:20 +00003 * based on previous code from Rusty Russell <rusty@linuxcare.com.au>
4 *
Harald Welte10a907f2002-08-07 09:07:41 +00005 * This code is distributed under the terms of GNU GPL v2
Harald Welteccd49e52001-01-23 22:54:34 +00006 *
Martin Josefsson357d59d2004-12-27 19:49:28 +00007 * $Id$
Harald Welteae1ff9f2000-12-01 14:26:20 +00008 */
9
Marc Bouchere6869a82000-03-20 06:03:29 +000010#include <getopt.h>
11#include <sys/errno.h>
12#include <string.h>
13#include <stdio.h>
Harald Welteae1ff9f2000-12-01 14:26:20 +000014#include <stdlib.h>
15#include "iptables.h"
16#include "libiptc/libiptc.h"
17
18#ifdef DEBUG
Harald Weltec45c3152000-12-06 08:11:24 +000019#define DEBUGP(x, args...) fprintf(stderr, x, ## args)
Harald Welteae1ff9f2000-12-01 14:26:20 +000020#else
Harald Weltec45c3152000-12-06 08:11:24 +000021#define DEBUGP(x, args...)
Harald Welteae1ff9f2000-12-01 14:26:20 +000022#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000023
Harald Weltea9d27082000-12-19 16:10:42 +000024static int binary = 0, counters = 0, verbose = 0, noflush = 0;
25
Marc Bouchere6869a82000-03-20 06:03:29 +000026/* Keeping track of external matches and targets. */
27static struct option options[] = {
Harald Weltea9d27082000-12-19 16:10:42 +000028 { "binary", 0, 0, 'b' },
29 { "counters", 0, 0, 'c' },
Martin Josefssonec789712004-01-31 19:28:13 +000030 { "verbose", 0, 0, 'v' },
Martin Josefssonbb2f68a2004-02-01 22:03:27 +000031 { "test", 0, 0, 't' },
Harald Weltea9d27082000-12-19 16:10:42 +000032 { "help", 0, 0, 'h' },
33 { "noflush", 0, 0, 'n'},
Harald Welte58918652001-06-16 18:25:25 +000034 { "modprobe", 1, 0, 'M'},
Marc Bouchere6869a82000-03-20 06:03:29 +000035 { 0 }
36};
37
38static void print_usage(const char *name, const char *version) __attribute__((noreturn));
39
40static void print_usage(const char *name, const char *version)
41{
Martin Josefssonbb2f68a2004-02-01 22:03:27 +000042 fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-t] [-h]\n"
Harald Weltea9d27082000-12-19 16:10:42 +000043 " [ --binary ]\n"
44 " [ --counters ]\n"
45 " [ --verbose ]\n"
Martin Josefssonbb2f68a2004-02-01 22:03:27 +000046 " [ --test ]\n"
Harald Weltea9d27082000-12-19 16:10:42 +000047 " [ --help ]\n"
Harald Welte58918652001-06-16 18:25:25 +000048 " [ --noflush ]\n"
49 " [ --modprobe=<command>]\n", name);
Harald Weltea9d27082000-12-19 16:10:42 +000050
Marc Bouchere6869a82000-03-20 06:03:29 +000051 exit(1);
52}
53
Harald Welte58918652001-06-16 18:25:25 +000054iptc_handle_t create_handle(const char *tablename, const char* modprobe )
Marc Bouchere6869a82000-03-20 06:03:29 +000055{
Harald Welteae1ff9f2000-12-01 14:26:20 +000056 iptc_handle_t handle;
Marc Bouchere6869a82000-03-20 06:03:29 +000057
Harald Welteae1ff9f2000-12-01 14:26:20 +000058 handle = iptc_init(tablename);
Harald Welte58918652001-06-16 18:25:25 +000059
60 if (!handle) {
61 /* try to insmod the module if iptc_init failed */
62 iptables_insmod("ip_tables", modprobe);
63 handle = iptc_init(tablename);
64 }
65
Harald Welteae1ff9f2000-12-01 14:26:20 +000066 if (!handle) {
67 exit_error(PARAMETER_PROBLEM, "%s: unable to initialize"
68 "table '%s'\n", program_name, tablename);
69 exit(1);
Marc Bouchere6869a82000-03-20 06:03:29 +000070 }
Harald Welteae1ff9f2000-12-01 14:26:20 +000071 return handle;
Marc Bouchere6869a82000-03-20 06:03:29 +000072}
73
Harald Welted8e65632001-01-05 15:20:07 +000074int parse_counters(char *string, struct ipt_counters *ctr)
75{
Martin Josefssona28d4952004-05-26 16:04:48 +000076 return (sscanf(string, "[%llu:%llu]", (unsigned long long *)&ctr->pcnt, (unsigned long long *)&ctr->bcnt) == 2);
Harald Welted8e65632001-01-05 15:20:07 +000077}
Harald Welteae1ff9f2000-12-01 14:26:20 +000078
Harald Welte9f7fa492001-03-15 15:12:02 +000079/* global new argv and argc */
Harald Welte26e643f2001-05-03 20:50:03 +000080static char *newargv[255];
Harald Welte9f7fa492001-03-15 15:12:02 +000081static int newargc;
82
83/* function adding one argument to newargv, updating newargc
84 * returns true if argument added, false otherwise */
85static int add_argv(char *what) {
Harald Weltebb41f882001-10-21 14:11:54 +000086 DEBUGP("add_argv: %s\n", what);
Harald Welte9f7fa492001-03-15 15:12:02 +000087 if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) {
Harald Welte26e643f2001-05-03 20:50:03 +000088 newargv[newargc] = strdup(what);
Harald Welte9f7fa492001-03-15 15:12:02 +000089 newargc++;
90 return 1;
91 } else
92 return 0;
93}
94
Harald Welte26e643f2001-05-03 20:50:03 +000095static void free_argv(void) {
96 int i;
97
98 for (i = 0; i < newargc; i++)
99 free(newargv[i]);
100}
101
Bastiaan Bakker4e3771f2004-06-25 11:18:57 +0000102#ifdef IPTABLES_MULTI
103int
104iptables_restore_main(int argc, char *argv[])
105#else
106int
107main(int argc, char *argv[])
108#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000109{
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000110 iptc_handle_t handle = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000111 char buffer[10240];
Harald Weltea9d27082000-12-19 16:10:42 +0000112 int c;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000113 char curtable[IPT_TABLE_MAXNAMELEN + 1];
Marc Bouchere6869a82000-03-20 06:03:29 +0000114 FILE *in;
Harald Welte58918652001-06-16 18:25:25 +0000115 const char *modprobe = 0;
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000116 int in_table = 0, testing = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000117
118 program_name = "iptables-restore";
Harald Welte80fe35d2002-05-29 13:08:15 +0000119 program_version = IPTABLES_VERSION;
Illes Marci63e90632003-03-03 08:08:37 +0000120 line = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000121
Martin Josefsson357d59d2004-12-27 19:49:28 +0000122 lib_dir = getenv("IPTABLES_LIB_DIR");
123 if (!lib_dir)
124 lib_dir = IPT_LIB_DIR;
125
Harald Welte3efb6ea2001-08-06 18:50:21 +0000126#ifdef NO_SHARED_LIBS
127 init_extensions();
128#endif
129
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000130 while ((c = getopt_long(argc, argv, "bcvthnM:", options, NULL)) != -1) {
Harald Weltea9d27082000-12-19 16:10:42 +0000131 switch (c) {
132 case 'b':
133 binary = 1;
134 break;
135 case 'c':
136 counters = 1;
137 break;
Marc Boucher1277b592001-12-06 15:06:34 +0000138 case 'v':
139 verbose = 1;
140 break;
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000141 case 't':
142 testing = 1;
143 break;
Harald Weltea9d27082000-12-19 16:10:42 +0000144 case 'h':
145 print_usage("iptables-restore",
Harald Welte80fe35d2002-05-29 13:08:15 +0000146 IPTABLES_VERSION);
Harald Weltea9d27082000-12-19 16:10:42 +0000147 break;
148 case 'n':
149 noflush = 1;
150 break;
Harald Welte58918652001-06-16 18:25:25 +0000151 case 'M':
152 modprobe = optarg;
153 break;
Harald Weltea9d27082000-12-19 16:10:42 +0000154 }
155 }
156
Marc Bouchere6869a82000-03-20 06:03:29 +0000157 if (optind == argc - 1) {
158 in = fopen(argv[optind], "r");
159 if (!in) {
160 fprintf(stderr, "Can't open %s: %s", argv[optind],
161 strerror(errno));
162 exit(1);
163 }
164 }
165 else if (optind < argc) {
166 fprintf(stderr, "Unknown arguments found on commandline");
167 exit(1);
168 }
169 else in = stdin;
Harald Welte9f7fa492001-03-15 15:12:02 +0000170
Marc Bouchere6869a82000-03-20 06:03:29 +0000171 /* Grab standard input. */
172 while (fgets(buffer, sizeof(buffer), in)) {
Harald Welteb3f22192003-03-06 11:56:31 +0000173 int ret = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000174
175 line++;
Martin Josefsson5065afa2004-01-31 19:33:47 +0000176 if (buffer[0] == '\n')
177 continue;
Marc Bouchere6869a82000-03-20 06:03:29 +0000178 else if (buffer[0] == '#') {
Martin Josefsson3673dcb2004-01-31 19:41:49 +0000179 if (verbose)
180 fputs(buffer, stdout);
Marc Bouchere6869a82000-03-20 06:03:29 +0000181 continue;
Illes Marci26100fa2003-03-03 08:05:07 +0000182 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000183 if (!testing) {
184 DEBUGP("Calling commit\n");
185 ret = iptc_commit(&handle);
186 } else {
187 DEBUGP("Not calling commit, testing\n");
188 ret = 1;
189 }
Illes Marci26100fa2003-03-03 08:05:07 +0000190 in_table = 0;
Harald Welteb3f22192003-03-06 11:56:31 +0000191 } else if ((buffer[0] == '*') && (!in_table)) {
Harald Welteae1ff9f2000-12-01 14:26:20 +0000192 /* New table */
193 char *table;
194
195 table = strtok(buffer+1, " \t\n");
196 DEBUGP("line %u, table '%s'\n", line, table);
197 if (!table) {
198 exit_error(PARAMETER_PROBLEM,
199 "%s: line %u table name invalid\n",
200 program_name, line);
201 exit(1);
202 }
203 strncpy(curtable, table, IPT_TABLE_MAXNAMELEN);
Karsten Desler073df8f2004-01-31 15:33:55 +0000204 curtable[IPT_TABLE_MAXNAMELEN] = '\0';
Harald Welteae1ff9f2000-12-01 14:26:20 +0000205
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000206 if (handle)
207 iptc_free(&handle);
208
Harald Welte58918652001-06-16 18:25:25 +0000209 handle = create_handle(table, modprobe);
Harald Weltea9d27082000-12-19 16:10:42 +0000210 if (noflush == 0) {
211 DEBUGP("Cleaning all chains of table '%s'\n",
212 table);
213 for_each_chain(flush_entries, verbose, 1,
214 &handle);
215
216 DEBUGP("Deleting all user-defined chains "
217 "of table '%s'\n", table);
218 for_each_chain(delete_chain, verbose, 0,
219 &handle) ;
220 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000221
222 ret = 1;
Illes Marci26100fa2003-03-03 08:05:07 +0000223 in_table = 1;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000224
Illes Marci26100fa2003-03-03 08:05:07 +0000225 } else if ((buffer[0] == ':') && (in_table)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000226 /* New chain. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000227 char *policy, *chain;
Marc Bouchere6869a82000-03-20 06:03:29 +0000228
Marc Bouchere6869a82000-03-20 06:03:29 +0000229 chain = strtok(buffer+1, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000230 DEBUGP("line %u, chain '%s'\n", line, chain);
Marc Bouchere6869a82000-03-20 06:03:29 +0000231 if (!chain) {
232 exit_error(PARAMETER_PROBLEM,
233 "%s: line %u chain name invalid\n",
234 program_name, line);
235 exit(1);
236 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000237
Harald Welte3a506ac2004-08-30 16:00:09 +0000238 if (iptc_builtin(chain, handle) <= 0) {
Charlie Brady595e4932005-06-12 15:54:15 +0000239 if (noflush && iptc_is_chain(chain, handle)) {
240 DEBUGP("Flushing existing user defined chain '%s'\n", chain);
241 if (!iptc_flush_entries(chain, &handle))
242 exit_error(PARAMETER_PROBLEM,
243 "error flushing chain "
244 "'%s':%s\n", chain,
245 strerror(errno));
246 } else {
247 DEBUGP("Creating new chain '%s'\n", chain);
248 if (!iptc_create_chain(chain, &handle))
249 exit_error(PARAMETER_PROBLEM,
250 "error creating chain "
251 "'%s':%s\n", chain,
252 strerror(errno));
253 }
Harald Welte9f7fa492001-03-15 15:12:02 +0000254 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000255
Marc Bouchere6869a82000-03-20 06:03:29 +0000256 policy = strtok(NULL, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000257 DEBUGP("line %u, policy '%s'\n", line, policy);
Marc Bouchere6869a82000-03-20 06:03:29 +0000258 if (!policy) {
259 exit_error(PARAMETER_PROBLEM,
260 "%s: line %u policy invalid\n",
261 program_name, line);
262 exit(1);
263 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000264
265 if (strcmp(policy, "-") != 0) {
Harald Welted8e65632001-01-05 15:20:07 +0000266 struct ipt_counters count;
267
268 if (counters) {
269 char *ctrs;
270 ctrs = strtok(NULL, " \t\n");
271
Harald Welte6f38a302006-02-09 14:35:38 +0000272 if (!ctrs || !parse_counters(ctrs, &count))
273 exit_error(PARAMETER_PROBLEM,
274 "invalid policy counters "
275 "for chain '%s'\n", chain);
Harald Welted8e65632001-01-05 15:20:07 +0000276
277 } else {
278 memset(&count, 0,
279 sizeof(struct ipt_counters));
280 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000281
282 DEBUGP("Setting policy of chain %s to %s\n",
283 chain, policy);
284
Harald Welted8e65632001-01-05 15:20:07 +0000285 if (!iptc_set_policy(chain, policy, &count,
286 &handle))
Harald Welteae1ff9f2000-12-01 14:26:20 +0000287 exit_error(OTHER_PROBLEM,
288 "Can't set policy `%s'"
289 " on `%s' line %u: %s\n",
290 chain, policy, line,
291 iptc_strerror(errno));
292 }
293
294 ret = 1;
295
Illes Marci26100fa2003-03-03 08:05:07 +0000296 } else if (in_table) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000297 int a;
Marc Bouchere6869a82000-03-20 06:03:29 +0000298 char *ptr = buffer;
Harald Welteccd49e52001-01-23 22:54:34 +0000299 char *pcnt = NULL;
300 char *bcnt = NULL;
Harald Welte9f7fa492001-03-15 15:12:02 +0000301 char *parsestart;
302
Harald Welte26e643f2001-05-03 20:50:03 +0000303 /* the parser */
304 char *param_start, *curchar;
305 int quote_open;
306
Harald Welte9f7fa492001-03-15 15:12:02 +0000307 /* reset the newargv */
308 newargc = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000309
Marc Bouchere6869a82000-03-20 06:03:29 +0000310 if (buffer[0] == '[') {
Harald Welte9f7fa492001-03-15 15:12:02 +0000311 /* we have counters in our input */
Marc Bouchere6869a82000-03-20 06:03:29 +0000312 ptr = strchr(buffer, ']');
313 if (!ptr)
314 exit_error(PARAMETER_PROBLEM,
315 "Bad line %u: need ]\n",
316 line);
Harald Welte9f7fa492001-03-15 15:12:02 +0000317
Harald Welteccd49e52001-01-23 22:54:34 +0000318 pcnt = strtok(buffer+1, ":");
Harald Welte9f7fa492001-03-15 15:12:02 +0000319 if (!pcnt)
320 exit_error(PARAMETER_PROBLEM,
321 "Bad line %u: need :\n",
322 line);
323
Harald Welteccd49e52001-01-23 22:54:34 +0000324 bcnt = strtok(NULL, "]");
Harald Welte9f7fa492001-03-15 15:12:02 +0000325 if (!bcnt)
326 exit_error(PARAMETER_PROBLEM,
327 "Bad line %u: need ]\n",
328 line);
329
330 /* start command parsing after counter */
331 parsestart = ptr + 1;
332 } else {
333 /* start command parsing at start of line */
334 parsestart = buffer;
Marc Bouchere6869a82000-03-20 06:03:29 +0000335 }
Rusty Russell7e53bf92000-03-20 07:03:28 +0000336
Harald Welte9f7fa492001-03-15 15:12:02 +0000337 add_argv(argv[0]);
338 add_argv("-t");
339 add_argv((char *) &curtable);
340
Harald Welteccd49e52001-01-23 22:54:34 +0000341 if (counters && pcnt && bcnt) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000342 add_argv("--set-counters");
343 add_argv((char *) pcnt);
344 add_argv((char *) bcnt);
Harald Welteccd49e52001-01-23 22:54:34 +0000345 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000346
Harald Welte26e643f2001-05-03 20:50:03 +0000347 /* After fighting with strtok enough, here's now
348 * a 'real' parser. According to Rusty I'm now no
349 * longer a real hacker, but I can live with that */
350
351 quote_open = 0;
352 param_start = parsestart;
353
354 for (curchar = parsestart; *curchar; curchar++) {
355 if (*curchar == '"') {
Michael Rash29cdbd02004-01-05 09:41:50 +0000356 /* quote_open cannot be true if there
357 * was no previous character. Thus,
358 * curchar-1 has to be within bounds */
359 if (quote_open &&
360 *(curchar-1) != '\\') {
Harald Welte26e643f2001-05-03 20:50:03 +0000361 quote_open = 0;
362 *curchar = ' ';
363 } else {
364 quote_open = 1;
365 param_start++;
366 }
367 }
368 if (*curchar == ' '
369 || *curchar == '\t'
370 || * curchar == '\n') {
371 char param_buffer[1024];
372 int param_len = curchar-param_start;
373
374 if (quote_open)
375 continue;
376
Harald Welte974d0102001-05-26 04:41:56 +0000377 if (!param_len) {
378 /* two spaces? */
379 param_start++;
380 continue;
381 }
Harald Welte26e643f2001-05-03 20:50:03 +0000382
383 /* end of one parameter */
384 strncpy(param_buffer, param_start,
385 param_len);
386 *(param_buffer+param_len) = '\0';
Harald Weltebb41f882001-10-21 14:11:54 +0000387
388 /* check if table name specified */
Harald Welte48a6f902001-10-22 15:16:21 +0000389 if (!strncmp(param_buffer, "-t", 3)
390 || !strncmp(param_buffer, "--table", 8)) {
Harald Weltebb41f882001-10-21 14:11:54 +0000391 exit_error(PARAMETER_PROBLEM,
392 "Line %u seems to have a "
393 "-t table option.\n", line);
394 exit(1);
395 }
396
Harald Welte26e643f2001-05-03 20:50:03 +0000397 add_argv(param_buffer);
398 param_start += param_len + 1;
399 } else {
400 /* regular character, skip */
401 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000402 }
403
Harald Welte26e643f2001-05-03 20:50:03 +0000404 DEBUGP("calling do_command(%u, argv, &%s, handle):\n",
405 newargc, curtable);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000406
Harald Weltefa48fc82001-10-16 09:51:33 +0000407 for (a = 0; a < newargc; a++)
Harald Welteae1ff9f2000-12-01 14:26:20 +0000408 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
409
Harald Welte26e643f2001-05-03 20:50:03 +0000410 ret = do_command(newargc, newargv,
411 &newargv[2], &handle);
412
413 free_argv();
Marc Bouchere6869a82000-03-20 06:03:29 +0000414 }
415 if (!ret) {
416 fprintf(stderr, "%s: line %u failed\n",
417 program_name, line);
418 exit(1);
419 }
420 }
Martin Josefsson3229a7c2004-02-01 21:46:04 +0000421 if (in_table) {
422 fprintf(stderr, "%s: COMMIT expected at line %u\n",
423 program_name, line + 1);
424 exit(1);
425 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000426
427 return 0;
428}