1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2005 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"
21 #include "inspircd_io.h"
24 #include <sys/errno.h>
28 #include <ext/hash_map>
45 #include "inspstring.h"
46 #include "helperfuncs.h"
49 #include "cull_list.h"
51 extern ServerConfig *Config;
54 extern std::vector<Module*> modules;
55 extern std::vector<ircd_module*> factory;
56 extern ServerConfig* Config;
57 extern user_hash clientlist;
58 extern std::vector<userrec*> local_users;
60 /* Version two, now with optimized expiry!
62 * Because the old way was horrendously slow, the new way of expiring xlines is very
63 * very efficient. I have improved the efficiency of the algorithm in two ways:
65 * (1) There are now two lists of items for each linetype. One list holds temporary
66 * items, and the other list holds permenant items (ones which will expire).
67 * Items which are on the permenant list are NEVER checked at all by the
68 * expire_lines() function.
69 * (2) The temporary xline lists are always kept in strict numerical order, keyed by
70 * current time + duration. This means that the line which is due to expire the
71 * soonest is always pointed at by vector::begin(), so a simple while loop can
72 * very efficiently, very quickly and above all SAFELY pick off the first few
73 * items in the vector which need zapping.
82 /* Lists for temporary lines with an expiry time */
84 std::vector<KLine> klines;
85 std::vector<GLine> glines;
86 std::vector<ZLine> zlines;
87 std::vector<QLine> qlines;
88 std::vector<ELine> elines;
90 /* Seperate lists for perm XLines that isnt checked by expiry functions */
92 std::vector<KLine> pklines;
93 std::vector<GLine> pglines;
94 std::vector<ZLine> pzlines;
95 std::vector<QLine> pqlines;
96 std::vector<ELine> pelines;
99 bool GSortComparison ( const GLine one, const GLine two );
100 bool ZSortComparison ( const ZLine one, const ZLine two );
101 bool ESortComparison ( const ELine one, const ELine two );
102 bool QSortComparison ( const QLine one, const QLine two );
103 bool KSortComparison ( const KLine one, const KLine two );
105 // Reads the default bans from the config file.
106 // only a very small number of bans are defined
107 // this way these days, such as qlines against
108 // services nicks, etc.
110 void read_xline_defaults()
117 for (int i = 0; i < Config->ConfValueEnum("badip",&Config->config_f); i++)
119 Config->ConfValue("badip","ipmask",i,ipmask,&Config->config_f);
120 Config->ConfValue("badip","reason",i,reason,&Config->config_f);
121 add_zline(0,"<Config>",reason,ipmask);
122 log(DEBUG,"Read Z line (badip tag): ipmask=%s reason=%s",ipmask,reason);
125 for (int i = 0; i < Config->ConfValueEnum("badnick",&Config->config_f); i++)
127 Config->ConfValue("badnick","nick",i,nick,&Config->config_f);
128 Config->ConfValue("badnick","reason",i,reason,&Config->config_f);
129 add_qline(0,"<Config>",reason,nick);
130 log(DEBUG,"Read Q line (badnick tag): nick=%s reason=%s",nick,reason);
133 for (int i = 0; i < Config->ConfValueEnum("badhost",&Config->config_f); i++)
135 Config->ConfValue("badhost","host",i,host,&Config->config_f);
136 Config->ConfValue("badhost","reason",i,reason,&Config->config_f);
137 add_kline(0,"<Config>",reason,host);
138 log(DEBUG,"Read K line (badhost tag): host=%s reason=%s",host,reason);
140 for (int i = 0; i < Config->ConfValueEnum("exception",&Config->config_f); i++)
142 Config->ConfValue("exception","host",i,host,&Config->config_f);
143 Config->ConfValue("exception","reason",i,reason,&Config->config_f);
144 add_eline(0,"<Config>",reason,host);
145 log(DEBUG,"Read E line (exception tag): host=%s reason=%s",host,reason);
151 void add_gline(long duration, const char* source,const char* reason,const char* 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;
163 glines.push_back(item);
164 sort(glines.begin(), glines.end(),GSortComparison);
168 pglines.push_back(item);
172 // adds an e:line (exception to bans)
174 void add_eline(long duration, const char* source, const char* reason, const char* hostmask)
178 item.duration = duration;
179 strlcpy(item.hostmask,hostmask,199);
180 strlcpy(item.reason,reason,MAXBUF);
181 strlcpy(item.source,source,255);
183 item.set_time = TIME;
186 elines.push_back(item);
187 sort(elines.begin(), elines.end(),ESortComparison);
191 pelines.push_back(item);
197 void add_qline(long duration, const char* source, const char* reason, const char* 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);
221 void add_zline(long duration, const char* source, const char* reason, const char* ipaddr)
225 item.duration = duration;
226 if (strchr(ipaddr,'@'))
228 while (*ipaddr != '@')
232 strlcpy(item.ipaddr,ipaddr,39);
233 strlcpy(item.reason,reason,MAXBUF);
234 strlcpy(item.source,source,255);
236 item.is_global = false;
237 item.set_time = TIME;
240 zlines.push_back(item);
241 sort(zlines.begin(), zlines.end(),ZSortComparison);
245 pzlines.push_back(item);
251 void add_kline(long duration, const char* source, const char* reason, const char* hostmask)
255 item.duration = duration;
256 strlcpy(item.hostmask,hostmask,200);
257 strlcpy(item.reason,reason,MAXBUF);
258 strlcpy(item.source,source,255);
260 item.set_time = TIME;
263 klines.push_back(item);
264 sort(klines.begin(), klines.end(),KSortComparison);
268 pklines.push_back(item);
272 // deletes a g:line, returns true if the line existed and was removed
274 bool del_gline(const char* hostmask)
276 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
278 if (!strcasecmp(hostmask,i->hostmask))
284 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
286 if (!strcasecmp(hostmask,i->hostmask))
295 // deletes a e:line, returns true if the line existed and was removed
297 bool del_eline(const char* hostmask)
299 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
301 if (!strcasecmp(hostmask,i->hostmask))
307 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
309 if (!strcasecmp(hostmask,i->hostmask))
318 // deletes a q:line, returns true if the line existed and was removed
320 bool del_qline(const char* nickname)
322 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
324 if (!strcasecmp(nickname,i->nick))
330 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
332 if (!strcasecmp(nickname,i->nick))
341 bool qline_make_global(const char* nickname)
343 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
345 if (!strcasecmp(nickname,i->nick))
354 bool zline_make_global(const char* ipaddr)
356 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
358 if (!strcasecmp(ipaddr,i->ipaddr))
367 // deletes a z:line, returns true if the line existed and was removed
369 bool del_zline(const char* ipaddr)
371 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
373 if (!strcasecmp(ipaddr,i->ipaddr))
379 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
381 if (!strcasecmp(ipaddr,i->ipaddr))
390 // deletes a k:line, returns true if the line existed and was removed
392 bool del_kline(const char* hostmask)
394 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
396 if (!strcasecmp(hostmask,i->hostmask))
402 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
404 if (!strcasecmp(hostmask,i->hostmask))
413 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
415 char* matches_qline(const char* nick)
417 if ((qlines.empty()) && (pqlines.empty()))
419 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
420 if (match(nick,i->nick))
422 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
423 if (match(nick,i->nick))
428 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
430 char* matches_gline(const char* host)
432 if ((glines.empty()) && (pglines.empty()))
434 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
435 if (match(host,i->hostmask))
437 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
438 if (match(host,i->hostmask))
443 char* matches_exception(const char* host)
445 if ((elines.empty()) && (pelines.empty()))
448 snprintf(host2,MAXBUF,"*@%s",host);
449 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
450 if ((match(host,i->hostmask)) || (match(host2,i->hostmask)))
452 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
453 if ((match(host,i->hostmask)) || (match(host2,i->hostmask)))
459 void gline_set_creation_time(char* host, time_t create_time)
461 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
463 if (!strcasecmp(host,i->hostmask))
465 i->set_time = create_time;
469 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
471 if (!strcasecmp(host,i->hostmask))
473 i->set_time = create_time;
480 void eline_set_creation_time(char* host, time_t create_time)
482 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
484 if (!strcasecmp(host,i->hostmask))
486 i->set_time = create_time;
490 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
492 if (!strcasecmp(host,i->hostmask))
494 i->set_time = create_time;
501 void qline_set_creation_time(char* nick, time_t create_time)
503 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
505 if (!strcasecmp(nick,i->nick))
507 i->set_time = create_time;
511 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
513 if (!strcasecmp(nick,i->nick))
515 i->set_time = create_time;
522 void zline_set_creation_time(char* ip, time_t create_time)
524 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
526 if (!strcasecmp(ip,i->ipaddr))
528 i->set_time = create_time;
532 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
534 if (!strcasecmp(ip,i->ipaddr))
536 i->set_time = create_time;
543 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
545 char* matches_zline(const char* ipaddr)
547 if ((zlines.empty()) && (pzlines.empty()))
549 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
550 if (match(ipaddr,i->ipaddr))
552 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
553 if (match(ipaddr,i->ipaddr))
558 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
560 char* matches_kline(const char* host)
562 if ((klines.empty()) && (pklines.empty()))
564 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
565 if (match(host,i->hostmask))
567 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
568 if (match(host,i->hostmask))
573 bool GSortComparison ( const GLine one, const GLine two )
575 return (one.duration + one.set_time) < (two.duration + two.set_time);
578 bool ESortComparison ( const ELine one, const ELine two )
580 return (one.duration + one.set_time) < (two.duration + two.set_time);
583 bool ZSortComparison ( const ZLine one, const ZLine two )
585 return (one.duration + one.set_time) < (two.duration + two.set_time);
588 bool KSortComparison ( const KLine one, const KLine two )
590 return (one.duration + one.set_time) < (two.duration + two.set_time);
593 bool QSortComparison ( const QLine one, const QLine two )
595 return (one.duration + one.set_time) < (two.duration + two.set_time);
598 // removes lines that have expired
602 time_t current = TIME;
604 /* Because we now store all our XLines in sorted order using (i->duration + i->set_time) as a key, this
605 * means that to expire the XLines we just need to do a while, picking off the top few until there are
606 * none left at the head of the queue that are after the current time.
609 while ((glines.size()) && (current > (glines.begin()->duration + glines.begin()->set_time)))
611 std::vector<GLine>::iterator i = glines.begin();
612 WriteOpers("Expiring timed G-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
616 while ((elines.size()) && (current > (elines.begin()->duration + elines.begin()->set_time)))
618 std::vector<ELine>::iterator i = elines.begin();
619 WriteOpers("Expiring timed E-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
623 while ((zlines.size()) && (current > (zlines.begin()->duration + zlines.begin()->set_time)))
625 std::vector<ZLine>::iterator i = zlines.begin();
626 WriteOpers("Expiring timed Z-Line %s (set by %s %d seconds ago)",i->ipaddr,i->source,i->duration);
630 while ((klines.size()) && (current > (klines.begin()->duration + klines.begin()->set_time)))
632 std::vector<KLine>::iterator i = klines.begin();
633 WriteOpers("Expiring timed K-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
637 while ((qlines.size()) && (current > (qlines.begin()->duration + qlines.begin()->set_time)))
639 std::vector<QLine>::iterator i = qlines.begin();
640 WriteOpers("Expiring timed Q-Line %s (set by %s %d seconds ago)",i->nick,i->source,i->duration);
646 // applies lines, removing clients and changing nicks etc as applicable
648 void apply_lines(const int What)
653 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()) &&
654 (!pglines.size()) && (!pklines.size()) && (!pzlines.size()) && (!pqlines.size()))
657 CullList* Goners = new CullList();
659 for (std::vector<userrec*>::const_iterator u2 = local_users.begin(); u2 != local_users.end(); u2++)
661 userrec* u = (userrec*)(*u2);
665 // ignore people matching exempts
666 if (matches_exception(host))
669 if ((What & APPLY_GLINES) && (glines.size() || pglines.size()))
671 if (check = matches_gline(host))
673 snprintf(reason,MAXBUF,"G-Lined: %s",check);
674 Goners->AddItem(u,reason);
677 if ((What & APPLY_KLINES) && (klines.size() || pklines.size()))
679 if (check = matches_kline(host))
681 snprintf(reason,MAXBUF,"K-Lined: %s",check);
682 Goners->AddItem(u,reason);
685 if ((What & APPLY_QLINES) && (qlines.size() || pqlines.size()))
687 if (check = matches_qline(u->nick))
689 snprintf(reason,MAXBUF,"Matched Q-Lined nick: %s",check);
690 Goners->AddItem(u,reason);
693 if ((What & APPLY_ZLINES) && (zlines.size() || pzlines.size()))
695 if (check = matches_zline(u->ip))
697 snprintf(reasonz,MAXBUF,"Z-Lined: %s",check);
698 Goners->AddItem(u,reason);
707 void stats_k(userrec* user)
709 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
710 WriteServ(user->fd,"216 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
711 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
712 WriteServ(user->fd,"216 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
715 void stats_g(userrec* user)
717 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
718 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
719 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
720 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
723 void stats_q(userrec* user)
725 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
726 WriteServ(user->fd,"217 %s :%s %d %d %s %s",user->nick,i->nick,i->set_time,i->duration,i->source,i->reason);
727 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
728 WriteServ(user->fd,"217 %s :%s %d %d %s %s",user->nick,i->nick,i->set_time,i->duration,i->source,i->reason);
731 void stats_z(userrec* user)
733 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
734 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->ipaddr,i->set_time,i->duration,i->source,i->reason);
735 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
736 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->ipaddr,i->set_time,i->duration,i->source,i->reason);
739 void stats_e(userrec* user)
741 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
742 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
743 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
744 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);