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