1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/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
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
58 StringExtItem realhost;
60 LocalStringExt webirc_hostname;
61 LocalStringExt webirc_ip;
64 CommandWebirc(Module* Creator)
65 : Command(Creator, "WEBIRC", 4),
66 realhost("cgiirc_realhost", Creator), realip("cgiirc_realip", Creator),
67 webirc_hostname("cgiirc_webirc_hostname", Creator), webirc_ip("cgiirc_webirc_ip", Creator)
69 works_before_reg = true;
70 this->syntax = "password client hostname ip";
72 CmdResult Handle(const std::vector<std::string> ¶meters, User *user)
74 if(user->registered == REG_ALL)
77 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
79 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
81 if(iter->type == WEBIRC && parameters[0] == iter->password)
83 realhost.set(user, user->host);
84 realip.set(user, user->GetIPString());
86 ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from %s", user->nick.c_str(), user->host.c_str(), parameters[2].c_str(), user->host.c_str());
87 webirc_hostname.set(user, parameters[2]);
88 webirc_ip.set(user, parameters[3]);
94 ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s tried to use WEBIRC, but didn't match any configured webirc blocks.", user->GetFullRealHost().c_str());
100 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
102 class CGIResolver : public Resolver
105 std::string theiruid;
106 LocalIntExt& waiting;
109 CGIResolver(Module* me, bool NotifyOpers, const std::string &source, bool forward, LocalUser* u,
110 const std::string &type, bool &cached, LocalIntExt& ext)
111 : Resolver(source, forward ? DNS_QUERY_A : DNS_QUERY_PTR4, cached, me), typ(type), theiruid(u->uuid),
112 waiting(ext), notify(NotifyOpers)
116 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
118 /* Check the user still exists */
119 User* them = ServerInstance->FindUUID(theiruid);
123 ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from %s", them->nick.c_str(), them->host.c_str(), result.c_str(), typ.c_str());
125 if (result.length() > 64)
128 them->dhost = result;
129 them->InvalidateCache();
130 them->CheckLines(true);
134 virtual void OnError(ResolverError e, const std::string &errormessage)
136 User* them = ServerInstance->FindUUID(theiruid);
140 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.c_str(), them->host.c_str(), typ.c_str());
144 virtual ~CGIResolver()
146 User* them = ServerInstance->FindUUID(theiruid);
149 int count = waiting.get(them);
151 waiting.set(them, count - 1);
155 class ModuleCgiIRC : public Module
160 ModuleCgiIRC() : cmd(this), waiting("cgiirc-delay", this)
167 ServerInstance->AddCommand(&cmd);
168 ServerInstance->Extensions.Register(&cmd.realhost);
169 ServerInstance->Extensions.Register(&cmd.realip);
170 ServerInstance->Extensions.Register(&cmd.webirc_hostname);
171 ServerInstance->Extensions.Register(&cmd.webirc_ip);
173 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnCheckReady, I_OnUserConnect };
174 ServerInstance->Modules->Attach(eventlist, this, 4);
179 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIORITY_FIRST);
180 Module* umodes = ServerInstance->Modules->Find("m_conn_umodes.so");
181 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIORITY_BEFORE, &umodes);
184 void OnRehash(User* user)
188 // Do we send an oper notice when a CGI:IRC has their host changed?
189 cmd.notify = ServerInstance->Config->ConfValue("cgiirc")->getBool("opernotice", true);
191 ConfigTagList tags = ServerInstance->Config->ConfTags("cgihost");
192 for (ConfigIter i = tags.first; i != tags.second; ++i)
194 ConfigTag* tag = i->second;
195 std::string hostmask = tag->getString("mask"); // An allowed CGI:IRC host
196 std::string type = tag->getString("type"); // What type of user-munging we do on this host.
197 std::string password = tag->getString("password");
199 if(hostmask.length())
201 if (type == "webirc" && !password.length()) {
202 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
206 CGItype cgitype = INVALID;
209 else if (type == "ident")
211 else if (type == "passfirst")
213 else if (type == "webirc")
218 if (cgitype == INVALID)
221 cmd.Hosts.push_back(CGIhost(hostmask,cgitype, password.length() ? password : "" ));
226 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
232 ModResult OnCheckReady(LocalUser *user)
234 if (waiting.get(user))
236 return MOD_RES_PASSTHRU;
239 ModResult OnUserRegister(LocalUser* user)
241 for(CGIHostlist::iterator iter = cmd.Hosts.begin(); iter != cmd.Hosts.end(); iter++)
243 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
246 if(iter->type == PASS)
248 CheckPass(user); // We do nothing if it fails so...
249 user->CheckLines(true);
251 else if(iter->type == PASSFIRST && !CheckPass(user))
253 // If the password lookup failed, try the ident
254 CheckIdent(user); // If this fails too, do nothing
255 user->CheckLines(true);
257 else if(iter->type == IDENT)
259 CheckIdent(user); // Nothing on failure.
260 user->CheckLines(true);
262 else if(iter->type == IDENTFIRST && !CheckIdent(user))
264 // If the ident lookup fails, try the password.
266 user->CheckLines(true);
268 else if(iter->type == WEBIRC)
270 // We don't need to do anything here
272 return MOD_RES_PASSTHRU;
275 return MOD_RES_PASSTHRU;
278 virtual void OnUserConnect(LocalUser* user)
280 std::string *webirc_hostname = cmd.webirc_hostname.get(user);
281 std::string *webirc_ip = cmd.webirc_ip.get(user);
284 ServerInstance->Users->RemoveCloneCounts(user);
285 user->SetClientIP(webirc_ip->c_str());
286 user->InvalidateCache();
287 if (webirc_hostname && webirc_hostname->length() < 64)
288 user->host = user->dhost = *webirc_hostname;
290 user->host = user->dhost = user->GetIPString();
291 user->InvalidateCache();
292 ServerInstance->Users->AddLocalClone(user);
293 ServerInstance->Users->AddGlobalClone(user);
296 user->CheckLines(true);
297 cmd.webirc_ip.unset(user);
298 cmd.webirc_hostname.unset(user);
301 bool CheckPass(LocalUser* user)
303 if(IsValidHost(user->password))
305 cmd.realhost.set(user, user->host);
306 cmd.realip.set(user, user->GetIPString());
307 user->host = user->password;
308 user->dhost = user->password;
309 user->InvalidateCache();
311 ServerInstance->Users->RemoveCloneCounts(user);
312 user->SetClientIP(user->password.c_str());
313 ServerInstance->Users->AddLocalClone(user);
314 ServerInstance->Users->AddGlobalClone(user);
321 CGIResolver* r = new CGIResolver(this, cmd.notify, user->password, false, user, "PASS", cached, waiting);
322 ServerInstance->AddResolver(r, cached);
323 waiting.set(user, waiting.get(user) + 1);
328 ServerInstance->SNO->WriteToSnoMask('a', "Connecting user %s detected as using CGI:IRC (%s), but I could not resolve their hostname!", user->nick.c_str(), user->host.c_str());
331 user->password.clear();
338 bool CheckIdent(LocalUser* user)
341 int len = user->ident.length();
345 ident = user->ident.c_str();
346 else if(len == 9 && user->ident[0] == '~')
347 ident = user->ident.c_str() + 1;
352 unsigned long ipaddr = strtoul(ident, NULL, 16);
355 newip.s_addr = htonl(ipaddr);
356 char* newipstr = inet_ntoa(newip);
358 cmd.realhost.set(user, user->host);
359 cmd.realip.set(user, user->GetIPString());
360 ServerInstance->Users->RemoveCloneCounts(user);
361 user->SetClientIP(newipstr);
362 ServerInstance->Users->AddLocalClone(user);
363 ServerInstance->Users->AddGlobalClone(user);
366 user->host = newipstr;
367 user->dhost = newipstr;
368 user->ident.assign("~cgiirc", 0, 8);
373 CGIResolver* r = new CGIResolver(this, cmd.notify, newipstr, false, user, "IDENT", cached, waiting);
374 ServerInstance->AddResolver(r, cached);
375 waiting.set(user, waiting.get(user) + 1);
379 user->InvalidateCache();
382 ServerInstance->SNO->WriteToSnoMask('a', "Connecting user %s detected as using CGI:IRC (%s), but I could not resolve their hostname!", user->nick.c_str(), user->host.c_str());
388 bool IsValidHost(const std::string &host)
390 if(!host.size() || host.size() > 64)
393 for(unsigned int i = 0; i < host.size(); i++)
395 if( ((host[i] >= '0') && (host[i] <= '9')) ||
396 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
397 ((host[i] >= 'a') && (host[i] <= 'z')) ||
398 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
399 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
409 bool IsValidIP(const std::string &ip)
411 if(ip.size() < 7 || ip.size() > 15)
417 for(unsigned int i = 0; i < ip.size(); i++)
419 if((dots <= 3) && (sincedot <= 3))
421 if((ip[i] >= '0') && (ip[i] <= '9'))
425 else if(ip[i] == '.')
444 virtual ~ModuleCgiIRC()
448 virtual Version GetVersion()
450 return Version("Change user's hosts connecting from known CGI:IRC hosts",VF_VENDOR);
455 MODULE_INIT(ModuleCgiIRC)