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