]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_spanningtree.cpp
Mode +b stuff, probably wont work yet
[user/henk/code/inspircd.git] / src / modules / m_spanningtree.cpp
index 7a59960f4a428d0d58979cc62d511323488b3e3e..841206b8f4037023856304bc1bd75b3a20979bea 100644 (file)
@@ -198,6 +198,7 @@ class TreeServer
                VersionString = Srv->GetVersion();
                Route = NULL;
                Socket = NULL; /* Fix by brain */
+               DontModifyHash = false;
                AddHashEntry();
        }
 
@@ -209,6 +210,7 @@ class TreeServer
        {
                VersionString = "";
                UserCount = OperCount = 0;
+               DontModifyHash = false;
                this->SetNextPingTime(time(NULL) + 120);
                this->SetPingFlag();
 
@@ -269,7 +271,10 @@ class TreeServer
        void AddUser(userrec* user)
        {
                if (this->DontModifyHash)
+               {
+                       log(DEBUG,"Not modifying hash");
                        return;
+               }
 
                log(DEBUG,"Add user %s to server %s",user->nick,this->ServerName.c_str());
                std::map<userrec*,userrec*>::iterator iter;
@@ -286,7 +291,10 @@ class TreeServer
                 * When netsplitting, this->DontModifyHash is set to prevent it now!
                 */
                if (this->DontModifyHash)
+               {
+                       log(DEBUG,"Not modifying hash");
                        return;
+               }
 
                log(DEBUG,"Remove user %s from server %s",user->nick,this->ServerName.c_str());
                std::map<userrec*,userrec*>::iterator iter;
@@ -298,7 +306,7 @@ class TreeServer
        int QuitUsers(const std::string &reason)
        {
                int x = Users.size();
-               log(DEBUG,"Removing all users from server %s",this->ServerName.c_str());
+               log(DEBUG,"Removing %d users from server %s",x,this->ServerName.c_str());
                const char* reason_s = reason.c_str();
                this->DontModifyHash = true;
                for (std::map<userrec*,userrec*>::iterator n = Users.begin(); n != Users.end(); n++)
@@ -957,7 +965,7 @@ class TreeSocket : public InspSocket
                                        userrec* user = Srv->FindNick(source);
                                        if (!user)
                                        {
-                                               WriteChannelWithServ((char*)source.c_str(), c, "TOPIC %s :%s", c->name, c->topic);
+                                               WriteChannelWithServ(source.c_str(), c, "TOPIC %s :%s", c->name, c->topic);
                                        }
                                        else
                                        {
@@ -1022,7 +1030,7 @@ class TreeSocket : public InspSocket
                for (unsigned int usernum = 2; usernum < params.size(); usernum++)
                {
                        /* process one channel at a time, applying modes. */
-                       char* usr = (char*)params[usernum].c_str();
+                       char* usr = const_cast<char*>(params[usernum].c_str());
                        /* Safety check just to make sure someones not sent us an FJOIN full of spaces
                         * (is this even possible?) */
                        if (usr && *usr)
@@ -1139,18 +1147,17 @@ class TreeSocket : public InspSocket
                // NICK age nick host dhost ident +modes ip :gecos
                //   0   123  4 56   7
                time_t age = atoi(params[0].c_str());
-               std::string modes = params[5];
-               while (*(modes.c_str()) == '+')
-               {
-                       char* m = (char*)modes.c_str();
-                       m++;
-                       modes = m;
-               }
-               char* tempnick = (char*)params[1].c_str();
+               
+               /* This used to have a pretty craq'y loop doing the same thing,
+                * now we just let the STL do the hard work (more efficiently)
+                */
+               params[5] = params[5].substr(params[5].find_first_not_of('+'));
+               
+               const char* tempnick = params[1].c_str();
                log(DEBUG,"Introduce client %s!%s@%s",tempnick,params[4].c_str(),params[2].c_str());
                
-               user_hash::iterator iter;
-               iter = clientlist.find(tempnick);
+               user_hash::iterator iter = clientlist.find(tempnick);
+               
                if (iter != clientlist.end())
                {
                        // nick collision
@@ -1164,12 +1171,13 @@ class TreeSocket : public InspSocket
                strlcpy(clientlist[tempnick]->nick, tempnick,NICKMAX-1);
                strlcpy(clientlist[tempnick]->host, params[2].c_str(),63);
                strlcpy(clientlist[tempnick]->dhost, params[3].c_str(),63);
-               clientlist[tempnick]->server = (char*)FindServerNamePtr(source.c_str());
+               clientlist[tempnick]->server = FindServerNamePtr(source.c_str());
                strlcpy(clientlist[tempnick]->ident, params[4].c_str(),IDENTMAX);
                strlcpy(clientlist[tempnick]->fullname, params[7].c_str(),MAXGECOS);
                clientlist[tempnick]->registered = 7;
                clientlist[tempnick]->signon = age;
-               strlcpy(clientlist[tempnick]->modes, modes.c_str(),53);
+               strlcpy(clientlist[tempnick]->modes, params[5].c_str(),53);
+               
                for (char *v = clientlist[tempnick]->modes; *v; v++)
                {
                        switch (*v)
@@ -1189,7 +1197,7 @@ class TreeSocket : public InspSocket
                }
                inet_aton(params[6].c_str(),&clientlist[tempnick]->ip4);
 
-               WriteOpers("*** Client connecting at %s: %s!%s@%s [%s]",clientlist[tempnick]->server,clientlist[tempnick]->nick,clientlist[tempnick]->ident,clientlist[tempnick]->host,(char*)inet_ntoa(clientlist[tempnick]->ip4));
+               WriteOpers("*** Client connecting at %s: %s!%s@%s [%s]",clientlist[tempnick]->server,clientlist[tempnick]->nick,clientlist[tempnick]->ident,clientlist[tempnick]->host, inet_ntoa(clientlist[tempnick]->ip4));
 
                params[7] = ":" + params[7];
                DoOneToAllButSender(source,"NICK",params,source);
@@ -1198,6 +1206,7 @@ class TreeSocket : public InspSocket
                TreeServer* SourceServer = FindServer(source);
                if (SourceServer)
                {
+                       log(DEBUG,"Found source server of %s",clientlist[tempnick]->nick);
                        SourceServer->AddUser(clientlist[tempnick]);
                        SourceServer->AddUserCount();
                }
@@ -1382,7 +1391,7 @@ class TreeSocket : public InspSocket
                {
                        if (u->second->registered == 7)
                        {
-                               snprintf(data,MAXBUF,":%s NICK %lu %s %s %s %s +%s %s :%s",u->second->server,(unsigned long)u->second->age,u->second->nick,u->second->host,u->second->dhost,u->second->ident,u->second->modes,(char*)inet_ntoa(u->second->ip4),u->second->fullname);
+                               snprintf(data,MAXBUF,":%s NICK %lu %s %s %s %s +%s %s :%s",u->second->server,(unsigned long)u->second->age,u->second->nick,u->second->host,u->second->dhost,u->second->ident,u->second->modes,inet_ntoa(u->second->ip4),u->second->fullname);
                                this->WriteLine(data);
                                if (*u->second->oper)
                                {
@@ -1573,7 +1582,9 @@ class TreeSocket : public InspSocket
                        {
                                std::deque<std::string> par;
                                par.push_back(params[1]);
-                               DoOneToMany(u->nick,"NICK",par);
+                               /* This is not required as one is sent in OnUserPostNick below
+                                */
+                               //DoOneToMany(u->nick,"NICK",par);
                                Srv->ChangeUserNick(u,params[1]);
                                u->age = atoi(params[2].c_str());
                        }
@@ -1637,6 +1648,7 @@ class TreeSocket : public InspSocket
                        std::string reason = params[1];
                        params[1] = ":" + params[1];
                        DoOneToAllButSender(prefix,"KILL",params,sourceserv);
+                       ::Write(who->fd, ":%s KILL %s :%s (%s)", sourceserv.c_str(), who->nick, sourceserv.c_str(), reason.c_str());
                        Srv->QuitUser(who,reason);
                }
                return true;
@@ -1761,19 +1773,19 @@ class TreeSocket : public InspSocket
                {
                        case 'Z':
                                propogate = add_zline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
-                               zline_set_creation_time((char*)params[1].c_str(), atoi(params[3].c_str()));
+                               zline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
                        break;
                        case 'Q':
                                propogate = add_qline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
-                               qline_set_creation_time((char*)params[1].c_str(), atoi(params[3].c_str()));
+                               qline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
                        break;
                        case 'E':
                                propogate = add_eline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
-                               eline_set_creation_time((char*)params[1].c_str(), atoi(params[3].c_str()));
+                               eline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
                        break;
                        case 'G':
                                propogate = add_gline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
-                               gline_set_creation_time((char*)params[1].c_str(), atoi(params[3].c_str()));
+                               gline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
                        break;
                        case 'K':
                                propogate = add_kline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
@@ -1872,7 +1884,7 @@ class TreeSocket : public InspSocket
                                        unsigned long signon = atoi(params[1].c_str());
                                        unsigned long idle = atoi(params[2].c_str());
                                        if ((who_to_send_to) && (who_to_send_to->fd > -1))
-                                               do_whois(who_to_send_to,u,signon,idle,(char*)nick_whoised.c_str());
+                                               do_whois(who_to_send_to,u,signon,idle,nick_whoised.c_str());
                                }
                                else
                                {
@@ -2014,8 +2026,8 @@ class TreeSocket : public InspSocket
                TreeServer* CheckDupe = FindServer(servername);
                if (CheckDupe)
                {
-                       this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
-                       Srv->SendOpers("*** Server connection from \2"+servername+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
+                       this->WriteLine("ERROR :Server "+servername+" already exists!");
+                       Srv->SendOpers("*** Server connection from \2"+servername+"\2 denied, already exists");
                        return false;
                }
                TreeServer* Node = new TreeServer(servername,description,ParentOfThis,NULL);
@@ -2201,37 +2213,28 @@ class TreeSocket : public InspSocket
 
        bool ProcessLine(std::string line)
        {
-               char* l = (char*)line.c_str();
-               for (char* x = l; *x; x++)
-               {
-                       if ((*x == '\r') || (*x == '\n'))
-                               *x = 0;
-               }
-               if (!*l)
-                       return true;
-
-               log(DEBUG,"IN: %s",l);
-
                std::deque<std::string> params;
-               this->Split(l,true,params);
-               irc::string command = "";
-               std::string prefix = "";
-               if (((params[0].c_str())[0] == ':') && (params.size() > 1))
-               {
-                       prefix = params[0];
-                       command = params[1].c_str();
-                       char* pref = (char*)prefix.c_str();
-                       prefix = ++pref;
-                       params.pop_front();
-                       params.pop_front();
-               }
-               else
+               irc::string command;
+               std::string prefix;
+               
+               if (line.empty())
+                       return true;
+               
+               line = line.substr(0, line.find_first_of("\r\n"));
+               
+               log(DEBUG,"IN: %s", line.c_str());
+               
+               this->Split(line.c_str(),true,params);
+                       
+               if ((params[0][0] == ':') && (params.size() > 1))
                {
-                       prefix = "";
-                       command = params[0].c_str();
+                       prefix = params[0].substr(1);
                        params.pop_front();
                }
 
+               command = params[0].c_str();
+               params.pop_front();
+
                if ((!this->ctx_in) && (command == "AES"))
                {
                        std::string sserv = params[0];
@@ -2596,7 +2599,7 @@ class TreeSocket : public InspSocket
                                                         * and our copy.
                                                         */
                                                        userrec* x = Srv->FindNick(params[0]);
-                                                       if (x)
+                                                       if ((x) && (x != who))
                                                        {
                                                                std::deque<std::string> p;
                                                                p.push_back(params[0]);
@@ -2610,6 +2613,11 @@ class TreeSocket : public InspSocket
                                                                userrec* y = Srv->FindNick(prefix);
                                                                if (y)
                                                                {
+                                                                       TreeServer* n = FindServer(y->server);
+                                                                       if (n)
+                                                                       {
+                                                                               n->DelUser(y);
+                                                                       }       
                                                                        Srv->QuitUser(y,"Nickname collision");
                                                                }
                                                                return DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
@@ -2688,6 +2696,38 @@ class TreeSocket : public InspSocket
 
        virtual int OnIncomingConnection(int newsock, char* ip)
        {
+               /* To prevent anyone from attempting to flood opers/DDoS by connecting to the server port,
+                * or discovering if this port is the server port, we don't allow connections from any
+                * IPs for which we don't have a link block.
+                */
+               bool found = false;
+               char resolved_host[MAXBUF];
+               vector<Link>::iterator i;
+               for (i = LinkBlocks.begin(); i != LinkBlocks.end(); i++)
+               {
+                       if (i->IPAddr == ip)
+                       {
+                               found = true;
+                               break;
+                       }
+                       /* XXX: Fixme: blocks for a very short amount of time,
+                        * we should cache these on rehash/startup
+                        */
+                       if (CleanAndResolve(resolved_host,i->IPAddr.c_str(),true))
+                       {
+                               if (std::string(resolved_host) == ip)
+                               {
+                                       found = true;
+                                       break;
+                               }
+                       }
+               }
+               if (!found)
+               {
+                       WriteOpers("Server connection from %s denied (no link blocks with that IP address)", ip);
+                       close(newsock);
+                       return false;
+               }
                TreeSocket* s = new TreeSocket(newsock, ip);
                Srv->AddSocket(s);
                return true;
@@ -3655,6 +3695,7 @@ class ModuleSpanningTree : public Module
                if (SourceServer)
                {
                        SourceServer->DelUserCount();
+                       SourceServer->DelUser(user);
                }
 
        }
@@ -3695,6 +3736,17 @@ class ModuleSpanningTree : public Module
                params.push_back(dest->nick);
                params.push_back(":"+reason);
                DoOneToMany(source->nick,"KILL",params);
+               /* NOTE: We must remove the user from the servers list here.
+                * If we do not, there is a chance the user could hang around
+                * in the list if there is a desync for example (this would
+                * not be good).
+                * Part of the 'random crash on netsplit' tidying up. -Brain
+                */
+               TreeServer* n = FindServer(dest->server);
+               if (n)
+               {
+                       n->DelUser(dest);
+               }
        }
 
        virtual void OnRehash(const std::string &parameter)