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"
55 #define nspace __gnu_cxx
64 extern std::vector<Module*> modules;
65 extern std::vector<ircd_module*> factory;
68 extern char ServerName[MAXBUF];
69 extern char Network[MAXBUF];
70 extern char ServerDesc[MAXBUF];
71 extern char AdminName[MAXBUF];
72 extern char AdminEmail[MAXBUF];
73 extern char AdminNick[MAXBUF];
74 extern char diepass[MAXBUF];
75 extern char restartpass[MAXBUF];
76 extern char motd[MAXBUF];
77 extern char rules[MAXBUF];
78 extern char list[MAXBUF];
79 extern char PrefixQuit[MAXBUF];
80 extern char DieValue[MAXBUF];
83 extern int WHOWAS_STALE;
84 extern int WHOWAS_MAX;
86 extern time_t startup_time;
87 extern int NetBufferSize;
88 extern time_t nb_start;
90 extern std::vector<int> fd_reap;
91 extern std::vector<std::string> module_names;
93 extern int boundPortCount;
95 extern int UDPportCount;
96 extern int ports[MAXSOCKS];
97 extern int defaultRoute;
99 extern std::vector<long> auth_cookies;
100 extern std::stringstream config_f;
102 extern serverrec* me[32];
104 extern FILE *log_file;
109 template<> struct hash<in_addr>
111 template<> struct nspace::hash<in_addr>
114 size_t operator()(const struct in_addr &a) const
117 memcpy(&q,&a,sizeof(size_t));
122 template<> struct hash<string>
124 template<> struct nspace::hash<string>
127 size_t operator()(const string &s) const
130 static struct hash<const char *> strhash;
131 strlcpy(a,s.c_str(),MAXBUF);
142 bool operator()(const string& s1, const string& s2) const
144 char a[MAXBUF],b[MAXBUF];
145 strlcpy(a,s1.c_str(),MAXBUF);
146 strlcpy(b,s2.c_str(),MAXBUF);
147 return (strcasecmp(a,b) == 0);
152 struct InAddr_HashComp
155 bool operator()(const in_addr &s1, const in_addr &s2) const
160 memcpy(&q,&s1,sizeof(size_t));
161 memcpy(&p,&s2,sizeof(size_t));
169 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
170 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
171 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
172 typedef std::deque<command_t> command_table;
175 extern user_hash clientlist;
176 extern chan_hash chanlist;
177 extern user_hash whowas;
178 extern command_table cmdlist;
179 extern file_cache MOTD;
180 extern file_cache RULES;
181 extern address_cache IP;
184 std::vector<KLine> klines;
185 std::vector<GLine> glines;
186 std::vector<ZLine> zlines;
187 std::vector<QLine> qlines;
189 // Reads the default bans from the config file.
190 // only a very small number of bans are defined
191 // this way these days, such as qlines against
192 // services nicks, etc.
194 void read_xline_defaults()
201 for (int i = 0; i < ConfValueEnum("badip",&config_f); i++)
203 ConfValue("badip","ipmask",i,ipmask,&config_f);
204 ConfValue("badip","reason",i,reason,&config_f);
205 add_zline(0,"<Config>",reason,ipmask);
206 log(DEBUG,"Read Z line (badip tag): ipmask=%s reason=%s",ipmask,reason);
209 for (int i = 0; i < ConfValueEnum("badnick",&config_f); i++)
211 ConfValue("badnick","nick",i,nick,&config_f);
212 ConfValue("badnick","reason",i,reason,&config_f);
213 add_qline(0,"<Config>",reason,nick);
214 log(DEBUG,"Read Q line (badnick tag): nick=%s reason=%s",nick,reason);
217 for (int i = 0; i < ConfValueEnum("badhost",&config_f); i++)
219 ConfValue("badhost","host",i,host,&config_f);
220 ConfValue("badhost","reason",i,reason,&config_f);
221 add_kline(0,"<Config>",reason,host);
222 log(DEBUG,"Read K line (badhost tag): host=%s reason=%s",host,reason);
228 void add_gline(long duration, char* source, char* reason, char* hostmask)
232 item.duration = duration;
233 strlcpy(item.hostmask,hostmask,MAXBUF);
234 strlcpy(item.reason,reason,MAXBUF);
235 strlcpy(item.source,source,MAXBUF);
237 item.set_time = time(NULL);
238 glines.push_back(item);
243 void add_qline(long duration, char* source, char* reason, char* nickname)
247 item.duration = duration;
248 strlcpy(item.nick,nickname,MAXBUF);
249 strlcpy(item.reason,reason,MAXBUF);
250 strlcpy(item.source,source,MAXBUF);
252 item.is_global = false;
253 item.set_time = time(NULL);
254 qlines.push_back(item);
259 void add_zline(long duration, char* source, char* reason, char* ipaddr)
263 item.duration = duration;
264 strlcpy(item.ipaddr,ipaddr,MAXBUF);
265 strlcpy(item.reason,reason,MAXBUF);
266 strlcpy(item.source,source,MAXBUF);
268 item.is_global = false;
269 item.set_time = time(NULL);
270 zlines.push_back(item);
275 void add_kline(long duration, char* source, char* reason, char* hostmask)
279 item.duration = duration;
280 strlcpy(item.hostmask,hostmask,MAXBUF);
281 strlcpy(item.reason,reason,MAXBUF);
282 strlcpy(item.source,source,MAXBUF);
284 item.set_time = time(NULL);
285 klines.push_back(item);
288 // deletes a g:line, returns true if the line existed and was removed
290 bool del_gline(char* hostmask)
292 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
294 if (!strcasecmp(hostmask,i->hostmask))
303 // deletes a q:line, returns true if the line existed and was removed
305 bool del_qline(char* nickname)
307 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
309 if (!strcasecmp(nickname,i->nick))
318 bool qline_make_global(char* nickname)
320 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
322 if (!strcasecmp(nickname,i->nick))
331 bool zline_make_global(char* ipaddr)
333 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
335 if (!strcasecmp(ipaddr,i->ipaddr))
344 void sync_xlines(serverrec* serv, char* tcp_host)
348 // for zlines and qlines, we should first check if theyre global...
349 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
353 snprintf(data,MAXBUF,"} %s %s %ld %ld :%s",i->ipaddr,i->source,i->set_time,i->duration,i->reason);
354 serv->SendPacket(data,tcp_host);
357 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
361 snprintf(data,MAXBUF,"{ %s %s %ld %ld :%s",i->nick,i->source,i->set_time,i->duration,i->reason);
362 serv->SendPacket(data,tcp_host);
365 // glines are always global, so no need to check
366 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
368 snprintf(data,MAXBUF,"# %s %s %ld %ld :%s",i->hostmask,i->source,i->set_time,i->duration,i->reason);
369 serv->SendPacket(data,tcp_host);
374 // deletes a z:line, returns true if the line existed and was removed
376 bool del_zline(char* ipaddr)
378 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.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(char* hostmask)
393 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
395 if (!strcasecmp(hostmask,i->hostmask))
404 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
406 char* matches_qline(const char* nick)
408 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
410 if (match(nick,i->nick))
418 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
420 char* matches_gline(const char* host)
422 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
424 if (match(host,i->hostmask))
432 void gline_set_creation_time(char* host, time_t create_time)
434 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
436 if (!strcasecmp(host,i->hostmask))
438 i->set_time = create_time;
445 void qline_set_creation_time(char* nick, time_t create_time)
447 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
449 if (!strcasecmp(nick,i->nick))
451 i->set_time = create_time;
458 void zline_set_creation_time(char* ip, time_t create_time)
460 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
462 if (!strcasecmp(ip,i->ipaddr))
464 i->set_time = create_time;
471 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
473 char* matches_zline(const char* ipaddr)
475 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
477 if (match(ipaddr,i->ipaddr))
485 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
487 char* matches_kline(const char* host)
489 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
491 if (match(host,i->hostmask))
499 // removes lines that have expired
503 bool go_again = true;
504 time_t current = time(NULL);
506 // because we mess up an iterator when we remove from the vector, we must bail from
507 // the loop early if we delete an item, therefore this outer while loop is required.
512 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
514 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
516 WriteOpers("Expiring timed K-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
523 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
525 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
527 WriteOpers("Expiring timed G-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
534 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
536 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
538 WriteOpers("Expiring timed Z-Line %s (set by %s %d seconds ago)",i->ipaddr,i->source,i->duration);
545 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
547 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
549 WriteOpers("Expiring timed Q-Line %s (set by %s %d seconds ago)",i->nick,i->source,i->duration);
558 // applies lines, removing clients and changing nicks etc as applicable
562 bool go_again = true;
566 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()))
572 for (user_hash::const_iterator u = clientlist.begin(); u != clientlist.end(); u++)
574 if (!strcasecmp(u->second->server,ServerName))
576 snprintf(host,MAXBUF,"%s@%s",u->second->ident,u->second->host);
579 char* check = matches_gline(host);
582 WriteOpers("*** User %s matches G-Line: %s",u->second->nick,check);
583 snprintf(reason,MAXBUF,"G-Lined: %s",check);
584 kill_link(u->second,reason);
591 char* check = matches_kline(host);
594 WriteOpers("*** User %s matches K-Line: %s",u->second->nick,check);
595 snprintf(reason,MAXBUF,"K-Lined: %s",check);
596 kill_link(u->second,reason);
603 char* check = matches_qline(u->second->nick);
606 snprintf(reason,MAXBUF,"Matched Q-Lined nick: %s",check);
607 WriteOpers("*** Q-Lined nickname %s from %s: %s",u->second->nick,u->second->host,check);
608 kill_link(u->second,reason);
615 char* check = matches_zline(u->second->ip);
618 snprintf(reason,MAXBUF,"Z-Lined: %s",check);
619 WriteOpers("*** User %s matches Z-Line: %s",u->second->nick,u->second->host,check);
620 kill_link(u->second,reason);
630 void stats_k(userrec* user)
632 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
634 WriteServ(user->fd,"216 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
638 void stats_g(userrec* user)
640 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
642 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
646 void stats_q(userrec* user)
648 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
650 WriteServ(user->fd,"217 %s :%s %d %d %s %s",user->nick,i->nick,i->set_time,i->duration,i->source,i->reason);
654 void stats_z(userrec* user)
656 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
658 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->ipaddr,i->set_time,i->duration,i->source,i->reason);