2 #include "inspircd_io.h"
3 #include "inspircd_util.h"
4 #include "inspircd_config.h"
9 #include <sys/utsname.h>
14 #include <ext/hash_map>
26 #include "connection.h"
39 #define nspace __gnu_cxx
48 extern vector<Module*> modules;
49 extern vector<ircd_module*> factory;
52 extern char ServerName[MAXBUF];
53 extern char Network[MAXBUF];
54 extern char ServerDesc[MAXBUF];
55 extern char AdminName[MAXBUF];
56 extern char AdminEmail[MAXBUF];
57 extern char AdminNick[MAXBUF];
58 extern char diepass[MAXBUF];
59 extern char restartpass[MAXBUF];
60 extern char motd[MAXBUF];
61 extern char rules[MAXBUF];
62 extern char list[MAXBUF];
63 extern char PrefixQuit[MAXBUF];
64 extern char DieValue[MAXBUF];
67 extern int WHOWAS_STALE;
68 extern int WHOWAS_MAX;
70 extern time_t startup_time;
71 extern int NetBufferSize;
72 extern time_t nb_start;
74 extern std::vector<int> fd_reap;
75 extern std::vector<std::string> module_names;
77 extern char bannerBuffer[MAXBUF];
78 extern int boundPortCount;
80 extern int UDPportCount;
81 extern int ports[MAXSOCKS];
82 extern int defaultRoute;
84 extern std::vector<long> auth_cookies;
85 extern std::stringstream config_f;
87 extern serverrec* me[32];
89 extern FILE *log_file;
93 template<> struct nspace::hash<in_addr>
95 size_t operator()(const struct in_addr &a) const
98 memcpy(&q,&a,sizeof(size_t));
103 template<> struct nspace::hash<string>
105 size_t operator()(const string &s) const
108 static struct hash<const char *> strhash;
120 bool operator()(const string& s1, const string& s2) const
122 char a[MAXBUF],b[MAXBUF];
123 strcpy(a,s1.c_str());
124 strcpy(b,s2.c_str());
125 return (strcasecmp(a,b) == 0);
130 struct InAddr_HashComp
133 bool operator()(const in_addr &s1, const in_addr &s2) const
138 memcpy(&q,&s1,sizeof(size_t));
139 memcpy(&p,&s2,sizeof(size_t));
147 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
148 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
149 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
150 typedef std::deque<command_t> command_table;
153 extern user_hash clientlist;
154 extern chan_hash chanlist;
155 extern user_hash whowas;
156 extern command_table cmdlist;
157 extern file_cache MOTD;
158 extern file_cache RULES;
159 extern address_cache IP;
162 std::vector<KLine> klines;
163 std::vector<GLine> glines;
164 std::vector<ZLine> zlines;
165 std::vector<QLine> qlines;
167 // Reads the default bans from the config file.
168 // only a very small number of bans are defined
169 // this way these days, such as qlines against
170 // services nicks, etc.
172 void read_xline_defaults()
179 for (int i = 0; i < ConfValueEnum("badip",&config_f); i++)
181 ConfValue("badip","ipmask",i,ipmask,&config_f);
182 ConfValue("badip","reason",i,reason,&config_f);
183 add_zline(0,"<Config>",reason,ipmask);
184 log(DEBUG,"Read Z line (badip tag): ipmask=%s reason=%s",ipmask,reason);
187 for (int i = 0; i < ConfValueEnum("badnick",&config_f); i++)
189 ConfValue("badnick","nick",i,nick,&config_f);
190 ConfValue("badnick","reason",i,reason,&config_f);
191 add_qline(0,"<Config>",reason,nick);
192 log(DEBUG,"Read Q line (badnick tag): nick=%s reason=%s",nick,reason);
195 for (int i = 0; i < ConfValueEnum("badhost",&config_f); i++)
197 ConfValue("badhost","host",i,host,&config_f);
198 ConfValue("badhost","reason",i,reason,&config_f);
199 add_kline(0,"<Config>",reason,host);
200 log(DEBUG,"Read K line (badhost tag): host=%s reason=%s",host,reason);
206 void add_gline(long duration, char* source, char* reason, char* hostmask)
210 item.duration = duration;
211 strncpy(item.hostmask,hostmask,MAXBUF);
212 strncpy(item.reason,reason,MAXBUF);
213 strncpy(item.source,source,MAXBUF);
215 item.set_time = time(NULL);
216 glines.push_back(item);
221 void add_qline(long duration, char* source, char* reason, char* nickname)
225 item.duration = duration;
226 strncpy(item.nick,nickname,MAXBUF);
227 strncpy(item.reason,reason,MAXBUF);
228 strncpy(item.source,source,MAXBUF);
230 item.is_global = false;
231 item.set_time = time(NULL);
232 qlines.push_back(item);
237 void add_zline(long duration, char* source, char* reason, char* ipaddr)
241 item.duration = duration;
242 strncpy(item.ipaddr,ipaddr,MAXBUF);
243 strncpy(item.reason,reason,MAXBUF);
244 strncpy(item.source,source,MAXBUF);
246 item.is_global = false;
247 item.set_time = time(NULL);
248 zlines.push_back(item);
253 void add_kline(long duration, char* source, char* reason, char* hostmask)
257 item.duration = duration;
258 strncpy(item.hostmask,hostmask,MAXBUF);
259 strncpy(item.reason,reason,MAXBUF);
260 strncpy(item.source,source,MAXBUF);
262 item.set_time = time(NULL);
263 klines.push_back(item);
266 // deletes a g:line, returns true if the line existed and was removed
268 bool del_gline(char* hostmask)
270 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
272 if (!strcasecmp(hostmask,i->hostmask))
281 // deletes a q:line, returns true if the line existed and was removed
283 bool del_qline(char* nickname)
285 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
287 if (!strcasecmp(nickname,i->nick))
296 bool qline_make_global(char* nickname)
298 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
300 if (!strcasecmp(nickname,i->nick))
309 bool zline_make_global(char* ipaddr)
311 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
313 if (!strcasecmp(ipaddr,i->ipaddr))
322 void sync_xlines(serverrec* serv, char* tcp_host)
326 // for zlines and qlines, we should first check if theyre global...
327 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
331 snprintf(data,MAXBUF,"} %s %s %ld %ld :%s",i->ipaddr,i->source,i->set_time,i->duration,i->reason);
332 serv->SendPacket(data,tcp_host);
335 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
339 snprintf(data,MAXBUF,"{ %s %s %ld %ld :%s",i->nick,i->source,i->set_time,i->duration,i->reason);
340 serv->SendPacket(data,tcp_host);
343 // glines are always global, so no need to check
344 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
346 snprintf(data,MAXBUF,"# %s %s %ld %ld :%s",i->hostmask,i->source,i->set_time,i->duration,i->reason);
347 serv->SendPacket(data,tcp_host);
352 // deletes a z:line, returns true if the line existed and was removed
354 bool del_zline(char* ipaddr)
356 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
358 if (!strcasecmp(ipaddr,i->ipaddr))
367 // deletes a k:line, returns true if the line existed and was removed
369 bool del_kline(char* hostmask)
371 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
373 if (!strcasecmp(hostmask,i->hostmask))
382 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
384 char* matches_qline(const char* nick)
386 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
388 if (match(nick,i->nick))
396 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
398 char* matches_gline(const char* host)
400 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
402 if (match(host,i->hostmask))
410 void gline_set_creation_time(char* host, time_t create_time)
412 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
414 if (!strcasecmp(host,i->hostmask))
416 i->set_time = create_time;
423 void qline_set_creation_time(char* nick, time_t create_time)
425 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
427 if (!strcasecmp(nick,i->nick))
429 i->set_time = create_time;
436 void zline_set_creation_time(char* ip, time_t create_time)
438 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
440 if (!strcasecmp(ip,i->ipaddr))
442 i->set_time = create_time;
449 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
451 char* matches_zline(const char* ipaddr)
453 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
455 if (match(ipaddr,i->ipaddr))
463 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
465 char* matches_kline(const char* host)
467 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
469 if (match(host,i->hostmask))
477 // removes lines that have expired
481 bool go_again = true;
482 time_t current = time(NULL);
484 // because we mess up an iterator when we remove from the vector, we must bail from
485 // the loop early if we delete an item, therefore this outer while loop is required.
490 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
492 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
494 WriteOpers("Expiring timed K-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
501 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
503 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
505 WriteOpers("Expiring timed G-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
512 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
514 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
516 WriteOpers("Expiring timed Z-Line %s (set by %s %d seconds ago)",i->ipaddr,i->source,i->duration);
523 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
525 if ((current > (i->duration + i->set_time)) && (i->duration > 0))
527 WriteOpers("Expiring timed Q-Line %s (set by %s %d seconds ago)",i->nick,i->source,i->duration);
536 // applies lines, removing clients and changing nicks etc as applicable
540 bool go_again = true;
547 for (user_hash::const_iterator u = clientlist.begin(); u != clientlist.end(); u++)
549 if (!strcasecmp(u->second->server,ServerName))
551 snprintf(host,MAXBUF,"%s@%s",u->second->ident,u->second->host);
552 char* check = matches_gline(host);
555 WriteOpers("*** User %s matches G-Line: %s",u->second->nick,check);
556 snprintf(reason,MAXBUF,"G-Lined: %s",check);
557 kill_link(u->second,reason);
564 for (user_hash::const_iterator u = clientlist.begin(); u != clientlist.end(); u++)
566 if (!strcasecmp(u->second->server,ServerName))
568 snprintf(host,MAXBUF,"%s@%s",u->second->ident,u->second->host);
569 char* check = matches_kline(host);
572 WriteOpers("*** User %s matches K-Line: %s",u->second->nick,check);
573 snprintf(reason,MAXBUF,"K-Lined: %s",check);
574 kill_link(u->second,reason);
581 for (user_hash::const_iterator u = clientlist.begin(); u != clientlist.end(); u++)
583 if (!strcasecmp(u->second->server,ServerName))
585 char* check = matches_qline(u->second->nick);
588 snprintf(reason,MAXBUF,"Matched Q-Lined nick: %s",check);
589 WriteOpers("*** Q-Lined nickname %s from %s: %s",u->second->nick,u->second->host,check);
590 WriteServ(u->second->fd,"432 %s %s :Invalid nickname: %s",u->second->nick,u->second->nick,check);
591 kill_link(u->second,reason);
598 for (user_hash::const_iterator u = clientlist.begin(); u != clientlist.end(); u++)
600 if (!strcasecmp(u->second->server,ServerName))
602 char* check = matches_zline(u->second->ip);
605 snprintf(reason,MAXBUF,"Z-Lined: %s",check);
606 WriteOpers("*** User %s matches Z-Line: %s",u->second->nick,u->second->host,check);
607 kill_link(u->second,reason);
617 void stats_k(userrec* user)
619 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
621 WriteServ(user->fd,"216 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
625 void stats_g(userrec* user)
627 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
629 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
633 void stats_q(userrec* user)
635 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
637 WriteServ(user->fd,"217 %s :%s %d %d %s %s",user->nick,i->nick,i->set_time,i->duration,i->source,i->reason);
641 void stats_z(userrec* user)
643 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
645 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->ipaddr,i->set_time,i->duration,i->source,i->reason);