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