From 933a89c8a18b9bb5376bee32313f061d624f66da Mon Sep 17 00:00:00 2001 From: Rory& Date: Tue, 13 May 2025 08:09:14 +0200 Subject: Pattern update, add cli flags --- main.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 102 insertions(+), 20 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index e59aef8..18e9f80 100644 --- a/main.c +++ b/main.c @@ -5,12 +5,13 @@ #include #include #include +#include #define ALLOWED_PATTERNS_FILE "allowed_patterns.txt" #define ALLOWED_IPS_FILE "allowed_ips.txt" #define PATTERNS_FILE "patterns.txt" -#define ACCESS_LOG_FILE "access.log.1" -// #define ACCESS_LOG_FILE "/var/log/nginx/access.log" +// #define ACCESS_LOG_FILE "access.log.1" +#define ACCESS_LOG_FILE "/var/log/nginx/access.log" typedef struct { @@ -23,11 +24,16 @@ typedef struct size_t bannedIpSize; } MatchRules; - bool print_allowed_ip = false; bool print_allowed_pattern = false; -bool print_banned_ip = false; -// bool print_allowed_ip = strcmp(getenv("PRINT_"), "1") == 0; +bool print_already_banned_ip = false; +bool print_indeterminate = false; +bool print_bans = true; + +char* allowed_patterns_path = ALLOWED_PATTERNS_FILE; +char* allowed_ips_path = ALLOWED_IPS_FILE; +char* patterns_path = PATTERNS_FILE; +char* access_log_path = ACCESS_LOG_FILE; void* xmalloc(size_t size) { @@ -40,6 +46,26 @@ void* xmalloc(size_t size) return ptr; } +void signal_handler(int signal_number) +{ + int wait_status; + pid_t return_pid = wait(&wait_status); + if (return_pid == -1) + { + perror("wait()"); + } + if (WIFEXITED(wait_status)) + { + printf("job [%d] | pid: %d | exit status: %d\n", signal_number, return_pid, WEXITSTATUS(wait_status)); + } + else + { + printf("exit abnormally\n"); + } + + fprintf(stderr, "the signal %d was received\n", signal_number); +} + void read_lines_cb(const char* filename, void cb(char*, void*), void* cbData) { if (cb == nullptr) @@ -199,19 +225,18 @@ void process_line(char* line, void* cbData) if (strstr(ip, rules->allowedIps[j]) == ip) { if (print_allowed_ip) - printf("Allowed IP: \"%s\" (~%s): %s\r", ip, rules->allowedIps[j], line); + printf("Allowed IP: \"%s\" (~%s): %s\n", ip, rules->allowedIps[j], line); free(ip); return; } } - // printf("Checking IP %s against %d banned IPs\n", ip, rules->bannedIpCount); for (size_t j = 0; j < rules->bannedIpCount; j++) { if (strstr(ip, rules->bannedIps[j]) == ip) { - if (print_banned_ip) - printf("Banned IP: \"%s\": %s\r", rules->bannedIps[j], line); + if (print_already_banned_ip) + printf("Banned IP: \"%s\": %s\n", rules->bannedIps[j], line); free(ip); return; } @@ -241,10 +266,9 @@ void process_line(char* line, void* cbData) rules->bannedIps[rules->bannedIpCount][strcspn(rules->bannedIps[rules->bannedIpCount], "\n")] = 0; rules->bannedIps[rules->bannedIpCount + 1] = nullptr; rules->bannedIpCount++; - printf("[Bans=%6lu] %15s matched pattern \"%s\": %s\n", rules->bannedIpCount, ip, rules->patterns[i], line); + if (print_bans) + printf("[Bans=%6lu] %15s matched pattern %4d (\"%s\"): %s\n", rules->bannedIpCount, ip, i, rules->patterns[i], line); - - signal(SIGCHLD, nullptr); __pid_t pid = fork(); if (pid == 0) { @@ -259,32 +283,90 @@ void process_line(char* line, void* cbData) perror("Fork failed"); exit(1); } + else + { + signal(SIGCHLD, SIG_IGN); + } return; } } + if (print_indeterminate) + printf("[Indeterminate]: %s\n", line); + free(ip); } - -int main(void) +int main(int argc, char* argv[]) { printf("Hello, World!\n"); + print_allowed_ip = getenv("D_PRINT_ALLOWED_IP") != NULL && strcmp(getenv("D_PRINT_ALLOWED_IP"), "1") == 0; print_allowed_pattern = getenv("D_PRINT_ALLOWED_PATTERN") != NULL && strcmp(getenv("D_PRINT_ALLOWED_PATTERN"), "1") == 0; - print_banned_ip = getenv("D_PRINT_BANNED_IP") != NULL && strcmp(getenv("D_PRINT_BANNED_IP"), "1") == 0; + print_already_banned_ip = getenv("D_PRINT_BANNED_IP") != NULL && strcmp(getenv("D_PRINT_BANNED_IP"), "1") == 0; + + for (int i = 1; i < argc; i++) + { + if (strcmp(argv[i], "--allowed-ips") == 0 && i + 1 < argc) + allowed_ips_path = argv[++i]; + else if (strcmp(argv[i], "--allowed-patterns") == 0 && i + 1 < argc) + allowed_patterns_path = argv[++i]; + else if (strcmp(argv[i], "--patterns") == 0 && i + 1 < argc) + patterns_path = argv[++i]; + else if (strcmp(argv[i], "--access-log") == 0 && i + 1 < argc) + access_log_path = argv[++i]; + else if (strstr(argv[i], "--print-allowed-ip") != NULL) + print_allowed_ip = strstr(argv[i], "=false") == NULL; + else if (strstr(argv[i], "--print-allowed-pattern") != NULL) + print_allowed_pattern = strstr(argv[i], "=false") == NULL; + else if (strstr(argv[i], "--print-already-banned-ip") != NULL) + print_already_banned_ip = strstr(argv[i], "=false") == NULL; + else if (strstr(argv[i], "--print-bans") != NULL) + print_bans = strstr(argv[i], "=false") == NULL; + else if (strstr(argv[i], "--print-indeterminate") != NULL) + print_indeterminate = strstr(argv[i], "=false") == NULL; + else if (strcmp(argv[i], "--debug") == 0) + { + print_allowed_ip = true; + print_allowed_pattern = true; + print_already_banned_ip = true; + print_indeterminate = true; + print_bans = true; + } + else + { + fprintf(stderr, "Unknown argument: %s\n", argv[i]); + return 1; + } + } + + if (strcmp(access_log_path, "stdin") == 0 || strcmp(access_log_path, "-") == 0) + { + access_log_path = "/dev/stdin"; + } + + fprintf(stderr, "allowed_patterns_path: %s\n", allowed_patterns_path); + fprintf(stderr, "allowed_ips_path: %s\n", allowed_ips_path); + fprintf(stderr, "patterns_path: %s\n", patterns_path); + fprintf(stderr, "access_log_path: %s\n", access_log_path); + + fprintf(stderr, "print_allowed_ip: %hhd\n", print_allowed_ip); + fprintf(stderr, "print_allowed_pattern: %hhd\n", print_allowed_pattern); + fprintf(stderr, "print_already_banned_ip: %hhd\n", print_already_banned_ip); + fprintf(stderr, "print_indeterminate: %hhd\n", print_indeterminate); + fprintf(stderr, "print_bans: %hhd\n", print_bans); MatchRules rules; - rules.patterns = read_lines(PATTERNS_FILE); - rules.allowed_patterns = read_lines(ALLOWED_PATTERNS_FILE); - rules.allowedIps = read_lines(ALLOWED_IPS_FILE); - rules.bannedIpSize = 64; + rules.patterns = read_lines(patterns_path); + rules.allowed_patterns = read_lines(allowed_patterns_path); + rules.allowedIps = read_lines(allowed_ips_path); + rules.bannedIpSize = 8; rules.bannedIpCount = 0; rules.bannedIps = xmalloc(sizeof(char**) * rules.bannedIpSize); //rules.bannedIps[0] = ; - read_lines_cb(ACCESS_LOG_FILE, process_line, &rules); + read_lines_cb(access_log_path, process_line, &rules); for (int i = 0; rules.bannedIps[i] != NULL; i++) { -- cgit 1.5.1