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 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
|
/* OperServ core functions
*
* (C) 2003-2016 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*
* Based on the original code of Epona by Lara.
* Based on the original code of Services by Andy Church.
*/
#include "module.h"
#include "modules/os_session.h"
enum DefconLevel
{
DEFCON_NO_NEW_CHANNELS,
DEFCON_NO_NEW_NICKS,
DEFCON_NO_MLOCK_CHANGE,
DEFCON_FORCE_CHAN_MODES,
DEFCON_REDUCE_SESSION,
DEFCON_NO_NEW_CLIENTS,
DEFCON_OPER_ONLY,
DEFCON_SILENT_OPER_ONLY,
DEFCON_AKILL_NEW_CLIENTS,
DEFCON_NO_NEW_MEMOS
};
bool DefConModesSet = false;
struct DefconConfig
{
std::vector<std::bitset<32> > DefCon;
std::set<Anope::string> DefConModesOn, DefConModesOff;
std::map<Anope::string, Anope::string> DefConModesOnParams;
int defaultlevel, sessionlimit;
Anope::string chanmodes, message, offmessage, akillreason;
std::vector<Anope::string> defcons;
time_t akillexpire, timeout;
bool globalondefcon;
unsigned max_session_kill;
time_t session_autokill_expiry;
Anope::string sle_reason, sle_detailsloc;
DefconConfig()
{
this->DefCon.resize(6);
this->defcons.resize(5);
}
bool Check(DefconLevel level)
{
return this->Check(this->defaultlevel, level);
}
bool Check(int dlevel, DefconLevel level)
{
return this->DefCon[dlevel].test(level);
}
void Add(int dlevel, DefconLevel level)
{
this->DefCon[dlevel][level] = true;
}
void Del(int dlevel, DefconLevel level)
{
this->DefCon[dlevel][level] = false;
}
bool SetDefConParam(const Anope::string &name, const Anope::string &buf)
{
return DefConModesOnParams.insert(std::make_pair(name, buf)).second;
}
void UnsetDefConParam(const Anope::string &name)
{
DefConModesOnParams.erase(name);
}
bool GetDefConParam(const Anope::string &name, Anope::string &buf)
{
std::map<Anope::string, Anope::string>::iterator it = DefConModesOnParams.find(name);
buf.clear();
if (it != DefConModesOnParams.end())
{
buf = it->second;
return true;
}
return false;
}
};
static DefconConfig DConfig;
static void runDefCon();
static Anope::string defconReverseModes(const Anope::string &modes);
static ServiceReference<GlobalService> GlobalService("GlobalService", "Global");
static Timer *timeout;
class DefConTimeout : public Timer
{
int level;
public:
DefConTimeout(Module *mod, int newlevel) : Timer(mod, DConfig.timeout), level(newlevel)
{
timeout = this;
}
~DefConTimeout()
{
timeout = NULL;
}
void Tick(time_t) anope_override
{
if (DConfig.defaultlevel != level)
{
DConfig.defaultlevel = level;
FOREACH_MOD(OnDefconLevel, (level));
Log(Config->GetClient("OperServ"), "operserv/defcon") << "Defcon level timeout, returning to level " << level;
if (DConfig.globalondefcon)
{
if (!DConfig.offmessage.empty())
GlobalService->SendGlobal(NULL, "", DConfig.offmessage);
else
GlobalService->SendGlobal(NULL, "", Anope::printf(Language::Translate(_("The Defcon level is now at: \002%d\002")), DConfig.defaultlevel));
if (!DConfig.message.empty())
GlobalService->SendGlobal(NULL, "", DConfig.message);
}
runDefCon();
}
}
};
class CommandOSDefcon : public Command
{
void SendLevels(CommandSource &source)
{
if (DConfig.Check(DEFCON_NO_NEW_CHANNELS))
source.Reply(_("* No new channel registrations"));
if (DConfig.Check(DEFCON_NO_NEW_NICKS))
source.Reply(_("* No new nick registrations"));
if (DConfig.Check(DEFCON_NO_MLOCK_CHANGE))
source.Reply(_("* No mode lock changes"));
if (DConfig.Check(DEFCON_FORCE_CHAN_MODES) && !DConfig.chanmodes.empty())
source.Reply(_("* Force channel modes (%s) to be set on all channels"), DConfig.chanmodes.c_str());
if (DConfig.Check(DEFCON_REDUCE_SESSION))
source.Reply(_("* Use the reduced session limit of %d"), DConfig.sessionlimit);
if (DConfig.Check(DEFCON_NO_NEW_CLIENTS))
source.Reply(_("* Kill any new clients connecting"));
if (DConfig.Check(DEFCON_OPER_ONLY))
source.Reply(_("* Ignore non-opers with a message"));
if (DConfig.Check(DEFCON_SILENT_OPER_ONLY))
source.Reply(_("* Silently ignore non-opers"));
if (DConfig.Check(DEFCON_AKILL_NEW_CLIENTS))
source.Reply(_("* AKILL any new clients connecting"));
if (DConfig.Check(DEFCON_NO_NEW_MEMOS))
source.Reply(_("* No new memos sent"));
}
public:
CommandOSDefcon(Module *creator) : Command(creator, "operserv/defcon", 1, 1)
{
this->SetDesc(_("Manipulate the DefCon system"));
this->SetSyntax(_("[\0021\002|\0022\002|\0023\002|\0024\002|\0025\002]"));
}
void Execute(CommandSource &source, const std::vector<Anope::string> ¶ms) anope_override
{
const Anope::string &lvl = params[0];
if (lvl.empty())
{
source.Reply(_("Services are now at DEFCON \002%d\002."), DConfig.defaultlevel);
this->SendLevels(source);
return;
}
int newLevel = 0;
try
{
newLevel = convertTo<int>(lvl);
}
catch (const ConvertException &) { }
if (newLevel < 1 || newLevel > 5)
{
this->OnSyntaxError(source, "");
return;
}
DConfig.defaultlevel = newLevel;
FOREACH_MOD(OnDefconLevel, (newLevel));
delete timeout;
if (DConfig.timeout)
timeout = new DefConTimeout(this->module, 5);
source.Reply(_("Services are now at DEFCON \002%d\002."), DConfig.defaultlevel);
this->SendLevels(source);
Log(LOG_ADMIN, source, this) << "to change defcon level to " << newLevel;
/* Global notice the user what is happening. Also any Message that
the Admin would like to add. Set in config file. */
if (DConfig.globalondefcon)
{
if (DConfig.defaultlevel == 5 && !DConfig.offmessage.empty())
GlobalService->SendGlobal(NULL, "", DConfig.offmessage);
else if (DConfig.defaultlevel != 5)
{
GlobalService->SendGlobal(NULL, "", Anope::printf(_("The Defcon level is now at: \002%d\002"), DConfig.defaultlevel));
if (!DConfig.message.empty())
GlobalService->SendGlobal(NULL, "", DConfig.message);
}
}
/* Run any defcon functions, e.g. FORCE CHAN MODE */
runDefCon();
return;
}
bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
{
this->SendSyntax(source);
source.Reply(" ");
source.Reply(_("The defcon system can be used to implement a pre-defined\n"
"set of restrictions to services useful during an attempted\n"
"attack on the network."));
return true;
}
};
class OSDefcon : public Module
{
ServiceReference<SessionService> session_service;
ServiceReference<XLineManager> akills;
CommandOSDefcon commandosdefcon;
void ParseModeString()
{
int add = -1; /* 1 if adding, 0 if deleting, -1 if neither */
unsigned char mode;
ChannelMode *cm;
ChannelModeParam *cmp;
Anope::string modes, param;
spacesepstream ss(DConfig.chanmodes);
DConfig.DefConModesOn.clear();
DConfig.DefConModesOff.clear();
ss.GetToken(modes);
/* Loop while there are modes to set */
for (unsigned i = 0, end = modes.length(); i < end; ++i)
{
mode = modes[i];
switch (mode)
{
case '+':
add = 1;
continue;
case '-':
add = 0;
continue;
default:
if (add < 0)
continue;
}
if ((cm = ModeManager::FindChannelModeByChar(mode)))
{
if (cm->type == MODE_STATUS || cm->type == MODE_LIST)
{
Log(this) << "DefConChanModes mode character '" << mode << "' cannot be locked";
continue;
}
else if (add)
{
DConfig.DefConModesOn.insert(cm->name);
DConfig.DefConModesOff.erase(cm->name);
if (cm->type == MODE_PARAM)
{
cmp = anope_dynamic_static_cast<ChannelModeParam *>(cm);
if (!ss.GetToken(param))
{
Log(this) << "DefConChanModes mode character '" << mode << "' has no parameter while one is expected";
continue;
}
if (!cmp->IsValid(param))
continue;
DConfig.SetDefConParam(cmp->name, param);
}
}
else if (DConfig.DefConModesOn.count(cm->name))
{
DConfig.DefConModesOn.erase(cm->name);
if (cm->type == MODE_PARAM)
DConfig.UnsetDefConParam(cm->name);
}
}
}
/* We can't mlock +L if +l is not mlocked as well. */
if ((cm = ModeManager::FindChannelModeByName("REDIRECT")) && DConfig.DefConModesOn.count(cm->name) && !DConfig.DefConModesOn.count("LIMIT"))
{
DConfig.DefConModesOn.erase("REDIRECT");
Log(this) << "DefConChanModes must lock mode +l as well to lock mode +L";
}
}
public:
OSDefcon(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), session_service("SessionService", "session"), akills("XLineManager", "xlinemanager/sgline"), commandosdefcon(this)
{
}
void OnReload(Configuration::Conf *conf) anope_override
{
Configuration::Block *block = conf->GetModule(this);
DefconConfig dconfig;
dconfig.defaultlevel = block->Get<int>("defaultlevel");
dconfig.defcons[4] = block->Get<const Anope::string>("level4");
dconfig.defcons[3] = block->Get<const Anope::string>("level3");
dconfig.defcons[2] = block->Get<const Anope::string>("level2");
dconfig.defcons[1] = block->Get<const Anope::string>("level1");
dconfig.sessionlimit = block->Get<int>("sessionlimit");
dconfig.akillreason = block->Get<const Anope::string>("akillreason");
dconfig.akillexpire = block->Get<time_t>("akillexpire");
dconfig.chanmodes = block->Get<const Anope::string>("chanmodes");
dconfig.timeout = block->Get<time_t>("timeout");
dconfig.globalondefcon = block->Get<bool>("globalondefcon");
dconfig.message = block->Get<const Anope::string>("message");
dconfig.offmessage = block->Get<const Anope::string>("offmessage");
block = conf->GetModule("os_session");
dconfig.max_session_kill = block->Get<int>("maxsessionkill");
dconfig.session_autokill_expiry = block->Get<time_t>("sessionautokillexpiry");
dconfig.sle_reason = block->Get<const Anope::string>("sessionlimitexceeded");
dconfig.sle_detailsloc = block->Get<const Anope::string>("sessionlimitdetailsloc");
if (dconfig.defaultlevel < 1 || dconfig.defaultlevel > 5)
throw ConfigException("The value for <defcon:defaultlevel> must be between 1 and 5");
else if (dconfig.akillexpire <= 0)
throw ConfigException("The value for <defcon:akillexpire> must be greater than zero!");
for (unsigned level = 1; level < 5; ++level)
{
spacesepstream operations(dconfig.defcons[level]);
Anope::string operation;
while (operations.GetToken(operation))
{
if (operation.equals_ci("nonewchannels"))
dconfig.Add(level, DEFCON_NO_NEW_CHANNELS);
else if (operation.equals_ci("nonewnicks"))
dconfig.Add(level, DEFCON_NO_NEW_NICKS);
else if (operation.equals_ci("nomlockchanges"))
dconfig.Add(level, DEFCON_NO_MLOCK_CHANGE);
else if (operation.equals_ci("forcechanmodes"))
dconfig.Add(level, DEFCON_FORCE_CHAN_MODES);
else if (operation.equals_ci("reducedsessions"))
dconfig.Add(level, DEFCON_REDUCE_SESSION);
else if (operation.equals_ci("nonewclients"))
dconfig.Add(level, DEFCON_NO_NEW_CLIENTS);
else if (operation.equals_ci("operonly"))
dconfig.Add(level, DEFCON_OPER_ONLY);
else if (operation.equals_ci("silentoperonly"))
dconfig.Add(level, DEFCON_SILENT_OPER_ONLY);
else if (operation.equals_ci("akillnewclients"))
dconfig.Add(level, DEFCON_AKILL_NEW_CLIENTS);
else if (operation.equals_ci("nonewmemos"))
dconfig.Add(level, DEFCON_NO_NEW_MEMOS);
}
if (dconfig.Check(level, DEFCON_REDUCE_SESSION) && dconfig.sessionlimit <= 0)
throw ConfigException("The value for <defcon:sessionlimit> must be greater than zero!");
else if (dconfig.Check(level, DEFCON_AKILL_NEW_CLIENTS) && dconfig.akillreason.empty())
throw ConfigException("The value for <defcon:akillreason> must not be empty!");
else if (dconfig.Check(level, DEFCON_FORCE_CHAN_MODES) && dconfig.chanmodes.empty())
throw ConfigException("The value for <defcon:chanmodes> must not be empty!");
}
DConfig = dconfig;
this->ParseModeString();
}
EventReturn OnChannelModeSet(Channel *c, MessageSource &source, ChannelMode *mode, const Anope::string ¶m) anope_override
{
if (DConfig.Check(DEFCON_FORCE_CHAN_MODES) && DConfig.DefConModesOff.count(mode->name) && source.GetUser() && !source.GetBot())
{
c->RemoveMode(Config->GetClient("OperServ"), mode, param);
return EVENT_STOP;
}
return EVENT_CONTINUE;
}
EventReturn OnChannelModeUnset(Channel *c, MessageSource &source, ChannelMode *mode, const Anope::string &) anope_override
{
if (DConfig.Check(DEFCON_FORCE_CHAN_MODES) && DConfig.DefConModesOn.count(mode->name) && source.GetUser() && !source.GetBot())
{
Anope::string param;
if (DConfig.GetDefConParam(mode->name, param))
c->SetMode(Config->GetClient("OperServ"), mode, param);
else
c->SetMode(Config->GetClient("OperServ"), mode);
return EVENT_STOP;
}
return EVENT_CONTINUE;
}
EventReturn OnPreCommand(CommandSource &source, Command *command, std::vector<Anope::string> ¶ms) anope_override
{
if (DConfig.Check(DEFCON_OPER_ONLY) && !source.IsOper())
{
source.Reply(_("Services are in DefCon mode, please try again later."));
return EVENT_STOP;
}
else if (DConfig.Check(DEFCON_SILENT_OPER_ONLY) && !source.IsOper())
{
return EVENT_STOP;
}
else if (command->name == "nickserv/register" || command->name == "nickserv/group")
{
if (DConfig.Check(DEFCON_NO_NEW_NICKS))
{
source.Reply(_("Services are in DefCon mode, please try again later."));
return EVENT_STOP;
}
}
else if (command->name == "chanserv/mode" && params.size() > 1 && params[1].equals_ci("LOCK"))
{
if (DConfig.Check(DEFCON_NO_MLOCK_CHANGE))
{
source.Reply(_("Services are in DefCon mode, please try again later."));
return EVENT_STOP;
}
}
else if (command->name == "chanserv/register")
{
if (DConfig.Check(DEFCON_NO_NEW_CHANNELS))
{
source.Reply(_("Services are in DefCon mode, please try again later."));
return EVENT_STOP;
}
}
else if (command->name == "memoserv/send")
{
if (DConfig.Check(DEFCON_NO_NEW_MEMOS))
{
source.Reply(_("Services are in DefCon mode, please try again later."));
return EVENT_STOP;
}
}
return EVENT_CONTINUE;
}
void OnUserConnect(User *u, bool &exempt) anope_override
{
if (exempt || u->Quitting() || !u->server->IsSynced() || u->server->IsULined())
return;
BotInfo *OperServ = Config->GetClient("OperServ");
if (DConfig.Check(DEFCON_AKILL_NEW_CLIENTS) && akills)
{
Log(OperServ, "operserv/defcon") << "DEFCON: adding akill for *@" << u->host;
XLine x("*@" + u->host, OperServ ? OperServ->nick : "defcon", Anope::CurTime + DConfig.akillexpire, DConfig.akillreason, XLineManager::GenerateUID());
akills->Send(NULL, &x);
}
if (DConfig.Check(DEFCON_NO_NEW_CLIENTS) || DConfig.Check(DEFCON_AKILL_NEW_CLIENTS))
{
u->Kill(OperServ, DConfig.akillreason);
return;
}
if (DConfig.sessionlimit <= 0 || !session_service)
return;
Session *session = session_service->FindSession(u->ip.addr());
Exception *exception = session_service->FindException(u);
if (DConfig.Check(DEFCON_REDUCE_SESSION) && !exception)
{
if (session && session->count > static_cast<unsigned>(DConfig.sessionlimit))
{
if (!DConfig.sle_reason.empty())
{
Anope::string message = DConfig.sle_reason.replace_all_cs("%IP%", u->ip.addr());
u->SendMessage(OperServ, message);
}
if (!DConfig.sle_detailsloc.empty())
u->SendMessage(OperServ, DConfig.sle_detailsloc);
++session->hits;
if (akills && DConfig.max_session_kill && session->hits >= DConfig.max_session_kill)
{
XLine x("*@" + session->addr.mask(), OperServ ? OperServ->nick : "", Anope::CurTime + DConfig.session_autokill_expiry, "Defcon session limit exceeded", XLineManager::GenerateUID());
akills->Send(NULL, &x);
Log(OperServ, "akill/defcon") << "[DEFCON] Added a temporary AKILL for \002*@" << session->addr.mask() << "\002 due to excessive connections";
}
else
{
u->Kill(OperServ, "Defcon session limit exceeded");
}
}
}
}
void OnChannelModeAdd(ChannelMode *cm) anope_override
{
if (DConfig.chanmodes.find(cm->mchar) != Anope::string::npos)
this->ParseModeString();
}
void OnChannelSync(Channel *c) anope_override
{
if (DConfig.Check(DEFCON_FORCE_CHAN_MODES))
c->SetModes(Config->GetClient("OperServ"), false, "%s", DConfig.chanmodes.c_str());
}
};
static void runDefCon()
{
BotInfo *OperServ = Config->GetClient("OperServ");
if (DConfig.Check(DEFCON_FORCE_CHAN_MODES))
{
if (!DConfig.chanmodes.empty() && !DefConModesSet)
{
if (DConfig.chanmodes[0] == '+' || DConfig.chanmodes[0] == '-')
{
Log(OperServ, "operserv/defcon") << "DEFCON: setting " << DConfig.chanmodes << " on all channels";
DefConModesSet = true;
for (channel_map::const_iterator it = ChannelList.begin(), it_end = ChannelList.end(); it != it_end; ++it)
it->second->SetModes(OperServ, false, "%s", DConfig.chanmodes.c_str());
}
}
}
else
{
if (!DConfig.chanmodes.empty() && DefConModesSet)
{
if (DConfig.chanmodes[0] == '+' || DConfig.chanmodes[0] == '-')
{
DefConModesSet = false;
Anope::string newmodes = defconReverseModes(DConfig.chanmodes);
if (!newmodes.empty())
{
Log(OperServ, "operserv/defcon") << "DEFCON: setting " << newmodes << " on all channels";
for (channel_map::const_iterator it = ChannelList.begin(), it_end = ChannelList.end(); it != it_end; ++it)
it->second->SetModes(OperServ, true, "%s", newmodes.c_str());
}
}
}
}
}
static Anope::string defconReverseModes(const Anope::string &modes)
{
if (modes.empty())
return "";
Anope::string newmodes;
for (unsigned i = 0, end = modes.length(); i < end; ++i)
{
if (modes[i] == '+')
newmodes += '-';
else if (modes[i] == '-')
newmodes += '+';
else
newmodes += modes[i];
}
return newmodes;
}
MODULE_INIT(OSDefcon)
|