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