Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / gline.c
1 /*
2  * IRC - Internet Relay Chat, ircd/gline.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Finland
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 /** @file
21  * @brief Implementation of Gline manipulation functions.
22  * @version $Id$
23  */
24 #include "config.h"
25
26 #include "gline.h"
27 #include "client.h"
28 #include "ircd.h"
29 #include "ircd_alloc.h"
30 #include "ircd_features.h"
31 #include "ircd_log.h"
32 #include "ircd_reply.h"
33 #include "ircd_snprintf.h"
34 #include "ircd_string.h"
35 #include "match.h"
36 #include "numeric.h"
37 #include "s_bsd.h"
38 #include "s_debug.h"
39 #include "s_misc.h"
40 #include "s_stats.h"
41 #include "send.h"
42 #include "struct.h"
43 #include "msg.h"
44 #include "numnicks.h"
45 #include "numeric.h"
46 #include "whocmds.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 #define gliter(list, gl, next)                          \
80   /* Iterate through the G-lines in the list */         \
81   for ((gl) = (list); (gl); (gl) = (next))              \
82     /* Figure out the next pointer in list... */        \
83     if ((((next) = (gl)->gl_next) || 1) &&              \
84         /* Then see if it's expired */                  \
85         (gl)->gl_rexpire <= CurrentTime)                \
86       /* Record has expired, so free the G-line */      \
87       gline_free((gl));                                 \
88     /* See if we need to expire the G-line */           \
89     else if (((gl)->gl_expire > CurrentTime ||          \
90               ((gl)->gl_flags &= ~GLINE_ACTIVE)) && 0)  \
91       ; /* empty statement */                           \
92     else
93
94 /** Find canonical user and host for a string.
95  * If \a userhost starts with '$', assign \a userhost to *user_p and NULL to *host_p.
96  * Otherwise, if \a userhost contains '@', assign the earlier part of it to *user_p and the rest to *host_p.
97  * Otherwise, assign \a def_user to *user_p and \a userhost to *host_p.
98  *
99  * @param[in] userhost Input string from user.
100  * @param[out] user_p Gets pointer to user (or channel/realname) part of hostmask.
101  * @param[out] host_p Gets point to host part of hostmask (may be assigned NULL).
102  * @param[in] def_user Default value for user part.
103  */
104 static void
105 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
106 {
107   char *tmp;
108
109   if (*userhost == '$') {
110     *user_p = userhost;
111     *host_p = NULL;
112     return;
113   }
114
115   if (!(tmp = strchr(userhost, '@'))) {
116     *user_p = def_user;
117     *host_p = userhost;
118   } else {
119     *user_p = userhost;
120     *(tmp++) = '\0';
121     *host_p = tmp;
122   }
123 }
124
125 /** Create a Gline structure.
126  * @param[in] user User part of mask.
127  * @param[in] host Host part of mask (NULL if not applicable).
128  * @param[in] reason Reason for G-line.
129  * @param[in] expire Expiration timestamp.
130  * @param[in] lastmod Last modification timestamp.
131  * @param[in] flags Bitwise combination of GLINE_* bits.
132  * @return Newly allocated G-line.
133  */
134 static struct Gline *
135 make_gline(char *user, char *host, char *reason, time_t expire, time_t lastmod,
136            unsigned int flags)
137 {
138   struct Gline *gline, *sgline, *after = 0;
139
140   if (!(flags & GLINE_BADCHAN)) { /* search for overlapping glines first */
141
142     for (gline = GlobalGlineList; gline; gline = sgline) {
143       sgline = gline->gl_next;
144
145       if (gline->gl_expire <= CurrentTime)
146         gline_free(gline);
147       else if (((gline->gl_flags & GLINE_LOCAL) != (flags & GLINE_LOCAL)) ||
148                (gline->gl_host && !host) || (!gline->gl_host && host))
149         continue;
150       else if (!mmatch(gline->gl_user, user) /* gline contains new mask */
151                && (gline->gl_host == NULL || !mmatch(gline->gl_host, host))) {
152         if (expire <= gline->gl_expire) /* will expire before wider gline */
153           return 0;
154         else
155           after = gline; /* stick new gline after this one */
156       } else if (!mmatch(user, gline->gl_user) /* new mask contains gline */
157                  && (gline->gl_host==NULL || !mmatch(host, gline->gl_host)) 
158                  && gline->gl_expire <= expire) /* old expires before new */
159         gline_free(gline); /* save some memory */
160     }
161   }
162
163   gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
164   assert(0 != gline);
165
166   DupString(gline->gl_reason, reason); /* initialize gline... */
167   gline->gl_expire = expire;
168   gline->gl_rexpire = expire;
169   gline->gl_lastmod = lastmod;
170   gline->gl_flags = flags & GLINE_MASK;
171
172   if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
173     DupString(gline->gl_user, user); /* first, remember channel */
174     gline->gl_host = 0;
175
176     gline->gl_next = BadChanGlineList; /* then link it into list */
177     gline->gl_prev_p = &BadChanGlineList;
178     if (BadChanGlineList)
179       BadChanGlineList->gl_prev_p = &gline->gl_next;
180     BadChanGlineList = gline;
181   } else {
182     DupString(gline->gl_user, user); /* remember them... */
183     if (*user != '$')
184       DupString(gline->gl_host, host);
185     else
186       gline->gl_host = NULL;
187
188     if (*user != '$' && ipmask_parse(host, &gline->gl_addr, &gline->gl_bits))
189       gline->gl_flags |= GLINE_IPMASK;
190
191     if (after) {
192       gline->gl_next = after->gl_next;
193       gline->gl_prev_p = &after->gl_next;
194       if (after->gl_next)
195         after->gl_next->gl_prev_p = &gline->gl_next;
196       after->gl_next = gline;
197     } else {
198       gline->gl_next = GlobalGlineList; /* then link it into list */
199       gline->gl_prev_p = &GlobalGlineList;
200       if (GlobalGlineList)
201         GlobalGlineList->gl_prev_p = &gline->gl_next;
202       GlobalGlineList = gline;
203     }
204   }
205
206   return gline;
207 }
208
209 /** Check local clients against a new G-line.
210  * If the G-line is inactive or a badchan, return immediately.
211  * Otherwise, if any users match it, disconnect them.
212  * @param[in] cptr Peer connect that sent the G-line.
213  * @param[in] sptr Client that originated the G-line.
214  * @param[in] gline New G-line to check.
215  * @return Zero, unless \a sptr G-lined himself, in which case CPTR_KILLED.
216  */
217 static int
218 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
219 {
220   struct Client *acptr;
221   int fd, retval = 0, tval;
222
223   if (feature_bool(FEAT_DISABLE_GLINES))
224     return 0; /* G-lines are disabled */
225
226   if (GlineIsBadChan(gline)) /* no action taken on badchan glines */
227     return 0;
228   if (!GlineIsActive(gline)) /* no action taken on inactive glines */
229     return 0;
230
231   for (fd = HighestFd; fd >= 0; --fd) {
232     /*
233      * get the users!
234      */
235     if ((acptr = LocalClientArray[fd])) {
236       if (!cli_user(acptr))
237         continue;
238
239       if (GlineIsRealName(gline)) { /* Realname Gline */
240         Debug((DEBUG_DEBUG,"Realname Gline: %s %s",(cli_info(acptr)),
241                                         gline->gl_user+2));
242         if (match(gline->gl_user+2, cli_info(acptr)) != 0)
243             continue;
244         Debug((DEBUG_DEBUG,"Matched!"));
245       } else { /* Host/IP gline */
246         if (cli_user(acptr)->username &&
247             match(gline->gl_user, (cli_user(acptr))->username) != 0)
248           continue;
249
250         if (GlineIsIpMask(gline)) {
251           if (!ipmask_check(&cli_ip(acptr), &gline->gl_addr, gline->gl_bits))
252             continue;
253         }
254         else {
255           if (match(gline->gl_host, cli_sockhost(acptr)) != 0)
256             continue;
257         }
258       }
259
260       /* ok, here's one that got G-lined */
261       send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
262            gline->gl_reason);
263
264       /* let the ops know about it */
265       sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
266                            get_client_name(acptr, SHOW_IP));
267
268       /* and get rid of him */
269       if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
270           gline->gl_reason)))
271         retval = tval; /* retain killed status */
272     }
273   }
274   return retval;
275 }
276
277 /**
278  * Implements the mask checking applied to local G-lines.
279  * Basically, host masks must have a minimum of two non-wild domain
280  * fields, and IP masks must have a minimum of 16 bits.  If the mask
281  * has even one wild-card, OVERRIDABLE is returned, assuming the other
282  * check doesn't fail.
283  * @param[in] mask G-line mask to check.
284  * @return One of CHECK_REJECTED, CHECK_OVERRIDABLE, or CHECK_APPROVED.
285  */
286 static int
287 gline_checkmask(char *mask)
288 {
289   unsigned int flags = MASK_IP;
290   unsigned int dots = 0;
291   unsigned int ipmask = 0;
292
293   for (; *mask; mask++) { /* go through given mask */
294     if (*mask == '.') { /* it's a separator; advance positional wilds */
295       flags = (flags & ~MASK_WILD_MASK) | ((flags << 1) & MASK_WILD_MASK);
296       dots++;
297
298       if ((flags & (MASK_IP | MASK_WILDS)) == MASK_IP)
299         ipmask += 8; /* It's an IP with no wilds, count bits */
300     } else if (*mask == '*' || *mask == '?')
301       flags |= MASK_WILD_0 | MASK_WILDS; /* found a wildcard */
302     else if (*mask == '/') { /* n.n.n.n/n notation; parse bit specifier */
303       ++mask;
304       ipmask = strtoul(mask, &mask, 10);
305
306       /* sanity-check to date */
307       if (*mask || (flags & (MASK_WILDS | MASK_IP)) != MASK_IP)
308         return CHECK_REJECTED;
309       if (!dots) {
310         if (ipmask > 128)
311           return CHECK_REJECTED;
312         if (ipmask < 128)
313           flags |= MASK_WILDS;
314       } else {
315         if (dots != 3 || ipmask > 32)
316           return CHECK_REJECTED;
317         if (ipmask < 32)
318           flags |= MASK_WILDS;
319       }
320
321       flags |= MASK_HALT; /* Halt the ipmask calculation */
322       break; /* get out of the loop */
323     } else if (!IsIP6Char(*mask)) {
324       flags &= ~MASK_IP; /* not an IP anymore! */
325       ipmask = 0;
326     }
327   }
328
329   /* Sanity-check quads */
330   if (dots > 3 || (!(flags & MASK_WILDS) && dots < 3)) {
331     flags &= ~MASK_IP;
332     ipmask = 0;
333   }
334
335   /* update bit count if necessary */
336   if ((flags & (MASK_IP | MASK_WILDS | MASK_HALT)) == MASK_IP)
337     ipmask += 8;
338
339   /* Check to see that it's not too wide of a mask */
340   if (flags & MASK_WILDS &&
341       ((!(flags & MASK_IP) && (dots < 2 || flags & MASK_WILD_MASK)) ||
342        (flags & MASK_IP && ipmask < 16)))
343     return CHECK_REJECTED; /* to wide, reject */
344
345   /* Ok, it's approved; require override if it has wildcards, though */
346   return flags & MASK_WILDS ? CHECK_OVERRIDABLE : CHECK_APPROVED;
347 }
348
349 /** Forward a G-line to other servers.
350  * @param[in] cptr Client that sent us the G-line.
351  * @param[in] sptr Client that originated the G-line.
352  * @param[in] gline G-line to forward.
353  * @return Zero.
354  */
355 int
356 gline_propagate(struct Client *cptr, struct Client *sptr, struct Gline *gline)
357 {
358   if (GlineIsLocal(gline) || (IsUser(sptr) && !gline->gl_lastmod))
359     return 0;
360
361   if (gline->gl_lastmod)
362     sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %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_reason);
368   else
369     sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
370                           (GlineIsRemActive(gline) ?
371                            "* +%s%s%s %Tu :%s" : "* -%s%s%s"),
372                           gline->gl_user, 
373                           gline->gl_host ? "@" : "",
374                           gline->gl_host ? gline->gl_host : "",
375                           gline->gl_expire - CurrentTime, gline->gl_reason);
376
377   return 0;
378 }
379
380 /** Create a new G-line and add it to global lists.
381  * \a userhost may be in one of four forms:
382  * \li A channel name, to add a BadChan.
383  * \li A string starting with $R and followed by a mask to match against their realname.
384  * \li A user\@IP mask (user\@ part optional) to create an IP-based ban.
385  * \li A user\@host mask (user\@ part optional) to create a hostname ban.
386  *
387  * @param[in] cptr Client that sent us the G-line.
388  * @param[in] sptr Client that originated the G-line.
389  * @param[in] userhost Text mask for the G-line.
390  * @param[in] reason Reason for G-line.
391  * @param[in] expire Duration of G-line in seconds.
392  * @param[in] lastmod Last modification time of G-line.
393  * @param[in] flags Bitwise combination of GLINE_* flags.
394  * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
395  */
396 int
397 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
398           char *reason, time_t expire, time_t lastmod, unsigned int flags)
399 {
400   struct Gline *agline;
401   char uhmask[USERLEN + HOSTLEN + 2];
402   char *user, *host;
403   int tmp;
404
405   assert(0 != userhost);
406   assert(0 != reason);
407
408   if (*userhost == '#' || *userhost == '&') {
409     if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
410       return send_reply(sptr, ERR_NOPRIVILEGES);
411
412     flags |= GLINE_BADCHAN;
413     user = userhost;
414     host = 0;
415   } else if (*userhost == '$') {
416     switch (*userhost == '$' ? userhost[1] : userhost[3]) {
417       case 'R': flags |= GLINE_REALNAME; break;
418       default:
419         /* uh, what to do here? */
420         /* The answer, my dear Watson, is we throw a protocol_violation()
421            -- hikari */
422         if (IsServer(cptr))
423           return protocol_violation(sptr,"%s has been smoking the sweet leaf and sent me a whacky gline",cli_name(sptr));
424         else {
425          sendto_opmask_butone(NULL, SNO_GLINE, "%s has been smoking the sweet leaf and sent me a whacky gline", cli_name(sptr));
426          return 0;
427         }
428         break;
429     }
430      user = (*userhost =='$' ? userhost : userhost+2);
431      host = 0;
432   } else {
433     canon_userhost(userhost, &user, &host, "*");
434     if (sizeof(uhmask) <
435         ircd_snprintf(0, uhmask, sizeof(uhmask), "%s@%s", user, host))
436       return send_reply(sptr, ERR_LONGMASK);
437     else if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
438       switch (gline_checkmask(host)) {
439       case CHECK_OVERRIDABLE: /* oper overrided restriction */
440         if (flags & GLINE_OPERFORCE)
441           break;
442         /*FALLTHROUGH*/
443       case CHECK_REJECTED:
444         return send_reply(sptr, ERR_MASKTOOWIDE, uhmask);
445         break;
446       }
447
448       if ((tmp = count_users(uhmask)) >=
449           feature_int(FEAT_GLINEMAXUSERCOUNT) && !(flags & GLINE_OPERFORCE))
450         return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
451     }
452   }
453
454   /*
455    * You cannot set a negative (or zero) expire time, nor can you set an
456    * expiration time for greater than GLINE_MAX_EXPIRE.
457    */
458   if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
459     if (!IsServer(sptr) && MyConnect(sptr))
460       send_reply(sptr, ERR_BADEXPIRE, expire);
461     return 0;
462   }
463
464   expire += CurrentTime; /* convert from lifetime to timestamp */
465
466   /* Inform ops... */
467   sendto_opmask_butone(0, ircd_strncmp(reason, "AUTO", 4) ? SNO_GLINE :
468                        SNO_AUTO, "%s adding %s %s for %s%s%s, expiring at "
469                        "%Tu: %s",
470                        (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
471                          cli_name(sptr) :
472                          cli_name((cli_user(sptr))->server),
473                        (flags & GLINE_LOCAL) ? "local" : "global",
474                        (flags & GLINE_BADCHAN) ? "BADCHAN" : "GLINE", user,
475                        (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : "@",
476                        (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : host,
477                        expire + TSoffset, reason);
478
479   /* and log it */
480   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
481             "%#C adding %s %s for %s%s%s, expiring at %Tu: %s", sptr,
482             flags & GLINE_LOCAL ? "local" : "global",
483             flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", user,
484             flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : "@",
485             flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : host,
486             expire + TSoffset, reason);
487
488   /* make the gline */
489   agline = make_gline(user, host, reason, expire, lastmod, flags);
490
491   if (!agline) /* if it overlapped, silently return */
492     return 0;
493
494   gline_propagate(cptr, sptr, agline);
495
496   return do_gline(cptr, sptr, agline); /* knock off users if necessary */
497 }
498
499 /** Activate a currently inactive G-line.
500  * @param[in] cptr Peer that told us to activate the G-line.
501  * @param[in] sptr Client that originally thought it was a good idea.
502  * @param[in] gline G-line to activate.
503  * @param[in] lastmod New value for last modification timestamp.
504  * @param[in] flags 0 if the activation should be propagated, GLINE_LOCAL if not.
505  * @return Zero, unless \a sptr had a death wish (in which case CPTR_KILLED).
506  */
507 int
508 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
509                time_t lastmod, unsigned int flags)
510 {
511   unsigned int saveflags = 0;
512
513   assert(0 != gline);
514
515   saveflags = gline->gl_flags;
516
517   if (flags & GLINE_LOCAL)
518     gline->gl_flags &= ~GLINE_LDEACT;
519   else {
520     gline->gl_flags |= GLINE_ACTIVE;
521
522     if (gline->gl_lastmod) {
523       if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
524         gline->gl_lastmod++;
525       else
526         gline->gl_lastmod = lastmod;
527     }
528   }
529
530   if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
531     return 0; /* was active to begin with */
532
533   /* Inform ops and log it */
534   sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
535                        "expiring at %Tu: %s",
536                        (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
537                          cli_name(sptr) :
538                          cli_name((cli_user(sptr))->server),
539                        GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
540                        gline->gl_user, gline->gl_host ? "@" : "",
541                        gline->gl_host ? gline->gl_host : "",
542                        gline->gl_expire + TSoffset, gline->gl_reason);
543   
544   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
545             "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
546             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
547             gline->gl_host ? "@" : "",
548             gline->gl_host ? gline->gl_host : "",
549             gline->gl_expire + TSoffset, gline->gl_reason);
550
551   if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
552     gline_propagate(cptr, sptr, gline);
553
554   return do_gline(cptr, sptr, gline);
555 }
556
557 /** Deactivate a G-line.
558  * @param[in] cptr Peer that gave us the message.
559  * @param[in] sptr Client that initiated the deactivation.
560  * @param[in] gline G-line to deactivate.
561  * @param[in] lastmod New value for G-line last modification timestamp.
562  * @param[in] flags GLINE_LOCAL to only deactivate locally, 0 to propagate.
563  * @return Zero.
564  */
565 int
566 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
567                  time_t lastmod, unsigned int flags)
568 {
569   unsigned int saveflags = 0;
570   char *msg;
571
572   assert(0 != gline);
573
574   saveflags = gline->gl_flags;
575
576   if (GlineIsLocal(gline))
577     msg = "removing local";
578   else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
579     msg = "removing global";
580     gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
581   } else {
582     msg = "deactivating global";
583
584     if (flags & GLINE_LOCAL)
585       gline->gl_flags |= GLINE_LDEACT;
586     else {
587       gline->gl_flags &= ~GLINE_ACTIVE;
588
589       if (gline->gl_lastmod) {
590         if (gline->gl_lastmod >= lastmod)
591           gline->gl_lastmod++;
592         else
593           gline->gl_lastmod = lastmod;
594       }
595     }
596
597     if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
598       return 0; /* was inactive to begin with */
599   }
600
601   /* Inform ops and log it */
602   sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
603                        "%s",
604                        (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
605                          cli_name(sptr) :
606                          cli_name((cli_user(sptr))->server),
607                        msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
608                        gline->gl_user, gline->gl_host ? "@" : "",
609                        gline->gl_host ? gline->gl_host : "",
610                        gline->gl_expire + TSoffset, gline->gl_reason);
611
612   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
613             "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
614             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
615             gline->gl_host ? "@" : "",
616             gline->gl_host ? gline->gl_host : "",
617             gline->gl_expire + TSoffset, gline->gl_reason);
618
619   if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
620     gline_propagate(cptr, sptr, gline);
621
622   /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
623   if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
624     gline_free(gline); /* get rid of it */
625
626   return 0;
627 }
628
629 /** Find a G-line for a particular mask, guided by certain flags.
630  * Certain bits in \a flags are interpreted specially:
631  * <dl>
632  * <dt>GLINE_ANY</dt><dd>Search both BadChans and user G-lines.</dd>
633  * <dt>GLINE_BADCHAN</dt><dd>Search BadChans.</dd>
634  * <dt>GLINE_GLOBAL</dt><dd>Only match global G-lines.</dd>
635  * <dt>GLINE_LOCAL</dt><dd>Only match local G-lines.</dd>
636  * <dt>GLINE_LASTMOD</dt><dd>Only match G-lines with a last modification time.</dd>
637  * <dt>GLINE_EXACT</dt><dd>Require an exact match of G-line mask.</dd>
638  * <dt>anything else</dt><dd>Search user G-lines.</dd>
639  * </dl>
640  * @param[in] userhost Mask to search for.
641  * @param[in] flags Bitwise combination of GLINE_* flags.
642  * @return First matching G-line, or NULL if none are found.
643  */
644 struct Gline *
645 gline_find(char *userhost, unsigned int flags)
646 {
647   struct Gline *gline = 0;
648   struct Gline *sgline;
649   char *user, *host, *t_uh;
650
651   if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
652     gliter(BadChanGlineList, gline, sgline) {
653       if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
654           (flags & GLINE_LOCAL && gline->gl_flags & GLINE_GLOBAL) ||
655           (flags & GLINE_LASTMOD && !gline->gl_lastmod))
656         continue;
657       else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
658                 match(gline->gl_user, userhost)) == 0)
659         return gline;
660     }
661   }
662
663   if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
664       *userhost == '#' || *userhost == '&')
665     return 0;
666
667   DupString(t_uh, userhost);
668   canon_userhost(t_uh, &user, &host, "*");
669
670   gliter(GlobalGlineList, gline, sgline) {
671     if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
672         (flags & GLINE_LOCAL && gline->gl_flags & GLINE_GLOBAL) ||
673         (flags & GLINE_LASTMOD && !gline->gl_lastmod))
674       continue;
675     else if (flags & GLINE_EXACT) {
676       if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
677            || (!gline->gl_host && !host)) &&
678           (ircd_strcmp(gline->gl_user, user) == 0))
679         break;
680     } else {
681       if (((gline->gl_host && host && match(gline->gl_host, host) == 0)
682            || (!gline->gl_host && !host)) &&
683           (match(gline->gl_user, user) == 0))
684         break;
685     }
686   }
687
688   MyFree(t_uh);
689
690   return gline;
691 }
692
693 /** Find a matching G-line for a user.
694  * @param[in] cptr Client to compare against.
695  * @param[in] flags Bitwise combination of GLINE_GLOBAL and/or
696  * GLINE_LASTMOD to limit matches.
697  * @return Matching G-line, or NULL if none are found.
698  */
699 struct Gline *
700 gline_lookup(struct Client *cptr, unsigned int flags)
701 {
702   struct Gline *gline;
703   struct Gline *sgline;
704
705   gliter(GlobalGlineList, gline, sgline) {
706     if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
707         (flags & GLINE_LASTMOD && !gline->gl_lastmod))
708       continue;
709
710     if (GlineIsRealName(gline)) {
711       Debug((DEBUG_DEBUG,"realname gline: '%s' '%s'",gline->gl_user,cli_info(cptr)));
712       if (match(gline->gl_user+2, cli_info(cptr)) != 0)
713         continue;
714     }
715     else {
716       if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
717         continue;
718
719       if (GlineIsIpMask(gline)) {
720         if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
721           continue;
722       }
723       else {
724         if (match(gline->gl_host, (cli_user(cptr))->realhost) != 0)
725           continue;
726       }
727     }
728     if (GlineIsActive(gline))
729       return gline;
730   }
731   /*
732    * No Glines matched
733    */
734   return 0;
735 }
736
737 /** Delink and free a G-line.
738  * @param[in] gline G-line to free.
739  */
740 void
741 gline_free(struct Gline *gline)
742 {
743   assert(0 != gline);
744
745   *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
746   if (gline->gl_next)
747     gline->gl_next->gl_prev_p = gline->gl_prev_p;
748
749   MyFree(gline->gl_user); /* free up the memory */
750   if (gline->gl_host)
751     MyFree(gline->gl_host);
752   MyFree(gline->gl_reason);
753   MyFree(gline);
754 }
755
756 /** Burst all known global G-lines to another server.
757  * @param[in] cptr Destination of burst.
758  */
759 void
760 gline_burst(struct Client *cptr)
761 {
762   struct Gline *gline;
763   struct Gline *sgline;
764
765   gliter(GlobalGlineList, gline, sgline) {
766     if (!GlineIsLocal(gline) && gline->gl_lastmod)
767       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
768                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
769                     gline->gl_host ? "@" : "",
770                     gline->gl_host ? gline->gl_host : "",
771                     gline->gl_expire - CurrentTime, gline->gl_lastmod,
772                     gline->gl_reason);
773   }
774
775   gliter(BadChanGlineList, gline, sgline) {
776     if (!GlineIsLocal(gline) && gline->gl_lastmod)
777       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
778                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
779                     gline->gl_expire - CurrentTime, gline->gl_lastmod,
780                     gline->gl_reason);
781   }
782 }
783
784 /** Send a G-line to another server.
785  * @param[in] cptr Who to inform of the G-line.
786  * @param[in] gline G-line to send.
787  * @return Zero.
788  */
789 int
790 gline_resend(struct Client *cptr, struct Gline *gline)
791 {
792   if (GlineIsLocal(gline) || !gline->gl_lastmod)
793     return 0;
794
795   sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
796                 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
797                 gline->gl_host ? "@" : "",
798                 gline->gl_host ? gline->gl_host : "",
799                 gline->gl_expire - CurrentTime, gline->gl_lastmod,
800                 gline->gl_reason);
801
802   return 0;
803 }
804
805 /** Display one or all G-lines to a user.
806  * If \a userhost is not NULL, only send the first matching G-line.
807  * Otherwise send the whole list.
808  * @param[in] sptr User asking for G-line list.
809  * @param[in] userhost G-line mask to search for (or NULL).
810  * @return Zero.
811  */
812 int
813 gline_list(struct Client *sptr, char *userhost)
814 {
815   struct Gline *gline;
816   struct Gline *sgline;
817
818   if (userhost) {
819     if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
820       return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
821
822     /* send gline information along */
823     send_reply(sptr, RPL_GLIST, gline->gl_user,
824                gline->gl_host ? "@" : "",
825                gline->gl_host ? gline->gl_host : "",
826                gline->gl_expire + TSoffset,
827                GlineIsLocal(gline) ? cli_name(&me) : "*",
828                GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
829   } else {
830     gliter(GlobalGlineList, gline, sgline) {
831       send_reply(sptr, RPL_GLIST, gline->gl_user,
832                  gline->gl_host ? "@" : "",
833                  gline->gl_host ? gline->gl_host : "",
834                  gline->gl_expire + TSoffset,
835                  GlineIsLocal(gline) ? cli_name(&me) : "*",
836                  GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
837     }
838
839     gliter(BadChanGlineList, gline, sgline) {
840       send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
841                  gline->gl_expire + TSoffset,
842                  GlineIsLocal(gline) ? cli_name(&me) : "*",
843                  GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
844     }
845   }
846
847   /* end of gline information */
848   return send_reply(sptr, RPL_ENDOFGLIST);
849 }
850
851 /** Statistics callback to list G-lines.
852  * @param[in] sptr Client requesting statistics.
853  * @param[in] sd Stats descriptor for request (ignored).
854  * @param[in] param Extra parameter from user (ignored).
855  */
856 void
857 gline_stats(struct Client *sptr, const struct StatDesc *sd,
858             char *param)
859 {
860   struct Gline *gline;
861   struct Gline *sgline;
862
863   gliter(GlobalGlineList, gline, sgline) {
864     send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user,
865                gline->gl_host ? "@" : "",
866                gline->gl_host ? gline->gl_host : "",
867                gline->gl_expire + TSoffset,
868                GlineIsActive(gline) ? '+' : '-',
869                gline->gl_reason);
870   }
871 }
872
873 /** Calculate memory used by G-lines.
874  * @param[out] gl_size Number of bytes used by G-lines.
875  * @return Number of G-lines in use.
876  */
877 int
878 gline_memory_count(size_t *gl_size)
879 {
880   struct Gline *gline;
881   unsigned int gl = 0;
882
883   for (gline = GlobalGlineList; gline; gline = gline->gl_next)
884   {
885     gl++;
886     *gl_size += sizeof(struct Gline);
887     *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
888     *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
889     *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;
890   }
891   return gl;
892 }