blob: 287e86406e76c0facb9bcaa109f4c9d8b287d4e8 [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 Josefsson3673dcb2004-01-31 19:41:49 +000010 * $Id: ip6tables-restore.c,v 1.16 2004/01/31 19:33:47 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' },
András Kis-Szabó2f523792001-02-27 09:59:48 +000034 { "help", 0, 0, 'h' },
35 { "noflush", 0, 0, 'n'},
Harald Welte58918652001-06-16 18:25:25 +000036 { "modprobe", 1, 0, 'M'},
András Kis-Szabó2f523792001-02-27 09:59:48 +000037 { 0 }
38};
39
40static void print_usage(const char *name, const char *version) __attribute__((noreturn));
41
42static void print_usage(const char *name, const char *version)
43{
44 fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-h]\n"
45 " [ --binary ]\n"
46 " [ --counters ]\n"
47 " [ --verbose ]\n"
48 " [ --help ]\n"
Harald Welte58918652001-06-16 18:25:25 +000049 " [ --noflush ]\n"
50 " [ --modprobe=<command>]\n", name);
András Kis-Szabó2f523792001-02-27 09:59:48 +000051
52 exit(1);
53}
54
Harald Welte58918652001-06-16 18:25:25 +000055ip6tc_handle_t create_handle(const char *tablename, const char* modprobe)
András Kis-Szabó2f523792001-02-27 09:59:48 +000056{
57 ip6tc_handle_t handle;
58
59 handle = ip6tc_init(tablename);
Harald Welte58918652001-06-16 18:25:25 +000060
61 if (!handle) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000062 /* try to insmod the module if iptc_init failed */
63 ip6tables_insmod("ip6_tables", modprobe);
64 handle = ip6tc_init(tablename);
Harald Welte58918652001-06-16 18:25:25 +000065 }
66
András Kis-Szabó2f523792001-02-27 09:59:48 +000067 if (!handle) {
68 exit_error(PARAMETER_PROBLEM, "%s: unable to initialize"
69 "table '%s'\n", program_name, tablename);
70 exit(1);
71 }
72 return handle;
73}
74
75int parse_counters(char *string, struct ip6t_counters *ctr)
76{
77 return (sscanf(string, "[%llu:%llu]", &ctr->pcnt, &ctr->bcnt) == 2);
78}
79
Harald Welte885c6eb2001-10-04 08:30:46 +000080/* global new argv and argc */
81static char *newargv[255];
82static int newargc;
83
84/* function adding one argument to newargv, updating newargc
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000085 * returns true if argument added, false otherwise */
Harald Welte885c6eb2001-10-04 08:30:46 +000086static int add_argv(char *what) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000087 DEBUGP("add_argv: %s\n", what);
88 if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) {
89 newargv[newargc] = strdup(what);
90 newargc++;
91 return 1;
92 } else
93 return 0;
Harald Welte885c6eb2001-10-04 08:30:46 +000094}
95
96static void free_argv(void) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000097 int i;
Harald Welte885c6eb2001-10-04 08:30:46 +000098
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000099 for (i = 0; i < newargc; i++)
100 free(newargv[i]);
Harald Welte885c6eb2001-10-04 08:30:46 +0000101}
102
András Kis-Szabó2f523792001-02-27 09:59:48 +0000103int main(int argc, char *argv[])
104{
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000105 ip6tc_handle_t handle = NULL;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000106 char buffer[10240];
András Kis-Szabó2f523792001-02-27 09:59:48 +0000107 int c;
108 char curtable[IP6T_TABLE_MAXNAMELEN + 1];
András Kis-Szabó2f523792001-02-27 09:59:48 +0000109 FILE *in;
Harald Welte58918652001-06-16 18:25:25 +0000110 const char *modprobe = 0;
Harald Weltea8658ca2003-03-05 07:46:15 +0000111 int in_table = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000112
113 program_name = "ip6tables-restore";
Harald Welte80fe35d2002-05-29 13:08:15 +0000114 program_version = IPTABLES_VERSION;
Harald Weltea8658ca2003-03-05 07:46:15 +0000115 line = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000116
Harald Welte3efb6ea2001-08-06 18:50:21 +0000117#ifdef NO_SHARED_LIBS
118 init_extensions();
119#endif
120
Harald Welte58918652001-06-16 18:25:25 +0000121 while ((c = getopt_long(argc, argv, "bcvhnM:", options, NULL)) != -1) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000122 switch (c) {
123 case 'b':
124 binary = 1;
125 break;
126 case 'c':
127 counters = 1;
128 break;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000129 case 'v':
130 verbose = 1;
131 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000132 case 'h':
133 print_usage("ip6tables-restore",
Harald Welte80fe35d2002-05-29 13:08:15 +0000134 IPTABLES_VERSION);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000135 break;
136 case 'n':
137 noflush = 1;
138 break;
Harald Welte58918652001-06-16 18:25:25 +0000139 case 'M':
140 modprobe = optarg;
141 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000142 }
143 }
144
145 if (optind == argc - 1) {
146 in = fopen(argv[optind], "r");
147 if (!in) {
148 fprintf(stderr, "Can't open %s: %s", argv[optind],
149 strerror(errno));
150 exit(1);
151 }
152 }
153 else if (optind < argc) {
154 fprintf(stderr, "Unknown arguments found on commandline");
155 exit(1);
156 }
157 else in = stdin;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000158
András Kis-Szabó2f523792001-02-27 09:59:48 +0000159 /* Grab standard input. */
160 while (fgets(buffer, sizeof(buffer), in)) {
161 int ret;
162
163 line++;
Martin Josefsson5065afa2004-01-31 19:33:47 +0000164 if (buffer[0] == '\n')
165 continue;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000166 else if (buffer[0] == '#') {
Martin Josefsson3673dcb2004-01-31 19:41:49 +0000167 if (verbose)
168 fputs(buffer, stdout);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000169 continue;
Harald Weltea8658ca2003-03-05 07:46:15 +0000170 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000171 DEBUGP("Calling commit\n");
172 ret = ip6tc_commit(&handle);
Harald Weltea8658ca2003-03-05 07:46:15 +0000173 in_table = 0;
174 } else if ((buffer[0] == '*') && (!in_table)) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000175 /* New table */
176 char *table;
177
178 table = strtok(buffer+1, " \t\n");
179 DEBUGP("line %u, table '%s'\n", line, table);
180 if (!table) {
181 exit_error(PARAMETER_PROBLEM,
182 "%s: line %u table name invalid\n",
183 program_name, line);
184 exit(1);
185 }
186 strncpy(curtable, table, IP6T_TABLE_MAXNAMELEN);
Karsten Desler073df8f2004-01-31 15:33:55 +0000187 curtable[IP6T_TABLE_MAXNAMELEN] = '\0';
András Kis-Szabó2f523792001-02-27 09:59:48 +0000188
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000189 if (handle)
190 ip6tc_free(&handle);
191
Harald Welte58918652001-06-16 18:25:25 +0000192 handle = create_handle(table, modprobe);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000193 if (noflush == 0) {
194 DEBUGP("Cleaning all chains of table '%s'\n",
195 table);
196 for_each_chain(flush_entries, verbose, 1,
197 &handle);
198
199 DEBUGP("Deleting all user-defined chains "
200 "of table '%s'\n", table);
201 for_each_chain(delete_chain, verbose, 0,
202 &handle) ;
203 }
204
205 ret = 1;
Harald Weltea8658ca2003-03-05 07:46:15 +0000206 in_table = 1;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000207
Harald Weltea8658ca2003-03-05 07:46:15 +0000208 } else if ((buffer[0] == ':') && (in_table)) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000209 /* New chain. */
210 char *policy, *chain;
211
212 chain = strtok(buffer+1, " \t\n");
213 DEBUGP("line %u, chain '%s'\n", line, chain);
214 if (!chain) {
215 exit_error(PARAMETER_PROBLEM,
216 "%s: line %u chain name invalid\n",
217 program_name, line);
218 exit(1);
219 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000220
Harald Welte885c6eb2001-10-04 08:30:46 +0000221 if (!ip6tc_builtin(chain, handle)) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000222 DEBUGP("Creating new chain '%s'\n", chain);
Harald Welte885c6eb2001-10-04 08:30:46 +0000223 if (!ip6tc_create_chain(chain, &handle))
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000224 exit_error(PARAMETER_PROBLEM,
225 "error creating chain "
226 "'%s':%s\n", chain,
227 strerror(errno));
Harald Welte885c6eb2001-10-04 08:30:46 +0000228 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000229
230 policy = strtok(NULL, " \t\n");
231 DEBUGP("line %u, policy '%s'\n", line, policy);
232 if (!policy) {
233 exit_error(PARAMETER_PROBLEM,
234 "%s: line %u policy invalid\n",
235 program_name, line);
236 exit(1);
237 }
238
239 if (strcmp(policy, "-") != 0) {
240 struct ip6t_counters count;
241
242 if (counters) {
243 char *ctrs;
244 ctrs = strtok(NULL, " \t\n");
245
246 parse_counters(ctrs, &count);
247
248 } else {
249 memset(&count, 0,
250 sizeof(struct ip6t_counters));
251 }
252
253 DEBUGP("Setting policy of chain %s to %s\n",
254 chain, policy);
255
256 if (!ip6tc_set_policy(chain, policy, &count,
257 &handle))
258 exit_error(OTHER_PROBLEM,
259 "Can't set policy `%s'"
260 " on `%s' line %u: %s\n",
261 chain, policy, line,
262 ip6tc_strerror(errno));
263 }
264
265 ret = 1;
266
Harald Weltea8658ca2003-03-05 07:46:15 +0000267 } else if (in_table) {
Harald Welte885c6eb2001-10-04 08:30:46 +0000268 int a;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000269 char *ptr = buffer;
270 char *pcnt = NULL;
271 char *bcnt = NULL;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000272 char *parsestart;
Harald Welte885c6eb2001-10-04 08:30:46 +0000273
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000274 /* the parser */
275 char *param_start, *curchar;
276 int quote_open;
Harald Welte885c6eb2001-10-04 08:30:46 +0000277
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000278 /* reset the newargv */
279 newargc = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000280
281 if (buffer[0] == '[') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000282 /* we have counters in our input */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000283 ptr = strchr(buffer, ']');
284 if (!ptr)
285 exit_error(PARAMETER_PROBLEM,
286 "Bad line %u: need ]\n",
287 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000288
András Kis-Szabó2f523792001-02-27 09:59:48 +0000289 pcnt = strtok(buffer+1, ":");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000290 if (!pcnt)
291 exit_error(PARAMETER_PROBLEM,
292 "Bad line %u: need :\n",
293 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000294
András Kis-Szabó2f523792001-02-27 09:59:48 +0000295 bcnt = strtok(NULL, "]");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000296 if (!bcnt)
297 exit_error(PARAMETER_PROBLEM,
298 "Bad line %u: need ]\n",
299 line);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000300
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000301 /* start command parsing after counter */
302 parsestart = ptr + 1;
303 } else {
304 /* start command parsing at start of line */
305 parsestart = buffer;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000306 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000307
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000308 add_argv(argv[0]);
309 add_argv("-t");
310 add_argv((char *) &curtable);
311
312 if (counters && pcnt && bcnt) {
313 add_argv("--set-counters");
314 add_argv((char *) pcnt);
315 add_argv((char *) bcnt);
316 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000317
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000318 /* After fighting with strtok enough, here's now
319 * a 'real' parser. According to Rusty I'm now no
320 * longer a real hacker, but I can live with that */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000321
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000322 quote_open = 0;
323 param_start = parsestart;
324
325 for (curchar = parsestart; *curchar; curchar++) {
326 if (*curchar == '"') {
Michael Rash29cdbd02004-01-05 09:41:50 +0000327 /* quote_open cannot be true if there
328 * was no previous character. Thus,
329 * curchar-1 has to be within bounds */
330 if (quote_open &&
331 *(curchar-1) != '\\') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000332 quote_open = 0;
333 *curchar = ' ';
334 } else {
335 quote_open = 1;
336 param_start++;
337 }
338 }
339 if (*curchar == ' '
340 || *curchar == '\t'
341 || * curchar == '\n') {
342 char param_buffer[1024];
343 int param_len = curchar-param_start;
Harald Welte885c6eb2001-10-04 08:30:46 +0000344
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000345 if (quote_open)
346 continue;
Harald Welte885c6eb2001-10-04 08:30:46 +0000347
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000348 if (!param_len) {
349 /* two spaces? */
350 param_start++;
351 continue;
352 }
353
354 /* end of one parameter */
355 strncpy(param_buffer, param_start,
356 param_len);
357 *(param_buffer+param_len) = '\0';
Harald Weltebb41f882001-10-21 14:11:54 +0000358
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000359 /* check if table name specified */
Harald Welte48a6f902001-10-22 15:16:21 +0000360 if (!strncmp(param_buffer, "-t", 3)
361 || !strncmp(param_buffer, "--table", 8)) {
Harald Weltebb41f882001-10-21 14:11:54 +0000362 exit_error(PARAMETER_PROBLEM,
363 "Line %u seems to have a "
364 "-t table option.\n", line);
365 exit(1);
366 }
367
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000368 add_argv(param_buffer);
369 param_start += param_len + 1;
370 } else {
371 /* regular character, skip */
372 }
373 }
Harald Welte885c6eb2001-10-04 08:30:46 +0000374
375 DEBUGP("calling do_command6(%u, argv, &%s, handle):\n",
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000376 newargc, curtable);
Harald Welte885c6eb2001-10-04 08:30:46 +0000377
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000378 for (a = 0; a < newargc; a++)
András Kis-Szabó2f523792001-02-27 09:59:48 +0000379 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
380
Harald Welte885c6eb2001-10-04 08:30:46 +0000381 ret = do_command6(newargc, newargv,
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000382 &newargv[2], &handle);
Harald Welte885c6eb2001-10-04 08:30:46 +0000383
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000384 free_argv();
András Kis-Szabó2f523792001-02-27 09:59:48 +0000385 }
386 if (!ret) {
387 fprintf(stderr, "%s: line %u failed\n",
388 program_name, line);
389 exit(1);
390 }
391 }
392
393 return 0;
394}