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 bool TreeSocket::Modules(const std::string &prefix, std::deque<std::string> ¶ms)
46 if (!this->Instance->MatchText(this->Instance->Config->ServerName, params[0]))
48 /* Pass it on, not for us */
49 Utils->DoOneToOne(prefix, "MODULES", params, params[0]);
54 std::deque<std::string> par;
55 par.push_back(prefix);
58 userrec* source = this->Instance->FindNick(prefix);
62 for (unsigned int i = 0; i < Instance->Config->module_names.size(); i++)
64 Version V = Instance->modules[i]->GetVersion();
65 char modulename[MAXBUF];
66 char flagstate[MAXBUF];
68 if (V.Flags & VF_STATIC)
69 strlcat(flagstate,", static",MAXBUF);
70 if (V.Flags & VF_VENDOR)
71 strlcat(flagstate,", vendor",MAXBUF);
72 if (V.Flags & VF_COMMON)
73 strlcat(flagstate,", common",MAXBUF);
74 if (V.Flags & VF_SERVICEPROVIDER)
75 strlcat(flagstate,", service provider",MAXBUF);
77 strcpy(flagstate," <no flags>");
78 strlcpy(modulename,Instance->Config->module_names[i].c_str(),256);
81 snprintf(strbuf, MAXBUF, "::%s 900 %s :0x%08lx %d.%d.%d.%d %s (%s)",Instance->Config->ServerName,source->nick,(long unsigned int)Instance->modules[i],V.Major,V.Minor,V.Revision,V.Build,ServerConfig::CleanFilename(modulename),flagstate+2);
85 snprintf(strbuf, MAXBUF, "::%s 900 %s :%s",Instance->Config->ServerName,source->nick,ServerConfig::CleanFilename(modulename));
88 Utils->DoOneToOne(Instance->Config->ServerName, "PUSH", par, source->server);
90 snprintf(strbuf, MAXBUF, "::%s 901 %s :End of MODULES list", Instance->Config->ServerName, source->nick);
92 Utils->DoOneToOne(Instance->Config->ServerName, "PUSH", par, source->server);
96 /** remote MOTD. leet, huh? */
97 bool TreeSocket::Motd(const std::string &prefix, std::deque<std::string> ¶ms)
99 if (params.size() > 0)
101 if (this->Instance->MatchText(this->Instance->Config->ServerName, params[0]))
103 /* It's for our server */
105 userrec* source = this->Instance->FindNick(prefix);
109 std::deque<std::string> par;
110 par.push_back(prefix);
113 if (!Instance->Config->MOTD.size())
115 par[1] = std::string("::")+Instance->Config->ServerName+" 422 "+source->nick+" :Message of the day file is missing.";
116 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
120 par[1] = std::string("::")+Instance->Config->ServerName+" 375 "+source->nick+" :"+Instance->Config->ServerName+" message of the day";
121 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
123 for (unsigned int i = 0; i < Instance->Config->MOTD.size(); i++)
125 par[1] = std::string("::")+Instance->Config->ServerName+" 372 "+source->nick+" :- "+Instance->Config->MOTD[i];
126 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
129 par[1] = std::string("::")+Instance->Config->ServerName+" 376 "+source->nick+" End of message of the day.";
130 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
136 userrec* source = this->Instance->FindNick(prefix);
138 Utils->DoOneToOne(prefix, "MOTD", params, params[0]);
144 /** remote ADMIN. leet, huh? */
145 bool TreeSocket::Admin(const std::string &prefix, std::deque<std::string> ¶ms)
147 if (params.size() > 0)
149 if (this->Instance->MatchText(this->Instance->Config->ServerName, params[0]))
151 /* It's for our server */
153 userrec* source = this->Instance->FindNick(prefix);
156 std::deque<std::string> par;
157 par.push_back(prefix);
159 par[1] = std::string("::")+Instance->Config->ServerName+" 256 "+source->nick+" :Administrative info for "+Instance->Config->ServerName;
160 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
161 par[1] = std::string("::")+Instance->Config->ServerName+" 257 "+source->nick+" :Name - "+Instance->Config->AdminName;
162 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
163 par[1] = std::string("::")+Instance->Config->ServerName+" 258 "+source->nick+" :Nickname - "+Instance->Config->AdminNick;
164 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
165 par[1] = std::string("::")+Instance->Config->ServerName+" 258 "+source->nick+" :E-Mail - "+Instance->Config->AdminEmail;
166 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
172 userrec* source = this->Instance->FindNick(prefix);
174 Utils->DoOneToOne(prefix, "ADMIN", params, params[0]);
180 bool TreeSocket::Stats(const std::string &prefix, std::deque<std::string> ¶ms)
182 /* Get the reply to a STATS query if it matches this servername,
183 * and send it back as a load of PUSH queries
185 if (params.size() > 1)
187 if (this->Instance->MatchText(this->Instance->Config->ServerName, params[1]))
189 /* It's for our server */
191 userrec* source = this->Instance->FindNick(prefix);
194 std::deque<std::string> par;
195 par.push_back(prefix);
197 DoStats(this->Instance, *(params[0].c_str()), source, results);
198 for (size_t i = 0; i < results.size(); i++)
200 par[1] = "::" + results[i];
201 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
208 userrec* source = this->Instance->FindNick(prefix);
210 Utils->DoOneToOne(prefix, "STATS", params, params[1]);
217 /** Because the core won't let users or even SERVERS set +o,
218 * we use the OPERTYPE command to do this.
220 bool TreeSocket::OperType(const std::string &prefix, std::deque<std::string> ¶ms)
222 if (params.size() != 1)
224 std::string opertype = params[0];
225 userrec* u = this->Instance->FindNick(prefix);
228 u->modes[UM_OPERATOR] = 1;
229 this->Instance->all_opers.push_back(u);
230 strlcpy(u->oper,opertype.c_str(),NICKMAX-1);
231 Utils->DoOneToAllButSender(u->nick,"OPERTYPE",params,u->server);
232 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()));
237 /** Because Andy insists that services-compatible servers must
238 * implement SVSNICK and SVSJOIN, that's exactly what we do :p
240 bool TreeSocket::ForceNick(const std::string &prefix, std::deque<std::string> ¶ms)
242 if (params.size() < 3)
245 userrec* u = this->Instance->FindNick(params[0]);
249 Utils->DoOneToAllButSender(prefix,"SVSNICK",params,prefix);
252 std::deque<std::string> par;
253 par.push_back(params[1]);
254 if (!u->ForceNickChange(params[1].c_str()))
256 userrec::QuitUser(this->Instance, u, "Nickname collision");
259 u->age = atoi(params[2].c_str());
265 bool TreeSocket::OperQuit(const std::string &prefix, std::deque<std::string> ¶ms)
267 if (params.size() < 1)
270 userrec* u = this->Instance->FindNick(prefix);
274 Utils->DoOneToAllButSender(prefix,"OPERQUIT",params,prefix);
275 u->SetOperQuit(params[0]);
281 * Remote SQUIT (RSQUIT). Routing works similar to SVSNICK: Route it to the server that the target is connected to locally,
282 * then let that server do the dirty work (squit it!). Example:
283 * 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
285 bool TreeSocket::RemoteSquit(const std::string &prefix, std::deque<std::string> ¶ms)
287 /* ok.. :w00t RSQUIT jupe.barafranca.com :reason here */
288 if (params.size() < 2)
291 TreeServer* s = Utils->FindServerMask(params[0]);
295 if (s == Utils->TreeRoot)
297 this->Instance->SNO->WriteToSnoMask('l',"What the fuck, I recieved a remote SQUIT for myself? :< (from %s", prefix.c_str());
301 TreeSocket* sock = s->GetSocket();
305 /* it's locally connected, KILL IT! */
306 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());
307 sock->Squit(s,"Server quit by " + prefix + ": " + params[1]);
308 Instance->SE->DelFd(sock);
314 /* route the rsquit */
315 params[1] = ":" + params[1];
316 Utils->DoOneToOne(prefix, "RSQUIT", params, params[0]);
321 /* mother fucker! it doesn't exist */
327 bool TreeSocket::ServiceJoin(const std::string &prefix, std::deque<std::string> ¶ms)
329 if (params.size() < 2)
332 userrec* u = this->Instance->FindNick(params[0]);
336 /* only join if it's local, otherwise just pass it on! */
338 chanrec::JoinUser(this->Instance, u, params[1].c_str(), false, "", Instance->Time());
339 Utils->DoOneToAllButSender(prefix,"SVSJOIN",params,prefix);
344 bool TreeSocket::RemoteRehash(const std::string &prefix, std::deque<std::string> ¶ms)
346 if (params.size() < 1)
349 std::string servermask = params[0];
351 if (this->Instance->MatchText(this->Instance->Config->ServerName,servermask))
353 this->Instance->SNO->WriteToSnoMask('l',"Remote rehash initiated by \002"+prefix+"\002.");
354 this->Instance->RehashServer();
355 Utils->ReadConfiguration(false);
356 InitializeDisabledCommands(Instance->Config->DisabledCommands, Instance);
358 Utils->DoOneToAllButSender(prefix,"REHASH",params,prefix);
362 bool TreeSocket::RemoteKill(const std::string &prefix, std::deque<std::string> ¶ms)
364 if (params.size() != 2)
367 std::string nick = params[0];
368 userrec* u = this->Instance->FindNick(prefix);
369 userrec* who = this->Instance->FindNick(nick);
373 /* Prepend kill source, if we don't have one */
374 std::string sourceserv = prefix;
377 sourceserv = u->server;
379 if (*(params[1].c_str()) != '[')
381 params[1] = "[" + sourceserv + "] Killed (" + params[1] +")";
383 std::string reason = params[1];
384 params[1] = ":" + params[1];
385 Utils->DoOneToAllButSender(prefix,"KILL",params,sourceserv);
386 who->Write(":%s KILL %s :%s (%s)", sourceserv.c_str(), who->nick, sourceserv.c_str(), reason.c_str());
387 userrec::QuitUser(this->Instance,who,reason);
392 bool TreeSocket::LocalPong(const std::string &prefix, std::deque<std::string> ¶ms)
394 if (params.size() < 1)
397 if (params.size() == 1)
399 TreeServer* ServerSource = Utils->FindServer(prefix);
402 ServerSource->SetPingFlag();
403 ServerSource->rtt = Instance->Time() - ServerSource->LastPing;
408 std::string forwardto = params[1];
409 if (forwardto == this->Instance->Config->ServerName)
412 * this is a PONG for us
413 * if the prefix is a user, check theyre local, and if they are,
414 * dump the PONG reply back to their fd. If its a server, do nowt.
415 * Services might want to send these s->s, but we dont need to yet.
417 userrec* u = this->Instance->FindNick(prefix);
420 u->WriteServ("PONG %s %s",params[0].c_str(),params[1].c_str());
425 // not for us, pass it on :)
426 Utils->DoOneToOne(prefix,"PONG",params,forwardto);
433 bool TreeSocket::MetaData(const std::string &prefix, std::deque<std::string> ¶ms)
435 if (params.size() < 2)
438 params.push_back("");
439 TreeServer* ServerSource = Utils->FindServer(prefix);
442 if (params[0] == "*")
444 FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_OTHER,NULL,params[1],params[2]));
446 else if (*(params[0].c_str()) == '#')
448 chanrec* c = this->Instance->FindChan(params[0]);
451 FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_CHANNEL,c,params[1],params[2]));
454 else if (*(params[0].c_str()) != '#')
456 userrec* u = this->Instance->FindNick(params[0]);
459 FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_USER,u,params[1],params[2]));
464 params[2] = ":" + params[2];
465 Utils->DoOneToAllButSender(prefix,"METADATA",params,prefix);
469 bool TreeSocket::ServerVersion(const std::string &prefix, std::deque<std::string> ¶ms)
471 if (params.size() < 1)
474 TreeServer* ServerSource = Utils->FindServer(prefix);
478 ServerSource->SetVersion(params[0]);
480 params[0] = ":" + params[0];
481 Utils->DoOneToAllButSender(prefix,"VERSION",params,prefix);
485 bool TreeSocket::ChangeHost(const std::string &prefix, std::deque<std::string> ¶ms)
487 if (params.size() < 1)
489 userrec* u = this->Instance->FindNick(prefix);
493 u->ChangeDisplayedHost(params[0].c_str());
494 Utils->DoOneToAllButSender(prefix,"FHOST",params,u->server);
499 bool TreeSocket::AddLine(const std::string &prefix, std::deque<std::string> ¶ms)
501 if (params.size() < 6)
503 bool propogate = false;
505 Utils->lines_to_apply = 0;
506 switch (*(params[0].c_str()))
509 propogate = Instance->XLines->add_zline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
510 Instance->XLines->zline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
512 Utils->lines_to_apply |= APPLY_ZLINES;
515 propogate = Instance->XLines->add_qline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
516 Instance->XLines->qline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
518 Utils->lines_to_apply |= APPLY_QLINES;
521 propogate = Instance->XLines->add_eline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
522 Instance->XLines->eline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
525 propogate = Instance->XLines->add_gline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
526 Instance->XLines->gline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
528 Utils->lines_to_apply |= APPLY_GLINES;
531 propogate = Instance->XLines->add_kline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
533 Utils->lines_to_apply |= APPLY_KLINES;
536 /* Just in case... */
537 this->Instance->SNO->WriteToSnoMask('x',"\2WARNING\2: Invalid xline type '"+params[0]+"' sent by server "+prefix+", ignored!");
541 /* Send it on its way */
544 if (atoi(params[4].c_str()))
546 time_t c_requires_crap = ConvToInt(params[4]) + Instance->Time();
547 this->Instance->SNO->WriteToSnoMask('x',"%s Added %cLINE on %s to expire on %s (%s).",prefix.c_str(),*(params[0].c_str()),params[1].c_str(),ctime(&c_requires_crap),params[5].c_str());
551 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());
553 params[5] = ":" + params[5];
554 Utils->DoOneToAllButSender(prefix,"ADDLINE",params,prefix);
558 Instance->XLines->apply_lines(Utils->lines_to_apply);
559 Utils->lines_to_apply = 0;
564 bool TreeSocket::ChangeName(const std::string &prefix, std::deque<std::string> ¶ms)
566 if (params.size() < 1)
568 userrec* u = this->Instance->FindNick(prefix);
571 u->ChangeName(params[0].c_str());
572 params[0] = ":" + params[0];
573 Utils->DoOneToAllButSender(prefix,"FNAME",params,u->server);
578 bool TreeSocket::Whois(const std::string &prefix, std::deque<std::string> ¶ms)
580 if (params.size() < 1)
582 userrec* u = this->Instance->FindNick(prefix);
585 // an incoming request
586 if (params.size() == 1)
588 userrec* x = this->Instance->FindNick(params[0]);
589 if ((x) && (IS_LOCAL(x)))
591 userrec* x = this->Instance->FindNick(params[0]);
594 snprintf(signon,MAXBUF,"%lu",(unsigned long)x->signon);
595 snprintf(idle,MAXBUF,"%lu",(unsigned long)abs((x->idle_lastmsg)-Instance->Time(true)));
596 std::deque<std::string> par;
597 par.push_back(prefix);
598 par.push_back(signon);
600 // ours, we're done, pass it BACK
601 Utils->DoOneToOne(params[0],"IDLE",par,u->server);
605 // not ours pass it on
606 Utils->DoOneToOne(prefix,"IDLE",params,x->server);
609 else if (params.size() == 3)
611 std::string who_did_the_whois = params[0];
612 userrec* who_to_send_to = this->Instance->FindNick(who_did_the_whois);
613 if ((who_to_send_to) && (IS_LOCAL(who_to_send_to)))
615 // an incoming reply to a whois we sent out
616 std::string nick_whoised = prefix;
617 unsigned long signon = atoi(params[1].c_str());
618 unsigned long idle = atoi(params[2].c_str());
619 if ((who_to_send_to) && (IS_LOCAL(who_to_send_to)))
620 do_whois(this->Instance,who_to_send_to,u,signon,idle,nick_whoised.c_str());
624 // not ours, pass it on
625 Utils->DoOneToOne(prefix,"IDLE",params,who_to_send_to->server);
632 bool TreeSocket::Push(const std::string &prefix, std::deque<std::string> ¶ms)
634 if (params.size() < 2)
636 userrec* u = this->Instance->FindNick(params[0]);
645 // continue the raw onwards
646 params[1] = ":" + params[1];
647 Utils->DoOneToOne(prefix,"PUSH",params,u->server);
652 bool TreeSocket::HandleSetTime(const std::string &prefix, std::deque<std::string> ¶ms)
654 if (!params.size() || !Utils->EnableTimeSync)
659 if ((params.size() == 2) && (params[1] == "FORCE"))
662 time_t them = atoi(params[0].c_str());
663 time_t us = Instance->Time(false);
665 time_t diff = them - us;
667 Utils->DoOneToAllButSender(prefix, "TIMESET", params, prefix);
669 if (force || (them != us))
671 time_t old = Instance->SetTimeDelta(diff);
672 Instance->Log(DEBUG, "TS (diff %d) from %s applied (old delta was %d)", diff, prefix.c_str(), old);
678 bool TreeSocket::Time(const std::string &prefix, std::deque<std::string> ¶ms)
680 // :source.server TIME remote.server sendernick
681 // :remote.server TIME source.server sendernick TS
682 if (params.size() == 2)
684 // someone querying our time?
685 if (this->Instance->Config->ServerName == params[0])
687 userrec* u = this->Instance->FindNick(params[1]);
690 params.push_back(ConvToStr(Instance->Time(false)));
692 Utils->DoOneToOne(this->Instance->Config->ServerName,"TIME",params,params[0]);
697 // not us, pass it on
698 userrec* u = this->Instance->FindNick(params[1]);
700 Utils->DoOneToOne(prefix,"TIME",params,params[0]);
703 else if (params.size() == 3)
705 // a response to a previous TIME
706 userrec* u = this->Instance->FindNick(params[1]);
707 if ((u) && (IS_LOCAL(u)))
709 time_t rawtime = atol(params[2].c_str());
710 struct tm * timeinfo;
711 timeinfo = localtime(&rawtime);
713 snprintf(tms,26,"%s",asctime(timeinfo));
715 u->WriteServ("391 %s %s :%s",u->nick,prefix.c_str(),tms);
720 Utils->DoOneToOne(prefix,"TIME",params,u->server);
726 bool TreeSocket::LocalPing(const std::string &prefix, std::deque<std::string> ¶ms)
728 if (params.size() < 1)
730 if (params.size() == 1)
732 std::string stufftobounce = params[0];
733 this->WriteLine(std::string(":")+this->Instance->Config->ServerName+" PONG "+stufftobounce);
738 std::string forwardto = params[1];
739 if (forwardto == this->Instance->Config->ServerName)
741 // this is a ping for us, send back PONG to the requesting server
742 params[1] = params[0];
743 params[0] = forwardto;
744 Utils->DoOneToOne(forwardto,"PONG",params,params[1]);
748 // not for us, pass it on :)
749 Utils->DoOneToOne(prefix,"PING",params,forwardto);
755 /** TODO: This creates a total mess of output and needs to really use irc::modestacker.
757 bool TreeSocket::RemoveStatus(const std::string &prefix, std::deque<std::string> ¶ms)
759 if (params.size() < 1)
761 chanrec* c = Instance->FindChan(params[0]);
764 for (char modeletter = 'A'; modeletter <= 'z'; modeletter++)
766 ModeHandler* mh = Instance->Modes->FindMode(modeletter, MODETYPE_CHANNEL);
774 bool TreeSocket::RemoteServer(const std::string &prefix, std::deque<std::string> ¶ms)
776 if (params.size() < 4)
778 std::string servername = params[0];
779 std::string password = params[1];
780 // hopcount is not used for a remote server, we calculate this ourselves
781 std::string description = params[3];
782 TreeServer* ParentOfThis = Utils->FindServer(prefix);
785 this->WriteLine("ERROR :Protocol error - Introduced remote server from unknown server "+prefix);
788 TreeServer* CheckDupe = Utils->FindServer(servername);
791 this->WriteLine("ERROR :Server "+servername+" already exists!");
792 this->Instance->SNO->WriteToSnoMask('l',"Server \2"+servername+"\2 being introduced from \2" + prefix + "\2 denied, already exists. Closing link with " + prefix);
795 Link* lnk = Utils->FindLink(servername);
796 TreeServer* Node = new TreeServer(this->Utils,this->Instance,servername,description,ParentOfThis,NULL, lnk ? lnk->Hidden : false);
797 ParentOfThis->AddChild(Node);
798 params[3] = ":" + params[3];
799 Utils->DoOneToAllButSender(prefix,"SERVER",params,prefix);
800 this->Instance->SNO->WriteToSnoMask('l',"Server \002"+prefix+"\002 introduced server \002"+servername+"\002 ("+description+")");
804 bool TreeSocket::Outbound_Reply_Server(std::deque<std::string> ¶ms)
806 if (params.size() < 4)
809 irc::string servername = params[0].c_str();
810 std::string sname = params[0];
811 std::string password = params[1];
812 int hops = atoi(params[2].c_str());
816 this->WriteLine("ERROR :Server too far away for authentication");
817 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
820 std::string description = params[3];
821 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
823 if ((x->Name == servername) && (x->RecvPass == password))
825 TreeServer* CheckDupe = Utils->FindServer(sname);
828 this->WriteLine("ERROR :Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
829 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
832 // Begin the sync here. this kickstarts the
833 // other side, waiting in WAIT_AUTH_2 state,
834 // into starting their burst, as it shows
836 this->LinkState = CONNECTED;
837 // we should add the details of this server now
838 // to the servers tree, as a child of the root
840 TreeServer* Node = new TreeServer(this->Utils,this->Instance,sname,description,Utils->TreeRoot,this,x->Hidden);
841 Utils->TreeRoot->AddChild(Node);
842 params[3] = ":" + params[3];
843 Utils->DoOneToAllButSender(Utils->TreeRoot->GetName(),"SERVER",params,sname);
844 this->bursting = true;
849 this->WriteLine("ERROR :Invalid credentials");
850 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
854 bool TreeSocket::Inbound_Server(std::deque<std::string> ¶ms)
856 if (params.size() < 4)
858 irc::string servername = params[0].c_str();
859 std::string sname = params[0];
860 std::string password = params[1];
861 int hops = atoi(params[2].c_str());
865 this->WriteLine("ERROR :Server too far away for authentication");
866 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
869 std::string description = params[3];
870 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
872 if ((x->Name == servername) && (x->RecvPass == password))
874 TreeServer* CheckDupe = Utils->FindServer(sname);
877 this->WriteLine("ERROR :Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
878 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
881 this->Instance->SNO->WriteToSnoMask('l',"Verified incoming server connection from \002"+sname+"\002["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] ("+description+")");
884 std::string name = InspSocketNameRequest((Module*)Utils->Creator, this->Hook).Send();
885 this->Instance->SNO->WriteToSnoMask('l',"Connection from \2"+sname+"\2["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] using transport \2"+name+"\2");
888 this->InboundServerName = sname;
889 this->InboundDescription = description;
890 // this is good. Send our details: Our server name and description and hopcount of 0,
891 // along with the sendpass from this block.
892 this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+x->SendPass+" 0 :"+this->Instance->Config->ServerDesc);
893 // move to the next state, we are now waiting for THEM.
894 this->LinkState = WAIT_AUTH_2;
898 this->WriteLine("ERROR :Invalid credentials");
899 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
903 void TreeSocket::Split(const std::string &line, std::deque<std::string> &n)
906 irc::tokenstream tokens(line);
908 while (tokens.GetToken(param))
913 bool TreeSocket::ProcessLine(std::string &line)
915 std::deque<std::string> params;
919 line = line.substr(0, line.find_first_of("\r\n"));
924 Instance->Log(DEBUG, "<- %s", line.c_str());
926 this->Split(line.c_str(),params);
928 if ((params[0][0] == ':') && (params.size() > 1))
930 prefix = params[0].substr(1);
933 command = params[0].c_str();
935 switch (this->LinkState)
940 // Waiting for SERVER command from remote server. Server initiating
941 // the connection sends the first SERVER command, listening server
942 // replies with theirs if its happy, then if the initiator is happy,
943 // it starts to send its net sync, which starts the merge, otherwise
944 // it sends an ERROR.
945 if (command == "PASS")
947 /* Silently ignored */
949 else if (command == "SERVER")
951 return this->Inbound_Server(params);
953 else if (command == "ERROR")
955 return this->Error(params);
957 else if (command == "USER")
959 this->WriteLine("ERROR :Client connections to this port are prohibited.");
962 else if (command == "CAPAB")
964 return this->Capab(params);
966 else if ((command == "U") || (command == "S"))
968 this->WriteLine("ERROR :Cannot use the old-style mesh linking protocol with m_spanningtree.so!");
973 std::string error("ERROR :Invalid command in negotiation phase: ");
974 error.append(command.c_str());
975 this->WriteLine(error);
980 // Waiting for start of other side's netmerge to say they liked our
982 if (command == "SERVER")
984 // cant do this, they sent it to us in the WAIT_AUTH_1 state!
988 else if ((command == "U") || (command == "S"))
990 this->WriteLine("ERROR :Cannot use the old-style mesh linking protocol with m_spanningtree.so!");
993 else if (command == "BURST")
995 if (params.size() && Utils->EnableTimeSync)
997 bool we_have_delta = (Instance->Time(false) != Instance->Time(true));
998 time_t them = atoi(params[0].c_str());
999 time_t delta = them - Instance->Time(false);
1000 if ((delta < -300) || (delta > 300))
1002 Instance->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(delta));
1003 WriteLine("ERROR :Your clocks are out by "+ConvToStr(abs(delta))+" seconds (this is more than ten minutes). Link aborted, PLEASE SYNC YOUR CLOCKS!");
1006 else if ((delta < -30) || (delta > 30))
1008 Instance->SNO->WriteToSnoMask('l',"\2WARNING\2: Your clocks are out by %d seconds. Please consider synching your clocks.", abs(delta));
1011 if (!Utils->MasterTime && !we_have_delta)
1013 this->Instance->SetTimeDelta(delta);
1014 // Send this new timestamp to any other servers
1015 Utils->DoOneToMany(Utils->TreeRoot->GetName(), "TIMESET", params);
1018 this->LinkState = CONNECTED;
1019 Link* lnk = Utils->FindLink(InboundServerName);
1020 Node = new TreeServer(this->Utils,this->Instance, InboundServerName, InboundDescription, Utils->TreeRoot, this, lnk ? lnk->Hidden : false);
1021 Utils->TreeRoot->AddChild(Node);
1023 params.push_back(InboundServerName);
1024 params.push_back("*");
1025 params.push_back("1");
1026 params.push_back(":"+InboundDescription);
1027 Utils->DoOneToAllButSender(Utils->TreeRoot->GetName(),"SERVER",params,InboundServerName);
1028 this->bursting = true;
1029 this->DoBurst(Node);
1031 else if (command == "ERROR")
1033 return this->Error(params);
1035 else if (command == "CAPAB")
1037 return this->Capab(params);
1042 this->WriteLine("ERROR :Internal error -- listening socket accepted its own descriptor!!!");
1046 if (command == "SERVER")
1048 // another server we connected to, which was in WAIT_AUTH_1 state,
1049 // has just sent us their credentials. If we get this far, theyre
1050 // happy with OUR credentials, and they are now in WAIT_AUTH_2 state.
1051 // if we're happy with this, we should send our netburst which
1052 // kickstarts the merge.
1053 return this->Outbound_Reply_Server(params);
1055 else if (command == "ERROR")
1057 return this->Error(params);
1061 // This is the 'authenticated' state, when all passwords
1062 // have been exchanged and anything past this point is taken
1067 std::string direction = prefix;
1068 userrec* t = this->Instance->FindNick(prefix);
1071 direction = t->server;
1073 TreeServer* route_back_again = Utils->BestRouteTo(direction);
1074 if ((!route_back_again) || (route_back_again->GetSocket() != this))
1076 if (route_back_again)
1077 Instance->Log(DEBUG,"Protocol violation: Fake direction in command '%s' from connection '%s'",line.c_str(),this->GetName().c_str());
1081 * When there is activity on the socket, reset the ping counter so
1082 * that we're not wasting bandwidth pinging an active server.
1084 route_back_again->SetNextPingTime(time(NULL) + 60);
1085 route_back_again->SetPingFlag();
1088 if (command == "SVSMODE")
1090 /* Services expects us to implement
1091 * SVSMODE. In inspircd its the same as
1096 std::string target = "";
1097 /* Yes, know, this is a mess. Its reasonably fast though as we're
1098 * working with std::string here.
1100 if ((command == "NICK") && (params.size() >= 8))
1102 return this->IntroduceClient(prefix,params);
1104 else if (command == "FJOIN")
1106 return this->ForceJoin(prefix,params);
1108 else if (command == "STATS")
1110 return this->Stats(prefix, params);
1112 else if (command == "MOTD")
1114 return this->Motd(prefix, params);
1116 else if (command == "MODULES")
1118 return this->Modules(prefix, params);
1120 else if (command == "ADMIN")
1122 return this->Admin(prefix, params);
1124 else if (command == "SERVER")
1126 return this->RemoteServer(prefix,params);
1128 else if (command == "ERROR")
1130 return this->Error(params);
1132 else if (command == "OPERTYPE")
1134 return this->OperType(prefix,params);
1136 else if (command == "FMODE")
1138 return this->ForceMode(prefix,params);
1140 else if (command == "KILL")
1142 return this->RemoteKill(prefix,params);
1144 else if (command == "FTOPIC")
1146 return this->ForceTopic(prefix,params);
1148 else if (command == "REHASH")
1150 return this->RemoteRehash(prefix,params);
1152 else if (command == "METADATA")
1154 return this->MetaData(prefix,params);
1156 else if (command == "REMSTATUS")
1158 return this->RemoveStatus(prefix,params);
1160 else if (command == "PING")
1163 * We just got a ping from a server that's bursting.
1164 * This can't be right, so set them to not bursting, and
1165 * apply their lines.
1169 this->bursting = false;
1170 Instance->XLines->apply_lines(Utils->lines_to_apply);
1171 Utils->lines_to_apply = 0;
1175 prefix = this->GetName();
1177 return this->LocalPing(prefix,params);
1179 else if (command == "PONG")
1182 * We just got a pong from a server that's bursting.
1183 * This can't be right, so set them to not bursting, and
1184 * apply their lines.
1188 this->bursting = false;
1189 Instance->XLines->apply_lines(Utils->lines_to_apply);
1190 Utils->lines_to_apply = 0;
1194 prefix = this->GetName();
1196 return this->LocalPong(prefix,params);
1198 else if (command == "VERSION")
1200 return this->ServerVersion(prefix,params);
1202 else if (command == "FHOST")
1204 return this->ChangeHost(prefix,params);
1206 else if (command == "FNAME")
1208 return this->ChangeName(prefix,params);
1210 else if (command == "ADDLINE")
1212 return this->AddLine(prefix,params);
1214 else if (command == "SVSNICK")
1218 prefix = this->GetName();
1220 return this->ForceNick(prefix,params);
1222 else if (command == "OPERQUIT")
1224 return this->OperQuit(prefix,params);
1226 else if (command == "RSQUIT")
1228 return this->RemoteSquit(prefix, params);
1230 else if (command == "IDLE")
1232 return this->Whois(prefix,params);
1234 else if (command == "PUSH")
1236 return this->Push(prefix,params);
1238 else if (command == "TIMESET")
1240 return this->HandleSetTime(prefix, params);
1242 else if (command == "TIME")
1244 return this->Time(prefix,params);
1246 else if ((command == "KICK") && (Utils->IsServer(prefix)))
1248 std::string sourceserv = this->myhost;
1249 if (params.size() == 3)
1251 userrec* user = this->Instance->FindNick(params[1]);
1252 chanrec* chan = this->Instance->FindChan(params[0]);
1255 if (!chan->ServerKickUser(user, params[2].c_str(), false))
1256 /* Yikes, the channels gone! */
1260 if (this->InboundServerName != "")
1262 sourceserv = this->InboundServerName;
1264 return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1266 else if (command == "SVSJOIN")
1270 prefix = this->GetName();
1272 return this->ServiceJoin(prefix,params);
1274 else if (command == "SQUIT")
1276 if (params.size() == 2)
1278 this->Squit(Utils->FindServer(params[0]),params[1]);
1282 else if (command == "OPERNOTICE")
1284 std::string sourceserv = this->myhost;
1285 if (this->InboundServerName != "")
1286 sourceserv = this->InboundServerName;
1287 if (params.size() >= 1)
1288 Instance->WriteOpers("*** From " + sourceserv + ": " + params[0]);
1289 return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params);
1291 else if (command == "MODENOTICE")
1293 std::string sourceserv = this->myhost;
1294 if (this->InboundServerName != "")
1295 sourceserv = this->InboundServerName;
1296 if (params.size() >= 2)
1298 Instance->WriteMode(params[0].c_str(), WM_AND, "*** From %s: %s", sourceserv.c_str(), params[1].c_str());
1300 return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params);
1302 else if (command == "SNONOTICE")
1304 std::string sourceserv = this->myhost;
1305 if (this->InboundServerName != "")
1306 sourceserv = this->InboundServerName;
1307 if (params.size() >= 2)
1309 Instance->SNO->WriteToSnoMask(*(params[0].c_str()), "From " + sourceserv + ": "+ params[1]);
1311 return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params);
1313 else if (command == "ENDBURST")
1315 this->bursting = false;
1316 Instance->XLines->apply_lines(Utils->lines_to_apply);
1317 Utils->lines_to_apply = 0;
1318 std::string sourceserv = this->myhost;
1319 if (this->InboundServerName != "")
1321 sourceserv = this->InboundServerName;
1323 this->Instance->SNO->WriteToSnoMask('l',"Received end of netburst from \2%s\2",sourceserv.c_str());
1325 Event rmode((char*)sourceserv.c_str(), (Module*)Utils->Creator, "new_server");
1326 rmode.Send(Instance);
1332 // not a special inter-server command.
1333 // Emulate the actual user doing the command,
1334 // this saves us having a huge ugly parser.
1335 userrec* who = this->Instance->FindNick(prefix);
1336 std::string sourceserv = this->myhost;
1337 if (this->InboundServerName != "")
1339 sourceserv = this->InboundServerName;
1341 if ((!who) && (command == "MODE"))
1343 if (Utils->IsServer(prefix))
1345 const char* modelist[127];
1346 for (size_t i = 0; i < params.size(); i++)
1347 modelist[i] = params[i].c_str();
1348 userrec* fake = new userrec(Instance);
1349 fake->SetFd(FD_MAGIC_NUMBER);
1350 this->Instance->SendMode(modelist, params.size(), fake);
1353 /* Hot potato! pass it on! */
1354 return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1359 if ((command == "NICK") && (params.size() > 0))
1361 /* On nick messages, check that the nick doesnt
1362 * already exist here. If it does, kill their copy,
1365 userrec* x = this->Instance->FindNick(params[0]);
1366 if ((x) && (x != who))
1368 std::deque<std::string> p;
1369 p.push_back(params[0]);
1370 p.push_back("Nickname collision ("+prefix+" -> "+params[0]+")");
1371 Utils->DoOneToMany(this->Instance->Config->ServerName,"KILL",p);
1373 p.push_back(prefix);
1374 p.push_back("Nickname collision");
1375 Utils->DoOneToMany(this->Instance->Config->ServerName,"KILL",p);
1376 userrec::QuitUser(this->Instance,x,"Nickname collision ("+prefix+" -> "+params[0]+")");
1377 userrec* y = this->Instance->FindNick(prefix);
1380 userrec::QuitUser(this->Instance,y,"Nickname collision");
1382 return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1386 target = who->server;
1387 const char* strparams[127];
1388 for (unsigned int q = 0; q < params.size(); q++)
1390 strparams[q] = params[q].c_str();
1392 switch (this->Instance->CallCommandHandler(command.c_str(), strparams, params.size(), who))
1395 this->WriteLine("ERROR :Unrecognised command '"+std::string(command.c_str())+"' -- possibly loaded mismatched modules");
1402 /* CMD_SUCCESS and CMD_USER_DELETED fall through here */
1408 // its not a user. Its either a server, or somethings screwed up.
1409 if (Utils->IsServer(prefix))
1410 target = this->Instance->Config->ServerName;
1414 return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1423 std::string TreeSocket::GetName()
1425 std::string sourceserv = this->myhost;
1426 if (this->InboundServerName != "")
1428 sourceserv = this->InboundServerName;
1433 void TreeSocket::OnTimeout()
1435 if (this->LinkState == CONNECTING)
1437 this->Instance->SNO->WriteToSnoMask('l',"CONNECT: Connection to \002"+myhost+"\002 timed out.");
1438 Link* MyLink = Utils->FindLink(myhost);
1440 Utils->DoFailOver(MyLink);
1444 void TreeSocket::OnClose()
1446 // Connection closed.
1447 // If the connection is fully up (state CONNECTED)
1448 // then propogate a netsplit to all peers.
1449 std::string quitserver = this->myhost;
1450 if (this->InboundServerName != "")
1452 quitserver = this->InboundServerName;
1454 TreeServer* s = Utils->FindServer(quitserver);
1457 Squit(s,"Remote host closed the connection");
1460 if (quitserver != "")
1461 this->Instance->SNO->WriteToSnoMask('l',"Connection to '\2%s\2' failed.",quitserver.c_str());
1464 int TreeSocket::OnIncomingConnection(int newsock, char* ip)
1466 /* To prevent anyone from attempting to flood opers/DDoS by connecting to the server port,
1467 * or discovering if this port is the server port, we don't allow connections from any
1468 * IPs for which we don't have a link block.
1472 found = (std::find(Utils->ValidIPs.begin(), Utils->ValidIPs.end(), ip) != Utils->ValidIPs.end());
1475 for (vector<std::string>::iterator i = Utils->ValidIPs.begin(); i != Utils->ValidIPs.end(); i++)
1476 if (irc::sockets::MatchCIDR(ip, (*i).c_str()))
1481 this->Instance->SNO->WriteToSnoMask('l',"Server connection from %s denied (no link blocks with that IP address)", ip);
1487 TreeSocket* s = new TreeSocket(this->Utils, this->Instance, newsock, ip, this->Hook);
1488 s = s; /* Whinge whinge whinge, thats all GCC ever does. */