-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoBlock.h
More file actions
222 lines (171 loc) · 6.63 KB
/
Copy pathAutoBlock.h
File metadata and controls
222 lines (171 loc) · 6.63 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
/*
This source file is part of the AutoBlock project.
Copyright (C) 2026 Dennis Hawkins
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
If you use this code in any way, I would love to hear from you. My email
address is: dennis@galliform.com
*/
#define VERSION "AutoBlock v1.5.0"
#define VERSION2 "Copyright 2026 by Dennis Hawkins (n4mwd@yahoo.com)"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#if defined(__BORLANDC__)
#define CONFIGPATH "./AutoBlock.conf" // config location
#define DEFAULTLOGFILE "./AutoBlock.log" // default until changed
#define SRCPATH "Bad-Ips.txt" // local bad ip file sent to router
#define MAINPATH "Whois.txt" // whois file
#else
#define CONFIGPATH "/etc/AutoBlock.conf" // config location
#define DEFAULTLOGFILE "/var/log/AutoBlock.log" // default until changed
#define SRCPATH "/root/AutoBlock/Bad-Ips.txt" // local bad ip file sent to router
#define MAINPATH "/root/AutoBlock/Whois.txt" // whois file
#endif
#define MIN(X,Y) ((X)<(Y)?(X):(Y))
#if defined(__BORLANDC__)
#include <winsock.h>
#define SOCKET_TYPE SOCKET
#define INVALID_SOCKET_VAL INVALID_SOCKET
#define CLOSE_SOCKET(s) closesocket(s)
//#define PRINT_NET_ERROR(m) fprintf(stderr, "%s. Winsock Error: %d\n", m, WSAGetLastError())
#define snprintf(buf, len, fmt,data) sprintf(buf, fmt, data)
#define sleep(x) Sleep(x * 1000)
#define SetNetTmo(sk, sc) { DWORD timeout = (sc * 1000); \
setsockopt(sk, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(timeout)); }
#define CLOSE_NETWORK() {WSACleanup();}
#define popen _popen
#define pclose _pclose
#define WEXITSTATUS(status) status
typedef unsigned __int64 QWORD;
typedef unsigned char BYTE;
typedef unsigned char bool;
#define false 0
#define FALSE 0
#define true 1
#define TRUE 1
#else
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/wait.h>
#define SOCKET_TYPE int
#define INVALID_SOCKET_VAL -1
#define CLOSE_SOCKET(s) close(s)
//#define PRINT_NET_ERROR(m) perror(m)
#define SetNetTmo(sk, sc) { struct timeval tv = {sc, 0}; \
setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv)); }
#define CLOSE_NETWORK() {/* */}
#define snprintf snprintf
#define strncmpi strncasecmp
#define stricmp strcasecmp
typedef uint64_t QWORD;
typedef uint32_t DWORD;
typedef uint16_t WORD;
typedef uint8_t BYTE;
#endif
#define HASH_SIZE 256 // size of hash table
#define PATH_SIZE 512 // File/cmd path length
// Definition of Bitmask Positions
#define BIT_NOTICE ((DWORD)(1 << 0)) // 0x01
#define BIT_REGISTER ((DWORD)(1 << 1)) // 0x02
#define BIT_INVITE ((DWORD)(1 << 2)) // 0x04
#define BIT_OPTIONS ((DWORD)(1 << 3)) // 0x08
#define BIT_ID_IS_ALPHA ((DWORD)(1 << 4)) // 0x10
#define BIT_ID_IS_NUM ((DWORD)(1 << 5)) // 0x20
#define BIT_ADDR_IS_IP ((DWORD)(1 << 6)) // 0x40
#define BIT_ADDR_IS_NAME ((DWORD)(1 << 7)) // 0x80
#define BIT_FAILED_FOR ((DWORD)(1 << 8)) // 0x100
#define BIT_FAILED_TO_AUTHENTICATE ((DWORD)(1 << 9)) // 0x200
#define BIT_NO_MATCHING_ENDPOINT ((DWORD)(1 << 10))// 0x400
#define REQTYPE_ALL (BIT_REGISTER | BIT_INVITE | BIT_OPTIONS)
// Verbosity values
enum {QUIET, FATAL, WARN, STATUS };
// Type definitions
// A binary trie node representing a bit path (0 or 1)
typedef struct TrieNode
{
struct TrieNode *children[2];
DWORD count[2];
bool is_block; // Marks if this specific prefix represents an active netblock
WORD DateAdded;
} TrieNode;
typedef struct
{
int first, last;
} RANGE;
typedef struct
{
DWORD IP;
DWORD bits;
} IPTYPE;
extern bool G_DryRun;
extern int P_DryRun;
extern char G_LogFilePath[PATH_SIZE];
extern char G_AsteriskNoticeFile[PATH_SIZE];
extern char G_FirewallBlockListPath[PATH_SIZE];
extern char G_TransferCmd[PATH_SIZE];
extern char G_ReloadCmd[PATH_SIZE];
extern DWORD G_Verbosity;
extern int P_Verbosity;
extern DWORD G_RequestTypes;
extern int G_AllowNamedServer;
extern RANGE *G_AllowedNumericalIds;
extern int G_NumAllowedNumericalIds;
extern IPTYPE *G_IgnoreBlocks;
extern int G_NumIgnoreBlocks;
extern int G_MinCidr;
extern int G_MaxCidr;
extern bool G_UseWhois;
extern IPTYPE *G_Imports;
extern int G_NumImports;
extern int G_Compress;
extern int G_Expire;
// Functions
void PrintErr(DWORD level, const char *fmt, ...);
bool insert_netblock(TrieNode* root, IPTYPE ip, WORD DateStamp);
TrieNode* create_node(void);
int Str2Ip(const char *ip_str, IPTYPE *ip);
char *IP2Str(IPTYPE ip);
DWORD ValidateHacker(char *line, DWORD *flags);
void destroy_hash_table(void);
int HashFlat(void);
int HashMessages(void);
DWORD GetNextHash(int init);
int query_radb_netblock(char *ip_address, IPTYPE *ip);
bool is_ip_covered(TrieNode* root, DWORD ip, WORD DateStamp);
void free_subtree(TrieNode* node);
void export_netblocks_to_file(TrieNode* root, const char* filename, WORD DateStamp);
DWORD process_ip_file(const char *filename, TrieNode *root);
DWORD Domain2Ip(char *str);
int ParseConfig(const char *filepath);
int SearchID(int ext);
void FreeConfig(void);
int whitelist_netblock(TrieNode* root, IPTYPE ip);
bool is_ip_whitelisted(DWORD ip);
int connect_to_server(char *url, int port);
int NetRead(int sock_fd, char *Buf, int BufSize);
int ReadImports(char *path);
int ImportIPs(TrieNode* root, WORD DateStamp);
char *trim(char *s);
void CompressTrie(TrieNode *node);
bool is_root(void);
bool IpMaskMatch(IPTYPE block_ip, DWORD match_ip);
WORD GetDateStamp(void);
WORD ReadWhoisIp(const char *ip_str, IPTYPE *ip);
int ProcessCmdLine(int argc, char *argv[]);