Project

General

Profile

Download (5.17 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
# Licensed under the Apache License, Version 2.0 (the "License");
12
# you may not use this file except in compliance with the License.
13
# You may obtain a copy of the License at
14
#
15
# http://www.apache.org/licenses/LICENSE-2.0
16
#
17
# Unless required by applicable law or agreed to in writing, software
18
# distributed under the License is distributed on an "AS IS" BASIS,
19
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
# See the License for the specific language governing permissions and
21
# limitations under the License.
22

    
23
# Global variables
24
proc_error=""
25

    
26
# Download and extract if necessary
27
process_url() {
28
	local file=$1
29
	local url=$2
30
	local filename=${url##*/}
31
	local ext=${filename#*.}
32

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

    
35
	if [ ! -f $file ]; then
36
		echo "Could not download ${url}" | logger
37
		proc_error="true"
38
	fi
39

    
40
	case "$ext" in
41
		tar)
42
			mv $file $file.tmp
43
			/usr/bin/tar -xf $file.tmp -O > $file 2> /dev/null
44
			;;
45
		tar.gz)
46
			mv $file $file.tmp
47
			/usr/bin/tar -xzf $file.tmp -O > $file 2> /dev/null
48
			;;
49
		tgz)
50
			mv $file $file.tmp
51
			/usr/bin/tar -xzf $file.tmp -O > $file 2> /dev/null
52
			;;
53
		tar.bz2)
54
			mv $file $file.tmp
55
			/usr/bin/tar -xjf $file.tmp -O > $file 2> /dev/null
56
			;;
57
		*)
58
			;;
59
	esac
60

    
61
	if [ -f $file.tmp ]; then
62
		rm $file.tmp
63
	fi
64

    
65
	if [ ! -f $file ]; then
66
		echo "Could not extract ${filename}" | logger
67
		proc_error="true"
68
	fi
69
}
70

    
71
echo "rc.update_bogons.sh is starting up." | logger
72

    
73
# Sleep for some time, unless an argument is specified.
74
if [ "$1" = "" ]; then
75
	# Grab a random value
76
	value=`od -A n -d -N2 /dev/random | awk '{ print $1 }'`
77
	echo "rc.update_bogons.sh is sleeping for $value" | logger
78
	sleep $value
79
fi
80

    
81
echo "rc.update_bogons.sh is beginning the update cycle." | logger
82

    
83
# Load custom bogon configuration
84
if [ -f /var/etc/bogon_custom ]; then
85
	. /var/etc/bogon_custom
86
fi
87

    
88
# Set default values if not overriden
89
v4url=${v4url:-"https://files.pfsense.org/lists/fullbogons-ipv4.txt"}
90
v6url=${v6url:-"https://files.pfsense.org/lists/fullbogons-ipv6.txt"}
91
v4urlcksum=${v4urlcksum:-"${v4url}.md5"}
92
v6urlcksum=${v6urlcksum:-"${v6url}.md5"}
93

    
94
process_url /tmp/bogons "${v4url}"
95
process_url /tmp/bogonsv6 "${v6url}"
96

    
97
if [ "$proc_error" != "" ]; then
98
	# Relaunch and sleep
99
	sh /etc/rc.update_bogons.sh &
100
	exit
101
fi
102

    
103
BOGON_V4_CKSUM=`/usr/bin/fetch -T 30 -q -o - "${v4urlcksum}" | awk '{ print $4 }'`
104
ON_DISK_V4_CKSUM=`md5 /tmp/bogons | awk '{ print $4 }'`
105
BOGON_V6_CKSUM=`/usr/bin/fetch -T 30 -q -o - "${v6urlcksum}" | awk '{ print $4 }'`
106
ON_DISK_V6_CKSUM=`md5 /tmp/bogonsv6 | awk '{ print $4 }'`
107

    
108
if [ "$BOGON_V4_CKSUM" = "$ON_DISK_V4_CKSUM" ] || [ "$BOGON_V6_CKSUM" = "$ON_DISK_V6_CKSUM" ]; then
109
	# At least one of the downloaded checksums matches, so mount RW
