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 %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_ACTIVE) ? "" : "deactivated ",
521 (flags & GLINE_LOCAL) ? "local" : "global",
522 (flags & GLINE_BADCHAN) ? "BADCHAN" : "GLINE", user,
523 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : "@",
524 (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : host,
525 expire + TSoffset, reason);
528 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
529 "%#C adding %s %s for %s%s%s, expiring at %Tu: %s", sptr,
530 flags & GLINE_LOCAL ? "local" : "global",
531 flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", user,
532 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : "@",
533 flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : host,
534 expire + TSoffset, reason);
537 agline = make_gline(user, host, reason, expire, lastmod, lifetime, flags);
539 /* since we've disabled overlapped G-line checking, agline should
544 gline_propagate(cptr, sptr, agline);
546 return do_gline(cptr, sptr, agline); /* knock off users if necessary */
549 /** Activate a currently inactive G-line.
550 * @param[in] cptr Peer that told us to activate the G-line.
551 * @param[in] sptr Client that originally thought it was a good idea.
552 * @param[in] gline G-line to activate.
553 * @param[in] lastmod New value for last modification timestamp.
554 * @param[in] flags 0 if the activation should be propagated, GLINE_LOCAL if not.
555 * @return Zero, unless \a sptr had a death wish (in which case CPTR_KILLED).
558 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
559 time_t lastmod, unsigned int flags)
561 unsigned int saveflags = 0;
565 saveflags = gline->gl_flags;
567 if (flags & GLINE_LOCAL)
568 gline->gl_flags &= ~GLINE_LDEACT;
570 gline->gl_flags |= GLINE_ACTIVE;
572 if (gline->gl_lastmod) {
573 if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
576 gline->gl_lastmod = lastmod;
580 if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
581 return 0; /* was active to begin with */
583 /* Inform ops and log it */
584 sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
585 "expiring at %Tu: %s",
586 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
588 cli_name((cli_user(sptr))->server),
589 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
590 gline->gl_user, gline->gl_host ? "@" : "",
591 gline->gl_host ? gline->gl_host : "",
592 gline->gl_expire + TSoffset, gline->gl_reason);
594 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
595 "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
596 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
597 gline->gl_host ? "@" : "",
598 gline->gl_host ? gline->gl_host : "",
599 gline->gl_expire + TSoffset, gline->gl_reason);
601 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
602 gline_propagate(cptr, sptr, gline);
604 return do_gline(cptr, sptr, gline);
607 /** Deactivate a G-line.
608 * @param[in] cptr Peer that gave us the message.
609 * @param[in] sptr Client that initiated the deactivation.
610 * @param[in] gline G-line to deactivate.
611 * @param[in] lastmod New value for G-line last modification timestamp.
612 * @param[in] flags GLINE_LOCAL to only deactivate locally, 0 to propagate.
616 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
617 time_t lastmod, unsigned int flags)
619 unsigned int saveflags = 0;
624 saveflags = gline->gl_flags;
626 if (GlineIsLocal(gline))
627 msg = "removing local";
628 else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
629 msg = "removing global";
630 gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
632 msg = "deactivating global";
634 if (flags & GLINE_LOCAL)
635 gline->gl_flags |= GLINE_LDEACT;
637 gline->gl_flags &= ~GLINE_ACTIVE;
639 if (gline->gl_lastmod) {
640 if (gline->gl_lastmod >= lastmod)
643 gline->gl_lastmod = lastmod;
647 if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
648 return 0; /* was inactive to begin with */
651 /* Inform ops and log it */
652 sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
654 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
656 cli_name((cli_user(sptr))->server),
657 msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
658 gline->gl_user, gline->gl_host ? "@" : "",
659 gline->gl_host ? gline->gl_host : "",
660 gline->gl_expire + TSoffset, gline->gl_reason);
662 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
663 "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
664 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
665 gline->gl_host ? "@" : "",
666 gline->gl_host ? gline->gl_host : "",
667 gline->gl_expire + TSoffset, gline->gl_reason);
669 if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
670 gline_propagate(cptr, sptr, gline);
672 /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
673 if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
674 gline_free(gline); /* get rid of it */
679 /** Modify a global G-line.
680 * @param[in] cptr Client that sent us the G-line modification.
681 * @param[in] sptr Client that originated the G-line modification.
682 * @param[in] gline G-line being modified.
683 * @param[in] action Resultant status of the G-line.
684 * @param[in] reason Reason for G-line.
685 * @param[in] expire Expiration time of G-line.
686 * @param[in] lastmod Last modification time of G-line.
687 * @param[in] lifetime Lifetime of G-line.
688 * @param[in] flags Bitwise combination of GLINE_* flags.
689 * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
692 gline_modify(struct Client *cptr, struct Client *sptr, struct Gline *gline,
693 enum GlineAction action, char *reason, time_t expire,
694 time_t lastmod, time_t lifetime, unsigned int flags)
696 char buf[BUFSIZE], *op = "";
700 assert(!GlineIsLocal(gline));
702 Debug((DEBUG_DEBUG, "gline_modify(\"%s\", \"%s\", \"%s%s%s\", %s, \"%s\", "
703 "%Tu, %Tu, %Tu, 0x%04x)", cli_name(cptr), cli_name(sptr),
704 gline->gl_user, gline->gl_host ? "@" : "",
705 gline->gl_host ? gline->gl_host : "",
706 action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
707 (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
708 (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
709 (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
710 (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>")))),
711 reason, expire, lastmod, lifetime, flags));
713 /* First, let's check lastmod... */
714 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE) {
715 if (GlineLastMod(gline) > lastmod) { /* we have a more recent version */
716 if (IsBurstOrBurstAck(cptr))
717 return 0; /* middle of a burst, it'll resync on its own */
718 return gline_resend(cptr, gline); /* resync the server */
719 } else if (GlineLastMod(gline) == lastmod)
720 return 0; /* we have that version of the G-line... */
723 /* All right, we know that there's a change of some sort. What is it? */
724 /* first, check out the expiration time... */
725 if ((flags & GLINE_EXPIRE) && expire) {
726 if (!(flags & GLINE_FORCE) &&
727 (expire <= CurrentTime || expire > CurrentTime + GLINE_MAX_EXPIRE)) {
728 if (!IsServer(sptr) && MyConnect(sptr)) /* bad expiration time */
729 send_reply(sptr, ERR_BADEXPIRE, expire);
733 flags &= ~GLINE_EXPIRE;
735 /* Now check to see if there's any change... */
736 if ((flags & GLINE_EXPIRE) && expire == gline->gl_expire) {
737 flags &= ~GLINE_EXPIRE; /* no change to expiration time... */
741 /* Next, check out lifetime--this one's a bit trickier... */
742 if (!(flags & GLINE_LIFETIME) || !lifetime)
743 lifetime = gline->gl_lifetime; /* use G-line lifetime */
745 lifetime = IRCD_MAX(lifetime, expire); /* set lifetime to the max */
747 /* OK, let's see which is greater... */
748 if (lifetime > gline->gl_lifetime)
749 flags |= GLINE_LIFETIME; /* have to update lifetime */
751 flags &= ~GLINE_LIFETIME; /* no change to lifetime */
755 /* Finally, let's see if the reason needs to be updated */
756 if ((flags & GLINE_REASON) && reason &&
757 !ircd_strcmp(gline->gl_reason, reason))
758 flags &= ~GLINE_REASON; /* no changes to the reason */
760 /* OK, now let's take a look at the action... */
761 if ((action == GLINE_ACTIVATE && (gline->gl_flags & GLINE_ACTIVE)) ||
762 (action == GLINE_DEACTIVATE && !(gline->gl_flags & GLINE_ACTIVE)) ||
763 (action == GLINE_LOCAL_ACTIVATE &&
764 (gline->gl_state == GLOCAL_ACTIVATED)) ||
765 (action == GLINE_LOCAL_DEACTIVATE &&
766 (gline->gl_state == GLOCAL_DEACTIVATED)) ||
767 /* can't activate an expired G-line */
768 IRCD_MAX(gline->gl_expire, expire) <= CurrentTime)
769 action = GLINE_MODIFY; /* no activity state modifications */
771 Debug((DEBUG_DEBUG, "About to perform changes; flags 0x%04x, action %s",
772 flags, action == GLINE_ACTIVATE ? "GLINE_ACTIVATE" :
773 (action == GLINE_DEACTIVATE ? "GLINE_DEACTIVATE" :
774 (action == GLINE_LOCAL_ACTIVATE ? "GLINE_LOCAL_ACTIVATE" :
775 (action == GLINE_LOCAL_DEACTIVATE ? "GLINE_LOCAL_DEACTIVATE" :
776 (action == GLINE_MODIFY ? "GLINE_MODIFY" : "<UNKNOWN>"))))));
778 /* If there are no changes to perform, do no changes */
779 if (!(flags & GLINE_UPDATE) && action == GLINE_MODIFY)
782 /* Now we know what needs to be changed, so let's process the changes... */
784 /* Start by updating lastmod, if indicated... */
785 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
786 gline->gl_lastmod = lastmod;
788 /* Then move on to activity status changes... */
790 case GLINE_ACTIVATE: /* Globally activating G-line */
791 gline->gl_flags |= GLINE_ACTIVE; /* make it active... */
792 gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
793 pos += ircd_snprintf(0, buf, sizeof(buf), " globally activating G-line");
794 op = "+"; /* operation for G-line propagation */
797 case GLINE_DEACTIVATE: /* Globally deactivating G-line */
798 gline->gl_flags &= ~GLINE_ACTIVE; /* make it inactive... */
799 gline->gl_state = GLOCAL_GLOBAL; /* reset local activity state */
800 pos += ircd_snprintf(0, buf, sizeof(buf), " globally deactivating G-line");
801 op = "-"; /* operation for G-line propagation */
804 case GLINE_LOCAL_ACTIVATE: /* Locally activating G-line */
805 gline->gl_state = GLOCAL_ACTIVATED; /* make it locally active */
806 pos += ircd_snprintf(0, buf, sizeof(buf), " locally activating G-line");
809 case GLINE_LOCAL_DEACTIVATE: /* Locally deactivating G-line */
810 gline->gl_state = GLOCAL_DEACTIVATED; /* make it locally inactive */
811 pos += ircd_snprintf(0, buf, sizeof(buf), " locally deactivating G-line");
814 case GLINE_MODIFY: /* no change to activity status */
818 /* Handle expiration changes... */
819 if (flags & GLINE_EXPIRE) {
820 gline->gl_expire = expire; /* save new expiration time */
822 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
823 "%s%s changing expiration time to %Tu",
825 pos && !(flags & (GLINE_LIFETIME | GLINE_REASON)) ?
826 " and" : "", expire);
829 /* Next, handle lifetime changes... */
830 if (flags & GLINE_LIFETIME) {
831 gline->gl_lifetime = lifetime; /* save new lifetime */
833 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
834 "%s%s extending record lifetime to %Tu",
835 pos ? ";" : "", pos && !(flags & GLINE_REASON) ?
836 " and" : "", lifetime);
839 /* Now, handle reason changes... */
840 if (flags & GLINE_REASON) {
841 MyFree(gline->gl_reason); /* release old reason */
842 DupString(gline->gl_reason, reason); /* store new reason */
844 pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
845 "%s%s changing reason to \"%s\"",
846 pos ? ";" : "", pos ? " and" : "", reason);
849 /* All right, inform ops... */
850 sendto_opmask_butone(0, SNO_GLINE, "%s modifying global %s for %s%s%s:%s",
851 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
852 cli_name(sptr) : cli_name((cli_user(sptr))->server),
853 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
854 gline->gl_user, gline->gl_host ? "@" : "",
855 gline->gl_host ? gline->gl_host : "", buf);
857 /* and log the change */
858 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
859 "%#C modifying global %s for %s%s%s:%s", sptr,
860 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
861 gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "",
864 /* We'll be simple for this release, but we can update this to change
865 * the propagation syntax on future updates
867 if (action != GLINE_LOCAL_ACTIVATE && action != GLINE_LOCAL_DEACTIVATE)
868 sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
869 "* %s%s%s%s%s %Tu %Tu %Tu :%s",
870 flags & GLINE_OPERFORCE ? "!" : "", op,
871 gline->gl_user, gline->gl_host ? "@" : "",
872 gline->gl_host ? gline->gl_host : "",
873 gline->gl_expire - CurrentTime, gline->gl_lastmod,
874 gline->gl_lifetime, gline->gl_reason);
876 /* OK, let's do the G-line... */
877 return do_gline(cptr, sptr, gline);
880 /** Destroy a local G-line.
881 * @param[in] cptr Peer that gave us the message.
882 * @param[in] sptr Client that initiated the destruction.
883 * @param[in] gline G-line to destroy.
887 gline_destroy(struct Client *cptr, struct Client *sptr, struct Gline *gline)
890 assert(GlineIsLocal(gline));
892 /* Inform ops and log it */
893 sendto_opmask_butone(0, SNO_GLINE, "%s removing local %s for %s%s%s",
894 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
895 cli_name(sptr) : cli_name((cli_user(sptr))->server),
896 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
897 gline->gl_user, gline->gl_host ? "@" : "",
898 gline->gl_host ? gline->gl_host : "");
899 log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
900 "%#C removing local %s for %s%s%s", sptr,
901 GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
902 gline->gl_host ? "@" : "", gline->gl_host ? gline->gl_host : "");
904 gline_free(gline); /* get rid of the G-line */
906 return 0; /* convenience return */
909 /** Find a G-line for a particular mask, guided by certain flags.
910 * Certain bits in \a flags are interpreted specially:
912 * <dt>GLINE_ANY</dt><dd>Search both BadChans and user G-lines.</dd>
913 * <dt>GLINE_BADCHAN</dt><dd>Search BadChans.</dd>
914 * <dt>GLINE_GLOBAL</dt><dd>Only match global G-lines.</dd>
915 * <dt>GLINE_LOCAL</dt><dd>Only match local G-lines.</dd>
916 * <dt>GLINE_LASTMOD</dt><dd>Only match G-lines with a last modification time.</dd>
917 * <dt>GLINE_EXACT</dt><dd>Require an exact match of G-line mask.</dd>
918 * <dt>anything else</dt><dd>Search user G-lines.</dd>
920 * @param[in] userhost Mask to search for.
921 * @param[in] flags Bitwise combination of GLINE_* flags.
922 * @return First matching G-line, or NULL if none are found.
925 gline_find(char *userhost, unsigned int flags)
927 struct Gline *gline = 0;
928 struct Gline *sgline;
929 char *user, *host, *t_uh;
931 if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
932 gliter(BadChanGlineList, gline, sgline) {
933 if ((flags & (GlineIsLocal(gline) ? GLINE_GLOBAL : GLINE_LOCAL)) ||
934 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
936 else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
937 match(gline->gl_user, userhost)) == 0)
942 if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
943 *userhost == '#' || *userhost == '&')
946 DupString(t_uh, userhost);
947 canon_userhost(t_uh, &user, &host, "*");
949 gliter(GlobalGlineList, gline, sgline) {
950 if ((flags & (GlineIsLocal(gline) ? GLINE_GLOBAL : GLINE_LOCAL)) ||
951 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
953 else if (flags & GLINE_EXACT) {
954 if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
955 || (!gline->gl_host && !host)) &&
956 (ircd_strcmp(gline->gl_user, user) == 0))
959 if (((gline->gl_host && host && match(gline->gl_host, host) == 0)
960 || (!gline->gl_host && !host)) &&
961 (match(gline->gl_user, user) == 0))
971 /** Find a matching G-line for a user.
972 * @param[in] cptr Client to compare against.
973 * @param[in] flags Bitwise combination of GLINE_GLOBAL and/or
974 * GLINE_LASTMOD to limit matches.
975 * @return Matching G-line, or NULL if none are found.
978 gline_lookup(struct Client *cptr, unsigned int flags)
981 struct Gline *sgline;
983 gliter(GlobalGlineList, gline, sgline) {
984 if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
985 (flags & GLINE_LASTMOD && !gline->gl_lastmod))
988 if (GlineIsRealName(gline)) {
989 Debug((DEBUG_DEBUG,"realname gline: '%s' '%s'",gline->gl_user,cli_info(cptr)));
990 if (match(gline->gl_user+2, cli_info(cptr)) != 0)
994 if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
997 if (GlineIsIpMask(gline)) {
998 if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
1002 if (match(gline->gl_host, (cli_user(cptr))->realhost) != 0)
1006 if (GlineIsActive(gline))
1015 /** Delink and free a G-line.
1016 * @param[in] gline G-line to free.
1019 gline_free(struct Gline *gline)
1023 *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
1025 gline->gl_next->gl_prev_p = gline->gl_prev_p;
1027 MyFree(gline->gl_user); /* free up the memory */
1029 MyFree(gline->gl_host);
1030 MyFree(gline->gl_reason);
1034 /** Burst all known global G-lines to another server.
1035 * @param[in] cptr Destination of burst.
1038 gline_burst(struct Client *cptr)
1040 struct Gline *gline;
1041 struct Gline *sgline;
1043 gliter(GlobalGlineList, gline, sgline) {
1044 if (!GlineIsLocal(gline) && gline->gl_lastmod)
1045 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1046 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1047 gline->gl_host ? "@" : "",
1048 gline->gl_host ? gline->gl_host : "",
1049 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1050 gline->gl_lifetime, gline->gl_reason);
1053 gliter(BadChanGlineList, gline, sgline) {
1054 if (!GlineIsLocal(gline) && gline->gl_lastmod)
1055 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu %Tu :%s",
1056 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1057 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1058 gline->gl_lifetime, gline->gl_reason);
1062 /** Send a G-line to another server.
1063 * @param[in] cptr Who to inform of the G-line.
1064 * @param[in] gline G-line to send.
1068 gline_resend(struct Client *cptr, struct Gline *gline)
1070 if (GlineIsLocal(gline) || !gline->gl_lastmod)
1073 sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu %Tu :%s",
1074 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
1075 gline->gl_host ? "@" : "",
1076 gline->gl_host ? gline->gl_host : "",
1077 gline->gl_expire - CurrentTime, gline->gl_lastmod,
1078 gline->gl_lifetime, gline->gl_reason);
1083 /** Display one or all G-lines to a user.
1084 * If \a userhost is not NULL, only send the first matching G-line.
1085 * Otherwise send the whole list.
1086 * @param[in] sptr User asking for G-line list.
1087 * @param[in] userhost G-line mask to search for (or NULL).
1091 gline_list(struct Client *sptr, char *userhost)
1093 struct Gline *gline;
1094 struct Gline *sgline;
1097 if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
1098 return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
1100 /* send gline information along */
1101 send_reply(sptr, RPL_GLIST, gline->gl_user,
1102 gline->gl_host ? "@" : "",
1103 gline->gl_host ? gline->gl_host : "",
1104 gline->gl_expire + TSoffset, gline->gl_lastmod,
1105 gline->gl_lifetime + TSoffset,
1106 GlineIsLocal(gline) ? cli_name(&me) : "*",
1107 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1108 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1109 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1111 gliter(GlobalGlineList, gline, sgline) {
1112 send_reply(sptr, RPL_GLIST, gline->gl_user,
1113 gline->gl_host ? "@" : "",
1114 gline->gl_host ? gline->gl_host : "",
1115 gline->gl_expire + TSoffset, gline->gl_lastmod,
1116 gline->gl_lifetime + TSoffset,
1117 GlineIsLocal(gline) ? cli_name(&me) : "*",
1118 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1119 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1120 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1123 gliter(BadChanGlineList, gline, sgline) {
1124 send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
1125 gline->gl_expire + TSoffset, gline->gl_lastmod,
1126 gline->gl_lifetime + TSoffset,
1127 GlineIsLocal(gline) ? cli_name(&me) : "*",
1128 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1129 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1130 GlineIsRemActive(gline) ? '+' : '-', gline->gl_reason);
1134 /* end of gline information */
1135 return send_reply(sptr, RPL_ENDOFGLIST);
1138 /** Statistics callback to list G-lines.
1139 * @param[in] sptr Client requesting statistics.
1140 * @param[in] sd Stats descriptor for request (ignored).
1141 * @param[in] param Extra parameter from user (ignored).
1144 gline_stats(struct Client *sptr, const struct StatDesc *sd,
1147 struct Gline *gline;
1148 struct Gline *sgline;
1150 gliter(GlobalGlineList, gline, sgline) {
1151 send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user,
1152 gline->gl_host ? "@" : "",
1153 gline->gl_host ? gline->gl_host : "",
1154 gline->gl_expire + TSoffset, gline->gl_lastmod,
1155 gline->gl_lifetime + TSoffset,
1156 gline->gl_state == GLOCAL_ACTIVATED ? ">" :
1157 (gline->gl_state == GLOCAL_DEACTIVATED ? "<" : ""),
1158 GlineIsRemActive(gline) ? '+' : '-',
1163 /** Calculate memory used by G-lines.
1164 * @param[out] gl_size Number of bytes used by G-lines.
1165 * @return Number of G-lines in use.
1168 gline_memory_count(size_t *gl_size)
1170 struct Gline *gline;
1171 unsigned int gl = 0;
1173 for (gline = GlobalGlineList; gline; gline = gline->gl_next) {
1175 *gl_size += sizeof(struct Gline);
1176 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1177 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1178 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;
1181 for (gline = BadChanGlineList; gline; gline = gline->gl_next) {
1183 *gl_size += sizeof(struct Gline);
1184 *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
1185 *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
1186 *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;