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_reply.h"
27 #include "ircd_string.h"
28 #include "match.h"
29 #include "numeric.h"
30 #include "s_bsd.h"
31 #include "s_misc.h"
32 #include "send.h"
33 #include "struct.h"
34 #include "support.h"
35 #include "msg.h"
36 #include "numnicks.h"
37 #include "numeric.h"
38 #include "sys.h"    /* FALSE bleah */
39
40 #include <assert.h>
41 #include <string.h>
42
43 struct Gline* GlobalGlineList  = 0;
44 struct Gline* BadChanGlineList = 0;
45
46 static void
47 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
48 {
49   char *tmp;
50
51   if (!(tmp = strchr(userhost, '@'))) {
52     *user_p = def_user;
53     *host_p = userhost;
54   } else {
55     *user_p = userhost;
56     *(tmp++) = '\0';
57     *host_p = tmp;
58   }
59 }
60
61 static struct Gline *
62 make_gline(char *userhost, char *reason, time_t expire, time_t lastmod,
63            unsigned int flags)
64 {
65   struct Gline *gline, *sgline, *after = 0;
66   char *user, *host;
67
68   if (!(flags & GLINE_BADCHAN)) { /* search for overlapping glines first */
69     canon_userhost(userhost, &user, &host, "*"); /* find user and host */
70
71     for (gline = GlobalGlineList; gline; gline = sgline) {
72       sgline = gline->gl_next;
73
74       if (gline->gl_expire <= CurrentTime)
75         gline_free(gline);
76       else if ((gline->gl_flags & GLINE_LOCAL) != (flags & GLINE_LOCAL))
77         continue;
78       else if (!mmatch(gline->gl_user, user) && /* gline contains new mask */
79                !mmatch(gline->gl_host, host)) {
80         if (expire <= gline->gl_expire) /* will expire before wider gline */
81           return 0;
82         else
83           after = gline; /* stick new gline after this one */
84       } else if (!mmatch(user, gline->gl_user) && /* new mask contains gline */
85                  !mmatch(host, gline->gl_host) &&
86                  gline->gl_expire <= expire) /* gline expires before new one */
87         gline_free(gline); /* save some memory */
88     }
89   }
90
91   gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
92   assert(0 != gline);
93
94   DupString(gline->gl_reason, reason); /* initialize gline... */
95   gline->gl_expire = expire;
96   gline->gl_lastmod = lastmod;
97   gline->gl_flags = flags & GLINE_MASK;
98
99   if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
100     DupString(gline->gl_user, userhost); /* first, remember channel */
101     gline->gl_host = 0;
102
103     gline->gl_next = BadChanGlineList; /* then link it into list */
104     gline->gl_prev_p = &BadChanGlineList;
105     if (BadChanGlineList)
106       BadChanGlineList->gl_prev_p = &gline->gl_next;
107     BadChanGlineList = gline;
108   } else {
109     DupString(gline->gl_user, user); /* remember them... */
110     DupString(gline->gl_host, host);
111
112     if (check_if_ipmask(host)) /* mark if it's an IP mask */
113       gline->gl_flags |= GLINE_IPMASK;
114
115     if (after) {
116       gline->gl_next = after->gl_next;
117       gline->gl_prev_p = &after->gl_next;
118       if (after->gl_next)
119         after->gl_next->gl_prev_p = &gline->gl_next;
120       after->gl_next = gline;
121     } else {
122       gline->gl_next = GlobalGlineList; /* then link it into list */
123       gline->gl_prev_p = &GlobalGlineList;
124       if (GlobalGlineList)
125         GlobalGlineList->gl_prev_p = &gline->gl_next;
126       GlobalGlineList = gline;
127     }
128   }
129
130   return gline;
131 }
132
133 static int
134 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
135 {
136   struct Client *acptr;
137   int fd, retval = 0, tval;
138
139   if (!GlineIsActive(gline)) /* no action taken on inactive glines */
140     return 0;
141
142   for (fd = HighestFd; fd >= 0; --fd) {
143     /*
144      * get the users!
145      */
146     if ((acptr = LocalClientArray[fd])) {
147       if (!acptr->user)
148         continue;
149
150       if ((GlineIsIpMask(gline) ? match(gline->gl_host, acptr->sock_ip) :
151            match(gline->gl_host, acptr->sockhost)) == 0 &&
152           (!acptr->user->username ||
153            match(gline->gl_user, acptr->user->username) == 0)) {
154         /* ok, here's one that got G-lined */
155         /* XXX sendto_one used for K-line reason */
156         sendto_one(acptr, ":%s %d %s :*** %s.", me.name, ERR_YOUREBANNEDCREEP,
157                    acptr->name, gline->gl_reason);
158
159         /* let the ops know about it */
160         sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
161                              get_client_name(acptr, FALSE));
162
163         /* and get rid of him */
164         if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
165                                     gline->gl_reason)))
166           retval = tval; /* retain killed status */
167       }
168     }
169   }
170
171   return retval;
172 }
173
174 static void
175 propagate_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
176 {
177   if (GlineIsLocal(gline) || (IsUser(sptr) && !gline->gl_lastmod))
178     return;
179
180   if (gline->gl_lastmod)
181     sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
182                           GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
183                           GlineIsBadChan(gline) ? "" : "@",
184                           GlineIsBadChan(gline) ? "" : gline->gl_host,
185                           gline->gl_expire - CurrentTime, gline->gl_lastmod,
186                           gline->gl_reason);
187   else
188     sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu :%s",
189                           GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
190                           GlineIsBadChan(gline) ? "" : "@",
191                           GlineIsBadChan(gline) ? "" : gline->gl_host,
192                           gline->gl_expire - CurrentTime, gline->gl_reason);
193 }
194
195 int 
196 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
197           char *reason, time_t expire, time_t lastmod, unsigned int flags)
198 {
199   struct Gline *agline;
200
201   assert(0 != userhost);
202   assert(0 != reason);
203
204   /*
205    * You cannot set a negative (or zero) expire time, nor can you set an
206    * expiration time for greater than GLINE_MAX_EXPIRE.
207    */
208   if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
209     if (!IsServer(sptr) && MyConnect(sptr))
210       send_reply(sptr, ERR_BADEXPIRE, expire);
211     return 0;
212   }
213
214   expire += CurrentTime; /* convert from lifetime to timestamp */
215
216   /* NO_OLD_GLINE allows *@#channel to work correctly */
217 #ifdef BADCHAN
218   if (*userhost == '#' || *userhost == '&' || *userhost == '+'
219 # ifndef NO_OLD_GLINE
220       || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
221 # endif /* OLD_GLINE */
222       ) {
223 # ifndef LOCAL_BADCHAN
224     if (flags & GLINE_LOCAL)
225       return 0;
226 # endif
227     flags |= GLINE_BADCHAN;
228   }
229 #endif /* BADCHAN */
230
231   /* Inform ops... */
232   sendto_opmask_butone(0, SNO_GLINE, "%s adding %s %s for %s, expiring at "
233                        "%Tu: %s",
234                        IsServer(sptr) ? sptr->name : sptr->user->server->name,
235                        flags & GLINE_LOCAL ? "local" : "global",
236                        flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
237                        expire + TSoffset, reason);
238
239 #ifdef GPATH
240   /* and log it */
241   if (IsServer(sptr))
242     write_log(GPATH, "# " TIME_T_FMT " %s adding %s %s for %s, expiring at "
243               TIME_T_FMT ": %s\n", TStime(), sptr->name,
244               flags & GLINE_LOCAL ? "local" : "global",
245               flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
246               expire + TSoffset, reason);
247   else
248     write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s adding %s %s for %s, "
249               "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
250               sptr->user->username, sptr->user->host,
251               flags & GLINE_LOCAL ? "local" : "global",
252               flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
253               expire + TSoffset, reason);
254 #endif /* GPATH */
255
256   /* make the gline */
257   agline = make_gline(userhost, reason, expire, lastmod, flags);
258
259   if (!agline) /* if it overlapped, silently return */
260     return 0;
261
262   propagate_gline(cptr, sptr, agline);
263
264   if (GlineIsBadChan(agline))
265     return 0;
266
267 #ifdef GPATH
268   /* this can be inserted into the conf */
269   write_log(GPATH, "%c:%s:%s:%s\n", GlineIsIpMask(agline) ? 'k' : 'K',
270             GlineHost(agline), GlineReason(agline), GlineUser(agline));
271 #endif /* GPATH */
272
273   return do_gline(cptr, sptr, agline); /* knock off users if necessary */
274 }
275
276 int
277 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
278                time_t lastmod, unsigned int flags)
279 {
280   unsigned int saveflags = 0;
281
282   assert(0 != gline);
283   assert(!GlineIsLocal(gline));
284
285   saveflags = gline->gl_flags;
286
287   if (flags & GLINE_LOCAL)
288     gline->gl_flags &= ~GLINE_LDEACT;
289   else {
290     gline->gl_flags |= GLINE_ACTIVE;
291
292     if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
293       gline->gl_lastmod++;
294     else
295       gline->gl_lastmod = lastmod;
296   }
297
298   if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
299     return 0; /* was active to begin with */
300
301   /* Inform ops and log it */
302   sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
303                        "expiring at %Tu: %s",
304                        IsServer(sptr) ? sptr->name : sptr->user->server->name,
305                        GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
306                        gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
307                        GlineIsBadChan(gline) ? "" : gline->gl_host,
308                        gline->gl_expire + TSoffset, gline->gl_reason);
309
310 #ifdef GPATH
311   if (IsServer(sptr))
312     write_log(GPATH, "# " TIME_T_FMT " %s activating global %s for %s%s%s, "
313               "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
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   else
319     write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s activating %s for "
320               "%s%s%s, expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
321               sptr->user->username, sptr->user->host,
322               GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
323               gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
324               GlineIsBadChan(gline) ? "" : gline->gl_host,
325               gline->gl_expire + TSoffset, gline->gl_reason);
326 #endif /* GPATH */
327
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
339   assert(0 != gline);
340
341   saveflags = gline->gl_flags;
342
343   if (!GlineIsLocal(gline)) {
344     if (flags & GLINE_LOCAL)
345       gline->gl_flags |= GLINE_LDEACT;
346     else {
347       gline->gl_flags &= ~GLINE_ACTIVE;
348
349       if (gline->gl_lastmod >= lastmod)
350         gline->gl_lastmod++;
351       else
352         gline->gl_lastmod = lastmod;
353     }
354
355     if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
356       return 0; /* was inactive to begin with */
357   }
358
359   /* Inform ops and log it */
360   sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
361                        "%s",
362                        IsServer(sptr) ? sptr->name : sptr->user->server->name,
363                        GlineIsLocal(gline) ? "removing local" :
364                        "deactivating global",
365                        GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
366                        gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
367                        GlineIsBadChan(gline) ? "" : gline->gl_host,
368                        gline->gl_expire + TSoffset, gline->gl_reason);
369
370 #ifdef GPATH
371   if (IsServer(sptr))
372     write_log(GPATH, "# " TIME_T_FMT " %s %s %s for %s%s%s, "
373               "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
374               GlineIsLocal(gline) ? "removing local" : "deactivating global",
375               GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
376               gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
377               GlineIsBadChan(gline) ? "" : gline->gl_host,
378               gline->gl_expire + TSoffset, gline->gl_reason);
379   else
380     write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s %s %s for "
381               "%s%s%s, expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
382               sptr->user->username, sptr->user->host,
383               GlineIsLocal(gline) ? "removing local" : "deactivating global",
384               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))
391     gline_free(gline);
392   else
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_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
412                 match(gline->gl_user, userhost)) == 0)
413         return gline;
414     }
415   }
416
417   if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
418       *userhost == '#' || *userhost == '&' || *userhost == '+'
419 #ifndef NO_OLD_GLINE
420       || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
421 #endif /* NO_OLD_GLINE */
422       )
423     return 0;
424
425   DupString(t_uh, userhost);
426   canon_userhost(t_uh, &user, &host, 0);
427
428   for (gline = GlobalGlineList; gline; gline = sgline) {
429     sgline = gline->gl_next;
430
431     if (gline->gl_expire <= CurrentTime)
432       gline_free(gline);
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)
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     else if ((GlineIsIpMask(gline) ?
463               match(gline->gl_host, ircd_ntoa((const char *)&cptr->ip)) :
464               match(gline->gl_host, cptr->user->host)) == 0 &&
465              match(gline->gl_user, cptr->user->username) == 0)
466       return gline;
467   }
468
469   return 0;
470 }
471
472 void
473 gline_free(struct Gline *gline)
474 {
475   assert(0 != gline);
476
477   *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
478   if (gline->gl_next)
479     gline->gl_next->gl_prev_p = gline->gl_prev_p;
480
481   MyFree(gline->gl_user); /* free up the memory */
482   if (gline->gl_host)
483     MyFree(gline->gl_host);
484   MyFree(gline->gl_reason);
485   MyFree(gline);
486 }
487
488 void
489 gline_burst(struct Client *cptr)
490 {
491   struct Gline *gline;
492   struct Gline *sgline;
493
494   for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
495     sgline = gline->gl_next;
496
497     if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
498       gline_free(gline);
499     else if (!GlineIsLocal(gline) && gline->gl_lastmod)
500       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s@%s %Tu %Tu :%s",
501                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
502                     gline->gl_host, gline->gl_expire - CurrentTime,
503                     gline->gl_lastmod, gline->gl_reason);
504   }
505
506   for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
507     sgline = gline->gl_next;
508
509     if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
510       gline_free(gline);
511     else if (!GlineIsLocal(gline) && gline->gl_lastmod)
512       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
513                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
514                     gline->gl_expire - CurrentTime, gline->gl_lastmod,
515                     gline->gl_reason);
516   }
517 }
518
519 int
520 gline_resend(struct Client *cptr, struct Gline *gline)
521 {
522   if (GlineIsLocal(gline) || !gline->gl_lastmod)
523     return 0;
524
525   sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
526                 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
527                 GlineIsBadChan(gline) ? "" : "@",
528                 GlineIsBadChan(gline) ? "" : gline->gl_host,
529                 gline->gl_expire - CurrentTime, gline->gl_lastmod,
530                 gline->gl_reason);
531
532   return 0;
533 }
534
535 int
536 gline_list(struct Client *sptr, char *userhost)
537 {
538   struct Gline *gline;
539   struct Gline *sgline;
540
541   if (userhost) {
542     if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
543       return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
544
545     /* send gline information along */
546     send_reply(sptr, RPL_GLIST, gline->gl_user,
547                GlineIsBadChan(gline) ? "" : "@",
548                GlineIsBadChan(gline) ? "" : gline->gl_host,
549                gline->gl_expire + TSoffset,
550                GlineIsLocal(gline) ? me.name : "*",
551                GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
552   } else {
553     for (gline = GlobalGlineList; gline; gline = sgline) {
554       sgline = gline->gl_next;
555
556       if (gline->gl_expire <= CurrentTime)
557         gline_free(gline);
558       else
559         send_reply(sptr, RPL_GLIST, gline->gl_user, "@", gline->gl_host,
560                    gline->gl_expire + TSoffset,
561                    GlineIsLocal(gline) ? me.name : "*",
562                    GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
563     }
564
565     for (gline = BadChanGlineList; gline; gline = sgline) {
566       sgline = gline->gl_next;
567
568       if (gline->gl_expire <= CurrentTime)
569         gline_free(gline);
570       else
571         send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
572                    gline->gl_expire + TSoffset,
573                    GlineIsLocal(gline) ? me.name : "*",
574                    GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
575     }
576   }
577
578   /* end of gline information */
579   return send_reply(sptr, RPL_ENDOFGLIST);
580 }
581
582 void
583 gline_stats(struct Client *sptr)
584 {
585   struct Gline *gline;
586   struct Gline *sgline;
587
588   for (gline = GlobalGlineList; 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_STATSGLINE, 'G', gline->gl_user, gline->gl_host,
595                  gline->gl_expire + TSoffset, gline->gl_reason);
596   }
597 }