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