Don't use 'release' macro.
[platform/upstream/ebtables.git] / ebtables-restore.c
1 /*
2  * ebtables-restore.c, October 2005
3  *
4  * Author: Bart De Schuymer
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include "include/ebtables_u.h"
27
28 static const struct option options[] = {
29         {.name = "noflush", .has_arg = 0, .val = 'n'},
30         { 0 }
31 };
32
33 static struct ebt_u_replace replace[3];
34 void ebt_early_init_once();
35
36 #define OPT_KERNELDATA  0x800 /* Also defined in ebtables.c */
37
38 static void print_usage()
39 {
40         fprintf(stderr, "Usage: ebtables-restore [ --noflush ]\n");
41         exit(1);
42 }
43
44 static void copy_table_names()
45 {
46         strcpy(replace[0].name, "filter");
47         strcpy(replace[1].name, "nat");
48         strcpy(replace[2].name, "broute");
49 }
50
51 #define ebtrest_print_error(format, args...) do {fprintf(stderr, "ebtables-restore: "\
52                                              "line %d: "format".\n", line, ##args); exit(-1);} while (0)
53 int main(int argc_, char *argv_[])
54 {
55         char *argv[EBTD_ARGC_MAX], cmdline[EBTD_CMDLINE_MAXLN];
56         int i, offset, quotemode = 0, argc, table_nr = -1, line = 0, whitespace, c, flush = 1;
57         char ebtables_str[] = "ebtables";
58
59         while ((c = getopt_long(argc_, argv_, "n", options, NULL)) != -1) {
60                 switch(c) {
61                         case 'n':
62                                 flush = 0;
63                                 break;
64                         default:
65                                 print_usage();
66                                 break;
67                 }
68         }
69
70         ebt_silent = 0;
71         copy_table_names();
72         ebt_early_init_once();
73         argv[0] = ebtables_str;
74
75         while (fgets(cmdline, EBTD_CMDLINE_MAXLN, stdin)) {
76                 line++;
77                 if (*cmdline == '#' || *cmdline == '\n')
78                         continue;
79                 *strchr(cmdline, '\n') = '\0';
80                 if (*cmdline == '*') {
81                         if (table_nr != -1) {
82                                 ebt_deliver_table(&replace[table_nr]);
83                                 ebt_deliver_counters(&replace[table_nr]);
84                         }
85                         for (i = 0; i < 3; i++)
86                                 if (!strcmp(replace[i].name, cmdline+1))
87                                         break;
88                         if (i == 3)
89                                 ebtrest_print_error("table '%s' was not recognized", cmdline+1);
90                         table_nr = i;
91                         replace[table_nr].command = 11;
92                         ebt_get_kernel_table(&replace[table_nr], flush);
93                         replace[table_nr].command = 0;
94                         replace[table_nr].flags = OPT_KERNELDATA; /* Prevent do_command from initialising replace */
95                         continue;
96                 } else if (table_nr == -1)
97                         ebtrest_print_error("no table specified");
98                 if (*cmdline == ':') {
99                         int policy, chain_nr;
100                         char *ch;
101
102                         if (!(ch = strchr(cmdline, ' ')))
103                                 ebtrest_print_error("no policy specified");
104                         *ch = '\0';
105                         for (i = 0; i < NUM_STANDARD_TARGETS; i++)
106                                 if (!strcmp(ch+1, ebt_standard_targets[i])) {
107                                         policy = -i -1;
108                                         if (policy == EBT_CONTINUE)
109                                                 i = NUM_STANDARD_TARGETS;
110                                         break;
111                                 }
112                         if (i == NUM_STANDARD_TARGETS)
113                                 ebtrest_print_error("invalid policy specified");
114                         /* No need to check chain name for consistency, since
115                          * we're supposed to be reading an automatically generated
116                          * file. */
117                         if ((chain_nr = ebt_get_chainnr(&replace[table_nr], cmdline+1)) == -1)
118                                 ebt_new_chain(&replace[table_nr], cmdline+1, policy);
119                         else
120                                 replace[table_nr].chains[chain_nr]->policy = policy;
121                         continue;
122                 }
123                 argv[1] = cmdline;
124                 offset = whitespace = 0;
125                 argc = 2;
126                 while (cmdline[offset] != '\0') {
127                         if (cmdline[offset] == '\"') {
128                                 whitespace = 0;
129                                 quotemode ^= 1;
130                                 if (quotemode)
131                                         argv[argc++] = &cmdline[offset+1];
132                                 else if (cmdline[offset+1] != ' ' && cmdline[offset+1] != '\0')
133                                         ebtrest_print_error("syntax error at \"");
134                                 cmdline[offset] = '\0';
135                         } else if (!quotemode && cmdline[offset] == ' ') {
136                                 whitespace = 1;
137                                 cmdline[offset] = '\0';
138                         } else if (whitespace == 1) {
139                                 argv[argc++] = &cmdline[offset];
140                                 whitespace = 0;
141                         }
142                         offset++;
143                 }
144                 if (quotemode)
145                         ebtrest_print_error("wrong use of '\"'");
146                 optind = 0; /* Setting optind = 1 causes serious annoyances */
147                 do_command(argc, argv, EXEC_STYLE_DAEMON, &replace[table_nr]);
148                 ebt_reinit_extensions();
149         }
150
151         if (table_nr != -1) {
152                 ebt_deliver_table(&replace[table_nr]);
153                 ebt_deliver_counters(&replace[table_nr]);
154         }
155         return 0;
156 }