blob: 668011334bcb6561e7be9bc6fe0a1a6e5417d233 [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 *
András Kis-Szabó0c4188f2002-08-14 11:40:41 +000010 * $Id: ip6tables-restore.c,v 1.9 2002/05/29 13:08:15 laforge 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' },
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000033 { "verbose", 1, 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{
105 ip6tc_handle_t handle;
106 char buffer[10240];
107 unsigned int line = 0;
108 int c;
109 char curtable[IP6T_TABLE_MAXNAMELEN + 1];
András Kis-Szabó2f523792001-02-27 09:59:48 +0000110 FILE *in;
Harald Welte58918652001-06-16 18:25:25 +0000111 const char *modprobe = 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;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000115
Harald Welte3efb6ea2001-08-06 18:50:21 +0000116#ifdef NO_SHARED_LIBS
117 init_extensions();
118#endif
119
Harald Welte58918652001-06-16 18:25:25 +0000120 while ((c = getopt_long(argc, argv, "bcvhnM:", options, NULL)) != -1) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000121 switch (c) {
122 case 'b':
123 binary = 1;
124 break;
125 case 'c':
126 counters = 1;
127 break;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000128 case 'v':
129 verbose = 1;
130 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000131 case 'h':
132 print_usage("ip6tables-restore",
Harald Welte80fe35d2002-05-29 13:08:15 +0000133 IPTABLES_VERSION);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000134 break;
135 case 'n':
136 noflush = 1;
137 break;
Harald Welte58918652001-06-16 18:25:25 +0000138 case 'M':
139 modprobe = optarg;
140 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000141 }
142 }
143
144 if (optind == argc - 1) {
145 in = fopen(argv[optind], "r");
146 if (!in) {
147 fprintf(stderr, "Can't open %s: %s", argv[optind],
148 strerror(errno));
149 exit(1);
150 }
151 }
152 else if (optind < argc) {
153 fprintf(stderr, "Unknown arguments found on commandline");
154 exit(1);
155 }
156 else in = stdin;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000157
András Kis-Szabó2f523792001-02-27 09:59:48 +0000158 /* Grab standard input. */
159 while (fgets(buffer, sizeof(buffer), in)) {
160 int ret;
161
162 line++;
163 if (buffer[0] == '\n') continue;
164 else if (buffer[0] == '#') {
165 if (verbose) fputs(buffer, stdout);
166 continue;
167 } else if (strcmp(buffer, "COMMIT\n") == 0) {
168 DEBUGP("Calling commit\n");
169 ret = ip6tc_commit(&handle);
170 } else if (buffer[0] == '*') {
171 /* New table */
172 char *table;
173
174 table = strtok(buffer+1, " \t\n");
175 DEBUGP("line %u, table '%s'\n", line, table);
176 if (!table) {
177 exit_error(PARAMETER_PROBLEM,
178 "%s: line %u table name invalid\n",
179 program_name, line);
180 exit(1);
181 }
182 strncpy(curtable, table, IP6T_TABLE_MAXNAMELEN);
183
Harald Welte58918652001-06-16 18:25:25 +0000184 handle = create_handle(table, modprobe);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000185 if (noflush == 0) {
186 DEBUGP("Cleaning all chains of table '%s'\n",
187 table);
188 for_each_chain(flush_entries, verbose, 1,
189 &handle);
190
191 DEBUGP("Deleting all user-defined chains "
192 "of table '%s'\n", table);
193 for_each_chain(delete_chain, verbose, 0,
194 &handle) ;
195 }
196
197 ret = 1;
198
199 } else if (buffer[0] == ':') {
200 /* New chain. */
201 char *policy, *chain;
202
203 chain = strtok(buffer+1, " \t\n");
204 DEBUGP("line %u, chain '%s'\n", line, chain);
205 if (!chain) {
206 exit_error(PARAMETER_PROBLEM,
207 "%s: line %u chain name invalid\n",
208 program_name, line);
209 exit(1);
210 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000211
Harald Welte885c6eb2001-10-04 08:30:46 +0000212 if (!ip6tc_builtin(chain, handle)) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000213 DEBUGP("Creating new chain '%s'\n", chain);
Harald Welte885c6eb2001-10-04 08:30:46 +0000214 if (!ip6tc_create_chain(chain, &handle))
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000215 exit_error(PARAMETER_PROBLEM,
216 "error creating chain "
217 "'%s':%s\n", chain,
218 strerror(errno));
Harald Welte885c6eb2001-10-04 08:30:46 +0000219 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000220
221 policy = strtok(NULL, " \t\n");
222 DEBUGP("line %u, policy '%s'\n", line, policy);
223 if (!policy) {
224 exit_error(PARAMETER_PROBLEM,
225 "%s: line %u policy invalid\n",
226 program_name, line);
227 exit(1);
228 }
229
230 if (strcmp(policy, "-") != 0) {
231 struct ip6t_counters count;
232
233 if (counters) {
234 char *ctrs;
235 ctrs = strtok(NULL, " \t\n");
236
237 parse_counters(ctrs, &count);
238
239 } else {
240 memset(&count, 0,
241 sizeof(struct ip6t_counters));
242 }
243
244 DEBUGP("Setting policy of chain %s to %s\n",
245 chain, policy);
246
247 if (!ip6tc_set_policy(chain, policy, &count,
248 &handle))
249 exit_error(OTHER_PROBLEM,
250 "Can't set policy `%s'"
251 " on `%s' line %u: %s\n",
252 chain, policy, line,
253 ip6tc_strerror(errno));
254 }
255
256 ret = 1;
257
258 } else {
Harald Welte885c6eb2001-10-04 08:30:46 +0000259 int a;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000260 char *ptr = buffer;
261 char *pcnt = NULL;
262 char *bcnt = NULL;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000263 char *parsestart;
Harald Welte885c6eb2001-10-04 08:30:46 +0000264
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000265 /* the parser */
266 char *param_start, *curchar;
267 int quote_open;
Harald Welte885c6eb2001-10-04 08:30:46 +0000268
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000269 /* reset the newargv */
270 newargc = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000271
272 if (buffer[0] == '[') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000273 /* we have counters in our input */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000274 ptr = strchr(buffer, ']');
275 if (!ptr)
276 exit_error(PARAMETER_PROBLEM,
277 "Bad line %u: need ]\n",
278 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000279
András Kis-Szabó2f523792001-02-27 09:59:48 +0000280 pcnt = strtok(buffer+1, ":");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000281 if (!pcnt)
282 exit_error(PARAMETER_PROBLEM,
283 "Bad line %u: need :\n",
284 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000285
András Kis-Szabó2f523792001-02-27 09:59:48 +0000286 bcnt = strtok(NULL, "]");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000287 if (!bcnt)
288 exit_error(PARAMETER_PROBLEM,
289 "Bad line %u: need ]\n",
290 line);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000291
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000292 /* start command parsing after counter */
293 parsestart = ptr + 1;
294 } else {
295 /* start command parsing at start of line */
296 parsestart = buffer;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000297 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000298
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000299 add_argv(argv[0]);
300 add_argv("-t");
301 add_argv((char *) &curtable);
302
303 if (counters && pcnt && bcnt) {
304 add_argv("--set-counters");
305 add_argv((char *) pcnt);
306 add_argv((char *) bcnt);
307 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000308
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000309 /* After fighting with strtok enough, here's now
310 * a 'real' parser. According to Rusty I'm now no
311 * longer a real hacker, but I can live with that */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000312
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000313 quote_open = 0;
314 param_start = parsestart;
315
316 for (curchar = parsestart; *curchar; curchar++) {
317 if (*curchar == '"') {
318 if (quote_open) {
319 quote_open = 0;
320 *curchar = ' ';
321 } else {
322 quote_open = 1;
323 param_start++;
324 }
325 }
326 if (*curchar == ' '
327 || *curchar == '\t'
328 || * curchar == '\n') {
329 char param_buffer[1024];
330 int param_len = curchar-param_start;
Harald Welte885c6eb2001-10-04 08:30:46 +0000331
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000332 if (quote_open)
333 continue;
Harald Welte885c6eb2001-10-04 08:30:46 +0000334
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000335 if (!param_len) {
336 /* two spaces? */
337 param_start++;
338 continue;
339 }
340
341 /* end of one parameter */
342 strncpy(param_buffer, param_start,
343 param_len);
344 *(param_buffer+param_len) = '\0';
Harald Weltebb41f882001-10-21 14:11:54 +0000345
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000346 /* check if table name specified */
Harald Welte48a6f902001-10-22 15:16:21 +0000347 if (!strncmp(param_buffer, "-t", 3)
348 || !strncmp(param_buffer, "--table", 8)) {
Harald Weltebb41f882001-10-21 14:11:54 +0000349 exit_error(PARAMETER_PROBLEM,
350 "Line %u seems to have a "
351 "-t table option.\n", line);
352 exit(1);
353 }
354
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000355 add_argv(param_buffer);
356 param_start += param_len + 1;
357 } else {
358 /* regular character, skip */
359 }
360 }
Harald Welte885c6eb2001-10-04 08:30:46 +0000361
362 DEBUGP("calling do_command6(%u, argv, &%s, handle):\n",
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000363 newargc, curtable);
Harald Welte885c6eb2001-10-04 08:30:46 +0000364
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000365 for (a = 0; a < newargc; a++)
András Kis-Szabó2f523792001-02-27 09:59:48 +0000366 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
367
Harald Welte885c6eb2001-10-04 08:30:46 +0000368 ret = do_command6(newargc, newargv,
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000369 &newargv[2], &handle);
Harald Welte885c6eb2001-10-04 08:30:46 +0000370
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000371 free_argv();
András Kis-Szabó2f523792001-02-27 09:59:48 +0000372 }
373 if (!ret) {
374 fprintf(stderr, "%s: line %u failed\n",
375 program_name, line);
376 exit(1);
377 }
378 }
379
380 return 0;
381}