blob: 9ebc5775b92df7b88ffdd9dd64620cce857da8f9 [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'},
38 { 0 }
39};
40
41static void print_usage(const char *name, const char *version) __attribute__((noreturn));
42
43static void print_usage(const char *name, const char *version)
44{
45 fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-h]\n"
46 " [ --binary ]\n"
47 " [ --counters ]\n"
48 " [ --verbose ]\n"
49 " [ --help ]\n"
50 " [ --noflush ]\n", name);
51
52 exit(1);
53}
54
55ip6tc_handle_t create_handle(const char *tablename)
56{
57 ip6tc_handle_t handle;
58
59 handle = ip6tc_init(tablename);
60 if (!handle) {
61 exit_error(PARAMETER_PROBLEM, "%s: unable to initialize"
62 "table '%s'\n", program_name, tablename);
63 exit(1);
64 }
65 return handle;
66}
67
68int parse_counters(char *string, struct ip6t_counters *ctr)
69{
70 return (sscanf(string, "[%llu:%llu]", &ctr->pcnt, &ctr->bcnt) == 2);
71}
72
73int main(int argc, char *argv[])
74{
75 ip6tc_handle_t handle;
76 char buffer[10240];
77 unsigned int line = 0;
78 int c;
79 char curtable[IP6T_TABLE_MAXNAMELEN + 1];
80 char curchain[IP6T_FUNCTION_MAXNAMELEN + 1];
81 FILE *in;
82
83 program_name = "ip6tables-restore";
84 program_version = NETFILTER_VERSION;
85
86 while ((c = getopt_long(argc, argv, "bcvhn", options, NULL)) != -1) {
87 switch (c) {
88 case 'b':
89 binary = 1;
90 break;
91 case 'c':
92 counters = 1;
93 break;
94 case 'h':
95 print_usage("ip6tables-restore",
96 NETFILTER_VERSION);
97 break;
98 case 'n':
99 noflush = 1;
100 break;
101 }
102 }
103
104 if (optind == argc - 1) {
105 in = fopen(argv[optind], "r");
106 if (!in) {
107 fprintf(stderr, "Can't open %s: %s", argv[optind],
108 strerror(errno));
109 exit(1);
110 }
111 }
112 else if (optind < argc) {
113 fprintf(stderr, "Unknown arguments found on commandline");
114 exit(1);
115 }
116 else in = stdin;
117/*
118 handle = iptc_init("filter");
119 if (!handle)
120 exit_error(VERSION_PROBLEM,
121 "can't initialize iptables-restore: %s",
122 iptc_strerror(errno));
123
124 if (!clean_slate(&handle))
125 exit_error(OTHER_PROBLEM, "Deleting old chains: %s",
126 iptc_strerror(errno));
127*/
128 /* Grab standard input. */
129 while (fgets(buffer, sizeof(buffer), in)) {
130 int ret;
131
132 line++;
133 if (buffer[0] == '\n') continue;
134 else if (buffer[0] == '#') {
135 if (verbose) fputs(buffer, stdout);
136 continue;
137 } else if (strcmp(buffer, "COMMIT\n") == 0) {
138 DEBUGP("Calling commit\n");
139 ret = ip6tc_commit(&handle);
140 } else if (buffer[0] == '*') {
141 /* New table */
142 char *table;
143
144 table = strtok(buffer+1, " \t\n");
145 DEBUGP("line %u, table '%s'\n", line, table);
146 if (!table) {
147 exit_error(PARAMETER_PROBLEM,
148 "%s: line %u table name invalid\n",
149 program_name, line);
150 exit(1);
151 }
152 strncpy(curtable, table, IP6T_TABLE_MAXNAMELEN);
153
154 handle = create_handle(table);
155 if (noflush == 0) {
156 DEBUGP("Cleaning all chains of table '%s'\n",
157 table);
158 for_each_chain(flush_entries, verbose, 1,
159 &handle);
160
161 DEBUGP("Deleting all user-defined chains "
162 "of table '%s'\n", table);
163 for_each_chain(delete_chain, verbose, 0,
164 &handle) ;
165 }
166
167 ret = 1;
168
169 } else if (buffer[0] == ':') {
170 /* New chain. */
171 char *policy, *chain;
172
173 chain = strtok(buffer+1, " \t\n");
174 DEBUGP("line %u, chain '%s'\n", line, chain);
175 if (!chain) {
176 exit_error(PARAMETER_PROBLEM,
177 "%s: line %u chain name invalid\n",
178 program_name, line);
179 exit(1);
180 }
181 strncpy(curchain, chain, IP6T_FUNCTION_MAXNAMELEN);
182
183 /* why the f... does iptc_builtin not work here ? */
184 /* FIXME: abort if chain creation fails --RR */
185// if (!iptc_builtin(curchain, &handle)) {
186 DEBUGP("Creating new chain '%s'\n", curchain);
187 if (!ip6tc_create_chain(curchain, &handle))
188 DEBUGP("unable to create chain '%s':%s\n", curchain,
189 strerror(errno));
190// }
191
192 policy = strtok(NULL, " \t\n");
193 DEBUGP("line %u, policy '%s'\n", line, policy);
194 if (!policy) {
195 exit_error(PARAMETER_PROBLEM,
196 "%s: line %u policy invalid\n",
197 program_name, line);
198 exit(1);
199 }
200
201 if (strcmp(policy, "-") != 0) {
202 struct ip6t_counters count;
203
204 if (counters) {
205 char *ctrs;
206 ctrs = strtok(NULL, " \t\n");
207
208 parse_counters(ctrs, &count);
209
210 } else {
211 memset(&count, 0,
212 sizeof(struct ip6t_counters));
213 }
214
215 DEBUGP("Setting policy of chain %s to %s\n",
216 chain, policy);
217
218 if (!ip6tc_set_policy(chain, policy, &count,
219 &handle))
220 exit_error(OTHER_PROBLEM,
221 "Can't set policy `%s'"
222 " on `%s' line %u: %s\n",
223 chain, policy, line,
224 ip6tc_strerror(errno));
225 }
226
227 ret = 1;
228
229 } else {
230 char *newargv[1024];
231 int i,a, argvsize;
232 char *ptr = buffer;
233 char *pcnt = NULL;
234 char *bcnt = NULL;
235
236 if (buffer[0] == '[') {
237 ptr = strchr(buffer, ']');
238 if (!ptr)
239 exit_error(PARAMETER_PROBLEM,
240 "Bad line %u: need ]\n",
241 line);
242 pcnt = strtok(buffer+1, ":");
243 bcnt = strtok(NULL, "]");
244 }
245
246 newargv[0] = argv[0];
247 newargv[1] = "-t";
248 newargv[2] = (char *) &curtable;
249 newargv[3] = "-A";
250 newargv[4] = (char *) &curchain;
251 argvsize = 5;
252
253 if (counters && pcnt && bcnt) {
254 newargv[5] = "--set-counters";
255 newargv[6] = (char *) pcnt;
256 newargv[7] = (char *) bcnt;
257 argvsize = 8;
258 }
259
260 // strtok initcialize!
261 if ( buffer[0]!='[' )
262 {
263 if (!(newargv[argvsize] = strtok(buffer, " \t\n")))
264 goto ImLaMeR;
265 //break;
266 argvsize++;
267 }
268
269 /* strtok: a function only a coder could love */
270 for (i = argvsize; i < sizeof(newargv)/sizeof(char *);
271 i++) {
272 if (!(newargv[i] = strtok(NULL, " \t\n")))
273 break;
274 ptr = NULL;
275 }
276ImLaMeR: if (i == sizeof(newargv)/sizeof(char *)) {
277 fprintf(stderr,
278 "%s: line %u too many arguments\n",
279 program_name, line);
280 exit(1);
281 }
282
283 DEBUGP("===>calling do_command6(%u, argv, &%s, handle):\n",
284 i, curtable);
285
286 for (a = 0; a <= i; a++)
287 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
288
289 ret = do_command6(i, newargv, &newargv[2], &handle);
290 }
291 if (!ret) {
292 fprintf(stderr, "%s: line %u failed\n",
293 program_name, line);
294 exit(1);
295 }
296 }
297
298 return 0;
299}