-
Notifications
You must be signed in to change notification settings - Fork 1
/
m_autoban.c
410 lines (352 loc) · 10.1 KB
/
m_autoban.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "unrealircd.h"
#if __APPLE__
#define s6_addr16 __u6_addr.__u6_addr16
#endif
#if UnrealProtocol < 4200
#define BANPERMISSION "tkl:zline:global"
#else
#define BANPERMISSION "server-ban:zline:global"
#endif
CMD_FUNC(autoban_func);
int autoban_config_run (ConfigFile *cf, ConfigEntry *ce, int type);
int autoban_config_test (ConfigFile *cf, ConfigEntry *ce, int type, int *errs);
int subnet = 56;
char* defaultReason = "You have been issued a %s ban due to a terms of service violation.";
char* customBanReason = NULL;
struct IPUserInfo {
char* username;
char* ipAddress;
};
char* irccloudIPv4List[] = {
"5.254.36.57",
"5.254.36.58",
"5.254.36.59",
"5.254.36.60",
"5.254.36.61",
"5.254.36.62",
"5.254.36.105",
"5.254.36.106",
"5.254.36.107",
"5.254.36.108",
"5.254.36.109",
"5.254.36.110"
};
char* irccloudIPv6Subnet = "2a03:5180:f:";
/**
* Module information
*/
ModuleHeader MOD_HEADER = {
"third/m_autoban",
"2.0",
"Module that automatically retrieves user IP and performs a GZLine",
"Fuel Rats",
"unrealircd-6"
};
MOD_TEST() {
HookAdd(modinfo->handle, HOOKTYPE_CONFIGTEST, 0, autoban_config_test);
return MOD_SUCCESS;
}
/**
* Called when the module is initialised by UnrealIRCD
*/
MOD_INIT() {
HookAdd(modinfo->handle, HOOKTYPE_CONFIGRUN, 0, autoban_config_run);
CommandAdd(modinfo->handle, "AUTOBAN", autoban_func, 3, CMD_OPER);
CommandAdd(modinfo->handle, "AUTOBAHN", autoban_func, 3, CMD_OPER);
return MOD_SUCCESS;
}
/**
* Called when the module is loaded by UnrealIRCD
*/
MOD_LOAD() {
return MOD_SUCCESS;
}
MOD_UNLOAD() {
free(customBanReason);
return MOD_SUCCESS;
}
int autoban_config_test (ConfigFile *cf, ConfigEntry *ce, int type, int *errs) {
int errors = 0;
ConfigEntry *cep, *cep2;
if (type != CONFIG_SET) {
return 0;
}
if (!ce || !ce->name || strcmp(ce->name, "autoban")) {
return 0;
}
for (cep = ce->items; cep; cep = cep->next) {
if (!cep->name) {
config_error("%s:%i: blank set::autoban item",
cep->file->filename, cep->line_number);
errors += 1;
continue;
} else if (!strcmp(cep->name, "subnet")) {
if (cep->value == NULL || atoi(cep->value) == 0) {
config_error("%s:%i: expected a valid IPv6 subnet as integer set::autoban::%s",
cep->file->filename, cep->line_number, cep->name);
errors += 1;
}
} else if (!strcmp(cep->name, "message")) {
if (cep->value == NULL || strlen(cep->value) < 1) {
config_error("%s:%i: default ban message required set::autoban::%s",
cep->file->filename, cep->line_number, cep->name);
errors += 1;
}
} else {
config_error("%s:%i: unknown directive set::autoban::%s",
cep->file->filename, cep->line_number, cep->name);
errors += 1;
}
}
*errs = errors;
return errors ? -1 : 1;
}
int autoban_config_run (ConfigFile *cf, ConfigEntry *ce, int type) {
ConfigEntry *cep;
if (type != CONFIG_SET) {
return 0;
}
if (!ce || !ce->name || strcmp(ce->name, "autoban")) {
return 0;
}
for (cep = ce->items; cep; cep = cep->next) {
if (!strcmp(cep->name, "subnet")) {
subnet = atoi(cep->value);
} else if (!strcmp(cep->name, "message")) {
customBanReason = strdup(cep->value);
defaultReason = customBanReason;
}
}
return 1;
}
/**
* Check whether a string is a valid IPv4 address
* @param ipAddress a string possibly containing an IPv4 address
* @return whether the string is a valid IPv4 address
*/
bool isValidIpv4Address (char *ipAddress) {
struct sockaddr_in sa;
int result = inet_pton(AF_INET, ipAddress, &(sa.sin_addr));
return result != 0;
}
/**
* Check whether a string is a valid IPv6 address
* @param ipAddress a string possibly containing an IPv6 address
* @return whether the string is a valid IPv6 address
*/
bool isValidIpv6Address (char *ipAddress) {
struct sockaddr_in6 sa;
int result = inet_pton(AF_INET6, ipAddress, &(sa.sin6_addr));
return result != 0;
}
void substr (char* str, char* sub , int start, int len){
memcpy(sub, &str[start], len);
sub[len] = '\0';
}
/**
* Get an IPV6 ban mask wildcarded for the configured subnet
* @param ipAddress an IPv6 address
* @return A wildcarded IPV6 ban mask
*/
char* getIPv6BanRange (char *ipAddress) {
struct sockaddr_in6 result;
int success = inet_pton(AF_INET6, ipAddress, &(result.sin6_addr));
if (success != 1) {
return NULL;
}
uint16_t address[8];
memcpy(address, result.sin6_addr.s6_addr16, sizeof address);
// Switch byte order, UnrealIRCD expects network order
int i = 0;
while (i < 8) {
address[i] = htons(address[i]);
i += 1;
}
int range = subnet / 4;
int index = 0;
char* ipRange = malloc(sizeof (char) * 64);
ipRange[0] = '\0';
while (index < range) {
uint16_t group = address[(index / 4)];
int remainder = range - index;
char output[9];
// Our subnet division is inside the current group, calculate where and cut if off
if (remainder < 4) {
if (group == 0) {
sprintf(output, "*");
} else {
sprintf(output, "%x", group);
char delimitedOutput[8];
substr(output, delimitedOutput, 0, remainder);
sprintf(output, "%s%c", delimitedOutput, '*');
}
// Our subnet division is exactly at the end of the current group, cut it off
} else if (remainder == 4) {
sprintf(output, "%x:*", group);
}
// Our subnet is further along, add this group and move on to the next
else {
sprintf(output, "%x:", group);
}
strcat(ipRange, output);
index += 4;
}
return ipRange;
}
/**
* Check whether an IP belongs to IRCCloud
* @param address the IP address to check
* @return whether the IP address belongs to IRCCloud
*/
bool isIRCCloudAddress (const char *address) {
int index = 0;
int listLength = sizeof(irccloudIPv4List) / sizeof(irccloudIPv4List[0]);
while (index < listLength) {
if (strncmp(irccloudIPv4List[index], address, strlen(address)) == 0) {
return true;
}
index += 1;
}
return strncmp(address, irccloudIPv6Subnet, strlen(irccloudIPv6Subnet)) == 0;
}
/**
* Get IP information for an active user by their nickname
* @param nickname the user's nickname
* @return struct containing username (if defined) and ip address
*/
struct IPUserInfo getIPForNickname (char* nickname) {
struct IPUserInfo noInfo = { NULL, NULL };
struct Client* user = find_user(nickname, NULL);
char* username = NULL;
if (!user) {
return noInfo;
}
char* ipAddress = GetIP(user);
if (!ipAddress) {
return noInfo;
}
if (isIRCCloudAddress(ipAddress)) {
username = user->user->username;
}
struct IPUserInfo userInfo = { username, ipAddress };
return userInfo;
}
/**
* Generate a simple human readable time span
* @param seconds
* @return
*/
char* timespanFromSeconds (long seconds) {
char* timespan = malloc(64);
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;
if (seconds == 0) {
sprintf(timespan, "permanent");
} else if (days > 1) {
sprintf(timespan, "%ld day", days);
} else if (hours > 1) {
sprintf(timespan, "%ld hour", hours);
} else if (minutes > 1) {
sprintf(timespan, "%ld minute", minutes);
} else {
sprintf(timespan, "%ld second", seconds);
}
return timespan;
}
/**
* The function containing the actual logic for the /autoban command
*/
CMD_FUNC(autoban_func) {
if ((parc < 2) || BadPtr(parv[1])) {
sendnotice(client, "Error: Nick/IP required");
return;
}
if (IsServer(client)) {
return;
}
if (!ValidatePermissionsForPath(BANPERMISSION, client,NULL,NULL,NULL)) {
sendnumeric(client, ERR_NOPRIVILEGES);
return;
}
// Allow the user to pass an IP address, if input is not recognised as one assume it's a nickname
char* banTarget = (char *)parv[1];
char* username = "*";
if (!isValidIpv4Address(banTarget) && !isValidIpv6Address(banTarget)) {
struct IPUserInfo userInfo = getIPForNickname(banTarget);
banTarget = userInfo.ipAddress;
if (userInfo.username != NULL) {
username = userInfo.username;
}
} else if (isIRCCloudAddress(banTarget)) {
sendnotice(client, "Error: Do not directly ban IRCCloud IP addresses, use /autoban user");
return;
}
if (!banTarget) {
sendnotice(client, "Error: No valid ban target found, need an IP or an active user");
return;
}
char* ipRange = NULL;
// Get a correct ban mask for IPv6 address according to configured subnet
if (isValidIpv6Address(banTarget)) {
ipRange = getIPv6BanRange(banTarget);
if (!ipRange) {
sendnotice(client, "Error: Failed to create ban mask for IPv6 address");
return;
}
banTarget = ipRange;
}
long secs = 0;
char expireAt[1024], setAt[1024];
if (parc > 2) {
secs = config_checkval(parv[2], CFG_TIME);
if (secs < 0) {
sendnotice(client, "*** [error] The time you specified is out of range!");
return;
}
} else {
secs = 604800;
}
if (secs == 0) {
if (DEFAULT_BANTIME && (parc <= 3)) {
ircsnprintf(expireAt, sizeof(expireAt), "%li", DEFAULT_BANTIME + TStime());
} else {
ircsnprintf(expireAt, sizeof(expireAt), "%li", secs);
}
} else {
ircsnprintf(expireAt, sizeof(expireAt), "%li", secs + TStime());
}
ircsnprintf(setAt, sizeof(setAt), "%li", TStime());
char* banReason = defaultReason;
if (parc > 3) {
banReason = (char *)parv[3];
}
char* formattedTimeSpan = timespanFromSeconds(secs);
char formattedBanReason[512];
sprintf(formattedBanReason, banReason, formattedTimeSpan);
char* tkllayer[9] = {
me.name,
"+",
"G",
username,
banTarget,
make_nick_user_host(client->name, client->user->username, GetHost(client)),
expireAt,
setAt,
formattedBanReason
};
long i = atol(expireAt);
struct tm *t = gmtime(&i);
if (!t) {
sendnotice(client, "*** [error] The time you specified is out of range");
return;
}
cmd_tkl(&me, NULL, 9, (const char**)tkllayer);
if (ipRange != NULL) {
free(ipRange);
}
free(formattedTimeSpan);
}