blob: 25c6ebd9d6c4f39d30eaf3f5a73ff2f5dc1e8962 [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"
19#include "libiptc/libip6tc.h"
20
21#ifdef DEBUG
22#define DEBUGP(x, args...) fprintf(stderr, x, ## args)
23#else
24#define DEBUGP(x, args...)
25#endif
26
András Kis-Szabó2f523792001-02-27 09:59:48 +000027static int binary = 0, counters = 0, verbose = 0, noflush = 0;
28
29/* Keeping track of external matches and targets. */
30static struct option options[] = {
31 { "binary", 0, 0, 'b' },
32 { "counters", 0, 0, 'c' },
Martin Josefssonec789712004-01-31 19:28:13 +000033 { "verbose", 0, 0, 'v' },
Martin Josefsson4e5e29a2004-02-02 19:59:17 +000034 { "test", 0, 0, 't' },
András Kis-Szabó2f523792001-02-27 09:59:48 +000035 { "help", 0, 0, 'h' },
36 { "noflush", 0, 0, 'n'},
Harald Welte58918652001-06-16 18:25:25 +000037 { "modprobe", 1, 0, 'M'},
András Kis-Szabó2f523792001-02-27 09:59:48 +000038 { 0 }
39};
40
41static void print_usage(const char *name, const char *version) __attribute__((noreturn));
42
43static void print_usage(const char *name, const char *version)
44{
Martin Josefsson60044392004-02-02 20:12:33 +000045 fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-t] [-h]\n"
András Kis-Szabó2f523792001-02-27 09:59:48 +000046 " [ --binary ]\n"
47 " [ --counters ]\n"
48 " [ --verbose ]\n"
Martin Josefsson4e5e29a2004-02-02 19:59:17 +000049 " [ --test ]\n"
András Kis-Szabó2f523792001-02-27 09:59:48 +000050 " [ --help ]\n"
Harald Welte58918652001-06-16 18:25:25 +000051 " [ --noflush ]\n"
52 " [ --modprobe=<command>]\n", name);
András Kis-Szabó2f523792001-02-27 09:59:48 +000053
54 exit(1);
55}
56
Harald Welte58918652001-06-16 18:25:25 +000057ip6tc_handle_t create_handle(const char *tablename, const char* modprobe)
András Kis-Szabó2f523792001-02-27 09:59:48 +000058{
59 ip6tc_handle_t handle;
60
61 handle = ip6tc_init(tablename);
Harald Welte58918652001-06-16 18:25:25 +000062
63 if (!handle) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000064 /* try to insmod the module if iptc_init failed */
Yasuyuki KOZAKAI64bcccb2007-03-20 16:50:21 +000065 ip6tables_insmod("ip6_tables", modprobe, 0);
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000066 handle = ip6tc_init(tablename);
Harald Welte58918652001-06-16 18:25:25 +000067 }
68
András Kis-Szabó2f523792001-02-27 09:59:48 +000069 if (!handle) {
Patrick McHardy5b098ac2007-02-14 13:59:12 +000070 exit_error(PARAMETER_PROBLEM, "%s: unable to initialize "
András Kis-Szabó2f523792001-02-27 09:59:48 +000071 "table '%s'\n", program_name, tablename);
72 exit(1);
73 }
74 return handle;
75}
76
Lutz Jaenickee78c69c2006-12-09 13:06:04 +000077static int parse_counters(char *string, struct ip6t_counters *ctr)
András Kis-Szabó2f523792001-02-27 09:59:48 +000078{
Martin Josefssona28d4952004-05-26 16:04:48 +000079 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 +000080}
81
Harald Welte885c6eb2001-10-04 08:30:46 +000082/* global new argv and argc */
83static char *newargv[255];
84static int newargc;
85
86/* function adding one argument to newargv, updating newargc
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000087 * returns true if argument added, false otherwise */
Harald Welte885c6eb2001-10-04 08:30:46 +000088static int add_argv(char *what) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000089 DEBUGP("add_argv: %s\n", what);
90 if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) {
91 newargv[newargc] = strdup(what);
92 newargc++;
93 return 1;
94 } else
95 return 0;
Harald Welte885c6eb2001-10-04 08:30:46 +000096}
97
98static void free_argv(void) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000099 int i;
Harald Welte885c6eb2001-10-04 08:30:46 +0000100
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000101 for (i = 0; i < newargc; i++)
102 free(newargv[i]);
Harald Welte885c6eb2001-10-04 08:30:46 +0000103}
104
András Kis-Szabó2f523792001-02-27 09:59:48 +0000105int main(int argc, char *argv[])
106{
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000107 ip6tc_handle_t handle = NULL;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000108 char buffer[10240];
András Kis-Szabó2f523792001-02-27 09:59:48 +0000109 int c;
110 char curtable[IP6T_TABLE_MAXNAMELEN + 1];
András Kis-Szabó2f523792001-02-27 09:59:48 +0000111 FILE *in;
Harald Welte58918652001-06-16 18:25:25 +0000112 const char *modprobe = 0;
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000113 int in_table = 0, testing = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000114
115 program_name = "ip6tables-restore";
Harald Welte80fe35d2002-05-29 13:08:15 +0000116 program_version = IPTABLES_VERSION;
Harald Weltea8658ca2003-03-05 07:46:15 +0000117 line = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000118
Martin Josefsson357d59d2004-12-27 19:49:28 +0000119 lib_dir = getenv("IP6TABLES_LIB_DIR");
120 if (!lib_dir)
121 lib_dir = IP6T_LIB_DIR;
122
Harald Welte3efb6ea2001-08-06 18:50:21 +0000123#ifdef NO_SHARED_LIBS
124 init_extensions();
125#endif
126
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000127 while ((c = getopt_long(argc, argv, "bcvthnM:", options, NULL)) != -1) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000128 switch (c) {
129 case 'b':
130 binary = 1;
131 break;
132 case 'c':
133 counters = 1;
134 break;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000135 case 'v':
136 verbose = 1;
137 break;
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000138 case 't':
139 testing = 1;
140 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000141 case 'h':
142 print_usage("ip6tables-restore",
Harald Welte80fe35d2002-05-29 13:08:15 +0000143 IPTABLES_VERSION);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000144 break;
145 case 'n':
146 noflush = 1;
147 break;
Harald Welte58918652001-06-16 18:25:25 +0000148 case 'M':
149 modprobe = optarg;
150 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000151 }
152 }
153
154 if (optind == argc - 1) {
155 in = fopen(argv[optind], "r");
156 if (!in) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000157 fprintf(stderr, "Can't open %s: %s\n", argv[optind],
András Kis-Szabó2f523792001-02-27 09:59:48 +0000158 strerror(errno));
159 exit(1);
160 }
161 }
162 else if (optind < argc) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000163 fprintf(stderr, "Unknown arguments found on commandline\n");
András Kis-Szabó2f523792001-02-27 09:59:48 +0000164 exit(1);
165 }
166 else in = stdin;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000167
András Kis-Szabó2f523792001-02-27 09:59:48 +0000168 /* Grab standard input. */
169 while (fgets(buffer, sizeof(buffer), in)) {
Martin Josefssoncc536282004-02-02 20:14:56 +0000170 int ret = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000171
172 line++;
Martin Josefsson5065afa2004-01-31 19:33:47 +0000173 if (buffer[0] == '\n')
174 continue;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000175 else if (buffer[0] == '#') {
Martin Josefsson3673dcb2004-01-31 19:41:49 +0000176 if (verbose)
177 fputs(buffer, stdout);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000178 continue;
Harald Weltea8658ca2003-03-05 07:46:15 +0000179 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000180 if (!testing) {
181 DEBUGP("Calling commit\n");
182 ret = ip6tc_commit(&handle);
183 } else {
184 DEBUGP("Not calling commit, testing\n");
185 ret = 1;
186 }
Harald Weltea8658ca2003-03-05 07:46:15 +0000187 in_table = 0;
188 } else if ((buffer[0] == '*') && (!in_table)) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000189 /* New table */
190 char *table;
191
192 table = strtok(buffer+1, " \t\n");
193 DEBUGP("line %u, table '%s'\n", line, table);
194 if (!table) {
195 exit_error(PARAMETER_PROBLEM,
196 "%s: line %u table name invalid\n",
197 program_name, line);
198 exit(1);
199 }
200 strncpy(curtable, table, IP6T_TABLE_MAXNAMELEN);
Karsten Desler073df8f2004-01-31 15:33:55 +0000201 curtable[IP6T_TABLE_MAXNAMELEN] = '\0';
András Kis-Szabó2f523792001-02-27 09:59:48 +0000202
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000203 if (handle)
204 ip6tc_free(&handle);
205
Harald Welte58918652001-06-16 18:25:25 +0000206 handle = create_handle(table, modprobe);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000207 if (noflush == 0) {
208 DEBUGP("Cleaning all chains of table '%s'\n",
209 table);
210 for_each_chain(flush_entries, verbose, 1,
211 &handle);
212
213 DEBUGP("Deleting all user-defined chains "
214 "of table '%s'\n", table);
215 for_each_chain(delete_chain, verbose, 0,
216 &handle) ;
217 }
218
219 ret = 1;
Harald Weltea8658ca2003-03-05 07:46:15 +0000220 in_table = 1;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000221
Harald Weltea8658ca2003-03-05 07:46:15 +0000222 } else if ((buffer[0] == ':') && (in_table)) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000223 /* New chain. */
224 char *policy, *chain;
225
226 chain = strtok(buffer+1, " \t\n");
227 DEBUGP("line %u, chain '%s'\n", line, chain);
228 if (!chain) {
229 exit_error(PARAMETER_PROBLEM,
230 "%s: line %u chain name invalid\n",
231 program_name, line);
232 exit(1);
233 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000234
Harald Weltedf1c71c2004-08-30 16:00:32 +0000235 if (ip6tc_builtin(chain, handle) <= 0) {
Charlie Brady595e4932005-06-12 15:54:15 +0000236 if (noflush && ip6tc_is_chain(chain, handle)) {
237 DEBUGP("Flushing existing user defined chain '%s'\n", chain);
238 if (!ip6tc_flush_entries(chain, &handle))
239 exit_error(PARAMETER_PROBLEM,
240 "error flushing chain "
241 "'%s':%s\n", chain,
242 strerror(errno));
243 } else {
244 DEBUGP("Creating new chain '%s'\n", chain);
245 if (!ip6tc_create_chain(chain, &handle))
246 exit_error(PARAMETER_PROBLEM,
247 "error creating chain "
248 "'%s':%s\n", chain,
249 strerror(errno));
250 }
Harald Welte885c6eb2001-10-04 08:30:46 +0000251 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000252
253 policy = strtok(NULL, " \t\n");
254 DEBUGP("line %u, policy '%s'\n", line, policy);
255 if (!policy) {
256 exit_error(PARAMETER_PROBLEM,
257 "%s: line %u policy invalid\n",
258 program_name, line);
259 exit(1);
260 }
261
262 if (strcmp(policy, "-") != 0) {
263 struct ip6t_counters count;
264
265 if (counters) {
266 char *ctrs;
267 ctrs = strtok(NULL, " \t\n");
268
Harald Welte6f38a302006-02-09 14:35:38 +0000269 if (!ctrs || !parse_counters(ctrs, &count))
270 exit_error(PARAMETER_PROBLEM,
271 "invalid policy counters "
272 "for chain '%s'\n", chain);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000273
274 } else {
275 memset(&count, 0,
276 sizeof(struct ip6t_counters));
277 }
278
279 DEBUGP("Setting policy of chain %s to %s\n",
280 chain, policy);
281
282 if (!ip6tc_set_policy(chain, policy, &count,
283 &handle))
284 exit_error(OTHER_PROBLEM,
285 "Can't set policy `%s'"
286 " on `%s' line %u: %s\n",
287 chain, policy, line,
288 ip6tc_strerror(errno));
289 }
290
291 ret = 1;
292
Harald Weltea8658ca2003-03-05 07:46:15 +0000293 } else if (in_table) {
Harald Welte885c6eb2001-10-04 08:30:46 +0000294 int a;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000295 char *ptr = buffer;
296 char *pcnt = NULL;
297 char *bcnt = NULL;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000298 char *parsestart;
Harald Welte885c6eb2001-10-04 08:30:46 +0000299
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000300 /* the parser */
301 char *param_start, *curchar;
302 int quote_open;
Harald Welte885c6eb2001-10-04 08:30:46 +0000303
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000304 /* reset the newargv */
305 newargc = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000306
307 if (buffer[0] == '[') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000308 /* we have counters in our input */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000309 ptr = strchr(buffer, ']');
310 if (!ptr)
311 exit_error(PARAMETER_PROBLEM,
312 "Bad line %u: need ]\n",
313 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000314
András Kis-Szabó2f523792001-02-27 09:59:48 +0000315 pcnt = strtok(buffer+1, ":");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000316 if (!pcnt)
317 exit_error(PARAMETER_PROBLEM,
318 "Bad line %u: need :\n",
319 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000320
András Kis-Szabó2f523792001-02-27 09:59:48 +0000321 bcnt = strtok(NULL, "]");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000322 if (!bcnt)
323 exit_error(PARAMETER_PROBLEM,
324 "Bad line %u: need ]\n",
325 line);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000326
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000327 /* start command parsing after counter */
328 parsestart = ptr + 1;
329 } else {
330 /* start command parsing at start of line */
331 parsestart = buffer;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000332 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000333
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000334 add_argv(argv[0]);
335 add_argv("-t");
336 add_argv((char *) &curtable);
337
338 if (counters && pcnt && bcnt) {
339 add_argv("--set-counters");
340 add_argv((char *) pcnt);
341 add_argv((char *) bcnt);
342 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000343
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000344 /* After fighting with strtok enough, here's now
345 * a 'real' parser. According to Rusty I'm now no
346 * longer a real hacker, but I can live with that */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000347
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000348 quote_open = 0;
349 param_start = parsestart;
350
351 for (curchar = parsestart; *curchar; curchar++) {
352 if (*curchar == '"') {
Michael Rash29cdbd02004-01-05 09:41:50 +0000353 /* quote_open cannot be true if there
354 * was no previous character. Thus,
355 * curchar-1 has to be within bounds */
356 if (quote_open &&
357 *(curchar-1) != '\\') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000358 quote_open = 0;
359 *curchar = ' ';
360 } else {
361 quote_open = 1;
362 param_start++;
363 }
364 }
365 if (*curchar == ' '
366 || *curchar == '\t'
367 || * curchar == '\n') {
368 char param_buffer[1024];
369 int param_len = curchar-param_start;
Harald Welte885c6eb2001-10-04 08:30:46 +0000370
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000371 if (quote_open)
372 continue;
Harald Welte885c6eb2001-10-04 08:30:46 +0000373
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000374 if (!param_len) {
375 /* two spaces? */
376 param_start++;
377 continue;
378 }
379
380 /* end of one parameter */
381 strncpy(param_buffer, param_start,
382 param_len);
383 *(param_buffer+param_len) = '\0';
Harald Weltebb41f882001-10-21 14:11:54 +0000384
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000385 /* check if table name specified */
Harald Welte48a6f902001-10-22 15:16:21 +0000386 if (!strncmp(param_buffer, "-t", 3)
387 || !strncmp(param_buffer, "--table", 8)) {
Harald Weltebb41f882001-10-21 14:11:54 +0000388 exit_error(PARAMETER_PROBLEM,
389 "Line %u seems to have a "
390 "-t table option.\n", line);
391 exit(1);
392 }
393
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000394 add_argv(param_buffer);
395 param_start += param_len + 1;
396 } else {
397 /* regular character, skip */
398 }
399 }
Harald Welte885c6eb2001-10-04 08:30:46 +0000400
401 DEBUGP("calling do_command6(%u, argv, &%s, handle):\n",
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000402 newargc, curtable);
Harald Welte885c6eb2001-10-04 08:30:46 +0000403
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000404 for (a = 0; a < newargc; a++)
András Kis-Szabó2f523792001-02-27 09:59:48 +0000405 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
406
Harald Welte885c6eb2001-10-04 08:30:46 +0000407 ret = do_command6(newargc, newargv,
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000408 &newargv[2], &handle);
Harald Welte885c6eb2001-10-04 08:30:46 +0000409
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000410 free_argv();
András Kis-Szabó2f523792001-02-27 09:59:48 +0000411 }
412 if (!ret) {
413 fprintf(stderr, "%s: line %u failed\n",
414 program_name, line);
415 exit(1);
416 }
417 }
Martin Josefssone0dc5812004-02-02 19:58:36 +0000418 if (in_table) {
419 fprintf(stderr, "%s: COMMIT expected at line %u\n",
420 program_name, line + 1);
421 exit(1);
422 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000423
424 return 0;
425}