blob: 56812ee9d211d7d73e8b9f245eb97d9c96ca2031 [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 Engelhardtdacafa52009-01-27 20:56:23 +0100132 program_version = IPTABLES_VERSION;
Illes Marci63e90632003-03-03 08:08:37 +0000133 line = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000134
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100135 xtables_program_name = program_name;
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100136 xtables_init();
Harald Welte3efb6ea2001-08-06 18:50:21 +0000137#ifdef NO_SHARED_LIBS
138 init_extensions();
139#endif
140
Peter Warasinb9cde3a2007-11-05 19:35:31 +0000141 while ((c = getopt_long(argc, argv, "bcvthnM:T:", options, NULL)) != -1) {
Harald Weltea9d27082000-12-19 16:10:42 +0000142 switch (c) {
143 case 'b':
144 binary = 1;
145 break;
146 case 'c':
147 counters = 1;
148 break;
Marc Boucher1277b592001-12-06 15:06:34 +0000149 case 'v':
150 verbose = 1;
151 break;
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000152 case 't':
153 testing = 1;
154 break;
Harald Weltea9d27082000-12-19 16:10:42 +0000155 case 'h':
156 print_usage("iptables-restore",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100157 IPTABLES_VERSION);
Harald Weltea9d27082000-12-19 16:10:42 +0000158 break;
159 case 'n':
160 noflush = 1;
161 break;
Harald Welte58918652001-06-16 18:25:25 +0000162 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100163 xtables_modprobe_program = optarg;
Harald Welte58918652001-06-16 18:25:25 +0000164 break;
Peter Warasinb9cde3a2007-11-05 19:35:31 +0000165 case 'T':
166 tablename = optarg;
167 break;
Harald Weltea9d27082000-12-19 16:10:42 +0000168 }
169 }
Max Kellermann5b76f682008-01-29 13:42:48 +0000170
Marc Bouchere6869a82000-03-20 06:03:29 +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],
Marc Bouchere6869a82000-03-20 06:03:29 +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");
Marc Bouchere6869a82000-03-20 06:03:29 +0000181 exit(1);
182 }
183 else in = stdin;
Max Kellermann5b76f682008-01-29 13:42:48 +0000184
Marc Bouchere6869a82000-03-20 06:03:29 +0000185 /* Grab standard input. */
186 while (fgets(buffer, sizeof(buffer), in)) {
Harald Welteb3f22192003-03-06 11:56:31 +0000187 int ret = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000188
189 line++;
Martin Josefsson5065afa2004-01-31 19:33:47 +0000190 if (buffer[0] == '\n')
191 continue;
Marc Bouchere6869a82000-03-20 06:03:29 +0000192 else if (buffer[0] == '#') {
Martin Josefsson3673dcb2004-01-31 19:41:49 +0000193 if (verbose)
194 fputs(buffer, stdout);
Marc Bouchere6869a82000-03-20 06:03:29 +0000195 continue;
Illes Marci26100fa2003-03-03 08:05:07 +0000196 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000197 if (!testing) {
198 DEBUGP("Calling commit\n");
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100199 ret = iptc_commit(handle);
200 iptc_free(handle);
201 handle = NULL;
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000202 } else {
203 DEBUGP("Not calling commit, testing\n");
204 ret = 1;
205 }
Illes Marci26100fa2003-03-03 08:05:07 +0000206 in_table = 0;
Harald Welteb3f22192003-03-06 11:56:31 +0000207 } else if ((buffer[0] == '*') && (!in_table)) {
Harald Welteae1ff9f2000-12-01 14:26:20 +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) {
Max Kellermann5b76f682008-01-29 13:42:48 +0000214 exit_error(PARAMETER_PROBLEM,
Harald Welteae1ff9f2000-12-01 14:26:20 +0000215 "%s: line %u table name invalid\n",
216 program_name, line);
217 exit(1);
218 }
219 strncpy(curtable, table, IPT_TABLE_MAXNAMELEN);
Karsten Desler073df8f2004-01-31 15:33:55 +0000220 curtable[IPT_TABLE_MAXNAMELEN] = '\0';
Harald Welteae1ff9f2000-12-01 14:26:20 +0000221
Peter Warasinb9cde3a2007-11-05 19:35:31 +0000222 if (tablename && (strcmp(tablename, table) != 0))
223 continue;
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000224 if (handle)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100225 iptc_free(handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000226
Jan Engelhardtc3541052008-11-18 12:26:26 +0100227 handle = create_handle(table);
Harald Weltea9d27082000-12-19 16:10:42 +0000228 if (noflush == 0) {
229 DEBUGP("Cleaning all chains of table '%s'\n",
230 table);
Max Kellermann5b76f682008-01-29 13:42:48 +0000231 for_each_chain(flush_entries, verbose, 1,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100232 handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000233
Harald Weltea9d27082000-12-19 16:10:42 +0000234 DEBUGP("Deleting all user-defined chains "
235 "of table '%s'\n", table);
Max Kellermann5b76f682008-01-29 13:42:48 +0000236 for_each_chain(delete_chain, verbose, 0,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100237 handle);
Harald Weltea9d27082000-12-19 16:10:42 +0000238 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000239
240 ret = 1;
Illes Marci26100fa2003-03-03 08:05:07 +0000241 in_table = 1;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000242
Illes Marci26100fa2003-03-03 08:05:07 +0000243 } else if ((buffer[0] == ':') && (in_table)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000244 /* New chain. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000245 char *policy, *chain;
Marc Bouchere6869a82000-03-20 06:03:29 +0000246
Marc Bouchere6869a82000-03-20 06:03:29 +0000247 chain = strtok(buffer+1, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000248 DEBUGP("line %u, chain '%s'\n", line, chain);
Marc Bouchere6869a82000-03-20 06:03:29 +0000249 if (!chain) {
250 exit_error(PARAMETER_PROBLEM,
251 "%s: line %u chain name invalid\n",
252 program_name, line);
253 exit(1);
254 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000255
Harald Welte3a506ac2004-08-30 16:00:09 +0000256 if (iptc_builtin(chain, handle) <= 0) {
Charlie Brady595e4932005-06-12 15:54:15 +0000257 if (noflush && iptc_is_chain(chain, handle)) {
258 DEBUGP("Flushing existing user defined chain '%s'\n", chain);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100259 if (!iptc_flush_entries(chain, handle))
Charlie Brady595e4932005-06-12 15:54:15 +0000260 exit_error(PARAMETER_PROBLEM,
261 "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 (!iptc_create_chain(chain, handle))
Charlie Brady595e4932005-06-12 15:54:15 +0000267 exit_error(PARAMETER_PROBLEM,
268 "error creating chain "
269 "'%s':%s\n", chain,
270 strerror(errno));
271 }
Harald Welte9f7fa492001-03-15 15:12:02 +0000272 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000273
Marc Bouchere6869a82000-03-20 06:03:29 +0000274 policy = strtok(NULL, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000275 DEBUGP("line %u, policy '%s'\n", line, policy);
Marc Bouchere6869a82000-03-20 06:03:29 +0000276 if (!policy) {
277 exit_error(PARAMETER_PROBLEM,
278 "%s: line %u policy invalid\n",
279 program_name, line);
280 exit(1);
281 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000282
283 if (strcmp(policy, "-") != 0) {
Harald Welted8e65632001-01-05 15:20:07 +0000284 struct ipt_counters count;
285
286 if (counters) {
287 char *ctrs;
288 ctrs = strtok(NULL, " \t\n");
289
Harald Welte6f38a302006-02-09 14:35:38 +0000290 if (!ctrs || !parse_counters(ctrs, &count))
291 exit_error(PARAMETER_PROBLEM,
292 "invalid policy counters "
293 "for chain '%s'\n", chain);
Harald Welted8e65632001-01-05 15:20:07 +0000294
295 } else {
Max Kellermann5b76f682008-01-29 13:42:48 +0000296 memset(&count, 0,
Harald Welted8e65632001-01-05 15:20:07 +0000297 sizeof(struct ipt_counters));
298 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000299
300 DEBUGP("Setting policy of chain %s to %s\n",
301 chain, policy);
302
Harald Welted8e65632001-01-05 15:20:07 +0000303 if (!iptc_set_policy(chain, policy, &count,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100304 handle))
Harald Welteae1ff9f2000-12-01 14:26:20 +0000305 exit_error(OTHER_PROBLEM,
306 "Can't set policy `%s'"
307 " on `%s' line %u: %s\n",
308 chain, policy, line,
309 iptc_strerror(errno));
310 }
311
312 ret = 1;
313
Illes Marci26100fa2003-03-03 08:05:07 +0000314 } else if (in_table) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000315 int a;
Marc Bouchere6869a82000-03-20 06:03:29 +0000316 char *ptr = buffer;
Harald Welteccd49e52001-01-23 22:54:34 +0000317 char *pcnt = NULL;
318 char *bcnt = NULL;
Harald Welte9f7fa492001-03-15 15:12:02 +0000319 char *parsestart;
320
Harald Welte26e643f2001-05-03 20:50:03 +0000321 /* the parser */
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000322 char *curchar;
Max Kellermann3bf54df2008-01-29 13:45:29 +0000323 int quote_open, escaped;
Max Kellermannb4ef34f2008-01-29 13:43:35 +0000324 size_t param_len;
Harald Welte26e643f2001-05-03 20:50:03 +0000325
Harald Welte9f7fa492001-03-15 15:12:02 +0000326 /* reset the newargv */
327 newargc = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000328
Marc Bouchere6869a82000-03-20 06:03:29 +0000329 if (buffer[0] == '[') {
Harald Welte9f7fa492001-03-15 15:12:02 +0000330 /* we have counters in our input */
Marc Bouchere6869a82000-03-20 06:03:29 +0000331 ptr = strchr(buffer, ']');
332 if (!ptr)
333 exit_error(PARAMETER_PROBLEM,
334 "Bad line %u: need ]\n",
335 line);
Harald Welte9f7fa492001-03-15 15:12:02 +0000336
Harald Welteccd49e52001-01-23 22:54:34 +0000337 pcnt = strtok(buffer+1, ":");
Harald Welte9f7fa492001-03-15 15:12:02 +0000338 if (!pcnt)
339 exit_error(PARAMETER_PROBLEM,
340 "Bad line %u: need :\n",
341 line);
342
Harald Welteccd49e52001-01-23 22:54:34 +0000343 bcnt = strtok(NULL, "]");
Harald Welte9f7fa492001-03-15 15:12:02 +0000344 if (!bcnt)
345 exit_error(PARAMETER_PROBLEM,
346 "Bad line %u: need ]\n",
347 line);
348
349 /* start command parsing after counter */
350 parsestart = ptr + 1;
351 } else {
352 /* start command parsing at start of line */
353 parsestart = buffer;
Marc Bouchere6869a82000-03-20 06:03:29 +0000354 }
Rusty Russell7e53bf92000-03-20 07:03:28 +0000355
Harald Welte9f7fa492001-03-15 15:12:02 +0000356 add_argv(argv[0]);
357 add_argv("-t");
358 add_argv((char *) &curtable);
Max Kellermann5b76f682008-01-29 13:42:48 +0000359
Harald Welteccd49e52001-01-23 22:54:34 +0000360 if (counters && pcnt && bcnt) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000361 add_argv("--set-counters");
362 add_argv((char *) pcnt);
363 add_argv((char *) bcnt);
Harald Welteccd49e52001-01-23 22:54:34 +0000364 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000365
Harald Welte26e643f2001-05-03 20:50:03 +0000366 /* After fighting with strtok enough, here's now
367 * a 'real' parser. According to Rusty I'm now no
368 * longer a real hacker, but I can live with that */
369
370 quote_open = 0;
Max Kellermann3bf54df2008-01-29 13:45:29 +0000371 escaped = 0;
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000372 param_len = 0;
Max Kellermann5b76f682008-01-29 13:42:48 +0000373
Harald Welte26e643f2001-05-03 20:50:03 +0000374 for (curchar = parsestart; *curchar; curchar++) {
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000375 char param_buffer[1024];
376
Max Kellermann3bf54df2008-01-29 13:45:29 +0000377 if (quote_open) {
Max Kellermannfe05c762008-01-29 13:46:01 +0000378 if (escaped) {
379 param_buffer[param_len++] = *curchar;
380 escaped = 0;
381 continue;
382 } else if (*curchar == '\\') {
Max Kellermann3bf54df2008-01-29 13:45:29 +0000383 escaped = 1;
384 continue;
385 } else if (*curchar == '"') {
Harald Welte26e643f2001-05-03 20:50:03 +0000386 quote_open = 0;
387 *curchar = ' ';
Max Kellermann3bf54df2008-01-29 13:45:29 +0000388 } else {
389 param_buffer[param_len++] = *curchar;
390 continue;
391 }
392 } else {
393 if (*curchar == '"') {
Harald Welte26e643f2001-05-03 20:50:03 +0000394 quote_open = 1;
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000395 continue;
Harald Welte26e643f2001-05-03 20:50:03 +0000396 }
Max Kellermann3bf54df2008-01-29 13:45:29 +0000397 }
398
Harald Welte26e643f2001-05-03 20:50:03 +0000399 if (*curchar == ' '
400 || *curchar == '\t'
401 || * curchar == '\n') {
Harald Welte974d0102001-05-26 04:41:56 +0000402 if (!param_len) {
403 /* two spaces? */
Harald Welte974d0102001-05-26 04:41:56 +0000404 continue;
405 }
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000406
407 param_buffer[param_len] = '\0';
Harald Weltebb41f882001-10-21 14:11:54 +0000408
409 /* check if table name specified */
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +0200410 if (!strncmp(param_buffer, "-t", 2)
Max Kellermann5b76f682008-01-29 13:42:48 +0000411 || !strncmp(param_buffer, "--table", 8)) {
412 exit_error(PARAMETER_PROBLEM,
Harald Weltebb41f882001-10-21 14:11:54 +0000413 "Line %u seems to have a "
414 "-t table option.\n", line);
415 exit(1);
416 }
417
Harald Welte26e643f2001-05-03 20:50:03 +0000418 add_argv(param_buffer);
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000419 param_len = 0;
Harald Welte26e643f2001-05-03 20:50:03 +0000420 } else {
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000421 /* regular character, copy to buffer */
422 param_buffer[param_len++] = *curchar;
423
424 if (param_len >= sizeof(param_buffer))
Max Kellermann5b76f682008-01-29 13:42:48 +0000425 exit_error(PARAMETER_PROBLEM,
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000426 "Parameter too long!");
Harald Welte26e643f2001-05-03 20:50:03 +0000427 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000428 }
429
Harald Welte26e643f2001-05-03 20:50:03 +0000430 DEBUGP("calling do_command(%u, argv, &%s, handle):\n",
431 newargc, curtable);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000432
Harald Weltefa48fc82001-10-16 09:51:33 +0000433 for (a = 0; a < newargc; a++)
Harald Welteae1ff9f2000-12-01 14:26:20 +0000434 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
435
Max Kellermann5b76f682008-01-29 13:42:48 +0000436 ret = do_command(newargc, newargv,
Harald Welte26e643f2001-05-03 20:50:03 +0000437 &newargv[2], &handle);
438
439 free_argv();
Henrik Nordstromd2137602008-05-12 20:51:45 +0200440 fflush(stdout);
Marc Bouchere6869a82000-03-20 06:03:29 +0000441 }
Peter Warasinb9cde3a2007-11-05 19:35:31 +0000442 if (tablename && (strcmp(tablename, curtable) != 0))
443 continue;
Marc Bouchere6869a82000-03-20 06:03:29 +0000444 if (!ret) {
445 fprintf(stderr, "%s: line %u failed\n",
446 program_name, line);
447 exit(1);
448 }
449 }
Martin Josefsson3229a7c2004-02-01 21:46:04 +0000450 if (in_table) {
451 fprintf(stderr, "%s: COMMIT expected at line %u\n",
452 program_name, line + 1);
453 exit(1);
454 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000455
456 return 0;
457}