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