Add more G-line length tests. Resolves SF#2985922.
[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 2, 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 /** @file
21  * @brief Implementation of Gline manipulation functions.
22  * @version $Id$
23  */
24 #include "config.h"
25
26 #include "gline.h"
27 #include "channel.h"
28 #include "client.h"
29 #include "ircd.h"
30 #include "ircd_alloc.h"
31 #include "ircd_features.h"
32 #include "ircd_log.h"
33 #include "ircd_reply.h"
34 #include "ircd_snprintf.h"
35 #include "ircd_string.h"
36 #include "match.h"
37 #include "numeric.h"
38 #include "s_bsd.h"
39 #include "s_debug.h"
40 #include "s_misc.h"
41 #include "s_stats.h"
42 #include "send.h"
43 #include "struct.h"
44 #include "sys.h"
45 #include "msg.h"
46 #include "numnicks.h"
47 #include "numeric.h"
48
49 /* #include <assert.h> -- Now using assert in ircd_log.h */
50 #include <string.h>
51 #include <stdio.h>
52 #include <stdlib.h>
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 /** List of user G-lines. */
68 struct Gline* GlobalGlineList  = 0;
69 /** List of BadChan G-lines. */
70 struct Gline* BadChanGlineList = 0;
71
72 /** Iterate through \a list of G-lines.  Use this like a for loop,
73  * i.e., follow it with braces and use whatever you passed as \a gl
74  * as a single G-line to be acted upon.
75  *
76  * @param[in] list List of G-lines to iterate over.
77  * @param[in] gl Name of a struct Gline pointer variable that will be made to point to the G-lines in sequence.
78  * @param[in] next Name of a scratch struct Gline pointer variable.
79  */
80 /* There is some subtlety here with the boolean operators:
81  * (x || 1) is used to continue in a logical-and series even when !x.
82  * (x && 0) is used to continue in a logical-or series even when x.
83  */
84 #define gliter(list, gl, next)                          \
85   /* Iterate through the G-lines in the list */         \
86   for ((gl) = (list); (gl); (gl) = (next))              \
87     /* Figure out the next pointer in list... */        \
88     if ((((next) = (gl)->gl_next) || 1) &&              \
89         /* Then see if it's expired */                  \
90         (gl)->gl_lifetime <= TStime())                  \
91       /* Record has expired, so free the G-line */      \
92       gline_free((gl));                                 \
93     /* See if we need to expire the G-line */           \
94     else if ((((gl)->gl_expire > TStime()) ||           \
95               (((gl)->gl_flags &= ~GLINE_ACTIVE) && 0) ||       \
96               ((gl)->gl_state = GLOCAL_GLOBAL)) && 0)   \
97       ; /* empty statement */                           \
98     else
99
100 /** Find canonical user and host for a string.
101  * If \a userhost starts with '$', assign \a userhost to *user_p and NULL to *host_p.
102  * Otherwise, if \a userhost contains '@', assign the earlier part of it to *user_p and the rest to *host_p.
103  * Otherwise, assign \a def_user to *user_p and \a userhost to *host_p.
104  *
105  * @param[in] userhost Input string from user.
106  * @param[out] user_p Gets pointer to user (or channel/realname) part of hostmask.
107  * @param[out] host_p Gets point to host part of hostmask (may be assigned NULL).
108  * @param[in] def_user Default value for user part.
109  */
110 static void
111 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
112 {
113   char *tmp;
114
115   if (*userhost == '$') {
116     *user_p = userhost;
117     *host_p = NULL;
118     return;
119   }
120
121   if (!(tmp = strchr(userhost, '@'))) {
122     *user_p = def_user;
123     *host_p = userhost;
124   } else {
125     *user_p = userhost;
126     *(tmp++) = '\0';
127     *host_p = tmp;
128   }
129 }
130
131 /** Create a Gline structure.
132  * @param[in] user User part of mask.
133  * @param[in] host Host part of mask (NULL if not applicable).
134  * @param[in] reason Reason for G-line.
135  * @param[in] expire Expiration timestamp.
136  * @param[in] lastmod Last modification timestamp.
137  * @param[in] flags Bitwise combination of GLINE_* bits.
138  * @return Newly allocated G-line.
139  */
140 static struct Gline *
141 make_gline(char *user, char *host, char *reason, time_t expire, time_t lastmod,
142            time_t lifetime, unsigned int flags)
143 {
144   struct Gline *gline;
145
146   assert(0 != expire);
147
148   gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
149   assert(0 != gline);
150
151   DupString(gline->gl_reason, reason); /* initialize gline... */
152   gline->gl_expire = expire;
153   gline->gl_lifetime = lifetime;
154   gline->gl_lastmod = lastmod;
155   gline->gl_flags = flags & GLINE_MASK;
156   gline->gl_state = GLOCAL_GLOBAL; /* not locally modified */
157
158   if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
159     DupString(gline->gl_user, user); /* first, remember channel */
160     gline->gl_host = NULL;
161
162     gline->gl_next = BadChanGlineList; /* then link it into list */
163     gline->gl_prev_p = &BadChanGlineList;
164     if (BadChanGlineList)
165       BadChanGlineList->gl_prev_p = &gline->gl_next;
166     BadChanGlineList = gline;
167   } else {
168     DupString(gline->gl_user, user); /* remember them... */
169     if (*user != '$')
170       DupString(gline->gl_host, host);
171     else
172       gline->gl_host = NULL;
173
174     if (*user != '$' && ipmask_parse(host, &gline->gl_addr, &gline->gl_bits))
175       gline->gl_flags |= GLINE_IPMASK;
176
177     gline->gl_next = GlobalGlineList; /* then link it into list */
178     gline->gl_prev_p = &GlobalGlineList;
179     if (GlobalGlineList)
180       GlobalGlineList->gl_prev_p = &gline->gl_next;
181     GlobalGlineList = gline;
182   }
183
184   return gline;
185 }
186
187 /** Check local clients against a new G-line.
188  * If the G-line is inactive or a badchan, return immediately.
189  * Otherwise, if any users match it, disconnect them.
190  * @param[in] cptr Peer connect that sent the G-line.
191  * @param[in] sptr Client that originated the G-line.
192  * @param[in] gline New G-line to check.
193  * @return Zero, unless \a sptr G-lined himself, in which case CPTR_KILLED.
194  */
195 static int
196 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
197 {
198   struct Client *acptr;
199   int fd, retval = 0, tval;
200
201   if (feature_bool(FEAT_DISABLE_GLINES))
202     return 0; /* G-lines are disabled */
203
204   if (GlineIsBadChan(gline)) /* no action taken on badchan glines */
205     return 0;
206   if (!GlineIsActive(gline)) /* no action taken on inactive glines */
207     return 0;
208
209   for (fd = HighestFd; fd >= 0; --fd) {
210     /*
211      * get the users!
212      */
213     if ((acptr = LocalClientArray[fd])) {
214       if (!cli_user(acptr))
215         continue;
216
217       if (GlineIsRealName(gline)) { /* Realname Gline */
218         Debug((DEBUG_DEBUG,"Realname Gline: %s %s",(cli_info(acptr)),
219                                         gline->gl_user+2));
220         if (match(gline->gl_user+2, cli_info(acptr)) != 0)
221             continue;
222         Debug((DEBUG_DEBUG,"Matched!"));
223       } else { /* Host/IP gline */
224         if (cli_user(acptr)->username &&
225             match(gline->gl_user, (cli_user(acptr))->username) != 0)
226           continue;
227
228         if (GlineIsIpMask(gline)) {
229           if (!ipmask_check(&cli_ip(acptr), &gline->gl_addr, gline->gl_bits))
230             continue;
231         }
232         else {
233           if (match(gline->gl_host, cli_sockhost(acptr)) != 0)
234             continue;
235         }
236       }
237
238       /* ok, here's one that got G-lined */
239       send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
240            gline->gl_reason);
241
242       /* let the ops know about it */
243       sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
244                            get_client_name(acptr, SHOW_IP));
245
246       /* and get rid of him */
247       if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
248           gline->gl_reason)))
249         retval = tval; /* retain killed status */
250     }
251   }
252   return retval;
253 }
254
255 /**
256  * Implements the mask checking applied to local G-lines.
257  * Basically, host masks must have a minimum of two non-wild domain
258  * fields, and IP masks must have a minimum of 16 bits.  If the mask
259  * has even one wild-card, OVERRIDABLE is returned, assuming the other
260  * check doesn't fail.
261  * @param[in] mask G-line mask to check.
262  * @return One of CHECK_REJECTED, CHECK_OVERRIDABLE, or CHECK_APPROVED.
263  */
264 static int
265 gline_checkmask(char *mask)
266 {
267   unsigned int flags = MASK_IP;
268   unsigned int dots = 0;
269   unsigned int ipmask = 0;
270
271   for (; *mask; mask++) { /* go through given mask */
272     if (*mask == '.') { /* it's a separator; advance positional wilds */
273       flags = (flags & ~MASK_WILD_MASK) | ((flags << 1) & MASK_WILD_MASK);
274       dots++;
275
276       if ((flags & (MASK_IP | MASK_WILDS)) == MASK_IP)
277         ipmask += 8; /* It's an IP with no wilds, count bits */
278     } else if (*mask == '*' || *mask == '?')
279       flags |= MASK_WILD_0 | MASK_WILDS; /* found a wildcard */
280     else if (*mask == '/') { /* n.n.n.n/n notation; parse bit specifier */
281       ++mask;
282       ipmask = strtoul(mask, &mask, 10);
283
284       /* sanity-check to date */
285       if (*mask || (flags & (MASK_WILDS | MASK_IP)) != MASK_IP)
286         return CHECK_REJECTED;
287       if (!dots) {
288         if (ipmask > 128)
289           return CHECK_REJECTED;
290         if (ipmask < 128)
291           flags |= MASK_WILDS;
292       } else {
293         if (dots != 3 || ipmask > 32)
294           return CHECK_REJECTED;
295         if (ipmask < 32)
296           flags |= MASK_WILDS;
297       }
298
299       flags |= MASK_HALT; /* Halt the ipmask calculation */
300       break; /* get out of the loop */
301     } else if (!IsIP6Char(*mask)) {
302       flags &= ~MASK_IP; /* not an IP anymore! */
303       ipmask = 0;
304     }
305   }
306
307   /* Sanity-check quads */
308   if (dots > 3 || (!(flags & MASK_WILDS) && dots < 3)) {
309     flags &= ~MASK_IP;
310     ipmask = 0;
311   }
312
313   /* update bit count if necessary */
314   if ((flags & (MASK_IP | MASK_WILDS | MASK_HALT)) == MASK_IP)
315     ipmask += 8;
316
317   /* Check to see that it's not too wide of a mask */
318   if (flags & MASK_WILDS &&
319       ((!(flags & MASK_IP) && (dots < 2 || flags & MASK_WILD_MASK)) ||
320        (flags & MASK_IP && ipmask < 16)))
321     return CHECK_REJECTED; /* to wide, reject */
322
323   /* Ok, it's approved; require override if it has wildcards, though */
324   return flags & MASK_WILDS ? CHECK_OVERRIDABLE : CHECK_APPROVED;
325 }
326
327 /** Forward a G-line to other servers.
328  * @param[in] cptr Client that sent us the G-line.
329  * @param[in] sptr Client that originated the G-line.
330  * @param[in] gline G-line to forward.
331  * @return Zero.
332  */
333 static int
334 gline_propagate(struct Client *cptr, struct Client *sptr, struct Gline *gline)
335 {
336   if (GlineIsLocal(gline))
337     return 0;
338
339   assert(gline->gl_lastmod);
340
341   sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
342                         GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
343                         gline->gl_host ? "@" : "",
344                         gline->gl_host ? gline->gl_host : "",
345                         gline->gl_expire - TStime(), gline->gl_lastmod,
346                         gline->gl_lifetime, gline->gl_reason);
347
348   return 0;
349 }
350
351 /** Count number of users who match \a mask.
352  * @param[in] mask user\@host or user\@ip mask to check.
353  * @param[in] flags Bitmask possibly containing the value GLINE_LOCAL, to limit searches to this server.
354  * @return Count of matching users.
355  */
356 static int
357 count_users(char *mask, int flags)
358 {
359   struct irc_in_addr ipmask;
360   struct Client *acptr;
361   int count = 0;
362   int ipmask_valid;
363   char namebuf[USERLEN + HOSTLEN + 2];
364   char ipbuf[USERLEN + SOCKIPLEN + 2];
365   unsigned char ipmask_len;
366
367   ipmask_valid = ipmask_parse(mask, &ipmask, &ipmask_len);
368   for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
369     if (!IsUser(acptr))
370       continue;
371     if ((flags & GLINE_LOCAL) && !MyConnect(acptr))
372       continue;
373
374     ircd_snprintf(0, namebuf, sizeof(namebuf), "%s@%s",
375                   cli_user(acptr)->username, cli_user(acptr)->realhost);
376     ircd_snprintf(0, ipbuf, sizeof(ipbuf), "%s@%s", cli_user(acptr)->username,
377                   ircd_ntoa(&cli_ip(acptr)));
378
379     if (!match(mask, namebuf)
380         || !match(mask, ipbuf)
381         || (ipmask_valid && ipmask_check(&cli_ip(acptr), &ipmask, ipmask_len)))
382       count++;
383   }
384
385   return count;
386 }
387
388 /** Count number of users with a realname matching \a mask.
389  * @param[in] mask Wildcard mask to match against realnames.
390  * @return Count of matching users.
391  */
392 static int
393 count_realnames(const char *mask)
394 {
395   struct Client *acptr;
396   int minlen;
397   int count;
398   char cmask[BUFSIZE];
399
400   count = 0;
401   matchcomp(cmask, &minlen, NULL, mask);
402   for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
403     if (!IsUser(acptr))
404       continue;
405     if (strlen(cli_info(acptr)) < minlen)
406       continue;
407     if (!matchexec(cli_info(acptr), cmask, minlen))
408       count++;
409   }
410   return count;
411 }
412
413 /** Create a new G-line and add it to global lists.
414  * \a userhost may be in one of four forms:
415  * \li A channel name, to add a BadChan.
416  * \li A string starting with $R and followed by a mask to match against their realname.
417  * \li A user\@IP mask (user\@ part optional) to create an IP-based ban.
418  * \li A user\@host mask (user\@ part optional) to create a hostname ban.
419  *
420  * @param[in] cptr Client that sent us the G-line.
421  * @param[in] sptr Client that originated the G-line.
422  * @param[in] userhost Text mask for the G-line.
423  * @param[in] reason Reason for G-line.
424  * @param[in] expire Expiration time of G-line.
425  * @param[in] lastmod Last modification time of G-line.
426  * @param[in] lifetime Lifetime of G-line.
427  * @param[in] flags Bitwise combination of GLINE_* flags.
428  * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
429  */
430 int
431 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
432           char *reason, time_t expire, time_t lastmod, time_t lifetime,
433           unsigned int flags)
434 {
435   struct Gline *agline;
436   char uhmask[USERLEN + HOSTLEN + 2];
437   char *user, *host;
438   int tmp;
439
440   assert(0 != userhost);
441   assert(0 != reason);
442   assert(((flags & (GLINE_GLOBAL | GLINE_LOCAL)) == GLINE_GLOBAL) ||
443          ((flags & (GLINE_GLOBAL | GLINE_LOCAL)) == GLINE_LOCAL));
444
445   Debug((DEBUG_DEBUG, "gline_add(\"%s\", \"%s\", \"%s\", \"%s\", %Tu, %Tu "
446          "%Tu, 0x%04x)", cli_name(cptr), cli_name(sptr), userhost, reason,
447          expire, lastmod, lifetime, flags));
448
449   if (*userhost == '#' || *userhost == '&') {
450     if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
451       return send_reply(sptr, ERR_NOPRIVILEGES);
452     /* Allow maximum channel name length, plus margin for wildcards. */
453     if (strlen(userhost+1) >= CHANNELLEN + 6)
454       return send_reply(sptr, ERR_LONGMASK);
455
456     flags |= GLINE_BADCHAN;
457     user = userhost;
458     host = NULL;
459   } else if (*userhost == '$') {
460     switch (userhost[1]) {
461       case 'R':
462         /* Allow REALLEN for the real name, plus margin for wildcards. */
463         if (strlen(userhost+2) >= REALLEN + 6)
464           return send_reply(sptr, ERR_LONGMASK);
465         flags |= GLINE_REALNAME;
466         break;
467       default:
468         /* uh, what to do here? */
469         /* The answer, my dear Watson, is we throw a protocol_violation()
470            -- hikari */
471         if (IsServer(cptr))
472           return protocol_violation(sptr,"%s has been smoking the sweet leaf and sent me a whacky gline",cli_name(sptr));
473         sendto_opmask_butone(NULL, SNO_GLINE, "%s has been smoking the sweet leaf and sent me a whacky gline", cli_name(sptr));
474         return 0;
475     }
476     user = userhost;
477     host = NULL;
478     if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
479       tmp = count_realnames(userhost + 2);
480       if ((tmp >= feature_int(FEAT_GLINEMAXUSERCOUNT))
481           && !(flags & GLINE_OPERFORCE))
482         return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
483     }
484   } else {
485     canon_userhost(userhost, &user, &host, "*");
486     if (sizeof(uhmask) <
487         ircd_snprintf(0, uhmask, sizeof(uhmask), "%s@%s", user, host))
488       return send_reply(sptr, ERR_LONGMASK);
489     else if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
490       switch (gline_checkmask(host)) {
491       case CHECK_OVERRIDABLE: /* oper overrided restriction */
492         if (flags & GLINE_OPERFORCE)
493           break;
494         /*FALLTHROUGH*/
495       case CHECK_REJECTED:
496         return send_reply(sptr, ERR_MASKTOOWIDE, uhmask);
497         break;
498       }
499
500       if ((tmp = count_users(uhmask, flags)) >=
501           feature_int(FEAT_GLINEMAXUSERCOUNT) && !(flags & GLINE_OPERFORCE))
502         return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
503     }
504   }
505
506   /*
507    * You cannot set a negative (or zero) expire time, nor can you set an
508    * expiration time for greater than GLINE_MAX_EXPIRE.
509    */
510   if (!(flags & GLINE_FORCE) &&
511       (expire <= TStime() || expire > TStime() + GLINE_MAX_EXPIRE)) {
512     if (!IsServer(sptr) && MyConnect(sptr))
513       send_reply(sptr, ERR_BADEXPIRE, expire);
514     return 0;
515   } else if (expire <= TStime()) {
516     /* This expired G-line was forced to be added, so mark it inactive. */
517     flags &= ~GLINE_ACTIVE;
518   }
519
520   if (!lifetime) /* no lifetime set, use expiration time */
521     lifetime = expire;
522
523   /* lifetime is already an absolute timestamp */
524
525   /* Inform ops... */
526   sendto_opmask_butone(0, ircd_strncmp(reason, "AUTO", 4) ? SNO_GLINE :
527                        SNO_AUTO, "%s adding %s%s %s for %s%s%s, expiring at "
528                        "%Tu: %s",
529                        (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
530                          cli_name(sptr) :
531                          cli_name((cli_user(sptr))->server),
532                        (flags & GLINE_ACTIVE) ? "" : "deactivated ",
533                        (flags & GLINE_LOCAL) ? "local" : "global",
534                        (flags & GLINE_BADCHAN) ? "BADCHAN" : "GLINE", user,
535                        (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : "@",
536                        (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : host,
537                        expire, reason);
538
539   /* and log it */
540   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
541             "%#C adding %s %s for %s%s%s, expiring at %Tu: %s", sptr,
542             flags & GLINE_LOCAL ? "local" : "global",
543             flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", user,
544             flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : "@",
545             flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : host,
546             expire, reason);
547
548   /* make the gline */
549   agline = make_gline(user, host, reason, expire, lastmod, lifetime, flags);
550
551   /* since we've disabled overlapped G-line checking, agline should
552    * never be NULL...
553    */
554   assert(agline);
555
556   gline_propagate(cptr, sptr, agline);
557
558   return do_gline(cptr, sptr, agline); /* knock off users if necessary */
559 }
560
561 /** Activate a currently inactive G-line.
562  * @param[in] cptr Peer that told us to activate the G-line.
563  * @param[in] sptr Client that originally thought it was a good idea.
564  * @param[in] gline G-line to activate.
565  * @param[in] lastmod New value for last modification timestamp.
566  * @param[in] flags 0 if the activation should be propagated, GLINE_LOCAL if not.
567  * @return Zero, unless \a sptr had a death wish (in which case CPTR_KILLED).
568  */
569 int
570 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
571                time_t lastmod, unsigned int flags)
572 {
573   unsigned int saveflags = 0;
574
575   assert(0 != gline);
576
577   saveflags = gline->gl_flags;
578
579   if (flags & GLINE_LOCAL)
580     gline->gl_flags &= ~GLINE_LDEACT;
581   else {
582     gline->gl_flags |= GLINE_ACTIVE;
583
584     if (gline->gl_lastmod) {
585       if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
586         gline->gl_lastmod++;
587       else
588         gline->gl_lastmod = lastmod;
589     }
590   }
591
592   if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
593     return 0; /* was active to begin with */
594
595   /* Inform ops and log it */
596   sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
597                        "expiring at %Tu: %s",
598                        (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
599                          cli_name(sptr) :
600                          cli_name((cli_user(sptr))->server),
601                        GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
602                        gline->gl_user, gline->gl_host ? "@" : "",
603                        gline->gl_host ? gline->gl_host : "",
604                        gline->gl_expire, gline->gl_reason);
605
606   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
607             "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
608             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
609             gline->gl_host ? "@" : "",
610             gline->gl_host ? gline->gl_host : "",
611             gline->gl_expire, gline->gl_reason);
612
613   if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
614     gline_propagate(cptr, sptr, gline);
615
616   return do_gline(cptr, sptr, gline);
617 }
618
619 /** Deactivate a G-line.
620  * @param[in] cptr Peer that gave us the message.
621  * @param[in] sptr Client that initiated the deactivation.
622  * @param[in] gline G-line to deactivate.
623  * @param[in] lastmod New value for G-line last modification timestamp.
624  * @param[in] flags GLINE_LOCAL to only deactivate locally, 0 to propagate.
625  * @return Zero.
626  */
627 int
628 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
629                  time_t lastmod, unsigned int flags)
630 {
631   unsigned int saveflags = 0;
632   char *msg;
633
634   assert(0 != gline);
635
636   saveflags = gline->gl_flags;
637
638   if (GlineIsLocal(gline))
639     msg = "removing local";
640   else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
641     msg = "removing global";
642     gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
643   } else {
644     msg = "deactivating global";
645
646     if (flags & GLINE_LOCAL)
647       gline->gl_flags |= GLINE_LDEACT;
648     else {
649       gline->gl_flags &= ~GLINE_ACTIVE;
650
651       if (gline->gl_lastmod) {
652         if (gline->gl_lastmod >= lastmod)
653           gline->gl_lastmod++;
654         else
655           gline->gl_lastmod = lastmod;
656       }
657     }
658
659     if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
660       return 0; /* was inactive to begin with */
661   }
662
663   /* Inform ops and log it */
664   sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
665                        "%s",
666                        (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
667                          cli_name(sptr) :
668                          cli_name((cli_user(sptr))->server),
669                        msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
670                        gline->gl_user, gline->gl_host ? "@" : "",
671                        gline->gl_host ? gline->gl_host : "",
672                        gline->gl_expire, gline->gl_reason);
673
674   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
675             "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
676             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
677             gline->gl_host ? "@" : "",
678             gline->gl_host ? gline->gl_host : "",
679             gline->gl_expire, gline->gl_reason);
680
681   if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
682     gline_propagate(cptr, sptr, gline);
683
684   /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
685   if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
686     gline_free(gline); /* get rid of it */
687
688   return 0;
689 }
690
691 /** Modify a global G-line.
692  * @param[in] cptr Client that sent us the G-line modification.
693  * @param[in] sptr Client that originated the G-line modification.
694  * @param[in] gline G-line being modified.
695  * @param[in] action Resultant status of the G-line.
696  * @param[in] reason Reason for G-line.
697  * @param[in] expire Expiration time of G-line.
698  * @param[in] lastmod Last modification time of G-line.
699  * @param[in] lifetime Lifetime of G-line.
700  * @param[in] flags Bitwise combination of GLINE_* flags.
701  * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
702  */
703 int
704 gline_modify(struct Client *cptr, struct Client *sptr, struct Gline *gline,
705              enum GlineAction action, char *reason, time_t expire,
706              time_t lastmod, time_t lifetime, unsigned int flags)
707 {
708   char buf[BUFSIZE], *op = "";
709   int pos = 0, non_auto = 0;
710
711   assert(gline);
712   assert(!GlineIsLocal(gline));
713
714   Debug((DEBUG_DEBUG,  "gline_modify(\"%s\", \"%s\", \"%s%s%s\", %s, \"%s\", "
715          "%Tu, %Tu, %Tu, 0x%04x)", cli_name(cptr), cli_name(sptr),
716          gline->gl_user, gline->gl_host ? "@" : "",
717          gline->gl_host ? gline->gl_host : "",
718          action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
719          (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
720           (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
721            (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
722             (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>")))),
723          reason, expire, lastmod, lifetime, flags));
724
725   /* First, let's check lastmod... */
726   if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE) {
727     if (GlineLastMod(gline) > lastmod) { /* we have a more recent version */
728       if (IsBurstOrBurstAck(cptr))
729         return 0; /* middle of a burst, it'll resync on its own */
730       return gline_resend(cptr, gline); /* resync the server */
731     } else if (GlineLastMod(gline) == lastmod)
732       return 0; /* we have that version of the G-line... */
733   }
734
735   /* All right, we know that there's a change of some sort.  What is it? */
736   /* first, check out the expiration time... */
737   if ((flags & GLINE_EXPIRE) && expire) {
738     if (!(flags & GLINE_FORCE) &&
739         (expire <= TStime() || expire > TStime() + GLINE_MAX_EXPIRE)) {
740       if (!IsServer(sptr) && MyConnect(sptr)) /* bad expiration time */
741         send_reply(sptr, ERR_BADEXPIRE, expire);
742       return 0;
743     }
744   } else
745     flags &= ~GLINE_EXPIRE;
746
747   /* Now check to see if there's any change... */
748   if ((flags & GLINE_EXPIRE) && expire == gline->gl_expire) {
749     flags &= ~GLINE_EXPIRE; /* no change to expiration time... */
750     expire = 0;
751   }
752
753   /* Next, check out lifetime--this one's a bit trickier... */
754   if (!(flags & GLINE_LIFETIME) || !lifetime)
755     lifetime = gline->gl_lifetime; /* use G-line lifetime */
756
757   lifetime = IRCD_MAX(lifetime, expire); /* set lifetime to the max */
758
759   /* OK, let's see which is greater... */
760   if (lifetime > gline->gl_lifetime)
761     flags |= GLINE_LIFETIME; /* have to update lifetime */
762   else {
763     flags &= ~GLINE_LIFETIME; /* no change to lifetime */
764     lifetime = 0;
765   }
766
767   /* Finally, let's see if the reason needs to be updated */
768   if ((flags & GLINE_REASON) && reason &&
769       !ircd_strcmp(gline->gl_reason, reason))
770     flags &= ~GLINE_REASON; /* no changes to the reason */
771
772   /* OK, now let's take a look at the action... */
773   if ((action == GLINE_ACTIVATE && (gline->gl_flags & GLINE_ACTIVE)) ||
774       (action == GLINE_DEACTIVATE && !(gline->gl_flags & GLINE_ACTIVE)) ||
775       (action == GLINE_LOCAL_ACTIVATE &&
776        (gline->gl_state == GLOCAL_ACTIVATED)) ||
777       (action == GLINE_LOCAL_DEACTIVATE &&
778        (gline->gl_state == GLOCAL_DEACTIVATED)) ||
779       /* can't activate an expired G-line */
780       IRCD_MAX(gline->gl_expire, expire) <= TStime())
781     action = GLINE_MODIFY; /* no activity state modifications */
782
783   Debug((DEBUG_DEBUG,  "About to perform changes; flags 0x%04x, action %s",
784          flags, action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
785          (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
786           (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
787            (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
788             (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>"))))));
789
790   /* If there are no changes to perform, do no changes */
791   if (!(flags & GLINE_UPDATE) && action == GLINE_MODIFY)
792     return 0;
793
794   /* Now we know what needs to be changed, so let's process the changes... */
795
796   /* Start by updating lastmod, if indicated... */
797   if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
798     gline->gl_lastmod = lastmod;
799
800   /* Then move on to activity status changes... */
801   switch (action) {
802   case GLINE_ACTIVATE: /* Globally activating G-line */
803     gline->gl_flags |= GLINE_ACTIVE; /* make it active... */
804     gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
805     pos += ircd_snprintf(0, buf, sizeof(buf), " globally activating G-line");
806     op = "+"; /* operation for G-line propagation */
807     break;
808
809   case GLINE_DEACTIVATE: /* Globally deactivating G-line */
810     gline->gl_flags &= ~GLINE_ACTIVE; /* make it inactive... */
811     gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
812     pos += ircd_snprintf(0, buf, sizeof(buf), " globally deactivating G-line");
813     op = "-"; /* operation for G-line propagation */
814     break;
815
816   case GLINE_LOCAL_ACTIVATE: /* Locally activating G-line */
817     gline->gl_state = GLOCAL_ACTIVATED; /* make it locally active */
818     pos += ircd_snprintf(0, buf, sizeof(buf), " locally activating G-line");
819     break;
820
821   case GLINE_LOCAL_DEACTIVATE: /* Locally deactivating G-line */
822     gline->gl_state = GLOCAL_DEACTIVATED; /* make it locally inactive */
823     pos += ircd_snprintf(0, buf, sizeof(buf), " locally deactivating G-line");
824     break;
825
826   case GLINE_MODIFY: /* no change to activity status */
827     break;
828   }
829
830   /* Handle expiration changes... */
831   if (flags & GLINE_EXPIRE) {
832     gline->gl_expire = expire; /* save new expiration time */
833     if (pos < BUFSIZE)
834       pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
835                            "%s%s changing expiration time to %Tu",
836                            pos ? ";" : "",
837                            pos && !(flags & (GLINE_LIFETIME | GLINE_REASON)) ?
838                            " and" : "", expire);
839   }
840
841   /* Next, handle lifetime changes... */
842   if (flags & GLINE_LIFETIME) {
843     gline->gl_lifetime = lifetime; /* save new lifetime */
844     if (pos < BUFSIZE)
845       pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
846                            "%s%s extending record lifetime to %Tu",
847                            pos ? ";" : "", pos && !(flags & GLINE_REASON) ?
848                            " and" : "", lifetime);
849   }
850
851   /* Now, handle reason changes... */
852   if (flags & GLINE_REASON) {
853     non_auto = non_auto || ircd_strncmp(gline->gl_reason, "AUTO", 4);
854     MyFree(gline->gl_reason); /* release old reason */
855     DupString(gline->gl_reason, reason); /* store new reason */
856     if (pos < BUFSIZE)
857       pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
858                            "%s%s changing reason to \"%s\"",
859                            pos ? ";" : "", pos ? " and" : "", reason);
860   }
861
862   /* All right, inform ops... */
863   non_auto = non_auto || ircd_strncmp(gline->gl_reason, "AUTO", 4);
864   sendto_opmask_butone(0, non_auto ? SNO_GLINE : SNO_AUTO,
865                        "%s modifying global %s for %s%s%s:%s",
866                        (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
867                        cli_name(sptr) : cli_name((cli_user(sptr))->server),
868                        GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
869                        gline->gl_user, gline->gl_host ? "@" : "",
870                        gline->gl_host ? gline->gl_host : "", buf);
871
872   /* and log the change */
873   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
874             "%#C modifying global %s for %s%s%s:%s", sptr,
875             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
876             gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "",
877             buf);
878
879   /* We'll be simple for this release, but we can update this to change
880    * the propagation syntax on future updates
881    */
882   if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
883     sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
884                           "* %s%s%s%s%s %Tu %Tu %Tu :%s",
885                           flags & GLINE_OPERFORCE ? "!" : "", op,
886                           gline->gl_user, gline->gl_host ? "@" : "",
887                           gline->gl_host ? gline->gl_host : "",
888                           gline->gl_expire - TStime(), gline->gl_lastmod,
889                           gline->gl_lifetime, gline->gl_reason);
890
891   /* OK, let's do the G-line... */
892   return do_gline(cptr, sptr, gline);
893 }
894
895 /** Destroy a local G-line.
896  * @param[in] cptr Peer that gave us the message.
897  * @param[in] sptr Client that initiated the destruction.
898  * @param[in] gline G-line to destroy.
899  * @return Zero.
900  */
901 int
902 gline_destroy(struct Client *cptr, struct Client *sptr, struct Gline *gline)
903 {
904   assert(gline);
905   assert(GlineIsLocal(gline));
906
907   /* Inform ops and log it */
908   sendto_opmask_butone(0, SNO_GLINE, "%s removing local %s for %s%s%s",
909                        (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
910                        cli_name(sptr) : cli_name((cli_user(sptr))->server),
911                        GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
912                        gline->gl_user, gline->gl_host ? "@" : "",
913                        gline->gl_host ? gline->gl_host : "");
914   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
915             "%#C removing local %s for %s%s%s", sptr,
916             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
917             gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "");
918
919   gline_free(gline); /* get rid of the G-line */
920
921   return 0; /* convenience return */
922 }
923
924 /** Find a G-line for a particular mask, guided by certain flags.
925  * Certain bits in \a flags are interpreted specially:
926  * <dl>
927  * <dt>GLINE_ANY</dt><dd>Search both BadChans and user G-lines.</dd>
928  * <dt>GLINE_BADCHAN</dt><dd>Search BadChans.</dd>
929  * <dt>GLINE_GLOBAL</dt><dd>Only match global G-lines.</dd>
930  * <dt>GLINE_LOCAL</dt><dd>Only match local G-lines.</dd>
931  * <dt>GLINE_LASTMOD</dt><dd>Only match G-lines with a last modification time.</dd>
932  * <dt>GLINE_EXACT</dt><dd>Require an exact match of G-line mask.</dd>
933  * <dt>anything else</dt><dd>Search user G-lines.</dd>
934  * </dl>
935  * @param[in] userhost Mask to search for.
936  * @param[in] flags Bitwise combination of GLINE_* flags.
937  * @return First matching G-line, or NULL if none are found.
938  */
939 struct Gline *
940 gline_find(char *userhost, unsigned int flags)
941 {
942   struct Gline *gline = 0;
943   struct Gline *sgline;
944   char *user, *host, *t_uh;
945
946   if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
947     gliter(BadChanGlineList, gline, sgline) {
948         if ((flags & (GlineIsLocal(gline) ? GLINE_GLOBAL : GLINE_LOCAL)) ||
949           (flags & GLINE_LASTMOD && !gline->gl_lastmod))
950         continue;
951       else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
952                 match(gline->gl_user, userhost)) == 0)
953         return gline;
954     }
955   }
956
957   if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
958       *userhost == '#' || *userhost == '&')
959     return 0;
960
961   DupString(t_uh, userhost);
962   canon_userhost(t_uh, &user, &host, "*");
963
964   gliter(GlobalGlineList, gline, sgline) {
965     if ((flags & (GlineIsLocal(gline) ? GLINE_GLOBAL : GLINE_LOCAL)) ||
966         (flags & GLINE_LASTMOD && !gline->gl_lastmod))
967       continue;
968     else if (flags & GLINE_EXACT) {
969       if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
970            || (!gline->gl_host && !host)) &&
971           (ircd_strcmp(gline->gl_user, user) == 0))
972         break;
973     } else {
974       if (((gline->gl_host && host && match(gline->gl_host, host) == 0)
975            || (!gline->gl_host && !host)) &&
976           (match(gline->gl_user, user) == 0))
977         break;
978     }
979   }
980
981   MyFree(t_uh);
982
983   return gline;
984 }
985
986 /** Find a matching G-line for a user.
987  * @param[in] cptr Client to compare against.
988  * @param[in] flags Bitwise combination of GLINE_GLOBAL and/or
989  * GLINE_LASTMOD to limit matches.
990  * @return Matching G-line, or NULL if none are found.
991  */
992 struct Gline *
993 gline_lookup(struct Client *cptr, unsigned int flags)
994 {
995   struct Gline *gline;
996   struct Gline *sgline;
997
998   gliter(GlobalGlineList, gline, sgline) {
999     if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
1000         (flags & GLINE_LASTMOD && !gline->gl_lastmod))
1001       continue;
1002
1003     if (GlineIsRealName(gline)) {
1004       Debug((DEBUG_DEBUG,"realname gline: '%s' '%s'",gline->gl_user,cli_info(cptr)));
1005       if (match(gline->gl_user+2, cli_info(cptr)) != 0)
1006         continue;
1007     }
1008     else {
1009       if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
1010         continue;
1011
1012       if (GlineIsIpMask(gline)) {
1013         if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
1014           continue;
1015       }
1016       else {
1017         if (match(gline->gl_host, (cli_user(cptr))->realhost) != 0)
1018           continue;
1019       }
1020     }
1021     if (GlineIsActive(gline))
1022       return gline;
1023   }
1024   /*
1025    * No Glines matched
1026    */
1027   return 0;
1028 }
1029
1030 /** Delink and free a G-line.
1031  * @param[in] gline G-line to free.
1032  */
1033 void
1034 gline_free(struct Gline *gline)
1035 {
1036   assert(0 != gline);
1037
1038   *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
1039   if (gline->gl_next)
1040     gline->gl_next->gl_prev_p = gline->gl_prev_p;
1041
1042   MyFree(gline->gl_user); /* free up the memory */
1043   if (gline->gl_host)
1044     MyFree(gline->gl_host);
1045   MyFree(gline->gl_reason);
1046   MyFree(gline);
1047 }
1048
1049 /** Burst all known global G-lines to another server.
1050  * @param[in] cptr Destination of burst.
1051  */
1052 void
1053 gline_burst(struct Client *cptr)
1054 {
1055   struct Gline *gline;
1056   struct Gline *sgline;
1057
1058   gliter(GlobalGlineList, gline, sgline) {
1059     if (!GlineIsLocal(gline) && gline->gl_lastmod)
1060       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1061                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1062                     gline->gl_host ? "@" : "",
1063                     gline->gl_host ? gline->gl_host : "",
1064                     gline->gl_expire - TStime(), gline->gl_lastmod,
1065                     gline->gl_lifetime, gline->gl_reason);
1066   }
1067
1068   gliter(BadChanGlineList, gline, sgline) {
1069     if (!GlineIsLocal(gline) && gline->gl_lastmod)
1070       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu %Tu :%s",
1071                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1072                     gline->gl_expire - TStime(), gline->gl_lastmod,
1073                     gline->gl_lifetime, gline->gl_reason);
1074   }
1075 }
1076
1077 /** Send a G-line to another server.
1078  * @param[in] cptr Who to inform of the G-line.
1079  * @param[in] gline G-line to send.
1080  * @return Zero.
1081  */
1082 int
1083 gline_resend(struct Client *cptr, struct Gline *gline)
1084 {
1085   if (GlineIsLocal(gline) || !gline->gl_lastmod)
1086     return 0;
1087
1088   sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1089                 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1090                 gline->gl_host ? "@" : "",
1091                 gline->gl_host ? gline->gl_host : "",
1092                 gline->gl_expire - TStime(), gline->gl_lastmod,
1093                 gline->gl_lifetime, gline->gl_reason);
1094
1095   return 0;
1096 }
1097
1098 /** Display one or all G-lines to a user.
1099  * If \a userhost is not NULL, only send the first matching G-line.
1100  * Otherwise send the whole list.
1101  * @param[in] sptr User asking for G-line list.
1102  * @param[in] userhost G-line mask to search for (or NULL).
1103  * @return Zero.
1104  */
1105 int
1106 gline_list(struct Client *sptr, char *userhost)
1107 {
1108   struct Gline *gline;
1109   struct Gline *sgline;
1110
1111   if (userhost) {
1112     if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
1113       return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
1114
1115     /* send gline information along */
1116     send_reply(sptr, RPL_GLIST, gline->gl_user,
1117                gline->gl_host ? "@" : "",
1118                gline->gl_host ? gline->gl_host : "",
1119                gline->gl_expire, gline->gl_lastmod,
1120                gline->gl_lifetime,
1121                GlineIsLocal(gline) ? cli_name(&me) : "*",
1122                gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1123                (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1124                GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1125   } else {
1126     gliter(GlobalGlineList, gline, sgline) {
1127       send_reply(sptr, RPL_GLIST, gline->gl_user,
1128                  gline->gl_host ? "@" : "",
1129                  gline->gl_host ? gline->gl_host : "",
1130                  gline->gl_expire, gline->gl_lastmod,
1131                  gline->gl_lifetime,
1132                  GlineIsLocal(gline) ? cli_name(&me) : "*",
1133                  gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1134                  (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1135                  GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1136     }
1137
1138     gliter(BadChanGlineList, gline, sgline) {
1139       send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
1140                  gline->gl_expire, gline->gl_lastmod,
1141                  gline->gl_lifetime,
1142                  GlineIsLocal(gline) ? cli_name(&me) : "*",
1143                  gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1144                  (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1145                  GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1146     }
1147   }
1148
1149   /* end of gline information */
1150   return send_reply(sptr, RPL_ENDOFGLIST);
1151 }
1152
1153 /** Statistics callback to list G-lines.
1154  * @param[in] sptr Client requesting statistics.
1155  * @param[in] sd Stats descriptor for request (ignored).
1156  * @param[in] param Extra parameter from user (ignored).
1157  */
1158 void
1159 gline_stats(struct Client *sptr, const struct StatDesc *sd,
1160             char *param)
1161 {
1162   struct Gline *gline;
1163   struct Gline *sgline;
1164
1165   gliter(GlobalGlineList, gline, sgline) {
1166     send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user,
1167                gline->gl_host ? "@" : "",
1168                gline->gl_host ? gline->gl_host : "",
1169                gline->gl_expire, gline->gl_lastmod,
1170                gline->gl_lifetime,
1171                gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1172                (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1173                GlineIsRemActive(gline) ? '+' : '-',
1174                gline->gl_reason);
1175   }
1176 }
1177
1178 /** Calculate memory used by G-lines.
1179  * @param[out] gl_size Number of bytes used by G-lines.
1180  * @return Number of G-lines in use.
1181  */
1182 int
1183 gline_memory_count(size_t *gl_size)
1184 {
1185   struct Gline *gline;
1186   unsigned int gl = 0;
1187
1188   for (gline = GlobalGlineList; gline; gline = gline->gl_next) {
1189     gl++;
1190     *gl_size += sizeof(struct Gline);
1191     *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1192     *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1193     *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;
1194   }
1195
1196   for (gline = BadChanGlineList; gline; gline = gline->gl_next) {
1197     gl++;
1198     *gl_size += sizeof(struct Gline);
1199     *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1200     *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1201     *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;
1202   }
1203
1204   return gl;
1205 }