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