]> git.ipfire.org Git - ipfire-2.x.git/blob - src/scripts/xt_geoip_update
Merge branch 'master' of ssh://git.ipfire.org/pub/git/ipfire-2.x into seventeen-geoip
[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)
24
25 SCRIPT_PATH=/usr/libexec/xtables-addons
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 # Get the latest GeoIP database from server.
39 wget $DL_URL/$DL_FILE -O $TMP_PATH/$TMP_FILE
40
41 # Extract files.
42 unzip $TMP_PATH/$TMP_FILE -d $TMP_PATH
43
44 return 0
45 }
46
47 function build() {
48 echo "Convert database..."
49
50 # Check if the csv file exists.
51 if [ ! -e $TMP_PATH/$CSV_FILE ]; then
52 echo "$TMP_PATH/$CSV_FILE not found. Exiting."
53 return 1
54 fi
55
56 # Run script to convert the CSV file into several xtables
57 # compatible binary files.
58 if ! $SCRIPT_PATH/xt_geoip_build $TMP_DIR/$CSV_FILE -D $TMP_DIR; then
59 echo "Could not convert ruleset. Aborting." >&2
60 return 1
61 fi
62
63 return 0
64 }
65
66 function install() {
67 echo "Install databases..."
68
69 # Check if our destination exist.
70 if [ ! -e "$DEST_PATH" ]; then
71 mkdir -p $DEST_PATH &>/dev/null
72 fi
73
74 # Install databases.
75 if ! cp -af $TMP_PATH/$ARCH $DEST_PATH &>/dev/null; then
76 echo "Could not copy files. Aborting." >&2
77 return 1
78 fi
79
80 return 0
81 }
82
83 function cleanup() {
84 echo "Cleaning up temporary files..."
85 if ! rm -rf $TMP_PATH &>/dev/null; then
86 echo "Could not remove files. Aborting." >&2
87 return 1
88 fi
89
90 return 0
91 }
92
93 function main() {
94 # Download ruleset.
95 download || exit $?
96
97 # Convert the ruleset.
98 if ! build; then
99 # Do cleanup.
100 cleanup || exit $?
101 exit 1
102 fi
103
104 # Install the converted ruleset.
105 if ! install; then
106 # Do cleanup.
107 cleanup || exit $?
108 exit 1
109 fi
110
111 # Finaly remove temporary files.
112 cleanup || exit $?
113
114 return 0
115 }
116
117 # Run the main function.
118 main