]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cgiirc.cpp
305801b0ac8b095c29b8db5aa22e40af9091788d
[user/henk/code/inspircd.git] / src / modules / m_cgiirc.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007-2008 John Brooks <john.brooks@dereferenced.net>
6  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
7  *   Copyright (C) 2006-2008 Craig Edwards <craigedwards@brainbox.cc>
8  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
9  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
10  *   Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #include "inspircd.h"
27 #include "xline.h"
28 #include "modules/dns.h"
29
30 enum CGItype { IDENT, WEBIRC };
31
32 // We need this method up here so that it can be accessed from anywhere
33 static void ChangeIP(User* user, const std::string& newip)
34 {
35         ServerInstance->Users->RemoveCloneCounts(user);
36         user->SetClientIP(newip.c_str());
37         ServerInstance->Users->AddClone(user);
38 }
39
40 /** Holds a CGI site's details
41  */
42 class CGIhost
43 {
44 public:
45         std::string hostmask;
46         CGItype type;
47         std::string password;
48
49         CGIhost(const std::string &mask, CGItype t, const std::string &spassword)
50         : hostmask(mask), type(t), password(spassword)
51         {
52         }
53 };
54 typedef std::vector<CGIhost> CGIHostlist;
55
56 /*
57  * WEBIRC
58  *  This is used for the webirc method of CGIIRC auth, and is (really) the best way to do these things.
59  *  Syntax: WEBIRC password gateway hostname ip
60  *  Where password is a shared key, gateway is the name of the WebIRC gateway and version (e.g. cgiirc), hostname
61  *  is the resolved host of the client issuing the command and IP is the real IP of the client.
62  *
63  * How it works:
64  *  To tie in with the rest of cgiirc module, and to avoid race conditions, /webirc is only processed locally
65  *  and simply sets metadata on the user, which is later decoded on full connect to give something meaningful.
66  */
67 class CommandWebIRC : public SplitCommand
68 {
69  public:
70         bool notify;
71         StringExtItem gateway;
72         StringExtItem realhost;
73         StringExtItem realip;
74
75         CGIHostlist Hosts;
76         CommandWebIRC(Module* Creator)
77                 : SplitCommand(Creator, "WEBIRC", 4)
78                 , gateway("cgiirc_gateway", ExtensionItem::EXT_USER, Creator)
79                 , realhost("cgiirc_realhost", ExtensionItem::EXT_USER, Creator)
80                 , realip("cgiirc_realip", ExtensionItem::EXT_USER, Creator)
81                 {
82                         allow_empty_last_param = false;
83                         works_before_reg = true;
84                         this->syntax = "password gateway hostname ip";
85                 }
86                 CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user)
87                 {
88                         if(user->registered == REG_ALL)
89                                 return CMD_FAILURE;
90
91                         irc::sockets::sockaddrs ipaddr;
92                         if (!irc::sockets::aptosa(parameters[3], 0, ipaddr))
93                         {
94                                 user->CommandFloodPenalty += 5000;
95                                 ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s tried to use WEBIRC but gave an invalid IP address.", user->GetFullRealHost().c_str());
96                                 return CMD_FAILURE;
97                         }
98
99                         for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
100                         {
101                                 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
102                                 {
103                                         if(iter->type == WEBIRC && parameters[0] == iter->password)
104                                         {
105                                                 gateway.set(user, parameters[1]);
106                                                 realhost.set(user, user->host);
107                                                 realip.set(user, user->GetIPString());
108
109                                                 // Check if we're happy with the provided hostname. If it's problematic then make sure we won't set a host later, just the IP
110                                                 bool host_ok = (parameters[2].length() <= ServerInstance->Config->Limits.MaxHost);
111                                                 const std::string& newhost = (host_ok ? parameters[2] : parameters[3]);
112
113                                                 if (notify)
114                                                         ServerInstance->SNO->WriteGlobalSno('w', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from %s", user->nick.c_str(), user->host.c_str(), newhost.c_str(), user->host.c_str());
115
116                                                 // Where the magic happens - change their IP
117                                                 ChangeIP(user, parameters[3]);
118                                                 // And follow this up by changing their host
119                                                 user->host = user->dhost = newhost;
120                                                 user->InvalidateCache();
121
122                                                 return CMD_SUCCESS;
123                                         }
124                                 }
125                         }
126
127                         user->CommandFloodPenalty += 5000;
128                         ServerInstance->SNO->WriteGlobalSno('w', "Connecting user %s tried to use WEBIRC, but didn't match any configured webirc blocks.", user->GetFullRealHost().c_str());
129                         return CMD_FAILURE;
130                 }
131 };
132
133
134 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
135  */
136 class CGIResolver : public DNS::Request
137 {
138         std::string theiruid;
139         LocalIntExt& waiting;
140         bool notify;
141  public:
142         CGIResolver(DNS::Manager* mgr, Module* me, bool NotifyOpers, const std::string &source, LocalUser* u, LocalIntExt& ext)
143                 : DNS::Request(mgr, me, source, DNS::QUERY_PTR)
144                 , theiruid(u->uuid)
145                 , waiting(ext)
146                 , notify(NotifyOpers)
147         {
148         }
149
150         void OnLookupComplete(const DNS::Query *r) CXX11_OVERRIDE
151         {
152                 /* Check the user still exists */
153                 User* them = ServerInstance->FindUUID(theiruid);
154                 if ((them) && (!them->quitting))
155                 {
156                         LocalUser* lu = IS_LOCAL(them);
157                         if (!lu)
158                                 return;
159
160                         const DNS::ResourceRecord &ans_record = r->answers[0];
161                         if (ans_record.rdata.empty() || ans_record.rdata.length() > ServerInstance->Config->Limits.MaxHost)
162                                 return;
163
164                         if (notify)
165                                 ServerInstance->SNO->WriteGlobalSno('w', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s", them->nick.c_str(), them->host.c_str(), ans_record.rdata.c_str());
166
167                         them->host = them->dhost = ans_record.rdata;
168                         them->InvalidateCache();
169                         lu->CheckLines(true);
170                 }
171         }
172
173         void OnError(const DNS::Query *r) CXX11_OVERRIDE
174         {
175                 if (!notify)
176                         return;
177
178                 User* them = ServerInstance->FindUUID(theiruid);
179                 if ((them) && (!them->quitting))
180                 {
181                         ServerInstance->SNO->WriteToSnoMask('w', "Connecting user %s detected as using CGI:IRC (%s), but their host can't be resolved!", them->nick.c_str(), them->host.c_str());
182                 }
183         }
184
185         ~CGIResolver()
186         {
187                 User* them = ServerInstance->FindUUID(theiruid);
188                 if (!them)
189                         return;
190                 int count = waiting.get(them);
191                 if (count)
192                         waiting.set(them, count - 1);
193         }
194 };
195
196 class ModuleCgiIRC : public Module
197 {
198         CommandWebIRC cmd;
199         LocalIntExt waiting;
200         dynamic_reference<DNS::Manager> DNS;
201
202         static void RecheckClass(LocalUser* user)
203         {
204                 user->MyClass = NULL;
205                 user->SetClass();
206                 user->CheckClass();
207         }
208
209         void HandleIdent(LocalUser* user, const std::string& newip)
210         {
211                 cmd.realhost.set(user, user->host);
212                 cmd.realip.set(user, user->GetIPString());
213                 ChangeIP(user, newip);
214                 user->host = user->dhost = user->GetIPString();
215                 user->InvalidateCache();
216                 RecheckClass(user);
217
218                 // Don't create the resolver if the core couldn't put the user in a connect class or when dns is disabled
219                 if (user->quitting || !DNS || !user->MyClass->resolvehostnames)
220                         return;
221
222                 CGIResolver* r = new CGIResolver(*this->DNS, this, cmd.notify, newip, user, waiting);
223                 try
224                 {
225                         waiting.set(user, waiting.get(user) + 1);
226                         this->DNS->Process(r);
227                 }
228                 catch (DNS::Exception &ex)
229                 {
230                         int count = waiting.get(user);
231                         if (count)
232                                 waiting.set(user, count - 1);
233                         delete r;
234                         if (cmd.notify)
235                                  ServerInstance->SNO->WriteToSnoMask('w', "Connecting user %s detected as using CGI:IRC (%s), but I could not resolve their hostname; %s", user->nick.c_str(), user->host.c_str(), ex.GetReason().c_str());
236                 }
237         }
238
239 public:
240         ModuleCgiIRC()
241                 : cmd(this)
242                 , waiting("cgiirc-delay", ExtensionItem::EXT_USER, this)
243                 , DNS(this, "DNS")
244         {
245         }
246
247         void init() CXX11_OVERRIDE
248         {
249                 ServerInstance->SNO->EnableSnomask('w', "CGIIRC");
250         }
251
252         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
253         {
254                 cmd.Hosts.clear();
255
256                 // Do we send an oper notice when a CGI:IRC has their host changed?
257                 cmd.notify = ServerInstance->Config->ConfValue("cgiirc")->getBool("opernotice", true);
258
259                 ConfigTagList tags = ServerInstance->Config->ConfTags("cgihost");
260                 for (ConfigIter i = tags.first; i != tags.second; ++i)
261                 {
262                         ConfigTag* tag = i->second;
263                         std::string hostmask = tag->getString("mask"); // An allowed CGI:IRC host
264                         std::string type = tag->getString("type"); // What type of user-munging we do on this host.
265                         std::string password = tag->getString("password");
266
267                         if(hostmask.length())
268                         {
269                                 if (type == "webirc" && password.empty())
270                                 {
271                                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Missing password in config: %s", hostmask.c_str());
272                                 }
273                                 else
274                                 {
275                                         CGItype cgitype;
276                                         if (type == "ident")
277                                                 cgitype = IDENT;
278                                         else if (type == "webirc")
279                                                 cgitype = WEBIRC;
280                                         else
281                                         {
282                                                 cgitype = IDENT;
283                                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Invalid <cgihost:type> value in config: %s, setting it to \"ident\"", type.c_str());
284                                         }
285
286                                         cmd.Hosts.push_back(CGIhost(hostmask, cgitype, password));
287                                 }
288                         }
289                         else
290                         {
291                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
292                                 continue;
293                         }
294                 }
295         }
296
297         ModResult OnCheckReady(LocalUser *user) CXX11_OVERRIDE
298         {
299                 if (waiting.get(user))
300                         return MOD_RES_DENY;
301
302                 if (!cmd.realip.get(user))
303                         return MOD_RES_PASSTHRU;
304
305                 RecheckClass(user);
306                 if (user->quitting)
307                         return MOD_RES_DENY;
308
309                 user->CheckLines(true);
310                 if (user->quitting)
311                         return MOD_RES_DENY;
312
313                 return MOD_RES_PASSTHRU;
314         }
315
316         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
317         {
318                 // If <connect:webirc> is not set then we have nothing to do.
319                 const std::string webirc = myclass->config->getString("webirc");
320                 if (webirc.empty())
321                         return MOD_RES_PASSTHRU;
322
323                 // If the user is not connecting via a WebIRC gateway then they
324                 // cannot match this connect class.
325                 const std::string* gateway = cmd.gateway.get(user);
326                 if (!gateway)
327                         return MOD_RES_DENY;
328
329                 // If the gateway matches the <connect:webirc> constraint then
330                 // allow the check to continue. Otherwise, reject it.
331                 return InspIRCd::Match(*gateway, webirc) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
332         }
333
334         ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
335         {
336                 for(CGIHostlist::iterator iter = cmd.Hosts.begin(); iter != cmd.Hosts.end(); iter++)
337                 {
338                         if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
339                         {
340                                 // Deal with it...
341                                 if(iter->type == IDENT)
342                                 {
343                                         CheckIdent(user); // Nothing on failure.
344                                         user->CheckLines(true);
345                                 }
346                                 else if(iter->type == WEBIRC)
347                                 {
348                                         // We don't need to do anything here
349                                 }
350                                 return MOD_RES_PASSTHRU;
351                         }
352                 }
353                 return MOD_RES_PASSTHRU;
354         }
355
356         bool CheckIdent(LocalUser* user)
357         {
358                 const char* ident;
359                 in_addr newip;
360
361                 if (user->ident.length() == 8)
362                         ident = user->ident.c_str();
363                 else if (user->ident.length() == 9 && user->ident[0] == '~')
364                         ident = user->ident.c_str() + 1;
365                 else
366                         return false;
367
368                 errno = 0;
369                 unsigned long ipaddr = strtoul(ident, NULL, 16);
370                 if (errno)
371                         return false;
372                 newip.s_addr = htonl(ipaddr);
373                 std::string newipstr(inet_ntoa(newip));
374
375                 user->ident = "~cgiirc";
376                 HandleIdent(user, newipstr);
377
378                 return true;
379         }
380
381         Version GetVersion() CXX11_OVERRIDE
382         {
383                 return Version("Change user's hosts connecting from known CGI:IRC hosts",VF_VENDOR);
384         }
385 };
386
387 MODULE_INIT(ModuleCgiIRC)