blob: 6909c8d7abd43a472563f7094ab929a28fcb9260 [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 */
65 ip6tables_insmod("ip6_tables", modprobe);
66 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) {
70 exit_error(PARAMETER_PROBLEM, "%s: unable to initialize"
71 "table '%s'\n", program_name, tablename);
72 exit(1);
73 }
74 return handle;
75}
76
77int parse_counters(char *string, struct ip6t_counters *ctr)
78{
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) {
157 fprintf(stderr, "Can't open %s: %s", argv[optind],
158 strerror(errno));
159 exit(1);
160 }
161 }
162 else if (optind < argc) {
163 fprintf(stderr, "Unknown arguments found on commandline");
164 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) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000236 DEBUGP("Creating new chain '%s'\n", chain);
Harald Welte885c6eb2001-10-04 08:30:46 +0000237 if (!ip6tc_create_chain(chain, &handle))
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000238 exit_error(PARAMETER_PROBLEM,
239 "error creating chain "
240 "'%s':%s\n", chain,
241 strerror(errno));
Harald Welte885c6eb2001-10-04 08:30:46 +0000242 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000243
244 policy = strtok(NULL, " \t\n");
245 DEBUGP("line %u, policy '%s'\n", line, policy);
246 if (!policy) {
247 exit_error(PARAMETER_PROBLEM,
248 "%s: line %u policy invalid\n",
249 program_name, line);
250 exit(1);
251 }
252
253 if (strcmp(policy, "-") != 0) {
254 struct ip6t_counters count;
255
256 if (counters) {
257 char *ctrs;
258 ctrs = strtok(NULL, " \t\n");
259
260 parse_counters(ctrs, &count);
261
262 } else {
263 memset(&count, 0,
264 sizeof(struct ip6t_counters));
265 }
266
267 DEBUGP("Setting policy of chain %s to %s\n",
268 chain, policy);
269
270 if (!ip6tc_set_policy(chain, policy, &count,
271 &handle))
272 exit_error(OTHER_PROBLEM,
273 "Can't set policy `%s'"
274 " on `%s' line %u: %s\n",
275 chain, policy, line,
276 ip6tc_strerror(errno));
277 }
278
279 ret = 1;
280
Harald Weltea8658ca2003-03-05 07:46:15 +0000281 } else if (in_table) {
Harald Welte885c6eb2001-10-04 08:30:46 +0000282 int a;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000283 char *ptr = buffer;
284 char *pcnt = NULL;
285 char *bcnt = NULL;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000286 char *parsestart;
Harald Welte885c6eb2001-10-04 08:30:46 +0000287
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000288 /* the parser */
289 char *param_start, *curchar;
290 int quote_open;
Harald Welte885c6eb2001-10-04 08:30:46 +0000291
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000292 /* reset the newargv */
293 newargc = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000294
295 if (buffer[0] == '[') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000296 /* we have counters in our input */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000297 ptr = strchr(buffer, ']');
298 if (!ptr)
299 exit_error(PARAMETER_PROBLEM,
300 "Bad line %u: need ]\n",
301 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000302
András Kis-Szabó2f523792001-02-27 09:59:48 +0000303 pcnt = strtok(buffer+1, ":");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000304 if (!pcnt)
305 exit_error(PARAMETER_PROBLEM,
306 "Bad line %u: need :\n",
307 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000308
András Kis-Szabó2f523792001-02-27 09:59:48 +0000309 bcnt = strtok(NULL, "]");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000310 if (!bcnt)
311 exit_error(PARAMETER_PROBLEM,
312 "Bad line %u: need ]\n",
313 line);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000314
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000315 /* start command parsing after counter */
316 parsestart = ptr + 1;
317 } else {
318 /* start command parsing at start of line */
319 parsestart = buffer;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000320 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000321
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000322 add_argv(argv[0]);
323 add_argv("-t");
324 add_argv((char *) &curtable);
325
326 if (counters && pcnt && bcnt) {
327 add_argv("--set-counters");
328 add_argv((char *) pcnt);
329 add_argv((char *) bcnt);
330 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000331
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000332 /* After fighting with strtok enough, here's now
333 * a 'real' parser. According to Rusty I'm now no
334 * longer a real hacker, but I can live with that */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000335
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000336 quote_open = 0;
337 param_start = parsestart;
338
339 for (curchar = parsestart; *curchar; curchar++) {
340 if (*curchar == '"') {
Michael Rash29cdbd02004-01-05 09:41:50 +0000341 /* quote_open cannot be true if there
342 * was no previous character. Thus,
343 * curchar-1 has to be within bounds */
344 if (quote_open &&
345 *(curchar-1) != '\\') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000346 quote_open = 0;
347 *curchar = ' ';
348 } else {
349 quote_open = 1;
350 param_start++;
351 }
352 }
353 if (*curchar == ' '
354 || *curchar == '\t'
355 || * curchar == '\n') {
356 char param_buffer[1024];
357 int param_len = curchar-param_start;
Harald Welte885c6eb2001-10-04 08:30:46 +0000358
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000359 if (quote_open)
360 continue;
Harald Welte885c6eb2001-10-04 08:30:46 +0000361
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000362 if (!param_len) {
363 /* two spaces? */
364 param_start++;
365 continue;
366 }
367
368 /* end of one parameter */
369 strncpy(param_buffer, param_start,
370 param_len);
371 *(param_buffer+param_len) = '\0';
Harald Weltebb41f882001-10-21 14:11:54 +0000372
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000373 /* check if table name specified */
Harald Welte48a6f902001-10-22 15:16:21 +0000374 if (!strncmp(param_buffer, "-t", 3)
375 || !strncmp(param_buffer, "--table", 8)) {
Harald Weltebb41f882001-10-21 14:11:54 +0000376 exit_error(PARAMETER_PROBLEM,
377 "Line %u seems to have a "
378 "-t table option.\n", line);
379 exit(1);
380 }
381
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000382 add_argv(param_buffer);
383 param_start += param_len + 1;
384 } else {
385 /* regular character, skip */
386 }
387 }
Harald Welte885c6eb2001-10-04 08:30:46 +0000388
389 DEBUGP("calling do_command6(%u, argv, &%s, handle):\n",
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000390 newargc, curtable);
Harald Welte885c6eb2001-10-04 08:30:46 +0000391
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000392 for (a = 0; a < newargc; a++)
András Kis-Szabó2f523792001-02-27 09:59:48 +0000393 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
394
Harald Welte885c6eb2001-10-04 08:30:46 +0000395 ret = do_command6(newargc, newargv,
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000396 &newargv[2], &handle);
Harald Welte885c6eb2001-10-04 08:30:46 +0000397
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000398 free_argv();
András Kis-Szabó2f523792001-02-27 09:59:48 +0000399 }
400 if (!ret) {
401 fprintf(stderr, "%s: line %u failed\n",
402 program_name, line);
403 exit(1);
404 }
405 }
Martin Josefssone0dc5812004-02-02 19:58:36 +0000406 if (in_table) {
407 fprintf(stderr, "%s: COMMIT expected at line %u\n",
408 program_name, line + 1);
409 exit(1);
410 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000411
412 return 0;
413}