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