blob: 6ccf0b546356cb0495bcc26037638b8335ad5377 [file] [log] [blame]
Harald Welteae1ff9f2000-12-01 14:26:20 +00001/* Code to restore the iptables state, from file by iptables-save.
Harald Welte10a907f2002-08-07 09:07:41 +00002 * (C) 2000-2002 by Harald Welte <laforge@gnumonks.org>
Harald Welteae1ff9f2000-12-01 14:26:20 +00003 * based on previous code from Rusty Russell <rusty@linuxcare.com.au>
4 *
Harald Welte10a907f2002-08-07 09:07:41 +00005 * This code is distributed under the terms of GNU GPL v2
Harald Welteccd49e52001-01-23 22:54:34 +00006 *
Martin Josefsson3673dcb2004-01-31 19:41:49 +00007 * $Id: iptables-restore.c,v 1.30 2004/01/31 19:33:47 gandalf 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' },
Martin Josefssonec789712004-01-31 19:28:13 +000030 { "verbose", 0, 0, 'v' },
Harald Weltea9d27082000-12-19 16:10:42 +000031 { "help", 0, 0, 'h' },
32 { "noflush", 0, 0, 'n'},
Harald Welte58918652001-06-16 18:25:25 +000033 { "modprobe", 1, 0, 'M'},
Marc Bouchere6869a82000-03-20 06:03:29 +000034 { 0 }
35};
36
37static void print_usage(const char *name, const char *version) __attribute__((noreturn));
38
39static void print_usage(const char *name, const char *version)
40{
Harald Weltea9d27082000-12-19 16:10:42 +000041 fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-h]\n"
42 " [ --binary ]\n"
43 " [ --counters ]\n"
44 " [ --verbose ]\n"
45 " [ --help ]\n"
Harald Welte58918652001-06-16 18:25:25 +000046 " [ --noflush ]\n"
47 " [ --modprobe=<command>]\n", name);
Harald Weltea9d27082000-12-19 16:10:42 +000048
Marc Bouchere6869a82000-03-20 06:03:29 +000049 exit(1);
50}
51
Harald Welte58918652001-06-16 18:25:25 +000052iptc_handle_t create_handle(const char *tablename, const char* modprobe )
Marc Bouchere6869a82000-03-20 06:03:29 +000053{
Harald Welteae1ff9f2000-12-01 14:26:20 +000054 iptc_handle_t handle;
Marc Bouchere6869a82000-03-20 06:03:29 +000055
Harald Welteae1ff9f2000-12-01 14:26:20 +000056 handle = iptc_init(tablename);
Harald Welte58918652001-06-16 18:25:25 +000057
58 if (!handle) {
59 /* try to insmod the module if iptc_init failed */
60 iptables_insmod("ip_tables", modprobe);
61 handle = iptc_init(tablename);
62 }
63
Harald Welteae1ff9f2000-12-01 14:26:20 +000064 if (!handle) {
65 exit_error(PARAMETER_PROBLEM, "%s: unable to initialize"
66 "table '%s'\n", program_name, tablename);
67 exit(1);
Marc Bouchere6869a82000-03-20 06:03:29 +000068 }
Harald Welteae1ff9f2000-12-01 14:26:20 +000069 return handle;
Marc Bouchere6869a82000-03-20 06:03:29 +000070}
71
Harald Welted8e65632001-01-05 15:20:07 +000072int parse_counters(char *string, struct ipt_counters *ctr)
73{
74 return (sscanf(string, "[%llu:%llu]", &ctr->pcnt, &ctr->bcnt) == 2);
75}
Harald Welteae1ff9f2000-12-01 14:26:20 +000076
Harald Welte9f7fa492001-03-15 15:12:02 +000077/* global new argv and argc */
Harald Welte26e643f2001-05-03 20:50:03 +000078static char *newargv[255];
Harald Welte9f7fa492001-03-15 15:12:02 +000079static int newargc;
80
81/* function adding one argument to newargv, updating newargc
82 * returns true if argument added, false otherwise */
83static int add_argv(char *what) {
Harald Weltebb41f882001-10-21 14:11:54 +000084 DEBUGP("add_argv: %s\n", what);
Harald Welte9f7fa492001-03-15 15:12:02 +000085 if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) {
Harald Welte26e643f2001-05-03 20:50:03 +000086 newargv[newargc] = strdup(what);
Harald Welte9f7fa492001-03-15 15:12:02 +000087 newargc++;
88 return 1;
89 } else
90 return 0;
91}
92
Harald Welte26e643f2001-05-03 20:50:03 +000093static void free_argv(void) {
94 int i;
95
96 for (i = 0; i < newargc; i++)
97 free(newargv[i]);
98}
99
Marc Bouchere6869a82000-03-20 06:03:29 +0000100int main(int argc, char *argv[])
101{
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000102 iptc_handle_t handle = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000103 char buffer[10240];
Harald Weltea9d27082000-12-19 16:10:42 +0000104 int c;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000105 char curtable[IPT_TABLE_MAXNAMELEN + 1];
Marc Bouchere6869a82000-03-20 06:03:29 +0000106 FILE *in;
Harald Welte58918652001-06-16 18:25:25 +0000107 const char *modprobe = 0;
Illes Marci26100fa2003-03-03 08:05:07 +0000108 int in_table = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000109
110 program_name = "iptables-restore";
Harald Welte80fe35d2002-05-29 13:08:15 +0000111 program_version = IPTABLES_VERSION;
Illes Marci63e90632003-03-03 08:08:37 +0000112 line = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000113
Harald Welte3efb6ea2001-08-06 18:50:21 +0000114#ifdef NO_SHARED_LIBS
115 init_extensions();
116#endif
117
Harald Welte58918652001-06-16 18:25:25 +0000118 while ((c = getopt_long(argc, argv, "bcvhnM:", options, NULL)) != -1) {
Harald Weltea9d27082000-12-19 16:10:42 +0000119 switch (c) {
120 case 'b':
121 binary = 1;
122 break;
123 case 'c':
124 counters = 1;
125 break;
Marc Boucher1277b592001-12-06 15:06:34 +0000126 case 'v':
127 verbose = 1;
128 break;
Harald Weltea9d27082000-12-19 16:10:42 +0000129 case 'h':
130 print_usage("iptables-restore",
Harald Welte80fe35d2002-05-29 13:08:15 +0000131 IPTABLES_VERSION);
Harald Weltea9d27082000-12-19 16:10:42 +0000132 break;
133 case 'n':
134 noflush = 1;
135 break;
Harald Welte58918652001-06-16 18:25:25 +0000136 case 'M':
137 modprobe = optarg;
138 break;
Harald Weltea9d27082000-12-19 16:10:42 +0000139 }
140 }
141
Marc Bouchere6869a82000-03-20 06:03:29 +0000142 if (optind == argc - 1) {
143 in = fopen(argv[optind], "r");
144 if (!in) {
145 fprintf(stderr, "Can't open %s: %s", argv[optind],
146 strerror(errno));
147 exit(1);
148 }
149 }
150 else if (optind < argc) {
151 fprintf(stderr, "Unknown arguments found on commandline");
152 exit(1);
153 }
154 else in = stdin;
Harald Welte9f7fa492001-03-15 15:12:02 +0000155
Marc Bouchere6869a82000-03-20 06:03:29 +0000156 /* Grab standard input. */
157 while (fgets(buffer, sizeof(buffer), in)) {
Harald Welteb3f22192003-03-06 11:56:31 +0000158 int ret = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000159
160 line++;
Martin Josefsson5065afa2004-01-31 19:33:47 +0000161 if (buffer[0] == '\n')
162 continue;
Marc Bouchere6869a82000-03-20 06:03:29 +0000163 else if (buffer[0] == '#') {
Martin Josefsson3673dcb2004-01-31 19:41:49 +0000164 if (verbose)
165 fputs(buffer, stdout);
Marc Bouchere6869a82000-03-20 06:03:29 +0000166 continue;
Illes Marci26100fa2003-03-03 08:05:07 +0000167 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
Harald Welteae1ff9f2000-12-01 14:26:20 +0000168 DEBUGP("Calling commit\n");
Marc Bouchere6869a82000-03-20 06:03:29 +0000169 ret = iptc_commit(&handle);
Illes Marci26100fa2003-03-03 08:05:07 +0000170 in_table = 0;
Harald Welteb3f22192003-03-06 11:56:31 +0000171 } else if ((buffer[0] == '*') && (!in_table)) {
Harald Welteae1ff9f2000-12-01 14:26:20 +0000172 /* New table */
173 char *table;
174
175 table = strtok(buffer+1, " \t\n");
176 DEBUGP("line %u, table '%s'\n", line, table);
177 if (!table) {
178 exit_error(PARAMETER_PROBLEM,
179 "%s: line %u table name invalid\n",
180 program_name, line);
181 exit(1);
182 }
183 strncpy(curtable, table, IPT_TABLE_MAXNAMELEN);
Karsten Desler073df8f2004-01-31 15:33:55 +0000184 curtable[IPT_TABLE_MAXNAMELEN] = '\0';
Harald Welteae1ff9f2000-12-01 14:26:20 +0000185
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000186 if (handle)
187 iptc_free(&handle);
188
Harald Welte58918652001-06-16 18:25:25 +0000189 handle = create_handle(table, modprobe);
Harald Weltea9d27082000-12-19 16:10:42 +0000190 if (noflush == 0) {
191 DEBUGP("Cleaning all chains of table '%s'\n",
192 table);
193 for_each_chain(flush_entries, verbose, 1,
194 &handle);
195
196 DEBUGP("Deleting all user-defined chains "
197 "of table '%s'\n", table);
198 for_each_chain(delete_chain, verbose, 0,
199 &handle) ;
200 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000201
202 ret = 1;
Illes Marci26100fa2003-03-03 08:05:07 +0000203 in_table = 1;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000204
Illes Marci26100fa2003-03-03 08:05:07 +0000205 } else if ((buffer[0] == ':') && (in_table)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000206 /* New chain. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000207 char *policy, *chain;
Marc Bouchere6869a82000-03-20 06:03:29 +0000208
Marc Bouchere6869a82000-03-20 06:03:29 +0000209 chain = strtok(buffer+1, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000210 DEBUGP("line %u, chain '%s'\n", line, chain);
Marc Bouchere6869a82000-03-20 06:03:29 +0000211 if (!chain) {
212 exit_error(PARAMETER_PROBLEM,
213 "%s: line %u chain name invalid\n",
214 program_name, line);
215 exit(1);
216 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000217
Harald Welte9f7fa492001-03-15 15:12:02 +0000218 if (!iptc_builtin(chain, handle)) {
219 DEBUGP("Creating new chain '%s'\n", chain);
Harald Welte26e643f2001-05-03 20:50:03 +0000220 if (!iptc_create_chain(chain, &handle))
221 exit_error(PARAMETER_PROBLEM,
222 "error creating chain "
223 "'%s':%s\n", chain,
224 strerror(errno));
Harald Welte9f7fa492001-03-15 15:12:02 +0000225 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000226
Marc Bouchere6869a82000-03-20 06:03:29 +0000227 policy = strtok(NULL, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000228 DEBUGP("line %u, policy '%s'\n", line, policy);
Marc Bouchere6869a82000-03-20 06:03:29 +0000229 if (!policy) {
230 exit_error(PARAMETER_PROBLEM,
231 "%s: line %u policy invalid\n",
232 program_name, line);
233 exit(1);
234 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000235
236 if (strcmp(policy, "-") != 0) {
Harald Welted8e65632001-01-05 15:20:07 +0000237 struct ipt_counters count;
238
239 if (counters) {
240 char *ctrs;
241 ctrs = strtok(NULL, " \t\n");
242
243 parse_counters(ctrs, &count);
244
245 } else {
246 memset(&count, 0,
247 sizeof(struct ipt_counters));
248 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000249
250 DEBUGP("Setting policy of chain %s to %s\n",
251 chain, policy);
252
Harald Welted8e65632001-01-05 15:20:07 +0000253 if (!iptc_set_policy(chain, policy, &count,
254 &handle))
Harald Welteae1ff9f2000-12-01 14:26:20 +0000255 exit_error(OTHER_PROBLEM,
256 "Can't set policy `%s'"
257 " on `%s' line %u: %s\n",
258 chain, policy, line,
259 iptc_strerror(errno));
260 }
261
262 ret = 1;
263
Illes Marci26100fa2003-03-03 08:05:07 +0000264 } else if (in_table) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000265 int a;
Marc Bouchere6869a82000-03-20 06:03:29 +0000266 char *ptr = buffer;
Harald Welteccd49e52001-01-23 22:54:34 +0000267 char *pcnt = NULL;
268 char *bcnt = NULL;
Harald Welte9f7fa492001-03-15 15:12:02 +0000269 char *parsestart;
270
Harald Welte26e643f2001-05-03 20:50:03 +0000271 /* the parser */
272 char *param_start, *curchar;
273 int quote_open;
274
Harald Welte9f7fa492001-03-15 15:12:02 +0000275 /* reset the newargv */
276 newargc = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000277
Marc Bouchere6869a82000-03-20 06:03:29 +0000278 if (buffer[0] == '[') {
Harald Welte9f7fa492001-03-15 15:12:02 +0000279 /* we have counters in our input */
Marc Bouchere6869a82000-03-20 06:03:29 +0000280 ptr = strchr(buffer, ']');
281 if (!ptr)
282 exit_error(PARAMETER_PROBLEM,
283 "Bad line %u: need ]\n",
284 line);
Harald Welte9f7fa492001-03-15 15:12:02 +0000285
Harald Welteccd49e52001-01-23 22:54:34 +0000286 pcnt = strtok(buffer+1, ":");
Harald Welte9f7fa492001-03-15 15:12:02 +0000287 if (!pcnt)
288 exit_error(PARAMETER_PROBLEM,
289 "Bad line %u: need :\n",
290 line);
291
Harald Welteccd49e52001-01-23 22:54:34 +0000292 bcnt = strtok(NULL, "]");
Harald Welte9f7fa492001-03-15 15:12:02 +0000293 if (!bcnt)
294 exit_error(PARAMETER_PROBLEM,
295 "Bad line %u: need ]\n",
296 line);
297
298 /* start command parsing after counter */
299 parsestart = ptr + 1;
300 } else {
301 /* start command parsing at start of line */
302 parsestart = buffer;
Marc Bouchere6869a82000-03-20 06:03:29 +0000303 }
Rusty Russell7e53bf92000-03-20 07:03:28 +0000304
Harald Welte9f7fa492001-03-15 15:12:02 +0000305 add_argv(argv[0]);
306 add_argv("-t");
307 add_argv((char *) &curtable);
308
Harald Welteccd49e52001-01-23 22:54:34 +0000309 if (counters && pcnt && bcnt) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000310 add_argv("--set-counters");
311 add_argv((char *) pcnt);
312 add_argv((char *) bcnt);
Harald Welteccd49e52001-01-23 22:54:34 +0000313 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000314
Harald Welte26e643f2001-05-03 20:50:03 +0000315 /* After fighting with strtok enough, here's now
316 * a 'real' parser. According to Rusty I'm now no
317 * longer a real hacker, but I can live with that */
318
319 quote_open = 0;
320 param_start = parsestart;
321
322 for (curchar = parsestart; *curchar; curchar++) {
323 if (*curchar == '"') {
Michael Rash29cdbd02004-01-05 09:41:50 +0000324 /* quote_open cannot be true if there
325 * was no previous character. Thus,
326 * curchar-1 has to be within bounds */
327 if (quote_open &&
328 *(curchar-1) != '\\') {
Harald Welte26e643f2001-05-03 20:50:03 +0000329 quote_open = 0;
330 *curchar = ' ';
331 } else {
332 quote_open = 1;
333 param_start++;
334 }
335 }
336 if (*curchar == ' '
337 || *curchar == '\t'
338 || * curchar == '\n') {
339 char param_buffer[1024];
340 int param_len = curchar-param_start;
341
342 if (quote_open)
343 continue;
344
Harald Welte974d0102001-05-26 04:41:56 +0000345 if (!param_len) {
346 /* two spaces? */
347 param_start++;
348 continue;
349 }
Harald Welte26e643f2001-05-03 20:50:03 +0000350
351 /* end of one parameter */
352 strncpy(param_buffer, param_start,
353 param_len);
354 *(param_buffer+param_len) = '\0';
Harald Weltebb41f882001-10-21 14:11:54 +0000355
356 /* check if table name specified */
Harald Welte48a6f902001-10-22 15:16:21 +0000357 if (!strncmp(param_buffer, "-t", 3)
358 || !strncmp(param_buffer, "--table", 8)) {
Harald Weltebb41f882001-10-21 14:11:54 +0000359 exit_error(PARAMETER_PROBLEM,
360 "Line %u seems to have a "
361 "-t table option.\n", line);
362 exit(1);
363 }
364
Harald Welte26e643f2001-05-03 20:50:03 +0000365 add_argv(param_buffer);
366 param_start += param_len + 1;
367 } else {
368 /* regular character, skip */
369 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000370 }
371
Harald Welte26e643f2001-05-03 20:50:03 +0000372 DEBUGP("calling do_command(%u, argv, &%s, handle):\n",
373 newargc, curtable);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000374
Harald Weltefa48fc82001-10-16 09:51:33 +0000375 for (a = 0; a < newargc; a++)
Harald Welteae1ff9f2000-12-01 14:26:20 +0000376 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
377
Harald Welte26e643f2001-05-03 20:50:03 +0000378 ret = do_command(newargc, newargv,
379 &newargv[2], &handle);
380
381 free_argv();
Marc Bouchere6869a82000-03-20 06:03:29 +0000382 }
383 if (!ret) {
384 fprintf(stderr, "%s: line %u failed\n",
385 program_name, line);
386 exit(1);
387 }
388 }
389
390 return 0;
391}