2013-02-27 17:19:39 -08:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2013-02-27 17:19:39 -08:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2013-02-27 17:19:39 -08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file */
|
|
|
|
|
2019-06-11 20:32:21 -07:00
|
|
|
#if defined(HAVE_GEOIP2)
|
|
|
|
#include <maxminddb.h>
|
|
|
|
#elif defined(HAVE_GEOIP)
|
|
|
|
#include <GeoIP.h>
|
|
|
|
#include <GeoIPCity.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <isc/print.h>
|
|
|
|
#include <isc/string.h>
|
2013-03-01 09:58:32 +11:00
|
|
|
#include <isc/util.h>
|
|
|
|
|
2019-06-11 20:32:21 -07:00
|
|
|
#include <dns/geoip.h>
|
|
|
|
|
2013-02-27 17:19:39 -08:00
|
|
|
#include <named/log.h>
|
|
|
|
#include <named/geoip.h>
|
|
|
|
|
2019-06-11 20:32:21 -07:00
|
|
|
static dns_geoip_databases_t geoip_table = DNS_GEOIP_DATABASE_INIT;
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2019-06-11 20:32:21 -07:00
|
|
|
#if defined(HAVE_GEOIP)
|
2013-02-27 17:19:39 -08:00
|
|
|
static void
|
2019-06-11 18:36:52 -07:00
|
|
|
init_geoip_db(void **dbp, GeoIPDBTypes edition, GeoIPDBTypes fallback,
|
2013-02-27 17:19:39 -08:00
|
|
|
GeoIPOptions method, const char *name)
|
|
|
|
{
|
|
|
|
char *info;
|
|
|
|
GeoIP *db;
|
|
|
|
|
|
|
|
REQUIRE(dbp != NULL);
|
|
|
|
|
2019-06-11 18:36:52 -07:00
|
|
|
db = (GeoIP *)*dbp;
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
if (db != NULL) {
|
|
|
|
GeoIP_delete(db);
|
|
|
|
db = *dbp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! GeoIP_db_avail(edition)) {
|
2017-09-08 13:39:09 -07:00
|
|
|
isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
|
|
|
|
NAMED_LOGMODULE_SERVER, ISC_LOG_INFO,
|
2014-01-23 12:46:02 -08:00
|
|
|
"GeoIP %s (type %d) DB not available", name, edition);
|
|
|
|
goto fail;
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
2017-09-08 13:39:09 -07:00
|
|
|
isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
|
|
|
|
NAMED_LOGMODULE_SERVER, ISC_LOG_INFO,
|
2014-01-23 12:46:02 -08:00
|
|
|
"initializing GeoIP %s (type %d) DB", name, edition);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
db = GeoIP_open_type(edition, method);
|
|
|
|
if (db == NULL) {
|
2017-09-08 13:39:09 -07:00
|
|
|
isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
|
|
|
|
NAMED_LOGMODULE_SERVER, ISC_LOG_ERROR,
|
2014-01-23 12:46:02 -08:00
|
|
|
"failed to initialize GeoIP %s (type %d) DB%s",
|
|
|
|
name, edition, fallback == 0
|
|
|
|
? "geoip matches using this database will fail" : "");
|
|
|
|
goto fail;
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
info = GeoIP_database_info(db);
|
2016-08-29 11:56:36 -07:00
|
|
|
if (info != NULL) {
|
2017-09-08 13:39:09 -07:00
|
|
|
isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
|
|
|
|
NAMED_LOGMODULE_SERVER, ISC_LOG_INFO,
|
2013-02-27 17:19:39 -08:00
|
|
|
"%s", info);
|
2016-08-29 11:56:36 -07:00
|
|
|
free(info);
|
|
|
|
}
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
*dbp = db;
|
2014-01-23 12:46:02 -08:00
|
|
|
return;
|
2019-06-11 20:32:21 -07:00
|
|
|
|
2014-01-23 12:46:02 -08:00
|
|
|
fail:
|
2019-06-11 20:32:21 -07:00
|
|
|
if (fallback != 0) {
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(dbp, fallback, 0, method, name);
|
2019-06-11 20:32:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#elif defined(HAVE_GEOIP2)
|
|
|
|
static MMDB_s geoip_country, geoip_city, geoip_as, geoip_isp, geoip_domain;
|
|
|
|
|
|
|
|
static MMDB_s *
|
|
|
|
open_geoip2(const char *dir, const char *dbfile, MMDB_s *mmdb) {
|
|
|
|
char pathbuf[PATH_MAX];
|
|
|
|
unsigned int n;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
n = snprintf(pathbuf, sizeof(pathbuf), "%s/%s", dir, dbfile);
|
|
|
|
if (n >= sizeof(pathbuf)) {
|
|
|
|
isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
|
|
|
|
NAMED_LOGMODULE_SERVER, ISC_LOG_ERROR,
|
|
|
|
"GeoIP2 database '%s/%s': path too long",
|
|
|
|
(dir != NULL) ? dir : ".", dbfile);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = MMDB_open(pathbuf, MMDB_MODE_MMAP, mmdb);
|
|
|
|
if (ret == MMDB_SUCCESS) {
|
|
|
|
isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
|
|
|
|
NAMED_LOGMODULE_SERVER, ISC_LOG_INFO,
|
|
|
|
"opened GeoIP2 database '%s'", pathbuf);
|
|
|
|
return (mmdb);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
|
|
|
|
NAMED_LOGMODULE_SERVER, ISC_LOG_DEBUG(1),
|
|
|
|
"unable to open GeoIP2 database '%s' (status %d)",
|
|
|
|
pathbuf, ret);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2019-06-11 20:32:21 -07:00
|
|
|
return (NULL);
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
2019-06-11 20:32:21 -07:00
|
|
|
#endif /* HAVE_GEOIP2 */
|
|
|
|
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
void
|
2017-09-08 13:39:09 -07:00
|
|
|
named_geoip_init(void) {
|
2019-06-11 20:32:21 -07:00
|
|
|
#if defined(HAVE_GEOIP) || defined(HAVE_GEOIP2)
|
|
|
|
if (named_g_geoip == NULL) {
|
2017-09-08 13:39:09 -07:00
|
|
|
named_g_geoip = &geoip_table;
|
2019-06-11 20:32:21 -07:00
|
|
|
}
|
|
|
|
#if defined(HAVE_GEOIP)
|
|
|
|
GeoIP_cleanup();
|
|
|
|
#endif
|
2019-06-11 18:36:52 -07:00
|
|
|
#else
|
|
|
|
return;
|
2013-02-27 17:19:39 -08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-09-08 13:39:09 -07:00
|
|
|
named_geoip_load(char *dir) {
|
2019-06-11 18:36:52 -07:00
|
|
|
#if defined(HAVE_GEOIP2)
|
2019-06-11 20:32:21 -07:00
|
|
|
REQUIRE(dir != NULL);
|
|
|
|
|
|
|
|
named_g_geoip->country = open_geoip2(dir, "GeoIP2-Country.mmdb",
|
|
|
|
&geoip_country);
|
|
|
|
if (named_g_geoip->country == NULL) {
|
|
|
|
named_g_geoip->country = open_geoip2(dir,
|
|
|
|
"GeoLite2-Country.mmdb",
|
|
|
|
&geoip_country);
|
|
|
|
}
|
|
|
|
|
|
|
|
named_g_geoip->city = open_geoip2(dir, "GeoIP2-City.mmdb",
|
|
|
|
&geoip_city);
|
|
|
|
if (named_g_geoip->city == NULL) {
|
|
|
|
named_g_geoip->city = open_geoip2(dir, "GeoLite2-City.mmdb",
|
|
|
|
&geoip_city);
|
|
|
|
}
|
|
|
|
|
|
|
|
named_g_geoip->as = open_geoip2(dir, "GeoIP2-ASN.mmdb", &geoip_as);
|
|
|
|
if (named_g_geoip->as == NULL) {
|
|
|
|
named_g_geoip->as = open_geoip2(dir, "GeoLite2-ASN.mmdb",
|
|
|
|
&geoip_as);
|
|
|
|
}
|
|
|
|
|
|
|
|
named_g_geoip->isp = open_geoip2(dir, "GeoIP2-ISP.mmdb", &geoip_isp);
|
|
|
|
named_g_geoip->domain = open_geoip2(dir, "GeoIP2-Domain.mmdb",
|
|
|
|
&geoip_domain);
|
2019-06-11 18:36:52 -07:00
|
|
|
#elif defined(HAVE_GEOIP)
|
2013-02-27 17:19:39 -08:00
|
|
|
GeoIPOptions method;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
method = GEOIP_STANDARD;
|
|
|
|
#else
|
|
|
|
method = GEOIP_MMAP_CACHE;
|
|
|
|
#endif
|
|
|
|
|
2017-09-08 13:39:09 -07:00
|
|
|
named_geoip_init();
|
2013-02-27 17:19:39 -08:00
|
|
|
if (dir != NULL) {
|
2017-09-08 13:39:09 -07:00
|
|
|
isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
|
|
|
|
NAMED_LOGMODULE_SERVER, ISC_LOG_INFO,
|
2013-02-27 17:19:39 -08:00
|
|
|
"using \"%s\" as GeoIP directory", dir);
|
|
|
|
GeoIP_setup_custom_directory(dir);
|
|
|
|
}
|
|
|
|
|
2017-09-08 13:39:09 -07:00
|
|
|
init_geoip_db(&named_g_geoip->country_v4, GEOIP_COUNTRY_EDITION, 0,
|
2013-02-27 17:19:39 -08:00
|
|
|
method, "Country (IPv4)");
|
|
|
|
#ifdef HAVE_GEOIP_V6
|
2017-09-08 13:39:09 -07:00
|
|
|
init_geoip_db(&named_g_geoip->country_v6, GEOIP_COUNTRY_EDITION_V6, 0,
|
2013-02-27 17:19:39 -08:00
|
|
|
method, "Country (IPv6)");
|
|
|
|
#endif
|
|
|
|
|
2017-09-08 13:39:09 -07:00
|
|
|
init_geoip_db(&named_g_geoip->city_v4, GEOIP_CITY_EDITION_REV1,
|
2014-01-23 12:46:02 -08:00
|
|
|
GEOIP_CITY_EDITION_REV0, method, "City (IPv4)");
|
2013-02-27 17:19:39 -08:00
|
|
|
#if defined(HAVE_GEOIP_V6) && defined(HAVE_GEOIP_CITY_V6)
|
2017-09-08 13:39:09 -07:00
|
|
|
init_geoip_db(&named_g_geoip->city_v6, GEOIP_CITY_EDITION_REV1_V6,
|
2014-01-23 12:46:02 -08:00
|
|
|
GEOIP_CITY_EDITION_REV0_V6, method, "City (IPv6)");
|
2013-02-27 17:19:39 -08:00
|
|
|
#endif
|
|
|
|
|
2017-09-08 13:39:09 -07:00
|
|
|
init_geoip_db(&named_g_geoip->region, GEOIP_REGION_EDITION_REV1,
|
2014-01-23 12:46:02 -08:00
|
|
|
GEOIP_REGION_EDITION_REV0, method, "Region");
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2017-09-08 13:39:09 -07:00
|
|
|
init_geoip_db(&named_g_geoip->isp, GEOIP_ISP_EDITION, 0,
|
2013-02-27 17:19:39 -08:00
|
|
|
method, "ISP");
|
2017-09-08 13:39:09 -07:00
|
|
|
init_geoip_db(&named_g_geoip->org, GEOIP_ORG_EDITION, 0,
|
2013-02-27 17:19:39 -08:00
|
|
|
method, "Org");
|
2017-09-08 13:39:09 -07:00
|
|
|
init_geoip_db(&named_g_geoip->as, GEOIP_ASNUM_EDITION, 0,
|
2013-02-27 17:19:39 -08:00
|
|
|
method, "AS");
|
2017-09-08 13:39:09 -07:00
|
|
|
init_geoip_db(&named_g_geoip->domain, GEOIP_DOMAIN_EDITION, 0,
|
2013-02-27 17:19:39 -08:00
|
|
|
method, "Domain");
|
2017-09-08 13:39:09 -07:00
|
|
|
init_geoip_db(&named_g_geoip->netspeed, GEOIP_NETSPEED_EDITION, 0,
|
2013-02-27 17:19:39 -08:00
|
|
|
method, "NetSpeed");
|
2019-06-11 18:36:52 -07:00
|
|
|
#else
|
|
|
|
UNUSED(dir);
|
|
|
|
|
|
|
|
return;
|
|
|
|
#endif
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
2019-06-11 20:32:21 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
named_geoip_shutdown(void) {
|
|
|
|
#ifdef HAVE_GEOIP2
|
|
|
|
if (named_g_geoip->country != NULL) {
|
|
|
|
MMDB_close(named_g_geoip->country);
|
|
|
|
named_g_geoip->country = NULL;
|
|
|
|
}
|
|
|
|
if (named_g_geoip->city != NULL) {
|
|
|
|
MMDB_close(named_g_geoip->city);
|
|
|
|
named_g_geoip->city = NULL;
|
|
|
|
}
|
|
|
|
if (named_g_geoip->as != NULL) {
|
|
|
|
MMDB_close(named_g_geoip->as);
|
|
|
|
named_g_geoip->as = NULL;
|
|
|
|
}
|
|
|
|
if (named_g_geoip->isp != NULL) {
|
|
|
|
MMDB_close(named_g_geoip->isp);
|
|
|
|
named_g_geoip->isp = NULL;
|
|
|
|
}
|
|
|
|
if (named_g_geoip->domain != NULL) {
|
|
|
|
MMDB_close(named_g_geoip->domain);
|
|
|
|
named_g_geoip->domain = NULL;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_GEOIP2 */
|
|
|
|
|
|
|
|
dns_geoip_shutdown();
|
|
|
|
}
|