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