Author: net <simms@LUCIDA.QC.CA>
[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_reply.h"
27 #include "ircd_string.h"
28 #include "match.h"
29 #include "numeric.h"
30 #include "s_bsd.h"
31 #include "s_debug.h"
32 #include "s_misc.h"
33 #include "send.h"
34 #include "struct.h"
35 #include "support.h"
36 #include "msg.h"
37 #include "numnicks.h"
38 #include "numeric.h"
39 #include "sys.h"    /* FALSE bleah */
40
41 #include <assert.h>
42 #include <string.h>
43 #include <stdio.h>
44 #include <arpa/inet.h> /* for inet_ntoa */
45
46 struct Gline* GlobalGlineList  = 0;
47 struct Gline* BadChanGlineList = 0;
48
49 static void
50 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
51 {
52   char *tmp;
53
54   if (!(tmp = strchr(userhost, '@'))) {
55     *user_p = def_user;
56     *host_p = userhost;
57   } else {
58     *user_p = userhost;
59     *(tmp++) = '\0';
60     *host_p = tmp;
61   }
62 }
63
64 static struct Gline *
65 make_gline(char *userhost, char *reason, time_t expire, time_t lastmod,
66            unsigned int flags)
67 {
68   struct Gline *gline, *sgline, *after = 0;
69   char *user, *host;
70
71   if (!(flags & GLINE_BADCHAN)) { /* search for overlapping glines first */
72     canon_userhost(userhost, &user, &host, "*"); /* find user and host */
73
74     for (gline = GlobalGlineList; gline; gline = sgline) {
75       sgline = gline->gl_next;
76
77       if (gline->gl_expire <= CurrentTime)
78         gline_free(gline);
79       else if ((gline->gl_flags & GLINE_LOCAL) != (flags & GLINE_LOCAL))
80         continue;
81       else if (!mmatch(gline->gl_user, user) && /* gline contains new mask */
82                !mmatch(gline->gl_host, host)) {
83         if (expire <= gline->gl_expire) /* will expire before wider gline */
84           return 0;
85         else
86           after = gline; /* stick new gline after this one */
87       } else if (!mmatch(user, gline->gl_user) && /* new mask contains gline */
88                  !mmatch(host, gline->gl_host) &&
89                  gline->gl_expire <= expire) /* gline expires before new one */
90         gline_free(gline); /* save some memory */
91     }
92   }
93
94   gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
95   assert(0 != gline);
96
97   DupString(gline->gl_reason, reason); /* initialize gline... */
98   gline->gl_expire = expire;
99   gline->gl_lastmod = lastmod;
100   gline->gl_flags = flags & GLINE_MASK;
101
102   if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
103     DupString(gline->gl_user, userhost); /* first, remember channel */
104     gline->gl_host = 0;
105
106     gline->gl_next = BadChanGlineList; /* then link it into list */
107     gline->gl_prev_p = &BadChanGlineList;
108     if (BadChanGlineList)
109       BadChanGlineList->gl_prev_p = &gline->gl_next;
110     BadChanGlineList = gline;
111   } else {
112     DupString(gline->gl_user, user); /* remember them... */
113     DupString(gline->gl_host, host);
114
115     if (check_if_ipmask(host)) { /* mark if it's an IP mask */
116       int class;
117       char ipname[16];
118       int ad[4] = { 0 };
119       int bits2 = 0;
120        
121       class = sscanf(host,"%d.%d.%d.%d/%d",
122                      &ad[0],&ad[1],&ad[2],&ad[3], &bits2);
123       if (class!=5) {
124         gline->bits=class*8;
125       }
126       else {
127         gline->bits=bits2;
128       }
129       sprintf_irc(ipname,"%d.%d.%d.%d",ad[0],ad[1],ad[2],ad[3]);
130       gline->ipnum.s_addr = inet_addr(ipname);
131       Debug((DEBUG_DEBUG,"IP gline: %08x/%i",gline->ipnum.s_addr,gline->bits));
132       gline->gl_flags |= GLINE_IPMASK;
133     }
134
135     if (after) {
136       gline->gl_next = after->gl_next;
137       gline->gl_prev_p = &after->gl_next;
138       if (after->gl_next)
139         after->gl_next->gl_prev_p = &gline->gl_next;
140       after->gl_next = gline;
141     } else {
142       gline->gl_next = GlobalGlineList; /* then link it into list */
143       gline->gl_prev_p = &GlobalGlineList;
144       if (GlobalGlineList)
145         GlobalGlineList->gl_prev_p = &gline->gl_next;
146       GlobalGlineList = gline;
147     }
148   }
149
150   return gline;
151 }
152
153 static int
154 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
155 {
156   struct Client *acptr;
157   int fd, retval = 0, tval;
158
159   if (!GlineIsActive(gline)) /* no action taken on inactive glines */
160     return 0;
161
162   for (fd = HighestFd; fd >= 0; --fd) {
163     /*
164      * get the users!
165      */
166     if ((acptr = LocalClientArray[fd])) {
167       if (!acptr->user)
168         continue;
169         
170       if (acptr->user->username && 
171           match (gline->gl_user, acptr->user->username) != 0)
172                continue;
173           
174       if (GlineIsIpMask(gline)) {
175         Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",cptr->ip.s_addr,gline->ipnum.s_addr,gline->bits));
176         if ((acptr->ip.s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
177           continue;
178       }
179       else {
180         if (match(gline->gl_host, acptr->sockhost) != 0)
181           continue;
182       }
183
184       /* ok, here's one that got G-lined */
185       send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
186            gline->gl_reason);
187
188       /* let the ops know about it */
189       sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
190                      get_client_name(acptr, FALSE));
191
192       /* and get rid of him */
193       if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
194           gline->gl_reason)))
195         retval = tval; /* retain killed status */
196     }
197   }
198   return retval;
199 }
200
201 static void
202 propagate_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
203 {
204   if (GlineIsLocal(gline) || (IsUser(sptr) && !gline->gl_lastmod))
205     return;
206
207   if (gline->gl_lastmod)
208     sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
209                           GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
210                           GlineIsBadChan(gline) ? "" : "@",
211                           GlineIsBadChan(gline) ? "" : gline->gl_host,
212                           gline->gl_expire - CurrentTime, gline->gl_lastmod,
213                           gline->gl_reason);
214   else
215     sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu :%s",
216                           GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
217                           GlineIsBadChan(gline) ? "" : "@",
218                           GlineIsBadChan(gline) ? "" : gline->gl_host,
219                           gline->gl_expire - CurrentTime, gline->gl_reason);
220 }
221
222 int 
223 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
224           char *reason, time_t expire, time_t lastmod, unsigned int flags)
225 {
226   struct Gline *agline;
227
228   assert(0 != userhost);
229   assert(0 != reason);
230
231   /*
232    * You cannot set a negative (or zero) expire time, nor can you set an
233    * expiration time for greater than GLINE_MAX_EXPIRE.
234    */
235   if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
236     if (!IsServer(sptr) && MyConnect(sptr))
237       send_reply(sptr, ERR_BADEXPIRE, expire);
238     return 0;
239   }
240
241   expire += CurrentTime; /* convert from lifetime to timestamp */
242
243   /* NO_OLD_GLINE allows *@#channel to work correctly */
244   if (*userhost == '#' || *userhost == '&' || *userhost == '+'
245 # ifndef NO_OLD_GLINE
246       || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
247 # endif /* OLD_GLINE */
248       ) {
249 # ifndef LOCAL_BADCHAN
250     if (flags & GLINE_LOCAL)
251       return 0;
252 # endif
253     flags |= GLINE_BADCHAN;
254   }
255
256   /* Inform ops... */
257   sendto_opmask_butone(0, SNO_GLINE, "%s adding %s %s for %s, expiring at "
258                        "%Tu: %s",
259                        IsServer(sptr) ? sptr->name : sptr->user->server->name,
260                        flags & GLINE_LOCAL ? "local" : "global",
261                        flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
262                        expire + TSoffset, reason);
263
264 #ifdef GPATH
265   /* and log it */
266   write_log(GPATH, "# %Tu %C adding %s %s for %s, expiring at %Tu: %s\n",
267             TStime(), sptr, flags & GLINE_LOCAL ? "local" : "global",
268             flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
269             expire + TSoffset, reason);
270 #endif /* GPATH */
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 #ifdef GPATH
284   /* this can be inserted into the conf */
285   write_log(GPATH, "%c:%s:%s:%s\n", GlineIsIpMask(agline) ? 'k' : 'K',
286             GlineHost(agline), GlineReason(agline), GlineUser(agline));
287 #endif /* GPATH */
288
289   return do_gline(cptr, sptr, agline); /* knock off users if necessary */
290 }
291
292 int
293 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
294                time_t lastmod, unsigned int flags)
295 {
296   unsigned int saveflags = 0;
297
298   assert(0 != gline);
299
300   saveflags = gline->gl_flags;
301
302   if (flags & GLINE_LOCAL)
303     gline->gl_flags &= ~GLINE_LDEACT;
304   else {
305     gline->gl_flags |= GLINE_ACTIVE;
306
307     if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
308       gline->gl_lastmod++;
309     else
310       gline->gl_lastmod = lastmod;
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",
319                        IsServer(sptr) ? sptr->name : sptr->user->server->name,
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 #ifdef GPATH
326   write_log(GPATH, "# %Tu %C activating global %s for %s%s%s, expiring at "
327             "%Tu: %s\n", TStime(), sptr,
328             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
329             gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
330             GlineIsBadChan(gline) ? "" : gline->gl_host,
331             gline->gl_expire + TSoffset, gline->gl_reason);
332 #endif /* GPATH */
333
334   if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
335     propagate_gline(cptr, sptr, gline);
336
337   return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
338 }
339
340 int
341 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
342                  time_t lastmod, unsigned int flags)
343 {
344   unsigned int saveflags = 0;
345   char *msg;
346
347   assert(0 != gline);
348
349   saveflags = gline->gl_flags;
350
351   if (GlineIsLocal(gline))
352     msg = "removing local";
353   else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL))
354     msg = "removing global";
355   else {
356     msg = "deactivating global";
357
358     if (flags & GLINE_LOCAL)
359       gline->gl_flags |= GLINE_LDEACT;
360     else {
361       gline->gl_flags &= ~GLINE_ACTIVE;
362
363       if (gline->gl_lastmod >= lastmod)
364         gline->gl_lastmod++;
365       else
366         gline->gl_lastmod = lastmod;
367     }
368
369     if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
370       return 0; /* was inactive to begin with */
371   }
372
373   /* Inform ops and log it */
374   sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
375                        "%s",
376                        IsServer(sptr) ? sptr->name : sptr->user->server->name,
377                        msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
378                        gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
379                        GlineIsBadChan(gline) ? "" : gline->gl_host,
380                        gline->gl_expire + TSoffset, gline->gl_reason);
381
382 #ifdef GPATH
383   write_log(GPATH, "# %Tu %C %s %s for %s%s%s, expiring at %Tu: %s\n",
384             TStime(), sptr, msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
385             gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
386             GlineIsBadChan(gline) ? "" : gline->gl_host,
387             gline->gl_expire + TSoffset, gline->gl_reason);
388 #endif /* GPATH */
389
390   if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
391     gline_free(gline);
392   else if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
393     propagate_gline(cptr, sptr, gline);
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, cptr->user->username) != 0)
476       continue;
477          
478     if (GlineIsIpMask(gline)) {
479       Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",cptr->ip.s_addr,gline->ipnum.s_addr,gline->bits));
480       if ((cptr->ip.s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
481         continue;
482     }
483     else {
484       if (match(gline->gl_host, cptr->user->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) ? me.name : "*",
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) ? me.name : "*",
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) ? me.name : "*",
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 }