]>
Commit | Line | Data |
---|---|---|
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 | ||
3cd8d550 | 22 | TMP_PATH=$(mktemp -dp /var/tmp) |
6897c329 | 23 | TMP_FILE=$(mktemp -p $TMP_PATH) |
2285f9da | 24 | |
663221a2 | 25 | SCRIPT_PATH=/usr/local/bin |
2285f9da | 26 | DEST_PATH=/usr/share/xt_geoip |
b76a8a00 | 27 | DB_PATH=/var/lib/GeoIP |
392994dc | 28 | DB1_PATH=/usr/share/GeoIP |
2285f9da | 29 | |
d38e7e25 | 30 | DL_URL=https://geolite.maxmind.com/download/geoip/database |
b76a8a00 | 31 | DL_FILE=GeoLite2-Country-CSV.zip |
2285f9da | 32 | |
d9f47d9b SS |
33 | eval $(/usr/local/bin/readhash /var/ipfire/proxy/settings) |
34 | ||
2285f9da SS |
35 | function download() { |
36 | echo "Downloading latest GeoIP ruleset..." | |
37 | ||
d9f47d9b SS |
38 | # Proxy settings. |
39 | # Check if a proxy should be used. | |
40 | if [[ $UPSTREAM_PROXY ]]; then | |
d38e7e25 | 41 | PROXYSETTINGS="-e https_proxy=http://" |
d9f47d9b SS |
42 | |
43 | # Check if authentication against the proxy is configured. | |
44 | if [[ $UPSTREAM_USER && $UPSTREAM_PASSWORD ]]; then | |
45 | PROXYSETTINGS="$PROXYSETTINGS$UPSTREAM_USER:$UPSTREAM_PASSWORD@" | |
46 | fi | |
47 | ||
48 | # Add proxy server. | |
49 | PROXYSETTINGS="$PROXYSETTINGS$UPSTREAM_PROXY" | |
50 | fi | |
51 | ||
2285f9da | 52 | # Get the latest GeoIP database from server. |
d9f47d9b | 53 | wget $DL_URL/$DL_FILE $PROXYSETTINGS -O $TMP_FILE |
2285f9da | 54 | |
b76a8a00 | 55 | # Extract files to database path. |
6897c329 | 56 | unzip $TMP_FILE -d $TMP_PATH |
2285f9da SS |
57 | |
58 | return 0 | |
59 | } | |
60 | ||
b76a8a00 SS |
61 | function install() { |
62 | echo "Install CSV database..." | |
2285f9da | 63 | |
b76a8a00 SS |
64 | # Check if the database dir exists. |
65 | if [ ! -e "$DB_PATH" ]; then | |
66 | mkdir -p $DB_PATH &>/dev/null | |
2285f9da SS |
67 | fi |
68 | ||
b76a8a00 SS |
69 | # Check if the directory for binary databases exists. |
70 | if [ ! -e "$DEST_PATH" ]; then | |
71 | mkdir -p $DEST_PATH &>/dev/null | |
72 | fi | |
73 | ||
74 | # Install CSV databases. | |
75 | if ! cp -af $TMP_PATH/*/* $DB_PATH &>/dev/null; then | |
76 | echo "Could not copy files. Aborting." >&2 | |
2285f9da SS |
77 | return 1 |
78 | fi | |
79 | ||
80 | return 0 | |
81 | } | |
82 | ||
392994dc | 83 | function build_legacy() { |
392994dc AF |
84 | echo "Convert database to legacy GeoIP.dat ..." |
85 | cat $DB_PATH/GeoLite2-Country-Blocks-IPv4.csv | \ | |
86 | $DB1_PATH/bin/geolite2-to-legacy-csv.sh $DB1_PATH/bin/countryInfo.txt > \ | |
87 | $TMP_FILE | |
9e20c024 AF |
88 | $DB1_PATH/bin/geoip-generator -v -4 --info="$(date -u +'GEO-106FREE %Y%m%d Build -IPFire-' \ |
89 | -r $DB_PATH/GeoLite2-Country-Blocks-IPv4.csv) $(<$DB_PATH/COPYRIGHT.txt)" -o \ | |
392994dc AF |
90 | $DB1_PATH/GeoIP.dat $TMP_FILE |
91 | ||
92 | return 0 | |
93 | } | |
94 | ||
95 | ||
b76a8a00 SS |
96 | function build() { |
97 | echo "Convert database..." | |
2285f9da | 98 | |
b76a8a00 SS |
99 | # Run script to convert the CSV file into several xtables |
100 | # compatible binary files. | |
101 | if ! $SCRIPT_PATH/xt_geoip_build -S $DB_PATH -D $DEST_PATH; then | |
102 | echo "Could not convert ruleset. Aborting." >&2 | |
2285f9da SS |
103 | return 1 |
104 | fi | |
105 | ||
106 | return 0 | |
107 | } | |
108 | ||
109 | function cleanup() { | |
110 | echo "Cleaning up temporary files..." | |
111 | if ! rm -rf $TMP_PATH &>/dev/null; then | |
112 | echo "Could not remove files. Aborting." >&2 | |
113 | return 1 | |
114 | fi | |
115 | ||
116 | return 0 | |
117 | } | |
118 | ||
119 | function main() { | |
a18addb9 DW |
120 | local func |
121 | for func in download install build build_legacy; do | |
122 | if ! ${func}; then | |
123 | # Cleanup any temporary data | |
124 | cleanup | |
2285f9da | 125 | |
a18addb9 DW |
126 | return 1 |
127 | fi | |
128 | done | |
b76a8a00 | 129 | |
a18addb9 DW |
130 | # Cleanup |
131 | cleanup || return $? | |
392994dc | 132 | |
a18addb9 DW |
133 | # All done |
134 | return 0 | |
2285f9da SS |
135 | } |
136 | ||
137 | # Run the main function. | |
a18addb9 | 138 | main || exit $? |