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>
24 #include <sys/ioctl.h>
25 #include <sys/utsname.h>
30 #include <ext/hash_map>
42 #include "connection.h"
53 #include "inspstring.h"
56 #define nspace __gnu_cxx
65 extern std::vector<Module*> modules;
66 extern std::vector<ircd_module*> factory;
69 extern char ServerName[MAXBUF];
70 extern char Network[MAXBUF];
71 extern char ServerDesc[MAXBUF];
72 extern char AdminName[MAXBUF];
73 extern char AdminEmail[MAXBUF];
74 extern char AdminNick[MAXBUF];
75 extern char diepass[MAXBUF];
76 extern char restartpass[MAXBUF];
77 extern char motd[MAXBUF];
78 extern char rules[MAXBUF];
79 extern char list[MAXBUF];
80 extern char PrefixQuit[MAXBUF];
81 extern char DieValue[MAXBUF];
84 extern int WHOWAS_STALE;
85 extern int WHOWAS_MAX;
87 extern time_t startup_time;
88 extern int NetBufferSize;
89 extern time_t nb_start;
91 extern std::vector<int> fd_reap;
92 extern std::vector<std::string> module_names;
94 extern int boundPortCount;
96 extern int UDPportCount;
97 extern int ports[MAXSOCKS];
98 extern int defaultRoute;
100 extern std::vector<long> auth_cookies;
101 extern std::stringstream config_f;
103 extern serverrec* me[32];
105 extern FILE *log_file;
110 template<> struct hash<in_addr>
112 template<> struct nspace::hash<in_addr>
115 size_t operator()(const struct in_addr &a) const
118 memcpy(&q,&a,sizeof(size_t));
123 template<> struct hash<string>
125 template<> struct nspace::hash<string>
128 size_t operator()(const string &s) const
131 static struct hash<const char *> strhash;
132 strlcpy(a,s.c_str(),MAXBUF);
143 bool operator()(const string& s1, const string& s2) const
145 char a[MAXBUF],b[MAXBUF];
146 strlcpy(a,s1.c_str(),MAXBUF);
147 strlcpy(b,s2.c_str(),MAXBUF);
148 return (strcasecmp(a,b) == 0);
153 struct InAddr_HashComp
156 bool operator()(const in_addr &s1, const in_addr &s2) const
161 memcpy(&q,&s1,sizeof(size_t));
162 memcpy(&p,&s2,sizeof(size_t));
170 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
171 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
172 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
173 typedef std::deque<command_t> command_table;
176 extern user_hash clientlist;
177 extern chan_hash chanlist;
178 extern user_hash whowas;
179 extern command_table cmdlist;
180 extern file_cache MOTD;
181 extern file_cache RULES;
182 extern address_cache IP;
185 std::vector<KLine> klines;
186 std::vector<GLine> glines;
187 std::vector<ZLine> zlines;
188 std::vector<QLine> qlines;
189 std::vector<ELine> elines;
191 // Reads the default bans from the config file.
192 // only a very small number of bans are defined
193 // this way these days, such as qlines against
194 // services nicks, etc.
196 void read_xline_defaults()
203 for (int i = 0; i < ConfValueEnum("badip",&config_f); i++)
205 ConfValue("badip","ipmask",i,ipmask,&config_f);
206 ConfValue("badip","reason",i,reason,&config_f);
207 add_zline(0,"<Config>",reason,ipmask);
208 log(DEBUG,"Read Z line (badip tag): ipmask=%s reason=%s",ipmask,reason);
211 for (int i = 0; i < ConfValueEnum("badnick",&config_f); i++)
213 ConfValue("badnick","nick",i,nick,&config_f);
214 ConfValue("badnick","reason",i,reason,&config_f);
215 add_qline(0,"<Config>",reason,nick);
216 log(DEBUG,"Read Q line (badnick tag): nick=%s reason=%s",nick,reason);
219 for (int i = 0; i < ConfValueEnum("badhost",&config_f); i++)
221 ConfValue("badhost","host",i,host,&config_f);
222 ConfValue("badhost","reason",i,reason,&config_f);
223 add_kline(0,"<Config>",reason,host);
224 log(DEBUG,"Read K line (badhost tag): host=%s reason=%s",host,reason);
226 for (int i = 0; i < ConfValueEnum("exception",&config_f); i++)
228 ConfValue("exception","host",i,host,&config_f);
229 ConfValue("exception","reason",i,reason,&config_f);
230 add_eline(0,"<Config>",reason,host);
231 log(DEBUG,"Read E line (exception tag): host=%s reason=%s",host,reason);
237 void add_gline(long duration, char* source, char* reason, char* hostmask)
241 item.duration = duration;
242 strlcpy(item.hostmask,hostmask,MAXBUF);
243 strlcpy(item.reason,reason,MAXBUF);
244 strlcpy(item.source,source,MAXBUF);
246 item.set_time = time(NULL);
247 glines.push_back(item);
250 // adds an e:line (exception to bans)
252 void add_eline(long duration, char* source, char* reason, char* hostmask)
256 item.duration = duration;
257 strlcpy(item.hostmask,hostmask,MAXBUF);
258 strlcpy(item.reason,reason,MAXBUF);
259 strlcpy(item.source,source,MAXBUF);
261 item.set_time = time(NULL);
262 elines.push_back(item);
267 void add_qline(long duration, char* source, char* reason, char* nickname)
271 item.duration = duration;
272 strlcpy(item.nick,nickname,MAXBUF);
273 strlcpy(item.reason,reason,MAXBUF);
274 strlcpy(item.source,source,MAXBUF);
276 item.is_global = false;
277 item.set_time = time(NULL);
278 qlines.push_back(item);
283 void add_zline(long duration, char* source, char* reason, char* ipaddr)
287 item.duration = duration;
288 strlcpy(item.ipaddr,ipaddr,MAXBUF);
289 strlcpy(item.reason,reason,MAXBUF);
290 strlcpy(item.source,source,MAXBUF);
292 item.is_global = false;
293 item.set_time = time(NULL);
294 zlines.push_back(item);
299 void add_kline(long duration, char* source, char* reason, char* hostmask)
303 item.duration = duration;
304 strlcpy(item.hostmask,hostmask,MAXBUF);
305 strlcpy(item.reason,reason,MAXBUF);
306 strlcpy(item.source,source,MAXBUF);
308 item.set_time = time(NULL);
309 klines.push_back(item);
312 // deletes a g:line, returns true if the line existed and was removed
314 bool del_gline(char* hostmask)
316 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
318 if (!strcasecmp(hostmask,i->hostmask))
327 // deletes a e:line, returns true if the line existed and was removed
329 bool del_eline(char* hostmask)
331 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
333 if (!strcasecmp(hostmask,i->hostmask))
342 // deletes a q:line, returns true if the line existed and was removed
344 bool del_qline(char* nickname)
346 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
348 if (!strcasecmp(nickname,i->nick))
357 bool qline_make_global(char* nickname)
359 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
361 if (!strcasecmp(nickname,i->nick))
370 bool zline_make_global(char* ipaddr)
372 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
374 if (!strcasecmp(ipaddr,i->ipaddr))
383 void sync_xlines(serverrec* serv, char* tcp_host)
387 // for zlines and qlines, we should first check if theyre global...
388 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
392 snprintf(data,MAXBUF,"} %s %s %ld %ld :%s",i->ipaddr,i->source,i->set_time,i->duration,i->reason);
393 serv->SendPacket(data,tcp_host);
396 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
400 snprintf(data,MAXBUF,"{ %s %s %ld %ld :%s",i->nick,i->source,i->set_time,i->duration,i->reason);
401 serv->SendPacket(data,tcp_host);
404 // glines are always global, so no need to check
405 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
407 snprintf(data,MAXBUF,"# %s %s %ld %ld :%s",i->hostmask,i->source,i->set_time,i->duration,i->reason);
408 serv->SendPacket(data,tcp_host);
413 // deletes a z:line, returns true if the line existed and was removed
415 bool del_zline(char* ipaddr)
417 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
419 if (!strcasecmp(ipaddr,i->ipaddr))
428 // deletes a k:line, returns true if the line existed and was removed
430 bool del_kline(char* hostmask)
432 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
434 if (!strcasecmp(hostmask,i->hostmask))
443 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
445 char* matches_qline(const char* nick)
447 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
449 if (match(nick,i->nick))
457 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
459 char* matches_gline(const char* host)
461 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
463 if (match(host,i->hostmask))
471 char* matches_exception(const char* host)
474 snprintf(host2,MAXBUF,"*@%s",host);
475 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
477 if ((match(host,i->hostmask)) || (match(host2,i->hostmask)))
486 void gline_set_creation_time(char* host, time_t create_time)
488 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
490 if (!strcasecmp(host,i->hostmask))
492 i->set_time = create_time;
499 void qline_set_creation_time(char* nick, time_t create_time)
501 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
503 if (!strcasecmp(nick,i->nick))
505 i->set_time = create_time;
512 void zline_set_creation_time(char* ip, time_t create_time)
514 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
516 if (!strcasecmp(ip,i->ipaddr))
518 i->set_time = create_time;
525 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
527 char* matches_zline(const char* ipaddr)
529 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
531 if (match(ipaddr,i->ipaddr))
539 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
541 char* matches_kline(const char* host)
543 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
545 if (match(host,i->hostmask))
553 // removes lines that have expired
557 bool go_again = true;
558 time_t current = time(NULL);
560 // because we mess up an iterator when we remove from the vector, we must bail from
561 // the loop early if we delete an item, therefore this outer while loop is required.
566 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
568 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
570 WriteOpers("Expiring timed K-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
577 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
579 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
581 WriteOpers("Expiring timed E-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
588 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
590 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
592 WriteOpers("Expiring timed G-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
599 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
601 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
603 WriteOpers("Expiring timed Z-Line %s (set by %s %d seconds ago)",i->ipaddr,i->source,i->duration);
610 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
612 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
614 WriteOpers("Expiring timed Q-Line %s (set by %s %d seconds ago)",i->nick,i->source,i->duration);
623 // applies lines, removing clients and changing nicks etc as applicable
627 bool go_again = true;
631 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()))
637 for (user_hash::const_iterator u = clientlist.begin(); u != clientlist.end(); u++)
639 if (!strcasecmp(u->second->server,ServerName))
641 snprintf(host,MAXBUF,"%s@%s",u->second->ident,u->second->host);
644 // ignore people matching exempts
645 if (matches_exception(host))
650 char* check = matches_gline(host);
653 WriteOpers("*** User %s matches G-Line: %s",u->second->nick,check);
654 snprintf(reason,MAXBUF,"G-Lined: %s",check);
655 kill_link(u->second,reason);
662 char* check = matches_kline(host);
665 WriteOpers("*** User %s matches K-Line: %s",u->second->nick,check);
666 snprintf(reason,MAXBUF,"K-Lined: %s",check);
667 kill_link(u->second,reason);
674 char* check = matches_qline(u->second->nick);
677 snprintf(reason,MAXBUF,"Matched Q-Lined nick: %s",check);
678 WriteOpers("*** Q-Lined nickname %s from %s: %s",u->second->nick,u->second->host,check);
679 kill_link(u->second,reason);
686 char* check = matches_zline(u->second->ip);
689 snprintf(reason,MAXBUF,"Z-Lined: %s",check);
690 WriteOpers("*** User %s matches Z-Line: %s",u->second->nick,u->second->host,check);
691 kill_link(u->second,reason);
701 void stats_k(userrec* user)
703 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
705 WriteServ(user->fd,"216 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
709 void stats_g(userrec* user)
711 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
713 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
717 void stats_q(userrec* user)
719 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
721 WriteServ(user->fd,"217 %s :%s %d %d %s %s",user->nick,i->nick,i->set_time,i->duration,i->source,i->reason);
725 void stats_z(userrec* user)
727 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
729 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->ipaddr,i->set_time,i->duration,i->source,i->reason);
733 void stats_e(userrec* user)
735 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
737 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);