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 (!cli_user(acptr))
169         continue;
170         
171       if (cli_user(acptr)->username && 
172           match (gline->gl_user, (cli_user(acptr))->username) != 0)
173                continue;
174           
175       if (GlineIsIpMask(gline)) {
176         Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",(cli_ip(cptr)).s_addr,gline->ipnum.s_addr,gline->bits));
177         if (((cli_ip(acptr)).s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
178           continue;
179       }
180       else {
181         if (match(gline->gl_host, cli_sockhost(acptr)) != 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     if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
252       return send_reply(sptr, ERR_NOPRIVILEGES);
253
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", IsServer(sptr) ? cli_name(sptr) :
260                        cli_name((cli_user(sptr))->server),
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", IsServer(sptr) ? cli_name(sptr) :
313                        cli_name((cli_user(sptr))->server),
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     gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
348   } else {
349     msg = "deactivating global";
350
351     if (flags & GLINE_LOCAL)
352       gline->gl_flags |= GLINE_LDEACT;
353     else {
354       gline->gl_flags &= ~GLINE_ACTIVE;
355
356       if (gline->gl_lastmod >= lastmod)
357         gline->gl_lastmod++;
358       else
359         gline->gl_lastmod = lastmod;
360     }
361
362     if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
363       return 0; /* was inactive to begin with */
364   }
365
366   /* Inform ops and log it */
367   sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
368                        "%s", IsServer(sptr) ? cli_name(sptr) :
369                        cli_name((cli_user(sptr))->server),
370                        msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
371                        gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
372                        GlineIsBadChan(gline) ? "" : gline->gl_host,
373                        gline->gl_expire + TSoffset, gline->gl_reason);
374
375   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
376             "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
377             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
378             GlineIsBadChan(gline) ? "" : "@",
379             GlineIsBadChan(gline) ? "" : gline->gl_host,
380             gline->gl_expire + TSoffset, gline->gl_reason);
381
382   if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
383     propagate_gline(cptr, sptr, gline);
384
385   /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
386   if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
387     gline_free(gline); /* get rid of it */
388
389   return 0;
390 }
391
392 struct Gline *
393 gline_find(char *userhost, unsigned int flags)
394 {
395   struct Gline *gline;
396   struct Gline *sgline;
397   char *user, *host, *t_uh;
398
399   if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
400     for (gline = BadChanGlineList; gline; gline = sgline) {
401       sgline = gline->gl_next;
402
403       if (gline->gl_expire <= CurrentTime)
404         gline_free(gline);
405       else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
406                (flags & GLINE_LASTMOD && !gline->gl_lastmod))
407         continue;
408       else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
409                 match(gline->gl_user, userhost)) == 0)
410         return gline;
411     }
412   }
413
414   if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
415       *userhost == '#' || *userhost == '&' || *userhost == '+'
416 #ifndef NO_OLD_GLINE
417       || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
418 #endif /* NO_OLD_GLINE */
419       )
420     return 0;
421
422   DupString(t_uh, userhost);
423   canon_userhost(t_uh, &user, &host, 0);
424
425   for (gline = GlobalGlineList; gline; gline = sgline) {
426     sgline = gline->gl_next;
427
428     if (gline->gl_expire <= CurrentTime)
429       gline_free(gline);
430     else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
431              (flags & GLINE_LASTMOD && !gline->gl_lastmod))
432       continue;
433     else if (flags & GLINE_EXACT) {
434       if (ircd_strcmp(gline->gl_host, host) == 0 &&
435           ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
436            ircd_strcmp(gline->gl_user, user) == 0))
437         break;
438     } else {
439       if (match(gline->gl_host, host) == 0 &&
440           ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
441            match(gline->gl_user, user) == 0))
442       break;
443     }
444   }
445
446   MyFree(t_uh);
447
448   return gline;
449 }
450
451 struct Gline *
452 gline_lookup(struct Client *cptr, unsigned int flags)
453 {
454   struct Gline *gline;
455   struct Gline *sgline;
456
457   for (gline = GlobalGlineList; gline; gline = sgline) {
458     sgline = gline->gl_next;
459
460     if (gline->gl_expire <= CurrentTime) {
461       gline_free(gline);
462       continue;
463     }
464     
465     if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
466              (flags & GLINE_LASTMOD && !gline->gl_lastmod))
467       continue;
468      
469     if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
470       continue;
471          
472     if (GlineIsIpMask(gline)) {
473       Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",(cli_ip(cptr)).s_addr,gline->ipnum.s_addr,gline->bits));
474       if (((cli_ip(cptr)).s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
475         continue;
476     }
477     else {
478       if (match(gline->gl_host, (cli_user(cptr))->host) != 0) 
479         continue;
480     }
481     return gline;
482   }
483   /*
484    * No Glines matched
485    */
486   return 0;
487 }
488
489 void
490 gline_free(struct Gline *gline)
491 {
492   assert(0 != gline);
493
494   *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
495   if (gline->gl_next)
496     gline->gl_next->gl_prev_p = gline->gl_prev_p;
497
498   MyFree(gline->gl_user); /* free up the memory */
499   if (gline->gl_host)
500     MyFree(gline->gl_host);
501   MyFree(gline->gl_reason);
502   MyFree(gline);
503 }
504
505 void
506 gline_burst(struct Client *cptr)
507 {
508   struct Gline *gline;
509   struct Gline *sgline;
510
511   for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
512     sgline = gline->gl_next;
513
514     if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
515       gline_free(gline);
516     else if (!GlineIsLocal(gline) && gline->gl_lastmod)
517       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s@%s %Tu %Tu :%s",
518                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
519                     gline->gl_host, gline->gl_expire - CurrentTime,
520                     gline->gl_lastmod, gline->gl_reason);
521   }
522
523   for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
524     sgline = gline->gl_next;
525
526     if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
527       gline_free(gline);
528     else if (!GlineIsLocal(gline) && gline->gl_lastmod)
529       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
530                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
531                     gline->gl_expire - CurrentTime, gline->gl_lastmod,
532                     gline->gl_reason);
533   }
534 }
535
536 int
537 gline_resend(struct Client *cptr, struct Gline *gline)
538 {
539   if (GlineIsLocal(gline) || !gline->gl_lastmod)
540     return 0;
541
542   sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
543                 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
544                 GlineIsBadChan(gline) ? "" : "@",
545                 GlineIsBadChan(gline) ? "" : gline->gl_host,
546                 gline->gl_expire - CurrentTime, gline->gl_lastmod,
547                 gline->gl_reason);
548
549   return 0;
550 }
551
552 int
553 gline_list(struct Client *sptr, char *userhost)
554 {
555   struct Gline *gline;
556   struct Gline *sgline;
557
558   if (userhost) {
559     if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
560       return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
561
562     /* send gline information along */
563     send_reply(sptr, RPL_GLIST, gline->gl_user,
564                GlineIsBadChan(gline) ? "" : "@",
565                GlineIsBadChan(gline) ? "" : gline->gl_host,
566                gline->gl_expire + TSoffset,
567                GlineIsLocal(gline) ? cli_name(&me) : "*",
568                GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
569   } else {
570     for (gline = GlobalGlineList; gline; gline = sgline) {
571       sgline = gline->gl_next;
572
573       if (gline->gl_expire <= CurrentTime)
574         gline_free(gline);
575       else
576         send_reply(sptr, RPL_GLIST, gline->gl_user, "@", gline->gl_host,
577                    gline->gl_expire + TSoffset,
578                    GlineIsLocal(gline) ? cli_name(&me) : "*",
579                    GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
580     }
581
582     for (gline = BadChanGlineList; gline; gline = sgline) {
583       sgline = gline->gl_next;
584
585       if (gline->gl_expire <= CurrentTime)
586         gline_free(gline);
587       else
588         send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
589                    gline->gl_expire + TSoffset,
590                    GlineIsLocal(gline) ? cli_name(&me) : "*",
591                    GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
592     }
593   }
594
595   /* end of gline information */
596   return send_reply(sptr, RPL_ENDOFGLIST);
597 }
598
599 void
600 gline_stats(struct Client *sptr)
601 {
602   struct Gline *gline;
603   struct Gline *sgline;
604
605   for (gline = GlobalGlineList; gline; gline = sgline) {
606     sgline = gline->gl_next;
607
608     if (gline->gl_expire <= CurrentTime)
609       gline_free(gline);
610     else
611       send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user, gline->gl_host,
612                  gline->gl_expire + TSoffset, gline->gl_reason);
613   }
614 }