#!/bin/bash #---------------------------------------------------------------------------- # /usr/bin/geoipupdate-legacy # # Creation : 2020-05-13 pgajdos@suse.com # Last update: $Id$ # # Copyright (c) 2020 pgajdos@suse.com # Copyright (c) 2020-@@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' # --------------------------------------------------------------------------- # check if clamav runnnig # --------------------------------------------------------------------------- check_clamav() { . /etc/config.d/geoipupdate-legacy if [ "${GEOIPUPDATE_LEGACY_STOP_CLAMAV:-no}" = "yes" ] then if /usr/sbin/service is-enabled clamav.socket then clamav_running=true /usr/sbin/service --quiet stop clamav.socket fi fi } # --------------------------------------------------------------------------- # 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_legacy() { csv_product=${1} myecho ">>> Converting ${csv_product}.zip to legacy format" if ! which geolite2legacy >/dev/null 2>&1 then echo 'geolite2legacy program required' return fi 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 geolite2legacy -i ${csv_product}.zip geolite2legacy -i ${csv_product}.zip -6 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' shift ;; esac done check_clamav license_key=$(conf_value 'LicenseKey') database_directory=$(conf_value 'DatabaseDirectory') account_id=$(conf_value 'AccountID') download_geodata_csv GeoLite2-ASN-CSV geolite_to_legacy GeoLite2-ASN-CSV download_geodata_csv GeoLite2-City-CSV geolite_to_legacy GeoLite2-City-CSV download_geodata_csv GeoLite2-Country-CSV geolite_to_legacy GeoLite2-Country-CSV if ${clamav_running:-false} then /usr/sbin/service --quiet start clamav.socket fi } # ---------------------------------------------------------------------------- # call function main # ---------------------------------------------------------------------------- main "${@}" # --------------------------------------------------------------------------- # end #----------------------------------------------------------------------------