1
|
#!/bin/sh
|
2
|
#
|
3
|
# rc.update_bogons.sh
|
4
|
#
|
5
|
# part of pfSense (https://www.pfsense.org)
|
6
|
# Copyright (c) 2004-2018 Rubicon Communications, LLC (Netgate)
|
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
|
do_not_send_uniqueid=$(/usr/local/sbin/read_xml_tag.sh boolean system/do_not_send_uniqueid)
|
59
|
if [ "${do_not_send_uniqueid}" != "true" ]; then
|
60
|
uniqueid=$(/usr/sbin/gnid)
|
61
|
export HTTP_USER_AGENT="${product}/${product_version}:${uniqueid}"
|
62
|
else
|
63
|
export HTTP_USER_AGENT="${product}/${product_version}"
|
64
|
fi
|
65
|
|
66
|
# Download and extract if necessary
|
67
|
process_url() {
|
68
|
local file=$1
|
69
|
local url=$2
|
70
|
local filename=${url##*/}
|
71
|
local ext=${filename#*.}
|
72
|
|
73
|
/usr/bin/fetch -a -w 600 -T 30 -q -o $file "${url}"
|
74
|
|
75
|
if [ ! -f $file ]; then
|
76
|
echo "Could not download ${url}" | logger
|
77
|
proc_error="true"
|
78
|
fi
|
79
|
|
80
|
case "$ext" in
|
81
|
tar)
|
82
|
mv $file $file.tmp
|
83
|
/usr/bin/tar -xf $file.tmp -O > $file 2> /dev/null
|
84
|
;;
|
85
|
tar.gz)
|
86
|
mv $file $file.tmp
|
87
|
/usr/bin/tar -xzf $file.tmp -O > $file 2> /dev/null
|
88
|
;;
|
89
|
tgz)
|
90
|
mv $file $file.tmp
|
91
|
/usr/bin/tar -xzf $file.tmp -O > $file 2> /dev/null
|
92
|
;;
|
93
|
tar.bz2)
|
94
|
mv $file $file.tmp
|
95
|
/usr/bin/tar -xjf $file.tmp -O > $file 2> /dev/null
|
96
|
;;
|
97
|
*)
|
98
|
;;
|
99
|
esac
|
100
|
|
101
|
if [ -f $file.tmp ]; then
|
102
|
rm $file.tmp
|
103
|
fi
|
104
|
|
105
|
if [ ! -f $file ]; then
|
106
|
echo "Could not extract ${filename}" | logger
|
107
|
proc_error="true"
|
108
|
fi
|
109
|
}
|
110
|
|
111
|
echo "rc.update_bogons.sh is starting up." | logger
|
112
|
|
113
|
# Sleep for some time, unless an argument is specified.
|
114
|
if [ "$1" = "" ]; then
|
115
|
# Grab a random value
|
116
|
value=`od -A n -d -N2 /dev/random | awk '{ print $1 }'`
|
117
|
echo "rc.update_bogons.sh is sleeping for $value" | logger
|
118
|
sleep $value
|
119
|
fi
|
120
|
|
121
|
echo "rc.update_bogons.sh is beginning the update cycle." | logger
|
122
|
|
123
|
# Load custom bogon configuration
|
124
|
if [ -f /var/etc/bogon_custom ]; then
|
125
|
. /var/etc/bogon_custom
|
126
|
fi
|
127
|
|
128
|
# Set default values if not overriden
|
129
|
v4url=${v4url:-"https://files.pfsense.org/lists/fullbogons-ipv4.txt"}
|
130
|
v6url=${v6url:-"https://files.pfsense.org/lists/fullbogons-ipv6.txt"}
|
131
|
v4urlcksum=${v4urlcksum:-"${v4url}.md5"}
|
132
|
v6urlcksum=${v6urlcksum:-"${v6url}.md5"}
|
133
|
|
134
|
process_url /tmp/bogons "${v4url}"
|
135
|
process_url /tmp/bogonsv6 "${v6url}"
|
136
|
|
137
|
if [ "$proc_error" != "" ]; then
|
138
|
# Relaunch and sleep
|
139
|
sh /etc/rc.update_bogons.sh &
|
140
|
exit
|
141
|
fi
|
142
|
|
143
|
BOGON_V4_CKSUM=`/usr/bin/fetch -T 30 -q -o - "${v4urlcksum}" | awk '{ print $4 }'`
|
144
|
ON_DISK_V4_CKSUM=`md5 /tmp/bogons | awk '{ print $4 }'`
|
145
|
BOGON_V6_CKSUM=`/usr/bin/fetch -T 30 -q -o - "${v6urlcksum}" | awk '{ print $4 }'`
|
146
|
ON_DISK_V6_CKSUM=`md5 /tmp/bogonsv6 | awk '{ print $4 }'`
|
147
|
|
148
|
if [ "$BOGON_V4_CKSUM" = "$ON_DISK_V4_CKSUM" ] || [ "$BOGON_V6_CKSUM" = "$ON_DISK_V6_CKSUM" ]; then
|
149
|
# At least one of the downloaded checksums matches, so mount RW
|
150
|
/etc/rc.conf_mount_rw
|
151
|
|
152
|
ENTRIES_MAX=`pfctl -s memory | awk '/table-entries/ { print $4 }'`
|
153
|
|
154
|
if [ "$BOGON_V4_CKSUM" = "$ON_DISK_V4_CKSUM" ]; then
|
155
|
ENTRIES_TOT=`pfctl -vvsTables | awk '/Addresses/ {s+=$2}; END {print s}'`
|
156
|
ENTRIES_V4=`pfctl -vvsTables | awk '/-\tbogons$/ {getline; print $2}'`
|
157
|
LINES_V4=`wc -l /tmp/bogons | awk '{ print $1 }'`
|
158
|
if [ $ENTRIES_MAX -gt $((2*ENTRIES_TOT-${ENTRIES_V4:-0}+LINES_V4)) ]; then
|
159
|
egrep -v "^192.168.0.0/16|^172.16.0.0/12|^10.0.0.0/8" /tmp/bogons > /etc/bogons
|
160
|
RESULT=`/sbin/pfctl -t bogons -T replace -f /etc/bogons 2>&1`
|
161
|
echo "$RESULT" | awk '{ print "Bogons V4 file downloaded: " $0 }' | logger
|
162
|
else
|
163
|
echo "Not updating IPv4 bogons (increase table-entries limit)" | logger
|
164
|
fi
|
165
|
rm /tmp/bogons
|
166
|
else
|
167
|
echo "Could not download ${v4url} (checksum mismatch)" | logger
|
168
|
checksum_error="true"
|
169
|
fi
|
170
|
|
171
|
if [ "$BOGON_V6_CKSUM" = "$ON_DISK_V6_CKSUM" ]; then
|
172
|
BOGONS_V6_TABLE_COUNT=`pfctl -sTables | grep ^bogonsv6$ | wc -l | awk '{ print $1 }'`
|
173
|
ENTRIES_TOT=`pfctl -vvsTables | awk '/Addresses/ {s+=$2}; END {print s}'`
|
174
|
LINES_V6=`wc -l /tmp/bogonsv6 | awk '{ print $1 }'`
|
175
|
if [ $BOGONS_V6_TABLE_COUNT -gt 0 ]; then
|
176
|
ENTRIES_V6=`pfctl -vvsTables | awk '/-\tbogonsv6$/ {getline; print $2}'`
|
177
|
if [ $ENTRIES_MAX -gt $((2*ENTRIES_TOT-${ENTRIES_V6:-0}+LINES_V6)) ]; then
|
178
|
egrep -iv "^fc00::/7" /tmp/bogonsv6 > /etc/bogonsv6
|
179
|
RESULT=`/sbin/pfctl -t bogonsv6 -T replace -f /etc/bogonsv6 2>&1`
|
180
|
echo "$RESULT" | awk '{ print "Bogons V6 file downloaded: " $0 }' | logger
|
181
|
else
|
182
|
echo "Not saving or updating IPv6 bogons (increase table-entries limit)" | logger
|
183
|
fi
|
184
|
else
|
185
|
if [ $ENTRIES_MAX -gt $((2*ENTRIES_TOT+LINES_V6)) ]; then
|
186
|
egrep -iv "^fc00::/7" /tmp/bogonsv6 > /etc/bogonsv6
|
187
|
echo "Bogons V6 file downloaded but not updating IPv6 bogons table because it is not in use." | logger
|
188
|
else
|
189
|
echo "Not saving IPv6 bogons table (IPv6 Allow is off and table-entries limit is potentially too low)" | logger
|
190
|
fi
|
191
|
fi
|
192
|
rm /tmp/bogonsv6
|
193
|
else
|
194
|
echo "Could not download ${v6url} (checksum mismatch)" | logger
|
195
|
checksum_error="true"
|
196
|
fi
|
197
|
|
198
|
# We mounted RW, so switch back to RO
|
199
|
/etc/rc.conf_mount_ro
|
200
|
fi
|
201
|
|
202
|
if [ "$checksum_error" != "" ]; then
|
203
|
# Relaunch and sleep
|
204
|
sh /etc/rc.update_bogons.sh &
|
205
|
exit
|
206
|
fi
|
207
|
|
208
|
echo "rc.update_bogons.sh is ending the update cycle." | logger
|