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