blob: 10c3acfc5849c2efc2f6543716cb7163af9f87b9 [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 */
10
11#include <getopt.h>
12#include <sys/errno.h>
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010013#include <stdbool.h>
András Kis-Szabó2f523792001-02-27 09:59:48 +000014#include <string.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include "ip6tables.h"
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000018#include "xtables.h"
András Kis-Szabó2f523792001-02-27 09:59:48 +000019#include "libiptc/libip6tc.h"
Jan Engelhardt33690a12008-02-11 00:54:00 +010020#include "ip6tables-multi.h"
András Kis-Szabó2f523792001-02-27 09:59:48 +000021
22#ifdef DEBUG
23#define DEBUGP(x, args...) fprintf(stderr, x, ## args)
24#else
Max Kellermann5b76f682008-01-29 13:42:48 +000025#define DEBUGP(x, args...)
András Kis-Szabó2f523792001-02-27 09:59:48 +000026#endif
27
András Kis-Szabó2f523792001-02-27 09:59:48 +000028static int binary = 0, counters = 0, verbose = 0, noflush = 0;
29
30/* Keeping track of external matches and targets. */
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +010031static const struct option options[] = {
32 {.name = "binary", .has_arg = false, .val = 'b'},
33 {.name = "counters", .has_arg = false, .val = 'c'},
34 {.name = "verbose", .has_arg = false, .val = 'v'},
35 {.name = "test", .has_arg = false, .val = 't'},
36 {.name = "help", .has_arg = false, .val = 'h'},
37 {.name = "noflush", .has_arg = false, .val = 'n'},
38 {.name = "modprobe", .has_arg = true, .val = 'M'},
39 {NULL},
András Kis-Szabó2f523792001-02-27 09:59:48 +000040};
41
42static void print_usage(const char *name, const char *version) __attribute__((noreturn));
43
44static void print_usage(const char *name, const char *version)
45{
Martin Josefsson60044392004-02-02 20:12:33 +000046 fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-t] [-h]\n"
András Kis-Szabó2f523792001-02-27 09:59:48 +000047 " [ --binary ]\n"
48 " [ --counters ]\n"
49 " [ --verbose ]\n"
Martin Josefsson4e5e29a2004-02-02 19:59:17 +000050 " [ --test ]\n"
András Kis-Szabó2f523792001-02-27 09:59:48 +000051 " [ --help ]\n"
Harald Welte58918652001-06-16 18:25:25 +000052 " [ --noflush ]\n"
Max Kellermann5b76f682008-01-29 13:42:48 +000053 " [ --modprobe=<command>]\n", name);
54
András Kis-Szabó2f523792001-02-27 09:59:48 +000055 exit(1);
56}
57
Jan Engelhardtc3541052008-11-18 12:26:26 +010058static struct ip6tc_handle *create_handle(const char *tablename)
András Kis-Szabó2f523792001-02-27 09:59:48 +000059{
Jan Engelhardtfd187312008-11-10 16:59:27 +010060 struct ip6tc_handle *handle;
András Kis-Szabó2f523792001-02-27 09:59:48 +000061
62 handle = ip6tc_init(tablename);
Harald Welte58918652001-06-16 18:25:25 +000063
64 if (!handle) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000065 /* try to insmod the module if iptc_init failed */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010066 xtables_load_ko(xtables_modprobe_program, false);
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000067 handle = ip6tc_init(tablename);
Harald Welte58918652001-06-16 18:25:25 +000068 }
69
András Kis-Szabó2f523792001-02-27 09:59:48 +000070 if (!handle) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +010071 xtables_error(PARAMETER_PROBLEM, "%s: unable to initialize "
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -050072 "table '%s'\n", ip6tables_globals.program_name,
73 tablename);
András Kis-Szabó2f523792001-02-27 09:59:48 +000074 exit(1);
75 }
76 return handle;
77}
78
Lutz Jaenickee78c69c2006-12-09 13:06:04 +000079static int parse_counters(char *string, struct ip6t_counters *ctr)
András Kis-Szabó2f523792001-02-27 09:59:48 +000080{
Patrick McHardy875441e2007-10-17 08:48:58 +000081 unsigned long long pcnt, bcnt;
82 int ret;
Patrick McHardy31f51c62007-10-16 08:49:31 +000083
Jan Engelhardtbb8be302011-01-31 02:41:23 +010084 ret = sscanf(string, "[%llu:%llu]", &pcnt, &bcnt);
Patrick McHardy875441e2007-10-17 08:48:58 +000085 ctr->pcnt = pcnt;
86 ctr->bcnt = bcnt;
87 return ret == 2;
András Kis-Szabó2f523792001-02-27 09:59:48 +000088}
89
Harald Welte885c6eb2001-10-04 08:30:46 +000090/* global new argv and argc */
91static char *newargv[255];
92static int newargc;
93
Max Kellermann5b76f682008-01-29 13:42:48 +000094/* function adding one argument to newargv, updating newargc
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000095 * returns true if argument added, false otherwise */
Harald Welte885c6eb2001-10-04 08:30:46 +000096static int add_argv(char *what) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000097 DEBUGP("add_argv: %s\n", what);
Jan Engelhardt2c69b552009-04-30 19:32:02 +020098 if (what && newargc + 1 < ARRAY_SIZE(newargv)) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000099 newargv[newargc] = strdup(what);
100 newargc++;
101 return 1;
Jan Engelhardt6a0448e2011-01-31 02:34:49 +0100102 } else {
103 xtables_error(PARAMETER_PROBLEM,
104 "Parser cannot handle more arguments\n");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000105 return 0;
Jan Engelhardt6a0448e2011-01-31 02:34:49 +0100106 }
Harald Welte885c6eb2001-10-04 08:30:46 +0000107}
108
109static void free_argv(void) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000110 int i;
Harald Welte885c6eb2001-10-04 08:30:46 +0000111
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000112 for (i = 0; i < newargc; i++)
113 free(newargv[i]);
Harald Welte885c6eb2001-10-04 08:30:46 +0000114}
115
Hann-Huei Chiou4bc48332007-10-23 14:22:34 +0000116#ifdef IPTABLES_MULTI
117int ip6tables_restore_main(int argc, char *argv[])
118#else
András Kis-Szabó2f523792001-02-27 09:59:48 +0000119int main(int argc, char *argv[])
Hann-Huei Chiou4bc48332007-10-23 14:22:34 +0000120#endif
András Kis-Szabó2f523792001-02-27 09:59:48 +0000121{
Jan Engelhardtfd187312008-11-10 16:59:27 +0100122 struct ip6tc_handle *handle = NULL;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000123 char buffer[10240];
András Kis-Szabó2f523792001-02-27 09:59:48 +0000124 int c;
125 char curtable[IP6T_TABLE_MAXNAMELEN + 1];
András Kis-Szabó2f523792001-02-27 09:59:48 +0000126 FILE *in;
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000127 int in_table = 0, testing = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000128
Harald Weltea8658ca2003-03-05 07:46:15 +0000129 line = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000130
Jamal Hadi Salim617d3d12009-02-11 16:28:31 -0500131 ip6tables_globals.program_name = "ip6tables-restore";
Jamal Hadi Salim7e4db2f2009-02-13 09:14:17 -0500132 c = xtables_init_all(&ip6tables_globals, NFPROTO_IPV6);
133 if (c < 0) {
134 fprintf(stderr, "%s/%s Failed to initialize xtables\n",
135 ip6tables_globals.program_name,
136 ip6tables_globals.program_version);
137 exit(1);
138 }
Jan Engelhardtb79ec692009-07-23 17:41:21 +0200139#if defined(ALL_INCLUSIVE) || defined(NO_SHARED_LIBS)
Harald Welte3efb6ea2001-08-06 18:50:21 +0000140 init_extensions();
141#endif
142
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000143 while ((c = getopt_long(argc, argv, "bcvthnM:", options, NULL)) != -1) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000144 switch (c) {
145 case 'b':
146 binary = 1;
147 break;
148 case 'c':
149 counters = 1;
150 break;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000151 case 'v':
152 verbose = 1;
153 break;
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000154 case 't':
155 testing = 1;
156 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000157 case 'h':
158 print_usage("ip6tables-restore",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100159 IPTABLES_VERSION);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000160 break;
161 case 'n':
162 noflush = 1;
163 break;
Harald Welte58918652001-06-16 18:25:25 +0000164 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100165 xtables_modprobe_program = optarg;
Harald Welte58918652001-06-16 18:25:25 +0000166 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000167 }
168 }
Max Kellermann5b76f682008-01-29 13:42:48 +0000169
András Kis-Szabó2f523792001-02-27 09:59:48 +0000170 if (optind == argc - 1) {
Maciej Zenczykowskia2397282011-04-04 15:30:32 +0200171 in = fopen(argv[optind], "re");
András Kis-Szabó2f523792001-02-27 09:59:48 +0000172 if (!in) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000173 fprintf(stderr, "Can't open %s: %s\n", argv[optind],
András Kis-Szabó2f523792001-02-27 09:59:48 +0000174 strerror(errno));
175 exit(1);
176 }
177 }
178 else if (optind < argc) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000179 fprintf(stderr, "Unknown arguments found on commandline\n");
András Kis-Szabó2f523792001-02-27 09:59:48 +0000180 exit(1);
181 }
182 else in = stdin;
Max Kellermann5b76f682008-01-29 13:42:48 +0000183
András Kis-Szabó2f523792001-02-27 09:59:48 +0000184 /* Grab standard input. */
185 while (fgets(buffer, sizeof(buffer), in)) {
Martin Josefssoncc536282004-02-02 20:14:56 +0000186 int ret = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000187
188 line++;
Martin Josefsson5065afa2004-01-31 19:33:47 +0000189 if (buffer[0] == '\n')
190 continue;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000191 else if (buffer[0] == '#') {
Martin Josefsson3673dcb2004-01-31 19:41:49 +0000192 if (verbose)
193 fputs(buffer, stdout);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000194 continue;
Harald Weltea8658ca2003-03-05 07:46:15 +0000195 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000196 if (!testing) {
197 DEBUGP("Calling commit\n");
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100198 ret = ip6tc_commit(handle);
199 ip6tc_free(handle);
200 handle = NULL;
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000201 } else {
202 DEBUGP("Not calling commit, testing\n");
203 ret = 1;
204 }
Harald Weltea8658ca2003-03-05 07:46:15 +0000205 in_table = 0;
206 } else if ((buffer[0] == '*') && (!in_table)) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000207 /* New table */
208 char *table;
209
210 table = strtok(buffer+1, " \t\n");
211 DEBUGP("line %u, table '%s'\n", line, table);
212 if (!table) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100213 xtables_error(PARAMETER_PROBLEM,
András Kis-Szabó2f523792001-02-27 09:59:48 +0000214 "%s: line %u table name invalid\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500215 ip6tables_globals.program_name,
216 line);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000217 exit(1);
218 }
219 strncpy(curtable, table, IP6T_TABLE_MAXNAMELEN);
Karsten Desler073df8f2004-01-31 15:33:55 +0000220 curtable[IP6T_TABLE_MAXNAMELEN] = '\0';
András Kis-Szabó2f523792001-02-27 09:59:48 +0000221
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000222 if (handle)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100223 ip6tc_free(handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000224
Jan Engelhardtc3541052008-11-18 12:26:26 +0100225 handle = create_handle(table);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000226 if (noflush == 0) {
227 DEBUGP("Cleaning all chains of table '%s'\n",
228 table);
Max Kellermann5b76f682008-01-29 13:42:48 +0000229 for_each_chain(flush_entries, verbose, 1,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100230 handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000231
András Kis-Szabó2f523792001-02-27 09:59:48 +0000232 DEBUGP("Deleting all user-defined chains "
233 "of table '%s'\n", table);
Max Kellermann5b76f682008-01-29 13:42:48 +0000234 for_each_chain(delete_chain, verbose, 0,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100235 handle);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000236 }
237
238 ret = 1;
Harald Weltea8658ca2003-03-05 07:46:15 +0000239 in_table = 1;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000240
Harald Weltea8658ca2003-03-05 07:46:15 +0000241 } else if ((buffer[0] == ':') && (in_table)) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000242 /* New chain. */
243 char *policy, *chain;
244
245 chain = strtok(buffer+1, " \t\n");
246 DEBUGP("line %u, chain '%s'\n", line, chain);
247 if (!chain) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100248 xtables_error(PARAMETER_PROBLEM,
András Kis-Szabó2f523792001-02-27 09:59:48 +0000249 "%s: line %u chain name invalid\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500250 ip6tables_globals.program_name,
251 line);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000252 exit(1);
253 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000254
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200255 if (strlen(chain) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt21d12832010-03-16 16:49:21 +0100256 xtables_error(PARAMETER_PROBLEM,
257 "Invalid chain name `%s' "
258 "(%u chars max)",
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200259 chain, XT_EXTENSION_MAXNAMELEN - 1);
Jan Engelhardt21d12832010-03-16 16:49:21 +0100260
Harald Weltedf1c71c2004-08-30 16:00:32 +0000261 if (ip6tc_builtin(chain, handle) <= 0) {
Charlie Brady595e4932005-06-12 15:54:15 +0000262 if (noflush && ip6tc_is_chain(chain, handle)) {
263 DEBUGP("Flushing existing user defined chain '%s'\n", chain);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100264 if (!ip6tc_flush_entries(chain, handle))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100265 xtables_error(PARAMETER_PROBLEM,
Charlie Brady595e4932005-06-12 15:54:15 +0000266 "error flushing chain "
267 "'%s':%s\n", chain,
268 strerror(errno));
269 } else {
270 DEBUGP("Creating new chain '%s'\n", chain);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100271 if (!ip6tc_create_chain(chain, handle))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100272 xtables_error(PARAMETER_PROBLEM,
Charlie Brady595e4932005-06-12 15:54:15 +0000273 "error creating chain "
274 "'%s':%s\n", chain,
275 strerror(errno));
276 }
Harald Welte885c6eb2001-10-04 08:30:46 +0000277 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000278
279 policy = strtok(NULL, " \t\n");
280 DEBUGP("line %u, policy '%s'\n", line, policy);
281 if (!policy) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100282 xtables_error(PARAMETER_PROBLEM,
András Kis-Szabó2f523792001-02-27 09:59:48 +0000283 "%s: line %u policy invalid\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500284 ip6tables_globals.program_name,
285 line);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000286 exit(1);
287 }
288
289 if (strcmp(policy, "-") != 0) {
290 struct ip6t_counters count;
291
292 if (counters) {
293 char *ctrs;
294 ctrs = strtok(NULL, " \t\n");
295
Harald Welte6f38a302006-02-09 14:35:38 +0000296 if (!ctrs || !parse_counters(ctrs, &count))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100297 xtables_error(PARAMETER_PROBLEM,
Harald Welte6f38a302006-02-09 14:35:38 +0000298 "invalid policy counters "
299 "for chain '%s'\n", chain);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000300
301 } else {
Max Kellermann5b76f682008-01-29 13:42:48 +0000302 memset(&count, 0,
András Kis-Szabó2f523792001-02-27 09:59:48 +0000303 sizeof(struct ip6t_counters));
304 }
305
306 DEBUGP("Setting policy of chain %s to %s\n",
307 chain, policy);
308
309 if (!ip6tc_set_policy(chain, policy, &count,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100310 handle))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100311 xtables_error(OTHER_PROBLEM,
András Kis-Szabó2f523792001-02-27 09:59:48 +0000312 "Can't set policy `%s'"
313 " on `%s' line %u: %s\n",
Rob Leslief6d64492010-09-28 00:43:00 -0700314 policy, chain, line,
András Kis-Szabó2f523792001-02-27 09:59:48 +0000315 ip6tc_strerror(errno));
316 }
317
318 ret = 1;
319
Harald Weltea8658ca2003-03-05 07:46:15 +0000320 } else if (in_table) {
Harald Welte885c6eb2001-10-04 08:30:46 +0000321 int a;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000322 char *ptr = buffer;
323 char *pcnt = NULL;
324 char *bcnt = NULL;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000325 char *parsestart;
Harald Welte885c6eb2001-10-04 08:30:46 +0000326
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000327 /* the parser */
Max Kellermann3bf54df2008-01-29 13:45:29 +0000328 char *curchar;
329 int quote_open, escaped;
330 size_t param_len;
Harald Welte885c6eb2001-10-04 08:30:46 +0000331
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000332 /* reset the newargv */
333 newargc = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000334
335 if (buffer[0] == '[') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000336 /* we have counters in our input */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000337 ptr = strchr(buffer, ']');
338 if (!ptr)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100339 xtables_error(PARAMETER_PROBLEM,
András Kis-Szabó2f523792001-02-27 09:59:48 +0000340 "Bad line %u: need ]\n",
341 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000342
András Kis-Szabó2f523792001-02-27 09:59:48 +0000343 pcnt = strtok(buffer+1, ":");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000344 if (!pcnt)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100345 xtables_error(PARAMETER_PROBLEM,
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000346 "Bad line %u: need :\n",
347 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000348
András Kis-Szabó2f523792001-02-27 09:59:48 +0000349 bcnt = strtok(NULL, "]");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000350 if (!bcnt)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100351 xtables_error(PARAMETER_PROBLEM,
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000352 "Bad line %u: need ]\n",
353 line);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000354
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000355 /* start command parsing after counter */
356 parsestart = ptr + 1;
357 } else {
358 /* start command parsing at start of line */
359 parsestart = buffer;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000360 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000361
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000362 add_argv(argv[0]);
363 add_argv("-t");
Jan Engelhardt00696592011-01-31 02:39:46 +0100364 add_argv(curtable);
Max Kellermann5b76f682008-01-29 13:42:48 +0000365
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000366 if (counters && pcnt && bcnt) {
367 add_argv("--set-counters");
368 add_argv((char *) pcnt);
369 add_argv((char *) bcnt);
370 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000371
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000372 /* After fighting with strtok enough, here's now
373 * a 'real' parser. According to Rusty I'm now no
374 * longer a real hacker, but I can live with that */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000375
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000376 quote_open = 0;
Max Kellermann3bf54df2008-01-29 13:45:29 +0000377 escaped = 0;
378 param_len = 0;
Max Kellermann5b76f682008-01-29 13:42:48 +0000379
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000380 for (curchar = parsestart; *curchar; curchar++) {
Max Kellermann3bf54df2008-01-29 13:45:29 +0000381 char param_buffer[1024];
382
Max Kellermann3bf54df2008-01-29 13:45:29 +0000383 if (quote_open) {
Max Kellermannfe05c762008-01-29 13:46:01 +0000384 if (escaped) {
385 param_buffer[param_len++] = *curchar;
386 escaped = 0;
387 continue;
388 } else if (*curchar == '\\') {
Max Kellermann3bf54df2008-01-29 13:45:29 +0000389 escaped = 1;
390 continue;
391 } else if (*curchar == '"') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000392 quote_open = 0;
393 *curchar = ' ';
394 } else {
Max Kellermann3bf54df2008-01-29 13:45:29 +0000395 param_buffer[param_len++] = *curchar;
396 continue;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000397 }
Max Kellermann3bf54df2008-01-29 13:45:29 +0000398 } else {
399 if (*curchar == '"') {
400 quote_open = 1;
401 continue;
402 }
403 }
404
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000405 if (*curchar == ' '
406 || *curchar == '\t'
407 || * curchar == '\n') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000408 if (!param_len) {
409 /* two spaces? */
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000410 continue;
411 }
Max Kellermann3bf54df2008-01-29 13:45:29 +0000412
413 param_buffer[param_len] = '\0';
Harald Weltebb41f882001-10-21 14:11:54 +0000414
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000415 /* check if table name specified */
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +0200416 if (!strncmp(param_buffer, "-t", 2)
Harald Welte48a6f902001-10-22 15:16:21 +0000417 || !strncmp(param_buffer, "--table", 8)) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100418 xtables_error(PARAMETER_PROBLEM,
Harald Weltebb41f882001-10-21 14:11:54 +0000419 "Line %u seems to have a "
420 "-t table option.\n", line);
421 exit(1);
422 }
423
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000424 add_argv(param_buffer);
Max Kellermann3bf54df2008-01-29 13:45:29 +0000425 param_len = 0;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000426 } else {
Max Kellermann3bf54df2008-01-29 13:45:29 +0000427 /* regular character, copy to buffer */
428 param_buffer[param_len++] = *curchar;
429
430 if (param_len >= sizeof(param_buffer))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100431 xtables_error(PARAMETER_PROBLEM,
Max Kellermann3bf54df2008-01-29 13:45:29 +0000432 "Parameter too long!");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000433 }
434 }
Harald Welte885c6eb2001-10-04 08:30:46 +0000435
436 DEBUGP("calling do_command6(%u, argv, &%s, handle):\n",
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000437 newargc, curtable);
Harald Welte885c6eb2001-10-04 08:30:46 +0000438
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000439 for (a = 0; a < newargc; a++)
András Kis-Szabó2f523792001-02-27 09:59:48 +0000440 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
441
Max Kellermann5b76f682008-01-29 13:42:48 +0000442 ret = do_command6(newargc, newargv,
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000443 &newargv[2], &handle);
Harald Welte885c6eb2001-10-04 08:30:46 +0000444
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000445 free_argv();
Henrik Nordstromd2137602008-05-12 20:51:45 +0200446 fflush(stdout);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000447 }
448 if (!ret) {
449 fprintf(stderr, "%s: line %u failed\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500450 ip6tables_globals.program_name,
451 line);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000452 exit(1);
453 }
454 }
Martin Josefssone0dc5812004-02-02 19:58:36 +0000455 if (in_table) {
456 fprintf(stderr, "%s: COMMIT expected at line %u\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500457 ip6tables_globals.program_name,
458 line + 1);
Martin Josefssone0dc5812004-02-02 19:58:36 +0000459 exit(1);
460 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000461
Jan Engelhardtf1afcc82009-06-10 13:52:58 +0200462 if (in != NULL)
463 fclose(in);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000464 return 0;
465}