]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cgiirc.cpp
Fix m_cgiirc to set the user's host straight away when using WEBIRC.
[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 { PASS, IDENT, PASSFIRST, IDENTFIRST, 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 client hostname ip
60  *  Where password is a shared key, client is the name of the "client" 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 Command
68 {
69  public:
70         bool notify;
71         StringExtItem realhost;
72         StringExtItem realip;
73
74         CGIHostlist Hosts;
75         CommandWebirc(Module* Creator)
76                 : Command(Creator, "WEBIRC", 4),
77                   realhost("cgiirc_realhost", Creator), realip("cgiirc_realip", Creator)
78                 {
79                         works_before_reg = true;
80                         this->syntax = "password client hostname ip";
81                 }
82                 CmdResult Handle(const std::vector<std::string> &parameters, User *user)
83                 {
84                         if(user->registered == REG_ALL)
85                                 return CMD_FAILURE;
86
87                         for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
88                         {
89                                 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
90                                 {
91                                         if(iter->type == WEBIRC && parameters[0] == iter->password)
92                                         {
93                                                 realhost.set(user, user->host);
94                                                 realip.set(user, user->GetIPString());
95
96                                                 // 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
97                                                 bool host_ok = (parameters[2].length() <= ServerInstance->Config->Limits.MaxHost);
98                                                 const std::string& newhost = (host_ok ? parameters[2] : parameters[3]);
99
100                                                 if (notify)
101                                                         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());
102
103                                                 // Where the magic happens - change their IP
104                                                 ChangeIP(user, parameters[3]);
105                                                 // And follow this up by changing their host
106                                                 user->host = user->dhost = newhost;
107
108                                                 return CMD_SUCCESS;
109                                         }
110                                 }
111                         }
112
113                         ServerInstance->SNO->WriteGlobalSno('w', "Connecting user %s tried to use WEBIRC, but didn't match any configured webirc blocks.", user->GetFullRealHost().c_str());
114                         return CMD_FAILURE;
115                 }
116 };
117
118
119 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
120  */
121 class CGIResolver : public DNS::Request
122 {
123         std::string typ;
124         std::string theiruid;
125         LocalIntExt& waiting;
126         bool notify;
127  public:
128         CGIResolver(DNS::Manager *mgr, Module* me, bool NotifyOpers, const std::string &source, LocalUser* u,
129                         const std::string &ttype, LocalIntExt& ext)
130                 : DNS::Request(mgr, me, source, DNS::QUERY_PTR), typ(ttype), theiruid(u->uuid),
131                 waiting(ext), notify(NotifyOpers)
132         {
133         }
134
135         void OnLookupComplete(const DNS::Query *r) CXX11_OVERRIDE
136         {
137                 /* Check the user still exists */
138                 User* them = ServerInstance->FindUUID(theiruid);
139                 if ((them) && (!them->quitting))
140                 {
141                         LocalUser* lu = IS_LOCAL(them);
142                         if (!lu)
143                                 return;
144
145                         const DNS::ResourceRecord &ans_record = r->answers[0];
146                         if (ans_record.rdata.empty() || ans_record.rdata.length() > ServerInstance->Config->Limits.MaxHost)
147                                 return;
148
149                         if (notify)
150                                 ServerInstance->SNO->WriteGlobalSno('w', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from %s", them->nick.c_str(), them->host.c_str(), ans_record.rdata.c_str(), typ.c_str());
151
152                         them->host = them->dhost = ans_record.rdata;
153                         them->InvalidateCache();
154                         lu->CheckLines(true);
155                 }
156         }
157
158         void OnError(const DNS::Query *r) CXX11_OVERRIDE
159         {
160                 if (!notify)
161                         return;
162
163                 User* them = ServerInstance->FindUUID(theiruid);
164                 if ((them) && (!them->quitting))
165                 {
166                         ServerInstance->SNO->WriteToSnoMask('w', "Connecting user %s detected as using CGI:IRC (%s), but their host can't be resolved from their %s!", them->nick.c_str(), them->host.c_str(), typ.c_str());
167                 }
168         }
169
170         ~CGIResolver()
171         {
172                 User* them = ServerInstance->FindUUID(theiruid);
173                 if (!them)
174                         return;
175                 int count = waiting.get(them);
176                 if (count)
177                         waiting.set(them, count - 1);
178         }
179 };
180
181 class ModuleCgiIRC : public Module
182 {
183         CommandWebirc cmd;
184         LocalIntExt waiting;
185         dynamic_reference<DNS::Manager> DNS;
186
187         static void RecheckClass(LocalUser* user)
188         {
189                 user->MyClass = NULL;
190                 user->SetClass();
191                 user->CheckClass();
192         }
193
194         void HandleIdentOrPass(LocalUser* user, const std::string& newip, bool was_pass)
195         {
196                 cmd.realhost.set(user, user->host);
197                 cmd.realip.set(user, user->GetIPString());
198                 ChangeIP(user, newip);
199                 user->host = user->dhost = user->GetIPString();
200                 user->InvalidateCache();
201                 RecheckClass(user);
202
203                 // Don't create the resolver if the core couldn't put the user in a connect class or when dns is disabled
204                 if (user->quitting || !DNS || !user->MyClass->resolvehostnames)
205                         return;
206
207                 CGIResolver* r = new CGIResolver(*this->DNS, this, cmd.notify, newip, user, (was_pass ? "PASS" : "IDENT"), waiting);
208                 try
209                 {
210                         waiting.set(user, waiting.get(user) + 1);
211                         this->DNS->Process(r);
212                 }
213                 catch (DNS::Exception &ex)
214                 {
215                         int count = waiting.get(user);
216                         if (count)
217                                 waiting.set(user, count - 1);
218                         delete r;
219                         if (cmd.notify)
220                                  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());
221                 }
222         }
223
224 public:
225         ModuleCgiIRC()
226                 : cmd(this)
227                 , waiting("cgiirc-delay", this)
228                 , DNS(this, "DNS")
229         {
230         }
231
232         void init() CXX11_OVERRIDE
233         {
234                 ServerInstance->SNO->EnableSnomask('w', "CGIIRC");
235         }
236
237         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
238         {
239                 cmd.Hosts.clear();
240
241                 // Do we send an oper notice when a CGI:IRC has their host changed?
242                 cmd.notify = ServerInstance->Config->ConfValue("cgiirc")->getBool("opernotice", true);
243
244                 ConfigTagList tags = ServerInstance->Config->ConfTags("cgihost");
245                 for (ConfigIter i = tags.first; i != tags.second; ++i)
246                 {
247                         ConfigTag* tag = i->second;
248                         std::string hostmask = tag->getString("mask"); // An allowed CGI:IRC host
249                         std::string type = tag->getString("type"); // What type of user-munging we do on this host.
250                         std::string password = tag->getString("password");
251
252                         if(hostmask.length())
253                         {
254                                 if (type == "webirc" && password.empty())
255                                 {
256                                         ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
257                                 }
258                                 else
259                                 {
260                                         CGItype cgitype;
261                                         if (type == "pass")
262                                                 cgitype = PASS;
263                                         else if (type == "ident")
264                                                 cgitype = IDENT;
265                                         else if (type == "passfirst")
266                                                 cgitype = PASSFIRST;
267                                         else if (type == "webirc")
268                                                 cgitype = WEBIRC;
269                                         else
270                                         {
271                                                 cgitype = PASS;
272                                                 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "Invalid <cgihost:type> value in config: %s, setting it to \"pass\"", type.c_str());
273                                         }
274
275                                         cmd.Hosts.push_back(CGIhost(hostmask, cgitype, password));
276                                 }
277                         }
278                         else
279                         {
280                                 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
281                                 continue;
282                         }
283                 }
284         }
285
286         ModResult OnCheckReady(LocalUser *user) CXX11_OVERRIDE
287         {
288                 if (waiting.get(user))
289                         return MOD_RES_DENY;
290
291                 if (!cmd.realip.get(user))
292                         return MOD_RES_PASSTHRU;
293
294                 RecheckClass(user);
295                 if (user->quitting)
296                         return MOD_RES_DENY;
297
298                 user->CheckLines(true);
299                 if (user->quitting)
300                         return MOD_RES_DENY;
301
302                 return MOD_RES_PASSTHRU;
303         }
304
305         ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
306         {
307                 for(CGIHostlist::iterator iter = cmd.Hosts.begin(); iter != cmd.Hosts.end(); iter++)
308                 {
309                         if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
310                         {
311                                 // Deal with it...
312                                 if(iter->type == PASS)
313                                 {
314                                         CheckPass(user); // We do nothing if it fails so...
315                                         user->CheckLines(true);
316                                 }
317                                 else if(iter->type == PASSFIRST && !CheckPass(user))
318                                 {
319                                         // If the password lookup failed, try the ident
320                                         CheckIdent(user);       // If this fails too, do nothing
321                                         user->CheckLines(true);
322                                 }
323                                 else if(iter->type == IDENT)
324                                 {
325                                         CheckIdent(user); // Nothing on failure.
326                                         user->CheckLines(true);
327                                 }
328                                 else if(iter->type == IDENTFIRST && !CheckIdent(user))
329                                 {
330                                         // If the ident lookup fails, try the password.
331                                         CheckPass(user);
332                                         user->CheckLines(true);
333                                 }
334                                 else if(iter->type == WEBIRC)
335                                 {
336                                         // We don't need to do anything here
337                                 }
338                                 return MOD_RES_PASSTHRU;
339                         }
340                 }
341                 return MOD_RES_PASSTHRU;
342         }
343
344         bool CheckPass(LocalUser* user)
345         {
346                 if(IsValidHost(user->password))
347                 {
348                         HandleIdentOrPass(user, user->password, true);
349                         user->password.clear();
350                         return true;
351                 }
352
353                 return false;
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                 HandleIdentOrPass(user, newipstr, false);
377
378                 return true;
379         }
380
381         bool IsValidHost(const std::string &host)
382         {
383                 if(!host.size() || host.size() > ServerInstance->Config->Limits.MaxHost)
384                         return false;
385
386                 for(unsigned int i = 0; i < host.size(); i++)
387                 {
388                         if(     ((host[i] >= '0') && (host[i] <= '9')) ||
389                                         ((host[i] >= 'A') && (host[i] <= 'Z')) ||
390                                         ((host[i] >= 'a') && (host[i] <= 'z')) ||
391                                         ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
392                                         ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
393
394                                 continue;
395                         else
396                                 return false;
397                 }
398
399                 return true;
400         }
401
402         Version GetVersion() CXX11_OVERRIDE
403         {
404                 return Version("Change user's hosts connecting from known CGI:IRC hosts",VF_VENDOR);
405         }
406 };
407
408 MODULE_INIT(ModuleCgiIRC)