1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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>
22 /* $ModDesc: Change user's hosts connecting from known CGI:IRC hosts */
24 enum CGItype { INVALID, PASS, IDENT, PASSFIRST, IDENTFIRST, WEBIRC };
27 /** Holds a CGI site's details
29 class CGIhost : public classbase
36 CGIhost(const std::string &mask = "", CGItype t = IDENTFIRST, const std::string &spassword ="")
37 : hostmask(mask), type(t), password(spassword)
41 typedef std::vector<CGIhost> CGIHostlist;
45 * This is used for the webirc method of CGIIRC auth, and is (really) the best way to do these things.
46 * Syntax: WEBIRC password client hostname ip
47 * Where password is a shared key, client is the name of the "client" and version (e.g. cgiirc), hostname
48 * is the resolved host of the client issuing the command and IP is the real IP of the client.
51 * To tie in with the rest of cgiirc module, and to avoid race conditions, /webirc is only processed locally
52 * and simply sets metadata on the user, which is later decoded on full connect to give something meaningful.
54 class CommandWebirc : public Command
56 // XXX why is inspircd declared here? does class command not have one?
61 CommandWebirc(InspIRCd* iMe, CGIHostlist &cHosts, bool bnotify) : Command(iMe, "WEBIRC", 0, 4, true), Hosts(cHosts), notify(bnotify)
63 this->source = "m_cgiirc.so";
64 this->syntax = "password client hostname ip";
66 CmdResult Handle(const char** parameters, int pcnt, User *user)
68 if(user->registered == REG_ALL)
71 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
73 if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
75 if(iter->type == WEBIRC && parameters[0] == iter->password)
77 user->Extend("cgiirc_realhost", new std::string(user->host));
78 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
80 ServerInstance->SNO->WriteToSnoMask('A', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from %s", user->nick, user->host, parameters[2], user->host);
81 user->Extend("cgiirc_webirc_hostname", new std::string(parameters[2]));
82 user->Extend("cgiirc_webirc_ip", new std::string(parameters[3]));
92 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
94 class CGIResolver : public Resolver
101 CGIResolver(Module* me, InspIRCd* Instance, bool NotifyOpers, const std::string &source, bool forward, User* u, int userfd, const std::string &type, bool &cached)
102 : Resolver(Instance, source, forward ? DNS_QUERY_A : DNS_QUERY_PTR4, cached, me), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
104 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached, int resultnum = 0)
109 /* Check the user still exists */
110 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
113 ServerInstance->SNO->WriteToSnoMask('A', "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());
115 strlcpy(them->host, result.c_str(), 63);
116 strlcpy(them->dhost, result.c_str(), 63);
117 strlcpy(them->ident, "~cgiirc", 8);
118 them->InvalidateCache();
122 virtual void OnError(ResolverError e, const std::string &errormessage)
124 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
127 ServerInstance->SNO->WriteToSnoMask('A', "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());
131 virtual ~CGIResolver()
136 class ModuleCgiIRC : public Module
138 CommandWebirc* mycommand;
142 ModuleCgiIRC(InspIRCd* Me) : Module(Me)
146 mycommand = new CommandWebirc(Me, Hosts, NotifyOpers);
147 ServerInstance->AddCommand(mycommand);
149 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnCleanup, I_OnSyncUserMetaData, I_OnDecodeMetaData, I_OnUserDisconnect, I_OnUserConnect };
150 ServerInstance->Modules->Attach(eventlist, this, 7);
154 virtual void Prioritize()
156 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIO_FIRST);
159 virtual void OnRehash(User* user, const std::string ¶meter)
161 ConfigReader Conf(ServerInstance);
163 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
165 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
168 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
170 std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
171 std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
172 std::string password = Conf.ReadValue("cgihost", "password", i);
174 if(hostmask.length())
176 if (type == "webirc" && !password.length()) {
177 ServerInstance->Log(DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
181 CGItype cgitype = INVALID;
184 else if (type == "ident")
186 else if (type == "passfirst")
188 else if (type == "webirc")
193 if (cgitype == INVALID)
196 Hosts.push_back(CGIhost(hostmask,cgitype, password.length() ? password : "" ));
201 ServerInstance->Log(DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
207 virtual void OnCleanup(int target_type, void* item)
209 if(target_type == TYPE_USER)
211 User* user = (User*)item;
212 std::string* realhost;
215 if(user->GetExt("cgiirc_realhost", realhost))
218 user->Shrink("cgiirc_realhost");
221 if(user->GetExt("cgiirc_realip", realip))
224 user->Shrink("cgiirc_realip");
229 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
231 if((extname == "cgiirc_realhost") || (extname == "cgiirc_realip"))
235 if(user->GetExt(extname, data))
237 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *data);
242 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
244 if(target_type == TYPE_USER)
246 User* dest = (User*)target;
248 if(((extname == "cgiirc_realhost") || (extname == "cgiirc_realip")) && (!dest->GetExt(extname, bleh)))
250 dest->Extend(extname, new std::string(extdata));
255 virtual void OnUserDisconnect(User* user)
257 OnCleanup(TYPE_USER, user);
261 virtual int OnUserRegister(User* user)
263 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
265 if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
268 if(iter->type == PASS)
270 CheckPass(user); // We do nothing if it fails so...
272 else if(iter->type == PASSFIRST && !CheckPass(user))
274 // If the password lookup failed, try the ident
275 CheckIdent(user); // If this fails too, do nothing
277 else if(iter->type == IDENT)
279 CheckIdent(user); // Nothing on failure.
281 else if(iter->type == IDENTFIRST && !CheckIdent(user))
283 // If the ident lookup fails, try the password.
286 else if(iter->type == WEBIRC)
288 // We don't need to do anything here
296 virtual void OnUserConnect(User* user)
298 std::string *webirc_hostname, *webirc_ip;
299 if(user->GetExt("cgiirc_webirc_hostname", webirc_hostname))
301 strlcpy(user->host,webirc_hostname->c_str(),63);
302 strlcpy(user->dhost,webirc_hostname->c_str(),63);
303 delete webirc_hostname;
304 user->InvalidateCache();
305 user->Shrink("cgiirc_webirc_hostname");
307 if(user->GetExt("cgiirc_webirc_ip", webirc_ip))
310 ServerInstance->Users->RemoveCloneCounts(user);
312 valid = (inet_pton(AF_INET6, webirc_ip->c_str(), &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
315 valid = (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr));
317 if (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr))
322 user->InvalidateCache();
323 user->Shrink("cgiirc_webirc_ip");
324 ServerInstance->Users->AddLocalClone(user);
325 ServerInstance->Users->AddGlobalClone(user);
330 bool CheckPass(User* user)
332 if(IsValidHost(user->password))
334 user->Extend("cgiirc_realhost", new std::string(user->host));
335 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
336 strlcpy(user->host, user->password, 64);
337 strlcpy(user->dhost, user->password, 64);
338 user->InvalidateCache();
341 ServerInstance->Users->RemoveCloneCounts(user);
343 if (user->GetProtocolFamily() == AF_INET6)
344 valid = (inet_pton(AF_INET6, user->password, &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
346 valid = (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr));
348 if (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr))
351 ServerInstance->Users->AddLocalClone(user);
352 ServerInstance->Users->AddGlobalClone(user);
357 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
359 ServerInstance->SNO->WriteToSnoMask('A', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);
363 /* We got as resolved hostname in the password. */
368 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
369 ServerInstance->AddResolver(r, cached);
374 ServerInstance->SNO->WriteToSnoMask('A', "Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
381 ServerInstance->SNO->WriteToSnoMask('A', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);*/
389 bool CheckIdent(User* user)
394 int len = strlen(user->ident);
398 else if(len == 9 && *user->ident == '~')
399 ident = user->ident+1;
403 for(int i = 0; i < 4; i++)
404 if(!HexToInt(ip[i], ident + i*2))
407 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
409 user->Extend("cgiirc_realhost", new std::string(user->host));
410 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
411 ServerInstance->Users->RemoveCloneCounts(user);
413 if (user->GetProtocolFamily() == AF_INET6)
414 inet_pton(AF_INET6, newip, &((sockaddr_in6*)user->ip)->sin6_addr);
417 inet_aton(newip, &((sockaddr_in*)user->ip)->sin_addr);
418 ServerInstance->Users->AddLocalClone(user);
419 ServerInstance->Users->AddGlobalClone(user);
423 strlcpy(user->host, newip, 16);
424 strlcpy(user->dhost, newip, 16);
425 strlcpy(user->ident, "~cgiirc", 8);
428 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newip, false, user, user->GetFd(), "IDENT", cached);
429 ServerInstance->AddResolver(r, cached);
433 strlcpy(user->host, newip, 16);
434 strlcpy(user->dhost, newip, 16);
435 strlcpy(user->ident, "~cgiirc", 8);
436 user->InvalidateCache();
439 ServerInstance->SNO->WriteToSnoMask('A', "Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
441 /*strlcpy(user->host, newip, 16);
442 strlcpy(user->dhost, newip, 16);
443 strlcpy(user->ident, "~cgiirc", 8);*/
448 bool IsValidHost(const std::string &host)
453 for(unsigned int i = 0; i < host.size(); i++)
455 if( ((host[i] >= '0') && (host[i] <= '9')) ||
456 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
457 ((host[i] >= 'a') && (host[i] <= 'z')) ||
458 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
459 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
469 bool IsValidIP(const std::string &ip)
471 if(ip.size() < 7 || ip.size() > 15)
477 for(unsigned int i = 0; i < ip.size(); i++)
479 if((dots <= 3) && (sincedot <= 3))
481 if((ip[i] >= '0') && (ip[i] <= '9'))
485 else if(ip[i] == '.')
504 bool HexToInt(int &out, const char* in)
510 out = strtol(ip, NULL, 16);
512 if(out > 255 || out < 0)
518 virtual ~ModuleCgiIRC()
522 virtual Version GetVersion()
524 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
529 MODULE_INIT(ModuleCgiIRC)