Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / gline.c
1 /*
2  * IRC - Internet Relay Chat, ircd/gline.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Finland
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 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 <= CurrentTime)               \
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 > CurrentTime) ||        \
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 - CurrentTime, 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  * @return Count of matching users.
353  */
354 static int
355 count_users(char *mask)
356 {
357   struct Client *acptr;
358   int count = 0;
359   char namebuf[USERLEN + HOSTLEN + 2];
360   char ipbuf[USERLEN + SOCKIPLEN + 2];
361
362   for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
363     if (!IsUser(acptr))
364       continue;
365
366     ircd_snprintf(0, namebuf, sizeof(namebuf), "%s@%s",
367                   cli_user(acptr)->username, cli_user(acptr)->host);
368     ircd_snprintf(0, ipbuf, sizeof(ipbuf), "%s@%s", cli_user(acptr)->username,
369                   ircd_ntoa(&cli_ip(acptr)));
370
371     if (!match(mask, namebuf) || !match(mask, ipbuf))
372       count++;
373   }
374
375   return count;
376 }
377
378 /** Count number of users with a realname matching \a mask.
379  * @param[in] mask Wildcard mask to match against realnames.
380  * @return Count of matching users.
381  */
382 static int
383 count_realnames(const char *mask)
384 {
385   struct Client *acptr;
386   int minlen;
387   int count;
388   char cmask[BUFSIZE];
389
390   count = 0;
391   matchcomp(cmask, &minlen, NULL, mask);
392   for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
393     if (!IsUser(acptr))
394       continue;
395     if (strlen(cli_info(acptr)) < minlen)
396       continue;
397     if (!matchexec(cli_info(acptr), cmask, minlen))
398       count++;
399   }
400   return count;
401 }
402
403 /** Create a new G-line and add it to global lists.
404  * \a userhost may be in one of four forms:
405  * \li A channel name, to add a BadChan.
406  * \li A string starting with $R and followed by a mask to match against their realname.
407  * \li A user\@IP mask (user\@ part optional) to create an IP-based ban.
408  * \li A user\@host mask (user\@ part optional) to create a hostname ban.
409  *
410  * @param[in] cptr Client that sent us the G-line.
411  * @param[in] sptr Client that originated the G-line.
412  * @param[in] userhost Text mask for the G-line.
413  * @param[in] reason Reason for G-line.
414  * @param[in] expire Expiration time of G-line.
415  * @param[in] lastmod Last modification time of G-line.
416  * @param[in] lifetime Lifetime of G-line.
417  * @param[in] flags Bitwise combination of GLINE_* flags.
418  * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
419  */
420 int
421 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
422           char *reason, time_t expire, time_t lastmod, time_t lifetime,
423           unsigned int flags)
424 {
425   struct Gline *agline;
426   char uhmask[USERLEN + HOSTLEN + 2];
427   char *user, *host;
428   int tmp;
429
430   assert(0 != userhost);
431   assert(0 != reason);
432
433   Debug((DEBUG_DEBUG, "gline_add(\"%s\", \"%s\", \"%s\", \"%s\", %Tu, %Tu "
434          "%Tu, 0x%04x)", cli_name(cptr), cli_name(sptr), userhost, reason,
435          expire, lastmod, lifetime, flags));
436
437   if (*userhost == '#' || *userhost == '&') {
438     if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
439       return send_reply(sptr, ERR_NOPRIVILEGES);
440
441     flags |= GLINE_BADCHAN;
442     user = userhost;
443     host = NULL;
444   } else if (*userhost == '$') {
445     switch (userhost[1]) {
446       case 'R': flags |= GLINE_REALNAME; break;
447       default:
448         /* uh, what to do here? */
449         /* The answer, my dear Watson, is we throw a protocol_violation()
450            -- hikari */
451         if (IsServer(cptr))
452           return protocol_violation(sptr,"%s has been smoking the sweet leaf and sent me a whacky gline",cli_name(sptr));
453         sendto_opmask_butone(NULL, SNO_GLINE, "%s has been smoking the sweet leaf and sent me a whacky gline", cli_name(sptr));
454         return 0;
455     }
456     user = userhost;
457     host = NULL;
458     if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
459       tmp = count_realnames(userhost + 2);
460       if ((tmp >= feature_int(FEAT_GLINEMAXUSERCOUNT))
461           && !(flags & GLINE_OPERFORCE))
462         return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
463     }
464   } else {
465     canon_userhost(userhost, &user, &host, "*");
466     if (sizeof(uhmask) <
467         ircd_snprintf(0, uhmask, sizeof(uhmask), "%s@%s", user, host))
468       return send_reply(sptr, ERR_LONGMASK);
469     else if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
470       switch (gline_checkmask(host)) {
471       case CHECK_OVERRIDABLE: /* oper overrided restriction */
472         if (flags & GLINE_OPERFORCE)
473           break;
474         /*FALLTHROUGH*/
475       case CHECK_REJECTED:
476         return send_reply(sptr, ERR_MASKTOOWIDE, uhmask);
477         break;
478       }
479
480       if ((tmp = count_users(uhmask)) >=
481           feature_int(FEAT_GLINEMAXUSERCOUNT) && !(flags & GLINE_OPERFORCE))
482         return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
483     }
484   }
485
486   /*
487    * You cannot set a negative (or zero) expire time, nor can you set an
488    * expiration time for greater than GLINE_MAX_EXPIRE.
489    */
490   if (!(flags & GLINE_FORCE) &&
491       (expire <= CurrentTime || expire > CurrentTime + GLINE_MAX_EXPIRE)) {
492     if (!IsServer(sptr) && MyConnect(sptr))
493       send_reply(sptr, ERR_BADEXPIRE, expire);
494     return 0;
495   }
496
497   if (!lifetime) /* no lifetime set, use expiration time */
498     lifetime = expire;
499
500   /* lifetime is already an absolute timestamp */
501
502   /* Inform ops... */
503   sendto_opmask_butone(0, ircd_strncmp(reason, "AUTO", 4) ? SNO_GLINE :
504                        SNO_AUTO, "%s adding %s %s for %s%s%s, expiring at "
505                        "%Tu: %s",
506                        (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
507                          cli_name(sptr) :
508                          cli_name((cli_user(sptr))->server),
509                        (flags & GLINE_LOCAL) ? "local" : "global",
510                        (flags & GLINE_BADCHAN) ? "BADCHAN" : "GLINE", user,
511                        (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : "@",
512                        (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : host,
513                        expire + TSoffset, reason);
514
515   /* and log it */
516   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
517             "%#C adding %s %s for %s%s%s, expiring at %Tu: %s", sptr,
518             flags & GLINE_LOCAL ? "local" : "global",
519             flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", user,
520             flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : "@",
521             flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : host,
522             expire + TSoffset, reason);
523
524   /* make the gline */
525   agline = make_gline(user, host, reason, expire, lastmod, lifetime, flags);
526
527   /* since we've disabled overlapped G-line checking, agline should
528    * never be NULL...
529    */
530   assert(agline);
531
532   gline_propagate(cptr, sptr, agline);
533
534   return do_gline(cptr, sptr, agline); /* knock off users if necessary */
535 }
536
537 /** Activate a currently inactive G-line.
538  * @param[in] cptr Peer that told us to activate the G-line.
539  * @param[in] sptr Client that originally thought it was a good idea.
540  * @param[in] gline G-line to activate.
541  * @param[in] lastmod New value for last modification timestamp.
542  * @param[in] flags 0 if the activation should be propagated, GLINE_LOCAL if not.
543  * @return Zero, unless \a sptr had a death wish (in which case CPTR_KILLED).
544  */
545 int
546 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
547                time_t lastmod, unsigned int flags)
548 {
549   unsigned int saveflags = 0;
550
551   assert(0 != gline);
552
553   saveflags = gline->gl_flags;
554
555   if (flags & GLINE_LOCAL)
556     gline->gl_flags &= ~GLINE_LDEACT;
557   else {
558     gline->gl_flags |= GLINE_ACTIVE;
559
560     if (gline->gl_lastmod) {
561       if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
562         gline->gl_lastmod++;
563       else
564         gline->gl_lastmod = lastmod;
565     }
566   }
567
568   if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
569     return 0; /* was active to begin with */
570
571   /* Inform ops and log it */
572   sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
573                        "expiring at %Tu: %s",
574                        (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
575                          cli_name(sptr) :
576                          cli_name((cli_user(sptr))->server),
577                        GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
578                        gline->gl_user, gline->gl_host ? "@" : "",
579                        gline->gl_host ? gline->gl_host : "",
580                        gline->gl_expire + TSoffset, gline->gl_reason);
581   
582   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
583             "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
584             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
585             gline->gl_host ? "@" : "",
586             gline->gl_host ? gline->gl_host : "",
587             gline->gl_expire + TSoffset, gline->gl_reason);
588
589   if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
590     gline_propagate(cptr, sptr, gline);
591
592   return do_gline(cptr, sptr, gline);
593 }
594
595 /** Deactivate a G-line.
596  * @param[in] cptr Peer that gave us the message.
597  * @param[in] sptr Client that initiated the deactivation.
598  * @param[in] gline G-line to deactivate.
599  * @param[in] lastmod New value for G-line last modification timestamp.
600  * @param[in] flags GLINE_LOCAL to only deactivate locally, 0 to propagate.
601  * @return Zero.
602  */
603 int
604 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
605                  time_t lastmod, unsigned int flags)
606 {
607   unsigned int saveflags = 0;
608   char *msg;
609
610   assert(0 != gline);
611
612   saveflags = gline->gl_flags;
613
614   if (GlineIsLocal(gline))
615     msg = "removing local";
616   else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
617     msg = "removing global";
618     gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
619   } else {
620     msg = "deactivating global";
621
622     if (flags & GLINE_LOCAL)
623       gline->gl_flags |= GLINE_LDEACT;
624     else {
625       gline->gl_flags &= ~GLINE_ACTIVE;
626
627       if (gline->gl_lastmod) {
628         if (gline->gl_lastmod >= lastmod)
629           gline->gl_lastmod++;
630         else
631           gline->gl_lastmod = lastmod;
632       }
633     }
634
635     if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
636       return 0; /* was inactive to begin with */
637   }
638
639   /* Inform ops and log it */
640   sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
641                        "%s",
642                        (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
643                          cli_name(sptr) :
644                          cli_name((cli_user(sptr))->server),
645                        msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
646                        gline->gl_user, gline->gl_host ? "@" : "",
647                        gline->gl_host ? gline->gl_host : "",
648                        gline->gl_expire + TSoffset, gline->gl_reason);
649
650   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
651             "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
652             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
653             gline->gl_host ? "@" : "",
654             gline->gl_host ? gline->gl_host : "",
655             gline->gl_expire + TSoffset, gline->gl_reason);
656
657   if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
658     gline_propagate(cptr, sptr, gline);
659
660   /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
661   if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
662     gline_free(gline); /* get rid of it */
663
664   return 0;
665 }
666
667 /** Modify a global G-line.
668  * @param[in] cptr Client that sent us the G-line modification.
669  * @param[in] sptr Client that originated the G-line modification.
670  * @param[in] gline G-line being modified.
671  * @param[in] action Resultant status of the G-line.
672  * @param[in] reason Reason for G-line.
673  * @param[in] expire Expiration time of G-line.
674  * @param[in] lastmod Last modification time of G-line.
675  * @param[in] lifetime Lifetime of G-line.
676  * @param[in] flags Bitwise combination of GLINE_* flags.
677  * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
678  */
679 int
680 gline_modify(struct Client *cptr, struct Client *sptr, struct Gline *gline,
681              enum GlineAction action, char *reason, time_t expire,
682              time_t lastmod, time_t lifetime, unsigned int flags)
683 {
684   char buf[BUFSIZE], *op = "";
685   int pos = 0;
686
687   assert(gline);
688   assert(!GlineIsLocal(gline));
689
690   Debug((DEBUG_DEBUG,  "gline_modify(\"%s\", \"%s\", \"%s%s%s\", %s, \"%s\", "
691          "%Tu, %Tu, %Tu, 0x%04x)", cli_name(cptr), cli_name(sptr),
692          gline->gl_user, gline->gl_host ? "@" : "",
693          gline->gl_host ? gline->gl_host : "",
694          action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
695          (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
696           (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
697            (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
698             (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>")))),
699          reason, expire, lastmod, lifetime, flags));
700
701   /* First, let's check lastmod... */
702   if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE) {
703     if (GlineLastMod(gline) > lastmod) { /* we have a more recent version */
704       if (IsBurstOrBurstAck(cptr))
705         return 0; /* middle of a burst, it'll resync on its own */
706       return gline_resend(cptr, gline); /* resync the server */
707     } else if (GlineLastMod(gline) == lastmod)
708       return 0; /* we have that version of the G-line... */
709   }
710
711   /* All right, we know that there's a change of some sort.  What is it? */
712   /* first, check out the expiration time... */
713   if ((flags & GLINE_EXPIRE) && expire) {
714     if (!(flags & GLINE_FORCE) &&
715         (expire <= CurrentTime || expire > CurrentTime + GLINE_MAX_EXPIRE)) {
716       if (!IsServer(sptr) && MyConnect(sptr)) /* bad expiration time */
717         send_reply(sptr, ERR_BADEXPIRE, expire);
718       return 0;
719     }
720   } else
721     flags &= ~GLINE_EXPIRE;
722
723   /* Now check to see if there's any change... */
724   if ((flags & GLINE_EXPIRE) && expire == gline->gl_expire) {
725     flags &= ~GLINE_EXPIRE; /* no change to expiration time... */
726     expire = 0;
727   }
728
729   /* Next, check out lifetime--this one's a bit trickier... */
730   if (!(flags & GLINE_LIFETIME) || !lifetime)
731     lifetime = gline->gl_lifetime; /* use G-line lifetime */
732
733   lifetime = IRCD_MAX(lifetime, expire); /* set lifetime to the max */
734
735   /* OK, let's see which is greater... */
736   if (lifetime > gline->gl_lifetime)
737     flags |= GLINE_LIFETIME; /* have to update lifetime */
738   else {
739     flags &= ~GLINE_LIFETIME; /* no change to lifetime */
740     lifetime = 0;
741   }
742
743   /* Finally, let's see if the reason needs to be updated */
744   if ((flags & GLINE_REASON) && reason &&
745       !ircd_strcmp(gline->gl_reason, reason))
746     flags &= ~GLINE_REASON; /* no changes to the reason */
747
748   /* OK, now let's take a look at the action... */
749   if ((action == GLINE_ACTIVATE && (gline->gl_flags & GLINE_ACTIVE)) ||
750       (action == GLINE_DEACTIVATE && !(gline->gl_flags & GLINE_ACTIVE)) ||
751       (action == GLINE_LOCAL_ACTIVATE &&
752        (gline->gl_state == GLOCAL_ACTIVATED)) ||
753       (action == GLINE_LOCAL_DEACTIVATE &&
754        (gline->gl_state == GLOCAL_DEACTIVATED)) ||
755       /* can't activate an expired G-line */
756       IRCD_MAX(gline->gl_expire, expire) <= CurrentTime)
757     action = GLINE_MODIFY; /* no activity state modifications */
758
759   Debug((DEBUG_DEBUG,  "About to perform changes; flags 0x%04x, action %s",
760          flags, action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
761          (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
762           (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
763            (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
764             (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>"))))));
765
766   /* If there are no changes to perform, do no changes */
767   if (!(flags & GLINE_UPDATE) && action == GLINE_MODIFY)
768     return 0;
769
770   /* Now we know what needs to be changed, so let's process the changes... */
771
772   /* Start by updating lastmod, if indicated... */
773   if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
774     gline->gl_lastmod = lastmod;
775
776   /* Then move on to activity status changes... */
777   switch (action) {
778   case GLINE_ACTIVATE: /* Globally activating G-line */
779     gline->gl_flags |= GLINE_ACTIVE; /* make it active... */
780     gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
781     pos += ircd_snprintf(0, buf, sizeof(buf), " globally activating G-line");
782     op = "+"; /* operation for G-line propagation */
783     break;
784
785   case GLINE_DEACTIVATE: /* Globally deactivating G-line */
786     gline->gl_flags &= ~GLINE_ACTIVE; /* make it inactive... */
787     gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
788     pos += ircd_snprintf(0, buf, sizeof(buf), " globally deactivating G-line");
789     op = "-"; /* operation for G-line propagation */
790     break;
791
792   case GLINE_LOCAL_ACTIVATE: /* Locally activating G-line */
793     gline->gl_state = GLOCAL_ACTIVATED; /* make it locally active */
794     pos += ircd_snprintf(0, buf, sizeof(buf), " locally activating G-line");
795     break;
796
797   case GLINE_LOCAL_DEACTIVATE: /* Locally deactivating G-line */
798     gline->gl_state = GLOCAL_DEACTIVATED; /* make it locally inactive */
799     pos += ircd_snprintf(0, buf, sizeof(buf), " locally deactivating G-line");
800     break;
801
802   case GLINE_MODIFY: /* no change to activity status */
803     break;
804   }
805
806   /* Handle expiration changes... */
807   if (flags & GLINE_EXPIRE) {
808     gline->gl_expire = expire; /* save new expiration time */
809     if (pos < BUFSIZE)
810       pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
811                            "%s%s changing expiration time to %Tu",
812                            pos ? ";" : "",
813                            pos && !(flags & (GLINE_LIFETIME | GLINE_REASON)) ?
814                            " and" : "", expire);
815   }
816
817   /* Next, handle lifetime changes... */
818   if (flags & GLINE_LIFETIME) {
819     gline->gl_lifetime = lifetime; /* save new lifetime */
820     if (pos < BUFSIZE)
821       pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
822                            "%s%s extending record lifetime to %Tu",
823                            pos ? ";" : "", pos && !(flags & GLINE_REASON) ?
824                            " and" : "", lifetime);
825   }
826
827   /* Now, handle reason changes... */
828   if (flags & GLINE_REASON) {
829     MyFree(gline->gl_reason); /* release old reason */
830     DupString(gline->gl_reason, reason); /* store new reason */
831     if (pos < BUFSIZE)
832       pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
833                            "%s%s changing reason to \"%s\"",
834                            pos ? ";" : "", pos ? " and" : "", reason);
835   }
836
837   /* All right, inform ops... */
838   sendto_opmask_butone(0, SNO_GLINE, "%s modifying global %s for %s%s%s:%s",
839                        (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
840                        cli_name(sptr) : cli_name((cli_user(sptr))->server),
841                        GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
842                        gline->gl_user, gline->gl_host ? "@" : "",
843                        gline->gl_host ? gline->gl_host : "", buf);
844
845   /* and log the change */
846   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
847             "%#C modifying global %s for %s%s%s:%s", sptr,
848             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
849             gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "",
850             buf);
851
852   /* We'll be simple for this release, but we can update this to change
853    * the propagation syntax on future updates
854    */
855   if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
856     sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
857                           "* %s%s%s%s%s %Tu %Tu %Tu :%s",
858                           flags & GLINE_OPERFORCE ? "!" : "", op,
859                           gline->gl_user, gline->gl_host ? "@" : "",
860                           gline->gl_host ? gline->gl_host : "",
861                           gline->gl_expire - CurrentTime, gline->gl_lastmod,
862                           gline->gl_lifetime, gline->gl_reason);
863
864   /* OK, let's do the G-line... */
865   return do_gline(cptr, sptr, gline);
866 }
867
868 /** Destroy a local G-line.
869  * @param[in] cptr Peer that gave us the message.
870  * @param[in] sptr Client that initiated the destruction.
871  * @param[in] gline G-line to destroy.
872  * @return Zero.
873  */
874 int
875 gline_destroy(struct Client *cptr, struct Client *sptr, struct Gline *gline)
876 {
877   assert(gline);
878   assert(GlineIsLocal(gline));
879
880   /* Inform ops and log it */
881   sendto_opmask_butone(0, SNO_GLINE, "%s removing local %s for %s%s%s",
882                        (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
883                        cli_name(sptr) : cli_name((cli_user(sptr))->server),
884                        GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
885                        gline->gl_user, gline->gl_host ? "@" : "",
886                        gline->gl_host ? gline->gl_host : "");
887   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
888             "%#C removing local %s for %s%s%s", sptr,
889             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
890             gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "");
891
892   gline_free(gline); /* get rid of the G-line */
893
894   return 0; /* convenience return */
895 }
896
897 /** Find a G-line for a particular mask, guided by certain flags.
898  * Certain bits in \a flags are interpreted specially:
899  * <dl>
900  * <dt>GLINE_ANY</dt><dd>Search both BadChans and user G-lines.</dd>
901  * <dt>GLINE_BADCHAN</dt><dd>Search BadChans.</dd>
902  * <dt>GLINE_GLOBAL</dt><dd>Only match global G-lines.</dd>
903  * <dt>GLINE_LOCAL</dt><dd>Only match local G-lines.</dd>
904  * <dt>GLINE_LASTMOD</dt><dd>Only match G-lines with a last modification time.</dd>
905  * <dt>GLINE_EXACT</dt><dd>Require an exact match of G-line mask.</dd>
906  * <dt>anything else</dt><dd>Search user G-lines.</dd>
907  * </dl>
908  * @param[in] userhost Mask to search for.
909  * @param[in] flags Bitwise combination of GLINE_* flags.
910  * @return First matching G-line, or NULL if none are found.
911  */
912 struct Gline *
913 gline_find(char *userhost, unsigned int flags)
914 {
915   struct Gline *gline = 0;
916   struct Gline *sgline;
917   char *user, *host, *t_uh;
918
919   if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
920     gliter(BadChanGlineList, gline, sgline) {
921       if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
922           (flags & GLINE_LOCAL && gline->gl_flags & GLINE_GLOBAL) ||
923           (flags & GLINE_LASTMOD && !gline->gl_lastmod))
924         continue;
925       else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
926                 match(gline->gl_user, userhost)) == 0)
927         return gline;
928     }
929   }
930
931   if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
932       *userhost == '#' || *userhost == '&')
933     return 0;
934
935   DupString(t_uh, userhost);
936   canon_userhost(t_uh, &user, &host, "*");
937
938   gliter(GlobalGlineList, gline, sgline) {
939     if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
940         (flags & GLINE_LOCAL && gline->gl_flags & GLINE_GLOBAL) ||
941         (flags & GLINE_LASTMOD && !gline->gl_lastmod))
942       continue;
943     else if (flags & GLINE_EXACT) {
944       if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
945            || (!gline->gl_host && !host)) &&
946           (ircd_strcmp(gline->gl_user, user) == 0))
947         break;
948     } else {
949       if (((gline->gl_host && host && match(gline->gl_host, host) == 0)
950            || (!gline->gl_host && !host)) &&
951           (match(gline->gl_user, user) == 0))
952         break;
953     }
954   }
955
956   MyFree(t_uh);
957
958   return gline;
959 }
960
961 /** Find a matching G-line for a user.
962  * @param[in] cptr Client to compare against.
963  * @param[in] flags Bitwise combination of GLINE_GLOBAL and/or
964  * GLINE_LASTMOD to limit matches.
965  * @return Matching G-line, or NULL if none are found.
966  */
967 struct Gline *
968 gline_lookup(struct Client *cptr, unsigned int flags)
969 {
970   struct Gline *gline;
971   struct Gline *sgline;
972
973   gliter(GlobalGlineList, gline, sgline) {
974     if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
975         (flags & GLINE_LASTMOD && !gline->gl_lastmod))
976       continue;
977
978     if (GlineIsRealName(gline)) {
979       Debug((DEBUG_DEBUG,"realname gline: '%s' '%s'",gline->gl_user,cli_info(cptr)));
980       if (match(gline->gl_user+2, cli_info(cptr)) != 0)
981         continue;
982     }
983     else {
984       if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
985         continue;
986
987       if (GlineIsIpMask(gline)) {
988         if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
989           continue;
990       }
991       else {
992         if (match(gline->gl_host, (cli_user(cptr))->realhost) != 0)
993           continue;
994       }
995     }
996     if (GlineIsActive(gline))
997       return gline;
998   }
999   /*
1000    * No Glines matched
1001    */
1002   return 0;
1003 }
1004
1005 /** Delink and free a G-line.
1006  * @param[in] gline G-line to free.
1007  */
1008 void
1009 gline_free(struct Gline *gline)
1010 {
1011   assert(0 != gline);
1012
1013   *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
1014   if (gline->gl_next)
1015     gline->gl_next->gl_prev_p = gline->gl_prev_p;
1016
1017   MyFree(gline->gl_user); /* free up the memory */
1018   if (gline->gl_host)
1019     MyFree(gline->gl_host);
1020   MyFree(gline->gl_reason);
1021   MyFree(gline);
1022 }
1023
1024 /** Burst all known global G-lines to another server.
1025  * @param[in] cptr Destination of burst.
1026  */
1027 void
1028 gline_burst(struct Client *cptr)
1029 {
1030   struct Gline *gline;
1031   struct Gline *sgline;
1032
1033   gliter(GlobalGlineList, gline, sgline) {
1034     if (!GlineIsLocal(gline) && gline->gl_lastmod)
1035       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1036                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1037                     gline->gl_host ? "@" : "",
1038                     gline->gl_host ? gline->gl_host : "",
1039                     gline->gl_expire - CurrentTime, gline->gl_lastmod,
1040                     gline->gl_lifetime, gline->gl_reason);
1041   }
1042
1043   gliter(BadChanGlineList, gline, sgline) {
1044     if (!GlineIsLocal(gline) && gline->gl_lastmod)
1045       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu %Tu :%s",
1046                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1047                     gline->gl_expire - CurrentTime, gline->gl_lastmod,
1048                     gline->gl_lifetime, gline->gl_reason);
1049   }
1050 }
1051
1052 /** Send a G-line to another server.
1053  * @param[in] cptr Who to inform of the G-line.
1054  * @param[in] gline G-line to send.
1055  * @return Zero.
1056  */
1057 int
1058 gline_resend(struct Client *cptr, struct Gline *gline)
1059 {
1060   if (GlineIsLocal(gline) || !gline->gl_lastmod)
1061     return 0;
1062
1063   sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1064                 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1065                 gline->gl_host ? "@" : "",
1066                 gline->gl_host ? gline->gl_host : "",
1067                 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1068                 gline->gl_lifetime, gline->gl_reason);
1069
1070   return 0;
1071 }
1072
1073 /** Display one or all G-lines to a user.
1074  * If \a userhost is not NULL, only send the first matching G-line.
1075  * Otherwise send the whole list.
1076  * @param[in] sptr User asking for G-line list.
1077  * @param[in] userhost G-line mask to search for (or NULL).
1078  * @return Zero.
1079  */
1080 int
1081 gline_list(struct Client *sptr, char *userhost)
1082 {
1083   struct Gline *gline;
1084   struct Gline *sgline;
1085
1086   if (userhost) {
1087     if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
1088       return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
1089
1090     /* send gline information along */
1091     send_reply(sptr, RPL_GLIST, gline->gl_user,
1092                gline->gl_host ? "@" : "",
1093                gline->gl_host ? gline->gl_host : "",
1094                gline->gl_expire + TSoffset, gline->gl_lastmod,
1095                gline->gl_lifetime + TSoffset,
1096                GlineIsLocal(gline) ? cli_name(&me) : "*",
1097                gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1098                (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1099                GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1100   } else {
1101     gliter(GlobalGlineList, gline, sgline) {
1102       send_reply(sptr, RPL_GLIST, gline->gl_user,
1103                  gline->gl_host ? "@" : "",
1104                  gline->gl_host ? gline->gl_host : "",
1105                  gline->gl_expire + TSoffset, gline->gl_lastmod,
1106                  gline->gl_lifetime + TSoffset,
1107                  GlineIsLocal(gline) ? cli_name(&me) : "*",
1108                  gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1109                  (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1110                  GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1111     }
1112
1113     gliter(BadChanGlineList, gline, sgline) {
1114       send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
1115                  gline->gl_expire + TSoffset, gline->gl_lastmod,
1116                  gline->gl_lifetime + TSoffset,
1117                  GlineIsLocal(gline) ? cli_name(&me) : "*",
1118                  gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1119                  (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1120                  GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1121     }
1122   }
1123
1124   /* end of gline information */
1125   return send_reply(sptr, RPL_ENDOFGLIST);
1126 }
1127
1128 /** Statistics callback to list G-lines.
1129  * @param[in] sptr Client requesting statistics.
1130  * @param[in] sd Stats descriptor for request (ignored).
1131  * @param[in] param Extra parameter from user (ignored).
1132  */
1133 void
1134 gline_stats(struct Client *sptr, const struct StatDesc *sd,
1135             char *param)
1136 {
1137   struct Gline *gline;
1138   struct Gline *sgline;
1139
1140   gliter(GlobalGlineList, gline, sgline) {
1141     send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user,
1142                gline->gl_host ? "@" : "",
1143                gline->gl_host ? gline->gl_host : "",
1144                gline->gl_expire + TSoffset, gline->gl_lastmod,
1145                gline->gl_lifetime + TSoffset,
1146                gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1147                (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1148                GlineIsRemActive(gline) ? '+' : '-',
1149                gline->gl_reason);
1150   }
1151 }
1152
1153 /** Calculate memory used by G-lines.
1154  * @param[out] gl_size Number of bytes used by G-lines.
1155  * @return Number of G-lines in use.
1156  */
1157 int
1158 gline_memory_count(size_t *gl_size)
1159 {
1160   struct Gline *gline;
1161   unsigned int gl = 0;
1162
1163   for (gline = GlobalGlineList; gline; gline = gline->gl_next) {
1164     gl++;
1165     *gl_size += sizeof(struct Gline);
1166     *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1167     *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1168     *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;
1169   }
1170
1171   for (gline = BadChanGlineList; gline; gline = gline->gl_next) {
1172     gl++;
1173     *gl_size += sizeof(struct Gline);
1174     *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1175     *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1176     *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;
1177   }
1178
1179   return gl;
1180 }