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,
217                           (GlineIsRemActive(gline) ?
218                            "* +%s%s%s %Tu :%s" : "* -%s%s%s"),
219                           gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
220                           GlineIsBadChan(gline) ? "" : gline->gl_host,
221                           gline->gl_expire - CurrentTime, gline->gl_reason);
222 }
223
224 int 
225 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
226           char *reason, time_t expire, time_t lastmod, unsigned int flags)
227 {
228   struct Gline *agline;
229
230   assert(0 != userhost);
231   assert(0 != reason);
232
233   /*
234    * You cannot set a negative (or zero) expire time, nor can you set an
235    * expiration time for greater than GLINE_MAX_EXPIRE.
236    */
237   if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
238     if (!IsServer(sptr) && MyConnect(sptr))
239       send_reply(sptr, ERR_BADEXPIRE, expire);
240     return 0;
241   }
242
243   expire += CurrentTime; /* convert from lifetime to timestamp */
244
245   /* NO_OLD_GLINE allows *@#channel to work correctly */
246   if (*userhost == '#' || *userhost == '&' || *userhost == '+'
247 # ifndef NO_OLD_GLINE
248       || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
249 # endif /* OLD_GLINE */
250       ) {
251 # ifndef LOCAL_BADCHAN
252     if (flags & GLINE_LOCAL)
253       return 0;
254 # endif
255     flags |= GLINE_BADCHAN;
256   }
257
258   /* Inform ops... */
259   sendto_opmask_butone(0, SNO_GLINE, "%s adding %s %s for %s, expiring at "
260                        "%Tu: %s",
261                        IsServer(sptr) ? sptr->name : sptr->user->server->name,
262                        flags & GLINE_LOCAL ? "local" : "global",
263                        flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
264                        expire + TSoffset, reason);
265
266   /* and log it */
267   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
268             "%#C adding %s %s for %s, expiring at %Tu: %s", sptr,
269             flags & GLINE_LOCAL ? "local" : "global",
270             flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
271             expire + TSoffset, reason);
272
273   /* make the gline */
274   agline = make_gline(userhost, reason, expire, lastmod, flags);
275
276   if (!agline) /* if it overlapped, silently return */
277     return 0;
278
279   propagate_gline(cptr, sptr, agline);
280
281   if (GlineIsBadChan(agline))
282     return 0;
283
284   return do_gline(cptr, sptr, agline); /* knock off users if necessary */
285 }
286
287 int
288 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
289                time_t lastmod, unsigned int flags)
290 {
291   unsigned int saveflags = 0;
292
293   assert(0 != gline);
294
295   saveflags = gline->gl_flags;
296
297   if (flags & GLINE_LOCAL)
298     gline->gl_flags &= ~GLINE_LDEACT;
299   else {
300     gline->gl_flags |= GLINE_ACTIVE;
301
302     if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
303       gline->gl_lastmod++;
304     else
305       gline->gl_lastmod = lastmod;
306   }
307
308   if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
309     return 0; /* was active to begin with */
310
311   /* Inform ops and log it */
312   sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
313                        "expiring at %Tu: %s",
314                        IsServer(sptr) ? sptr->name : sptr->user->server->name,
315                        GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
316                        gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
317                        GlineIsBadChan(gline) ? "" : gline->gl_host,
318                        gline->gl_expire + TSoffset, gline->gl_reason);
319
320   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
321             "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
322             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
323             GlineIsBadChan(gline) ? "" : "@",
324             GlineIsBadChan(gline) ? "" : gline->gl_host,
325             gline->gl_expire + TSoffset, gline->gl_reason);
326
327   if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
328     propagate_gline(cptr, sptr, gline);
329
330   return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
331 }
332
333 int
334 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
335                  time_t lastmod, unsigned int flags)
336 {
337   unsigned int saveflags = 0;
338   char *msg;
339
340   assert(0 != gline);
341
342   saveflags = gline->gl_flags;
343
344   if (GlineIsLocal(gline))
345     msg = "removing local";
346   else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
347     msg = "removing global";
348     gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
349   } else {
350     msg = "deactivating global";
351
352     if (flags & GLINE_LOCAL)
353       gline->gl_flags |= GLINE_LDEACT;
354     else {
355       gline->gl_flags &= ~GLINE_ACTIVE;
356
357       if (gline->gl_lastmod >= lastmod)
358         gline->gl_lastmod++;
359       else
360         gline->gl_lastmod = lastmod;
361     }
362
363     if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
364       return 0; /* was inactive to begin with */
365   }
366
367   /* Inform ops and log it */
368   sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
369                        "%s",
370                        IsServer(sptr) ? sptr->name : sptr->user->server->name,
371                        msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
372                        gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
373                        GlineIsBadChan(gline) ? "" : gline->gl_host,
374                        gline->gl_expire + TSoffset, gline->gl_reason);
375
376   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
377             "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
378             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
379             GlineIsBadChan(gline) ? "" : "@",
380             GlineIsBadChan(gline) ? "" : gline->gl_host,
381             gline->gl_expire + TSoffset, gline->gl_reason);
382
383   if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
384     propagate_gline(cptr, sptr, gline);
385
386   /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
387   if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
388     gline_free(gline); /* get rid of it */
389
390   return 0;
391 }
392
393 struct Gline *
394 gline_find(char *userhost, unsigned int flags)
395 {
396   struct Gline *gline;
397   struct Gline *sgline;
398   char *user, *host, *t_uh;
399
400   if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
401     for (gline = BadChanGlineList; gline; gline = sgline) {
402       sgline = gline->gl_next;
403
404       if (gline->gl_expire <= CurrentTime)
405         gline_free(gline);
406       else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
407                (flags & GLINE_LASTMOD && !gline->gl_lastmod))
408         continue;
409       else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
410                 match(gline->gl_user, userhost)) == 0)
411         return gline;
412     }
413   }
414
415   if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
416       *userhost == '#' || *userhost == '&' || *userhost == '+'
417 #ifndef NO_OLD_GLINE
418       || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
419 #endif /* NO_OLD_GLINE */
420       )
421     return 0;
422
423   DupString(t_uh, userhost);
424   canon_userhost(t_uh, &user, &host, 0);
425
426   for (gline = GlobalGlineList; gline; gline = sgline) {
427     sgline = gline->gl_next;
428
429     if (gline->gl_expire <= CurrentTime)
430       gline_free(gline);
431     else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
432              (flags & GLINE_LASTMOD && !gline->gl_lastmod))
433       continue;
434     else if (flags & GLINE_EXACT) {
435       if (ircd_strcmp(gline->gl_host, host) == 0 &&
436           ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
437            ircd_strcmp(gline->gl_user, user) == 0))
438         break;
439     } else {
440       if (match(gline->gl_host, host) == 0 &&
441           ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
442            match(gline->gl_user, user) == 0))
443       break;
444     }
445   }
446
447   MyFree(t_uh);
448
449   return gline;
450 }
451
452 struct Gline *
453 gline_lookup(struct Client *cptr, unsigned int flags)
454 {
455   struct Gline *gline;
456   struct Gline *sgline;
457
458   for (gline = GlobalGlineList; gline; gline = sgline) {
459     sgline = gline->gl_next;
460
461     if (gline->gl_expire <= CurrentTime) {
462       gline_free(gline);
463       continue;
464     }
465     
466     if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
467              (flags & GLINE_LASTMOD && !gline->gl_lastmod))
468       continue;
469      
470     if (match(gline->gl_user, cptr->user->username) != 0)
471       continue;
472          
473     if (GlineIsIpMask(gline)) {
474       Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",cptr->ip.s_addr,gline->ipnum.s_addr,gline->bits));
475       if ((cptr->ip.s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
476         continue;
477     }
478     else {
479       if (match(gline->gl_host, cptr->user->host) != 0) 
480         continue;
481     }
482     return gline;
483   }
484   /*
485    * No Glines matched
486    */
487   return 0;
488 }
489
490 void
491 gline_free(struct Gline *gline)
492 {
493   assert(0 != gline);
494
495   *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
496   if (gline->gl_next)
497     gline->gl_next->gl_prev_p = gline->gl_prev_p;
498
499   MyFree(gline->gl_user); /* free up the memory */
500   if (gline->gl_host)
501     MyFree(gline->gl_host);
502   MyFree(gline->gl_reason);
503   MyFree(gline);
504 }
505
506 void
507 gline_burst(struct Client *cptr)
508 {
509   struct Gline *gline;
510   struct Gline *sgline;
511
512   for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
513     sgline = gline->gl_next;
514
515     if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
516       gline_free(gline);
517     else if (!GlineIsLocal(gline) && gline->gl_lastmod)
518       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s@%s %Tu %Tu :%s",
519                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
520                     gline->gl_host, gline->gl_expire - CurrentTime,
521                     gline->gl_lastmod, gline->gl_reason);
522   }
523
524   for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
525     sgline = gline->gl_next;
526
527     if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
528       gline_free(gline);
529     else if (!GlineIsLocal(gline) && gline->gl_lastmod)
530       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
531                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
532                     gline->gl_expire - CurrentTime, gline->gl_lastmod,
533                     gline->gl_reason);
534   }
535 }
536
537 int
538 gline_resend(struct Client *cptr, struct Gline *gline)
539 {
540   if (GlineIsLocal(gline) || !gline->gl_lastmod)
541     return 0;
542
543   sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
544                 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
545                 GlineIsBadChan(gline) ? "" : "@",
546                 GlineIsBadChan(gline) ? "" : gline->gl_host,
547                 gline->gl_expire - CurrentTime, gline->gl_lastmod,
548                 gline->gl_reason);
549
550   return 0;
551 }
552
553 int
554 gline_list(struct Client *sptr, char *userhost)
555 {
556   struct Gline *gline;
557   struct Gline *sgline;
558
559   if (userhost) {
560     if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
561       return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
562
563     /* send gline information along */
564     send_reply(sptr, RPL_GLIST, gline->gl_user,
565                GlineIsBadChan(gline) ? "" : "@",
566                GlineIsBadChan(gline) ? "" : gline->gl_host,
567                gline->gl_expire + TSoffset,
568                GlineIsLocal(gline) ? me.name : "*",
569                GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
570   } else {
571     for (gline = GlobalGlineList; gline; gline = sgline) {
572       sgline = gline->gl_next;
573
574       if (gline->gl_expire <= CurrentTime)
575         gline_free(gline);
576       else
577         send_reply(sptr, RPL_GLIST, gline->gl_user, "@", gline->gl_host,
578                    gline->gl_expire + TSoffset,
579                    GlineIsLocal(gline) ? me.name : "*",
580                    GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
581     }
582
583     for (gline = BadChanGlineList; gline; gline = sgline) {
584       sgline = gline->gl_next;
585
586       if (gline->gl_expire <= CurrentTime)
587         gline_free(gline);
588       else
589         send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
590                    gline->gl_expire + TSoffset,
591                    GlineIsLocal(gline) ? me.name : "*",
592                    GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
593     }
594   }
595
596   /* end of gline information */
597   return send_reply(sptr, RPL_ENDOFGLIST);
598 }
599
600 void
601 gline_stats(struct Client *sptr)
602 {
603   struct Gline *gline;
604   struct Gline *sgline;
605
606   for (gline = GlobalGlineList; gline; gline = sgline) {
607     sgline = gline->gl_next;
608
609     if (gline->gl_expire <= CurrentTime)
610       gline_free(gline);
611     else
612       send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user, gline->gl_host,
613                  gline->gl_expire + TSoffset, gline->gl_reason);
614   }
615 }