2 * ebtables-restore.c, October 2005
4 * Author: Bart De Schuymer
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.
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.
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.
25 #include "include/ebtables_u.h"
27 static struct ebt_u_replace replace
[3];
28 void ebt_early_init_once();
30 #define OPT_KERNELDATA 0x800 /* Also defined in ebtables.c */
32 static void copy_table_names()
34 strcpy(replace
[0].name
, "filter");
35 strcpy(replace
[1].name
, "nat");
36 strcpy(replace
[2].name
, "broute");
39 #define ebtrest_print_error(format, args...) do {fprintf(stderr, "ebtables-restore: "\
40 "line %d: "format".\n", line, ##args); exit(-1);} while (0)
41 int main(int argc_
, char *argv_
[])
43 char *argv
[EBTD_ARGC_MAX
], cmdline
[EBTD_CMDLINE_MAXLN
];
44 int i
, offset
, quotemode
= 0, argc
, table_nr
= -1, line
= 0, whitespace
;
45 char ebtables_str
[] = "ebtables";
48 ebtrest_print_error("options are not supported");
51 ebt_early_init_once();
52 argv
[0] = ebtables_str
;
54 while (fgets(cmdline
, EBTD_CMDLINE_MAXLN
, stdin
)) {
56 if (*cmdline
== '#' || *cmdline
== '\n')
58 *strchr(cmdline
, '\n') = '\0';
59 if (*cmdline
== '*') {
61 ebt_deliver_table(&replace
[table_nr
]);
62 ebt_deliver_counters(&replace
[table_nr
]);
64 for (i
= 0; i
< 3; i
++)
65 if (!strcmp(replace
[i
].name
, cmdline
+1))
68 ebtrest_print_error("table '%s' was not recognized", cmdline
+1);
70 replace
[table_nr
].command
= 11;
71 ebt_get_kernel_table(&replace
[table_nr
], 1);
72 replace
[table_nr
].command
= 0;
73 replace
[table_nr
].flags
= OPT_KERNELDATA
; /* Prevent do_command from initialising replace */
75 } else if (table_nr
== -1)
76 ebtrest_print_error("no table specified");
77 if (*cmdline
== ':') {
81 if (!(ch
= strchr(cmdline
, ' ')))
82 ebtrest_print_error("no policy specified");
84 for (i
= 0; i
< NUM_STANDARD_TARGETS
; i
++)
85 if (!strcmp(ch
+1, ebt_standard_targets
[i
])) {
87 if (policy
== EBT_CONTINUE
)
88 i
= NUM_STANDARD_TARGETS
;
91 if (i
== NUM_STANDARD_TARGETS
)
92 ebtrest_print_error("invalid policy specified");
93 /* No need to check chain name for consistency, since
94 * we're supposed to be reading an automatically generated
96 if ((chain_nr
= ebt_get_chainnr(&replace
[table_nr
], cmdline
+1)) == -1)
97 ebt_new_chain(&replace
[table_nr
], cmdline
+1, policy
);
99 replace
[table_nr
].chains
[chain_nr
]->policy
= policy
;
103 offset
= whitespace
= 0;
105 while (cmdline
[offset
] != '\0') {
106 if (cmdline
[offset
] == '\"') {
110 argv
[argc
++] = &cmdline
[offset
+1];
111 else if (cmdline
[offset
+1] != ' ' && cmdline
[offset
+1] != '\0')
112 ebtrest_print_error("syntax error at \"");
113 cmdline
[offset
] = '\0';
114 } else if (!quotemode
&& cmdline
[offset
] == ' ') {
116 cmdline
[offset
] = '\0';
117 } else if (whitespace
== 1) {
118 argv
[argc
++] = &cmdline
[offset
];
124 ebtrest_print_error("wrong use of '\"'");
125 optind
= 0; /* Setting optind = 1 causes serious annoyances */
126 do_command(argc
, argv
, EXEC_STYLE_DAEMON
, &replace
[table_nr
]);
127 ebt_reinit_extensions();
130 if (table_nr
!= -1) {
131 ebt_deliver_table(&replace
[table_nr
]);
132 ebt_deliver_counters(&replace
[table_nr
]);