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