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