1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
24 /* $ModDesc: Change user's hosts connecting from known CGI:IRC hosts */
26 enum CGItype { INVALID, PASS, IDENT, PASSFIRST, IDENTFIRST, WEBIRC };
29 /** Holds a CGI site's details
31 class CGIhost : public classbase
38 CGIhost(const std::string &mask = "", CGItype t = IDENTFIRST, const std::string &password ="")
39 : hostmask(mask), type(t), password(password)
43 typedef std::vector<CGIhost> CGIHostlist;
45 class cmd_webirc : public command_t
51 cmd_webirc(InspIRCd* Me, CGIHostlist &Hosts, bool notify) : command_t(Me, "WEBIRC", 0, 4, true), Hosts(Hosts), notify(notify)
53 this->source = "m_cgiirc.so";
54 this->syntax = "password client hostname ip";
56 CmdResult Handle(const char** parameters, int pcnt, userrec *user)
58 if(user->registered == REG_ALL)
61 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
63 if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
65 if(iter->type == WEBIRC && parameters[0] == iter->password)
67 user->Extend("cgiirc_realhost", new std::string(user->host));
68 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
70 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from %s", user->nick, user->host, parameters[2], user->host);
71 user->Extend("cgiirc_webirc_hostname", new std::string(parameters[2]));
72 user->Extend("cgiirc_webirc_ip", new std::string(parameters[3]));
82 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
84 class CGIResolver : public Resolver
91 CGIResolver(Module* me, InspIRCd* ServerInstance, bool NotifyOpers, const std::string &source, bool forward, userrec* u, int userfd, const std::string &type, bool &cached)
92 : Resolver(ServerInstance, source, forward ? DNS_QUERY_A : DNS_QUERY_PTR4, cached, me), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
94 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
96 /* Check the user still exists */
97 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
100 ServerInstance->WriteOpers("*** 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());
102 strlcpy(them->host, result.c_str(), 63);
103 strlcpy(them->dhost, result.c_str(), 63);
104 strlcpy(them->ident, "~cgiirc", 8);
105 them->InvalidateCache();
109 virtual void OnError(ResolverError e, const std::string &errormessage)
111 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
114 ServerInstance->WriteOpers("*** 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());
118 virtual ~CGIResolver()
123 class ModuleCgiIRC : public Module
125 cmd_webirc* mycommand;
129 ModuleCgiIRC(InspIRCd* Me) : Module(Me)
133 mycommand=new cmd_webirc(Me, Hosts, NotifyOpers);
134 ServerInstance->AddCommand(mycommand);
137 void Implements(char* List)
139 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCleanup] = List[I_OnSyncUserMetaData] = List[I_OnDecodeMetaData] = List[I_OnUserQuit] = List[I_OnUserConnect] = 1;
142 virtual Priority Prioritize()
144 // We want to get here before m_cloaking and m_hostchange etc
145 return PRIORITY_FIRST;
148 virtual void OnRehash(userrec* user, const std::string ¶meter)
150 ConfigReader Conf(ServerInstance);
152 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
154 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
157 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
159 std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
160 std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
161 std::string password = Conf.ReadValue("cgihost", "password", i);
163 if(hostmask.length())
165 if (type == "webirc" && !password.length()) {
166 ServerInstance->Log(DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
170 CGItype cgitype = INVALID;
173 else if (type == "ident")
175 else if (type == "passfirst")
177 else if (type == "webirc")
182 if (cgitype == INVALID)
185 Hosts.push_back(CGIhost(hostmask,cgitype, password.length() ? password : "" ));
190 ServerInstance->Log(DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
196 virtual void OnCleanup(int target_type, void* item)
198 if(target_type == TYPE_USER)
200 userrec* user = (userrec*)item;
201 std::string* realhost;
204 if(user->GetExt("cgiirc_realhost", realhost))
207 user->Shrink("cgiirc_realhost");
210 if(user->GetExt("cgiirc_realip", realip))
213 user->Shrink("cgiirc_realip");
218 virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
220 if((extname == "cgiirc_realhost") || (extname == "cgiirc_realip"))
224 if(user->GetExt(extname, data))
226 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *data);
231 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
233 if(target_type == TYPE_USER)
235 userrec* dest = (userrec*)target;
237 if(((extname == "cgiirc_realhost") || (extname == "cgiirc_realip")) && (!dest->GetExt(extname, bleh)))
239 dest->Extend(extname, new std::string(extdata));
244 virtual void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
246 OnCleanup(TYPE_USER, user);
250 virtual int OnUserRegister(userrec* user)
252 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
254 if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
257 if(iter->type == PASS)
259 CheckPass(user); // We do nothing if it fails so...
261 else if(iter->type == PASSFIRST && !CheckPass(user))
263 // If the password lookup failed, try the ident
264 CheckIdent(user); // If this fails too, do nothing
266 else if(iter->type == IDENT)
268 CheckIdent(user); // Nothing on failure.
270 else if(iter->type == IDENTFIRST && !CheckIdent(user))
272 // If the ident lookup fails, try the password.
275 else if(iter->type == WEBIRC)
277 // We don't need to do anything here
285 virtual void OnUserConnect(userrec* user)
287 std::string *webirc_hostname, *webirc_ip;
288 if(user->GetExt("cgiirc_webirc_hostname", webirc_hostname))
290 strlcpy(user->host,webirc_hostname->c_str(),63);
291 strlcpy(user->dhost,webirc_hostname->c_str(),63);
292 delete webirc_hostname;
293 user->InvalidateCache();
294 user->Shrink("cgiirc_webirc_hostname");
296 if(user->GetExt("cgiirc_webirc_ip", webirc_ip))
299 user->RemoveCloneCounts();
301 valid = (inet_pton(AF_INET6, webirc_ip->c_str(), &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
304 valid = (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr));
306 if (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr))
311 user->InvalidateCache();
312 user->Shrink("cgiirc_webirc_ip");
313 ServerInstance->AddLocalClone(user);
314 ServerInstance->AddGlobalClone(user);
319 bool CheckPass(userrec* user)
321 if(IsValidHost(user->password))
323 user->Extend("cgiirc_realhost", new std::string(user->host));
324 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
325 strlcpy(user->host, user->password, 64);
326 strlcpy(user->dhost, user->password, 64);
327 user->InvalidateCache();
330 user->RemoveCloneCounts();
332 if (user->GetProtocolFamily() == AF_INET6)
333 valid = (inet_pton(AF_INET6, user->password, &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
335 valid = (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr));
337 if (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr))
340 ServerInstance->AddLocalClone(user);
341 ServerInstance->AddGlobalClone(user);
346 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
348 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);
352 /* We got as resolved hostname in the password. */
357 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
358 ServerInstance->AddResolver(r, cached);
363 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
370 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);*/
378 bool CheckIdent(userrec* user)
383 int len = strlen(user->ident);
387 else if(len == 9 && *user->ident == '~')
388 ident = user->ident+1;
392 for(int i = 0; i < 4; i++)
393 if(!HexToInt(ip[i], ident + i*2))
396 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
398 user->Extend("cgiirc_realhost", new std::string(user->host));
399 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
400 user->RemoveCloneCounts();
402 if (user->GetProtocolFamily() == AF_INET6)
403 inet_pton(AF_INET6, newip, &((sockaddr_in6*)user->ip)->sin6_addr);
406 inet_aton(newip, &((sockaddr_in*)user->ip)->sin_addr);
407 ServerInstance->AddLocalClone(user);
408 ServerInstance->AddGlobalClone(user);
412 strlcpy(user->host, newip, 16);
413 strlcpy(user->dhost, newip, 16);
414 strlcpy(user->ident, "~cgiirc", 8);
417 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newip, false, user, user->GetFd(), "IDENT", cached);
418 ServerInstance->AddResolver(r, cached);
422 strlcpy(user->host, newip, 16);
423 strlcpy(user->dhost, newip, 16);
424 strlcpy(user->ident, "~cgiirc", 8);
425 user->InvalidateCache();
428 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
430 /*strlcpy(user->host, newip, 16);
431 strlcpy(user->dhost, newip, 16);
432 strlcpy(user->ident, "~cgiirc", 8);*/
437 bool IsValidHost(const std::string &host)
442 for(unsigned int i = 0; i < host.size(); i++)
444 if( ((host[i] >= '0') && (host[i] <= '9')) ||
445 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
446 ((host[i] >= 'a') && (host[i] <= 'z')) ||
447 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
448 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
458 bool IsValidIP(const std::string &ip)
460 if(ip.size() < 7 || ip.size() > 15)
466 for(unsigned int i = 0; i < ip.size(); i++)
468 if((dots <= 3) && (sincedot <= 3))
470 if((ip[i] >= '0') && (ip[i] <= '9'))
474 else if(ip[i] == '.')
493 bool HexToInt(int &out, const char* in)
499 out = strtol(ip, NULL, 16);
501 if(out > 255 || out < 0)
507 virtual ~ModuleCgiIRC()
511 virtual Version GetVersion()
513 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
518 MODULE_INIT(ModuleCgiIRC)