]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/users.cpp
Undo my constructor playing, I'm pretty sure it will break stuff. This will require...
[user/henk/code/inspircd.git] / src / users.cpp
index 34c7735fe3fb80d9dca1b223327b7b91dc7f1c9e..d7671de3c8a5a6496cdb26ec1aaebb145a6ba908 100644 (file)
@@ -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
@@ -190,6 +190,7 @@ User::User(InspIRCd* Instance, const std::string &uid) : ServerInstance(Instance
        Visibility = NULL;
        ip = NULL;
        MyClass = NULL;
+       AllowedOperCommands = NULL;
        chans.clear();
        invites.clear();
        memset(modes,0,sizeof(modes));
@@ -211,29 +212,6 @@ User::User(InspIRCd* Instance, const std::string &uid) : ServerInstance(Instance
                throw CoreException("Duplicate UUID "+std::string(uuid)+" in User constructor");
 }
 
-void User::RemoveCloneCounts()
-{
-       clonemap::iterator x = ServerInstance->local_clones.find(this->GetIPString());
-       if (x != ServerInstance->local_clones.end())
-       {
-               x->second--;
-               if (!x->second)
-               {
-                       ServerInstance->local_clones.erase(x);
-               }
-       }
-       
-       clonemap::iterator y = ServerInstance->global_clones.find(this->GetIPString());
-       if (y != ServerInstance->global_clones.end())
-       {
-               y->second--;
-               if (!y->second)
-               {
-                       ServerInstance->global_clones.erase(y);
-               }
-       }
-}
-
 User::~User()
 {
        /* NULL for remote users :) */
@@ -242,6 +220,11 @@ User::~User()
                this->MyClass->RefCount--;
                ServerInstance->Log(DEBUG, "User destructor -- connect refcount now: %u", this->MyClass->RefCount);
        }
+       if (this->AllowedOperCommands)
+       {
+               delete AllowedOperCommands;
+               AllowedOperCommands = NULL;
+       }
 
        this->InvalidateCache();
        this->DecrementModes();
@@ -249,7 +232,7 @@ User::~User()
                free(operquit);
        if (ip)
        {
-               this->RemoveCloneCounts();
+               ServerInstance->Users->RemoveCloneCounts(this);
 
                if (this->GetProtocolFamily() == AF_INET)
                {
@@ -419,10 +402,6 @@ void User::RemoveInvite(const irc::string &channel)
 
 bool User::HasPermission(const std::string &command)
 {
-       char* mycmd;
-       char* savept;
-       char* savept2;
-
        /*
         * users on remote servers can completely bypass all permissions based checks.
         * This prevents desyncs when one server has different type/class tags to another.
@@ -439,38 +418,13 @@ bool User::HasPermission(const std::string &command)
                return false;
        }
 
-       // check their opertype exists (!). This won't affect local users, of course.
-       opertype_t::iterator iter_opertype = ServerInstance->Config->opertypes.find(this->oper);
-       if (iter_opertype == ServerInstance->Config->opertypes.end())
-       {
+       if (!AllowedOperCommands)
                return false;
-       }
 
-       /* XXX all this strtok/strdup stuff is a bit ick and horrid -- w00t */
-       char* Classes = strdup(iter_opertype->second);
-       char* myclass = strtok_r(Classes," ",&savept);
-       while (myclass)
-       {
-               operclass_t::iterator iter_operclass = ServerInstance->Config->operclass.find(myclass);
-               if (iter_operclass != ServerInstance->Config->operclass.end())
-               {
-                       char* CommandList = strdup(iter_operclass->second);
-                       mycmd = strtok_r(CommandList," ",&savept2);
-                       while (mycmd)
-                       {
-                               if ((!strcasecmp(mycmd,command.c_str())) || (*mycmd == '*'))
-                               {
-                                       free(Classes);
-                                       free(CommandList);
-                                       return true;
-                               }
-                               mycmd = strtok_r(NULL," ",&savept2);
-                       }
-                       free(CommandList);
-               }
-               myclass = strtok_r(NULL," ",&savept);
-       }
-       free(Classes);
+       if (AllowedOperCommands->find(command) != AllowedOperCommands->end())
+               return true;
+       else if (AllowedOperCommands->find("*") != AllowedOperCommands->end())
+               return true;
 
        return false;
 }
@@ -670,8 +624,12 @@ const char* User::GetWriteError()
        return this->WriteError.c_str();
 }
 
-void User::Oper(const std::string &opertype)
+void User::Oper(const std::string &opertype, const std::string &opername)
 {
+       char* mycmd;
+       char* savept;
+       char* savept2;
+
        try
        {
                this->modes[UM_OPERATOR] = 1;
@@ -680,7 +638,38 @@ void User::Oper(const std::string &opertype)
                ServerInstance->Log(DEFAULT,"OPER: %s!%s@%s opered as type: %s", this->nick, this->ident, this->host, opertype.c_str());
                strlcpy(this->oper, opertype.c_str(), NICKMAX - 1);
                ServerInstance->all_opers.push_back(this);
-               FOREACH_MOD(I_OnPostOper,OnPostOper(this, opertype));
+
+               opertype_t::iterator iter_opertype = ServerInstance->Config->opertypes.find(this->oper);
+               if (iter_opertype != ServerInstance->Config->opertypes.end())
+               {
+
+                       if (AllowedOperCommands)
+                               AllowedOperCommands->clear();
+                       else
+                               AllowedOperCommands = new std::map<std::string, bool>;
+
+                       char* Classes = strdup(iter_opertype->second);
+                       char* myclass = strtok_r(Classes," ",&savept);
+                       while (myclass)
+                       {
+                               operclass_t::iterator iter_operclass = ServerInstance->Config->operclass.find(myclass);
+                               if (iter_operclass != ServerInstance->Config->operclass.end())
+                               {
+                                       char* CommandList = strdup(iter_operclass->second);
+                                       mycmd = strtok_r(CommandList," ",&savept2);
+                                       while (mycmd)
+                                       {
+                                               this->AllowedOperCommands->insert(std::make_pair(mycmd, true));
+                                               mycmd = strtok_r(NULL," ",&savept2);
+                                       }
+                                       free(CommandList);
+                               }
+                               myclass = strtok_r(NULL," ",&savept);
+                       }
+                       free(Classes);
+               }
+
+               FOREACH_MOD(I_OnPostOper,OnPostOper(this, opertype, opername));
        }
 
        catch (...)
@@ -701,6 +690,12 @@ void User::UnOper()
                        
                        // remove the user from the oper list. Will remove multiple entries as a safeguard against bug #404
                        ServerInstance->all_opers.remove(this);
+
+                       if (AllowedOperCommands)
+                       {
+                               delete AllowedOperCommands;
+                               AllowedOperCommands = NULL;
+                       }
                }
        }
 
@@ -730,191 +725,6 @@ void User::AddToWhoWas()
        }
 }
 
