]> git.ipfire.org Git - ipfire-2.x.git/blob - src/scripts/xt_geoip_update
xt_geoip_update: Fix script path.
[ipfire-2.x.git] / src / scripts / xt_geoip_update
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2014 IPFire Development Team <info@ipfire.org> #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 TMP_PATH=$(mktemp -d)
23 TMP_FILE=$(mktemp -p $TMP_PATH)
24
25 SCRIPT_PATH=/usr/local/bin
26 DEST_PATH=/usr/share/xt_geoip
27
28 DL_URL=http://geolite.maxmind.com/download/geoip/database
29 DL_FILE=GeoIPCountryCSV.zip
30
31 CSV_FILE=GeoIPCountryWhois.csv
32
33 ARCH=LE
34
35 function download() {
36 echo "Downloading latest GeoIP ruleset..."
37
38 # Create temporary directory.
39 mkdir -pv $TMP_PATH
40
41 # Get the latest GeoIP database from server.
42 wget $DL_URL/$DL_FILE -O $TMP_FILE
43
44 # Extract files.
45 unzip $TMP_FILE -d $TMP_PATH
46
47 return 0
48 }
49
50 function build() {
51 echo "Convert database..."
52
53 # Check if the csv file exists.
54 if [ ! -e $TMP_PATH/$CSV_FILE ]; then
55 echo "$TMP_PATH/$CSV_FILE not found. Exiting."
56 return 1
57 fi
58
59 # Run script to convert the CSV file into several xtables
60 # compatible binary files.
61 if ! $SCRIPT_PATH/xt_geoip_build $TMP_PATH/$CSV_FILE -D $TMP_PATH; then
62 echo "Could not convert ruleset. Aborting." >&2
63 return 1
64 fi
65
66 return 0
67 }
68
69 function install() {
70 echo "Install databases..."
71
72 # Check if our destination exist.
73 if [ ! -e "$DEST_PATH" ]; then
74 mkdir -p $DEST_PATH &>/dev/null
75 fi
76
77 # Install databases.
78 if ! cp -af $TMP_PATH/$ARCH $DEST_PATH &>/dev/null; then
79 echo "Could not copy files. Aborting." >&2
80 return 1
81 fi
82
83 return 0
84 }
85
86 function cleanup() {
87 echo "Cleaning up temporary files..."
88 if ! rm -rf $TMP_PATH &>/dev/null; then
89 echo "Could not remove files. Aborting." >&2
90 return 1
91 fi
92
93 return 0
94 }
95
96 function main() {
97 # Download ruleset.
98 download || exit $?
99
100 # Convert the ruleset.
101 if ! build; then
102 # Do cleanup.
103 cleanup || exit $?
104 exit 1
105 fi
106
107 # Install the converted ruleset.
108 if ! install; then
109 # Do cleanup.
110 cleanup || exit $?
111 exit 1
112 fi
113
114 # Finaly remove temporary files.
115 cleanup || exit $?
116
117 return 0
118 }
119
120 # Run the main function.
121 main