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.
30 #include "ircd_alloc.h"
31 #include "ircd_features.h"
33 #include "ircd_reply.h"
34 #include "ircd_snprintf.h"
35 #include "ircd_string.h"
49 /* #include <assert.h> -- Now using assert in ircd_log.h */
54 #define CHECK_APPROVED 0 /**< Mask is acceptable */
55 #define CHECK_OVERRIDABLE 1 /**< Mask is acceptable, but not by default */
56 #define CHECK_REJECTED 2 /**< Mask is totally unacceptable */
58 #define MASK_WILD_0 0x01 /**< Wildcards in the last position */
59 #define MASK_WILD_1 0x02 /**< Wildcards in the next-to-last position */
61 #define MASK_WILD_MASK 0x03 /**< Mask out the positional wildcards */
63 #define MASK_WILDS 0x10 /**< Mask contains wildcards */
64 #define MASK_IP 0x20 /**< Mask is an IP address */
65 #define MASK_HALT 0x40 /**< Finished processing mask */
67 /** List of user G-lines. */
68 struct Gline* GlobalGlineList = 0;
69 /** List of BadChan G-lines. */
70 struct Gline* BadChanGlineList = 0;
72 /** Iterate through \a list of G-lines. Use this like a for loop,
73 * i.e., follow it with braces and use whatever you passed as \a gl
74 * as a single G-line to be acted upon.
76 * @param[in] list List of G-lines to iterate over.
77 * @param[in] gl Name of a struct Gline pointer variable that will be made to point to the G-lines in sequence.
78 * @param[in] next Name of a scratch struct Gline pointer variable.
80 /* There is some subtlety here with the boolean operators:
81 * (x || 1) is used to continue in a logical-and series even when !x.
82 * (x && 0) is used to continue in a logical-or series even when x.
84 #define gliter(list, gl, next) \
85 /* Iterate through the G-lines in the list */ \
86 for ((gl) = (list); (gl); (gl) = (next)) \
87 /* Figure out the next pointer in list... */ \
88 if ((((next) = (gl)->gl_next) || 1) && \
89 /* Then see if it's expired */ \
90 (gl)->gl_lifetime <= TStime()) \
91 /* Record has expired, so free the G-line */ \
93 /* See if we need to expire the G-line */ \
94 else if ((((gl)->gl_expire > TStime()) || \
95 (((gl)->gl_flags &= ~GLINE_ACTIVE) && 0) || \
96 ((gl)->gl_state = GLOCAL_GLOBAL)) && 0) \
97 ; /* empty statement */ \
100 /** Find canonical user and host for a string.
101 * If \a userhost starts with '$', assign \a userhost to *user_p and NULL to *host_p.
102 * Otherwise, if \a userhost contains '@', assign the earlier part of it to *user_p and the rest to *host_p.
103 * Otherwise, assign \a def_user to *user_p and \a userhost to *host_p.
105 * @param[in] userhost Input string from user.
106 * @param[out] user_p Gets pointer to user (or channel/realname) part of hostmask.
107 * @param[out] host_p Gets point to host part of hostmask (may be assigned NULL).
108 * @param[in] def_user Default value for user part.
111 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
115 if (*userhost == '$') {
121 if (!(tmp = strchr(userhost, '@'))) {
131 /** Create a Gline structure.
132 * @param[in] user User part of mask.
133 * @param[in] host Host part of mask (NULL if not applicable).
134 * @param[in] reason Reason for G-line.
135 * @param[in] expire Expiration timestamp.
136 * @param[in] lastmod Last modification timestamp.
137 * @param[in] flags Bitwise combination of GLINE_* bits.
138 * @return Newly allocated G-line.
140 static struct Gline *
141 make_gline(char *user, char *host, char *reason, time_t expire, time_t lastmod,
142 time_t lifetime, unsigned int flags)
148 gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
151 DupString(gline->gl_reason, reason); /* initialize gline... */
152 gline->gl_expire = expire;
153 gline->gl_lifetime = lifetime;
154 gline->gl_lastmod = lastmod;
155 gline->gl_flags = flags & GLINE_MASK;
156 gline->gl_state = GLOCAL_GLOBAL; /* not locally modified */
158 if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
159 DupString(gline->gl_user, user); /* first, remember channel */
160 gline->gl_host = NULL;
162 gline->gl_next = BadChanGlineList; /* then link it into list */
163 gline->gl_prev_p = &BadChanGlineList;
164 if (BadChanGlineList)
165 BadChanGlineList->gl_prev_p = &gline->gl_next;
166 BadChanGlineList = gline;
168 DupString(gline->gl_user, user); /* remember them... */
170 DupString(gline->gl_host, host);
172 gline->gl_host = NULL;
174 if (*user != '$' && ipmask_parse(host, &gline->gl_addr, &gline->gl_bits))
175 gline->gl_flags |= GLINE_IPMASK;
177 gline->gl_next = GlobalGlineList; /* then link it into list */
178 gline->gl_prev_p = &GlobalGlineList;
180 GlobalGlineList->gl_prev_p = &gline->gl_next;
181 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 (feature_bool(FEAT_DISABLE_GLINES))
202 return 0; /* G-lines are disabled */
204 if (GlineIsBadChan(gline)) /* no action taken on badchan glines */
206 if (!GlineIsActive(gline)) /* no action taken on inactive glines */
209 for (fd = HighestFd; fd >= 0; --fd) {
213 if ((acptr = LocalClientArray[fd])) {
214 if (!cli_user(acptr))
217 if (GlineIsRealName(gline)) { /* Realname Gline */
218 Debug((DEBUG_DEBUG,"Realname Gline: %s %s",(cli_info(acptr)),
220 if (match(gline->gl_user+2, cli_info(acptr)) != 0)
222 Debug((DEBUG_DEBUG,"Matched!"));
223 } else { /* Host/IP gline */
224 if (cli_user(acptr)->username &&
225 match(gline->gl_user, (cli_user(acptr))->username) != 0)
228 if (GlineIsIpMask(gline)) {
229 if (!ipmask_check(&cli_ip(acptr), &gline->gl_addr, gline->gl_bits))
233 if (match(gline->gl_host, cli_sockhost(acptr)) != 0)
238 /* ok, here's one that got G-lined */
239 send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
242 /* let the ops know about it */
243 sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
244 get_client_name(acptr, SHOW_IP));
246 /* and get rid of him */
247 if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
249 retval = tval; /* retain killed status */
256 * Implements the mask checking applied to local G-lines.
257 * Basically, host masks must have a minimum of two non-wild domain
258 * fields, and IP masks must have a minimum of 16 bits. If the mask
259 * has even one wild-card, OVERRIDABLE is returned, assuming the other
260 * check doesn't fail.
261 * @param[in] mask G-line mask to check.
262 * @return One of CHECK_REJECTED, CHECK_OVERRIDABLE, or CHECK_APPROVED.
265 gline_checkmask(char *mask)
267 unsigned int flags = MASK_IP;
268 unsigned int dots = 0;
269 unsigned int ipmask = 0;
271 for (; *mask; mask++) { /* go through given mask */
272 if (*mask == '.') { /* it's a separator; advance positional wilds */
273 flags = (flags & ~MASK_WILD_MASK) | ((flags << 1) & MASK_WILD_MASK);
276 if ((flags & (MASK_IP | MASK_WILDS)) == MASK_IP)
277 ipmask += 8; /* It's an IP with no wilds, count bits */
278 } else if (*mask == '*' || *mask == '?')
279 flags |= MASK_WILD_0 | MASK_WILDS; /* found a wildcard */
280 else if (*mask == '/') { /* n.n.n.n/n notation; parse bit specifier */
282 ipmask = strtoul(mask, &mask, 10);
284 /* sanity-check to date */
285 if (*mask || (flags & (MASK_WILDS | MASK_IP)) != MASK_IP)
286 return CHECK_REJECTED;
289 return CHECK_REJECTED;
293 if (dots != 3 || ipmask > 32)
294 return CHECK_REJECTED;
299 flags |= MASK_HALT; /* Halt the ipmask calculation */
300 break; /* get out of the loop */
301 } else if (!IsIP6Char(*mask)) {
302 flags &= ~MASK_IP; /* not an IP anymore! */
307 /* Sanity-check quads */
308 if (dots > 3 || (!(flags & MASK_WILDS) && dots < 3)) {
313 /* update bit count if necessary */
314 if ((flags & (MASK_IP | MASK_WILDS | MASK_HALT)) == MASK_IP)
317 /* Check to see that it's not too wide of a mask */
318 if (flags & MASK_WILDS &&
319 ((!(flags & MASK_IP) && (dots < 2 || flags & MASK_WILD_MASK)) ||
320 (flags & MASK_IP && ipmask < 16)))
321 return CHECK_REJECTED; /* to wide, reject */
323 /* Ok, it's approved; require override if it has wildcards, though */
324 return flags & MASK_WILDS ? CHECK_OVERRIDABLE : CHECK_APPROVED;
327 /** Forward a G-line to other servers.
328 * @param[in] cptr Client that sent us the G-line.
329 * @param[in] sptr Client that originated the G-line.
330 * @param[in] gline G-line to forward.
334 gline_propagate(struct Client *cptr, struct Client *sptr, struct Gline *gline)
336 if (GlineIsLocal(gline))
339 assert(gline->gl_lastmod);
341 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
342 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
343 gline->gl_host ? "@" : "",
344 gline->gl_host ? gline->gl_host : "",
345 gline->gl_expire - TStime(), gline->gl_lastmod,
346 gline->gl_lifetime, gline->gl_reason);
351 /** Count number of users who match \a mask.
352 * @param[in] mask user\@host or user\@ip mask to check.
353 * @param[in] flags Bitmask possibly containing the value GLINE_LOCAL, to limit searches to this server.
354 * @return Count of matching users.
357 count_users(char *mask, int flags)
359 struct irc_in_addr ipmask;
360 struct Client *acptr;
363 char namebuf[USERLEN + HOSTLEN + 2];
364 char ipbuf[USERLEN + SOCKIPLEN + 2];
365 unsigned char ipmask_len;
367 ipmask_valid = ipmask_parse(mask, &ipmask, &ipmask_len);
368 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
371 if ((flags & GLINE_LOCAL) && !MyConnect(acptr))
374 ircd_snprintf(0, namebuf, sizeof(namebuf), "%s@%s",
375 cli_user(acptr)->username, cli_user(acptr)->realhost);
376 ircd_snprintf(0, ipbuf, sizeof(ipbuf), "%s@%s", cli_user(acptr)->username,
377 ircd_ntoa(&cli_ip(acptr)));
379 if (!match(mask, namebuf)
380 || !match(mask, ipbuf)
381 || (ipmask_valid && ipmask_check(&cli_ip(acptr), &ipmask, ipmask_len)))
388 /** Count number of users with a realname matching \a mask.
389 * @param[in] mask Wildcard mask to match against realnames.
390 * @return Count of matching users.
393 count_realnames(const char *mask)
395 struct Client *acptr;
401 matchcomp(cmask, &minlen, NULL, mask);
402 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
405 if (strlen(cli_info(acptr)) < minlen)
407 if (!matchexec(cli_info(acptr), cmask, minlen))
413 /** Create a new G-line and add it to global lists.
414 * \a userhost may be in one of four forms:
415 * \li A channel name, to add a BadChan.
416 * \li A string starting with $R and followed by a mask to match against their realname.
417 * \li A user\@IP mask (user\@ part optional) to create an IP-based ban.
418 * \li A user\@host mask (user\@ part optional) to create a hostname ban.
420 * @param[in] cptr Client that sent us the G-line.
421 * @param[in] sptr Client that originated the G-line.
422 * @param[in] userhost Text mask for the G-line.
423 * @param[in] reason Reason for G-line.
424 * @param[in] expire Expiration time of G-line.
425 * @param[in] lastmod Last modification time of G-line.
426 * @param[in] lifetime Lifetime of G-line.
427 * @param[in] flags Bitwise combination of GLINE_* flags.
428 * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
431 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
432 char *reason, time_t expire, time_t lastmod, time_t lifetime,
435 struct Gline *agline;
436 char uhmask[USERLEN + HOSTLEN + 2];
440 assert(0 != userhost);
442 assert(((flags & (GLINE_GLOBAL | GLINE_LOCAL)) == GLINE_GLOBAL) ||
443 ((flags & (GLINE_GLOBAL | GLINE_LOCAL)) == GLINE_LOCAL));
445 Debug((DEBUG_DEBUG, "gline_add(\"%s\", \"%s\", \"%s\", \"%s\", %Tu, %Tu "
446 "%Tu, 0x%04x)", cli_name(cptr), cli_name(sptr), userhost, reason,
447 expire, lastmod, lifetime, flags));
449 if (*userhost == '#' || *userhost == '&') {
450 if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
451 return send_reply(sptr, ERR_NOPRIVILEGES);
452 /* Allow maximum channel name length, plus margin for wildcards. */
453 if (strlen(userhost+1) >= CHANNELLEN + 6)
454 return send_reply(sptr, ERR_LONGMASK);
456 flags |= GLINE_BADCHAN;
459 } else if (*userhost == '$') {
460 switch (userhost[1]) {
462 /* Allow REALLEN for the real name, plus margin for wildcards. */
463 if (strlen(userhost+2) >= REALLEN + 6)
464 return send_reply(sptr, ERR_LONGMASK);
465 flags |= GLINE_REALNAME;
468 /* uh, what to do here? */
469 /* The answer, my dear Watson, is we throw a protocol_violation()
472 return protocol_violation(sptr,"%s has been smoking the sweet leaf and sent me a whacky gline",cli_name(sptr));
473 sendto_opmask_butone(NULL, SNO_GLINE, "%s has been smoking the sweet leaf and sent me a whacky gline", cli_name(sptr));
478 if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
479 tmp = count_realnames(userhost + 2);
480 if ((tmp >= feature_int(FEAT_GLINEMAXUSERCOUNT))
481 && !(flags & GLINE_OPERFORCE))
482 return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
485 canon_userhost(userhost, &user, &host, "*");
487 ircd_snprintf(0, uhmask, sizeof(uhmask), "%s@%s", user, host))
488 return send_reply(sptr, ERR_LONGMASK);
489 else if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
490 switch (gline_checkmask(host)) {
491 case CHECK_OVERRIDABLE: /* oper overrided restriction */
492 if (flags & GLINE_OPERFORCE)
496 return send_reply(sptr, ERR_MASKTOOWIDE, uhmask);
500 if ((tmp = count_users(uhmask, flags)) >=
501 feature_int(FEAT_GLINEMAXUSERCOUNT) && !(flags & GLINE_OPERFORCE))
502 return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
507 * You cannot set a negative (or zero) expire time, nor can you set an
508 * expiration time for greater than GLINE_MAX_EXPIRE.
510 if (!(flags & GLINE_FORCE) &&
511 (expire <= TStime() || expire > TStime() + GLINE_MAX_EXPIRE)) {
512 if (!IsServer(sptr) && MyConnect(sptr))
513 send_reply(sptr, ERR_BADEXPIRE, expire);
515 } else if (expire <= TStime()) {
516 /* This expired G-line was forced to be added, so mark it inactive. */
517 flags &= ~GLINE_ACTIVE;
520 if (!lifetime) /* no lifetime set, use expiration time */
523 /* lifetime is already an absolute timestamp */
526 sendto_opmask_butone(0, ircd_strncmp(reason, "AUTO", 4) ? SNO_GLINE :
527 SNO_AUTO, "%s adding %s%s %s for %s%s%s, expiring at "
529 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
531 cli_name((cli_user(sptr))->server),
532 (flags & GLINE_ACTIVE) ? "" : "deactivated ",
533 (flags & GLINE_LOCAL) ? "local" : "global",
534 (flags & GLINE_BADCHAN) ? "BADCHAN" : "GLINE", user,
535 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : "@",
536 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : host,
540 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
541 "%#C adding %s %s for %s%s%s, expiring at %Tu: %s", sptr,
542 flags & GLINE_LOCAL ? "local" : "global",
543 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", user,
544 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : "@",
545 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : host,
549 agline = make_gline(user, host, reason, expire, lastmod, lifetime, flags);
551 /* since we've disabled overlapped G-line checking, agline should
556 gline_propagate(cptr, sptr, agline);
558 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
561 /** Activate a currently inactive G-line.
562 * @param[in] cptr Peer that told us to activate the G-line.
563 * @param[in] sptr Client that originally thought it was a good idea.
564 * @param[in] gline G-line to activate.
565 * @param[in] lastmod New value for last modification timestamp.
566 * @param[in] flags 0 if the activation should be propagated, GLINE_LOCAL if not.
567 * @return Zero, unless \a sptr had a death wish (in which case CPTR_KILLED).
570 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
571 time_t lastmod, unsigned int flags)
573 unsigned int saveflags = 0;
577 saveflags = gline->gl_flags;
579 if (flags & GLINE_LOCAL)
580 gline->gl_flags &= ~GLINE_LDEACT;
582 gline->gl_flags |= GLINE_ACTIVE;
584 if (gline->gl_lastmod) {
585 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
588 gline->gl_lastmod = lastmod;
592 if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
593 return 0; /* was active to begin with */
595 /* Inform ops and log it */
596 sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
597 "expiring at %Tu: %s",
598 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
600 cli_name((cli_user(sptr))->server),
601 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
602 gline->gl_user, gline->gl_host ? "@" : "",
603 gline->gl_host ? gline->gl_host : "",
604 gline->gl_expire, gline->gl_reason);
606 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
607 "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
608 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
609 gline->gl_host ? "@" : "",
610 gline->gl_host ? gline->gl_host : "",
611 gline->gl_expire, gline->gl_reason);
613 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
614 gline_propagate(cptr, sptr, gline);
616 return do_gline(cptr, sptr, gline);
619 /** Deactivate a G-line.
620 * @param[in] cptr Peer that gave us the message.
621 * @param[in] sptr Client that initiated the deactivation.
622 * @param[in] gline G-line to deactivate.
623 * @param[in] lastmod New value for G-line last modification timestamp.
624 * @param[in] flags GLINE_LOCAL to only deactivate locally, 0 to propagate.
628 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
629 time_t lastmod, unsigned int flags)
631 unsigned int saveflags = 0;
636 saveflags = gline->gl_flags;
638 if (GlineIsLocal(gline))
639 msg = "removing local";
640 else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
641 msg = "removing global";
642 gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
644 msg = "deactivating global";
646 if (flags & GLINE_LOCAL)
647 gline->gl_flags |= GLINE_LDEACT;
649 gline->gl_flags &= ~GLINE_ACTIVE;
651 if (gline->gl_lastmod) {
652 if (gline->gl_lastmod >= lastmod)
655 gline->gl_lastmod = lastmod;
659 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
660 return 0; /* was inactive to begin with */
663 /* Inform ops and log it */
664 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
666 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
668 cli_name((cli_user(sptr))->server),
669 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
670 gline->gl_user, gline->gl_host ? "@" : "",
671 gline->gl_host ? gline->gl_host : "",
672 gline->gl_expire, gline->gl_reason);
674 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
675 "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
676 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
677 gline->gl_host ? "@" : "",
678 gline->gl_host ? gline->gl_host : "",
679 gline->gl_expire, gline->gl_reason);
681 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
682 gline_propagate(cptr, sptr, gline);
684 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
685 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
686 gline_free(gline); /* get rid of it */
691 /** Modify a global G-line.
692 * @param[in] cptr Client that sent us the G-line modification.
693 * @param[in] sptr Client that originated the G-line modification.
694 * @param[in] gline G-line being modified.
695 * @param[in] action Resultant status of the G-line.
696 * @param[in] reason Reason for G-line.
697 * @param[in] expire Expiration time of G-line.
698 * @param[in] lastmod Last modification time of G-line.
699 * @param[in] lifetime Lifetime of G-line.
700 * @param[in] flags Bitwise combination of GLINE_* flags.
701 * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
704 gline_modify(struct Client *cptr, struct Client *sptr, struct Gline *gline,
705 enum GlineAction action, char *reason, time_t expire,
706 time_t lastmod, time_t lifetime, unsigned int flags)
708 char buf[BUFSIZE], *op = "";
709 int pos = 0, non_auto = 0;
712 assert(!GlineIsLocal(gline));
714 Debug((DEBUG_DEBUG, "gline_modify(\"%s\", \"%s\", \"%s%s%s\", %s, \"%s\", "
715 "%Tu, %Tu, %Tu, 0x%04x)", cli_name(cptr), cli_name(sptr),
716 gline->gl_user, gline->gl_host ? "@" : "",
717 gline->gl_host ? gline->gl_host : "",
718 action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
719 (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
720 (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
721 (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
722 (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>")))),
723 reason, expire, lastmod, lifetime, flags));
725 /* First, let's check lastmod... */
726 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE) {
727 if (GlineLastMod(gline) > lastmod) { /* we have a more recent version */
728 if (IsBurstOrBurstAck(cptr))
729 return 0; /* middle of a burst, it'll resync on its own */
730 return gline_resend(cptr, gline); /* resync the server */
731 } else if (GlineLastMod(gline) == lastmod)
732 return 0; /* we have that version of the G-line... */
735 /* All right, we know that there's a change of some sort. What is it? */
736 /* first, check out the expiration time... */
737 if ((flags & GLINE_EXPIRE) && expire) {
738 if (!(flags & GLINE_FORCE) &&
739 (expire <= TStime() || expire > TStime() + GLINE_MAX_EXPIRE)) {
740 if (!IsServer(sptr) && MyConnect(sptr)) /* bad expiration time */
741 send_reply(sptr, ERR_BADEXPIRE, expire);
745 flags &= ~GLINE_EXPIRE;
747 /* Now check to see if there's any change... */
748 if ((flags & GLINE_EXPIRE) && expire == gline->gl_expire) {
749 flags &= ~GLINE_EXPIRE; /* no change to expiration time... */
753 /* Next, check out lifetime--this one's a bit trickier... */
754 if (!(flags & GLINE_LIFETIME) || !lifetime)
755 lifetime = gline->gl_lifetime; /* use G-line lifetime */
757 lifetime = IRCD_MAX(lifetime, expire); /* set lifetime to the max */
759 /* OK, let's see which is greater... */
760 if (lifetime > gline->gl_lifetime)
761 flags |= GLINE_LIFETIME; /* have to update lifetime */
763 flags &= ~GLINE_LIFETIME; /* no change to lifetime */
767 /* Finally, let's see if the reason needs to be updated */
768 if ((flags & GLINE_REASON) && reason &&
769 !ircd_strcmp(gline->gl_reason, reason))
770 flags &= ~GLINE_REASON; /* no changes to the reason */
772 /* OK, now let's take a look at the action... */
773 if ((action == GLINE_ACTIVATE && (gline->gl_flags & GLINE_ACTIVE)) ||
774 (action == GLINE_DEACTIVATE && !(gline->gl_flags & GLINE_ACTIVE)) ||
775 (action == GLINE_LOCAL_ACTIVATE &&
776 (gline->gl_state == GLOCAL_ACTIVATED)) ||
777 (action == GLINE_LOCAL_DEACTIVATE &&
778 (gline->gl_state == GLOCAL_DEACTIVATED)) ||
779 /* can't activate an expired G-line */
780 IRCD_MAX(gline->gl_expire, expire) <= TStime())
781 action = GLINE_MODIFY; /* no activity state modifications */
783 Debug((DEBUG_DEBUG, "About to perform changes; flags 0x%04x, action %s",
784 flags, action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
785 (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
786 (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
787 (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
788 (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>"))))));
790 /* If there are no changes to perform, do no changes */
791 if (!(flags & GLINE_UPDATE) && action == GLINE_MODIFY)
794 /* Now we know what needs to be changed, so let's process the changes... */
796 /* Start by updating lastmod, if indicated... */
797 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
798 gline->gl_lastmod = lastmod;
800 /* Then move on to activity status changes... */
802 case GLINE_ACTIVATE: /* Globally activating G-line */
803 gline->gl_flags |= GLINE_ACTIVE; /* make it active... */
804 gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
805 pos += ircd_snprintf(0, buf, sizeof(buf), " globally activating G-line");
806 op = "+"; /* operation for G-line propagation */
809 case GLINE_DEACTIVATE: /* Globally deactivating G-line */
810 gline->gl_flags &= ~GLINE_ACTIVE; /* make it inactive... */
811 gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
812 pos += ircd_snprintf(0, buf, sizeof(buf), " globally deactivating G-line");
813 op = "-"; /* operation for G-line propagation */
816 case GLINE_LOCAL_ACTIVATE: /* Locally activating G-line */
817 gline->gl_state = GLOCAL_ACTIVATED; /* make it locally active */
818 pos += ircd_snprintf(0, buf, sizeof(buf), " locally activating G-line");
821 case GLINE_LOCAL_DEACTIVATE: /* Locally deactivating G-line */
822 gline->gl_state = GLOCAL_DEACTIVATED; /* make it locally inactive */
823 pos += ircd_snprintf(0, buf, sizeof(buf), " locally deactivating G-line");
826 case GLINE_MODIFY: /* no change to activity status */
830 /* Handle expiration changes... */
831 if (flags & GLINE_EXPIRE) {
832 gline->gl_expire = expire; /* save new expiration time */
834 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
835 "%s%s changing expiration time to %Tu",
837 pos && !(flags & (GLINE_LIFETIME | GLINE_REASON)) ?
838 " and" : "", expire);
841 /* Next, handle lifetime changes... */
842 if (flags & GLINE_LIFETIME) {
843 gline->gl_lifetime = lifetime; /* save new lifetime */
845 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
846 "%s%s extending record lifetime to %Tu",
847 pos ? ";" : "", pos && !(flags & GLINE_REASON) ?
848 " and" : "", lifetime);
851 /* Now, handle reason changes... */
852 if (flags & GLINE_REASON) {
853 non_auto = non_auto || ircd_strncmp(gline->gl_reason, "AUTO", 4);
854 MyFree(gline->gl_reason); /* release old reason */
855 DupString(gline->gl_reason, reason); /* store new reason */
857 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
858 "%s%s changing reason to \"%s\"",
859 pos ? ";" : "", pos ? " and" : "", reason);
862 /* All right, inform ops... */
863 non_auto = non_auto || ircd_strncmp(gline->gl_reason, "AUTO", 4);
864 sendto_opmask_butone(0, non_auto ? SNO_GLINE : SNO_AUTO,
865 "%s modifying global %s for %s%s%s:%s",
866 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
867 cli_name(sptr) : cli_name((cli_user(sptr))->server),
868 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
869 gline->gl_user, gline->gl_host ? "@" : "",
870 gline->gl_host ? gline->gl_host : "", buf);
872 /* and log the change */
873 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
874 "%#C modifying global %s for %s%s%s:%s", sptr,
875 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
876 gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "",
879 /* We'll be simple for this release, but we can update this to change
880 * the propagation syntax on future updates
882 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
883 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
884 "* %s%s%s%s%s %Tu %Tu %Tu :%s",
885 flags & GLINE_OPERFORCE ? "!" : "", op,
886 gline->gl_user, gline->gl_host ? "@" : "",
887 gline->gl_host ? gline->gl_host : "",
888 gline->gl_expire - TStime(), gline->gl_lastmod,
889 gline->gl_lifetime, gline->gl_reason);
891 /* OK, let's do the G-line... */
892 return do_gline(cptr, sptr, gline);
895 /** Destroy a local G-line.
896 * @param[in] cptr Peer that gave us the message.
897 * @param[in] sptr Client that initiated the destruction.
898 * @param[in] gline G-line to destroy.
902 gline_destroy(struct Client *cptr, struct Client *sptr, struct Gline *gline)
905 assert(GlineIsLocal(gline));
907 /* Inform ops and log it */
908 sendto_opmask_butone(0, SNO_GLINE, "%s removing local %s for %s%s%s",
909 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
910 cli_name(sptr) : cli_name((cli_user(sptr))->server),
911 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
912 gline->gl_user, gline->gl_host ? "@" : "",
913 gline->gl_host ? gline->gl_host : "");
914 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
915 "%#C removing local %s for %s%s%s", sptr,
916 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
917 gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "");
919 gline_free(gline); /* get rid of the G-line */
921 return 0; /* convenience return */
924 /** Find a G-line for a particular mask, guided by certain flags.
925 * Certain bits in \a flags are interpreted specially:
927 * <dt>GLINE_ANY</dt><dd>Search both BadChans and user G-lines.</dd>
928 * <dt>GLINE_BADCHAN</dt><dd>Search BadChans.</dd>
929 * <dt>GLINE_GLOBAL</dt><dd>Only match global G-lines.</dd>
930 * <dt>GLINE_LOCAL</dt><dd>Only match local G-lines.</dd>
931 * <dt>GLINE_LASTMOD</dt><dd>Only match G-lines with a last modification time.</dd>
932 * <dt>GLINE_EXACT</dt><dd>Require an exact match of G-line mask.</dd>
933 * <dt>anything else</dt><dd>Search user G-lines.</dd>
935 * @param[in] userhost Mask to search for.
936 * @param[in] flags Bitwise combination of GLINE_* flags.
937 * @return First matching G-line, or NULL if none are found.
940 gline_find(char *userhost, unsigned int flags)
942 struct Gline *gline = 0;
943 struct Gline *sgline;
944 char *user, *host, *t_uh;
946 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
947 gliter(BadChanGlineList, gline, sgline) {
948 if ((flags & (GlineIsLocal(gline) ? GLINE_GLOBAL : GLINE_LOCAL)) ||
949 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
951 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
952 match(gline->gl_user, userhost)) == 0)
957 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
958 *userhost == '#' || *userhost == '&')
961 DupString(t_uh, userhost);
962 canon_userhost(t_uh, &user, &host, "*");
964 gliter(GlobalGlineList, gline, sgline) {
965 if ((flags & (GlineIsLocal(gline) ? GLINE_GLOBAL : GLINE_LOCAL)) ||
966 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
968 else if (flags & GLINE_EXACT) {
969 if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
970 || (!gline->gl_host && !host)) &&
971 (ircd_strcmp(gline->gl_user, user) == 0))
974 if (((gline->gl_host && host && match(gline->gl_host, host) == 0)
975 || (!gline->gl_host && !host)) &&
976 (match(gline->gl_user, user) == 0))
986 /** Find a matching G-line for a user.
987 * @param[in] cptr Client to compare against.
988 * @param[in] flags Bitwise combination of GLINE_GLOBAL and/or
989 * GLINE_LASTMOD to limit matches.
990 * @return Matching G-line, or NULL if none are found.
993 gline_lookup(struct Client *cptr, unsigned int flags)
996 struct Gline *sgline;
998 gliter(GlobalGlineList, gline, sgline) {
999 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
1000 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
1003 if (GlineIsRealName(gline)) {
1004 Debug((DEBUG_DEBUG,"realname gline: '%s' '%s'",gline->gl_user,cli_info(cptr)));
1005 if (match(gline->gl_user+2, cli_info(cptr)) != 0)
1009 if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
1012 if (GlineIsIpMask(gline)) {
1013 if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
1017 if (match(gline->gl_host, (cli_user(cptr))->realhost) != 0)
1021 if (GlineIsActive(gline))
1030 /** Delink and free a G-line.
1031 * @param[in] gline G-line to free.
1034 gline_free(struct Gline *gline)
1038 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
1040 gline->gl_next->gl_prev_p = gline->gl_prev_p;
1042 MyFree(gline->gl_user); /* free up the memory */
1044 MyFree(gline->gl_host);
1045 MyFree(gline->gl_reason);
1049 /** Burst all known global G-lines to another server.
1050 * @param[in] cptr Destination of burst.
1053 gline_burst(struct Client *cptr)
1055 struct Gline *gline;
1056 struct Gline *sgline;
1058 gliter(GlobalGlineList, gline, sgline) {
1059 if (!GlineIsLocal(gline) && gline->gl_lastmod)
1060 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1061 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1062 gline->gl_host ? "@" : "",
1063 gline->gl_host ? gline->gl_host : "",
1064 gline->gl_expire - TStime(), gline->gl_lastmod,
1065 gline->gl_lifetime, gline->gl_reason);
1068 gliter(BadChanGlineList, gline, sgline) {
1069 if (!GlineIsLocal(gline) && gline->gl_lastmod)
1070 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu %Tu :%s",
1071 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1072 gline->gl_expire - TStime(), gline->gl_lastmod,
1073 gline->gl_lifetime, gline->gl_reason);
1077 /** Send a G-line to another server.
1078 * @param[in] cptr Who to inform of the G-line.
1079 * @param[in] gline G-line to send.
1083 gline_resend(struct Client *cptr, struct Gline *gline)
1085 if (GlineIsLocal(gline) || !gline->gl_lastmod)
1088 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1089 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1090 gline->gl_host ? "@" : "",
1091 gline->gl_host ? gline->gl_host : "",
1092 gline->gl_expire - TStime(), gline->gl_lastmod,
1093 gline->gl_lifetime, gline->gl_reason);
1098 /** Display one or all G-lines to a user.
1099 * If \a userhost is not NULL, only send the first matching G-line.
1100 * Otherwise send the whole list.
1101 * @param[in] sptr User asking for G-line list.
1102 * @param[in] userhost G-line mask to search for (or NULL).
1106 gline_list(struct Client *sptr, char *userhost)
1108 struct Gline *gline;
1109 struct Gline *sgline;
1112 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
1113 return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
1115 /* send gline information along */
1116 send_reply(sptr, RPL_GLIST, gline->gl_user,
1117 gline->gl_host ? "@" : "",
1118 gline->gl_host ? gline->gl_host : "",
1119 gline->gl_expire, gline->gl_lastmod,
1121 GlineIsLocal(gline) ? cli_name(&me) : "*",
1122 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1123 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1124 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1126 gliter(GlobalGlineList, gline, sgline) {
1127 send_reply(sptr, RPL_GLIST, gline->gl_user,
1128 gline->gl_host ? "@" : "",
1129 gline->gl_host ? gline->gl_host : "",
1130 gline->gl_expire, gline->gl_lastmod,
1132 GlineIsLocal(gline) ? cli_name(&me) : "*",
1133 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1134 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1135 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1138 gliter(BadChanGlineList, gline, sgline) {
1139 send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
1140 gline->gl_expire, gline->gl_lastmod,
1142 GlineIsLocal(gline) ? cli_name(&me) : "*",
1143 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1144 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1145 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1149 /* end of gline information */
1150 return send_reply(sptr, RPL_ENDOFGLIST);
1153 /** Statistics callback to list G-lines.
1154 * @param[in] sptr Client requesting statistics.
1155 * @param[in] sd Stats descriptor for request (ignored).
1156 * @param[in] param Extra parameter from user (ignored).
1159 gline_stats(struct Client *sptr, const struct StatDesc *sd,
1162 struct Gline *gline;
1163 struct Gline *sgline;
1165 gliter(GlobalGlineList, gline, sgline) {
1166 send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user,
1167 gline->gl_host ? "@" : "",
1168 gline->gl_host ? gline->gl_host : "",
1169 gline->gl_expire, gline->gl_lastmod,
1171 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1172 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1173 GlineIsRemActive(gline) ? '+' : '-',
1178 /** Calculate memory used by G-lines.
1179 * @param[out] gl_size Number of bytes used by G-lines.
1180 * @return Number of G-lines in use.
1183 gline_memory_count(size_t *gl_size)
1185 struct Gline *gline;
1186 unsigned int gl = 0;
1188 for (gline = GlobalGlineList; gline; gline = gline->gl_next) {
1190 *gl_size += sizeof(struct Gline);
1191 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1192 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1193 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;
1196 for (gline = BadChanGlineList; gline; gline = gline->gl_next) {
1198 *gl_size += sizeof(struct Gline);
1199 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1200 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1201 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;