-/* +------------------------------------+\r * | Inspire Internet Relay Chat Daemon |\r * +------------------------------------+\r *\r * InspIRCd: (C) 2002-2007 InspIRCd Development Team\r * See: http://www.inspircd.org/wiki/index.php/Credits\r *\r * This program is free but copyrighted software; see\r * the file COPYING for details.\r *\r * ---------------------------------------------------\r */\r\r#include "inspircd.h"\r#include "configreader.h"\r#include "users.h"\r#include "modules.h"\r#include "wildcard.h"\r#include "xline.h"\r#include "socketengine.h"\r#include "command_parse.h"\r\rvoid InspIRCd::FloodQuitUser(userrec* current)\r{\r this->Log(DEFAULT,"Excess flood from: %s@%s", current->ident, current->host);\r this->SNO->WriteToSnoMask('f',"Excess flood from: %s%s%s@%s",\r current->registered == REG_ALL ? current->nick : "",\r current->registered == REG_ALL ? "!" : "", current->ident, current->host);\r current->SetWriteError("Excess flood");\r if (current->registered != REG_ALL)\r {\r XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());\r XLines->apply_lines(APPLY_ZLINES);\r }\r}\r\rvoid InspIRCd::ProcessUser(userrec* cu)\r{\r int result = EAGAIN;\r\r if (cu->GetFd() == FD_MAGIC_NUMBER)\r return;\r\r if (this->Config->GetIOHook(cu->GetPort()))\r {\r int result2 = 0;\r int MOD_RESULT = 0;\r\r try\r {\r MOD_RESULT = this->Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->GetFd(),ReadBuffer,sizeof(ReadBuffer),result2);\r }\r catch (CoreException& modexcept)\r {\r this->Log(DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());\r }\r\r if (MOD_RESULT < 0)\r {\r result = -EAGAIN;\r }\r else\r {\r result = result2;\r }\r }\r else\r {\r result = cu->ReadData(ReadBuffer, sizeof(ReadBuffer));\r }\r\r if ((result) && (result != -EAGAIN))\r {\r userrec *current;\r int currfd;\r int floodlines = 0;\r\r this->stats->statsRecv += result;\r /*\r * perform a check on the raw buffer as an array (not a string!) to remove\r * character 0 which is illegal in the RFC - replace them with spaces.\r * XXX - no garauntee there's not \0's in the middle of the data,\r * and no reason for it to be terminated either. -- Om\r */\r\r for (int checker = 0; checker < result; checker++)\r {\r if (ReadBuffer[checker] == 0)\r ReadBuffer[checker] = ' ';\r }\r\r if (result > 0)\r ReadBuffer[result] = '\0';\r\r current = cu;\r currfd = current->GetFd();\r\r // add the data to the users buffer\r if (result > 0)\r {\r if (!current->AddBuffer(ReadBuffer))\r {\r // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.\r if (current->registered == REG_ALL)\r {\r // Make sure they arn't flooding long lines.\r if (TIME > current->reset_due)\r {\r current->reset_due = TIME + current->threshold;\r current->lines_in = 0;\r }\r\r current->lines_in++;\r\r if (current->flood && current->lines_in > current->flood)\r FloodQuitUser(current);\r else\r {\r current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over %d chars) Please shorten it.", current->nick, MAXBUF-2);\r current->recvq.clear();\r }\r }\r else\r FloodQuitUser(current);\r\r return;\r }\r\r // while there are complete lines to process...\r while (current->BufferIsReady())\r {\r if (TIME > current->reset_due)\r {\r current->reset_due = TIME + current->threshold;\r current->lines_in = 0;\r }\r\r if (++current->lines_in > current->flood && current->flood)\r {\r FloodQuitUser(current);\r return;\r }\r\r if ((++floodlines > current->flood) && (current->flood != 0))\r {\r FloodQuitUser(current);\r return;\r }\r\r // use GetBuffer to copy single lines into the sanitized string\r std::string single_line = current->GetBuffer();\r current->bytes_in += single_line.length();\r current->cmds_in++;\r if (single_line.length() > MAXBUF - 2) /* MAXBUF is 514 to allow for neccessary line terminators */\r single_line.resize(MAXBUF - 2); /* So to trim to 512 here, we use MAXBUF - 2 */\r\r EventHandler* old_comp = this->SE->GetRef(currfd);\r\r this->Parser->ProcessBuffer(single_line,current);\r /*\r * look for the user's record in case it's changed... if theyve quit,\r * we cant do anything more with their buffer, so bail.\r * there used to be an ugly, slow loop here. Now we have a reference\r * table, life is much easier (and FASTER)\r */\r EventHandler* new_comp = this->SE->GetRef(currfd);\r\r if (new_comp != old_comp)\r return;\r }\r\r return;\r }\r\r if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))\r {\r cu->SetWriteError(strerror(errno));\r return;\r }\r }\r\r // result EAGAIN means nothing read\r else if ((result == EAGAIN) || (result == -EAGAIN))\r {\r /* do nothing */\r }\r else if (result == 0)\r {\r cu->SetWriteError("Connection closed");\r return;\r }\r}\r\r/**\r * This function is called once a second from the mainloop.\r * It is intended to do background checking on all the user structs, e.g.\r * stuff like ping checks, registration timeouts, etc.\r */\rvoid InspIRCd::DoBackgroundUserStuff(time_t TIME)\r{\r /* Is it time yet? */\r if (TIME < next_call)\r return;\r else\r {\r /* Time we actually need to call this again */\r const time_t DUMMY_VALUE = 32768;\r next_call = TIME + DUMMY_VALUE;\r\r /* XXX: IT IS NOT SAFE TO USE AN ITERATOR HERE. DON'T EVEN THINK ABOUT IT. */\r for (unsigned long count2 = 0; count2 != this->local_users.size(); count2++)\r {\r if (count2 >= this->local_users.size())\r break;\r\r userrec* curr = this->local_users[count2];\r\r if (curr)\r {\r /*\r * registration timeout -- didnt send USER/NICK/HOST\r * in the time specified in their connection class.\r */\r if ((TIME > curr->timeout) && (curr->registered != REG_ALL))\r {\r curr->muted = true;\r GlobalCulls.AddItem(curr,"Registration timeout");\r continue;\r }\r else\r {\r if ((curr->registered != REG_ALL) && (next_call > (time_t)curr->timeout))\r next_call = curr->timeout;\r }\r\r /*\r * user has signed on with USER/NICK/PASS, and dns has completed, all the modules\r * say this user is ok to proceed, fully connect them.\r */\r bool ready = AllModulesReportReady(curr);\r if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (ready))\r {\r if (!curr->dns_done)\r {\r curr->WriteServ("NOTICE Auth :*** Could not resolve your hostname: Request timed out; using your IP address (%s) instead.", curr->GetIPString());\r curr->dns_done = true;\r }\r this->stats->statsDnsBad++;\r curr->FullConnect();\r continue;\r }\r else\r {\r if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon))\r next_call = curr->signon;\r }\r\r if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (ready))\r {\r curr->FullConnect();\r continue;\r }\r else\r {\r if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon + this->Config->dns_timeout))\r next_call = curr->signon + this->Config->dns_timeout;\r }\r\r // It's time to PING this user. Send them a ping.\r if ((TIME > curr->nping) && (curr->registered == REG_ALL))\r {\r // This user didn't answer the last ping, remove them\r if (!curr->lastping)\r {\r /* Everybody loves boobies. */\r time_t time = this->Time(false) - (curr->nping - curr->pingmax);\r char message[MAXBUF];\r snprintf(message, MAXBUF, "Ping timeout: %ld second%s", time, time > 1 ? "s" : "");\r curr->muted = true;\r GlobalCulls.AddItem(curr, message);\r curr->lastping = 1;\r curr->nping = TIME+curr->pingmax;\r continue;\r }\r curr->Write("PING :%s",this->Config->ServerName);\r curr->lastping = 0;\r curr->nping = TIME+curr->pingmax;\r }\r else\r {\r if ((curr->registered == REG_ALL) && (next_call > curr->nping))\r next_call = curr->nping;\r }\r }\r }\r\r /* If theres nothing to do, trigger in the next second, something might come up */\r time_t delta = next_call - TIME;\r if (delta == DUMMY_VALUE)\r {\r next_call = TIME + 1;\r delta = 1;\r }\r }\r}\r
\ No newline at end of file
+/* +------------------------------------+
+ * | Inspire Internet Relay Chat Daemon |
+ * +------------------------------------+
+ *
+ * InspIRCd: (C) 2002-2008 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
+ *
+ * This program is free but copyrighted software; see
+ * the file COPYING for details.
+ *
+ * ---------------------------------------------------
+ */
+
+/* $Core: libIRCDuserprocess */
+
+#include "inspircd.h"
+#include "wildcard.h"
+#include "xline.h"
+#include "socketengine.h"
+#include "command_parse.h"
+
+void FloodQuitUserHandler::Call(User* current)
+{
+ Server->Logs->Log("USERS",DEFAULT,"Excess flood from: %s@%s", current->ident, current->host);
+ Server->SNO->WriteToSnoMask('f',"Excess flood from: %s%s%s@%s",
+ current->registered == REG_ALL ? current->nick : "",
+ current->registered == REG_ALL ? "!" : "", current->ident, current->host);
+ Server->Users->QuitUser(current, "Excess flood");
+
+ if (current->registered != REG_ALL)
+ {
+ ZLine* zl = new ZLine(Server, Server->Time(), 0, Server->Config->ServerName, "Flood from unregistered connection", current->GetIPString());
+ if (Server->XLines->AddLine(zl,NULL))
+ Server->XLines->ApplyLines();
+ else
+ delete zl;
+ }
+}
+
+void ProcessUserHandler::Call(User* cu)
+{
+ int result = EAGAIN;
+
+ if (cu->GetFd() == FD_MAGIC_NUMBER)
+ return;
+
+ char* ReadBuffer = Server->GetReadBuffer();
+
+ if (cu->io)
+ {
+ int result2 = 0;
+ int MOD_RESULT = 0;
+
+ try
+ {
+ MOD_RESULT = cu->io->OnRawSocketRead(cu->GetFd(),ReadBuffer,Server->Config->NetBufferSize,result2);
+ }
+ catch (CoreException& modexcept)
+ {
+ Server->Logs->Log("USERS",DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
+ }
+
+ if (MOD_RESULT < 0)
+ {
+ result = -EAGAIN;
+ }
+ else
+ {
+ result = result2;
+ }
+ }
+ else
+ {
+ result = cu->ReadData(ReadBuffer, sizeof(ReadBuffer));
+ }
+
+ if ((result) && (result != -EAGAIN))
+ {
+ User *current;
+ int currfd;
+
+ Server->stats->statsRecv += result;
+ /*
+ * perform a check on the raw buffer as an array (not a string!) to remove
+ * character 0 which is illegal in the RFC - replace them with spaces.
+ */
+
+ for (int checker = 0; checker < result; checker++)
+ {
+ if (ReadBuffer[checker] == 0)
+ ReadBuffer[checker] = ' ';
+ }
+
+ if (result > 0)
+ ReadBuffer[result] = '\0';
+
+ current = cu;
+ currfd = current->GetFd();
+
+ // add the data to the users buffer
+ if (result > 0)
+ {
+ if (!current->AddBuffer(ReadBuffer))
+ {
+ // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
+ if (current->registered == REG_ALL)
+ {
+ if (current->MyClass)
+ {
+ // Make sure they arn't flooding long lines.
+ if (Server->Time() > current->reset_due)
+ {
+ current->reset_due = Server->Time() + current->MyClass->GetThreshold();
+ current->lines_in = 0;
+ }
+
+ current->lines_in++;
+
+ if (current->MyClass->GetFlood() && current->lines_in > current->MyClass->GetFlood())
+ Server->FloodQuitUser(current);
+ else
+ {
+ current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over %d chars) Please shorten it.", current->nick, MAXBUF-2);
+ current->recvq.clear();
+ }
+ }
+ }
+ else
+ Server->FloodQuitUser(current);
+
+ return;
+ }
+
+ /* If user is over penalty, dont process here, just build up */
+ if (!current->OverPenalty)
+ Server->Parser->DoLines(current);
+
+ return;
+ }
+
+ if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
+ {
+ Server->Users->QuitUser(cu, errno ? strerror(errno) : "EOF from client");
+ return;
+ }
+ }
+
+ // result EAGAIN means nothing read
+ else if ((result == EAGAIN) || (result == -EAGAIN))
+ {
+ /* do nothing */
+ }
+ else if (result == 0)
+ {
+ Server->Users->QuitUser(cu, "Connection closed");
+ return;
+ }
+}
+
+/**
+ * This function is called once a second from the mainloop.
+ * It is intended to do background checking on all the user structs, e.g.
+ * stuff like ping checks, registration timeouts, etc.
+ */
+void InspIRCd::DoBackgroundUserStuff()
+{
+ /*
+ * loop over all local users..
+ */
+ for (std::vector<User*>::iterator count2 = this->Users->local_users.begin(); count2 != this->Users->local_users.end(); count2++)
+ {
+ User *curr = *count2;
+
+ if (curr->Penalty)
+ {
+ curr->Penalty--;
+ if (curr->Penalty < 10)
+ Parser->DoLines(curr, true);
+ }
+
+ if (curr->OverPenalty)
+ {
+ if (curr->sendq.empty())
+ curr->OverPenalty = false;
+ }
+
+ if ((curr->registered != REG_ALL) && (TIME > curr->timeout))
+ {
+ /*
+ * registration timeout -- didnt send USER/NICK/HOST
+ * in the time specified in their connection class.
+ */
+ this->Users->QuitUser(curr, "Registration timeout");
+ continue;
+ }
+
+ /*
+ * `ready` means that the user has provided NICK/USER(/PASS), and all modules agree
+ * that the user is okay to proceed. The one thing we are then waiting for now is DNS...
+ */
+ bool ready = ((curr->registered == REG_NICKUSER) && AllModulesReportReady(curr));
+
+ if (ready)
+ {
+ if (curr->dns_done)
+ {
+ /* DNS passed, connect the user */
+ curr->FullConnect();
+ continue;
+ }
+ }
+
+ // It's time to PING this user. Send them a ping.
+ if ((TIME > curr->nping) && (curr->registered == REG_ALL))
+ {
+ // This user didn't answer the last ping, remove them
+ if (!curr->lastping)
+ {
+ time_t time = this->Time() - (curr->nping - curr->MyClass->GetPingTime());
+ char message[MAXBUF];
+ snprintf(message, MAXBUF, "Ping timeout: %ld second%s", (long)time, time > 1 ? "s" : "");
+ curr->lastping = 1;
+ curr->nping = TIME + curr->MyClass->GetPingTime();
+ this->Users->QuitUser(curr, message);
+ continue;
+ }
+
+ curr->Write("PING :%s",this->Config->ServerName);
+ curr->lastping = 0;
+ curr->nping = TIME +curr->MyClass->GetPingTime();
+ }
+ }
+}
+