]> git.ipfire.org Git - ipfire-2.x.git/blame - src/scripts/xt_geoip_update
xtables-addons: Fix generating GeoIP database
[ipfire-2.x.git] / src / scripts / xt_geoip_update
CommitLineData
2285f9da
SS
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
271bac39 5# Copyright (C) 2019 IPFire Development Team <info@ipfire.org> #
2285f9da
SS
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
22TMP_PATH=$(mktemp -d)
6897c329 23TMP_FILE=$(mktemp -p $TMP_PATH)
2285f9da 24
663221a2 25SCRIPT_PATH=/usr/local/bin
2285f9da 26DEST_PATH=/usr/share/xt_geoip
b76a8a00 27DB_PATH=/var/lib/GeoIP
2285f9da 28
271bac39 29DL_URL=http://geolite.maxmind.com/download/geoip/database
b76a8a00 30DL_FILE=GeoLite2-Country-CSV.zip
2285f9da 31
d9f47d9b
SS
32eval $(/usr/local/bin/readhash /var/ipfire/proxy/settings)
33
2285f9da
SS
34function download() {
35 echo "Downloading latest GeoIP ruleset..."
36
6897c329
SS
37 # Create temporary directory.
38 mkdir -pv $TMP_PATH
39
d9f47d9b
SS
40 # Proxy settings.
41 # Check if a proxy should be used.
42 if [[ $UPSTREAM_PROXY ]]; then
43 PROXYSETTINGS="-e http_proxy=http://"
44
45 # Check if authentication against the proxy is configured.
46 if [[ $UPSTREAM_USER && $UPSTREAM_PASSWORD ]]; then
47 PROXYSETTINGS="$PROXYSETTINGS$UPSTREAM_USER:$UPSTREAM_PASSWORD@"
48 fi
49
50 # Add proxy server.
51 PROXYSETTINGS="$PROXYSETTINGS$UPSTREAM_PROXY"
52 fi
53
2285f9da 54 # Get the latest GeoIP database from server.
d9f47d9b 55 wget $DL_URL/$DL_FILE $PROXYSETTINGS -O $TMP_FILE
2285f9da 56
b76a8a00 57 # Extract files to database path.
6897c329 58 unzip $TMP_FILE -d $TMP_PATH
2285f9da
SS
59
60 return 0
61}
62
b76a8a00
SS
63function install() {
64 echo "Install CSV database..."
2285f9da 65
b76a8a00
SS
66 # Check if the database dir exists.
67 if [ ! -e "$DB_PATH" ]; then
68 mkdir -p $DB_PATH &>/dev/null
2285f9da
SS
69 fi
70
b76a8a00
SS
71 # Check if the directory for binary databases exists.
72 if [ ! -e "$DEST_PATH" ]; then
73 mkdir -p $DEST_PATH &>/dev/null
74 fi
75
76 # Install CSV databases.
77 if ! cp -af $TMP_PATH/*/* $DB_PATH &>/dev/null; then
78 echo "Could not copy files. Aborting." >&2
2285f9da
SS
79 return 1
80 fi
81
82 return 0
83}
84
b76a8a00
SS
85function build() {
86 echo "Convert database..."
2285f9da 87
b76a8a00
SS
88 # Run script to convert the CSV file into several xtables
89 # compatible binary files.
90 if ! $SCRIPT_PATH/xt_geoip_build -S $DB_PATH -D $DEST_PATH; then
91 echo "Could not convert ruleset. Aborting." >&2
2285f9da
SS
92 return 1
93 fi
94
95 return 0
96}
97
98function cleanup() {
99 echo "Cleaning up temporary files..."
100 if ! rm -rf $TMP_PATH &>/dev/null; then
101 echo "Could not remove files. Aborting." >&2
102 return 1
103 fi
104
105 return 0
106}
107
108function main() {
109 # Download ruleset.
110 download || exit $?
111
2285f9da
SS
112 if ! install; then
113 # Do cleanup.
114 cleanup || exit $?
115 exit 1
116 fi
117
b76a8a00 118 # Remove temporary files.
2285f9da
SS
119 cleanup || exit $?
120
b76a8a00
SS
121 # Convert the ruleset.
122 build || exit $?
123
2285f9da
SS
124 return 0
125}
126
127# Run the main function.
128main