Project

General

Profile

Download (6.8 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2
#
3
# rc.update_bogons.sh
4
#
5
# part of pfSense (https://www.pfsense.org)
6
# Copyright (c) 2004-2016 Electric Sheep Fencing, LLC
7
# All rights reserved.
8
#
9
# Based on src/etc/rc.d/savecore from FreeBSD
10
#
11
# Redistribution and use in source and binary forms, with or without
12
# modification, are permitted provided that the following conditions are met:
13
#
14
# 1. Redistributions of source code must retain the above copyright notice,
15
#    this list of conditions and the following disclaimer.
16
#
17
# 2. Redistributions in binary form must reproduce the above copyright
18
#    notice, this list of conditions and the following disclaimer in
19
#    the documentation and/or other materials provided with the
20
#    distribution.
21
#
22
# 3. All advertising materials mentioning features or use of this software
23
#    must display the following acknowledgment:
24
#    "This product includes software developed by the pfSense Project
25
#    for use in the pfSense® software distribution. (http://www.pfsense.org/).
26
#
27
# 4. The names "pfSense" and "pfSense Project" must not be used to
28
#    endorse or promote products derived from this software without
29
#    prior written permission. For written permission, please contact
30
#    coreteam@pfsense.org.
31
#
32
# 5. Products derived from this software may not be called "pfSense"
33
#    nor may "pfSense" appear in their names without prior written
34
#    permission of the Electric Sheep Fencing, LLC.
35
#
36
# 6. Redistributions of any form whatsoever must retain the following
37
#    acknowledgment:
38
#
39
# "This product includes software developed by the pfSense Project
40
# for use in the pfSense software distribution (http://www.pfsense.org/).
41
#
42
# THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
43
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
45
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
46
# ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
49
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
51
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
53
# OF THE POSSIBILITY OF SUCH DAMAGE.
54

    
55
# Global variables
56
proc_error=""
57

    
58
# Download and extract if necessary
59
process_url() {
60
	local file=$1
61
	local url=$2
62
	local filename=${url##*/}
63
	local ext=${filename#*.}
64

    
65
	/usr/bin/fetch -a -w 600 -T 30 -q -o $file "${url}"
66

    
67
	if [ ! -f $file ]; then
68
		echo "Could not download ${url}" | logger
69
		proc_error="true"
70
	fi
71

    
72
	case "$ext" in
73
		tar)
74
			mv $file $file.tmp
75
			/usr/bin/tar -xf $file.tmp -O > $file 2> /dev/null
76
			;;
77
		tar.gz)
78
			mv $file $file.tmp
79
			/usr/bin/tar -xzf $file.tmp -O > $file 2> /dev/null
80
			;;
81
		tgz)
82
			mv $file $file.tmp
83
			/usr/bin/tar -xzf $file.tmp -O > $file 2> /dev/null
84
			;;
85
		tar.bz2)
86
			mv $file $file.tmp
87
			/usr/bin/tar -xjf $file.tmp -O > $file 2> /dev/null
88
			;;
89
		*)
90
			;;
91
	esac
92

    
93
	if [ -f $file.tmp ]; then
94
		rm $file.tmp
95
	fi
96

    
97
	if [ ! -f $file ]; then
98
		echo "Could not extract ${filename}" | logger
99
		proc_error="true"
100
	fi
101
}
102

    
103
echo "rc.update_bogons.sh is starting up." | logger
104

    
105
# Sleep for some time, unless an argument is specified.
106
if [ "$1" = "" ]; then
107
	# Grab a random value
108
	value=`od -A n -d -N2 /dev/random | awk '{ print $1 }'`
109
	echo "rc.update_bogons.sh is sleeping for $value" | logger
110
	sleep $value
111
fi
112

    
113
echo "rc.update_bogons.sh is beginning the update cycle." | logger
114

    
115
# Load custom bogon configuration
116
if [ -f /var/etc/bogon_custom ]; then
117
	. /var/etc/bogon_custom
118
fi
119

    
120
# Set default values if not overriden
121
v4url=${v4url:-"https://files.pfsense.org/lists/fullbogons-ipv4.txt"}
122
v6url=${v6url:-"https://files.pfsense.org/lists/fullbogons-ipv6.txt"}
123
v4urlcksum=${v4urlcksum:-"${v4url}.md5"}
124
v6urlcksum=${v6urlcksum:-"${v6url}.md5"}
125

    
126
process_url /tmp/bogons "${v4url}"
127
process_url /tmp/bogonsv6 "${v6url}"
128

    
129
if [ "$proc_error" != "" ]; then
130
	# Relaunch and sleep
131
	sh /etc/rc.update_bogons.sh &
132
	exit
