summaryrefslogtreecommitdiffstats
path: root/ebtablesd.c
blob: 59bbe1ccfc31a29154d9901d2ca2fe9406a33f32 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 * ebtablesd.c, January 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 <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <errno.h>
#include "include/ebtables_u.h"

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

static struct ebt_u_replace replace[3];
#define OPEN_METHOD_FILE 1
#define OPEN_METHOD_KERNEL 2
static int open_method[3];
void ebt_early_init_once();

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

int main(int argc_, char *argv_[])
{
	char *argv[EBTD_ARGC_MAX], *args[4], name[] = "mkdir",
	     mkdir_option[] = "-p", mkdir_dir[] = EBTD_PIPE_DIR,
	     cmdline[EBTD_CMDLINE_MAXLN];
	int readfd, base = 0, offset = 0, n = 0, ret = 0;

	/* Make sure the pipe directory exists */
	args[0] = name;
	args[1] = mkdir_option;
	args[2] = mkdir_dir;
	args[3] = NULL;
	switch (fork()) {
	case 0:
		execvp(args[0], args);

		/* Not usually reached */
		exit(0);
	case -1:
		return -1;

	default: /* Parent */
		wait(NULL);
	}

	if (mkfifo(EBTD_PIPE, 0600) < 0 && errno != EEXIST) {
		printf("Error creating FIFO " EBTD_PIPE "\n");
		ret = -1;
		goto do_exit;
	}

	if ((readfd = open(EBTD_PIPE, O_RDONLY | O_NONBLOCK, 0)) == -1) {
		perror("open");
		ret = -1;
		goto do_exit;
	}

	if (signal(SIGPIPE, sigpipe_handler) == SIG_ERR) {
		perror("signal");
		ret = -1;
		goto do_exit;
	}

	ebt_silent = 1;

	copy_table_names();
	ebt_early_init_once();

	while (1) {
		int n2, i, argc, table_nr, ntot;

		/* base == 0 */
		ntot = read(readfd, cmdline+offset, EBTD_CMDLINE_MAXLN-offset-1);
		if (ntot <= 0)
			continue;
		ntot += offset;
continue_read:
		/* Change all ' ' into '\0'. This implies that the user is not
		 * allowed to use spaces (that don't distinguish options or
		 * commands) in her rules. This comes down to not allowing spaces
		 * in options like the string of --ulog-prefix (use '_' instead).
		 */
		for (; offset < ntot; n++, offset++) {
			if (cmdline[offset] == ' ')
				cmdline[offset] = '\0';
			if (cmdline[offset] == '\n') {
				cmdline[offset] = '\0';
				break;
			}
		}
		if (n == 0) {
			if (offset == ntot) {
				/* The ntot bytes were parsed and ended with '\n' */
				base = 0;
				offset = 0;
				continue;
			}
			offset++;
			base = offset;
			n = 0;
			goto continue_read;
		}
		if (offset == ntot) { /* The ntot bytes were parsed but no complete rule is yet specified */
			if (base == 0) {
				ebt_print_error("ebtablesd: the maximum command line length is %d", EBTD_CMDLINE_MAXLN-1);
				goto write_msg;
			}
			memmove(cmdline, cmdline+base+offset, ntot-offset);
			offset -= base;
			offset++;
			base = 0;
			continue;
		}

		table_nr = 0;
		n2 = 0;
		argc = 0;
		while (n2 < n && argc < EBTD_ARGC_MAX) {
			argv[argc++] = cmdline + base + n2;
			n2 += strlen(cmdline + base + n2) + 1;
		}
		offset++; /* Move past the '\n' */
		base = offset;

		if (argc > EBTD_ARGC_MAX) {
			ebt_print_error("ebtablesd: maximum %d arguments "
			                "allowed", EBTD_ARGC_MAX - 1);
			goto write_msg;
		}
		if (argc == 1) {
			ebt_print_error("ebtablesd: no arguments");
			goto write_msg;
		}

		/* Parse the options */
		if (!strcmp(argv[1], "-t")) {
			if (argc < 3) {
				ebt_print_error("ebtablesd: -t but no table");
				goto write_msg;
			}
			for (i = 0; i < 3; i++)
				if (!strcmp(replace[i].name, argv[2]))
					break;
			if (i == 3) {
				ebt_print_error("ebtablesd: table '%s' was "
				                "not recognized", argv[2]);
				goto write_msg;
			}
			table_nr = i;
		} else if (!strcmp(argv[1], "free")) {
			if (argc != 3) {
				ebt_print_error("ebtablesd: command free "
				                "needs exactly one argument");
				goto write_msg;
			}
			for (i = 0; i < 3; i++)
				if (!strcmp(replace[i].name, argv[2]))
					break;
			if (i == 3) {
				ebt_print_error("ebtablesd: table '%s' was "
				                "not recognized", argv[2]);
				goto write_msg;
			}
			if (!(replace[i].flags & OPT_KERNELDATA)) {
				ebt_print_error("ebtablesd: table %s has not "
				                "been opened");
				goto write_msg;
			}
			ebt_cleanup_replace(&replace[i]);
			copy_table_names();
			replace[i].flags &= ~OPT_KERNELDATA;
			goto write_msg;
		} else if (!strcmp(argv[1], "open")) {
			if (argc != 3) {
				ebt_print_error("ebtablesd: command open "
				                "needs exactly one argument");
				goto write_msg;
			}

			for (i = 0; i < 3; i++)
				if (!strcmp(replace[i].name, argv[2]))
					break;
			if (i == 3) {
				ebt_print_error("ebtablesd: table '%s' was "
				                "not recognized", argv[2]);
				goto write_msg;
			}
			if (replace[i].flags & OPT_KERNELDATA) {
				ebt_print_error("ebtablesd: table %s needs to "
				                "be freed before it can be "
				                "opened");
				goto write_msg;
			}
			if (!ebt_get_kernel_table(&replace[i], 0)) {
				replace[i].flags |= OPT_KERNELDATA;
				open_method[i] = OPEN_METHOD_KERNEL;
			}
			goto write_msg;
		} else if (!strcmp(argv[1], "fopen")) {
			struct ebt_u_replace tmp;

			memset(&tmp, 0, sizeof(tmp));
			if (argc != 4) {
				ebt_print_error("ebtablesd: command fopen "
				                "needs exactly two arguments");
				goto write_msg;
			}

			for (i = 0; i < 3; i++)
				if (!strcmp(replace[i].name, argv[2]))
					break;
			if (i == 3) {
				ebt_print_error("ebtablesd: table '%s' was "
				                "not recognized", argv[2]);
				goto write_msg;
			}
			if (replace[i].flags & OPT_KERNELDATA) {
				ebt_print_error("ebtablesd: table %s needs to "
				                "be freed before it can be "
				                "opened");
				goto write_msg;
			}
			tmp.filename = (char *)malloc(strlen(argv[3]) + 1);
			if (!tmp.filename) {
				ebt_print_error("Out of memory");
				goto write_msg;
			}
			strcpy(tmp.filename, argv[3]);
			strcpy(tmp.name, "filter");
			tmp.command = 'L'; /* Make sure retrieve_from_file()
			                    * doesn't complain about wrong
			                    * table name */

			ebt_get_kernel_table(&tmp, 0);
			free(tmp.filename);
			tmp.filename = NULL;
			if (ebt_errormsg[0] != '\0')
				goto write_msg;

			if (strcmp(tmp.name, argv[2])) {
				ebt_print_error("ebtablesd: opened file with "
				                "wrong table name '%s'", tmp.name);
				ebt_cleanup_replace(&tmp);
				goto write_msg;
			}
			replace[i] = tmp;
			replace[i].command = '\0';
			replace[i].flags |= OPT_KERNELDATA;
			open_method[i] = OPEN_METHOD_FILE;
			goto write_msg;
		} else if (!strcmp(argv[1], "commit")) {
			if (argc != 3) {
				ebt_print_error("ebtablesd: command commit "
				                "needs exactly one argument");
				goto write_msg;
			}

			for (i = 0; i < 3; i++)
				if (!strcmp(replace[i].name, argv[2]))
					break;
			if (i == 3) {
				ebt_print_error("ebtablesd: table '%s' was "
				                "not recognized", argv[2]);
				goto write_msg;
			}
			if (!(replace[i].flags & OPT_KERNELDATA)) {
				ebt_print_error("ebtablesd: table %s has not "
				                "been opened");
				goto write_msg;
			}
			/* The counters from the kernel are useless if we 
			 * didn't start from a kernel table */
			if (open_method[i] == OPEN_METHOD_FILE)
				replace[i].num_counters = 0;
			ebt_deliver_table(&replace[i]);
			if (ebt_errormsg[0] == '\0' && open_method[i] == OPEN_METHOD_KERNEL)
				ebt_deliver_counters(&replace[i], EXEC_STYLE_DAEMON);
			goto write_msg;
		} else if (!strcmp(argv[1], "fcommit")) {
			if (argc != 4) {
				ebt_print_error("ebtablesd: command commit "
				                "needs exactly two argument");
				goto write_msg;
			}

			for (i = 0; i < 3; i++)
				if (!strcmp(replace[i].name, argv[2]))
					break;
			if (i == 3) {
				ebt_print_error("ebtablesd: table '%s' was "
				                "not recognized", argv[2]);
				goto write_msg;
			}
			if (!(replace[i].flags & OPT_KERNELDATA)) {
				ebt_print_error("ebtablesd: table %s has not "
				                "been opened");
				goto write_msg;
			}
			replace[i].filename = (char *)malloc(strlen(argv[3]) + 1);
			if (!replace[i].filename) {
				ebt_print_error("Out of memory");
				goto write_msg;
			}
			strcpy(replace[i].filename, argv[3]);
			ebt_deliver_table(&replace[i]);
			if (ebt_errormsg[0] == '\0' && open_method[i] == OPEN_METHOD_KERNEL)
				ebt_deliver_counters(&replace[i], EXEC_STYLE_DAEMON);
			free(replace[i].filename);
			replace[i].filename = NULL;
			goto write_msg;
		}else if (!strcmp(argv[1], "quit")) {
			if (argc != 2) {
				ebt_print_error("ebtablesd: command quit does "
				                "not take any arguments");
				goto write_msg;
			}
			break;
		}
		if (!(replace[table_nr].flags & OPT_KERNELDATA)) {
			ebt_print_error("ebtablesd: table %s has not been "
			                "opened", replace[table_nr].name);
			goto write_msg;
		}
		optind = 0; /* Setting optind = 1 causes serious annoyances */
		do_command(argc, argv, EXEC_STYLE_DAEMON, &replace[table_nr]);
		ebt_reinit_extensions();
write_msg:
#ifndef SILENT_DAEMON
		if (ebt_errormsg[0] != '\0')
			printf("%s.\n", ebt_errormsg);
#endif
		ebt_errormsg[0]= '\0';
		n = 0;
		goto continue_read;
	}
do_exit:
	unlink(EBTD_PIPE);
	
	return 0;
}