1 |
/* |
2 |
* Copyright (c) 2002 Erik Fears |
3 |
* Copyright (c) 2014-2015 ircd-hybrid development team |
4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
6 |
* it under the terms of the GNU General Public License as published by |
7 |
* the Free Software Foundation; either version 2 of the License, or |
8 |
* (at your option) any later version. |
9 |
* |
10 |
* This program is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
* GNU General Public License for more details. |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License |
16 |
* along with this program; if not, write to the Free Software |
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
18 |
* USA |
19 |
*/ |
20 |
|
21 |
#include "setup.h" |
22 |
|
23 |
#include <stdio.h> |
24 |
#include <unistd.h> |
25 |
#include <assert.h> |
26 |
#include <stdlib.h> |
27 |
#include <string.h> |
28 |
#include <sys/time.h> |
29 |
#include <time.h> |
30 |
#include <errno.h> |
31 |
#include <fcntl.h> |
32 |
#include <poll.h> |
33 |
#include <sys/socket.h> |
34 |
#include <netinet/in.h> |
35 |
#include <arpa/inet.h> |
36 |
|
37 |
#include "compat.h" |
38 |
#include "config.h" |
39 |
#include "irc.h" |
40 |
#include "log.h" |
41 |
#include "stats.h" |
42 |
#include "dnsbl.h" |
43 |
#include "options.h" |
44 |
#include "negcache.h" |
45 |
#include "main.h" |
46 |
#include "memory.h" |
47 |
#include "match.h" |
48 |
#include "scan.h" |
49 |
|
50 |
/* Libopm */ |
51 |
|
52 |
#include "libopm/src/opm.h" |
53 |
#include "libopm/src/opm_common.h" |
54 |
#include "libopm/src/opm_error.h" |
55 |
#include "libopm/src/opm_types.h" |
56 |
|
57 |
|
58 |
/* GLOBAL LISTS */ |
59 |
|
60 |
static list_t *SCANNERS = NULL; /* List of OPM_T */ |
61 |
static list_t *MASKS = NULL; /* Associative list of masks->scanners */ |
62 |
|
63 |
|
64 |
/* Function declarations */ |
65 |
static struct scan_struct *scan_create(const char *[], const char *); |
66 |
static void scan_free(struct scan_struct *); |
67 |
static void scan_irckline(const struct scan_struct *, const char *, const char *); |
68 |
static void scan_negative(const struct scan_struct *); |
69 |
static void scan_log(OPM_REMOTE_T *); |
70 |
|
71 |
/** Callbacks for LIBOPM */ |
72 |
static void scan_open_proxy(OPM_T *, OPM_REMOTE_T *, int, void *); |
73 |
static void scan_negotiation_failed(OPM_T *, OPM_REMOTE_T *, int, void *); |
74 |
static void scan_timeout(OPM_T *, OPM_REMOTE_T *, int, void *); |
75 |
static void scan_end(OPM_T *, OPM_REMOTE_T *, int, void *); |
76 |
static void scan_handle_error(OPM_T *, OPM_REMOTE_T *, int, void *); |
77 |
|
78 |
extern FILE *scanlogfile; |
79 |
|
80 |
|
81 |
/* scan_cycle |
82 |
* |
83 |
* Perform scanner tasks. |
84 |
*/ |
85 |
void |
86 |
scan_cycle(void) |
87 |
{ |
88 |
node_t *p; |
89 |
|
90 |
/* Cycle through the blacklist first.. */ |
91 |
dnsbl_cycle(); |
92 |
|
93 |
/* Cycle each scanner object */ |
94 |
LIST_FOREACH(p, SCANNERS->head) |
95 |
{ |
96 |
struct scanner_struct *scs = p->data; |
97 |
opm_cycle(scs->scanner); |
98 |
} |
99 |
} |
100 |
|
101 |
/* scan_timer |
102 |
* |
103 |
* Perform actions that are to be performed every ~1 second. |
104 |
* |
105 |
* Parameters: NONE |
106 |
* Return: NONE |
107 |
* |
108 |
*/ |
109 |
void |
110 |
scan_timer(void) |
111 |
{ |
112 |
static time_t nc_counter; |
113 |
|
114 |
if (OptionsItem->negcache > 0) |
115 |
{ |
116 |
if (nc_counter++ >= OptionsItem->negcache_rebuild) |
117 |
{ |
118 |
/* |
119 |
* Time to rebuild the negative cache. |
120 |
*/ |
121 |
if (OPT_DEBUG) |
122 |
log_printf("SCAN -> Rebuilding negative cache"); |
123 |
|
124 |
negcache_rebuild(); |
125 |
nc_counter = 0; |
126 |
} |
127 |
} |
128 |
} |
129 |
|
130 |
/* scan_gettype(int protocol) |
131 |
* |
132 |
* Return human readable name of OPM PROTOCOL given OPM_TYPE_PROTOCOL |
133 |
* |
134 |
* Parameters: |
135 |
* protocol: Protocol to return (from libopm/src/opm_types.h) |
136 |
* |
137 |
* Return: |
138 |
* Pointer to static string containing human readable form of protocol |
139 |
* name |
140 |
* |
141 |
*/ |
142 |
const char * |
143 |
scan_gettype(int protocol) |
144 |
{ |
145 |
static const char *undef = "undefined"; |
146 |
static const struct protocol_assoc protocols[] = |
147 |
{ |
148 |
{ OPM_TYPE_HTTP, "HTTP" }, |
149 |
{ OPM_TYPE_HTTPPOST, "HTTPPOST" }, |
150 |
{ OPM_TYPE_SOCKS4, "SOCKS4" }, |
151 |
{ OPM_TYPE_SOCKS5, "SOCKS5" }, |
152 |
{ OPM_TYPE_WINGATE, "WINGATE" }, |
153 |
{ OPM_TYPE_ROUTER, "ROUTER" } |
154 |
}; |
155 |
|
156 |
for (unsigned int i = 0; i < (sizeof(protocols) / sizeof(struct protocol_assoc)); ++i) |
157 |
if (protocol == protocols[i].type) |
158 |
return protocols[i].name; |
159 |
|
160 |
return undef; |
161 |
} |
162 |
|
163 |
/* scan_init |
164 |
|
165 |
Initialize scanner and masks list based on configuration. |
166 |
|
167 |
Parameters: |
168 |
None |
169 |
|
170 |
Return: |
171 |
None |
172 |
*/ |
173 |
void |
174 |
scan_init(void) |
175 |
{ |
176 |
node_t *p, *p2, *p3, *p4, *node; |
177 |
struct UserConf *uc; |
178 |
struct ScannerConf *sc; |
179 |
struct ProtocolConf *pc; |
180 |
struct scanner_struct *scs; |
181 |
char *mask; |
182 |
char *scannername; |
183 |
|
184 |
/* FIXME: If rehash code is ever added, cleanup would need done here. */ |
185 |
|
186 |
SCANNERS = list_create(); |
187 |
MASKS = list_create(); |
188 |
|
189 |
/* Setup each individual scanner */ |
190 |
LIST_FOREACH(p, ScannerItemList->head) |
191 |
{ |
192 |
sc = p->data; |
193 |
scs = xcalloc(sizeof *scs); |
194 |
|
195 |
if (OPT_DEBUG) |
196 |
log_printf("SCAN -> Setting up scanner [%s]", sc->name); |
197 |
|
198 |
/* Build the scanner */ |
199 |
scs->scanner = opm_create(); |
200 |
scs->name = xstrdup(sc->name); |
201 |
scs->masks = list_create(); |
202 |
|
203 |
/* Setup configuration */ |
204 |
opm_config(scs->scanner, OPM_CONFIG_FD_LIMIT, &sc->fd); |
205 |
opm_config(scs->scanner, OPM_CONFIG_SCAN_IP, sc->target_ip); |
206 |
opm_config(scs->scanner, OPM_CONFIG_SCAN_PORT, &sc->target_port); |
207 |
opm_config(scs->scanner, OPM_CONFIG_TIMEOUT, &sc->timeout); |
208 |
opm_config(scs->scanner, OPM_CONFIG_MAX_READ, &sc->max_read); |
209 |
opm_config(scs->scanner, OPM_CONFIG_BIND_IP, sc->vhost); |
210 |
|
211 |
/* add target strings */ |
212 |
LIST_FOREACH(p2, sc->target_string->head) |
213 |
opm_config(scs->scanner, OPM_CONFIG_TARGET_STRING, p2->data); |
214 |
|
215 |
/* Setup callbacks */ |
216 |
opm_callback(scs->scanner, OPM_CALLBACK_OPENPROXY, &scan_open_proxy, scs); |
217 |
opm_callback(scs->scanner, OPM_CALLBACK_NEGFAIL, &scan_negotiation_failed, scs); |
218 |
opm_callback(scs->scanner, OPM_CALLBACK_TIMEOUT, &scan_timeout, scs); |
219 |
opm_callback(scs->scanner, OPM_CALLBACK_END, &scan_end, scs); |
220 |
opm_callback(scs->scanner, OPM_CALLBACK_ERROR, &scan_handle_error, scs); |
221 |
|
222 |
/* Setup the protocols */ |
223 |
LIST_FOREACH(p2, sc->protocols->head) |
224 |
{ |
225 |
pc = p2->data; |
226 |
|
227 |
if (OPT_DEBUG >= 2) |
228 |
log_printf("SCAN -> Adding protocol %s:%d to scanner [%s]", |
229 |
scan_gettype(pc->type), pc->port, scs->name); |
230 |
|
231 |
if (opm_addtype(scs->scanner, pc->type, pc->port) == OPM_ERR_BADPROTOCOL) |
232 |
log_printf("SCAN -> Error bad protocol %s:%d in scanner [%s]", |
233 |
scan_gettype(pc->type), pc->port, scs->name); |
234 |
} |
235 |
|
236 |
node = node_create(scs); |
237 |
list_add(SCANNERS, node); |
238 |
} |
239 |
|
240 |
/* Give scanners a list of masks they scan */ |
241 |
LIST_FOREACH(p, SCANNERS->head) |
242 |
{ |
243 |
scs = p->data; |
244 |
|
245 |
LIST_FOREACH(p2, UserItemList->head) |
246 |
{ |
247 |
uc = p2->data; |
248 |
|
249 |
LIST_FOREACH(p3, uc->scanners->head) |
250 |
{ |
251 |
scannername = p3->data; |
252 |
|
253 |
/* Add all these masks to scanner */ |
254 |
if (strcasecmp(scannername, scs->name) == 0) |
255 |
{ |
256 |
LIST_FOREACH(p4, uc->masks->head) |
257 |
{ |
258 |
mask = p4->data; |
259 |
|
260 |
if (OPT_DEBUG) |
261 |
log_printf("SCAN -> Linking the mask [%s] to scanner [%s]", mask, scannername); |
262 |
|
263 |
node = node_create(xstrdup(mask)); |
264 |
list_add(scs->masks, node); |
265 |
} |
266 |
|
267 |
break; |
268 |
} |
269 |
} |
270 |
} |
271 |
} |
272 |
|
273 |
/* Initialise negative cache */ |
274 |
if (OptionsItem->negcache > 0) |
275 |
{ |
276 |
if (OPT_DEBUG >= 2) |
277 |
log_printf("SCAN -> Initializing negative cache"); |
278 |
|
279 |
nc_init(&nc_head); |
280 |
} |
281 |
} |
282 |
|
283 |
/* scan_connect |
284 |
* |
285 |
* scan_connect is called when m_notice (irc.c) matches a connection |
286 |
* notice and parses the connecting user out of it. |
287 |
* |
288 |
* Parameters: |
289 |
* user: Parsed items from the connection notice: |
290 |
* user[0] = connecting users nickname |
291 |
* user[1] = connecting users username |
292 |
* user[2] = connecting users hostname |
293 |
* user[3] = connecting users IP |
294 |
* msg = Original connect notice |
295 |
* Return: NONE |
296 |
* |
297 |
*/ |
298 |
void |
299 |
scan_connect(const char *user[], const char *msg) |
300 |
{ |
301 |
struct sockaddr_in ip; |
302 |
node_t *p, *p2; |
303 |
struct scan_struct *ss; |
304 |
struct scanner_struct *scs; |
305 |
char *scsmask; |
306 |
int ret; |
307 |
|
308 |
/* |
309 |
* Have to use MSGLENMAX here because it is unknown what the max size of |
310 |
* username/hostname can be. Some ircds use really mad values for |
311 |
* these. |
312 |
*/ |
313 |
char mask[MSGLENMAX]; |
314 |
char ipmask[MSGLENMAX]; |
315 |
|
316 |
/* Check negcache before anything */ |
317 |
if (OptionsItem->negcache > 0) |
318 |
{ |
319 |
if (inet_pton(AF_INET, user[3], &ip.sin_addr) <= 0) |
320 |
{ |
321 |
log_printf("SCAN -> Invalid IPv4 address '%s'!", user[3]); |
322 |
return; |
323 |
} |
324 |
else |
325 |
{ |
326 |
if (check_neg_cache(ip.sin_addr.s_addr)) |
327 |
{ |
328 |
if (OPT_DEBUG) |
329 |
log_printf("SCAN -> %s!%s@%s (%s) is negatively cached. " |
330 |
"Skipping all tests.", user[0], user[1], user[2], |
331 |
user[3]); |
332 |
return; |
333 |
} |
334 |
} |
335 |
} |
336 |
|
337 |
/* Generate user mask */ |
338 |
snprintf(mask, MSGLENMAX, "%s!%s@%s", user[0], user[1], user[2]); |
339 |
snprintf(ipmask, MSGLENMAX, "%s!%s@%s", user[0], user[1], user[3]); |
340 |
|
341 |
/* Check exempt list now that we have a mask */ |
342 |
if (scan_checkexempt(mask, ipmask)) |
343 |
{ |
344 |
if (OPT_DEBUG) |
345 |
log_printf("SCAN -> %s is exempt from scanning", mask); |
346 |
|
347 |
return; |
348 |
} |
349 |
|
350 |
/* create scan_struct */ |
351 |
ss = scan_create(user, msg); |
352 |
|
353 |
/* Store ss in the remote struct, so that in callbacks we have ss */ |
354 |
ss->remote->data = ss; |
355 |
|
356 |
/* Start checking our DNSBLs */ |
357 |
if (LIST_SIZE(OpmItem->blacklists) > 0) |
358 |
dnsbl_add(ss); |
359 |
|
360 |
/* Add ss->remote to all matching scanners */ |
361 |
LIST_FOREACH(p, SCANNERS->head) |
362 |
{ |
363 |
scs = p->data; |
364 |
|
365 |
LIST_FOREACH(p2, scs->masks->head) |
366 |
{ |
367 |
scsmask = p2->data; |
368 |
|
369 |
if (!match(scsmask, mask)) |
370 |
{ |
371 |
if (OPT_DEBUG) |
372 |
log_printf("SCAN -> Passing %s to scanner [%s]", mask, scs->name); |
373 |
|
374 |
if ((ret = opm_scan(scs->scanner, ss->remote)) != OPM_SUCCESS) |
375 |
{ |
376 |
switch (ret) |
377 |
{ |
378 |
case OPM_ERR_NOPROTOCOLS: |
379 |
continue; |
380 |
break; |
381 |
case OPM_ERR_BADADDR: |
382 |
log_printf("OPM -> Bad address %s [%s].", |
383 |
(ss->manual_target ? ss->manual_target->name : |
384 |
"(unknown)"), ss->ip); |
385 |
break; |
386 |
default: |
387 |
log_printf("OPM -> Unknown error %s [%s].", |
388 |
(ss->manual_target ? ss->manual_target->name : |
389 |
"(unknown)"), ss->ip); |
390 |
break; |
391 |
} |
392 |
} |
393 |
else |
394 |
++ss->scans; /* Increase scan count only if OPM_SUCCESS */ |
395 |
|
396 |
break; /* Continue to next scanner */ |
397 |
} |
398 |
} |
399 |
} |
400 |
|
401 |
/* All scanners returned !OPM_SUCCESS and there were no dnsbl checks */ |
402 |
if (ss->scans == 0) |
403 |
scan_free(ss); |
404 |
} |
405 |
|
406 |
/* scan_create |
407 |
* |
408 |
* Allocate scan struct, including user information and REMOTE |
409 |
* for LIBOPM. |
410 |
* |
411 |
* Parameters: |
412 |
* user: Parsed items from the connection notice: |
413 |
* user[0] = connecting users nickname |
414 |
* user[1] = connecting users username |
415 |
* user[2] = connecting users hostname |
416 |
* user[3] = connecting users IP |
417 |
* msg = Original connect notice (used as PROOF) |
418 |
* |
419 |
* Return: Pointer to new scan_struct |
420 |
* |
421 |
*/ |
422 |
static struct scan_struct * |
423 |
scan_create(const char *user[], const char *msg) |
424 |
{ |
425 |
struct scan_struct *const ss = xcalloc(sizeof *ss); |
426 |
|
427 |
ss->irc_nick = xstrdup(user[0]); |
428 |
ss->irc_username = xstrdup(user[1]); |
429 |
ss->irc_hostname = xstrdup(user[2]); |
430 |
ss->ip = xstrdup(user[3]); |
431 |
ss->proof = xstrdup(msg); |
432 |
ss->remote = opm_remote_create(ss->ip); |
433 |
|
434 |
assert(ss->remote); |
435 |
return ss; |
436 |
} |
437 |
|
438 |
/* scan_free |
439 |
* |
440 |
* Free a scan_struct. This should only be done if the scan struct has |
441 |
* no scans left! |
442 |
* |
443 |
* Parameters: |
444 |
* ss: scan_struct to free |
445 |
* |
446 |
* Return: NONE |
447 |
*/ |
448 |
static void |
449 |
scan_free(struct scan_struct *ss) |
450 |
{ |
451 |
if (ss == NULL) |
452 |
return; |
453 |
|
454 |
xfree(ss->irc_nick); |
455 |
xfree(ss->irc_username); |
456 |
xfree(ss->irc_hostname); |
457 |
xfree(ss->ip); |
458 |
xfree(ss->proof); |
459 |
|
460 |
opm_remote_free(ss->remote); |
461 |
xfree(ss); |
462 |
} |
463 |
|
464 |
/* scan_checkfinished |
465 |
* |
466 |
* Check if a scan is complete (ss->scans <= 0) |
467 |
* and free it if need be. |
468 |
*/ |
469 |
void |
470 |
scan_checkfinished(struct scan_struct *ss) |
471 |
{ |
472 |
if (ss->scans <= 0) |
473 |
{ |
474 |
if (ss->manual_target) |
475 |
irc_send("PRIVMSG %s :CHECK -> All tests on %s completed.", |
476 |
ss->manual_target->name, ss->ip); |
477 |
else |
478 |
{ |
479 |
if (OPT_DEBUG) |
480 |
/* If there was a manual_target, then irc_nick, etc is NULL. */ |
481 |
log_printf("SCAN -> All tests on %s!%s@%s complete.", |
482 |
ss->irc_nick, ss->irc_username, ss->irc_hostname); |
483 |
|
484 |
/* Scan was a negative */ |
485 |
if (ss->positive == 0) |
486 |
scan_negative(ss); |
487 |
} |
488 |
|
489 |
scan_free(ss); |
490 |
} |
491 |
} |
492 |
|
493 |
/* scan_positive |
494 |
* |
495 |
* Remote host (defined by ss) has been found positive by one or more |
496 |
* tests. |
497 |
* |
498 |
* Parameters: |
499 |
* ss: scan_struct containing information regarding positive host |
500 |
* kline: command to send to IRC server to ban the user (see scan_irckline) |
501 |
* type: string of the type of proxy found to be running on the host |
502 |
* |
503 |
* Return: NONE |
504 |
* |
505 |
*/ |
506 |
void |
507 |
scan_positive(struct scan_struct *ss, const char *kline, const char *type) |
508 |
{ |
509 |
node_t *node; |
510 |
|
511 |
/* If already a positive, don't kline/close again */ |
512 |
if (ss->positive) |
513 |
return; |
514 |
|
515 |
/* Format KLINE and send to IRC server */ |
516 |
scan_irckline(ss, kline, type); |
517 |
|
518 |
/* Speed up the cleanup procedure */ |
519 |
/* Close all scans prematurely */ |
520 |
LIST_FOREACH(node, SCANNERS->head) |
521 |
{ |
522 |
OPM_T *scanner = ((struct scanner_struct *)node->data)->scanner; |
523 |
opm_end(scanner, ss->remote); |
524 |
} |
525 |
|
526 |
/* Set it as a positive (to avoid a scan_negative call later on */ |
527 |
ss->positive = 1; |
528 |
} |
529 |
|
530 |
/* scan_open_proxy CALLBACK |
531 |
* |
532 |
* Called by libopm when a proxy is verified open. |
533 |
* |
534 |
* Parameters: |
535 |
* scanner: Scanner that found the open proxy. |
536 |
* remote: Remote struct containing information regarding remote end |
537 |
* |
538 |
* Return: NONE |
539 |
*/ |
540 |
static void |
541 |
scan_open_proxy(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, void *data) |
542 |
{ |
543 |
struct scan_struct *ss; |
544 |
struct scanner_struct *scs; |
545 |
|
546 |
/* Record that a scan happened */ |
547 |
scan_log(remote); |
548 |
|
549 |
scs = data; |
550 |
ss = remote->data; |
551 |
|
552 |
if (ss->manual_target == NULL) |
553 |
{ |
554 |
/* kline and close scan */ |
555 |
scan_positive(ss, IRCItem->kline, scan_gettype(remote->protocol)); |
556 |
|
557 |
/* Report to blacklist */ |
558 |
dnsbl_report(ss); |
559 |
|
560 |
irc_send_channels("OPEN PROXY -> %s!%s@%s %s:%d (%s) [%s]", |
561 |
ss->irc_nick, ss->irc_username, ss->irc_hostname, remote->ip, |
562 |
remote->port, scan_gettype(remote->protocol), scs->name); |
563 |
log_printf("SCAN -> OPEN PROXY %s!%s@%s %s:%d (%s) [%s]", |
564 |
ss->irc_nick, ss->irc_username, ss->irc_hostname, remote->ip, |
565 |
remote->port, scan_gettype(remote->protocol), scs->name); |
566 |
} |
567 |
else |
568 |
{ |
569 |
irc_send("PRIVMSG %s :CHECK -> OPEN PROXY %s:%d (%s) [%s]", |
570 |
ss->manual_target->name, remote->ip, remote->port, |
571 |
scan_gettype(remote->protocol), scs->name); |
572 |
log_printf("SCAN -> OPEN PROXY %s:%d (%s) [%s]", remote->ip, |
573 |
remote->port, scan_gettype(remote->protocol), scs->name); |
574 |
} |
575 |
|
576 |
/* Record the proxy for stats purposes */ |
577 |
stats_openproxy(remote->protocol); |
578 |
} |
579 |
|
580 |
/* scan_negotiation_failed CALLBACK |
581 |
* |
582 |
* Called by libopm when negotiation of a specific protocol failed. |
583 |
* |
584 |
* Parameters: |
585 |
* scanner: Scanner where the negotiation failed. |
586 |
* remote: Remote struct containing information regarding remote end |
587 |
* |
588 |
* Return: NONE |
589 |
* |
590 |
*/ |
591 |
static void |
592 |
scan_negotiation_failed(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, void *data) |
593 |
{ |
594 |
//struct scan_struct *ss; |
595 |
struct scanner_struct *scs; |
596 |
|
597 |
/* Record that a scan happened */ |
598 |
scan_log(remote); |
599 |
|
600 |
scs = data; |
601 |
//ss = remote->data; |
602 |
|
603 |
if (OPT_DEBUG) |
604 |
log_printf("SCAN -> Negotiation failed %s:%d (%s) [%s] (%d bytes read)", |
605 |
remote->ip, remote->port, scan_gettype(remote->protocol), |
606 |
scs->name, remote->bytes_read); |
607 |
/* |
608 |
if (ss->manual_target) |
609 |
irc_send("PRIVMSG %s :CHECK -> Negotiation failed %s:%d (%s) [%s] " |
610 |
"(%d bytes read)", ss->manual_target->name, remote->ip, |
611 |
remote->port, scan_gettype(remote->protocol), scs->name, |
612 |
remote->bytes_read); |
613 |
*/ |
614 |
} |
615 |
|
616 |
/* scan_timeout CALLBACK |
617 |
* |
618 |
* Called by libopm when the negotiation of a specific protocol timed out. |
619 |
* |
620 |
* Parameters: |
621 |
* scanner: Scanner where the connection timed out. |
622 |
* remote: Remote struct containing information regarding remote end |
623 |
* |
624 |
* Return: NONE |
625 |
* |
626 |
*/ |
627 |
static void |
628 |
scan_timeout(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, void *data) |
629 |
{ |
630 |
//struct scan_struct *ss; |
631 |
struct scanner_struct *scs; |
632 |
|
633 |
/* Record that a scan happened */ |
634 |
scan_log(remote); |
635 |
|
636 |
scs = data; |
637 |
//ss = remote->data; |
638 |
|
639 |
if (OPT_DEBUG) |
640 |
log_printf("SCAN -> Negotiation timed out %s:%d (%s) [%s] (%d bytes read)", |
641 |
remote->ip, remote->port, |
642 |
scan_gettype(remote->protocol), scs->name, |
643 |
remote->bytes_read); |
644 |
/* |
645 |
if (ss->manual_target) |
646 |
irc_send("PRIVMSG %s :CHECK -> Negotiation timed out %s:%d (%s) [%s] " |
647 |
"(%d bytes read)", ss->manual_target->name, remote->ip, |
648 |
remote->port, scan_gettype(remote->protocol), scs->name, |
649 |
remote->bytes_read); |
650 |
*/ |
651 |
} |
652 |
|
653 |
/* scan_end CALLBACK |
654 |
* |
655 |
* Called by libopm when a specific SCAN has completed (all protocols in |
656 |
* that scan). |
657 |
* |
658 |
* Parameters: |
659 |
* scanner: Scanner the scan ended on. |
660 |
* remote: Remote struct containing information regarding remote end |
661 |
* |
662 |
* Return: NONE |
663 |
*/ |
664 |
static void |
665 |
scan_end(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, void *data) |
666 |
{ |
667 |
struct scan_struct *ss; |
668 |
struct scanner_struct *scs; |
669 |
|
670 |
scs = data; |
671 |
ss = remote->data; |
672 |
|
673 |
if (OPT_DEBUG) |
674 |
log_printf("SCAN -> Scan %s [%s] completed", remote->ip, scs->name); |
675 |
|
676 |
--ss->scans; |
677 |
scan_checkfinished(ss); |
678 |
} |
679 |
|
680 |
/* scan_handle_error CALLBACK |
681 |
* |
682 |
* Called by libopm when an error occurs with a specific connection. This |
683 |
* does not mean the entire scan has ended. |
684 |
* |
685 |
* Parameters: |
686 |
* scanner: Scanner where the error occured. |
687 |
* remote: Remote struct containing information regarding remote end |
688 |
* err: OPM_ERROR code describing the error. |
689 |
* |
690 |
* Return: NONE |
691 |
*/ |
692 |
static void |
693 |
scan_handle_error(OPM_T *scanner, OPM_REMOTE_T *remote, int err, void *data) |
694 |
{ |
695 |
struct scan_struct *ss; |
696 |
struct scanner_struct *scs; |
697 |
|
698 |
scs = data; |
699 |
ss = remote->data; |
700 |
|
701 |
switch (err) |
702 |
{ |
703 |
case OPM_ERR_MAX_READ: |
704 |
if (OPT_DEBUG >= 2) |
705 |
log_printf("SCAN -> Max read on %s:%d (%s) [%s] (%d bytes read)", |
706 |
remote->ip, remote->port, scan_gettype(remote->protocol), |
707 |
scs->name, remote->bytes_read); |
708 |
|
709 |
if (ss->manual_target) |
710 |
irc_send("PRIVMSG %s :CHECK -> Negotiation failed %s:%d (%s) " |
711 |
"[%s] (%d bytes read)", ss->manual_target->name, |
712 |
remote->ip, remote->port, scan_gettype(remote->protocol), |
713 |
scs->name, remote->bytes_read); |
714 |
break; |
715 |
case OPM_ERR_BIND: |
716 |
log_printf("SCAN -> Bind error on %s:%d (%s) [%s]", remote->ip, |
717 |
remote->port, scan_gettype(remote->protocol), scs->name); |
718 |
break; |
719 |
case OPM_ERR_NOFD: |
720 |
log_printf("SCAN -> File descriptor allocation error %s:%d (%s) " |
721 |
"[%s]", remote->ip, remote->port, |
722 |
scan_gettype(remote->protocol), scs->name); |
723 |
|
724 |
if (ss->manual_target) |
725 |
irc_send("PRIVMSG %s :CHECK -> Scan failed %s:%d (%s) [%s] " |
726 |
"(file descriptor allocation error)", |
727 |
ss->manual_target->name, remote->ip, remote->port, |
728 |
scan_gettype(remote->protocol), scs->name); |
729 |
break; |
730 |
default: /* Unknown Error! */ |
731 |
if (OPT_DEBUG) |
732 |
log_printf("SCAN -> Unknown error %s:%d (%s) [%s]", remote->ip, |
733 |
remote->port, scan_gettype(remote->protocol), scs->name); |
734 |
break; |
735 |
} |
736 |
} |
737 |
|
738 |
/* scan_negative |
739 |
* |
740 |
* Remote host (defined by ss) has passed all tests. |
741 |
* |
742 |
* Parameters: |
743 |
* ss: scan_struct containing information regarding negative host. |
744 |
* |
745 |
* Return: NONE |
746 |
* |
747 |
*/ |
748 |
static void |
749 |
scan_negative(const struct scan_struct *ss) |
750 |
{ |
751 |
/* Insert IP in negcache */ |
752 |
if (OptionsItem->negcache > 0) |
753 |
{ |
754 |
if (OPT_DEBUG >= 2) |
755 |
log_printf("SCAN -> Adding %s to negative cache", ss->ip); |
756 |
|
757 |
negcache_insert(ss->ip); |
758 |
} |
759 |
} |
760 |
|
761 |
/* scan_irckline |
762 |
* |
763 |
* ss has been found as a positive host and is to be klined. |
764 |
* Format a kline message using the kline message provided |
765 |
* as a format, then pass it to irc_send() to be sent to the remote server. |
766 |
* |
767 |
* Parameters: |
768 |
* ss: scan_struct containing information regarding host to be klined |
769 |
* format: kline message to format |
770 |
* type: type of proxy found (%t format character) |
771 |
* |
772 |
* Return: NONE |
773 |
* |
774 |
*/ |
775 |
static void |
776 |
scan_irckline(const struct scan_struct *ss, const char *format, const char *type) |
777 |
{ |
778 |
char message[MSGLENMAX]; /* OUTPUT */ |
779 |
|
780 |
unsigned int pos = 0; /* position in format */ |
781 |
unsigned int len = 0; /* position in message */ |
782 |
unsigned int size = 0; /* temporary size buffer */ |
783 |
unsigned int i; |
784 |
struct kline_format_assoc table[] = |
785 |
{ |
786 |
{'i', NULL }, |
787 |
{'h', NULL }, |
788 |
{'u', NULL }, |
789 |
{'n', NULL }, |
790 |
{'t', NULL } |
791 |
}; |
792 |
|
793 |
table[0].data = ss->ip; |
794 |
table[1].data = ss->irc_hostname; |
795 |
table[2].data = ss->irc_username; |
796 |
table[3].data = ss->irc_nick; |
797 |
table[4].data = type; |
798 |
|
799 |
/* |
800 |
* Copy format to message character by character, inserting any matching |
801 |
* data after %. |
802 |
*/ |
803 |
while (format[pos] != '\0' && len < (MSGLENMAX - 2)) |
804 |
{ |
805 |
switch (format[pos]) |
806 |
{ |
807 |
case '%': |
808 |
/* % is the last char in the string, move on */ |
809 |
if (format[pos + 1] == '\0') |
810 |
continue; |
811 |
|
812 |
/* %% escapes % and becomes % */ |
813 |
if (format[pos + 1] == '%') |
814 |
{ |
815 |
message[len++] = '%'; |
816 |
++pos; /* Skip past the escaped % */ |
817 |
break; |
818 |
} |
819 |
|
820 |
/* Safe to check against table now */ |
821 |
for (i = 0; i < (sizeof(table) / sizeof(struct kline_format_assoc)); ++i) |
822 |
{ |
823 |
if (table[i].key == format[pos + 1]) |
824 |
{ |
825 |
size = strlen(table[i].data); |
826 |
|
827 |
/* Check if the new string can fit! */ |
828 |
if ((size + len) > (MSGLENMAX - 1)) |
829 |
break; |
830 |
else |
831 |
{ |
832 |
strlcat(message, table[i].data, sizeof(message)); |
833 |
len += size; |
834 |
} |
835 |
} |
836 |
} |
837 |
|
838 |
/* Skip key character */ |
839 |
++pos; |
840 |
break; |
841 |
|
842 |
default: |
843 |
message[len++] = format[pos]; |
844 |
message[len] = '\0'; |
845 |
break; |
846 |
} |
847 |
|
848 |
/* Continue to next character in format */ |
849 |
++pos; |
850 |
} |
851 |
|
852 |
irc_send("%s", message); |
853 |
} |
854 |
|
855 |
/* scan_manual |
856 |
* |
857 |
* Create a manual scan. A manual scan is a scan where the |
858 |
* scan_struct contains a manual_target pointer. |
859 |
*/ |
860 |
void |
861 |
scan_manual(char *param, const struct ChannelConf *target) |
862 |
{ |
863 |
char buf[INET6_ADDRSTRLEN]; |
864 |
const void *addr = NULL; |
865 |
struct scan_struct *ss; |
866 |
struct scanner_struct *scs; |
867 |
const char *ip = NULL; |
868 |
char *scannername; |
869 |
node_t *p; |
870 |
int ret; |
871 |
|
872 |
/* If there were no parameters sent, simply alert the user and return */ |
873 |
if (param == NULL) |
874 |
{ |
875 |
irc_send("PRIVMSG %s :OPM -> Invalid parameters.", target->name); |
876 |
return; |
877 |
} |
878 |
|
879 |
/* |
880 |
* Try to extract a scanner name from param, otherwise we'll be |
881 |
* adding to all scanners |
882 |
*/ |
883 |
ip = param; |
884 |
|
885 |
if ((scannername = strchr(param, ' '))) |
886 |
{ |
887 |
*scannername = '\0'; |
888 |
scannername++; |
889 |
} |
890 |
|
891 |
/* If IP is a hostname, resolve it using gethostbyname (which will block!) */ |
892 |
if ((addr = firedns_resolveip4(ip)) == NULL) |
893 |
{ |
894 |
irc_send("PRIVMSG %s :CHECK -> Error resolving host '%s': %s", |
895 |
target->name, ip, firedns_strerror(firedns_errno)); |
896 |
return; |
897 |
} |
898 |
|
899 |
/* IP = the resolved IP now (it was the ip OR hostname before) */ |
900 |
if ((ip = inet_ntop(AF_INET, addr, buf, sizeof(buf))) == NULL) |
901 |
{ |
902 |
irc_send("PRIVMSG %s :CHECK -> invalid address: %s", |
903 |
target->name, strerror(errno)); |
904 |
return; |
905 |
} |
906 |
|
907 |
ss = xcalloc(sizeof *ss); |
908 |
ss->ip = xstrdup(ip); |
909 |
ss->remote = opm_remote_create(ss->ip); |
910 |
ss->remote->data = ss; |
911 |
ss->manual_target = target; |
912 |
|
913 |
assert(ss->remote); |
914 |
|
915 |
if (scannername) |
916 |
irc_send("PRIVMSG %s :CHECK -> Checking '%s' for open proxies [%s]", |
917 |
target->name, ip, scannername); |
918 |
else |
919 |
irc_send("PRIVMSG %s :CHECK -> Checking '%s' for open proxies on all " |
920 |
"scanners", target->name, ip); |
921 |
|
922 |
if (LIST_SIZE(OpmItem->blacklists) > 0) |
923 |
dnsbl_add(ss); |
924 |
|
925 |
/* Add ss->remote to all scanners */ |
926 |
LIST_FOREACH(p, SCANNERS->head) |
927 |
{ |
928 |
scs = p->data; |
929 |
|
930 |
/* |
931 |
* If we have a scannername, only allow that scanner |
932 |
* to be used |
933 |
*/ |
934 |
if (scannername) |
935 |
if (strcasecmp(scannername, scs->name)) |
936 |
continue; |
937 |
|
938 |
if (OPT_DEBUG) |
939 |
log_printf("SCAN -> Passing %s to scanner [%s] (MANUAL SCAN)", ip, scs->name); |
940 |
|
941 |
if ((ret = opm_scan(scs->scanner, ss->remote)) != OPM_SUCCESS) |
942 |
{ |
943 |
switch (ret) |
944 |
{ |
945 |
case OPM_ERR_NOPROTOCOLS: |
946 |
break; |
947 |
case OPM_ERR_BADADDR: |
948 |
irc_send("PRIVMSG %s :OPM -> Bad address %s [%s]", |
949 |
ss->manual_target->name, ss->ip, scs->name); |
950 |
break; |
951 |
default: |
952 |
irc_send("PRIVMSG %s :OPM -> Unknown error %s [%s]", |
953 |
ss->manual_target->name, ss->ip, scs->name); |
954 |
break; |
955 |
} |
956 |
} |
957 |
else |
958 |
++ss->scans; /* Increase scan count only if OPM_SUCCESS */ |
959 |
} |
960 |
|
961 |
/* |
962 |
* If all of the scanners gave !OPM_SUCCESS and there were no dnsbl checks, |
963 |
* cleanup here |
964 |
*/ |
965 |
if (ss->scans == 0) |
966 |
{ |
967 |
if (scannername) |
968 |
irc_send("PRIVMSG %s :CHECK -> No such scanner '%s', or '%s' has " |
969 |
"0 protocols.", ss->manual_target->name, scannername, |
970 |
scannername); |
971 |
|
972 |
irc_send("PRIVMSG %s :CHECK -> No scans active on '%s', aborting scan.", |
973 |
ss->manual_target->name, ss->ip); |
974 |
scan_free(ss); |
975 |
} |
976 |
} |
977 |
|
978 |
/* scan_checkexempt |
979 |
* |
980 |
* Check mask against exempt list. |
981 |
* |
982 |
* Parameters: |
983 |
* mask: Mask to check |
984 |
* |
985 |
* Return: |
986 |
* 1 if mask is in list |
987 |
* 0 if mask is not in list |
988 |
*/ |
989 |
int |
990 |
scan_checkexempt(const char *mask, const char *ipmask) |
991 |
{ |
992 |
node_t *node; |
993 |
|
994 |
LIST_FOREACH(node, ExemptItem->masks->head) |
995 |
{ |
996 |
const char *exempt_mask = node->data; |
997 |
|
998 |
if (!match(exempt_mask, mask) || !match(exempt_mask, ipmask)) |
999 |
return 1; |
1000 |
} |
1001 |
|
1002 |
return 0; |
1003 |
} |
1004 |
|
1005 |
/* scan_log |
1006 |
* |
1007 |
* Log the fact that a given ip/port/protocol has just been scanned, if the |
1008 |
* user has asked for this to be logged. |
1009 |
* |
1010 |
* Parameters: |
1011 |
* remote: OPM_REMOTE_T for the remote end |
1012 |
*/ |
1013 |
static void |
1014 |
scan_log(OPM_REMOTE_T *remote) |
1015 |
{ |
1016 |
char buf_present[32]; |
1017 |
time_t present = 0; |
1018 |
struct scan_struct *ss = remote->data; |
1019 |
|
1020 |
if (!(OptionsItem->scanlog && scanlogfile)) |
1021 |
return; |
1022 |
|
1023 |
time(&present); |
1024 |
strftime(buf_present, sizeof(buf_present), "%FT%H:%M:%S%z", localtime(&present)); |
1025 |
|
1026 |
fprintf(scanlogfile, "[%s] %s:%d (%s) \"%s\"\n", buf_present, remote->ip, |
1027 |
remote->port, scan_gettype(remote->protocol), ss->proof); |
1028 |
fflush(scanlogfile); |
1029 |
} |