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 * ---------------------------------------------------
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
25 /* $ModDesc: Change user's hosts connecting from known CGI:IRC hosts */
27 enum CGItype { 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], "_");
71 user->Extend("cgiirc_webirc_hostname", new std::string(parameters[2]));
81 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
83 class CGIResolver : public Resolver
90 CGIResolver(Module* me, InspIRCd* ServerInstance, bool NotifyOpers, const std::string &source, bool forward, userrec* u, int userfd, const std::string &type, bool &cached)
91 : Resolver(ServerInstance, source, forward ? DNS_QUERY_FORWARD : DNS_QUERY_REVERSE, cached, me), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
93 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
95 /* Check the user still exists */
96 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
99 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());
101 strlcpy(them->host, result.c_str(), 63);
102 strlcpy(them->dhost, result.c_str(), 63);
103 strlcpy(them->ident, "~cgiirc", 8);
104 them->InvalidateCache();
108 virtual void OnError(ResolverError e, const std::string &errormessage)
110 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
113 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());
117 virtual ~CGIResolver()
122 class ModuleCgiIRC : public Module
124 cmd_webirc* mycommand;
128 ModuleCgiIRC(InspIRCd* Me) : Module::Module(Me)
132 mycommand=new cmd_webirc(Me, Hosts, NotifyOpers);
133 ServerInstance->AddCommand(mycommand);
136 void Implements(char* List)
138 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCleanup] = List[I_OnSyncUserMetaData] = List[I_OnDecodeMetaData] = List[I_OnUserQuit] = List[I_OnUserConnect] = 1;
141 virtual Priority Prioritize()
143 // We want to get here before m_cloaking and m_hostchange etc
144 return PRIORITY_FIRST;
147 virtual void OnRehash(userrec* user, const std::string ¶meter)
149 ConfigReader Conf(ServerInstance);
151 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
153 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
156 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
158 std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
159 std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
160 std::string password = Conf.ReadValue("cgihost", "password", i);
162 if(hostmask.length())
164 Hosts.push_back(CGIhost(hostmask));
167 Hosts.back().type = PASS;
168 else if(type == "ident")
169 Hosts.back().type = IDENT;
170 else if(type == "passfirst")
171 Hosts.back().type = PASSFIRST;
172 else if(type == "webirc") {
173 Hosts.back().type = WEBIRC;
174 if(password.length())
175 Hosts.back().password=password;
177 ServerInstance->Log(DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
182 ServerInstance->Log(DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
188 virtual void OnCleanup(int target_type, void* item)
190 if(target_type == TYPE_USER)
192 userrec* user = (userrec*)item;
193 std::string* realhost;
196 if(user->GetExt("cgiirc_realhost", realhost))
199 user->Shrink("cgiirc_realhost");
202 if(user->GetExt("cgiirc_realip", realip))
205 user->Shrink("cgiirc_realip");
210 virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname)
212 if((extname == "cgiirc_realhost") || (extname == "cgiirc_realip"))
216 if(user->GetExt(extname, data))
218 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *data);
223 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
225 if(target_type == TYPE_USER)
227 userrec* dest = (userrec*)target;
229 if(((extname == "cgiirc_realhost") || (extname == "cgiirc_realip")) && (!dest->GetExt(extname, bleh)))
231 dest->Extend(extname, new std::string(extdata));
236 virtual void OnUserQuit(userrec* user, const std::string &message)
238 OnCleanup(TYPE_USER, user);
242 virtual int OnUserRegister(userrec* user)
244 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
246 if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
249 if(iter->type == PASS)
251 CheckPass(user); // We do nothing if it fails so...
253 else if(iter->type == PASSFIRST && !CheckPass(user))
255 // If the password lookup failed, try the ident
256 CheckIdent(user); // If this fails too, do nothing
258 else if(iter->type == IDENT)
260 CheckIdent(user); // Nothing on failure.
262 else if(iter->type == IDENTFIRST && !CheckIdent(user))
264 // If the ident lookup fails, try the password.
267 else if(iter->type == WEBIRC)
269 // We don't need to do anything here
277 virtual void OnUserConnect(userrec* user)
279 std::string* webirc_hostname;
280 if(user->GetExt("cgiirc_webirc_hostname", webirc_hostname))
282 strlcpy(user->host,webirc_hostname->c_str(),63);
283 strlcpy(user->dhost,webirc_hostname->c_str(),63);
284 user->InvalidateCache();
285 delete webirc_hostname;
286 user->Shrink("cgiirc_webirc_hostname");
290 bool CheckPass(userrec* user)
292 if(IsValidHost(user->password))
294 user->Extend("cgiirc_realhost", new std::string(user->host));
295 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
296 strlcpy(user->host, user->password, 64);
297 strlcpy(user->dhost, user->password, 64);
298 user->InvalidateCache();
302 if (user->GetProtocolFamily() == AF_INET6)
303 valid = (inet_pton(AF_INET6, user->password, &((sockaddr_in6*)&user->ip)->sin6_addr) > 0);
305 valid = (inet_aton(user->password, &((sockaddr_in*)&user->ip)->sin_addr));
307 if (inet_aton(user->password, &((sockaddr_in*)&user->ip)->sin_addr))
312 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
314 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);
318 /* We got as resolved hostname in the password. */
322 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
323 ServerInstance->AddResolver(r, cached);
325 catch (ModuleException& e)
328 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
335 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);*/
343 bool CheckIdent(userrec* user)
348 int len = strlen(user->ident);
352 else if(len == 9 && *user->ident == '~')
353 ident = user->ident+1;
357 for(int i = 0; i < 4; i++)
358 if(!HexToInt(ip[i], ident + i*2))
361 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
363 user->Extend("cgiirc_realhost", new std::string(user->host));
364 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
366 if (user->GetProtocolFamily() == AF_INET6)
367 inet_pton(AF_INET6, newip, &((sockaddr_in6*)&user->ip)->sin6_addr);
369 inet_aton(newip, &((sockaddr_in*)&user->ip)->sin_addr);
371 inet_aton(newip, &((sockaddr_in*)&user->ip)->sin_addr);
378 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newip, false, user, user->GetFd(), "IDENT", cached);
379 ServerInstance->AddResolver(r, cached);
381 catch (ModuleException& e)
383 strlcpy(user->host, newip, 16);
384 strlcpy(user->dhost, newip, 16);
385 strlcpy(user->ident, "~cgiirc", 8);
386 user->InvalidateCache();
389 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
391 /*strlcpy(user->host, newip, 16);
392 strlcpy(user->dhost, newip, 16);
393 strlcpy(user->ident, "~cgiirc", 8);*/
398 bool IsValidHost(const std::string &host)
403 for(unsigned int i = 0; i < host.size(); i++)
405 if( ((host[i] >= '0') && (host[i] <= '9')) ||
406 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
407 ((host[i] >= 'a') && (host[i] <= 'z')) ||
408 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
409 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
419 bool IsValidIP(const std::string &ip)
421 if(ip.size() < 7 || ip.size() > 15)
427 for(unsigned int i = 0; i < ip.size(); i++)
429 if((dots <= 3) && (sincedot <= 3))
431 if((ip[i] >= '0') && (ip[i] <= '9'))
435 else if(ip[i] == '.')
454 bool HexToInt(int &out, const char* in)
460 out = strtol(ip, NULL, 16);
462 if(out > 255 || out < 0)
468 virtual ~ModuleCgiIRC()
472 virtual Version GetVersion()
474 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
479 class ModuleCgiIRCFactory : public ModuleFactory
482 ModuleCgiIRCFactory()
486 ~ModuleCgiIRCFactory()
490 virtual Module * CreateModule(InspIRCd* Me)
492 return new ModuleCgiIRC(Me);
498 extern "C" void * init_module( void )
500 return new ModuleCgiIRCFactory;