blob: f7c360973c7786316f36cc90f9733b09f24b38ea [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{
Patrick McHardy875441e2007-10-17 08:48:58 +000080 unsigned long long pcnt, bcnt;
81 int ret;
Patrick McHardy31f51c62007-10-16 08:49:31 +000082
Patrick McHardy875441e2007-10-17 08:48:58 +000083 ret = sscanf(string, "[%llu:%llu]",
84 (unsigned long long *)&pcnt,
85 (unsigned long long *)&bcnt);
86 ctr->pcnt = pcnt;
87 ctr->bcnt = bcnt;
88 return ret == 2;
András Kis-Szabó2f523792001-02-27 09:59:48 +000089}
90
Harald Welte885c6eb2001-10-04 08:30:46 +000091/* global new argv and argc */
92static char *newargv[255];
93static int newargc;
94
95/* function adding one argument to newargv, updating newargc
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000096 * returns true if argument added, false otherwise */
Harald Welte885c6eb2001-10-04 08:30:46 +000097static int add_argv(char *what) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000098 DEBUGP("add_argv: %s\n", what);
99 if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) {
100 newargv[newargc] = strdup(what);
101 newargc++;
102 return 1;
103 } else
104 return 0;
Harald Welte885c6eb2001-10-04 08:30:46 +0000105}
106
107static void free_argv(void) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000108 int i;
Harald Welte885c6eb2001-10-04 08:30:46 +0000109
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000110 for (i = 0; i < newargc; i++)
111 free(newargv[i]);
Harald Welte885c6eb2001-10-04 08:30:46 +0000112}
113
Hann-Huei Chiou4bc48332007-10-23 14:22:34 +0000114#ifdef IPTABLES_MULTI
115int ip6tables_restore_main(int argc, char *argv[])
116#else
András Kis-Szabó2f523792001-02-27 09:59:48 +0000117int main(int argc, char *argv[])
Hann-Huei Chiou4bc48332007-10-23 14:22:34 +0000118#endif
András Kis-Szabó2f523792001-02-27 09:59:48 +0000119{
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000120 ip6tc_handle_t handle = NULL;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000121 char buffer[10240];
András Kis-Szabó2f523792001-02-27 09:59:48 +0000122 int c;
123 char curtable[IP6T_TABLE_MAXNAMELEN + 1];
András Kis-Szabó2f523792001-02-27 09:59:48 +0000124 FILE *in;
Harald Welte58918652001-06-16 18:25:25 +0000125 const char *modprobe = 0;
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000126 int in_table = 0, testing = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000127
128 program_name = "ip6tables-restore";
Harald Welte80fe35d2002-05-29 13:08:15 +0000129 program_version = IPTABLES_VERSION;
Harald Weltea8658ca2003-03-05 07:46:15 +0000130 line = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000131
Martin Josefsson357d59d2004-12-27 19:49:28 +0000132 lib_dir = getenv("IP6TABLES_LIB_DIR");
133 if (!lib_dir)
134 lib_dir = IP6T_LIB_DIR;
135
Harald Welte3efb6ea2001-08-06 18:50:21 +0000136#ifdef NO_SHARED_LIBS
137 init_extensions();
138#endif
139
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000140 while ((c = getopt_long(argc, argv, "bcvthnM:", options, NULL)) != -1) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000141 switch (c) {
142 case 'b':
143 binary = 1;
144 break;
145 case 'c':
146 counters = 1;
147 break;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000148 case 'v':
149 verbose = 1;
150 break;
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000151 case 't':
152 testing = 1;
153 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000154 case 'h':
155 print_usage("ip6tables-restore",
Harald Welte80fe35d2002-05-29 13:08:15 +0000156 IPTABLES_VERSION);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000157 break;
158 case 'n':
159 noflush = 1;
160 break;
Harald Welte58918652001-06-16 18:25:25 +0000161 case 'M':
162 modprobe = optarg;
163 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000164 }
165 }
166
167 if (optind == argc - 1) {
168 in = fopen(argv[optind], "r");
169 if (!in) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000170 fprintf(stderr, "Can't open %s: %s\n", argv[optind],
András Kis-Szabó2f523792001-02-27 09:59:48 +0000171 strerror(errno));
172 exit(1);
173 }
174 }
175 else if (optind < argc) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000176 fprintf(stderr, "Unknown arguments found on commandline\n");
András Kis-Szabó2f523792001-02-27 09:59:48 +0000177 exit(1);
178 }
179 else in = stdin;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000180
András Kis-Szabó2f523792001-02-27 09:59:48 +0000181 /* Grab standard input. */
182 while (fgets(buffer, sizeof(buffer), in)) {
Martin Josefssoncc536282004-02-02 20:14:56 +0000183 int ret = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000184
185 line++;
Martin Josefsson5065afa2004-01-31 19:33:47 +0000186 if (buffer[0] == '\n')
187 continue;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000188 else if (buffer[0] == '#') {
Martin Josefsson3673dcb2004-01-31 19:41:49 +0000189 if (verbose)
190 fputs(buffer, stdout);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000191 continue;
Harald Weltea8658ca2003-03-05 07:46:15 +0000192 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000193 if (!testing) {
194 DEBUGP("Calling commit\n");
195 ret = ip6tc_commit(&handle);
196 } else {
197 DEBUGP("Not calling commit, testing\n");
198 ret = 1;
199 }
Harald Weltea8658ca2003-03-05 07:46:15 +0000200 in_table = 0;
201 } else if ((buffer[0] == '*') && (!in_table)) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000202 /* New table */
203 char *table;
204
205 table = strtok(buffer+1, " \t\n");
206 DEBUGP("line %u, table '%s'\n", line, table);
207 if (!table) {
208 exit_error(PARAMETER_PROBLEM,
209 "%s: line %u table name invalid\n",
210 program_name, line);
211 exit(1);
212 }
213 strncpy(curtable, table, IP6T_TABLE_MAXNAMELEN);
Karsten Desler073df8f2004-01-31 15:33:55 +0000214 curtable[IP6T_TABLE_MAXNAMELEN] = '\0';
András Kis-Szabó2f523792001-02-27 09:59:48 +0000215
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000216 if (handle)
217 ip6tc_free(&handle);
218
Harald Welte58918652001-06-16 18:25:25 +0000219 handle = create_handle(table, modprobe);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000220 if (noflush == 0) {
221 DEBUGP("Cleaning all chains of table '%s'\n",
222 table);
223 for_each_chain(flush_entries, verbose, 1,
224 &handle);
225
226 DEBUGP("Deleting all user-defined chains "
227 "of table '%s'\n", table);
228 for_each_chain(delete_chain, verbose, 0,
229 &handle) ;
230 }
231
232 ret = 1;
Harald Weltea8658ca2003-03-05 07:46:15 +0000233 in_table = 1;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000234
Harald Weltea8658ca2003-03-05 07:46:15 +0000235 } else if ((buffer[0] == ':') && (in_table)) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000236 /* New chain. */
237 char *policy, *chain;
238
239 chain = strtok(buffer+1, " \t\n");
240 DEBUGP("line %u, chain '%s'\n", line, chain);
241 if (!chain) {
242 exit_error(PARAMETER_PROBLEM,
243 "%s: line %u chain name invalid\n",
244 program_name, line);
245 exit(1);
246 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000247
Harald Weltedf1c71c2004-08-30 16:00:32 +0000248 if (ip6tc_builtin(chain, handle) <= 0) {
Charlie Brady595e4932005-06-12 15:54:15 +0000249 if (noflush && ip6tc_is_chain(chain, handle)) {
250 DEBUGP("Flushing existing user defined chain '%s'\n", chain);
251 if (!ip6tc_flush_entries(chain, &handle))
252 exit_error(PARAMETER_PROBLEM,
253 "error flushing chain "
254 "'%s':%s\n", chain,
255 strerror(errno));
256 } else {
257 DEBUGP("Creating new chain '%s'\n", chain);
258 if (!ip6tc_create_chain(chain, &handle))
259 exit_error(PARAMETER_PROBLEM,
260 "error creating chain "
261 "'%s':%s\n", chain,
262 strerror(errno));
263 }
Harald Welte885c6eb2001-10-04 08:30:46 +0000264 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000265
266 policy = strtok(NULL, " \t\n");
267 DEBUGP("line %u, policy '%s'\n", line, policy);
268 if (!policy) {
269 exit_error(PARAMETER_PROBLEM,
270 "%s: line %u policy invalid\n",
271 program_name, line);
272 exit(1);
273 }
274
275 if (strcmp(policy, "-") != 0) {
276 struct ip6t_counters count;
277
278 if (counters) {
279 char *ctrs;
280 ctrs = strtok(NULL, " \t\n");
281
Harald Welte6f38a302006-02-09 14:35:38 +0000282 if (!ctrs || !parse_counters(ctrs, &count))
283 exit_error(PARAMETER_PROBLEM,
284 "invalid policy counters "
285 "for chain '%s'\n", chain);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000286
287 } else {
288 memset(&count, 0,
289 sizeof(struct ip6t_counters));
290 }
291
292 DEBUGP("Setting policy of chain %s to %s\n",
293 chain, policy);
294
295 if (!ip6tc_set_policy(chain, policy, &count,
296 &handle))
297 exit_error(OTHER_PROBLEM,
298 "Can't set policy `%s'"
299 " on `%s' line %u: %s\n",
300 chain, policy, line,
301 ip6tc_strerror(errno));
302 }
303
304 ret = 1;
305
Harald Weltea8658ca2003-03-05 07:46:15 +0000306 } else if (in_table) {
Harald Welte885c6eb2001-10-04 08:30:46 +0000307 int a;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000308 char *ptr = buffer;
309 char *pcnt = NULL;
310 char *bcnt = NULL;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000311 char *parsestart;
Harald Welte885c6eb2001-10-04 08:30:46 +0000312
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000313 /* the parser */
314 char *param_start, *curchar;
315 int quote_open;
Harald Welte885c6eb2001-10-04 08:30:46 +0000316
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000317 /* reset the newargv */
318 newargc = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000319
320 if (buffer[0] == '[') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000321 /* we have counters in our input */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000322 ptr = strchr(buffer, ']');
323 if (!ptr)
324 exit_error(PARAMETER_PROBLEM,
325 "Bad line %u: need ]\n",
326 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000327
András Kis-Szabó2f523792001-02-27 09:59:48 +0000328 pcnt = strtok(buffer+1, ":");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000329 if (!pcnt)
330 exit_error(PARAMETER_PROBLEM,
331 "Bad line %u: need :\n",
332 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000333
András Kis-Szabó2f523792001-02-27 09:59:48 +0000334 bcnt = strtok(NULL, "]");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000335 if (!bcnt)
336 exit_error(PARAMETER_PROBLEM,
337 "Bad line %u: need ]\n",
338 line);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000339
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000340 /* start command parsing after counter */
341 parsestart = ptr + 1;
342 } else {
343 /* start command parsing at start of line */
344 parsestart = buffer;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000345 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000346
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000347 add_argv(argv[0]);
348 add_argv("-t");
349 add_argv((char *) &curtable);
350
351 if (counters && pcnt && bcnt) {
352 add_argv("--set-counters");
353 add_argv((char *) pcnt);
354 add_argv((char *) bcnt);
355 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000356
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000357 /* After fighting with strtok enough, here's now
358 * a 'real' parser. According to Rusty I'm now no
359 * longer a real hacker, but I can live with that */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000360
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000361 quote_open = 0;
362 param_start = parsestart;
363
364 for (curchar = parsestart; *curchar; curchar++) {
365 if (*curchar == '"') {
Michael Rash29cdbd02004-01-05 09:41:50 +0000366 /* quote_open cannot be true if there
367 * was no previous character. Thus,
368 * curchar-1 has to be within bounds */
369 if (quote_open &&
370 *(curchar-1) != '\\') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000371 quote_open = 0;
372 *curchar = ' ';
373 } else {
374 quote_open = 1;
375 param_start++;
376 }
377 }
378 if (*curchar == ' '
379 || *curchar == '\t'
380 || * curchar == '\n') {
381 char param_buffer[1024];
382 int param_len = curchar-param_start;
Harald Welte885c6eb2001-10-04 08:30:46 +0000383
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000384 if (quote_open)
385 continue;
Harald Welte885c6eb2001-10-04 08:30:46 +0000386
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000387 if (!param_len) {
388 /* two spaces? */
389 param_start++;
390 continue;
391 }
392
393 /* end of one parameter */
394 strncpy(param_buffer, param_start,
395 param_len);
396 *(param_buffer+param_len) = '\0';
Harald Weltebb41f882001-10-21 14:11:54 +0000397
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000398 /* check if table name specified */
Harald Welte48a6f902001-10-22 15:16:21 +0000399 if (!strncmp(param_buffer, "-t", 3)
400 || !strncmp(param_buffer, "--table", 8)) {
Harald Weltebb41f882001-10-21 14:11:54 +0000401 exit_error(PARAMETER_PROBLEM,
402 "Line %u seems to have a "
403 "-t table option.\n", line);
404 exit(1);
405 }
406
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000407 add_argv(param_buffer);
408 param_start += param_len + 1;
409 } else {
410 /* regular character, skip */
411 }
412 }
Harald Welte885c6eb2001-10-04 08:30:46 +0000413
414 DEBUGP("calling do_command6(%u, argv, &%s, handle):\n",
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000415 newargc, curtable);
Harald Welte885c6eb2001-10-04 08:30:46 +0000416
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000417 for (a = 0; a < newargc; a++)
András Kis-Szabó2f523792001-02-27 09:59:48 +0000418 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
419
Harald Welte885c6eb2001-10-04 08:30:46 +0000420 ret = do_command6(newargc, newargv,
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000421 &newargv[2], &handle);
Harald Welte885c6eb2001-10-04 08:30:46 +0000422
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000423 free_argv();
András Kis-Szabó2f523792001-02-27 09:59:48 +0000424 }
425 if (!ret) {
426 fprintf(stderr, "%s: line %u failed\n",
427 program_name, line);
428 exit(1);
429 }
430 }
Martin Josefssone0dc5812004-02-02 19:58:36 +0000431 if (in_table) {
432 fprintf(stderr, "%s: COMMIT expected at line %u\n",
433 program_name, line + 1);
434 exit(1);
435 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000436
437 return 0;
438}