Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 1 | /* Code to restore the iptables state, from file by iptables-save. |
| 2 | * (C) 2000 by Harald Welte <laforge@gnumonks.org> |
| 3 | * based on previous code from Rusty Russell <rusty@linuxcare.com.au> |
| 4 | * |
| 5 | * This coude is distributed under the terms of GNU GPL |
| 6 | */ |
| 7 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 8 | #include <getopt.h> |
| 9 | #include <sys/errno.h> |
| 10 | #include <string.h> |
| 11 | #include <stdio.h> |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 12 | #include <stdlib.h> |
| 13 | #include "iptables.h" |
| 14 | #include "libiptc/libiptc.h" |
| 15 | |
| 16 | #ifdef DEBUG |
Harald Welte | c45c315 | 2000-12-06 08:11:24 +0000 | [diff] [blame] | 17 | #define DEBUGP(x, args...) fprintf(stderr, x, ## args) |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 18 | #else |
Harald Welte | c45c315 | 2000-12-06 08:11:24 +0000 | [diff] [blame] | 19 | #define DEBUGP(x, args...) |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 20 | #endif |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 21 | |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame^] | 22 | static int binary = 0, counters = 0, verbose = 0, noflush = 0; |
| 23 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 24 | /* Keeping track of external matches and targets. */ |
| 25 | static struct option options[] = { |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame^] | 26 | { "binary", 0, 0, 'b' }, |
| 27 | { "counters", 0, 0, 'c' }, |
| 28 | /* { "verbose", 1, 0, 'v' }, */ |
| 29 | { "help", 0, 0, 'h' }, |
| 30 | { "noflush", 0, 0, 'n'}, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 31 | { 0 } |
| 32 | }; |
| 33 | |
| 34 | static void print_usage(const char *name, const char *version) __attribute__((noreturn)); |
| 35 | |
| 36 | static void print_usage(const char *name, const char *version) |
| 37 | { |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame^] | 38 | fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-h]\n" |
| 39 | " [ --binary ]\n" |
| 40 | " [ --counters ]\n" |
| 41 | " [ --verbose ]\n" |
| 42 | " [ --help ]\n" |
| 43 | " [ --noflush ]\n", name); |
| 44 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 45 | exit(1); |
| 46 | } |
| 47 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 48 | iptc_handle_t create_handle(const char *tablename) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 49 | { |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 50 | iptc_handle_t handle; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 51 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 52 | handle = iptc_init(tablename); |
| 53 | if (!handle) { |
| 54 | exit_error(PARAMETER_PROBLEM, "%s: unable to initialize" |
| 55 | "table '%s'\n", program_name, tablename); |
| 56 | exit(1); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 57 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 58 | return handle; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 61 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 62 | int main(int argc, char *argv[]) |
| 63 | { |
| 64 | iptc_handle_t handle; |
| 65 | char buffer[10240]; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 66 | unsigned int line = 0; |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame^] | 67 | int c; |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 68 | char curtable[IPT_TABLE_MAXNAMELEN + 1]; |
| 69 | char curchain[IPT_FUNCTION_MAXNAMELEN + 1]; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 70 | FILE *in; |
| 71 | |
| 72 | program_name = "iptables-restore"; |
| 73 | program_version = NETFILTER_VERSION; |
| 74 | |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame^] | 75 | while ((c = getopt_long(argc, argv, "bcvhn", options, NULL)) != -1) { |
| 76 | switch (c) { |
| 77 | case 'b': |
| 78 | binary = 1; |
| 79 | break; |
| 80 | case 'c': |
| 81 | counters = 1; |
| 82 | break; |
| 83 | case 'h': |
| 84 | print_usage("iptables-restore", |
| 85 | NETFILTER_VERSION); |
| 86 | break; |
| 87 | case 'n': |
| 88 | noflush = 1; |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 93 | if (optind == argc - 1) { |
| 94 | in = fopen(argv[optind], "r"); |
| 95 | if (!in) { |
| 96 | fprintf(stderr, "Can't open %s: %s", argv[optind], |
| 97 | strerror(errno)); |
| 98 | exit(1); |
| 99 | } |
| 100 | } |
| 101 | else if (optind < argc) { |
| 102 | fprintf(stderr, "Unknown arguments found on commandline"); |
| 103 | exit(1); |
| 104 | } |
| 105 | else in = stdin; |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 106 | /* |
| 107 | handle = iptc_init("filter"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 108 | if (!handle) |
| 109 | exit_error(VERSION_PROBLEM, |
| 110 | "can't initialize iptables-restore: %s", |
| 111 | iptc_strerror(errno)); |
| 112 | |
| 113 | if (!clean_slate(&handle)) |
| 114 | exit_error(OTHER_PROBLEM, "Deleting old chains: %s", |
| 115 | iptc_strerror(errno)); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 116 | */ |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 117 | /* Grab standard input. */ |
| 118 | while (fgets(buffer, sizeof(buffer), in)) { |
| 119 | int ret; |
| 120 | |
| 121 | line++; |
| 122 | if (buffer[0] == '\n') continue; |
| 123 | else if (buffer[0] == '#') { |
| 124 | if (verbose) fputs(buffer, stdout); |
| 125 | continue; |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 126 | } else if (strcmp(buffer, "COMMIT\n") == 0) { |
| 127 | DEBUGP("Calling commit\n"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 128 | ret = iptc_commit(&handle); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 129 | } else if (buffer[0] == '*') { |
| 130 | /* New table */ |
| 131 | char *table; |
| 132 | |
| 133 | table = strtok(buffer+1, " \t\n"); |
| 134 | DEBUGP("line %u, table '%s'\n", line, table); |
| 135 | if (!table) { |
| 136 | exit_error(PARAMETER_PROBLEM, |
| 137 | "%s: line %u table name invalid\n", |
| 138 | program_name, line); |
| 139 | exit(1); |
| 140 | } |
| 141 | strncpy(curtable, table, IPT_TABLE_MAXNAMELEN); |
| 142 | |
| 143 | handle = create_handle(table); |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame^] | 144 | if (noflush == 0) { |
| 145 | DEBUGP("Cleaning all chains of table '%s'\n", |
| 146 | table); |
| 147 | for_each_chain(flush_entries, verbose, 1, |
| 148 | &handle); |
| 149 | |
| 150 | DEBUGP("Deleting all user-defined chains " |
| 151 | "of table '%s'\n", table); |
| 152 | for_each_chain(delete_chain, verbose, 0, |
| 153 | &handle) ; |
| 154 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 155 | |
| 156 | ret = 1; |
| 157 | |
| 158 | } else if (buffer[0] == ':') { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 159 | /* New chain. */ |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 160 | char *policy, *chain; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 161 | |
| 162 | /* FIXME: Don't ignore counters. */ |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 163 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 164 | chain = strtok(buffer+1, " \t\n"); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 165 | DEBUGP("line %u, chain '%s'\n", line, chain); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 166 | if (!chain) { |
| 167 | exit_error(PARAMETER_PROBLEM, |
| 168 | "%s: line %u chain name invalid\n", |
| 169 | program_name, line); |
| 170 | exit(1); |
| 171 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 172 | strncpy(curchain, chain, IPT_FUNCTION_MAXNAMELEN); |
| 173 | |
| 174 | /* why the f... does iptc_builtin not work here ? */ |
| 175 | // if (!iptc_builtin(curchain, &handle)) { |
| 176 | DEBUGP("Creating new chain '%s'\n", curchain); |
| 177 | if (!iptc_create_chain(curchain, &handle)) |
| 178 | DEBUGP("unable to create chain '%s':%s\n", curchain, |
| 179 | strerror(errno)); |
| 180 | // } |
| 181 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 182 | policy = strtok(NULL, " \t\n"); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 183 | DEBUGP("line %u, policy '%s'\n", line, policy); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 184 | if (!policy) { |
| 185 | exit_error(PARAMETER_PROBLEM, |
| 186 | "%s: line %u policy invalid\n", |
| 187 | program_name, line); |
| 188 | exit(1); |
| 189 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 190 | |
| 191 | if (strcmp(policy, "-") != 0) { |
| 192 | |
| 193 | DEBUGP("Setting policy of chain %s to %s\n", |
| 194 | chain, policy); |
| 195 | |
| 196 | if (!iptc_set_policy(chain, policy, &handle)) |
| 197 | exit_error(OTHER_PROBLEM, |
| 198 | "Can't set policy `%s'" |
| 199 | " on `%s' line %u: %s\n", |
| 200 | chain, policy, line, |
| 201 | iptc_strerror(errno)); |
| 202 | } |
| 203 | |
| 204 | ret = 1; |
| 205 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 206 | } else { |
| 207 | char *newargv[1024]; |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 208 | int i,a; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 209 | char *ptr = buffer; |
| 210 | |
| 211 | /* FIXME: Don't ignore counters. */ |
| 212 | if (buffer[0] == '[') { |
| 213 | ptr = strchr(buffer, ']'); |
| 214 | if (!ptr) |
| 215 | exit_error(PARAMETER_PROBLEM, |
| 216 | "Bad line %u: need ]\n", |
| 217 | line); |
| 218 | } |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 219 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 220 | newargv[0] = argv[0]; |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 221 | newargv[1] = "-t"; |
| 222 | newargv[2] = (char *) &curtable; |
| 223 | newargv[3] = "-A"; |
| 224 | newargv[4] = (char *) &curchain; |
| 225 | |
| 226 | /* strtok: a function only a coder could love */ |
| 227 | for (i = 5; i < sizeof(newargv)/sizeof(char *); i++) { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 228 | if (!(newargv[i] = strtok(ptr, " \t\n"))) |
| 229 | break; |
| 230 | ptr = NULL; |
| 231 | } |
| 232 | if (i == sizeof(newargv)/sizeof(char *)) { |
| 233 | fprintf(stderr, |
| 234 | "%s: line %u too many arguments\n", |
| 235 | program_name, line); |
| 236 | exit(1); |
| 237 | } |
| 238 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 239 | DEBUGP("===>calling do_command(%u, argv, &%s, handle):\n", |
| 240 | i, curtable); |
| 241 | |
| 242 | for (a = 0; a <= i; a++) |
| 243 | DEBUGP("argv[%u]: %s\n", a, newargv[a]); |
| 244 | |
| 245 | ret = do_command(i, newargv, &newargv[2], &handle); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 246 | } |
| 247 | if (!ret) { |
| 248 | fprintf(stderr, "%s: line %u failed\n", |
| 249 | program_name, line); |
| 250 | exit(1); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | return 0; |
| 255 | } |