]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/xline.cpp
ce6257a663decff4bd14c9409871747d53cc751f
[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 UDPportCount;
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                 return (strcasecmp(a,b) == 0);
149         }
150
151 };
152
153 struct InAddr_HashComp
154 {
155
156         bool operator()(const in_addr &s1, const in_addr &s2) const
157         {
158                 size_t q;
159                 size_t p;
160                 
161                 memcpy(&q,&s1,sizeof(size_t));
162                 memcpy(&p,&s2,sizeof(size_t));
163                 
164                 return (q == p);
165         }
166
167 };
168
169
170 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
171 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
172 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
173 typedef std::deque<command_t> command_table;
174
175
176 extern user_hash clientlist;
177 extern chan_hash chanlist;
178 extern user_hash whowas;
179 extern command_table cmdlist;
180 extern file_cache MOTD;
181 extern file_cache RULES;
182 extern address_cache IP;
183
184
185 std::vector<KLine> klines;
186 std::vector<GLine> glines;
187 std::vector<ZLine> zlines;
188 std::vector<QLine> qlines;
189 std::vector<ELine> elines;
190
191 // Reads the default bans from the config file.
192 // only a very small number of bans are defined
193 // this way these days, such as qlines against 
194 // services nicks, etc.
195
196 void read_xline_defaults()
197 {
198         char ipmask[MAXBUF];
199         char nick[MAXBUF];
200         char host[MAXBUF];
201         char reason[MAXBUF];
202
203         for (int i = 0; i < ConfValueEnum("badip",&config_f); i++)
204         {
205                 ConfValue("badip","ipmask",i,ipmask,&config_f);
206                 ConfValue("badip","reason",i,reason,&config_f);
207                 add_zline(0,"<Config>",reason,ipmask);
208                 log(DEBUG,"Read Z line (badip tag): ipmask=%s reason=%s",ipmask,reason);
209         }
210         
211         for (int i = 0; i < ConfValueEnum("badnick",&config_f); i++)
212         {
213                 ConfValue("badnick","nick",i,nick,&config_f);
214                 ConfValue("badnick","reason",i,reason,&config_f);
215                 add_qline(0,"<Config>",reason,nick);
216                 log(DEBUG,"Read Q line (badnick tag): nick=%s reason=%s",nick,reason);
217         }
218         
219         for (int i = 0; i < ConfValueEnum("badhost",&config_f); i++)
220         {
221                 ConfValue("badhost","host",i,host,&config_f);
222                 ConfValue("badhost","reason",i,reason,&config_f);
223                 add_kline(0,"<Config>",reason,host);
224                 log(DEBUG,"Read K line (badhost tag): host=%s reason=%s",host,reason);
225         }
226         for (int i = 0; i < ConfValueEnum("exception",&config_f); i++)
227         {
228                 ConfValue("exception","host",i,host,&config_f);
229                 ConfValue("exception","reason",i,reason,&config_f);
230                 add_eline(0,"<Config>",reason,host);
231                 log(DEBUG,"Read E line (exception tag): host=%s reason=%s",host,reason);
232         }
233 }
234
235 // adds a g:line
236
237 void add_gline(long duration, char* source, char* reason, char* hostmask)
238 {
239         del_gline(hostmask);
240         GLine item;
241         item.duration = duration;
242         strlcpy(item.hostmask,hostmask,MAXBUF);
243         strlcpy(item.reason,reason,MAXBUF);
244         strlcpy(item.source,source,MAXBUF);
245         item.n_matches = 0;
246         item.set_time = time(NULL);
247         glines.push_back(item);
248 }
249
250 // adds an e:line (exception to bans)
251
252 void add_eline(long duration, char* source, char* reason, char* hostmask)
253 {
254         del_eline(hostmask);
255         ELine item;
256         item.duration = duration;
257         strlcpy(item.hostmask,hostmask,MAXBUF);
258         strlcpy(item.reason,reason,MAXBUF);
259         strlcpy(item.source,source,MAXBUF);
260         item.n_matches = 0;
261         item.set_time = time(NULL);
262         elines.push_back(item);
263 }
264
265 // adds a q:line
266
267 void add_qline(long duration, char* source, char* reason, char* nickname)
268 {
269         del_qline(nickname);
270         QLine item;
271         item.duration = duration;
272         strlcpy(item.nick,nickname,MAXBUF);
273         strlcpy(item.reason,reason,MAXBUF);
274         strlcpy(item.source,source,MAXBUF);
275         item.n_matches = 0;
276         item.is_global = false;
277         item.set_time = time(NULL);
278         qlines.push_back(item);
279 }
280
281 // adds a z:line
282
283 void add_zline(long duration, char* source, char* reason, char* ipaddr)
284 {
285         del_zline(ipaddr);
286         ZLine item;
287         item.duration = duration;
288         strlcpy(item.ipaddr,ipaddr,MAXBUF);
289         strlcpy(item.reason,reason,MAXBUF);
290         strlcpy(item.source,source,MAXBUF);
291         item.n_matches = 0;
292         item.is_global = false;
293         item.set_time = time(NULL);
294         zlines.push_back(item);
295 }
296
297 // adds a k:line
298
299 void add_kline(long duration, char* source, char* reason, char* hostmask)
300 {
301         del_kline(hostmask);
302         KLine item;
303         item.duration = duration;
304         strlcpy(item.hostmask,hostmask,MAXBUF);
305         strlcpy(item.reason,reason,MAXBUF);
306         strlcpy(item.source,source,MAXBUF);
307         item.n_matches = 0;
308         item.set_time = time(NULL);
309         klines.push_back(item);
310 }
311
312 // deletes a g:line, returns true if the line existed and was removed
313
314 bool del_gline(char* hostmask)
315 {
316         for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
317         {
318                 if (!strcasecmp(hostmask,i->hostmask))
319                 {
320                         glines.erase(i);
321                         return true;
322                 }
323         }
324         return false;
325 }
326
327 // deletes a e:line, returns true if the line existed and was removed
328
329 bool del_eline(char* hostmask)
330 {
331         for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
332         {
333                 if (!strcasecmp(hostmask,i->hostmask))
334                 {
335                         elines.erase(i);
336                         return true;
337                 }
338         }
339         return false;
340 }
341
342 // deletes a q:line, returns true if the line existed and was removed
343
344 bool del_qline(char* nickname)
345 {
346         for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
347         {
348                 if (!strcasecmp(nickname,i->nick))
349                 {
350                         qlines.erase(i);
351                         return true;
352                 }
353         }
354         return false;
355 }
356
357 bool qline_make_global(char* nickname)
358 {
359         for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
360         {
361                 if (!strcasecmp(nickname,i->nick))
362                 {
363                         i->is_global = true;
364                         return true;
365                 }
366         }
367         return false;
368 }
369
370 bool zline_make_global(char* ipaddr)
371 {
372         for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
373         {
374                 if (!strcasecmp(ipaddr,i->ipaddr))
375                 {
376                         i->is_global = true;
377                         return true;
378                 }
379         }
380         return false;
381 }
382
383 void sync_xlines(serverrec* serv, char* tcp_host)
384 {
385         char data[MAXBUF];
386         
387         // for zlines and qlines, we should first check if theyre global...
388         for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
389         {
390                 if (i->is_global)
391                 {
392                         snprintf(data,MAXBUF,"} %s %s %ld %ld :%s",i->ipaddr,i->source,i->set_time,i->duration,i->reason);
393                         serv->SendPacket(data,tcp_host);
394                 }
395         }
396         for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
397         {
398                 if (i->is_global)
399                 {
400                         snprintf(data,MAXBUF,"{ %s %s %ld %ld :%s",i->nick,i->source,i->set_time,i->duration,i->reason);
401                         serv->SendPacket(data,tcp_host);
402                 }
403         }
404         // glines are always global, so no need to check
405         for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
406         {
407                 snprintf(data,MAXBUF,"# %s %s %ld %ld :%s",i->hostmask,i->source,i->set_time,i->duration,i->reason);
408                 serv->SendPacket(data,tcp_host);
409         }
410 }
411
412
413 // deletes a z:line, returns true if the line existed and was removed
414
415 bool del_zline(char* ipaddr)
416 {
417         for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
418         {
419                 if (!strcasecmp(ipaddr,i->ipaddr))
420                 {
421                         zlines.erase(i);
422                         return true;
423                 }
424         }
425         return false;
426 }
427
428 // deletes a k:line, returns true if the line existed and was removed
429
430 bool del_kline(char* hostmask)
431 {
432         for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
433         {
434                 if (!strcasecmp(hostmask,i->hostmask))
435                 {
436                         klines.erase(i);
437                         return true;
438                 }
439         }
440         return false;
441 }
442
443 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
444
445 char* matches_qline(const char* nick)
446 {
447         for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
448         {
449                 if (match(nick,i->nick))
450                 {
451                         return i->reason;
452                 }
453         }
454         return NULL;
455 }
456
457 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
458
459 char* matches_gline(const char* host)
460 {
461         for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
462         {
463                 if (match(host,i->hostmask))
464                 {
465                         return i->reason;
466                 }
467         }
468         return NULL;
469 }
470
471 char* matches_exception(const char* host)
472 {
473         char host2[MAXBUF];
474         snprintf(host2,MAXBUF,"*@%s",host);
475         for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
476         {
477                 if ((match(host,i->hostmask)) || (match(host2,i->hostmask)))
478                 {
479                         return i->reason;
480                 }
481         }
482         return NULL;
483 }
484
485
486 void gline_set_creation_time(char* host, time_t create_time)
487 {
488         for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
489         {
490                 if (!strcasecmp(host,i->hostmask))
491                 {
492                         i->set_time = create_time;
493                         return;
494                 }
495         }
496         return ;        
497 }
498
499 void qline_set_creation_time(char* nick, time_t create_time)
500 {
501         for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
502         {
503                 if (!strcasecmp(nick,i->nick))
504                 {
505                         i->set_time = create_time;
506                         return;
507                 }
508         }
509         return ;        
510 }
511
512 void zline_set_creation_time(char* ip, time_t create_time)
513 {
514         for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
515         {
516                 if (!strcasecmp(ip,i->ipaddr))
517                 {
518                         i->set_time = create_time;
519                         return;
520                 }
521         }
522         return ;        
523 }
524
525 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
526
527 char* matches_zline(const char* ipaddr)
528 {
529         for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
530         {
531                 if (match(ipaddr,i->ipaddr))
532                 {
533                         return i->reason;
534                 }
535         }
536         return NULL;
537 }
538
539 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
540
541 char* matches_kline(const char* host)
542 {
543         for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
544         {
545                 if (match(host,i->hostmask))
546                 {
547                         return i->reason;
548                 }
549         }
550         return NULL;
551 }
552
553 // removes lines that have expired
554
555 void expire_lines()
556 {
557         bool go_again = true;
558         time_t current = time(NULL);
559         
560         // because we mess up an iterator when we remove from the vector, we must bail from
561         // the loop early if we delete an item, therefore this outer while loop is required.
562         while (go_again)
563         {
564                 go_again = false;
565
566                 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
567                 {
568                         if ((current > (i->duration + i->set_time)) && (i->duration > 0))
569                         {
570                                 WriteOpers("Expiring timed K-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
571                                 klines.erase(i);
572                                 go_again = true;
573                                 break;
574                         }
575                 }
576
577                 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
578                 {
579                         if ((current > (i->duration + i->set_time)) && (i->duration > 0))
580                         {
581                                 WriteOpers("Expiring timed E-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
582                                 elines.erase(i);
583                                 go_again = true;
584                                 break;
585                         }
586                 }
587
588                 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
589                 {
590                         if ((current > (i->duration + i->set_time)) && (i->duration > 0))
591                         {
592                                 WriteOpers("Expiring timed G-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
593                                 glines.erase(i);
594                                 go_again = true;
595                                 break;
596                         }
597                 }
598
599                 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
600                 {
601                         if ((current > (i->duration + i->set_time)) && (i->duration > 0))
602                         {
603                                 WriteOpers("Expiring timed Z-Line %s (set by %s %d seconds ago)",i->ipaddr,i->source,i->duration);
604                                 zlines.erase(i);
605                                 go_again = true;
606                                 break;
607                         }
608                 }
609
610                 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
611                 {
612                         if ((current > (i->duration + i->set_time)) && (i->duration > 0))
613                         {
614                                 WriteOpers("Expiring timed Q-Line %s (set by %s %d seconds ago)",i->nick,i->source,i->duration);
615                                 qlines.erase(i);
616                                 go_again = true;
617                                 break;
618                         }
619                 }
620         }
621 }
622
623 // applies lines, removing clients and changing nicks etc as applicable
624
625 void apply_lines()
626 {
627         bool go_again = true;
628         char reason[MAXBUF];
629         char host[MAXBUF];
630         
631         if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()))
632                 return;
633         
634         while (go_again)
635         {
636                 go_again = false;
637                 for (user_hash::const_iterator u = clientlist.begin(); u != clientlist.end(); u++)
638                 {
639                         if (!strcasecmp(u->second->server,ServerName))
640                         {
641                                 snprintf(host,MAXBUF,"%s@%s",u->second->ident,u->second->host);
642                                 if (elines.size())
643                                 {
644                                         // ignore people matching exempts
645                                         if (matches_exception(host))
646                                                 continue;
647                                 }
648                                 if (glines.size())
649                                 {
650                                         char* check = matches_gline(host);
651                                         if (check)
652                                         {
653                                                 WriteOpers("*** User %s matches G-Line: %s",u->second->nick,check);
654                                                 snprintf(reason,MAXBUF,"G-Lined: %s",check);
655                                                 kill_link(u->second,reason);
656                                                 go_again = true;
657                                                 break;
658                                         }
659                                 }
660                                 if (klines.size())
661                                 {
662                                         char* check = matches_kline(host);
663                                         if (check)
664                                         {
665                                                 WriteOpers("*** User %s matches K-Line: %s",u->second->nick,check);
666                                                 snprintf(reason,MAXBUF,"K-Lined: %s",check);
667                                                 kill_link(u->second,reason);
668                                                 go_again = true;
669                                                 break;
670                                         }
671                                 }
672                                 if (qlines.size())
673                                 {
674                                         char* check = matches_qline(u->second->nick);
675                                         if (check)
676                                         {
677                                                 snprintf(reason,MAXBUF,"Matched Q-Lined nick: %s",check);
678                                                 WriteOpers("*** Q-Lined nickname %s from %s: %s",u->second->nick,u->second->host,check);
679                                                 kill_link(u->second,reason);
680                                                 go_again = true;
681                                                 break;
682                                         }
683                                 }
684                                 if (zlines.size())
685                                 {
686                                         char* check = matches_zline(u->second->ip);
687                                         if (check)
688                                         {
689                                                 snprintf(reason,MAXBUF,"Z-Lined: %s",check);
690                                                 WriteOpers("*** User %s matches Z-Line: %s",u->second->nick,u->second->host,check);
691                                                 kill_link(u->second,reason);
692                                                 go_again = true;
693                                                 break;
694                                         }
695                                 }
696                         }
697                 }
698         }
699 }
700
701 void stats_k(userrec* user)
702 {
703         for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
704         {
705                 WriteServ(user->fd,"216 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
706         }
707 }
708
709 void stats_g(userrec* user)
710 {
711         for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
712         {
713                 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
714         }
715 }
716
717 void stats_q(userrec* user)
718 {
719         for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
720         {
721                 WriteServ(user->fd,"217 %s :%s %d %d %s %s",user->nick,i->nick,i->set_time,i->duration,i->source,i->reason);
722         }
723 }
724
725 void stats_z(userrec* user)
726 {
727         for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
728         {
729                 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->ipaddr,i->set_time,i->duration,i->source,i->reason);
730         }
731 }
732
733 void stats_e(userrec* user)
734 {
735         for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
736         {
737                 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
738         }
739 }