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