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