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"
35 #include "inspstring.h"
36 #include "helperfuncs.h"
39 #include "configreader.h"
40 #include "cull_list.h"
42 extern ServerConfig *Config;
45 extern std::vector<Module*> modules;
46 extern std::vector<ircd_module*> factory;
47 extern ServerConfig* Config;
48 extern user_hash clientlist;
49 extern std::vector<userrec*> local_users;
51 /* Version two, now with optimized expiry!
53 * Because the old way was horrendously slow, the new way of expiring xlines is very
54 * very efficient. I have improved the efficiency of the algorithm in two ways:
56 * (1) There are now two lists of items for each linetype. One list holds temporary
57 * items, and the other list holds permanent items (ones which will expire).
58 * Items which are on the permanent list are NEVER checked at all by the
59 * expire_lines() function.
60 * (2) The temporary xline lists are always kept in strict numerical order, keyed by
61 * current time + duration. This means that the line which is due to expire the
62 * soonest is always pointed at by vector::begin(), so a simple while loop can
63 * very efficiently, very quickly and above all SAFELY pick off the first few
64 * items in the vector which need zapping.
73 /* Lists for temporary lines with an expiry time */
75 std::vector<KLine> klines;
76 std::vector<GLine> glines;
77 std::vector<ZLine> zlines;
78 std::vector<QLine> qlines;
79 std::vector<ELine> elines;
81 /* Seperate lists for perm XLines that isnt checked by expiry functions */
83 std::vector<KLine> pklines;
84 std::vector<GLine> pglines;
85 std::vector<ZLine> pzlines;
86 std::vector<QLine> pqlines;
87 std::vector<ELine> pelines;
90 bool GSortComparison ( const GLine one, const GLine two );
91 bool ZSortComparison ( const ZLine one, const ZLine two );
92 bool ESortComparison ( const ELine one, const ELine two );
93 bool QSortComparison ( const QLine one, const QLine two );
94 bool KSortComparison ( const KLine one, const KLine two );
96 // Reads the default bans from the config file.
97 // only a very small number of bans are defined
98 // this way these days, such as qlines against
99 // services nicks, etc.
101 bool InitXLine(const char* tag)
106 bool DoneXLine(const char* tag)
108 apply_lines(APPLY_ALL);
112 bool DoZLine(const char* tag, char** entries, void** values, int* types)
114 char* reason = (char*)values[0];
115 char* ipmask = (char*)values[1];
117 add_zline(0,"<Config>",reason,ipmask);
118 log(DEBUG,"Read Z line (badip tag): ipmask=%s reason=%s",ipmask,reason);
122 bool DoQLine(const char* tag, char** entries, void** values, int* types)
124 char* reason = (char*)values[0];
125 char* nick = (char*)values[1];
127 add_qline(0,"<Config>",reason,nick);
128 log(DEBUG,"Read Q line (badnick tag): nick=%s reason=%s",nick,reason);
132 bool DoKLine(const char* tag, char** entries, void** values, int* types)
134 char* reason = (char*)values[0];
135 char* host = (char*)values[1];
137 add_kline(0,"<Config>",reason,host);
138 log(DEBUG,"Read K line (badhost tag): host=%s reason=%s",host,reason);
142 bool DoELine(const char* tag, char** entries, void** values, int* types)
144 char* reason = (char*)values[0];
145 char* host = (char*)values[1];
147 add_eline(0,"<Config>",reason,host);
148 log(DEBUG,"Read E line (exception tag): host=%s reason=%s",host,reason);
154 bool add_gline(long duration, const char* source,const char* reason,const char* hostmask)
156 bool ret = del_gline(hostmask);
159 item.duration = duration;
160 strlcpy(item.hostmask,hostmask,199);
161 strlcpy(item.reason,reason,MAXBUF);
162 strlcpy(item.source,source,255);
164 item.set_time = TIME;
168 glines.push_back(item);
169 sort(glines.begin(), glines.end(),GSortComparison);
173 pglines.push_back(item);
179 // adds an e:line (exception to bans)
181 bool add_eline(long duration, const char* source, const char* reason, const char* hostmask)
183 bool ret = del_eline(hostmask);
185 item.duration = duration;
186 strlcpy(item.hostmask,hostmask,199);
187 strlcpy(item.reason,reason,MAXBUF);
188 strlcpy(item.source,source,255);
190 item.set_time = TIME;
193 elines.push_back(item);
194 sort(elines.begin(), elines.end(),ESortComparison);
198 pelines.push_back(item);
205 bool add_qline(long duration, const char* source, const char* reason, const char* nickname)
207 bool ret = del_qline(nickname);
209 item.duration = duration;
210 strlcpy(item.nick,nickname,63);
211 strlcpy(item.reason,reason,MAXBUF);
212 strlcpy(item.source,source,255);
214 item.is_global = false;
215 item.set_time = TIME;
218 qlines.push_back(item);
219 sort(qlines.begin(), qlines.end(),QSortComparison);
223 pqlines.push_back(item);
230 bool add_zline(long duration, const char* source, const char* reason, const char* ipaddr)
232 bool ret = del_zline(ipaddr);
234 item.duration = duration;
235 if (strchr(ipaddr,'@'))
237 while (*ipaddr != '@')
241 strlcpy(item.ipaddr,ipaddr,39);
242 strlcpy(item.reason,reason,MAXBUF);
243 strlcpy(item.source,source,255);
245 item.is_global = false;
246 item.set_time = TIME;
249 zlines.push_back(item);
250 sort(zlines.begin(), zlines.end(),ZSortComparison);
254 pzlines.push_back(item);
261 bool add_kline(long duration, const char* source, const char* reason, const char* hostmask)
263 bool ret = del_kline(hostmask);
265 item.duration = duration;
266 strlcpy(item.hostmask,hostmask,200);
267 strlcpy(item.reason,reason,MAXBUF);
268 strlcpy(item.source,source,255);
270 item.set_time = TIME;
273 klines.push_back(item);
274 sort(klines.begin(), klines.end(),KSortComparison);
278 pklines.push_back(item);
283 // deletes a g:line, returns true if the line existed and was removed
285 bool del_gline(const char* hostmask)
287 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
289 if (!strcasecmp(hostmask,i->hostmask))
295 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
297 if (!strcasecmp(hostmask,i->hostmask))
306 // deletes a e:line, returns true if the line existed and was removed
308 bool del_eline(const char* hostmask)
310 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
312 if (!strcasecmp(hostmask,i->hostmask))
318 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
320 if (!strcasecmp(hostmask,i->hostmask))
329 // deletes a q:line, returns true if the line existed and was removed
331 bool del_qline(const char* nickname)
333 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
335 if (!strcasecmp(nickname,i->nick))
341 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
343 if (!strcasecmp(nickname,i->nick))
352 bool qline_make_global(const char* nickname)
354 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
356 if (!strcasecmp(nickname,i->nick))
365 bool zline_make_global(const char* ipaddr)
367 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
369 if (!strcasecmp(ipaddr,i->ipaddr))
378 // deletes a z:line, returns true if the line existed and was removed
380 bool del_zline(const char* ipaddr)
382 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
384 if (!strcasecmp(ipaddr,i->ipaddr))
390 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
392 if (!strcasecmp(ipaddr,i->ipaddr))
401 // deletes a k:line, returns true if the line existed and was removed
403 bool del_kline(const char* hostmask)
405 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
407 if (!strcasecmp(hostmask,i->hostmask))
413 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
415 if (!strcasecmp(hostmask,i->hostmask))
424 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
426 char* matches_qline(const char* nick)
428 if ((qlines.empty()) && (pqlines.empty()))
430 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
431 if (match(nick,i->nick))
433 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
434 if (match(nick,i->nick))
439 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
441 char* matches_gline(const char* host)
443 if ((glines.empty()) && (pglines.empty()))
445 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
446 if (match(host,i->hostmask, true))
448 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
449 if (match(host,i->hostmask, true))
454 char* matches_exception(const char* host)
456 if ((elines.empty()) && (pelines.empty()))
459 snprintf(host2,MAXBUF,"*@%s",host);
460 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
461 if ((match(host,i->hostmask)) || (match(host2,i->hostmask, true)))
463 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
464 if ((match(host,i->hostmask)) || (match(host2,i->hostmask, true)))
470 void gline_set_creation_time(const char* host, time_t create_time)
472 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
474 if (!strcasecmp(host,i->hostmask))
476 i->set_time = create_time;
480 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
482 if (!strcasecmp(host,i->hostmask))
484 i->set_time = create_time;
491 void eline_set_creation_time(const char* host, time_t create_time)
493 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
495 if (!strcasecmp(host,i->hostmask))
497 i->set_time = create_time;
501 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
503 if (!strcasecmp(host,i->hostmask))
505 i->set_time = create_time;
512 void qline_set_creation_time(const char* nick, time_t create_time)
514 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
516 if (!strcasecmp(nick,i->nick))
518 i->set_time = create_time;
522 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
524 if (!strcasecmp(nick,i->nick))
526 i->set_time = create_time;
533 void zline_set_creation_time(const char* ip, time_t create_time)
535 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
537 if (!strcasecmp(ip,i->ipaddr))
539 i->set_time = create_time;
543 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
545 if (!strcasecmp(ip,i->ipaddr))
547 i->set_time = create_time;
554 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
556 char* matches_zline(const char* ipaddr)
558 if ((zlines.empty()) && (pzlines.empty()))
560 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
561 if (match(ipaddr,i->ipaddr, true))
563 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
564 if (match(ipaddr,i->ipaddr, true))
569 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
571 char* matches_kline(const char* host)
573 if ((klines.empty()) && (pklines.empty()))
575 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
576 if (match(host,i->hostmask, true))
578 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
579 if (match(host,i->hostmask, true))
584 bool GSortComparison ( const GLine one, const GLine two )
586 return (one.duration + one.set_time) < (two.duration + two.set_time);
589 bool ESortComparison ( const ELine one, const ELine two )
591 return (one.duration + one.set_time) < (two.duration + two.set_time);
594 bool ZSortComparison ( const ZLine one, const ZLine two )
596 return (one.duration + one.set_time) < (two.duration + two.set_time);
599 bool KSortComparison ( const KLine one, const KLine two )
601 return (one.duration + one.set_time) < (two.duration + two.set_time);
604 bool QSortComparison ( const QLine one, const QLine two )
606 return (one.duration + one.set_time) < (two.duration + two.set_time);
609 // removes lines that have expired
613 time_t current = TIME;
615 /* Because we now store all our XLines in sorted order using (i->duration + i->set_time) as a key, this
616 * means that to expire the XLines we just need to do a while, picking off the top few until there are
617 * none left at the head of the queue that are after the current time.
620 while ((glines.size()) && (current > (glines.begin()->duration + glines.begin()->set_time)))
622 std::vector<GLine>::iterator i = glines.begin();
623 WriteOpers("*** Expiring timed G-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
627 while ((elines.size()) && (current > (elines.begin()->duration + elines.begin()->set_time)))
629 std::vector<ELine>::iterator i = elines.begin();
630 WriteOpers("*** Expiring timed E-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
634 while ((zlines.size()) && (current > (zlines.begin()->duration + zlines.begin()->set_time)))
636 std::vector<ZLine>::iterator i = zlines.begin();
637 WriteOpers("*** Expiring timed Z-Line %s (set by %s %d seconds ago)",i->ipaddr,i->source,i->duration);
641 while ((klines.size()) && (current > (klines.begin()->duration + klines.begin()->set_time)))
643 std::vector<KLine>::iterator i = klines.begin();
644 WriteOpers("*** Expiring timed K-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
648 while ((qlines.size()) && (current > (qlines.begin()->duration + qlines.begin()->set_time)))
650 std::vector<QLine>::iterator i = qlines.begin();
651 WriteOpers("*** Expiring timed Q-Line %s (set by %s %d seconds ago)",i->nick,i->source,i->duration);
657 // applies lines, removing clients and changing nicks etc as applicable
659 void apply_lines(const int What)
664 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()) &&
665 (!pglines.size()) && (!pklines.size()) && (!pzlines.size()) && (!pqlines.size()))
668 CullList* Goners = new CullList();
670 for (std::vector<userrec*>::const_iterator u2 = local_users.begin(); u2 != local_users.end(); u2++)
672 userrec* u = (userrec*)(*u2);
674 if (elines.size() || pelines.size())
676 // ignore people matching exempts
677 if (matches_exception(host))
680 if ((What & APPLY_GLINES) && (glines.size() || pglines.size()))
682 if ((check = matches_gline(host)))
684 snprintf(reason,MAXBUF,"G-Lined: %s",check);
685 Goners->AddItem(u,reason);
688 if ((What & APPLY_KLINES) && (klines.size() || pklines.size()))
690 if ((check = matches_kline(host)))
692 snprintf(reason,MAXBUF,"K-Lined: %s",check);
693 Goners->AddItem(u,reason);
696 if ((What & APPLY_QLINES) && (qlines.size() || pqlines.size()))
698 if ((check = matches_qline(u->nick)))
700 snprintf(reason,MAXBUF,"Q-Lined: %s",check);
701 Goners->AddItem(u,reason);
704 if ((What & APPLY_ZLINES) && (zlines.size() || pzlines.size()))
706 if ((check = matches_zline(u->GetIPString())))
708 snprintf(reason,MAXBUF,"Z-Lined: %s",check);
709 Goners->AddItem(u,reason);
718 void stats_k(userrec* user, string_list &results)
720 std::string sn = Config->ServerName;
721 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
722 results.push_back(sn+" 216 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
723 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
724 results.push_back(sn+" 216 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
727 void stats_g(userrec* user, string_list &results)
729 std::string sn = Config->ServerName;
730 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
731 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
732 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
733 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
736 void stats_q(userrec* user, string_list &results)
738 std::string sn = Config->ServerName;
739 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
740 results.push_back(sn+" 217 "+user->nick+" :"+i->nick+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
741 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
742 results.push_back(sn+" 217 "+user->nick+" :"+i->nick+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
745 void stats_z(userrec* user, string_list &results)
747 std::string sn = Config->ServerName;
748 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
749 results.push_back(sn+" 223 "+user->nick+" :"+i->ipaddr+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
750 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
751 results.push_back(sn+" 223 "+user->nick+" :"+i->ipaddr+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
754 void stats_e(userrec* user, string_list &results)
756 std::string sn = Config->ServerName;
757 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
758 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
759 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
760 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);