]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_geoip.cpp
Implement support for IPv6 GeoIP lookups.
[user/henk/code/inspircd.git] / src / modules / extra / m_geoip.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /// $CompilerFlags: find_compiler_flags("geoip" "")
21 /// $LinkerFlags: find_linker_flags("geoip" "-lGeoIP")
22
23 /// $PackageInfo: require_system("centos" "7.0") GeoIP-devel pkgconfig
24 /// $PackageInfo: require_system("darwin") geoip pkg-config
25 /// $PackageInfo: require_system("debian") libgeoip-dev pkg-config
26 /// $PackageInfo: require_system("ubuntu") libgeoip-dev pkg-config
27
28 #include "inspircd.h"
29 #include "xline.h"
30 #include "modules/stats.h"
31 #include "modules/whois.h"
32
33 // Fix warnings about the use of commas at end of enumerator lists on C++03.
34 #if defined __clang__
35 # pragma clang diagnostic ignored "-Wc++11-extensions"
36 #elif defined __GNUC__
37 # pragma GCC diagnostic ignored "-pedantic"
38 #endif
39
40 #include <GeoIP.h>
41
42 #ifdef _WIN32
43 # pragma comment(lib, "GeoIP.lib")
44 #endif
45
46 enum
47 {
48         // InspIRCd-specific.
49         RPL_WHOISCOUNTRY = 344
50 };
51
52 class ModuleGeoIP : public Module, public Stats::EventListener, public Whois::EventListener
53 {
54         StringExtItem ext;
55         bool extban;
56         GeoIP* ipv4db;
57         GeoIP* ipv6db;
58
59         std::string* SetExt(User* user)
60         {
61                 const char* code = NULL;
62                 switch (user->client_sa.family())
63                 {
64                         case AF_INET:
65                                 code = GeoIP_country_code_by_addr(ipv4db, user->GetIPString().c_str());
66                                 break;
67
68                         case AF_INET6:
69                                 code = GeoIP_country_code_by_addr_v6(ipv6db, user->GetIPString().c_str());
70                                 break;
71                 }
72
73                 ext.set(user, code ? code : "UNK");
74                 return ext.get(user);
75         }
76
77  public:
78         ModuleGeoIP()
79                 : Stats::EventListener(this)
80                 , Whois::EventListener(this)
81                 , ext("geoip_cc", ExtensionItem::EXT_USER, this)
82                 , extban(true)
83                 , ipv4db(NULL)
84                 , ipv6db(NULL)
85         {
86         }
87
88         void init() CXX11_OVERRIDE
89         {
90                 ipv4db = GeoIP_open_type(GEOIP_COUNTRY_EDITION, GEOIP_STANDARD);
91                 if (!ipv4db)
92                         throw ModuleException("Unable to load the IPv4 GeoIP database. Are you missing GeoIP.dat?");
93
94                 ipv6db = GeoIP_open_type(GEOIP_COUNTRY_EDITION_V6, GEOIP_STANDARD);
95                 if (!ipv6db)
96                         throw ModuleException("Unable to load the IPv6 GeoIP database. Are you missing GeoIPv6.dat?");
97
98                 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
99                 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
100                 {
101                         LocalUser* user = *i;
102                         if ((user->registered == REG_ALL) && (!ext.get(user)))
103                         {
104                                 SetExt(user);
105                         }
106                 }
107         }
108
109         ~ModuleGeoIP()
110         {
111                 if (ipv4db)
112                         GeoIP_delete(ipv4db);
113
114                 if (ipv6db)
115                         GeoIP_delete(ipv6db);
116         }
117
118         void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
119         {
120                 ConfigTag* tag = ServerInstance->Config->ConfValue("geoip");
121                 extban = tag->getBool("extban");
122         }
123
124         Version GetVersion() CXX11_OVERRIDE
125         {
126                 return Version("Provides a way to assign users to connect classes by country using GeoIP lookup", VF_OPTCOMMON|VF_VENDOR);
127         }
128
129         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
130         {
131                 if (extban)
132                         tokens["EXTBAN"].push_back('G');
133         }
134
135         ModResult OnCheckBan(User* user, Channel*, const std::string& mask) CXX11_OVERRIDE
136         {
137                 if (extban && (mask.length() > 2) && (mask[0] == 'G') && (mask[1] == ':'))
138                 {
139                         std::string* cc = ext.get(user);
140                         if (!cc)
141                                 cc = SetExt(user);
142
143                         if (InspIRCd::Match(*cc, mask.substr(2)))
144                                 return MOD_RES_DENY;
145                 }
146                 return MOD_RES_PASSTHRU;
147         }
148
149         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
150         {
151                 std::string* cc = ext.get(user);
152                 if (!cc)
153                         cc = SetExt(user);
154
155                 std::string geoip = myclass->config->getString("geoip");
156                 if (geoip.empty())
157                         return MOD_RES_PASSTHRU;
158                 irc::commasepstream list(geoip);
159                 std::string country;
160                 while (list.GetToken(country))
161                         if (country == *cc)
162                                 return MOD_RES_PASSTHRU;
163                 return MOD_RES_DENY;
164         }
165
166         void OnSetUserIP(LocalUser* user) CXX11_OVERRIDE
167         {
168                 // If user has sent NICK/USER, re-set the ExtItem as this is likely CGI:IRC changing the IP
169                 if (user->registered == REG_NICKUSER)
170                         SetExt(user);
171         }
172
173         void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
174         {
175                 // If the extban is disabled we don't expose users location.
176                 if (!extban)
177                         return;
178
179                 std::string* cc = ext.get(whois.GetTarget());
180                 if (!cc)
181                         cc = SetExt(whois.GetTarget());
182
183                 whois.SendLine(RPL_WHOISCOUNTRY, *cc, "is located in this country");
184         }
185
186         ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
187         {
188                 if (stats.GetSymbol() != 'G')
189                         return MOD_RES_PASSTHRU;
190
191                 unsigned int unknown = 0;
192                 std::map<std::string, unsigned int> results;
193
194                 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
195                 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
196                 {
197                         std::string* cc = ext.get(*i);
198                         if (cc)
199                                 results[*cc]++;
200                         else
201                                 unknown++;
202                 }
203
204                 for (std::map<std::string, unsigned int>::const_iterator i = results.begin(); i != results.end(); ++i)
205                 {
206                         stats.AddRow(801, "GeoIPSTATS " + i->first + " " + ConvToStr(i->second));
207                 }
208
209                 if (unknown)
210                         stats.AddRow(801, "GeoIPSTATS Unknown " + ConvToStr(unknown));
211
212                 return MOD_RES_DENY;
213         }
214 };
215
216 MODULE_INIT(ModuleGeoIP)