blob: 088270a534ae899bf0c0a4232e1568e3421b3629 [file] [log] [blame]
Harald Welteae1ff9f2000-12-01 14:26:20 +00001/* Code to restore the iptables state, from file by iptables-save.
2 * (C) 2000 by Harald Welte <laforge@gnumonks.org>
3 * based on previous code from Rusty Russell <rusty@linuxcare.com.au>
4 *
5 * This coude is distributed under the terms of GNU GPL
6 */
7
Marc Bouchere6869a82000-03-20 06:03:29 +00008#include <getopt.h>
9#include <sys/errno.h>
10#include <string.h>
11#include <stdio.h>
Harald Welteae1ff9f2000-12-01 14:26:20 +000012#include <stdlib.h>
13#include "iptables.h"
14#include "libiptc/libiptc.h"
15
16#ifdef DEBUG
Harald Weltec45c3152000-12-06 08:11:24 +000017#define DEBUGP(x, args...) fprintf(stderr, x, ## args)
Harald Welteae1ff9f2000-12-01 14:26:20 +000018#else
Harald Weltec45c3152000-12-06 08:11:24 +000019#define DEBUGP(x, args...)
Harald Welteae1ff9f2000-12-01 14:26:20 +000020#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000021
22/* Keeping track of external matches and targets. */
23static struct option options[] = {
24 { "binary", 1, 0, 'b' },
25 { "counters", 1, 0, 'c' },
26 { "verbose", 1, 0, 'v' },
27 { "help", 1, 0, 'h' },
28 { 0 }
29};
30
31static void print_usage(const char *name, const char *version) __attribute__((noreturn));
32
33static void print_usage(const char *name, const char *version)
34{
35 fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-h]\n", name);
36 exit(1);
37}
38
Harald Welteae1ff9f2000-12-01 14:26:20 +000039iptc_handle_t create_handle(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +000040{
Harald Welteae1ff9f2000-12-01 14:26:20 +000041 iptc_handle_t handle;
Marc Bouchere6869a82000-03-20 06:03:29 +000042
Harald Welteae1ff9f2000-12-01 14:26:20 +000043 handle = iptc_init(tablename);
44 if (!handle) {
45 exit_error(PARAMETER_PROBLEM, "%s: unable to initialize"
46 "table '%s'\n", program_name, tablename);
47 exit(1);
Marc Bouchere6869a82000-03-20 06:03:29 +000048 }
Harald Welteae1ff9f2000-12-01 14:26:20 +000049 return handle;
Marc Bouchere6869a82000-03-20 06:03:29 +000050}
51
Harald Welteae1ff9f2000-12-01 14:26:20 +000052
Marc Bouchere6869a82000-03-20 06:03:29 +000053int main(int argc, char *argv[])
54{
55 iptc_handle_t handle;
56 char buffer[10240];
57 int counters = 0, binary = 0, verbose = 0;
58 unsigned int line = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +000059 char curtable[IPT_TABLE_MAXNAMELEN + 1];
60 char curchain[IPT_FUNCTION_MAXNAMELEN + 1];
Marc Bouchere6869a82000-03-20 06:03:29 +000061 FILE *in;
62
63 program_name = "iptables-restore";
64 program_version = NETFILTER_VERSION;
65
66 /* Don't use getopt here; it would interfere 8-(. */
67 if (optind == argc - 1) {
68 in = fopen(argv[optind], "r");
69 if (!in) {
70 fprintf(stderr, "Can't open %s: %s", argv[optind],
71 strerror(errno));
72 exit(1);
73 }
74 }
75 else if (optind < argc) {
76 fprintf(stderr, "Unknown arguments found on commandline");
77 exit(1);
78 }
79 else in = stdin;
Harald Welteae1ff9f2000-12-01 14:26:20 +000080/*
81 handle = iptc_init("filter");
Marc Bouchere6869a82000-03-20 06:03:29 +000082 if (!handle)
83 exit_error(VERSION_PROBLEM,
84 "can't initialize iptables-restore: %s",
85 iptc_strerror(errno));
86
87 if (!clean_slate(&handle))
88 exit_error(OTHER_PROBLEM, "Deleting old chains: %s",
89 iptc_strerror(errno));
Harald Welteae1ff9f2000-12-01 14:26:20 +000090*/
Marc Bouchere6869a82000-03-20 06:03:29 +000091 /* Grab standard input. */
92 while (fgets(buffer, sizeof(buffer), in)) {
93 int ret;
94
95 line++;
96 if (buffer[0] == '\n') continue;
97 else if (buffer[0] == '#') {
98 if (verbose) fputs(buffer, stdout);
99 continue;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000100 } else if (strcmp(buffer, "COMMIT\n") == 0) {
101 DEBUGP("Calling commit\n");
Marc Bouchere6869a82000-03-20 06:03:29 +0000102 ret = iptc_commit(&handle);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000103 } else if (buffer[0] == '*') {
104 /* New table */
105 char *table;
106
107 table = strtok(buffer+1, " \t\n");
108 DEBUGP("line %u, table '%s'\n", line, table);
109 if (!table) {
110 exit_error(PARAMETER_PROBLEM,
111 "%s: line %u table name invalid\n",
112 program_name, line);
113 exit(1);
114 }
115 strncpy(curtable, table, IPT_TABLE_MAXNAMELEN);
116
117 handle = create_handle(table);
118
119 DEBUGP("Cleaning all chains of table '%s'\n", table);
120 for_each_chain(flush_entries, verbose, 1, &handle) ;
121
122 DEBUGP("Deleting all user-defined chains of table '%s'\n", table);
123 for_each_chain(delete_chain, verbose, 0, &handle) ;
124
125 ret = 1;
126
127 } else if (buffer[0] == ':') {
Marc Bouchere6869a82000-03-20 06:03:29 +0000128 /* New chain. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000129 char *policy, *chain;
Marc Bouchere6869a82000-03-20 06:03:29 +0000130
131 /* FIXME: Don't ignore counters. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000132
Marc Bouchere6869a82000-03-20 06:03:29 +0000133 chain = strtok(buffer+1, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000134 DEBUGP("line %u, chain '%s'\n", line, chain);
Marc Bouchere6869a82000-03-20 06:03:29 +0000135 if (!chain) {
136 exit_error(PARAMETER_PROBLEM,
137 "%s: line %u chain name invalid\n",
138 program_name, line);
139 exit(1);
140 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000141 strncpy(curchain, chain, IPT_FUNCTION_MAXNAMELEN);
142
143 /* why the f... does iptc_builtin not work here ? */
144// if (!iptc_builtin(curchain, &handle)) {
145 DEBUGP("Creating new chain '%s'\n", curchain);
146 if (!iptc_create_chain(curchain, &handle))
147 DEBUGP("unable to create chain '%s':%s\n", curchain,
148 strerror(errno));
149// }
150
Marc Bouchere6869a82000-03-20 06:03:29 +0000151 policy = strtok(NULL, " \t\n");
Harald Welteae1ff9f2000-12-01 14:26:20 +0000152 DEBUGP("line %u, policy '%s'\n", line, policy);
Marc Bouchere6869a82000-03-20 06:03:29 +0000153 if (!policy) {
154 exit_error(PARAMETER_PROBLEM,
155 "%s: line %u policy invalid\n",
156 program_name, line);
157 exit(1);
158 }
Harald Welteae1ff9f2000-12-01 14:26:20 +0000159
160 if (strcmp(policy, "-") != 0) {
161
162 DEBUGP("Setting policy of chain %s to %s\n",
163 chain, policy);
164
165 if (!iptc_set_policy(chain, policy, &handle))
166 exit_error(OTHER_PROBLEM,
167 "Can't set policy `%s'"
168 " on `%s' line %u: %s\n",
169 chain, policy, line,
170 iptc_strerror(errno));
171 }
172
173 ret = 1;
174
Marc Bouchere6869a82000-03-20 06:03:29 +0000175 } else {
176 char *newargv[1024];
Harald Welteae1ff9f2000-12-01 14:26:20 +0000177 int i,a;
Marc Bouchere6869a82000-03-20 06:03:29 +0000178 char *ptr = buffer;
179
180 /* FIXME: Don't ignore counters. */
181 if (buffer[0] == '[') {
182 ptr = strchr(buffer, ']');
183 if (!ptr)
184 exit_error(PARAMETER_PROBLEM,
185 "Bad line %u: need ]\n",
186 line);
187 }
Rusty Russell7e53bf92000-03-20 07:03:28 +0000188
Marc Bouchere6869a82000-03-20 06:03:29 +0000189 newargv[0] = argv[0];
Harald Welteae1ff9f2000-12-01 14:26:20 +0000190 newargv[1] = "-t";
191 newargv[2] = (char *) &curtable;
192 newargv[3] = "-A";
193 newargv[4] = (char *) &curchain;
194
195 /* strtok: a function only a coder could love */
196 for (i = 5; i < sizeof(newargv)/sizeof(char *); i++) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000197 if (!(newargv[i] = strtok(ptr, " \t\n")))
198 break;
199 ptr = NULL;
200 }
201 if (i == sizeof(newargv)/sizeof(char *)) {
202 fprintf(stderr,
203 "%s: line %u too many arguments\n",
204 program_name, line);
205 exit(1);
206 }
207
Harald Welteae1ff9f2000-12-01 14:26:20 +0000208 DEBUGP("===>calling do_command(%u, argv, &%s, handle):\n",
209 i, curtable);
210
211 for (a = 0; a <= i; a++)
212 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
213
214 ret = do_command(i, newargv, &newargv[2], &handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000215 }
216 if (!ret) {
217 fprintf(stderr, "%s: line %u failed\n",
218 program_name, line);
219 exit(1);
220 }
221 }
222
223 return 0;
224}