Bart De Schuymer | 005f638 | 2005-09-28 19:27:55 +0000 | [diff] [blame] | 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 "include/ebtables_u.h" |
| 26 | |
| 27 | static struct ebt_u_replace replace[3]; |
| 28 | void ebt_early_init_once(); |
| 29 | |
| 30 | #define OPT_KERNELDATA 0x800 /* Also defined in ebtables.c */ |
| 31 | |
| 32 | static void copy_table_names() |
| 33 | { |
| 34 | strcpy(replace[0].name, "filter"); |
| 35 | strcpy(replace[1].name, "nat"); |
| 36 | strcpy(replace[2].name, "broute"); |
| 37 | } |
| 38 | |
| 39 | int main(int argc_, char *argv_[]) |
| 40 | { |
| 41 | char *argv[EBTD_ARGC_MAX], cmdline[EBTD_CMDLINE_MAXLN]; |
| 42 | int i, offset, quotemode = 0, argc, table_nr = -1, line = 0, whitespace; |
| 43 | char ebtables_str[] = "ebtables"; |
| 44 | |
| 45 | ebt_silent = 0; |
| 46 | copy_table_names(); |
| 47 | ebt_early_init_once(); |
| 48 | argv[0] = ebtables_str; |
| 49 | |
| 50 | while (fgets(cmdline, EBTD_CMDLINE_MAXLN, stdin)) { |
| 51 | line++; |
| 52 | if (*cmdline == '#' || *cmdline == '\n') |
| 53 | continue; |
| 54 | *strchr(cmdline, '\n') = '\0'; |
| 55 | if (*cmdline == '*') { |
| 56 | if (table_nr != -1) { |
| 57 | ebt_deliver_table(&replace[table_nr]); |
| 58 | ebt_deliver_counters(&replace[table_nr]); |
| 59 | } |
| 60 | for (i = 0; i < 3; i++) |
| 61 | if (!strcmp(replace[i].name, cmdline+1)) |
| 62 | break; |
| 63 | if (i == 3) { |
| 64 | fprintf(stderr, "ebtables-restore: line %d: table '%s' was not recognized\n", line, cmdline+1); |
| 65 | exit(-1); |
| 66 | } |
| 67 | table_nr = i; |
| 68 | replace[table_nr].command = 11; |
| 69 | ebt_get_kernel_table(replace, 1); |
| 70 | replace[table_nr].command = 0; |
| 71 | replace[table_nr].flags = OPT_KERNELDATA; /* Prevent do_command from initialising replace */ |
| 72 | continue; |
| 73 | } else if (table_nr == -1) { |
| 74 | fprintf(stderr, "ebtables-restore: line %d: no table specified\n", line); |
| 75 | exit(-1); |
| 76 | } |
| 77 | if (*cmdline == ':') { |
| 78 | int policy; |
| 79 | char *ch; |
| 80 | |
| 81 | if (!(ch = strchr(cmdline, ' '))) { |
| 82 | fprintf(stderr, "ebtables-restore: line %d: no policy specified\n", line); |
| 83 | exit(-1); |
| 84 | } |
| 85 | *ch = '\0'; |
| 86 | for (i = 0; i < NUM_STANDARD_TARGETS; i++) |
| 87 | if (!strcmp(ch+1, ebt_standard_targets[i])) { |
| 88 | policy = -i -1; |
| 89 | if (policy == EBT_CONTINUE) |
| 90 | i = NUM_STANDARD_TARGETS; |
| 91 | break; |
| 92 | } |
| 93 | if (i == NUM_STANDARD_TARGETS) { |
| 94 | fprintf(stderr, "ebtables-restore: line %d: invalid policy specified\n", line); |
| 95 | exit(-1); |
| 96 | } |
| 97 | if (ebt_get_chainnr(&replace[table_nr], cmdline+1) == -1) { |
| 98 | ebt_new_chain(&replace[table_nr], cmdline+1, policy); |
| 99 | } |
| 100 | continue; |
| 101 | } |
| 102 | argv[1] = cmdline; |
| 103 | offset = whitespace = 0; |
| 104 | argc = 2; |
| 105 | while (cmdline[offset] != '\0') { |
| 106 | if (cmdline[offset] == '\"') { |
| 107 | whitespace = 0; |
| 108 | quotemode ^= 1; |
| 109 | if (quotemode) |
| 110 | argv[argc++] = &cmdline[offset+1]; |
| 111 | cmdline[offset] = '\0'; |
| 112 | } else if (!quotemode && cmdline[offset] == ' ') { |
| 113 | whitespace = 1; |
| 114 | cmdline[offset] = '\0'; |
| 115 | } else if (whitespace == 1) { |
| 116 | argv[argc++] = &cmdline[offset]; |
| 117 | whitespace = 0; |
| 118 | } |
| 119 | offset++; |
| 120 | } |
| 121 | if (quotemode) { |
| 122 | fprintf(stderr, "ebtables-restore: line %d: wrong use of '\"'\n", line); |
| 123 | exit(-1); |
| 124 | } |
| 125 | optind = 0; /* Setting optind = 1 causes serious annoyances */ |
| 126 | do_command(argc, argv, EXEC_STYLE_DAEMON, &replace[table_nr]); |
| 127 | ebt_reinit_extensions(); |
| 128 | } |
| 129 | |
| 130 | if (table_nr != -1) { |
| 131 | ebt_deliver_table(&replace[table_nr]); |
| 132 | ebt_deliver_counters(&replace[table_nr]); |
| 133 | } |
| 134 | return 0; |
| 135 | } |