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