]>
Commit | Line | Data |
---|---|---|
2285f9da SS |
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) | |
6897c329 | 23 | TMP_FILE=$(mktemp -p $TMP_PATH) |
2285f9da | 24 | |
663221a2 | 25 | SCRIPT_PATH=/usr/local/bin |
2285f9da SS |
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 | ||
d9f47d9b SS |
35 | eval $(/usr/local/bin/readhash /var/ipfire/proxy/settings) |
36 | ||
2285f9da SS |
37 | function download() { |
38 | echo "Downloading latest GeoIP ruleset..." | |
39 | ||
6897c329 SS |
40 | # Create temporary directory. |
41 | mkdir -pv $TMP_PATH | |
42 | ||
d9f47d9b SS |
43 | # Proxy settings. |
44 | # Check if a proxy should be used. | |
45 | if [[ $UPSTREAM_PROXY ]]; then | |
46 | PROXYSETTINGS="-e http_proxy=http://" | |
47 | ||
48 | # Check if authentication against the proxy is configured. | |
49 | if [[ $UPSTREAM_USER && $UPSTREAM_PASSWORD ]]; then | |
50 | PROXYSETTINGS="$PROXYSETTINGS$UPSTREAM_USER:$UPSTREAM_PASSWORD@" | |
51 | fi | |
52 | ||
53 | # Add proxy server. | |
54 | PROXYSETTINGS="$PROXYSETTINGS$UPSTREAM_PROXY" | |
55 | fi | |
56 | ||
2285f9da | 57 | # Get the latest GeoIP database from server. |
d9f47d9b | 58 | wget $DL_URL/$DL_FILE $PROXYSETTINGS -O $TMP_FILE |
2285f9da SS |
59 | |
60 | # Extract files. | |
6897c329 | 61 | unzip $TMP_FILE -d $TMP_PATH |
2285f9da SS |
62 | |
63 | return 0 | |
64 | } | |
65 | ||
66 | function build() { | |
67 | echo "Convert database..." | |
68 | ||
69 | # Check if the csv file exists. | |
70 | if [ ! -e $TMP_PATH/$CSV_FILE ]; then | |
71 | echo "$TMP_PATH/$CSV_FILE not found. Exiting." | |
72 | return 1 | |
73 | fi | |
74 | ||
75 | # Run script to convert the CSV file into several xtables | |
76 | # compatible binary files. | |
6897c329 | 77 | if ! $SCRIPT_PATH/xt_geoip_build $TMP_PATH/$CSV_FILE -D $TMP_PATH; then |
2285f9da SS |
78 | echo "Could not convert ruleset. Aborting." >&2 |
79 | return 1 | |
80 | fi | |
81 | ||
82 | return 0 | |
83 | } | |
84 | ||
85 | function install() { | |
86 | echo "Install databases..." | |
87 | ||
88 | # Check if our destination exist. | |
89 | if [ ! -e "$DEST_PATH" ]; then | |
90 | mkdir -p $DEST_PATH &>/dev/null | |
91 | fi | |
92 | ||
93 | # Install databases. | |
94 | if ! cp -af $TMP_PATH/$ARCH $DEST_PATH &>/dev/null; then | |
95 | echo "Could not copy files. Aborting." >&2 | |
96 | return 1 | |
97 | fi | |
98 | ||
99 | return 0 | |
100 | } | |
101 | ||
102 | function cleanup() { | |
103 | echo "Cleaning up temporary files..." | |
104 | if ! rm -rf $TMP_PATH &>/dev/null; then | |
105 | echo "Could not remove files. Aborting." >&2 | |
106 | return 1 | |
107 | fi | |
108 | ||
109 | return 0 | |
110 | } | |
111 | ||
112 | function main() { | |
113 | # Download ruleset. | |
114 | download || exit $? | |
115 | ||
116 | # Convert the ruleset. | |
117 | if ! build; then | |
118 | # Do cleanup. | |
119 | cleanup || exit $? | |
120 | exit 1 | |
121 | fi | |
122 | ||
123 | # Install the converted ruleset. | |
124 | if ! install; then | |
125 | # Do cleanup. | |
126 | cleanup || exit $? | |
127 | exit 1 | |
128 | fi | |
129 | ||
130 | # Finaly remove temporary files. | |
131 | cleanup || exit $? | |
132 | ||
133 | return 0 | |
134 | } | |
135 | ||
136 | # Run the main function. | |
137 | main |