1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 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"
22 #include "inspircd_util.h"
25 #include <sys/errno.h>
29 #include <ext/hash_map>
46 #include "inspstring.h"
47 #include "helperfuncs.h"
51 extern std::vector<Module*> modules;
52 extern std::vector<ircd_module*> factory;
55 extern char ServerName[MAXBUF];
56 extern char Network[MAXBUF];
57 extern char ServerDesc[MAXBUF];
58 extern char AdminName[MAXBUF];
59 extern char AdminEmail[MAXBUF];
60 extern char AdminNick[MAXBUF];
61 extern char diepass[MAXBUF];
62 extern char restartpass[MAXBUF];
63 extern char motd[MAXBUF];
64 extern char rules[MAXBUF];
65 extern char list[MAXBUF];
66 extern char PrefixQuit[MAXBUF];
67 extern char DieValue[MAXBUF];
70 extern int WHOWAS_STALE;
71 extern int WHOWAS_MAX;
73 extern time_t startup_time;
74 extern int NetBufferSize;
75 extern time_t nb_start;
77 extern std::vector<std::string> module_names;
79 extern int boundPortCount;
82 extern int ports[MAXSOCKS];
84 extern std::stringstream config_f;
86 extern FILE *log_file;
88 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, irc::StrHashComp> user_hash;
89 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, irc::StrHashComp> chan_hash;
90 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, irc::InAddr_HashComp> address_cache;
91 typedef nspace::hash_map<std::string, WhoWasUser*, nspace::hash<string>, irc::StrHashComp> whowas_hash;
92 typedef std::deque<command_t> command_table;
95 extern user_hash clientlist;
96 extern chan_hash chanlist;
97 extern whowas_hash whowas;
98 extern command_table cmdlist;
99 extern file_cache MOTD;
100 extern file_cache RULES;
101 extern address_cache IP;
103 /* Version two, now with optimized expiry!
105 * Because the old way was horrendously slow, the new way of expiring xlines is very
106 * very efficient. I have improved the efficiency of the algorithm in two ways:
108 * (1) There are now two lists of items for each linetype. One list holds temporary
109 * items, and the other list holds permenant items (ones which will expire).
110 * Items which are on the permenant list are NEVER checked at all by the
111 * expire_lines() function.
112 * (2) The temporary xline lists are always kept in strict numerical order, keyed by
113 * current time + duration. This means that the line which is due to expire the
114 * soonest is always pointed at by vector::begin(), so a simple while loop can
115 * very efficiently, very quickly and above all SAFELY pick off the first few
116 * items in the vector which need zapping.
125 /* Lists for temporary lines with an expiry time */
127 std::vector<KLine> klines;
128 std::vector<GLine> glines;
129 std::vector<ZLine> zlines;
130 std::vector<QLine> qlines;
131 std::vector<ELine> elines;
133 /* Seperate lists for perm XLines that isnt checked by expiry functions */
135 std::vector<KLine> pklines;
136 std::vector<GLine> pglines;
137 std::vector<ZLine> pzlines;
138 std::vector<QLine> pqlines;
139 std::vector<ELine> pelines;
142 bool GSortComparison ( const GLine one, const GLine two );
143 bool ZSortComparison ( const ZLine one, const ZLine two );
144 bool ESortComparison ( const ELine one, const ELine two );
145 bool QSortComparison ( const QLine one, const QLine two );
146 bool KSortComparison ( const KLine one, const KLine two );
148 // Reads the default bans from the config file.
149 // only a very small number of bans are defined
150 // this way these days, such as qlines against
151 // services nicks, etc.
153 void read_xline_defaults()
160 for (int i = 0; i < ConfValueEnum("badip",&config_f); i++)
162 ConfValue("badip","ipmask",i,ipmask,&config_f);
163 ConfValue("badip","reason",i,reason,&config_f);
164 add_zline(0,"<Config>",reason,ipmask);
165 log(DEBUG,"Read Z line (badip tag): ipmask=%s reason=%s",ipmask,reason);
168 for (int i = 0; i < ConfValueEnum("badnick",&config_f); i++)
170 ConfValue("badnick","nick",i,nick,&config_f);
171 ConfValue("badnick","reason",i,reason,&config_f);
172 add_qline(0,"<Config>",reason,nick);
173 log(DEBUG,"Read Q line (badnick tag): nick=%s reason=%s",nick,reason);
176 for (int i = 0; i < ConfValueEnum("badhost",&config_f); i++)
178 ConfValue("badhost","host",i,host,&config_f);
179 ConfValue("badhost","reason",i,reason,&config_f);
180 add_kline(0,"<Config>",reason,host);
181 log(DEBUG,"Read K line (badhost tag): host=%s reason=%s",host,reason);
183 for (int i = 0; i < ConfValueEnum("exception",&config_f); i++)
185 ConfValue("exception","host",i,host,&config_f);
186 ConfValue("exception","reason",i,reason,&config_f);
187 add_eline(0,"<Config>",reason,host);
188 log(DEBUG,"Read E line (exception tag): host=%s reason=%s",host,reason);
194 void add_gline(long duration, const char* source,const char* reason,const char* hostmask)
198 item.duration = duration;
199 strlcpy(item.hostmask,hostmask,199);
200 strlcpy(item.reason,reason,MAXBUF);
201 strlcpy(item.source,source,255);
203 item.set_time = TIME;
206 glines.push_back(item);
207 sort(glines.begin(), glines.end(),GSortComparison);
211 pglines.push_back(item);
215 // adds an e:line (exception to bans)
217 void add_eline(long duration, const char* source, const char* reason, const char* hostmask)
221 item.duration = duration;
222 strlcpy(item.hostmask,hostmask,199);
223 strlcpy(item.reason,reason,MAXBUF);
224 strlcpy(item.source,source,255);
226 item.set_time = TIME;
229 elines.push_back(item);
230 sort(elines.begin(), elines.end(),ESortComparison);
234 pelines.push_back(item);
240 void add_qline(long duration, const char* source, const char* reason, const char* nickname)
244 item.duration = duration;
245 strlcpy(item.nick,nickname,63);
246 strlcpy(item.reason,reason,MAXBUF);
247 strlcpy(item.source,source,255);
249 item.is_global = false;
250 item.set_time = TIME;
253 qlines.push_back(item);
254 sort(qlines.begin(), qlines.end(),QSortComparison);
258 pqlines.push_back(item);
264 void add_zline(long duration, const char* source, const char* reason, const char* ipaddr)
268 item.duration = duration;
269 if (strchr(ipaddr,'@'))
271 while (*ipaddr != '@')
275 strlcpy(item.ipaddr,ipaddr,39);
276 strlcpy(item.reason,reason,MAXBUF);
277 strlcpy(item.source,source,255);
279 item.is_global = false;
280 item.set_time = TIME;
283 zlines.push_back(item);
284 sort(zlines.begin(), zlines.end(),ZSortComparison);
288 pzlines.push_back(item);
294 void add_kline(long duration, const char* source, const char* reason, const char* hostmask)
298 item.duration = duration;
299 strlcpy(item.hostmask,hostmask,200);
300 strlcpy(item.reason,reason,MAXBUF);
301 strlcpy(item.source,source,255);
303 item.set_time = TIME;
306 klines.push_back(item);
307 sort(klines.begin(), klines.end(),KSortComparison);
311 pklines.push_back(item);
315 // deletes a g:line, returns true if the line existed and was removed
317 bool del_gline(const char* hostmask)
319 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
321 if (!strcasecmp(hostmask,i->hostmask))
327 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
329 if (!strcasecmp(hostmask,i->hostmask))
338 // deletes a e:line, returns true if the line existed and was removed
340 bool del_eline(const char* hostmask)
342 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
344 if (!strcasecmp(hostmask,i->hostmask))
350 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
352 if (!strcasecmp(hostmask,i->hostmask))
361 // deletes a q:line, returns true if the line existed and was removed
363 bool del_qline(const char* nickname)
365 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
367 if (!strcasecmp(nickname,i->nick))
373 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
375 if (!strcasecmp(nickname,i->nick))
384 bool qline_make_global(const char* nickname)
386 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
388 if (!strcasecmp(nickname,i->nick))
397 bool zline_make_global(const char* ipaddr)
399 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
401 if (!strcasecmp(ipaddr,i->ipaddr))
410 // deletes a z:line, returns true if the line existed and was removed
412 bool del_zline(const char* ipaddr)
414 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
416 if (!strcasecmp(ipaddr,i->ipaddr))
422 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
424 if (!strcasecmp(ipaddr,i->ipaddr))
433 // deletes a k:line, returns true if the line existed and was removed
435 bool del_kline(const char* hostmask)
437 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
439 if (!strcasecmp(hostmask,i->hostmask))
445 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
447 if (!strcasecmp(hostmask,i->hostmask))
456 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
458 char* matches_qline(const char* nick)
462 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
463 if (match(nick,i->nick))
465 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
466 if (match(nick,i->nick))
471 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
473 char* matches_gline(const char* host)
477 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
478 if (match(host,i->hostmask))
480 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
481 if (match(host,i->hostmask))
486 char* matches_exception(const char* host)
491 snprintf(host2,MAXBUF,"*@%s",host);
492 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
493 if ((match(host,i->hostmask)) || (match(host2,i->hostmask)))
495 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
496 if ((match(host,i->hostmask)) || (match(host2,i->hostmask)))
502 void gline_set_creation_time(char* host, time_t create_time)
504 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
506 if (!strcasecmp(host,i->hostmask))
508 i->set_time = create_time;
512 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
514 if (!strcasecmp(host,i->hostmask))
516 i->set_time = create_time;
523 void eline_set_creation_time(char* host, time_t create_time)
525 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
527 if (!strcasecmp(host,i->hostmask))
529 i->set_time = create_time;
533 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
535 if (!strcasecmp(host,i->hostmask))
537 i->set_time = create_time;
544 void qline_set_creation_time(char* nick, time_t create_time)
546 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
548 if (!strcasecmp(nick,i->nick))
550 i->set_time = create_time;
554 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
556 if (!strcasecmp(nick,i->nick))
558 i->set_time = create_time;
565 void zline_set_creation_time(char* ip, time_t create_time)
567 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
569 if (!strcasecmp(ip,i->ipaddr))
571 i->set_time = create_time;
575 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
577 if (!strcasecmp(ip,i->ipaddr))
579 i->set_time = create_time;
586 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
588 char* matches_zline(const char* ipaddr)
592 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
593 if (match(ipaddr,i->ipaddr))
595 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
596 if (match(ipaddr,i->ipaddr))
601 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
603 char* matches_kline(const char* host)
607 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
608 if (match(host,i->hostmask))
610 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
611 if (match(host,i->hostmask))
616 bool GSortComparison ( const GLine one, const GLine two )
618 return (one.duration + one.set_time) < (two.duration + two.set_time);
621 bool ESortComparison ( const ELine one, const ELine two )
623 return (one.duration + one.set_time) < (two.duration + two.set_time);
626 bool ZSortComparison ( const ZLine one, const ZLine two )
628 return (one.duration + one.set_time) < (two.duration + two.set_time);
631 bool KSortComparison ( const KLine one, const KLine two )
633 return (one.duration + one.set_time) < (two.duration + two.set_time);
636 bool QSortComparison ( const QLine one, const QLine two )
638 return (one.duration + one.set_time) < (two.duration + two.set_time);
641 // removes lines that have expired
645 time_t current = TIME;
647 /* Because we now store all our XLines in sorted order using (i->duration + i->set_time) as a key, this
648 * means that to expire the XLines we just need to do a while, picking off the top few until there are
649 * none left at the head of the queue that are after the current time.
652 while ((glines.size()) && (current > (glines.begin()->duration + glines.begin()->set_time)))
654 std::vector<GLine>::iterator i = glines.begin();
655 WriteOpers("Expiring timed G-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
659 while ((elines.size()) && (current > (elines.begin()->duration + elines.begin()->set_time)))
661 std::vector<ELine>::iterator i = elines.begin();
662 WriteOpers("Expiring timed E-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
666 while ((zlines.size()) && (current > (zlines.begin()->duration + zlines.begin()->set_time)))
668 std::vector<ZLine>::iterator i = zlines.begin();
669 WriteOpers("Expiring timed Z-Line %s (set by %s %d seconds ago)",i->ipaddr,i->source,i->duration);
673 while ((klines.size()) && (current > (klines.begin()->duration + klines.begin()->set_time)))
675 std::vector<KLine>::iterator i = klines.begin();
676 WriteOpers("Expiring timed K-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
680 while ((qlines.size()) && (current > (qlines.begin()->duration + qlines.begin()->set_time)))
682 std::vector<QLine>::iterator i = qlines.begin();
683 WriteOpers("Expiring timed Q-Line %s (set by %s %d seconds ago)",i->nick,i->source,i->duration);
689 // applies lines, removing clients and changing nicks etc as applicable
693 bool go_again = true;
697 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()))
703 for (user_hash::const_iterator u = clientlist.begin(); u != clientlist.end(); u++)
705 if (!strcasecmp(u->second->server,ServerName))
707 snprintf(host,MAXBUF,"%s@%s",u->second->ident,u->second->host);
710 // ignore people matching exempts
711 if (matches_exception(host))
714 if (glines.size() || pglines.size())
716 char* check = matches_gline(host);
719 WriteOpers("*** User %s matches G-Line: %s",u->second->registered == 7 ? u->second->nick:"<unknown>",check);
720 snprintf(reason,MAXBUF,"G-Lined: %s",check);
721 kill_link(u->second,reason);
726 if (klines.size() || pklines.size())
728 char* check = matches_kline(host);
731 WriteOpers("*** User %s matches K-Line: %s",u->second->registered == 7 ? u->second->nick:"<unknown>",check);
732 snprintf(reason,MAXBUF,"K-Lined: %s",check);
733 kill_link(u->second,reason);
738 if (qlines.size() || pqlines.size())
740 char* check = matches_qline(u->second->nick);
743 snprintf(reason,MAXBUF,"Matched Q-Lined nick: %s",check);
744 WriteOpers("*** Q-Lined nickname %s from %s: %s",u->second->registered == 7 ? u->second->nick:"<unknown>",u->second->host,check);
745 kill_link(u->second,reason);
750 if (zlines.size() || pzlines.size())
752 char* check = matches_zline(u->second->ip);
755 snprintf(reason,MAXBUF,"Z-Lined: %s",check);
756 WriteOpers("*** User %s matches Z-Line: %s",u->second->registered == 7 ? u->second->nick:"<unknown>",u->second->host,check);
757 kill_link(u->second,reason);
767 void stats_k(userrec* user)
769 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
770 WriteServ(user->fd,"216 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
771 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
772 WriteServ(user->fd,"216 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
775 void stats_g(userrec* user)
777 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
778 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
779 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
780 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
783 void stats_q(userrec* user)
785 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
786 WriteServ(user->fd,"217 %s :%s %d %d %s %s",user->nick,i->nick,i->set_time,i->duration,i->source,i->reason);
787 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
788 WriteServ(user->fd,"217 %s :%s %d %d %s %s",user->nick,i->nick,i->set_time,i->duration,i->source,i->reason);
791 void stats_z(userrec* user)
793 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
794 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->ipaddr,i->set_time,i->duration,i->source,i->reason);
795 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
796 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->ipaddr,i->set_time,i->duration,i->source,i->reason);
799 void stats_e(userrec* user)
801 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
802 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
803 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
804 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);