ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/hopm/branches/1.1.x/src/scan.c
Revision: 6293
Committed: Fri Jul 17 12:01:33 2015 UTC (8 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 26036 byte(s)
Log Message:
- scan.c:scan_connect(): style corrections

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 5116 node_t *p, *p2;
300     struct scan_struct *ss;
301     struct scanner_struct *scs;
302     int ret;
303 michael 5052
304 michael 5116 /*
305     * Have to use MSGLENMAX here because it is unknown what the max size of
306     * username/hostname can be. Some ircds use really mad values for
307     * these.
308     */
309 michael 6292 char hostmask[MSGLENMAX];
310     char addrmask[MSGLENMAX];
311 michael 5052
312 michael 5116 /* Check negcache before anything */
313 michael 5695 if (OptionsItem->negcache)
314 michael 5116 {
315 michael 6289 struct sockaddr_in ip;
316    
317 michael 5311 if (inet_pton(AF_INET, user[3], &ip.sin_addr) <= 0)
318 michael 5284 {
319     log_printf("SCAN -> Invalid IPv4 address '%s'!", user[3]);
320     return;
321     }
322 michael 6293
323     if (check_neg_cache(ip.sin_addr.s_addr))
324 michael 5284 {
325 michael 6293 if (OPT_DEBUG)
326     log_printf("SCAN -> %s!%s@%s (%s) is negatively cached. Skipping all tests.",
327     user[0], user[1], user[2], user[3]);
328     return;
329 michael 5284 }
330     }
331 michael 5052
332 michael 5284 /* Generate user mask */
333 michael 6292 snprintf(hostmask, sizeof(hostmask), "%s!%s@%s", user[0], user[1], user[2]);
334     snprintf(addrmask, sizeof(addrmask), "%s!%s@%s", user[0], user[1], user[3]);
335 michael 5052
336 michael 5284 /* Check exempt list now that we have a mask */
337 michael 6292 if (scan_checkexempt(hostmask, addrmask))
338 michael 5284 {
339     if (OPT_DEBUG)
340 michael 6292 log_printf("SCAN -> %s (%s) is exempt from scanning", hostmask, addrmask);
341 michael 5052
342 michael 5284 return;
343     }
344 michael 5052
345 michael 6293 /* Create scan_struct */
346 michael 5284 ss = scan_create(user, msg);
347 michael 5052
348 michael 5284 /* Store ss in the remote struct, so that in callbacks we have ss */
349     ss->remote->data = ss;
350 michael 5052
351 michael 5284 /* Start checking our DNSBLs */
352 michael 6293 if (LIST_SIZE(OpmItem->blacklists))
353 michael 5284 dnsbl_add(ss);
354    
355     /* Add ss->remote to all matching scanners */
356     LIST_FOREACH(p, SCANNERS->head)
357     {
358     scs = p->data;
359    
360     LIST_FOREACH(p2, scs->masks->head)
361     {
362 michael 5699 const char *scsmask = p2->data;
363 michael 5284
364 michael 6292 if (!match(scsmask, hostmask))
365 michael 5052 {
366 michael 5284 if (OPT_DEBUG)
367 michael 6292 log_printf("SCAN -> Passing %s to scanner [%s]", hostmask, scs->name);
368 michael 5052
369 michael 5284 if ((ret = opm_scan(scs->scanner, ss->remote)) != OPM_SUCCESS)
370     {
371     switch (ret)
372     {
373     case OPM_ERR_NOPROTOCOLS:
374     continue;
375     break;
376     case OPM_ERR_BADADDR:
377     log_printf("OPM -> Bad address %s [%s].",
378     (ss->manual_target ? ss->manual_target->name :
379     "(unknown)"), ss->ip);
380     break;
381     default:
382     log_printf("OPM -> Unknown error %s [%s].",
383     (ss->manual_target ? ss->manual_target->name :
384     "(unknown)"), ss->ip);
385     break;
386     }
387     }
388     else
389     ++ss->scans; /* Increase scan count only if OPM_SUCCESS */
390 michael 5052
391 michael 5284 break; /* Continue to next scanner */
392 michael 5052 }
393 michael 5284 }
394     }
395 michael 5052
396 michael 5116 /* All scanners returned !OPM_SUCCESS and there were no dnsbl checks */
397     if (ss->scans == 0)
398     scan_free(ss);
399 michael 5052 }
400    
401     /* scan_create
402     *
403 michael 5116 * Allocate scan struct, including user information and REMOTE
404 michael 5052 * for LIBOPM.
405     *
406     * Parameters:
407     * user: Parsed items from the connection notice:
408     * user[0] = connecting users nickname
409     * user[1] = connecting users username
410     * user[2] = connecting users hostname
411     * user[3] = connecting users IP
412     * msg = Original connect notice (used as PROOF)
413     *
414     * Return: Pointer to new scan_struct
415     *
416     */
417 michael 5322 static struct scan_struct *
418 michael 5338 scan_create(const char *user[], const char *msg)
419 michael 5052 {
420 michael 6149 struct scan_struct *ss = xcalloc(sizeof(*ss));
421 michael 5052
422 michael 5116 ss->irc_nick = xstrdup(user[0]);
423     ss->irc_username = xstrdup(user[1]);
424     ss->irc_hostname = xstrdup(user[2]);
425     ss->ip = xstrdup(user[3]);
426     ss->proof = xstrdup(msg);
427     ss->remote = opm_remote_create(ss->ip);
428 michael 5052
429 michael 5116 return ss;
430 michael 5052 }
431    
432     /* scan_free
433     *
434     * Free a scan_struct. This should only be done if the scan struct has
435     * no scans left!
436     *
437     * Parameters:
438     * ss: scan_struct to free
439 michael 5116 *
440 michael 5052 * Return: NONE
441     */
442 michael 5322 static void
443 michael 5114 scan_free(struct scan_struct *ss)
444 michael 5052 {
445 michael 5426 xfree(ss->irc_nick);
446     xfree(ss->irc_username);
447     xfree(ss->irc_hostname);
448     xfree(ss->ip);
449     xfree(ss->proof);
450 michael 5052
451 michael 5114 opm_remote_free(ss->remote);
452 michael 5426 xfree(ss);
453 michael 5052 }
454    
455     /* scan_checkfinished
456     *
457     * Check if a scan is complete (ss->scans <= 0)
458     * and free it if need be.
459     */
460 michael 5114 void
461     scan_checkfinished(struct scan_struct *ss)
462 michael 5052 {
463 michael 5116 if (ss->scans <= 0)
464     {
465     if (ss->manual_target)
466     irc_send("PRIVMSG %s :CHECK -> All tests on %s completed.",
467 michael 5052 ss->manual_target->name, ss->ip);
468 michael 5116 else
469     {
470     if (OPT_DEBUG)
471     /* If there was a manual_target, then irc_nick, etc is NULL. */
472     log_printf("SCAN -> All tests on %s!%s@%s complete.",
473     ss->irc_nick, ss->irc_username, ss->irc_hostname);
474 michael 5052
475 michael 5116 /* Scan was a negative */
476 michael 5322 if (ss->positive == 0)
477 michael 5116 scan_negative(ss);
478     }
479 michael 5052
480 michael 5116 scan_free(ss);
481     }
482 michael 5052 }
483    
484     /* scan_positive
485     *
486     * Remote host (defined by ss) has been found positive by one or more
487     * tests.
488     *
489     * Parameters:
490 michael 5116 * ss: scan_struct containing information regarding positive host
491 michael 5052 * kline: command to send to IRC server to ban the user (see scan_irckline)
492     * type: string of the type of proxy found to be running on the host
493     *
494     * Return: NONE
495     *
496     */
497 michael 5114 void
498     scan_positive(struct scan_struct *ss, const char *kline, const char *type)
499 michael 5052 {
500 michael 5114 node_t *node;
501 michael 5052
502 michael 5114 /* If already a positive, don't kline/close again */
503     if (ss->positive)
504     return;
505 michael 5052
506 michael 5114 /* Format KLINE and send to IRC server */
507     scan_irckline(ss, kline, type);
508 michael 5052
509 michael 5114 /* Speed up the cleanup procedure */
510     /* Close all scans prematurely */
511     LIST_FOREACH(node, SCANNERS->head)
512     {
513     OPM_T *scanner = ((struct scanner_struct *)node->data)->scanner;
514     opm_end(scanner, ss->remote);
515     }
516 michael 5052
517 michael 5114 /* Set it as a positive (to avoid a scan_negative call later on */
518     ss->positive = 1;
519 michael 5052 }
520    
521     /* scan_open_proxy CALLBACK
522     *
523     * Called by libopm when a proxy is verified open.
524     *
525     * Parameters:
526     * scanner: Scanner that found the open proxy.
527     * remote: Remote struct containing information regarding remote end
528     *
529     * Return: NONE
530     */
531 michael 5338 static void
532 michael 5114 scan_open_proxy(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, void *data)
533 michael 5052 {
534 michael 5116 struct scan_struct *ss;
535     struct scanner_struct *scs;
536 michael 5052
537 michael 5116 /* Record that a scan happened */
538     scan_log(remote);
539 michael 5052
540 michael 5116 scs = data;
541     ss = remote->data;
542 michael 5052
543 michael 5116 if (ss->manual_target == NULL)
544     {
545     /* kline and close scan */
546     scan_positive(ss, IRCItem->kline, scan_gettype(remote->protocol));
547 michael 5052
548 michael 5116 /* Report to blacklist */
549     dnsbl_report(ss);
550 michael 5052
551 michael 5116 irc_send_channels("OPEN PROXY -> %s!%s@%s %s:%d (%s) [%s]",
552     ss->irc_nick, ss->irc_username, ss->irc_hostname, remote->ip,
553     remote->port, scan_gettype(remote->protocol), scs->name);
554     log_printf("SCAN -> OPEN PROXY %s!%s@%s %s:%d (%s) [%s]",
555     ss->irc_nick, ss->irc_username, ss->irc_hostname, remote->ip,
556     remote->port, scan_gettype(remote->protocol), scs->name);
557     }
558     else
559     {
560     irc_send("PRIVMSG %s :CHECK -> OPEN PROXY %s:%d (%s) [%s]",
561     ss->manual_target->name, remote->ip, remote->port,
562     scan_gettype(remote->protocol), scs->name);
563     log_printf("SCAN -> OPEN PROXY %s:%d (%s) [%s]", remote->ip,
564     remote->port, scan_gettype(remote->protocol), scs->name);
565     }
566 michael 5052
567 michael 5116 /* Record the proxy for stats purposes */
568     stats_openproxy(remote->protocol);
569 michael 5052 }
570    
571     /* scan_negotiation_failed CALLBACK
572     *
573     * Called by libopm when negotiation of a specific protocol failed.
574     *
575     * Parameters:
576     * scanner: Scanner where the negotiation failed.
577     * remote: Remote struct containing information regarding remote end
578     *
579     * Return: NONE
580     *
581     */
582 michael 5338 static void
583 michael 5116 scan_negotiation_failed(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, void *data)
584 michael 5052 {
585 michael 6285 const struct scanner_struct *scs = data;
586 michael 5052
587 michael 5116 /* Record that a scan happened */
588     scan_log(remote);
589 michael 5052
590 michael 5116 if (OPT_DEBUG)
591     log_printf("SCAN -> Negotiation failed %s:%d (%s) [%s] (%d bytes read)",
592 michael 6285 remote->ip, remote->port, scan_gettype(remote->protocol), scs->name,
593     remote->bytes_read);
594 michael 5052 }
595    
596     /* scan_timeout CALLBACK
597     *
598     * Called by libopm when the negotiation of a specific protocol timed out.
599     *
600     * Parameters:
601     * scanner: Scanner where the connection timed out.
602     * remote: Remote struct containing information regarding remote end
603     *
604     * Return: NONE
605     *
606     */
607 michael 5116 static void
608     scan_timeout(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, void *data)
609 michael 5052 {
610 michael 6285 const struct scanner_struct *scs = data;
611 michael 5052
612 michael 5116 /* Record that a scan happened */
613     scan_log(remote);
614 michael 5052
615 michael 5116 if (OPT_DEBUG)
616     log_printf("SCAN -> Negotiation timed out %s:%d (%s) [%s] (%d bytes read)",
617 michael 6285 remote->ip, remote->port, scan_gettype(remote->protocol), scs->name,
618 michael 5116 remote->bytes_read);
619 michael 5052 }
620    
621     /* scan_end CALLBACK
622     *
623     * Called by libopm when a specific SCAN has completed (all protocols in
624     * that scan).
625     *
626     * Parameters:
627     * scanner: Scanner the scan ended on.
628     * remote: Remote struct containing information regarding remote end
629     *
630     * Return: NONE
631     */
632 michael 5116 static void
633     scan_end(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, void *data)
634 michael 5052 {
635 michael 5116 struct scan_struct *ss;
636     struct scanner_struct *scs;
637 michael 5052
638 michael 5116 scs = data;
639     ss = remote->data;
640 michael 5052
641 michael 5116 if (OPT_DEBUG)
642     log_printf("SCAN -> Scan %s [%s] completed", remote->ip, scs->name);
643 michael 5052
644 michael 5116 --ss->scans;
645     scan_checkfinished(ss);
646 michael 5052 }
647    
648     /* scan_handle_error CALLBACK
649     *
650     * Called by libopm when an error occurs with a specific connection. This
651     * does not mean the entire scan has ended.
652     *
653     * Parameters:
654     * scanner: Scanner where the error occured.
655     * remote: Remote struct containing information regarding remote end
656     * err: OPM_ERROR code describing the error.
657     *
658     * Return: NONE
659     */
660 michael 5116 static void
661     scan_handle_error(OPM_T *scanner, OPM_REMOTE_T *remote, int err, void *data)
662 michael 5052 {
663 michael 5116 struct scan_struct *ss;
664     struct scanner_struct *scs;
665 michael 5052
666 michael 5116 scs = data;
667     ss = remote->data;
668 michael 5052
669 michael 5116 switch (err)
670     {
671 michael 5284 case OPM_ERR_MAX_READ:
672     if (OPT_DEBUG >= 2)
673     log_printf("SCAN -> Max read on %s:%d (%s) [%s] (%d bytes read)",
674     remote->ip, remote->port, scan_gettype(remote->protocol),
675     scs->name, remote->bytes_read);
676 michael 5114
677 michael 5284 if (ss->manual_target)
678     irc_send("PRIVMSG %s :CHECK -> Negotiation failed %s:%d (%s) "
679     "[%s] (%d bytes read)", ss->manual_target->name,
680     remote->ip, remote->port, scan_gettype(remote->protocol),
681     scs->name, remote->bytes_read);
682     break;
683     case OPM_ERR_BIND:
684     log_printf("SCAN -> Bind error on %s:%d (%s) [%s]", remote->ip,
685     remote->port, scan_gettype(remote->protocol), scs->name);
686     break;
687     case OPM_ERR_NOFD:
688     log_printf("SCAN -> File descriptor allocation error %s:%d (%s) "
689     "[%s]", remote->ip, remote->port,
690     scan_gettype(remote->protocol), scs->name);
691 michael 5116
692 michael 5284 if (ss->manual_target)
693     irc_send("PRIVMSG %s :CHECK -> Scan failed %s:%d (%s) [%s] "
694     "(file descriptor allocation error)",
695     ss->manual_target->name, remote->ip, remote->port,
696     scan_gettype(remote->protocol), scs->name);
697     break;
698     default: /* Unknown Error! */
699     if (OPT_DEBUG)
700     log_printf("SCAN -> Unknown error %s:%d (%s) [%s]", remote->ip,
701     remote->port, scan_gettype(remote->protocol), scs->name);
702     break;
703 michael 5116 }
704 michael 5052 }
705    
706     /* scan_negative
707     *
708     * Remote host (defined by ss) has passed all tests.
709     *
710     * Parameters:
711     * ss: scan_struct containing information regarding negative host.
712     *
713     * Return: NONE
714     *
715     */
716 michael 5114 static void
717 michael 5338 scan_negative(const struct scan_struct *ss)
718 michael 5114 {
719     /* Insert IP in negcache */
720 michael 5695 if (OptionsItem->negcache)
721 michael 5114 {
722     if (OPT_DEBUG >= 2)
723     log_printf("SCAN -> Adding %s to negative cache", ss->ip);
724 michael 5052
725 michael 5114 negcache_insert(ss->ip);
726     }
727 michael 5052 }
728    
729     /* scan_irckline
730     *
731 michael 5876 * ss has been found as a positive host and is to be klined.
732 michael 5052 * Format a kline message using the kline message provided
733     * as a format, then pass it to irc_send() to be sent to the remote server.
734     *
735     * Parameters:
736     * ss: scan_struct containing information regarding host to be klined
737     * format: kline message to format
738     * type: type of proxy found (%t format character)
739 michael 5876 *
740 michael 5052 * Return: NONE
741     */
742 michael 5114 static void
743 michael 5338 scan_irckline(const struct scan_struct *ss, const char *format, const char *type)
744 michael 5052 {
745 michael 6238 char message[MSGLENMAX] = ""; /* OUTPUT */
746 michael 5052
747 michael 5114 unsigned int pos = 0; /* position in format */
748     unsigned int len = 0; /* position in message */
749     unsigned int size = 0; /* temporary size buffer */
750 michael 6025 struct kline_format_assoc
751 michael 5114 {
752 michael 6025 const char key;
753 michael 6030 const char *data;
754 michael 6025 } table[] =
755     {
756 michael 6288 { 'i', ss->ip },
757     { 'h', ss->irc_hostname },
758     { 'u', ss->irc_username },
759     { 'n', ss->irc_nick },
760     { 't', type },
761     { '\0', NULL }
762 michael 5114 };
763 michael 5052
764 michael 5114 /*
765     * Copy format to message character by character, inserting any matching
766     * data after %.
767     */
768     while (format[pos] != '\0' && len < (MSGLENMAX - 2))
769     {
770     switch (format[pos])
771     {
772 michael 5284 case '%':
773     /* % is the last char in the string, move on */
774     if (format[pos + 1] == '\0')
775     continue;
776 michael 5052
777 michael 5284 /* %% escapes % and becomes % */
778     if (format[pos + 1] == '%')
779     {
780     message[len++] = '%';
781     ++pos; /* Skip past the escaped % */
782     break;
783     }
784 michael 5052
785 michael 5284 /* Safe to check against table now */
786 michael 6029 for (const struct kline_format_assoc *tab = table; tab->key; ++tab)
787 michael 5284 {
788 michael 6029 if (tab->key == format[pos + 1])
789 michael 5284 {
790 michael 6029 size = strlen(tab->data);
791 michael 5284
792     /* Check if the new string can fit! */
793     if ((size + len) > (MSGLENMAX - 1))
794     break;
795     else
796 michael 5052 {
797 michael 6029 strlcat(message, tab->data, sizeof(message));
798 michael 5284 len += size;
799 michael 5052 }
800 michael 5284 }
801     }
802 michael 5052
803 michael 5284 /* Skip key character */
804     ++pos;
805     break;
806 michael 5052
807 michael 5284 default:
808     message[len++] = format[pos];
809     message[len] = '\0';
810     break;
811     }
812 michael 5052
813 michael 5284 /* Continue to next character in format */
814     ++pos;
815 michael 5114 }
816    
817     irc_send("%s", message);
818 michael 5052 }
819    
820     /* scan_manual
821     *
822     * Create a manual scan. A manual scan is a scan where the
823     * scan_struct contains a manual_target pointer.
824     */
825 michael 5114 void
826 michael 5338 scan_manual(char *param, const struct ChannelConf *target)
827 michael 5052 {
828 michael 5374 char buf[INET6_ADDRSTRLEN];
829     const void *addr = NULL;
830 michael 5114 struct scan_struct *ss;
831     struct scanner_struct *scs;
832 michael 5374 const char *ip = NULL;
833 michael 5114 char *scannername;
834 michael 5695 node_t *node;
835 michael 5114 int ret;
836 michael 5052
837 michael 5114 /* If there were no parameters sent, simply alert the user and return */
838     if (param == NULL)
839     {
840     irc_send("PRIVMSG %s :OPM -> Invalid parameters.", target->name);
841     return;
842     }
843 michael 5052
844 michael 5114 /*
845     * Try to extract a scanner name from param, otherwise we'll be
846     * adding to all scanners
847     */
848     ip = param;
849 michael 5052
850 michael 5114 if ((scannername = strchr(param, ' ')))
851     {
852     *scannername = '\0';
853     scannername++;
854     }
855 michael 5052
856 michael 5114 /* If IP is a hostname, resolve it using gethostbyname (which will block!) */
857     if ((addr = firedns_resolveip4(ip)) == NULL)
858     {
859     irc_send("PRIVMSG %s :CHECK -> Error resolving host '%s': %s",
860 michael 5390 target->name, ip, firedns_strerror(firedns_errno));
861 michael 5114 return;
862     }
863 michael 5052
864 michael 5726 /* IP = the resolved IP now (it was the IP or hostname before) */
865 michael 5374 if ((ip = inet_ntop(AF_INET, addr, buf, sizeof(buf))) == NULL)
866     {
867 michael 5377 irc_send("PRIVMSG %s :CHECK -> invalid address: %s",
868     target->name, strerror(errno));
869 michael 5374 return;
870     }
871 michael 5052
872 michael 6149 ss = xcalloc(sizeof(*ss));
873 michael 5114 ss->ip = xstrdup(ip);
874     ss->remote = opm_remote_create(ss->ip);
875     ss->remote->data = ss;
876     ss->manual_target = target;
877 michael 5052
878 michael 5114 if (scannername)
879     irc_send("PRIVMSG %s :CHECK -> Checking '%s' for open proxies [%s]",
880     target->name, ip, scannername);
881     else
882     irc_send("PRIVMSG %s :CHECK -> Checking '%s' for open proxies on all "
883     "scanners", target->name, ip);
884 michael 5052
885 michael 5114 if (LIST_SIZE(OpmItem->blacklists) > 0)
886     dnsbl_add(ss);
887 michael 5052
888 michael 5284 /* Add ss->remote to all scanners */
889 michael 5695 LIST_FOREACH(node, SCANNERS->head)
890 michael 5284 {
891 michael 5695 scs = node->data;
892 michael 5052
893 michael 5284 /*
894     * If we have a scannername, only allow that scanner
895     * to be used
896     */
897     if (scannername)
898     if (strcasecmp(scannername, scs->name))
899     continue;
900 michael 5052
901 michael 5284 if (OPT_DEBUG)
902     log_printf("SCAN -> Passing %s to scanner [%s] (MANUAL SCAN)", ip, scs->name);
903 michael 5052
904 michael 5284 if ((ret = opm_scan(scs->scanner, ss->remote)) != OPM_SUCCESS)
905     {
906     switch (ret)
907 michael 5052 {
908 michael 5284 case OPM_ERR_NOPROTOCOLS:
909     break;
910     case OPM_ERR_BADADDR:
911     irc_send("PRIVMSG %s :OPM -> Bad address %s [%s]",
912     ss->manual_target->name, ss->ip, scs->name);
913     break;
914     default:
915     irc_send("PRIVMSG %s :OPM -> Unknown error %s [%s]",
916     ss->manual_target->name, ss->ip, scs->name);
917     break;
918 michael 5052 }
919 michael 5284 }
920     else
921     ++ss->scans; /* Increase scan count only if OPM_SUCCESS */
922     }
923 michael 5052
924 michael 5114 /*
925     * If all of the scanners gave !OPM_SUCCESS and there were no dnsbl checks,
926     * cleanup here
927     */
928     if (ss->scans == 0)
929     {
930     if (scannername)
931     irc_send("PRIVMSG %s :CHECK -> No such scanner '%s', or '%s' has "
932 michael 5052 "0 protocols.", ss->manual_target->name, scannername,
933     scannername);
934    
935 michael 5114 irc_send("PRIVMSG %s :CHECK -> No scans active on '%s', aborting scan.",
936     ss->manual_target->name, ss->ip);
937     scan_free(ss);
938     }
939 michael 5052 }
940    
941     /* scan_checkexempt
942 michael 5876 *
943 michael 5052 * Check mask against exempt list.
944 michael 5876 *
945 michael 5052 * Parameters:
946     * mask: Mask to check
947 michael 5876 *
948 michael 5052 * Return:
949     * 1 if mask is in list
950 michael 5876 * 0 if mask is not in list
951 michael 5052 */
952 michael 5114 int
953 michael 5279 scan_checkexempt(const char *mask, const char *ipmask)
954 michael 5052 {
955 michael 5114 node_t *node;
956 michael 5052
957 michael 5114 LIST_FOREACH(node, ExemptItem->masks->head)
958     {
959 michael 5279 const char *exempt_mask = node->data;
960 michael 5052
961 michael 5114 if (!match(exempt_mask, mask) || !match(exempt_mask, ipmask))
962     return 1;
963     }
964    
965     return 0;
966 michael 5052 }
967    
968     /* scan_log
969     *
970     * Log the fact that a given ip/port/protocol has just been scanned, if the
971     * user has asked for this to be logged.
972     *
973     * Parameters:
974     * remote: OPM_REMOTE_T for the remote end
975     */
976 michael 5114 static void
977     scan_log(OPM_REMOTE_T *remote)
978 michael 5052 {
979 michael 5659 char buf_present[32];
980     time_t present = 0;
981 michael 5114 struct scan_struct *ss = remote->data;
982 michael 5052
983 michael 5114 if (!(OptionsItem->scanlog && scanlogfile))
984     return;
985 michael 5052
986 michael 5114 time(&present);
987 michael 5659 strftime(buf_present, sizeof(buf_present), "%FT%H:%M:%S%z", localtime(&present));
988 michael 5052
989 michael 5114 fprintf(scanlogfile, "[%s] %s:%d (%s) \"%s\"\n", buf_present, remote->ip,
990     remote->port, scan_gettype(remote->protocol), ss->proof);
991     fflush(scanlogfile);
992 michael 5052 }

Properties

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