110
	/etc/rc.conf_mount_rw
111

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

    
114
	if [ "$BOGON_V4_CKSUM" = "$ON_DISK_V4_CKSUM" ]; then
115
		ENTRIES_TOT=`pfctl -vvsTables | awk '/Addresses/ {s+=$2}; END {print s}'`
116
		ENTRIES_V4=`pfctl -vvsTables | awk '/-\tbogons$/ {getline; print $2}'`
117
		LINES_V4=`wc -l /tmp/bogons | awk '{ print $1 }'`
118
		if [ $ENTRIES_MAX -gt $((2*ENTRIES_TOT-${ENTRIES_V4:-0}+LINES_V4)) ]; then
119
			egrep -v "^192.168.0.0/16|^172.16.0.0/12|^10.0.0.0/8" /tmp/bogons > /etc/bogons
120
			RESULT=`/sbin/pfctl -t bogons -T replace -f /etc/bogons 2>&1`
121
			echo "$RESULT" | awk '{ print "Bogons V4 file downloaded: " $0 }' | logger
122
		else
123
			echo "Not updating IPv4 bogons (increase table-entries limit)" | logger
124
		fi
125
		rm /tmp/bogons
126
	else
127
		echo "Could not download ${v4url} (checksum mismatch)" | logger
128
		checksum_error="true"
129
	fi
130

    
131
	if [ "$BOGON_V6_CKSUM" = "$ON_DISK_V6_CKSUM" ]; then
132
		BOGONS_V6_TABLE_COUNT=`pfctl -sTables | grep ^bogonsv6$ | wc -l | awk '{ print $1 }'`
133
		ENTRIES_TOT=`pfctl -vvsTables | awk '/Addresses/ {s+=$2}; END {print s}'`
134
		LINES_V6=`wc -l /tmp/bogonsv6 | awk '{ print $1 }'`
135
		if [ $BOGONS_V6_TABLE_COUNT -gt 0 ]; then
136
			ENTRIES_V6=`pfctl -vvsTables | awk '/-\tbogonsv6$/ {getline; print $2}'`
137
			if [ $ENTRIES_MAX -gt $((2*ENTRIES_TOT-${ENTRIES_V6:-0}+LINES_V6)) ]; then
138
				egrep -iv "^fc00::/7" /tmp/bogonsv6 > /etc/bogonsv6
139
				RESULT=`/sbin/pfctl -t bogonsv6 -T replace -f /etc/bogonsv6 2>&1`
140
				echo "$RESULT" | awk '{ print "Bogons V6 file downloaded: " $0 }' | logger
141
			else
142
				echo "Not saving or updating IPv6 bogons (increase table-entries limit)" | logger
143
			fi
144
		else
145
			if [ $ENTRIES_MAX -gt $((2*ENTRIES_TOT+LINES_V6)) ]; then
146
				egrep -iv "^fc00::/7" /tmp/bogonsv6 > /etc/bogonsv6
147
				echo "Bogons V6 file downloaded but not updating IPv6 bogons table because it is not in use." | logger
148
			else
149
				echo "Not saving IPv6 bogons table (IPv6 Allow is off and table-entries limit is potentially too low)" | logger
150
			fi
151
		fi
152
		rm /tmp/bogonsv6
153
	else
154
		echo "Could not download ${v6url} (checksum mismatch)" | logger
155
		checksum_error="true"
156
	fi
157

    
158
	# We mounted RW, so switch back to RO
159
	/etc/rc.conf_mount_ro
160
fi
161

    
162
if [ "$checksum_error" != "" ]; then
163
	# Relaunch and sleep
164
	sh /etc/rc.update_bogons.sh &
165
	exit
166
fi
167

    
168
echo "rc.update_bogons.sh is ending the update cycle." | logger
(75-75/78)