]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/xline.cpp
4bd3e51b587f85822410e645edc10f6aa9771446
[user/henk/code/inspircd.git] / src / xline.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 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         ServerInstance->BanCache->RemoveEntries(line->type, false); // XXX perhaps remove ELines here?
169
170         if (line->duration && ServerInstance->Time() > line->expiry)
171                 return false; // Don't apply expired XLines.
172
173         /* Don't apply duplicate xlines */
174         ContainerIter x = lookup_lines.find(line->type);
175         if (x != lookup_lines.end())
176         {
177                 return false;
178         }
179
180         /*ELine* item = new ELine(ServerInstance, ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());*/
181         XLineFactory* xlf = GetFactory(line->type);
182         if (!xlf)
183                 return false;
184
185         if (xlf->AutoApplyToUserList(line))
186                 pending_lines.push_back(line);
187
188         lookup_lines[line->type][line->Displayable()] = line;
189         line->OnAdd();
190
191         FOREACH_MOD(I_OnAddLine,OnAddLine(user, line));
192
193         return true;
194 }
195
196 // deletes a line, returns true if the line existed and was removed
197
198 bool XLineManager::DelLine(const char* hostmask, const std::string &type, User* user, bool simulate)
199 {
200         ContainerIter x = lookup_lines.find(type);
201
202         if (x == lookup_lines.end())
203                 return false;
204
205         LookupIter y = x->second.find(hostmask);
206
207         if (y == x->second.end())
208                 return false;
209
210         if (simulate)
211                 return true;
212
213         ServerInstance->BanCache->RemoveEntries(y->second->type, true);
214
215         FOREACH_MOD(I_OnDelLine,OnDelLine(user, y->second));
216
217         y->second->Unset();
218
219         std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), y->second);
220         if (pptr != pending_lines.end())
221                 pending_lines.erase(pptr);
222
223         delete y->second;
224         x->second.erase(y);
225
226         return true;
227 }
228
229
230 void ELine::Unset()
231 {
232         /* remove exempt from everyone and force recheck after deleting eline */
233         for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
234         {
235                 User* u = (User*)(*u2);
236                 u->exempt = false;
237         }
238
239         ServerInstance->XLines->CheckELines();
240 }
241
242 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
243
244 XLine* XLineManager::MatchesLine(const std::string &type, User* user)
245 {
246         ContainerIter x = lookup_lines.find(type);
247
248         if (x == lookup_lines.end())
249                 return NULL;
250
251         const time_t current = ServerInstance->Time();
252
253         LookupIter safei;
254
255         for (LookupIter i = x->second.begin(); i != x->second.end(); )
256         {
257                 safei = i;
258                 safei++;
259
260                 if (i->second->duration && current > i->second->expiry)
261                 {
262                         /* Expire the line, proceed to next one */
263                         ExpireLine(x, i);
264                         i = safei;
265                         continue;
266                 }
267
268                 if (i->second->Matches(user))
269                 {
270                         return i->second;
271                 }
272
273                 i = safei;
274         }
275         return NULL;
276 }
277
278 XLine* XLineManager::MatchesLine(const std::string &type, const std::string &pattern)
279 {
280         ContainerIter x = lookup_lines.find(type);
281
282         if (x == lookup_lines.end())
283                 return NULL;
284
285         const time_t current = ServerInstance->Time();
286
287          LookupIter safei;
288
289         for (LookupIter i = x->second.begin(); i != x->second.end(); )
290         {
291                 safei = i;
292                 safei++;
293
294                 if (i->second->Matches(pattern))
295                 {
296                         if (i->second->duration && current > i->second->expiry)
297                         {
298                                 /* Expire the line, return nothing */
299                                 ExpireLine(x, i);
300                                 /* See above */
301                                 i = safei;
302                                 continue;
303                         }
304                         else
305                                 return i->second;
306                 }
307
308                 i = safei;
309         }
310         return NULL;
311 }
312
313 // removes lines that have expired
314 void XLineManager::ExpireLine(ContainerIter container, LookupIter item)
315 {
316         FOREACH_MOD(I_OnExpireLine, OnExpireLine(item->second));
317
318         item->second->DisplayExpiry();
319         item->second->Unset();
320
321         /* TODO: Can we skip this loop by having a 'pending' field in the XLine class, which is set when a line
322          * is pending, cleared when it is no longer pending, so we skip over this loop if its not pending?
323          * -- Brain
324          */
325         std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), item->second);
326         if (pptr != pending_lines.end())
327                 pending_lines.erase(pptr);
328
329         delete item->second;
330         container->second.erase(item);
331 }
332
333
334 // applies lines, removing clients and changing nicks etc as applicable
335 void XLineManager::ApplyLines()
336 {
337         for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
338         {
339                 User* u = (User*)(*u2);
340
341                 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
342                 {
343                         XLine *x = *i;
344                         if (x->Matches(u))
345                                 x->Apply(u);
346                 }
347         }
348
349         pending_lines.clear();
350 }
351
352 void XLineManager::InvokeStats(const std::string &type, int numeric, User* user, string_list &results)
353 {
354         std::string sn = ServerInstance->Config->ServerName;
355
356         ContainerIter n = lookup_lines.find(type);
357
358         time_t current = ServerInstance->Time();
359
360         LookupIter safei;
361
362         if (n != lookup_lines.end())
363         {
364                 XLineLookup& list = n->second;
365                 for (LookupIter i = list.begin(); i != list.end(); )
366                 {
367                         safei = i;
368                         safei++;
369
370                         if (i->second->duration && current > i->second->expiry)
371                         {
372                                 ExpireLine(n, i);
373                         }
374                         else
375                                 results.push_back(sn+" "+ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
376                                         ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+std::string(i->second->source)+" :"+(i->second->reason));
377                         i = safei;
378                 }
379         }
380 }
381
382
383 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)
384 {
385         GFact = new GLineFactory(Instance);
386         EFact = new ELineFactory(Instance);
387         KFact = new KLineFactory(Instance);
388         QFact = new QLineFactory(Instance);
389         ZFact = new ZLineFactory(Instance);
390
391         RegisterFactory(GFact);
392         RegisterFactory(EFact);
393         RegisterFactory(KFact);
394         RegisterFactory(QFact);
395         RegisterFactory(ZFact);
396 }
397
398 XLineManager::~XLineManager()
399 {
400         UnregisterFactory(GFact);
401         UnregisterFactory(EFact);
402         UnregisterFactory(KFact);
403         UnregisterFactory(QFact);
404         UnregisterFactory(ZFact);
405
406         delete GFact;
407         delete EFact;
408         delete KFact;
409         delete QFact;
410         delete ZFact;
411
412         // Delete all existing XLines
413         for (XLineContainer::iterator i = lookup_lines.begin(); i != lookup_lines.end(); i++)
414         {
415                 for (XLineLookup::iterator j = i->second.begin(); j != i->second.end(); j++)
416                 {
417                         delete j->second;
418                 }
419                 i->second.clear();
420         }
421         lookup_lines.clear();
422
423 }
424
425 void XLine::Apply(User* u)
426 {
427 }
428
429 bool XLine::IsBurstable()
430 {
431         return true;
432 }
433
434 void XLine::DefaultApply(User* u, const std::string &line, bool bancache)
435 {
436         char sreason[MAXBUF];
437         snprintf(sreason, MAXBUF, "%s-Lined: %s", line.c_str(), this->reason);
438         if (*ServerInstance->Config->MoronBanner)
439                 u->WriteServ("NOTICE %s :*** %s", u->nick.c_str(), ServerInstance->Config->MoronBanner);
440
441         if (ServerInstance->Config->HideBans)
442                 ServerInstance->Users->QuitUser(u, line + "-Lined", sreason);
443         else
444                 ServerInstance->Users->QuitUser(u, sreason);
445
446
447         if (bancache)
448         {
449                 ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Adding positive hit (") + line + ") for " + u->GetIPString());
450                 if (this->duration > 0)
451                         ServerInstance->BanCache->AddHit(u->GetIPString(), this->type, line + "-Lined: " + this->reason, this->duration);
452                 else
453                         ServerInstance->BanCache->AddHit(u->GetIPString(), this->type, line + "-Lined: " + this->reason);
454         }
455 }
456
457 bool KLine::Matches(User *u)
458 {
459         if (u->exempt)
460                 return false;
461
462         if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
463         {
464                 if (InspIRCd::MatchCIDR(u->host, this->hostmask, ascii_case_insensitive_map) ||
465                     InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
466                 {
467                         return true;
468                 }
469         }
470
471         return false;
472 }
473
474 void KLine::Apply(User* u)
475 {
476         DefaultApply(u, "K", (strcmp(this->identmask, "*") == 0) ? true : false);
477 }
478
479 bool GLine::Matches(User *u)
480 {
481         if (u->exempt)
482                 return false;
483
484         if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
485         {
486                 if (InspIRCd::MatchCIDR(u->host, this->hostmask, ascii_case_insensitive_map) ||
487                     InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
488                 {
489                         return true;
490                 }
491         }
492
493         return false;
494 }
495
496 void GLine::Apply(User* u)
497 {
498         DefaultApply(u, "G", (strcmp(this->identmask, "*") == 0) ? true : false);
499 }
500
501 bool ELine::Matches(User *u)
502 {
503         if (u->exempt)
504                 return false;
505
506         if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
507         {
508                 if (InspIRCd::MatchCIDR(u->host, this->hostmask, ascii_case_insensitive_map) ||
509                     InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
510                 {
511                         return true;
512                 }
513         }
514
515         return false;
516 }
517
518 bool ZLine::Matches(User *u)
519 {
520         if (u->exempt)
521                 return false;
522
523         if (InspIRCd::MatchCIDR(u->GetIPString(), this->ipaddr))
524                 return true;
525         else
526                 return false;
527 }
528
529 void ZLine::Apply(User* u)
530 {
531         DefaultApply(u, "Z", true);
532 }
533
534
535 bool QLine::Matches(User *u)
536 {
537         if (InspIRCd::Match(u->nick, this->nick))
538                 return true;
539
540         return false;
541 }
542
543 void QLine::Apply(User* u)
544 {
545         /* Force to uuid on apply of qline, no need to disconnect any more :) */
546         u->ForceNickChange(u->uuid.c_str());
547 }
548
549
550 bool ZLine::Matches(const std::string &str)
551 {
552         if (InspIRCd::MatchCIDR(str, this->ipaddr))
553                 return true;
554         else
555                 return false;
556 }
557
558 bool QLine::Matches(const std::string &str)
559 {
560         if (InspIRCd::Match(str, this->nick))
561                 return true;
562
563         return false;
564 }
565
566 bool ELine::Matches(const std::string &str)
567 {
568         return (InspIRCd::MatchCIDR(str, matchtext));
569 }
570
571 bool KLine::Matches(const std::string &str)
572 {
573         return (InspIRCd::MatchCIDR(str.c_str(), matchtext));
574 }
575
576 bool GLine::Matches(const std::string &str)
577 {
578         return (InspIRCd::MatchCIDR(str, matchtext));
579 }
580
581 void ELine::OnAdd()
582 {
583         /* When adding one eline, only check the one eline */
584         for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
585         {
586                 User* u = (User*)(*u2);
587                 if (this->Matches(u))
588                         u->exempt = true;
589         }
590 }
591
592 void ELine::DisplayExpiry()
593 {
594         ServerInstance->SNO->WriteToSnoMask('x',"Removing expired E-Line %s@%s (set by %s %ld seconds ago)",this->identmask,this->hostmask,this->source,(long int)(ServerInstance->Time() - this->set_time));
595 }
596
597 void QLine::DisplayExpiry()
598 {
599         ServerInstance->SNO->WriteToSnoMask('x',"Removing expired Q-Line %s (set by %s %ld seconds ago)",this->nick,this->source,(long int)(ServerInstance->Time() - this->set_time));
600 }
601
602 void ZLine::DisplayExpiry()
603 {
604         ServerInstance->SNO->WriteToSnoMask('x',"Removing expired Z-Line %s (set by %s %ld seconds ago)",this->ipaddr,this->source,(long int)(ServerInstance->Time() - this->set_time));
605 }
606
607 void KLine::DisplayExpiry()
608 {
609         ServerInstance->SNO->WriteToSnoMask('x',"Removing expired K-Line %s@%s (set by %s %ld seconds ago)",this->identmask,this->hostmask,this->source,(long int)(ServerInstance->Time() - this->set_time));
610 }
611
612 void GLine::DisplayExpiry()
613 {
614         ServerInstance->SNO->WriteToSnoMask('x',"Removing expired G-Line %s@%s (set by %s %ld seconds ago)",this->identmask,this->hostmask,this->source,(long int)(ServerInstance->Time() - this->set_time));
615 }
616
617 const char* ELine::Displayable()
618 {
619         return matchtext.c_str();
620 }
621
622 const char* KLine::Displayable()
623 {
624         return matchtext.c_str();
625 }
626
627 const char* GLine::Displayable()
628 {
629         return matchtext.c_str();
630 }
631
632 const char* ZLine::Displayable()
633 {
634         return ipaddr;
635 }
636
637 const char* QLine::Displayable()
638 {
639         return nick;
640 }
641
642 bool KLine::IsBurstable()
643 {
644         return false;
645 }
646
647 bool XLineManager::RegisterFactory(XLineFactory* xlf)
648 {
649         XLineFactMap::iterator n = line_factory.find(xlf->GetType());
650
651         if (n != line_factory.end())
652                 return false;
653
654         line_factory[xlf->GetType()] = xlf;
655
656         return true;
657 }
658
659 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
660 {
661         XLineFactMap::iterator n = line_factory.find(xlf->GetType());
662
663         if (n == line_factory.end())
664                 return false;
665
666         line_factory.erase(n);
667
668         return true;
669 }
670
671 XLineFactory* XLineManager::GetFactory(const std::string &type)
672 {
673         XLineFactMap::iterator n = line_factory.find(type);
674
675         if (n == line_factory.end())
676                 return NULL;
677
678         return n->second;
679 }