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 * ---------------------------------------------------
18 #include "inspircd_io.h"
19 #include "inspircd_util.h"
20 #include "inspircd_config.h"
23 #include <sys/errno.h>
27 #include <ext/hash_map>
35 #include "connection.h"
46 #include "inspstring.h"
47 #include "helperfuncs.h"
53 extern std::vector<Module*> modules;
54 extern std::vector<ircd_module*> factory;
57 extern char ServerName[MAXBUF];
58 extern char Network[MAXBUF];
59 extern char ServerDesc[MAXBUF];
60 extern char AdminName[MAXBUF];
61 extern char AdminEmail[MAXBUF];
62 extern char AdminNick[MAXBUF];
63 extern char diepass[MAXBUF];
64 extern char restartpass[MAXBUF];
65 extern char motd[MAXBUF];
66 extern char rules[MAXBUF];
67 extern char list[MAXBUF];
68 extern char PrefixQuit[MAXBUF];
69 extern char DieValue[MAXBUF];
72 extern int WHOWAS_STALE;
73 extern int WHOWAS_MAX;
75 extern time_t startup_time;
76 extern int NetBufferSize;
77 extern time_t nb_start;
79 extern std::vector<int> fd_reap;
80 extern std::vector<std::string> module_names;
82 extern int boundPortCount;
84 extern int SERVERportCount;
85 extern int ports[MAXSOCKS];
86 extern int defaultRoute;
88 extern std::vector<long> auth_cookies;
89 extern std::stringstream config_f;
91 extern serverrec* me[32];
93 extern FILE *log_file;
95 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
96 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
97 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
98 typedef nspace::hash_map<std::string, WhoWasUser*, nspace::hash<string>, StrHashComp> whowas_hash;
99 typedef std::deque<command_t> command_table;
102 extern user_hash clientlist;
103 extern chan_hash chanlist;
104 extern whowas_hash whowas;
105 extern command_table cmdlist;
106 extern file_cache MOTD;
107 extern file_cache RULES;
108 extern address_cache IP;
112 std::vector<KLine> klines;
113 std::vector<GLine> glines;
114 std::vector<ZLine> zlines;
115 std::vector<QLine> qlines;
116 std::vector<ELine> elines;
118 // Reads the default bans from the config file.
119 // only a very small number of bans are defined
120 // this way these days, such as qlines against
121 // services nicks, etc.
123 void read_xline_defaults()
130 for (int i = 0; i < ConfValueEnum("badip",&config_f); i++)
132 ConfValue("badip","ipmask",i,ipmask,&config_f);
133 ConfValue("badip","reason",i,reason,&config_f);
134 add_zline(0,"<Config>",reason,ipmask);
135 log(DEBUG,"Read Z line (badip tag): ipmask=%s reason=%s",ipmask,reason);
138 for (int i = 0; i < ConfValueEnum("badnick",&config_f); i++)
140 ConfValue("badnick","nick",i,nick,&config_f);
141 ConfValue("badnick","reason",i,reason,&config_f);
142 add_qline(0,"<Config>",reason,nick);
143 log(DEBUG,"Read Q line (badnick tag): nick=%s reason=%s",nick,reason);
146 for (int i = 0; i < ConfValueEnum("badhost",&config_f); i++)
148 ConfValue("badhost","host",i,host,&config_f);
149 ConfValue("badhost","reason",i,reason,&config_f);
150 add_kline(0,"<Config>",reason,host);
151 log(DEBUG,"Read K line (badhost tag): host=%s reason=%s",host,reason);
153 for (int i = 0; i < ConfValueEnum("exception",&config_f); i++)
155 ConfValue("exception","host",i,host,&config_f);
156 ConfValue("exception","reason",i,reason,&config_f);
157 add_eline(0,"<Config>",reason,host);
158 log(DEBUG,"Read E line (exception tag): host=%s reason=%s",host,reason);
164 void add_gline(long duration, const char* source,const char* reason,const char* hostmask)
168 item.duration = duration;
169 strlcpy(item.hostmask,hostmask,199);
170 strlcpy(item.reason,reason,MAXBUF);
171 strlcpy(item.source,source,255);
173 item.set_time = TIME;
174 glines.push_back(item);
177 // adds an e:line (exception to bans)
179 void add_eline(long duration, const char* source, const char* reason, const char* hostmask)
183 item.duration = duration;
184 strlcpy(item.hostmask,hostmask,199);
185 strlcpy(item.reason,reason,MAXBUF);
186 strlcpy(item.source,source,255);
188 item.set_time = TIME;
189 elines.push_back(item);
194 void add_qline(long duration, const char* source, const char* reason, const char* nickname)
198 item.duration = duration;
199 strlcpy(item.nick,nickname,63);
200 strlcpy(item.reason,reason,MAXBUF);
201 strlcpy(item.source,source,255);
203 item.is_global = false;
204 item.set_time = TIME;
205 qlines.push_back(item);
210 void add_zline(long duration, const char* source, const char* reason, const char* ipaddr)
214 item.duration = duration;
215 if (strchr(ipaddr,'@'))
217 while (*ipaddr != '@')
221 strlcpy(item.ipaddr,ipaddr,39);
222 strlcpy(item.reason,reason,MAXBUF);
223 strlcpy(item.source,source,255);
225 item.is_global = false;
226 item.set_time = TIME;
227 zlines.push_back(item);
232 void add_kline(long duration, const char* source, const char* reason, const char* hostmask)
236 item.duration = duration;
237 strlcpy(item.hostmask,hostmask,200);
238 strlcpy(item.reason,reason,MAXBUF);
239 strlcpy(item.source,source,255);
241 item.set_time = TIME;
242 klines.push_back(item);
245 // deletes a g:line, returns true if the line existed and was removed
247 bool del_gline(const char* hostmask)
249 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
251 if (!strcasecmp(hostmask,i->hostmask))
260 // deletes a e:line, returns true if the line existed and was removed
262 bool del_eline(const char* hostmask)
264 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
266 if (!strcasecmp(hostmask,i->hostmask))
275 // deletes a q:line, returns true if the line existed and was removed
277 bool del_qline(const char* nickname)
279 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
281 if (!strcasecmp(nickname,i->nick))
290 bool qline_make_global(const char* nickname)
292 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
294 if (!strcasecmp(nickname,i->nick))
303 bool zline_make_global(const char* ipaddr)
305 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
307 if (!strcasecmp(ipaddr,i->ipaddr))
316 void sync_xlines(serverrec* serv, char* tcp_host)
320 // for zlines and qlines, we should first check if theyre global...
321 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
325 snprintf(data,MAXBUF,"%s } %s %s %lu %lu :%s",CreateSum().c_str(),i->ipaddr,i->source,(unsigned long)i->set_time,(unsigned long)i->duration,i->reason);
326 serv->SendPacket(data,tcp_host);
329 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
333 snprintf(data,MAXBUF,"%s { %s %s %lu %lu :%s",CreateSum().c_str(),i->nick,i->source,(unsigned long)i->set_time,(unsigned long)i->duration,i->reason);
334 serv->SendPacket(data,tcp_host);
337 // glines are always global, so no need to check
338 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
340 snprintf(data,MAXBUF,"%s # %s %s %lu %lu :%s",CreateSum().c_str(),i->hostmask,i->source,(unsigned long)i->set_time,(unsigned long)i->duration,i->reason);
341 serv->SendPacket(data,tcp_host);
346 // deletes a z:line, returns true if the line existed and was removed
348 bool del_zline(const char* ipaddr)
350 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
352 if (!strcasecmp(ipaddr,i->ipaddr))
361 // deletes a k:line, returns true if the line existed and was removed
363 bool del_kline(const char* hostmask)
365 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
367 if (!strcasecmp(hostmask,i->hostmask))
376 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
378 char* matches_qline(const char* nick)
382 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
384 if (match(nick,i->nick))
392 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
394 char* matches_gline(const char* host)
398 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
400 if (match(host,i->hostmask))
408 char* matches_exception(const char* host)
413 snprintf(host2,MAXBUF,"*@%s",host);
414 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
416 if ((match(host,i->hostmask)) || (match(host2,i->hostmask)))
425 void gline_set_creation_time(char* host, time_t create_time)
427 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
429 if (!strcasecmp(host,i->hostmask))
431 i->set_time = create_time;
438 void qline_set_creation_time(char* nick, time_t create_time)
440 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
442 if (!strcasecmp(nick,i->nick))
444 i->set_time = create_time;
451 void zline_set_creation_time(char* ip, time_t create_time)
453 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
455 if (!strcasecmp(ip,i->ipaddr))
457 i->set_time = create_time;
464 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
466 char* matches_zline(const char* ipaddr)
470 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
472 if (match(ipaddr,i->ipaddr))
480 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
482 char* matches_kline(const char* host)
486 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
488 if (match(host,i->hostmask))
496 // removes lines that have expired
500 bool go_again = true;
501 time_t current = TIME;
503 // because we mess up an iterator when we remove from the vector, we must bail from
504 // the loop early if we delete an item, therefore this outer while loop is required.
509 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
511 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
513 WriteOpers("Expiring timed K-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
520 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
522 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
524 WriteOpers("Expiring timed E-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
531 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
533 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
535 WriteOpers("Expiring timed G-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
542 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
544 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
546 WriteOpers("Expiring timed Z-Line %s (set by %s %d seconds ago)",i->ipaddr,i->source,i->duration);
553 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
555 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
557 WriteOpers("Expiring timed Q-Line %s (set by %s %d seconds ago)",i->nick,i->source,i->duration);
566 // applies lines, removing clients and changing nicks etc as applicable
570 bool go_again = true;
574 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()))
580 for (user_hash::const_iterator u = clientlist.begin(); u != clientlist.end(); u++)
582 if (!strcasecmp(u->second->server,ServerName))
584 snprintf(host,MAXBUF,"%s@%s",u->second->ident,u->second->host);
587 // ignore people matching exempts
588 if (matches_exception(host))
593 char* check = matches_gline(host);
596 WriteOpers("*** User %s matches G-Line: %s",u->second->registered == 7 ? u->second->nick:"<unknown>",check);
597 snprintf(reason,MAXBUF,"G-Lined: %s",check);
598 kill_link(u->second,reason);
605 char* check = matches_kline(host);
608 WriteOpers("*** User %s matches K-Line: %s",u->second->registered == 7 ? u->second->nick:"<unknown>",check);
609 snprintf(reason,MAXBUF,"K-Lined: %s",check);
610 kill_link(u->second,reason);
617 char* check = matches_qline(u->second->nick);
620 snprintf(reason,MAXBUF,"Matched Q-Lined nick: %s",check);
621 WriteOpers("*** Q-Lined nickname %s from %s: %s",u->second->registered == 7 ? u->second->nick:"<unknown>",u->second->host,check);
622 kill_link(u->second,reason);
629 char* check = matches_zline(u->second->ip);
632 snprintf(reason,MAXBUF,"Z-Lined: %s",check);
633 WriteOpers("*** User %s matches Z-Line: %s",u->second->registered == 7 ? u->second->nick:"<unknown>",u->second->host,check);
634 kill_link(u->second,reason);
644 void stats_k(userrec* user)
646 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
648 WriteServ(user->fd,"216 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
652 void stats_g(userrec* user)
654 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
656 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
660 void stats_q(userrec* user)
662 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
664 WriteServ(user->fd,"217 %s :%s %d %d %s %s",user->nick,i->nick,i->set_time,i->duration,i->source,i->reason);
668 void stats_z(userrec* user)
670 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
672 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->ipaddr,i->set_time,i->duration,i->source,i->reason);
676 void stats_e(userrec* user)
678 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
680 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);