]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cgiirc.cpp
45447148bcde234da90f29ed3708f7631eaf9c26
[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         int theirfd;
106         LocalUser* them;
107         bool notify;
108  public:
109         CGIResolver(Module* me, bool NotifyOpers, const std::string &source, bool forward, LocalUser* u, int userfd, const std::string &type, bool &cached)
110                 : Resolver(source, forward ? DNS_QUERY_A : DNS_QUERY_PTR4, cached, me), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
111
112         virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
113         {
114                 /* Check the user still exists */
115                 if ((them) && (&them->eh == ServerInstance->SE->GetRef(theirfd)))
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                         them->host.assign(result,0, 64);
121                         them->dhost.assign(result, 0, 64);
122                         them->InvalidateCache();
123                         them->CheckLines(true);
124                 }
125         }
126
127         virtual void OnError(ResolverError e, const std::string &errormessage)
128         {
129                 if ((them) && (&them->eh == ServerInstance->SE->GetRef(theirfd)))
130                 {
131                         if (notify)
132                                 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());
133                 }
134         }
135
136         virtual ~CGIResolver()
137         {
138         }
139 };
140
141 class ModuleCgiIRC : public Module
142 {
143         CommandWebirc cmd;
144         bool NotifyOpers;
145 public:
146         ModuleCgiIRC() : cmd(this, NotifyOpers)
147         {
148                 OnRehash(NULL);
149                 ServerInstance->AddCommand(&cmd);
150                 ServerInstance->Extensions.Register(&cmd.realhost);
151                 ServerInstance->Extensions.Register(&cmd.realip);
152                 ServerInstance->Extensions.Register(&cmd.webirc_hostname);
153                 ServerInstance->Extensions.Register(&cmd.webirc_ip);
154
155                 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnDecodeMetaData, I_OnUserDisconnect, I_OnUserConnect };
156                 ServerInstance->Modules->Attach(eventlist, this, 5);
157         }
158
159
160         virtual void Prioritize()
161         {
162                 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIORITY_FIRST);
163         }
164
165         virtual void OnRehash(User* user)
166         {
167                 ConfigReader Conf;
168                 cmd.Hosts.clear();
169
170                 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
171
172                 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
173                         NotifyOpers = true;
174
175                 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
176                 {
177                         std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
178                         std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
179                         std::string password = Conf.ReadValue("cgihost", "password", i);
180
181                         if(hostmask.length())
182                         {
183                                 if (type == "webirc" && !password.length()) {
184                                                 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
185                                 }
186                                 else
187                                 {
188                                         CGItype cgitype = INVALID;
189                                         if (type == "pass")
190                                                 cgitype = PASS;
191                                         else if (type == "ident")
192                                                 cgitype = IDENT;
193                                         else if (type == "passfirst")
194                                                 cgitype = PASSFIRST;
195                                         else if (type == "webirc")
196                                         {
197                                                 cgitype = WEBIRC;
198                                         }
199
200                                         if (cgitype == INVALID)
201                                                 cgitype = PASS;
202
203                                         cmd.Hosts.push_back(CGIhost(hostmask,cgitype, password.length() ? password : "" ));
204                                 }
205                         }
206                         else
207                         {
208                                 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
209                                 continue;
210                         }
211                 }
212         }
213
214         virtual ModResult OnUserRegister(LocalUser* user)
215         {
216                 for(CGIHostlist::iterator iter = cmd.Hosts.begin(); iter != cmd.Hosts.end(); iter++)
217                 {
218                         if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
219                         {
220                                 // Deal with it...
221                                 if(iter->type == PASS)
222                                 {
223                                         CheckPass(user); // We do nothing if it fails so...
224                                         user->CheckLines(true);
225                                 }
226                                 else if(iter->type == PASSFIRST && !CheckPass(user))
227                                 {
228                                         // If the password lookup failed, try the ident
229                                         CheckIdent(user);       // If this fails too, do nothing
230                                         user->CheckLines(true);
231                                 }
232                                 else if(iter->type == IDENT)
233                                 {
234                                         CheckIdent(user); // Nothing on failure.
235                                         user->CheckLines(true);
236                                 }
237                                 else if(iter->type == IDENTFIRST && !CheckIdent(user))
238                                 {
239                                         // If the ident lookup fails, try the password.
240                                         CheckPass(user);
241                                         user->CheckLines(true);
242                                 }
243                                 else if(iter->type == WEBIRC)
244                                 {
245                                         // We don't need to do anything here
246                                 }
247                                 return MOD_RES_PASSTHRU;
248                         }
249                 }
250                 return MOD_RES_PASSTHRU;
251         }
252
253         virtual void OnUserConnect(LocalUser* user)
254         {
255                 std::string *webirc_hostname = cmd.webirc_hostname.get(user);
256                 std::string *webirc_ip = cmd.webirc_ip.get(user);
257                 if (webirc_hostname)
258                 {
259                         user->host.assign(*webirc_hostname, 0, 64);
260                         user->dhost.assign(*webirc_hostname, 0, 64);
261                         user->InvalidateCache();
262                         cmd.webirc_hostname.unset(user);
263                 }
264                 if (webirc_ip)
265                 {
266                         ServerInstance->Users->RemoveCloneCounts(user);
267                         user->SetClientIP(webirc_ip->c_str());
268                         user->InvalidateCache();
269                         cmd.webirc_ip.unset(user);
270                         ServerInstance->Users->AddLocalClone(user);
271                         ServerInstance->Users->AddGlobalClone(user);
272                         user->SetClass();
273                         user->CheckClass();
274                         user->CheckLines(true);
275                 }
276         }
277
278         bool CheckPass(LocalUser* user)
279         {
280                 if(IsValidHost(user->password))
281                 {
282                         cmd.realhost.set(user, user->host);
283                         cmd.realip.set(user, user->GetIPString());
284                         user->host.assign(user->password, 0, 64);
285                         user->dhost.assign(user->password, 0, 64);
286                         user->InvalidateCache();
287
288                         ServerInstance->Users->RemoveCloneCounts(user);
289                         user->SetClientIP(user->password.c_str());
290                         ServerInstance->Users->AddLocalClone(user);
291                         ServerInstance->Users->AddGlobalClone(user);
292                         user->SetClass();
293                         user->CheckClass();
294
295                         try
296                         {
297
298                                 bool cached;
299                                 CGIResolver* r = new CGIResolver(this, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
300                                 ServerInstance->AddResolver(r, cached);
301                         }
302                         catch (...)
303                         {
304                                 if (NotifyOpers)
305                                         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());
306                         }
307
308                         user->password.clear();
309                         return true;
310                 }
311
312                 return false;
313         }
314
315         bool CheckIdent(LocalUser* user)
316         {
317                 const char* ident;
318                 int len = user->ident.length();
319                 in_addr newip;
320
321                 if(len == 8)
322                         ident = user->ident.c_str();
323                 else if(len == 9 && user->ident[0] == '~')
324                         ident = user->ident.c_str() + 1;
325                 else
326                         return false;
327
328                 errno = 0;
329                 unsigned long ipaddr = strtoul(ident, NULL, 16);
330                 if (errno)
331                         return false;
332                 newip.s_addr = htonl(ipaddr);
333                 char* newipstr = inet_ntoa(newip);
334
335                 cmd.realhost.set(user, user->host);
336                 cmd.realip.set(user, user->GetIPString());
337                 ServerInstance->Users->RemoveCloneCounts(user);
338                 user->SetClientIP(newipstr);
339                 ServerInstance->Users->AddLocalClone(user);
340                 ServerInstance->Users->AddGlobalClone(user);
341                 user->SetClass();
342                 user->CheckClass();
343                 user->host = newipstr;
344                 user->dhost = newipstr;
345                 user->ident.assign("~cgiirc", 0, 8);
346                 try
347                 {
348
349                         bool cached;
350                         CGIResolver* r = new CGIResolver(this, NotifyOpers, newipstr, false, user, user->GetFd(), "IDENT", cached);
351                         ServerInstance->AddResolver(r, cached);
352                 }
353                 catch (...)
354                 {
355                         user->InvalidateCache();
356
357                         if(NotifyOpers)
358                                  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());
359                 }
360
361                 return true;
362         }
363
364         bool IsValidHost(const std::string &host)
365         {
366                 if(!host.size())
367                         return false;
368
369                 for(unsigned int i = 0; i < host.size(); i++)
370                 {
371                         if(     ((host[i] >= '0') && (host[i] <= '9')) ||
372                                         ((host[i] >= 'A') && (host[i] <= 'Z')) ||
373                                         ((host[i] >= 'a') && (host[i] <= 'z')) ||
374                                         ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
375                                         ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
376
377                                 continue;
378                         else
379                                 return false;
380                 }
381
382                 return true;
383         }
384
385         bool IsValidIP(const std::string &ip)
386         {
387                 if(ip.size() < 7 || ip.size() > 15)
388                         return false;
389
390                 short sincedot = 0;
391                 short dots = 0;
392
393                 for(unsigned int i = 0; i < ip.size(); i++)
394                 {
395                         if((dots <= 3) && (sincedot <= 3))
396                         {
397                                 if((ip[i] >= '0') && (ip[i] <= '9'))
398                                 {
399                                         sincedot++;
400                                 }
401                                 else if(ip[i] == '.')
402                                 {
403                                         sincedot = 0;
404                                         dots++;
405                                 }
406                         }
407                         else
408                         {
409                                 return false;
410
411                         }
412                 }
413
414                 if(dots != 3)
415                         return false;
416
417                 return true;
418         }
419
420         virtual ~ModuleCgiIRC()
421         {
422         }
423
424         virtual Version GetVersion()
425         {
426                 return Version("Change user's hosts connecting from known CGI:IRC hosts",VF_VENDOR);
427         }
428
429 };
430
431 MODULE_INIT(ModuleCgiIRC)