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
|
/* 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"
class ExceptionDelCallback : public NumberList
{
protected:
CommandSource &source;
unsigned Deleted;
public:
ExceptionDelCallback(CommandSource &_source, const Anope::string &numlist) : NumberList(numlist, true), source(_source), Deleted(0)
{
}
~ExceptionDelCallback()
{
if (!Deleted)
source.Reply(_("No matching entries on session-limit exception list."));
else if (Deleted == 1)
source.Reply(_("Deleted 1 entry from session-limit exception list."));
else
source.Reply(_("Deleted %d entries from session-limit exception list."), Deleted);
}
virtual void HandleNumber(unsigned Number)
{
if (!Number || Number > exceptions.size())
return;
++Deleted;
DoDel(source, Number - 1);
}
static void DoDel(CommandSource &source, unsigned index)
{
FOREACH_MOD(I_OnExceptionDel, OnExceptionDel(source.u, exceptions[index]));
delete exceptions[index];
exceptions.erase(exceptions.begin() + index);
}
};
class ExceptionListCallback : public NumberList
{
protected:
CommandSource &source;
bool SentHeader;
public:
ExceptionListCallback(CommandSource &_source, const Anope::string &numlist) : NumberList(numlist, false), source(_source), SentHeader(false)
{
}
virtual void HandleNumber(unsigned Number)
{
if (!Number || Number > exceptions.size())
return;
if (!SentHeader)
{
SentHeader = true;
source.Reply(_("Current Session Limit Exception list:"));
source.Reply(_("Num Limit Host"));
}
DoList(source, Number - 1);
}
static void DoList(CommandSource &source, unsigned index)
{
if (index >= exceptions.size())
return;
source.Reply(_("%3d %4d %s"), index + 1, exceptions[index]->limit, exceptions[index]->mask.c_str());
}
};
class ExceptionViewCallback : public ExceptionListCallback
{
public:
ExceptionViewCallback(CommandSource &_source, const Anope::string &numlist) : ExceptionListCallback(_source, numlist)
{
}
void HandleNumber(unsigned Number)
{
if (!Number || Number > exceptions.size())
return;
if (!SentHeader)
{
SentHeader = true;
source.Reply(_("Current Session Limit Exception list:"));
}
DoList(source, Number - 1);
}
static void DoList(CommandSource &source, unsigned index)
{
if (index >= exceptions.size())
return;
Anope::string expirebuf = expire_left(source.u->Account(), exceptions[index]->expires);
source.Reply(_("%3d. %s (by %s on %s; %s)\n " " Limit: %-4d - %s"), index + 1, exceptions[index]->mask.c_str(), !exceptions[index]->who.empty() ? exceptions[index]->who.c_str() : "<unknown>", do_strftime((exceptions[index]->time ? exceptions[index]->time : Anope::CurTime)).c_str(), expirebuf.c_str(), exceptions[index]->limit, exceptions[index]->reason.c_str());
}
};
class CommandOSSession : public Command
{
private:
CommandReturn DoList(CommandSource &source, const std::vector<Anope::string> ¶ms)
{
Anope::string param = params[1];
unsigned mincount = 0;
try
{
mincount = convertTo<unsigned>(param);
}
catch (const ConvertException &) { }
if (mincount <= 1)
source.Reply(_("Invalid threshold value. It must be a valid integer greater than 1."));
else
{
source.Reply(_("Hosts with at least \002%d\002 sessions:"), mincount);
source.Reply(_("Sessions Host"));
for (patricia_tree<Session *>::iterator it(SessionList); it.next();)
{
Session *session = *it;
if (session->count >= mincount)
source.Reply(_("%6d %s"), session->count, session->host.c_str());
}
}
return MOD_CONT;
}
CommandReturn DoView(CommandSource &source, const std::vector<Anope::string> ¶ms)
{
Anope::string param = params[1];
Session *session = findsession(param);
if (!session)
source.Reply(_("\002%s\002 not found on session list."), param.c_str());
else
{
Exception *exception = find_host_exception(param);
source.Reply(_("The host \002%s\002 currently has \002%d\002 sessions with a limit of \002%d\002."), param.c_str(), session->count, exception ? exception-> limit : Config->DefSessionLimit);
}
return MOD_CONT;
}
public:
CommandOSSession() : Command("SESSION", 2, 2, "operserv/session")
{
this->SetDesc("View the list of host sessions");
}
CommandReturn Execute(CommandSource &source, const std::vector<Anope::string> ¶ms)
{
const Anope::string &cmd = params[0];
if (!Config->LimitSessions)
{
source.Reply(_("Session limiting is disabled."));
return MOD_CONT;
}
if (cmd.equals_ci("LIST"))
return this->DoList(source, params);
else if (cmd.equals_ci("VIEW"))
return this->DoView(source, params);
else
this->OnSyntaxError(source, "");
return MOD_CONT;
}
bool OnHelp(CommandSource &source, const Anope::string &subcommand)
{
source.Reply(_("Syntax: \002SESSION LIST \037threshold\037\002\n"
" \002SESSION VIEW \037host\037\002\n"
" \n"
"Allows Services Operators to view the session list.\n"
"\002SESSION LIST\002 lists hosts with at least \037threshold\037 sessions.\n"
"The threshold must be a number greater than 1. This is to \n"
"prevent accidental listing of the large number of single \n"
"session hosts.\n"
"\002SESSION VIEW\002 displays detailed information about a specific\n"
"host - including the current session count and session limit.\n"
"The \037host\037 value may not include wildcards.\n"
"See the \002EXCEPTION\002 help for more information about session\n"
"limiting and how to set session limits specific to certain\n"
"hosts and groups thereof."));
return true;
}
void OnSyntaxError(CommandSource &source, const Anope::string &subcommand)
{
SyntaxError(source, "SESSION", _("SESSION LIST \037limit\037"));
}
};
class CommandOSException : public Command
{
private:
CommandReturn DoAdd(CommandSource &source, const std::vector<Anope::string> ¶ms)
{
User *u = source.u;
Anope::string mask, expiry, limitstr;
unsigned last_param = 3;
int x;
mask = params.size() > 1 ? params[1] : "";
if (!mask.empty() && mask[0] == '+')
{
expiry = mask;
mask = params.size() > 2 ? params[2] : "";
last_param = 4;
}
limitstr = params.size() > last_param - 1 ? params[last_param - 1] : "";
if (params.size() <= last_param)
{
this->OnSyntaxError(source, "ADD");
return MOD_CONT;
}
Anope::string reason = params[last_param];
if (last_param == 3 && params.size() > 4)
reason += " " + params[4];
if (reason.empty())
{
this->OnSyntaxError(source, "ADD");
return MOD_CONT;
}
time_t expires = !expiry.empty() ? dotime(expiry) : Config->ExceptionExpiry;
if (expires < 0)
{
source.Reply(LanguageString::BAD_EXPIRY_TIME);
return MOD_CONT;
}
else if (expires > 0)
expires += Anope::CurTime;
int limit = -1;
try
{
limit = convertTo<int>(limitstr);
}
catch (const ConvertException &) { }
if (limit < 0 || limit > static_cast<int>(Config->MaxSessionLimit))
{
source.Reply(_("Invalid session limit. It must be a valid integer greater than or equal to zero and less than \002%d\002."), Config->MaxSessionLimit);
return MOD_CONT;
}
else
{
if (mask.find('!') != Anope::string::npos || mask.find('@') != Anope::string::npos)
{
source.Reply(_("Invalid hostmask. Only real hostmasks are valid as exceptions are not matched against nicks or usernames."));
return MOD_CONT;
}
x = exception_add(u, mask, limit, reason, u->nick, expires);
if (x == 1)
source.Reply(_("Session limit for \002%s\002 set to \002%d\002."), mask.c_str(), limit);
if (readonly)
source.Reply(LanguageString::READ_ONLY_MODE);
}
return MOD_CONT;
}
CommandReturn DoDel(CommandSource &source, const std::vector<Anope::string> ¶ms)
{
const Anope::string &mask = params.size() > 1 ? params[1] : "";
if (mask.empty())
{
this->OnSyntaxError(source, "DEL");
return MOD_CONT;
}
if (isdigit(mask[0]) && mask.find_first_not_of("1234567890,-") == Anope::string::npos)
{
ExceptionDelCallback list(source, mask);
list.Process();
}
else
{
unsigned i = 0, end = exceptions.size();
for (; i < end; ++i)
if (mask.equals_ci(exceptions[i]->mask))
{
ExceptionDelCallback::DoDel(source, i);
source.Reply(_("\002%s\002 deleted from session-limit exception list."), mask.c_str());
break;
}
if (i == end)
source.Reply(_("\002%s\002 not found on session-limit exception list."), mask.c_str());
}
if (readonly)
source.Reply(LanguageString::READ_ONLY_MODE);
return MOD_CONT;
}
CommandReturn DoMove(CommandSource &source, const std::vector<Anope::string> ¶ms)
{
const Anope::string &n1str = params.size() > 1 ? params[1] : ""; /* From position */
const Anope::string &n2str = params.size() > 2 ? params[2] : ""; /* To position */
int n1, n2;
if (n2str.empty())
{
this->OnSyntaxError(source, "MOVE");
return MOD_CONT;
}
n1 = n2 = -1;
try
{
n1 = convertTo<int>(n1str);
n2 = convertTo<int>(n2str);
}
catch (const ConvertException &) { }
if (n1 >= 0 && n1 < exceptions.size() && n2 >= 0 && n2 < exceptions.size() && n1 != n2)
{
Exception *temp = exceptions[n1];
exceptions[n1] = exceptions[n2];
exceptions[n2] = temp;
source.Reply(_("Exception for \002%s\002 (#%d) moved to position \002%d\002."), exceptions[n1]->mask.c_str(), n1 + 1, n2 + 1);
if (readonly)
source.Reply(LanguageString::READ_ONLY_MODE);
}
else
this->OnSyntaxError(source, "MOVE");
return MOD_CONT;
}
CommandReturn DoList(CommandSource &source, const std::vector<Anope::string> ¶ms)
{
expire_exceptions();
Anope::string mask = params.size() > 1 ? params[1] : "";
if (!mask.empty() && mask.find_first_not_of("1234567890,-") == Anope::string::npos)
{
ExceptionListCallback list(source, mask);
list.Process();
}
else
{
bool SentHeader = false;
for (unsigned i = 0, end = exceptions.size(); i < end; ++i)
if (mask.empty() || Anope::Match(exceptions[i]->mask, mask))
{
if (!SentHeader)
{
SentHeader = true;
source.Reply(_("Current Session Limit Exception list:"));
source.Reply(_("Num Limit Host"));
}
ExceptionListCallback::DoList(source, i);
}
if (!SentHeader)
source.Reply(_("No matching entries on session-limit exception list."));
}
return MOD_CONT;
}
CommandReturn DoView(CommandSource &source, const std::vector<Anope::string> ¶ms)
{
expire_exceptions();
Anope::string mask = params.size() > 1 ? params[1] : "";
if (!mask.empty() && mask.find_first_not_of("1234567890,-") == Anope::string::npos)
{
ExceptionViewCallback list(source, mask);
list.Process();
}
else
{
bool SentHeader = false;
for (unsigned i = 0, end = exceptions.size(); i < end; ++i)
if (mask.empty() || Anope::Match(exceptions[i]->mask, mask))
{
if (!SentHeader)
{
SentHeader = true;
source.Reply(_("Current Session Limit Exception list:"));
}
ExceptionViewCallback::DoList(source, i);
}
if (!SentHeader)
source.Reply(_("No matching entries on session-limit exception list."));
}
return MOD_CONT;
}
public:
CommandOSException() : Command("EXCEPTION", 1, 5)
{
this->SetDesc("Modify the session-limit exception list");
}
CommandReturn Execute(CommandSource &source, const std::vector<Anope::string> ¶ms)
{
const Anope::string &cmd = params[0];
if (!Config->LimitSessions)
{
source.Reply(_("Session limiting is disabled."));
return MOD_CONT;
}
if (cmd.equals_ci("ADD"))
return this->DoAdd(source, params);
else if (cmd.equals_ci("DEL"))
return this->DoDel(source, params);
else if (cmd.equals_ci("MOVE"))
return this->DoMove(source, params);
else if (cmd.equals_ci("LIST"))
return this->DoList(source, params);
else if (cmd.equals_ci("VIEW"))
return this->DoView(source, params);
else
this->OnSyntaxError(source, "");
return MOD_CONT;
}
bool OnHelp(CommandSource &source, const Anope::string &subcommand)
{
source.Reply(_("Syntax: \002EXCEPTION ADD [\037+expiry\037] \037mask\037 \037limit\037 \037reason\037\002\n"
" \002EXCEPTION DEL {\037mask\037 | \037list\037}\002\n"
" \002EXCEPTION MOVE \037num\037 \037position\037\002\n"
" \002EXCEPTION LIST [\037mask\037 | \037list\037]\002\n"
" \002EXCEPTION VIEW [\037mask\037 | \037list\037]\002\n"
" \n"
"Allows Services Operators to manipulate the list of hosts that\n"
"have specific session limits - allowing certain machines, \n"
"such as shell servers, to carry more than the default number\n"
"of clients at a time. Once a host reaches its session limit,\n"
"all clients attempting to connect from that host will be\n"
"killed. Before the user is killed, they are notified, via a\n"
"/NOTICE from %s, of a source of help regarding session\n"
"limiting. The content of this notice is a config setting.\n"
" \n"
"\002EXCEPTION ADD\002 adds the given host mask to the exception list.\n"
"Note that \002nick!user@host\002 and \002user@host\002 masks are invalid!\n"
"Only real host masks, such as \002box.host.dom\002 and \002*.host.dom\002,\n"
"are allowed because sessions limiting does not take nick or\n"
"user names into account. \037limit\037 must be a number greater than\n"
"or equal to zero. This determines how many sessions this host\n"
"may carry at a time. A value of zero means the host has an\n"
"unlimited session limit. See the \002AKILL\002 help for details about\n"
"the format of the optional \037expiry\037 parameter.\n"
"\002EXCEPTION DEL\002 removes the given mask from the exception list.\n"
"\002EXCEPTION MOVE\002 moves exception \037num\037 to \037position\037. The\n"
"exceptions inbetween will be shifted up or down to fill the gap.\n"
"\002EXCEPTION LIST\002 and \002EXCEPTION VIEW\002 show all current\n"
"exceptions; if the optional mask is given, the list is limited\n"
"to those exceptions matching the mask. The difference is that\n"
"\002EXCEPTION VIEW\002 is more verbose, displaying the name of the\n"
"person who added the exception, its session limit, reason, \n"
"host mask and the expiry date and time.\n"
" \n"
"Note that a connecting client will \"use\" the first exception\n"
"their host matches. Large exception lists and widely matching\n"
"exception masks are likely to degrade services' performance."),
OperServ->nick.c_str());
return true;
}
void OnSyntaxError(CommandSource &source, const Anope::string &subcommand)
{
SyntaxError(source, "EXCEPTION", _("EXCEPTION {ADD | DEL | MOVE | LIST | VIEW} [\037params\037]"));
}
};
class OSSession : public Module
{
CommandOSSession commandossession;
CommandOSException commandosexception;
public:
OSSession(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator)
{
this->SetAuthor("Anope");
this->SetType(CORE);
this->AddCommand(OperServ, &commandossession);
this->AddCommand(OperServ, &commandosexception);
}
};
MODULE_INIT(OSSession)
|