blob: 438903ece636c3999d2a8d789132f36f9124f5b3 [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
Harald Welteccd49e52001-01-23 22:54:34 +00006 *
Harald Welte26e643f2001-05-03 20:50:03 +00007 * $Id: iptables-restore.c,v 1.10 2001/03/15 15:12:02 laforge Exp $
Harald Welteae1ff9f2000-12-01 14:26:20 +00008 */
9
Marc Bouchere6869a82000-03-20 06:03:29 +000010#include <getopt.h>
11#include <sys/errno.h>
12#include <string.h>
13#include <stdio.h>
Harald Welteae1ff9f2000-12-01 14:26:20 +000014#include <stdlib.h>
15#include "iptables.h"
16#include "libiptc/libiptc.h"
17
18#ifdef DEBUG
Harald Weltec45c3152000-12-06 08:11:24 +000019#define DEBUGP(x, args...) fprintf(stderr, x, ## args)
Harald Welteae1ff9f2000-12-01 14:26:20 +000020#else
Harald Weltec45c3152000-12-06 08:11:24 +000021#define DEBUGP(x, args...)
Harald Welteae1ff9f2000-12-01 14:26:20 +000022#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000023
Harald Weltea9d27082000-12-19 16:10:42 +000024static int binary = 0, counters = 0, verbose = 0, noflush = 0;
25
Marc Bouchere6869a82000-03-20 06:03:29 +000026/* Keeping track of external matches and targets. */
27static struct option options[] = {
Harald Weltea9d27082000-12-19 16:10:42 +000028 { "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 Bouchere6869a82000-03-20 06:03:29 +000033 { 0 }
34};
35
36static void print_usage(const char *name, const char *version) __attribute__((noreturn));
37
38static void print_usage(const char *name, const char *version)
39{
Harald Weltea9d27082000-12-19 16:10:42 +000040 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 Bouchere6869a82000-03-20 06:03:29 +000047 exit(1);
48}
49
Harald Welteae1ff9f2000-12-01 14:26:20 +000050iptc_handle_t create_handle(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +000051{
Harald Welteae1ff9f2000-12-01 14:26:20 +000052 iptc_handle_t handle;
Marc Bouchere6869a82000-03-20 06:03:29 +000053
Harald Welteae1ff9f2000-12-01 14:26:20 +000054 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 Bouchere6869a82000-03-20 06:03:29 +000059 }
Harald Welteae1ff9f2000-12-01 14:26:20 +000060 return handle;
Marc Bouchere6869a82000-03-20 06:03:29 +000061}
62
Harald Welted8e65632001-01-05 15:20:07 +000063int parse_counters(char *string, struct ipt_counters *ctr)
64{
65 return (sscanf(string, "[%llu:%llu]", &ctr->pcnt, &ctr->bcnt) == 2);
66}
Harald Welteae1ff9f2000-12-01 14:26:20 +000067
Harald Welte9f7fa492001-03-15 15:12:02 +000068/* global new argv and argc */
Harald Welte26e643f2001-05-03 20:50:03 +000069static char *newargv[255];
Harald Welte9f7fa492001-03-15 15:12:02 +000070static int newargc;
71
72/* function adding one argument to newargv, updating newargc
73 * returns true if argument added, false otherwise */
74static int add_argv(char *what) {
75 if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) {
Harald Welte26e643f2001-05-03 20:50:03 +000076 newargv[newargc] = strdup(what);
Harald Welte9f7fa492001-03-15 15:12:02 +000077 newargc++;
78 return 1;
79 } else
80 return 0;
81}
82
Harald Welte26e643f2001-05-03 20:50:03 +000083static void free_argv(void) {
84 int i;
85
86 for (i = 0; i < newargc; i++)
87 free(newargv[i]);
88}
89
Marc Bouchere6869a82000-03-20 06:03:29 +000090int main(int argc, char *argv[])
91{
92 iptc_handle_t handle;
93 char buffer[10240];
Marc Bouchere6869a82000-03-20 06:03:29 +000094 unsigned int line = 0;
Harald Weltea9d27082000-12-19 16:10:42 +000095 int c;
Harald Welteae1ff9f2000-12-01 14:26:20 +000096 char curtable[IPT_TABLE_MAXNAMELEN + 1];
Marc Bouchere6869a82000-03-20 06:03:29 +000097 FILE *in;
98
99 program_name = "iptables-restore";
100 program_version = NETFILTER_VERSION;
101
Harald Weltea9d27082000-12-19 16:10:42 +0000102 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 Bouchere6869a82000-03-20 06:03:29 +0000120 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 Welte9f7fa492001-03-15 15:12:02 +0000133
Marc Bouchere6869a82000-03-20 06:03:29 +0000134 /* 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 Welteae1ff9f2000-12-01 14:26:20 +0000143 } else if (strcmp(buffer, "COMMIT\n") == 0) {
144 DEBUGP("Calling commit\n");
Marc Bouchere6869a82000-03-20 06:03:29 +0000145 ret = iptc_commit(&handle);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000146 } 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 Weltea9d27082000-12-19 16:10:42 +0000161 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 Welteae1ff9f2000-12-01 14:26:20 +0000172
173 ret = 1;
174
175 } else if (buffer[0] == ':') {
Marc Bouchere6869a82000-03-20 06:03:29 +0000176 /* New chain. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000177 char *policy, *chain;
Marc Bouchere6869a82000-03-20 06:03:29 +0000178
Marc Bouchere6869a82000-03-20 06:03:29 +0000179 chain = strtok(buffer+1, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000180 DEBUGP("line %u, chain '%s'\n", line, chain);
Marc Bouchere6869a82000-03-20 06:03:29 +0000181 if (!chain) {
182 exit_error(PARAMETER_PROBLEM,
183 "%s: line %u chain name invalid\n",
184 program_name, line);
185 exit(1);
186 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000187
Harald Welte9f7fa492001-03-15 15:12:02 +0000188 if (!iptc_builtin(chain, handle)) {
189 DEBUGP("Creating new chain '%s'\n", chain);
Harald Welte26e643f2001-05-03 20:50:03 +0000190 if (!iptc_create_chain(chain, &handle))
191 exit_error(PARAMETER_PROBLEM,
192 "error creating chain "
193 "'%s':%s\n", chain,
194 strerror(errno));
Harald Welte9f7fa492001-03-15 15:12:02 +0000195 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000196
Marc Bouchere6869a82000-03-20 06:03:29 +0000197 policy = strtok(NULL, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000198 DEBUGP("line %u, policy '%s'\n", line, policy);
Marc Bouchere6869a82000-03-20 06:03:29 +0000199 if (!policy) {
200 exit_error(PARAMETER_PROBLEM,
201 "%s: line %u policy invalid\n",
202 program_name, line);
203 exit(1);
204 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000205
206 if (strcmp(policy, "-") != 0) {
Harald Welted8e65632001-01-05 15:20:07 +0000207 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 Welteae1ff9f2000-12-01 14:26:20 +0000219
220 DEBUGP("Setting policy of chain %s to %s\n",
221 chain, policy);
222
Harald Welted8e65632001-01-05 15:20:07 +0000223 if (!iptc_set_policy(chain, policy, &count,
224 &handle))
Harald Welteae1ff9f2000-12-01 14:26:20 +0000225 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 Bouchere6869a82000-03-20 06:03:29 +0000234 } else {
Harald Welte9f7fa492001-03-15 15:12:02 +0000235 int a;
Marc Bouchere6869a82000-03-20 06:03:29 +0000236 char *ptr = buffer;
Harald Welteccd49e52001-01-23 22:54:34 +0000237 char *pcnt = NULL;
238 char *bcnt = NULL;
Harald Welte9f7fa492001-03-15 15:12:02 +0000239 char *parsestart;
240
Harald Welte26e643f2001-05-03 20:50:03 +0000241 /* the parser */
242 char *param_start, *curchar;
243 int quote_open;
244
Harald Welte9f7fa492001-03-15 15:12:02 +0000245 /* reset the newargv */
246 newargc = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000247
Marc Bouchere6869a82000-03-20 06:03:29 +0000248 if (buffer[0] == '[') {
Harald Welte9f7fa492001-03-15 15:12:02 +0000249 /* we have counters in our input */
Marc Bouchere6869a82000-03-20 06:03:29 +0000250 ptr = strchr(buffer, ']');
251 if (!ptr)
252 exit_error(PARAMETER_PROBLEM,
253 "Bad line %u: need ]\n",
254 line);
Harald Welte9f7fa492001-03-15 15:12:02 +0000255
Harald Welteccd49e52001-01-23 22:54:34 +0000256 pcnt = strtok(buffer+1, ":");
Harald Welte9f7fa492001-03-15 15:12:02 +0000257 if (!pcnt)
258 exit_error(PARAMETER_PROBLEM,
259 "Bad line %u: need :\n",
260 line);
261
Harald Welteccd49e52001-01-23 22:54:34 +0000262 bcnt = strtok(NULL, "]");
Harald Welte9f7fa492001-03-15 15:12:02 +0000263 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 Bouchere6869a82000-03-20 06:03:29 +0000273 }
Rusty Russell7e53bf92000-03-20 07:03:28 +0000274
Harald Welte9f7fa492001-03-15 15:12:02 +0000275 add_argv(argv[0]);
276 add_argv("-t");
277 add_argv((char *) &curtable);
278
Harald Welteccd49e52001-01-23 22:54:34 +0000279 if (counters && pcnt && bcnt) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000280 add_argv("--set-counters");
281 add_argv((char *) pcnt);
282 add_argv((char *) bcnt);
Harald Welteccd49e52001-01-23 22:54:34 +0000283 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000284
Harald Welte26e643f2001-05-03 20:50:03 +0000285 /* 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 Bouchere6869a82000-03-20 06:03:29 +0000323 }
324
Harald Welte26e643f2001-05-03 20:50:03 +0000325 DEBUGP("calling do_command(%u, argv, &%s, handle):\n",
326 newargc, curtable);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000327
Harald Welte9f7fa492001-03-15 15:12:02 +0000328 for (a = 0; a <= newargc; a++)
Harald Welteae1ff9f2000-12-01 14:26:20 +0000329 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
330
Harald Welte26e643f2001-05-03 20:50:03 +0000331 ret = do_command(newargc, newargv,
332 &newargv[2], &handle);
333
334 free_argv();
Marc Bouchere6869a82000-03-20 06:03:29 +0000335 }
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}