-
Notifications
You must be signed in to change notification settings - Fork 0
/
identd.cc
300 lines (243 loc) · 5.91 KB
/
identd.cc
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
#include <cassert>
#include <unordered_set>
#include <znc/main.h>
#include <znc/Modules.h>
#include <znc/znc.h>
#include <znc/IRCNetwork.h>
#include <znc/IRCSock.h>
#include <znc/User.h>
class Identd;
class IdentSock : public CSocket
{
public:
explicit IdentSock(CModule* module, bool isListener = false)
: CSocket(module)
, mIsListener(isListener)
{
}
private:
Identd* GetIdentd();
// CSocket
Csock* GetSockObj(CS_STRING const& /*host*/, uint16_t /*port*/) override { return new IdentSock(GetModule()); }
void Disconnected() override;
void Timeout() override { SockError(ETIMEDOUT, "socket timed out"); }
void ReadLine(CS_STRING const& line) override;
void SockError(int error, CS_STRING const& description) override;
bool ConnectionFrom(CS_STRING const& host, uint16_t port) override;
bool const mIsListener;
};
class Identd : public CModule
{
public:
MODCONSTRUCTOR(Identd) {}
~Identd()
{
RemoveListener();
}
bool AddSocket(CIRCSock const* sock)
{
if (mIsBlocking && !mSockets.empty())
{
PutModule("IRC connection already in progress");
return false;
}
if (Start())
{
auto network = sock->GetNetwork();
DEBUG("identd: adding IRC socket for " + network->GetUser()->GetUserName() + "/" + network->GetName());
mSockets.insert(sock);
if (mIsBlocking)
{
assert(mSockets.size() == 1);
CZNC::Get().PauseConnectQueue();
}
}
return true;
}
void RemoveSocket(CIRCSock const* sock)
{
if (mListener)
{
if (mSockets.erase(sock))
{
auto network = sock->GetNetwork();
DEBUG("identd: removed IRC socket for " + network->GetUser()->GetUserName() + "/" + network->GetName());
if (mIsBlocking)
{
assert(mSockets.empty());
CZNC::Get().ResumeConnectQueue();
}
}
Stop();
}
}
bool HasSockets() const
{
return !mSockets.empty();
}
CIRCSock const* FindSocket(CS_STRING const& ip, uint16_t port) const
{
if (mListener)
{
for (auto i : mSockets)
{
if (mIsBlocking || (i->IsConnected() && i->GetLocalPort() == port && i->GetLocalIP() == ip))
{
return i;
}
}
}
return nullptr;
}
void RemoveListener()
{
if (mIsBlocking && !mSockets.empty())
{
CZNC::Get().ResumeConnectQueue();
}
mSockets.clear();
Stop();
}
void OnStopped()
{
if (mListener)
{
DEBUG("identd: stopped listening on port " + CString(mPort));
// Socket will be deleted by manager
mListener = nullptr;
}
}
private:
bool Start()
{
if (mListener)
{
if (mListener->IsClosed())
{
mListener->Close(CSocket::CLT_DONT);
DEBUG("identd: resumed listening on port " + CString(mPort));
}
}
else
{
mListener = new IdentSock(this, true);
if (!GetManager()->ListenAll(mPort, "identd", false, SOMAXCONN, mListener))
{
// Socket already deleted by manager
mListener = nullptr;
PutModule("Failed to listen on port " + CString(mPort) + ", try reloading identd module");
return false;
}
// Hack to get the Disconnected() callback
mListener->SetIsConnected(true);
DEBUG("identd: started listening on port " + CString(mPort));
}
return true;
}
void Stop()
{
if (mListener && mSockets.empty())
{
DEBUG("identd: will stop listening on port " + CString(mPort));
mListener->Close();
}
}
bool OnLoad(CString const& args, CString& message) override
{
mPort = args.ToUShort();
mIsBlocking = !args.empty() && args[0] == '+';
if (!mPort)
{
mPort = 9113;
}
message = "will listen on port " + CString(mPort) + " (" + (mIsBlocking ? "" : "non-") + "blocking)";
return true;
}
EModRet OnIRCConnecting(CIRCSock* sock) override
{
return AddSocket(sock) ? CONTINUE : HALTCORE;
}
void OnIRCConnectionError(CIRCSock* sock) override
{
RemoveSocket(sock);
}
void OnIRCConnected() override
{
RemoveSocket(GetNetwork()->GetIRCSock());
}
void OnIRCDisconnected() override
{
RemoveSocket(GetNetwork()->GetIRCSock());
}
uint16_t mPort = 0;
bool mIsBlocking = false;
IdentSock* mListener = nullptr;
std::unordered_set<CIRCSock const*> mSockets;
};
Identd* IdentSock::GetIdentd()
{
assert(dynamic_cast<Identd*>(GetModule()));
return static_cast<Identd*>(GetModule());
}
void IdentSock::Disconnected()
{
if (mIsListener)
{
GetIdentd()->OnStopped();
}
}
void IdentSock::ReadLine(const CS_STRING& line)
{
if (mIsListener)
{
// Should not happen
assert(false);
return;
}
DEBUG("identd: got query: " + line.Trim_n());
VCString query;
line.Split(",", query, false, "", "", true, true);
if (query.size() != 2)
{
// Malformed query
Close();
return;
}
CString reply = query[0] + " , " + query[1] + " : ";
uint16_t port = query[0].ToUShort();
auto sock = GetIdentd()->FindSocket(GetLocalIP(), port);
if (sock && sock->GetNetwork() && sock->GetNetwork()->GetUser())
{
auto network = sock->GetNetwork();
DEBUG("identd: found IRC socket for " + network->GetUser()->GetUserName() + "/" + network->GetName() + " on " + GetLocalIP() + ":" + CString(port));
reply += "USERID : UNIX : " + network->GetUser()->GetIdent();
}
else
{
DEBUG("identd: IRC socket on " + GetLocalIP() + ":" + CString(port) + " not found");
reply += "ERROR : NO-USER";
}
Write(reply + "\r\n");
Close(CLT_AFTERWRITE);
}
void IdentSock::SockError(int error, CS_STRING const& description)
{
GetModule()->PutModule("Socket error " + CString(error) + ": " + description);
Close();
if (mIsListener)
{
GetIdentd()->RemoveListener();
GetIdentd()->OnStopped();
}
}
bool IdentSock::ConnectionFrom(CS_STRING const& host, uint16_t port)
{
DEBUG(CString("identd: ") + (GetIdentd()->HasSockets() ? "allow" : "deny") + "ing connection from " + host + ":" + CString(port));
return GetIdentd()->HasSockets() ? CSocket::ConnectionFrom(host, port) : false;
}
template<> void TModInfo<Identd>(CModInfo& info)
{
info.SetHasArgs(true);
info.SetArgsHelpText("Specify the listening port (default: 9113). Prepend with + for blocking connect.");
}
GLOBALMODULEDEFS(Identd, "identd")