]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cgiirc.cpp
8e46a5d4d719e6feb01f0a7683c7a302704761b4
[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
29 /* $ModDesc: Change user's hosts connecting from known CGI:IRC hosts */
30
31 enum CGItype { PASS, IDENT, PASSFIRST, IDENTFIRST, WEBIRC };
32
33
34 /** Holds a CGI site's details
35  */
36 class CGIhost
37 {
38 public:
39         std::string hostmask;
40         CGItype type;
41         std::string password;
42
43         CGIhost(const std::string &mask, CGItype t, const std::string &spassword)
44         : hostmask(mask), type(t), password(spassword)
45         {
46         }
47 };
48 typedef std::vector<CGIhost> CGIHostlist;
49
50 /*
51  * WEBIRC
52  *  This is used for the webirc method of CGIIRC auth, and is (really) the best way to do these things.
53  *  Syntax: WEBIRC password client hostname ip
54  *  Where password is a shared key, client is the name of the "client" and version (e.g. cgiirc), hostname
55  *  is the resolved host of the client issuing the command and IP is the real IP of the client.
56  *
57  * How it works:
58  *  To tie in with the rest of cgiirc module, and to avoid race conditions, /webirc is only processed locally
59  *  and simply sets metadata on the user, which is later decoded on full connect to give something meaningful.
60  */
61 class CommandWebirc : public Command
62 {
63  public:
64         bool notify;
65         StringExtItem realhost;
66         StringExtItem realip;
67         LocalStringExt webirc_hostname;
68         LocalStringExt webirc_ip;
69
70         CGIHostlist Hosts;
71         CommandWebirc(Module* Creator)
72                 : Command(Creator, "WEBIRC", 4),
73                   realhost("cgiirc_realhost", Creator), realip("cgiirc_realip", Creator),
74                   webirc_hostname("cgiirc_webirc_hostname", Creator), webirc_ip("cgiirc_webirc_ip", Creator)
75                 {
76                         works_before_reg = true;
77                         this->syntax = "password client hostname ip";
78                 }
79                 CmdResult Handle(const std::vector<std::string> &parameters, User *user)
80                 {
81                         if(user->registered == REG_ALL)
82                                 return CMD_FAILURE;
83
84                         for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
85                         {
86                                 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
87                                 {
88                                         if(iter->type == WEBIRC && parameters[0] == iter->password)
89                                         {
90                                                 realhost.set(user, user->host);
91                                                 realip.set(user, user->GetIPString());
92                                                 if (notify)
93                                                         ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from %s", user->nick.c_str(), user->host.c_str(), parameters[2].c_str(), user->host.c_str());
94
95                                                 // 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
96                                                 if (parameters[2].length() < 64)
97                                                         webirc_hostname.set(user, parameters[2]);
98                                                 else
99                                                         webirc_hostname.unset(user);
100
101                                                 webirc_ip.set(user, parameters[3]);
102                                                 return CMD_SUCCESS;
103                                         }
104                                 }
105                         }
106
107                         ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s tried to use WEBIRC, but didn't match any configured webirc blocks.", user->GetFullRealHost().c_str());
108                         return CMD_FAILURE;
109                 }
110 };
111
112
113 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
114  */
115 class CGIResolver : public Resolver
116 {
117         std::string typ;
118         std::string theiruid;
119         LocalIntExt& waiting;
120         bool notify;
121  public:
122         CGIResolver(Module* me, bool NotifyOpers, const std::string &source, LocalUser* u,
123                         const std::string &type, bool &cached, LocalIntExt& ext)
124                 : Resolver(source, DNS_QUERY_PTR4, cached, me), typ(type), theiruid(u->uuid),
125                 waiting(ext), notify(NotifyOpers)
126         {
127         }
128
129         virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
130         {
131                 /* Check the user still exists */
132                 User* them = ServerInstance->FindUUID(theiruid);
133                 if (them)
134                 {
135                         if (notify)
136                                 ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from %s", them->nick.c_str(), them->host.c_str(), result.c_str(), typ.c_str());
137
138                         if (result.length() > 64)
139                                 return;
140                         them->host = result;
141                         them->dhost = result;
142                         them->InvalidateCache();
143                         them->CheckLines(true);
144                 }
145         }
146
147         virtual void OnError(ResolverError e, const std::string &errormessage)
148         {
149                 User* them = ServerInstance->FindUUID(theiruid);
150                 if (them)
151                 {
152                         if (notify)
153                                 ServerInstance->SNO->WriteToSnoMask('a', "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());
154                 }
155         }
156
157         virtual ~CGIResolver()
158         {
159                 User* them = ServerInstance->FindUUID(theiruid);
160                 if (!them)
161                         return;
162                 int count = waiting.get(them);
163                 if (count)
164                         waiting.set(them, count - 1);
165         }
166 };
167
168 class ModuleCgiIRC : public Module
169 {
170         CommandWebirc cmd;
171         LocalIntExt waiting;
172
173         static void RecheckElineAndClass(LocalUser* user)
174         {
175                 user->exempt = (ServerInstance->XLines->MatchesLine("E", user) != NULL);
176                 user->SetClass();
177                 user->CheckClass();
178         }
179
180         static void ChangeIP(LocalUser* user, const std::string& newip)
181         {
182                 ServerInstance->Users->RemoveCloneCounts(user);
183                 user->SetClientIP(newip.c_str());
184                 ServerInstance->Users->AddLocalClone(user);
185                 ServerInstance->Users->AddGlobalClone(user);
186         }
187
188         void HandleIdentOrPass(LocalUser* user, const std::string& newip, bool was_pass)
189         {
190                 cmd.realhost.set(user, user->host);
191                 cmd.realip.set(user, user->GetIPString());
192                 ChangeIP(user, newip);
193                 user->host = user->dhost = user->GetIPString();
194                 user->InvalidateCache();
195                 RecheckElineAndClass(user);
196                 // Don't create the resolver if the core couldn't put the user in a connect class
197                 if (user->quitting)
198                         return;
199
200                 try
201                 {
202                         bool cached;
203                         CGIResolver* r = new CGIResolver(this, cmd.notify, newip, user, (was_pass ? "PASS" : "IDENT"), cached, waiting);
204                         ServerInstance->AddResolver(r, cached);
205                         waiting.set(user, waiting.get(user) + 1);
206                 }
207                 catch (...)
208                 {
209                         if (cmd.notify)
210                                  ServerInstance->SNO->WriteToSnoMask('a', "Connecting user %s detected as using CGI:IRC (%s), but I could not resolve their hostname!", user->nick.c_str(), user->host.c_str());
211                 }
212         }
213
214 public:
215         ModuleCgiIRC() : cmd(this), waiting("cgiirc-delay", this)
216         {
217         }
218
219         void init()
220         {
221                 OnRehash(NULL);
222                 ServerInstance->AddCommand(&cmd);
223                 ServerInstance->Extensions.Register(&cmd.realhost);
224                 ServerInstance->Extensions.Register(&cmd.realip);
225                 ServerInstance->Extensions.Register(&cmd.webirc_hostname);
226                 ServerInstance->Extensions.Register(&cmd.webirc_ip);
227                 ServerInstance->Extensions.Register(&waiting);
228
229                 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnCheckReady };
230                 ServerInstance->Modules->Attach(eventlist, this, 3);
231         }
232
233         void OnRehash(User* user)
234         {
235                 cmd.Hosts.clear();
236
237                 // Do we send an oper notice when a CGI:IRC has their host changed?
238                 cmd.notify = ServerInstance->Config->ConfValue("cgiirc")->getBool("opernotice", true);
239
240                 ConfigTagList tags = ServerInstance->Config->ConfTags("cgihost");
241                 for (ConfigIter i = tags.first; i != tags.second; ++i)
242                 {
243                         ConfigTag* tag = i->second;
244                         std::string hostmask = tag->getString("mask"); // An allowed CGI:IRC host
245                         std::string type = tag->getString("type"); // What type of user-munging we do on this host.
246                         std::string password = tag->getString("password");
247
248                         if(hostmask.length())
249                         {
250                                 if (type == "webirc" && password.empty())
251                                 {
252                                         ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
253                                 }
254                                 else
255                                 {
256                                         CGItype cgitype;
257                                         if (type == "pass")
258                                                 cgitype = PASS;
259                                         else if (type == "ident")
260                                                 cgitype = IDENT;
261                                         else if (type == "passfirst")
262                                                 cgitype = PASSFIRST;
263                                         else if (type == "webirc")
264                                                 cgitype = WEBIRC;
265                                         else
266                                         {
267                                                 cgitype = PASS;
268                                                 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc.so: Invalid <cgihost:type> value in config: %s, setting it to \"pass\"", type.c_str());
269                                         }
270
271                                         cmd.Hosts.push_back(CGIhost(hostmask, cgitype, password));
272                                 }
273                         }
274                         else
275                         {
276                                 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
277                                 continue;
278                         }
279                 }
280         }
281
282         ModResult OnCheckReady(LocalUser *user)
283         {
284                 if (waiting.get(user))
285                         return MOD_RES_DENY;
286
287                 std::string *webirc_ip = cmd.webirc_ip.get(user);
288                 if (!webirc_ip)
289                         return MOD_RES_PASSTHRU;
290
291                 ChangeIP(user, *webirc_ip);
292
293                 std::string* webirc_hostname = cmd.webirc_hostname.get(user);
294                 user->host = user->dhost = (webirc_hostname ? *webirc_hostname : user->GetIPString());
295
296                 RecheckElineAndClass(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                 cmd.webirc_hostname.unset(user);
305                 cmd.webirc_ip.unset(user);
306
307                 return MOD_RES_PASSTHRU;
308         }
309
310         ModResult OnUserRegister(LocalUser* user)
311         {
312                 for(CGIHostlist::iterator iter = cmd.Hosts.begin(); iter != cmd.Hosts.end(); iter++)
313                 {
314                         if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
315                         {
316                                 // Deal with it...
317                                 if(iter->type == PASS)
318                                 {
319                                         CheckPass(user); // We do nothing if it fails so...
320                                         user->CheckLines(true);
321                                 }
322                                 else if(iter->type == PASSFIRST && !CheckPass(user))
323                                 {
324                                         // If the password lookup failed, try the ident
325                                         CheckIdent(user);       // If this fails too, do nothing
326                                         user->CheckLines(true);
327                                 }
328                                 else if(iter->type == IDENT)
329                                 {
330                                         CheckIdent(user); // Nothing on failure.
331                                         user->CheckLines(true);
332                                 }
333                                 else if(iter->type == IDENTFIRST && !CheckIdent(user))
334                                 {
335                                         // If the ident lookup fails, try the password.
336                                         CheckPass(user);
337                                         user->CheckLines(true);
338                                 }
339                                 else if(iter->type == WEBIRC)
340                                 {
341                                         // We don't need to do anything here
342                                 }
343                                 return MOD_RES_PASSTHRU;
344                         }
345                 }
346                 return MOD_RES_PASSTHRU;
347         }
348
349         bool CheckPass(LocalUser* user)
350         {
351                 if(IsValidHost(user->password))
352                 {
353                         HandleIdentOrPass(user, user->password, true);
354                         user->password.clear();
355                         return true;
356                 }
357
358                 return false;
359         }
360
361         bool CheckIdent(LocalUser* user)
362         {
363                 const char* ident;
364                 in_addr newip;
365
366                 if (user->ident.length() == 8)
367                         ident = user->ident.c_str();
368                 else if (user->ident.length() == 9 && user->ident[0] == '~')
369                         ident = user->ident.c_str() + 1;
370                 else
371                         return false;
372
373                 errno = 0;
374                 unsigned long ipaddr = strtoul(ident, NULL, 16);
375                 if (errno)
376                         return false;
377                 newip.s_addr = htonl(ipaddr);
378                 std::string newipstr(inet_ntoa(newip));
379
380                 user->ident = "~cgiirc";
381                 HandleIdentOrPass(user, newipstr, false);
382
383                 return true;
384         }
385
386         bool IsValidHost(const std::string &host)
387         {
388                 if(!host.size() || host.size() > 64)
389                         return false;
390
391                 for(unsigned int i = 0; i < host.size(); i++)
392                 {
393                         if(     ((host[i] >= '0') && (host[i] <= '9')) ||
394                                         ((host[i] >= 'A') && (host[i] <= 'Z')) ||
395                                         ((host[i] >= 'a') && (host[i] <= 'z')) ||
396                                         ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
397                                         ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
398
399                                 continue;
400                         else
401                                 return false;
402                 }
403
404                 return true;
405         }
406
407         virtual Version GetVersion()
408         {
409                 return Version("Change user's hosts connecting from known CGI:IRC hosts",VF_VENDOR);
410         }
411
412 };
413
414 MODULE_INIT(ModuleCgiIRC)