1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
11 * Written by Craig Edwards, Craig McLure, and others.
12 * This program is free but copyrighted software; see
13 * the file COPYING for details.
15 * ---------------------------------------------------
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
26 #include "helperfuncs.h"
29 /* $ModDesc: Change user's hosts connecting from known CGI:IRC hosts */
32 /* We need this for checking our user hasnt /quit before we finish our lookup */
33 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
35 enum CGItype { PASS, IDENT, PASSFIRST, IDENTFIRST };
37 class CGIhost : public classbase
43 CGIhost(const std::string &mask = "", CGItype t = IDENTFIRST)
44 : hostmask(mask), type(t)
49 typedef std::vector<CGIhost> CGIHostlist;
51 class CGIResolver : public Resolver
58 CGIResolver(bool NotifyOpers, const std::string &source, bool forward, userrec* u, int userfd, const std::string &type)
59 : Resolver(source, forward ? DNS_QUERY_FORWARD : DNS_QUERY_REVERSE), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
61 virtual void OnLookupComplete(const std::string &result)
63 /* Check the user still exists */
64 if ((them) && (them == fd_ref_table[theirfd]))
67 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());
69 strlcpy(them->host, result.c_str(), 63);
70 strlcpy(them->dhost, result.c_str(), 63);
71 strlcpy(them->ident, "~cgiirc", 8);
75 virtual void OnError(ResolverError e, const std::string &errormessage)
77 if ((them) && (them == fd_ref_table[theirfd]))
80 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());
84 virtual ~CGIResolver()
89 class ModuleCgiIRC : public Module
95 ModuleCgiIRC(Server* Me) : Module::Module(Me)
101 void Implements(char* List)
103 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCleanup] = List[I_OnSyncUserMetaData] = List[I_OnDecodeMetaData] = List[I_OnUserQuit] = 1;
106 virtual Priority Prioritize()
108 // We want to get here before m_cloaking and m_hostchange etc
109 return PRIORITY_FIRST;
112 virtual void OnRehash(const std::string ¶meter)
116 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
118 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
121 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
123 std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
124 std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
126 if(hostmask.length())
128 Hosts.push_back(CGIhost(hostmask));
131 Hosts.back().type = PASS;
132 else if(type == "ident")
133 Hosts.back().type = IDENT;
134 else if(type == "passfirst")
135 Hosts.back().type = PASSFIRST;
139 log(DEBUG, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
145 virtual void OnCleanup(int target_type, void* item)
147 if(target_type == TYPE_USER)
149 userrec* user = (userrec*)item;
150 std::string* realhost;
153 if(user->GetExt("cgiirc_realhost", realhost))
156 user->Shrink("cgiirc_realhost");
159 if(user->GetExt("cgiirc_realip", realip))
162 user->Shrink("cgiirc_realip");
167 virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname)
169 if((extname == "cgiirc_realhost") || (extname == "cgiirc_realip"))
173 if(user->GetExt(extname, data))
175 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *data);
180 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
182 if(target_type == TYPE_USER)
184 userrec* dest = (userrec*)target;
186 if(((extname == "cgiirc_realhost") || (extname == "cgiirc_realip")) && (!dest->GetExt(extname, bleh)))
188 dest->Extend(extname, new std::string(extdata));
193 virtual void OnUserQuit(userrec* user, const std::string &message)
195 OnCleanup(TYPE_USER, user);
199 virtual void OnUserRegister(userrec* user)
201 log(DEBUG, "m_cgiirc.so: User %s registering, %s %s", user->nick,user->host,user->GetIPString());
203 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
205 log(DEBUG, "m_cgiirc.so: Matching %s against (%s or %s)", iter->hostmask.c_str(), user->host, user->GetIPString());
207 if(Srv->MatchText(user->host, iter->hostmask) || Srv->MatchText(user->GetIPString(), iter->hostmask))
210 log(DEBUG, "m_cgiirc.so: Handling CGI:IRC user: %s (%s) matched %s", user->GetFullRealHost(), user->GetIPString(), iter->hostmask.c_str());
212 if(iter->type == PASS)
214 CheckPass(user); // We do nothing if it fails so...
216 else if(iter->type == PASSFIRST && !CheckPass(user))
218 // If the password lookup failed, try the ident
219 CheckIdent(user); // If this fails too, do nothing
221 else if(iter->type == IDENT)
223 CheckIdent(user); // Nothing on failure.
225 else if(iter->type == IDENTFIRST && !CheckIdent(user))
227 // If the ident lookup fails, try the password.
236 bool CheckPass(userrec* user)
238 log(DEBUG, "m_cgiirc.so: CheckPass(%s) - %s", user->nick, user->password);
240 if(IsValidHost(user->password))
242 user->Extend("cgiirc_realhost", new std::string(user->host));
243 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
244 strlcpy(user->host, user->password, 64);
245 strlcpy(user->dhost, user->password, 64);
248 if (insp_aton(user->password, (insp_inaddr*)&((sockaddr_in6*)&user->ip)->sin6_addr))
250 if (insp_aton(user->password, (insp_inaddr*)&((sockaddr_in*)&user->ip)->sin_addr))
253 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
254 log(DEBUG, "m_cgiirc.so: Got an IP in the user's password");
257 WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);
261 /* We got as resolved hostname in the password. */
262 log(DEBUG, "m_cgiirc.so: Got a hostname in the user's password");
266 CGIResolver* r = new CGIResolver(NotifyOpers, user->password, false, user, user->fd, "PASS");
269 catch (ModuleException& e)
272 WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
279 WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);*/
285 log(DEBUG, "m_cgiirc.so: User's password was not a valid host");
291 bool CheckIdent(userrec* user)
296 int len = strlen(user->ident);
300 else if(len == 9 && *user->ident == '~')
301 ident = user->ident+1;
305 for(int i = 0; i < 4; i++)
306 if(!HexToInt(ip[i], ident + i*2))
309 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
311 user->Extend("cgiirc_realhost", new std::string(user->host));
312 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
314 insp_aton(newip, (insp_inaddr*)&((sockaddr_in6*)&user->ip)->sin6_addr);
316 insp_aton(newip, (insp_inaddr*)&((sockaddr_in*)&user->ip)->sin_addr);
321 log(DEBUG,"MAKE RESOLVER: %s %d %s",newip, user->fd, "IDENT");
322 CGIResolver* r = new CGIResolver(NotifyOpers, newip, false, user, user->fd, "IDENT");
325 catch (ModuleException& e)
327 strlcpy(user->host, newip, 16);
328 strlcpy(user->dhost, newip, 16);
329 strlcpy(user->ident, "~cgiirc", 8);
332 WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
334 /*strlcpy(user->host, newip, 16);
335 strlcpy(user->dhost, newip, 16);
336 strlcpy(user->ident, "~cgiirc", 8);*/
341 bool IsValidHost(const std::string &host)
346 for(unsigned int i = 0; i < host.size(); i++)
348 if( ((host[i] >= '0') && (host[i] <= '9')) ||
349 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
350 ((host[i] >= 'a') && (host[i] <= 'z')) ||
351 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
352 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
362 bool IsValidIP(const std::string &ip)
364 if(ip.size() < 7 || ip.size() > 15)
370 for(unsigned int i = 0; i < ip.size(); i++)
372 if((dots <= 3) && (sincedot <= 3))
374 if((ip[i] >= '0') && (ip[i] <= '9'))
378 else if(ip[i] == '.')
397 bool HexToInt(int &out, const char* in)
403 out = strtol(ip, NULL, 16);
405 if(out > 255 || out < 0)
411 virtual ~ModuleCgiIRC()
415 virtual Version GetVersion()
417 return Version(1,0,0,0,VF_VENDOR);
422 class ModuleCgiIRCFactory : public ModuleFactory
425 ModuleCgiIRCFactory()
429 ~ModuleCgiIRCFactory()
433 virtual Module * CreateModule(Server* Me)
435 return new ModuleCgiIRC(Me);
441 extern "C" void * init_module( void )
443 return new ModuleCgiIRCFactory;