lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Code to restore the iptables state, from file by iptables-save. |
| 2 | * (C) 2000-2002 by Harald Welte <laforge@gnumonks.org> |
| 3 | * based on previous code from Rusty Russell <rusty@linuxcare.com.au> |
| 4 | * |
| 5 | * This code is distributed under the terms of GNU GPL v2 |
| 6 | * |
| 7 | * $Id$ |
| 8 | */ |
| 9 | |
| 10 | #include <getopt.h> |
| 11 | #include <sys/errno.h> |
| 12 | #include <stdbool.h> |
| 13 | #include <string.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include "iptables.h" |
| 17 | #include "xtables.h" |
| 18 | #include "libiptc/libiptc.h" |
| 19 | #include "iptables-multi.h" |
| 20 | |
| 21 | #ifdef DEBUG |
| 22 | #define DEBUGP(x, args...) fprintf(stderr, x, ## args) |
| 23 | #else |
| 24 | #define DEBUGP(x, args...) |
| 25 | #endif |
| 26 | |
| 27 | static int binary = 0, counters = 0, verbose = 0, noflush = 0; |
| 28 | |
| 29 | /* Keeping track of external matches and targets. */ |
| 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}, |
| 40 | }; |
| 41 | |
| 42 | static void print_usage(const char *name, const char *version) __attribute__((noreturn)); |
| 43 | |
| 44 | #define prog_name iptables_globals.program_name |
| 45 | |
| 46 | static void print_usage(const char *name, const char *version) |
| 47 | { |
| 48 | fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-t] [-h]\n" |
| 49 | " [ --binary ]\n" |
| 50 | " [ --counters ]\n" |
| 51 | " [ --verbose ]\n" |
| 52 | " [ --test ]\n" |
| 53 | " [ --help ]\n" |
| 54 | " [ --noflush ]\n" |
| 55 | " [ --table=<TABLE> ]\n" |
| 56 | " [ --modprobe=<command>]\n", name); |
| 57 | |
| 58 | exit(1); |
| 59 | } |
| 60 | |
| 61 | static struct iptc_handle *create_handle(const char *tablename) |
| 62 | { |
| 63 | struct iptc_handle *handle; |
| 64 | |
| 65 | handle = iptc_init(tablename); |
| 66 | |
| 67 | if (!handle) { |
| 68 | /* try to insmod the module if iptc_init failed */ |
| 69 | xtables_load_ko(xtables_modprobe_program, false); |
| 70 | handle = iptc_init(tablename); |
| 71 | } |
| 72 | |
| 73 | if (!handle) { |
| 74 | xtables_error(PARAMETER_PROBLEM, "%s: unable to initialize " |
| 75 | "table '%s'\n", prog_name, tablename); |
| 76 | exit(1); |
| 77 | } |
| 78 | return handle; |
| 79 | } |
| 80 | |
| 81 | static int parse_counters(char *string, struct ipt_counters *ctr) |
| 82 | { |
| 83 | unsigned long long pcnt, bcnt; |
| 84 | int ret; |
| 85 | |
| 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; |
| 92 | } |
| 93 | |
| 94 | /* global new argv and argc */ |
| 95 | static char *newargv[255]; |
| 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) { |
| 101 | DEBUGP("add_argv: %s\n", what); |
| 102 | if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) { |
| 103 | newargv[newargc] = strdup(what); |
| 104 | newargc++; |
| 105 | return 1; |
| 106 | } else |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static void free_argv(void) { |
| 111 | int i; |
| 112 | |
| 113 | for (i = 0; i < newargc; i++) |
| 114 | free(newargv[i]); |
| 115 | } |
| 116 | |
| 117 | #ifdef IPTABLES_MULTI |
| 118 | int |
| 119 | iptables_restore_main(int argc, char *argv[]) |
| 120 | #else |
| 121 | int |
| 122 | main(int argc, char *argv[]) |
| 123 | #endif |
| 124 | { |
| 125 | struct iptc_handle *handle = NULL; |
| 126 | char buffer[10240]; |
| 127 | int c; |
| 128 | char curtable[IPT_TABLE_MAXNAMELEN + 1]; |
| 129 | FILE *in; |
| 130 | int in_table = 0, testing = 0; |
| 131 | const char *tablename = NULL; |
| 132 | |
| 133 | line = 0; |
| 134 | |
| 135 | iptables_globals.program_name = "iptables-restore"; |
| 136 | c = xtables_init_all(&iptables_globals, NFPROTO_IPV4); |
| 137 | if (c < 0) { |
| 138 | fprintf(stderr, "%s/%s Failed to initialize xtables\n", |
| 139 | iptables_globals.program_name, |
| 140 | iptables_globals.program_version); |
| 141 | exit(1); |
| 142 | } |
| 143 | #ifdef NO_SHARED_LIBS |
| 144 | init_extensions(); |
| 145 | #endif |
| 146 | |
| 147 | while ((c = getopt_long(argc, argv, "bcvthnM:T:", options, NULL)) != -1) { |
| 148 | switch (c) { |
| 149 | case 'b': |
| 150 | binary = 1; |
| 151 | break; |
| 152 | case 'c': |
| 153 | counters = 1; |
| 154 | break; |
| 155 | case 'v': |
| 156 | verbose = 1; |
| 157 | break; |
| 158 | case 't': |
| 159 | testing = 1; |
| 160 | break; |
| 161 | case 'h': |
| 162 | print_usage("iptables-restore", |
| 163 | IPTABLES_VERSION); |
| 164 | break; |
| 165 | case 'n': |
| 166 | noflush = 1; |
| 167 | break; |
| 168 | case 'M': |
| 169 | xtables_modprobe_program = optarg; |
| 170 | break; |
| 171 | case 'T': |
| 172 | tablename = optarg; |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (optind == argc - 1) { |
| 178 | in = fopen(argv[optind], "r"); |
| 179 | if (!in) { |
| 180 | fprintf(stderr, "Can't open %s: %s\n", argv[optind], |
| 181 | strerror(errno)); |
| 182 | exit(1); |
| 183 | } |
| 184 | } |
| 185 | else if (optind < argc) { |
| 186 | fprintf(stderr, "Unknown arguments found on commandline\n"); |
| 187 | exit(1); |
| 188 | } |
| 189 | else in = stdin; |
| 190 | |
| 191 | /* Grab standard input. */ |
| 192 | while (fgets(buffer, sizeof(buffer), in)) { |
| 193 | int ret = 0; |
| 194 | |
| 195 | line++; |
| 196 | if (buffer[0] == '\n') |
| 197 | continue; |
| 198 | else if (buffer[0] == '#') { |
| 199 | if (verbose) |
| 200 | fputs(buffer, stdout); |
| 201 | continue; |
| 202 | } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) { |
| 203 | if (!testing) { |
| 204 | DEBUGP("Calling commit\n"); |
| 205 | ret = iptc_commit(handle); |
| 206 | iptc_free(handle); |
| 207 | handle = NULL; |
| 208 | } else { |
| 209 | DEBUGP("Not calling commit, testing\n"); |
| 210 | ret = 1; |
| 211 | } |
| 212 | in_table = 0; |
| 213 | } else if ((buffer[0] == '*') && (!in_table)) { |
| 214 | /* New table */ |
| 215 | char *table; |
| 216 | |
| 217 | table = strtok(buffer+1, " \t\n"); |
| 218 | DEBUGP("line %u, table '%s'\n", line, table); |
| 219 | if (!table) { |
| 220 | xtables_error(PARAMETER_PROBLEM, |
| 221 | "%s: line %u table name invalid\n", |
| 222 | prog_name, line); |
| 223 | exit(1); |
| 224 | } |
| 225 | strncpy(curtable, table, IPT_TABLE_MAXNAMELEN); |
| 226 | curtable[IPT_TABLE_MAXNAMELEN] = '\0'; |
| 227 | |
| 228 | if (tablename && (strcmp(tablename, table) != 0)) |
| 229 | continue; |
| 230 | if (handle) |
| 231 | iptc_free(handle); |
| 232 | |
| 233 | handle = create_handle(table); |
| 234 | if (noflush == 0) { |
| 235 | DEBUGP("Cleaning all chains of table '%s'\n", |
| 236 | table); |
| 237 | for_each_chain(flush_entries, verbose, 1, |
| 238 | handle); |
| 239 | |
| 240 | DEBUGP("Deleting all user-defined chains " |
| 241 | "of table '%s'\n", table); |
| 242 | for_each_chain(delete_chain, verbose, 0, |
| 243 | handle); |
| 244 | } |
| 245 | |
| 246 | ret = 1; |
| 247 | in_table = 1; |
| 248 | |
| 249 | } else if ((buffer[0] == ':') && (in_table)) { |
| 250 | /* New chain. */ |
| 251 | char *policy, *chain; |
| 252 | |
| 253 | chain = strtok(buffer+1, " \t\n"); |
| 254 | DEBUGP("line %u, chain '%s'\n", line, chain); |
| 255 | if (!chain) { |
| 256 | xtables_error(PARAMETER_PROBLEM, |
| 257 | "%s: line %u chain name invalid\n", |
| 258 | prog_name, line); |
| 259 | exit(1); |
| 260 | } |
| 261 | |
| 262 | if (iptc_builtin(chain, handle) <= 0) { |
| 263 | if (noflush && iptc_is_chain(chain, handle)) { |
| 264 | DEBUGP("Flushing existing user defined chain '%s'\n", chain); |
| 265 | if (!iptc_flush_entries(chain, handle)) |
| 266 | xtables_error(PARAMETER_PROBLEM, |
| 267 | "error flushing chain " |
| 268 | "'%s':%s\n", chain, |
| 269 | strerror(errno)); |
| 270 | } else { |
| 271 | DEBUGP("Creating new chain '%s'\n", chain); |
| 272 | if (!iptc_create_chain(chain, handle)) |
| 273 | xtables_error(PARAMETER_PROBLEM, |
| 274 | "error creating chain " |
| 275 | "'%s':%s\n", chain, |
| 276 | strerror(errno)); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | policy = strtok(NULL, " \t\n"); |
| 281 | DEBUGP("line %u, policy '%s'\n", line, policy); |
| 282 | if (!policy) { |
| 283 | xtables_error(PARAMETER_PROBLEM, |
| 284 | "%s: line %u policy invalid\n", |
| 285 | prog_name, line); |
| 286 | exit(1); |
| 287 | } |
| 288 | |
| 289 | if (strcmp(policy, "-") != 0) { |
| 290 | struct ipt_counters count; |
| 291 | |
| 292 | if (counters) { |
| 293 | char *ctrs; |
| 294 | ctrs = strtok(NULL, " \t\n"); |
| 295 | |
| 296 | if (!ctrs || !parse_counters(ctrs, &count)) |
| 297 | xtables_error(PARAMETER_PROBLEM, |
| 298 | "invalid policy counters " |
| 299 | "for chain '%s'\n", chain); |
| 300 | |
| 301 | } else { |
| 302 | memset(&count, 0, |
| 303 | sizeof(struct ipt_counters)); |
| 304 | } |
| 305 | |
| 306 | DEBUGP("Setting policy of chain %s to %s\n", |
| 307 | chain, policy); |
| 308 | |
| 309 | if (!iptc_set_policy(chain, policy, &count, |
| 310 | handle)) |
| 311 | xtables_error(OTHER_PROBLEM, |
| 312 | "Can't set policy `%s'" |
| 313 | " on `%s' line %u: %s\n", |
| 314 | chain, policy, line, |
| 315 | iptc_strerror(errno)); |
| 316 | } |
| 317 | |
| 318 | ret = 1; |
| 319 | |
| 320 | } else if (in_table) { |
| 321 | int a; |
| 322 | char *ptr = buffer; |
| 323 | char *pcnt = NULL; |
| 324 | char *bcnt = NULL; |
| 325 | char *parsestart; |
| 326 | |
| 327 | /* the parser */ |
| 328 | char *curchar; |
| 329 | int quote_open, escaped; |
| 330 | size_t param_len; |
| 331 | |
| 332 | /* reset the newargv */ |
| 333 | newargc = 0; |
| 334 | |
| 335 | if (buffer[0] == '[') { |
| 336 | /* we have counters in our input */ |
| 337 | ptr = strchr(buffer, ']'); |
| 338 | if (!ptr) |
| 339 | xtables_error(PARAMETER_PROBLEM, |
| 340 | "Bad line %u: need ]\n", |
| 341 | line); |
| 342 | |
| 343 | pcnt = strtok(buffer+1, ":"); |
| 344 | if (!pcnt) |
| 345 | xtables_error(PARAMETER_PROBLEM, |
| 346 | "Bad line %u: need :\n", |
| 347 | line); |
| 348 | |
| 349 | bcnt = strtok(NULL, "]"); |
| 350 | if (!bcnt) |
| 351 | xtables_error(PARAMETER_PROBLEM, |
| 352 | "Bad line %u: need ]\n", |
| 353 | line); |
| 354 | |
| 355 | /* start command parsing after counter */ |
| 356 | parsestart = ptr + 1; |
| 357 | } else { |
| 358 | /* start command parsing at start of line */ |
| 359 | parsestart = buffer; |
| 360 | } |
| 361 | |
| 362 | add_argv(argv[0]); |
| 363 | add_argv("-t"); |
| 364 | add_argv((char *) &curtable); |
| 365 | |
| 366 | if (counters && pcnt && bcnt) { |
| 367 | add_argv("--set-counters"); |
| 368 | add_argv((char *) pcnt); |
| 369 | add_argv((char *) bcnt); |
| 370 | } |
| 371 | |
| 372 | /* After fighting with strtok enough, here's now |
| 373 | * a 'real' parser. According to Rusty I'm now no |
| 374 | * longer a real hacker, but I can live with that */ |
| 375 | |
| 376 | quote_open = 0; |
| 377 | escaped = 0; |
| 378 | param_len = 0; |
| 379 | |
| 380 | for (curchar = parsestart; *curchar; curchar++) { |
| 381 | char param_buffer[1024]; |
| 382 | |
| 383 | if (quote_open) { |
| 384 | if (escaped) { |
| 385 | param_buffer[param_len++] = *curchar; |
| 386 | escaped = 0; |
| 387 | continue; |
| 388 | } else if (*curchar == '\\') { |
| 389 | escaped = 1; |
| 390 | continue; |
| 391 | } else if (*curchar == '"') { |
| 392 | quote_open = 0; |
| 393 | *curchar = ' '; |
| 394 | } else { |
| 395 | param_buffer[param_len++] = *curchar; |
| 396 | continue; |
| 397 | } |
| 398 | } else { |
| 399 | if (*curchar == '"') { |
| 400 | quote_open = 1; |
| 401 | continue; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | if (*curchar == ' ' |
| 406 | || *curchar == '\t' |
| 407 | || * curchar == '\n') { |
| 408 | if (!param_len) { |
| 409 | /* two spaces? */ |
| 410 | continue; |
| 411 | } |
| 412 | |
| 413 | param_buffer[param_len] = '\0'; |
| 414 | |
| 415 | /* check if table name specified */ |
| 416 | if (!strncmp(param_buffer, "-t", 2) |
| 417 | || !strncmp(param_buffer, "--table", 8)) { |
| 418 | xtables_error(PARAMETER_PROBLEM, |
| 419 | "Line %u seems to have a " |
| 420 | "-t table option.\n", line); |
| 421 | exit(1); |
| 422 | } |
| 423 | |
| 424 | add_argv(param_buffer); |
| 425 | param_len = 0; |
| 426 | } else { |
| 427 | /* regular character, copy to buffer */ |
| 428 | param_buffer[param_len++] = *curchar; |
| 429 | |
| 430 | if (param_len >= sizeof(param_buffer)) |
| 431 | xtables_error(PARAMETER_PROBLEM, |
| 432 | "Parameter too long!"); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | DEBUGP("calling do_command(%u, argv, &%s, handle):\n", |
| 437 | newargc, curtable); |
| 438 | |
| 439 | for (a = 0; a < newargc; a++) |
| 440 | DEBUGP("argv[%u]: %s\n", a, newargv[a]); |
| 441 | |
| 442 | ret = do_command(newargc, newargv, |
| 443 | &newargv[2], &handle); |
| 444 | |
| 445 | free_argv(); |
| 446 | fflush(stdout); |
| 447 | } |
| 448 | if (tablename && (strcmp(tablename, curtable) != 0)) |
| 449 | continue; |
| 450 | if (!ret) { |
| 451 | fprintf(stderr, "%s: line %u failed\n", |
| 452 | prog_name, line); |
| 453 | exit(1); |
| 454 | } |
| 455 | } |
| 456 | if (in_table) { |
| 457 | fprintf(stderr, "%s: COMMIT expected at line %u\n", |
| 458 | prog_name, line + 1); |
| 459 | exit(1); |
| 460 | } |
| 461 | |
| 462 | return 0; |
| 463 | } |