blob: d982fca156b0db1b5ee88130d5070ea9c640bfdd [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 Engelhardt39bf9c82009-01-27 15:59:06 +0100135 xtables_init();
Harald Welte3efb6ea2001-08-06 18:50:21 +0000136#ifdef NO_SHARED_LIBS
137 init_extensions();
138#endif
139
Peter Warasinb9cde3a2007-11-05 19:35:31 +0000140 while ((c = getopt_long(argc, argv, "bcvthnM:T:", options, NULL)) != -1) {
Harald Weltea9d27082000-12-19 16:10:42 +0000141 switch (c) {
142 case 'b':
143 binary = 1;
144 break;
145 case 'c':
146 counters = 1;
147 break;
Marc Boucher1277b592001-12-06 15:06:34 +0000148 case 'v':
149 verbose = 1;
150 break;
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000151 case 't':
152 testing = 1;
153 break;
Harald Weltea9d27082000-12-19 16:10:42 +0000154 case 'h':
155 print_usage("iptables-restore",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200156 XTABLES_VERSION);
Harald Weltea9d27082000-12-19 16:10:42 +0000157 break;
158 case 'n':
159 noflush = 1;
160 break;
Harald Welte58918652001-06-16 18:25:25 +0000161 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100162 xtables_modprobe_program = optarg;
Harald Welte58918652001-06-16 18:25:25 +0000163 break;
Peter Warasinb9cde3a2007-11-05 19:35:31 +0000164 case 'T':
165 tablename = optarg;
166 break;
Harald Weltea9d27082000-12-19 16:10:42 +0000167 }
168 }
Max Kellermann5b76f682008-01-29 13:42:48 +0000169
Marc Bouchere6869a82000-03-20 06:03:29 +0000170 if (optind == argc - 1) {
171 in = fopen(argv[optind], "r");
172 if (!in) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000173 fprintf(stderr, "Can't open %s: %s\n", argv[optind],
Marc Bouchere6869a82000-03-20 06:03:29 +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");
Marc Bouchere6869a82000-03-20 06:03:29 +0000180 exit(1);
181 }
182 else in = stdin;
Max Kellermann5b76f682008-01-29 13:42:48 +0000183
Marc Bouchere6869a82000-03-20 06:03:29 +0000184 /* Grab standard input. */
185 while (fgets(buffer, sizeof(buffer), in)) {
Harald Welteb3f22192003-03-06 11:56:31 +0000186 int ret = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000187
188 line++;
Martin Josefsson5065afa2004-01-31 19:33:47 +0000189 if (buffer[0] == '\n')
190 continue;
Marc Bouchere6869a82000-03-20 06:03:29 +0000191 else if (buffer[0] == '#') {
Martin Josefsson3673dcb2004-01-31 19:41:49 +0000192 if (verbose)
193 fputs(buffer, stdout);
Marc Bouchere6869a82000-03-20 06:03:29 +0000194 continue;
Illes Marci26100fa2003-03-03 08:05:07 +0000195 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000196 if (!testing) {
197 DEBUGP("Calling commit\n");
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100198 ret = iptc_commit(handle);
199 iptc_free(handle);
200 handle = NULL;
Martin Josefssonbb2f68a2004-02-01 22:03:27 +0000201 } else {
202 DEBUGP("Not calling commit, testing\n");
203 ret = 1;
204 }
Illes Marci26100fa2003-03-03 08:05:07 +0000205 in_table = 0;
Harald Welteb3f22192003-03-06 11:56:31 +0000206 } else if ((buffer[0] == '*') && (!in_table)) {
Harald Welteae1ff9f2000-12-01 14:26:20 +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) {
Max Kellermann5b76f682008-01-29 13:42:48 +0000213 exit_error(PARAMETER_PROBLEM,
Harald Welteae1ff9f2000-12-01 14:26:20 +0000214 "%s: line %u table name invalid\n",
215 program_name, line);
216 exit(1);
217 }
218 strncpy(curtable, table, IPT_TABLE_MAXNAMELEN);
Karsten Desler073df8f2004-01-31 15:33:55 +0000219 curtable[IPT_TABLE_MAXNAMELEN] = '\0';
Harald Welteae1ff9f2000-12-01 14:26:20 +0000220
Peter Warasinb9cde3a2007-11-05 19:35:31 +0000221 if (tablename && (strcmp(tablename, table) != 0))
222 continue;
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000223 if (handle)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100224 iptc_free(handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000225
Jan Engelhardtc3541052008-11-18 12:26:26 +0100226 handle = create_handle(table);
Harald Weltea9d27082000-12-19 16:10:42 +0000227 if (noflush == 0) {
228 DEBUGP("Cleaning all chains of table '%s'\n",
229 table);
Max Kellermann5b76f682008-01-29 13:42:48 +0000230 for_each_chain(flush_entries, verbose, 1,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100231 handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000232
Harald Weltea9d27082000-12-19 16:10:42 +0000233 DEBUGP("Deleting all user-defined chains "
234 "of table '%s'\n", table);
Max Kellermann5b76f682008-01-29 13:42:48 +0000235 for_each_chain(delete_chain, verbose, 0,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100236 handle);
Harald Weltea9d27082000-12-19 16:10:42 +0000237 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000238
239 ret = 1;
Illes Marci26100fa2003-03-03 08:05:07 +0000240 in_table = 1;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000241
Illes Marci26100fa2003-03-03 08:05:07 +0000242 } else if ((buffer[0] == ':') && (in_table)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000243 /* New chain. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000244 char *policy, *chain;
Marc Bouchere6869a82000-03-20 06:03:29 +0000245
Marc Bouchere6869a82000-03-20 06:03:29 +0000246 chain = strtok(buffer+1, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000247 DEBUGP("line %u, chain '%s'\n", line, chain);
Marc Bouchere6869a82000-03-20 06:03:29 +0000248 if (!chain) {
249 exit_error(PARAMETER_PROBLEM,
250 "%s: line %u chain name invalid\n",
251 program_name, line);
252 exit(1);
253 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000254
Harald Welte3a506ac2004-08-30 16:00:09 +0000255 if (iptc_builtin(chain, handle) <= 0) {
Charlie Brady595e4932005-06-12 15:54:15 +0000256 if (noflush && iptc_is_chain(chain, handle)) {
257 DEBUGP("Flushing existing user defined chain '%s'\n", chain);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100258 if (!iptc_flush_entries(chain, handle))
Charlie Brady595e4932005-06-12 15:54:15 +0000259 exit_error(PARAMETER_PROBLEM,
260 "error flushing chain "
261 "'%s':%s\n", chain,
262 strerror(errno));
263 } else {
264 DEBUGP("Creating new chain '%s'\n", chain);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100265 if (!iptc_create_chain(chain, handle))
Charlie Brady595e4932005-06-12 15:54:15 +0000266 exit_error(PARAMETER_PROBLEM,
267 "error creating chain "
268 "'%s':%s\n", chain,
269 strerror(errno));
270 }
Harald Welte9f7fa492001-03-15 15:12:02 +0000271 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000272
Marc Bouchere6869a82000-03-20 06:03:29 +0000273 policy = strtok(NULL, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000274 DEBUGP("line %u, policy '%s'\n", line, policy);
Marc Bouchere6869a82000-03-20 06:03:29 +0000275 if (!policy) {
276 exit_error(PARAMETER_PROBLEM,
277 "%s: line %u policy invalid\n",
278 program_name, line);
279 exit(1);
280 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000281
282 if (strcmp(policy, "-") != 0) {
Harald Welted8e65632001-01-05 15:20:07 +0000283 struct ipt_counters count;
284
285 if (counters) {
286 char *ctrs;
287 ctrs = strtok(NULL, " \t\n");
288
Harald Welte6f38a302006-02-09 14:35:38 +0000289 if (!ctrs || !parse_counters(ctrs, &count))
290 exit_error(PARAMETER_PROBLEM,
291 "invalid policy counters "
292 "for chain '%s'\n", chain);
Harald Welted8e65632001-01-05 15:20:07 +0000293
294 } else {
Max Kellermann5b76f682008-01-29 13:42:48 +0000295 memset(&count, 0,
Harald Welted8e65632001-01-05 15:20:07 +0000296 sizeof(struct ipt_counters));
297 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000298
299 DEBUGP("Setting policy of chain %s to %s\n",
300 chain, policy);
301
Harald Welted8e65632001-01-05 15:20:07 +0000302 if (!iptc_set_policy(chain, policy, &count,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100303 handle))
Harald Welteae1ff9f2000-12-01 14:26:20 +0000304 exit_error(OTHER_PROBLEM,
305 "Can't set policy `%s'"
306 " on `%s' line %u: %s\n",
307 chain, policy, line,
308 iptc_strerror(errno));
309 }
310
311 ret = 1;
312
Illes Marci26100fa2003-03-03 08:05:07 +0000313 } else if (in_table) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000314 int a;
Marc Bouchere6869a82000-03-20 06:03:29 +0000315 char *ptr = buffer;
Harald Welteccd49e52001-01-23 22:54:34 +0000316 char *pcnt = NULL;
317 char *bcnt = NULL;
Harald Welte9f7fa492001-03-15 15:12:02 +0000318 char *parsestart;
319
Harald Welte26e643f2001-05-03 20:50:03 +0000320 /* the parser */
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000321 char *curchar;
Max Kellermann3bf54df2008-01-29 13:45:29 +0000322 int quote_open, escaped;
Max Kellermannb4ef34f2008-01-29 13:43:35 +0000323 size_t param_len;
Harald Welte26e643f2001-05-03 20:50:03 +0000324
Harald Welte9f7fa492001-03-15 15:12:02 +0000325 /* reset the newargv */
326 newargc = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000327
Marc Bouchere6869a82000-03-20 06:03:29 +0000328 if (buffer[0] == '[') {
Harald Welte9f7fa492001-03-15 15:12:02 +0000329 /* we have counters in our input */
Marc Bouchere6869a82000-03-20 06:03:29 +0000330 ptr = strchr(buffer, ']');
331 if (!ptr)
332 exit_error(PARAMETER_PROBLEM,
333 "Bad line %u: need ]\n",
334 line);
Harald Welte9f7fa492001-03-15 15:12:02 +0000335
Harald Welteccd49e52001-01-23 22:54:34 +0000336 pcnt = strtok(buffer+1, ":");
Harald Welte9f7fa492001-03-15 15:12:02 +0000337 if (!pcnt)
338 exit_error(PARAMETER_PROBLEM,
339 "Bad line %u: need :\n",
340 line);
341
Harald Welteccd49e52001-01-23 22:54:34 +0000342 bcnt = strtok(NULL, "]");
Harald Welte9f7fa492001-03-15 15:12:02 +0000343 if (!bcnt)
344 exit_error(PARAMETER_PROBLEM,
345 "Bad line %u: need ]\n",
346 line);
347
348 /* start command parsing after counter */
349 parsestart = ptr + 1;
350 } else {
351 /* start command parsing at start of line */
352 parsestart = buffer;
Marc Bouchere6869a82000-03-20 06:03:29 +0000353 }
Rusty Russell7e53bf92000-03-20 07:03:28 +0000354
Harald Welte9f7fa492001-03-15 15:12:02 +0000355 add_argv(argv[0]);
356 add_argv("-t");
357 add_argv((char *) &curtable);
Max Kellermann5b76f682008-01-29 13:42:48 +0000358
Harald Welteccd49e52001-01-23 22:54:34 +0000359 if (counters && pcnt && bcnt) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000360 add_argv("--set-counters");
361 add_argv((char *) pcnt);
362 add_argv((char *) bcnt);
Harald Welteccd49e52001-01-23 22:54:34 +0000363 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000364
Harald Welte26e643f2001-05-03 20:50:03 +0000365 /* After fighting with strtok enough, here's now
366 * a 'real' parser. According to Rusty I'm now no
367 * longer a real hacker, but I can live with that */
368
369 quote_open = 0;
Max Kellermann3bf54df2008-01-29 13:45:29 +0000370 escaped = 0;
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000371 param_len = 0;
Max Kellermann5b76f682008-01-29 13:42:48 +0000372
Harald Welte26e643f2001-05-03 20:50:03 +0000373 for (curchar = parsestart; *curchar; curchar++) {
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000374 char param_buffer[1024];
375
Max Kellermann3bf54df2008-01-29 13:45:29 +0000376 if (quote_open) {
Max Kellermannfe05c762008-01-29 13:46:01 +0000377 if (escaped) {
378 param_buffer[param_len++] = *curchar;
379 escaped = 0;
380 continue;
381 } else if (*curchar == '\\') {
Max Kellermann3bf54df2008-01-29 13:45:29 +0000382 escaped = 1;
383 continue;
384 } else if (*curchar == '"') {
Harald Welte26e643f2001-05-03 20:50:03 +0000385 quote_open = 0;
386 *curchar = ' ';
Max Kellermann3bf54df2008-01-29 13:45:29 +0000387 } else {
388 param_buffer[param_len++] = *curchar;
389 continue;
390 }
391 } else {
392 if (*curchar == '"') {
Harald Welte26e643f2001-05-03 20:50:03 +0000393 quote_open = 1;
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000394 continue;
Harald Welte26e643f2001-05-03 20:50:03 +0000395 }
Max Kellermann3bf54df2008-01-29 13:45:29 +0000396 }
397
Harald Welte26e643f2001-05-03 20:50:03 +0000398 if (*curchar == ' '
399 || *curchar == '\t'
400 || * curchar == '\n') {
Harald Welte974d0102001-05-26 04:41:56 +0000401 if (!param_len) {
402 /* two spaces? */
Harald Welte974d0102001-05-26 04:41:56 +0000403 continue;
404 }
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000405
406 param_buffer[param_len] = '\0';
Harald Weltebb41f882001-10-21 14:11:54 +0000407
408 /* check if table name specified */
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +0200409 if (!strncmp(param_buffer, "-t", 2)
Max Kellermann5b76f682008-01-29 13:42:48 +0000410 || !strncmp(param_buffer, "--table", 8)) {
411 exit_error(PARAMETER_PROBLEM,
Harald Weltebb41f882001-10-21 14:11:54 +0000412 "Line %u seems to have a "
413 "-t table option.\n", line);
414 exit(1);
415 }
416
Harald Welte26e643f2001-05-03 20:50:03 +0000417 add_argv(param_buffer);
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000418 param_len = 0;
Harald Welte26e643f2001-05-03 20:50:03 +0000419 } else {
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000420 /* regular character, copy to buffer */
421 param_buffer[param_len++] = *curchar;
422
423 if (param_len >= sizeof(param_buffer))
Max Kellermann5b76f682008-01-29 13:42:48 +0000424 exit_error(PARAMETER_PROBLEM,
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000425 "Parameter too long!");
Harald Welte26e643f2001-05-03 20:50:03 +0000426 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000427 }
428
Harald Welte26e643f2001-05-03 20:50:03 +0000429 DEBUGP("calling do_command(%u, argv, &%s, handle):\n",
430 newargc, curtable);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000431
Harald Weltefa48fc82001-10-16 09:51:33 +0000432 for (a = 0; a < newargc; a++)
Harald Welteae1ff9f2000-12-01 14:26:20 +0000433 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
434
Max Kellermann5b76f682008-01-29 13:42:48 +0000435 ret = do_command(newargc, newargv,
Harald Welte26e643f2001-05-03 20:50:03 +0000436 &newargv[2], &handle);
437
438 free_argv();
Henrik Nordstromd2137602008-05-12 20:51:45 +0200439 fflush(stdout);
Marc Bouchere6869a82000-03-20 06:03:29 +0000440 }
Peter Warasinb9cde3a2007-11-05 19:35:31 +0000441 if (tablename && (strcmp(tablename, curtable) != 0))
442 continue;
Marc Bouchere6869a82000-03-20 06:03:29 +0000443 if (!ret) {
444 fprintf(stderr, "%s: line %u failed\n",
445 program_name, line);
446 exit(1);
447 }
448 }
Martin Josefsson3229a7c2004-02-01 21:46:04 +0000449 if (in_table) {
450 fprintf(stderr, "%s: COMMIT expected at line %u\n",
451 program_name, line + 1);
452 exit(1);
453 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000454
455 return 0;
456}