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;
59 /* Version two, now with optimized expiry!
61 * Because the old way was horrendously slow, the new way of expiring xlines is very
62 * very efficient. I have improved the efficiency of the algorithm in two ways:
64 * (1) There are now two lists of items for each linetype. One list holds temporary
65 * items, and the other list holds permenant items (ones which will expire).
66 * Items which are on the permenant list are NEVER checked at all by the
67 * expire_lines() function.
68 * (2) The temporary xline lists are always kept in strict numerical order, keyed by
69 * current time + duration. This means that the line which is due to expire the
70 * soonest is always pointed at by vector::begin(), so a simple while loop can
71 * very efficiently, very quickly and above all SAFELY pick off the first few
72 * items in the vector which need zapping.
81 /* Lists for temporary lines with an expiry time */
83 std::vector<KLine> klines;
84 std::vector<GLine> glines;
85 std::vector<ZLine> zlines;
86 std::vector<QLine> qlines;
87 std::vector<ELine> elines;
89 /* Seperate lists for perm XLines that isnt checked by expiry functions */
91 std::vector<KLine> pklines;
92 std::vector<GLine> pglines;
93 std::vector<ZLine> pzlines;
94 std::vector<QLine> pqlines;
95 std::vector<ELine> pelines;
98 bool GSortComparison ( const GLine one, const GLine two );
99 bool ZSortComparison ( const ZLine one, const ZLine two );
100 bool ESortComparison ( const ELine one, const ELine two );
101 bool QSortComparison ( const QLine one, const QLine two );
102 bool KSortComparison ( const KLine one, const KLine two );
104 // Reads the default bans from the config file.
105 // only a very small number of bans are defined
106 // this way these days, such as qlines against
107 // services nicks, etc.
109 void read_xline_defaults()
116 for (int i = 0; i < Config->ConfValueEnum("badip",&Config->config_f); i++)
118 Config->ConfValue("badip","ipmask",i,ipmask,&Config->config_f);
119 Config->ConfValue("badip","reason",i,reason,&Config->config_f);
120 add_zline(0,"<Config>",reason,ipmask);
121 log(DEBUG,"Read Z line (badip tag): ipmask=%s reason=%s",ipmask,reason);
124 for (int i = 0; i < Config->ConfValueEnum("badnick",&Config->config_f); i++)
126 Config->ConfValue("badnick","nick",i,nick,&Config->config_f);
127 Config->ConfValue("badnick","reason",i,reason,&Config->config_f);
128 add_qline(0,"<Config>",reason,nick);
129 log(DEBUG,"Read Q line (badnick tag): nick=%s reason=%s",nick,reason);
132 for (int i = 0; i < Config->ConfValueEnum("badhost",&Config->config_f); i++)
134 Config->ConfValue("badhost","host",i,host,&Config->config_f);
135 Config->ConfValue("badhost","reason",i,reason,&Config->config_f);
136 add_kline(0,"<Config>",reason,host);
137 log(DEBUG,"Read K line (badhost tag): host=%s reason=%s",host,reason);
139 for (int i = 0; i < Config->ConfValueEnum("exception",&Config->config_f); i++)
141 Config->ConfValue("exception","host",i,host,&Config->config_f);
142 Config->ConfValue("exception","reason",i,reason,&Config->config_f);
143 add_eline(0,"<Config>",reason,host);
144 log(DEBUG,"Read E line (exception tag): host=%s reason=%s",host,reason);
150 void add_gline(long duration, const char* source,const char* reason,const char* hostmask)
154 item.duration = duration;
155 strlcpy(item.hostmask,hostmask,199);
156 strlcpy(item.reason,reason,MAXBUF);
157 strlcpy(item.source,source,255);
159 item.set_time = TIME;
162 glines.push_back(item);
163 sort(glines.begin(), glines.end(),GSortComparison);
167 pglines.push_back(item);
171 // adds an e:line (exception to bans)
173 void add_eline(long duration, const char* source, const char* reason, const char* 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);
196 void add_qline(long duration, const char* source, const char* reason, const char* nickname)
200 item.duration = duration;
201 strlcpy(item.nick,nickname,63);
202 strlcpy(item.reason,reason,MAXBUF);
203 strlcpy(item.source,source,255);
205 item.is_global = false;
206 item.set_time = TIME;
209 qlines.push_back(item);
210 sort(qlines.begin(), qlines.end(),QSortComparison);
214 pqlines.push_back(item);
220 void add_zline(long duration, const char* source, const char* reason, const char* ipaddr)
224 item.duration = duration;
225 if (strchr(ipaddr,'@'))
227 while (*ipaddr != '@')
231 strlcpy(item.ipaddr,ipaddr,39);
232 strlcpy(item.reason,reason,MAXBUF);
233 strlcpy(item.source,source,255);
235 item.is_global = false;
236 item.set_time = TIME;
239 zlines.push_back(item);
240 sort(zlines.begin(), zlines.end(),ZSortComparison);
244 pzlines.push_back(item);
250 void add_kline(long duration, const char* source, const char* reason, const char* hostmask)
254 item.duration = duration;
255 strlcpy(item.hostmask,hostmask,200);
256 strlcpy(item.reason,reason,MAXBUF);
257 strlcpy(item.source,source,255);
259 item.set_time = TIME;
262 klines.push_back(item);
263 sort(klines.begin(), klines.end(),KSortComparison);
267 pklines.push_back(item);
271 // deletes a g:line, returns true if the line existed and was removed
273 bool del_gline(const char* hostmask)
275 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
277 if (!strcasecmp(hostmask,i->hostmask))
283 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
285 if (!strcasecmp(hostmask,i->hostmask))
294 // deletes a e:line, returns true if the line existed and was removed
296 bool del_eline(const char* hostmask)
298 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
300 if (!strcasecmp(hostmask,i->hostmask))
306 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
308 if (!strcasecmp(hostmask,i->hostmask))
317 // deletes a q:line, returns true if the line existed and was removed
319 bool del_qline(const char* nickname)
321 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
323 if (!strcasecmp(nickname,i->nick))
329 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
331 if (!strcasecmp(nickname,i->nick))
340 bool qline_make_global(const char* nickname)
342 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
344 if (!strcasecmp(nickname,i->nick))
353 bool zline_make_global(const char* ipaddr)
355 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
357 if (!strcasecmp(ipaddr,i->ipaddr))
366 // deletes a z:line, returns true if the line existed and was removed
368 bool del_zline(const char* ipaddr)
370 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
372 if (!strcasecmp(ipaddr,i->ipaddr))
378 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
380 if (!strcasecmp(ipaddr,i->ipaddr))
389 // deletes a k:line, returns true if the line existed and was removed
391 bool del_kline(const char* hostmask)
393 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
395 if (!strcasecmp(hostmask,i->hostmask))
401 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
403 if (!strcasecmp(hostmask,i->hostmask))
412 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
414 char* matches_qline(const char* nick)
416 if ((qlines.empty()) && (pqlines.empty()))
418 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
419 if (match(nick,i->nick))
421 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
422 if (match(nick,i->nick))
427 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
429 char* matches_gline(const char* host)
431 if ((glines.empty()) && (pglines.empty()))
433 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
434 if (match(host,i->hostmask))
436 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
437 if (match(host,i->hostmask))
442 char* matches_exception(const char* host)
444 if ((elines.empty()) && (pelines.empty()))
447 snprintf(host2,MAXBUF,"*@%s",host);
448 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
449 if ((match(host,i->hostmask)) || (match(host2,i->hostmask)))
451 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
452 if ((match(host,i->hostmask)) || (match(host2,i->hostmask)))
458 void gline_set_creation_time(char* host, time_t create_time)
460 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
462 if (!strcasecmp(host,i->hostmask))
464 i->set_time = create_time;
468 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
470 if (!strcasecmp(host,i->hostmask))
472 i->set_time = create_time;
479 void eline_set_creation_time(char* host, time_t create_time)
481 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
483 if (!strcasecmp(host,i->hostmask))
485 i->set_time = create_time;
489 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
491 if (!strcasecmp(host,i->hostmask))
493 i->set_time = create_time;
500 void qline_set_creation_time(char* nick, time_t create_time)
502 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
504 if (!strcasecmp(nick,i->nick))
506 i->set_time = create_time;
510 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
512 if (!strcasecmp(nick,i->nick))
514 i->set_time = create_time;
521 void zline_set_creation_time(char* ip, time_t create_time)
523 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
525 if (!strcasecmp(ip,i->ipaddr))
527 i->set_time = create_time;
531 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
533 if (!strcasecmp(ip,i->ipaddr))
535 i->set_time = create_time;
542 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
544 char* matches_zline(const char* ipaddr)
546 if ((zlines.empty()) && (pzlines.empty()))
548 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
549 if (match(ipaddr,i->ipaddr))
551 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
552 if (match(ipaddr,i->ipaddr))
557 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
559 char* matches_kline(const char* host)
561 if ((klines.empty()) && (pklines.empty()))
563 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
564 if (match(host,i->hostmask))
566 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
567 if (match(host,i->hostmask))
572 bool GSortComparison ( const GLine one, const GLine two )
574 return (one.duration + one.set_time) < (two.duration + two.set_time);
577 bool ESortComparison ( const ELine one, const ELine two )
579 return (one.duration + one.set_time) < (two.duration + two.set_time);
582 bool ZSortComparison ( const ZLine one, const ZLine two )
584 return (one.duration + one.set_time) < (two.duration + two.set_time);
587 bool KSortComparison ( const KLine one, const KLine two )
589 return (one.duration + one.set_time) < (two.duration + two.set_time);
592 bool QSortComparison ( const QLine one, const QLine two )
594 return (one.duration + one.set_time) < (two.duration + two.set_time);
597 // removes lines that have expired
601 time_t current = TIME;
603 /* Because we now store all our XLines in sorted order using (i->duration + i->set_time) as a key, this
604 * means that to expire the XLines we just need to do a while, picking off the top few until there are
605 * none left at the head of the queue that are after the current time.
608 while ((glines.size()) && (current > (glines.begin()->duration + glines.begin()->set_time)))
610 std::vector<GLine>::iterator i = glines.begin();
611 WriteOpers("Expiring timed G-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
615 while ((elines.size()) && (current > (elines.begin()->duration + elines.begin()->set_time)))
617 std::vector<ELine>::iterator i = elines.begin();
618 WriteOpers("Expiring timed E-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
622 while ((zlines.size()) && (current > (zlines.begin()->duration + zlines.begin()->set_time)))
624 std::vector<ZLine>::iterator i = zlines.begin();
625 WriteOpers("Expiring timed Z-Line %s (set by %s %d seconds ago)",i->ipaddr,i->source,i->duration);
629 while ((klines.size()) && (current > (klines.begin()->duration + klines.begin()->set_time)))
631 std::vector<KLine>::iterator i = klines.begin();
632 WriteOpers("Expiring timed K-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
636 while ((qlines.size()) && (current > (qlines.begin()->duration + qlines.begin()->set_time)))
638 std::vector<QLine>::iterator i = qlines.begin();
639 WriteOpers("Expiring timed Q-Line %s (set by %s %d seconds ago)",i->nick,i->source,i->duration);
645 // applies lines, removing clients and changing nicks etc as applicable
647 void apply_lines(const int What)
652 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()) &&
653 (!pglines.size()) && (!pklines.size()) && (!pzlines.size()) && (!pqlines.size()))
656 CullList* Goners = new CullList();
658 for (user_hash::const_iterator u = clientlist.begin(); u != clientlist.end(); u++)
660 if (u->second->fd > -1)
662 snprintf(host,MAXBUF,"%s@%s",u->second->ident,u->second->host);
665 // ignore people matching exempts
666 if (matches_exception(host))
669 if ((What & APPLY_GLINES) && (glines.size() || pglines.size()))
671 char* check = matches_gline(host);
674 snprintf(reason,MAXBUF,"G-Lined: %s",check);
675 Goners->AddItem(u->second,reason);
678 if ((What & APPLY_KLINES) && (klines.size() || pklines.size()))
680 char* check = matches_kline(host);
683 snprintf(reason,MAXBUF,"K-Lined: %s",check);
684 Goners->AddItem(u->second,reason);
687 if ((What & APPLY_QLINES) && (qlines.size() || pqlines.size()))
689 char* check = matches_qline(u->second->nick);
692 snprintf(reason,MAXBUF,"Matched Q-Lined nick: %s",check);
693 Goners->AddItem(u->second,reason);
696 if ((What & APPLY_ZLINES) && (zlines.size() || pzlines.size()))
698 char* check = matches_zline(u->second->ip);
701 snprintf(reason,MAXBUF,"Z-Lined: %s",check);
702 Goners->AddItem(u->second,reason);
712 void stats_k(userrec* user)
714 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
715 WriteServ(user->fd,"216 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
716 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
717 WriteServ(user->fd,"216 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
720 void stats_g(userrec* user)
722 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
723 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
724 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
725 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
728 void stats_q(userrec* user)
730 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
731 WriteServ(user->fd,"217 %s :%s %d %d %s %s",user->nick,i->nick,i->set_time,i->duration,i->source,i->reason);
732 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
733 WriteServ(user->fd,"217 %s :%s %d %d %s %s",user->nick,i->nick,i->set_time,i->duration,i->source,i->reason);
736 void stats_z(userrec* user)
738 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
739 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->ipaddr,i->set_time,i->duration,i->source,i->reason);
740 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
741 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->ipaddr,i->set_time,i->duration,i->source,i->reason);
744 void stats_e(userrec* user)
746 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
747 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
748 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
749 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);