]>
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 | ||
22 | TMP_PATH=$(mktemp -d) | |
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 | ||
6897c329 SS |
38 | # Create temporary directory. |
39 | mkdir -pv $TMP_PATH | |
40 | ||
d9f47d9b SS |
41 | # Proxy settings. |
42 | # Check if a proxy should be used. | |
43 | if [[ $UPSTREAM_PROXY ]]; then | |
d38e7e25 | 44 | PROXYSETTINGS="-e https_proxy=http://" |
d9f47d9b SS |
45 | |
46 | # Check if authentication against the proxy is configured. | |
47 | if [[ $UPSTREAM_USER && $UPSTREAM_PASSWORD ]]; then | |
48 | PROXYSETTINGS="$PROXYSETTINGS$UPSTREAM_USER:$UPSTREAM_PASSWORD@" | |
49 | fi | |
50 | ||
51 | # Add proxy server. | |
52 | PROXYSETTINGS="$PROXYSETTINGS$UPSTREAM_PROXY" | |
53 | fi | |
54 | ||
2285f9da | 55 | # Get the latest GeoIP database from server. |
d9f47d9b | 56 | wget $DL_URL/$DL_FILE $PROXYSETTINGS -O $TMP_FILE |
2285f9da | 57 | |
b76a8a00 | 58 | # Extract files to database path. |
6897c329 | 59 | unzip $TMP_FILE -d $TMP_PATH |
2285f9da SS |
60 | |
61 | return 0 | |
62 | } | |
63 | ||
b76a8a00 SS |
64 | function install() { |
65 | echo "Install CSV database..." | |
2285f9da | 66 | |
b76a8a00 SS |
67 | # Check if the database dir exists. |
68 | if [ ! -e "$DB_PATH" ]; then | |
69 | mkdir -p $DB_PATH &>/dev/null | |
2285f9da SS |
70 | fi |
71 | ||
b76a8a00 SS |
72 | # Check if the directory for binary databases exists. |
73 | if [ ! -e "$DEST_PATH" ]; then | |
74 | mkdir -p $DEST_PATH &>/dev/null | |
75 | fi | |
76 | ||
77 | # Install CSV databases. | |
78 | if ! cp -af $TMP_PATH/*/* $DB_PATH &>/dev/null; then | |
79 | echo "Could not copy files. Aborting." >&2 | |
2285f9da SS |
80 | return 1 |
81 | fi | |
82 | ||
83 | return 0 | |
84 | } | |
85 | ||
392994dc AF |
86 | function build_legacy() { |
87 | # Create temporary directory. | |
88 | mkdir -pv $TMP_PATH | |
89 | ||
90 | echo "Convert database to legacy GeoIP.dat ..." | |
91 | cat $DB_PATH/GeoLite2-Country-Blocks-IPv4.csv | \ | |
92 | $DB1_PATH/bin/geolite2-to-legacy-csv.sh $DB1_PATH/bin/countryInfo.txt > \ | |
93 | $TMP_FILE | |
94 | $DB1_PATH/bin/geoip-generator -v -4 --info="$(date -u +'GEO-106FREE %Y%m%d Build')" -o \ | |
95 | $DB1_PATH/GeoIP.dat $TMP_FILE | |
96 | ||
97 | return 0 | |
98 | } | |
99 | ||
100 | ||
b76a8a00 SS |
101 | function build() { |
102 | echo "Convert database..." | |
2285f9da | 103 | |
b76a8a00 SS |
104 | # Run script to convert the CSV file into several xtables |
105 | # compatible binary files. | |
106 | if ! $SCRIPT_PATH/xt_geoip_build -S $DB_PATH -D $DEST_PATH; then | |
107 | echo "Could not convert ruleset. Aborting." >&2 | |
2285f9da SS |
108 | return 1 |
109 | fi | |
110 | ||
111 | return 0 | |
112 | } | |
113 | ||
114 | function cleanup() { | |
115 | echo "Cleaning up temporary files..." | |
116 | if ! rm -rf $TMP_PATH &>/dev/null; then | |
117 | echo "Could not remove files. Aborting." >&2 | |
118 | return 1 | |
119 | fi | |
120 | ||
121 | return 0 | |
122 | } | |
123 | ||
124 | function main() { | |
125 | # Download ruleset. | |
126 | download || exit $? | |
127 | ||
2285f9da SS |
128 | if ! install; then |
129 | # Do cleanup. | |
130 | cleanup || exit $? | |
131 | exit 1 | |
132 | fi | |
133 | ||
b76a8a00 | 134 | # Remove temporary files. |
2285f9da SS |
135 | cleanup || exit $? |
136 | ||
b76a8a00 SS |
137 | # Convert the ruleset. |
138 | build || exit $? | |
139 | ||
392994dc AF |
140 | # Convert GeoIP2 to lagacy. |
141 | build_legacy || exit $? | |
2285f9da | 142 | return 0 |
392994dc AF |
143 | |
144 | # Remove temporary files. | |
145 | cleanup || exit $? | |
2285f9da SS |
146 | } |
147 | ||
148 | # Run the main function. | |
149 | main |