blob: a6e818e61634f1a56c4b9e6210eef3f02492fd8e [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 Weltefa48fc82001-10-16 09:51:33 +00007 * $Id: iptables-restore.c,v 1.15 2001/10/16 07:53:34 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) {
84 if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) {
Harald Welte26e643f2001-05-03 20:50:03 +000085 newargv[newargc] = strdup(what);
Harald Welte9f7fa492001-03-15 15:12:02 +000086 newargc++;
87 return 1;
88 } else
89 return 0;
90}
91
Harald Welte26e643f2001-05-03 20:50:03 +000092static void free_argv(void) {
93 int i;
94
95 for (i = 0; i < newargc; i++)
96 free(newargv[i]);
97}
98
Marc Bouchere6869a82000-03-20 06:03:29 +000099int main(int argc, char *argv[])
100{
101 iptc_handle_t handle;
102 char buffer[10240];
Marc Bouchere6869a82000-03-20 06:03:29 +0000103 unsigned int line = 0;
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;
Marc Bouchere6869a82000-03-20 06:03:29 +0000108
109 program_name = "iptables-restore";
110 program_version = NETFILTER_VERSION;
111
Harald Welte3efb6ea2001-08-06 18:50:21 +0000112#ifdef NO_SHARED_LIBS
113 init_extensions();
114#endif
115
Harald Welte58918652001-06-16 18:25:25 +0000116 while ((c = getopt_long(argc, argv, "bcvhnM:", options, NULL)) != -1) {
Harald Weltea9d27082000-12-19 16:10:42 +0000117 switch (c) {
118 case 'b':
119 binary = 1;
120 break;
121 case 'c':
122 counters = 1;
123 break;
124 case 'h':
125 print_usage("iptables-restore",
126 NETFILTER_VERSION);
127 break;
128 case 'n':
129 noflush = 1;
130 break;
Harald Welte58918652001-06-16 18:25:25 +0000131 case 'M':
132 modprobe = optarg;
133 break;
Harald Weltea9d27082000-12-19 16:10:42 +0000134 }
135 }
136
Marc Bouchere6869a82000-03-20 06:03:29 +0000137 if (optind == argc - 1) {
138 in = fopen(argv[optind], "r");
139 if (!in) {
140 fprintf(stderr, "Can't open %s: %s", argv[optind],
141 strerror(errno));
142 exit(1);
143 }
144 }
145 else if (optind < argc) {
146 fprintf(stderr, "Unknown arguments found on commandline");
147 exit(1);
148 }
149 else in = stdin;
Harald Welte9f7fa492001-03-15 15:12:02 +0000150
Marc Bouchere6869a82000-03-20 06:03:29 +0000151 /* Grab standard input. */
152 while (fgets(buffer, sizeof(buffer), in)) {
153 int ret;
154
155 line++;
156 if (buffer[0] == '\n') continue;
157 else if (buffer[0] == '#') {
158 if (verbose) fputs(buffer, stdout);
159 continue;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000160 } else if (strcmp(buffer, "COMMIT\n") == 0) {
161 DEBUGP("Calling commit\n");
Marc Bouchere6869a82000-03-20 06:03:29 +0000162 ret = iptc_commit(&handle);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000163 } else if (buffer[0] == '*') {
164 /* New table */
165 char *table;
166
167 table = strtok(buffer+1, " \t\n");
168 DEBUGP("line %u, table '%s'\n", line, table);
169 if (!table) {
170 exit_error(PARAMETER_PROBLEM,
171 "%s: line %u table name invalid\n",
172 program_name, line);
173 exit(1);
174 }
175 strncpy(curtable, table, IPT_TABLE_MAXNAMELEN);
176
Harald Welte58918652001-06-16 18:25:25 +0000177 handle = create_handle(table, modprobe);
Harald Weltea9d27082000-12-19 16:10:42 +0000178 if (noflush == 0) {
179 DEBUGP("Cleaning all chains of table '%s'\n",
180 table);
181 for_each_chain(flush_entries, verbose, 1,
182 &handle);
183
184 DEBUGP("Deleting all user-defined chains "
185 "of table '%s'\n", table);
186 for_each_chain(delete_chain, verbose, 0,
187 &handle) ;
188 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000189
190 ret = 1;
191
192 } else if (buffer[0] == ':') {
Marc Bouchere6869a82000-03-20 06:03:29 +0000193 /* New chain. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000194 char *policy, *chain;
Marc Bouchere6869a82000-03-20 06:03:29 +0000195
Marc Bouchere6869a82000-03-20 06:03:29 +0000196 chain = strtok(buffer+1, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000197 DEBUGP("line %u, chain '%s'\n", line, chain);
Marc Bouchere6869a82000-03-20 06:03:29 +0000198 if (!chain) {
199 exit_error(PARAMETER_PROBLEM,
200 "%s: line %u chain name invalid\n",
201 program_name, line);
202 exit(1);
203 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000204
Harald Welte9f7fa492001-03-15 15:12:02 +0000205 if (!iptc_builtin(chain, handle)) {
206 DEBUGP("Creating new chain '%s'\n", chain);
Harald Welte26e643f2001-05-03 20:50:03 +0000207 if (!iptc_create_chain(chain, &handle))
208 exit_error(PARAMETER_PROBLEM,
209 "error creating chain "
210 "'%s':%s\n", chain,
211 strerror(errno));
Harald Welte9f7fa492001-03-15 15:12:02 +0000212 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000213
Marc Bouchere6869a82000-03-20 06:03:29 +0000214 policy = strtok(NULL, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000215 DEBUGP("line %u, policy '%s'\n", line, policy);
Marc Bouchere6869a82000-03-20 06:03:29 +0000216 if (!policy) {
217 exit_error(PARAMETER_PROBLEM,
218 "%s: line %u policy invalid\n",
219 program_name, line);
220 exit(1);
221 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000222
223 if (strcmp(policy, "-") != 0) {
Harald Welted8e65632001-01-05 15:20:07 +0000224 struct ipt_counters count;
225
226 if (counters) {
227 char *ctrs;
228 ctrs = strtok(NULL, " \t\n");
229
230 parse_counters(ctrs, &count);
231
232 } else {
233 memset(&count, 0,
234 sizeof(struct ipt_counters));
235 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000236
237 DEBUGP("Setting policy of chain %s to %s\n",
238 chain, policy);
239
Harald Welted8e65632001-01-05 15:20:07 +0000240 if (!iptc_set_policy(chain, policy, &count,
241 &handle))
Harald Welteae1ff9f2000-12-01 14:26:20 +0000242 exit_error(OTHER_PROBLEM,
243 "Can't set policy `%s'"
244 " on `%s' line %u: %s\n",
245 chain, policy, line,
246 iptc_strerror(errno));
247 }
248
249 ret = 1;
250
Marc Bouchere6869a82000-03-20 06:03:29 +0000251 } else {
Harald Welte9f7fa492001-03-15 15:12:02 +0000252 int a;
Marc Bouchere6869a82000-03-20 06:03:29 +0000253 char *ptr = buffer;
Harald Welteccd49e52001-01-23 22:54:34 +0000254 char *pcnt = NULL;
255 char *bcnt = NULL;
Harald Welte9f7fa492001-03-15 15:12:02 +0000256 char *parsestart;
257
Harald Welte26e643f2001-05-03 20:50:03 +0000258 /* the parser */
259 char *param_start, *curchar;
260 int quote_open;
261
Harald Welte9f7fa492001-03-15 15:12:02 +0000262 /* reset the newargv */
263 newargc = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000264
Marc Bouchere6869a82000-03-20 06:03:29 +0000265 if (buffer[0] == '[') {
Harald Welte9f7fa492001-03-15 15:12:02 +0000266 /* we have counters in our input */
Marc Bouchere6869a82000-03-20 06:03:29 +0000267 ptr = strchr(buffer, ']');
268 if (!ptr)
269 exit_error(PARAMETER_PROBLEM,
270 "Bad line %u: need ]\n",
271 line);
Harald Welte9f7fa492001-03-15 15:12:02 +0000272
Harald Welteccd49e52001-01-23 22:54:34 +0000273 pcnt = strtok(buffer+1, ":");
Harald Welte9f7fa492001-03-15 15:12:02 +0000274 if (!pcnt)
275 exit_error(PARAMETER_PROBLEM,
276 "Bad line %u: need :\n",
277 line);
278
Harald Welteccd49e52001-01-23 22:54:34 +0000279 bcnt = strtok(NULL, "]");
Harald Welte9f7fa492001-03-15 15:12:02 +0000280 if (!bcnt)
281 exit_error(PARAMETER_PROBLEM,
282 "Bad line %u: need ]\n",
283 line);
284
285 /* start command parsing after counter */
286 parsestart = ptr + 1;
287 } else {
288 /* start command parsing at start of line */
289 parsestart = buffer;
Marc Bouchere6869a82000-03-20 06:03:29 +0000290 }
Rusty Russell7e53bf92000-03-20 07:03:28 +0000291
Harald Weltefe127c92001-10-16 07:53:34 +0000292 /* prevent iptables-restore from crashing in do_command
293 * when someone passes a "-t" on the line.
294 * - Ben Reser <ben@reser.org> */
295 if (strstr(buffer, "-t")) {
296 exit_error(PARAMETER_PROBLEM,
297 "Line %u seems to have a "
298 " -t table option.\n", line);
299 exit(1);
300 }
301 if (!strlen((char *) &curtable)) {
302 exit_error(PARAMETER_PROBLEM,
303 "Line %u seems to to have a "
304 " zero-length table name.\n", line);
305 exit(1);
306 }
307
Harald Welte9f7fa492001-03-15 15:12:02 +0000308 add_argv(argv[0]);
309 add_argv("-t");
310 add_argv((char *) &curtable);
311
Harald Welteccd49e52001-01-23 22:54:34 +0000312 if (counters && pcnt && bcnt) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000313 add_argv("--set-counters");
314 add_argv((char *) pcnt);
315 add_argv((char *) bcnt);
Harald Welteccd49e52001-01-23 22:54:34 +0000316 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000317
Harald Welte26e643f2001-05-03 20:50:03 +0000318 /* After fighting with strtok enough, here's now
319 * a 'real' parser. According to Rusty I'm now no
320 * longer a real hacker, but I can live with that */
321
322 quote_open = 0;
323 param_start = parsestart;
324
325 for (curchar = parsestart; *curchar; curchar++) {
326 if (*curchar == '"') {
327 if (quote_open) {
328 quote_open = 0;
329 *curchar = ' ';
330 } else {
331 quote_open = 1;
332 param_start++;
333 }
334 }
335 if (*curchar == ' '
336 || *curchar == '\t'
337 || * curchar == '\n') {
338 char param_buffer[1024];
339 int param_len = curchar-param_start;
340
341 if (quote_open)
342 continue;
343
Harald Welte974d0102001-05-26 04:41:56 +0000344 if (!param_len) {
345 /* two spaces? */
346 param_start++;
347 continue;
348 }
Harald Welte26e643f2001-05-03 20:50:03 +0000349
350 /* end of one parameter */
351 strncpy(param_buffer, param_start,
352 param_len);
353 *(param_buffer+param_len) = '\0';
354 add_argv(param_buffer);
355 param_start += param_len + 1;
356 } else {
357 /* regular character, skip */
358 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000359 }
360
Harald Welte26e643f2001-05-03 20:50:03 +0000361 DEBUGP("calling do_command(%u, argv, &%s, handle):\n",
362 newargc, curtable);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000363
Harald Weltefa48fc82001-10-16 09:51:33 +0000364 for (a = 0; a < newargc; a++)
Harald Welteae1ff9f2000-12-01 14:26:20 +0000365 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
366
Harald Welte26e643f2001-05-03 20:50:03 +0000367 ret = do_command(newargc, newargv,
368 &newargv[2], &handle);
369
370 free_argv();
Marc Bouchere6869a82000-03-20 06:03:29 +0000371 }
372 if (!ret) {
373 fprintf(stderr, "%s: line %u failed\n",
374 program_name, line);
375 exit(1);
376 }
377 }
378
379 return 0;
380}