Merge commit 'origin/trunk@12184' into fedora
[iptables.git] / iptables-restore.c.counters
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,v 1.26 2003/05/02 15:30:11 laforge Exp $
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", 1, 0, 'v' },
31         { "help", 0, 0, 'h' },
32         { "noflush", 0, 0, 'n'},
33         { "modprobe", 1, 0, 'M'},
34         { 0 }
35 };
36
37 static void print_usage(const char *name, const char *version) __attribute__((noreturn));
38
39 static void print_usage(const char *name, const char *version)
40 {
41         fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-h]\n"
42                         "          [ --binary ]\n"
43                         "          [ --counters ]\n"
44                         "          [ --verbose ]\n"
45                         "          [ --help ]\n"
46                         "          [ --noflush ]\n"
47                         "          [ --modprobe=<command>]\n", name);
48                 
49         exit(1);
50 }
51
52 iptc_handle_t create_handle(const char *tablename, const char* modprobe )
53 {
54         iptc_handle_t handle;
55
56         handle = iptc_init(tablename);
57
58         if (!handle) {
59                 /* try to insmod the module if iptc_init failed */
60                 iptables_insmod("ip_tables", modprobe);
61                 handle = iptc_init(tablename);
62         }
63
64         if (!handle) {
65                 exit_error(PARAMETER_PROBLEM, "%s: unable to initialize"
66                         "table '%s'\n", program_name, tablename);
67                 exit(1);
68         }
69         return handle;
70 }
71
72 int parse_counters(char *string, struct ipt_counters *ctr)
73 {
74         return (sscanf(string, "[%llu:%llu]", &ctr->pcnt, &ctr->bcnt) == 2);
75 }
76
77 /* global new argv and argc */
78 static char *newargv[255];
79 static int newargc;
80
81 /* function adding one argument to newargv, updating newargc 
82  * returns true if argument added, false otherwise */
83 static int add_argv(char *what) {
84         DEBUGP("add_argv: %s\n", what);
85         if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) {
86                 newargv[newargc] = strdup(what);
87                 newargc++;
88                 return 1;
89         } else 
90                 return 0;
91 }
92
93 static void free_argv(void) {
94         int i;
95
96         for (i = 0; i < newargc; i++)
97                 free(newargv[i]);
98 }
99
100 int main(int argc, char *argv[])
101 {
102         iptc_handle_t handle = NULL;
103         char buffer[10240];
104         int c;
105         char curtable[IPT_TABLE_MAXNAMELEN + 1];
106         FILE *in;
107         const char *modprobe = 0;
108         int in_table = 0;
109
110         program_name = "iptables-restore";
111         program_version = IPTABLES_VERSION;
112         line = 0;
113
114 #ifdef NO_SHARED_LIBS
115         init_extensions();
116 #endif
117
118         while ((c = getopt_long(argc, argv, "bcvhnM:", options, NULL)) != -1) {
119                 switch (c) {
120                         case 'b':
121                                 binary = 1;
122                                 break;
123                         case 'c':
124                                 counters = 1;
125                                 break;
126                         case 'v':
127                                 verbose = 1;
128                                 break;
129                         case 'h':
130                                 print_usage("iptables-restore",
131                                             IPTABLES_VERSION);
132                                 break;
133                         case 'n':
134                                 noflush = 1;
135                                 break;
136                         case 'M':
137                                 modprobe = optarg;
138                                 break;
139                 }
140         }
141         
142         if (optind == argc - 1) {
143                 in = fopen(argv[optind], "r");
144                 if (!in) {
145                         fprintf(stderr, "Can't open %s: %s", argv[optind],
146                                 strerror(errno));
147                         exit(1);
148                 }
149         }
150         else if (optind < argc) {
151                 fprintf(stderr, "Unknown arguments found on commandline");
152                 exit(1);
153         }
154         else in = stdin;
155         
156         /* Grab standard input. */
157         while (fgets(buffer, sizeof(buffer), in)) {
158                 int ret = 0;
159
160                 line++;
161                 if (buffer[0] == '\n') continue;
162                 else if (buffer[0] == '#') {
163                         if (verbose) fputs(buffer, stdout);
164                         continue;
165                 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
166                         DEBUGP("Calling commit\n");
167                         ret = iptc_commit(&handle);
168                         in_table = 0;
169                 } else if ((buffer[0] == '*') && (!in_table)) {
170                         /* New table */
171                         char *table;
172
173                         table = strtok(buffer+1, " \t\n");
174                         DEBUGP("line %u, table '%s'\n", line, table);
175                         if (!table) {
176                                 exit_error(PARAMETER_PROBLEM, 
177                                         "%s: line %u table name invalid\n",
178                                         program_name, line);
179                                 exit(1);
180                         }
181                         strncpy(curtable, table, IPT_TABLE_MAXNAMELEN);
182
183                         if (handle)
184                                 iptc_free(&handle);
185
186                         handle = create_handle(table, modprobe);
187                         if (noflush == 0) {
188                                 DEBUGP("Cleaning all chains of table '%s'\n",
189                                         table);
190                                 for_each_chain(flush_entries, verbose, 1, 
191                                                 &handle);
192         
193                                 DEBUGP("Deleting all user-defined chains "
194                                        "of table '%s'\n", table);
195                                 for_each_chain(delete_chain, verbose, 0, 
196                                                 &handle) ;
197                         }
198
199                         ret = 1;
200                         in_table = 1;
201
202                 } else if ((buffer[0] == ':') && (in_table)) {
203                         /* New chain. */
204                         char *policy, *chain;
205
206                         chain = strtok(buffer+1, " \t\n");
207                         DEBUGP("line %u, chain '%s'\n", line, chain);
208                         if (!chain) {
209                                 exit_error(PARAMETER_PROBLEM,
210                                            "%s: line %u chain name invalid\n",
211                                            program_name, line);
212                                 exit(1);
213                         }
214
215                         if (!iptc_builtin(chain, handle)) {
216                                 DEBUGP("Creating new chain '%s'\n", chain);
217                                 if (!iptc_create_chain(chain, &handle)) 
218                                         exit_error(PARAMETER_PROBLEM, 
219                                                    "error creating chain "
220                                                    "'%s':%s\n", chain, 
221                                                    strerror(errno));
222                         }
223
224                         policy = strtok(NULL, " \t\n");
225                         DEBUGP("line %u, policy '%s'\n", line, policy);
226                         if (!policy) {
227                                 exit_error(PARAMETER_PROBLEM,
228                                            "%s: line %u policy invalid\n",
229                                            program_name, line);
230                                 exit(1);
231                         }
232
233                         if (strcmp(policy, "-") != 0) {
234                                 struct ipt_counters count;
235
236                                 if (counters) {
237                                         char *ctrs;
238                                         ctrs = strtok(NULL, " \t\n");
239
240                                         parse_counters(ctrs, &count);
241
242                                 } else {
243                                         memset(&count, 0, 
244                                                sizeof(struct ipt_counters));
245                                 }
246
247                                 DEBUGP("Setting policy of chain %s to %s\n",
248                                         chain, policy);
249
250                                 if (!iptc_set_policy(chain, policy, &count,
251                                                      &handle))
252                                         exit_error(OTHER_PROBLEM,
253                                                 "Can't set policy `%s'"
254                                                 " on `%s' line %u: %s\n",
255                                                 chain, policy, line,
256                                                 iptc_strerror(errno));
257                         }
258
259                         ret = 1;
260
261                 } else if (in_table) {
262                         int a;
263                         char *ptr = buffer;
264                         char *pcnt = NULL;
265                         char *bcnt = NULL;
266                         char *parsestart;
267
268                         /* the parser */
269                         char *param_start, *curchar;
270                         int quote_open;
271
272                         /* reset the newargv */
273                         newargc = 0;
274
275                         if (buffer[0] == '[') {
276                                 /* we have counters in our input */
277                                 ptr = strchr(buffer, ']');
278                                 if (!ptr)
279                                         exit_error(PARAMETER_PROBLEM,
280                                                    "Bad line %u: need ]\n",
281                                                    line);
282
283                                 pcnt = strtok(buffer+1, ":");
284                                 if (!pcnt)
285                                         exit_error(PARAMETER_PROBLEM,
286                                                    "Bad line %u: need :\n",
287                                                    line);
288
289                                 bcnt = strtok(NULL, "]");
290                                 if (!bcnt)
291                                         exit_error(PARAMETER_PROBLEM,
292                                                    "Bad line %u: need ]\n",
293                                                    line);
294
295                                 /* start command parsing after counter */
296                                 parsestart = ptr + 1;
297                         } else {
298                                 /* start command parsing at start of line */
299                                 parsestart = buffer;
300                         }
301
302                         add_argv(argv[0]);
303                         add_argv("-t");
304                         add_argv((char *) &curtable);
305                         
306                         if (counters && pcnt && bcnt) {
307                                 add_argv("--set-counters");
308                                 add_argv((char *) pcnt);
309                                 add_argv((char *) bcnt);
310                         }
311
312                         /* After fighting with strtok enough, here's now
313                          * a 'real' parser. According to Rusty I'm now no
314                          * longer a real hacker, but I can live with that */
315
316                         quote_open = 0;
317                         param_start = parsestart;
318                         
319                         for (curchar = parsestart; *curchar; curchar++) {
320                                 if (*curchar == '"') {
321                                         if (quote_open) {
322                                                 quote_open = 0;
323                                                 *curchar = ' ';
324                                         } else {
325                                                 quote_open = 1;
326                                                 param_start++;
327                                         }
328                                 } 
329                                 if (*curchar == ' '
330                                     || *curchar == '\t'
331                                     || * curchar == '\n') {
332                                         char param_buffer[1024];
333                                         int param_len = curchar-param_start;
334
335                                         if (quote_open)
336                                                 continue;
337
338                                         if (!param_len) {
339                                                 /* two spaces? */
340                                                 param_start++;
341                                                 continue;
342                                         }
343                                         
344                                         /* end of one parameter */
345                                         strncpy(param_buffer, param_start,
346                                                 param_len);
347                                         *(param_buffer+param_len) = '\0';
348
349                                         /* check if table name specified */
350                                         if (!strncmp(param_buffer, "-t", 3)
351                                             || !strncmp(param_buffer, "--table", 8)) {
352                                                 exit_error(PARAMETER_PROBLEM, 
353                                                    "Line %u seems to have a "
354                                                    "-t table option.\n", line);
355                                                 exit(1);
356                                         }
357
358                                         add_argv(param_buffer);
359                                         param_start += param_len + 1;
360                                 } else {
361                                         /* regular character, skip */
362                                 }
363                         }
364
365                         DEBUGP("calling do_command(%u, argv, &%s, handle):\n",
366                                 newargc, curtable);
367
368                         for (a = 0; a < newargc; a++)
369                                 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
370
371                         ret = do_command(newargc, newargv, 
372                                          &newargv[2], &handle);
373
374                         free_argv();
375                 }
376                 if (!ret) {
377                         fprintf(stderr, "%s: line %u failed\n",
378                                         program_name, line);
379                         exit(1);
380                 }
381         }
382
383         return 0;
384 }