blob: d0efbeed73f23d59683f29e0f255323c1b6eefa8 [file] [log] [blame]
Max Kellermann5b76f682008-01-29 13:42:48 +00001/* Code to restore the iptables state, from file by ip6tables-save.
András Kis-Szabó2f523792001-02-27 09:59:48 +00002 * Author: Andras Kis-Szabo <kisza@sch.bme.hu>
3 *
4 * based on iptables-restore
5 * Authors:
Max Kellermann5b76f682008-01-29 13:42:48 +00006 * 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 Josefsson357d59d2004-12-27 19:49:28 +000010 * $Id$
András Kis-Szabó2f523792001-02-27 09:59:48 +000011 */
12
13#include <getopt.h>
14#include <sys/errno.h>
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010015#include <stdbool.h>
András Kis-Szabó2f523792001-02-27 09:59:48 +000016#include <string.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include "ip6tables.h"
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000020#include "xtables.h"
András Kis-Szabó2f523792001-02-27 09:59:48 +000021#include "libiptc/libip6tc.h"
Jan Engelhardt33690a12008-02-11 00:54:00 +010022#include "ip6tables-multi.h"
András Kis-Szabó2f523792001-02-27 09:59:48 +000023
24#ifdef DEBUG
25#define DEBUGP(x, args...) fprintf(stderr, x, ## args)
26#else
Max Kellermann5b76f682008-01-29 13:42:48 +000027#define DEBUGP(x, args...)
András Kis-Szabó2f523792001-02-27 09:59:48 +000028#endif
29
András Kis-Szabó2f523792001-02-27 09:59:48 +000030static int binary = 0, counters = 0, verbose = 0, noflush = 0;
31
32/* Keeping track of external matches and targets. */
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +010033static const struct option options[] = {
34 {.name = "binary", .has_arg = false, .val = 'b'},
35 {.name = "counters", .has_arg = false, .val = 'c'},
36 {.name = "verbose", .has_arg = false, .val = 'v'},
37 {.name = "test", .has_arg = false, .val = 't'},
38 {.name = "help", .has_arg = false, .val = 'h'},
39 {.name = "noflush", .has_arg = false, .val = 'n'},
40 {.name = "modprobe", .has_arg = true, .val = 'M'},
41 {NULL},
András Kis-Szabó2f523792001-02-27 09:59:48 +000042};
43
44static void print_usage(const char *name, const char *version) __attribute__((noreturn));
45
46static void print_usage(const char *name, const char *version)
47{
Martin Josefsson60044392004-02-02 20:12:33 +000048 fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-t] [-h]\n"
András Kis-Szabó2f523792001-02-27 09:59:48 +000049 " [ --binary ]\n"
50 " [ --counters ]\n"
51 " [ --verbose ]\n"
Martin Josefsson4e5e29a2004-02-02 19:59:17 +000052 " [ --test ]\n"
András Kis-Szabó2f523792001-02-27 09:59:48 +000053 " [ --help ]\n"
Harald Welte58918652001-06-16 18:25:25 +000054 " [ --noflush ]\n"
Max Kellermann5b76f682008-01-29 13:42:48 +000055 " [ --modprobe=<command>]\n", name);
56
András Kis-Szabó2f523792001-02-27 09:59:48 +000057 exit(1);
58}
59
Jan Engelhardtc3541052008-11-18 12:26:26 +010060static struct ip6tc_handle *create_handle(const char *tablename)
András Kis-Szabó2f523792001-02-27 09:59:48 +000061{
Jan Engelhardtfd187312008-11-10 16:59:27 +010062 struct ip6tc_handle *handle;
András Kis-Szabó2f523792001-02-27 09:59:48 +000063
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 */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010068 xtables_load_ko(xtables_modprobe_program, false);
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000069 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) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +010073 xtables_error(PARAMETER_PROBLEM, "%s: unable to initialize "
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -050074 "table '%s'\n", ip6tables_globals.program_name,
75 tablename);
András Kis-Szabó2f523792001-02-27 09:59:48 +000076 exit(1);
77 }
78 return handle;
79}
80
Lutz Jaenickee78c69c2006-12-09 13:06:04 +000081static int parse_counters(char *string, struct ip6t_counters *ctr)
András Kis-Szabó2f523792001-02-27 09:59:48 +000082{
Patrick McHardy875441e2007-10-17 08:48:58 +000083 unsigned long long pcnt, bcnt;
84 int ret;
Patrick McHardy31f51c62007-10-16 08:49:31 +000085
Patrick McHardy875441e2007-10-17 08:48:58 +000086 ret = sscanf(string, "[%llu:%llu]",
87 (unsigned long long *)&pcnt,
88 (unsigned long long *)&bcnt);
89 ctr->pcnt = pcnt;
90 ctr->bcnt = bcnt;
91 return ret == 2;
András Kis-Szabó2f523792001-02-27 09:59:48 +000092}
93
Harald Welte885c6eb2001-10-04 08:30:46 +000094/* global new argv and argc */
95static char *newargv[255];
96static int newargc;
97
Max Kellermann5b76f682008-01-29 13:42:48 +000098/* function adding one argument to newargv, updating newargc
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000099 * returns true if argument added, false otherwise */
Harald Welte885c6eb2001-10-04 08:30:46 +0000100static int add_argv(char *what) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000101 DEBUGP("add_argv: %s\n", what);
Jan Engelhardt2c69b552009-04-30 19:32:02 +0200102 if (what && newargc + 1 < ARRAY_SIZE(newargv)) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000103 newargv[newargc] = strdup(what);
104 newargc++;
105 return 1;
Max Kellermann5b76f682008-01-29 13:42:48 +0000106 } else
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000107 return 0;
Harald Welte885c6eb2001-10-04 08:30:46 +0000108}
109
110static void free_argv(void) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000111 int i;
Harald Welte885c6eb2001-10-04 08:30:46 +0000112
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000113 for (i = 0; i < newargc; i++)
114 free(newargv[i]);
Harald Welte885c6eb2001-10-04 08:30:46 +0000115}
116
Hann-Huei Chiou4bc48332007-10-23 14:22:34 +0000117#ifdef IPTABLES_MULTI
118int ip6tables_restore_main(int argc, char *argv[])
119#else
András Kis-Szabó2f523792001-02-27 09:59:48 +0000120int main(int argc, char *argv[])
Hann-Huei Chiou4bc48332007-10-23 14:22:34 +0000121#endif
András Kis-Szabó2f523792001-02-27 09:59:48 +0000122{
Jan Engelhardtfd187312008-11-10 16:59:27 +0100123 struct ip6tc_handle *handle = NULL;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000124 char buffer[10240];
András Kis-Szabó2f523792001-02-27 09:59:48 +0000125 int c;
126 char curtable[IP6T_TABLE_MAXNAMELEN + 1];
András Kis-Szabó2f523792001-02-27 09:59:48 +0000127 FILE *in;
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000128 int in_table = 0, testing = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000129
Harald Weltea8658ca2003-03-05 07:46:15 +0000130 line = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000131
Jamal Hadi Salim617d3d12009-02-11 16:28:31 -0500132 ip6tables_globals.program_name = "ip6tables-restore";
Jamal Hadi Salim7e4db2f2009-02-13 09:14:17 -0500133 c = xtables_init_all(&ip6tables_globals, NFPROTO_IPV6);
134 if (c < 0) {
135 fprintf(stderr, "%s/%s Failed to initialize xtables\n",
136 ip6tables_globals.program_name,
137 ip6tables_globals.program_version);
138 exit(1);
139 }
Jan Engelhardtb79ec692009-07-23 17:41:21 +0200140#if defined(ALL_INCLUSIVE) || defined(NO_SHARED_LIBS)
Harald Welte3efb6ea2001-08-06 18:50:21 +0000141 init_extensions();
142#endif
143
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000144 while ((c = getopt_long(argc, argv, "bcvthnM:", options, NULL)) != -1) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000145 switch (c) {
146 case 'b':
147 binary = 1;
148 break;
149 case 'c':
150 counters = 1;
151 break;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000152 case 'v':
153 verbose = 1;
154 break;
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000155 case 't':
156 testing = 1;
157 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000158 case 'h':
159 print_usage("ip6tables-restore",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100160 IPTABLES_VERSION);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000161 break;
162 case 'n':
163 noflush = 1;
164 break;
Harald Welte58918652001-06-16 18:25:25 +0000165 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100166 xtables_modprobe_program = optarg;
Harald Welte58918652001-06-16 18:25:25 +0000167 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000168 }
169 }
Max Kellermann5b76f682008-01-29 13:42:48 +0000170
András Kis-Szabó2f523792001-02-27 09:59:48 +0000171 if (optind == argc - 1) {
172 in = fopen(argv[optind], "r");
173 if (!in) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000174 fprintf(stderr, "Can't open %s: %s\n", argv[optind],
András Kis-Szabó2f523792001-02-27 09:59:48 +0000175 strerror(errno));
176 exit(1);
177 }
178 }
179 else if (optind < argc) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000180 fprintf(stderr, "Unknown arguments found on commandline\n");
András Kis-Szabó2f523792001-02-27 09:59:48 +0000181 exit(1);
182 }
183 else in = stdin;
Max Kellermann5b76f682008-01-29 13:42:48 +0000184
András Kis-Szabó2f523792001-02-27 09:59:48 +0000185 /* Grab standard input. */
186 while (fgets(buffer, sizeof(buffer), in)) {
Martin Josefssoncc536282004-02-02 20:14:56 +0000187 int ret = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000188
189 line++;
Martin Josefsson5065afa2004-01-31 19:33:47 +0000190 if (buffer[0] == '\n')
191 continue;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000192 else if (buffer[0] == '#') {
Martin Josefsson3673dcb2004-01-31 19:41:49 +0000193 if (verbose)
194 fputs(buffer, stdout);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000195 continue;
Harald Weltea8658ca2003-03-05 07:46:15 +0000196 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000197 if (!testing) {
198 DEBUGP("Calling commit\n");
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100199 ret = ip6tc_commit(handle);
200 ip6tc_free(handle);
201 handle = NULL;
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000202 } else {
203 DEBUGP("Not calling commit, testing\n");
204 ret = 1;
205 }
Harald Weltea8658ca2003-03-05 07:46:15 +0000206 in_table = 0;
207 } else if ((buffer[0] == '*') && (!in_table)) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000208 /* New table */
209 char *table;
210
211 table = strtok(buffer+1, " \t\n");
212 DEBUGP("line %u, table '%s'\n", line, table);
213 if (!table) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100214 xtables_error(PARAMETER_PROBLEM,
András Kis-Szabó2f523792001-02-27 09:59:48 +0000215 "%s: line %u table name invalid\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500216 ip6tables_globals.program_name,
217 line);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000218 exit(1);
219 }
220 strncpy(curtable, table, IP6T_TABLE_MAXNAMELEN);
Karsten Desler073df8f2004-01-31 15:33:55 +0000221 curtable[IP6T_TABLE_MAXNAMELEN] = '\0';
András Kis-Szabó2f523792001-02-27 09:59:48 +0000222
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000223 if (handle)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100224 ip6tc_free(handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000225
Jan Engelhardtc3541052008-11-18 12:26:26 +0100226 handle = create_handle(table);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000227 if (noflush == 0) {
228 DEBUGP("Cleaning all chains of table '%s'\n",
229 table);
Max Kellermann5b76f682008-01-29 13:42:48 +0000230 for_each_chain(flush_entries, verbose, 1,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100231 handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000232
András Kis-Szabó2f523792001-02-27 09:59:48 +0000233 DEBUGP("Deleting all user-defined chains "
234 "of table '%s'\n", table);
Max Kellermann5b76f682008-01-29 13:42:48 +0000235 for_each_chain(delete_chain, verbose, 0,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100236 handle);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000237 }
238
239 ret = 1;
Harald Weltea8658ca2003-03-05 07:46:15 +0000240 in_table = 1;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000241
Harald Weltea8658ca2003-03-05 07:46:15 +0000242 } else if ((buffer[0] == ':') && (in_table)) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000243 /* New chain. */
244 char *policy, *chain;
245
246 chain = strtok(buffer+1, " \t\n");
247 DEBUGP("line %u, chain '%s'\n", line, chain);
248 if (!chain) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100249 xtables_error(PARAMETER_PROBLEM,
András Kis-Szabó2f523792001-02-27 09:59:48 +0000250 "%s: line %u chain name invalid\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500251 ip6tables_globals.program_name,
252 line);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000253 exit(1);
254 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000255
Harald Weltedf1c71c2004-08-30 16:00:32 +0000256 if (ip6tc_builtin(chain, handle) <= 0) {
Charlie Brady595e4932005-06-12 15:54:15 +0000257 if (noflush && ip6tc_is_chain(chain, handle)) {
258 DEBUGP("Flushing existing user defined chain '%s'\n", chain);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100259 if (!ip6tc_flush_entries(chain, handle))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100260 xtables_error(PARAMETER_PROBLEM,
Charlie Brady595e4932005-06-12 15:54:15 +0000261 "error flushing chain "
262 "'%s':%s\n", chain,
263 strerror(errno));
264 } else {
265 DEBUGP("Creating new chain '%s'\n", chain);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100266 if (!ip6tc_create_chain(chain, handle))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100267 xtables_error(PARAMETER_PROBLEM,
Charlie Brady595e4932005-06-12 15:54:15 +0000268 "error creating chain "
269 "'%s':%s\n", chain,
270 strerror(errno));
271 }
Harald Welte885c6eb2001-10-04 08:30:46 +0000272 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000273
274 policy = strtok(NULL, " \t\n");
275 DEBUGP("line %u, policy '%s'\n", line, policy);
276 if (!policy) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100277 xtables_error(PARAMETER_PROBLEM,
András Kis-Szabó2f523792001-02-27 09:59:48 +0000278 "%s: line %u policy invalid\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500279 ip6tables_globals.program_name,
280 line);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000281 exit(1);
282 }
283
284 if (strcmp(policy, "-") != 0) {
285 struct ip6t_counters count;
286
287 if (counters) {
288 char *ctrs;
289 ctrs = strtok(NULL, " \t\n");
290
Harald Welte6f38a302006-02-09 14:35:38 +0000291 if (!ctrs || !parse_counters(ctrs, &count))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100292 xtables_error(PARAMETER_PROBLEM,
Harald Welte6f38a302006-02-09 14:35:38 +0000293 "invalid policy counters "
294 "for chain '%s'\n", chain);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000295
296 } else {
Max Kellermann5b76f682008-01-29 13:42:48 +0000297 memset(&count, 0,
András Kis-Szabó2f523792001-02-27 09:59:48 +0000298 sizeof(struct ip6t_counters));
299 }
300
301 DEBUGP("Setting policy of chain %s to %s\n",
302 chain, policy);
303
304 if (!ip6tc_set_policy(chain, policy, &count,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100305 handle))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100306 xtables_error(OTHER_PROBLEM,
András Kis-Szabó2f523792001-02-27 09:59:48 +0000307 "Can't set policy `%s'"
308 " on `%s' line %u: %s\n",
309 chain, policy, line,
310 ip6tc_strerror(errno));
311 }
312
313 ret = 1;
314
Harald Weltea8658ca2003-03-05 07:46:15 +0000315 } else if (in_table) {
Harald Welte885c6eb2001-10-04 08:30:46 +0000316 int a;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000317 char *ptr = buffer;
318 char *pcnt = NULL;
319 char *bcnt = NULL;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000320 char *parsestart;
Harald Welte885c6eb2001-10-04 08:30:46 +0000321
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000322 /* the parser */
Max Kellermann3bf54df2008-01-29 13:45:29 +0000323 char *curchar;
324 int quote_open, escaped;
325 size_t param_len;
Harald Welte885c6eb2001-10-04 08:30:46 +0000326
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000327 /* reset the newargv */
328 newargc = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000329
330 if (buffer[0] == '[') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000331 /* we have counters in our input */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000332 ptr = strchr(buffer, ']');
333 if (!ptr)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100334 xtables_error(PARAMETER_PROBLEM,
András Kis-Szabó2f523792001-02-27 09:59:48 +0000335 "Bad line %u: need ]\n",
336 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000337
András Kis-Szabó2f523792001-02-27 09:59:48 +0000338 pcnt = strtok(buffer+1, ":");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000339 if (!pcnt)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100340 xtables_error(PARAMETER_PROBLEM,
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000341 "Bad line %u: need :\n",
342 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000343
András Kis-Szabó2f523792001-02-27 09:59:48 +0000344 bcnt = strtok(NULL, "]");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000345 if (!bcnt)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100346 xtables_error(PARAMETER_PROBLEM,
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000347 "Bad line %u: need ]\n",
348 line);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000349
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000350 /* start command parsing after counter */
351 parsestart = ptr + 1;
352 } else {
353 /* start command parsing at start of line */
354 parsestart = buffer;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000355 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000356
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000357 add_argv(argv[0]);
358 add_argv("-t");
359 add_argv((char *) &curtable);
Max Kellermann5b76f682008-01-29 13:42:48 +0000360
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000361 if (counters && pcnt && bcnt) {
362 add_argv("--set-counters");
363 add_argv((char *) pcnt);
364 add_argv((char *) bcnt);
365 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000366
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000367 /* After fighting with strtok enough, here's now
368 * a 'real' parser. According to Rusty I'm now no
369 * longer a real hacker, but I can live with that */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000370
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000371 quote_open = 0;
Max Kellermann3bf54df2008-01-29 13:45:29 +0000372 escaped = 0;
373 param_len = 0;
Max Kellermann5b76f682008-01-29 13:42:48 +0000374
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000375 for (curchar = parsestart; *curchar; curchar++) {
Max Kellermann3bf54df2008-01-29 13:45:29 +0000376 char param_buffer[1024];
377
Max Kellermann3bf54df2008-01-29 13:45:29 +0000378 if (quote_open) {
Max Kellermannfe05c762008-01-29 13:46:01 +0000379 if (escaped) {
380 param_buffer[param_len++] = *curchar;
381 escaped = 0;
382 continue;
383 } else if (*curchar == '\\') {
Max Kellermann3bf54df2008-01-29 13:45:29 +0000384 escaped = 1;
385 continue;
386 } else if (*curchar == '"') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000387 quote_open = 0;
388 *curchar = ' ';
389 } else {
Max Kellermann3bf54df2008-01-29 13:45:29 +0000390 param_buffer[param_len++] = *curchar;
391 continue;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000392 }
Max Kellermann3bf54df2008-01-29 13:45:29 +0000393 } else {
394 if (*curchar == '"') {
395 quote_open = 1;
396 continue;
397 }
398 }
399
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000400 if (*curchar == ' '
401 || *curchar == '\t'
402 || * curchar == '\n') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000403 if (!param_len) {
404 /* two spaces? */
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000405 continue;
406 }
Max Kellermann3bf54df2008-01-29 13:45:29 +0000407
408 param_buffer[param_len] = '\0';
Harald Weltebb41f882001-10-21 14:11:54 +0000409
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000410 /* check if table name specified */
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +0200411 if (!strncmp(param_buffer, "-t", 2)
Harald Welte48a6f902001-10-22 15:16:21 +0000412 || !strncmp(param_buffer, "--table", 8)) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100413 xtables_error(PARAMETER_PROBLEM,
Harald Weltebb41f882001-10-21 14:11:54 +0000414 "Line %u seems to have a "
415 "-t table option.\n", line);
416 exit(1);
417 }
418
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000419 add_argv(param_buffer);
Max Kellermann3bf54df2008-01-29 13:45:29 +0000420 param_len = 0;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000421 } else {
Max Kellermann3bf54df2008-01-29 13:45:29 +0000422 /* regular character, copy to buffer */
423 param_buffer[param_len++] = *curchar;
424
425 if (param_len >= sizeof(param_buffer))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100426 xtables_error(PARAMETER_PROBLEM,
Max Kellermann3bf54df2008-01-29 13:45:29 +0000427 "Parameter too long!");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000428 }
429 }
Harald Welte885c6eb2001-10-04 08:30:46 +0000430
431 DEBUGP("calling do_command6(%u, argv, &%s, handle):\n",
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000432 newargc, curtable);
Harald Welte885c6eb2001-10-04 08:30:46 +0000433
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000434 for (a = 0; a < newargc; a++)
András Kis-Szabó2f523792001-02-27 09:59:48 +0000435 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
436
Max Kellermann5b76f682008-01-29 13:42:48 +0000437 ret = do_command6(newargc, newargv,
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000438 &newargv[2], &handle);
Harald Welte885c6eb2001-10-04 08:30:46 +0000439
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000440 free_argv();
Henrik Nordstromd2137602008-05-12 20:51:45 +0200441 fflush(stdout);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000442 }
443 if (!ret) {
444 fprintf(stderr, "%s: line %u failed\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500445 ip6tables_globals.program_name,
446 line);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000447 exit(1);
448 }
449 }
Martin Josefssone0dc5812004-02-02 19:58:36 +0000450 if (in_table) {
451 fprintf(stderr, "%s: COMMIT expected at line %u\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500452 ip6tables_globals.program_name,
453 line + 1);
Martin Josefssone0dc5812004-02-02 19:58:36 +0000454 exit(1);
455 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000456
Jan Engelhardtf1afcc82009-06-10 13:52:58 +0200457 if (in != NULL)
458 fclose(in);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000459 return 0;
460}