Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +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" |
Yasuyuki KOZAKAI | 0b82e8e | 2007-07-24 05:47:40 +0000 | [diff] [blame] | 16 | #include "xtables.h" |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 17 | #include "libiptc/libiptc.h" |
Jan Engelhardt | 33690a1 | 2008-02-11 00:54:00 +0100 | [diff] [blame] | 18 | #include "iptables-multi.h" |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 19 | |
| 20 | #ifdef DEBUG |
Harald Welte | c45c315 | 2000-12-06 08:11:24 +0000 | [diff] [blame] | 21 | #define DEBUGP(x, args...) fprintf(stderr, x, ## args) |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 22 | #else |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 23 | #define DEBUGP(x, args...) |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 24 | #endif |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 25 | |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame] | 26 | static int binary = 0, counters = 0, verbose = 0, noflush = 0; |
| 27 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 28 | /* Keeping track of external matches and targets. */ |
Gáspár Lajos | 7bc3cb7 | 2008-03-27 08:20:39 +0100 | [diff] [blame] | 29 | static 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 Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | static void print_usage(const char *name, const char *version) __attribute__((noreturn)); |
| 42 | |
| 43 | static void print_usage(const char *name, const char *version) |
| 44 | { |
Martin Josefsson | bb2f68a | 2004-02-01 22:03:27 +0000 | [diff] [blame] | 45 | fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-t] [-h]\n" |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame] | 46 | " [ --binary ]\n" |
| 47 | " [ --counters ]\n" |
| 48 | " [ --verbose ]\n" |
Martin Josefsson | bb2f68a | 2004-02-01 22:03:27 +0000 | [diff] [blame] | 49 | " [ --test ]\n" |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame] | 50 | " [ --help ]\n" |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 51 | " [ --noflush ]\n" |
Peter Warasin | b9cde3a | 2007-11-05 19:35:31 +0000 | [diff] [blame] | 52 | " [ --table=<TABLE> ]\n" |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 53 | " [ --modprobe=<command>]\n", name); |
| 54 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 55 | exit(1); |
| 56 | } |
| 57 | |
Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame] | 58 | static struct iptc_handle *create_handle(const char *tablename, const char *modprobe) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 59 | { |
Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame] | 60 | struct iptc_handle *handle; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 61 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 62 | handle = iptc_init(tablename); |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 63 | |
| 64 | if (!handle) { |
| 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); |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 67 | handle = iptc_init(tablename); |
| 68 | } |
| 69 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +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 " |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 72 | "table '%s'\n", program_name, tablename); |
| 73 | exit(1); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 74 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 75 | return handle; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Lutz Jaenicke | e78c69c | 2006-12-09 13:06:04 +0000 | [diff] [blame] | 78 | static int parse_counters(char *string, struct ipt_counters *ctr) |
Harald Welte | d8e6563 | 2001-01-05 15:20:07 +0000 | [diff] [blame] | 79 | { |
Patrick McHardy | 875441e | 2007-10-17 08:48:58 +0000 | [diff] [blame] | 80 | unsigned long long pcnt, bcnt; |
| 81 | int ret; |
Patrick McHardy | b73691f | 2007-09-05 14:10:53 +0000 | [diff] [blame] | 82 | |
Patrick McHardy | 875441e | 2007-10-17 08:48:58 +0000 | [diff] [blame] | 83 | 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 Welte | d8e6563 | 2001-01-05 15:20:07 +0000 | [diff] [blame] | 89 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 90 | |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 91 | /* global new argv and argc */ |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 92 | static char *newargv[255]; |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 93 | static int newargc; |
| 94 | |
| 95 | /* function adding one argument to newargv, updating newargc |
| 96 | * returns true if argument added, false otherwise */ |
| 97 | static int add_argv(char *what) { |
Harald Welte | bb41f88 | 2001-10-21 14:11:54 +0000 | [diff] [blame] | 98 | DEBUGP("add_argv: %s\n", what); |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 99 | if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) { |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 100 | newargv[newargc] = strdup(what); |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 101 | newargc++; |
| 102 | return 1; |
| 103 | } else |
| 104 | return 0; |
| 105 | } |
| 106 | |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 107 | static void free_argv(void) { |
| 108 | int i; |
| 109 | |
| 110 | for (i = 0; i < newargc; i++) |
| 111 | free(newargv[i]); |
| 112 | } |
| 113 | |
Bastiaan Bakker | 4e3771f | 2004-06-25 11:18:57 +0000 | [diff] [blame] | 114 | #ifdef IPTABLES_MULTI |
| 115 | int |
| 116 | iptables_restore_main(int argc, char *argv[]) |
| 117 | #else |
| 118 | int |
| 119 | main(int argc, char *argv[]) |
| 120 | #endif |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 121 | { |
Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame] | 122 | struct iptc_handle *handle = NULL; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 123 | char buffer[10240]; |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame] | 124 | int c; |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 125 | char curtable[IPT_TABLE_MAXNAMELEN + 1]; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +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 | bb2f68a | 2004-02-01 22:03:27 +0000 | [diff] [blame] | 128 | int in_table = 0, testing = 0; |
Patrick McHardy | 0ea82bc | 2008-06-07 15:15:29 +0200 | [diff] [blame] | 129 | const char *tablename = NULL; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 130 | |
| 131 | program_name = "iptables-restore"; |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 132 | program_version = XTABLES_VERSION; |
Illes Marci | 63e9063 | 2003-03-03 08:08:37 +0000 | [diff] [blame] | 133 | line = 0; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 134 | |
Jan Engelhardt | 21b41ee | 2008-02-11 01:02:00 +0100 | [diff] [blame] | 135 | 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 Josefsson | 357d59d | 2004-12-27 19:49:28 +0000 | [diff] [blame] | 143 | |
Harald Welte | 3efb6ea | 2001-08-06 18:50:21 +0000 | [diff] [blame] | 144 | #ifdef NO_SHARED_LIBS |
| 145 | init_extensions(); |
| 146 | #endif |
| 147 | |
Peter Warasin | b9cde3a | 2007-11-05 19:35:31 +0000 | [diff] [blame] | 148 | while ((c = getopt_long(argc, argv, "bcvthnM:T:", options, NULL)) != -1) { |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame] | 149 | switch (c) { |
| 150 | case 'b': |
| 151 | binary = 1; |
| 152 | break; |
| 153 | case 'c': |
| 154 | counters = 1; |
| 155 | break; |
Marc Boucher | 1277b59 | 2001-12-06 15:06:34 +0000 | [diff] [blame] | 156 | case 'v': |
| 157 | verbose = 1; |
| 158 | break; |
Martin Josefsson | bb2f68a | 2004-02-01 22:03:27 +0000 | [diff] [blame] | 159 | case 't': |
| 160 | testing = 1; |
| 161 | break; |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame] | 162 | case 'h': |
| 163 | print_usage("iptables-restore", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 164 | XTABLES_VERSION); |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame] | 165 | break; |
| 166 | case 'n': |
| 167 | noflush = 1; |
| 168 | break; |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 169 | case 'M': |
| 170 | modprobe = optarg; |
| 171 | break; |
Peter Warasin | b9cde3a | 2007-11-05 19:35:31 +0000 | [diff] [blame] | 172 | case 'T': |
| 173 | tablename = optarg; |
| 174 | break; |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 177 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 178 | if (optind == argc - 1) { |
| 179 | in = fopen(argv[optind], "r"); |
| 180 | if (!in) { |
Pavel Rusnak | 972af09 | 2007-05-10 15:00:39 +0000 | [diff] [blame] | 181 | fprintf(stderr, "Can't open %s: %s\n", argv[optind], |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 182 | strerror(errno)); |
| 183 | exit(1); |
| 184 | } |
| 185 | } |
| 186 | else if (optind < argc) { |
Pavel Rusnak | 972af09 | 2007-05-10 15:00:39 +0000 | [diff] [blame] | 187 | fprintf(stderr, "Unknown arguments found on commandline\n"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 188 | exit(1); |
| 189 | } |
| 190 | else in = stdin; |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 191 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 192 | /* Grab standard input. */ |
| 193 | while (fgets(buffer, sizeof(buffer), in)) { |
Harald Welte | b3f2219 | 2003-03-06 11:56:31 +0000 | [diff] [blame] | 194 | int ret = 0; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 195 | |
| 196 | line++; |
Martin Josefsson | 5065afa | 2004-01-31 19:33:47 +0000 | [diff] [blame] | 197 | if (buffer[0] == '\n') |
| 198 | continue; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 199 | else if (buffer[0] == '#') { |
Martin Josefsson | 3673dcb | 2004-01-31 19:41:49 +0000 | [diff] [blame] | 200 | if (verbose) |
| 201 | fputs(buffer, stdout); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 202 | continue; |
Illes Marci | 26100fa | 2003-03-03 08:05:07 +0000 | [diff] [blame] | 203 | } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) { |
Martin Josefsson | bb2f68a | 2004-02-01 22:03:27 +0000 | [diff] [blame] | 204 | if (!testing) { |
| 205 | DEBUGP("Calling commit\n"); |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame^] | 206 | ret = iptc_commit(handle); |
| 207 | iptc_free(handle); |
| 208 | handle = NULL; |
Martin Josefsson | bb2f68a | 2004-02-01 22:03:27 +0000 | [diff] [blame] | 209 | } else { |
| 210 | DEBUGP("Not calling commit, testing\n"); |
| 211 | ret = 1; |
| 212 | } |
Illes Marci | 26100fa | 2003-03-03 08:05:07 +0000 | [diff] [blame] | 213 | in_table = 0; |
Harald Welte | b3f2219 | 2003-03-06 11:56:31 +0000 | [diff] [blame] | 214 | } else if ((buffer[0] == '*') && (!in_table)) { |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 215 | /* New table */ |
| 216 | char *table; |
| 217 | |
| 218 | table = strtok(buffer+1, " \t\n"); |
| 219 | DEBUGP("line %u, table '%s'\n", line, table); |
| 220 | if (!table) { |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 221 | exit_error(PARAMETER_PROBLEM, |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 222 | "%s: line %u table name invalid\n", |
| 223 | program_name, line); |
| 224 | exit(1); |
| 225 | } |
| 226 | strncpy(curtable, table, IPT_TABLE_MAXNAMELEN); |
Karsten Desler | 073df8f | 2004-01-31 15:33:55 +0000 | [diff] [blame] | 227 | curtable[IPT_TABLE_MAXNAMELEN] = '\0'; |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 228 | |
Peter Warasin | b9cde3a | 2007-11-05 19:35:31 +0000 | [diff] [blame] | 229 | if (tablename && (strcmp(tablename, table) != 0)) |
| 230 | continue; |
Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 231 | if (handle) |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame^] | 232 | iptc_free(handle); |
Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 233 | |
Harald Welte | 5891865 | 2001-06-16 18:25:25 +0000 | [diff] [blame] | 234 | handle = create_handle(table, modprobe); |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame] | 235 | if (noflush == 0) { |
| 236 | DEBUGP("Cleaning all chains of table '%s'\n", |
| 237 | table); |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 238 | for_each_chain(flush_entries, verbose, 1, |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame^] | 239 | handle); |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 240 | |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame] | 241 | DEBUGP("Deleting all user-defined chains " |
| 242 | "of table '%s'\n", table); |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 243 | for_each_chain(delete_chain, verbose, 0, |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame^] | 244 | handle); |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame] | 245 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 246 | |
| 247 | ret = 1; |
Illes Marci | 26100fa | 2003-03-03 08:05:07 +0000 | [diff] [blame] | 248 | in_table = 1; |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 249 | |
Illes Marci | 26100fa | 2003-03-03 08:05:07 +0000 | [diff] [blame] | 250 | } else if ((buffer[0] == ':') && (in_table)) { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 251 | /* New chain. */ |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 252 | char *policy, *chain; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 253 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 254 | chain = strtok(buffer+1, " \t\n"); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 255 | DEBUGP("line %u, chain '%s'\n", line, chain); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 256 | if (!chain) { |
| 257 | exit_error(PARAMETER_PROBLEM, |
| 258 | "%s: line %u chain name invalid\n", |
| 259 | program_name, line); |
| 260 | exit(1); |
| 261 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 262 | |
Harald Welte | 3a506ac | 2004-08-30 16:00:09 +0000 | [diff] [blame] | 263 | if (iptc_builtin(chain, handle) <= 0) { |
Charlie Brady | 595e493 | 2005-06-12 15:54:15 +0000 | [diff] [blame] | 264 | if (noflush && iptc_is_chain(chain, handle)) { |
| 265 | DEBUGP("Flushing existing user defined chain '%s'\n", chain); |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame^] | 266 | if (!iptc_flush_entries(chain, handle)) |
Charlie Brady | 595e493 | 2005-06-12 15:54:15 +0000 | [diff] [blame] | 267 | exit_error(PARAMETER_PROBLEM, |
| 268 | "error flushing chain " |
| 269 | "'%s':%s\n", chain, |
| 270 | strerror(errno)); |
| 271 | } else { |
| 272 | DEBUGP("Creating new chain '%s'\n", chain); |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame^] | 273 | if (!iptc_create_chain(chain, handle)) |
Charlie Brady | 595e493 | 2005-06-12 15:54:15 +0000 | [diff] [blame] | 274 | exit_error(PARAMETER_PROBLEM, |
| 275 | "error creating chain " |
| 276 | "'%s':%s\n", chain, |
| 277 | strerror(errno)); |
| 278 | } |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 279 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 280 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 281 | policy = strtok(NULL, " \t\n"); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 282 | DEBUGP("line %u, policy '%s'\n", line, policy); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 283 | if (!policy) { |
| 284 | exit_error(PARAMETER_PROBLEM, |
| 285 | "%s: line %u policy invalid\n", |
| 286 | program_name, line); |
| 287 | exit(1); |
| 288 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 289 | |
| 290 | if (strcmp(policy, "-") != 0) { |
Harald Welte | d8e6563 | 2001-01-05 15:20:07 +0000 | [diff] [blame] | 291 | struct ipt_counters count; |
| 292 | |
| 293 | if (counters) { |
| 294 | char *ctrs; |
| 295 | ctrs = strtok(NULL, " \t\n"); |
| 296 | |
Harald Welte | 6f38a30 | 2006-02-09 14:35:38 +0000 | [diff] [blame] | 297 | if (!ctrs || !parse_counters(ctrs, &count)) |
| 298 | exit_error(PARAMETER_PROBLEM, |
| 299 | "invalid policy counters " |
| 300 | "for chain '%s'\n", chain); |
Harald Welte | d8e6563 | 2001-01-05 15:20:07 +0000 | [diff] [blame] | 301 | |
| 302 | } else { |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 303 | memset(&count, 0, |
Harald Welte | d8e6563 | 2001-01-05 15:20:07 +0000 | [diff] [blame] | 304 | sizeof(struct ipt_counters)); |
| 305 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 306 | |
| 307 | DEBUGP("Setting policy of chain %s to %s\n", |
| 308 | chain, policy); |
| 309 | |
Harald Welte | d8e6563 | 2001-01-05 15:20:07 +0000 | [diff] [blame] | 310 | if (!iptc_set_policy(chain, policy, &count, |
Jan Engelhardt | 1c9015b | 2008-11-10 17:00:41 +0100 | [diff] [blame^] | 311 | handle)) |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 312 | exit_error(OTHER_PROBLEM, |
| 313 | "Can't set policy `%s'" |
| 314 | " on `%s' line %u: %s\n", |
| 315 | chain, policy, line, |
| 316 | iptc_strerror(errno)); |
| 317 | } |
| 318 | |
| 319 | ret = 1; |
| 320 | |
Illes Marci | 26100fa | 2003-03-03 08:05:07 +0000 | [diff] [blame] | 321 | } else if (in_table) { |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 322 | int a; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 323 | char *ptr = buffer; |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 324 | char *pcnt = NULL; |
| 325 | char *bcnt = NULL; |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 326 | char *parsestart; |
| 327 | |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 328 | /* the parser */ |
Pablo Neira Ayuso | b4f3164 | 2007-04-18 10:27:02 +0000 | [diff] [blame] | 329 | char *curchar; |
Max Kellermann | 3bf54df | 2008-01-29 13:45:29 +0000 | [diff] [blame] | 330 | int quote_open, escaped; |
Max Kellermann | b4ef34f | 2008-01-29 13:43:35 +0000 | [diff] [blame] | 331 | size_t param_len; |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 332 | |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 333 | /* reset the newargv */ |
| 334 | newargc = 0; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 335 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 336 | if (buffer[0] == '[') { |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 337 | /* we have counters in our input */ |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 338 | ptr = strchr(buffer, ']'); |
| 339 | if (!ptr) |
| 340 | exit_error(PARAMETER_PROBLEM, |
| 341 | "Bad line %u: need ]\n", |
| 342 | line); |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 343 | |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 344 | pcnt = strtok(buffer+1, ":"); |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 345 | if (!pcnt) |
| 346 | exit_error(PARAMETER_PROBLEM, |
| 347 | "Bad line %u: need :\n", |
| 348 | line); |
| 349 | |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 350 | bcnt = strtok(NULL, "]"); |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 351 | if (!bcnt) |
| 352 | exit_error(PARAMETER_PROBLEM, |
| 353 | "Bad line %u: need ]\n", |
| 354 | line); |
| 355 | |
| 356 | /* start command parsing after counter */ |
| 357 | parsestart = ptr + 1; |
| 358 | } else { |
| 359 | /* start command parsing at start of line */ |
| 360 | parsestart = buffer; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 361 | } |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 362 | |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 363 | add_argv(argv[0]); |
| 364 | add_argv("-t"); |
| 365 | add_argv((char *) &curtable); |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 366 | |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 367 | if (counters && pcnt && bcnt) { |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 368 | add_argv("--set-counters"); |
| 369 | add_argv((char *) pcnt); |
| 370 | add_argv((char *) bcnt); |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 371 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 372 | |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 373 | /* After fighting with strtok enough, here's now |
| 374 | * a 'real' parser. According to Rusty I'm now no |
| 375 | * longer a real hacker, but I can live with that */ |
| 376 | |
| 377 | quote_open = 0; |
Max Kellermann | 3bf54df | 2008-01-29 13:45:29 +0000 | [diff] [blame] | 378 | escaped = 0; |
Pablo Neira Ayuso | b4f3164 | 2007-04-18 10:27:02 +0000 | [diff] [blame] | 379 | param_len = 0; |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 380 | |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 381 | for (curchar = parsestart; *curchar; curchar++) { |
Pablo Neira Ayuso | b4f3164 | 2007-04-18 10:27:02 +0000 | [diff] [blame] | 382 | char param_buffer[1024]; |
| 383 | |
Max Kellermann | 3bf54df | 2008-01-29 13:45:29 +0000 | [diff] [blame] | 384 | if (quote_open) { |
Max Kellermann | fe05c76 | 2008-01-29 13:46:01 +0000 | [diff] [blame] | 385 | if (escaped) { |
| 386 | param_buffer[param_len++] = *curchar; |
| 387 | escaped = 0; |
| 388 | continue; |
| 389 | } else if (*curchar == '\\') { |
Max Kellermann | 3bf54df | 2008-01-29 13:45:29 +0000 | [diff] [blame] | 390 | escaped = 1; |
| 391 | continue; |
| 392 | } else if (*curchar == '"') { |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 393 | quote_open = 0; |
| 394 | *curchar = ' '; |
Max Kellermann | 3bf54df | 2008-01-29 13:45:29 +0000 | [diff] [blame] | 395 | } else { |
| 396 | param_buffer[param_len++] = *curchar; |
| 397 | continue; |
| 398 | } |
| 399 | } else { |
| 400 | if (*curchar == '"') { |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 401 | quote_open = 1; |
Pablo Neira Ayuso | b4f3164 | 2007-04-18 10:27:02 +0000 | [diff] [blame] | 402 | continue; |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 403 | } |
Max Kellermann | 3bf54df | 2008-01-29 13:45:29 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 406 | if (*curchar == ' ' |
| 407 | || *curchar == '\t' |
| 408 | || * curchar == '\n') { |
Harald Welte | 974d010 | 2001-05-26 04:41:56 +0000 | [diff] [blame] | 409 | if (!param_len) { |
| 410 | /* two spaces? */ |
Harald Welte | 974d010 | 2001-05-26 04:41:56 +0000 | [diff] [blame] | 411 | continue; |
| 412 | } |
Pablo Neira Ayuso | b4f3164 | 2007-04-18 10:27:02 +0000 | [diff] [blame] | 413 | |
| 414 | param_buffer[param_len] = '\0'; |
Harald Welte | bb41f88 | 2001-10-21 14:11:54 +0000 | [diff] [blame] | 415 | |
| 416 | /* check if table name specified */ |
Jan Engelhardt | d0cbf5f | 2008-08-04 12:51:01 +0200 | [diff] [blame] | 417 | if (!strncmp(param_buffer, "-t", 2) |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 418 | || !strncmp(param_buffer, "--table", 8)) { |
| 419 | exit_error(PARAMETER_PROBLEM, |
Harald Welte | bb41f88 | 2001-10-21 14:11:54 +0000 | [diff] [blame] | 420 | "Line %u seems to have a " |
| 421 | "-t table option.\n", line); |
| 422 | exit(1); |
| 423 | } |
| 424 | |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 425 | add_argv(param_buffer); |
Pablo Neira Ayuso | b4f3164 | 2007-04-18 10:27:02 +0000 | [diff] [blame] | 426 | param_len = 0; |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 427 | } else { |
Pablo Neira Ayuso | b4f3164 | 2007-04-18 10:27:02 +0000 | [diff] [blame] | 428 | /* regular character, copy to buffer */ |
| 429 | param_buffer[param_len++] = *curchar; |
| 430 | |
| 431 | if (param_len >= sizeof(param_buffer)) |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 432 | exit_error(PARAMETER_PROBLEM, |
Pablo Neira Ayuso | b4f3164 | 2007-04-18 10:27:02 +0000 | [diff] [blame] | 433 | "Parameter too long!"); |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 434 | } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 437 | DEBUGP("calling do_command(%u, argv, &%s, handle):\n", |
| 438 | newargc, curtable); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 439 | |
Harald Welte | fa48fc8 | 2001-10-16 09:51:33 +0000 | [diff] [blame] | 440 | for (a = 0; a < newargc; a++) |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 441 | DEBUGP("argv[%u]: %s\n", a, newargv[a]); |
| 442 | |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 443 | ret = do_command(newargc, newargv, |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 444 | &newargv[2], &handle); |
| 445 | |
| 446 | free_argv(); |
Henrik Nordstrom | d213760 | 2008-05-12 20:51:45 +0200 | [diff] [blame] | 447 | fflush(stdout); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 448 | } |
Peter Warasin | b9cde3a | 2007-11-05 19:35:31 +0000 | [diff] [blame] | 449 | if (tablename && (strcmp(tablename, curtable) != 0)) |
| 450 | continue; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 451 | if (!ret) { |
| 452 | fprintf(stderr, "%s: line %u failed\n", |
| 453 | program_name, line); |
| 454 | exit(1); |
| 455 | } |
| 456 | } |
Martin Josefsson | 3229a7c | 2004-02-01 21:46:04 +0000 | [diff] [blame] | 457 | if (in_table) { |
| 458 | fprintf(stderr, "%s: COMMIT expected at line %u\n", |
| 459 | program_name, line + 1); |
| 460 | exit(1); |
| 461 | } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 462 | |
| 463 | return 0; |
| 464 | } |