133
fi
134

    
135
BOGON_V4_CKSUM=`/usr/bin/fetch -T 30 -q -o - "${v4urlcksum}" | awk '{ print $4 }'`
136
ON_DISK_V4_CKSUM=`md5 /tmp/bogons | awk '{ print $4 }'`
137
BOGON_V6_CKSUM=`/usr/bin/fetch -T 30 -q -o - "${v6urlcksum}" | awk '{ print $4 }'`
138
ON_DISK_V6_CKSUM=`md5 /tmp/bogonsv6 | awk '{ print $4 }'`
139

    
140
if [ "$BOGON_V4_CKSUM" = "$ON_DISK_V4_CKSUM" ] || [ "$BOGON_V6_CKSUM" = "$ON_DISK_V6_CKSUM" ]; then
141
	# At least one of the downloaded checksums matches, so mount RW
142
	/etc/rc.conf_mount_rw
143

    
144
	ENTRIES_MAX=`pfctl -s memory | awk '/table-entries/ { print $4 }'`
145

    
146
	if [ "$BOGON_V4_CKSUM" = "$ON_DISK_V4_CKSUM" ]; then
147
		ENTRIES_TOT=`pfctl -vvsTables | awk '/Addresses/ {s+=$2}; END {print s}'`
148
		ENTRIES_V4=`pfctl -vvsTables | awk '/-\tbogons$/ {getline; print $2}'`
149
		LINES_V4=`wc -l /tmp/bogons | awk '{ print $1 }'`
150
		if [ $ENTRIES_MAX -gt $((2*ENTRIES_TOT-${ENTRIES_V4:-0}+LINES_V4)) ]; then
151
			egrep -v "^192.168.0.0/16|^172.16.0.0/12|^10.0.0.0/8" /tmp/bogons > /etc/bogons
152
			RESULT=`/sbin/pfctl -t bogons -T replace -f /etc/bogons 2>&1`
153
			echo "$RESULT" | awk '{ print "Bogons V4 file downloaded: " $0 }' | logger
154
		else
155
			echo "Not updating IPv4 bogons (increase table-entries limit)" | logger
156
		fi
157
		rm /tmp/bogons
158
	else
159
		echo "Could not download ${v4url} (checksum mismatch)" | logger
160
		checksum_error="true"
161
	fi
162

    
163
	if [ "$BOGON_V6_CKSUM" = "$ON_DISK_V6_CKSUM" ]; then
164
		BOGONS_V6_TABLE_COUNT=`pfctl -sTables | grep ^bogonsv6$ | wc -l | awk '{ print $1 }'`
165
		ENTRIES_TOT=`pfctl -vvsTables | awk '/Addresses/ {s+=$2}; END {print s}'`
166
		LINES_V6=`wc -l /tmp/bogonsv6 | awk '{ print $1 }'`
167
		if [ $BOGONS_V6_TABLE_COUNT -gt 0 ]; then
168
			ENTRIES_V6=`pfctl -vvsTables | awk '/-\tbogonsv6$/ {getline; print $2}'`
169
			if [ $ENTRIES_MAX -gt $((2*ENTRIES_TOT-${ENTRIES_V6:-0}+LINES_V6)) ]; then
170
				egrep -iv "^fc00::/7" /tmp/bogonsv6 > /etc/bogonsv6
171
				RESULT=`/sbin/pfctl -t bogonsv6 -T replace -f /etc/bogonsv6 2>&1`
172
				echo "$RESULT" | awk '{ print "Bogons V6 file downloaded: " $0 }' | logger
173
			else
174
				echo "Not saving or updating IPv6 bogons (increase table-entries limit)" | logger
175
			fi
176
		else
177
			if [ $ENTRIES_MAX -gt $((2*ENTRIES_TOT+LINES_V6)) ]; then
178
				egrep -iv "^fc00::/7" /tmp/bogonsv6 > /etc/bogonsv6
179
				echo "Bogons V6 file downloaded but not updating IPv6 bogons table because it is not in use." | logger
180
			else
181
				echo "Not saving IPv6 bogons table (IPv6 Allow is off and table-entries limit is potentially too low)" | logger
182
			fi
183
		fi
184
		rm /tmp/bogonsv6
185
	else
186
		echo "Could not download ${v6url} (checksum mismatch)" | logger
187
		checksum_error="true"
188
	fi
189

    
190
	# We mounted RW, so switch back to RO
191
	/etc/rc.conf_mount_ro
192
fi
193

    
194
if [ "$checksum_error" != "" ]; then
195
	# Relaunch and sleep
196
	sh /etc/rc.update_bogons.sh &
197
	exit
198
fi
199

    
200
echo "rc.update_bogons.sh is ending the update cycle." | logger
(88-88/94)