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