]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cgiirc.cpp
a9abb4b15d69a7a0885a48c7cab2806dbfa4875c
[user/henk/code/inspircd.git] / src / modules / m_cgiirc.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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         CGIHostlist Hosts;
57         bool notify;
58         public:
59                 CommandWebirc(InspIRCd* Instance, CGIHostlist &cHosts, bool bnotify) : Command(Instance, "WEBIRC", 0, 4, true), Hosts(cHosts), notify(bnotify)
60                 {
61                         this->source = "m_cgiirc.so";
62                         this->syntax = "password client hostname ip";
63                 }
64                 CmdResult Handle(const std::vector<std::string> &parameters, User *user)
65                 {
66                         if(user->registered == REG_ALL)
67                                 return CMD_FAILURE;
68
69                         for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
70                         {
71                                 if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
72                                 {
73                                         if(iter->type == WEBIRC && parameters[0] == iter->password)
74                                         {
75                                                 user->Extend("cgiirc_realhost", new std::string(user->host));
76                                                 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
77                                                 if (notify)
78                                                         ServerInstance->SNO->WriteToSnoMask('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());
79                                                 user->Extend("cgiirc_webirc_hostname", new std::string(parameters[2]));
80                                                 user->Extend("cgiirc_webirc_ip", new std::string(parameters[3]));
81                                                 return CMD_LOCALONLY;
82                                         }
83                                 }
84                         }
85                         return CMD_FAILURE;
86                 }
87 };
88
89
90 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
91  */
92 class CGIResolver : public Resolver
93 {
94         std::string typ;
95         int theirfd;
96         User* them;
97         bool notify;
98  public:
99         CGIResolver(Module* me, InspIRCd* Instance, bool NotifyOpers, const std::string &source, bool forward, User* u, int userfd, const std::string &type, bool &cached)
100                 : Resolver(Instance, source, forward ? DNS_QUERY_A : DNS_QUERY_PTR4, cached, me), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
101
102         virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached, int resultnum = 0)
103         {
104                 if (resultnum)
105                         return;
106
107                 /* Check the user still exists */
108                 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
109                 {
110                         if (notify)
111                                 ServerInstance->SNO->WriteToSnoMask('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());
112
113                         them->host.assign(result,0, 64);
114                         them->dhost.assign(result, 0, 64);
115                         them->ident.assign("~cgiirc", 0, 8);
116                         them->InvalidateCache();
117                         them->CheckLines();
118                 }
119         }
120
121         virtual void OnError(ResolverError e, const std::string &errormessage)
122         {
123                 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
124                 {
125                         if (notify)
126                                 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());
127                 }
128         }
129
130         virtual ~CGIResolver()
131         {
132         }
133 };
134
135 class ModuleCgiIRC : public Module
136 {
137         CommandWebirc* mycommand;
138         bool NotifyOpers;
139         CGIHostlist Hosts;
140 public:
141         ModuleCgiIRC(InspIRCd* Me) : Module(Me)
142         {
143
144                 OnRehash(NULL,"");
145                 mycommand = new CommandWebirc(Me, Hosts, NotifyOpers);
146                 ServerInstance->AddCommand(mycommand);
147
148                 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnCleanup, I_OnSyncUserMetaData, I_OnDecodeMetaData, I_OnUserDisconnect, I_OnUserConnect };
149                 ServerInstance->Modules->Attach(eventlist, this, 7);
150         }
151
152
153         virtual void Prioritize()
154         {
155                 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIO_FIRST);
156         }
157
158         virtual void OnRehash(User* user, const std::string &parameter)
159         {
160                 ConfigReader Conf(ServerInstance);
161                 Hosts.clear();
162
163                 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
164
165                 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
166                         NotifyOpers = true;
167
168                 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
169                 {
170                         std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
171                         std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
172                         std::string password = Conf.ReadValue("cgihost", "password", i);
173
174                         if(hostmask.length())
175                         {
176                                 if (type == "webirc" && !password.length()) {
177                                                 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
178                                 }
179                                 else
180                                 {
181                                         CGItype cgitype = INVALID;
182                                         if (type == "pass")
183                                                 cgitype = PASS;
184                                         else if (type == "ident")
185                                                 cgitype = IDENT;
186                                         else if (type == "passfirst")
187                                                 cgitype = PASSFIRST;
188                                         else if (type == "webirc")
189                                         {
190                                                 cgitype = WEBIRC;
191                                         }
192
193                                         if (cgitype == INVALID)
194                                                 cgitype = PASS;
195
196                                         Hosts.push_back(CGIhost(hostmask,cgitype, password.length() ? password : "" ));
197                                 }
198                         }
199                         else
200                         {
201                                 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
202                                 continue;
203                         }
204                 }
205         }
206
207         virtual void OnCleanup(int target_type, void* item)
208         {
209                 if(target_type == TYPE_USER)
210                 {
211                         User* user = (User*)item;
212                         std::string* realhost;
213                         std::string* realip;
214
215                         if(user->GetExt("cgiirc_realhost", realhost))
216                         {
217                                 delete realhost;
218                                 user->Shrink("cgiirc_realhost");
219                         }
220
221                         if(user->GetExt("cgiirc_realip", realip))
222                         {
223                                 delete realip;
224                                 user->Shrink("cgiirc_realip");
225                         }
226                 }
227         }
228
229         virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
230         {
231                 if((extname == "cgiirc_realhost") || (extname == "cgiirc_realip"))
232                 {
233                         std::string* data;
234
235                         if(user->GetExt(extname, data))
236                         {
237                                 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *data);
238                         }
239                 }
240         }
241
242         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
243         {
244                 if(target_type == TYPE_USER)
245                 {
246                         User* dest = (User*)target;
247                         std::string* bleh;
248                         if(((extname == "cgiirc_realhost") || (extname == "cgiirc_realip")) && (!dest->GetExt(extname, bleh)))
249                         {
250                                 dest->Extend(extname, new std::string(extdata));
251                         }
252                 }
253         }
254
255         virtual void OnUserDisconnect(User* user)
256         {
257                 OnCleanup(TYPE_USER, user);
258         }
259
260
261         virtual int OnUserRegister(User* user)
262         {
263                 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
264                 {
265                         if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
266                         {
267                                 // Deal with it...
268                                 if(iter->type == PASS)
269                                 {
270                                         CheckPass(user); // We do nothing if it fails so...
271                                         user->CheckLines();
272                                 }
273                                 else if(iter->type == PASSFIRST && !CheckPass(user))
274                                 {
275                                         // If the password lookup failed, try the ident
276                                         CheckIdent(user);       // If this fails too, do nothing
277                                         user->CheckLines();
278                                 }
279                                 else if(iter->type == IDENT)
280                                 {
281                                         CheckIdent(user); // Nothing on failure.
282                                         user->CheckLines();
283                                 }
284                                 else if(iter->type == IDENTFIRST && !CheckIdent(user))
285                                 {
286                                         // If the ident lookup fails, try the password.
287                                         CheckPass(user);
288                                         user->CheckLines();
289                                 }
290                                 else if(iter->type == WEBIRC)
291                                 {
292                                         // We don't need to do anything here
293                                 }
294                                 return 0;
295                         }
296                 }
297                 return 0;
298         }
299
300         virtual void OnUserConnect(User* user)
301         {
302                 std::string *webirc_hostname, *webirc_ip;
303                 if(user->GetExt("cgiirc_webirc_hostname", webirc_hostname))
304                 {
305                         user->host.assign(*webirc_hostname, 0, 64);
306                         user->dhost.assign(*webirc_hostname, 0, 64);
307                         delete webirc_hostname;
308                         user->InvalidateCache();
309                         user->Shrink("cgiirc_webirc_hostname");
310                 }
311                 if(user->GetExt("cgiirc_webirc_ip", webirc_ip))
312                 {
313                         bool valid=false;
314                         ServerInstance->Users->RemoveCloneCounts(user);
315 #ifdef IPV6
316                         valid = (inet_pton(AF_INET6, webirc_ip->c_str(), &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
317
318                         if(!valid)
319                                 valid = (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr));
320 #else
321                         if (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr))
322                                 valid = true;
323 #endif
324
325                         delete webirc_ip;
326                         user->InvalidateCache();
327                         user->Shrink("cgiirc_webirc_ip");
328                         ServerInstance->Users->AddLocalClone(user);
329                         ServerInstance->Users->AddGlobalClone(user);
330                         user->CheckClass();
331                         user->CheckLines();
332                 }
333         }
334
335         bool CheckPass(User* user)
336         {
337                 if(IsValidHost(user->password))
338                 {
339                         user->Extend("cgiirc_realhost", new std::string(user->host));
340                         user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
341                         user->host.assign(user->password, 0, 64);
342                         user->dhost.assign(user->password, 0, 64);
343                         user->InvalidateCache();
344
345                         bool valid = false;
346                         ServerInstance->Users->RemoveCloneCounts(user);
347 #ifdef IPV6
348                         if (user->GetProtocolFamily() == AF_INET6)
349                                 valid = (inet_pton(AF_INET6, user->password.c_str(), &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
350                         else
351                                 valid = (inet_aton(user->password.c_str(), &((sockaddr_in*)user->ip)->sin_addr));
352 #else
353                         if (inet_aton(user->password.c_str(), &((sockaddr_in*)user->ip)->sin_addr))
354                                 valid = true;
355 #endif
356                         ServerInstance->Users->AddLocalClone(user);
357                         ServerInstance->Users->AddGlobalClone(user);
358                         user->CheckClass();
359
360                         if (valid)
361                         {
362                                 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
363                                 if(NotifyOpers)
364                                         ServerInstance->SNO->WriteToSnoMask('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());
365                         }
366                         else
367                         {
368                                 /* We got as resolved hostname in the password. */
369                                 try
370                                 {
371
372                                         bool cached;
373                                         CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
374                                         ServerInstance->AddResolver(r, cached);
375                                 }
376                                 catch (...)
377                                 {
378                                         if (NotifyOpers)
379                                                 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());
380                                 }
381                         }
382
383                         user->password.clear();
384                         return true;
385                 }
386
387                 return false;
388         }
389
390         bool CheckIdent(User* user)
391         {
392                 int ip[4];
393                 const char* ident;
394                 char newip[16];
395                 int len = user->ident.length();
396
397                 if(len == 8)
398                         ident = user->ident.c_str();
399                 else if(len == 9 && user->ident[0] == '~')
400                         ident = user->ident.c_str() + 1;
401                 else
402                         return false;
403
404                 for(int i = 0; i < 4; i++)
405                         if(!HexToInt(ip[i], ident + i*2))
406                                 return false;
407
408                 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
409
410                 user->Extend("cgiirc_realhost", new std::string(user->host));
411                 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
412                 ServerInstance->Users->RemoveCloneCounts(user);
413 #ifdef IPV6
414                 if (user->GetProtocolFamily() == AF_INET6)
415                         inet_pton(AF_INET6, newip, &((sockaddr_in6*)user->ip)->sin6_addr);
416                 else
417 #endif
418                 inet_aton(newip, &((sockaddr_in*)user->ip)->sin_addr);
419                 ServerInstance->Users->AddLocalClone(user);
420                 ServerInstance->Users->AddGlobalClone(user);
421                 user->CheckClass();
422                 try
423                 {
424                         user->host.assign(newip, 0, 16);
425                         user->dhost.assign(newip, 0, 16);
426                         user->ident.assign("~cgiirc", 0, 8);
427
428                         bool cached;
429                         CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newip, false, user, user->GetFd(), "IDENT", cached);
430                         ServerInstance->AddResolver(r, cached);
431                 }
432                 catch (...)
433                 {
434                         user->host.assign(newip, 0, 16);
435                         user->dhost.assign(newip, 0, 16);
436                         user->ident.assign("~cgiirc", 0, 8);
437                         user->InvalidateCache();
438
439                         if(NotifyOpers)
440                                  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());
441                 }
442
443                 return true;
444         }
445
446         bool IsValidHost(const std::string &host)
447         {
448                 if(!host.size())
449                         return false;
450
451                 for(unsigned int i = 0; i < host.size(); i++)
452                 {
453                         if(     ((host[i] >= '0') && (host[i] <= '9')) ||
454                                         ((host[i] >= 'A') && (host[i] <= 'Z')) ||
455                                         ((host[i] >= 'a') && (host[i] <= 'z')) ||
456                                         ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
457                                         ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
458
459                                 continue;
460                         else
461                                 return false;
462                 }
463
464                 return true;
465         }
466
467         bool IsValidIP(const std::string &ip)
468         {
469                 if(ip.size() < 7 || ip.size() > 15)
470                         return false;
471
472                 short sincedot = 0;
473                 short dots = 0;
474
475                 for(unsigned int i = 0; i < ip.size(); i++)
476                 {
477                         if((dots <= 3) && (sincedot <= 3))
478                         {
479                                 if((ip[i] >= '0') && (ip[i] <= '9'))
480                                 {
481                                         sincedot++;
482                                 }
483                                 else if(ip[i] == '.')
484                                 {
485                                         sincedot = 0;
486                                         dots++;
487                                 }
488                         }
489                         else
490                         {
491                                 return false;
492
493                         }
494                 }
495
496                 if(dots != 3)
497                         return false;
498
499                 return true;
500         }
501
502         bool HexToInt(int &out, const char* in)
503         {
504                 char ip[3];
505                 ip[0] = in[0];
506                 ip[1] = in[1];
507                 ip[2] = 0;
508                 out = strtol(ip, NULL, 16);
509
510                 if(out > 255 || out < 0)
511                         return false;
512
513                 return true;
514         }
515
516         virtual ~ModuleCgiIRC()
517         {
518         }
519
520         virtual Version GetVersion()
521         {
522                 return Version(1,2,0,0,VF_VENDOR,API_VERSION);
523         }
524
525 };
526
527 MODULE_INIT(ModuleCgiIRC)