Busybox: Upgrade to 1.21.1 (stable). lsof active.
[tomato.git] / release / src / router / iptables / ip6tables-restore.c
blobee1c140f005fa9fe234ac2d1d3d3e0d179a557dd
1 /* Code to restore the iptables state, from file by ip6tables-save.
2 * Author: Andras Kis-Szabo <kisza@sch.bme.hu>
4 * based on iptables-restore
5 * Authors:
6 * Harald Welte <laforge@gnumonks.org>
7 * Rusty Russell <rusty@linuxcare.com.au>
8 * This code is distributed under the terms of GNU GPL v2
10 * $Id: ip6tables-restore.c 6828 2007-05-10 15:00:39Z /C=EU/ST=EU/CN=Patrick McHardy/emailAddress=kaber@trash.net $
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"
21 #ifdef DEBUG
22 #define DEBUGP(x, args...) fprintf(stderr, x, ## args)
23 #else
24 #define DEBUGP(x, args...)
25 #endif
27 static int binary = 0, counters = 0, verbose = 0, noflush = 0;
29 /* Keeping track of external matches and targets. */
30 static struct option options[] = {
31 { "binary", 0, 0, 'b' },
32 { "counters", 0, 0, 'c' },
33 { "verbose", 0, 0, 'v' },
34 { "test", 0, 0, 't' },
35 { "help", 0, 0, 'h' },
36 { "noflush", 0, 0, 'n'},
37 { "modprobe", 1, 0, 'M'},
38 { 0 }
41 static void print_usage(const char *name, const char *version) __attribute__((noreturn));
43 static void print_usage(const char *name, const char *version)
45 fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-t] [-h]\n"
46 " [ --binary ]\n"
47 " [ --counters ]\n"
48 " [ --verbose ]\n"
49 " [ --test ]\n"
50 " [ --help ]\n"
51 " [ --noflush ]\n"
52 " [ --modprobe=<command>]\n", name);
54 exit(1);
57 ip6tc_handle_t create_handle(const char *tablename, const char* modprobe)
59 ip6tc_handle_t handle;
61 handle = ip6tc_init(tablename);
63 if (!handle) {
64 /* try to insmod the module if iptc_init failed */
65 ip6tables_insmod("ip6_tables", modprobe, 0);
66 handle = ip6tc_init(tablename);
69 if (!handle) {
70 exit_error(PARAMETER_PROBLEM, "%s: unable to initialize "
71 "table '%s'\n", program_name, tablename);
72 exit(1);
74 return handle;
77 static int parse_counters(char *string, struct ip6t_counters *ctr)
79 unsigned long long pcnt, bcnt;
80 int ret;
82 ret = sscanf(string, "[%llu:%llu]",
83 (unsigned long long *)&pcnt,
84 (unsigned long long *)&bcnt);
85 ctr->pcnt = pcnt;
86 ctr->bcnt = bcnt;
87 return ret == 2;
90 /* global new argv and argc */
91 static char *newargv[255];
92 static int newargc;
94 /* function adding one argument to newargv, updating newargc
95 * returns true if argument added, false otherwise */
96 static int add_argv(char *what) {
97 DEBUGP("add_argv: %s\n", what);
98 if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) {
99 newargv[newargc] = strdup(what);
100 newargc++;
101 return 1;
102 } else
103 return 0;
106 static void free_argv(void) {
107 int i;
109 for (i = 0; i < newargc; i++)
110 free(newargv[i]);
113 #ifdef IPTABLES_MULTI
114 int ip6tables_restore_main(int argc, char *argv[])
115 #else
116 int main(int argc, char *argv[])
117 #endif
119 ip6tc_handle_t handle = NULL;
120 char buffer[10240];
121 int c;
122 char curtable[IP6T_TABLE_MAXNAMELEN + 1];
123 FILE *in;
124 const char *modprobe = 0;
125 int in_table = 0, testing = 0;
127 program_name = "ip6tables-restore";
128 program_version = IPTABLES_VERSION;
129 line = 0;
131 lib_dir = getenv("IP6TABLES_LIB_DIR");
132 if (!lib_dir)
133 lib_dir = IP6T_LIB_DIR;
135 #ifdef NO_SHARED_LIBS
136 init_extensions();
137 #endif
139 while ((c = getopt_long(argc, argv, "bcvthnM:", options, NULL)) != -1) {
140 switch (c) {
141 case 'b':
142 binary = 1;
143 break;
144 case 'c':
145 counters = 1;
146 break;
147 case 'v':
148 verbose = 1;
149 break;
150 case 't':
151 testing = 1;
152 break;
153 case 'h':
154 print_usage("ip6tables-restore",
155 IPTABLES_VERSION);
156 break;
157 case 'n':
158 noflush = 1;
159 break;
160 case 'M':
161 modprobe = optarg;
162 break;
166 if (optind == argc - 1) {
167 in = fopen(argv[optind], "r");
168 if (!in) {
169 fprintf(stderr, "Can't open %s: %s\n", argv[optind],
170 strerror(errno));
171 exit(1);
174 else if (optind < argc) {
175 fprintf(stderr, "Unknown arguments found on commandline\n");
176 exit(1);
178 else in = stdin;
180 /* Grab standard input. */
181 while (fgets(buffer, sizeof(buffer), in)) {
182 int ret = 0;
184 line++;
185 if (buffer[0] == '\n')
186 continue;
187 else if (buffer[0] == '#') {
188 if (verbose)
189 fputs(buffer, stdout);
190 continue;
191 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
192 if (!testing) {
193 DEBUGP("Calling commit\n");
194 ret = ip6tc_commit(&handle);
195 } else {
196 DEBUGP("Not calling commit, testing\n");
197 ret = 1;
199 in_table = 0;
200 } else if ((buffer[0] == '*') && (!in_table)) {
201 /* New table */
202 char *table;
204 table = strtok(buffer+1, " \t\n");
205 DEBUGP("line %u, table '%s'\n", line, table);
206 if (!table) {
207 exit_error(PARAMETER_PROBLEM,
208 "%s: line %u table name invalid\n",
209 program_name, line);
210 exit(1);
212 strncpy(curtable, table, IP6T_TABLE_MAXNAMELEN);
213 curtable[IP6T_TABLE_MAXNAMELEN] = '\0';
215 if (handle)
216 ip6tc_free(&handle);
218 handle = create_handle(table, modprobe);
219 if (noflush == 0) {
220 DEBUGP("Cleaning all chains of table '%s'\n",
221 table);
222 for_each_chain(flush_entries, verbose, 1,
223 &handle);
225 DEBUGP("Deleting all user-defined chains "
226 "of table '%s'\n", table);
227 for_each_chain(delete_chain, verbose, 0,
228 &handle) ;
231 ret = 1;
232 in_table = 1;
234 } else if ((buffer[0] == ':') && (in_table)) {
235 /* New chain. */
236 char *policy, *chain;
238 chain = strtok(buffer+1, " \t\n");
239 DEBUGP("line %u, chain '%s'\n", line, chain);
240 if (!chain) {
241 exit_error(PARAMETER_PROBLEM,
242 "%s: line %u chain name invalid\n",
243 program_name, line);
244 exit(1);
247 if (ip6tc_builtin(chain, handle) <= 0) {
248 if (noflush && ip6tc_is_chain(chain, handle)) {
249 DEBUGP("Flushing existing user defined chain '%s'\n", chain);
250 if (!ip6tc_flush_entries(chain, &handle))
251 exit_error(PARAMETER_PROBLEM,
252 "error flushing chain "
253 "'%s':%s\n", chain,
254 strerror(errno));
255 } else {
256 DEBUGP("Creating new chain '%s'\n", chain);
257 if (!ip6tc_create_chain(chain, &handle))
258 exit_error(PARAMETER_PROBLEM,
259 "error creating chain "
260 "'%s':%s\n", chain,
261 strerror(errno));
265 policy = strtok(NULL, " \t\n");
266 DEBUGP("line %u, policy '%s'\n", line, policy);
267 if (!policy) {
268 exit_error(PARAMETER_PROBLEM,
269 "%s: line %u policy invalid\n",
270 program_name, line);
271 exit(1);
274 if (strcmp(policy, "-") != 0) {
275 struct ip6t_counters count;
277 if (counters) {
278 char *ctrs;
279 ctrs = strtok(NULL, " \t\n");
281 if (!ctrs || !parse_counters(ctrs, &count))
282 exit_error(PARAMETER_PROBLEM,
283 "invalid policy counters "
284 "for chain '%s'\n", chain);
286 } else {
287 memset(&count, 0,
288 sizeof(struct ip6t_counters));
291 DEBUGP("Setting policy of chain %s to %s\n",
292 chain, policy);
294 if (!ip6tc_set_policy(chain, policy, &count,
295 &handle))
296 exit_error(OTHER_PROBLEM,
297 "Can't set policy `%s'"
298 " on `%s' line %u: %s\n",
299 chain, policy, line,
300 ip6tc_strerror(errno));
303 ret = 1;
305 } else if (in_table) {
306 int a;
307 char *ptr = buffer;
308 char *pcnt = NULL;
309 char *bcnt = NULL;
310 char *parsestart;
312 /* the parser */
313 char *curchar;
314 int quote_open, escaped;
315 size_t param_len;
317 /* reset the newargv */
318 newargc = 0;
320 if (buffer[0] == '[') {
321 /* we have counters in our input */
322 ptr = strchr(buffer, ']');
323 if (!ptr)
324 exit_error(PARAMETER_PROBLEM,
325 "Bad line %u: need ]\n",
326 line);
328 pcnt = strtok(buffer+1, ":");
329 if (!pcnt)
330 exit_error(PARAMETER_PROBLEM,
331 "Bad line %u: need :\n",
332 line);
334 bcnt = strtok(NULL, "]");
335 if (!bcnt)
336 exit_error(PARAMETER_PROBLEM,
337 "Bad line %u: need ]\n",
338 line);
340 /* start command parsing after counter */
341 parsestart = ptr + 1;
342 } else {
343 /* start command parsing at start of line */
344 parsestart = buffer;
347 add_argv(argv[0]);
348 add_argv("-t");
349 add_argv((char *) &curtable);
351 if (counters && pcnt && bcnt) {
352 add_argv("--set-counters");
353 add_argv((char *) pcnt);
354 add_argv((char *) bcnt);
357 /* After fighting with strtok enough, here's now
358 * a 'real' parser. According to Rusty I'm now no
359 * longer a real hacker, but I can live with that */
361 quote_open = 0;
362 escaped = 0;
363 param_len = 0;
365 for (curchar = parsestart; *curchar; curchar++) {
366 char param_buffer[1024];
368 if (quote_open) {
369 if (escaped) {
370 param_buffer[param_len++] = *curchar;
371 escaped = 0;
372 continue;
373 } else if (*curchar == '\\') {
374 escaped = 1;
375 continue;
376 } else if (*curchar == '"') {
377 quote_open = 0;
378 *curchar = ' ';
379 } else {
380 param_buffer[param_len++] = *curchar;
381 continue;
383 } else {
384 if (*curchar == '"') {
385 quote_open = 1;
386 continue;
390 if (*curchar == ' '
391 || *curchar == '\t'
392 || * curchar == '\n') {
393 if (!param_len) {
394 /* two spaces? */
395 continue;
398 param_buffer[param_len] = '\0';
400 /* check if table name specified */
401 if (!strncmp(param_buffer, "-t", 2)
402 || !strncmp(param_buffer, "--table", 8)) {
403 exit_error(PARAMETER_PROBLEM,
404 "Line %u seems to have a "
405 "-t table option.\n", line);
406 exit(1);
409 add_argv(param_buffer);
410 param_len = 0;
411 } else {
412 /* regular character, copy to buffer */
413 param_buffer[param_len++] = *curchar;
415 if (param_len >= sizeof(param_buffer))
416 exit_error(PARAMETER_PROBLEM,
417 "Parameter too long!");
421 DEBUGP("calling do_command6(%u, argv, &%s, handle):\n",
422 newargc, curtable);
424 for (a = 0; a < newargc; a++)
425 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
427 ret = do_command6(newargc, newargv,
428 &newargv[2], &handle);
430 free_argv();
431 fflush(stdout);
433 if (!ret) {
434 fprintf(stderr, "%s: line %u failed\n",
435 program_name, line);
436 exit(1);
439 if (in_table) {
440 fprintf(stderr, "%s: COMMIT expected at line %u\n",
441 program_name, line + 1);
442 exit(1);
445 return 0;