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);
150 return (strcasecmp(a,b) == 0);
155 struct InAddr_HashComp
158 bool operator()(const in_addr &s1, const in_addr &s2) const
163 memcpy(&q,&s1,sizeof(size_t));
164 memcpy(&p,&s2,sizeof(size_t));
172 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
173 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
174 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
175 typedef std::deque<command_t> command_table;
178 extern user_hash clientlist;
179 extern chan_hash chanlist;
180 extern user_hash whowas;
181 extern command_table cmdlist;
182 extern file_cache MOTD;
183 extern file_cache RULES;
184 extern address_cache IP;
188 std::vector<KLine> klines;
189 std::vector<GLine> glines;
190 std::vector<ZLine> zlines;
191 std::vector<QLine> qlines;
192 std::vector<ELine> elines;
194 // Reads the default bans from the config file.
195 // only a very small number of bans are defined
196 // this way these days, such as qlines against
197 // services nicks, etc.
199 void read_xline_defaults()
206 for (int i = 0; i < ConfValueEnum("badip",&config_f); i++)
208 ConfValue("badip","ipmask",i,ipmask,&config_f);
209 ConfValue("badip","reason",i,reason,&config_f);
210 add_zline(0,"<Config>",reason,ipmask);
211 log(DEBUG,"Read Z line (badip tag): ipmask=%s reason=%s",ipmask,reason);
214 for (int i = 0; i < ConfValueEnum("badnick",&config_f); i++)
216 ConfValue("badnick","nick",i,nick,&config_f);
217 ConfValue("badnick","reason",i,reason,&config_f);
218 add_qline(0,"<Config>",reason,nick);
219 log(DEBUG,"Read Q line (badnick tag): nick=%s reason=%s",nick,reason);
222 for (int i = 0; i < ConfValueEnum("badhost",&config_f); i++)
224 ConfValue("badhost","host",i,host,&config_f);
225 ConfValue("badhost","reason",i,reason,&config_f);
226 add_kline(0,"<Config>",reason,host);
227 log(DEBUG,"Read K line (badhost tag): host=%s reason=%s",host,reason);
229 for (int i = 0; i < ConfValueEnum("exception",&config_f); i++)
231 ConfValue("exception","host",i,host,&config_f);
232 ConfValue("exception","reason",i,reason,&config_f);
233 add_eline(0,"<Config>",reason,host);
234 log(DEBUG,"Read E line (exception tag): host=%s reason=%s",host,reason);
240 void add_gline(long duration, const char* source,const char* reason,const char* hostmask)
244 item.duration = duration;
245 strlcpy(item.hostmask,hostmask,MAXBUF);
246 strlcpy(item.reason,reason,MAXBUF);
247 strlcpy(item.source,source,MAXBUF);
249 item.set_time = TIME;
250 glines.push_back(item);
253 // adds an e:line (exception to bans)
255 void add_eline(long duration, const char* source, const char* reason, const char* hostmask)
259 item.duration = duration;
260 strlcpy(item.hostmask,hostmask,MAXBUF);
261 strlcpy(item.reason,reason,MAXBUF);
262 strlcpy(item.source,source,MAXBUF);
264 item.set_time = TIME;
265 elines.push_back(item);
270 void add_qline(long duration, const char* source, const char* reason, const char* nickname)
274 item.duration = duration;
275 strlcpy(item.nick,nickname,MAXBUF);
276 strlcpy(item.reason,reason,MAXBUF);
277 strlcpy(item.source,source,MAXBUF);
279 item.is_global = false;
280 item.set_time = TIME;
281 qlines.push_back(item);
286 void add_zline(long duration, const char* source, const char* reason, const char* ipaddr)
290 item.duration = duration;
291 strlcpy(item.ipaddr,ipaddr,MAXBUF);
292 strlcpy(item.reason,reason,MAXBUF);
293 strlcpy(item.source,source,MAXBUF);
295 item.is_global = false;
296 item.set_time = TIME;
297 zlines.push_back(item);
302 void add_kline(long duration, const char* source, const char* reason, const char* hostmask)
306 item.duration = duration;
307 strlcpy(item.hostmask,hostmask,MAXBUF);
308 strlcpy(item.reason,reason,MAXBUF);
309 strlcpy(item.source,source,MAXBUF);
311 item.set_time = TIME;
312 klines.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))
330 // deletes a e:line, returns true if the line existed and was removed
332 bool del_eline(const char* hostmask)
334 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
336 if (!strcasecmp(hostmask,i->hostmask))
345 // deletes a q:line, returns true if the line existed and was removed
347 bool del_qline(const char* nickname)
349 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
351 if (!strcasecmp(nickname,i->nick))
360 bool qline_make_global(const char* nickname)
362 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
364 if (!strcasecmp(nickname,i->nick))
373 bool zline_make_global(const char* ipaddr)
375 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
377 if (!strcasecmp(ipaddr,i->ipaddr))
386 void sync_xlines(serverrec* serv, char* tcp_host)
390 // for zlines and qlines, we should first check if theyre global...
391 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
395 snprintf(data,MAXBUF,"} %s %s %ld %ld :%s",i->ipaddr,i->source,i->set_time,i->duration,i->reason);
396 serv->SendPacket(data,tcp_host);
399 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
403 snprintf(data,MAXBUF,"{ %s %s %ld %ld :%s",i->nick,i->source,i->set_time,i->duration,i->reason);
404 serv->SendPacket(data,tcp_host);
407 // glines are always global, so no need to check
408 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
410 snprintf(data,MAXBUF,"# %s %s %ld %ld :%s",i->hostmask,i->source,i->set_time,i->duration,i->reason);
411 serv->SendPacket(data,tcp_host);
416 // deletes a z:line, returns true if the line existed and was removed
418 bool del_zline(const char* ipaddr)
420 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
422 if (!strcasecmp(ipaddr,i->ipaddr))
431 // deletes a k:line, returns true if the line existed and was removed
433 bool del_kline(const char* hostmask)
435 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
437 if (!strcasecmp(hostmask,i->hostmask))
446 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
448 char* matches_qline(const char* nick)
452 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
454 if (match(nick,i->nick))
462 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
464 char* matches_gline(const char* host)
468 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
470 if (match(host,i->hostmask))
478 char* matches_exception(const char* host)
483 snprintf(host2,MAXBUF,"*@%s",host);
484 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
486 if ((match(host,i->hostmask)) || (match(host2,i->hostmask)))
495 void gline_set_creation_time(char* host, time_t create_time)
497 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
499 if (!strcasecmp(host,i->hostmask))
501 i->set_time = create_time;
508 void qline_set_creation_time(char* nick, time_t create_time)
510 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.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;
534 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
536 char* matches_zline(const char* ipaddr)
540 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
542 if (match(ipaddr,i->ipaddr))
550 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
552 char* matches_kline(const char* host)
556 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
558 if (match(host,i->hostmask))
566 // removes lines that have expired
570 bool go_again = true;
571 time_t current = TIME;
573 // because we mess up an iterator when we remove from the vector, we must bail from
574 // the loop early if we delete an item, therefore this outer while loop is required.
579 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
581 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
583 WriteOpers("Expiring timed K-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
590 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
592 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
594 WriteOpers("Expiring timed E-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
601 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
603 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
605 WriteOpers("Expiring timed G-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
612 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
614 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
616 WriteOpers("Expiring timed Z-Line %s (set by %s %d seconds ago)",i->ipaddr,i->source,i->duration);
623 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
625 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
627 WriteOpers("Expiring timed Q-Line %s (set by %s %d seconds ago)",i->nick,i->source,i->duration);
636 // applies lines, removing clients and changing nicks etc as applicable
640 bool go_again = true;
644 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()))
650 for (user_hash::const_iterator u = clientlist.begin(); u != clientlist.end(); u++)
652 if (!strcasecmp(u->second->server,ServerName))
654 snprintf(host,MAXBUF,"%s@%s",u->second->ident,u->second->host);
657 // ignore people matching exempts
658 if (matches_exception(host))
663 char* check = matches_gline(host);
666 WriteOpers("*** User %s matches G-Line: %s",u->second->nick,check);
667 snprintf(reason,MAXBUF,"G-Lined: %s",check);
668 kill_link(u->second,reason);
675 char* check = matches_kline(host);
678 WriteOpers("*** User %s matches K-Line: %s",u->second->nick,check);
679 snprintf(reason,MAXBUF,"K-Lined: %s",check);
680 kill_link(u->second,reason);
687 char* check = matches_qline(u->second->nick);
690 snprintf(reason,MAXBUF,"Matched Q-Lined nick: %s",check);
691 WriteOpers("*** Q-Lined nickname %s from %s: %s",u->second->nick,u->second->host,check);
692 kill_link(u->second,reason);
699 char* check = matches_zline(u->second->ip);
702 snprintf(reason,MAXBUF,"Z-Lined: %s",check);
703 WriteOpers("*** User %s matches Z-Line: %s",u->second->nick,u->second->host,check);
704 kill_link(u->second,reason);
714 void stats_k(userrec* user)
716 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
718 WriteServ(user->fd,"216 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
722 void stats_g(userrec* user)
724 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
726 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
730 void stats_q(userrec* user)
732 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
734 WriteServ(user->fd,"217 %s :%s %d %d %s %s",user->nick,i->nick,i->set_time,i->duration,i->source,i->reason);
738 void stats_z(userrec* user)
740 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
742 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->ipaddr,i->set_time,i->duration,i->source,i->reason);
746 void stats_e(userrec* user)
748 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
750 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);