blob: c2cc58c864fa469bfabbe9297d2a3b4e1b87b72b [file] [log] [blame]
Max Kellermann5b76f682008-01-29 13:42:48 +00001/* Code to restore the iptables state, from file by iptables-save.
Harald Welte10a907f2002-08-07 09:07:41 +00002 * (C) 2000-2002 by Harald Welte <laforge@gnumonks.org>
Harald Welteae1ff9f2000-12-01 14:26:20 +00003 * based on previous code from Rusty Russell <rusty@linuxcare.com.au>
4 *
Harald Welte10a907f2002-08-07 09:07:41 +00005 * This code is distributed under the terms of GNU GPL v2
Harald Welteae1ff9f2000-12-01 14:26:20 +00006 */
7
Marc Bouchere6869a82000-03-20 06:03:29 +00008#include <getopt.h>
9#include <sys/errno.h>
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010010#include <stdbool.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000011#include <string.h>
12#include <stdio.h>
Harald Welteae1ff9f2000-12-01 14:26:20 +000013#include <stdlib.h>
14#include "iptables.h"
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000015#include "xtables.h"
Harald Welteae1ff9f2000-12-01 14:26:20 +000016#include "libiptc/libiptc.h"
Jan Engelhardt33690a12008-02-11 00:54:00 +010017#include "iptables-multi.h"
Harald Welteae1ff9f2000-12-01 14:26:20 +000018
19#ifdef DEBUG
Harald Weltec45c3152000-12-06 08:11:24 +000020#define DEBUGP(x, args...) fprintf(stderr, x, ## args)
Harald Welteae1ff9f2000-12-01 14:26:20 +000021#else
Max Kellermann5b76f682008-01-29 13:42:48 +000022#define DEBUGP(x, args...)
Harald Welteae1ff9f2000-12-01 14:26:20 +000023#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000024
Harald Weltea9d27082000-12-19 16:10:42 +000025static int binary = 0, counters = 0, verbose = 0, noflush = 0;
26
Marc Bouchere6869a82000-03-20 06:03:29 +000027/* Keeping track of external matches and targets. */
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +010028static const struct option options[] = {
29 {.name = "binary", .has_arg = false, .val = 'b'},
30 {.name = "counters", .has_arg = false, .val = 'c'},
31 {.name = "verbose", .has_arg = false, .val = 'v'},
32 {.name = "test", .has_arg = false, .val = 't'},
33 {.name = "help", .has_arg = false, .val = 'h'},
34 {.name = "noflush", .has_arg = false, .val = 'n'},
35 {.name = "modprobe", .has_arg = true, .val = 'M'},
36 {.name = "table", .has_arg = true, .val = 'T'},
37 {NULL},
Marc Bouchere6869a82000-03-20 06:03:29 +000038};
39
40static void print_usage(const char *name, const char *version) __attribute__((noreturn));
41
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -050042#define prog_name iptables_globals.program_name
43
Marc Bouchere6869a82000-03-20 06:03:29 +000044static void print_usage(const char *name, const char *version)
45{
Martin Josefssonbb2f68a2004-02-01 22:03:27 +000046 fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-t] [-h]\n"
Harald Weltea9d27082000-12-19 16:10:42 +000047 " [ --binary ]\n"
48 " [ --counters ]\n"
49 " [ --verbose ]\n"
Martin Josefssonbb2f68a2004-02-01 22:03:27 +000050 " [ --test ]\n"
Harald Weltea9d27082000-12-19 16:10:42 +000051 " [ --help ]\n"
Harald Welte58918652001-06-16 18:25:25 +000052 " [ --noflush ]\n"
Peter Warasinb9cde3a2007-11-05 19:35:31 +000053 " [ --table=<TABLE> ]\n"
Max Kellermann5b76f682008-01-29 13:42:48 +000054 " [ --modprobe=<command>]\n", name);
55
Marc Bouchere6869a82000-03-20 06:03:29 +000056 exit(1);
57}
58
Jan Engelhardtc3541052008-11-18 12:26:26 +010059static struct iptc_handle *create_handle(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +000060{
Jan Engelhardtfd187312008-11-10 16:59:27 +010061 struct iptc_handle *handle;
Marc Bouchere6869a82000-03-20 06:03:29 +000062
Harald Welteae1ff9f2000-12-01 14:26:20 +000063 handle = iptc_init(tablename);
Harald Welte58918652001-06-16 18:25:25 +000064
65 if (!handle) {
66 /* try to insmod the module if iptc_init failed */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010067 xtables_load_ko(xtables_modprobe_program, false);
Harald Welte58918652001-06-16 18:25:25 +000068 handle = iptc_init(tablename);
69 }
70
Harald Welteae1ff9f2000-12-01 14:26:20 +000071 if (!handle) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +010072 xtables_error(PARAMETER_PROBLEM, "%s: unable to initialize "
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -050073 "table '%s'\n", prog_name, tablename);
Harald Welteae1ff9f2000-12-01 14:26:20 +000074 exit(1);
Marc Bouchere6869a82000-03-20 06:03:29 +000075 }
Harald Welteae1ff9f2000-12-01 14:26:20 +000076 return handle;
Marc Bouchere6869a82000-03-20 06:03:29 +000077}
78
Lutz Jaenickee78c69c2006-12-09 13:06:04 +000079static int parse_counters(char *string, struct ipt_counters *ctr)
Harald Welted8e65632001-01-05 15:20:07 +000080{
Patrick McHardy875441e2007-10-17 08:48:58 +000081 unsigned long long pcnt, bcnt;
82 int ret;
Patrick McHardyb73691f2007-09-05 14:10:53 +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;
Harald Welted8e65632001-01-05 15:20:07 +000088}
Harald Welteae1ff9f2000-12-01 14:26:20 +000089
Harald Welte9f7fa492001-03-15 15:12:02 +000090/* global new argv and argc */
Harald Welte26e643f2001-05-03 20:50:03 +000091static char *newargv[255];
Harald Welte9f7fa492001-03-15 15:12:02 +000092static int newargc;
93
94/* function adding one argument to newargv, updating newargc
95 * returns true if argument added, false otherwise */
96static int add_argv(char *what) {
Harald Weltebb41f882001-10-21 14:11:54 +000097 DEBUGP("add_argv: %s\n", what);
Jan Engelhardt2c69b552009-04-30 19:32:02 +020098 if (what && newargc + 1 < ARRAY_SIZE(newargv)) {
Harald Welte26e643f2001-05-03 20:50:03 +000099 newargv[newargc] = strdup(what);
Harald Welte9f7fa492001-03-15 15:12:02 +0000100 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");
Harald Welte9f7fa492001-03-15 15:12:02 +0000105 return 0;
Jan Engelhardt6a0448e2011-01-31 02:34:49 +0100106 }
Harald Welte9f7fa492001-03-15 15:12:02 +0000107}
108
Harald Welte26e643f2001-05-03 20:50:03 +0000109static void free_argv(void) {
110 int i;
111
112 for (i = 0; i < newargc; i++)
113 free(newargv[i]);
114}
115
Bastiaan Bakker4e3771f2004-06-25 11:18:57 +0000116#ifdef IPTABLES_MULTI
117int
118iptables_restore_main(int argc, char *argv[])
119#else
120int
121main(int argc, char *argv[])
122#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000123{
Jan Engelhardtfd187312008-11-10 16:59:27 +0100124 struct iptc_handle *handle = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000125 char buffer[10240];
Harald Weltea9d27082000-12-19 16:10:42 +0000126 int c;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000127 char curtable[IPT_TABLE_MAXNAMELEN + 1];
Marc Bouchere6869a82000-03-20 06:03:29 +0000128 FILE *in;
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000129 int in_table = 0, testing = 0;
Patrick McHardy0ea82bc2008-06-07 15:15:29 +0200130 const char *tablename = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000131
Illes Marci63e90632003-03-03 08:08:37 +0000132 line = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000133
Jamal Hadi Salim617d3d12009-02-11 16:28:31 -0500134 iptables_globals.program_name = "iptables-restore";
Jamal Hadi Salim7e4db2f2009-02-13 09:14:17 -0500135 c = xtables_init_all(&iptables_globals, NFPROTO_IPV4);
136 if (c < 0) {
137 fprintf(stderr, "%s/%s Failed to initialize xtables\n",
138 iptables_globals.program_name,
139 iptables_globals.program_version);
140 exit(1);
141 }
Jan Engelhardtb79ec692009-07-23 17:41:21 +0200142#if defined(ALL_INCLUSIVE) || defined(NO_SHARED_LIBS)
Harald Welte3efb6ea2001-08-06 18:50:21 +0000143 init_extensions();
144#endif
145
Peter Warasinb9cde3a2007-11-05 19:35:31 +0000146 while ((c = getopt_long(argc, argv, "bcvthnM:T:", options, NULL)) != -1) {
Harald Weltea9d27082000-12-19 16:10:42 +0000147 switch (c) {
148 case 'b':
149 binary = 1;
150 break;
151 case 'c':
152 counters = 1;
153 break;
Marc Boucher1277b592001-12-06 15:06:34 +0000154 case 'v':
155 verbose = 1;
156 break;
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000157 case 't':
158 testing = 1;
159 break;
Harald Weltea9d27082000-12-19 16:10:42 +0000160 case 'h':
161 print_usage("iptables-restore",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100162 IPTABLES_VERSION);
Harald Weltea9d27082000-12-19 16:10:42 +0000163 break;
164 case 'n':
165 noflush = 1;
166 break;
Harald Welte58918652001-06-16 18:25:25 +0000167 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100168 xtables_modprobe_program = optarg;
Harald Welte58918652001-06-16 18:25:25 +0000169 break;
Peter Warasinb9cde3a2007-11-05 19:35:31 +0000170 case 'T':
171 tablename = optarg;
172 break;
Harald Weltea9d27082000-12-19 16:10:42 +0000173 }
174 }
Max Kellermann5b76f682008-01-29 13:42:48 +0000175
Marc Bouchere6869a82000-03-20 06:03:29 +0000176 if (optind == argc - 1) {
Maciej Zenczykowskia2397282011-04-04 15:30:32 +0200177 in = fopen(argv[optind], "re");
Marc Bouchere6869a82000-03-20 06:03:29 +0000178 if (!in) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000179 fprintf(stderr, "Can't open %s: %s\n", argv[optind],
Marc Bouchere6869a82000-03-20 06:03:29 +0000180 strerror(errno));
181 exit(1);
182 }
183 }
184 else if (optind < argc) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000185 fprintf(stderr, "Unknown arguments found on commandline\n");
Marc Bouchere6869a82000-03-20 06:03:29 +0000186 exit(1);
187 }
188 else in = stdin;
Max Kellermann5b76f682008-01-29 13:42:48 +0000189
Marc Bouchere6869a82000-03-20 06:03:29 +0000190 /* Grab standard input. */
191 while (fgets(buffer, sizeof(buffer), in)) {
Harald Welteb3f22192003-03-06 11:56:31 +0000192 int ret = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000193
194 line++;
Martin Josefsson5065afa2004-01-31 19:33:47 +0000195 if (buffer[0] == '\n')
196 continue;
Marc Bouchere6869a82000-03-20 06:03:29 +0000197 else if (buffer[0] == '#') {
Martin Josefsson3673dcb2004-01-31 19:41:49 +0000198 if (verbose)
199 fputs(buffer, stdout);
Marc Bouchere6869a82000-03-20 06:03:29 +0000200 continue;
Illes Marci26100fa2003-03-03 08:05:07 +0000201 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000202 if (!testing) {
203 DEBUGP("Calling commit\n");
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100204 ret = iptc_commit(handle);
205 iptc_free(handle);
206 handle = NULL;
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000207 } else {
208 DEBUGP("Not calling commit, testing\n");
209 ret = 1;
210 }
Illes Marci26100fa2003-03-03 08:05:07 +0000211 in_table = 0;
Harald Welteb3f22192003-03-06 11:56:31 +0000212 } else if ((buffer[0] == '*') && (!in_table)) {
Harald Welteae1ff9f2000-12-01 14:26:20 +0000213 /* New table */
214 char *table;
215
216 table = strtok(buffer+1, " \t\n");
217 DEBUGP("line %u, table '%s'\n", line, table);
218 if (!table) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100219 xtables_error(PARAMETER_PROBLEM,
Harald Welteae1ff9f2000-12-01 14:26:20 +0000220 "%s: line %u table name invalid\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500221 prog_name, line);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000222 exit(1);
223 }
224 strncpy(curtable, table, IPT_TABLE_MAXNAMELEN);
Karsten Desler073df8f2004-01-31 15:33:55 +0000225 curtable[IPT_TABLE_MAXNAMELEN] = '\0';
Harald Welteae1ff9f2000-12-01 14:26:20 +0000226
Peter Warasinb9cde3a2007-11-05 19:35:31 +0000227 if (tablename && (strcmp(tablename, table) != 0))
228 continue;
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000229 if (handle)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100230 iptc_free(handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000231
Jan Engelhardtc3541052008-11-18 12:26:26 +0100232 handle = create_handle(table);
Harald Weltea9d27082000-12-19 16:10:42 +0000233 if (noflush == 0) {
234 DEBUGP("Cleaning all chains of table '%s'\n",
235 table);
Max Kellermann5b76f682008-01-29 13:42:48 +0000236 for_each_chain(flush_entries, verbose, 1,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100237 handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000238
Harald Weltea9d27082000-12-19 16:10:42 +0000239 DEBUGP("Deleting all user-defined chains "
240 "of table '%s'\n", table);
Max Kellermann5b76f682008-01-29 13:42:48 +0000241 for_each_chain(delete_chain, verbose, 0,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100242 handle);
Harald Weltea9d27082000-12-19 16:10:42 +0000243 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000244
245 ret = 1;
Illes Marci26100fa2003-03-03 08:05:07 +0000246 in_table = 1;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000247
Illes Marci26100fa2003-03-03 08:05:07 +0000248 } else if ((buffer[0] == ':') && (in_table)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000249 /* New chain. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000250 char *policy, *chain;
Marc Bouchere6869a82000-03-20 06:03:29 +0000251
Marc Bouchere6869a82000-03-20 06:03:29 +0000252 chain = strtok(buffer+1, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000253 DEBUGP("line %u, chain '%s'\n", line, chain);
Marc Bouchere6869a82000-03-20 06:03:29 +0000254 if (!chain) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100255 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000256 "%s: line %u chain name invalid\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500257 prog_name, line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000258 exit(1);
259 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000260
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200261 if (strlen(chain) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt21d12832010-03-16 16:49:21 +0100262 xtables_error(PARAMETER_PROBLEM,
263 "Invalid chain name `%s' "
264 "(%u chars max)",
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200265 chain, XT_EXTENSION_MAXNAMELEN - 1);
Jan Engelhardt21d12832010-03-16 16:49:21 +0100266
Harald Welte3a506ac2004-08-30 16:00:09 +0000267 if (iptc_builtin(chain, handle) <= 0) {
Charlie Brady595e4932005-06-12 15:54:15 +0000268 if (noflush && iptc_is_chain(chain, handle)) {
269 DEBUGP("Flushing existing user defined chain '%s'\n", chain);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100270 if (!iptc_flush_entries(chain, handle))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100271 xtables_error(PARAMETER_PROBLEM,
Charlie Brady595e4932005-06-12 15:54:15 +0000272 "error flushing chain "
273 "'%s':%s\n", chain,
274 strerror(errno));
275 } else {
276 DEBUGP("Creating new chain '%s'\n", chain);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100277 if (!iptc_create_chain(chain, handle))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100278 xtables_error(PARAMETER_PROBLEM,
Charlie Brady595e4932005-06-12 15:54:15 +0000279 "error creating chain "
280 "'%s':%s\n", chain,
281 strerror(errno));
282 }
Harald Welte9f7fa492001-03-15 15:12:02 +0000283 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000284
Marc Bouchere6869a82000-03-20 06:03:29 +0000285 policy = strtok(NULL, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000286 DEBUGP("line %u, policy '%s'\n", line, policy);
Marc Bouchere6869a82000-03-20 06:03:29 +0000287 if (!policy) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100288 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000289 "%s: line %u policy invalid\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500290 prog_name, line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000291 exit(1);
292 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000293
294 if (strcmp(policy, "-") != 0) {
Harald Welted8e65632001-01-05 15:20:07 +0000295 struct ipt_counters count;
296
297 if (counters) {
298 char *ctrs;
299 ctrs = strtok(NULL, " \t\n");
300
Harald Welte6f38a302006-02-09 14:35:38 +0000301 if (!ctrs || !parse_counters(ctrs, &count))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100302 xtables_error(PARAMETER_PROBLEM,
Harald Welte6f38a302006-02-09 14:35:38 +0000303 "invalid policy counters "
304 "for chain '%s'\n", chain);
Harald Welted8e65632001-01-05 15:20:07 +0000305
306 } else {
Max Kellermann5b76f682008-01-29 13:42:48 +0000307 memset(&count, 0,
Harald Welted8e65632001-01-05 15:20:07 +0000308 sizeof(struct ipt_counters));
309 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000310
311 DEBUGP("Setting policy of chain %s to %s\n",
312 chain, policy);
313
Harald Welted8e65632001-01-05 15:20:07 +0000314 if (!iptc_set_policy(chain, policy, &count,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100315 handle))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100316 xtables_error(OTHER_PROBLEM,
Harald Welteae1ff9f2000-12-01 14:26:20 +0000317 "Can't set policy `%s'"
318 " on `%s' line %u: %s\n",
Rob Leslief6d64492010-09-28 00:43:00 -0700319 policy, chain, line,
Harald Welteae1ff9f2000-12-01 14:26:20 +0000320 iptc_strerror(errno));
321 }
322
323 ret = 1;
324
Illes Marci26100fa2003-03-03 08:05:07 +0000325 } else if (in_table) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000326 int a;
Marc Bouchere6869a82000-03-20 06:03:29 +0000327 char *ptr = buffer;
Harald Welteccd49e52001-01-23 22:54:34 +0000328 char *pcnt = NULL;
329 char *bcnt = NULL;
Harald Welte9f7fa492001-03-15 15:12:02 +0000330 char *parsestart;
331
Harald Welte26e643f2001-05-03 20:50:03 +0000332 /* the parser */
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000333 char *curchar;
Max Kellermann3bf54df2008-01-29 13:45:29 +0000334 int quote_open, escaped;
Max Kellermannb4ef34f2008-01-29 13:43:35 +0000335 size_t param_len;
Harald Welte26e643f2001-05-03 20:50:03 +0000336
Harald Welte9f7fa492001-03-15 15:12:02 +0000337 /* reset the newargv */
338 newargc = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000339
Marc Bouchere6869a82000-03-20 06:03:29 +0000340 if (buffer[0] == '[') {
Harald Welte9f7fa492001-03-15 15:12:02 +0000341 /* we have counters in our input */
Marc Bouchere6869a82000-03-20 06:03:29 +0000342 ptr = strchr(buffer, ']');
343 if (!ptr)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100344 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000345 "Bad line %u: need ]\n",
346 line);
Harald Welte9f7fa492001-03-15 15:12:02 +0000347
Harald Welteccd49e52001-01-23 22:54:34 +0000348 pcnt = strtok(buffer+1, ":");
Harald Welte9f7fa492001-03-15 15:12:02 +0000349 if (!pcnt)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100350 xtables_error(PARAMETER_PROBLEM,
Harald Welte9f7fa492001-03-15 15:12:02 +0000351 "Bad line %u: need :\n",
352 line);
353
Harald Welteccd49e52001-01-23 22:54:34 +0000354 bcnt = strtok(NULL, "]");
Harald Welte9f7fa492001-03-15 15:12:02 +0000355 if (!bcnt)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100356 xtables_error(PARAMETER_PROBLEM,
Harald Welte9f7fa492001-03-15 15:12:02 +0000357 "Bad line %u: need ]\n",
358 line);
359
360 /* start command parsing after counter */
361 parsestart = ptr + 1;
362 } else {
363 /* start command parsing at start of line */
364 parsestart = buffer;
Marc Bouchere6869a82000-03-20 06:03:29 +0000365 }
Rusty Russell7e53bf92000-03-20 07:03:28 +0000366
Harald Welte9f7fa492001-03-15 15:12:02 +0000367 add_argv(argv[0]);
368 add_argv("-t");
Jan Engelhardt00696592011-01-31 02:39:46 +0100369 add_argv(curtable);
Max Kellermann5b76f682008-01-29 13:42:48 +0000370
Harald Welteccd49e52001-01-23 22:54:34 +0000371 if (counters && pcnt && bcnt) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000372 add_argv("--set-counters");
373 add_argv((char *) pcnt);
374 add_argv((char *) bcnt);
Harald Welteccd49e52001-01-23 22:54:34 +0000375 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000376
Harald Welte26e643f2001-05-03 20:50:03 +0000377 /* After fighting with strtok enough, here's now
378 * a 'real' parser. According to Rusty I'm now no
379 * longer a real hacker, but I can live with that */
380
381 quote_open = 0;
Max Kellermann3bf54df2008-01-29 13:45:29 +0000382 escaped = 0;
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000383 param_len = 0;
Max Kellermann5b76f682008-01-29 13:42:48 +0000384
Harald Welte26e643f2001-05-03 20:50:03 +0000385 for (curchar = parsestart; *curchar; curchar++) {
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000386 char param_buffer[1024];
387
Max Kellermann3bf54df2008-01-29 13:45:29 +0000388 if (quote_open) {
Max Kellermannfe05c762008-01-29 13:46:01 +0000389 if (escaped) {
390 param_buffer[param_len++] = *curchar;
391 escaped = 0;
392 continue;
393 } else if (*curchar == '\\') {
Max Kellermann3bf54df2008-01-29 13:45:29 +0000394 escaped = 1;
395 continue;
396 } else if (*curchar == '"') {
Harald Welte26e643f2001-05-03 20:50:03 +0000397 quote_open = 0;
398 *curchar = ' ';
Max Kellermann3bf54df2008-01-29 13:45:29 +0000399 } else {
400 param_buffer[param_len++] = *curchar;
401 continue;
402 }
403 } else {
404 if (*curchar == '"') {
Harald Welte26e643f2001-05-03 20:50:03 +0000405 quote_open = 1;
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000406 continue;
Harald Welte26e643f2001-05-03 20:50:03 +0000407 }
Max Kellermann3bf54df2008-01-29 13:45:29 +0000408 }
409
Harald Welte26e643f2001-05-03 20:50:03 +0000410 if (*curchar == ' '
411 || *curchar == '\t'
412 || * curchar == '\n') {
Harald Welte974d0102001-05-26 04:41:56 +0000413 if (!param_len) {
414 /* two spaces? */
Harald Welte974d0102001-05-26 04:41:56 +0000415 continue;
416 }
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000417
418 param_buffer[param_len] = '\0';
Harald Weltebb41f882001-10-21 14:11:54 +0000419
420 /* check if table name specified */
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +0200421 if (!strncmp(param_buffer, "-t", 2)
Max Kellermann5b76f682008-01-29 13:42:48 +0000422 || !strncmp(param_buffer, "--table", 8)) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100423 xtables_error(PARAMETER_PROBLEM,
Harald Weltebb41f882001-10-21 14:11:54 +0000424 "Line %u seems to have a "
425 "-t table option.\n", line);
426 exit(1);
427 }
428
Harald Welte26e643f2001-05-03 20:50:03 +0000429 add_argv(param_buffer);
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000430 param_len = 0;
Harald Welte26e643f2001-05-03 20:50:03 +0000431 } else {
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000432 /* regular character, copy to buffer */
433 param_buffer[param_len++] = *curchar;
434
435 if (param_len >= sizeof(param_buffer))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100436 xtables_error(PARAMETER_PROBLEM,
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000437 "Parameter too long!");
Harald Welte26e643f2001-05-03 20:50:03 +0000438 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000439 }
440
Harald Welte26e643f2001-05-03 20:50:03 +0000441 DEBUGP("calling do_command(%u, argv, &%s, handle):\n",
442 newargc, curtable);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000443
Harald Weltefa48fc82001-10-16 09:51:33 +0000444 for (a = 0; a < newargc; a++)
Harald Welteae1ff9f2000-12-01 14:26:20 +0000445 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
446
Max Kellermann5b76f682008-01-29 13:42:48 +0000447 ret = do_command(newargc, newargv,
Harald Welte26e643f2001-05-03 20:50:03 +0000448 &newargv[2], &handle);
449
450 free_argv();
Henrik Nordstromd2137602008-05-12 20:51:45 +0200451 fflush(stdout);
Marc Bouchere6869a82000-03-20 06:03:29 +0000452 }
Peter Warasinb9cde3a2007-11-05 19:35:31 +0000453 if (tablename && (strcmp(tablename, curtable) != 0))
454 continue;
Marc Bouchere6869a82000-03-20 06:03:29 +0000455 if (!ret) {
456 fprintf(stderr, "%s: line %u failed\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500457 prog_name, line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000458 exit(1);
459 }
460 }
Martin Josefsson3229a7c2004-02-01 21:46:04 +0000461 if (in_table) {
462 fprintf(stderr, "%s: COMMIT expected at line %u\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500463 prog_name, line + 1);
Martin Josefsson3229a7c2004-02-01 21:46:04 +0000464 exit(1);
465 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000466
Jan Engelhardtf1afcc82009-06-10 13:52:58 +0200467 if (in != NULL)
468 fclose(in);
Marc Bouchere6869a82000-03-20 06:03:29 +0000469 return 0;
470}