blob: a8ce7cc46e04325dfa1616d640187a039baba979 [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>
12#include <string.h>
13#include <stdio.h>
Harald Welteae1ff9f2000-12-01 14:26:20 +000014#include <stdlib.h>
15#include "iptables.h"
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000016#include "xtables.h"
Harald Welteae1ff9f2000-12-01 14:26:20 +000017#include "libiptc/libiptc.h"
Jan Engelhardt33690a12008-02-11 00:54:00 +010018#include "iptables-multi.h"
Harald Welteae1ff9f2000-12-01 14:26:20 +000019
20#ifdef DEBUG
Harald Weltec45c3152000-12-06 08:11:24 +000021#define DEBUGP(x, args...) fprintf(stderr, x, ## args)
Harald Welteae1ff9f2000-12-01 14:26:20 +000022#else
Max Kellermann5b76f682008-01-29 13:42:48 +000023#define DEBUGP(x, args...)
Harald Welteae1ff9f2000-12-01 14:26:20 +000024#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000025
Harald Weltea9d27082000-12-19 16:10:42 +000026static int binary = 0, counters = 0, verbose = 0, noflush = 0;
27
Marc Bouchere6869a82000-03-20 06:03:29 +000028/* Keeping track of external matches and targets. */
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +010029static const struct option options[] = {
30 {.name = "binary", .has_arg = false, .val = 'b'},
31 {.name = "counters", .has_arg = false, .val = 'c'},
32 {.name = "verbose", .has_arg = false, .val = 'v'},
33 {.name = "test", .has_arg = false, .val = 't'},
34 {.name = "help", .has_arg = false, .val = 'h'},
35 {.name = "noflush", .has_arg = false, .val = 'n'},
36 {.name = "modprobe", .has_arg = true, .val = 'M'},
37 {.name = "table", .has_arg = true, .val = 'T'},
38 {NULL},
Marc Bouchere6869a82000-03-20 06:03:29 +000039};
40
41static void print_usage(const char *name, const char *version) __attribute__((noreturn));
42
43static void print_usage(const char *name, const char *version)
44{
Martin Josefssonbb2f68a2004-02-01 22:03:27 +000045 fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-t] [-h]\n"
Harald Weltea9d27082000-12-19 16:10:42 +000046 " [ --binary ]\n"
47 " [ --counters ]\n"
48 " [ --verbose ]\n"
Martin Josefssonbb2f68a2004-02-01 22:03:27 +000049 " [ --test ]\n"
Harald Weltea9d27082000-12-19 16:10:42 +000050 " [ --help ]\n"
Harald Welte58918652001-06-16 18:25:25 +000051 " [ --noflush ]\n"
Peter Warasinb9cde3a2007-11-05 19:35:31 +000052 " [ --table=<TABLE> ]\n"
Max Kellermann5b76f682008-01-29 13:42:48 +000053 " [ --modprobe=<command>]\n", name);
54
Marc Bouchere6869a82000-03-20 06:03:29 +000055 exit(1);
56}
57
Jan Engelhardtfd187312008-11-10 16:59:27 +010058static struct iptc_handle *create_handle(const char *tablename, const char *modprobe)
Marc Bouchere6869a82000-03-20 06:03:29 +000059{
Jan Engelhardtfd187312008-11-10 16:59:27 +010060 struct iptc_handle *handle;
Marc Bouchere6869a82000-03-20 06:03:29 +000061
Harald Welteae1ff9f2000-12-01 14:26:20 +000062 handle = iptc_init(tablename);
Harald Welte58918652001-06-16 18:25:25 +000063
64 if (!handle) {
65 /* try to insmod the module if iptc_init failed */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000066 load_xtables_ko(modprobe, 0);
Harald Welte58918652001-06-16 18:25:25 +000067 handle = iptc_init(tablename);
68 }
69
Harald Welteae1ff9f2000-12-01 14:26:20 +000070 if (!handle) {
Patrick McHardy5b098ac2007-02-14 13:59:12 +000071 exit_error(PARAMETER_PROBLEM, "%s: unable to initialize "
Harald Welteae1ff9f2000-12-01 14:26:20 +000072 "table '%s'\n", program_name, tablename);
73 exit(1);
Marc Bouchere6869a82000-03-20 06:03:29 +000074 }
Harald Welteae1ff9f2000-12-01 14:26:20 +000075 return handle;
Marc Bouchere6869a82000-03-20 06:03:29 +000076}
77
Lutz Jaenickee78c69c2006-12-09 13:06:04 +000078static int parse_counters(char *string, struct ipt_counters *ctr)
Harald Welted8e65632001-01-05 15:20:07 +000079{
Patrick McHardy875441e2007-10-17 08:48:58 +000080 unsigned long long pcnt, bcnt;
81 int ret;
Patrick McHardyb73691f2007-09-05 14:10:53 +000082
Patrick McHardy875441e2007-10-17 08:48:58 +000083 ret = sscanf(string, "[%llu:%llu]",
84 (unsigned long long *)&pcnt,
85 (unsigned long long *)&bcnt);
86 ctr->pcnt = pcnt;
87 ctr->bcnt = bcnt;
88 return ret == 2;
Harald Welted8e65632001-01-05 15:20:07 +000089}
Harald Welteae1ff9f2000-12-01 14:26:20 +000090
Harald Welte9f7fa492001-03-15 15:12:02 +000091/* global new argv and argc */
Harald Welte26e643f2001-05-03 20:50:03 +000092static char *newargv[255];
Harald Welte9f7fa492001-03-15 15:12:02 +000093static int newargc;
94
95/* function adding one argument to newargv, updating newargc
96 * returns true if argument added, false otherwise */
97static int add_argv(char *what) {
Harald Weltebb41f882001-10-21 14:11:54 +000098 DEBUGP("add_argv: %s\n", what);
Harald Welte9f7fa492001-03-15 15:12:02 +000099 if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) {
Harald Welte26e643f2001-05-03 20:50:03 +0000100 newargv[newargc] = strdup(what);
Harald Welte9f7fa492001-03-15 15:12:02 +0000101 newargc++;
102 return 1;
103 } else
104 return 0;
105}
106
Harald Welte26e643f2001-05-03 20:50:03 +0000107static void free_argv(void) {
108 int i;
109
110 for (i = 0; i < newargc; i++)
111 free(newargv[i]);
112}
113
Bastiaan Bakker4e3771f2004-06-25 11:18:57 +0000114#ifdef IPTABLES_MULTI
115int
116iptables_restore_main(int argc, char *argv[])
117#else
118int
119main(int argc, char *argv[])
120#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000121{
Jan Engelhardtfd187312008-11-10 16:59:27 +0100122 struct iptc_handle *handle = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000123 char buffer[10240];
Harald Weltea9d27082000-12-19 16:10:42 +0000124 int c;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000125 char curtable[IPT_TABLE_MAXNAMELEN + 1];
Marc Bouchere6869a82000-03-20 06:03:29 +0000126 FILE *in;
Patrick McHardy0ea82bc2008-06-07 15:15:29 +0200127 const char *modprobe = NULL;
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)
139 fprintf(stderr, "IPTABLES_LIB_DIR is deprecated\n");
140 }
141 if (lib_dir == NULL)
142 lib_dir = XTABLES_LIBDIR;
Martin Josefsson357d59d2004-12-27 19:49:28 +0000143
Harald Welte3efb6ea2001-08-06 18:50:21 +0000144#ifdef NO_SHARED_LIBS
145 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 Engelhardt8b7c64d2008-04-15 11:48:25 +0200164 XTABLES_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':
170 modprobe = optarg;
171 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");
206 ret = iptc_commit(&handle);
207 } 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) {
Max Kellermann5b76f682008-01-29 13:42:48 +0000219 exit_error(PARAMETER_PROBLEM,
Harald Welteae1ff9f2000-12-01 14:26:20 +0000220 "%s: line %u table name invalid\n",
221 program_name, line);
222 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)
230 iptc_free(&handle);
231
Harald Welte58918652001-06-16 18:25:25 +0000232 handle = create_handle(table, modprobe);
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,
Harald Weltea9d27082000-12-19 16:10:42 +0000237 &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,
Harald Weltea9d27082000-12-19 16:10:42 +0000242 &handle) ;
243 }
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) {
255 exit_error(PARAMETER_PROBLEM,
256 "%s: line %u chain name invalid\n",
257 program_name, line);
258 exit(1);
259 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000260
Harald Welte3a506ac2004-08-30 16:00:09 +0000261 if (iptc_builtin(chain, handle) <= 0) {
Charlie Brady595e4932005-06-12 15:54:15 +0000262 if (noflush && iptc_is_chain(chain, handle)) {
263 DEBUGP("Flushing existing user defined chain '%s'\n", chain);
264 if (!iptc_flush_entries(chain, &handle))
265 exit_error(PARAMETER_PROBLEM,
266 "error flushing chain "
267 "'%s':%s\n", chain,
268 strerror(errno));
269 } else {
270 DEBUGP("Creating new chain '%s'\n", chain);
271 if (!iptc_create_chain(chain, &handle))
272 exit_error(PARAMETER_PROBLEM,
273 "error creating chain "
274 "'%s':%s\n", chain,
275 strerror(errno));
276 }
Harald Welte9f7fa492001-03-15 15:12:02 +0000277 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000278
Marc Bouchere6869a82000-03-20 06:03:29 +0000279 policy = strtok(NULL, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000280 DEBUGP("line %u, policy '%s'\n", line, policy);
Marc Bouchere6869a82000-03-20 06:03:29 +0000281 if (!policy) {
282 exit_error(PARAMETER_PROBLEM,
283 "%s: line %u policy invalid\n",
284 program_name, line);
285 exit(1);
286 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000287
288 if (strcmp(policy, "-") != 0) {
Harald Welted8e65632001-01-05 15:20:07 +0000289 struct ipt_counters count;
290
291 if (counters) {
292 char *ctrs;
293 ctrs = strtok(NULL, " \t\n");
294
Harald Welte6f38a302006-02-09 14:35:38 +0000295 if (!ctrs || !parse_counters(ctrs, &count))
296 exit_error(PARAMETER_PROBLEM,
297 "invalid policy counters "
298 "for chain '%s'\n", chain);
Harald Welted8e65632001-01-05 15:20:07 +0000299
300 } else {
Max Kellermann5b76f682008-01-29 13:42:48 +0000301 memset(&count, 0,
Harald Welted8e65632001-01-05 15:20:07 +0000302 sizeof(struct ipt_counters));
303 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000304
305 DEBUGP("Setting policy of chain %s to %s\n",
306 chain, policy);
307
Harald Welted8e65632001-01-05 15:20:07 +0000308 if (!iptc_set_policy(chain, policy, &count,
309 &handle))
Harald Welteae1ff9f2000-12-01 14:26:20 +0000310 exit_error(OTHER_PROBLEM,
311 "Can't set policy `%s'"
312 " on `%s' line %u: %s\n",
313 chain, policy, line,
314 iptc_strerror(errno));
315 }
316
317 ret = 1;
318
Illes Marci26100fa2003-03-03 08:05:07 +0000319 } else if (in_table) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000320 int a;
Marc Bouchere6869a82000-03-20 06:03:29 +0000321 char *ptr = buffer;
Harald Welteccd49e52001-01-23 22:54:34 +0000322 char *pcnt = NULL;
323 char *bcnt = NULL;
Harald Welte9f7fa492001-03-15 15:12:02 +0000324 char *parsestart;
325
Harald Welte26e643f2001-05-03 20:50:03 +0000326 /* the parser */
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000327 char *curchar;
Max Kellermann3bf54df2008-01-29 13:45:29 +0000328 int quote_open, escaped;
Max Kellermannb4ef34f2008-01-29 13:43:35 +0000329 size_t param_len;
Harald Welte26e643f2001-05-03 20:50:03 +0000330
Harald Welte9f7fa492001-03-15 15:12:02 +0000331 /* reset the newargv */
332 newargc = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000333
Marc Bouchere6869a82000-03-20 06:03:29 +0000334 if (buffer[0] == '[') {
Harald Welte9f7fa492001-03-15 15:12:02 +0000335 /* we have counters in our input */
Marc Bouchere6869a82000-03-20 06:03:29 +0000336 ptr = strchr(buffer, ']');
337 if (!ptr)
338 exit_error(PARAMETER_PROBLEM,
339 "Bad line %u: need ]\n",
340 line);
Harald Welte9f7fa492001-03-15 15:12:02 +0000341
Harald Welteccd49e52001-01-23 22:54:34 +0000342 pcnt = strtok(buffer+1, ":");
Harald Welte9f7fa492001-03-15 15:12:02 +0000343 if (!pcnt)
344 exit_error(PARAMETER_PROBLEM,
345 "Bad line %u: need :\n",
346 line);
347
Harald Welteccd49e52001-01-23 22:54:34 +0000348 bcnt = strtok(NULL, "]");
Harald Welte9f7fa492001-03-15 15:12:02 +0000349 if (!bcnt)
350 exit_error(PARAMETER_PROBLEM,
351 "Bad line %u: need ]\n",
352 line);
353
354 /* start command parsing after counter */
355 parsestart = ptr + 1;
356 } else {
357 /* start command parsing at start of line */
358 parsestart = buffer;
Marc Bouchere6869a82000-03-20 06:03:29 +0000359 }
Rusty Russell7e53bf92000-03-20 07:03:28 +0000360
Harald Welte9f7fa492001-03-15 15:12:02 +0000361 add_argv(argv[0]);
362 add_argv("-t");
363 add_argv((char *) &curtable);
Max Kellermann5b76f682008-01-29 13:42:48 +0000364
Harald Welteccd49e52001-01-23 22:54:34 +0000365 if (counters && pcnt && bcnt) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000366 add_argv("--set-counters");
367 add_argv((char *) pcnt);
368 add_argv((char *) bcnt);
Harald Welteccd49e52001-01-23 22:54:34 +0000369 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000370
Harald Welte26e643f2001-05-03 20:50:03 +0000371 /* After fighting with strtok enough, here's now
372 * a 'real' parser. According to Rusty I'm now no
373 * longer a real hacker, but I can live with that */
374
375 quote_open = 0;
Max Kellermann3bf54df2008-01-29 13:45:29 +0000376 escaped = 0;
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000377 param_len = 0;
Max Kellermann5b76f682008-01-29 13:42:48 +0000378
Harald Welte26e643f2001-05-03 20:50:03 +0000379 for (curchar = parsestart; *curchar; curchar++) {
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000380 char param_buffer[1024];
381
Max Kellermann3bf54df2008-01-29 13:45:29 +0000382 if (quote_open) {
Max Kellermannfe05c762008-01-29 13:46:01 +0000383 if (escaped) {
384 param_buffer[param_len++] = *curchar;
385 escaped = 0;
386 continue;
387 } else if (*curchar == '\\') {
Max Kellermann3bf54df2008-01-29 13:45:29 +0000388 escaped = 1;
389 continue;
390 } else if (*curchar == '"') {
Harald Welte26e643f2001-05-03 20:50:03 +0000391 quote_open = 0;
392 *curchar = ' ';
Max Kellermann3bf54df2008-01-29 13:45:29 +0000393 } else {
394 param_buffer[param_len++] = *curchar;
395 continue;
396 }
397 } else {
398 if (*curchar == '"') {
Harald Welte26e643f2001-05-03 20:50:03 +0000399 quote_open = 1;
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000400 continue;
Harald Welte26e643f2001-05-03 20:50:03 +0000401 }
Max Kellermann3bf54df2008-01-29 13:45:29 +0000402 }
403
Harald Welte26e643f2001-05-03 20:50:03 +0000404 if (*curchar == ' '
405 || *curchar == '\t'
406 || * curchar == '\n') {
Harald Welte974d0102001-05-26 04:41:56 +0000407 if (!param_len) {
408 /* two spaces? */
Harald Welte974d0102001-05-26 04:41:56 +0000409 continue;
410 }
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000411
412 param_buffer[param_len] = '\0';
Harald Weltebb41f882001-10-21 14:11:54 +0000413
414 /* check if table name specified */
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +0200415 if (!strncmp(param_buffer, "-t", 2)
Max Kellermann5b76f682008-01-29 13:42:48 +0000416 || !strncmp(param_buffer, "--table", 8)) {
417 exit_error(PARAMETER_PROBLEM,
Harald Weltebb41f882001-10-21 14:11:54 +0000418 "Line %u seems to have a "
419 "-t table option.\n", line);
420 exit(1);
421 }
422
Harald Welte26e643f2001-05-03 20:50:03 +0000423 add_argv(param_buffer);
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000424 param_len = 0;
Harald Welte26e643f2001-05-03 20:50:03 +0000425 } else {
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000426 /* regular character, copy to buffer */
427 param_buffer[param_len++] = *curchar;
428
429 if (param_len >= sizeof(param_buffer))
Max Kellermann5b76f682008-01-29 13:42:48 +0000430 exit_error(PARAMETER_PROBLEM,
Pablo Neira Ayusob4f31642007-04-18 10:27:02 +0000431 "Parameter too long!");
Harald Welte26e643f2001-05-03 20:50:03 +0000432 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000433 }
434
Harald Welte26e643f2001-05-03 20:50:03 +0000435 DEBUGP("calling do_command(%u, argv, &%s, handle):\n",
436 newargc, curtable);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000437
Harald Weltefa48fc82001-10-16 09:51:33 +0000438 for (a = 0; a < newargc; a++)
Harald Welteae1ff9f2000-12-01 14:26:20 +0000439 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
440
Max Kellermann5b76f682008-01-29 13:42:48 +0000441 ret = do_command(newargc, newargv,
Harald Welte26e643f2001-05-03 20:50:03 +0000442 &newargv[2], &handle);
443
444 free_argv();
Henrik Nordstromd2137602008-05-12 20:51:45 +0200445 fflush(stdout);
Marc Bouchere6869a82000-03-20 06:03:29 +0000446 }
Peter Warasinb9cde3a2007-11-05 19:35:31 +0000447 if (tablename && (strcmp(tablename, curtable) != 0))
448 continue;
Marc Bouchere6869a82000-03-20 06:03:29 +0000449 if (!ret) {
450 fprintf(stderr, "%s: line %u failed\n",
451 program_name, line);
452 exit(1);
453 }
454 }
Martin Josefsson3229a7c2004-02-01 21:46:04 +0000455 if (in_table) {
456 fprintf(stderr, "%s: COMMIT expected at line %u\n",
457 program_name, line + 1);
458 exit(1);
459 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000460
461 return 0;
462}