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