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 "cull_list.h"
41 extern ServerConfig *Config;
44 extern std::vector<Module*> modules;
45 extern std::vector<ircd_module*> factory;
46 extern ServerConfig* Config;
47 extern user_hash clientlist;
48 extern std::vector<userrec*> local_users;
50 /* Version two, now with optimized expiry!
52 * Because the old way was horrendously slow, the new way of expiring xlines is very
53 * very efficient. I have improved the efficiency of the algorithm in two ways:
55 * (1) There are now two lists of items for each linetype. One list holds temporary
56 * items, and the other list holds permenant items (ones which will expire).
57 * Items which are on the permenant list are NEVER checked at all by the
58 * expire_lines() function.
59 * (2) The temporary xline lists are always kept in strict numerical order, keyed by
60 * current time + duration. This means that the line which is due to expire the
61 * soonest is always pointed at by vector::begin(), so a simple while loop can
62 * very efficiently, very quickly and above all SAFELY pick off the first few
63 * items in the vector which need zapping.
72 /* Lists for temporary lines with an expiry time */
74 std::vector<KLine> klines;
75 std::vector<GLine> glines;
76 std::vector<ZLine> zlines;
77 std::vector<QLine> qlines;
78 std::vector<ELine> elines;
80 /* Seperate lists for perm XLines that isnt checked by expiry functions */
82 std::vector<KLine> pklines;
83 std::vector<GLine> pglines;
84 std::vector<ZLine> pzlines;
85 std::vector<QLine> pqlines;
86 std::vector<ELine> pelines;
89 bool GSortComparison ( const GLine one, const GLine two );
90 bool ZSortComparison ( const ZLine one, const ZLine two );
91 bool ESortComparison ( const ELine one, const ELine two );
92 bool QSortComparison ( const QLine one, const QLine two );
93 bool KSortComparison ( const KLine one, const KLine two );
95 // Reads the default bans from the config file.
96 // only a very small number of bans are defined
97 // this way these days, such as qlines against
98 // services nicks, etc.
100 bool InitXLine(const char* tag)
105 bool DoneXLine(const char* tag)
107 apply_lines(APPLY_ALL);
111 bool DoZLine(const char* tag, char** entries, void** values, int* types)
113 char* reason = (char*)values[0];
114 char* ipmask = (char*)values[1];
116 add_zline(0,"<Config>",reason,ipmask);
117 log(DEBUG,"Read Z line (badip tag): ipmask=%s reason=%s",ipmask,reason);
121 bool DoQLine(const char* tag, char** entries, void** values, int* types)
123 char* reason = (char*)values[0];
124 char* nick = (char*)values[1];
126 add_qline(0,"<Config>",reason,nick);
127 log(DEBUG,"Read Q line (badnick tag): nick=%s reason=%s",nick,reason);
131 bool DoKLine(const char* tag, char** entries, void** values, int* types)
133 char* reason = (char*)values[0];
134 char* host = (char*)values[1];
136 add_kline(0,"<Config>",reason,host);
137 log(DEBUG,"Read K line (badhost tag): host=%s reason=%s",host,reason);
141 bool DoELine(const char* tag, char** entries, void** values, int* types)
143 char* reason = (char*)values[0];
144 char* host = (char*)values[1];
146 add_eline(0,"<Config>",reason,host);
147 log(DEBUG,"Read E line (exception tag): host=%s reason=%s",host,reason);
153 bool add_gline(long duration, const char* source,const char* reason,const char* hostmask)
155 bool ret = del_gline(hostmask);
158 item.duration = duration;
159 strlcpy(item.hostmask,hostmask,199);
160 strlcpy(item.reason,reason,MAXBUF);
161 strlcpy(item.source,source,255);
163 item.set_time = TIME;
167 glines.push_back(item);
168 sort(glines.begin(), glines.end(),GSortComparison);
172 pglines.push_back(item);
178 // adds an e:line (exception to bans)
180 bool add_eline(long duration, const char* source, const char* reason, const char* hostmask)
182 bool ret = del_eline(hostmask);
184 item.duration = duration;
185 strlcpy(item.hostmask,hostmask,199);
186 strlcpy(item.reason,reason,MAXBUF);
187 strlcpy(item.source,source,255);
189 item.set_time = TIME;
192 elines.push_back(item);
193 sort(elines.begin(), elines.end(),ESortComparison);
197 pelines.push_back(item);
204 bool add_qline(long duration, const char* source, const char* reason, const char* nickname)
206 bool ret = del_qline(nickname);
208 item.duration = duration;
209 strlcpy(item.nick,nickname,63);
210 strlcpy(item.reason,reason,MAXBUF);
211 strlcpy(item.source,source,255);
213 item.is_global = false;
214 item.set_time = TIME;
217 qlines.push_back(item);
218 sort(qlines.begin(), qlines.end(),QSortComparison);
222 pqlines.push_back(item);
229 bool add_zline(long duration, const char* source, const char* reason, const char* ipaddr)
231 bool ret = del_zline(ipaddr);
233 item.duration = duration;
234 if (strchr(ipaddr,'@'))
236 while (*ipaddr != '@')
240 strlcpy(item.ipaddr,ipaddr,39);
241 strlcpy(item.reason,reason,MAXBUF);
242 strlcpy(item.source,source,255);
244 item.is_global = false;
245 item.set_time = TIME;
248 zlines.push_back(item);
249 sort(zlines.begin(), zlines.end(),ZSortComparison);
253 pzlines.push_back(item);
260 bool add_kline(long duration, const char* source, const char* reason, const char* hostmask)
262 bool ret = del_kline(hostmask);
264 item.duration = duration;
265 strlcpy(item.hostmask,hostmask,200);
266 strlcpy(item.reason,reason,MAXBUF);
267 strlcpy(item.source,source,255);
269 item.set_time = TIME;
272 klines.push_back(item);
273 sort(klines.begin(), klines.end(),KSortComparison);
277 pklines.push_back(item);
282 // deletes a g:line, returns true if the line existed and was removed
284 bool del_gline(const char* hostmask)
286 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
288 if (!strcasecmp(hostmask,i->hostmask))
294 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
296 if (!strcasecmp(hostmask,i->hostmask))
305 // deletes a e:line, returns true if the line existed and was removed
307 bool del_eline(const char* hostmask)
309 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
311 if (!strcasecmp(hostmask,i->hostmask))
317 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
319 if (!strcasecmp(hostmask,i->hostmask))
328 // deletes a q:line, returns true if the line existed and was removed
330 bool del_qline(const char* nickname)
332 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
334 if (!strcasecmp(nickname,i->nick))
340 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
342 if (!strcasecmp(nickname,i->nick))
351 bool qline_make_global(const char* nickname)
353 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
355 if (!strcasecmp(nickname,i->nick))
364 bool zline_make_global(const char* ipaddr)
366 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
368 if (!strcasecmp(ipaddr,i->ipaddr))
377 // deletes a z:line, returns true if the line existed and was removed
379 bool del_zline(const char* ipaddr)
381 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
383 if (!strcasecmp(ipaddr,i->ipaddr))
389 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
391 if (!strcasecmp(ipaddr,i->ipaddr))
400 // deletes a k:line, returns true if the line existed and was removed
402 bool del_kline(const char* hostmask)
404 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
406 if (!strcasecmp(hostmask,i->hostmask))
412 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
414 if (!strcasecmp(hostmask,i->hostmask))
423 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
425 char* matches_qline(const char* nick)
427 if ((qlines.empty()) && (pqlines.empty()))
429 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
430 if (match(nick,i->nick))
432 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
433 if (match(nick,i->nick))
438 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
440 char* matches_gline(const char* host)
442 if ((glines.empty()) && (pglines.empty()))
444 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
445 if (match(host,i->hostmask))
447 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
448 if (match(host,i->hostmask))
453 char* matches_exception(const char* host)
455 if ((elines.empty()) && (pelines.empty()))
458 snprintf(host2,MAXBUF,"*@%s",host);
459 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
460 if ((match(host,i->hostmask)) || (match(host2,i->hostmask)))
462 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
463 if ((match(host,i->hostmask)) || (match(host2,i->hostmask)))
469 void gline_set_creation_time(char* host, time_t create_time)
471 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
473 if (!strcasecmp(host,i->hostmask))
475 i->set_time = create_time;
479 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
481 if (!strcasecmp(host,i->hostmask))
483 i->set_time = create_time;
490 void eline_set_creation_time(char* host, time_t create_time)
492 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
494 if (!strcasecmp(host,i->hostmask))
496 i->set_time = create_time;
500 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
502 if (!strcasecmp(host,i->hostmask))
504 i->set_time = create_time;
511 void qline_set_creation_time(char* nick, time_t create_time)
513 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
515 if (!strcasecmp(nick,i->nick))
517 i->set_time = create_time;
521 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
523 if (!strcasecmp(nick,i->nick))
525 i->set_time = create_time;
532 void zline_set_creation_time(char* ip, time_t create_time)
534 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
536 if (!strcasecmp(ip,i->ipaddr))
538 i->set_time = create_time;
542 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
544 if (!strcasecmp(ip,i->ipaddr))
546 i->set_time = create_time;
553 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
555 char* matches_zline(const char* ipaddr)
557 if ((zlines.empty()) && (pzlines.empty()))
559 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
560 if (match(ipaddr,i->ipaddr))
562 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
563 if (match(ipaddr,i->ipaddr))
568 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
570 char* matches_kline(const char* host)
572 if ((klines.empty()) && (pklines.empty()))
574 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
575 if (match(host,i->hostmask))
577 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
578 if (match(host,i->hostmask))
583 bool GSortComparison ( const GLine one, const GLine two )
585 return (one.duration + one.set_time) < (two.duration + two.set_time);
588 bool ESortComparison ( const ELine one, const ELine two )
590 return (one.duration + one.set_time) < (two.duration + two.set_time);
593 bool ZSortComparison ( const ZLine one, const ZLine two )
595 return (one.duration + one.set_time) < (two.duration + two.set_time);
598 bool KSortComparison ( const KLine one, const KLine two )
600 return (one.duration + one.set_time) < (two.duration + two.set_time);
603 bool QSortComparison ( const QLine one, const QLine two )
605 return (one.duration + one.set_time) < (two.duration + two.set_time);
608 // removes lines that have expired
612 time_t current = TIME;
614 /* Because we now store all our XLines in sorted order using (i->duration + i->set_time) as a key, this
615 * means that to expire the XLines we just need to do a while, picking off the top few until there are
616 * none left at the head of the queue that are after the current time.
619 while ((glines.size()) && (current > (glines.begin()->duration + glines.begin()->set_time)))
621 std::vector<GLine>::iterator i = glines.begin();
622 WriteOpers("*** Expiring timed G-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
626 while ((elines.size()) && (current > (elines.begin()->duration + elines.begin()->set_time)))
628 std::vector<ELine>::iterator i = elines.begin();
629 WriteOpers("*** Expiring timed E-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
633 while ((zlines.size()) && (current > (zlines.begin()->duration + zlines.begin()->set_time)))
635 std::vector<ZLine>::iterator i = zlines.begin();
636 WriteOpers("*** Expiring timed Z-Line %s (set by %s %d seconds ago)",i->ipaddr,i->source,i->duration);
640 while ((klines.size()) && (current > (klines.begin()->duration + klines.begin()->set_time)))
642 std::vector<KLine>::iterator i = klines.begin();
643 WriteOpers("*** Expiring timed K-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
647 while ((qlines.size()) && (current > (qlines.begin()->duration + qlines.begin()->set_time)))
649 std::vector<QLine>::iterator i = qlines.begin();
650 WriteOpers("*** Expiring timed Q-Line %s (set by %s %d seconds ago)",i->nick,i->source,i->duration);
656 // applies lines, removing clients and changing nicks etc as applicable
658 void apply_lines(const int What)
663 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()) &&
664 (!pglines.size()) && (!pklines.size()) && (!pzlines.size()) && (!pqlines.size()))
667 CullList* Goners = new CullList();
669 for (std::vector<userrec*>::const_iterator u2 = local_users.begin(); u2 != local_users.end(); u2++)
671 userrec* u = (userrec*)(*u2);
673 if (elines.size() || pelines.size())
675 // ignore people matching exempts
676 if (matches_exception(host))
679 if ((What & APPLY_GLINES) && (glines.size() || pglines.size()))
681 if ((check = matches_gline(host)))
683 snprintf(reason,MAXBUF,"G-Lined: %s",check);
684 Goners->AddItem(u,reason);
687 if ((What & APPLY_KLINES) && (klines.size() || pklines.size()))
689 if ((check = matches_kline(host)))
691 snprintf(reason,MAXBUF,"K-Lined: %s",check);
692 Goners->AddItem(u,reason);
695 if ((What & APPLY_QLINES) && (qlines.size() || pqlines.size()))
697 if ((check = matches_qline(u->nick)))
699 snprintf(reason,MAXBUF,"Q-Lined: %s",check);
700 Goners->AddItem(u,reason);
703 if ((What & APPLY_ZLINES) && (zlines.size() || pzlines.size()))
705 if ((check = matches_zline((char*)inet_ntoa(u->ip4))))
707 snprintf(reason,MAXBUF,"Z-Lined: %s",check);
708 Goners->AddItem(u,reason);
717 void stats_k(userrec* user)
719 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
720 WriteServ(user->fd,"216 %s :%s %d %d %s :%s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
721 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
722 WriteServ(user->fd,"216 %s :%s %d %d %s :%s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
725 void stats_g(userrec* user)
727 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
728 WriteServ(user->fd,"223 %s :%s %d %d %s :%s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
729 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
730 WriteServ(user->fd,"223 %s :%s %d %d %s :%s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
733 void stats_q(userrec* user)
735 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
736 WriteServ(user->fd,"217 %s :%s %d %d %s :%s",user->nick,i->nick,i->set_time,i->duration,i->source,i->reason);
737 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
738 WriteServ(user->fd,"217 %s :%s %d %d %s :%s",user->nick,i->nick,i->set_time,i->duration,i->source,i->reason);
741 void stats_z(userrec* user)
743 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
744 WriteServ(user->fd,"223 %s :%s %d %d %s :%s",user->nick,i->ipaddr,i->set_time,i->duration,i->source,i->reason);
745 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
746 WriteServ(user->fd,"223 %s :%s %d %d %s :%s",user->nick,i->ipaddr,i->set_time,i->duration,i->source,i->reason);
749 void stats_e(userrec* user)
751 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
752 WriteServ(user->fd,"223 %s :%s %d %d %s :%s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
753 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
754 WriteServ(user->fd,"223 %s :%s %d %d %s :%s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);