summaryrefslogtreecommitdiffstats
path: root/ebtables-restore.c
blob: c0f9f6b7fa505f4d74855c33f346cbfa5c158c11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * ebtables-restore.c, October 2005
 *
 * Author: Bart De Schuymer
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "include/ebtables_u.h"

static struct ebt_u_replace replace[3];
void ebt_early_init_once();

#define OPT_KERNELDATA  0x800 /* Also defined in ebtables.c */

static void copy_table_names()
{
	strcpy(replace[0].name, "filter");
	strcpy(replace[1].name, "nat");
	strcpy(replace[2].name, "broute");
}

#define ebtrest_print_error(format, args...) {fprintf(stderr, "ebtables-restore: "\
                                             "line %d: "format".\n", line, ##args); exit(-1);}
int main(int argc_, char *argv_[])
{
	char *argv[EBTD_ARGC_MAX], cmdline[EBTD_CMDLINE_MAXLN];
	int i, offset, quotemode = 0, argc, table_nr = -1, line = 0, whitespace;
	char ebtables_str[] = "ebtables";

	if (argc_ != 1)
		ebtrest_print_error("options are not supported");
	ebt_silent = 0;
	copy_table_names();
	ebt_early_init_once();
	argv[0] = ebtables_str;

	while (fgets(cmdline, EBTD_CMDLINE_MAXLN, stdin)) {
		line++;
		if (*cmdline == '#' || *cmdline == '\n')
			continue;
		*strchr(cmdline, '\n') = '\0';
		if (*cmdline == '*') {
			if (table_nr != -1) {
				ebt_deliver_table(&replace[table_nr]);
				ebt_deliver_counters(&replace[table_nr]);
			}
			for (i = 0; i < 3; i++)
				if (!strcmp(replace[i].name, cmdline+1))
					break;
			if (i == 3)
				ebtrest_print_error("table '%s' was not recognized", cmdline+1);
			table_nr = i;
			replace[table_nr].command = 11;
			ebt_get_kernel_table(replace, 1);
			replace[table_nr].command = 0;
			replace[table_nr].flags = OPT_KERNELDATA; /* Prevent do_command from initialising replace */
			continue;
		} else if (table_nr == -1)
			ebtrest_print_error("no table specified\n");
		if (*cmdline == ':') {
			int policy;
			char *ch;

			if (!(ch = strchr(cmdline, ' ')))
				ebtrest_print_error("no policy specified");
			*ch = '\0';
			for (i = 0; i < NUM_STANDARD_TARGETS; i++)
				if (!strcmp(ch+1, ebt_standard_targets[i])) {
					policy = -i -1;
					if (policy == EBT_CONTINUE)
						i = NUM_STANDARD_TARGETS;
					break;
				}
			if (i == NUM_STANDARD_TARGETS)
				ebtrest_print_error("invalid policy specified");
			if (ebt_get_chainnr(&replace[table_nr], cmdline+1) == -1)
				ebt_new_chain(&replace[table_nr], cmdline+1, policy);
			continue;
		}
		argv[1] = cmdline;
		offset = whitespace = 0;
		argc = 2;
		while (cmdline[offset] != '\0') {
			if (cmdline[offset] == '\"') {
				whitespace = 0;
				quotemode ^= 1;
				if (quotemode)
					argv[argc++] = &cmdline[offset+1];
				cmdline[offset] = '\0';
			} else if (!quotemode && cmdline[offset] == ' ') {
				whitespace = 1;
				cmdline[offset] = '\0';
			} else if (whitespace == 1) {
				argv[argc++] = &cmdline[offset];
				whitespace = 0;
			}
			offset++;
		}
		if (quotemode)
			ebtrest_print_error("wrong use of '\"'");
		optind = 0; /* Setting optind = 1 causes serious annoyances */
		do_command(argc, argv, EXEC_STYLE_DAEMON, &replace[table_nr]);
		ebt_reinit_extensions();
	}

	if (table_nr != -1) {
		ebt_deliver_table(&replace[table_nr]);
		ebt_deliver_counters(&replace[table_nr]);
	}
	return 0;
}