]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/uid.cpp
e5061489f014a046a8eb6b4d46d02e0912aa3f44
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / uid.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2017-2019 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2013 Adam <Adam@anope.org>
6  *   Copyright (C) 2012-2016 Attila Molnar <attilamolnar@hush.com>
7  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
8  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
9  *   Copyright (C) 2008, 2010 Craig Edwards <brain@inspircd.org>
10  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #include "inspircd.h"
27 #include "commands.h"
28
29 #include "utils.h"
30 #include "treeserver.h"
31 #include "remoteuser.h"
32
33 CmdResult CommandUID::HandleServer(TreeServer* remoteserver, CommandBase::Params& params)
34 {
35         /**
36          *      0    1    2    3    4    5        6        7     8        9       (n-1)
37          * UID uuid age nick host dhost ident ip.string signon +modes (modepara) :real
38          */
39         time_t age_t = ServerCommand::ExtractTS(params[1]);
40         time_t signon = ServerCommand::ExtractTS(params[7]);
41         std::string empty;
42         const std::string& modestr = params[8];
43
44         // Check if the length of the uuid is correct and confirm the sid portion of the uuid matches the sid of the server introducing the user
45         if (params[0].length() != UIDGenerator::UUID_LENGTH || params[0].compare(0, 3, remoteserver->GetId()))
46                 throw ProtocolException("Bogus UUID");
47         // Sanity check on mode string: must begin with '+'
48         if (modestr[0] != '+')
49                 throw ProtocolException("Invalid mode string");
50
51         // See if there is a nick collision
52         User* collideswith = ServerInstance->FindNickOnly(params[2]);
53         if ((collideswith) && (collideswith->registered != REG_ALL))
54         {
55                 // User that the incoming user is colliding with is not fully registered, we force nick change the
56                 // unregistered user to their uuid and tell them what happened
57                 LocalUser* const localuser = static_cast<LocalUser*>(collideswith);
58                 localuser->OverruleNick();
59         }
60         else if (collideswith)
61         {
62                 // The user on this side is registered, handle the collision
63                 bool they_change = Utils->DoCollision(collideswith, remoteserver, age_t, params[5], params[6], params[0], "UID");
64                 if (they_change)
65                 {
66                         // The client being introduced needs to change nick to uuid, change the nick in the message before
67                         // processing/forwarding it. Also change the nick TS to CommandSave::SavedTimestamp.
68                         age_t = CommandSave::SavedTimestamp;
69                         params[1] = ConvToStr(CommandSave::SavedTimestamp);
70                         params[2] = params[0];
71                 }
72         }
73
74         /* For remote users, we pass the UUID they sent to the constructor.
75          * If the UUID already exists User::User() throws an exception which causes this connection to be closed.
76          */
77         RemoteUser* _new = new SpanningTree::RemoteUser(params[0], remoteserver);
78         ServerInstance->Users->clientlist[params[2]] = _new;
79         _new->nick = params[2];
80         _new->ChangeRealHost(params[3], false);
81         _new->ChangeDisplayedHost(params[4]);
82         _new->ident = params[5];
83         _new->ChangeRealName(params.back());
84         _new->registered = REG_ALL;
85         _new->signon = signon;
86         _new->age = age_t;
87
88         unsigned int paramptr = 9;
89
90         for (std::string::const_iterator v = modestr.begin(); v != modestr.end(); ++v)
91         {
92                 // Accept more '+' chars, for now
93                 if (*v == '+')
94                         continue;
95
96                 /* For each mode thats set, find the mode handler and set it on the new user */
97                 ModeHandler* mh = ServerInstance->Modes->FindMode(*v, MODETYPE_USER);
98                 if (!mh)
99                         throw ProtocolException("Unrecognised mode '" + std::string(1, *v) + "'");
100
101                 if (mh->NeedsParam(true))
102                 {
103                         if (paramptr >= params.size() - 1)
104                                 throw ProtocolException("Out of parameters while processing modes");
105                         std::string mp = params[paramptr++];
106                         /* IMPORTANT NOTE:
107                          * All modes are assumed to succeed here as they are being set by a remote server.
108                          * Modes CANNOT FAIL here. If they DO fail, then the failure is ignored. This is important
109                          * to note as all but one modules currently cannot ever fail in this situation, except for
110                          * m_servprotect which specifically works this way to prevent the mode being set ANYWHERE
111                          * but here, at client introduction. You may safely assume this behaviour is standard and
112                          * will not change in future versions if you want to make use of this protective behaviour
113                          * yourself.
114                          */
115                         mh->OnModeChange(_new, _new, NULL, mp, true);
116                 }
117                 else
118                         mh->OnModeChange(_new, _new, NULL, empty, true);
119                 _new->SetMode(mh, true);
120         }
121
122         _new->SetClientIP(params[6]);
123
124         ServerInstance->Users->AddClone(_new);
125         remoteserver->UserCount++;
126
127         bool dosend = true;
128
129         if ((Utils->quiet_bursts && remoteserver->IsBehindBursting()) || _new->server->IsSilentULine())
130                 dosend = false;
131
132         if (dosend)
133                 ServerInstance->SNO->WriteToSnoMask('C',"Client connecting at %s: %s (%s) [%s]", remoteserver->GetName().c_str(), _new->GetFullRealHost().c_str(), _new->GetIPString().c_str(), _new->GetRealName().c_str());
134
135         FOREACH_MOD(OnPostConnect, (_new));
136
137         return CMD_SUCCESS;
138 }
139
140 CmdResult CommandFHost::HandleRemote(RemoteUser* src, Params& params)
141 {
142         src->ChangeDisplayedHost(params[0]);
143         return CMD_SUCCESS;
144 }
145
146 CmdResult CommandFIdent::HandleRemote(RemoteUser* src, Params& params)
147 {
148         src->ChangeIdent(params[0]);
149         return CMD_SUCCESS;
150 }
151
152 CmdResult CommandFName::HandleRemote(RemoteUser* src, Params& params)
153 {
154         src->ChangeRealName(params[0]);
155         return CMD_SUCCESS;
156 }
157
158 CommandUID::Builder::Builder(User* user)
159         : CmdBuilder(TreeServer::Get(user), "UID")
160 {
161         push(user->uuid);
162         push_int(user->age);
163         push(user->nick);
164         push(user->GetRealHost());
165         push(user->GetDisplayedHost());
166         push(user->ident);
167         push(user->GetIPString());
168         push_int(user->signon);
169         push(user->GetModeLetters(true));
170         push_last(user->GetRealName());
171 }