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
|
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2023 Sadie Powell <sadie@witchery.services>
* Copyright (C) 2017 Adam <Adam@anope.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/>.
*/
#pragma once
namespace Cloak
{
class API;
class APIBase;
class Engine;
class Method;
struct Info;
/** Encapsulates a list of cloaks. */
typedef std::vector<Info> List;
/** A shared pointer to a cloak method. */
typedef std::shared_ptr<Method> MethodPtr;
/** Takes a hostname and retrieves the part which should be visible.
*
* This is usually the last \p hostparts segments but if not enough are
* present then all but the most specific segments are used. If the domain
* name consists of one label only then none are used.
*
* Here are some examples for how domain names will be shortened assuming
* \p domainparts is set to the default of 3.
*
* "this.is.an.example.com" => "an.example.com"
* "an.example.com" => "example.com"
* "example.com" => "com"
* "localhost" => ""
*
* "/var/run/inspircd/client.sock" => "run/inspircd/client.sock"
* "/run/inspircd/client.sock" => "inspircd/client.sock"
* "/inspircd/client.sock" => "client.sock"
* "/client.sock" => ""
*
* @param host The hostname to cloak.
* @param hostparts The number of host labels that should be visible.
* @param separator The character that separates hostname segments.
* @return The visible segment of the hostname.
*/
inline std::string VisiblePart(const std::string& host, size_t hostparts, char separator);
}
/** Defines the interface for the cloak API. */
class Cloak::APIBase
: public DataProvider
{
public:
APIBase(Module* parent)
: DataProvider(parent, "cloakapi")
{
}
/** Retrieves the cloak list for the specified user.
* @param user The user to retrieve cloaks for.
*/
virtual List* GetCloaks(LocalUser* user) = 0;
/** Determines whether any cloaks of the specified type exist.
* @param engine The engine to check the active status of.
*/
virtual bool IsActiveCloak(const Cloak::Engine& engine) = 0;
/** Reset the cloaks for the specified user.
* @param user The user to reset the cloaks for.
* @param resetdisplay Whether to reset the currently displayed cloak.
*/
virtual void ResetCloaks(LocalUser* user, bool resetdisplay) = 0;
};
/** Allows modules to access information regarding cloaks. */
class Cloak::API final
: public dynamic_reference<Cloak::APIBase>
{
public:
API(Module* parent)
: dynamic_reference<Cloak::APIBase>(parent, "cloakapi")
{
}
};
/** Base class for cloak engines. */
class Cloak::Engine
: public DataProvider
{
protected:
Engine(Module* Creator, const std::string& Name)
: DataProvider(Creator, "cloak/" + Name)
{
}
public:
/** Creates a new cloak method from the specified config.
* @param tag The config tag to configure the cloak method with.
* @param primary Whether the created cloak method is the primary method.
*/
virtual MethodPtr Create(const std::shared_ptr<ConfigTag>& tag, bool primary) = 0;
};
/** Base class for cloak methods. */
class Cloak::Method
{
private:
/** The name of the engine that created this method. */
std::string provname;
/** If non-empty the connect classes that a user must be in one of to be cloaked by this method. */
insp::flat_set<std::string> classes;
protected:
Method(const Engine* engine, const std::shared_ptr<ConfigTag>& tag) ATTR_NOT_NULL(2)
: provname(engine->name)
{
irc::commasepstream klassstream(tag->getString("class"));
for (std::string klass; klassstream.GetToken(klass); )
classes.insert(klass);
}
bool MatchesUser(LocalUser* user) const
{
if (!classes.empty() && classes.find(user->GetClass()->GetName()) == classes.end())
return false;
// All fields matched.
return true;
}
public:
virtual ~Method() = default;
/** Generates a cloak for the specified user.
* @param user The user to generate a cloak for.
*/
virtual std::optional<Info> Cloak(LocalUser* user) ATTR_NOT_NULL(2) = 0;
/** Generates a cloak for the specified hostname, IP address, or UNIX socket path.
* @param hostip The hostname, IP address, or UNIX socket path to generate a cloak for.
*/
virtual std::optional<Info> Cloak(const std::string& hostip) = 0;
/** Retrieves link compatibility data for this cloak method.
* @param data The location to store link compatibility data.
*/
virtual void GetLinkData(Module::LinkData& data) = 0;
/** Retrieves the name of this cloaking method. */
const char* GetName() const
{
return provname.c_str() + 6;
}
/** Determines whether when this cloak method is behind non-sensitive cloak methods
* if it should be treated as if it was the primary cloak method for the purposes of
* generating link data.
*/
virtual bool IsLinkSensitive() const
{
return false;
}
/** Determines whether this method is provided by the specified service provider.
* @param prov The service provider to check.
*/
bool IsProvidedBy(const ServiceProvider& prov) const
{
return prov.name == provname;
}
};
/** Encapsulates information about a cloak. */
struct Cloak::Info final
{
/** The hostname of the cloak. */
const std::string hostname;
/** The username of the cloak (can be empty). */
const std::string username;
/** Creates a new cloak with both a username and hostname.
* @param u The username of the cloak.
* @param h The hostname of the cloak.
*/
Info(const std::string& u, const std::string& h)
: hostname(h)
, username(u)
{
}
/** Creates a new cloak with just a hostname.
* @param h The hostname of the cloak.
*/
Info(const std::string& h)
: hostname(h)
{
}
/** Default comparator for cloaks. */
auto operator<=>(const Info&) const = default;
/** Converts a cloak from the username@hostname form to a \p Cloak::Info.
* @param str The cloak to convert.
*/
static Cloak::Info FromString(const std::string& cloak)
{
auto sep = cloak.find('@');
if (sep == std::string::npos)
return Info(cloak);
return Info(cloak.substr(0, sep), cloak.substr(sep + 1));
}
/** Converts a \p Cloak::Info to the username@hostname form. */
std::string ToString() const
{
std::string ret;
if (!username.empty())
ret.append(username).push_back('@');
if (!hostname.empty())
ret.append(hostname);
return ret;
}
};
inline std::string Cloak::VisiblePart(const std::string& host, size_t hostparts, char separator)
{
// The position at which we found the last separator.
std::string::const_reverse_iterator seppos;
// The number of separators we have seen so far.
size_t seenseps = 0;
for (std::string::const_reverse_iterator it = host.rbegin(); it != host.rend(); ++it)
{
if (*it != separator)
continue;
// We have found a separator!
seppos = it;
seenseps += 1;
// Do we have enough segments to stop?
if (seenseps >= hostparts)
break;
}
// We only returns a domain part if more than one label is
// present. See above for a full explanation.
if (!seenseps)
return "";
return std::string(seppos.base(), host.end());
}
|