IPv6 support (hopefully with fewer future transition pains)
[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, expiring at %Tu: %s", sptr,
402             flags & GLINE_LOCAL ? "local" : "global",
403             flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
404             expire + TSoffset, reason);
405
406   /* make the gline */
407   agline = make_gline(user, host, reason, expire, lastmod, flags);
408
409   if (!agline) /* if it overlapped, silently return */
410     return 0;
411
412   gline_propagate(cptr, sptr, agline);
413
414   if (GlineIsBadChan(agline))
415     return 0;
416
417   return do_gline(cptr, sptr, agline); /* knock off users if necessary */
418 }
419
420 int
421 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
422                time_t lastmod, unsigned int flags)
423 {
424   unsigned int saveflags = 0;
425
426   assert(0 != gline);
427
428   saveflags = gline->gl_flags;
429
430   if (flags & GLINE_LOCAL)
431     gline->gl_flags &= ~GLINE_LDEACT;
432   else {
433     gline->gl_flags |= GLINE_ACTIVE;
434
435     if (gline->gl_lastmod) {
436       if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
437         gline->gl_lastmod++;
438       else
439         gline->gl_lastmod = lastmod;
440     }
441   }
442
443   if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
444     return 0; /* was active to begin with */
445
446   /* Inform ops and log it */
447   sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
448                        "expiring at %Tu: %s",
449                        (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
450                          cli_name(sptr) :
451                          cli_name((cli_user(sptr))->server),
452                        GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
453                        gline->gl_user, gline->gl_host ? "@" : "",
454                        gline->gl_host ? gline->gl_host : "",
455                        gline->gl_expire + TSoffset, gline->gl_reason);
456   
457   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
458             "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
459             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
460             gline->gl_host ? "@" : "",
461             gline->gl_host ? gline->gl_host : "",
462             gline->gl_expire + TSoffset, gline->gl_reason);
463
464   if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
465     gline_propagate(cptr, sptr, gline);
466
467   return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
468 }
469
470 int
471 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
472                  time_t lastmod, unsigned int flags)
473 {
474   unsigned int saveflags = 0;
475   char *msg;
476
477   assert(0 != gline);
478
479   saveflags = gline->gl_flags;
480
481   if (GlineIsLocal(gline))
482     msg = "removing local";
483   else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
484     msg = "removing global";
485     gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
486   } else {
487     msg = "deactivating global";
488
489     if (flags & GLINE_LOCAL)
490       gline->gl_flags |= GLINE_LDEACT;
491     else {
492       gline->gl_flags &= ~GLINE_ACTIVE;
493
494       if (gline->gl_lastmod) {
495         if (gline->gl_lastmod >= lastmod)
496           gline->gl_lastmod++;
497         else
498           gline->gl_lastmod = lastmod;
499       }
500     }
501
502     if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
503       return 0; /* was inactive to begin with */
504   }
505
506   /* Inform ops and log it */
507   sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
508                        "%s",
509                        (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
510                          cli_name(sptr) :
511                          cli_name((cli_user(sptr))->server),
512                        msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
513                        gline->gl_user, gline->gl_host ? "@" : "",
514                        gline->gl_host ? gline->gl_host : "",
515                        gline->gl_expire + TSoffset, gline->gl_reason);
516
517   log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
518             "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
519             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
520             gline->gl_host ? "@" : "",
521             gline->gl_host ? gline->gl_host : "",
522             gline->gl_expire + TSoffset, gline->gl_reason);
523
524   if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
525     gline_propagate(cptr, sptr, gline);
526
527   /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
528   if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
529     gline_free(gline); /* get rid of it */
530
531   return 0;
532 }
533
534 struct Gline *
535 gline_find(char *userhost, unsigned int flags)
536 {
537   struct Gline *gline;
538   struct Gline *sgline;
539   char *user, *host, *t_uh;
540
541   if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
542     for (gline = BadChanGlineList; gline; gline = sgline) {
543       sgline = gline->gl_next;
544
545       if (gline->gl_expire <= CurrentTime)
546         gline_free(gline);
547       else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
548                (flags & GLINE_LASTMOD && !gline->gl_lastmod))
549         continue;
550       else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
551                 match(gline->gl_user, userhost)) == 0)
552         return gline;
553     }
554   }
555
556   if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
557       *userhost == '#' || *userhost == '&')
558     return 0;
559
560   DupString(t_uh, userhost);
561   canon_userhost(t_uh, &user, &host, 0);
562
563   if (BadPtr(user))
564     return 0;
565
566   for (gline = GlobalGlineList; gline; gline = sgline) {
567     sgline = gline->gl_next;
568
569     if (gline->gl_expire <= CurrentTime)
570       gline_free(gline);
571     else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
572              (flags & GLINE_LASTMOD && !gline->gl_lastmod))
573       continue;
574     else if (flags & GLINE_EXACT) {
575       if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
576            || (!gline->gl_host && !host)) &&
577           ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
578            ircd_strcmp(gline->gl_user, user) == 0))
579         break;
580     } else {
581       if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
582            || (!gline->gl_host && !host)) &&
583           ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
584            match(gline->gl_user, user) == 0))
585       break;
586     }
587   }
588
589   MyFree(t_uh);
590
591   return gline;
592 }
593
594 struct Gline *
595 gline_lookup(struct Client *cptr, unsigned int flags)
596 {
597   struct Gline *gline;
598   struct Gline *sgline;
599
600   for (gline = GlobalGlineList; gline; gline = sgline) {
601     sgline = gline->gl_next;
602
603     if (gline->gl_expire <= CurrentTime) {
604       gline_free(gline);
605       continue;
606     }
607     
608     if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
609         (flags & GLINE_LASTMOD && !gline->gl_lastmod))
610       continue;
611
612     if (GlineIsRealName(gline)) {
613       Debug((DEBUG_DEBUG,"realname gline: '%s' '%s'",gline->gl_user,cli_info(cptr)));
614       if (match(gline->gl_user+2, cli_info(cptr)) != 0)
615         continue;
616       if (!GlineIsActive(gline))
617         continue;
618       return gline;
619     }
620     else {
621       if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
622         continue;
623
624       if (GlineIsIpMask(gline)) {
625 #ifdef DEBUGMODE
626         char tbuf1[SOCKIPLEN], tbuf2[SOCKIPLEN];
627         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));
628 #endif
629         if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
630           continue;
631       }
632       else {
633         if (match(gline->gl_host, (cli_user(cptr))->realhost) != 0)
634           continue;
635       }
636     }
637     if (GlineIsActive(gline))
638       return gline;
639   }
640   /*
641    * No Glines matched
642    */
643   return 0;
644 }
645
646 void
647 gline_free(struct Gline *gline)
648 {
649   assert(0 != gline);
650
651   *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
652   if (gline->gl_next)
653     gline->gl_next->gl_prev_p = gline->gl_prev_p;
654
655   MyFree(gline->gl_user); /* free up the memory */
656   if (gline->gl_host)
657     MyFree(gline->gl_host);
658   MyFree(gline->gl_reason);
659   MyFree(gline);
660 }
661
662 void
663 gline_burst(struct Client *cptr)
664 {
665   struct Gline *gline;
666   struct Gline *sgline;
667
668   for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
669     sgline = gline->gl_next;
670
671     if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
672       gline_free(gline);
673     else if (!GlineIsLocal(gline) && gline->gl_lastmod)
674       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
675                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
676                     gline->gl_host ? "@" : "",
677                     gline->gl_host ? gline->gl_host : "",
678                     gline->gl_expire - CurrentTime, gline->gl_lastmod,
679                     gline->gl_reason);
680   }
681
682   for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
683     sgline = gline->gl_next;
684
685     if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
686       gline_free(gline);
687     else if (!GlineIsLocal(gline) && gline->gl_lastmod)
688       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
689                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
690                     gline->gl_expire - CurrentTime, gline->gl_lastmod,
691                     gline->gl_reason);
692   }
693 }
694
695 int
696 gline_resend(struct Client *cptr, struct Gline *gline)
697 {
698   if (GlineIsLocal(gline) || !gline->gl_lastmod)
699     return 0;
700
701   sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
702                 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
703                 gline->gl_host ? "@" : "",
704                 gline->gl_host ? gline->gl_host : "",
705                 gline->gl_expire - CurrentTime, gline->gl_lastmod,
706                 gline->gl_reason);
707
708   return 0;
709 }
710
711 int
712 gline_list(struct Client *sptr, char *userhost)
713 {
714   struct Gline *gline;
715   struct Gline *sgline;
716
717   if (userhost) {
718     if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
719       return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
720
721     /* send gline information along */
722     send_reply(sptr, RPL_GLIST, gline->gl_user,
723                gline->gl_host ? "@" : "",
724                gline->gl_host ? gline->gl_host : "",
725                gline->gl_expire + TSoffset,
726                GlineIsLocal(gline) ? cli_name(&me) : "*",
727                GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
728   } else {
729     for (gline = GlobalGlineList; gline; gline = sgline) {
730       sgline = gline->gl_next;
731
732       if (gline->gl_expire <= CurrentTime)
733         gline_free(gline);
734       else
735         send_reply(sptr, RPL_GLIST, gline->gl_user,
736                    gline->gl_host ? "@" : "",
737                    gline->gl_host ? gline->gl_host : "",
738                    gline->gl_expire + TSoffset,
739                    GlineIsLocal(gline) ? cli_name(&me) : "*",
740                    GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
741     }
742
743     for (gline = BadChanGlineList; gline; gline = sgline) {
744       sgline = gline->gl_next;
745
746       if (gline->gl_expire <= CurrentTime)
747         gline_free(gline);
748       else
749         send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
750                    gline->gl_expire + TSoffset,
751                    GlineIsLocal(gline) ? cli_name(&me) : "*",
752                    GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
753     }
754   }
755
756   /* end of gline information */
757   return send_reply(sptr, RPL_ENDOFGLIST);
758 }
759
760 void
761 gline_stats(struct Client *sptr, struct StatDesc *sd, int stat,
762             char *param)
763 {
764   struct Gline *gline;
765   struct Gline *sgline;
766
767   for (gline = GlobalGlineList; gline; gline = sgline) {
768     sgline = gline->gl_next;
769
770     if (gline->gl_expire <= CurrentTime)
771       gline_free(gline);
772     else
773       send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user,
774                  gline->gl_host ? "@" : "",
775                  gline->gl_host ? gline->gl_host : "",
776                  gline->gl_expire + TSoffset, gline->gl_reason);
777   }
778 }
779
780 int
781 gline_memory_count(size_t *gl_size)
782 {
783   struct Gline *gline;
784   unsigned int gl = 0;
785
786   for (gline = GlobalGlineList; gline; gline = gline->gl_next)
787   {
788     gl++;
789     *gl_size += sizeof(struct Gline);
790     *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
791     *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
792     *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;
793   }
794   return gl;
795 }