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