Move check_if_ipmask() from support.* to match.*.
[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 1, 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  * $Id$
21  */
22 #include "config.h"
23
24 #include "gline.h"
25 #include "client.h"
26 #include "ircd.h"
27 #include "ircd_alloc.h"
28 #include "ircd_features.h"
29 #include "ircd_log.h"
30 #include "ircd_reply.h"
31 #include "ircd_snprintf.h"
32 #include "ircd_string.h"
33 #include "match.h"
34 #include "numeric.h"
35 #include "s_bsd.h"
36 #include "s_debug.h"
37 #include "s_misc.h"
38 #include "s_stats.h"
39 #include "send.h"
40 #include "struct.h"
41 #include "msg.h"
42 #include "numnicks.h"
43 #include "numeric.h"
44 #include "sys.h"    /* FALSE bleah */
45 #include "whocmds.h"
46
47 #include <assert.h>
48 #include <string.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <arpa/inet.h> /* for inet_ntoa */
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 struct Gline* GlobalGlineList  = 0;
67 struct Gline* BadChanGlineList = 0;
68
69 static void
70 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
71 {
72   char *tmp;
73
74   if (*userhost == '$') {
75     *user_p = userhost;
76     *host_p = NULL;
77     return;
78   }
79
80   if (!(tmp = strchr(userhost, '@'))) {
81     *user_p = def_user;
82     *host_p = userhost;
83   } else {
84     *user_p = userhost;
85     *(tmp++) = '\0';
86     *host_p = tmp;
87   }
88 }
89
90 static struct Gline *
91 make_gline(char *user, char *host, char *reason, time_t expire, time_t lastmod,
92            unsigned int flags)
93 {
94   struct Gline *gline, *sgline, *after = 0;
95
96   if (!(flags & GLINE_BADCHAN)) { /* search for overlapping glines first */
97
98     for (gline = GlobalGlineList; gline; gline = sgline) {
99       sgline = gline->gl_next;
100
101       if (gline->gl_expire <= CurrentTime)
102         gline_free(gline);
103       else if (((gline->gl_flags & GLINE_LOCAL) != (flags & GLINE_LOCAL)) ||
104                (gline->gl_host && !host) || (!gline->gl_host && host))
105         continue;
106       else if (!mmatch(gline->gl_user, user) /* gline contains new mask */
107                && (gline->gl_host == NULL || !mmatch(gline->gl_host, host))) {
108         if (expire <= gline->gl_expire) /* will expire before wider gline */
109           return 0;
110         else
111           after = gline; /* stick new gline after this one */
112       } else if (!mmatch(user, gline->gl_user) /* new mask contains gline */
113                  && (gline->gl_host==NULL || !mmatch(host, gline->gl_host)) 
114                  && gline->gl_expire <= expire) /* old expires before new */
115         gline_free(gline); /* save some memory */
116     }
117   }
118
119   gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
120   assert(0 != gline);
121
122   DupString(gline->gl_reason, reason); /* initialize gline... */
123   gline->gl_expire = expire;
124   gline->gl_lastmod = lastmod;
125   gline->gl_flags = flags & GLINE_MASK;
126
127   if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
128     DupString(gline->gl_user, user); /* first, remember channel */
129     gline->gl_host = 0;
130
131     gline->gl_next = BadChanGlineList; /* then link it into list */
132     gline->gl_prev_p = &BadChanGlineList;
133     if (BadChanGlineList)
134       BadChanGlineList->gl_prev_p = &gline->gl_next;
135     BadChanGlineList = gline;
136   } else {
137     DupString(gline->gl_user, user); /* remember them... */
138     if (*user != '$')
139       DupString(gline->gl_host, host);
140     else
141       gline->gl_host = NULL;
142
143     if (*user != '$' && ipmask_parse(host, &gline->gl_addr, &gline->gl_bits)) {
144       Debug((DEBUG_DEBUG,"IP gline: %s/%u", ircd_ntoa(&gline->gl_addr), gline->gl_bits));
145       gline->gl_flags |= GLINE_IPMASK;
146     }
147
148     if (after) {
149       gline->gl_next = after->gl_next;
150       gline->gl_prev_p = &after->gl_next;
151       if (after->gl_next)
152         after->gl_next->gl_prev_p = &gline->gl_next;
153       after->gl_next = gline;
154     } else {
155       gline->gl_next = GlobalGlineList; /* then link it into list */
156       gline->gl_prev_p = &GlobalGlineList;
157       if (GlobalGlineList)
158         GlobalGlineList->gl_prev_p = &gline->gl_next;
159       GlobalGlineList = gline;
160     }
161   }
162
163   return gline;
164 }
165
166 static int
167 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
168 {
169   struct Client *acptr;
170   int fd, retval = 0, tval;
171
172   if (!GlineIsActive(gline)) /* no action taken on inactive glines */
173     return 0;
174
175   for (fd = HighestFd; fd >= 0; --fd) {
176     /*
177      * get the users!
178      */
179     if ((acptr = LocalClientArray[fd])) {
180       if (!cli_user(acptr))
181         continue;
182
183       if (GlineIsRealName(gline)) { /* Realname Gline */
184         Debug((DEBUG_DEBUG,"Realname Gline: %s %s",(cli_info(acptr)),
185                                         gline->gl_user+2));
186         if (match(gline->gl_user+2, cli_info(acptr)) != 0)
187             continue;
188         Debug((DEBUG_DEBUG,"Matched!"));
189       } else { /* Host/IP gline */
190         if (cli_user(acptr)->username &&
191             match(gline->gl_user, (cli_user(acptr))->username) != 0)
192           continue;
193
194         if (GlineIsIpMask(gline)) {
195 #ifdef DEBUGMODE
196           char tbuf1[SOCKIPLEN], tbuf2[SOCKIPLEN];
197           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));
198 #endif
199           if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
200             continue;
201         }
202         else {
203           if (match(gline->gl_host, cli_sockhost(acptr)) != 0)
204             continue;
205         }
206       }
207
208       /* ok, here's one that got G-lined */
209       send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
210            gline->gl_reason);
211
212       /* let the ops know about it */
213       sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
214                      get_client_name(acptr, TRUE));
215
216       /* and get rid of him */
217       if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
218           gline->gl_reason)))
219         retval = tval; /* retain killed status */
220     }
221   }
222   return retval;
223 }
224
225 /*
226  * This routine implements the mask checking applied to local
227  * G-lines.  Basically, host masks must have a minimum of two non-wild
228  * domain fields, and IP masks must have a minimum of 16 bits.  If the
229  * mask has even one wild-card, OVERRIDABLE is returned, assuming the
230  * other check doesn't fail.
231  */
232 static int
233 gline_checkmask(char *mask)
234 {
235   unsigned int flags = MASK_IP;
236   unsigned int dots = 0;
237   unsigned int ipmask = 0;
238
239   for (; *mask; mask++) { /* go through given mask */
240     if (*mask == '.') { /* it's a separator; advance positional wilds */
241       flags = (flags & ~MASK_WILD_MASK) | ((flags << 1) & MASK_WILD_MASK);
242       dots++;
243
244       if ((flags & (MASK_IP | MASK_WILDS)) == MASK_IP)
245         ipmask += 8; /* It's an IP with no wilds, count bits */
246     } else if (*mask == '*' || *mask == '?')
247       flags |= MASK_WILD_0 | MASK_WILDS; /* found a wildcard */
248     else if (*mask == '/') { /* n.n.n.n/n notation; parse bit specifier */
249       ++mask;
250       ipmask = strtoul(mask, &mask, 10);
251
252       /* sanity-check to date */
253       if (*mask || (flags & (MASK_WILDS | MASK_IP)) != MASK_IP)
254         return CHECK_REJECTED;
255       if (!dots) {
256         if (ipmask > 128)
257           return CHECK_REJECTED;
258         if (ipmask < 128)
259           flags |= MASK_WILDS;
260       } else {
261         if (dots != 3 || ipmask > 3)
262           return CHECK_REJECTED;
263         if (ipmask < 32)
264           flags |= MASK_WILDS;
265       }
266
267       flags |= MASK_HALT; /* Halt the ipmask calculation */
268       break; /* get out of the loop */
269     } else if (!IsIP6Char(*mask)) {
270       flags &= ~MASK_IP; /* not an IP anymore! */
271       ipmask = 0;
272     }
273   }
274
275   /* Sanity-check quads */
276   if (dots > 3 || (!(flags & MASK_WILDS) && dots < 3)) {
277     flags &= ~MASK_IP;
278     ipmask = 0;
279   }
280
281   /* update bit count if necessary */
282   if ((flags & (MASK_IP | MASK_WILDS | MASK_HALT)) == MASK_IP)
283     ipmask += 8;
284
285   /* Check to see that it's not too wide of a mask */
286   if (flags & MASK_WILDS &&
287       ((!(flags & MASK_IP) && (dots < 2 || flags & MASK_WILD_MASK)) ||
288        (flags & MASK_IP && ipmask < 16)))
289     return CHECK_REJECTED; /* to wide, reject */
290
291   /* Ok, it's approved; require override if it has wildcards, though */
292   return flags & MASK_WILDS ? CHECK_OVERRIDABLE : CHECK_APPROVED;
293 }
294
295 int
296 gline_propagate(struct Client *cptr, struct Client *sptr, struct Gline *gline)
297 {
298   if (GlineIsLocal(gline) || (IsUser(sptr) && !gline->gl_lastmod))
299     return 0;
300
301   if (gline->gl_lastmod)
302     sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
303                           GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
304                           gline->gl_host ? "@" : "",
305                           gline->gl_host ? gline->gl_host : "",
306                           gline->gl_expire - CurrentTime, gline->gl_lastmod,
307                           gline->gl_reason);
308   else
309     sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
310                           (GlineIsRemActive(gline) ?
311                            "* +%s%s%s %Tu :%s" : "* -%s%s%s"),
312                           gline->gl_user, 
313                           gline->gl_host ? "@" : "",
314                           gline->gl_host ? gline->gl_host : "",
315                           gline->gl_expire - CurrentTime, gline->gl_reason);
316
317   return 0;
318 }
319
320 int
321 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
322           char *reason, time_t expire, time_t lastmod, unsigned int flags)
323 {
324   struct Gline *agline;
325   char uhmask[USERLEN + HOSTLEN + 2];
326   char *user, *host;
327   int tmp;
328
329   assert(0 != userhost);
330   assert(0 != reason);
331
332   if (*userhost == '#' || *userhost == '&') {
333     if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
334       return send_reply(sptr, ERR_NOPRIVILEGES);
335
336     flags |= GLINE_BADCHAN;
337     user = userhost;
338     host = 0;
339   } else if (*userhost == '$') {
340     switch (*userhost == '$' ? userhost[1] : userhost[3]) {
341       case 'R': flags |= GLINE_REALNAME; break;
342       default:
343         /* uh, what to do here? */
344         /* The answer, my dear Watson, is we throw a protocol_violation()
345            -- hikari */
346         return protocol_violation(sptr,"%s has been smoking the sweet leaf and sent me a whacky gline",cli_name(sptr));
347         break;
348     }
349      user = (*userhost =='$' ? userhost : userhost+2);
350      host = 0;
351   } else {
352     canon_userhost(userhost, &user, &host, "*");
353     if (sizeof(uhmask) <
354         ircd_snprintf(0, uhmask, sizeof(uhmask), "%s@%s", user, host))
355       return send_reply(sptr, ERR_LONGMASK);
356     else if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
357       switch (gline_checkmask(host)) {
358       case CHECK_OVERRIDABLE: /* oper overrided restriction */
359         if (flags & GLINE_OPERFORCE)
360           break;
361         /*FALLTHROUGH*/
362       case CHECK_REJECTED:
363         return send_reply(sptr, ERR_MASKTOOWIDE, uhmask);
364         break;
365       }
366
367       if ((tmp = count_users(uhmask)) >=
368           feature_int(FEAT_GLINEMAXUSERCOUNT) && !(flags & GLINE_OPERFORCE))
369         return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
370     }
371   }
372
373   /*
374    * You cannot set a negative (or zero) expire time, nor can you set an
375    * expiration time for greater than GLINE_MAX_EXPIRE.
376    */
377   if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
378     if (!IsServer(sptr) && MyConnect(sptr))
379       send_reply(sptr, ERR_BADEXPIRE, expire);
380     return 0;
381   }
382
383   expire += CurrentTime; /* convert from lifetime to timestamp */
384
385   /* Inform ops... */
386   sendto_opmask_butone(0, ircd_strncmp(reason, "AUTO", 4) ? SNO_GLINE :
387                        SNO_AUTO, "%s adding %s %s for %s%s%s, expiring at "
388                        "%Tu: %s",
389                        (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
390                          cli_name(sptr) :
391                          cli_name((cli_user(sptr))->server),
392                        (flags & GLINE_LOCAL) ? "local" : "global",
393                        (flags & GLINE_BADCHAN) ? "BADCHAN" : "GLINE", user,
394                        (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : "@",
395                        (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : host,
396                        expire + TSoffset, reason);
397
398   /* and log it */
399   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
400             "%#C adding %s %s for %s%s%s, expiring at %Tu: %s", sptr,
401             flags & GLINE_LOCAL ? "local" : "global",
402             flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", user,
403             flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : "@",
404             flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : host,
405             expire + TSoffset, reason);
406
407   /* make the gline */
408   agline = make_gline(user, host, reason, expire, lastmod, flags);
409
410   if (!agline) /* if it overlapped, silently return */
411     return 0;
412
413   gline_propagate(cptr, sptr, agline);
414
415   if (GlineIsBadChan(agline))
416     return 0;
417
418   return do_gline(cptr, sptr, agline); /* knock off users if necessary */
419 }
420
421 int
422 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
423                time_t lastmod, unsigned int flags)
424 {
425   unsigned int saveflags = 0;
426
427   assert(0 != gline);
428
429   saveflags = gline->gl_flags;
430
431   if (flags & GLINE_LOCAL)
432     gline->gl_flags &= ~GLINE_LDEACT;
433   else {
434     gline->gl_flags |= GLINE_ACTIVE;
435
436     if (gline->gl_lastmod) {
437       if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
438         gline->gl_lastmod++;
439       else
440         gline->gl_lastmod = lastmod;
441     }
442   }
443
444   if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
445     return 0; /* was active to begin with */
446
447   /* Inform ops and log it */
448   sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
449                        "expiring at %Tu: %s",
450                        (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
451                          cli_name(sptr) :
452                          cli_name((cli_user(sptr))->server),
453                        GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
454                        gline->gl_user, gline->gl_host ? "@" : "",
455                        gline->gl_host ? gline->gl_host : "",
456                        gline->gl_expire + TSoffset, gline->gl_reason);
457   
458   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
459             "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
460             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
461             gline->gl_host ? "@" : "",
462             gline->gl_host ? gline->gl_host : "",
463             gline->gl_expire + TSoffset, gline->gl_reason);
464
465   if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
466     gline_propagate(cptr, sptr, gline);
467
468   return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
469 }
470
471 int
472 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
473                  time_t lastmod, unsigned int flags)
474 {
475   unsigned int saveflags = 0;
476   char *msg;
477
478   assert(0 != gline);
479
480   saveflags = gline->gl_flags;
481
482   if (GlineIsLocal(gline))
483     msg = "removing local";
484   else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
485     msg = "removing global";
486     gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
487   } else {
488     msg = "deactivating global";
489
490     if (flags & GLINE_LOCAL)
491       gline->gl_flags |= GLINE_LDEACT;
492     else {
493       gline->gl_flags &= ~GLINE_ACTIVE;
494
495       if (gline->gl_lastmod) {
496         if (gline->gl_lastmod >= lastmod)
497           gline->gl_lastmod++;
498         else
499           gline->gl_lastmod = lastmod;
500       }
501     }
502
503     if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
504       return 0; /* was inactive to begin with */
505   }
506
507   /* Inform ops and log it */
508   sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
509                        "%s",
510                        (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
511                          cli_name(sptr) :
512                          cli_name((cli_user(sptr))->server),
513                        msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
514                        gline->gl_user, gline->gl_host ? "@" : "",
515                        gline->gl_host ? gline->gl_host : "",
516                        gline->gl_expire + TSoffset, gline->gl_reason);
517
518   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
519             "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
520             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
521             gline->gl_host ? "@" : "",
522             gline->gl_host ? gline->gl_host : "",
523             gline->gl_expire + TSoffset, gline->gl_reason);
524
525   if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
526     gline_propagate(cptr, sptr, gline);
527
528   /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
529   if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
530     gline_free(gline); /* get rid of it */
531
532   return 0;
533 }
534
535 struct Gline *
536 gline_find(char *userhost, unsigned int flags)
537 {
538   struct Gline *gline;
539   struct Gline *sgline;
540   char *user, *host, *t_uh;
541
542   if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
543     for (gline = BadChanGlineList; gline; gline = sgline) {
544       sgline = gline->gl_next;
545
546       if (gline->gl_expire <= CurrentTime)
547         gline_free(gline);
548       else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
549                (flags & GLINE_LASTMOD && !gline->gl_lastmod))
550         continue;
551       else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
552                 match(gline->gl_user, userhost)) == 0)
553         return gline;
554     }
555   }
556
557   if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
558       *userhost == '#' || *userhost == '&')
559     return 0;
560
561   DupString(t_uh, userhost);
562   canon_userhost(t_uh, &user, &host, 0);
563
564   if (BadPtr(user))
565     return 0;
566
567   for (gline = GlobalGlineList; gline; gline = sgline) {
568     sgline = gline->gl_next;
569
570     if (gline->gl_expire <= CurrentTime)
571       gline_free(gline);
572     else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
573              (flags & GLINE_LASTMOD && !gline->gl_lastmod))
574       continue;
575     else if (flags & GLINE_EXACT) {
576       if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
577            || (!gline->gl_host && !host)) &&
578           ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
579            ircd_strcmp(gline->gl_user, user) == 0))
580         break;
581     } else {
582       if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
583            || (!gline->gl_host && !host)) &&
584           ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
585            match(gline->gl_user, user) == 0))
586       break;
587     }
588   }
589
590   MyFree(t_uh);
591
592   return gline;
593 }
594
595 struct Gline *
596 gline_lookup(struct Client *cptr, unsigned int flags)
597 {
598   struct Gline *gline;
599   struct Gline *sgline;
600
601   for (gline = GlobalGlineList; gline; gline = sgline) {
602     sgline = gline->gl_next;
603
604     if (gline->gl_expire <= CurrentTime) {
605       gline_free(gline);
606       continue;
607     }
608     
609     if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
610         (flags & GLINE_LASTMOD && !gline->gl_lastmod))
611       continue;
612
613     if (GlineIsRealName(gline)) {
614       Debug((DEBUG_DEBUG,"realname gline: '%s' '%s'",gline->gl_user,cli_info(cptr)));
615       if (match(gline->gl_user+2, cli_info(cptr)) != 0)
616         continue;
617       if (!GlineIsActive(gline))
618         continue;
619       return gline;
620     }
621     else {
622       if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
623         continue;
624
625       if (GlineIsIpMask(gline)) {
626 #ifdef DEBUGMODE
627         char tbuf1[SOCKIPLEN], tbuf2[SOCKIPLEN];
628         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));
629 #endif
630         if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
631           continue;
632       }
633       else {
634         if (match(gline->gl_host, (cli_user(cptr))->realhost) != 0)
635           continue;
636       }
637     }
638     if (GlineIsActive(gline))
639       return gline;
640   }
641   /*
642    * No Glines matched
643    */
644   return 0;
645 }
646
647 void
648 gline_free(struct Gline *gline)
649 {
650   assert(0 != gline);
651
652   *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
653   if (gline->gl_next)
654     gline->gl_next->gl_prev_p = gline->gl_prev_p;
655
656   MyFree(gline->gl_user); /* free up the memory */
657   if (gline->gl_host)
658     MyFree(gline->gl_host);
659   MyFree(gline->gl_reason);
660   MyFree(gline);
661 }
662
663 void
664 gline_burst(struct Client *cptr)
665 {
666   struct Gline *gline;
667   struct Gline *sgline;
668
669   for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
670     sgline = gline->gl_next;
671
672     if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
673       gline_free(gline);
674     else if (!GlineIsLocal(gline) && gline->gl_lastmod)
675       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
676                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
677                     gline->gl_host ? "@" : "",
678                     gline->gl_host ? gline->gl_host : "",
679                     gline->gl_expire - CurrentTime, gline->gl_lastmod,
680                     gline->gl_reason);
681   }
682
683   for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
684     sgline = gline->gl_next;
685
686     if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
687       gline_free(gline);
688     else if (!GlineIsLocal(gline) && gline->gl_lastmod)
689       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
690                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
691                     gline->gl_expire - CurrentTime, gline->gl_lastmod,
692                     gline->gl_reason);
693   }
694 }
695
696 int
697 gline_resend(struct Client *cptr, struct Gline *gline)
698 {
699   if (GlineIsLocal(gline) || !gline->gl_lastmod)
700     return 0;
701
702   sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
703                 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
704                 gline->gl_host ? "@" : "",
705                 gline->gl_host ? gline->gl_host : "",
706                 gline->gl_expire - CurrentTime, gline->gl_lastmod,
707                 gline->gl_reason);
708
709   return 0;
710 }
711
712 int
713 gline_list(struct Client *sptr, char *userhost)
714 {
715   struct Gline *gline;
716   struct Gline *sgline;
717
718   if (userhost) {
719     if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
720       return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
721
722     /* send gline information along */
723     send_reply(sptr, RPL_GLIST, gline->gl_user,
724                gline->gl_host ? "@" : "",
725                gline->gl_host ? gline->gl_host : "",
726                gline->gl_expire + TSoffset,
727                GlineIsLocal(gline) ? cli_name(&me) : "*",
728                GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
729   } else {
730     for (gline = GlobalGlineList; gline; gline = sgline) {
731       sgline = gline->gl_next;
732
733       if (gline->gl_expire <= CurrentTime)
734         gline_free(gline);
735       else
736         send_reply(sptr, RPL_GLIST, gline->gl_user,
737                    gline->gl_host ? "@" : "",
738                    gline->gl_host ? gline->gl_host : "",
739                    gline->gl_expire + TSoffset,
740                    GlineIsLocal(gline) ? cli_name(&me) : "*",
741                    GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
742     }
743
744     for (gline = BadChanGlineList; gline; gline = sgline) {
745       sgline = gline->gl_next;
746
747       if (gline->gl_expire <= CurrentTime)
748         gline_free(gline);
749       else
750         send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
751                    gline->gl_expire + TSoffset,
752                    GlineIsLocal(gline) ? cli_name(&me) : "*",
753                    GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
754     }
755   }
756
757   /* end of gline information */
758   return send_reply(sptr, RPL_ENDOFGLIST);
759 }
760
761 void
762 gline_stats(struct Client *sptr, const struct StatDesc *sd,
763             char *param)
764 {
765   struct Gline *gline;
766   struct Gline *sgline;
767
768   for (gline = GlobalGlineList; gline; gline = sgline) {
769     sgline = gline->gl_next;
770
771     if (gline->gl_expire <= CurrentTime)
772       gline_free(gline);
773     else
774       send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user,
775                  gline->gl_host ? "@" : "",
776                  gline->gl_host ? gline->gl_host : "",
777                  gline->gl_expire + TSoffset, gline->gl_reason);
778   }
779 }
780
781 int
782 gline_memory_count(size_t *gl_size)
783 {
784   struct Gline *gline;
785   unsigned int gl = 0;
786
787   for (gline = GlobalGlineList; gline; gline = gline->gl_next)
788   {
789     gl++;
790     *gl_size += sizeof(struct Gline);
791     *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
792     *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
793     *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;
794   }
795   return gl;
796 }