1
|
#!/bin/sh
|
2
|
#
|
3
|
# rc.update_bogons.sh
|
4
|
#
|
5
|
# part of pfSense (https://www.pfsense.org)
|
6
|
# Copyright (c) 2004-2013 BSD Perimeter
|
7
|
# Copyright (c) 2013-2016 Electric Sheep Fencing
|
8
|
# Copyright (c) 2014-2021 Rubicon Communications, LLC (Netgate)
|
9
|
# All rights reserved.
|
10
|
#
|
11
|
# Based on src/etc/rc.d/savecore from FreeBSD
|
12
|
#
|
13
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
14
|
# you may not use this file except in compliance with the License.
|
15
|
# You may obtain a copy of the License at
|
16
|
#
|
17
|
# http://www.apache.org/licenses/LICENSE-2.0
|
18
|
#
|
19
|
# Unless required by applicable law or agreed to in writing, software
|
20
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
21
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
22
|
# See the License for the specific language governing permissions and
|
23
|
# limitations under the License.
|
24
|
|
25
|
# Global variables
|
26
|
proc_error=""
|
27
|
|
28
|
do_not_send_uniqueid=$(/usr/local/sbin/read_xml_tag.sh boolean system/do_not_send_uniqueid)
|
29
|
if [ "${do_not_send_uniqueid}" != "true" ]; then
|
30
|
uniqueid=$(/usr/sbin/gnid)
|
31
|
export HTTP_USER_AGENT="${product}/${product_version}:${uniqueid}"
|
32
|
else
|
33
|
export HTTP_USER_AGENT="${product}/${product_version}"
|
34
|
fi
|
35
|
|
36
|
# Download and extract if necessary
|
37
|
process_url() {
|
38
|
local file=$1
|
39
|
local url=$2
|
40
|
local filename=${url##*/}
|
41
|
local ext=${filename#*.}
|
42
|
|
43
|
/usr/bin/fetch -a -w 600 -T 30 -q -o $file "${url}"
|
44
|
|
45
|
if [ ! -f $file ]; then
|
46
|
echo "Could not download ${url}" | logger
|
47
|
proc_error="true"
|
48
|
fi
|
49
|
|
50
|
case "$ext" in
|
51
|
tar)
|
52
|
mv $file $file.tmp
|
53
|
/usr/bin/tar -xf $file.tmp -O > $file 2> /dev/null
|
54
|
;;
|
55
|
tar.gz)
|
56
|
mv $file $file.tmp
|
57
|
/usr/bin/tar -xzf $file.tmp -O > $file 2> /dev/null
|
58
|
;;
|
59
|
tgz)
|
60
|
mv $file $file.tmp
|
61
|
/usr/bin/tar -xzf $file.tmp -O > $file 2> /dev/null
|
62
|
;;
|
63
|
tar.bz2)
|
64
|
mv $file $file.tmp
|
65
|
/usr/bin/tar -xjf $file.tmp -O > $file 2> /dev/null
|
66
|
;;
|
67
|
*)
|
68
|
;;
|
69
|
esac
|
70
|
|
71
|
if [ -f $file.tmp ]; then
|
72
|
rm $file.tmp
|
73
|
fi
|
74
|
|
75
|
if [ ! -f $file ]; then
|
76
|
echo "Could not extract ${filename}" | logger
|
77
|
proc_error="true"
|
78
|
fi
|
79
|
}
|
80
|
|
81
|
echo "rc.update_bogons.sh is starting up." | logger
|
82
|
|
83
|
# Sleep for some time, unless an argument is specified.
|
84
|
if [ "$1" = "" ]; then
|
85
|
# Grab a random value
|
86
|
value=`od -A n -d -N2 /dev/random | awk '{ print $1 }'`
|
87
|
echo "rc.update_bogons.sh is sleeping for $value" | logger
|
88
|
sleep $value
|
89
|
fi
|
90
|
|
91
|
echo "rc.update_bogons.sh is beginning the update cycle." | logger
|
92
|
|
93
|
# Load custom bogon configuration
|
94
|
if [ -f /var/etc/bogon_custom ]; then
|
95
|
. /var/etc/bogon_custom
|
96
|
fi
|
97
|
|
98
|
# Set default values if not overriden
|
99
|
v4url=${v4url:-"https://files.netgate.com/lists/fullbogons-ipv4.txt"}
|
100
|
v6url=${v6url:-"https://files.netgate.com/lists/fullbogons-ipv6.txt"}
|
101
|
v4urlcksum=${v4urlcksum:-"${v4url}.md5"}
|
102
|
v6urlcksum=${v6urlcksum:-"${v6url}.md5"}
|
103
|
|
104
|
process_url /tmp/bogons "${v4url}"
|
105
|
process_url /tmp/bogonsv6 "${v6url}"
|
106
|
|
107
|
if [ "$proc_error" != "" ]; then
|
108
|
# Relaunch and sleep
|
109
|
sh /etc/rc.update_bogons.sh &
|
110
|
exit
|
111
|
fi
|
112
|
|
113
|
BOGON_V4_CKSUM=`/usr/bin/fetch -T 30 -q -o - "${v4urlcksum}" | awk '{ print $4 }'`
|
114
|
ON_DISK_V4_CKSUM=`md5 /tmp/bogons | awk '{ print $4 }'`
|
115
|
BOGON_V6_CKSUM=`/usr/bin/fetch -T 30 -q -o - "${v6urlcksum}" | awk '{ print $4 }'`
|
116
|
ON_DISK_V6_CKSUM=`md5 /tmp/bogonsv6 | awk '{ print $4 }'`
|
117
|
|
118
|
if [ "$BOGON_V4_CKSUM" = "$ON_DISK_V4_CKSUM" ] || [ "$BOGON_V6_CKSUM" = "$ON_DISK_V6_CKSUM" ]; then
|
119
|
ENTRIES_MAX=`pfctl -s memory | awk '/table-entries/ { print $4 }'`
|
120
|
|
121
|
if [ "$BOGON_V4_CKSUM" = "$ON_DISK_V4_CKSUM" ]; then
|
122
|
ENTRIES_TOT=`pfctl -vvsTables | awk '/Addresses/ {s+=$2}; END {print s}'`
|
123
|
ENTRIES_V4=`pfctl -vvsTables | awk '/-\tbogons$/ {getline; print $2}'`
|
124
|
LINES_V4=`wc -l /tmp/bogons | awk '{ print $1 }'`
|
125
|
if [ $ENTRIES_MAX -gt $((2*ENTRIES_TOT-${ENTRIES_V4:-0}+LINES_V4)) ]; then
|
126
|
egrep -v "^192.168.0.0/16|^172.16.0.0/12|^10.0.0.0/8" /tmp/bogons > /etc/bogons
|
127
|
RESULT=`/sbin/pfctl -t bogons -T replace -f /etc/bogons 2>&1`
|
128
|
echo "$RESULT" | awk '{ print "Bogons V4 file downloaded: " $0 }' | logger
|
129
|
else
|
130
|
echo "Not updating IPv4 bogons (increase table-entries limit)" | logger
|
131
|
fi
|
132
|
rm /tmp/bogons
|
133
|
else
|
134
|
echo "Could not download ${v4url} (checksum mismatch)" | logger
|
135
|
checksum_error="true"
|
136
|
fi
|
137
|
|
138
|
if [ "$BOGON_V6_CKSUM" = "$ON_DISK_V6_CKSUM" ]; then
|
139
|
BOGONS_V6_TABLE_COUNT=`pfctl -sTables | grep ^bogonsv6$ | wc -l | awk '{ print $1 }'`
|
140
|
ENTRIES_TOT=`pfctl -vvsTables | awk '/Addresses/ {s+=$2}; END {print s}'`
|
141
|
LINES_V6=`wc -l /tmp/bogonsv6 | awk '{ print $1 }'`
|
142
|
if [ $BOGONS_V6_TABLE_COUNT -gt 0 ]; then
|
143
|
ENTRIES_V6=`pfctl -vvsTables | awk '/-\tbogonsv6$/ {getline; print $2}'`
|
144
|
if [ $ENTRIES_MAX -gt $((2*ENTRIES_TOT-${ENTRIES_V6:-0}+LINES_V6)) ]; then
|
145
|
egrep -iv "^fc00::/7" /tmp/bogonsv6 > /etc/bogonsv6
|
146
|
RESULT=`/sbin/pfctl -t bogonsv6 -T replace -f /etc/bogonsv6 2>&1`
|
147
|
echo "$RESULT" | awk '{ print "Bogons V6 file downloaded: " $0 }' | logger
|
148
|
else
|
149
|
echo "Not saving or updating IPv6 bogons (increase table-entries limit)" | logger
|
150
|
fi
|
151
|
else
|
152
|
if [ $ENTRIES_MAX -gt $((2*ENTRIES_TOT+LINES_V6)) ]; then
|
153
|
egrep -iv "^fc00::/7" /tmp/bogonsv6 > /etc/bogonsv6
|
154
|
echo "Bogons V6 file downloaded but not updating IPv6 bogons table because it is not in use." | logger
|
155
|
else
|
156
|
echo "Not saving IPv6 bogons table (IPv6 Allow is off and table-entries limit is potentially too low)" | logger
|
157
|
fi
|
158
|
fi
|
159
|
rm /tmp/bogonsv6
|
160
|
else
|
161
|
echo "Could not download ${v6url} (checksum mismatch)" | logger
|
162
|
checksum_error="true"
|
163
|
fi
|
164
|
fi
|
165
|
|
166
|
if [ "$checksum_error" != "" ]; then
|
167
|
# Relaunch and sleep
|
168
|
sh /etc/rc.update_bogons.sh &
|
169
|
exit
|
170
|
fi
|
171
|
|
172
|
echo "rc.update_bogons.sh is ending the update cycle." | logger
|