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 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
|
/*
* Copyright (C) 2004-2010 See the AUTHORS file for details.
*
* 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 "Chan.h"
#include "User.h"
class CAutoOpMod;
#define AUTOOP_CHALLENGE_LENGTH 32
class CAutoOpTimer : public CTimer {
public:
CAutoOpTimer(CAutoOpMod* pModule)
: CTimer((CModule*) pModule, 20, 0, "AutoOpChecker", "Check channels for auto op candidates") {
m_pParent = pModule;
}
virtual ~CAutoOpTimer() {}
private:
protected:
virtual void RunJob();
CAutoOpMod* m_pParent;
};
class CAutoOpUser {
public:
CAutoOpUser() {}
CAutoOpUser(const CString& sLine) {
FromString(sLine);
}
CAutoOpUser(const CString& sUsername, const CString& sUserKey, const CString& sHostmask, const CString& sChannels) :
m_sUsername(sUsername),
m_sUserKey(sUserKey),
m_sHostmask(sHostmask) {
AddChans(sChannels);
}
virtual ~CAutoOpUser() {}
const CString& GetUsername() const { return m_sUsername; }
const CString& GetUserKey() const { return m_sUserKey; }
const CString& GetHostmask() const { return m_sHostmask; }
bool ChannelMatches(const CString& sChan) const {
for (set<CString>::const_iterator it = m_ssChans.begin(); it != m_ssChans.end(); ++it) {
if (sChan.AsLower().WildCmp(*it)) {
return true;
}
}
return false;
}
bool HostMatches(const CString& sHostmask) {
return sHostmask.WildCmp(m_sHostmask);
}
CString GetChannels() const {
CString sRet;
for (set<CString>::const_iterator it = m_ssChans.begin(); it != m_ssChans.end(); ++it) {
if (!sRet.empty()) {
sRet += " ";
}
sRet += *it;
}
return sRet;
}
void DelChans(const CString& sChans) {
VCString vsChans;
sChans.Split(" ", vsChans);
for (unsigned int a = 0; a < vsChans.size(); a++) {
m_ssChans.erase(vsChans[a].AsLower());
}
}
void AddChans(const CString& sChans) {
VCString vsChans;
sChans.Split(" ", vsChans);
for (unsigned int a = 0; a < vsChans.size(); a++) {
m_ssChans.insert(vsChans[a].AsLower());
}
}
CString ToString() const {
CString sChans;
for (set<CString>::const_iterator it = m_ssChans.begin(); it != m_ssChans.end(); ++it) {
if (!sChans.empty()) {
sChans += " ";
}
sChans += *it;
}
return m_sUsername + "\t" + m_sHostmask + "\t" + m_sUserKey + "\t" + sChans;
}
bool FromString(const CString& sLine) {
m_sUsername = sLine.Token(0, false, "\t");
m_sHostmask = sLine.Token(1, false, "\t");
m_sUserKey = sLine.Token(2, false, "\t");
sLine.Token(3, false, "\t").Split(" ", m_ssChans);
return !m_sUserKey.empty();
}
private:
protected:
CString m_sUsername;
CString m_sUserKey;
CString m_sHostmask;
set<CString> m_ssChans;
};
class CAutoOpMod : public CModule {
public:
MODCONSTRUCTOR(CAutoOpMod) {}
virtual bool OnLoad(const CString& sArgs, CString& sMessage) {
AddTimer(new CAutoOpTimer(this));
// Load the users
for (MCString::iterator it = BeginNV(); it != EndNV(); ++it) {
const CString& sLine = it->second;
CAutoOpUser* pUser = new CAutoOpUser;
if (!pUser->FromString(sLine) || FindUser(pUser->GetUsername().AsLower())) {
delete pUser;
} else {
m_msUsers[pUser->GetUsername().AsLower()] = pUser;
}
}
return true;
}
virtual ~CAutoOpMod() {
for (map<CString, CAutoOpUser*>::iterator it = m_msUsers.begin(); it != m_msUsers.end(); ++it) {
delete it->second;
}
m_msUsers.clear();
}
virtual void OnJoin(const CNick& Nick, CChan& Channel) {
// If we have ops in this chan
if (Channel.HasPerm(CChan::Op)) {
for (map<CString, CAutoOpUser*>::iterator it = m_msUsers.begin(); it != m_msUsers.end(); ++it) {
// and the nick who joined is a valid user
if (it->second->HostMatches(Nick.GetHostMask()) && it->second->ChannelMatches(Channel.GetName())) {
if (it->second->GetUserKey().Equals("__NOKEY__")) {
PutIRC("MODE " + Channel.GetName() + " +o " + Nick.GetNick());
} else {
// then insert this nick into the queue, the timer does the rest
CString sNick = Nick.GetNick().AsLower();
if (m_msQueue.find(sNick) == m_msQueue.end()) {
m_msQueue[sNick] = "";
}
}
break;
}
}
}
}
virtual void OnQuit(const CNick& Nick, const CString& sMessage, const vector<CChan*>& vChans) {
MCString::iterator it = m_msQueue.find(Nick.GetNick().AsLower());
if (it != m_msQueue.end()) {
m_msQueue.erase(it);
}
}
virtual void OnNick(const CNick& OldNick, const CString& sNewNick, const vector<CChan*>& vChans) {
// Update the queue with nick changes
MCString::iterator it = m_msQueue.find(OldNick.GetNick().AsLower());
if (it != m_msQueue.end()) {
m_msQueue[sNewNick.AsLower()] = it->second;
m_msQueue.erase(it);
}
}
virtual EModRet OnPrivNotice(CNick& Nick, CString& sMessage) {
if (!sMessage.Token(0).Equals("!ZNCAO")) {
return CONTINUE;
}
CString sCommand = sMessage.Token(1);
if (sCommand.Equals("CHALLENGE")) {
ChallengeRespond(Nick, sMessage.Token(2));
} else if (sCommand.Equals("RESPONSE")) {
VerifyResponse(Nick, sMessage.Token(2));
}
return HALTCORE;
}
virtual void OnModCommand(const CString& sLine) {
CString sCommand = sLine.Token(0).AsUpper();
if (sCommand.Equals("HELP")) {
PutModule("Commands are: ListUsers, AddChans, DelChans, AddUser, DelUser");
} else if (sCommand.Equals("TIMERS")) {
ListTimers();
} else if (sCommand.Equals("ADDUSER") || sCommand.Equals("DELUSER")) {
CString sUser = sLine.Token(1);
CString sHost = sLine.Token(2);
CString sKey = sLine.Token(3);
if (sCommand.Equals("ADDUSER")) {
if (sHost.empty()) {
PutModule("Usage: " + sCommand + " <user> <hostmask> <key> [channels]");
} else {
CAutoOpUser* pUser = AddUser(sUser, sKey, sHost, sLine.Token(4, true));
if (pUser) {
SetNV(sUser, pUser->ToString());
}
}
} else {
DelUser(sUser);
DelNV(sUser);
}
} else if (sCommand.Equals("LISTUSERS")) {
if (m_msUsers.empty()) {
PutModule("There are no users defined");
return;
}
CTable Table;
Table.AddColumn("User");
Table.AddColumn("Hostmask");
Table.AddColumn("Key");
Table.AddColumn("Channels");
for (map<CString, CAutoOpUser*>::iterator it = m_msUsers.begin(); it != m_msUsers.end(); ++it) {
Table.AddRow();
Table.SetCell("User", it->second->GetUsername());
Table.SetCell("Hostmask", it->second->GetHostmask());
Table.SetCell("Key", it->second->GetUserKey());
Table.SetCell("Channels", it->second->GetChannels());
}
PutModule(Table);
} else if (sCommand.Equals("ADDCHANS") || sCommand.Equals("DELCHANS")) {
CString sUser = sLine.Token(1);
CString sChans = sLine.Token(2, true);
if (sChans.empty()) {
PutModule("Usage: " + sCommand + " <user> <channel> [channel] ...");
return;
}
CAutoOpUser* pUser = FindUser(sUser);
if (!pUser) {
PutModule("No such user");
return;
}
if (sCommand.Equals("ADDCHANS")) {
pUser->AddChans(sChans);
PutModule("Channel(s) added to user [" + pUser->GetUsername() + "]");
} else {
pUser->DelChans(sChans);
PutModule("Channel(s) Removed from user [" + pUser->GetUsername() + "]");
}
SetNV(pUser->GetUsername(), pUser->ToString());
} else {
PutModule("Unknown command, try HELP");
}
}
CAutoOpUser* FindUser(const CString& sUser) {
map<CString, CAutoOpUser*>::iterator it = m_msUsers.find(sUser.AsLower());
return (it != m_msUsers.end()) ? it->second : NULL;
}
CAutoOpUser* FindUserByHost(const CString& sHostmask, const CString& sChannel = "") {
for (map<CString, CAutoOpUser*>::iterator it = m_msUsers.begin(); it != m_msUsers.end(); ++it) {
CAutoOpUser* pUser = it->second;
if (pUser->HostMatches(sHostmask) && (sChannel.empty() || pUser->ChannelMatches(sChannel))) {
return pUser;
}
}
return NULL;
}
void DelUser(const CString& sUser) {
map<CString, CAutoOpUser*>::iterator it = m_msUsers.find(sUser.AsLower());
if (it == m_msUsers.end()) {
PutModule("That user does not exist");
return;
}
delete it->second;
m_msUsers.erase(it);
PutModule("User [" + sUser + "] removed");
}
CAutoOpUser* AddUser(const CString& sUser, const CString& sKey, const CString& sHost, const CString& sChans) {
if (m_msUsers.find(sUser) != m_msUsers.end()) {
PutModule("That user already exists");
return NULL;
}
CAutoOpUser* pUser = new CAutoOpUser(sUser, sKey, sHost, sChans);
m_msUsers[sUser.AsLower()] = pUser;
PutModule("User [" + sUser + "] added with hostmask [" + sHost + "]");
return pUser;
}
bool ChallengeRespond(const CNick& Nick, const CString& sChallenge) {
// Validate before responding - don't blindly trust everyone
bool bValid = false;
bool bMatchedHost = false;
CAutoOpUser* pUser = NULL;
for (map<CString, CAutoOpUser*>::iterator it = m_msUsers.begin(); it != m_msUsers.end(); ++it) {
pUser = it->second;
// First verify that the guy who challenged us matches a user's host
if (pUser->HostMatches(Nick.GetHostMask())) {
const vector<CChan*>& Chans = m_pUser->GetChans();
bMatchedHost = true;
// Also verify that they are opped in at least one of the user's chans
for (size_t a = 0; a < Chans.size(); a++) {
const CChan& Chan = *Chans[a];
CNick* pNick = Chan.FindNick(Nick.GetNick());
if (pNick) {
if (pNick->HasPerm(CChan::Op) && pUser->ChannelMatches(Chan.GetName())) {
bValid = true;
break;
}
}
}
if (bValid) {
break;
}
}
}
if (!bValid) {
if (bMatchedHost) {
PutModule("[" + Nick.GetHostMask() + "] sent us a challenge but they are not opped in any defined channels.");
} else {
PutModule("[" + Nick.GetHostMask() + "] sent us a challenge but they do not match a defined user.");
}
return false;
}
if (sChallenge.length() != AUTOOP_CHALLENGE_LENGTH) {
PutModule("WARNING! [" + Nick.GetHostMask() + "] sent an invalid challenge.");
return false;
}
CString sResponse = pUser->GetUserKey() + "::" + sChallenge;
PutIRC("NOTICE " + Nick.GetNick() + " :!ZNCAO RESPONSE " + sResponse.MD5());
return false;
}
bool VerifyResponse(const CNick& Nick, const CString& sResponse) {
MCString::iterator itQueue = m_msQueue.find(Nick.GetNick().AsLower());
if (itQueue == m_msQueue.end()) {
PutModule("[" + Nick.GetHostMask() + "] sent an unchallenged response. This could be due to lag.");
return false;
}
CString sChallenge = itQueue->second;
m_msQueue.erase(itQueue);
for (map<CString, CAutoOpUser*>::iterator it = m_msUsers.begin(); it != m_msUsers.end(); ++it) {
if (it->second->HostMatches(Nick.GetHostMask())) {
if (sResponse == CString(it->second->GetUserKey() + "::" + sChallenge).MD5()) {
OpUser(Nick, *it->second);
return true;
} else {
PutModule("WARNING! [" + Nick.GetHostMask() + "] sent a bad response. Please verify that you have their correct password.");
return false;
}
}
}
PutModule("WARNING! [" + Nick.GetHostMask() + "] sent a response but did not match any defined users.");
return false;
}
void ProcessQueue() {
bool bRemoved = true;
// First remove any stale challenges
while (bRemoved) {
bRemoved = false;
for (MCString::iterator it = m_msQueue.begin(); it != m_msQueue.end(); ++it) {
if (!it->second.empty()) {
m_msQueue.erase(it);
bRemoved = true;
break;
}
}
}
// Now issue challenges for the new users in the queue
for (MCString::iterator it = m_msQueue.begin(); it != m_msQueue.end(); ++it) {
it->second = CString::RandomString(AUTOOP_CHALLENGE_LENGTH);
PutIRC("NOTICE " + it->first + " :!ZNCAO CHALLENGE " + it->second);
}
}
void OpUser(const CNick& Nick, const CAutoOpUser& User) {
const vector<CChan*>& Chans = m_pUser->GetChans();
for (size_t a = 0; a < Chans.size(); a++) {
const CChan& Chan = *Chans[a];
if (Chan.HasPerm(CChan::Op) && User.ChannelMatches(Chan.GetName())) {
CNick* pNick = Chan.FindNick(Nick.GetNick());
if (pNick && !pNick->HasPerm(CChan::Op)) {
PutIRC("MODE " + Chan.GetName() + " +o " + Nick.GetNick());
}
}
}
}
private:
map<CString, CAutoOpUser*> m_msUsers;
MCString m_msQueue;
};
void CAutoOpTimer::RunJob() {
m_pParent->ProcessQueue();
}
MODULEDEFS(CAutoOpMod, "Auto op the good guys")
|