-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtable_loader.c
101 lines (83 loc) · 2.4 KB
/
table_loader.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
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define TABLES_DIR "/var/pfpb/tables/"
#define PFCTL_CMD "pfctl"
extern int pfcount_main(void);
// Function prototypes
void load_pf_tables(DIR *dp);
void flush_pf_tables(DIR *dp);
int loader_main(const char *mode);
// Executes pfctl to load a table
void execute_pfctl_load(const char *filename) {
char command[512];
snprintf(command, sizeof(command), "%s -t %s -T add -f %s%s 2>/dev/null",
PFCTL_CMD, filename, TABLES_DIR, filename);
printf("Loading %s...\n", filename);
int ret = system(command);
if (ret != 0) {
fprintf(stderr, "Error loading table: %s\n", filename);
}
}
// Executes pfctl to flush a table
void execute_pfctl_flush(const char *filename) {
char command[512];
snprintf(command, sizeof(command), "%s -t %s -T kill 2>/dev/null", PFCTL_CMD, filename);
printf("Stopping %s...\n", filename);
int ret = system(command);
if (ret != 0) {
fprintf(stderr, "Error flushing table: %s\n", filename);
}
}
// Loads all tables from the directory
void load_pf_tables(DIR *dp) {
struct dirent *entry;
while ((entry = readdir(dp)) != NULL) {
// Skip "." and ".."
if (entry->d_name[0] == '.') {
continue;
}
// Execute pfctl for each file
execute_pfctl_load(entry->d_name);
}
}
// Flushes all tables from the directory
void flush_pf_tables(DIR *dp) {
struct dirent *entry;
while ((entry = readdir(dp)) != NULL) {
// Skip "." and ".."
if (entry->d_name[0] == '.') {
continue;
}
// Execute pfctl for each file
execute_pfctl_flush(entry->d_name);
}
}
// Main loader function to be called
int loader_main(const char *mode) {
DIR *dp = opendir(TABLES_DIR);
if (dp == NULL) {
perror("opendir");
return EXIT_FAILURE;
}
if (strcmp(mode, "start") == 0) {
load_pf_tables(dp);
pfcount_main();
printf("Done.\n");
} else if (strcmp(mode, "stop") == 0) {
flush_pf_tables(dp);
//pfcount_main();
printf("Total IP Ranges Blocked: 0\n");
printf("Done.\n");
} else {
fprintf(stderr, "Unknown mode: %s\n", mode);
closedir(dp);
return EXIT_FAILURE;
}
closedir(dp);
return EXIT_SUCCESS;
}