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