blob: 936c66671876f4cb457974722cba0475393b1961 [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 Welte9f7fa492001-03-15 15:12:02 +00007 * $Id: iptables-restore.c,v 1.9 2001/02/26 17:31:20 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 */
69static char* newargv[1024];
70static 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 *))) {
76 newargv[newargc] = what;
77 newargc++;
78 return 1;
79 } else
80 return 0;
81}
82
Marc Bouchere6869a82000-03-20 06:03:29 +000083int main(int argc, char *argv[])
84{
85 iptc_handle_t handle;
86 char buffer[10240];
Marc Bouchere6869a82000-03-20 06:03:29 +000087 unsigned int line = 0;
Harald Weltea9d27082000-12-19 16:10:42 +000088 int c;
Harald Welteae1ff9f2000-12-01 14:26:20 +000089 char curtable[IPT_TABLE_MAXNAMELEN + 1];
Marc Bouchere6869a82000-03-20 06:03:29 +000090 FILE *in;
91
92 program_name = "iptables-restore";
93 program_version = NETFILTER_VERSION;
94
Harald Weltea9d27082000-12-19 16:10:42 +000095 while ((c = getopt_long(argc, argv, "bcvhn", options, NULL)) != -1) {
96 switch (c) {
97 case 'b':
98 binary = 1;
99 break;
100 case 'c':
101 counters = 1;
102 break;
103 case 'h':
104 print_usage("iptables-restore",
105 NETFILTER_VERSION);
106 break;
107 case 'n':
108 noflush = 1;
109 break;
110 }
111 }
112
Marc Bouchere6869a82000-03-20 06:03:29 +0000113 if (optind == argc - 1) {
114 in = fopen(argv[optind], "r");
115 if (!in) {
116 fprintf(stderr, "Can't open %s: %s", argv[optind],
117 strerror(errno));
118 exit(1);
119 }
120 }
121 else if (optind < argc) {
122 fprintf(stderr, "Unknown arguments found on commandline");
123 exit(1);
124 }
125 else in = stdin;
Harald Welte9f7fa492001-03-15 15:12:02 +0000126
Marc Bouchere6869a82000-03-20 06:03:29 +0000127 /* Grab standard input. */
128 while (fgets(buffer, sizeof(buffer), in)) {
129 int ret;
130
131 line++;
132 if (buffer[0] == '\n') continue;
133 else if (buffer[0] == '#') {
134 if (verbose) fputs(buffer, stdout);
135 continue;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000136 } else if (strcmp(buffer, "COMMIT\n") == 0) {
137 DEBUGP("Calling commit\n");
Marc Bouchere6869a82000-03-20 06:03:29 +0000138 ret = iptc_commit(&handle);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000139 } else if (buffer[0] == '*') {
140 /* New table */
141 char *table;
142
143 table = strtok(buffer+1, " \t\n");
144 DEBUGP("line %u, table '%s'\n", line, table);
145 if (!table) {
146 exit_error(PARAMETER_PROBLEM,
147 "%s: line %u table name invalid\n",
148 program_name, line);
149 exit(1);
150 }
151 strncpy(curtable, table, IPT_TABLE_MAXNAMELEN);
152
153 handle = create_handle(table);
Harald Weltea9d27082000-12-19 16:10:42 +0000154 if (noflush == 0) {
155 DEBUGP("Cleaning all chains of table '%s'\n",
156 table);
157 for_each_chain(flush_entries, verbose, 1,
158 &handle);
159
160 DEBUGP("Deleting all user-defined chains "
161 "of table '%s'\n", table);
162 for_each_chain(delete_chain, verbose, 0,
163 &handle) ;
164 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000165
166 ret = 1;
167
168 } else if (buffer[0] == ':') {
Marc Bouchere6869a82000-03-20 06:03:29 +0000169 /* New chain. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000170 char *policy, *chain;
Marc Bouchere6869a82000-03-20 06:03:29 +0000171
Marc Bouchere6869a82000-03-20 06:03:29 +0000172 chain = strtok(buffer+1, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000173 DEBUGP("line %u, chain '%s'\n", line, chain);
Marc Bouchere6869a82000-03-20 06:03:29 +0000174 if (!chain) {
175 exit_error(PARAMETER_PROBLEM,
176 "%s: line %u chain name invalid\n",
177 program_name, line);
178 exit(1);
179 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000180
Rusty Russell5df9bda2001-01-07 06:51:00 +0000181 /* FIXME: abort if chain creation fails --RR */
Harald Welte9f7fa492001-03-15 15:12:02 +0000182 if (!iptc_builtin(chain, handle)) {
183 DEBUGP("Creating new chain '%s'\n", chain);
184 if (!iptc_create_chain(chain, &handle))
185 DEBUGP("unable to create chain '%s':%s\n", chain,
Harald Welteae1ff9f2000-12-01 14:26:20 +0000186 strerror(errno));
Harald Welte9f7fa492001-03-15 15:12:02 +0000187 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000188
Marc Bouchere6869a82000-03-20 06:03:29 +0000189 policy = strtok(NULL, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000190 DEBUGP("line %u, policy '%s'\n", line, policy);
Marc Bouchere6869a82000-03-20 06:03:29 +0000191 if (!policy) {
192 exit_error(PARAMETER_PROBLEM,
193 "%s: line %u policy invalid\n",
194 program_name, line);
195 exit(1);
196 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000197
198 if (strcmp(policy, "-") != 0) {
Harald Welted8e65632001-01-05 15:20:07 +0000199 struct ipt_counters count;
200
201 if (counters) {
202 char *ctrs;
203 ctrs = strtok(NULL, " \t\n");
204
205 parse_counters(ctrs, &count);
206
207 } else {
208 memset(&count, 0,
209 sizeof(struct ipt_counters));
210 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000211
212 DEBUGP("Setting policy of chain %s to %s\n",
213 chain, policy);
214
Harald Welted8e65632001-01-05 15:20:07 +0000215 if (!iptc_set_policy(chain, policy, &count,
216 &handle))
Harald Welteae1ff9f2000-12-01 14:26:20 +0000217 exit_error(OTHER_PROBLEM,
218 "Can't set policy `%s'"
219 " on `%s' line %u: %s\n",
220 chain, policy, line,
221 iptc_strerror(errno));
222 }
223
224 ret = 1;
225
Marc Bouchere6869a82000-03-20 06:03:29 +0000226 } else {
Harald Welte9f7fa492001-03-15 15:12:02 +0000227 int a;
Marc Bouchere6869a82000-03-20 06:03:29 +0000228 char *ptr = buffer;
Harald Welteccd49e52001-01-23 22:54:34 +0000229 char *pcnt = NULL;
230 char *bcnt = NULL;
Harald Welte9f7fa492001-03-15 15:12:02 +0000231 char *parsestart;
232
233 /* reset the newargv */
234 newargc = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000235
Marc Bouchere6869a82000-03-20 06:03:29 +0000236 if (buffer[0] == '[') {
Harald Welte9f7fa492001-03-15 15:12:02 +0000237 /* we have counters in our input */
Marc Bouchere6869a82000-03-20 06:03:29 +0000238 ptr = strchr(buffer, ']');
239 if (!ptr)
240 exit_error(PARAMETER_PROBLEM,
241 "Bad line %u: need ]\n",
242 line);
Harald Welte9f7fa492001-03-15 15:12:02 +0000243
Harald Welteccd49e52001-01-23 22:54:34 +0000244 pcnt = strtok(buffer+1, ":");
Harald Welte9f7fa492001-03-15 15:12:02 +0000245 if (!pcnt)
246 exit_error(PARAMETER_PROBLEM,
247 "Bad line %u: need :\n",
248 line);
249
Harald Welteccd49e52001-01-23 22:54:34 +0000250 bcnt = strtok(NULL, "]");
Harald Welte9f7fa492001-03-15 15:12:02 +0000251 if (!bcnt)
252 exit_error(PARAMETER_PROBLEM,
253 "Bad line %u: need ]\n",
254 line);
255
256 /* start command parsing after counter */
257 parsestart = ptr + 1;
258 } else {
259 /* start command parsing at start of line */
260 parsestart = buffer;
Marc Bouchere6869a82000-03-20 06:03:29 +0000261 }
Rusty Russell7e53bf92000-03-20 07:03:28 +0000262
Harald Welte9f7fa492001-03-15 15:12:02 +0000263 add_argv(argv[0]);
264 add_argv("-t");
265 add_argv((char *) &curtable);
266
Harald Welteccd49e52001-01-23 22:54:34 +0000267 if (counters && pcnt && bcnt) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000268 add_argv("--set-counters");
269 add_argv((char *) pcnt);
270 add_argv((char *) bcnt);
Harald Welteccd49e52001-01-23 22:54:34 +0000271 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000272
Harald Welte9f7fa492001-03-15 15:12:02 +0000273 /* parse till end of line */
274 if (add_argv(strtok(parsestart, " \t\n"))) {
275 while (add_argv(strtok(NULL, " \t\n")));
Marc Bouchere6869a82000-03-20 06:03:29 +0000276 }
277
Harald Welteae1ff9f2000-12-01 14:26:20 +0000278 DEBUGP("===>calling do_command(%u, argv, &%s, handle):\n",
Harald Welte9f7fa492001-03-15 15:12:02 +0000279 newargc, curtable);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000280
Harald Welte9f7fa492001-03-15 15:12:02 +0000281 for (a = 0; a <= newargc; a++)
Harald Welteae1ff9f2000-12-01 14:26:20 +0000282 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
283
Harald Welte9f7fa492001-03-15 15:12:02 +0000284 ret = do_command(newargc, newargv, &newargv[2], &handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000285 }
286 if (!ret) {
287 fprintf(stderr, "%s: line %u failed\n",
288 program_name, line);
289 exit(1);
290 }
291 }
292
293 return 0;
294}