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"
48 /* #include <assert.h> -- Now using assert in ircd_log.h */
53 #define CHECK_APPROVED 0 /**< Mask is acceptable */
54 #define CHECK_OVERRIDABLE 1 /**< Mask is acceptable, but not by default */
55 #define CHECK_REJECTED 2 /**< Mask is totally unacceptable */
57 #define MASK_WILD_0 0x01 /**< Wildcards in the last position */
58 #define MASK_WILD_1 0x02 /**< Wildcards in the next-to-last position */
60 #define MASK_WILD_MASK 0x03 /**< Mask out the positional wildcards */
62 #define MASK_WILDS 0x10 /**< Mask contains wildcards */
63 #define MASK_IP 0x20 /**< Mask is an IP address */
64 #define MASK_HALT 0x40 /**< Finished processing mask */
66 /** List of user G-lines. */
67 struct Gline* GlobalGlineList = 0;
68 /** List of BadChan G-lines. */
69 struct Gline* BadChanGlineList = 0;
71 /** Find canonical user and host for a string.
72 * If \a userhost starts with '$', assign \a userhost to *user_p and NULL to *host_p.
73 * Otherwise, if \a userhost contains '@', assign the earlier part of it to *user_p and the rest to *host_p.
74 * Otherwise, assign \a def_user to *user_p and \a userhost to *host_p.
76 * @param[in] userhost Input string from user.
77 * @param[out] user_p Gets pointer to user (or channel/realname) part of hostmask.
78 * @param[out] host_p Gets point to host part of hostmask (may be assigned NULL).
79 * @param[in] def_user Default value for user part.
82 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
86 if (*userhost == '$') {
92 if (!(tmp = strchr(userhost, '@'))) {
102 /** Create a Gline structure.
103 * @param[in] user User part of mask.
104 * @param[in] host Host part of mask (NULL if not applicable).
105 * @param[in] reason Reason for G-line.
106 * @param[in] expire Expiration timestamp.
107 * @param[in] lastmod Last modification timestamp.
108 * @param[in] flags Bitwise combination of GLINE_* bits.
109 * @return Newly allocated G-line.
111 static struct Gline *
112 make_gline(char *user, char *host, char *reason, time_t expire, time_t lastmod,
115 struct Gline *gline, *sgline, *after = 0;
117 if (!(flags & GLINE_BADCHAN)) { /* search for overlapping glines first */
119 for (gline = GlobalGlineList; gline; gline = sgline) {
120 sgline = gline->gl_next;
122 if (gline->gl_expire <= CurrentTime)
124 else if (((gline->gl_flags & GLINE_LOCAL) != (flags & GLINE_LOCAL)) ||
125 (gline->gl_host && !host) || (!gline->gl_host && host))
127 else if (!mmatch(gline->gl_user, user) /* gline contains new mask */
128 && (gline->gl_host == NULL || !mmatch(gline->gl_host, host))) {
129 if (expire <= gline->gl_expire) /* will expire before wider gline */
132 after = gline; /* stick new gline after this one */
133 } else if (!mmatch(user, gline->gl_user) /* new mask contains gline */
134 && (gline->gl_host==NULL || !mmatch(host, gline->gl_host))
135 && gline->gl_expire <= expire) /* old expires before new */
136 gline_free(gline); /* save some memory */
140 gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
143 DupString(gline->gl_reason, reason); /* initialize gline... */
144 gline->gl_expire = expire;
145 gline->gl_lastmod = lastmod;
146 gline->gl_flags = flags & GLINE_MASK;
148 if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
149 DupString(gline->gl_user, user); /* first, remember channel */
152 gline->gl_next = BadChanGlineList; /* then link it into list */
153 gline->gl_prev_p = &BadChanGlineList;
154 if (BadChanGlineList)
155 BadChanGlineList->gl_prev_p = &gline->gl_next;
156 BadChanGlineList = gline;
158 DupString(gline->gl_user, user); /* remember them... */
160 DupString(gline->gl_host, host);
162 gline->gl_host = NULL;
164 if (*user != '$' && ipmask_parse(host, &gline->gl_addr, &gline->gl_bits)) {
165 Debug((DEBUG_DEBUG,"IP gline: %s/%u", ircd_ntoa(&gline->gl_addr), gline->gl_bits));
166 gline->gl_flags |= GLINE_IPMASK;
170 gline->gl_next = after->gl_next;
171 gline->gl_prev_p = &after->gl_next;
173 after->gl_next->gl_prev_p = &gline->gl_next;
174 after->gl_next = gline;
176 gline->gl_next = GlobalGlineList; /* then link it into list */
177 gline->gl_prev_p = &GlobalGlineList;
179 GlobalGlineList->gl_prev_p = &gline->gl_next;
180 GlobalGlineList = gline;
187 /** Check local clients against a new G-line.
188 * If the G-line is inactive or a badchan, return immediately.
189 * Otherwise, if any users match it, disconnect them.
190 * @param[in] cptr Peer connect that sent the G-line.
191 * @param[in] sptr Client that originated the G-line.
192 * @param[in] gline New G-line to check.
193 * @return Zero, unless \a sptr G-lined himself, in which case CPTR_KILLED.
196 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
198 struct Client *acptr;
199 int fd, retval = 0, tval;
201 if (GlineIsBadChan(gline)) /* no action taken on badchan glines */
203 if (!GlineIsActive(gline)) /* no action taken on inactive glines */
206 for (fd = HighestFd; fd >= 0; --fd) {
210 if ((acptr = LocalClientArray[fd])) {
211 if (!cli_user(acptr))
214 if (GlineIsRealName(gline)) { /* Realname Gline */
215 Debug((DEBUG_DEBUG,"Realname Gline: %s %s",(cli_info(acptr)),
217 if (match(gline->gl_user+2, cli_info(acptr)) != 0)
219 Debug((DEBUG_DEBUG,"Matched!"));
220 } else { /* Host/IP gline */
221 if (cli_user(acptr)->username &&
222 match(gline->gl_user, (cli_user(acptr))->username) != 0)
225 if (GlineIsIpMask(gline)) {
227 char tbuf1[SOCKIPLEN], tbuf2[SOCKIPLEN];
228 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));
230 if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
234 if (match(gline->gl_host, cli_sockhost(acptr)) != 0)
239 /* ok, here's one that got G-lined */
240 send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
243 /* let the ops know about it */
244 sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
245 get_client_name(acptr, SHOW_IP));
247 /* and get rid of him */
248 if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
250 retval = tval; /* retain killed status */
257 * Implements the mask checking applied to local G-lines.
258 * Basically, host masks must have a minimum of two non-wild domain
259 * fields, and IP masks must have a minimum of 16 bits. If the mask
260 * has even one wild-card, OVERRIDABLE is returned, assuming the other
261 * check doesn't fail.
262 * @param[in] mask G-line mask to check.
263 * @return One of CHECK_REJECTED, CHECK_OVERRIDABLE, or CHECK_APPROVED.
266 gline_checkmask(char *mask)
268 unsigned int flags = MASK_IP;
269 unsigned int dots = 0;
270 unsigned int ipmask = 0;
272 for (; *mask; mask++) { /* go through given mask */
273 if (*mask == '.') { /* it's a separator; advance positional wilds */
274 flags = (flags & ~MASK_WILD_MASK) | ((flags << 1) & MASK_WILD_MASK);
277 if ((flags & (MASK_IP | MASK_WILDS)) == MASK_IP)
278 ipmask += 8; /* It's an IP with no wilds, count bits */
279 } else if (*mask == '*' || *mask == '?')
280 flags |= MASK_WILD_0 | MASK_WILDS; /* found a wildcard */
281 else if (*mask == '/') { /* n.n.n.n/n notation; parse bit specifier */
283 ipmask = strtoul(mask, &mask, 10);
285 /* sanity-check to date */
286 if (*mask || (flags & (MASK_WILDS | MASK_IP)) != MASK_IP)
287 return CHECK_REJECTED;
290 return CHECK_REJECTED;
294 if (dots != 3 || ipmask > 3)
295 return CHECK_REJECTED;
300 flags |= MASK_HALT; /* Halt the ipmask calculation */
301 break; /* get out of the loop */
302 } else if (!IsIP6Char(*mask)) {
303 flags &= ~MASK_IP; /* not an IP anymore! */
308 /* Sanity-check quads */
309 if (dots > 3 || (!(flags & MASK_WILDS) && dots < 3)) {
314 /* update bit count if necessary */
315 if ((flags & (MASK_IP | MASK_WILDS | MASK_HALT)) == MASK_IP)
318 /* Check to see that it's not too wide of a mask */
319 if (flags & MASK_WILDS &&
320 ((!(flags & MASK_IP) && (dots < 2 || flags & MASK_WILD_MASK)) ||
321 (flags & MASK_IP && ipmask < 16)))
322 return CHECK_REJECTED; /* to wide, reject */
324 /* Ok, it's approved; require override if it has wildcards, though */
325 return flags & MASK_WILDS ? CHECK_OVERRIDABLE : CHECK_APPROVED;
328 /** Forward a G-line to other servers.
329 * @param[in] cptr Client that sent us the G-line.
330 * @param[in] sptr Client that originated the G-line.
331 * @param[in] gline G-line to forward.
335 gline_propagate(struct Client *cptr, struct Client *sptr, struct Gline *gline)
337 if (GlineIsLocal(gline) || (IsUser(sptr) && !gline->gl_lastmod))
340 if (gline->gl_lastmod)
341 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
342 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
343 gline->gl_host ? "@" : "",
344 gline->gl_host ? gline->gl_host : "",
345 gline->gl_expire - CurrentTime, gline->gl_lastmod,
348 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
349 (GlineIsRemActive(gline) ?
350 "* +%s%s%s %Tu :%s" : "* -%s%s%s"),
352 gline->gl_host ? "@" : "",
353 gline->gl_host ? gline->gl_host : "",
354 gline->gl_expire - CurrentTime, gline->gl_reason);
359 /** Create a new G-line and add it to global lists.
360 * \a userhost may be in one of four forms:
361 * \li A channel name, to add a BadChan.
362 * \li A string starting with $R and followed by a mask to match against their realname.
363 * \li A user\@IP mask (user\@ part optional) to create an IP-based ban.
364 * \li A user\@host mask (user\@ part optional) to create a hostname ban.
366 * @param[in] cptr Client that sent us the G-line.
367 * @param[in] sptr Client that originated the G-line.
368 * @param[in] userhost Text mask for the G-line.
369 * @param[in] reason Reason for G-line.
370 * @param[in] expire Duration of G-line in seconds.
371 * @param[in] lastmod Last modification time of G-line.
372 * @param[in] flags Bitwise combination of GLINE_* flags.
373 * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
376 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
377 char *reason, time_t expire, time_t lastmod, unsigned int flags)
379 struct Gline *agline;
380 char uhmask[USERLEN + HOSTLEN + 2];
384 assert(0 != userhost);
387 if (*userhost == '#' || *userhost == '&') {
388 if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
389 return send_reply(sptr, ERR_NOPRIVILEGES);
391 flags |= GLINE_BADCHAN;
394 } else if (*userhost == '$') {
395 switch (*userhost == '$' ? userhost[1] : userhost[3]) {
396 case 'R': flags |= GLINE_REALNAME; break;
398 /* uh, what to do here? */
399 /* The answer, my dear Watson, is we throw a protocol_violation()
401 return protocol_violation(sptr,"%s has been smoking the sweet leaf and sent me a whacky gline",cli_name(sptr));
404 user = (*userhost =='$' ? userhost : userhost+2);
407 canon_userhost(userhost, &user, &host, "*");
409 ircd_snprintf(0, uhmask, sizeof(uhmask), "%s@%s", user, host))
410 return send_reply(sptr, ERR_LONGMASK);
411 else if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
412 switch (gline_checkmask(host)) {
413 case CHECK_OVERRIDABLE: /* oper overrided restriction */
414 if (flags & GLINE_OPERFORCE)
418 return send_reply(sptr, ERR_MASKTOOWIDE, uhmask);
422 if ((tmp = count_users(uhmask)) >=
423 feature_int(FEAT_GLINEMAXUSERCOUNT) && !(flags & GLINE_OPERFORCE))
424 return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
429 * You cannot set a negative (or zero) expire time, nor can you set an
430 * expiration time for greater than GLINE_MAX_EXPIRE.
432 if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
433 if (!IsServer(sptr) && MyConnect(sptr))
434 send_reply(sptr, ERR_BADEXPIRE, expire);
438 expire += CurrentTime; /* convert from lifetime to timestamp */
441 sendto_opmask_butone(0, ircd_strncmp(reason, "AUTO", 4) ? SNO_GLINE :
442 SNO_AUTO, "%s adding %s %s for %s%s%s, expiring at "
444 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
446 cli_name((cli_user(sptr))->server),
447 (flags & GLINE_LOCAL) ? "local" : "global",
448 (flags & GLINE_BADCHAN) ? "BADCHAN" : "GLINE", user,
449 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : "@",
450 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : host,
451 expire + TSoffset, reason);
454 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
455 "%#C adding %s %s for %s%s%s, expiring at %Tu: %s", sptr,
456 flags & GLINE_LOCAL ? "local" : "global",
457 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", user,
458 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : "@",
459 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : host,
460 expire + TSoffset, reason);
463 agline = make_gline(user, host, reason, expire, lastmod, flags);
465 if (!agline) /* if it overlapped, silently return */
468 gline_propagate(cptr, sptr, agline);
470 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
473 /** Activate a currently inactive G-line.
474 * @param[in] cptr Peer that told us to activate the G-line.
475 * @param[in] sptr Client that originally thought it was a good idea.
476 * @param[in] gline G-line to activate.
477 * @param[in] lastmod New value for last modification timestamp.
478 * @param[in] flags 0 if the activation should be propagated, GLINE_LOCAL if not.
479 * @return Zero, unless \a sptr had a death wish (in which case CPTR_KILLED).
482 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
483 time_t lastmod, unsigned int flags)
485 unsigned int saveflags = 0;
489 saveflags = gline->gl_flags;
491 if (flags & GLINE_LOCAL)
492 gline->gl_flags &= ~GLINE_LDEACT;
494 gline->gl_flags |= GLINE_ACTIVE;
496 if (gline->gl_lastmod) {
497 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
500 gline->gl_lastmod = lastmod;
504 if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
505 return 0; /* was active to begin with */
507 /* Inform ops and log it */
508 sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
509 "expiring at %Tu: %s",
510 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
512 cli_name((cli_user(sptr))->server),
513 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
514 gline->gl_user, gline->gl_host ? "@" : "",
515 gline->gl_host ? gline->gl_host : "",
516 gline->gl_expire + TSoffset, gline->gl_reason);
518 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
519 "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
520 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
521 gline->gl_host ? "@" : "",
522 gline->gl_host ? gline->gl_host : "",
523 gline->gl_expire + TSoffset, gline->gl_reason);
525 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
526 gline_propagate(cptr, sptr, gline);
528 return do_gline(cptr, sptr, gline);
531 /** Deactivate a G-line.
532 * @param[in] cptr Peer that gave us the message.
533 * @param[in] sptr Client that initiated the deactivation.
534 * @param[in] gline G-line to deactivate.
535 * @param[in] lastmod New value for G-line last modification timestamp.
536 * @param[in] flags GLINE_LOCAL to only deactivate locally, 0 to propagate.
540 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
541 time_t lastmod, unsigned int flags)
543 unsigned int saveflags = 0;
548 saveflags = gline->gl_flags;
550 if (GlineIsLocal(gline))
551 msg = "removing local";
552 else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
553 msg = "removing global";
554 gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
556 msg = "deactivating global";
558 if (flags & GLINE_LOCAL)
559 gline->gl_flags |= GLINE_LDEACT;
561 gline->gl_flags &= ~GLINE_ACTIVE;
563 if (gline->gl_lastmod) {
564 if (gline->gl_lastmod >= lastmod)
567 gline->gl_lastmod = lastmod;
571 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
572 return 0; /* was inactive to begin with */
575 /* Inform ops and log it */
576 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
578 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
580 cli_name((cli_user(sptr))->server),
581 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
582 gline->gl_user, gline->gl_host ? "@" : "",
583 gline->gl_host ? gline->gl_host : "",
584 gline->gl_expire + TSoffset, gline->gl_reason);
586 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
587 "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
588 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
589 gline->gl_host ? "@" : "",
590 gline->gl_host ? gline->gl_host : "",
591 gline->gl_expire + TSoffset, gline->gl_reason);
593 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
594 gline_propagate(cptr, sptr, gline);
596 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
597 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
598 gline_free(gline); /* get rid of it */
603 /** Find a G-line for a particular mask, guided by certain flags.
604 * Certain bits in \a flags are interpreted specially:
606 * <dt>GLINE_ANY</dt><dd>Search both BadChans and user G-lines.</dd>
607 * <dt>GLINE_BADCHAN</dt><dd>Search BadChans.</dd>
608 * <dt>GLINE_GLOBAL</dt><dd>Only match global G-lines.</dd>
609 * <dt>GLINE_LASTMOD</dt><dd>Only match G-lines with a last modification time.</dd>
610 * <dt>GLINE_EXACT</dt><dd>Require an exact match of G-line mask.</dd>
611 * <dt>anything else</dt><dd>Search user G-lines.</dd>
613 * @param[in] userhost Mask to search for.
614 * @param[in] flags Bitwise combination of GLINE_* flags.
615 * @return First matching G-line, or NULL if none are found.
618 gline_find(char *userhost, unsigned int flags)
621 struct Gline *sgline;
622 char *user, *host, *t_uh;
624 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
625 for (gline = BadChanGlineList; gline; gline = sgline) {
626 sgline = gline->gl_next;
628 if (gline->gl_expire <= CurrentTime)
630 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
631 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
633 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
634 match(gline->gl_user, userhost)) == 0)
639 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
640 *userhost == '#' || *userhost == '&')
643 DupString(t_uh, userhost);
644 canon_userhost(t_uh, &user, &host, "*");
646 for (gline = GlobalGlineList; gline; gline = sgline) {
647 sgline = gline->gl_next;
649 if (gline->gl_expire <= CurrentTime)
651 else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
652 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
654 else if (flags & GLINE_EXACT) {
655 if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
656 || (!gline->gl_host && !host)) &&
657 (ircd_strcmp(gline->gl_user, user) == 0))
660 if (((gline->gl_host && host && match(gline->gl_host, host) == 0)
661 || (!gline->gl_host && !host)) &&
662 (match(gline->gl_user, user) == 0))
672 /** Find a matching G-line for a user.
673 * @param[in] cptr Client to compare against.
674 * @param[in] flags Bitwise combination of GLINE_GLOBAL and/or
675 * GLINE_LASTMOD to limit matches.
676 * @return Matching G-line, or NULL if none are found.
679 gline_lookup(struct Client *cptr, unsigned int flags)
682 struct Gline *sgline;
684 for (gline = GlobalGlineList; gline; gline = sgline) {
685 sgline = gline->gl_next;
687 if (gline->gl_expire <= CurrentTime) {
692 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
693 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
696 if (GlineIsRealName(gline)) {
697 Debug((DEBUG_DEBUG,"realname gline: '%s' '%s'",gline->gl_user,cli_info(cptr)));
698 if (match(gline->gl_user+2, cli_info(cptr)) != 0)
700 if (!GlineIsActive(gline))
705 if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
708 if (GlineIsIpMask(gline)) {
710 char tbuf1[SOCKIPLEN], tbuf2[SOCKIPLEN];
711 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));
713 if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
717 if (match(gline->gl_host, (cli_user(cptr))->realhost) != 0)
721 if (GlineIsActive(gline))
730 /** Delink and free a G-line.
731 * @param[in] gline G-line to free.
734 gline_free(struct Gline *gline)
738 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
740 gline->gl_next->gl_prev_p = gline->gl_prev_p;
742 MyFree(gline->gl_user); /* free up the memory */
744 MyFree(gline->gl_host);
745 MyFree(gline->gl_reason);
749 /** Burst all known global G-lines to another server.
750 * @param[in] cptr Destination of burst.
753 gline_burst(struct Client *cptr)
756 struct Gline *sgline;
758 for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
759 sgline = gline->gl_next;
761 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
763 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
764 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
765 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
766 gline->gl_host ? "@" : "",
767 gline->gl_host ? gline->gl_host : "",
768 gline->gl_expire - CurrentTime, gline->gl_lastmod,
772 for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
773 sgline = gline->gl_next;
775 if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
777 else if (!GlineIsLocal(gline) && gline->gl_lastmod)
778 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
779 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
780 gline->gl_expire - CurrentTime, gline->gl_lastmod,
785 /** Send a G-line to another server.
786 * @param[in] cptr Who to inform of the G-line.
787 * @param[in] gline G-line to send.
791 gline_resend(struct Client *cptr, struct Gline *gline)
793 if (GlineIsLocal(gline) || !gline->gl_lastmod)
796 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
797 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
798 gline->gl_host ? "@" : "",
799 gline->gl_host ? gline->gl_host : "",
800 gline->gl_expire - CurrentTime, gline->gl_lastmod,
806 /** Display one or all G-lines to a user.
807 * If \a userhost is not NULL, only send the first matching G-line.
808 * Otherwise send the whole list.
809 * @param[in] sptr User asking for G-line list.
810 * @param[in] userhost G-line mask to search for (or NULL).
814 gline_list(struct Client *sptr, char *userhost)
817 struct Gline *sgline;
820 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
821 return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
823 /* send gline information along */
824 send_reply(sptr, RPL_GLIST, gline->gl_user,
825 gline->gl_host ? "@" : "",
826 gline->gl_host ? gline->gl_host : "",
827 gline->gl_expire + TSoffset,
828 GlineIsLocal(gline) ? cli_name(&me) : "*",
829 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
831 for (gline = GlobalGlineList; gline; gline = sgline) {
832 sgline = gline->gl_next;
834 if (gline->gl_expire <= CurrentTime)
837 send_reply(sptr, RPL_GLIST, gline->gl_user,
838 gline->gl_host ? "@" : "",
839 gline->gl_host ? gline->gl_host : "",
840 gline->gl_expire + TSoffset,
841 GlineIsLocal(gline) ? cli_name(&me) : "*",
842 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
845 for (gline = BadChanGlineList; gline; gline = sgline) {
846 sgline = gline->gl_next;
848 if (gline->gl_expire <= CurrentTime)
851 send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
852 gline->gl_expire + TSoffset,
853 GlineIsLocal(gline) ? cli_name(&me) : "*",
854 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
858 /* end of gline information */
859 return send_reply(sptr, RPL_ENDOFGLIST);
862 /** Statistics callback to list G-lines.
863 * @param[in] sptr Client requesting statistics.
864 * @param[in] sd Stats descriptor for request (ignored).
865 * @param[in] param Extra parameter from user (ignored).
868 gline_stats(struct Client *sptr, const struct StatDesc *sd,
872 struct Gline *sgline;
874 for (gline = GlobalGlineList; gline; gline = sgline) {
875 sgline = gline->gl_next;
877 if (gline->gl_expire <= CurrentTime)
880 send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user,
881 gline->gl_host ? "@" : "",
882 gline->gl_host ? gline->gl_host : "",
883 gline->gl_expire + TSoffset, gline->gl_reason);
887 /** Calculate memory used by G-lines.
888 * @param[out] gl_size Number of bytes used by G-lines.
889 * @return Number of G-lines in use.
892 gline_memory_count(size_t *gl_size)
897 for (gline = GlobalGlineList; gline; gline = gline->gl_next)
900 *gl_size += sizeof(struct Gline);
901 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
902 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
903 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;