András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 1 | /* Code to restore the iptables state, from file by ip6tables-save. |
| 2 | * Author: Andras Kis-Szabo <kisza@sch.bme.hu> |
| 3 | * |
| 4 | * based on iptables-restore |
| 5 | * Authors: |
| 6 | * Harald Welte <laforge@gnumonks.org> |
| 7 | * Rusty Russell <rusty@linuxcare.com.au> |
András Kis-Szabó | 0c4188f | 2002-08-14 11:40:41 +0000 | [diff] [blame] | 8 | * This code is distributed under the terms of GNU GPL v2 |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 9 | * |
Martin Josefsson | 357d59d | 2004-12-27 19:49:28 +0000 | [diff] [blame] | 10 | * $Id$ |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <getopt.h> |
| 14 | #include <sys/errno.h> |
| 15 | #include <string.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include "ip6tables.h" |
Yasuyuki KOZAKAI | 0b82e8e | 2007-07-24 05:47:40 +0000 | [diff] [blame] | 19 | #include "xtables.h" |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 20 | #include "libiptc/libip6tc.h" |
| 21 | |
| 22 | #ifdef DEBUG |
| 23 | #define DEBUGP(x, args...) fprintf(stderr, x, ## args) |
| 24 | #else |
| 25 | #define DEBUGP(x, args...) |
| 26 | #endif |
| 27 | |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 28 | static int binary = 0, counters = 0, verbose = 0, noflush = 0; |
| 29 | |
| 30 | /* Keeping track of external matches and targets. */ |
| 31 | static struct option options[] = { |
| 32 | { "binary", 0, 0, 'b' }, |
| 33 | { "counters", 0, 0, 'c' }, |
Martin Josefsson | ec78971 | 2004-01-31 19:28:13 +0000 | [diff] [blame] | 34 | { "verbose", 0, 0, 'v' }, |
Martin Josefsson | 4e5e29a | 2004-02-02 19:59:17 +0000 | [diff] [blame] | 35 | { "test", 0, 0, 't' }, |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 36 | { "help", 0, 0, 'h' }, |
| 37 | { "noflush", 0, 0, 'n'}, |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 38 | { "modprobe", 1, 0, 'M'}, |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 39 | { 0 } |
| 40 | }; |
| 41 | |
| 42 | static void print_usage(const char *name, const char *version) __attribute__((noreturn)); |
| 43 | |
| 44 | static void print_usage(const char *name, const char *version) |
| 45 | { |
Martin Josefsson | 6004439 | 2004-02-02 20:12:33 +0000 | [diff] [blame] | 46 | fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-t] [-h]\n" |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 47 | " [ --binary ]\n" |
| 48 | " [ --counters ]\n" |
| 49 | " [ --verbose ]\n" |
Martin Josefsson | 4e5e29a | 2004-02-02 19:59:17 +0000 | [diff] [blame] | 50 | " [ --test ]\n" |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 51 | " [ --help ]\n" |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 52 | " [ --noflush ]\n" |
| 53 | " [ --modprobe=<command>]\n", name); |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 54 | |
| 55 | exit(1); |
| 56 | } |
| 57 | |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 58 | ip6tc_handle_t create_handle(const char *tablename, const char* modprobe) |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 59 | { |
| 60 | ip6tc_handle_t handle; |
| 61 | |
| 62 | handle = ip6tc_init(tablename); |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 63 | |
| 64 | if (!handle) { |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 65 | /* try to insmod the module if iptc_init failed */ |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame^] | 66 | load_xtables_ko(modprobe, 0); |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 67 | handle = ip6tc_init(tablename); |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 68 | } |
| 69 | |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 70 | if (!handle) { |
Patrick McHardy | 5b098ac | 2007-02-14 13:59:12 +0000 | [diff] [blame] | 71 | exit_error(PARAMETER_PROBLEM, "%s: unable to initialize " |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 72 | "table '%s'\n", program_name, tablename); |
| 73 | exit(1); |
| 74 | } |
| 75 | return handle; |
| 76 | } |
| 77 | |
Lutz Jaenicke | e78c69c | 2006-12-09 13:06:04 +0000 | [diff] [blame] | 78 | static int parse_counters(char *string, struct ip6t_counters *ctr) |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 79 | { |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 80 | return (sscanf(string, "[%llu:%llu]", (unsigned long long *)&ctr->pcnt, (unsigned long long *)&ctr->bcnt) == 2); |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 83 | /* global new argv and argc */ |
| 84 | static char *newargv[255]; |
| 85 | static int newargc; |
| 86 | |
| 87 | /* function adding one argument to newargv, updating newargc |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 88 | * returns true if argument added, false otherwise */ |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 89 | static int add_argv(char *what) { |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 90 | DEBUGP("add_argv: %s\n", what); |
| 91 | if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) { |
| 92 | newargv[newargc] = strdup(what); |
| 93 | newargc++; |
| 94 | return 1; |
| 95 | } else |
| 96 | return 0; |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static void free_argv(void) { |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 100 | int i; |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 101 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 102 | for (i = 0; i < newargc; i++) |
| 103 | free(newargv[i]); |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 104 | } |
| 105 | |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 106 | int main(int argc, char *argv[]) |
| 107 | { |
Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 108 | ip6tc_handle_t handle = NULL; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 109 | char buffer[10240]; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 110 | int c; |
| 111 | char curtable[IP6T_TABLE_MAXNAMELEN + 1]; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 112 | FILE *in; |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 113 | const char *modprobe = 0; |
Martin Josefsson | 4e5e29a | 2004-02-02 19:59:17 +0000 | [diff] [blame] | 114 | int in_table = 0, testing = 0; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 115 | |
| 116 | program_name = "ip6tables-restore"; |
Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 117 | program_version = IPTABLES_VERSION; |
Harald Welte | a8658ca | 2003-03-05 07:46:15 +0000 | [diff] [blame] | 118 | line = 0; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 119 | |
Martin Josefsson | 357d59d | 2004-12-27 19:49:28 +0000 | [diff] [blame] | 120 | lib_dir = getenv("IP6TABLES_LIB_DIR"); |
| 121 | if (!lib_dir) |
| 122 | lib_dir = IP6T_LIB_DIR; |
| 123 | |
Harald Welte | 3efb6ea | 2001-08-06 18:50:21 +0000 | [diff] [blame] | 124 | #ifdef NO_SHARED_LIBS |
| 125 | init_extensions(); |
| 126 | #endif |
| 127 | |
Martin Josefsson | 4e5e29a | 2004-02-02 19:59:17 +0000 | [diff] [blame] | 128 | while ((c = getopt_long(argc, argv, "bcvthnM:", options, NULL)) != -1) { |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 129 | switch (c) { |
| 130 | case 'b': |
| 131 | binary = 1; |
| 132 | break; |
| 133 | case 'c': |
| 134 | counters = 1; |
| 135 | break; |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 136 | case 'v': |
| 137 | verbose = 1; |
| 138 | break; |
Martin Josefsson | 4e5e29a | 2004-02-02 19:59:17 +0000 | [diff] [blame] | 139 | case 't': |
| 140 | testing = 1; |
| 141 | break; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 142 | case 'h': |
| 143 | print_usage("ip6tables-restore", |
Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 144 | IPTABLES_VERSION); |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 145 | break; |
| 146 | case 'n': |
| 147 | noflush = 1; |
| 148 | break; |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 149 | case 'M': |
| 150 | modprobe = optarg; |
| 151 | break; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | if (optind == argc - 1) { |
| 156 | in = fopen(argv[optind], "r"); |
| 157 | if (!in) { |
Pavel Rusnak | 972af09 | 2007-05-10 15:00:39 +0000 | [diff] [blame] | 158 | fprintf(stderr, "Can't open %s: %s\n", argv[optind], |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 159 | strerror(errno)); |
| 160 | exit(1); |
| 161 | } |
| 162 | } |
| 163 | else if (optind < argc) { |
Pavel Rusnak | 972af09 | 2007-05-10 15:00:39 +0000 | [diff] [blame] | 164 | fprintf(stderr, "Unknown arguments found on commandline\n"); |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 165 | exit(1); |
| 166 | } |
| 167 | else in = stdin; |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 168 | |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 169 | /* Grab standard input. */ |
| 170 | while (fgets(buffer, sizeof(buffer), in)) { |
Martin Josefsson | cc53628 | 2004-02-02 20:14:56 +0000 | [diff] [blame] | 171 | int ret = 0; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 172 | |
| 173 | line++; |
Martin Josefsson | 5065afa | 2004-01-31 19:33:47 +0000 | [diff] [blame] | 174 | if (buffer[0] == '\n') |
| 175 | continue; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 176 | else if (buffer[0] == '#') { |
Martin Josefsson | 3673dcb | 2004-01-31 19:41:49 +0000 | [diff] [blame] | 177 | if (verbose) |
| 178 | fputs(buffer, stdout); |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 179 | continue; |
Harald Welte | a8658ca | 2003-03-05 07:46:15 +0000 | [diff] [blame] | 180 | } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) { |
Martin Josefsson | 4e5e29a | 2004-02-02 19:59:17 +0000 | [diff] [blame] | 181 | if (!testing) { |
| 182 | DEBUGP("Calling commit\n"); |
| 183 | ret = ip6tc_commit(&handle); |
| 184 | } else { |
| 185 | DEBUGP("Not calling commit, testing\n"); |
| 186 | ret = 1; |
| 187 | } |
Harald Welte | a8658ca | 2003-03-05 07:46:15 +0000 | [diff] [blame] | 188 | in_table = 0; |
| 189 | } else if ((buffer[0] == '*') && (!in_table)) { |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 190 | /* New table */ |
| 191 | char *table; |
| 192 | |
| 193 | table = strtok(buffer+1, " \t\n"); |
| 194 | DEBUGP("line %u, table '%s'\n", line, table); |
| 195 | if (!table) { |
| 196 | exit_error(PARAMETER_PROBLEM, |
| 197 | "%s: line %u table name invalid\n", |
| 198 | program_name, line); |
| 199 | exit(1); |
| 200 | } |
| 201 | strncpy(curtable, table, IP6T_TABLE_MAXNAMELEN); |
Karsten Desler | 073df8f | 2004-01-31 15:33:55 +0000 | [diff] [blame] | 202 | curtable[IP6T_TABLE_MAXNAMELEN] = '\0'; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 203 | |
Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 204 | if (handle) |
| 205 | ip6tc_free(&handle); |
| 206 | |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 207 | handle = create_handle(table, modprobe); |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 208 | if (noflush == 0) { |
| 209 | DEBUGP("Cleaning all chains of table '%s'\n", |
| 210 | table); |
| 211 | for_each_chain(flush_entries, verbose, 1, |
| 212 | &handle); |
| 213 | |
| 214 | DEBUGP("Deleting all user-defined chains " |
| 215 | "of table '%s'\n", table); |
| 216 | for_each_chain(delete_chain, verbose, 0, |
| 217 | &handle) ; |
| 218 | } |
| 219 | |
| 220 | ret = 1; |
Harald Welte | a8658ca | 2003-03-05 07:46:15 +0000 | [diff] [blame] | 221 | in_table = 1; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 222 | |
Harald Welte | a8658ca | 2003-03-05 07:46:15 +0000 | [diff] [blame] | 223 | } else if ((buffer[0] == ':') && (in_table)) { |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 224 | /* New chain. */ |
| 225 | char *policy, *chain; |
| 226 | |
| 227 | chain = strtok(buffer+1, " \t\n"); |
| 228 | DEBUGP("line %u, chain '%s'\n", line, chain); |
| 229 | if (!chain) { |
| 230 | exit_error(PARAMETER_PROBLEM, |
| 231 | "%s: line %u chain name invalid\n", |
| 232 | program_name, line); |
| 233 | exit(1); |
| 234 | } |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 235 | |
Harald Welte | df1c71c | 2004-08-30 16:00:32 +0000 | [diff] [blame] | 236 | if (ip6tc_builtin(chain, handle) <= 0) { |
Charlie Brady | 595e493 | 2005-06-12 15:54:15 +0000 | [diff] [blame] | 237 | if (noflush && ip6tc_is_chain(chain, handle)) { |
| 238 | DEBUGP("Flushing existing user defined chain '%s'\n", chain); |
| 239 | if (!ip6tc_flush_entries(chain, &handle)) |
| 240 | exit_error(PARAMETER_PROBLEM, |
| 241 | "error flushing chain " |
| 242 | "'%s':%s\n", chain, |
| 243 | strerror(errno)); |
| 244 | } else { |
| 245 | DEBUGP("Creating new chain '%s'\n", chain); |
| 246 | if (!ip6tc_create_chain(chain, &handle)) |
| 247 | exit_error(PARAMETER_PROBLEM, |
| 248 | "error creating chain " |
| 249 | "'%s':%s\n", chain, |
| 250 | strerror(errno)); |
| 251 | } |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 252 | } |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 253 | |
| 254 | policy = strtok(NULL, " \t\n"); |
| 255 | DEBUGP("line %u, policy '%s'\n", line, policy); |
| 256 | if (!policy) { |
| 257 | exit_error(PARAMETER_PROBLEM, |
| 258 | "%s: line %u policy invalid\n", |
| 259 | program_name, line); |
| 260 | exit(1); |
| 261 | } |
| 262 | |
| 263 | if (strcmp(policy, "-") != 0) { |
| 264 | struct ip6t_counters count; |
| 265 | |
| 266 | if (counters) { |
| 267 | char *ctrs; |
| 268 | ctrs = strtok(NULL, " \t\n"); |
| 269 | |
Harald Welte | 6f38a30 | 2006-02-09 14:35:38 +0000 | [diff] [blame] | 270 | if (!ctrs || !parse_counters(ctrs, &count)) |
| 271 | exit_error(PARAMETER_PROBLEM, |
| 272 | "invalid policy counters " |
| 273 | "for chain '%s'\n", chain); |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 274 | |
| 275 | } else { |
| 276 | memset(&count, 0, |
| 277 | sizeof(struct ip6t_counters)); |
| 278 | } |
| 279 | |
| 280 | DEBUGP("Setting policy of chain %s to %s\n", |
| 281 | chain, policy); |
| 282 | |
| 283 | if (!ip6tc_set_policy(chain, policy, &count, |
| 284 | &handle)) |
| 285 | exit_error(OTHER_PROBLEM, |
| 286 | "Can't set policy `%s'" |
| 287 | " on `%s' line %u: %s\n", |
| 288 | chain, policy, line, |
| 289 | ip6tc_strerror(errno)); |
| 290 | } |
| 291 | |
| 292 | ret = 1; |
| 293 | |
Harald Welte | a8658ca | 2003-03-05 07:46:15 +0000 | [diff] [blame] | 294 | } else if (in_table) { |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 295 | int a; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 296 | char *ptr = buffer; |
| 297 | char *pcnt = NULL; |
| 298 | char *bcnt = NULL; |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 299 | char *parsestart; |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 300 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 301 | /* the parser */ |
| 302 | char *param_start, *curchar; |
| 303 | int quote_open; |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 304 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 305 | /* reset the newargv */ |
| 306 | newargc = 0; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 307 | |
| 308 | if (buffer[0] == '[') { |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 309 | /* we have counters in our input */ |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 310 | ptr = strchr(buffer, ']'); |
| 311 | if (!ptr) |
| 312 | exit_error(PARAMETER_PROBLEM, |
| 313 | "Bad line %u: need ]\n", |
| 314 | line); |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 315 | |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 316 | pcnt = strtok(buffer+1, ":"); |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 317 | if (!pcnt) |
| 318 | exit_error(PARAMETER_PROBLEM, |
| 319 | "Bad line %u: need :\n", |
| 320 | line); |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 321 | |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 322 | bcnt = strtok(NULL, "]"); |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 323 | if (!bcnt) |
| 324 | exit_error(PARAMETER_PROBLEM, |
| 325 | "Bad line %u: need ]\n", |
| 326 | line); |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 327 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 328 | /* start command parsing after counter */ |
| 329 | parsestart = ptr + 1; |
| 330 | } else { |
| 331 | /* start command parsing at start of line */ |
| 332 | parsestart = buffer; |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 333 | } |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 334 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 335 | add_argv(argv[0]); |
| 336 | add_argv("-t"); |
| 337 | add_argv((char *) &curtable); |
| 338 | |
| 339 | if (counters && pcnt && bcnt) { |
| 340 | add_argv("--set-counters"); |
| 341 | add_argv((char *) pcnt); |
| 342 | add_argv((char *) bcnt); |
| 343 | } |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 344 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 345 | /* After fighting with strtok enough, here's now |
| 346 | * a 'real' parser. According to Rusty I'm now no |
| 347 | * longer a real hacker, but I can live with that */ |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 348 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 349 | quote_open = 0; |
| 350 | param_start = parsestart; |
| 351 | |
| 352 | for (curchar = parsestart; *curchar; curchar++) { |
| 353 | if (*curchar == '"') { |
Michael Rash | 29cdbd0 | 2004-01-05 09:41:50 +0000 | [diff] [blame] | 354 | /* quote_open cannot be true if there |
| 355 | * was no previous character. Thus, |
| 356 | * curchar-1 has to be within bounds */ |
| 357 | if (quote_open && |
| 358 | *(curchar-1) != '\\') { |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 359 | quote_open = 0; |
| 360 | *curchar = ' '; |
| 361 | } else { |
| 362 | quote_open = 1; |
| 363 | param_start++; |
| 364 | } |
| 365 | } |
| 366 | if (*curchar == ' ' |
| 367 | || *curchar == '\t' |
| 368 | || * curchar == '\n') { |
| 369 | char param_buffer[1024]; |
| 370 | int param_len = curchar-param_start; |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 371 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 372 | if (quote_open) |
| 373 | continue; |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 374 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 375 | if (!param_len) { |
| 376 | /* two spaces? */ |
| 377 | param_start++; |
| 378 | continue; |
| 379 | } |
| 380 | |
| 381 | /* end of one parameter */ |
| 382 | strncpy(param_buffer, param_start, |
| 383 | param_len); |
| 384 | *(param_buffer+param_len) = '\0'; |
Harald Welte | bb41f88 | 2001-10-21 14:11:54 +0000 | [diff] [blame] | 385 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 386 | /* check if table name specified */ |
Harald Welte | 48a6f90 | 2001-10-22 15:16:21 +0000 | [diff] [blame] | 387 | if (!strncmp(param_buffer, "-t", 3) |
| 388 | || !strncmp(param_buffer, "--table", 8)) { |
Harald Welte | bb41f88 | 2001-10-21 14:11:54 +0000 | [diff] [blame] | 389 | exit_error(PARAMETER_PROBLEM, |
| 390 | "Line %u seems to have a " |
| 391 | "-t table option.\n", line); |
| 392 | exit(1); |
| 393 | } |
| 394 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 395 | add_argv(param_buffer); |
| 396 | param_start += param_len + 1; |
| 397 | } else { |
| 398 | /* regular character, skip */ |
| 399 | } |
| 400 | } |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 401 | |
| 402 | DEBUGP("calling do_command6(%u, argv, &%s, handle):\n", |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 403 | newargc, curtable); |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 404 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 405 | for (a = 0; a < newargc; a++) |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 406 | DEBUGP("argv[%u]: %s\n", a, newargv[a]); |
| 407 | |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 408 | ret = do_command6(newargc, newargv, |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 409 | &newargv[2], &handle); |
Harald Welte | 885c6eb | 2001-10-04 08:30:46 +0000 | [diff] [blame] | 410 | |
András Kis-Szabó | 97fd91a | 2002-03-03 09:44:31 +0000 | [diff] [blame] | 411 | free_argv(); |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 412 | } |
| 413 | if (!ret) { |
| 414 | fprintf(stderr, "%s: line %u failed\n", |
| 415 | program_name, line); |
| 416 | exit(1); |
| 417 | } |
| 418 | } |
Martin Josefsson | e0dc581 | 2004-02-02 19:58:36 +0000 | [diff] [blame] | 419 | if (in_table) { |
| 420 | fprintf(stderr, "%s: COMMIT expected at line %u\n", |
| 421 | program_name, line + 1); |
| 422 | exit(1); |
| 423 | } |
András Kis-Szabó | 2f52379 | 2001-02-27 09:59:48 +0000 | [diff] [blame] | 424 | |
| 425 | return 0; |
| 426 | } |