]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/users.cpp
Whoops, ++it++? Thats gonna break :p
[user/henk/code/inspircd.git] / src / users.cpp
index e404d9107bc16db584cbabd5f6cb4681d6850063..906a2e9c3dccda8936052850b3ccae66d11cc3ba 100644 (file)
  * ---------------------------------------------------
  */
 
+#include "inspircd.h"
 #include "configreader.h"
 #include "channels.h"
 #include "users.h"
-#include "inspircd.h"
 #include <stdarg.h>
 #include "socketengine.h"
 #include "wildcard.h"
@@ -61,7 +61,7 @@ bool DoType(ServerConfig* conf, const char* tag, char** entries, ValueList &valu
        const char* TypeName = values[0].GetString();
        const char* Classes = values[1].GetString();
 
-       conf->opertypes[TypeName] = strdup(Classes);
+       conf->opertypes[TypeName] = strnewdup(Classes);
        return true;
 }
 
@@ -70,7 +70,7 @@ bool DoClass(ServerConfig* conf, const char* tag, char** entries, ValueList &val
        const char* ClassName = values[0].GetString();
        const char* CommandList = values[1].GetString();
 
-       conf->operclass[ClassName] = strdup(CommandList);
+       conf->operclass[ClassName] = strnewdup(CommandList);
        return true;
 }
 
