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