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 /** Iterate through \a list of G-lines. Use this like a for loop,
72 * i.e., follow it with braces and use whatever you passed as \a gl
73 * as a single G-line to be acted upon.
75 * @param[in] list List of G-lines to iterate over.
76 * @param[in] gl Name of a struct Gline pointer variable that will be made to point to the G-lines in sequence.
77 * @param[in] next Name of a scratch struct Gline pointer variable.
79 #define gliter(list, gl, next) \
80 /* Iterate through the G-lines in the list */ \
81 for ((gl) = (list); (gl); (gl) = (next)) \
82 /* Figure out the next pointer in list... */ \
83 if ((((next) = (gl)->gl_next) || 1) && \
84 /* Then see if it's expired */ \
85 (gl)->gl_rexpire <= CurrentTime) \
86 /* Record has expired, so free the G-line */ \
88 /* See if we need to expire the G-line */ \
89 else if (((gl)->gl_expire > CurrentTime || \
90 ((gl)->gl_flags &= ~GLINE_ACTIVE)) && 0) \
91 ; /* empty statement */ \
94 /** Find canonical user and host for a string.
95 * If \a userhost starts with '$', assign \a userhost to *user_p and NULL to *host_p.
96 * Otherwise, if \a userhost contains '@', assign the earlier part of it to *user_p and the rest to *host_p.
97 * Otherwise, assign \a def_user to *user_p and \a userhost to *host_p.
99 * @param[in] userhost Input string from user.
100 * @param[out] user_p Gets pointer to user (or channel/realname) part of hostmask.
101 * @param[out] host_p Gets point to host part of hostmask (may be assigned NULL).
102 * @param[in] def_user Default value for user part.
105 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
109 if (*userhost == '$') {
115 if (!(tmp = strchr(userhost, '@'))) {
125 /** Create a Gline structure.
126 * @param[in] user User part of mask.
127 * @param[in] host Host part of mask (NULL if not applicable).
128 * @param[in] reason Reason for G-line.
129 * @param[in] expire Expiration timestamp.
130 * @param[in] lastmod Last modification timestamp.
131 * @param[in] flags Bitwise combination of GLINE_* bits.
132 * @return Newly allocated G-line.
134 static struct Gline *
135 make_gline(char *user, char *host, char *reason, time_t expire, time_t lastmod,
138 struct Gline *gline, *sgline, *after = 0;
140 if (!(flags & GLINE_BADCHAN)) { /* search for overlapping glines first */
142 for (gline = GlobalGlineList; gline; gline = sgline) {
143 sgline = gline->gl_next;
145 if (gline->gl_expire <= CurrentTime)
147 else if (((gline->gl_flags & GLINE_LOCAL) != (flags & GLINE_LOCAL)) ||
148 (gline->gl_host && !host) || (!gline->gl_host && host))
150 else if (!mmatch(gline->gl_user, user) /* gline contains new mask */
151 && (gline->gl_host == NULL || !mmatch(gline->gl_host, host))) {
152 if (expire <= gline->gl_expire) /* will expire before wider gline */
155 after = gline; /* stick new gline after this one */
156 } else if (!mmatch(user, gline->gl_user) /* new mask contains gline */
157 && (gline->gl_host==NULL || !mmatch(host, gline->gl_host))
158 && gline->gl_expire <= expire) /* old expires before new */
159 gline_free(gline); /* save some memory */
163 gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
166 DupString(gline->gl_reason, reason); /* initialize gline... */
167 gline->gl_expire = expire;
168 gline->gl_rexpire = expire;
169 gline->gl_lastmod = lastmod;
170 gline->gl_flags = flags & GLINE_MASK;
172 if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
173 DupString(gline->gl_user, user); /* first, remember channel */
176 gline->gl_next = BadChanGlineList; /* then link it into list */
177 gline->gl_prev_p = &BadChanGlineList;
178 if (BadChanGlineList)
179 BadChanGlineList->gl_prev_p = &gline->gl_next;
180 BadChanGlineList = gline;
182 DupString(gline->gl_user, user); /* remember them... */
184 DupString(gline->gl_host, host);
186 gline->gl_host = NULL;
188 if (*user != '$' && ipmask_parse(host, &gline->gl_addr, &gline->gl_bits))
189 gline->gl_flags |= GLINE_IPMASK;
192 gline->gl_next = after->gl_next;
193 gline->gl_prev_p = &after->gl_next;
195 after->gl_next->gl_prev_p = &gline->gl_next;
196 after->gl_next = gline;
198 gline->gl_next = GlobalGlineList; /* then link it into list */
199 gline->gl_prev_p = &GlobalGlineList;
201 GlobalGlineList->gl_prev_p = &gline->gl_next;
202 GlobalGlineList = gline;
209 /** Check local clients against a new G-line.
210 * If the G-line is inactive or a badchan, return immediately.
211 * Otherwise, if any users match it, disconnect them.
212 * @param[in] cptr Peer connect that sent the G-line.
213 * @param[in] sptr Client that originated the G-line.
214 * @param[in] gline New G-line to check.
215 * @return Zero, unless \a sptr G-lined himself, in which case CPTR_KILLED.
218 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
220 struct Client *acptr;
221 int fd, retval = 0, tval;
223 if (feature_bool(FEAT_DISABLE_GLINES))
224 return 0; /* G-lines are disabled */
226 if (GlineIsBadChan(gline)) /* no action taken on badchan glines */
228 if (!GlineIsActive(gline)) /* no action taken on inactive glines */
231 for (fd = HighestFd; fd >= 0; --fd) {
235 if ((acptr = LocalClientArray[fd])) {
236 if (!cli_user(acptr))
239 if (GlineIsRealName(gline)) { /* Realname Gline */
240 Debug((DEBUG_DEBUG,"Realname Gline: %s %s",(cli_info(acptr)),
242 if (match(gline->gl_user+2, cli_info(acptr)) != 0)
244 Debug((DEBUG_DEBUG,"Matched!"));
245 } else { /* Host/IP gline */
246 if (cli_user(acptr)->username &&
247 match(gline->gl_user, (cli_user(acptr))->username) != 0)
250 if (GlineIsIpMask(gline)) {
251 if (!ipmask_check(&cli_ip(acptr), &gline->gl_addr, gline->gl_bits))
255 if (match(gline->gl_host, cli_sockhost(acptr)) != 0)
260 /* ok, here's one that got G-lined */
261 send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
264 /* let the ops know about it */
265 sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
266 get_client_name(acptr, SHOW_IP));
268 /* and get rid of him */
269 if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
271 retval = tval; /* retain killed status */
278 * Implements the mask checking applied to local G-lines.
279 * Basically, host masks must have a minimum of two non-wild domain
280 * fields, and IP masks must have a minimum of 16 bits. If the mask
281 * has even one wild-card, OVERRIDABLE is returned, assuming the other
282 * check doesn't fail.
283 * @param[in] mask G-line mask to check.
284 * @return One of CHECK_REJECTED, CHECK_OVERRIDABLE, or CHECK_APPROVED.
287 gline_checkmask(char *mask)
289 unsigned int flags = MASK_IP;
290 unsigned int dots = 0;
291 unsigned int ipmask = 0;
293 for (; *mask; mask++) { /* go through given mask */
294 if (*mask == '.') { /* it's a separator; advance positional wilds */
295 flags = (flags & ~MASK_WILD_MASK) | ((flags << 1) & MASK_WILD_MASK);
298 if ((flags & (MASK_IP | MASK_WILDS)) == MASK_IP)
299 ipmask += 8; /* It's an IP with no wilds, count bits */
300 } else if (*mask == '*' || *mask == '?')
301 flags |= MASK_WILD_0 | MASK_WILDS; /* found a wildcard */
302 else if (*mask == '/') { /* n.n.n.n/n notation; parse bit specifier */
304 ipmask = strtoul(mask, &mask, 10);
306 /* sanity-check to date */
307 if (*mask || (flags & (MASK_WILDS | MASK_IP)) != MASK_IP)
308 return CHECK_REJECTED;
311 return CHECK_REJECTED;
315 if (dots != 3 || ipmask > 32)
316 return CHECK_REJECTED;
321 flags |= MASK_HALT; /* Halt the ipmask calculation */
322 break; /* get out of the loop */
323 } else if (!IsIP6Char(*mask)) {
324 flags &= ~MASK_IP; /* not an IP anymore! */
329 /* Sanity-check quads */
330 if (dots > 3 || (!(flags & MASK_WILDS) && dots < 3)) {
335 /* update bit count if necessary */
336 if ((flags & (MASK_IP | MASK_WILDS | MASK_HALT)) == MASK_IP)
339 /* Check to see that it's not too wide of a mask */
340 if (flags & MASK_WILDS &&
341 ((!(flags & MASK_IP) && (dots < 2 || flags & MASK_WILD_MASK)) ||
342 (flags & MASK_IP && ipmask < 16)))
343 return CHECK_REJECTED; /* to wide, reject */
345 /* Ok, it's approved; require override if it has wildcards, though */
346 return flags & MASK_WILDS ? CHECK_OVERRIDABLE : CHECK_APPROVED;
349 /** Forward a G-line to other servers.
350 * @param[in] cptr Client that sent us the G-line.
351 * @param[in] sptr Client that originated the G-line.
352 * @param[in] gline G-line to forward.
356 gline_propagate(struct Client *cptr, struct Client *sptr, struct Gline *gline)
358 if (GlineIsLocal(gline) || (IsUser(sptr) && !gline->gl_lastmod))
361 if (gline->gl_lastmod)
362 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
363 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
364 gline->gl_host ? "@" : "",
365 gline->gl_host ? gline->gl_host : "",
366 gline->gl_expire - CurrentTime, gline->gl_lastmod,
369 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
370 (GlineIsRemActive(gline) ?
371 "* +%s%s%s %Tu :%s" : "* -%s%s%s"),
373 gline->gl_host ? "@" : "",
374 gline->gl_host ? gline->gl_host : "",
375 gline->gl_expire - CurrentTime, gline->gl_reason);
380 /** Create a new G-line and add it to global lists.
381 * \a userhost may be in one of four forms:
382 * \li A channel name, to add a BadChan.
383 * \li A string starting with $R and followed by a mask to match against their realname.
384 * \li A user\@IP mask (user\@ part optional) to create an IP-based ban.
385 * \li A user\@host mask (user\@ part optional) to create a hostname ban.
387 * @param[in] cptr Client that sent us the G-line.
388 * @param[in] sptr Client that originated the G-line.
389 * @param[in] userhost Text mask for the G-line.
390 * @param[in] reason Reason for G-line.
391 * @param[in] expire Duration of G-line in seconds.
392 * @param[in] lastmod Last modification time of G-line.
393 * @param[in] flags Bitwise combination of GLINE_* flags.
394 * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
397 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
398 char *reason, time_t expire, time_t lastmod, unsigned int flags)
400 struct Gline *agline;
401 char uhmask[USERLEN + HOSTLEN + 2];
405 assert(0 != userhost);
408 if (*userhost == '#' || *userhost == '&') {
409 if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
410 return send_reply(sptr, ERR_NOPRIVILEGES);
412 flags |= GLINE_BADCHAN;
415 } else if (*userhost == '$') {
416 switch (*userhost == '$' ? userhost[1] : userhost[3]) {
417 case 'R': flags |= GLINE_REALNAME; break;
419 /* uh, what to do here? */
420 /* The answer, my dear Watson, is we throw a protocol_violation()
423 return protocol_violation(sptr,"%s has been smoking the sweet leaf and sent me a whacky gline",cli_name(sptr));
425 sendto_opmask_butone(NULL, SNO_GLINE, "%s has been smoking the sweet leaf and sent me a whacky gline", cli_name(sptr));
430 user = (*userhost =='$' ? userhost : userhost+2);
433 canon_userhost(userhost, &user, &host, "*");
435 ircd_snprintf(0, uhmask, sizeof(uhmask), "%s@%s", user, host))
436 return send_reply(sptr, ERR_LONGMASK);
437 else if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
438 switch (gline_checkmask(host)) {
439 case CHECK_OVERRIDABLE: /* oper overrided restriction */
440 if (flags & GLINE_OPERFORCE)
444 return send_reply(sptr, ERR_MASKTOOWIDE, uhmask);
448 if ((tmp = count_users(uhmask)) >=
449 feature_int(FEAT_GLINEMAXUSERCOUNT) && !(flags & GLINE_OPERFORCE))
450 return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
455 * You cannot set a negative (or zero) expire time, nor can you set an
456 * expiration time for greater than GLINE_MAX_EXPIRE.
458 if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
459 if (!IsServer(sptr) && MyConnect(sptr))
460 send_reply(sptr, ERR_BADEXPIRE, expire);
464 expire += CurrentTime; /* convert from lifetime to timestamp */
467 sendto_opmask_butone(0, ircd_strncmp(reason, "AUTO", 4) ? SNO_GLINE :
468 SNO_AUTO, "%s adding %s %s for %s%s%s, expiring at "
470 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
472 cli_name((cli_user(sptr))->server),
473 (flags & GLINE_LOCAL) ? "local" : "global",
474 (flags & GLINE_BADCHAN) ? "BADCHAN" : "GLINE", user,
475 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : "@",
476 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : host,
477 expire + TSoffset, reason);
480 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
481 "%#C adding %s %s for %s%s%s, expiring at %Tu: %s", sptr,
482 flags & GLINE_LOCAL ? "local" : "global",
483 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", user,
484 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : "@",
485 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : host,
486 expire + TSoffset, reason);
489 agline = make_gline(user, host, reason, expire, lastmod, flags);
491 if (!agline) /* if it overlapped, silently return */
494 gline_propagate(cptr, sptr, agline);
496 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
499 /** Activate a currently inactive G-line.
500 * @param[in] cptr Peer that told us to activate the G-line.
501 * @param[in] sptr Client that originally thought it was a good idea.
502 * @param[in] gline G-line to activate.
503 * @param[in] lastmod New value for last modification timestamp.
504 * @param[in] flags 0 if the activation should be propagated, GLINE_LOCAL if not.
505 * @return Zero, unless \a sptr had a death wish (in which case CPTR_KILLED).
508 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
509 time_t lastmod, unsigned int flags)
511 unsigned int saveflags = 0;
515 saveflags = gline->gl_flags;
517 if (flags & GLINE_LOCAL)
518 gline->gl_flags &= ~GLINE_LDEACT;
520 gline->gl_flags |= GLINE_ACTIVE;
522 if (gline->gl_lastmod) {
523 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
526 gline->gl_lastmod = lastmod;
530 if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
531 return 0; /* was active to begin with */
533 /* Inform ops and log it */
534 sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
535 "expiring at %Tu: %s",
536 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
538 cli_name((cli_user(sptr))->server),
539 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
540 gline->gl_user, gline->gl_host ? "@" : "",
541 gline->gl_host ? gline->gl_host : "",
542 gline->gl_expire + TSoffset, gline->gl_reason);
544 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
545 "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
546 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
547 gline->gl_host ? "@" : "",
548 gline->gl_host ? gline->gl_host : "",
549 gline->gl_expire + TSoffset, gline->gl_reason);
551 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
552 gline_propagate(cptr, sptr, gline);
554 return do_gline(cptr, sptr, gline);
557 /** Deactivate a G-line.
558 * @param[in] cptr Peer that gave us the message.
559 * @param[in] sptr Client that initiated the deactivation.
560 * @param[in] gline G-line to deactivate.
561 * @param[in] lastmod New value for G-line last modification timestamp.
562 * @param[in] flags GLINE_LOCAL to only deactivate locally, 0 to propagate.
566 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
567 time_t lastmod, unsigned int flags)
569 unsigned int saveflags = 0;
574 saveflags = gline->gl_flags;
576 if (GlineIsLocal(gline))
577 msg = "removing local";
578 else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
579 msg = "removing global";
580 gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
582 msg = "deactivating global";
584 if (flags & GLINE_LOCAL)
585 gline->gl_flags |= GLINE_LDEACT;
587 gline->gl_flags &= ~GLINE_ACTIVE;
589 if (gline->gl_lastmod) {
590 if (gline->gl_lastmod >= lastmod)
593 gline->gl_lastmod = lastmod;
597 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
598 return 0; /* was inactive to begin with */
601 /* Inform ops and log it */
602 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
604 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
606 cli_name((cli_user(sptr))->server),
607 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
608 gline->gl_user, gline->gl_host ? "@" : "",
609 gline->gl_host ? gline->gl_host : "",
610 gline->gl_expire + TSoffset, gline->gl_reason);
612 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
613 "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
614 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
615 gline->gl_host ? "@" : "",
616 gline->gl_host ? gline->gl_host : "",
617 gline->gl_expire + TSoffset, gline->gl_reason);
619 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
620 gline_propagate(cptr, sptr, gline);
622 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
623 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
624 gline_free(gline); /* get rid of it */
629 /** Find a G-line for a particular mask, guided by certain flags.
630 * Certain bits in \a flags are interpreted specially:
632 * <dt>GLINE_ANY</dt><dd>Search both BadChans and user G-lines.</dd>
633 * <dt>GLINE_BADCHAN</dt><dd>Search BadChans.</dd>
634 * <dt>GLINE_GLOBAL</dt><dd>Only match global G-lines.</dd>
635 * <dt>GLINE_LOCAL</dt><dd>Only match local G-lines.</dd>
636 * <dt>GLINE_LASTMOD</dt><dd>Only match G-lines with a last modification time.</dd>
637 * <dt>GLINE_EXACT</dt><dd>Require an exact match of G-line mask.</dd>
638 * <dt>anything else</dt><dd>Search user G-lines.</dd>
640 * @param[in] userhost Mask to search for.
641 * @param[in] flags Bitwise combination of GLINE_* flags.
642 * @return First matching G-line, or NULL if none are found.
645 gline_find(char *userhost, unsigned int flags)
647 struct Gline *gline = 0;
648 struct Gline *sgline;
649 char *user, *host, *t_uh;
651 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
652 gliter(BadChanGlineList, gline, sgline) {
653 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
654 (flags & GLINE_LOCAL && gline->gl_flags & GLINE_GLOBAL) ||
655 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
657 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
658 match(gline->gl_user, userhost)) == 0)
663 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
664 *userhost == '#' || *userhost == '&')
667 DupString(t_uh, userhost);
668 canon_userhost(t_uh, &user, &host, "*");
670 gliter(GlobalGlineList, gline, sgline) {
671 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
672 (flags & GLINE_LOCAL && gline->gl_flags & GLINE_GLOBAL) ||
673 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
675 else if (flags & GLINE_EXACT) {
676 if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
677 || (!gline->gl_host && !host)) &&
678 (ircd_strcmp(gline->gl_user, user) == 0))
681 if (((gline->gl_host && host && match(gline->gl_host, host) == 0)
682 || (!gline->gl_host && !host)) &&
683 (match(gline->gl_user, user) == 0))
693 /** Find a matching G-line for a user.
694 * @param[in] cptr Client to compare against.
695 * @param[in] flags Bitwise combination of GLINE_GLOBAL and/or
696 * GLINE_LASTMOD to limit matches.
697 * @return Matching G-line, or NULL if none are found.
700 gline_lookup(struct Client *cptr, unsigned int flags)
703 struct Gline *sgline;
705 gliter(GlobalGlineList, gline, sgline) {
706 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
707 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
710 if (GlineIsRealName(gline)) {
711 Debug((DEBUG_DEBUG,"realname gline: '%s' '%s'",gline->gl_user,cli_info(cptr)));
712 if (match(gline->gl_user+2, cli_info(cptr)) != 0)
716 if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
719 if (GlineIsIpMask(gline)) {
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 gliter(GlobalGlineList, gline, sgline) {
766 if (!GlineIsLocal(gline) && gline->gl_lastmod)
767 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
768 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
769 gline->gl_host ? "@" : "",
770 gline->gl_host ? gline->gl_host : "",
771 gline->gl_expire - CurrentTime, gline->gl_lastmod,
775 gliter(BadChanGlineList, gline, sgline) {
776 if (!GlineIsLocal(gline) && gline->gl_lastmod)
777 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
778 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
779 gline->gl_expire - CurrentTime, gline->gl_lastmod,
784 /** Send a G-line to another server.
785 * @param[in] cptr Who to inform of the G-line.
786 * @param[in] gline G-line to send.
790 gline_resend(struct Client *cptr, struct Gline *gline)
792 if (GlineIsLocal(gline) || !gline->gl_lastmod)
795 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
796 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
797 gline->gl_host ? "@" : "",
798 gline->gl_host ? gline->gl_host : "",
799 gline->gl_expire - CurrentTime, gline->gl_lastmod,
805 /** Display one or all G-lines to a user.
806 * If \a userhost is not NULL, only send the first matching G-line.
807 * Otherwise send the whole list.
808 * @param[in] sptr User asking for G-line list.
809 * @param[in] userhost G-line mask to search for (or NULL).
813 gline_list(struct Client *sptr, char *userhost)
816 struct Gline *sgline;
819 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
820 return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
822 /* send gline information along */
823 send_reply(sptr, RPL_GLIST, gline->gl_user,
824 gline->gl_host ? "@" : "",
825 gline->gl_host ? gline->gl_host : "",
826 gline->gl_expire + TSoffset,
827 GlineIsLocal(gline) ? cli_name(&me) : "*",
828 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
830 gliter(GlobalGlineList, gline, sgline) {
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);
839 gliter(BadChanGlineList, gline, sgline) {
840 send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
841 gline->gl_expire + TSoffset,
842 GlineIsLocal(gline) ? cli_name(&me) : "*",
843 GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
847 /* end of gline information */
848 return send_reply(sptr, RPL_ENDOFGLIST);
851 /** Statistics callback to list G-lines.
852 * @param[in] sptr Client requesting statistics.
853 * @param[in] sd Stats descriptor for request (ignored).
854 * @param[in] param Extra parameter from user (ignored).
857 gline_stats(struct Client *sptr, const struct StatDesc *sd,
861 struct Gline *sgline;
863 gliter(GlobalGlineList, gline, sgline) {
864 send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user,
865 gline->gl_host ? "@" : "",
866 gline->gl_host ? gline->gl_host : "",
867 gline->gl_expire + TSoffset,
868 GlineIsActive(gline) ? '+' : '-',
873 /** Calculate memory used by G-lines.
874 * @param[out] gl_size Number of bytes used by G-lines.
875 * @return Number of G-lines in use.
878 gline_memory_count(size_t *gl_size)
883 for (gline = GlobalGlineList; gline; gline = gline->gl_next)
886 *gl_size += sizeof(struct Gline);
887 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
888 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
889 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;