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