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 vector<Module*> modules;
65 extern 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;
108 template<> struct nspace::hash<in_addr>
110 size_t operator()(const struct in_addr &a) const
113 memcpy(&q,&a,sizeof(size_t));
118 template<> struct nspace::hash<string>
120 size_t operator()(const string &s) const
123 static struct hash<const char *> strhash;
135 bool operator()(const string& s1, const string& s2) const
137 char a[MAXBUF],b[MAXBUF];
138 strcpy(a,s1.c_str());
139 strcpy(b,s2.c_str());
140 return (strcasecmp(a,b) == 0);
145 struct InAddr_HashComp
148 bool operator()(const in_addr &s1, const in_addr &s2) const
153 memcpy(&q,&s1,sizeof(size_t));
154 memcpy(&p,&s2,sizeof(size_t));
162 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
163 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
164 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
165 typedef std::deque<command_t> command_table;
168 extern user_hash clientlist;
169 extern chan_hash chanlist;
170 extern user_hash whowas;
171 extern command_table cmdlist;
172 extern file_cache MOTD;
173 extern file_cache RULES;
174 extern address_cache IP;
177 std::vector<KLine> klines;
178 std::vector<GLine> glines;
179 std::vector<ZLine> zlines;
180 std::vector<QLine> qlines;
182 // Reads the default bans from the config file.
183 // only a very small number of bans are defined
184 // this way these days, such as qlines against
185 // services nicks, etc.
187 void read_xline_defaults()
194 for (int i = 0; i < ConfValueEnum("badip",&config_f); i++)
196 ConfValue("badip","ipmask",i,ipmask,&config_f);
197 ConfValue("badip","reason",i,reason,&config_f);
198 add_zline(0,"<Config>",reason,ipmask);
199 log(DEBUG,"Read Z line (badip tag): ipmask=%s reason=%s",ipmask,reason);
202 for (int i = 0; i < ConfValueEnum("badnick",&config_f); i++)
204 ConfValue("badnick","nick",i,nick,&config_f);
205 ConfValue("badnick","reason",i,reason,&config_f);
206 add_qline(0,"<Config>",reason,nick);
207 log(DEBUG,"Read Q line (badnick tag): nick=%s reason=%s",nick,reason);
210 for (int i = 0; i < ConfValueEnum("badhost",&config_f); i++)
212 ConfValue("badhost","host",i,host,&config_f);
213 ConfValue("badhost","reason",i,reason,&config_f);
214 add_kline(0,"<Config>",reason,host);
215 log(DEBUG,"Read K line (badhost tag): host=%s reason=%s",host,reason);
221 void add_gline(long duration, char* source, char* reason, char* hostmask)
225 item.duration = duration;
226 strncpy(item.hostmask,hostmask,MAXBUF);
227 strncpy(item.reason,reason,MAXBUF);
228 strncpy(item.source,source,MAXBUF);
230 item.set_time = time(NULL);
231 glines.push_back(item);
236 void add_qline(long duration, char* source, char* reason, char* nickname)
240 item.duration = duration;
241 strncpy(item.nick,nickname,MAXBUF);
242 strncpy(item.reason,reason,MAXBUF);
243 strncpy(item.source,source,MAXBUF);
245 item.is_global = false;
246 item.set_time = time(NULL);
247 qlines.push_back(item);
252 void add_zline(long duration, char* source, char* reason, char* ipaddr)
256 item.duration = duration;
257 strncpy(item.ipaddr,ipaddr,MAXBUF);
258 strncpy(item.reason,reason,MAXBUF);
259 strncpy(item.source,source,MAXBUF);
261 item.is_global = false;
262 item.set_time = time(NULL);
263 zlines.push_back(item);
268 void add_kline(long duration, char* source, char* reason, char* hostmask)
272 item.duration = duration;
273 strncpy(item.hostmask,hostmask,MAXBUF);
274 strncpy(item.reason,reason,MAXBUF);
275 strncpy(item.source,source,MAXBUF);
277 item.set_time = time(NULL);
278 klines.push_back(item);
281 // deletes a g:line, returns true if the line existed and was removed
283 bool del_gline(char* hostmask)
285 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
287 if (!strcasecmp(hostmask,i->hostmask))
296 // deletes a q:line, returns true if the line existed and was removed
298 bool del_qline(char* nickname)
300 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
302 if (!strcasecmp(nickname,i->nick))
311 bool qline_make_global(char* nickname)
313 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
315 if (!strcasecmp(nickname,i->nick))
324 bool zline_make_global(char* ipaddr)
326 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
328 if (!strcasecmp(ipaddr,i->ipaddr))
337 void sync_xlines(serverrec* serv, char* tcp_host)
341 // for zlines and qlines, we should first check if theyre global...
342 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
346 snprintf(data,MAXBUF,"} %s %s %ld %ld :%s",i->ipaddr,i->source,i->set_time,i->duration,i->reason);
347 serv->SendPacket(data,tcp_host);
350 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
354 snprintf(data,MAXBUF,"{ %s %s %ld %ld :%s",i->nick,i->source,i->set_time,i->duration,i->reason);
355 serv->SendPacket(data,tcp_host);
358 // glines are always global, so no need to check
359 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
361 snprintf(data,MAXBUF,"# %s %s %ld %ld :%s",i->hostmask,i->source,i->set_time,i->duration,i->reason);
362 serv->SendPacket(data,tcp_host);
367 // deletes a z:line, returns true if the line existed and was removed
369 bool del_zline(char* ipaddr)
371 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
373 if (!strcasecmp(ipaddr,i->ipaddr))
382 // deletes a k:line, returns true if the line existed and was removed
384 bool del_kline(char* hostmask)
386 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
388 if (!strcasecmp(hostmask,i->hostmask))
397 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
399 char* matches_qline(const char* nick)
401 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
403 if (match(nick,i->nick))
411 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
413 char* matches_gline(const char* host)
415 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
417 if (match(host,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)
468 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
470 if (match(ipaddr,i->ipaddr))
478 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
480 char* matches_kline(const char* host)
482 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
484 if (match(host,i->hostmask))
492 // removes lines that have expired
496 bool go_again = true;
497 time_t current = time(NULL);
499 // because we mess up an iterator when we remove from the vector, we must bail from
500 // the loop early if we delete an item, therefore this outer while loop is required.
505 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
507 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
509 WriteOpers("Expiring timed K-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
516 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
518 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
520 WriteOpers("Expiring timed G-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
527 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
529 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
531 WriteOpers("Expiring timed Z-Line %s (set by %s %d seconds ago)",i->ipaddr,i->source,i->duration);
538 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
540 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
542 WriteOpers("Expiring timed Q-Line %s (set by %s %d seconds ago)",i->nick,i->source,i->duration);
551 // applies lines, removing clients and changing nicks etc as applicable
555 bool go_again = true;
559 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()))
565 for (user_hash::const_iterator u = clientlist.begin(); u != clientlist.end(); u++)
567 if (!strcasecmp(u->second->server,ServerName))
569 snprintf(host,MAXBUF,"%s@%s",u->second->ident,u->second->host);
572 char* check = matches_gline(host);
575 WriteOpers("*** User %s matches G-Line: %s",u->second->nick,check);
576 snprintf(reason,MAXBUF,"G-Lined: %s",check);
577 kill_link(u->second,reason);
584 char* check = matches_kline(host);
587 WriteOpers("*** User %s matches K-Line: %s",u->second->nick,check);
588 snprintf(reason,MAXBUF,"K-Lined: %s",check);
589 kill_link(u->second,reason);
596 char* check = matches_qline(u->second->nick);
599 snprintf(reason,MAXBUF,"Matched Q-Lined nick: %s",check);
600 WriteOpers("*** Q-Lined nickname %s from %s: %s",u->second->nick,u->second->host,check);
601 kill_link(u->second,reason);
608 char* check = matches_zline(u->second->ip);
611 snprintf(reason,MAXBUF,"Z-Lined: %s",check);
612 WriteOpers("*** User %s matches Z-Line: %s",u->second->nick,u->second->host,check);
613 kill_link(u->second,reason);
623 void stats_k(userrec* user)
625 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
627 WriteServ(user->fd,"216 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
631 void stats_g(userrec* user)
633 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
635 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
639 void stats_q(userrec* user)
641 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
643 WriteServ(user->fd,"217 %s :%s %d %d %s %s",user->nick,i->nick,i->set_time,i->duration,i->source,i->reason);
647 void stats_z(userrec* user)
649 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
651 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->ipaddr,i->set_time,i->duration,i->source,i->reason);