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 };
29 /** Holds a CGI site's details
31 class CGIhost : public classbase
37 CGIhost(const std::string &mask = "", CGItype t = IDENTFIRST)
38 : hostmask(mask), type(t)
43 typedef std::vector<CGIhost> CGIHostlist;
45 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
47 class CGIResolver : public Resolver
54 CGIResolver(Module* me, InspIRCd* ServerInstance, bool NotifyOpers, const std::string &source, bool forward, userrec* u, int userfd, const std::string &type, bool &cached)
55 : Resolver(ServerInstance, source, forward ? DNS_QUERY_FORWARD : DNS_QUERY_REVERSE, cached, me), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
57 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
59 /* Check the user still exists */
60 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
63 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());
65 strlcpy(them->host, result.c_str(), 63);
66 strlcpy(them->dhost, result.c_str(), 63);
67 strlcpy(them->ident, "~cgiirc", 8);
68 them->InvalidateCache();
72 virtual void OnError(ResolverError e, const std::string &errormessage)
74 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
77 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());
81 virtual ~CGIResolver()
86 class ModuleCgiIRC : public Module
92 ModuleCgiIRC(InspIRCd* Me) : Module::Module(Me)
98 void Implements(char* List)
100 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCleanup] = List[I_OnSyncUserMetaData] = List[I_OnDecodeMetaData] = List[I_OnUserQuit] = 1;
103 virtual Priority Prioritize()
105 // We want to get here before m_cloaking and m_hostchange etc
106 return PRIORITY_FIRST;
109 virtual void OnRehash(userrec* user, const std::string ¶meter)
111 ConfigReader Conf(ServerInstance);
113 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
115 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
118 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
120 std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
121 std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
123 if(hostmask.length())
125 Hosts.push_back(CGIhost(hostmask));
128 Hosts.back().type = PASS;
129 else if(type == "ident")
130 Hosts.back().type = IDENT;
131 else if(type == "passfirst")
132 Hosts.back().type = PASSFIRST;
136 ServerInstance->Log(DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
142 virtual void OnCleanup(int target_type, void* item)
144 if(target_type == TYPE_USER)
146 userrec* user = (userrec*)item;
147 std::string* realhost;
150 if(user->GetExt("cgiirc_realhost", realhost))
153 user->Shrink("cgiirc_realhost");
156 if(user->GetExt("cgiirc_realip", realip))
159 user->Shrink("cgiirc_realip");
164 virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname)
166 if((extname == "cgiirc_realhost") || (extname == "cgiirc_realip"))
170 if(user->GetExt(extname, data))
172 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *data);
177 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
179 if(target_type == TYPE_USER)
181 userrec* dest = (userrec*)target;
183 if(((extname == "cgiirc_realhost") || (extname == "cgiirc_realip")) && (!dest->GetExt(extname, bleh)))
185 dest->Extend(extname, new std::string(extdata));
190 virtual void OnUserQuit(userrec* user, const std::string &message)
192 OnCleanup(TYPE_USER, user);
196 virtual int OnUserRegister(userrec* user)
198 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
200 if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
203 if(iter->type == PASS)
205 CheckPass(user); // We do nothing if it fails so...
207 else if(iter->type == PASSFIRST && !CheckPass(user))
209 // If the password lookup failed, try the ident
210 CheckIdent(user); // If this fails too, do nothing
212 else if(iter->type == IDENT)
214 CheckIdent(user); // Nothing on failure.
216 else if(iter->type == IDENTFIRST && !CheckIdent(user))
218 // If the ident lookup fails, try the password.
227 bool CheckPass(userrec* user)
229 if(IsValidHost(user->password))
231 user->Extend("cgiirc_realhost", new std::string(user->host));
232 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
233 strlcpy(user->host, user->password, 64);
234 strlcpy(user->dhost, user->password, 64);
235 user->InvalidateCache();
239 if (strchr(user->password,':'))
240 valid = (inet_pton(user->password, &((sockaddr_in6*)&user->ip)->sin6_addr));
242 valid = (inet_aton(user->password, &((sockaddr_in*)&user->ip)->sin_addr));
244 if (inet_aton(user->password, &((sockaddr_in*)&user->ip)->sin_addr))
249 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
251 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);
255 /* We got as resolved hostname in the password. */
259 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
260 ServerInstance->AddResolver(r, cached);
262 catch (ModuleException& e)
265 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
272 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);*/
280 bool CheckIdent(userrec* user)
285 int len = strlen(user->ident);
289 else if(len == 9 && *user->ident == '~')
290 ident = user->ident+1;
294 for(int i = 0; i < 4; i++)
295 if(!HexToInt(ip[i], ident + i*2))
298 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
300 user->Extend("cgiirc_realhost", new std::string(user->host));
301 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
303 insp_aton(newip, (insp_inaddr*)&((sockaddr_in6*)&user->ip)->sin6_addr);
305 insp_aton(newip, (insp_inaddr*)&((sockaddr_in*)&user->ip)->sin_addr);
311 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newip, false, user, user->GetFd(), "IDENT", cached);
312 ServerInstance->AddResolver(r, cached);
314 catch (ModuleException& e)
316 strlcpy(user->host, newip, 16);
317 strlcpy(user->dhost, newip, 16);
318 strlcpy(user->ident, "~cgiirc", 8);
321 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
323 /*strlcpy(user->host, newip, 16);
324 strlcpy(user->dhost, newip, 16);
325 strlcpy(user->ident, "~cgiirc", 8);*/
330 bool IsValidHost(const std::string &host)
335 for(unsigned int i = 0; i < host.size(); i++)
337 if( ((host[i] >= '0') && (host[i] <= '9')) ||
338 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
339 ((host[i] >= 'a') && (host[i] <= 'z')) ||
340 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
341 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
351 bool IsValidIP(const std::string &ip)
353 if(ip.size() < 7 || ip.size() > 15)
359 for(unsigned int i = 0; i < ip.size(); i++)
361 if((dots <= 3) && (sincedot <= 3))
363 if((ip[i] >= '0') && (ip[i] <= '9'))
367 else if(ip[i] == '.')
386 bool HexToInt(int &out, const char* in)
392 out = strtol(ip, NULL, 16);
394 if(out > 255 || out < 0)
400 virtual ~ModuleCgiIRC()
404 virtual Version GetVersion()
406 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
411 class ModuleCgiIRCFactory : public ModuleFactory
414 ModuleCgiIRCFactory()
418 ~ModuleCgiIRCFactory()
422 virtual Module * CreateModule(InspIRCd* Me)
424 return new ModuleCgiIRC(Me);
430 extern "C" void * init_module( void )
432 return new ModuleCgiIRCFactory;