/* * ip2country.c - MaxMind GeoIP dlfunc for Exim * https://mta.org.ua/exim-4.94-conf/dlfunc/ip2country/ip2country.c * * Copyright (C) 2006-2020 Victor Ustugov * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published * by the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Based on source code from http://www.ols.es/exim/dlext/ * by David Saez */ #include #include #include //#define DLFUNC_IMPL //#include "local_scan.h" //#include "macros.h" #include "exim.h" //# define string_copy(s) string_copy_function(s) //# define string_copyn(s, n) string_copyn_function((s), (n)) //# define string_copy_taint(s, t) string_copy_taint_function((s), (t)) extern uschar * string_copy_function(const uschar *); extern uschar * string_copyn_function(const uschar *, int n); extern uschar * string_copy_taint_function(const uschar *, BOOL tainted); static GeoIP *gi=NULL; /***************************************************************************** * Country code lookup function: * *****************************************************************************/ int ip2country(uschar **yield, int argc, uschar *argv[]) { uschar *s; if (argc != 1) { *yield = string_copy(US"Invalid number of arguments"); return ERROR; } if (!gi) { gi = GeoIP_new(GEOIP_STANDARD); if (!gi) { *yield = string_copy(US"Cannot initialize GeoIP"); return ERROR; } } s = (uschar *)GeoIP_country_code_by_addr(gi, (char *)argv[0]); if (!s) { *yield = string_copy(US"--"); } else { *yield = string_copy(s); } return OK; } int ip2continent(uschar **yield, int argc, uschar *argv[]) { uschar *s; uschar *country; if (argc != 1) { *yield = *yield = string_copy(US"Invalid number of arguments"); return ERROR; } if (!gi) { gi = GeoIP_new(GEOIP_STANDARD); if (!gi) { *yield = string_copy(US"Cannot initialize GeoIP"); return ERROR; } } country = (uschar *)GeoIP_country_code_by_addr(gi, (char *)argv[0]); if (!country) { *yield = string_copy(US"--"); } else { s = (uschar *)GeoIP_continent_by_id(GeoIP_id_by_code((char *)country)); if (!s) { *yield = string_copy(US"--"); } else { *yield = string_copy(s); } } return OK; }