blob: e2d1859ed7e0cfd8e2aabb3bd2f3cddabc03e753 [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 Welteccd49e52001-01-23 22:54:34 +00006 *
Martin Josefsson357d59d2004-12-27 19:49:28 +00007 * $Id$
Harald Welteae1ff9f2000-12-01 14:26:20 +00008 */
9
Marc Bouchere6869a82000-03-20 06:03:29 +000010#include <getopt.h>
11#include <sys/errno.h>
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010012#include <stdbool.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000013#include <string.h>
14#include <stdio.h>
Harald Welteae1ff9f2000-12-01 14:26:20 +000015#include <stdlib.h>
16#include "iptables.h"
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000017#include "xtables.h"
Harald Welteae1ff9f2000-12-01 14:26:20 +000018#include "libiptc/libiptc.h"
Jan Engelhardt33690a12008-02-11 00:54:00 +010019#include "iptables-multi.h"
Harald Welteae1ff9f2000-12-01 14:26:20 +000020
21#ifdef DEBUG
Harald Weltec45c3152000-12-06 08:11:24 +000022#define DEBUGP(x, args...) fprintf(stderr, x, ## args)
Harald Welteae1ff9f2000-12-01 14:26:20 +000023#else
Max Kellermann5b76f682008-01-29 13:42:48 +000024#define DEBUGP(x, args...)
Harald Welteae1ff9f2000-12-01 14:26:20 +000025#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000026
Harald Weltea9d27082000-12-19 16:10:42 +000027static int binary = 0, counters = 0, verbose = 0, noflush = 0;
28
Marc Bouchere6869a82000-03-20 06:03:29 +000029/* Keeping track of external matches and targets. */
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +010030static const struct option options[] = {
31 {.name = "binary", .has_arg = false, .val = 'b'},
32 {.name = "counters", .has_arg = false, .val = 'c'},
33 {.name = "verbose", .has_arg = false, .val = 'v'},
34 {.name = "test", .has_arg = false, .val = 't'},
35 {.name = "help", .has_arg = false, .val = 'h'},
36 {.name = "noflush", .has_arg = false, .val = 'n'},
37 {.name = "modprobe", .has_arg = true, .val = 'M'},
38 {.name = "table", .has_arg = true, .val = 'T'},
39 {NULL},
Marc Bouchere6869a82000-03-20 06:03:29 +000040};
41
42static void print_usage(const char *name, const char *version) __attribute__((noreturn));
43
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -050044#define prog_name iptables_globals.program_name
45
Marc Bouchere6869a82000-03-20 06:03:29 +000046static void print_usage(const char *name, const char *version)
47{
Martin Josefssonbb2f68a2004-02-01 22:03:27 +000048 fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-t] [-h]\n"
Harald Weltea9d27082000-12-19 16:10:42 +000049 " [ --binary ]\n"
50 " [ --counters ]\n"
51 " [ --verbose ]\n"
Martin Josefssonbb2f68a2004-02-01 22:03:27 +000052 " [ --test ]\n"
Harald Weltea9d27082000-12-19 16:10:42 +000053 " [ --help ]\n"
Harald Welte58918652001-06-16 18:25:25 +000054 " [ --noflush ]\n"
Peter Warasinb9cde3a2007-11-05 19:35:31 +000055 " [ --table=<TABLE> ]\n"
Max Kellermann5b76f682008-01-29 13:42:48 +000056 " [ --modprobe=<command>]\n", name);
57
Marc Bouchere6869a82000-03-20 06:03:29 +000058 exit(1);
59}
60
Jan Engelhardtc3541052008-11-18 12:26:26 +010061static struct iptc_handle *create_handle(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +000062{
Jan Engelhardtfd187312008-11-10 16:59:27 +010063 struct iptc_handle *handle;
Marc Bouchere6869a82000-03-20 06:03:29 +000064
Harald Welteae1ff9f2000-12-01 14:26:20 +000065 handle = iptc_init(tablename);
Harald Welte58918652001-06-16 18:25:25 +000066
67 if (!handle) {
68 /* try to insmod the module if iptc_init failed */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010069 xtables_load_ko(xtables_modprobe_program, false);
Harald Welte58918652001-06-16 18:25:25 +000070 handle = iptc_init(tablename);
71 }
72
Harald Welteae1ff9f2000-12-01 14:26:20 +000073 if (!handle) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +010074 xtables_error(PARAMETER_PROBLEM, "%s: unable to initialize "
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -050075 "table '%s'\n", prog_name, tablename);
Harald Welteae1ff9f2000-12-01 14:26:20 +000076 exit(1);
Marc Bouchere6869a82000-03-20 06:03:29 +000077 }
Harald Welteae1ff9f2000-12-01 14:26:20 +000078 return handle;
Marc Bouchere6869a82000-03-20 06:03:29 +000079}
80
Lutz Jaenickee78c69c2006-12-09 13:06:04 +000081static int parse_counters(char *string, struct ipt_counters *ctr)
Harald Welted8e65632001-01-05 15:20:07 +000082{
Patrick McHardy875441e2007-10-17 08:48:58 +000083 unsigned long long pcnt, bcnt;
84 int ret;
Patrick McHardyb73691f2007-09-05 14:10:53 +000085
Jan Engelhardtbb8be302011-01-31 02:41:23 +010086 ret = sscanf(string, "[%llu:%llu]", &pcnt, &bcnt);
Patrick McHardy875441e2007-10-17 08:48:58 +000087 ctr->pcnt = pcnt;
88 ctr->bcnt = bcnt;
89 return ret == 2;
Harald Welted8e65632001-01-05 15:20:07 +000090}
Harald Welteae1ff9f2000-12-01 14:26:20 +000091
Harald Welte9f7fa492001-03-15 15:12:02 +000092/* global new argv and argc */
Harald Welte26e643f2001-05-03 20:50:03 +000093static char *newargv[255];
Harald Welte9f7fa492001-03-15 15:12:02 +000094static int newargc;
95
96/* function adding one argument to newargv, updating newargc
97 * returns true if argument added, false otherwise */
98static int add_argv(char *what) {
Harald Weltebb41f882001-10-21 14:11:54 +000099 DEBUGP("add_argv: %s\n", what);
Jan Engelhardt2c69b552009-04-30 19:32:02 +0200100 if (what && newargc + 1 < ARRAY_SIZE(newargv)) {
Harald Welte26e643f2001-05-03 20:50:03 +0000101 newargv[newargc] = strdup(what);
Harald Welte9f7fa492001-03-15 15:12:02 +0000102 newargc++;
103 return 1;
Jan Engelhardt6a0448e2011-01-31 02:34:49 +0100104 } else {
105 xtables_error(PARAMETER_PROBLEM,
106 "Parser cannot handle more arguments\n");
Harald Welte9f7fa492001-03-15 15:12:02 +0000107 return 0;
Jan Engelhardt6a0448e2011-01-31 02:34:49 +0100108 }
Harald Welte9f7fa492001-03-15 15:12:02 +0000109}
110
Harald Welte26e643f2001-05-03 20:50:03 +0000111static void free_argv(void) {
112 int i;
113
114 for (i = 0; i < newargc; i++)
115 free(newargv[i]);
116}
117
Bastiaan Bakker4e3771f2004-06-25 11:18:57 +0000118#ifdef IPTABLES_MULTI
119int
120iptables_restore_main(int argc, char *argv[])
121#else
122int
123main(int argc, char *argv[])
124#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000125{
Jan Engelhardtfd187312008-11-10 16:59:27 +0100126 struct iptc_handle *handle = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000127 char buffer[10240];
Harald Weltea9d27082000-12-19 16:10:42 +0000128 int c;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000129 char curtable[IPT_TABLE_MAXNAMELEN + 1];
Marc Bouchere6869a82000-03-20 06:03:29 +0000130 FILE *in;
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000131 int in_table = 0, testing = 0;
Patrick McHardy0ea82bc2008-06-07 15:15:29 +0200132 const char *tablename = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000133
Illes Marci63e90632003-03-03 08:08:37 +0000134 line = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000135
Jamal Hadi Salim617d3d12009-02-11 16:28:31 -0500136 iptables_globals.program_name = "iptables-restore";
Jamal Hadi Salim7e4db2f2009-02-13 09:14:17 -0500137 c = xtables_init_all(&iptables_globals, NFPROTO_IPV4);
138 if (c < 0) {
139 fprintf(stderr, "%s/%s Failed to initialize xtables\n",
140 iptables_globals.program_name,
141 iptables_globals.program_version);
142 exit(1);
143 }
Jan Engelhardtb79ec692009-07-23 17:41:21 +0200144#if defined(ALL_INCLUSIVE) || defined(NO_SHARED_LIBS)
Harald Welte3efb6ea2001-08-06 18:50:21 +0000145 init_extensions();
146#endif
147
Peter Warasinb9cde3a2007-11-05 19:35:31 +0000148 while ((c = getopt_long(argc, argv, "bcvthnM:T:", options, NULL)) != -1) {
Harald Weltea9d27082000-12-19 16:10:42 +0000149 switch (c) {
150 case 'b':
151 binary = 1;
152 break;
153 case 'c':
154 counters = 1;
155 break;
Marc Boucher1277b592001-12-06 15:06:34 +0000156 case 'v':
157 verbose = 1;
158 break;
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000159 case 't':
160 testing = 1;
161 break;
Harald Weltea9d27082000-12-19 16:10:42 +0000162 case 'h':
163 print_usage("iptables-restore",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100164 IPTABLES_VERSION);
Harald Weltea9d27082000-12-19 16:10:42 +0000165 break;
166 case 'n':
167 noflush = 1;
168 break;
Harald Welte58918652001-06-16 18:25:25 +0000169 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100170 xtables_modprobe_program = optarg;
Harald Welte58918652001-06-16 18:25:25 +0000171 break;
Peter Warasinb9cde3a2007-11-05 19:35:31 +0000172 case 'T':
173 tablename = optarg;
174 break;
Harald Weltea9d27082000-12-19 16:10:42 +0000175 }
176 }
Max Kellermann5b76f682008-01-29 13:42:48 +0000177
Marc Bouchere6869a82000-03-20 06:03:29 +0000178 if (optind == argc - 1) {
179 in = fopen(argv[optind], "r");
180 if (!in) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000181 fprintf(stderr, "Can't open %s: %s\n", argv[optind],
Marc Bouchere6869a82000-03-20 06:03:29 +0000182 strerror(errno));
183 exit(1);
184 }
185 }
186 else if (optind < argc) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000187 fprintf(stderr, "Unknown arguments found on commandline\n");
Marc Bouchere6869a82000-03-20 06:03:29 +0000188 exit(1);
189 }
190 else in = stdin;
Max Kellermann5b76f682008-01-29 13:42:48 +0000191
Marc Bouchere6869a82000-03-20 06:03:29 +0000192 /* Grab standard input. */
193 while (fgets(buffer, sizeof(buffer), in)) {
Harald Welteb3f22192003-03-06 11:56:31 +0000194 int ret = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000195
196 line++;
Martin Josefsson5065afa2004-01-31 19:33:47 +0000197 if (buffer[0] == '\n')
198 continue;
Marc Bouchere6869a82000-03-20 06:03:29 +0000199 else if (buffer[0] == '#') {
Martin Josefsson3673dcb2004-01-31 19:41:49 +0000200 if (verbose)
201 fputs(buffer, stdout);
Marc Bouchere6869a82000-03-20 06:03:29 +0000202 continue;
Illes Marci26100fa2003-03-03 08:05:07 +0000203 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000204 if (!testing) {
205 DEBUGP("Calling commit\n");
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100206 ret = iptc_commit(handle);
207 iptc_free(handle);
208 handle = NULL;
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000209 } else {
210 DEBUGP("Not calling commit, testing\n");
211 ret = 1;
212 }
Illes Marci26100fa2003-03-03 08:05:07 +0000213 in_table = 0;
Harald Welteb3f22192003-03-06 11:56:31 +0000214 } else if ((buffer[0] == '*') && (!in_table)) {
Harald Welteae1ff9f2000-12-01 14:26:20 +0000215 /* New table */
216 char *table;
217
218 table = strtok(buffer+1, " \t\n");
219 DEBUGP("line %u, table '%s'\n", line, table);
220 if (!table) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100221 xtables_error(PARAMETER_PROBLEM,
Harald Welteae1ff9f2000-12-01 14:26:20 +0000222 "%s: line %u table name invalid\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500223 prog_name, line);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000224 exit(1);
225 }
226 strncpy(curtable, table, IPT_TABLE_MAXNAMELEN);
Karsten Desler073df8f2004-01-31 15:33:55 +0000227 curtable[IPT_TABLE_MAXNAMELEN] = '\0';
Harald Welteae1ff9f2000-12-01 14:26:20 +0000228
Peter Warasinb9cde3a2007-11-05 19:35:31 +0000229 if (tablename && (strcmp(tablename, table) != 0))
230 continue;
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000231 if (handle)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100232 iptc_free(handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000233
Jan Engelhardtc3541052008-11-18 12:26:26 +0100234 handle = create_handle(table);
Harald Weltea9d27082000-12-19 16:10:42 +0000235 if (noflush == 0) {
236 DEBUGP("Cleaning all chains of table '%s'\n",
237 table);
Max Kellermann5b76f682008-01-29 13:42:48 +0000238 for_each_chain(flush_entries, verbose, 1,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100239 handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000240
Harald Weltea9d27082000-12-19 16:10:42 +0000241 DEBUGP("Deleting all user-defined chains "
242 "of table '%s'\n", table);
Max Kellermann5b76f682008-01-29 13:42:48 +0000243 for_each_chain(delete_chain, verbose, 0,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100244 handle);
Harald Weltea9d27082000-12-19 16:10:42 +0000245 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000246
247 ret = 1;
Illes Marci26100fa2003-03-03 08:05:07 +0000248 in_table = 1;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000249
Illes Marci26100fa2003-03-03 08:05:07 +0000250 } else if ((buffer[0] == ':') && (in_table)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000251 /* New chain. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000252 char *policy, *chain;
Marc Bouchere6869a82000-03-20 06:03:29 +0000253
Marc Bouchere6869a82000-03-20 06:03:29 +0000254 chain = strtok(buffer+1, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000255 DEBUGP("line %u, chain '%s'\n", line, chain);
Marc Bouchere6869a82000-03-20 06:03:29 +0000256 if (!chain) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100257 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000258 "%s: line %u chain name invalid\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500259 prog_name, line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000260 exit(1);
261 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000262
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200263 if (strlen(chain) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt21d12832010-03-16 16:49:21 +0100264 xtables_error(PARAMETER_PROBLEM,
265 "Invalid chain name `%s' "
266 "(%u chars max)",
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200267 chain, XT_EXTENSION_MAXNAMELEN - 1);
Jan Engelhardt21d12832010-03-16 16:49:21 +0100268
Harald Welte3a506ac2004-08-30 16:00:09 +0000269 if (iptc_builtin(chain, handle) <= 0) {
Charlie Brady595e4932005-06-12 15:54:15 +0000270 if (noflush && iptc_is_chain(chain, handle)) {
271 DEBUGP("Flushing existing user defined chain '%s'\n", chain);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100272 if (!iptc_flush_entries(chain, handle))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100273 xtables_error(PARAMETER_PROBLEM,
Charlie Brady595e4932005-06-12 15:54:15 +0000274 "error flushing chain "
275 "'%s':%s\n", chain,
276 strerror(errno));
277 } else {
278 DEBUGP("Creating new chain '%s'\n", chain);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100279 if (!iptc_create_chain(chain, handle))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100280 xtables_error(PARAMETER_PROBLEM,
Charlie Brady595e4932005-06-12 15:54:15 +0000281 "error creating chain "
282 "'%s':%s\n", chain,
283 strerror(errno));
284 }
Harald Welte9f7fa492001-03-15 15:12:02 +0000285 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000286
Marc Bouchere6869a82000-03-20 06:03:29 +0000287 policy = strtok(NULL, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000288 DEBUGP("line %u, policy '%s'\n", line, policy);
Marc Bouchere6869a82000-03-20 06:03:29 +0000289 if (!policy) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100290 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000291 "%s: line %u policy invalid\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500292 prog_name, line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000293 exit(1);
294 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000295
296 if (strcmp(policy, "-") != 0) {
Harald Welted8e65632001-01-05 15:20:07 +0000297 struct ipt_counters count;
298
299 if (counters) {
300 char *ctrs;
301 ctrs = strtok(NULL, " \t\n");
302
Harald Welte6f38a302006-02-09 14:35:38 +0000303 if (!ctrs || !parse_counters(ctrs, &count))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100304 xtables_error(PARAMETER_PROBLEM,
Harald Welte6f38a302006-02-09 14:35:38 +0000305 "invalid policy counters "
306 "for chain '%s'\n", chain);
Harald Welted8e65632001-01-05 15:20:07 +0000307
308 } else {
Max Kellermann5b76f682008-01-29 13:42:48 +0000309 memset(&count, 0,
Harald Welted8e65632001-01-05 15:20:07 +0000310 sizeof(struct ipt_counters));
311 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000312
313 DEBUGP("Setting policy of chain %s to %s\n",
314 chain, policy);
315
Harald Welted8e65632001-01-05 15:20:07 +0000316 if (!iptc_set_policy(chain, policy, &count,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100317 handle))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100318 xtables_error(OTHER_PROBLEM,
Harald Welteae1ff9f2000-12-01 14:26:20 +0000319 "Can't set policy `%s'"
320 " on `%s' line %u: %s\n",
Rob Leslief6d64492010-09-28 00:43:00 -0700321 policy, chain, line,
Harald Welteae1ff9f2000-12-01 14:26:20 +0000322 iptc_strerror(errno));
323 }
324
325 ret = 1;
326
Illes Marci26100fa2003-03-03 08:05:07 +0000327 } else if (in_table) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000328 int a;
Marc Bouchere6869a82000-03-20 06:03:29 +0000329 char *ptr = buffer;
Harald Welteccd49e52001-01-23 22:54:34 +0000330 char *pcnt = NULL;
331 char *bcnt = NULL;
Harald Welte9f7fa492001-03-15 15:12:02 +0000332 char *parsestart;
333
Harald Welte26e643f2001-05-03 20:50:03 +0000334 /* the parser */
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000335 char *curchar;
Max Kellermann3bf54df2008-01-29 13:45:29 +0000336 int quote_open, escaped;
Max Kellermannb4ef34f2008-01-29 13:43:35 +0000337 size_t param_len;
Harald Welte26e643f2001-05-03 20:50:03 +0000338
Harald Welte9f7fa492001-03-15 15:12:02 +0000339 /* reset the newargv */
340 newargc = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000341
Marc Bouchere6869a82000-03-20 06:03:29 +0000342 if (buffer[0] == '[') {
Harald Welte9f7fa492001-03-15 15:12:02 +0000343 /* we have counters in our input */
Marc Bouchere6869a82000-03-20 06:03:29 +0000344 ptr = strchr(buffer, ']');
345 if (!ptr)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100346 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000347 "Bad line %u: need ]\n",
348 line);
Harald Welte9f7fa492001-03-15 15:12:02 +0000349
Harald Welteccd49e52001-01-23 22:54:34 +0000350 pcnt = strtok(buffer+1, ":");
Harald Welte9f7fa492001-03-15 15:12:02 +0000351 if (!pcnt)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100352 xtables_error(PARAMETER_PROBLEM,
Harald Welte9f7fa492001-03-15 15:12:02 +0000353 "Bad line %u: need :\n",
354 line);
355
Harald Welteccd49e52001-01-23 22:54:34 +0000356 bcnt = strtok(NULL, "]");
Harald Welte9f7fa492001-03-15 15:12:02 +0000357 if (!bcnt)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100358 xtables_error(PARAMETER_PROBLEM,
Harald Welte9f7fa492001-03-15 15:12:02 +0000359 "Bad line %u: need ]\n",
360 line);
361
362 /* start command parsing after counter */
363 parsestart = ptr + 1;
364 } else {
365 /* start command parsing at start of line */
366 parsestart = buffer;
Marc Bouchere6869a82000-03-20 06:03:29 +0000367 }
Rusty Russell7e53bf92000-03-20 07:03:28 +0000368
Harald Welte9f7fa492001-03-15 15:12:02 +0000369 add_argv(argv[0]);
370 add_argv("-t");
Jan Engelhardt00696592011-01-31 02:39:46 +0100371 add_argv(curtable);
Max Kellermann5b76f682008-01-29 13:42:48 +0000372
Harald Welteccd49e52001-01-23 22:54:34 +0000373 if (counters && pcnt && bcnt) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000374 add_argv("--set-counters");
375 add_argv((char *) pcnt);
376 add_argv((char *) bcnt);
Harald Welteccd49e52001-01-23 22:54:34 +0000377 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000378
Harald Welte26e643f2001-05-03 20:50:03 +0000379 /* After fighting with strtok enough, here's now
380 * a 'real' parser. According to Rusty I'm now no
381 * longer a real hacker, but I can live with that */
382
383 quote_open = 0;
Max Kellermann3bf54df2008-01-29 13:45:29 +0000384 escaped = 0;
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000385 param_len = 0;
Max Kellermann5b76f682008-01-29 13:42:48 +0000386
Harald Welte26e643f2001-05-03 20:50:03 +0000387 for (curchar = parsestart; *curchar; curchar++) {
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000388 char param_buffer[1024];
389
Max Kellermann3bf54df2008-01-29 13:45:29 +0000390 if (quote_open) {
Max Kellermannfe05c762008-01-29 13:46:01 +0000391 if (escaped) {
392 param_buffer[param_len++] = *curchar;
393 escaped = 0;
394 continue;
395 } else if (*curchar == '\\') {
Max Kellermann3bf54df2008-01-29 13:45:29 +0000396 escaped = 1;
397 continue;
398 } else if (*curchar == '"') {
Harald Welte26e643f2001-05-03 20:50:03 +0000399 quote_open = 0;
400 *curchar = ' ';
Max Kellermann3bf54df2008-01-29 13:45:29 +0000401 } else {
402 param_buffer[param_len++] = *curchar;
403 continue;
404 }
405 } else {
406 if (*curchar == '"') {
Harald Welte26e643f2001-05-03 20:50:03 +0000407 quote_open = 1;
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000408 continue;
Harald Welte26e643f2001-05-03 20:50:03 +0000409 }
Max Kellermann3bf54df2008-01-29 13:45:29 +0000410 }
411
Harald Welte26e643f2001-05-03 20:50:03 +0000412 if (*curchar == ' '
413 || *curchar == '\t'
414 || * curchar == '\n') {
Harald Welte974d0102001-05-26 04:41:56 +0000415 if (!param_len) {
416 /* two spaces? */
Harald Welte974d0102001-05-26 04:41:56 +0000417 continue;
418 }
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000419
420 param_buffer[param_len] = '\0';
Harald Weltebb41f882001-10-21 14:11:54 +0000421
422 /* check if table name specified */
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +0200423 if (!strncmp(param_buffer, "-t", 2)
Max Kellermann5b76f682008-01-29 13:42:48 +0000424 || !strncmp(param_buffer, "--table", 8)) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100425 xtables_error(PARAMETER_PROBLEM,
Harald Weltebb41f882001-10-21 14:11:54 +0000426 "Line %u seems to have a "
427 "-t table option.\n", line);
428 exit(1);
429 }
430
Harald Welte26e643f2001-05-03 20:50:03 +0000431 add_argv(param_buffer);
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000432 param_len = 0;
Harald Welte26e643f2001-05-03 20:50:03 +0000433 } else {
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000434 /* regular character, copy to buffer */
435 param_buffer[param_len++] = *curchar;
436
437 if (param_len >= sizeof(param_buffer))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100438 xtables_error(PARAMETER_PROBLEM,
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000439 "Parameter too long!");
Harald Welte26e643f2001-05-03 20:50:03 +0000440 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000441 }
442
Harald Welte26e643f2001-05-03 20:50:03 +0000443 DEBUGP("calling do_command(%u, argv, &%s, handle):\n",
444 newargc, curtable);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000445
Harald Weltefa48fc82001-10-16 09:51:33 +0000446 for (a = 0; a < newargc; a++)
Harald Welteae1ff9f2000-12-01 14:26:20 +0000447 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
448
Max Kellermann5b76f682008-01-29 13:42:48 +0000449 ret = do_command(newargc, newargv,
Harald Welte26e643f2001-05-03 20:50:03 +0000450 &newargv[2], &handle);
451
452 free_argv();
Henrik Nordstromd2137602008-05-12 20:51:45 +0200453 fflush(stdout);
Marc Bouchere6869a82000-03-20 06:03:29 +0000454 }
Peter Warasinb9cde3a2007-11-05 19:35:31 +0000455 if (tablename && (strcmp(tablename, curtable) != 0))
456 continue;
Marc Bouchere6869a82000-03-20 06:03:29 +0000457 if (!ret) {
458 fprintf(stderr, "%s: line %u failed\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500459 prog_name, line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000460 exit(1);
461 }
462 }
Martin Josefsson3229a7c2004-02-01 21:46:04 +0000463 if (in_table) {
464 fprintf(stderr, "%s: COMMIT expected at line %u\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500465 prog_name, line + 1);
Martin Josefsson3229a7c2004-02-01 21:46:04 +0000466 exit(1);
467 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000468
Jan Engelhardtf1afcc82009-06-10 13:52:58 +0200469 if (in != NULL)
470 fclose(in);
Marc Bouchere6869a82000-03-20 06:03:29 +0000471 return 0;
472}