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 <= TStime()) \
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 > TStime()) || \
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 - TStime(), 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 <= TStime() || expire > TStime() + GLINE_MAX_EXPIRE)) {
503 if (!IsServer(sptr) && MyConnect(sptr))
504 send_reply(sptr, ERR_BADEXPIRE, expire);
506 } else if (expire <= TStime()) {
507 /* This expired G-line was forced to be added, so mark it inactive. */
508 flags &= ~GLINE_ACTIVE;
511 if (!lifetime) /* no lifetime set, use expiration time */
514 /* lifetime is already an absolute timestamp */
517 sendto_opmask_butone(0, ircd_strncmp(reason, "AUTO", 4) ? SNO_GLINE :
518 SNO_AUTO, "%s adding %s%s %s for %s%s%s, expiring at "
520 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
522 cli_name((cli_user(sptr))->server),
523 (flags & GLINE_ACTIVE) ? "" : "deactivated ",
524 (flags & GLINE_LOCAL) ? "local" : "global",
525 (flags & GLINE_BADCHAN) ? "BADCHAN" : "GLINE", user,
526 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : "@",
527 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : host,
531 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
532 "%#C adding %s %s for %s%s%s, expiring at %Tu: %s", sptr,
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 agline = make_gline(user, host, reason, expire, lastmod, lifetime, flags);
542 /* since we've disabled overlapped G-line checking, agline should
547 gline_propagate(cptr, sptr, agline);
549 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
552 /** Activate a currently inactive G-line.
553 * @param[in] cptr Peer that told us to activate the G-line.
554 * @param[in] sptr Client that originally thought it was a good idea.
555 * @param[in] gline G-line to activate.
556 * @param[in] lastmod New value for last modification timestamp.
557 * @param[in] flags 0 if the activation should be propagated, GLINE_LOCAL if not.
558 * @return Zero, unless \a sptr had a death wish (in which case CPTR_KILLED).
561 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
562 time_t lastmod, unsigned int flags)
564 unsigned int saveflags = 0;
568 saveflags = gline->gl_flags;
570 if (flags & GLINE_LOCAL)
571 gline->gl_flags &= ~GLINE_LDEACT;
573 gline->gl_flags |= GLINE_ACTIVE;
575 if (gline->gl_lastmod) {
576 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
579 gline->gl_lastmod = lastmod;
583 if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
584 return 0; /* was active to begin with */
586 /* Inform ops and log it */
587 sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
588 "expiring at %Tu: %s",
589 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
591 cli_name((cli_user(sptr))->server),
592 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
593 gline->gl_user, gline->gl_host ? "@" : "",
594 gline->gl_host ? gline->gl_host : "",
595 gline->gl_expire, gline->gl_reason);
597 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
598 "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
599 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
600 gline->gl_host ? "@" : "",
601 gline->gl_host ? gline->gl_host : "",
602 gline->gl_expire, gline->gl_reason);
604 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
605 gline_propagate(cptr, sptr, gline);
607 return do_gline(cptr, sptr, gline);
610 /** Deactivate a G-line.
611 * @param[in] cptr Peer that gave us the message.
612 * @param[in] sptr Client that initiated the deactivation.
613 * @param[in] gline G-line to deactivate.
614 * @param[in] lastmod New value for G-line last modification timestamp.
615 * @param[in] flags GLINE_LOCAL to only deactivate locally, 0 to propagate.
619 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
620 time_t lastmod, unsigned int flags)
622 unsigned int saveflags = 0;
627 saveflags = gline->gl_flags;
629 if (GlineIsLocal(gline))
630 msg = "removing local";
631 else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
632 msg = "removing global";
633 gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
635 msg = "deactivating global";
637 if (flags & GLINE_LOCAL)
638 gline->gl_flags |= GLINE_LDEACT;
640 gline->gl_flags &= ~GLINE_ACTIVE;
642 if (gline->gl_lastmod) {
643 if (gline->gl_lastmod >= lastmod)
646 gline->gl_lastmod = lastmod;
650 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
651 return 0; /* was inactive to begin with */
654 /* Inform ops and log it */
655 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
657 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
659 cli_name((cli_user(sptr))->server),
660 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
661 gline->gl_user, gline->gl_host ? "@" : "",
662 gline->gl_host ? gline->gl_host : "",
663 gline->gl_expire, gline->gl_reason);
665 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
666 "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
667 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
668 gline->gl_host ? "@" : "",
669 gline->gl_host ? gline->gl_host : "",
670 gline->gl_expire, gline->gl_reason);
672 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
673 gline_propagate(cptr, sptr, gline);
675 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
676 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
677 gline_free(gline); /* get rid of it */
682 /** Modify a global G-line.
683 * @param[in] cptr Client that sent us the G-line modification.
684 * @param[in] sptr Client that originated the G-line modification.
685 * @param[in] gline G-line being modified.
686 * @param[in] action Resultant status of the G-line.
687 * @param[in] reason Reason for G-line.
688 * @param[in] expire Expiration time of G-line.
689 * @param[in] lastmod Last modification time of G-line.
690 * @param[in] lifetime Lifetime of G-line.
691 * @param[in] flags Bitwise combination of GLINE_* flags.
692 * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
695 gline_modify(struct Client *cptr, struct Client *sptr, struct Gline *gline,
696 enum GlineAction action, char *reason, time_t expire,
697 time_t lastmod, time_t lifetime, unsigned int flags)
699 char buf[BUFSIZE], *op = "";
703 assert(!GlineIsLocal(gline));
705 Debug((DEBUG_DEBUG, "gline_modify(\"%s\", \"%s\", \"%s%s%s\", %s, \"%s\", "
706 "%Tu, %Tu, %Tu, 0x%04x)", cli_name(cptr), cli_name(sptr),
707 gline->gl_user, gline->gl_host ? "@" : "",
708 gline->gl_host ? gline->gl_host : "",
709 action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
710 (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
711 (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
712 (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
713 (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>")))),
714 reason, expire, lastmod, lifetime, flags));
716 /* First, let's check lastmod... */
717 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE) {
718 if (GlineLastMod(gline) > lastmod) { /* we have a more recent version */
719 if (IsBurstOrBurstAck(cptr))
720 return 0; /* middle of a burst, it'll resync on its own */
721 return gline_resend(cptr, gline); /* resync the server */
722 } else if (GlineLastMod(gline) == lastmod)
723 return 0; /* we have that version of the G-line... */
726 /* All right, we know that there's a change of some sort. What is it? */
727 /* first, check out the expiration time... */
728 if ((flags & GLINE_EXPIRE) && expire) {
729 if (!(flags & GLINE_FORCE) &&
730 (expire <= TStime() || expire > TStime() + GLINE_MAX_EXPIRE)) {
731 if (!IsServer(sptr) && MyConnect(sptr)) /* bad expiration time */
732 send_reply(sptr, ERR_BADEXPIRE, expire);
736 flags &= ~GLINE_EXPIRE;
738 /* Now check to see if there's any change... */
739 if ((flags & GLINE_EXPIRE) && expire == gline->gl_expire) {
740 flags &= ~GLINE_EXPIRE; /* no change to expiration time... */
744 /* Next, check out lifetime--this one's a bit trickier... */
745 if (!(flags & GLINE_LIFETIME) || !lifetime)
746 lifetime = gline->gl_lifetime; /* use G-line lifetime */
748 lifetime = IRCD_MAX(lifetime, expire); /* set lifetime to the max */
750 /* OK, let's see which is greater... */
751 if (lifetime > gline->gl_lifetime)
752 flags |= GLINE_LIFETIME; /* have to update lifetime */
754 flags &= ~GLINE_LIFETIME; /* no change to lifetime */
758 /* Finally, let's see if the reason needs to be updated */
759 if ((flags & GLINE_REASON) && reason &&
760 !ircd_strcmp(gline->gl_reason, reason))
761 flags &= ~GLINE_REASON; /* no changes to the reason */
763 /* OK, now let's take a look at the action... */
764 if ((action == GLINE_ACTIVATE && (gline->gl_flags & GLINE_ACTIVE)) ||
765 (action == GLINE_DEACTIVATE && !(gline->gl_flags & GLINE_ACTIVE)) ||
766 (action == GLINE_LOCAL_ACTIVATE &&
767 (gline->gl_state == GLOCAL_ACTIVATED)) ||
768 (action == GLINE_LOCAL_DEACTIVATE &&
769 (gline->gl_state == GLOCAL_DEACTIVATED)) ||
770 /* can't activate an expired G-line */
771 IRCD_MAX(gline->gl_expire, expire) <= TStime())
772 action = GLINE_MODIFY; /* no activity state modifications */
774 Debug((DEBUG_DEBUG, "About to perform changes; flags 0x%04x, action %s",
775 flags, action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
776 (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
777 (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
778 (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
779 (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>"))))));
781 /* If there are no changes to perform, do no changes */
782 if (!(flags & GLINE_UPDATE) && action == GLINE_MODIFY)
785 /* Now we know what needs to be changed, so let's process the changes... */
787 /* Start by updating lastmod, if indicated... */
788 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
789 gline->gl_lastmod = lastmod;
791 /* Then move on to activity status changes... */
793 case GLINE_ACTIVATE: /* Globally activating G-line */
794 gline->gl_flags |= GLINE_ACTIVE; /* make it active... */
795 gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
796 pos += ircd_snprintf(0, buf, sizeof(buf), " globally activating G-line");
797 op = "+"; /* operation for G-line propagation */
800 case GLINE_DEACTIVATE: /* Globally deactivating G-line */
801 gline->gl_flags &= ~GLINE_ACTIVE; /* make it inactive... */
802 gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
803 pos += ircd_snprintf(0, buf, sizeof(buf), " globally deactivating G-line");
804 op = "-"; /* operation for G-line propagation */
807 case GLINE_LOCAL_ACTIVATE: /* Locally activating G-line */
808 gline->gl_state = GLOCAL_ACTIVATED; /* make it locally active */
809 pos += ircd_snprintf(0, buf, sizeof(buf), " locally activating G-line");
812 case GLINE_LOCAL_DEACTIVATE: /* Locally deactivating G-line */
813 gline->gl_state = GLOCAL_DEACTIVATED; /* make it locally inactive */
814 pos += ircd_snprintf(0, buf, sizeof(buf), " locally deactivating G-line");
817 case GLINE_MODIFY: /* no change to activity status */
821 /* Handle expiration changes... */
822 if (flags & GLINE_EXPIRE) {
823 gline->gl_expire = expire; /* save new expiration time */
825 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
826 "%s%s changing expiration time to %Tu",
828 pos && !(flags & (GLINE_LIFETIME | GLINE_REASON)) ?
829 " and" : "", expire);
832 /* Next, handle lifetime changes... */
833 if (flags & GLINE_LIFETIME) {
834 gline->gl_lifetime = lifetime; /* save new lifetime */
836 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
837 "%s%s extending record lifetime to %Tu",
838 pos ? ";" : "", pos && !(flags & GLINE_REASON) ?
839 " and" : "", lifetime);
842 /* Now, handle reason changes... */
843 if (flags & GLINE_REASON) {
844 MyFree(gline->gl_reason); /* release old reason */
845 DupString(gline->gl_reason, reason); /* store new reason */
847 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
848 "%s%s changing reason to \"%s\"",
849 pos ? ";" : "", pos ? " and" : "", reason);
852 /* All right, inform ops... */
853 sendto_opmask_butone(0, SNO_GLINE, "%s modifying global %s for %s%s%s:%s",
854 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
855 cli_name(sptr) : cli_name((cli_user(sptr))->server),
856 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
857 gline->gl_user, gline->gl_host ? "@" : "",
858 gline->gl_host ? gline->gl_host : "", buf);
860 /* and log the change */
861 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
862 "%#C modifying global %s for %s%s%s:%s", sptr,
863 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
864 gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "",
867 /* We'll be simple for this release, but we can update this to change
868 * the propagation syntax on future updates
870 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
871 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
872 "* %s%s%s%s%s %Tu %Tu %Tu :%s",
873 flags & GLINE_OPERFORCE ? "!" : "", op,
874 gline->gl_user, gline->gl_host ? "@" : "",
875 gline->gl_host ? gline->gl_host : "",
876 gline->gl_expire - TStime(), gline->gl_lastmod,
877 gline->gl_lifetime, gline->gl_reason);
879 /* OK, let's do the G-line... */
880 return do_gline(cptr, sptr, gline);
883 /** Destroy a local G-line.
884 * @param[in] cptr Peer that gave us the message.
885 * @param[in] sptr Client that initiated the destruction.
886 * @param[in] gline G-line to destroy.
890 gline_destroy(struct Client *cptr, struct Client *sptr, struct Gline *gline)
893 assert(GlineIsLocal(gline));
895 /* Inform ops and log it */
896 sendto_opmask_butone(0, SNO_GLINE, "%s removing local %s for %s%s%s",
897 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
898 cli_name(sptr) : cli_name((cli_user(sptr))->server),
899 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
900 gline->gl_user, gline->gl_host ? "@" : "",
901 gline->gl_host ? gline->gl_host : "");
902 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
903 "%#C removing local %s for %s%s%s", sptr,
904 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
905 gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "");
907 gline_free(gline); /* get rid of the G-line */
909 return 0; /* convenience return */
912 /** Find a G-line for a particular mask, guided by certain flags.
913 * Certain bits in \a flags are interpreted specially:
915 * <dt>GLINE_ANY</dt><dd>Search both BadChans and user G-lines.</dd>
916 * <dt>GLINE_BADCHAN</dt><dd>Search BadChans.</dd>
917 * <dt>GLINE_GLOBAL</dt><dd>Only match global G-lines.</dd>
918 * <dt>GLINE_LOCAL</dt><dd>Only match local G-lines.</dd>
919 * <dt>GLINE_LASTMOD</dt><dd>Only match G-lines with a last modification time.</dd>
920 * <dt>GLINE_EXACT</dt><dd>Require an exact match of G-line mask.</dd>
921 * <dt>anything else</dt><dd>Search user G-lines.</dd>
923 * @param[in] userhost Mask to search for.
924 * @param[in] flags Bitwise combination of GLINE_* flags.
925 * @return First matching G-line, or NULL if none are found.
928 gline_find(char *userhost, unsigned int flags)
930 struct Gline *gline = 0;
931 struct Gline *sgline;
932 char *user, *host, *t_uh;
934 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
935 gliter(BadChanGlineList, gline, sgline) {
936 if ((flags & (GlineIsLocal(gline) ? GLINE_GLOBAL : GLINE_LOCAL)) ||
937 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
939 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
940 match(gline->gl_user, userhost)) == 0)
945 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
946 *userhost == '#' || *userhost == '&')
949 DupString(t_uh, userhost);
950 canon_userhost(t_uh, &user, &host, "*");
952 gliter(GlobalGlineList, gline, sgline) {
953 if ((flags & (GlineIsLocal(gline) ? GLINE_GLOBAL : GLINE_LOCAL)) ||
954 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
956 else if (flags & GLINE_EXACT) {
957 if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
958 || (!gline->gl_host && !host)) &&
959 (ircd_strcmp(gline->gl_user, user) == 0))
962 if (((gline->gl_host && host && match(gline->gl_host, host) == 0)
963 || (!gline->gl_host && !host)) &&
964 (match(gline->gl_user, user) == 0))
974 /** Find a matching G-line for a user.
975 * @param[in] cptr Client to compare against.
976 * @param[in] flags Bitwise combination of GLINE_GLOBAL and/or
977 * GLINE_LASTMOD to limit matches.
978 * @return Matching G-line, or NULL if none are found.
981 gline_lookup(struct Client *cptr, unsigned int flags)
984 struct Gline *sgline;
986 gliter(GlobalGlineList, gline, sgline) {
987 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
988 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
991 if (GlineIsRealName(gline)) {
992 Debug((DEBUG_DEBUG,"realname gline: '%s' '%s'",gline->gl_user,cli_info(cptr)));
993 if (match(gline->gl_user+2, cli_info(cptr)) != 0)
997 if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
1000 if (GlineIsIpMask(gline)) {
1001 if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
1005 if (match(gline->gl_host, (cli_user(cptr))->realhost) != 0)
1009 if (GlineIsActive(gline))
1018 /** Delink and free a G-line.
1019 * @param[in] gline G-line to free.
1022 gline_free(struct Gline *gline)
1026 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
1028 gline->gl_next->gl_prev_p = gline->gl_prev_p;
1030 MyFree(gline->gl_user); /* free up the memory */
1032 MyFree(gline->gl_host);
1033 MyFree(gline->gl_reason);
1037 /** Burst all known global G-lines to another server.
1038 * @param[in] cptr Destination of burst.
1041 gline_burst(struct Client *cptr)
1043 struct Gline *gline;
1044 struct Gline *sgline;
1046 gliter(GlobalGlineList, gline, sgline) {
1047 if (!GlineIsLocal(gline) && gline->gl_lastmod)
1048 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1049 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1050 gline->gl_host ? "@" : "",
1051 gline->gl_host ? gline->gl_host : "",
1052 gline->gl_expire - TStime(), gline->gl_lastmod,
1053 gline->gl_lifetime, gline->gl_reason);
1056 gliter(BadChanGlineList, gline, sgline) {
1057 if (!GlineIsLocal(gline) && gline->gl_lastmod)
1058 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu %Tu :%s",
1059 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1060 gline->gl_expire - TStime(), gline->gl_lastmod,
1061 gline->gl_lifetime, gline->gl_reason);
1065 /** Send a G-line to another server.
1066 * @param[in] cptr Who to inform of the G-line.
1067 * @param[in] gline G-line to send.
1071 gline_resend(struct Client *cptr, struct Gline *gline)
1073 if (GlineIsLocal(gline) || !gline->gl_lastmod)
1076 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1077 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1078 gline->gl_host ? "@" : "",
1079 gline->gl_host ? gline->gl_host : "",
1080 gline->gl_expire - TStime(), gline->gl_lastmod,
1081 gline->gl_lifetime, gline->gl_reason);
1086 /** Display one or all G-lines to a user.
1087 * If \a userhost is not NULL, only send the first matching G-line.
1088 * Otherwise send the whole list.
1089 * @param[in] sptr User asking for G-line list.
1090 * @param[in] userhost G-line mask to search for (or NULL).
1094 gline_list(struct Client *sptr, char *userhost)
1096 struct Gline *gline;
1097 struct Gline *sgline;
1100 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
1101 return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
1103 /* send gline information along */
1104 send_reply(sptr, RPL_GLIST, gline->gl_user,
1105 gline->gl_host ? "@" : "",
1106 gline->gl_host ? gline->gl_host : "",
1107 gline->gl_expire, gline->gl_lastmod,
1109 GlineIsLocal(gline) ? cli_name(&me) : "*",
1110 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1111 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1112 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1114 gliter(GlobalGlineList, gline, sgline) {
1115 send_reply(sptr, RPL_GLIST, gline->gl_user,
1116 gline->gl_host ? "@" : "",
1117 gline->gl_host ? gline->gl_host : "",
1118 gline->gl_expire, gline->gl_lastmod,
1120 GlineIsLocal(gline) ? cli_name(&me) : "*",
1121 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1122 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1123 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1126 gliter(BadChanGlineList, gline, sgline) {
1127 send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
1128 gline->gl_expire, gline->gl_lastmod,
1130 GlineIsLocal(gline) ? cli_name(&me) : "*",
1131 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1132 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1133 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1137 /* end of gline information */
1138 return send_reply(sptr, RPL_ENDOFGLIST);
1141 /** Statistics callback to list G-lines.
1142 * @param[in] sptr Client requesting statistics.
1143 * @param[in] sd Stats descriptor for request (ignored).
1144 * @param[in] param Extra parameter from user (ignored).
1147 gline_stats(struct Client *sptr, const struct StatDesc *sd,
1150 struct Gline *gline;
1151 struct Gline *sgline;
1153 gliter(GlobalGlineList, gline, sgline) {
1154 send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user,
1155 gline->gl_host ? "@" : "",
1156 gline->gl_host ? gline->gl_host : "",
1157 gline->gl_expire, gline->gl_lastmod,
1159 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1160 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1161 GlineIsRemActive(gline) ? '+' : '-',
1166 /** Calculate memory used by G-lines.
1167 * @param[out] gl_size Number of bytes used by G-lines.
1168 * @return Number of G-lines in use.
1171 gline_memory_count(size_t *gl_size)
1173 struct Gline *gline;
1174 unsigned int gl = 0;
1176 for (gline = GlobalGlineList; gline; gline = gline->gl_next) {
1178 *gl_size += sizeof(struct Gline);
1179 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1180 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1181 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;
1184 for (gline = BadChanGlineList; gline; gline = gline->gl_next) {
1186 *gl_size += sizeof(struct Gline);
1187 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1188 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1189 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;