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
|
/*
* Connection Manager
*
* Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
* Copyright (C) 2013 BMW Car IT GmbH.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <glib.h>
#include "../src/connman.h"
enum iptables_command {
IPTABLES_COMMAND_APPEND,
IPTABLES_COMMAND_INSERT,
IPTABLES_COMMAND_DELETE,
IPTABLES_COMMAND_POLICY,
IPTABLES_COMMAND_CHAIN_INSERT,
IPTABLES_COMMAND_CHAIN_DELETE,
IPTABLES_COMMAND_CHAIN_FLUSH,
IPTABLES_COMMAND_DUMP,
IPTABLES_COMMAND_UNKNOWN,
};
int main(int argc, char *argv[])
{
enum iptables_command cmd = IPTABLES_COMMAND_UNKNOWN;
char *table = NULL, *chain = NULL, *rule = NULL, *tmp;
int err, c, i;
opterr = 0;
while ((c = getopt_long(argc, argv,
"-A:I:D:P:N:X:F:Lt:", NULL, NULL)) != -1) {
switch (c) {
case 'A':
chain = optarg;
cmd = IPTABLES_COMMAND_APPEND;
break;
case 'I':
chain = optarg;
cmd = IPTABLES_COMMAND_INSERT;
break;
case 'D':
chain = optarg;
cmd = IPTABLES_COMMAND_DELETE;
break;
case 'P':
chain = optarg;
/* The policy will be stored in rule. */
cmd = IPTABLES_COMMAND_POLICY;
break;
case 'N':
chain = optarg;
cmd = IPTABLES_COMMAND_CHAIN_INSERT;
break;
case 'X':
chain = optarg;
cmd = IPTABLES_COMMAND_CHAIN_DELETE;
break;
case 'F':
chain = optarg;
cmd = IPTABLES_COMMAND_CHAIN_FLUSH;
break;
case 'L':
cmd = IPTABLES_COMMAND_DUMP;
break;
case 't':
table = optarg;
break;
default:
goto out;
}
}
out:
if (!table)
table = "filter";
for (i = optind - 1; i < argc; i++) {
if (rule) {
tmp = rule;
rule = g_strdup_printf("%s %s", rule, argv[i]);
g_free(tmp);
} else
rule = g_strdup(argv[i]);
}
__connman_iptables_init();
switch (cmd) {
case IPTABLES_COMMAND_APPEND:
err = __connman_iptables_append(table, chain, rule);
break;
case IPTABLES_COMMAND_INSERT:
err = __connman_iptables_insert(table, chain, rule);
break;
case IPTABLES_COMMAND_DELETE:
err = __connman_iptables_delete(table, chain, rule);
break;
case IPTABLES_COMMAND_POLICY:
err = __connman_iptables_change_policy(table, chain, rule);
break;
case IPTABLES_COMMAND_CHAIN_INSERT:
err = __connman_iptables_new_chain(table, chain);
break;
case IPTABLES_COMMAND_CHAIN_DELETE:
err = __connman_iptables_delete_chain(table, chain);
break;
case IPTABLES_COMMAND_CHAIN_FLUSH:
err = __connman_iptables_flush_chain(table, chain);
break;
case IPTABLES_COMMAND_DUMP:
__connman_log_init(argv[0], "*", false, false,
"iptables-test", "1");
err = __connman_iptables_dump(table);
break;
case IPTABLES_COMMAND_UNKNOWN:
printf("Missing command\n");
printf("usage: iptables-test [-t table] {-A|-I|-D} chain rule\n");
printf(" iptables-test [-t table] {-N|-X|-F} chain\n");
printf(" iptables-test [-t table] -L\n");
printf(" iptables-test [-t table] -P chain target\n");
exit(-EINVAL);
}
if (err < 0) {
printf("Error: %s\n", strerror(-err));
exit(err);
}
err = __connman_iptables_commit(table);
if (err < 0) {
printf("Failed to commit changes: %s\n", strerror(-err));
exit(err);
}
g_free(rule);
__connman_iptables_cleanup();
return 0;
}
|