/
identserv.cpp
418 lines (331 loc) · 10.2 KB
/
identserv.cpp
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
* Copyright (C) 2009-2013 Ingmar Runge <ingmar@irsoft.de>
* See the AUTHORS file for details.
*
* An ident server module for Windows ZNC.
* http://code.google.com/p/znc-msvc/
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/
#include "znc/znc.h"
#include "znc/ZNCString.h"
#include "znc/User.h"
#include "znc/IRCNetwork.h"
#include "znc/IRCSock.h"
#include "znc/Modules.h"
#include <map>
#include <set>
/************************************************************************/
/* CLASS DECLARATIONS */
/************************************************************************/
class CIdentServer;
class CIdentServerMod : public CModule
{
protected:
unsigned short m_serverPort;
CIdentServer *m_identServer;
bool m_listenFailed;
CString m_sLastReply;
CString m_sLastRequest;
public:
MODCONSTRUCTOR(CIdentServerMod)
{
m_serverPort = 11300;
m_identServer = NULL;
m_listenFailed = false;
}
virtual ~CIdentServerMod();
EModRet OnIRCConnecting(CIRCSock *pIRCSock) override;
void OnIRCConnected() override;
void OnIRCDisconnected() override;
void OnClientLogin() override;
EModRet OnDeleteUser(CUser& User) override;
EModRet OnDeleteNetwork(CIRCNetwork& Network) override;
void OnModCommand(const CString& sLine) override;
void NoLongerNeedsIdentServer();
CIdentServer *GetIdentServer() { return m_identServer; }
void SetLastReply(const CString& s) { m_sLastReply = s; };
void SetLastRequest(const CString& s) { m_sLastRequest = s; };
};
class CIdentAcceptedSocket : public CSocket
{
public:
CIdentAcceptedSocket(CModule *pMod);
virtual ~CIdentAcceptedSocket();
void ReadLine(const CS_STRING & sLine) override;
};
/**
* Ident server implementation.
* RFC 1413: http://www.faqs.org/rfcs/rfc1413.html
* Not thread safe!
**/
class CIdentServer : public CSocket
{
protected:
std::set<CIRCNetwork*> m_activeUsers;
CModule *m_pModule;
unsigned short m_uPort;
static bool AreIPStringsEqual(const CString& sIP1, const CString& sIP2);
public:
CIdentServer(CModule *pMod, unsigned short uPort);
virtual ~CIdentServer();
bool IncreaseUseCount(CIRCNetwork* pUser);
bool DecreaseUseCount(CIRCNetwork* pUser);
bool InUse() { return !m_activeUsers.empty(); }
bool StartListening();
Csock *GetSockObj(const CS_STRING & sHostname, u_short uPort) override;
bool ConnectionFrom(const CS_STRING & sHostname, u_short uPort) override;
CString GetResponse(const CString& sLine, const CString& sSocketIP, const CString& sRemoteIP);
const std::set<CIRCNetwork*>& GetActiveUsers() { return m_activeUsers; };
};
/************************************************************************/
/* CIdentServer method implementation section */
/************************************************************************/
CIdentServer::CIdentServer(CModule *pMod, unsigned short uPort) : CSocket(pMod)
{
m_pModule = pMod;
m_uPort = uPort;
}
bool CIdentServer::IncreaseUseCount(CIRCNetwork *pUser)
{
if(m_activeUsers.find(pUser) != m_activeUsers.end())
{
return false;
}
m_activeUsers.insert(pUser);
return true;
}
bool CIdentServer::DecreaseUseCount(CIRCNetwork *pUser)
{
return (m_activeUsers.erase(pUser) != 0);
}
CString CIdentServer::GetResponse(const CString& sLine, const CString& sSocketIP, const CString& sRemoteIP)
{
unsigned short uLocalPort = 0; // local port that ZNC connected to IRC FROM
unsigned short uRemotePort = 0; // remote server port that ZNC connected TO, e.g. 6667
CString sResponseType = "ERROR";
CString sAddInfo = "INVALID-PORT";
DEBUG("IDENT request: " << sLine << " from " << sRemoteIP << " on " << sSocketIP);
if(sscanf(sLine.c_str(), "%hu , %hu", &uLocalPort, &uRemotePort) == 2)
{
sAddInfo = "NO-USER";
for(auto itu = CZNC::Get().GetUserMap().begin();
itu != CZNC::Get().GetUserMap().end(); ++itu)
{
CUser* pUser = itu->second;
bool bFound = false;
for(CIRCNetwork* pNetwork : pUser->GetNetworks())
{
CIRCSock *pSock = pNetwork->GetIRCSock();
if(!pSock)
continue;
DEBUG("Checking user (" << pSock->GetLocalPort() << ", " << pSock->GetRemotePort() << ", " << pSock->GetLocalIP() << ")");
if(pSock->GetLocalPort() == uLocalPort &&
pSock->GetRemotePort() == uRemotePort &&
AreIPStringsEqual(pSock->GetLocalIP(), sSocketIP))
{
sResponseType = "USERID";
sAddInfo = "UNIX : " + pUser->GetIdent();
// exact match found, leave the loop:
bFound = true;
break;
}
DEBUG("Checking user fallback (" << pSock->GetRemoteIP() << ", " << pSock->GetRemotePort() << ", " << pSock->GetLocalIP() << ")");
if(pSock->GetRemoteIP() == sRemoteIP &&
pSock->GetRemotePort() == uRemotePort &&
AreIPStringsEqual(pSock->GetLocalIP(), sSocketIP))
{
sResponseType = "USERID";
sAddInfo = "UNIX : " + pUser->GetIdent();
// keep looping, we may find something better
}
}
if(bFound)
break;
}
}
CString sReply = CString(uLocalPort) + ", " + CString(uRemotePort) + " : " + sResponseType + " : " + sAddInfo;
DEBUG("IDENT response: " << sReply);
CIdentServerMod *pMod = reinterpret_cast<CIdentServerMod*>(m_pModule);
if(pMod)
{
pMod->SetLastRequest(sLine.Replace_n("\r", "").Replace_n("\n", " ") + "from " + sRemoteIP + " on " + sSocketIP);
pMod->SetLastReply(sReply);
}
return sReply;
}
bool CIdentServer::StartListening()
{
return GetModule()->GetManager()->ListenAll(m_uPort, "IDENT_SERVER", false, SOMAXCONN, this);
}
Csock *CIdentServer::GetSockObj(const CS_STRING & sHostname, u_short uPort)
{
return new CIdentAcceptedSocket(m_pModule);
}
bool CIdentServer::ConnectionFrom(const CS_STRING & sHostname, u_short uPort)
{
DEBUG("IDENT connection from " << sHostname << ":" << uPort << " (on " << GetLocalIP() << ":" << GetLocalPort() << ")");
return (!m_activeUsers.empty());
}
bool CIdentServer::AreIPStringsEqual(const CString& sIP1, const CString& sIP2)
{
return sIP1.TrimPrefix_n("::ffff:").Equals(sIP2.TrimPrefix_n("::ffff:"));
}
CIdentServer::~CIdentServer()
{
}
/************************************************************************/
/* CIdentAcceptedSocket method implementation section */
/************************************************************************/
CIdentAcceptedSocket::CIdentAcceptedSocket(CModule *pMod) : CSocket(pMod)
{
EnableReadLine();
}
void CIdentAcceptedSocket::ReadLine(const CS_STRING & sLine)
{
CIdentServerMod *pMod = reinterpret_cast<CIdentServerMod*>(m_pModule);
const CString sReply = pMod->GetIdentServer()->GetResponse(sLine, GetLocalIP(), GetRemoteIP());
Write(sReply + "\r\n");
Close(CLT_AFTERWRITE);
}
CIdentAcceptedSocket::~CIdentAcceptedSocket()
{
}
/************************************************************************/
/* CIdentServerMod method implementation section */
/************************************************************************/
CIdentServerMod::EModRet CIdentServerMod::OnIRCConnecting(CIRCSock *pIRCSock)
{
assert(m_pNetwork != NULL);
DEBUG("CIdentServerMod::OnIRCConnecting");
if(!m_identServer)
{
DEBUG("Starting up IDENT listener.");
m_identServer = new CIdentServer(this, m_serverPort);
if(!m_identServer->StartListening())
{
DEBUG("WARNING: Opening the listening socket failed!");
m_listenFailed = true;
m_identServer = NULL; /* Csock deleted the instance. (gross) */
return CONTINUE;
}
else
{
m_listenFailed = false;
}
}
m_identServer->IncreaseUseCount(m_pNetwork);
return CONTINUE;
}
void CIdentServerMod::NoLongerNeedsIdentServer()
{
assert(m_pNetwork != NULL);
if(m_identServer)
{
m_identServer->DecreaseUseCount(m_pNetwork);
if(!m_identServer->InUse())
{
DEBUG("Closing down IDENT listener.");
m_identServer->Close();
m_identServer = NULL;
}
}
}
void CIdentServerMod::OnIRCConnected()
{
if((!m_pClient) && (m_listenFailed))
{
PutModule("*** WARNING: Opening the listening socket failed!");
PutModule("*** IDENT listener is NOT running.");
}
NoLongerNeedsIdentServer();
}
void CIdentServerMod::OnIRCDisconnected()
{
NoLongerNeedsIdentServer();
}
CIdentServerMod::EModRet CIdentServerMod::OnDeleteUser(CUser& User)
{
// NoLongerNeedsIdentServer needs m_pNetwork, so we have to provide it:
CIRCNetwork* pBackup = m_pNetwork;
for(CIRCNetwork* pNetwork : User.GetNetworks())
{
m_pNetwork = pNetwork;
NoLongerNeedsIdentServer();
}
m_pNetwork = pBackup;
return CONTINUE;
}
CIdentServerMod::EModRet CIdentServerMod::OnDeleteNetwork(CIRCNetwork& Network)
{
CIRCNetwork* pBackup = m_pNetwork;
m_pNetwork = &Network; // meh
NoLongerNeedsIdentServer();
m_pNetwork = pBackup;
return CONTINUE;
}
void CIdentServerMod::OnClientLogin()
{
if(m_listenFailed)
{
PutModule("*** WARNING: Opening the listening socket failed!");
PutModule("*** IDENT listener is NOT running.");
}
}
void CIdentServerMod::OnModCommand(const CString& sLine)
{
CString sCommand = sLine.Token(0);
if(sCommand.Equals("HELP"))
{
CTable Table;
Table.AddColumn("Command");
Table.AddColumn("Description");
Table.AddRow();
Table.SetCell("Command", "Status");
Table.SetCell("Description", "Displays status information about IdentServer");
PutModule(Table);
return;
}
else if(sCommand.Equals("STATUS"))
{
if(m_identServer)
{
PutModule("IdentServer is listening on: " + m_identServer->GetLocalIP() + ":" + CString(m_serverPort));
if(m_pUser->IsAdmin())
{
PutModule("List of active users/networks:");
for(CIRCNetwork* pNetwork : m_identServer->GetActiveUsers())
{
PutModule("* " + pNetwork->GetUser()->GetCleanUserName() + "/" + pNetwork->GetName());
}
}
}
else
{
if(m_listenFailed)
{
PutModule("WARNING: Opening the listening socket failed!");
}
PutModule("IdentServer isn't listening.");
}
if(m_pUser->IsAdmin())
{
PutModule("Last IDENT request: " + m_sLastRequest);
PutModule("Last IDENT reply: " + m_sLastReply);
}
}
else
{
PutModule("Unknown command [" + sCommand + "] try 'Help'");
}
}
CIdentServerMod::~CIdentServerMod()
{
if(m_identServer)
{
m_identServer->Close();
}
}
GLOBALMODULEDEFS(CIdentServerMod, "Provides a simple IDENT server implementation.")