ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/hopm/branches/1.1.x/src/scan.c
Revision: 5281
Committed: Fri Jan 2 21:19:48 2015 UTC (9 years, 2 months ago) by michael
Content type: text/x-csrc
Original Path: hopm/trunk/src/scan.c
File size: 27068 byte(s)
Log Message:
- scan.c, scan.h: remove FORMATTYPE_STRING. It's default since we don't support anything else.

File Contents

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

Properties

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