-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpdscli.c
More file actions
332 lines (241 loc) · 7.46 KB
/
Copy pathpdscli.c
File metadata and controls
332 lines (241 loc) · 7.46 KB
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
/*
* Simple netfilter firewall for Linux
* by Jakub Vojvoda [vojvoda@swdeveloper.sk]
*
* file: pdscli.c
* - implementation of userspace application for communication with kernel module
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <errno.h>
#include <string.h>
#define YY_CURRENT_BUFFER 0
#define PROCF_NAME "/proc/xvojvo00_pdsprocfile"
#define CHAR_SEPARATORS 8
typedef struct {
char *number;
char *action;
char *protocol;
char *srcip;
char *dstip;
char *srcport;
char *dstport;
} firewall_rule;
typedef struct yy_buffer_state* YY_BUFFER_STATE;
extern int yyparse();
int parse_string(char *str);
int get_rule_size(firewall_rule rule);
char *encode_rule(firewall_rule rule);
firewall_rule decode_rule(char *str);
void print_rules();
int write_to_procf(char *str);
int main(int argc, char **argv)
{
int option;
int errval = 0;
while ((option = getopt(argc, argv, "f:pa:d:")) != -1) {
// nacitanie pravidiel zo vstupneho suboru
// jedno pravidlo na riadku
if (option == 'f') {
// otvorenie suboru
FILE *input = fopen(optarg, "r");
char *line = NULL;
size_t length = 0;
ssize_t chars;
if (input == NULL) {
fprintf(stderr, "%s: cannot open file %s\n", argv[0], optarg);
return 1;
}
// citanie suboru riadok po riadku
while ((chars = getline(&line, &length, input)) != -1) {
if (line[chars - 1] == '\n')
line[chars - 1] = '\0';
// overenie syntaktickej spravnosti pravidla
int accept = parse_string(line);
// dekodovanie a pridanie pravidla do modulu
if (accept == 0) {
firewall_rule rule = decode_rule(line);
char *er = encode_rule(rule);
write_to_procf(er);
free(er);
}
else {
fprintf(stderr, "%s: invalid format of line '%s'\n", argv[0], line);
errval = 1;
}
}
}
// vypis aktualnych pravidiel v module
else if (option == 'p') {
print_rules();
}
// pridanie pravidla do modulu
else if (option == 'a') {
// overenie syntaktickej spravnosti pravidla
int accept = parse_string(optarg);
// pridanie pravidla
if (accept == 0) {
firewall_rule rule = decode_rule(optarg);
char *er = encode_rule(rule);
write_to_procf(er);
free(er);
}
else {
fprintf(stderr, "%s: invalid format of argument '%s'\n", argv[0], optarg);
errval = 1;
}
}
// odstranenie pravidla z modulu
else if (option == 'd') {
char *endptr;
long int rule_number = strtol(optarg, &endptr, 10);
if (*endptr == '\0') {
int len = strlen(optarg) + 3;
char *stat = (char *) malloc(len * sizeof(char));
if (stat == NULL) {
fprintf(stderr, "%s: memory allocation error\n", argv[0]);
return 1;
}
// typ spravy a cislo pravidla
strcpy(stat, "d#");
strcat(stat, optarg);
stat[len - 1] = '\0';
// zapis do proc suboru
write_to_procf(stat);
free(stat);
}
else {
fprintf(stderr, "%s: invalid option -d argument\n", argv[0]);
errval = 1;
}
}
else {
return 2;
}
}
return errval;
}
/* get_rule_size
*
* - zistenie potrebneho poctu znakov pre spravu
*/
int get_rule_size(firewall_rule rule)
{
int length = strlen(rule.number);
length += strlen(rule.action);
length += strlen(rule.protocol);
length += strlen(rule.srcip);
length += strlen(rule.dstip);
length += strlen(rule.srcport);
length += strlen(rule.dstport);
return length + CHAR_SEPARATORS + 1;
}
/* encode_rule
*
* - zakodovanie pravidla do formatu urceneho
* k zapisu do proc suboru
*/
char *encode_rule(firewall_rule rule)
{
int length = get_rule_size(rule);
// typ spravy
char *str = (char *) malloc(length * sizeof(char));
strcpy(str, "a#");
// cislo pravidla
strcat(str, rule.number);
strcat(str, " ");
// akcia a protokol
strcat(str, rule.action);
strcat(str, " ");
strcat(str, rule.protocol);
strcat(str, " ");
// zdrojova a cielova ip adresa
strcat(str, rule.srcip);
strcat(str, " ");
strcat(str, rule.dstip);
strcat(str, " ");
// zdrojovy a cielovy port
strcat(str, rule.srcport);
strcat(str, " ");
strcat(str, rule.dstport);
strcat(str, "\0");
return str;
}
/* decode_rule
*
* - dekodovanie pravidla pre vypis na standard. vystup
*/
firewall_rule decode_rule(char *str)
{
char *item;
firewall_rule rule;
// cislo pravidla
rule.number = strtok(str, " ");
// akcia a protokol
rule.action = strtok(NULL, " ");
rule.protocol = strtok(NULL, " ");
// zdrojova a cielova ip adresa
strtok(NULL, " ");
rule.srcip = strtok(NULL, " ");
strtok(NULL, " ");
rule.dstip = strtok(NULL, " ");
// zdrojovy a cielovy port
item = strtok(NULL, " ");
rule.srcport = "-";
rule.dstport = "-";
if (item != NULL && strcmp(item, "src-port") == 0) {
rule.srcport = strtok(NULL, " ");
item = strtok(NULL, " ");
}
if (item != NULL && strcmp(item, "dst-port") == 0) {
rule.dstport = strtok(NULL, " ");
}
return rule;
}
/* parse_string
*
* - lexikalna a syntakticka analyza retazca
* reprezentujuceho pravidlo
*/
int parse_string(char *str)
{
yy_scan_string(str);
int parsing = yyparse();
yy_delete_buffer(YY_CURRENT_BUFFER);
return parsing;
}
/* print_rules
*
* - vypis pravidiel z proc suboru na standardny vystup
*/
void print_rules()
{
FILE *procf = fopen(PROCF_NAME, "r");
if (procf == NULL) {
fprintf(stderr, "pdscli: cannot open proc file\n");
return;
}
fprintf(stdout, "id\taction\tsrcip\tsrcport\tdstip\tdstport\tprotocol\n");
char c;
while ((c = fgetc(procf)) != EOF) {
fprintf(stdout, "%c", (char) c);
}
fclose(procf);
}
/* write_to_procf
*
* - zapis retazca do proc suboru
*/
int write_to_procf(char *str)
{
FILE *procf = fopen(PROCF_NAME, "w");
if (procf == NULL) {
fprintf(stderr, "pdscli: cannot open proc file\n");
return 1;
}
fprintf(procf, "%s", str);
fclose(procf);
return 0;
}