blob: 27ea4e3c680fe5eb650d50dcd506ef69a0d8a3f0 [file] [log] [blame]
Harald Welteae1ff9f2000-12-01 14:26:20 +00001/* 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 Bouchere6869a82000-03-20 06:03:29 +00008#include <getopt.h>
9#include <sys/errno.h>
10#include <string.h>
11#include <stdio.h>
Harald Welteae1ff9f2000-12-01 14:26:20 +000012#include <stdlib.h>
13#include "iptables.h"
14#include "libiptc/libiptc.h"
15
16#ifdef DEBUG
Harald Weltec45c3152000-12-06 08:11:24 +000017#define DEBUGP(x, args...) fprintf(stderr, x, ## args)
Harald Welteae1ff9f2000-12-01 14:26:20 +000018#else
Harald Weltec45c3152000-12-06 08:11:24 +000019#define DEBUGP(x, args...)
Harald Welteae1ff9f2000-12-01 14:26:20 +000020#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000021
Harald Weltea9d27082000-12-19 16:10:42 +000022static int binary = 0, counters = 0, verbose = 0, noflush = 0;
23
Marc Bouchere6869a82000-03-20 06:03:29 +000024/* Keeping track of external matches and targets. */
25static struct option options[] = {
Harald Weltea9d27082000-12-19 16:10:42 +000026 { "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 Bouchere6869a82000-03-20 06:03:29 +000031 { 0 }
32};
33
34static void print_usage(const char *name, const char *version) __attribute__((noreturn));
35
36static void print_usage(const char *name, const char *version)
37{
Harald Weltea9d27082000-12-19 16:10:42 +000038 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 Bouchere6869a82000-03-20 06:03:29 +000045 exit(1);
46}
47
Harald Welteae1ff9f2000-12-01 14:26:20 +000048iptc_handle_t create_handle(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +000049{
Harald Welteae1ff9f2000-12-01 14:26:20 +000050 iptc_handle_t handle;
Marc Bouchere6869a82000-03-20 06:03:29 +000051
Harald Welteae1ff9f2000-12-01 14:26:20 +000052 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 Bouchere6869a82000-03-20 06:03:29 +000057 }
Harald Welteae1ff9f2000-12-01 14:26:20 +000058 return handle;
Marc Bouchere6869a82000-03-20 06:03:29 +000059}
60
Harald Welteae1ff9f2000-12-01 14:26:20 +000061
Marc Bouchere6869a82000-03-20 06:03:29 +000062int main(int argc, char *argv[])
63{
64 iptc_handle_t handle;
65 char buffer[10240];
Marc Bouchere6869a82000-03-20 06:03:29 +000066 unsigned int line = 0;
Harald Weltea9d27082000-12-19 16:10:42 +000067 int c;
Harald Welteae1ff9f2000-12-01 14:26:20 +000068 char curtable[IPT_TABLE_MAXNAMELEN + 1];
69 char curchain[IPT_FUNCTION_MAXNAMELEN + 1];
Marc Bouchere6869a82000-03-20 06:03:29 +000070 FILE *in;
71
72 program_name = "iptables-restore";
73 program_version = NETFILTER_VERSION;
74
Harald Weltea9d27082000-12-19 16:10:42 +000075 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 Bouchere6869a82000-03-20 06:03:29 +000093 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 Welteae1ff9f2000-12-01 14:26:20 +0000106/*
107 handle = iptc_init("filter");
Marc Bouchere6869a82000-03-20 06:03:29 +0000108 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 Welteae1ff9f2000-12-01 14:26:20 +0000116*/
Marc Bouchere6869a82000-03-20 06:03:29 +0000117 /* 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 Welteae1ff9f2000-12-01 14:26:20 +0000126 } else if (strcmp(buffer, "COMMIT\n") == 0) {
127 DEBUGP("Calling commit\n");
Marc Bouchere6869a82000-03-20 06:03:29 +0000128 ret = iptc_commit(&handle);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000129 } 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 Weltea9d27082000-12-19 16:10:42 +0000144 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 Welteae1ff9f2000-12-01 14:26:20 +0000155
156 ret = 1;
157
158 } else if (buffer[0] == ':') {
Marc Bouchere6869a82000-03-20 06:03:29 +0000159 /* New chain. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000160 char *policy, *chain;
Marc Bouchere6869a82000-03-20 06:03:29 +0000161
162 /* FIXME: Don't ignore counters. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000163
Marc Bouchere6869a82000-03-20 06:03:29 +0000164 chain = strtok(buffer+1, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000165 DEBUGP("line %u, chain '%s'\n", line, chain);
Marc Bouchere6869a82000-03-20 06:03:29 +0000166 if (!chain) {
167 exit_error(PARAMETER_PROBLEM,
168 "%s: line %u chain name invalid\n",
169 program_name, line);
170 exit(1);
171 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000172 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 Bouchere6869a82000-03-20 06:03:29 +0000182 policy = strtok(NULL, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000183 DEBUGP("line %u, policy '%s'\n", line, policy);
Marc Bouchere6869a82000-03-20 06:03:29 +0000184 if (!policy) {
185 exit_error(PARAMETER_PROBLEM,
186 "%s: line %u policy invalid\n",
187 program_name, line);
188 exit(1);
189 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000190
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 Bouchere6869a82000-03-20 06:03:29 +0000206 } else {
207 char *newargv[1024];
Harald Welteae1ff9f2000-12-01 14:26:20 +0000208 int i,a;
Marc Bouchere6869a82000-03-20 06:03:29 +0000209 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 Russell7e53bf92000-03-20 07:03:28 +0000219
Marc Bouchere6869a82000-03-20 06:03:29 +0000220 newargv[0] = argv[0];
Harald Welteae1ff9f2000-12-01 14:26:20 +0000221 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 Bouchere6869a82000-03-20 06:03:29 +0000228 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 Welteae1ff9f2000-12-01 14:26:20 +0000239 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 Bouchere6869a82000-03-20 06:03:29 +0000246 }
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}