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 |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 6 | * |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 7 | * $Id: iptables-restore.c,v 1.10 2001/03/15 15:12:02 laforge Exp $ |
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" |
| 16 | #include "libiptc/libiptc.h" |
| 17 | |
| 18 | #ifdef DEBUG |
Harald Welte | c45c315 | 2000-12-06 08:11:24 +0000 | [diff] [blame] | 19 | #define DEBUGP(x, args...) fprintf(stderr, x, ## args) |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 20 | #else |
Harald Welte | c45c315 | 2000-12-06 08:11:24 +0000 | [diff] [blame] | 21 | #define DEBUGP(x, args...) |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 22 | #endif |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 23 | |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame] | 24 | static int binary = 0, counters = 0, verbose = 0, noflush = 0; |
| 25 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 26 | /* Keeping track of external matches and targets. */ |
| 27 | static struct option options[] = { |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame] | 28 | { "binary", 0, 0, 'b' }, |
| 29 | { "counters", 0, 0, 'c' }, |
| 30 | /* { "verbose", 1, 0, 'v' }, */ |
| 31 | { "help", 0, 0, 'h' }, |
| 32 | { "noflush", 0, 0, 'n'}, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 33 | { 0 } |
| 34 | }; |
| 35 | |
| 36 | static void print_usage(const char *name, const char *version) __attribute__((noreturn)); |
| 37 | |
| 38 | static void print_usage(const char *name, const char *version) |
| 39 | { |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame] | 40 | fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-h]\n" |
| 41 | " [ --binary ]\n" |
| 42 | " [ --counters ]\n" |
| 43 | " [ --verbose ]\n" |
| 44 | " [ --help ]\n" |
| 45 | " [ --noflush ]\n", name); |
| 46 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 47 | exit(1); |
| 48 | } |
| 49 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 50 | iptc_handle_t create_handle(const char *tablename) |
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 | iptc_handle_t handle; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 53 | |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 54 | handle = iptc_init(tablename); |
| 55 | if (!handle) { |
| 56 | exit_error(PARAMETER_PROBLEM, "%s: unable to initialize" |
| 57 | "table '%s'\n", program_name, tablename); |
| 58 | exit(1); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 59 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 60 | return handle; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Harald Welte | d8e6563 | 2001-01-05 15:20:07 +0000 | [diff] [blame] | 63 | int parse_counters(char *string, struct ipt_counters *ctr) |
| 64 | { |
| 65 | return (sscanf(string, "[%llu:%llu]", &ctr->pcnt, &ctr->bcnt) == 2); |
| 66 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 67 | |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 68 | /* global new argv and argc */ |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 69 | static char *newargv[255]; |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 70 | static int newargc; |
| 71 | |
| 72 | /* function adding one argument to newargv, updating newargc |
| 73 | * returns true if argument added, false otherwise */ |
| 74 | static int add_argv(char *what) { |
| 75 | if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) { |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 76 | newargv[newargc] = strdup(what); |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 77 | newargc++; |
| 78 | return 1; |
| 79 | } else |
| 80 | return 0; |
| 81 | } |
| 82 | |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 83 | static void free_argv(void) { |
| 84 | int i; |
| 85 | |
| 86 | for (i = 0; i < newargc; i++) |
| 87 | free(newargv[i]); |
| 88 | } |
| 89 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 90 | int main(int argc, char *argv[]) |
| 91 | { |
| 92 | iptc_handle_t handle; |
| 93 | char buffer[10240]; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 94 | unsigned int line = 0; |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame] | 95 | int c; |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 96 | char curtable[IPT_TABLE_MAXNAMELEN + 1]; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 97 | FILE *in; |
| 98 | |
| 99 | program_name = "iptables-restore"; |
| 100 | program_version = NETFILTER_VERSION; |
| 101 | |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame] | 102 | while ((c = getopt_long(argc, argv, "bcvhn", options, NULL)) != -1) { |
| 103 | switch (c) { |
| 104 | case 'b': |
| 105 | binary = 1; |
| 106 | break; |
| 107 | case 'c': |
| 108 | counters = 1; |
| 109 | break; |
| 110 | case 'h': |
| 111 | print_usage("iptables-restore", |
| 112 | NETFILTER_VERSION); |
| 113 | break; |
| 114 | case 'n': |
| 115 | noflush = 1; |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 120 | if (optind == argc - 1) { |
| 121 | in = fopen(argv[optind], "r"); |
| 122 | if (!in) { |
| 123 | fprintf(stderr, "Can't open %s: %s", argv[optind], |
| 124 | strerror(errno)); |
| 125 | exit(1); |
| 126 | } |
| 127 | } |
| 128 | else if (optind < argc) { |
| 129 | fprintf(stderr, "Unknown arguments found on commandline"); |
| 130 | exit(1); |
| 131 | } |
| 132 | else in = stdin; |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 133 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 134 | /* Grab standard input. */ |
| 135 | while (fgets(buffer, sizeof(buffer), in)) { |
| 136 | int ret; |
| 137 | |
| 138 | line++; |
| 139 | if (buffer[0] == '\n') continue; |
| 140 | else if (buffer[0] == '#') { |
| 141 | if (verbose) fputs(buffer, stdout); |
| 142 | continue; |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 143 | } else if (strcmp(buffer, "COMMIT\n") == 0) { |
| 144 | DEBUGP("Calling commit\n"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 145 | ret = iptc_commit(&handle); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 146 | } else if (buffer[0] == '*') { |
| 147 | /* New table */ |
| 148 | char *table; |
| 149 | |
| 150 | table = strtok(buffer+1, " \t\n"); |
| 151 | DEBUGP("line %u, table '%s'\n", line, table); |
| 152 | if (!table) { |
| 153 | exit_error(PARAMETER_PROBLEM, |
| 154 | "%s: line %u table name invalid\n", |
| 155 | program_name, line); |
| 156 | exit(1); |
| 157 | } |
| 158 | strncpy(curtable, table, IPT_TABLE_MAXNAMELEN); |
| 159 | |
| 160 | handle = create_handle(table); |
Harald Welte | a9d2708 | 2000-12-19 16:10:42 +0000 | [diff] [blame] | 161 | if (noflush == 0) { |
| 162 | DEBUGP("Cleaning all chains of table '%s'\n", |
| 163 | table); |
| 164 | for_each_chain(flush_entries, verbose, 1, |
| 165 | &handle); |
| 166 | |
| 167 | DEBUGP("Deleting all user-defined chains " |
| 168 | "of table '%s'\n", table); |
| 169 | for_each_chain(delete_chain, verbose, 0, |
| 170 | &handle) ; |
| 171 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 172 | |
| 173 | ret = 1; |
| 174 | |
| 175 | } else if (buffer[0] == ':') { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 176 | /* New chain. */ |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 177 | char *policy, *chain; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 178 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 179 | chain = strtok(buffer+1, " \t\n"); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 180 | DEBUGP("line %u, chain '%s'\n", line, chain); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 181 | if (!chain) { |
| 182 | exit_error(PARAMETER_PROBLEM, |
| 183 | "%s: line %u chain name invalid\n", |
| 184 | program_name, line); |
| 185 | exit(1); |
| 186 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 187 | |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 188 | if (!iptc_builtin(chain, handle)) { |
| 189 | DEBUGP("Creating new chain '%s'\n", chain); |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 190 | if (!iptc_create_chain(chain, &handle)) |
| 191 | exit_error(PARAMETER_PROBLEM, |
| 192 | "error creating chain " |
| 193 | "'%s':%s\n", chain, |
| 194 | strerror(errno)); |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 195 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 196 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 197 | policy = strtok(NULL, " \t\n"); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 198 | DEBUGP("line %u, policy '%s'\n", line, policy); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 199 | if (!policy) { |
| 200 | exit_error(PARAMETER_PROBLEM, |
| 201 | "%s: line %u policy invalid\n", |
| 202 | program_name, line); |
| 203 | exit(1); |
| 204 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 205 | |
| 206 | if (strcmp(policy, "-") != 0) { |
Harald Welte | d8e6563 | 2001-01-05 15:20:07 +0000 | [diff] [blame] | 207 | struct ipt_counters count; |
| 208 | |
| 209 | if (counters) { |
| 210 | char *ctrs; |
| 211 | ctrs = strtok(NULL, " \t\n"); |
| 212 | |
| 213 | parse_counters(ctrs, &count); |
| 214 | |
| 215 | } else { |
| 216 | memset(&count, 0, |
| 217 | sizeof(struct ipt_counters)); |
| 218 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 219 | |
| 220 | DEBUGP("Setting policy of chain %s to %s\n", |
| 221 | chain, policy); |
| 222 | |
Harald Welte | d8e6563 | 2001-01-05 15:20:07 +0000 | [diff] [blame] | 223 | if (!iptc_set_policy(chain, policy, &count, |
| 224 | &handle)) |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 225 | exit_error(OTHER_PROBLEM, |
| 226 | "Can't set policy `%s'" |
| 227 | " on `%s' line %u: %s\n", |
| 228 | chain, policy, line, |
| 229 | iptc_strerror(errno)); |
| 230 | } |
| 231 | |
| 232 | ret = 1; |
| 233 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 234 | } else { |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 235 | int a; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 236 | char *ptr = buffer; |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 237 | char *pcnt = NULL; |
| 238 | char *bcnt = NULL; |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 239 | char *parsestart; |
| 240 | |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 241 | /* the parser */ |
| 242 | char *param_start, *curchar; |
| 243 | int quote_open; |
| 244 | |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 245 | /* reset the newargv */ |
| 246 | newargc = 0; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 247 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 248 | if (buffer[0] == '[') { |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 249 | /* we have counters in our input */ |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 250 | ptr = strchr(buffer, ']'); |
| 251 | if (!ptr) |
| 252 | exit_error(PARAMETER_PROBLEM, |
| 253 | "Bad line %u: need ]\n", |
| 254 | line); |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 255 | |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 256 | pcnt = strtok(buffer+1, ":"); |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 257 | if (!pcnt) |
| 258 | exit_error(PARAMETER_PROBLEM, |
| 259 | "Bad line %u: need :\n", |
| 260 | line); |
| 261 | |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 262 | bcnt = strtok(NULL, "]"); |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 263 | if (!bcnt) |
| 264 | exit_error(PARAMETER_PROBLEM, |
| 265 | "Bad line %u: need ]\n", |
| 266 | line); |
| 267 | |
| 268 | /* start command parsing after counter */ |
| 269 | parsestart = ptr + 1; |
| 270 | } else { |
| 271 | /* start command parsing at start of line */ |
| 272 | parsestart = buffer; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 273 | } |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 274 | |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 275 | add_argv(argv[0]); |
| 276 | add_argv("-t"); |
| 277 | add_argv((char *) &curtable); |
| 278 | |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 279 | if (counters && pcnt && bcnt) { |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 280 | add_argv("--set-counters"); |
| 281 | add_argv((char *) pcnt); |
| 282 | add_argv((char *) bcnt); |
Harald Welte | ccd49e5 | 2001-01-23 22:54:34 +0000 | [diff] [blame] | 283 | } |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 284 | |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 285 | /* After fighting with strtok enough, here's now |
| 286 | * a 'real' parser. According to Rusty I'm now no |
| 287 | * longer a real hacker, but I can live with that */ |
| 288 | |
| 289 | quote_open = 0; |
| 290 | param_start = parsestart; |
| 291 | |
| 292 | for (curchar = parsestart; *curchar; curchar++) { |
| 293 | if (*curchar == '"') { |
| 294 | if (quote_open) { |
| 295 | quote_open = 0; |
| 296 | *curchar = ' '; |
| 297 | } else { |
| 298 | quote_open = 1; |
| 299 | param_start++; |
| 300 | } |
| 301 | } |
| 302 | if (*curchar == ' ' |
| 303 | || *curchar == '\t' |
| 304 | || * curchar == '\n') { |
| 305 | char param_buffer[1024]; |
| 306 | int param_len = curchar-param_start; |
| 307 | |
| 308 | if (quote_open) |
| 309 | continue; |
| 310 | |
| 311 | if (!param_len) |
| 312 | break; |
| 313 | |
| 314 | /* end of one parameter */ |
| 315 | strncpy(param_buffer, param_start, |
| 316 | param_len); |
| 317 | *(param_buffer+param_len) = '\0'; |
| 318 | add_argv(param_buffer); |
| 319 | param_start += param_len + 1; |
| 320 | } else { |
| 321 | /* regular character, skip */ |
| 322 | } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 325 | DEBUGP("calling do_command(%u, argv, &%s, handle):\n", |
| 326 | newargc, curtable); |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 327 | |
Harald Welte | 9f7fa49 | 2001-03-15 15:12:02 +0000 | [diff] [blame] | 328 | for (a = 0; a <= newargc; a++) |
Harald Welte | ae1ff9f | 2000-12-01 14:26:20 +0000 | [diff] [blame] | 329 | DEBUGP("argv[%u]: %s\n", a, newargv[a]); |
| 330 | |
Harald Welte | 26e643f | 2001-05-03 20:50:03 +0000 | [diff] [blame] | 331 | ret = do_command(newargc, newargv, |
| 332 | &newargv[2], &handle); |
| 333 | |
| 334 | free_argv(); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 335 | } |
| 336 | if (!ret) { |
| 337 | fprintf(stderr, "%s: line %u failed\n", |
| 338 | program_name, line); |
| 339 | exit(1); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | return 0; |
| 344 | } |