summaryrefslogtreecommitdiff
path: root/modules/core/os_defcon.cpp
blob: 03a8f0de49c939429b92b1d2e3cb045ec8f7a995 (plain)
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
/* OperServ core functions
 *
 * (C) 2003-2011 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"

void defcon_sendlvls(CommandSource &source);
void runDefCon();
void defconParseModeString(const Anope::string &str);
void resetDefCon(int level);
static Anope::string defconReverseModes(const Anope::string &modes);

class DefConTimeout : public Timer
{
	int level;

 public:
	DefConTimeout(int newlevel) : Timer(Config->DefConTimeOut), level(newlevel) { }

	void Tick(time_t)
	{
		if (Config->DefConLevel != level)
		{
			Config->DefConLevel = level;
			FOREACH_MOD(I_OnDefconLevel, OnDefconLevel(level));
			Log(OperServ, "operserv/defcon") << "Defcon level timeout, returning to level " << level;
			ircdproto->SendGlobops(OperServ, GetString(NULL, _("\002%s\002 Changed the DEFCON level to \002%d\002")).c_str(), Config->s_OperServ.c_str(), level);

			if (Config->GlobalOnDefcon)
			{
				if (!Config->DefConOffMessage.empty())
					oper_global("", "%s", Config->DefConOffMessage.c_str());
				else
					oper_global("", GetString(NULL, _("The Defcon Level is now at Level: \002%d\002")).c_str(), Config->DefConLevel);

				if (Config->GlobalOnDefconMore && Config->DefConOffMessage.empty())
					oper_global("", "%s", Config->DefconMessage.c_str());
			}

			runDefCon();
		}
	}
};
static DefConTimeout *timeout;

class CommandOSDefcon : public Command
{
 public:
	CommandOSDefcon() : Command("DEFCON", 1, 1, "operserv/defcon")
	{
		this->SetDesc(_("Manipulate the DefCon system"));
	}

	CommandReturn Execute(CommandSource &source, const std::vector<Anope::string> &params)
	{
		User *u = source.u;
		const Anope::string &lvl = params[0];

		if (lvl.empty())
		{
			source.Reply(_("Services are now at DEFCON \002%d\002"), Config->DefConLevel);
			defcon_sendlvls(source);
			return MOD_CONT;
		}

		int newLevel = 0;
		try
		{
			newLevel = convertTo<int>(lvl);
		}
		catch (const ConvertException &) { }

		if (newLevel < 1 || newLevel > 5)
		{
			this->OnSyntaxError(source, "");
			return MOD_CONT;
		}

		Config->DefConLevel = newLevel;

		FOREACH_MOD(I_OnDefconLevel, OnDefconLevel(newLevel));

		if (timeout)
		{
			delete timeout;
			timeout = NULL;
		}

		if (Config->DefConTimeOut)
			timeout = new DefConTimeout(5);

		source.Reply(_("Services are now at DEFCON \002%d\002"), Config->DefConLevel);
		defcon_sendlvls(source);
		Log(LOG_ADMIN, u, this) << "to change defcon level to " << newLevel;
		ircdproto->SendGlobops(OperServ, GetString(NULL, _("\002%s\002 Changed the DEFCON level to \002%d\002")).c_str(), u->nick.c_str(), newLevel);
		/* Global notice the user what is happening. Also any Message that
		   the Admin would like to add. Set in config file. */
		if (Config->GlobalOnDefcon)
		{
			if (Config->DefConLevel == 5 && !Config->DefConOffMessage.empty())
				oper_global("", "%s", Config->DefConOffMessage.c_str());
			else
				oper_global("", GetString(NULL, _("The Defcon Level is now at Level: \002%d\002")).c_str(), Config->DefConLevel);
		}
		if (Config->GlobalOnDefconMore)
		{
			if (Config->DefConOffMessage.empty() || Config->DefConLevel != 5)
				oper_global("", "%s", Config->DefconMessage.c_str());
		}
		/* Run any defcon functions, e.g. FORCE CHAN MODE */
		runDefCon();
		return MOD_CONT;
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand)
	{
		source.Reply(_("Syntax: \002DEFCON\002 [\0021\002|\0022\002|\0023\002|\0024\002|\0025\002]\n"
				"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;
	}

	void OnSyntaxError(CommandSource &source, const Anope::string &subcommand)
	{
		SyntaxError(source, "DEFCON", _("DEFCON [\0021\002|\0022\002|\0023\002|\0024\002|\0025\002]"));
	}
};

class OSDefcon : public Module
{
	CommandOSDefcon commandosdefcon;

 public:
	OSDefcon(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator)
	{
		if (!Config->DefConLevel)
			throw ModuleException("Invalid configuration settings");

		this->SetAuthor("Anope");
		this->SetType(CORE);

		Implementation i[] = { I_OnPreUserConnect, I_OnChannelModeSet, I_OnChannelModeUnset, I_OnPreCommandRun, I_OnPreCommand, I_OnUserConnect, I_OnChannelModeAdd, I_OnChannelCreate };
		ModuleManager::Attach(i, this, 8);

		this->AddCommand(OperServ, &commandosdefcon);

		defconParseModeString(Config->DefConChanModes);
	}

	EventReturn OnPreUserConnect(User *u)
	{
		if (u->server->IsSynced() && CheckDefCon(DEFCON_AKILL_NEW_CLIENTS) && !u->server->IsULined())
		{
			if (CheckDefCon(DEFCON_AKILL_NEW_CLIENTS))
			{
				Log(OperServ, "operserv/defcon") << "DEFCON: adding akill for *@" << u->host;
				XLine *x = SGLine->Add(NULL, NULL, "*@" + u->host, Anope::CurTime + Config->DefConAKILL, !Config->DefConAkillReason.empty() ? Config->DefConAkillReason : "DEFCON AKILL");
				if (x)
					x->By = Config->s_OperServ;
			}

			if (CheckDefCon(DEFCON_NO_NEW_CLIENTS) || CheckDefCon(DEFCON_AKILL_NEW_CLIENTS))
				kill_user(Config->s_OperServ, u, Config->DefConAkillReason);

			return EVENT_STOP;
		}

		return EVENT_CONTINUE;
	}

	EventReturn OnChannelModeSet(Channel *c, ChannelModeName Name, const Anope::string &param)
	{
		ChannelMode *cm = ModeManager::FindChannelModeByName(Name);

		if (CheckDefCon(DEFCON_FORCE_CHAN_MODES) && cm && DefConModesOff.HasFlag(Name))
		{
			c->RemoveMode(OperServ, Name, param);

			return EVENT_STOP;
		}

		return EVENT_CONTINUE;
	}

	EventReturn OnChannelModeUnset(Channel *c, ChannelModeName Name, const Anope::string &)
	{
		ChannelMode *cm = ModeManager::FindChannelModeByName(Name);

		if (CheckDefCon(DEFCON_FORCE_CHAN_MODES) && cm && DefConModesOn.HasFlag(Name))
		{
			Anope::string param;

			if (GetDefConParam(Name, param))
				c->SetMode(OperServ, Name, param);
			else
				c->SetMode(OperServ, Name);

			return EVENT_STOP;

		}

		return EVENT_CONTINUE;
	}

	EventReturn OnPreCommandRun(User *&u, BotInfo *&bi, Anope::string &command, Anope::string &message, ChannelInfo *&ci)
	{
		if (!u->HasMode(UMODE_OPER) && (CheckDefCon(DEFCON_OPER_ONLY) || CheckDefCon(DEFCON_SILENT_OPER_ONLY)))
		{
			if (!CheckDefCon(DEFCON_SILENT_OPER_ONLY))
				u->SendMessage(bi, _("Services are in Defcon mode, Please try again later."));

			return EVENT_STOP;
		}

		return EVENT_CONTINUE;
	}

	EventReturn OnPreCommand(CommandSource &source, Command *command, const std::vector<Anope::string> &params)
	{
		BotInfo *service = source.owner;
		if (service == NickServ)
		{
			if (command->name.equals_ci("REGISTER") || command->name.equals_ci("GROUP"))
			{
				if (CheckDefCon(DEFCON_NO_NEW_NICKS))
				{
					source.Reply(_("Services are in Defcon mode, Please try again later."));
					return EVENT_STOP;
				}
			}
		}
		else if (ChanServ && service == ChanServ)
		{
			if (command->name.equals_ci("SET"))
			{
				if (!params.empty() && params[0].equals_ci("MLOCK") && CheckDefCon(DEFCON_NO_MLOCK_CHANGE))
				{
					source.Reply(_("Services are in Defcon mode, Please try again later."));
					return EVENT_STOP;
				}
			}
			else if (command->name.equals_ci("REGISTER"))
			{
				if (CheckDefCon(DEFCON_NO_NEW_CHANNELS))
				{
					source.Reply(_("Services are in Defcon mode, Please try again later."));
					return EVENT_STOP;
				}
			}
		}
		else if (MemoServ && service == MemoServ)
		{
			if (command->name.equals_ci("SEND") || command->name.equals_ci("SENDALL"))
			{
				if (CheckDefCon(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)
	{
		Session *session = findsession(u->host);
		Exception *exception = find_hostip_exception(u->host, u->ip.addr());

		if (CheckDefCon(DEFCON_REDUCE_SESSION) && !exception)
		{
			if (session && session->count > Config->DefConSessionLimit)
			{
				if (!Config->SessionLimitExceeded.empty())
					ircdproto->SendMessage(OperServ, u->nick, Config->SessionLimitExceeded.c_str(), u->host.c_str());
				if (!Config->SessionLimitDetailsLoc.empty())
					ircdproto->SendMessage(OperServ, u->nick, "%s", Config->SessionLimitDetailsLoc.c_str());

				kill_user(Config->s_OperServ, u, "Session limit exceeded");
				++session->hits;
				if (Config->MaxSessionKill && session->hits >= Config->MaxSessionKill)
				{
					SGLine->Add(NULL, NULL, "*@" + u->host, Anope::CurTime + Config->SessionAutoKillExpiry, "Session limit exceeded");
					ircdproto->SendGlobops(OperServ, "Added a temporary AKILL for \2*@%s\2 due to excessive connections", u->host.c_str());
				}
			}
		}
	}

	void OnChannelModeAdd(ChannelMode *cm)
	{
		if (!Config->DefConChanModes.empty())
		{
			Anope::string modes = Config->DefConChanModes;

			if (modes.find(cm->ModeChar) != Anope::string::npos)
				/* New mode has been added to Anope, check to see if defcon
				 * requires it
				 */
				defconParseModeString(Config->DefConChanModes);
		}
	}

	void OnChannelCreate(Channel *c)
	{
		if (CheckDefCon(DEFCON_FORCE_CHAN_MODES))
			c->SetModes(OperServ, false, "%s", Config->DefConChanModes.c_str());
	}
};

/**
 * Send a message to the oper about which precautions are "active" for this level
 **/
void defcon_sendlvls(CommandSource &source)
{
	if (CheckDefCon(DEFCON_NO_NEW_CHANNELS))
		source.Reply(_("* No new channel registrations"));
	if (CheckDefCon(DEFCON_NO_NEW_NICKS))
		source.Reply(_("* No new nick registrations"));
	if (CheckDefCon(DEFCON_NO_MLOCK_CHANGE))
		source.Reply(_("* No MLOCK changes"));
	if (CheckDefCon(DEFCON_FORCE_CHAN_MODES) && !Config->DefConChanModes.empty())
		source.Reply(_("* Force Chan Modes (%s) to be set on all channels"), Config->DefConChanModes.c_str());
	if (CheckDefCon(DEFCON_REDUCE_SESSION))
		source.Reply(_("* Use the reduced session limit of %d"), Config->DefConSessionLimit);
	if (CheckDefCon(DEFCON_NO_NEW_CLIENTS))
		source.Reply(_("* Kill any NEW clients connecting"));
	if (CheckDefCon(DEFCON_OPER_ONLY))
		source.Reply(_("* Ignore any non-opers with message"));
	if (CheckDefCon(DEFCON_SILENT_OPER_ONLY))
		source.Reply(_("* Silently ignore non-opers"));
	if (CheckDefCon(DEFCON_AKILL_NEW_CLIENTS))
		source.Reply(_("* AKILL any new clients connecting"));
	if (CheckDefCon(DEFCON_NO_NEW_MEMOS))
		source.Reply(_("* No new memos sent"));
}

void runDefCon()
{
	if (CheckDefCon(DEFCON_FORCE_CHAN_MODES))
	{
		if (!Config->DefConChanModes.empty() && !DefConModesSet)
		{
			if (Config->DefConChanModes[0] == '+' || Config->DefConChanModes[0] == '-')
			{
				Log(OperServ, "operserv/defcon") << "DEFCON: setting " << Config->DefConChanModes << " on all channels";
				DefConModesSet = true;
				MassChannelModes(OperServ, Config->DefConChanModes);
			}
		}
	}
	else
	{
		if (!Config->DefConChanModes.empty() && DefConModesSet)
		{
			if (Config->DefConChanModes[0] == '+' || Config->DefConChanModes[0] == '-')
			{
				DefConModesSet = false;
				Anope::string newmodes = defconReverseModes(Config->DefConChanModes);
				if (!newmodes.empty())
				{
					Log(OperServ, "operserv/defcon") << "DEFCON: setting " << newmodes << " on all channels";
					MassChannelModes(OperServ, newmodes);
				}
			}
		}
	}
}

/* Parse the defcon mlock mode string and set the correct global vars.
 *
 * @param str mode string to parse
 * @return 1 if accepted, 0 if failed
 */
void defconParseModeString(const Anope::string &str)
{
	int add = -1; /* 1 if adding, 0 if deleting, -1 if neither */
	unsigned char mode;
	ChannelMode *cm;
	ChannelModeParam *cmp;
	Anope::string modes, param;

	if (str.empty())
		return;

	spacesepstream ss(str);

	DefConModesOn.ClearFlags();
	DefConModesOff.ClearFlags();
	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 || !cm->CanSet(NULL))
			{
				Log() << "DefConChanModes mode character '" << mode << "' cannot be locked";
				continue;
			}
			else if (add)
			{
				DefConModesOn.SetFlag(cm->Name);
				DefConModesOff.UnsetFlag(cm->Name);

				if (cm->Type == MODE_PARAM)
				{
					cmp = debug_cast<ChannelModeParam *>(cm);

					if (!ss.GetToken(param))
					{
						Log() << "DefConChanModes mode character '" << mode << "' has no parameter while one is expected";
						continue;
					}

					if (!cmp->IsValid(param))
						continue;

					SetDefConParam(cmp->Name, param);
				}
			}
			else
			{
				if (DefConModesOn.HasFlag(cm->Name))
				{
					DefConModesOn.UnsetFlag(cm->Name);

					if (cm->Type == MODE_PARAM)
						UnsetDefConParam(cm->Name);
				}
			}
		}
	}

	/* We can't mlock +L if +l is not mlocked as well. */
	if ((cm = ModeManager::FindChannelModeByName(CMODE_REDIRECT)) && DefConModesOn.HasFlag(cm->Name) && !DefConModesOn.HasFlag(CMODE_LIMIT))
	{
		DefConModesOn.UnsetFlag(CMODE_REDIRECT);

		Log() << "DefConChanModes must lock mode +l as well to lock mode +L";
	}

	/* Some ircd we can't set NOKNOCK without INVITE */
	/* So check if we need there is a NOKNOCK MODE and that we need INVITEONLY */
	if (ircd->knock_needs_i && (cm = ModeManager::FindChannelModeByName(CMODE_NOKNOCK)) && DefConModesOn.HasFlag(cm->Name) && !DefConModesOn.HasFlag(CMODE_INVITE))
	{
		DefConModesOn.UnsetFlag(CMODE_NOKNOCK);
		Log() << "DefConChanModes must lock mode +i as well to lock mode +K";
	}
}

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)