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