]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/xline.cpp
Added squit stuff... dont like recursive delete.
[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         while (go_again)
469         {
470                 go_again = false;
471
472                 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
473                 {
474                         if ((current > (i->duration + i->set_time)) && (i->duration > 0))
475                         {
476                                 WriteOpers("Expiring timed K-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
477                                 klines.erase(i);
478                                 go_again = true;
479                                 break;
480                         }
481                 }
482
483                 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
484                 {
485                         if ((current > (i->duration + i->set_time)) && (i->duration > 0))
486                         {
487                                 WriteOpers("Expiring timed E-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
488                                 elines.erase(i);
489                                 go_again = true;
490                                 break;
491                         }
492                 }
493
494                 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
495                 {
496                         if ((current > (i->duration + i->set_time)) && (i->duration > 0))
497                         {
498                                 WriteOpers("Expiring timed G-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
499                                 glines.erase(i);
500                                 go_again = true;
501                                 break;
502                         }
503                 }
504
505                 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
506                 {
507                         if ((current > (i->duration + i->set_time)) && (i->duration > 0))
508                         {
509                                 WriteOpers("Expiring timed Z-Line %s (set by %s %d seconds ago)",i->ipaddr,i->source,i->duration);
510                                 zlines.erase(i);
511                                 go_again = true;
512                                 break;
513                         }
514                 }
515
516                 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
517                 {
518                         if ((current > (i->duration + i->set_time)) && (i->duration > 0))
519                         {
520                                 WriteOpers("Expiring timed Q-Line %s (set by %s %d seconds ago)",i->nick,i->source,i->duration);
521                                 qlines.erase(i);
522                                 go_again = true;
523                                 break;
524                         }
525                 }
526         }
527 }
528
529 // applies lines, removing clients and changing nicks etc as applicable
530
531 void apply_lines()
532 {
533         bool go_again = true;
534         char reason[MAXBUF];
535         char host[MAXBUF];
536         
537         if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()))
538                 return;
539         
540         while (go_again)
541         {
542                 go_again = false;
543                 for (user_hash::const_iterator u = clientlist.begin(); u != clientlist.end(); u++)
544                 {
545                         if (!strcasecmp(u->second->server,ServerName))
546                         {
547                                 snprintf(host,MAXBUF,"%s@%s",u->second->ident,u->second->host);
548                                 if (elines.size())
549                                 {
550                                         // ignore people matching exempts
551                                         if (matches_exception(host))
552                                                 continue;
553                                 }
554                                 if (glines.size())
555                                 {
556                                         char* check = matches_gline(host);
557                                         if (check)
558                                         {
559                                                 WriteOpers("*** User %s matches G-Line: %s",u->second->registered == 7 ? u->second->nick:"<unknown>",check);
560                                                 snprintf(reason,MAXBUF,"G-Lined: %s",check);
561                                                 kill_link(u->second,reason);
562                                                 go_again = true;
563                                                 break;
564                                         }
565                                 }
566                                 if (klines.size())
567                                 {
568                                         char* check = matches_kline(host);
569                                         if (check)
570                                         {
571                                                 WriteOpers("*** User %s matches K-Line: %s",u->second->registered == 7 ? u->second->nick:"<unknown>",check);
572                                                 snprintf(reason,MAXBUF,"K-Lined: %s",check);
573                                                 kill_link(u->second,reason);
574                                                 go_again = true;
575                                                 break;
576                                         }
577                                 }
578                                 if (qlines.size())
579                                 {
580                                         char* check = matches_qline(u->second->nick);
581                                         if (check)
582                                         {
583                                                 snprintf(reason,MAXBUF,"Matched Q-Lined nick: %s",check);
584                                                 WriteOpers("*** Q-Lined nickname %s from %s: %s",u->second->registered == 7 ? u->second->nick:"<unknown>",u->second->host,check);
585                                                 kill_link(u->second,reason);
586                                                 go_again = true;
587                                                 break;
588                                         }
589                                 }
590                                 if (zlines.size())
591                                 {
592                                         char* check = matches_zline(u->second->ip);
593                                         if (check)
594                                         {
595                                                 snprintf(reason,MAXBUF,"Z-Lined: %s",check);
596                                                 WriteOpers("*** User %s matches Z-Line: %s",u->second->registered == 7 ? u->second->nick:"<unknown>",u->second->host,check);
597                                                 kill_link(u->second,reason);
598                                                 go_again = true;
599                                                 break;
600                                         }
601                                 }
602                         }
603                 }
604         }
605 }
606
607 void stats_k(userrec* user)
608 {
609         for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
610         {
611                 WriteServ(user->fd,"216 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
612         }
613 }
614
615 void stats_g(userrec* user)
616 {
617         for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
618         {
619                 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->hostmask,i->set_time,i->duration,i->source,i->reason);
620         }
621 }
622
623 void stats_q(userrec* user)
624 {
625         for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
626         {
627                 WriteServ(user->fd,"217 %s :%s %d %d %s %s",user->nick,i->nick,i->set_time,i->duration,i->source,i->reason);
628         }
629 }
630
631 void stats_z(userrec* user)
632 {
633         for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
634         {
635                 WriteServ(user->fd,"223 %s :%s %d %d %s %s",user->nick,i->ipaddr,i->set_time,i->duration,i->source,i->reason);
636         }
637 }
638
639 void stats_e(userrec* user)
640 {
641         for (std::vector<ELine>::iterator i = elines.begin(); i != elines.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 }