-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpfwall_admin.c
272 lines (226 loc) · 5.24 KB
/
pfwall_admin.c
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
/*
All the admin actions are performed here
*/
#include "pfwall_admin.h"
#include "pfwall_public.h"
#define SYSFS_PATH "/sys/class/pfwall/pfwall_file"
void out_help()
{
// Help for various commands.
printf("Help.\n");
}
void out_list()
{
char buff[1024];
FILE *f = fopen(SYSFS_PATH, "r");
if (!f) {
fprintf(stderr, "Can't open %s\n.", SYSFS_PATH);
return;
}
while (fgets(buff, sizeof(buff) - 1, f) != NULL)
fputs(buff, stdout);
fclose(f);
}
void out_error()
{
// Take int argument for different error messages.
printf("Errors be here.\n");
}
// Initialize a PF_rule structure.
//backlash is used in order to separate different statement tht has to be written on single line
//part of awk GNU interpretation
#define PF_rule_new(pfrule) \
do { \
pfrule = calloc(1, sizeof(*pfrule)); \
if (!pfrule) \
return 1; \
} while (0);
// Rule structure -1: Error. 0: Output help. 1: Add rule. 2: Delete rule. 3: List rules. 4: Flush rules.
int parse_args(int argc, char **argv, struct PF_rule *pfrule)
{
int opt, arg_index;
// Command-line options.
struct option PF_options[] = {
{ "index", required_argument, NULL, 'x' },
{ "delete", required_argument, NULL, 'd' },
{ "action", required_argument, NULL, 'c' },
{ "direction", required_argument, NULL, 'e' },
{ "proto", required_argument, NULL, 'r' },
{ "srcip", required_argument, NULL, 'i' },
{ "dstip", required_argument, NULL, 'I' },
{ "srcport", required_argument, NULL, 'p' },
{ "dstport", required_argument, NULL, 'P' },
{ "list", no_argument, NULL, 'l' },
{ "flush", no_argument, NULL, 'f' },
{ "help", no_argument, NULL, 'h' },
{ 0, 0, 0, 0 }
};
if (!argv || !pfrule || argc < 2)
return -1;
// Parse arguments
opt = getopt_long(argc, argv, "lh", PF_options, &arg_index);
while (opt != -1)
{
int port, index;
__u32 ip;
switch (opt)
{
// index
case 'x':
index = atoi(optarg);
if (index < 0)
return -1;
pfrule->index = index;
break;
// direction
case 'e':
if (!strcasecmp(optarg, DIRECTION_ALL_STR))
pfrule->direction = DIRECTION_ALL;
else if (!strcasecmp(optarg, DIRECTION_IN_STR))
pfrule->direction = DIRECTION_IN;
else if (!strcasecmp(optarg, DIRECTION_OUT_STR))
pfrule->direction = DIRECTION_OUT;
else
return -1;
break;
// protocol
case 'r':
if (!strcasecmp(optarg, PROTO_ALL_STR))
pfrule->proto = PROTO_ALL;
else if (!strcasecmp(optarg, PROTO_TCP_STR))
pfrule->proto = PROTO_TCP;
else if (!strcasecmp(optarg, PROTO_UDP_STR))
pfrule->proto = PROTO_UDP;
else
return -1;
break;
// source IP
case 'i':
if (inet_pton(AF_INET, optarg, &ip) <= 0)
return -1;
pfrule->srcip = ip;
break;
// destination IP
case 'I':
if (inet_pton(AF_INET, optarg, &ip) <= 0)
return -1;
pfrule->dstip = ip;
break;
// sourceport
case 'p':
port = atoi(optarg);
if (port < 0 || port > PORT_MAX)
return -1;
pfrule->srcport = port;
break;
// destination port
case 'P':
port = atoi(optarg);
if (port < 0 || port > PORT_MAX)
return -1;
pfrule->dstport = port;
break;
// action
case 'c':
if (!strcasecmp(optarg, ACT_DROP_STR))
pfrule->action = ACT_DROP;
else if (!strcasecmp(optarg, ACT_PASS_STR))
pfrule->action = ACT_PASS;
else if (!strcasecmp(optarg, ACT_LOG_STR))
pfrule->action = ACT_LOG;
else
return -1;
break;
// delete
case 'd':
index = atoi(optarg);
if (index < 0)
return -1;
pfrule->index = index;
return 2;
// list
case 'l':
return 3;
// flush
case 'f':
return 4;
// help
case 'h':
return 0;
// error
case '?':
default:
return -1;
}
opt = getopt_long(argc, argv, "lh", PF_options, &arg_index);
}
// Default: Add rule
return 1;
}
// return error
int handle_cmd(int cmd, struct PF_rule *pfrule)
{
FILE *sysfs_file;
if (!pfrule)
return -1;
sysfs_file = fopen(SYSFS_PATH, "w");
if (!sysfs_file) {
fprintf(stderr, "Can't open %s\n.", SYSFS_PATH);
return -1;
}
switch (cmd)
{
case CMD_ADD:
// CMD_ADD INDEX ACT DIR PROTO SRCIP DSTIP SRCPORT DSTPORT
fprintf(sysfs_file, "%d %d %d %d %d %u %u %d %d", CMD_ADD,
pfrule->index, pfrule->action, pfrule->direction,
pfrule->proto, pfrule->srcip, pfrule->dstip,
pfrule->srcport, pfrule->dstport);
break;
case CMD_DEL:
// CMD_DEL INDEX
fprintf(sysfs_file, "%d %d", CMD_DEL, pfrule->index);
break;
case CMD_FLUSH:
// CMD_FLUSH
fprintf(sysfs_file, "%d", CMD_FLUSH);
break;
default:
fprintf(stderr, "Ambiguos command value %d.\n", cmd);
return -1;
}
return 0;
}
int main(int argc, char *argv[])
{
struct PF_rule *pfrule;
PF_rule_new(pfrule);
switch (parse_args(argc, argv, pfrule))
{
// Error
case -1:
out_error();
break;
// Output help
case 0:
out_help();
break;
// Add rule
case 1:
return handle_cmd(CMD_ADD, pfrule);
// Delete rule
case 2:
return handle_cmd(CMD_DEL, pfrule);
// List rules
case 3:
out_list();
break;
// List rules
case 4:
return handle_cmd(CMD_FLUSH, pfrule);
default:
fprintf(stderr, "Unknown parse_args() return value.\n");
return -1;
}
return 0;
}