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