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 /* There is some subtlety here with the boolean operators:
80 * (x || 1) is used to continue in a logical-and series even when !x.
81 * (x && 0) is used to continue in a logical-or series even when x.
83 #define gliter(list, gl, next) \
84 /* Iterate through the G-lines in the list */ \
85 for ((gl) = (list); (gl); (gl) = (next)) \
86 /* Figure out the next pointer in list... */ \
87 if ((((next) = (gl)->gl_next) || 1) && \
88 /* Then see if it's expired */ \
89 (gl)->gl_lifetime <= CurrentTime) \
90 /* Record has expired, so free the G-line */ \
92 /* See if we need to expire the G-line */ \
93 else if ((((gl)->gl_expire > CurrentTime) || \
94 (((gl)->gl_flags &= ~GLINE_ACTIVE) && 0) || \
95 ((gl)->gl_state = GLOCAL_GLOBAL)) && 0) \
96 ; /* empty statement */ \
99 /** Find canonical user and host for a string.
100 * If \a userhost starts with '$', assign \a userhost to *user_p and NULL to *host_p.
101 * Otherwise, if \a userhost contains '@', assign the earlier part of it to *user_p and the rest to *host_p.
102 * Otherwise, assign \a def_user to *user_p and \a userhost to *host_p.
104 * @param[in] userhost Input string from user.
105 * @param[out] user_p Gets pointer to user (or channel/realname) part of hostmask.
106 * @param[out] host_p Gets point to host part of hostmask (may be assigned NULL).
107 * @param[in] def_user Default value for user part.
110 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
114 if (*userhost == '$') {
120 if (!(tmp = strchr(userhost, '@'))) {
130 /** Create a Gline structure.
131 * @param[in] user User part of mask.
132 * @param[in] host Host part of mask (NULL if not applicable).
133 * @param[in] reason Reason for G-line.
134 * @param[in] expire Expiration timestamp.
135 * @param[in] lastmod Last modification timestamp.
136 * @param[in] flags Bitwise combination of GLINE_* bits.
137 * @return Newly allocated G-line.
139 static struct Gline *
140 make_gline(char *user, char *host, char *reason, time_t expire, time_t lastmod,
141 time_t lifetime, unsigned int flags)
147 gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
150 DupString(gline->gl_reason, reason); /* initialize gline... */
151 gline->gl_expire = expire;
152 gline->gl_lifetime = lifetime;
153 gline->gl_lastmod = lastmod;
154 gline->gl_flags = flags & GLINE_MASK;
155 gline->gl_state = GLOCAL_GLOBAL; /* not locally modified */
157 if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
158 DupString(gline->gl_user, user); /* first, remember channel */
159 gline->gl_host = NULL;
161 gline->gl_next = BadChanGlineList; /* then link it into list */
162 gline->gl_prev_p = &BadChanGlineList;
163 if (BadChanGlineList)
164 BadChanGlineList->gl_prev_p = &gline->gl_next;
165 BadChanGlineList = gline;
167 DupString(gline->gl_user, user); /* remember them... */
169 DupString(gline->gl_host, host);
171 gline->gl_host = NULL;
173 if (*user != '$' && ipmask_parse(host, &gline->gl_addr, &gline->gl_bits))
174 gline->gl_flags |= GLINE_IPMASK;
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;
186 /** Check local clients against a new G-line.
187 * If the G-line is inactive or a badchan, return immediately.
188 * Otherwise, if any users match it, disconnect them.
189 * @param[in] cptr Peer connect that sent the G-line.
190 * @param[in] sptr Client that originated the G-line.
191 * @param[in] gline New G-line to check.
192 * @return Zero, unless \a sptr G-lined himself, in which case CPTR_KILLED.
195 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
197 struct Client *acptr;
198 int fd, retval = 0, tval;
200 if (feature_bool(FEAT_DISABLE_GLINES))
201 return 0; /* G-lines are disabled */
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)) {
228 if (!ipmask_check(&cli_ip(acptr), &gline->gl_addr, gline->gl_bits))
232 if (match(gline->gl_host, cli_sockhost(acptr)) != 0)
237 /* ok, here's one that got G-lined */
238 send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
241 /* let the ops know about it */
242 sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
243 get_client_name(acptr, SHOW_IP));
245 /* and get rid of him */
246 if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
248 retval = tval; /* retain killed status */
255 * Implements the mask checking applied to local G-lines.
256 * Basically, host masks must have a minimum of two non-wild domain
257 * fields, and IP masks must have a minimum of 16 bits. If the mask
258 * has even one wild-card, OVERRIDABLE is returned, assuming the other
259 * check doesn't fail.
260 * @param[in] mask G-line mask to check.
261 * @return One of CHECK_REJECTED, CHECK_OVERRIDABLE, or CHECK_APPROVED.
264 gline_checkmask(char *mask)
266 unsigned int flags = MASK_IP;
267 unsigned int dots = 0;
268 unsigned int ipmask = 0;
270 for (; *mask; mask++) { /* go through given mask */
271 if (*mask == '.') { /* it's a separator; advance positional wilds */
272 flags = (flags & ~MASK_WILD_MASK) | ((flags << 1) & MASK_WILD_MASK);
275 if ((flags & (MASK_IP | MASK_WILDS)) == MASK_IP)
276 ipmask += 8; /* It's an IP with no wilds, count bits */
277 } else if (*mask == '*' || *mask == '?')
278 flags |= MASK_WILD_0 | MASK_WILDS; /* found a wildcard */
279 else if (*mask == '/') { /* n.n.n.n/n notation; parse bit specifier */
281 ipmask = strtoul(mask, &mask, 10);
283 /* sanity-check to date */
284 if (*mask || (flags & (MASK_WILDS | MASK_IP)) != MASK_IP)
285 return CHECK_REJECTED;
288 return CHECK_REJECTED;
292 if (dots != 3 || ipmask > 32)
293 return CHECK_REJECTED;
298 flags |= MASK_HALT; /* Halt the ipmask calculation */
299 break; /* get out of the loop */
300 } else if (!IsIP6Char(*mask)) {
301 flags &= ~MASK_IP; /* not an IP anymore! */
306 /* Sanity-check quads */
307 if (dots > 3 || (!(flags & MASK_WILDS) && dots < 3)) {
312 /* update bit count if necessary */
313 if ((flags & (MASK_IP | MASK_WILDS | MASK_HALT)) == MASK_IP)
316 /* Check to see that it's not too wide of a mask */
317 if (flags & MASK_WILDS &&
318 ((!(flags & MASK_IP) && (dots < 2 || flags & MASK_WILD_MASK)) ||
319 (flags & MASK_IP && ipmask < 16)))
320 return CHECK_REJECTED; /* to wide, reject */
322 /* Ok, it's approved; require override if it has wildcards, though */
323 return flags & MASK_WILDS ? CHECK_OVERRIDABLE : CHECK_APPROVED;
326 /** Forward a G-line to other servers.
327 * @param[in] cptr Client that sent us the G-line.
328 * @param[in] sptr Client that originated the G-line.
329 * @param[in] gline G-line to forward.
333 gline_propagate(struct Client *cptr, struct Client *sptr, struct Gline *gline)
335 if (GlineIsLocal(gline))
338 assert(gline->gl_lastmod);
340 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
341 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
342 gline->gl_host ? "@" : "",
343 gline->gl_host ? gline->gl_host : "",
344 gline->gl_expire - CurrentTime, gline->gl_lastmod,
345 gline->gl_lifetime, gline->gl_reason);
350 /** Count number of users who match \a mask.
351 * @param[in] mask user\@host or user\@ip mask to check.
352 * @param[in] flags Bitmask possibly containing the value GLINE_LOCAL, to limit searches to this server.
353 * @return Count of matching users.
356 count_users(char *mask, int flags)
358 struct irc_in_addr ipmask;
359 struct Client *acptr;
362 char namebuf[USERLEN + HOSTLEN + 2];
363 char ipbuf[USERLEN + SOCKIPLEN + 2];
364 unsigned char ipmask_len;
366 ipmask_valid = ipmask_parse(mask, &ipmask, &ipmask_len);
367 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
370 if ((flags & GLINE_LOCAL) && !MyConnect(acptr))
373 ircd_snprintf(0, namebuf, sizeof(namebuf), "%s@%s",
374 cli_user(acptr)->username, cli_user(acptr)->host);
375 ircd_snprintf(0, ipbuf, sizeof(ipbuf), "%s@%s", cli_user(acptr)->username,
376 ircd_ntoa(&cli_ip(acptr)));
378 if (!match(mask, namebuf)
379 || !match(mask, ipbuf)
380 || (ipmask_valid && ipmask_check(&cli_ip(acptr), &ipmask, ipmask_len)))
387 /** Count number of users with a realname matching \a mask.
388 * @param[in] mask Wildcard mask to match against realnames.
389 * @return Count of matching users.
392 count_realnames(const char *mask)
394 struct Client *acptr;
400 matchcomp(cmask, &minlen, NULL, mask);
401 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
404 if (strlen(cli_info(acptr)) < minlen)
406 if (!matchexec(cli_info(acptr), cmask, minlen))
412 /** Create a new G-line and add it to global lists.
413 * \a userhost may be in one of four forms:
414 * \li A channel name, to add a BadChan.
415 * \li A string starting with $R and followed by a mask to match against their realname.
416 * \li A user\@IP mask (user\@ part optional) to create an IP-based ban.
417 * \li A user\@host mask (user\@ part optional) to create a hostname ban.
419 * @param[in] cptr Client that sent us the G-line.
420 * @param[in] sptr Client that originated the G-line.
421 * @param[in] userhost Text mask for the G-line.
422 * @param[in] reason Reason for G-line.
423 * @param[in] expire Expiration time of G-line.
424 * @param[in] lastmod Last modification time of G-line.
425 * @param[in] lifetime Lifetime of G-line.
426 * @param[in] flags Bitwise combination of GLINE_* flags.
427 * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
430 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
431 char *reason, time_t expire, time_t lastmod, time_t lifetime,
434 struct Gline *agline;
435 char uhmask[USERLEN + HOSTLEN + 2];
439 assert(0 != userhost);
441 assert(((flags & (GLINE_GLOBAL | GLINE_LOCAL)) == GLINE_GLOBAL) ||
442 ((flags & (GLINE_GLOBAL | GLINE_LOCAL)) == GLINE_LOCAL));
444 Debug((DEBUG_DEBUG, "gline_add(\"%s\", \"%s\", \"%s\", \"%s\", %Tu, %Tu "
445 "%Tu, 0x%04x)", cli_name(cptr), cli_name(sptr), userhost, reason,
446 expire, lastmod, lifetime, flags));
448 if (*userhost == '#' || *userhost == '&') {
449 if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
450 return send_reply(sptr, ERR_NOPRIVILEGES);
452 flags |= GLINE_BADCHAN;
455 } else if (*userhost == '$') {
456 switch (userhost[1]) {
457 case 'R': flags |= GLINE_REALNAME; break;
459 /* uh, what to do here? */
460 /* The answer, my dear Watson, is we throw a protocol_violation()
463 return protocol_violation(sptr,"%s has been smoking the sweet leaf and sent me a whacky gline",cli_name(sptr));
464 sendto_opmask_butone(NULL, SNO_GLINE, "%s has been smoking the sweet leaf and sent me a whacky gline", cli_name(sptr));
469 if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
470 tmp = count_realnames(userhost + 2);
471 if ((tmp >= feature_int(FEAT_GLINEMAXUSERCOUNT))
472 && !(flags & GLINE_OPERFORCE))
473 return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
476 canon_userhost(userhost, &user, &host, "*");
478 ircd_snprintf(0, uhmask, sizeof(uhmask), "%s@%s", user, host))
479 return send_reply(sptr, ERR_LONGMASK);
480 else if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
481 switch (gline_checkmask(host)) {
482 case CHECK_OVERRIDABLE: /* oper overrided restriction */
483 if (flags & GLINE_OPERFORCE)
487 return send_reply(sptr, ERR_MASKTOOWIDE, uhmask);
491 if ((tmp = count_users(uhmask, flags)) >=
492 feature_int(FEAT_GLINEMAXUSERCOUNT) && !(flags & GLINE_OPERFORCE))
493 return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
498 * You cannot set a negative (or zero) expire time, nor can you set an
499 * expiration time for greater than GLINE_MAX_EXPIRE.
501 if (!(flags & GLINE_FORCE) &&
502 (expire <= CurrentTime || expire > CurrentTime + GLINE_MAX_EXPIRE)) {
503 if (!IsServer(sptr) && MyConnect(sptr))
504 send_reply(sptr, ERR_BADEXPIRE, expire);
508 if (!lifetime) /* no lifetime set, use expiration time */
511 /* lifetime is already an absolute timestamp */
514 sendto_opmask_butone(0, ircd_strncmp(reason, "AUTO", 4) ? SNO_GLINE :
515 SNO_AUTO, "%s adding %s %s for %s%s%s, expiring at "
517 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
519 cli_name((cli_user(sptr))->server),
520 (flags & GLINE_LOCAL) ? "local" : "global",
521 (flags & GLINE_BADCHAN) ? "BADCHAN" : "GLINE", user,
522 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : "@",
523 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : host,
524 expire + TSoffset, reason);
527 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
528 "%#C adding %s %s for %s%s%s, expiring at %Tu: %s", sptr,
529 flags & GLINE_LOCAL ? "local" : "global",
530 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", user,
531 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : "@",
532 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : host,
533 expire + TSoffset, reason);
536 agline = make_gline(user, host, reason, expire, lastmod, lifetime, flags);
538 /* since we've disabled overlapped G-line checking, agline should
543 gline_propagate(cptr, sptr, agline);
545 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
548 /** Activate a currently inactive G-line.
549 * @param[in] cptr Peer that told us to activate the G-line.
550 * @param[in] sptr Client that originally thought it was a good idea.
551 * @param[in] gline G-line to activate.
552 * @param[in] lastmod New value for last modification timestamp.
553 * @param[in] flags 0 if the activation should be propagated, GLINE_LOCAL if not.
554 * @return Zero, unless \a sptr had a death wish (in which case CPTR_KILLED).
557 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
558 time_t lastmod, unsigned int flags)
560 unsigned int saveflags = 0;
564 saveflags = gline->gl_flags;
566 if (flags & GLINE_LOCAL)
567 gline->gl_flags &= ~GLINE_LDEACT;
569 gline->gl_flags |= GLINE_ACTIVE;
571 if (gline->gl_lastmod) {
572 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
575 gline->gl_lastmod = lastmod;
579 if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
580 return 0; /* was active to begin with */
582 /* Inform ops and log it */
583 sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
584 "expiring at %Tu: %s",
585 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
587 cli_name((cli_user(sptr))->server),
588 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
589 gline->gl_user, gline->gl_host ? "@" : "",
590 gline->gl_host ? gline->gl_host : "",
591 gline->gl_expire + TSoffset, gline->gl_reason);
593 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
594 "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
595 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
596 gline->gl_host ? "@" : "",
597 gline->gl_host ? gline->gl_host : "",
598 gline->gl_expire + TSoffset, gline->gl_reason);
600 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
601 gline_propagate(cptr, sptr, gline);
603 return do_gline(cptr, sptr, gline);
606 /** Deactivate a G-line.
607 * @param[in] cptr Peer that gave us the message.
608 * @param[in] sptr Client that initiated the deactivation.
609 * @param[in] gline G-line to deactivate.
610 * @param[in] lastmod New value for G-line last modification timestamp.
611 * @param[in] flags GLINE_LOCAL to only deactivate locally, 0 to propagate.
615 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
616 time_t lastmod, unsigned int flags)
618 unsigned int saveflags = 0;
623 saveflags = gline->gl_flags;
625 if (GlineIsLocal(gline))
626 msg = "removing local";
627 else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
628 msg = "removing global";
629 gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
631 msg = "deactivating global";
633 if (flags & GLINE_LOCAL)
634 gline->gl_flags |= GLINE_LDEACT;
636 gline->gl_flags &= ~GLINE_ACTIVE;
638 if (gline->gl_lastmod) {
639 if (gline->gl_lastmod >= lastmod)
642 gline->gl_lastmod = lastmod;
646 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
647 return 0; /* was inactive to begin with */
650 /* Inform ops and log it */
651 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
653 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
655 cli_name((cli_user(sptr))->server),
656 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
657 gline->gl_user, gline->gl_host ? "@" : "",
658 gline->gl_host ? gline->gl_host : "",
659 gline->gl_expire + TSoffset, gline->gl_reason);
661 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
662 "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
663 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
664 gline->gl_host ? "@" : "",
665 gline->gl_host ? gline->gl_host : "",
666 gline->gl_expire + TSoffset, gline->gl_reason);
668 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
669 gline_propagate(cptr, sptr, gline);
671 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
672 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
673 gline_free(gline); /* get rid of it */
678 /** Send a deactivation request for a locally unknown G-line.
679 * @param[in] cptr Client that sent us the G-line modification.
680 * @param[in] sptr Client that originated the G-line modification.
681 * @param[in] userhost Text representation of G-line target.
682 * @param[in] expire Expiration time of G-line.
683 * @param[in] lastmod Last modification time of G-line.
684 * @param[in] lifetime Lifetime of G-line.
685 * @param[in] flags Bitwise combination of GLINE_* flags.
689 gline_forward_deactivation(struct Client *cptr, struct Client *sptr,
690 char *userhost, time_t expire, time_t lastmod,
691 time_t lifetime, unsigned int flags)
693 char *msg = "deactivating unknown global";
698 /* Inform ops and log it */
699 sendto_opmask_butone(0, SNO_GLINE, "%s %s GLINE for %s, expiring at %Tu",
700 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
701 cli_name(sptr) : cli_name((cli_user(sptr))->server),
702 msg, userhost, expire + TSoffset);
704 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
705 "%#C %s GLINE for %s, expiring at %Tu", sptr, msg, userhost,
708 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* -%s %Tu %Tu %Tu",
709 userhost, expire, lastmod, lifetime);
714 /** Modify a global G-line.
715 * @param[in] cptr Client that sent us the G-line modification.
716 * @param[in] sptr Client that originated the G-line modification.
717 * @param[in] gline G-line being modified.
718 * @param[in] action Resultant status of the G-line.
719 * @param[in] reason Reason for G-line.
720 * @param[in] expire Expiration time of G-line.
721 * @param[in] lastmod Last modification time of G-line.
722 * @param[in] lifetime Lifetime of G-line.
723 * @param[in] flags Bitwise combination of GLINE_* flags.
724 * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
727 gline_modify(struct Client *cptr, struct Client *sptr, struct Gline *gline,
728 enum GlineAction action, char *reason, time_t expire,
729 time_t lastmod, time_t lifetime, unsigned int flags)
731 char buf[BUFSIZE], *op = "";
735 assert(!GlineIsLocal(gline));
737 Debug((DEBUG_DEBUG, "gline_modify(\"%s\", \"%s\", \"%s%s%s\", %s, \"%s\", "
738 "%Tu, %Tu, %Tu, 0x%04x)", cli_name(cptr), cli_name(sptr),
739 gline->gl_user, gline->gl_host ? "@" : "",
740 gline->gl_host ? gline->gl_host : "",
741 action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
742 (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
743 (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
744 (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
745 (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>")))),
746 reason, expire, lastmod, lifetime, flags));
748 /* First, let's check lastmod... */
749 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE) {
750 if (GlineLastMod(gline) > lastmod) { /* we have a more recent version */
751 if (IsBurstOrBurstAck(cptr))
752 return 0; /* middle of a burst, it'll resync on its own */
753 return gline_resend(cptr, gline); /* resync the server */
754 } else if (GlineLastMod(gline) == lastmod)
755 return 0; /* we have that version of the G-line... */
758 /* All right, we know that there's a change of some sort. What is it? */
759 /* first, check out the expiration time... */
760 if ((flags & GLINE_EXPIRE) && expire) {
761 if (!(flags & GLINE_FORCE) &&
762 (expire <= CurrentTime || expire > CurrentTime + GLINE_MAX_EXPIRE)) {
763 if (!IsServer(sptr) && MyConnect(sptr)) /* bad expiration time */
764 send_reply(sptr, ERR_BADEXPIRE, expire);
768 flags &= ~GLINE_EXPIRE;
770 /* Now check to see if there's any change... */
771 if ((flags & GLINE_EXPIRE) && expire == gline->gl_expire) {
772 flags &= ~GLINE_EXPIRE; /* no change to expiration time... */
776 /* Next, check out lifetime--this one's a bit trickier... */
777 if (!(flags & GLINE_LIFETIME) || !lifetime)
778 lifetime = gline->gl_lifetime; /* use G-line lifetime */
780 lifetime = IRCD_MAX(lifetime, expire); /* set lifetime to the max */
782 /* OK, let's see which is greater... */
783 if (lifetime > gline->gl_lifetime)
784 flags |= GLINE_LIFETIME; /* have to update lifetime */
786 flags &= ~GLINE_LIFETIME; /* no change to lifetime */
790 /* Finally, let's see if the reason needs to be updated */
791 if ((flags & GLINE_REASON) && reason &&
792 !ircd_strcmp(gline->gl_reason, reason))
793 flags &= ~GLINE_REASON; /* no changes to the reason */
795 /* OK, now let's take a look at the action... */
796 if ((action == GLINE_ACTIVATE && (gline->gl_flags & GLINE_ACTIVE)) ||
797 (action == GLINE_DEACTIVATE && !(gline->gl_flags & GLINE_ACTIVE)) ||
798 (action == GLINE_LOCAL_ACTIVATE &&
799 (gline->gl_state == GLOCAL_ACTIVATED)) ||
800 (action == GLINE_LOCAL_DEACTIVATE &&
801 (gline->gl_state == GLOCAL_DEACTIVATED)) ||
802 /* can't activate an expired G-line */
803 IRCD_MAX(gline->gl_expire, expire) <= CurrentTime)
804 action = GLINE_MODIFY; /* no activity state modifications */
806 Debug((DEBUG_DEBUG, "About to perform changes; flags 0x%04x, action %s",
807 flags, action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
808 (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
809 (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
810 (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
811 (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>"))))));
813 /* If there are no changes to perform, do no changes */
814 if (!(flags & GLINE_UPDATE) && action == GLINE_MODIFY)
817 /* Now we know what needs to be changed, so let's process the changes... */
819 /* Start by updating lastmod, if indicated... */
820 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
821 gline->gl_lastmod = lastmod;
823 /* Then move on to activity status changes... */
825 case GLINE_ACTIVATE: /* Globally activating G-line */
826 gline->gl_flags |= GLINE_ACTIVE; /* make it active... */
827 gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
828 pos += ircd_snprintf(0, buf, sizeof(buf), " globally activating G-line");
829 op = "+"; /* operation for G-line propagation */
832 case GLINE_DEACTIVATE: /* Globally deactivating G-line */
833 gline->gl_flags &= ~GLINE_ACTIVE; /* make it inactive... */
834 gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
835 pos += ircd_snprintf(0, buf, sizeof(buf), " globally deactivating G-line");
836 op = "-"; /* operation for G-line propagation */
839 case GLINE_LOCAL_ACTIVATE: /* Locally activating G-line */
840 gline->gl_state = GLOCAL_ACTIVATED; /* make it locally active */
841 pos += ircd_snprintf(0, buf, sizeof(buf), " locally activating G-line");
844 case GLINE_LOCAL_DEACTIVATE: /* Locally deactivating G-line */
845 gline->gl_state = GLOCAL_DEACTIVATED; /* make it locally inactive */
846 pos += ircd_snprintf(0, buf, sizeof(buf), " locally deactivating G-line");
849 case GLINE_MODIFY: /* no change to activity status */
853 /* Handle expiration changes... */
854 if (flags & GLINE_EXPIRE) {
855 gline->gl_expire = expire; /* save new expiration time */
857 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
858 "%s%s changing expiration time to %Tu",
860 pos && !(flags & (GLINE_LIFETIME | GLINE_REASON)) ?
861 " and" : "", expire);
864 /* Next, handle lifetime changes... */
865 if (flags & GLINE_LIFETIME) {
866 gline->gl_lifetime = lifetime; /* save new lifetime */
868 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
869 "%s%s extending record lifetime to %Tu",
870 pos ? ";" : "", pos && !(flags & GLINE_REASON) ?
871 " and" : "", lifetime);
874 /* Now, handle reason changes... */
875 if (flags & GLINE_REASON) {
876 MyFree(gline->gl_reason); /* release old reason */
877 DupString(gline->gl_reason, reason); /* store new reason */
879 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
880 "%s%s changing reason to \"%s\"",
881 pos ? ";" : "", pos ? " and" : "", reason);
884 /* All right, inform ops... */
885 sendto_opmask_butone(0, SNO_GLINE, "%s modifying global %s for %s%s%s:%s",
886 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
887 cli_name(sptr) : cli_name((cli_user(sptr))->server),
888 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
889 gline->gl_user, gline->gl_host ? "@" : "",
890 gline->gl_host ? gline->gl_host : "", buf);
892 /* and log the change */
893 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
894 "%#C modifying global %s for %s%s%s:%s", sptr,
895 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
896 gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "",
899 /* We'll be simple for this release, but we can update this to change
900 * the propagation syntax on future updates
902 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
903 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
904 "* %s%s%s%s%s %Tu %Tu %Tu :%s",
905 flags & GLINE_OPERFORCE ? "!" : "", op,
906 gline->gl_user, gline->gl_host ? "@" : "",
907 gline->gl_host ? gline->gl_host : "",
908 gline->gl_expire - CurrentTime, gline->gl_lastmod,
909 gline->gl_lifetime, gline->gl_reason);
911 /* OK, let's do the G-line... */
912 return do_gline(cptr, sptr, gline);
915 /** Destroy a local G-line.
916 * @param[in] cptr Peer that gave us the message.
917 * @param[in] sptr Client that initiated the destruction.
918 * @param[in] gline G-line to destroy.
922 gline_destroy(struct Client *cptr, struct Client *sptr, struct Gline *gline)
925 assert(GlineIsLocal(gline));
927 /* Inform ops and log it */
928 sendto_opmask_butone(0, SNO_GLINE, "%s removing local %s for %s%s%s",
929 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
930 cli_name(sptr) : cli_name((cli_user(sptr))->server),
931 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
932 gline->gl_user, gline->gl_host ? "@" : "",
933 gline->gl_host ? gline->gl_host : "");
934 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
935 "%#C removing local %s for %s%s%s", sptr,
936 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
937 gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "");
939 gline_free(gline); /* get rid of the G-line */
941 return 0; /* convenience return */
944 /** Find a G-line for a particular mask, guided by certain flags.
945 * Certain bits in \a flags are interpreted specially:
947 * <dt>GLINE_ANY</dt><dd>Search both BadChans and user G-lines.</dd>
948 * <dt>GLINE_BADCHAN</dt><dd>Search BadChans.</dd>
949 * <dt>GLINE_GLOBAL</dt><dd>Only match global G-lines.</dd>
950 * <dt>GLINE_LOCAL</dt><dd>Only match local G-lines.</dd>
951 * <dt>GLINE_LASTMOD</dt><dd>Only match G-lines with a last modification time.</dd>
952 * <dt>GLINE_EXACT</dt><dd>Require an exact match of G-line mask.</dd>
953 * <dt>anything else</dt><dd>Search user G-lines.</dd>
955 * @param[in] userhost Mask to search for.
956 * @param[in] flags Bitwise combination of GLINE_* flags.
957 * @return First matching G-line, or NULL if none are found.
960 gline_find(char *userhost, unsigned int flags)
962 struct Gline *gline = 0;
963 struct Gline *sgline;
964 char *user, *host, *t_uh;
966 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
967 gliter(BadChanGlineList, gline, sgline) {
968 if ((flags & (GlineIsLocal(gline) ? GLINE_GLOBAL : GLINE_LOCAL)) ||
969 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
971 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
972 match(gline->gl_user, userhost)) == 0)
977 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
978 *userhost == '#' || *userhost == '&')
981 DupString(t_uh, userhost);
982 canon_userhost(t_uh, &user, &host, "*");
984 gliter(GlobalGlineList, gline, sgline) {
985 if ((flags & (GlineIsLocal(gline) ? GLINE_GLOBAL : GLINE_LOCAL)) ||
986 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
988 else if (flags & GLINE_EXACT) {
989 if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
990 || (!gline->gl_host && !host)) &&
991 (ircd_strcmp(gline->gl_user, user) == 0))
994 if (((gline->gl_host && host && match(gline->gl_host, host) == 0)
995 || (!gline->gl_host && !host)) &&
996 (match(gline->gl_user, user) == 0))
1006 /** Find a matching G-line for a user.
1007 * @param[in] cptr Client to compare against.
1008 * @param[in] flags Bitwise combination of GLINE_GLOBAL and/or
1009 * GLINE_LASTMOD to limit matches.
1010 * @return Matching G-line, or NULL if none are found.
1013 gline_lookup(struct Client *cptr, unsigned int flags)
1015 struct Gline *gline;
1016 struct Gline *sgline;
1018 gliter(GlobalGlineList, gline, sgline) {
1019 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
1020 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
1023 if (GlineIsRealName(gline)) {
1024 Debug((DEBUG_DEBUG,"realname gline: '%s' '%s'",gline->gl_user,cli_info(cptr)));
1025 if (match(gline->gl_user+2, cli_info(cptr)) != 0)
1029 if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
1032 if (GlineIsIpMask(gline)) {
1033 if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
1037 if (match(gline->gl_host, (cli_user(cptr))->realhost) != 0)
1041 if (GlineIsActive(gline))
1050 /** Delink and free a G-line.
1051 * @param[in] gline G-line to free.
1054 gline_free(struct Gline *gline)
1058 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
1060 gline->gl_next->gl_prev_p = gline->gl_prev_p;
1062 MyFree(gline->gl_user); /* free up the memory */
1064 MyFree(gline->gl_host);
1065 MyFree(gline->gl_reason);
1069 /** Burst all known global G-lines to another server.
1070 * @param[in] cptr Destination of burst.
1073 gline_burst(struct Client *cptr)
1075 struct Gline *gline;
1076 struct Gline *sgline;
1078 gliter(GlobalGlineList, gline, sgline) {
1079 if (!GlineIsLocal(gline) && gline->gl_lastmod)
1080 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1081 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1082 gline->gl_host ? "@" : "",
1083 gline->gl_host ? gline->gl_host : "",
1084 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1085 gline->gl_lifetime, gline->gl_reason);
1088 gliter(BadChanGlineList, gline, sgline) {
1089 if (!GlineIsLocal(gline) && gline->gl_lastmod)
1090 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu %Tu :%s",
1091 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1092 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1093 gline->gl_lifetime, gline->gl_reason);
1097 /** Send a G-line to another server.
1098 * @param[in] cptr Who to inform of the G-line.
1099 * @param[in] gline G-line to send.
1103 gline_resend(struct Client *cptr, struct Gline *gline)
1105 if (GlineIsLocal(gline) || !gline->gl_lastmod)
1108 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1109 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1110 gline->gl_host ? "@" : "",
1111 gline->gl_host ? gline->gl_host : "",
1112 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1113 gline->gl_lifetime, gline->gl_reason);
1118 /** Display one or all G-lines to a user.
1119 * If \a userhost is not NULL, only send the first matching G-line.
1120 * Otherwise send the whole list.
1121 * @param[in] sptr User asking for G-line list.
1122 * @param[in] userhost G-line mask to search for (or NULL).
1126 gline_list(struct Client *sptr, char *userhost)
1128 struct Gline *gline;
1129 struct Gline *sgline;
1132 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
1133 return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
1135 /* send gline information along */
1136 send_reply(sptr, RPL_GLIST, gline->gl_user,
1137 gline->gl_host ? "@" : "",
1138 gline->gl_host ? gline->gl_host : "",
1139 gline->gl_expire + TSoffset, gline->gl_lastmod,
1140 gline->gl_lifetime + TSoffset,
1141 GlineIsLocal(gline) ? cli_name(&me) : "*",
1142 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1143 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1144 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1146 gliter(GlobalGlineList, gline, sgline) {
1147 send_reply(sptr, RPL_GLIST, gline->gl_user,
1148 gline->gl_host ? "@" : "",
1149 gline->gl_host ? gline->gl_host : "",
1150 gline->gl_expire + TSoffset, gline->gl_lastmod,
1151 gline->gl_lifetime + TSoffset,
1152 GlineIsLocal(gline) ? cli_name(&me) : "*",
1153 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1154 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1155 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1158 gliter(BadChanGlineList, gline, sgline) {
1159 send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
1160 gline->gl_expire + TSoffset, gline->gl_lastmod,
1161 gline->gl_lifetime + TSoffset,
1162 GlineIsLocal(gline) ? cli_name(&me) : "*",
1163 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1164 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1165 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1169 /* end of gline information */
1170 return send_reply(sptr, RPL_ENDOFGLIST);
1173 /** Statistics callback to list G-lines.
1174 * @param[in] sptr Client requesting statistics.
1175 * @param[in] sd Stats descriptor for request (ignored).
1176 * @param[in] param Extra parameter from user (ignored).
1179 gline_stats(struct Client *sptr, const struct StatDesc *sd,
1182 struct Gline *gline;
1183 struct Gline *sgline;
1185 gliter(GlobalGlineList, gline, sgline) {
1186 send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user,
1187 gline->gl_host ? "@" : "",
1188 gline->gl_host ? gline->gl_host : "",
1189 gline->gl_expire + TSoffset, gline->gl_lastmod,
1190 gline->gl_lifetime + TSoffset,
1191 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1192 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1193 GlineIsRemActive(gline) ? '+' : '-',
1198 /** Calculate memory used by G-lines.
1199 * @param[out] gl_size Number of bytes used by G-lines.
1200 * @return Number of G-lines in use.
1203 gline_memory_count(size_t *gl_size)
1205 struct Gline *gline;
1206 unsigned int gl = 0;
1208 for (gline = GlobalGlineList; gline; gline = gline->gl_next) {
1210 *gl_size += sizeof(struct Gline);
1211 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1212 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1213 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;
1216 for (gline = BadChanGlineList; gline; gline = gline->gl_next) {
1218 *gl_size += sizeof(struct Gline);
1219 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1220 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1221 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;