ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/hopm/trunk/src/scan.c
Revision: 7927
Committed: Sat Dec 31 13:59:46 2016 UTC (8 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 25667 byte(s)
Log Message:
- Update copyright years

File Contents

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

Properties

Name Value
svn:eol-style native
svn:keywords Id