501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2013, 2014, 2016 Internet Systems Consortium, Inc. ("ISC")
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt */
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt/*! \file */
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#include <config.h>
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
8a896bc645a6236e9a47e33fb57510a451009a90Mark Andrews#include <isc/util.h>
8a896bc645a6236e9a47e33fb57510a451009a90Mark Andrews
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#include <named/log.h>
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#include <named/geoip.h>
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#include <dns/geoip.h>
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews#ifdef HAVE_GEOIP
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Huntstatic dns_geoip_databases_t geoip_table = {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt};
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Huntstatic void
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Huntinit_geoip_db(GeoIP **dbp, GeoIPDBTypes edition, GeoIPDBTypes fallback,
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt GeoIPOptions method, const char *name)
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt{
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt char *info;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt GeoIP *db;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt REQUIRE(dbp != NULL);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt db = *dbp;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt if (db != NULL) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt GeoIP_delete(db);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt db = *dbp = NULL;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt }
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt if (! GeoIP_db_avail(edition)) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt NS_LOGMODULE_SERVER, ISC_LOG_INFO,
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt "GeoIP %s (type %d) DB not available", name, edition);
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt goto fail;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt }
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt NS_LOGMODULE_SERVER, ISC_LOG_INFO,
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt "initializing GeoIP %s (type %d) DB", name, edition);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt db = GeoIP_open_type(edition, method);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt if (db == NULL) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt "failed to initialize GeoIP %s (type %d) DB%s",
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt name, edition, fallback == 0
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt ? "geoip matches using this database will fail" : "");
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt goto fail;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt }
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt info = GeoIP_database_info(db);
af326c2e3f90d86a8966a1298d7aa157667f97cdEvan Hunt if (info != NULL) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt NS_LOGMODULE_SERVER, ISC_LOG_INFO,
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt "%s", info);
af326c2e3f90d86a8966a1298d7aa157667f97cdEvan Hunt free(info);
af326c2e3f90d86a8966a1298d7aa157667f97cdEvan Hunt }
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt *dbp = db;
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt return;
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt fail:
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt if (fallback != 0)
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt init_geoip_db(dbp, fallback, 0, method, name);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt}
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#endif /* HAVE_GEOIP */
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Huntvoid
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Huntns_geoip_init(void) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#ifndef HAVE_GEOIP
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt return;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#else
a0b4f6d952cc6adde281948cf995868b44f366e8Evan Hunt GeoIP_cleanup();
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt if (ns_g_geoip == NULL)
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt ns_g_geoip = &geoip_table;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#endif
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt}
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Huntvoid
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Huntns_geoip_load(char *dir) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#ifndef HAVE_GEOIP
8a896bc645a6236e9a47e33fb57510a451009a90Mark Andrews
8a896bc645a6236e9a47e33fb57510a451009a90Mark Andrews UNUSED(dir);
8a896bc645a6236e9a47e33fb57510a451009a90Mark Andrews
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt return;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#else
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt GeoIPOptions method;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#ifdef _WIN32
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt method = GEOIP_STANDARD;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#else
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt method = GEOIP_MMAP_CACHE;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#endif
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt ns_geoip_init();
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt if (dir != NULL) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt NS_LOGMODULE_SERVER, ISC_LOG_INFO,
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt "using \"%s\" as GeoIP directory", dir);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt GeoIP_setup_custom_directory(dir);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt }
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt init_geoip_db(&ns_g_geoip->country_v4, GEOIP_COUNTRY_EDITION, 0,
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt method, "Country (IPv4)");
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#ifdef HAVE_GEOIP_V6
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt init_geoip_db(&ns_g_geoip->country_v6, GEOIP_COUNTRY_EDITION_V6, 0,
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt method, "Country (IPv6)");
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#endif
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt init_geoip_db(&ns_g_geoip->city_v4, GEOIP_CITY_EDITION_REV1,
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt GEOIP_CITY_EDITION_REV0, method, "City (IPv4)");
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#if defined(HAVE_GEOIP_V6) && defined(HAVE_GEOIP_CITY_V6)
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt init_geoip_db(&ns_g_geoip->city_v6, GEOIP_CITY_EDITION_REV1_V6,
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt GEOIP_CITY_EDITION_REV0_V6, method, "City (IPv6)");
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#endif
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt init_geoip_db(&ns_g_geoip->region, GEOIP_REGION_EDITION_REV1,
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt GEOIP_REGION_EDITION_REV0, method, "Region");
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt init_geoip_db(&ns_g_geoip->isp, GEOIP_ISP_EDITION, 0,
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt method, "ISP");
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt init_geoip_db(&ns_g_geoip->org, GEOIP_ORG_EDITION, 0,
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt method, "Org");
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt init_geoip_db(&ns_g_geoip->as, GEOIP_ASNUM_EDITION, 0,
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt method, "AS");
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt init_geoip_db(&ns_g_geoip->domain, GEOIP_DOMAIN_EDITION, 0,
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt method, "Domain");
83f69fcd6ef72c9e2ebcb025b66a2ee74176becdEvan Hunt init_geoip_db(&ns_g_geoip->netspeed, GEOIP_NETSPEED_EDITION, 0,
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt method, "NetSpeed");
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#endif /* HAVE_GEOIP */
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt}