blob: 9d01841e37cd726048aa47bc37f8e23bddebe35b [file] [log] [blame]
András Kis-Szabó2f523792001-02-27 09:59:48 +00001/* Code to restore the iptables state, from file by ip6tables-save.
2 * Author: Andras Kis-Szabo <kisza@sch.bme.hu>
3 *
4 * based on iptables-restore
5 * Authors:
6 * Harald Welte <laforge@gnumonks.org>
7 * Rusty Russell <rusty@linuxcare.com.au>
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00008 * This code is distributed under the terms of GNU GPL v2
András Kis-Szabó2f523792001-02-27 09:59:48 +00009 *
Martin Josefsson357d59d2004-12-27 19:49:28 +000010 * $Id$
András Kis-Szabó2f523792001-02-27 09:59:48 +000011 */
12
13#include <getopt.h>
14#include <sys/errno.h>
15#include <string.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include "ip6tables.h"
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000019#include "xtables.h"
András Kis-Szabó2f523792001-02-27 09:59:48 +000020#include "libiptc/libip6tc.h"
21
22#ifdef DEBUG
23#define DEBUGP(x, args...) fprintf(stderr, x, ## args)
24#else
25#define DEBUGP(x, args...)
26#endif
27
András Kis-Szabó2f523792001-02-27 09:59:48 +000028static int binary = 0, counters = 0, verbose = 0, noflush = 0;
29
30/* Keeping track of external matches and targets. */
31static struct option options[] = {
32 { "binary", 0, 0, 'b' },
33 { "counters", 0, 0, 'c' },
Martin Josefssonec789712004-01-31 19:28:13 +000034 { "verbose", 0, 0, 'v' },
Martin Josefsson4e5e29a2004-02-02 19:59:17 +000035 { "test", 0, 0, 't' },
András Kis-Szabó2f523792001-02-27 09:59:48 +000036 { "help", 0, 0, 'h' },
37 { "noflush", 0, 0, 'n'},
Harald Welte58918652001-06-16 18:25:25 +000038 { "modprobe", 1, 0, 'M'},
András Kis-Szabó2f523792001-02-27 09:59:48 +000039 { 0 }
40};
41
42static void print_usage(const char *name, const char *version) __attribute__((noreturn));
43
44static void print_usage(const char *name, const char *version)
45{
Martin Josefsson60044392004-02-02 20:12:33 +000046 fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-t] [-h]\n"
András Kis-Szabó2f523792001-02-27 09:59:48 +000047 " [ --binary ]\n"
48 " [ --counters ]\n"
49 " [ --verbose ]\n"
Martin Josefsson4e5e29a2004-02-02 19:59:17 +000050 " [ --test ]\n"
András Kis-Szabó2f523792001-02-27 09:59:48 +000051 " [ --help ]\n"
Harald Welte58918652001-06-16 18:25:25 +000052 " [ --noflush ]\n"
53 " [ --modprobe=<command>]\n", name);
András Kis-Szabó2f523792001-02-27 09:59:48 +000054
55 exit(1);
56}
57
Harald Welte58918652001-06-16 18:25:25 +000058ip6tc_handle_t create_handle(const char *tablename, const char* modprobe)
András Kis-Szabó2f523792001-02-27 09:59:48 +000059{
60 ip6tc_handle_t handle;
61
62 handle = ip6tc_init(tablename);
Harald Welte58918652001-06-16 18:25:25 +000063
64 if (!handle) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000065 /* try to insmod the module if iptc_init failed */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000066 load_xtables_ko(modprobe, 0);
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000067 handle = ip6tc_init(tablename);
Harald Welte58918652001-06-16 18:25:25 +000068 }
69
András Kis-Szabó2f523792001-02-27 09:59:48 +000070 if (!handle) {
Patrick McHardy5b098ac2007-02-14 13:59:12 +000071 exit_error(PARAMETER_PROBLEM, "%s: unable to initialize "
András Kis-Szabó2f523792001-02-27 09:59:48 +000072 "table '%s'\n", program_name, tablename);
73 exit(1);
74 }
75 return handle;
76}
77
Lutz Jaenickee78c69c2006-12-09 13:06:04 +000078static int parse_counters(char *string, struct ip6t_counters *ctr)
András Kis-Szabó2f523792001-02-27 09:59:48 +000079{
Martin Josefssona28d4952004-05-26 16:04:48 +000080 return (sscanf(string, "[%llu:%llu]", (unsigned long long *)&ctr->pcnt, (unsigned long long *)&ctr->bcnt) == 2);
András Kis-Szabó2f523792001-02-27 09:59:48 +000081}
82
Harald Welte885c6eb2001-10-04 08:30:46 +000083/* global new argv and argc */
84static char *newargv[255];
85static int newargc;
86
87/* function adding one argument to newargv, updating newargc
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000088 * returns true if argument added, false otherwise */
Harald Welte885c6eb2001-10-04 08:30:46 +000089static int add_argv(char *what) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000090 DEBUGP("add_argv: %s\n", what);
91 if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) {
92 newargv[newargc] = strdup(what);
93 newargc++;
94 return 1;
95 } else
96 return 0;
Harald Welte885c6eb2001-10-04 08:30:46 +000097}
98
99static void free_argv(void) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000100 int i;
Harald Welte885c6eb2001-10-04 08:30:46 +0000101
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000102 for (i = 0; i < newargc; i++)
103 free(newargv[i]);
Harald Welte885c6eb2001-10-04 08:30:46 +0000104}
105
András Kis-Szabó2f523792001-02-27 09:59:48 +0000106int main(int argc, char *argv[])
107{
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000108 ip6tc_handle_t handle = NULL;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000109 char buffer[10240];
András Kis-Szabó2f523792001-02-27 09:59:48 +0000110 int c;
111 char curtable[IP6T_TABLE_MAXNAMELEN + 1];
András Kis-Szabó2f523792001-02-27 09:59:48 +0000112 FILE *in;
Harald Welte58918652001-06-16 18:25:25 +0000113 const char *modprobe = 0;
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000114 int in_table = 0, testing = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000115
116 program_name = "ip6tables-restore";
Harald Welte80fe35d2002-05-29 13:08:15 +0000117 program_version = IPTABLES_VERSION;
Harald Weltea8658ca2003-03-05 07:46:15 +0000118 line = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000119
Martin Josefsson357d59d2004-12-27 19:49:28 +0000120 lib_dir = getenv("IP6TABLES_LIB_DIR");
121 if (!lib_dir)
122 lib_dir = IP6T_LIB_DIR;
123
Harald Welte3efb6ea2001-08-06 18:50:21 +0000124#ifdef NO_SHARED_LIBS
125 init_extensions();
126#endif
127
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000128 while ((c = getopt_long(argc, argv, "bcvthnM:", options, NULL)) != -1) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000129 switch (c) {
130 case 'b':
131 binary = 1;
132 break;
133 case 'c':
134 counters = 1;
135 break;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000136 case 'v':
137 verbose = 1;
138 break;
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000139 case 't':
140 testing = 1;
141 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000142 case 'h':
143 print_usage("ip6tables-restore",
Harald Welte80fe35d2002-05-29 13:08:15 +0000144 IPTABLES_VERSION);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000145 break;
146 case 'n':
147 noflush = 1;
148 break;
Harald Welte58918652001-06-16 18:25:25 +0000149 case 'M':
150 modprobe = optarg;
151 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000152 }
153 }
154
155 if (optind == argc - 1) {
156 in = fopen(argv[optind], "r");
157 if (!in) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000158 fprintf(stderr, "Can't open %s: %s\n", argv[optind],
András Kis-Szabó2f523792001-02-27 09:59:48 +0000159 strerror(errno));
160 exit(1);
161 }
162 }
163 else if (optind < argc) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000164 fprintf(stderr, "Unknown arguments found on commandline\n");
András Kis-Szabó2f523792001-02-27 09:59:48 +0000165 exit(1);
166 }
167 else in = stdin;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000168
András Kis-Szabó2f523792001-02-27 09:59:48 +0000169 /* Grab standard input. */
170 while (fgets(buffer, sizeof(buffer), in)) {
Martin Josefssoncc536282004-02-02 20:14:56 +0000171 int ret = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000172
173 line++;
Martin Josefsson5065afa2004-01-31 19:33:47 +0000174 if (buffer[0] == '\n')
175 continue;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000176 else if (buffer[0] == '#') {
Martin Josefsson3673dcb2004-01-31 19:41:49 +0000177 if (verbose)
178 fputs(buffer, stdout);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000179 continue;
Harald Weltea8658ca2003-03-05 07:46:15 +0000180 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000181 if (!testing) {
182 DEBUGP("Calling commit\n");
183 ret = ip6tc_commit(&handle);
184 } else {
185 DEBUGP("Not calling commit, testing\n");
186 ret = 1;
187 }
Harald Weltea8658ca2003-03-05 07:46:15 +0000188 in_table = 0;
189 } else if ((buffer[0] == '*') && (!in_table)) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000190 /* New table */
191 char *table;
192
193 table = strtok(buffer+1, " \t\n");
194 DEBUGP("line %u, table '%s'\n", line, table);
195 if (!table) {
196 exit_error(PARAMETER_PROBLEM,
197 "%s: line %u table name invalid\n",
198 program_name, line);
199 exit(1);
200 }
201 strncpy(curtable, table, IP6T_TABLE_MAXNAMELEN);
Karsten Desler073df8f2004-01-31 15:33:55 +0000202 curtable[IP6T_TABLE_MAXNAMELEN] = '\0';
András Kis-Szabó2f523792001-02-27 09:59:48 +0000203
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000204 if (handle)
205 ip6tc_free(&handle);
206
Harald Welte58918652001-06-16 18:25:25 +0000207 handle = create_handle(table, modprobe);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000208 if (noflush == 0) {
209 DEBUGP("Cleaning all chains of table '%s'\n",
210 table);
211 for_each_chain(flush_entries, verbose, 1,
212 &handle);
213
214 DEBUGP("Deleting all user-defined chains "
215 "of table '%s'\n", table);
216 for_each_chain(delete_chain, verbose, 0,
217 &handle) ;
218 }
219
220 ret = 1;
Harald Weltea8658ca2003-03-05 07:46:15 +0000221 in_table = 1;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000222
Harald Weltea8658ca2003-03-05 07:46:15 +0000223 } else if ((buffer[0] == ':') && (in_table)) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000224 /* New chain. */
225 char *policy, *chain;
226
227 chain = strtok(buffer+1, " \t\n");
228 DEBUGP("line %u, chain '%s'\n", line, chain);
229 if (!chain) {
230 exit_error(PARAMETER_PROBLEM,
231 "%s: line %u chain name invalid\n",
232 program_name, line);
233 exit(1);
234 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000235
Harald Weltedf1c71c2004-08-30 16:00:32 +0000236 if (ip6tc_builtin(chain, handle) <= 0) {
Charlie Brady595e4932005-06-12 15:54:15 +0000237 if (noflush && ip6tc_is_chain(chain, handle)) {
238 DEBUGP("Flushing existing user defined chain '%s'\n", chain);
239 if (!ip6tc_flush_entries(chain, &handle))
240 exit_error(PARAMETER_PROBLEM,
241 "error flushing chain "
242 "'%s':%s\n", chain,
243 strerror(errno));
244 } else {
245 DEBUGP("Creating new chain '%s'\n", chain);
246 if (!ip6tc_create_chain(chain, &handle))
247 exit_error(PARAMETER_PROBLEM,
248 "error creating chain "
249 "'%s':%s\n", chain,
250 strerror(errno));
251 }
Harald Welte885c6eb2001-10-04 08:30:46 +0000252 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000253
254 policy = strtok(NULL, " \t\n");
255 DEBUGP("line %u, policy '%s'\n", line, policy);
256 if (!policy) {
257 exit_error(PARAMETER_PROBLEM,
258 "%s: line %u policy invalid\n",
259 program_name, line);
260 exit(1);
261 }
262
263 if (strcmp(policy, "-") != 0) {
264 struct ip6t_counters count;
265
266 if (counters) {
267 char *ctrs;
268 ctrs = strtok(NULL, " \t\n");
269
Harald Welte6f38a302006-02-09 14:35:38 +0000270 if (!ctrs || !parse_counters(ctrs, &count))
271 exit_error(PARAMETER_PROBLEM,
272 "invalid policy counters "
273 "for chain '%s'\n", chain);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000274
275 } else {
276 memset(&count, 0,
277 sizeof(struct ip6t_counters));
278 }
279
280 DEBUGP("Setting policy of chain %s to %s\n",
281 chain, policy);
282
283 if (!ip6tc_set_policy(chain, policy, &count,
284 &handle))
285 exit_error(OTHER_PROBLEM,
286 "Can't set policy `%s'"
287 " on `%s' line %u: %s\n",
288 chain, policy, line,
289 ip6tc_strerror(errno));
290 }
291
292 ret = 1;
293
Harald Weltea8658ca2003-03-05 07:46:15 +0000294 } else if (in_table) {
Harald Welte885c6eb2001-10-04 08:30:46 +0000295 int a;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000296 char *ptr = buffer;
297 char *pcnt = NULL;
298 char *bcnt = NULL;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000299 char *parsestart;
Harald Welte885c6eb2001-10-04 08:30:46 +0000300
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000301 /* the parser */
302 char *param_start, *curchar;
303 int quote_open;
Harald Welte885c6eb2001-10-04 08:30:46 +0000304
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000305 /* reset the newargv */
306 newargc = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000307
308 if (buffer[0] == '[') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000309 /* we have counters in our input */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000310 ptr = strchr(buffer, ']');
311 if (!ptr)
312 exit_error(PARAMETER_PROBLEM,
313 "Bad line %u: need ]\n",
314 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000315
András Kis-Szabó2f523792001-02-27 09:59:48 +0000316 pcnt = strtok(buffer+1, ":");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000317 if (!pcnt)
318 exit_error(PARAMETER_PROBLEM,
319 "Bad line %u: need :\n",
320 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000321
András Kis-Szabó2f523792001-02-27 09:59:48 +0000322 bcnt = strtok(NULL, "]");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000323 if (!bcnt)
324 exit_error(PARAMETER_PROBLEM,
325 "Bad line %u: need ]\n",
326 line);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000327
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000328 /* start command parsing after counter */
329 parsestart = ptr + 1;
330 } else {
331 /* start command parsing at start of line */
332 parsestart = buffer;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000333 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000334
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000335 add_argv(argv[0]);
336 add_argv("-t");
337 add_argv((char *) &curtable);
338
339 if (counters && pcnt && bcnt) {
340 add_argv("--set-counters");
341 add_argv((char *) pcnt);
342 add_argv((char *) bcnt);
343 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000344
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000345 /* After fighting with strtok enough, here's now
346 * a 'real' parser. According to Rusty I'm now no
347 * longer a real hacker, but I can live with that */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000348
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000349 quote_open = 0;
350 param_start = parsestart;
351
352 for (curchar = parsestart; *curchar; curchar++) {
353 if (*curchar == '"') {
Michael Rash29cdbd02004-01-05 09:41:50 +0000354 /* quote_open cannot be true if there
355 * was no previous character. Thus,
356 * curchar-1 has to be within bounds */
357 if (quote_open &&
358 *(curchar-1) != '\\') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000359 quote_open = 0;
360 *curchar = ' ';
361 } else {
362 quote_open = 1;
363 param_start++;
364 }
365 }
366 if (*curchar == ' '
367 || *curchar == '\t'
368 || * curchar == '\n') {
369 char param_buffer[1024];
370 int param_len = curchar-param_start;
Harald Welte885c6eb2001-10-04 08:30:46 +0000371
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000372 if (quote_open)
373 continue;
Harald Welte885c6eb2001-10-04 08:30:46 +0000374
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000375 if (!param_len) {
376 /* two spaces? */
377 param_start++;
378 continue;
379 }
380
381 /* end of one parameter */
382 strncpy(param_buffer, param_start,
383 param_len);
384 *(param_buffer+param_len) = '\0';
Harald Weltebb41f882001-10-21 14:11:54 +0000385
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000386 /* check if table name specified */
Harald Welte48a6f902001-10-22 15:16:21 +0000387 if (!strncmp(param_buffer, "-t", 3)
388 || !strncmp(param_buffer, "--table", 8)) {
Harald Weltebb41f882001-10-21 14:11:54 +0000389 exit_error(PARAMETER_PROBLEM,
390 "Line %u seems to have a "
391 "-t table option.\n", line);
392 exit(1);
393 }
394
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000395 add_argv(param_buffer);
396 param_start += param_len + 1;
397 } else {
398 /* regular character, skip */
399 }
400 }
Harald Welte885c6eb2001-10-04 08:30:46 +0000401
402 DEBUGP("calling do_command6(%u, argv, &%s, handle):\n",
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000403 newargc, curtable);
Harald Welte885c6eb2001-10-04 08:30:46 +0000404
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000405 for (a = 0; a < newargc; a++)
András Kis-Szabó2f523792001-02-27 09:59:48 +0000406 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
407
Harald Welte885c6eb2001-10-04 08:30:46 +0000408 ret = do_command6(newargc, newargv,
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000409 &newargv[2], &handle);
Harald Welte885c6eb2001-10-04 08:30:46 +0000410
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000411 free_argv();
András Kis-Szabó2f523792001-02-27 09:59:48 +0000412 }
413 if (!ret) {
414 fprintf(stderr, "%s: line %u failed\n",
415 program_name, line);
416 exit(1);
417 }
418 }
Martin Josefssone0dc5812004-02-02 19:58:36 +0000419 if (in_table) {
420 fprintf(stderr, "%s: COMMIT expected at line %u\n",
421 program_name, line + 1);
422 exit(1);
423 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000424
425 return 0;
426}