]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/uid.cpp
Prevent using invalid UIDs and enforce UID/SID matching
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / uid.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "commands.h"
16
17 #include "utils.h"
18 #include "link.h"
19 #include "treesocket.h"
20 #include "treeserver.h"
21 #include "resolvers.h"
22
23 CmdResult CommandUID::Handle(const parameterlist &params, User* serversrc)
24 {
25         SpanningTreeUtilities* Utils = ((ModuleSpanningTree*)(Module*)creator)->Utils;
26         /** Do we have enough parameters:
27          *      0    1    2    3    4    5        6        7     8        9       (n-1)
28          * UID uuid age nick host dhost ident ip.string signon +modes (modepara) :gecos
29          */
30         if (params.size() < 10)
31                 return CMD_INVALID;
32
33         time_t age_t = ConvToInt(params[1]);
34         time_t signon = ConvToInt(params[7]);
35         std::string empty;
36         std::string modestr(params[8]);
37
38         TreeServer* remoteserver = Utils->FindServer(serversrc->server);
39
40         if (!remoteserver)
41                 return CMD_INVALID;
42         /* Is this a valid UID, and not misrouted? */
43         if (params[0].length() != 9 || params[0].substr(0,3) != serversrc->uuid)
44                 return CMD_INVALID;
45         /* Check parameters for validity before introducing the client, discovered by dmb */
46         if (!age_t)
47                 return CMD_INVALID;
48         if (!signon)
49                 return CMD_INVALID;
50         if (modestr[0] != '+')
51                 return CMD_INVALID;
52         TreeSocket* sock = remoteserver->GetRoute()->GetSocket();
53
54         /* check for collision */
55         user_hash::iterator iter = ServerInstance->Users->clientlist->find(params[2]);
56
57         if (iter != ServerInstance->Users->clientlist->end())
58         {
59                 /*
60                  * Nick collision.
61                  */
62                 int collide = sock->DoCollision(iter->second, age_t, params[5], modestr, params[0]);
63                 ServerInstance->Logs->Log("m_spanningtree",DEBUG,"*** Collision on %s, collide=%d", params[2].c_str(), collide);
64
65                 if (collide != 1)
66                 {
67                         /* remote client lost, make sure we change their nick for the hash too
68                          *
69                          * This alters the line that will be sent to other servers, which
70                          * commands normally shouldn't do; hence the required const_cast.
71                          */
72                         const_cast<parameterlist&>(params)[2] = params[0];
73                 }
74         }
75
76         /* IMPORTANT NOTE: For remote users, we pass the UUID in the constructor. This automatically
77          * sets it up in the UUID hash for us.
78          */
79         User* _new = NULL;
80         try
81         {
82                 _new = new RemoteUser(params[0], remoteserver->GetName());
83         }
84         catch (...)
85         {
86                 ServerInstance->Logs->Log("m_spanningtree", DEFAULT, "Duplicate UUID %s in client introduction", params[0].c_str());
87                 return CMD_INVALID;
88         }
89         (*(ServerInstance->Users->clientlist))[params[2]] = _new;
90         _new->nick = params[2];
91         _new->host = params[3];
92         _new->dhost = params[4];
93         _new->ident = params[5];
94         _new->fullname = params[params.size() - 1];
95         _new->registered = REG_ALL;
96         _new->signon = signon;
97         _new->age = age_t;
98
99         /* we need to remove the + from the modestring, so we can do our stuff */
100         std::string::size_type pos_after_plus = modestr.find_first_not_of('+');
101         if (pos_after_plus != std::string::npos)
102         modestr = modestr.substr(pos_after_plus);
103
104         unsigned int paramptr = 9;
105         for (std::string::iterator v = modestr.begin(); v != modestr.end(); v++)
106         {
107                 /* For each mode thats set, increase counter */
108                 ModeHandler* mh = ServerInstance->Modes->FindMode(*v, MODETYPE_USER);
109
110                 if (mh)
111                 {
112                         if (mh->GetNumParams(true))
113                         {
114                                 if (paramptr >= params.size() - 1)
115                                         return CMD_INVALID;
116                                 std::string mp = params[paramptr++];
117                                 /* IMPORTANT NOTE:
118                                  * All modes are assumed to succeed here as they are being set by a remote server.
119                                  * Modes CANNOT FAIL here. If they DO fail, then the failure is ignored. This is important
120                                  * to note as all but one modules currently cannot ever fail in this situation, except for
121                                  * m_servprotect which specifically works this way to prevent the mode being set ANYWHERE
122                                  * but here, at client introduction. You may safely assume this behaviour is standard and
123                                  * will not change in future versions if you want to make use of this protective behaviour
124                                  * yourself.
125                                  */
126                                 mh->OnModeChange(_new, _new, NULL, mp, true);
127                         }
128                         else
129                                 mh->OnModeChange(_new, _new, NULL, empty, true);
130                         _new->SetMode(*v, true);
131                 }
132         }
133
134         /* now we've done with modes processing, put the + back for remote servers */
135         if (modestr[0] != '+')
136                 modestr = "+" + modestr;
137
138         _new->SetClientIP(params[6].c_str());
139
140         ServerInstance->Users->AddGlobalClone(_new);
141         remoteserver->SetUserCount(1); // increment by 1
142
143         bool dosend = true;
144
145         if ((Utils->quiet_bursts && remoteserver->bursting) || ServerInstance->SilentULine(_new->server))
146                 dosend = false;
147
148         if (dosend)
149                 ServerInstance->SNO->WriteToSnoMask('C',"Client connecting at %s: %s!%s@%s [%s] [%s]", _new->server.c_str(), _new->nick.c_str(), _new->ident.c_str(), _new->host.c_str(), _new->GetIPString(), _new->fullname.c_str());
150
151         FOREACH_MOD(I_OnPostConnect,OnPostConnect(_new));
152
153         return CMD_SUCCESS;
154 }
155
156 CmdResult CommandFHost::Handle(const parameterlist &params, User* src)
157 {
158         if (params.size() < 1)
159                 return CMD_INVALID;
160         if (IS_SERVER(src))
161                 return CMD_FAILURE;
162         src->ChangeDisplayedHost(params[0].c_str());
163         return CMD_SUCCESS;
164 }
165
166 CmdResult CommandFIdent::Handle(const parameterlist &params, User* src)
167 {
168         if (params.size() < 1)
169                 return CMD_INVALID;
170         if (IS_SERVER(src))
171                 return CMD_FAILURE;
172         src->ChangeIdent(params[0].c_str());
173         return CMD_SUCCESS;
174 }
175
176 CmdResult CommandFName::Handle(const parameterlist &params, User* src)
177 {
178         if (params.size() < 1)
179                 return CMD_INVALID;
180         if (IS_SERVER(src))
181                 return CMD_FAILURE;
182         src->ChangeName(params[0].c_str());
183         return CMD_SUCCESS;
184 }
185