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;
44 extern std::vector<Module*> modules;
45 extern std::vector<ircd_module*> factory;
47 /* Version two, now with optimized expiry!
49 * Because the old way was horrendously slow, the new way of expiring xlines is very
50 * very efficient. I have improved the efficiency of the algorithm in two ways:
52 * (1) There are now two lists of items for each linetype. One list holds temporary
53 * items, and the other list holds permanent items (ones which will expire).
54 * Items which are on the permanent list are NEVER checked at all by the
55 * expire_lines() function.
56 * (2) The temporary xline lists are always kept in strict numerical order, keyed by
57 * current time + duration. This means that the line which is due to expire the
58 * soonest is always pointed at by vector::begin(), so a simple while loop can
59 * very efficiently, very quickly and above all SAFELY pick off the first few
60 * items in the vector which need zapping.
69 /* Lists for temporary lines with an expiry time */
71 std::vector<KLine> klines;
72 std::vector<GLine> glines;
73 std::vector<ZLine> zlines;
74 std::vector<QLine> qlines;
75 std::vector<ELine> elines;
77 /* Seperate lists for perm XLines that isnt checked by expiry functions */
79 std::vector<KLine> pklines;
80 std::vector<GLine> pglines;
81 std::vector<ZLine> pzlines;
82 std::vector<QLine> pqlines;
83 std::vector<ELine> pelines;
86 bool GSortComparison ( const GLine one, const GLine two );
87 bool ZSortComparison ( const ZLine one, const ZLine two );
88 bool ESortComparison ( const ELine one, const ELine two );
89 bool QSortComparison ( const QLine one, const QLine two );
90 bool KSortComparison ( const KLine one, const KLine two );
92 // Reads the default bans from the config file.
93 // only a very small number of bans are defined
94 // this way these days, such as qlines against
95 // services nicks, etc.
97 bool InitXLine(ServerConfig* conf, const char* tag)
102 bool DoneXLine(ServerConfig* conf, const char* tag)
104 apply_lines(APPLY_ALL);
108 bool DoZLine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
110 char* reason = (char*)values[0];
111 char* ipmask = (char*)values[1];
113 add_zline(0,"<Config>",reason,ipmask);
114 log(DEBUG,"Read Z line (badip tag): ipmask=%s reason=%s",ipmask,reason);
118 bool DoQLine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
120 char* reason = (char*)values[0];
121 char* nick = (char*)values[1];
123 add_qline(0,"<Config>",reason,nick);
124 log(DEBUG,"Read Q line (badnick tag): nick=%s reason=%s",nick,reason);
128 bool DoKLine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
130 char* reason = (char*)values[0];
131 char* host = (char*)values[1];
133 add_kline(0,"<Config>",reason,host);
134 log(DEBUG,"Read K line (badhost tag): host=%s reason=%s",host,reason);
138 bool DoELine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
140 char* reason = (char*)values[0];
141 char* host = (char*)values[1];
143 add_eline(0,"<Config>",reason,host);
144 log(DEBUG,"Read E line (exception tag): host=%s reason=%s",host,reason);
150 bool add_gline(long duration, const char* source,const char* reason,const char* hostmask)
152 bool ret = del_gline(hostmask);
155 item.duration = duration;
156 strlcpy(item.hostmask,hostmask,199);
157 strlcpy(item.reason,reason,MAXBUF);
158 strlcpy(item.source,source,255);
160 item.set_time = TIME;
164 glines.push_back(item);
165 sort(glines.begin(), glines.end(),GSortComparison);
169 pglines.push_back(item);
175 // adds an e:line (exception to bans)
177 bool add_eline(long duration, const char* source, const char* reason, const char* hostmask)
179 bool ret = del_eline(hostmask);
181 item.duration = duration;
182 strlcpy(item.hostmask,hostmask,199);
183 strlcpy(item.reason,reason,MAXBUF);
184 strlcpy(item.source,source,255);
186 item.set_time = TIME;
189 elines.push_back(item);
190 sort(elines.begin(), elines.end(),ESortComparison);
194 pelines.push_back(item);
201 bool add_qline(long duration, const char* source, const char* reason, const char* nickname)
203 bool ret = del_qline(nickname);
205 item.duration = duration;
206 strlcpy(item.nick,nickname,63);
207 strlcpy(item.reason,reason,MAXBUF);
208 strlcpy(item.source,source,255);
210 item.is_global = false;
211 item.set_time = TIME;
214 qlines.push_back(item);
215 sort(qlines.begin(), qlines.end(),QSortComparison);
219 pqlines.push_back(item);
226 bool add_zline(long duration, const char* source, const char* reason, const char* ipaddr)
228 bool ret = del_zline(ipaddr);
230 item.duration = duration;
231 if (strchr(ipaddr,'@'))
233 while (*ipaddr != '@')
237 strlcpy(item.ipaddr,ipaddr,39);
238 strlcpy(item.reason,reason,MAXBUF);
239 strlcpy(item.source,source,255);
241 item.is_global = false;
242 item.set_time = TIME;
245 zlines.push_back(item);
246 sort(zlines.begin(), zlines.end(),ZSortComparison);
250 pzlines.push_back(item);
257 bool add_kline(long duration, const char* source, const char* reason, const char* hostmask)
259 bool ret = del_kline(hostmask);
261 item.duration = duration;
262 strlcpy(item.hostmask,hostmask,200);
263 strlcpy(item.reason,reason,MAXBUF);
264 strlcpy(item.source,source,255);
266 item.set_time = TIME;
269 klines.push_back(item);
270 sort(klines.begin(), klines.end(),KSortComparison);
274 pklines.push_back(item);
279 // deletes a g:line, returns true if the line existed and was removed
281 bool del_gline(const char* hostmask)
283 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
285 if (!strcasecmp(hostmask,i->hostmask))
291 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
293 if (!strcasecmp(hostmask,i->hostmask))
302 // deletes a e:line, returns true if the line existed and was removed
304 bool del_eline(const char* hostmask)
306 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
308 if (!strcasecmp(hostmask,i->hostmask))
314 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
316 if (!strcasecmp(hostmask,i->hostmask))
325 // deletes a q:line, returns true if the line existed and was removed
327 bool del_qline(const char* nickname)
329 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
331 if (!strcasecmp(nickname,i->nick))
337 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
339 if (!strcasecmp(nickname,i->nick))
348 bool qline_make_global(const char* nickname)
350 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
352 if (!strcasecmp(nickname,i->nick))
361 bool zline_make_global(const char* ipaddr)
363 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
365 if (!strcasecmp(ipaddr,i->ipaddr))
374 // deletes a z:line, returns true if the line existed and was removed
376 bool del_zline(const char* ipaddr)
378 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
380 if (!strcasecmp(ipaddr,i->ipaddr))
386 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
388 if (!strcasecmp(ipaddr,i->ipaddr))
397 // deletes a k:line, returns true if the line existed and was removed
399 bool del_kline(const char* hostmask)
401 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
403 if (!strcasecmp(hostmask,i->hostmask))
409 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
411 if (!strcasecmp(hostmask,i->hostmask))
420 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
422 char* matches_qline(const char* nick)
424 if ((qlines.empty()) && (pqlines.empty()))
426 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
427 if (match(nick,i->nick))
429 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
430 if (match(nick,i->nick))
435 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
437 char* matches_gline(const char* host)
439 if ((glines.empty()) && (pglines.empty()))
441 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
442 if (match(host,i->hostmask, true))
444 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
445 if (match(host,i->hostmask, true))
450 char* matches_exception(const char* host)
452 if ((elines.empty()) && (pelines.empty()))
455 snprintf(host2,MAXBUF,"*@%s",host);
456 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
457 if ((match(host,i->hostmask)) || (match(host2,i->hostmask, true)))
459 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
460 if ((match(host,i->hostmask)) || (match(host2,i->hostmask, true)))
466 void gline_set_creation_time(const char* host, time_t create_time)
468 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
470 if (!strcasecmp(host,i->hostmask))
472 i->set_time = create_time;
476 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
478 if (!strcasecmp(host,i->hostmask))
480 i->set_time = create_time;
487 void eline_set_creation_time(const char* host, time_t create_time)
489 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
491 if (!strcasecmp(host,i->hostmask))
493 i->set_time = create_time;
497 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
499 if (!strcasecmp(host,i->hostmask))
501 i->set_time = create_time;
508 void qline_set_creation_time(const char* nick, time_t create_time)
510 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
512 if (!strcasecmp(nick,i->nick))
514 i->set_time = create_time;
518 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
520 if (!strcasecmp(nick,i->nick))
522 i->set_time = create_time;
529 void zline_set_creation_time(const char* ip, time_t create_time)
531 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
533 if (!strcasecmp(ip,i->ipaddr))
535 i->set_time = create_time;
539 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
541 if (!strcasecmp(ip,i->ipaddr))
543 i->set_time = create_time;
550 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
552 char* matches_zline(const char* ipaddr)
554 if ((zlines.empty()) && (pzlines.empty()))
556 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
557 if (match(ipaddr,i->ipaddr, true))
559 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
560 if (match(ipaddr,i->ipaddr, true))
565 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
567 char* matches_kline(const char* host)
569 if ((klines.empty()) && (pklines.empty()))
571 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
572 if (match(host,i->hostmask, true))
574 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
575 if (match(host,i->hostmask, true))
580 bool GSortComparison ( const GLine one, const GLine two )
582 return (one.duration + one.set_time) < (two.duration + two.set_time);
585 bool ESortComparison ( const ELine one, const ELine two )
587 return (one.duration + one.set_time) < (two.duration + two.set_time);
590 bool ZSortComparison ( const ZLine one, const ZLine two )
592 return (one.duration + one.set_time) < (two.duration + two.set_time);
595 bool KSortComparison ( const KLine one, const KLine two )
597 return (one.duration + one.set_time) < (two.duration + two.set_time);
600 bool QSortComparison ( const QLine one, const QLine two )
602 return (one.duration + one.set_time) < (two.duration + two.set_time);
605 // removes lines that have expired
609 time_t current = TIME;
611 /* Because we now store all our XLines in sorted order using (i->duration + i->set_time) as a key, this
612 * means that to expire the XLines we just need to do a while, picking off the top few until there are
613 * none left at the head of the queue that are after the current time.
616 while ((glines.size()) && (current > (glines.begin()->duration + glines.begin()->set_time)))
618 std::vector<GLine>::iterator i = glines.begin();
619 ServerInstance->WriteOpers("*** Expiring timed G-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
623 while ((elines.size()) && (current > (elines.begin()->duration + elines.begin()->set_time)))
625 std::vector<ELine>::iterator i = elines.begin();
626 ServerInstance->WriteOpers("*** Expiring timed E-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
630 while ((zlines.size()) && (current > (zlines.begin()->duration + zlines.begin()->set_time)))
632 std::vector<ZLine>::iterator i = zlines.begin();
633 ServerInstance->WriteOpers("*** Expiring timed Z-Line %s (set by %s %d seconds ago)",i->ipaddr,i->source,i->duration);
637 while ((klines.size()) && (current > (klines.begin()->duration + klines.begin()->set_time)))
639 std::vector<KLine>::iterator i = klines.begin();
640 ServerInstance->WriteOpers("*** Expiring timed K-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
644 while ((qlines.size()) && (current > (qlines.begin()->duration + qlines.begin()->set_time)))
646 std::vector<QLine>::iterator i = qlines.begin();
647 ServerInstance->WriteOpers("*** Expiring timed Q-Line %s (set by %s %d seconds ago)",i->nick,i->source,i->duration);
653 // applies lines, removing clients and changing nicks etc as applicable
655 void apply_lines(const int What)
660 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()) &&
661 (!pglines.size()) && (!pklines.size()) && (!pzlines.size()) && (!pqlines.size()))
664 CullList* Goners = new CullList();
666 for (std::vector<userrec*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
668 userrec* u = (userrec*)(*u2);
670 if (elines.size() || pelines.size())
672 // ignore people matching exempts
673 if (matches_exception(host))
676 if ((What & APPLY_GLINES) && (glines.size() || pglines.size()))
678 if ((check = matches_gline(host)))
680 snprintf(reason,MAXBUF,"G-Lined: %s",check);
681 Goners->AddItem(u,reason);
684 if ((What & APPLY_KLINES) && (klines.size() || pklines.size()))
686 if ((check = matches_kline(host)))
688 snprintf(reason,MAXBUF,"K-Lined: %s",check);
689 Goners->AddItem(u,reason);
692 if ((What & APPLY_QLINES) && (qlines.size() || pqlines.size()))
694 if ((check = matches_qline(u->nick)))
696 snprintf(reason,MAXBUF,"Q-Lined: %s",check);
697 Goners->AddItem(u,reason);
700 if ((What & APPLY_ZLINES) && (zlines.size() || pzlines.size()))
702 if ((check = matches_zline(u->GetIPString())))
704 snprintf(reason,MAXBUF,"Z-Lined: %s",check);
705 Goners->AddItem(u,reason);
714 void stats_k(userrec* user, string_list &results)
716 std::string sn = ServerInstance->Config->ServerName;
717 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
718 results.push_back(sn+" 216 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
719 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
720 results.push_back(sn+" 216 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
723 void stats_g(userrec* user, string_list &results)
725 std::string sn = ServerInstance->Config->ServerName;
726 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
727 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
728 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
729 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
732 void stats_q(userrec* user, string_list &results)
734 std::string sn = ServerInstance->Config->ServerName;
735 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
736 results.push_back(sn+" 217 "+user->nick+" :"+i->nick+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
737 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
738 results.push_back(sn+" 217 "+user->nick+" :"+i->nick+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
741 void stats_z(userrec* user, string_list &results)
743 std::string sn = ServerInstance->Config->ServerName;
744 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
745 results.push_back(sn+" 223 "+user->nick+" :"+i->ipaddr+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
746 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
747 results.push_back(sn+" 223 "+user->nick+" :"+i->ipaddr+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
750 void stats_e(userrec* user, string_list &results)
752 std::string sn = ServerInstance->Config->ServerName;
753 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
754 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
755 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
756 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);