]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/xline.cpp
- Tear out a useless load of XLine clutters that did nothing much except confuse...
[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
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. Expiry will
40  *  still use a sorted list, and we'll just ignore anything permanent.
41  *
42  *  Application will be by a list of lines 'pending' application, meaning only the newly added lines
43  *  will be gone over. Much faster.
44  *
45  * More of course is to come.
46  */
47
48 /* Version two, now with optimized expiry!
49  *
50  * Because the old way was horrendously slow, the new way of expiring xlines is very
51  * very efficient. I have improved the efficiency of the algorithm in two ways:
52  *
53  * (1) There are now two lists of items for each linetype. One list holds temporary
54  *     items, and the other list holds permanent items (ones which will expire).
55  *     Items which are on the permanent list are NEVER checked at all by the
56  *     expire_lines() function.
57  * (2) The temporary xline lists are always kept in strict numerical order, keyed by
58  *     current time + duration. This means that the line which is due to expire the
59  *     soonest is always pointed at by vector::begin(), so a simple while loop can
60  *     very efficiently, very quickly and above all SAFELY pick off the first few
61  *     items in the vector which need zapping.
62  *
63  *     -- Brain
64  */
65
66 bool InitXLine(ServerConfig* conf, const char* tag)
67 {
68         return true;
69 }
70
71 bool DoneZLine(ServerConfig* conf, const char* tag)
72 {
73         // XXX we should really only call this once - after we've finished processing configuration all together
74         conf->GetInstance()->XLines->apply_lines();
75         return true;
76 }
77
78 bool DoneQLine(ServerConfig* conf, const char* tag)
79 {
80         // XXX we should really only call this once - after we've finished processing configuration all together
81         conf->GetInstance()->XLines->apply_lines();
82         return true;
83 }
84
85 bool DoneKLine(ServerConfig* conf, const char* tag)
86 {
87         // XXX we should really only call this once - after we've finished processing configuration all together
88         conf->GetInstance()->XLines->apply_lines();
89         return true;
90 }
91
92 bool DoneELine(ServerConfig* conf, const char* tag)
93 {
94         // XXX we should really only call this once - after we've finished processing configuration all together
95         conf->GetInstance()->XLines->apply_lines();
96         return true;
97 }
98
99 bool DoZLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
100 {
101         const char* reason = values[0].GetString();
102         const char* ipmask = values[1].GetString();
103
104         conf->GetInstance()->XLines->add_zline(0,"<Config>",reason,ipmask);
105         return true;
106 }
107
108 bool DoQLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
109 {
110         const char* reason = values[0].GetString();
111         const char* nick = values[1].GetString();
112
113         conf->GetInstance()->XLines->add_qline(0,"<Config>",reason,nick);
114         return true;
115 }
116
117 bool DoKLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
118 {
119         const char* reason = values[0].GetString();
120         const char* host = values[1].GetString();
121
122         conf->GetInstance()->XLines->add_kline(0,"<Config>",reason,host);
123         return true;
124 }
125
126 bool DoELine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
127 {
128         const char* reason = values[0].GetString();
129         const char* host = values[1].GetString();
130
131         conf->GetInstance()->XLines->add_eline(0,"<Config>",reason,host);
132         return true;
133 }
134
135 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
136 {
137         IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
138         std::string::size_type x = ident_and_host.find('@');
139         if (x != std::string::npos)
140         {
141                 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
142                 n.first = ident_and_host.substr(0, x);
143                 if (!n.first.length())
144                         n.first.assign("*");
145                 if (!n.second.length())
146                         n.second.assign("*");
147         }
148         else
149         {
150                 n.second = ident_and_host;
151         }
152
153         return n;
154 }
155
156 // adds a g:line
157
158 bool XLineManager::add_gline(long duration, const char* source,const char* reason,const char* hostmask)
159 {
160         IdentHostPair ih = IdentSplit(hostmask);
161
162         if (del_gline(hostmask, true))
163                 return false;
164
165         GLine* item = new GLine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
166
167         glines.push_back(item);
168         sort(glines.begin(), glines.end(),XLineManager::XSortComparison);
169
170         return true;
171 }
172
173 // adds an e:line (exception to bans)
174
175 bool XLineManager::add_eline(long duration, const char* source, const char* reason, const char* hostmask)
176 {
177         IdentHostPair ih = IdentSplit(hostmask);
178
179         if (del_eline(hostmask, true))
180                 return false;
181
182         ELine* item = new ELine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
183
184         elines.push_back(item);
185         sort(elines.begin(), elines.end(),XLineManager::XSortComparison);
186
187         return true;
188 }
189
190 // adds a q:line
191
192 bool XLineManager::add_qline(long duration, const char* source, const char* reason, const char* nickname)
193 {
194         if (del_qline(nickname, true))
195                 return false;
196
197         QLine* item = new QLine(ServerInstance->Time(), duration, source, reason, nickname);
198
199         qlines.push_back(item);
200         sort(qlines.begin(), qlines.end(),XLineManager::XSortComparison);
201
202         return true;
203 }
204
205 // adds a z:line
206
207 bool XLineManager::add_zline(long duration, const char* source, const char* reason, const char* ipaddr)
208 {
209         if (strchr(ipaddr,'@'))
210         {
211                 while (*ipaddr != '@')
212                         ipaddr++;
213                 ipaddr++;
214         }
215
216         if (del_zline(ipaddr, true))
217                 return false;
218
219         ZLine* item = new ZLine(ServerInstance->Time(), duration, source, reason, ipaddr);
220
221         zlines.push_back(item);
222         sort(zlines.begin(), zlines.end(),XLineManager::XSortComparison);
223
224         return true;
225 }
226
227 // adds a k:line
228
229 bool XLineManager::add_kline(long duration, const char* source, const char* reason, const char* hostmask)
230 {
231         IdentHostPair ih = IdentSplit(hostmask);
232
233         if (del_kline(hostmask, true))
234                 return false;
235
236         KLine* item = new KLine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
237
238         klines.push_back(item);
239         sort(klines.begin(), klines.end(),XLineManager::XSortComparison);
240
241         return true;
242 }
243
244 // deletes a g:line, returns true if the line existed and was removed
245
246 bool XLineManager::del_gline(const char* hostmask, bool simulate)
247 {
248         IdentHostPair ih = IdentSplit(hostmask);
249         for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
250         {
251                 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
252                 {
253                         if (!simulate)
254                         {
255                                 delete *i;
256                                 glines.erase(i);
257                         }
258                         return true;
259                 }
260         }
261
262         return false;
263 }
264
265 // deletes a e:line, returns true if the line existed and was removed
266
267 bool XLineManager::del_eline(const char* hostmask, bool simulate)
268 {
269         IdentHostPair ih = IdentSplit(hostmask);
270         for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
271         {
272                 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
273                 {
274                         if (!simulate)
275                         {
276                                 delete *i;
277                                 elines.erase(i);
278                         }
279                         return true;
280                 }
281         }
282
283         return false;
284 }
285
286 // deletes a q:line, returns true if the line existed and was removed
287
288 bool XLineManager::del_qline(const char* nickname, bool simulate)
289 {
290         for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
291         {
292                 if (!strcasecmp(nickname,(*i)->nick))
293                 {
294                         if (!simulate)
295                         {
296                                 delete *i;
297                                 qlines.erase(i);
298                         }
299                         return true;
300                 }
301         }
302
303         return false;
304 }
305
306 // deletes a z:line, returns true if the line existed and was removed
307
308 bool XLineManager::del_zline(const char* ipaddr, bool simulate)
309 {
310         for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
311         {
312                 if (!strcasecmp(ipaddr,(*i)->ipaddr))
313                 {
314                         if (!simulate)
315                         {
316                                 delete *i;
317                                 zlines.erase(i);
318                         }
319                         return true;
320                 }
321         }
322
323         return false;
324 }
325
326 // deletes a k:line, returns true if the line existed and was removed
327
328 bool XLineManager::del_kline(const char* hostmask, bool simulate)
329 {
330         IdentHostPair ih = IdentSplit(hostmask);
331         for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
332         {
333                 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
334                 {
335                         if (!simulate)
336                         {
337                                 delete *i;
338                                 klines.erase(i);
339                         }
340                         return true;
341                 }
342         }
343
344         return false;
345 }
346
347 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
348
349 QLine* XLineManager::matches_qline(const char* nick)
350 {
351         if (qlines.empty())
352                 return NULL;
353
354         for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
355                 if (match(nick,(*i)->nick))
356                         return (*i);
357         return NULL;
358 }
359
360 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
361
362 GLine* XLineManager::matches_gline(User* user)
363 {
364         if (glines.empty())
365                 return NULL;
366
367         for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
368         {
369                 if ((match(user->ident,(*i)->identmask)))
370                 {
371                         if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
372                         {
373                                 return (*i);
374                         }
375                 }
376         }
377
378         return NULL;
379 }
380
381 ELine* XLineManager::matches_exception(User* user)
382 {
383         if (elines.empty())
384                 return NULL;
385         char host2[MAXBUF];
386
387         snprintf(host2,MAXBUF,"*@%s",user->host);
388         for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
389         {
390                 if ((match(user->ident,(*i)->identmask)))
391                 {
392                         if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
393                         {
394                                 return (*i);
395                         }
396                 }
397         }
398         return NULL;
399 }
400
401
402 void XLineManager::gline_set_creation_time(const char* host, time_t create_time)
403 {
404         for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
405         {
406                 if (!strcasecmp(host,(*i)->hostmask))
407                 {
408                         (*i)->set_time = create_time;
409                         (*i)->expiry = create_time + (*i)->duration;
410                         return;
411                 }
412         }
413
414         return ;
415 }
416
417 void XLineManager::eline_set_creation_time(const char* host, time_t create_time)
418 {
419         for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
420         {
421                 if (!strcasecmp(host,(*i)->hostmask))
422                 {
423                         (*i)->set_time = create_time;
424                         (*i)->expiry = create_time + (*i)->duration;
425                         return;
426                 }
427         }
428
429         return;
430 }
431
432 void XLineManager::qline_set_creation_time(const char* nick, time_t create_time)
433 {
434         for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
435         {
436                 if (!strcasecmp(nick,(*i)->nick))
437                 {
438                         (*i)->set_time = create_time;
439                         (*i)->expiry = create_time + (*i)->duration;
440                         return;
441                 }
442         }
443
444         return;
445 }
446
447 void XLineManager::zline_set_creation_time(const char* ip, time_t create_time)
448 {
449         for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
450         {
451                 if (!strcasecmp(ip,(*i)->ipaddr))
452                 {
453                         (*i)->set_time = create_time;
454                         (*i)->expiry = create_time + (*i)->duration;
455                         return;
456                 }
457         }
458
459         return;
460 }
461
462 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
463
464 ZLine* XLineManager::matches_zline(const char* ipaddr)
465 {
466         if (zlines.empty())
467                 return NULL;
468
469         for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
470                 if (match(ipaddr,(*i)->ipaddr, true))
471                         return (*i);
472         return NULL;
473 }
474
475 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
476
477 KLine* XLineManager::matches_kline(User* user)
478 {
479         if (klines.empty())
480                 return NULL;
481
482         for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
483         {
484                 if ((match(user->ident,(*i)->identmask)))
485                 {
486                         if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
487                         {
488                                 return (*i);
489                         }
490                 }
491         }
492
493         return NULL;
494 }
495
496 bool XLineManager::XSortComparison(const XLine *one, const XLine *two)
497 {
498         // account for permanent lines
499         if (one->expiry == 0)
500         {
501                 return false;
502         }
503         return (one->expiry) < (two->expiry);
504 }
505
506 // removes lines that have expired
507 void XLineManager::expire_lines()
508 {
509         time_t current = ServerInstance->Time();
510
511         /* Because we now store all our XLines in sorted order using ((*i)->duration + (*i)->set_time) as a key, this
512          * means that to expire the XLines we just need to do a while, picking off the top few until there are
513          * none left at the head of the queue that are after the current time.
514          */
515
516         while ((glines.size()) && (current > (*glines.begin())->expiry) && ((*glines.begin())->duration != 0))
517         {
518                 std::vector<GLine*>::iterator i = glines.begin();
519                 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",(*i)->identmask,(*i)->hostmask,(*i)->source,(*i)->duration);
520                 glines.erase(i);
521         }
522
523         while ((elines.size()) && (current > (*elines.begin())->expiry) && ((*elines.begin())->duration != 0))
524         {
525                 std::vector<ELine*>::iterator i = elines.begin();
526                 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",(*i)->identmask,(*i)->hostmask,(*i)->source,(*i)->duration);
527                 elines.erase(i);
528         }
529
530         while ((zlines.size()) && (current > (*zlines.begin())->expiry) && ((*zlines.begin())->duration != 0))
531         {
532                 std::vector<ZLine*>::iterator i = zlines.begin();
533                 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",(*i)->ipaddr,(*i)->source,(*i)->duration);
534                 zlines.erase(i);
535         }
536
537         while ((klines.size()) && (current > (*klines.begin())->expiry) && ((*klines.begin())->duration != 0))
538         {
539                 std::vector<KLine*>::iterator i = klines.begin();
540                 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",(*i)->identmask,(*i)->hostmask,(*i)->source,(*i)->duration);
541                 klines.erase(i);
542         }
543
544         while ((qlines.size()) && (current > (*qlines.begin())->expiry) && ((*qlines.begin())->duration != 0))
545         {
546                 std::vector<QLine*>::iterator i = qlines.begin();
547                 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",(*i)->nick,(*i)->source,(*i)->duration);
548                 qlines.erase(i);
549         }
550
551 }
552
553 // applies lines, removing clients and changing nicks etc as applicable
554
555 void XLineManager::apply_lines()
556 {
557         int What = 0; // XXX remove me
558         char reason[MAXBUF];
559
560         XLine* check = NULL;
561         for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
562         {
563                 User* u = (User*)(*u2);
564
565                 if (elines.size())
566                 {
567                         // ignore people matching exempts
568                         if (matches_exception(u))
569                                 continue;
570                 }
571                 if ((What & APPLY_GLINES) && (glines.size()))
572                 {
573                         if ((check = matches_gline(u)))
574                         {
575                                 snprintf(reason,MAXBUF,"G-Lined: %s",check->reason);
576                                 if (*ServerInstance->Config->MoronBanner)
577                                         u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
578                                 if (ServerInstance->Config->HideBans)
579                                         User::QuitUser(ServerInstance, u, "G-Lined", reason);
580                                 else
581                                         User::QuitUser(ServerInstance, u, reason);
582                         }
583                 }
584                 if ((What & APPLY_KLINES) && (klines.size()))
585                 {
586                         if ((check = matches_kline(u)))
587                         {
588                                 snprintf(reason,MAXBUF,"K-Lined: %s",check->reason);
589                                 if (*ServerInstance->Config->MoronBanner)
590                                         u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
591                                 if (ServerInstance->Config->HideBans)
592                                         User::QuitUser(ServerInstance, u, "K-Lined", reason);
593                                 else
594                                         User::QuitUser(ServerInstance, u, reason);
595                         }
596                 }
597                 if ((What & APPLY_QLINES) && (qlines.size()))
598                 {
599                         if ((check = matches_qline(u->nick)))
600                         {
601                                 snprintf(reason,MAXBUF,"Q-Lined: %s",check->reason);
602                                 if (*ServerInstance->Config->MoronBanner)
603                                         u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
604                                 if (ServerInstance->Config->HideBans)
605                                         User::QuitUser(ServerInstance, u, "Q-Lined", reason);
606                                 else
607                                         User::QuitUser(ServerInstance, u, reason);
608                         }
609                 }
610                 if ((What & APPLY_ZLINES) && (zlines.size()))
611                 {
612                         if ((check = matches_zline(u->GetIPString())))
613                         {
614                                 snprintf(reason,MAXBUF,"Z-Lined: %s", check->reason);
615                                 if (*ServerInstance->Config->MoronBanner)
616                                         u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
617                                 if (ServerInstance->Config->HideBans)
618                                         User::QuitUser(ServerInstance, u, "Z-Lined", reason);
619                                 else
620                                         User::QuitUser(ServerInstance, u, reason);
621                         }
622                 }
623         }
624 }
625
626 void XLineManager::stats_k(User* user, string_list &results)
627 {
628         std::string sn = ServerInstance->Config->ServerName;
629         for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
630                 results.push_back(sn+" 216 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
631 }
632
633 void XLineManager::stats_g(User* user, string_list &results)
634 {
635         std::string sn = ServerInstance->Config->ServerName;
636         for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
637                 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
638 }
639
640 void XLineManager::stats_q(User* user, string_list &results)
641 {
642         std::string sn = ServerInstance->Config->ServerName;
643         for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
644                 results.push_back(sn+" 217 "+user->nick+" :"+(*i)->nick+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
645 }
646
647 void XLineManager::stats_z(User* user, string_list &results)
648 {
649         std::string sn = ServerInstance->Config->ServerName;
650         for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
651                 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->ipaddr+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
652 }
653
654 void XLineManager::stats_e(User* user, string_list &results)
655 {
656         std::string sn = ServerInstance->Config->ServerName;
657         for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
658                 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
659 }
660
661 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)
662 {
663 }