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