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