-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblsb.c
More file actions
580 lines (530 loc) · 16.3 KB
/
blsb.c
File metadata and controls
580 lines (530 loc) · 16.3 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/* NeoStats - IRC Statistical Services
** Copyright (c) 1999-2005 Adam Rutter, Justin Hammond, Mark Hetherington
** http://www.neostats.net/
**
** Additions (v0.1b1) Copyright (c) 2014 DarkSpirit IRC Network
** http://www.darkspirit.org/
**
** 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
**
** NeoStats CVS Identification
** $Id: blsb.c 41 2005-12-12 12:57:28Z DNB $
*/
/* TODO:
* - Akill support.
* - Buffer sizes for name and domain should not need to be so big
* and should be made a more appropriate size.
* - If a remove akill command is added, it must check whether an akill
* was added by blsb before removing it otherwise blsb becomes a way
* for opers to remove any akill on the network including those they
* may not normally have access to.
* - Do we need cache support?.
*/
#include "neostats.h"
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_ARPA_NAMESER_H
#include <arpa/nameser.h>
#endif
#include "blsb.h"
static int event_nickip( const CmdParams *cmdparams );
static int blsb_cmd_list( const CmdParams *cmdparams );
static int blsb_cmd_add( const CmdParams *cmdparams );
static int blsb_cmd_del( const CmdParams *cmdparams );
static int blsb_cmd_check( const CmdParams *cmdparams );
static int blsb_set_exclusions_cb( const CmdParams *cmdparams, SET_REASON reason );
static struct blsb {
int akilltime;
int doakill;
int verbose;
int exclusions;
list_t *domains;
} blsb;
static Bot *blsb_bot;
static dom_list stddomlist[] =
{
{"Blitzed_OPM", "opm.blitzed.org", BL_LOOKUP_TXT_RECORD, "Open proxy - see http://opm.blitzed.org/%s", 0},
{"Secure-IRC", "bl.irc-chat.net", BL_LOOKUP_TXT_RECORD, "Insecure Host - See http://secure.irc-chat.net/ipinfo.php?ip=%s", 0},
{"Tor_Exit_Server", "tor.dnsbl.sectoor.de", BL_LOOKUP_TXT_RECORD, "Your Host is a Tor Exit Server", 0},
{"", "", 0}
};
/** Copyright info */
static const char *blsb_copyright[] =
{
"Copyright (c) 1999-2005, NeoStats",
"http://www.neostats.net/",
NULL
};
/** Module Info definition
* This describes the module to the NeoStats core and provides information
* to end users when modules are queried.
* The structure is required but some fields are optional.
*/
ModuleInfo module_info =
{
"BLSB",
"Black List Scanning Bot",
blsb_copyright,
blsb_about,
NEOSTATS_VERSION,
MODULE_VERSION,
__DATE__,
__TIME__,
MODULE_FLAG_LOCAL_EXCLUDES,
0,
0,
};
static bot_cmd blsb_commands[]=
{
{"ADD", blsb_cmd_add, 4, NS_ULEVEL_ADMIN, blsb_help_add, 0, NULL, NULL},
{"DEL", blsb_cmd_del, 1, NS_ULEVEL_ADMIN, blsb_help_del, 0, NULL, NULL},
{"LIST", blsb_cmd_list, 0, NS_ULEVEL_ADMIN, blsb_help_list, 0, NULL, NULL},
{"CHECK", blsb_cmd_check, 1, NS_ULEVEL_OPER, blsb_help_check, 0, NULL, NULL},
NS_CMD_END()
};
static bot_setting blsb_settings[]=
{
{"AKILL", &blsb.doakill, SET_TYPE_BOOLEAN, 0, 0, NS_ULEVEL_ADMIN, NULL, blsb_help_set_akill, NULL, (void*)1 },
{"AKILLTIME", &blsb.akilltime, SET_TYPE_INT, 0, 20736000, NS_ULEVEL_ADMIN, NULL, blsb_help_set_akilltime, NULL, (void*)TS_ONE_DAY },
{"VERBOSE", &blsb.verbose, SET_TYPE_BOOLEAN, 0, 0, NS_ULEVEL_ADMIN, NULL, blsb_help_set_verbose, NULL, (void*)1 },
{"EXCLUSIONS", &blsb.exclusions, SET_TYPE_BOOLEAN, 0, 0, NS_ULEVEL_ADMIN, NULL, blsb_help_set_exclusions, blsb_set_exclusions_cb, (void *)0 },
NS_SETTING_END()
};
/** BotInfo */
static BotInfo blsb_botinfo =
{
"blsb",
"blsb1",
"blsb",
BOT_COMMON_HOST,
"BlackList Scanning Bot",
BOT_FLAG_SERVICEBOT|BOT_FLAG_RESTRICT_OPERS|BOT_FLAG_DEAF,
blsb_commands,
blsb_settings,
};
/** Module event list
* What events we will act on
*/
ModuleEvent module_events[] =
{
{ EVENT_NICKIP, event_nickip, EVENT_FLAG_EXCLUDE_ME},
NS_EVENT_END()
};
/** @brief new_bldomain
*
* Allocate a new blacklist domain entry and add to the list
*
* @param name of service
* @param domain of service
* @param type of service
*
* @return pointer to newly allocated entry
*/
static dom_list *new_bldomain( const char *name, const char *domain, BL_LOOKUP_TYPE type, const char *msg , int noban)
{
dom_list *dl;
dl = ns_calloc( sizeof( dom_list ) );
strlcpy( dl->name, name, BUFSIZE );
strlcpy( dl->domain, domain, BUFSIZE );
strlcpy( dl->msg, msg, BUFSIZE );
dl->type = type;
dl->noban = noban;
lnode_create_append( blsb.domains, dl );
DBAStore( "domains", dl->name, (void *)dl, sizeof( dom_list ) );
return dl;
}
/** @brief dnsbl_callback
*
* DNS callback
*
* @param data
* @param a
*
* @return NS_SUCCESS if suceeds else result of command
*/
static void dnsbl_callback(void *data, adns_answer *a)
{
scanclient *sc = (scanclient *)data;
int i;
char *show = NULL;
struct in_addr inp;
if (a && (a->nrrs > 0) && (a->status == adns_s_ok)) {
for (i = 0; i < a->nrrs; i++) {
if (a->type == adns_r_a) {
/* here we should actually check the return IP address and see if we really want to do something with it */
inp = *((struct in_addr*)&a->rrs.inaddr[i]);
show = ns_malloc(BUFSIZE);
ircsnprintf(show, BUFSIZE, sc->domain->msg, sc->ip);
} if (a->type == adns_r_txt) {
show = a->rrs.manyistr[i]->str;
}
irc_chanalert( blsb_bot, "%s (%s) exists in %s blacklist: %s %s", sc->nick, sc->ip, sc->domain->name, show, sc->domain->noban ? "(NOBAN)" : "" );
if (sc->check)
irc_prefmsg(blsb_bot, sc->check, "%s (%s) exists in %s blacklist: %s %s", sc->nick, sc->ip, sc->domain->name, show, sc->domain->noban ? "(NOBAN)" : "" );
if (sc->banned == 0 && !sc->domain->noban) {
sc->banned = 1;
/* only ban the user once
irc_prefmsg(blsb_bot, sc->user, "Your Host is listed as a inscure host at %s: %s", sc->domain->name, show); */
if (blsb.doakill) {
irc_akill (blsb_bot, sc->ip, "*", blsb.akilltime, "Your Host is listed as a insecure host at %s: %s", sc->domain->name, show);
/* HACK: the following line could be means for other use-after-free issues. (user gets freed too quickly after disconnection)
irc_chanalert( blsb_bot, "Akilling %s!%s@%s", sc->nick, sc->user->user->username, sc->user->user->hostname); */
irc_chanalert( blsb_bot, "Akilling %s", sc->nick);
}
}
if (a->type == adns_r_a) ns_free(show);
}
} else if (a && (a->status == adns_s_nxdomain)) {
if (blsb.verbose)
irc_chanalert( blsb_bot, "%s (%s) does not exist in %s blacklist", sc->nick, sc->ip, sc->domain->name);
if (sc->check)
irc_prefmsg(blsb_bot, sc->check, "%s (%s) does not exist in %s blacklist", sc->nick, sc->ip, sc->domain->name);
} else if (a->status != adns_s_ok) {
nlog(LOG_WARNING, "DNS error %s", adns_strerror(a->status));
}
}
/** @brief do_lookup
*
* trigger lookups
*
* @param Client *lookupuser
* @param Client *reportuser
*
* @return NS_SUCCESS if suceeds else result of command
*/
static scanclient *do_lookup( Client *lookupuser, Client *reportuser )
{
static char ip[HOSTIPLEN];
static char reverseip[HOSTIPLEN];
lnode_t *node;
dom_list *dl;
scanclient *sc = NULL;
unsigned char a, b, c, d;
unsigned int buflen;
d = (unsigned char) ( lookupuser->ip.s_addr >> 24 ) & 0xFF;
c = (unsigned char) ( lookupuser->ip.s_addr >> 16 ) & 0xFF;
b = (unsigned char) ( lookupuser->ip.s_addr >> 8 ) & 0xFF;
a = (unsigned char) ( lookupuser->ip.s_addr & 0xFF );
ircsnprintf( ip, HOSTIPLEN, "%d.%d.%d.%d", a, b, c, d );
ircsnprintf( reverseip, HOSTIPLEN, "%d.%d.%d.%d", d, c, b, a );
node = list_first( blsb.domains );
while( node )
{
dl = lnode_get( node);
/* Allocate enough for domain, ip address, additional period and NULL */
buflen = strlen( dl->domain ) + HOSTIPLEN + 1 + 1;
sc = ns_malloc( sizeof( scanclient ) );
sc->check = reportuser;
sc->user = lookupuser;
/* HACK: Prevent sc->user use-after-free in certain conditions. */
strlcpy( sc->nick, sc->user->name, 30 );
sc->domain = dl;
sc->banned = 0;
sc->lookup = ns_malloc( buflen );
strlcpy( sc->ip, ip, HOSTIPLEN );
strlcpy( sc->reverseip, reverseip, HOSTIPLEN );
ircsnprintf( sc->lookup, buflen, "%s.%s", reverseip, dl->domain );
switch (dl->type)
{
case BL_LOOKUP_TXT_RECORD: /* TXT record */
dns_lookup( sc->lookup, adns_r_txt, dnsbl_callback, sc );
break;
case BL_LOOKUP_A_RECORD: /* A record */
dns_lookup( sc->lookup, adns_r_a, dnsbl_callback, sc );
break;
default:
nlog( LOG_WARNING, "Unknown Type for DNS BL %s", dl->name );
break;
}
node = list_next( blsb.domains, node );
}
return sc;
}
/** @brief blsb_cmd_list
*
* LIST command handler
* List entries in the blacklist domain list
*
* @param cmdparam struct
*
* @return NS_SUCCESS if suceeds else result of command
*/
int blsb_cmd_list( const CmdParams *cmdparams )
{
dom_list *dl;
lnode_t *lnode;
lnode = list_first(blsb.domains);
irc_prefmsg (blsb_bot, cmdparams->source, "BlackList domains:");
while (lnode) {
dl = lnode_get(lnode);
irc_prefmsg (blsb_bot, cmdparams->source, "%s: %s (type %d) %s", dl->domain, dl->name, dl->type, dl->noban ? "NOBAN" : "");
lnode = list_next(blsb.domains, lnode);
}
irc_prefmsg (blsb_bot, cmdparams->source, "End of list.");
CommandReport(blsb_bot, "%s requested blacklist domain list", cmdparams->source->name);
return NS_SUCCESS;
}
/** @brief blsb_cmd_add
*
* ADD command handler
* Add an entry to the blacklist domain list
*
* @param cmdparam struct
* cmdparams->av[0] = domain
* cmdparams->av[1] = type
* cmdparams->av[2] = name
*
* @return NS_SUCCESS if suceeds else result of command
*/
int blsb_cmd_add( const CmdParams *cmdparams )
{
dom_list *dl;
lnode_t *lnode;
int type;
char *msg;
if( list_isfull( blsb.domains ) )
{
irc_prefmsg( blsb_bot, cmdparams->source, "Error, domain list is full" );
return NS_FAILURE;
}
type = atoi( cmdparams->av[1] );
if( type <= BL_LOOKUP_TYPE_MIN || type >= BL_LOOKUP_TYPE_MAX )
{
irc_prefmsg( blsb_bot, cmdparams->source, "type field does not contain a valid type" );
return NS_FAILURE;
}
msg = joinbuf( cmdparams->av, cmdparams->ac, 3 );
/* XXX do a initial lookup on the domain to check it exists? */
/* check for duplicates */
lnode = list_first( blsb.domains );
while( lnode )
{
dl = lnode_get( lnode );
if( ( ircstrcasecmp( dl->name, cmdparams->av[2] ) == 0 ) || ( ircstrcasecmp(dl->domain, cmdparams->av[0] ) == 0 ) )
{
irc_prefmsg( blsb_bot, cmdparams->source, "%s already has an entry", cmdparams->av[0] );
return NS_SUCCESS;
}
lnode = list_next(blsb.domains, lnode);
}
dl = new_bldomain( cmdparams->av[2], cmdparams->av[0], type, msg , ircstrcasecmp( cmdparams->av[3], "NOBAN" ) ? 1 : 0);
irc_prefmsg( blsb_bot, cmdparams->source, "Added domain %s (%s) as type %d", dl->name, dl->domain, dl->type );
CommandReport( blsb_bot, "%s added domain %s (%s) as type %d", cmdparams->source->name, dl->name, dl->domain, dl->type );
return NS_SUCCESS;
}
/** @brief blsb_cmd_del
*
* DEL command handler
* deletes an entry from the blacklist domain list
*
* @param cmdparam struct
* cmdparams->av[0] = domain
*
* @return NS_SUCCESS if suceeds else result of command
*/
int blsb_cmd_del( const CmdParams *cmdparams )
{
dom_list *dl;
lnode_t *lnode;
lnode = list_first(blsb.domains);
while (lnode) {
dl = lnode_get(lnode);
if( ircstrcasecmp( dl->domain, cmdparams->av[0] ) == 0 )
{
list_delete(blsb.domains, lnode);
lnode_destroy(lnode);
irc_prefmsg (blsb_bot, cmdparams->source, "Deleted %s (%s) from blacklist domains", dl->name, dl->domain);
CommandReport(blsb_bot, "%s deleted %s (%s) from blacklist domains", cmdparams->source->name, dl->name, dl->domain);
DBADelete("domains", dl->name);
ns_free(dl);
return NS_SUCCESS;
}
lnode = list_next(blsb.domains, lnode);
}
/* if we get here, then we can't find the entry */
irc_prefmsg (blsb_bot, cmdparams->source, "Error, no entry for %s", cmdparams->av[0]);
return NS_FAILURE;
}
/** @brief blsb_cmd_check
*
* CHECK command handler
*
* @param cmdparam struct
*
* @return NS_SUCCESS if suceeds else result of command
*/
int blsb_cmd_check( const CmdParams *cmdparams )
{
scanclient *sc = NULL;
Client *user;
user = FindUser(cmdparams->av[0]);
if (!user)
{
#if 0
/* XXX TODO: Lookup Hostname */
if (!ValidateHost(cmdparams->av[0]) {
irc_prefmsg(blsb_bot, cmdparams->source, "Invalid Nick or Host");
return NS_FAILURE;
} else {
sc = ns_malloc(sizeof(scanclient));
sc->check = 1;
sc->user = cmdparams->source;
/* HACK: Prevent sc->user use-after-free in certain conditions. */
strlcpy( sc->nick, sc->user->name, 30 );
sc->domain = dl;
sc->lookup = ns_malloc(buflen);
ircsnprintf(sc->lookup, buflen, "%d.%d.%d.%d.%s", d, c, b, a, dl->domain);
#endif
irc_prefmsg(blsb_bot, cmdparams->source, "Can not find %s online", cmdparams->av[0]);
return NS_ERR_SYNTAX_ERROR;
}
sc = do_lookup( user, cmdparams->source );
if( sc )
{
irc_prefmsg( blsb_bot, cmdparams->source, "Checking %s (%s) against DNS Blacklists", sc->nick, sc->ip );
CommandReport( blsb_bot, "%s is checking %s (%s) against DNS Blacklists", cmdparams->source->name, sc->nick, sc->ip );
}
return NS_SUCCESS;
}
/** @brief event_nickip
*
* NICKIP event handler
* scan user that just signed on the network
*
* @cmdparams pointer to commands param struct
*
* @return NS_SUCCESS if suceeds else NS_FAILURE
*/
static int event_nickip( const CmdParams *cmdparams )
{
SET_SEGV_LOCATION();
if (ModIsServerExcluded(cmdparams->source->uplink))
return NS_SUCCESS;
if (IsNetSplit(cmdparams->source))
return NS_SUCCESS;
(void)do_lookup( cmdparams->source, NULL );
return NS_SUCCESS;
}
/** @brief load_dom
*
* Database load domains row callback handler
*
* @param pointer to loaded data
* @param size of loaded data
*
* @return NS_FALSE
*/
static int load_dom( void *data, int size )
{
dom_list *dl;
if( size == sizeof(dom_list) )
{
dl = ns_calloc( sizeof(dom_list));
os_memcpy(dl, data, sizeof (dom_list));
lnode_create_append(blsb.domains, dl);
}
return NS_FALSE;
}
/** @brief load_default_bldomains
*
* Load default domain settings
*
* @param none
*
* @return none
*/
static void load_default_bldomains( void )
{
dom_list *default_domains;
default_domains = stddomlist;
while( default_domains->type != BL_LOOKUP_TYPE_MIN )
{
(void)new_bldomain( default_domains->name, default_domains->domain, default_domains->type, default_domains->msg , default_domains->noban );
default_domains++;
}
}
/** @brief ModInit
*
* Init handler
*
* @param none
*
* @return NS_SUCCESS if suceeds else NS_FAILURE
*/
int ModInit( void )
{
ModuleConfig( blsb_settings );
blsb.domains = list_create( LISTCOUNT_T_MAX );
me.want_nickip = 1;
if( !blsb.domains ) {
nlog( LOG_CRITICAL, "Unable to create domain list" );
return NS_FAILURE;
}
DBAFetchRows( "domains", load_dom );
/* If no domains, this must be our first run so load defaults */
if( list_count( blsb.domains ) == 0 )
load_default_bldomains();
return NS_SUCCESS;
}
/** @brief ModSynch
*
* Startup handler
*
* @param none
*
* @return NS_SUCCESS if suceeds else NS_FAILURE
*/
int ModSynch (void)
{
SET_SEGV_LOCATION();
blsb_bot = AddBot (&blsb_botinfo);
if( !blsb_bot )
return NS_FAILURE;
if( blsb.verbose )
irc_chanalert (blsb_bot, "Black List Scanning bot has started");
return NS_SUCCESS;
}
/** @brief ModFini
*
* Fini handler
*
* @param none
*
* @return NS_SUCCESS if suceeds else NS_FAILURE
*/
int ModFini( void )
{
return NS_SUCCESS;
}
/** @brief blsb_set_exclusions_cb
*
* Set callback for exclusions
* Enable or disable exclude event flag
*
* @cmdparams pointer to commands param struct
* @cmdparams reason for SET
*
* @return NS_SUCCESS if suceeds else NS_FAILURE
*/
static int blsb_set_exclusions_cb( const CmdParams *cmdparams, SET_REASON reason )
{
if( reason == SET_LOAD || reason == SET_CHANGE )
{
SetAllEventFlags( EVENT_FLAG_USE_EXCLUDE, blsb.exclusions );
}
return NS_SUCCESS;
}