Merge to iptables-1.3.5
[iptables.git] / iptables-restore.c
1 /* Code to restore the iptables state, from file by iptables-save. 
2  * (C) 2000-2002 by Harald Welte <laforge@gnumonks.org>
3  * based on previous code from Rusty Russell <rusty@linuxcare.com.au>
4  *
5  * This code is distributed under the terms of GNU GPL v2
6  *
7  * $Id: iptables-restore.c 3980 2005-06-12 15:54:15Z /C=DE/ST=Berlin/L=Berlin/O=Netfilter Project/OU=Development/CN=kaber/emailAddress=kaber@netfilter.org $
8  */
9
10 #include <getopt.h>
11 #include <sys/errno.h>
12 #include <string.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include "iptables.h"
16 #include "libiptc/libiptc.h"
17
18 #ifdef DEBUG
19 #define DEBUGP(x, args...) fprintf(stderr, x, ## args)
20 #else
21 #define DEBUGP(x, args...) 
22 #endif
23
24 static int binary = 0, counters = 0, verbose = 0, noflush = 0;
25
26 /* Keeping track of external matches and targets.  */
27 static struct option options[] = {
28         { "binary", 0, 0, 'b' },
29         { "counters", 0, 0, 'c' },
30         { "verbose", 0, 0, 'v' },
31         { "test", 0, 0, 't' },
32         { "help", 0, 0, 'h' },
33         { "noflush", 0, 0, 'n'},
34         { "modprobe", 1, 0, 'M'},
35         { 0 }
36 };
37
38 static void print_usage(const char *name, const char *version) __attribute__((noreturn));
39
40 static void print_usage(const char *name, const char *version)
41 {
42         fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-t] [-h]\n"
43                         "          [ --binary ]\n"
44                         "          [ --counters ]\n"
45                         "          [ --verbose ]\n"
46                         "          [ --test ]\n"
47                         "          [ --help ]\n"
48                         "          [ --noflush ]\n"
49                         "          [ --modprobe=<command>]\n", name);
50                 
51         exit(1);
52 }
53
54 iptc_handle_t create_handle(const char *tablename, const char* modprobe )
55 {
56         iptc_handle_t handle;
57
58         handle = iptc_init(tablename);
59
60         if (!handle) {
61                 /* try to insmod the module if iptc_init failed */
62                 iptables_insmod("ip_tables", modprobe);
63                 handle = iptc_init(tablename);
64         }
65
66         if (!handle) {
67                 exit_error(PARAMETER_PROBLEM, "%s: unable to initialize"
68                         "table '%s'\n", program_name, tablename);
69                 exit(1);
70         }
71         return handle;
72 }
73
74 int parse_counters(char *string, struct ipt_counters *ctr)
75 {
76         return (sscanf(string, "[%llu:%llu]", (unsigned long long *)&ctr->pcnt, (unsigned long long *)&ctr->bcnt) == 2);
77 }
78
79 /* global new argv and argc */
80 static char *newargv[255];
81 static int newargc;
82
83 /* function adding one argument to newargv, updating newargc 
84  * returns true if argument added, false otherwise */
85 static int add_argv(char *what) {
86         DEBUGP("add_argv: %s\n", what);
87         if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) {
88                 newargv[newargc] = strdup(what);
89                 newargc++;
90                 return 1;
91         } else 
92                 return 0;
93 }
94
95 static void free_argv(void) {
96         int i;
97
98         for (i = 0; i < newargc; i++)
99                 free(newargv[i]);
100 }
101
102 #ifdef IPTABLES_MULTI
103 int
104 iptables_restore_main(int argc, char *argv[])
105 #else
106 int
107 main(int argc, char *argv[])
108 #endif
109 {
110         iptc_handle_t handle = NULL;
111         char buffer[10240];
112         int c;
113         char curtable[IPT_TABLE_MAXNAMELEN + 1];
114         FILE *in;
115         const char *modprobe = 0;
116         int in_table = 0, testing = 0;
117
118         program_name = "iptables-restore";
119         program_version = IPTABLES_VERSION;
120         line = 0;
121
122         lib_dir = getenv("IPTABLES_LIB_DIR");
123         if (!lib_dir)
124                 lib_dir = IPT_LIB_DIR;
125
126 #ifdef NO_SHARED_LIBS
127         init_extensions();
128 #endif
129
130         while ((c = getopt_long(argc, argv, "bcvthnM:", options, NULL)) != -1) {
131                 switch (c) {
132                         case 'b':
133                                 binary = 1;
134                                 break;
135                         case 'c':
136                                 counters = 1;
137                                 break;
138                         case 'v':
139                                 verbose = 1;
140                                 break;
141                         case 't':
142                                 testing = 1;
143                                 break;
144                         case 'h':
145                                 print_usage("iptables-restore",
146                                             IPTABLES_VERSION);
147                                 break;
148                         case 'n':
149                                 noflush = 1;
150                                 break;
151                         case 'M':
152                                 modprobe = optarg;
153                                 break;
154                 }
155         }
156         
157         if (optind == argc - 1) {
158                 in = fopen(argv[optind], "r");
159                 if (!in) {
160                         fprintf(stderr, "Can't open %s: %s", argv[optind],
161                                 strerror(errno));
162                         exit(1);
163                 }
164         }
165         else if (optind < argc) {
166                 fprintf(stderr, "Unknown arguments found on commandline");
167                 exit(1);
168         }
169         else in = stdin;
170         
171         /* Grab standard input. */
172         while (fgets(buffer, sizeof(buffer), in)) {
173                 int ret = 0;
174
175                 line++;
176                 if (buffer[0] == '\n')
177                         continue;
178                 else if (buffer[0] == '#') {
179                         if (verbose)
180                                 fputs(buffer, stdout);
181                         continue;
182                 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
183                         if (!testing) {
184                                 DEBUGP("Calling commit\n");
185                                 ret = iptc_commit(&handle);
186                         } else {
187                                 DEBUGP("Not calling commit, testing\n");
188                                 ret = 1;
189                         }
190                         in_table = 0;
191                 } else if ((buffer[0] == '*') && (!in_table)) {
192                         /* New table */
193                         char *table;
194
195                         table = strtok(buffer+1, " \t\n");
196                         DEBUGP("line %u, table '%s'\n", line, table);
197                         if (!table) {
198                                 exit_error(PARAMETER_PROBLEM, 
199                                         "%s: line %u table name invalid\n",
200                                         program_name, line);
201                                 exit(1);
202                         }
203                         strncpy(curtable, table, IPT_TABLE_MAXNAMELEN);
204                         curtable[IPT_TABLE_MAXNAMELEN] = '\0';
205
206                         if (handle)
207                                 iptc_free(&handle);
208
209                         handle = create_handle(table, modprobe);
210                         if (noflush == 0) {
211                                 DEBUGP("Cleaning all chains of table '%s'\n",
212                                         table);
213                                 for_each_chain(flush_entries, verbose, 1, 
214                                                 &handle);
215         
216                                 DEBUGP("Deleting all user-defined chains "
217                                        "of table '%s'\n", table);
218                                 for_each_chain(delete_chain, verbose, 0, 
219                                                 &handle) ;
220                         }
221
222                         ret = 1;
223                         in_table = 1;
224
225                 } else if ((buffer[0] == ':') && (in_table)) {
226                         /* New chain. */
227                         char *policy, *chain;
228
229                         chain = strtok(buffer+1, " \t\n");
230                         DEBUGP("line %u, chain '%s'\n", line, chain);
231                         if (!chain) {
232                                 exit_error(PARAMETER_PROBLEM,
233                                            "%s: line %u chain name invalid\n",
234                                            program_name, line);
235                                 exit(1);
236                         }
237
238                         if (iptc_builtin(chain, handle) <= 0) {
239                                 if (noflush && iptc_is_chain(chain, handle)) {
240                                         DEBUGP("Flushing existing user defined chain '%s'\n", chain);
241                                         if (!iptc_flush_entries(chain, &handle))
242                                                 exit_error(PARAMETER_PROBLEM,
243                                                            "error flushing chain "
244                                                            "'%s':%s\n", chain,
245                                                            strerror(errno));
246                                 } else {
247                                         DEBUGP("Creating new chain '%s'\n", chain);
248                                         if (!iptc_create_chain(chain, &handle))
249                                                 exit_error(PARAMETER_PROBLEM,
250                                                            "error creating chain "
251                                                            "'%s':%s\n", chain,
252                                                            strerror(errno));
253                                 }
254                         }
255
256                         policy = strtok(NULL, " \t\n");
257                         DEBUGP("line %u, policy '%s'\n", line, policy);
258                         if (!policy) {
259                                 exit_error(PARAMETER_PROBLEM,
260                                            "%s: line %u policy invalid\n",
261                                            program_name, line);
262                                 exit(1);
263                         }
264
265                         if (strcmp(policy, "-") != 0) {
266                                 struct ipt_counters count;
267
268                                 if (counters) {
269                                         char *ctrs;
270                                         ctrs = strtok(NULL, " \t\n");
271
272                                         parse_counters(ctrs, &count);
273
274                                 } else {
275                                         memset(&count, 0, 
276                                                sizeof(struct ipt_counters));
277                                 }
278
279                                 DEBUGP("Setting policy of chain %s to %s\n",
280                                         chain, policy);
281
282                                 if (!iptc_set_policy(chain, policy, &count,
283                                                      &handle))
284                                         exit_error(OTHER_PROBLEM,
285                                                 "Can't set policy `%s'"
286                                                 " on `%s' line %u: %s\n",
287                                                 chain, policy, line,
288                                                 iptc_strerror(errno));
289                         }
290
291                         ret = 1;
292
293                 } else if (in_table) {
294                         int a;
295                         char *ptr = buffer;
296                         char *pcnt = NULL;
297                         char *bcnt = NULL;
298                         char *parsestart;
299
300                         /* the parser */
301                         char *param_start, *curchar;
302                         int quote_open;
303
304                         /* reset the newargv */
305                         newargc = 0;
306
307                         if (buffer[0] == '[') {
308                                 /* we have counters in our input */
309                                 ptr = strchr(buffer, ']');
310                                 if (!ptr)
311                                         exit_error(PARAMETER_PROBLEM,
312                                                    "Bad line %u: need ]\n",
313                                                    line);
314
315                                 pcnt = strtok(buffer+1, ":");
316                                 if (!pcnt)
317                                         exit_error(PARAMETER_PROBLEM,
318                                                    "Bad line %u: need :\n",
319                                                    line);
320
321                                 bcnt = strtok(NULL, "]");
322                                 if (!bcnt)
323                                         exit_error(PARAMETER_PROBLEM,
324                                                    "Bad line %u: need ]\n",
325                                                    line);
326
327                                 /* start command parsing after counter */
328                                 parsestart = ptr + 1;
329                         } else {
330                                 /* start command parsing at start of line */
331                                 parsestart = buffer;
332                         }
333
334                         add_argv(argv[0]);
335                         add_argv("-t");
336                         add_argv((char *) &curtable);
337                         
338                         if (counters && pcnt && bcnt) {
339                                 add_argv("--set-counters");
340                                 add_argv((char *) pcnt);
341                                 add_argv((char *) bcnt);
342                         }
343
344                         /* After fighting with strtok enough, here's now
345                          * a 'real' parser. According to Rusty I'm now no
346                          * longer a real hacker, but I can live with that */
347
348                         quote_open = 0;
349                         param_start = parsestart;
350                         
351                         for (curchar = parsestart; *curchar; curchar++) {
352                                 if (*curchar == '"') {
353                                         /* quote_open cannot be true if there
354                                          * was no previous character.  Thus, 
355                                          * curchar-1 has to be within bounds */
356                                         if (quote_open && 
357                                             *(curchar-1) != '\\') {
358                                                 quote_open = 0;
359                                                 *curchar = ' ';
360                                         } else {
361                                                 quote_open = 1;
362                                                 param_start++;
363                                         }
364                                 } 
365                                 if (*curchar == ' '
366                                     || *curchar == '\t'
367                                     || * curchar == '\n') {
368                                         char param_buffer[1024];
369                                         int param_len = curchar-param_start;
370
371                                         if (quote_open)
372                                                 continue;
373
374                                         if (!param_len) {
375                                                 /* two spaces? */
376                                                 param_start++;
377                                                 continue;
378                                         }
379                                         
380                                         /* end of one parameter */
381                                         strncpy(param_buffer, param_start,
382                                                 param_len);
383                                         *(param_buffer+param_len) = '\0';
384
385                                         /* check if table name specified */
386                                         if (!strncmp(param_buffer, "-t", 3)
387                                             || !strncmp(param_buffer, "--table", 8)) {
388                                                 exit_error(PARAMETER_PROBLEM, 
389                                                    "Line %u seems to have a "
390                                                    "-t table option.\n", line);
391                                                 exit(1);
392                                         }
393
394                                         add_argv(param_buffer);
395                                         param_start += param_len + 1;
396                                 } else {
397                                         /* regular character, skip */
398                                 }
399                         }
400
401                         DEBUGP("calling do_command(%u, argv, &%s, handle):\n",
402                                 newargc, curtable);
403
404                         for (a = 0; a < newargc; a++)
405                                 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
406
407                         ret = do_command(newargc, newargv, 
408                                          &newargv[2], &handle);
409
410                         free_argv();
411                 }
412                 if (!ret) {
413                         fprintf(stderr, "%s: line %u failed\n",
414                                         program_name, line);
415                         exit(1);
416                 }
417         }
418         if (in_table) {
419                 fprintf(stderr, "%s: COMMIT expected at line %u\n",
420                                 program_name, line + 1);
421                 exit(1);
422         }
423
424         return 0;
425 }