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