]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cgiirc.cpp
53fdb6ff7fc6f6bc8478aac01d1a6a2a5f62e32c
[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         // XXX why is inspircd declared here? does class command not have one?
57         InspIRCd* Me;
58         CGIHostlist Hosts;
59         bool notify;
60         public:
61                 CommandWebirc(InspIRCd* iMe, CGIHostlist &cHosts, bool bnotify) : Command(iMe, "WEBIRC", 0, 4, true), Hosts(cHosts), notify(bnotify)
62                 {
63                         this->source = "m_cgiirc.so";
64                         this->syntax = "password client hostname ip";
65                 }
66                 CmdResult Handle(const char** parameters, int pcnt, User *user)
67                 {
68                         if(user->registered == REG_ALL)
69                                 return CMD_FAILURE;
70                         
71                         for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
72                         {
73                                 if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
74                                 {
75                                         if(iter->type == WEBIRC && parameters[0] == iter->password)
76                                         {
77                                                 user->Extend("cgiirc_realhost", new std::string(user->host));
78                                                 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
79                                                 if (notify)
80                                                         ServerInstance->SNO->WriteToSnoMask('A', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from %s", user->nick, user->host, parameters[2], user->host);
81                                                 user->Extend("cgiirc_webirc_hostname", new std::string(parameters[2]));
82                                                 user->Extend("cgiirc_webirc_ip", new std::string(parameters[3]));
83                                                 return CMD_LOCALONLY;
84                                         }
85                                 }
86                         }
87                         return CMD_FAILURE;
88                 }
89 };
90
91
92 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
93  */
94 class CGIResolver : public Resolver
95 {
96         std::string typ;
97         int theirfd;
98         User* them;
99         bool notify;
100  public:
101         CGIResolver(Module* me, InspIRCd* Instance, bool NotifyOpers, const std::string &source, bool forward, User* u, int userfd, const std::string &type, bool &cached)
102                 : Resolver(Instance, source, forward ? DNS_QUERY_A : DNS_QUERY_PTR4, cached, me), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
103
104         virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached, int resultnum = 0)
105         {
106                 if (resultnum)
107                         return;
108
109                 /* Check the user still exists */
110                 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
111                 {
112                         if (notify)
113                                 ServerInstance->SNO->WriteToSnoMask('A', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from %s", them->nick, them->host, result.c_str(), typ.c_str());
114
115                         strlcpy(them->host, result.c_str(), 63);
116                         strlcpy(them->dhost, result.c_str(), 63);
117                         strlcpy(them->ident, "~cgiirc", 8);
118                         them->InvalidateCache();
119                 }
120         }
121
122         virtual void OnError(ResolverError e, const std::string &errormessage)
123         {
124                 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
125                 {
126                         if (notify)
127                                 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, them->host,typ.c_str());
128                 }
129         }
130
131         virtual ~CGIResolver()
132         {
133         }
134 };
135
136 class ModuleCgiIRC : public Module
137 {
138         CommandWebirc* mycommand;
139         bool NotifyOpers;
140         CGIHostlist Hosts;
141 public:
142         ModuleCgiIRC(InspIRCd* Me) : Module(Me)
143         {
144                 
145                 OnRehash(NULL,"");
146                 mycommand = new CommandWebirc(Me, Hosts, NotifyOpers);
147                 ServerInstance->AddCommand(mycommand);
148
149                 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnCleanup, I_OnSyncUserMetaData, I_OnDecodeMetaData, I_OnUserDisconnect, I_OnUserConnect };
150                 ServerInstance->Modules->Attach(eventlist, this, 7);
151         }
152
153         
154         virtual void Prioritize()
155         {
156                 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIO_FIRST);
157         }
158
159         virtual void OnRehash(User* user, const std::string &parameter)
160         {
161                 ConfigReader Conf(ServerInstance);
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->Log(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->Log(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                                 }
272                                 else if(iter->type == PASSFIRST && !CheckPass(user))
273                                 {
274                                         // If the password lookup failed, try the ident
275                                         CheckIdent(user);       // If this fails too, do nothing
276                                 }
277                                 else if(iter->type == IDENT)
278                                 {
279                                         CheckIdent(user); // Nothing on failure.
280                                 }
281                                 else if(iter->type == IDENTFIRST && !CheckIdent(user))
282                                 {
283                                         // If the ident lookup fails, try the password.
284                                         CheckPass(user);
285                                 }
286                                 else if(iter->type == WEBIRC)
287                                 {
288                                         // We don't need to do anything here
289                                 }
290                                 return 0;
291                         }
292                 }
293                 return 0;
294         }
295
296         virtual void OnUserConnect(User* user)
297         {
298                 std::string *webirc_hostname, *webirc_ip;
299                 if(user->GetExt("cgiirc_webirc_hostname", webirc_hostname))
300                 {
301                         strlcpy(user->host,webirc_hostname->c_str(),63);
302                         strlcpy(user->dhost,webirc_hostname->c_str(),63);
303                         delete webirc_hostname;
304                         user->InvalidateCache();
305                         user->Shrink("cgiirc_webirc_hostname");
306                 }
307                 if(user->GetExt("cgiirc_webirc_ip", webirc_ip))
308                 {
309                         bool valid=false;
310                         ServerInstance->Users->RemoveCloneCounts(user);
311 #ifdef IPV6
312                         valid = (inet_pton(AF_INET6, webirc_ip->c_str(), &((sockaddr_in6*)user->ip)->sin6_addr) > 0); 
313
314                         if(!valid)
315                                 valid = (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr));
316 #else
317                         if (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr))
318                                 valid = true;
319 #endif
320
321                         delete webirc_ip;
322                         user->InvalidateCache();
323                         user->Shrink("cgiirc_webirc_ip");
324                         ServerInstance->Users->AddLocalClone(user);
325                         ServerInstance->Users->AddGlobalClone(user);
326                         user->CheckClass();
327                 }
328         }
329
330         bool CheckPass(User* user)
331         {
332                 if(IsValidHost(user->password))
333                 {
334                         user->Extend("cgiirc_realhost", new std::string(user->host));
335                         user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
336                         strlcpy(user->host, user->password, 64);
337                         strlcpy(user->dhost, user->password, 64);
338                         user->InvalidateCache();
339
340                         bool valid = false;
341                         ServerInstance->Users->RemoveCloneCounts(user);
342 #ifdef IPV6
343                         if (user->GetProtocolFamily() == AF_INET6)
344                                 valid = (inet_pton(AF_INET6, user->password, &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
345                         else
346                                 valid = (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr));
347 #else
348                         if (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr))
349                                 valid = true;
350 #endif
351                         ServerInstance->Users->AddLocalClone(user);
352                         ServerInstance->Users->AddGlobalClone(user);
353                         user->CheckClass();
354
355                         if (valid)
356                         {
357                                 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
358                                 if(NotifyOpers)
359                                         ServerInstance->SNO->WriteToSnoMask('A', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);
360                         }
361                         else
362                         {
363                                 /* We got as resolved hostname in the password. */
364                                 try
365                                 {
366
367                                         bool cached;
368                                         CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
369                                         ServerInstance->AddResolver(r, cached);
370                                 }
371                                 catch (...)
372                                 {
373                                         if (NotifyOpers)
374                                                 ServerInstance->SNO->WriteToSnoMask('A', "Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
375                                 }
376                         }
377                         
378                         *user->password = 0;
379
380                         /*if(NotifyOpers)
381                                 ServerInstance->SNO->WriteToSnoMask('A', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);*/
382
383                         return true;
384                 }
385                 
386                 return false;
387         }
388         
389         bool CheckIdent(User* user)
390         {
391                 int ip[4];
392                 char* ident;
393                 char newip[16];
394                 int len = strlen(user->ident);
395                 
396                 if(len == 8)
397                         ident = user->ident;
398                 else if(len == 9 && *user->ident == '~')
399                         ident = user->ident+1;
400                 else
401                         return false;
402         
403                 for(int i = 0; i < 4; i++)
404                         if(!HexToInt(ip[i], ident + i*2))
405                                 return false;
406
407                 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
408                         
409                 user->Extend("cgiirc_realhost", new std::string(user->host));
410                 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
411                 ServerInstance->Users->RemoveCloneCounts(user);
412 #ifdef IPV6
413                 if (user->GetProtocolFamily() == AF_INET6)
414                         inet_pton(AF_INET6, newip, &((sockaddr_in6*)user->ip)->sin6_addr);
415                 else
416 #endif
417                 inet_aton(newip, &((sockaddr_in*)user->ip)->sin_addr);
418                 ServerInstance->Users->AddLocalClone(user);
419                 ServerInstance->Users->AddGlobalClone(user);
420                 user->CheckClass();
421                 try
422                 {
423                         strlcpy(user->host, newip, 16);
424                         strlcpy(user->dhost, newip, 16);
425                         strlcpy(user->ident, "~cgiirc", 8);
426
427                         bool cached;
428                         CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newip, false, user, user->GetFd(), "IDENT", cached);
429                         ServerInstance->AddResolver(r, cached);
430                 }
431                 catch (...)
432                 {
433                         strlcpy(user->host, newip, 16);
434                         strlcpy(user->dhost, newip, 16);
435                         strlcpy(user->ident, "~cgiirc", 8);
436                         user->InvalidateCache();
437
438                         if(NotifyOpers)
439                                  ServerInstance->SNO->WriteToSnoMask('A', "Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
440                 }
441                 /*strlcpy(user->host, newip, 16);
442                 strlcpy(user->dhost, newip, 16);
443                 strlcpy(user->ident, "~cgiirc", 8);*/
444
445                 return true;
446         }
447         
448         bool IsValidHost(const std::string &host)
449         {
450                 if(!host.size())
451                         return false;
452         
453                 for(unsigned int i = 0; i < host.size(); i++)
454                 {
455                         if(     ((host[i] >= '0') && (host[i] <= '9')) ||
456                                         ((host[i] >= 'A') && (host[i] <= 'Z')) ||
457                                         ((host[i] >= 'a') && (host[i] <= 'z')) ||
458                                         ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
459                                         ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
460                                         
461                                 continue;
462                         else
463                                 return false;
464                 }
465                 
466                 return true;
467         }
468
469         bool IsValidIP(const std::string &ip)
470         {
471                 if(ip.size() < 7 || ip.size() > 15)
472                         return false;
473         
474                 short sincedot = 0;
475                 short dots = 0;
476         
477                 for(unsigned int i = 0; i < ip.size(); i++)
478                 {
479                         if((dots <= 3) && (sincedot <= 3))
480                         {
481                                 if((ip[i] >= '0') && (ip[i] <= '9'))
482                                 {
483                                         sincedot++;
484                                 }
485                                 else if(ip[i] == '.')
486                                 {
487                                         sincedot = 0;
488                                         dots++;
489                                 }
490                         }
491                         else
492                         {
493                                 return false;
494                         
495                         }
496                 }
497                 
498                 if(dots != 3)
499                         return false;
500                 
501                 return true;
502         }
503         
504         bool HexToInt(int &out, const char* in)
505         {
506                 char ip[3];
507                 ip[0] = in[0];
508                 ip[1] = in[1];
509                 ip[2] = 0;
510                 out = strtol(ip, NULL, 16);
511                 
512                 if(out > 255 || out < 0)
513                         return false;
514
515                 return true;
516         }
517         
518         virtual ~ModuleCgiIRC()
519         {
520         }
521          
522         virtual Version GetVersion()
523         {
524                 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
525         }
526         
527 };
528
529 MODULE_INIT(ModuleCgiIRC)