1 #include "configreader.h"
5 #include "commands/cmd_whois.h"
6 #include "commands/cmd_stats.h"
11 #include "transport.h"
12 #include "socketengine.h"
14 #include "m_spanningtree/main.h"
15 #include "m_spanningtree/utils.h"
16 #include "m_spanningtree/treeserver.h"
17 #include "m_spanningtree/link.h"
18 #include "m_spanningtree/treesocket.h"
19 #include "m_spanningtree/resolvers.h"
20 #include "m_spanningtree/handshaketimer.h"
22 /* $ModDep: m_spanningtree/timesynctimer.h m_spanningtree/resolvers.h m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/link.h m_spanningtree/treesocket.h */
24 int TreeSocket::WriteLine(std::string line)
26 Instance->Log(DEBUG, "-> %s", line.c_str());
28 return this->Write(line);
31 /* Handle ERROR command */
32 bool TreeSocket::Error(std::deque<std::string> ¶ms)
34 if (params.size() < 1)
36 this->Instance->SNO->WriteToSnoMask('l',"ERROR from %s: %s",(InboundServerName != "" ? InboundServerName.c_str() : myhost.c_str()),params[0].c_str());
37 /* we will return false to cause the socket to close. */
41 /** remote MOTD. leet, huh? */
42 bool TreeSocket::Motd(const std::string &prefix, std::deque<std::string> ¶ms)
44 if (params.size() > 0)
46 if (this->Instance->MatchText(this->Instance->Config->ServerName, params[0]))
48 /* It's for our server */
50 userrec* source = this->Instance->FindNick(prefix);
54 std::deque<std::string> par;
55 par.push_back(prefix);
58 if (!Instance->Config->MOTD.size())
60 par[1] = std::string("::")+Instance->Config->ServerName+" 422 "+source->nick+" :Message of the day file is missing.";
61 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
65 par[1] = std::string("::")+Instance->Config->ServerName+" 375 "+source->nick+" :"+Instance->Config->ServerName+" message of the day";
66 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
68 for (unsigned int i = 0; i < Instance->Config->MOTD.size(); i++)
70 par[1] = std::string("::")+Instance->Config->ServerName+" 372 "+source->nick+" :- "+Instance->Config->MOTD[i];
71 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
74 par[1] = std::string("::")+Instance->Config->ServerName+" 376 "+source->nick+" End of message of the day.";
75 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
81 userrec* source = this->Instance->FindNick(prefix);
83 Utils->DoOneToOne(prefix, "MOTD", params, params[0]);
89 /** remote ADMIN. leet, huh? */
90 bool TreeSocket::Admin(const std::string &prefix, std::deque<std::string> ¶ms)
92 if (params.size() > 0)
94 if (this->Instance->MatchText(this->Instance->Config->ServerName, params[0]))
96 /* It's for our server */
98 userrec* source = this->Instance->FindNick(prefix);
101 std::deque<std::string> par;
102 par.push_back(prefix);
104 par[1] = std::string("::")+Instance->Config->ServerName+" 256 "+source->nick+" :Administrative info for "+Instance->Config->ServerName;
105 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
106 par[1] = std::string("::")+Instance->Config->ServerName+" 257 "+source->nick+" :Name - "+Instance->Config->AdminName;
107 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
108 par[1] = std::string("::")+Instance->Config->ServerName+" 258 "+source->nick+" :Nickname - "+Instance->Config->AdminNick;
109 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
110 par[1] = std::string("::")+Instance->Config->ServerName+" 258 "+source->nick+" :E-Mail - "+Instance->Config->AdminEmail;
111 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
117 userrec* source = this->Instance->FindNick(prefix);
119 Utils->DoOneToOne(prefix, "ADMIN", params, params[0]);
125 bool TreeSocket::Stats(const std::string &prefix, std::deque<std::string> ¶ms)
127 /* Get the reply to a STATS query if it matches this servername,
128 * and send it back as a load of PUSH queries
130 if (params.size() > 1)
132 if (this->Instance->MatchText(this->Instance->Config->ServerName, params[1]))
134 /* It's for our server */
136 userrec* source = this->Instance->FindNick(prefix);
139 std::deque<std::string> par;
140 par.push_back(prefix);
142 DoStats(this->Instance, *(params[0].c_str()), source, results);
143 for (size_t i = 0; i < results.size(); i++)
145 par[1] = "::" + results[i];
146 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
153 userrec* source = this->Instance->FindNick(prefix);
155 Utils->DoOneToOne(prefix, "STATS", params, params[1]);
162 /** Because the core won't let users or even SERVERS set +o,
163 * we use the OPERTYPE command to do this.
165 bool TreeSocket::OperType(const std::string &prefix, std::deque<std::string> ¶ms)
167 if (params.size() != 1)
169 std::string opertype = params[0];
170 userrec* u = this->Instance->FindNick(prefix);
173 u->modes[UM_OPERATOR] = 1;
174 this->Instance->all_opers.push_back(u);
175 strlcpy(u->oper,opertype.c_str(),NICKMAX-1);
176 Utils->DoOneToAllButSender(u->nick,"OPERTYPE",params,u->server);
177 this->Instance->SNO->WriteToSnoMask('o',"From %s: User %s (%s@%s) is now an IRC operator of type %s",u->server, u->nick,u->ident,u->host,irc::Spacify(opertype.c_str()));
182 /** Because Andy insists that services-compatible servers must
183 * implement SVSNICK and SVSJOIN, that's exactly what we do :p
185 bool TreeSocket::ForceNick(const std::string &prefix, std::deque<std::string> ¶ms)
187 if (params.size() < 3)
190 userrec* u = this->Instance->FindNick(params[0]);
194 Utils->DoOneToAllButSender(prefix,"SVSNICK",params,prefix);
197 std::deque<std::string> par;
198 par.push_back(params[1]);
199 if (!u->ForceNickChange(params[1].c_str()))
201 userrec::QuitUser(this->Instance, u, "Nickname collision");
204 u->age = atoi(params[2].c_str());
211 * Remote SQUIT (RSQUIT). Routing works similar to SVSNICK: Route it to the server that the target is connected to locally,
212 * then let that server do the dirty work (squit it!). Example:
213 * A -> B -> C -> D: oper on A squits D, A routes to B, B routes to C, C notices D connected locally, kills it. -- w00t
215 bool TreeSocket::RemoteSquit(const std::string &prefix, std::deque<std::string> ¶ms)
217 /* ok.. :w00t RSQUIT jupe.barafranca.com :reason here */
218 if (params.size() < 2)
221 TreeServer* s = Utils->FindServerMask(params[0]);
225 if (s == Utils->TreeRoot)
227 this->Instance->SNO->WriteToSnoMask('l',"What the fuck, I recieved a remote SQUIT for myself? :< (from %s", prefix.c_str());
231 TreeSocket* sock = s->GetSocket();
235 /* it's locally connected, KILL IT! */
236 Instance->SNO->WriteToSnoMask('l',"RSQUIT: Server \002%s\002 removed from network by %s: %s", params[0].c_str(), prefix.c_str(), params[1].c_str());
237 sock->Squit(s,"Server quit by " + prefix + ": " + params[1]);
238 Instance->SE->DelFd(sock);
244 /* route the rsquit */
245 params[1] = ":" + params[1];
246 Utils->DoOneToOne(prefix, "RSQUIT", params, params[0]);
251 /* mother fucker! it doesn't exist */
257 bool TreeSocket::ServiceJoin(const std::string &prefix, std::deque<std::string> ¶ms)
259 if (params.size() < 2)
262 userrec* u = this->Instance->FindNick(params[0]);
266 /* only join if it's local, otherwise just pass it on! */
268 chanrec::JoinUser(this->Instance, u, params[1].c_str(), false);
269 Utils->DoOneToAllButSender(prefix,"SVSJOIN",params,prefix);
274 bool TreeSocket::RemoteRehash(const std::string &prefix, std::deque<std::string> ¶ms)
276 if (params.size() < 1)
279 std::string servermask = params[0];
281 if (this->Instance->MatchText(this->Instance->Config->ServerName,servermask))
283 this->Instance->SNO->WriteToSnoMask('l',"Remote rehash initiated by \002"+prefix+"\002.");
284 this->Instance->RehashServer();
285 Utils->ReadConfiguration(false);
286 InitializeDisabledCommands(Instance->Config->DisabledCommands, Instance);
288 Utils->DoOneToAllButSender(prefix,"REHASH",params,prefix);
292 bool TreeSocket::RemoteKill(const std::string &prefix, std::deque<std::string> ¶ms)
294 if (params.size() != 2)
297 std::string nick = params[0];
298 userrec* u = this->Instance->FindNick(prefix);
299 userrec* who = this->Instance->FindNick(nick);
303 /* Prepend kill source, if we don't have one */
304 std::string sourceserv = prefix;
307 sourceserv = u->server;
309 if (*(params[1].c_str()) != '[')
311 params[1] = "[" + sourceserv + "] Killed (" + params[1] +")";
313 std::string reason = params[1];
314 params[1] = ":" + params[1];
315 Utils->DoOneToAllButSender(prefix,"KILL",params,sourceserv);
316 who->Write(":%s KILL %s :%s (%s)", sourceserv.c_str(), who->nick, sourceserv.c_str(), reason.c_str());
317 userrec::QuitUser(this->Instance,who,reason);
322 bool TreeSocket::LocalPong(const std::string &prefix, std::deque<std::string> ¶ms)
324 if (params.size() < 1)
327 if (params.size() == 1)
329 TreeServer* ServerSource = Utils->FindServer(prefix);
332 ServerSource->SetPingFlag();
337 std::string forwardto = params[1];
338 if (forwardto == this->Instance->Config->ServerName)
341 * this is a PONG for us
342 * if the prefix is a user, check theyre local, and if they are,
343 * dump the PONG reply back to their fd. If its a server, do nowt.
344 * Services might want to send these s->s, but we dont need to yet.
346 userrec* u = this->Instance->FindNick(prefix);
349 u->WriteServ("PONG %s %s",params[0].c_str(),params[1].c_str());
354 // not for us, pass it on :)
355 Utils->DoOneToOne(prefix,"PONG",params,forwardto);
362 bool TreeSocket::MetaData(const std::string &prefix, std::deque<std::string> ¶ms)
364 if (params.size() < 2)
367 params.push_back("");
368 TreeServer* ServerSource = Utils->FindServer(prefix);
371 if (params[0] == "*")
373 FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_OTHER,NULL,params[1],params[2]));
375 else if (*(params[0].c_str()) == '#')
377 chanrec* c = this->Instance->FindChan(params[0]);
380 FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_CHANNEL,c,params[1],params[2]));
383 else if (*(params[0].c_str()) != '#')
385 userrec* u = this->Instance->FindNick(params[0]);
388 FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_USER,u,params[1],params[2]));
393 params[2] = ":" + params[2];
394 Utils->DoOneToAllButSender(prefix,"METADATA",params,prefix);
398 bool TreeSocket::ServerVersion(const std::string &prefix, std::deque<std::string> ¶ms)
400 if (params.size() < 1)
403 TreeServer* ServerSource = Utils->FindServer(prefix);
407 ServerSource->SetVersion(params[0]);
409 params[0] = ":" + params[0];
410 Utils->DoOneToAllButSender(prefix,"VERSION",params,prefix);
414 bool TreeSocket::ChangeHost(const std::string &prefix, std::deque<std::string> ¶ms)
416 if (params.size() < 1)
418 userrec* u = this->Instance->FindNick(prefix);
422 u->ChangeDisplayedHost(params[0].c_str());
423 Utils->DoOneToAllButSender(prefix,"FHOST",params,u->server);
428 bool TreeSocket::AddLine(const std::string &prefix, std::deque<std::string> ¶ms)
430 if (params.size() < 6)
432 bool propogate = false;
434 Utils->lines_to_apply = 0;
435 switch (*(params[0].c_str()))
438 propogate = Instance->XLines->add_zline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
439 Instance->XLines->zline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
441 Utils->lines_to_apply |= APPLY_ZLINES;
444 propogate = Instance->XLines->add_qline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
445 Instance->XLines->qline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
447 Utils->lines_to_apply |= APPLY_QLINES;
450 propogate = Instance->XLines->add_eline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
451 Instance->XLines->eline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
454 propogate = Instance->XLines->add_gline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
455 Instance->XLines->gline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
457 Utils->lines_to_apply |= APPLY_GLINES;
460 propogate = Instance->XLines->add_kline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
462 Utils->lines_to_apply |= APPLY_KLINES;
465 /* Just in case... */
466 this->Instance->SNO->WriteToSnoMask('x',"\2WARNING\2: Invalid xline type '"+params[0]+"' sent by server "+prefix+", ignored!");
470 /* Send it on its way */
473 if (atoi(params[4].c_str()))
475 this->Instance->SNO->WriteToSnoMask('x',"%s Added %cLINE on %s to expire in %lu seconds (%s).",prefix.c_str(),*(params[0].c_str()),params[1].c_str(),atoi(params[4].c_str()),params[5].c_str());
479 this->Instance->SNO->WriteToSnoMask('x',"%s Added permenant %cLINE on %s (%s).",prefix.c_str(),*(params[0].c_str()),params[1].c_str(),params[5].c_str());
481 params[5] = ":" + params[5];
482 Utils->DoOneToAllButSender(prefix,"ADDLINE",params,prefix);
486 Instance->XLines->apply_lines(Utils->lines_to_apply);
487 Utils->lines_to_apply = 0;
492 bool TreeSocket::ChangeName(const std::string &prefix, std::deque<std::string> ¶ms)
494 if (params.size() < 1)
496 userrec* u = this->Instance->FindNick(prefix);
499 u->ChangeName(params[0].c_str());
500 params[0] = ":" + params[0];
501 Utils->DoOneToAllButSender(prefix,"FNAME",params,u->server);
506 bool TreeSocket::Whois(const std::string &prefix, std::deque<std::string> ¶ms)
508 if (params.size() < 1)
510 userrec* u = this->Instance->FindNick(prefix);
513 // an incoming request
514 if (params.size() == 1)
516 userrec* x = this->Instance->FindNick(params[0]);
517 if ((x) && (IS_LOCAL(x)))
519 userrec* x = this->Instance->FindNick(params[0]);
522 snprintf(signon,MAXBUF,"%lu",(unsigned long)x->signon);
523 snprintf(idle,MAXBUF,"%lu",(unsigned long)abs((x->idle_lastmsg)-Instance->Time(true)));
524 std::deque<std::string> par;
525 par.push_back(prefix);
526 par.push_back(signon);
528 // ours, we're done, pass it BACK
529 Utils->DoOneToOne(params[0],"IDLE",par,u->server);
533 // not ours pass it on
534 Utils->DoOneToOne(prefix,"IDLE",params,x->server);
537 else if (params.size() == 3)
539 std::string who_did_the_whois = params[0];
540 userrec* who_to_send_to = this->Instance->FindNick(who_did_the_whois);
541 if ((who_to_send_to) && (IS_LOCAL(who_to_send_to)))
543 // an incoming reply to a whois we sent out
544 std::string nick_whoised = prefix;
545 unsigned long signon = atoi(params[1].c_str());
546 unsigned long idle = atoi(params[2].c_str());
547 if ((who_to_send_to) && (IS_LOCAL(who_to_send_to)))
548 do_whois(this->Instance,who_to_send_to,u,signon,idle,nick_whoised.c_str());
552 // not ours, pass it on
553 Utils->DoOneToOne(prefix,"IDLE",params,who_to_send_to->server);
560 bool TreeSocket::Push(const std::string &prefix, std::deque<std::string> ¶ms)
562 if (params.size() < 2)
564 userrec* u = this->Instance->FindNick(params[0]);
573 // continue the raw onwards
574 params[1] = ":" + params[1];
575 Utils->DoOneToOne(prefix,"PUSH",params,u->server);
580 bool TreeSocket::HandleSetTime(const std::string &prefix, std::deque<std::string> ¶ms)
582 if (!params.size() || !Utils->EnableTimeSync)
587 if ((params.size() == 2) && (params[1] == "FORCE"))
590 time_t rts = atoi(params[0].c_str());
591 time_t us = Instance->Time(true);
595 Utils->DoOneToAllButSender(prefix, "TIMESET", params, prefix);
597 else if (force || (rts < us))
599 int old = Instance->SetTimeDelta(rts - us);
600 Instance->Log(DEBUG, "%s TS (diff %d) from %s applied (old delta was %d)", (force) ? "Forced" : "Lower", rts - us, prefix.c_str(), old);
602 Utils->DoOneToAllButSender(prefix, "TIMESET", params, prefix);
606 Instance->Log(DEBUG, "Higher TS (diff %d) from %s overridden", us - rts, prefix.c_str());
608 std::deque<std::string> oparams;
609 oparams.push_back(ConvToStr(us));
611 Utils->DoOneToMany(prefix, "TIMESET", oparams);
617 bool TreeSocket::Time(const std::string &prefix, std::deque<std::string> ¶ms)
619 // :source.server TIME remote.server sendernick
620 // :remote.server TIME source.server sendernick TS
621 if (params.size() == 2)
623 // someone querying our time?
624 if (this->Instance->Config->ServerName == params[0])
626 userrec* u = this->Instance->FindNick(params[1]);
629 params.push_back(ConvToStr(Instance->Time(false)));
631 Utils->DoOneToOne(this->Instance->Config->ServerName,"TIME",params,params[0]);
636 // not us, pass it on
637 userrec* u = this->Instance->FindNick(params[1]);
639 Utils->DoOneToOne(prefix,"TIME",params,params[0]);
642 else if (params.size() == 3)
644 // a response to a previous TIME
645 userrec* u = this->Instance->FindNick(params[1]);
646 if ((u) && (IS_LOCAL(u)))
648 time_t rawtime = atol(params[2].c_str());
649 struct tm * timeinfo;
650 timeinfo = localtime(&rawtime);
652 snprintf(tms,26,"%s",asctime(timeinfo));
654 u->WriteServ("391 %s %s :%s",u->nick,prefix.c_str(),tms);
659 Utils->DoOneToOne(prefix,"TIME",params,u->server);
665 bool TreeSocket::LocalPing(const std::string &prefix, std::deque<std::string> ¶ms)
667 if (params.size() < 1)
669 if (params.size() == 1)
671 std::string stufftobounce = params[0];
672 this->WriteLine(std::string(":")+this->Instance->Config->ServerName+" PONG "+stufftobounce);
677 std::string forwardto = params[1];
678 if (forwardto == this->Instance->Config->ServerName)
680 // this is a ping for us, send back PONG to the requesting server
681 params[1] = params[0];
682 params[0] = forwardto;
683 Utils->DoOneToOne(forwardto,"PONG",params,params[1]);
687 // not for us, pass it on :)
688 Utils->DoOneToOne(prefix,"PING",params,forwardto);
694 bool TreeSocket::RemoveStatus(const std::string &prefix, std::deque<std::string> ¶ms)
696 if (params.size() < 1)
698 chanrec* c = Instance->FindChan(params[0]);
701 irc::modestacker modestack(false);
702 CUList *ulist = c->GetUsers();
704 std::deque<std::string> stackresult;
706 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
708 std::string modesequence = Instance->Modes->ModeString(i->second, c);
709 if (modesequence.length())
711 irc::spacesepstream sep(modesequence);
712 std::string modeletters = sep.GetToken();
713 while (!modeletters.empty())
715 char mletter = *(modeletters.begin());
716 modestack.Push(mletter,sep.GetToken());
717 modeletters.erase(modeletters.begin());
722 while (modestack.GetStackedLine(stackresult))
724 stackresult.push_front(ConvToStr(c->age));
725 stackresult.push_front(c->name);
726 Utils->DoOneToMany(Instance->Config->ServerName, "FMODE", stackresult);
727 stackresult.erase(stackresult.begin() + 1);
728 for (size_t z = 0; z < stackresult.size(); z++)
730 y[z] = stackresult[z].c_str();
732 userrec* n = new userrec(Instance);
733 n->SetFd(FD_MAGIC_NUMBER);
734 Instance->SendMode(y, stackresult.size(), n);
741 bool TreeSocket::RemoteServer(const std::string &prefix, std::deque<std::string> ¶ms)
743 if (params.size() < 4)
745 std::string servername = params[0];
746 std::string password = params[1];
747 // hopcount is not used for a remote server, we calculate this ourselves
748 std::string description = params[3];
749 TreeServer* ParentOfThis = Utils->FindServer(prefix);
752 this->WriteLine("ERROR :Protocol error - Introduced remote server from unknown server "+prefix);
755 TreeServer* CheckDupe = Utils->FindServer(servername);
758 this->WriteLine("ERROR :Server "+servername+" already exists!");
759 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+servername+"\2 denied, already exists");
762 TreeServer* Node = new TreeServer(this->Utils,this->Instance,servername,description,ParentOfThis,NULL);
763 ParentOfThis->AddChild(Node);
764 params[3] = ":" + params[3];
765 Utils->DoOneToAllButSender(prefix,"SERVER",params,prefix);
766 this->Instance->SNO->WriteToSnoMask('l',"Server \002"+prefix+"\002 introduced server \002"+servername+"\002 ("+description+")");
770 bool TreeSocket::Outbound_Reply_Server(std::deque<std::string> ¶ms)
772 if (params.size() < 4)
775 irc::string servername = params[0].c_str();
776 std::string sname = params[0];
777 std::string password = params[1];
778 int hops = atoi(params[2].c_str());
782 this->WriteLine("ERROR :Server too far away for authentication");
783 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
786 std::string description = params[3];
787 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
789 if ((x->Name == servername) && (x->RecvPass == password))
791 TreeServer* CheckDupe = Utils->FindServer(sname);
794 this->WriteLine("ERROR :Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
795 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
798 // Begin the sync here. this kickstarts the
799 // other side, waiting in WAIT_AUTH_2 state,
800 // into starting their burst, as it shows
802 this->LinkState = CONNECTED;
803 // we should add the details of this server now
804 // to the servers tree, as a child of the root
806 TreeServer* Node = new TreeServer(this->Utils,this->Instance,sname,description,Utils->TreeRoot,this);
807 Utils->TreeRoot->AddChild(Node);
808 params[3] = ":" + params[3];
809 Utils->DoOneToAllButSender(Utils->TreeRoot->GetName(),"SERVER",params,sname);
810 this->bursting = true;
815 this->WriteLine("ERROR :Invalid credentials");
816 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
820 bool TreeSocket::Inbound_Server(std::deque<std::string> ¶ms)
822 if (params.size() < 4)
824 irc::string servername = params[0].c_str();
825 std::string sname = params[0];
826 std::string password = params[1];
827 int hops = atoi(params[2].c_str());
831 this->WriteLine("ERROR :Server too far away for authentication");
832 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
835 std::string description = params[3];
836 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
838 if ((x->Name == servername) && (x->RecvPass == password))
840 TreeServer* CheckDupe = Utils->FindServer(sname);
843 this->WriteLine("ERROR :Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
844 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
847 this->Instance->SNO->WriteToSnoMask('l',"Verified incoming server connection from \002"+sname+"\002["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] ("+description+")");
850 std::string name = InspSocketNameRequest((Module*)Utils->Creator, this->Hook).Send();
851 this->Instance->SNO->WriteToSnoMask('l',"Connection from \2"+sname+"\2["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] using transport \2"+name+"\2");
854 this->InboundServerName = sname;
855 this->InboundDescription = description;
856 // this is good. Send our details: Our server name and description and hopcount of 0,
857 // along with the sendpass from this block.
858 this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+x->SendPass+" 0 :"+this->Instance->Config->ServerDesc);
859 // move to the next state, we are now waiting for THEM.
860 this->LinkState = WAIT_AUTH_2;
864 this->WriteLine("ERROR :Invalid credentials");
865 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
869 void TreeSocket::Split(const std::string &line, std::deque<std::string> &n)
872 irc::tokenstream tokens(line);
874 while ((param = tokens.GetToken()) != "")
879 bool TreeSocket::ProcessLine(std::string &line)
881 std::deque<std::string> params;
885 line = line.substr(0, line.find_first_of("\r\n"));
890 Instance->Log(DEBUG, "<- %s", line.c_str());
892 this->Split(line.c_str(),params);
894 if ((params[0][0] == ':') && (params.size() > 1))
896 prefix = params[0].substr(1);
899 command = params[0].c_str();
901 switch (this->LinkState)
906 // Waiting for SERVER command from remote server. Server initiating
907 // the connection sends the first SERVER command, listening server
908 // replies with theirs if its happy, then if the initiator is happy,
909 // it starts to send its net sync, which starts the merge, otherwise
910 // it sends an ERROR.
911 if (command == "PASS")
913 /* Silently ignored */
915 else if (command == "SERVER")
917 return this->Inbound_Server(params);
919 else if (command == "ERROR")
921 return this->Error(params);
923 else if (command == "USER")
925 this->WriteLine("ERROR :Client connections to this port are prohibited.");
928 else if (command == "CAPAB")
930 return this->Capab(params);
932 else if ((command == "U") || (command == "S"))
934 this->WriteLine("ERROR :Cannot use the old-style mesh linking protocol with m_spanningtree.so!");
939 std::string error("ERROR :Invalid command in negotiation phase: ");
940 error.append(command.c_str());
941 this->WriteLine(error);
946 // Waiting for start of other side's netmerge to say they liked our
948 if (command == "SERVER")
950 // cant do this, they sent it to us in the WAIT_AUTH_1 state!
954 else if ((command == "U") || (command == "S"))
956 this->WriteLine("ERROR :Cannot use the old-style mesh linking protocol with m_spanningtree.so!");
959 else if (command == "BURST")
961 if (params.size() && Utils->EnableTimeSync)
963 /* If a time stamp is provided, apply synchronization */
965 time_t them = atoi(params[0].c_str());
966 time_t us = Instance->Time(false);
967 int delta = them - us;
968 if ((params.size() == 2) && (params[1] == "FORCE"))
970 if ((delta < -600) || (delta > 600))
972 this->Instance->SNO->WriteToSnoMask('l',"\2ERROR\2: Your clocks are out by %d seconds (this is more than ten minutes). Link aborted, \2PLEASE SYNC YOUR CLOCKS!\2",abs(delta));
973 this->WriteLine("ERROR :Your clocks are out by "+ConvToStr(abs(delta))+" seconds (this is more than ten minutes). Link aborted, PLEASE SYNC YOUR CLOCKS!");
977 if (force || (us > them))
979 this->Instance->SetTimeDelta(them - us);
980 // Send this new timestamp to any other servers
981 Utils->DoOneToMany(Utils->TreeRoot->GetName(), "TIMESET", params);
985 // Override the timestamp
986 this->WriteLine(":" + Utils->TreeRoot->GetName() + " TIMESET " + ConvToStr(us));
989 this->LinkState = CONNECTED;
990 Node = new TreeServer(this->Utils,this->Instance,InboundServerName,InboundDescription,Utils->TreeRoot,this);
991 Utils->TreeRoot->AddChild(Node);
993 params.push_back(InboundServerName);
994 params.push_back("*");
995 params.push_back("1");
996 params.push_back(":"+InboundDescription);
997 Utils->DoOneToAllButSender(Utils->TreeRoot->GetName(),"SERVER",params,InboundServerName);
998 this->bursting = true;
1001 else if (command == "ERROR")
1003 return this->Error(params);
1005 else if (command == "CAPAB")
1007 return this->Capab(params);
1012 this->WriteLine("ERROR :Internal error -- listening socket accepted its own descriptor!!!");
1016 if (command == "SERVER")
1018 // another server we connected to, which was in WAIT_AUTH_1 state,
1019 // has just sent us their credentials. If we get this far, theyre
1020 // happy with OUR credentials, and they are now in WAIT_AUTH_2 state.
1021 // if we're happy with this, we should send our netburst which
1022 // kickstarts the merge.
1023 return this->Outbound_Reply_Server(params);
1025 else if (command == "ERROR")
1027 return this->Error(params);
1031 // This is the 'authenticated' state, when all passwords
1032 // have been exchanged and anything past this point is taken
1037 std::string direction = prefix;
1038 userrec* t = this->Instance->FindNick(prefix);
1041 direction = t->server;
1043 TreeServer* route_back_again = Utils->BestRouteTo(direction);
1044 if ((!route_back_again) || (route_back_again->GetSocket() != this))
1046 if (route_back_again)
1047 Instance->Log(DEBUG,"Protocol violation: Fake direction in command '%s' from connection '%s'",line.c_str(),this->GetName().c_str());
1051 * When there is activity on the socket, reset the ping counter so
1052 * that we're not wasting bandwidth pinging an active server.
1054 route_back_again->SetNextPingTime(time(NULL) + 60);
1055 route_back_again->SetPingFlag();
1058 if (command == "SVSMODE")
1060 /* Services expects us to implement
1061 * SVSMODE. In inspircd its the same as
1066 std::string target = "";
1067 /* Yes, know, this is a mess. Its reasonably fast though as we're
1068 * working with std::string here.
1070 if ((command == "NICK") && (params.size() > 1))
1072 return this->IntroduceClient(prefix,params);
1074 else if (command == "FJOIN")
1076 return this->ForceJoin(prefix,params);
1078 else if (command == "STATS")
1080 return this->Stats(prefix, params);
1082 else if (command == "MOTD")
1084 return this->Motd(prefix, params);
1086 else if (command == "ADMIN")
1088 return this->Admin(prefix, params);
1090 else if (command == "SERVER")
1092 return this->RemoteServer(prefix,params);
1094 else if (command == "ERROR")
1096 return this->Error(params);
1098 else if (command == "OPERTYPE")
1100 return this->OperType(prefix,params);
1102 else if (command == "FMODE")
1104 return this->ForceMode(prefix,params);
1106 else if (command == "KILL")
1108 return this->RemoteKill(prefix,params);
1110 else if (command == "FTOPIC")
1112 return this->ForceTopic(prefix,params);
1114 else if (command == "REHASH")
1116 return this->RemoteRehash(prefix,params);
1118 else if (command == "METADATA")
1120 return this->MetaData(prefix,params);
1122 else if (command == "REMSTATUS")
1124 return this->RemoveStatus(prefix,params);
1126 else if (command == "PING")
1129 * We just got a ping from a server that's bursting.
1130 * This can't be right, so set them to not bursting, and
1131 * apply their lines.
1135 this->bursting = false;
1136 Instance->XLines->apply_lines(Utils->lines_to_apply);
1137 Utils->lines_to_apply = 0;
1141 prefix = this->GetName();
1143 return this->LocalPing(prefix,params);
1145 else if (command == "PONG")
1148 * We just got a pong from a server that's bursting.
1149 * This can't be right, so set them to not bursting, and
1150 * apply their lines.
1154 this->bursting = false;
1155 Instance->XLines->apply_lines(Utils->lines_to_apply);
1156 Utils->lines_to_apply = 0;
1160 prefix = this->GetName();
1162 return this->LocalPong(prefix,params);
1164 else if (command == "VERSION")
1166 return this->ServerVersion(prefix,params);
1168 else if (command == "FHOST")
1170 return this->ChangeHost(prefix,params);
1172 else if (command == "FNAME")
1174 return this->ChangeName(prefix,params);
1176 else if (command == "ADDLINE")
1178 return this->AddLine(prefix,params);
1180 else if (command == "SVSNICK")
1184 prefix = this->GetName();
1186 return this->ForceNick(prefix,params);
1188 else if (command == "RSQUIT")
1190 return this->RemoteSquit(prefix, params);
1192 else if (command == "IDLE")
1194 return this->Whois(prefix,params);
1196 else if (command == "PUSH")
1198 return this->Push(prefix,params);
1200 else if (command == "TIMESET")
1202 return this->HandleSetTime(prefix, params);
1204 else if (command == "TIME")
1206 return this->Time(prefix,params);
1208 else if ((command == "KICK") && (Utils->IsServer(prefix)))
1210 std::string sourceserv = this->myhost;
1211 if (params.size() == 3)
1213 userrec* user = this->Instance->FindNick(params[1]);
1214 chanrec* chan = this->Instance->FindChan(params[0]);
1217 if (!chan->ServerKickUser(user, params[2].c_str(), false))
1218 /* Yikes, the channels gone! */
1222 if (this->InboundServerName != "")
1224 sourceserv = this->InboundServerName;
1226 return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1228 else if (command == "SVSJOIN")
1232 prefix = this->GetName();
1234 return this->ServiceJoin(prefix,params);
1236 else if (command == "SQUIT")
1238 if (params.size() == 2)
1240 this->Squit(Utils->FindServer(params[0]),params[1]);
1244 else if (command == "OPERNOTICE")
1246 std::string sourceserv = this->myhost;
1247 if (this->InboundServerName != "")
1248 sourceserv = this->InboundServerName;
1249 if (params.size() >= 1)
1250 Instance->WriteOpers("*** From " + sourceserv + ": " + params[0]);
1251 return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params);
1253 else if (command == "MODENOTICE")
1255 std::string sourceserv = this->myhost;
1256 if (this->InboundServerName != "")
1257 sourceserv = this->InboundServerName;
1258 if (params.size() >= 2)
1260 Instance->WriteMode(params[0].c_str(), WM_AND, "*** From %s: %s", sourceserv.c_str(), params[1].c_str());
1262 return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params);
1264 else if (command == "SNONOTICE")
1266 std::string sourceserv = this->myhost;
1267 if (this->InboundServerName != "")
1268 sourceserv = this->InboundServerName;
1269 if (params.size() >= 2)
1271 Instance->SNO->WriteToSnoMask(*(params[0].c_str()), "From " + sourceserv + ": "+ params[1]);
1273 return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params);
1275 else if (command == "ENDBURST")
1277 this->bursting = false;
1278 Instance->XLines->apply_lines(Utils->lines_to_apply);
1279 Utils->lines_to_apply = 0;
1280 std::string sourceserv = this->myhost;
1281 if (this->InboundServerName != "")
1283 sourceserv = this->InboundServerName;
1285 this->Instance->SNO->WriteToSnoMask('l',"Received end of netburst from \2%s\2",sourceserv.c_str());
1287 Event rmode((char*)sourceserv.c_str(), (Module*)Utils->Creator, "new_server");
1288 rmode.Send(Instance);
1294 // not a special inter-server command.
1295 // Emulate the actual user doing the command,
1296 // this saves us having a huge ugly parser.
1297 userrec* who = this->Instance->FindNick(prefix);
1298 std::string sourceserv = this->myhost;
1299 if (this->InboundServerName != "")
1301 sourceserv = this->InboundServerName;
1303 if ((!who) && (command == "MODE"))
1305 if (Utils->IsServer(prefix))
1307 const char* modelist[127];
1308 for (size_t i = 0; i < params.size(); i++)
1309 modelist[i] = params[i].c_str();
1310 userrec* fake = new userrec(Instance);
1311 fake->SetFd(FD_MAGIC_NUMBER);
1312 this->Instance->SendMode(modelist, params.size(), fake);
1315 /* Hot potato! pass it on! */
1316 return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1321 if ((command == "NICK") && (params.size() > 0))
1323 /* On nick messages, check that the nick doesnt
1324 * already exist here. If it does, kill their copy,
1327 userrec* x = this->Instance->FindNick(params[0]);
1328 if ((x) && (x != who))
1330 std::deque<std::string> p;
1331 p.push_back(params[0]);
1332 p.push_back("Nickname collision ("+prefix+" -> "+params[0]+")");
1333 Utils->DoOneToMany(this->Instance->Config->ServerName,"KILL",p);
1335 p.push_back(prefix);
1336 p.push_back("Nickname collision");
1337 Utils->DoOneToMany(this->Instance->Config->ServerName,"KILL",p);
1338 userrec::QuitUser(this->Instance,x,"Nickname collision ("+prefix+" -> "+params[0]+")");
1339 userrec* y = this->Instance->FindNick(prefix);
1342 userrec::QuitUser(this->Instance,y,"Nickname collision");
1344 return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1348 target = who->server;
1349 const char* strparams[127];
1350 for (unsigned int q = 0; q < params.size(); q++)
1352 strparams[q] = params[q].c_str();
1354 switch (this->Instance->CallCommandHandler(command.c_str(), strparams, params.size(), who))
1357 this->WriteLine("ERROR :Unrecognised command '"+std::string(command.c_str())+"' -- possibly loaded mismatched modules");
1364 /* CMD_SUCCESS and CMD_USER_DELETED fall through here */
1370 // its not a user. Its either a server, or somethings screwed up.
1371 if (Utils->IsServer(prefix))
1372 target = this->Instance->Config->ServerName;
1376 return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1385 std::string TreeSocket::GetName()
1387 std::string sourceserv = this->myhost;
1388 if (this->InboundServerName != "")
1390 sourceserv = this->InboundServerName;
1395 void TreeSocket::OnTimeout()
1397 if (this->LinkState == CONNECTING)
1399 this->Instance->SNO->WriteToSnoMask('l',"CONNECT: Connection to \002"+myhost+"\002 timed out.");
1400 Link* MyLink = Utils->FindLink(myhost);
1402 Utils->DoFailOver(MyLink);
1406 void TreeSocket::OnClose()
1408 // Connection closed.
1409 // If the connection is fully up (state CONNECTED)
1410 // then propogate a netsplit to all peers.
1411 std::string quitserver = this->myhost;
1412 if (this->InboundServerName != "")
1414 quitserver = this->InboundServerName;
1416 TreeServer* s = Utils->FindServer(quitserver);
1419 Squit(s,"Remote host closed the connection");
1422 if (quitserver != "")
1423 this->Instance->SNO->WriteToSnoMask('l',"Connection to '\2%s\2' failed.",quitserver.c_str());
1426 int TreeSocket::OnIncomingConnection(int newsock, char* ip)
1428 /* To prevent anyone from attempting to flood opers/DDoS by connecting to the server port,
1429 * or discovering if this port is the server port, we don't allow connections from any
1430 * IPs for which we don't have a link block.
1434 found = (std::find(Utils->ValidIPs.begin(), Utils->ValidIPs.end(), ip) != Utils->ValidIPs.end());
1437 for (vector<std::string>::iterator i = Utils->ValidIPs.begin(); i != Utils->ValidIPs.end(); i++)
1438 if (irc::sockets::MatchCIDR(ip, (*i).c_str()))
1443 this->Instance->SNO->WriteToSnoMask('l',"Server connection from %s denied (no link blocks with that IP address)", ip);
1449 TreeSocket* s = new TreeSocket(this->Utils, this->Instance, newsock, ip, this->Hook);
1450 s = s; /* Whinge whinge whinge, thats all GCC ever does. */