File: filterSites.cc

package info (click to toggle)
srg 1.3.5-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,956 kB
  • ctags: 776
  • sloc: sh: 9,073; cpp: 3,383; ansic: 563; makefile: 74; php: 57; yacc: 35; lex: 30
file content (133 lines) | stat: -rw-r--r-- 3,719 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
	SRG - Squid Report Generator
	Destination site filtering.
	
	Copyright (C) 2008 Matt Brown <matt@mattb.net.nz>

	This file is part of SRG.

	SRG is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation version 2.

	SRG is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with SRG; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

*/

#include "srg.h"
#include "prototypes.h"

extern Resolver *dnscache;

/* Global Data Structures */
list<char *> exclude_hostnames;
list<exclude_netblock> exclude_netblocks;

/* Initialise the IP address to user mappings */
void init_sitefilter(void) {

	/* Exit if no site filter file specified */
	if (!srg.sitefilter)
		return;

	/* Read the file */
	Line *iLine = new Line(srg.sitefilter);
	if (iLine->getError() || iLine->eof()) {
		fprintf(stderr, "%s: Could not open the destination site "
				"filter file: %s\n", progname, srg.sitefilter);
		exit(1);
	}

	/* Process each line of the file */
	while(!iLine->eof()) {
		char *excludeLine = iLine->getline();

		/* Check for comment lines */
		if (excludeLine[0] == '\0' || excludeLine[0] == '#')
			continue;

		/* Parse the exclusion type and data */
		char hostname[1024] = {'\0'};
		char network[1024] = {'\0'};
		char netmask[1024] = {'\0'};
		if (sscanf(excludeLine, "host %1023s", hostname) != 1) {
			if (sscanf(excludeLine, "subnet %1023s %1023s",
						network, netmask) != 2) {
				fprintf(stderr, "%s: Invalid line in destination site "
						"filter file: %s\n", progname, srg.sitefilter);
				exit(1);
			}
		}

		/* If a hostname was set, store it and continue */
		if (hostname[0] != '\0') {
			exclude_hostnames.push_back(strdup(hostname));
			continue;
		}

		/* If no hostname was set, try and parse the network/netmask */
		exclude_netblock this_netblock;
		if (inet_aton(network, &this_netblock.network)==0) {
			fprintf(stderr, "Invalid network Address in destination "
					"site filter file: %s\n", network);
			exit(1);
		}
		if (inet_aton(netmask, &this_netblock.netmask)==0) {
			fprintf(stderr, "Invalid netmask in destination site "
					"filter file: %s\n", netmask);
			exit(1);
		}

		/* Add this entry to the list */
		exclude_netblocks.push_back(this_netblock);
	}

	/* Free memory */
	delete iLine;

}

/* Returns true if the specified destination site matches one of the entries in
 * the destination site filter file. */
bool destination_is_excluded(const char *site_hostname) {

	/* Check destination against excluded hostnames first */
	list<char *>::const_iterator iter;
	for (iter=exclude_hostnames.begin(); iter != exclude_hostnames.end();
			iter++) {
		if (strcasecmp((const char *)(*iter), site_hostname) == 0) {
			return true;
		}
	}

	/* Don't continue if there are not netblocks, resolving is slow. */
	if (exclude_netblocks.empty()) {
		return false;
	}

	/* Didn't match any hostnames, resolve to an IP and check netblocks */
	in_addr dest_ip;
	if (dnscache->get_ip(site_hostname, &dest_ip) != 0) {
		/* Could not resolve site_hostname to an IP */
		return false;
	}
	list<exclude_netblock>::const_iterator nb_iter;
	for (nb_iter=exclude_netblocks.begin();
			nb_iter != exclude_netblocks.end();
			nb_iter++) {
		if ((dest_ip.s_addr&(*nb_iter).netmask.s_addr)==
				(*nb_iter).network.s_addr) {
			return true;
		}
	}

	/*  Not found in either list*/
	return false;
}