1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2025 Sadie Powell <sadie@witchery.services>
*
* This file is part of InspIRCd. InspIRCd is free software: you can
* redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "inspircd.h"
#include "extension.h"
#include "modules/cloak.h"
#include "modules/hash.h"
#include "modules/ircv3.h"
class CustomCloakExtItem final
: public SimpleExtItem<Cloak::Info>
{
public:
Cloak::API cloakapi;
Cloak::Engine& customcloak;
CustomCloakExtItem(Module* mod, Cloak::Engine& cloak)
: SimpleExtItem<Cloak::Info>(mod, "custom-cloak", ExtensionType::USER, true)
, cloakapi(mod)
, customcloak(cloak)
{
}
void FromInternal(Extensible* container, const std::string& value) noexcept override
{
if (container->extype != this->extype)
return;
if (value.empty())
Unset(container, false);
else
{
auto cloak = Cloak::Info::FromString(Percent::Decode(value));
if (cloak.hostname.empty())
return; // Malformed.
Set(container, cloak, false);
auto *luser = IS_LOCAL(static_cast<User*>(container));
if (!luser || !cloakapi)
return;
auto update = false;
if (!cloak.username.empty() && luser->GetDisplayedUser() != cloak.username)
update = true;
else if (cloak.hostname != luser->GetDisplayedUser())
update = true;
if (update && cloakapi->IsActiveCloak(customcloak))
cloakapi->ResetCloaks(luser, true);
}
}
std::string ToInternal(const Extensible* container, void* item) const noexcept override
{
return item ? Percent::Encode(static_cast<Cloak::Info*>(item)->ToString()) : std::string();
}
};
class CloakAccount final
{
private:
const std::string host;
const std::string password;
const std::string passwordhash;
public:
const Cloak::Info cloak;
CloakAccount(const std::shared_ptr<ConfigTag>& tag, const Cloak::Info &c, const std::string& p)
: host(tag->getString("host", "*@*", 1))
, password(p)
, passwordhash(tag->getString("hash", "plaintext", 1))
, cloak(c)
{
if (insp::equalsci(passwordhash, "plaintext"))
{
ServerInstance->Logs.Warning(MODNAME, "<customcloak> tag at {} contains an plain text password, this is insecure!",
tag->source.str());
}
}
bool CheckHost(LocalUser* user) const
{
return InspIRCd::MatchMask(host, user->GetRealUserHost(), user->GetUserAddress());
}
bool CheckPassword(const std::string& pass) const
{
return Hash::CheckPassword(password, passwordhash, pass);
}
};
using CloakAccounts = std::unordered_map<std::string, CloakAccount>;
class CommandCustomCloak final
: public SplitCommand
{
private:
CustomCloakExtItem& cloakext;
UserModeReference cloakmode;
IRCv3::ReplyCapReference stdrplcap;
CmdResult FailedLogin(LocalUser* user, const std::string& account)
{
IRCv3::WriteReply(Reply::Type::FAIL, user, stdrplcap, this, "LOGIN_FAIL", account, FMT::format("Failed to log into the \x02{}\x02 custom cloak account.", account));
user->CommandFloodPenalty += 2500;
return CmdResult::FAILURE;
}
public:
CloakAccounts accounts;
CommandCustomCloak(Module* mod, CustomCloakExtItem& ext)
: SplitCommand(mod, "CUSTOMCLOAK", 2)
, cloakext(ext)
, cloakmode(mod, "cloak")
, stdrplcap(mod)
{
syntax = { "<username> <password>" };
}
CmdResult HandleLocal(LocalUser* user, const Params& parameters) override
{
// Check whether the account exists.
auto it = accounts.find(parameters[0]);
if (it == accounts.end())
{
ServerInstance->Logs.Debug(MODNAME, "{} ({}) [{}] failed to log into the \x02{}\x02 custom cloak account because no account with that name exists.",
user->nick, user->GetRealUserHost(), user->GetAddress(), parameters[0]);
return FailedLogin(user, parameters[0]);
}
// Check whether the host is correct.
auto &account = it->second;
if (!account.CheckHost(user))
{
ServerInstance->Logs.Normal(MODNAME, "{} ({}) [{}] failed to log into the \x02{}\x02 custom cloak account because they are connecting from the wrong user@host.",
user->nick, user->GetRealUserHost(), user->GetAddress(), parameters[0]);
return FailedLogin(user, it->first);
}
// Check whether the password is correct.
if (!account.CheckPassword(parameters[1]))
{
ServerInstance->Logs.Normal(MODNAME, "{} ({}) [{}] failed to log into the \x02{}\x02 custom cloak account because they specified the wrong password.",
user->nick, user->GetRealUserHost(), user->GetAddress(), parameters[0]);
return FailedLogin(user, it->first);
}
// If they have reached this point then the login succeeded,
IRCv3::WriteReply(Reply::Type::NOTE, user, stdrplcap, this, "LOGIN_SUCCESS", it->first, account.cloak.ToString(), FMT::format("You are now logged in as \x02{}\x02; updating your cloak to \x02{}\x02.",
it->first, account.cloak.ToString()));
cloakext.Set(user, account.cloak);
if (cloakext.cloakapi && cloakext.cloakapi->IsActiveCloak(cloakext.customcloak))
cloakext.cloakapi->ResetCloaks(user, true);
if (cloakmode && !user->IsModeSet(cloakmode))
{
Modes::ChangeList changelist;
changelist.push_add(*cloakmode);
ServerInstance->Modes.Process(ServerInstance->FakeClient, nullptr, user, changelist);
}
return CmdResult::SUCCESS;
}
};
class CustomMethod final
: public Cloak::Method
{
private:
// The cloak to set on users.
CustomCloakExtItem &customcloakext;
public:
static bool created;
CustomMethod(const Cloak::Engine* engine, const std::shared_ptr<ConfigTag>& tag, CustomCloakExtItem &ext) ATTR_NOT_NULL(2)
: Cloak::Method(engine, tag)
, customcloakext(ext)
{
created = true;
}
~CustomMethod()
{
created = false;
}
std::optional<Cloak::Info> Cloak(LocalUser* user) override ATTR_NOT_NULL(2)
{
if (!MatchesUser(user))
return {}; // We shouldn't cloak this user.
auto *cloak = customcloakext.Get(user);
if (!cloak)
return {}; // No custom cloak;
return *cloak;
}
std::optional<Cloak::Info> Cloak(const std::string& hostip) override
{
// We can't generate custom cloaks without a user.
return std::nullopt;
}
void GetLinkData(Module::LinkData& data) override
{
// We don't have any link data.
}
};
bool CustomMethod::created = false;
class CustomEngine final
: public Cloak::Engine
{
public:
CustomCloakExtItem customcloakext;
CustomEngine(Module* mod)
: Cloak::Engine(mod, "custom")
, customcloakext(mod, *this)
{
}
Cloak::MethodPtr Create(const std::shared_ptr<ConfigTag>& tag, bool primary) override
{
if (CustomMethod::created)
throw ModuleException(creator, "You can only have one custom cloak method, at " + tag->source.str());
return std::make_shared<CustomMethod>(this, tag, customcloakext);
}
};
class ModuleCloakCustom final
: public Module
{
private:
CommandCustomCloak cmdcustomcloak;
CustomEngine customcloak;
public:
ModuleCloakCustom()
: Module(VF_VENDOR, "Adds the custom cloaking method for use with the cloak module.")
, cmdcustomcloak(this, customcloak.customcloakext)
, customcloak(this)
{
}
void ReadConfig(ConfigStatus& status) override
{
CloakAccounts newaccounts;
for (const auto& [_, tag] : ServerInstance->Config->ConfTags("customcloak"))
{
const auto name = tag->getString("name");
if (name.empty())
throw ModuleException(this, "<customcloak:name> must not be empty, at " + tag->source.str());
const auto password = tag->getString("password");
if (password.empty())
throw ModuleException(this, "<customcloak:password> must not be empty, at " + tag->source.str());
Cloak::Info cloak(tag->getString("username"), tag->getString("hostname"));
if (cloak.hostname.empty())
throw ModuleException(this, "<customcloak:hostname> must not be empty, at " + tag->source.str());
CloakAccount account(tag, cloak, password);
if (!newaccounts.emplace(name, account).second)
throw ModuleException(this, "<customcloak:name> (" + name + ") used in multiple tags, at " + tag->source.str());
}
std::swap(newaccounts, cmdcustomcloak.accounts);
}
};
MODULE_INIT(ModuleCloakCustom)
|