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         sendto_one(acptr, ":%s %d %s :*** %s.", me.name, ERR_YOUREBANNEDCREEP,
156                    acptr->name, gline->gl_reason);
157
158         /* let the ops know about it */
159         sendto_op_mask(SNO_GLINE, "G-line active for %s",
160                        get_client_name(acptr, FALSE));
161
162         /* and get rid of him */
163         if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
164                                     gline->gl_reason)))
165           retval = tval; /* retain killed status */
166       }
167     }
168   }
169
170   return retval;
171 }
172
173 static void
174 propagate_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
175 {
176   if (GlineIsLocal(gline) || (IsUser(sptr) && !gline->gl_lastmod))
177     return;
178
179   if (gline->gl_lastmod)
180     sendcmdto_serv_butone(cptr, CMD_GLINE, sptr, "* %c%s%s%s %Tu %Tu :%s",
181                           GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
182                           GlineIsBadChan(gline) ? "" : "@",
183                           GlineIsBadChan(gline) ? "" : gline->gl_host,
184                           gline->gl_expire - CurrentTime, gline->gl_lastmod,
185                           gline->gl_reason);
186   else
187     sendcmdto_serv_butone(cptr, CMD_GLINE, sptr, "* %c%s%s%s %Tu :%s",
188                           GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
189                           GlineIsBadChan(gline) ? "" : "@",
190                           GlineIsBadChan(gline) ? "" : gline->gl_host,
191                           gline->gl_expire - CurrentTime, gline->gl_reason);
192 }
193
194 int 
195 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
196           char *reason, time_t expire, time_t lastmod, unsigned int flags)
197 {
198   struct Gline *agline;
199
200   assert(0 != userhost);
201   assert(0 != reason);
202
203   /*
204    * You cannot set a negative (or zero) expire time, nor can you set an
205    * expiration time for greater than GLINE_MAX_EXPIRE.
206    */
207   if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
208     if (!IsServer(sptr) && MyConnect(sptr))
209       send_error_to_client(sptr, ERR_BADEXPIRE, expire);
210     return 0;
211   }
212
213   expire += CurrentTime; /* convert from lifetime to timestamp */
214
215   /* NO_OLD_GLINE allows *@#channel to work correctly */
216 #ifdef BADCHAN
217   if (*userhost == '#' || *userhost == '&' || *userhost == '+'
218 # ifndef NO_OLD_GLINE
219       || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
220 # endif /* OLD_GLINE */
221       ) {
222 # ifndef LOCAL_BADCHAN
223     if (flags & GLINE_LOCAL)
224       return 0;
225 # endif
226     flags |= GLINE_BADCHAN;
227   }
228 #endif /* BADCHAN */
229
230   /* Inform ops... */
231   sendto_op_mask(SNO_GLINE, "%s adding %s %s for %s, expiring at "
232                  TIME_T_FMT ": %s",
233                  IsServer(sptr) ? sptr->name : sptr->user->server->name,
234                  flags & GLINE_LOCAL ? "local" : "global",
235                  flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
236                  expire + TSoffset, reason);
237
238 #ifdef GPATH
239   /* and log it */
240   if (IsServer(sptr))
241     write_log(GPATH, "# " TIME_T_FMT " %s adding %s %s for %s, expiring at "
242               TIME_T_FMT ": %s\n", TStime(), sptr->name,
243               flags & GLINE_LOCAL ? "local" : "global",
244               flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
245               expire + TSoffset, reason);
246   else
247     write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s adding %s %s for %s, "
248               "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
249               sptr->user->username, sptr->user->host,
250               flags & GLINE_LOCAL ? "local" : "global",
251               flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
252               expire + TSoffset, reason);
253 #endif /* GPATH */
254
255   /* make the gline */
256   agline = make_gline(userhost, reason, expire, lastmod, flags);
257
258   if (!agline) /* if it overlapped, silently return */
259     return 0;
260
261   propagate_gline(cptr, sptr, agline);
262
263   if (GlineIsBadChan(agline))
264     return 0;
265
266 #ifdef GPATH
267   /* this can be inserted into the conf */
268   write_log(GPATH, "%c:%s:%s:%s\n", GlineIsIpMask(agline) ? 'k' : 'K',
269             GlineHost(agline), GlineReason(agline), GlineUser(agline));
270 #endif /* GPATH */
271
272   return do_gline(cptr, sptr, agline); /* knock off users if necessary */
273 }
274
275 int
276 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
277                time_t lastmod, unsigned int flags)
278 {
279   unsigned int saveflags = 0;
280
281   assert(0 != gline);
282   assert(!GlineIsLocal(gline));
283
284   if (flags & GLINE_LOCAL)
285     sendto_ops("gline_activate called with GLINE_LOCAL");
286
287   saveflags = gline->gl_flags;
288
289   if (flags & GLINE_LOCAL)
290     gline->gl_flags &= ~GLINE_LDEACT;
291   else {
292     gline->gl_flags |= GLINE_ACTIVE;
293
294     if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
295       gline->gl_lastmod++;
296     else
297       gline->gl_lastmod = lastmod;
298   }
299
300   if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
301     return 0; /* was active to begin with */
302
303   /* Inform ops and log it */
304   sendto_op_mask(SNO_GLINE, "%s activating global %s for %s%s%s, expiring at "
305                  TIME_T_FMT ": %s",
306                  IsServer(sptr) ? sptr->name : sptr->user->server->name,
307                  GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
308                  gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
309                  GlineIsBadChan(gline) ? "" : gline->gl_host,
310                  gline->gl_expire + TSoffset, gline->gl_reason);
311
312 #ifdef GPATH
313   if (IsServer(sptr))
314     write_log(GPATH, "# " TIME_T_FMT " %s activating global %s for %s%s%s, "
315               "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
316               GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
317               gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
318               GlineIsBadChan(gline) ? "" : gline->gl_host,
319               gline->gl_expire + TSoffset, gline->gl_reason);
320   else
321     write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s activating %s for "
322               "%s%s%s, expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
323               sptr->user->username, sptr->user->host,
324               GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
325               gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
326               GlineIsBadChan(gline) ? "" : gline->gl_host,
327               gline->gl_expire + TSoffset, gline->gl_reason);
328 #endif /* GPATH */
329
330   propagate_gline(cptr, sptr, gline);
331
332   return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
333 }
334
335 int
336 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
337                  time_t lastmod, unsigned int flags)
338 {
339   unsigned int saveflags = 0;
340
341   assert(0 != gline);
342
343   if (flags & GLINE_LOCAL)
344     sendto_ops("gline_deactivate called with GLINE_LOCAL");
345
346   saveflags = gline->gl_flags;
347
348   if (!GlineIsLocal(gline)) {
349     if (flags & GLINE_LOCAL)
350       gline->gl_flags |= GLINE_LDEACT;
351     else {
352       gline->gl_flags &= ~GLINE_ACTIVE;
353
354       if (gline->gl_lastmod >= lastmod)
355         gline->gl_lastmod++;
356       else
357         gline->gl_lastmod = lastmod;
358     }
359
360     if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
361       return 0; /* was inactive to begin with */
362   }
363
364   /* Inform ops and log it */
365   sendto_op_mask(SNO_GLINE, "%s %s %s for %s%s%s, expiring at "
366                  TIME_T_FMT ": %s",
367                  IsServer(sptr) ? sptr->name : sptr->user->server->name,
368                  GlineIsLocal(gline) ? "removing local" :
369                  "deactivating global",
370                  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 #ifdef GPATH
376   if (IsServer(sptr))
377     write_log(GPATH, "# " TIME_T_FMT " %s %s %s for %s%s%s, "
378               "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
379               GlineIsLocal(gline) ? "removing local" : "deactivating global",
380               GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
381               gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
382               GlineIsBadChan(gline) ? "" : gline->gl_host,
383               gline->gl_expire + TSoffset, gline->gl_reason);
384   else
385     write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s %s %s for "
386               "%s%s%s, expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
387               sptr->user->username, sptr->user->host,
388               GlineIsLocal(gline) ? "removing local" : "deactivating global",
389               GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
390               gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
391               GlineIsBadChan(gline) ? "" : gline->gl_host,
392               gline->gl_expire + TSoffset, gline->gl_reason);
393 #endif /* GPATH */
394
395   if (GlineIsLocal(gline))
396     gline_free(gline);
397   else
398     propagate_gline(cptr, sptr, gline);
399
400   return 0;
401 }
402
403 struct Gline *
404 gline_find(char *userhost, unsigned int flags)
405 {
406   struct Gline *gline;
407   struct Gline *sgline;
408   char *user, *host, *t_uh;
409
410   if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
411     for (gline = BadChanGlineList; gline; gline = sgline) {
412       sgline = gline->gl_next;
413
414       if (gline->gl_expire <= CurrentTime)
415         gline_free(gline);
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_EXACT) {
439       if (ircd_strcmp(gline->gl_host, host) == 0 &&
440           ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
441            ircd_strcmp(gline->gl_user, user) == 0))
442         break;
443     } else {
444       if (match(gline->gl_host, host) == 0 &&
445           ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
446            match(gline->gl_user, user) == 0))
447       break;
448     }
449   }
450
451   MyFree(t_uh);
452
453   return gline;
454 }
455
456 struct Gline *
457 gline_lookup(struct Client *cptr)
458 {
459   struct Gline *gline;
460   struct Gline *sgline;
461
462   for (gline = GlobalGlineList; gline; gline = sgline) {
463     sgline = gline->gl_next;
464
465     if (gline->gl_expire <= CurrentTime)
466       gline_free(gline);
467     else if ((GlineIsIpMask(gline) ?
468               match(gline->gl_host, ircd_ntoa((const char *)&cptr->ip)) :
469               match(gline->gl_host, cptr->user->host)) == 0 &&
470              match(gline->gl_user, cptr->user->username) == 0)
471       return gline;
472   }
473
474   return 0;
475 }
476
477 void
478 gline_free(struct Gline *gline)
479 {
480   assert(0 != gline);
481
482   *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
483   if (gline->gl_next)
484     gline->gl_next->gl_prev_p = gline->gl_prev_p;
485
486   MyFree(gline->gl_user); /* free up the memory */
487   if (gline->gl_host)
488     MyFree(gline->gl_host);
489   MyFree(gline->gl_reason);
490   MyFree(gline);
491 }
492
493 void
494 gline_burst(struct Client *cptr)
495 {
496   struct Gline *gline;
497   struct Gline *sgline;
498
499   for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
500     sgline = gline->gl_next;
501
502     if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
503       gline_free(gline);
504     else if (!GlineIsLocal(gline) && gline->gl_lastmod)
505       sendcmdto_one(cptr, CMD_GLINE, &me, "* %c%s@%s %Tu %Tu :%s",
506                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
507                     gline->gl_host, gline->gl_expire - CurrentTime,
508                     gline->gl_lastmod, gline->gl_reason);
509   }
510
511   for (gline = BadChanGlineList; 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(cptr, CMD_GLINE, &me, "* %c%s %Tu %Tu :%s",
518                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
519                     gline->gl_expire - CurrentTime, gline->gl_lastmod,
520                     gline->gl_reason);
521   }
522 }
523
524 int
525 gline_resend(struct Client *cptr, struct Gline *gline)
526 {
527   if (GlineIsLocal(gline) || !gline->gl_lastmod)
528     return 0;
529
530   sendcmdto_one(cptr, CMD_GLINE, &me, "* %c%s%s%s %Tu %Tu :%s",
531                 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
532                 GlineIsBadChan(gline) ? "" : "@",
533                 GlineIsBadChan(gline) ? "" : gline->gl_host,
534                 gline->gl_expire - CurrentTime, gline->gl_lastmod,
535                 gline->gl_reason);
536
537   return 0;
538 }
539
540 int
541 gline_list(struct Client *sptr, char *userhost)
542 {
543   struct Gline *gline;
544   struct Gline *sgline;
545
546   if (userhost) {
547     if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
548       return send_error_to_client(sptr, ERR_NOSUCHGLINE, userhost);
549
550     /* send gline information along */
551     sendto_one(sptr, rpl_str(RPL_GLIST), me.name, sptr->name, gline->gl_user,
552                GlineIsBadChan(gline) ? "" : "@",
553                GlineIsBadChan(gline) ? "" : gline->gl_host,
554                gline->gl_expire + TSoffset,
555                GlineIsLocal(gline) ? me.name : "*",
556                GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
557   } else {
558     for (gline = GlobalGlineList; gline; gline = sgline) {
559       sgline = gline->gl_next;
560
561       if (gline->gl_expire <= CurrentTime)
562         gline_free(gline);
563       else
564         sendto_one(sptr, rpl_str(RPL_GLIST), me.name, sptr->name,
565                    gline->gl_user, "@", gline->gl_host,
566                    gline->gl_expire + TSoffset,
567                    GlineIsLocal(gline) ? me.name : "*",
568                    GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
569     }
570
571     for (gline = BadChanGlineList; gline; gline = sgline) {
572       sgline = gline->gl_next;
573
574       if (gline->gl_expire <= CurrentTime)
575         gline_free(gline);
576       else
577         sendto_one(sptr, rpl_str(RPL_GLIST), me.name, sptr->name,
578                    gline->gl_user, "", "", gline->gl_expire + TSoffset,
579                    GlineIsLocal(gline) ? me.name : "*",
580                    GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
581     }
582   }
583
584   /* end of gline information */
585   sendto_one(sptr, rpl_str(RPL_ENDOFGLIST), me.name, sptr->name);
586   return 0;
587 }
588
589 void
590 gline_stats(struct Client *sptr)
591 {
592   struct Gline *gline;
593   struct Gline *sgline;
594
595   for (gline = GlobalGlineList; gline; gline = sgline) {
596     sgline = gline->gl_next;
597
598     if (gline->gl_expire <= CurrentTime)
599       gline_free(gline);
600     else
601       sendto_one(sptr, rpl_str(RPL_STATSGLINE), me.name, sptr->name, 'G',
602                  gline->gl_user, gline->gl_host, gline->gl_expire + TSoffset,
603                  gline->gl_reason);
604   }
605 }