blob: 76dddcba69b162b6718063fca0de97740817268c [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 *
Harald Weltedf1c71c2004-08-30 16:00:32 +000010 * $Id: ip6tables-restore.c,v 1.22 2004/05/26 16:04:48 gandalf Exp $
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
Harald Welte3efb6ea2001-08-06 18:50:21 +0000119#ifdef NO_SHARED_LIBS
120 init_extensions();
121#endif
122
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000123 while ((c = getopt_long(argc, argv, "bcvthnM:", options, NULL)) != -1) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000124 switch (c) {
125 case 'b':
126 binary = 1;
127 break;
128 case 'c':
129 counters = 1;
130 break;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000131 case 'v':
132 verbose = 1;
133 break;
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000134 case 't':
135 testing = 1;
136 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000137 case 'h':
138 print_usage("ip6tables-restore",
Harald Welte80fe35d2002-05-29 13:08:15 +0000139 IPTABLES_VERSION);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000140 break;
141 case 'n':
142 noflush = 1;
143 break;
Harald Welte58918652001-06-16 18:25:25 +0000144 case 'M':
145 modprobe = optarg;
146 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000147 }
148 }
149
150 if (optind == argc - 1) {
151 in = fopen(argv[optind], "r");
152 if (!in) {
153 fprintf(stderr, "Can't open %s: %s", argv[optind],
154 strerror(errno));
155 exit(1);
156 }
157 }
158 else if (optind < argc) {
159 fprintf(stderr, "Unknown arguments found on commandline");
160 exit(1);
161 }
162 else in = stdin;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000163
András Kis-Szabó2f523792001-02-27 09:59:48 +0000164 /* Grab standard input. */
165 while (fgets(buffer, sizeof(buffer), in)) {
Martin Josefssoncc536282004-02-02 20:14:56 +0000166 int ret = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000167
168 line++;
Martin Josefsson5065afa2004-01-31 19:33:47 +0000169 if (buffer[0] == '\n')
170 continue;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000171 else if (buffer[0] == '#') {
Martin Josefsson3673dcb2004-01-31 19:41:49 +0000172 if (verbose)
173 fputs(buffer, stdout);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000174 continue;
Harald Weltea8658ca2003-03-05 07:46:15 +0000175 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000176 if (!testing) {
177 DEBUGP("Calling commit\n");
178 ret = ip6tc_commit(&handle);
179 } else {
180 DEBUGP("Not calling commit, testing\n");
181 ret = 1;
182 }
Harald Weltea8658ca2003-03-05 07:46:15 +0000183 in_table = 0;
184 } else if ((buffer[0] == '*') && (!in_table)) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000185 /* New table */
186 char *table;
187
188 table = strtok(buffer+1, " \t\n");
189 DEBUGP("line %u, table '%s'\n", line, table);
190 if (!table) {
191 exit_error(PARAMETER_PROBLEM,
192 "%s: line %u table name invalid\n",
193 program_name, line);
194 exit(1);
195 }
196 strncpy(curtable, table, IP6T_TABLE_MAXNAMELEN);
Karsten Desler073df8f2004-01-31 15:33:55 +0000197 curtable[IP6T_TABLE_MAXNAMELEN] = '\0';
András Kis-Szabó2f523792001-02-27 09:59:48 +0000198
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000199 if (handle)
200 ip6tc_free(&handle);
201
Harald Welte58918652001-06-16 18:25:25 +0000202 handle = create_handle(table, modprobe);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000203 if (noflush == 0) {
204 DEBUGP("Cleaning all chains of table '%s'\n",
205 table);
206 for_each_chain(flush_entries, verbose, 1,
207 &handle);
208
209 DEBUGP("Deleting all user-defined chains "
210 "of table '%s'\n", table);
211 for_each_chain(delete_chain, verbose, 0,
212 &handle) ;
213 }
214
215 ret = 1;
Harald Weltea8658ca2003-03-05 07:46:15 +0000216 in_table = 1;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000217
Harald Weltea8658ca2003-03-05 07:46:15 +0000218 } else if ((buffer[0] == ':') && (in_table)) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000219 /* New chain. */
220 char *policy, *chain;
221
222 chain = strtok(buffer+1, " \t\n");
223 DEBUGP("line %u, chain '%s'\n", line, chain);
224 if (!chain) {
225 exit_error(PARAMETER_PROBLEM,
226 "%s: line %u chain name invalid\n",
227 program_name, line);
228 exit(1);
229 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000230
Harald Weltedf1c71c2004-08-30 16:00:32 +0000231 if (ip6tc_builtin(chain, handle) <= 0) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000232 DEBUGP("Creating new chain '%s'\n", chain);
Harald Welte885c6eb2001-10-04 08:30:46 +0000233 if (!ip6tc_create_chain(chain, &handle))
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000234 exit_error(PARAMETER_PROBLEM,
235 "error creating chain "
236 "'%s':%s\n", chain,
237 strerror(errno));
Harald Welte885c6eb2001-10-04 08:30:46 +0000238 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000239
240 policy = strtok(NULL, " \t\n");
241 DEBUGP("line %u, policy '%s'\n", line, policy);
242 if (!policy) {
243 exit_error(PARAMETER_PROBLEM,
244 "%s: line %u policy invalid\n",
245 program_name, line);
246 exit(1);
247 }
248
249 if (strcmp(policy, "-") != 0) {
250 struct ip6t_counters count;
251
252 if (counters) {
253 char *ctrs;
254 ctrs = strtok(NULL, " \t\n");
255
256 parse_counters(ctrs, &count);
257
258 } else {
259 memset(&count, 0,
260 sizeof(struct ip6t_counters));
261 }
262
263 DEBUGP("Setting policy of chain %s to %s\n",
264 chain, policy);
265
266 if (!ip6tc_set_policy(chain, policy, &count,
267 &handle))
268 exit_error(OTHER_PROBLEM,
269 "Can't set policy `%s'"
270 " on `%s' line %u: %s\n",
271 chain, policy, line,
272 ip6tc_strerror(errno));
273 }
274
275 ret = 1;
276
Harald Weltea8658ca2003-03-05 07:46:15 +0000277 } else if (in_table) {
Harald Welte885c6eb2001-10-04 08:30:46 +0000278 int a;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000279 char *ptr = buffer;
280 char *pcnt = NULL;
281 char *bcnt = NULL;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000282 char *parsestart;
Harald Welte885c6eb2001-10-04 08:30:46 +0000283
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000284 /* the parser */
285 char *param_start, *curchar;
286 int quote_open;
Harald Welte885c6eb2001-10-04 08:30:46 +0000287
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000288 /* reset the newargv */
289 newargc = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000290
291 if (buffer[0] == '[') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000292 /* we have counters in our input */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000293 ptr = strchr(buffer, ']');
294 if (!ptr)
295 exit_error(PARAMETER_PROBLEM,
296 "Bad line %u: need ]\n",
297 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000298
András Kis-Szabó2f523792001-02-27 09:59:48 +0000299 pcnt = strtok(buffer+1, ":");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000300 if (!pcnt)
301 exit_error(PARAMETER_PROBLEM,
302 "Bad line %u: need :\n",
303 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000304
András Kis-Szabó2f523792001-02-27 09:59:48 +0000305 bcnt = strtok(NULL, "]");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000306 if (!bcnt)
307 exit_error(PARAMETER_PROBLEM,
308 "Bad line %u: need ]\n",
309 line);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000310
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000311 /* start command parsing after counter */
312 parsestart = ptr + 1;
313 } else {
314 /* start command parsing at start of line */
315 parsestart = buffer;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000316 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000317
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000318 add_argv(argv[0]);
319 add_argv("-t");
320 add_argv((char *) &curtable);
321
322 if (counters && pcnt && bcnt) {
323 add_argv("--set-counters");
324 add_argv((char *) pcnt);
325 add_argv((char *) bcnt);
326 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000327
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000328 /* After fighting with strtok enough, here's now
329 * a 'real' parser. According to Rusty I'm now no
330 * longer a real hacker, but I can live with that */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000331
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000332 quote_open = 0;
333 param_start = parsestart;
334
335 for (curchar = parsestart; *curchar; curchar++) {
336 if (*curchar == '"') {
Michael Rash29cdbd02004-01-05 09:41:50 +0000337 /* quote_open cannot be true if there
338 * was no previous character. Thus,
339 * curchar-1 has to be within bounds */
340 if (quote_open &&
341 *(curchar-1) != '\\') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000342 quote_open = 0;
343 *curchar = ' ';
344 } else {
345 quote_open = 1;
346 param_start++;
347 }
348 }
349 if (*curchar == ' '
350 || *curchar == '\t'
351 || * curchar == '\n') {
352 char param_buffer[1024];
353 int param_len = curchar-param_start;
Harald Welte885c6eb2001-10-04 08:30:46 +0000354
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000355 if (quote_open)
356 continue;
Harald Welte885c6eb2001-10-04 08:30:46 +0000357
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000358 if (!param_len) {
359 /* two spaces? */
360 param_start++;
361 continue;
362 }
363
364 /* end of one parameter */
365 strncpy(param_buffer, param_start,
366 param_len);
367 *(param_buffer+param_len) = '\0';
Harald Weltebb41f882001-10-21 14:11:54 +0000368
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000369 /* check if table name specified */
Harald Welte48a6f902001-10-22 15:16:21 +0000370 if (!strncmp(param_buffer, "-t", 3)
371 || !strncmp(param_buffer, "--table", 8)) {
Harald Weltebb41f882001-10-21 14:11:54 +0000372 exit_error(PARAMETER_PROBLEM,
373 "Line %u seems to have a "
374 "-t table option.\n", line);
375 exit(1);
376 }
377
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000378 add_argv(param_buffer);
379 param_start += param_len + 1;
380 } else {
381 /* regular character, skip */
382 }
383 }
Harald Welte885c6eb2001-10-04 08:30:46 +0000384
385 DEBUGP("calling do_command6(%u, argv, &%s, handle):\n",
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000386 newargc, curtable);
Harald Welte885c6eb2001-10-04 08:30:46 +0000387
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000388 for (a = 0; a < newargc; a++)
András Kis-Szabó2f523792001-02-27 09:59:48 +0000389 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
390
Harald Welte885c6eb2001-10-04 08:30:46 +0000391 ret = do_command6(newargc, newargv,
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000392 &newargv[2], &handle);
Harald Welte885c6eb2001-10-04 08:30:46 +0000393
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000394 free_argv();
András Kis-Szabó2f523792001-02-27 09:59:48 +0000395 }
396 if (!ret) {
397 fprintf(stderr, "%s: line %u failed\n",
398 program_name, line);
399 exit(1);
400 }
401 }
Martin Josefssone0dc5812004-02-02 19:58:36 +0000402 if (in_table) {
403 fprintf(stderr, "%s: COMMIT expected at line %u\n",
404 program_name, line + 1);
405 exit(1);
406 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000407
408 return 0;
409}