-/* add a client connection to the sockets list */
-void User::AddClient(InspIRCd* Instance, int socket, int port, bool iscached, int socketfamily, sockaddr* ip)
-{
-       /* NOTE: Calling this one parameter constructor for User automatically
-        * allocates a new UUID and places it in the hash_map.
-        */
-       User* New = NULL;
-       try
-       {
-               New = new User(Instance);
-       }
-       catch (...)
-       {
-               Instance->Log(DEFAULT,"*** WTF *** Duplicated UUID! -- Crack smoking monkies have been unleashed.");
-               Instance->WriteOpers("*** WARNING *** Duplicate UUID allocated!");
-               return;
-       }
-
-       Instance->Log(DEBUG,"New user fd: %d", socket);
-
-       int j = 0;
-
-       Instance->unregistered_count++;
-
-       char ipaddr[MAXBUF];
-#ifdef IPV6
-       if (socketfamily == AF_INET6)
-               inet_ntop(AF_INET6, &((const sockaddr_in6*)ip)->sin6_addr, ipaddr, sizeof(ipaddr));
-       else
-#endif
-       inet_ntop(AF_INET, &((const sockaddr_in*)ip)->sin_addr, ipaddr, sizeof(ipaddr));
-
-       (*(Instance->clientlist))[New->uuid] = New;
-       New->SetFd(socket);
-
-       /* The users default nick is their UUID */
-       strlcpy(New->nick, New->uuid, NICKMAX - 1);
-
-       New->server = Instance->FindServerNamePtr(Instance->Config->ServerName);
-       /* We don't need range checking here, we KNOW 'unknown\0' will fit into the ident field. */
-       strcpy(New->ident, "unknown");
-
-       New->registered = REG_NONE;
-       New->signon = Instance->Time() + Instance->Config->dns_timeout;
-       New->lastping = 1;
-
-       New->SetSockAddr(socketfamily, ipaddr, port);
-
-       /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
-       for (const char* temp = New->GetIPString(); *temp && j < 64; temp++, j++)
-               New->dhost[j] = New->host[j] = *temp;
-       New->dhost[j] = New->host[j] = 0;
-
-       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->SetClass();
-
-       if (!i)
-       {
-               User::QuitUser(Instance, New, "Access denied by configuration");
-               return;
-       }
-
-       /*
-        * Check connect class settings and initialise settings into User.
-        * This will be done again after DNS resolution. -- w00t
-        */
-       New->CheckClass();
-
-       Instance->local_users.push_back(New);
-
-       if ((Instance->local_users.size() > Instance->Config->SoftLimit) || (Instance->local_users.size() >= MAXCLIENTS))
-       {
-               Instance->WriteOpers("*** Warning: softlimit value has been reached: %d clients", Instance->Config->SoftLimit);
-               User::QuitUser(Instance, New,"No more connections allowed");
-               return;
-       }
-
-       /*
-        * XXX -
-        * this is done as a safety check to keep the file descriptors within range of fd_ref_table.
-        * its a pretty big but for the moment valid assumption:
-        * file descriptors are handed out starting at 0, and are recycled as theyre freed.
-        * therefore if there is ever an fd over 65535, 65536 clients must be connected to the
-        * irc server at once (or the irc server otherwise initiating this many connections, files etc)
-        * 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)
-       {
-               User::QuitUser(Instance, New, "Server is full");
-               return;
-       }
-#endif
-       /*
-        * even with bancache, we still have to keep User::exempt current.
-        * besides that, if we get a positive bancache hit, we still won't fuck
-        * them over if they are exempt. -- w00t
-        */
-       New->exempt = (Instance->XLines->MatchesLine("E",New) != NULL);
-
-       if (BanCacheHit *b = Instance->BanCache->GetHit(New->GetIPString()))
-       {
-               if (!b->Type.empty() && !New->exempt)
-               {
-                       /* user banned */
-                       Instance->Log(DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
-                       if (*Instance->Config->MoronBanner)
-                               New->WriteServ("NOTICE %s :*** %s", New->nick, Instance->Config->MoronBanner);
-                       User::QuitUser(Instance, New, b->Reason);
-                       return;
-               }
-               else
-               {
-                       Instance->Log(DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
-               }
-       }
-       else
-       {
-               if (!New->exempt)
-               {
-                       XLine* r = Instance->XLines->MatchesLine("Z",New);
-
-                       if (r)
-                       {
-                               Instance->Log(DEBUG, std::string("BanCache: Adding positive hit for ") + New->GetIPString());
-                               Instance->BanCache->AddHit(New->GetIPString(), "Z", std::string("Z-Lined: ") + r->reason);
-                               char reason[MAXBUF];
-                               if (*Instance->Config->MoronBanner)
-                                       New->WriteServ("NOTICE %s :*** %s", New->nick, Instance->Config->MoronBanner);
-                               snprintf(reason,MAXBUF,"Z-Lined: %s",r->reason);
-                               User::QuitUser(Instance, New, reason);
-                               return;
-                       }
-               }
-       }
-
-        if (socket > -1)
-        {
-                if (!Instance->SE->AddFd(New))
-                {
-                       Instance->Log(DEBUG,"Internal error on new connection");
-                       User::QuitUser(Instance, New, "Internal error handling connection");
-                }
-        }
-
-       /* NOTE: even if dns lookups are *off*, we still need to display this.
-        * BOPM and other stuff requires it.
-        */
-       New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
-
-       if (Instance->Config->NoUserDns)
-       {
-               New->dns_done = true;
-       }
-       else
-       {
-               New->StartDNSLookup();
-       }
-}
-
-unsigned long User::GlobalCloneCount()
-{
-       clonemap::iterator x = ServerInstance->global_clones.find(this->GetIPString());
-       if (x != ServerInstance->global_clones.end())
-               return x->second;
-       else
-               return 0;
-}
-
-unsigned long User::LocalCloneCount()
-{
-       clonemap::iterator x = ServerInstance->local_clones.find(this->GetIPString());
-       if (x != ServerInstance->local_clones.end())
-               return x->second;
-       else
-               return 0;
-}
-
 /*
  * Check class restrictions
  */
@@ -927,13 +737,13 @@ void User::CheckClass()
                User::QuitUser(ServerInstance, this, "Unauthorised connection");
                return;
        }
-       else if ((a->GetMaxLocal()) && (this->LocalCloneCount() > a->GetMaxLocal()))
+       else if ((a->GetMaxLocal()) && (ServerInstance->Users->LocalCloneCount(this) > a->GetMaxLocal()))
        {
                User::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()))