@@ -473,7 +473,11 @@ int userrec::ReadData(void* buffer, size_t size)
 {
        if (IS_LOCAL(this))
        {
+#ifndef WIN32
                return read(this->fd, buffer, size);
+#else
+               return recv(this->fd, (char*)buffer, size, 0);
+#endif
        }
        else
                return 0;
@@ -646,8 +650,12 @@ std::string userrec::GetBuffer()
                 * Usually there are only one or two of these,
                 * so its is computationally cheap to do.
                 */
-               while ((*recvq.begin() == '\r') || (*recvq.begin() == '\n'))
-                       recvq.erase(recvq.begin());
+               std::string::iterator t = recvq.begin();
+               while (t != recvq.end() && (*t == '\r' || *t == '\n'))
+               {
+                       recvq.erase(t);
+                       t = recvq.begin();
+               }
 
                for (std::string::iterator x = recvq.begin(); x != recvq.end(); x++)
                {
@@ -690,8 +698,8 @@ void userrec::AddWriteBuf(const std::string &data)
 
        try
        {
-               if (data.length() > 512)
-                       sendq.append(data.substr(0,510)).append("\r\n");
+               if (data.length() > MAXBUF - 2) /* MAXBUF has a value of 514, to account for line terminators */
+                       sendq.append(data.substr(0,MAXBUF - 4)).append("\r\n"); /* MAXBUF-4 = 510 */
                else
                        sendq.append(data);
        }
@@ -714,7 +722,11 @@ void userrec::FlushWriteBuf()
                if ((sendq.length()) && (this->fd != FD_MAGIC_NUMBER))
                {
                        int old_sendq_length = sendq.length();
-                       int n_sent = write(this->fd, this->sendq.data(), this->sendq.length());
+#ifndef WIN32
+               int n_sent = write(this->fd, this->sendq.data(), this->sendq.length());
+#else
+               int n_sent = send(this->fd, (const char*)this->sendq.data(), this->sendq.length(), 0);
+#endif
                        if (n_sent == -1)
                        {
                                if (errno == EAGAIN)
@@ -900,14 +912,20 @@ void userrec::AddClient(InspIRCd* Instance, int socket, int port, bool iscached,
        Instance->AddLocalClone(New);
        Instance->AddGlobalClone(New);
 
+       /*
+        * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
+        * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
+        */
        ConnectClass* i = New->GetClass();
 
-       if ((!i) || (i->GetType() == CC_DENY))
+       if (!i)
        {
-               userrec::QuitUser(Instance, New,"Unauthorised connection");
+               userrec::QuitUser(Instance, New, "Access denied by configuration");
                return;
        }
 
+       New->CheckClass();
+
        New->pingmax = i->GetPingTime();
        New->nping = Instance->Time() + i->GetPingTime() + Instance->Config->dns_timeout;
        New->timeout = Instance->Time() + i->GetRegTimeout();
@@ -935,11 +953,13 @@ void userrec::AddClient(InspIRCd* Instance, int socket, int port, bool iscached,
         * which for the time being is a physical impossibility (even the largest networks dont have more
         * than about 10,000 users on ONE server!)
         */
+#ifndef WINDOWS
        if ((unsigned int)socket >= MAX_DESCRIPTORS)
        {
                userrec::QuitUser(Instance, New, "Server is full");
                return;
        }
+#endif
 
        New->exempt = (Instance->XLines->matches_exception(New) != NULL);
        if (!New->exempt)
@@ -987,41 +1007,57 @@ unsigned long userrec::LocalCloneCount()
                return 0;
 }
 
-void userrec::FullConnect()
+/*
+ * Check class restrictions
+ */
+void userrec::CheckClass()
 {
-       ServerInstance->stats->statsConnects++;
-       this->idle_lastmsg = ServerInstance->Time();
-
        ConnectClass* a = this->GetClass();
 
        if ((!a) || (a->GetType() == CC_DENY))
        {
-               this->muted = true;
-               ServerInstance->GlobalCulls.AddItem(this,"Unauthorised connection");
+               userrec::QuitUser(ServerInstance, this, "Unauthorised connection");
                return;
        }
 
        if ((!a->GetPass().empty()) && (!this->haspassed))
        {
-               this->muted = true;
-               ServerInstance->GlobalCulls.AddItem(this,"Invalid password");
+               userrec::QuitUser(ServerInstance, this, "Invalid password");
+               return;
+       }
+
+       if ((!a) || (a->GetType() == CC_DENY))
+       {
+               userrec::QuitUser(ServerInstance, this,"Unauthorised connection");
                return;
        }
 
        if ((a->GetMaxLocal()) && (this->LocalCloneCount() > a->GetMaxLocal()))
        {
-               this->muted = true;
-               ServerInstance->GlobalCulls.AddItem(this, "No more connections allowed from your host via this connect class (local)");
+               userrec::QuitUser(ServerInstance, this, "No more connections allowed from your host via this connect class (local)");
                ServerInstance->WriteOpers("*** WARNING: maximum LOCAL connections (%ld) exceeded for IP %s", a->GetMaxLocal(), this->GetIPString());
                return;
        }
        else if ((a->GetMaxGlobal()) && (this->GlobalCloneCount() > a->GetMaxGlobal()))
        {
-               this->muted = true;
-               ServerInstance->GlobalCulls.AddItem(this, "No more connections allowed from your host via this connect class (global)");
-               ServerInstance->WriteOpers("*** WARNING: maximum GLOBAL connections (%ld) exceeded for IP %s",a->GetMaxGlobal(), this->GetIPString());
+               userrec::QuitUser(ServerInstance, this, "No more connections allowed from your host via this connect class (global)");
+               ServerInstance->WriteOpers("*** WARNING: maximum GLOBAL connections (%ld) exceeded for IP %s", a->GetMaxGlobal(), this->GetIPString());
                return;
        }
+}
+
+void userrec::FullConnect()
+{
+       ServerInstance->stats->statsConnects++;
+       this->idle_lastmsg = ServerInstance->Time();
+
+       /*
+        * You may be thinking "wtf, we checked this in userrec::AddClient!" - and yes, we did, BUT.
+        * At the time AddClient is called, we don't have a resolved host, by here we probably do - which
+        * may put the user into a totally seperate class with different restrictions! so we *must* check again.
+        * Don't remove this! -- w00t
+        */
+       this->CheckClass();
 
        if (!this->exempt)
        {
@@ -1321,12 +1357,16 @@ const char* userrec::GetIPString(char* buf)
  */
 void userrec::Write(std::string text)
 {
+#ifdef WINDOWS
+       if ((this->fd < 0) || (this->m_internalFd > MAX_DESCRIPTORS))
+#else
        if ((this->fd < 0) || (this->fd > MAX_DESCRIPTORS))
+#endif
                return;
 
        try
        {
-               /* ServerInstance->Log(DEBUG,"<- %s", text.c_str());
+               /* ServerInstance->Log(DEBUG,"C[%d] <- %s", this->GetFd(), text.c_str());
                 * WARNING: The above debug line is VERY loud, do NOT
                 * enable it till we have a good way of filtering it
                 * out of the logs (e.g. 1.2 would be good).
@@ -1585,17 +1625,7 @@ void userrec::WriteWallOps(const std::string &text)
        if (!IS_OPER(this) && IS_LOCAL(this))
                return;
 
-       std::string wallop = "WALLOPS :";
-
-       try
-       {
-               wallop.append(text);
-       }
-       catch (...)
-       {
-               ServerInstance->Log(DEBUG,"Exception in userrec::Write() std::string::append");
-               return;
-       }
+       std::string wallop = "WALLOPS :" + text;
 
        for (std::vector<userrec*>::const_iterator i = ServerInstance->local_users.begin(); i != ServerInstance->local_users.end(); i++)
        {