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