2 * IRC - Internet Relay Chat, ircd/gline.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Finland
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
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.
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.
21 * @brief Implementation of Gline manipulation functions.
29 #include "ircd_alloc.h"
30 #include "ircd_features.h"
32 #include "ircd_reply.h"
33 #include "ircd_snprintf.h"
34 #include "ircd_string.h"
46 #include "sys.h" /* FALSE bleah */
53 #include <arpa/inet.h> /* for inet_ntoa */
55 #define CHECK_APPROVED 0 /**< Mask is acceptable */
56 #define CHECK_OVERRIDABLE 1 /**< Mask is acceptable, but not by default */
57 #define CHECK_REJECTED 2 /**< Mask is totally unacceptable */
59 #define MASK_WILD_0 0x01 /**< Wildcards in the last position */
60 #define MASK_WILD_1 0x02 /**< Wildcards in the next-to-last position */
62 #define MASK_WILD_MASK 0x03 /**< Mask out the positional wildcards */
64 #define MASK_WILDS 0x10 /**< Mask contains wildcards */
65 #define MASK_IP 0x20 /**< Mask is an IP address */
66 #define MASK_HALT 0x40 /**< Finished processing mask */
68 /** List of user G-lines. */
69 struct Gline* GlobalGlineList = 0;
70 /** List of BadChan G-lines. */
71 struct Gline* BadChanGlineList = 0;
73 /** Find canonical user and host for a string.
74 * If \a userhost starts with '$', assign \a userhost to *user_p and NULL to *host_p.
75 * Otherwise, if \a userhost contains '@', assign the earlier part of it to *user_p and the rest to *host_p.
76 * Otherwise, assign \a def_user to *user_p and \a userhost to *host_p.
78 * @param[in] userhost Input string from user.
79 * @param[out] user_p Gets pointer to user (or channel/realname) part of hostmask.
80 * @param[out] host_p Gets point to host part of hostmask (may be assigned NULL).
81 * @param[in] def_user Default value for user part.
84 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
88 if (*userhost == '$') {
94 if (!(tmp = strchr(userhost, '@'))) {
104 /** Create a Gline structure.
105 * @param[in] user User part of mask.
106 * @param[in] host Host part of mask (NULL if not applicable).
107 * @param[in] reason Reason for G-line.
108 * @param[in] expire Expiration timestamp.
109 * @param[in] lastmod Last modification timestamp.
110 * @param[in] flags Bitwise combination of GLINE_* bits.
111 * @return Newly allocated G-line.
113 static struct Gline *
114 make_gline(char *user, char *host, char *reason, time_t expire, time_t lastmod,
117 struct Gline *gline, *sgline, *after = 0;
119 if (!(flags & GLINE_BADCHAN)) { /* search for overlapping glines first */
121 for (gline = GlobalGlineList; gline; gline = sgline) {
122 sgline = gline->gl_next;
124 if (gline->gl_expire <= CurrentTime)
126 else if (((gline->gl_flags & GLINE_LOCAL) != (flags & GLINE_LOCAL)) ||
127 (gline->gl_host && !host) || (!gline->gl_host && host))
129 else if (!mmatch(gline->gl_user, user) /* gline contains new mask */
130 && (gline->gl_host == NULL || !mmatch(gline->gl_host, host))) {
131 if (expire <= gline->gl_expire) /* will expire before wider gline */
134 after = gline; /* stick new gline after this one */
135 } else if (!mmatch(user, gline->gl_user) /* new mask contains gline */
136 && (gline->gl_host==NULL || !mmatch(host, gline->gl_host))
137 && gline->gl_expire <= expire) /* old expires before new */
138 gline_free(gline); /* save some memory */
142 gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
145 DupString(gline->gl_reason, reason); /* initialize gline... */
146 gline->gl_expire = expire;
147 gline->gl_lastmod = lastmod;
148 gline->gl_flags = flags & GLINE_MASK;
150 if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
151 DupString(gline->gl_user, user); /* first, remember channel */
154 gline->gl_next = BadChanGlineList; /* then link it into list */
155 gline->gl_prev_p = &BadChanGlineList;
156 if (BadChanGlineList)
157 BadChanGlineList->gl_prev_p = &gline->gl_next;
158 BadChanGlineList = gline;
160 DupString(gline->gl_user, user); /* remember them... */
162 DupString(gline->gl_host, host);
164 gline->gl_host = NULL;
166 if (*user != '$' && ipmask_parse(host, &gline->gl_addr, &gline->gl_bits)) {
167 Debug((DEBUG_DEBUG,"IP gline: %s/%u", ircd_ntoa(&gline->gl_addr), gline->gl_bits));
168 gline->gl_flags |= GLINE_IPMASK;
172 gline->gl_next = after->gl_next;
173 gline->gl_prev_p = &after->gl_next;
175 after->gl_next->gl_prev_p = &gline->gl_next;
176 after->gl_next = gline;
178 gline->gl_next = GlobalGlineList; /* then link it into list */
179 gline->gl_prev_p = &GlobalGlineList;
181 GlobalGlineList->gl_prev_p = &gline->gl_next;
182 GlobalGlineList = gline;
189 /** Check local clients against a new G-line.
190 * If the G-line is inactive or a badchan, return immediately.
191 * Otherwise, if any users match it, disconnect them.
192 * @param[in] cptr Peer connect that sent the G-line.
193 * @param[in] sptr Client that originated the G-line.
194 * @param[in] gline New G-line to check.
195 * @return Zero, unless \a sptr G-lined himself, in which case CPTR_KILLED.
198 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
200 struct Client *acptr;
201 int fd, retval = 0, tval;
203 if (GlineIsBadChan(gline)) /* no action taken on badchan glines */
205 if (!GlineIsActive(gline)) /* no action taken on inactive glines */
208 for (fd = HighestFd; fd >= 0; --fd) {
212 if ((acptr = LocalClientArray[fd])) {
213 if (!cli_user(acptr))
216 if (GlineIsRealName(gline)) { /* Realname Gline */
217 Debug((DEBUG_DEBUG,"Realname Gline: %s %s",(cli_info(acptr)),
219 if (match(gline->gl_user+2, cli_info(acptr)) != 0)
221 Debug((DEBUG_DEBUG,"Matched!"));
222 } else { /* Host/IP gline */
223 if (cli_user(acptr)->username &&
224 match(gline->gl_user, (cli_user(acptr))->username) != 0)
227 if (GlineIsIpMask(gline)) {
229 char tbuf1[SOCKIPLEN], tbuf2[SOCKIPLEN];
230 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));
232 if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
236 if (match(gline->gl_host, cli_sockhost(acptr)) != 0)
241 /* ok, here's one that got G-lined */
242 send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
245 /* let the ops know about it */
246 sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
247 get_client_name(acptr, TRUE));
249 /* and get rid of him */
250 if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
252 retval = tval; /* retain killed status */
259 * Implements the mask checking applied to local G-lines.
260 * Basically, host masks must have a minimum of two non-wild domain
261 * fields, and IP masks must have a minimum of 16 bits. If the mask
262 * has even one wild-card, OVERRIDABLE is returned, assuming the other
263 * check doesn't fail.
264 * @param[in] mask G-line mask to check.
265 * @return One of CHECK_REJECTED, CHECK_OVERRIDABLE, or CHECK_APPROVED.
268 gline_checkmask(char *mask)
270 unsigned int flags = MASK_IP;
271 unsigned int dots = 0;
272 unsigned int ipmask = 0;
274 for (; *mask; mask++) { /* go through given mask */
275 if (*mask == '.') { /* it's a separator; advance positional wilds */
276 flags = (flags & ~MASK_WILD_MASK) | ((flags << 1) & MASK_WILD_MASK);
279 if ((flags & (MASK_IP | MASK_WILDS)) == MASK_IP)
280 ipmask += 8; /* It's an IP with no wilds, count bits */
281 } else if (*mask == '*' || *mask == '?')
282 flags |= MASK_WILD_0 | MASK_WILDS; /* found a wildcard */
283 else if (*mask == '/') { /* n.n.n.n/n notation; parse bit specifier */
285 ipmask = strtoul(mask, &mask, 10);
287 /* sanity-check to date */
288 if (*mask || (flags & (MASK_WILDS | MASK_IP)) != MASK_IP)
289 return CHECK_REJECTED;
292 return CHECK_REJECTED;
296 if (dots != 3 || ipmask > 3)
297 return CHECK_REJECTED;
302 flags |= MASK_HALT; /* Halt the ipmask calculation */
303 break; /* get out of the loop */
304 } else if (!IsIP6Char(*mask)) {
305 flags &= ~MASK_IP; /* not an IP anymore! */
310 /* Sanity-check quads */
311 if (dots > 3 || (!(flags & MASK_WILDS) && dots < 3)) {
316 /* update bit count if necessary */
317 if ((flags & (MASK_IP | MASK_WILDS | MASK_HALT)) == MASK_IP)
320 /* Check to see that it's not too wide of a mask */
321 if (flags & MASK_WILDS &&
322 ((!(flags & MASK_IP) && (dots < 2 || flags & MASK_WILD_MASK)) ||
323 (flags & MASK_IP && ipmask < 16)))
324 return CHECK_REJECTED; /* to wide, reject */
326 /* Ok, it's approved; require override if it has wildcards, though */
327 return flags & MASK_WILDS ? CHECK_OVERRIDABLE : CHECK_APPROVED;
330 /** Forward a G-line to other servers.
331 * @param[in] cptr Client that sent us the G-line.
332 * @param[in] sptr Client that originated the G-line.
333 * @param[in] gline G-line to forward.
337 gline_propagate(struct Client *cptr, struct Client *sptr, struct Gline *gline)
339 if (GlineIsLocal(gline) || (IsUser(sptr) && !gline->gl_lastmod))
342 if (gline->gl_lastmod)
343 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
344 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
345 gline->gl_host ? "@" : "",
346 gline->gl_host ? gline->gl_host : "",
347 gline->gl_expire - CurrentTime, gline->gl_lastmod,
350 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
351 (GlineIsRemActive(gline) ?
352 "* +%s%s%s %Tu :%s" : "* -%s%s%s"),
354 gline->gl_host ? "@" : "",
355 gline->gl_host ? gline->gl_host : "",
356 gline->gl_expire - CurrentTime, gline->gl_reason);
361 /** Create a new G-line and add it to global lists.
362 * \a userhost may be in one of four forms:
363 * \li A channel name, to add a BadChan.
364 * \li A string starting with $R and followed by a mask to match against their realname.
365 * \li A user\@IP mask (user\@ part optional) to create an IP-based ban.
366 * \li A user\@host mask (user\@ part optional) to create a hostname ban.
368 * @param[in] cptr Client that sent us the G-line.
369 * @param[in] sptr Client that originated the G-line.
370 * @param[in] userhost Text mask for the G-line.
371 * @param[in] reason Reason for G-line.
372 * @param[in] expire Duration of G-line in seconds.
373 * @param[in] lastmod Last modification time of G-line.
374 * @param[in] flags Bitwise combination of GLINE_* flags.
375 * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
378 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
379 char *reason, time_t expire, time_t lastmod, unsigned int flags)
381 struct Gline *agline;
382 char uhmask[USERLEN + HOSTLEN + 2];
386 assert(0 != userhost);
389 if (*userhost == '#' || *userhost == '&') {
390 if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
391 return send_reply(sptr, ERR_NOPRIVILEGES);
393 flags |= GLINE_BADCHAN;
396 } else if (*userhost == '$') {
397 switch (*userhost == '$' ? userhost[1] : userhost[3]) {
398 case 'R': flags |= GLINE_REALNAME; break;
400 /* uh, what to do here? */
401 /* The answer, my dear Watson, is we throw a protocol_violation()
403 return protocol_violation(sptr,"%s has been smoking the sweet leaf and sent me a whacky gline",cli_name(sptr));
406 user = (*userhost =='$' ? userhost : userhost+2);
409 canon_userhost(userhost, &user, &host, "*");
411 ircd_snprintf(0, uhmask, sizeof(uhmask), "%s@%s", user, host))
412 return send_reply(sptr, ERR_LONGMASK);
413 else if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
414 switch (gline_checkmask(host)) {
415 case CHECK_OVERRIDABLE: /* oper overrided restriction */
416 if (flags & GLINE_OPERFORCE)
420 return send_reply(sptr, ERR_MASKTOOWIDE, uhmask);
424 if ((tmp = count_users(uhmask)) >=
425 feature_int(FEAT_GLINEMAXUSERCOUNT) && !(flags & GLINE_OPERFORCE))
426 return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
431 * You cannot set a negative (or zero) expire time, nor can you set an
432 * expiration time for greater than GLINE_MAX_EXPIRE.
434 if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
435 if (!IsServer(sptr) && MyConnect(sptr))
436 send_reply(sptr, ERR_BADEXPIRE, expire);
440 expire += CurrentTime; /* convert from lifetime to timestamp */
443 sendto_opmask_butone(0, ircd_strncmp(reason, "AUTO", 4) ? SNO_GLINE :
444 SNO_AUTO, "%s adding %s %s for %s%s%s, expiring at "
446 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
448 cli_name((cli_user(sptr))->server),
449 (flags & GLINE_LOCAL) ? "local" : "global",
450 (flags & GLINE_BADCHAN) ? "BADCHAN" : "GLINE", user,
451 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : "@",
452 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : host,
453 expire + TSoffset, reason);
456 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
457 "%#C adding %s %s for %s%s%s, expiring at %Tu: %s", sptr,
458 flags & GLINE_LOCAL ? "local" : "global",
459 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", user,
460 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : "@",
461 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : host,
462 expire + TSoffset, reason);
465 agline = make_gline(user, host, reason, expire, lastmod, flags);
467 if (!agline) /* if it overlapped, silently return */
470 gline_propagate(cptr, sptr, agline);
472 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
475 /** Activate a currently inactive G-line.
476 * @param[in] cptr Peer that told us to activate the G-line.
477 * @param[in] sptr Client that originally thought it was a good idea.
478 * @param[in] gline G-line to activate.
479 * @param[in] lastmod New value for last modification timestamp.
480 * @param[in] flags 0 if the activation should be propagated, GLINE_LOCAL if not.
481 * @return Zero, unless \a sptr had a death wish (in which case CPTR_KILLED).
484 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
485 time_t lastmod, unsigned int flags)
487 unsigned int saveflags = 0;
491 saveflags = gline->gl_flags;
493 if (flags & GLINE_LOCAL)
494 gline->gl_flags &= ~GLINE_LDEACT;
496 gline->gl_flags |= GLINE_ACTIVE;
498 if (gline->gl_lastmod) {
499 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
502 gline->gl_lastmod = lastmod;
506 if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
507 return 0; /* was active to begin with */
509 /* Inform ops and log it */
510 sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
511 "expiring at %Tu: %s",
512 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
514 cli_name((cli_user(sptr))->server),
515 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
516 gline->gl_user, gline->gl_host ? "@" : "",
517 gline->gl_host ? gline->gl_host : "",
518 gline->gl_expire + TSoffset, gline->gl_reason);
520 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
521 "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
522 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
523 gline->gl_host ? "@" : "",
524 gline->gl_host ? gline->gl_host : "",
525 gline->gl_expire + TSoffset, gline->gl_reason);
527 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
528 gline_propagate(cptr, sptr, gline);
530 return do_gline(cptr, sptr, gline);
533 /** Deactivate a G-line.
534 * @param[in] cptr Peer that gave us the message.
535 * @param[in] sptr Client that initiated the deactivation.
536 * @param[in] gline G-line to deactivate.
537 * @param[in] lastmod New value for G-line last modification timestamp.
538 * @param[in] flags GLINE_LOCAL to only deactivate locally, 0 to propagate.
542 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
543 time_t lastmod, unsigned int flags)
545 unsigned int saveflags = 0;
550 saveflags = gline->gl_flags;
552 if (GlineIsLocal(gline))
553 msg = "removing local";
554 else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
555 msg = "removing global";
556 gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
558 msg = "deactivating global";
560 if (flags & GLINE_LOCAL)
561 gline->gl_flags |= GLINE_LDEACT;
563 gline->gl_flags &= ~GLINE_ACTIVE;
565 if (gline->gl_lastmod) {
566 if (gline->gl_lastmod >= lastmod)
569 gline->gl_lastmod = lastmod;
573 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
574 return 0; /* was inactive to begin with */
577 /* Inform ops and log it */
578 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
580 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
582 cli_name((cli_user(sptr))->server),
583 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
584 gline->gl_user, gline->gl_host ? "@" : "",
585 gline->gl_host ? gline->gl_host : "",
586 gline->gl_expire + TSoffset, gline->gl_reason);
588 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
589 "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
590 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
591 gline->gl_host ? "@" : "",
592 gline->gl_host ? gline->gl_host : "",
593 gline->gl_expire + TSoffset, gline->gl_reason);
595 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
596 gline_propagate(cptr, sptr, gline);
598 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
599 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
600 gline_free(gline); /* get rid of it */
605 /** Find a G-line for a particular mask, guided by certain flags.
606 * Certain bits in \a flags are interpreted specially:
608 * <dt>GLINE_ANY</dt><dd>Search both BadChans and user G-lines.</dd>
609 * <dt>GLINE_BADCHAN</dt><dd>Search BadChans.</dd>
610 * <dt>GLINE_GLOBAL</dt><dd>Only match global G-lines.</dd>
611 * <dt>GLINE_LASTMOD</dt><dd>Only match G-lines with a last modification time.</dd>
612 * <dt>GLINE_EXACT</dt><dd>Require an exact match of G-line mask.</dd>
613 * <dt>anything else</dt><dd>Search user G-lines.</dd>
615 * @param[in] userhost Mask to search for.
616 * @param[in] flags Bitwise combination of GLINE_* flags.
617 * @return First matching G-line, or NULL if none are found.
620 gline_find(char *userhost, unsigned int flags)
623 struct Gline *sgline;
624 char *user, *host, *t_uh;
626 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
627 for (gline = BadChanGlineList; gline; gline = sgline) {
628 sgline = gline->gl_next;
630 if (gline->gl_expire <= CurrentTime)
632 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
633 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
635 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
636 match(gline->gl_user, userhost)) == 0)
641 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
642 *userhost == '#' || *userhost == '&')
645 DupString(t_uh, userhost);
646 canon_userhost(t_uh, &user, &host, 0);
651 for (gline = GlobalGlineList; gline; gline = sgline) {
652 sgline = gline->gl_next;
654 if (gline->gl_expire <= CurrentTime)
656 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
657 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
659 else if (flags & GLINE_EXACT) {
660 if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
661 || (!gline->gl_host && !host)) &&
662 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
663 ircd_strcmp(gline->gl_user, user) == 0))
666 if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
667 || (!gline->gl_host && !host)) &&
668 ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
669 match(gline->gl_user, user) == 0))
679 /** Find a matching G-line for a user.
680 * @param[in] cptr Client to compare against.
681 * @param[in] flags Bitwise combination of GLINE_GLOBAL and/or
682 * GLINE_LASTMOD to limit matches.
683 * @return Matching G-line, or NULL if none are found.
686 gline_lookup(struct Client *cptr, unsigned int flags)
689 struct Gline *sgline;
691 for (gline = GlobalGlineList; gline; gline = sgline) {
692 sgline = gline->gl_next;
694 if (gline->gl_expire <= CurrentTime) {
699 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
700 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
703 if (GlineIsRealName(gline)) {
704 Debug((DEBUG_DEBUG,"realname gline: '%s' '%s'",gline->gl_user,cli_info(cptr)));
705 if (match(gline->gl_user+2, cli_info(cptr)) != 0)
707 if (!GlineIsActive(gline))
712 if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
715 if (GlineIsIpMask(gline)) {
717 char tbuf1[SOCKIPLEN], tbuf2[SOCKIPLEN];
718 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));
720 if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
724 if (match(gline->gl_host, (cli_user(cptr))->realhost) != 0)
728 if (GlineIsActive(gline))
737 /** Delink and free a G-line.
738 * @param[in] gline G-line to free.
741 gline_free(struct Gline *gline)
745 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
747 gline->gl_next->gl_prev_p = gline->gl_prev_p;
749 MyFree(gline->gl_user); /* free up the memory */
751 MyFree(gline->gl_host);
752 MyFree(gline->gl_reason);
756 /** Burst all known global G-lines to another server.
757 * @param[in] cptr Destination of burst.
760 gline_burst(struct Client *cptr)
763 struct Gline *sgline;
765 for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
766 sgline = gline->gl_next;
768 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
770 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
771 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
772 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
773 gline->gl_host ? "@" : "",
774 gline->gl_host ? gline->gl_host : "",
775 gline->gl_expire - CurrentTime, gline->gl_lastmod,
779 for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
780 sgline = gline->gl_next;
782 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
784 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
785 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
786 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
787 gline->gl_expire - CurrentTime, gline->gl_lastmod,
792 /** Send a G-line to another server.
793 * @param[in] cptr Who to inform of the G-line.
794 * @param[in] gline G-line to send.
798 gline_resend(struct Client *cptr, struct Gline *gline)
800 if (GlineIsLocal(gline) || !gline->gl_lastmod)
803 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
804 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
805 gline->gl_host ? "@" : "",
806 gline->gl_host ? gline->gl_host : "",
807 gline->gl_expire - CurrentTime, gline->gl_lastmod,
813 /** Display one or all G-lines to a user.
814 * If \a userhost is not NULL, only send the first matching G-line.
815 * Otherwise send the whole list.
816 * @param[in] sptr User asking for G-line list.
817 * @param[in] userhost G-line mask to search for (or NULL).
821 gline_list(struct Client *sptr, char *userhost)
824 struct Gline *sgline;
827 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
828 return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
830 /* send gline information along */
831 send_reply(sptr, RPL_GLIST, gline->gl_user,
832 gline->gl_host ? "@" : "",
833 gline->gl_host ? gline->gl_host : "",
834 gline->gl_expire + TSoffset,
835 GlineIsLocal(gline) ? cli_name(&me) : "*",
836 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
838 for (gline = GlobalGlineList; gline; gline = sgline) {
839 sgline = gline->gl_next;
841 if (gline->gl_expire <= CurrentTime)
844 send_reply(sptr, RPL_GLIST, gline->gl_user,
845 gline->gl_host ? "@" : "",
846 gline->gl_host ? gline->gl_host : "",
847 gline->gl_expire + TSoffset,
848 GlineIsLocal(gline) ? cli_name(&me) : "*",
849 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
852 for (gline = BadChanGlineList; gline; gline = sgline) {
853 sgline = gline->gl_next;
855 if (gline->gl_expire <= CurrentTime)
858 send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
859 gline->gl_expire + TSoffset,
860 GlineIsLocal(gline) ? cli_name(&me) : "*",
861 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
865 /* end of gline information */
866 return send_reply(sptr, RPL_ENDOFGLIST);
869 /** Statistics callback to list G-lines.
870 * @param[in] sptr Client requesting statistics.
871 * @param[in] sd Stats descriptor for request (ignored).
872 * @param[in] param Extra parameter from user (ignored).
875 gline_stats(struct Client *sptr, const struct StatDesc *sd,
879 struct Gline *sgline;
881 for (gline = GlobalGlineList; gline; gline = sgline) {
882 sgline = gline->gl_next;
884 if (gline->gl_expire <= CurrentTime)
887 send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user,
888 gline->gl_host ? "@" : "",
889 gline->gl_host ? gline->gl_host : "",
890 gline->gl_expire + TSoffset, gline->gl_reason);
894 /** Calculate memory used by G-lines.
895 * @param[out] gl_size Number of bytes used by G-lines.
896 * @return Number of G-lines in use.
899 gline_memory_count(size_t *gl_size)
904 for (gline = GlobalGlineList; gline; gline = gline->gl_next)
907 *gl_size += sizeof(struct Gline);
908 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
909 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
910 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;