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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2019 Dylan Frank <b00mx0r@aureus.pw>
* Copyright (C) 2017 Sheogorath <sheogorath@shivering-isles.com>
* Copyright (C) 2016 Adam <Adam@anope.org>
* Copyright (C) 2013, 2017-2019, 2021-2025 Sadie Powell <sadie@witchery.services>
* Copyright (C) 2012 Robby <robby@chatbelgie.be>
* Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
* Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
* Copyright (C) 2007-2008 Craig Edwards <brain@inspircd.org>
* Copyright (C) 2007 John Brooks <john@jbrooks.io>
* Copyright (C) 2006 Oliver Lupton <om@inspircd.org>
*
* 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 "modules/cloak.h"
#include "modules/hash.h"
enum CloakMode
{
/** 2.0 cloak of "half" of the hostname plus the full IP hash */
MODE_HALF_CLOAK,
/** 2.0 cloak of IP hash, split at 2 common CIDR range points */
MODE_OPAQUE
};
// lowercase-only encoding similar to base64, used for hash output
static constexpr char base32[] = "0123456789abcdefghijklmnopqrstuv";
// The minimum length of a cloak key.
static constexpr size_t minkeylen = 30;
struct CloakInfo final
: public Cloak::Method
{
// The method used for cloaking users.
CloakMode mode;
// The number of parts of the hostname shown when using half cloaking.
unsigned int domainparts;
// Whether to ignore the case of a hostname when cloaking it.
bool ignorecase;
// The secret used for generating cloaks.
std::string key;
// Dynamic reference to the md5 implementation.
dynamic_reference_nocheck<HashProvider> md5;
// The prefix for cloaks (e.g. MyNet-).
std::string prefix;
// The suffix for IP cloaks (e.g. .IP).
std::string suffix;
CloakInfo(const Cloak::Engine* engine, const std::shared_ptr<ConfigTag>& tag, CloakMode Mode, const std::string& Key)
: Cloak::Method(engine, tag)
, mode(Mode)
, domainparts(tag->getNum<unsigned int>("domainparts", 3, 1, 10))
, ignorecase(tag->getBool("ignorecase"))
, key(Key)
, md5(engine->creator, "hash/md5")
, prefix(tag->getString("prefix"))
, suffix(tag->getString("suffix", ".IP"))
{
}
/*
* 2.0-style cloaking function
* @param item The item to cloak (part of an IP or hostname)
* @param id A unique ID for this type of item (to make it unique if the item matches)
* @param len The length of the output. Maximum for MD5 is 16 characters.
*/
std::string SegmentCloak(const std::string& item, char id, size_t len)
{
std::string input;
input.reserve(key.length() + 3 + item.length());
input.append(1, id);
input.append(key);
input.append(1, '\0'); // null does not terminate a C++ string
if (ignorecase)
std::transform(item.begin(), item.end(), std::back_inserter(input), ::tolower);
else
input.append(item);
std::string rv = md5->GenerateRaw(input).substr(0, len);
for(size_t i = 0; i < len; i++)
{
// this discards 3 bits per byte. We have an
// overabundance of bits in the hash output, doesn't
// matter which ones we are discarding.
rv[i] = base32[rv[i] & 0x1F];
}
return rv;
}
std::string SegmentIP(const irc::sockets::sockaddrs& ip, bool full)
{
std::string bindata;
size_t hop1;
size_t hop2;
size_t hop3;
size_t len1;
size_t len2;
std::string rv;
if (ip.family() == AF_INET6)
{
bindata = std::string((const char*)ip.in6.sin6_addr.s6_addr, 16);
hop1 = 8;
hop2 = 6;
hop3 = 4;
len1 = 6;
len2 = 4;
// pfx s1.s2.s3. (xxxx.xxxx or s4) sfx
// 6 4 4 9/6
rv.reserve(prefix.length() + 26 + suffix.length());
}
else
{
bindata = std::string((const char*)&ip.in4.sin_addr, 4);
hop1 = 3;
hop2 = 0;
hop3 = 2;
len1 = len2 = 3;
// pfx s1.s2. (xxx.xxx or s3) sfx
rv.reserve(prefix.length() + 15 + suffix.length());
}
rv.append(prefix);
rv.append(SegmentCloak(bindata, 10, len1));
rv.append(1, '.');
bindata.erase(hop1);
rv.append(SegmentCloak(bindata, 11, len2));
if (hop2)
{
rv.append(1, '.');
bindata.erase(hop2);
rv.append(SegmentCloak(bindata, 12, len2));
}
if (full)
{
rv.append(1, '.');
bindata.erase(hop3);
rv.append(SegmentCloak(bindata, 13, 6));
rv.append(suffix);
}
else
{
if (ip.family() == AF_INET6)
{
rv.append(INSP_FORMAT(".{:02x}{:02x}.{:02x}{:02x}{}",
ip.in6.sin6_addr.s6_addr[2], ip.in6.sin6_addr.s6_addr[3],
ip.in6.sin6_addr.s6_addr[0], ip.in6.sin6_addr.s6_addr[1],
suffix));
}
else
{
const unsigned char* ip4 = (const unsigned char*)&ip.in4.sin_addr;
rv.append(INSP_FORMAT(".{}.{}{}", ip4[1], ip4[0], suffix));
}
}
return rv;
}
std::string GetCompatLinkData()
{
std::string data = "broken";
if (md5)
{
switch (mode)
{
case MODE_HALF_CLOAK:
// Use old cloaking verification to stay compatible with 2.0
// But verify domainparts and ignorecase when use 3.0-only features
if (domainparts == 3 && !ignorecase)
data = prefix + SegmentCloak("*", 3, 8) + suffix;
else
{
irc::sockets::sockaddrs sa;
data = GenCloak(sa, "", data + ConvToStr(domainparts)) + (ignorecase ? "-ci" : "");
}
break;
case MODE_OPAQUE:
data = prefix + SegmentCloak("*", 4, 8) + suffix + (ignorecase ? "-ci" : "");
}
}
return data;
}
void GetLinkData(Module::LinkData& data, std::string& compatdata) override
{
data["domain-parts"] = ConvToStr(domainparts);
data["ignore-case"] = ignorecase ? "yes" : "no";
data["prefix"] = prefix;
data["suffix"] = suffix;
// IMPORTANT: link data is sent over unauthenticated server links so we
// can't directly send the key here. Instead we use dummy cloaks that
// allow verification of or less the same thing.
const std::string broken = "missing-md5-module";
data["cloak-v4"] = md5 ? Generate("123.123.123.123") : broken;
data["cloak-v6"] = md5 ? Generate("dead:beef:cafe::") : broken;
data["cloak-host"] = md5 ? Generate("extremely.long.inspircd.cloak.example") : broken;
compatdata = GetCompatLinkData();
}
bool IsLinkSensitive() const override
{
// This method always wants to be at the front.
return true;
}
std::string GenCloak(const irc::sockets::sockaddrs& ip, const std::string& ipstr, const std::string& host)
{
std::string chost;
irc::sockets::sockaddrs hostip(false);
bool host_is_ip = hostip.from_ip_port(host, ip.port()) && hostip == ip;
switch (mode)
{
case MODE_HALF_CLOAK:
{
if (!host_is_ip)
chost = prefix + SegmentCloak(host, 1, 6) + "." + Cloak::VisiblePart(host, domainparts, '.');
if (chost.empty() || chost.length() > 50)
chost = SegmentIP(ip, false);
break;
}
case MODE_OPAQUE:
chost = SegmentIP(ip, true);
break;
}
return chost;
}
std::string Generate(LocalUser* user) override ATTR_NOT_NULL(2)
{
if (!md5 || !user->client_sa.is_ip() || !MatchesUser(user))
return {};
return GenCloak(user->client_sa, user->GetAddress(), user->GetRealHost());
}
std::string Generate(const std::string& hostip) override
{
if (!md5)
return {};
irc::sockets::sockaddrs sa;
const char* ipaddr = sa.from_ip(hostip) ? hostip.c_str() : "";
return GenCloak(sa, ipaddr, hostip);
}
};
class MD5Engine final
: public Cloak::Engine
{
private:
// Dynamic reference to the md5 implementation.
dynamic_reference_nocheck<HashProvider> md5;
// The method used for cloaking users.
CloakMode mode;
public:
MD5Engine(Module* Creator, const std::string& Name, CloakMode cm)
: Cloak::Engine(Creator, Name)
, md5(Creator, "hash/md5")
, mode(cm)
{
}
Cloak::MethodPtr Create(const std::shared_ptr<ConfigTag>& tag, bool primary) override
{
// Check any dependent modules are loaded.
if (!md5)
throw ModuleException(creator, "Unable to create a " + name.substr(6) + " cloak without the md5 module, at" + tag->source.str());
// Ensure that we have the <cloak:key> parameter.
const std::string key = tag->getString("key");
if (key.empty())
throw ModuleException(creator, "You have not defined a cloaking key. Define <cloak:key> as a {}+ character network-wide secret, at {}", minkeylen, tag->source.str());
// If we are the first cloak method then mandate a strong key.
if (primary)
{
if (key.length() < minkeylen)
throw ModuleException(creator, "Your cloaking key is not secure. It should be at least {} characters long, at {}", minkeylen, tag->source.str());
ServerInstance->Logs.Normal(MODNAME, "The {} cloak method is deprecated and will be removed in the next major version of InspIRCd. Consider migrating to cloak_sha256 instead. See " INSPIRCD_DOCS "modules/cloak_md5 for more information.",
name.c_str() + 6);
}
return std::make_shared<CloakInfo>(this, tag, mode, key);
}
};
class ModuleCloakMD5 final
: public Module
{
private:
MD5Engine halfcloak;
MD5Engine fullcloak;
public:
ModuleCloakMD5()
: Module(VF_VENDOR, "Adds the half and full cloaking methods for use with the cloak module.")
, halfcloak(this, "half", MODE_HALF_CLOAK)
, fullcloak(this, "full", MODE_OPAQUE)
{
}
};
MODULE_INIT(ModuleCloakMD5)
|