Project

General

Profile

Download (3.32 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2

    
3
# Update bogons file
4
# Part of the pfSense project
5
# www.pfsense.com
6

    
7
# Global variables
8
proc_error=""
9

    
10
# Download and extract if necessary
11
function process_url() {
12
	local file=$1
13
	local url=$2
14
	local filename=${url##*/}
15
	local ext=${filename#*.}
16
	
17
	/usr/bin/fetch -q -o $file "${url}"
18
	
19
	if [ ! -f $file ]; then
20
		echo "Could not download ${url}" | logger
21
		proc_error="true"
22
	fi
23
	
24
	case "$ext" in
25
		tar)
26
			mv $file $file.tmp
27
			/usr/bin/tar -xf $file.tmp -O > $file 2> /dev/null
28
			;;
29
		tar.gz)
30
			;&
31
		tgz)
32
			mv $file $file.tmp
33
			/usr/bin/tar -xzf $file.tmp -O > $file 2> /dev/null
34
			;;
35
		tar.bz2)
36
			mv $file $file.tmp
37
			/usr/bin/tar -xjf $file.tmp -O > $file 2> /dev/null
38
			;;
39
		*)
40
			;;
41
	esac
42
	
43
	if [ -f $file.tmp ]; then
44
		rm $file.tmp
45
	fi
46
	
47
	if [ ! -f $file ]; then
48
		echo "Could not extract ${filename}" | logger
49
		proc_error="true"
50
	fi
51
}
52

    
53
echo "rc.update_bogons.sh is starting up." | logger
54

    
55
# Sleep for some time, unless an argument is specified.
56
if [ "$1" = "" ]; then
57
    # Grab a random value  
58
    value=`od -A n -d -N2 /dev/random | awk '{ print $1 }'`
59
    echo "rc.update_bogons.sh is sleeping for $value" | logger
60
    sleep $value
61
fi    
62

    
63
echo "rc.update_bogons.sh is beginning the update cycle." | logger
64

    
65
# Load custom bogon configuration
66
if [ -f /var/etc/bogon_custom ]; then
67
	. /var/etc/bogon_custom
68
fi
69

    
70
# Set default values if not overriden
71
v4url=${v4url:-"http://files.pfsense.org/lists/fullbogons-ipv4.txt"}
72
v6url=${v6url:-"http://files.pfsense.org/lists/fullbogons-ipv6.txt"}
73
v4urlcksum=${v4urlcksum:-"${v4url}.md5"}
74
v6urlcksum=${v6urlcksum:-"${v6url}.md5"}
75

    
76
process_url /tmp/bogons "${v4url}"
77
process_url /tmp/bogonsv6 "${v6url}"
78

    
79
if [ "$proc_error" != "" ]; then
80
	# Relaunch and sleep
81
	sh /etc/rc.update_bogons.sh &
82
	exit
83
fi
84

    
85
BOGON_V4_CKSUM=`/usr/bin/fetch -q -o - "${v4urlcksum}" | awk '{ print $4 }'`
86
ON_DISK_V4_CKSUM=`md5 /tmp/bogons | awk '{ print $4 }'`
87
BOGON_V6_CKSUM=`/usr/bin/fetch -q -o - "${v6urlcksum}" | awk '{ print $4 }'`
88
ON_DISK_V6_CKSUM=`md5 /tmp/bogonsv6 | awk '{ print $4 }'`
89

    
90
if [ "$BOGON_V4_CKSUM" = "$ON_DISK_V4_CKSUM" ] || [ "$BOGON_V6_CKSUM" = "$ON_DISK_V6_CKSUM" ]; then
91
	# At least one of the downloaded checksums matches, so mount RW
92
	/etc/rc.conf_mount_rw
93
	
94
	MAXENTRIES=`pfctl -s memory | awk '/table-entries/ { print $4 }'`
95
	
96
	if [ "$BOGON_V4_CKSUM" = "$ON_DISK_V4_CKSUM" ]; then
97
		egrep -v "^192.168.0.0/16|^172.16.0.0/12|^10.0.0.0/8" /tmp/bogons > /etc/bogons
98
		RESULT=`/sbin/pfctl -t bogons -T replace -f /etc/bogons 2>&1`
99
		echo "$RESULT" |awk '{ print "Bogons V4 file downloaded: " $0 }' | logger
100
		rm /tmp/bogons
101
	else
102
		echo "Could not download ${v4url} (checksum mismatch)" | logger
103
		checksum_error="true"
104
	fi
105

    
106
	if [ "$BOGON_V6_CKSUM" = "$ON_DISK_V6_CKSUM" ]; then
107
		LINES=`wc -l /tmp/bogonsv6 | awk '{ print $1 }'`
108
		if [ $MAXENTRIES -gt $((2*LINES)) ]; then
109
			egrep -v "^fc00::/7" /tmp/bogonsv6 > /etc/bogonsv6
110
			RESULT=`/sbin/pfctl -t bogonsv6 -T replace -f /etc/bogonsv6 2>&1`
111
			echo "$RESULT" |awk '{ print "Bogons V6 file downloaded: " $0 }' | logger
112
		fi
113
		rm /tmp/bogonsv6
114
	else
115
		echo "Could not download ${v6url} (checksum mismatch)" | logger
116
		checksum_error="true"
117
	fi
118
	
119
	# We mounted RW, so switch back to RO
120
	/etc/rc.conf_mount_ro
121
fi
122

    
123
if [ "$checksum_error" != "" ]; then
124
	# Relaunch and sleep
125
	sh /etc/rc.update_bogons.sh & 
126
	exit
127
fi
128

    
129
echo "rc.update_bogons.sh is ending the update cycle." | logger
(98-98/108)