blob: dbee1b67f8cf5fdd89daa47f68ecb2de459c389e [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
6 */
7
Marc Bouchere6869a82000-03-20 06:03:29 +00008#include <getopt.h>
9#include <sys/errno.h>
10#include <string.h>
11#include <stdio.h>
Harald Welteae1ff9f2000-12-01 14:26:20 +000012#include <stdlib.h>
13#include "iptables.h"
14#include "libiptc/libiptc.h"
15
16#ifdef DEBUG
Harald Weltec45c3152000-12-06 08:11:24 +000017#define DEBUGP(x, args...) fprintf(stderr, x, ## args)
Harald Welteae1ff9f2000-12-01 14:26:20 +000018#else
Harald Weltec45c3152000-12-06 08:11:24 +000019#define DEBUGP(x, args...)
Harald Welteae1ff9f2000-12-01 14:26:20 +000020#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000021
Harald Weltea9d27082000-12-19 16:10:42 +000022static int binary = 0, counters = 0, verbose = 0, noflush = 0;
23
Marc Bouchere6869a82000-03-20 06:03:29 +000024/* Keeping track of external matches and targets. */
25static struct option options[] = {
Harald Weltea9d27082000-12-19 16:10:42 +000026 { "binary", 0, 0, 'b' },
27 { "counters", 0, 0, 'c' },
28/* { "verbose", 1, 0, 'v' }, */
29 { "help", 0, 0, 'h' },
30 { "noflush", 0, 0, 'n'},
Marc Bouchere6869a82000-03-20 06:03:29 +000031 { 0 }
32};
33
34static void print_usage(const char *name, const char *version) __attribute__((noreturn));
35
36static void print_usage(const char *name, const char *version)
37{
Harald Weltea9d27082000-12-19 16:10:42 +000038 fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-h]\n"
39 " [ --binary ]\n"
40 " [ --counters ]\n"
41 " [ --verbose ]\n"
42 " [ --help ]\n"
43 " [ --noflush ]\n", name);
44
Marc Bouchere6869a82000-03-20 06:03:29 +000045 exit(1);
46}
47
Harald Welteae1ff9f2000-12-01 14:26:20 +000048iptc_handle_t create_handle(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +000049{
Harald Welteae1ff9f2000-12-01 14:26:20 +000050 iptc_handle_t handle;
Marc Bouchere6869a82000-03-20 06:03:29 +000051
Harald Welteae1ff9f2000-12-01 14:26:20 +000052 handle = iptc_init(tablename);
53 if (!handle) {
54 exit_error(PARAMETER_PROBLEM, "%s: unable to initialize"
55 "table '%s'\n", program_name, tablename);
56 exit(1);
Marc Bouchere6869a82000-03-20 06:03:29 +000057 }
Harald Welteae1ff9f2000-12-01 14:26:20 +000058 return handle;
Marc Bouchere6869a82000-03-20 06:03:29 +000059}
60
Harald Welted8e65632001-01-05 15:20:07 +000061int parse_counters(char *string, struct ipt_counters *ctr)
62{
63 return (sscanf(string, "[%llu:%llu]", &ctr->pcnt, &ctr->bcnt) == 2);
64}
Harald Welteae1ff9f2000-12-01 14:26:20 +000065
Marc Bouchere6869a82000-03-20 06:03:29 +000066int main(int argc, char *argv[])
67{
68 iptc_handle_t handle;
69 char buffer[10240];
Marc Bouchere6869a82000-03-20 06:03:29 +000070 unsigned int line = 0;
Harald Weltea9d27082000-12-19 16:10:42 +000071 int c;
Harald Welteae1ff9f2000-12-01 14:26:20 +000072 char curtable[IPT_TABLE_MAXNAMELEN + 1];
73 char curchain[IPT_FUNCTION_MAXNAMELEN + 1];
Marc Bouchere6869a82000-03-20 06:03:29 +000074 FILE *in;
75
76 program_name = "iptables-restore";
77 program_version = NETFILTER_VERSION;
78
Harald Weltea9d27082000-12-19 16:10:42 +000079 while ((c = getopt_long(argc, argv, "bcvhn", options, NULL)) != -1) {
80 switch (c) {
81 case 'b':
82 binary = 1;
83 break;
84 case 'c':
85 counters = 1;
86 break;
87 case 'h':
88 print_usage("iptables-restore",
89 NETFILTER_VERSION);
90 break;
91 case 'n':
92 noflush = 1;
93 break;
94 }
95 }
96
Marc Bouchere6869a82000-03-20 06:03:29 +000097 if (optind == argc - 1) {
98 in = fopen(argv[optind], "r");
99 if (!in) {
100 fprintf(stderr, "Can't open %s: %s", argv[optind],
101 strerror(errno));
102 exit(1);
103 }
104 }
105 else if (optind < argc) {
106 fprintf(stderr, "Unknown arguments found on commandline");
107 exit(1);
108 }
109 else in = stdin;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000110/*
111 handle = iptc_init("filter");
Marc Bouchere6869a82000-03-20 06:03:29 +0000112 if (!handle)
113 exit_error(VERSION_PROBLEM,
114 "can't initialize iptables-restore: %s",
115 iptc_strerror(errno));
116
117 if (!clean_slate(&handle))
118 exit_error(OTHER_PROBLEM, "Deleting old chains: %s",
119 iptc_strerror(errno));
Harald Welteae1ff9f2000-12-01 14:26:20 +0000120*/
Marc Bouchere6869a82000-03-20 06:03:29 +0000121 /* Grab standard input. */
122 while (fgets(buffer, sizeof(buffer), in)) {
123 int ret;
124
125 line++;
126 if (buffer[0] == '\n') continue;
127 else if (buffer[0] == '#') {
128 if (verbose) fputs(buffer, stdout);
129 continue;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000130 } else if (strcmp(buffer, "COMMIT\n") == 0) {
131 DEBUGP("Calling commit\n");
Marc Bouchere6869a82000-03-20 06:03:29 +0000132 ret = iptc_commit(&handle);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000133 } else if (buffer[0] == '*') {
134 /* New table */
135 char *table;
136
137 table = strtok(buffer+1, " \t\n");
138 DEBUGP("line %u, table '%s'\n", line, table);
139 if (!table) {
140 exit_error(PARAMETER_PROBLEM,
141 "%s: line %u table name invalid\n",
142 program_name, line);
143 exit(1);
144 }
145 strncpy(curtable, table, IPT_TABLE_MAXNAMELEN);
146
147 handle = create_handle(table);
Harald Weltea9d27082000-12-19 16:10:42 +0000148 if (noflush == 0) {
149 DEBUGP("Cleaning all chains of table '%s'\n",
150 table);
151 for_each_chain(flush_entries, verbose, 1,
152 &handle);
153
154 DEBUGP("Deleting all user-defined chains "
155 "of table '%s'\n", table);
156 for_each_chain(delete_chain, verbose, 0,
157 &handle) ;
158 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000159
160 ret = 1;
161
162 } else if (buffer[0] == ':') {
Marc Bouchere6869a82000-03-20 06:03:29 +0000163 /* New chain. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000164 char *policy, *chain;
Marc Bouchere6869a82000-03-20 06:03:29 +0000165
Marc Bouchere6869a82000-03-20 06:03:29 +0000166 chain = strtok(buffer+1, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000167 DEBUGP("line %u, chain '%s'\n", line, chain);
Marc Bouchere6869a82000-03-20 06:03:29 +0000168 if (!chain) {
169 exit_error(PARAMETER_PROBLEM,
170 "%s: line %u chain name invalid\n",
171 program_name, line);
172 exit(1);
173 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000174 strncpy(curchain, chain, IPT_FUNCTION_MAXNAMELEN);
175
176 /* why the f... does iptc_builtin not work here ? */
177// if (!iptc_builtin(curchain, &handle)) {
178 DEBUGP("Creating new chain '%s'\n", curchain);
179 if (!iptc_create_chain(curchain, &handle))
180 DEBUGP("unable to create chain '%s':%s\n", curchain,
181 strerror(errno));
182// }
183
Marc Bouchere6869a82000-03-20 06:03:29 +0000184 policy = strtok(NULL, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000185 DEBUGP("line %u, policy '%s'\n", line, policy);
Marc Bouchere6869a82000-03-20 06:03:29 +0000186 if (!policy) {
187 exit_error(PARAMETER_PROBLEM,
188 "%s: line %u policy invalid\n",
189 program_name, line);
190 exit(1);
191 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000192
193 if (strcmp(policy, "-") != 0) {
Harald Welted8e65632001-01-05 15:20:07 +0000194 struct ipt_counters count;
195
196 if (counters) {
197 char *ctrs;
198 ctrs = strtok(NULL, " \t\n");
199
200 parse_counters(ctrs, &count);
201
202 } else {
203 memset(&count, 0,
204 sizeof(struct ipt_counters));
205 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000206
207 DEBUGP("Setting policy of chain %s to %s\n",
208 chain, policy);
209
Harald Welted8e65632001-01-05 15:20:07 +0000210 if (!iptc_set_policy(chain, policy, &count,
211 &handle))
Harald Welteae1ff9f2000-12-01 14:26:20 +0000212 exit_error(OTHER_PROBLEM,
213 "Can't set policy `%s'"
214 " on `%s' line %u: %s\n",
215 chain, policy, line,
216 iptc_strerror(errno));
217 }
218
219 ret = 1;
220
Marc Bouchere6869a82000-03-20 06:03:29 +0000221 } else {
222 char *newargv[1024];
Harald Welteae1ff9f2000-12-01 14:26:20 +0000223 int i,a;
Marc Bouchere6869a82000-03-20 06:03:29 +0000224 char *ptr = buffer;
Harald Welted8e65632001-01-05 15:20:07 +0000225 char *ctrs = NULL;
226 struct ipt_counters count;
Marc Bouchere6869a82000-03-20 06:03:29 +0000227
Marc Bouchere6869a82000-03-20 06:03:29 +0000228 if (buffer[0] == '[') {
229 ptr = strchr(buffer, ']');
230 if (!ptr)
231 exit_error(PARAMETER_PROBLEM,
232 "Bad line %u: need ]\n",
233 line);
Harald Welted8e65632001-01-05 15:20:07 +0000234 ctrs = strtok(ptr, " \t\n");
Marc Bouchere6869a82000-03-20 06:03:29 +0000235 }
Rusty Russell7e53bf92000-03-20 07:03:28 +0000236
Harald Welted8e65632001-01-05 15:20:07 +0000237 if (counters && ctrs) {
238
239 parse_counters(ctrs, &count);
240 }
241
242 /* FIXME: Don't ignore counters. */
243
Marc Bouchere6869a82000-03-20 06:03:29 +0000244 newargv[0] = argv[0];
Harald Welteae1ff9f2000-12-01 14:26:20 +0000245 newargv[1] = "-t";
246 newargv[2] = (char *) &curtable;
247 newargv[3] = "-A";
248 newargv[4] = (char *) &curchain;
249
250 /* strtok: a function only a coder could love */
251 for (i = 5; i < sizeof(newargv)/sizeof(char *); i++) {
Harald Welted8e65632001-01-05 15:20:07 +0000252 if (!(newargv[i] = strtok(NULL, " \t\n")))
Marc Bouchere6869a82000-03-20 06:03:29 +0000253 break;
254 ptr = NULL;
255 }
256 if (i == sizeof(newargv)/sizeof(char *)) {
257 fprintf(stderr,
258 "%s: line %u too many arguments\n",
259 program_name, line);
260 exit(1);
261 }
262
Harald Welteae1ff9f2000-12-01 14:26:20 +0000263 DEBUGP("===>calling do_command(%u, argv, &%s, handle):\n",
264 i, curtable);
265
266 for (a = 0; a <= i; a++)
267 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
268
269 ret = do_command(i, newargv, &newargv[2], &handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000270 }
271 if (!ret) {
272 fprintf(stderr, "%s: line %u failed\n",
273 program_name, line);
274 exit(1);
275 }
276 }
277
278 return 0;
279}