Busybox: Upgrade to 1.21.1 (stable). lsof active.
[tomato.git] / release / src / router / iptables / iptables-restore.c
blobd8f6022df22826a45e1c9bd71573ee64cb39deff
1 /* Code to restore the iptables state, from file by iptables-save.
2 * (C) 2000-2002 by Harald Welte <laforge@gnumonks.org>
3 * based on previous code from Rusty Russell <rusty@linuxcare.com.au>
5 * This code is distributed under the terms of GNU GPL v2
7 * $Id: iptables-restore.c 6828 2007-05-10 15:00:39Z /C=EU/ST=EU/CN=Patrick McHardy/emailAddress=kaber@trash.net $
8 */
10 #include <getopt.h>
11 #include <sys/errno.h>
12 #include <string.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include "iptables.h"
16 #include "libiptc/libiptc.h"
18 #ifdef DEBUG
19 #define DEBUGP(x, args...) fprintf(stderr, x, ## args)
20 #else
21 #define DEBUGP(x, args...)
22 #endif
24 static int binary = 0, counters = 0, verbose = 0, noflush = 0;
26 /* Keeping track of external matches and targets. */
27 static struct option options[] = {
28 { "binary", 0, 0, 'b' },
29 { "counters", 0, 0, 'c' },
30 { "verbose", 0, 0, 'v' },
31 { "test", 0, 0, 't' },
32 { "help", 0, 0, 'h' },
33 { "noflush", 0, 0, 'n'},
34 { "modprobe", 1, 0, 'M'},
35 { 0 }
38 static void print_usage(const char *name, const char *version) __attribute__((noreturn));
40 static void print_usage(const char *name, const char *version)
42 fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-t] [-h]\n"
43 " [ --binary ]\n"
44 " [ --counters ]\n"
45 " [ --verbose ]\n"
46 " [ --test ]\n"
47 " [ --help ]\n"
48 " [ --noflush ]\n"
49 " [ --modprobe=<command>]\n", name);
51 exit(1);
54 iptc_handle_t create_handle(const char *tablename, const char* modprobe )
56 iptc_handle_t handle;
58 handle = iptc_init(tablename);
60 if (!handle) {
61 /* try to insmod the module if iptc_init failed */
62 iptables_insmod("ip_tables", modprobe, 0);
63 handle = iptc_init(tablename);
66 if (!handle) {
67 exit_error(PARAMETER_PROBLEM, "%s: unable to initialize "
68 "table '%s'\n", program_name, tablename);
69 exit(1);
71 return handle;
74 static int parse_counters(char *string, struct ipt_counters *ctr)
76 unsigned long long pcnt, bcnt;
77 int ret;
79 ret = sscanf(string, "[%llu:%llu]",
80 (unsigned long long *)&pcnt,
81 (unsigned long long *)&bcnt);
82 ctr->pcnt = pcnt;
83 ctr->bcnt = bcnt;
84 return ret == 2;
87 /* global new argv and argc */
88 static char *newargv[255];
89 static int newargc;
91 /* function adding one argument to newargv, updating newargc
92 * returns true if argument added, false otherwise */
93 static int add_argv(char *what) {
94 DEBUGP("add_argv: %s\n", what);
95 if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) {
96 newargv[newargc] = strdup(what);
97 newargc++;
98 return 1;
99 } else
100 return 0;
103 static void free_argv(void) {
104 int i;
106 for (i = 0; i < newargc; i++)
107 free(newargv[i]);
110 #ifdef IPTABLES_MULTI
112 iptables_restore_main(int argc, char *argv[])
113 #else
115 main(int argc, char *argv[])
116 #endif
118 iptc_handle_t handle = NULL;
119 char buffer[10240];
120 int c;
121 char curtable[IPT_TABLE_MAXNAMELEN + 1];
122 FILE *in;
123 const char *modprobe = 0;
124 int in_table = 0, testing = 0;
126 program_name = "iptables-restore";
127 program_version = IPTABLES_VERSION;
128 line = 0;
130 lib_dir = getenv("IPTABLES_LIB_DIR");
131 if (!lib_dir)
132 lib_dir = IPT_LIB_DIR;
134 #ifdef NO_SHARED_LIBS
135 init_extensions();
136 #endif
138 while ((c = getopt_long(argc, argv, "bcvthnM:", options, NULL)) != -1) {
139 switch (c) {
140 case 'b':
141 binary = 1;
142 break;
143 case 'c':
144 counters = 1;
145 break;
146 case 'v':
147 verbose = 1;
148 break;
149 case 't':
150 testing = 1;
151 break;
152 case 'h':
153 print_usage("iptables-restore",
154 IPTABLES_VERSION);
155 break;
156 case 'n':
157 noflush = 1;
158 break;
159 case 'M':
160 modprobe = optarg;
161 break;
165 if (optind == argc - 1) {
166 in = fopen(argv[optind], "r");
167 if (!in) {
168 fprintf(stderr, "Can't open %s: %s\n", argv[optind],
169 strerror(errno));
170 exit(1);
173 else if (optind < argc) {
174 fprintf(stderr, "Unknown arguments found on commandline\n");
175 exit(1);
177 else in = stdin;
179 /* Grab standard input. */
180 while (fgets(buffer, sizeof(buffer), in)) {
181 int ret = 0;
183 line++;
184 if (buffer[0] == '\n')
185 continue;
186 else if (buffer[0] == '#') {
187 if (verbose)
188 fputs(buffer, stdout);
189 continue;
190 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
191 if (!testing) {
192 DEBUGP("Calling commit\n");
193 ret = iptc_commit(&handle);
194 } else {
195 DEBUGP("Not calling commit, testing\n");
196 ret = 1;
198 in_table = 0;
199 } else if ((buffer[0] == '*') && (!in_table)) {
200 /* New table */
201 char *table;
203 table = strtok(buffer+1, " \t\n");
204 DEBUGP("line %u, table '%s'\n", line, table);
205 if (!table) {
206 exit_error(PARAMETER_PROBLEM,
207 "%s: line %u table name invalid\n",
208 program_name, line);
209 exit(1);
211 strncpy(curtable, table, IPT_TABLE_MAXNAMELEN);
212 curtable[IPT_TABLE_MAXNAMELEN] = '\0';
214 if (handle)
215 iptc_free(&handle);
217 handle = create_handle(table, modprobe);
218 if (noflush == 0) {
219 DEBUGP("Cleaning all chains of table '%s'\n",
220 table);
221 for_each_chain(flush_entries, verbose, 1,
222 &handle);
224 DEBUGP("Deleting all user-defined chains "
225 "of table '%s'\n", table);
226 for_each_chain(delete_chain, verbose, 0,
227 &handle) ;
230 ret = 1;
231 in_table = 1;
233 } else if ((buffer[0] == ':') && (in_table)) {
234 /* New chain. */
235 char *policy, *chain;
237 chain = strtok(buffer+1, " \t\n");
238 DEBUGP("line %u, chain '%s'\n", line, chain);
239 if (!chain) {
240 exit_error(PARAMETER_PROBLEM,
241 "%s: line %u chain name invalid\n",
242 program_name, line);
243 exit(1);
246 if (iptc_builtin(chain, handle) <= 0) {
247 if (noflush && iptc_is_chain(chain, handle)) {
248 DEBUGP("Flushing existing user defined chain '%s'\n", chain);
249 if (!iptc_flush_entries(chain, &handle))
250 exit_error(PARAMETER_PROBLEM,
251 "error flushing chain "
252 "'%s':%s\n", chain,
253 strerror(errno));
254 } else {
255 DEBUGP("Creating new chain '%s'\n", chain);
256 if (!iptc_create_chain(chain, &handle))
257 exit_error(PARAMETER_PROBLEM,
258 "error creating chain "
259 "'%s':%s\n", chain,
260 strerror(errno));
264 policy = strtok(NULL, " \t\n");
265 DEBUGP("line %u, policy '%s'\n", line, policy);
266 if (!policy) {
267 exit_error(PARAMETER_PROBLEM,
268 "%s: line %u policy invalid\n",
269 program_name, line);
270 exit(1);
273 if (strcmp(policy, "-") != 0) {
274 struct ipt_counters count;
276 if (counters) {
277 char *ctrs;
278 ctrs = strtok(NULL, " \t\n");
280 if (!ctrs || !parse_counters(ctrs, &count))
281 exit_error(PARAMETER_PROBLEM,
282 "invalid policy counters "
283 "for chain '%s'\n", chain);
285 } else {
286 memset(&count, 0,
287 sizeof(struct ipt_counters));
290 DEBUGP("Setting policy of chain %s to %s\n",
291 chain, policy);
293 if (!iptc_set_policy(chain, policy, &count,
294 &handle))
295 exit_error(OTHER_PROBLEM,
296 "Can't set policy `%s'"
297 " on `%s' line %u: %s\n",
298 chain, policy, line,
299 iptc_strerror(errno));
302 ret = 1;
304 } else if (in_table) {
305 int a;
306 char *ptr = buffer;
307 char *pcnt = NULL;
308 char *bcnt = NULL;
309 char *parsestart;
311 /* the parser */
312 char *curchar;
313 int quote_open, escaped;
314 size_t param_len;
316 /* reset the newargv */
317 newargc = 0;
319 if (buffer[0] == '[') {
320 /* we have counters in our input */
321 ptr = strchr(buffer, ']');
322 if (!ptr)
323 exit_error(PARAMETER_PROBLEM,
324 "Bad line %u: need ]\n",
325 line);
327 pcnt = strtok(buffer+1, ":");
328 if (!pcnt)
329 exit_error(PARAMETER_PROBLEM,
330 "Bad line %u: need :\n",
331 line);
333 bcnt = strtok(NULL, "]");
334 if (!bcnt)
335 exit_error(PARAMETER_PROBLEM,
336 "Bad line %u: need ]\n",
337 line);
339 /* start command parsing after counter */
340 parsestart = ptr + 1;
341 } else {
342 /* start command parsing at start of line */
343 parsestart = buffer;
346 add_argv(argv[0]);
347 add_argv("-t");
348 add_argv((char *) &curtable);
350 if (counters && pcnt && bcnt) {
351 add_argv("--set-counters");
352 add_argv((char *) pcnt);
353 add_argv((char *) bcnt);
356 /* After fighting with strtok enough, here's now
357 * a 'real' parser. According to Rusty I'm now no
358 * longer a real hacker, but I can live with that */
360 quote_open = 0;
361 escaped = 0;
362 param_len = 0;
364 for (curchar = parsestart; *curchar; curchar++) {
365 char param_buffer[1024];
367 if (quote_open) {
368 if (escaped) {
369 param_buffer[param_len++] = *curchar;
370 escaped = 0;
371 continue;
372 } else if (*curchar == '\\') {
373 escaped = 1;
374 continue;
375 } else if (*curchar == '"') {
376 quote_open = 0;
377 *curchar = ' ';
378 } else {
379 param_buffer[param_len++] = *curchar;
380 continue;
382 } else {
383 if (*curchar == '"') {
384 quote_open = 1;
385 continue;
389 if (*curchar == ' '
390 || *curchar == '\t'
391 || * curchar == '\n') {
392 if (!param_len) {
393 /* two spaces? */
394 continue;
397 param_buffer[param_len] = '\0';
399 /* check if table name specified */
400 if (!strncmp(param_buffer, "-t", 2)
401 || !strncmp(param_buffer, "--table", 8)) {
402 exit_error(PARAMETER_PROBLEM,
403 "Line %u seems to have a "
404 "-t table option.\n", line);
405 exit(1);
408 add_argv(param_buffer);
409 param_len = 0;
410 } else {
411 /* regular character, copy to buffer */
412 param_buffer[param_len++] = *curchar;
414 if (param_len >= sizeof(param_buffer))
415 exit_error(PARAMETER_PROBLEM,
416 "Parameter too long!");
420 DEBUGP("calling do_command(%u, argv, &%s, handle):\n",
421 newargc, curtable);
423 for (a = 0; a < newargc; a++)
424 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
426 ret = do_command(newargc, newargv,
427 &newargv[2], &handle);
429 free_argv();
430 fflush(stdout);
432 if (!ret) {
433 fprintf(stderr, "%s: line %u failed\n",
434 program_name, line);
435 exit(1);
438 if (in_table) {
439 fprintf(stderr, "%s: COMMIT expected at line %u\n",
440 program_name, line + 1);
441 exit(1);
444 return 0;