]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/xline.cpp
8f69455df96615861f10e86da61f0be2bb25c1e2
[user/henk/code/inspircd.git] / src / xline.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include "inspircd.h"
18 #include "inspircd_io.h"
19 #include "inspircd_util.h"
20 #include "inspircd_config.h"
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <sys/errno.h>
24 #include <sys/ioctl.h>
25 #include <sys/utsname.h>
26 #include <cstdio>
27 #include <time.h>
28 #include <string>
29 #ifdef GCC3
30 #include <ext/hash_map>
31 #else
32 #include <hash_map>
33 #endif
34 #include <map>
35 #include <sstream>
36 #include <vector>
37 #include <errno.h>
38 #include <deque>
39 #include <errno.h>
40 #include <unistd.h>
41 #include <sched.h>
42 #include "connection.h"
43 #include "users.h"
44 #include "servers.h"
45 #include "ctables.h"
46 #include "globals.h"
47 #include "modules.h"
48 #include "dynamic.h"
49 #include "wildcard.h"
50 #include "message.h"
51 #include "commands.h"
52 #include "xline.h"
53 #include "inspstring.h"
54
55 #ifdef GCC3
56 #define nspace __gnu_cxx
57 #else
58 #define nspace std
59 #endif
60
61
62 using namespace std;
63
64 extern int MODCOUNT;
65 extern std::vector<Module*> modules;
66 extern std::vector<ircd_module*> factory;
67
68 extern int LogLevel;
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];
82
83 extern int debugging;
84 extern int WHOWAS_STALE;
85 extern int WHOWAS_MAX;
86 extern int DieDelay;
87 extern time_t startup_time;
88 extern int NetBufferSize;
89 extern time_t nb_start;
90
91 extern std::vector<int> fd_reap;
92 extern std::vector<std::string> module_names;
93
94 extern int boundPortCount;
95 extern int portCount;
96 extern int SERVERportCount;
97 extern int ports[MAXSOCKS];
98 extern int defaultRoute;
99
100 extern std::vector<long> auth_cookies;
101 extern std::stringstream config_f;
102
103 extern serverrec* me[32];
104
105 extern FILE *log_file;
106
107 namespace nspace
108 {
109 #ifdef GCC34
110         template<> struct hash<in_addr>
111 #else
112         template<> struct nspace::hash<in_addr>
113 #endif
114         {
115                 size_t operator()(const struct in_addr &a) const
116                 {
117                         size_t q;
118                         memcpy(&q,&a,sizeof(size_t));
119                         return q;
120                 }
121         };
122 #ifdef GCC34
123         template<> struct hash<string>
124 #else
125         template<> struct nspace::hash<string>
126 #endif
127         {
128                 size_t operator()(const string &s) const
129                 {
130                         char a[MAXBUF];
131                         static struct hash<const char *> strhash;
132                         strlcpy(a,s.c_str(),MAXBUF);
133                         strlower(a);
134                         return strhash(a);
135                 }
136         };
137 }       
138
139
140 struct StrHashComp
141 {
142
143         bool operator()(const string& s1, const string& s2) const
144         {
145                 char a[MAXBUF],b[MAXBUF];
146                 strlcpy(a,s1.c_str(),MAXBUF);
147                 strlcpy(b,s2.c_str(),MAXBUF);
148                 strlower(a);
149                 strlower(b);
150                 return (strcasecmp(a,b) == 0);
151         }
152
153 };
154
155 struct InAddr_HashComp
156 {
157
158         bool operator()(const in_addr &s1, const in_addr &s2) const
159         {
160                 size_t q;
161                 size_t p;
162                 
163                 memcpy(&q,&s1,sizeof(size_t));
164                 memcpy(&p,&s2,sizeof(size_t));
165                 
166                 return (q == p);
167         }
168
169 };
170
171
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 nspace::hash_map<std::string, WhoWasUser*, nspace::hash<string>, StrHashComp> whowas_hash;
176 typedef std::deque<command_t> command_table;
177
178
179 extern user_hash clientlist;
180 extern chan_hash chanlist;
181 extern whowas_hash whowas;
182 extern command_table cmdlist;
183 extern file_cache MOTD;
184 extern file_cache RULES;
185 extern address_cache IP;
186
187 extern time_t TIME;
188
189 std::vector<KLine> klines;
190 std::vector<GLine> glines;
191 std::vector<ZLine> zlines;
192 std::vector<QLine> qlines;
193 std::vector<ELine> elines;
194
195 // Reads the default bans from the config file.
196 // only a very small number of bans are defined
197 // this way these days, such as qlines against 
198 // services nicks, etc.
199
200 void read_xline_defaults()
201 {
202         char ipmask[MAXBUF];
203         char nick[MAXBUF];
204         char host[MAXBUF];
205         char reason[MAXBUF];
206
207         for (int i = 0; i < ConfValueEnum("badip",&config_f); i++)
208         {
209                 ConfValue("badip","ipmask",i,ipmask,&config_f);
210                 ConfValue("badip","reason",i,reason,&config_f);
211                 add_zline(0,"<Config>",reason,ipmask);
212                 log(DEBUG,"Read Z line (badip tag): ipmask=%s reason=%s",ipmask,reason);
213         }
214         
215         for (int i = 0; i < ConfValueEnum("badnick",&config_f); i++)
216         {
217                 ConfValue("badnick","nick",i,nick,&config_f);
218                 ConfValue("badnick","reason",i,reason,&config_f);
219                 add_qline(0,"<Config>",reason,nick);
220                 log(DEBUG,"Read Q line (badnick tag): nick=%s reason=%s",nick,reason);
221         }
222         
223         for (int i = 0; i < ConfValueEnum("badhost",&config_f); i++)
224         {
225                 ConfValue("badhost","host",i,host,&config_f);
226                 ConfValue("badhost","reason",i,reason,&config_f);
227                 add_kline(0,"<Config>",reason,host);
228                 log(DEBUG,"Read K line (badhost tag): host=%s reason=%s",host,reason);
229         }
230         for (int i = 0; i < ConfValueEnum("exception",&config_f); i++)
231         {
232                 ConfValue("exception","host",i,host,&config_f);
233                 ConfValue("exception","reason",i,reason,&config_f);
234                 add_eline(0,"<Config>",reason,host);
235                 log(DEBUG,"Read E line (exception tag): host=%s reason=%s",host,reason);
236         }
237 }
238
239 // adds a g:line
240
241 void add_gline(long duration, const char* source,const char* reason,const char* hostmask)
242 {
243         del_gline(hostmask);
244         GLine item;
245         item.duration = duration;
246         strlcpy(item.hostmask,hostmask,199);
247         strlcpy(item.reason,reason,MAXBUF);
248         strlcpy(item.source,source,255);
249         item.n_matches = 0;
250         item.set_time = TIME;
251         glines.push_back(item);
252 }
253
254 // adds an e:line (exception to bans)
255
256 void add_eline(long duration, const char* source, const char* reason, const char* hostmask)
257 {
258         del_eline(hostmask);
259         ELine item;
260         item.duration = duration;
261         strlcpy(item.hostmask,hostmask,199);
262         strlcpy(item.reason,reason,MAXBUF);
263         strlcpy(item.source,source,255);
264         item.n_matches = 0;
265         item.set_time = TIME;
266         elines.push_back(item);
267 }
268
269 // adds a q:line
270
271 void add_qline(long duration, const char* source, const char* reason, const char* nickname)
272 {
273         del_qline(nickname);
274         QLine item;
275         item.duration = duration;
276         strlcpy(item.nick,nickname,63);
277         strlcpy(item.reason,reason,MAXBUF);
278         strlcpy(item.source,source,255);
279         item.n_matches = 0;
280         item.is_global = false;
281         item.set_time = TIME;
282         qlines.push_back(item);
283 }
284
285 // adds a z:line
286
287 void add_zline(long duration, const char* source, const char* reason, const char* ipaddr)
288 {
289         del_zline(ipaddr);
290         ZLine item;
291         item.duration = duration;
292         if (strchr(ipaddr,'@'))
293         {
294                 while (*ipaddr != '@')
295                         ipaddr++;
296                 ipaddr++;
297         }
298         strlcpy(item.ipaddr,ipaddr,39);
299         strlcpy(item.reason,reason,MAXBUF);
300         strlcpy(item.source,source,255);
301         item.n_matches = 0;
302         item.is_global = false;
303         item.set_time = TIME;
304         zlines.push_back(item);
305 }
306
307 // adds a k:line
308
309 void add_kline(long duration, const char* source, const char* reason, const char* hostmask)
310 {
311         del_kline(hostmask);
312         KLine item;
313         item.duration = duration;
314         strlcpy(item.hostmask,hostmask,200);
315         strlcpy(item.reason,reason,MAXBUF);
316         strlcpy(item.source,source,255);
317         item.n_matches = 0;
318         item.set_time = TIME;
319         klines.push_back(item);
320 }
321
322 // deletes a g:line, returns true if the line existed and was removed
323
324 bool del_gline(const char* hostmask)
325 {
326         for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
327         {
328                 if (!strcasecmp(hostmask,i->hostmask))
329                 {
330                         glines.erase(i);
331                         return true;
332                 }
333         }
334         return false;
335 }
336
337 // deletes a e:line, returns true if the line existed and was removed
338
339 bool del_eline(const char* hostmask)
340 {
341         for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
342         {
343                 if (!strcasecmp(hostmask,i->hostmask))
344                 {
345                         elines.erase(i);
346                         return true;
347                 }
348         }
349         return false;
350 }
351
352 // deletes a q:line, returns true if the line existed and was removed
353
354 bool del_qline(const char* nickname)
355 {
356         for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
357         {
358                 if (!strcasecmp(nickname,i->nick))
359                 {
360                         qlines.erase(i);
361                         return true;
362                 }
363         }
364         return false;
365 }
366
367 bool qline_make_global(const char* nickname)
368 {
369         for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
370         {
371                 if (!strcasecmp(nickname,i->nick))
372                 {
373                         i->is_global = true;
374                         return true;
375                 }
376         }
377         return false;
378 }
379
380 bool zline_make_global(const char* ipaddr)
381 {
382         for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
383         {
384                 if (!strcasecmp(ipaddr,i->ipaddr))
385                 {
386                         i->is_global = true;
387                         return true;
388                 }
389         }
390         return false;
391 }
392
393 void sync_xlines(serverrec* serv, char* tcp_host)
394 {
395         char data[MAXBUF];
396         
397         // for zlines and qlines, we should first check if theyre global...
398         for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
399         {
400                 if (i->is_global)
401                 {
402                         snprintf(data,MAXBUF,"} %s %s %lu %lu :%s",i->ipaddr,i->source,(unsigned long)i->set_time,(unsigned long)i->duration,i->reason);
403                         serv->SendPacket(data,tcp_host);
404                 }
405         }
406         for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
407         {
408                 if (i->is_global)
409                 {
410                         snprintf(data,MAXBUF,"{ %s %s %lu %lu :%s",i->nick,i->source,(unsigned long)i->set_time,(unsigned long)i->duration,i->reason);
411                         serv->SendPacket(data,tcp_host);
412                 }
413         }
414         // glines are always global, so no need to check
415         for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
416         {
417                 snprintf(data,MAXBUF,"# %s %s %lu %lu :%s",i->hostmask,i->source,(unsigned long)i->set_time,(unsigned long)i->duration,i->reason);
418                 serv->SendPacket(data,tcp_host);
419         }
420 }
421
422
423 // deletes a z:line, returns true if the line existed and was removed
424
425 bool del_zline(const char* ipaddr)
426 {
427         for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
428         {
429                 if (!strcasecmp(ipaddr,i->ipaddr))
430                 {
431                         zlines.erase(i);
432                         return true;
433                 }
434         }
435         return false;
436 }
437
438 // deletes a k:line, returns true if the line existed and was removed
439
440 bool del_kline(const char* hostmask)
441 {
442         for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
443         {
444                 if (!strcasecmp(hostmask,i->hostmask))
445                 {
446                         klines.erase(i);
447                         return true;
448                 }
449         }
450         return false;
451 }
452
453 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
454
455 char* matches_qline(const char* nick)
456 {
457         if (qlines.empty())
458                 return NULL;
459         for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
460         {
461                 if (match(nick,i->nick))
462                 {
463                         return i->reason;
464                 }
465         }
466         return NULL;
467 }
468
469 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
470
471 char* matches_gline(const char* host)
472 {
473         if (glines.empty())
474                 return NULL;
475         for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
476         {
477                 if (match(host,i->hostmask))
478                 {
479                         return i->reason;
480                 }
481         }
482         return NULL;
483 }
484
485 char* matches_exception(const char* host)
486 {
487         if (elines.empty())
488                 return NULL;
489         char host2[MAXBUF];
490         snprintf(host2,MAXBUF,"*@%s",host);
491         for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
492         {
493                 if ((match(host,i->hostmask)) || (match(host2,i->hostmask)))
494                 {
495                         return i->reason;
496                 }
497         }
498         return NULL;
499 }
500
501
502 void gline_set_creation_time(char* host, time_t create_time)
503 {
504         for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
505         {
506                 if (!strcasecmp(host,i->hostmask))
507                 {
508                         i->set_time = create_time;
509                         return;
510                 }
511         }
512         return ;        
513 }
514
515 void qline_set_creation_time(char* nick, time_t create_time)
516 {
517         for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
518         {
519                 if (!strcasecmp(nick,i->nick))
520                 {
521                         i->set_time = create_time;
522                         return;
523                 }
524         }
525         return ;        
526 }
527
528 void zline_set_creation_time(char* ip, time_t create_time)
529 {
530         for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
531         {
532                 if (!strcasecmp(ip,i->ipaddr))
533                 {
534                         i->set_time = create_time;
535                         return;
536                 }
537         }
538         return ;        
539 }
540
541 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
542
543 char* matches_zline(const char* ipaddr)
544 {
545         if (zlines.empty())
546                 return NULL;
547         for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
548         {
549                 if (match(ipaddr,i->ipaddr))
550                 {
551                         return i->reason;
552                 }
553         }
554         return NULL;
555 }
556
557 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
558
559 char* matches_kline(const char* host)
560 {
561         if (klines.empty())
562                 return NULL;
563         for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
564         {
565                 if (match(host,i->hostmask))
566                 {
567                         return i->reason;
568                 }
569         }
570         return NULL;
571 }
572
573 // removes lines that have expired
574
575 void expire_lines()
576 {
577         bool go_again = true;
578         time_t current = TIME;
579         
580         // because we mess up an iterator when we remove from the vector, we must bail from
581         // the loop early if we delete an item, therefore this outer while loop is required.
582         while (go_again)
583         {
584                 go_again = false;
585
586                 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
587                 {
588                         if ((current > (i->duration + i->set_time)) && (i->duration > 0))
589                         {
590                                 WriteOpers("Expiring timed K-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
591                                 klines.erase(i);
592                                 go_again = true;
593                                 break;
594                         }
595                 }
596
597                 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
598                 {
599                         if ((current > (i->duration + i->set_time)) && (i->duration > 0))
600                         {
601                                 WriteOpers("Expiring timed E-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
602                                 elines.erase(i);
603                                 go_again = true;
604                                 break;
605                         }
606                 }
607
608                 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
609                 {
610                         if ((current > (i->duration + i->set_time)) && (i->duration > 0))
611                         {
612                                 WriteOpers("Expiring timed G-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
613                                 glines.erase(i);
614                                 go_again = true;
615                                 break;
616                         }
617                 }
618
619                 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
620                 {
621                         if ((current > (i->duration + i->set_time)) && (i->duration > 0))
622                         {
623                                 WriteOpers("Expiring timed Z-Line %s (set by %s %d seconds ago)",i->ipaddr,i->source,i->duration);
624                                 zlines.erase(i);
625                                 go_again = true;
626                                 break;
627                         }
628                 }
629
630                 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
631                 {
632                         if ((current > (i->duration + i->set_time)) && (i->duration > 0))
633                         {
634                                 WriteOpers("Expiring timed Q-Line %s (set by %s %d seconds ago)",i->nick,i->source,i->duration);
635                                 qlines.erase(i);
636                                 go_again = true;
637                                 break;
638                         }
639                 }
640         }
641 }
642
643 // applies lines, removing clients and changing nicks etc as applicable
644
645 void apply_lines()
646 {
647         bool go_again = true;
648         char reason[MAXBUF];
649         char host[MAXBUF];
650         
651         if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()))
652                 return;
653         
654         while (go_again)
655         {
656                 go_again = false;
657                 for (user_hash::const_iterator u = clientlist.begin(); u != clientlist.end(); u++)
658                 {
659                         if (!strcasecmp(u->second->server,ServerName))
660                         {
661                                 snprintf(host,MAXBUF,"%s@%s",u->second->ident,u->second->host);
662                                 if (elines.size())
663                                 {
664                                         // ignore people matching exempts
665                                         if (matches_exception(host))
666                                                 continue;
667                                 }
668                                 if (glines.size())
669                                 {
670                                         char* check = matches_gline(host);
671                                         if (check)
672                                         {
673                                                 WriteOpers("*** User %s matches G-Line: %s",u->second->registered == 7 ? u->second->nick:"<unknown>",check);
674                                                 snprintf(reason,MAXBUF,"G-Lined: %s",check);
675                                                 kill_link(u->second,reason);
676                                                 go_again = true;
677                                                 break;
678                                         }
679                                 }
680                                 if (klines.size())
681                                 {
682                                         char* check = matches_kline(host);
683                                         if (check)
684                                         {
685                                                 WriteOpers("*** User %s matches K-Line: %s",u->second->registered == 7 ? u->second->nick:"<unknown>",check);
686                                                 snprintf(reason,MAXBUF,"K-Lined: %s",check);
687                                                 kill_link(u->second,reason);
688                                                 go_again = true;
689                                                 break;
690                                         }
691                                 }
692                                 if (qlines.size())
693                                 {
694                                         char* check = matches_qline(u->second->nick);
695                                         if (check)
696                                         {
697                                                 snprintf(reason,MAXBUF,"Matched Q-Lined nick: %s",check);
698                                                 WriteOpers("*** Q-Lined nickname %s from %s: %s",u->second->registered == 7 ? u->second->nick:"<unknown>",u->second->host,check);
699                                                 kill_link(u->second,reason);
700                                                 go_again = true;
701                                                 break;
702                                         }
703                                 }
704                                 if (zlines.size())
705                                 {
706                                         char* check = matches_zline(u->second->ip);
707                                         if (check)
708                                         {
709                                                 snprintf(reason,MAXBUF,"Z-Lined: %s",check);
710                                                 WriteOpers("*** User %s matches Z-Line: %s",u->second->registered == 7 ? u->second->nick:"<unknown>",u->second->host,check);
711                                                 kill_link(u->second,reason);
712                                                 go_again = true;
713                                                 break;
714                                         }
715                                 }
716                         }
717                 }
718         }
719 }
720
721 void stats_k(userrec* user)
722 {
723         for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
724         {
725                 WriteServ(user->fd,"216 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
726         }
727 }
728
729 void stats_g(userrec* user)
730 {
731         for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
732         {
733                 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
734         }
735 }
736
737 void stats_q(userrec* user)
738 {
739         for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
740         {
741                 WriteServ(user->fd,"217 %s :%s %d %d %s %s",user->nick,i->nick,i->set_time,i->duration,i->source,i->reason);
742         }
743 }
744
745 void stats_z(userrec* user)
746 {
747         for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
748         {
749                 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->ipaddr,i->set_time,i->duration,i->source,i->reason);
750         }
751 }
752
753 void stats_e(userrec* user)
754 {
755         for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
756         {
757                 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
758         }
759 }