]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/xline.cpp
3c9d8a269a050f26ddf04b4c6c6166314f2c140a
[user/henk/code/inspircd.git] / src / xline.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core: libIRCDxline */
15
16 #include "inspircd.h"
17 #include "wildcard.h"
18 #include "xline.h"
19
20 /*
21  * This is now version 3 of the XLine subsystem, let's see if we can get it as nice and 
22  * efficient as we can this time so we can close this file and never ever touch it again ..
23  *
24  * Background:
25  *  Version 1 stored all line types in one list (one for g, one for z, etc). This was fine,
26  *  but both version 1 and 2 suck at applying lines efficiently. That is, every time a new line
27  *  was added, it iterated every existing line for every existing user. Ow. Expiry was also
28  *  expensive, as the lists were NOT sorted.
29  *
30  *  Version 2 moved permanent lines into a seperate list from non-permanent to help optimize
31  *  matching speed, but matched in the same way.
32  *  Expiry was also sped up by sorting the list by expiry (meaning just remove the items at the
33  *  head of the list that are outdated.)
34  *
35  * This was fine and good, but it looked less than ideal in code, and matching was still slower
36  * than it could have been, something which we address here.
37  *
38  * VERSION 3:
39  *  All lines are (as in v1) stored together -- no seperation of perm and non-perm. Expiry will
40  *  still use a sorted list, and we'll just ignore anything permanent.
41  *
42  *  Application will be by a list of lines 'pending' application, meaning only the newly added lines
43  *  will be gone over. Much faster.
44  *
45  * More of course is to come.
46  */
47
48 /* Version two, now with optimized expiry!
49  *
50  * Because the old way was horrendously slow, the new way of expiring xlines is very
51  * very efficient. I have improved the efficiency of the algorithm in two ways:
52  *
53  * (1) There are now two lists of items for each linetype. One list holds temporary
54  *     items, and the other list holds permanent items (ones which will expire).
55  *     Items which are on the permanent list are NEVER checked at all by the
56  *     expire_lines() function.
57  * (2) The temporary xline lists are always kept in strict numerical order, keyed by
58  *     current time + duration. This means that the line which is due to expire the
59  *     soonest is always pointed at by vector::begin(), so a simple while loop can
60  *     very efficiently, very quickly and above all SAFELY pick off the first few
61  *     items in the vector which need zapping.
62  *
63  *     -- Brain
64  */
65
66 bool InitXLine(ServerConfig* conf, const char* tag)
67 {
68         return true;
69 }
70
71 bool DoneZLine(ServerConfig* conf, const char* tag)
72 {
73         // XXX we should really only call this once - after we've finished processing configuration all together
74         conf->GetInstance()->XLines->ApplyLines();
75         return true;
76 }
77
78 bool DoneQLine(ServerConfig* conf, const char* tag)
79 {
80         // XXX we should really only call this once - after we've finished processing configuration all together
81         conf->GetInstance()->XLines->ApplyLines();
82         return true;
83 }
84
85 bool DoneKLine(ServerConfig* conf, const char* tag)
86 {
87         // XXX we should really only call this once - after we've finished processing configuration all together
88         conf->GetInstance()->XLines->ApplyLines();
89         return true;
90 }
91
92 bool DoneELine(ServerConfig* conf, const char* tag)
93 {
94         // XXX we should really only call this once - after we've finished processing configuration all together
95         conf->GetInstance()->XLines->ApplyLines();
96         return true;
97 }
98
99 bool DoZLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
100 {
101         const char* reason = values[0].GetString();
102         const char* ipmask = values[1].GetString();
103
104         conf->GetInstance()->XLines->AddZLine(0,"<Config>",reason,ipmask);
105         return true;
106 }
107
108 bool DoQLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
109 {
110         const char* reason = values[0].GetString();
111         const char* nick = values[1].GetString();
112
113         conf->GetInstance()->XLines->AddQLine(0,"<Config>",reason,nick);
114         return true;
115 }
116
117 bool DoKLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
118 {
119         const char* reason = values[0].GetString();
120         const char* host = values[1].GetString();
121
122         conf->GetInstance()->XLines->AddKLine(0,"<Config>",reason,host);
123         return true;
124 }
125
126 bool DoELine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
127 {
128         const char* reason = values[0].GetString();
129         const char* host = values[1].GetString();
130
131         conf->GetInstance()->XLines->AddELine(0,"<Config>",reason,host);
132         return true;
133 }
134
135 bool XLine::Matches(User *u)
136 {
137 }
138
139 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
140 {
141         IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
142         std::string::size_type x = ident_and_host.find('@');
143         if (x != std::string::npos)
144         {
145                 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
146                 n.first = ident_and_host.substr(0, x);
147                 if (!n.first.length())
148                         n.first.assign("*");
149                 if (!n.second.length())
150                         n.second.assign("*");
151         }
152         else
153         {
154                 n.second = ident_and_host;
155         }
156
157         return n;
158 }
159
160 // adds a g:line
161
162 bool XLineManager::AddGLine(long duration, const char* source,const char* reason,const char* hostmask)
163 {
164         IdentHostPair ih = IdentSplit(hostmask);
165
166         if (DelGLine(hostmask, true))
167                 return false;
168
169         GLine* item = new GLine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
170
171         glines.push_back(item);
172         sort(glines.begin(), glines.end(),XLineManager::XSortComparison);
173
174         return true;
175 }
176
177 // adds an e:line (exception to bans)
178
179 bool XLineManager::AddELine(long duration, const char* source, const char* reason, const char* hostmask)
180 {
181         IdentHostPair ih = IdentSplit(hostmask);
182
183         if (DelELine(hostmask, true))
184                 return false;
185
186         ELine* item = new ELine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
187
188         elines.push_back(item);
189         sort(elines.begin(), elines.end(),XLineManager::XSortComparison);
190
191         return true;
192 }
193
194 // adds a q:line
195
196 bool XLineManager::AddQLine(long duration, const char* source, const char* reason, const char* nickname)
197 {
198         if (DelQLine(nickname, true))
199                 return false;
200
201         QLine* item = new QLine(ServerInstance->Time(), duration, source, reason, nickname);
202
203         qlines.push_back(item);
204         sort(qlines.begin(), qlines.end(),XLineManager::XSortComparison);
205
206         return true;
207 }
208
209 // adds a z:line
210
211 bool XLineManager::AddZLine(long duration, const char* source, const char* reason, const char* ipaddr)
212 {
213         if (strchr(ipaddr,'@'))
214         {
215                 while (*ipaddr != '@')
216                         ipaddr++;
217                 ipaddr++;
218         }
219
220         if (DelZLine(ipaddr, true))
221                 return false;
222
223         ZLine* item = new ZLine(ServerInstance->Time(), duration, source, reason, ipaddr);
224
225         zlines.push_back(item);
226         sort(zlines.begin(), zlines.end(),XLineManager::XSortComparison);
227
228         return true;
229 }
230
231 // adds a k:line
232
233 bool XLineManager::AddKLine(long duration, const char* source, const char* reason, const char* hostmask)
234 {
235         IdentHostPair ih = IdentSplit(hostmask);
236
237         if (DelKLine(hostmask, true))
238                 return false;
239
240         KLine* item = new KLine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
241
242         klines.push_back(item);
243         sort(klines.begin(), klines.end(),XLineManager::XSortComparison);
244
245         return true;
246 }
247
248 // deletes a g:line, returns true if the line existed and was removed
249
250 bool XLineManager::DelGLine(const char* hostmask, bool simulate)
251 {
252         IdentHostPair ih = IdentSplit(hostmask);
253         for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
254         {
255                 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
256                 {
257                         if (!simulate)
258                         {
259                                 delete *i;
260                                 glines.erase(i);
261                         }
262                         return true;
263                 }
264         }
265
266         return false;
267 }
268
269 // deletes a e:line, returns true if the line existed and was removed
270
271 bool XLineManager::DelELine(const char* hostmask, bool simulate)
272 {
273         IdentHostPair ih = IdentSplit(hostmask);
274         for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
275         {
276                 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
277                 {
278                         if (!simulate)
279                         {
280                                 delete *i;
281                                 elines.erase(i);
282                         }
283                         return true;
284                 }
285         }
286
287         return false;
288 }
289
290 // deletes a q:line, returns true if the line existed and was removed
291
292 bool XLineManager::DelQLine(const char* nickname, bool simulate)
293 {
294         for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
295         {
296                 if (!strcasecmp(nickname,(*i)->nick))
297                 {
298                         if (!simulate)
299                         {
300                                 delete *i;
301                                 qlines.erase(i);
302                         }
303                         return true;
304                 }
305         }
306
307         return false;
308 }
309
310 // deletes a z:line, returns true if the line existed and was removed
311
312 bool XLineManager::DelZLine(const char* ipaddr, bool simulate)
313 {
314         for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
315         {
316                 if (!strcasecmp(ipaddr,(*i)->ipaddr))
317                 {
318                         if (!simulate)
319                         {
320                                 delete *i;
321                                 zlines.erase(i);
322                         }
323                         return true;
324                 }
325         }
326
327         return false;
328 }
329
330 // deletes a k:line, returns true if the line existed and was removed
331
332 bool XLineManager::DelKLine(const char* hostmask, bool simulate)
333 {
334         IdentHostPair ih = IdentSplit(hostmask);
335         for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
336         {
337                 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
338                 {
339                         if (!simulate)
340                         {
341                                 delete *i;
342                                 klines.erase(i);
343                         }
344                         return true;
345                 }
346         }
347
348         return false;
349 }
350
351 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
352
353 QLine* XLineManager::matches_qline(const char* nick)
354 {
355         if (qlines.empty())
356                 return NULL;
357
358         for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
359                 if (match(nick,(*i)->nick))
360                         return (*i);
361         return NULL;
362 }
363
364 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
365
366 GLine* XLineManager::matches_gline(User* user)
367 {
368         if (glines.empty())
369                 return NULL;
370
371         for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
372         {
373                 if ((match(user->ident,(*i)->identmask)))
374                 {
375                         if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
376                         {
377                                 return (*i);
378                         }
379                 }
380         }
381
382         return NULL;
383 }
384
385 ELine* XLineManager::matches_exception(User* user)
386 {
387         if (elines.empty())
388                 return NULL;
389         char host2[MAXBUF];
390
391         snprintf(host2,MAXBUF,"*@%s",user->host);
392         for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
393         {
394                 if ((match(user->ident,(*i)->identmask)))
395                 {
396                         if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
397                         {
398                                 return (*i);
399                         }
400                 }
401         }
402         return NULL;
403 }
404
405
406 void XLineManager::gline_set_creation_time(const char* host, time_t create_time)
407 {
408         for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
409         {
410                 if (!strcasecmp(host,(*i)->hostmask))
411                 {
412                         (*i)->set_time = create_time;
413                         (*i)->expiry = create_time + (*i)->duration;
414                         return;
415                 }
416         }
417
418         return ;
419 }
420
421 void XLineManager::eline_set_creation_time(const char* host, time_t create_time)
422 {
423         for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
424         {
425                 if (!strcasecmp(host,(*i)->hostmask))
426                 {
427                         (*i)->set_time = create_time;
428                         (*i)->expiry = create_time + (*i)->duration;
429                         return;
430                 }
431         }
432
433         return;
434 }
435
436 void XLineManager::qline_set_creation_time(const char* nick, time_t create_time)
437 {
438         for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
439         {
440                 if (!strcasecmp(nick,(*i)->nick))
441                 {
442                         (*i)->set_time = create_time;
443                         (*i)->expiry = create_time + (*i)->duration;
444                         return;
445                 }
446         }
447
448         return;
449 }
450
451 void XLineManager::zline_set_creation_time(const char* ip, time_t create_time)
452 {
453         for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
454         {
455                 if (!strcasecmp(ip,(*i)->ipaddr))
456                 {
457                         (*i)->set_time = create_time;
458                         (*i)->expiry = create_time + (*i)->duration;
459                         return;
460                 }
461         }
462
463         return;
464 }
465
466 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
467
468 ZLine* XLineManager::matches_zline(const char* ipaddr)
469 {
470         if (zlines.empty())
471                 return NULL;
472
473         for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
474                 if (match(ipaddr,(*i)->ipaddr, true))
475                         return (*i);
476         return NULL;
477 }
478
479 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
480
481 KLine* XLineManager::matches_kline(User* user)
482 {
483         if (klines.empty())
484                 return NULL;
485
486         for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
487         {
488                 if ((match(user->ident,(*i)->identmask)))
489                 {
490                         if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
491                         {
492                                 return (*i);
493                         }
494                 }
495         }
496
497         return NULL;
498 }
499
500 bool XLineManager::XSortComparison(const XLine *one, const XLine *two)
501 {
502         // account for permanent lines
503         if (one->expiry == 0)
504         {
505                 return false;
506         }
507         return (one->expiry) < (two->expiry);
508 }
509
510 // removes lines that have expired
511 void XLineManager::expire_lines()
512 {
513         time_t current = ServerInstance->Time();
514
515         /* Because we now store all our XLines in sorted order using ((*i)->duration + (*i)->set_time) as a key, this
516          * means that to expire the XLines we just need to do a while, picking off the top few until there are
517          * none left at the head of the queue that are after the current time.
518          */
519
520         while ((glines.size()) && (current > (*glines.begin())->expiry) && ((*glines.begin())->duration != 0))
521         {
522                 std::vector<GLine*>::iterator i = glines.begin();
523                 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",(*i)->identmask,(*i)->hostmask,(*i)->source,(*i)->duration);
524                 glines.erase(i);
525         }
526
527         while ((elines.size()) && (current > (*elines.begin())->expiry) && ((*elines.begin())->duration != 0))
528         {
529                 std::vector<ELine*>::iterator i = elines.begin();
530                 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",(*i)->identmask,(*i)->hostmask,(*i)->source,(*i)->duration);
531                 elines.erase(i);
532         }
533
534         while ((zlines.size()) && (current > (*zlines.begin())->expiry) && ((*zlines.begin())->duration != 0))
535         {
536                 std::vector<ZLine*>::iterator i = zlines.begin();
537                 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",(*i)->ipaddr,(*i)->source,(*i)->duration);
538                 zlines.erase(i);
539         }
540
541         while ((klines.size()) && (current > (*klines.begin())->expiry) && ((*klines.begin())->duration != 0))
542         {
543                 std::vector<KLine*>::iterator i = klines.begin();
544                 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",(*i)->identmask,(*i)->hostmask,(*i)->source,(*i)->duration);
545                 klines.erase(i);
546         }
547
548         while ((qlines.size()) && (current > (*qlines.begin())->expiry) && ((*qlines.begin())->duration != 0))
549         {
550                 std::vector<QLine*>::iterator i = qlines.begin();
551                 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",(*i)->nick,(*i)->source,(*i)->duration);
552                 qlines.erase(i);
553         }
554
555 }
556
557 // applies lines, removing clients and changing nicks etc as applicable
558
559 void XLineManager::ApplyLines()
560 {
561         int What = 0; // XXX remove me
562         char reason[MAXBUF];
563
564         XLine* check = NULL;
565         for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
566         {
567                 User* u = (User*)(*u2);
568
569                 if (elines.size())
570                 {
571                         // ignore people matching exempts
572                         if (matches_exception(u))
573                                 continue;
574                 }
575                 if ((What & APPLY_GLINES) && (glines.size()))
576                 {
577                         if ((check = matches_gline(u)))
578                         {
579                                 snprintf(reason,MAXBUF,"G-Lined: %s",check->reason);
580                                 if (*ServerInstance->Config->MoronBanner)
581                                         u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
582                                 if (ServerInstance->Config->HideBans)
583                                         User::QuitUser(ServerInstance, u, "G-Lined", reason);
584                                 else
585                                         User::QuitUser(ServerInstance, u, reason);
586                         }
587                 }
588                 if ((What & APPLY_KLINES) && (klines.size()))
589                 {
590                         if ((check = matches_kline(u)))
591                         {
592                                 snprintf(reason,MAXBUF,"K-Lined: %s",check->reason);
593                                 if (*ServerInstance->Config->MoronBanner)
594                                         u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
595                                 if (ServerInstance->Config->HideBans)
596                                         User::QuitUser(ServerInstance, u, "K-Lined", reason);
597                                 else
598                                         User::QuitUser(ServerInstance, u, reason);
599                         }
600                 }
601                 if ((What & APPLY_QLINES) && (qlines.size()))
602                 {
603                         if ((check = matches_qline(u->nick)))
604                         {
605                                 snprintf(reason,MAXBUF,"Q-Lined: %s",check->reason);
606                                 if (*ServerInstance->Config->MoronBanner)
607                                         u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
608                                 if (ServerInstance->Config->HideBans)
609                                         User::QuitUser(ServerInstance, u, "Q-Lined", reason);
610                                 else
611                                         User::QuitUser(ServerInstance, u, reason);
612                         }
613                 }
614                 if ((What & APPLY_ZLINES) && (zlines.size()))
615                 {
616                         if ((check = matches_zline(u->GetIPString())))
617                         {
618                                 snprintf(reason,MAXBUF,"Z-Lined: %s", check->reason);
619                                 if (*ServerInstance->Config->MoronBanner)
620                                         u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
621                                 if (ServerInstance->Config->HideBans)
622                                         User::QuitUser(ServerInstance, u, "Z-Lined", reason);
623                                 else
624                                         User::QuitUser(ServerInstance, u, reason);
625                         }
626                 }
627         }
628 }
629
630 void XLineManager::stats_k(User* user, string_list &results)
631 {
632         std::string sn = ServerInstance->Config->ServerName;
633         for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
634                 results.push_back(sn+" 216 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
635 }
636
637 void XLineManager::stats_g(User* user, string_list &results)
638 {
639         std::string sn = ServerInstance->Config->ServerName;
640         for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
641                 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
642 }
643
644 void XLineManager::stats_q(User* user, string_list &results)
645 {
646         std::string sn = ServerInstance->Config->ServerName;
647         for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
648                 results.push_back(sn+" 217 "+user->nick+" :"+(*i)->nick+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
649 }
650
651 void XLineManager::stats_z(User* user, string_list &results)
652 {
653         std::string sn = ServerInstance->Config->ServerName;
654         for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
655                 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->ipaddr+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
656 }
657
658 void XLineManager::stats_e(User* user, string_list &results)
659 {
660         std::string sn = ServerInstance->Config->ServerName;
661         for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
662                 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
663 }
664
665 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)
666 {
667 }