ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/hopm/trunk/src/scan.c
Revision: 6283
Committed: Fri Jul 17 11:24:00 2015 UTC (10 years, 1 month ago) by michael
Content type: text/x-csrc
File size: 26795 byte(s)
Log Message:
- scan.c:scan_free(): removed useless test on 'ss'

File Contents

# User Rev Content
1 michael 5052 /*
2 michael 5351 * Copyright (c) 2002 Erik Fears
3     * Copyright (c) 2014-2015 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     #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 michael 5699 static list_t *SCANNERS; /* List of OPM_T */
60     static list_t *MASKS; /* Associative list of masks->scanners */
61 michael 5052
62    
63     /* Function declarations */
64 michael 5338 static struct scan_struct *scan_create(const char *[], const char *);
65 michael 5322 static void scan_free(struct scan_struct *);
66 michael 5338 static void scan_irckline(const struct scan_struct *, const char *, const char *);
67     static void scan_negative(const struct scan_struct *);
68 michael 5052 static void scan_log(OPM_REMOTE_T *);
69    
70     /** Callbacks for LIBOPM */
71 michael 5338 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 michael 5052 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 michael 5114
80 michael 5052 /* scan_cycle
81     *
82     * Perform scanner tasks.
83     */
84 michael 5114 void
85     scan_cycle(void)
86 michael 5052 {
87 michael 5695 node_t *node;
88 michael 5052
89 michael 5114 /* Cycle through the blacklist first.. */
90     dnsbl_cycle();
91 michael 5052
92 michael 5114 /* Cycle each scanner object */
93 michael 5695 LIST_FOREACH(node, SCANNERS->head)
94 michael 5114 {
95 michael 5695 struct scanner_struct *scs = node->data;
96 michael 5114 opm_cycle(scs->scanner);
97     }
98 michael 5052 }
99    
100     /* scan_timer
101 michael 5876 *
102 michael 5052 * Perform actions that are to be performed every ~1 second.
103     *
104     * Parameters: NONE
105     * Return: NONE
106     *
107     */
108 michael 5114 void
109     scan_timer(void)
110 michael 5052 {
111 michael 5332 static time_t nc_counter;
112 michael 5052
113 michael 5695 if (OptionsItem->negcache)
114 michael 5114 {
115 michael 5332 if (nc_counter++ >= OptionsItem->negcache_rebuild)
116 michael 5114 {
117     /*
118 michael 5332 * Time to rebuild the negative cache.
119 michael 5114 */
120     if (OPT_DEBUG)
121     log_printf("SCAN -> Rebuilding negative cache");
122 michael 5052
123 michael 5114 negcache_rebuild();
124     nc_counter = 0;
125     }
126     }
127 michael 5052 }
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 michael 5114 const char *
142     scan_gettype(int protocol)
143 michael 5052 {
144 michael 5114 static const char *undef = "undefined";
145 michael 5338 static const struct protocol_assoc protocols[] =
146 michael 5114 {
147 michael 6222 { 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 michael 5114 };
157 michael 5052
158 michael 5116 for (unsigned int i = 0; i < (sizeof(protocols) / sizeof(struct protocol_assoc)); ++i)
159 michael 5114 if (protocol == protocols[i].type)
160     return protocols[i].name;
161 michael 5052
162 michael 5114 return undef;
163 michael 5052 }
164    
165     /* scan_init
166    
167     Initialize scanner and masks list based on configuration.
168    
169     Parameters:
170     None
171 michael 5876
172 michael 5052 Return:
173     None
174     */
175 michael 5114 void
176     scan_init(void)
177 michael 5052 {
178 michael 5116 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 michael 5052
184 michael 5116 SCANNERS = list_create();
185     MASKS = list_create();
186 michael 5052
187 michael 5116 /* Setup each individual scanner */
188     LIST_FOREACH(p, ScannerItemList->head)
189     {
190     sc = p->data;
191 michael 6149 scs = xcalloc(sizeof(*scs));
192 michael 5052
193 michael 5116 if (OPT_DEBUG)
194     log_printf("SCAN -> Setting up scanner [%s]", sc->name);
195 michael 5052
196 michael 5116 /* Build the scanner */
197     scs->scanner = opm_create();
198     scs->name = xstrdup(sc->name);
199     scs->masks = list_create();
200 michael 5052
201 michael 5116 /* Setup configuration */
202 michael 5121 opm_config(scs->scanner, OPM_CONFIG_FD_LIMIT, &sc->fd);
203 michael 5116 opm_config(scs->scanner, OPM_CONFIG_SCAN_IP, sc->target_ip);
204 michael 5121 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 michael 5116 opm_config(scs->scanner, OPM_CONFIG_BIND_IP, sc->vhost);
208 michael 5052
209 michael 5116 /* add target strings */
210     LIST_FOREACH(p2, sc->target_string->head)
211 michael 5227 opm_config(scs->scanner, OPM_CONFIG_TARGET_STRING, p2->data);
212 michael 5052
213 michael 5116 /* 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 michael 5052
220 michael 5116 /* Setup the protocols */
221     LIST_FOREACH(p2, sc->protocols->head)
222     {
223     pc = p2->data;
224 michael 5052
225 michael 5116 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 michael 5052
229 michael 5116 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 michael 5052
234 michael 5116 node = node_create(scs);
235     list_add(SCANNERS, node);
236     }
237 michael 5052
238 michael 5116 /* Give scanners a list of masks they scan */
239     LIST_FOREACH(p, SCANNERS->head)
240     {
241     scs = p->data;
242 michael 5052
243 michael 5116 LIST_FOREACH(p2, UserItemList->head)
244     {
245     uc = p2->data;
246 michael 5052
247 michael 5116 LIST_FOREACH(p3, uc->scanners->head)
248     {
249 michael 5699 const char *scannername = p3->data;
250 michael 5052
251 michael 5116 /* Add all these masks to scanner */
252     if (strcasecmp(scannername, scs->name) == 0)
253     {
254     LIST_FOREACH(p4, uc->masks->head)
255     {
256 michael 5699 const char *mask = p4->data;
257 michael 5052
258 michael 5116 if (OPT_DEBUG)
259     log_printf("SCAN -> Linking the mask [%s] to scanner [%s]", mask, scannername);
260 michael 5052
261 michael 5116 node = node_create(xstrdup(mask));
262     list_add(scs->masks, node);
263     }
264 michael 5052
265 michael 5116 break;
266     }
267 michael 5052 }
268 michael 5116 }
269     }
270 michael 5052
271 michael 5116 /* Initialise negative cache */
272 michael 5695 if (OptionsItem->negcache)
273 michael 5116 {
274     if (OPT_DEBUG >= 2)
275     log_printf("SCAN -> Initializing negative cache");
276    
277     nc_init(&nc_head);
278     }
279 michael 5052 }
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 michael 5116 void
297 michael 5338 scan_connect(const char *user[], const char *msg)
298 michael 5052 {
299 michael 5311 struct sockaddr_in ip;
300 michael 5116 node_t *p, *p2;
301     struct scan_struct *ss;
302     struct scanner_struct *scs;
303     int ret;
304 michael 5052
305 michael 5116 /*
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 michael 5279 char mask[MSGLENMAX];
311     char ipmask[MSGLENMAX];
312 michael 5052
313 michael 5116 /* Check negcache before anything */
314 michael 5695 if (OptionsItem->negcache)
315 michael 5116 {
316 michael 5311 if (inet_pton(AF_INET, user[3], &ip.sin_addr) <= 0)
317 michael 5284 {
318     log_printf("SCAN -> Invalid IPv4 address '%s'!", user[3]);
319     return;
320     }
321     else
322     {
323 michael 5311 if (check_neg_cache(ip.sin_addr.s_addr))
324 michael 5052 {
325 michael 5284 if (OPT_DEBUG)
326     log_printf("SCAN -> %s!%s@%s (%s) is negatively cached. "
327 michael 5052 "Skipping all tests.", user[0], user[1], user[2],
328     user[3]);
329 michael 5284 return;
330 michael 5052 }
331 michael 5284 }
332     }
333 michael 5052
334 michael 5284 /* Generate user mask */
335 michael 6015 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 michael 5052
338 michael 5284 /* 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 michael 5052
344 michael 5284 return;
345     }
346 michael 5052
347 michael 5284 /* create scan_struct */
348     ss = scan_create(user, msg);
349 michael 5052
350 michael 5284 /* Store ss in the remote struct, so that in callbacks we have ss */
351     ss->remote->data = ss;
352 michael 5052
353 michael 5284 /* 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 michael 5699 const char *scsmask = p2->data;
365 michael 5284
366     if (!match(scsmask, mask))
367 michael 5052 {
368 michael 5284 if (OPT_DEBUG)
369     log_printf("SCAN -> Passing %s to scanner [%s]", mask, scs->name);
370 michael 5052
371 michael 5284 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 michael 5052
393 michael 5284 break; /* Continue to next scanner */
394 michael 5052 }
395 michael 5284 }
396     }
397 michael 5052
398 michael 5116 /* All scanners returned !OPM_SUCCESS and there were no dnsbl checks */
399     if (ss->scans == 0)
400     scan_free(ss);
401 michael 5052 }
402    
403     /* scan_create
404     *
405 michael 5116 * Allocate scan struct, including user information and REMOTE
406 michael 5052 * 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 michael 5322 static struct scan_struct *
420 michael 5338 scan_create(const char *user[], const char *msg)
421 michael 5052 {
422 michael 6149 struct scan_struct *ss = xcalloc(sizeof(*ss));
423 michael 5052
424 michael 5116 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 michael 5052
431 michael 5116 return ss;
432 michael 5052 }
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 michael 5116 *
442 michael 5052 * Return: NONE
443     */
444 michael 5322 static void
445 michael 5114 scan_free(struct scan_struct *ss)
446 michael 5052 {
447 michael 5426 xfree(ss->irc_nick);
448     xfree(ss->irc_username);
449     xfree(ss->irc_hostname);
450     xfree(ss->ip);
451     xfree(ss->proof);
452 michael 5052
453 michael 5114 opm_remote_free(ss->remote);
454 michael 5426 xfree(ss);
455 michael 5052 }
456    
457     /* scan_checkfinished
458     *
459     * Check if a scan is complete (ss->scans <= 0)
460     * and free it if need be.
461     */
462 michael 5114 void
463     scan_checkfinished(struct scan_struct *ss)
464 michael 5052 {
465 michael 5116 if (ss->scans <= 0)
466     {
467     if (ss->manual_target)
468     irc_send("PRIVMSG %s :CHECK -> All tests on %s completed.",
469 michael 5052 ss->manual_target->name, ss->ip);
470 michael 5116 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 michael 5052
477 michael 5116 /* Scan was a negative */
478 michael 5322 if (ss->positive == 0)
479 michael 5116 scan_negative(ss);
480     }
481 michael 5052
482 michael 5116 scan_free(ss);
483     }
484 michael 5052 }
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 michael 5116 * ss: scan_struct containing information regarding positive host
493 michael 5052 * 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 michael 5114 void
500     scan_positive(struct scan_struct *ss, const char *kline, const char *type)
501 michael 5052 {
502 michael 5114 node_t *node;
503 michael 5052
504 michael 5114 /* If already a positive, don't kline/close again */
505     if (ss->positive)
506     return;
507 michael 5052
508 michael 5114 /* Format KLINE and send to IRC server */
509     scan_irckline(ss, kline, type);
510 michael 5052
511 michael 5114 /* 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 michael 5052
519 michael 5114 /* Set it as a positive (to avoid a scan_negative call later on */
520     ss->positive = 1;
521 michael 5052 }
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 michael 5338 static void
534 michael 5114 scan_open_proxy(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, void *data)
535 michael 5052 {
536 michael 5116 struct scan_struct *ss;
537     struct scanner_struct *scs;
538 michael 5052
539 michael 5116 /* Record that a scan happened */
540     scan_log(remote);
541 michael 5052
542 michael 5116 scs = data;
543     ss = remote->data;
544 michael 5052
545 michael 5116 if (ss->manual_target == NULL)
546     {
547     /* kline and close scan */
548     scan_positive(ss, IRCItem->kline, scan_gettype(remote->protocol));
549 michael 5052
550 michael 5116 /* Report to blacklist */
551     dnsbl_report(ss);
552 michael 5052
553 michael 5116 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 michael 5052
569 michael 5116 /* Record the proxy for stats purposes */
570     stats_openproxy(remote->protocol);
571 michael 5052 }
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 michael 5338 static void
585 michael 5116 scan_negotiation_failed(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, void *data)
586 michael 5052 {
587 michael 5116 //struct scan_struct *ss;
588     struct scanner_struct *scs;
589 michael 5052
590 michael 5116 /* Record that a scan happened */
591     scan_log(remote);
592 michael 5052
593 michael 5116 scs = data;
594     //ss = remote->data;
595 michael 5052
596 michael 5116 if (OPT_DEBUG)
597     log_printf("SCAN -> Negotiation failed %s:%d (%s) [%s] (%d bytes read)",
598     remote->ip, remote->port, scan_gettype(remote->protocol),
599     scs->name, remote->bytes_read);
600 michael 5052 /*
601 michael 5116 if (ss->manual_target)
602     irc_send("PRIVMSG %s :CHECK -> Negotiation failed %s:%d (%s) [%s] "
603     "(%d bytes read)", ss->manual_target->name, remote->ip,
604     remote->port, scan_gettype(remote->protocol), scs->name,
605     remote->bytes_read);
606 michael 5052 */
607     }
608    
609     /* scan_timeout CALLBACK
610     *
611     * Called by libopm when the negotiation of a specific protocol timed out.
612     *
613     * Parameters:
614     * scanner: Scanner where the connection timed out.
615     * remote: Remote struct containing information regarding remote end
616     *
617     * Return: NONE
618     *
619     */
620 michael 5116 static void
621     scan_timeout(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, void *data)
622 michael 5052 {
623 michael 5116 //struct scan_struct *ss;
624     struct scanner_struct *scs;
625 michael 5052
626 michael 5116 /* Record that a scan happened */
627     scan_log(remote);
628 michael 5052
629 michael 5116 scs = data;
630     //ss = remote->data;
631 michael 5052
632 michael 5116 if (OPT_DEBUG)
633     log_printf("SCAN -> Negotiation timed out %s:%d (%s) [%s] (%d bytes read)",
634     remote->ip, remote->port,
635     scan_gettype(remote->protocol), scs->name,
636     remote->bytes_read);
637 michael 5052 /*
638 michael 5116 if (ss->manual_target)
639     irc_send("PRIVMSG %s :CHECK -> Negotiation timed out %s:%d (%s) [%s] "
640     "(%d bytes read)", ss->manual_target->name, remote->ip,
641     remote->port, scan_gettype(remote->protocol), scs->name,
642     remote->bytes_read);
643 michael 5052 */
644     }
645    
646     /* scan_end CALLBACK
647     *
648     * Called by libopm when a specific SCAN has completed (all protocols in
649     * that scan).
650     *
651     * Parameters:
652     * scanner: Scanner the scan ended on.
653     * remote: Remote struct containing information regarding remote end
654     *
655     * Return: NONE
656     */
657 michael 5116 static void
658     scan_end(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, void *data)
659 michael 5052 {
660 michael 5116 struct scan_struct *ss;
661     struct scanner_struct *scs;
662 michael 5052
663 michael 5116 scs = data;
664     ss = remote->data;
665 michael 5052
666 michael 5116 if (OPT_DEBUG)
667     log_printf("SCAN -> Scan %s [%s] completed", remote->ip, scs->name);
668 michael 5052
669 michael 5116 --ss->scans;
670     scan_checkfinished(ss);
671 michael 5052 }
672    
673     /* scan_handle_error CALLBACK
674     *
675     * Called by libopm when an error occurs with a specific connection. This
676     * does not mean the entire scan has ended.
677     *
678     * Parameters:
679     * scanner: Scanner where the error occured.
680     * remote: Remote struct containing information regarding remote end
681     * err: OPM_ERROR code describing the error.
682     *
683     * Return: NONE
684     */
685 michael 5116 static void
686     scan_handle_error(OPM_T *scanner, OPM_REMOTE_T *remote, int err, void *data)
687 michael 5052 {
688 michael 5116 struct scan_struct *ss;
689     struct scanner_struct *scs;
690 michael 5052
691 michael 5116 scs = data;
692     ss = remote->data;
693 michael 5052
694 michael 5116 switch (err)
695     {
696 michael 5284 case OPM_ERR_MAX_READ:
697     if (OPT_DEBUG >= 2)
698     log_printf("SCAN -> Max read on %s:%d (%s) [%s] (%d bytes read)",
699     remote->ip, remote->port, scan_gettype(remote->protocol),
700     scs->name, remote->bytes_read);
701 michael 5114
702 michael 5284 if (ss->manual_target)
703     irc_send("PRIVMSG %s :CHECK -> Negotiation failed %s:%d (%s) "
704     "[%s] (%d bytes read)", ss->manual_target->name,
705     remote->ip, remote->port, scan_gettype(remote->protocol),
706     scs->name, remote->bytes_read);
707     break;
708     case OPM_ERR_BIND:
709     log_printf("SCAN -> Bind error on %s:%d (%s) [%s]", remote->ip,
710     remote->port, scan_gettype(remote->protocol), scs->name);
711     break;
712     case OPM_ERR_NOFD:
713     log_printf("SCAN -> File descriptor allocation error %s:%d (%s) "
714     "[%s]", remote->ip, remote->port,
715     scan_gettype(remote->protocol), scs->name);
716 michael 5116
717 michael 5284 if (ss->manual_target)
718     irc_send("PRIVMSG %s :CHECK -> Scan failed %s:%d (%s) [%s] "
719     "(file descriptor allocation error)",
720     ss->manual_target->name, remote->ip, remote->port,
721     scan_gettype(remote->protocol), scs->name);
722     break;
723     default: /* Unknown Error! */
724     if (OPT_DEBUG)
725     log_printf("SCAN -> Unknown error %s:%d (%s) [%s]", remote->ip,
726     remote->port, scan_gettype(remote->protocol), scs->name);
727     break;
728 michael 5116 }
729 michael 5052 }
730    
731     /* scan_negative
732     *
733     * Remote host (defined by ss) has passed all tests.
734     *
735     * Parameters:
736     * ss: scan_struct containing information regarding negative host.
737     *
738     * Return: NONE
739     *
740     */
741 michael 5114 static void
742 michael 5338 scan_negative(const struct scan_struct *ss)
743 michael 5114 {
744     /* Insert IP in negcache */
745 michael 5695 if (OptionsItem->negcache)
746 michael 5114 {
747     if (OPT_DEBUG >= 2)
748     log_printf("SCAN -> Adding %s to negative cache", ss->ip);
749 michael 5052
750 michael 5114 negcache_insert(ss->ip);
751     }
752 michael 5052 }
753    
754     /* scan_irckline
755     *
756 michael 5876 * ss has been found as a positive host and is to be klined.
757 michael 5052 * Format a kline message using the kline message provided
758     * as a format, then pass it to irc_send() to be sent to the remote server.
759     *
760     * Parameters:
761     * ss: scan_struct containing information regarding host to be klined
762     * format: kline message to format
763     * type: type of proxy found (%t format character)
764 michael 5876 *
765 michael 5052 * Return: NONE
766     */
767 michael 5114 static void
768 michael 5338 scan_irckline(const struct scan_struct *ss, const char *format, const char *type)
769 michael 5052 {
770 michael 6237 char message[MSGLENMAX] = ""; /* OUTPUT */
771 michael 5052
772 michael 5114 unsigned int pos = 0; /* position in format */
773     unsigned int len = 0; /* position in message */
774     unsigned int size = 0; /* temporary size buffer */
775 michael 6025 struct kline_format_assoc
776 michael 5114 {
777 michael 6025 const char key;
778 michael 6030 const char *data;
779 michael 6025 } table[] =
780     {
781 michael 6029 { 'i', NULL },
782     { 'h', NULL },
783     { 'u', NULL },
784     { 'n', NULL },
785     { 't', NULL },
786     { '\0', NULL }
787 michael 5114 };
788 michael 5052
789 michael 5114 table[0].data = ss->ip;
790     table[1].data = ss->irc_hostname;
791     table[2].data = ss->irc_username;
792     table[3].data = ss->irc_nick;
793     table[4].data = type;
794 michael 5052
795 michael 5114 /*
796     * Copy format to message character by character, inserting any matching
797     * data after %.
798     */
799     while (format[pos] != '\0' && len < (MSGLENMAX - 2))
800     {
801     switch (format[pos])
802     {
803 michael 5284 case '%':
804     /* % is the last char in the string, move on */
805     if (format[pos + 1] == '\0')
806     continue;
807 michael 5052
808 michael 5284 /* %% escapes % and becomes % */
809     if (format[pos + 1] == '%')
810     {
811     message[len++] = '%';
812     ++pos; /* Skip past the escaped % */
813     break;
814     }
815 michael 5052
816 michael 5284 /* Safe to check against table now */
817 michael 6029 for (const struct kline_format_assoc *tab = table; tab->key; ++tab)
818 michael 5284 {
819 michael 6029 if (tab->key == format[pos + 1])
820 michael 5284 {
821 michael 6029 size = strlen(tab->data);
822 michael 5284
823     /* Check if the new string can fit! */
824     if ((size + len) > (MSGLENMAX - 1))
825     break;
826     else
827 michael 5052 {
828 michael 6029 strlcat(message, tab->data, sizeof(message));
829 michael 5284 len += size;
830 michael 5052 }
831 michael 5284 }
832     }
833 michael 5052
834 michael 5284 /* Skip key character */
835     ++pos;
836     break;
837 michael 5052
838 michael 5284 default:
839     message[len++] = format[pos];
840     message[len] = '\0';
841     break;
842     }
843 michael 5052
844 michael 5284 /* Continue to next character in format */
845     ++pos;
846 michael 5114 }
847    
848     irc_send("%s", message);
849 michael 5052 }
850    
851     /* scan_manual
852     *
853     * Create a manual scan. A manual scan is a scan where the
854     * scan_struct contains a manual_target pointer.
855     */
856 michael 5114 void
857 michael 5338 scan_manual(char *param, const struct ChannelConf *target)
858 michael 5052 {
859 michael 5374 char buf[INET6_ADDRSTRLEN];
860     const void *addr = NULL;
861 michael 5114 struct scan_struct *ss;
862     struct scanner_struct *scs;
863 michael 5374 const char *ip = NULL;
864 michael 5114 char *scannername;
865 michael 5695 node_t *node;
866 michael 5114 int ret;
867 michael 5052
868 michael 5114 /* If there were no parameters sent, simply alert the user and return */
869     if (param == NULL)
870     {
871     irc_send("PRIVMSG %s :OPM -> Invalid parameters.", target->name);
872     return;
873     }
874 michael 5052
875 michael 5114 /*
876     * Try to extract a scanner name from param, otherwise we'll be
877     * adding to all scanners
878     */
879     ip = param;
880 michael 5052
881 michael 5114 if ((scannername = strchr(param, ' ')))
882     {
883     *scannername = '\0';
884     scannername++;
885     }
886 michael 5052
887 michael 5114 /* If IP is a hostname, resolve it using gethostbyname (which will block!) */
888     if ((addr = firedns_resolveip4(ip)) == NULL)
889     {
890     irc_send("PRIVMSG %s :CHECK -> Error resolving host '%s': %s",
891 michael 5390 target->name, ip, firedns_strerror(firedns_errno));
892 michael 5114 return;
893     }
894 michael 5052
895 michael 5726 /* IP = the resolved IP now (it was the IP or hostname before) */
896 michael 5374 if ((ip = inet_ntop(AF_INET, addr, buf, sizeof(buf))) == NULL)
897     {
898 michael 5377 irc_send("PRIVMSG %s :CHECK -> invalid address: %s",
899     target->name, strerror(errno));
900 michael 5374 return;
901     }
902 michael 5052
903 michael 6149 ss = xcalloc(sizeof(*ss));
904 michael 5114 ss->ip = xstrdup(ip);
905     ss->remote = opm_remote_create(ss->ip);
906     ss->remote->data = ss;
907     ss->manual_target = target;
908 michael 5052
909 michael 5114 if (scannername)
910     irc_send("PRIVMSG %s :CHECK -> Checking '%s' for open proxies [%s]",
911     target->name, ip, scannername);
912     else
913     irc_send("PRIVMSG %s :CHECK -> Checking '%s' for open proxies on all "
914     "scanners", target->name, ip);
915 michael 5052
916 michael 5114 if (LIST_SIZE(OpmItem->blacklists) > 0)
917     dnsbl_add(ss);
918 michael 5052
919 michael 5284 /* Add ss->remote to all scanners */
920 michael 5695 LIST_FOREACH(node, SCANNERS->head)
921 michael 5284 {
922 michael 5695 scs = node->data;
923 michael 5052
924 michael 5284 /*
925     * If we have a scannername, only allow that scanner
926     * to be used
927     */
928     if (scannername)
929     if (strcasecmp(scannername, scs->name))
930     continue;
931 michael 5052
932 michael 5284 if (OPT_DEBUG)
933     log_printf("SCAN -> Passing %s to scanner [%s] (MANUAL SCAN)", ip, scs->name);
934 michael 5052
935 michael 5284 if ((ret = opm_scan(scs->scanner, ss->remote)) != OPM_SUCCESS)
936     {
937     switch (ret)
938 michael 5052 {
939 michael 5284 case OPM_ERR_NOPROTOCOLS:
940     break;
941     case OPM_ERR_BADADDR:
942     irc_send("PRIVMSG %s :OPM -> Bad address %s [%s]",
943     ss->manual_target->name, ss->ip, scs->name);
944     break;
945     default:
946     irc_send("PRIVMSG %s :OPM -> Unknown error %s [%s]",
947     ss->manual_target->name, ss->ip, scs->name);
948     break;
949 michael 5052 }
950 michael 5284 }
951     else
952     ++ss->scans; /* Increase scan count only if OPM_SUCCESS */
953     }
954 michael 5052
955 michael 5114 /*
956     * If all of the scanners gave !OPM_SUCCESS and there were no dnsbl checks,
957     * cleanup here
958     */
959     if (ss->scans == 0)
960     {
961     if (scannername)
962     irc_send("PRIVMSG %s :CHECK -> No such scanner '%s', or '%s' has "
963 michael 5052 "0 protocols.", ss->manual_target->name, scannername,
964     scannername);
965    
966 michael 5114 irc_send("PRIVMSG %s :CHECK -> No scans active on '%s', aborting scan.",
967     ss->manual_target->name, ss->ip);
968     scan_free(ss);
969     }
970 michael 5052 }
971    
972     /* scan_checkexempt
973 michael 5876 *
974 michael 5052 * Check mask against exempt list.
975 michael 5876 *
976 michael 5052 * Parameters:
977     * mask: Mask to check
978 michael 5876 *
979 michael 5052 * Return:
980     * 1 if mask is in list
981 michael 5876 * 0 if mask is not in list
982 michael 5052 */
983 michael 5114 int
984 michael 5279 scan_checkexempt(const char *mask, const char *ipmask)
985 michael 5052 {
986 michael 5114 node_t *node;
987 michael 5052
988 michael 5114 LIST_FOREACH(node, ExemptItem->masks->head)
989     {
990 michael 5279 const char *exempt_mask = node->data;
991 michael 5052
992 michael 5114 if (!match(exempt_mask, mask) || !match(exempt_mask, ipmask))
993     return 1;
994     }
995    
996     return 0;
997 michael 5052 }
998    
999     /* scan_log
1000     *
1001     * Log the fact that a given ip/port/protocol has just been scanned, if the
1002     * user has asked for this to be logged.
1003     *
1004     * Parameters:
1005     * remote: OPM_REMOTE_T for the remote end
1006     */
1007 michael 5114 static void
1008     scan_log(OPM_REMOTE_T *remote)
1009 michael 5052 {
1010 michael 5659 char buf_present[32];
1011     time_t present = 0;
1012 michael 5114 struct scan_struct *ss = remote->data;
1013 michael 5052
1014 michael 5114 if (!(OptionsItem->scanlog && scanlogfile))
1015     return;
1016 michael 5052
1017 michael 5114 time(&present);
1018 michael 5659 strftime(buf_present, sizeof(buf_present), "%FT%H:%M:%S%z", localtime(&present));
1019 michael 5052
1020 michael 5114 fprintf(scanlogfile, "[%s] %s:%d (%s) \"%s\"\n", buf_present, remote->ip,
1021     remote->port, scan_gettype(remote->protocol), ss->proof);
1022     fflush(scanlogfile);
1023 michael 5052 }

Properties

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