+       else if ((a->GetMaxGlobal()) && (ServerInstance->Users->GlobalCloneCount(this) > a->GetMaxGlobal()))
        {
                User::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());
@@ -969,29 +779,21 @@ void User::FullConnect()
 
        if (!this->exempt)
        {
-               XLine* r = ServerInstance->XLines->MatchesLine("G",this);
+               GLine *r = (GLine *)ServerInstance->XLines->MatchesLine("G", this);
 
                if (r)
                {
                        this->muted = true;
-                       char reason[MAXBUF];
-                       if (*ServerInstance->Config->MoronBanner)
-                               this->WriteServ("NOTICE %s :*** %s", this->nick, ServerInstance->Config->MoronBanner);
-                       snprintf(reason,MAXBUF,"G-Lined: %s",r->reason);
-                       User::QuitUser(ServerInstance, this, reason);
+                       r->Apply(this);
                        return;
                }
 
-               XLine* n = ServerInstance->XLines->MatchesLine("K",this);
+               KLine *n = (KLine *)ServerInstance->XLines->MatchesLine("K", this);
 
                if (n)
                {
                        this->muted = true;
-                       char reason[MAXBUF];
-                       if (*ServerInstance->Config->MoronBanner)
-                               this->WriteServ("NOTICE %s :*** %s", this, ServerInstance->Config->MoronBanner);
-                       snprintf(reason,MAXBUF,"K-Lined: %s",n->reason);
-                       User::QuitUser(ServerInstance, this, reason);
+                       n->Apply(this);
                        return;
                }
        }
@@ -1030,6 +832,9 @@ void User::FullConnect()
        FOREACH_MOD(I_OnPostConnect,OnPostConnect(this));
 
        ServerInstance->SNO->WriteToSnoMask('c',"Client connecting on port %d: %s!%s@%s [%s] [%s]", this->GetPort(), this->nick, this->ident, this->host, this->GetIPString(), this->fullname);
+
+       ServerInstance->Log(DEBUG, "BanCache: Adding NEGATIVE hit for %s", this->GetIPString());
+       ServerInstance->BanCache->AddHit(this->GetIPString(), "", "");
 }
 
 /** User::UpdateNick()
@@ -1853,7 +1658,7 @@ void User::PurgeEmptyChannels()
                if (i2 != ServerInstance->chanlist->end())
                {
                        FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
-                       DELETE(i2->second);
+                       delete i2->second;
                        ServerInstance->chanlist->erase(i2);
                        this->chans.erase(*n);
                }