blob: 40804eef7e7035803805df380985159657f6ee19 [file] [log] [blame]
András Kis-Szabó2f523792001-02-27 09:59:48 +00001/* Code to restore the iptables state, from file by ip6tables-save.
2 * Author: Andras Kis-Szabo <kisza@sch.bme.hu>
3 *
4 * based on iptables-restore
5 * Authors:
6 * Harald Welte <laforge@gnumonks.org>
7 * Rusty Russell <rusty@linuxcare.com.au>
8 *
9 */
10
11#include <getopt.h>
12#include <sys/errno.h>
13#include <string.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include "ip6tables.h"
17#include "libiptc/libip6tc.h"
18
19#ifdef DEBUG
20#define DEBUGP(x, args...) fprintf(stderr, x, ## args)
21#else
22#define DEBUGP(x, args...)
23#endif
24
25extern int for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *), int verbose, int builtinstoo, ip6tc_handle_t *handle);
26extern int flush_entries(const ip6t_chainlabel chain, int verbose, ip6tc_handle_t *handle);
27extern int delete_chain(const ip6t_chainlabel chain, int verbose, ip6tc_handle_t *handle);
28
29static int binary = 0, counters = 0, verbose = 0, noflush = 0;
30
31/* Keeping track of external matches and targets. */
32static struct option options[] = {
33 { "binary", 0, 0, 'b' },
34 { "counters", 0, 0, 'c' },
35/* { "verbose", 1, 0, 'v' }, */
36 { "help", 0, 0, 'h' },
37 { "noflush", 0, 0, 'n'},
Harald Welte58918652001-06-16 18:25:25 +000038 { "modprobe", 1, 0, 'M'},
András Kis-Szabó2f523792001-02-27 09:59:48 +000039 { 0 }
40};
41
42static void print_usage(const char *name, const char *version) __attribute__((noreturn));
43
44static void print_usage(const char *name, const char *version)
45{
46 fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-h]\n"
47 " [ --binary ]\n"
48 " [ --counters ]\n"
49 " [ --verbose ]\n"
50 " [ --help ]\n"
Harald Welte58918652001-06-16 18:25:25 +000051 " [ --noflush ]\n"
52 " [ --modprobe=<command>]\n", name);
András Kis-Szabó2f523792001-02-27 09:59:48 +000053
54 exit(1);
55}
56
Harald Welte58918652001-06-16 18:25:25 +000057ip6tc_handle_t create_handle(const char *tablename, const char* modprobe)
András Kis-Szabó2f523792001-02-27 09:59:48 +000058{
59 ip6tc_handle_t handle;
60
61 handle = ip6tc_init(tablename);
Harald Welte58918652001-06-16 18:25:25 +000062
63 if (!handle) {
64 /* try to insmod the module if iptc_init failed */
65 ip6tables_insmod("ip6_tables", modprobe);
66 handle = ip6tc_init(tablename);
67 }
68
András Kis-Szabó2f523792001-02-27 09:59:48 +000069 if (!handle) {
70 exit_error(PARAMETER_PROBLEM, "%s: unable to initialize"
71 "table '%s'\n", program_name, tablename);
72 exit(1);
73 }
74 return handle;
75}
76
77int parse_counters(char *string, struct ip6t_counters *ctr)
78{
79 return (sscanf(string, "[%llu:%llu]", &ctr->pcnt, &ctr->bcnt) == 2);
80}
81
82int main(int argc, char *argv[])
83{
84 ip6tc_handle_t handle;
85 char buffer[10240];
86 unsigned int line = 0;
87 int c;
88 char curtable[IP6T_TABLE_MAXNAMELEN + 1];
89 char curchain[IP6T_FUNCTION_MAXNAMELEN + 1];
90 FILE *in;
Harald Welte58918652001-06-16 18:25:25 +000091 const char *modprobe = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +000092
93 program_name = "ip6tables-restore";
94 program_version = NETFILTER_VERSION;
95
Harald Welte3efb6ea2001-08-06 18:50:21 +000096#ifdef NO_SHARED_LIBS
97 init_extensions();
98#endif
99
Harald Welte58918652001-06-16 18:25:25 +0000100 while ((c = getopt_long(argc, argv, "bcvhnM:", options, NULL)) != -1) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000101 switch (c) {
102 case 'b':
103 binary = 1;
104 break;
105 case 'c':
106 counters = 1;
107 break;
108 case 'h':
109 print_usage("ip6tables-restore",
110 NETFILTER_VERSION);
111 break;
112 case 'n':
113 noflush = 1;
114 break;
Harald Welte58918652001-06-16 18:25:25 +0000115 case 'M':
116 modprobe = optarg;
117 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000118 }
119 }
120
121 if (optind == argc - 1) {
122 in = fopen(argv[optind], "r");
123 if (!in) {
124 fprintf(stderr, "Can't open %s: %s", argv[optind],
125 strerror(errno));
126 exit(1);
127 }
128 }
129 else if (optind < argc) {
130 fprintf(stderr, "Unknown arguments found on commandline");
131 exit(1);
132 }
133 else in = stdin;
134/*
135 handle = iptc_init("filter");
136 if (!handle)
137 exit_error(VERSION_PROBLEM,
138 "can't initialize iptables-restore: %s",
139 iptc_strerror(errno));
140
141 if (!clean_slate(&handle))
142 exit_error(OTHER_PROBLEM, "Deleting old chains: %s",
143 iptc_strerror(errno));
144*/
145 /* Grab standard input. */
146 while (fgets(buffer, sizeof(buffer), in)) {
147 int ret;
148
149 line++;
150 if (buffer[0] == '\n') continue;
151 else if (buffer[0] == '#') {
152 if (verbose) fputs(buffer, stdout);
153 continue;
154 } else if (strcmp(buffer, "COMMIT\n") == 0) {
155 DEBUGP("Calling commit\n");
156 ret = ip6tc_commit(&handle);
157 } else if (buffer[0] == '*') {
158 /* New table */
159 char *table;
160
161 table = strtok(buffer+1, " \t\n");
162 DEBUGP("line %u, table '%s'\n", line, table);
163 if (!table) {
164 exit_error(PARAMETER_PROBLEM,
165 "%s: line %u table name invalid\n",
166 program_name, line);
167 exit(1);
168 }
169 strncpy(curtable, table, IP6T_TABLE_MAXNAMELEN);
170
Harald Welte58918652001-06-16 18:25:25 +0000171 handle = create_handle(table, modprobe);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000172 if (noflush == 0) {
173 DEBUGP("Cleaning all chains of table '%s'\n",
174 table);
175 for_each_chain(flush_entries, verbose, 1,
176 &handle);
177
178 DEBUGP("Deleting all user-defined chains "
179 "of table '%s'\n", table);
180 for_each_chain(delete_chain, verbose, 0,
181 &handle) ;
182 }
183
184 ret = 1;
185
186 } else if (buffer[0] == ':') {
187 /* New chain. */
188 char *policy, *chain;
189
190 chain = strtok(buffer+1, " \t\n");
191 DEBUGP("line %u, chain '%s'\n", line, chain);
192 if (!chain) {
193 exit_error(PARAMETER_PROBLEM,
194 "%s: line %u chain name invalid\n",
195 program_name, line);
196 exit(1);
197 }
198 strncpy(curchain, chain, IP6T_FUNCTION_MAXNAMELEN);
199
200 /* why the f... does iptc_builtin not work here ? */
201 /* FIXME: abort if chain creation fails --RR */
202// if (!iptc_builtin(curchain, &handle)) {
203 DEBUGP("Creating new chain '%s'\n", curchain);
204 if (!ip6tc_create_chain(curchain, &handle))
205 DEBUGP("unable to create chain '%s':%s\n", curchain,
206 strerror(errno));
207// }
208
209 policy = strtok(NULL, " \t\n");
210 DEBUGP("line %u, policy '%s'\n", line, policy);
211 if (!policy) {
212 exit_error(PARAMETER_PROBLEM,
213 "%s: line %u policy invalid\n",
214 program_name, line);
215 exit(1);
216 }
217
218 if (strcmp(policy, "-") != 0) {
219 struct ip6t_counters count;
220
221 if (counters) {
222 char *ctrs;
223 ctrs = strtok(NULL, " \t\n");
224
225 parse_counters(ctrs, &count);
226
227 } else {
228 memset(&count, 0,
229 sizeof(struct ip6t_counters));
230 }
231
232 DEBUGP("Setting policy of chain %s to %s\n",
233 chain, policy);
234
235 if (!ip6tc_set_policy(chain, policy, &count,
236 &handle))
237 exit_error(OTHER_PROBLEM,
238 "Can't set policy `%s'"
239 " on `%s' line %u: %s\n",
240 chain, policy, line,
241 ip6tc_strerror(errno));
242 }
243
244 ret = 1;
245
246 } else {
247 char *newargv[1024];
248 int i,a, argvsize;
249 char *ptr = buffer;
250 char *pcnt = NULL;
251 char *bcnt = NULL;
252
253 if (buffer[0] == '[') {
254 ptr = strchr(buffer, ']');
255 if (!ptr)
256 exit_error(PARAMETER_PROBLEM,
257 "Bad line %u: need ]\n",
258 line);
259 pcnt = strtok(buffer+1, ":");
260 bcnt = strtok(NULL, "]");
261 }
262
263 newargv[0] = argv[0];
264 newargv[1] = "-t";
265 newargv[2] = (char *) &curtable;
266 newargv[3] = "-A";
267 newargv[4] = (char *) &curchain;
268 argvsize = 5;
269
270 if (counters && pcnt && bcnt) {
271 newargv[5] = "--set-counters";
272 newargv[6] = (char *) pcnt;
273 newargv[7] = (char *) bcnt;
274 argvsize = 8;
275 }
276
277 // strtok initcialize!
278 if ( buffer[0]!='[' )
279 {
280 if (!(newargv[argvsize] = strtok(buffer, " \t\n")))
281 goto ImLaMeR;
282 //break;
283 argvsize++;
284 }
285
286 /* strtok: a function only a coder could love */
287 for (i = argvsize; i < sizeof(newargv)/sizeof(char *);
288 i++) {
289 if (!(newargv[i] = strtok(NULL, " \t\n")))
290 break;
291 ptr = NULL;
292 }
293ImLaMeR: if (i == sizeof(newargv)/sizeof(char *)) {
294 fprintf(stderr,
295 "%s: line %u too many arguments\n",
296 program_name, line);
297 exit(1);
298 }
299
300 DEBUGP("===>calling do_command6(%u, argv, &%s, handle):\n",
301 i, curtable);
302
303 for (a = 0; a <= i; a++)
304 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
305
306 ret = do_command6(i, newargv, &newargv[2], &handle);
307 }
308 if (!ret) {
309 fprintf(stderr, "%s: line %u failed\n",
310 program_name, line);
311 exit(1);
312 }
313 }
314
315 return 0;
316}