]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/nickcollide.cpp
bef739ed011ee174bad2220a11f39ce19bce6fd5
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / nickcollide.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2013-2015 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
8  *   Copyright (C) 2009 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
28 #include "treesocket.h"
29 #include "treeserver.h"
30 #include "utils.h"
31 #include "commandbuilder.h"
32 #include "commands.h"
33
34 /*
35  * Yes, this function looks a little ugly.
36  * However, in some circumstances we may not have a User, so we need to do things this way.
37  * Returns true if remote or both lost, false otherwise.
38  * Sends SAVEs as appropriate and forces nick change of the user 'u' if our side loses or if both lose.
39  * Does not change the nick of the user that is trying to claim the nick of 'u', i.e. the "remote" user.
40  */
41 bool SpanningTreeUtilities::DoCollision(User* u, TreeServer* server, time_t remotets, const std::string& remoteident, const std::string& remoteip, const std::string& remoteuid, const char* collidecmd)
42 {
43         // At this point we're sure that a collision happened, increment the counter regardless of who wins
44         ServerInstance->stats.Collisions++;
45
46         /*
47          * Under old protocol rules, we would have had to kill both clients.
48          * Really, this sucks.
49          * These days, we have UID. And, so what we do is, force nick change client(s)
50          * involved according to timestamp rules.
51          *
52          * RULES:
53          *  user@ip equal:
54          *   Force nick change on OLDER timestamped client
55          *  user@ip differ:
56          *   Force nick change on NEWER timestamped client
57          *  TS EQUAL:
58          *   FNC both.
59          *
60          * This stops abusive use of collisions, simplifies problems with loops, and so on.
61          *   -- w00t
62          */
63         bool bChangeLocal = true;
64         bool bChangeRemote = true;
65
66         // If the timestamps are not equal only one of the users has to change nick,
67         // otherwise both have to change
68         const time_t localts = u->age;
69         if (remotets != localts)
70         {
71                 /* first, let's see if ident@host matches. */
72                 const std::string& localident = u->ident;
73                 const std::string& localip = u->GetIPString();
74                 bool SamePerson = (localident == remoteident)
75                                 && (localip == remoteip);
76
77                 /*
78                  * if ident@ip is equal, and theirs is newer, or
79                  * ident@ip differ, and ours is newer
80                  */
81                 if ((SamePerson && remotets < localts) || (!SamePerson && remotets > localts))
82                 {
83                         // Only remote needs to change
84                         bChangeLocal = false;
85                 }
86                 else
87                 {
88                         // Only ours needs to change
89                         bChangeRemote = false;
90                 }
91         }
92
93         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Nick collision on \"%s\" caused by %s: %s/%lu/%s@%s %d <-> %s/%lu/%s@%s %d", u->nick.c_str(), collidecmd,
94                 u->uuid.c_str(), (unsigned long)localts, u->ident.c_str(), u->GetIPString().c_str(), bChangeLocal,
95                 remoteuid.c_str(), (unsigned long)remotets, remoteident.c_str(), remoteip.c_str(), bChangeRemote);
96
97         /*
98          * Send SAVE and accept the losing client with its UID (as we know the SAVE will
99          * not fail under any circumstances -- UIDs are netwide exclusive).
100          *
101          * This means that each side of a collide will generate one extra NICK back to where
102          * they have just linked (and where it got the SAVE from), however, it will
103          * be dropped harmlessly as it will come in as :928AAAB NICK 928AAAB, and we already
104          * have 928AAAB's nick set to that.
105          *   -- w00t
106          */
107
108         if (bChangeLocal)
109         {
110                 /*
111                  * Local-side nick needs to change. Just in case we are hub, and
112                  * this "local" nick is actually behind us, send a SAVE out.
113                  */
114                 CmdBuilder params("SAVE");
115                 params.push(u->uuid);
116                 params.push(ConvToStr(u->age));
117                 params.Broadcast();
118
119                 u->ChangeNick(u->uuid, CommandSave::SavedTimestamp);
120         }
121         if (bChangeRemote)
122         {
123                 /*
124                  * Remote side needs to change. If this happens, we modify the UID or NICK and
125                  * send back a SAVE to the source.
126                  */
127                 CmdBuilder("SAVE").push(remoteuid).push_int(remotets).Unicast(server->ServerUser);
128         }
129
130         return bChangeRemote;
131 }