blob: 2a18a0950a49b8c8ac4d8e0b3b9bea40e650b70c [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 Weltebb41f882001-10-21 14:11:54 +00007 * $Id: iptables-restore.c,v 1.16 2001/10/16 09:51:33 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'},
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{
102 iptc_handle_t handle;
103 char buffer[10240];
Marc Bouchere6869a82000-03-20 06:03:29 +0000104 unsigned int line = 0;
Harald Weltea9d27082000-12-19 16:10:42 +0000105 int c;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000106 char curtable[IPT_TABLE_MAXNAMELEN + 1];
Marc Bouchere6869a82000-03-20 06:03:29 +0000107 FILE *in;
Harald Welte58918652001-06-16 18:25:25 +0000108 const char *modprobe = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000109
110 program_name = "iptables-restore";
111 program_version = NETFILTER_VERSION;
112
Harald Welte3efb6ea2001-08-06 18:50:21 +0000113#ifdef NO_SHARED_LIBS
114 init_extensions();
115#endif
116
Harald Welte58918652001-06-16 18:25:25 +0000117 while ((c = getopt_long(argc, argv, "bcvhnM:", options, NULL)) != -1) {
Harald Weltea9d27082000-12-19 16:10:42 +0000118 switch (c) {
119 case 'b':
120 binary = 1;
121 break;
122 case 'c':
123 counters = 1;
124 break;
125 case 'h':
126 print_usage("iptables-restore",
127 NETFILTER_VERSION);
128 break;
129 case 'n':
130 noflush = 1;
131 break;
Harald Welte58918652001-06-16 18:25:25 +0000132 case 'M':
133 modprobe = optarg;
134 break;
Harald Weltea9d27082000-12-19 16:10:42 +0000135 }
136 }
137
Marc Bouchere6869a82000-03-20 06:03:29 +0000138 if (optind == argc - 1) {
139 in = fopen(argv[optind], "r");
140 if (!in) {
141 fprintf(stderr, "Can't open %s: %s", argv[optind],
142 strerror(errno));
143 exit(1);
144 }
145 }
146 else if (optind < argc) {
147 fprintf(stderr, "Unknown arguments found on commandline");
148 exit(1);
149 }
150 else in = stdin;
Harald Welte9f7fa492001-03-15 15:12:02 +0000151
Marc Bouchere6869a82000-03-20 06:03:29 +0000152 /* Grab standard input. */
153 while (fgets(buffer, sizeof(buffer), in)) {
154 int ret;
155
156 line++;
157 if (buffer[0] == '\n') continue;
158 else if (buffer[0] == '#') {
159 if (verbose) fputs(buffer, stdout);
160 continue;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000161 } else if (strcmp(buffer, "COMMIT\n") == 0) {
162 DEBUGP("Calling commit\n");
Marc Bouchere6869a82000-03-20 06:03:29 +0000163 ret = iptc_commit(&handle);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000164 } else if (buffer[0] == '*') {
165 /* New table */
166 char *table;
167
168 table = strtok(buffer+1, " \t\n");
169 DEBUGP("line %u, table '%s'\n", line, table);
170 if (!table) {
171 exit_error(PARAMETER_PROBLEM,
172 "%s: line %u table name invalid\n",
173 program_name, line);
174 exit(1);
175 }
176 strncpy(curtable, table, IPT_TABLE_MAXNAMELEN);
177
Harald Welte58918652001-06-16 18:25:25 +0000178 handle = create_handle(table, modprobe);
Harald Weltea9d27082000-12-19 16:10:42 +0000179 if (noflush == 0) {
180 DEBUGP("Cleaning all chains of table '%s'\n",
181 table);
182 for_each_chain(flush_entries, verbose, 1,
183 &handle);
184
185 DEBUGP("Deleting all user-defined chains "
186 "of table '%s'\n", table);
187 for_each_chain(delete_chain, verbose, 0,
188 &handle) ;
189 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000190
191 ret = 1;
192
193 } else if (buffer[0] == ':') {
Marc Bouchere6869a82000-03-20 06:03:29 +0000194 /* New chain. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000195 char *policy, *chain;
Marc Bouchere6869a82000-03-20 06:03:29 +0000196
Marc Bouchere6869a82000-03-20 06:03:29 +0000197 chain = strtok(buffer+1, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000198 DEBUGP("line %u, chain '%s'\n", line, chain);
Marc Bouchere6869a82000-03-20 06:03:29 +0000199 if (!chain) {
200 exit_error(PARAMETER_PROBLEM,
201 "%s: line %u chain name invalid\n",
202 program_name, line);
203 exit(1);
204 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000205
Harald Welte9f7fa492001-03-15 15:12:02 +0000206 if (!iptc_builtin(chain, handle)) {
207 DEBUGP("Creating new chain '%s'\n", chain);
Harald Welte26e643f2001-05-03 20:50:03 +0000208 if (!iptc_create_chain(chain, &handle))
209 exit_error(PARAMETER_PROBLEM,
210 "error creating chain "
211 "'%s':%s\n", chain,
212 strerror(errno));
Harald Welte9f7fa492001-03-15 15:12:02 +0000213 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000214
Marc Bouchere6869a82000-03-20 06:03:29 +0000215 policy = strtok(NULL, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000216 DEBUGP("line %u, policy '%s'\n", line, policy);
Marc Bouchere6869a82000-03-20 06:03:29 +0000217 if (!policy) {
218 exit_error(PARAMETER_PROBLEM,
219 "%s: line %u policy invalid\n",
220 program_name, line);
221 exit(1);
222 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000223
224 if (strcmp(policy, "-") != 0) {
Harald Welted8e65632001-01-05 15:20:07 +0000225 struct ipt_counters count;
226
227 if (counters) {
228 char *ctrs;
229 ctrs = strtok(NULL, " \t\n");
230
231 parse_counters(ctrs, &count);
232
233 } else {
234 memset(&count, 0,
235 sizeof(struct ipt_counters));
236 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000237
238 DEBUGP("Setting policy of chain %s to %s\n",
239 chain, policy);
240
Harald Welted8e65632001-01-05 15:20:07 +0000241 if (!iptc_set_policy(chain, policy, &count,
242 &handle))
Harald Welteae1ff9f2000-12-01 14:26:20 +0000243 exit_error(OTHER_PROBLEM,
244 "Can't set policy `%s'"
245 " on `%s' line %u: %s\n",
246 chain, policy, line,
247 iptc_strerror(errno));
248 }
249
250 ret = 1;
251
Marc Bouchere6869a82000-03-20 06:03:29 +0000252 } else {
Harald Welte9f7fa492001-03-15 15:12:02 +0000253 int a;
Marc Bouchere6869a82000-03-20 06:03:29 +0000254 char *ptr = buffer;
Harald Welteccd49e52001-01-23 22:54:34 +0000255 char *pcnt = NULL;
256 char *bcnt = NULL;
Harald Welte9f7fa492001-03-15 15:12:02 +0000257 char *parsestart;
258
Harald Welte26e643f2001-05-03 20:50:03 +0000259 /* the parser */
260 char *param_start, *curchar;
261 int quote_open;
262
Harald Welte9f7fa492001-03-15 15:12:02 +0000263 /* reset the newargv */
264 newargc = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000265
Marc Bouchere6869a82000-03-20 06:03:29 +0000266 if (buffer[0] == '[') {
Harald Welte9f7fa492001-03-15 15:12:02 +0000267 /* we have counters in our input */
Marc Bouchere6869a82000-03-20 06:03:29 +0000268 ptr = strchr(buffer, ']');
269 if (!ptr)
270 exit_error(PARAMETER_PROBLEM,
271 "Bad line %u: need ]\n",
272 line);
Harald Welte9f7fa492001-03-15 15:12:02 +0000273
Harald Welteccd49e52001-01-23 22:54:34 +0000274 pcnt = strtok(buffer+1, ":");
Harald Welte9f7fa492001-03-15 15:12:02 +0000275 if (!pcnt)
276 exit_error(PARAMETER_PROBLEM,
277 "Bad line %u: need :\n",
278 line);
279
Harald Welteccd49e52001-01-23 22:54:34 +0000280 bcnt = strtok(NULL, "]");
Harald Welte9f7fa492001-03-15 15:12:02 +0000281 if (!bcnt)
282 exit_error(PARAMETER_PROBLEM,
283 "Bad line %u: need ]\n",
284 line);
285
286 /* start command parsing after counter */
287 parsestart = ptr + 1;
288 } else {
289 /* start command parsing at start of line */
290 parsestart = buffer;
Marc Bouchere6869a82000-03-20 06:03:29 +0000291 }
Rusty Russell7e53bf92000-03-20 07:03:28 +0000292
Harald Welte9f7fa492001-03-15 15:12:02 +0000293 add_argv(argv[0]);
294 add_argv("-t");
295 add_argv((char *) &curtable);
296
Harald Welteccd49e52001-01-23 22:54:34 +0000297 if (counters && pcnt && bcnt) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000298 add_argv("--set-counters");
299 add_argv((char *) pcnt);
300 add_argv((char *) bcnt);
Harald Welteccd49e52001-01-23 22:54:34 +0000301 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000302
Harald Welte26e643f2001-05-03 20:50:03 +0000303 /* After fighting with strtok enough, here's now
304 * a 'real' parser. According to Rusty I'm now no
305 * longer a real hacker, but I can live with that */
306
307 quote_open = 0;
308 param_start = parsestart;
309
310 for (curchar = parsestart; *curchar; curchar++) {
311 if (*curchar == '"') {
312 if (quote_open) {
313 quote_open = 0;
314 *curchar = ' ';
315 } else {
316 quote_open = 1;
317 param_start++;
318 }
319 }
320 if (*curchar == ' '
321 || *curchar == '\t'
322 || * curchar == '\n') {
323 char param_buffer[1024];
324 int param_len = curchar-param_start;
325
326 if (quote_open)
327 continue;
328
Harald Welte974d0102001-05-26 04:41:56 +0000329 if (!param_len) {
330 /* two spaces? */
331 param_start++;
332 continue;
333 }
Harald Welte26e643f2001-05-03 20:50:03 +0000334
335 /* end of one parameter */
336 strncpy(param_buffer, param_start,
337 param_len);
338 *(param_buffer+param_len) = '\0';
Harald Weltebb41f882001-10-21 14:11:54 +0000339
340 /* check if table name specified */
341 if (!strncmp(param_buffer, "-t", 3)) {
342 exit_error(PARAMETER_PROBLEM,
343 "Line %u seems to have a "
344 "-t table option.\n", line);
345 exit(1);
346 }
347
Harald Welte26e643f2001-05-03 20:50:03 +0000348 add_argv(param_buffer);
349 param_start += param_len + 1;
350 } else {
351 /* regular character, skip */
352 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000353 }
354
Harald Welte26e643f2001-05-03 20:50:03 +0000355 DEBUGP("calling do_command(%u, argv, &%s, handle):\n",
356 newargc, curtable);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000357
Harald Weltefa48fc82001-10-16 09:51:33 +0000358 for (a = 0; a < newargc; a++)
Harald Welteae1ff9f2000-12-01 14:26:20 +0000359 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
360
Harald Welte26e643f2001-05-03 20:50:03 +0000361 ret = do_command(newargc, newargv,
362 &newargv[2], &handle);
363
364 free_argv();
Marc Bouchere6869a82000-03-20 06:03:29 +0000365 }
366 if (!ret) {
367 fprintf(stderr, "%s: line %u failed\n",
368 program_name, line);
369 exit(1);
370 }
371 }
372
373 return 0;
374}