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