X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fm_spanningtree%2Ftreesocket2.cpp;h=f3eff8d74d3d460ea868e7a7a0900edfb97e29ef;hb=f0683739e2cfaae900a8e3ffac48ad1bc7d75e51;hp=2495077997814a661a63fc2841eea45297e5bece;hpb=b3961a3e36b20cbb1f17197203739fb52222a9ef;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/m_spanningtree/treesocket2.cpp b/src/modules/m_spanningtree/treesocket2.cpp index 249507799..f3eff8d74 100644 --- a/src/modules/m_spanningtree/treesocket2.cpp +++ b/src/modules/m_spanningtree/treesocket2.cpp @@ -2,7 +2,7 @@ * | Inspire Internet Relay Chat Daemon | * +------------------------------------+ * - * InspIRCd: (C) 2002-2007 InspIRCd Development Team + * InspIRCd: (C) 2002-2008 InspIRCd Development Team * See: http://www.inspircd.org/wiki/index.php/Credits * * This program is free but copyrighted software; see @@ -67,13 +67,16 @@ bool TreeSocket::Modules(const std::string &prefix, std::deque &par par.push_back(prefix); par.push_back(""); - userrec* source = this->Instance->FindNick(prefix); + User* source = this->Instance->FindNick(prefix); if (!source) return true; - for (unsigned int i = 0; i < Instance->Config->module_names.size(); i++) + std::vector module_names = Instance->Modules->GetAllModuleNames(0); + + for (unsigned int i = 0; i < module_names.size(); i++) { - Version V = Instance->modules[i]->GetVersion(); + Module* m = Instance->Modules->Find(module_names[i]); + Version V = m->GetVersion(); char modulename[MAXBUF]; char flagstate[MAXBUF]; *flagstate = 0; @@ -87,10 +90,11 @@ bool TreeSocket::Modules(const std::string &prefix, std::deque &par strlcat(flagstate,", service provider",MAXBUF); if (!flagstate[0]) strcpy(flagstate," "); - strlcpy(modulename,Instance->Config->module_names[i].c_str(),256); + strlcpy(modulename,module_names[i].c_str(),256); if (*source->oper) { - 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); + snprintf(strbuf, MAXBUF, "::%s 900 %s :0x%08lx %d.%d.%d.%d %s (%s)",Instance->Config->ServerName,source->nick,(unsigned long)m, + V.Major,V.Minor,V.Revision,V.Build,ServerConfig::CleanFilename(modulename),flagstate+2); } else { @@ -114,7 +118,7 @@ bool TreeSocket::Motd(const std::string &prefix, std::deque ¶ms { /* It's for our server */ string_list results; - userrec* source = this->Instance->FindNick(prefix); + User* source = this->Instance->FindNick(prefix); if (source) { @@ -145,7 +149,7 @@ bool TreeSocket::Motd(const std::string &prefix, std::deque ¶ms else { /* Pass it on */ - userrec* source = this->Instance->FindNick(prefix); + User* source = this->Instance->FindNick(prefix); if (source) Utils->DoOneToOne(prefix, "MOTD", params, params[0]); } @@ -162,7 +166,7 @@ bool TreeSocket::Admin(const std::string &prefix, std::deque ¶m { /* It's for our server */ string_list results; - userrec* source = this->Instance->FindNick(prefix); + User* source = this->Instance->FindNick(prefix); if (source) { std::deque par; @@ -181,7 +185,7 @@ bool TreeSocket::Admin(const std::string &prefix, std::deque ¶m else { /* Pass it on */ - userrec* source = this->Instance->FindNick(prefix); + User* source = this->Instance->FindNick(prefix); if (source) Utils->DoOneToOne(prefix, "ADMIN", params, params[0]); } @@ -200,7 +204,7 @@ bool TreeSocket::Stats(const std::string &prefix, std::deque ¶m { /* It's for our server */ string_list results; - userrec* source = this->Instance->FindNick(prefix); + User* source = this->Instance->FindNick(prefix); if (source) { std::deque par; @@ -217,7 +221,7 @@ bool TreeSocket::Stats(const std::string &prefix, std::deque ¶m else { /* Pass it on */ - userrec* source = this->Instance->FindNick(prefix); + User* source = this->Instance->FindNick(prefix); if (source) Utils->DoOneToOne(source->uuid, "STATS", params, params[1]); } @@ -234,11 +238,12 @@ bool TreeSocket::OperType(const std::string &prefix, std::deque &pa if (params.size() != 1) return true; std::string opertype = params[0]; - userrec* u = this->Instance->FindNick(prefix); + User* u = this->Instance->FindNick(prefix); if (u) { + if (!u->IsModeSet('o')) + this->Instance->Users->all_opers.push_back(u); u->modes[UM_OPERATOR] = 1; - this->Instance->all_opers.push_back(u); strlcpy(u->oper,opertype.c_str(),NICKMAX-1); Utils->DoOneToAllButSender(u->nick,"OPERTYPE",params,u->server); @@ -252,8 +257,7 @@ bool TreeSocket::OperType(const std::string &prefix, std::deque &pa * then do nothing. -- w00t */ if ( - this->bursting || - Utils->FindRemoteBurstServer(remoteserver) || + remoteserver->bursting || this->Instance->SilentULine(this->Instance->FindServerNamePtr(u->server)) ) { @@ -275,7 +279,7 @@ bool TreeSocket::ForceNick(const std::string &prefix, std::deque &p if (params.size() < 3) return true; - userrec* u = this->Instance->FindNick(params[0]); + User* u = this->Instance->FindNick(params[0]); if (u) { @@ -291,7 +295,7 @@ bool TreeSocket::ForceNick(const std::string &prefix, std::deque &p /* buh. UID them */ if (!u->ForceNickChange(u->uuid)) { - userrec::QuitUser(this->Instance, u, "Nickname collision"); + User::QuitUser(this->Instance, u, "Nickname collision"); return true; } } @@ -308,7 +312,7 @@ bool TreeSocket::OperQuit(const std::string &prefix, std::deque &pa if (params.size() < 1) return true; - userrec* u = this->Instance->FindNick(prefix); + User* u = this->Instance->FindNick(prefix); if (u) { @@ -327,13 +331,13 @@ bool TreeSocket::ServiceJoin(const std::string &prefix, std::deque if (!this->Instance->IsChannel(params[1].c_str())) return true; - userrec* u = this->Instance->FindNick(params[0]); + User* u = this->Instance->FindNick(params[0]); if (u) { /* only join if it's local, otherwise just pass it on! */ if (IS_LOCAL(u)) - chanrec::JoinUser(this->Instance, u, params[1].c_str(), false, "", Instance->Time()); + Channel::JoinUser(this->Instance, u, params[1].c_str(), false, "", false, Instance->Time()); Utils->DoOneToAllButSender(prefix,"SVSJOIN",params,prefix); } return true; @@ -347,8 +351,8 @@ bool TreeSocket::ServicePart(const std::string &prefix, std::deque if (!this->Instance->IsChannel(params[1].c_str())) return true; - userrec* u = this->Instance->FindNick(params[0]); - chanrec* c = this->Instance->FindChan(params[1]); + User* u = this->Instance->FindNick(params[0]); + Channel* c = this->Instance->FindChan(params[1]); if (u) { @@ -373,7 +377,7 @@ bool TreeSocket::RemoteRehash(const std::string &prefix, std::deque { this->Instance->SNO->WriteToSnoMask('l',"Remote rehash initiated by \002"+prefix+"\002."); this->Instance->RehashServer(); - Utils->ReadConfiguration(false); + Utils->ReadConfiguration(true); InitializeDisabledCommands(Instance->Config->DisabledCommands, Instance); } Utils->DoOneToAllButSender(prefix,"REHASH",params,prefix); @@ -385,7 +389,7 @@ bool TreeSocket::RemoteKill(const std::string &prefix, std::deque & if (params.size() != 2) return true; - userrec* who = this->Instance->FindNick(params[0]); + User* who = this->Instance->FindNick(params[0]); if (who) { @@ -400,7 +404,7 @@ bool TreeSocket::RemoteKill(const std::string &prefix, std::deque & // NOTE: This is safe with kill hiding on, as RemoteKill is only reached if we have a server prefix. // in short this is not executed for USERS. who->Write(":%s KILL %s :%s (%s)", prefix.c_str(), who->nick, prefix.c_str(), reason.c_str()); - userrec::QuitUser(this->Instance,who,reason); + User::QuitUser(this->Instance,who,reason); } return true; } @@ -433,7 +437,7 @@ bool TreeSocket::LocalPong(const std::string &prefix, std::deque &p * dump the PONG reply back to their fd. If its a server, do nowt. * Services might want to send these s->s, but we dont need to yet. */ - userrec* u = this->Instance->FindNick(prefix); + User* u = this->Instance->FindNick(prefix); if (u) { u->WriteServ("PONG %s %s",params[0].c_str(),params[1].c_str()); @@ -458,15 +462,13 @@ bool TreeSocket::MetaData(const std::string &prefix, std::deque &pa TreeServer* ServerSource = Utils->FindServer(prefix); if (ServerSource) { - Utils->SetRemoteBursting(ServerSource, false); - if (params[0] == "*") { FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_OTHER,NULL,params[1],params[2])); } else if (*(params[0].c_str()) == '#') { - chanrec* c = this->Instance->FindChan(params[0]); + Channel* c = this->Instance->FindChan(params[0]); if (c) { FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_CHANNEL,c,params[1],params[2])); @@ -474,7 +476,7 @@ bool TreeSocket::MetaData(const std::string &prefix, std::deque &pa } else if (*(params[0].c_str()) != '#') { - userrec* u = this->Instance->FindNick(params[0]); + User* u = this->Instance->FindNick(params[0]); if (u) { FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_USER,u,params[1],params[2])); @@ -507,7 +509,7 @@ bool TreeSocket::ChangeHost(const std::string &prefix, std::deque & { if (params.size() < 1) return true; - userrec* u = this->Instance->FindNick(prefix); + User* u = this->Instance->FindNick(prefix); if (u) { @@ -520,64 +522,63 @@ bool TreeSocket::ChangeHost(const std::string &prefix, std::deque & bool TreeSocket::AddLine(const std::string &prefix, std::deque ¶ms) { if (params.size() < 6) + { + this->Instance->SNO->WriteToSnoMask('x',"%s sent me a malformed ADDLINE of type %s.",prefix.c_str(),params[0].c_str()); return true; - bool propogate = false; - if (!this->bursting) - Utils->lines_to_apply = 0; - switch (*(params[0].c_str())) + } + + XLineFactory* xlf = Instance->XLines->GetFactory(params[0]); + + if (!xlf) { - case 'Z': - propogate = Instance->XLines->add_zline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str()); - Instance->XLines->zline_set_creation_time(params[1].c_str(), atoi(params[3].c_str())); - if (propogate) - Utils->lines_to_apply |= APPLY_ZLINES; - break; - case 'Q': - propogate = Instance->XLines->add_qline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str()); - Instance->XLines->qline_set_creation_time(params[1].c_str(), atoi(params[3].c_str())); - if (propogate) - Utils->lines_to_apply |= APPLY_QLINES; - break; - case 'E': - propogate = Instance->XLines->add_eline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str()); - Instance->XLines->eline_set_creation_time(params[1].c_str(), atoi(params[3].c_str())); - break; - case 'G': - propogate = Instance->XLines->add_gline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str()); - Instance->XLines->gline_set_creation_time(params[1].c_str(), atoi(params[3].c_str())); - if (propogate) - Utils->lines_to_apply |= APPLY_GLINES; - break; - case 'K': - propogate = Instance->XLines->add_kline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str()); - if (propogate) - Utils->lines_to_apply |= APPLY_KLINES; - break; - default: - /* Just in case... */ - this->Instance->SNO->WriteToSnoMask('x',"\2WARNING\2: Invalid xline type '"+params[0]+"' sent by server "+prefix+", ignored!"); - propogate = false; - break; + this->Instance->SNO->WriteToSnoMask('x',"%s sent me an unknown ADDLINE type (%s).",prefix.c_str(),params[0].c_str()); + return true; } - /* Send it on its way */ - if (propogate) + + XLine* xl = xlf->Generate(Instance->Time(), atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str()); + xl->SetCreateTime(atoi(params[3].c_str())); + if (Instance->XLines->AddLine(xl,NULL)) { - if (atoi(params[4].c_str())) + if (xl->duration) { - time_t c_requires_crap = ConvToInt(params[4]) + Instance->Time(); - 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(),Instance->TimeString(c_requires_crap).c_str(),params[5].c_str()); + this->Instance->SNO->WriteToSnoMask('x',"%s added %s%s on %s to expire on %s (%s).",prefix.c_str(),params[0].c_str(),params[0].length() == 1 ? "LINE" : "", + params[1].c_str(),Instance->TimeString(xl->expiry).c_str(),params[5].c_str()); } else { - 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()); + this->Instance->SNO->WriteToSnoMask('x',"%s added permanent %s%s on %s (%s).",prefix.c_str(),params[0].c_str(),params[0].length() == 1 ? "LINE" : "", + params[1].c_str(),params[5].c_str()); } params[5] = ":" + params[5]; - Utils->DoOneToAllButSender(prefix,"ADDLINE",params,prefix); + + User* u = Instance->FindNick(prefix); + Utils->DoOneToAllButSender(prefix, "ADDLINE", params, u ? u->server : prefix); + TreeServer *remoteserver = Utils->FindServer(u ? u->server : prefix); + + if (!remoteserver->bursting) + { + Instance->XLines->ApplyLines(); + } } - if (!this->bursting) + else + delete xl; + + return true; +} + +bool TreeSocket::DelLine(const std::string &prefix, std::deque ¶ms) +{ + if (params.size() < 2) + return true; + + User* user = Instance->FindNick(prefix); + + /* NOTE: No check needed on 'user', this function safely handles NULL */ + if (Instance->XLines->DelLine(params[0].c_str(), params[1], user)) { - Instance->XLines->apply_lines(Utils->lines_to_apply); - Utils->lines_to_apply = 0; + this->Instance->SNO->WriteToSnoMask('x',"%s removed %s%s on %s.", prefix.c_str(), + params[0].c_str(), params[0].length() == 1 ? "LINE" : "", params[1].c_str()); + Utils->DoOneToAllButSender(prefix,"DELLINE", params, prefix); } return true; } @@ -586,7 +587,7 @@ bool TreeSocket::ChangeName(const std::string &prefix, std::deque & { if (params.size() < 1) return true; - userrec* u = this->Instance->FindNick(prefix); + User* u = this->Instance->FindNick(prefix); if (u) { u->ChangeName(params[0].c_str()); @@ -600,16 +601,16 @@ bool TreeSocket::Whois(const std::string &prefix, std::deque ¶m { if (params.size() < 1) return true; - userrec* u = this->Instance->FindNick(prefix); + User* u = this->Instance->FindNick(prefix); if (u) { // an incoming request if (params.size() == 1) { - userrec* x = this->Instance->FindNick(params[0]); + User* x = this->Instance->FindNick(params[0]); if ((x) && (IS_LOCAL(x))) { - userrec* x = this->Instance->FindNick(params[0]); + User* x = this->Instance->FindNick(params[0]); char signon[MAXBUF]; char idle[MAXBUF]; snprintf(signon, MAXBUF, "%lu", (unsigned long)x->signon); @@ -631,7 +632,7 @@ bool TreeSocket::Whois(const std::string &prefix, std::deque ¶m else if (params.size() == 3) { std::string who_did_the_whois = params[0]; - userrec* who_to_send_to = this->Instance->FindNick(who_did_the_whois); + User* who_to_send_to = this->Instance->FindNick(who_did_the_whois); if ((who_to_send_to) && (IS_LOCAL(who_to_send_to))) { // an incoming reply to a whois we sent out @@ -658,7 +659,7 @@ bool TreeSocket::Push(const std::string &prefix, std::deque ¶ms { if (params.size() < 2) return true; - userrec* u = this->Instance->FindNick(params[0]); + User* u = this->Instance->FindNick(params[0]); if (!u) return true; if (IS_LOCAL(u)) @@ -709,7 +710,7 @@ bool TreeSocket::Time(const std::string &prefix, std::deque ¶ms // someone querying our time? if (this->Instance->Config->ServerName == params[0] || this->Instance->Config->GetSID() == params[0]) { - userrec* u = this->Instance->FindNick(params[1]); + User* u = this->Instance->FindNick(params[1]); if (u) { params.push_back(ConvToStr(Instance->Time(false))); @@ -720,7 +721,7 @@ bool TreeSocket::Time(const std::string &prefix, std::deque ¶ms else { // not us, pass it on - userrec* u = this->Instance->FindNick(params[1]); + User* u = this->Instance->FindNick(params[1]); if (u) Utils->DoOneToOne(prefix,"TIME",params,params[0]); } @@ -728,7 +729,7 @@ bool TreeSocket::Time(const std::string &prefix, std::deque ¶ms else if (params.size() == 3) { // a response to a previous TIME - userrec* u = this->Instance->FindNick(params[1]); + User* u = this->Instance->FindNick(params[1]); if ((u) && (IS_LOCAL(u))) { time_t rawtime = atol(params[2].c_str()); @@ -783,7 +784,7 @@ bool TreeSocket::RemoveStatus(const std::string &prefix, std::deque { if (params.size() < 1) return true; - chanrec* c = Instance->FindChan(params[0]); + Channel* c = Instance->FindChan(params[0]); if (c) { for (char modeletter = 'A'; modeletter <= 'z'; modeletter++) @@ -815,6 +816,11 @@ bool TreeSocket::RemoteServer(const std::string &prefix, std::deque this->SendError("Protocol error - Introduced remote server from unknown server "+prefix); return false; } + if (!this->Instance->IsSID(sid)) + { + this->SendError("Invalid format server ID: "+sid+"!"); + return false; + } TreeServer* CheckDupe = Utils->FindServer(servername); if (CheckDupe) { @@ -836,7 +842,6 @@ bool TreeSocket::RemoteServer(const std::string &prefix, std::deque ParentOfThis->AddChild(Node); params[4] = ":" + params[4]; - Utils->SetRemoteBursting(Node, true); Utils->DoOneToAllButSender(prefix,"SERVER",params,prefix); this->Instance->SNO->WriteToSnoMask('l',"Server \002"+prefix+"\002 introduced server \002"+servername+"\002 ("+description+")"); return true; @@ -849,7 +854,7 @@ bool TreeSocket::ComparePass(const std::string &ours, const std::string &theirs) /* One or both of us specified hmac sha256, but we don't have sha256 module loaded! * We can't allow this password as valid. */ - if (!Instance->FindModule("m_sha256.so") || !Utils->ChallengeResponse) + if (!Instance->Modules->Find("m_sha256.so") || !Utils->ChallengeResponse) return false; else /* Straight string compare of hashes */ @@ -889,6 +894,12 @@ bool TreeSocket::Outbound_Reply_Server(std::deque ¶ms) return false; } + if (!this->Instance->IsSID(sid)) + { + this->SendError("Invalid format server ID: "+sid+"!"); + return false; + } + for (std::vector::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++) { if ((x->Name == servername) && ((ComparePass(this->MakePass(x->RecvPass,this->GetOurChallenge()),password)) || (x->RecvPass == password && (this->GetTheirChallenge().empty())))) @@ -921,7 +932,7 @@ bool TreeSocket::Outbound_Reply_Server(std::deque ¶ms) Utils->TreeRoot->AddChild(Node); params[4] = ":" + params[4]; Utils->DoOneToAllButSender(Instance->Config->GetSID(),"SERVER",params,sname); - this->bursting = true; + Node->bursting = true; this->DoBurst(Node); return true; } @@ -960,22 +971,16 @@ bool TreeSocket::Inbound_Server(std::deque ¶ms) return false; } + if (!this->Instance->IsSID(sid)) + { + this->SendError("Invalid format server ID: "+sid+"!"); + return false; + } + for (std::vector::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++) { if ((x->Name == servername) && ((ComparePass(this->MakePass(x->RecvPass,this->GetOurChallenge()),password) || x->RecvPass == password && (this->GetTheirChallenge().empty())))) { - /* First check for instances of the server that are waiting between the inbound and outbound SERVER command */ - TreeSocket* CheckDupeSocket = Utils->FindBurstingServer(sname); - if (CheckDupeSocket) - { - /* If we find one, we abort the link to prevent a race condition */ - this->SendError("Negotiation collision"); - this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists in a negotiating state."); - CheckDupeSocket->SendError("Negotiation collision"); - Instance->SE->DelFd(CheckDupeSocket); - CheckDupeSocket->Close(); - return false; - } /* Check for fully initialized instances of the server by id */ Instance->Log(DEBUG,"Looking for dupe SID %s", sid.c_str()); TreeServer* CheckDupeSID = Utils->FindServerID(sid); @@ -997,12 +1002,10 @@ bool TreeSocket::Inbound_Server(std::deque ¶ms) this->Instance->SNO->WriteToSnoMask('l',"Verified incoming server connection from \002"+sname+"\002["+(x->HiddenFromStats ? "" : this->GetIP())+"] ("+description+")"); if (this->Hook) { - std::string name = InspSocketNameRequest((Module*)Utils->Creator, this->Hook).Send(); + std::string name = BufferedSocketNameRequest((Module*)Utils->Creator, this->Hook).Send(); this->Instance->SNO->WriteToSnoMask('l',"Connection from \2"+sname+"\2["+(x->HiddenFromStats ? "" : this->GetIP())+"] using transport \2"+name+"\2"); } - Utils->AddBurstingServer(sname,this); - // this is good. Send our details: Our server name and description and hopcount of 0, // along with the sendpass from this block. this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+this->MakePass(x->SendPass, this->GetTheirChallenge())+" 0 "+Instance->Config->GetSID()+" :"+this->Instance->Config->ServerDesc); @@ -1023,8 +1026,7 @@ void TreeSocket::Split(const std::string &line, std::deque &n) std::string param; while (tokens.GetToken(param)) { - if (!param.empty()) - n.push_back(param); + n.push_back(param); } return; } @@ -1051,22 +1053,38 @@ bool TreeSocket::ProcessLine(std::string &line) { prefix = params[0].substr(1); params.pop_front(); + + if (prefix.empty()) + { + this->SendError("BUG (?) Empty prefix recieved."); + return false; + } } + command = params[0].c_str(); params.pop_front(); + switch (this->LinkState) { TreeServer* Node; case WAIT_AUTH_1: - // Waiting for SERVER command from remote server. Server initiating - // the connection sends the first SERVER command, listening server - // replies with theirs if its happy, then if the initiator is happy, - // it starts to send its net sync, which starts the merge, otherwise - // it sends an ERROR. + /* + * State WAIT_AUTH_1: + * Waiting for SERVER command from remote server. Server initiating + * the connection sends the first SERVER command, listening server + * replies with theirs if its happy, then if the initiator is happy, + * it starts to send its net sync, which starts the merge, otherwise + * it sends an ERROR. + */ if (command == "PASS") { - /* Silently ignored */ + /* + * Ignore this silently. Some services packages insist on sending PASS, even + * when it is not required (i.e. by us). We have to ignore this here, otherwise + * as it's an unknown command (effectively), it will cause the connection to be + * closed, which probably isn't what people want. -- w00t + */ } else if (command == "SERVER") { @@ -1087,18 +1105,26 @@ bool TreeSocket::ProcessLine(std::string &line) } else { + // XXX ...wtf. irc::string error = "Invalid command in negotiation phase: " + command; this->SendError(assign(error)); return false; } break; case WAIT_AUTH_2: - // Waiting for start of other side's netmerge to say they liked our - // password. + /* + * State WAIT_AUTH_2: + * We have sent SERVER to the other side of the connection. Now we're waiting for them to start BURST. + * The other option at this stage of things, of course, is for them to close our connection thanks + * to invalid credentials.. -- w + */ if (command == "SERVER") { - // cant do this, they sent it to us in the WAIT_AUTH_1 state! - // silently ignore. + /* + * Connection is either attempting to re-auth itself (stupid) or sending netburst without sending BURST. + * Both of these aren't allowable, so block them here. -- w + */ + this->SendError("You may not re-authenticate or commence netburst without sending BURST."); return true; } else if (command == "BURST") @@ -1137,7 +1163,7 @@ bool TreeSocket::ProcessLine(std::string &line) this->Instance->SNO->WriteToSnoMask('l',"Server \2"+InboundServerName+"\2 being introduced from \2" + prefix + "\2 denied, server ID already exists on the network. Closing link."); return false; } - Utils->DelBurstingServer(this); + Utils->TreeRoot->AddChild(Node); params.clear(); params.push_back(InboundServerName); @@ -1146,7 +1172,7 @@ bool TreeSocket::ProcessLine(std::string &line) params.push_back(InboundSID); params.push_back(":"+InboundDescription); Utils->DoOneToAllButSender(Instance->Config->GetSID(),"SERVER",params,InboundServerName); - this->bursting = true; + Node->bursting = true; this->DoBurst(Node); } else if (command == "ERROR") @@ -1160,17 +1186,22 @@ bool TreeSocket::ProcessLine(std::string &line) break; case LISTENER: + /* + * This really shouldn't happen. + */ this->SendError("Internal error -- listening socket accepted its own descriptor!!!"); return false; break; case CONNECTING: + /* + * State CONNECTING: + * We're connecting (OUTGOING) to another server. They are in state WAIT_AUTH_1 until they verify + * our credentials, when they proceed into WAIT_AUTH_2 and send SERVER to us. We then send BURST + * + our netburst, which will put them into CONNECTED state. -- w + */ if (command == "SERVER") { - // another server we connected to, which was in WAIT_AUTH_1 state, - // has just sent us their credentials. If we get this far, theyre - // happy with OUR credentials, and they are now in WAIT_AUTH_2 state. - // if we're happy with this, we should send our netburst which - // kickstarts the merge. + // Our credentials have been accepted, send netburst. (this puts US into the CONNECTED state) return this->Outbound_Reply_Server(params); } else if (command == "ERROR") @@ -1183,15 +1214,28 @@ bool TreeSocket::ProcessLine(std::string &line) } break; case CONNECTED: - // This is the 'authenticated' state, when all passwords - // have been exchanged and anything past this point is taken - // as gospel. - + /* + * State CONNECTED: + * Credentials have been exchanged, we've gotten their 'BURST' (or sent ours). + * Anything from here on should be accepted a little more reasonably. + */ if (!prefix.empty()) { + /* + * Check for fake direction here, and drop any instances that are found. + * What is fake direction? Imagine the following server setup: + * 0AA <-> 0AB <-> 0AC + * Fake direction would be 0AC sending a message to 0AB claiming to be from + * 0AA, or something similar. Basically, a message taking a path that *cannot* + * be correct. + * + * When would this be seen? + * Well, hopefully never. It could be caused by race conditions, bugs, or + * "miscreant" servers, though, so let's check anyway. -- w + */ std::string direction = prefix; - userrec *t = this->Instance->FindUUID(prefix); + User *t = this->Instance->FindUUID(prefix); if (t) { direction = t->server; @@ -1213,38 +1257,64 @@ bool TreeSocket::ProcessLine(std::string &line) } else { - prefix = this->GetName(); + /* + * Empty prefix from a server to server link: + * This is somewhat bad/naughty, so let's set the prefix + * to be the link that we got it from, so we don't break anything. -- w + */ + TreeServer* n = Utils->FindServer(GetName()); + if (n) + prefix = n->GetID(); + else + prefix = GetName(); } - if ((command == "MODE") && (params.size() >= 2)) + /* + * First up, check for any malformed commands (e.g. MODE without a timestamp) + * and rewrite commands where necessary (SVSMODE -> MODE for services). -- w + */ + if (command == "MODE") { - chanrec* channel = Instance->FindChan(params[0]); - if (channel) + if (params.size() >= 2) { - userrec* x = Instance->FindNick(prefix); - if (x) + Channel* channel = Instance->FindChan(params[0]); + if (channel) { - if (warned.find(x->server) == warned.end()) + User* x = Instance->FindNick(prefix); + if (x) { - Instance->Log(DEFAULT,"WARNING: I revceived modes '%s' from another server '%s'. This is not compliant with InspIRCd. Please check that server for bugs.", params[1].c_str(), x->server); - Instance->SNO->WriteToSnoMask('d', "WARNING: The server %s is sending nonstandard modes: '%s MODE %s' where FMODE should be used, and may cause desyncs.", x->server, x->nick, params[1].c_str()); - warned[x->server] = x->nick; + if (warned.find(x->server) == warned.end()) + { + Instance->Log(DEFAULT,"WARNING: I revceived modes '%s' from another server '%s'. This is not compliant with InspIRCd. Please check that server for bugs.", params[1].c_str(), x->server); + Instance->SNO->WriteToSnoMask('d', "WARNING: The server %s is sending nonstandard modes: '%s MODE %s' where FMODE should be used, and may cause desyncs.", x->server, x->nick, params[1].c_str()); + warned[x->server] = x->nick; + } } } } } - - if (command == "SVSMODE") + else if (command == "SVSMODE") { - /* Services expects us to implement - * SVSMODE. In inspircd its the same as - * MODE anyway. - */ command = "MODE"; } - std::string target; - /* Yes, know, this is a mess. Its reasonably fast though as we're - * working with std::string here. + + + /* + * Now, check for (and parse) commands as appropriate. -- w + */ + + /* Find the server that this command originated from, used in the handlers below */ + TreeServer *ServerSource = Utils->FindServer(prefix); + + /* Find the link we just got this from so we don't bounce it back incorrectly */ + std::string sourceserv = this->myhost; + if (!this->InboundServerName.empty()) + { + sourceserv = this->InboundServerName; + } + + /* + * XXX one of these days, this needs to be moved into class Commands. */ if (command == "UID") { @@ -1252,9 +1322,6 @@ bool TreeSocket::ProcessLine(std::string &line) } else if (command == "FJOIN") { - TreeServer* ServerSource = Utils->FindServer(prefix); - if (ServerSource) - Utils->SetRemoteBursting(ServerSource, false); return this->ForceJoin(prefix,params); } else if (command == "STATS") @@ -1265,8 +1332,9 @@ bool TreeSocket::ProcessLine(std::string &line) { return this->Motd(prefix, params); } - else if (command == "KILL" && Utils->IsServer(prefix)) + else if (command == "KILL" && ServerSource) { + // Kill from a server return this->RemoteKill(prefix,params); } else if (command == "MODULES") @@ -1291,9 +1359,6 @@ bool TreeSocket::ProcessLine(std::string &line) } else if (command == "FMODE") { - TreeServer* ServerSource = Utils->FindServer(prefix); - if (ServerSource) - Utils->SetRemoteBursting(ServerSource, false); return this->ForceMode(prefix,params); } else if (command == "FTOPIC") @@ -1314,46 +1379,10 @@ bool TreeSocket::ProcessLine(std::string &line) } else if (command == "PING") { - if (prefix.empty()) - prefix = this->GetName(); - /* - * We just got a ping from a server that's bursting. - * This can't be right, so set them to not bursting, and - * apply their lines. - */ - TreeServer* ServerSource = Utils->FindServer(prefix); - if (ServerSource) - Utils->SetRemoteBursting(ServerSource, false); - - if (this->bursting) - { - this->bursting = false; - Instance->XLines->apply_lines(Utils->lines_to_apply); - Utils->lines_to_apply = 0; - } - return this->LocalPing(prefix,params); } else if (command == "PONG") { - if (prefix.empty()) - prefix = this->GetName(); - /* - * We just got a pong from a server that's bursting. - * This can't be right, so set them to not bursting, and - * apply their lines. - */ - TreeServer* ServerSource = Utils->FindServer(prefix); - if (ServerSource) - Utils->SetRemoteBursting(ServerSource, false); - - if (this->bursting) - { - this->bursting = false; - Instance->XLines->apply_lines(Utils->lines_to_apply); - Utils->lines_to_apply = 0; - } - return this->LocalPong(prefix,params); } else if (command == "VERSION") @@ -1370,17 +1399,14 @@ bool TreeSocket::ProcessLine(std::string &line) } else if (command == "ADDLINE") { - TreeServer* ServerSource = Utils->FindServer(prefix); - if (ServerSource) - Utils->SetRemoteBursting(ServerSource, false); return this->AddLine(prefix,params); } + else if (command == "DELLINE") + { + return this->DelLine(prefix,params); + } else if (command == "SVSNICK") { - if (prefix.empty()) - { - prefix = this->GetName(); - } return this->ForceNick(prefix,params); } else if (command == "OPERQUIT") @@ -1405,11 +1431,10 @@ bool TreeSocket::ProcessLine(std::string &line) } else if ((command == "KICK") && (Utils->IsServer(prefix))) { - std::string sourceserv = this->myhost; if (params.size() == 3) { - userrec* user = this->Instance->FindNick(params[1]); - chanrec* chan = this->Instance->FindChan(params[0]); + User* user = this->Instance->FindNick(params[1]); + Channel* chan = this->Instance->FindChan(params[0]); if (user && chan) { if (!chan->ServerKickUser(user, params[2].c_str(), false)) @@ -1417,24 +1442,15 @@ bool TreeSocket::ProcessLine(std::string &line) delete chan; } } - if (!this->InboundServerName.empty()) - { - sourceserv = this->InboundServerName; - } + return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params); } else if (command == "SVSJOIN") { - if (prefix.empty()) - { - prefix = this->GetName(); - } return this->ServiceJoin(prefix,params); } else if (command == "SVSPART") { - if (prefix.empty()) - prefix = this->GetName(); return this->ServicePart(prefix,params); } else if (command == "SQUIT") @@ -1447,49 +1463,61 @@ bool TreeSocket::ProcessLine(std::string &line) } else if (command == "OPERNOTICE") { - std::string sourceserv = this->myhost; - if (!this->InboundServerName.empty()) - sourceserv = this->InboundServerName; if (params.size() >= 1) - Instance->WriteOpers("*** From " + sourceserv + ": " + params[0]); + Instance->SNO->WriteToSnoMask('A', "From " + prefix + ": " + params[0]); return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params); } else if (command == "MODENOTICE") { - std::string sourceserv = this->myhost; - if (!this->InboundServerName.empty()) - sourceserv = this->InboundServerName; if (params.size() >= 2) { - Instance->WriteMode(params[0].c_str(), WM_AND, "*** From %s: %s", sourceserv.c_str(), params[1].c_str()); + Instance->Users->WriteMode(params[0].c_str(), WM_AND, "*** From %s: %s", prefix.c_str(), params[1].c_str()); } return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params); } else if (command == "SNONOTICE") { - std::string sourceserv = this->myhost; - if (!this->InboundServerName.empty()) - sourceserv = this->InboundServerName; if (params.size() >= 2) { - Instance->SNO->WriteToSnoMask(*(params[0].c_str()), "From " + sourceserv + ": "+ params[1]); + Instance->SNO->WriteToSnoMask(*(params[0].c_str()), "From " + prefix + ": "+ params[1]); } return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params); } + else if (command == "BURST") + { + // Set prefix server as bursting + if (!ServerSource) + { + this->Instance->SNO->WriteToSnoMask('l', "WTF: Got BURST from a nonexistant server(?): %s", prefix.c_str()); + return false; + } + + ServerSource->bursting = true; + return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params); + } else if (command == "ENDBURST") { - this->bursting = false; - Instance->XLines->apply_lines(Utils->lines_to_apply); - Utils->lines_to_apply = 0; - std::string sourceserv = this->myhost; - if (!this->InboundServerName.empty()) - sourceserv = this->InboundServerName; - this->Instance->SNO->WriteToSnoMask('l',"Received end of netburst from \2%s\2",sourceserv.c_str()); - - Event rmode((char*)sourceserv.c_str(), (Module*)Utils->Creator, "new_server"); - rmode.Send(Instance); - - return true; + if (!ServerSource) + { + this->Instance->SNO->WriteToSnoMask('l', "WTF: Got ENDBURST from a nonexistant server(?): %s", prefix.c_str()); + return false; + } + + ServerSource->FinishBurst(); + return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params); + } + else if (command == "MODE") + { + // Server-prefix MODE. + const char* modelist[MAXPARAMETERS]; + for (size_t i = 0; i < params.size(); i++) + modelist[i] = params[i].c_str(); + + // Insert into the parser + this->Instance->SendMode(modelist, params.size(), this->Instance->FakeClient); + + // Pass out to the network + return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params); } else { @@ -1497,113 +1525,77 @@ bool TreeSocket::ProcessLine(std::string &line) * Not a special s2s command. Emulate the user doing it. * This saves us having a huge ugly command parser again. */ - userrec *who = this->Instance->FindUUID(prefix); + User *who = this->Instance->FindUUID(prefix); - std::string sourceserv = this->myhost; - if (!this->InboundServerName.empty()) + if (!who) { - sourceserv = this->InboundServerName; + // this looks ugly because command is an irc::string + this->SendError("Command (" + std::string(command.c_str()) + ") from unknown prefix (" + prefix + ")! Dropping link."); + return false; } - if ((!who) && (command == "MODE")) + + if (command == "NICK") { - if (Utils->IsServer(prefix)) + if (params.size() != 2) { - const char* modelist[MAXPARAMETERS]; - for (size_t i = 0; i < params.size(); i++) - modelist[i] = params[i].c_str(); - this->Instance->SendMode(modelist, params.size(), this->Instance->FakeClient); - /* Hot potato! pass it on! */ - return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params); + SendError("Protocol violation: NICK message without TS - :"+std::string(who->uuid)+" NICK "+params[0]); + return false; } - } - if (who) - { - if (command == "NICK") + /* Update timestamp on user when they change nicks */ + who->age = atoi(params[1].c_str()); + + /* + * On nick messages, check that the nick doesnt already exist here. + * If it does, perform collision logic. + */ + User* x = this->Instance->FindNickOnly(params[0]); + if ((x) && (x != who)) { - if (params.size() != 2) + int collideret = 0; + /* x is local, who is remote */ + collideret = this->DoCollision(x, who->age, who->ident, who->GetIPString(), who->uuid); + if (collideret != 1) { - SendError("Protocol violation: NICK message without TS - :"+std::string(who->uuid)+" NICK "+params[0]); - return false; - } - /* Update timestamp on user when they change nicks */ - who->age = atoi(params[1].c_str()); - - /* On nick messages, check that the nick doesnt - * already exist here. If it does, kill their copy, - * and our copy. - */ - userrec* x = this->Instance->FindNickOnly(params[0]); - if ((x) && (x != who)) - { - int collideret = 0; - /* x is local, who is remote */ - collideret = this->DoCollision(x, who->age, who->ident, who->GetIPString(), who->uuid); - if (collideret != 1) - { - /* - * Remote client lost, or both lost, - * parsing this nickchange would be - * pointless, as the incoming client's - * server will soon recieve SVSNICK to - * change its nick to its UID. :) - * -- w00t - */ - return true; - } -/* -Old nickname collision logic.. - std::deque p; - p.push_back(params[0]); - p.push_back(":Nickname collision ("+prefix+" -> "+params[0]+")"); - Utils->DoOneToMany(this->Instance->Config->ServerName,"KILL",p); - p.clear(); - p.push_back(prefix); - p.push_back(":Nickname collision"); - Utils->DoOneToMany(this->Instance->Config->ServerName,"KILL",p); - userrec::QuitUser(this->Instance,x,"Nickname collision ("+prefix+" -> "+params[0]+")"); - userrec* y = this->Instance->FindNick(prefix); - if (y) - { - userrec::QuitUser(this->Instance,y,"Nickname collision"); - } - return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params); -*/ - } - } - // its a user - target = who->server; - const char* strparams[127]; - for (unsigned int q = 0; q < params.size(); q++) - { - strparams[q] = params[q].c_str(); - } - switch (this->Instance->CallCommandHandler(command.c_str(), strparams, params.size(), who)) - { - case CMD_INVALID: - this->SendError("Unrecognised command '"+std::string(command.c_str())+"' -- possibly loaded mismatched modules"); - return false; - break; - case CMD_FAILURE: + /* + * Remote client lost, or both lost, parsing this nickchange would be + * pointless, as the incoming client's server will soon recieve SVSNICK to + * change its nick to its UID. :) -- w00t + */ return true; - break; - default: - /* CMD_SUCCESS and CMD_USER_DELETED fall through here */ - break; + } } } - else + + // its a user + const char* strparams[127]; + for (unsigned int q = 0; q < params.size(); q++) { - // its not a user. Its either a server, or somethings screwed up. - if (Utils->IsServer(prefix)) - target = this->Instance->Config->GetSID(); - else + strparams[q] = params[q].c_str(); + } + + switch (this->Instance->CallCommandHandler(command.c_str(), strparams, params.size(), who)) + { + case CMD_INVALID: + // command is irc::string, hence ugliness + this->SendError("Unrecognised or malformed command '" + std::string(command.c_str()) + "' -- possibly loaded mismatched modules"); + return false; + break; + /* + * CMD_LOCALONLY is aliased to CMD_FAILURE, so this won't go out onto the network. + */ + case CMD_FAILURE: return true; + break; + default: + /* CMD_SUCCESS and CMD_USER_DELETED fall through here */ + break; } + return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params); } return true; - break; + break; // end of state CONNECTED (phew). } return true; } @@ -1631,7 +1623,8 @@ void TreeSocket::OnTimeout() void TreeSocket::OnClose() { - if (this->LinkState == LISTENER) + // Test fix for big fuckup + if (this->LinkState != CONNECTED) return; // Connection closed. @@ -1675,7 +1668,7 @@ int TreeSocket::OnIncomingConnection(int newsock, char* ip) if (!found) { Utils->Creator->RemoteMessage(NULL,"Server connection from %s denied (no link blocks with that IP address)", ip); - close(newsock); + Instance->SE->Close(newsock); return false; } }