#!/bin/bash #---------------------------------------------------------------------------- # /usr/bin/xtables-geoipupdate # # Creation : 2021-04-08 hb # Last update: $Id: xtables-geoipupdate-legacy 84318 2021-04-08 15:15:47Z hbfl $ # # Copyright (c) 2021-@@YEAR@@ Holger Bruenjes, holgerbruenjes(at)gmx(dot)net # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------- geoip_conf='/etc/GeoIP/GeoIP.conf' # --------------------------------------------------------------------------- # conf value # --------------------------------------------------------------------------- conf_value() { key=${1} value=$(/usr/bin/grep "^${key}" ${geoip_conf} | /usr/bin/sed -e 's:#.*::' -e "s:${key}::") if [ -z "${value}" ] then myecho ${key} not configured in ${geoip_conf} exit 1 fi echo ${value} } # --------------------------------------------------------------------------- # download geodata csv # --------------------------------------------------------------------------- download_geodata_csv() { csv_product=${1} myecho ">>> Downloading ${csv_product}.zip" /usr/bin/curl ${silent} -J -L -u ${account_id}:${license_key} "https://download.maxmind.com/geoip/databases/${csv_product}/download?suffix=zip" \ -o ${database_directory}/${csv_product}.zip } # --------------------------------------------------------------------------- # geolite to legacy # --------------------------------------------------------------------------- geolite_to_db() { csv_product=${1} myecho ">>> Converting ${csv_product}.zip to xt_geoip format" pushd ${database_directory} if [ ! -f ${csv_product}.zip ] then myecho "${database_directory}/${csv_product}.zip not found" return fi if ! /usr/bin/file -b --mime-type ${csv_product}.zip | /usr/bin/grep -q 'application/zip' then message=$(cat ${csv_product}.zip) myecho "Please check your LicenseKey = '${license_key}'" myecho -e "${message}" return fi /usr/bin/unzip -o -j ${quiet} ${csv_product}.zip /usr/bin/rm -f ${csv_product}.zip /usr/libexec/xtables-addons/xt_geoip_build_maxmind ${quiet} -D "/usr/share/xt_geoip/" -S ${database_directory} /usr/bin/rm -f ${database_directory}/* popd >/dev/null } # --------------------------------------------------------------------------- # myecho # --------------------------------------------------------------------------- myecho() { if ! ${quietflag:-false} then echo ${@} fi } # --------------------------------------------------------------------------- # main # --------------------------------------------------------------------------- main() { while [ ${#} -ne 0 ] do case "${1}" in --quiet) quietflag=true silent='-s' quiet='-q' shift ;; esac done license_key=$(conf_value 'LicenseKey') database_directory='/var/lib/xtables-geoip' account_id=$(conf_value 'AccountID') download_geodata_csv GeoLite2-Country-CSV geolite_to_db GeoLite2-Country-CSV } # ---------------------------------------------------------------------------- # call function main # ---------------------------------------------------------------------------- main "${@}" # --------------------------------------------------------------------------- # end #----------------------------------------------------------------------------