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