]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/xline.cpp
0c6965fe31b03b7f8aaac6b1a30c5be035ae180f
[user/henk/code/inspircd.git] / src / xline.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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 */
15
16 #include "inspircd.h"
17 #include "xline.h"
18 #include "bancache.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. They are stored in
40  *  a map of maps (first map is line type, second map is for quick lookup on add/delete/etc).
41  *
42  *  Expiry is *no longer* performed on a timer, and no longer uses a sorted list of any variety. This
43  *  is now done by only checking for expiry when a line is accessed, meaning that expiry is no longer
44  *  a resource intensive problem.
45  *
46  *  Application no longer tries to apply every single line on every single user - instead, now only lines
47  *  added since the previous application are applied. This keeps S2S ADDLINE during burst nice and fast,
48  *  while at the same time not slowing things the fuck down when we try adding a ban with lots of preexisting
49  *  bans. :)
50  */
51
52 bool XLine::Matches(User *u)
53 {
54         return false;
55 }
56
57 /*
58  * Checks what users match a given vector of ELines and sets their ban exempt flag accordingly.
59  */
60 void XLineManager::CheckELines()
61 {
62         ContainerIter n = lookup_lines.find("E");
63
64         if (n == lookup_lines.end())
65                 return;
66
67         XLineLookup& ELines = n->second;
68
69         if (ELines.empty())
70                 return;
71
72         for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
73         {
74                 User* u = (User*)(*u2);
75
76                 /* This uses safe iteration to ensure that if a line expires here, it doenst trash the iterator */
77                 LookupIter safei;
78
79                 for (LookupIter i = ELines.begin(); i != ELines.end(); )
80                 {
81                         safei = i;
82                         safei++;
83
84                         XLine *e = i->second;
85                         u->exempt = e->Matches(u);
86
87                         i = safei;
88                 }
89         }
90 }
91
92
93 XLineLookup* XLineManager::GetAll(const std::string &type)
94 {
95         ContainerIter n = lookup_lines.find(type);
96
97         if (n == lookup_lines.end())
98                 return NULL;
99
100         LookupIter safei;
101         const time_t current = ServerInstance->Time();
102
103         /* Expire any dead ones, before sending */
104         for (LookupIter x = n->second.begin(); x != n->second.end(); )
105         {
106                 safei = x;
107                 safei++;
108                 if (x->second->duration && current > x->second->expiry)
109                 {
110                         ExpireLine(n, x);
111                 }
112                 x = safei;
113         }
114
115         return &(n->second);
116 }
117
118 void XLineManager::DelAll(const std::string &type)
119 {
120         ContainerIter n = lookup_lines.find(type);
121
122         if (n == lookup_lines.end())
123                 return;
124
125         LookupIter x;
126
127         /* Delete all of a given type (this should probably use DelLine, but oh well) */
128         while ((x = n->second.begin()) != n->second.end())
129         {
130                 ExpireLine(n, x);
131         }
132 }
133
134 std::vector<std::string> XLineManager::GetAllTypes()
135 {
136         std::vector<std::string> items;
137         for (ContainerIter x = lookup_lines.begin(); x != lookup_lines.end(); ++x)
138                 items.push_back(x->first);
139         return items;
140 }
141
142 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
143 {
144         IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
145         std::string::size_type x = ident_and_host.find('@');
146         if (x != std::string::npos)
147         {
148                 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
149                 n.first = ident_and_host.substr(0, x);
150                 if (!n.first.length())
151                         n.first.assign("*");
152                 if (!n.second.length())
153                         n.second.assign("*");
154         }
155         else
156         {
157                 n.first = "";
158                 n.second = ident_and_host;
159         }
160
161         return n;
162 }
163
164 // adds a line
165
166 bool XLineManager::AddLine(XLine* line, User* user)
167 {
168         /*IdentHostPair ih = IdentSplit(hostmask);*/
169
170         ServerInstance->BanCache->RemoveEntries(line->type, false); // XXX perhaps remove ELines here?
171
172         if (DelLine(line->Displayable(), line->type, user, true))
173                 return false;
174
175         /*ELine* item = new ELine(ServerInstance, ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());*/
176         XLineFactory* xlf = GetFactory(line->type);
177         if (!xlf)
178                 return false;
179
180         if (xlf->AutoApplyToUserList(line))
181                 pending_lines.push_back(line);
182
183         lookup_lines[line->type][line->Displayable()] = line;
184         line->OnAdd();
185
186         FOREACH_MOD(I_OnAddLine,OnAddLine(user, line)); 
187
188         return true;
189 }
190
191 // deletes a line, returns true if the line existed and was removed
192
193 bool XLineManager::DelLine(const char* hostmask, const std::string &type, User* user, bool simulate)
194 {
195         ContainerIter x = lookup_lines.find(type);
196
197         if (x == lookup_lines.end())
198                 return false;
199
200         LookupIter y = x->second.find(hostmask);
201
202         if (y == x->second.end())
203                 return false;
204
205         if (simulate)
206                 return true;
207
208         ServerInstance->BanCache->RemoveEntries(y->second->type, true);
209
210         FOREACH_MOD(I_OnDelLine,OnDelLine(user, y->second));
211
212         y->second->Unset();
213
214         std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), y->second);
215         if (pptr != pending_lines.end())
216                 pending_lines.erase(pptr);
217
218         delete y->second;
219         x->second.erase(y);
220
221         return true;
222 }
223
224
225 void ELine::Unset()
226 {
227         /* remove exempt from everyone and force recheck after deleting eline */
228         for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
229         {
230                 User* u = (User*)(*u2);
231                 u->exempt = false;
232         }
233
234         ServerInstance->XLines->CheckELines();
235 }
236
237 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
238
239 XLine* XLineManager::MatchesLine(const std::string &type, User* user)
240 {
241         ContainerIter x = lookup_lines.find(type);
242
243         if (x == lookup_lines.end())
244                 return NULL;
245
246         const time_t current = ServerInstance->Time();
247
248         LookupIter safei;
249
250         for (LookupIter i = x->second.begin(); i != x->second.end(); )
251         {
252                 safei = i;
253                 safei++;
254
255                 if (i->second->Matches(user))
256                 {
257                         if (i->second->duration && current > i->second->expiry)
258                         {
259                                 /* Expire the line, return nothing */
260                                 ExpireLine(x, i);
261                                 /* Continue, there may be another that matches
262                                  * (thanks aquanight)
263                                  */
264                                 i = safei;
265                                 continue;
266                         }
267                         else
268                                 return i->second;
269                 }
270
271                 i = safei;
272         }
273         return NULL;
274 }
275
276 XLine* XLineManager::MatchesLine(const std::string &type, const std::string &pattern)
277 {
278         ContainerIter x = lookup_lines.find(type);
279
280         if (x == lookup_lines.end())
281                 return NULL;
282
283         const time_t current = ServerInstance->Time();
284
285          LookupIter safei;
286
287         for (LookupIter i = x->second.begin(); i != x->second.end(); )
288         {
289                 safei = i;
290                 safei++;
291
292                 if (i->second->Matches(pattern))
293                 {
294                         if (i->second->duration && current > i->second->expiry)
295                         {
296                                 /* Expire the line, return nothing */
297                                 ExpireLine(x, i);
298                                 /* See above */
299                                 i = safei;
300                                 continue;
301                         }
302                         else
303                                 return i->second;
304                 }
305
306                 i = safei;
307         }
308         return NULL;
309 }
310
311 // removes lines that have expired
312 void XLineManager::ExpireLine(ContainerIter container, LookupIter item)
313 {
314         FOREACH_MOD(I_OnExpireLine, OnExpireLine(item->second));
315
316         item->second->DisplayExpiry();
317         item->second->Unset();
318
319         /* TODO: Can we skip this loop by having a 'pending' field in the XLine class, which is set when a line
320          * is pending, cleared when it is no longer pending, so we skip over this loop if its not pending?
321          * -- Brain
322          */
323         std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), item->second);
324         if (pptr != pending_lines.end())
325                 pending_lines.erase(pptr);
326
327         delete item->second;
328         container->second.erase(item);
329 }
330
331
332 // applies lines, removing clients and changing nicks etc as applicable
333 void XLineManager::ApplyLines()
334 {
335         for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
336         {
337                 User* u = (User*)(*u2);
338
339                 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
340                 {
341                         XLine *x = *i;
342                         if (x->Matches(u))
343                                 x->Apply(u);
344                 }
345         }
346
347         pending_lines.clear();
348 }
349
350 void XLineManager::InvokeStats(const std::string &type, int numeric, User* user, string_list &results)
351 {
352         std::string sn = ServerInstance->Config->ServerName;
353
354         ContainerIter n = lookup_lines.find(type);
355
356         time_t current = ServerInstance->Time();
357
358         LookupIter safei;
359
360         if (n != lookup_lines.end())
361         {
362                 XLineLookup& list = n->second;
363                 for (LookupIter i = list.begin(); i != list.end(); )
364                 {
365                         safei = i;
366                         safei++;
367
368                         if (i->second->duration && current > i->second->expiry)
369                         {
370                                 ExpireLine(n, i);
371                         }
372                         else
373                                 results.push_back(sn+" "+ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
374                                         ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+std::string(i->second->source)+" :"+(i->second->reason));
375                         i = safei;
376                 }
377         }
378 }
379
380
381 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)
382 {
383         GFact = new GLineFactory(Instance);
384         EFact = new ELineFactory(Instance);
385         KFact = new KLineFactory(Instance);
386         QFact = new QLineFactory(Instance);
387         ZFact = new ZLineFactory(Instance);
388
389         RegisterFactory(GFact);
390         RegisterFactory(EFact);
391         RegisterFactory(KFact);
392         RegisterFactory(QFact);
393         RegisterFactory(ZFact);
394 }
395
396 XLineManager::~XLineManager()
397 {
398         UnregisterFactory(GFact);
399         UnregisterFactory(EFact);
400         UnregisterFactory(KFact);
401         UnregisterFactory(QFact);
402         UnregisterFactory(ZFact);
403
404         delete GFact;
405         delete EFact;
406         delete KFact;
407         delete QFact;
408         delete ZFact;
409
410         // Delete all existing XLines
411         for (XLineContainer::iterator i = lookup_lines.begin(); i != lookup_lines.end(); i++)
412         {
413                 for (XLineLookup::iterator j = i->second.begin(); j != i->second.end(); j++)
414                 {
415                         delete j->second;
416                 }
417                 i->second.clear();
418         }
419         lookup_lines.clear();
420         
421 }
422
423 void XLine::Apply(User* u)
424 {
425 }
426
427 bool XLine::IsBurstable()
428 {
429         return true;
430 }
431
432 void XLine::DefaultApply(User* u, const std::string &line, bool bancache)
433 {
434         char sreason[MAXBUF];
435         snprintf(sreason, MAXBUF, "%s-Lined: %s", line.c_str(), this->reason);
436         if (*ServerInstance->Config->MoronBanner)
437                 u->WriteServ("NOTICE %s :*** %s", u->nick.c_str(), ServerInstance->Config->MoronBanner);
438
439         if (ServerInstance->Config->HideBans)
440                 ServerInstance->Users->QuitUser(u, line + "-Lined", sreason);
441         else
442                 ServerInstance->Users->QuitUser(u, sreason);
443
444
445         if (bancache)
446         {
447                 ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Adding positive hit (") + line + ") for " + u->GetIPString());
448                 if (this->duration > 0)
449                         ServerInstance->BanCache->AddHit(u->GetIPString(), this->type, line + "-Lined: " + this->reason, this->duration);
450                 else
451                         ServerInstance->BanCache->AddHit(u->GetIPString(), this->type, line + "-Lined: " + this->reason);
452         }
453 }
454
455 bool KLine::Matches(User *u)
456 {
457         if (u->exempt)
458                 return false;
459
460         if (InspIRCd::Match(u->ident, this->identmask))
461         {
462                 if (InspIRCd::MatchCIDR(u->host, this->hostmask) ||
463                     InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask))
464                 {
465                         return true;
466                 }
467         }
468
469         return false;
470 }
471
472 void KLine::Apply(User* u)
473 {
474         DefaultApply(u, "K", (strcmp(this->identmask, "*") == 0) ? true : false);
475 }
476
477 bool GLine::Matches(User *u)
478 {
479         if (u->exempt)
480                 return false;
481
482         if (InspIRCd::Match(u->ident, this->identmask))
483         {
484                 if (InspIRCd::MatchCIDR(u->host, this->hostmask) ||
485                     InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask))
486                 {
487                         return true;
488                 }
489         }
490
491         return false;
492 }
493
494 void GLine::Apply(User* u)
495 {       
496         DefaultApply(u, "G", (strcmp(this->identmask, "*") == 0) ? true : false);
497 }
498
499 bool ELine::Matches(User *u)
500 {
501         if (u->exempt)
502                 return false;
503
504         if (InspIRCd::Match(u->ident, this->identmask))
505         {
506                 if (InspIRCd::MatchCIDR(u->host, this->hostmask) ||
507                     InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask))
508                 {
509                         return true;
510                 }
511         }
512
513         return false;
514 }
515
516 bool ZLine::Matches(User *u)
517 {
518         if (u->exempt)
519                 return false;
520
521         if (InspIRCd::MatchCIDR(u->GetIPString(), this->ipaddr))
522                 return true;
523         else
524                 return false;
525 }
526
527 void ZLine::Apply(User* u)
528 {       
529         DefaultApply(u, "Z", true);
530 }
531
532
533 bool QLine::Matches(User *u)
534 {
535         if (InspIRCd::Match(u->nick, this->nick))
536                 return true;
537
538         return false;
539 }
540
541 void QLine::Apply(User* u)
542 {       
543         /* Force to uuid on apply of qline, no need to disconnect any more :) */
544         u->ForceNickChange(u->uuid.c_str());
545 }
546
547
548 bool ZLine::Matches(const std::string &str)
549 {
550         if (InspIRCd::MatchCIDR(str, this->ipaddr))
551                 return true;
552         else
553                 return false;
554 }
555
556 bool QLine::Matches(const std::string &str)
557 {
558         if (InspIRCd::Match(str, this->nick))
559                 return true;
560
561         return false;
562 }
563
564 bool ELine::Matches(const std::string &str)
565 {
566         return (InspIRCd::MatchCIDR(str, matchtext));
567 }
568
569 bool KLine::Matches(const std::string &str)
570 {
571         return (InspIRCd::MatchCIDR(str.c_str(), matchtext));
572 }
573
574 bool GLine::Matches(const std::string &str)
575 {
576         return (InspIRCd::MatchCIDR(str, matchtext));
577 }
578
579 void ELine::OnAdd()
580 {
581         /* When adding one eline, only check the one eline */
582         for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
583         {
584                 User* u = (User*)(*u2);
585                 if (this->Matches(u))
586                         u->exempt = true;
587         }
588 }
589
590 void ELine::DisplayExpiry()
591 {
592         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %ld seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
593 }
594
595 void QLine::DisplayExpiry()
596 {
597         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %ld seconds ago)",this->nick,this->source,this->duration);
598 }
599
600 void ZLine::DisplayExpiry()
601 {
602         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %ld seconds ago)",this->ipaddr,this->source,this->duration);
603 }
604
605 void KLine::DisplayExpiry()
606 {
607         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %ld seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
608 }
609
610 void GLine::DisplayExpiry()
611 {
612         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %ld seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
613 }
614
615 const char* ELine::Displayable()
616 {
617         return matchtext.c_str();
618 }
619
620 const char* KLine::Displayable()
621 {
622         return matchtext.c_str();
623 }
624
625 const char* GLine::Displayable()
626 {
627         return matchtext.c_str();
628 }
629
630 const char* ZLine::Displayable()
631 {
632         return ipaddr;
633 }
634
635 const char* QLine::Displayable()
636 {
637         return nick;
638 }
639
640 bool KLine::IsBurstable()
641 {
642         return false;
643 }
644
645 bool XLineManager::RegisterFactory(XLineFactory* xlf)
646 {
647         XLineFactMap::iterator n = line_factory.find(xlf->GetType());
648
649         if (n != line_factory.end())
650                 return false;
651
652         line_factory[xlf->GetType()] = xlf;
653
654         return true;
655 }
656
657 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
658 {
659         XLineFactMap::iterator n = line_factory.find(xlf->GetType());
660
661         if (n == line_factory.end())
662                 return false;
663
664         line_factory.erase(n);
665
666         return true;
667 }
668
669 XLineFactory* XLineManager::GetFactory(const std::string &type)
670 {
671         XLineFactMap::iterator n = line_factory.find(type);
672
673         if (n == line_factory.end())
674                 return NULL;
675
676         return n->second;
677 }
678