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