Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / gline.c
1 /*
2  * IRC - Internet Relay Chat, ircd/gline.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Finland
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 1, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * $Id$
21  */
22 #include "gline.h"
23 #include "client.h"
24 #include "ircd.h"
25 #include "ircd_alloc.h"
26 #include "ircd_log.h"
27 #include "ircd_reply.h"
28 #include "ircd_string.h"
29 #include "match.h"
30 #include "numeric.h"
31 #include "s_bsd.h"
32 #include "s_debug.h"
33 #include "s_misc.h"
34 #include "send.h"
35 #include "struct.h"
36 #include "support.h"
37 #include "msg.h"
38 #include "numnicks.h"
39 #include "numeric.h"
40 #include "sys.h"    /* FALSE bleah */
41
42 #include <assert.h>
43 #include <string.h>
44 #include <stdio.h>
45 #include <arpa/inet.h> /* for inet_ntoa */
46
47 struct Gline* GlobalGlineList  = 0;
48 struct Gline* BadChanGlineList = 0;
49
50 static void
51 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
52 {
53   char *tmp;
54
55   if (!(tmp = strchr(userhost, '@'))) {
56     *user_p = def_user;
57     *host_p = userhost;
58   } else {
59     *user_p = userhost;
60     *(tmp++) = '\0';
61     *host_p = tmp;
62   }
63 }
64
65 static struct Gline *
66 make_gline(char *userhost, char *reason, time_t expire, time_t lastmod,
67            unsigned int flags)
68 {
69   struct Gline *gline, *sgline, *after = 0;
70   char *user, *host;
71
72   if (!(flags & GLINE_BADCHAN)) { /* search for overlapping glines first */
73     canon_userhost(userhost, &user, &host, "*"); /* find user and host */
74
75     for (gline = GlobalGlineList; gline; gline = sgline) {
76       sgline = gline->gl_next;
77
78       if (gline->gl_expire <= CurrentTime)
79         gline_free(gline);
80       else if ((gline->gl_flags & GLINE_LOCAL) != (flags & GLINE_LOCAL))
81         continue;
82       else if (!mmatch(gline->gl_user, user) && /* gline contains new mask */
83                !mmatch(gline->gl_host, host)) {
84         if (expire <= gline->gl_expire) /* will expire before wider gline */
85           return 0;
86         else
87           after = gline; /* stick new gline after this one */
88       } else if (!mmatch(user, gline->gl_user) && /* new mask contains gline */
89                  !mmatch(host, gline->gl_host) &&
90                  gline->gl_expire <= expire) /* gline expires before new one */
91         gline_free(gline); /* save some memory */
92     }
93   }
94
95   gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
96   assert(0 != gline);
97
98   DupString(gline->gl_reason, reason); /* initialize gline... */
99   gline->gl_expire = expire;
100   gline->gl_lastmod = lastmod;
101   gline->gl_flags = flags & GLINE_MASK;
102
103   if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
104     DupString(gline->gl_user, userhost); /* first, remember channel */
105     gline->gl_host = 0;
106
107     gline->gl_next = BadChanGlineList; /* then link it into list */
108     gline->gl_prev_p = &BadChanGlineList;
109     if (BadChanGlineList)
110       BadChanGlineList->gl_prev_p = &gline->gl_next;
111     BadChanGlineList = gline;
112   } else {
113     DupString(gline->gl_user, user); /* remember them... */
114     DupString(gline->gl_host, host);
115
116     if (check_if_ipmask(host)) { /* mark if it's an IP mask */
117       int class;
118       char ipname[16];
119       int ad[4] = { 0 };
120       int bits2 = 0;
121        
122       class = sscanf(host,"%d.%d.%d.%d/%d",
123                      &ad[0],&ad[1],&ad[2],&ad[3], &bits2);
124       if (class!=5) {
125         gline->bits=class*8;
126       }
127       else {
128         gline->bits=bits2;
129       }
130       sprintf_irc(ipname,"%d.%d.%d.%d",ad[0],ad[1],ad[2],ad[3]);
131       gline->ipnum.s_addr = inet_addr(ipname);
132       Debug((DEBUG_DEBUG,"IP gline: %08x/%i",gline->ipnum.s_addr,gline->bits));
133       gline->gl_flags |= GLINE_IPMASK;
134     }
135
136     if (after) {
137       gline->gl_next = after->gl_next;
138       gline->gl_prev_p = &after->gl_next;
139       if (after->gl_next)
140         after->gl_next->gl_prev_p = &gline->gl_next;
141       after->gl_next = gline;
142     } else {
143       gline->gl_next = GlobalGlineList; /* then link it into list */
144       gline->gl_prev_p = &GlobalGlineList;
145       if (GlobalGlineList)
146         GlobalGlineList->gl_prev_p = &gline->gl_next;
147       GlobalGlineList = gline;
148     }
149   }
150
151   return gline;
152 }
153
154 static int
155 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
156 {
157   struct Client *acptr;
158   int fd, retval = 0, tval;
159
160   if (!GlineIsActive(gline)) /* no action taken on inactive glines */
161     return 0;
162
163   for (fd = HighestFd; fd >= 0; --fd) {
164     /*
165      * get the users!
166      */
167     if ((acptr = LocalClientArray[fd])) {
168       if (!acptr->user)
169         continue;
170         
171       if (acptr->user->username && 
172           match (gline->gl_user, acptr->user->username) != 0)
173                continue;
174           
175       if (GlineIsIpMask(gline)) {
176         Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",cptr->ip.s_addr,gline->ipnum.s_addr,gline->bits));
177         if ((acptr->ip.s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
178           continue;
179       }
180       else {
181         if (match(gline->gl_host, acptr->sockhost) != 0)
182           continue;
183       }
184
185       /* ok, here's one that got G-lined */
186       send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
187            gline->gl_reason);
188
189       /* let the ops know about it */
190       sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
191                      get_client_name(acptr, FALSE));
192
193       /* and get rid of him */
194       if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
195           gline->gl_reason)))
196         retval = tval; /* retain killed status */
197     }
198   }
199   return retval;
200 }
201
202 static void
203 propagate_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
204 {
205   if (GlineIsLocal(gline) || (IsUser(sptr) && !gline->gl_lastmod))
206     return;
207
208   if (gline->gl_lastmod)
209     sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
210                           GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
211                           GlineIsBadChan(gline) ? "" : "@",
212                           GlineIsBadChan(gline) ? "" : gline->gl_host,
213                           gline->gl_expire - CurrentTime, gline->gl_lastmod,
214                           gline->gl_reason);
215   else
216     sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu :%s",
217                           GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
218                           GlineIsBadChan(gline) ? "" : "@",
219                           GlineIsBadChan(gline) ? "" : gline->gl_host,
220                           gline->gl_expire - CurrentTime, gline->gl_reason);
221 }
222
223 int 
224 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
225           char *reason, time_t expire, time_t lastmod, unsigned int flags)
226 {
227   struct Gline *agline;
228
229   assert(0 != userhost);
230   assert(0 != reason);
231
232   /*
233    * You cannot set a negative (or zero) expire time, nor can you set an
234    * expiration time for greater than GLINE_MAX_EXPIRE.
235    */
236   if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
237     if (!IsServer(sptr) && MyConnect(sptr))
238       send_reply(sptr, ERR_BADEXPIRE, expire);
239     return 0;
240   }
241
242   expire += CurrentTime; /* convert from lifetime to timestamp */
243
244   /* NO_OLD_GLINE allows *@#channel to work correctly */
245   if (*userhost == '#' || *userhost == '&' || *userhost == '+'
246 # ifndef NO_OLD_GLINE
247       || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
248 # endif /* OLD_GLINE */
249       ) {
250 # ifndef LOCAL_BADCHAN
251     if (flags & GLINE_LOCAL)
252       return 0;
253 # endif
254     flags |= GLINE_BADCHAN;
255   }
256
257   /* Inform ops... */
258   sendto_opmask_butone(0, SNO_GLINE, "%s adding %s %s for %s, expiring at "
259                        "%Tu: %s",
260                        IsServer(sptr) ? sptr->name : sptr->user->server->name,
261                        flags & GLINE_LOCAL ? "local" : "global",
262                        flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
263                        expire + TSoffset, reason);
264
265   /* and log it */
266   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
267             "%#C adding %s %s for %s, expiring at %Tu: %s", sptr,
268             flags & GLINE_LOCAL ? "local" : "global",
269             flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
270             expire + TSoffset, reason);
271
272   /* make the gline */
273   agline = make_gline(userhost, reason, expire, lastmod, flags);
274
275   if (!agline) /* if it overlapped, silently return */
276     return 0;
277
278   propagate_gline(cptr, sptr, agline);
279
280   if (GlineIsBadChan(agline))
281     return 0;
282
283   return do_gline(cptr, sptr, agline); /* knock off users if necessary */
284 }
285
286 int
287 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
288                time_t lastmod, unsigned int flags)
289 {
290   unsigned int saveflags = 0;
291
292   assert(0 != gline);
293
294   saveflags = gline->gl_flags;
295
296   if (flags & GLINE_LOCAL)
297     gline->gl_flags &= ~GLINE_LDEACT;
298   else {
299     gline->gl_flags |= GLINE_ACTIVE;
300
301     if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
302       gline->gl_lastmod++;
303     else
304       gline->gl_lastmod = lastmod;
305   }
306
307   if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
308     return 0; /* was active to begin with */
309
310   /* Inform ops and log it */
311   sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
312                        "expiring at %Tu: %s",
313                        IsServer(sptr) ? sptr->name : sptr->user->server->name,
314                        GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
315                        gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
316                        GlineIsBadChan(gline) ? "" : gline->gl_host,
317                        gline->gl_expire + TSoffset, gline->gl_reason);
318
319   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
320             "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
321             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
322             GlineIsBadChan(gline) ? "" : "@",
323             GlineIsBadChan(gline) ? "" : gline->gl_host,
324             gline->gl_expire + TSoffset, gline->gl_reason);
325
326   if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
327     propagate_gline(cptr, sptr, gline);
328
329   return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
330 }
331
332 int
333 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
334                  time_t lastmod, unsigned int flags)
335 {
336   unsigned int saveflags = 0;
337   char *msg;
338
339   assert(0 != gline);
340
341   saveflags = gline->gl_flags;
342
343   if (GlineIsLocal(gline))
344     msg = "removing local";
345   else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL))
346     msg = "removing global";
347   else {
348     msg = "deactivating global";
349
350     if (flags & GLINE_LOCAL)
351       gline->gl_flags |= GLINE_LDEACT;
352     else {
353       gline->gl_flags &= ~GLINE_ACTIVE;
354
355       if (gline->gl_lastmod >= lastmod)
356         gline->gl_lastmod++;
357       else
358         gline->gl_lastmod = lastmod;
359     }
360
361     if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
362       return 0; /* was inactive to begin with */
363   }
364
365   /* Inform ops and log it */
366   sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
367                        "%s",
368                        IsServer(sptr) ? sptr->name : sptr->user->server->name,
369                        msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
370                        gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
371                        GlineIsBadChan(gline) ? "" : gline->gl_host,
372                        gline->gl_expire + TSoffset, gline->gl_reason);
373
374   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
375             "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
376             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
377             GlineIsBadChan(gline) ? "" : "@",
378             GlineIsBadChan(gline) ? "" : gline->gl_host,
379             gline->gl_expire + TSoffset, gline->gl_reason);
380
381   if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
382     gline_free(gline);
383   else if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
384     propagate_gline(cptr, sptr, gline);
385
386   return 0;
387 }
388
389 struct Gline *
390 gline_find(char *userhost, unsigned int flags)
391 {
392   struct Gline *gline;
393   struct Gline *sgline;
394   char *user, *host, *t_uh;
395
396   if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
397     for (gline = BadChanGlineList; gline; gline = sgline) {
398       sgline = gline->gl_next;
399
400       if (gline->gl_expire <= CurrentTime)
401         gline_free(gline);
402       else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
403                (flags & GLINE_LASTMOD && !gline->gl_lastmod))
404         continue;
405       else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
406                 match(gline->gl_user, userhost)) == 0)
407         return gline;
408     }
409   }
410
411   if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
412       *userhost == '#' || *userhost == '&' || *userhost == '+'
413 #ifndef NO_OLD_GLINE
414       || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
415 #endif /* NO_OLD_GLINE */
416       )
417     return 0;
418
419   DupString(t_uh, userhost);
420   canon_userhost(t_uh, &user, &host, 0);
421
422   for (gline = GlobalGlineList; gline; gline = sgline) {
423     sgline = gline->gl_next;
424
425     if (gline->gl_expire <= CurrentTime)
426       gline_free(gline);
427     else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
428              (flags & GLINE_LASTMOD && !gline->gl_lastmod))
429       continue;
430     else if (flags & GLINE_EXACT) {
431       if (ircd_strcmp(gline->gl_host, host) == 0 &&
432           ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
433            ircd_strcmp(gline->gl_user, user) == 0))
434         break;
435     } else {
436       if (match(gline->gl_host, host) == 0 &&
437           ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
438            match(gline->gl_user, user) == 0))
439       break;
440     }
441   }
442
443   MyFree(t_uh);
444
445   return gline;
446 }
447
448 struct Gline *
449 gline_lookup(struct Client *cptr, unsigned int flags)
450 {
451   struct Gline *gline;
452   struct Gline *sgline;
453
454   for (gline = GlobalGlineList; gline; gline = sgline) {
455     sgline = gline->gl_next;
456
457     if (gline->gl_expire <= CurrentTime) {
458       gline_free(gline);
459       continue;
460     }
461     
462     if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
463              (flags & GLINE_LASTMOD && !gline->gl_lastmod))
464       continue;
465      
466     if (match(gline->gl_user, cptr->user->username) != 0)
467       continue;
468          
469     if (GlineIsIpMask(gline)) {
470       Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",cptr->ip.s_addr,gline->ipnum.s_addr,gline->bits));
471       if ((cptr->ip.s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
472         continue;
473     }
474     else {
475       if (match(gline->gl_host, cptr->user->host) != 0) 
476         continue;
477     }
478     return gline;
479   }
480   /*
481    * No Glines matched
482    */
483   return 0;
484 }
485
486 void
487 gline_free(struct Gline *gline)
488 {
489   assert(0 != gline);
490
491   *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
492   if (gline->gl_next)
493     gline->gl_next->gl_prev_p = gline->gl_prev_p;
494
495   MyFree(gline->gl_user); /* free up the memory */
496   if (gline->gl_host)
497     MyFree(gline->gl_host);
498   MyFree(gline->gl_reason);
499   MyFree(gline);
500 }
501
502 void
503 gline_burst(struct Client *cptr)
504 {
505   struct Gline *gline;
506   struct Gline *sgline;
507
508   for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
509     sgline = gline->gl_next;
510
511     if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
512       gline_free(gline);
513     else if (!GlineIsLocal(gline) && gline->gl_lastmod)
514       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s@%s %Tu %Tu :%s",
515                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
516                     gline->gl_host, gline->gl_expire - CurrentTime,
517                     gline->gl_lastmod, gline->gl_reason);
518   }
519
520   for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
521     sgline = gline->gl_next;
522
523     if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
524       gline_free(gline);
525     else if (!GlineIsLocal(gline) && gline->gl_lastmod)
526       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
527                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
528                     gline->gl_expire - CurrentTime, gline->gl_lastmod,
529                     gline->gl_reason);
530   }
531 }
532
533 int
534 gline_resend(struct Client *cptr, struct Gline *gline)
535 {
536   if (GlineIsLocal(gline) || !gline->gl_lastmod)
537     return 0;
538
539   sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
540                 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
541                 GlineIsBadChan(gline) ? "" : "@",
542                 GlineIsBadChan(gline) ? "" : gline->gl_host,
543                 gline->gl_expire - CurrentTime, gline->gl_lastmod,
544                 gline->gl_reason);
545
546   return 0;
547 }
548
549 int
550 gline_list(struct Client *sptr, char *userhost)
551 {
552   struct Gline *gline;
553   struct Gline *sgline;
554
555   if (userhost) {
556     if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
557       return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
558
559     /* send gline information along */
560     send_reply(sptr, RPL_GLIST, gline->gl_user,
561                GlineIsBadChan(gline) ? "" : "@",
562                GlineIsBadChan(gline) ? "" : gline->gl_host,
563                gline->gl_expire + TSoffset,
564                GlineIsLocal(gline) ? me.name : "*",
565                GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
566   } else {
567     for (gline = GlobalGlineList; gline; gline = sgline) {
568       sgline = gline->gl_next;
569
570       if (gline->gl_expire <= CurrentTime)
571         gline_free(gline);
572       else
573         send_reply(sptr, RPL_GLIST, gline->gl_user, "@", gline->gl_host,
574                    gline->gl_expire + TSoffset,
575                    GlineIsLocal(gline) ? me.name : "*",
576                    GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
577     }
578
579     for (gline = BadChanGlineList; gline; gline = sgline) {
580       sgline = gline->gl_next;
581
582       if (gline->gl_expire <= CurrentTime)
583         gline_free(gline);
584       else
585         send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
586                    gline->gl_expire + TSoffset,
587                    GlineIsLocal(gline) ? me.name : "*",
588                    GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
589     }
590   }
591
592   /* end of gline information */
593   return send_reply(sptr, RPL_ENDOFGLIST);
594 }
595
596 void
597 gline_stats(struct Client *sptr)
598 {
599   struct Gline *gline;
600   struct Gline *sgline;
601
602   for (gline = GlobalGlineList; gline; gline = sgline) {
603     sgline = gline->gl_next;
604
605     if (gline->gl_expire <= CurrentTime)
606       gline_free(gline);
607     else
608       send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user, gline->gl_host,
609                  gline->gl_expire + TSoffset, gline->gl_reason);
610   }
611 }