1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 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 "socketengine.h"
21 #include "treeserver.h"
23 #include "treesocket.h"
24 #include "resolvers.h"
26 /* Handle ERROR command */
27 bool TreeSocket::Error(parameterlist ¶ms)
29 if (params.size() < 1)
31 ServerInstance->SNO->WriteToSnoMask('l',"ERROR from %s: %s",(!InboundServerName.empty() ? InboundServerName.c_str() : myhost.c_str()),params[0].c_str());
32 /* we will return false to cause the socket to close. */
36 void TreeSocket::Split(const std::string& line, std::string& prefix, std::string& command, parameterlist& params)
38 irc::tokenstream tokens(line);
40 if (!tokens.GetToken(prefix))
45 prefix = prefix.substr(1);
49 this->SendError("BUG (?) Empty prefix received: " + line);
52 if (!tokens.GetToken(command))
54 this->SendError("BUG (?) Empty command received: " + line);
64 this->SendError("BUG (?) Empty command received: " + line);
67 while (tokens.GetToken(param))
69 params.push_back(param);
73 void TreeSocket::ProcessLine(std::string &line)
79 ServerInstance->Logs->Log("m_spanningtree",DEBUG, "S[%d] I %s", this->GetFd(), line.c_str());
81 Split(line, prefix, command, params);
86 switch (this->LinkState)
93 * Waiting for SERVER command from remote server. Server initiating
94 * the connection sends the first SERVER command, listening server
95 * replies with theirs if its happy, then if the initiator is happy,
96 * it starts to send its net sync, which starts the merge, otherwise
99 if (command == "PASS")
102 * Ignore this silently. Some services packages insist on sending PASS, even
103 * when it is not required (i.e. by us). We have to ignore this here, otherwise
104 * as it's an unknown command (effectively), it will cause the connection to be
105 * closed, which probably isn't what people want. -- w00t
108 else if (command == "SERVER")
110 this->Inbound_Server(params);
112 else if (command == "ERROR")
116 else if (command == "USER")
118 this->SendError("Client connections to this port are prohibited.");
120 else if (command == "CAPAB")
126 this->SendError(std::string("Invalid command in negotiation phase: ") + command.c_str());
132 * We have sent SERVER to the other side of the connection. Now we're waiting for them to start BURST.
133 * The other option at this stage of things, of course, is for them to close our connection thanks
134 * to invalid credentials.. -- w
136 if (command == "SERVER")
139 * Connection is either attempting to re-auth itself (stupid) or sending netburst without sending BURST.
140 * Both of these aren't allowable, so block them here. -- w
142 this->SendError("You may not re-authenticate or commence netburst without sending BURST.");
144 else if (command == "BURST")
148 time_t them = atoi(params[0].c_str());
149 time_t delta = them - ServerInstance->Time();
150 if ((delta < -600) || (delta > 600))
152 ServerInstance->SNO->WriteToSnoMask('l',"\2ERROR\2: Your clocks are out by %d seconds (this is more than five minutes). Link aborted, \2PLEASE SYNC YOUR CLOCKS!\2",abs((long)delta));
153 SendError("Your clocks are out by "+ConvToStr(abs((long)delta))+" seconds (this is more than five minutes). Link aborted, PLEASE SYNC YOUR CLOCKS!");
156 else if ((delta < -30) || (delta > 30))
158 ServerInstance->SNO->WriteToSnoMask('l',"\2WARNING\2: Your clocks are out by %d seconds. Please consider synching your clocks.", abs((long)delta));
161 this->LinkState = CONNECTED;
163 Utils->timeoutlist.erase(this);
165 Link* lnk = Utils->FindLink(InboundServerName);
167 Node = new TreeServer(this->Utils, InboundServerName, InboundDescription, InboundSID, Utils->TreeRoot, this, lnk ? lnk->Hidden : false);
169 Utils->TreeRoot->AddChild(Node);
170 parameterlist sparams;
171 sparams.push_back(InboundServerName);
172 sparams.push_back("*");
173 sparams.push_back("1");
174 sparams.push_back(InboundSID);
175 sparams.push_back(":"+InboundDescription);
176 Utils->DoOneToAllButSender(ServerInstance->Config->GetSID(),"SERVER",sparams,InboundServerName);
177 Utils->DoOneToAllButSender(prefix, "BURST", params, InboundServerName);
178 Node->bursting = true;
181 else if (command == "ERROR")
185 else if (command == "CAPAB")
194 * We're connecting (OUTGOING) to another server. They are in state WAIT_AUTH_1 until they verify
195 * our credentials, when they proceed into WAIT_AUTH_2 and send SERVER to us. We then send BURST
196 * + our netburst, which will put them into CONNECTED state. -- w
198 if (command == "SERVER")
200 // Our credentials have been accepted, send netburst. (this puts US into the CONNECTED state)
201 this->Outbound_Reply_Server(params);
203 else if (command == "ERROR")
207 else if (command == "CAPAB")
215 * Credentials have been exchanged, we've gotten their 'BURST' (or sent ours).
216 * Anything from here on should be accepted a little more reasonably.
218 this->ProcessConnectedLine(prefix, command, params);
223 void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command, parameterlist& params)
225 User* who = ServerInstance->FindUUID(prefix);
226 std::string direction;
230 direction = who->server;
234 TreeServer* ServerSource = Utils->FindServer(prefix);
236 ServerSource = Utils->FindServer(GetName());
240 who = Utils->ServerUser;
241 Utils->ServerUser->SetFakeServer(ServerSource->GetName());
242 Utils->ServerUser->uuid = ServerSource->GetID();
247 /* It is important that we don't close the link here, unknown prefix can occur
248 * due to various race conditions such as the KILL message for a user somehow
249 * crossing the users QUIT further upstream from the server. Thanks jilles!
251 ServerInstance->Logs->Log("m_spanningtree", DEBUG, "Command '%s' from unknown prefix '%s'! Dropping entire command.",
252 command.c_str(), prefix.c_str());
257 // Make sure prefix is still good
261 * Check for fake direction here, and drop any instances that are found.
262 * What is fake direction? Imagine the following server setup:
263 * 0AA <-> 0AB <-> 0AC
264 * Fake direction would be 0AC sending a message to 0AB claiming to be from
265 * 0AA, or something similar. Basically, a message taking a path that *cannot*
268 * When would this be seen?
269 * Well, hopefully never. It could be caused by race conditions, bugs, or
270 * "miscreant" servers, though, so let's check anyway. -- w
272 * We also check here for totally invalid prefixes (prefixes that are neither
273 * a valid SID or a valid UUID, so that invalid UUID or SID never makes it
274 * to the higher level functions. -- B
276 TreeServer* route_back_again = Utils->BestRouteTo(direction);
277 if ((!route_back_again) || (route_back_again->GetSocket() != this))
279 if (route_back_again)
280 ServerInstance->Logs->Log("m_spanningtree",DEBUG,"Protocol violation: Fake direction '%s' from connection '%s'",
281 prefix.c_str(),this->GetName().c_str());
286 * First up, check for any malformed commands (e.g. MODE without a timestamp)
287 * and rewrite commands where necessary (SVSMODE -> MODE for services). -- w
289 if (command == "SVSMODE") // This isn't in an "else if" so we still force FMODE for changes on channels.
292 // TODO move all this into Commands
293 if (command == "UID")
295 this->ParseUID(prefix, params);
297 else if (command == "FJOIN")
299 this->ForceJoin(prefix,params);
301 else if (command == "STATS")
303 this->Stats(prefix, params);
305 else if (command == "MOTD")
307 this->Motd(prefix, params);
309 else if (command == "ADMIN")
311 this->Admin(prefix, params);
313 else if (command == "MAP")
315 Utils->Creator->HandleMap(params, who);
317 else if (command == "SERVER")
319 this->RemoteServer(prefix,params);
321 else if (command == "ERROR")
325 else if (command == "OPERTYPE")
327 this->OperType(prefix,params);
329 else if (command == "AWAY")
331 this->Away(prefix,params);
333 else if (command == "FMODE")
335 this->ForceMode(prefix,params);
337 else if (command == "FTOPIC")
339 this->ForceTopic(prefix,params);
341 else if (command == "METADATA")
343 this->MetaData(prefix,params);
345 else if (command == "PING")
347 this->LocalPing(prefix,params);
349 else if (command == "PONG")
351 TreeServer *s = Utils->FindServer(prefix);
352 if (s && s->bursting)
354 ServerInstance->SNO->WriteToSnoMask('l',"Server \002%s\002 has not finished burst, forcing end of burst (send ENDBURST!)", prefix.c_str());
357 this->LocalPong(prefix,params);
359 else if (command == "VERSION")
361 this->ServerVersion(prefix,params);
363 else if (command == "FHOST")
365 this->ChangeHost(prefix,params);
367 else if (command == "FNAME")
369 this->ChangeName(prefix,params);
371 else if (command == "FIDENT")
373 this->ChangeIdent(prefix,params);
375 else if (command == "ADDLINE")
377 this->AddLine(prefix,params);
379 else if (command == "DELLINE")
381 this->DelLine(prefix,params);
383 else if (command == "SVSNICK")
385 this->SVSNick(prefix,params);
387 else if (command == "SAVE")
389 this->ForceNick(prefix,params);
391 else if (command == "OPERQUIT")
393 this->OperQuit(prefix,params);
395 else if (command == "IDLE")
397 this->Whois(prefix,params);
399 else if (command == "PUSH")
401 this->Push(prefix,params);
403 else if (command == "TIME")
405 this->Time(prefix,params);
407 else if (command == "SVSJOIN")
409 this->ServiceJoin(prefix,params);
411 else if (command == "SVSPART")
413 this->ServicePart(prefix,params);
415 else if (command == "SQUIT")
417 if (params.size() == 2)
419 this->Squit(Utils->FindServer(params[0]),params[1]);
422 else if (command == "MODENOTICE")
424 if (params.size() >= 2)
426 ServerInstance->Users->WriteMode(params[0].c_str(), WM_AND, "*** From %s: %s",
427 who->nick.c_str(), params[1].c_str());
429 Utils->DoOneToAllButSender(prefix, command, params, prefix);
431 else if (command == "SNONOTICE")
433 if (params.size() >= 2)
435 ServerInstance->SNO->WriteToSnoMask(*(params[0].c_str()), "From " + who->nick + ": "+ params[1]);
436 Utils->DoOneToAllButSender(prefix, command, params, prefix);
439 else if (command == "BURST")
441 // Set prefix server as bursting
444 ServerInstance->SNO->WriteToSnoMask('l', "WTF: Got BURST from a non-server(?): %s", prefix.c_str());
448 route_back_again->bursting = true;
449 Utils->DoOneToAllButSender(prefix, command, params, prefix);
451 else if (command == "ENDBURST")
455 ServerInstance->SNO->WriteToSnoMask('l', "WTF: Got ENDBURST from a non-server(?): %s", prefix.c_str());
459 route_back_again->FinishBurst();
460 Utils->DoOneToAllButSender(prefix, command, params, prefix);
462 else if (command == "ENCAP")
464 this->Encap(prefix, params);
466 else if (command == "NICK")
468 if (params.size() != 2)
470 SendError("Protocol violation: NICK message without TS - :"+std::string(who->uuid)+" NICK "+params[0]);
476 SendError("Protocol violation: Server changing nick");
480 /* Update timestamp on user when they change nicks */
481 who->age = atoi(params[1].c_str());
484 * On nick messages, check that the nick doesnt already exist here.
485 * If it does, perform collision logic.
487 User* x = ServerInstance->FindNickOnly(params[0]);
488 if ((x) && (x != who))
491 /* x is local, who is remote */
492 collideret = this->DoCollision(x, who->age, who->ident, who->GetIPString(), who->uuid);
496 * Remote client lost, or both lost, parsing or passing on this
497 * nickchange would be pointless, as the incoming client's server will
498 * soon recieve SVSNICK to change its nick to its UID. :) -- w00t
503 who->ForceNickChange(params[0].c_str());
504 Utils->RouteCommand(route_back_again, command, params, who);
508 Command* cmd = ServerInstance->Parser->GetHandler(command);
509 CmdResult res = CMD_INVALID;
510 if (cmd && params.size() >= cmd->min_params)
512 res = cmd->Handle(params, who);
515 if (res == CMD_INVALID)
516 SendError("Unrecognised or malformed command '" + command + "' -- possibly loaded mismatched modules");
517 if (res == CMD_SUCCESS)
518 Utils->RouteCommand(route_back_again, command, params, who);
522 std::string TreeSocket::GetName()
524 std::string sourceserv = this->myhost;
525 if (!this->InboundServerName.empty())
527 sourceserv = this->InboundServerName;
532 void TreeSocket::OnTimeout()
534 if (this->LinkState == CONNECTING)
536 ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Connection to \002%s\002 timed out.", myhost.c_str());
537 Utils->Creator->ConnectServer(myautoconnect);
541 void TreeSocket::Close()
543 this->BufferedSocket::Close();
545 // Test fix for big fuckup
546 if (this->LinkState != CONNECTED)
549 // Connection closed.
550 // If the connection is fully up (state CONNECTED)
551 // then propogate a netsplit to all peers.
552 std::string quitserver = this->myhost;
553 if (!this->InboundServerName.empty())
555 quitserver = this->InboundServerName;
557 TreeServer* s = Utils->FindServer(quitserver);
560 Squit(s,"Remote host closed the connection");
563 if (!quitserver.empty())
565 ServerInstance->SNO->WriteToSnoMask('l', "Connection to '\2%s\2' failed.",quitserver.c_str());
567 time_t server_uptime = ServerInstance->Time() - this->age;
569 ServerInstance->SNO->WriteToSnoMask('l', "Connection to '\2%s\2' was established for %s", quitserver.c_str(), Utils->Creator->TimeToStr(server_uptime).c_str());