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