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