blob: d75507d6e7a46acf81e39d567db0a5bfc1b92a78 [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>
8 *
András Kis-Szabó97fd91a2002-03-03 09:44:31 +00009 * $Id: ip6tables-restore.c,v 1.20 2002/01/17 20:43:10 laforge Exp $
András Kis-Szabó2f523792001-02-27 09:59:48 +000010 */
11
12#include <getopt.h>
13#include <sys/errno.h>
14#include <string.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include "ip6tables.h"
18#include "libiptc/libip6tc.h"
19
20#ifdef DEBUG
21#define DEBUGP(x, args...) fprintf(stderr, x, ## args)
22#else
23#define DEBUGP(x, args...)
24#endif
25
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000026/*
András Kis-Szabó2f523792001-02-27 09:59:48 +000027extern int for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *), int verbose, int builtinstoo, ip6tc_handle_t *handle);
28extern int flush_entries(const ip6t_chainlabel chain, int verbose, ip6tc_handle_t *handle);
29extern int delete_chain(const ip6t_chainlabel chain, int verbose, ip6tc_handle_t *handle);
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000030*/
András Kis-Szabó2f523792001-02-27 09:59:48 +000031
32static int binary = 0, counters = 0, verbose = 0, noflush = 0;
33
34/* Keeping track of external matches and targets. */
35static struct option options[] = {
36 { "binary", 0, 0, 'b' },
37 { "counters", 0, 0, 'c' },
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000038 { "verbose", 1, 0, 'v' },
András Kis-Szabó2f523792001-02-27 09:59:48 +000039 { "help", 0, 0, 'h' },
40 { "noflush", 0, 0, 'n'},
Harald Welte58918652001-06-16 18:25:25 +000041 { "modprobe", 1, 0, 'M'},
András Kis-Szabó2f523792001-02-27 09:59:48 +000042 { 0 }
43};
44
45static void print_usage(const char *name, const char *version) __attribute__((noreturn));
46
47static void print_usage(const char *name, const char *version)
48{
49 fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-h]\n"
50 " [ --binary ]\n"
51 " [ --counters ]\n"
52 " [ --verbose ]\n"
53 " [ --help ]\n"
Harald Welte58918652001-06-16 18:25:25 +000054 " [ --noflush ]\n"
55 " [ --modprobe=<command>]\n", name);
András Kis-Szabó2f523792001-02-27 09:59:48 +000056
57 exit(1);
58}
59
Harald Welte58918652001-06-16 18:25:25 +000060ip6tc_handle_t create_handle(const char *tablename, const char* modprobe)
András Kis-Szabó2f523792001-02-27 09:59:48 +000061{
62 ip6tc_handle_t handle;
63
64 handle = ip6tc_init(tablename);
Harald Welte58918652001-06-16 18:25:25 +000065
66 if (!handle) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000067 /* try to insmod the module if iptc_init failed */
68 ip6tables_insmod("ip6_tables", modprobe);
69 handle = ip6tc_init(tablename);
Harald Welte58918652001-06-16 18:25:25 +000070 }
71
András Kis-Szabó2f523792001-02-27 09:59:48 +000072 if (!handle) {
73 exit_error(PARAMETER_PROBLEM, "%s: unable to initialize"
74 "table '%s'\n", program_name, tablename);
75 exit(1);
76 }
77 return handle;
78}
79
80int parse_counters(char *string, struct ip6t_counters *ctr)
81{
82 return (sscanf(string, "[%llu:%llu]", &ctr->pcnt, &ctr->bcnt) == 2);
83}
84
Harald Welte885c6eb2001-10-04 08:30:46 +000085/* global new argv and argc */
86static char *newargv[255];
87static int newargc;
88
89/* function adding one argument to newargv, updating newargc
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000090 * returns true if argument added, false otherwise */
Harald Welte885c6eb2001-10-04 08:30:46 +000091static int add_argv(char *what) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000092 DEBUGP("add_argv: %s\n", what);
93 if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) {
94 newargv[newargc] = strdup(what);
95 newargc++;
96 return 1;
97 } else
98 return 0;
Harald Welte885c6eb2001-10-04 08:30:46 +000099}
100
101static void free_argv(void) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000102 int i;
Harald Welte885c6eb2001-10-04 08:30:46 +0000103
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000104 for (i = 0; i < newargc; i++)
105 free(newargv[i]);
Harald Welte885c6eb2001-10-04 08:30:46 +0000106}
107
András Kis-Szabó2f523792001-02-27 09:59:48 +0000108int main(int argc, char *argv[])
109{
110 ip6tc_handle_t handle;
111 char buffer[10240];
112 unsigned int line = 0;
113 int c;
114 char curtable[IP6T_TABLE_MAXNAMELEN + 1];
András Kis-Szabó2f523792001-02-27 09:59:48 +0000115 FILE *in;
Harald Welte58918652001-06-16 18:25:25 +0000116 const char *modprobe = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000117
118 program_name = "ip6tables-restore";
119 program_version = NETFILTER_VERSION;
120
Harald Welte3efb6ea2001-08-06 18:50:21 +0000121#ifdef NO_SHARED_LIBS
122 init_extensions();
123#endif
124
Harald Welte58918652001-06-16 18:25:25 +0000125 while ((c = getopt_long(argc, argv, "bcvhnM:", options, NULL)) != -1) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000126 switch (c) {
127 case 'b':
128 binary = 1;
129 break;
130 case 'c':
131 counters = 1;
132 break;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000133 case 'v':
134 verbose = 1;
135 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000136 case 'h':
137 print_usage("ip6tables-restore",
138 NETFILTER_VERSION);
139 break;
140 case 'n':
141 noflush = 1;
142 break;
Harald Welte58918652001-06-16 18:25:25 +0000143 case 'M':
144 modprobe = optarg;
145 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000146 }
147 }
148
149 if (optind == argc - 1) {
150 in = fopen(argv[optind], "r");
151 if (!in) {
152 fprintf(stderr, "Can't open %s: %s", argv[optind],
153 strerror(errno));
154 exit(1);
155 }
156 }
157 else if (optind < argc) {
158 fprintf(stderr, "Unknown arguments found on commandline");
159 exit(1);
160 }
161 else in = stdin;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000162
András Kis-Szabó2f523792001-02-27 09:59:48 +0000163 /* Grab standard input. */
164 while (fgets(buffer, sizeof(buffer), in)) {
165 int ret;
166
167 line++;
168 if (buffer[0] == '\n') continue;
169 else if (buffer[0] == '#') {
170 if (verbose) fputs(buffer, stdout);
171 continue;
172 } else if (strcmp(buffer, "COMMIT\n") == 0) {
173 DEBUGP("Calling commit\n");
174 ret = ip6tc_commit(&handle);
175 } else if (buffer[0] == '*') {
176 /* New table */
177 char *table;
178
179 table = strtok(buffer+1, " \t\n");
180 DEBUGP("line %u, table '%s'\n", line, table);
181 if (!table) {
182 exit_error(PARAMETER_PROBLEM,
183 "%s: line %u table name invalid\n",
184 program_name, line);
185 exit(1);
186 }
187 strncpy(curtable, table, IP6T_TABLE_MAXNAMELEN);
188
Harald Welte58918652001-06-16 18:25:25 +0000189 handle = create_handle(table, modprobe);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000190 if (noflush == 0) {
191 DEBUGP("Cleaning all chains of table '%s'\n",
192 table);
193 for_each_chain(flush_entries, verbose, 1,
194 &handle);
195
196 DEBUGP("Deleting all user-defined chains "
197 "of table '%s'\n", table);
198 for_each_chain(delete_chain, verbose, 0,
199 &handle) ;
200 }
201
202 ret = 1;
203
204 } else if (buffer[0] == ':') {
205 /* New chain. */
206 char *policy, *chain;
207
208 chain = strtok(buffer+1, " \t\n");
209 DEBUGP("line %u, chain '%s'\n", line, chain);
210 if (!chain) {
211 exit_error(PARAMETER_PROBLEM,
212 "%s: line %u chain name invalid\n",
213 program_name, line);
214 exit(1);
215 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000216
Harald Welte885c6eb2001-10-04 08:30:46 +0000217 if (!ip6tc_builtin(chain, handle)) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000218 DEBUGP("Creating new chain '%s'\n", chain);
Harald Welte885c6eb2001-10-04 08:30:46 +0000219 if (!ip6tc_create_chain(chain, &handle))
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000220 exit_error(PARAMETER_PROBLEM,
221 "error creating chain "
222 "'%s':%s\n", chain,
223 strerror(errno));
Harald Welte885c6eb2001-10-04 08:30:46 +0000224 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000225
226 policy = strtok(NULL, " \t\n");
227 DEBUGP("line %u, policy '%s'\n", line, policy);
228 if (!policy) {
229 exit_error(PARAMETER_PROBLEM,
230 "%s: line %u policy invalid\n",
231 program_name, line);
232 exit(1);
233 }
234
235 if (strcmp(policy, "-") != 0) {
236 struct ip6t_counters count;
237
238 if (counters) {
239 char *ctrs;
240 ctrs = strtok(NULL, " \t\n");
241
242 parse_counters(ctrs, &count);
243
244 } else {
245 memset(&count, 0,
246 sizeof(struct ip6t_counters));
247 }
248
249 DEBUGP("Setting policy of chain %s to %s\n",
250 chain, policy);
251
252 if (!ip6tc_set_policy(chain, policy, &count,
253 &handle))
254 exit_error(OTHER_PROBLEM,
255 "Can't set policy `%s'"
256 " on `%s' line %u: %s\n",
257 chain, policy, line,
258 ip6tc_strerror(errno));
259 }
260
261 ret = 1;
262
263 } else {
Harald Welte885c6eb2001-10-04 08:30:46 +0000264 int a;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000265 char *ptr = buffer;
266 char *pcnt = NULL;
267 char *bcnt = NULL;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000268 char *parsestart;
Harald Welte885c6eb2001-10-04 08:30:46 +0000269
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000270 /* the parser */
271 char *param_start, *curchar;
272 int quote_open;
Harald Welte885c6eb2001-10-04 08:30:46 +0000273
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000274 /* reset the newargv */
275 newargc = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000276
277 if (buffer[0] == '[') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000278 /* we have counters in our input */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000279 ptr = strchr(buffer, ']');
280 if (!ptr)
281 exit_error(PARAMETER_PROBLEM,
282 "Bad line %u: need ]\n",
283 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000284
András Kis-Szabó2f523792001-02-27 09:59:48 +0000285 pcnt = strtok(buffer+1, ":");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000286 if (!pcnt)
287 exit_error(PARAMETER_PROBLEM,
288 "Bad line %u: need :\n",
289 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000290
András Kis-Szabó2f523792001-02-27 09:59:48 +0000291 bcnt = strtok(NULL, "]");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000292 if (!bcnt)
293 exit_error(PARAMETER_PROBLEM,
294 "Bad line %u: need ]\n",
295 line);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000296
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000297 /* start command parsing after counter */
298 parsestart = ptr + 1;
299 } else {
300 /* start command parsing at start of line */
301 parsestart = buffer;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000302 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000303
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000304 add_argv(argv[0]);
305 add_argv("-t");
306 add_argv((char *) &curtable);
307
308 if (counters && pcnt && bcnt) {
309 add_argv("--set-counters");
310 add_argv((char *) pcnt);
311 add_argv((char *) bcnt);
312 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000313
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000314 /* After fighting with strtok enough, here's now
315 * a 'real' parser. According to Rusty I'm now no
316 * longer a real hacker, but I can live with that */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000317
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000318 quote_open = 0;
319 param_start = parsestart;
320
321 for (curchar = parsestart; *curchar; curchar++) {
322 if (*curchar == '"') {
323 if (quote_open) {
324 quote_open = 0;
325 *curchar = ' ';
326 } else {
327 quote_open = 1;
328 param_start++;
329 }
330 }
331 if (*curchar == ' '
332 || *curchar == '\t'
333 || * curchar == '\n') {
334 char param_buffer[1024];
335 int param_len = curchar-param_start;
Harald Welte885c6eb2001-10-04 08:30:46 +0000336
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000337 if (quote_open)
338 continue;
Harald Welte885c6eb2001-10-04 08:30:46 +0000339
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000340 if (!param_len) {
341 /* two spaces? */
342 param_start++;
343 continue;
344 }
345
346 /* end of one parameter */
347 strncpy(param_buffer, param_start,
348 param_len);
349 *(param_buffer+param_len) = '\0';
Harald Weltebb41f882001-10-21 14:11:54 +0000350
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000351 /* check if table name specified */
Harald Welte48a6f902001-10-22 15:16:21 +0000352 if (!strncmp(param_buffer, "-t", 3)
353 || !strncmp(param_buffer, "--table", 8)) {
Harald Weltebb41f882001-10-21 14:11:54 +0000354 exit_error(PARAMETER_PROBLEM,
355 "Line %u seems to have a "
356 "-t table option.\n", line);
357 exit(1);
358 }
359
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000360 add_argv(param_buffer);
361 param_start += param_len + 1;
362 } else {
363 /* regular character, skip */
364 }
365 }
Harald Welte885c6eb2001-10-04 08:30:46 +0000366
367 DEBUGP("calling do_command6(%u, argv, &%s, handle):\n",
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000368 newargc, curtable);
Harald Welte885c6eb2001-10-04 08:30:46 +0000369
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000370 for (a = 0; a < newargc; a++)
András Kis-Szabó2f523792001-02-27 09:59:48 +0000371 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
372
Harald Welte885c6eb2001-10-04 08:30:46 +0000373 ret = do_command6(newargc, newargv,
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000374 &newargv[2], &handle);
Harald Welte885c6eb2001-10-04 08:30:46 +0000375
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000376 free_argv();
András Kis-Szabó2f523792001-02-27 09:59:48 +0000377 }
378 if (!ret) {
379 fprintf(stderr, "%s: line %u failed\n",
380 program_name, line);
381 exit(1);
382 }
383 }
384
385 return 0;
386}