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 { 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 Hosts.push_back(CGIhost(hostmask));
168 Hosts.back().type = PASS;
169 else if(type == "ident")
170 Hosts.back().type = IDENT;
171 else if(type == "passfirst")
172 Hosts.back().type = PASSFIRST;
173 else if(type == "webirc") {
174 Hosts.back().type = WEBIRC;
175 if(password.length())
176 Hosts.back().password=password;
178 ServerInstance->Log(DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
183 ServerInstance->Log(DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
189 virtual void OnCleanup(int target_type, void* item)
191 if(target_type == TYPE_USER)
193 userrec* user = (userrec*)item;
194 std::string* realhost;
197 if(user->GetExt("cgiirc_realhost", realhost))
200 user->Shrink("cgiirc_realhost");
203 if(user->GetExt("cgiirc_realip", realip))
206 user->Shrink("cgiirc_realip");
211 virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
213 if((extname == "cgiirc_realhost") || (extname == "cgiirc_realip"))
217 if(user->GetExt(extname, data))
219 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *data);
224 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
226 if(target_type == TYPE_USER)
228 userrec* dest = (userrec*)target;
230 if(((extname == "cgiirc_realhost") || (extname == "cgiirc_realip")) && (!dest->GetExt(extname, bleh)))
232 dest->Extend(extname, new std::string(extdata));
237 virtual void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
239 OnCleanup(TYPE_USER, user);
243 virtual int OnUserRegister(userrec* user)
245 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
247 if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
250 if(iter->type == PASS)
252 CheckPass(user); // We do nothing if it fails so...
254 else if(iter->type == PASSFIRST && !CheckPass(user))
256 // If the password lookup failed, try the ident
257 CheckIdent(user); // If this fails too, do nothing
259 else if(iter->type == IDENT)
261 CheckIdent(user); // Nothing on failure.
263 else if(iter->type == IDENTFIRST && !CheckIdent(user))
265 // If the ident lookup fails, try the password.
268 else if(iter->type == WEBIRC)
270 // We don't need to do anything here
278 virtual void OnUserConnect(userrec* user)
280 std::string *webirc_hostname, *webirc_ip;
281 if(user->GetExt("cgiirc_webirc_hostname", webirc_hostname))
283 strlcpy(user->host,webirc_hostname->c_str(),63);
284 strlcpy(user->dhost,webirc_hostname->c_str(),63);
285 delete webirc_hostname;
286 user->InvalidateCache();
287 user->Shrink("cgiirc_webirc_hostname");
289 if(user->GetExt("cgiirc_webirc_ip", webirc_ip))
293 valid = (inet_pton(AF_INET6, webirc_ip->c_str(), &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
296 valid = (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr));
298 if (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr))
303 user->InvalidateCache();
304 user->Shrink("cgiirc_webirc_ip");
308 bool CheckPass(userrec* user)
310 if(IsValidHost(user->password))
312 user->Extend("cgiirc_realhost", new std::string(user->host));
313 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
314 strlcpy(user->host, user->password, 64);
315 strlcpy(user->dhost, user->password, 64);
316 user->InvalidateCache();
320 if (user->GetProtocolFamily() == AF_INET6)
321 valid = (inet_pton(AF_INET6, user->password, &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
323 valid = (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr));
325 if (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr))
330 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
332 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);
336 /* We got as resolved hostname in the password. */
341 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
342 ServerInstance->AddResolver(r, cached);
347 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
354 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);*/
362 bool CheckIdent(userrec* user)
367 int len = strlen(user->ident);
371 else if(len == 9 && *user->ident == '~')
372 ident = user->ident+1;
376 for(int i = 0; i < 4; i++)
377 if(!HexToInt(ip[i], ident + i*2))
380 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
382 user->Extend("cgiirc_realhost", new std::string(user->host));
383 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
385 if (user->GetProtocolFamily() == AF_INET6)
386 inet_pton(AF_INET6, newip, &((sockaddr_in6*)user->ip)->sin6_addr);
389 inet_aton(newip, &((sockaddr_in*)user->ip)->sin_addr);
393 strlcpy(user->host, newip, 16);
394 strlcpy(user->dhost, newip, 16);
395 strlcpy(user->ident, "~cgiirc", 8);
398 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newip, false, user, user->GetFd(), "IDENT", cached);
399 ServerInstance->AddResolver(r, cached);
403 strlcpy(user->host, newip, 16);
404 strlcpy(user->dhost, newip, 16);
405 strlcpy(user->ident, "~cgiirc", 8);
406 user->InvalidateCache();
409 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
411 /*strlcpy(user->host, newip, 16);
412 strlcpy(user->dhost, newip, 16);
413 strlcpy(user->ident, "~cgiirc", 8);*/
418 bool IsValidHost(const std::string &host)
423 for(unsigned int i = 0; i < host.size(); i++)
425 if( ((host[i] >= '0') && (host[i] <= '9')) ||
426 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
427 ((host[i] >= 'a') && (host[i] <= 'z')) ||
428 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
429 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
439 bool IsValidIP(const std::string &ip)
441 if(ip.size() < 7 || ip.size() > 15)
447 for(unsigned int i = 0; i < ip.size(); i++)
449 if((dots <= 3) && (sincedot <= 3))
451 if((ip[i] >= '0') && (ip[i] <= '9'))
455 else if(ip[i] == '.')
474 bool HexToInt(int &out, const char* in)
480 out = strtol(ip, NULL, 16);
482 if(out > 255 || out < 0)
488 virtual ~ModuleCgiIRC()
492 virtual Version GetVersion()
494 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
499 MODULE_INIT(ModuleCgiIRC);