1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
19 #include "inspircd_config.h"
34 #include "inspstring.h"
35 #include "helperfuncs.h"
38 #include "configreader.h"
39 #include "cull_list.h"
41 extern InspIRCd* ServerInstance;
43 /* Version two, now with optimized expiry!
45 * Because the old way was horrendously slow, the new way of expiring xlines is very
46 * very efficient. I have improved the efficiency of the algorithm in two ways:
48 * (1) There are now two lists of items for each linetype. One list holds temporary
49 * items, and the other list holds permanent items (ones which will expire).
50 * Items which are on the permanent list are NEVER checked at all by the
51 * expire_lines() function.
52 * (2) The temporary xline lists are always kept in strict numerical order, keyed by
53 * current time + duration. This means that the line which is due to expire the
54 * soonest is always pointed at by vector::begin(), so a simple while loop can
55 * very efficiently, very quickly and above all SAFELY pick off the first few
56 * items in the vector which need zapping.
65 /* Lists for temporary lines with an expiry time */
67 std::vector<KLine> klines;
68 std::vector<GLine> glines;
69 std::vector<ZLine> zlines;
70 std::vector<QLine> qlines;
71 std::vector<ELine> elines;
73 /* Seperate lists for perm XLines that isnt checked by expiry functions */
75 std::vector<KLine> pklines;
76 std::vector<GLine> pglines;
77 std::vector<ZLine> pzlines;
78 std::vector<QLine> pqlines;
79 std::vector<ELine> pelines;
82 bool GSortComparison ( const GLine one, const GLine two );
83 bool ZSortComparison ( const ZLine one, const ZLine two );
84 bool ESortComparison ( const ELine one, const ELine two );
85 bool QSortComparison ( const QLine one, const QLine two );
86 bool KSortComparison ( const KLine one, const KLine two );
88 // Reads the default bans from the config file.
89 // only a very small number of bans are defined
90 // this way these days, such as qlines against
91 // services nicks, etc.
93 bool InitXLine(ServerConfig* conf, const char* tag)
98 bool DoneXLine(ServerConfig* conf, const char* tag)
100 apply_lines(APPLY_ALL);
104 bool DoZLine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
106 char* reason = (char*)values[0];
107 char* ipmask = (char*)values[1];
109 add_zline(0,"<Config>",reason,ipmask);
110 log(DEBUG,"Read Z line (badip tag): ipmask=%s reason=%s",ipmask,reason);
114 bool DoQLine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
116 char* reason = (char*)values[0];
117 char* nick = (char*)values[1];
119 add_qline(0,"<Config>",reason,nick);
120 log(DEBUG,"Read Q line (badnick tag): nick=%s reason=%s",nick,reason);
124 bool DoKLine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
126 char* reason = (char*)values[0];
127 char* host = (char*)values[1];
129 add_kline(0,"<Config>",reason,host);
130 log(DEBUG,"Read K line (badhost tag): host=%s reason=%s",host,reason);
134 bool DoELine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
136 char* reason = (char*)values[0];
137 char* host = (char*)values[1];
139 add_eline(0,"<Config>",reason,host);
140 log(DEBUG,"Read E line (exception tag): host=%s reason=%s",host,reason);
146 bool add_gline(long duration, const char* source,const char* reason,const char* hostmask)
148 bool ret = del_gline(hostmask);
151 item.duration = duration;
152 strlcpy(item.hostmask,hostmask,199);
153 strlcpy(item.reason,reason,MAXBUF);
154 strlcpy(item.source,source,255);
156 item.set_time = TIME;
160 glines.push_back(item);
161 sort(glines.begin(), glines.end(),GSortComparison);
165 pglines.push_back(item);
171 // adds an e:line (exception to bans)
173 bool add_eline(long duration, const char* source, const char* reason, const char* hostmask)
175 bool ret = del_eline(hostmask);
177 item.duration = duration;
178 strlcpy(item.hostmask,hostmask,199);
179 strlcpy(item.reason,reason,MAXBUF);
180 strlcpy(item.source,source,255);
182 item.set_time = TIME;
185 elines.push_back(item);
186 sort(elines.begin(), elines.end(),ESortComparison);
190 pelines.push_back(item);
197 bool add_qline(long duration, const char* source, const char* reason, const char* nickname)
199 bool ret = del_qline(nickname);
201 item.duration = duration;
202 strlcpy(item.nick,nickname,63);
203 strlcpy(item.reason,reason,MAXBUF);
204 strlcpy(item.source,source,255);
206 item.is_global = false;
207 item.set_time = TIME;
210 qlines.push_back(item);
211 sort(qlines.begin(), qlines.end(),QSortComparison);
215 pqlines.push_back(item);
222 bool add_zline(long duration, const char* source, const char* reason, const char* ipaddr)
224 bool ret = del_zline(ipaddr);
226 item.duration = duration;
227 if (strchr(ipaddr,'@'))
229 while (*ipaddr != '@')
233 strlcpy(item.ipaddr,ipaddr,39);
234 strlcpy(item.reason,reason,MAXBUF);
235 strlcpy(item.source,source,255);
237 item.is_global = false;
238 item.set_time = TIME;
241 zlines.push_back(item);
242 sort(zlines.begin(), zlines.end(),ZSortComparison);
246 pzlines.push_back(item);
253 bool add_kline(long duration, const char* source, const char* reason, const char* hostmask)
255 bool ret = del_kline(hostmask);
257 item.duration = duration;
258 strlcpy(item.hostmask,hostmask,200);
259 strlcpy(item.reason,reason,MAXBUF);
260 strlcpy(item.source,source,255);
262 item.set_time = TIME;
265 klines.push_back(item);
266 sort(klines.begin(), klines.end(),KSortComparison);
270 pklines.push_back(item);
275 // deletes a g:line, returns true if the line existed and was removed
277 bool del_gline(const char* hostmask)
279 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
281 if (!strcasecmp(hostmask,i->hostmask))
287 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
289 if (!strcasecmp(hostmask,i->hostmask))
298 // deletes a e:line, returns true if the line existed and was removed
300 bool del_eline(const char* hostmask)
302 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
304 if (!strcasecmp(hostmask,i->hostmask))
310 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
312 if (!strcasecmp(hostmask,i->hostmask))
321 // deletes a q:line, returns true if the line existed and was removed
323 bool del_qline(const char* nickname)
325 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
327 if (!strcasecmp(nickname,i->nick))
333 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
335 if (!strcasecmp(nickname,i->nick))
344 bool qline_make_global(const char* nickname)
346 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
348 if (!strcasecmp(nickname,i->nick))
357 bool zline_make_global(const char* ipaddr)
359 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
361 if (!strcasecmp(ipaddr,i->ipaddr))
370 // deletes a z:line, returns true if the line existed and was removed
372 bool del_zline(const char* ipaddr)
374 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
376 if (!strcasecmp(ipaddr,i->ipaddr))
382 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
384 if (!strcasecmp(ipaddr,i->ipaddr))
393 // deletes a k:line, returns true if the line existed and was removed
395 bool del_kline(const char* hostmask)
397 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
399 if (!strcasecmp(hostmask,i->hostmask))
405 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
407 if (!strcasecmp(hostmask,i->hostmask))
416 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
418 char* matches_qline(const char* nick)
420 if ((qlines.empty()) && (pqlines.empty()))
422 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
423 if (match(nick,i->nick))
425 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
426 if (match(nick,i->nick))
431 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
433 char* matches_gline(const char* host)
435 if ((glines.empty()) && (pglines.empty()))
437 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
438 if (match(host,i->hostmask, true))
440 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
441 if (match(host,i->hostmask, true))
446 char* matches_exception(const char* host)
448 if ((elines.empty()) && (pelines.empty()))
451 snprintf(host2,MAXBUF,"*@%s",host);
452 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
453 if ((match(host,i->hostmask)) || (match(host2,i->hostmask, true)))
455 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
456 if ((match(host,i->hostmask)) || (match(host2,i->hostmask, true)))
462 void gline_set_creation_time(const char* host, time_t create_time)
464 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
466 if (!strcasecmp(host,i->hostmask))
468 i->set_time = create_time;
472 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
474 if (!strcasecmp(host,i->hostmask))
476 i->set_time = create_time;
483 void eline_set_creation_time(const char* host, time_t create_time)
485 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
487 if (!strcasecmp(host,i->hostmask))
489 i->set_time = create_time;
493 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
495 if (!strcasecmp(host,i->hostmask))
497 i->set_time = create_time;
504 void qline_set_creation_time(const char* nick, time_t create_time)
506 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
508 if (!strcasecmp(nick,i->nick))
510 i->set_time = create_time;
514 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
516 if (!strcasecmp(nick,i->nick))
518 i->set_time = create_time;
525 void zline_set_creation_time(const char* ip, time_t create_time)
527 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
529 if (!strcasecmp(ip,i->ipaddr))
531 i->set_time = create_time;
535 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
537 if (!strcasecmp(ip,i->ipaddr))
539 i->set_time = create_time;
546 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
548 char* matches_zline(const char* ipaddr)
550 if ((zlines.empty()) && (pzlines.empty()))
552 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
553 if (match(ipaddr,i->ipaddr, true))
555 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
556 if (match(ipaddr,i->ipaddr, true))
561 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
563 char* matches_kline(const char* host)
565 if ((klines.empty()) && (pklines.empty()))
567 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
568 if (match(host,i->hostmask, true))
570 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
571 if (match(host,i->hostmask, true))
576 bool GSortComparison ( const GLine one, const GLine two )
578 return (one.duration + one.set_time) < (two.duration + two.set_time);
581 bool ESortComparison ( const ELine one, const ELine two )
583 return (one.duration + one.set_time) < (two.duration + two.set_time);
586 bool ZSortComparison ( const ZLine one, const ZLine two )
588 return (one.duration + one.set_time) < (two.duration + two.set_time);
591 bool KSortComparison ( const KLine one, const KLine two )
593 return (one.duration + one.set_time) < (two.duration + two.set_time);
596 bool QSortComparison ( const QLine one, const QLine two )
598 return (one.duration + one.set_time) < (two.duration + two.set_time);
601 // removes lines that have expired
605 time_t current = TIME;
607 /* Because we now store all our XLines in sorted order using (i->duration + i->set_time) as a key, this
608 * means that to expire the XLines we just need to do a while, picking off the top few until there are
609 * none left at the head of the queue that are after the current time.
612 while ((glines.size()) && (current > (glines.begin()->duration + glines.begin()->set_time)))
614 std::vector<GLine>::iterator i = glines.begin();
615 ServerInstance->WriteOpers("*** Expiring timed G-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
619 while ((elines.size()) && (current > (elines.begin()->duration + elines.begin()->set_time)))
621 std::vector<ELine>::iterator i = elines.begin();
622 ServerInstance->WriteOpers("*** Expiring timed E-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
626 while ((zlines.size()) && (current > (zlines.begin()->duration + zlines.begin()->set_time)))
628 std::vector<ZLine>::iterator i = zlines.begin();
629 ServerInstance->WriteOpers("*** Expiring timed Z-Line %s (set by %s %d seconds ago)",i->ipaddr,i->source,i->duration);
633 while ((klines.size()) && (current > (klines.begin()->duration + klines.begin()->set_time)))
635 std::vector<KLine>::iterator i = klines.begin();
636 ServerInstance->WriteOpers("*** Expiring timed K-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
640 while ((qlines.size()) && (current > (qlines.begin()->duration + qlines.begin()->set_time)))
642 std::vector<QLine>::iterator i = qlines.begin();
643 ServerInstance->WriteOpers("*** Expiring timed Q-Line %s (set by %s %d seconds ago)",i->nick,i->source,i->duration);
649 // applies lines, removing clients and changing nicks etc as applicable
651 void apply_lines(const int What)
656 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()) &&
657 (!pglines.size()) && (!pklines.size()) && (!pzlines.size()) && (!pqlines.size()))
660 CullList* Goners = new CullList(ServerInstance);
662 for (std::vector<userrec*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
664 userrec* u = (userrec*)(*u2);
666 if (elines.size() || pelines.size())
668 // ignore people matching exempts
669 if (matches_exception(host))
672 if ((What & APPLY_GLINES) && (glines.size() || pglines.size()))
674 if ((check = matches_gline(host)))
676 snprintf(reason,MAXBUF,"G-Lined: %s",check);
677 Goners->AddItem(u,reason);
680 if ((What & APPLY_KLINES) && (klines.size() || pklines.size()))
682 if ((check = matches_kline(host)))
684 snprintf(reason,MAXBUF,"K-Lined: %s",check);
685 Goners->AddItem(u,reason);
688 if ((What & APPLY_QLINES) && (qlines.size() || pqlines.size()))
690 if ((check = matches_qline(u->nick)))
692 snprintf(reason,MAXBUF,"Q-Lined: %s",check);
693 Goners->AddItem(u,reason);
696 if ((What & APPLY_ZLINES) && (zlines.size() || pzlines.size()))
698 if ((check = matches_zline(u->GetIPString())))
700 snprintf(reason,MAXBUF,"Z-Lined: %s",check);
701 Goners->AddItem(u,reason);
710 void stats_k(userrec* user, string_list &results)
712 std::string sn = ServerInstance->Config->ServerName;
713 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
714 results.push_back(sn+" 216 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
715 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
716 results.push_back(sn+" 216 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
719 void stats_g(userrec* user, string_list &results)
721 std::string sn = ServerInstance->Config->ServerName;
722 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
723 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
724 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
725 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
728 void stats_q(userrec* user, string_list &results)
730 std::string sn = ServerInstance->Config->ServerName;
731 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
732 results.push_back(sn+" 217 "+user->nick+" :"+i->nick+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
733 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
734 results.push_back(sn+" 217 "+user->nick+" :"+i->nick+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
737 void stats_z(userrec* user, string_list &results)
739 std::string sn = ServerInstance->Config->ServerName;
740 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
741 results.push_back(sn+" 223 "+user->nick+" :"+i->ipaddr+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
742 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
743 results.push_back(sn+" 223 "+user->nick+" :"+i->ipaddr+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
746 void stats_e(userrec* user, string_list &results)
748 std::string sn = ServerInstance->Config->ServerName;
749 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
750 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
751 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
752 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);