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_features.h"
29 #include "ircd_log.h"
30 #include "ircd_policy.h"
31 #include "ircd_reply.h"
32 #include "ircd_snprintf.h"
33 #include "ircd_string.h"
34 #include "match.h"
35 #include "numeric.h"
36 #include "s_bsd.h"
37 #include "s_debug.h"
38 #include "s_misc.h"
39 #include "send.h"
40 #include "struct.h"
41 #include "support.h"
42 #include "msg.h"
43 #include "numnicks.h"
44 #include "numeric.h"
45 #include "sys.h"    /* FALSE bleah */
46 #include "whocmds.h"
47
48 #include <assert.h>
49 #include <string.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <arpa/inet.h> /* for inet_ntoa */
53
54 #define CHECK_APPROVED     0    /* Mask is acceptable */
55 #define CHECK_OVERRIDABLE  1    /* Mask is acceptable, but not by default */
56 #define CHECK_REJECTED     2    /* Mask is totally unacceptable */
57
58 #define MASK_WILD_0     0x01    /* Wildcards in the last position */
59 #define MASK_WILD_1     0x02    /* Wildcards in the next-to-last position */
60
61 #define MASK_WILD_MASK  0x03    /* Mask out the positional wildcards */
62
63 #define MASK_WILDS      0x10    /* Mask contains wildcards */
64 #define MASK_IP         0x20    /* Mask is an IP address */
65 #define MASK_HALT       0x40    /* Finished processing mask */
66
67 struct Gline* GlobalGlineList  = 0;
68 struct Gline* BadChanGlineList = 0;
69
70 static void
71 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
72 {
73   char *tmp;
74
75   if (!(tmp = strchr(userhost, '@'))) {
76     *user_p = def_user;
77     *host_p = userhost;
78   } else {
79     *user_p = userhost;
80     *(tmp++) = '\0';
81     *host_p = tmp;
82   }
83 }
84
85 static struct Gline *
86 make_gline(char *user, char *host, char *reason, time_t expire, time_t lastmod,
87            unsigned int flags)
88 {
89   struct Gline *gline, *sgline, *after = 0;
90
91   if (!(flags & GLINE_BADCHAN)) { /* search for overlapping glines first */
92
93     for (gline = GlobalGlineList; gline; gline = sgline) {
94       sgline = gline->gl_next;
95
96       if (gline->gl_expire <= CurrentTime)
97         gline_free(gline);
98       else if ((gline->gl_flags & GLINE_LOCAL) != (flags & GLINE_LOCAL))
99         continue;
100       else if (!mmatch(gline->gl_user, user) && /* gline contains new mask */
101                !mmatch(gline->gl_host, host)) {
102         if (expire <= gline->gl_expire) /* will expire before wider gline */
103           return 0;
104         else
105           after = gline; /* stick new gline after this one */
106       } else if (!mmatch(user, gline->gl_user) && /* new mask contains gline */
107                  !mmatch(host, gline->gl_host) &&
108                  gline->gl_expire <= expire) /* gline expires before new one */
109         gline_free(gline); /* save some memory */
110     }
111   }
112
113   gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
114   assert(0 != gline);
115
116   DupString(gline->gl_reason, reason); /* initialize gline... */
117   gline->gl_expire = expire;
118   gline->gl_lastmod = lastmod;
119   gline->gl_flags = flags & GLINE_MASK;
120
121   if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
122     DupString(gline->gl_user, user); /* first, remember channel */
123     gline->gl_host = 0;
124
125     gline->gl_next = BadChanGlineList; /* then link it into list */
126     gline->gl_prev_p = &BadChanGlineList;
127     if (BadChanGlineList)
128       BadChanGlineList->gl_prev_p = &gline->gl_next;
129     BadChanGlineList = gline;
130   } else {
131     DupString(gline->gl_user, user); /* remember them... */
132     DupString(gline->gl_host, host);
133
134     if (check_if_ipmask(host)) { /* mark if it's an IP mask */
135       int class;
136       char ipname[16];
137       int ad[4] = { 0 };
138       int bits2 = 0;
139        
140       class = sscanf(host,"%d.%d.%d.%d/%d",
141                      &ad[0],&ad[1],&ad[2],&ad[3], &bits2);
142       if (class!=5) {
143         gline->bits=class*8;
144       }
145       else {
146         gline->bits=bits2;
147       }
148       sprintf_irc(ipname,"%d.%d.%d.%d",ad[0],ad[1],ad[2],ad[3]);
149       gline->ipnum.s_addr = inet_addr(ipname);
150       Debug((DEBUG_DEBUG,"IP gline: %08x/%i",gline->ipnum.s_addr,gline->bits));
151       gline->gl_flags |= GLINE_IPMASK;
152     }
153
154     if (after) {
155       gline->gl_next = after->gl_next;
156       gline->gl_prev_p = &after->gl_next;
157       if (after->gl_next)
158         after->gl_next->gl_prev_p = &gline->gl_next;
159       after->gl_next = gline;
160     } else {
161       gline->gl_next = GlobalGlineList; /* then link it into list */
162       gline->gl_prev_p = &GlobalGlineList;
163       if (GlobalGlineList)
164         GlobalGlineList->gl_prev_p = &gline->gl_next;
165       GlobalGlineList = gline;
166     }
167   }
168
169   return gline;
170 }
171
172 static int
173 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
174 {
175   struct Client *acptr;
176   int fd, retval = 0, tval;
177
178   if (!GlineIsActive(gline)) /* no action taken on inactive glines */
179     return 0;
180
181   for (fd = HighestFd; fd >= 0; --fd) {
182     /*
183      * get the users!
184      */
185     if ((acptr = LocalClientArray[fd])) {
186       if (!cli_user(acptr))
187         continue;
188         
189       if (cli_user(acptr)->username && 
190           match (gline->gl_user, (cli_user(acptr))->username) != 0)
191                continue;
192           
193       if (GlineIsIpMask(gline)) {
194         Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",(cli_ip(cptr)).s_addr,gline->ipnum.s_addr,gline->bits));
195         if (((cli_ip(acptr)).s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
196           continue;
197       }
198       else {
199         if (match(gline->gl_host, cli_sockhost(acptr)) != 0)
200           continue;
201       }
202
203       /* ok, here's one that got G-lined */
204       send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
205            gline->gl_reason);
206
207       /* let the ops know about it */
208       sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
209                      get_client_name(acptr, TRUE));
210
211       /* and get rid of him */
212       if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
213           gline->gl_reason)))
214         retval = tval; /* retain killed status */
215     }
216   }
217   return retval;
218 }
219
220 /*
221  * This routine implements the mask checking applied to local
222  * G-lines.  Basically, host masks must have a minimum of two non-wild
223  * domain fields, and IP masks must have a minimum of 16 bits.  If the
224  * mask has even one wild-card, OVERRIDABLE is returned, assuming the
225  * other check doesn't fail.
226  */
227 static int
228 gline_checkmask(char *mask)
229 {
230   unsigned int flags = MASK_IP;
231   unsigned int dots = 0;
232   unsigned int ipmask = 0;
233
234   for (; *mask; mask++) { /* go through given mask */
235     if (*mask == '.') { /* it's a separator; advance positional wilds */
236       flags = (flags & ~MASK_WILD_MASK) | ((flags << 1) & MASK_WILD_MASK);
237       dots++;
238
239       if ((flags & (MASK_IP | MASK_WILDS)) == MASK_IP)
240         ipmask += 8; /* It's an IP with no wilds, count bits */
241     } else if (*mask == '*' || *mask == '?')
242       flags |= MASK_WILD_0 | MASK_WILDS; /* found a wildcard */
243     else if (*mask == '/') { /* n.n.n.n/n notation; parse bit specifier */
244       ipmask = strtoul(++mask, &mask, 10);
245
246       if (*mask || dots != 3 || ipmask > 32 || /* sanity-check to date */
247           (flags & (MASK_WILDS | MASK_IP)) != MASK_IP)
248         return CHECK_REJECTED; /* how strange... */
249
250       if (ipmask < 32) /* it's a masked address; mark wilds */
251         flags |= MASK_WILDS;
252
253       flags |= MASK_HALT; /* Halt the ipmask calculation */
254
255       break; /* get out of the loop */
256     } else if (!IsDigit(*mask)) {
257       flags &= ~MASK_IP; /* not an IP anymore! */
258       ipmask = 0;
259     }
260   }
261
262   /* Sanity-check quads */
263   if (dots > 3 || (!(flags & MASK_WILDS) && dots < 3)) {
264     flags &= ~MASK_IP;
265     ipmask = 0;
266   }
267
268   /* update bit count if necessary */
269   if ((flags & (MASK_IP | MASK_WILDS | MASK_HALT)) == MASK_IP)
270     ipmask += 8;
271
272   /* Check to see that it's not too wide of a mask */
273   if (flags & MASK_WILDS &&
274       ((!(flags & MASK_IP) && (dots < 2 || flags & MASK_WILD_MASK)) ||
275        (flags & MASK_IP && ipmask < 16)))
276     return CHECK_REJECTED; /* to wide, reject */
277
278   /* Ok, it's approved; require override if it has wildcards, though */
279   return flags & MASK_WILDS ? CHECK_OVERRIDABLE : CHECK_APPROVED;
280 }
281
282 int
283 gline_propagate(struct Client *cptr, struct Client *sptr, struct Gline *gline)
284 {
285   if (GlineIsLocal(gline) || (IsUser(sptr) && !gline->gl_lastmod))
286     return 0;
287
288   if (gline->gl_lastmod)
289     sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
290                           GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
291                           GlineIsBadChan(gline) ? "" : "@",
292                           GlineIsBadChan(gline) ? "" : gline->gl_host,
293                           gline->gl_expire - CurrentTime, gline->gl_lastmod,
294                           gline->gl_reason);
295   else
296     sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
297                           (GlineIsRemActive(gline) ?
298                            "* +%s%s%s %Tu :%s" : "* -%s%s%s"),
299                           gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
300                           GlineIsBadChan(gline) ? "" : gline->gl_host,
301                           gline->gl_expire - CurrentTime, gline->gl_reason);
302
303   return 0;
304 }
305
306 int 
307 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
308           char *reason, time_t expire, time_t lastmod, unsigned int flags)
309 {
310   struct Gline *agline;
311   char uhmask[USERLEN + HOSTLEN + 2];
312   char *user, *host;
313   int tmp;
314
315   assert(0 != userhost);
316   assert(0 != reason);
317
318   /* NO_OLD_GLINE allows *@#channel to work correctly */
319   if (*userhost == '#' || *userhost == '&' || *userhost == '+'
320 # ifndef NO_OLD_GLINE
321       || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
322 # endif /* OLD_GLINE */
323       ) {
324     if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
325       return send_reply(sptr, ERR_NOPRIVILEGES);
326
327     flags |= GLINE_BADCHAN;
328 # ifndef NO_OLD_GLINE
329     if (userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+')
330       user = userhost + 2;
331     else
332 # endif /* OLD_GLINE */
333       user = userhost;
334     host = 0;
335   } else {
336     canon_userhost(userhost, &user, &host, "*");
337     if (sizeof(uhmask) <
338         ircd_snprintf(0, uhmask, sizeof(uhmask), "%s@%s", user, host))
339       return send_reply(sptr, ERR_LONGMASK);
340     else if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
341       switch (gline_checkmask(host)) {
342       case CHECK_OVERRIDABLE: /* oper overrided restriction */
343         if (flags & GLINE_OPERFORCE)
344           break;
345         /*FALLTHROUGH*/
346       case CHECK_REJECTED:
347         return send_reply(sptr, ERR_MASKTOOWIDE, uhmask);
348         break;
349       }
350
351       if ((tmp = count_users(uhmask)) >=
352           feature_int(FEAT_GLINEMAXUSERCOUNT) && !(flags & GLINE_OPERFORCE))
353         return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
354     }
355   }
356
357   /*
358    * You cannot set a negative (or zero) expire time, nor can you set an
359    * expiration time for greater than GLINE_MAX_EXPIRE.
360    */
361   if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
362     if (!IsServer(sptr) && MyConnect(sptr))
363       send_reply(sptr, ERR_BADEXPIRE, expire);
364     return 0;
365   }
366
367   expire += CurrentTime; /* convert from lifetime to timestamp */
368
369   /* Inform ops... */
370   sendto_opmask_butone(0, SNO_GLINE, "%s adding %s %s for %s%s%s, expiring at "
371                        "%Tu: %s",
372 #ifdef HEAD_IN_SAND_SNOTICES
373                        cli_name(sptr),
374 #else
375                        IsServer(sptr) ? cli_name(sptr) :
376                        cli_name((cli_user(sptr))->server),
377 #endif
378                        flags & GLINE_LOCAL ? "local" : "global",
379                        flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", user,
380                        flags & GLINE_BADCHAN ? "" : "@",
381                        flags & GLINE_BADCHAN ? "" : host,
382                        expire + TSoffset, reason);
383
384   /* and log it */
385   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
386             "%#C adding %s %s for %s, expiring at %Tu: %s", sptr,
387             flags & GLINE_LOCAL ? "local" : "global",
388             flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
389             expire + TSoffset, reason);
390
391   /* make the gline */
392   agline = make_gline(user, host, reason, expire, lastmod, flags);
393
394   if (!agline) /* if it overlapped, silently return */
395     return 0;
396
397   gline_propagate(cptr, sptr, agline);
398
399   if (GlineIsBadChan(agline))
400     return 0;
401
402   return do_gline(cptr, sptr, agline); /* knock off users if necessary */
403 }
404
405 int
406 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
407                time_t lastmod, unsigned int flags)
408 {
409   unsigned int saveflags = 0;
410
411   assert(0 != gline);
412
413   saveflags = gline->gl_flags;
414
415   if (flags & GLINE_LOCAL)
416     gline->gl_flags &= ~GLINE_LDEACT;
417   else {
418     gline->gl_flags |= GLINE_ACTIVE;
419
420     if (gline->gl_lastmod) {
421       if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
422         gline->gl_lastmod++;
423       else
424         gline->gl_lastmod = lastmod;
425     }
426   }
427
428   if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
429     return 0; /* was active to begin with */
430
431   /* Inform ops and log it */
432   sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
433                        "expiring at %Tu: %s",
434 #ifdef HEAD_IN_SAND_SNOTICES
435                        cli_name(sptr),
436 #else
437                        IsServer(sptr) ? cli_name(sptr) :
438                        cli_name((cli_user(sptr))->server),
439 #endif
440                        GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
441                        gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
442                        GlineIsBadChan(gline) ? "" : gline->gl_host,
443                        gline->gl_expire + TSoffset, gline->gl_reason);
444
445   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
446             "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
447             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
448             GlineIsBadChan(gline) ? "" : "@",
449             GlineIsBadChan(gline) ? "" : gline->gl_host,
450             gline->gl_expire + TSoffset, gline->gl_reason);
451
452   if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
453     gline_propagate(cptr, sptr, gline);
454
455   return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
456 }
457
458 int
459 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
460                  time_t lastmod, unsigned int flags)
461 {
462   unsigned int saveflags = 0;
463   char *msg;
464
465   assert(0 != gline);
466
467   saveflags = gline->gl_flags;
468
469   if (GlineIsLocal(gline))
470     msg = "removing local";
471   else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
472     msg = "removing global";
473     gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
474   } else {
475     msg = "deactivating global";
476
477     if (flags & GLINE_LOCAL)
478       gline->gl_flags |= GLINE_LDEACT;
479     else {
480       gline->gl_flags &= ~GLINE_ACTIVE;
481
482       if (gline->gl_lastmod) {
483         if (gline->gl_lastmod >= lastmod)
484           gline->gl_lastmod++;
485         else
486           gline->gl_lastmod = lastmod;
487       }
488     }
489
490     if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
491       return 0; /* was inactive to begin with */
492   }
493
494   /* Inform ops and log it */
495   sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
496                        "%s",
497 #ifdef HEAD_IN_SAND_SNOTICES
498                        cli_name(sptr),
499 #else
500                        IsServer(sptr) ? cli_name(sptr) :
501                        cli_name((cli_user(sptr))->server),
502 #endif
503                        msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
504                        gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
505                        GlineIsBadChan(gline) ? "" : gline->gl_host,
506                        gline->gl_expire + TSoffset, gline->gl_reason);
507
508   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
509             "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
510             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
511             GlineIsBadChan(gline) ? "" : "@",
512             GlineIsBadChan(gline) ? "" : gline->gl_host,
513             gline->gl_expire + TSoffset, gline->gl_reason);
514
515   if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
516     gline_propagate(cptr, sptr, gline);
517
518   /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
519   if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
520     gline_free(gline); /* get rid of it */
521
522   return 0;
523 }
524
525 struct Gline *
526 gline_find(char *userhost, unsigned int flags)
527 {
528   struct Gline *gline;
529   struct Gline *sgline;
530   char *user, *host, *t_uh;
531
532   if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
533     for (gline = BadChanGlineList; gline; gline = sgline) {
534       sgline = gline->gl_next;
535
536       if (gline->gl_expire <= CurrentTime)
537         gline_free(gline);
538       else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
539                (flags & GLINE_LASTMOD && !gline->gl_lastmod))
540         continue;
541       else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
542                 match(gline->gl_user, userhost)) == 0)
543         return gline;
544     }
545   }
546
547   if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
548       *userhost == '#' || *userhost == '&' || *userhost == '+'
549 #ifndef NO_OLD_GLINE
550       || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
551 #endif /* NO_OLD_GLINE */
552       )
553     return 0;
554
555   DupString(t_uh, userhost);
556   canon_userhost(t_uh, &user, &host, 0);
557
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 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
564              (flags & GLINE_LASTMOD && !gline->gl_lastmod))
565       continue;
566     else if (flags & GLINE_EXACT) {
567       if (ircd_strcmp(gline->gl_host, host) == 0 &&
568           ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
569            ircd_strcmp(gline->gl_user, user) == 0))
570         break;
571     } else {
572       if (match(gline->gl_host, host) == 0 &&
573           ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
574            match(gline->gl_user, user) == 0))
575       break;
576     }
577   }
578
579   MyFree(t_uh);
580
581   return gline;
582 }
583
584 struct Gline *
585 gline_lookup(struct Client *cptr, unsigned int flags)
586 {
587   struct Gline *gline;
588   struct Gline *sgline;
589
590   for (gline = GlobalGlineList; gline; gline = sgline) {
591     sgline = gline->gl_next;
592
593     if (gline->gl_expire <= CurrentTime) {
594       gline_free(gline);
595       continue;
596     }
597     
598     if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
599              (flags & GLINE_LASTMOD && !gline->gl_lastmod))
600       continue;
601      
602     if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
603       continue;
604          
605     if (GlineIsIpMask(gline)) {
606       Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",(cli_ip(cptr)).s_addr,gline->ipnum.s_addr,gline->bits));
607       if (((cli_ip(cptr)).s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
608         continue;
609     }
610     else {
611       if (match(gline->gl_host, (cli_user(cptr))->host) != 0) 
612         continue;
613     }
614     return gline;
615   }
616   /*
617    * No Glines matched
618    */
619   return 0;
620 }
621
622 void
623 gline_free(struct Gline *gline)
624 {
625   assert(0 != gline);
626
627   *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
628   if (gline->gl_next)
629     gline->gl_next->gl_prev_p = gline->gl_prev_p;
630
631   MyFree(gline->gl_user); /* free up the memory */
632   if (gline->gl_host)
633     MyFree(gline->gl_host);
634   MyFree(gline->gl_reason);
635   MyFree(gline);
636 }
637
638 void
639 gline_burst(struct Client *cptr)
640 {
641   struct Gline *gline;
642   struct Gline *sgline;
643
644   for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
645     sgline = gline->gl_next;
646
647     if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
648       gline_free(gline);
649     else if (!GlineIsLocal(gline) && gline->gl_lastmod)
650       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s@%s %Tu %Tu :%s",
651                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
652                     gline->gl_host, gline->gl_expire - CurrentTime,
653                     gline->gl_lastmod, gline->gl_reason);
654   }
655
656   for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
657     sgline = gline->gl_next;
658
659     if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
660       gline_free(gline);
661     else if (!GlineIsLocal(gline) && gline->gl_lastmod)
662       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
663                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
664                     gline->gl_expire - CurrentTime, gline->gl_lastmod,
665                     gline->gl_reason);
666   }
667 }
668
669 int
670 gline_resend(struct Client *cptr, struct Gline *gline)
671 {
672   if (GlineIsLocal(gline) || !gline->gl_lastmod)
673     return 0;
674
675   sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
676                 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
677                 GlineIsBadChan(gline) ? "" : "@",
678                 GlineIsBadChan(gline) ? "" : gline->gl_host,
679                 gline->gl_expire - CurrentTime, gline->gl_lastmod,
680                 gline->gl_reason);
681
682   return 0;
683 }
684
685 int
686 gline_list(struct Client *sptr, char *userhost)
687 {
688   struct Gline *gline;
689   struct Gline *sgline;
690
691   if (userhost) {
692     if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
693       return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
694
695     /* send gline information along */
696     send_reply(sptr, RPL_GLIST, gline->gl_user,
697                GlineIsBadChan(gline) ? "" : "@",
698                GlineIsBadChan(gline) ? "" : gline->gl_host,
699                gline->gl_expire + TSoffset,
700                GlineIsLocal(gline) ? cli_name(&me) : "*",
701                GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
702   } else {
703     for (gline = GlobalGlineList; gline; gline = sgline) {
704       sgline = gline->gl_next;
705
706       if (gline->gl_expire <= CurrentTime)
707         gline_free(gline);
708       else
709         send_reply(sptr, RPL_GLIST, gline->gl_user, "@", gline->gl_host,
710                    gline->gl_expire + TSoffset,
711                    GlineIsLocal(gline) ? cli_name(&me) : "*",
712                    GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
713     }
714
715     for (gline = BadChanGlineList; gline; gline = sgline) {
716       sgline = gline->gl_next;
717
718       if (gline->gl_expire <= CurrentTime)
719         gline_free(gline);
720       else
721         send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
722                    gline->gl_expire + TSoffset,
723                    GlineIsLocal(gline) ? cli_name(&me) : "*",
724                    GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
725     }
726   }
727
728   /* end of gline information */
729   return send_reply(sptr, RPL_ENDOFGLIST);
730 }
731
732 void
733 gline_stats(struct Client *sptr)
734 {
735   struct Gline *gline;
736   struct Gline *sgline;
737
738   for (gline = GlobalGlineList; gline; gline = sgline) {
739     sgline = gline->gl_next;
740
741     if (gline->gl_expire <= CurrentTime)
742       gline_free(gline);
743     else
744       send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user, gline->gl_host,
745                  gline->gl_expire + TSoffset, gline->gl_reason);
746   }
747 }