blob: 3fbc90870f440422034a4143da0d747ff850cf2c [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
44static 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) {
Patrick McHardy5b098ac2007-02-14 13:59:12 +000072 exit_error(PARAMETER_PROBLEM, "%s: unable to initialize "
Harald Welteae1ff9f2000-12-01 14:26:20 +000073 "table '%s'\n", program_name, tablename);
74 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
Patrick McHardy875441e2007-10-17 08:48:58 +000084 ret = sscanf(string, "[%llu:%llu]",
85 (unsigned long long *)&pcnt,
86 (unsigned long long *)&bcnt);
87 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);
Harald Welte9f7fa492001-03-15 15:12:02 +0000100 if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) {
Harald Welte26e643f2001-05-03 20:50:03 +0000101 newargv[newargc] = strdup(what);
Harald Welte9f7fa492001-03-15 15:12:02 +0000102 newargc++;
103 return 1;
104 } else
105 return 0;
106}
107
Harald Welte26e643f2001-05-03 20:50:03 +0000108static void free_argv(void) {
109 int i;
110
111 for (i = 0; i < newargc; i++)
112 free(newargv[i]);
113}
114
Bastiaan Bakker4e3771f2004-06-25 11:18:57 +0000115#ifdef IPTABLES_MULTI
116int
117iptables_restore_main(int argc, char *argv[])
118#else
119int
120main(int argc, char *argv[])
121#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000122{
Jan Engelhardtfd187312008-11-10 16:59:27 +0100123 struct iptc_handle *handle = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000124 char buffer[10240];
Harald Weltea9d27082000-12-19 16:10:42 +0000125 int c;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000126 char curtable[IPT_TABLE_MAXNAMELEN + 1];
Marc Bouchere6869a82000-03-20 06:03:29 +0000127 FILE *in;
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000128 int in_table = 0, testing = 0;
Patrick McHardy0ea82bc2008-06-07 15:15:29 +0200129 const char *tablename = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000130
131 program_name = "iptables-restore";
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200132 program_version = XTABLES_VERSION;
Illes Marci63e90632003-03-03 08:08:37 +0000133 line = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000134
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100135 lib_dir = getenv("XTABLES_LIBDIR");
136 if (lib_dir == NULL) {
137 lib_dir = getenv("IPTABLES_LIB_DIR");
138 if (lib_dir != NULL)
Jan Engelhardt87830712009-01-07 14:43:47 +0100139 fprintf(stderr, "IPTABLES_LIB_DIR is deprecated, "
140 "use XTABLES_LIBDIR.\n");
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100141 }
142 if (lib_dir == NULL)
143 lib_dir = XTABLES_LIBDIR;
Martin Josefsson357d59d2004-12-27 19:49:28 +0000144
Harald Welte3efb6ea2001-08-06 18:50:21 +0000145#ifdef NO_SHARED_LIBS
146 init_extensions();
147#endif
148
Peter Warasinb9cde3a2007-11-05 19:35:31 +0000149 while ((c = getopt_long(argc, argv, "bcvthnM:T:", options, NULL)) != -1) {
Harald Weltea9d27082000-12-19 16:10:42 +0000150 switch (c) {
151 case 'b':
152 binary = 1;
153 break;
154 case 'c':
155 counters = 1;
156 break;
Marc Boucher1277b592001-12-06 15:06:34 +0000157 case 'v':
158 verbose = 1;
159 break;
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000160 case 't':
161 testing = 1;
162 break;
Harald Weltea9d27082000-12-19 16:10:42 +0000163 case 'h':
164 print_usage("iptables-restore",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200165 XTABLES_VERSION);
Harald Weltea9d27082000-12-19 16:10:42 +0000166 break;
167 case 'n':
168 noflush = 1;
169 break;
Harald Welte58918652001-06-16 18:25:25 +0000170 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100171 xtables_modprobe_program = optarg;
Harald Welte58918652001-06-16 18:25:25 +0000172 break;
Peter Warasinb9cde3a2007-11-05 19:35:31 +0000173 case 'T':
174 tablename = optarg;
175 break;
Harald Weltea9d27082000-12-19 16:10:42 +0000176 }
177 }
Max Kellermann5b76f682008-01-29 13:42:48 +0000178
Marc Bouchere6869a82000-03-20 06:03:29 +0000179 if (optind == argc - 1) {
180 in = fopen(argv[optind], "r");
181 if (!in) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000182 fprintf(stderr, "Can't open %s: %s\n", argv[optind],
Marc Bouchere6869a82000-03-20 06:03:29 +0000183 strerror(errno));
184 exit(1);
185 }
186 }
187 else if (optind < argc) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000188 fprintf(stderr, "Unknown arguments found on commandline\n");
Marc Bouchere6869a82000-03-20 06:03:29 +0000189 exit(1);
190 }
191 else in = stdin;
Max Kellermann5b76f682008-01-29 13:42:48 +0000192
Marc Bouchere6869a82000-03-20 06:03:29 +0000193 /* Grab standard input. */
194 while (fgets(buffer, sizeof(buffer), in)) {
Harald Welteb3f22192003-03-06 11:56:31 +0000195 int ret = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000196
197 line++;
Martin Josefsson5065afa2004-01-31 19:33:47 +0000198 if (buffer[0] == '\n')
199 continue;
Marc Bouchere6869a82000-03-20 06:03:29 +0000200 else if (buffer[0] == '#') {
Martin Josefsson3673dcb2004-01-31 19:41:49 +0000201 if (verbose)
202 fputs(buffer, stdout);
Marc Bouchere6869a82000-03-20 06:03:29 +0000203 continue;
Illes Marci26100fa2003-03-03 08:05:07 +0000204 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000205 if (!testing) {
206 DEBUGP("Calling commit\n");
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100207 ret = iptc_commit(handle);
208 iptc_free(handle);
209 handle = NULL;
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000210 } else {
211 DEBUGP("Not calling commit, testing\n");
212 ret = 1;
213 }
Illes Marci26100fa2003-03-03 08:05:07 +0000214 in_table = 0;
Harald Welteb3f22192003-03-06 11:56:31 +0000215 } else if ((buffer[0] == '*') && (!in_table)) {
Harald Welteae1ff9f2000-12-01 14:26:20 +0000216 /* New table */
217 char *table;
218
219 table = strtok(buffer+1, " \t\n");
220 DEBUGP("line %u, table '%s'\n", line, table);
221 if (!table) {
Max Kellermann5b76f682008-01-29 13:42:48 +0000222 exit_error(PARAMETER_PROBLEM,
Harald Welteae1ff9f2000-12-01 14:26:20 +0000223 "%s: line %u table name invalid\n",
224 program_name, line);
225 exit(1);
226 }
227 strncpy(curtable, table, IPT_TABLE_MAXNAMELEN);
Karsten Desler073df8f2004-01-31 15:33:55 +0000228 curtable[IPT_TABLE_MAXNAMELEN] = '\0';
Harald Welteae1ff9f2000-12-01 14:26:20 +0000229
Peter Warasinb9cde3a2007-11-05 19:35:31 +0000230 if (tablename && (strcmp(tablename, table) != 0))
231 continue;
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000232 if (handle)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100233 iptc_free(handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000234
Jan Engelhardtc3541052008-11-18 12:26:26 +0100235 handle = create_handle(table);
Harald Weltea9d27082000-12-19 16:10:42 +0000236 if (noflush == 0) {
237 DEBUGP("Cleaning all chains of table '%s'\n",
238 table);
Max Kellermann5b76f682008-01-29 13:42:48 +0000239 for_each_chain(flush_entries, verbose, 1,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100240 handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000241
Harald Weltea9d27082000-12-19 16:10:42 +0000242 DEBUGP("Deleting all user-defined chains "
243 "of table '%s'\n", table);
Max Kellermann5b76f682008-01-29 13:42:48 +0000244 for_each_chain(delete_chain, verbose, 0,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100245 handle);
Harald Weltea9d27082000-12-19 16:10:42 +0000246 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000247
248 ret = 1;
Illes Marci26100fa2003-03-03 08:05:07 +0000249 in_table = 1;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000250
Illes Marci26100fa2003-03-03 08:05:07 +0000251 } else if ((buffer[0] == ':') && (in_table)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000252 /* New chain. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000253 char *policy, *chain;
Marc Bouchere6869a82000-03-20 06:03:29 +0000254
Marc Bouchere6869a82000-03-20 06:03:29 +0000255 chain = strtok(buffer+1, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000256 DEBUGP("line %u, chain '%s'\n", line, chain);
Marc Bouchere6869a82000-03-20 06:03:29 +0000257 if (!chain) {
258 exit_error(PARAMETER_PROBLEM,
259 "%s: line %u chain name invalid\n",
260 program_name, line);
261 exit(1);
262 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000263
Harald Welte3a506ac2004-08-30 16:00:09 +0000264 if (iptc_builtin(chain, handle) <= 0) {
Charlie Brady595e4932005-06-12 15:54:15 +0000265 if (noflush && iptc_is_chain(chain, handle)) {
266 DEBUGP("Flushing existing user defined chain '%s'\n", chain);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100267 if (!iptc_flush_entries(chain, handle))
Charlie Brady595e4932005-06-12 15:54:15 +0000268 exit_error(PARAMETER_PROBLEM,
269 "error flushing chain "
270 "'%s':%s\n", chain,
271 strerror(errno));
272 } else {
273 DEBUGP("Creating new chain '%s'\n", chain);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100274 if (!iptc_create_chain(chain, handle))
Charlie Brady595e4932005-06-12 15:54:15 +0000275 exit_error(PARAMETER_PROBLEM,
276 "error creating chain "
277 "'%s':%s\n", chain,
278 strerror(errno));
279 }
Harald Welte9f7fa492001-03-15 15:12:02 +0000280 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000281
Marc Bouchere6869a82000-03-20 06:03:29 +0000282 policy = strtok(NULL, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000283 DEBUGP("line %u, policy '%s'\n", line, policy);
Marc Bouchere6869a82000-03-20 06:03:29 +0000284 if (!policy) {
285 exit_error(PARAMETER_PROBLEM,
286 "%s: line %u policy invalid\n",
287 program_name, line);
288 exit(1);
289 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000290
291 if (strcmp(policy, "-") != 0) {
Harald Welted8e65632001-01-05 15:20:07 +0000292 struct ipt_counters count;
293
294 if (counters) {
295 char *ctrs;
296 ctrs = strtok(NULL, " \t\n");
297
Harald Welte6f38a302006-02-09 14:35:38 +0000298 if (!ctrs || !parse_counters(ctrs, &count))
299 exit_error(PARAMETER_PROBLEM,
300 "invalid policy counters "
301 "for chain '%s'\n", chain);
Harald Welted8e65632001-01-05 15:20:07 +0000302
303 } else {
Max Kellermann5b76f682008-01-29 13:42:48 +0000304 memset(&count, 0,
Harald Welted8e65632001-01-05 15:20:07 +0000305 sizeof(struct ipt_counters));
306 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000307
308 DEBUGP("Setting policy of chain %s to %s\n",
309 chain, policy);
310
Harald Welted8e65632001-01-05 15:20:07 +0000311 if (!iptc_set_policy(chain, policy, &count,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100312 handle))
Harald Welteae1ff9f2000-12-01 14:26:20 +0000313 exit_error(OTHER_PROBLEM,
314 "Can't set policy `%s'"
315 " on `%s' line %u: %s\n",
316 chain, policy, line,
317 iptc_strerror(errno));
318 }
319
320 ret = 1;
321
Illes Marci26100fa2003-03-03 08:05:07 +0000322 } else if (in_table) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000323 int a;
Marc Bouchere6869a82000-03-20 06:03:29 +0000324 char *ptr = buffer;
Harald Welteccd49e52001-01-23 22:54:34 +0000325 char *pcnt = NULL;
326 char *bcnt = NULL;
Harald Welte9f7fa492001-03-15 15:12:02 +0000327 char *parsestart;
328
Harald Welte26e643f2001-05-03 20:50:03 +0000329 /* the parser */
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000330 char *curchar;
Max Kellermann3bf54df2008-01-29 13:45:29 +0000331 int quote_open, escaped;
Max Kellermannb4ef34f2008-01-29 13:43:35 +0000332 size_t param_len;
Harald Welte26e643f2001-05-03 20:50:03 +0000333
Harald Welte9f7fa492001-03-15 15:12:02 +0000334 /* reset the newargv */
335 newargc = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000336
Marc Bouchere6869a82000-03-20 06:03:29 +0000337 if (buffer[0] == '[') {
Harald Welte9f7fa492001-03-15 15:12:02 +0000338 /* we have counters in our input */
Marc Bouchere6869a82000-03-20 06:03:29 +0000339 ptr = strchr(buffer, ']');
340 if (!ptr)
341 exit_error(PARAMETER_PROBLEM,
342 "Bad line %u: need ]\n",
343 line);
Harald Welte9f7fa492001-03-15 15:12:02 +0000344
Harald Welteccd49e52001-01-23 22:54:34 +0000345 pcnt = strtok(buffer+1, ":");
Harald Welte9f7fa492001-03-15 15:12:02 +0000346 if (!pcnt)
347 exit_error(PARAMETER_PROBLEM,
348 "Bad line %u: need :\n",
349 line);
350
Harald Welteccd49e52001-01-23 22:54:34 +0000351 bcnt = strtok(NULL, "]");
Harald Welte9f7fa492001-03-15 15:12:02 +0000352 if (!bcnt)
353 exit_error(PARAMETER_PROBLEM,
354 "Bad line %u: need ]\n",
355 line);
356
357 /* start command parsing after counter */
358 parsestart = ptr + 1;
359 } else {
360 /* start command parsing at start of line */
361 parsestart = buffer;
Marc Bouchere6869a82000-03-20 06:03:29 +0000362 }
Rusty Russell7e53bf92000-03-20 07:03:28 +0000363
Harald Welte9f7fa492001-03-15 15:12:02 +0000364 add_argv(argv[0]);
365 add_argv("-t");
366 add_argv((char *) &curtable);
Max Kellermann5b76f682008-01-29 13:42:48 +0000367
Harald Welteccd49e52001-01-23 22:54:34 +0000368 if (counters && pcnt && bcnt) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000369 add_argv("--set-counters");
370 add_argv((char *) pcnt);
371 add_argv((char *) bcnt);
Harald Welteccd49e52001-01-23 22:54:34 +0000372 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000373
Harald Welte26e643f2001-05-03 20:50:03 +0000374 /* After fighting with strtok enough, here's now
375 * a 'real' parser. According to Rusty I'm now no
376 * longer a real hacker, but I can live with that */
377
378 quote_open = 0;
Max Kellermann3bf54df2008-01-29 13:45:29 +0000379 escaped = 0;
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000380 param_len = 0;
Max Kellermann5b76f682008-01-29 13:42:48 +0000381
Harald Welte26e643f2001-05-03 20:50:03 +0000382 for (curchar = parsestart; *curchar; curchar++) {
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000383 char param_buffer[1024];
384
Max Kellermann3bf54df2008-01-29 13:45:29 +0000385 if (quote_open) {
Max Kellermannfe05c762008-01-29 13:46:01 +0000386 if (escaped) {
387 param_buffer[param_len++] = *curchar;
388 escaped = 0;
389 continue;
390 } else if (*curchar == '\\') {
Max Kellermann3bf54df2008-01-29 13:45:29 +0000391 escaped = 1;
392 continue;
393 } else if (*curchar == '"') {
Harald Welte26e643f2001-05-03 20:50:03 +0000394 quote_open = 0;
395 *curchar = ' ';
Max Kellermann3bf54df2008-01-29 13:45:29 +0000396 } else {
397 param_buffer[param_len++] = *curchar;
398 continue;
399 }
400 } else {
401 if (*curchar == '"') {
Harald Welte26e643f2001-05-03 20:50:03 +0000402 quote_open = 1;
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000403 continue;
Harald Welte26e643f2001-05-03 20:50:03 +0000404 }
Max Kellermann3bf54df2008-01-29 13:45:29 +0000405 }
406
Harald Welte26e643f2001-05-03 20:50:03 +0000407 if (*curchar == ' '
408 || *curchar == '\t'
409 || * curchar == '\n') {
Harald Welte974d0102001-05-26 04:41:56 +0000410 if (!param_len) {
411 /* two spaces? */
Harald Welte974d0102001-05-26 04:41:56 +0000412 continue;
413 }
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000414
415 param_buffer[param_len] = '\0';
Harald Weltebb41f882001-10-21 14:11:54 +0000416
417 /* check if table name specified */
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +0200418 if (!strncmp(param_buffer, "-t", 2)
Max Kellermann5b76f682008-01-29 13:42:48 +0000419 || !strncmp(param_buffer, "--table", 8)) {
420 exit_error(PARAMETER_PROBLEM,
Harald Weltebb41f882001-10-21 14:11:54 +0000421 "Line %u seems to have a "
422 "-t table option.\n", line);
423 exit(1);
424 }
425
Harald Welte26e643f2001-05-03 20:50:03 +0000426 add_argv(param_buffer);
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000427 param_len = 0;
Harald Welte26e643f2001-05-03 20:50:03 +0000428 } else {
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000429 /* regular character, copy to buffer */
430 param_buffer[param_len++] = *curchar;
431
432 if (param_len >= sizeof(param_buffer))
Max Kellermann5b76f682008-01-29 13:42:48 +0000433 exit_error(PARAMETER_PROBLEM,
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000434 "Parameter too long!");
Harald Welte26e643f2001-05-03 20:50:03 +0000435 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000436 }
437
Harald Welte26e643f2001-05-03 20:50:03 +0000438 DEBUGP("calling do_command(%u, argv, &%s, handle):\n",
439 newargc, curtable);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000440
Harald Weltefa48fc82001-10-16 09:51:33 +0000441 for (a = 0; a < newargc; a++)
Harald Welteae1ff9f2000-12-01 14:26:20 +0000442 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
443
Max Kellermann5b76f682008-01-29 13:42:48 +0000444 ret = do_command(newargc, newargv,
Harald Welte26e643f2001-05-03 20:50:03 +0000445 &newargv[2], &handle);
446
447 free_argv();
Henrik Nordstromd2137602008-05-12 20:51:45 +0200448 fflush(stdout);
Marc Bouchere6869a82000-03-20 06:03:29 +0000449 }
Peter Warasinb9cde3a2007-11-05 19:35:31 +0000450 if (tablename && (strcmp(tablename, curtable) != 0))
451 continue;
Marc Bouchere6869a82000-03-20 06:03:29 +0000452 if (!ret) {
453 fprintf(stderr, "%s: line %u failed\n",
454 program_name, line);
455 exit(1);
456 }
457 }
Martin Josefsson3229a7c2004-02-01 21:46:04 +0000458 if (in_table) {
459 fprintf(stderr, "%s: COMMIT expected at line %u\n",
460 program_name, line + 1);
461 exit(1);
462 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000463
464 return 0;
465}